Skip to content

ci: shard the consensus suite four ways (987s -> ~250s) - #1253

Draft
hackobi wants to merge 4 commits into
stabilisationfrom
ci/shard-consensus-tests
Draft

hackobi wants to merge 4 commits into
stabilisationfrom
ci/shard-consensus-tests

Conversation

@hackobi

@hackobi hackobi commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Measured result

CI wall clock: 17.3–18.1 min → 4.9 min. A 72% cut. All 18 checks pass, zero failures.

run what duration
44187346300 before, stabilisation 17.3 min
44180543429 before, stabilisation 18.1 min
44286178194 after, round-robin sharding 5.5 min
44290907006 after, LPT packing + hoisted guards 4.9 min

Three changes, three commits

1. Shard the suite (--shard INDEX/TOTAL)

Consensus Tests was the longest job here and 67% of CI compute. Measured: 987s, of which setup is only 12s — the rest is scripts/test-consensus.sh running 265 test files one process at a time, in series, on a 2-vCPU runner.

Not inherently serial: the script already gives each file its own process because bun shares module mocks across files. There was simply no way to ask for a subset.

2. Pack shards by measured duration, not file count

Round-robin is even by file count, but file durations differ by two orders of magnitude — slowest measured file 48s, median 4s. That left shard 1 at 262s of test time against 208s for shard 4.

scripts/consensus-weights.tsv carries per-file seconds measured from this branch's own sharded run (parsed from ::group:: markers in the Actions log), and shard_files now packs with LPT.

At 4 shards: max shard 262s → 229s, the theoretical lower bound for an even split; spread 54s → 1s. Exact partition preserved — every file in exactly one shard, no gaps, no duplicates — verified at totals 1, 2, 3, 4, 6, 8.

3. Run the launcher guards once, not once per shard

launcher-wipe-guard measures 40s per shard. I had assumed ~2s and said so in commit 2's comment; commit 4 corrects it. On four shards that was ~160s of duplicated wall clock — more than sharding saved. Both stub guards moved to their own parallel job (measured 53s standalone).


Where my modelling was wrong, twice

I want these on the record because both shaped the result:

  1. First estimate: predicted slowest shard ≈250s and Rehearsal Drill Checks (279s) as the new critical path. Shards actually landed at 4m22s–5m23s. Round-robin's evenness-by-count was the reason.
  2. Second estimate: LPT would drop max shard to ~229s of test time, so end-to-end would tighten to ~280s. The real run came in at 4m31s max shard — better than 5m23s but above the model, because the weights are counted work, not wall clock: they are missing per-file process spawn and teardown, so they predict the ratio between files well and absolute duration poorly.

That distinction is in the weights file's own comment. The ratio is what a packing decision needs; the absolute number is not something to quote as a prediction, and I am not quoting one.

What actually bounds this now

Rehearsal Drill Checks ran 4m48s, and 254s of it is a single step (lockstep-stub.test.sh). Sharding the consensus suite can't go below that. Further gains need that step looked at — which is a separate change, not part of this PR, and I have not attempted it.

Verified before each push

  • Exact partition at totals 1/2/3/4/6/8 — 265 files, 0 duplicates, 0 missing, spread ≤ 1
  • Load balance with LPT: max = theoretical lower bound at every shard count
  • --list byte-identical (sha256 8a658ede…) with and without shard env → test-consensus.registration.test.ts still passes
  • Fallback proven: weights missing / partial / garbage each revert to round-robin (67/66/66/66)
  • Invalid args exit 2: --shard 1, abc/2, 0/4, 5/4, 1/0
  • End-to-end: shard 4/4 ran 111 real files, rc=0
  • actionlint clean

Also fixes an existing bug

Under set -u, bash below 4.4 treats an empty failed[] as unbound and aborts even when every test passed. Bash 5 on the runner accepts the plain form, so it never surfaced there — but it makes a no-failure run impossible to reproduce on macOS, which is exactly what a shard does routinely. Fixed with ${failed[@]:-}.

