Skip to content

The unit job runs as six parts that each collect only their own files, their coverage combined in one job - #297

Merged
zlareb1 merged 19 commits into
mainfrom
ci/unit-parts
Nov 7, 2026
Merged

zlareb1 merged 19 commits into
mainfrom
ci/unit-parts

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

Changelog entry (one or two sentences a user of Loom can read without the diff; the release pull request collects these into the release notes):

...

Why

With pytest-xdist the unit job runs on both cores of its runner, and it still takes about 30 minutes (29:39 and 30:35 for the pytest step). The repository's runners have two cores, so going faster means more runners. The unit job is now six parts, and a unit-coverage job combines their coverage.

A first version split the suite the way the integration job does: each part collected the whole suite, then deselected the other parts' tests by test count. On CI those parts ran from 5:56 to 13:55. Two causes:

  • Every part spent about four minutes collecting all 27,500 tests under coverage before dropping five sixths of them.
  • Equal test counts are not equal time: test_delete_version_sql and test_grep_plan each take several minutes, while most files take under a second.

So the shared split (tests/_shards.py) changes in two ways:

  • Before collection. LOOM_TEST_SHARD=k/n picks a part's files before collection, through pytest_ignore_collect in tests/conftest.py, so a part imports and collects only its own files. A file or node id named on the command line is never offered to that hook, so pytest_collection_modifyitems also deselects what such an argument brought in from another part. A test file the walk does not find runs in part 1. Either way every test file runs in exactly one part.
  • By seconds. Files are dealt heaviest first by the seconds tests/shard_weights.json records for each. A file it does not list weighs its count of test functions times the recorded seconds per test function, or one second each when nothing is recorded, as for every integration file. The weights only balance the parts; they cannot drop or repeat a file. unit-coverage uploads a shard-weights artifact rebuilt from the parts' JUnit reports, for refreshing the committed file when the parts drift apart (python -m tests._shards junit-*.xml).

In CI:

  • unit is a matrix of six parts, each pytest tests/unit -n auto with LOOM_TEST_SHARD=k/6 and its own coverage data file (COVERAGE_FILE=.coverage.unit-k) and JUnit report.
  • unit-coverage runs coverage combine, the 40% floor (coverage report --fail-under=40) and the per-module floors on the combined coverage.json. It also runs the bench harnesses and the generic integration sample, the steps the old job ran after its tests.

