Skip to content

Commit e0399ba

Browse files
committed
test(code-mode): await terminal subscribed bridge outcomes
1 parent 59e44c2 commit e0399ba

3 files changed

Lines changed: 60 additions & 26 deletions

File tree

‎src/agents/code-mode.bridge.host-denial.test.ts‎

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
resetCodeModeTestState,
3535
resultDetails,
3636
testing,
37+
waitUntilCompleted,
3738
} from "./code-mode.test-support.js";
3839
import { consumeTrustedToolNoStartError } from "./tool-result-error.js";
3940
import { createToolTerminalObserver } from "./tool-terminal-outcome.js";
@@ -80,16 +81,21 @@ function createHostHarness(
8081
);
8182
applyCodeModeCatalog({ ...harness, tools: [...harness.tools, shell] });
8283
let nextCall = 0;
84+
const run = async (code = deniedCode) =>
85+
resultDetails(
86+
await expectDefined(harness.tools[0], "outer exec").execute(`host-${nextCall++}`, { code }),
87+
);
88+
const complete = (details: Record<string, unknown>) =>
89+
waitUntilCompleted({ details, waitTool: expectDefined(harness.tools[1], "wait") });
8390
return {
8491
...harness,
8592
source,
8693
shell,
8794
spawn: vi.spyOn(getProcessSupervisor(), "spawn"),
8895
remote: vi.spyOn(nodeHost, "executeNodeHostCommand"),
89-
run: async (code = deniedCode) =>
90-
resultDetails(
91-
await expectDefined(harness.tools[0], "outer exec").execute(`host-${nextCall++}`, { code }),
92-
),
96+
run,
97+
complete,
98+
runToCompletion: async (code = deniedCode) => complete(await run(code)),
9399
};
94100
}
95101

