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.

Swarm read marks: reading a chat never swallows a thread-only reply you haven't seen (Diego) (#2485)

0xken15e42f7530006b
diff --git a/backend/integration/superTabFollowedThreads.test.ts b/backend/integration/superTabFollowedThreads.test.ts
index e6a36a367..0db70a05f 100644
--- a/backend/integration/superTabFollowedThreads.test.ts
+++ b/backend/integration/superTabFollowedThreads.test.ts
@@ -297,11 +297,14 @@ describe('a thread reply past the person\'s thread mark, though the chat\'s mark
expect((await inbox(s.tabId, peer)).body.data.threads).toEqual([])
})
- it('keeps the chat\'s mark for a thread the person never opened, and a Follow alone is no mark', async () => {
- expect((await inbox((await behindScene()).tabId, peer)).body.data.threads).toEqual([])
+ it('counts a thread the person never opened from the chat\'s mark before the read, and a Follow alone takes that mark (Diego, 11-01)', async () => {
+ // Both replies are only in the thread: reading the chat at the main message read neither (read/threadMarkSeeds.ts).
+ const never = await behindScene()
+ expect((await inbox(never.tabId, peer)).body.data.threads).toMatchObject([{ rootId: never.root, unreadReplies: 2 }])
const followed = await behindScene({ followOnly: true })
- expect((await inbox(followed.tabId, peer)).body.data.threads).toEqual([])
- expect(roots((await threads(followed.tabId, peer)).body.data.read)).toEqual(['Peer root'])
+ expect((await inbox(followed.tabId, peer)).body.data.threads).toMatchObject([{ rootId: followed.root, unreadReplies: 2 }])
+ expect(roots((await threads(followed.tabId, peer)).body.data.unread)).toEqual(['Peer root'])
+ expect((await threads(followed.tabId, peer)).body.data.read).toEqual([])
})
})
@@ -411,8 +414,12 @@ describe('a person\'s followed threads (GET /threads)', () => {
await markRead(s.tabId, s.web, await sequenceOf(many.at(-1)!), peer)
await insert(s.tabId, s.web, many.map(thread => ({ text: 'Reply', author: agentAuthor(s.worker), thread })))
await markRead(s.tabId, s.web, await newestIn(s.tabId, s.web), peer)
+ // Each read in its thread: reading the chat reads no reply only in a thread.
+ const top = await newestIn(s.tabId, s.web)
+ for (const root of many) await writeThreadMark(db, { tabId: s.tabId, personId: peer.sub, conversationId: s.web, threadId: root, sequence: top, now: Date.now() })
for (const root of many) await follow(s.tabId, s.web, root, true, peer)
const { body } = await threads(s.tabId, peer)
+ expect(body.data.unread).toEqual([])
expect(body.data.read).toHaveLength(MAX_READ_THREADS)
expect(body.data.more).toBe(true)
})
diff --git a/backend/integration/superTabInbox.test.ts b/backend/integration/superTabInbox.test.ts
index c8dd94c36..96a413b4e 100644
--- a/backend/integration/superTabInbox.test.ts
+++ b/backend/integration/superTabInbox.test.ts
@@ -20,6 +20,7 @@ import { SwarmReads } from '../src/superTabs/read/service.js'
import { HostReads } from '../src/superTabs/read/host.js'
import { buddyChatId } from '../src/superTabs/buddies.js'
import { INBOX_READS, INBOX_WINDOW_MS, LATEST_CHATS, MAX_THREADS, PREVIEW_CHARS, resetInboxReads, THREAD_CHATS } from '../src/superTabs/read/inbox.js'
+import { SEED_SCAN } from '../src/superTabs/read/threadMarkSeeds.js'
import { stableId, type Author, type Conversation, type SuperTabState } from '../src/superTabs/model.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'
@@ -243,12 +244,17 @@ describe('a person\'s inbox: unread threads and each chat\'s latest message', ()
createdAt: shown('Web latest').createdAt, attachments: 0 } })
})
- it('counts only replies past the person\'s read mark, and drops a thread they have read', async () => {
+ it('counts replies only in a thread until the person reads the thread, never by reading the chat, and drops a thread they have read', async () => {
const s = await busyScene()
const reply1 = await db.superTabMessage.findFirstOrThrow({ where: { tabId: s.tabId, text: 'Web reply 1' } })
- await markRead(s.tabId, s.web, reply1.sequence, peer)
- expect(threadsOf((await inbox(s.tabId, peer)).body.data).filter(t => t.chat === s.web)).toEqual([{ chat: s.web, root: 'Web root', unread: 2, last: s.worker }])
+ const markThread = async (sequence: number) => expect((await app.inject({ method: 'POST', url: `/api/super-tabs/${s.tabId}/conversations/${s.web}/threads/${s.webRoot.id}/read`,
+ headers: { authorization: `Bearer ${peer.sub}` }, payload: { sequence } })).statusCode).toBe(200)
+ // The three replies are only in the thread: reading the chat past them reads none (Diego, 11-01).
await markRead(s.tabId, s.web, s.webLatest.sequence, peer)
+ expect(threadsOf((await inbox(s.tabId, peer)).body.data).filter(t => t.chat === s.web)).toEqual([{ chat: s.web, root: 'Web root', unread: 3, last: s.worker }])
+ await markThread(reply1.sequence)
+ expect(threadsOf((await inbox(s.tabId, peer)).body.data).filter(t => t.chat === s.web)).toEqual([{ chat: s.web, root: 'Web root', unread: 2, last: s.worker }])
+ await markThread(s.webLatest.sequence)
const after = (await inbox(s.tabId, peer)).body.data
expect(threadsOf(after).filter(t => t.chat === s.web)).toEqual([])
// The latest message stays: the Buddies list shows it whether read or not.
@@ -423,6 +429,169 @@ describe('thread unread as Desktop marks it (controller.dart threadUnread)', ()
})
})
+describe('reading a chat never reads a reply only in a thread (Diego, 11-01)', () => {
+ const threadsRead = (tabId: string, as: AuthUser | 'team' = owner) => get(`/api/super-tabs/${tabId}/threads`, as)
+ const post = async (url: string, as: AuthUser | 'team', payload: unknown) => {
+ const response = await app.inject({ method: 'POST', url, headers: { authorization: `Bearer ${as === 'team' ? 'team' : as.sub}` }, payload: payload as never })
+ return { status: response.statusCode, body: response.json() as { data: any; error?: { code: string } } }
+ }
+ const markThread = async (tabId: string, conversationId: string, root: string, sequence: number, as: AuthUser) =>
+ expect((await post(`/api/super-tabs/${tabId}/conversations/${conversationId}/threads/${root}/read`, as, { sequence })).status).toBe(200)
+ const follow = async (tabId: string, conversationId: string, root: string, value: boolean, as: AuthUser) =>
+ expect((await post(`/api/super-tabs/${tabId}/conversations/${conversationId}/threads/${root}/follow`, as, { follow: value })).status).toBe(200)
+ const newest = async (tabId: string, conversationId: string) =>
+ (await db.superTabMessage.findFirstOrThrow({ where: { tabId, conversationId }, orderBy: { sequence: 'desc' } })).sequence
+ /** The person's thread mark rows, by root id. */
+ const marksOf = async (tabId: string, as: AuthUser) => Object.fromEntries((await db.superTabThreadMark.findMany({ where: { tabId, personId: as.sub } }))
+ .map(r => [r.threadId, { sequence: r.sequence, follow: r.follow ?? null }]))
+ const unreadRoots = (data: any) => threadsOf(data).map(t => `${t.root}: ${t.unread}`).sort()
+
+ it('keeps a thread-only reply unread in the inbox and in Threads after the person reads the chat past it', async () => {
+ const s = await scene()
+ const [root] = await insert(s.tabId, s.web, [{ text: 'Peer root', author: personAuthor(peer) }])
+ await markRead(s.tabId, s.web, await sequenceOf(root!), peer)
+ await insert(s.tabId, s.web, [{ text: 'Thread-only reply', author: agentAuthor(s.worker), thread: root }])
+ const [main] = await insert(s.tabId, s.web, [{ text: 'Main message', author: personAuthor(owner) }])
+ await markRead(s.tabId, s.web, await sequenceOf(main!), peer)
+ expect(threadsOf((await inbox(s.tabId, peer)).body.data)).toEqual([{ chat: s.web, root: 'Peer root', unread: 1, last: s.worker }])
+ const listed = (await threadsRead(s.tabId, peer)).body.data
+ expect(listed.unread.map((t: any) => t.root.text)).toEqual(['Peer root'])
+ expect(listed.read).toEqual([])
+ // Read in the thread, it leaves the inbox and Threads lists it as read.
+ await markThread(s.tabId, s.web, root!, await newest(s.tabId, s.web), peer)
+ expect((await inbox(s.tabId, peer)).body.data.threads).toEqual([])
+ const after = (await threadsRead(s.tabId, peer)).body.data
+ expect(after.unread).toEqual([])
+ expect(after.read.map((t: any) => t.root.text)).toEqual(['Peer root'])
+ })
+
+ it('does so for each thread the person started, replied in, was tagged in or follows, and for no other', async () => {
+ const s = await scene()
+ const [started, replied, taggedRoot, taggedReply, followed, notTheirs] = await insert(s.tabId, s.web, [
+ { text: 'Started', author: personAuthor(peer) },
+ { text: 'Replied', author: personAuthor(owner) },
+ { text: 'Tagged root', author: personAuthor(owner), mentions: [peer.sub] },
+ { text: 'Tagged reply', author: personAuthor(owner) },
+ { text: 'Followed', author: personAuthor(owner) },
+ { text: 'Not theirs', author: personAuthor(owner) },
+ ])
+ await insert(s.tabId, s.web, [{ text: 'Peer answers', author: personAuthor(peer), thread: replied }])
+ await markRead(s.tabId, s.web, await newest(s.tabId, s.web), peer)
+ await follow(s.tabId, s.web, followed!, true, peer)
+ await insert(s.tabId, s.web, [...[started, replied, taggedRoot, followed, notTheirs].map(thread => ({ text: 'Worker reply', author: agentAuthor(s.worker), thread })),
+ { text: 'Worker tags peer', author: agentAuthor(s.worker), thread: taggedReply, mentions: [peer.sub] }])
+ await insert(s.tabId, s.web, [{ text: 'Main message', author: personAuthor(owner) }])
+ await markRead(s.tabId, s.web, await newest(s.tabId, s.web), peer)
+ expect(unreadRoots((await inbox(s.tabId, peer)).body.data)).toEqual(['Followed: 1', 'Replied: 1', 'Started: 1', 'Tagged reply: 1', 'Tagged root: 1'])
+ expect((await threadsRead(s.tabId, peer)).body.data.unread.map((t: any) => t.root.text).sort())
+ .toEqual(['Followed', 'Replied', 'Started', 'Tagged reply', 'Tagged root'])
+ expect(Object.keys(await marksOf(s.tabId, peer))).not.toContain(notTheirs)
+ })
+
+ it('leaves a thread the person already opened at its own mark, and writes none on a thread they unfollowed', async () => {
+ const s = await scene()
+ const [root, unfollowed] = await insert(s.tabId, s.web, [{ text: 'Opened', author: personAuthor(peer) }, { text: 'Unfollowed', author: personAuthor(peer) }])
+ await markRead(s.tabId, s.web, await sequenceOf(unfollowed!), peer)
+ await follow(s.tabId, s.web, unfollowed!, false, peer)
+ const [first] = await insert(s.tabId, s.web, [{ text: 'Reply 1', author: agentAuthor(s.worker), thread: root }])
+ await markThread(s.tabId, s.web, root!, await sequenceOf(first!), peer)
+ await insert(s.tabId, s.web, [{ text: 'Reply 2', author: agentAuthor(s.worker), thread: root }, { text: 'Main 1', author: personAuthor(owner) }])
+ await markRead(s.tabId, s.web, await newest(s.tabId, s.web), peer)
+ // The chat's mark is now past the thread's own: a later read of the chat must not raise the thread's to it.
+ await insert(s.tabId, s.web, [{ text: 'Reply 3', author: agentAuthor(s.worker), thread: root },
+ { text: 'Unfollowed reply', author: agentAuthor(s.worker), thread: unfollowed }, { text: 'Main 2', author: personAuthor(owner) }])
+ await markRead(s.tabId, s.web, await newest(s.tabId, s.web), peer)
+ expect(await marksOf(s.tabId, peer)).toEqual({ [root!]: { sequence: await sequenceOf(first!), follow: null }, [unfollowed!]: { sequence: 0, follow: false } })
+ expect(unreadRoots((await inbox(s.tabId, peer)).body.data)).toEqual(['Opened: 2'])
+ })
+
+ it('gives a Follow alone the chat\'s old mark', async () => {
+ const s = await scene()
+ const [root] = await insert(s.tabId, s.web, [{ text: 'Followed', author: personAuthor(owner) }])
+ const old = await sequenceOf(root!)
+ await markRead(s.tabId, s.web, old, peer)
+ await follow(s.tabId, s.web, root!, true, peer)
+ await insert(s.tabId, s.web, [{ text: 'Reply', author: agentAuthor(s.worker), thread: root }, { text: 'Main', author: personAuthor(owner) }])
+ await markRead(s.tabId, s.web, await newest(s.tabId, s.web), peer)
+ expect(await marksOf(s.tabId, peer)).toEqual({ [root!]: { sequence: old, follow: true } })
+ })
+
+ it('marks nothing in a chat the person never read: its threads wait for their first visit', async () => {
+ const s = await scene()
+ const [root] = await insert(s.tabId, s.web, [{ text: 'Peer root', author: personAuthor(peer) }])
+ await insert(s.tabId, s.web, [{ text: 'Reply', author: agentAuthor(s.worker), thread: root }, { text: 'Main', author: personAuthor(owner) }])
+ await markRead(s.tabId, s.web, await newest(s.tabId, s.web), peer)
+ expect(await marksOf(s.tabId, peer)).toEqual({})
+ expect((await inbox(s.tabId, peer)).body.data.threads).toEqual([])
+ })
+
+ it('writes no mark for a root or reply the person cannot read, and none through a chat they cannot read', async () => {
+ const s = await scene()
+ const [own] = await insert(s.tabId, s.web, [{ text: 'Peer root', author: personAuthor(peer) }])
+ await markRead(s.tabId, s.web, await sequenceOf(own!), peer)
+ const [hiddenRoot] = await insert(s.tabId, s.web, [{ text: 'Hidden root', author: agentAuthor('hidden-agent'), source: { kind: 'note', recipientsOnly: true },
+ recipientIds: [s.worker], mentions: [peer.sub] }])
+ await insert(s.tabId, s.web, [{ text: 'Reply under a hidden root', author: agentAuthor(s.worker), thread: hiddenRoot, mentions: [peer.sub] },
+ { text: 'Hidden reply', author: agentAuthor('hidden-agent'), thread: own, source: { kind: 'note', recipientsOnly: true }, recipientIds: [s.worker] },
+ { text: 'Main', author: personAuthor(owner) }])
+ await markRead(s.tabId, s.web, await newest(s.tabId, s.web), peer)
+ expect(await marksOf(s.tabId, peer)).toEqual({})
+ expect((await inbox(s.tabId, peer)).body.data.threads).toEqual([])
+ const first = await get(`/api/super-tabs/${s.tabId}`, peer)
+ expect(JSON.stringify(first.body.data?.derived ?? {})).not.toContain(hiddenRoot!)
+ // The owner's private project and peer's Buddy chat: refused alike to the person who cannot read them, writing nothing.
+ const [secretRoot] = await insert(s.tabId, s.secretGroup, [{ text: 'Secret root', author: personAuthor(owner), mentions: [peer.sub] }])
+ await insert(s.tabId, s.secretGroup, [{ text: 'Secret reply', author: agentAuthor(s.worker), thread: secretRoot, mentions: [peer.sub] }])
+ const [buddyRoot] = await insert(s.tabId, s.buddyId, [{ text: 'Buddy root', author: personAuthor(peer), mentions: [owner.sub] }])
+ await insert(s.tabId, s.buddyId, [{ text: 'Buddy reply', author: agentAuthor('buddy-agent'), thread: buddyRoot, mentions: [owner.sub] }])
+ const refusals = [await post(`/api/super-tabs/${s.tabId}/conversations/${s.secretGroup}/read`, peer, { sequence: 10_000 }),
+ await post(`/api/super-tabs/${s.tabId}/conversations/${s.buddyId}/read`, owner, { sequence: 10_000 }),
+ await post(`/api/super-tabs/${s.tabId}/conversations/${s.web}/read`, 'team', { sequence: 10_000 })]
+ expect(refusals.map(r => r.status)).toEqual([404, 404, 403])
+ expect(JSON.stringify(refusals)).not.toMatch(/Secret|Buddy/)
+ expect(await db.superTabThreadMark.count({ where: { tabId: s.tabId, threadId: { in: [secretRoot!, buddyRoot!, hiddenRoot!] } } })).toBe(0)
+ })
+
+ it('keeps the higher thread mark when the chat is read on two devices while the thread is opened on a third', async () => {
+ const s = await scene()
+ const [root] = await insert(s.tabId, s.web, [{ text: 'Peer root', author: personAuthor(peer) }])
+ await markRead(s.tabId, s.web, await sequenceOf(root!), peer)
+ const [reply1] = await insert(s.tabId, s.web, [{ text: 'Reply 1', author: agentAuthor(s.worker), thread: root }])
+ await insert(s.tabId, s.web, [{ text: 'Reply 2', author: agentAuthor(s.worker), thread: root }, { text: 'Main', author: personAuthor(owner) }])
+ const top = await newest(s.tabId, s.web)
+ await Promise.all([markRead(s.tabId, s.web, top, peer), markRead(s.tabId, s.web, top, peer), markThread(s.tabId, s.web, root!, await sequenceOf(reply1!), peer)])
+ expect(await marksOf(s.tabId, peer)).toEqual({ [root!]: { sequence: await sequenceOf(reply1!), follow: null } })
+ expect(unreadRoots((await inbox(s.tabId, peer)).body.data)).toEqual(['Peer root: 1'])
+ })
+
+ it(`reads one bounded range from the old mark upward (${SEED_SCAN} rows), whatever the range's length`, async () => {
+ const s = await scene()
+ const [early, late] = await insert(s.tabId, s.web, [{ text: 'Early', author: personAuthor(peer) }, { text: 'Late', author: personAuthor(peer) }])
+ await markRead(s.tabId, s.web, await sequenceOf(late!), peer)
+ await insert(s.tabId, s.web, [{ text: 'Early reply', author: agentAuthor(s.worker), thread: early },
+ ...Array.from({ length: SEED_SCAN + 100 }, (_, i) => ({ text: `Main ${i}`, author: personAuthor(owner) })),
+ { text: 'Late reply', author: agentAuthor(s.worker), thread: late }])
+ const top = await newest(s.tabId, s.web)
+ 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 { await markRead(s.tabId, s.web, top, peer) } finally { await db.$runCommandRaw({ profile: 0 }) }
+ const rows = (await db.$runCommandRaw({ aggregate: 'system.profile', pipeline: [{ $match: { ns: { $not: { $regex: 'system\\.' } } } }, ...PROFILE_STAGES],
+ cursor: {} }) as unknown as { cursor: { firstBatch: ProfileEntry[] } }).cursor.firstBatch.map(profileEntry)
+ expect(rows.filter(r => r.planSummary === 'COLLSCAN')).toEqual([])
+ const messages = rows.filter(r => r.ns.endsWith('.SuperTabMessage') && r.op !== 'getmore')
+ const examined = messages.reduce((n, r) => n + (r.docsExamined ?? 0), 0)
+ console.info(`mark-read seed cost: ${messages.length} message reads, ${examined} message docs examined, ${rows.filter(r => r.op !== 'getmore').length} operations\n` +
+ rows.filter(r => r.op !== 'getmore').map(r => ` ${r.ns.split('.').at(-1)} ${r.op} ${r.planSummary ?? ''} docs=${r.docsExamined ?? 0} keys=${r.keysExamined ?? 0}`).join('\n'))
+ // The range read, the thread's root and the look-back for whether the person is in it: never one read per row or thread.
+ expect(messages.length).toBeLessThanOrEqual(3)
+ expect(examined).toBeLessThanOrEqual(SEED_SCAN + 10)
+ // The cap: a thread-only reply past the first SEED_SCAN rows of the range is not seen by this read.
+ expect(await marksOf(s.tabId, peer)).toEqual({ [early!]: { sequence: await sequenceOf(late!), follow: null } })
+ }, 60_000)
+})
+
describe('the inbox\'s caps', () => {
it('cuts preview text to PREVIEW_CHARS characters in the database, counting a surrogate pair as one', async () => {
const s = await scene()
diff --git a/backend/src/superTabs/read/service.ts b/backend/src/superTabs/read/service.ts
index 5566abf90..ba4d297bc 100644
--- a/backend/src/superTabs/read/service.ts
+++ b/backend/src/superTabs/read/service.ts
@@ -34,6 +34,7 @@ import { cursorFor, readCursor } from './cursor.js'
import { changesFor } from './changes.js'
import { envelope, groupView, readerView, seenIn, type ReaderView } from './views.js'
import { decodeDormantPage, encodeDormantPage } from './dormantPage.js'
+import { seedThreadMarks } from './threadMarkSeeds.js'
import { memberSwarms } from '../swarmRows.js'
type LoadOptions = NonNullable<Parameters<typeof parts.loadActive>[2]>
@@ -199,28 +200,37 @@ export class SwarmReads {
}
async group(user: AuthUser, tabId: string, conversationId: string, asAgentId?: string): Promise<OneGroup> {
+ return { group: (await this.readGroup(user, tabId, conversationId, asAgentId)).group }
+ }
+
+ /** One conversation the reader can read, with the Swarm's state it was checked against. */
+ private async readGroup(user: AuthUser, tabId: string, conversationId: string, asAgentId?: string): Promise<OneGroup & { state: SuperTabState }> {
const t0 = this.now()
return this.read(user, tabId, async (tx, cached) => {
const { view, parts: loaded, actor } = await this.load(tx, cached, user, tabId, asAgentId, t0, { groupIds: [conversationId] })
const active = view.parts.groups.find(g => g.key === conversationId)
- if (active) return { group: active }
+ if (active) return { group: active, state: view.state }
// A dormant group is in no whole-Swarm view; it passes the same read check as on the dormant list.
const dormant = loaded.groups.find(g => g.key === conversationId && g.dormant)
if (dormant && canReadGroup(view.state, actor, dormant.data!)) {
const data = isBuddyReader(view.state, actor) ? groupForBuddy(dormant.data!) : dormant.data!
- return { group: envelope(dormant, groupView({ ...data, dormant: true }, seenIn(view.parts))) }
+ return { group: envelope(dormant, groupView({ ...data, dormant: true }, seenIn(view.parts))), state: view.state }
}
return fail('NOT_FOUND', 'This conversation is unavailable.', 404)
})
}
/** Moves the person's read mark in a conversation they can read forward, never past its newest message (readMarks.ts). Only a
- * person reading as themselves has marks: an agent, or a Team session, is refused before anything is written. */
+ * person reading as themselves has marks: an agent, or a Team session, is refused before anything is written. Reading the
+ * chat never reads a reply only in a thread: first, each such thread of theirs between the old mark and the new one with no
+ * mark of its own gets one at the old mark (threadMarkSeeds.ts), so the inbox and Threads still count it. */
async markRead(user: AuthUser, tabId: string, conversationId: string, sequence: number): Promise<{ sequence: number }> {
if (isTeamSession(user)) fail(TEAM_SESSION_LIMITED, TEAM_SESSION_MESSAGE, 403)
- const { group } = await this.group(user, tabId, conversationId)
- const newest = group.data!.lastMessageSequence
- return { sequence: await writeReadMark(this.db, { tabId, personId: user.sub, conversationId, sequence: Math.min(sequence, newest), now: this.now() }) }
+ const { group, state } = await this.readGroup(user, tabId, conversationId)
+ const next = Math.min(sequence, group.data!.lastMessageSequence), now = this.now()
+ const held = await this.db.superTabReadMark.findUnique({ where: { id: stableId(tabId, conversationId, user.sub) }, select: { sequence: true } })
+ await seedThreadMarks(this.db, { tabId, state, personId: user.sub, conversationId, old: held?.sequence ?? 0, next, now })
+ return { sequence: await writeReadMark(this.db, { tabId, personId: user.sub, conversationId, sequence: next, now }) }
}
/** Moves the person's mark of thread `threadId` in a conversation they can read forward, never past the conversation's
diff --git a/backend/src/superTabs/read/threadMarkSeeds.ts b/backend/src/superTabs/read/threadMarkSeeds.ts
new file mode 100644
index 000000000..a1e9f6eb8
--- /dev/null
+++ b/backend/src/superTabs/read/threadMarkSeeds.ts
@@ -0,0 +1,84 @@
+/**
+ * Reading a chat never reads a reply only in a thread (inbox.ts, the file's comment). The inbox and Threads count a thread from
+ * the person's own mark of it when that mark is behind the chat's (inbox.ts behindMarks), but a thread they never opened had
+ * none, so once the chat's mark passed its replies they were gone (Diego, 11-01). So before the chat's mark moves from `old` to
+ * `next`, each thread with a reply only in it in that range by someone else, that the person is in (started it, replied in it,
+ * was tagged in it, or follows it) and has no mark of, gets one at `old`: what they had read of the chat. A thread they opened
+ * keeps its own mark, and one they unfollowed gets none. A chat never marked read gets none: it counts as read (inbox.ts).
+ *
+ * Privacy: only rows a page shows the person count (inbox.ts readable): never an agent-only notice, a delivered copy, or a row
+ * of a project they no longer read in their Buddy chat. A thread whose root they cannot read gets no mark, so its id never
+ * reaches their marks. The caller has checked they read the chat, as a person reading as themselves.
+ *
+ * Cost: one read of the range's first SEED_SCAN rows from `old` upward by the (tabId, conversationId, sequence) index, answering
+ * only thread replies. Only when one is someone else's reply only in its thread: the delivered-copy check, the threads' mark rows
+ * and roots by id, the look-back for the person in at most SEED_THREADS threads (MEMBER_SCAN rows each, one read), then one
+ * conditional write per thread. A reply past the range's first SEED_SCAN rows, or in more than SEED_THREADS threads, is not
+ * marked; the server logs it.
+ */
+import type { PrismaClient } from '@prisma/client'
+import { logger } from '../../utils/logger.js'
+import { stableId, type Author, type SuperTabState } from '../model.js'
+import { seedThreadMark } from '../readMarks.js'
+import { readBy } from '../parts/indexed.js'
+import { BEHIND_THREADS, groupBy, isReply, MEMBER_SCAN, perRange, readable, row, SCAN_FIELDS, UNREAD_SCAN, type Row } from './inbox.js'
+
+/** Rows of the range one chat read looks at, from the old mark upward. */
+export const SEED_SCAN = 5 * UNREAD_SCAN
+/** Threads one chat read marks at most, newest reply first: as many as the inbox reads behind their chat's mark. */
+export const SEED_THREADS = BEHIND_THREADS
+
+export interface SeedInput { tabId: string; state: SuperTabState; personId: string; conversationId: string; old: number; next: number; now: number }
+
+/** A row is a thread reply: the root is another row. */
+const REPLY = { $and: [{ $ne: [{ $ifNull: ['$thread', null] }, null] }, { $ne: ['$thread', '$_id'] }] }
+
+/** Marks the threads described above and answers how many it marked. */
+export async function seedThreadMarks(db: PrismaClient, input: SeedInput): Promise<number> {
+ const { tabId, state, personId, conversationId, old, next } = input
+ if (old <= 0 || next <= old) return 0
+ const [scan] = await db.superTabMessage.aggregateRaw({ pipeline: [
+ { $match: { tabId, conversationId, sequence: { $gt: old, $lte: next } } }, { $sort: { sequence: 1, _id: 1 } }, { $limit: SEED_SCAN },
+ { $project: { ...SCAN_FIELDS, alsoInChat: 1 } },
+ { $group: { _id: null, scanned: { $sum: 1 }, last: { $last: '$sequence' }, replies: { $push: { $cond: [REPLY, '$$ROOT', '$$REMOVE'] } } } },
+ ] }) as unknown as Array<{ scanned: number; last: number; replies: Array<Record<string, unknown>> }>
+ if (!scan) return 0
+ if (scan.scanned >= SEED_SCAN && scan.last < next) logger.warn('[swarm-read-marks] thread replies past the scan not marked', { tabId, conversationId, old, next })
+ const own = (a: Pick<Author, 'kind' | 'id'>) => a.kind === 'person' && a.id === personId
+ const inIt = (m: Row) => own(m.author) || m.mentions.includes(personId)
+ const waits = (m: Row) => !m.alsoInChat && !own(m.author)
+ const rows = scan.replies.map(raw => row({ ...raw, conversationId }))
+ if (!rows.some(waits)) return 0
+ const shown = await readable(db, tabId, rows, state)
+ // Newest reply first.
+ const byThread = [...groupBy(shown.toReversed(), m => m.thread!)].filter(([, replies]) => replies.some(waits))
+ if (!byThread.length) return 0
+ if (byThread.length > SEED_THREADS) logger.warn('[swarm-read-marks] threads past the cap not marked', { tabId, conversationId, threads: byThread.length })
+ const threads = new Map(byThread.slice(0, SEED_THREADS))
+ // The person's rows of these threads, by id: one with a mark keeps it, an unfollowed one gets none.
+ const held = new Map((await db.superTabThreadMark.aggregateRaw({ pipeline: [
+ { $match: { _id: { $in: [...threads.keys()].map(thread => stableId(tabId, conversationId, thread, personId)) } } },
+ { $project: { _id: 0, threadId: 1, sequence: 1, follow: 1 } },
+ ] }) as unknown as Array<{ threadId: string; sequence: number; follow?: boolean | null }>).map(r => [r.threadId, r]))
+ const open = [...threads.keys()].filter(thread => { const mark = held.get(thread); return !mark || (Number(mark.sequence) <= 0 && mark.follow !== false) })
+ if (!open.length) return 0
+ // Each root by its id, in this chat, one the person reads.
+ const found = (await readBy(db, 'SuperTabMessage', 'id', [{ $match: { _id: { $in: open }, tabId, conversationId } }, { $project: { ...SCAN_FIELDS, conversationId: 1 } }])).map(row)
+ const roots = new Map((await readable(db, tabId, found.filter(m => !isReply(m)), state)).map(m => [m.id, m]))
+ const withRoot = open.filter(thread => roots.has(thread))
+ const member = new Set(withRoot.filter(thread => held.get(thread)?.follow === true || inIt(roots.get(thread)!) || threads.get(thread)!.some(inIt)))
+ // The rest: did the person write or get tagged before the old mark? Each thread's newest MEMBER_SCAN rows there, by its
+ // thread index range; only the rows that name them come back.
+ const unknown = withRoot.filter(thread => !member.has(thread))
+ if (unknown.length) {
+ const named = await perRange(db, unknown.map(thread => [{ $match: { tabId, conversationId, thread, sequence: { $lte: old } } },
+ { $sort: { sequence: -1 } }, { $limit: MEMBER_SCAN },
+ { $match: { $or: [{ 'author.kind': 'person', 'author.id': personId }, { mentions: personId }] } }, { $project: { ...SCAN_FIELDS, conversationId: 1 } }]))
+ for (const m of await readable(db, tabId, named, state)) member.add(m.thread!)
+ }
+ let marked = 0
+ for (const thread of withRoot.filter(t => member.has(t))) {
+ if (await seedThreadMark(db, { tabId, personId, conversationId, threadId: thread, sequence: old, now: input.now })) marked++
+ }
+ return marked
+}
diff --git a/backend/src/superTabs/readMarks.ts b/backend/src/superTabs/readMarks.ts
index 6dd2a7615..7482f513a 100644
--- a/backend/src/superTabs/readMarks.ts
+++ b/backend/src/superTabs/readMarks.ts
@@ -70,6 +70,29 @@ export async function writeThreadMark(db: PrismaClient, mark: ThreadMarkWrite):
return stored.sequence
}
+/** Gives one thread the person has no mark of a mark at `sequence` (read/threadMarkSeeds.ts: the chat's mark before a read
+ * moved it past a reply only in this thread), and answers whether it wrote one. A row a Follow alone wrote (sequence 0) takes
+ * it and keeps its Follow; a row with any mark keeps its own, lower or higher, so a thread opened on another device is never
+ * moved by a chat read. Each write matches only a row at 0 or creates one; when another device creates the row between the
+ * two, the match runs again on it. A new row counts toward THREAD_MARK_CAP as any mark. */
+export async function seedThreadMark(db: PrismaClient, mark: ThreadMarkWrite): Promise<boolean> {
+ const { tabId, personId, conversationId, threadId, sequence } = mark
+ const id = stableId(tabId, conversationId, threadId, personId), updatedAt = new Date(mark.now)
+ for (let attempt = 0; attempt < 4; attempt++) {
+ if ((await db.superTabThreadMark.updateMany({ where: { id, sequence: 0 }, data: { sequence, updatedAt } })).count) return true
+ if (await db.superTabThreadMark.findUnique({ where: { id }, select: { sequence: true } })) return false
+ try {
+ await db.superTabThreadMark.create({ data: { id, tabId, personId, conversationId, threadId, sequence, updatedAt } })
+ } catch (error) {
+ if (!(error instanceof Prisma.PrismaClientKnownRequestError) || error.code !== 'P2002') throw error
+ continue
+ }
+ await capThreadMarks(db, tabId, personId)
+ return true
+ }
+ return fail('READ_MARK_BUSY', 'This conversation is being marked read on another device. Try again.', 503)
+}
+
export interface ThreadFollowWrite { tabId: string; personId: string; conversationId: string; threadId: string; follow: boolean | null; now: number }
/** Stores the person's Follow (true), Unfollow (false) or neither (null) of one thread on its mark row, and answers it. The