Skip to content

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

3.7 KB
Raw Blame
import type { RuntimeToolResult } from "@vetta/runtime-core/kernel";
import type { RuntimeToolResultArtifactStore } from "@vetta/runtime-tools";
import { describe, expect, it, vi } from "vitest";
import { createCodingAgentCodingToolResultPolicy } from "../src/tool-results/result-policy.js";

const context = {
	sessionId: "session-1",
	turnId: "turn-1",
	toolCallId: "call-1",
	toolName: "read",
};

describe("coding agent coding tool result policy", () => {
	it("preserves small results", async () => {
		const store = artifactStore();
		const policy = createCodingAgentCodingToolResultPolicy({ artifactStore: store, maxInlineResultBytes: 8 });
		const small = textResult("small");

		expect(await policy.project(small, context)).toBe(small);
		expect(store.write).not.toHaveBeenCalled();
	});

	it("offloads the full result while preserving details and images", async () => {
		const store = artifactStore();
		const policy = createCodingAgentCodingToolResultPolicy({ artifactStore: store, maxInlineResultBytes: 20 });
		const details = { exitCode: 0 };
		const result: RuntimeToolResult = {
			content: [
				{ type: "text", text: "start-0123456789-end-abcdefghij" },
				{ type: "image", data: "image-data", mimeType: "image/png" },
			],
			details,
		};

		const projected = await policy.project(result, context);

		expect(projected.details).toBe(details);
		expect(projected.content[0]?.type === "text" ? projected.content[0].text : "").toContain(
			"Full result: artifact.json",
		);
		expect(projected.content[1]).toEqual({ type: "image", data: "image-data", mimeType: "image/png" });
		const request = vi.mocked(store.write).mock.calls[0]?.[0];
		expect(request ? JSON.parse(request.data) : undefined).toEqual({ content: result.content, details });
		expect(request?.byteLength).toBe(new TextEncoder().encode(request?.data ?? "").length);
	});

	it("does not offload image payloads that remain attached", async () => {
		const store = artifactStore();
		const policy = createCodingAgentCodingToolResultPolicy({ artifactStore: store, maxInlineResultBytes: 20 });
		const result: RuntimeToolResult = {
			content: [
				{ type: "text", text: "ok" },
				{ type: "image", data: "x".repeat(40), mimeType: "image/png" },
			],
		};

		const projected = await policy.project(result, context);

		expect(projected).toBe(result);
		expect(store.write).not.toHaveBeenCalled();
	});

	it("includes details in the inline result budget", async () => {
		const store = artifactStore();
		const policy = createCodingAgentCodingToolResultPolicy({ artifactStore: store, maxInlineResultBytes: 20 });
		const result: RuntimeToolResult = {
			content: [{ type: "text", text: "ok" }],
			details: { diagnostic: "y".repeat(40) },
		};

		const projected = await policy.project(result, context);

		expect(store.write).toHaveBeenCalledOnce();
		expect(projected.content[0]?.type === "text" ? projected.content[0].text : "").toContain(
			"Full result: artifact.json",
		);
		expect(projected.details).toBe(result.details);
	});

	it("keeps the only result copy when artifact storage fails", async () => {
		const store: RuntimeToolResultArtifactStore = {
			write: vi.fn(async () => {
				throw new Error("disk unavailable");
			}),
		};
		const policy = createCodingAgentCodingToolResultPolicy({ artifactStore: store, maxInlineResultBytes: 4 });
		const result = textResult("large result");

		expect(await policy.project(result, context)).toBe(result);
	});
});

function artifactStore(): RuntimeToolResultArtifactStore & { readonly write: ReturnType<typeof vi.fn> } {
	return { write: vi.fn(async () => ({ reference: "artifact.json" })) };
}

function textResult(text: string): RuntimeToolResult {
	return { content: [{ type: "text", text }] };
}