|
| 1 | +import test from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | +import { registerHandoffCommand } from "./handoff/command.js"; |
| 4 | +import { registerHandoffTool } from "./handoff/tool.js"; |
| 5 | +import { registerHandoffCompaction } from "./handoff/compact.js"; |
| 6 | +import { registerWatchdog } from "./watchdog.js"; |
| 7 | +import { createState } from "./state.js"; |
| 8 | +import { buildChildToolNames, createChildTools } from "./spawn/index.js"; |
| 9 | +import { registerLedgerRehydration } from "./ledger/rehydration.js"; |
| 10 | +import registerAgenticoding from "./index.js"; |
| 11 | + |
| 12 | +type Handler = (args: any, ctx: any) => any; |
| 13 | + |
| 14 | +class MockPi { |
| 15 | + commands = new Map<string, { description?: string; handler: Handler }>(); |
| 16 | + tools = new Map<string, any>(); |
| 17 | + handlers = new Map<string, Handler[]>(); |
| 18 | + activeTools: string[] = []; |
| 19 | + sentUserMessages: Array<{ content: string; options: any }> = []; |
| 20 | + |
| 21 | + registerCommand(name: string, definition: { description?: string; handler: Handler }) { |
| 22 | + this.commands.set(name, definition); |
| 23 | + } |
| 24 | + |
| 25 | + registerTool(definition: any) { |
| 26 | + this.tools.set(definition.name, definition); |
| 27 | + } |
| 28 | + |
| 29 | + on(event: string, handler: Handler) { |
| 30 | + const handlers = this.handlers.get(event) ?? []; |
| 31 | + handlers.push(handler); |
| 32 | + this.handlers.set(event, handlers); |
| 33 | + } |
| 34 | + |
| 35 | + getActiveTools() { |
| 36 | + return [...this.activeTools]; |
| 37 | + } |
| 38 | + |
| 39 | + setActiveTools(tools: string[]) { |
| 40 | + this.activeTools = [...tools]; |
| 41 | + } |
| 42 | + |
| 43 | + sendUserMessage(content: string, options?: any) { |
| 44 | + this.sentUserMessages.push({ content, options }); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +test("/handoff sends the direction back through the LLM without opening the editor", async () => { |
| 49 | + const pi = new MockPi(); |
| 50 | + const state = createState(); |
| 51 | + registerHandoffCommand(pi as any, state); |
| 52 | + |
| 53 | + await pi.commands.get("handoff")!.handler("implement auth", { |
| 54 | + hasUI: true, |
| 55 | + isIdle: () => true, |
| 56 | + ui: { notify: (_message: string) => {} }, |
| 57 | + }); |
| 58 | + |
| 59 | + assert.deepEqual(state.pendingRequestedHandoff, { |
| 60 | + direction: "implement auth", |
| 61 | + enforcementAttempts: 0, |
| 62 | + toolCalled: false, |
| 63 | + }); |
| 64 | + assert.deepEqual(pi.sentUserMessages, [ |
| 65 | + { |
| 66 | + content: |
| 67 | + "Handoff direction: implement auth\n\nPrepare a real handoff in the current session and current context. Before calling the handoff tool, capture any reusable state in the ledger if needed. Then complete the picture in a concise but sufficiently detailed handoff brief and call the handoff tool in this turn. Preserve the important knowledge that is still only present in the current context so the next clean context can start well without re-deriving it. Use any structure that makes the next work unambiguous. Include findings, current state, unresolved questions, failed paths worth avoiding, next steps, refs, constraints, and spawn ideas when useful. Reference ledger entries by name when relevant.", |
| 68 | + options: undefined, |
| 69 | + }, |
| 70 | + ]); |
| 71 | +}); |
| 72 | + |
| 73 | +test("/handoff requires a direction", async () => { |
| 74 | + const pi = new MockPi(); |
| 75 | + const state = createState(); |
| 76 | + registerHandoffCommand(pi as any, state); |
| 77 | + |
| 78 | + const notifications: string[] = []; |
| 79 | + await pi.commands.get("handoff")!.handler(" ", { |
| 80 | + hasUI: true, |
| 81 | + isIdle: () => true, |
| 82 | + ui: { notify: (message: string) => notifications.push(message) }, |
| 83 | + }); |
| 84 | + |
| 85 | + assert.deepEqual(notifications, ["Usage: /handoff <direction>"]); |
| 86 | + assert.deepEqual(pi.sentUserMessages, []); |
| 87 | +}); |
| 88 | + |
| 89 | +test("handoff tool triggers compaction and resumes with the compacted task", async () => { |
| 90 | + const pi = new MockPi(); |
| 91 | + const state = createState(); |
| 92 | + state.pendingRequestedHandoff = { direction: "implement auth", enforcementAttempts: 0, toolCalled: false }; |
| 93 | + registerHandoffTool(pi as any, state); |
| 94 | + |
| 95 | + let compactOptions: any; |
| 96 | + const result = await pi.tools.get("handoff").execute( |
| 97 | + "1", |
| 98 | + { task: "Goal: continue" }, |
| 99 | + undefined, |
| 100 | + undefined, |
| 101 | + { |
| 102 | + compact: (options: any) => { |
| 103 | + compactOptions = options; |
| 104 | + }, |
| 105 | + }, |
| 106 | + ); |
| 107 | + |
| 108 | + assert.equal(state.pendingHandoff?.source, "tool"); |
| 109 | + assert.match(state.pendingHandoff?.task ?? "", /## Handoff — Continue Previous Work/); |
| 110 | + assert.match(state.pendingHandoff?.task ?? "", /Goal: continue/); |
| 111 | + assert.equal(state.pendingRequestedHandoff?.toolCalled, true); |
| 112 | + assert.equal(typeof compactOptions?.onComplete, "function"); |
| 113 | + assert.equal(result.content[0].text, "Handoff started."); |
| 114 | + assert.equal(result.terminate, true); |
| 115 | + |
| 116 | + compactOptions.onComplete({}); |
| 117 | + assert.deepEqual(pi.sentUserMessages, [{ content: "Proceed.", options: undefined }]); |
| 118 | +}); |
| 119 | + |
| 120 | +test("handoff compaction replaces old context with the queued task", async () => { |
| 121 | + const pi = new MockPi(); |
| 122 | + const state = createState(); |
| 123 | + state.pendingHandoff = { task: "Goal: continue", source: "tool" }; |
| 124 | + state.pendingRequestedHandoff = { direction: "implement auth", enforcementAttempts: 1, toolCalled: true }; |
| 125 | + registerHandoffCompaction(pi as any, state); |
| 126 | + |
| 127 | + const [handler] = pi.handlers.get("session_before_compact")!; |
| 128 | + const result = await handler( |
| 129 | + { |
| 130 | + preparation: { tokensBefore: 123 }, |
| 131 | + branchEntries: [{ id: "leaf-1" }], |
| 132 | + }, |
| 133 | + {}, |
| 134 | + ); |
| 135 | + |
| 136 | + assert.equal(state.pendingHandoff, null); |
| 137 | + assert.equal(state.pendingRequestedHandoff, null); |
| 138 | + assert.equal(result.compaction.summary, "Goal: continue"); |
| 139 | + assert.equal(result.compaction.tokensBefore, 123); |
| 140 | + assert.equal(result.compaction.firstKeptEntryId, "leaf-1-handoff-cut"); |
| 141 | + assert.deepEqual(result.compaction.details, { handoff: true, task: "Goal: continue" }); |
| 142 | +}); |
| 143 | + |
| 144 | +test("watchdog records context usage without user notifications", async () => { |
| 145 | + const pi = new MockPi(); |
| 146 | + const state = createState(); |
| 147 | + registerWatchdog(pi as any, state); |
| 148 | + const [handler] = pi.handlers.get("agent_end")!; |
| 149 | + |
| 150 | + const notifications: string[] = []; |
| 151 | + await handler( |
| 152 | + {}, |
| 153 | + { |
| 154 | + hasUI: true, |
| 155 | + ui: { notify: (message: string) => notifications.push(message) }, |
| 156 | + getContextUsage: () => ({ percent: 70 }), |
| 157 | + }, |
| 158 | + ); |
| 159 | + |
| 160 | + assert.equal(state.lastContextPercent, 70); |
| 161 | + assert.deepEqual(notifications, []); |
| 162 | +}); |
| 163 | + |
| 164 | +test("context injects watchdog reminder before each LLM call", async () => { |
| 165 | + const pi = new MockPi(); |
| 166 | + registerAgenticoding(pi as any); |
| 167 | + const [handler] = pi.handlers.get("context")!; |
| 168 | + |
| 169 | + const result = await handler( |
| 170 | + { messages: [{ role: "user", content: "hi", timestamp: 1 }] }, |
| 171 | + { |
| 172 | + getContextUsage: () => ({ percent: 70 }), |
| 173 | + }, |
| 174 | + ); |
| 175 | + |
| 176 | + assert.equal(result.messages.length, 2); |
| 177 | + assert.deepEqual(result.messages[0], { role: "user", content: "hi", timestamp: 1 }); |
| 178 | + assert.equal(result.messages[1].role, "custom"); |
| 179 | + assert.equal(result.messages[1].customType, "agenticoding-watchdog"); |
| 180 | + assert.equal(result.messages[1].display, false); |
| 181 | + assert.match(result.messages[1].content, /Context at 70%/); |
| 182 | +}); |
| 183 | + |
| 184 | +test("watchdog stays advisory when a requested handoff is not completed", async () => { |
| 185 | + const pi = new MockPi(); |
| 186 | + const state = createState(); |
| 187 | + state.pendingRequestedHandoff = { direction: "implement auth", enforcementAttempts: 0, toolCalled: false }; |
| 188 | + registerWatchdog(pi as any, state); |
| 189 | + const [handler] = pi.handlers.get("agent_end")!; |
| 190 | + |
| 191 | + const notifications: string[] = []; |
| 192 | + await handler( |
| 193 | + {}, |
| 194 | + { |
| 195 | + hasUI: true, |
| 196 | + ui: { notify: (message: string) => notifications.push(message) }, |
| 197 | + getContextUsage: () => ({ percent: 20 }), |
| 198 | + }, |
| 199 | + ); |
| 200 | + |
| 201 | + assert.equal(state.pendingRequestedHandoff, null); |
| 202 | + assert.deepEqual(notifications, []); |
| 203 | + assert.deepEqual(pi.sentUserMessages, []); |
| 204 | +}); |
| 205 | + |
| 206 | +test("child tool set keeps spawn recursion but blocks handoff", () => { |
| 207 | + const state = createState(); |
| 208 | + const childTools = createChildTools(new MockPi() as any, state, "medium"); |
| 209 | + const toolNames = buildChildToolNames(["read", "bash", "handoff", "spawn"], childTools); |
| 210 | + |
| 211 | + assert.ok(toolNames.includes("read")); |
| 212 | + assert.ok(toolNames.includes("bash")); |
| 213 | + assert.ok(toolNames.includes("ledger_add")); |
| 214 | + assert.ok(toolNames.includes("ledger_get")); |
| 215 | + assert.ok(toolNames.includes("ledger_list")); |
| 216 | + assert.equal(toolNames.includes("handoff"), false); |
| 217 | + assert.equal(toolNames.includes("spawn"), true); |
| 218 | +}); |
| 219 | + |
| 220 | +test("ledger rehydration rebuilds the latest epoch and enables ledger tools", async () => { |
| 221 | + const pi = new MockPi(); |
| 222 | + const state = createState(); |
| 223 | + registerLedgerRehydration(pi as any, state); |
| 224 | + const [handler] = pi.handlers.get("session_start")!; |
| 225 | + |
| 226 | + await handler( |
| 227 | + {}, |
| 228 | + { |
| 229 | + sessionManager: { |
| 230 | + getBranch: () => [ |
| 231 | + { type: "custom", customType: "ledger-entry", data: { epoch: 1, name: "old", content: "old" } }, |
| 232 | + { type: "custom", customType: "ledger-entry", data: { epoch: 2, name: "keep", content: "new" } }, |
| 233 | + { type: "custom", customType: "ledger-entry", data: { epoch: 2, name: "keep", content: "newer" } }, |
| 234 | + ], |
| 235 | + }, |
| 236 | + }, |
| 237 | + ); |
| 238 | + |
| 239 | + assert.equal(state.epoch, 2); |
| 240 | + assert.deepEqual(Array.from(state.ledger.entries()), [["keep", "newer"]]); |
| 241 | + assert.deepEqual(pi.activeTools, ["ledger_get", "ledger_list"]); |
| 242 | +}); |
0 commit comments