Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions packages/provider-grok/src/server/driver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,59 @@ it.layer(layerTest)("GrokDriver", (it) => {
});
}

// `grok update` installs into `$GROK_HOME/bin`, so an instance GROK_HOME that does not own
// binaryPath would update a different copy and leave the verified binary outdated.
// `target` is where `bin/grok` links; null is a copied binary, as the Windows installer leaves.
const scriptInstallCases = [
{ target: "grok-1.0.77", downloads: false, owner: ".grok" },
{ target: "../downloads/grok-linux-x86_64", downloads: true, owner: ".grok" },
{ target: "../downloads/grok-1.0.88-linux-x86_64", downloads: true, owner: ".grok" },
{ target: null, downloads: true, owner: ".grok" },
{ target: null, downloads: false, owner: ".grok-hub" },
] as const;
for (const { target, downloads, owner } of scriptInstallCases) {
it.effect.skipIf(windowsHost)(
`runs grok update with GROK_HOME=${owner} for bin/grok -> ${target ?? "copy"}${downloads ? " beside downloads" : ""}`,
() =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const home = yield* fs.realPath(
yield* fs.makeTempDirectoryScoped({ prefix: "t3-grok-home-" }),
);
const scriptHome = path.join(home, ".grok");
const binaryPath = path.join(scriptHome, "bin", "grok");
const targetPath = path.resolve(path.dirname(binaryPath), target ?? "grok");
yield* fs.makeDirectory(path.dirname(binaryPath), { recursive: true });
if (downloads) {
yield* fs.makeDirectory(path.join(scriptHome, "downloads"), { recursive: true });
}
yield* fs.writeFileString(targetPath, "#!/bin/sh\n");
yield* fs.chmod(targetPath, 0o755);
if (target) yield* fs.symlink(target, binaryPath);

const instance = yield* GrokDriver.create({
instanceId: ProviderInstanceId.make("grok-separate-home"),
displayName: "Grok test",
enabled: false,
environment: [
{ name: "GROK_HOME", value: path.join(home, ".grok-hub"), sensitive: false },
],
config: { ...GrokDriver.defaultConfig(), binaryPath },
});
const { update } = yield* instance.snapshot.resolveMaintenance();

expect(update).toEqual(
expect.objectContaining({
executable: binaryPath,
args: ["update"],
env: expect.objectContaining({ GROK_HOME: path.join(home, owner) }),
}),
);
}).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, noSpawner)),
);
}

// An `npx t3` server inherits npm_config_user_agent, which Grok ranks above `[cli] installer`.
it.effect.skipIf(windowsHost)("keeps a script install off the npm updater under npx", () =>
Effect.gen(function* () {
Expand Down
20 changes: 19 additions & 1 deletion packages/provider-grok/src/server/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ const decodeGrokSettings = Schema.decodeSync(GrokSettings);
const DRIVER_KIND = ProviderDriverKind.make("grok");
// npm latest matches the stable channel installed by grok update.
const GROK_NPM_PACKAGE = "@xai-official/grok";
// `grok update` installs under `GROK_HOME` from the resolved executable and instance environment.
// npm 12 blocks the postinstall that moves the new binary in, yet exits 0, so allow it by env.
// Mise and Homebrew casks upgrade through their manager; `grok update` would install a second
// copy, so a cask its brew cannot vouch for stays manual, as does a Nix store install.
Expand Down Expand Up @@ -92,6 +91,24 @@ const UPDATE: ProviderMaintenanceCapabilitiesResolver = {
})
);
}
const path = yield* Path.Path;
const fileSystem = yield* FileSystem.FileSystem;
const name = path.basename(context.realCommandPath).toLowerCase();
const installDir = path.dirname(context.realCommandPath);
const installDirName = path.basename(installDir).toLowerCase();
const home = path.dirname(installDir);
// `grok update` installs under `$GROK_HOME`, so a script install keeps its own home even if the
// instance sets another: `<home>/bin/grok` links into `<home>/downloads`, or on Windows is a copy
// beside it. Only an install script creates `downloads`, so a bare `bin/grok` elsewhere is not one.
const isScriptInstall =
(installDirName === "downloads" && name.startsWith("grok-")) ||
(installDirName === "bin" &&
(/^grok-\d/.test(name) ||
((name === "grok" || name === "grok.exe") &&
(yield* fileSystem
.exists(path.join(home, "downloads"))
.pipe(Effect.orElseSucceed(() => false))))));
const scriptHome = isScriptInstall ? { GROK_HOME: home } : {};
return makeProviderMaintenanceCapabilities({
provider: DRIVER_KIND,
packageName: GROK_NPM_PACKAGE,
Expand All @@ -101,6 +118,7 @@ const UPDATE: ProviderMaintenanceCapabilitiesResolver = {
platform: context.platform,
env: {
...context.env,
...scriptHome,
npm_config_allow_scripts: GROK_NPM_PACKAGE,
// An npx-launched server's npm_config_user_agent outranks Grok's `[cli] installer`, steering
// a script install to npm. The runner extends process.env; only undefined removes a key.
Expand Down
Loading