Skip to content

The agent that grows with you

test(desktop): pin the bot_relay.deliver budget by import, not by regexing relay.ts

kshitijk4poor7e6b8d9ec8f8
diff --git a/apps/desktop/src/plugins/hermes-bots/relay-budget.ts b/apps/desktop/src/plugins/hermes-bots/relay-budget.ts
new file mode 100644
index 00000000000..a7a740d3753
--- /dev/null
+++ b/apps/desktop/src/plugins/hermes-bots/relay-budget.ts
@@ -0,0 +1,30 @@
+// #93911: a delivered turn runs on the target gateway, so the client must
+// outlive the backend's own bound. Without this the call fell to the pool's
+// generic 30s deadline and every long turn (Computer Use, deep research) came
+// back as an unclassified failure.
+//
+// The backend's MAXIMUM WORK budget is spelled out below. The client deadline
+// must be strictly GREATER than it: after those bounded waits the handler still
+// has to classify the failure, build and run the retry, classify/serialize the
+// terminal result, unwind the temp-file and lock scopes, and get the JSON-RPC
+// response back through the event loop. A call that consumes nearly all of the
+// work budget would otherwise lose the race to this timer by milliseconds and
+// reproduce #93911 at the upper boundary — the backend knowing a typed reason
+// while Desktop reports its generic timeout first.
+//
+// These three are mirrors of backend values, so a change there must not
+// silently invalidate this constant: relay-deliver-budget.test.ts compares
+// them with hermes_cli/config_defaults.py and tools/bot_relay.py and fails if
+// the mirrors drift or the margin stops being positive.
+export const RELAY_TURN_LOCK_WAIT_MS = 120_000 // bot_mode.turn_wait_seconds default
+export const RELAY_TURN_ATTEMPT_MS = 600_000 // tools/bot_relay.py TURN_ATTEMPT_TIMEOUT_SECONDS
+export const RELAY_TURN_MAX_ATTEMPTS = 2 // first attempt + the policy-gated re-run
+
+export const RELAY_DELIVER_BACKEND_CEILING_MS =
+ RELAY_TURN_LOCK_WAIT_MS + RELAY_TURN_ATTEMPT_MS * RELAY_TURN_MAX_ATTEMPTS
+
+// Settlement + transport headroom on top of the ceiling, so a backend that
+// answers at its own limit still wins the race against this timer.
+export const RELAY_DELIVER_SETTLEMENT_MARGIN_MS = 180_000
+// tools/bot_relay.py REPLY_WAIT_SECONDS rebuilds this sum and waits past it for the relay's timeout reply.
+export const RELAY_DELIVER_TIMEOUT_MS = RELAY_DELIVER_BACKEND_CEILING_MS + RELAY_DELIVER_SETTLEMENT_MARGIN_MS
diff --git a/apps/desktop/src/plugins/hermes-bots/relay-deliver-budget.test.ts b/apps/desktop/src/plugins/hermes-bots/relay-deliver-budget.test.ts
index 97c82ad8fe0..4f577e98a22 100644
--- a/apps/desktop/src/plugins/hermes-bots/relay-deliver-budget.test.ts
+++ b/apps/desktop/src/plugins/hermes-bots/relay-deliver-budget.test.ts
@@ -3,25 +3,26 @@ import { join } from 'node:path'
import { describe, expect, it } from 'vitest'
+import {
+ RELAY_DELIVER_BACKEND_CEILING_MS,
+ RELAY_DELIVER_SETTLEMENT_MARGIN_MS,
+ RELAY_DELIVER_TIMEOUT_MS,
+ RELAY_TURN_ATTEMPT_MS,
+ RELAY_TURN_LOCK_WAIT_MS,
+ RELAY_TURN_MAX_ATTEMPTS
+} from './relay-budget'
+
// #93911 review follow-up: the Desktop deadline for bot_relay.deliver mirrors
// three backend numbers. Nothing in the type system links a TS constant to a
-// Python default, so this file is the seam: it reads the backend sources and
+// Python default, so this file is the seam: it reads the backend defaults and
// fails when a mirror drifts or the settlement margin stops being positive.
// Without it, raising the backend turn timeout would silently reintroduce
// #93911 — the client giving up before a valid typed settlement arrives.
-const relaySource = readFileSync(join(process.cwd(), 'src/plugins/hermes-bots/relay.ts'), 'utf8')
const repoRoot = join(process.cwd(), '..', '..')
const configDefaults = readFileSync(join(repoRoot, 'hermes_cli/config_defaults.py'), 'utf8')
const relayPlumbing = readFileSync(join(repoRoot, 'tools/bot_relay.py'), 'utf8')
-function tsConstant(name: string): number {
- const match = relaySource.match(new RegExp(`const ${name} = ([0-9_]+)`))
- expect(match, `${name} must stay a literal so this test can read it`).toBeTruthy()
-
- return Number(match![1].replaceAll('_', ''))
-}
-
function pyConstant(name: string): number {
const match = relayPlumbing.match(new RegExp(`^${name}\\s*=\\s*(\\d+)`, 'm'))
expect(match, `${name} must exist as a literal in tools/bot_relay.py`).toBeTruthy()
@@ -34,37 +35,25 @@ describe('bot_relay.deliver budget mirrors', () => {
const lockWaitMatch = configDefaults.match(/"turn_wait_seconds":\s*(\d+)/)
expect(lockWaitMatch, 'bot_mode.turn_wait_seconds default must exist in config_defaults.py').toBeTruthy()
- expect(tsConstant('RELAY_TURN_LOCK_WAIT_MS')).toBe(Number(lockWaitMatch![1]) * 1000)
+ expect(RELAY_TURN_LOCK_WAIT_MS).toBe(Number(lockWaitMatch![1]) * 1000)
})
it('mirrors the backend per-attempt turn timeout', () => {
// The backend names both numbers explicitly (tools/bot_relay.py) so the mirror is a
// constant-to-constant check, not a count of textual subprocess.run(...) call sites.
- expect(tsConstant('RELAY_TURN_ATTEMPT_MS')).toBe(pyConstant('TURN_ATTEMPT_TIMEOUT_SECONDS') * 1000)
- expect(tsConstant('RELAY_TURN_MAX_ATTEMPTS')).toBe(pyConstant('TURN_MAX_ATTEMPTS'))
+ expect(RELAY_TURN_ATTEMPT_MS).toBe(pyConstant('TURN_ATTEMPT_TIMEOUT_SECONDS') * 1000)
+ expect(RELAY_TURN_MAX_ATTEMPTS).toBe(pyConstant('TURN_MAX_ATTEMPTS'))
})
it('shares its settlement margin with the sender-side waiter budget', () => {
// tests/tools/test_bot_relay.py checks that REPLY_WAIT_SECONDS exceeds the rebuilt sum.
- expect(tsConstant('RELAY_DELIVER_SETTLEMENT_MARGIN_MS')).toBe(
- pyConstant('DESKTOP_DELIVER_SETTLEMENT_MARGIN_SECONDS') * 1000
- )
+ expect(RELAY_DELIVER_SETTLEMENT_MARGIN_MS).toBe(pyConstant('DESKTOP_DELIVER_SETTLEMENT_MARGIN_SECONDS') * 1000)
})
it('keeps the client deadline strictly greater than the backend ceiling', () => {
- const margin = tsConstant('RELAY_DELIVER_SETTLEMENT_MARGIN_MS')
-
// Strictly greater, not equal: a backend that answers at its own limit
// still has to serialize and transport that answer.
- expect(margin, 'settlement margin must be positive').toBeGreaterThan(0)
-
- // The call site must pass the composed budget, not a bare literal.
- const drain = relaySource.slice(
- relaySource.indexOf('async function drainRelayOutboxes'),
- relaySource.indexOf('export function startBotRelay')
- )
-
- expect(drain).toMatch(/'bot_relay\.deliver'[\s\S]{0,400}RELAY_DELIVER_TIMEOUT_MS/)
- expect(drain).not.toMatch(/'bot_relay\.deliver'[\s\S]{0,400}\d{6,}/)
+ expect(RELAY_DELIVER_SETTLEMENT_MARGIN_MS, 'settlement margin must be positive').toBeGreaterThan(0)
+ expect(RELAY_DELIVER_TIMEOUT_MS).toBeGreaterThan(RELAY_DELIVER_BACKEND_CEILING_MS)
})
})
diff --git a/apps/desktop/src/plugins/hermes-bots/relay.test.ts b/apps/desktop/src/plugins/hermes-bots/relay.test.ts
index 009f7f8727a..503be2cc6ec 100644
--- a/apps/desktop/src/plugins/hermes-bots/relay.test.ts
+++ b/apps/desktop/src/plugins/hermes-bots/relay.test.ts
@@ -24,6 +24,7 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+import { RELAY_DELIVER_TIMEOUT_MS } from './relay-budget'
import type { ProfileRoute } from './types'
const { clearBotAttentionMock, hostMock, noteBotAttentionMock, UnboundedCache } = vi.hoisted(() => ({
@@ -645,6 +646,13 @@ describe('the drain loop wires drain → deliver → reply', () => {
connectionId: 'b',
params: { message: 'status?', profile: 'ops' }
})
+
+ // #93911: the deliver deadline must outlive the backend's own turn bound.
+ const deliverCall = (hostMock.requestProfile as ReturnType<typeof vi.fn>).mock.calls.find(
+ ([, method]) => method === 'bot_relay.deliver'
+ )
+
+ expect(deliverCall?.[3]).toBe(RELAY_DELIVER_TIMEOUT_MS)
expect(calls.find(call => call.method === 'bot_relay.reply')).toMatchObject({
connectionId: 'a',
params: { id: 'env-1', reply: 'all green' }
diff --git a/apps/desktop/src/plugins/hermes-bots/relay.ts b/apps/desktop/src/plugins/hermes-bots/relay.ts
index bce85927edb..5b53a9b464a 100644
--- a/apps/desktop/src/plugins/hermes-bots/relay.ts
+++ b/apps/desktop/src/plugins/hermes-bots/relay.ts
@@ -10,6 +10,7 @@
import { host, LruCache } from '@hermes/plugin-sdk'
import { botHandle, clearBotAttention, noteBotAttention } from './data'
+import { RELAY_DELIVER_TIMEOUT_MS } from './relay-budget'
import { ID } from './shared'
import type { ProfileRoute, RosterRow } from './types'
@@ -36,35 +37,6 @@ const RELAY_ROSTER_INTERVAL_MS = 60_000
// poll WAS the delivery path, which (before route retention) also meant a
// fresh WebSocket dial + teardown per registered connection every 4s.
const RELAY_DRAIN_INTERVAL_MS = 30_000
-// #93911: a delivered turn runs on the target gateway, so the client must
-// outlive the backend's own bound. Without this the call fell to the pool's
-// generic 30s deadline and every long turn (Computer Use, deep research) came
-// back as an unclassified failure.
-//
-// The backend's MAXIMUM WORK budget is spelled out below. The client deadline
-// must be strictly GREATER than it: after those bounded waits the handler still
-// has to classify the failure, build and run the retry, classify/serialize the
-// terminal result, unwind the temp-file and lock scopes, and get the JSON-RPC
-// response back through the event loop. A call that consumes nearly all of the
-// work budget would otherwise lose the race to this timer by milliseconds and
-// reproduce #93911 at the upper boundary — the backend knowing a typed reason
-// while Desktop reports its generic timeout first.
-//
-// These three are mirrors of backend values, so a change there must not
-// silently invalidate this constant: relay-deliver-budget.test.ts reads
-// hermes_cli/config_defaults.py and tools/bot_relay.py and fails if the
-// mirrors drift or the margin stops being positive.
-const RELAY_TURN_LOCK_WAIT_MS = 120_000 // bot_mode.turn_wait_seconds default
-const RELAY_TURN_ATTEMPT_MS = 600_000 // tools/bot_relay.py TURN_ATTEMPT_TIMEOUT_SECONDS
-const RELAY_TURN_MAX_ATTEMPTS = 2 // first attempt + the policy-gated re-run
-
-const RELAY_DELIVER_BACKEND_CEILING_MS = RELAY_TURN_LOCK_WAIT_MS + RELAY_TURN_ATTEMPT_MS * RELAY_TURN_MAX_ATTEMPTS
-
-// Settlement + transport headroom on top of the ceiling, so a backend that
-// answers at its own limit still wins the race against this timer.
-const RELAY_DELIVER_SETTLEMENT_MARGIN_MS = 180_000
-// tools/bot_relay.py REPLY_WAIT_SECONDS rebuilds this sum and waits past it for the timeout reply below.
-const RELAY_DELIVER_TIMEOUT_MS = RELAY_DELIVER_BACKEND_CEILING_MS + RELAY_DELIVER_SETTLEMENT_MARGIN_MS
// Push path (#93091): the gateway broadcasts `bot_relay.outbox.pending` when
// an envelope lands on disk; a burst of signals inside this window collapses
// to ONE drain. The interval poll above stays as the backstop for older