|
| 1 | +/** |
| 2 | + * Channel 3 proof for an OpenAI-compatible SSE error before the first chunk. |
| 3 | + * |
| 4 | + * The first request returns a protocol-level `event: error` carrying the exact |
| 5 | + * DigitalOcean message. The second request returns one successful completion. |
| 6 | + * The real source CLI must retry inside the provider adapter and print exactly |
| 7 | + * one final marker. |
| 8 | + */ |
| 9 | + |
| 10 | +import { createServer } from "node:http"; |
| 11 | +import { writeFileSync } from "node:fs"; |
| 12 | +import { join } from "node:path"; |
| 13 | +import { |
| 14 | + createChecks, |
| 15 | + evidenceDir, |
| 16 | + guardRealAuth, |
| 17 | + installCleanupHooks, |
| 18 | + makeSandbox, |
| 19 | + runCli, |
| 20 | +} from "./lib/common.mjs"; |
| 21 | +import { |
| 22 | + API_PRESETS, |
| 23 | + checkRealAuthUnchanged, |
| 24 | + hermeticEnv, |
| 25 | + writeMockModelsJson, |
| 26 | +} from "./lib/mock-loop-support.mjs"; |
| 27 | + |
| 28 | +const ERROR_MESSAGE = "Upstream error from DigitalOcean: stream failed"; |
| 29 | +const FINAL_MARKER = "SENPI-QA-DIGITALOCEAN-STREAM-RECOVERED-7a4f"; |
| 30 | +const EVIDENCE_SLUG = "digitalocean-stream-retry"; |
| 31 | + |
| 32 | +function startServer() { |
| 33 | + const requests = []; |
| 34 | + let attempts = 0; |
| 35 | + const server = createServer((request, response) => { |
| 36 | + const chunks = []; |
| 37 | + request.on("data", (chunk) => chunks.push(chunk)); |
| 38 | + request.on("end", () => { |
| 39 | + attempts++; |
| 40 | + requests.push({ |
| 41 | + attempt: attempts, |
| 42 | + url: request.url, |
| 43 | + body: Buffer.concat(chunks).toString("utf8"), |
| 44 | + }); |
| 45 | + response.writeHead(200, { |
| 46 | + "content-type": "text/event-stream", |
| 47 | + "cache-control": "no-cache", |
| 48 | + connection: "keep-alive", |
| 49 | + }); |
| 50 | + if (attempts === 1) { |
| 51 | + response.end(`event: error\ndata: ${JSON.stringify({ error: { message: ERROR_MESSAGE, type: "server_error" } })}\n\n`); |
| 52 | + return; |
| 53 | + } |
| 54 | + const base = { |
| 55 | + id: "chatcmpl-digitalocean-retry", |
| 56 | + object: "chat.completion.chunk", |
| 57 | + created: 0, |
| 58 | + model: API_PRESETS["openai-completions"].modelId, |
| 59 | + }; |
| 60 | + const send = (delta, finishReason = null) => { |
| 61 | + response.write( |
| 62 | + `data: ${JSON.stringify({ ...base, choices: [{ index: 0, delta, finish_reason: finishReason }] })}\n\n`, |
| 63 | + ); |
| 64 | + }; |
| 65 | + send({ role: "assistant", content: "" }); |
| 66 | + send({ content: FINAL_MARKER }); |
| 67 | + send({}, "stop"); |
| 68 | + response.write( |
| 69 | + `data: ${JSON.stringify({ |
| 70 | + ...base, |
| 71 | + choices: [], |
| 72 | + usage: { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 }, |
| 73 | + })}\n\n`, |
| 74 | + ); |
| 75 | + response.end("data: [DONE]\n\n"); |
| 76 | + }); |
| 77 | + }); |
| 78 | + return new Promise((resolve, reject) => { |
| 79 | + server.once("error", reject); |
| 80 | + server.listen(0, "127.0.0.1", () => { |
| 81 | + const address = server.address(); |
| 82 | + if (!address || typeof address === "string") { |
| 83 | + reject(new Error("Failed to resolve fake server port")); |
| 84 | + return; |
| 85 | + } |
| 86 | + const origin = `http://127.0.0.1:${address.port}`; |
| 87 | + resolve({ |
| 88 | + url: `${origin}/v1`, |
| 89 | + origin, |
| 90 | + port: address.port, |
| 91 | + requests, |
| 92 | + stop: () => new Promise((done) => server.close(done)), |
| 93 | + }); |
| 94 | + }); |
| 95 | + }); |
| 96 | +} |
| 97 | + |
| 98 | +async function main() { |
| 99 | + installCleanupHooks(); |
| 100 | + const checks = createChecks("mock-loop-stream-retry.mjs"); |
| 101 | + const guard = guardRealAuth(); |
| 102 | + const box = makeSandbox("mock-loop-digitalocean-stream-retry"); |
| 103 | + const server = await startServer(); |
| 104 | + try { |
| 105 | + writeMockModelsJson(box.agentDir, server, "openai-completions", {}, { |
| 106 | + retry: { |
| 107 | + enabled: false, |
| 108 | + maxRetries: 0, |
| 109 | + baseDelayMs: 0, |
| 110 | + provider: { maxRetries: 1, maxRetryDelayMs: 60000 }, |
| 111 | + }, |
| 112 | + }); |
| 113 | + const preset = API_PRESETS["openai-completions"]; |
| 114 | + const result = await runCli( |
| 115 | + [ |
| 116 | + "--provider", |
| 117 | + preset.provider, |
| 118 | + "--model", |
| 119 | + preset.modelId, |
| 120 | + "--no-context-files", |
| 121 | + "--no-extensions", |
| 122 | + "--print", |
| 123 | + `Return ${FINAL_MARKER} after the transient stream error.`, |
| 124 | + ], |
| 125 | + { env: hermeticEnv(box.env), cwd: box.cwd, timeoutMs: 60000 }, |
| 126 | + ); |
| 127 | + const combined = `${result.stdout}\n${result.stderr}`; |
| 128 | + const finalCount = combined.split(FINAL_MARKER).length - 1; |
| 129 | + const pass = result.code === 0 && !result.timedOut && server.requests.length === 2 && finalCount === 1; |
| 130 | + checks.ok( |
| 131 | + "real CLI retries the literal pre-output stream failure exactly once", |
| 132 | + pass, |
| 133 | + `code=${result.code} requests=${server.requests.length} finalCount=${finalCount}`, |
| 134 | + ); |
| 135 | + checkRealAuthUnchanged(checks, guard); |
| 136 | + const dir = evidenceDir(EVIDENCE_SLUG); |
| 137 | + writeFileSync(join(dir, "mock-loop-stream-retry-stdout.txt"), result.stdout); |
| 138 | + writeFileSync(join(dir, "mock-loop-stream-retry-stderr.txt"), result.stderr); |
| 139 | + writeFileSync( |
| 140 | + join(dir, "mock-loop-stream-retry-summary.json"), |
| 141 | + `${JSON.stringify( |
| 142 | + { |
| 143 | + command: "node .agents/skills/senpi-qa/scripts/mock-loop-stream-retry.mjs", |
| 144 | + literalError: ERROR_MESSAGE, |
| 145 | + exitCode: result.code, |
| 146 | + timedOut: result.timedOut, |
| 147 | + serverPort: server.port, |
| 148 | + sandboxDir: box.dir, |
| 149 | + requests: server.requests.length, |
| 150 | + finalMarkerCount: finalCount, |
| 151 | + pass, |
| 152 | + }, |
| 153 | + null, |
| 154 | + 2, |
| 155 | + )}\n`, |
| 156 | + ); |
| 157 | + process.exitCode = checks.finish() ? 0 : 1; |
| 158 | + } finally { |
| 159 | + await server.stop(); |
| 160 | + box.cleanup(); |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +main().catch((error) => { |
| 165 | + process.stderr.write(`${error instanceof Error ? error.stack : String(error)}\n`); |
| 166 | + process.exit(1); |
| 167 | +}); |
0 commit comments