Skip to content

OpenConceptLab/ocl_issues#3003 | expansion health backfill (PR 4 of 4, stacked on #1029) - #1031

Open
snyaggarwal wants to merge 9 commits into
issues-3003-expansion-health-apifrom
issues-3003-expansion-health-backfill
Open

snyaggarwal wants to merge 9 commits into
issues-3003-expansion-health-apifrom
issues-3003-expansion-health-backfill

Conversation

@snyaggarwal

@snyaggarwal snyaggarwal commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

PR 4 of 4 for OpenConceptLab/ocl_issues#3003 (epic OpenConceptLab/ocl_issues#2942): the backfill, decision 8. It's stacked on #1029. Expansions built from here on store their health at build; this command computes it for the ones built before.

python manage.py backfill_expansion_health --all --output report.jsonl           # dry run
python manage.py backfill_expansion_health --all --apply --output applied.jsonl
python manage.py backfill_expansion_health <expansion or collection version URI> ...

What it does

  • Which expansions: --all takes every active expansion of an active collection version that has no health yet, whose health isn't current, or whose health was computed with older rules (rules_version). --force takes the rest too.
  • Skipped, and reported on lines of their own with totals:
    • expansions being built, whether listed by --all or passed as URIs, because their build stores health when it ends. A build that died with the flag still set shows up here.
    • with --all, expansions that a build made current after the run listed them.
  • Dry run (the default): computes each expansion's health with the classifier and stores nothing.
  • --apply: stores it through Expansion.update_health, the same path builds use, so a change while it computes still marks the expansion out of date. It refuses to run while EXPANSION_HEALTH_TOGGLE is off, because the stored health wouldn't be kept current.
  • Resuming and load: --start-after-id and --limit resume a long run; --pause waits between expansions to keep production load down.

The report

  • One line per expansion: each expansion's state before and after, and its reasons.
  • The JSON file: with --output, each line holds the stored state before, the full health summary and the seconds it took.
  • Failures: each is reported on its own line, and the run goes on.
  • Totals: at the end, counts by state and by reason.

