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.
Design: a small, shared light read for Desktop/web Swarm changes (DB roadmap 2.3)
Date: 2026-10-30. Code read at origin/main (2bbcad4c4). Spec 1 of 8 in the CPU-ranked list of unfinished items from
the database roadmap (.vscode/2026-10-29-database-optimization-roadmap.md, Phase 2 item 3) and the
WebSocket roadmap. Each spec is approved before the next
one is written.
Why
GET /api/super-tabs/:tabId/changes is the read every open Desktop window and web tab makes for each Swarm it shows,
now mostly right after a swarm_changed push (lib/swarmClientPushes.ts, coalesced to one a second). It is the most
expensive unfinished item.
Prod, 2026-10-30, 3 pods, v1.3.6x, 17 minutes of logs:
The route takes about 2.06 s of event-loop time per minute per pod, 20% of all busy time, at about 110 reads per minute per pod.
About 30% of reads (32 a minute per pod) miss the read cache and run a full
loadLight. Those loads are about 9.5 MB a minute per pod, 70% of the bytes the route reads:Shape under the route (per pod) Calls/min Docs/call MB/min SwarmGroupbykey $in(light projection)52 244 6.97 SwarmProgress.findMany33 1 1.85 SwarmAgent.findMany32 11 0.42 SwarmGrouprunning work31 6 0.20 change feed, machines + groups 140 2–5 2.6 Where backend CPU goes (the
cpu profilelog, all routes): Prisma decoding results 33%, garbage collection about 10%,structuredClone9%, the cache'sfreeze2.6%. The Swarm logic itself (reassemble, views,changesFor) is under 3%. CPU follows the bytes read from Mongo and copied, not the number of readers. The way to cut it is to read fewer bytes.
Why each miss is so large. On the busiest Swarm (c51479bd…, openharness: 6 members, 126 live and 1,080 dormant
conversations, 396 commands an hour):
- Conversations of every live start.
loadLightwithrecentStartGroupsloads the conversation of every live start (parts/light.ts:129-132). Finished starts are kept 7 days: 837 live starts, 0 unfinished, in 623 conversations, most of them dormant. #2247 kept this load for one reason only: an agent stored with noprojectIdtakes its project from the first conversation it is in (projects.ts:98-100 agentProjectId). That Swarm has no such agent. All of prod has 6 of 66 live agents, in 6 Swarms, last changed 27–29 Oct. - The progress part. One
SwarmProgressdocument of 81 KB, read on every miss. Achangesreply sends progress only from the change feed (read/changes.ts), never from the loaded base. - One entry per reader.
read/service.ts:120-125passesgroupIds: mentioned(every conversation the person was tagged in) toreadLight, whose cache name includes them (cachedReads.ts:64-65). Every reader has their own entry, so eachmainstamp costs one full load per reader per pod. Reads land on pods at random.
Goals
- A
changesmiss reads only what the reply needs: no conversations of finished starts, no progress part. - One light base per Swarm per
mainstamp per pod, shared by every reader. - The reply of every
changescall is what the full state gives, for every reader (person, agent viaasAgentId, Buddy): unchanged wherever today's light reply already was.
Non-goals
- Keeping the base across writes by patching it with the change feed. With the base this small, a reload per write is cheap; patching adds risk for little.
- The per-person reads in a poll (
superTabMentiondistinct list,waitingMentions,decisionsWaiting,readMarks): small indexed reads, per person by nature. - The change feed's own cost (machine bumps reload it). Measured here, decided later.
structuredCloneof whole states (DB 2.6), thelastChangedAtaggregates (DB 2.4): specs 2 and 5 of this series.- Retention of finished starts (DB roadmap Phase 5).
- Any client change.
Anti-goals
- A reader seeing a conversation, agent, start, read mark or machine record that a first load would not show them,
or missing one it would. The checks (
read/views.ts readerView,read/changes.ts changesFor,readableMarks) keep the inputs they need for every reader. - An agent stored without a
projectIdlanding in a different project in a reply than it does today. - A cache entry whose content depends on who read it.
Design decided
0. The reference answer
(Author's call after the builder check.) Today's light reply is not one fixed answer: what it derives (an agent's
project, who awaits a decision) depends on which conversations happened to be loaded, including that reader's
mentions and every start of the last 7 days. The reference for every reply is therefore the answer the full state
gives, as commands and firstLoad see it: the Swarm's active conversations (loadActive). Where today's light reply
differs from that, the new reply follows the full state, and the equality tests compare against a reply built from
the full state, not against today's light reply.
1. Legacy agents: load only their own active conversations
agentProjectIdkeeps its fallback (lowest-ordinal conversation the agent is in, else'lead').- The light base no longer loads every start's conversation to serve that fallback.
- Prod, 2026-10-30: the 6 live agents stored without a
projectIdare all in Swarms whose head has noprojects. There every conversation has no project, so the answer is always'lead'and nothing needs loading. New agents without aprojectIdare still created only in such Swarms (commands.ts:1476-1477,1744-1745). - So the base adds, through the
agentsindex, the active conversations of the live agents stored without aprojectId, as commands see them, and exactly as a machine report's load already does (parts/load.ts loadObserved,legacy). With no such agent, as on every Swarm but six today, no query runs. - (Author's call.) No backfill and no change to stored data.
2. The light base reads only what a poll needs
loadLight'srecentStartGroupsoption goes, with itsdistinctover every live start. The base holds the Lead conversations, running work, conversations of unfinished starts, invitations' conversations, andundecidedWork, as the'working'selection already does.- The starts a poll adds after the base (
pollStarts: changed starts from the feed, and finished starts a machine record still lists) need their conversations for the visibility checks. Their conversation ids joinunheld(read/service.ts:137), which already loads the conversations of changed starts and moved read marks. No other reader ofloadLightusedrecentStartGroups. - The light base reads no progress parts (
machines: falsealready shows the pattern). The change feed still carries changed progress parts, andchangesForsends them as today. If anything on thechangespath readsparts.progressorstate.projects[].progressfrom the base, the builder moves it to the feed's part or proves it unused; the response equality tests decide.
2b. Who awaits a decision: the base's busy rule matches awaitingWorkers
derived.agents[].workDecisioncomes frommodel.ts awaitingWorkers: an agent with no stored decision awaits one if an active conversation shows its finished, non-lost work, unless it is a Reviewer, leads a conversation, or is the assignee of active work.undecidedWork(parts/light.ts:63-71) counts as busy also every member (agentIds) of running work, so it skips loading the finished work of an agent that is only a member of running work. Today the start conversations happen to cover that case; without them the reply would lose an'awaiting'.- (Author's call after the builder check.)
undecidedWork's busy rule becomes the same asawaitingWorkers' (Lead of a conversation, or assignee of active work; Reviewers never wait), so the base holds every active conversationawaitingWorkersreads. This adds a few active conversations on some Swarms.
3. One shared base, named groups held per group
- The poll calls
readLightwith nogroupIds, so its cache name is the same for every reader. - (Author's call after the builder check.) A person's named conversations are those of their waiting tags
(unresolved, under 30 days: the rows
mentions.ts readablecounts), not every conversation they were ever tagged in. Only the waiting count needs them, andreadablereads any it lacks itself. The all-timedistinctquery atservice.ts:120goes. (Prod: 2–15 tagged conversations per person on the busiest Swarm, so the bulk of today's 244 docs a call is the start conversations of section 2.) - A new read in
cachedReads.ts(working namereadLightGroups(db, view, tabId, ids)) returns the light groups for ids the base lacks (a reader's waiting-tag conversations, andunheld):- one cache entry per id at the
mainstamp (for examplelight-group:<id>), holding the group or "none" for a deleted or unknown id; - one query for the misses of a call, through the
keyindex with the light projection (parts/light.ts loadLightGroups(tx, tabId, 'key', { key: { $in: ids } }), which exists), batched asreadLastChangedAtsbatches its misses.
- one cache entry per id at the
cache.tskeeps an index of entry names per Swarm, sodrop,heldand eviction touch only that Swarm's entries. Todaydropscans every entry of every Swarm on each bump, machine-only bumps included, and per-group entries would multiply the entry count.- The loader the poll gives
loadreturns the shared base, plus the named groups it lacks, plus the poll's own starts (withStarts). Both loads of a poll (service.ts:132and:139) use it. No per-personreadLightname remains.
4. Equal responses
- The composed parts must give, for every reader, the reply the full state gives (section 0). Today's light reply already does in the common case; the tests below pin the cases where it did not.
undecidedWorkmay now hold a few extra active conversations; extra active conversations never change a reply built from the full state, which holds them all.- Group order:
reassemblesorts by ordinal (parts/shape.ts), so composition order does not matter. - Snapshot: named groups may be read outside the base's read transaction, as
readPartsalready readsgroup:<id>. A write between the two moves the stamp; the cursor'sT0, taken before any read, makes the next poll see it.
Left to the builder
- Names, cache entry names, and removing whatever becomes dead (
recentStartGroups,readLight's per-person options if no caller is left, the startdistinct). - Whether
undecidedWorkkeeps loading members' conversations once its busy rule matchesawaitingWorkers, as long as the replies match the full state.
Tests
- Same reply as the full state. A scene with several readers (owner, a member of a private project, a member
outside it, an agent via
asAgentId, a Buddy) and: waiting and resolved tags in active and dormant conversations; finished starts in dormant conversations, one changed since the cursor and one listed by a machine record; a moved read mark; a progress change; an agent with no stored decision that is a member (not assignee) of running work and has finished work in another active conversation; an agent stored withoutprojectIdin a Swarm with projects and in one without. Each reader'schangesreply under the new code equals the reply built from the full state (readParts, asfirstLoadreads it), and equals today's reply wherever today's already did. - Small miss. A Swarm with hundreds of finished starts in dormant conversations: a
changesmiss reads none of those conversations and no progress part (count part reads, asintegration/superTabCache.test.tsdoes). - Shared load. Three people polling one Swarm at one stamp run
loadLightonce; after a write, once more in total. - Named groups. Two readers mentioned in one dormant conversation read it once per stamp; an unknown id once.
- Per-Swarm drop. A bump of one Swarm drops only its entries; byte and entry counts stay right after drops, evictions and expiry.
- Existing suites stay green:
read/changes.test.ts,read/views.test.ts,parts/light.test.ts,integration/superTabCache.test.ts,integration/superTabRead.test.ts,integration/superTabHotReads.test.ts, and theprojects/commandsunit tests.
Measuring
Before and after, same weekday hours, from prod logs (cpu by kind, db by shape, route latency, cpu profile):
- the route's busy ms per pod per minute (baseline 2.06 s);
- calls, docs and bytes of each shape under the route (baseline table above);
- Prisma's and GC's share in
cpu profile; - the read cache's entry count per pod (
SwarmCache.size).
Done when: tests show no reply change and one loadLight per Swarm, stamp and pod; and prod shows the
SwarmGroup key $in and SwarmProgress shapes under the route falling from about 8.8 MB to under 1 MB a minute per
pod. The CPU before/after is reported with it (expected: the route's busy time about halves; an estimate, since the
log does not split a read's time between loading and the rest).
Design (AGENTS.md rules)
- Growth: the LRU (
SWARM_CACHE_MB, 64 MB, 60 s max age) holds the new per-group entries; everymainbump drops a Swarm's entries. Per-reader light entries go, and the base itself shrinks from hundreds of conversations to tens. The per-Swarm name index keeps bumps proportional to one Swarm's entries. - Concurrency: no locks, as the read cache: entries are held under the stamp read before loading; concurrent misses of one entry share one load.
- Read cost: a hit costs the stamp
HMGET, as today. A miss costs one smallloadLightper pod per stamp, plus one batched group read for named ids that miss. - Scale: at 10× readers per Swarm the base stays one load per stamp per pod; the per-person indexed reads grow
linearly and are the first to watch in
db by shape. At 10× live starts the base no longer grows with them. At 10× writes, the reload per write is the limit; the route'scpu by kindwarns first. - Compatibility: reply shapes are unchanged, so every client is unaffected. Values change only where today's
light reply disagreed with the full state (section 0), toward what
firstLoadalready sends. During a rolling deploy old pods keep their per-person entries under other names; all pods read the same stamps. - Migration + rollback: nothing stored changes. Roll back by deploying the previous release; the read cache can
be turned off with
SWARM_READ_CACHE=off. - Audience: permissions stay after the cache; held values are the stored state, the same for every reader. The equality tests enforce it per reader kind.
- Identity: N/A because nothing here acts as or reads the machine owner's login.
- Failure: without stamps every read goes to Mongo, as today. A failed group read fails the poll as a failed
loadLightdoes today. - Platforms: backend only.