@wrongstack/tools 0.289.0 → 0.291.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.
@@ -0,0 +1,128 @@
1
+ /**
2
+ * Auto-proceed loop guard.
3
+ *
4
+ * ## Problem
5
+ *
6
+ * The REPL and TUI both implement an "auto" autonomy mode that, after every
7
+ * completed agent turn, feeds the next suggestion (or top auto="true" item)
8
+ * back as a fresh prompt. When the agent emits the same `<nextsteps>` block
9
+ * on every reply — which happens whenever the model's output is stable but
10
+ * autonomy treats the next step as "still actionable" — the loop self-feeds
11
+ * the same instruction 2–3 times in a row before the longer
12
+ * `autoProceedMaxIterations` cap (default 50) trips.
13
+ *
14
+ * The user reports the visible symptom: the same response, including the same
15
+ * `<nextsteps>` block, repeats. Manual input from the user is the only thing
16
+ * that breaks out. They suspect the next-steps mechanism is the source.
17
+ *
18
+ * The 50-iteration cap is intentionally loose — it is a runaway safety net,
19
+ * not a UX signal. By the time it trips, the user has already watched the
20
+ * same response 50 times.
21
+ *
22
+ * ## Approach
23
+ *
24
+ * A small, pure, browser-safe stateful helper that records the last few
25
+ * prompts fed through the auto-proceed / auto-submit path. It normalizes
26
+ * prompts (whitespace, casing) so trivial rewordings are not collapsed, but
27
+ * identical re-feeds are. When the same normalized prompt is fed
28
+ * `repeatThreshold` (default 2) times in a row, the guard returns
29
+ * `{ shouldHalt: true }`.
30
+ *
31
+ * Callers are expected to:
32
+ * 1. Call `record(prompt)` immediately before each auto-feed.
33
+ * 2. If `shouldHalt` is true, clear the suggestion + auto-suggestion store,
34
+ * cancel any pending countdown, and surface a "we detected a loop"
35
+ * message asking the user what's happening. Do NOT feed the prompt.
36
+ * 3. Call `reset()` whenever the user types anything manually (REPL manual
37
+ * input or any user-driven submit) so the next auto-feed starts clean.
38
+ *
39
+ * The default `repeatThreshold` is 2 — i.e. the second consecutive identical
40
+ * feed (the first re-feed of an already-seen prompt) halts the loop. That
41
+ * matches the user's report ("receiving the same prompt 2-3 times in a row
42
+ * causes the system to enter a loop"). Setting it to 2 catches the loop on
43
+ * the first identical re-feed rather than after the 50-iteration runaway cap trips.
44
+ *
45
+ * The window size is intentionally small (default 3). The loop we care about
46
+ * is the immediate repetition — a longer history would misfire on legitimate
47
+ * "do X, then do Y, then do X" sequences the model occasionally returns to.
48
+ *
49
+ * This module is BROWSER-SAFE — no Node-only imports — so it can be imported
50
+ * from Vite-bundled WebUI as well as Node-based CLI/TUI.
51
+ */
52
+ /**
53
+ * Normalize a prompt for repetition comparison. Whitespace is collapsed and
54
+ * trimmed; case is folded to lowercase. Nothing semantic is removed, so two
55
+ * prompts that differ in any meaningful way (extra word, different code,
56
+ * different file path) compare as distinct. Two prompts that differ only by
57
+ * leading/trailing whitespace and casing are treated as identical.
58
+ */
59
+ export declare function normalizeForRepetition(prompt: string): string;
60
+ export interface LoopGuardOptions {
61
+ /**
62
+ * How many consecutive identical prompts (after normalization) trigger a
63
+ * halt. Default 2 — i.e. the second consecutive identical feed (the first
64
+ * re-feed of an already-seen prompt) halts the loop. Clamped to >= 2.
65
+ */
66
+ repeatThreshold?: number;
67
+ /**
68
+ * How many of the most recent feeds to retain for comparison. The window
69
+ * is searched from newest to oldest; the guard cares about the run of
70
+ * identical entries ending at the most recent one, not about identical
71
+ * entries anywhere in history. Default 3.
72
+ */
73
+ windowSize?: number;
74
+ }
75
+ export interface RepetitionSignal {
76
+ /** The normalized prompt that just got recorded. */
77
+ normalized: string;
78
+ /**
79
+ * Number of consecutive identical feeds ending at the most recent feed,
80
+ * including this one. Always >= 1.
81
+ */
82
+ runLength: number;
83
+ /**
84
+ * True when the run length has crossed `repeatThreshold`. Callers must
85
+ * stop feeding and surface a user prompt instead.
86
+ */
87
+ shouldHalt: boolean;
88
+ }
89
+ export interface AutoProceedLoopGuard {
90
+ /**
91
+ * Record a prompt that is about to be auto-fed. Returns the repetition
92
+ * signal for that prompt. When `shouldHalt` is true the caller MUST NOT
93
+ * feed the prompt — it has been recorded for post-mortem inspection, but
94
+ * the loop must be broken instead.
95
+ */
96
+ record(prompt: string): RepetitionSignal;
97
+ /**
98
+ * Drop the history. Call this on any manual user input so a fresh run
99
+ * starts with no memory of the prior cycle.
100
+ */
101
+ reset(): void;
102
+ /**
103
+ * Read-only view of the most recent normalized prompts (newest last).
104
+ * Useful for diagnostics and for the message shown to the user when the
105
+ * guard halts the loop.
106
+ */
107
+ history(): readonly string[];
108
+ /**
109
+ * How many entries the guard is currently retaining.
110
+ */
111
+ size(): number;
112
+ }
113
+ /**
114
+ * Build a fresh loop guard. Defaults: `repeatThreshold = 2`, `windowSize = 3`.
115
+ *
116
+ * @example
117
+ * const guard = createAutoProceedLoopGuard();
118
+ * for (const prompt of candidates) {
119
+ * const signal = guard.record(prompt);
120
+ * if (signal.shouldHalt) {
121
+ * haltAutoProceedAndAskUser(signal);
122
+ * break;
123
+ * }
124
+ * feedPrompt(prompt);
125
+ * }
126
+ */
127
+ export declare function createAutoProceedLoopGuard(options?: LoopGuardOptions): AutoProceedLoopGuard;
128
+ //# sourceMappingURL=auto-proceed-loop-guard.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auto-proceed-loop-guard.d.ts","sourceRoot":"","sources":["../src/auto-proceed-loop-guard.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkDG;AAEH;;;;;;GAMG;AACH,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAE7D;AAED,MAAM,WAAW,gBAAgB;IAC/B;;;;OAIG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB;;;;;OAKG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,gBAAgB;IAC/B,oDAAoD;IACpD,UAAU,EAAE,MAAM,CAAC;IACnB;;;OAGG;IACH,SAAS,EAAE,MAAM,CAAC;IAClB;;;OAGG;IACH,UAAU,EAAE,OAAO,CAAC;CACrB;AAED,MAAM,WAAW,oBAAoB;IACnC;;;;;OAKG;IACH,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,gBAAgB,CAAC;IACzC;;;OAGG;IACH,KAAK,IAAI,IAAI,CAAC;IACd;;;;OAIG;IACH,OAAO,IAAI,SAAS,MAAM,EAAE,CAAC;IAC7B;;OAEG;IACH,IAAI,IAAI,MAAM,CAAC;CAChB;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,0BAA0B,CAAC,OAAO,GAAE,gBAAqB,GAAG,oBAAoB,CAmD/F"}
@@ -0,0 +1,46 @@
1
+ // src/auto-proceed-loop-guard.ts
2
+ function normalizeForRepetition(prompt) {
3
+ return prompt.replace(/\s+/g, " ").trim().toLowerCase();
4
+ }
5
+ function createAutoProceedLoopGuard(options = {}) {
6
+ const safeIntegerOption = (value, fallback) => {
7
+ if (value === void 0) return fallback;
8
+ if (!Number.isFinite(value)) return fallback;
9
+ return Math.floor(value);
10
+ };
11
+ const repeatThreshold = Math.max(2, safeIntegerOption(options.repeatThreshold, 2));
12
+ const windowSize = Math.max(repeatThreshold, safeIntegerOption(options.windowSize, 3));
13
+ let buffer = [];
14
+ function record(prompt) {
15
+ const normalized = normalizeForRepetition(prompt);
16
+ buffer.push(normalized);
17
+ if (buffer.length > windowSize) {
18
+ buffer = buffer.slice(buffer.length - windowSize);
19
+ }
20
+ let runLength = 0;
21
+ for (let i = buffer.length - 1; i >= 0; i--) {
22
+ if (buffer[i] === normalized) runLength++;
23
+ else break;
24
+ }
25
+ return {
26
+ normalized,
27
+ runLength,
28
+ shouldHalt: runLength >= repeatThreshold
29
+ };
30
+ }
31
+ function reset() {
32
+ buffer = [];
33
+ }
34
+ function history() {
35
+ return buffer.slice();
36
+ }
37
+ function size() {
38
+ return buffer.length;
39
+ }
40
+ return { record, reset, history, size };
41
+ }
42
+ export {
43
+ createAutoProceedLoopGuard,
44
+ normalizeForRepetition
45
+ };
46
+ //# sourceMappingURL=auto-proceed-loop-guard.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/auto-proceed-loop-guard.ts"],
4
+ "sourcesContent": ["/**\n * Auto-proceed loop guard.\n *\n * ## Problem\n *\n * The REPL and TUI both implement an \"auto\" autonomy mode that, after every\n * completed agent turn, feeds the next suggestion (or top auto=\"true\" item)\n * back as a fresh prompt. When the agent emits the same `<nextsteps>` block\n * on every reply \u2014 which happens whenever the model's output is stable but\n * autonomy treats the next step as \"still actionable\" \u2014 the loop self-feeds\n * the same instruction 2\u20133 times in a row before the longer\n * `autoProceedMaxIterations` cap (default 50) trips.\n *\n * The user reports the visible symptom: the same response, including the same\n * `<nextsteps>` block, repeats. Manual input from the user is the only thing\n * that breaks out. They suspect the next-steps mechanism is the source.\n *\n * The 50-iteration cap is intentionally loose \u2014 it is a runaway safety net,\n * not a UX signal. By the time it trips, the user has already watched the\n * same response 50 times.\n *\n * ## Approach\n *\n * A small, pure, browser-safe stateful helper that records the last few\n * prompts fed through the auto-proceed / auto-submit path. It normalizes\n * prompts (whitespace, casing) so trivial rewordings are not collapsed, but\n * identical re-feeds are. When the same normalized prompt is fed\n * `repeatThreshold` (default 2) times in a row, the guard returns\n * `{ shouldHalt: true }`.\n *\n * Callers are expected to:\n * 1. Call `record(prompt)` immediately before each auto-feed.\n * 2. If `shouldHalt` is true, clear the suggestion + auto-suggestion store,\n * cancel any pending countdown, and surface a \"we detected a loop\"\n * message asking the user what's happening. Do NOT feed the prompt.\n * 3. Call `reset()` whenever the user types anything manually (REPL manual\n * input or any user-driven submit) so the next auto-feed starts clean.\n *\n * The default `repeatThreshold` is 2 \u2014 i.e. the second consecutive identical\n * feed (the first re-feed of an already-seen prompt) halts the loop. That\n * matches the user's report (\"receiving the same prompt 2-3 times in a row\n * causes the system to enter a loop\"). Setting it to 2 catches the loop on\n * the first identical re-feed rather than after the 50-iteration runaway cap trips.\n *\n * The window size is intentionally small (default 3). The loop we care about\n * is the immediate repetition \u2014 a longer history would misfire on legitimate\n * \"do X, then do Y, then do X\" sequences the model occasionally returns to.\n *\n * This module is BROWSER-SAFE \u2014 no Node-only imports \u2014 so it can be imported\n * from Vite-bundled WebUI as well as Node-based CLI/TUI.\n */\n\n/**\n * Normalize a prompt for repetition comparison. Whitespace is collapsed and\n * trimmed; case is folded to lowercase. Nothing semantic is removed, so two\n * prompts that differ in any meaningful way (extra word, different code,\n * different file path) compare as distinct. Two prompts that differ only by\n * leading/trailing whitespace and casing are treated as identical.\n */\nexport function normalizeForRepetition(prompt: string): string {\n return prompt.replace(/\\s+/g, ' ').trim().toLowerCase();\n}\n\nexport interface LoopGuardOptions {\n /**\n * How many consecutive identical prompts (after normalization) trigger a\n * halt. Default 2 \u2014 i.e. the second consecutive identical feed (the first\n * re-feed of an already-seen prompt) halts the loop. Clamped to >= 2.\n */\n repeatThreshold?: number;\n /**\n * How many of the most recent feeds to retain for comparison. The window\n * is searched from newest to oldest; the guard cares about the run of\n * identical entries ending at the most recent one, not about identical\n * entries anywhere in history. Default 3.\n */\n windowSize?: number;\n}\n\nexport interface RepetitionSignal {\n /** The normalized prompt that just got recorded. */\n normalized: string;\n /**\n * Number of consecutive identical feeds ending at the most recent feed,\n * including this one. Always >= 1.\n */\n runLength: number;\n /**\n * True when the run length has crossed `repeatThreshold`. Callers must\n * stop feeding and surface a user prompt instead.\n */\n shouldHalt: boolean;\n}\n\nexport interface AutoProceedLoopGuard {\n /**\n * Record a prompt that is about to be auto-fed. Returns the repetition\n * signal for that prompt. When `shouldHalt` is true the caller MUST NOT\n * feed the prompt \u2014 it has been recorded for post-mortem inspection, but\n * the loop must be broken instead.\n */\n record(prompt: string): RepetitionSignal;\n /**\n * Drop the history. Call this on any manual user input so a fresh run\n * starts with no memory of the prior cycle.\n */\n reset(): void;\n /**\n * Read-only view of the most recent normalized prompts (newest last).\n * Useful for diagnostics and for the message shown to the user when the\n * guard halts the loop.\n */\n history(): readonly string[];\n /**\n * How many entries the guard is currently retaining.\n */\n size(): number;\n}\n\n/**\n * Build a fresh loop guard. Defaults: `repeatThreshold = 2`, `windowSize = 3`.\n *\n * @example\n * const guard = createAutoProceedLoopGuard();\n * for (const prompt of candidates) {\n * const signal = guard.record(prompt);\n * if (signal.shouldHalt) {\n * haltAutoProceedAndAskUser(signal);\n * break;\n * }\n * feedPrompt(prompt);\n * }\n */\nexport function createAutoProceedLoopGuard(options: LoopGuardOptions = {}): AutoProceedLoopGuard {\n // Validate options defensively. Non-finite values (NaN, Infinity) MUST\n // fall back to the documented default \u2014 a guard that silently disables\n // itself is worse than no guard at all. Specifically:\n // - `repeatThreshold: NaN` would cause `Math.floor(NaN) = NaN`,\n // `Math.max(2, NaN) = NaN`, and `runLength >= NaN` is always false,\n // making the guard permanent no-op.\n // - `windowSize: NaN` / `Infinity` would skip the trim branch entirely\n // (`buffer.length > NaN` and `buffer.length > Infinity` are both\n // false), so the buffer would grow without bound.\n const safeIntegerOption = (value: number | undefined, fallback: number): number => {\n if (value === undefined) return fallback;\n if (!Number.isFinite(value)) return fallback;\n return Math.floor(value);\n };\n const repeatThreshold = Math.max(2, safeIntegerOption(options.repeatThreshold, 2));\n const windowSize = Math.max(repeatThreshold, safeIntegerOption(options.windowSize, 3));\n let buffer: string[] = [];\n\n function record(prompt: string): RepetitionSignal {\n const normalized = normalizeForRepetition(prompt);\n buffer.push(normalized);\n if (buffer.length > windowSize) {\n buffer = buffer.slice(buffer.length - windowSize);\n }\n // Count the trailing run of identical normalized prompts.\n let runLength = 0;\n for (let i = buffer.length - 1; i >= 0; i--) {\n if (buffer[i] === normalized) runLength++;\n else break;\n }\n return {\n normalized,\n runLength,\n shouldHalt: runLength >= repeatThreshold,\n };\n }\n\n function reset(): void {\n buffer = [];\n }\n\n function history(): readonly string[] {\n return buffer.slice();\n }\n\n function size(): number {\n return buffer.length;\n }\n\n return { record, reset, history, size };\n}\n"],
5
+ "mappings": ";AA2DO,SAAS,uBAAuB,QAAwB;AAC7D,SAAO,OAAO,QAAQ,QAAQ,GAAG,EAAE,KAAK,EAAE,YAAY;AACxD;AAwEO,SAAS,2BAA2B,UAA4B,CAAC,GAAyB;AAU/F,QAAM,oBAAoB,CAAC,OAA2B,aAA6B;AACjF,QAAI,UAAU,OAAW,QAAO;AAChC,QAAI,CAAC,OAAO,SAAS,KAAK,EAAG,QAAO;AACpC,WAAO,KAAK,MAAM,KAAK;AAAA,EACzB;AACA,QAAM,kBAAkB,KAAK,IAAI,GAAG,kBAAkB,QAAQ,iBAAiB,CAAC,CAAC;AACjF,QAAM,aAAa,KAAK,IAAI,iBAAiB,kBAAkB,QAAQ,YAAY,CAAC,CAAC;AACrF,MAAI,SAAmB,CAAC;AAExB,WAAS,OAAO,QAAkC;AAChD,UAAM,aAAa,uBAAuB,MAAM;AAChD,WAAO,KAAK,UAAU;AACtB,QAAI,OAAO,SAAS,YAAY;AAC9B,eAAS,OAAO,MAAM,OAAO,SAAS,UAAU;AAAA,IAClD;AAEA,QAAI,YAAY;AAChB,aAAS,IAAI,OAAO,SAAS,GAAG,KAAK,GAAG,KAAK;AAC3C,UAAI,OAAO,CAAC,MAAM,WAAY;AAAA,UACzB;AAAA,IACP;AACA,WAAO;AAAA,MACL;AAAA,MACA;AAAA,MACA,YAAY,aAAa;AAAA,IAC3B;AAAA,EACF;AAEA,WAAS,QAAc;AACrB,aAAS,CAAC;AAAA,EACZ;AAEA,WAAS,UAA6B;AACpC,WAAO,OAAO,MAAM;AAAA,EACtB;AAEA,WAAS,OAAe;AACtB,WAAO,OAAO;AAAA,EAChB;AAEA,SAAO,EAAE,QAAQ,OAAO,SAAS,KAAK;AACxC;",
6
+ "names": []
7
+ }