Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

fix(backend): reuse cached timestamps on unchanged host polls (#2007)

0xken15b766b4166132
diff --git a/backend/integration/superTabHotReads.test.ts b/backend/integration/superTabHotReads.test.ts
index 9148277aa..db332f187 100644
--- a/backend/integration/superTabHotReads.test.ts
+++ b/backend/integration/superTabHotReads.test.ts
@@ -11,6 +11,8 @@ import { SuperTabStore } from '../src/superTabs/store.js'
import { migrateSwarm } from '../src/superTabs/migration/migrate.js'
import { waitingIn } from '../src/superTabs/decisions.js'
import { HostReads } from '../src/superTabs/read/host.js'
+import { cursorFor } from '../src/superTabs/read/cursor.js'
+import { lastChangedAt } from '../src/superTabs/parts/index.js'
import type { SuperTabState } from '../src/superTabs/model.js'
import { writeLegacySwarm } from '../scripts/swarm-parts/legacy.js'
import { NOW, owner as ownerActor, swarm } from '../src/superTabs/read/fixture.test-support.js'
@@ -106,6 +108,37 @@ function reads(entries: ProfileEntry[], collection: string, fields: string[], no
}
const plan = (rows: ProfileEntry[]) => rows.map(r => ({ plan: r.planSummary, docsExamined: r.docsExamined, keysExamined: r.keysExamined, returned: r.nreturned }))
+it('reuses the host timestamp at the same stamp, including a repeated wake with a held etag', async () => {
+ const s = await scene()
+ const hosts = new HostReads(store, { now: () => Date.now() })
+ const first = await hosts.changes(owner, s.machineId, { full: true })
+ const held = Object.fromEntries(first.tabs.map(tab => [tab.tabId, tab.etag]))
+ // An older cursor wakes this Swarm again without another write or stamp bump.
+ const entries = await profiled(async () => {
+ const second = await hosts.changes(owner, s.machineId, { cursor: cursorFor(MIGRATED - 1, 0), held })
+ expect(second.tabs).toEqual(first.tabs.map(tab => ({ tabId: tab.tabId, etag: tab.etag,
+ lastChangedAt: tab.lastChangedAt, unchanged: true })))
+ })
+ const scans = entries.filter(e => /^Swarm(Head|Member|Agent|Group|Machine|Start|Progress)$/.test(e.command?.aggregate as string))
+ .filter(e => {
+ const match = (e.command?.pipeline as Array<{ $match?: { tabId?: { $in?: unknown } } }>)?.[0]?.$match
+ // Timestamp scans match only tabId; the host's indexed wake query also bounds changedAt and hostMachineIds.
+ return match?.tabId?.$in && Object.keys(match).length === 1
+ })
+ expect(scans.map(e => e.command?.aggregate)).toEqual([])
+
+ // A committed change invalidates the timestamp along with the host inputs; a cold/direct read agrees.
+ clock = Date.now()
+ await store.execute(owner, s.tabId, { operationId: randomUUID(), command: {
+ type: 'message.send', conversationId: s.web, text: 'A newer change', replyTo: null, recipientIds: [] } })
+ const changed = await hosts.changes(owner, s.machineId, { full: true })
+ const newest = (await lastChangedAt(db, [s.tabId])).get(s.tabId)
+ expect(newest).toBeGreaterThan(first.tabs[0]!.lastChangedAt!)
+ expect(changed.tabs[0]!.lastChangedAt).toBe(newest)
+ const direct = new HostReads(new SuperTabStore(db, () => clock, null, { readCache: null }))
+ expect((await direct.changes(owner, s.machineId, { full: true })).tabs).toEqual(changed.tabs)
+})
+
describe('hot reads read only what they return (roadmap 2.7)', () => {
let entries: ProfileEntry[]
let s: Awaited<ReturnType<typeof scene>>
diff --git a/backend/src/superTabs/read/host.ts b/backend/src/superTabs/read/host.ts
index e36683057..aaf97be6d 100644
--- a/backend/src/superTabs/read/host.ts
+++ b/backend/src/superTabs/read/host.ts
@@ -12,7 +12,7 @@ import { machineBillingAllowsDataPlane } from '../../lib/billingState.js'
import { fail, type AgentStart, type SuperTabState } from '../model.js'
import type { SuperTabStore } from '../store.js'
import type { CacheView } from '../cache.js'
-import { readMachines } from '../cachedReads.js'
+import { readLastChangedAt, readMachines } from '../cachedReads.js'
import * as parts from '../parts/index.js'
import type { MachineData, Part } from '../parts/index.js'
import { groupsBy, retryStaleIndex } from '../parts/indexed.js'
@@ -63,14 +63,13 @@ export class HostReads {
hostMachineId: machineId, sessionKind: 'team', autonomousEnv: user.autonomousEnv, ...machineAlive }, select: { machineId: true } })
const full = 'reload' in from
const woken = full ? null : await this.woken(tx, ids, machineId, from.since)
- // Only for the Swarms answered for: this runs every second on every host, and seven groupBy over all of a Swarm's parts
- // are no use for one that has nothing to say (a production backend at 5 pods, 2026-10-27).
- const newest = await parts.lastChangedAt(tx, woken ? ids.filter(id => woken.has(id)) : ids)
const tabs = []
const cached = await this.store.view(woken ? ids.filter(id => woken.has(id)) : ids)
for (const tabId of ids) {
if (woken && !woken.has(tabId)) continue
- const lastChangedAt = newest.get(tabId) ?? null, held = request.held?.[tabId]
+ // A wake can repeat while the cursor overlaps the same change. Share the timestamp at the read's stamp instead
+ // of scanning every part kind again before the unchanged-etag shortcut.
+ const lastChangedAt = await readLastChangedAt(tx, cached, tabId), held = request.held?.[tabId]
// The etag is the rows' hash and then the signature of what they were built from. A matching signature answers
// 'unchanged' from version columns alone. Never on a full read: grants and billing live outside the parts.
const inputs = await cached.get(tabId, 'host:inputs', 'main', () => this.inputs(tx, tabId))