mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-11-16 17:09:09 +00:00
Merged. Thank you, @maxmad64bis — this is the best-diagnosed bug of the batch. Instance fields written at request start and read after later awaits is a textbook cross-request race, and the symptom you traced it to is a nasty one: a JSON caller of a Responses model getting `event: response.completed …` back because a Chat request happened to finish first. Moving target format and client session into a per-`execute()` context while keeping the public field names as accessors is exactly the minimal shape — callers outside `execute()` see no change at all. Validation before merge: re-reconciled twice, because the tip moved under it. #14148 had already squash-merged, so the branch's copy of it collapsed onto the tip's identical content — `changelog.d/fixes/14148-opencode-refused-tool-shape.md`, `opencodeRequestShape.ts`, `opencodeFreeTierContract.ts` and `opencode-request-shape-retry.test.ts` all merged to one copy, nothing duplicated. The final delta against the tip is byte-exactly your commit `386c427e`: the MuseSpark extraction, the accessor pair, and the `runInRequestContext` wrap. Nothing of #14464's completion-retry wiring was reverted — its import, `freeTierRetryCtx()`/`attemptFor(input.body)`, the direct fast-path call and the rotation-loop refusal arm are all byte-identical to the tip. `opencode-request-format-race` + `opencode-request-shape-retry` + `opencode-free-tier-refusal-rotation` + `opencode-free-tier-request-contract` 85 pass / 0 fail. `typecheck:core` clean, `check:file-size` and `check:changelog-integrity` OK — the extraction actually *shrinks* `opencode.ts` to 1233 against the 1303 ceiling, so no rebaseline was needed. Note: the release tip is currently base-red on `open-sse/executors/auggie.ts` (#14496). Inherited, unrelated to this diff.
149 lines
5.7 KiB
TypeScript
149 lines
5.7 KiB
TypeScript
/**
|
|
* The executor is one shared instance per provider alias, and its requests overlap. What is
|
|
* known about a request (its target format, the client session) used to be kept in fields of
|
|
* that instance, so a request finishing in the middle of another one changed what the other
|
|
* one saw. The visible symptom: a JSON caller of a Responses model, slow to answer, got the raw
|
|
* event stream back instead of a JSON body once a Chat request finished in the meantime, because
|
|
* the rebuild that turns the stream into JSON read the format the other request had just cleared.
|
|
*/
|
|
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { OpencodeExecutor } from "../../open-sse/executors/opencode.ts";
|
|
import { runInRequestContext } from "../../open-sse/executors/opencodeRequestContext.ts";
|
|
|
|
const RESPONSES_SSE =
|
|
'event: response.completed\ndata: {"type":"response.completed","response":{"id":"resp-1","object":"response","status":"completed","output":[{"type":"message","role":"assistant","content":[{"type":"output_text","text":"hi from responses"}]}]}}\n\n';
|
|
const CHAT_SSE =
|
|
'data: {"id":"gen-1","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"ok"},"finish_reason":null}]}\n\n' +
|
|
'data: {"id":"gen-1","object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}\n\n' +
|
|
"data: [DONE]\n\n";
|
|
|
|
const READ_TOOL = { type: "function", function: { name: "read", parameters: { type: "object" } } };
|
|
|
|
async function exec(
|
|
executor: OpencodeExecutor,
|
|
model: string,
|
|
body: object,
|
|
stream: boolean
|
|
): Promise<Response> {
|
|
const result = (await executor.execute({
|
|
model,
|
|
body,
|
|
stream,
|
|
signal: null,
|
|
credentials: { apiKey: "k", accessToken: null, connectionId: "c" },
|
|
log: { debug() {}, info() {}, warn() {}, error() {} },
|
|
})) as { response: Response };
|
|
return result.response;
|
|
}
|
|
|
|
test("a slow Responses request that wants JSON is not affected by a Chat request finishing meanwhile", async () => {
|
|
const originalFetch = globalThis.fetch;
|
|
let release!: () => void;
|
|
const gate = new Promise<void>((resolve) => {
|
|
release = resolve;
|
|
});
|
|
globalThis.fetch = (async (_input: RequestInfo | URL, init?: RequestInit) => {
|
|
const sent = JSON.parse(String(init?.body ?? "{}")) as { model?: string };
|
|
if (String(sent.model).startsWith("muse-spark")) {
|
|
await gate;
|
|
return new Response(RESPONSES_SSE, {
|
|
status: 200,
|
|
headers: { "Content-Type": "text/event-stream" },
|
|
});
|
|
}
|
|
return new Response(CHAT_SSE, {
|
|
status: 200,
|
|
headers: { "Content-Type": "text/event-stream" },
|
|
});
|
|
}) as typeof globalThis.fetch;
|
|
|
|
try {
|
|
const executor = new OpencodeExecutor("opencode-zen");
|
|
const slow = exec(
|
|
executor,
|
|
"muse-spark-1.3-contributor-free",
|
|
{
|
|
model: "muse-spark-1.3-contributor-free",
|
|
input: [{ type: "message", role: "user", content: [{ type: "input_text", text: "hi" }] }],
|
|
instructions: "You are helpful.",
|
|
tools: [{ type: "function", name: "read", parameters: { type: "object" } }],
|
|
},
|
|
false
|
|
);
|
|
await new Promise((resolve) => setImmediate(resolve));
|
|
const quick = await exec(
|
|
executor,
|
|
"nemotron-3.5-lightning-free",
|
|
{
|
|
model: "nemotron-3.5-lightning-free",
|
|
messages: [{ role: "user", content: "hi" }],
|
|
tools: [READ_TOOL],
|
|
},
|
|
true
|
|
);
|
|
await quick.body?.cancel();
|
|
release();
|
|
|
|
const response = await slow;
|
|
const text = await response.text();
|
|
assert.ok(
|
|
text.includes("hi from responses"),
|
|
`the caller got its content back, not ${text.slice(0, 40)}`
|
|
);
|
|
assert.ok(!text.startsWith("event:"), "and not the raw event stream");
|
|
assert.doesNotThrow(() => JSON.parse(text), "as a JSON body");
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
});
|
|
|
|
type Fields = { _requestFormat: string | null; _clientSession: string | undefined };
|
|
const tick = () => new Promise((resolve) => setImmediate(resolve));
|
|
|
|
test("each request in flight keeps its own client session and format", async () => {
|
|
const executor = new OpencodeExecutor("opencode-zen") as unknown as Fields;
|
|
const flow = (session: string, format: string) =>
|
|
runInRequestContext(async () => {
|
|
executor._clientSession = session;
|
|
executor._requestFormat = format;
|
|
await tick();
|
|
await tick();
|
|
return [executor._clientSession, executor._requestFormat];
|
|
});
|
|
const [a, b] = await Promise.all([flow("ses_A", "claude"), flow("ses_B", "openai-responses")]);
|
|
assert.deepEqual(a, ["ses_A", "claude"]);
|
|
assert.deepEqual(b, ["ses_B", "openai-responses"]);
|
|
});
|
|
|
|
test("a request that ends does not clear what another one is still using", async () => {
|
|
const executor = new OpencodeExecutor("opencode-zen") as unknown as Fields;
|
|
let release!: () => void;
|
|
const gate = new Promise<void>((resolve) => {
|
|
release = resolve;
|
|
});
|
|
const slow = runInRequestContext(async () => {
|
|
executor._requestFormat = "openai-responses";
|
|
await gate;
|
|
return executor._requestFormat;
|
|
});
|
|
await runInRequestContext(async () => {
|
|
executor._requestFormat = "openai";
|
|
executor._requestFormat = null;
|
|
});
|
|
release();
|
|
assert.equal(await slow, "openai-responses");
|
|
});
|
|
|
|
test("outside execute() the fields are plain fields and a request context does not leak into them", () => {
|
|
const executor = new OpencodeExecutor("opencode-zen") as unknown as Fields;
|
|
executor._requestFormat = "claude";
|
|
executor._clientSession = "ses_direct";
|
|
runInRequestContext(() => {
|
|
executor._requestFormat = "openai";
|
|
executor._clientSession = "ses_inside";
|
|
});
|
|
assert.equal(executor._requestFormat, "claude");
|
|
assert.equal(executor._clientSession, "ses_direct");
|
|
});
|