Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

test(opencode): surface provider error events in the door test (#6049)

garrytana8f8fb0ac2bf
diff --git a/test/e2e/install-real-opencode.serial.test.ts b/test/e2e/install-real-opencode.serial.test.ts
index 19cad52507..f9e0b4e59c 100644
--- a/test/e2e/install-real-opencode.serial.test.ts
+++ b/test/e2e/install-real-opencode.serial.test.ts
@@ -439,6 +439,7 @@ describe.skipIf(!CAN_RUN_KEYLESS)('install real-opencode door — keyless tier (
// a transient 5xx must not red the door; asserts NEVER soften.
let finalText = '';
let toolCalls: string[] = [];
+ let errors: string[] = [];
for (let attempt = 1; attempt <= 2; attempt++) {
const turn = await opencodeOneShotTurn({
prompt,
@@ -451,11 +452,14 @@ describe.skipIf(!CAN_RUN_KEYLESS)('install real-opencode door — keyless tier (
const parsed = parseOpencodeJsonl(turn.finalText.split('\n'));
finalText = parsed.finalText;
toolCalls = parsed.toolCalls;
+ errors = parsed.errors;
try { writeFileSync(join(home, 'door-turn.jsonl'), turn.finalText); } catch { /* evidence */ }
if (turn.exitCode === 0 && finalText.includes(nonce)) break;
if (attempt === 1) await new Promise((r) => setTimeout(r, 3_000));
}
+ // #6049: a turn the provider refused (e.g. FreeTierError 403) fails with its own message, not `Received: ""`.
+ if (!finalText.includes(nonce) && errors.length) throw new Error(`opencode turn ended with provider error(s): ${errors.join('; ')}`);
expect(finalText).toContain(nonce);
expect(finalText).not.toContain('NO-GBRAIN-TOOL');
// Structural tool-mediation proof: at least one gbrain MCP tool fired
diff --git a/test/helpers/agent-harness.ts b/test/helpers/agent-harness.ts
index 5a26c15905..8cd2fa7137 100644
--- a/test/helpers/agent-harness.ts
+++ b/test/helpers/agent-harness.ts
@@ -421,18 +421,23 @@ export interface ParsedOpencodeJsonl {
/** Tool names from tool events, in order (MCP tools: `<server>_<tool>`,
* e.g. `gbrain_recall` — observed v1.18.18). */
toolCalls: string[];
+ /** #6049: one line per `error` event: `<name> <statusCode> <responseBody error.type>: <message>`,
+ * so a turn the provider refused (e.g. a FreeTierError 403) says why instead of an empty reply. */
+ errors: string[];
}
/**
* Parse `opencode run --format json` NDJSON. Every event is
* `{type, timestamp, sessionID, part}` (observed v1.18.18 —
* OPENCODE-CLI-PIN.md §One-shot): `text` events carry `part.text`;
- * `tool_use` events carry `part.tool` + `part.state.{status,input,output}`.
+ * `tool_use` events carry `part.tool` + `part.state.{status,input,output}`;
+ * `error` events carry `error.{name,data.{message,statusCode,responseBody}}`.
* Skips malformed lines and unknown event types.
*/
export function parseOpencodeJsonl(lines: string[]): ParsedOpencodeJsonl {
const textParts: string[] = [];
const toolCalls: string[] = [];
+ const errors: string[] = [];
for (const line of lines) {
let evt: unknown;
try {
@@ -441,14 +446,22 @@ export function parseOpencodeJsonl(lines: string[]): ParsedOpencodeJsonl {
continue;
}
if (typeof evt !== 'object' || evt === null) continue;
- const e = evt as { type?: string; part?: { text?: unknown; tool?: unknown } };
+ const e = evt as { type?: string; part?: { text?: unknown; tool?: unknown };
+ error?: { name?: unknown; data?: { message?: unknown; statusCode?: unknown; responseBody?: unknown } } };
if (e.type === 'text' && typeof e.part?.text === 'string') {
textParts.push(e.part.text);
} else if (e.type === 'tool_use' && typeof e.part?.tool === 'string') {
toolCalls.push(e.part.tool);
+ } else if (e.type === 'error') {
+ const data = e.error?.data;
+ let bodyType: unknown;
+ try { bodyType = typeof data?.responseBody === 'string' ? JSON.parse(data.responseBody)?.error?.type : undefined; } catch { /* non-JSON body */ }
+ const head = [e.error?.name, data?.statusCode, bodyType].filter((v) => typeof v === 'string' || typeof v === 'number').join(' ');
+ const message = typeof data?.message === 'string' ? data.message : '';
+ errors.push(head && message ? `${head}: ${message}` : head || message || 'error event without details');
}
}
- return { finalText: textParts.join('\n').trim(), toolCalls };
+ return { finalText: textParts.join('\n').trim(), toolCalls, errors };
}
// ────────────────────────────────────────────────────────────────────────────
diff --git a/test/helpers/agent-harness.unit.test.ts b/test/helpers/agent-harness.unit.test.ts
index 11b6807176..5bd4689394 100644
--- a/test/helpers/agent-harness.unit.test.ts
+++ b/test/helpers/agent-harness.unit.test.ts
@@ -634,8 +634,27 @@ describe('parseOpencodeJsonl (pure — fixtures pinned to observed v1.18.18 shap
});
test('empty/garbage input → empty result, never throws', () => {
- expect(parseOpencodeJsonl([])).toEqual({ finalText: '', toolCalls: [] });
- expect(parseOpencodeJsonl(['{', 'null', '42'])).toEqual({ finalText: '', toolCalls: [] });
+ expect(parseOpencodeJsonl([])).toEqual({ finalText: '', toolCalls: [], errors: [] });
+ expect(parseOpencodeJsonl(['{', 'null', '42'])).toEqual({ finalText: '', toolCalls: [], errors: [] });
+ });
+
+ test('#6049: a provider error event is collected with its status, name and response error type', () => {
+ const line = JSON.stringify({ type: 'error', error: { name: 'APIError', data: {
+ message: "OpenCode's free tier can only be used from within OpenCode. Download it at https://opencode.ai and keep building.",
+ statusCode: 403, isRetryable: false,
+ responseBody: JSON.stringify({ type: 'error', error: { type: 'FreeTierError', message: '...' } }),
+ metadata: { url: 'https://opencode.ai/zen/v1/chat/completions' },
+ } } });
+ const parsed = parseOpencodeJsonl([line]);
+ expect(parsed.finalText).toBe('');
+ expect(parsed.errors).toEqual(["APIError 403 FreeTierError: OpenCode's free tier can only be used from within OpenCode. Download it at https://opencode.ai and keep building."]);
+ });
+
+ test('#6049: an error event without data or with an unparsable response body still names what it can', () => {
+ expect(parseOpencodeJsonl([JSON.stringify({ type: 'error', error: { name: 'UnknownError' } })]).errors).toEqual(['UnknownError']);
+ expect(parseOpencodeJsonl([JSON.stringify({ type: 'error', error: { name: 'APIError', data: { message: 'upstream 502', statusCode: 502, responseBody: '<html>' } } })]).errors)
+ .toEqual(['APIError 502: upstream 502']);
+ expect(parseOpencodeJsonl([JSON.stringify({ type: 'error' })]).errors).toEqual(['error event without details']);
});
});