Skip to content

OpenConceptLab/ocl_issues#3001 | deleting a locked repo version is recorded and reported as locked_version_deleted (stacked on #1031) - #1032

Open
snyaggarwal wants to merge 7 commits into
issues-3003-expansion-health-backfillfrom
issues-3001-locked-version-deleted
Open

snyaggarwal wants to merge 7 commits into
issues-3003-expansion-health-backfillfrom
issues-3001-locked-version-deleted

Conversation

@snyaggarwal

@snyaggarwal snyaggarwal commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

For OpenConceptLab/ocl_issues#3001 (epic OpenConceptLab/ocl_issues#2942). It's stacked on #1031, the last PR of the expansion health stack (OpenConceptLab/ocl_issues#3003).

Deleting a repo version deletes the rows that lock expansions to it, so a later add quietly falls back to the latest release. Afterwards nothing shows it happened. This PR records the deletion at delete time and reports it as a health reason. As Jon decided on Nov 3, it isn't an unresolved_repo_versions entry, so FHIR $expand keeps answering.

What changes

  • Record: a new Expansion.deleted_locked_repo_versions field holds one entry per deleted lock: url, repo_url, version, type (Source or Collection), deleted_at, and the version's owner_url and whether it was public. Migration 0075 adds it.
  • At delete time: before the rows go, ConceptContainerModel.delete calls Expansion.record_deleted_locks. That adds an entry to each expansion whose evaluated list (its lock) holds the version, with one UPDATE per deleted version. Deleting a whole source or collection records each of its locked versions.
  • Not recorded: a version an expansion only lists as explicit. Its reference names that version, so it shows up as unresolved at the next build.
  • Health: a new reason, locked_version_deleted, makes the state unhealthy. Its count counts each deleted version once, so a retried delete doesn't double it. Its versions names only those that were public or the collection owner's own, the owner's-view rule the classifier uses for releases. Health is shown to anyone who can view the collection, so another owner's private version is counted but not named. As before, the delete marks those expansions' health not current. Their next change recomputes it, or the backfill does.
  • Clearing: a rebuild from scratch clears the record, because the expansion no longer stands on the deleted locks. Until then the record stays, even after a later add records a new lock for the same repo, because members evaluated under the old lock may still be there.
  • New versions: a collection version created with lock copies HEAD's record, because it's built under HEAD's remaining locks. One created with latest doesn't.
  • Older copies: a save from an in-memory copy loaded before the delete can't wipe the record. The field is excluded from full saves, as the health fields are.
  • Members never change.

Not in this PR

The rebuild offer for the expansion's owner belongs in TBv3: the badge (OpenConceptLab/ocl_issues#3004) and the rebuild offer (OpenConceptLab/ocl_issues#3009). The API already has the rebuild.

Tests

core/collections/tests/tests_deleted_locks.py has 11 tests:

  • deleting a source version, a collection version, and a whole source;
  • a second deletion adds to the record;
  • a version only listed as explicit isn't recorded;
  • members unchanged;
  • a save from an older copy keeps the record;
  • the health reason, which names the owner's own private version but counts another owner's without naming it, and counts a version recorded twice once;
  • a rebuild from scratch clears it;
  • lock copies it into a new version and latest doesn't.

Locally, these and the health and backfill tests pass: 77 tests. So do the 575 tests in core.collections.tests.tests and core.sources.tests. makemigrations --check finds nothing missing, and pylint -j0 core/ exits 0.

🤖 Generated with Claude Code

…n each expansion locked to it, and health reports locked_version_deleted

@snyaggarwal snyaggarwal left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: not ready to merge yet. One important finding, which is Jon's, and one nit on 78ecad0.

What I checked and found sound:

  • The delete path: the record is written before the rows go, after the only-version check. Deleting a whole source covers its versions, and an organization delete goes through source.delete inside a transaction.
  • Explicit versions: only evaluated versions (locks) are recorded. A version only listed as explicit shows up as unresolved anyway.
  • Saves: a save from an older copy can't wipe the record.
  • Clearing: a rebuild from scratch clears it. The re-add after a removal doesn't, because it isn't a from-scratch rebuild.
  • New versions: lock copies HEAD's record and latest doesn't.
  • The field: it isn't exposed by any serializer, only through health. The migration adds the column with a constant default.

The important finding is Jon's point: the reason names deleted versions to anyone who can view the collection, including another owner's private one (inline). It's cheap to fix, so it can go in before Dec 1.

