Skip to content

Where agent teams gather. Cross-platform team chat where AI agents are first-class teammates — with cloud or bring-your-own (Claude Code / Codex) brains.

TypeScript

fix(byoa): do not advance inbox drain watermark on failed fetch (#304)

Leo Camus622498370cf7
diff --git a/server/src/__tests__/agents-computer-inbox-drain-watermark.test.ts b/server/src/__tests__/agents-computer-inbox-drain-watermark.test.ts
new file mode 100644
index 0000000..81d5934
--- /dev/null
+++ b/server/src/__tests__/agents-computer-inbox-drain-watermark.test.ts
@@ -0,0 +1,78 @@
+import assert from 'node:assert/strict'
+import { test } from 'node:test'
+import { AgentRunner, fallbackPollDue } from '../agents/computer/daemon.js'
+
+const NOW = 1_000_000
+
+type Runner = {
+ lastInboxDrainAt: number
+ token: string
+ busy: boolean
+ pendingBackgroundBrief: { title: string; body: string } | null
+ snapshotUnread(token: string): Promise<unknown>
+ runTurn(reason: string): Promise<void>
+ ensureToken(): Promise<string>
+ postStatus(token: string, status: 'avail' | 'thinking'): Promise<void>
+ maybeAgendaTurn(token: string): Promise<void>
+}
+
+function createRunner(): Runner {
+ return new AgentRunner(
+ { serverUrl: 'https://server.example.test', computerId: 'fixture', deviceToken: 'fixture' },
+ { id: 'fixture', name: 'Fixture', role: null, systemPrompt: null, engine: 'codex', model: null, fastModel: null },
+ 'codex',
+ ) as unknown as Runner
+}
+
+const failures: Array<[string, () => Promise<Response>]> = [
+ ['HTTP 503', async () => new Response('', { status: 503 })],
+ ['network error', async () => { throw new TypeError('fetch failed') }],
+ ['timeout', async () => { throw new DOMException('request timed out', 'TimeoutError') }],
+ ['invalid JSON', async () => new Response('{')],
+]
+
+for (const [name, fail] of failures) {
+ test(`${name} retries after a recent success and restores the slow interval on recovery`, async (t) => {
+ let now = NOW
+ let respond = fail
+ t.mock.method(Date, 'now', () => now)
+ t.mock.method(globalThis, 'fetch', () => respond())
+ const runner = createRunner()
+ runner.lastInboxDrainAt = now - 10_000
+ const due = (at: number) => fallbackPollDue({
+ now: at, streamLastSeenAt: at - 1_000, lastInboxDrainAt: runner.lastInboxDrainAt,
+ })
+
+ await assert.rejects(runner.snapshotUnread('fixture-token'), /inbox fetch failed/)
+ assert.equal(runner.lastInboxDrainAt, 0)
+ assert.equal(due(now + 20_000), true)
+
+ now += 20_000
+ respond = async () => Response.json({ rows: [] })
+ await runner.snapshotUnread('fixture-token')
+ assert.equal(runner.lastInboxDrainAt, now)
+ assert.equal(due(now + 20_000), false)
+ })
+}
+
+test('a failed inbox ends the turn and preserves the manual wake for retry', async (t) => {
+ t.mock.method(Date, 'now', () => NOW)
+ t.mock.method(globalThis, 'fetch', failures[0][1])
+ const runner = createRunner()
+ runner.lastInboxDrainAt = NOW - 10_000
+ runner.token = 'fixture-token'
+ const brief = { title: 'Review assigned card', body: 'Review the assigned change' }
+ runner.pendingBackgroundBrief = brief
+ t.mock.method(runner, 'ensureToken', async () => runner.token)
+ const status = t.mock.method(runner, 'postStatus', async () => { throw new Error('unexpected status update') })
+ const agenda = t.mock.method(runner, 'maybeAgendaTurn', async () => {})
+ const error = t.mock.method(console, 'error', () => {})
+
+ await runner.runTurn('sse-wake')
+ assert.equal(runner.busy, false)
+ assert.equal(runner.lastInboxDrainAt, 0)
+ assert.deepEqual(runner.pendingBackgroundBrief, brief)
+ assert.equal(status.mock.callCount(), 0)
+ assert.equal(agenda.mock.callCount(), 0)
+ assert.match(String(error.mock.calls[0]?.arguments[1]), /inbox fetch failed/)
+})
diff --git a/server/src/agents/computer/daemon.ts b/server/src/agents/computer/daemon.ts
index 3047986..ae88e25 100644
--- a/server/src/agents/computer/daemon.ts
+++ b/server/src/agents/computer/daemon.ts
@@ -1823,7 +1823,7 @@ export class AgentRunner {
/** Aborts the current wake-stream fetch, so a stream that has gone silent can
* be torn down and reconnected instead of waiting for TCP to notice. */
private streamAbort: AbortController | null = null
- /** When snapshotUnread last hit /inbox — the anchor for the slow double-check. */
+ /** Last successful inbox check; 0 means a retry is due. */
private lastInboxDrainAt = 0
/** The last status POST that succeeded, and when — see AVAIL_REASSERT_MS. */
private lastPostedStatus: string | null = null
@@ -2543,8 +2543,9 @@ export class AgentRunner {
}
private async snapshotUnread(token: string): Promise<{ seen: Map<string, string>; digest: string; hasReal: boolean; projectIds: string[] }> {
- this.lastInboxDrainAt = Date.now()
const inbox = await runtimeGet<RuntimeInboxResponse>(this.cfg.serverUrl, '/inbox', token)
+ this.lastInboxDrainAt = inbox ? Date.now() : 0
+ if (!inbox) throw new Error('inbox fetch failed')
const seen = new Map<string, string>()
// Unread grouped BY CONVERSATION (first-seen order), each with the header
// (title + topic) the cloud agent's context also carries — so a BYOA agent