Fix tests for sessionManager/modelRegistry on context

- compaction-hooks-example.test.ts: get sessionManager/modelRegistry from ctx
- compaction-hooks.test.ts:
  - Pass sessionManager/modelRegistry to HookRunner constructor
  - Remove setSessionFile call
  - Update tests to use session.sessionManager instead of event.sessionManager
This commit is contained in:
Mario Zechner 2025-12-27 02:46:52 +01:00
parent 29fec7848e
commit 5fee9005b7
4 changed files with 18 additions and 11 deletions

View file

@ -298,7 +298,8 @@ export async function loadCustomTools(
// Shared API object - all tools get the same instance // Shared API object - all tools get the same instance
const sharedApi: ToolAPI = { const sharedApi: ToolAPI = {
cwd, cwd,
exec: (command: string, args: string[], options?: ExecOptions) => execCommand(command, args, cwd, options), exec: (command: string, args: string[], options?: ExecOptions) =>
execCommand(command, args, options?.cwd ?? cwd, options),
ui: createNoOpUIContext(), ui: createNoOpUIContext(),
hasUI: false, hasUI: false,
}; };

View file

@ -31,6 +31,8 @@ export interface ExecOptions {
signal?: AbortSignal; signal?: AbortSignal;
/** Timeout in milliseconds */ /** Timeout in milliseconds */
timeout?: number; timeout?: number;
/** Working directory */
cwd?: string;
} }
/** API passed to custom tool factory (stable across session changes) */ /** API passed to custom tool factory (stable across session changes) */

View file

@ -9,11 +9,13 @@ describe("Documentation example", () => {
it("custom compaction example should type-check correctly", () => { it("custom compaction example should type-check correctly", () => {
// This is the example from hooks.md - verify it compiles // This is the example from hooks.md - verify it compiles
const exampleHook = (pi: HookAPI) => { const exampleHook = (pi: HookAPI) => {
pi.on("session", async (event, _ctx) => { pi.on("session", async (event, ctx) => {
if (event.reason !== "before_compact") return; if (event.reason !== "before_compact") return;
// After narrowing, these should all be accessible // After narrowing, these should all be accessible
const { preparation, previousCompactions, sessionManager, modelRegistry, model } = event; // sessionManager and modelRegistry come from ctx, not event
const { preparation, previousCompactions, model } = event;
const { sessionManager, modelRegistry } = ctx;
const { messagesToSummarize, messagesToKeep, tokensBefore, firstKeptEntryId, cutPoint } = preparation; const { messagesToSummarize, messagesToKeep, tokensBefore, firstKeptEntryId, cutPoint } = preparation;
// Get previous summary from most recent compaction // Get previous summary from most recent compaction

View file

@ -91,7 +91,7 @@ describe.skipIf(!API_KEY)("Compaction hooks", () => {
const authStorage = new AuthStorage(join(tempDir, "auth.json")); const authStorage = new AuthStorage(join(tempDir, "auth.json"));
const modelRegistry = new ModelRegistry(authStorage); const modelRegistry = new ModelRegistry(authStorage);
hookRunner = new HookRunner(hooks, tempDir); hookRunner = new HookRunner(hooks, tempDir, sessionManager, modelRegistry);
hookRunner.setUIContext( hookRunner.setUIContext(
{ {
select: async () => null, select: async () => null,
@ -101,7 +101,6 @@ describe.skipIf(!API_KEY)("Compaction hooks", () => {
}, },
false, false,
); );
hookRunner.setSessionFile(sessionManager.getSessionFile() ?? null);
session = new AgentSession({ session = new AgentSession({
agent, agent,
@ -140,8 +139,7 @@ describe.skipIf(!API_KEY)("Compaction hooks", () => {
expect(beforeEvent.preparation.messagesToKeep).toBeDefined(); expect(beforeEvent.preparation.messagesToKeep).toBeDefined();
expect(beforeEvent.preparation.tokensBefore).toBeGreaterThanOrEqual(0); expect(beforeEvent.preparation.tokensBefore).toBeGreaterThanOrEqual(0);
expect(beforeEvent.model).toBeDefined(); expect(beforeEvent.model).toBeDefined();
expect(beforeEvent.sessionManager).toBeDefined(); // sessionManager and modelRegistry are now on ctx, not event
expect(beforeEvent.modelRegistry).toBeDefined();
} }
const afterEvent = compactEvents[0]; const afterEvent = compactEvents[0];
@ -217,8 +215,9 @@ describe.skipIf(!API_KEY)("Compaction hooks", () => {
const afterEvent = compactEvents[0]; const afterEvent = compactEvents[0];
if (afterEvent.reason === "compact") { if (afterEvent.reason === "compact") {
const entries = afterEvent.sessionManager.getEntries(); // sessionManager is now on ctx, use session.sessionManager directly
const hasCompactionEntry = entries.some((e) => e.type === "compaction"); const entries = session.sessionManager.getEntries();
const hasCompactionEntry = entries.some((e: { type: string }) => e.type === "compaction");
expect(hasCompactionEntry).toBe(true); expect(hasCompactionEntry).toBe(true);
} }
}, 120000); }, 120000);
@ -361,9 +360,12 @@ describe.skipIf(!API_KEY)("Compaction hooks", () => {
expect(event.model).toHaveProperty("provider"); expect(event.model).toHaveProperty("provider");
expect(event.model).toHaveProperty("id"); expect(event.model).toHaveProperty("id");
expect(typeof event.modelRegistry.getApiKey).toBe("function"); // sessionManager and modelRegistry are now on ctx, not event
// Verify they're accessible via session
expect(typeof session.sessionManager.getEntries).toBe("function");
expect(typeof session.modelRegistry.getApiKey).toBe("function");
const entries = event.sessionManager.getEntries(); const entries = session.sessionManager.getEntries();
expect(Array.isArray(entries)).toBe(true); expect(Array.isArray(entries)).toBe(true);
expect(entries.length).toBeGreaterThan(0); expect(entries.length).toBeGreaterThan(0);
}, 120000); }, 120000);