Skip to content

test: serialize Vitest mock resolution in isolate:false shared workers - #109874

Merged
steipete merged 3 commits into
mainfrom
fix/flaky-orphan-recovery
Jul 17, 2026
Merged

steipete merged 3 commits into
mainfrom
fix/flaky-orphan-recovery

Conversation

@steipete

@steipete steipete commented Jul 17, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

subagent-orphan-recovery tests flaked on main (run 29566318167): vi.mock factory state silently split into two instances in isolate:false shared workers, so configured mocks never reached prod code.

Why This Change Was Made

Root cause is an upstream Vitest 4 mocker re-entrancy hole: BareModuleMocker.resolveMocks() has no in-flight guard — concurrent passes over the same pendingIds re-register manual mocks and invalidate the mock module mid-import-chain, splitting importers across two factory instances (deterministically reproduced with the exact CI signature).

The fix wraps resolveMocks in the shared runner (test/non-isolated-runner.ts) so each caller chains onto its own sequential pass. This preserves both invariants the mocker's callers rely on:

  • Serialization — a pass queued behind an in-flight one sees the cleared queue and no-ops, so a snapshot is never registered/invalidated twice (fixes the instance-split disease).
  • Freshness — every caller's pass starts at or after its call, so vi.doUnmock + immediate dynamic imports resolve against current state.

An earlier iteration of this PR used shared-pass coalescing, which violated freshness and regressed the auth shard on this PR's own CI (a caller could ride a pass snapshotted before its unmock was queued, loading the real provider-auth warm worker and a 120s oauth refresh timeout). That was caught here, root-caused, and replaced — details in the commit body. Abandoned-id requeue from the first review round is preserved.

Upstream note: the missing in-flight guard is a genuine @vitest/mocker bug worth filing against vitest 4.

User Impact

None at runtime — test-runner infrastructure only; hardens every isolate:false shard.

Evidence

  • Original disease: poison-file + resolveId-jitter repro reproduces the exact CI failure on the unfixed runner; 5/5 green with the chained pin; A/B both directions.
  • Freshness regression: cold-cache auth shard (exact CI include list, 37 files / 528 tests) fails 3/3 under coalescing, passes 20s on the unfixed runner, and passes 3/3 (~20–30s) under the chained design.
  • Warm sanity: subagents shard (63 files / 1220 tests) + auth shard green at OPENCLAW_VITEST_MAX_WORKERS=6; wrapper unit tests assert serialization, freshness, requeue, idempotent install.
  • oxlint / format / check:test-types / git diff --check clean; autoreview clean ("patch is correct 0.88").

@openclaw-barnacle openclaw-barnacle Bot added size: S maintainer Maintainer-authored PR labels Jul 17, 2026
@steipete
steipete force-pushed the fix/flaky-orphan-recovery branch 2 times, most recently from dff607d to e045e58 Compare July 17, 2026 09:52
@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 automation 🚨 May affect CI, automerge, proof capture, label sync, or maintainer automation. labels Jul 17, 2026
@clawsweeper

clawsweeper Bot commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed July 17, 2026, 5:59 AM ET / 09:59 UTC.

Summary
The PR serializes Vitest mock resolution in shared non-isolated workers and adds regression tests for coalescing, late queue entries, idempotent installation, and subsequent resolution passes.

PR surface: Tests +148. Total +148 across 2 files.

Reproducibility: yes. The PR provides a deterministic poison-file reproduction matching the two CI failures, unfixed/fixed A/B reversal, and repeated target-shard stress runs.

Review metrics: none identified.

Merge readiness
Overall: 🐚 platinum hermit
Proof: 🦞 diamond lobster
Patch quality: 🐚 platinum hermit
Result: ready for maintainer review.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

  • Link the upstream Vitest report or a concrete removal trigger so the internal workaround does not become permanent test architecture.

Risk before merge

  • [P1] The shared-runner wrapper depends on Vitest internals—particularly constructor.pendingIds and queue replacement after resolveMocks—so a future Vitest upgrade could invalidate the workaround or its late-registration handling.

Maintainer options:

  1. Land with the contract guard (recommended)
    Accept the bounded internal dependency because the focused tests and repeated A/B stress evidence cover the demonstrated shared-worker failure mode, while tracking upstream removal.
  2. Wait for upstream Vitest
    Pause until Vitest provides native re-entrancy protection, avoiding a local monkeypatch but leaving the demonstrated main-branch flake unresolved.

