Files
oh-my-pi/packages/coding-agent/test/agent-session-eager-todo.test.ts
T
can1357 1b9d9d0851 refactor(catalog)!: split model catalog from pi-ai
Move bundled models, model cache/manager, thinking metadata, effort helpers,
provider descriptors/discovery, wire constants, and model identity utilities
into the new @oh-my-pi/pi-catalog package.

Update pi-ai to keep provider runtime/auth concerns, move catalog provider
metadata into CATALOG_PROVIDERS, and migrate coding-agent, agent, stats, docs,
and tests to import catalog values from pi-catalog.

Split coding-agent model registry helpers into discovery, roles, and models
config modules while preserving registry orchestration.

BREAKING CHANGE: @oh-my-pi/pi-ai no longer exports catalog subpaths such as
/models, /model-cache, /model-manager, /model-thinking, /effort,
/provider-models*, discovery helpers, and provider wire constants; use the
matching @oh-my-pi/pi-catalog subpaths instead.
2026-06-10 04:06:57 +02:00

285 lines
9.5 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it } from "bun:test";
import * as path from "node:path";
import { Agent, type AgentMessage, type AgentTool } from "@oh-my-pi/pi-agent-core";
import type { AssistantMessage, TextContent, ToolCall } from "@oh-my-pi/pi-ai";
import { AssistantMessageEventStream } from "@oh-my-pi/pi-ai/utils/event-stream";
import { getBundledModel } from "@oh-my-pi/pi-catalog/models";
import { ModelRegistry } from "@oh-my-pi/pi-coding-agent/config/model-registry";
import { Settings } from "@oh-my-pi/pi-coding-agent/config/settings";
import { AgentSession } from "@oh-my-pi/pi-coding-agent/session/agent-session";
import { AuthStorage } from "@oh-my-pi/pi-coding-agent/session/auth-storage";
import { convertToLlm } from "@oh-my-pi/pi-coding-agent/session/messages";
import { SessionManager } from "@oh-my-pi/pi-coding-agent/session/session-manager";
import type { ToolSession } from "@oh-my-pi/pi-coding-agent/tools";
import { TodoTool } from "@oh-my-pi/pi-coding-agent/tools";
import { TempDir } from "@oh-my-pi/pi-utils";
import * as z from "zod/v4";
import { createAssistantMessage } from "./helpers/agent-session-setup";
type ObservedPromptCall = {
toolChoice: string | undefined;
toolNames: string[];
messageRoles: AgentMessage["role"][];
messageTexts: string[];
lastMessageRole: AgentMessage["role"];
lastMessageText: string;
};
function isTextContentBlock(value: unknown): value is TextContent {
if (!value || typeof value !== "object") return false;
return (value as TextContent).type === "text" && typeof (value as TextContent).text === "string";
}
function getToolChoiceName(choice: unknown): string | undefined {
if (!choice) return undefined;
if (typeof choice === "string") return choice;
if (typeof choice !== "object" || !("type" in choice)) return undefined;
const toolChoice = choice as { type?: string; name?: string; function?: { name?: string } };
if (toolChoice.type === "tool") return toolChoice.name;
if (toolChoice.type === "function") return toolChoice.name ?? toolChoice.function?.name;
return undefined;
}
function createToolCallAssistantMessage(name: string, args: Record<string, unknown>): AssistantMessage {
const toolCall: ToolCall = {
type: "toolCall",
id: `call_${name}`,
name,
arguments: args,
};
return {
role: "assistant",
content: [toolCall],
api: "anthropic-messages",
provider: "anthropic",
model: "mock",
usage: {
input: 0,
output: 0,
cacheRead: 0,
cacheWrite: 0,
totalTokens: 0,
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
},
stopReason: "toolUse",
timestamp: Date.now(),
};
}
function getMessageText(message: AgentMessage): string {
if (!("content" in message)) {
return "";
}
if (typeof message.content === "string") {
return message.content;
}
if (!Array.isArray(message.content)) {
return "";
}
return message.content
.filter(isTextContentBlock)
.map(content => content.text)
.join("\n");
}
describe("AgentSession eager todo enforcement", () => {
let tempDir: TempDir;
let session: AgentSession;
let streamCallCount = 0;
let scriptedResponses: AssistantMessage[] = [];
let authStorage: AuthStorage | undefined;
const observedCalls: ObservedPromptCall[] = [];
beforeEach(async () => {
tempDir = TempDir.createSync("@pi-agent-session-eager-todo-");
streamCallCount = 0;
scriptedResponses = [];
observedCalls.length = 0;
const model = getBundledModel("anthropic", "claude-sonnet-4-5");
if (!model) throw new Error("Expected claude-sonnet-4-5 model to exist");
authStorage = await AuthStorage.create(path.join(tempDir.path(), "testauth.db"));
authStorage.setRuntimeApiKey("anthropic", "test-key");
const modelRegistry = new ModelRegistry(authStorage, path.join(tempDir.path(), "models.yml"));
const settings = Settings.isolated({
"compaction.enabled": false,
"todo.enabled": true,
"todo.eager": true,
"todo.reminders": false,
});
const sessionManager = SessionManager.inMemory(tempDir.path());
const toolSession: ToolSession = {
cwd: tempDir.path(),
hasUI: false,
getSessionFile: () => sessionManager.getSessionFile() ?? null,
getSessionSpawns: () => "*",
settings,
};
const todoTool = new TodoTool(toolSession);
const mockBashTool: AgentTool = {
name: "bash",
label: "Bash",
description: "Mock bash tool",
parameters: z.object({}),
execute: async () => ({ content: [{ type: "text" as const, text: "ok" }] }),
};
const agent = new Agent({
getApiKey: () => "test-key",
initialState: {
model,
systemPrompt: ["Test"],
tools: [todoTool, mockBashTool],
messages: [],
},
convertToLlm,
getToolChoice: () => session?.nextToolChoice(),
streamFn: (_model, context, options) => {
streamCallCount++;
const lastMessage = context.messages.at(-1);
if (!lastMessage) {
throw new Error("Expected prompt context to include a message");
}
observedCalls.push({
toolChoice: getToolChoiceName(options?.toolChoice),
toolNames: (context.tools ?? []).map(tool => tool.name),
messageRoles: context.messages.map(message => message.role),
messageTexts: context.messages.map(message => getMessageText(message)),
lastMessageRole: lastMessage.role,
lastMessageText: getMessageText(lastMessage),
});
const response = scriptedResponses.shift() ?? createAssistantMessage("done");
const stream = new AssistantMessageEventStream();
queueMicrotask(() => {
stream.push({ type: "start", partial: response });
const reason =
response.stopReason === "toolUse" || response.stopReason === "length" ? response.stopReason : "stop";
stream.push({ type: "done", reason, message: response });
});
return stream;
},
});
const toolRegistry = new Map<string, AgentTool>([
[todoTool.name, todoTool as unknown as AgentTool],
[mockBashTool.name, mockBashTool],
]);
session = new AgentSession({
agent,
sessionManager,
settings,
modelRegistry,
toolRegistry,
});
});
afterEach(async () => {
if (session) {
await session.dispose();
}
authStorage?.close();
authStorage = undefined;
tempDir.removeSync();
});
it("prepends a hidden eager todo reminder without repeating the prompt text", async () => {
await session.prompt("list all work trees");
expect(observedCalls).toHaveLength(1);
expect(observedCalls[0]).toEqual({
toolChoice: "todo",
toolNames: ["todo", "bash"],
messageRoles: ["developer", "user"],
messageTexts: [expect.any(String), "list all work trees"],
lastMessageRole: "user",
lastMessageText: "list all work trees",
});
expect(observedCalls[0]?.messageTexts.filter(text => text.includes("list all work trees"))).toHaveLength(1);
expect(observedCalls[0]?.messageTexts[0]).not.toContain("list all work trees");
expect(session.formatSessionAsText()).not.toContain("<user-request>");
});
it("initializes todos once, then continues within the same user turn", async () => {
scriptedResponses = [
createToolCallAssistantMessage("todo", {
ops: [
{
op: "init",
list: [{ phase: "List worktrees", items: ["List all git worktrees in the current repository"] }],
},
],
}),
createAssistantMessage("real user turn handled"),
];
await session.prompt("list all work trees");
expect(streamCallCount).toBe(2);
expect(observedCalls).toHaveLength(2);
expect(observedCalls[0]).toEqual({
toolChoice: "todo",
toolNames: ["todo", "bash"],
messageRoles: ["developer", "user"],
messageTexts: [expect.any(String), "list all work trees"],
lastMessageRole: "user",
lastMessageText: "list all work trees",
});
expect(observedCalls[1]?.toolChoice).toBeUndefined();
expect(observedCalls[1]?.lastMessageRole).toBe("toolResult");
expect(observedCalls[1]?.messageRoles.slice(-2)).toEqual(["assistant", "toolResult"]);
expect(session.getTodoPhases()).toHaveLength(1);
expect(session.getTodoPhases()[0]?.tasks[0]?.content).toBe("List all git worktrees in the current repository");
});
it("skips eager todo enforcement for prompts ending with a question mark", async () => {
await session.prompt("list all work trees?");
expect(observedCalls).toHaveLength(1);
expect(observedCalls[0]).toEqual({
toolChoice: undefined,
toolNames: ["todo", "bash"],
messageRoles: ["user"],
messageTexts: ["list all work trees?"],
lastMessageRole: "user",
lastMessageText: "list all work trees?",
});
});
it("skips eager todo enforcement for prompts ending with an exclamation mark", async () => {
await session.prompt("list all work trees!");
expect(observedCalls).toHaveLength(1);
expect(observedCalls[0]).toEqual({
toolChoice: undefined,
toolNames: ["todo", "bash"],
messageRoles: ["user"],
messageTexts: ["list all work trees!"],
lastMessageRole: "user",
lastMessageText: "list all work trees!",
});
});
it("skips eager todo enforcement for subsequent user messages", async () => {
// First prompt: eager todo fires
await session.prompt("refactor the parser module");
expect(observedCalls).toHaveLength(1);
expect(observedCalls[0]?.toolChoice).toBe("todo");
// Second prompt: eager todo must NOT fire
observedCalls.length = 0;
await session.prompt("actually skip that, just fix the typo");
expect(observedCalls).toHaveLength(1);
expect(observedCalls[0]).toEqual({
toolChoice: undefined,
toolNames: ["todo", "bash"],
messageRoles: expect.arrayContaining(["user"]),
messageTexts: expect.arrayContaining(["actually skip that, just fix the typo"]),
lastMessageRole: "user",
lastMessageText: "actually skip that, just fix the typo",
});
});
});