e1897ce013
- Extracted thinking module with ThinkingEffort, ThinkingLevel, and ThinkingMode types to centralize reasoning configuration across packages. - Migrated ThinkingLevel type from pi-agent-core to pi-ai package with new validation functions parseThinkingLevel() and getAvailableThinkingLevel(). - Consolidated thinking level constants and descriptions into reusable exports (ALL_THINKING_LEVELS, THINKING_MODE_DESCRIPTIONS) for consistent UI display. - Removed local thinking-effort-label utility and replaced formatThinkingEffortLabel() with centralized formatThinking() function from pi-ai. - Refactored thinking mode handling to distinguish ThinkingSelector (user-facing with 'off' option) from ThinkingEffort (provider-level).
225 lines
6.8 KiB
TypeScript
225 lines
6.8 KiB
TypeScript
/**
|
|
* Test for compaction with thinking models.
|
|
*
|
|
* Tests both:
|
|
* - Claude via Antigravity (google-gemini-cli API)
|
|
* - Claude via real Anthropic API (anthropic-messages API)
|
|
*
|
|
* Reproduces issue where compact fails when maxTokens < thinkingBudget.
|
|
*/
|
|
|
|
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, type Model, type ThinkingLevel } from "@oh-my-pi/pi-ai";
|
|
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";
|
|
|
|
// Check for auth
|
|
const HAS_ANTIGRAVITY_AUTH = false; // OAuth not available in test environment
|
|
const HAS_ANTHROPIC_AUTH = !!e2eApiKey("ANTHROPIC_API_KEY");
|
|
|
|
describe.skipIf(!HAS_ANTIGRAVITY_AUTH)("Compaction with thinking models (Antigravity)", () => {
|
|
let session: AgentSession;
|
|
let tempDir: string;
|
|
|
|
beforeEach(() => {
|
|
tempDir = path.join(os.tmpdir(), `pi-thinking-compaction-test-${Snowflake.next()}`);
|
|
fs.mkdirSync(tempDir, { recursive: true });
|
|
});
|
|
|
|
afterEach(async () => {
|
|
if (session) {
|
|
session.dispose();
|
|
}
|
|
if (tempDir && fs.existsSync(tempDir)) {
|
|
fs.rmSync(tempDir, { recursive: true });
|
|
}
|
|
});
|
|
|
|
async function createSession(
|
|
modelId: "claude-opus-4-5-thinking" | "claude-sonnet-4-5",
|
|
thinkingLevel: ThinkingLevel = "high",
|
|
) {
|
|
const toolSession: ToolSession = {
|
|
cwd: tempDir,
|
|
hasUI: false,
|
|
getSessionFile: () => null,
|
|
getSessionSpawns: () => "*",
|
|
settings: Settings.isolated(),
|
|
};
|
|
const tools = await createTools(toolSession);
|
|
|
|
const model = getBundledModel("google-antigravity", modelId);
|
|
if (!model) {
|
|
throw new Error(`Model not found: google-antigravity/${modelId}`);
|
|
}
|
|
|
|
const agent = new Agent({
|
|
getApiKey: () => e2eApiKey("ANTHROPIC_API_KEY"),
|
|
initialState: {
|
|
model,
|
|
systemPrompt: "You are a helpful assistant. Be concise.",
|
|
tools,
|
|
thinkingLevel,
|
|
},
|
|
});
|
|
|
|
const sessionManager = SessionManager.inMemory();
|
|
const settings = Settings.isolated();
|
|
|
|
const authStorage = await AuthStorage.create(path.join(tempDir, "testauth.db"));
|
|
const modelRegistry = new ModelRegistry(authStorage);
|
|
|
|
session = new AgentSession({
|
|
agent,
|
|
sessionManager,
|
|
settings,
|
|
modelRegistry,
|
|
});
|
|
|
|
session.subscribe(() => {});
|
|
|
|
return session;
|
|
}
|
|
|
|
it("should compact successfully with claude-opus-4-5-thinking and thinking level high", async () => {
|
|
createSession("claude-opus-4-5-thinking", "high");
|
|
|
|
// Send a simple prompt
|
|
await session.prompt("Write down the first 10 prime numbers.");
|
|
await session.agent.waitForIdle();
|
|
|
|
// Verify we got a response
|
|
const messages = session.messages;
|
|
expect(messages.length).toBeGreaterThan(0);
|
|
|
|
const assistantMessages = messages.filter(m => m.role === "assistant");
|
|
expect(assistantMessages.length).toBeGreaterThan(0);
|
|
|
|
// Now try to compact - this should not throw
|
|
const result = await session.compact();
|
|
|
|
expect(result.summary).toBeDefined();
|
|
expect(result.summary.length).toBeGreaterThan(0);
|
|
expect(result.tokensBefore).toBeGreaterThan(0);
|
|
|
|
// Verify session is still usable after compaction
|
|
const messagesAfterCompact = session.messages;
|
|
expect(messagesAfterCompact.length).toBeGreaterThan(0);
|
|
expect(messagesAfterCompact[0].role).toBe("compactionSummary");
|
|
}, 180000);
|
|
|
|
it("should compact successfully with claude-sonnet-4-5 (non-thinking) for comparison", async () => {
|
|
createSession("claude-sonnet-4-5", "off");
|
|
|
|
await session.prompt("Write down the first 10 prime numbers.");
|
|
await session.agent.waitForIdle();
|
|
|
|
const messages = session.messages;
|
|
expect(messages.length).toBeGreaterThan(0);
|
|
|
|
const result = await session.compact();
|
|
|
|
expect(result.summary).toBeDefined();
|
|
expect(result.summary.length).toBeGreaterThan(0);
|
|
}, 180000);
|
|
});
|
|
|
|
// ============================================================================
|
|
// Real Anthropic API tests (for comparison)
|
|
// ============================================================================
|
|
|
|
describe.skipIf(!HAS_ANTHROPIC_AUTH)("Compaction with thinking models (Anthropic)", () => {
|
|
let session: AgentSession;
|
|
let tempDir: string;
|
|
|
|
beforeEach(() => {
|
|
tempDir = path.join(os.tmpdir(), `pi-thinking-compaction-anthropic-test-${Snowflake.next()}`);
|
|
fs.mkdirSync(tempDir, { recursive: true });
|
|
});
|
|
|
|
afterEach(async () => {
|
|
if (session) {
|
|
session.dispose();
|
|
}
|
|
if (tempDir && fs.existsSync(tempDir)) {
|
|
fs.rmSync(tempDir, { recursive: true });
|
|
}
|
|
});
|
|
|
|
async function createSession(model: Model, thinkingLevel: ThinkingLevel = "high") {
|
|
const toolSession: ToolSession = {
|
|
cwd: tempDir,
|
|
hasUI: false,
|
|
getSessionFile: () => null,
|
|
getSessionSpawns: () => "*",
|
|
settings: Settings.isolated(),
|
|
};
|
|
const tools = await createTools(toolSession);
|
|
|
|
const agent = new Agent({
|
|
getApiKey: () => e2eApiKey("ANTHROPIC_API_KEY"),
|
|
initialState: {
|
|
model,
|
|
systemPrompt: "You are a helpful assistant. Be concise.",
|
|
tools,
|
|
thinkingLevel,
|
|
},
|
|
});
|
|
|
|
const sessionManager = SessionManager.inMemory();
|
|
const settings = Settings.isolated();
|
|
|
|
const authStorage = await AuthStorage.create(path.join(tempDir, "testauth.db"));
|
|
const modelRegistry = new ModelRegistry(authStorage);
|
|
|
|
session = new AgentSession({
|
|
agent,
|
|
sessionManager,
|
|
settings,
|
|
modelRegistry,
|
|
});
|
|
|
|
session.subscribe(() => {});
|
|
|
|
return session;
|
|
}
|
|
|
|
it("should compact successfully with claude-3-7-sonnet and thinking level high", async () => {
|
|
const model = getBundledModel("anthropic", "claude-3-7-sonnet-latest")!;
|
|
createSession(model, "high");
|
|
|
|
// Send a simple prompt
|
|
await session.prompt("Write down the first 10 prime numbers.");
|
|
await session.agent.waitForIdle();
|
|
|
|
// Verify we got a response
|
|
const messages = session.messages;
|
|
expect(messages.length).toBeGreaterThan(0);
|
|
|
|
const assistantMessages = messages.filter(m => m.role === "assistant");
|
|
expect(assistantMessages.length).toBeGreaterThan(0);
|
|
|
|
// Now try to compact - this should not throw
|
|
const result = await session.compact();
|
|
|
|
expect(result.summary).toBeDefined();
|
|
expect(result.summary.length).toBeGreaterThan(0);
|
|
expect(result.tokensBefore).toBeGreaterThan(0);
|
|
|
|
// Verify session is still usable after compaction
|
|
const messagesAfterCompact = session.messages;
|
|
expect(messagesAfterCompact.length).toBeGreaterThan(0);
|
|
expect(messagesAfterCompact[0].role).toBe("compactionSummary");
|
|
}, 180000);
|
|
});
|