|
1 | 1 | // Provider thinking replay repair tests cover durable transcript cleanup after |
2 | 2 | // Anthropic/Bedrock proves a signed thinking block invalid. |
3 | | -import fs from "node:fs/promises"; |
4 | | -import os from "node:os"; |
5 | | -import path from "node:path"; |
6 | 3 | import type { AgentMessage } from "openclaw/plugin-sdk/agent-core"; |
7 | 4 | import { SessionManager } from "openclaw/plugin-sdk/agent-sessions"; |
8 | | -import type { Model } from "openclaw/plugin-sdk/llm"; |
9 | | -import { describe, expect, it, vi } from "vitest"; |
10 | | -import { formatSqliteSessionFileMarker } from "../../config/sessions/legacy-sqlite-marker.js"; |
11 | | -import { replaceSessionEntry } from "../../config/sessions/session-accessor.js"; |
12 | | -import type { SessionEntry } from "../../config/sessions/types.js"; |
13 | | -import { testing } from "../openai-transport-stream.test-support.js"; |
14 | | -import { convertToLlm } from "../sessions/messages.js"; |
| 5 | +import { describe, expect, it } from "vitest"; |
15 | 6 | import { |
16 | 7 | repairRejectedCompactionReplayInSessionManager, |
17 | 8 | repairRejectedThinkingReplayInSessionManager, |
@@ -189,153 +180,4 @@ describe("repairRejectedCompactionReplayInSessionManager", () => { |
189 | 180 | .map((message) => message.providerReplay), |
190 | 181 | ).toEqual([undefined, undefined]); |
191 | 182 | }); |
192 | | - |
193 | | - it("removes a rejected checkpoint from the reopened SQLite branch", async () => { |
194 | | - const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-compaction-repair-")); |
195 | | - try { |
196 | | - const storePath = path.join(dir, "sessions.json"); |
197 | | - const sessionId = "compaction-repair-session"; |
198 | | - const sessionKey = "agent:main:compaction-repair"; |
199 | | - const sessionFile = formatSqliteSessionFileMarker({ |
200 | | - agentId: "main", |
201 | | - sessionId, |
202 | | - storePath, |
203 | | - }); |
204 | | - const target = { agentId: "main", sessionId, sessionKey, storePath }; |
205 | | - await replaceSessionEntry({ sessionKey, storePath }, { |
206 | | - sessionFile, |
207 | | - sessionId, |
208 | | - updatedAt: 10, |
209 | | - } as SessionEntry); |
210 | | - |
211 | | - const model = { |
212 | | - id: "gpt-5.5", |
213 | | - name: "GPT-5.5", |
214 | | - api: "openai-responses", |
215 | | - provider: "openai", |
216 | | - baseUrl: "https://api.openai.com/v1", |
217 | | - reasoning: true, |
218 | | - input: ["text"], |
219 | | - cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, |
220 | | - contextWindow: 128_000, |
221 | | - maxTokens: 8_192, |
222 | | - } satisfies Model<"openai-responses">; |
223 | | - const identity = { sessionId, authProfileId: "profile-a" }; |
224 | | - const replayContext = testing.buildOpenAIResponsesReasoningReplayMetadata(model, identity); |
225 | | - const checkpointContext = { |
226 | | - provider: replayContext.provider, |
227 | | - api: replayContext.api, |
228 | | - model: replayContext.model, |
229 | | - baseUrlHash: replayContext.baseUrlHash, |
230 | | - sessionHash: replayContext.sessionHash, |
231 | | - authProfileHash: replayContext.authProfileHash, |
232 | | - }; |
233 | | - const sessionManager = SessionManager.open(target, dir); |
234 | | - sessionManager.appendMessage( |
235 | | - asAppendMessage({ role: "user", content: "full history prefix", timestamp: 1 }), |
236 | | - ); |
237 | | - sessionManager.appendMessage( |
238 | | - asAppendMessage({ |
239 | | - role: "assistant", |
240 | | - content: [{ type: "toolCall", id: "call_1", name: "read", arguments: {} }], |
241 | | - api: model.api, |
242 | | - provider: model.provider, |
243 | | - model: model.id, |
244 | | - stopReason: "toolUse", |
245 | | - timestamp: 2, |
246 | | - providerReplay: { |
247 | | - v: 1, |
248 | | - type: "openai-responses-compaction", |
249 | | - id: "cmp_rejected", |
250 | | - data: "rejected-compaction-ciphertext", |
251 | | - replayIndex: 0, |
252 | | - ...checkpointContext, |
253 | | - }, |
254 | | - }), |
255 | | - ); |
256 | | - sessionManager.appendMessage( |
257 | | - asAppendMessage({ |
258 | | - role: "toolResult", |
259 | | - toolCallId: "call_1", |
260 | | - toolName: "read", |
261 | | - content: [{ type: "text", text: "result" }], |
262 | | - isError: false, |
263 | | - timestamp: 3, |
264 | | - }), |
265 | | - ); |
266 | | - sessionManager.appendMessage( |
267 | | - asAppendMessage({ role: "user", content: "current turn", timestamp: 4 }), |
268 | | - ); |
269 | | - |
270 | | - const invalidEncryptedContent = Object.assign(new Error("invalid encrypted content"), { |
271 | | - code: "invalid_encrypted_content", |
272 | | - }); |
273 | | - const retryStream = { |
274 | | - [Symbol.asyncIterator]() { |
275 | | - return { |
276 | | - next: async () => { |
277 | | - throw new Error("retry stream failed"); |
278 | | - }, |
279 | | - }; |
280 | | - }, |
281 | | - }; |
282 | | - const create = vi |
283 | | - .fn() |
284 | | - .mockReturnValueOnce({ withResponse: vi.fn().mockRejectedValue(invalidEncryptedContent) }) |
285 | | - .mockReturnValueOnce({ |
286 | | - withResponse: vi.fn().mockResolvedValue({ |
287 | | - data: retryStream, |
288 | | - response: new Response(null, { status: 200 }), |
289 | | - }), |
290 | | - }); |
291 | | - let repairResult: |
292 | | - | ReturnType<typeof repairRejectedCompactionReplayInSessionManager> |
293 | | - | undefined; |
294 | | - const recovery = await testing.createResponsesStreamWithEncryptedContentRetry({ |
295 | | - client: { responses: { create } } as never, |
296 | | - request: { |
297 | | - model: model.id, |
298 | | - stream: true, |
299 | | - input: [ |
300 | | - { |
301 | | - type: "compaction", |
302 | | - id: "cmp_rejected", |
303 | | - encrypted_content: "rejected-compaction-ciphertext", |
304 | | - }, |
305 | | - ], |
306 | | - }, |
307 | | - requestOptions: undefined, |
308 | | - model, |
309 | | - onCompactionRejected: (checkpoint) => { |
310 | | - repairResult = repairRejectedCompactionReplayInSessionManager({ |
311 | | - sessionManager, |
312 | | - checkpoint, |
313 | | - }); |
314 | | - }, |
315 | | - }); |
316 | | - await expect(recovery.stream[Symbol.asyncIterator]().next()).rejects.toThrow( |
317 | | - "retry stream failed", |
318 | | - ); |
319 | | - expect(repairResult).toMatchObject({ repaired: true, repairedCount: 1 }); |
320 | | - |
321 | | - const reopened = SessionManager.open(target, dir); |
322 | | - const messages = reopened.buildSessionContext().messages; |
323 | | - expect( |
324 | | - messages.find((message) => message.role === "assistant")?.providerReplay, |
325 | | - ).toBeUndefined(); |
326 | | - const request = testing.buildOpenAIResponsesParams( |
327 | | - model, |
328 | | - { systemPrompt: "system", messages: convertToLlm(messages) }, |
329 | | - identity, |
330 | | - ); |
331 | | - expect(request.input.some((item) => item.type === "compaction")).toBe(false); |
332 | | - const encoded = JSON.stringify(request.input); |
333 | | - expect(encoded).not.toContain("rejected-compaction-ciphertext"); |
334 | | - expect(encoded).toContain("full history prefix"); |
335 | | - expect(encoded).toContain("function_call"); |
336 | | - expect(encoded).toContain("function_call_output"); |
337 | | - } finally { |
338 | | - await fs.rm(dir, { recursive: true, force: true }); |
339 | | - } |
340 | | - }); |
341 | 183 | }); |
0 commit comments