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
Merge batch 11-04 b2335: #3046 (#3051)
0xken15a5fbe5a6cb58
diff --git a/backend/integration/superTabThreads.test.ts b/backend/integration/superTabThreads.test.tsindex ef6872795..80474cb53 100644--- a/backend/integration/superTabThreads.test.ts+++ b/backend/integration/superTabThreads.test.ts@@ -7,10 +7,10 @@ import { afterAll, beforeAll, describe, expect, it } from 'vitest'import { MongoMemoryReplSet } from 'mongodb-memory-server-core'import { PrismaClient, type Prisma } from '@prisma/client'import type { AuthUser } from '../src/lib/ssoAuth.js'-import { SuperTabStore } from '../src/superTabs/store.js'+import { SuperTabStore, transaction } from '../src/superTabs/store.js'import { migrateSwarm } from '../src/superTabs/migration/migrate.js'import { buddyChatId } from '../src/superTabs/buddies.js'-import { refreshThreads, threadFacet } from '../src/superTabs/pageReads.js'+import { foldReplies, refreshThreads, threadFacet } from '../src/superTabs/pageReads.js'import { backfillThreads } from '../src/superTabs/threadBackfill.js'import type { SuperTabState } from '../src/superTabs/model.js'import { writeLegacySwarm } from '../scripts/swarm-parts/legacy.js'@@ -113,6 +113,10 @@ const pages = async (s: Awaited<ReturnType<typeof scene>>) => Object.fromEntries[name, await store.messages(as, s.tabId, conversationId, {}, agentId)] as const)))const replies = (page: { threads: Array<{ id: string; replies: number }> }) => Object.fromEntries(page.threads.map(t => [t.id.replace(/^s\d+-/, ''), t.replies]))const sorted = (rows: unknown[]) => rows.map(row => JSON.stringify(row)).sort()+/** The stored summaries of `roots` as the database holds them, types included, their parts and tags in a stable order. */+const rawSummaries = async (roots: string[]) => ((await db.$runCommandRaw({ find: 'SuperTabThread', filter: { _id: { $in: roots } }, sort: { _id: 1 } }) as unknown as+ { cursor: { firstBatch: Array<Record<string, unknown>> } }).cursor.firstBatch)+ .map(({ updatedAt: _, parts, mentions, ...rest }) => ({ ...rest, parts: sorted(parts as unknown[]), mentions: sorted(mentions as unknown[]) }))describe('thread summaries (spec 5 phase D)', () => {it('gives every reader the same page from the summaries as from the $facet, lone roots and hidden replies included', async () => {@@ -156,6 +160,130 @@ describe('thread summaries (spec 5 phase D)', () => {expect(replies(await store.messages(owner, s.tabId, s.web, {}))).toEqual({ 'w-a': 5, 'w-c': 1, 'w-lone': 1 })})+ it('folds a reply into a 500-reply thread\'s summary from its own row, never re-reading the thread (task #2974)', async () => {+ const s = await scene()+ const long = Array.from({ length: 500 }, (_, i) => ({ id: `w-a-long-${i}`, conversationId: s.web, thread: 'w-a',+ author: i % 2 ? person(peer) : agent(s.worker) }))+ await insert(s.tabId, s.prefix, long)+ await backfillThreads(db, Date.now())+ const entries = await profiled(db, () => store.execute(owner, s.tabId, { operationId: randomUUID(), command: { type: 'message.send', conversationId: s.web,+ text: 'One more', replyTo: s.id('w-a'), recipientIds: [] } as never }))+ const messages = entries.filter(entry => entry.ns.endsWith('.SuperTabMessage') && JSON.stringify(filterOf(entry) ?? null).includes(s.tabId))+ const examined = messages.reduce((sum, entry) => sum + entry.docsExamined, 0)+ // Before the fold: 509 (each of the thread's rows, then the root and quoted rows by id). After: the new row and those.+ console.info(`[#2974] SuperTabMessage docs examined by one reply in a 505-reply thread: ${examined}`)+ expect(plan(reads(entries, 'SuperTabMessage', ['conversationId', 'thread']))).toEqual([])+ expect(examined, JSON.stringify(plan(messages))).toBeLessThanOrEqual(4)+ const stored = await db.superTabThread.findUniqueOrThrow({ where: { id: s.id('w-a') } })+ expect(sorted(stored.parts as unknown[])).toEqual(sorted((await threadFacet(db, s.tabId, s.web, [s.id('w-a')])).parts))+ expect(replies(await store.messages(owner, s.tabId, s.web, {}))['w-a']).toBe(505)+ })++ it('stores a folded summary as the full rebuild stores it, for replies of every kind and tag (task #2974)', async () => {+ const s = await scene()+ await backfillThreads(db, Date.now())+ const more: Row[] = [+ { id: 'w-a6', conversationId: s.web, thread: 'w-a', author: agent(s.worker) },+ { id: 'w-a7', conversationId: s.web, thread: 'w-a', author: agent(s.lead), recipientIds: [s.worker], source: { kind: 'note', recipientsOnly: true } },+ { id: 'w-a8', conversationId: s.web, thread: 'w-a', author: agent(s.lead), source: { kind: 'system', ask: 'on-it' } },+ { id: 'w-a9', conversationId: s.web, thread: 'w-a', author: person(peer), mentions: [outsider.sub, owner.sub, outsider.sub] },+ { id: 'w-b2', conversationId: s.web, thread: 'w-b', author: person(owner), mentions: [peer.sub] },+ { id: 'w-b3', conversationId: s.web, thread: 'w-b', author: person(owner) },+ ]+ const roots = ['w-a', 'w-b'].map(s.id)+ await transaction(db, async tx => {+ await tx.superTabMessage.createMany({ data: more.map(row => ({ recipientIds: [], mentions: [], ...row, id: s.id(row.id), thread: s.id(row.thread),+ tabId: s.tabId, text: row.id, sequence: ++sequence, createdAt: new Date() })) })+ await foldReplies(tx, s.tabId, s.web, more.map(row => ({ id: s.id(row.id), thread: s.id(row.thread) })))+ })+ const chat = [{ id: s.id('b-r4'), conversationId: s.buddyChat, thread: s.id('b-root'), author: agent(s.buddy), buddySession: true }]+ await transaction(db, async tx => {+ await tx.superTabMessage.createMany({ data: chat.map(row => ({ recipientIds: [], mentions: [], ...row, tabId: s.tabId, text: 'x', sequence: ++sequence })) })+ await foldReplies(tx, s.tabId, s.buddyChat, chat)+ })+ const folded = await rawSummaries([...roots, s.id('b-root')])+ await refreshThreads(db, s.tabId, s.web, roots)+ await refreshThreads(db, s.tabId, s.buddyChat, [s.id('b-root')])+ expect(folded).toEqual(await rawSummaries([...roots, s.id('b-root')]))+ expect(Object.keys(folded[0]!).sort()).toEqual(['_id', 'conversationId', 'mentions', 'parts', 'tabId'])+ })++ it('leaves the full rebuild\'s summary after folded replies are edited and deleted, and none once the last reply goes (task #2974)', async () => {+ const s = await scene()+ await backfillThreads(db, Date.now())+ const send = async (replyTo: string, as: AuthUser, mentions: string[] = []) => String((await store.execute(as, s.tabId, { operationId: randomUUID(),+ command: { type: 'message.send', conversationId: s.web, text: 'r', replyTo, recipientIds: [], mentions } as never })).result.messageId)+ const run = (as: AuthUser, command: object) => store.execute(as, s.tabId, { operationId: randomUUID(), command: { conversationId: s.web, ...command } as never })+ const same = async (root: string) => {+ const stored = await db.superTabThread.findUnique({ where: { id: root } })+ const facet = await threadFacet(db, s.tabId, s.web, [root])+ if (!facet.parts.some(p => !p._id.root)) return expect(stored).toBeNull()+ expect(sorted(stored!.parts as unknown[])).toEqual(sorted(facet.parts))+ expect(sorted(stored!.mentions as unknown[])).toEqual(sorted(facet.mentions))+ }+ const root = s.id('w-c')+ const first = await send(root, owner, [peer.sub]), second = await send(root, peer), third = await send(root, owner, [outsider.sub])+ await same(root)+ await run(owner, { type: 'message.edit', messageId: first, text: 'edited' })+ await same(root)+ // The newest of its part, the only reply of its part, then the oldest of what is left: each moves what the summary holds.+ for (const [messageId, as] of [[third, owner], [second, peer], [first, owner]] as const) {+ await run(as, { type: 'message.delete', messageId })+ await same(root)+ }+ // The route row the scene wrote is the thread's last reply; once it goes (as no person can delete it), so does the summary.+ await db.superTabMessage.delete({ where: { id: s.id('w-c1') } })+ await refreshThreads(db, s.tabId, s.web, [root])+ await same(root)+ expect(replies(await store.messages(owner, s.tabId, s.web, {}))['w-c']).toBeUndefined()+ })++ it('counts both of two replies sent at once, the second reading the summary the first committed (task #2974)', async () => {+ const s = await scene()+ await backfillThreads(db, Date.now())+ const root = s.id('w-a')+ // Both transactions read the summary before either writes it: the later write conflicts and its transaction runs again.+ let waiting = 0, release!: () => void+ const bothRead = new Promise<void>(resolve => { release = resolve })+ const attempts: number[] = []+ const reply = (name: string, as: object) => { const at = attempts.push(0) - 1; return transaction(db, async tx => {+ const first = ++attempts[at]! === 1+ await tx.superTabMessage.create({ data: { id: s.id(name), tabId: s.tabId, conversationId: s.web, thread: root, sequence: ++sequence, text: name,+ author: as, recipientIds: [], mentions: [] } })+ const paused = new Proxy(tx, { get(target, key, receiver) {+ if (key !== '$runCommandRaw') return Reflect.get(target, key, receiver)+ return async (command: Record<string, unknown>) => {+ if (command.update === 'SuperTabThread' && first) {+ if (++waiting === 2) release()+ await Promise.race([bothRead, new Promise(resolve => setTimeout(resolve, 5_000))])+ }+ return target.$runCommandRaw(command as Prisma.InputJsonObject)+ }+ } })+ await foldReplies(paused, s.tabId, s.web, [{ id: s.id(name), thread: root }])+ }) }+ await Promise.all([reply('w-a-at-once-1', person(peer)), reply('w-a-at-once-2', agent(s.worker))])+ // Both read the summary before either wrote it, and one of them ran again after the other committed.+ expect(waiting).toBe(2)+ expect(Math.max(...attempts)).toBeGreaterThanOrEqual(2)+ const stored = await db.superTabThread.findUniqueOrThrow({ where: { id: root } })+ expect(sorted(stored.parts as unknown[])).toEqual(sorted((await threadFacet(db, s.tabId, s.web, [root])).parts))+ expect(replies(await store.messages(owner, s.tabId, s.web, {}))['w-a']).toBe(6)+ })++ it('counts both of two replies sent at once through the store (task #2974)', async () => {+ const s = await scene()+ await backfillThreads(db, Date.now())+ const send = (as: AuthUser) => store.execute(as, s.tabId, { operationId: randomUUID(), command: { type: 'message.send',+ conversationId: s.web, text: 'At once', replyTo: s.id('w-a'), recipientIds: [] } as never })+ // One reply each first, so the two at once fold into parts the summary already holds.+ for (const as of [owner, peer]) await send(as)+ await Promise.all([owner, peer].map(send))+ const stored = await db.superTabThread.findUniqueOrThrow({ where: { id: s.id('w-a') } })+ expect(sorted(stored.parts as unknown[])).toEqual(sorted((await threadFacet(db, s.tabId, s.web, [s.id('w-a')])).parts))+ expect(replies(await store.messages(owner, s.tabId, s.web, {}))['w-a']).toBe(8)+ })+it('leaves no summary when the reply\'s transaction fails', async () => {const s = await scene()await expect(db.$transaction(async tx => {diff --git a/backend/src/superTabs/pageReads.test.ts b/backend/src/superTabs/pageReads.test.tsnew file mode 100644index 000000000..f74f1ed73--- /dev/null+++ b/backend/src/superTabs/pageReads.test.ts@@ -0,0 +1,56 @@+import { describe, expect, it } from 'vitest'+import { foldFacet, type ThreadFacet } from './pageReads.js'++// A reply's rows folded into its thread's stored summary must give what threadFacet's $group gives every row (task #2974).+const author = (id: string) => ({ kind: 'person' as const, id, name: id, membershipGeneration: 'gen' })+const key = (id: string, extra: object = {}) => ({ thread: 't', root: false, to: false, author: { kind: 'person', id }, session: false, ...extra })+const place = (s: number, id: string, slot?: number) => ({ s, i: `m${s}`, at: { $date: new Date(s * 1000).toISOString() }, author: author(id),+ ...slot === undefined ? {} : { slot } })+type Part = ThreadFacet['parts'][number]+const part = (id: string, count: number, oldest: number, newest: number, extra: object = {}): Part =>+ ({ _id: key(id, extra), count, oldest: place(oldest, id), newest: place(newest, id), root: null })+const tag = (by: string, person: string, s: number, slot = 0) => ({ _id: { ...key(by), person }, first: place(s, by, slot) })+const stored: ThreadFacet = {+ parts: [{ _id: key('ann', { root: true }), count: 1,+ oldest: place(1, 'ann'), newest: place(1, 'ann'), root: { source: null, recipientIds: [] } }, part('bob', 3, 2, 9)],+ mentions: [tag('bob', 'cy', 4)],+}++describe('foldFacet', () => {+ it('adds a reply of a part already in the thread: its count grows, its newest row moves, its oldest stays', () => {+ expect(foldFacet(stored, { parts: [part('bob', 1, 12, 12)], mentions: [] })).toEqual({+ parts: [stored.parts[0], { ...part('bob', 4, 2, 12) }], mentions: stored.mentions })+ })++ it('adds a part new to the thread as its rows sum it up', () => {+ const route = part('bob', 2, 12, 13, { kind: 'route' })+ expect(foldFacet(stored, { parts: [route], mentions: [] }).parts).toEqual([...stored.parts, route])+ })++ it('keeps a person\'s first tag in a part, and adds one first tagged now', () => {+ const folded = foldFacet(stored, { parts: [part('bob', 1, 12, 12)], mentions: [tag('bob', 'cy', 12), tag('bob', 'dee', 12, 1)] })+ expect(folded.mentions).toEqual([tag('bob', 'cy', 4), tag('bob', 'dee', 12, 1)])+ })++ it('orders places as the database does, by sequence, then id, then a tag\'s slot', () => {+ const same = { parts: [part('bob', 1, 9, 9)], mentions: [tag('bob', 'cy', 4, 0)] }+ const earlier = { ...tag('bob', 'cy', 4, 0), first: { ...place(4, 'bob', 0), i: 'm3' } }+ expect(foldFacet(stored, same).parts[1]).toEqual(part('bob', 4, 2, 9))+ expect(foldFacet(stored, { parts: [], mentions: [earlier] }).mentions).toEqual([earlier])+ expect(foldFacet({ parts: [], mentions: [tag('bob', 'cy', 4, 2)] }, { parts: [], mentions: [tag('bob', 'cy', 4, 1)] }).mentions)+ .toEqual([tag('bob', 'cy', 4, 1)])+ })++ it('matches a part by its key\'s values, whatever order the stored key\'s fields came back in', () => {+ const { thread, root, ...rest } = key('bob')+ const reordered: ThreadFacet = { parts: [{ ...part('bob', 3, 2, 9), _id: { ...rest, root, thread } as Part['_id'] }], mentions: [] }+ expect(foldFacet(reordered, { parts: [part('bob', 1, 12, 12)], mentions: [] }).parts).toHaveLength(1)+ expect(foldFacet(reordered, { parts: [part('bob', 1, 12, 12)], mentions: [] }).parts[0]!.count).toBe(4)+ })++ it('leaves the stored summary as it was', () => {+ const before = structuredClone(stored)+ foldFacet(stored, { parts: [part('bob', 1, 12, 12)], mentions: [tag('bob', 'cy', 1)] })+ expect(stored).toEqual(before)+ })+})diff --git a/backend/src/superTabs/pageReads.ts b/backend/src/superTabs/pageReads.tsindex 134e2188b..cdb08039f 100644--- a/backend/src/superTabs/pageReads.ts+++ b/backend/src/superTabs/pageReads.ts@@ -238,14 +238,23 @@ export interface ThreadFacet {* still examines each of the threads' rows.*/export async function threadFacet(tx: Transaction, tabId: string, conversationId: string, roots: string[]): Promise<ThreadFacet> {+ // No $sort: it makes the planner walk the conversation's sequence index instead.+ return summed(tx, 'thread', { tabId, conversationId, thread: { $in: roots } })+}++/** threadFacet of the rows `ids` alone, read by `_id`: what a reply's rows add to their threads' summaries (foldReplies). */+async function rowsFacet(tx: Transaction, tabId: string, conversationId: string, ids: string[]): Promise<ThreadFacet> {+ return summed(tx, 'id', { _id: { $in: ids }, tabId, conversationId })+}++async function summed(tx: Transaction, index: 'thread' | 'id', match: Prisma.InputJsonObject): Promise<ThreadFacet> {const root = { $eq: ['$_id', '$thread'] }const part = { thread: '$thread', root, kind: '$source.kind', only: '$source.recipientsOnly', ask: '$source.ask',to: { $gt: [{ $size: { $ifNull: ['$recipientIds', []] } }, 0] }, author: { kind: '$author.kind', id: '$author.id' },session: { $eq: ['$buddySession', true] } }// (sequence, id) lead each place, so the database's min and max order rows as threadReplies sorts them (ids are ASCII).const place = { s: '$sequence', i: '$_id', at: '$createdAt', author: '$author' }- // No $sort: it makes the planner walk the conversation's sequence index instead.- const [found] = await readBy(tx, 'SuperTabMessage', 'thread', [{ $match: { tabId, conversationId, thread: { $in: roots } } },+ const [found] = await readBy(tx, 'SuperTabMessage', index, [{ $match: match },{ $facet: {parts: [{ $group: { _id: part, count: { $sum: 1 }, oldest: { $min: place }, newest: { $max: place },// A root is its part's only row: what deliveredCopies reads of it.@@ -370,19 +379,72 @@ export async function refreshThreads(tx: Transaction, tabId: string, conversatioreturn facet}-/** Refreshes the summaries of the threads that `rows`, just inserted, reply in (refreshThreads), and answers each thread's facet- * by its root, which the same transaction's Activity reads instead of the thread's rows (activity.ts writeReplyActivity). A row- * that is its own thread's root, or has none (written before threads), changes no summary. */+/** A part's or tag's key by value, whatever its fields' order: the database groups by the whole key. */+const keyOf = (key: unknown): string => JSON.stringify(key, (_, value: unknown) => value && typeof value === 'object' && !Array.isArray(value)+ ? Object.fromEntries(Object.entries(value).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0)) : value)+/** Places in the order the database's $min and $max give them: by sequence, then id, then a tag's slot. */+const before = (a: ThreadPlace, b: ThreadPlace) => Number(a.s) - Number(b.s) || (a.i < b.i ? -1 : a.i > b.i ? 1 : 0) || (a.slot ?? 0) - (b.slot ?? 0)++/** A thread's summary with the rows `added` sums up folded in, as threadFacet gives every row of both: each part's count adds,+ * its oldest row is the older and its newest the newer; each person's first tag in a part is the earlier. */+export function foldFacet(stored: ThreadFacet, added: ThreadFacet): ThreadFacet {+ const parts = new Map(stored.parts.map(p => [keyOf(p._id), { ...p }]))+ for (const p of added.parts) {+ const kept = parts.get(keyOf(p._id))+ if (!kept) parts.set(keyOf(p._id), { ...p })+ else Object.assign(kept, { count: kept.count + p.count, oldest: before(p.oldest, kept.oldest) < 0 ? p.oldest : kept.oldest,+ newest: before(p.newest, kept.newest) > 0 ? p.newest : kept.newest })+ }+ const mentions = new Map(stored.mentions.map(m => [keyOf(m._id), { ...m }]))+ for (const m of added.mentions) {+ const kept = mentions.get(keyOf(m._id))+ if (!kept || before(m.first, kept.first) < 0) mentions.set(keyOf(m._id), { ...m })+ }+ return { parts: [...parts.values()], mentions: [...mentions.values()] }+}++/**+ * Folds the rows `ids`, just inserted in this transaction and replying in threads of one conversation, into those threads'+ * stored summaries (foldFacet): it reads each summary by root id and only the new rows by `_id`, never the threads' other+ * rows, so a reply in a 500-reply thread examines a few documents, not 500 (task #2974). A thread with no summary yet (its+ * first reply, or a Swarm not backfilled) is counted whole once (refreshThreads). Two replies at once both write the same+ * summary, so one transaction meets a write conflict and runs again, reading the summary the other committed. Answers the+ * threads' whole facets, as refreshThreads does.+ */+export async function foldReplies(tx: Transaction, tabId: string, conversationId: string,+ rows: ReadonlyArray<{ id: string; thread: string }>): Promise<ThreadFacet> {+ const roots = [...new Set(rows.map(row => row.thread))]+ const stored = new Map((await readBy(tx, 'SuperTabThread', 'id', [{ $match: { _id: { $in: roots } } },+ { $project: { tabId: 1, conversationId: 1, parts: 1, mentions: 1 } }]) as unknown as Array<ThreadFacet & { _id: string; tabId: string; conversationId: string }>)+ .filter(t => t.tabId === tabId && t.conversationId === conversationId).map(t => [t._id, t]))+ const whole = await refreshThreads(tx, tabId, conversationId, roots.filter(root => !stored.has(root)))+ const folding = rows.filter(row => stored.has(row.thread))+ if (!folding.length) return whole+ const added = await rowsFacet(tx, tabId, conversationId, folding.map(row => row.id))+ const updatedAt = { $date: new Date().toISOString() }+ const folded = [...new Set(folding.map(row => row.thread))].map(root => {+ const summary = stored.get(root)!+ return { root, ...foldFacet(summary, { parts: added.parts.filter(p => p._id.thread === root), mentions: added.mentions.filter(m => m._id.thread === root) }) }+ })+ // Raw, as refreshThreads writes it: the stored parts read back as the $facet returns them, dates as dates.+ await tx.$runCommandRaw({ update: 'SuperTabThread', updates: folded.map(({ root, parts, mentions }) =>+ ({ q: { _id: root, tabId, conversationId }, u: { $set: { parts, mentions, updatedAt } } })) as unknown as Prisma.InputJsonArray })+ return { parts: [...whole.parts, ...folded.flatMap(f => f.parts)], mentions: [...whole.mentions, ...folded.flatMap(f => f.mentions)] }+}++/** Folds the rows `rows`, just inserted, into the summaries of the threads they reply in (foldReplies), and answers each+ * thread's facet by its root, which the same transaction's Activity reads instead of the thread's rows (activity.ts+ * writeReplyActivity). A row that is its own thread's root, or has none (written before threads), changes no summary. */export async function refreshReplies(tx: Transaction, tabId: string,rows: ReadonlyArray<{ id: string; conversationId: string; thread?: string | null }>): Promise<Map<string, ThreadFacet>> {- const roots = new Map<string, Set<string>>()+ const replies = new Map<string, Array<{ id: string; thread: string }>>()for (const row of rows) {- if (row.thread && row.thread !== row.id) roots.set(row.conversationId, (roots.get(row.conversationId) ?? new Set()).add(row.thread))+ if (row.thread && row.thread !== row.id) replies.set(row.conversationId, [...replies.get(row.conversationId) ?? [], { id: row.id, thread: row.thread }])}const facets = new Map<string, ThreadFacet>()- for (const [conversationId, threads] of roots) {- const facet = await refreshThreads(tx, tabId, conversationId, [...threads])- for (const root of threads) {+ for (const [conversationId, list] of replies) {+ const facet = await foldReplies(tx, tabId, conversationId, list)+ for (const root of new Set(list.map(row => row.thread))) {facets.set(root, { parts: facet.parts.filter(p => p._id.thread === root), mentions: facet.mentions.filter(m => m._id.thread === root) })}}diff --git a/docs/plans/2026-11-04-thread-summary-fold-plan.md b/docs/plans/2026-11-04-thread-summary-fold-plan.mdnew file mode 100644index 000000000..f6fb4515d--- /dev/null+++ b/docs/plans/2026-11-04-thread-summary-fold-plan.md@@ -0,0 +1,95 @@+# A reply updates its thread summary without re-reading the thread Implementation Plan++> Implemented by the task's Engineer task by task, each failing test committed before its code (project instructions,+> Engineer). One review on the pull request, focused on concurrent replies and on edits and deletes.++**Goal:** A reply updates its thread's stored summary (`SuperTabThread`) from its own new rows, not by re-reading every row of+the thread, so a reply in a 500-reply thread examines a few documents instead of ~500 (task #2974, from Ken's slow-query data+of 2026-11-04: the `$match {tabId, conversationId, thread}` shape is 7% of slow message time at peak and 19% in the evening;+traced by task #2966 to `refreshThreads`).++**Architecture:** A summary is what `threadFacet`'s `$group` gives a thread's rows: per part (one author and one kind of row)+its count, oldest and newest row; per tagged person and part, the first tag. Each of those folds: count adds, oldest is a+min, newest a max, a first tag a min. So the send transaction reads the stored summaries of the threads it replies in by+`_id`, sums up only the rows it just inserted with the same `$group` (matched by `_id`), and writes the fold. A thread with no+stored summary yet (its first reply, or a Swarm not yet backfilled) is counted whole once, as today. Deletes keep the full+recount (`refreshThreads`), as do the backfill (repair) and a deleted root kept for its replies. An edit changes only a row's+text and `editedAt`, which no summary holds, so it writes nothing.++**Tech Stack:** TypeScript, Prisma 5 on MongoDB (raw aggregations and commands), Vitest (unit + mongodb-memory-server+integration).++**Spec:** task #2974's brief. MongoDB schema design skill (Computed pattern, "update on write") and query optimizer skill+(update examples) read 2026-11-04.++## Global Constraints++- Correctness first: after any reply, edit, delete or two concurrent replies, a thread's stored summary equals what+ `refreshThreads` would write from its rows (parts and tags, in any order).+- The fold runs in the send's transaction. Two replies at once both write the same summary document, so Mongo's write+ conflict makes one transaction run again (store.ts `transaction`), which re-reads the summary the other committed.+- Matched by `_id` through the `id` index (parts/indexed.ts readBy), never by `{tabId, conversationId, thread}`.+- Stored document shape unchanged: `{ _id, tabId, conversationId, parts, mentions, updatedAt }`, parts and tags as the `$facet`+ returns them. Integration tests pin it against the full rebuild.+- `refreshThreads` stays the full rebuild: the backfill (threadBackfill.ts) and deletes use it.+- Backend only. Swarm code stays out of cli/src/core.+- PR body first line: "Requested by Ken through Harness." (the brief's line).++## Design++- growth: no new data. One summary per thread with a reply, as now; deleted with its Swarm (purge) or when its last reply goes.+- concurrency: document level. Every reply writes its thread's summary in its transaction; a concurrent reply's write+ conflicts and its transaction runs again. Replies also write their conversation's group part (messageSeq), so they were+ already serialized per conversation.+- read cost: per reply, the thread's summary by `_id` (1 doc) and the new rows by `_id` (1 per new row). Before: every row of+ the thread.+- scale: 10x replies cost 10x a few documents. A summary grows with distinct (author, kind) parts and tagged people, not with+ replies. Warning metric: docs examined per `SuperTabMessage` aggregate in the slow-query log, and `SuperTabThread` document+ size.+- compatibility: older backends still recount whole and write the same shape; summaries they leave behind fold correctly. A+ reply an older release wrote without a summary update is repaired by the 15-minute backfill, as today.+- migration + rollback: none; same stored shape. A rollback recounts whole again.+- audience: unchanged; readers filter summaries as today (threadReplies).+- identity: N/A because no actor is read or written beyond the rows' own authors.+- failure: a fold's error aborts the send's transaction as any write does; nothing is half-written.+- platforms: backend only.++## Review Focus++- Two concurrent replies in one thread: both counted, newest is the later one.+- A reply whose part (author + kind) is new to the thread, and one whose part exists.+- Tags: a person first tagged in the new reply, and one tagged before.+- Deletes and edits after folded replies leave the rebuild's summary.++## Task 1: Cost — a reply reads a few documents, not the thread++**Files:** Test `backend/integration/superTabThreads.test.ts`. Modify `backend/src/superTabs/pageReads.ts`.++**Interfaces:**+- `foldReplies(tx, tabId, conversationId, roots: string[], ids: string[]): Promise<ThreadFacet>`: folds the rows `ids`, just+ inserted and replying in `roots`, into those threads' summaries; a root with no summary is counted whole+ (refreshThreads). Answers the threads' whole facets, as refreshThreads does.+- `refreshReplies` calls `foldReplies` instead of `refreshThreads`.++- [ ] Write a failing test: a backfilled 500-reply thread; one `message.send` reply, profiled. No `SuperTabMessage` read is+ matched by `thread`, and the reply's `SuperTabMessage` docs examined total at most a few. Print before/after numbers.+- [ ] Run it, watch it fail (~500 docs examined).+- [ ] Implement `foldReplies` (summaries by `_id`, new rows' `$group` by `_id`, fold, `$set`), and call it from+ `refreshReplies`.+- [ ] Run the thread and index integration tests.++## Task 2: Correctness — folds equal the full rebuild++**Files:** Test `backend/integration/superTabThreads.test.ts`, `backend/src/superTabs/pageReads.test.ts` (new, the pure fold).+Modify `backend/src/superTabs/pageReads.ts`.++**Interfaces:** `foldFacet(stored: ThreadFacet, added: ThreadFacet): ThreadFacet` (pure).++- [ ] Write failing unit tests for `foldFacet`: a new part, an existing part (count adds, oldest stays, newest moves), a new tag,+ an earlier tag kept, part keys compared by value whatever their field order.+- [ ] Write failing integration tests: replies of every kind in the scene (plain, note to an agent, route row, tag, Buddy+ session) leave the stored document equal to the full rebuild's (shape pinned); an edit leaves it unchanged; deleting the+ newest, the oldest and the only reply of a part, and the last reply, leave the rebuild's summary (or none); two concurrent+ replies, both past the summary read before either writes, leave both counted.+- [ ] Watch them fail, then make them pass.+- [ ] Run the thread, message edit, activity and index integration tests.