Files
plugins/packages/grok/loop-check.test.mjs
T
yetone 9608d0311d 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.
2026-10-30 06:51:57 +08:00

53 lines
2.1 KiB
JavaScript

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)
})