Skip to content

The agent that grows with you

fix(desktop): cold-start-tolerant timeouts for remote backend connections

Finn76378b5b7abb881
diff --git a/apps/desktop/electron/remote-lifecycle.test.ts b/apps/desktop/electron/remote-lifecycle.test.ts
index b8da6875e46..6d03b98e16e 100644
--- a/apps/desktop/electron/remote-lifecycle.test.ts
+++ b/apps/desktop/electron/remote-lifecycle.test.ts
@@ -14,6 +14,7 @@ import {
classifySshReuseProof,
cleanupStale,
connect,
+ DEFAULT_READY_TIMEOUT_MS,
disconnect,
expandRemotePath,
fingerprintToken,
@@ -23,6 +24,7 @@ import {
locateHermes,
LOCKFILE_SCHEMA_VERSION,
lockfilePath,
+ MIN_READY_TIMEOUT_MS,
openForward,
ownershipDirectory,
pidIsOurDashboard,
@@ -34,6 +36,7 @@ import {
READY_RE,
remotePidAlive,
remoteSupportsSshOwnership,
+ resolveReadyTimeoutMs,
scrapeReadyPort,
spawnLogPath,
spawnRemoteDashboard,
@@ -2001,7 +2004,6 @@ test('cleanupStale escalates to SIGKILL when the backend survives the graceful w
hermesPath: '/x/hermes',
logPath: spawnLogPath(OWNERSHIP_ID, SPAWN_NONCE)
})
-
assert.ok(
ssh.calls.some(c => /kill -9 9\b/.test(c)),
'must escalate to SIGKILL after the graceful wait fails'
@@ -2011,7 +2013,6 @@ test('cleanupStale escalates to SIGKILL when the backend survives the graceful w
'lockfile must still be dropped after the forced kill'
)
})
-
test('cleanupStale keeps the lockfile when even SIGKILL cannot confirm the pid died', async () => {
const ssh = fakeSsh([
[/print\("OWNED"/, 'OWNED\n'],
@@ -2028,7 +2029,6 @@ test('cleanupStale keeps the lockfile when even SIGKILL cannot confirm the pid d
}),
/Could not terminate/
)
-
// 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)))
})
@@ -2195,3 +2195,29 @@ test('connect() post-spawn cleanup that cannot prove ownership keeps the origina
'record must survive for the next connect to reap'
)
})
+
+// ---------------------------------------------------------------------------
+// resolveReadyTimeoutMs (issue #94642): cold-start-tolerant remote budgets
+// ---------------------------------------------------------------------------
+test('honors a valid HERMES_DESKTOP_REMOTE_READY_TIMEOUT_MS override', () => {
+ const env = { HERMES_DESKTOP_REMOTE_READY_TIMEOUT_MS: '180000' }
+ assert.equal(resolveReadyTimeoutMs(env), 180_000)
+})
+test('clamps an override below the floor up to the 45s minimum', () => {
+ const env = { HERMES_DESKTOP_REMOTE_READY_TIMEOUT_MS: '1000' }
+ assert.equal(resolveReadyTimeoutMs(env), MIN_READY_TIMEOUT_MS)
+})
+test('rounds a fractional override', () => {
+ const env = { HERMES_DESKTOP_REMOTE_READY_TIMEOUT_MS: '60000.7' }
+ assert.equal(resolveReadyTimeoutMs(env), 60_001)
+})
+test('falls back to the default for malformed / non-positive overrides', () => {
+ for (const bad of ['', 'abc', '0', '-5', 'NaN', undefined]) {
+ const env = bad === undefined ? {} : { HERMES_DESKTOP_REMOTE_READY_TIMEOUT_MS: bad }
+ assert.equal(
+ resolveReadyTimeoutMs(env),
+ DEFAULT_READY_TIMEOUT_MS,
+ `override ${JSON.stringify(bad)} should fall through to the default`
+ )
+ }
+})
diff --git a/apps/desktop/electron/remote-lifecycle.ts b/apps/desktop/electron/remote-lifecycle.ts
index e331ebcc4f9..df1a36c0759 100644
--- a/apps/desktop/electron/remote-lifecycle.ts
+++ b/apps/desktop/electron/remote-lifecycle.ts
@@ -39,7 +39,16 @@ const PROTOCOL_VERSION = 1
const READY_RE = READY_IN_MERGED_OUTPUT_RE // the remote log is `>> log 2>&1`: merged, not line-accurate
const REMOTE_LOCK_DIR = '~/.hermes/desktop-ssh'
const SUPPORTED_REMOTE_OS = new Set(['Linux', 'Darwin'])
-const DEFAULT_READY_TIMEOUT_MS = 45_000
+// On a busy remote host a healthy cold boot can take 60-150s before the
+// freshly spawned `hermes serve --isolated` prints its READY line (event-loop
+// stalls of 5-27s each during spawn storms are routine). The historical 45s
+// budget gave up milliseconds before a healthy backend announced ready and
+// surfaced a timeout toast (issue #94642). A roomier default absorbs the
+// cold-start cost; a warm start still announces in well under a second.
+const DEFAULT_READY_TIMEOUT_MS = 120_000
+// Never trust a deadline tighter than the warm-start path needs; floor at 45s
+// (the historical default) so a malformed override can't reintroduce the loop.
+const MIN_READY_TIMEOUT_MS = 45_000
const READY_POLL_INTERVAL_MS = 750
// macOS sshd starts non-interactive shells with a 256-FD soft limit even when
// the hard limit is unlimited. A Desktop backend can legitimately exceed that
@@ -56,6 +65,22 @@ function classifySshReuseProof(proof, spawnNonce) {
: 'authenticated-stale'
}
+/**
+ * Resolve the remote ready-port deadline. Honors the
+ * HERMES_DESKTOP_REMOTE_READY_TIMEOUT_MS env override (for users on busy
+ * hosts whose cold boots routinely exceed the default), clamped to a sane
+ * floor so a bad value can't make boot flakier than the default.
+ */
+function resolveReadyTimeoutMs(env = process.env) {
+ const parsed = Number(env.HERMES_DESKTOP_REMOTE_READY_TIMEOUT_MS)
+
+ if (Number.isFinite(parsed) && parsed > 0) {
+ return Math.max(MIN_READY_TIMEOUT_MS, Math.round(parsed))
+ }
+
+ return DEFAULT_READY_TIMEOUT_MS
+}
+
function mintToken() {
return crypto.randomBytes(32).toString('hex')
}
@@ -1226,7 +1251,7 @@ async function remoteSupportsSshOwnership(ssh, hermesPath) {
.endsWith('YES')
}
-async function scrapeReadyPort(ssh, logPath, { timeoutMs = DEFAULT_READY_TIMEOUT_MS, isAlive, signal }: any = {}) {
+async function scrapeReadyPort(ssh, logPath, { timeoutMs = resolveReadyTimeoutMs(), isAlive, signal }: any = {}) {
const deadline = Date.now() + timeoutMs
const remoteLog = expandRemotePath(logPath)
@@ -1490,7 +1515,7 @@ async function connect(deps) {
probeReuseProof,
adoptServedToken,
rememberLog = () => {},
- readyTimeoutMs = DEFAULT_READY_TIMEOUT_MS,
+ readyTimeoutMs = resolveReadyTimeoutMs(),
guestOnboarding = false,
signal
} = deps
@@ -1783,6 +1808,7 @@ export {
locateHermes,
LOCKFILE_SCHEMA_VERSION,
lockfilePath,
+ MIN_READY_TIMEOUT_MS,
mintToken,
openForward,
ownershipDirectory,
@@ -1799,6 +1825,7 @@ export {
remoteProcessCreationTime,
remoteSupportsSshOwnership,
removeLockfile,
+ resolveReadyTimeoutMs,
scrapeReadyPort,
shq,
spawnLogPath,
diff --git a/apps/desktop/electron/windows-remote-lifecycle.ts b/apps/desktop/electron/windows-remote-lifecycle.ts
index 6a3d846c633..becfd7fba2d 100644
--- a/apps/desktop/electron/windows-remote-lifecycle.ts
+++ b/apps/desktop/electron/windows-remote-lifecycle.ts
@@ -1,5 +1,6 @@
import crypto from 'node:crypto'
+import { resolveReadyTimeoutMs } from './remote-lifecycle'
import { assertBootstrapNotSuperseded, redactSecrets, SSH_ERROR } from './ssh-connection'
const LOCKFILE_SCHEMA_VERSION = 2
@@ -560,7 +561,7 @@ async function connectWindowsRemote(deps) {
waitForHermes,
probeReuseProof,
rememberLog = () => {},
- readyTimeoutMs = 45_000
+ readyTimeoutMs = resolveReadyTimeoutMs()
} = deps
assertBootstrapNotSuperseded(signal)