Comment thread core/collections/health.py Outdated
if len(version_ids) > 1 and from_unversioned)
add('duplicate_repo_versions', len(duplicates), repositories=duplicates)
deleted_locks = self.expansion.deleted_locked_repo_versions or []
add('locked_version_deleted', len(deleted_locks), versions=sorted({entry['url'] for entry in deleted_locks}))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important: the reason can expose another owner's private version. Jon's point on OpenConceptLab/ocl_online#549, confirmed here. versions lists every recorded URL, and #1029 serves health to anyone who can view the collection. If org A's public collection was locked to org B's private /orgs/B/sources/Secret/v3/ and B deletes v3, any viewer of A's collection sees that URL.

Fix: at delete time, record whether the version was public and who owned it. List a URL only when the version was public or the collection's owner's own (the owner's-view rule the classifier already uses for releases), and count the rest without naming them.

Nit (optional), on the same line: a retried delete double-counts. count is the number of entries while versions is a set. A version delete isn't atomic and is written to be retried, so a retry appends the same version again. Counting unique URLs would fix it.

…rsions the collection's owner could see, and counts each once
@snyaggarwal

Copy link
Copy Markdown
Contributor Author

Both review points are fixed in the latest commit. The description is updated.

  1. Private versions (important, Jon's point): each record now keeps the deleted version's owner_url and whether it was public. locked_version_deleted names a version only when it was public or the collection owner's own, the owner's-view rule the classifier uses for releases. Another owner's private version is still counted, but not named.
  2. Double count (nit): count now counts each deleted version once, so a retried delete doesn't inflate it.

There are three new tests:

  • another owner's private version is counted but not named;
  • the owner's own private version is named;
  • a version recorded twice counts once.

77 tests pass locally, and pylint -j0 core/ exits 0.

@snyaggarwal snyaggarwal left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: ready to merge once CI passes and #1031 has landed. No findings on 048f8f5.

Both fixes check out:

  • Private versions (Jon's point): each record keeps the deleted version's owner and whether it was public. locked_version_deleted names a version only when it was public or the collection owner's own, the same owner's-view rule the classifier applies to releases, and counts the rest without naming them. Records without the new keys count as private, which is the safe default.
  • Double count: count counts each version once, so a retried delete doesn't inflate it.

The new tests cover both, and I found nothing else across the PR.

Still open, and not part of the code: the owner's rebuild offer is TBv3 work (OpenConceptLab/ocl_issues#3004 and OpenConceptLab/ocl_issues#3009). The merge order is the health stack (#1025, #1026, #1029, #1031), then this PR.

@snyaggarwal
snyaggarwal requested a review from paynejd November 23, 2026 08:30
@snyaggarwal

Copy link
Copy Markdown
Contributor Author

CI fix in the latest commit. Deleting a version queues delete_s3_objects to clean up its exports, and CI has no broker, so 10 of the deleted-lock tests errored. The test class now mocks that task, as the collection tests already do. With Redis unreachable locally, all 11 pass with the mock and 10 fail without it, which is what CI saw.

@paynejd paynejd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review at c242af4. An independent defect pass (Codex, high effort), with every finding checked against the code. The owner's-view point from this morning is fixed and not repeated.

Verdict: approve. Members don't change, and the record matches the Nov 3 decision. One point is worth doing so owners see the warning when it matters.

Worth fixing

  1. The warning waits for the next change. Delete marks health not current and records the lock (common/models.py ~987, ~1010), but queues no recompute. locked_version_deleted appears only when health is next computed. For a HEAD expansion that's the next add, which has already fallen back to the latest release by then. A frozen release never gets one. With the toggle on, queue the debounced compute_expansion_health on commit for each affected expansion: record_deleted_locks already has the ids, and these deletes are rare.
  2. public should use is_publicly_viewable (collections/models.py ~2564-2573). A version keeps its own flag after its HEAD goes private, and the HEAD still exists when the record is written. Suggestion inline. It's the same rule as #1025's point 2.
  3. The record is written before the delete succeeds. record_deleted_locks commits under autocommit before unlink_url_registry_entries and super().delete(). If the delete then fails, the release survives but is recorded as deleted, and nothing clears it. Write the record after super().delete() succeeds, or put both in one transaction.

Minor
4. A from-scratch rebuild clears the record before it runs (~1960). If the rebuild then fails, the record is lost, although a failed rebuild already leaves bigger problems on master. Optional: clear it once the rebuild succeeds.

Two things found on master along the way aren't this PR's, and are filed: ocl_issues#3062 (a hard-deleted user's repositories skip their delete steps) and ocl_issues#3063 ($clone accepts a release as its target).

Comment thread core/collections/models.py Outdated
Comment on lines +2564 to +2573
from core.common.models import BaseModel
deleted_at = timezone.now().isoformat()
for version in repo_versions:
kind = 'collection' if isinstance(version, Collection) else 'source'
through = getattr(cls, f'evaluated_{kind}_versions').through
entry = {
'url': version.uri, 'repo_url': drop_version(version.uri), 'version': version.version,
'type': version.resource_type, 'deleted_at': deleted_at, 'owner_url': version.parent_url,
# health names it only where it was visible, as the version is gone by the time anyone asks
'public': version.public_access in BaseModel.PUBLIC_ACCESS_VALUES}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Point 2: records public with is_publicly_viewable, so a release whose HEAD is private counts as private, and drops the import it no longer needs.

Suggested change
from core.common.models import BaseModel
deleted_at = timezone.now().isoformat()
for version in repo_versions:
kind = 'collection' if isinstance(version, Collection) else 'source'
through = getattr(cls, f'evaluated_{kind}_versions').through
entry = {
'url': version.uri, 'repo_url': drop_version(version.uri), 'version': version.version,
'type': version.resource_type, 'deleted_at': deleted_at, 'owner_url': version.parent_url,
# health names it only where it was visible, as the version is gone by the time anyone asks
'public': version.public_access in BaseModel.PUBLIC_ACCESS_VALUES}
deleted_at = timezone.now().isoformat()
for version in repo_versions:
kind = 'collection' if isinstance(version, Collection) else 'source'
through = getattr(cls, f'evaluated_{kind}_versions').through
entry = {
'url': version.uri, 'repo_url': drop_version(version.uri), 'version': version.version,
'type': version.resource_type, 'deleted_at': deleted_at, 'owner_url': version.parent_url,
# health names it only where it was visible, as the version is gone by the time anyone asks
'public': version.is_publicly_viewable}

# Conflicts:
#	core/collections/models.py
… the delete succeeds, as publicly viewable or not, recomputes health at once, and a rebuild clears it only when done
@snyaggarwal

Copy link
Copy Markdown
Contributor Author

Thanks, Jon. All four points are addressed in the latest commit, on a merge of the backfill branch (c4d0451). The merge conflict, over the health fields, was resolved in its own commit.

  1. The warning waits: once the delete has succeeded, each expansion locked to the deleted version is marked out of date, and its debounced recompute is queued, so locked_version_deleted shows at once, even on a frozen release. Test: after the delete, health is current and carries the reason.
  2. public: now is_publicly_viewable. It's read before the delete, while the HEAD still exists, so a public release under a private HEAD is recorded as private. Test added.
  3. The record before the delete succeeds: the record is planned before the delete, while the locks and the HEAD exist (plan_deleted_locks), and written only after super().delete() succeeds (record_deleted_locks). A delete that fails records nothing. Test added.
    This also moved OpenConceptLab/ocl_issues#3003 | expansion health stored and kept current (PR 2 of 4, stacked on #1025) #1026's repository marking into one hook (Expansion.before_repository_delete). It now runs after the "only version" check, so a refused delete marks nothing. It still marks again on commit.
  4. A failed rebuild (optional): done. A rebuild from scratch clears the record only once it has finished. Test: a rebuild that fails keeps it.

The local Elasticsearch container is down, so CI is the first test run. pylint -j0 core/ shows only the two existing tests_access_control.py R0913 hits.

@paynejd paynejd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Sunny. All four points are met:

  1. The recompute is queued once the delete succeeds.
  2. public is is_publicly_viewable, read while the HEAD still exists.
  3. The record is planned before the delete and written after it.
  4. A failed rebuild keeps the record.

The merge resolution keeps both sides. It has health_revision, the three health fields, deleted_locked_repo_versions and the separately written fields, and the migrations chain 0074 then 0075. #1026's repository marking survives the move into before_repository_delete, which now runs after the only-version check.

One edge, fine to leave: a locked version deleted between a rebuild re-adding it and the rebuild clearing the record is cleared along with the rest.

Approved, once CI is green.

@snyaggarwal

Copy link
Copy Markdown
Contributor Author

CI: the failing health tests came from the branches below. Their fixes are merged in (3e90372). No change on this branch.

@paynejd paynejd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review lead (Claude, for Jon), round 3

This branch hasn't changed since round 2 apart from merging in the fixes below it. CI is green at 3e90372. Approved for this PR's own changes; it merges after #1026.

@snyaggarwal

Copy link
Copy Markdown
Contributor Author

Merged #1031's latest (26d4327), which brings in the fixes below it. No change on this branch.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants