@polderlabs/bizar-plugin 0.5.4
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/LICENSE +21 -0
- package/README.md +448 -0
- package/bun.lock +88 -0
- package/index.ts +1113 -0
- package/package.json +42 -0
- package/scripts/check-forbidden-imports.sh +33 -0
- package/src/background-state.ts +463 -0
- package/src/background.ts +964 -0
- package/src/commands-impl.ts +369 -0
- package/src/commands.ts +880 -0
- package/src/event-stream.ts +574 -0
- package/src/fingerprint.ts +120 -0
- package/src/handoff.ts +79 -0
- package/src/http-client.ts +467 -0
- package/src/logger.ts +144 -0
- package/src/loop.ts +176 -0
- package/src/options.ts +421 -0
- package/src/plan-fs.ts +323 -0
- package/src/report.ts +178 -0
- package/src/research-prompt.ts +35 -0
- package/src/serve.ts +476 -0
- package/src/settings.ts +349 -0
- package/src/state.ts +298 -0
- package/src/tools/bg-collect.ts +104 -0
- package/src/tools/bg-get-comments.ts +239 -0
- package/src/tools/bg-kill.ts +87 -0
- package/src/tools/bg-spawn.ts +263 -0
- package/src/tools/bg-status.ts +99 -0
- package/src/tools/plan-action.ts +767 -0
- package/src/tools/wait-for-feedback.ts +402 -0
- package/tests/attach-handler-bug.test.ts +166 -0
- package/tests/background-state.test.ts +277 -0
- package/tests/background.test.ts +402 -0
- package/tests/block.test.ts +193 -0
- package/tests/canonical-key-order.test.ts +71 -0
- package/tests/commands-impl.test.ts +442 -0
- package/tests/commands.test.ts +548 -0
- package/tests/config.test.ts +122 -0
- package/tests/dispose.test.ts +336 -0
- package/tests/event-stream.test.ts +409 -0
- package/tests/event.test.ts +262 -0
- package/tests/fingerprint.test.ts +161 -0
- package/tests/http-client.test.ts +403 -0
- package/tests/init-helpers.test.ts +203 -0
- package/tests/integration/slash-command.test.ts +348 -0
- package/tests/integration/tool-routing.test.ts +314 -0
- package/tests/loop.test.ts +397 -0
- package/tests/options.test.ts +274 -0
- package/tests/serve.test.ts +335 -0
- package/tests/settings.test.ts +351 -0
- package/tests/stall-think.test.ts +749 -0
- package/tests/state.test.ts +275 -0
- package/tests/tools/bg-collect.test.ts +337 -0
- package/tests/tools/bg-get-comments.test.ts +485 -0
- package/tests/tools/bg-kill.test.ts +231 -0
- package/tests/tools/bg-spawn.test.ts +311 -0
- package/tests/tools/bg-status.test.ts +216 -0
- package/tests/tools/plan-action.test.ts +599 -0
- package/tests/tools/wait-for-feedback.test.ts +390 -0
- package/tsconfig.json +29 -0
|
@@ -0,0 +1,397 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Loop decision-tree tests.
|
|
3
|
+
*
|
|
4
|
+
* Spec contract (tests/loop.test.ts per §12.1):
|
|
5
|
+
* - Threshold-3 band returns `warn` (caller will log only — no injection).
|
|
6
|
+
* - Threshold-5 band returns `warn` with the inject-warn handoff message.
|
|
7
|
+
* - Threshold-8 band returns `escalate` with the inject-escalate message.
|
|
8
|
+
* - Threshold-12 band returns `block` with the throw message.
|
|
9
|
+
* - The throw message contains the tool name and the substring `loop`
|
|
10
|
+
* (case-insensitive) or `escalate` (spec §12.1, last bullet).
|
|
11
|
+
* - Window rolling: with `loopWindowSize = 4` and a window of
|
|
12
|
+
* `[bash X, bash X, read, bash X]`, the fingerprint for X reports
|
|
13
|
+
* 3 repetitions (spec §5.5).
|
|
14
|
+
* - Out-of-window entries do not count.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { describe, test, expect } from "bun:test";
|
|
18
|
+
|
|
19
|
+
import { decide, isLogOnlyWarn, type Decision } from "../src/loop.js";
|
|
20
|
+
import {
|
|
21
|
+
DEFAULT_OPTIONS,
|
|
22
|
+
normalizeOptions,
|
|
23
|
+
type NormalizedOptions,
|
|
24
|
+
} from "../src/options.js";
|
|
25
|
+
import {
|
|
26
|
+
warnMessage,
|
|
27
|
+
escalateMessage,
|
|
28
|
+
blockMessage,
|
|
29
|
+
CANONICAL_TEMPLATES,
|
|
30
|
+
} from "../src/handoff.js";
|
|
31
|
+
|
|
32
|
+
// --- Local re-declaration of SessionState -----------------------------------
|
|
33
|
+
// The `SessionState` type lives in `src/state.ts` (Thor's module). We
|
|
34
|
+
// re-declare the structural shape here so these tests are self-contained
|
|
35
|
+
// and do not require Thor's module to be present. The integration test
|
|
36
|
+
// pins the real interface.
|
|
37
|
+
interface ToolCall {
|
|
38
|
+
tool: string;
|
|
39
|
+
fingerprint: string;
|
|
40
|
+
at: number;
|
|
41
|
+
outcome?: "ok" | "error";
|
|
42
|
+
}
|
|
43
|
+
interface SessionState {
|
|
44
|
+
sessionId: string;
|
|
45
|
+
parentAgent: string | null;
|
|
46
|
+
startedAt: number;
|
|
47
|
+
lastActivityAt: number;
|
|
48
|
+
turnCount: number;
|
|
49
|
+
toolCalls: ToolCall[];
|
|
50
|
+
warningsIssued: number;
|
|
51
|
+
blocksTriggered: number;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function emptyState(): SessionState {
|
|
55
|
+
return {
|
|
56
|
+
sessionId: "sess-1",
|
|
57
|
+
parentAgent: "odin",
|
|
58
|
+
startedAt: 1_700_000_000_000,
|
|
59
|
+
lastActivityAt: 1_700_000_000_000,
|
|
60
|
+
turnCount: 0,
|
|
61
|
+
toolCalls: [],
|
|
62
|
+
warningsIssued: 0,
|
|
63
|
+
blocksTriggered: 0,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function call(tool: string, fingerprint: string, at: number): ToolCall {
|
|
68
|
+
return { tool, fingerprint, at };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function pushFingerprint(
|
|
72
|
+
state: SessionState,
|
|
73
|
+
tool: string,
|
|
74
|
+
fingerprint: string,
|
|
75
|
+
count: number,
|
|
76
|
+
atStart = 1_700_000_000_000,
|
|
77
|
+
): void {
|
|
78
|
+
for (let i = 0; i < count; i++) {
|
|
79
|
+
state.toolCalls.push(call(tool, fingerprint, atStart + i));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const FP = "fp:read:same";
|
|
84
|
+
const TOOL = "read";
|
|
85
|
+
const NOW = 1_700_000_001_000;
|
|
86
|
+
|
|
87
|
+
describe("decide() — canonical threshold table (spec §5.4)", () => {
|
|
88
|
+
test("count < 3 (default warn-2) returns allow", () => {
|
|
89
|
+
const state = emptyState();
|
|
90
|
+
pushFingerprint(state, TOOL, FP, 2); // count = 2
|
|
91
|
+
const d = decide(state, FP, NOW, DEFAULT_OPTIONS);
|
|
92
|
+
expect(d).toEqual({ action: "allow" });
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test("count = 3 (log-only band) returns warn with canonical warn message", () => {
|
|
96
|
+
const state = emptyState();
|
|
97
|
+
pushFingerprint(state, TOOL, FP, 3);
|
|
98
|
+
const d = decide(state, FP, NOW, DEFAULT_OPTIONS);
|
|
99
|
+
expect(d.action).toBe("warn");
|
|
100
|
+
if (d.action === "warn") {
|
|
101
|
+
expect(d.count).toBe(3);
|
|
102
|
+
expect(d.fingerprint).toBe(FP);
|
|
103
|
+
expect(d.reason).toBe(warnMessage(TOOL));
|
|
104
|
+
expect(isLogOnlyWarn(d, DEFAULT_OPTIONS)).toBe(true);
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test("count = 4 (still log-only band) returns warn with log-only flag", () => {
|
|
109
|
+
const state = emptyState();
|
|
110
|
+
pushFingerprint(state, TOOL, FP, 4);
|
|
111
|
+
const d = decide(state, FP, NOW, DEFAULT_OPTIONS);
|
|
112
|
+
expect(d.action).toBe("warn");
|
|
113
|
+
if (d.action === "warn") {
|
|
114
|
+
expect(d.count).toBe(4);
|
|
115
|
+
expect(isLogOnlyWarn(d, DEFAULT_OPTIONS)).toBe(true);
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
test("count = 5 (inject-warn band) returns warn with inject flag", () => {
|
|
120
|
+
const state = emptyState();
|
|
121
|
+
pushFingerprint(state, TOOL, FP, 5);
|
|
122
|
+
const d = decide(state, FP, NOW, DEFAULT_OPTIONS);
|
|
123
|
+
expect(d.action).toBe("warn");
|
|
124
|
+
if (d.action === "warn") {
|
|
125
|
+
expect(d.count).toBe(5);
|
|
126
|
+
expect(d.reason).toBe(warnMessage(TOOL));
|
|
127
|
+
expect(isLogOnlyWarn(d, DEFAULT_OPTIONS)).toBe(false);
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
test("count = 7 (still warn band) returns warn", () => {
|
|
132
|
+
const state = emptyState();
|
|
133
|
+
pushFingerprint(state, TOOL, FP, 7);
|
|
134
|
+
const d = decide(state, FP, NOW, DEFAULT_OPTIONS);
|
|
135
|
+
expect(d.action).toBe("warn");
|
|
136
|
+
if (d.action === "warn") {
|
|
137
|
+
expect(d.count).toBe(7);
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
test("count = 8 (escalate band) returns escalate with canonical escalate message", () => {
|
|
142
|
+
const state = emptyState();
|
|
143
|
+
pushFingerprint(state, TOOL, FP, 8);
|
|
144
|
+
const d = decide(state, FP, NOW, DEFAULT_OPTIONS);
|
|
145
|
+
expect(d.action).toBe("escalate");
|
|
146
|
+
if (d.action === "escalate") {
|
|
147
|
+
expect(d.count).toBe(8);
|
|
148
|
+
expect(d.fingerprint).toBe(FP);
|
|
149
|
+
expect(d.reason).toBe(escalateMessage(TOOL));
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test("count = 11 (still escalate band) returns escalate", () => {
|
|
154
|
+
// Use a window wide enough that 11 entries can all fit; with the
|
|
155
|
+
// default window of 10, the count is bounded by the window size.
|
|
156
|
+
const opts: NormalizedOptions = { ...DEFAULT_OPTIONS, loopWindowSize: 15 };
|
|
157
|
+
const state = emptyState();
|
|
158
|
+
pushFingerprint(state, TOOL, FP, 11);
|
|
159
|
+
const d = decide(state, FP, NOW, opts);
|
|
160
|
+
expect(d.action).toBe("escalate");
|
|
161
|
+
if (d.action === "escalate") {
|
|
162
|
+
expect(d.count).toBe(11);
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
test("count = 12 (block band) returns block with canonical block message", () => {
|
|
167
|
+
// Use a window wide enough that 12 entries can all fit; with the
|
|
168
|
+
// default window of 10, the count is bounded by the window size
|
|
169
|
+
// and the block band becomes unreachable. This is a known spec
|
|
170
|
+
// limitation (see README "Limitations" §13 #11).
|
|
171
|
+
const opts: NormalizedOptions = { ...DEFAULT_OPTIONS, loopWindowSize: 15 };
|
|
172
|
+
const state = emptyState();
|
|
173
|
+
pushFingerprint(state, TOOL, FP, 12);
|
|
174
|
+
const d = decide(state, FP, NOW, opts);
|
|
175
|
+
expect(d.action).toBe("block");
|
|
176
|
+
if (d.action === "block") {
|
|
177
|
+
expect(d.count).toBe(12);
|
|
178
|
+
expect(d.fingerprint).toBe(FP);
|
|
179
|
+
expect(d.reason).toBe(blockMessage(TOOL));
|
|
180
|
+
// Spec §12.1: the throw message contains the tool name and the
|
|
181
|
+
// substring `loop` (case-insensitive) OR `escalate`.
|
|
182
|
+
expect(d.reason).toContain(TOOL);
|
|
183
|
+
expect(/loop/i.test(d.reason) || /escalate/i.test(d.reason)).toBe(true);
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
test("count > 12 still returns block (no upper clamp)", () => {
|
|
188
|
+
const opts: NormalizedOptions = { ...DEFAULT_OPTIONS, loopWindowSize: 25 };
|
|
189
|
+
const state = emptyState();
|
|
190
|
+
pushFingerprint(state, TOOL, FP, 20);
|
|
191
|
+
const d = decide(state, FP, NOW, opts);
|
|
192
|
+
expect(d.action).toBe("block");
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
describe("decide() — window rolling (spec §5.5)", () => {
|
|
197
|
+
test("window [bash X, bash X, read, bash X] with windowSize=4 reports 3 repetitions of X", () => {
|
|
198
|
+
// Spec example. We construct a state whose toolCalls contain exactly
|
|
199
|
+
// this window, with the LAST entry being the current call we are
|
|
200
|
+
// deciding on.
|
|
201
|
+
const state = emptyState();
|
|
202
|
+
const fpX = "fp:bash:X";
|
|
203
|
+
const fpRead = "fp:read:other";
|
|
204
|
+
state.toolCalls.push(call("bash", fpX, 1));
|
|
205
|
+
state.toolCalls.push(call("bash", fpX, 2));
|
|
206
|
+
state.toolCalls.push(call("read", fpRead, 3));
|
|
207
|
+
state.toolCalls.push(call("bash", fpX, 4));
|
|
208
|
+
|
|
209
|
+
const d = decide(state, fpX, 5, {
|
|
210
|
+
...DEFAULT_OPTIONS,
|
|
211
|
+
loopWindowSize: 4,
|
|
212
|
+
});
|
|
213
|
+
// The 4th entry in the window is the current call; the other 3
|
|
214
|
+
// X-entries are previous calls. The count returned is the number
|
|
215
|
+
// of entries in the last `loopWindowSize` calls that share the
|
|
216
|
+
// fingerprint, which is 3 (the 3 bash X entries in the window).
|
|
217
|
+
expect(d.action).not.toBe("allow");
|
|
218
|
+
if (d.action !== "allow") {
|
|
219
|
+
expect(d.count).toBe(3);
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
test("out-of-window entries do not count", () => {
|
|
224
|
+
const state = emptyState();
|
|
225
|
+
const fpX = "fp:bash:X";
|
|
226
|
+
// 10 older X calls that fall OUTSIDE the window of size 10.
|
|
227
|
+
for (let i = 0; i < 10; i++) {
|
|
228
|
+
state.toolCalls.push(call("bash", fpX, 1_000 + i));
|
|
229
|
+
}
|
|
230
|
+
// 10 non-matching calls inside the window.
|
|
231
|
+
for (let i = 0; i < 10; i++) {
|
|
232
|
+
state.toolCalls.push(call("read", `fp:read:${i}`, 2_000 + i));
|
|
233
|
+
}
|
|
234
|
+
const d = decide(state, fpX, 9_000, {
|
|
235
|
+
...DEFAULT_OPTIONS,
|
|
236
|
+
loopWindowSize: 10,
|
|
237
|
+
});
|
|
238
|
+
// The last 10 entries are all `read` calls — fpX count is 0.
|
|
239
|
+
expect(d.action).toBe("allow");
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
test("intermediate non-matching calls do NOT reset the count", () => {
|
|
243
|
+
const state = emptyState();
|
|
244
|
+
const fpX = "fp:bash:X";
|
|
245
|
+
// [X, X, read, X, X] — count of X in the window of 5 is 4.
|
|
246
|
+
state.toolCalls.push(call("bash", fpX, 1));
|
|
247
|
+
state.toolCalls.push(call("bash", fpX, 2));
|
|
248
|
+
state.toolCalls.push(call("read", "fp:read:1", 3));
|
|
249
|
+
state.toolCalls.push(call("bash", fpX, 4));
|
|
250
|
+
state.toolCalls.push(call("bash", fpX, 5));
|
|
251
|
+
|
|
252
|
+
const d = decide(state, fpX, 6, {
|
|
253
|
+
...DEFAULT_OPTIONS,
|
|
254
|
+
loopWindowSize: 5,
|
|
255
|
+
});
|
|
256
|
+
expect(d.action).not.toBe("allow");
|
|
257
|
+
if (d.action !== "allow") {
|
|
258
|
+
expect(d.count).toBe(4);
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
describe("decide() — edge cases", () => {
|
|
264
|
+
test("empty state returns allow", () => {
|
|
265
|
+
const d = decide(emptyState(), FP, NOW, DEFAULT_OPTIONS);
|
|
266
|
+
expect(d.action).toBe("allow");
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
test("fingerprint not present in state returns allow", () => {
|
|
270
|
+
const state = emptyState();
|
|
271
|
+
pushFingerprint(state, "bash", "fp:bash:something", 5);
|
|
272
|
+
const d = decide(state, "fp:read:different", NOW, DEFAULT_OPTIONS);
|
|
273
|
+
expect(d.action).toBe("allow");
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
test("reason includes the tool name recovered from the window", () => {
|
|
277
|
+
const state = emptyState();
|
|
278
|
+
pushFingerprint(state, "edit", "fp:edit:foo", 5);
|
|
279
|
+
const d = decide(state, "fp:edit:foo", NOW, DEFAULT_OPTIONS);
|
|
280
|
+
expect(d.action).toBe("warn");
|
|
281
|
+
if (d.action === "warn") {
|
|
282
|
+
expect(d.reason).toContain("edit");
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
describe("decide() — custom options", () => {
|
|
288
|
+
test("user-configured warn=3 collapses the log-only band", () => {
|
|
289
|
+
// With warn=3, the log-only threshold becomes max(1, 3-2)=1, so the
|
|
290
|
+
// very first repetition already lands in the warn band (log-only
|
|
291
|
+
// sub-band). This is expected behavior; the log-only band exists
|
|
292
|
+
// to give a heads-up BEFORE the inject threshold, and a user who
|
|
293
|
+
// lowers warn shrinks or eliminates that gap.
|
|
294
|
+
const opts: NormalizedOptions = {
|
|
295
|
+
...DEFAULT_OPTIONS,
|
|
296
|
+
loopThresholdWarn: 3,
|
|
297
|
+
loopThresholdEscalate: 4,
|
|
298
|
+
loopThresholdBlock: 5,
|
|
299
|
+
};
|
|
300
|
+
const state = emptyState();
|
|
301
|
+
pushFingerprint(state, TOOL, FP, 1);
|
|
302
|
+
const d = decide(state, FP, NOW, opts);
|
|
303
|
+
// count=1 is now >= logOnly (1), so the result is "warn" in the
|
|
304
|
+
// log-only sub-band. The caller would log only (no injection)
|
|
305
|
+
// because count (1) is still below the inject-warn threshold (3).
|
|
306
|
+
expect(d.action).toBe("warn");
|
|
307
|
+
if (d.action === "warn") {
|
|
308
|
+
expect(isLogOnlyWarn(d, opts)).toBe(true);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
pushFingerprint(state, TOOL, FP, 2); // total count = 3
|
|
312
|
+
const d2 = decide(state, FP, NOW, opts);
|
|
313
|
+
expect(d2.action).toBe("warn");
|
|
314
|
+
if (d2.action === "warn") {
|
|
315
|
+
// count=3 is now >= the inject-warn threshold (3), so it is
|
|
316
|
+
// NOT a log-only warn.
|
|
317
|
+
expect(isLogOnlyWarn(d2, opts)).toBe(false);
|
|
318
|
+
}
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
test("custom window size limits how far back we scan", () => {
|
|
322
|
+
const state = emptyState();
|
|
323
|
+
const fpX = "fp:bash:X";
|
|
324
|
+
for (let i = 0; i < 5; i++) state.toolCalls.push(call("bash", fpX, 1 + i));
|
|
325
|
+
for (let i = 0; i < 5; i++) state.toolCalls.push(call("read", `fp:r:${i}`, 100 + i));
|
|
326
|
+
// Now the last 5 entries are all read; the 5 bash X entries are
|
|
327
|
+
// outside the window of size 5.
|
|
328
|
+
const d = decide(state, fpX, 1_000, {
|
|
329
|
+
...DEFAULT_OPTIONS,
|
|
330
|
+
loopWindowSize: 5,
|
|
331
|
+
});
|
|
332
|
+
expect(d.action).toBe("allow");
|
|
333
|
+
});
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
describe("isLogOnlyWarn()", () => {
|
|
337
|
+
test("returns false for allow decisions", () => {
|
|
338
|
+
expect(isLogOnlyWarn({ action: "allow" }, DEFAULT_OPTIONS)).toBe(false);
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
test("returns false for escalate decisions", () => {
|
|
342
|
+
const d: Decision = { action: "escalate", reason: "x", fingerprint: "f", count: 8 };
|
|
343
|
+
expect(isLogOnlyWarn(d, DEFAULT_OPTIONS)).toBe(false);
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
test("returns false for block decisions", () => {
|
|
347
|
+
const d: Decision = { action: "block", reason: "x", fingerprint: "f", count: 12 };
|
|
348
|
+
expect(isLogOnlyWarn(d, DEFAULT_OPTIONS)).toBe(false);
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
test("returns true for warn with count below warn threshold", () => {
|
|
352
|
+
const d: Decision = { action: "warn", reason: "x", fingerprint: "f", count: 3 };
|
|
353
|
+
expect(isLogOnlyWarn(d, DEFAULT_OPTIONS)).toBe(true);
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
test("returns false for warn with count at or above warn threshold", () => {
|
|
357
|
+
const d: Decision = { action: "warn", reason: "x", fingerprint: "f", count: 5 };
|
|
358
|
+
expect(isLogOnlyWarn(d, DEFAULT_OPTIONS)).toBe(false);
|
|
359
|
+
});
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
describe("canonical handoff templates (spec §11.2)", () => {
|
|
363
|
+
test("warn template matches the canonical text", () => {
|
|
364
|
+
expect(CANONICAL_TEMPLATES.warn).toBe(
|
|
365
|
+
"[loop guard: 5 identical calls to %TOOL%]. Consider using the task tool to report back to your parent with what you've learned and what you need.",
|
|
366
|
+
);
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
test("escalate template matches the canonical text", () => {
|
|
370
|
+
expect(CANONICAL_TEMPLATES.escalate).toBe(
|
|
371
|
+
"[loop guard: 8 identical calls to %TOOL%]. Consider using the task tool to report back to your parent with what you've learned and what you need.",
|
|
372
|
+
);
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
test("block template matches the canonical text", () => {
|
|
376
|
+
expect(CANONICAL_TEMPLATES.block).toBe(
|
|
377
|
+
"Loop protection: 12 identical calls to %TOOL%. Use task to escalate.",
|
|
378
|
+
);
|
|
379
|
+
});
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
describe("normalizeOptions() integration with decide()", () => {
|
|
383
|
+
test("clamped options feed into decide() correctly", () => {
|
|
384
|
+
const { options } = normalizeOptions({
|
|
385
|
+
loopThresholdWarn: -5,
|
|
386
|
+
loopWindowSize: 100,
|
|
387
|
+
});
|
|
388
|
+
expect(options.loopThresholdWarn).toBe(1);
|
|
389
|
+
expect(options.loopWindowSize).toBe(50);
|
|
390
|
+
|
|
391
|
+
const state = emptyState();
|
|
392
|
+
pushFingerprint(state, TOOL, FP, 1);
|
|
393
|
+
// With warn=1, the warn band starts at count >= 1.
|
|
394
|
+
const d = decide(state, FP, NOW, options);
|
|
395
|
+
expect(d.action).toBe("warn");
|
|
396
|
+
});
|
|
397
|
+
});
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import {
|
|
3
|
+
DEFAULT_OPTIONS,
|
|
4
|
+
expandHome,
|
|
5
|
+
findOffendingPath,
|
|
6
|
+
findSecretDirMatch,
|
|
7
|
+
normalizeOptions,
|
|
8
|
+
readEnvFlags,
|
|
9
|
+
type NormalizedOptions,
|
|
10
|
+
} from "../src/options";
|
|
11
|
+
|
|
12
|
+
const D = DEFAULT_OPTIONS;
|
|
13
|
+
|
|
14
|
+
// ── expandHome ────────────────────────────────────────────────────────────────
|
|
15
|
+
|
|
16
|
+
test("expandHome converts ~ to homedir", () => {
|
|
17
|
+
const home = process.env.HOME ?? "/home/test";
|
|
18
|
+
expect(expandHome("~")).toBe(home);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test("expandHome converts ~-prefixed paths", () => {
|
|
22
|
+
const home = process.env.HOME ?? "/home/test";
|
|
23
|
+
expect(expandHome("~/foo/bar")).toBe(`${home}/foo/bar`);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
test("expandHome leaves absolute paths alone", () => {
|
|
27
|
+
expect(expandHome("/absolute/path")).toBe("/absolute/path");
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
// ── normalizeOptions ──────────────────────────────────────────────────────────
|
|
31
|
+
|
|
32
|
+
describe("normalizeOptions", () => {
|
|
33
|
+
test("returns DEFAULT_OPTIONS when passed undefined", () => {
|
|
34
|
+
const { options, notes } = normalizeOptions(undefined);
|
|
35
|
+
expect(options.loopThresholdWarn).toBe(D.loopThresholdWarn);
|
|
36
|
+
expect(options.loopThresholdEscalate).toBe(D.loopThresholdEscalate);
|
|
37
|
+
expect(options.loopThresholdBlock).toBe(D.loopThresholdBlock);
|
|
38
|
+
expect(options.loopWindowSize).toBe(D.loopWindowSize);
|
|
39
|
+
expect(options.logDir).toBe(D.logDir);
|
|
40
|
+
expect(options.stateDir).toBe(D.stateDir);
|
|
41
|
+
expect(options.logRotationBytes).toBe(D.logRotationBytes);
|
|
42
|
+
expect(notes).toContain(`loopThresholdWarn defaulted to ${D.loopThresholdWarn}`);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test("returns DEFAULT_OPTIONS when passed empty object", () => {
|
|
46
|
+
const { options, notes } = normalizeOptions({});
|
|
47
|
+
expect(options.loopThresholdWarn).toBe(D.loopThresholdWarn);
|
|
48
|
+
expect(notes).toContain(`loopThresholdWarn defaulted to ${D.loopThresholdWarn}`);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("negative loopThresholdWarn is clamped to 1", () => {
|
|
52
|
+
const { options, notes } = normalizeOptions({ loopThresholdWarn: -5 });
|
|
53
|
+
expect(options.loopThresholdWarn).toBe(1);
|
|
54
|
+
expect(notes).toContain("loopThresholdWarn -5 clamped to 1");
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test("zero loopThresholdWarn is clamped to 1", () => {
|
|
58
|
+
const { options } = normalizeOptions({ loopThresholdWarn: 0 });
|
|
59
|
+
expect(options.loopThresholdWarn).toBe(1);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test("loopThresholdWarn of 3 is preserved", () => {
|
|
63
|
+
const { options } = normalizeOptions({ loopThresholdWarn: 3 });
|
|
64
|
+
expect(options.loopThresholdWarn).toBe(3);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("warn >= escalate → escalate bumped above warn", () => {
|
|
68
|
+
// warn=5, escalate=5 → escalate becomes 6
|
|
69
|
+
const { options, notes } = normalizeOptions({ loopThresholdWarn: 5, loopThresholdEscalate: 5 });
|
|
70
|
+
expect(options.loopThresholdWarn).toBe(5);
|
|
71
|
+
expect(options.loopThresholdEscalate).toBe(6);
|
|
72
|
+
expect(notes.some((n) => n.includes("adjusted from 5 to 6"))).toBe(true);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test("escalate >= block → block bumped above escalate", () => {
|
|
76
|
+
// escalate=8, block=8 → block becomes 9
|
|
77
|
+
const { options, notes } = normalizeOptions({ loopThresholdEscalate: 8, loopThresholdBlock: 8 });
|
|
78
|
+
expect(options.loopThresholdEscalate).toBe(8);
|
|
79
|
+
expect(options.loopThresholdBlock).toBe(9);
|
|
80
|
+
expect(notes.some((n) => n.includes("adjusted from 8 to 9"))).toBe(true);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test("loopWindowSize below 3 is clamped to 3", () => {
|
|
84
|
+
const { options, notes } = normalizeOptions({ loopWindowSize: 1 });
|
|
85
|
+
expect(options.loopWindowSize).toBe(3);
|
|
86
|
+
expect(notes).toContain("loopWindowSize 1 clamped to 3 (minimum)");
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test("loopWindowSize above 50 is clamped to 50", () => {
|
|
90
|
+
const { options, notes } = normalizeOptions({ loopWindowSize: 200 });
|
|
91
|
+
expect(options.loopWindowSize).toBe(50);
|
|
92
|
+
expect(notes).toContain("loopWindowSize 200 clamped to 50 (maximum)");
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test("loopWindowSize within [3,50] is preserved", () => {
|
|
96
|
+
const { options } = normalizeOptions({ loopWindowSize: 25 });
|
|
97
|
+
expect(options.loopWindowSize).toBe(25);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test("block > window + 2 → block clamped down", () => {
|
|
101
|
+
// block=50, window=10 → block must be ≤ 12
|
|
102
|
+
const { options, notes } = normalizeOptions({ loopThresholdBlock: 50, loopWindowSize: 10 });
|
|
103
|
+
expect(options.loopThresholdBlock).toBe(12);
|
|
104
|
+
expect(notes.some((n) => n.includes("adjusted to 12"))).toBe(true);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
test("NaN threshold → uses default", () => {
|
|
108
|
+
const { options } = normalizeOptions({ loopThresholdWarn: NaN });
|
|
109
|
+
expect(options.loopThresholdWarn).toBe(D.loopThresholdWarn);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test("string threshold value is parsed", () => {
|
|
113
|
+
const { options } = normalizeOptions({ loopThresholdWarn: "7" });
|
|
114
|
+
expect(options.loopThresholdWarn).toBe(7);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
test("non-numeric string threshold → uses default", () => {
|
|
118
|
+
const { options } = normalizeOptions({ loopThresholdWarn: "not-a-number" });
|
|
119
|
+
expect(options.loopThresholdWarn).toBe(D.loopThresholdWarn);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
test("logRotationBytes below 1024 is clamped to 1024", () => {
|
|
123
|
+
const { options, notes } = normalizeOptions({ logRotationBytes: 500 });
|
|
124
|
+
expect(options.logRotationBytes).toBe(1024);
|
|
125
|
+
expect(notes).toContain("logRotationBytes 500 clamped to 1024 (minimum)");
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
test("custom logDir and stateDir are preserved", () => {
|
|
129
|
+
const { options } = normalizeOptions({ logDir: "/tmp/my-logs", stateDir: "/tmp/my-state" });
|
|
130
|
+
expect(options.logDir).toBe("/tmp/my-logs");
|
|
131
|
+
expect(options.stateDir).toBe("/tmp/my-state");
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
// ── findSecretDirMatch ───────────────────────────────────────────────────────
|
|
136
|
+
|
|
137
|
+
describe("findSecretDirMatch", () => {
|
|
138
|
+
const home = process.env.HOME ?? "/home/test";
|
|
139
|
+
|
|
140
|
+
test("returns null for safe paths", () => {
|
|
141
|
+
expect(findSecretDirMatch("/tmp/foo")).toBeNull();
|
|
142
|
+
expect(findSecretDirMatch("/home/user/project")).toBeNull();
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
test("returns null for paths that are prefix siblings", () => {
|
|
146
|
+
// ~/.ssh-foo is NOT ~/.ssh
|
|
147
|
+
expect(findSecretDirMatch(`${home}/.ssh-foo`)).toBeNull();
|
|
148
|
+
expect(findSecretDirMatch(`${home}/.ssh-foo/bar`)).toBeNull();
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
test("returns the secret kind for exact secret dir", () => {
|
|
152
|
+
expect(findSecretDirMatch(`${home}/.ssh`)).toBe("~/.ssh");
|
|
153
|
+
expect(findSecretDirMatch(`${home}/.gnupg`)).toBe("~/.gnupg");
|
|
154
|
+
expect(findSecretDirMatch(`${home}/.aws`)).toBe("~/.aws");
|
|
155
|
+
expect(findSecretDirMatch(`${home}/.kube`)).toBe("~/.kube");
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
test("returns the secret kind for descendant paths", () => {
|
|
159
|
+
expect(findSecretDirMatch(`${home}/.ssh/keys`)).toBe("~/.ssh");
|
|
160
|
+
expect(findSecretDirMatch(`${home}/.ssh/keys/id_rsa`)).toBe("~/.ssh");
|
|
161
|
+
expect(findSecretDirMatch(`${home}/.gnupg/private`)).toBe("~/.gnupg");
|
|
162
|
+
expect(findSecretDirMatch(`${home}/.aws/credentials`)).toBe("~/.aws");
|
|
163
|
+
expect(findSecretDirMatch(`${home}/.kube/config`)).toBe("~/.kube");
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
test("tilde paths are expanded before comparison", () => {
|
|
167
|
+
expect(findSecretDirMatch("~/.ssh")).toBe("~/.ssh");
|
|
168
|
+
expect(findSecretDirMatch("~/.ssh/id_rsa")).toBe("~/.ssh");
|
|
169
|
+
expect(findSecretDirMatch("~/some-other-dir")).toBeNull();
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
test("refuses paths inside secret dir", () => {
|
|
173
|
+
expect(findSecretDirMatch(`${home}/.ssh/../ssh`)).toBeNull(); // resolves outside
|
|
174
|
+
expect(findSecretDirMatch(`${home}/.ssh/../.ssh/legit`)).toBe("~/.ssh"); // resolves to ~/.ssh
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
// ── findOffendingPath ────────────────────────────────────────────────────────
|
|
179
|
+
|
|
180
|
+
describe("findOffendingPath", () => {
|
|
181
|
+
test("returns null when both paths are safe", () => {
|
|
182
|
+
const opts: NormalizedOptions = {
|
|
183
|
+
...D,
|
|
184
|
+
logDir: "/tmp/bizar-logs",
|
|
185
|
+
stateDir: "/tmp/bizar-state",
|
|
186
|
+
};
|
|
187
|
+
expect(findOffendingPath(opts)).toBeNull();
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
test("returns offending logDir when it is inside a secret dir", () => {
|
|
191
|
+
const home = process.env.HOME ?? "/home/test";
|
|
192
|
+
const opts: NormalizedOptions = {
|
|
193
|
+
...D,
|
|
194
|
+
logDir: `${home}/.ssh/evil`,
|
|
195
|
+
stateDir: "/tmp/bizar-state",
|
|
196
|
+
};
|
|
197
|
+
const result = findOffendingPath(opts);
|
|
198
|
+
expect(result).not.toBeNull();
|
|
199
|
+
expect(result!.kind).toBe("~/.ssh");
|
|
200
|
+
expect(result!.path).toContain(".ssh");
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
test("returns offending stateDir when it is inside a secret dir", () => {
|
|
204
|
+
const home = process.env.HOME ?? "/home/test";
|
|
205
|
+
const opts: NormalizedOptions = {
|
|
206
|
+
...D,
|
|
207
|
+
logDir: "/tmp/bizar-logs",
|
|
208
|
+
stateDir: `${home}/.aws/creds`,
|
|
209
|
+
};
|
|
210
|
+
const result = findOffendingPath(opts);
|
|
211
|
+
expect(result).not.toBeNull();
|
|
212
|
+
expect(result!.kind).toBe("~/.aws");
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
test("logDir takes priority over stateDir in error message", () => {
|
|
216
|
+
const home = process.env.HOME ?? "/home/test";
|
|
217
|
+
const opts: NormalizedOptions = {
|
|
218
|
+
...D,
|
|
219
|
+
logDir: `${home}/.ssh/evil`,
|
|
220
|
+
stateDir: `${home}/.gnupg/evil`,
|
|
221
|
+
};
|
|
222
|
+
const result = findOffendingPath(opts);
|
|
223
|
+
// logDir is checked first
|
|
224
|
+
expect(result!.kind).toBe("~/.ssh");
|
|
225
|
+
});
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
// ── readEnvFlags ─────────────────────────────────────────────────────────────
|
|
229
|
+
|
|
230
|
+
describe("readEnvFlags", () => {
|
|
231
|
+
test("returns all false when env vars are not set", () => {
|
|
232
|
+
delete process.env.BIZAR_DISABLE;
|
|
233
|
+
delete process.env.BIZAR_DISABLE_LOOP;
|
|
234
|
+
delete process.env.BIZAR_DISABLE_LOG;
|
|
235
|
+
const flags = readEnvFlags();
|
|
236
|
+
expect(flags.disable).toBe(false);
|
|
237
|
+
expect(flags.disableLoop).toBe(false);
|
|
238
|
+
expect(flags.disableLog).toBe(false);
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
test("BIZAR_DISABLE=1 sets disable=true", () => {
|
|
242
|
+
process.env.BIZAR_DISABLE = "1";
|
|
243
|
+
const flags = readEnvFlags();
|
|
244
|
+
expect(flags.disable).toBe(true);
|
|
245
|
+
delete process.env.BIZAR_DISABLE;
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
test("BIZAR_DISABLE_LOOP=1 sets disableLoop=true", () => {
|
|
249
|
+
process.env.BIZAR_DISABLE_LOOP = "1";
|
|
250
|
+
const flags = readEnvFlags();
|
|
251
|
+
expect(flags.disableLoop).toBe(true);
|
|
252
|
+
delete process.env.BIZAR_DISABLE_LOOP;
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
test("BIZAR_DISABLE_LOG=1 sets disableLog=true", () => {
|
|
256
|
+
process.env.BIZAR_DISABLE_LOG = "1";
|
|
257
|
+
const flags = readEnvFlags();
|
|
258
|
+
expect(flags.disableLog).toBe(true);
|
|
259
|
+
delete process.env.BIZAR_DISABLE_LOG;
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
test("non-1 values are treated as false", () => {
|
|
263
|
+
process.env.BIZAR_DISABLE = "true";
|
|
264
|
+
process.env.BIZAR_DISABLE_LOOP = "yes";
|
|
265
|
+
process.env.BIZAR_DISABLE_LOG = "0";
|
|
266
|
+
const flags = readEnvFlags();
|
|
267
|
+
expect(flags.disable).toBe(false);
|
|
268
|
+
expect(flags.disableLoop).toBe(false);
|
|
269
|
+
expect(flags.disableLog).toBe(false);
|
|
270
|
+
delete process.env.BIZAR_DISABLE;
|
|
271
|
+
delete process.env.BIZAR_DISABLE_LOOP;
|
|
272
|
+
delete process.env.BIZAR_DISABLE_LOG;
|
|
273
|
+
});
|
|
274
|
+
});
|