Skip to content

Commit 05278b8

Browse files
bompusjuliusmarmingeclaude
authored andcommitted
fix(provider-grok): Grok health checks no longer start Grok's updater (pingdotgg#18075)
Co-authored-by: Julius Marminge <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]> (cherry picked from commit 2a7bd16)
1 parent 50966a3 commit 05278b8

5 files changed

Lines changed: 84 additions & 18 deletions

File tree

‎apps/server/src/provider/GrokProvider.test.ts‎

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,11 @@ it.layer(NodeServices.layer)("checkGrokProviderStatus", (it) => {
402402

403403
// A stand-in for the Grok CLI: `--version` and `models` print canned text,
404404
// and `agent stdio` execs the mock ACP agent so `initialize` returns model metadata.
405-
const writeFakeGrokCli = (input: { readonly modelsOutput: string; readonly acp: boolean }) =>
405+
const writeFakeGrokCli = (input: {
406+
readonly modelsOutput: string;
407+
readonly acp: boolean;
408+
readonly updateLogPath?: string;
409+
}) =>
406410
Effect.gen(function* () {
407411
const fs = yield* FileSystem.FileSystem;
408412
const dir = yield* fs.makeTempDirectoryScoped({ prefix: "t3code-grok-probe-" });
@@ -411,6 +415,14 @@ it.layer(NodeServices.layer)("checkGrokProviderStatus", (it) => {
411415
directory: dir,
412416
name: "grok",
413417
source: [
418+
...(input.updateLogPath
419+
? [
420+
'import { appendFileSync as appendUpdateAttempt } from "node:fs";',
421+
'if (process.env.GROK_DISABLE_AUTOUPDATER !== "1") {',
422+
` appendUpdateAttempt(${JSON.stringify(input.updateLogPath)}, "update\\n");`,
423+
"}",
424+
]
425+
: []),
414426
'if (process.argv[2] === "--version") {',
415427
' process.stdout.write("grok 1.0.13\\n");',
416428
" process.exit(0);",
@@ -426,21 +438,29 @@ it.layer(NodeServices.layer)("checkGrokProviderStatus", (it) => {
426438
});
427439
});
428440

429-
it.effect("reports ready with ACP-discovered models when logged in", () =>
441+
it.effect("reports ready with ACP-discovered models without running startup updates", () =>
430442
Effect.gen(function* () {
431-
const snapshot = yield* Effect.scoped(
443+
const environment = { ...process.env, XAI_API_KEY: "", GROK_DISABLE_AUTOUPDATER: "0" };
444+
const { snapshot, updateRan } = yield* Effect.scoped(
432445
Effect.gen(function* () {
446+
const fs = yield* FileSystem.FileSystem;
447+
const dir = yield* fs.makeTempDirectoryScoped({ prefix: "t3code-grok-updater-" });
448+
const updateLogPath = NodePath.join(dir, "updates.log");
433449
const grokPath = yield* writeFakeGrokCli({
434450
modelsOutput: LOGGED_IN_MODELS_OUTPUT,
435451
acp: true,
452+
updateLogPath,
436453
});
437-
return yield* checkGrokProviderStatus(
454+
const snapshot = yield* checkGrokProviderStatus(
438455
decodeGrokSettings({ enabled: true, binaryPath: grokPath }),
439-
{ ...process.env, XAI_API_KEY: "" },
456+
environment,
440457
);
458+
return { snapshot, updateRan: yield* fs.exists(updateLogPath) };
441459
}),
442460
);
443461

462+
expect(updateRan).toBe(false);
463+
expect(environment.GROK_DISABLE_AUTOUPDATER).toBe("0");
444464
expect(snapshot.status).toBe("ready");
445465
expect(snapshot.version).toBe("1.0.13");
446466
expect(snapshot.auth).toEqual({
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Grok checks for a newer CLI when it launches. T3's health probes
3+
* (`grok --version`, `grok models`, `grok inspect --json`, ACP `initialize`)
4+
* run periodically in the background, so an update they start is invisible,
5+
* and on Windows it opens a console window for npm. Grok documents
6+
* `GROK_DISABLE_AUTOUPDATER=1` as process-scoped suppression, so probes set it
7+
* on a copy of the caller's environment. Sessions, text generation, and
8+
* `grok update` keep the caller's environment and update as the user configured.
9+
*/
10+
export function grokProbeEnvironment(environment: NodeJS.ProcessEnv): NodeJS.ProcessEnv {
11+
return { ...environment, GROK_DISABLE_AUTOUPDATER: "1" };
12+
}

‎packages/provider-grok/src/server/skills.test.ts‎

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@ import { describe, expect, it } from "@effect/vitest";
22
import * as Effect from "effect/Effect";
33
import * as Sink from "effect/Sink";
44
import * as Stream from "effect/Stream";
5+
import type * as ChildProcess from "effect/process/ChildProcess";
56
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
67

78
import { discoverGrokSkills } from "./skills.ts";
89

910
const inspectPayload = (skills: ReadonlyArray<unknown>) => JSON.stringify({ skills });
1011

11-
const makeInspectSpawner = (stdout: string, exitCode = 0, spawnCwds?: Array<string | undefined>) =>
12+
const makeInspectSpawner = (
13+
stdout: string,
14+
exitCode = 0,
15+
spawnOptions?: Array<ChildProcess.CommandOptions | undefined>,
16+
) =>
1217
ChildProcessSpawner.make((command) => {
13-
spawnCwds?.push(command._tag === "StandardCommand" ? command.options.cwd : undefined);
18+
spawnOptions?.push(command._tag === "StandardCommand" ? command.options : undefined);
1419
return Effect.succeed(
1520
ChildProcessSpawner.makeHandle({
1621
pid: ChildProcessSpawner.ProcessId(1),
@@ -142,7 +147,7 @@ describe("discoverGrokSkills", () => {
142147
);
143148

144149
it.effect("spawns in the configured cwd and rejects a failed probe", () => {
145-
const spawnCwds: Array<string | undefined> = [];
150+
const spawnOptions: Array<ChildProcess.CommandOptions | undefined> = [];
146151
const stdout = inspectPayload([
147152
{
148153
name: "kept",
@@ -154,11 +159,11 @@ describe("discoverGrokSkills", () => {
154159
const skills = yield* discoverGrokSkills({ binaryPath: "grok" }, {}, "/workspaces/demo").pipe(
155160
Effect.provideService(
156161
ChildProcessSpawner.ChildProcessSpawner,
157-
makeInspectSpawner(stdout, 0, spawnCwds),
162+
makeInspectSpawner(stdout, 0, spawnOptions),
158163
),
159164
);
160165

161-
expect(spawnCwds).toEqual(["/workspaces/demo"]);
166+
expect(spawnOptions.map((options) => options?.cwd)).toEqual(["/workspaces/demo"]);
162167
expect(skills.map((skill) => skill.name)).toEqual(["kept"]);
163168

164169
const failed = yield* discoverGrokSkills({ binaryPath: "grok" }).pipe(
@@ -171,4 +176,26 @@ describe("discoverGrokSkills", () => {
171176
expect(failed._tag).toBe("Failure");
172177
});
173178
});
179+
180+
// Workspace skill probes run on their own from the driver, outside the
181+
// status check, so they must suppress Grok's launch-time updater themselves.
182+
it.effect("disables Grok's updater for the probe without touching the caller's env", () => {
183+
const spawnOptions: Array<ChildProcess.CommandOptions | undefined> = [];
184+
const environment = { GROK_DISABLE_AUTOUPDATER: "0", GROK_HOME: "/home/dev/.grok" };
185+
186+
return Effect.gen(function* () {
187+
yield* discoverGrokSkills({ binaryPath: "grok" }, environment, "/workspaces/demo").pipe(
188+
Effect.provideService(
189+
ChildProcessSpawner.ChildProcessSpawner,
190+
makeInspectSpawner(inspectPayload([]), 0, spawnOptions),
191+
),
192+
);
193+
194+
expect(spawnOptions[0]?.env).toEqual({
195+
GROK_DISABLE_AUTOUPDATER: "1",
196+
GROK_HOME: "/home/dev/.grok",
197+
});
198+
expect(environment.GROK_DISABLE_AUTOUPDATER).toBe("0");
199+
});
200+
});
174201
});

‎packages/provider-grok/src/server/skills.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import * as ChildProcess from "effect/process/ChildProcess";
2424
import { resolveSpawnCommand } from "@t3tools/shared/shell";
2525

2626
import { spawnAndCollect } from "@t3tools/provider-core/server/snapshotProbe";
27+
import { grokProbeEnvironment } from "./probeEnvironment.ts";
2728

2829
const GROK_SKILLS_PROBE_TIMEOUT_MS = 4_000;
2930

@@ -103,15 +104,16 @@ export const discoverGrokSkills = Effect.fn("discoverGrokSkills")(function* (
103104
cwd?: string,
104105
) {
105106
const command = grokSettings.binaryPath || "grok";
107+
const probeEnvironment = grokProbeEnvironment(environment);
106108
const inspectResult = yield* Effect.gen(function* () {
107109
const spawnCommand = yield* resolveSpawnCommand(command, ["inspect", "--json"], {
108-
env: environment,
110+
env: probeEnvironment,
109111
});
110112
return yield* spawnAndCollect(
111113
command,
112114
ChildProcess.make(spawnCommand.command, spawnCommand.args, {
113115
...(cwd ? { cwd } : {}),
114-
env: environment,
116+
env: probeEnvironment,
115117
shell: spawnCommand.shell,
116118
}),
117119
);

‎packages/provider-grok/src/server/status.ts‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import {
4646
} from "./acpSupport.ts";
4747
import { sessionModelStateFromInitialize } from "@t3tools/provider-acp/server/runtimeModel";
4848
import { discoverGrokSkills } from "./skills.ts";
49+
import { grokProbeEnvironment } from "./probeEnvironment.ts";
4950

5051
const GROK_PRESENTATION = {
5152
displayName: "Grok",
@@ -398,10 +399,13 @@ export const checkGrokProviderStatus = Effect.fn("checkGrokProviderStatus")(func
398399
});
399400
}
400401

401-
const versionResult = yield* runGrokCliCommand(grokSettings, ["--version"], environment).pipe(
402-
Effect.timeoutOption(VERSION_PROBE_TIMEOUT_MS),
403-
Effect.result,
404-
);
402+
const probeEnvironment = grokProbeEnvironment(environment);
403+
404+
const versionResult = yield* runGrokCliCommand(
405+
grokSettings,
406+
["--version"],
407+
probeEnvironment,
408+
).pipe(Effect.timeoutOption(VERSION_PROBE_TIMEOUT_MS), Effect.result);
405409

406410
if (Result.isFailure(versionResult)) {
407411
const error = versionResult.failure;
@@ -465,7 +469,7 @@ export const checkGrokProviderStatus = Effect.fn("checkGrokProviderStatus")(func
465469
}
466470

467471
// `grok models` reports login state and model slugs without starting the agent.
468-
const modelsResult = yield* runGrokCliCommand(grokSettings, ["models"], environment).pipe(
472+
const modelsResult = yield* runGrokCliCommand(grokSettings, ["models"], probeEnvironment).pipe(
469473
Effect.timeoutOption(AUTH_PROBE_TIMEOUT_MS),
470474
Effect.result,
471475
);
@@ -498,12 +502,13 @@ export const checkGrokProviderStatus = Effect.fn("checkGrokProviderStatus")(func
498502
? { status: "unauthenticated" }
499503
: { status: "unknown" };
500504

505+
// Skill discovery applies the probe environment itself.
501506
const skills = yield* discoverGrokSkills(grokSettings, environment, cwd).pipe(
502507
Effect.tapError((cause) => Effect.logDebug("Grok skill discovery failed.", { cause })),
503508
Effect.orElseSucceed(() => []),
504509
);
505510

506-
const acpExit = yield* discoverGrokMetadataViaAcpInitialize(grokSettings, environment).pipe(
511+
const acpExit = yield* discoverGrokMetadataViaAcpInitialize(grokSettings, probeEnvironment).pipe(
507512
Effect.timeoutOption(GROK_ACP_INITIALIZE_TIMEOUT_MS),
508513
Effect.exit,
509514
);

0 commit comments

Comments
 (0)