Parallel parts exposed these test problems, each fixed at its source:

  • SQL split bound. The extraction tests' bounded-time timer _within now pauses the tracer while it times, so a coverage run times the parser and not itself. Coverage's tracer had taken the one-line SQL split to 11.2 s of CPU against its 10 s bound on CI; on the development host the split takes 1.46 s untraced, 3.23 s traced and 1.41 s with the tracer paused (sql._spans on the test's input under coverage.Coverage(branch=True)). Every _within caller runs extractor code, which the unit coverage does not measure. pyproject.toml pins coverage's C tracer (core = "ctrace", the 3.12 default), the one sys.settrace(None) pauses.
  • Wall-clock overlap bounds. Five tests check that two awaits overlap with a wall-clock bound a few tens of milliseconds above the overlapped time: the two in test_code_search_legs.py, test_code_grep_scan.py::test_as_of_runs_beside_the_chunk_read, and the two in test_memory_code_grounding.py. Their fake reads now sleep 0.2 s instead of 0.05 s, with each bound scaled by the same factor. The check is the same, with four times the margin for a loaded runner. Two of them had failed at the old size: 0.185 s against 0.17, and 0.0965 s against 0.095. The two timed tests in test_code_search_legs.py also failed when one was the first code search in its process: that search imports the tracing modules on the event loop, about 0.2 s on a cold start, inside the vector chain before the embed. The vector read then started on the tick the lexical read ended, or the fuzzy-vote search ran over its bound. Both tests now run one untimed search first and empty the corpus-stats cache that search filled. With a 0.25 s blocking first span injected, both old tests fail (the fuzzy one at 0.81 s against 0.68) and both new ones pass.
  • Coverage by chance. Four paths were covered only through other tests' leftover state or a hash-seeded order; each now has a test of its own (Tests, below).
  • A leaked cost accumulator. cost_accumulator.begin_op() binds the op's tally to the current context, and sync tests that call it left it bound in the main thread's context for every later test in the process. A new autouse guard in tests/unit/conftest.py starts each test with no accumulator and puts back what was there, as the query-tag guard does; accounting state is reset rather than failing the test that bound it.
  • A timestamp holding the card's digits. test_session_brief.py's redaction checks assert that 4111 from a test card number is absent from the stored row. They left out only version, so a part failed when the row's created_at read 21:47:23.941119. They now leave out every *_at column too. The same checks fail on main whenever a timestamp holds those digits.

Scope and risk

CI configuration, tests/_shards.py and the root conftest, the unit conftest's new guard, the weights file, ten unit test files (test_test_shards.py, test_session_brief.py, test_code_search_legs.py, test_code_grep_scan.py, test_memory_code_grounding.py, test_document_extraction.py, test_loom_search_fanout_kinds.py, test_code_observability.py, test_code_outline.py, test_build_cost_accounting.py), a Makefile comment, pyproject.toml (its coverage comment and core), make test-unit-cov (now with branches and the per-module floors, as CI), the testing and local-development docs, the tests README, and the loom-investigate-ci and loom-pre-push skills' rows. No server code changes.

The integration job uses the same split, now before collection too. Its files are weighed by test-function count instead of collected test count: the four parts now collect 514, 505, 462 and 461 tests, 1,942 in all, as before (LOOM_TEST_SHARD=k/4 pytest --co -q tests/integration, k = 1..4).

A part's failure skips unit-coverage, because coverage from five parts would miss the floors for the wrong reason.

Compatibility, rollout and rollback

  • Wire: none.
  • Operator steps: none. No required status check names the old pytest unit + coverage job.
  • Rollback: revert.

Tests

CI with the split before collection, at three heads (6c6615d is this head, on main at f92e76e):

46f5031 93e1929 6c6615d
part 1 5:07 5:48 5:40
part 2 8:24 6:31 6:52
part 3 9:04 9:24 8:38
part 4 8:45 8:32 7:06
part 5 7:00 7:12 5:41
part 6 7:42 8:07 6:10
unit-coverage 2:02 1:30 2:10
unit stage, first start to last finish 11:09 10:57 10:50

Earlier heads: at 8321160 the parts took 5:43 to 7:45 and unit-coverage 1:29, 9:17 for the stage.

At 0b3e1f1 the parts took 5:43 to 8:18 and unit-coverage 1:41, 10:01 for the stage.

main's single unit job took 31 to 32 minutes.

Coverage. Combined across the parts, coverage first came out a few lines short of main's single job at the same base: 178,318 against 178,324 lines and 46,731 against 46,734 branches at 46f5031. Every missing line was a path that some test reached only by chance, either through state an earlier test in the same process left behind or through rows in a hash-seeded order. Splitting changes which tests share a process, so these lines come and go. Each now has a test of its own:

  • namespace_kinds.kind_map serving its last snapshot when a read fails after the TTL, and narrowing nothing with no snapshot (test_loom_search_fanout_kinds.py);
  • schedule_code_trace dropping a trace task of another loop (test_code_observability.py);
  • collapse_outline_rows merging a wider row for the same declaration, with a scip row on either side (test_code_outline.py);
  • _account_build pricing each written row from the request's cost accumulator (test_build_cost_accounting.py).

At 0b3e1f1, rebased on main at 4abdcda, the combined coverage covers 178,572 of 193,260 lines and 46,796 of 55,000 branches; main's single job at 4abdcda covered 178,567 lines and 46,792 branches. The per-module floors pass on the combined data in every run.

Collection. On the development host the six parts' collections add up to the whole suite, and so do the four integration parts' (1,942). tests/unit/test_test_shards.py covers the following:

  • the spec parser;
  • the weights and their fallback;
  • the walk;
  • every file landing in exactly one part;
  • a stray file going to part 1;
  • the usage error;
  • the JUnit reader;
  • a real pytest run on a directory, where the other part's files are never collected (nothing is deselected);
  • a real pytest run with file and node-id arguments, whose two parts add up to the unsplit collection.

The full unit suite passes at d4d5b88 (this branch rebased on main at 29a6d7e, which adds only extractor changes), run locally as two LOOM_TEST_SHARD halves under -n 8 (27,757 passed). The exception is the four cases of the delimiter-sniff test, which fail on the development host's CPU and pass on CI.

AI assistance in the code (leave one; writing this description with AI is fine):

  • AI-generated (an agent wrote most of the diff): Claude Code. The author checked the combined coverage against main's, the parts' collections and the CI times.

  • I can explain every line of this diff and how it interacts with the rest of Loom.

Checklist

  • make format-check, make lint, make typecheck and make test-unit pass; the integration suite passes if server behaviour changed.
  • Generated files are regenerated in this PR, not by hand.
  • A new migration is additive and numbered after the latest on main; no merged migration is edited.
  • A behaviour change to any file a plugin ships bumps both plugin manifests' version.
  • The frontend builds and its tests pass, if frontend/ changed.
  • Docs under docs/ describe the system as it now is.
  • Repository text has no dates, people, host names, internal namespaces, ticket or decision numbers.
  • Commits are signed off (git commit -s).

🤖 Generated with Claude Code

@zlareb1 zlareb1 changed the title The unit job runs as four parts on separate runners, their coverage combined in one job The unit job runs as six parts that each collect only their own files, their coverage combined in one job Nov 6, 2026
@zlareb1
zlareb1 force-pushed the ci/unit-parts branch 2 times, most recently from 48e4249 to 6c6615d Compare November 7, 2026 07:08
zlareb1 and others added 19 commits November 7, 2026 08:09
…ombined in one job

With two cores per runner, one unit job took 30 minutes. LOOM_TEST_SHARD splits the suite by whole files (tests/_shards.py); each part writes its own coverage data file, and unit-coverage combines them and applies the 40% and per-module floors.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…e checks time the parser with the tracer off

A coverage run's tracer tripled the SQL split's CPU time to 11 s against its 10 s bound; every _within caller runs extractor code, which the unit coverage does not measure.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…st of its own

The branch was covered only when an earlier test in the same process left a snapshot behind; split into parts the unit run stopped covering it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
Every part collected the whole suite under coverage before dropping five sixths of it, which took about four minutes per runner, and parts dealt by test count ran from 6 to 14 minutes. LOOM_TEST_SHARD now picks files before collection (pytest_ignore_collect), weighted by tests/shard_weights.json, seconds per file from a run's JUnit reports, which the unit-coverage job regenerates as an artifact.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…code-search timing tests have margin, docs name the integration weights

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…der needs a report, the snapshot test moves time

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
A trace task of another loop, a wider outline row for one declaration and the per-row extraction cost were covered only when another test left state behind or fed rows in a hash-seeded order; split into parts, the unit coverage lost them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…weights upload first, one test-module check, the clock seam, a directory-argument test

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…ll-clock bounds get the sleeps' margin, stale comments

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
… the TTL test counts its reads, the outline test pins the signature, the node-id test names its own file

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…, fresh contexts, resolved paths, the stray file under tmp_path, a weights sanity test

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…tory test splits the integration suite, coverage pins the C tracer, a failing part still uploads its times, make test-unit-cov measures as CI

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…he pre-push skill point at make test-unit-cov, a local part matches CI's in a clean checkout

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…the conftest docstring, re-runs overwrite a part's artifact

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…r shard tests

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
@zlareb1
zlareb1 merged commit 60a482b into main Nov 7, 2026
23 checks passed
@zlareb1
zlareb1 deleted the ci/unit-parts branch November 7, 2026 08:32
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