|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { spawnSync } from "node:child_process"; |
| 3 | +import { randomUUID } from "node:crypto"; |
| 4 | +import { createPermissionGate } from "../permission/gate.js"; |
| 5 | +import { createAgentToolset } from "./tools.js"; |
| 6 | +import type { BackgroundShellExit } from "../shell/background-shell.js"; |
| 7 | +import { buildShellBackgroundMessage } from "../session/runtime-assembly.js"; |
| 8 | +import { OPERATOR_ORIGINATED_FLAG } from "../agent/message-provenance.js"; |
| 9 | + |
| 10 | +function gate(cwd: string) { |
| 11 | + return createPermissionGate({ |
| 12 | + approvals: [], |
| 13 | + interactive: false, |
| 14 | + skipPermissions: true, |
| 15 | + reactorGated: false, |
| 16 | + cwd, |
| 17 | + }); |
| 18 | +} |
| 19 | + |
| 20 | +describe("background shell through the agent toolset", () => { |
| 21 | + test("run_shell background:true returns a handle and delivers the exit on exit", async () => { |
| 22 | + const exits: BackgroundShellExit[] = []; |
| 23 | + const toolset = await createAgentToolset({ |
| 24 | + cwd: process.cwd(), |
| 25 | + permissionGate: gate(process.cwd()), |
| 26 | + onOperatorGate: async () => ({ kind: "cancel" as const }), |
| 27 | + onBackgroundShellExit: (exit) => exits.push(exit), |
| 28 | + }); |
| 29 | + try { |
| 30 | + const started = await toolset.dynamicRunner.run( |
| 31 | + { |
| 32 | + id: "bg-start", |
| 33 | + name: "run_shell", |
| 34 | + arguments: { command: "sleep 0.4; echo bg-done", background: true }, |
| 35 | + }, |
| 36 | + new AbortController().signal, |
| 37 | + ); |
| 38 | + expect(started.isError).not.toBe(true); |
| 39 | + const parsed = JSON.parse(String(started.content)) as { shell_id: string }; |
| 40 | + const snapshotNow = await toolset.dynamicRunner.run( |
| 41 | + { |
| 42 | + id: "bg-collect", |
| 43 | + name: "shell_collect", |
| 44 | + arguments: { shell_id: parsed.shell_id, action: "collect" }, |
| 45 | + }, |
| 46 | + new AbortController().signal, |
| 47 | + ); |
| 48 | + expect(JSON.parse(String(snapshotNow.content))).toMatchObject({ status: "running" }); |
| 49 | + const final = await toolset.dynamicRunner.run( |
| 50 | + { |
| 51 | + id: "bg-collect2", |
| 52 | + name: "shell_collect", |
| 53 | + arguments: { shell_id: parsed.shell_id, action: "collect", wait_ms: 5_000 }, |
| 54 | + }, |
| 55 | + new AbortController().signal, |
| 56 | + ); |
| 57 | + const result = JSON.parse(String(final.content)) as { |
| 58 | + status: string; |
| 59 | + exit_code: number; |
| 60 | + output: string; |
| 61 | + }; |
| 62 | + expect(result).toMatchObject({ status: "completed", exit_code: 0 }); |
| 63 | + expect(result.output).toContain("bg-done"); |
| 64 | + await new Promise((r) => setTimeout(r, 50)); |
| 65 | + expect(exits).toHaveLength(1); |
| 66 | + expect(exits[0]!.id).toBe(parsed.shell_id); |
| 67 | + const message = buildShellBackgroundMessage(exits[0]!); |
| 68 | + expect(message.headers.messageId).toBe(`bg-shell-${parsed.shell_id}@local`); |
| 69 | + expect(message.ref.mailbox).toBe("system"); |
| 70 | + expect(message.flags).not.toContain(OPERATOR_ORIGINATED_FLAG); |
| 71 | + expect(message.content).toContain("exit code 0"); |
| 72 | + expect(message.content).toContain("bg-done"); |
| 73 | + } finally { |
| 74 | + await toolset.dispose(); |
| 75 | + } |
| 76 | + }); |
| 77 | + |
| 78 | + test("shell_collect cancel kills the session's own child process group", async () => { |
| 79 | + const token = `ic_toolset_cancel_${randomUUID()}`; |
| 80 | + const toolset = await createAgentToolset({ |
| 81 | + cwd: process.cwd(), |
| 82 | + permissionGate: gate(process.cwd()), |
| 83 | + onOperatorGate: async () => ({ kind: "cancel" as const }), |
| 84 | + }); |
| 85 | + try { |
| 86 | + const started = await toolset.dynamicRunner.run( |
| 87 | + { |
| 88 | + id: "c-start", |
| 89 | + name: "run_shell", |
| 90 | + arguments: { |
| 91 | + command: `sleep 600 # ${token}`, |
| 92 | + background: true, |
| 93 | + }, |
| 94 | + }, |
| 95 | + new AbortController().signal, |
| 96 | + ); |
| 97 | + const { shell_id } = JSON.parse(String(started.content)) as { shell_id: string }; |
| 98 | + const cancelled = await toolset.dynamicRunner.run( |
| 99 | + { |
| 100 | + id: "c-cancel", |
| 101 | + name: "shell_collect", |
| 102 | + arguments: { shell_id, action: "cancel" }, |
| 103 | + }, |
| 104 | + new AbortController().signal, |
| 105 | + ); |
| 106 | + expect(JSON.parse(String(cancelled.content))).toMatchObject({ status: "cancelling" }); |
| 107 | + await new Promise((r) => setTimeout(r, 300)); |
| 108 | + const probe = spawnSync("pgrep", ["-f", token], { encoding: "utf8" }); |
| 109 | + expect(probe.stdout?.trim() ?? "").toBe(""); |
| 110 | + expect(probe.status).not.toBe(0); |
| 111 | + } finally { |
| 112 | + await toolset.dispose(); |
| 113 | + } |
| 114 | + }); |
| 115 | + |
| 116 | + test("toolset dispose kills every live background process group", async () => { |
| 117 | + const token = `ic_toolset_dispose_${randomUUID()}`; |
| 118 | + const toolset = await createAgentToolset({ |
| 119 | + cwd: process.cwd(), |
| 120 | + permissionGate: gate(process.cwd()), |
| 121 | + onOperatorGate: async () => ({ kind: "cancel" as const }), |
| 122 | + }); |
| 123 | + const started = await toolset.dynamicRunner.run( |
| 124 | + { |
| 125 | + id: "d-start", |
| 126 | + name: "run_shell", |
| 127 | + arguments: { command: `sleep 600 # ${token}`, background: true }, |
| 128 | + }, |
| 129 | + new AbortController().signal, |
| 130 | + ); |
| 131 | + expect(started.isError).not.toBe(true); |
| 132 | + await toolset.dispose(); |
| 133 | + await new Promise((r) => setTimeout(r, 300)); |
| 134 | + const probe = spawnSync("pgrep", ["-f", token], { encoding: "utf8" }); |
| 135 | + expect(probe.stdout?.trim() ?? "").toBe(""); |
| 136 | + expect(probe.status).not.toBe(0); |
| 137 | + }); |
| 138 | +}); |
0 commit comments