e6d33e0a48
- Converted SettingsManager.create(), loadSettings(), discoverSkills(), loadSlashCommands(), buildSystemPrompt(), loadSkills(), loadProjectContextFiles(), and getShellConfig() from synchronous to asynchronous APIs. - Changed capability provider load() method from supporting both sync and async to async-only by removing loadSync(). - Removed fs property from LoadContext interface in capability types. - Added new fs.ts module with caching layer for filesystem operations using readFile, readDirEntries, readDir, walkUp, and cache management functions. - Refactored all discovery providers (builtin, claude, cline, codex, cursor, gemini, github, mcp-json, ssh, vscode, windsurf) to use async file operations with Promise.all for parallel loading.
164 lines
5.1 KiB
TypeScript
164 lines
5.1 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 { existsSync, mkdirSync, rmSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import { Agent } from "@oh-my-pi/pi-agent-core";
|
|
import { getModel } from "@oh-my-pi/pi-ai";
|
|
import { nanoid } from "nanoid";
|
|
import { AgentSession } from "../src/core/agent-session";
|
|
import { AuthStorage } from "../src/core/auth-storage";
|
|
import { ModelRegistry } from "../src/core/model-registry";
|
|
import { SessionManager } from "../src/core/session-manager";
|
|
import { SettingsManager } from "../src/core/settings-manager";
|
|
import { createTools, type ToolSession } from "../src/core/tools/index";
|
|
import { API_KEY } from "./utilities";
|
|
|
|
describe.skipIf(!API_KEY)("AgentSession branching", () => {
|
|
let session: AgentSession;
|
|
let tempDir: string;
|
|
let sessionManager: SessionManager;
|
|
|
|
beforeEach(() => {
|
|
// Create temp directory for session files
|
|
tempDir = join(tmpdir(), `pi-branching-test-${nanoid()}`);
|
|
mkdirSync(tempDir, { recursive: true });
|
|
});
|
|
|
|
afterEach(async () => {
|
|
if (session) {
|
|
session.dispose();
|
|
}
|
|
if (tempDir && existsSync(tempDir)) {
|
|
rmSync(tempDir, { recursive: true });
|
|
}
|
|
});
|
|
|
|
async function createSession(noSession: boolean = false) {
|
|
const toolSession: ToolSession = {
|
|
cwd: tempDir,
|
|
hasUI: false,
|
|
getSessionFile: () => null,
|
|
getSessionSpawns: () => "*",
|
|
};
|
|
const tools = await createTools(toolSession);
|
|
|
|
const model = getModel("anthropic", "claude-sonnet-4-5")!;
|
|
const agent = new Agent({
|
|
getApiKey: () => 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);
|
|
const settingsManager = await SettingsManager.create(tempDir, tempDir);
|
|
const authStorage = new AuthStorage(join(tempDir, "auth.json"));
|
|
const modelRegistry = new ModelRegistry(authStorage, tempDir);
|
|
|
|
session = new AgentSession({
|
|
agent,
|
|
sessionManager,
|
|
settingsManager,
|
|
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(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");
|
|
});
|
|
});
|