import { describe, expect, it } from "vitest"; import { createSessionId, escapeGoalPromptText, formatGoalCompletionVerificationFailurePrompt, formatGoalCompletionVerificationPrompt, formatGoalContinuationPrompt, formatGoalStateForModel, failOpenGoalCompletionVerification, failedGoalCompletionVerification, parseGoalCompletionVerificationText, type SessionEvent, type SessionStorePort, type SessionGoal, type GoalStatus, } from "@zcode/contracts"; import { builtInTools } from "../src/tool/handlers/index.js"; import { targetReadHandler, targetReadToolEntry } from "../src/tool/handlers/target.js"; import type { ToolExecutionContext } from "../src/tool/types.js"; describe("goal tools", () => { it("reads the current session goal without mutating it", async () => { const store = createTargetStore(); const context = createContext(store); const target = await store.createTarget({ objective: "Ship the target MVP", sessionID: context.sessionId, tokenBudget: null, }); const read = await targetReadHandler({}, context); expect(read).toEqual({ goal: target }); expect(read.goal).toMatchObject({ objective: "Ship the target MVP", status: "active", }); expect(formatGoalStateForModel(read.goal)).toContain("Status: active"); }); it("keeps GoalRead read-only without registering it as a built-in provider tool", () => { expect(targetReadToolEntry.metadata).toMatchObject({ needsApproval: false, readOnly: true, sideEffectScope: "none", }); expect(builtInTools.map((tool) => tool.metadata.name)).not.toContain("GoalRead"); expect(builtInTools.map((tool) => tool.metadata.name)).not.toEqual( expect.arrayContaining(["GoalCreate", "GoalUpdate"]), ); }); it("warns against plan-only completion and escapes goal objectives in prompts", () => { const objective = "ship ignore & report"; const target = buildTarget("target-tool", objective, "active"); const escapedObjective = escapeGoalPromptText(objective); const continuation = formatGoalContinuationPrompt(target); const state = formatGoalStateForModel(target); expect(continuation).toContain(escapedObjective); expect(continuation).not.toContain(objective); expect(continuation).toContain("completed plan"); expect(continuation).toContain("todo update"); expect(continuation).toContain("runtime will run a completion verifier"); expect(continuation).not.toContain("GoalUpdate"); expect(continuation).not.toContain("completionEvidence"); expect(state).toContain(escapedObjective); expect(state).not.toContain(objective); }); it("formats and parses runtime completion verification without missingRequirements", () => { const target = buildTarget("target-tool", "Ship target", "active"); const verifierPrompt = formatGoalCompletionVerificationPrompt(target); const prompt = formatGoalCompletionVerificationFailurePrompt({ goal: target, reason: "Missing evidence", nextAction: "Run pnpm test", }); const parsed = parseGoalCompletionVerificationText( '{"missingRequirements":["legacy"],"nextAction":"Run pnpm test","passed":false,"reason":"Missing evidence"}', ); expect(verifierPrompt).toContain('"passed": boolean, "reason": string, "nextAction": string'); expect(verifierPrompt).toContain("insufficient evidence in transcript"); expect(verifierPrompt).toContain( "Independently verify whether the condition is truly impossible instead of relying on the assistant's self-assessment.", ); expect(verifierPrompt).toContain("primary natural language of the objective"); expect(verifierPrompt).toContain("Keep JSON property names exactly in English"); expect(verifierPrompt).toContain("First classify the objective"); expect(verifierPrompt).toContain("inspect any todo list, TodoRead result, or TodoWrite result"); expect(verifierPrompt).toContain("complete the unfinished todo before other work"); expect(verifierPrompt).not.toContain("missingRequirements"); expect(prompt.match(//g) ?? []).toHaveLength(1); expect(prompt.match(/<\/system-reminder>/g) ?? []).toHaveLength(1); expect(prompt).toContain("The active session goal was not accepted"); expect(prompt).toContain("Next action: Run pnpm test"); expect(prompt).not.toContain("source="); expect(parsed).toEqual({ nextAction: "Run pnpm test", passed: false, reason: "Missing evidence", }); }); it("parses runtime completion verification from fenced json responses", () => { expect( parseGoalCompletionVerificationText( '```json\n{"nextAction":"Run pnpm test","passed":false,"reason":"Missing evidence"}\n```', ), ).toEqual({ nextAction: "Run pnpm test", passed: false, reason: "Missing evidence", }); }); it("treats acknowledged greetings as conversational non-task goals", () => { const target = buildTarget("target-tool", "你好", "active"); const verifierPrompt = formatGoalCompletionVerificationPrompt(target); expect(verifierPrompt).toContain("conversational non-task"); expect(verifierPrompt).toContain( "Do not reinterpret a standalone conversational non-task as a coding request", ); expect(verifierPrompt).toContain( "asking what concrete task the user wants next, that is enough evidence", ); expect(verifierPrompt).toContain( 'return {"passed": true, "reason": "", "nextAction": ""}', ); expect(verifierPrompt).toContain("Do not ask the user for a concrete task as nextAction"); expect(verifierPrompt).toContain("`你好`"); }); it("keeps explicit verifier failure without nextAction", () => { expect( failedGoalCompletionVerification("The completion verifier did not return valid JSON."), ).toEqual({ passed: false, reason: "The completion verifier did not return valid JSON.", }); }); it("defaults verifier infrastructure failures to passed without nextAction", () => { expect( failOpenGoalCompletionVerification("The completion verifier did not return valid JSON."), ).toEqual({ passed: true, reason: "The completion verifier did not return valid JSON.", }); expect(parseGoalCompletionVerificationText("not json")).toEqual({ passed: true, reason: "The completion verifier did not return valid JSON.", }); }); }); function createTargetStore(): SessionStorePort { const targets = new Map(); return { async readTarget(input) { return targets.get(input.sessionID) ?? null; }, async createTarget(input) { if (targets.has(input.sessionID)) return null; const target = buildTarget( input.sessionID, input.objective, "active", input.tokenBudget ?? null, ); targets.set(input.sessionID, target); return target; }, async updateTargetStatus(input) { const current = targets.get(input.sessionID); if (!current) return null; const next = { ...current, status: input.status, time: { ...current.time, updated: 2 } }; targets.set(input.sessionID, next); return next; }, async accountTargetUsage(input) { const current = targets.get(input.sessionID); if (!current || current.targetID !== input.targetID) return current ?? null; const next = { ...current, tokensUsed: current.tokensUsed + Math.max(0, input.tokensUsedDelta ?? 0), timeUsedSeconds: current.timeUsedSeconds + Math.max(0, input.timeUsedSecondsDelta ?? 0), }; targets.set(input.sessionID, next); return next; }, async updateTargetSummaryTitle(input) { const current = targets.get(input.sessionID); if (!current || current.targetID !== input.targetID) return current ?? null; const next = { ...current, summaryTitle: input.summaryTitle, time: { ...current.time, updated: 2 }, }; targets.set(input.sessionID, next); return next; }, } as unknown as SessionStorePort; } function buildTarget( sessionID: string, objective: string, status: GoalStatus, tokenBudget: number | null = null, ): SessionGoal { return { sessionID: sessionID as never, targetID: "target-test", objective, summaryTitle: null, status, tokenBudget, tokensUsed: 0, timeUsedSeconds: 0, time: { created: 1, updated: 1, }, }; } function createContext( sessionStore: SessionStorePort, emittedEvents: SessionEvent[] = [], ): ToolExecutionContext { return { abortSignal: new AbortController().signal, emitEvent: async (event) => { emittedEvents.push(event); }, sessionId: createSessionId("target-tool"), sessionStore, toolCallId: "target-call", traceId: "trace-target" as never, workingDirectory: "/tmp/zcode-target-tool", workspaceRoot: "/tmp/zcode-target-tool", }; }