Skip to content

Fix runtime polling after scheduler stalls - #2352

Merged
ymichael merged 1 commit into
mainfrom
bb/ci-flake-fix-codex-topology-late-child-release-t-thr_a6kgm5yaew
Aug 24, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/ci-flake-fix-codex-topology-late-child-release-t-thr_a6kgm5yaew

Conversation

@ymichael

Copy link
Copy Markdown
Collaborator

What was wrong

The runtime polling helper checked its deadline before rechecking the predicate after each sleep. If a Vitest worker was descheduled past the deadline under package-shard contention, it exited the polling loop and threw without observing state that had already become true while it slept. That ordering explains the same-SHA packages failure: the Codex topology case spent its full 10-second wait on an otherwise roughly 3.7-second path while other process-backed suites were heavily slowed.

What changed

Reordered the shared runtime wait loop so it observes the predicate before enforcing the deadline on every wake. The timeout, polling interval, Codex topology assertion, and child-release behavior are unchanged. Added a fake-timer regression that advances the clock past the deadline while the polling sleep is pending and verifies that already-satisfied state still resolves. This is test infrastructure only; there is no server/daemon wire change and no HOST_DAEMON_PROTOCOL_VERSION bump.

How you verified

  • Deterministic pre-fix reproduction: advanced the clock past a 50 ms deadline after making the predicate true; the helper incorrectly reported Timed out after 50ms waiting for ready state. The same check resolves after the fix.
  • pnpm exec turbo run test --filter=@bb/agent-runtime --force -- src/test/runtime-wait-helpers.test.ts src/runtime.codex-topology.test.ts — 2 files, 5 tests passed.
  • pnpm exec turbo run build typecheck test --filter=@bb/agent-runtime --force — 6 Turbo tasks passed; full agent-runtime suite 22 files / 317 tests passed. (@bb/agent-runtime has no build script, so its build target is a no-op.)
  • pnpm exec prettier --check packages/agent-runtime/src/test/runtime-wait-helpers.ts packages/agent-runtime/src/test/runtime-wait-helpers.test.ts — passed.

Fixes: no issue — exact open GitHub issue/PR and bb thread searches found no overlapping work.

AGENT GENERATED: by GPT-5.6-Sol

@ymichael
ymichael merged commit fd16f6c into main Aug 24, 2026
24 of 25 checks passed
@ymichael
ymichael deleted the bb/ci-flake-fix-codex-topology-late-child-release-t-thr_a6kgm5yaew branch August 24, 2026 22:46
ymichael added a commit that referenced this pull request Aug 25, 2026
## What was wrong

