|
| 1 | +import { afterEach, beforeEach, describe, expect, test } from "bun:test"; |
| 2 | +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { handleResponses } from "../../src/server/responses/core"; |
| 6 | +import { resetReasoningMetadataCachesForTests } from "../../src/providers/reasoning-metadata"; |
| 7 | +import type { RequestLogContext } from "../../src/server/request-log"; |
| 8 | +import type { OcxConfig } from "../../src/types"; |
| 9 | + |
| 10 | +/** |
| 11 | + * Rejected-rung learning on the request path: a rung the catalog advertises can still be refused |
| 12 | + * upstream because the ladder describes the model, not this account's entitlement (max on |
| 13 | + * muse-spark-1.3-contributor needs an active Muse Code subscription). The pipeline must learn the |
| 14 | + * refusal, replay once at the next published rung, and never replay an unrelated 400. |
| 15 | + */ |
| 16 | + |
| 17 | +const originalFetch = globalThis.fetch; |
| 18 | +const originalOpenCodexHome = process.env.OPENCODEX_HOME; |
| 19 | +const MODEL = "muse-spark-1.3-contributor"; |
| 20 | +const REFUSAL = JSON.stringify({ |
| 21 | + error: { |
| 22 | + param: "reasoning.effort", |
| 23 | + type: "invalid_request_error", |
| 24 | + message: "Error from provider (Console Go): Upstream request failed: [invalid_request_error] reasoning_effort max requires an active Muse Code subscription for model muse-spark-1.3-contributor.", |
| 25 | + }, |
| 26 | +}); |
| 27 | +const UNRELATED = JSON.stringify({ error: { type: "invalid_request_error", message: "Invalid upload request." } }); |
| 28 | + |
| 29 | +let testDir = ""; |
| 30 | + |
| 31 | +function writeSnapshot(values: string[]): void { |
| 32 | + writeFileSync(join(testDir, "reasoning-metadata-cache.json"), JSON.stringify({ |
| 33 | + version: 1, |
| 34 | + fetchedAt: Date.now(), |
| 35 | + source: "test", |
| 36 | + providers: { "opencode-go": { [MODEL]: { reasoning: true, options: [{ type: "effort", values }] } } }, |
| 37 | + })); |
| 38 | +} |
| 39 | + |
| 40 | +function config(): OcxConfig { |
| 41 | + return { |
| 42 | + defaultProvider: "first", |
| 43 | + providers: { |
| 44 | + first: { |
| 45 | + adapter: "openai-chat", |
| 46 | + baseUrl: "https://opencode.ai/zen/go/v1", |
| 47 | + authMode: "key", |
| 48 | + apiKey: "test-key", |
| 49 | + }, |
| 50 | + }, |
| 51 | + } as OcxConfig; |
| 52 | +} |
| 53 | + |
| 54 | +function request(stream = false): Request { |
| 55 | + return new Request("http://localhost/v1/responses", { |
| 56 | + method: "POST", |
| 57 | + headers: { "content-type": "application/json" }, |
| 58 | + body: JSON.stringify({ |
| 59 | + model: "first/" + MODEL, |
| 60 | + stream, |
| 61 | + store: false, |
| 62 | + reasoning: { effort: "max" }, |
| 63 | + input: [{ type: "message", role: "user", content: [{ type: "input_text", text: "go" }] }], |
| 64 | + }), |
| 65 | + }); |
| 66 | +} |
| 67 | + |
| 68 | +function success(): Response { |
| 69 | + return Response.json({ id: "resp-ok", object: "response", status: "completed", model: MODEL, output: [] }); |
| 70 | +} |
| 71 | + |
| 72 | +beforeEach(() => { |
| 73 | + testDir = mkdtempSync(join(tmpdir(), "ocx-reasoning-downgrade-")); |
| 74 | + process.env.OPENCODEX_HOME = testDir; |
| 75 | + resetReasoningMetadataCachesForTests(); |
| 76 | +}); |
| 77 | + |
| 78 | +afterEach(() => { |
| 79 | + globalThis.fetch = originalFetch; |
| 80 | + resetReasoningMetadataCachesForTests(); |
| 81 | + if (originalOpenCodexHome === undefined) delete process.env.OPENCODEX_HOME; |
| 82 | + else process.env.OPENCODEX_HOME = originalOpenCodexHome; |
| 83 | + rmSync(testDir, { recursive: true, force: true }); |
| 84 | +}); |
| 85 | + |
| 86 | +describe("rejected reasoning rungs", () => { |
| 87 | + test("clamps a rung the model does not publish before dispatch", async () => { |
| 88 | + writeSnapshot(["minimal", "low", "medium", "high", "xhigh"]); |
| 89 | + const outbound: Array<Record<string, unknown>> = []; |
| 90 | + globalThis.fetch = (async (_input: RequestInfo | URL, init?: RequestInit) => { |
| 91 | + outbound.push(JSON.parse(String(init?.body)) as Record<string, unknown>); |
| 92 | + return success(); |
| 93 | + }) as typeof fetch; |
| 94 | + const logCtx: RequestLogContext = { model: "", provider: "" }; |
| 95 | + |
| 96 | + const response = await handleResponses(request(), config(), logCtx); |
| 97 | + await response.text(); |
| 98 | + |
| 99 | + expect(response.status).toBe(200); |
| 100 | + expect(outbound).toHaveLength(1); |
| 101 | + expect(outbound[0]?.reasoning_effort).toBe("xhigh"); |
| 102 | + }); |
| 103 | + |
| 104 | + test("learns the refusal and replays once at the next published rung", async () => { |
| 105 | + writeSnapshot(["low", "high", "max"]); |
| 106 | + const outbound: Array<Record<string, unknown>> = []; |
| 107 | + globalThis.fetch = (async (_input: RequestInfo | URL, init?: RequestInit) => { |
| 108 | + outbound.push(JSON.parse(String(init?.body)) as Record<string, unknown>); |
| 109 | + return outbound.length === 1 |
| 110 | + ? new Response(REFUSAL, { status: 400, headers: { "content-type": "application/json" } }) |
| 111 | + : success(); |
| 112 | + }) as typeof fetch; |
| 113 | + const logCtx: RequestLogContext = { model: "", provider: "" }; |
| 114 | + |
| 115 | + const response = await handleResponses(request(), config(), logCtx); |
| 116 | + await response.text(); |
| 117 | + |
| 118 | + expect(outbound).toHaveLength(2); |
| 119 | + expect(outbound[0]?.reasoning_effort).toBe("max"); |
| 120 | + expect(outbound[1]?.reasoning_effort).toBe("high"); |
| 121 | + expect(logCtx.activeAttempt?.recoveryKinds).toEqual(["reasoning-effort-downgrade"]); |
| 122 | + }); |
| 123 | + |
| 124 | + test("does not replay an unrelated 400", async () => { |
| 125 | + writeSnapshot(["low", "high", "max"]); |
| 126 | + const outbound: Array<Record<string, unknown>> = []; |
| 127 | + globalThis.fetch = (async (_input: RequestInfo | URL, init?: RequestInit) => { |
| 128 | + outbound.push(JSON.parse(String(init?.body)) as Record<string, unknown>); |
| 129 | + return new Response(UNRELATED, { status: 400, headers: { "content-type": "application/json" } }); |
| 130 | + }) as typeof fetch; |
| 131 | + const logCtx: RequestLogContext = { model: "", provider: "" }; |
| 132 | + |
| 133 | + const response = await handleResponses(request(), config(), logCtx); |
| 134 | + await response.text(); |
| 135 | + |
| 136 | + expect(outbound).toHaveLength(1); |
| 137 | + expect(response.ok).toBe(false); |
| 138 | + expect(logCtx.activeAttempt?.recoveryKinds ?? []).toEqual([]); |
| 139 | + }); |
| 140 | + test("replays once on the streamed passthrough path too", async () => { |
| 141 | + writeSnapshot(["low", "high", "max"]); |
| 142 | + const outbound: Array<Record<string, unknown>> = []; |
| 143 | + globalThis.fetch = (async (_input: RequestInfo | URL, init?: RequestInit) => { |
| 144 | + outbound.push(JSON.parse(String(init?.body)) as Record<string, unknown>); |
| 145 | + return outbound.length === 1 |
| 146 | + ? new Response(REFUSAL, { status: 400, headers: { "content-type": "application/json" } }) |
| 147 | + : success(); |
| 148 | + }) as typeof fetch; |
| 149 | + const logCtx: RequestLogContext = { model: "", provider: "" }; |
| 150 | + |
| 151 | + const response = await handleResponses(request(true), config(), logCtx); |
| 152 | + await response.text(); |
| 153 | + |
| 154 | + expect(outbound).toHaveLength(2); |
| 155 | + expect(outbound[1]?.reasoning_effort).toBe("high"); |
| 156 | + expect(logCtx.activeAttempt?.recoveryKinds).toEqual(["reasoning-effort-downgrade"]); |
| 157 | + }); |
| 158 | +}); |
0 commit comments