TypeScript
fix(provider-grok): Grok health checks no longer start Grok's updater (#18075)
Aaron Queen2a7bd1673c7b
diff --git a/apps/server/src/provider/GrokProvider.test.ts b/apps/server/src/provider/GrokProvider.test.tsindex 73f56a3558d..801af29c990 100644--- a/apps/server/src/provider/GrokProvider.test.ts+++ b/apps/server/src/provider/GrokProvider.test.ts@@ -402,7 +402,11 @@ it.layer(NodeServices.layer)("checkGrokProviderStatus", (it) => {// A stand-in for the Grok CLI: `--version` and `models` print canned text,// and `agent stdio` execs the mock ACP agent so `initialize` returns model metadata.- const writeFakeGrokCli = (input: { readonly modelsOutput: string; readonly acp: boolean }) =>+ const writeFakeGrokCli = (input: {+ readonly modelsOutput: string;+ readonly acp: boolean;+ readonly updateLogPath?: string;+ }) =>Effect.gen(function* () {const fs = yield* FileSystem.FileSystem;const dir = yield* fs.makeTempDirectoryScoped({ prefix: "t3code-grok-probe-" });@@ -411,6 +415,14 @@ it.layer(NodeServices.layer)("checkGrokProviderStatus", (it) => {directory: dir,name: "grok",source: [+ ...(input.updateLogPath+ ? [+ 'import { appendFileSync as appendUpdateAttempt } from "node:fs";',+ 'if (process.env.GROK_DISABLE_AUTOUPDATER !== "1") {',+ ` appendUpdateAttempt(${JSON.stringify(input.updateLogPath)}, "update\\n");`,+ "}",+ ]+ : []),'if (process.argv[2] === "--version") {',' process.stdout.write("grok 1.0.13\\n");'," process.exit(0);",@@ -426,21 +438,29 @@ it.layer(NodeServices.layer)("checkGrokProviderStatus", (it) => {});});- it.effect("reports ready with ACP-discovered models when logged in", () =>+ it.effect("reports ready with ACP-discovered models without running startup updates", () =>Effect.gen(function* () {- const snapshot = yield* Effect.scoped(+ const environment = { ...process.env, XAI_API_KEY: "", GROK_DISABLE_AUTOUPDATER: "0" };+ const { snapshot, updateRan } = yield* Effect.scoped(Effect.gen(function* () {+ const fs = yield* FileSystem.FileSystem;+ const dir = yield* fs.makeTempDirectoryScoped({ prefix: "t3code-grok-updater-" });+ const updateLogPath = NodePath.join(dir, "updates.log");const grokPath = yield* writeFakeGrokCli({modelsOutput: LOGGED_IN_MODELS_OUTPUT,acp: true,+ updateLogPath,});- return yield* checkGrokProviderStatus(+ const snapshot = yield* checkGrokProviderStatus(decodeGrokSettings({ enabled: true, binaryPath: grokPath }),- { ...process.env, XAI_API_KEY: "" },+ environment,);+ return { snapshot, updateRan: yield* fs.exists(updateLogPath) };}),);+ expect(updateRan).toBe(false);+ expect(environment.GROK_DISABLE_AUTOUPDATER).toBe("0");expect(snapshot.status).toBe("ready");expect(snapshot.version).toBe("1.0.13");expect(snapshot.auth).toEqual({diff --git a/packages/provider-grok/src/server/probeEnvironment.ts b/packages/provider-grok/src/server/probeEnvironment.tsnew file mode 100644index 00000000000..e003fff6dd8--- /dev/null+++ b/packages/provider-grok/src/server/probeEnvironment.ts@@ -0,0 +1,12 @@+/**+ * Grok checks for a newer CLI when it launches. T3's health probes+ * (`grok --version`, `grok models`, `grok inspect --json`, ACP `initialize`)+ * run periodically in the background, so an update they start is invisible,+ * and on Windows it opens a console window for npm. Grok documents+ * `GROK_DISABLE_AUTOUPDATER=1` as process-scoped suppression, so probes set it+ * on a copy of the caller's environment. Sessions, text generation, and+ * `grok update` keep the caller's environment and update as the user configured.+ */+export function grokProbeEnvironment(environment: NodeJS.ProcessEnv): NodeJS.ProcessEnv {+ return { ...environment, GROK_DISABLE_AUTOUPDATER: "1" };+}diff --git a/packages/provider-grok/src/server/skills.test.ts b/packages/provider-grok/src/server/skills.test.tsindex 73e8ea0edd4..069dfa7597a 100644--- a/packages/provider-grok/src/server/skills.test.ts+++ b/packages/provider-grok/src/server/skills.test.ts@@ -2,15 +2,20 @@ import { describe, expect, it } from "@effect/vitest";import * as Effect from "effect/Effect";import * as Sink from "effect/Sink";import * as Stream from "effect/Stream";+import type * as ChildProcess from "effect/process/ChildProcess";import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";import { discoverGrokSkills } from "./skills.ts";const inspectPayload = (skills: ReadonlyArray<unknown>) => JSON.stringify({ skills });-const makeInspectSpawner = (stdout: string, exitCode = 0, spawnCwds?: Array<string | undefined>) =>+const makeInspectSpawner = (+ stdout: string,+ exitCode = 0,+ spawnOptions?: Array<ChildProcess.CommandOptions | undefined>,+) =>ChildProcessSpawner.make((command) => {- spawnCwds?.push(command._tag === "StandardCommand" ? command.options.cwd : undefined);+ spawnOptions?.push(command._tag === "StandardCommand" ? command.options : undefined);return Effect.succeed(ChildProcessSpawner.makeHandle({pid: ChildProcessSpawner.ProcessId(1),@@ -142,7 +147,7 @@ describe("discoverGrokSkills", () => {);it.effect("spawns in the configured cwd and rejects a failed probe", () => {- const spawnCwds: Array<string | undefined> = [];+ const spawnOptions: Array<ChildProcess.CommandOptions | undefined> = [];const stdout = inspectPayload([{name: "kept",@@ -154,11 +159,11 @@ describe("discoverGrokSkills", () => {const skills = yield* discoverGrokSkills({ binaryPath: "grok" }, {}, "/workspaces/demo").pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner,- makeInspectSpawner(stdout, 0, spawnCwds),+ makeInspectSpawner(stdout, 0, spawnOptions),),);- expect(spawnCwds).toEqual(["/workspaces/demo"]);+ expect(spawnOptions.map((options) => options?.cwd)).toEqual(["/workspaces/demo"]);expect(skills.map((skill) => skill.name)).toEqual(["kept"]);const failed = yield* discoverGrokSkills({ binaryPath: "grok" }).pipe(@@ -171,4 +176,26 @@ describe("discoverGrokSkills", () => {expect(failed._tag).toBe("Failure");});});++ // Workspace skill probes run on their own from the driver, outside the+ // status check, so they must suppress Grok's launch-time updater themselves.+ it.effect("disables Grok's updater for the probe without touching the caller's env", () => {+ const spawnOptions: Array<ChildProcess.CommandOptions | undefined> = [];+ const environment = { GROK_DISABLE_AUTOUPDATER: "0", GROK_HOME: "/home/dev/.grok" };++ return Effect.gen(function* () {+ yield* discoverGrokSkills({ binaryPath: "grok" }, environment, "/workspaces/demo").pipe(+ Effect.provideService(+ ChildProcessSpawner.ChildProcessSpawner,+ makeInspectSpawner(inspectPayload([]), 0, spawnOptions),+ ),+ );++ expect(spawnOptions[0]?.env).toEqual({+ GROK_DISABLE_AUTOUPDATER: "1",+ GROK_HOME: "/home/dev/.grok",+ });+ expect(environment.GROK_DISABLE_AUTOUPDATER).toBe("0");+ });+ });});diff --git a/packages/provider-grok/src/server/skills.ts b/packages/provider-grok/src/server/skills.tsindex ba1b72482c7..01a93799533 100644--- a/packages/provider-grok/src/server/skills.ts+++ b/packages/provider-grok/src/server/skills.ts@@ -24,6 +24,7 @@ import * as ChildProcess from "effect/process/ChildProcess";import { resolveSpawnCommand } from "@t3tools/shared/shell";import { spawnAndCollect } from "@t3tools/provider-core/server/snapshotProbe";+import { grokProbeEnvironment } from "./probeEnvironment.ts";const GROK_SKILLS_PROBE_TIMEOUT_MS = 4_000;@@ -103,15 +104,16 @@ export const discoverGrokSkills = Effect.fn("discoverGrokSkills")(function* (cwd?: string,) {const command = grokSettings.binaryPath || "grok";+ const probeEnvironment = grokProbeEnvironment(environment);const inspectResult = yield* Effect.gen(function* () {const spawnCommand = yield* resolveSpawnCommand(command, ["inspect", "--json"], {- env: environment,+ env: probeEnvironment,});return yield* spawnAndCollect(command,ChildProcess.make(spawnCommand.command, spawnCommand.args, {...(cwd ? { cwd } : {}),- env: environment,+ env: probeEnvironment,shell: spawnCommand.shell,}),);diff --git a/packages/provider-grok/src/server/status.ts b/packages/provider-grok/src/server/status.tsindex f901e157789..4e45bb9b0f0 100644--- a/packages/provider-grok/src/server/status.ts+++ b/packages/provider-grok/src/server/status.ts@@ -46,6 +46,7 @@ import {} from "./acpSupport.ts";import { sessionModelStateFromInitialize } from "@t3tools/provider-acp/server/runtimeModel";import { discoverGrokSkills } from "./skills.ts";+import { grokProbeEnvironment } from "./probeEnvironment.ts";const GROK_PRESENTATION = {displayName: "Grok",@@ -398,10 +399,13 @@ export const checkGrokProviderStatus = Effect.fn("checkGrokProviderStatus")(func});}- const versionResult = yield* runGrokCliCommand(grokSettings, ["--version"], environment).pipe(- Effect.timeoutOption(VERSION_PROBE_TIMEOUT_MS),- Effect.result,- );+ const probeEnvironment = grokProbeEnvironment(environment);++ const versionResult = yield* runGrokCliCommand(+ grokSettings,+ ["--version"],+ probeEnvironment,+ ).pipe(Effect.timeoutOption(VERSION_PROBE_TIMEOUT_MS), Effect.result);if (Result.isFailure(versionResult)) {const error = versionResult.failure;@@ -465,7 +469,7 @@ export const checkGrokProviderStatus = Effect.fn("checkGrokProviderStatus")(func}// `grok models` reports login state and model slugs without starting the agent.- const modelsResult = yield* runGrokCliCommand(grokSettings, ["models"], environment).pipe(+ const modelsResult = yield* runGrokCliCommand(grokSettings, ["models"], probeEnvironment).pipe(Effect.timeoutOption(AUTH_PROBE_TIMEOUT_MS),Effect.result,);@@ -498,12 +502,13 @@ export const checkGrokProviderStatus = Effect.fn("checkGrokProviderStatus")(func? { status: "unauthenticated" }: { status: "unknown" };+ // Skill discovery applies the probe environment itself.const skills = yield* discoverGrokSkills(grokSettings, environment, cwd).pipe(Effect.tapError((cause) => Effect.logDebug("Grok skill discovery failed.", { cause })),Effect.orElseSucceed(() => []),);- const acpExit = yield* discoverGrokMetadataViaAcpInitialize(grokSettings, environment).pipe(+ const acpExit = yield* discoverGrokMetadataViaAcpInitialize(grokSettings, probeEnvironment).pipe(Effect.timeoutOption(GROK_ACP_INITIALIZE_TIMEOUT_MS),Effect.exit,);