import { describe, expect, it } from "vitest"; import { isWorkflowTerminalBatch } from "./chatTools"; import type { NormalizedToolCall, NormalizedToolResult } from "./llm"; function makeCall(name: string, id?: string): NormalizedToolCall { return { id: id ?? `${name}-1`, name, input: {} }; } function makeResult(callId: string, content: Record): NormalizedToolResult { return { tool_use_id: callId, content: JSON.stringify(content) }; } function makeRow(overrides: Partial<{ apply_status: string; finding_status: string }>) { return { row_id: "doc-0:test", doc_id: "doc-0", comment_key: "test", block_id: "P_0001", finding_status: overrides.finding_status ?? "REVIEW", apply_status: overrides.apply_status ?? "applied", action: "COMMENT" as const, issue: "test issue", source: "plan_comments_on_document" as const, }; } describe("isWorkflowTerminalBatch", () => { it("read-only tool batch is non-terminal", () => { const decision = isWorkflowTerminalBatch({ calls: [makeCall("read_document")], toolResults: [makeResult("read_document-1", { ok: true })], workflowRows: [], }); expect(decision.terminal).toBe(false); }); it("plan_comments_on_document with needs_correction > 0 and ready_to_apply: false is non-terminal", () => { const decision = isWorkflowTerminalBatch({ calls: [makeCall("plan_comments_on_document")], toolResults: [makeResult("plan_comments_on_document-1", { ok: true, needs_correction: 2, ready_to_apply: false, })], workflowRows: [], }); expect(decision.terminal).toBe(false); }); it("partial valid + correctable rows is non-terminal", () => { const decision = isWorkflowTerminalBatch({ calls: [makeCall("plan_comments_on_document")], toolResults: [makeResult("plan_comments_on_document-1", { ok: true, valid: 3, needs_correction: 2, ready_to_apply: false, })], workflowRows: [ makeRow({ apply_status: "applied" }), makeRow({ apply_status: "needs_correction" }), ], }); expect(decision.terminal).toBe(false); }); it("partial valid + permanent failures with no correctable rows is terminal with reason applied", () => { const decision = isWorkflowTerminalBatch({ calls: [makeCall("plan_comments_on_document")], toolResults: [makeResult("plan_comments_on_document-1", { ok: true, valid: 3, needs_correction: 0, failed_permanently: 1, ready_to_apply: true, applied: true, })], workflowRows: [ makeRow({ apply_status: "applied" }), makeRow({ apply_status: "failed_validation" }), ], }); expect(decision.terminal).toBe(true); if (decision.terminal) expect(decision.reason).toBe("applied"); }); it("permanent failures only is terminal with reason failed", () => { const decision = isWorkflowTerminalBatch({ calls: [makeCall("plan_comments_on_document")], toolResults: [makeResult("plan_comments_on_document-1", { ok: false, valid: 0, needs_correction: 0, failed_permanently: 3, ready_to_apply: true, })], workflowRows: [ makeRow({ apply_status: "failed_validation" }), makeRow({ apply_status: "not_applied" }), ], }); expect(decision.terminal).toBe(true); if (decision.terminal) expect(decision.reason).toBe("failed"); }); it("ignored PASS only is terminal with reason no_action", () => { const decision = isWorkflowTerminalBatch({ calls: [makeCall("plan_comments_on_document")], toolResults: [makeResult("plan_comments_on_document-1", { ok: true, valid: 0, needs_correction: 0, failed_permanently: 0, ignored_pass: 2, ready_to_apply: true, no_action: true, })], workflowRows: [ makeRow({ apply_status: "ignored_pass" }), ], }); expect(decision.terminal).toBe(true); if (decision.terminal) expect(decision.reason).toBe("no_action"); }); it("correct_comment_anchors is always terminal after one cycle", () => { const decision = isWorkflowTerminalBatch({ calls: [makeCall("correct_comment_anchors")], toolResults: [makeResult("correct_comment_anchors-1", { ok: true, applied: 2, failed: 1, ready_to_apply: true, })], workflowRows: [ makeRow({ apply_status: "applied" }), makeRow({ apply_status: "failed_validation" }), ], }); expect(decision.terminal).toBe(true); }); it("correct_comment_anchors with no applied rows is still terminal", () => { const decision = isWorkflowTerminalBatch({ calls: [makeCall("correct_comment_anchors")], toolResults: [makeResult("correct_comment_anchors-1", { ok: false, applied: 0, failed: 2, })], workflowRows: [ makeRow({ apply_status: "failed_validation" }), ], }); expect(decision.terminal).toBe(true); if (decision.terminal) expect(decision.reason).toBe("failed"); }); });