Skip to content

Commit e045e58

Browse files
committed
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.
1 parent 459c84b commit e045e58

2 files changed

Lines changed: 49 additions & 6 deletions

File tree

‎test/non-isolated-runner.resolve-mocks.test.ts‎

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
11
// Guards the resolveMocks serialization pin: concurrent callers must coalesce
2-
// into one shared pass so a late second pass cannot re-register and invalidate
3-
// already-evaluated manual mock modules mid-import-chain.
2+
// into one shared drain so a late second pass cannot re-register and invalidate
3+
// already-evaluated manual mock modules mid-import-chain, while ids queued
4+
// during an in-flight pass still get registered before callers proceed.
45
import { describe, expect, it } from "vitest";
56
import { serializeMockerResolveMocks } from "./non-isolated-runner.js";
67

7-
// Mirrors BareModuleMocker: pendingIds is a static queue that each pass
8-
// snapshots at start and reassigns to [] at the end.
8+
// Mirrors BareModuleMocker.resolveMocks: snapshots the static queue's contents
9+
// at pass start, awaits its RPCs, then reassigns the static to [] so ids
10+
// pushed during the await land in the abandoned array.
911
class FakeMocker {
1012
static pendingIds: unknown[] = [];
1113
passes = 0;
1214
active = 0;
1315
maxConcurrentPasses = 0;
16+
processed: unknown[] = [];
1417

1518
async resolveMocks(): Promise<void> {
19+
if (FakeMocker.pendingIds.length === 0) {
20+
return;
21+
}
1622
this.active += 1;
1723
this.maxConcurrentPasses = Math.max(this.maxConcurrentPasses, this.active);
1824
this.passes += 1;
25+
const snapshot = [...FakeMocker.pendingIds];
1926
// Simulate the parallel resolveId RPC round-trips inside one pass.
2027
await new Promise((resolve) => {
2128
setTimeout(resolve, 1);
2229
});
30+
this.processed.push(...snapshot);
2331
FakeMocker.pendingIds = [];
2432
this.active -= 1;
2533
}
@@ -35,6 +43,25 @@ describe("serializeMockerResolveMocks", () => {
3543

3644
expect(mocker.maxConcurrentPasses).toBe(1);
3745
expect(mocker.passes).toBe(1);
46+
expect(mocker.processed).toEqual(["mock-a", "mock-b"]);
47+
expect(FakeMocker.pendingIds).toEqual([]);
48+
});
49+
50+
it("registers ids queued while a pass is in flight before callers proceed", async () => {
51+
FakeMocker.pendingIds = ["mock-a"];
52+
const mocker = new FakeMocker();
53+
serializeMockerResolveMocks(mocker);
54+
55+
const first = mocker.resolveMocks();
56+
// Upstream would abandon this push when it reassigns pendingIds to [];
57+
// the wrapper must requeue it into a follow-up pass.
58+
FakeMocker.pendingIds.push("mock-late");
59+
const coalesced = mocker.resolveMocks();
60+
await Promise.all([first, coalesced]);
61+
62+
expect(mocker.processed).toEqual(["mock-a", "mock-late"]);
63+
expect(mocker.maxConcurrentPasses).toBe(1);
64+
expect(mocker.passes).toBe(2);
3865
expect(FakeMocker.pendingIds).toEqual([]);
3966
});
4067

@@ -61,6 +88,7 @@ describe("serializeMockerResolveMocks", () => {
6188
await mocker.resolveMocks();
6289

6390
expect(mocker.passes).toBe(2);
91+
expect(mocker.processed).toEqual(["mock-a", "mock-b"]);
6492
expect(FakeMocker.pendingIds).toEqual([]);
6593
});
6694
});

‎test/non-isolated-runner.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ const SERIALIZED_RESOLVE_MOCKS = Symbol.for("openclaw.serializedResolveMocks");
243243
// already-evaluated manual mock modules mid-import-chain, so importers before
244244
// the wipe hold one factory instance and later importers get a fresh one
245245
// (vi.mocked(...) on the test's binding silently stops reaching prod). Coalesce
246-
// concurrent callers into one shared pass so registration happens exactly once
246+
// concurrent callers into one shared drain so registration happens exactly once
247247
// before any awaiting import proceeds.
248248
export function serializeMockerResolveMocks(
249249
mocker: SerializableMocker & { [SERIALIZED_RESOLVE_MOCKS]?: boolean },
@@ -253,9 +253,24 @@ export function serializeMockerResolveMocks(
253253
}
254254
mocker[SERIALIZED_RESOLVE_MOCKS] = true;
255255
const original = mocker.resolveMocks.bind(mocker);
256+
const statics = mocker.constructor as { pendingIds?: unknown[] };
257+
const drain = async (): Promise<void> => {
258+
do {
259+
const queue = statics.pendingIds;
260+
const processedCount = queue?.length ?? 0;
261+
await original();
262+
// Upstream snapshots the queue contents at pass start and reassigns the
263+
// pendingIds static to [] at the end, so ids queued during the pass's
264+
// RPC window land in the abandoned array. Requeue them or a coalesced
265+
// caller's vi.mock registration would be silently dropped.
266+
if (queue && queue !== statics.pendingIds && queue.length > processedCount) {
267+
statics.pendingIds?.push(...queue.slice(processedCount));
268+
}
269+
} while ((statics.pendingIds?.length ?? 0) > 0);
270+
};
256271
let inflight: Promise<void> | null = null;
257272
mocker.resolveMocks = () => {
258-
inflight ??= original().finally(() => {
273+
inflight ??= drain().finally(() => {
259274
inflight = null;
260275
});
261276
return inflight;

0 commit comments

Comments
 (0)