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.

Swarm reads hash a finished start's agent only when they name one (messages route ~3x less CPU) (#2374)

0xken15cdf21a6179ce
diff --git a/backend/src/superTabs/cachedReads.test.ts b/backend/src/superTabs/cachedReads.test.ts
new file mode 100644
index 000000000..883e61153
--- /dev/null
+++ b/backend/src/superTabs/cachedReads.test.ts
@@ -0,0 +1,30 @@
+import { describe, expect, it } from 'vitest'
+import { stableId, type AgentStart } from './model.js'
+import type { Part } from './parts/index.js'
+import { namedStarts } from './cachedReads.js'
+
+/** A start, finished once it has a harness. `untouchable`: reading its request throws, so a test sees the read hash its
+ * agent (stableId, a sha256 of the request's machine and the harness). */
+const start = (key: string, done: boolean, untouchable = false): Part<AgentStart> => {
+ const data = { id: key, harnessId: done ? `${key}-harness` : null } as Record<string, unknown>
+ Object.defineProperty(data, 'request', { enumerable: true, get: () => {
+ if (untouchable) throw new Error(`read the request of ${key}`)
+ return { machineId: 'host', conversationId: 'c' }
+ } })
+ return { kind: 'start', key, version: 1, changedAt: 1, ordinal: 1, data: data as unknown as AgentStart }
+}
+
+describe('the starts a read keeps (cachedReads.ts namedStarts)', () => {
+ it('keeps the unfinished starts without hashing any finished one when no agent is named (prod 10-30: 873 a messages read)', () => {
+ const all = [...Array.from({ length: 1000 }, (_, i) => start(`old-${i}`, true, true)), start('queued', false)]
+ expect(namedStarts(all, {}).map(s => s.key)).toEqual(['queued'])
+ expect(namedStarts(all, { startIds: ['old-3'] }).map(s => s.key)).toEqual(['old-3', 'queued'])
+ })
+
+ it('keeps the finished starts named by id or by the agent they started, and every one for a first load', () => {
+ const all = [...Array.from({ length: 10 }, (_, i) => start(`old-${i}`, true)), start('queued', false)]
+ const agent = stableId('host', 'old-7-harness')
+ expect(namedStarts(all, { startIds: ['old-3'], startAgentIds: [agent] }).map(s => s.key)).toEqual(['old-3', 'old-7', 'queued'])
+ expect(namedStarts(all, { includeRecentStarts: true })).toBe(all)
+ })
+})
diff --git a/backend/src/superTabs/cachedReads.ts b/backend/src/superTabs/cachedReads.ts
index 3fc81e1ef..6d46cfc6c 100644
--- a/backend/src/superTabs/cachedReads.ts
+++ b/backend/src/superTabs/cachedReads.ts
@@ -52,11 +52,18 @@ export async function readParts(db: Db, view: CacheView, tabId: string, named: R
for (const id of named.groupIds ?? []) {
if (!held.has(id)) add(await view.get(tabId, `group:${id}`, 'main', () => store.loadGroups(db, tabId, 'key', { key: id })))
}
+ return { ...active, machines, groups: extra.length ? [...active.groups, ...extra] : active.groups, starts: namedStarts(active.starts, named) }
+}
+
+/** The starts loadActive gives for `named`: every unfinished one, and the finished ones named by id or by the agent they
+ * started (or all of them with `includeRecentStarts`). A finished start's agent is a sha256 (stableId), so it is worked
+ * out only when the read names an agent: hashing every finished start was ~40% of a messages read's CPU (local profile of
+ * prod's busiest Swarm, 873 starts, 2026-10-30). */
+export function namedStarts(starts: Part<AgentStart>[], named: ReadNamed): Part<AgentStart>[] {
+ if (named.includeRecentStarts) return starts
const startIds = new Set(named.startIds ?? []), startAgents = new Set(named.startAgentIds ?? [])
const agentOf = (start: AgentStart) => start.harnessId ? stableId(start.request.machineId, start.harnessId) : null
- const starts = named.includeRecentStarts ? active.starts : active.starts.filter(s => !startFinished(s.data!) || startIds.has(s.key)
- || startAgents.has(agentOf(s.data!) ?? ''))
- return { ...active, machines, groups: extra.length ? [...active.groups, ...extra] : active.groups, starts }
+ return starts.filter(s => !startFinished(s.data!) || startIds.has(s.key) || (startAgents.size > 0 && startAgents.has(agentOf(s.data!) ?? '')))
}
/** The parts loadLight gives for `opts`, held whole by what it selects. */