Skip to content

OpenConceptLab/ocl_online#452 | An expansion keeps the rules it was built under until it's rebuilt - #1136

Open
paynejd wants to merge 7 commits into
fhir-452-b-codesfrom
fhir-452-b-kept-rules
Open

paynejd wants to merge 7 commits into
fhir-452-b-codesfrom
fhir-452-b-kept-rules

Conversation

@paynejd

@paynejd paynejd commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

Merge after #1094, and before EXPANSION_FHIR_RULES_TOGGLE (#1110) is turned on anywhere. With that switch off, members, saved results and recorded versions stay as they are.

What it means for people

A collection keeps the rules it was built with until it's rebuilt. New collections and new releases are built under the rules the switch gives.

  • Turning the switch on changes no existing collection, and neither does the next edit of one.
  • In a collection built under FHIR's rules, an edit changes only what the edited references select. That holds when the edit brings in a second version of a source, and when it removes a reference.
  • After turning the switch off again, an edit works as OCL's always have. It rebuilds nothing.

Two limits:

  • A HEAD built before the rules shows one version of a concept where its release may show two. It stays so until someone rebuilds it.
  • A rebuild also moves it to newer releases of the sources it names no version of. There is no way to get only FHIR's rules.

The problem

With the switch on, #1090 keeps two versions of one source apart in an expansion. Every HEAD that exists was built without that. The first edit that met two versions of a source rebuilt the whole HEAD, so members nobody touched could change or go. Every removal evaluated all the references that were left, with the same effect. So did the first edit after the switch was turned off again.

An edit of a very large HEAD was also slow. Three queries compared all its recorded memberships with each other, which takes PostgreSQL minutes above about 130,000 of them (at the default work_mem). Two whole releases of a source with 66,000 concepts are enough for that.

How it works

Expansion.versions_apart now says which rules an expansion was built under. There is no migration.

Value Built An edit
null without FHIR's rules keeps no versions apart, and never converts it
False under them, with one version of each source records each member in its source's one version when a second one comes in, then sets True
True under them, versions kept apart keeps them apart, as now
  • What sets False or True: a rebuild, a release, a new expansion, or the first references a collection is given.
  • An add takes away nothing it doesn't select. The collection's other exclusions apply only to what the add brings in.
  • Which memberships stay at the end of a build is worked out in Python, from one read.
  • A removal uses the same task as before. On a HEAD built under the rules, the task is told what the removed references selected, and adds back only that, where other references select it too.

What isn't obvious

  • How a reference itself is read follows the switch, whichever HEAD it's in: a version like 1.0.x, a filter, a code that ignores case. Only keeping versions apart follows the HEAD. So in an old HEAD, a new reference to 1.0.x selects what that release holds, and an edit can record the release that such a reference resolves to.
  • On a default HEAD with one version of each source, the exclusions that were there act through what OCL saved for them, as they do today. One that would also match a concept added to the source since then takes effect at the next rebuild.
  • A removal takes the concepts its references selected in every version, and its task adds them back as the remaining references select them now. If one of those no longer resolves, a concept it shared with the removed reference can go, as at a rebuild.
  • The removal's task carries the list of what the removed references selected. For 100,000 concepts that's 0.4 MB stored in the task's row and 1.3 MB on the queue, as large as the list a removal already sends to the search index. Nothing limits it, and the tasks API doesn't show it.
  • An old collection that is emptied and filled again at once, as an import can do, may stay an old one until a rebuild. Its new references count as its first only once its members have been counted again.
  • Two rare cases are left, and a rebuild repairs both. A removal in the seconds after a collection's default expansion was switched can lose a concept that another reference selects too. And when a removal's task fails after it has added members, and runs again after a new release of the source, a concept can be listed in two versions by one reference.
  • After the switch is off again, what the rules selected stays until a rebuild, with one version of each concept.
  • A whole build now reads the switch once, where most read it not at all. With the switch off, an add to a HEAD whose collection has no counted members runs one small query more. Every other statement is as it was: I compared 9,000, from 160 edits, releases and rebuilds, with OpenConceptLab/ocl_online#452 | An expansion evaluation narrowed to some codes; an import's codes matched as a code reference matches them #1094's.

Where to read

  1. core/collections/member_versions.py: the last three points of the docstring, then decide, is_whole and record_members, the methods from leaves_found to wanted_ids, and dedupe.
  2. core/collections/models.py: Expansion.delete_references and readd_selected, then the changes in add_references.
  3. core/common/tasks.py and core/tasks/models.py: the removal's task takes a third argument. One that has it no longer clears the record of an earlier one that failed, which may have been of other concepts.

