Skip to content

The agent that grows with you

fix(desktop): stop double-quoting expandRemotePath fragments in the SSH spawn path

Kolton Jacobs63b77aa593c9
diff --git a/apps/desktop/electron/remote-lifecycle.test.ts b/apps/desktop/electron/remote-lifecycle.test.ts
index dffc716193c..4ca82c2ed90 100644
--- a/apps/desktop/electron/remote-lifecycle.test.ts
+++ b/apps/desktop/electron/remote-lifecycle.test.ts
@@ -1849,3 +1849,73 @@ test('cleanupStale keeps the lockfile when even SIGKILL cannot confirm the pid d
// The record must survive so the next connect's reap pass retries.
assert.ok(!ssh.calls.some(c => /rm -f .*backend\.lock\.json/.test(c)))
})
+test('buildSpawnCommand quotes expandRemotePath fragments exactly once (real sh parse)', async () => {
+ // Regression: expandRemotePath() returns an already-quoted shell fragment
+ // ("$HOME"'/path'). Wrapping such a fragment in shq() again ships literal
+ // quote characters to the remote: python received a mutex path named
+ // '"$HOME"'"'"'/…'"'"'', and the payload's `mkdir "$reservation"` spun
+ // forever on a path that can never exist, so every SSH backend spawn hung
+ // until the connect timeout. Parse the composed command with a real sh the
+ // way the remote login shell does, and require the paths to come out clean.
+ const cmd = buildSpawnCommand('/x/hermes', 'work', {
+ hermesHome: '~/.hermes',
+ logPath: spawnLogPath(OWNERSHIP_ID, SPAWN_NONCE),
+ ownershipId: OWNERSHIP_ID,
+ reservationNonce: SPAWN_NONCE,
+ spawnNonce: SPAWN_NONCE,
+ tokenFilePath: spawnTokenPath(OWNERSHIP_ID, SPAWN_NONCE),
+ lockMetadata: {
+ ownershipId: OWNERSHIP_ID,
+ spawnNonce: SPAWN_NONCE,
+ port: 0,
+ profile: 'work',
+ hermesPath: '/x/hermes',
+ hermesHome: '~/.hermes',
+ logPath: spawnLogPath(OWNERSHIP_ID, SPAWN_NONCE),
+ tokenFingerprint: fingerprintToken('stored-token'),
+ protocolVersion: PROTOCOL_VERSION,
+ startedAt: '2026-07-14T00:00:00.000Z'
+ }
+ })
+
+ // Capture the argv a remote shell would hand to python3, via a shim on PATH.
+ const shimDir = await mkdtemp(path.join(os.tmpdir(), 'hermes-argv-shim-'))
+ const fakeHome = await mkdtemp(path.join(os.tmpdir(), 'hermes-fake-home-'))
+ try {
+ const argvFile = path.join(shimDir, 'argv')
+ await writeFile(
+ path.join(shimDir, 'python3'),
+ `#!/bin/sh\nprintf '%s\\0' "$@" > ${argvFile}\n`,
+ { mode: 0o755 }
+ )
+ await promisify(execCallback)(cmd, {
+ env: { ...process.env, PATH: `${shimDir}:${process.env.PATH}`, HOME: fakeHome }
+ })
+ const argv = (await readFile(argvFile, 'utf8')).split('\0')
+
+ // argv: ['-c', <mutex script>, <mutex path>, <payload>]
+ const mutexPath = argv[2]
+ assert.equal(
+ mutexPath,
+ `${fakeHome}/.hermes/.hermes-update-in-progress.mutex`,
+ 'mutex path must reach python fully expanded, with no quote characters'
+ )
+
+ // The payload assigns reservation/lock/owner_file before its mkdir loop.
+ // Evaluate that prefix the way the remote sh does and require real paths.
+ const payload = argv[3]
+ const prefix = payload.slice(0, payload.indexOf('i=0;'))
+ const { stdout } = await promisify(execCallback)(
+ `${prefix} printf '%s\\n' "$reservation" "$lock" "$owner_file"`,
+ { env: { ...process.env, HOME: fakeHome } }
+ )
+ const [reservation, lock, ownerFile] = stdout.split('\n')
+ const base = `${fakeHome}/.hermes/desktop-ssh/${OWNERSHIP_ID}`
+ assert.equal(reservation, `${base}/.connect.lock`)
+ assert.equal(lock, `${base}/backend.lock.json`)
+ assert.equal(ownerFile, `${base}/.connect.lock/owner`)
+ } finally {
+ await rm(shimDir, { recursive: true, force: true })
+ await rm(fakeHome, { recursive: true, force: true })
+ }
+})
diff --git a/apps/desktop/electron/remote-lifecycle.ts b/apps/desktop/electron/remote-lifecycle.ts
index d096705c95c..17c98a97f5e 100644
--- a/apps/desktop/electron/remote-lifecycle.ts
+++ b/apps/desktop/electron/remote-lifecycle.ts
@@ -913,7 +913,8 @@ finally:
sys.exit(result.returncode if result is not None else 1)
`.trim()
- return `python3 -c ${shq(script)} ${shq(mutexPath)} ${shq(command)}`
+ // mutexPath is an expandRemotePath fragment — already quoted; do not shq again.
+ return `python3 -c ${shq(script)} ${mutexPath} ${shq(command)}`
}
/**