diff --git a/packages/grok/README.md b/packages/grok/README.md index 0dd39c4..6a1dd59 100644 --- a/packages/grok/README.md +++ b/packages/grok/README.md @@ -93,6 +93,9 @@ API (`@ai-sdk/openai`), signed as the CLI signs them: (`500000` for `@500k`). The plain id sends no such header and stays on the default window. - `x-grok-conv-id`: the request's `prompt_cache_key`. +- `x-grok-doom-loop-check: 1024` and `x-grok-exact-repetition-check: 64` + on a Responses request, as Grok Build sends them: the proxy then stops a + reply that repeats itself after ~1K tokens instead of letting it run on. The backend turns away some of what a request may carry, so the plugin leaves these out: diff --git a/packages/grok/index.mjs b/packages/grok/index.mjs index 8442f33..0ce1b3f 100644 --- a/packages/grok/index.mjs +++ b/packages/grok/index.mjs @@ -19,6 +19,7 @@ const BASE = "https://cli-chat-proxy.grok.com/v1" const CLIENT_VERSION = "1.0.41" const CPA_CLIENT_ID = "b1a00492-073a-47ea-816f-4c329264a828" const CPA_SCOPE = `https://auth.x.ai::${CPA_CLIENT_ID}` +const LOOP_CHECKS = [["x-grok-doom-loop-check", "1024"], ["x-grok-exact-repetition-check", "64"]] const REFRESH_MARGIN = 5 * 60 * 1000 // a token this close to its end is renewed before a request const LEAD_MS = REFRESH_MARGIN // and this close, magpie renews it ahead of time (auth.refresh) const LINK_WAIT = 30 * 1000 @@ -987,6 +988,15 @@ export const GrokAuthPlugin = async ({ client, magpie }) => { } if (asked.window) headers.set("x-grok-context-window", String(asked.window)) if (typeof parsed.prompt_cache_key === "string" && parsed.prompt_cache_key) headers.set("x-grok-conv-id", parsed.prompt_cache_key) + // Grok Build asks the proxy to watch every Responses stream for a + // model stuck repeating itself: without these the proxy cuts a + // loop only after ~4K tokens, and a looser one (a fence or a line + // over and over) runs to max_output_tokens (yetone/magpie#3594). + // The values are Grok Build 1.0.67's (doom_loop_recovery's + // max_threshold is the 64). + if (new URL(req.url).pathname.endsWith("/responses")) { + for (const [k, v] of LOOP_CHECKS) if (!headers.has(k)) headers.set(k, v) + } headers.delete("content-length") const send = async (key) => { await sign(headers, key) diff --git a/packages/grok/loop-check.test.mjs b/packages/grok/loop-check.test.mjs new file mode 100644 index 0000000..e7e6d23 --- /dev/null +++ b/packages/grok/loop-check.test.mjs @@ -0,0 +1,52 @@ +import { noRealGrok } from "./nocli.mjs" +import { afterAll, afterEach, expect, test } from "bun:test" +import { mkdtempSync, rmSync, writeFileSync } from "node:fs" +import { tmpdir } from "node:os" +import { join } from "node:path" +import { GrokAuthPlugin } from "./index.mjs" + +noRealGrok() + +// Grok Build sends these on every Responses request; without them the proxy +// lets a looping reply run to max_output_tokens (yetone/magpie#3594). +const realFetch = globalThis.fetch +afterEach(() => { globalThis.fetch = realFetch }) +const home = mkdtempSync(join(tmpdir(), "grok-loop-check-")) +afterAll(() => rmSync(home, { recursive: true, force: true })) +const expires = Date.now() + 24 * 3600_000 +writeFileSync(join(home, "auth.json"), JSON.stringify({ + test: { key: "test-token", email: "test@example.invalid", expires_at: new Date(expires).toISOString() }, +})) +const auth = { type: "oauth", refresh: home, access: "test-token", expires, accountId: "test@example.invalid" } + +async function send(path, headers = {}) { + let seen + globalThis.fetch = async (_, init) => { + seen = new Headers(init.headers) + return Response.json({ output: [] }) + } + const hooks = await GrokAuthPlugin({ client: {} }) + const opts = await hooks.auth.loader(async () => auth) + await opts.fetch(`https://cli-chat-proxy.grok.com/v1/${path}`, { + method: "POST", headers, body: JSON.stringify({ model: "grok-4.7", input: "hi", stream: true }), + }) + return seen +} + +test("a Responses request asks for Grok Build's loop checks", async () => { + const h = await send("responses") + expect(h.get("x-grok-doom-loop-check")).toBe("1024") + expect(h.get("x-grok-exact-repetition-check")).toBe("64") +}) + +test("a value the caller set is kept", async () => { + const h = await send("responses", { "x-grok-doom-loop-check": "2048" }) + expect(h.get("x-grok-doom-loop-check")).toBe("2048") + expect(h.get("x-grok-exact-repetition-check")).toBe("64") +}) + +test("other paths go without them", async () => { + const h = await send("models") + expect(h.has("x-grok-doom-loop-check")).toBe(false) + expect(h.has("x-grok-exact-repetition-check")).toBe(false) +}) diff --git a/packages/grok/package.json b/packages/grok/package.json index 46dc92d..dfcace2 100644 --- a/packages/grok/package.json +++ b/packages/grok/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-grok-auth", - "version": "0.2.1", + "version": "0.2.2", "description": "OpenCode provider plugin: Grok (SuperGrok / X Premium+) through the Grok Build CLI's sign-in", "type": "module", "main": "./index.mjs",