Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(grok): heal a corrupt remote hook file, and make the GROK_HOME tests able to fail

enes214016ee47c7
diff --git a/src/main/remote-ssh/remote-hooks.test.ts b/src/main/remote-ssh/remote-hooks.test.ts
index d30ab76c3..01aae924a 100644
--- a/src/main/remote-ssh/remote-hooks.test.ts
+++ b/src/main/remote-ssh/remote-hooks.test.ts
@@ -174,14 +174,32 @@ describe('RemoteHooks.setup', () => {
})
describe('RemoteHooks.setup — grok', () => {
+ // The host's $GROK_HOME is deliberately a path the `$HOME/.grok` fallback could NEVER produce.
+ // With the two byte-identical (`$HOME: /home/dev` + `GROK_HOME: /home/dev/.grok`) the whole
+ // resolution — probe, validation, override — can be deleted for `${home}/.grok` and every
+ // assertion still passes. The `/home/dev/.grok` negative assertions are what make that fail.
+ const GROK_EVENTS = [
+ 'Notification',
+ 'PostToolUse',
+ 'PostToolUseFailure',
+ 'PreToolUse',
+ 'SessionEnd',
+ 'SessionStart',
+ 'Stop',
+ 'StopFailure',
+ 'UserPromptSubmit'
+ ]
+
it('writes our hook file under the HOST\'s $GROK_HOME with the `.*` tool matcher', async () => {
const { rh, conn, runs } = harness({
// The host answers $HOME first (existing behavior), then its own $GROK_HOME.
- responses: { '$HOME': '/home/dev', 'GROK_HOME': '/home/dev/.grok' }
+ responses: { '$HOME': '/home/dev', 'GROK_HOME': '/opt/grok-home' }
})
await rh.setup('p1', conn, '/s.sock', { port: 1234, token: 't', version: '1' })
- const write = runs.find((r) => r.cmd.includes('/home/dev/.grok/hooks/nodeterm-status.json') && r.stdin)
+ const write = runs.find((r) => r.cmd.includes('/opt/grok-home/hooks/nodeterm-status.json') && r.stdin)
expect(write).toBeTruthy()
+ // The HOST's answer wins outright: nothing may touch the $HOME/.grok default.
+ expect(runs.some((r) => r.cmd.includes('/home/dev/.grok'))).toBe(false)
const cfg = JSON.parse(write!.stdin!)
expect(cfg.hooks.PreToolUse[0].matcher).toBe('.*')
expect(cfg.hooks.Stop[0].matcher).toBeUndefined()
@@ -191,6 +209,20 @@ describe('RemoteHooks.setup — grok', () => {
expect(script!.stdin).toContain('/hook/grok')
})
+ it('trims the probe answer, so a trailing newline still resolves to the host path', async () => {
+ // `printf %s` should emit no newline, but a shell wrapper or a login-shell banner can add one —
+ // and `isSafeRemoteGrokHome` REFUSES an untrimmed string by design (an embedded newline is a
+ // command separator). Without the trim at the read site every host silently gets the fallback.
+ const { rh, conn, runs } = harness({
+ responses: { '$HOME': '/home/dev', 'GROK_HOME': '/opt/grok-home\n' }
+ })
+ await rh.setup('p1', conn, '/s.sock', { port: 1234, token: 't', version: '1' })
+ expect(runs.some((r) => r.cmd.includes("'/opt/grok-home/hooks/nodeterm-status.json'"))).toBe(true)
+ expect(runs.some((r) => r.cmd.includes('/home/dev/.grok'))).toBe(false)
+ // and no raw newline survives into any remote command line.
+ expect(runs.some((r) => r.cmd.includes('/opt/grok-home\n'))).toBe(false)
+ })
+
it('falls back to $HOME/.grok when the host reports an unusable GROK_HOME', async () => {
const { rh, conn, runs } = harness({ responses: { '$HOME': '/home/dev', 'GROK_HOME': 'relative/oops' } })
await rh.setup('p1', conn, '/s.sock', { port: 1234, token: 't', version: '1' })
@@ -198,13 +230,31 @@ describe('RemoteHooks.setup — grok', () => {
expect(runs.some((r) => r.cmd.includes('relative/oops'))).toBe(false)
})
- it('never clobbers a present-but-malformed remote hook file', async () => {
+ it('HEALS a present-but-malformed remote hook file (the file is ours to rewrite)', async () => {
+ // The opposite of the codex/AGENT_TARGETS guard, on purpose: `nodeterm-status.json` is a file
+ // WE own by name and rewrite wholesale, so there is no user content to preserve — and skipping
+ // the write would leave that host's grok nodes dark forever with no in-app repair. The local
+ // installer (installHooksInto) already heals; this matches it.
+ const { rh, conn, runs } = harness({
+ responses: { '$HOME': '/home/dev', 'GROK_HOME': '/opt/grok-home', 'nodeterm-status.json': '{ oops' }
+ })
+ await rh.setup('p1', conn, '/s.sock', { port: 1234, token: 't', version: '1' })
+ const write = runs.find((r) => r.cmd.includes('cat > ') && r.cmd.includes('nodeterm-status.json'))
+ expect(write).toBeTruthy()
+ const cfg = JSON.parse(write!.stdin!)
+ expect(Object.keys(cfg.hooks).sort()).toEqual(GROK_EVENTS)
+ })
+
+ it('quotes the remote dirname, so a $GROK_HOME containing a space still installs', async () => {
+ // isSafeRemoteGrokHome deliberately permits spaces. An unquoted `mkdir -p $(dirname '…')`
+ // word-splits into two args, never creates the directory, and the correctly-quoted `cat >`
+ // then fails — fail-open, so the only symptom is a host with no hooks and no diagnostic.
const { rh, conn, runs } = harness({
- responses: { '$HOME': '/home/dev', 'GROK_HOME': '/home/dev/.grok', 'nodeterm-status.json': '{ oops' }
+ responses: { '$HOME': '/home/dev', 'GROK_HOME': '/opt/my grok' }
})
await rh.setup('p1', conn, '/s.sock', { port: 1234, token: 't', version: '1' })
const write = runs.find((r) => r.cmd.includes('cat > ') && r.cmd.includes('nodeterm-status.json'))
- expect(write).toBeUndefined()
+ expect(write!.cmd).toContain(`mkdir -p "$(dirname '/opt/my grok/hooks/nodeterm-status.json')"`)
})
it('a grok failure never breaks the connect (fail open)', async () => {
diff --git a/src/main/remote-ssh/remote-hooks.ts b/src/main/remote-ssh/remote-hooks.ts
index ecadfc094..4bf0cfb90 100644
--- a/src/main/remote-ssh/remote-hooks.ts
+++ b/src/main/remote-ssh/remote-hooks.ts
@@ -252,6 +252,11 @@ export class RemoteHooks {
* $HOME-relative when the host sets GROK_HOME — which we therefore ASK THE HOST for, and
* validate, because a host-reported string is data and not truth.
*
+ * Because the file is ours outright, an unparseable one is HEALED (parse error → `{}` → write our
+ * fresh config), exactly as the local installer does (`installHooksInto`). Skipping the write
+ * instead — the guard codex and the AGENT_TARGETS loop need, because those write the USER's
+ * files — would leave a host's grok nodes permanently dark with no in-app repair.
+ *
* Fail-open at every step: a remote grok session simply runs without status hooks.
*/
private async installGrokRemote(
@@ -267,8 +272,12 @@ export class RemoteHooks {
// Trim at the READ site: isSafeRemoteGrokHome judges the exact string we would go on to
// interpolate into a remote command line, so it (correctly) refuses an untrimmed value.
const reported = rawHome.trim()
- const grokHome = isSafeRemoteGrokHome(reported) ? reported.replace(/\/+$/, '') : `${home}/.grok`
- const config = `${grokHome}/hooks/${GROK_HOOK_FILE}`
+ // `|| '/'`: a host that genuinely reports `/` means `/`, and letting the strip leave `''`
+ // would make `grokHome` a value no host ever said.
+ const stripped = reported.replace(/\/+$/, '') || '/'
+ const grokHome = isSafeRemoteGrokHome(reported) ? stripped : `${home}/.grok`
+ // Joined so the separator is never doubled (`//hooks` is implementation-defined in POSIX).
+ const config = `${grokHome.replace(/\/$/, '')}/hooks/${GROK_HOOK_FILE}`
const script = `${remoteDir}/agent-hooks/grok.sh`
await this.r.run(
@@ -279,15 +288,23 @@ export class RemoteHooks {
),
buildManagedScript('grok')
)
- // `|| echo '{}'` fires ONLY when the file is missing, so a present-but-malformed file reaches
- // JSON.parse and throws → we skip the write and leave the host's file alone.
+ // `|| echo '{}'` fires ONLY when the file is missing — still the read that distinguishes a
+ // missing file from an unreadable one. An unreadable one is then HEALED, not preserved: this
+ // file is ours by name and we rewrite it wholesale, so there is no user content to lose.
const { stdout: cfgRaw } = await this.r.run(
childArgs(conn, controlPath, `cat ${posixQuote(config)} 2>/dev/null || echo '{}'`)
)
- const cfg = JSON.parse(cfgRaw || '{}') as HookSettings
+ let cfg: HookSettings = {}
+ try {
+ cfg = JSON.parse(cfgRaw || '{}') as HookSettings
+ } catch {
+ cfg = {}
+ }
const merged = mergeManagedHook(cfg, buildManagedHookCommand(script), GROK_HOOK_EVENTS)
+ // The `$(dirname …)` is QUOTED: a valid $GROK_HOME may contain spaces, which would otherwise
+ // word-split into two mkdir args, leave the directory absent, and fail the quoted `cat >`.
await this.r.run(
- childArgs(conn, controlPath, `mkdir -p $(dirname ${posixQuote(config)}) && cat > ${posixQuote(config)}`),
+ childArgs(conn, controlPath, `mkdir -p "$(dirname ${posixQuote(config)})" && cat > ${posixQuote(config)}`),
JSON.stringify(merged, null, 2)
)
} catch {