|
| 1 | +import { describe, expect, it } from "bun:test"; |
| 2 | +import { createBuiltinRegistry } from "@intx/inference/providers"; |
| 3 | +import type { ConversationTurn, LastCycleSource } from "@intx/types/runtime"; |
| 4 | +import { sanitizeReplayTurns, withReplaySanitizer } from "./replay-sanitizer.js"; |
| 5 | + |
| 6 | +const GROK_SIGNATURE = "grok-opaque-signature-blob"; |
| 7 | + |
| 8 | +function grokThinkingHistory(): ConversationTurn[] { |
| 9 | + return [ |
| 10 | + { |
| 11 | + role: "user", |
| 12 | + content: [{ type: "text", text: "hello" }], |
| 13 | + timestamp: 1, |
| 14 | + }, |
| 15 | + { |
| 16 | + role: "assistant", |
| 17 | + model: "grok-4", |
| 18 | + content: [ |
| 19 | + { type: "thinking", thinking: "pondering", signature: GROK_SIGNATURE }, |
| 20 | + { type: "text", text: "answer", signature: GROK_SIGNATURE }, |
| 21 | + ], |
| 22 | + timestamp: 2, |
| 23 | + }, |
| 24 | + { |
| 25 | + role: "user", |
| 26 | + content: [{ type: "text", text: "continue" }], |
| 27 | + timestamp: 3, |
| 28 | + }, |
| 29 | + ]; |
| 30 | +} |
| 31 | + |
| 32 | +function resolveSanitized(source: LastCycleSource) { |
| 33 | + return withReplaySanitizer(createBuiltinRegistry()).resolve(source); |
| 34 | +} |
| 35 | + |
| 36 | +describe("sanitizeReplayTurns", () => { |
| 37 | + it("strips foreign thinking blocks and signatures", () => { |
| 38 | + const turns = sanitizeReplayTurns(grokThinkingHistory(), "claude-opus-4"); |
| 39 | + const assistant = turns.find((t) => t.role === "assistant"); |
| 40 | + expect(assistant).toBeDefined(); |
| 41 | + expect(assistant?.content.some((b) => b.type === "thinking")).toBe(false); |
| 42 | + expect(JSON.stringify(turns)).not.toContain(GROK_SIGNATURE); |
| 43 | + }); |
| 44 | + |
| 45 | + it("keeps thinking blocks for same-model replay", () => { |
| 46 | + const turns = sanitizeReplayTurns(grokThinkingHistory(), "grok-4"); |
| 47 | + const assistant = turns.find((t) => t.role === "assistant"); |
| 48 | + expect(assistant?.content.some((b) => b.type === "thinking")).toBe(true); |
| 49 | + }); |
| 50 | + |
| 51 | + it("converts foreign refusal blocks to text", () => { |
| 52 | + const turns = sanitizeReplayTurns( |
| 53 | + [ |
| 54 | + { |
| 55 | + role: "assistant", |
| 56 | + model: "gpt-5", |
| 57 | + content: [{ type: "refusal", reason: "cannot comply" }], |
| 58 | + timestamp: 1, |
| 59 | + }, |
| 60 | + ], |
| 61 | + "claude-opus-4", |
| 62 | + ); |
| 63 | + expect(turns[0]?.content).toEqual([{ type: "text", text: "cannot comply" }]); |
| 64 | + }); |
| 65 | + |
| 66 | + it("drops foreign redacted_thinking and citation blocks", () => { |
| 67 | + const turns = sanitizeReplayTurns( |
| 68 | + [ |
| 69 | + { |
| 70 | + role: "assistant", |
| 71 | + model: "claude-opus-4", |
| 72 | + content: [ |
| 73 | + { type: "redacted_thinking", data: "opaque" }, |
| 74 | + { type: "text", text: "cited answer" }, |
| 75 | + { type: "citation", citedText: "quote", source: {} }, |
| 76 | + ], |
| 77 | + timestamp: 1, |
| 78 | + }, |
| 79 | + ], |
| 80 | + "gemini-2.5-pro", |
| 81 | + ); |
| 82 | + expect(turns[0]?.content).toEqual([{ type: "text", text: "cited answer" }]); |
| 83 | + }); |
| 84 | + |
| 85 | + it("answers dangling tool_calls with a synthetic error result", () => { |
| 86 | + const turns = sanitizeReplayTurns( |
| 87 | + [ |
| 88 | + { |
| 89 | + role: "assistant", |
| 90 | + model: "grok-4", |
| 91 | + content: [ |
| 92 | + { type: "text", text: "running tool" }, |
| 93 | + { type: "tool_call", id: "call_1", name: "ls", arguments: {} }, |
| 94 | + ], |
| 95 | + timestamp: 1, |
| 96 | + }, |
| 97 | + ], |
| 98 | + "claude-opus-4", |
| 99 | + ); |
| 100 | + const results = turns.flatMap((t) => t.content.filter((b) => b.type === "tool_result")); |
| 101 | + expect(results).toHaveLength(1); |
| 102 | + expect(results[0]).toMatchObject({ callId: "call_1", isError: true }); |
| 103 | + }); |
| 104 | +}); |
| 105 | + |
| 106 | +describe("withReplaySanitizer", () => { |
| 107 | + it("builds an Anthropic request from a grok-signed thinking turn", () => { |
| 108 | + const adapter = resolveSanitized({ |
| 109 | + sourceId: "s1", |
| 110 | + provider: "anthropic", |
| 111 | + model: "claude-opus-4", |
| 112 | + }); |
| 113 | + const request = adapter.buildRequest(grokThinkingHistory(), "claude-opus-4", {}); |
| 114 | + expect(request.body).not.toContain(GROK_SIGNATURE); |
| 115 | + expect(request.body).not.toContain('"thinking"'); |
| 116 | + }); |
| 117 | + |
| 118 | + it("builds a Google request from a grok-signed thinking turn", () => { |
| 119 | + const adapter = resolveSanitized({ |
| 120 | + sourceId: "s1", |
| 121 | + provider: "google-genai", |
| 122 | + model: "gemini-2.5-pro", |
| 123 | + }); |
| 124 | + const request = adapter.buildRequest(grokThinkingHistory(), "gemini-2.5-pro", {}); |
| 125 | + expect(request.body).not.toContain(GROK_SIGNATURE); |
| 126 | + expect(request.body).not.toContain("thoughtSignature"); |
| 127 | + }); |
| 128 | + |
| 129 | + it("builds an Anthropic request from a persisted refusal block", () => { |
| 130 | + const adapter = resolveSanitized({ |
| 131 | + sourceId: "s1", |
| 132 | + provider: "anthropic", |
| 133 | + model: "claude-opus-4", |
| 134 | + }); |
| 135 | + const request = adapter.buildRequest( |
| 136 | + [ |
| 137 | + { |
| 138 | + role: "user", |
| 139 | + content: [{ type: "text", text: "do it" }], |
| 140 | + timestamp: 1, |
| 141 | + }, |
| 142 | + { |
| 143 | + role: "assistant", |
| 144 | + model: "gpt-5", |
| 145 | + content: [{ type: "refusal", reason: "cannot comply" }], |
| 146 | + timestamp: 2, |
| 147 | + }, |
| 148 | + { |
| 149 | + role: "user", |
| 150 | + content: [{ type: "text", text: "why not" }], |
| 151 | + timestamp: 3, |
| 152 | + }, |
| 153 | + ], |
| 154 | + "claude-opus-4", |
| 155 | + {}, |
| 156 | + ); |
| 157 | + expect(request.body).toContain("cannot comply"); |
| 158 | + }); |
| 159 | + |
| 160 | + it("builds an Anthropic request from a dangling tool_call", () => { |
| 161 | + const adapter = resolveSanitized({ |
| 162 | + sourceId: "s1", |
| 163 | + provider: "anthropic", |
| 164 | + model: "claude-opus-4", |
| 165 | + }); |
| 166 | + const request = adapter.buildRequest( |
| 167 | + [ |
| 168 | + { |
| 169 | + role: "user", |
| 170 | + content: [{ type: "text", text: "list files" }], |
| 171 | + timestamp: 1, |
| 172 | + }, |
| 173 | + { |
| 174 | + role: "assistant", |
| 175 | + model: "grok-4", |
| 176 | + content: [ |
| 177 | + { type: "text", text: "running tool" }, |
| 178 | + { type: "tool_call", id: "call_1", name: "ls", arguments: {} }, |
| 179 | + ], |
| 180 | + timestamp: 2, |
| 181 | + }, |
| 182 | + ], |
| 183 | + "claude-opus-4", |
| 184 | + {}, |
| 185 | + ); |
| 186 | + expect(request.body).toContain("tool_result"); |
| 187 | + expect(request.body).toContain("call_1"); |
| 188 | + }); |
| 189 | +}); |
0 commit comments