Skip to content

The agent that grows with you

fix: retain identified desktop bot deliveries until settlement

Tekniume1734e7de616
diff --git a/apps/desktop/src/plugins/hermes-bots/relay.test.ts b/apps/desktop/src/plugins/hermes-bots/relay.test.ts
index 55ba196b35d..11f8c3c4d8c 100644
--- a/apps/desktop/src/plugins/hermes-bots/relay.test.ts
+++ b/apps/desktop/src/plugins/hermes-bots/relay.test.ts
@@ -486,7 +486,7 @@ describe('the drain loop wires drain → deliver → reply', () => {
}
if (call.method === 'bot_relay.deliver') {
- return { reply: 'all green' }
+ return { status: 'settled', delivery_id: envelope.id, admission_id: 'admission-1', reply: 'all green' }
}
return {}
@@ -499,7 +499,7 @@ describe('the drain loop wires drain → deliver → reply', () => {
expect(calls.find(call => call.method === 'bot_relay.deliver')).toMatchObject({
connectionId: 'b',
- params: { message: 'status?', profile: 'ops' }
+ params: { id: envelope.id, message: 'status?', profile: 'ops' }
})
expect(calls.find(call => call.method === 'bot_relay.reply')).toMatchObject({
connectionId: 'a',
@@ -511,6 +511,37 @@ describe('the drain loop wires drain → deliver → reply', () => {
stopBotRelay()
})
+ it('retries the same envelope after pending or lost ACK without replying early', async () => {
+ let attempts = 0
+ let drained = false
+ const calls = respondWith(call => {
+ if (call.method === 'bot_relay.outbox.drain') {
+ if (call.connectionId !== 'a' || drained) return { envelopes: [] }
+ drained = true
+ return { envelopes: [envelope] }
+ }
+ if (call.method === 'bot_relay.deliver') {
+ attempts += 1
+ if (attempts === 1) throw new Error('socket lost after commit')
+ return { status: attempts === 2 ? 'queued' : 'settled', delivery_id: envelope.id,
+ admission_id: 'exact-admission', reply: attempts === 2 ? '' : 'exact reply' }
+ }
+ return {}
+ })
+ const { startBotRelay, stopBotRelay } = await loadRelay()
+ startBotRelay()
+ await pushAndSettle()
+ expect(calls.filter(call => call.method === 'bot_relay.reply')).toHaveLength(0)
+ await pushAndSettle()
+ expect(calls.filter(call => call.method === 'bot_relay.reply')).toHaveLength(0)
+ await pushAndSettle()
+ const deliveries = calls.filter(call => call.method === 'bot_relay.deliver')
+ expect(deliveries).toHaveLength(3)
+ expect(deliveries.every(call => call.params.id === envelope.id)).toBe(true)
+ expect(calls.find(call => call.method === 'bot_relay.reply')?.params).toEqual({id: envelope.id, reply: 'exact reply'})
+ stopBotRelay()
+ })
+
it('still posts a reply when the target connection is gone — the waiter must never dangle', async () => {
const calls = respondWith(call =>
call.method === 'bot_relay.outbox.drain'
diff --git a/apps/desktop/src/plugins/hermes-bots/relay.ts b/apps/desktop/src/plugins/hermes-bots/relay.ts
index df92c8f173a..f793a0fd3f1 100644
--- a/apps/desktop/src/plugins/hermes-bots/relay.ts
+++ b/apps/desktop/src/plugins/hermes-bots/relay.ts
@@ -101,6 +101,9 @@ const relay: RelayLifecycle = {
// leases. Local routes get a no-op release inside the host (idle-reaper
// exemption). stopBotRelay releases everything.
const relayRouteRetentions = new Map<string, () => void>()
+// Claimed envelopes remain pinned to their sender until terminal reply ACK.
+// The sender's durable claimed directory restores these after renderer restart.
+const pendingRelays = new Map<string, Map<string, RelayEnvelope>>()
/** One reachable gateway plus a representative route onto it. The route comes
* from `host.profileRoutes()`, which carries identity only — the optional
@@ -357,7 +360,13 @@ async function drainRelayOutboxes() {
continue
}
+ const senderKey = JSON.stringify(sender.route)
+ const pending = pendingRelays.get(senderKey) || new Map<string, RelayEnvelope>()
+ pendingRelays.set(senderKey, pending)
for (const envelope of envelopes) {
+ if (envelope.id && !pending.has(envelope.id)) pending.set(envelope.id, structuredClone(envelope))
+ }
+ for (const envelope of pending.values()) {
if (relay.disposed) {
return
}
@@ -371,6 +380,7 @@ async function drainRelayOutboxes() {
id: envelopeId,
...payload
})
+ pending.delete(envelopeId)
} catch {
// Sender gateway unreachable — its waiter times out with guidance.
}
@@ -393,16 +403,30 @@ async function drainRelayOutboxes() {
const attentionKey = `${target.id}::${String(envelope?.target_profile || '')}`
try {
- const res = await host.requestProfile<{ reply?: string }>(
+ const res = await host.requestProfile<{ status?: string; delivery_id?: string; admission_id?: string; reply?: string; error?: string; reason?: string }>(
target.route,
'bot_relay.deliver',
{
+ id: envelopeId,
profile: String(envelope?.target_profile || ''),
message: String(envelope?.message || '')
},
RELAY_DELIVER_TIMEOUT_MS
)
+ if (res.delivery_id !== envelopeId || !res.admission_id) {
+ noteBotAttention(attentionKey, 'Delivery identity unavailable; retained for recovery')
+ continue
+ }
+ if (res.status !== 'settled' && res.status !== 'failed') {
+ if (res.status === 'ambiguous') noteBotAttention(attentionKey, 'unknown_execution')
+ continue
+ }
+ if (res.status === 'failed') {
+ noteBotAttention(attentionKey, res.reason || res.error || 'delivery failed')
+ await postReply({ error: res.error || res.reply || 'delivery failed', reason: res.reason })
+ continue
+ }
clearBotAttention(attentionKey)
await postReply({
reply: String(res?.reply || '')
@@ -415,6 +439,8 @@ async function drainRelayOutboxes() {
// classified codes beat free-text re-parsing.
const reason = String(error?.data?.reason || '').trim()
noteBotAttention(attentionKey, reason || error?.message || error)
+ // A transport exception can follow a committed admission; never settle it as failure.
+ if (!reason || reason === 'runtime_unavailable') continue
await postReply({
error: String(error?.message || error || 'delivery failed'),
...(reason