grok 0.2.2: Responses requests ask for Grok Build's loop checks (yetone/magpie#3594)

Grok Build 1.0.67 sends `x-grok-doom-loop-check: 1024` and
`x-grok-exact-repetition-check: 64` on every /v1/responses request
(seen in a mitm capture of the real CLI; the strings are in its binary
next to "server reported doom-loop triggers for this response"). The
plugin sent neither. Without them cli-chat-proxy lets a reply that
repeats itself run on: it cut "* " repeated after ~4,265 output tokens,
and a looser loop runs to max_output_tokens (128000 from Pi). That is
the "repeats until max output, ends in Internal error during token
generation" of #3594, seen on Pi -> magpie -> Grok but not in Grok
Build. The fetch now sets both on a Responses request unless the
caller set them already.

Ruled out while diffing the CLI's request against magpie's: the system
prompt. `instructions`, a system message and a developer message in
`input` all reach the model (same input_tokens, 1266 vs 1249 without,
same reply), so magpie's `instructions` is not lost.

Run:
- bun test in packages/grok on mote: 85 pass 0 fail.
- bun run check (whole repo) on mote: exit 0.
- Broken by hand: loop-check.test.mjs against index.mjs at HEAD:
  2 of 3 fail (the headers are missing); with the change 3 pass.
- Real account (owner's SuperGrok, sandbox magpie on origin/main with
  this plugin from disk, requests through mitm): the same "* " x3000
  prompt over /v1/responses, /v1/chat/completions and /v1/messages each
  carried both headers upstream and was stopped after ~1,020 deltas
  (~1K tokens) with "Internal error during token generation", against
  ~4,265 tokens without the headers (direct curl, earlier).

Not tried: the CLI's client-side doom-loop recovery (it resamples a
confident tool-call loop with a reminder). The proxy's
response.doom_loop_check event (a soft loop it reports, not stops)
goes through as it came and nothing acts on it yet.
This commit is contained in:
yetone
2026-10-30 06:51:57 +08:00
parent 24d0d428f0
commit 9608d0311d
4 changed files with 66 additions and 1 deletions
+3
View File
@@ -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:
+10
View File
@@ -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)
+52
View File
@@ -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)
})
+1 -1
View File
@@ -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",