Order on production

  1. Rebuild every expansion's explicit and evaluated repo version lists ocl_issues#3005's rebuild_expansion_version_lists (OpenConceptLab/ocl_issues#3005 | rebuild_expansion_version_lists rebuilds expansions' explicit and evaluated version lists, dry run by default #1030) runs first, because health reads the lists it rebuilds.
  2. EXPANSION_HEALTH_TOGGLE goes on in production.
  3. This command's dry run, then --apply with approval.

The staging run comes first, once PR 3 is on staging.

Tests

core/collections/tests/tests_health_backfill.py has 14 tests:

  • the dry run stores nothing;
  • --apply stores current health;
  • --apply is refused with the toggle off;
  • which expansions --all takes, and --force;
  • expansions being built are skipped and reported, by --all and by URI;
  • an expansion made current after the run listed it is skipped;
  • --pause waits only between computations;
  • resuming and the limit;
  • a collection version URI;
  • failures in a dry run and while applying;
  • the JSON report;
  • the arguments.

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

🤖 Generated with Claude Code

…ealth for expansions that need it, dry run by default

@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 after CI, once #1029 lands. Three nits on 64e8a65, all about expansions that change or are stuck while a run is under way. Fixes follow in this PR.

What I checked:

  • Selection: --all takes expansions with no health, health not current, or older rules.
  • Dry run: it's read-only, since the classifier's compute() stores nothing.
  • --apply: it goes through update_health, so a concurrent change still marks the expansion out of date and queues its recompute. It's refused while the toggle is off.
  • Reliability: failures are isolated and resuming works.
  • Order on production: the PR body states it, after #1030's rebuild.

Nothing is commercial, and the conventions are met.

return expansion

def backfill(self, expansion_id):
expansion = Expansion.objects.filter(id=expansion_id).select_related('collection_version').first()

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.

Nit (optional): an expansion that becomes current mid-run is still recomputed. --all lists the ids once, so one that a build makes current during a long run gets computed again when its turn comes. Unless --force is given, backfill() could re-check, skip it, and count it as skipped.


def get_expansion_ids(self):
if not self.options['all']:
return [self.get_expansion(uri).id for uri in self.options['uris']]

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.

Nit (optional): explicit URIs don't skip expansions being built. --all skips them, but a URI on the command line is computed mid-build. With --apply, that stores health judged from part of the members until the build's own update replaces it. The same check belongs in backfill().

if not self.options['all']:
return [self.get_expansion(uri).id for uri in self.options['uris']]
expansions = Expansion.objects.filter(
is_active=True, collection_version__is_active=True, is_processing=False,

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.

Nit (optional): expansions skipped as being built aren't counted. One whose build died with is_processing still set is never backfilled, and the totals don't show it. Counting and listing them would surface a stuck flag in the dry run.

…sions being built, and ones a build made current since the run listed them
@snyaggarwal

Copy link
Copy Markdown
Contributor Author

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

  1. Made current mid-run: unless --force is given, --all now re-checks each expansion when its turn comes. One that's current with the current rules is skipped as now current.
  2. URIs mid-build: the check for an expansion being built now runs in backfill(), so a URI given on the command line is skipped too.
  3. Skips counted: --all now lists expansions being built rather than filtering them out. Each one gets a SKIPPED <uri>: being built line and is counted in skipped_being_built, so a stuck flag shows up in the dry run.

There are three new tests, so 13 in all. They pass locally, and pylint is clean.

@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 #1029 has landed. One optional nit on 1992835.

All three fixes check out:

  • Being built: an expansion being built is skipped and reported, whether it comes from --all or a URI, so a stuck flag shows in the dry run.
  • Made current mid-run: with --all, an expansion that a build made current after the run listed it is skipped, unless --force is given.
  • Report: skips get their own SKIPPED lines, JSON lines and totals.

I went over the whole PR again: selection, the read-only dry run, --apply through update_health with the toggle guard, failures, resuming and the report. I found nothing else.

The production order still stands, as the PR body says: #1030's rebuild, then the toggle, then this dry run, then --apply with approval.

mode = 'APPLY' if options['apply'] else 'DRY RUN'
self.stdout.write(f'{mode}. Rules version {RULES_VERSION}.')
for index, expansion_id in enumerate(self.get_expansion_ids()):
if index and options['pause']:

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.

Nit (optional): --pause also waits before expansions that turn out to be skipped or gone. The sleep is in the loop, before backfill() decides to skip. A resumed run over many expansions made current since then waits for no work. Sleeping only after an expansion whose health was actually computed would avoid that.

@snyaggarwal

Copy link
Copy Markdown
Contributor Author

The --pause nit is fixed in the latest commit. The wait now happens right before an expansion's health is computed, and only if one has been computed earlier in the run. An expansion that's skipped or gone costs no wait. A new test covers it, so there are 14. They pass locally, and pylint is clean.

@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 #1029 has landed. No findings on ae9b874.

  • The --pause fix checks out: the wait now sits right before a computation and only after an earlier one, so skipped or missing expansions cost nothing. The new test runs one computed, one skipped and one computed expansion, and checks for exactly one wait.
  • The rest of the PR is unchanged since the last review, which found nothing else.

Still open, and not part of the code:

  • Merge order: #1025, #1026, #1029, then this PR. All of them wait on Jon.
  • On production:
    1. #1030's rebuild of the version lists;
    2. the health toggle on;
    3. this command's dry run;
    4. --apply with approval, after the staging run.

@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 ae9b874. An independent defect pass (Codex, high effort), with every finding checked against the code. Nobody had read this PR before today's pass.

Verdict: approve. The dry run writes nothing but the --output file. --apply writes only health and health_is_current, through .update(), so members can't change. A plain rerun of --all --apply picks up failures, because failed rows are left not current. Nothing here blocks the merge or the staging run.

Worth fixing before the production run

  1. An interrupted --apply leaves one row current but stale. update_health writes health_is_current=True before computing. Ctrl-C or SIGTERM isn't caught, so that expansion keeps its old health marked current, and later runs skip it. It's the same root as #1026's point 3. Until that's fixed, add a runbook line: after an interrupted run, rerun --force from the last completed id with --limit 1.
  2. Pause before reading, not after. --pause sleeps between loading the expansion and computing (~75-91), so a rebuild that finishes in that window gets overwritten. Suggestion inline.
  3. --limit 0 means no limit (~62). Suggestion inline.

Minor
4. Print each expansion's id on its stdout line, and exit non-zero when any failed. Today the id that --start-after-id needs is only in the --output JSON.
5. The tests use only empty expansions. Please add one dry-run and one apply test on an expansion with a concept and a mapping, asserting that member ids are unchanged and that a dry run leaves health null.

expansions = expansions.filter(
Q(health__isnull=True) | ~Q(health_is_current=True) | ~Q(health__rules_version=RULES_VERSION))
ids = expansions.order_by('id').values_list('id', flat=True)
return list(ids[:self.options['limit']] if self.options['limit'] else ids)

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 3: --limit 0 runs nothing instead of everything.

Suggested change
return list(ids[:self.options['limit']] if self.options['limit'] else ids)
return list(ids[:self.options['limit']] if self.options['limit'] is not None else ids)

Comment on lines +74 to +86
def backfill(self, expansion_id):
expansion = Expansion.objects.filter(id=expansion_id).select_related('collection_version').first()
if not expansion:
return
skipped = self.get_skip_reason(expansion)
if skipped:
self.totals[f'skipped_{skipped.replace(" ", "_")}'] += 1
self.write({'expansion': expansion.uri, 'id': expansion.id, 'skipped': skipped})
return
# waits only between computations: a skip costs nothing
if self.has_computed and self.options['pause']:
time.sleep(self.options['pause'])
self.has_computed = True

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: waits before reading the next expansion, not between reading and computing, so a build that ends during the wait isn't overwritten.

Suggested change
def backfill(self, expansion_id):
expansion = Expansion.objects.filter(id=expansion_id).select_related('collection_version').first()
if not expansion:
return
skipped = self.get_skip_reason(expansion)
if skipped:
self.totals[f'skipped_{skipped.replace(" ", "_")}'] += 1
self.write({'expansion': expansion.uri, 'id': expansion.id, 'skipped': skipped})
return
# waits only between computations: a skip costs nothing
if self.has_computed and self.options['pause']:
time.sleep(self.options['pause'])
self.has_computed = True
def backfill(self, expansion_id):
# after a computation, waits before reading the next expansion: a build that ends meanwhile isn't overwritten
if self.has_computed and self.options['pause']:
time.sleep(self.options['pause'])
self.has_computed = False
expansion = Expansion.objects.filter(id=expansion_id).select_related('collection_version').first()
if not expansion:
return
skipped = self.get_skip_reason(expansion)
if skipped:
self.totals[f'skipped_{skipped.replace(" ", "_")}'] += 1
self.write({'expansion': expansion.uri, 'id': expansion.id, 'skipped': skipped})
return
self.has_computed = True

…s only, waits before reading, honours --limit 0, prints ids and exits non-zero on a failure
@snyaggarwal

Copy link
Copy Markdown
Contributor Author

Thanks, Jon. All five points are addressed in the latest commit, on a merge of the API branch (0f61439).

  1. An interrupted --apply: fixed at the root in OpenConceptLab/ocl_issues#3003 | expansion health stored and kept current (PR 2 of 4, stacked on #1025) #1026 (point 3). update_health no longer marks health current before computing. It stores the result and True in one conditional update, only if nothing marked the expansion meanwhile. An interrupted run therefore leaves the row not current, and a plain rerun picks it up, so no runbook line is needed. If an expansion changes while it computes, the run reports "changed while it computed; a recompute is queued".
  2. Pausing: applied. After a computation, the command waits before reading the next expansion, so a build that ends in the meantime isn't overwritten.
  3. --limit 0: applied. It runs nothing.
  4. Ids and exit status: every stdout line now starts with the expansion's id, for --start-after-id. A run with any failure still reports every expansion, then exits non-zero.
  5. Tests with members: a dry run and an --apply on an expansion with a concept and a mapping. Both leave the member ids unchanged; the dry run leaves health null, and --apply stores it as current.

Also from #1025, point 1: health is kept for each version's default expansion only. --all now lists default expansions only, and a URI for any other expansion is skipped as "not a default expansion". Tests cover both.

The local Elasticsearch container is down, so CI is the first test run; pylint is clean.

@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 five points are met:

  1. An interrupted --apply leaves the row not current, through #1026's revision check.
  2. The command pauses before reading the next expansion.
  3. --limit 0 runs nothing.
  4. Every line starts with the id, and a failure exits non-zero after the totals.
  5. The tests with members show the members unchanged.

Taking default expansions only with --all is right. My approval stands.

Two notes:

  • CI: two of this PR's own tests fail (test_all_takes_only_expansions_that_need_it, and an error in test_all_skips_an_expansion_a_build_made_current_since_the_run_listed_it). An expansion that should be current reads None, so this is the same setUp order as #1026's five failures: the default is set after the expansion is built.
  • A misleading message: get_apply_error says "computing health failed; see the log" when the store lost to a repository mark. That mark sets None, not False, so the message names a failure that was never logged. Nothing is lost: it counts as failed, and a rerun picks it up.

@snyaggarwal

Copy link
Copy Markdown
Contributor Author

CI fix (6eb76f7): test_all_takes_only_expansions_that_need_it and test_all_skips_an_expansion_a_build_made_current_since_the_run_listed_it built expansions that became the default only after seeding, so the "current" one had no health. The second test then summed a mocked compute. The tests now claim the default before seeding. Merged the fixes below (9b2fd6b).

@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

Thanks, Sunny. The backfill tests now claim the default before seeding, so a just-built expansion is current. CI is green at 9b2fd6b. Every round-2 point is met. Approved for this PR's own changes; it merges after #1026.

The get_apply_error wording note from round 2 still applies; fix it or leave it as you prefer.

@snyaggarwal

Copy link
Copy Markdown
Contributor Author

The get_apply_error wording is fixed (915ab93). The command now reads health_revision before update_health, and on a store that returned nothing it compares the revision again:

  • The revision moved (any mark or store, whether it left the row False or None): changed while it computed, so it was not stored.
  • It didn't move: computing health failed; see the log, which is now only a real, logged failure.

New test: a repository-style mark that sets None during compute is reported as changed, not failed. Merged #1029 (9a91614). Local Elasticsearch is down, so CI is the first run.

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