Skip to content

Commit 8064cbd

Browse files
committed
test: add process group termination and display_name tests
- Test that child processes are terminated when parent is killed (validates set -m) - Test that display_name round-trips through spawn and list
1 parent 63dfc3a commit 8064cbd

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

src/node/services/backgroundProcessManager.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,50 @@ describe("BackgroundProcessManager", () => {
309309
});
310310
});
311311

312+
describe("process group termination", () => {
313+
it("should terminate child processes when parent is killed", async () => {
314+
// This test validates that set -m creates a process group where PID === PGID,
315+
// allowing kill -PID to terminate the entire process tree.
316+
317+
// Spawn a parent that creates a child process
318+
// The parent runs: (sleep 60 &); wait
319+
// This creates: parent bash -> child sleep
320+
const result = await manager.spawn(
321+
runtime,
322+
testWorkspaceId,
323+
"bash -c 'sleep 60 & wait'",
324+
{ cwd: process.cwd() }
325+
);
326+
327+
expect(result.success).toBe(true);
328+
if (!result.success) return;
329+
330+
// Give the child process time to start
331+
await new Promise((resolve) => setTimeout(resolve, 100));
332+
333+
// Verify process is running
334+
const procBefore = await manager.getProcess(result.processId);
335+
expect(procBefore?.status).toBe("running");
336+
337+
// Terminate - this should kill both parent and child via process group
338+
await manager.terminate(result.processId);
339+
340+
// Verify parent is killed
341+
const procAfter = await manager.getProcess(result.processId);
342+
expect(procAfter?.status).toBe("killed");
343+
344+
// Wait a moment for any orphaned processes to show up
345+
await new Promise((resolve) => setTimeout(resolve, 500));
346+
347+
// Verify no orphaned sleep processes from our test
348+
// (checking via ps would be flaky, so we rely on the exit code being set,
349+
// which only happens after the entire process group is dead)
350+
const exitCode = procAfter?.exitCode;
351+
expect(exitCode).not.toBeNull();
352+
expect(exitCode).toBeGreaterThanOrEqual(128); // Signal exit code
353+
});
354+
});
355+
312356
describe("exit_code file", () => {
313357
it("should write exit_code file when process exits", async () => {
314358
const result = await manager.spawn(runtime, testWorkspaceId, "exit 42", {

src/node/services/tools/bash_background_list.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,38 @@ describe("bash_background_list tool", () => {
109109
tempDir[Symbol.dispose]();
110110
});
111111

112+
it("should include display_name in listed processes", async () => {
113+
const manager = new BackgroundProcessManager();
114+
const tempDir = new TestTempDir("test-bash-bg-list");
115+
const runtime = createTestRuntime(tempDir.path);
116+
const config = createTestToolConfig(process.cwd(), { sessionsDir: tempDir.path });
117+
config.runtimeTempDir = tempDir.path;
118+
config.backgroundProcessManager = manager;
119+
120+
// Spawn a process with display_name
121+
const spawnResult = await manager.spawn(runtime, "test-workspace", "sleep 10", {
122+
cwd: process.cwd(),
123+
displayName: "Dev Server",
124+
});
125+
126+
if (!spawnResult.success) {
127+
throw new Error("Failed to spawn process");
128+
}
129+
130+
const tool = createBashBackgroundListTool(config);
131+
const result = (await tool.execute!({}, mockToolCallOptions)) as BashBackgroundListResult;
132+
133+
expect(result.success).toBe(true);
134+
if (result.success) {
135+
expect(result.processes.length).toBe(1);
136+
expect(result.processes[0].display_name).toBe("Dev Server");
137+
}
138+
139+
// Cleanup
140+
await manager.cleanup("test-workspace");
141+
tempDir[Symbol.dispose]();
142+
});
143+
112144
it("should only list processes for the current workspace", async () => {
113145
const manager = new BackgroundProcessManager();
114146
const tempDir = new TestTempDir("test-bash-bg-list");

0 commit comments

Comments
 (0)