Skip to content

fix(cognition): prove grounding-deferral by control flow, not by a stopwatch - #2330

Merged
joelteply merged 1 commit into
canaryfrom
fix/flaky-grounding-cost-test
Aug 16, 2026
Merged

joelteply merged 1 commit into
canaryfrom
fix/flaky-grounding-cost-test

Conversation

@joelteply

Copy link
Copy Markdown
Contributor

The failure

PR #2329 was CI-red on exactly one test — cognition::persona_workspace::tests::deferring_grounding_removes_its_deliver_cost_from_the_critical_path — despite that PR's only changed file being cognition/swe_bench.rs, which does not touch this module.

Root cause: the assertion was unfalsifiable, not the code

The test asserted deferred_critical_path < 30ms on a live cognition cycle. critical_path_us is built from FacultyTiming.elapsed_us, which is real wall clock and by its own doc "includes scheduling waits, which is honest: it's the latency the barrier actually saw". Under cargo test's parallelism that number measures the runner, not grounding.

The CI numbers prove it rather than merely suggesting it:

fork measured critical path
defer_grounding=false (pays a 60ms deliver) 62,427µs
defer_grounding=true (pays none) 99,798µs

The fork that pays no grounding cost measured higher than the one that pays 60ms. That is impossible for a grounding measurement — it can only be scheduler stall, and the same run has capacity::trace_tail::tests::k3_fixture_measured_schedulable_coverage logging "has been running for over 60 seconds" concurrently. Solo on this machine the same fork reads 784µs, ~127× under the CI value.

So: class (a) — non-deterministic by construction. The production path is correct; DeferredFaculty::contribute returns reprojected last-good and never awaits the inner faculty on a warm ambient tick (deferred_faculty.rs, the return served branch). The invariant is control flow, and it was being probed with a stopwatch.

The fix — split into its two honest halves, both clock-free

Structural (persona_workspace.rs): SlowGrounding's sleep becomes GatedGrounding, whose deliver() parks until the test opens a gate. With the gate held shut:

  • the deferred fork's warm tick must still complete — it can only do that by serving last-good, i.e. never awaiting deliver();
  • the synchronous fork's warm tick must run into deliver() and park there — the control that makes the completion above evidence of anything.

A future parked on a shut gate cannot complete no matter how loaded the machine is, so neither assertion has a timing tolerance to blow. The two remaining timeouts are 30s liveness bounds that only fire on a genuine hang — never perf thresholds. (One real bug found while building this: watch::Sender::send silently leaves the value unchanged when no receiver is parked, which made the gate a no-op; send_replace is the repo idiom, as DashboardCaptureSink already documents.)

Cost (workspace_dashboard.rs): the max(perception) + max(deliberation) barrier arithmetic is lifted out of record() into a pure critical_path_us(&[FacultyTiming]) and table-tested with the timings as parameters — including that the barrier is a MAX, so a second slow perception faculty means deferral is correctly credited nothing. Same shape as the pure verdict fns in inference/llama_server.rs.

Verification

  • Positive control: mutating the deferred fork back onto the barrier fails with the intended diagnostic ("the deferred source is back ON the perception barrier") — the test has teeth.
  • New tests: 5/5 green repeated.
  • Full lib suite cargo test -p continuum-core --lib --features metal,accelerate: 7194 passed, 0 failed — twice.
  • cargo check --all-targets clean; no new warnings (one pre-existing unused ToolExecutor import removed, boy-scout).
  • Test wall time drops 450ms → ~10ms: the sleeps existed only to feed the stopwatch.

No #[ignore], no loosened threshold, no retry, no new test mod (both tests extend the existing mod tests in their file).

Unblocks #2329 (django runner seam + astropy sdist build deps + era pins), which is the #1 scoreboard blocker.

🤖 Generated with Claude Code

https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

…opwatch

`deferring_grounding_removes_its_deliver_cost_from_the_critical_path` asserted
`deferred_critical_path < 30ms` on a LIVE cognition cycle. `FacultyTiming.elapsed_us`
is real wall clock and, by its own doc, "includes scheduling waits" — so under
`cargo test`'s parallelism the number measures the runner, not grounding.

