forked from NoeFabris/opencode-antigravity-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththinking-recovery.test.ts
More file actions
321 lines (280 loc) · 11.1 KB
/
Copy paththinking-recovery.test.ts
File metadata and controls
321 lines (280 loc) · 11.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import { describe, expect, it } from "vitest";
import {
analyzeConversationState,
closeToolLoopForThinking,
hasPossibleCompactedThinking,
looksLikeCompactedThinkingTurn,
needsThinkingRecovery,
} from "./thinking-recovery";
// ─── Fixtures ────────────────────────────────────────────────────────────────
function userMsg(text: string) {
return { role: "user", parts: [{ text }] };
}
function modelMsg(text: string) {
return { role: "model", parts: [{ text }] };
}
function modelWithThinking(text: string) {
return {
role: "model",
parts: [{ thought: true, text: "thinking..." }, { text }],
};
}
function modelWithToolCall(name = "myTool") {
return {
role: "model",
parts: [{ functionCall: { name, args: {} } }],
};
}
function modelWithThinkingAndToolCall(name = "myTool") {
return {
role: "model",
parts: [
{ thought: true, text: "reasoning..." },
{ functionCall: { name, args: {} } },
],
};
}
function toolResultMsg(name = "myTool") {
return {
role: "user",
parts: [{ functionResponse: { name, response: { result: "ok" } } }],
};
}
// ─── analyzeConversationState ─────────────────────────────────────────────────
describe("analyzeConversationState", () => {
it("returns default state for empty contents", () => {
const state = analyzeConversationState([]);
expect(state.inToolLoop).toBe(false);
expect(state.turnStartIdx).toBe(-1);
expect(state.lastModelIdx).toBe(-1);
});
it("returns default state for non-array input", () => {
const state = analyzeConversationState(null as any);
expect(state.inToolLoop).toBe(false);
});
it("detects a simple user→model conversation (not in tool loop)", () => {
const contents = [userMsg("hello"), modelMsg("hi there")];
const state = analyzeConversationState(contents);
expect(state.inToolLoop).toBe(false);
expect(state.lastModelIdx).toBe(1);
expect(state.lastModelHasThinking).toBe(false);
expect(state.lastModelHasToolCalls).toBe(false);
});
it("detects thinking in last model message", () => {
const contents = [userMsg("hello"), modelWithThinking("hi there")];
const state = analyzeConversationState(contents);
expect(state.lastModelHasThinking).toBe(true);
expect(state.turnHasThinking).toBe(true);
});
it("detects tool loop: conversation ends with tool result", () => {
const contents = [
userMsg("do something"),
modelWithToolCall("search"),
toolResultMsg("search"),
];
const state = analyzeConversationState(contents);
expect(state.inToolLoop).toBe(true);
expect(state.lastModelIdx).toBe(1);
expect(state.lastModelHasToolCalls).toBe(true);
});
it("detects tool loop with multiple tool results", () => {
const contents = [
userMsg("do two things"),
{ role: "model", parts: [
{ functionCall: { name: "a", args: {} } },
{ functionCall: { name: "b", args: {} } },
]},
{ role: "user", parts: [
{ functionResponse: { name: "a", response: {} } },
{ functionResponse: { name: "b", response: {} } },
]},
];
const state = analyzeConversationState(contents);
expect(state.inToolLoop).toBe(true);
});
it("is NOT in tool loop when last message is a real user message", () => {
const contents = [
userMsg("task"),
modelWithToolCall(),
toolResultMsg(),
modelMsg("done"),
userMsg("thanks"),
];
const state = analyzeConversationState(contents);
expect(state.inToolLoop).toBe(false);
});
it("tracks turn start correctly across multi-step tool loop", () => {
const contents = [
userMsg("first real user"),
modelWithThinkingAndToolCall("step1"),
toolResultMsg("step1"),
modelWithToolCall("step2"),
toolResultMsg("step2"),
];
const state = analyzeConversationState(contents);
expect(state.turnStartIdx).toBe(1); // first model message in turn
expect(state.turnHasThinking).toBe(true);
expect(state.inToolLoop).toBe(true);
});
it("turns NOT having thinking when first model msg has no thinking", () => {
const contents = [
userMsg("go"),
modelWithToolCall("t1"),
toolResultMsg("t1"),
];
const state = analyzeConversationState(contents);
expect(state.turnHasThinking).toBe(false);
expect(state.inToolLoop).toBe(true);
});
});
// ─── needsThinkingRecovery ────────────────────────────────────────────────────
describe("needsThinkingRecovery", () => {
it("returns false when not in tool loop", () => {
expect(needsThinkingRecovery({ inToolLoop: false, turnHasThinking: false,
turnStartIdx: -1, lastModelIdx: -1, lastModelHasThinking: false,
lastModelHasToolCalls: false })).toBe(false);
});
it("returns false when in tool loop but turn had thinking", () => {
expect(needsThinkingRecovery({ inToolLoop: true, turnHasThinking: true,
turnStartIdx: 1, lastModelIdx: 2, lastModelHasThinking: false,
lastModelHasToolCalls: true })).toBe(false);
});
it("returns true when in tool loop without thinking", () => {
expect(needsThinkingRecovery({ inToolLoop: true, turnHasThinking: false,
turnStartIdx: 1, lastModelIdx: 2, lastModelHasThinking: false,
lastModelHasToolCalls: true })).toBe(true);
});
});
// ─── closeToolLoopForThinking ─────────────────────────────────────────────────
describe("closeToolLoopForThinking", () => {
it("appends synthetic model + user messages", () => {
const contents = [
userMsg("go"),
modelWithToolCall("search"),
toolResultMsg("search"),
];
const result = closeToolLoopForThinking(contents);
expect(result.length).toBe(5);
expect(result[3]?.role).toBe("model");
expect(result[4]?.role).toBe("user");
expect(result[4]?.parts[0]?.text).toBe("[Continue]");
});
it("strips thinking blocks from prior messages", () => {
const contents = [
userMsg("hello"),
modelWithThinking("response"),
toolResultMsg(),
];
const result = closeToolLoopForThinking(contents);
const modelMessages = result.filter((m) => m.role === "model");
for (const msg of modelMessages) {
const parts: any[] = msg.parts ?? [];
const hasThinking = parts.some((p: any) => p?.thought === true);
expect(hasThinking).toBe(false);
}
});
it("uses singular message for single tool result", () => {
const contents = [userMsg("go"), modelWithToolCall(), toolResultMsg()];
const result = closeToolLoopForThinking(contents);
const syntheticModel = result[result.length - 2];
expect(syntheticModel?.parts[0]?.text).toBe("[Tool execution completed.]");
});
it("uses plural message for multiple tool results", () => {
const contents = [
userMsg("go"),
{ role: "model", parts: [
{ functionCall: { name: "a", args: {} } },
{ functionCall: { name: "b", args: {} } },
]},
{ role: "user", parts: [
{ functionResponse: { name: "a", response: {} } },
{ functionResponse: { name: "b", response: {} } },
]},
];
const result = closeToolLoopForThinking(contents);
const syntheticModel = result[result.length - 2];
expect(syntheticModel?.parts[0]?.text).toBe("[2 tool executions completed.]");
});
it("uses fallback message when no tool results present", () => {
const contents = [userMsg("go"), modelMsg("working...")];
const result = closeToolLoopForThinking(contents);
const syntheticModel = result[result.length - 2];
expect(syntheticModel?.parts[0]?.text).toBe("[Processing previous context.]");
});
it("does not mutate original contents array", () => {
const contents = [userMsg("go"), modelWithToolCall(), toolResultMsg()];
const original = JSON.stringify(contents);
closeToolLoopForThinking(contents);
expect(JSON.stringify(contents)).toBe(original);
});
});
// ─── looksLikeCompactedThinkingTurn ──────────────────────────────────────────
describe("looksLikeCompactedThinkingTurn", () => {
it("returns false for null / undefined", () => {
expect(looksLikeCompactedThinkingTurn(null)).toBe(false);
expect(looksLikeCompactedThinkingTurn(undefined)).toBe(false);
});
it("returns false for message with no parts", () => {
expect(looksLikeCompactedThinkingTurn({ role: "model", parts: [] })).toBe(false);
});
it("returns false for message without function calls", () => {
expect(looksLikeCompactedThinkingTurn(modelMsg("just text"))).toBe(false);
});
it("returns false when message has thinking blocks alongside function call", () => {
const msg = {
role: "model",
parts: [
{ thought: true, text: "thinking" },
{ functionCall: { name: "t", args: {} } },
],
};
expect(looksLikeCompactedThinkingTurn(msg)).toBe(false);
});
it("returns false when text appears before function call (non-compacted)", () => {
const msg = {
role: "model",
parts: [
{ text: "I will now call the tool." },
{ functionCall: { name: "t", args: {} } },
],
};
expect(looksLikeCompactedThinkingTurn(msg)).toBe(false);
});
it("returns true for bare function call with no preceding text (looks compacted)", () => {
const msg = modelWithToolCall("search");
expect(looksLikeCompactedThinkingTurn(msg)).toBe(true);
});
});
// ─── hasPossibleCompactedThinking ────────────────────────────────────────────
describe("hasPossibleCompactedThinking", () => {
it("returns false for empty contents", () => {
expect(hasPossibleCompactedThinking([], 0)).toBe(false);
});
it("returns false for invalid turnStartIdx", () => {
expect(hasPossibleCompactedThinking([modelMsg("hi")], -1)).toBe(false);
});
it("returns false when no model messages look compacted", () => {
const contents = [userMsg("go"), modelWithThinkingAndToolCall(), toolResultMsg()];
expect(hasPossibleCompactedThinking(contents, 1)).toBe(false);
});
it("returns true when a model message in turn looks compacted", () => {
const contents = [
userMsg("go"),
modelWithToolCall("search"),
toolResultMsg("search"),
];
expect(hasPossibleCompactedThinking(contents, 1)).toBe(true);
});
it("ignores model messages before turnStartIdx", () => {
const contents = [
userMsg("first turn"),
modelWithToolCall("old"),
toolResultMsg("old"),
userMsg("second turn"),
modelWithThinkingAndToolCall("new"),
toolResultMsg("new"),
];
// turnStart is 4 (second model message)
expect(hasPossibleCompactedThinking(contents, 4)).toBe(false);
});
});