diff --git a/packages/agent-setup/src/agent-wrappers-grok.ts b/packages/agent-setup/src/agent-wrappers-grok.ts index 1c77e2321e8..447bad72861 100644 --- a/packages/agent-setup/src/agent-wrappers-grok.ts +++ b/packages/agent-setup/src/agent-wrappers-grok.ts @@ -3,7 +3,6 @@ import path from "node:path"; import { buildWrapperScript, createWrapper, - getManagedNotifyHookCommand, MANAGED_NOTIFY_RELATIVE_PATH, removeOwnedFileIfMarked, writeFileIfChanged, @@ -48,7 +47,13 @@ export const GROK_BLOCKING_NOTIFICATION_TYPES = [ "elicitation_dialog", ] as const; -const GROK_MANAGED_HOOK_COMMAND = getManagedNotifyHookCommand("grok"); +// Grok will not run a hook whose text names an unset env var (`$X`, `${X:-}`), +// so the home dir comes in through printenv as `$1` (#8375). +export function getGrokManagedNotifyHookCommand(): string { + return `sh -c '[ -n "$1" ] && [ -x "$1/${MANAGED_NOTIFY_RELATIVE_PATH}" ] && SUPERSET_HOOK_HARNESS=grok "$1/${MANAGED_NOTIFY_RELATIVE_PATH}" || true' sh "$(printenv SUPERSET_HOME_DIR)"`; +} + +const GROK_MANAGED_HOOK_COMMAND = getGrokManagedNotifyHookCommand(); // Vendor hook configs Superset also manages. Grok replays them in compat mode; // their events carry Claude's or Cursor's harness id, so the notify script diff --git a/packages/agent-setup/src/agent-wrappers.test.ts b/packages/agent-setup/src/agent-wrappers.test.ts index a380745eaa1..1efec25acdc 100644 --- a/packages/agent-setup/src/agent-wrappers.test.ts +++ b/packages/agent-setup/src/agent-wrappers.test.ts @@ -2498,6 +2498,7 @@ import { GROK_COMPAT_MARKER_START, getGrokConfigTomlContent, getGrokHooksJsonContent, + getGrokManagedNotifyHookCommand, getGrokWrapperScript, } from "./agent-wrappers-grok"; @@ -2539,6 +2540,37 @@ describe("grok hooks json", () => { ); }); + it("names no env var and notifies only when SUPERSET_HOME_DIR is set", () => { + const command = getGrokManagedNotifyHookCommand(); + expect(command).not.toMatch(/\$\{?[A-Za-z_]/); + + const home = path.join(TEST_ROOT, "grok-notify"); + try { + const log = path.join(home, "log"); + const notifyPath = path.join(home, "hooks", "notify.sh"); + mkdirSync(path.dirname(notifyPath), { recursive: true }); + writeFileSync( + notifyPath, + `#!/bin/sh\necho "$SUPERSET_HOOK_HARNESS" >> "${log}"\n`, + ); + chmodSync(notifyPath, 0o755); + const { SUPERSET_HOME_DIR: _unset, ...env } = process.env; + + const outside = spawnSync("sh", ["-c", command], { env }); + expect(outside.status).toBe(0); + expect(outside.stderr.toString()).toBe(""); + expect(existsSync(log)).toBe(false); + + const inside = spawnSync("sh", ["-c", command], { + env: { ...env, SUPERSET_HOME_DIR: home }, + }); + expect(inside.status).toBe(0); + expect(readFileSync(log, "utf-8").trim()).toBe("grok"); + } finally { + rmSync(home, { recursive: true, force: true }); + } + }); + it("keeps the notify template's subtype filter in sync with the matcher", () => { const template = readFileSync( path.join(import.meta.dir, "..", "templates", "notify-hook.template.sh"),