|
1 | 1 | // Full-entry coverage for retrying empty errored assistant turns. |
2 | | -import http from "node:http"; |
3 | | -import type { AddressInfo } from "node:net"; |
4 | 2 | import { afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest"; |
5 | | -import type { Context, Model } from "../../../packages/ai/src/types.js"; |
6 | 3 | import type { OpenClawTestState } from "../../test-utils/openclaw-test-state.js"; |
7 | 4 | import { makeAssistantMessageFixture } from "../test-helpers/assistant-message-fixtures.js"; |
8 | 5 | import { makeAttemptResult } from "./run.overflow-compaction.fixture.js"; |
@@ -113,129 +110,6 @@ describe("runEmbeddedAgent silent-error retry", () => { |
113 | 110 | expect(result.payloads).toBeUndefined(); |
114 | 111 | }); |
115 | 112 |
|
116 | | - it.each(["This request is not allowed.", "This request is refused; prompt is too long."])( |
117 | | - "surfaces a loopback Anthropic transport refusal without retrying: %s", |
118 | | - async (explanation) => { |
119 | | - let providerRequests = 0; |
120 | | - const server = http.createServer((request, response) => { |
121 | | - providerRequests += 1; |
122 | | - request.resume(); |
123 | | - response.writeHead(200, { "content-type": "text/event-stream" }); |
124 | | - response.end( |
125 | | - [ |
126 | | - { type: "message_start", message: { id: "msg_refusal", usage: {} } }, |
127 | | - { |
128 | | - type: "message_delta", |
129 | | - delta: { |
130 | | - stop_reason: "refusal", |
131 | | - stop_details: { |
132 | | - type: "refusal", |
133 | | - category: "reasoning_extraction", |
134 | | - explanation, |
135 | | - }, |
136 | | - }, |
137 | | - usage: { input_tokens: 3, output_tokens: 0 }, |
138 | | - }, |
139 | | - { type: "message_stop" }, |
140 | | - ] |
141 | | - .map((event) => `event: ${event.type}\ndata: ${JSON.stringify(event)}\n\n`) |
142 | | - .join(""), |
143 | | - ); |
144 | | - }); |
145 | | - await new Promise<void>((resolve, reject) => { |
146 | | - server.once("error", reject); |
147 | | - server.listen(0, "127.0.0.1", resolve); |
148 | | - }); |
149 | | - const { port } = server.address() as AddressInfo; |
150 | | - const model = { |
151 | | - id: "claude-opus-5", |
152 | | - name: "Claude Opus 5 refusal proof", |
153 | | - api: "anthropic-messages", |
154 | | - provider: "anthropic", |
155 | | - baseUrl: `http://127.0.0.1:${port}`, |
156 | | - reasoning: true, |
157 | | - input: ["text"], |
158 | | - cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, |
159 | | - contextWindow: 200_000, |
160 | | - maxTokens: 4_096, |
161 | | - } satisfies Model<"anthropic-messages">; |
162 | | - const context = { |
163 | | - messages: [{ role: "user", content: "hello", timestamp: 1 }], |
164 | | - } satisfies Context; |
165 | | - |
166 | | - try { |
167 | | - mockedRunEmbeddedAttempt.mockImplementation(async () => { |
168 | | - const { streamAnthropic } = |
169 | | - await import("../../../packages/ai/src/providers/anthropic.js"); |
170 | | - const stream = streamAnthropic(model, context, { apiKey: "redacted-fixture-token" }); |
171 | | - for await (const event of stream) { |
172 | | - void event; |
173 | | - } |
174 | | - const assistant = await stream.result(); |
175 | | - return makeAttemptResult({ |
176 | | - assistantTexts: [], |
177 | | - lastAssistant: assistant, |
178 | | - currentAttemptAssistant: assistant, |
179 | | - }); |
180 | | - }); |
181 | | - |
182 | | - const result = await runEmbeddedAgent({ |
183 | | - ...createOverflowRunParams(state), |
184 | | - provider: "anthropic", |
185 | | - model: "claude-opus-5", |
186 | | - runId: "run-anthropic-refusal", |
187 | | - }); |
188 | | - |
189 | | - expect(providerRequests).toBe(1); |
190 | | - expect(mockedRunEmbeddedAttempt).toHaveBeenCalledTimes(1); |
191 | | - expect(result.payloads?.[0]).toMatchObject({ |
192 | | - isError: true, |
193 | | - text: "The provider refused this request (category: reasoning_extraction).", |
194 | | - }); |
195 | | - expect(result.meta.error).toMatchObject({ |
196 | | - message: "The provider refused this request (category: reasoning_extraction).", |
197 | | - }); |
198 | | - } finally { |
199 | | - await new Promise<void>((resolve, reject) => { |
200 | | - server.close((error) => (error ? reject(error) : resolve())); |
201 | | - }); |
202 | | - } |
203 | | - }, |
204 | | - ); |
205 | | - |
206 | | - it("also treats Bedrock Anthropic refusal diagnostics as terminal", async () => { |
207 | | - const refusal = emptyErrorAttempt("amazon-bedrock", "claude-opus-5"); |
208 | | - const diagnostics = [ |
209 | | - { |
210 | | - type: "provider_refusal", |
211 | | - timestamp: 0, |
212 | | - details: { provider: "amazon-bedrock", category: "cyber" }, |
213 | | - }, |
214 | | - ]; |
215 | | - refusal.lastAssistant!.diagnostics = diagnostics; |
216 | | - refusal.currentAttemptAssistant!.diagnostics = diagnostics; |
217 | | - mockedRunEmbeddedAttempt.mockResolvedValueOnce(refusal); |
218 | | - mockedRunEmbeddedAttempt.mockResolvedValueOnce( |
219 | | - successAttempt("amazon-bedrock", "claude-opus-5"), |
220 | | - ); |
221 | | - |
222 | | - const result = await runEmbeddedAgent({ |
223 | | - ...createOverflowRunParams(state), |
224 | | - provider: "amazon-bedrock", |
225 | | - model: "claude-opus-5", |
226 | | - runId: "run-bedrock-refusal", |
227 | | - }); |
228 | | - |
229 | | - expect(mockedRunEmbeddedAttempt).toHaveBeenCalledTimes(1); |
230 | | - expect(result.payloads?.[0]).toMatchObject({ |
231 | | - isError: true, |
232 | | - text: "The provider refused this request (category: cyber).", |
233 | | - }); |
234 | | - expect(result.meta.error).toMatchObject({ |
235 | | - message: "The provider refused this request (category: cyber).", |
236 | | - }); |
237 | | - }); |
238 | | - |
239 | 113 | it("does not intercept concrete non-transient failover errors", async () => { |
240 | 114 | mockedClassifyFailoverReason.mockReturnValue("model_not_found"); |
241 | 115 | mockedRunEmbeddedAttempt.mockResolvedValueOnce( |
@@ -313,44 +187,39 @@ describe("runEmbeddedAgent silent-error retry", () => { |
313 | 187 | }); |
314 | 188 | }); |
315 | 189 | describe("current-assistant provenance", () => { |
316 | | - it.each([0, 1])( |
317 | | - "ignores a historical refusal when a later empty attempt reports %s compactions", |
318 | | - async (compactionCount) => { |
319 | | - const refusal = makeAssistantMessageFixture({ |
320 | | - api: "anthropic-messages", |
321 | | - provider: "anthropic", |
322 | | - model: "claude-opus-5", |
323 | | - stopReason: "error", |
324 | | - content: [], |
325 | | - errorMessage: "historical refusal", |
326 | | - diagnostics: [{ type: "provider_refusal", timestamp: 1, details: { category: "cyber" } }], |
327 | | - }); |
328 | | - mockedRunEmbeddedAttempt |
329 | | - .mockResolvedValueOnce( |
330 | | - makeAttemptResult({ |
331 | | - assistantTexts: [], |
332 | | - lastAssistant: refusal, |
333 | | - currentAttemptAssistant: undefined, |
334 | | - currentAttemptCompletedAssistant: undefined, |
335 | | - compactionCount, |
336 | | - }), |
337 | | - ) |
338 | | - .mockResolvedValueOnce(successAttempt("anthropic", "claude-opus-5")); |
| 190 | + it("ignores a historical refusal after compaction", async () => { |
| 191 | + const refusal = makeAssistantMessageFixture({ |
| 192 | + api: "anthropic-messages", |
| 193 | + provider: "anthropic", |
| 194 | + model: "claude-opus-5", |
| 195 | + stopReason: "error", |
| 196 | + content: [], |
| 197 | + errorMessage: "historical refusal", |
| 198 | + diagnostics: [{ type: "provider_refusal", timestamp: 1, details: { category: "cyber" } }], |
| 199 | + }); |
| 200 | + mockedRunEmbeddedAttempt |
| 201 | + .mockResolvedValueOnce( |
| 202 | + makeAttemptResult({ |
| 203 | + assistantTexts: [], |
| 204 | + lastAssistant: refusal, |
| 205 | + currentAttemptAssistant: undefined, |
| 206 | + currentAttemptCompletedAssistant: undefined, |
| 207 | + compactionCount: 1, |
| 208 | + }), |
| 209 | + ) |
| 210 | + .mockResolvedValueOnce(successAttempt("anthropic", "claude-opus-5")); |
339 | 211 |
|
340 | | - const result = await runEmbeddedAgent({ |
341 | | - ...createOverflowRunParams(state), |
342 | | - provider: "anthropic", |
343 | | - model: "claude-opus-5", |
344 | | - runId: `run-historical-refusal-empty-${compactionCount}`, |
345 | | - }); |
| 212 | + const result = await runEmbeddedAgent({ |
| 213 | + ...createOverflowRunParams(state), |
| 214 | + provider: "anthropic", |
| 215 | + model: "claude-opus-5", |
| 216 | + runId: "run-historical-refusal-after-compaction", |
| 217 | + }); |
346 | 218 |
|
347 | | - expect(mockedRunEmbeddedAttempt).toHaveBeenCalledTimes(2); |
348 | | - expect(result.meta.error).toBeUndefined(); |
349 | | - expect(result.payloads?.some((payload) => payload.text?.includes("refused"))).not.toBe( |
350 | | - true, |
351 | | - ); |
352 | | - }, |
353 | | - ); |
| 219 | + expect(mockedRunEmbeddedAttempt).toHaveBeenCalledTimes(2); |
| 220 | + expect(result.meta.error).toBeUndefined(); |
| 221 | + expect(result.payloads?.some((payload) => payload.text?.includes("refused"))).not.toBe(true); |
| 222 | + }); |
354 | 223 |
|
355 | 224 | it("preserves a completed current refusal after transcript projection removes its slice", async () => { |
356 | 225 | const refusal = makeAssistantMessageFixture({ |
|
0 commit comments