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): a change poll reads only the starts it can send (prod 10-29) (#2247)

0xken150508b688c613
diff --git a/backend/integration/superTabRead.test.ts b/backend/integration/superTabRead.test.ts
index 17110a6be..676859b72 100644
--- a/backend/integration/superTabRead.test.ts
+++ b/backend/integration/superTabRead.test.ts
@@ -23,6 +23,7 @@ import type { Conversation, SuperTabState } from '../src/superTabs/model.js'
import { sendsEmptyIn, watched, type Query } from '../src/lib/queryRecorder.test-support.js'
import { writeLegacySwarm } from '../scripts/swarm-parts/legacy.js'
import { NOW, outsider as outsiderActor, owner as ownerActor, peer as peerActor, swarm } from '../src/superTabs/read/fixture.test-support.js'
+import { PROFILE_STAGES, profileEntry, type ProfileEntry } from './bench/profile.js'
// A fresh loopback replica set, created here. This suite never reads DATABASE_URL or an existing database.
let replica: MongoMemoryReplSet
@@ -1240,4 +1241,153 @@ describe('what a read costs the database (group 3b)', () => {
const posted = (await db.swarmGroup.findFirst({ where: { tabId: s.tabId, key: s.web } }))!.changedAt.getTime()
expect(woken).toMatchObject([{ tabId: s.tabId, lastChangedAt: posted }])
})
+
+describe('a change poll reads only the starts it can send (prod 10-29)', () => {
+ /** A busy week's finished starts: prod's largest Swarm held 763 on 10-29 and adds about 300 a day, kept 7 days. */
+ const FINISHED = 1000
+ type StartRow = { id: string; harnessId: string | null }
+ const startOf = (id: string, conversationId: string, harnessId: string | null, generation: string) => ({ id, creationId: `c-${id}`,
+ actorKey: `person:${owner.sub}`, contributorId: owner.sub, membershipGeneration: generation, machineOwnerId: owner.sub, hostGrant: null,
+ harnessId, dispatched: !!harnessId, error: null,
+ request: { conversationId, machineId: 'host', engine: 'codex' as const, name: 'Builder', cwd: '/repo', worktree: false,
+ requirements: { memoryBytes: 0, diskBytes: 0, maxCpuPercent: 100, tools: [], devices: [], quotaRequired: false } } })
+
+ /** The host machine lists, as stopped, a harness of a finished start that never became an agent ("ghost"), one of a finished
+ * start in the owner's private project ("secret-old") and one nobody's ("stale"). Finished starts: FINISHED old ones, ghost,
+ * secret-old and one to be removed. Unfinished: "finishing" (it finishes after the cursor) and "queued". */
+ const startsScene = () => scene((f, ids) => {
+ f.contribute('host', 'swarm')
+ // Active conversations: a reader never sees the starts of a dormant one, in a full read or a poll.
+ const publicStarts = f.group('Starts'), secretStarts = f.group('Secret starts', ids.secret)
+ f.edit(state => {
+ 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: ['ghost-harness', 'secret-harness', 'stale-harness'] } as never
+ state.starts = [
+ ...Array.from({ length: FINISHED }, (_, i) => startOf(`old-${i}`, publicStarts, `old-harness-${i}`, generation)),
+ startOf('ghost', publicStarts, 'ghost-harness', generation),
+ startOf('secret-old', secretStarts, 'secret-harness', generation),
+ startOf('removed', secretStarts, 'removed-harness', generation),
+ startOf('finishing', publicStarts, null, generation),
+ startOf('queued', publicStarts, null, generation),
+ ] as never
+ })
+ return { publicStarts, secretStarts }
+ })
+
+ /** After the cursor: "finishing" gets its harness (finished), "removed" is removed, and the host reports again. */
+ async function changeAfterCursor(tabId: string) {
+ const at = Date.now()
+ await swarmTransaction(db, async tx => {
+ const row = (key: string) => tx.swarmStart.findUniqueOrThrow({ where: { id: `${tabId}:${key}` } })
+ const finishing = await row('finishing'), removed = await row('removed')
+ await guardedWrite(tx, tabId, { op: 'update', kind: 'start', key: 'finishing', seen: finishing.version,
+ data: { ...(finishing.data as object), harnessId: 'finishing-harness', dispatched: true } as never }, at)
+ await guardedWrite(tx, tabId, { op: 'remove', kind: 'start', key: 'removed', seen: removed.version, data: removed.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 stopped = (machines: Array<{ key: string; data: { observation?: { stoppedHarnessIds: string[]; runningHarnessIds: string[] } } }>) =>
+ machines.filter(m => m.key === 'host').map(m => ({ running: m.data.observation?.runningHarnessIds, stopped: m.data.observation?.stoppedHarnessIds }))
+
+ it('each reader gets exactly the starts and machine record a full read gives them', async () => {
+ const s = await startsScene()
+ clock = Date.now()
+ const cursors = { owner: (await get(`/api/super-tabs/${s.tabId}/parts`, owner)).body.data.cursor, peer: (await get(`/api/super-tabs/${s.tabId}/parts`, peer)).body.data.cursor }
+ await changeAfterCursor(s.tabId)
+ clock = Date.now()
+ for (const [name, reader] of [['owner', owner], ['peer', peer]] as const) {
+ const changes = (await get(`/api/super-tabs/${s.tabId}/changes?cursor=${cursors[name]}`, reader)).body.data
+ const full = (await get(`/api/super-tabs/${s.tabId}/parts`, reader)).body.data
+ // What the old full read sent: the changed start as a first load shows it, and the machine record as a first load shows it.
+ expect(changes.starts.filter((p: { deleted?: boolean }) => !p.deleted), name).toEqual(full.starts.filter((p: { key: string }) => p.key === 'finishing'))
+ expect(changes.machines.filter((m: { key: string }) => m.key === 'host'), name).toEqual(full.machines.filter((m: { key: string }) => m.key === 'host'))
+ if (name === 'owner') {
+ // The owner reads the private project: its removed start's stub, and its finished start's harness on the machine.
+ expect(changes.starts.map((p: { key: string; deleted?: boolean }) => [p.key, !!p.deleted]).sort()).toEqual([['finishing', false], ['removed', true]])
+ expect(stopped(changes.machines)).toEqual([{ running: ['worker-harness'], stopped: ['ghost-harness', 'secret-harness'] }])
+ } else {
+ // A collaborator outside it: no stub, no harness, nothing that names the private project's starts.
+ expect(changes.starts.map((p: { key: string }) => p.key)).toEqual(['finishing'])
+ expect(stopped(changes.machines)).toEqual([{ running: ['worker-harness'], stopped: ['ghost-harness'] }])
+ expect(JSON.stringify(changes)).not.toMatch(/secret-harness|removed-harness|secret-old/)
+ }
+ }
+ })
+
+ it('an agent stored with no project stays hidden from a collaborator when only a finished start holds its private conversation', async () => {
+ const s = await scene((f, ids) => {
+ f.contribute('host', 'swarm')
+ const secretStarts = f.group('Secret starts', ids.secret)
+ const loner = f.add('loner-harness', secretStarts, false, ownerActor, 'host', owner.sub)
+ f.edit(state => {
+ const generation = state.people.find(p => p.id === owner.sub)!.generation
+ state.starts = [startOf('loner-start', secretStarts, 'loner-harness', generation)] as never
+ })
+ return { loner, secretStarts }
+ })
+ clock = Date.now()
+ const cursors = { owner: (await get(`/api/super-tabs/${s.tabId}/parts`, owner)).body.data.cursor, peer: (await get(`/api/super-tabs/${s.tabId}/parts`, peer)).body.data.cursor }
+ await swarmTransaction(db, async tx => {
+ const agent = await tx.swarmAgent.findUniqueOrThrow({ where: { id: `${s.tabId}:${s.more.loner}` } })
+ // As prod still holds some (6 of 48 live agents, 10-29): no stored project, so it is read from the conversation it is in
+ // (projects.ts agentProjectId). Migration stores one, so the part loses it here.
+ const { projectId: _stored, ...data } = agent.data as { projectId?: string }
+ await guardedWrite(tx, s.tabId, { op: 'update', kind: 'agent', key: s.more.loner, seen: agent.version,
+ // Kept after its work: an agent with no stored decision has its active conversations loaded anyway (light.ts undecidedWork).
+ data: { ...data, name: 'Loner, renamed', workDecision: 'keep' } as never }, Date.now())
+ })
+ clock = Date.now()
+ const changes = async (reader: AuthUser, cursor: string) => (await get(`/api/super-tabs/${s.tabId}/changes?cursor=${cursor}`, reader)).body.data
+ expect(keys((await changes(owner, cursors.owner)).agents)).toEqual([s.more.loner])
+ const seen = await changes(peer, cursors.peer)
+ expect(keys(seen.agents)).toEqual([])
+ expect(JSON.stringify(seen)).not.toMatch(/Loner|loner-harness/)
+ })
+
+ it(`a change poll of a Swarm with ${FINISHED} finished starts reads a few tens of KB, not every start`, async () => {
+ const s = await startsScene()
+ const starts = (await db.swarmStart.findMany({ where: { tabId: s.tabId } })).reduce((n, row) => n + JSON.stringify(row.data).length, 0)
+ expect(starts).toBeGreaterThan(400_000)
+ clock = Date.now()
+ const cursor = (await get(`/api/super-tabs/${s.tabId}/parts`, peer)).body.data.cursor
+ clock = Date.now()
+ const bytes = await databaseBytes(() => get(`/api/super-tabs/${s.tabId}/changes?cursor=${cursor}`, peer))
+ expect(bytes).toBeLessThan(25_000)
+ })
+
+ it('reads its starts by index: whole ones only as many as it returns, and no collection scan', async () => {
+ const s = await startsScene()
+ clock = Date.now()
+ const cursor = (await get(`/api/super-tabs/${s.tabId}/parts`, owner)).body.data.cursor
+ await changeAfterCursor(s.tabId)
+ clock = Date.now()
+ await db.$runCommandRaw({ profile: 0 })
+ await db.$runCommandRaw({ drop: 'system.profile' }).catch(() => {})
+ await db.$runCommandRaw({ create: 'system.profile', capped: true, size: 16 * 1024 * 1024 })
+ await db.$runCommandRaw({ profile: 2, slowms: 0 })
+ try {
+ expect((await get(`/api/super-tabs/${s.tabId}/changes?cursor=${cursor}`, owner)).status).toBe(200)
+ } finally { await db.$runCommandRaw({ profile: 0 }) }
+ const reply = await db.$runCommandRaw({ aggregate: 'system.profile', pipeline: [{ $match: { ns: { $regex: '\\.SwarmStart$' } } },
+ { $set: { distinct: '$command.distinct' } }, { $project: { ...PROFILE_STAGES[0]!.$project, distinct: 1 } }], cursor: {} }) as unknown as
+ { cursor: { firstBatch: Array<ProfileEntry & { distinct?: string }> } }
+ const reads = reply.cursor.firstBatch.map(row => ({ ...profileEntry(row), distinct: !!row.distinct }))
+ expect(reads.length).toBeGreaterThan(0)
+ expect(reads.filter(r => r.planSummary === 'COLLSCAN')).toEqual([])
+ // Whole starts: the unfinished one, the two changed (the change feed's own read), and those a machine lists (ghost,
+ // secret-old). The Swarm's newest change reads index keys only (docs 0).
+ 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)
+ })
+})
})
diff --git a/backend/src/superTabs/cachedReads.ts b/backend/src/superTabs/cachedReads.ts
index 935e0ae59..de70bd71b 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, ...(['groupIds', 'agentIds', 'projectIds'] as const)
+ const name = `light:${JSON.stringify([opts.groups ?? 'active', !!opts.includeRecentStarts, !!opts.recentStartGroups, ...(['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 }))))),
diff --git a/backend/src/superTabs/parts/index.ts b/backend/src/superTabs/parts/index.ts
index b80773d87..9d68f6c69 100644
--- a/backend/src/superTabs/parts/index.ts
+++ b/backend/src/superTabs/parts/index.ts
@@ -7,6 +7,6 @@ export * from './types.js'
export { clock, createClock } from './clock.js'
export { knownKind, reassemble, split, toLegacyState } from './shape.js'
-export { DORMANT_PAGE, insertParts, lastChangedAt, loadActive, loadAll, loadChanged, loadDormant, loadGroups, loadMachines, loadObserved, loadReadBase } from './load.js'
+export { DORMANT_PAGE, insertParts, lastChangedAt, loadActive, loadAll, loadChanged, loadDormant, loadGroups, loadMachines, loadObserved, loadReadBase, loadStartsOfAgents } from './load.js'
export { loadLight } from './light.js'
export { guardedWrite, PartConflict, type PartWrite } from './write.js'
diff --git a/backend/src/superTabs/parts/light.ts b/backend/src/superTabs/parts/light.ts
index f6e145a56..e3c42eb74 100644
--- a/backend/src/superTabs/parts/light.ts
+++ b/backend/src/superTabs/parts/light.ts
@@ -107,20 +107,28 @@ export async function loadDeliveryParts(tx: Transaction, tabId: string, groupIds
* `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).
*/
export async function loadLight(tx: Transaction, tabId: string,
- opts: { groupIds?: string[]; agentIds?: string[]; projectIds?: string[]; includeRecentStarts?: boolean; groups?: 'active' | 'working'
+ opts: { groupIds?: string[]; agentIds?: string[]; projectIds?: string[]; includeRecentStarts?: boolean; recentStartGroups?: 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 ?? '',
- ...starts.map(s => s.data!.request.conversationId), ...invitations.map(i => i.conversationId)]
+ ...startGroups, ...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).
diff --git a/backend/src/superTabs/parts/load.ts b/backend/src/superTabs/parts/load.ts
index ccc2fcc74..27d32329e 100644
--- a/backend/src/superTabs/parts/load.ts
+++ b/backend/src/superTabs/parts/load.ts
@@ -3,7 +3,7 @@ import { delegate, extracted, partId, type PartRow } from './write.js'
import type { GroupData, HeadData, MachineData, Part, PartKind, SwarmParts } from './types.js'
import { PART_KINDS } from './types.js'
import { noteChange } from '../cache.js'
-import { stableId } from '../model.js'
+import { stableId, type AgentStart } from '../model.js'
import { ms } from './light.js'
import { groupsBy, readBy, type GroupIndex, type NamedCollection } from './indexed.js'
@@ -150,6 +150,16 @@ export async function loadChanged(tx: Transaction, tabIds: string[], since: Date
return changed
}
+/** The live starts of these agents (an agent is its machine and harness: write.ts extracted), whole, by the (tabId, agentId)
+ * index: a change poll reads the finished starts whose harness a machine record still lists, and no other (read/service.ts).
+ * A plain match: through Prisma, `agentId: { in }` was $expr, which no index bounds. */
+export async function loadStartsOfAgents(tx: Transaction, tabId: string, agentIds: string[]): Promise<Part<AgentStart>[]> {
+ if (!agentIds.length) return []
+ const rows = await tx.swarmStart.aggregateRaw({ pipeline: [{ $match: { tabId, agentId: { $in: agentIds }, deleted: false } }] }) as unknown as
+ Array<Record<string, unknown>>
+ return rows.map(row => toPart<AgentStart>('start', rawPartRow(row)))
+}
+
const COLLECTION = { head: 'SwarmHead', member: 'SwarmMember', agent: 'SwarmAgent', group: 'SwarmGroup', machine: 'SwarmMachine', start: 'SwarmStart',
progress: 'SwarmProgress' } as const satisfies Record<PartKind, NamedCollection>
/** What a changed part is made of: its other fields are indexed copies of its data. */
diff --git a/backend/src/superTabs/read/service.ts b/backend/src/superTabs/read/service.ts
index 2018a7553..fe8673979 100644
--- a/backend/src/superTabs/read/service.ts
+++ b/backend/src/superTabs/read/service.ts
@@ -5,7 +5,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 { author, fail, groupWithoutMachines, isLeads, seenMachineIds, visibleState, type Actor, type AgentStart, type SuperTabState } from '../model.js'
+import { author, fail, groupWithoutMachines, isLeads, seenMachineIds, stableId, visibleState, type Actor, type AgentStart, type SuperTabState } from '../model.js'
import { mentionsDigest, swarmsWithTags, waitingMentions } from '../mentions.js'
import { decisionsWaiting } from '../decisions.js'
import { readMarks, writeReadMark } from '../readMarks.js'
@@ -32,7 +32,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'>
+type LoadOptions = NonNullable<Parameters<typeof parts.loadActive>[2]> & Pick<NonNullable<Parameters<typeof parts.loadLight>[2]>, 'groups' | 'recentStartGroups'>
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). */
@@ -49,6 +49,13 @@ function readableMarks(read: Read, marks: Map<string, number>): Pick<Derived, 'r
return { readMarks: Object.fromEntries([...marks].filter(([id]) => readable(id))) }
}
+/** `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
+ const held = new Set(loaded.starts.map(s => s.key))
+ return { ...loaded, starts: [...loaded.starts, ...extra.filter(s => !held.has(s.key))] }
+}
+
export class SwarmReads {
private readonly db: PrismaClient
private readonly now: () => number
@@ -73,6 +80,29 @@ export class SwarmReads {
})
}
+ /**
+ * The finished starts a change poll needs besides the unfinished ones it loaded: those changed since its cursor, as the
+ * change feed read them (it sends them as a first load shows them), and those whose harness a machine record still lists
+ * (model.ts visibleState shows a reader a listed harness only beside an agent or start it may read). Every other finished
+ * start leaves the reply as it was: a poll sends only changed parts. The second read is by the (tabId, agentId) index and
+ * held at the Swarm's main stamp, under the harnesses it names.
+ */
+ private async pollStarts(tx: PrismaClient, cached: CacheView, tabId: string, base: SwarmParts, changed: Part[]): Promise<Part<AgentStart>[]> {
+ const held = new Set(base.starts.map(s => s.key))
+ const fresh = (changed.filter(p => p.kind === 'start' && !p.deleted && !held.has(p.key)) as Part<AgentStart>[])
+ const agentOf = (s: Part<AgentStart>) => s.data!.harnessId ? stableId(s.data!.request.machineId, s.data!.harnessId) : null
+ const covered = new Set([...base.starts, ...fresh].map(agentOf))
+ const listed = [...new Set(base.machines.flatMap(m => {
+ const pool = m.data?.contribution, seen = pool?.observation
+ if (!pool || !seen) return []
+ return [...seen.runningHarnessIds, ...seen.stoppedHarnessIds, ...Object.keys(seen.harnessProgress ?? {}), ...(seen.stoppedReservations ?? []).map(r => r.harnessId)]
+ .map(harnessId => stableId(pool.machineId, harnessId))
+ }))].filter(id => !covered.has(id)).sort()
+ const named = listed.length ? await cached.get(tabId, `startsOf:${JSON.stringify(listed)}`, 'main', () => parts.loadStartsOfAgents(tx, tabId, listed)) : []
+ const taken = new Set([...held, ...fresh.map(s => s.key)])
+ return [...fresh, ...named.filter(s => !taken.has(s.key))]
+ }
+
/** The reader's avatar links (avatarLinks.ts), only ever in a member's read: issued afresh each time, never stored. */
private async withAvatars(view: ReaderView, now: number): Promise<Derived> {
if (!this.avatarLinks) return view.derived
@@ -89,12 +119,17 @@ export class SwarmReads {
// 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)
- const light = { includeRecentStarts: true, groups: 'working' as const, groupIds: mentioned }
- const lightly: Loader = (view, id, opts) => readLight(tx, view, id, opts)
- let read = await this.load(tx, cached, user, tabId, asAgentId, t0, light, lightly)
- const from = readCursor(cursor, read.parts.visibilityEpoch, t0)
- if ('reload' in from) return from
+ // 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)
+ 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)
+ 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.