Skip to content

OpenConceptLab/ocl_online#772 | Adding references merges unresolved entries instead of replacing them - #1069

Open
snyaggarwal wants to merge 4 commits into
masterfrom
ocl_online-772-merge-unresolved
Open

snyaggarwal wants to merge 4 commits into
masterfrom
ocl_online-772-merge-unresolved

Conversation

@snyaggarwal

Copy link
Copy Markdown
Contributor

Fixes OpenConceptLab/ocl_online#772.

What changed
Expansion.add_references now merges its unresolved entries into the expansion's existing unresolved_repo_versions instead of replacing them:

  • Each add records which entries it evaluated: a reference's system (with its version), its value sets, its resource version (when re-evaluating), and the system-version parameter entries (when it evaluates those).
  • Earlier entries the add did not evaluate are kept. Entries it did evaluate are replaced by what it finds now, so an entry drops once its reference resolves.
  • Duplicates are removed.
  • A full rebuild still starts from empty, and it now also saves the empty list when nothing is unresolved. Before, that [] was only set in memory.

Why
Before this change, each add overwrote the field with only what that add found. So adding A (X unresolved) and then B (Y unresolved) recorded only [Y]. The serializer, expansion health and FHIR $expand all read this field.

Tests
New tests in ExpansionTest:

  • two adds with different unresolved references leave both recorded, and re-adding one adds no duplicate;
  • an add that resolves an earlier unresolved reference drops its entry and keeps the other;
  • a rebuild with nothing unresolved clears the stored entries.

Focused pylint on the changed files passes (exit 0). The local Docker stack is down, so the tests haven't run locally. This relies on CI's Tests job.

🤖 Generated with Claude Code

@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.

Review: request changes

1. Blocker: the new test fails, and re-adding a reference moves its entry to the end.

  • Where: merge_unresolved_entries (core/collections/models.py, ~L2405) builds [*kept, *found]. A re-evaluated reference's old entry is filtered out of kept and comes back at the end through found.
  • Failure: test_add_references_keeps_earlier_unresolved_entries adds one, adds two, then re-adds one. The result is [two, one], but the test asserts [one, two].
  • Why it matters beyond the test: FHIR $expand raises on unresolved_repo_versions[0]. So re-adding a reference can change which reference $expand names first, which is the ticket's point.
  • Fix: replace an evaluated key's entry where it sits, and append only entries that are new.

2. Should-fix: a stale merge base can bring back entries a concurrent delete dropped.

  • Where: the merge starts from the in-memory self.unresolved_repo_versions, loaded when the add starts.
  • Failure: reference R is deleted while an add runs, and drop_unresolved_entries_of_deleted_references writes []. The add then saves R's entry back, because it didn't evaluate R. Health and $expand keep reporting a reference that no longer exists, until a rebuild.
  • Fix: re-read the stored list just before merging, and use it as the merge base and as the change baseline. Skip this on the rebuild path, which must still start from [].

Nits

  • Entry keys for reference.system and reference.valueset ignore namespace. This matches drop_unresolved_entries_of_deleted_references, and is rare.
  • The tests cover only reference.system entries. Value-set, system-version parameter and resource-version entries, and the delete path (add_references(..., attempt_reevaluate=False)), are untested.
  • The "only what this add found" comment above the save now sits over a merged list. It should say something like "only this field".

Checked, no issue:

  • Entry keys and evaluated keys line up for all four entry types.
  • should_reevaluate=False keeps the system-version and resource-version entries.
  • The rebuild paths still start from empty, and now save [].
  • The resolve test and the rebuild test would fail before the change and pass after it.

@snyaggarwal

Copy link
Copy Markdown
Contributor Author

Fixed in 396ba8e:

  • Order (blocker): merge_unresolved_entries now keeps earlier entries in place. An evaluated key's entries are replaced where they were, and only new keys are appended. test_merge_unresolved_entries_keeps_their_order covers this, and test_add_references_keeps_earlier_unresolved_entries now passes as written.
  • Stale merge base: the merge now starts from the stored row, read just before the save, and the change check compares against that same row. A rebuild still starts from []. Covered by test_add_references_merges_into_the_stored_entries_not_a_stale_copy.
  • Comment: now reads "only this field: …".
  • Not changed: the namespace in keys (kept consistent with drop_unresolved_entries_of_deleted_references) and the extra entry-type tests.

