Skip to content

The agent that grows with you

fix(desktop): resolve the serve subcommand positionally when a profile is named "serve"

beardthelion399bf7938fde
diff --git a/apps/desktop/electron/backend-command.test.ts b/apps/desktop/electron/backend-command.test.ts
index 8d38a0ed427..f4d8edc62eb 100644
--- a/apps/desktop/electron/backend-command.test.ts
+++ b/apps/desktop/electron/backend-command.test.ts
@@ -24,6 +24,56 @@ test('dashboardFallbackArgs preserves a --profile flag ahead of serve', () => {
])
})
+test('dashboardFallbackArgs skips a profile named "serve" and rewrites the subcommand', () => {
+ // The profile VALUE 'serve' is not the subcommand: rewriting at index 1
+ // yields `--profile dashboard --no-open serve`, which still runs `serve`.
+ const serve = ['-m', 'hermes_cli.main', '--profile', 'serve', 'serve', '--host', '127.0.0.1', '--port', '0']
+ assert.deepEqual(dashboardFallbackArgs(serve), [
+ '-m',
+ 'hermes_cli.main',
+ '--profile',
+ 'serve',
+ 'dashboard',
+ '--no-open',
+ '--host',
+ '127.0.0.1',
+ '--port',
+ '0'
+ ])
+})
+
+test('dashboardFallbackArgs skips a -p profile value named "serve"', () => {
+ const serve = ['-p', 'serve', 'serve', '--host', '127.0.0.1', '--port', '0']
+ assert.deepEqual(dashboardFallbackArgs(serve), [
+ '-p',
+ 'serve',
+ 'dashboard',
+ '--no-open',
+ '--host',
+ '127.0.0.1',
+ '--port',
+ '0'
+ ])
+})
+
+test('dashboardFallbackArgs rewrites through a --profile=serve self-contained flag', () => {
+ const serve = ['--profile=serve', 'serve', '--host', '127.0.0.1', '--port', '0']
+ assert.deepEqual(dashboardFallbackArgs(serve), [
+ '--profile=serve',
+ 'dashboard',
+ '--no-open',
+ '--host',
+ '127.0.0.1',
+ '--port',
+ '0'
+ ])
+})
+
+test('dashboardFallbackArgs leaves a serve-looking profile value alone when there is no subcommand', () => {
+ const args = ['--profile', 'serve']
+ assert.deepEqual(dashboardFallbackArgs(args), args)
+})
+
test('dashboardFallbackArgs is a no-op (copy) when there is no serve token', () => {
const args = ['-m', 'hermes_cli.main', 'dashboard', '--no-open']
const out = dashboardFallbackArgs(args)
diff --git a/apps/desktop/electron/backend-command.ts b/apps/desktop/electron/backend-command.ts
index 23cb87d4f78..e38d5275715 100644
--- a/apps/desktop/electron/backend-command.ts
+++ b/apps/desktop/electron/backend-command.ts
@@ -21,16 +21,25 @@ export function serveBackendArgs(profile?: string) {
return [...head, 'serve', '--host', '127.0.0.1', '--port', '0']
}
+// Flags that consume the next token; the subcommand is the first bare token
+// that is not one of their values. `--profile=serve` never collides (it is a
+// different string), but the two-token `--profile serve` / `-p serve` do.
+const VALUE_FLAGS = new Set(['-m', '--profile', '-p'])
+
/**
* Rewrite a resolved backend argv from `serve` to the legacy
* `dashboard --no-open` form, preserving every other argument (incl. a leading
* `-m hermes_cli.main` and any `--profile <name>`). Returns a copy; if there is
- * no `serve` token the argv is returned unchanged.
+ * no `serve` subcommand token the argv is returned unchanged.
*/
export function dashboardFallbackArgs(args) {
- const i = args.indexOf('serve')
+ let i = 0
+
+ while (i < args.length && args[i] !== 'serve') {
+ i += VALUE_FLAGS.has(args[i]) ? 2 : 1
+ }
- if (i === -1) {
+ if (i >= args.length) {
return args.slice()
}
diff --git a/apps/desktop/electron/remote-lifecycle.test.ts b/apps/desktop/electron/remote-lifecycle.test.ts
index bbabd65bb7f..2eadadf1035 100644
--- a/apps/desktop/electron/remote-lifecycle.test.ts
+++ b/apps/desktop/electron/remote-lifecycle.test.ts
@@ -755,6 +755,36 @@ test.skipIf(process.platform === 'win32')(
false,
'a duplicate conflicting profile must remain foreign'
)
+
+ const serveNamedProfile = spawnInstaller(['--profile', 'serve', 'serve', '--isolated', ...backendFlags])
+
+ assert.equal(await waitForEntrypoint(serveNamedProfile), true)
+ assert.equal(
+ await pidIsOurDashboard(
+ ssh,
+ serveNamedProfile.pid,
+ SPAWN_NONCE,
+ launcher,
+ '/unrelated/hermes-home',
+ OWNERSHIP_ID,
+ 'serve'
+ ),
+ true,
+ 'a profile named "serve" must not mask the real serve subcommand'
+ )
+ assert.equal(
+ await pidIsOurDashboard(
+ ssh,
+ serveNamedProfile.pid,
+ SPAWN_NONCE,
+ launcher,
+ '/unrelated/hermes-home',
+ OWNERSHIP_ID,
+ 'ops'
+ ),
+ false,
+ 'a "serve"-named profile must still match the expected profile exactly'
+ )
} finally {
for (const process of children) {
process.kill('SIGTERM')
@@ -765,6 +795,97 @@ test.skipIf(process.platform === 'win32')(
}
)
+test.skipIf(process.platform !== 'linux')(
+ 'terminateOwnedDashboardForUpdate SIGTERMs a serve pinned to a "serve"-named profile',
+ async () => {
+ const temp = await mkdtemp(path.join(os.tmpdir(), 'hermes wrapper ownership '))
+ const installDir = path.join(temp, 'install dir')
+ const venvBin = path.join(installDir, 'venv', 'bin')
+ const pythonLink = path.join(venvBin, 'python')
+ const entrypoint = path.join(installDir, 'hermes')
+ const launcher = path.join(temp, 'hermes launcher')
+ const python = (await exec('command -v python3')).stdout.trim()
+ const tokenPath = path.join(os.homedir(), spawnTokenPath(OWNERSHIP_ID, SPAWN_NONCE).replace(/^~\//, ''))
+
+ await mkdir(venvBin, { recursive: true })
+ await symlink(python, pythonLink)
+ await writeFile(entrypoint, 'import time\ntime.sleep(30)\n', 'utf8')
+ await writeFile(launcher, `#!/bin/bash\nexec "${pythonLink}" "${entrypoint}" "$@"\n`, 'utf8')
+ await chmod(launcher, 0o755)
+
+ const child = spawn(
+ launcher,
+ [
+ '--profile',
+ 'serve',
+ 'serve',
+ '--isolated',
+ '--host',
+ '127.0.0.1',
+ '--port',
+ '0',
+ '--ssh-session-token-file',
+ tokenPath,
+ '--ssh-owner-nonce',
+ SPAWN_NONCE
+ ],
+ { stdio: 'ignore' }
+ )
+
+ const exited = new Promise(resolve => child.once('exit', resolve))
+
+ try {
+ let execed = false
+
+ for (let attempt = 0; attempt < 40 && !execed; attempt += 1) {
+ const command = (await exec(`ps -ww -o command= -p ${child.pid}`)).stdout
+
+ execed = command.includes(entrypoint)
+
+ if (!execed) {
+ await new Promise(resolve => setTimeout(resolve, 25))
+ }
+ }
+
+ assert.equal(execed, true, 'wrapper must exec into the fake installer entrypoint')
+
+ const raw = await readFile(`/proc/${child.pid}/stat`, 'utf8')
+
+ const creationTime = `linux:${raw
+ .slice(raw.lastIndexOf(')') + 2)
+ .trim()
+ .split(/\s+/)[19]}`
+
+ const lock = ownedLock({
+ pid: child.pid,
+ profile: 'serve',
+ hermesPath: launcher,
+ hermesHome: '/unrelated/hermes-home',
+ creationTime
+ })
+
+ const ssh = {
+ exec: async (command: string) => {
+ if (/cat .*\.json/.test(command)) {
+ return `${JSON.stringify(lock)}\n`
+ }
+
+ return (await exec(command, { shell: '/bin/bash' })).stdout
+ }
+ }
+
+ const result = await terminateOwnedDashboardForUpdate(ssh, lock)
+
+ assert.equal(result.terminated, true)
+ await exited
+ assert.equal(child.signalCode, 'SIGTERM')
+ } finally {
+ child.kill('SIGKILL')
+ await rm(temp, { force: true, recursive: true })
+ }
+ }
+)
+
test('disconnect reaps the backend recorded for this desktop ownership', async () => {
const lock = ownedLock()
diff --git a/apps/desktop/electron/remote-lifecycle.ts b/apps/desktop/electron/remote-lifecycle.ts
index ca83ccecb90..a2f0783443d 100644
--- a/apps/desktop/electron/remote-lifecycle.ts
+++ b/apps/desktop/electron/remote-lifecycle.ts
@@ -682,12 +682,16 @@ async function pidIsOurDashboard(
' args=shlex.split(line)\n' +
'ok=False\n' +
'try:\n' +
- ' serve=args.index("serve")\n' +
+ // A profile literally named "serve" puts the value token before the
+ // subcommand; both index("serve") and count("serve") must skip it.
+ ' profile_arg=args.index("--profile") if expected_profile else -1\n' +
+ ' pval=profile_arg+1 if profile_arg>=0 else -1\n' +
+ ' serve_pos=[i for i,a in enumerate(args) if a=="serve" and i!=pval]\n' +
+ ' serve=serve_pos[0]\n' +
' owner=args.index("--ssh-owner-nonce",serve+1)\n' +
' token=args.index("--ssh-session-token-file",serve+1) if expected_token else -1\n' +
' isolated=args.index("--isolated",serve+1)\n' +
- ' profile_arg=args.index("--profile") if expected_profile else -1\n' +
- ' serve_count=args.count("serve")\n' +
+ ' serve_count=len(serve_pos)\n' +
' owner_count=args.count("--ssh-owner-nonce")\n' +
' token_count=args.count("--ssh-session-token-file")\n' +
' isolated_count=args.count("--isolated")\n' +
@@ -905,16 +909,20 @@ def identity_before_signal():
def owned(args):
try:
- serve=args.index("serve")
+ # Same "serve"-named profile collision as pidIsOurDashboard: skip the
+ # --profile value token when locating and counting the subcommand.
+ profile_arg=args.index("--profile") if expected_profile else -1
+ pval=profile_arg+1 if profile_arg>=0 else -1
+ serve_pos=[i for i,a in enumerate(args) if a=="serve" and i!=pval]
+ serve=serve_pos[0]
owner=args.index("--ssh-owner-nonce",serve+1)
token=args.index("--ssh-session-token-file",serve+1)
isolated=args.index("--isolated",serve+1)
- profile_arg=args.index("--profile") if expected_profile else -1
direct=args[0] in expected_entries
python_entry=len(args)>1 and args[1] in expected_entries and os.path.basename(args[0]).startswith("python")
profile_ok=(args.count("--profile")==1 and profile_arg<serve and args[profile_arg+1]==expected_profile) if expected_profile else args.count("--profile")==0
return ((direct or python_entry or (args[token+1]==expected_token and profile_ok)) and
- args.count("serve")==1 and args.count("--ssh-owner-nonce")==1 and
+ len(serve_pos)==1 and args.count("--ssh-owner-nonce")==1 and
args.count("--ssh-session-token-file")==1 and args.count("--isolated")==1 and
isolated>serve and args[owner+1]==nonce and args[token+1]==expected_token and profile_ok)
except (ValueError,IndexError):return False