|
| 1 | +/** |
| 2 | + * Regression tests for the whole extension: a ctx.compact() that throws |
| 3 | + * SYNCHRONOUSLY must not strand the `compacting` flag (which permanently |
| 4 | + * disables auto-compaction) nor, on the input-preflight path, leave the promise |
| 5 | + * unresolved so the input hook never returns {action:"continue"}. |
| 6 | + * |
| 7 | + * The extension only leaves its dormant state when pi's own compaction is off, |
| 8 | + * so each test points ctx.cwd at a temp project whose .pi/settings.json disables |
| 9 | + * pi's built-in compaction (roots are injected, never via HOME — os.homedir may |
| 10 | + * be cached under bun). Everything else is a hand-rolled fake ctx/pi. |
| 11 | + */ |
| 12 | +import { test } from "node:test"; |
| 13 | +import assert from "node:assert/strict"; |
| 14 | +import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from "node:fs"; |
| 15 | +import { tmpdir } from "node:os"; |
| 16 | +import { join } from "node:path"; |
| 17 | +import compact from "../extensions/compact.ts"; |
| 18 | +import type { ExtensionAPI } from "@earendil-works/pi-coding-agent"; |
| 19 | + |
| 20 | +type Handler = (event: unknown, ctx: unknown) => unknown; |
| 21 | + |
| 22 | +/** Instantiate the extension against a fake pi, returning its captured handlers. */ |
| 23 | +function loadExtension(): Map<string, Handler> { |
| 24 | + const handlers = new Map<string, Handler>(); |
| 25 | + const pi = { |
| 26 | + on(evt: string, fn: Handler) { |
| 27 | + handlers.set(evt, fn); |
| 28 | + }, |
| 29 | + registerCommand() {}, |
| 30 | + } as unknown as ExtensionAPI; |
| 31 | + compact(pi); |
| 32 | + return handlers; |
| 33 | +} |
| 34 | + |
| 35 | +/** A temp project dir with pi's built-in compaction OFF (so the ext is active). */ |
| 36 | +function makeActiveProjectDir(): string { |
| 37 | + const dir = mkdtempSync(join(tmpdir(), "pify-compact-")); |
| 38 | + mkdirSync(join(dir, ".pi"), { recursive: true }); |
| 39 | + writeFileSync(join(dir, ".pi", "settings.json"), JSON.stringify({ compaction: { enabled: false } })); |
| 40 | + return dir; |
| 41 | +} |
| 42 | + |
| 43 | +function cleanup(dir: string): void { |
| 44 | + try { |
| 45 | + rmSync(dir, { recursive: true, force: true }); |
| 46 | + } catch { |
| 47 | + // best-effort; a lingering lockfile on Windows must not fail the test |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +interface FakeCtx { |
| 52 | + cwd: string; |
| 53 | + hasUI: boolean; |
| 54 | + isProjectTrusted: () => boolean; |
| 55 | + sessionManager: { getBranch: () => unknown[] }; |
| 56 | + ui: { notify: (msg: string, level: string) => void; setStatus: () => void }; |
| 57 | + getContextUsage: () => { percent: number; tokens: number; contextWindow: number }; |
| 58 | + compact: () => void; |
| 59 | +} |
| 60 | + |
| 61 | +/** ctx whose compact() always throws synchronously, counting each attempt. */ |
| 62 | +function makeThrowingCtx(dir: string, usage: { percent: number; tokens: number; contextWindow: number }) { |
| 63 | + const state = { compactAttempts: 0, warnings: [] as string[] }; |
| 64 | + const ctx: FakeCtx = { |
| 65 | + cwd: dir, |
| 66 | + hasUI: true, |
| 67 | + isProjectTrusted: () => true, |
| 68 | + sessionManager: { getBranch: () => [] }, |
| 69 | + ui: { |
| 70 | + notify: (msg, level) => { |
| 71 | + if (level === "warning") state.warnings.push(msg); |
| 72 | + }, |
| 73 | + setStatus: () => {}, |
| 74 | + }, |
| 75 | + getContextUsage: () => usage, |
| 76 | + compact: () => { |
| 77 | + state.compactAttempts++; |
| 78 | + throw new Error("synthetic synchronous compact failure"); |
| 79 | + }, |
| 80 | + }; |
| 81 | + return { ctx, state }; |
| 82 | +} |
| 83 | + |
| 84 | +test("agent_settled: a synchronous compact throw resets the flag, so the next settle compacts again", async () => { |
| 85 | + const dir = makeActiveProjectDir(); |
| 86 | + try { |
| 87 | + const handlers = loadExtension(); |
| 88 | + const { ctx, state } = makeThrowingCtx(dir, { percent: 95, tokens: 500_000, contextWindow: 1_000_000 }); |
| 89 | + |
| 90 | + await handlers.get("session_start")!({}, ctx); // determines active = true |
| 91 | + |
| 92 | + // First settle: over threshold → compact → ctx.compact() throws synchronously. |
| 93 | + await handlers.get("agent_settled")!({}, ctx); |
| 94 | + // Second settle: if the throw had stranded `compacting` at true, shouldCompact |
| 95 | + // would refuse and this would be a no-op. It must compact again. |
| 96 | + await handlers.get("agent_settled")!({}, ctx); |
| 97 | + |
| 98 | + assert.equal(state.compactAttempts, 2, "compaction is attempted on both settles (flag was reset)"); |
| 99 | + assert.equal(state.warnings.length, 2, "each synchronous failure is surfaced as a warning"); |
| 100 | + } finally { |
| 101 | + cleanup(dir); |
| 102 | + } |
| 103 | +}); |
| 104 | + |
| 105 | +test("input preflight: a synchronous compact throw still resolves and returns continue", async () => { |
| 106 | + const dir = makeActiveProjectDir(); |
| 107 | + try { |
| 108 | + const handlers = loadExtension(); |
| 109 | + const { ctx, state } = makeThrowingCtx(dir, { percent: 95, tokens: 850_000, contextWindow: 1_000_000 }); |
| 110 | + |
| 111 | + await handlers.get("session_start")!({}, ctx); |
| 112 | + |
| 113 | + // A big-enough prompt while idle trips the preflight → compactAndWait → throw. |
| 114 | + const result = await handlers.get("input")!({ text: "hello", streamingBehavior: undefined }, ctx); |
| 115 | + assert.deepEqual(result, { action: "continue" }, "input hook returns despite the synchronous throw"); |
| 116 | + assert.equal(state.compactAttempts, 1, "preflight attempted the compaction"); |
| 117 | + |
| 118 | + // Flag must be cleared: a following settle over threshold compacts again. |
| 119 | + await handlers.get("agent_settled")!({}, ctx); |
| 120 | + assert.equal(state.compactAttempts, 2, "compacting flag was reset by the preflight failure"); |
| 121 | + } finally { |
| 122 | + cleanup(dir); |
| 123 | + } |
| 124 | +}); |
0 commit comments