@snyaggarwal
snyaggarwal marked this pull request as ready for review November 25, 2026 12:26
@snyaggarwal
snyaggarwal requested a review from paynejd November 25, 2026 12:26

@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.

Review round 2 (Expansion Health epic, reviewed with #1070–#1073): approve with nits

The fixes from round 1 hold, at 2bba48c:

  • Order: merge_unresolved_entries replaces an evaluated key's entries where they were and appends only new keys, so FHIR $expand's unresolved_repo_versions[0] stays put when a reference is re-added.
  • Stale base: the merge starts from the stored row, read just before the save. The rebuild path still starts from [].

Checked, no issue

  • The evaluated keys line up with what each entry type writes:
    • reference.system is keyed by (url, version);
    • a value set by its URL;
    • a resource version by the expression, and only when re-evaluating, which is the only time one is written;
    • system-version is one key, added only when it's re-evaluated.
  • A reference that computes against a system-version entry writes no reference.system entry, but its key is still evaluated. So its old entry drops, which is right.
  • On an auto-generated expansion, a non-forced add keeps the resource-version and system-version entries.
  • Exclude references go through get_ref_results too, so their keys count as evaluated.
  • No logical overlap with #1073, which only touches unresolved_reason in the not-re-evaluating branch.

Nits

  1. Merge conflict with #1073 in core/collections/tests/tests.py. Both add tests right after test_add_references_marks_pinned_reference_no_version_holds (~L4420). It's textual only: keep both blocks. Whichever merges second needs a master merge.
  2. Read-then-write isn't atomic. Two adds that overlap on one expansion can still lose one's entries, since each merges into the row it read. That window is far smaller than before, and adds are mostly serialized by is_processing. FYI only; select_for_update on that read would close it if it ever matters.
  3. Still open from round 1, and fine to leave: namespace isn't in the keys, and there are no tests for value-set, resource-version or system-version entries.

CI: Pylint passes; Tests is still running.

@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 1

Verdict: CHANGES_REQUESTED (head 2bba48c). One small fix before merge: an add that accepts no references can now change the stored unresolved list without marking health (item 1 below). The rest are nits.

Reviewed against OpenConceptLab/ocl_online#772. All three acceptance criteria are met: an add merges and de-duplicates, an add that resolves an entry drops it, and a full rebuild still starts from empty.

Earlier points

  • Re-adding moved an entry to the end (blocker): met. merge_unresolved_entries replaces an evaluated key's entries in place and appends only new keys (models.py ~L2390); test_merge_unresolved_entries_keeps_their_order covers it.
  • Stale in-memory merge base (should-fix): met. The merge starts from the stored list read just before the save, and a rebuild starts from []; test_add_references_merges_into_the_stored_entries_not_a_stale_copy covers it.
  • Comment above the save: met.
  • Namespace not in the entry keys: accepted, as it matches drop_unresolved_entries_of_deleted_references.
  • No tests for value-set, resource-version or system-version entries: not met, and fine to leave as a nit.
  • Read-then-write is not atomic: accepted as FYI.

New

  1. To fix before merge (small): an add that accepts no references can change the unresolved list without marking health out of date.
    • Collection.add_references calls expansion.add_references(added_references) even when added_references is empty (every reference rejected, for example as a duplicate), but marks health only if added_references: (models.py ~L309-311).
    • The add still re-evaluates the system-version parameter entries (the should_reevaluate and system_versions block). Before this PR that never cleared anything, because the field was written only when the add found something unresolved. Now the entry drops once the version resolves, and a health summary stored as current keeps counting it until the next change.
    • Fix, in Expansion.add_references right after the save of a changed list: Expansion.mark_health_not_current([self.id]). It bumps the revision and queues nothing, and a build's closing update_health still computes afterwards.
  2. Nit: SYSTEM_VERSION_UNRESOLVED_TYPE repeats SYSTEM_VERSION_PARAMETER_TYPE in core/collections/health.py:31. One constant, imported from one place, keeps the two from drifting.
  3. Nit: an earlier entry and a new one for the same lookup can differ only as version: null against version: "". The key treats them as one, but the final de-duplication compares the JSON, so both stay. Normalising the version before the JSON identity check would collapse them.

CI: Pylint and Tests pass.

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