@@ -143,7 +149,7 @@ describe("Code Mode subscribed host denial", () => {
143149
const observeToolTerminal = vi.fn(createToolTerminalObserver(`run-code-mode-host-${policy}`));
144150
const harness = createHostHarness({ name: `host-${policy}`, defaults, observeToolTerminal });
145151
try {
146-
const details = await harness.run(
152+
const details = await harness.runToCompletion(
147153
`return await exec(${JSON.stringify({ command: "printf host-denied", ...args })});`,
148154
);
149155
expect(details).toMatchObject({
@@ -192,9 +198,25 @@ describe("Code Mode subscribed host denial", () => {
192198
},
193199
};
194200
});
195-
const harness = createHostHarness({ name: `hook-${change}` });
201+
const timeoutMs = 1_500;
202+
const harness = createHostHarness({
203+
name: `hook-${change}`,
204+
timeoutMs,
205+
onToolStreamBoundary:
206+
change === "repair"
207+
? () => {
208+
// Exhaust the call budget after the real command completes, before VM restore.
209+
vi.advanceTimersByTime(timeoutMs);
210+
}
211+
: undefined,
212+
});
213+
if (change === "repair") {
214+
vi.useFakeTimers({ toFake: ["Date", "setTimeout", "clearTimeout"] });
215+
}
196216
try {
197-
const details = await harness.run(
217+
const waitTool = expectDefined(harness.tools[1], "wait");
218+
const wait = vi.spyOn(waitTool, "execute");
219+
const details = await harness.runToCompletion(
198220
change === "repair"
199221
? deniedCode
200222
: 'return await exec({ command: "printf initial", host: "gateway" });',
@@ -209,13 +231,15 @@ describe("Code Mode subscribed host denial", () => {
209231
expect(details.value).toMatchObject({ status: "blocked", reason: "hook veto" });
210232
}
211233
if (change === "repair") {
234+
expect(wait).toHaveBeenCalledOnce();
212235
expect(details.value).toMatchObject({ exitCode: 0, aggregated: "host-corrected" });
213236
}
214237
expect(consumeRepairableCodeModeFailure(details)).toBe(
215238
change === "deny" || change === "invalid",
216239
);
217240
} finally {
218241
harness.dispose();
242+
vi.useRealTimers();
219243
}
220244
},
221245
);
@@ -257,7 +281,7 @@ describe("Code Mode subscribed host denial", () => {
257281
});
258282
try {
259283
const allowed = decision === "allow-once" || decision === "allow-always";
260-
const details = await harness.run();
284+
const details = await harness.runToCompletion();
261285
expect(details.status).toBe("failed");
262286
expect(consumeRepairableCodeModeFailure(details)).toBe(allowed);
263287
expect(before).toHaveBeenCalledOnce();
@@ -276,7 +300,7 @@ describe("Code Mode subscribed host denial", () => {
276300
expect(harness.spawn).not.toHaveBeenCalled();
277301
expect(harness.remote).not.toHaveBeenCalled();
278302
if (allowed) {
279-
const corrected = await harness.run(
303+
const corrected = await harness.runToCompletion(
280304
'return await exec({ command: "printf approved-correction", host: "gateway" });',
281305
);
282306
expect(corrected).toMatchObject({
@@ -311,7 +335,7 @@ describe("Code Mode subscribed host denial", () => {
311335
});
312336
const harness = createHostHarness({ name: "approval-freeze" });
313337
try {
314-
const details = await harness.run();
338+
const details = await harness.runToCompletion();
315339
expect(details.error).toContain("requested node");
316340
expect(consumeRepairableCodeModeFailure(details)).toBe(true);
317341
expect(approve).toHaveBeenCalledOnce();
@@ -418,7 +442,7 @@ describe("Code Mode subscribed host denial", () => {
418442
);
419443
applyCodeModeCatalog({ ...harness, tools: [...harness.tools, capture] });
420444
try {
421-
const details = await harness.run();
445+
const details = await harness.runToCompletion();
422446
expect(details.error).toContain(replacement.message);
423447
expect(producerError).toBeInstanceOf(Error);
424448
expect(consumeTrustedToolNoStartError(producerError)).toBe(false);
@@ -437,7 +461,7 @@ describe("Code Mode subscribed host denial", () => {
437461
);
438462
const harness = createHostHarness({ name: "after-observer" });
439463
try {
440-
const details = await harness.run();
464+
const details = await harness.runToCompletion();
441465
await vi.waitFor(() => expect(after).toHaveBeenCalledOnce());
442466
expect(details.error).toContain("exec host not allowed");
443467
expect(consumeRepairableCodeModeFailure(details)).toBe(true);
@@ -548,6 +572,7 @@ describe("Code Mode subscribed host denial", () => {
548572
await vi.waitFor(() => expect(harness.subscription.getItemLifecycle().activeCount).toBe(0));
549573
expect(pending.bridgeDispatch.potentiallyMutatingDispatches).toBe(1);
550574
}
575+
details = await harness.complete(details);
551576
expect(details).toMatchObject({ status: "failed", bridgeDispatchStarted: true });
552577
expect(details.error).toContain("exec host not allowed");
553578
expect(await fs.readFile(marker, "utf8")).toBe("applied\n");
@@ -592,7 +617,7 @@ describe("Code Mode subscribed host denial", () => {
592617
applyCodeModeCatalog({ ...harness, tools: [...harness.tools, target] });
593618
}
594619
try {
595-
const details = await harness.run(
620+
const details = await harness.runToCompletion(
596621
kind === "unbranded" || kind === "input-error"
597622
? "await untrusted({});"
598623
: `return await exec({ command: "printf no", host: "gateway" });`,
@@ -649,12 +674,14 @@ describe("Code Mode subscribed host denial", () => {
649674
bindAuthorizedClientVoiceConfirmation({ grant, runId: harness.runId });
650675
try {
651676
expect(checkClientVoiceToolConfirmationPolicy(policy).allowed).toBe(true);
652-
const denied = await harness.run(`return await exec(${JSON.stringify(args)});`);
677+
const denied = await harness.runToCompletion(`return await exec(${JSON.stringify(args)});`);
653678
expect(denied.error).toContain("exec host not allowed");
654679
expect(consumeRepairableCodeModeFailure(denied)).toBe(true);
655680
expect(checkClientVoiceToolConfirmationPolicy(policy).allowed).toBe(false);
656681
for (const input of [args, { ...args, host: "gateway" }]) {
657-
const blocked = await harness.run(`return await exec(${JSON.stringify(input)});`);
682+
const blocked = await harness.runToCompletion(
683+
`return await exec(${JSON.stringify(input)});`,
684+
);
658685
expect(blocked).toMatchObject({
659686
status: "completed",
660687
value: { status: "blocked", deniedReason: "client-voice-confirmation" },
@@ -689,11 +716,11 @@ describe("Code Mode subscribed host denial", () => {
689716
return mode === "json" ? JSON.parse(serialized) : { ...settled };
690717
});
691718
try {
692-
const details = await harness.run();
719+
const details = await harness.runToCompletion();
693720
expect(details.error).toContain("exec host not allowed");
694721
expect(consumeRepairableCodeModeFailure(details)).toBe(mode === "reuse");
695722
if (mode === "reuse") {
696-
const reused = await harness.run();
723+
const reused = await harness.runToCompletion();
697724
expect(consumeRepairableCodeModeFailure(reused)).toBe(false);
698725
} else {
699726
expect(consumeTrustedToolNoStartError(first)).toBe(true);

‎src/agents/code-mode.bridge.lifecycle.test.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
pluginToolWithExecute,
1313
resetCodeModeTestState,
1414
resultDetails,
15+
runUntilCompleted,
1516
testing,
1617
} from "./code-mode.test-support.js";
1718
import { buildEmbeddedRunPayloads } from "./embedded-agent-runner/run/payloads.js";
@@ -147,12 +148,11 @@ describe("Code Mode subscribed bridge lifecycle", () => {
147148
applyCodeModeCatalog({ ...harness, tools: [...harness.tools, target] });
148149

149150
try {
150-
const result = resultDetails(
151-
await expectDefined(harness.tools[0], "Code Mode exec test invariant").execute(
152-
"code-call-circular-flush",
153-
{ code: "return await release_flush({});" },
154-
),
155-
);
151+
const result = await runUntilCompleted({
152+
execTool: expectDefined(harness.tools[0], "Code Mode exec test invariant"),
153+
waitTool: expectDefined(harness.tools[1], "Code Mode wait test invariant"),
154+
code: "return await release_flush({});",
155+
});
156156

157157
expect(result.status, JSON.stringify(result)).toBe("completed");
158158
expect(result.value).toEqual({ released: true });

‎src/agents/code-mode.test-support.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,15 +172,22 @@ export async function runUntilCompleted(params: {
172172
language?: "javascript" | "typescript";
173173
restartSafe?: boolean;
174174
}) {
175-
// Code Mode may return a waiting state before completion; tests poll through
176-
// the public wait tool instead of reaching into activeRuns.
177-
let details = resultDetails(
175+
const details = resultDetails(
178176
await params.execTool.execute("code-call-1", {
179177
code: params.code,
180178
language: params.language,
181179
restartSafe: params.restartSafe,
182180
}),
183181
);
182+
return await waitUntilCompleted({ details, waitTool: params.waitTool });
183+
}
184+
185+
export async function waitUntilCompleted(params: {
186+
details: Record<string, unknown>;
187+
waitTool: AnyAgentTool;
188+
}) {
189+
// Resume the existing run through public waits; never replay its actions.
190+
let details = params.details;
184191
for (let index = 0; index < 8 && details.status === "waiting"; index += 1) {
185192
const runId = details.runId;
186193
expect(typeof runId).toBe("string");

0 commit comments

Comments
 (0)