|
| 1 | +/** |
| 2 | + * Persist close_agent must surface a leftover-child posix dispose, not treat |
| 3 | + * it as a successful bounded close. |
| 4 | + */ |
| 5 | +import { describe, expect, test } from "bun:test"; |
| 6 | +import { mkdtemp } from "node:fs/promises"; |
| 7 | +import { tmpdir } from "node:os"; |
| 8 | +import { join } from "node:path"; |
| 9 | + |
| 10 | +import type { ReactorEmittedEvent } from "@intx/inference"; |
| 11 | + |
| 12 | +import { withMockedModuleDuring } from "../../tests/helpers/mock-module.js"; |
| 13 | +import { createPermissionGate } from "../permission/gate.js"; |
| 14 | +import type { RunSubAgentParams } from "./types.js"; |
| 15 | + |
| 16 | +const permissionGate = createPermissionGate({ |
| 17 | + approvals: [], |
| 18 | + interactive: false, |
| 19 | + skipPermissions: true, |
| 20 | + reactorGated: false, |
| 21 | +}); |
| 22 | + |
| 23 | +function stubAgent() { |
| 24 | + return { |
| 25 | + send: async () => { |
| 26 | + await new Promise((resolve) => setTimeout(resolve, 20)); |
| 27 | + return { |
| 28 | + type: "reply" as const, |
| 29 | + reply: "done", |
| 30 | + turn: { role: "assistant", content: [] }, |
| 31 | + }; |
| 32 | + }, |
| 33 | + stream: () => (async function* (): AsyncGenerator<ReactorEmittedEvent> {})(), |
| 34 | + deliver: () => {}, |
| 35 | + close: async () => {}, |
| 36 | + setSource: () => {}, |
| 37 | + setSources: () => {}, |
| 38 | + history: async () => [], |
| 39 | + checkpoints: async () => [], |
| 40 | + readAt: async () => [], |
| 41 | + blobReader: {}, |
| 42 | + }; |
| 43 | +} |
| 44 | + |
| 45 | +describe("persist close_agent leftover dispose", () => { |
| 46 | + test("onAgentReady close rejects when posix dispose reports leftover children", async () => { |
| 47 | + const cwd = await mkdtemp(join(tmpdir(), "corbits-persist-close-")); |
| 48 | + |
| 49 | + await withMockedModuleDuring( |
| 50 | + import.meta.resolve("@intx/tools-posix"), |
| 51 | + (real: typeof import("@intx/tools-posix")) => ({ |
| 52 | + ...real, |
| 53 | + createPosixTools: (opts: Parameters<typeof real.createPosixTools>[0]) => |
| 54 | + Object.assign(real.createPosixTools(opts), { |
| 55 | + dispose: async () => { |
| 56 | + throw new Error("1 shell child process still live after 2000ms reap"); |
| 57 | + }, |
| 58 | + }), |
| 59 | + }), |
| 60 | + async () => |
| 61 | + withMockedModuleDuring( |
| 62 | + import.meta.resolve("../agent/live-tool-dispatch.js"), |
| 63 | + (real: typeof import("../agent/live-tool-dispatch.js")) => ({ |
| 64 | + ...real, |
| 65 | + createAgentWithLiveToolDispatch: async () => |
| 66 | + stubAgent() as unknown as Awaited< |
| 67 | + ReturnType<typeof real.createAgentWithLiveToolDispatch> |
| 68 | + >, |
| 69 | + }), |
| 70 | + async () => { |
| 71 | + const { runSubAgent } = await import("./run.js"); |
| 72 | + let handles: |
| 73 | + | { |
| 74 | + close: (deadlineMs?: number) => Promise<void>; |
| 75 | + } |
| 76 | + | undefined; |
| 77 | + const params: RunSubAgentParams = { |
| 78 | + cwd, |
| 79 | + workdirBase: join(cwd, ".ctx"), |
| 80 | + permissionGate, |
| 81 | + provider: { providerName: "test", baseURL: "http://localhost", model: "test-model" }, |
| 82 | + description: "persist close leftover probe", |
| 83 | + prompt: "finish the first turn", |
| 84 | + persist: true, |
| 85 | + onAgentReady: (h) => { |
| 86 | + handles = h; |
| 87 | + }, |
| 88 | + }; |
| 89 | + const result = await runSubAgent(params); |
| 90 | + expect(result.agentRetained).toBe(true); |
| 91 | + if (handles === undefined) throw new Error("onAgentReady never fired"); |
| 92 | + await expect(handles.close(1000)).rejects.toThrow(/still live after 2000ms reap/); |
| 93 | + }, |
| 94 | + ), |
| 95 | + ); |
| 96 | + }); |
| 97 | +}); |
0 commit comments