Skip to content

flaky(runtime-host): handoff proof misses a drain residency when the runner is under CPU contention #5698

Closed

Description

@sunrioa

What happened

idle schedules and armed or paused Goals allow production handoff and recover in the successor fails intermittently when the runner is under CPU contention.

The failing assertion is the second handoff's exact-handle proof, at packages/runtime-host/src/__tests__/execution-composition.test.ts:204:

const prepared = await composition.prepareHandoff!('old-host', new AbortController().signal);
assert.ok(prepared);
assert.equal(await prepared.seal(), true);
const proof = await prepared.residencies();
assert.ok(proof);
assert.equal(residencies.hasDrainResidenciesExcept(proof), false);  // observed true

Between seal() and the proof snapshot, a drain residency existed that the proof did not cover.

HostKernel applies the same check at packages/runtime-host/src/server/host-kernel.ts:965 as one of the guards a handoff must clear before it commits, and that guard fails closed: an unproven drain residency makes the handoff return false. The production consequence of this window is therefore a declined handoff, not a committed bad one. The test asserts the stronger property that the window does not open at all.

I have not isolated which residency appears. The test arms Goals and creates scheduled tasks, so a scheduler tick landing between the seal and the snapshot is one candidate, but I have not confirmed it.

How to reproduce

Intermittent, and so far only under CPU contention.

Observed once in 15 executions of the full Runtime Host suite on a GitHub-hosted ubuntu-24.04 runner with two busy loops occupying two of the four cores. The same suite on the same runner image did not fail in 41 executions without that contention — 32 on an otherwise idle runner, and 9 run directly after the workspace test batch that ci.yml schedules immediately before this suite.

npm ci
npm run build
for i in 1 2; do ( while :; do :; done ) & done
npm --workspace @maka/runtime-host run test:dist

Environment

  • Commit: 17fa03647
  • Runner: GitHub-hosted ubuntu-24.04, 4 cores, 16 GB
  • Node.js: 24
  • Surface: Runtime Host

Logs, screenshots, or additional context

✖ idle schedules and armed or paused Goals allow production handoff and recover in the successor (540.416382ms)
  AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:

  true !== false

      at .../dist/__tests__/execution-composition.test.js:297:20
      at async withCompositionRoot (.../execution-composition.test.js:3045:9)
      at async TestContext.<anonymous> (.../execution-composition.test.js:225:5)
    actual: true,
    expected: false,
    operator: 'strictEqual',

Round totals: tests 2104, pass 2084, fail 1, cancelled 0, skipped 19.

