1b9d9d0851
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.
168 lines
5.4 KiB
TypeScript
168 lines
5.4 KiB
TypeScript
/**
|
|
* Tests for AgentSession branching behavior.
|
|
*
|
|
* These tests verify:
|
|
* - Branching from a single message works
|
|
* - Branching in --no-session mode (in-memory only)
|
|
* - getUserMessagesForBranching returns correct entries
|
|
*/
|
|
|
|
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
|
|
import * as fs from "node:fs";
|
|
import * as os from "node:os";
|
|
import * as path from "node:path";
|
|
import { Agent } from "@oh-my-pi/pi-agent-core";
|
|
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 { SessionManager } from "@oh-my-pi/pi-coding-agent/session/session-manager";
|
|
import { createTools, type ToolSession } from "@oh-my-pi/pi-coding-agent/tools";
|
|
import { Snowflake } from "@oh-my-pi/pi-utils";
|
|
import { e2eApiKey } from "./utilities";
|
|
|
|
describe.skipIf(!e2eApiKey("ANTHROPIC_API_KEY"))("AgentSession branching", () => {
|
|
let session: AgentSession;
|
|
let tempDir: string;
|
|
let sessionManager: SessionManager;
|
|
let authStorage: AuthStorage | undefined;
|
|
|
|
beforeEach(() => {
|
|
// Create temp directory for session files
|
|
tempDir = path.join(os.tmpdir(), `pi-branching-test-${Snowflake.next()}`);
|
|
fs.mkdirSync(tempDir, { recursive: true });
|
|
});
|
|
|
|
afterEach(async () => {
|
|
if (session) {
|
|
await session.dispose();
|
|
}
|
|
authStorage?.close();
|
|
authStorage = undefined;
|
|
if (tempDir && fs.existsSync(tempDir)) {
|
|
fs.rmSync(tempDir, { recursive: true });
|
|
}
|
|
});
|
|
|
|
async function createSession(noSession: boolean = false) {
|
|
const toolSession: ToolSession = {
|
|
cwd: tempDir,
|
|
hasUI: false,
|
|
getSessionFile: () => null,
|
|
getSessionSpawns: () => "*",
|
|
settings: Settings.isolated(),
|
|
};
|
|
const tools = await createTools(toolSession);
|
|
|
|
const model = getBundledModel("anthropic", "claude-sonnet-4-5")!;
|
|
const agent = new Agent({
|
|
getApiKey: () => e2eApiKey("ANTHROPIC_API_KEY"),
|
|
initialState: {
|
|
model,
|
|
systemPrompt: ["You are a helpful assistant. Be extremely concise, reply with just a few words."],
|
|
tools,
|
|
},
|
|
});
|
|
|
|
sessionManager = noSession ? SessionManager.inMemory() : SessionManager.create(tempDir, tempDir);
|
|
const settings = Settings.isolated();
|
|
authStorage = await AuthStorage.create(path.join(tempDir, "testauth.db"));
|
|
const modelRegistry = new ModelRegistry(authStorage, path.join(tempDir, "models.yml"));
|
|
|
|
session = new AgentSession({
|
|
agent,
|
|
sessionManager,
|
|
settings,
|
|
modelRegistry,
|
|
});
|
|
|
|
// Must subscribe to enable session persistence
|
|
session.subscribe(() => {});
|
|
|
|
return session;
|
|
}
|
|
|
|
it("should allow branching from single message", async () => {
|
|
await createSession();
|
|
|
|
// Send one message
|
|
await session.prompt("Say hello");
|
|
await session.agent.waitForIdle();
|
|
|
|
// Should have exactly 1 user message available for branching
|
|
const userMessages = session.getUserMessagesForBranching();
|
|
expect(userMessages.length).toBe(1);
|
|
expect(userMessages[0].text).toBe("Say hello");
|
|
|
|
// Branch from the first message
|
|
const result = await session.branch(userMessages[0].entryId);
|
|
expect(result.selectedText).toBe("Say hello");
|
|
expect(result.cancelled).toBe(false);
|
|
|
|
// After branching, conversation should be empty (branched before the first message)
|
|
expect(session.messages.length).toBe(0);
|
|
|
|
// Session file should exist (new branch)
|
|
expect(session.sessionFile).not.toBeNull();
|
|
expect(fs.existsSync(session.sessionFile!)).toBe(true);
|
|
});
|
|
|
|
it("should support in-memory branching in --no-session mode", async () => {
|
|
await createSession(true);
|
|
|
|
// Verify sessions are disabled
|
|
expect(session.sessionFile).toBeUndefined();
|
|
|
|
// Send one message
|
|
await session.prompt("Say hi");
|
|
await session.agent.waitForIdle();
|
|
|
|
// Should have 1 user message
|
|
const userMessages = session.getUserMessagesForBranching();
|
|
expect(userMessages.length).toBe(1);
|
|
|
|
// Verify we have messages before branching
|
|
expect(session.messages.length).toBeGreaterThan(0);
|
|
|
|
// Branch from the first message
|
|
const result = await session.branch(userMessages[0].entryId);
|
|
expect(result.selectedText).toBe("Say hi");
|
|
expect(result.cancelled).toBe(false);
|
|
|
|
// After branching, conversation should be empty
|
|
expect(session.messages.length).toBe(0);
|
|
|
|
// Session file should still be undefined (no file created)
|
|
expect(session.sessionFile).toBeUndefined();
|
|
});
|
|
|
|
it("should branch from middle of conversation", async () => {
|
|
await createSession();
|
|
|
|
// Send multiple messages
|
|
await session.prompt("Say one");
|
|
await session.agent.waitForIdle();
|
|
|
|
await session.prompt("Say two");
|
|
await session.agent.waitForIdle();
|
|
|
|
await session.prompt("Say three");
|
|
await session.agent.waitForIdle();
|
|
|
|
// Should have 3 user messages
|
|
const userMessages = session.getUserMessagesForBranching();
|
|
expect(userMessages.length).toBe(3);
|
|
|
|
// Branch from second message (keeps first message + response)
|
|
const secondMessage = userMessages[1];
|
|
const result = await session.branch(secondMessage.entryId);
|
|
expect(result.selectedText).toBe("Say two");
|
|
|
|
// After branching, should have first user message + assistant response
|
|
expect(session.messages.length).toBe(2);
|
|
expect(session.messages[0].role).toBe("user");
|
|
expect(session.messages[1].role).toBe("assistant");
|
|
});
|
|
});
|