@ftrdotdev/pi-tracker 0.5.1 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +56 -15
- package/package.json +8 -8
- package/src/compaction/candidate.test.ts +188 -0
- package/src/compaction/candidate.ts +75 -0
- package/src/compaction/classifier.test.ts +415 -0
- package/src/compaction/classifier.ts +196 -0
- package/src/compaction/completion.test.ts +179 -0
- package/src/compaction/completion.ts +96 -0
- package/src/compaction/digest.test.ts +90 -0
- package/src/compaction/digest.ts +71 -0
- package/src/compaction/pointer.test.ts +26 -0
- package/src/compaction/pointer.ts +15 -0
- package/src/compaction/settings.test.ts +259 -0
- package/src/compaction/settings.ts +163 -0
- package/src/compaction/settle.ts +94 -0
- package/src/index.test.ts +573 -33
- package/src/index.ts +94 -8
- package/src/{tool-metadata.test.ts → presentation/tool-metadata.test.ts} +1 -1
- package/src/{tool-metadata.ts → presentation/tool-metadata.ts} +3 -3
- package/src/{ui.test.ts → presentation/ui.test.ts} +68 -3
- package/src/{ui.ts → presentation/ui.ts} +98 -23
- /package/src/{deps.test.ts → core/deps.test.ts} +0 -0
- /package/src/{deps.ts → core/deps.ts} +0 -0
- /package/src/{domain.test.ts → core/domain.test.ts} +0 -0
- /package/src/{domain.ts → core/domain.ts} +0 -0
- /package/src/{persistence.test.ts → core/persistence.test.ts} +0 -0
- /package/src/{persistence.ts → core/persistence.ts} +0 -0
- /package/src/{store.test.ts → core/store.test.ts} +0 -0
- /package/src/{store.ts → core/store.ts} +0 -0
package/src/index.test.ts
CHANGED
|
@@ -5,47 +5,201 @@
|
|
|
5
5
|
* contract broke once: the scalar form returned one `TodoItem`, the renderer
|
|
6
6
|
* called `.map` on it, and the tool failed with `items.map is not a function`.
|
|
7
7
|
*/
|
|
8
|
-
import
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
import {
|
|
9
|
+
type AgentBeforeSettleEvent,
|
|
10
|
+
type AgentToolResult,
|
|
11
|
+
type CompactOptions,
|
|
12
|
+
type ExtensionAPI,
|
|
13
|
+
type ExtensionToolContext,
|
|
14
|
+
type ToolDefinition,
|
|
13
15
|
} from "@earendil-works/pi-coding-agent";
|
|
16
|
+
import type {
|
|
17
|
+
ClassifierApi,
|
|
18
|
+
ClassifierChoiceAnswer,
|
|
19
|
+
ClassifierModel,
|
|
20
|
+
ClassifierResult,
|
|
21
|
+
JsonObject,
|
|
22
|
+
} from "@earendil-works/pi-ai";
|
|
23
|
+
import * as NodeFileSystem from "@effect/platform-node/NodeFileSystem";
|
|
24
|
+
import { Effect } from "effect";
|
|
25
|
+
import * as FileSystem from "effect/FileSystem";
|
|
26
|
+
import { join } from "node:path";
|
|
14
27
|
import { describe, expect, it } from "vitest";
|
|
28
|
+
import {
|
|
29
|
+
NEEDS_CONTEXT_LABEL,
|
|
30
|
+
NEEDS_NONE_LABEL,
|
|
31
|
+
type ClassifierRegistry,
|
|
32
|
+
} from "./compaction/classifier.ts";
|
|
15
33
|
import tracker from "./index.ts";
|
|
16
|
-
import type { TrackerToolDetails, TrackerToolParams } from "./tool-metadata.ts";
|
|
34
|
+
import type { TrackerToolDetails, TrackerToolParams } from "./presentation/tool-metadata.ts";
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The variable `getAgentDir()` reads. Pi 1.0.2 does not re-export the
|
|
38
|
+
* `ENV_AGENT_DIR` constant from its package root, so its value is inlined here.
|
|
39
|
+
*/
|
|
40
|
+
const AGENT_DIR_ENV = "PI_CODING_AGENT_DIR";
|
|
17
41
|
|
|
18
42
|
interface Harness {
|
|
19
43
|
readonly tool: ToolDefinition<any, unknown, any>;
|
|
20
|
-
readonly ctx:
|
|
44
|
+
readonly ctx: ExtensionToolContext;
|
|
21
45
|
/** Every persisted snapshot, in the order the session received it. */
|
|
22
46
|
readonly appends: unknown[];
|
|
47
|
+
/** Every `ctx.compact` call's options, in order. */
|
|
48
|
+
readonly compacts: CompactOptions[];
|
|
49
|
+
/** Every `ctx.ui.notify` call. */
|
|
50
|
+
readonly notifications: Array<{ readonly message: string; readonly severity?: string }>;
|
|
51
|
+
/** Every `pi.sendMessage` call. */
|
|
52
|
+
readonly sends: Array<{ readonly message: any; readonly options?: any }>;
|
|
53
|
+
/** Every `pi.sendUserMessage` call. */
|
|
54
|
+
readonly userSends: unknown[];
|
|
55
|
+
/** Invoke the captured `agent_before_settle` handler. */
|
|
56
|
+
readonly settle: (event?: Partial<AgentBeforeSettleEvent>) => Promise<void>;
|
|
57
|
+
/** Invoke the captured `session_compact_failed` handler. */
|
|
58
|
+
readonly compactFailed: (aborted: boolean) => Promise<void>;
|
|
23
59
|
}
|
|
24
60
|
|
|
61
|
+
interface HarnessOptions {
|
|
62
|
+
/** Classifier registry double; defaults to one with no available classifier. */
|
|
63
|
+
readonly registry?: ClassifierRegistry;
|
|
64
|
+
/** Parsed contents for the temp `tui.json`; omitted means no config file. */
|
|
65
|
+
readonly config?: unknown;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const classifierModel = (provider: string, id: string): ClassifierModel<ClassifierApi> => ({
|
|
69
|
+
id,
|
|
70
|
+
name: id,
|
|
71
|
+
api: "typesafe-system-one",
|
|
72
|
+
provider,
|
|
73
|
+
baseUrl: "https://example.invalid",
|
|
74
|
+
input: ["text"],
|
|
75
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
76
|
+
type: "classifier",
|
|
77
|
+
contextWindow: 8192,
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
/** Answer every question in the classifier context with the same needs mass. */
|
|
81
|
+
const choiceResult = (
|
|
82
|
+
needsMass: number,
|
|
83
|
+
context: { readonly questions: Record<string, unknown> },
|
|
84
|
+
confidence = 1,
|
|
85
|
+
): ClassifierResult => {
|
|
86
|
+
const answers: Record<string, ClassifierChoiceAnswer> = {};
|
|
87
|
+
for (const key of Object.keys(context.questions)) {
|
|
88
|
+
answers[key] = {
|
|
89
|
+
type: "choice",
|
|
90
|
+
choice: needsMass >= 0.5 ? NEEDS_CONTEXT_LABEL : NEEDS_NONE_LABEL,
|
|
91
|
+
probabilities: { [NEEDS_CONTEXT_LABEL]: needsMass, [NEEDS_NONE_LABEL]: 1 - needsMass },
|
|
92
|
+
confidence,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
return {
|
|
96
|
+
api: "typesafe-system-one",
|
|
97
|
+
provider: "typesafe",
|
|
98
|
+
model: "jev-latest",
|
|
99
|
+
answers,
|
|
100
|
+
stopReason: "stop",
|
|
101
|
+
timestamp: 0,
|
|
102
|
+
};
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
/** Create a temp agent directory (and optional `tui.json`) via the FileSystem service. */
|
|
106
|
+
const prepareAgentDir = (config: unknown): Promise<string> =>
|
|
107
|
+
Effect.runPromise(
|
|
108
|
+
Effect.gen(function* () {
|
|
109
|
+
const fs = yield* FileSystem.FileSystem;
|
|
110
|
+
const directory = yield* fs.makeTempDirectory({ prefix: "tracker-smart-compaction-" });
|
|
111
|
+
if (config !== undefined) {
|
|
112
|
+
yield* fs.writeFileString(join(directory, "tui.json"), JSON.stringify(config));
|
|
113
|
+
}
|
|
114
|
+
return directory;
|
|
115
|
+
}).pipe(Effect.provide(NodeFileSystem.layer)),
|
|
116
|
+
);
|
|
117
|
+
|
|
25
118
|
/**
|
|
26
119
|
* Register the extension against a minimal fake API and return the captured
|
|
27
|
-
* tool plus a context that satisfies the paths `execute
|
|
28
|
-
*
|
|
120
|
+
* tool plus a context that satisfies the paths `execute`, the widget refresh,
|
|
121
|
+
* persistence, and the settle handler touch.
|
|
122
|
+
*
|
|
123
|
+
* Sets the agent-dir environment variable to a fresh temp directory before the
|
|
124
|
+
* factory runs, so the settings reader never touches the developer's real
|
|
125
|
+
* `~/.pi/agent/tui.json`.
|
|
29
126
|
*/
|
|
30
|
-
const makeHarness = (): Harness => {
|
|
127
|
+
const makeHarness = async (options: HarnessOptions = {}): Promise<Harness> => {
|
|
128
|
+
const agentDir = await prepareAgentDir(options.config);
|
|
129
|
+
process.env[AGENT_DIR_ENV] = agentDir;
|
|
130
|
+
|
|
31
131
|
let registered: ToolDefinition<any, unknown, any> | undefined;
|
|
132
|
+
const handlers = new Map<string, (event: any, ctx: any) => unknown>();
|
|
32
133
|
const appends: unknown[] = [];
|
|
134
|
+
const compacts: CompactOptions[] = [];
|
|
135
|
+
const notifications: Array<{ message: string; severity?: string }> = [];
|
|
136
|
+
const sends: Array<{ message: any; options?: any }> = [];
|
|
137
|
+
const userSends: unknown[] = [];
|
|
138
|
+
|
|
139
|
+
const registry: ClassifierRegistry = options.registry ?? {
|
|
140
|
+
getAvailableOfType: async () => [],
|
|
141
|
+
classify: async (_model, context) => choiceResult(1, context),
|
|
142
|
+
};
|
|
143
|
+
|
|
33
144
|
const api = {
|
|
34
145
|
registerTool: (tool: ToolDefinition<any, unknown, any>) => {
|
|
35
146
|
registered = tool;
|
|
36
147
|
},
|
|
37
148
|
registerCommand: () => {},
|
|
38
|
-
on: () => {
|
|
149
|
+
on: (event: string, handler: (event: any, ctx: any) => unknown) => {
|
|
150
|
+
handlers.set(event, handler);
|
|
151
|
+
},
|
|
39
152
|
appendEntry: (_type: string, data: unknown) => {
|
|
40
153
|
appends.push(data);
|
|
41
154
|
},
|
|
155
|
+
sendMessage: (message: any, options?: any) => {
|
|
156
|
+
sends.push({ message, options });
|
|
157
|
+
},
|
|
158
|
+
sendUserMessage: (content: unknown) => {
|
|
159
|
+
userSends.push(content);
|
|
160
|
+
},
|
|
42
161
|
} as unknown as ExtensionAPI;
|
|
162
|
+
|
|
43
163
|
tracker(api);
|
|
44
164
|
if (!registered) throw new Error("tracker did not register a tool");
|
|
165
|
+
|
|
45
166
|
const ctx = {
|
|
46
|
-
ui: {
|
|
47
|
-
|
|
48
|
-
|
|
167
|
+
ui: {
|
|
168
|
+
setWidget: () => {},
|
|
169
|
+
notify: (message: string, severity?: string) => {
|
|
170
|
+
notifications.push({ message, ...(severity === undefined ? {} : { severity }) });
|
|
171
|
+
},
|
|
172
|
+
},
|
|
173
|
+
modelRegistry: registry,
|
|
174
|
+
signal: undefined,
|
|
175
|
+
compact: (opts?: CompactOptions) => {
|
|
176
|
+
compacts.push(opts ?? {});
|
|
177
|
+
},
|
|
178
|
+
} as unknown as ExtensionToolContext;
|
|
179
|
+
|
|
180
|
+
const settle = async (event: Partial<AgentBeforeSettleEvent> = {}): Promise<void> => {
|
|
181
|
+
const handler = handlers.get("agent_before_settle");
|
|
182
|
+
if (!handler) throw new Error("tracker did not register an agent_before_settle handler");
|
|
183
|
+
await handler({ type: "agent_before_settle", outcome: "completed", ...event }, ctx);
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
const compactFailed = async (aborted: boolean): Promise<void> => {
|
|
187
|
+
const handler = handlers.get("session_compact_failed");
|
|
188
|
+
if (!handler) throw new Error("tracker did not register a session_compact_failed handler");
|
|
189
|
+
await handler({ type: "session_compact_failed", aborted }, ctx);
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
return {
|
|
193
|
+
tool: registered,
|
|
194
|
+
ctx,
|
|
195
|
+
appends,
|
|
196
|
+
compacts,
|
|
197
|
+
notifications,
|
|
198
|
+
sends,
|
|
199
|
+
userSends,
|
|
200
|
+
settle,
|
|
201
|
+
compactFailed,
|
|
202
|
+
};
|
|
49
203
|
};
|
|
50
204
|
|
|
51
205
|
const run = (
|
|
@@ -63,7 +217,7 @@ const textOf = (result: AgentToolResult<TrackerToolDetails>): string => {
|
|
|
63
217
|
|
|
64
218
|
describe("tracker tool bridge", () => {
|
|
65
219
|
it("returns an affected-items array for the scalar update_item form", async () => {
|
|
66
|
-
const harness = makeHarness();
|
|
220
|
+
const harness = await makeHarness();
|
|
67
221
|
await run(harness, {
|
|
68
222
|
action: "create_list",
|
|
69
223
|
name: "Work",
|
|
@@ -79,7 +233,7 @@ describe("tracker tool bridge", () => {
|
|
|
79
233
|
});
|
|
80
234
|
|
|
81
235
|
it("keeps the text patch for the scalar update_item form", async () => {
|
|
82
|
-
const harness = makeHarness();
|
|
236
|
+
const harness = await makeHarness();
|
|
83
237
|
await run(harness, { action: "create_list", name: "Work", initial_items: ["old"] });
|
|
84
238
|
|
|
85
239
|
const result = await run(harness, { action: "update_item", item_id: "Work:1", text: "new" });
|
|
@@ -90,7 +244,7 @@ describe("tracker tool bridge", () => {
|
|
|
90
244
|
});
|
|
91
245
|
|
|
92
246
|
it("returns one entry per patch for the batch update_item form", async () => {
|
|
93
|
-
const harness = makeHarness();
|
|
247
|
+
const harness = await makeHarness();
|
|
94
248
|
await run(harness, { action: "create_list", name: "Work", initial_items: ["a", "b", "c"] });
|
|
95
249
|
|
|
96
250
|
const result = await run(harness, {
|
|
@@ -108,7 +262,7 @@ describe("tracker tool bridge", () => {
|
|
|
108
262
|
});
|
|
109
263
|
|
|
110
264
|
it("shows id-based references in the list output", async () => {
|
|
111
|
-
const harness = makeHarness();
|
|
265
|
+
const harness = await makeHarness();
|
|
112
266
|
await run(harness, {
|
|
113
267
|
action: "create_list",
|
|
114
268
|
name: "Work",
|
|
@@ -122,7 +276,7 @@ describe("tracker tool bridge", () => {
|
|
|
122
276
|
});
|
|
123
277
|
|
|
124
278
|
it("shows stable ids after a removal", async () => {
|
|
125
|
-
const harness = makeHarness();
|
|
279
|
+
const harness = await makeHarness();
|
|
126
280
|
await run(harness, { action: "create_list", name: "Work", initial_items: ["a", "b", "c"] });
|
|
127
281
|
|
|
128
282
|
await run(harness, { action: "remove_item", item_id: "Work:1" });
|
|
@@ -135,7 +289,7 @@ describe("tracker tool bridge", () => {
|
|
|
135
289
|
});
|
|
136
290
|
|
|
137
291
|
it("reports an error result for a missing item instead of throwing", async () => {
|
|
138
|
-
const harness = makeHarness();
|
|
292
|
+
const harness = await makeHarness();
|
|
139
293
|
await run(harness, { action: "create_list", name: "Work", initial_items: ["only"] });
|
|
140
294
|
|
|
141
295
|
const result = await run(harness, { action: "update_item", item_id: "Work:9", done: true });
|
|
@@ -145,7 +299,7 @@ describe("tracker tool bridge", () => {
|
|
|
145
299
|
});
|
|
146
300
|
|
|
147
301
|
it("accepts dependencies in the object item form", async () => {
|
|
148
|
-
const harness = makeHarness();
|
|
302
|
+
const harness = await makeHarness();
|
|
149
303
|
await run(harness, {
|
|
150
304
|
action: "create_list",
|
|
151
305
|
name: "Work",
|
|
@@ -164,7 +318,7 @@ describe("tracker tool bridge", () => {
|
|
|
164
318
|
});
|
|
165
319
|
|
|
166
320
|
it("rejects a malformed dependency reference with an actionable message", async () => {
|
|
167
|
-
const harness = makeHarness();
|
|
321
|
+
const harness = await makeHarness();
|
|
168
322
|
await run(harness, { action: "create_list", name: "Work", initial_items: ["a"] });
|
|
169
323
|
|
|
170
324
|
const result = await run(harness, {
|
|
@@ -177,7 +331,7 @@ describe("tracker tool bridge", () => {
|
|
|
177
331
|
});
|
|
178
332
|
|
|
179
333
|
it("names the cycle when a dependency would close one", async () => {
|
|
180
|
-
const harness = makeHarness();
|
|
334
|
+
const harness = await makeHarness();
|
|
181
335
|
await run(harness, {
|
|
182
336
|
action: "create_list",
|
|
183
337
|
name: "Work",
|
|
@@ -195,7 +349,7 @@ describe("tracker tool bridge", () => {
|
|
|
195
349
|
});
|
|
196
350
|
|
|
197
351
|
it("annotates blocked items and names what is ready", async () => {
|
|
198
|
-
const harness = makeHarness();
|
|
352
|
+
const harness = await makeHarness();
|
|
199
353
|
await run(harness, {
|
|
200
354
|
action: "create_list",
|
|
201
355
|
name: "Work",
|
|
@@ -210,7 +364,7 @@ describe("tracker tool bridge", () => {
|
|
|
210
364
|
});
|
|
211
365
|
|
|
212
366
|
it("leaves the list output untouched for a dependency-free list", async () => {
|
|
213
|
-
const harness = makeHarness();
|
|
367
|
+
const harness = await makeHarness();
|
|
214
368
|
await run(harness, { action: "create_list", name: "Work", initial_items: ["a", "b"] });
|
|
215
369
|
|
|
216
370
|
const text = textOf(await run(harness, { action: "list" }));
|
|
@@ -221,7 +375,7 @@ describe("tracker tool bridge", () => {
|
|
|
221
375
|
});
|
|
222
376
|
|
|
223
377
|
it("names the blockers when a completion is refused", async () => {
|
|
224
|
-
const harness = makeHarness();
|
|
378
|
+
const harness = await makeHarness();
|
|
225
379
|
await run(harness, {
|
|
226
380
|
action: "create_list",
|
|
227
381
|
name: "Work",
|
|
@@ -235,7 +389,7 @@ describe("tracker tool bridge", () => {
|
|
|
235
389
|
});
|
|
236
390
|
|
|
237
391
|
it("notes the done dependents when a dependency is reopened", async () => {
|
|
238
|
-
const harness = makeHarness();
|
|
392
|
+
const harness = await makeHarness();
|
|
239
393
|
await run(harness, {
|
|
240
394
|
action: "create_list",
|
|
241
395
|
name: "Work",
|
|
@@ -253,7 +407,7 @@ describe("tracker tool bridge", () => {
|
|
|
253
407
|
});
|
|
254
408
|
|
|
255
409
|
it("adds no reopen note when no dependent is done", async () => {
|
|
256
|
-
const harness = makeHarness();
|
|
410
|
+
const harness = await makeHarness();
|
|
257
411
|
await run(harness, {
|
|
258
412
|
action: "create_list",
|
|
259
413
|
name: "Work",
|
|
@@ -269,7 +423,7 @@ describe("tracker tool bridge", () => {
|
|
|
269
423
|
});
|
|
270
424
|
|
|
271
425
|
it("marks a done item whose dependency is open again", async () => {
|
|
272
|
-
const harness = makeHarness();
|
|
426
|
+
const harness = await makeHarness();
|
|
273
427
|
await run(harness, {
|
|
274
428
|
action: "create_list",
|
|
275
429
|
name: "Work",
|
|
@@ -288,7 +442,7 @@ describe("tracker tool bridge", () => {
|
|
|
288
442
|
});
|
|
289
443
|
|
|
290
444
|
it("notes a done item that a dependency edit left waiting", async () => {
|
|
291
|
-
const harness = makeHarness();
|
|
445
|
+
const harness = await makeHarness();
|
|
292
446
|
await run(harness, { action: "create_list", name: "Work", initial_items: ["a", "b"] });
|
|
293
447
|
await run(harness, { action: "update_item", item_id: "Work:2", done: true });
|
|
294
448
|
|
|
@@ -300,7 +454,7 @@ describe("tracker tool bridge", () => {
|
|
|
300
454
|
});
|
|
301
455
|
|
|
302
456
|
it("names the dependencies a deps edit removed", async () => {
|
|
303
|
-
const harness = makeHarness();
|
|
457
|
+
const harness = await makeHarness();
|
|
304
458
|
await run(harness, {
|
|
305
459
|
action: "create_list",
|
|
306
460
|
name: "Work",
|
|
@@ -320,7 +474,7 @@ describe("tracker tool bridge", () => {
|
|
|
320
474
|
});
|
|
321
475
|
|
|
322
476
|
it("persists overlapping mutations in order", async () => {
|
|
323
|
-
const harness = makeHarness();
|
|
477
|
+
const harness = await makeHarness();
|
|
324
478
|
await run(harness, { action: "create_list", name: "Work", initial_items: ["a", "b"] });
|
|
325
479
|
|
|
326
480
|
// Two calls in flight at once. The last snapshot the session receives must
|
|
@@ -341,7 +495,7 @@ describe("tracker tool bridge", () => {
|
|
|
341
495
|
});
|
|
342
496
|
|
|
343
497
|
it("shows items in dependency order, not stored order", async () => {
|
|
344
|
-
const harness = makeHarness();
|
|
498
|
+
const harness = await makeHarness();
|
|
345
499
|
await run(harness, {
|
|
346
500
|
action: "create_list",
|
|
347
501
|
name: "Work",
|
|
@@ -356,3 +510,389 @@ describe("tracker tool bridge", () => {
|
|
|
356
510
|
expect(text).toContain("#Work:2: write plan");
|
|
357
511
|
});
|
|
358
512
|
});
|
|
513
|
+
|
|
514
|
+
describe("tracker smart-compaction bridge", () => {
|
|
515
|
+
const available: readonly ClassifierModel<ClassifierApi>[] = [
|
|
516
|
+
classifierModel("typesafe", "jev-latest"),
|
|
517
|
+
];
|
|
518
|
+
|
|
519
|
+
const compactRegistry = (
|
|
520
|
+
needsMass: number,
|
|
521
|
+
onDescribe?: (state: JsonObject) => void,
|
|
522
|
+
confidence = 1,
|
|
523
|
+
): ClassifierRegistry => ({
|
|
524
|
+
getAvailableOfType: async () => available,
|
|
525
|
+
classify: async (_model, context) => {
|
|
526
|
+
onDescribe?.(context.state);
|
|
527
|
+
return choiceResult(needsMass, context, confidence);
|
|
528
|
+
},
|
|
529
|
+
});
|
|
530
|
+
|
|
531
|
+
/** Create a two-item dependency list and complete the first item. */
|
|
532
|
+
const completeFirst = async (harness: Harness): Promise<void> => {
|
|
533
|
+
await run(harness, {
|
|
534
|
+
action: "create_list",
|
|
535
|
+
name: "Work",
|
|
536
|
+
initial_items: ["first", { text: "second", deps: ["Work:1"] }],
|
|
537
|
+
});
|
|
538
|
+
await run(harness, { action: "update_item", item_id: "Work:1", done: true });
|
|
539
|
+
};
|
|
540
|
+
|
|
541
|
+
it("a completed item with a compact verdict starts pi's compaction at the settle boundary", async () => {
|
|
542
|
+
const harness = await makeHarness({ registry: compactRegistry(0.1) });
|
|
543
|
+
await completeFirst(harness);
|
|
544
|
+
|
|
545
|
+
await harness.settle({ outcome: "completed" });
|
|
546
|
+
|
|
547
|
+
expect(harness.compacts).toHaveLength(1);
|
|
548
|
+
expect(harness.compacts[0]?.customInstructions).toBeUndefined();
|
|
549
|
+
expect(typeof harness.compacts[0]?.onComplete).toBe("function");
|
|
550
|
+
expect(typeof harness.compacts[0]?.onError).toBe("function");
|
|
551
|
+
});
|
|
552
|
+
|
|
553
|
+
it("a keep verdict leaves the run untouched", async () => {
|
|
554
|
+
const harness = await makeHarness({ registry: compactRegistry(0.9) });
|
|
555
|
+
await completeFirst(harness);
|
|
556
|
+
|
|
557
|
+
await harness.settle();
|
|
558
|
+
|
|
559
|
+
expect(harness.compacts).toHaveLength(0);
|
|
560
|
+
});
|
|
561
|
+
|
|
562
|
+
it("a raised keepContextThreshold compacts an answer the default would keep", async () => {
|
|
563
|
+
const harness = await makeHarness({
|
|
564
|
+
registry: compactRegistry(0.6),
|
|
565
|
+
config: { smartCompaction: { keepContextThreshold: 0.9 } },
|
|
566
|
+
});
|
|
567
|
+
await completeFirst(harness);
|
|
568
|
+
|
|
569
|
+
await harness.settle();
|
|
570
|
+
|
|
571
|
+
expect(harness.compacts).toHaveLength(1);
|
|
572
|
+
});
|
|
573
|
+
|
|
574
|
+
it("a lowered keepContextThreshold keeps an answer the default would compact", async () => {
|
|
575
|
+
const harness = await makeHarness({
|
|
576
|
+
registry: compactRegistry(0.45),
|
|
577
|
+
config: { smartCompaction: { keepContextThreshold: 0.2 } },
|
|
578
|
+
});
|
|
579
|
+
await completeFirst(harness);
|
|
580
|
+
|
|
581
|
+
await harness.settle();
|
|
582
|
+
|
|
583
|
+
expect(harness.compacts).toHaveLength(0);
|
|
584
|
+
});
|
|
585
|
+
|
|
586
|
+
it("keeps context when the classifier answer is below the confidence floor", async () => {
|
|
587
|
+
const harness = await makeHarness({
|
|
588
|
+
registry: compactRegistry(0.1, undefined, 0.2),
|
|
589
|
+
config: { smartCompaction: { keepContextMinConfidence: 0.9 } },
|
|
590
|
+
});
|
|
591
|
+
await completeFirst(harness);
|
|
592
|
+
|
|
593
|
+
await harness.settle();
|
|
594
|
+
|
|
595
|
+
expect(harness.compacts).toHaveLength(0);
|
|
596
|
+
});
|
|
597
|
+
|
|
598
|
+
it("compacts when the classifier answer reaches the confidence floor", async () => {
|
|
599
|
+
const harness = await makeHarness({
|
|
600
|
+
registry: compactRegistry(0.1, undefined, 0.9),
|
|
601
|
+
config: { smartCompaction: { keepContextMinConfidence: 0.5 } },
|
|
602
|
+
});
|
|
603
|
+
await completeFirst(harness);
|
|
604
|
+
|
|
605
|
+
await harness.settle();
|
|
606
|
+
|
|
607
|
+
expect(harness.compacts).toHaveLength(1);
|
|
608
|
+
});
|
|
609
|
+
|
|
610
|
+
it("judges the whole ready frontier and keeps context when any candidate needs it", async () => {
|
|
611
|
+
let questions: Record<string, unknown> = {};
|
|
612
|
+
const harness = await makeHarness({
|
|
613
|
+
registry: {
|
|
614
|
+
getAvailableOfType: async () => available,
|
|
615
|
+
classify: async (_model, context) => {
|
|
616
|
+
questions = context.questions;
|
|
617
|
+
const answers: Record<string, ClassifierChoiceAnswer> = {};
|
|
618
|
+
// Work:2 (dependent) says no; Work:3 (independent) says yes.
|
|
619
|
+
for (const key of Object.keys(context.questions)) {
|
|
620
|
+
const needsMass = key.includes("Work_3") ? 0.9 : 0.1;
|
|
621
|
+
answers[key] = {
|
|
622
|
+
type: "choice",
|
|
623
|
+
choice: needsMass >= 0.5 ? NEEDS_CONTEXT_LABEL : NEEDS_NONE_LABEL,
|
|
624
|
+
probabilities: {
|
|
625
|
+
[NEEDS_CONTEXT_LABEL]: needsMass,
|
|
626
|
+
[NEEDS_NONE_LABEL]: 1 - needsMass,
|
|
627
|
+
},
|
|
628
|
+
confidence: 1,
|
|
629
|
+
};
|
|
630
|
+
}
|
|
631
|
+
return {
|
|
632
|
+
api: "typesafe-system-one",
|
|
633
|
+
provider: "typesafe",
|
|
634
|
+
model: "jev-latest",
|
|
635
|
+
answers,
|
|
636
|
+
stopReason: "stop",
|
|
637
|
+
timestamp: 0,
|
|
638
|
+
};
|
|
639
|
+
},
|
|
640
|
+
},
|
|
641
|
+
});
|
|
642
|
+
await run(harness, {
|
|
643
|
+
action: "create_list",
|
|
644
|
+
name: "Work",
|
|
645
|
+
initial_items: ["first", { text: "second", deps: ["Work:1"] }, "third"],
|
|
646
|
+
});
|
|
647
|
+
await run(harness, { action: "update_item", item_id: "Work:1", done: true });
|
|
648
|
+
|
|
649
|
+
await harness.settle();
|
|
650
|
+
|
|
651
|
+
expect(Object.keys(questions).length).toBe(2);
|
|
652
|
+
expect(harness.compacts).toHaveLength(0);
|
|
653
|
+
});
|
|
654
|
+
|
|
655
|
+
it("compacts when every judged candidate is self-contained", async () => {
|
|
656
|
+
const harness = await makeHarness({ registry: compactRegistry(0.1) });
|
|
657
|
+
await run(harness, {
|
|
658
|
+
action: "create_list",
|
|
659
|
+
name: "Work",
|
|
660
|
+
initial_items: ["first", { text: "second", deps: ["Work:1"] }, "third"],
|
|
661
|
+
});
|
|
662
|
+
await run(harness, { action: "update_item", item_id: "Work:1", done: true });
|
|
663
|
+
|
|
664
|
+
await harness.settle();
|
|
665
|
+
|
|
666
|
+
expect(harness.compacts).toHaveLength(1);
|
|
667
|
+
});
|
|
668
|
+
|
|
669
|
+
it("no credential-available classifier leaves the run untouched", async () => {
|
|
670
|
+
let classifyCalls = 0;
|
|
671
|
+
const harness = await makeHarness({
|
|
672
|
+
registry: {
|
|
673
|
+
getAvailableOfType: async () => [],
|
|
674
|
+
classify: async (_model, context) => {
|
|
675
|
+
classifyCalls += 1;
|
|
676
|
+
return choiceResult(0.1, context);
|
|
677
|
+
},
|
|
678
|
+
},
|
|
679
|
+
});
|
|
680
|
+
await completeFirst(harness);
|
|
681
|
+
|
|
682
|
+
await harness.settle();
|
|
683
|
+
|
|
684
|
+
expect(harness.compacts).toHaveLength(0);
|
|
685
|
+
expect(classifyCalls).toBe(0);
|
|
686
|
+
});
|
|
687
|
+
|
|
688
|
+
it("an enabled: false setting leaves the run untouched even when a classifier is available", async () => {
|
|
689
|
+
const harness = await makeHarness({
|
|
690
|
+
registry: compactRegistry(0.1),
|
|
691
|
+
config: { smartCompaction: { enabled: false } },
|
|
692
|
+
});
|
|
693
|
+
await completeFirst(harness);
|
|
694
|
+
|
|
695
|
+
await harness.settle();
|
|
696
|
+
|
|
697
|
+
expect(harness.compacts).toHaveLength(0);
|
|
698
|
+
});
|
|
699
|
+
|
|
700
|
+
it("a settle with no pending completion calls no classifier", async () => {
|
|
701
|
+
let classifyCalls = 0;
|
|
702
|
+
const harness = await makeHarness({
|
|
703
|
+
registry: {
|
|
704
|
+
getAvailableOfType: async () => available,
|
|
705
|
+
classify: async (_model, context) => {
|
|
706
|
+
classifyCalls += 1;
|
|
707
|
+
return choiceResult(0.1, context);
|
|
708
|
+
},
|
|
709
|
+
},
|
|
710
|
+
});
|
|
711
|
+
await run(harness, {
|
|
712
|
+
action: "create_list",
|
|
713
|
+
name: "Work",
|
|
714
|
+
initial_items: ["first", { text: "second", deps: ["Work:1"] }],
|
|
715
|
+
});
|
|
716
|
+
|
|
717
|
+
await harness.settle();
|
|
718
|
+
|
|
719
|
+
expect(classifyCalls).toBe(0);
|
|
720
|
+
expect(harness.compacts).toHaveLength(0);
|
|
721
|
+
});
|
|
722
|
+
|
|
723
|
+
it("one completion yields at most one compaction across settles", async () => {
|
|
724
|
+
const harness = await makeHarness({ registry: compactRegistry(0.1) });
|
|
725
|
+
await completeFirst(harness);
|
|
726
|
+
|
|
727
|
+
await harness.settle();
|
|
728
|
+
await harness.settle();
|
|
729
|
+
|
|
730
|
+
expect(harness.compacts).toHaveLength(1);
|
|
731
|
+
});
|
|
732
|
+
|
|
733
|
+
it("an aborted settle clears the record without compacting", async () => {
|
|
734
|
+
const harness = await makeHarness({ registry: compactRegistry(0.1) });
|
|
735
|
+
await completeFirst(harness);
|
|
736
|
+
|
|
737
|
+
await harness.settle({ outcome: "aborted" });
|
|
738
|
+
await harness.settle({ outcome: "completed" });
|
|
739
|
+
|
|
740
|
+
expect(harness.compacts).toHaveLength(0);
|
|
741
|
+
});
|
|
742
|
+
|
|
743
|
+
it("an errored settle clears the record without compacting", async () => {
|
|
744
|
+
const harness = await makeHarness({ registry: compactRegistry(0.1) });
|
|
745
|
+
await completeFirst(harness);
|
|
746
|
+
|
|
747
|
+
await harness.settle({ outcome: "error" });
|
|
748
|
+
await harness.settle({ outcome: "completed" });
|
|
749
|
+
|
|
750
|
+
expect(harness.compacts).toHaveLength(0);
|
|
751
|
+
});
|
|
752
|
+
|
|
753
|
+
it("the digest centres on the candidate and carries the completed batch as prior work", async () => {
|
|
754
|
+
let captured: JsonObject | undefined;
|
|
755
|
+
const harness = await makeHarness({
|
|
756
|
+
registry: compactRegistry(0.1, (state) => {
|
|
757
|
+
captured = state;
|
|
758
|
+
}),
|
|
759
|
+
});
|
|
760
|
+
await completeFirst(harness);
|
|
761
|
+
|
|
762
|
+
await harness.settle();
|
|
763
|
+
|
|
764
|
+
const digest = captured as unknown as {
|
|
765
|
+
candidates: Array<{ ref: string; class: string }>;
|
|
766
|
+
completed: Array<{ ref: string }>;
|
|
767
|
+
};
|
|
768
|
+
expect(digest.candidates[0]?.ref).toBe("Work:2");
|
|
769
|
+
expect(digest.candidates[0]?.class).toBe("dependent-successor");
|
|
770
|
+
expect(digest.completed).toEqual([{ ref: "Work:1", text: "first" }]);
|
|
771
|
+
});
|
|
772
|
+
|
|
773
|
+
it("a classifier error leaves the run untouched", async () => {
|
|
774
|
+
const harness = await makeHarness({
|
|
775
|
+
registry: {
|
|
776
|
+
getAvailableOfType: async () => available,
|
|
777
|
+
classify: async () => {
|
|
778
|
+
throw new Error("provider unreachable");
|
|
779
|
+
},
|
|
780
|
+
},
|
|
781
|
+
});
|
|
782
|
+
await completeFirst(harness);
|
|
783
|
+
|
|
784
|
+
await harness.settle();
|
|
785
|
+
|
|
786
|
+
expect(harness.compacts).toHaveLength(0);
|
|
787
|
+
});
|
|
788
|
+
|
|
789
|
+
it("an abort signal during classification leaves the run untouched", async () => {
|
|
790
|
+
const controller = new AbortController();
|
|
791
|
+
const harness = await makeHarness({
|
|
792
|
+
registry: {
|
|
793
|
+
getAvailableOfType: async () => available,
|
|
794
|
+
classify: (_model, _context, options) => {
|
|
795
|
+
const signal = options?.signal;
|
|
796
|
+
return new Promise((_resolve, reject) => {
|
|
797
|
+
signal?.addEventListener("abort", () => reject(new Error("aborted")));
|
|
798
|
+
controller.abort();
|
|
799
|
+
});
|
|
800
|
+
},
|
|
801
|
+
},
|
|
802
|
+
});
|
|
803
|
+
harness.ctx.signal = controller.signal;
|
|
804
|
+
await completeFirst(harness);
|
|
805
|
+
|
|
806
|
+
await harness.settle();
|
|
807
|
+
|
|
808
|
+
expect(harness.compacts).toHaveLength(0);
|
|
809
|
+
});
|
|
810
|
+
|
|
811
|
+
it("the hook adds no tracker custom entry and does not change the tool result", async () => {
|
|
812
|
+
const harness = await makeHarness({ registry: compactRegistry(0.1) });
|
|
813
|
+
await run(harness, {
|
|
814
|
+
action: "create_list",
|
|
815
|
+
name: "Work",
|
|
816
|
+
initial_items: ["first", { text: "second", deps: ["Work:1"] }],
|
|
817
|
+
});
|
|
818
|
+
|
|
819
|
+
const result = await run(harness, { action: "update_item", item_id: "Work:1", done: true });
|
|
820
|
+
const appendsBefore = harness.appends.length;
|
|
821
|
+
|
|
822
|
+
await harness.settle();
|
|
823
|
+
|
|
824
|
+
expect(harness.appends).toHaveLength(appendsBefore);
|
|
825
|
+
expect(textOf(result)).toBe("Updated 1 item: Work:1 (completed)");
|
|
826
|
+
});
|
|
827
|
+
|
|
828
|
+
it("compaction completion sends one resume turn naming the next ready item", async () => {
|
|
829
|
+
const harness = await makeHarness({ registry: compactRegistry(0.1) });
|
|
830
|
+
await completeFirst(harness);
|
|
831
|
+
await harness.settle();
|
|
832
|
+
|
|
833
|
+
harness.compacts[0]?.onComplete?.({
|
|
834
|
+
summary: "summary",
|
|
835
|
+
firstKeptEntryId: "entry-1",
|
|
836
|
+
tokensBefore: 100,
|
|
837
|
+
});
|
|
838
|
+
|
|
839
|
+
expect(harness.sends).toHaveLength(1);
|
|
840
|
+
const message = harness.sends[0]?.message as { content: string };
|
|
841
|
+
expect(message.content).toContain("Work:2");
|
|
842
|
+
});
|
|
843
|
+
|
|
844
|
+
it("the resume turn is a continuation message that triggers a turn", async () => {
|
|
845
|
+
const harness = await makeHarness({ registry: compactRegistry(0.1) });
|
|
846
|
+
await completeFirst(harness);
|
|
847
|
+
await harness.settle();
|
|
848
|
+
harness.compacts[0]?.onComplete?.({
|
|
849
|
+
summary: "summary",
|
|
850
|
+
firstKeptEntryId: "entry-1",
|
|
851
|
+
tokensBefore: 100,
|
|
852
|
+
});
|
|
853
|
+
|
|
854
|
+
const message = harness.sends[0]?.message as { customType: string };
|
|
855
|
+
const options = harness.sends[0]?.options as { triggerTurn?: boolean };
|
|
856
|
+
expect(options.triggerTurn).toBe(true);
|
|
857
|
+
expect(message.customType).toBe("tracker/smart-compaction");
|
|
858
|
+
expect(harness.userSends).toHaveLength(0);
|
|
859
|
+
});
|
|
860
|
+
|
|
861
|
+
it("a compaction failure resumes with a warning", async () => {
|
|
862
|
+
const harness = await makeHarness({ registry: compactRegistry(0.1) });
|
|
863
|
+
await completeFirst(harness);
|
|
864
|
+
await harness.settle();
|
|
865
|
+
|
|
866
|
+
await harness.compactFailed(false);
|
|
867
|
+
harness.compacts[0]?.onError?.(new Error("boom"));
|
|
868
|
+
|
|
869
|
+
expect(harness.notifications).toHaveLength(1);
|
|
870
|
+
expect(harness.notifications[0]?.severity).toBe("warning");
|
|
871
|
+
expect(harness.sends).toHaveLength(1);
|
|
872
|
+
});
|
|
873
|
+
|
|
874
|
+
it("a cancelled compaction sends no resume turn", async () => {
|
|
875
|
+
const harness = await makeHarness({ registry: compactRegistry(0.1) });
|
|
876
|
+
await completeFirst(harness);
|
|
877
|
+
await harness.settle();
|
|
878
|
+
|
|
879
|
+
await harness.compactFailed(true);
|
|
880
|
+
harness.compacts[0]?.onError?.(new Error("cancelled"));
|
|
881
|
+
|
|
882
|
+
expect(harness.sends).toHaveLength(0);
|
|
883
|
+
expect(harness.notifications).toHaveLength(1);
|
|
884
|
+
expect(harness.notifications[0]?.severity).toBe("warning");
|
|
885
|
+
});
|
|
886
|
+
|
|
887
|
+
it("the resume turn is delivered once even if the completion callback fires twice", async () => {
|
|
888
|
+
const harness = await makeHarness({ registry: compactRegistry(0.1) });
|
|
889
|
+
await completeFirst(harness);
|
|
890
|
+
await harness.settle();
|
|
891
|
+
|
|
892
|
+
const onComplete = harness.compacts[0]?.onComplete;
|
|
893
|
+
onComplete?.({ summary: "summary", firstKeptEntryId: "entry-1", tokensBefore: 100 });
|
|
894
|
+
onComplete?.({ summary: "summary", firstKeptEntryId: "entry-1", tokensBefore: 100 });
|
|
895
|
+
|
|
896
|
+
expect(harness.sends).toHaveLength(1);
|
|
897
|
+
});
|
|
898
|
+
});
|