On PR #2329's CI (a changed-file set that never touches this module) the deferred
fork clocked 99,798µs against the synchronous fork's 62,427µs: the fork that pays
NO grounding cost measured HIGHER than the one that pays a 60ms deliver. That is
arithmetically impossible for a grounding measurement and can only be scheduler
stall — the run had a 60s+ CPU-hog test executing alongside it. Solo locally the
same fork reads 784µs. The code was never wrong; the assertion was unfalsifiable.

The invariant is structural, not temporal — `DeferredFaculty::contribute` returns
reprojected last-good and never awaits the inner faculty on a warm ambient tick —
so it is now proven that way, split into its two honest halves:

* Structural (persona_workspace.rs): `SlowGrounding`'s sleep becomes `GatedGrounding`,
  whose `deliver()` parks until the TEST opens a gate. With the gate held shut the
  deferred fork's warm tick must still COMPLETE (only possible by serving last-good),
  while the synchronous fork's warm tick must run INTO deliver and park there — the
  control that makes the completion mean something. A future parked on a shut gate
  cannot complete however loaded the machine is, so neither assertion has a timing
  tolerance to blow. The two remaining timeouts are 30s liveness bounds that only
  fire on a genuine hang, never perf thresholds.

* Cost (workspace_dashboard.rs): the `max(perception) + max(deliberation)` barrier
  arithmetic is extracted into a pure `critical_path_us(&[FacultyTiming])` and
  table-tested with the timings as parameters — including that the barrier is a MAX,
  so a second slow perception faculty means deferral is correctly credited nothing.
  Same shape as the pure verdict fns in `inference/llama_server.rs`.

Verified with teeth: mutating the deferred fork back onto the barrier fails with the
intended diagnostic. Full lib suite green twice (7194 passed, 0 failed). The test also
drops from 450ms to ~10ms — the sleeps it needed are gone with the stopwatch.

Also removes a pre-existing unused `ToolExecutor` import in the file (boy-scout).

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
@joelteply
joelteply merged commit 153c703 into canary Aug 16, 2026
5 checks passed
@joelteply
joelteply deleted the fix/flaky-grounding-cost-test branch August 16, 2026 21:10
joelteply added a commit that referenced this pull request Aug 16, 2026
…im (evaluator <10ms flake) (#2333)

`test_all_gates_pass_normal_message` failed CI at 10.72971ms against a hardcoded
`decision_time_ms < 10.0`. The gates did not regress — the runner was busy. It
is red on PR #2331, whose diff is CI workflow config and cannot possibly have
touched persona gating.

A correctness test that a loaded machine can fail is not measuring the code, it
is measuring the machine. Worse, it spends the reviewer's trust every time it
flakes: the next red is assumed to be noise, and eventually a real one is.

The performance claim is real and worth keeping, so it is NOT deleted — it MOVES
to a `#[cfg(feature = "stress-tests")]` block (CLAUDE.md § test rules, item 2),
where default `cargo test` does not run it and a quiet machine can. The stress
version also takes the MEDIAN of 50 runs instead of trusting one sample, since a
single timing of a live system is not a fact about it — one scheduler hiccup is
not a regression.

The correctness test keeps its correctness assertion (`should_respond`) and now
asserts nothing about the clock.

Same treatment as the grounding-cost flake in PR #2330 — second instance of this
class, so it is a missing constraint, not two bugs. Surveyed the rest of the
tree: 3 siblings exist (command_executor <500ms, rag/engine <250ms,
sentinel/parallel <180ms). Deliberately NOT touched — none is failing and each
has 20-50x more headroom than the 10ms budget that broke. Named in a comment at
the fix site so the next person has the map instead of rediscovering it.

cargo test persona::evaluator: 33 passed, 0 failed.
cargo check --features stress-tests --profile test: clean (the gated block builds).


Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

Co-authored-by: Claude Fable 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant