Skip to content

Open-source, local-first AI agent for real work.

2.6 KB
Raw Blame
import type { BackgroundCommandSnapshot } from "@vetta/runtime-tools";
import { describe, expect, it } from "vitest";
import { readCodingAgentBackgroundTasksObservation } from "../../src/execution/background/background-work-session-extension-contract.js";
import { buildCodingAgentBackgroundCommandNotification } from "../../src/execution/background/notification.js";

describe("Coding Agent background command notification", () => {
	it("formats a completed command for model context", () => {
		const completed: BackgroundCommandSnapshot = {
			id: "b1",
			command: "echo done",
			cwd: "C:/workspace",
			status: "completed",
			outputFile: "C:/tmp/task.log",
			exitCode: 0,
			startedAt: 1,
			endedAt: 2,
			toolCallId: "tool-call-1",
			tail: "done",
		};
		expect(buildCodingAgentBackgroundCommandNotification(completed)).toContain("<status>completed</status>");
	});

	it("retains caller cancellation guidance in the product layer", () => {
		const stopped: BackgroundCommandSnapshot = {
			id: "b2",
			command: "server",
			cwd: "C:/workspace",
			status: "killed",
			outputFile: "C:/tmp/task.log",
			exitCode: undefined,
			startedAt: 1,
			endedBy: "caller",
			tail: "",
		};
		expect(buildCodingAgentBackgroundCommandNotification(stopped)).toContain("The user manually stopped");
	});

	it("reports leftover processes as exited rather than with an exit code they never had", () => {
		const exited: BackgroundCommandSnapshot = {
			id: "b3",
			command: "nohup server &",
			cwd: "/workspace",
			status: "completed",
			outputFile: "/tmp/task.log",
			exitCode: undefined,
			startedAt: 1,
			endedAt: 2,
			tail: "",
			kind: "detached-process",
			processGroupId: 4242,
		};
		const text = buildCodingAgentBackgroundCommandNotification(exited);
		expect(text).toContain("exited (all processes it left running have ended)");
		expect(text).not.toContain("exit code 0");
	});

	it("lets detached-process snapshots through the host observation boundary", () => {
		const tasks = [
			{
				id: "b1",
				command: "sleep 30 &",
				cwd: "/workspace",
				status: "completed",
				outputFile: "/tmp/b1.log",
				startedAt: 1,
				tail: "",
				exitCode: 0,
				detachedTaskId: "b2",
			},
			{
				id: "b2",
				command: "sleep 30 &",
				cwd: "/workspace",
				status: "running",
				outputFile: "/tmp/b2.log",
				startedAt: 2,
				tail: "",
				kind: "detached-process",
				processGroupId: 4242,
			},
		];
		const read = readCodingAgentBackgroundTasksObservation({
			type: "session.extension",
			extensionId: "coding-agent.background-work",
			event: "tasks.changed",
			payload: tasks,
		} as never);
		expect(read).toMatchObject(tasks);
	});
});