Next step before merge

  • [P2] There is no concrete repair finding for automation; maintainers should complete normal merge review and let required checks gate the exact head.

Security
Cleared: The diff is confined to test-runner code and focused tests, with no production, dependency-source, secret, permission, publishing, or supply-chain changes.

Review details

Best possible solution:

Land the bounded runner-level guard with its regression coverage, file or link the upstream Vitest re-entrancy report, and remove the monkeypatch once the pinned dependency provides equivalent serialization.

Do we have a high-confidence way to reproduce the issue?

Yes. The PR provides a deterministic poison-file reproduction matching the two CI failures, unfixed/fixed A/B reversal, and repeated target-shard stress runs.

Is this the best way to solve the issue?

Yes. The shared non-isolated runner is the narrowest ownership boundary that protects all affected shards without rewriting individual production mocks or stabilizing only the two observed tests.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 8f3b2ab02177.

Label changes

Label changes:

  • add P2: The PR addresses a demonstrated repository-wide test reliability problem without affecting shipped runtime behavior.
  • add merge-risk: 🚨 automation: The patch changes the shared runner used across Vitest shards, and incorrect queue handling could hang tests, discard mock registrations, or produce misleading CI results.
  • add proof: sufficient: Contributor real behavior proof is sufficient. The PR body supplies after-fix deterministic A/B output and repeated real-runner stress across the target file, the full affected shard set, exact CI ordering, and shuffled seeds.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): The PR body supplies after-fix deterministic A/B output and repeated real-runner stress across the target file, the full affected shard set, exact CI ordering, and shuffled seeds.

Label justifications:

  • P2: The PR addresses a demonstrated repository-wide test reliability problem without affecting shipped runtime behavior.
  • merge-risk: 🚨 automation: The patch changes the shared runner used across Vitest shards, and incorrect queue handling could hang tests, discard mock registrations, or produce misleading CI results.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): The PR body supplies after-fix deterministic A/B output and repeated real-runner stress across the target file, the full affected shard set, exact CI ordering, and shuffled seeds.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR body supplies after-fix deterministic A/B output and repeated real-runner stress across the target file, the full affected shard set, exact CI ordering, and shuffled seeds.
Evidence reviewed

PR surface:

Tests +148. Total +148 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 0 0 0 0
Tests 2 149 1 +148
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 149 1 +148

What I checked:

  • Changed runner boundary: The branch wraps each worker mocker's asynchronous resolveMocks method with a shared drain and requeues IDs appended to the abandoned pending-array snapshot during an active pass. (test/non-isolated-runner.ts:250, e045e58a41ad)
  • Focused regression coverage: The added tests exercise concurrent-call coalescing, registrations queued during resolution, repeat installation, and a fresh pass after the first drain. (test/non-isolated-runner.resolve-mocks.test.ts:1, e045e58a41ad)
  • Current test architecture: OpenClaw's test documentation confirms that repository configurations use thread pools, isolate: false, and the shared non-isolated runner, giving this race repository-wide test-infrastructure scope. (docs.openclaw.ai) (docs/reference/test.md, 8f3b2ab02177)
  • Dependency contract: Published Vitest 4 declarations expose an asynchronous resolveMocks method and static pendingIds queue on the mocker, matching the internal object shape targeted by the wrapper. (app.unpkg.com) (node_modules/vitest/dist/chunks/moduleRunner.d.ts)
  • Feature provenance: Recent merged history attributes no-isolate test hardening, module reset work, and expanded Vitest thread lanes to commits 1ceaad18a6, 9105b3723d, and 8e09568bc7; planner-backed runner and concurrency-budget work appears in ab37d8810d. (git.mineracks.com) (test/non-isolated-runner.ts, 9105b3723d)

Likely related people:

  • steipete: Recent merged history includes the no-isolate coverage, module-reset, Vitest thread-lane, and changed-lane work directly adjacent to this runner. (role: recent test-infrastructure contributor; confidence: high; commits: 1ceaad18a6, 9105b3723d, 8e09568bc7; files: test/non-isolated-runner.ts, scripts/run-vitest.mjs)
  • Takhoffman: Introduced the planner-backed test runner and no-isolate concurrency-budget behavior that governs how shared workers execute. (role: test-runner feature owner; confidence: medium; commits: ab37d8810dbe; files: scripts/run-vitest.mjs, test)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