Activity

  1. ggbdpq commented on Sep 24, 2026

    @ggbdpq
    Contributor

    take

  2. ggbdpq commented on Sep 24, 2026

    @ggbdpq
    Contributor

    I took this earlier today and am releasing the claim (untake) — I could not reproduce the window within a local budget, but the analysis narrowed the suspects enough that it may save the next person some reading.

    Where the unproven residency can come from. The proof in execution-composition.ts aggregates exactly four sources: goalHold.residencies(prepared.executions), prepared.residencies() (per held active root turn: its active.residency plus message residencies), scheduleHold.residencies(), and dailyReviewHold.residencies(). All three coordinators stop their timers when the hold lands and their holds wait out in-flight work, so post-hold tick starts are excluded. The one drain handle that can be alive at proof time yet outside all four sources is active.residency of a root execution that is not in the held set — i.e. a root turn that started after RootTurnCoordinator.prepareHandoff's bounded convergence passes (three) stopped chasing new executions but before seal()/the proof snapshot. The convergence loop's own comment ("Natural completion can hand a queued follow-up to a fresh root while gates converge. Bound that convergence; never chase an endless queue.") describes exactly this admission. RootTurnCoordinator's residencies()/seal() only validate the held executions, so an unheld execution's residency sails through the proof.

    Why contention makes it rare. The gap needs a tick/start to straddle the last convergence pass and the seal. Under contention the passes stretch, widening the window; on an idle runner it closes before anything lands in it.

    Repro notes. I instrumented the test's failing assertion (registry snapshot + drainCount on failure) and ran the single test 15× under two busy loops with no reproduction, then the full execution-composition.test.js file under three busy loops, also without a hit — consistent with the report's 1/15 full-suite rate needing suite-scale load. The instrumentation is one if (residencies.hasDrainResidenciesExcept(proof)) console.log(...) before the failing assert and is worth keeping until this is fixed; the snapshot's label diffs against the expected [daily-review 1, goal 2, scheduled-task 1] will name the culprit directly.

    Two candidate fixes, needing a design call:

    1. Close the gap in seal() — after the held seals resolve, re-scan #executions for unheld entries and either wait them out or fold them into the hold. Airtight, but waiting on a freshly started turn could stall the handoff for the turn's whole duration, which changes handoff latency semantics.
    2. Fail the proof honestly — have the composition's residencies() re-check residencies.hasDrainResidenciesExcept(proof) before returning and return undefined on a mismatch, converting the lying proof into a declined handoff. This matches the fail-closed production behavior already in host-kernel.ts and makes the test's assert.ok(proof)/retry path the arbiter, but the test as written would then need to tolerate a declined handoff, weakening what it pins.

    I lean toward (1) with a bounded wait for the unheld executions to reach terminal (they are, by the test's own premise, idle-host ticks that should finish quickly), but that is a semantic change a maintainer should rule on.

  3. ggbdpq commented on Sep 24, 2026

    @ggbdpq
    Contributor

    untake

  4. sunrioa commented on Sep 26, 2026

    @sunrioa
    ContributorAuthor

    I traced the unproven residency. It is the hosted-execution drain residency that HostWorkHubResultCoordinator takes when its timer fires, and no handoff hold covers it.

    The path

    1. createCapturedExecutionComposition awaits composition.recover(), and recovery ends by starting the WorkHub result coordinator (execution-composition.ts#L3119-L3122). start() schedules a reconcile 100 ms later.
    2. When that timer fires, the coordinator acquires a hosted-execution drain residency and releases it only after reconcile() settles (workhub-result-coordinator.ts#L117-L158). The coordinator has no holdForHandoff(), and prepareHandoff holds only Goals, scheduled tasks and Daily Review (execution-composition.ts#L3159-L3203), so this residency is never part of the proof.
    3. cancelled.cancel() releases the Daily Review hold, which starts a scheduler pass that awaits readConfig() (daily-review-coordinator.ts#L188-L195, #L506-L508). The second prepareHandoff waits for that pass in settled(). That wait is real I/O, so the event loop turns there and a due timer can run; the rest of both handoffs is microtasks. This is why only the second proof fails.
    4. If the WorkHub timer comes due during that wait, reconcile() is still running when the test checks the second proof, and hasDrainResidenciesExcept(proof) returns true.

    Why only under contention

    In a normal run, the second check comes about 8 ms after the WorkHub coordinator starts, so the 100 ms timer fires long after the handoff, and instrumented local runs never take a hosted-execution residency while the test runs. The window opens only when the stretch between recovery and the handoff slows to about 100 ms. It closes again at larger slowdowns, because the timer then fires before waitFor(drainCount === 0) and the test waits it out. That narrow band fits one failure in 15 runs under load.

    Reproduction

    Shortening the 100 ms startup delay moves the timer across the handoff without waiting for contention to stretch the test. Sweeping the delay from 1 ms to 20 ms in 0.5 ms steps (macOS arm64, main at 538c37cb6, one run per value), the test failed at 7, 8, 8.5 and 9 ms and passed at every other value. All four failures were at the second proof, and in each one the only unproven handle was the hosted-execution residency taken by the WorkHub timer, which fired between the start and the end of that readConfig() wait.

    Production impact

    HostKernel runs the same check before committing a handoff and returns false when it fails (host-kernel.ts#L950-L961). A handoff that overlaps a WorkHub reconcile is therefore declined, not committed. The reconcile runs 100 ms after start or after a relevant notification, and every 5 s or 60 s while it has pending targets. This is a liveness cost, not a safety one.

    Proposed fix

    Give HostWorkHubResultCoordinator a holdForHandoff() like the other timer-driven coordinators: stop its timer, keep notify() from scheduling while held, have settled() wait for an in-flight reconcile(), and reschedule on release if work is pending. prepareHandoff would then take and release it together with the other three holds. Making the test wait for the first reconcile would also make it pass, but it would hide the same interaction in production.

  5. added a commit that references this issue on Sep 27, 2026
    bfb315a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions