ceulen 0.29.2 → 0.31.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/CHANGELOG.md +88 -0
- package/extensions/lib/test/smoke-install.test.sh +103 -0
- package/extensions/modules/repair/configPanel.ts +32 -2
- package/extensions/modules/repair/index.ts +126 -0
- package/extensions/modules/repair/lib/clock.ts +106 -0
- package/extensions/modules/repair/lib/repeat-reminder.ts +90 -0
- package/extensions/modules/repair/lib/settings.ts +22 -1
- package/extensions/modules/repair/lib/spill-store.ts +72 -0
- package/extensions/modules/repair/lib/spill.ts +287 -0
- package/extensions/modules/repair/test/clock.test.ts +85 -0
- package/extensions/modules/repair/test/dsh-enrichments.test.ts +375 -0
- package/extensions/modules/repair/test/repeat-reminder.test.ts +100 -0
- package/extensions/modules/repair/test/settings.test.ts +5 -1
- package/extensions/modules/repair/test/spill-utf8.test.ts +297 -0
- package/extensions/modules/repair/test/spill.test.ts +208 -0
- package/extensions/modules/steering/lib/guidance.ts +13 -3
- package/extensions/modules/steering/test/guidance.test.ts +7 -1
- package/package.json +1 -1
- package/scripts/check-provenance-fixtures.sh +7 -0
- package/scripts/smoke-install.sh +90 -0
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
// Handler-level contract for the three DSH enrichments wired into repair's
|
|
2
|
+
// `tool_result` / `before_agent_start` hooks.
|
|
3
|
+
//
|
|
4
|
+
// The exclusion cases are the point of this file. Stock read/bash truncate
|
|
5
|
+
// INSIDE the tool before `tool_result` fires, so spilling them would bound an
|
|
6
|
+
// already-bounded result and name a file that cannot restore the dropped
|
|
7
|
+
// bytes. The discriminator is METADATA, never notice text: a WRAPPED read of a
|
|
8
|
+
// file whose content contains "Use offset=" must still spill (the wrapper
|
|
9
|
+
// strips details.truncation on its own views; only the stock pass-through
|
|
10
|
+
// keeps it).
|
|
11
|
+
import assert from "node:assert/strict";
|
|
12
|
+
import { after, beforeEach, describe, it } from "node:test";
|
|
13
|
+
import { mkdtemp, readFile, rm, writeFile, mkdir } from "node:fs/promises";
|
|
14
|
+
import { existsSync } from "node:fs";
|
|
15
|
+
import { execFileSync } from "node:child_process";
|
|
16
|
+
import { tmpdir } from "node:os";
|
|
17
|
+
import { join } from "node:path";
|
|
18
|
+
import repairModule from "../index.js";
|
|
19
|
+
import { setSpillRootForTests } from "../lib/spill-store.js";
|
|
20
|
+
import { estimateTokens } from "../lib/spill.js";
|
|
21
|
+
|
|
22
|
+
const dirs: string[] = [];
|
|
23
|
+
let agentDir: string;
|
|
24
|
+
let cwd: string;
|
|
25
|
+
const originalAgentDir = process.env.PI_CODING_AGENT_DIR;
|
|
26
|
+
|
|
27
|
+
async function tempDir(prefix: string): Promise<string> {
|
|
28
|
+
const dir = await mkdtemp(join(tmpdir(), prefix));
|
|
29
|
+
dirs.push(dir);
|
|
30
|
+
return dir;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Settings live in <agentDir>/settings.json; trust.json admits the project. */
|
|
34
|
+
async function configure(repair: Record<string, unknown>): Promise<void> {
|
|
35
|
+
await mkdir(agentDir, { recursive: true });
|
|
36
|
+
await writeFile(join(agentDir, "settings.json"), JSON.stringify({ repair }));
|
|
37
|
+
await writeFile(join(agentDir, "trust.json"), JSON.stringify({ [cwd]: true }));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
interface Harness {
|
|
41
|
+
fire: (name: string, event: unknown) => Promise<any>;
|
|
42
|
+
ctx: any;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function createHarness(): Harness {
|
|
46
|
+
const handlers = new Map<string, (event: unknown, ctx: unknown) => unknown>();
|
|
47
|
+
const pi = {
|
|
48
|
+
on: (n: string, h: any) => { handlers.set(n, h); },
|
|
49
|
+
registerTool: () => {},
|
|
50
|
+
registerCommand: () => {},
|
|
51
|
+
};
|
|
52
|
+
repairModule(pi as never);
|
|
53
|
+
const ctx = {
|
|
54
|
+
cwd,
|
|
55
|
+
ui: { notify: () => {}, setStatus: () => {} },
|
|
56
|
+
sessionManager: { getSessionId: () => "session-1" },
|
|
57
|
+
isProjectTrusted: () => true,
|
|
58
|
+
};
|
|
59
|
+
return {
|
|
60
|
+
ctx,
|
|
61
|
+
fire: async (name, event) => {
|
|
62
|
+
const handler = handlers.get(name);
|
|
63
|
+
assert.ok(handler, `no handler for ${name}`);
|
|
64
|
+
return handler(event as never, ctx);
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** A text result event as pi delivers it. */
|
|
70
|
+
function resultEvent(overrides: Record<string, unknown>) {
|
|
71
|
+
return {
|
|
72
|
+
type: "tool_result",
|
|
73
|
+
toolName: "web_extract",
|
|
74
|
+
toolCallId: "call-1",
|
|
75
|
+
input: {},
|
|
76
|
+
content: [{ type: "text", text: "" }],
|
|
77
|
+
isError: false,
|
|
78
|
+
...overrides,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const BIG = "line of text\n".repeat(6000); // ~78KB → well over 12.5K tokens
|
|
83
|
+
|
|
84
|
+
let h: Harness;
|
|
85
|
+
|
|
86
|
+
beforeEach(async () => {
|
|
87
|
+
agentDir = await tempDir("repair-dsh-agent-");
|
|
88
|
+
cwd = await tempDir("repair-dsh-cwd-");
|
|
89
|
+
process.env.PI_CODING_AGENT_DIR = agentDir;
|
|
90
|
+
process.env.TMPDIR = await tempDir("repair-dsh-tmp-");
|
|
91
|
+
await configure({ spillMaxTokens: 12_500, repeatReminder: true, clock: false });
|
|
92
|
+
h = createHarness();
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
after(async () => {
|
|
96
|
+
if (originalAgentDir === undefined) delete process.env.PI_CODING_AGENT_DIR;
|
|
97
|
+
else process.env.PI_CODING_AGENT_DIR = originalAgentDir;
|
|
98
|
+
await Promise.all(dirs.map((d) => rm(d, { recursive: true, force: true })));
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
const textOf = (out: any): string =>
|
|
102
|
+
(out?.content ?? []).filter((b: any) => b.type === "text").map((b: any) => b.text).join("\n");
|
|
103
|
+
|
|
104
|
+
describe("spill", () => {
|
|
105
|
+
it("bounds an oversized EXTENSION-tool result and names a retrievable file", async () => {
|
|
106
|
+
const out = await h.fire("tool_result", resultEvent({ content: [{ type: "text", text: BIG }] }));
|
|
107
|
+
const text = textOf(out);
|
|
108
|
+
assert.match(text, /bytes omitted/);
|
|
109
|
+
const path = text.match(/Full output saved to: (\S+)/)?.[1];
|
|
110
|
+
assert.ok(path, "notice names a path");
|
|
111
|
+
assert.ok(existsSync(path!), "the spill file exists");
|
|
112
|
+
// The full text is recoverable — that is the whole point.
|
|
113
|
+
assert.equal(await readFile(path!, "utf8"), BIG);
|
|
114
|
+
assert.ok(text.startsWith(BIG.slice(0, 40)), "head retained");
|
|
115
|
+
assert.match(text, /Retrieve it in <=1900-line, ~\d+-byte UTF-8-safe pages with bash/, "recovery advice is last");
|
|
116
|
+
assert.ok(text.trimEnd().endsWith("_ OFFSET"), "notice is last and carries the offset argument");
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("leaves a result under budget untouched", async () => {
|
|
120
|
+
assert.equal(await h.fire("tool_result", resultEvent({ content: [{ type: "text", text: "short" }] })), undefined);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("does nothing when spill is disabled (0)", async () => {
|
|
124
|
+
await configure({ spillMaxTokens: 0 });
|
|
125
|
+
h = createHarness();
|
|
126
|
+
assert.equal(await h.fire("tool_result", resultEvent({ content: [{ type: "text", text: BIG }] })), undefined);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("skips a STOCK-truncated read (bytes already gone; offset pagination is its recovery)", async () => {
|
|
130
|
+
const out = await h.fire("tool_result", resultEvent({
|
|
131
|
+
toolName: "read",
|
|
132
|
+
content: [{ type: "text", text: BIG }],
|
|
133
|
+
details: { truncation: { truncated: true, truncatedBy: "bytes" } },
|
|
134
|
+
}));
|
|
135
|
+
assert.equal(out, undefined);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("skips self-truncated bash (fullOutputPath is its recovery)", async () => {
|
|
139
|
+
const out = await h.fire("tool_result", resultEvent({
|
|
140
|
+
toolName: "bash",
|
|
141
|
+
content: [{ type: "text", text: BIG }],
|
|
142
|
+
details: { fullOutputPath: "/tmp/full.txt" },
|
|
143
|
+
}));
|
|
144
|
+
assert.equal(out, undefined);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("skips codemode, which self-truncates at 10k tokens and sets fullOutputPath", async () => {
|
|
148
|
+
// Live herdr finding: codemode's own budget (execute.js:248,
|
|
149
|
+
// DEFAULT_MAX_OUTPUT_TOKENS 10000) lands at ~10,061 est tokens — under the
|
|
150
|
+
// 12,500 spill budget — so it can never reach the seam oversized. The
|
|
151
|
+
// generic fullOutputPath check is what makes that explicit rather than
|
|
152
|
+
// accidental, and it keeps a future budget change from double-spilling.
|
|
153
|
+
const out = await h.fire("tool_result", resultEvent({
|
|
154
|
+
toolName: "codemode",
|
|
155
|
+
content: [{ type: "text", text: BIG }],
|
|
156
|
+
details: { fullOutputPath: "/tmp/pi-codemode-x.txt" },
|
|
157
|
+
}));
|
|
158
|
+
assert.equal(out, undefined);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it("SPILLS a wrapped read view with NO truncation metadata (archive member / multi-range)", async () => {
|
|
162
|
+
// The wrapper builds these results itself and sets details: undefined.
|
|
163
|
+
const out = await h.fire("tool_result", resultEvent({
|
|
164
|
+
toolName: "read",
|
|
165
|
+
content: [{ type: "text", text: BIG }],
|
|
166
|
+
details: undefined,
|
|
167
|
+
}));
|
|
168
|
+
assert.match(textOf(out), /bytes omitted/);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it("SPILLS a wrapped read whose TEXT merely contains the stock notice phrase", async () => {
|
|
172
|
+
// The advisor regression: text matching would wrongly suppress this.
|
|
173
|
+
// A wrapped view of pi's own read.js (or this repo's AGENTS.md) contains
|
|
174
|
+
// "Use offset=" verbatim while being completely unbounded.
|
|
175
|
+
const content = `${"\n[Showing lines 1-2000 of 99999. Use offset=2001 to continue.]".repeat(2000)}`;
|
|
176
|
+
const out = await h.fire("tool_result", resultEvent({
|
|
177
|
+
toolName: "read",
|
|
178
|
+
content: [{ type: "text", text: content }],
|
|
179
|
+
details: undefined,
|
|
180
|
+
}));
|
|
181
|
+
assert.ok(out, "a wrapped view containing the phrase must still spill");
|
|
182
|
+
assert.match(textOf(out), /bytes omitted/);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it("skips image-bearing results (fail-open: no image token calculator here)", async () => {
|
|
186
|
+
const out = await h.fire("tool_result", resultEvent({
|
|
187
|
+
toolName: "read",
|
|
188
|
+
content: [{ type: "text", text: BIG }, { type: "image", data: "x", mimeType: "image/png" }],
|
|
189
|
+
}));
|
|
190
|
+
assert.equal(out, undefined);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it("fails open when the store is unwritable", async () => {
|
|
194
|
+
setSpillRootForTests("/dev/null/nope");
|
|
195
|
+
try {
|
|
196
|
+
const out = await h.fire("tool_result", resultEvent({ content: [{ type: "text", text: BIG }] }));
|
|
197
|
+
assert.equal(out, undefined, "the model keeps its result");
|
|
198
|
+
} finally {
|
|
199
|
+
setSpillRootForTests(undefined);
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("sizes the long-line recovery hint from the ACTIVE budget, not a constant", async () => {
|
|
204
|
+
// A hardcoded `head -c 50000` would re-spill (12,504 est tokens > 12,500).
|
|
205
|
+
// Lowering the budget must shrink the chunk the model is told to use.
|
|
206
|
+
const singleLine = "y".repeat(200_000);
|
|
207
|
+
const atDefault = await h.fire("tool_result", resultEvent({
|
|
208
|
+
toolName: "web_extract", content: [{ type: "text", text: singleLine }],
|
|
209
|
+
}));
|
|
210
|
+
const defaultChunk = Number(textOf(atDefault).match(/~(\d+)-byte UTF-8-safe pages/)?.[1]);
|
|
211
|
+
assert.ok(defaultChunk > 0 && defaultChunk < 50_000, `default chunk ${defaultChunk} must be under 50KB`);
|
|
212
|
+
|
|
213
|
+
await configure({ spillMaxTokens: 2_000 });
|
|
214
|
+
h = createHarness();
|
|
215
|
+
const atLow = await h.fire("tool_result", resultEvent({
|
|
216
|
+
toolName: "web_extract", content: [{ type: "text", text: singleLine }],
|
|
217
|
+
}));
|
|
218
|
+
const lowChunk = Number(textOf(atLow).match(/~(\d+)-byte UTF-8-safe pages/)?.[1]);
|
|
219
|
+
assert.ok(lowChunk > 0 && lowChunk < defaultChunk, `lower budget must shrink the chunk (${lowChunk} < ${defaultChunk})`);
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
// The recovery must TERMINATE: fetching a spilled result can never itself
|
|
223
|
+
// spill. These execute the emitted command rather than trusting its text.
|
|
224
|
+
for (const budget of [2_000, 12_500]) {
|
|
225
|
+
it(`recovery pages under budget and cannot re-spill (budget ${budget})`, async () => {
|
|
226
|
+
await configure({ spillMaxTokens: budget });
|
|
227
|
+
h = createHarness();
|
|
228
|
+
|
|
229
|
+
// A payload that FITS pi's read caps (under 50KB / 2000 lines) while
|
|
230
|
+
// exceeding the active budget — exactly the case where "use read" used to
|
|
231
|
+
// look safe. Only reachable at a LOWERED budget: pi's own 50KB read cap
|
|
232
|
+
// already estimates to 12,804 tokens, so at the 12,500 default NO
|
|
233
|
+
// read-cap-sized short-line payload can be under budget — which is itself
|
|
234
|
+
// the proof that read can never be a safe recommendation.
|
|
235
|
+
// At 2,000 this is the advisor's case: 9,000 chars / 100 lines, 2,254 tok.
|
|
236
|
+
const fitsReadCaps = budget < 12_500;
|
|
237
|
+
const lines = fitsReadCaps ? 100 : 2_500;
|
|
238
|
+
const payload = `${"x".repeat(89)}\n`.repeat(lines);
|
|
239
|
+
if (fitsReadCaps) {
|
|
240
|
+
assert.ok(payload.length < 50 * 1024, "fixture stays under pi's read byte cap");
|
|
241
|
+
assert.ok(lines < 2_000, "fixture stays under pi's read line cap");
|
|
242
|
+
}
|
|
243
|
+
assert.ok(estimateTokens([{ type: "text", text: payload }]) > budget, "fixture exceeds the budget");
|
|
244
|
+
const out = await h.fire("tool_result", resultEvent({
|
|
245
|
+
toolName: "web_extract", content: [{ type: "text", text: payload }],
|
|
246
|
+
}));
|
|
247
|
+
const notice = textOf(out);
|
|
248
|
+
const path = notice.match(/Full output saved to: (\S+)/)?.[1];
|
|
249
|
+
const chunk = Number(notice.match(/~(\d+)-byte UTF-8-safe pages/)?.[1]);
|
|
250
|
+
assert.ok(path && chunk > 0, "notice names a path and a chunk size");
|
|
251
|
+
assert.doesNotMatch(notice, /Use read with that path/, "never sends the model to read");
|
|
252
|
+
|
|
253
|
+
// Execute the notice's OWN command verbatim (offset 1), not a
|
|
254
|
+
// hand-written equivalent — the emitted template is what a model runs,
|
|
255
|
+
// so testing anything else would miss a broken hint.
|
|
256
|
+
const template = notice.slice(notice.indexOf("bash -c ")).trim();
|
|
257
|
+
const raw = execFileSync("/bin/sh", ["-c", template.replace("_ OFFSET", "_ 1")], {
|
|
258
|
+
encoding: "utf8", maxBuffer: 32 << 20,
|
|
259
|
+
});
|
|
260
|
+
assert.match(raw, /\[next OFFSET = \d+\]\n$/, "the page reports its own next offset");
|
|
261
|
+
const page = raw.slice(0, raw.lastIndexOf("[next OFFSET"));
|
|
262
|
+
|
|
263
|
+
assert.ok(page.length > 0, "the recovery returns real content");
|
|
264
|
+
const pageCost = estimateTokens([{ type: "text", text: page }]);
|
|
265
|
+
assert.ok(pageCost <= budget, `recovered ${pageCost} tokens must fit the ${budget} budget — else it re-spills`);
|
|
266
|
+
|
|
267
|
+
// Feed the recovered page back through the seam: it must not re-spill.
|
|
268
|
+
const echo = await h.fire("tool_result", resultEvent({
|
|
269
|
+
toolName: "bash", content: [{ type: "text", text: page }],
|
|
270
|
+
}));
|
|
271
|
+
assert.equal(echo, undefined, "a recovered page must not re-spill");
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
describe("repeat-tool-reminder", () => {
|
|
277
|
+
const call = (overrides: Record<string, unknown> = {}) =>
|
|
278
|
+
h.fire("tool_result", resultEvent({ toolName: "grep", input: { pattern: "TODO" }, content: [{ type: "text", text: "hit" }], ...overrides }));
|
|
279
|
+
|
|
280
|
+
it("appends a gentle footer on the 3rd identical call, not before", async () => {
|
|
281
|
+
assert.equal(await call(), undefined);
|
|
282
|
+
assert.equal(await call(), undefined);
|
|
283
|
+
const third = await call();
|
|
284
|
+
assert.match(textOf(third), /\[ceulen\] Same tool call repeated/);
|
|
285
|
+
assert.match(textOf(third), /^hit\n\n\[ceulen\]/, "the real result survives the footer");
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
it("escalates at the 5th call", async () => {
|
|
289
|
+
await call(); await call(); await call(); await call();
|
|
290
|
+
const fifth = await call();
|
|
291
|
+
assert.match(textOf(fifth), /consecutive_calls: 5/);
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
it("ignores property order in arguments", async () => {
|
|
295
|
+
// Identical arguments, different key order — must still form a chain.
|
|
296
|
+
await h.fire("tool_result", resultEvent({ toolName: "read", input: { path: "a", limit: 5 }, content: [{ type: "text", text: "x" }] }));
|
|
297
|
+
await h.fire("tool_result", resultEvent({ toolName: "read", input: { limit: 5, path: "a" }, content: [{ type: "text", text: "x" }] }));
|
|
298
|
+
const third = await h.fire("tool_result", resultEvent({ toolName: "read", input: { path: "a", limit: 5 }, content: [{ type: "text", text: "x" }] }));
|
|
299
|
+
assert.match(textOf(third), /Same tool call repeated/);
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
it("resets on a new user message", async () => {
|
|
303
|
+
await call(); await call();
|
|
304
|
+
await h.fire("before_agent_start", { type: "before_agent_start", messageType: "user" });
|
|
305
|
+
assert.equal(await call(), undefined, "chain restarted from 1");
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
it("does not reset on a non-user run (cron / follow-up)", async () => {
|
|
309
|
+
await call(); await call();
|
|
310
|
+
await h.fire("before_agent_start", { type: "before_agent_start", messageType: "custom" });
|
|
311
|
+
const third = await call();
|
|
312
|
+
assert.match(textOf(third), /Same tool call repeated/);
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
it("skips nested calls", async () => {
|
|
316
|
+
for (let i = 0; i < 3; i++) {
|
|
317
|
+
const out = await h.fire("tool_result", resultEvent({
|
|
318
|
+
toolName: "grep", input: { pattern: "TODO" }, content: [{ type: "text", text: "hit" }], parentToolCallId: "parent-1",
|
|
319
|
+
}));
|
|
320
|
+
assert.equal(out, undefined);
|
|
321
|
+
}
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
it("never advises on munin tools", async () => {
|
|
325
|
+
for (let i = 0; i < 4; i++) {
|
|
326
|
+
const out = await h.fire("tool_result", resultEvent({
|
|
327
|
+
toolName: "munin_search", input: { query: "x" }, content: [{ type: "text", text: "hit" }],
|
|
328
|
+
}));
|
|
329
|
+
assert.equal(out, undefined);
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
it("is off when the setting is off", async () => {
|
|
334
|
+
await configure({ repeatReminder: false });
|
|
335
|
+
h = createHarness();
|
|
336
|
+
await call(); await call();
|
|
337
|
+
assert.equal(await call(), undefined);
|
|
338
|
+
});
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
describe("clock", () => {
|
|
342
|
+
it("does not inject when off (default)", async () => {
|
|
343
|
+
assert.equal(await h.fire("before_agent_start", { type: "before_agent_start", messageType: "user" }), undefined);
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
it("injects a ceulen-clock custom message when on", async () => {
|
|
347
|
+
await configure({ clock: true, clockRefreshMs: 600_000 });
|
|
348
|
+
h = createHarness();
|
|
349
|
+
const out = await h.fire("before_agent_start", { type: "before_agent_start", messageType: "user" });
|
|
350
|
+
assert.equal(out?.message?.customType, "ceulen-clock");
|
|
351
|
+
assert.equal(out?.message?.display, false, "never rendered in the transcript");
|
|
352
|
+
assert.match(out?.message?.content, /^Time: \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-]\d{2}:\d{2} \(/);
|
|
353
|
+
assert.match(out?.message?.content, /Elapsed since last activity: unavailable/);
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
it("suppresses a second injection inside the refresh window, then allows it after", async () => {
|
|
357
|
+
await configure({ clock: true, clockRefreshMs: 600_000 });
|
|
358
|
+
h = createHarness();
|
|
359
|
+
assert.ok(await h.fire("before_agent_start", { type: "before_agent_start", messageType: "user" }));
|
|
360
|
+
assert.equal(await h.fire("before_agent_start", { type: "before_agent_start", messageType: "user" }), undefined);
|
|
361
|
+
|
|
362
|
+
await configure({ clock: true, clockRefreshMs: 0 });
|
|
363
|
+
h = createHarness();
|
|
364
|
+
assert.ok(await h.fire("before_agent_start", { type: "before_agent_start", messageType: "user" }));
|
|
365
|
+
assert.ok(await h.fire("before_agent_start", { type: "before_agent_start", messageType: "user" }), "0 = every step");
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
it("reports the elapsed span from the previous activity", async () => {
|
|
369
|
+
await configure({ clock: true, clockRefreshMs: 0 });
|
|
370
|
+
h = createHarness();
|
|
371
|
+
await h.fire("before_agent_start", { type: "before_agent_start", messageType: "user" });
|
|
372
|
+
const second = await h.fire("before_agent_start", { type: "before_agent_start", messageType: "user" });
|
|
373
|
+
assert.match(second?.message?.content, /Elapsed since last activity: \d+s/);
|
|
374
|
+
});
|
|
375
|
+
});
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// Repeat-tool-reminder: canonicalization, thresholds, and the chain contract.
|
|
2
|
+
//
|
|
3
|
+
// DSH parity points worth pinning: deep key-sort makes property order
|
|
4
|
+
// irrelevant; only EXACT repeats count; nested calls are skipped; a new user
|
|
5
|
+
// message resets the chain; munin tools are excluded (their own dampener owns
|
|
6
|
+
// them, and it BLOCKS where this only advises).
|
|
7
|
+
import assert from "node:assert/strict";
|
|
8
|
+
import { describe, it } from "node:test";
|
|
9
|
+
import {
|
|
10
|
+
ARGUMENTS_PREVIEW_CHARS,
|
|
11
|
+
REPEAT_THRESHOLDS,
|
|
12
|
+
advanceChain,
|
|
13
|
+
callKey,
|
|
14
|
+
canonicalizeArguments,
|
|
15
|
+
isTrackedTool,
|
|
16
|
+
reminderFor,
|
|
17
|
+
sortJsonValue,
|
|
18
|
+
} from "../lib/repeat-reminder.js";
|
|
19
|
+
|
|
20
|
+
describe("canonicalization", () => {
|
|
21
|
+
it("ignores property order at every depth", () => {
|
|
22
|
+
const a = { b: 1, a: { d: 2, c: [3, { f: 4, e: 5 }] } };
|
|
23
|
+
const b = { a: { c: [3, { e: 5, f: 4 }], d: 2 }, b: 1 };
|
|
24
|
+
assert.equal(canonicalizeArguments(a), canonicalizeArguments(b));
|
|
25
|
+
assert.equal(callKey("grep", a), callKey("grep", b));
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("keeps array order significant (it is semantic)", () => {
|
|
29
|
+
assert.notEqual(canonicalizeArguments([1, 2]), canonicalizeArguments([2, 1]));
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("distinguishes different tools with identical arguments", () => {
|
|
33
|
+
assert.notEqual(callKey("read", { path: "a" }), callKey("grep", { path: "a" }));
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("sorts nested objects without dropping values", () => {
|
|
37
|
+
assert.deepEqual(sortJsonValue({ z: { b: 1, a: 2 }, y: null }), { y: null, z: { a: 2, b: 1 } });
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
describe("chain", () => {
|
|
42
|
+
it("fires the gentle reminder on the first threshold only", () => {
|
|
43
|
+
let chain;
|
|
44
|
+
const footers: string[] = [];
|
|
45
|
+
for (let i = 1; i <= 3; i++) {
|
|
46
|
+
const out = advanceChain(chain, "bash", { command: "ls" });
|
|
47
|
+
assert.ok(out);
|
|
48
|
+
chain = out.chain;
|
|
49
|
+
if (out.footer) footers.push(out.footer);
|
|
50
|
+
}
|
|
51
|
+
assert.equal(footers.length, 1);
|
|
52
|
+
assert.match(footers[0]!, /Same tool call repeated/);
|
|
53
|
+
assert.doesNotMatch(footers[0]!, /consecutive_calls/);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("escalates to the detailed reminder at the later thresholds", () => {
|
|
57
|
+
let chain;
|
|
58
|
+
const seen: number[] = [];
|
|
59
|
+
for (let i = 1; i <= REPEAT_THRESHOLDS[REPEAT_THRESHOLDS.length - 1]; i++) {
|
|
60
|
+
const out = advanceChain(chain, "grep", { pattern: "x" });
|
|
61
|
+
assert.ok(out);
|
|
62
|
+
chain = out.chain;
|
|
63
|
+
if (out.footer) seen.push(i);
|
|
64
|
+
}
|
|
65
|
+
assert.deepEqual(seen, [...REPEAT_THRESHOLDS]);
|
|
66
|
+
const detailed = reminderFor("grep", 5, '{"pattern":"x"}');
|
|
67
|
+
assert.match(detailed!, /consecutive_calls: 5/);
|
|
68
|
+
assert.match(detailed!, /tool: grep/);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("resets when a different call intervenes", () => {
|
|
72
|
+
const first = advanceChain(undefined, "bash", { command: "ls" })!;
|
|
73
|
+
const second = advanceChain(first.chain, "bash", { command: "pwd" })!;
|
|
74
|
+
assert.equal(second.chain.count, 1, "a different call restarts the chain");
|
|
75
|
+
// Returning to the original call counts from 1 again, not 3.
|
|
76
|
+
const third = advanceChain(second.chain, "bash", { command: "pwd" })!;
|
|
77
|
+
assert.equal(third.chain.count, 2);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("excludes munin tools (their dampener owns them)", () => {
|
|
81
|
+
assert.equal(isTrackedTool("munin_search"), false);
|
|
82
|
+
assert.equal(isTrackedTool("munin_get"), false);
|
|
83
|
+
assert.equal(isTrackedTool("bash"), true);
|
|
84
|
+
assert.equal(advanceChain(undefined, "munin_search", { query: "x" }), undefined);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("caps the quoted arguments", () => {
|
|
88
|
+
const huge = JSON.stringify({ body: "y".repeat(2000) });
|
|
89
|
+
const footer = reminderFor("write", 5, huge)!;
|
|
90
|
+
assert.ok(footer.includes(`+${huge.length - ARGUMENTS_PREVIEW_CHARS} more chars`));
|
|
91
|
+
assert.ok(footer.length < huge.length);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("does not fire on non-threshold counts", () => {
|
|
95
|
+
assert.equal(reminderFor("bash", 1, "{}"), undefined);
|
|
96
|
+
assert.equal(reminderFor("bash", 2, "{}"), undefined);
|
|
97
|
+
assert.equal(reminderFor("bash", 4, "{}"), undefined);
|
|
98
|
+
assert.equal(reminderFor("bash", 9, "{}"), undefined);
|
|
99
|
+
});
|
|
100
|
+
});
|
|
@@ -175,7 +175,7 @@ describe("tool_call honors the trusted-project layer (A5 e2e)", () => {
|
|
|
175
175
|
});
|
|
176
176
|
|
|
177
177
|
describe("buildRepairGroups (panel contract)", () => {
|
|
178
|
-
it("renders the
|
|
178
|
+
it("renders the eleven repair.* rows on the Tools tab with the documented defaults", () => {
|
|
179
179
|
const cfg: RepairSettings = { ...DEFAULT_REPAIR_SETTINGS };
|
|
180
180
|
const groups = buildRepairGroups(cfg);
|
|
181
181
|
assert.equal(groups.length, 1);
|
|
@@ -190,6 +190,10 @@ describe("buildRepairGroups (panel contract)", () => {
|
|
|
190
190
|
"repair.autoBg",
|
|
191
191
|
"repair.autoBgSecs",
|
|
192
192
|
"repair.persistentBash",
|
|
193
|
+
"repair.spillMaxTokens",
|
|
194
|
+
"repair.repeatReminder",
|
|
195
|
+
"repair.clock",
|
|
196
|
+
"repair.clockRefreshMs",
|
|
193
197
|
]);
|
|
194
198
|
assert.ok(keys.every((k) => k.startsWith("repair.")), "row keys route back to this module");
|
|
195
199
|
const byKey = new Map(groups[0].rows.map((r) => [r.key, r]));
|