How this review workflow works
  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

steipete added 3 commits July 17, 2026 03:43
… runner

Root-causes the subagent-orphan-recovery flake (main run 29566318167, shard
agentic-agents-core-subagents): "includes last human message in resume when
available" and "adds config change hint..." received the bare resume template
because prod code called a different readSessionMessagesAsync mock instance
than the one the test configured.

Mechanism: vitest's BareModuleMocker.resolveMocks has no in-flight guard.
pendingIds is cleared only after all parallel resolveId RPCs settle, and every
registration re-invalidates the mock module node. In a shared isolate:false
worker, leaked async work from an earlier file (a real-timer dynamic import)
triggers a fetchModule while the next file's vi.mock registrations are still
pending, starting a second concurrent resolution pass over the same
pendingIds. The slower pass re-registers each manual mock and wipes the
already-evaluated mock module mid-import-chain: importers evaluated before the
wipe (the test file's own binding, subagent-orphan-recovery.test.ts:8) keep
factory instance A while later importers (subagent-orphan-recovery.ts via
test line 16) instantiate a fresh instance B. Only inline-factory mocks split;
vi.hoisted-stable mocks are immune, matching the CI failure signature exactly.

Fix: OpenClawNonIsolatedRunner installs a coalescing wrapper around
moduleRunner.mocker.resolveMocks (once per worker) so concurrent callers share
one pass and registration happens exactly once before any awaiting import
proceeds.

Proof:
- Deterministic repro (poison file leaving a 1ms recurring dynamic-import
  timer + resolveId jitter on session-transcript-readers) reproduced the exact
  CI assertion failures at lines 755/772 on the unfixed runner and passes 5/5
  with the fix.
- New unit tests cover coalescing, idempotent install, and fresh passes.
- Stress: 10x subagent-orphan-recovery.test.ts and 5x the full
  agentic-agents-core-subagents file set (63 files, 1220 tests) at
  OPENCLAW_VITEST_MAX_WORKERS=6, all green.
Review follow-up to the resolveMocks serialization pin: upstream snapshots the
pendingIds contents at pass start and reassigns the static to [] at the end,
so ids queued during the pass's RPC window land in the abandoned array. Pure
coalescing would silently drop those registrations (upstream's racy second
pass previously processed them). The wrapper now captures the queue reference
before each pass, requeues ids pushed past the processed count, and drains
until the queue is empty, so every coalesced caller's vi.mock registration is
applied before its fetch proceeds.

Proof: new unit test "registers ids queued while a pass is in flight before
callers proceed"; deterministic zombie+jitter repro still passes 5/5; full
agentic-agents-core-subagents file set green at OPENCLAW_VITEST_MAX_WORKERS=6.
The coalescing pin regressed shard agentic-agents-core-auth
(models-config.providers.auth-provenance.test.ts, runs 29570918219 and
29571513381): sharing one in-flight pass broke the mocker's freshness
invariant. Upstream gives every resolveMocks caller a pass that starts at or
after its call, so ids the caller queued (vi.doUnmock before dynamic imports
in that file's beforeAll/beforeEach) are always registered before its fetch
proceeds. A coalesced caller could instead ride a pass snapshotted before its
ids were queued and import with mock state unresolved. Reproduced locally on a
cold transform cache: the file then loaded the real provider-auth warm worker
plus a live oauth-manager refresh guarded by a 120s withRefreshCallTimeout
(the 105-142s stalls and the "Test timed out in 120000ms" CI failure), and the
real resolveProviderSyntheticAuthWithPlugin returned undefined (the
mode:"none" assertion failure at line 348).

The wrapper now chains each caller onto its own sequential pass. This keeps
both invariants: serialization (a pass queued behind an in-flight one sees the
cleared queue and no-ops, so a snapshot is never registered or its mock
modules invalidated twice - the original subagent-orphan-recovery disease) and
freshness (each caller's pass starts at or after its call). Abandoned-id
requeue is preserved so ids pushed during a pass's RPC window are registered
by the next chained pass instead of dropped.

Proof:
- Cold-cache auth shard (37 files, 528 tests): failed 105-142s with the
  coalescing pin (also with a coalescing-only variant), passed 20s with the
  unfixed runner, passes 3/3 in ~20-30s with the chained pin.
- Long-timer instrumentation pinpointed the stall: provider-auth warm worker
  spawn plus oauth-manager withRefreshCallTimeout delay=120000.
- Original zombie+jitter orphan-recovery repro still passes 5/5.
- Warm agentic-agents-core-subagents (63 files, 1220 tests) and auth shard
  green at OPENCLAW_VITEST_MAX_WORKERS=6; wrapper unit tests updated to
  assert serialization, freshness, requeue, idempotent install.
@steipete
steipete force-pushed the fix/flaky-orphan-recovery branch from e045e58 to ee6fcd1 Compare July 17, 2026 10:44

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ee6fcd1cf2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +276 to +277
if (queue && queue !== statics.pendingIds && queue.length > processedCount) {
statics.pendingIds?.push(...queue.slice(processedCount));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve late mock queue order when requeuing

When a mock/unmock is appended to the old pendingIds array during an in-flight Vitest pass, and another registration is queued in a microtask after Vitest has reassigned pendingIds but before this wrapper continues, push(...queue.slice(...)) places the earlier registration after the later one. That reverses call order for the next chained pass; for example an earlier doUnmock followed by a later doMock of the same module can be replayed as mock-then-unmock and leave the import unmocked. Reinsert the rescued entries before any entries already present in the new array.

Useful? React with 👍 / 👎.

@steipete
steipete merged commit b2e34b0 into main Jul 17, 2026
92 of 94 checks passed
@steipete
steipete deleted the fix/flaky-orphan-recovery branch July 17, 2026 10:54
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

steipete added a commit that referenced this pull request Jul 17, 2026
#109874)

* test: serialize concurrent vitest mock resolution in the non-isolated runner

Root-causes the subagent-orphan-recovery flake (main run 29566318167, shard
agentic-agents-core-subagents): "includes last human message in resume when
available" and "adds config change hint..." received the bare resume template
because prod code called a different readSessionMessagesAsync mock instance
than the one the test configured.

Mechanism: vitest's BareModuleMocker.resolveMocks has no in-flight guard.
pendingIds is cleared only after all parallel resolveId RPCs settle, and every
registration re-invalidates the mock module node. In a shared isolate:false
worker, leaked async work from an earlier file (a real-timer dynamic import)
triggers a fetchModule while the next file's vi.mock registrations are still
pending, starting a second concurrent resolution pass over the same
pendingIds. The slower pass re-registers each manual mock and wipes the
already-evaluated mock module mid-import-chain: importers evaluated before the
wipe (the test file's own binding, subagent-orphan-recovery.test.ts:8) keep
factory instance A while later importers (subagent-orphan-recovery.ts via
test line 16) instantiate a fresh instance B. Only inline-factory mocks split;
vi.hoisted-stable mocks are immune, matching the CI failure signature exactly.

Fix: OpenClawNonIsolatedRunner installs a coalescing wrapper around
moduleRunner.mocker.resolveMocks (once per worker) so concurrent callers share
one pass and registration happens exactly once before any awaiting import
proceeds.

Proof:
- Deterministic repro (poison file leaving a 1ms recurring dynamic-import
  timer + resolveId jitter on session-transcript-readers) reproduced the exact
  CI assertion failures at lines 755/772 on the unfixed runner and passes 5/5
  with the fix.
- New unit tests cover coalescing, idempotent install, and fresh passes.
- Stress: 10x subagent-orphan-recovery.test.ts and 5x the full
  agentic-agents-core-subagents file set (63 files, 1220 tests) at
  OPENCLAW_VITEST_MAX_WORKERS=6, all green.

* test: requeue mock ids queued during an in-flight resolveMocks pass

Review follow-up to the resolveMocks serialization pin: upstream snapshots the
pendingIds contents at pass start and reassigns the static to [] at the end,
so ids queued during the pass's RPC window land in the abandoned array. Pure
coalescing would silently drop those registrations (upstream's racy second
pass previously processed them). The wrapper now captures the queue reference
before each pass, requeues ids pushed past the processed count, and drains
until the queue is empty, so every coalesced caller's vi.mock registration is
applied before its fetch proceeds.

Proof: new unit test "registers ids queued while a pass is in flight before
callers proceed"; deterministic zombie+jitter repro still passes 5/5; full
agentic-agents-core-subagents file set green at OPENCLAW_VITEST_MAX_WORKERS=6.

* test: chain resolveMocks passes per caller instead of sharing one pass

The coalescing pin regressed shard agentic-agents-core-auth
(models-config.providers.auth-provenance.test.ts, runs 29570918219 and
29571513381): sharing one in-flight pass broke the mocker's freshness
invariant. Upstream gives every resolveMocks caller a pass that starts at or
after its call, so ids the caller queued (vi.doUnmock before dynamic imports
in that file's beforeAll/beforeEach) are always registered before its fetch
proceeds. A coalesced caller could instead ride a pass snapshotted before its
ids were queued and import with mock state unresolved. Reproduced locally on a
cold transform cache: the file then loaded the real provider-auth warm worker
plus a live oauth-manager refresh guarded by a 120s withRefreshCallTimeout
(the 105-142s stalls and the "Test timed out in 120000ms" CI failure), and the
real resolveProviderSyntheticAuthWithPlugin returned undefined (the
mode:"none" assertion failure at line 348).

The wrapper now chains each caller onto its own sequential pass. This keeps
both invariants: serialization (a pass queued behind an in-flight one sees the
cleared queue and no-ops, so a snapshot is never registered or its mock
modules invalidated twice - the original subagent-orphan-recovery disease) and
freshness (each caller's pass starts at or after its call). Abandoned-id
requeue is preserved so ids pushed during a pass's RPC window are registered
by the next chained pass instead of dropped.

Proof:
- Cold-cache auth shard (37 files, 528 tests): failed 105-142s with the
  coalescing pin (also with a coalescing-only variant), passed 20s with the
  unfixed runner, passes 3/3 in ~20-30s with the chained pin.
- Long-timer instrumentation pinpointed the stall: provider-auth warm worker
  spawn plus oauth-manager withRefreshCallTimeout delay=120000.
- Original zombie+jitter orphan-recovery repro still passes 5/5.
- Warm agentic-agents-core-subagents (63 files, 1220 tests) and auth shard
  green at OPENCLAW_VITEST_MAX_WORKERS=6; wrapper unit tests updated to
  assert serialization, freshness, requeue, idempotent install.
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Jul 18, 2026
openclaw#109874)

* test: serialize concurrent vitest mock resolution in the non-isolated runner

Root-causes the subagent-orphan-recovery flake (main run 29566318167, shard
agentic-agents-core-subagents): "includes last human message in resume when
available" and "adds config change hint..." received the bare resume template
because prod code called a different readSessionMessagesAsync mock instance
than the one the test configured.

Mechanism: vitest's BareModuleMocker.resolveMocks has no in-flight guard.
pendingIds is cleared only after all parallel resolveId RPCs settle, and every
registration re-invalidates the mock module node. In a shared isolate:false
worker, leaked async work from an earlier file (a real-timer dynamic import)
triggers a fetchModule while the next file's vi.mock registrations are still
pending, starting a second concurrent resolution pass over the same
pendingIds. The slower pass re-registers each manual mock and wipes the
already-evaluated mock module mid-import-chain: importers evaluated before the
wipe (the test file's own binding, subagent-orphan-recovery.test.ts:8) keep
factory instance A while later importers (subagent-orphan-recovery.ts via
test line 16) instantiate a fresh instance B. Only inline-factory mocks split;
vi.hoisted-stable mocks are immune, matching the CI failure signature exactly.

Fix: OpenClawNonIsolatedRunner installs a coalescing wrapper around
moduleRunner.mocker.resolveMocks (once per worker) so concurrent callers share
one pass and registration happens exactly once before any awaiting import
proceeds.

Proof:
- Deterministic repro (poison file leaving a 1ms recurring dynamic-import
  timer + resolveId jitter on session-transcript-readers) reproduced the exact
  CI assertion failures at lines 755/772 on the unfixed runner and passes 5/5
  with the fix.
- New unit tests cover coalescing, idempotent install, and fresh passes.
- Stress: 10x subagent-orphan-recovery.test.ts and 5x the full
  agentic-agents-core-subagents file set (63 files, 1220 tests) at
  OPENCLAW_VITEST_MAX_WORKERS=6, all green.

* test: requeue mock ids queued during an in-flight resolveMocks pass

Review follow-up to the resolveMocks serialization pin: upstream snapshots the
pendingIds contents at pass start and reassigns the static to [] at the end,
so ids queued during the pass's RPC window land in the abandoned array. Pure
coalescing would silently drop those registrations (upstream's racy second
pass previously processed them). The wrapper now captures the queue reference
before each pass, requeues ids pushed past the processed count, and drains
until the queue is empty, so every coalesced caller's vi.mock registration is
applied before its fetch proceeds.

Proof: new unit test "registers ids queued while a pass is in flight before
callers proceed"; deterministic zombie+jitter repro still passes 5/5; full
agentic-agents-core-subagents file set green at OPENCLAW_VITEST_MAX_WORKERS=6.

* test: chain resolveMocks passes per caller instead of sharing one pass

The coalescing pin regressed shard agentic-agents-core-auth
(models-config.providers.auth-provenance.test.ts, runs 29570918219 and
29571513381): sharing one in-flight pass broke the mocker's freshness
invariant. Upstream gives every resolveMocks caller a pass that starts at or
after its call, so ids the caller queued (vi.doUnmock before dynamic imports
in that file's beforeAll/beforeEach) are always registered before its fetch
proceeds. A coalesced caller could instead ride a pass snapshotted before its
ids were queued and import with mock state unresolved. Reproduced locally on a
cold transform cache: the file then loaded the real provider-auth warm worker
plus a live oauth-manager refresh guarded by a 120s withRefreshCallTimeout
(the 105-142s stalls and the "Test timed out in 120000ms" CI failure), and the
real resolveProviderSyntheticAuthWithPlugin returned undefined (the
mode:"none" assertion failure at line 348).

The wrapper now chains each caller onto its own sequential pass. This keeps
both invariants: serialization (a pass queued behind an in-flight one sees the
cleared queue and no-ops, so a snapshot is never registered or its mock
modules invalidated twice - the original subagent-orphan-recovery disease) and
freshness (each caller's pass starts at or after its call). Abandoned-id
requeue is preserved so ids pushed during a pass's RPC window are registered
by the next chained pass instead of dropped.

Proof:
- Cold-cache auth shard (37 files, 528 tests): failed 105-142s with the
  coalescing pin (also with a coalescing-only variant), passed 20s with the
  unfixed runner, passes 3/3 in ~20-30s with the chained pin.
- Long-timer instrumentation pinpointed the stall: provider-auth warm worker
  spawn plus oauth-manager withRefreshCallTimeout delay=120000.
- Original zombie+jitter orphan-recovery repro still passes 5/5.
- Warm agentic-agents-core-subagents (63 files, 1220 tests) and auth shard
  green at OPENCLAW_VITEST_MAX_WORKERS=6; wrapper unit tests updated to
  assert serialization, freshness, requeue, idempotent install.
tomorrowflow pushed a commit to tomorrowflow/openclaw that referenced this pull request Aug 10, 2026
openclaw#109874)

* test: serialize concurrent vitest mock resolution in the non-isolated runner

Root-causes the subagent-orphan-recovery flake (main run 29566318167, shard
agentic-agents-core-subagents): "includes last human message in resume when
available" and "adds config change hint..." received the bare resume template
because prod code called a different readSessionMessagesAsync mock instance
than the one the test configured.

Mechanism: vitest's BareModuleMocker.resolveMocks has no in-flight guard.
pendingIds is cleared only after all parallel resolveId RPCs settle, and every
registration re-invalidates the mock module node. In a shared isolate:false
worker, leaked async work from an earlier file (a real-timer dynamic import)
triggers a fetchModule while the next file's vi.mock registrations are still
pending, starting a second concurrent resolution pass over the same
pendingIds. The slower pass re-registers each manual mock and wipes the
already-evaluated mock module mid-import-chain: importers evaluated before the
wipe (the test file's own binding, subagent-orphan-recovery.test.ts:8) keep
factory instance A while later importers (subagent-orphan-recovery.ts via
test line 16) instantiate a fresh instance B. Only inline-factory mocks split;
vi.hoisted-stable mocks are immune, matching the CI failure signature exactly.

Fix: OpenClawNonIsolatedRunner installs a coalescing wrapper around
moduleRunner.mocker.resolveMocks (once per worker) so concurrent callers share
one pass and registration happens exactly once before any awaiting import
proceeds.

Proof:
- Deterministic repro (poison file leaving a 1ms recurring dynamic-import
  timer + resolveId jitter on session-transcript-readers) reproduced the exact
  CI assertion failures at lines 755/772 on the unfixed runner and passes 5/5
  with the fix.
- New unit tests cover coalescing, idempotent install, and fresh passes.
- Stress: 10x subagent-orphan-recovery.test.ts and 5x the full
  agentic-agents-core-subagents file set (63 files, 1220 tests) at
  OPENCLAW_VITEST_MAX_WORKERS=6, all green.

* test: requeue mock ids queued during an in-flight resolveMocks pass

Review follow-up to the resolveMocks serialization pin: upstream snapshots the
pendingIds contents at pass start and reassigns the static to [] at the end,
so ids queued during the pass's RPC window land in the abandoned array. Pure
coalescing would silently drop those registrations (upstream's racy second
pass previously processed them). The wrapper now captures the queue reference
before each pass, requeues ids pushed past the processed count, and drains
until the queue is empty, so every coalesced caller's vi.mock registration is
applied before its fetch proceeds.

Proof: new unit test "registers ids queued while a pass is in flight before
callers proceed"; deterministic zombie+jitter repro still passes 5/5; full
agentic-agents-core-subagents file set green at OPENCLAW_VITEST_MAX_WORKERS=6.

* test: chain resolveMocks passes per caller instead of sharing one pass

The coalescing pin regressed shard agentic-agents-core-auth
(models-config.providers.auth-provenance.test.ts, runs 29570918219 and
29571513381): sharing one in-flight pass broke the mocker's freshness
invariant. Upstream gives every resolveMocks caller a pass that starts at or
after its call, so ids the caller queued (vi.doUnmock before dynamic imports
in that file's beforeAll/beforeEach) are always registered before its fetch
proceeds. A coalesced caller could instead ride a pass snapshotted before its
ids were queued and import with mock state unresolved. Reproduced locally on a
cold transform cache: the file then loaded the real provider-auth warm worker
plus a live oauth-manager refresh guarded by a 120s withRefreshCallTimeout
(the 105-142s stalls and the "Test timed out in 120000ms" CI failure), and the
real resolveProviderSyntheticAuthWithPlugin returned undefined (the
mode:"none" assertion failure at line 348).

The wrapper now chains each caller onto its own sequential pass. This keeps
both invariants: serialization (a pass queued behind an in-flight one sees the
cleared queue and no-ops, so a snapshot is never registered or its mock
modules invalidated twice - the original subagent-orphan-recovery disease) and
freshness (each caller's pass starts at or after its call). Abandoned-id
requeue is preserved so ids pushed during a pass's RPC window are registered
by the next chained pass instead of dropped.

Proof:
- Cold-cache auth shard (37 files, 528 tests): failed 105-142s with the
  coalescing pin (also with a coalescing-only variant), passed 20s with the
  unfixed runner, passes 3/3 in ~20-30s with the chained pin.
- Long-timer instrumentation pinpointed the stall: provider-auth warm worker
  spawn plus oauth-manager withRefreshCallTimeout delay=120000.
- Original zombie+jitter orphan-recovery repro still passes 5/5.
- Warm agentic-agents-core-subagents (63 files, 1220 tests) and auth shard
  green at OPENCLAW_VITEST_MAX_WORKERS=6; wrapper unit tests updated to
  assert serialization, freshness, requeue, idempotent install.
zooqueen pushed a commit to zooqueen/openclaw that referenced this pull request Sep 11, 2026
openclaw#109874)

* test: serialize concurrent vitest mock resolution in the non-isolated runner

Root-causes the subagent-orphan-recovery flake (main run 29566318167, shard
agentic-agents-core-subagents): "includes last human message in resume when
available" and "adds config change hint..." received the bare resume template
because prod code called a different readSessionMessagesAsync mock instance
than the one the test configured.

Mechanism: vitest's BareModuleMocker.resolveMocks has no in-flight guard.
pendingIds is cleared only after all parallel resolveId RPCs settle, and every
registration re-invalidates the mock module node. In a shared isolate:false
worker, leaked async work from an earlier file (a real-timer dynamic import)
triggers a fetchModule while the next file's vi.mock registrations are still
pending, starting a second concurrent resolution pass over the same
pendingIds. The slower pass re-registers each manual mock and wipes the
already-evaluated mock module mid-import-chain: importers evaluated before the
wipe (the test file's own binding, subagent-orphan-recovery.test.ts:8) keep
factory instance A while later importers (subagent-orphan-recovery.ts via
test line 16) instantiate a fresh instance B. Only inline-factory mocks split;
vi.hoisted-stable mocks are immune, matching the CI failure signature exactly.

Fix: OpenClawNonIsolatedRunner installs a coalescing wrapper around
moduleRunner.mocker.resolveMocks (once per worker) so concurrent callers share
one pass and registration happens exactly once before any awaiting import
proceeds.

Proof:
- Deterministic repro (poison file leaving a 1ms recurring dynamic-import
  timer + resolveId jitter on session-transcript-readers) reproduced the exact
  CI assertion failures at lines 755/772 on the unfixed runner and passes 5/5
  with the fix.
- New unit tests cover coalescing, idempotent install, and fresh passes.
- Stress: 10x subagent-orphan-recovery.test.ts and 5x the full
  agentic-agents-core-subagents file set (63 files, 1220 tests) at
  OPENCLAW_VITEST_MAX_WORKERS=6, all green.

* test: requeue mock ids queued during an in-flight resolveMocks pass

Review follow-up to the resolveMocks serialization pin: upstream snapshots the
pendingIds contents at pass start and reassigns the static to [] at the end,
so ids queued during the pass's RPC window land in the abandoned array. Pure
coalescing would silently drop those registrations (upstream's racy second
pass previously processed them). The wrapper now captures the queue reference
before each pass, requeues ids pushed past the processed count, and drains
until the queue is empty, so every coalesced caller's vi.mock registration is
applied before its fetch proceeds.

Proof: new unit test "registers ids queued while a pass is in flight before
callers proceed"; deterministic zombie+jitter repro still passes 5/5; full
agentic-agents-core-subagents file set green at OPENCLAW_VITEST_MAX_WORKERS=6.

* test: chain resolveMocks passes per caller instead of sharing one pass

The coalescing pin regressed shard agentic-agents-core-auth
(models-config.providers.auth-provenance.test.ts, runs 29570918219 and
29571513381): sharing one in-flight pass broke the mocker's freshness
invariant. Upstream gives every resolveMocks caller a pass that starts at or
after its call, so ids the caller queued (vi.doUnmock before dynamic imports
in that file's beforeAll/beforeEach) are always registered before its fetch
proceeds. A coalesced caller could instead ride a pass snapshotted before its
ids were queued and import with mock state unresolved. Reproduced locally on a
cold transform cache: the file then loaded the real provider-auth warm worker
plus a live oauth-manager refresh guarded by a 120s withRefreshCallTimeout
(the 105-142s stalls and the "Test timed out in 120000ms" CI failure), and the
real resolveProviderSyntheticAuthWithPlugin returned undefined (the
mode:"none" assertion failure at line 348).

The wrapper now chains each caller onto its own sequential pass. This keeps
both invariants: serialization (a pass queued behind an in-flight one sees the
cleared queue and no-ops, so a snapshot is never registered or its mock
modules invalidated twice - the original subagent-orphan-recovery disease) and
freshness (each caller's pass starts at or after its call). Abandoned-id
requeue is preserved so ids pushed during a pass's RPC window are registered
by the next chained pass instead of dropped.

Proof:
- Cold-cache auth shard (37 files, 528 tests): failed 105-142s with the
  coalescing pin (also with a coalescing-only variant), passed 20s with the
  unfixed runner, passes 3/3 in ~20-30s with the chained pin.
- Long-timer instrumentation pinpointed the stall: provider-auth warm worker
  spawn plus oauth-manager withRefreshCallTimeout delay=120000.
- Original zombie+jitter orphan-recovery repro still passes 5/5.
- Warm agentic-agents-core-subagents (63 files, 1220 tests) and auth shard
  green at OPENCLAW_VITEST_MAX_WORKERS=6; wrapper unit tests updated to
  assert serialization, freshness, requeue, idempotent install.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintainer Maintainer-authored PR merge-risk: 🚨 automation 🚨 May affect CI, automerge, proof capture, label sync, or maintainer automation. P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant