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.

Hotfix: a command reads its Swarm once again (#4213) (#3470)

0xken15cbdcc6f061bb
diff --git a/backend/integration/superTabSwarmBuddy.test.ts b/backend/integration/superTabSwarmBuddy.test.ts
index 39abf0d88..861a8883b 100644
--- a/backend/integration/superTabSwarmBuddy.test.ts
+++ b/backend/integration/superTabSwarmBuddy.test.ts
@@ -15,6 +15,7 @@ import { poolRows } from './hostRows.test-support.js'
import { beforeFirstWrite } from './interleave.test-support.js'
import { readInbox } from '../src/superTabs/read/inbox.js'
import { SwarmReads } from '../src/superTabs/read/service.js'
+import { profiled } from './plans.test-support.js'
// One Buddy per person per Swarm (docs/specs/2026-10-31-one-buddy-per-swarm.md; Dee, 10-31): one chat at Swarm level, a main
// Buddy that reads every project its person reads and writes nowhere but that chat, and project sessions, the only Buddies that
@@ -861,6 +862,25 @@ describe('several Buddies through the store (#3818)', () => {
expect(chats.find(c => c.id === first.chat)!.buddyChats).toBeUndefined()
})
+ // Ken's 06:20 VN prod scan on v1.4.26 (task #4213): loading the listed chats loaded the whole Swarm again, so every command
+ // in a Swarm with a quiet second Buddy read its machines, agents, members and progress twice.
+ it('loads a quiet listed chat by itself: any command reads the Swarm\'s machines, agents, members and progress once', async () => {
+ const f = await swarm()
+ await buddy(f)
+ const research = await named(f, 'Research')
+ await quiet(f, research.chat)
+ const reads = await profiled(db, () => send(f, 'lead', 'Hello', {}, owner))
+ // Every live row of the Swarm, by tab: not one row's version check by its id.
+ const wholeSwarm = (collection: string) => reads.filter(e => e.ns.endsWith(`.${collection}`) && e.op !== 'update').filter(e => {
+ const filter = JSON.stringify(e.command?.pipeline ?? e.command?.filter ?? {})
+ return filter.includes('"$tabId"') && filter.includes('"$deleted"') && !filter.includes('"$_id"')
+ }).length
+ expect(['SwarmMachine', 'SwarmAgent', 'SwarmMember', 'SwarmProgress'].map(c => [c, wholeSwarm(c)]))
+ .toEqual([['SwarmMachine', 1], ['SwarmAgent', 1], ['SwarmMember', 1], ['SwarmProgress', 1]])
+ await expect(f.execute({ type: 'buddy.create', machineId: 'peer-mac', engine: 'claude', name: 'Research' } as Command, peer))
+ .rejects.toMatchObject({ code: 'BUDDY_NAME_TAKEN' })
+ })
+
it('keeps a quiet Buddy\'s name taken, and counts it against the cap', async () => {
const f = await swarm()
await buddy(f)
diff --git a/backend/src/superTabs/parts/index.ts b/backend/src/superTabs/parts/index.ts
index 62e403d20..bac7f463a 100644
--- a/backend/src/superTabs/parts/index.ts
+++ b/backend/src/superTabs/parts/index.ts
@@ -7,7 +7,7 @@ export * from './types.js'
export { clock, createClock } from './clock.js'
export { knownKind, reassemble, split, toLegacyState } from './shape.js'
-export { DORMANT_PAGE, insertParts, lastChangedAt, loadAccess, loadActive, loadAll, loadChanged, loadDelta, loadDormant, loadFinishedStarts, loadGroups, loadGroupUnless, loadMachineParts, loadMachines, loadMachineAccounts, loadObserved, loadReadBase, loadStartsOfAgents } from './load.js'
+export { DORMANT_PAGE, insertParts, lastChangedAt, loadAccess, loadActive, loadAll, loadChanged, loadDelta, loadDormant, loadFinishedStarts, loadGroups, loadGroupUnless, loadMachineParts, loadMachines, loadMachineAccounts, loadNamed, loadObserved, loadReadBase, loadStartsOfAgents } from './load.js'
export type { SwarmDelta } from './load.js'
export { loadLight, loadLightGroups } from './light.js'
export { guardedWrite, PartConflict, type PartWrite } from './write.js'
diff --git a/backend/src/superTabs/parts/load.ts b/backend/src/superTabs/parts/load.ts
index e84d25682..f0a5448bb 100644
--- a/backend/src/superTabs/parts/load.ts
+++ b/backend/src/superTabs/parts/load.ts
@@ -69,6 +69,33 @@ export async function loadActive(tx: Transaction, tabId: string,
return parts
}
+/**
+ * `loaded` with the parts `loadActive` would add for these names and `loaded` lacks: the groups named by key, the named
+ * agents' and projects' groups, and the starts named by key or agent. One indexed read per kind of name, and nothing else:
+ * reading the whole Swarm again for one quiet conversation doubled what every command read (task #4213). Inside one
+ * transaction every read sees one snapshot, so the result is what `loadActive` with the same names returns.
+ */
+export async function loadNamed(tx: Transaction, loaded: SwarmParts,
+ named: { groupIds?: string[]; agentIds?: string[]; projectIds?: string[]; startIds?: string[]; startAgentIds?: string[] }): Promise<SwarmParts> {
+ const { tabId } = loaded
+ const groups = new Set(loaded.groups.map(g => g.key)), starts = new Set(loaded.starts.map(s => s.key))
+ const groupIds = [...new Set(named.groupIds ?? [])].filter(id => !groups.has(id))
+ const startIds = [...new Set(named.startIds ?? [])].filter(id => !starts.has(id))
+ const projectIds = [...named.projectIds ?? [], ...named.projectIds?.includes('lead') ? [null] : []]
+ const reads: GroupReads = [...groupIds.length ? [['key', { key: { $in: groupIds } }] as const] : [],
+ ...named.agentIds?.length ? [['agents', { agentIds: { $in: named.agentIds } }] as const] : [],
+ ...projectIds.length ? [['project', { projectId: { $in: projectIds } }] as const] : []]
+ const [groupRows, byKey, byAgent] = await Promise.all([
+ reads.length ? liveGroups(tx, tabId, reads) : Promise.resolve([]),
+ startIds.length ? readBy(tx, 'SwarmStart', 'id', [{ $match: { _id: { $in: startIds.map(id => partId(tabId, id)) }, tabId, deleted: false } }]) : Promise.resolve([]),
+ loadStartsOfAgents(tx, tabId, named.startAgentIds ?? []),
+ ])
+ const addedGroups = groupRows.filter(row => !groups.has(row.key)).map(row => toPart<GroupData>('group', row))
+ const addedStarts = [...new Map([...byKey.map(row => toPart<AgentStart>('start', rawPartRow(row))), ...byAgent]
+ .filter(s => !starts.has(s.key)).map(s => [s.key, s])).values()]
+ return { ...loaded, groups: [...loaded.groups, ...addedGroups], starts: [...loaded.starts, ...addedStarts] }
+}
+
/**
* What a machine's report works on (store.ts observeMachine, #876): `loadActive`'s parts with only the conversations its
* checks read, whole, since it may write them: running work, the Lead conversations, the unfinished starts' conversations,
diff --git a/backend/src/superTabs/store.ts b/backend/src/superTabs/store.ts
index d229ce2b6..7d327e8f8 100644
--- a/backend/src/superTabs/store.ts
+++ b/backend/src/superTabs/store.ts
@@ -86,7 +86,7 @@ import { endedReply, taskLineChats } from './taskLine.js'
import { owedAsk } from './timeChecks.js'
import { ledgerGroupIds, settleReleases, type ReleaseFacts, type ReleaseRules, type SettledReleases } from './releases.js'
import { groupsOf, archivable, archiveLine, archiveWaitsFor, idleNotices, reviewClosable, reviewCloseLine, reviewPullRequests, type SettledPullRequest } from './tidy.js'
-import { clock, insertParts, loadActive, loadGroups, loadLightGroups, loadMachineParts, loadObserved, reassemble, split, type GroupData, type HeadData, type MachineData, type Part, type SwarmParts } from './parts/index.js'
+import { clock, insertParts, loadActive, loadGroups, loadLightGroups, loadMachineParts, loadNamed, loadObserved, reassemble, split, type GroupData, type HeadData, type MachineData, type Part, type SwarmParts } from './parts/index.js'
import { groupsBy, listingDrops, quietSince, readBy, withoutHints } from './parts/indexed.js'
import { autoJoinSwarms, machineSwarms, memberSwarms } from './swarmRows.js'
import type { MachineRename } from './machineRenames.js'
@@ -1035,10 +1035,9 @@ export class SuperTabStore {
if (!named.allGroups && has(named.groupIds, id => state.conversations.some(c => c.id === id)) && has(named.startIds, id => !!state.starts?.some(s => s.id === id))
&& !named.agentIds?.length && !named.projectIds?.length && !named.startAgentIds?.length) return loaded
recordReload()
- // An additional named read must retain quiet groups already needed by this transaction, such as a proposal or merge source.
- // Awake groups load automatically; only quiet keys need carrying through the next indexed read.
- const groupIds = [...new Set([...named.groupIds ?? [], ...loaded.parts.groups.filter(g => g.dormant).map(g => g.key)])]
- return loadRow(tx, loaded.row, { ...named, groupIds })
+ // Every live group, dormant ones too, is the rare whole read; any other name adds only what it names to this load.
+ if (named.allGroups) return loadRow(tx, loaded.row, { ...named, groupIds: [...new Set([...named.groupIds ?? [], ...loaded.parts.groups.filter(g => g.dormant).map(g => g.key)])] })
+ return loadedFrom(loaded.row, await loadNamed(tx, loaded.parts, named))
}
/** A Swarm with one start loaded even when finished, and the start's group even when dormant. */
diff --git a/docs/plans/2026-11-07-command-reload-cost-plan.md b/docs/plans/2026-11-07-command-reload-cost-plan.md
new file mode 100644
index 000000000..74d9f3302
--- /dev/null
+++ b/docs/plans/2026-11-07-command-reload-cost-plan.md
@@ -0,0 +1,43 @@
+# Command read cost back to v1.4.25 (task #4213) Implementation Plan
+
+> Written by the writing-plans way of working; the Engineer implements it task by task, test first.
+
+**Goal:** A command reads each of its Swarm's machines, agents, members and progress once, as on v1.4.25, however many quiet
+conversations it needs to add to its first load.
+
+**Architecture:** `SuperTabStore.ensure` (store.ts) loads more named conversations or starts when the first load lacks them. It
+did that by loading the whole Swarm again: head, members, agents, machines, progress, every awake conversation and the named
+ones. v1.4.26 (task #3818, several Buddies) calls it on every command for the chats listed on a person's main Buddy chat, and
+the proposal cards call it twice per pass. Once a person has a second Buddy and it goes quiet, every command in that Swarm
+read the whole Swarm twice: Ken's 06:20 VN prod scan shows SwarmMachine, SwarmAgent and SwarmProgress whole-Swarm reads per
+command at about 2.2x. `ensure` now reads only what is missing (the named conversations by key, the named agents' and
+projects' conversations, the named starts) and adds them to what is loaded. A transaction reads one snapshot, so the result
+is the one a whole reload gave.
+
+**Tech Stack:** TypeScript, Prisma on MongoDB, vitest integration tests on a loopback replica set.
+
+**Spec:** Task #4213's brief.
+
+## Global Constraints
+
+- Backend only; no schema or index change; no production write beyond the normal deploy.
+- Every new read uses an index already listed in `parts/indexed.ts` (key, agents, project; starts by key or agent).
+
+## Review Focus
+
+- The merged parts equal what `loadActive` with the same names returns inside the same transaction.
+- `allGroups` keeps the whole reload (a rare read across every live conversation).
+
+## Task 1: a command with a quiet listed Buddy chat reads the Swarm once
+
+**Files:** `backend/integration/superTabSwarmBuddy.test.ts`, `backend/src/superTabs/store.ts`,
+`backend/src/superTabs/parts/load.ts`
+
+**Interfaces:** `loadNamed(tx, tabId, named, loaded)` in `parts/load.ts`: the groups and starts `named` names that `loaded` lacks.
+
+- [ ] Test: a person with two Buddies, the second quiet; a post in the project chat reads SwarmMachine, SwarmAgent,
+ SwarmMember and SwarmProgress once each, and the quiet chat is still loaded (its name stays taken).
+- [ ] Watch it fail: two reads each.
+- [ ] `ensure` reads only the missing parts and merges them.
+- [ ] Watch it pass, with the store's integration tests for Buddies, command loads and hot reads.
+- [ ] Measure before and after with `integration/bench/v3ReadCost.ts` on a Swarm with a quiet second Buddy.