Tests

core/collections/tests/tests_edits.py is new, with 39 tests. Three of them make random edits of random expansions with fixed seeds, while the sources change:

  • An edit of a HEAD built before the rules leaves what it leaves with the switch off.
  • An edit of one built under them changes only what its references select. Once every reference is removed, nothing is left.
  • A removal leaves what a rebuild gives, when the sources haven't changed.

Outside the suite I ran each on 250 more expansions: 1,665 edits and 250 removals, with no difference.

Eleven tests of #1090 and #1094 change, because an edit no longer rebuilds, and a build now reads the switch once:

Test What it checks now
test_a_reference_added_to_an_expansion_built_without_the_rules, now …_built_with_one_version The same recording of members, on a HEAD built under the rules with one version
test_built_without_the_rules_then_added_to_under_them The add changes nothing else and records nothing. A rebuild keeps the versions apart
test_a_rebuild_for_new_rules_keeps_its_version_locks, now test_an_edit_once_they_are_off_again_rebuilds_nothing The members and the recorded version stay
test_a_rebuild_for_the_rules_drops_the_locks_no_reference_used An edit leaves the recorded versions. A rebuild drops the unused one
test_a_concept_version_s_lock_goes_when_other_references_took_another The same, at a rebuild instead of an edit
test_a_rebuild_for_the_rules_drops_the_value_set_locks_no_reference_used The same, at a rebuild instead of an edit
test_a_rebuild_for_new_rules_reads_only_the_references_it_was_given The same, for a build that starts from saved results instead of a removal's re-add
test_one_version_a_source_never_asks_the_toggle, now …_asks_the_toggle_once Such a build reads the switch once, and records that it wasn't built under the rules
test_the_toggle_is_read_only_for_a_wildcard_or_a_declared_algorithm, now test_a_build_reads_the_toggle_once One read, with a declared algorithm or without
test_version_parameters_read_the_toggle_once_a_build_at_most, now test_with_version_parameters_too One read, for each kind of version parameter
test_code_references_ask_for_the_rules_only_for_a_code_with_no_exact_match, now …_ask_for_the_rules_once One read, and still no more queries when every code matches exactly

Time on a HEAD of 40,000 codes with one reference each, on my machine:

Edit Before Now
The first edit of a HEAD built before the rules 23 s 1.7 s
The edit that brings a second version into a HEAD built under them 23 s 3.4 s
A removal from a HEAD built under them 24 s 3 s
The first edit after switching off again 25 s 1.5 s

On a HEAD with two whole releases of 100,000 codes (200,000 memberships):

Edit Before Now
An add to a HEAD that keeps them apart 232 s 2 s
A removal of one code from it 198 s 5 s
A rebuild 16 s 14 s

Two are slower. A removal from a HEAD of 40,000 codes with one reference each, built before the rules, takes 53 s, as it does today with the switch off (23 s before this change, with it on). Removing one of two whole releases of 40,000 codes takes 9.9 s, where it took 7.5 s.

The full suite passes (4,818 tests, and 822 with Elasticsearch), coverage is 97%, and lint is clean. It also passes on the branches above this one with this merged in, and no test of theirs changes.

🤖 Generated with Claude Code

https://claude.ai/code/session_01AaZ4qVQviVc1p6MQwqUUed

paynejd and others added 7 commits December 4, 2026 15:47
…uilt under until it's rebuilt

Under FHIR's rules, the first edit that met two versions of one source rebuilt the whole HEAD, so members nobody touched
could change or go. Now an edit of an expansion built before the rules does what it does without them, and an edit of one
built under them changes only what the references it adds or removes select.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qVQviVc1p6MQwqUUed
…rameters, and can run again if it fails

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qVQviVc1p6MQwqUUed
…rsion, and saves what its re-add selects

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qVQviVc1p6MQwqUUed
…ps versions apart takes seconds, not minutes

Past about 130,000 memberships a NOT IN over them no longer fits in PostgreSQL's work_mem, and reads them all again for
each one: 232 s for an add to an expansion with 200,000, and 2 s now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qVQviVc1p6MQwqUUed
…from one read of them

A join was as fast only where PostgreSQL has statistics, which a new expansion's memberships don't have yet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qVQviVc1p6MQwqUUed
… memory, not in each reference's queries

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qVQviVc1p6MQwqUUed
…s the database for them

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qVQviVc1p6MQwqUUed
@paynejd
paynejd marked this pull request as ready for review December 5, 2026 00:53
@paynejd
paynejd requested a review from snyaggarwal December 5, 2026 00:53

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.

1 participant