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.

perf(swarm): Desktop/web changes read one shared light base per Swarm (DB 2.3)

MAC29bb121f004b
diff --git a/backend/integration/superTabCache.test.ts b/backend/integration/superTabCache.test.ts
index 7c9965c19..200edfd46 100644
--- a/backend/integration/superTabCache.test.ts
+++ b/backend/integration/superTabCache.test.ts
@@ -15,7 +15,7 @@ import { guardedWrite } from '../src/superTabs/parts/write.js'
import type { HeadData, MachineData } from '../src/superTabs/parts/index.js'
import { sweepSwarms } from '../src/superTabs/sweeps.js'
import { SwarmCache } from '../src/superTabs/cache.js'
-import { readChanged, readLastChangedAt, readParts } from '../src/superTabs/cachedReads.js'
+import { DIRECT, readChanged, readLastChangedAt, readLightGroups, readParts } from '../src/superTabs/cachedReads.js'
import { SwarmReads } from '../src/superTabs/read/service.js'
import { migrateSwarm } from '../src/superTabs/migration/migrate.js'
import type { SuperTabState } from '../src/superTabs/model.js'
@@ -129,30 +129,35 @@ function pod(options: { cache?: SwarmCache | null } = {}) {
}
/** The rich fixture Swarm in parts, with a conversation private to `peer`: their Buddy chat. */
-async function privateScene() {
+async function privateScene(extra: (s: ReturnType<typeof richSwarm>) => void = () => {}) {
const s = richSwarm(Date.now())
const privateId = s.group('Peer and PM')
s.edit(state => {
const generation = state.people.find(p => p.id === peerActor.userId)!.generation
Object.assign(state.conversations.find(c => c.id === privateId)!, { kind: 'buddy', audience: { people: [{ id: peerActor.userId, generation }] } })
})
+ extra(s)
const tabId = `tab-${randomUUID()}`
await writeLegacySwarm(db, { state: { ...structuredClone(s.state), id: tabId } as SuperTabState, revision: 10 })
const report = await migrateSwarm(db, tabId)
if (!report.ok) throw new Error(report.error)
- return { tabId, privateId, secret: s.secret, secretGroup: s.secretGroup, web: s.web, lead: s.lead }
+ return { tabId, privateId, secret: s.secret, secretGroup: s.secretGroup, web: s.web, lead: s.lead, done: s.done }
}
const groupKeys = (read: { groups: Array<{ key: string }> }) => read.groups.map(g => g.key)
-/** The part collections' reads MongoDB served while `run` ran (its profiler, every operation). */
-async function partReads<T>(run: () => Promise<T>): Promise<{ value: T; reads: number }> {
+/** The part collections' reads MongoDB served while `run` ran (its profiler, every operation). Only each row's collection
+ * is read back: a light read's projection holds `$type`, which Prisma would take for one of its own tagged values. */
+async function partReads<T>(run: () => Promise<T>): Promise<{ value: T; reads: number; byCollection: Record<string, number> }> {
await db.$runCommandRaw({ profile: 2 })
const since = new Date()
try {
const value = await run()
const rows = await db.$runCommandRaw({ find: 'system.profile', filter: { ts: { $gte: { $date: since.toISOString() } },
- ns: { $regex: '\\.Swarm(Head|Member|Agent|Group|Machine|Start|Progress)$' } } }) as { cursor: { firstBatch: unknown[] } }
- return { value, reads: rows.cursor.firstBatch.length }
+ ns: { $regex: '\\.Swarm(Head|Member|Agent|Group|Machine|Start|Progress)$' } }, projection: { _id: 0, ns: 1 }, batchSize: 10_000 }) as
+ { cursor: { firstBatch: Array<{ ns: string }> } }
+ const byCollection: Record<string, number> = {}
+ for (const { ns } of rows.cursor.firstBatch) byCollection[ns.slice(ns.lastIndexOf('.') + 1)] = (byCollection[ns.slice(ns.lastIndexOf('.') + 1)] ?? 0) + 1
+ return { value, reads: rows.cursor.firstBatch.length, byCollection }
} finally {
await db.$runCommandRaw({ profile: 0 })
}
@@ -274,6 +279,80 @@ describe('composing a read from what the cache holds', () => {
})
})
+describe('a change poll\'s shared light read (DB 2.3)', () => {
+ const tag = (tabId: string, conversationId: string, personId: string) => db.superTabMention.create({ data: { id: `${tabId}-${conversationId}-${personId}`,
+ tabId, conversationId, messageId: `${conversationId}-msg`, personId, projectId: 'lead', authorAgentId: 'lead-agent', createdAt: new Date() } })
+ /** The change feed and the newest change at this stamp, held as a poll would hold them, so what is counted after is the
+ * light base and what each reader adds to it. */
+ const warm = async (cache: SwarmCache, tabId: string) => {
+ const view = await cache.at([tabId])
+ await readChanged(db, view, tabId, Date.now() - 120_000)
+ await readLastChangedAt(db, view, tabId)
+ }
+
+ it('three readers polling one Swarm at one stamp load its light base once, and once more after a write', async () => {
+ const machineId = `host-${randomUUID()}`
+ await db.machine.create({ data: { machineId, userId: people.owner.sub, apiKey: `fixture-${machineId}`, managerId: 'fixture', autonomousEnv: 'prod', authMode: 'remote' } })
+ let agentId = ''
+ const { tabId, web, done, secretGroup } = await privateScene(s => { agentId = s.add('reader-harness', s.web, false, ownerActor, machineId, ownerActor.userId) })
+ // Each person has their own waiting tags, so no two readers name the same conversations.
+ await tag(tabId, done, people.owner.sub)
+ await tag(tabId, secretGroup, people.owner.sub)
+ await tag(tabId, done, people.peer.sub)
+ const { reads, store, cache } = pod()
+ const cursors = [(await reads.firstLoad(people.owner, tabId)).cursor, (await reads.firstLoad(people.peer, tabId)).cursor,
+ (await reads.firstLoad(people.owner, tabId, agentId)).cursor]
+ const poll = () => Promise.all([reads.changes(people.owner, tabId, cursors[0]!), reads.changes(people.peer, tabId, cursors[1]!),
+ reads.changes(people.owner, tabId, cursors[2]!, agentId)])
+ await warm(cache!, tabId)
+ const first = await partReads(poll)
+ expect(first.byCollection.SwarmHead).toBe(1)
+ expect(first.byCollection.SwarmProgress ?? 0).toBe(0)
+ await store.execute(people.owner, tabId, { operationId: `send-${randomUUID()}`,
+ command: { type: 'message.send', conversationId: web, text: 'moves the stamp', replyTo: null, recipientIds: [] } })
+ await warm(cache!, tabId)
+ const after = await partReads(poll)
+ expect(after.byCollection.SwarmHead).toBe(1)
+ expect(after.byCollection.SwarmProgress ?? 0).toBe(0)
+ })
+
+ it('a second reader tagged in the same dormant conversation reads no part at the same stamp', async () => {
+ const { tabId, done, secretGroup } = await privateScene()
+ await db.swarmGroup.update({ where: { id: `${tabId}:${done}` }, data: { dormant: true } })
+ // The owner is also tagged elsewhere, so the two readers do not name the same conversations.
+ await tag(tabId, done, people.owner.sub)
+ await tag(tabId, secretGroup, people.owner.sub)
+ await tag(tabId, done, people.peer.sub)
+ const { reads } = pod()
+ const cursors = [(await reads.firstLoad(people.owner, tabId)).cursor, (await reads.firstLoad(people.peer, tabId)).cursor]
+ await reads.changes(people.owner, tabId, cursors[0]!)
+ const second = await partReads(() => reads.changes(people.peer, tabId, cursors[1]!))
+ expect(second.reads).toBe(0)
+ expect((second.value as { derived: { mentionsWaiting: number } }).derived.mentionsWaiting).toBe(1)
+ })
+})
+
+describe('named light conversations', () => {
+ it('holds named light conversations per group: a second reader and an unknown id read nothing at the same stamp', async () => {
+ const { tabId, web } = await privateScene()
+ const quiet = (await db.swarmGroup.findFirstOrThrow({ where: { tabId, key: { notIn: [web, 'lead'] }, kind: null, dormant: false } })).key
+ await db.swarmGroup.update({ where: { id: `${tabId}:${quiet}` }, data: { dormant: true } })
+ const { cache, store } = pod()
+ const read = async (ids: string[]) => readLightGroups(db, await cache!.at([tabId]), tabId, ids)
+ const first = await partReads(() => read([quiet, 'nope', web]))
+ expect(first.reads).toBe(1)
+ // In the order named, light (no brief), an unknown id left out.
+ expect(first.value.map(g => [g.key, !!g.dormant, g.data!.brief])).toEqual([[quiet, true, ''], [web, false, '']])
+ // Another reader at the same stamp: every id held, the unknown one as none.
+ expect(await partReads(() => read(['nope', web, quiet]))).toMatchObject({ reads: 0, value: [{ key: web }, { key: quiet }] })
+ await store.execute(people.owner, tabId, { operationId: `send-${randomUUID()}`,
+ command: { type: 'message.send', conversationId: web, text: 'moves the stamp', replyTo: null, recipientIds: [] } })
+ expect((await partReads(() => read([quiet, 'nope']))).reads).toBe(1)
+ // Without stamps: read from Mongo every time, still in one query.
+ expect((await partReads(() => readLightGroups(db, DIRECT, tabId, [quiet, 'nope', web]))).reads).toBe(1)
+ })
+})
+
describe('the change feed', () => {
it('gives what loadChanged gives for any cursor, and serves a later cursor at the same stamp from memory', async () => {
const { tabId, web } = await privateScene()
@@ -346,6 +425,6 @@ describe('a machine\'s own reading', () => {
const first = await readLastChangedAt(db, await cache.at([tabId]), tabId)
await write(Date.now(), { released: ['again'] }, false)
const again = await partReads(async () => readLastChangedAt(db, await cache.at([tabId]), tabId))
- expect(again).toEqual({ value: first, reads: 0 })
+ expect(again).toEqual({ value: first, reads: 0, byCollection: {} })
})
})
diff --git a/backend/integration/superTabRead.test.ts b/backend/integration/superTabRead.test.ts
index 676859b72..c4b2bd6eb 100644
--- a/backend/integration/superTabRead.test.ts
+++ b/backend/integration/superTabRead.test.ts
@@ -17,6 +17,8 @@ import { guardedWrite } from '../src/superTabs/parts/write.js'
import { migrateSwarm } from '../src/superTabs/migration/migrate.js'
import { SwarmReads } from '../src/superTabs/read/service.js'
import { decisionsWaiting } from '../src/superTabs/decisions.js'
+import { waitingTagConversations } from '../src/superTabs/mentions.js'
+import { writeReadMark } from '../src/superTabs/readMarks.js'
import { buddyChatId } from '../src/superTabs/buddies.js'
import { HostReads } from '../src/superTabs/read/host.js'
import type { Conversation, SuperTabState } from '../src/superTabs/model.js'
@@ -1386,8 +1388,169 @@ describe('a change poll reads only the starts it can send (prod 10-29)', () => {
const whole = reads.filter(r => !r.distinct && (r.docsExamined ?? 0) > 0)
expect(whole.reduce((n, r) => n + (r.nreturned ?? 0), 0)).toBeLessThanOrEqual(5)
for (const r of whole) expect(r.docsExamined, JSON.stringify(r)).toBeLessThanOrEqual((r.nreturned ?? 0) + 1)
- // Every live start's conversation id: one distinct, which sends ids, never starts.
- expect(reads.filter(r => r.distinct)).toHaveLength(1)
+ // No read over every live start, not even of their conversation ids (DB 2.3).
+ expect(reads.filter(r => r.distinct)).toEqual([])
+ })
+
+ describe('a change poll reads only what its reply needs (DB 2.3)', () => {
+ /** After the migration (which stores derived decisions), the agent has none, as a loss after it leaves it. */
+ async function withoutDecision(tabId: string, agentId: string) {
+ await swarmTransaction(db, async tx => {
+ const agent = await tx.swarmAgent.findUniqueOrThrow({ where: { id: `${tabId}:${agentId}` } })
+ const { workDecision: _gone, ...data } = agent.data as { workDecision?: string }
+ await guardedWrite(tx, tabId, { op: 'update', kind: 'agent', key: agentId, seen: agent.version, data: data as never }, Date.now())
+ })
+ }
+
+ it('reads an agent that is only a member of running work as awaiting its decision, as a full read does', async () => {
+ // X finished work alone in an active conversation with no start, and is a member (not the worker) of running work.
+ const s = await scene((f, ids) => {
+ const alone = f.group('Finished alone')
+ const member = f.add('member-harness', alone, false, ownerActor, 'host', owner.sub)
+ f.run({ type: 'work.assign', conversationId: alone, agentId: member })
+ f.edit(state => {
+ const group = state.conversations.find(c => c.id === alone)!
+ group.assignment = { ...group.assignment!, status: 'done', result: 'Shipped' }
+ state.conversations.find(c => c.id === ids.web)!.agentIds.push(member)
+ })
+ return { alone, member }
+ })
+ await withoutDecision(s.tabId, s.more.member)
+ clock = Date.now()
+ const cursor = (await get(`/api/super-tabs/${s.tabId}/parts`, owner)).body.data.cursor
+ await writeGroup(s.tabId, s.web, Date.now(), g => { g.title = 'Web, renamed' })
+ clock = Date.now()
+ const changes = (await get(`/api/super-tabs/${s.tabId}/changes?cursor=${cursor}`, owner)).body.data
+ const full = (await get(`/api/super-tabs/${s.tabId}/parts`, owner)).body.data
+ expect(keys(full.groups)).toContain(s.more.alone)
+ expect(full.derived.agents[s.more.member].workDecision).toBe('awaiting')
+ expect(changes.derived.agents[s.more.member].workDecision).toBe('awaiting')
+ })
+
+ /** A reply's derived values without what differs by design: a first load sends every read mark (and avatar link), a
+ * change read only the marks moved since its cursor. */
+ const derivedOf = ({ readMarks: _marks, threadMarks: _threads, avatars: _avatars, ...rest }: Record<string, unknown>) => rest
+ const tagged = (tabId: string, conversationId: string, personId: string, resolved = false) => db.superTabMention.create({ data: {
+ id: `${tabId}-${conversationId}-${personId}`, tabId, conversationId, messageId: `${conversationId}-msg`, personId, projectId: 'lead',
+ authorAgentId: 'lead-agent', createdAt: new Date(), ...(resolved ? { resolvedAt: new Date(), resolvedBy: 'reply' } : {}) } })
+ /** The agent's part as prod still holds some (6 of 66 live agents, 10-30): no stored project (projects.ts agentProjectId). */
+ async function withoutProject(tabId: string, agentId: string) {
+ await swarmTransaction(db, async tx => {
+ const agent = await tx.swarmAgent.findUniqueOrThrow({ where: { id: `${tabId}:${agentId}` } })
+ const { projectId: _stored, ...data } = agent.data as { projectId?: string }
+ await guardedWrite(tx, tabId, { op: 'update', kind: 'agent', key: agentId, seen: agent.version, data: data as never }, Date.now())
+ })
+ }
+
+ it('gives every reader the derived values a full read gives', async () => {
+ const s = await scene((f, ids) => {
+ f.contribute('host', 'swarm')
+ const oldStarts = f.group('Old starts'), liveStarts = f.group('Starts'), quietSecret = f.group('Quiet secret starts', ids.secret)
+ const secretTalk = f.group('Secret talk', ids.secret), alone = f.group('Finished alone')
+ // Stored with no project below: it takes the private project from the conversation it is in.
+ const legacy = f.add('legacy-harness', secretTalk, false, ownerActor, 'host', owner.sub)
+ // No stored decision below: finished work alone, and a member (not the worker) of running work.
+ const member = f.add('member-harness', alone, false, ownerActor, 'host', owner.sub)
+ f.run({ type: 'work.assign', conversationId: alone, agentId: member })
+ // peer's Buddy, in the project chat; read here by the machine's owner.
+ const buddy = f.add('buddy-harness', 'lead', false, ownerActor, 'host', owner.sub)
+ f.edit(state => {
+ const group = state.conversations.find(c => c.id === alone)!
+ group.assignment = { ...group.assignment!, status: 'done', result: 'Shipped' }
+ state.conversations.find(c => c.id === ids.web)!.agentIds.push(member)
+ const peerGeneration = state.people.find(p => p.id === peer.sub)!.generation
+ state.agents.find(a => a.id === buddy)!.buddyOf = { personId: peer.sub, generation: peerGeneration }
+ const generation = state.people.find(p => p.id === owner.sub)!.generation
+ const pool = state.machines!.find(m => m.machineId === 'host')!
+ pool.observation = { generation: pool.generation, receivedAt: MIGRATED, sessionKind: 'computer',
+ resources: { cpuPercent: null, memoryTotalBytes: null, memoryUsedBytes: null, diskFreeBytes: null, diskTotalBytes: null, projectDiskFreeBytes: null,
+ deviceType: null, chip: null, os: null, gpuPercent: null }, tools: {}, quotas: [],
+ runningHarnessIds: ['worker-harness'], stoppedHarnessIds: ['listed-harness', 'quiet-secret-harness'] } as never
+ // Finished starts a machine lists: one in an active conversation no light base holds, one in a private conversation
+ // that goes quiet. In a public one that goes quiet: one that changes after the cursor, and one removed.
+ state.starts = [startOf('listed', liveStarts, 'listed-harness', generation), startOf('quiet-secret', quietSecret, 'quiet-secret-harness', generation),
+ startOf('changing', oldStarts, 'changing-harness', generation), startOf('old', oldStarts, 'old-harness', generation)] as never
+ })
+ return { oldStarts, quietSecret, secretTalk, legacy, member, buddy, alone }
+ })
+ const { tabId, more } = s
+ for (const key of [more.oldStarts, more.quietSecret]) await writeGroup(tabId, key, Date.now(), g => { g.dormant = true })
+ await withoutProject(tabId, more.legacy)
+ await withoutDecision(tabId, more.member)
+ // Waiting tags in an active and a quiet conversation; peer's in the private project, which peer cannot read; one answered.
+ await tagged(tabId, s.web, owner.sub)
+ await tagged(tabId, s.done, owner.sub)
+ await tagged(tabId, more.secretTalk, owner.sub, true)
+ await tagged(tabId, s.done, peer.sub)
+ await tagged(tabId, more.secretTalk, peer.sub)
+
+ const readers = [['owner', owner, undefined], ['peer', peer, undefined], ['buddy', owner, more.buddy], ['worker', owner, s.worker],
+ ['legacy agent', owner, more.legacy]] as const
+ const query = (agentId?: string) => agentId ? `asAgentId=${agentId}` : ''
+ clock = Date.now()
+ const cursors = new Map<string, string>()
+ for (const [name, reader, agentId] of readers) {
+ const first = await get(`/api/super-tabs/${tabId}/parts?${query(agentId)}`, reader)
+ expect(first.status, name).toBe(200)
+ cursors.set(name, first.body.data.cursor)
+ }
+
+ // After the cursor: a post, a start changed and one removed in the quiet conversation, a machine report, a progress
+ // part, and both people's read marks moved in a quiet conversation.
+ const at = Date.now()
+ await writeGroup(tabId, s.web, at, g => { g.title = 'Web, renamed' })
+ await swarmTransaction(db, async tx => {
+ const start = (key: string) => tx.swarmStart.findUniqueOrThrow({ where: { id: `${tabId}:${key}` } })
+ const changing = await start('changing'), old = await start('old')
+ await guardedWrite(tx, tabId, { op: 'update', kind: 'start', key: 'changing', seen: changing.version,
+ data: { ...(changing.data as object), error: 'Stopped by its host' } as never }, at)
+ await guardedWrite(tx, tabId, { op: 'remove', kind: 'start', key: 'old', seen: old.version, data: old.data as never }, at)
+ const machine = await tx.swarmMachine.findUniqueOrThrow({ where: { id: `${tabId}:host` } })
+ const data = structuredClone(machine.data) as { contribution: { observation: { receivedAt: number } } }
+ data.contribution.observation.receivedAt = at
+ await guardedWrite(tx, tabId, { op: 'update', kind: 'machine', key: 'host', seen: machine.version, data: data as never }, at)
+ const progress = { since: at, summarizedFloor: 0, summarized: {}, requestedFloor: 0, requested: {} }
+ const held = await tx.swarmProgress.findUnique({ where: { id: `${tabId}:lead` } })
+ await guardedWrite(tx, tabId, held ? { op: 'update', kind: 'progress', key: 'lead', seen: held.version, data: progress }
+ : { op: 'insert', kind: 'progress', key: 'lead', data: progress, ordinal: 1 }, at)
+ })
+ for (const personId of [owner.sub, peer.sub]) await writeReadMark(db, { tabId, personId, conversationId: s.done, sequence: 5, now: at })
+
+ clock = Date.now()
+ for (const [name, reader, agentId] of readers) {
+ const changes = (await get(`/api/super-tabs/${tabId}/changes?cursor=${cursors.get(name)}&${query(agentId)}`, reader)).body.data
+ const full = (await get(`/api/super-tabs/${tabId}/parts?${query(agentId)}`, reader)).body.data
+ expect(changes.reload, name).toBe(false)
+ expect(derivedOf(changes.derived), name).toEqual(derivedOf(full.derived))
+ const sent = new Set(changes.starts.map((p: { key: string }) => p.key))
+ expect(changes.starts.filter((p: { deleted?: boolean }) => !p.deleted), name).toEqual(full.starts.filter((p: { key: string }) => sent.has(p.key)))
+ expect(changes.machines, name).toEqual(full.machines.filter((p: { key: string }) => p.key === 'host'))
+ // A quiet conversation is in no first load, so neither its starts nor their harnesses are, the private one's included.
+ expect(JSON.stringify(changes), name).not.toMatch(/quiet-secret|changing-harness/)
+ expect(changes.progress, name).toEqual(full.progress.filter((p: { key: string }) => p.key === 'lead'))
+ if (name === 'owner') expect(changes.derived.mentionsWaiting).toBe(2)
+ // peer's tag in the private project is not counted, though its conversation was read to tell.
+ if (name === 'peer') expect(changes.derived.mentionsWaiting).toBe(1)
+ // Its project is the private one, read from its conversation: only the owner and the agent itself see it.
+ expect(Object.keys(changes.derived.agents).includes(more.legacy), name).toBe(name === 'owner' || name === 'legacy agent')
+ if (name === 'owner') expect(changes.derived.agents[more.member].workDecision).toBe('awaiting')
+ if (name === 'buddy') expect(changes.starts.length + changes.machines.length).toBe(0)
+ }
+ })
+
+ it('names only the conversations of a person\'s waiting tags', async () => {
+ const tabId = `tab-${randomUUID()}`
+ clock = Date.now()
+ const tag = (conversationId: string, at: number, resolved = false, personId = owner.sub) => db.superTabMention.create({ data: {
+ id: `${tabId}-${conversationId}-${personId}`, tabId, conversationId, messageId: `${conversationId}-msg`, personId, projectId: 'lead',
+ authorAgentId: 'lead-agent', createdAt: new Date(at), ...(resolved ? { resolvedAt: new Date(at + 1), resolvedBy: 'reply' } : {}) } })
+ await tag('waiting', clock - 60_000)
+ await tag('answered', clock - 60_000, true)
+ await tag('expired', clock - 31 * DAY)
+ await tag('someone-else', clock - 60_000, false, peer.sub)
+ await tag('waiting-too', clock - 1_000)
+ expect(await waitingTagConversations(db, tabId, owner.sub, clock)).toEqual(['waiting-too', 'waiting'])
+ })
})
})
})
diff --git a/backend/src/superTabs/cache.test.ts b/backend/src/superTabs/cache.test.ts
index 1544f15a1..58ec3f03f 100644
--- a/backend/src/superTabs/cache.test.ts
+++ b/backend/src/superTabs/cache.test.ts
@@ -122,6 +122,53 @@ describe('SwarmCache', () => {
expect(cache.size).toEqual({ entries: 0, bytes: 0 })
})
+ it('drops only the bumped Swarm\'s entries, keeping counts right through drops, evictions and expiry', async () => {
+ let now = 1_000
+ const stamps = new MemoryStamps(), cache = new SwarmCache(stamps, { maxBytes: 10_000, maxAgeMs: 60_000, now: () => now })
+ const loads = new Map<string, number>()
+ // Loads of each entry so far. A 1 KB value is 1,012 bytes as the cache counts it, 'big' 8,012.
+ const get = async (tab: string, name: string, scope: 'main' | 'all' = 'main') => {
+ await (await cache.at([tab])).get(tab, name, scope, async () => {
+ loads.set(`${tab}/${name}`, (loads.get(`${tab}/${name}`) ?? 0) + 1)
+ return { text: 'x'.repeat(name === 'big' ? 8_000 : 1_000) }
+ })
+ return loads.get(`${tab}/${name}`)
+ }
+ for (const tab of ['a', 'b']) { await get(tab, 'one'); await get(tab, 'two'); await get(tab, 'machines', 'all') }
+ expect(cache.size).toEqual({ entries: 6, bytes: 6 * 1_012 })
+
+ await stamps.bump('a', ['main'])
+ expect(cache.size).toEqual({ entries: 3, bytes: 3 * 1_012 })
+ expect([await get('b', 'one'), await get('b', 'two'), await get('b', 'machines', 'all')]).toEqual([1, 1, 1])
+
+ // A machine's reading keeps b's main-scoped entries.
+ await stamps.bump('b', ['machine'])
+ expect(cache.size).toEqual({ entries: 2, bytes: 2 * 1_012 })
+
+ // Over the budget: b's least recently used entry goes, the other stays held.
+ await get('c', 'big')
+ expect(cache.size).toEqual({ entries: 2, bytes: 1_012 + 8_012 })
+ expect(await get('b', 'two')).toBe(1)
+ // So does a machine's reading after the eviction.
+ await stamps.bump('b', ['machine'])
+ expect(cache.size).toEqual({ entries: 2, bytes: 1_012 + 8_012 })
+
+ // Past the maximum age: a read forgets the entry and loads it again.
+ now += 60_000
+ expect(await get('b', 'two')).toBe(2)
+ expect(cache.size).toEqual({ entries: 2, bytes: 1_012 + 8_012 })
+
+ // Whatever was evicted or expired before, a bump drops exactly the Swarm's entries still held.
+ await stamps.bump('b', ['main'])
+ expect(cache.size).toEqual({ entries: 1, bytes: 8_012 })
+ await stamps.bump('a', ['main'])
+ expect(cache.size).toEqual({ entries: 1, bytes: 8_012 })
+ await stamps.bump('c', ['main'])
+ expect(cache.size).toEqual({ entries: 0, bytes: 0 })
+ expect(await get('b', 'one')).toBe(2)
+ expect(cache.size).toEqual({ entries: 1, bytes: 1_012 })
+ })
+
it('reads the stamps of several Swarms at once', async () => {
const stamps = new MemoryStamps(), cache = new SwarmCache(stamps)
const one = counted(n => ({ n })), two = counted(n => ({ n }))
diff --git a/backend/src/superTabs/cache.ts b/backend/src/superTabs/cache.ts
index 77ef82be1..0bf9f75da 100644
--- a/backend/src/superTabs/cache.ts
+++ b/backend/src/superTabs/cache.ts
@@ -24,7 +24,7 @@ export interface SwarmStamps {
subscribe(listener: (tabId: string, scopes: readonly StampScope[]) => void): () => void
}
-interface Entry { stamp: Stamp; scope: StampScope | 'all'; at: number; bytes: number; value: unknown }
+interface Entry { tabId: string; stamp: Stamp; scope: StampScope | 'all'; at: number; bytes: number; value: unknown }
export interface CacheOptions {
/** What the held values may take, counted as their JSON size. */
@@ -62,6 +62,8 @@ export function freeze(value: unknown): number {
export class SwarmCache {
private readonly entries = new Map<string, Entry>()
+ /** Each Swarm's entry names, so a bump touches only that Swarm's entries, not every Swarm's. */
+ private readonly bySwarm = new Map<string, Set<string>>()
private readonly loading = new Map<string, Promise<unknown>>()
private bytes = 0
private readonly maxBytes: number
@@ -94,11 +96,9 @@ export class SwarmCache {
/** Forgets what this process holds of a Swarm that these stamps' moves make stale; everything, by default. */
drop(tabId: string, scopes: readonly StampScope[] = ['main']): void {
- const prefix = key(tabId, ''), main = scopes.includes('main')
- for (const [name, entry] of this.entries) {
- if (!name.startsWith(prefix) || !main && entry.scope === 'main') continue
- this.entries.delete(name)
- this.bytes -= entry.bytes
+ const main = scopes.includes('main')
+ for (const name of this.bySwarm.get(tabId) ?? []) {
+ if (main || this.entries.get(name)!.scope !== 'main') this.drop1(name)
}
}
@@ -108,8 +108,7 @@ export class SwarmCache {
if (!entry) return undefined
const fresh = entry.stamp.main === stamp.main && (entry.scope === 'main' || entry.stamp.machine === stamp.machine) && this.now() - entry.at < this.maxAgeMs
if (!fresh) {
- this.entries.delete(key(tabId, name))
- this.bytes -= entry.bytes
+ this.drop1(key(tabId, name))
return undefined
}
// Most recently used last.
@@ -128,12 +127,13 @@ export class SwarmCache {
const bytes = freeze(value)
if (bytes <= this.maxBytes) {
this.drop1(key(tabId, name))
- this.entries.set(key(tabId, name), { stamp, scope, at, bytes, value })
+ this.entries.set(key(tabId, name), { tabId, stamp, scope, at, bytes, value })
this.bytes += bytes
- for (const [oldest, entry] of this.entries) {
+ const names = this.bySwarm.get(tabId) ?? new Set<string>()
+ this.bySwarm.set(tabId, names.add(key(tabId, name)))
+ for (const oldest of this.entries.keys()) {
if (this.bytes <= this.maxBytes) break
- this.entries.delete(oldest)
- this.bytes -= entry.bytes
+ this.drop1(oldest)
}
}
return value
@@ -147,6 +147,9 @@ export class SwarmCache {
if (!entry) return
this.entries.delete(name)
this.bytes -= entry.bytes
+ const names = this.bySwarm.get(entry.tabId)!
+ names.delete(name)
+ if (!names.size) this.bySwarm.delete(entry.tabId)
}
}
diff --git a/backend/src/superTabs/cachedReads.ts b/backend/src/superTabs/cachedReads.ts
index de70bd71b..3fc81e1ef 100644
--- a/backend/src/superTabs/cachedReads.ts
+++ b/backend/src/superTabs/cachedReads.ts
@@ -61,7 +61,7 @@ export async function readParts(db: Db, view: CacheView, tabId: string, named: R
/** The parts loadLight gives for `opts`, held whole by what it selects. */
export async function readLight(db: Db, view: CacheView, tabId: string, opts: Omit<LightOptions, 'machines'>): Promise<SwarmParts> {
- const name = `light:${JSON.stringify([opts.groups ?? 'active', !!opts.includeRecentStarts, !!opts.recentStartGroups, ...(['groupIds', 'agentIds', 'projectIds'] as const)
+ const name = `light:${JSON.stringify([opts.groups ?? 'active', !!opts.includeRecentStarts, ...(['groupIds', 'agentIds', 'projectIds'] as const)
.map(key => [...opts[key] ?? []].sort())])}`
const [light, machines] = await Promise.all([
view.get(tabId, name, 'main', () => retryTransient(() => retryStaleIndex(() => db.$transaction(tx => store.loadLight(tx, tabId, { ...opts, machines: false }))))),
@@ -70,6 +70,22 @@ export async function readLight(db: Db, view: CacheView, tabId: string, opts: Om
return { ...light, machines }
}
+/** Light conversations by id (parts/light.ts loadLightGroups), dormant ones too, in the order named: each held at the
+ * Swarm's main stamp, an id no live conversation has held as none. A read's misses are one query, as readLastChangedAts
+ * batches them; without stamps every id is read, still in one query. */
+export async function readLightGroups(db: Db, view: CacheView, tabId: string, ids: readonly string[]): Promise<Part<GroupData>[]> {
+ const missing: string[] = []
+ let batch: Promise<Map<string, Part<GroupData>>> | undefined
+ const groups = await Promise.all([...new Set(ids)].map(id => view.get(tabId, `light-group:${id}`, 'main', () => {
+ missing.push(id)
+ // As readLastChangedAts: every miss of this read joins before the query starts.
+ batch ??= Promise.resolve().then(async () => new Map((await retryStaleIndex(() => store.loadLightGroups(db, tabId, 'key', { key: { $in: missing } })))
+ .map(group => [group.key, group])))
+ return batch.then(found => found.get(id) ?? null)
+ })))
+ return groups.filter((group): group is Part<GroupData> => group !== null)
+}
+
/** The Swarm's newest data change (parts/load.ts lastChangedAt), held at its main stamp: a machine's reading leaves it. */
export async function readLastChangedAt(db: Db, view: CacheView, tabId: string): Promise<number | null> {
return view.get(tabId, 'lastChangedAt', 'main', async () => (await store.lastChangedAt(db, [tabId])).get(tabId) ?? null)
diff --git a/backend/src/superTabs/mentions.ts b/backend/src/superTabs/mentions.ts
index c0e170d98..b6e12a4a2 100644
--- a/backend/src/superTabs/mentions.ts
+++ b/backend/src/superTabs/mentions.ts
@@ -195,14 +195,26 @@ export async function markDone(tx: Tx, tabId: string, id: string, personId: stri
await tx.superTabMention.updateMany({ where: { id, tabId, personId, ...waiting }, data: { resolvedAt: new Date(now), resolvedBy: 'done' } })
}
+/** A person's unexpired tags in a Swarm, waiting or answered, newest first: the rows [readable] lists and counts. */
+const tagRows = (tabId: string, personId: string, now: number, open: boolean) => ({
+ where: { tabId, personId, createdAt: { gt: new Date(now - MENTION_EXPIRY_MS) }, ...(open ? waiting : { resolvedAt: { not: null } }) },
+ orderBy: { createdAt: 'desc' as const }, take: 500,
+})
+
+/** The conversations of a person's waiting tags, newest tag first, once each: those [waitingMentions] may read beyond what a
+ * read holds, so a change poll can hold them per conversation (read/service.ts). */
+export async function waitingTagConversations(tx: Tx, tabId: string, personId: string, now: number): Promise<string[]> {
+ const rows = await tx.superTabMention.findMany({ ...tagRows(tabId, personId, now, true), select: { conversationId: true } })
+ return [...new Set(rows.map(row => row.conversationId))]
+}
+
/** The person's own unexpired tags in conversations they can still read, newest first, each with its conversation. The badge
* counts this same list. Ordinary loads leave quiet (dormant) conversations out of the state, so the ones a tag is in are
* read here: the count and the list check the same conversations whichever state they get.
* ponytail: capped at 500 per state; page it if a person ever gets more in 30 days. */
async function readable(tx: Tx, tabId: string, state: SuperTabState, actor: Extract<Actor, { kind: 'person' }>, now: number, open: boolean) {
person(state, actor.userId)
- const rows = await tx.superTabMention.findMany({ where: { tabId, personId: actor.userId, createdAt: { gt: new Date(now - MENTION_EXPIRY_MS) },
- ...(open ? waiting : { resolvedAt: { not: null } }) }, orderBy: { createdAt: 'desc' }, take: 500 })
+ const rows = await tx.superTabMention.findMany(tagRows(tabId, actor.userId, now, open))
const groups = new Map(state.conversations.map(c => [c.id, c]))
const missing = [...new Set(rows.map(r => r.conversationId))].filter(id => !groups.has(id))
if (missing.length) {
diff --git a/backend/src/superTabs/parts/light.ts b/backend/src/superTabs/parts/light.ts
index 5c21e69b5..3bee331dd 100644
--- a/backend/src/superTabs/parts/light.ts
+++ b/backend/src/superTabs/parts/light.ts
@@ -6,7 +6,7 @@
* and host rows; it is never sent to a client as a conversation.
*/
import type { Prisma } from '@prisma/client'
-import { activeWork, type Agent, type AgentStart } from '../model.js'
+import { activeWork, agentRole, type Agent, type AgentStart } from '../model.js'
import type { GroupData, HeadData, Part, PartKind, SwarmParts } from './types.js'
import { delegate, partId, type PartRow } from './write.js'
import { groupsBy, type GroupIndex } from './indexed.js'
@@ -57,13 +57,14 @@ function lightGroup(row: Record<string, unknown>): Part<GroupData> {
}
/**
- * A worker with no stored decision and no running work is judged from its finished conversations (model.ts
- * awaitingWorkers): a lost task leaves none stored (liveness.ts). The 'working' selection then also needs every active
- * conversation such a worker is in, works in or leads, as a whole first load sees them (review of #1745).
+ * A worker with no stored decision is judged from its finished conversations (model.ts awaitingWorkers): a lost task leaves
+ * none stored (liveness.ts). The 'working' selection then also needs every active conversation such a worker is in, works
+ * in or leads, as a whole first load sees them (review of #1745). Busy as awaitingWorkers tells it: a Reviewer, the Lead of
+ * a conversation, or the worker of active work; a member of someone else's running work is not.
*/
async function undecidedWork(agents: Part<Agent>[], groups: Part<GroupData>[], lightGroups: (index: GroupIndex, match: Record<string, unknown>) => Promise<Part<GroupData>[]>) {
- const busy = new Set(groups.flatMap(g => [...g.data!.leadId ? [g.data!.leadId] : [], ...activeWork(g.data!) ? [g.data!.assignment!.agentId, ...g.data!.agentIds] : []]))
- const undecided = agents.filter(a => !a.data!.workDecision && !busy.has(a.key)).map(a => a.key)
+ const busy = new Set(groups.flatMap(g => [...g.data!.leadId ? [g.data!.leadId] : [], ...activeWork(g.data!) ? [g.data!.assignment!.agentId] : []]))
+ const undecided = agents.filter(a => !a.data!.workDecision && agentRole(a.data!) !== 'reviewer' && !busy.has(a.key)).map(a => a.key)
if (!undecided.length) return []
return lightGroups('dormancy', { dormant: false, key: { $nin: groups.map(g => g.key) },
$or: [{ 'data.assignment.agentId': { $in: undecided } }, { 'data.leadId': { $in: undecided } }, { agentIds: { $in: undecided } }] })
@@ -109,32 +110,27 @@ export async function loadLightGroups(tx: Transaction, tabId: string, index: Gro
}
/**
- * The live parts of one Swarm with conversations light. Conversations: every active one (`groups: 'active'`, as
+ * The live parts of one Swarm with conversations light, and no progress parts: a change poll sends those from the change
+ * feed (read/changes.ts), and nothing derived reads them. Conversations: every active one (`groups: 'active'`, as
* `loadActive`), or only the Lead conversations and those with running work (`'working'`: what visibility and derived
- * values read once every finished worker's decision is stored); plus those named by id and those the named agents are in,
- * dormant ones too, and the active ones of the named projects. Starts: unfinished, or every live start with `includeRecentStarts`.
- * With `recentStartGroups`, only unfinished starts are read whole, but the conversations of every live start are loaded as with
- * `includeRecentStarts`: a change poll sends only the starts that changed (read/service.ts), and the conversations it holds
- * decide what each reader may see (an agent stored with no project takes its project from them: projects.ts agentProjectId).
+ * values read once every finished worker's decision is stored), with the active ones of agents stored with no project (they
+ * take theirs from the first one they are in: projects.ts agentProjectId); plus those named by id and those the named agents
+ * are in, dormant ones too, and the active ones of the named projects. Starts: unfinished, or every live start with
+ * `includeRecentStarts`.
*/
export async function loadLight(tx: Transaction, tabId: string,
- opts: { groupIds?: string[]; agentIds?: string[]; projectIds?: string[]; includeRecentStarts?: boolean; recentStartGroups?: boolean; groups?: 'active' | 'working'
+ opts: { groupIds?: string[]; agentIds?: string[]; projectIds?: string[]; includeRecentStarts?: boolean; groups?: 'active' | 'working'
/** False leaves machines out: the read cache keeps them at their own stamp (cache.ts). */
machines?: boolean } = {}): Promise<SwarmParts> {
const head = await tx.swarmHead.findUnique({ where: { id: partId(tabId, 'head') } })
if (!head) throw Object.assign(new Error(`Swarm ${tabId} has no parts`), { code: 'PARTS_MISSING' })
const live = { tabId, deleted: false }
const starts = (await delegate(tx, 'start').findMany({ where: opts.includeRecentStarts ? live : { ...live, finishedAt: null } })).map(row => toPart<AgentStart>('start', row))
- // Each live start's conversation id alone (a busy Swarm's week of finished starts was ~1 MB whole each poll, prod 10-29).
- const startGroups = opts.recentStartGroups && !opts.includeRecentStarts
- ? ((await tx.$runCommandRaw({ distinct: 'SwarmStart', key: 'conversationId', query: live })) as unknown as { values: Array<string | null> }).values
- .filter((id): id is string => typeof id === 'string')
- : starts.map(s => s.data!.request.conversationId)
const { projects, leadsConversationId, invitations } = head.data as unknown as HeadData
// Besides the Lead conversations and running work, the conversations a start or an invitation names: a reader sees
// those only alongside their conversation (model.ts visibleState).
const working = [...projects ? projects.map(p => p.leadConversationId) : ['lead'], leadsConversationId ?? '',
- ...startGroups, ...invitations.map(i => i.conversationId)]
+ ...starts.map(s => s.data!.request.conversationId), ...invitations.map(i => i.conversationId)]
// One read per index, never one $or over several: Mongo plans an $or again whenever its plan cache misses (parts/indexed.ts).
const keys = [...opts.groups === 'working' ? working : [], ...opts.groupIds ?? []]
// The Swarm's first project also holds conversations stored with no project (model.ts groupProject).
@@ -146,13 +142,19 @@ export async function loadLight(tx: Transaction, tabId: string,
const lightGroups = async (index: GroupIndex, match: Record<string, unknown>) => (await groupsBy(tx, index, [{ $match: { tabId, deleted: false, ...match } },
{ $project: LIGHT_GROUP }])).map(lightGroup)
const unique = (lists: Part<GroupData>[][]) => [...new Map(lists.flat().map(group => [group.key, group])).values()]
- const [members, agents, machines, progress, groups] = await Promise.all([
+ const [members, agents, machines, groups] = await Promise.all([
...(['member', 'agent', 'machine'] as const).map(kind => kind === 'machine' && opts.machines === false ? Promise.resolve([])
: delegate(tx, kind).findMany({ where: live }).then(rows => rows.map(row => toPart(kind, row)))),
- delegate(tx, 'progress').findMany({ where: live }).then(rows => rows.map(row => toPart('progress', row))),
Promise.all(named.map(([index, match]) => lightGroups(index, match))).then(unique),
])
- if (opts.groups === 'working') groups.push(...await undecidedWork(agents as Part<unknown>[] as Part<Agent>[], groups as Part<GroupData>[], lightGroups))
- return { tabId, head: toPart<HeadData>('head', head as unknown as PartRow), members, agents, groups, machines, starts, progress,
+ if (opts.groups === 'working') {
+ const workers = agents as Part<unknown>[] as Part<Agent>[]
+ const legacy = workers.filter(a => !a.data!.projectId).map(a => a.key)
+ const [undecided, ofLegacy] = await Promise.all([undecidedWork(workers, groups as Part<GroupData>[], lightGroups),
+ legacy.length ? lightGroups('agents', { agentIds: { $in: legacy }, dormant: false }) : Promise.resolve([])])
+ const held = new Set(groups.map(g => g.key))
+ groups.push(...unique([undecided, ofLegacy]).filter(g => !held.has(g.key)))
+ }
+ return { tabId, head: toPart<HeadData>('head', head as unknown as PartRow), members, agents, groups, machines, starts, progress: [],
sequenceBase: head.sequenceBase, visibilityEpoch: head.visibilityEpoch, groupCounter: head.groupCounter } as SwarmParts
}
diff --git a/backend/src/superTabs/read/service.ts b/backend/src/superTabs/read/service.ts
index 93c078598..89b65f2ca 100644
--- a/backend/src/superTabs/read/service.ts
+++ b/backend/src/superTabs/read/service.ts
@@ -6,7 +6,7 @@ import type { PrismaClient } from '@prisma/client'
import type { AuthUser } from '../../lib/ssoAuth.js'
import { isTeamSession, TEAM_SESSION_LIMITED, TEAM_SESSION_MESSAGE } from '../../lib/teamSession.js'
import { agentOnly, author, fail, groupWithoutMachines, isLeads, seenMachineIds, stableId, SuperTabError, visibleState, type Actor, type AgentStart, type SuperTabState } from '../model.js'
-import { mentionsDigest, swarmsWithTags, waitingMentions } from '../mentions.js'
+import { mentionsDigest, swarmsWithTags, waitingMentions, waitingTagConversations } from '../mentions.js'
import { decisionsWaiting } from '../decisions.js'
import { readMarks, readThreadMarks, writeReadMark, writeThreadMark } from '../readMarks.js'
import { deliveredCopies } from '../pageReads.js'
@@ -14,7 +14,7 @@ import { readBy } from '../parts/indexed.js'
import type { SuperTabStore } from '../store.js'
import { transaction } from '../store.js'
import type { CacheView } from '../cache.js'
-import { readChanged, readLastChangedAt, readLight, readParts } from '../cachedReads.js'
+import { readChanged, readLastChangedAt, readLight, readLightGroups, readParts } from '../cachedReads.js'
import { migrateSwarm } from '../migration/migrate.js'
import * as parts from '../parts/index.js'
import type { GroupData, Part, SwarmParts } from '../parts/index.js'
@@ -34,7 +34,7 @@ import { envelope, groupView, readerView, seenIn, type ReaderView } from './view
import { decodeDormantPage, encodeDormantPage } from './dormantPage.js'
import { memberSwarms } from '../swarmRows.js'
-type LoadOptions = NonNullable<Parameters<typeof parts.loadActive>[2]> & Pick<NonNullable<Parameters<typeof parts.loadLight>[2]>, 'groups' | 'recentStartGroups'>
+type LoadOptions = NonNullable<Parameters<typeof parts.loadActive>[2]>
type Loader = (view: CacheView, tabId: string, opts: LoadOptions) => Promise<SwarmParts>
interface Read { parts: SwarmParts; actor: Actor; view: ReaderView; lastChangedAt: number | null }
/** How long one Swarm's machine lookups below are reused: machines report every 15 s anyway (Diego's review, item 5, 2026-10-28). */
@@ -57,6 +57,16 @@ function readableMarks(read: Read, marks: Map<string, number>, threads: Map<stri
...(Object.keys(threadMarks).length ? { threadMarks } : {}) }
}
+/** A change poll's light base (parts/light.ts): the same for every reader of a Swarm, so one load per stamp serves them all. */
+const LIGHT = { groups: 'working' } as const
+
+/** `loaded` with these conversations too, never one it holds already. */
+const withGroups = (loaded: SwarmParts, extra: Part<GroupData>[]): SwarmParts => {
+ if (!extra.length) return loaded
+ const held = new Set(loaded.groups.map(g => g.key))
+ return { ...loaded, groups: [...loaded.groups, ...extra.filter(g => !held.has(g.key))] }
+}
+
/** `loaded` with these starts too: a change poll's own (SwarmReads.pollStarts), never one it holds already. */
const withStarts = (loaded: SwarmParts, extra: Part<AgentStart>[]): SwarmParts => {
if (!extra.length) return loaded
@@ -124,30 +134,30 @@ export class SwarmReads {
const t0 = this.now()
return this.read(user, tabId, async (tx, cached) => {
// Light (parts/light.ts): changed parts come whole from the change feed; the rest is read only to check visibility and
- // derive values: the Lead conversations, those with running work, and those a person's mentions are in (their count).
- // Open decisions count their conversations themselves (decisions.ts).
- const mentioned = asAgentId ? [] : (await tx.superTabMention.findMany({ where: { tabId, personId: user.sub }, select: { conversationId: true },
- distinct: ['conversationId'] })).map(m => m.conversationId)
- // Starts: only unfinished ones whole, then the poll's own (below). Every live start's conversation is still held, so
- // what each reader may see is decided as before (parts/light.ts recentStartGroups).
- const light = { recentStartGroups: true, groups: 'working' as const, groupIds: mentioned }
- const base = await readLight(tx, cached, tabId, light)
+ // derive values. The base is one per Swarm and stamp, whoever reads: the Lead conversations, running work, unfinished
+ // starts and the like, and no progress parts (the feed sends changed ones). Open decisions count their conversations
+ // themselves (decisions.ts).
+ const base = await readLight(tx, cached, tabId, LIGHT)
const from = readCursor(cursor, base.visibilityEpoch, t0)
// Reloading readers still pass the access check first, as every read does.
- if ('reload' in from) { await this.load(tx, cached, user, tabId, asAgentId, t0, light, async () => base); return from }
- const changed = await readChanged(tx, cached, tabId, from.since)
+ if ('reload' in from) { await this.load(tx, cached, user, tabId, asAgentId, t0, {}, async () => base); return from }
+ // Without asAgentId the reader is a person (store.ts actor), whose marks moved since the cursor go out with this reply.
+ const person = asAgentId ? null : user.sub
+ const [changed, tagged, marks, threadMarks] = await Promise.all([readChanged(tx, cached, tabId, from.since),
+ person ? waitingTagConversations(tx, tabId, person, t0) : [],
+ person ? readMarks(tx, tabId, person, from.since) : new Map<string, number>(),
+ person ? readThreadMarks(tx, tabId, person, from.since) : new Map<string, Map<string, number>>()])
+ // Starts: only unfinished ones whole in the base, then the poll's own.
const extra = await this.pollStarts(tx, cached, tabId, base, changed)
- const lightly: Loader = async (view, id, opts) => withStarts(opts === light ? base : await readLight(tx, view, id, opts), extra)
- let read = await this.load(tx, cached, user, tabId, asAgentId, t0, light, lightly)
- // A changed start whose group went dormant is read against that group, so its readers alone learn of it.
- const held = new Set(read.parts.groups.map(g => g.key))
- // So is a conversation whose read mark moved, so the mark goes only to a reader who can still read it.
- const marks = await this.marksOf(tx, tabId, read.actor, from.since)
- const threadMarks = await this.threadMarksOf(tx, tabId, read.actor, from.since)
- const unheld = [...new Set([...changed.flatMap(p => p.kind === 'start' ? [(p.data as AgentStart).request.conversationId] : []), ...marks.keys(),
- ...threadMarks.keys()])]
- .filter(id => !held.has(id))
- if (unheld.length) read = await this.load(tx, cached, user, tabId, asAgentId, t0, { ...light, groupIds: [...mentioned, ...unheld] }, lightly)
+ // Each reader's own conversations, held one by one (cachedReads.ts readLightGroups): those of their waiting tags, which
+ // their count reads (mentions.ts), and those its starts are in, so a start the poll sends or a machine still lists is
+ // read against its conversation, quiet or not, and its readers alone learn of it. So is a conversation whose read mark
+ // moved, so the mark goes only to a reader who can still read it.
+ const held = new Set(base.groups.map(g => g.key))
+ const named = [...new Set([...tagged, ...[...changed.filter(p => p.kind === 'start'), ...extra].map(p => (p.data as AgentStart).request.conversationId),
+ ...marks.keys(), ...threadMarks.keys()])].filter(id => !held.has(id))
+ const read = await this.load(tx, cached, user, tabId, asAgentId, t0, {},
+ async () => withStarts(withGroups(base, await readLightGroups(tx, cached, tabId, named)), extra))
return { format: READ_FORMAT, reload: false, cursor: cursorFor(t0, read.parts.visibilityEpoch), serverTime: t0, lastChangedAt: read.lastChangedAt,
epoch: read.parts.visibilityEpoch, ...changesFor(read.view, changed, read.actor),
derived: { ...await this.withAvatars(read.view, t0), ...readableMarks(read, marks, threadMarks) } }