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): a failed engine-snapshot report must not count as delivered (#286)

OKNOTOKceee8728a901
diff --git a/server/src/__tests__/agents-computer-engine-inventory.test.ts b/server/src/__tests__/agents-computer-engine-inventory.test.ts
index 4287f77..249a7d0 100644
--- a/server/src/__tests__/agents-computer-engine-inventory.test.ts
+++ b/server/src/__tests__/agents-computer-engine-inventory.test.ts
@@ -7,6 +7,7 @@ import assert from 'node:assert/strict'
import {
EngineInventoryStabilizer,
replaceEngineInventory,
+ reportEngineSnapshot,
resolveAvailableEngine,
shouldReportEngineSnapshot,
type EngineInventory,
@@ -42,6 +43,41 @@ test('a requested refresh reports an unchanged engine snapshot', () => {
assert.equal(shouldReportEngineSnapshot(snapshot, snapshot, true), true)
})
+test('a failed snapshot report leaves the watermark where it was, so the next scan retries', async () => {
+ const before = JSON.stringify([{ id: 'codex', version: '1.2.3' }])
+ // The operator installed the dependency `claude` was blocked on, so the
+ // rescan computes a new fingerprint — and the POST 502s mid-deploy.
+ const after = JSON.stringify([{ id: 'claude', version: '2.0.0' }, { id: 'codex', version: '1.2.3' }])
+ let posts = 0
+ const failing = async (): Promise<never> => {
+ posts += 1
+ throw new Error('POST /api/computers/me/engines → HTTP 502')
+ }
+
+ assert.equal(await reportEngineSnapshot(after, before, false, failing), before)
+ assert.equal(posts, 1)
+
+ // Five minutes later the same scan produces the same fingerprint. Committing
+ // the watermark before the POST would make this a no-op and freeze the card
+ // on "missing dependency" for the daemon's lifetime.
+ const settled: string[] = []
+ const ok = async (): Promise<void> => { settled.push(after) }
+ assert.equal(await reportEngineSnapshot(after, before, false, ok), after)
+ assert.deepEqual(settled, [after])
+})
+
+test('a delivered snapshot is not reported again, and a forced rescan is', async () => {
+ const snapshot = JSON.stringify([{ id: 'codex', version: '1.2.3' }])
+ let posts = 0
+ const ok = async (): Promise<void> => { posts += 1 }
+
+ assert.equal(await reportEngineSnapshot(snapshot, snapshot, false, ok), snapshot)
+ assert.equal(posts, 0, 'an unchanged snapshot must not hit the network')
+
+ assert.equal(await reportEngineSnapshot(snapshot, snapshot, true, ok), snapshot)
+ assert.equal(posts, 1, 'Rescan has to report even an unchanged snapshot to clear the request')
+})
+
test('a transient version failure retains a previously runnable engine', () => {
const stabilizer = new EngineInventoryStabilizer()
const next = stabilizer.stabilize(['claude', 'codex'], ['claude', 'codex'], {
diff --git a/server/src/agents/computer/daemon.ts b/server/src/agents/computer/daemon.ts
index dc14cbc..2aea1fa 100644
--- a/server/src/agents/computer/daemon.ts
+++ b/server/src/agents/computer/daemon.ts
@@ -999,6 +999,40 @@ export function shouldReportEngineSnapshot(fingerprint: string, previous: string
return force || fingerprint !== previous
}
+/**
+ * Report the engine snapshot and return the fingerprint the daemon should
+ * remember — which is the OLD one whenever the report did not land.
+ *
+ * The predicate above is `force || fingerprint !== previous`, so the remembered
+ * fingerprint is a delivery watermark, not a "what did I last compute" note.
+ * Advancing it before the POST resolves makes a failure indistinguishable from
+ * a success: every later rescan re-derives the same fingerprint, skips the
+ * report, and the machine's card stays frozen at whatever the server last saw.
+ * An operator who installs the missing dependency an engine was blocked on
+ * would watch the card keep saying it is missing until they either hit Rescan
+ * (the only caller that passes `force`) or restart the daemon.
+ *
+ * Separate from the scan loop so this rule is testable without a daemon.
+ */
+export async function reportEngineSnapshot(
+ fingerprint: string,
+ previous: string,
+ force: boolean,
+ post: () => Promise<unknown>,
+): Promise<string> {
+ if (!shouldReportEngineSnapshot(fingerprint, previous, force)) return previous
+ try {
+ await post()
+ return fingerprint
+ } catch (err) {
+ console.warn(
+ '[computer] engine snapshot report failed; retrying on the next scan:',
+ err instanceof Error ? err.message : err,
+ )
+ return previous
+ }
+}
+
// ─── config ─────────────────────────────────────────────────────────────
async function loadConfig(): Promise<DaemonConfig | null> {
@@ -3474,20 +3508,25 @@ async function doRun(serverOverride?: string): Promise<void> {
const advertisedSnapshot = snapshot.map((entry) => entry.id === 'claude'
? { ...entry, providerProfiles: allowUnsandboxedByoa() ? [] : profiles.map(providerProfileMetadata) } : entry)
const fingerprint = JSON.stringify(advertisedSnapshot)
- if (shouldReportEngineSnapshot(fingerprint, lastEngineSnapshot, forceReport)) {
- lastEngineSnapshot = fingerprint
- await api(cfg.serverUrl, '/api/computers/me/engines', {
+ lastEngineSnapshot = await reportEngineSnapshot(
+ fingerprint,
+ lastEngineSnapshot,
+ forceReport,
+ () => api(cfg.serverUrl, '/api/computers/me/engines', {
method: 'POST',
headers: { Authorization: `Bearer ${cfg.deviceToken}` },
body: JSON.stringify({ engines: next, detected: advertisedSnapshot, blocked: blockedIds }),
- }).catch((err) => {
- console.warn('[computer] engine snapshot report failed', err instanceof Error ? err.message : err)
- })
- }
+ }),
+ )
// This is the same live inventory sync() uses to choose an agent's
// adapter. Updating only a heartbeat cache would advertise a newly
// installed engine while silently running that agent on the old default.
- } catch { /* transient — the next tick retries */ }
+ } catch (err) {
+ // Transient — the next tick retries, and that is now true of the report
+ // above too. Say so anyway: a scan failing every five minutes for a
+ // non-transient reason used to leave no trace anywhere.
+ console.warn('[computer] engine scan failed:', err instanceof Error ? err.message : err)
+ }
}
// Timer/startup/requested scans can land together. Share an in-flight scan;