fix: handle ambiguous prompt transport failures

This commit is contained in:
Bohdan Triapitsyn
2026-07-07 19:49:11 +03:00
parent c801c7a74b
commit 40dfff4a9a
12 changed files with 456 additions and 100 deletions
+69 -1
View File
@@ -1,4 +1,4 @@
import { describe, expect, mock, test } from 'bun:test';
import { beforeEach, describe, expect, mock, test } from 'bun:test';
type ConfigResponse = { data: Record<string, unknown> };
@@ -6,6 +6,15 @@ type ConfigResponse = { data: Record<string, unknown> };
const configResolvers: Array<(response: ConfigResponse) => void> = [];
let configCalls = 0;
const promptAsyncCalls: unknown[][] = [];
const promptAsyncResults: Array<unknown> = [];
const promptAsyncMock = mock(async (...args: unknown[]) => {
promptAsyncCalls.push(args);
const next = promptAsyncResults.shift();
if (next instanceof Error) throw next;
return next ?? { response: new Response(null, { status: 200 }) };
});
mock.module('@opencode-ai/sdk/v2', () => ({
createOpencodeClient: mock(() => ({
@@ -17,6 +26,9 @@ mock.module('@opencode-ai/sdk/v2', () => ({
});
}),
},
session: {
promptAsync: promptAsyncMock,
},
})),
}));
@@ -47,6 +59,11 @@ mock.module('@/lib/startupTrace', () => ({
const { opencodeClient } = await import(`./client?cache-test=${Date.now()}`);
beforeEach(() => {
promptAsyncCalls.length = 0;
promptAsyncResults.length = 0;
});
describe('opencodeClient getConfig cache', () => {
test('cleared stale in-flight requests do not repopulate cache or delete newer in-flight requests', async () => {
const first = opencodeClient.getConfig('/workspace/project');
@@ -72,3 +89,54 @@ describe('opencodeClient getConfig cache', () => {
expect(configCalls).toBe(2);
});
});
describe('opencodeClient prompt retry behavior', () => {
const sendPrompt = (providerID = 'anthropic') => opencodeClient.sendMessage({
id: 'ses_1',
providerID,
modelID: 'claude-sonnet',
text: 'hello',
});
test('does not retry 504 prompt responses because the POST may already be accepted', async () => {
promptAsyncResults.push({ response: new Response('gateway timeout', { status: 504 }) });
let error: unknown = null;
try {
await sendPrompt('anthropic-504');
} catch (caught) {
error = caught;
}
expect(promptAsyncCalls.length).toBe(1);
expect(error instanceof Error ? error.message : String(error)).toContain('Failed to send message (504)');
});
test('does not retry transport failures because the tunnel may have lost only the response', async () => {
promptAsyncResults.push(new TypeError('Failed to fetch'));
let error: unknown = null;
try {
await sendPrompt('anthropic-network');
} catch (caught) {
error = caught;
}
expect(promptAsyncCalls.length).toBe(1);
expect(error instanceof Error ? error.message : String(error)).toContain('Failed to fetch');
});
test('does not retry 503 prompt responses because proxy errors can be ambiguous too', async () => {
promptAsyncResults.push({ response: new Response('starting', { status: 503 }) });
let error: unknown = null;
try {
await sendPrompt('anthropic-503');
} catch (caught) {
error = caught;
}
expect(promptAsyncCalls.length).toBe(1);
expect(error instanceof Error ? error.message : String(error)).toContain('Failed to send message (503)');
});
});