-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.ts
More file actions
256 lines (228 loc) · 10.1 KB
/
test.ts
File metadata and controls
256 lines (228 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**
* Test for doom loop detection algorithm
* Run with: npx tsx test.ts
*/
import {
findRepeatedPhrase,
findRepeatedIntent,
findIntentCycle,
extractText,
detectDoomLoop,
DEFAULT_CONFIG,
} from "./detect-loop.ts";
import type { AgentMessage } from "@mariozechner/pi-agent-core";
function test(name: string, fn: () => void) {
try {
fn();
console.log(`✅ ${name}`);
} catch (err) {
console.error(`❌ ${name}:`, err);
process.exitCode = 1;
}
}
function assert(condition: boolean, message: string) {
if (!condition) throw new Error(message);
}
// Test 1: Simple 2-word phrase with minWords=2
test("detects simple phrase 'test phrase' with minWords=2", () => {
const result = findRepeatedPhrase("test phrase test phrase test phrase", { ...DEFAULT_CONFIG, minWords: 2 });
assert(result !== null, "Should detect repetition");
assert(result!.phrase === "test phrase", `Expected "test phrase", got "${result!.phrase}"`);
assert(result!.count === 3, `Expected count 3, got ${result!.count}`);
assert(result!.wordCount === 2, `Expected wordCount 2, got ${result!.wordCount}`);
});
// Test 2: Longer phrase (4 words, meets minWords=3)
test("detects longer phrase 'I will help you' (4 words)", () => {
const result = findRepeatedPhrase("I will help you I will help you I will help you", DEFAULT_CONFIG);
assert(result !== null, "Should detect repetition");
assert(result!.phrase === "I will help you", `Expected "I will help you", got "${result!.phrase}"`);
assert(result!.count === 3, `Expected count 3, got ${result!.count}`);
assert(result!.wordCount === 4, `Expected wordCount 4, got ${result!.wordCount}`);
});
// Test 3: No repetition
test("returns null when no repetition", () => {
const result = findRepeatedPhrase("This is a normal sentence with unique content", DEFAULT_CONFIG);
assert(result === null, "Should not detect repetition in normal text");
});
// Test 4: Too short for threshold
test("returns null for text shorter than minWords * threshold", () => {
const result = findRepeatedPhrase("hello hello hello", { ...DEFAULT_CONFIG, minWords: 5 });
assert(result === null, "Should return null for text too short for threshold");
});
// Test 5: extractText from AgentMessage
test("extracts text from assistant messages", () => {
const messages: AgentMessage[] = [
{
role: "assistant",
content: [
{ type: "text", text: "Hello world" },
{ type: "text", text: "More text here" },
],
api: "anthropic-messages",
provider: "anthropic",
model: "claude-3",
usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, totalTokens: 0, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 } },
stopReason: "stop",
timestamp: Date.now(),
},
] as AgentMessage[];
const text = extractText(messages);
assert(text === "Hello world\nMore text here", `Expected combined text, got "${text}"`);
});
// Test 6: detectDoomLoop on full messages with longer phrase
test("detects doom loop in messages with 4-word phrase", () => {
const messages: AgentMessage[] = [
{
role: "assistant",
content: [
{ type: "text", text: "I will help you I will help you I will help you" },
],
api: "anthropic-messages",
provider: "anthropic",
model: "claude-3",
usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, totalTokens: 0, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 } },
stopReason: "stop",
timestamp: Date.now(),
},
] as AgentMessage[];
const result = detectDoomLoop(messages, DEFAULT_CONFIG);
assert(result !== null, "Should detect doom loop");
assert(result!.phrase === "I will help you", `Expected "I will help you", got "${result!.phrase}"`);
});
// Test 7: Ignores tool calls in message content
test("ignores tool calls and detects text repetition", () => {
const messages: AgentMessage[] = [
{
role: "assistant",
content: [
{ type: "text", text: "Doing something else" },
{ type: "toolCall", id: "1", name: "bash", arguments: { command: "ls" } },
{ type: "text", text: "I will help I will help I will help" },
],
api: "anthropic-messages",
provider: "anthropic",
model: "claude-3",
usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, totalTokens: 0, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 } },
stopReason: "stop",
timestamp: Date.now(),
},
] as AgentMessage[];
const result = detectDoomLoop(messages, { ...DEFAULT_CONFIG, minWords: 3 });
assert(result !== null, "Should detect doom loop");
assert(result!.phrase === "I will help", `Expected "I will help", got "${result!.phrase}"`);
assert(result!.count === 3, `Expected count 3, got ${result!.count}`);
});
// Test 8: Only counts consecutive repetitions
test("only counts consecutive repetitions", () => {
const text = "unique test phrase test phrase unique test phrase test phrase test phrase";
const result = findRepeatedPhrase(text, { ...DEFAULT_CONFIG, minWords: 2 });
assert(result !== null, "Should detect");
// Should detect the second group (3 consecutive)
assert(result!.count === 3, `Expected count 3 for consecutive group, got ${result!.count}`);
assert(result!.phrase === "test phrase", `Expected "test phrase", got "${result!.phrase}"`);
});
// Test 9: The roadmap demo phrase
test("detects roadmap demo phrase 'test phrase test phrase test phrase'", () => {
// This is the exact phrase from the roadmap: "Can detect repeated phrase 'test phrase test phrase test phrase' in a message"
const result = findRepeatedPhrase("test phrase test phrase test phrase", { ...DEFAULT_CONFIG, minWords: 2 });
assert(result !== null, "Should detect the roadmap demo phrase");
assert(result!.phrase === "test phrase", `Expected "test phrase", got "${result!.phrase}"`);
assert(result!.count === 3, `Expected count 3, got ${result!.count}`);
});
// Test 10: Real-world loop with wording variation
test("detects repeated action intent with wording variation", () => {
const text = [
"Let me check .dockerignore and verify source files.",
"I should check .dockerignore and verify source files.",
"Let me do that.",
"Let me check .dockerignore and verify source files.",
"I should check .dockerignore and verify source files.",
"Let me do that.",
"Let me check .dockerignore and verify source files.",
].join(" ");
const result = findRepeatedIntent(text, { ...DEFAULT_CONFIG, minWords: 2 });
assert(result !== null, "Should detect repeated action intent");
assert(result!.phrase.includes("check dockerignore verify source files"), `Unexpected phrase: ${result!.phrase}`);
assert(result!.count >= 3, `Expected count >= 3, got ${result!.count}`);
});
// Test 11: Docker compose read loop from real failure mode
test("detects repeated docker compose read intent only through explicit intent helper", () => {
const text = [
"I'll read docker-compose.yml.",
"I'll read the docker compose file.",
"I'll read docker compose config.",
"OK I'll read it now.",
"I'll read docker-compose.yml.",
"Let me read the docker compose file now.",
"I'll read docker compose config.",
].join(" ");
const result = findRepeatedIntent(text, { ...DEFAULT_CONFIG, minWords: 2 });
assert(result !== null, "Should detect repeated read intent");
assert(result!.phrase.includes("read docker compose"), `Unexpected phrase: ${result!.phrase}`);
});
// Test 12: detectDoomLoop ignores fuzzy intent repetition to avoid false auto-aborts
test("detectDoomLoop does not flag repeated file-name intent summaries", () => {
const text = [
"Fix made in index.ts and test-recovery.ts.",
"Validation ran test.ts and test-recovery.ts.",
"Note: test-recovery.ts now covers ctx.abort.",
].join(" ");
const result = detectDoomLoop([
{
role: "assistant",
content: [{ type: "text", text }],
api: "anthropic-messages",
provider: "anthropic",
model: "claude-3",
usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, totalTokens: 0, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 } },
stopReason: "stop",
timestamp: Date.now(),
},
] as AgentMessage[], { ...DEFAULT_CONFIG, minWords: 2 });
assert(result === null, `Should not detect fuzzy file-name repetition, got ${result?.phrase}`);
});
// Test 13: Intent cycles across fragments
test("detects cyclic intent pattern", () => {
const text = [
"I'll read docker-compose.yml.",
"I'll check the logs.",
"I'll read docker-compose.yml.",
"I'll check the logs.",
"I'll read docker-compose.yml.",
"I'll check the logs.",
].join(" ");
const result = findIntentCycle(text, DEFAULT_CONFIG);
assert(result !== null, "Should detect A-B-A-B cycle");
assert(result!.kind === "cycle", `Expected cycle kind, got ${result!.kind}`);
});
// Test 13: Normal varied tool-using response should not trigger
test("does not flag normal varied assistant progress", () => {
const messages: AgentMessage[] = [
{
role: "assistant",
content: [
{ type: "text", text: "I'll inspect the project structure first." },
{ type: "toolCall", id: "1", name: "bash", arguments: { command: "ls" } },
],
api: "anthropic-messages",
provider: "anthropic",
model: "claude-3",
usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, totalTokens: 0, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 } },
stopReason: "toolUse",
timestamp: Date.now(),
},
{
role: "assistant",
content: [{ type: "text", text: "Package metadata shows a small TypeScript extension. Next I will review the tests." }],
api: "anthropic-messages",
provider: "anthropic",
model: "claude-3",
usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, totalTokens: 0, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 } },
stopReason: "stop",
timestamp: Date.now(),
},
] as AgentMessage[];
const result = detectDoomLoop(messages, DEFAULT_CONFIG);
assert(result === null, `Should not detect normal progress, got ${result?.phrase}`);
});
console.log("\n✅ All tests passed!");