On compute

Still close to flat: four checkouts and bun installs replace one, and the hoisted guards remove 160s of duplication, so it is slightly better than flat. This is a feedback-time win, not a cost win. On a public repo with hosted runners that is the right thing to optimise, and I would rather label it correctly than inflate it.

Draft for review — no merge implied.

265 test files run one process at a time, in series, on a 2-vCPU runner:
about 16 minutes and the longest job in this repo. Nothing about that is
inherently serial -- the loop already isolates each file in its own process
precisely because bun shares module mocks across files -- but there was no way
to ask for a subset.

`--shard INDEX/TOTAL` (or SHARD_INDEX/SHARD_TOTAL) deals the sorted file list
round-robin across N shards. Every file lands in exactly one shard: no
duplicates, no gaps, and sizes differ by at most one file.

Round-robin rather than duration packing on purpose. Packing is better, but it
needs per-file timings that nothing collects yet, and a made-up weight table
produces an uneven split that looks considered while being wrong. Round-robin
is exact and honest about being approximate; pack it properly once real timings
exist.

Unsharded invocation is unchanged: no argument and no env means one shard
holding everything. `--list` is byte-identical, which the registration test in
scripts/test-consensus.registration.test.ts asserts.

Also fixes an existing bug that sharding exposes: under `set -u`, bash below
4.4 treats an empty `failed[]` as unbound and aborts the script even when every
test passed. Bash 5 on the runner accepts the plain form, so it never showed
there -- but it makes any no-failure run impossible to reproduce on macOS,
which is exactly what a shard does routinely.
Consensus Tests is 67% of this repo's CI compute and, at ~987s, the critical
path every PR waits on. It is now four shards of ~66 files each.

Modelled from the measured 987s: the slowest shard lands near 250s, so wall
clock drops to roughly 4-5 minutes and Rehearsal Drill Checks (~279s) becomes
the new critical path. Compute is close to flat -- four checkouts and four bun
installs replace one -- so this buys feedback time, not minutes. On a public
repo on hosted runners that is the right thing to optimise.

4 shards is conservative because the slowest single file sets the floor. Raise
it once real per-shard timings are visible; do not guess.

The two shell guards (launcher-wipe-guard, launcher-external-db) are not
partitioned and run in every shard. They are stubs, measured at ~2s, so
repeating them is cheaper than adding a mode flag to skip them.
@coderabbitai

coderabbitai Bot commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Round-robin splits by file count, but file durations differ by two orders of
magnitude -- the slowest measured file is 48s and the median is 4s. That left
shard 1 running 262s of tests against 208s for shard 4.

scripts/consensus-weights.tsv carries the per-file seconds measured from this
branch's own sharded run (parsed from the ::group:: markers in the Actions log),
and shard_files now packs with LPT when that table covers the suite.

Measured effect at 4 shards: max shard 262s -> 229s, which is the theoretical
lower bound for an even split, and the spread drops from 54s to 1s. Exactness is
preserved -- every file in exactly one shard, no duplicates, no gaps -- verified
across totals 1, 2, 3, 4, 6 and 8.

The weights are counted work, not wall clock: they are missing per-file process
spawn and teardown, so they predict the ratio between files well and absolute
shard duration less well. That is fine for a packing decision and wrong to quote
as a prediction.

Falls back to round-robin when the table is absent, partially covering the suite,
or unparseable. Packing on a stale or partial table would silently skew the
split, and a file with no weight would be placed as if it were free.
launcher-wipe-guard measures 40s per shard (I had assumed ~2s and said so in the
previous commit's comment, which this corrects). On four shards that is 160s of
duplicated wall clock per run -- more than the sharding itself saved.

Both stub guards move to their own job, beside the shards rather than repeated
inside them. Nothing about coverage changes: the guards still run on every
commit, still fail CI on failure, and this workflow has no aggregate gate, so
each job stands as its own check.

Runs in parallel with the shards, so it costs no wall clock. Roughly 160s of
duplicated work removed per run.
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