@devries-cameron20 / t3code Mirror · read-only / commits / a2157eced7
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 23:15 UTC

fix(provider-grok): recheck Grok login once after a logged-out probe (#20358)

Co-authored-by: Abdurahmon <89484240+abdurahmon27@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Victor Vogel committed Oct 19, 2026 at 23:26 UTC a2157eced708fd25fab7b616f5a68cb65312eaed
2 files changed +71 -26
apps/server/src/provider/GrokProvider.test.ts
+30 -2
@@ -402,8 +402,9 @@ it.layer(NodeServices.layer)("checkGrokProviderStatus", (it) => {
402
403 // A stand-in for the Grok CLI: `--version` and `models` print canned text,
404 // and `agent stdio` execs the mock ACP agent so `initialize` returns model metadata.
405 + // A list of `models` outputs is served in call order, repeating the last one.
406 const writeFakeGrokCli = (input: {
406 - readonly modelsOutput: string;
407 + readonly modelsOutput: string | ReadonlyArray<string>;
408 readonly acp: boolean;
409 readonly updateLogPath?: string;
410 }) =>
@@ -428,7 +429,12 @@ it.layer(NodeServices.layer)("checkGrokProviderStatus", (it) => {
429 " process.exit(0);",
430 "}",
431 'if (process.argv[2] === "models") {',
431 - ` process.stdout.write(${JSON.stringify(input.modelsOutput)});`,
432 + ' const fs = await import("node:fs");',
433 + ` const callsPath = ${JSON.stringify(NodePath.join(dir, "models-calls"))};`,
434 + ' const calls = fs.existsSync(callsPath) ? fs.readFileSync(callsPath, "utf8").length : 0;',
435 + ' fs.appendFileSync(callsPath, ".");',
436 + ` const outputs = ${JSON.stringify([input.modelsOutput].flat())};`,
437 + " process.stdout.write(outputs[Math.min(calls, outputs.length - 1)]);",
438 " process.exit(0);",
439 "}",
440 'if (process.argv[2] !== "agent") process.exit(1);',
@@ -498,6 +504,28 @@ it.layer(NodeServices.layer)("checkGrokProviderStatus", (it) => {
504 }),
505 );
506
507 + // An expired access token prints the logged-out banner, then the CLI refreshes it in the
508 + // same run, so the next `grok models` reports the login (#14285).
509 + it.effect("reports authenticated when `grok models` logs in after refreshing a token", () =>
510 + Effect.gen(function* () {
511 + const snapshot = yield* Effect.scoped(
512 + Effect.gen(function* () {
513 + const grokPath = yield* writeFakeGrokCli({
514 + modelsOutput: [LOGGED_OUT_MODELS_OUTPUT, LOGGED_IN_MODELS_OUTPUT],
515 + acp: true,
516 + });
517 + return yield* checkGrokProviderStatus(
518 + decodeGrokSettings({ enabled: true, binaryPath: grokPath }),
519 + { ...process.env, XAI_API_KEY: "" },
520 + );
521 + }),
522 + );
523 +
524 + expect(snapshot.status).toBe("ready");
525 + expect(snapshot.auth.status).toBe("authenticated");
526 + }),
527 + );
528 +
529 it.effect("falls back to CLI-listed models with a warning when ACP initialize fails", () =>
530 Effect.gen(function* () {
531 const snapshot = yield* Effect.scoped(
packages/provider-grok/src/server/status.ts
+41 -24
@@ -304,6 +304,37 @@ const runGrokCliCommand = Effect.fn("GrokProviderStatus.runGrokCliCommand")(func
304 );
305 });
306
307 +const probeGrokModels = Effect.fn("GrokProviderStatus.probeGrokModels")(function* (
308 + grokSettings: GrokSettings,
309 + environment: NodeJS.ProcessEnv,
310 +) {
311 + const modelsResult = yield* runGrokCliCommand(grokSettings, ["models"], environment).pipe(
312 + Effect.timeoutOption(AUTH_PROBE_TIMEOUT_MS),
313 + Effect.result,
314 + );
315 + // Only a clean exit is parsed. Failed invocations print help or error text that
316 + // must not be read as model slugs or as a login verdict.
317 + const modelsOutput =
318 + Result.isSuccess(modelsResult) &&
319 + Option.isSome(modelsResult.success) &&
320 + modelsResult.success.value.code === 0
321 + ? modelsResult.success.value
322 + : undefined;
323 + const cliModels: GrokModelsCliOutput = modelsOutput
324 + ? parseGrokModelsCliOutput(`${modelsOutput.stdout}\n${modelsOutput.stderr}`)
325 + : { authenticated: null, models: [] };
326 + if (!modelsOutput) {
327 + yield* Effect.logWarning("Grok CLI model listing failed or timed out.", {
328 + errorTag: Result.isFailure(modelsResult)
329 + ? modelsResult.failure._tag
330 + : Option.isNone(modelsResult.success)
331 + ? "Timeout"
332 + : `ExitCode${modelsResult.success.value.code}`,
333 + });
334 + }
335 + return cliModels;
336 +});
337 +
338 const decodeAvailableCommands = Schema.decodeUnknownOption(Schema.Array(Schema.Unknown));
339 const decodeAvailableCommand = Schema.decodeUnknownOption(
340 Schema.Struct({
@@ -388,6 +419,7 @@ export const checkGrokProviderStatus = Effect.fn("checkGrokProviderStatus")(func
419 }
420
421 const probeEnvironment = grokProbeEnvironment(environment);
422 + const apiKey = environment[GROK_API_KEY_ENV]?.trim();
423
424 const versionResult = yield* runGrokCliCommand(
425 grokSettings,
@@ -457,32 +489,17 @@ export const checkGrokProviderStatus = Effect.fn("checkGrokProviderStatus")(func
489 }
490
491 // `grok models` reports login state and model slugs without starting the agent.
460 - const modelsResult = yield* runGrokCliCommand(grokSettings, ["models"], probeEnvironment).pipe(
461 - Effect.timeoutOption(AUTH_PROBE_TIMEOUT_MS),
462 - Effect.result,
463 - );
464 - // Only a clean exit is parsed. Failed invocations print help or error text that
465 - // must not be read as model slugs or as a login verdict.
466 - const modelsOutput =
467 - Result.isSuccess(modelsResult) &&
468 - Option.isSome(modelsResult.success) &&
469 - modelsResult.success.value.code === 0
470 - ? modelsResult.success.value
471 - : undefined;
472 - const cliModels: GrokModelsCliOutput = modelsOutput
473 - ? parseGrokModelsCliOutput(`${modelsOutput.stdout}\n${modelsOutput.stderr}`)
474 - : { authenticated: null, models: [] };
475 - if (!modelsOutput) {
476 - yield* Effect.logWarning("Grok CLI model listing failed or timed out.", {
477 - errorTag: Result.isFailure(modelsResult)
478 - ? modelsResult.failure._tag
479 - : Option.isNone(modelsResult.success)
480 - ? "Timeout"
481 - : `ExitCode${modelsResult.success.value.code}`,
482 - });
492 + let cliModels = yield* probeGrokModels(grokSettings, probeEnvironment);
493 + // An expired access token prints the logged-out line, then the CLI refreshes it in the same
494 + // process. Probing again after that process exited tells a refresh from a real logout.
495 + if (cliModels.authenticated === false && !apiKey) {
496 + const recheck = yield* probeGrokModels(grokSettings, probeEnvironment);
497 + if (recheck.authenticated !== null) {
498 + cliModels = recheck;
499 + }
500 }
501
485 - const auth: ServerProviderAuth = environment[GROK_API_KEY_ENV]?.trim()
502 + const auth: ServerProviderAuth = apiKey
503 ? { status: "authenticated", type: "api_key", label: "xAI API key" }
504 : cliModels.authenticated === true
505 ? { status: "authenticated", type: "cached_token", label: "Grok account" }