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