Skip to content

The agent that grows with you

test(desktop): prove the relaunch gate survives a transport that refuses big commands

xxxigmdb6a9c65e6ab
diff --git a/apps/desktop/electron/remote-lifecycle-gate-transport.test.ts b/apps/desktop/electron/remote-lifecycle-gate-transport.test.ts
new file mode 100644
index 00000000000..a4223a24c22
--- /dev/null
+++ b/apps/desktop/electron/remote-lifecycle-gate-transport.test.ts
@@ -0,0 +1,39 @@
+/**
+ * How the relaunch marker gate travels to the remote.
+ *
+ * `ssh.exec` rides the POSIX ControlMaster mux socket, which hands the client's
+ * stdio descriptors over with sendmsg(SCM_RIGHTS) after writing the session
+ * request. A request that still fills the socket buffer (8 KB for a unix-stream
+ * socket on macOS) leaves no room for that descriptor message, so the pass
+ * fails with EMSGSIZE and ssh exits 255 — which the fail-closed gate reports as
+ * "could not prove the install is clear" on every reconnect.
+ */
+
+import assert from 'node:assert/strict'
+
+import { test } from 'vitest'
+
+import { assertRemoteInstallUpdateClear } from './remote-lifecycle'
+import { REMOTE_MARKER_GATE_PY } from './remote-update-marker-programs'
+
+test('POSIX relaunch gate reaches a verdict over a transport that refuses a large command', async () => {
+ const calls: { command: string; stdinData?: string }[] = []
+
+ const ssh = {
+ async exec(command: string, { stdinData }: any = {}) {
+ calls.push({ command, stdinData })
+
+ if (command.length > 4096) {
+ throw Object.assign(new Error('mm_send_fd: sendmsg(0): Message too long'), { code: 255 })
+ }
+
+ return 'CLEAR'
+ }
+ }
+
+ await assertRemoteInstallUpdateClear(ssh, '/home/alice/.hermes', '~/.local/bin/hermes')
+ assert.equal(calls.length, 1)
+ assert.equal(calls[0].stdinData, REMOTE_MARKER_GATE_PY)
+ assert.ok(!calls[0].command.includes('marker_verdict'), 'the judge must not be inlined in argv')
+ assert.ok(calls[0].command.length < 512, `gate command grew to ${calls[0].command.length} bytes`)
+})
diff --git a/apps/desktop/electron/remote-lifecycle-spawn.test.ts b/apps/desktop/electron/remote-lifecycle-spawn.test.ts
index bda1a5f84b4..14f7c628ce3 100644
--- a/apps/desktop/electron/remote-lifecycle-spawn.test.ts
+++ b/apps/desktop/electron/remote-lifecycle-spawn.test.ts
@@ -22,6 +22,7 @@ import {
spawnLogPath,
spawnTokenPath
} from './remote-lifecycle'
+import { shellExec, shellSshDouble } from './remote-ssh-exec.test-helpers'
import type { SshConnection } from './ssh-connection'
const exec: (command: string, options?: ExecOptionsWithStringEncoding) => Promise<{ stdout: string; stderr: string }> =
@@ -87,12 +88,9 @@ async function spawnFixture(launcher: 'setsid' | 'nohup', owned: boolean): Promi
const localPath = (remotePath: string): string => remotePath.replace(/^~/, root)
const env: NodeJS.ProcessEnv = { HOME: root, HERMES_HOME: path.join(root, '.hermes'), PATH: bin, LANG: 'C.UTF-8' }
- const run = async (command: string): Promise<{ stdout: string; stderr: string }> =>
- exec(command, { shell, env, timeout: 10_000 })
+ const run = async (command: string): Promise<{ stdout: string; stderr: string }> => shellExec(command, { shell, env })
- const ssh: Pick<SshConnection, 'exec'> = {
- exec: async (command: string): Promise<string> => (await run(command)).stdout
- }
+ const ssh: Pick<SshConnection, 'exec'> = shellSshDouble({ shell, env })
const hermesPath: string = path.join(root, 'fake hermes')
const hermesHome: string = path.join(root, '.hermes')
diff --git a/apps/desktop/electron/remote-lifecycle-v2-marker.test.ts b/apps/desktop/electron/remote-lifecycle-v2-marker.test.ts
index a633a787ddb..9117126edf9 100644
--- a/apps/desktop/electron/remote-lifecycle-v2-marker.test.ts
+++ b/apps/desktop/electron/remote-lifecycle-v2-marker.test.ts
@@ -10,6 +10,7 @@ import { test } from 'vitest'
import { buildRemoteUpdateObservationCommand, parseRemoteUpdateObservation } from './managed-ssh-update'
import { assertRemoteInstallUpdateClear } from './remote-lifecycle'
+import { shellSshDouble } from './remote-ssh-exec.test-helpers'
const exec = promisify(execCallback)
@@ -22,7 +23,7 @@ test.runIf(process.platform === 'linux')(
const home = await mkdtemp(path.join(os.tmpdir(), 'hermes-v2-marker-'))
const marker = path.join(home, '.hermes-update-in-progress')
const shell = (await exec('command -v bash')).stdout.trim()
- const ssh = { exec: async (command: string) => (await exec(command, { shell })).stdout }
+ const ssh = shellSshDouble({ shell })
const exited = spawn(process.execPath, ['-e', ''])
await new Promise(resolve => exited.once('exit', resolve))
// The updater's v2 claim: pid, started_at, creation-time line (A2), then tagged lines.
@@ -76,7 +77,7 @@ test.runIf(process.platform === 'linux')(
async () => {
const home = await mkdtemp(path.join(os.tmpdir(), 'hermes-v2-shapes-'))
const marker = path.join(home, '.hermes-update-in-progress')
- const ssh = { exec: async (command: string) => (await exec(command, { shell: 'sh' })).stdout }
+ const ssh = shellSshDouble()
const target = { ssh, platform: 'Linux', hermesPath: '/opt/hermes/hermes', hermesHome: home }
const correlation = randomUUID()
diff --git a/apps/desktop/electron/remote-ssh-exec.test-helpers.ts b/apps/desktop/electron/remote-ssh-exec.test-helpers.ts
new file mode 100644
index 00000000000..be94bd86eae
--- /dev/null
+++ b/apps/desktop/electron/remote-ssh-exec.test-helpers.ts
@@ -0,0 +1,62 @@
+/**
+ * A local stand-in for `SshConnection.exec` used by the remote-lifecycle
+ * suites: runs the command through a POSIX shell and, like the real mux exec,
+ * writes `stdinData` to the remote process's stdin. A double that drops it
+ * leaves the programs the lifecycle ships on stdin (`python3 -`) reading the
+ * test runner's own stdin instead of the program they were handed.
+ */
+
+import { spawn } from 'node:child_process'
+
+export interface ShellExecOptions {
+ shell?: string
+ env?: NodeJS.ProcessEnv
+ timeoutMs?: number
+}
+
+export interface ShellExecResult {
+ stdout: string
+ stderr: string
+}
+
+/** Rejection shape of the promisified `child_process.exec` these doubles replaced. */
+export interface ShellExecFailure extends Error, ShellExecResult {
+ code: number | string
+}
+
+export function shellExec(
+ command: string,
+ { shell = 'sh', env, timeoutMs = 10_000, stdinData }: ShellExecOptions & { stdinData?: string } = {}
+): Promise<ShellExecResult> {
+ return new Promise((resolve, reject) => {
+ const child = spawn(shell, ['-c', command], { env, stdio: 'pipe', timeout: timeoutMs })
+ let stdout = ''
+ let stderr = ''
+
+ child.stdout.setEncoding('utf8').on('data', chunk => (stdout += chunk))
+ child.stderr.setEncoding('utf8').on('data', chunk => (stderr += chunk))
+ child.once('error', reject)
+ child.once('close', (code, signal) => {
+ if (code === 0) {
+ resolve({ stdout, stderr })
+
+ return
+ }
+
+ const failure = new Error(stderr.trim() || `${shell} -c exited ${code ?? signal}`) as ShellExecFailure
+ failure.code = code ?? (signal as string)
+ failure.stdout = stdout
+ failure.stderr = stderr
+ reject(failure)
+ })
+ child.stdin.end(stdinData ?? '')
+ })
+}
+
+/** `{ exec }` with the real exec's `(command, { stdinData })` signature. */
+export function shellSshDouble(options: ShellExecOptions = {}) {
+ return {
+ exec: async (command: string, { stdinData }: { stdinData?: string } = {}): Promise<string> =>
+ (await shellExec(command, { ...options, stdinData })).stdout
+ }
+}