Repository navigation
fix: stop a leaked console-routing latch from flaking CLI failure-output tests - #127604
Conversation
enableConsoleCapture() replaces every console method with a forwarder and routeLogsToStderr()/`--json` mode latch loggingState.forceConsoleToStderr, which production only unwinds at process exit. loggingState is keyed off globalThis so it survives vi.resetModules(), so a shared Vitest worker carried both into the next file: the inherited forwarder writes to process.stderr and the next file's console.error spy records nothing. That is the source of the order-dependent `src/entry.run-main.test.ts > keeps expected conditions at exit 1` flake in shard core-unit-src-security-2 — it failed only on workers that ran src/mcp/codex-supervision-tools-serve.test.ts (serveCodexSupervisionToolsMcp -> routeLogsToStderr) first. Restore the worker's baseline console methods and logging state in the runner's existing cross-file cleanup, and drop the two consumer-side workarounds that were compensating for it.
|
🦞👀 Pull request received. I will update this pull request when review starts. |
|
Codex review: needs maintainer review before merge. Reviewed August 21, 2026, 5:37 PM ET / 21:37 UTC. ClawSweeper reviewWhat this changesThis PR restores baseline console methods and logging state after each non-isolated Vitest file, removes two consumer-side test workarounds, and adds ordered cross-file regression coverage. Regression provenancePossible regression — suspected (reviewed change). No predecessor PR is attributed. Merge readinessKeep open: current main still relies on test-local console-routing workarounds and lacks the shared-worker cleanup. Source review found no blocking defect in the runner-owned reset; automated autoreview could not run because TruffleHog is unavailable in this review environment. Priority: P2 Review scores
Verification
Live VerificationCommand: Result: PASS (completed) Assertions:
How this fits togetherThe non-isolated Vitest runner reuses one worker across test files. CLI and stdio-MCP tests can latch console capture and stderr routing, so the runner must clear those file-scoped effects before the next test file runs. flowchart LR
A[Test file] --> B[Console capture and stderr routing]
B --> C[Shared Vitest worker]
C --> D[Runner cross-file cleanup]
D --> E[Clean console and logging baseline]
E --> F[Next test file and CLI assertion]
Before merge
Agent review detailsSecurityNone. PR surfaceTests +52. Total +52 across 4 files. View PR surface stats
Review metrics
Stored data modelPersistent data-model change detected: Merge-risk optionsMaintainer options:
Technical reviewBest possible solution: Land the runner-owned reset with the ordered regression fixture after the unavailable autoreview closeout is completed and exact-head CI is green. Do we have a high-confidence way to reproduce the issue? Yes, source provides a high-confidence ordered path: latch stderr routing and console capture in one non-isolated file, then run a CLI failure-output assertion in the same worker. This review did not execute tests because the checkout is read-only. Is this the best way to solve the issue? Yes. Resetting the shared worker at onAfterRunFiles fixes the producer-owned lifecycle leak; current main’s per-test capture and afterEach restoration only compensate at individual consumers. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 69e4fbbeb5f8. LabelsLabel changes:
Label justifications:
EvidenceWhat I checked:
Likely related people:
Rank-up movesOptional improvements that raise the rating; they are not merge blockers.
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
|
|
Merged via squash.
|
…law#127604) enableConsoleCapture() replaces every console method with a forwarder and routeLogsToStderr()/`--json` mode latch loggingState.forceConsoleToStderr, which production only unwinds at process exit. loggingState is keyed off globalThis so it survives vi.resetModules(), so a shared Vitest worker carried both into the next file: the inherited forwarder writes to process.stderr and the next file's console.error spy records nothing. That is the source of the order-dependent `src/entry.run-main.test.ts > keeps expected conditions at exit 1` flake in shard core-unit-src-security-2 — it failed only on workers that ran src/mcp/codex-supervision-tools-serve.test.ts (serveCodexSupervisionToolsMcp -> routeLogsToStderr) first. Restore the worker's baseline console methods and logging state in the runner's existing cross-file cleanup, and drop the two consumer-side workarounds that were compensating for it.
What Problem This Solves
Fixes an issue where
src/entry.run-main.test.ts > entry run-main boundary > keeps expected conditions at exit 1 without crash framingfailed intermittently in CI andblocked unrelated PRs (seen on #127011 and #127075, always in shard
core-unit-src-security-2):Rerunning the failed job turned the run green, so it was order-dependent, not
deterministic. Maintainers lost time to a red gate on changes that had nothing to do
with CLI failure output.
Why This Change Was Made
Root cause is a global that no test owned.
enableConsoleCapture()(src/logging/console.ts:253) replaces everyconsole.*method with a forwarder, and
routeLogsToStderr()/--jsonone-shot mode latchloggingState.forceConsoleToStderr. Production never unwinds either: a stdio MCPserver (
serveCodexSupervisionToolsMcp->routeLogsToStderr,src/mcp/codex-supervision-tools-serve.ts:107) andrunMainOrRootHelp'sretainConsoleRoutingUntilProcessExit: trueboth own the console until the processexits. That is correct for a CLI process and wrong for a Vitest worker, which never
exits.
loggingStateis deliberately keyed offglobalThis(
src/logging/state.ts:41) so it survives module reloads, so the runner's existingvi.resetModules()cleanup could not undo it either.The failing test spies on
console.error, thenonError(src/entry.ts:315) callsenableConsoleCapture()and writes through the forwarder. When the worker hadalready inherited
forceConsoleToStderr = truefrom an earlier file,writeFormattedConsoleOutputtook itsprocess.stderr.writebranch(
src/logging/console.ts:210) and never called the captured original, so the spyrecorded nothing. Whether the latching file landed in the same worker first was pure
scheduling luck, which is exactly the observed rerun-and-it-passes behavior.
The architectural owner is
OpenClawNonIsolatedRunner.onAfterRunFiles, whose statedjob is to "mirror the missing cleanup from Vitest isolate mode so shared workers do
not carry file-scoped timers, stubs, spies, or stale module state forward into the
next file". Console routing belongs in that list. The fix restores the worker's
baseline console methods and
loggingStatethere, and drops the two consumer-sideworkarounds that were compensating downstream:
src/entry.run-main.test.tsno longer needs a pre-emptiveenableConsoleCapture()to make its spy the outermost sink; it exercises the real forwarder path again.
src/mcp/codex-supervision-tools-serve.test.tsno longer hand-rolls asave/restore of
forceConsoleToStderraround a describe block.streamErrorHandlersInstalledis deliberately retained across the reset: those EPIPEhandlers really are attached to the worker's stdout/stderr, and clearing the flag
would let the next
enableConsoleCapture()stack a second pair on every file.User Impact
None at runtime. Test-harness only; production LOC delta is 0.
Evidence
Failure mechanism, reproduced in the original execution order with the real files
(forced sequencer so
codex-supervision-tools-serve.test.tsruns first in oneworker):
FAIL src/entry.run-main.test.ts > keeps expected conditions at exit 1 without crash framing,AssertionError: expected [] to deeply equal [ Array(1) ]— byte-identical to the CI failure.Test Files 2 passed (2) / Tests 6 passed (6).Regression coverage lands in
test/non-isolated-runner.test.tsas a06-a/06-bfixture pair (file A latches capture + stderr routing, file B asserts a clean start
and that its own
console.errorspy still receives the line). Against the pre-fixrunner it fails with
expected true to be falseonloggingState.forceConsoleToStderr; with the fix the child run reports1 failed | 11 passedas asserted.Local proof on this branch (rebased onto
origin/main@41acededbc5):node scripts/run-vitest.mjs --config test/vitest/vitest.unit-src.config.ts(fullproject, owns both changed test files) — green.
node scripts/run-vitest.mjs src/logging src/mcp test/non-isolated-runner.test.ts—
555 + 60 + 45 + 1tests passed.core-unit-src-security-2(146 files, pre-rebase membership that co-locatedthe two files) —
145 passed | 1 skipped.node scripts/check-changed.mjs— exit 0.autoreview --mode uncommitted(codex/gpt-5.6-sol) — clean, no accepted findings.Note on shard membership: the stripe split is content-derived, so the latching file
and
entry.run-main.test.tsonly co-locate incore-unit-src-security-2for somebases. After this rebase they no longer share that shard, which is why the flake
appeared on some PRs and not others.
Unrelated local-only failures, confirmed pre-existing (they reproduce with this
change stashed and on a clean
origin/main, andmain's CI check-runs are green):src/infra/system-run-approval-binding.test.ts,test/scripts/telegram-user-observer.test.ts,and
test/scripts/telegram-mantis-lane.test.tsall fail on this Mac's Python 3.14setup. Worth a follow-up to make those tests environment-independent; out of scope
here.