The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): diagnose an ensure child that produced no JSON instead of "Unexpected end of JSON input"
teknium1c776f41ea274
diff --git a/apps/desktop/electron/local-gateway.test.ts b/apps/desktop/electron/local-gateway.test.tsindex 2a732e17ecd..c50a14ac02b 100644--- a/apps/desktop/electron/local-gateway.test.ts+++ b/apps/desktop/electron/local-gateway.test.ts@@ -91,6 +91,17 @@ test('ensure consumes structured readiness without acquiring a child owner', asyawait expect(ensureLocalGateway(async () => ({ code: 5, stdout: JSON.stringify({ state: 'starting', reason_code: 'deadline' }) }))).rejects.toThrow('starting')})+test('an ensure child that never reached the protocol boundary is diagnosed from stderr, not as a JSON parse error', async () => {+ // `main`'s hermes has no `gateway ensure` subcommand: argparse usage on stderr, nothing on stdout.+ const legacy = { code: 2, stdout: '', stderr: "usage: hermes gateway [-h] ...\nhermes gateway: 'ensure' is not a `hermes gateway` command.\nRun `hermes gateway --help` to see all commands.\n" }+ await expect(ensureLocalGateway(async () => legacy)).rejects.toThrow(/produced no result \(exit 2\): Run `hermes gateway --help`/)+ // A missing profile: the CLI exits 1 before the ensure command runs.+ await expect(ensureLocalGateway(async () => ({ code: 1, stdout: '', stderr: "Error: Profile 'gone' does not exist.\n" }))).rejects.toThrow(/exit 1\): Error: Profile 'gone' does not exist\./)+ // A protocol outcome is never re-diagnosed: `incompatible` stays the gateway's own verdict.+ await expect(ensureLocalGateway(async () => ({ code: 3, stdout: '{"endpoint":null,"reason_code":"runtime_protocol","state":"incompatible"}', stderr: 'noise' }))).rejects.toThrow('Gateway incompatible (runtime_protocol)')+ await expect(ensureLocalGateway(async () => ({ code: 0, stdout: '', stderr: '' }))).rejects.toThrow(/produced no result \(exit 0\)\. Update Hermes/)+})+test('private dial credential is one-use and bound to the requesting native window', () => {const dials = createLocalGatewayDials()const dial = new URL(dials.prepare('http://127.0.0.1:1234', 'private-ticket', 7))diff --git a/apps/desktop/electron/local-gateway.ts b/apps/desktop/electron/local-gateway.tsindex 65cbcd6b4bd..cd2536d2d86 100644--- a/apps/desktop/electron/local-gateway.ts+++ b/apps/desktop/electron/local-gateway.ts@@ -3,18 +3,21 @@ import crypto from 'node:crypto'import { hiddenWindowsChildOptions } from './windows-child-options'-export function runGatewayEnsure(backend, cwd: string, home: string): Promise<{ code: number; stdout: string }> {+export function runGatewayEnsure(backend, cwd: string, home: string): Promise<{ code: number; stdout: string; stderr: string }> {return new Promise((resolve, reject) => {const child = spawn(backend.command, backend.args, hiddenWindowsChildOptions({ cwd, env: { ...process.env, HERMES_HOME: home, ...backend.env }, shell: backend.shell, stdio: ['ignore', 'pipe', 'pipe'] }))let stdout = ''+ let stderr = ''// Only the bounded ensure client is ours. Never retain/kill its detached owner.const timer = setTimeout(() => child.kill(), 40_000)child.stdout.on('data', data => { stdout += data.toString();if (stdout.length > 65536) {child.kill()} })- child.stderr.resume()+ // Diagnostics only (never protocol): an older `hermes` without the subcommand, a missing+ // profile or an import crash explain themselves here while stdout stays empty.+ child.stderr.on('data', data => { stderr = (stderr + data.toString()).slice(-4096) })child.on('error', () => { clearTimeout(timer); reject(new Error('Could not run hermes gateway ensure')) })- child.on('close', code => { clearTimeout(timer); resolve({ code: code ?? 7, stdout }) })+ child.on('close', code => { clearTimeout(timer); resolve({ code: code ?? 7, stdout, stderr }) })})}@@ -34,10 +37,29 @@ export interface GatewayEndpoint {control_home?: string | null}-export async function ensureLocalGateway(run: () => Promise<{ code: number; stdout: string }>, beforeEnsure?: () => Promise<void>) {+/** The single-line JSON `hermes gateway ensure --json` prints, or a diagnosis of why there is none.+ *+ * Every protocol outcome (ready/starting/incompatible/...) is JSON on stdout. Empty or+ * non-JSON stdout means the command never reached the protocol boundary: an older `hermes`+ * on PATH that has no `ensure` subcommand, a profile that does not exist, an interpreter+ * crash. A bare JSON.parse there surfaced as "Unexpected end of JSON input" with the real+ * reason discarded on stderr. */+export function parseGatewayEnsureOutput(result: { code: number; stdout: string; stderr?: string }): Record<string, any> {+ try {+ const payload = JSON.parse(result.stdout)++ if (payload && typeof payload === 'object' && !Array.isArray(payload)) {return payload}+ } catch { /* diagnosed below */ }++ const reason = String(result.stderr ?? '').trim().split(/\r?\n/).filter(Boolean).pop()++ throw new Error(`hermes gateway ensure produced no result (exit ${result.code})${reason ? `: ${reason}` : ''}. Update Hermes or check the profile, then retry.`)+}++export async function ensureLocalGateway(run: () => Promise<{ code: number; stdout: string; stderr?: string }>, beforeEnsure?: () => Promise<void>) {await beforeEnsure?.()const result = await run()- const payload = JSON.parse(result.stdout)+ const payload = parseGatewayEnsureOutput(result)if (result.code !== 0 || payload.state !== 'ready') {throw new Error(`Gateway ${payload.state || 'inaccessible'} (${payload.reason_code || 'ensure_failed'}). Use hermes gateway status for recovery.`)