Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Grok: timebox the live effort switch, read the login line anywhere, test a hung probe for real

milind-sonibf8c9fa8a7bd
diff --git a/server/drivers/acp/grok-session.test.ts b/server/drivers/acp/grok-session.test.ts
index 042720ceb6..b85c277b0b 100644
--- a/server/drivers/acp/grok-session.test.ts
+++ b/server/drivers/acp/grok-session.test.ts
@@ -147,12 +147,14 @@ describe("Grok changes model and effort inside the running session", () => {
}
});
- it("starts a new process when a narrowed tool profile names the model", async () => {
- const { instance, recorder, processes, ask } = await start("grok-live-narrowed", { FAKE_ACP_GROK_VERSION: "1.0.41" });
+ it("keeps the model in the process contract when a narrowed tool profile names it", async () => {
+ const { instance, recorder, processes, ask } = await start("grok-live-narrowed", {
+ FAKE_ACP_GROK_VERSION: "1.0.41", FAKE_ACP_SESSION_MODELS: "grok-4.5|Grok 4.5,grok-4.6|Grok 4.6",
+ });
const threadId = "t-grok-live-narrowed";
try {
const toolScope = { allow: ["native:read_file"] };
- await ask({ threadId, model: "grok-4.5", toolScope });
+ expect(await ask({ threadId, model: "grok-4.5", toolScope })).toMatchObject({ ok: true });
await ask({ threadId, model: "grok-4.6", toolScope, resumeCursor: "fake-acp-session" });
expect(processes()).toBe(2);
} finally {
diff --git a/server/drivers/acp/grok-sign-in.test.ts b/server/drivers/acp/grok-sign-in.test.ts
index c2eeb04b95..a8d4cb9a05 100644
--- a/server/drivers/acp/grok-sign-in.test.ts
+++ b/server/drivers/acp/grok-sign-in.test.ts
@@ -38,6 +38,7 @@ describe("reading `grok models`", () => {
["Not logged in. Run `grok login`.", "signed-out"],
["Your auth token is invalid or expired. Run `grok login` to re-authenticate.", "signed-out"],
["Token expired. Run `grok login` to re-authenticate.", "signed-out"],
+ ["\u001b[32m You are logged in with grok.com.\u001b[0m", "signed-in"],
["Available models:\n - grok-4.5 (default)", "unknown"],
["", "unknown"],
])("%j is %s", (output, login) => {
@@ -64,12 +65,13 @@ describe("the Grok sign-in check", () => {
expect(calls()).toBe(2);
});
- it("keeps the file's answer when Grok does not answer in time", async () => {
- const { env, authFile } = home("hang");
+ it.each(["hang", "signed-out-hang"])("keeps the file's answer when `grok models` does not finish in time (%s)", async (login) => {
+ const { env, authFile, calls } = home(login);
const startedAt = Date.now();
- expect(await grokLoginCheck({ timeoutMs: 1_500 })(FAKE_CLI, env, authFile)).toBe(true);
- expect(Date.now() - startedAt).toBeLessThan(15_000);
- });
+ expect(await grokLoginCheck({ timeoutMs: 2_000 })(FAKE_CLI, env, authFile)).toBe(true);
+ expect(Date.now() - startedAt).toBeGreaterThanOrEqual(1_000);
+ expect(calls()).toBe(1);
+ }, 30_000);
it("is signed out without auth.json, and never asks", async () => {
const { env, authFile, calls } = home("signed-in", false);
diff --git a/server/drivers/acp/grok-sign-in.ts b/server/drivers/acp/grok-sign-in.ts
index 4461a04754..412324e534 100644
--- a/server/drivers/acp/grok-sign-in.ts
+++ b/server/drivers/acp/grok-sign-in.ts
@@ -11,8 +11,8 @@ interface Probe { stamp: string; at: number; login: Promise<GrokLogin>; settled?
* deployment key." or "You are not authenticated.", and exits 0 either way; its errors say "Not logged in. Run `grok login`.",
* "Token expired. …" or "Your auth token is invalid or expired. …". */
export function readGrokModelsLogin(output: string): GrokLogin {
- if (/^You are (?:logged in|using XAI_API_KEY|authenticated via)\b/im.test(output)) return "signed-in";
- if (/^You are not authenticated\b|\bnot logged in\b|\btoken (?:is invalid or )?expired\b/im.test(output)) return "signed-out";
+ if (/\bYou are (?:logged in|using XAI_API_KEY|authenticated via)\b/i.test(output)) return "signed-in";
+ if (/\bYou are not authenticated\b|\bnot logged in\b|\btoken (?:is invalid or )?expired\b/i.test(output)) return "signed-out";
return "unknown";
}
diff --git a/server/drivers/acp/grok.ts b/server/drivers/acp/grok.ts
index 8827853c05..db1e9056eb 100644
--- a/server/drivers/acp/grok.ts
+++ b/server/drivers/acp/grok.ts
@@ -440,15 +440,15 @@ function grokContractArgs(args: string[], turn: SendTurnInput): string[] {
return args.map((arg, index) => index > 0 && (args[index - 1] === "-m" || args[index - 1] === "--reasoning-effort") ? "set" : arg);
}
-/** grok 1.0.x can leave session/load unanswered once it has replayed the history, which it streams from its own session
- * files with no pause between updates: two seconds without one means the replay is over. */
+/** grok 1.0.x can leave session/load unanswered once it has replayed the history. The replay is taken to be one burst
+ * from its own session files, so two seconds without an update ends it. */
const GROK_LOAD_REPLAY_QUIET_MS = 2_000;
/** grok 1.0.41 reports effort as the `reasoning_effort` select (category `thought_level`) of session/new's and
* session/load's configOptions, and session/set_config_option answers with the whole updated list. A load that went
* quiet reported no options, so the documented id is tried. */
async function setGrokEffort(
- request: (method: string, params: unknown) => Promise<any>,
+ request: (method: string, params: unknown, timeoutMs?: number) => Promise<any>,
sessionId: string,
effort: string,
configOptions: ReadonlyArray<{ id?: unknown; category?: unknown }>,
@@ -457,7 +457,7 @@ async function setGrokEffort(
const configId = typeof offered === "string" ? offered : "reasoning_effort";
let reply: { configOptions?: Array<{ id?: unknown; currentValue?: unknown }> } | undefined;
try {
- reply = await request("session/set_config_option", { sessionId, configId, value: effort });
+ reply = await request("session/set_config_option", { sessionId, configId, value: effort }, 15_000);
} catch (e) {
throw new LiveSwitchRefused(`Grok rejected effort "${effort}": ${(e as Error).message}`);
}
diff --git a/server/testing/fake-acp-cli.ts b/server/testing/fake-acp-cli.ts
index 0012033b95..e1ba8527ca 100755
--- a/server/testing/fake-acp-cli.ts
+++ b/server/testing/fake-acp-cli.ts
@@ -56,7 +56,8 @@
// FAKE_ACP_GROK_EFFORT=1 sessions report grok 1.0.41's `reasoning_effort` select (starting on --reasoning-effort, else
// high) and session/set_config_option changes it; =refuse answers that set with -32602
// FAKE_ACP_GROK_LOGIN what `models` says of the login, as grok 1.0.25 prints it: signed-in | signed-out | refreshes (signed
-// out once, then signed in, as a token refreshed by the first call) | hang; FAKE_ACP_GROK_LOGIN_COUNT counts the calls
+// out once, then signed in, as a token refreshed by the first call) | hang | signed-out-hang (says signed out, then hangs
+// as a slow token refresh would); FAKE_ACP_GROK_LOGIN_COUNT counts the calls
// FAKE_ACP_MODELS model ids: opencode-shaped configOptions and set_config_option (-32602 for others);
// a new session starts on AGY_ACP_DEFAULT_MODEL when set, taken unchecked as agy_acp_server 1.1.1 does
// FAKE_ACP_MODELS_FILE the same list, read once at startup, as `opencode acp` never re-reads a login
@@ -415,13 +416,15 @@ if (argv[0] === "models" && grokLogin) {
const countFile = process.env.FAKE_ACP_GROK_LOGIN_COUNT;
const calls = countFile && existsSync(countFile) ? Number(readFileSync(countFile, "utf8")) || 0 : 0;
if (countFile) writeFileSync(countFile, String(calls + 1));
- if (grokLogin === "hang") setInterval(() => {}, 1_000);
- else {
- const signedIn = grokLogin === "signed-in" || (grokLogin === "refreshes" && calls > 0);
- console.log([signedIn ? "You are logged in with grok.com." : "You are not authenticated.",
- "Default model: grok-4.5", "Available models:", " - grok-4.5 (default)"].join("\n"));
- process.exit(0);
+ if (grokLogin === "hang" || grokLogin === "signed-out-hang") {
+ if (grokLogin === "signed-out-hang") console.log("You are not authenticated.");
+ setInterval(() => {}, 1_000);
+ await new Promise(() => {});
}
+ const signedIn = grokLogin === "signed-in" || (grokLogin === "refreshes" && calls > 0);
+ console.log([signedIn ? "You are logged in with grok.com." : "You are not authenticated.",
+ "Default model: grok-4.5", "Available models:", " - grok-4.5 (default)"].join("\n"));
+ process.exit(0);
}
const catalogLock = process.env.FAKE_ACP_MODELS_LOCK;
if (catalogLock && (argv[0] === "models" || argv[0] === "acp") && !argv.includes("--refresh") && process.env.OPENCODE_DISABLE_MODELS_FETCH !== "1") {