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.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
perf(swarm): the route-decision list reads only the groups its decisions name (#2264)
0xken15812683c0689b
diff --git a/backend/integration/superTabRouteDecisions.test.ts b/backend/integration/superTabRouteDecisions.test.tsnew file mode 100644index 000000000..8278b5a84--- /dev/null+++ b/backend/integration/superTabRouteDecisions.test.ts@@ -0,0 +1,275 @@+import { randomUUID } from 'node:crypto'+import { execFile } from 'node:child_process'+import { promisify } from 'node:util'+import { join } from 'node:path'+import { tmpdir } from 'node:os'+import { afterAll, beforeAll, describe, expect, it } from 'vitest'+import { MongoMemoryReplSet } from 'mongodb-memory-server-core'+import { PrismaClient } from '@prisma/client'+import type { AuthUser } from '../src/lib/ssoAuth.js'+import { SuperTabStore } from '../src/superTabs/store.js'+import { swarmTransaction } from '../src/superTabs/cache.js'+import { migrateSwarm } from '../src/superTabs/migration/migrate.js'+import { writeLegacySwarm } from '../scripts/swarm-parts/legacy.js'+import { author, type SuperTabState } from '../src/superTabs/model.js'+import { refuseBuddyRouting } from '../src/superTabs/buddies.js'+import { canReadGroup } from '../src/superTabs/projects.js'+import { recentDecisions, timedDecisionOf } from '../src/superTabs/routeDecisions.js'+import { listNamedIndexes } from '../src/superTabs/parts/indexed.js'+import { NOW, outsider as outsiderActor, owner as ownerActor, peer as peerActor, swarm } from '../src/superTabs/read/fixture.test-support.js'++// A fresh loopback replica set, created here. This suite never reads DATABASE_URL or an existing database.+// The Lead's route command reads the route-decision list on every route (prod watch 10-29: 13% of slow Swarm reads). The read+// used to load every group the Swarm ever made, dormant ones too, to learn which the reader may read: 6.7 MB per call for 1,200+// groups. These tests hold the answer to exactly what that read gave every reader, and the cost to the groups the decisions name.+let replica: MongoMemoryReplSet+let db: PrismaClient+let store: SuperTabStore+const user = (actor: { userId: string; email: string }): AuthUser => ({ sub: actor.userId, email: actor.email, autonomousEnv: 'prod', role: 'user' })+const [owner, peer, outsider] = [ownerActor, peerActor, outsiderActor].map(user) as [AuthUser, AuthUser, AuthUser]+const MIGRATED = NOW - 60_000+const DAY = 86_400_000++beforeAll(async () => {+ replica = await MongoMemoryReplSet.create({ binary: { version: '7.0.24', downloadDir: join(tmpdir(), 'harness-super-tab-mongodb-binaries') },+ replSet: { count: 1, ip: '127.0.0.1', storageEngine: 'wiredTiger' } })+ const uri = replica.getUri(`swarm_route_decisions_test_${randomUUID().replaceAll('-', '_')}`)+ await promisify(execFile)(join(process.cwd(), 'node_modules/.bin/prisma'), ['db', 'push', '--skip-generate'], {+ env: { ...process.env, DATABASE_URL: uri }, timeout: 60000,+ })+ db = new PrismaClient({ datasources: { db: { url: uri } } })+ for (const each of [owner, peer, outsider]) await db.user.create({ data: { id: each.sub, externalId: `fixture-${each.sub}`, email: each.email, autonomousEnv: 'prod' } })+ await db.machine.create({ data: { machineId: 'host', userId: owner.sub, apiKey: 'fixture-only', managerId: 'fixture', autonomousEnv: 'prod', authMode: 'remote' } })+ // The server lists the indexes hot reads name at start (watchGroupIndexes): a read names one only once it is listed.+ await listNamedIndexes(db)+ store = new SuperTabStore(db)+}, 120000)+afterAll(async () => {+ await db?.$disconnect()+ await replica?.stop()+})++/** A Swarm in parts: a Lead and a worker on the owner's machine, the Web group (active), a private project, the Docs project+ * whose only worker is stored with no project of its own and whose only conversation is finished and quiet (dormant), a+ * closed Chat with PM and a Buddy chat of the peer, and `quiet` finished public groups quiet for two days (dormant). */+async function world(quiet = 0) {+ const s = swarm(MIGRATED)+ s.join(peerActor)+ s.join(outsiderActor)+ const lead = s.add('lead-harness', 'lead', true, ownerActor, 'host', owner.sub)+ const secret = s.run({ type: 'project.create', name: 'Secret', visibility: 'private', brief: '' }).projectId as string+ const secretLead = s.add('secret-lead', s.state.projects!.find(p => p.id === secret)!.leadConversationId, true, ownerActor, 'host', owner.sub)+ const docs = s.run({ type: 'project.create', name: 'Docs', visibility: 'public', brief: '' }).projectId as string+ const web = s.group('Web')+ const worker = s.add('worker-harness', web, false, ownerActor, 'host', owner.sub)+ s.run({ type: 'work.assign', conversationId: web, agentId: worker })+ const secretGroup = s.group('Secret work', secret)+ const secretWorker = s.add('secret-worker', secretGroup, false, ownerActor, 'host', owner.sub)+ s.run({ type: 'work.assign', conversationId: secretGroup, agentId: secretWorker })+ const docsGroup = s.group('Docs work', docs)+ const docsWorker = s.add('docs-worker', docsGroup, false, ownerActor, 'host', owner.sub)+ s.run({ type: 'work.assign', conversationId: docsGroup, agentId: docsWorker })+ const peerChat = s.group('Peer and PM')+ const buddyChat = s.group('Peer\'s Buddy')+ const finished = Array.from({ length: quiet }, (_, i) => s.group(`Done ${i}`))+ s.edit(state => {+ const peerGeneration = state.people.find(p => p.id === peer.sub)!.generation+ state.conversations.find(c => c.id === peerChat)!.audience = { people: [{ id: peer.sub, generation: peerGeneration }] }+ Object.assign(state.conversations.find(c => c.id === buddyChat)!, { kind: 'buddy', leadId: null, agentIds: [], audience: { people: [{ id: peer.sub, generation: peerGeneration }] } })+ const done = (group: string, as: string) => { state.conversations.find(c => c.id === group)!.assignment = { ...state.conversations.find(c => c.id === web)!.assignment!, id: as, status: 'done' } }+ done(docsGroup, 'docs-done')+ finished.forEach((group, i) => done(group, `done-${i}`))+ })+ const tabId = `tab-${randomUUID()}`+ const state = { ...structuredClone(s.state), id: tabId } as SuperTabState+ const quietIds = [docsGroup, ...finished]+ await writeLegacySwarm(db, { state, revision: 700, messages: quietIds.map((id, i) => ({ id: `${tabId}-m${i}`, conversationId: id, sequence: i + 5, text: 'Done',+ author: { kind: 'person' as const, id: owner.sub, name: owner.email, membershipGeneration: tabId }, createdAt: new Date(MIGRATED - 2 * DAY) })) })+ const report = await migrateSwarm(db, tabId, { now: () => MIGRATED })+ if (!report.ok) throw new Error(report.error)+ // An agent stored with no project takes it from its first conversation (projects.ts agentProjectId); the migration gives every+ // agent of a Swarm with projects its own, so the part is changed as an older write left it.+ const stored = await db.swarmAgent.findFirstOrThrow({ where: { tabId, key: docsWorker } })+ const { projectId: _project, ...legacy } = stored.data as Record<string, unknown>+ await db.swarmAgent.update({ where: { id: stored.id }, data: { data: legacy as never } })+ expect((await db.swarmGroup.findFirst({ where: { tabId, key: docsGroup } }))?.dormant, 'the Docs worker\'s only conversation is quiet').toBe(true)+ return { tabId, lead, worker, docsWorker, secretWorker, secretLead, web, docsGroup, secretGroup, peerChat, buddyChat, finished }+}+type World = Awaited<ReturnType<typeof world>>++/** Decisions, oldest first: `runs` lists each conversation and how many decisions it gets, in order, the last run newest. The+ * text of those in `hidden` conversations is what no one outside them may see. */+async function decide(w: World, runs: Array<[group: string, count: number]>, hidden: string[] = []) {+ let n = 0+ const rows = runs.flatMap(([conversationId, count]) => Array.from({ length: count }, () => {+ const i = n+++ return { id: `${w.tabId}-d${String(i).padStart(4, '0')}`, tabId: w.tabId, conversationId, record: { why: `${hidden.includes(conversationId) ? 'hidden' : 'shown'}-text-of-${conversationId}-${i}`, candidates: [] },+ createdAt: new Date(NOW - 40 * DAY + i * 1000) }+ }))+ for (let i = 0; i < rows.length; i += 500) await db.superTabRouteDecision.createMany({ data: rows.slice(i, i + 500) })+ return rows+}++/** What the list gave every reader before: every group of the Swarm loaded, the ones the reader may read kept, and the newest+ * 100 decisions of those. */+function listedBefore(w: World, reader: AuthUser, asAgentId?: string) {+ return swarmTransaction(db, async tx => {+ const { state } = await store.load(tx, reader, w.tabId, { allGroups: true })+ const actor = await store.actor(tx, reader, state, asAgentId)+ author(state, actor)+ refuseBuddyRouting(state, actor)+ const groups = state.conversations.filter(c => canReadGroup(state, actor, c)).map(c => c.id)+ return (await recentDecisions(tx, w.tabId, groups)).map(timedDecisionOf)+ })+}+const settle = async <T>(read: () => Promise<T>) => read().then(value => ({ value }), (error: Error) => ({ error: error.message }))+const readers = (w: World): Array<[string, AuthUser, string | undefined]> => [['the owner', owner, undefined], ['a collaborator', peer, undefined],+ ['a member outside the private project', outsider, undefined], ['the Lead', owner, w.lead], ['a worker', owner, w.worker], ['the Docs worker with no project of its own', owner, w.docsWorker],+ ['the Secret project\'s worker', owner, w.secretWorker], ['the Lead of the Secret project', owner, w.secretLead]]++/** Every reader gets, for the same Swarm, exactly what the list gave before. */+async function sameAnswers(w: World) {+ const got: Record<string, Awaited<ReturnType<typeof settle<unknown[]>>>> = {}+ for (const [name, reader, agent] of readers(w)) {+ const now = await settle(() => store.routeDecisions(reader, w.tabId, agent))+ expect(now, name).toEqual(await settle(() => listedBefore(w, reader, agent)))+ got[name] = now+ }+ return got+}+const ids = (answer: { value?: Array<{ id: string }> } | undefined) => (answer?.value ?? []).map(d => d.id)++describe('what the route-decision list gives each reader', () => {+ it('is what it gave before, for every kind of reader, over groups they may and may not read', async () => {+ const w = await world(5)+ // Newest last. Every kind of group, interleaved, with the secrets in the middle of the newest hundred.+ const all = [w.web, w.secretGroup, w.docsGroup, w.peerChat, w.buddyChat, ...w.finished, 'no-such-group']+ await decide(w, Array.from({ length: 130 }, (_, i) => [all[i % all.length]!, 1] as [string, number]))+ const got = await sameAnswers(w)+ // The test must tell readers apart, or "same as before" proves little.+ const seen = Object.fromEntries(Object.entries(got).map(([name, answer]) => [name, new Set((answer.value ?? []).map(d => (d as { conversationId: string }).conversationId))]))+ expect(seen['the owner']!.has(w.secretGroup)).toBe(true)+ expect(seen['a member outside the private project']!.has(w.secretGroup)).toBe(false)+ expect(seen['a collaborator']!.has(w.buddyChat)).toBe(true)+ expect(seen['the owner']!.has(w.buddyChat)).toBe(false)+ expect(seen['the Lead']!.has('no-such-group')).toBe(false)+ // The Docs worker reads the Docs project through its finished group, which is not loaded unless asked for by agent.+ expect(seen['the Docs worker with no project of its own']!.has(w.docsGroup)).toBe(true)+ expect(seen['the Docs worker with no project of its own']!.has(w.web)).toBe(false)+ })++ it('is what it gave before when the newest hundred are mostly the reader\'s unreadable ones: a wider window of decisions', async () => {+ const w = await world(60)+ await decide(w, [[w.web, 120], [w.secretGroup, 150]])+ const got = await sameAnswers(w)+ // The collaborator's read is the wider window, still without loading the Swarm's quiet groups.+ const [answer, profile] = await profiled(() => store.routeDecisions(peer, w.tabId))+ expect(profile.SuperTabRouteDecision!.plans).toContain('IXSCAN { tabId: 1, createdAt: 1 }')+ expect(profile.SuperTabRouteDecision!.hinted, 'the index scans name their index').toBe(true)+ // The wider window is read as conversation ids only: the replies hold the newest hundred whole, the answer's hundred, and ids.+ expect(profile.SuperTabRouteDecision!.bytes).toBeLessThan(3 * JSON.stringify(answer).length)+ expect(profile.SwarmGroup!.docsExamined).toBeLessThan(await db.swarmGroup.count({ where: { tabId: w.tabId, deleted: false } }))+ // The collaborator and the outsider read only Web: its newest 100 reach back past 150 secret decisions.+ expect(ids(got['a collaborator'])).toHaveLength(100)+ expect(ids(got['a member outside the private project'])).toHaveLength(100)+ expect(ids(got['the owner']).every(id => Number(id.slice(-4)) >= 170)).toBe(true)+ })++ it('is what it gave before when even a wider window holds fewer than a hundred of the reader\'s: the whole Swarm is read', async () => {+ const w = await world(60)+ await decide(w, [[w.web, 60], [w.secretGroup, 600]])+ const got = await sameAnswers(w)+ // Both windows, then every group.+ const [, profile] = await profiled(() => store.routeDecisions(peer, w.tabId))+ expect(profile.SwarmGroup!.docsExamined).toBeGreaterThanOrEqual(await db.swarmGroup.count({ where: { tabId: w.tabId, deleted: false } }))+ expect(ids(got['a collaborator'])).toHaveLength(60)+ expect(ids(got['the owner'])).toHaveLength(100)+ })++ it('is what it gave before for a project\'s worker and Lead who read fewer than a hundred: only its project\'s groups are read, not the Swarm\'s', async () => {+ const w = await world(60)+ await decide(w, [[w.secretGroup, 30], [w.web, 600]])+ const got = await sameAnswers(w)+ expect(ids(got['the Secret project\'s worker'])).toHaveLength(30)+ expect(ids(got['the Lead of the Secret project'])).toHaveLength(30)+ const all = await db.swarmGroup.count({ where: { tabId: w.tabId, deleted: false } })+ for (const agent of [w.secretWorker, w.secretLead]) {+ const [, profile] = await profiled(() => store.routeDecisions(owner, w.tabId, agent))+ expect(profile.SwarmGroup!.docsExamined, 'the quiet groups of other projects stay unread').toBeLessThan(all / 2)+ }+ })++ it('stops at one window of decisions when the Swarm holds no more than that, even if the reader reads fewer than a hundred of them', async () => {+ const w = await world(5)+ await decide(w, [[w.secretGroup, 60], [w.web, 40]], [w.secretGroup])+ const got = await sameAnswers(w)+ expect(ids(got['a member outside the private project'])).toHaveLength(40)+ const [, profile] = await profiled(() => store.routeDecisions(outsider, w.tabId))+ expect(profile.SuperTabRouteDecision!.docsExamined, 'a hundred decisions are all there are: no wider window to read').toBe(100)+ })++ it('names nothing of a conversation the reader may not read, whether it is private, another project\'s, a Buddy\'s or gone', async () => {+ const w = await world(2)+ await decide(w, [[w.secretGroup, 5], [w.buddyChat, 5], [w.peerChat, 5], ['gone-group', 5], [w.web, 5]], [w.secretGroup, w.buddyChat, w.peerChat, 'gone-group'])+ const as = async (reader: AuthUser, agent?: string) => JSON.stringify(await store.routeDecisions(reader, w.tabId, agent))+ for (const hidden of [w.secretGroup, w.buddyChat, w.peerChat, 'gone-group']) {+ expect(await as(outsider), hidden).not.toContain(hidden)+ expect(await as(owner, w.worker), hidden).not.toContain(hidden)+ }+ expect(await as(outsider)).not.toContain('hidden-text-of-')+ expect(JSON.parse(await as(outsider))).toHaveLength(5)+ expect(JSON.parse(await as(peer))).toHaveLength(10)+ })++ it('refuses a reader who has left the Swarm, with no decision in the answer', async () => {+ const w = await world(1)+ await decide(w, [[w.web, 3]], [w.web])+ await db.swarmMember.updateMany({ where: { tabId: w.tabId, key: outsider.sub }, data: { deleted: true } })+ const left = await settle(() => store.routeDecisions(outsider, w.tabId))+ expect(left).toEqual({ error: expect.any(String) })+ expect(left).toEqual(await settle(() => listedBefore(w, outsider)))+ expect(JSON.stringify(left)).not.toContain('hidden-text-of-')+ })+})++interface Profiled { plans: string[]; docsExamined: number; returned: number; bytes: number; hinted: boolean }+/** What Mongo did for each collection while `read` ran: its plans, the documents it examined and the rows it returned. */+async function profiled<T>(read: () => Promise<T>): Promise<[T, Record<string, Profiled>]> {+ await db.$runCommandRaw({ profile: 2 })+ const result = await read()+ await db.$runCommandRaw({ profile: 0 })+ const found = await db.$runCommandRaw({ find: 'system.profile', filter: { 'command.profile': { $exists: false } }, batchSize: 10000 }) as+ unknown as { cursor: { firstBatch: Array<{ ns: string; planSummary?: string; docsExamined?: number; nreturned?: number; responseLength?: number; command?: { hint?: unknown } }> } }+ await db.$runCommandRaw({ drop: 'system.profile' })+ const by: Record<string, Profiled> = {}+ for (const row of found.cursor.firstBatch) {+ const profile = by[row.ns.split('.').pop()!] ??= { plans: [], docsExamined: 0, returned: 0, bytes: 0, hinted: false }+ if (row.planSummary && !profile.plans.includes(row.planSummary)) profile.plans.push(row.planSummary)+ profile.docsExamined += row.docsExamined ?? 0+ profile.returned += row.nreturned ?? 0+ profile.bytes += row.responseLength ?? 0+ profile.hinted ||= row.command?.hint !== undefined+ }+ return [result, by]+}++describe('what the route-decision list costs the database', () => {+ const QUIET = 400+ it('reads the newest hundred decisions by index and only the groups they name, however many groups the Swarm has made', async () => {+ const w = await world(QUIET)+ // 300 decisions over 300 different quiet groups: the newest hundred name a hundred of them.+ await decide(w, w.finished.slice(0, 300).map(group => [group, 1] as [string, number]))+ const live = await db.swarmGroup.count({ where: { tabId: w.tabId, deleted: false, dormant: false } })+ const all = await db.swarmGroup.count({ where: { tabId: w.tabId, deleted: false } })+ expect(all - live).toBeGreaterThanOrEqual(QUIET)+ const [answer, profile] = await profiled(() => store.routeDecisions(owner, w.tabId, w.lead))+ const groups = profile.SwarmGroup!+ expect(answer).toHaveLength(100)+ expect(profile.SuperTabRouteDecision).toMatchObject({ plans: ['IXSCAN { tabId: 1, createdAt: 1 }'], docsExamined: 101, returned: 101, hinted: true })+ // The live groups, the Lead's own, and the hundred the decisions name: not the quiet ones nobody asked about.+ expect(groups.docsExamined).toBeLessThanOrEqual(live + 1 + 100)+ expect(groups.docsExamined).toBeLessThan(all / 2)+ // The answer is what it was.+ expect(answer).toEqual(await listedBefore(w, owner, w.lead))+ })+})diff --git a/backend/src/superTabs/parts/indexed.ts b/backend/src/superTabs/parts/indexed.tsindex 296084fcf..8c21c1fc9 100644--- a/backend/src/superTabs/parts/indexed.ts+++ b/backend/src/superTabs/parts/indexed.ts@@ -59,6 +59,8 @@ export const NAMED_INDEX = {/** One message. */id: { _id: 1 },},+ /** A Swarm's newest route decisions: the route-decision list. */+ SuperTabRouteDecision: { recent: { tabId: 1, createdAt: 1 } },SuperTabDelivery: {/** An agent's deliveries by status and age: its open ones, oldest first. */agentOpen: { tabId: 1, agentId: 1, status: 1, createdAt: 1 },diff --git a/backend/src/superTabs/routeDecisions.ts b/backend/src/superTabs/routeDecisions.tsindex 6a3f1606b..51c85654a 100644--- a/backend/src/superTabs/routeDecisions.ts+++ b/backend/src/superTabs/routeDecisions.ts@@ -1,6 +1,7 @@/** Persistence for route decisions. Called inside SuperTabStore's transactions. */import type { Prisma, SuperTabRouteDecision } from '@prisma/client'import { ms } from './parts/light.js'+import { readBy } from './parts/indexed.js'import type { SuperTabState } from './model.js'import { attemptChanges, endRun, startRun, type RouteRun } from './costs/attempts.js'import type { TokensByModel } from './costs/tokens.js'@@ -31,16 +32,34 @@ export async function readDecision(tx: Tx, tabId: string, id: string): Promise<Rreturn row ? decisionOf(row) : undefined}-/** The newest 100 decisions in the conversations a reader may see, newest first, as Prisma reads them. A plain $in match:- * Prisma's `conversationId: { in }` is an $expr $or that Mongo plans branch by branch, 25 ms a read over a Lead's ~360- * conversations (integration/superTabIndexes.test.ts). */+/** What the list shows of a Swarm: its newest decisions. */+export const LIST_SIZE = 100++const rowOf = (row: Record<string, unknown>): SuperTabRouteDecision => ({ id: row._id as string, tabId: row.tabId as string, conversationId: row.conversationId as string,+ record: row.record as Prisma.JsonValue, final: (row.final ?? null) as Prisma.JsonValue, override: (row.override ?? null) as Prisma.JsonValue,+ assignmentId: (row.assignmentId ?? null) as string | null, outcome: (row.outcome ?? null) as string | null, run: (row.run ?? null) as Prisma.JsonValue,+ evidence: (row.evidence ?? null) as Prisma.JsonValue, outcomeAt: row.outcomeAt == null ? null : new Date(ms(row.outcomeAt)), createdAt: new Date(ms(row.createdAt)) })++/** The newest 100 decisions in the conversations a reader may see, newest first, as Prisma reads them. Plain `aggregateRaw`+ * matches: Prisma's `conversationId: { in }` is an $expr $or that Mongo plans branch by branch, 25 ms a read over a Lead's+ * ~360 conversations (integration/superTabIndexes.test.ts). */export async function recentDecisions(tx: Tx, tabId: string, conversationIds: string[]): Promise<SuperTabRouteDecision[]> {const rows = await tx.superTabRouteDecision.aggregateRaw({ pipeline: [{ $match: { tabId, conversationId: { $in: conversationIds } } },- { $sort: { createdAt: -1 } }, { $limit: 100 }] }) as unknown as Array<Record<string, unknown>>- return rows.map(row => ({ id: row._id as string, tabId: row.tabId as string, conversationId: row.conversationId as string,- record: row.record as Prisma.JsonValue, final: (row.final ?? null) as Prisma.JsonValue, override: (row.override ?? null) as Prisma.JsonValue,- assignmentId: (row.assignmentId ?? null) as string | null, outcome: (row.outcome ?? null) as string | null, run: (row.run ?? null) as Prisma.JsonValue,- evidence: (row.evidence ?? null) as Prisma.JsonValue, outcomeAt: row.outcomeAt == null ? null : new Date(ms(row.outcomeAt)), createdAt: new Date(ms(row.createdAt)) }))+ { $sort: { createdAt: -1 } }, { $limit: LIST_SIZE }] }) as unknown as Array<Record<string, unknown>>+ return rows.map(rowOf)+}++/** The newest `limit` decisions of the Swarm in any conversation: one scan of the (tabId, createdAt) index of `limit` documents.+ * The reader's access is applied after, to the conversations these name only. */+export async function newestDecisions(tx: Tx, tabId: string, limit: number): Promise<SuperTabRouteDecision[]> {+ return (await readBy(tx, 'SuperTabRouteDecision', 'recent', [{ $match: { tabId } }, { $sort: { createdAt: -1 } }, { $limit: limit }])).map(rowOf)+}++/** The conversation of each of the Swarm's newest `limit` decisions, newest first: the same scan, a wider window, with none of+ * a decision's text in the reply. */+export async function newestConversations(tx: Tx, tabId: string, limit: number): Promise<string[]> {+ return (await readBy(tx, 'SuperTabRouteDecision', 'recent', [{ $match: { tabId } }, { $sort: { createdAt: -1 } }, { $limit: limit },+ { $project: { conversationId: 1 } }])).map(row => row.conversationId as string)}export async function writeRouteEffects(tx: Tx, tabId: string, effects: RouteDecisionEffect[], now: number): Promise<void> {diff --git a/backend/src/superTabs/store.ts b/backend/src/superTabs/store.tsindex 2f7e9d1ad..1108bac0e 100644--- a/backend/src/superTabs/store.ts+++ b/backend/src/superTabs/store.ts@@ -31,7 +31,7 @@ import { answersRequest, dirtyGroups, noteProjectReports, projectUpdateRequest,import { digestDue, digestMessageId, digestRequest, digestSince } from './digest.js'import { judgedMachines, loseTasks, sweepForLead, sweepLiveness, SWEEP_EVERY_MS } from './liveness.js'import { archivable, archiveLine, ASLEEP_FIELDS, asleepMatch, stopIdleWorkers, stoppedMachines, type GroupActivity } from './tidy.js'-import { clock, insertParts, loadActive, loadObserved, reassemble, split, type HeadData, type SwarmParts } from './parts/index.js'+import { clock, insertParts, loadActive, loadGroups, loadObserved, reassemble, split, type GroupData, type HeadData, type Part, type SwarmParts } from './parts/index.js'import { groupsBy, listingDrops, quietSince, readBy, withoutHints } from './parts/indexed.js'import { memberSwarms } from './swarmRows.js'import { commitParts, indexesOf, numberMessages, type Touch, type Written } from './parts/commit.js'@@ -45,7 +45,7 @@ import { activeMember, addMember, hasWorkspaceAccess, listWorkspaces, memberId,import { WORKSPACE_LABEL, type WorkspaceRole } from './workspaces.js'import {activeWork, actorKey, agent, agentOnly, agentRole, appendPullRequestUrls, author, conversation, createSuperTab, fail, person, stableId, visibleState, finishWorker,- SuperTabError, isLeads, parkLaterConversations, syncLeads, type Actor, type Agent, type Assignment, type Author, type Conversation, type MessageDraft, type Person, type SuperTabState,+ SuperTabError, isLeads, knownKind, parkLaterConversations, syncLeads, type Actor, type Agent, type Assignment, type Author, type Conversation, type MessageDraft, type Person, type SuperTabState,} from './model.js'import type { HoldReason } from './holdReason.js'import { stuck } from './deliverySettle.js'@@ -55,7 +55,7 @@ import { accrueLent } from './leaderboard/lent.js'import { mayDrive, TERMINAL_ANSWER_MS, TERMINAL_EXPIRY_MS, TERMINAL_PICKUP_MS, TERMINAL_RECHECK_MS, type TerminalEffect } from './terminal.js'import { costRows, summarizeCosts } from './costs/read.js'import { recordEvidence } from './evidence/store.js'-import { closeAssignment, closeUnassignedStart, readDecision, recentDecisions, recordAttempts, timedDecisionOf, writeRouteEffects } from './routeDecisions.js'+import { closeAssignment, closeUnassignedStart, LIST_SIZE, newestConversations, newestDecisions, readDecision, recentDecisions, recordAttempts, timedDecisionOf, writeRouteEffects } from './routeDecisions.js'import { dropHeldTags, listMentions, markDone, mentionsWaiting, openTags, readMention, resolveByReply, resolveWhenWorked, writeMentions } from './mentions.js'import { answerable, fullFor, listDecisions, type DecisionList, readAskedDecision, recordAnswer, repeatOf, waitingFor, waitingIn, waitingInProject, writeDecisions } from './decisions.js'import { CATCH_UP_SCAN, catchUpSummary, type CatchUp } from './catchUp.js'@@ -230,6 +230,8 @@ const pollMemoryFailed = (error: unknown): void => {}/** Callers kept for the quiet delivery poll before the stale ones are dropped. */const POLLED_CAP = 5_000+/** How many of a Swarm's newest decisions the route list reads, as conversation ids, to find a reader's hundred after the first hundred fell short. */+const WIDE_WINDOW = 5 * LIST_SIZE/** A Lead's start dispatches within seconds of being recorded; one still undispatched after this long was given up. */export const ABANDONED_START_MS = 15 * 60_000@@ -2078,16 +2080,61 @@ export class SuperTabStore {async routeDecisions(user: AuthUser, tabId: string, asAgentId?: string) {return transaction(this.db, async tx => {- const { state } = await this.load(tx, user, tabId, { allGroups: true })+ const { state } = await this.load(tx, user, tabId)const actor = await this.actor(tx, user, state, asAgentId)author(state, actor)refuseBuddyRouting(state, actor)- // Dormant conversations count too: their decisions stay readable (the list loads every group for this).- const groups = state.conversations.filter(c => canReadGroup(state, actor, c)).map(c => c.id)- return (await recentDecisions(tx, tabId, groups)).map(timedDecisionOf)+ return (await this.readableDecisions(tx, user, tabId, state, actor)).map(timedDecisionOf)})}+ /**+ * The newest 100 decisions in the conversations `actor` may read, dormant ones too (their decisions stay readable). The+ * newest decisions of the Swarm are read first, by index, and only the conversations they name are loaded to check the+ * reader's access: a Lead reads all, so one window of 100 answers. A reader of few finds fewer than 100 in it; the next 400+ * are read as conversation ids only, and if they hold 100 of the reader's, the answer is the newest 100 in those+ * conversations. If they hold fewer while older decisions exist, the conversations the reader may read are loaded to decide+ * (an agent's project's, a person's every one), as the list always did.+ */+ private async readableDecisions(tx: Transaction, user: AuthUser, tabId: string, state: SuperTabState, actor: Actor) {+ const groups = new Map(state.conversations.map(c => [c.id, c]))+ const reads = new Map<string, boolean>()+ // A conversation that is gone, or of a kind this release does not know, is read by no one (as in a full load).+ const adopt = (parts: Part<GroupData>[]) => { for (const part of parts) if (part.data && knownKind(part.data)) groups.set(part.key, part.data) }+ const check = async (ids: string[]) => {+ const unchecked = [...new Set(ids)].filter(id => !reads.has(id))+ const missing = unchecked.filter(id => !groups.has(id))+ if (missing.length) adopt(await loadGroups(tx, tabId, 'key', { key: { $in: missing } }))+ for (const id of unchecked) reads.set(id, groups.has(id) && canReadGroup(state, actor, groups.get(id)!))+ }+ // An agent stored with no project (a Swarm from before projects) takes its project from its first conversation, quiet ones+ // too (projects.ts agentProjectId): only a load of every group answers what it reads, so it goes straight to that.+ const target = actor.kind === 'agent' ? state.agents.find(a => a.id === actor.agentId) : undefined+ if (!target || target.projectId) {+ const first = await newestDecisions(tx, tabId, LIST_SIZE + 1)+ await check(first.slice(0, LIST_SIZE).map(d => d.conversationId))+ const mine = first.slice(0, LIST_SIZE).filter(d => reads.get(d.conversationId))+ if (mine.length === LIST_SIZE || first.length <= LIST_SIZE) return mine+ const wide = await newestConversations(tx, tabId, WIDE_WINDOW + 1)+ await check(wide.slice(0, WIDE_WINDOW))+ const readable = wide.slice(0, WIDE_WINDOW).filter(id => reads.get(id))+ if (readable.length >= LIST_SIZE || wide.length <= WIDE_WINDOW) return readable.length ? recentDecisions(tx, tabId, [...new Set(readable)]) : []+ // Fewer than 100 of the reader's among the newest 500: an agent reads only its project's conversations (and the Leads+ // conversations, by key); a person's can be any.+ if (target) {+ const project = target.projectId!+ adopt(await loadGroups(tx, tabId, 'project', { projectId: { $in: project === 'lead' ? ['lead', null] : [project] }, dormant: true }))+ const leads = [state.leadsConversationId, ...projectsOf(state).map(p => p.leadConversationId)].filter((id): id is string => !!id && !groups.has(id))+ if (leads.length) adopt(await loadGroups(tx, tabId, 'key', { key: { $in: leads } }))+ } else {+ adopt(await loadGroups(tx, tabId, 'dormancy', { dormant: true }))+ }+ return recentDecisions(tx, tabId, [...groups.values()].filter(c => canReadGroup(state, actor, c)).map(c => c.id))+ }+ const { state: whole } = await this.load(tx, user, tabId, { allGroups: true })+ return recentDecisions(tx, tabId, whole.conversations.filter(c => canReadGroup(whole, actor, c)).map(c => c.id))+ }+/** What the attempts routed in the last `days` cost and how they went (costs/read.ts), over the groups the reader may read.* The decisions are read first, by index, so only the groups they name are loaded. */async costSummary(user: AuthUser, tabId: string, days: number, asAgentId?: string) {diff --git a/backend/vitest.super-tabs.config.ts b/backend/vitest.super-tabs.config.tsindex 145397460..b500539bd 100644--- a/backend/vitest.super-tabs.config.ts+++ b/backend/vitest.super-tabs.config.ts@@ -2,7 +2,7 @@ import { defineConfig } from 'vitest/config'export default defineConfig({ test: { include: ['integration/superTabs.test.ts', 'integration/superTabSharing.test.ts', 'integration/superTabGithub.test.ts', 'integration/superTabTeam.test.ts', 'integration/superTabWorkspaces.test.ts','integration/superTabRead.test.ts', 'integration/superTabReadMarks.test.ts', 'integration/superTabLeaderboard.test.ts', 'integration/superTabMigration.test.ts', 'integration/superTabLiveness.test.ts',- 'integration/superTabTidy.test.ts', 'integration/superTabProfile.test.ts', 'integration/superTabLegacyDrop.test.ts', 'integration/shareRaces.test.ts', 'integration/superTabIndexes.test.ts',+ 'integration/superTabTidy.test.ts', 'integration/superTabProfile.test.ts', 'integration/superTabLegacyDrop.test.ts', 'integration/shareRaces.test.ts', 'integration/superTabIndexes.test.ts', 'integration/superTabRouteDecisions.test.ts','integration/superTabHotReads.test.ts', 'integration/swarmSweepStarts.test.ts', 'integration/superTabGroupPlans.test.ts', 'integration/superTabInvitationPlans.test.ts', 'integration/superTabCache.test.ts','integration/superTabObserver.test.ts', 'integration/productUsageConflicts.test.ts', 'integration/productUsageReceipts.test.ts', 'integration/schemaRelease.test.ts', 'integration/superTabBuddies.test.ts','integration/superTabBuddyProbes.test.ts', 'integration/superTabCosts.test.ts', 'integration/harnessSharesList.test.ts', 'integration/atlasSnapshot.test.ts', 'integration/queryStats.test.ts',