The real-process recovery suite launched both the provider bridge worker
and the scripted-echo bridge from TypeScript source, so every
`restartRecommended` replacement process paid the same `tsx`
transform/import cost again. Under package-shard CPU contention, the
runtime had already shut down the original bridge and registered the
replacement child, but the replacement had not reached the bridge's
top-level `spawn` log or answered `thread/resume` before the tests'
unchanged 5-second hang detector expired. That one startup path caused
both same-SHA signatures in [CI attempt
2](https://github.com/get-bb/bb/actions/runs/32787659928/job/97623628882)
and [attempt
4](https://github.com/get-bb/bb/actions/runs/32787659928/job/97625367016);
the polling-order bug fixed by #2352 was not involved.

## What changed

- Added a Turbo-cached `@bb/agent-runtime#generate:test-bridges` task
that bundles the real provider bridge worker and real scripted-echo
bridge once per test run.
- Pointed the standard scripted-echo runtime harness at those JavaScript
artifacts, while preserving source mode for tests that deliberately
inject a custom bridge module.
- Added bounded failure diagnostics to the two affected waits so a
future failure reports process lifecycle, resumed thread IDs, runtime
provider state, and restart stderr.

The tests still exercise real child-process shutdown, replacement,
Provider Bridge Protocol initialization, and `thread/resume`; no
timeout, retry budget, assertion, product behavior, or wire contract
changed, so `HOST_DAEMON_PROTOCOL_VERSION` is unchanged.

## How you verified

- Deterministic pre-fix reproduction: ran the two exact tests with 512
CPU burners on a 16-logical-CPU host. Both failed with the CI errors
(10.810s and 14.256s). Added diagnostics showed `processLog=[spawn:<old
pid>,...,exit:<old pid>]`, `resumedThreadIds=[]`, and
`runningProviders=[fake]`: the replacement child existed but was still
before bridge top-level startup.
- Same-load post-fix comparison: the identical two-test command passed
both cases in 12.348s total (5.361s and 6.986s) with the original
5-second waits unchanged; the pre-fix run took 25.067s and failed both.
- `pnpm exec turbo run test --filter=@bb/agent-runtime --force --
'src/runtime.recovery.test.ts' -t 'restartRecommended: an idle
thread|restartRecommended read while'` — 1 file, 2 tests passed.
- `pnpm exec turbo run build typecheck test --filter=@bb/agent-runtime
--force` — 7 Turbo tasks passed; 22 files / 318 tests passed.
- `pnpm exec prettier --check packages/agent-runtime/package.json
packages/agent-runtime/scripts/build-test-bridges.mjs
packages/agent-runtime/src/test/runtime-test-harness.ts
packages/agent-runtime/vitest.config.ts turbo.json` and `git diff
--check` — passed.

Fixes: no issue — repeated exact and broad open GitHub issue/PR searches
returned no matches, and the only exact bb thread match was this worker.

> AGENT GENERATED: by GPT-5.6-Sol
ymichael added a commit that referenced this pull request Sep 8, 2026
## Human comments

## What was wrong

The recurring [packages job
failure](https://github.com/get-bb/bb/actions/runs/34275422905/job/102227127822)
was triggered by package-shard CPU contention but rooted in a lifecycle
mismatch in the test. The runtime started its 200 ms `thread/start`
request deadline after provider initialization but before the bridge
necessarily handled the request and spawned the app-server child. The
test then required `spawned === 1 && exited === 1`; when cancellation
retired the bridge before its request handler ran, no child could ever
publish either marker. An instrumented 24-run stress reproduced 22 exact
failures, all with `spawned=0`, `exited=0`, no child PID, and an
expected bridge exit. The predicate-before-deadline polling fix from
#2352 remains present; this was an impossible lifecycle state rather
than a missed final poll.

The audit of closed PR #2506 also confirmed that its adjacent
child-stdin finding remains real on current `main`: a live app-server
child that closes fd 0 produced an unhandled `EPIPE` and left its
request pending. Current `main` has since made `kill()` awaitable in
#3025, so the old patch could not be reused unchanged.

The package shard's nested concurrency is systemic: CI runs four Turbo
package tasks on a 4-vCPU runner, and each package's Vitest process
independently schedules worker pools. That explains the reproduction
condition, but it is not the root cause addressed here.

The independently verified merge base is
`06047280e04e6ce57554052ec788a1c5defe7d1e`.

## What changed

- Pre-start the shared Codex bridge outside the construction deadline
scenario, stall the fake `thread/start`, hold only the runtime's request
clock until the real child publishes its spawn marker, then advance the
unchanged 200 ms deadline. The test still requires the child to record
its SIGTERM exit and verifies its PID is gone.
- Publish the fake child's spawn marker only after its SIGTERM handler
is installed, and replace the elapsed 800 ms response delay with a
construction that remains pending until release.
- Treat child-stdin `EPIPE` and `ERR_STREAM_DESTROYED` as terminal
connection failures. New work is rejected immediately, the unusable
child is force-stopped, and pending work still finalizes on the
observable child exit/close boundary so natural exit status, stderr, and
final stdout are preserved. The current awaitable `kill()` contract
remains intact.
- Add real-child regressions for post-exit stdout draining, EPIPE
immediately before a natural exit, and containment of a live child with
closed stdin.

No request, polling, release, or test timeout was increased. There is no
server/daemon wire change, so `HOST_DAEMON_PROTOCOL_VERSION` is
unchanged. There are no CLI, SDK, guide, or configuration-surface
changes.

## How you verified

- Original red-capable exact stress on the enrolled 8-logical-CPU Intel
host, 64 runs at 32-way parallelism:
- Before: 5/64 passed; 59/64 failed with the exact `Timed out after
10000ms waiting for the late-constructed child was released` signature.
- Instrumented classification: 22/24 exact failures; every failure
recorded `spawned=0`, `exited=0`, no child PID, and
`bridgeExits=[{expected:true}]`.
- Final: 64/64 passed with no timeout, EPIPE, or survivor; test-body
p50/p95/max were 21.48 s / 24.16 s / 24.51 s under deliberate 4x
oversubscription.
- Closed-stdin regression before the connection change: failed
deterministically because the request remained pending past 500 ms and
Vitest captured an unhandled `write EPIPE`. After: the three-test
connection file passed, then passed 32/32 parallel stress runs with no
unhandled error or survivor.
- `pnpm exec turbo run test --filter=@bb/agent-runtime -- --run
src/runtime.codex-topology.test.ts` — 1 file / 4 tests passed.
- `pnpm exec turbo run test --filter=bb-plugin-provider-codex -- --run
src/bridge/app-server-connection.test.ts` — 1 file / 3 tests passed.
- `pnpm exec turbo run test --filter=@bb/agent-runtime --force` — 22
files / 318 tests passed.
- `pnpm exec turbo run test --filter=bb-plugin-provider-codex --force` —
27 files / 264 tests passed.
- A combined two-package test run made nested contention visible: the
Codex provider passed, while agent-runtime hit three unrelated existing
failures in ACP topology and process-lifecycle tests. Both package
suites passed independently, and the changed focused tests remained
green.
- `pnpm exec turbo run typecheck --filter=@bb/agent-runtime
--filter=bb-plugin-provider-codex --force` — passed.
- `pnpm exec turbo run build --filter=@bb/agent-runtime
--filter=bb-plugin-provider-codex --force` — passed (the scoped packages
have no direct build tasks; their applicable dependency graph passed).
- `pnpm exec prettier --check
packages/agent-runtime/src/runtime.codex-topology.test.ts
plugins/provider-codex/src/bridge/app-server-connection.ts
plugins/provider-codex/src/bridge/app-server-connection.test.ts
plugins/provider-codex/src/bridge/fake-codex-app-server.mjs` — passed.
- Every reproduction, stress, test, typecheck, and build command ran
inside a bounded process group on `host_nwqfteeqz4`; cleanup checks
reported no surviving group or matching test/fake-child process.

Fixes: no issue — exact path/signature searches found no open issue or
PR, and the prior BB owners are archived without environments.

> AGENT GENERATED
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