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.

Threads: a thread read on any device reads as read everywhere (#2327)

0xken15f28bdbbf4071
diff --git a/backend/integration/superTabInbox.test.ts b/backend/integration/superTabInbox.test.ts
index 431a9ee33..8681f2844 100644
--- a/backend/integration/superTabInbox.test.ts
+++ b/backend/integration/superTabInbox.test.ts
@@ -378,6 +378,28 @@ describe('thread unread as Desktop marks it (controller.dart threadUnread)', ()
expect(threadsOf((await inbox(s.tabId, peer)).body.data)).toEqual([{ chat: s.web, root: 'Peer root', unread: 1, last: s.worker }])
})
+ it('counts only replies past the person\'s mark of the thread, read on any device, and drops a thread read to its end', 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 [first] = await insert(s.tabId, s.web, [{ text: 'Worker 1', author: agentAuthor(s.worker), thread: root }])
+ await insert(s.tabId, s.web, [{ text: 'Worker 2', author: agentAuthor(s.worker), thread: root }])
+ const markThread = async (sequence: number, as: AuthUser) => {
+ const response = await app.inject({ method: 'POST', url: `/api/super-tabs/${s.tabId}/conversations/${s.web}/threads/${root}/read`,
+ headers: { authorization: `Bearer ${as.sub}` }, payload: { sequence } })
+ expect(response.statusCode).toBe(200)
+ }
+ await markThread(await sequenceOf(first!), peer)
+ expect(threadsOf((await inbox(s.tabId, peer)).body.data)).toEqual([{ chat: s.web, root: 'Peer root', unread: 1, last: s.worker }])
+ // Another person's thread mark never reads it for them.
+ await markThread(10_000, owner)
+ expect(threadsOf((await inbox(s.tabId, peer)).body.data)).toEqual([{ chat: s.web, root: 'Peer root', unread: 1, last: s.worker }])
+ await markThread(10_000, peer)
+ expect((await inbox(s.tabId, peer)).body.data.threads).toEqual([])
+ await insert(s.tabId, s.web, [{ text: 'Worker 3', author: agentAuthor(s.worker), thread: root }])
+ expect(threadsOf((await inbox(s.tabId, peer)).body.data)).toEqual([{ chat: s.web, root: 'Peer root', unread: 1, last: s.worker }])
+ })
+
it('leaves archived chats out of the latest messages and the threads', async () => {
const s = await busyScene()
await editChat(s.tabId, s.web, data => { data.archived = true })
@@ -519,10 +541,11 @@ describe('what the inbox costs the database', () => {
`${docsExamined} docs examined (${messageDocs} messages), ${bytesFromDatabase} bytes from the database, ${replyBytes} reply bytes\n` +
reads.map(r => ` ${r.ns.split('.').at(-1)} ${r.op} ${r.planSummary ?? ''} docs=${r.docsExamined ?? 0}`).join('\n'))
// A fixed few reads whatever the chat count: the latest rows, the unread rows, their roots, the look-back for threads
- // whose newer rows do not show the person (the read marks are another collection). Nothing new to check for delivered
- // copies here, so no read for them.
+ // whose newer rows do not show the person (the read marks are another collection, and the person's thread marks of the
+ // scanned chats one more indexed read). Nothing new to check for delivered copies here, so no read for them.
expect(messageReads.length).toBeLessThanOrEqual(4)
- expect(reads.length).toBeLessThanOrEqual(6)
+ expect(reads.length).toBeLessThanOrEqual(7)
+ expect(rows.filter(r => r.ns.endsWith('.SuperTabThreadMark') && r.op !== 'getmore')).toHaveLength(1)
// Bounded by the caps, never by history: the long chat's rows are read only to its newest UNREAD_SCAN + 1, and each of its
// threads the person is not in only to MEMBER_SCAN rows before the mark (not its 301).
expect(messageDocs).toBeLessThan(2_000)
diff --git a/backend/integration/superTabReadMarks.test.ts b/backend/integration/superTabReadMarks.test.ts
index 7b997aa0e..2fcdf5e44 100644
--- a/backend/integration/superTabReadMarks.test.ts
+++ b/backend/integration/superTabReadMarks.test.ts
@@ -21,6 +21,8 @@ import { stableId } from '../src/superTabs/model.js'
import { HostReads } from '../src/superTabs/read/host.js'
import type { Conversation, SuperTabState } from '../src/superTabs/model.js'
import { writeLegacySwarm } from '../scripts/swarm-parts/legacy.js'
+import { READ_MARK_EXPIRY_MS, sweepThreadMarks, THREAD_MARK_CAP, writeThreadMark } from '../src/superTabs/readMarks.js'
+import { listNamedIndexes, NAMED_INDEX } from '../src/superTabs/parts/indexed.js'
import { NOW, outsider as outsiderActor, owner as ownerActor, peer as peerActor, swarm } from '../src/superTabs/read/fixture.test-support.js'
// A fresh loopback replica set, created here. This suite never reads DATABASE_URL or an existing database.
@@ -44,6 +46,8 @@ beforeAll(async () => {
})
db = new PrismaClient({ datasources: { db: { url: uri } } })
for (const each of [owner, peer, outsider]) await db.user.create({ data: { id: each.sub, externalId: `fixture-${each.sub}`, email: each.email, autonomousEnv: 'prod' } })
+ // The owner's machine, where the fixture's worker runs (reading as that agent).
+ await db.machine.create({ data: { machineId: 'host', userId: owner.sub, apiKey: 'fixture-only', managerId: 'fixture', autonomousEnv: 'prod', authMode: 'remote' } })
const store = new SuperTabStore(db, () => clock)
app = Fastify()
app.setErrorHandler(errorHandler)
@@ -71,6 +75,7 @@ async function scene() {
const secret = s.run({ type: 'project.create', name: 'Secret', visibility: 'private', brief: '' }).projectId as string
const web = s.group('Web')
const secretGroup = s.group('Secret work', secret)
+ const worker = s.add('worker', web, false, ownerActor, 'host', owner.sub)
s.edit(state => {
state.conversations.find(c => c.id === web)!.lastMessageSequence = 100
state.conversations.find(c => c.id === secretGroup)!.lastMessageSequence = 50
@@ -79,7 +84,7 @@ async function scene() {
await writeLegacySwarm(db, { state: { ...structuredClone(s.state), id: tabId } as SuperTabState, revision: 3 })
const report = await migrateSwarm(db, tabId, { now: () => MIGRATED })
if (!report.ok) throw new Error(report.error)
- return { tabId, web, secretGroup }
+ return { tabId, web, secretGroup, worker }
}
const call = async (method: 'GET' | 'POST', url: string, as: AuthUser, payload?: unknown) => {
@@ -263,3 +268,246 @@ describe('catch-up summary (what the reader missed since their mark)', () => {
expect((await call('GET', `/api/super-tabs/${s.tabId}/conversations/${s.web}/catch-up`, owner)).status).toBe(400)
})
})
+
+describe('thread marks (one per person and thread)', () => {
+ /** A thread rooted at `root` in `conversationId`: the root, then `replies` replies numbered after it. */
+ const thread = (tabId: string, conversationId: string, root: string, at: number, replies = 2) =>
+ db.superTabMessage.createMany({ data: Array.from({ length: replies + 1 }, (_, i) => ({ id: i ? `${root}-r${i}` : root, tabId, conversationId,
+ sequence: at + i, thread: root, author: { kind: 'agent', id: 'lead-agent' }, text: i ? `Reply ${i}` : 'Root', recipientIds: [], mentions: [] })) })
+ const scenario = async () => {
+ const s = await scene()
+ const root = `${s.tabId}-root`, secretRoot = `${s.tabId}-secret-root`, agentRoot = `${s.tabId}-agent-root`
+ await thread(s.tabId, s.web, root, 10)
+ await thread(s.tabId, s.secretGroup, secretRoot, 20)
+ // An agent-only notice to the Lead, with a reply: no person reads it.
+ await db.superTabMessage.createMany({ data: [agentRoot, `${agentRoot}-r1`].map((id, i) => ({ id, tabId: s.tabId, conversationId: s.web, sequence: 30 + i,
+ thread: agentRoot, author: { kind: 'agent', id: 'lead-agent' }, text: 'For the Lead', recipientIds: ['lead-agent'], mentions: [],
+ source: { kind: 'agent-only' } })) })
+ return { ...s, root, secretRoot, agentRoot }
+ }
+ const markThread = (tabId: string, conversationId: string, root: string, sequence: number, as: AuthUser = owner, query = '') =>
+ call('POST', `/api/super-tabs/${tabId}/conversations/${conversationId}/threads/${root}/read${query}`, as, { sequence })
+ const threadMarksOf = async (tabId: string, as: AuthUser, query = '') =>
+ (await call('GET', `/api/super-tabs/${tabId}/parts${query}`, as)).body.data.derived.threadMarks as Record<string, Record<string, number>> | undefined
+
+ it('keeps the person\'s thread mark and gives it back in their own read only', async () => {
+ const s = await scenario()
+ expect(await markThread(s.tabId, s.web, s.root, 11)).toMatchObject({ status: 200, body: { data: { sequence: 11 } } })
+ expect(await threadMarksOf(s.tabId, owner)).toEqual({ [s.web]: { [s.root]: 11 } })
+ // Another reader of the same thread never sees it, and a person with none gets no field at all.
+ expect(await threadMarksOf(s.tabId, peer)).toBeUndefined()
+ await markThread(s.tabId, s.web, s.root, 12, peer)
+ expect(await threadMarksOf(s.tabId, peer)).toEqual({ [s.web]: { [s.root]: 12 } })
+ expect(await threadMarksOf(s.tabId, owner)).toEqual({ [s.web]: { [s.root]: 11 } })
+ // An agent reading as itself (a Buddy is one) has none, though its owner has.
+ const asAgent = await call('GET', `/api/super-tabs/${s.tabId}/parts?asAgentId=${s.worker}`, owner)
+ expect(asAgent.status, JSON.stringify(asAgent.body.error)).toBe(200)
+ expect(asAgent.body.data.derived.threadMarks).toBeUndefined()
+ })
+
+ it('never moves a thread mark back, whichever device writes last', async () => {
+ const s = await scenario()
+ await markThread(s.tabId, s.web, s.root, 12)
+ expect((await markThread(s.tabId, s.web, s.root, 10)).body.data).toEqual({ sequence: 12 })
+ expect(await threadMarksOf(s.tabId, owner)).toEqual({ [s.web]: { [s.root]: 12 } })
+ const other = await scenario()
+ await Promise.all([10, 12, 11, 10, 12].map(n => markThread(other.tabId, other.web, other.root, n)))
+ expect(await threadMarksOf(other.tabId, owner)).toEqual({ [other.web]: { [other.root]: 12 } })
+ expect(await db.superTabThreadMark.count({ where: { tabId: other.tabId } })).toBe(1)
+ })
+
+ it('stops at the conversation\'s newest message', async () => {
+ const s = await scenario()
+ expect((await markThread(s.tabId, s.web, s.root, 5_000)).body.data).toEqual({ sequence: 100 })
+ })
+
+ it('leaves the conversation\'s own mark as it was: a client without thread marks reads the same', async () => {
+ const s = await scenario()
+ await mark(s.tabId, s.web, 5)
+ await markThread(s.tabId, s.web, s.root, 12)
+ expect(await marksOf(s.tabId, owner)).toEqual({ [s.web]: 5 })
+ expect(await db.superTabReadMark.count({ where: { tabId: s.tabId } })).toBe(1)
+ })
+
+ it('refuses a thread of another conversation, an unknown one and one the person cannot read alike, storing nothing', async () => {
+ const s = await scenario()
+ const refused = [
+ await markThread(s.tabId, s.secretGroup, s.secretRoot, 21, outsider),
+ // The private chat's thread named under a conversation the person reads.
+ await markThread(s.tabId, s.web, s.secretRoot, 21, outsider),
+ await markThread(s.tabId, s.web, 'no-such-root', 21, outsider),
+ await markThread(s.tabId, 'no-such-group', s.root, 21, outsider),
+ // A reply is no thread's root; an agent-only notice is no person's.
+ await markThread(s.tabId, s.web, `${s.root}-r1`, 21, outsider),
+ await markThread(s.tabId, s.web, s.agentRoot, 31, outsider),
+ ]
+ expect(refused.map(r => r.status)).toEqual([404, 404, 404, 404, 404, 404])
+ expect(new Set(refused.map(r => JSON.stringify(r.body.error)))).toHaveLength(1)
+ expect(JSON.stringify(refused)).not.toContain('Secret')
+ expect(await db.superTabThreadMark.count({ where: { tabId: s.tabId } })).toBe(0)
+ })
+
+ it('stops sending a thread mark once the person can no longer read its conversation', async () => {
+ const s = await scenario()
+ await markThread(s.tabId, s.web, s.root, 11, peer)
+ expect(await threadMarksOf(s.tabId, peer)).toEqual({ [s.web]: { [s.root]: 11 } })
+ await swarmTransaction(db, async tx => {
+ const current = (await parts.loadActive(tx, s.tabId, { groupIds: [s.web] })).groups.find(g => g.key === s.web)!
+ const data = { ...structuredClone(current.data!), audience: { people: [{ id: owner.sub, generation: 0 }] } } as Conversation
+ await guardedWrite(tx, s.tabId, { op: 'update', kind: 'group', key: s.web, seen: current.version, data }, clock)
+ })
+ expect(await threadMarksOf(s.tabId, peer)).toBeUndefined()
+ expect((await markThread(s.tabId, s.web, s.root, 12, peer)).status).toBe(404)
+ })
+
+ it('never sends a change read a thread mark of a conversation the reader lost', async () => {
+ const s = await scenario()
+ clock = NOW
+ const cursor = (await firstLoad(s.tabId, peer)).cursor as string
+ clock = NOW + 1_000
+ await markThread(s.tabId, s.web, s.root, 11, peer)
+ await swarmTransaction(db, async tx => {
+ const current = (await parts.loadActive(tx, s.tabId, { groupIds: [s.web] })).groups.find(g => g.key === s.web)!
+ const data = { ...structuredClone(current.data!), audience: { people: [{ id: owner.sub, generation: 0 }] } } as Conversation
+ await guardedWrite(tx, s.tabId, { op: 'update', kind: 'group', key: s.web, seen: current.version, data }, clock)
+ })
+ clock = NOW + 2_000
+ const changed = (await call('GET', `/api/super-tabs/${s.tabId}/changes?cursor=${cursor}`, peer)).body.data
+ expect(JSON.stringify(changed.derived ?? {})).not.toContain(s.root)
+ clock = NOW
+ })
+
+ it('keeps no thread marks for agents or Team sessions', async () => {
+ const s = await scenario()
+ expect((await markThread(s.tabId, s.web, s.root, 11, owner, '?asAgentId=some-agent')).status).toBe(400)
+ expect((await markThread(s.tabId, s.web, s.root, 11, ownerTeam)).status).toBe(403)
+ expect(await db.superTabThreadMark.count({ where: { tabId: s.tabId } })).toBe(0)
+ })
+
+ it('sends a later read only the thread marks that moved since its cursor', async () => {
+ const s = await scenario()
+ clock = NOW
+ await markThread(s.tabId, s.secretGroup, s.secretRoot, 21)
+ clock = NOW + 60_000
+ const cursor = (await firstLoad(s.tabId, owner)).cursor as string
+ const changes = async (at: number) => {
+ clock = at
+ return (await call('GET', `/api/super-tabs/${s.tabId}/changes?cursor=${cursor}`, owner)).body.data.derived.threadMarks
+ }
+ expect(await changes(NOW + 61_000)).toBeUndefined()
+ clock = NOW + 62_000
+ await markThread(s.tabId, s.web, s.root, 12)
+ expect(await changes(NOW + 63_000)).toEqual({ [s.web]: { [s.root]: 12 } })
+ clock = NOW
+ })
+
+ it(`keeps at most ${THREAD_MARK_CAP} per person and Swarm, dropping the least recently moved`, async () => {
+ const s = await scenario()
+ const write = (threadId: string, at: number, personId = owner.sub) =>
+ writeThreadMark(db, { tabId: s.tabId, personId, conversationId: s.web, threadId, sequence: 1, now: at })
+ for (let i = 0; i < THREAD_MARK_CAP; i++) await write(`t${i}`, NOW + i)
+ // Another person's marks count apart.
+ await write('t0', NOW, peer.sub)
+ // t0 moves on, so t1 is now the least recently moved.
+ await writeThreadMark(db, { tabId: s.tabId, personId: owner.sub, conversationId: s.web, threadId: 't0', sequence: 2, now: NOW + THREAD_MARK_CAP })
+ await write('newest', NOW + THREAD_MARK_CAP + 1)
+ const kept = await db.superTabThreadMark.findMany({ where: { tabId: s.tabId, personId: owner.sub }, select: { threadId: true } })
+ expect(kept).toHaveLength(THREAD_MARK_CAP)
+ expect(kept.map(k => k.threadId)).toEqual(expect.arrayContaining(['t0', 'newest']))
+ expect(kept.map(k => k.threadId)).not.toContain('t1')
+ expect(await db.superTabThreadMark.count({ where: { tabId: s.tabId, personId: peer.sub } })).toBe(1)
+ }, 60_000)
+
+ it('sweeps marks untouched for 180 days and those of Swarms and people gone, keeping every live one', async () => {
+ const s = await scenario()
+ const write = (threadId: string, at: number, personId = owner.sub, tabId = s.tabId) =>
+ writeThreadMark(db, { tabId, personId, conversationId: s.web, threadId, sequence: 1, now: at })
+ const now = NOW + READ_MARK_EXPIRY_MS + 60_000
+ await write('old', NOW)
+ await write('fresh', now - 1_000)
+ await write('peers', now - 1_000, peer.sub)
+ // Someone no longer in the Swarm, and a Swarm that is gone.
+ await write('left', now - 1_000, 'person-who-left')
+ await write('deleted-swarm', now - 1_000, owner.sub, `${s.tabId}-deleted`)
+ const result = await sweepThreadMarks(db, now, { walk: { after: '' }, swarms: 100_000 })
+ const kept = await db.superTabThreadMark.findMany({ where: { tabId: { startsWith: s.tabId } }, select: { threadId: true } })
+ expect(kept.map(k => k.threadId).sort()).toEqual(['fresh', 'peers'])
+ expect(result.expired).toBeGreaterThanOrEqual(1)
+ expect(result.lost).toBeGreaterThanOrEqual(2)
+ })
+
+ /** Marks in Swarms named `${prefix}-${name}`, one person each, moved now. */
+ const marksIn = async (prefix: string, names: string[]) => {
+ for (const name of names) await writeThreadMark(db, { tabId: `${prefix}-${name}`, personId: owner.sub, conversationId: 'c', threadId: 't', sequence: 1, now: NOW })
+ }
+ const swarmsWithMarks = async (prefix: string) =>
+ [...new Set((await db.superTabThreadMark.findMany({ where: { tabId: { startsWith: prefix } }, select: { tabId: true } })).map(m => m.tabId.slice(prefix.length + 1)))].sort()
+
+ it('never reads a Swarm row without its member list as having no members: only a row that is gone loses its marks', async () => {
+ const prefix = `zz-sweep-${randomUUID()}`
+ await marksIn(prefix, ['gone', 'no-members'])
+ // A Swarm row stored without memberIds (an older or partial row), written as is.
+ await db.$runCommandRaw({ insert: 'SuperTab', documents: [{ _id: `${prefix}-no-members`, ownerId: owner.sub, autonomousEnv: 'prod', state: {} }] })
+ const result = await sweepThreadMarks(db, NOW, { walk: { after: prefix }, swarms: 10 })
+ expect(await swarmsWithMarks(prefix)).toEqual(['no-members'])
+ expect(result.swarmsChecked).toBe(2)
+ })
+
+ it('checks a bounded number of Swarms a pass, going on from where the last pass stopped and starting over after the last', async () => {
+ const prefix = `zz-walk-${randomUUID()}`
+ await marksIn(prefix, ['a', 'b', 'c'])
+ const at = { after: prefix }
+ expect((await sweepThreadMarks(db, NOW, { walk: at, swarms: 2 })).swarmsChecked).toBe(2)
+ expect(await swarmsWithMarks(prefix)).toEqual(['c'])
+ expect(at.after).toBe(`${prefix}-b`)
+ await sweepThreadMarks(db, NOW, { walk: at, swarms: 2 })
+ expect(await swarmsWithMarks(prefix)).toEqual([])
+ // Past the last Swarm with marks, the next pass starts from the first.
+ expect(at.after).toBe('')
+ })
+
+ it('keeps its place in the database: a restarted worker goes on from there, and a Swarm whose check failed is checked again', async () => {
+ const prefix = `zz-restart-${randomUUID()}`
+ await marksIn(prefix, ['a', 'b', 'c'])
+ await db.superTabSweepCursor.upsert({ where: { id: 'thread-marks' }, create: { id: 'thread-marks', after: prefix, updatedAt: new Date(NOW) },
+ update: { after: prefix } })
+ // The first process checks two Swarms, then stops; nothing of it stays in memory.
+ expect((await sweepThreadMarks(db, NOW, { swarms: 2 })).swarmsChecked).toBe(2)
+ expect((await db.superTabSweepCursor.findUniqueOrThrow({ where: { id: 'thread-marks' } })).after).toBe(`${prefix}-b`)
+ // The next one fails while deleting c's marks: its place stays before c.
+ const failing = new Proxy(db, { get: (target, key) => key === '$runCommandRaw'
+ ? async (command: { deletes?: Array<{ q: { tabId?: string } }> }) => {
+ if (command.deletes?.some(d => d.q.tabId === `${prefix}-c`)) throw new Error('connection reset')
+ return target.$runCommandRaw(command as never)
+ }
+ : Reflect.get(target, key) }) as PrismaClient
+ await expect(sweepThreadMarks(failing, NOW, { swarms: 2 })).rejects.toThrow('connection reset')
+ expect((await db.superTabSweepCursor.findUniqueOrThrow({ where: { id: 'thread-marks' } })).after).toBe(`${prefix}-b`)
+ expect(await swarmsWithMarks(prefix)).toEqual(['c'])
+ await sweepThreadMarks(db, NOW, { swarms: 2 })
+ expect(await swarmsWithMarks(prefix)).toEqual([])
+ })
+
+ it('reads a person\'s thread marks through their named indexes, planned once', async () => {
+ const s = await scenario()
+ await markThread(s.tabId, s.web, s.root, 11)
+ await listNamedIndexes(db)
+ await db.$runCommandRaw({ planCacheClear: 'SuperTabThreadMark' })
+ 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 {
+ const cursor = (await firstLoad(s.tabId, owner)).cursor as string
+ await call('GET', `/api/super-tabs/${s.tabId}/changes?cursor=${cursor}`, owner)
+ } finally {
+ await db.$runCommandRaw({ profile: 0 })
+ }
+ const rows = (await db.$runCommandRaw({ find: 'system.profile', filter: { ns: { $regex: '\\.SuperTabThreadMark$' } }, batchSize: 1000 }) as unknown as
+ { cursor: { firstBatch: Array<{ command?: { hint?: unknown }; planSummary?: string; fromMultiPlanner?: boolean }> } }).cursor.firstBatch
+ const hints = rows.map(row => JSON.stringify(row.command?.hint))
+ expect(rows.length).toBeGreaterThanOrEqual(2)
+ expect(new Set(hints)).toEqual(new Set([JSON.stringify(NAMED_INDEX.SuperTabThreadMark.person)]))
+ expect(rows.every(row => row.planSummary?.startsWith('IXSCAN') && !row.fromMultiPlanner), JSON.stringify(rows.map(r => r.planSummary))).toBe(true)
+ })
+})
diff --git a/backend/prisma/schema.prisma b/backend/prisma/schema.prisma
index 172b2cdc9..da4bd2287 100644
--- a/backend/prisma/schema.prisma
+++ b/backend/prisma/schema.prisma
@@ -1163,6 +1163,34 @@ model SuperTabReadMark {
@@index([updatedAt], map: "read_mark_expiry")
}
+/// How far a person has read one thread: the newest reply sequence they saw there (superTabs/readMarks.ts). Apart from
+/// SuperTabReadMark so a backend without thread marks never reads one as a conversation's mark. At most THREAD_MARK_CAP per
+/// person and Swarm; the worker's hourly sweep deletes those untouched for 180 days and those of Swarms and people gone.
+model SuperTabThreadMark {
+ id String @id @map("_id")
+ tabId String
+ personId String
+ conversationId String
+ /// The thread's root message id.
+ threadId String
+ sequence Int
+ updatedAt DateTime
+
+ /// A person's marks: every one (a first load, the inbox, the cap's count and oldest) or those moved since a change read's
+ /// cursor, and each Swarm's people with marks (the sweep) (parts/indexed.ts NAMED_INDEX).
+ @@index([tabId, personId, updatedAt])
+ /// The sweep's marks untouched for 180 days (readMarks.ts sweepThreadMarks); no TTL option, which the app user cannot set.
+ @@index([updatedAt])
+}
+
+/// Where a Swarm sweep reads on from between passes and releases, one row per sweep (readMarks.ts sweepThreadMarks: id
+/// "thread-marks", the last Swarm whose people check finished). Written by the singleton worker alone.
+model SuperTabSweepCursor {
+ id String @id @map("_id")
+ after String
+ updatedAt DateTime
+}
+
/// A question to one person, with options and a recommendation (backend/src/superTabs/decisions.ts). Kept out of the tab
/// state, as mentions are: an asking agent and the asked person read it, and the person answers it once.
model SuperTabDecision {
diff --git a/backend/src/routes/superTabs.ts b/backend/src/routes/superTabs.ts
index a494dfc2b..b287bf602 100644
--- a/backend/src/routes/superTabs.ts
+++ b/backend/src/routes/superTabs.ts
@@ -66,6 +66,7 @@ const nativeParams = tabParams.extend({ contextId: id })
const messageParams = tabParams.extend({ conversationId: id })
// A person's own read mark (superTabs/readMarks.ts): no asAgentId, since agents keep none.
const readMarkBody = z.object({ sequence: z.number().int().nonnegative().safe() }).strict()
+const threadParams = messageParams.extend({ threadId: id })
const noQuery = z.object({}).strict()
// The reader's own catch-up (superTabs/catchUp.ts): no asAgentId, since agents read every message as it comes.
const catchUpQuery = z.object({ after: z.coerce.number().int().nonnegative().safe() }).strict()
@@ -517,6 +518,10 @@ export async function superTabRoutes(app: FastifyInstance, options: { store?: Su
app.post<{ Params: z.infer<typeof messageParams>; Querystring: z.infer<typeof noQuery>; Body: z.infer<typeof readMarkBody> }>('/api/super-tabs/:tabId/conversations/:conversationId/read',
{ preHandler: [validateParams(messageParams), validateQuery(noQuery), validateBody(readMarkBody)] },
(req, reply) => respond(reply, () => reads.markRead(req.user!, req.params.tabId, req.params.conversationId, req.body.sequence)))
+ // A person's own thread mark (superTabs/readMarks.ts), as the conversation's above.
+ app.post<{ Params: z.infer<typeof threadParams>; Querystring: z.infer<typeof noQuery>; Body: z.infer<typeof readMarkBody> }>('/api/super-tabs/:tabId/conversations/:conversationId/threads/:threadId/read',
+ { preHandler: [validateParams(threadParams), validateQuery(noQuery), validateBody(readMarkBody)] },
+ (req, reply) => respond(reply, () => reads.markThreadRead(req.user!, req.params.tabId, req.params.conversationId, req.params.threadId, req.body.sequence)))
app.get<{ Params: z.infer<typeof messageParams>; Querystring: z.infer<typeof catchUpQuery> }>('/api/super-tabs/:tabId/conversations/:conversationId/catch-up',
{ preHandler: [validateParams(messageParams), validateQuery(catchUpQuery)] },
(req, reply) => respond(reply, () => store.catchUp(req.user!, req.params.tabId, req.params.conversationId, req.query.after)))
diff --git a/backend/src/superTabs/parts/indexed.ts b/backend/src/superTabs/parts/indexed.ts
index 8c21c1fc9..671df9c7b 100644
--- a/backend/src/superTabs/parts/indexed.ts
+++ b/backend/src/superTabs/parts/indexed.ts
@@ -65,6 +65,12 @@ export const NAMED_INDEX = {
/** An agent's deliveries by status and age: its open ones, oldest first. */
agentOpen: { tabId: 1, agentId: 1, status: 1, createdAt: 1 },
},
+ /** A person's thread marks (readMarks.ts): `person`, every one or those moved since a cursor, the cap's count and oldest,
+ * and the sweep's people per Swarm; `age`, the sweep's marks untouched for 180 days. */
+ SuperTabThreadMark: {
+ person: { tabId: 1, personId: 1, updatedAt: 1 },
+ age: { updatedAt: 1 },
+ },
} as const
export type NamedCollection = keyof typeof NAMED_INDEX
export type NamedIndex<C extends NamedCollection> = keyof typeof NAMED_INDEX[C] & string
diff --git a/backend/src/superTabs/read/contract.ts b/backend/src/superTabs/read/contract.ts
index 50fabf690..d68405dec 100644
--- a/backend/src/superTabs/read/contract.ts
+++ b/backend/src/superTabs/read/contract.ts
@@ -82,6 +82,10 @@ export interface Derived {
* every one of the conversations it holds; a changes read only those moved since its cursor, kept by the client as the
* higher of the two. Never another person's. */
readMarks?: Record<string, number>
+ /** A person only: their own thread marks (readMarks.ts), the newest message sequence read by thread root, by conversation.
+ * Like `readMarks`: a first load has those of the conversations it holds, a changes read those moved since its cursor.
+ * Absent when there are none, and from an older backend. Never another person's. */
+ threadMarks?: Record<string, Record<string, number>>
/** Present, and false, only in a Swarm Harness Team accounts are not tried in yet (machines.ts teamAccountSwarm): this
* Mac's daemon and Desktop then offer no team account for it. Absent is every older backend's "yes". */
teamAccounts?: false
diff --git a/backend/src/superTabs/read/inbox.ts b/backend/src/superTabs/read/inbox.ts
index cd7635f31..e253f5c08 100644
--- a/backend/src/superTabs/read/inbox.ts
+++ b/backend/src/superTabs/read/inbox.ts
@@ -7,7 +7,7 @@
* A thread is unread as Desktop marks it (desktop super_tabs/controller.dart threadUnread): the person started it, replied in
* it or was tagged in it; its newest reply is someone else's; and it has replies by others after both the person's read mark
* on the chat and their own newest reply there. A chat they never marked read counts as read, as Desktop opens it at its end.
- * Desktop also keeps a seen mark per thread on the device; the server has none yet, so the chat's mark stands in for it.
+ * A thread the person read on any device (their thread mark, readMarks.ts) counts only replies past that mark too.
*
* Cost: every message read is bounded by an index range and a limit: the newest UNREAD_SCAN rows past the mark in at most
* THREAD_CHATS chats; the roots of the newest CANDIDATE_THREADS threads; MEMBER_SCAN rows before the mark of a thread whose
@@ -23,7 +23,7 @@ import { agentOnly, author, fail, SuperTabError, type Actor, type Author, type C
import { readConversation } from '../projects.js'
import { deliveredCopies, type ThreadReply } from '../pageReads.js'
import { threadSummaries } from '../threads.js'
-import { readMarks } from '../readMarks.js'
+import { readMarks, readThreadMarks } from '../readMarks.js'
import { readBy } from '../parts/indexed.js'
import { ms } from '../parts/light.js'
import type { SuperTabStore } from '../store.js'
@@ -163,8 +163,8 @@ interface Candidate { conversationId: string; root: string; mark: number; replie
/** The threads of `chats` the person has unread replies in (see the file's comment), from each chat's newest UNREAD_SCAN rows
* past their mark. */
-async function threadsIn(db: PrismaClient, tabId: string, state: SuperTabState, chats: Conversation[], personId: string, marks: Map<string, number>):
- Promise<{ threads: InboxThread[]; more: boolean }> {
+async function threadsIn(db: PrismaClient, tabId: string, state: SuperTabState, chats: Conversation[], personId: string, marks: Map<string, number>,
+ threadMarks: Map<string, Map<string, number>>): Promise<{ threads: InboxThread[]; more: boolean }> {
if (!chats.length) return { threads: [], more: false }
const own = (a: Pick<Author, 'kind' | 'id'>) => a.kind === 'person' && a.id === personId
const inIt = (m: Row) => own(m.author) || m.mentions.includes(personId)
@@ -180,8 +180,10 @@ async function threadsIn(db: PrismaClient, tabId: string, state: SuperTabState,
const newest = rows[0]!
// The person answered last: nothing in it waits for them.
if (own(newest.author)) continue
- const answered = rows.find(m => own(m.author))?.sequence ?? 0
- const unread = rows.filter(m => !own(m.author) && m.sequence > answered).length
+ // Read through the later of their own newest reply and their mark of this thread from any device.
+ const seen = Math.max(rows.find(m => own(m.author))?.sequence ?? 0, threadMarks.get(newest.conversationId)?.get(newest.thread!) ?? 0)
+ const unread = rows.filter(m => !own(m.author) && m.sequence > seen).length
+ if (!unread) continue
candidates.push({ conversationId: newest.conversationId, root: newest.thread!, mark: marks.get(newest.conversationId)!, replies: rows, unread,
member: rows.some(inIt), at: newest.createdAt.getTime(), id: newest.id })
}
@@ -233,7 +235,9 @@ export async function readInbox(store: SuperTabStore, user: AuthUser, tabId: str
// A chat never marked read counts as read: its threads wait for the person's first visit.
const unread = chats.filter(g => { const mark = marks.get(g.id) ?? 0; return mark > 0 && g.lastMessageSequence > mark })
const shown = chats.slice(0, LATEST_CHATS)
- const [latest, threaded] = await Promise.all([latestIn(db, tabId, shown), threadsIn(db, tabId, state, unread.slice(0, THREAD_CHATS), user.sub, marks)])
+ // The person's thread marks: one indexed read (readMarks.ts), beside the latest messages'.
+ const [latest, threaded] = await Promise.all([latestIn(db, tabId, shown),
+ readThreadMarks(db, tabId, user.sub).then(threadMarks => threadsIn(db, tabId, state, unread.slice(0, THREAD_CHATS), user.sub, marks, threadMarks))])
const threads = threaded.threads.sort((a, b) => newestFirst({ at: Date.parse(a.lastReplyAt), id: a.rootId }, { at: Date.parse(b.lastReplyAt), id: b.rootId }))
const more = chats.length > LATEST_CHATS || latest.more || unread.length > THREAD_CHATS || threaded.more || threads.length > MAX_THREADS
return { threads: threads.slice(0, MAX_THREADS), more,
diff --git a/backend/src/superTabs/read/service.ts b/backend/src/superTabs/read/service.ts
index fe8673979..93c078598 100644
--- a/backend/src/superTabs/read/service.ts
+++ b/backend/src/superTabs/read/service.ts
@@ -5,10 +5,12 @@
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, stableId, visibleState, type Actor, type AgentStart, type SuperTabState } from '../model.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 { decisionsWaiting } from '../decisions.js'
-import { readMarks, writeReadMark } from '../readMarks.js'
+import { readMarks, readThreadMarks, writeReadMark, writeThreadMark } from '../readMarks.js'
+import { deliveredCopies } from '../pageReads.js'
+import { readBy } from '../parts/indexed.js'
import type { SuperTabStore } from '../store.js'
import { transaction } from '../store.js'
import type { CacheView } from '../cache.js'
@@ -16,7 +18,7 @@ import { readChanged, readLastChangedAt, readLight, readParts } from '../cachedR
import { migrateSwarm } from '../migration/migrate.js'
import * as parts from '../parts/index.js'
import type { GroupData, Part, SwarmParts } from '../parts/index.js'
-import { canReadGroup, readProject } from '../projects.js'
+import { canReadGroup, readConversation, readProject } from '../projects.js'
import { isBuddyReader } from '../buddies.js'
import { workspacesRequired } from '../workspaceStore.js'
import type { Leaderboard } from '../leaderboard/contract.js'
@@ -39,14 +41,20 @@ interface Read { parts: SwarmParts; actor: Actor; view: ReaderView; lastChangedA
const MACHINE_READS_MS = 15_000
type MachineReads = [teamMachines: string[], otherWorkspaceMachines: string[], machineVersions: Record<string, string>]
-/** The reader's marks of the conversations they can read now, a dormant one read by id included: a mark never names a
- * conversation the reader has lost. Only in a person's read; nothing for an agent. */
-function readableMarks(read: Read, marks: Map<string, number>): Pick<Derived, 'readMarks'> {
+/** The conversations the reader can read now, a dormant one read by id included. */
+function readableGroups(read: Read): Set<string> {
+ return new Set([...read.view.parts.groups.map(g => g.key),
+ ...read.parts.groups.filter(g => g.dormant && canReadGroup(read.view.state, read.actor, g.data!)).map(g => g.key)])
+}
+
+/** The reader's marks of the conversations and threads they can read now: a mark never names a conversation the reader has
+ * lost. Only in a person's read; nothing for an agent. A read with no thread marks says none (`threadMarks` absent). */
+function readableMarks(read: Read, marks: Map<string, number>, threads: Map<string, Map<string, number>>): Pick<Derived, 'readMarks' | 'threadMarks'> {
if (read.actor.kind !== 'person') return {}
- const shown = new Set(read.view.parts.groups.map(g => g.key))
- const readable = (id: string) => shown.has(id)
- || read.parts.groups.some(g => g.key === id && g.dormant && canReadGroup(read.view.state, read.actor, g.data!))
- return { readMarks: Object.fromEntries([...marks].filter(([id]) => readable(id))) }
+ const readable = readableGroups(read)
+ const threadMarks = Object.fromEntries([...threads].filter(([id]) => readable.has(id)).map(([id, held]) => [id, Object.fromEntries(held)]))
+ return { readMarks: Object.fromEntries([...marks].filter(([id]) => readable.has(id))),
+ ...(Object.keys(threadMarks).length ? { threadMarks } : {}) }
}
/** `loaded` with these starts too: a change poll's own (SwarmReads.pollStarts), never one it holds already. */
@@ -76,7 +84,8 @@ export class SwarmReads {
const { parts: loaded, view, lastChangedAt } = read
return { format: READ_FORMAT, tabId, cursor: cursorFor(t0, loaded.visibilityEpoch), serverTime: t0, lastChangedAt, epoch: loaded.visibilityEpoch,
...(this.options.githubApp ? { githubApp: true as const } : {}), ...view.parts,
- derived: { ...await this.withAvatars(view, t0), ...readableMarks(read, await this.marksOf(tx, tabId, read.actor)) } }
+ derived: { ...await this.withAvatars(view, t0), ...readableMarks(read, await this.marksOf(tx, tabId, read.actor),
+ await this.threadMarksOf(tx, tabId, read.actor)) } }
})
}
@@ -134,12 +143,14 @@ export class SwarmReads {
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 unheld = [...new Set([...changed.flatMap(p => p.kind === 'start' ? [(p.data as AgentStart).request.conversationId] : []), ...marks.keys()])]
+ 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)
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) } }
+ derived: { ...await this.withAvatars(read.view, t0), ...readableMarks(read, marks, threadMarks) } }
})
}
@@ -197,6 +208,36 @@ export class SwarmReads {
return { sequence: await writeReadMark(this.db, { tabId, personId: user.sub, conversationId, sequence: Math.min(sequence, newest), now: this.now() }) }
}
+ /** Moves the person's mark of thread `threadId` in a conversation they can read forward, never past the conversation's
+ * newest message (readMarks.ts), which the reader sees already: a hidden reply's sequence never shows in the answer.
+ * Checked as a page of messages is (store.ts messages): the Swarm read with that conversation alone, then the root by its
+ * id, a root the person may read (not an agent-only notice, not a delivered copy) that roots its own thread. Every miss,
+ * an unreadable conversation or project included, is the same refusal. Only a person reading as themselves has marks. */
+ async markThreadRead(user: AuthUser, tabId: string, conversationId: string, threadId: string, sequence: number): Promise<{ sequence: number }> {
+ if (isTeamSession(user)) fail(TEAM_SESSION_LIMITED, TEAM_SESSION_MESSAGE, 403)
+ const unavailable = () => fail('NOT_FOUND', 'This conversation is unavailable.', 404)
+ const { state } = await this.store.readLoad(user, tabId, { groupIds: [conversationId] })
+ const actor = await this.store.actor(this.db, user, state)
+ let newest: number
+ try {
+ newest = readConversation(state, actor, conversationId).lastMessageSequence
+ } catch (error) {
+ if (error instanceof SuperTabError && error.status === 404) unavailable()
+ throw error
+ }
+ const [found] = await readBy(this.db, 'SuperTabMessage', 'id', [{ $match: { _id: threadId, tabId, conversationId } },
+ { $project: { source: 1, recipientIds: 1, thread: 1 } }])
+ const root = found && { id: found._id as string, source: found.source ?? null, recipientIds: (found.recipientIds as string[] | undefined) ?? [], thread: found.thread }
+ if (!root || root.thread !== root.id || agentOnly(root.source, root.recipientIds) || (await deliveredCopies(this.db, tabId, [root])).size) unavailable()
+ return { sequence: await writeThreadMark(this.db, { tabId, personId: user.sub, conversationId, threadId, sequence: Math.min(sequence, newest),
+ now: this.now() }) }
+ }
+
+ /** A person's own thread marks (an agent has none), every one or those moved since `since`. */
+ private async threadMarksOf(tx: PrismaClient, tabId: string, actor: Actor, since?: number): Promise<Map<string, Map<string, number>>> {
+ return actor.kind === 'person' ? readThreadMarks(tx, tabId, actor.userId, since) : new Map()
+ }
+
/** A person's own read marks (an agent has none), every one or those moved since `since`. */
private async marksOf(tx: PrismaClient, tabId: string, actor: Actor, since?: number): Promise<Map<string, number>> {
return actor.kind === 'person' ? readMarks(tx, tabId, actor.userId, since) : new Map()
diff --git a/backend/src/superTabs/readMarks.ts b/backend/src/superTabs/readMarks.ts
index ccbf478dc..c72c1f851 100644
--- a/backend/src/superTabs/readMarks.ts
+++ b/backend/src/superTabs/readMarks.ts
@@ -1,37 +1,49 @@
-/** How far each person has read each conversation: the newest message sequence they saw there, so unread state follows them
- * across devices. Marks live in SuperTabReadMark, never in the tab state: writing one wakes no other reader, and an older
- * backend reads and writes a tab that has them. A person's marks go only to their own reads (read/service.ts); agents have
- * none. */
+/** How far each person has read each conversation, and each thread in one: the newest message sequence they saw there, so
+ * unread state follows them across devices. Marks live in SuperTabReadMark and SuperTabThreadMark, never in the tab state:
+ * writing one wakes no other reader, and an older backend reads and writes a tab that has them (it never reads the thread
+ * marks' collection). A person's marks go only to their own reads (read/service.ts); agents, Buddies included, have none. */
import { Prisma, type PrismaClient } from '@prisma/client'
import { ensureIndex } from '../lib/db/indexes.js'
import { fail, stableId } from './model.js'
+import { readBy } from './parts/indexed.js'
/** A mark untouched this long is removed by a TTL index; the conversation then reads as the device last saw it. */
export const READ_MARK_EXPIRY_MS = 180 * 24 * 60 * 60_000
-/** Run by the singleton worker at startup, like the mention expiry. */
+/** The most thread marks one person keeps in one Swarm: a new one past it removes the least recently moved. A thread whose
+ * mark went reads as far as its conversation's mark, as on a device that never kept one. */
+export const THREAD_MARK_CAP = 500
+
+/** Run by the singleton worker at startup, like the mention expiry. Thread marks need no index option: the hourly sweep
+ * deletes them (sweepThreadMarks), as the app user may not set one on an index `db push` built (#2238). */
export async function ensureReadMarkExpiry(db: PrismaClient): Promise<void> {
await ensureIndex(db, 'SuperTabReadMark', { key: { updatedAt: 1 }, name: 'read_mark_expiry', expireAfterSeconds: READ_MARK_EXPIRY_MS / 1000 })
}
export interface ReadMarkWrite { tabId: string; personId: string; conversationId: string; sequence: number; now: number }
+export interface ThreadMarkWrite extends ReadMarkWrite { threadId: string }
-/** Moves the person's mark forward to `sequence` and answers the mark as stored. An older mark never replaces a newer one:
- * the update matches only a lower mark. When another device's write lands between the update and the look after it (two
- * first writes racing to create the row, or one raising it), the update runs again; every retry follows a write that
- * raised the mark, so a few settle it. */
-export async function writeReadMark(db: PrismaClient, mark: ReadMarkWrite): Promise<number> {
- const { tabId, personId, conversationId, sequence } = mark
- const id = stableId(tabId, conversationId, personId), updatedAt = new Date(mark.now)
+/** The calls `raise` makes, which both mark collections answer alike. */
+interface MarkRows {
+ updateMany(args: { where: { id: string; sequence: { lt: number } }; data: { sequence: number; updatedAt: Date } }): Promise<{ count: number }>
+ findUnique(args: { where: { id: string }; select: { sequence: true } }): Promise<{ sequence: number } | null>
+ create(args: { data: Record<string, unknown> & { id: string } }): Promise<unknown>
+}
+
+/** Moves row `id` forward to `sequence` and answers the mark as stored, with whether this call created the row. An older
+ * mark never replaces a newer one: the update matches only a lower mark. When another device's write lands between the
+ * update and the look after it (two first writes racing to create the row, or one raising it), the update runs again;
+ * every retry follows a write that raised the mark, so a few settle it. */
+async function raise(rows: MarkRows, id: string, sequence: number, updatedAt: Date, data: Record<string, unknown>): Promise<{ sequence: number; created: boolean }> {
for (let attempt = 0; attempt < 4; attempt++) {
- const moved = await db.superTabReadMark.updateMany({ where: { id, sequence: { lt: sequence } }, data: { sequence, updatedAt } })
- if (moved.count) return sequence
- const held = await db.superTabReadMark.findUnique({ where: { id }, select: { sequence: true } })
- if (held && held.sequence >= sequence) return held.sequence
+ const moved = await rows.updateMany({ where: { id, sequence: { lt: sequence } }, data: { sequence, updatedAt } })
+ if (moved.count) return { sequence, created: false }
+ const held = await rows.findUnique({ where: { id }, select: { sequence: true } })
+ if (held && held.sequence >= sequence) return { sequence: held.sequence, created: false }
if (held) continue
try {
- await db.superTabReadMark.create({ data: { id, tabId, personId, conversationId, sequence, updatedAt } })
- return sequence
+ await rows.create({ data: { id, ...data, sequence, updatedAt } })
+ return { sequence, created: true }
} catch (error) {
if (!(error instanceof Prisma.PrismaClientKnownRequestError) || error.code !== 'P2002') throw error
}
@@ -39,9 +51,136 @@ export async function writeReadMark(db: PrismaClient, mark: ReadMarkWrite): Prom
return fail('READ_MARK_BUSY', 'This conversation is being marked read on another device. Try again.', 503)
}
+/** Moves the person's mark of a conversation forward to `sequence` and answers the mark as stored (`raise`). */
+export async function writeReadMark(db: PrismaClient, mark: ReadMarkWrite): Promise<number> {
+ const { tabId, personId, conversationId, sequence } = mark
+ return (await raise(db.superTabReadMark as unknown as MarkRows, stableId(tabId, conversationId, personId), sequence, new Date(mark.now),
+ { tabId, personId, conversationId })).sequence
+}
+
+/** Moves the person's mark of one thread forward to `sequence` and answers the mark as stored (`raise`). A new mark past
+ * THREAD_MARK_CAP removes the person's least recently moved ones in this Swarm. Two first marks racing may each remove
+ * one; a removed mark only makes its thread read as far as its conversation's mark. */
+export async function writeThreadMark(db: PrismaClient, mark: ThreadMarkWrite): Promise<number> {
+ const { tabId, personId, conversationId, threadId, sequence } = mark
+ const stored = await raise(db.superTabThreadMark as unknown as MarkRows, stableId(tabId, conversationId, threadId, personId), sequence,
+ new Date(mark.now), { tabId, personId, conversationId, threadId })
+ if (stored.created) await capThreadMarks(db, tabId, personId)
+ return stored.sequence
+}
+
+/** Removes the person's least recently moved thread marks in the Swarm past THREAD_MARK_CAP, by the (tabId, personId,
+ * updatedAt) index: it counts the person's marks and reads only the ids of those past the cap. */
+async function capThreadMarks(db: PrismaClient, tabId: string, personId: string): Promise<void> {
+ const [counted] = await readBy(db, 'SuperTabThreadMark', 'person', [{ $match: { tabId, personId } }, { $count: 'n' }])
+ const over = Number(counted?.n ?? 0) - THREAD_MARK_CAP
+ if (over <= 0) return
+ const oldest = await readBy(db, 'SuperTabThreadMark', 'person', [{ $match: { tabId, personId } }, { $sort: { updatedAt: 1 } }, { $limit: over },
+ { $project: { _id: 1 } }])
+ // A plain $in on _id: Prisma's `in` plans slowly (parts/indexed.ts).
+ await db.$runCommandRaw({ delete: 'SuperTabThreadMark', deletes: [{ q: { _id: { $in: oldest.map(row => row._id as string) } }, limit: 0 }] })
+}
+
/** The person's marks in this tab, by conversation; with `since`, only those moved at or after it. */
export async function readMarks(db: PrismaClient, tabId: string, personId: string, since?: number): Promise<Map<string, number>> {
const rows = await db.superTabReadMark.findMany({ where: { tabId, personId, ...(since === undefined ? {} : { updatedAt: { gte: new Date(since) } }) },
select: { conversationId: true, sequence: true } })
return new Map(rows.map(row => [row.conversationId, row.sequence]))
}
+
+/** The person's thread marks in this tab, by conversation then thread root: every one (at most THREAD_MARK_CAP), or with
+ * `since` those moved at or after it. One raw match by the (tabId, personId, updatedAt) index: Prisma's filters plan slowly
+ * at this size (parts/indexed.ts). The caller keeps those of conversations the reader may read. */
+export async function readThreadMarks(db: Prisma.TransactionClient, tabId: string, personId: string, since?: number):
+ Promise<Map<string, Map<string, number>>> {
+ const marks = new Map<string, Map<string, number>>()
+ const rows = await readBy(db, 'SuperTabThreadMark', 'person', [{ $match: { tabId, personId,
+ ...(since === undefined ? {} : { updatedAt: { $gte: { $date: new Date(since).toISOString() } } }) } },
+ { $project: { _id: 0, conversationId: 1, threadId: 1, sequence: 1 } }])
+ for (const row of rows) {
+ const conversationId = row.conversationId as string, threadId = row.threadId as string, sequence = Number(row.sequence)
+ let held = marks.get(conversationId)
+ if (!held) marks.set(conversationId, held = new Map())
+ held.set(threadId, sequence)
+ }
+ return marks
+}
+
+/** How many marks one sweep deletes at most, in batches, each kind: the rest wait for the next hour. */
+export const THREAD_MARK_SWEEP = 20_000
+const SWEEP_BATCH = 1_000
+
+/** Deletes rows by id with a plain $in on _id (Prisma's `in` plans slowly). */
+async function deleteIds(db: PrismaClient, ids: string[]): Promise<number> {
+ if (!ids.length) return 0
+ const reply = await db.$runCommandRaw({ delete: 'SuperTabThreadMark', deletes: [{ q: { _id: { $in: ids } }, limit: 0 }] }) as { n?: number }
+ return Number(reply.n ?? 0)
+}
+
+/** How many Swarms one sweep checks for marks of people no longer there; the next pass goes on after the last one. */
+export const THREAD_MARK_SWARMS = 200
+
+/** Where the people check reads on from: after the last Swarm whose check finished. */
+export interface ThreadMarkWalk { after: string }
+/** The worker's walk, kept in the database so a release goes on where the last process stopped. */
+const CURSOR = 'thread-marks'
+const storedWalk = async (db: PrismaClient): Promise<ThreadMarkWalk> =>
+ ({ after: (await db.superTabSweepCursor.findUnique({ where: { id: CURSOR }, select: { after: true } }))?.after ?? '' })
+const storeWalk = (db: PrismaClient, after: string, now: number) =>
+ db.superTabSweepCursor.upsert({ where: { id: CURSOR }, create: { id: CURSOR, after, updatedAt: new Date(now) }, update: { after, updatedAt: new Date(now) } })
+
+/** The first mark's value of `field` past `after` within `match`, by one seek on the person index; null past the last. */
+async function nextOf(db: PrismaClient, match: Record<string, string>, field: 'tabId' | 'personId', after: string): Promise<string | null> {
+ const [row] = await readBy(db, 'SuperTabThreadMark', 'person', [{ $match: { ...match, [field]: { $gt: after } } }, { $sort: { tabId: 1, personId: 1 } },
+ { $limit: 1 }, { $project: { _id: 0, [field]: 1 } }])
+ return (row?.[field] as string | undefined) ?? null
+}
+
+/**
+ * The hourly sweep of thread marks (run by the singleton worker with the Swarm sweeps, sweeps.ts): deletes marks untouched for
+ * READ_MARK_EXPIRY_MS, by the age index in batches of SWEEP_BATCH, at most THREAD_MARK_SWEEP a pass. Then it checks the next
+ * THREAD_MARK_SWARMS Swarms with marks, in id order from where the last pass stopped (the stored walk, or `walk` when given),
+ * one Swarm and one person at a time by index seeks; the place is stored after each Swarm's check finishes, so a release goes
+ * on from there and a Swarm whose check failed is checked again: a Swarm whose row is gone loses all its marks, a person no longer in its `memberIds` theirs. A Swarm
+ * row without `memberIds` is skipped, never read as having no members. A delete only makes a thread read as far as its
+ * conversation's mark, so a mark swept a moment before its person moves it again costs nothing.
+ */
+export async function sweepThreadMarks(db: PrismaClient, now: number, options: { walk?: ThreadMarkWalk; swarms?: number } = {}):
+ Promise<{ expired: number; lost: number; swarmsChecked: number }> {
+ const swarms = options.swarms ?? THREAD_MARK_SWARMS
+ const at = options.walk ?? await storedWalk(db)
+ const moveTo = async (after: string) => {
+ at.after = after
+ if (!options.walk) await storeWalk(db, after, now)
+ }
+ let expired = 0
+ const before = { $date: new Date(now - READ_MARK_EXPIRY_MS).toISOString() }
+ while (expired < THREAD_MARK_SWEEP) {
+ const rows = await readBy(db, 'SuperTabThreadMark', 'age', [{ $match: { updatedAt: { $lt: before } } }, { $limit: SWEEP_BATCH }, { $project: { _id: 1 } }])
+ expired += await deleteIds(db, rows.map(row => row._id as string))
+ if (rows.length < SWEEP_BATCH) break
+ }
+ let lost = 0, swarmsChecked = 0
+ const remove = async (q: Record<string, string>) => {
+ const reply = await db.$runCommandRaw({ delete: 'SuperTabThreadMark', deletes: [{ q, limit: 0 }] }) as { n?: number }
+ lost += Number(reply.n ?? 0)
+ }
+ while (swarmsChecked < swarms) {
+ const tabId = await nextOf(db, {}, 'tabId', at.after)
+ // Past the last Swarm: the next pass starts again from the first.
+ if (tabId === null) { await moveTo(''); break }
+ swarmsChecked++
+ const [row] = await readBy(db, 'SuperTab', 'id', [{ $match: { _id: tabId } }, { $project: { memberIds: 1 } }])
+ if (!row) await remove({ tabId })
+ // A row without memberIds is skipped, never read as having no members.
+ else if (Array.isArray(row.memberIds)) {
+ const members = new Set(row.memberIds as string[])
+ for (let person = await nextOf(db, { tabId }, 'personId', ''); person !== null; person = await nextOf(db, { tabId }, 'personId', person)) {
+ if (!members.has(person)) await remove({ tabId, personId: person })
+ }
+ }
+ // Only now, its check done: a failure above leaves the place before it.
+ await moveTo(tabId)
+ }
+ return { expired, lost, swarmsChecked }
+}
diff --git a/backend/src/superTabs/sweeps.ts b/backend/src/superTabs/sweeps.ts
index 6b2379a1e..ba4d6d78e 100644
--- a/backend/src/superTabs/sweeps.ts
+++ b/backend/src/superTabs/sweeps.ts
@@ -8,6 +8,7 @@ import { swarmTransaction } from './cache.js'
import { listNamedIndexes, readBy } from './parts/indexed.js'
import { autonomousEnvironments } from '../lib/autonomousEnvironment.js'
import { expireEvidence } from './evidence/store.js'
+import { sweepThreadMarks } from './readMarks.js'
/** Parts whose key is never used twice (conversations, starts, projects' progress) lose their tombstone at `purgeAt`.
* A person, agent or machine can come back under the same key, so its tombstone is kept for good, compacted to its
@@ -143,10 +144,20 @@ async function expireInvitations(db: PrismaClient, now: number): Promise<number>
return expired
}
+/** The thread-mark sweep (readMarks.ts), its failure said and kept to itself: the other sweeps of the pass still run. */
+export async function threadMarks(db: PrismaClient, now: number) {
+ try {
+ return await sweepThreadMarks(db, now)
+ } catch (error) {
+ logger.warn('[swarm-sweeps] thread marks failed', { error: error instanceof Error ? error.message : String(error) })
+ return { failed: true as const }
+ }
+}
+
/** One pass of every sweep (spec, Archive). Each part it changes is a guarded write, so a command in between wins. */
export async function sweepSwarms(db: PrismaClient, now = Date.now()) {
return { slept: await sleepQuietGroups(db, now), startsExpired: await expireStarts(db, now), invitationsExpired: await expireInvitations(db, now),
- tombstonesCompacted: await compactTombstones(db, now), evidenceExpired: await expireEvidence(db, now) }
+ tombstonesCompacted: await compactTombstones(db, now), evidenceExpired: await expireEvidence(db, now), threadMarks: await threadMarks(db, now) }
}
/** Runs the sweeps every hour in the singleton worker. `finishWorkspaceRemovals` finishes Workspace removals whose Swarm removals
diff --git a/backend/src/superTabs/sweepsThreadMarks.test.ts b/backend/src/superTabs/sweepsThreadMarks.test.ts
new file mode 100644
index 000000000..af394b779
--- /dev/null
+++ b/backend/src/superTabs/sweepsThreadMarks.test.ts
@@ -0,0 +1,17 @@
+import type { PrismaClient } from '@prisma/client'
+import { describe, expect, it, vi } from 'vitest'
+import { logger } from '../utils/logger.js'
+import { threadMarks } from './sweeps.js'
+
+describe('the hourly thread-mark sweep inside the Swarm sweeps', () => {
+ it('says its failure and answers, so the sweeps after it in the pass still run', async () => {
+ const warn = vi.spyOn(logger, 'warn').mockImplementation(() => logger)
+ // Every call fails, as with the database out of reach.
+ const reset = async () => { throw new Error('connection reset') }
+ const failing = new Proxy({}, { get: (_, key) => key === 'then' ? undefined : key === '$runCommandRaw' ? reset
+ : new Proxy({}, { get: (__, method) => method === 'then' ? undefined : reset }) }) as unknown as PrismaClient
+ await expect(threadMarks(failing, Date.now())).resolves.toEqual({ failed: true })
+ expect(warn).toHaveBeenCalledWith('[swarm-sweeps] thread marks failed', { error: 'connection reset' })
+ warn.mockRestore()
+ })
+})
diff --git a/desktop/lib/super_tabs/controller.dart b/desktop/lib/super_tabs/controller.dart
index f78adfc27..64b585ead 100644
--- a/desktop/lib/super_tabs/controller.dart
+++ b/desktop/lib/super_tabs/controller.dart
@@ -1107,10 +1107,31 @@ class SuperTabController extends ChangeNotifier {
bool threadUnread(SuperTabThread thread) {
final last = thread.authors.firstOrNull;
return thread.people.contains(accountId) &&
- thread.lastSequence > (_threadSeen[thread.id] ?? 0) &&
+ thread.lastSequence > threadReadMark(thread.id) &&
!(last?['kind'] == 'person' && last?['id'] == accountId);
}
+ /// How far the reader has read [root]'s thread: the higher of this
+ /// device's mark and the server's, from any of their devices (backend
+ /// readMarks.ts), which reads a thread opened only elsewhere as read here.
+ int threadReadMark(String root) =>
+ max(_threadSeen[root] ?? 0, _serverThreadMark(root));
+
+ /// The server's thread marks by root, from the snapshot's by conversation;
+ /// worked out again only when a read brings new ones.
+ Map<String, Map<String, int>>? _threadMarksFrom;
+ var _threadMarksByRoot = const <String, int>{};
+
+ int _serverThreadMark(String root) {
+ final marks = snapshot?.threadMarks;
+ if (marks == null || marks.isEmpty) return 0;
+ if (!identical(marks, _threadMarksFrom)) {
+ _threadMarksFrom = marks;
+ _threadMarksByRoot = {for (final threads in marks.values) ...threads};
+ }
+ return _threadMarksByRoot[root] ?? 0;
+ }
+
void _keepThreads(String id, Object? rows) {
if (rows is! List) return;
final held = _threads[id] ??= {};
@@ -1137,6 +1158,17 @@ class SuperTabController extends ChangeNotifier {
0,
threadMessages(root).lastOrNull?.sequence ?? 0,
);
+ if (open != null &&
+ open.root == root &&
+ !accessRevoked &&
+ last > 0 &&
+ snapshot?.conversation(open.conversationId) != null) {
+ _serverThreadMarks.mark(
+ (open.conversationId, root),
+ last,
+ kept: snapshot?.threadMarks[open.conversationId]?[root] ?? 0,
+ );
+ }
if ((_threadSeen[root] ?? 0) >= last) return;
// Moved to the end: the newest marks are the ones a save keeps.
_threadSeen.remove(root);
@@ -1587,7 +1619,7 @@ class SuperTabController extends ChangeNotifier {
),
);
- late final _serverMarks = SuperTabReadMarkSender(
+ late final _serverMarks = SuperTabReadMarkSender<String>(
(id, sequence) => request(
'POST',
'$_path/conversations/${Uri.encodeComponent(id)}/read',
@@ -1595,6 +1627,20 @@ class SuperTabController extends ChangeNotifier {
),
);
+ /// Sends the marks of threads read here, by conversation and root. A server
+ /// without thread marks refuses them; this device's own mark stands, and a
+ /// refused mark waits a minute before it is tried again.
+ late final _serverThreadMarks = SuperTabReadMarkSender<(String, String)>(
+ (thread, sequence) => request(
+ 'POST',
+ '$_path/conversations/${Uri.encodeComponent(thread.$1)}'
+ '/threads/${Uri.encodeComponent(thread.$2)}/read',
+ body: {'sequence': sequence},
+ ),
+ retryAfter: const Duration(minutes: 1),
+ now: clock.now,
+ );
+
/// Whether [group] has a message in its main stream past the reader's
/// mark. A reply only in its thread leaves the chat as it was: it lights its
/// thread's reply line instead ([threadUnread]). The server's newest
@@ -3808,6 +3854,7 @@ class SuperTabController extends ChangeNotifier {
}
_queuedSends.clear();
_serverMarks.dispose();
+ _serverThreadMarks.dispose();
_poll?.cancel();
_toldRead?.cancel();
_graceEnd?.cancel();
diff --git a/desktop/lib/super_tabs/model.dart b/desktop/lib/super_tabs/model.dart
index 1b8890c26..a111ad07d 100644
--- a/desktop/lib/super_tabs/model.dart
+++ b/desktop/lib/super_tabs/model.dart
@@ -397,6 +397,15 @@ class SuperTabSnapshot {
for (final MapEntry(:key, :value) in marks.entries)
if (key is String && value is num) key: value.toInt(),
}),
+ threadMarks = Map.unmodifiable({
+ if (row['threadMarks'] case final Map marks)
+ for (final MapEntry(key: id, value: threads) in marks.entries)
+ if (id is String && threads is Map)
+ id: Map<String, int>.unmodifiable({
+ for (final MapEntry(:key, :value) in threads.entries)
+ if (key is String && value is num) key: value.toInt(),
+ }),
+ }),
buddyStarts = List.unmodifiable([
for (final start
in row['starts'] is List ? row['starts'] as List : const [])
@@ -477,6 +486,10 @@ class SuperTabSnapshot {
/// The reader's own read marks kept on the server (backend readMarks.ts): the newest message sequence read, by
/// conversation, on any of their devices. Never another person's.
final Map<String, int> readMarks;
+
+ /// The reader's own thread marks kept on the server (backend readMarks.ts): by conversation, the newest message
+ /// sequence read in each thread, by its root, on any of their devices. Empty from a backend before them.
+ final Map<String, Map<String, int>> threadMarks;
SuperTabConversation? conversation(String id) =>
conversations.where((c) => c.id == id).firstOrNull;
SuperTabAgent? agent(String id) =>
diff --git a/desktop/lib/super_tabs/parts.dart b/desktop/lib/super_tabs/parts.dart
index a70932576..16b46af98 100644
--- a/desktop/lib/super_tabs/parts.dart
+++ b/desktop/lib/super_tabs/parts.dart
@@ -60,6 +60,7 @@ class SuperTabParts {
// Read marks are held apart from the other derived values (_readMarks).
final derived = Map.of(superTabMap(answer['derived'] ?? const {}));
final marks = derived.remove('readMarks');
+ final threadMarks = derived.remove('threadMarks');
final parts = SuperTabParts._(
_envelope(answer['head'], live: true),
derived,
@@ -79,6 +80,7 @@ class SuperTabParts {
}
}
parts._takeReadMarks(marks);
+ parts._takeThreadMarks(threadMarks);
return parts;
}
@@ -101,6 +103,7 @@ class SuperTabParts {
}
}
parts._takeReadMarks(json['readMarks']);
+ parts._takeThreadMarks(json['threadMarks']);
return parts;
}
@@ -145,6 +148,27 @@ class SuperTabParts {
return moved;
}
+ /// The reader's own thread marks (backend readMarks.ts): by conversation, the newest message sequence read in each
+ /// thread, by its root. Kept as read marks are: the higher of each, and gone with their conversation.
+ final _threadMarks = <String, Map<String, int>>{};
+
+ /// Keeps the higher of each thread mark in [marks] and the one held; whether any moved.
+ bool _takeThreadMarks(Object? marks) {
+ var moved = false;
+ if (marks is! Map) return moved;
+ for (final MapEntry(key: id, value: threads) in marks.entries) {
+ if (id is! String || threads is! Map) continue;
+ for (final MapEntry(key: root, value: sequence) in threads.entries) {
+ if (root is! String || sequence is! num) continue;
+ final held = _threadMarks[id] ??= {};
+ if ((held[root] ?? 0) >= sequence) continue;
+ held[root] = sequence.toInt();
+ moved = true;
+ }
+ }
+ return moved;
+ }
+
/// Dormant groups held with their data from before a stub closed them: a read by id at the stub's version is
/// their current data.
final _stale = <String>{};
@@ -199,6 +223,7 @@ class SuperTabParts {
_dormant.remove(key);
_stale.remove(key);
_readMarks.remove(key);
+ _threadMarks.remove(key);
removed.add(key);
}
} else if (kind == 'groups' && part['dormant'] == true) {
@@ -226,7 +251,10 @@ class SuperTabParts {
}
}
final derived = Map.of(superTabMap(changes['derived'] ?? const {}));
- final marksMoved = _takeReadMarks(derived.remove('readMarks'));
+ final readMarksMoved = _takeReadMarks(derived.remove('readMarks'));
+ // Both run: one moving never skips the other.
+ final marksMoved =
+ _takeThreadMarks(derived.remove('threadMarks')) | readMarksMoved;
if (!const DeepCollectionEquality().equals(derived, _derived)) {
_derived = derived;
changed = true;
@@ -319,6 +347,12 @@ class SuperTabParts {
// Sent only where the server does not try Harness Team accounts yet.
if (_derived['teamAccounts'] == false) state['teamAccounts'] = false;
if (_readMarks.isNotEmpty) state['readMarks'] = Map.of(_readMarks);
+ if (_threadMarks.isNotEmpty) {
+ state['threadMarks'] = {
+ for (final MapEntry(:key, :value) in _threadMarks.entries)
+ key: Map<String, int>.of(value),
+ };
+ }
return state;
}
@@ -339,6 +373,10 @@ class SuperTabParts {
'seen': Map<String, int>.of(_seen),
'stale': _stale.toList(),
'readMarks': Map<String, int>.of(_readMarks),
+ 'threadMarks': {
+ for (final MapEntry(:key, :value) in _threadMarks.entries)
+ key: Map<String, int>.of(value),
+ },
};
/// [json] (from [toJson]) without what the screens never draw: agent starts carry their tasks, and project
diff --git a/desktop/lib/super_tabs/read_marks.dart b/desktop/lib/super_tabs/read_marks.dart
index 16bfe4304..946b62be1 100644
--- a/desktop/lib/super_tabs/read_marks.dart
+++ b/desktop/lib/super_tabs/read_marks.dart
@@ -1,44 +1,69 @@
-/// Sends the reader's read marks to the server (backend `superTabs/readMarks.ts`), so a conversation read on this
-/// device reads as read on every other one. One request per conversation at a time: a higher mark asked for while
-/// one is on its way goes after it, and a lower or equal one is never sent. A failed send is forgotten, so the next
-/// mark asked for tries again; meanwhile this device's own mark keeps the conversation read here.
-class SuperTabReadMarkSender {
- SuperTabReadMarkSender(this._send);
+/// Sends the reader's read marks to the server (backend `superTabs/readMarks.ts`), so a conversation, or a thread
+/// ([K] names which), read on this device reads as read on every other one. One request per conversation or thread
+/// at a time: a higher mark asked for while one is on its way goes after it, and a lower or equal one is never sent.
+/// A failed send is forgotten, so the next mark asked for tries again; meanwhile this device's own mark keeps it
+/// read here.
+class SuperTabReadMarkSender<K> {
+ /// With [retryAfter], a mark whose send failed is not sent again for that long, unless a higher one is asked for:
+ /// a thread asks for its mark on every read while it is open, and a server without thread marks refuses each.
+ SuperTabReadMarkSender(
+ this._send, {
+ this.retryAfter = Duration.zero,
+ DateTime Function()? now,
+ }) : _now = now ?? DateTime.now;
+
+ final Duration retryAfter;
+ final DateTime Function() _now;
+
+ /// The last mark that failed, and when, by conversation.
+ final _failed = <K, (int, DateTime)>{};
/// Writes [sequence] as the mark of [conversationId]; completes once the server kept it, or throws.
- final Future<void> Function(String conversationId, int sequence) _send;
+ final Future<void> Function(K conversationId, int sequence) _send;
/// The highest mark sent and kept, by conversation.
- final _sent = <String, int>{};
+ final _sent = <K, int>{};
/// The mark waiting for the one on its way, by conversation.
- final _next = <String, int>{};
- final _sending = <String>{};
+ final _next = <K, int>{};
+ final _sending = <K>{};
+
+ /// The mark on its way, by conversation: one asked for again meanwhile is not sent twice.
+ final _inFlight = <K, int>{};
bool _disposed = false;
/// Asks for [sequence] to be [conversationId]'s mark on the server, unless [kept] (what the server already
/// holds) or a mark sent from here is as high.
- void mark(String conversationId, int sequence, {int kept = 0}) {
+ void mark(K conversationId, int sequence, {int kept = 0}) {
if (_disposed || sequence <= kept) return;
if (sequence <= (_sent[conversationId] ?? 0)) return;
if (sequence <= (_next[conversationId] ?? 0)) return;
+ if (sequence <= (_inFlight[conversationId] ?? 0)) return;
+ if (_failed[conversationId] case (final failed, final at)
+ when sequence <= failed && _now().difference(at) < retryAfter) {
+ return;
+ }
_next[conversationId] = sequence;
if (_sending.add(conversationId)) _drain(conversationId);
}
- Future<void> _drain(String conversationId) async {
+ Future<void> _drain(K conversationId) async {
while (!_disposed) {
final sequence = _next.remove(conversationId);
if (sequence == null) break;
+ _inFlight[conversationId] = sequence;
try {
await _send(conversationId, sequence);
+ _failed.remove(conversationId);
if (sequence > (_sent[conversationId] ?? 0)) {
_sent[conversationId] = sequence;
}
} catch (_) {
+ _failed[conversationId] = (sequence, _now());
// Offline or refused: this device's mark stands, and the next one asked for is sent afresh.
}
}
+ _inFlight.remove(conversationId);
_sending.remove(conversationId);
}
diff --git a/desktop/test/super_tab_read_marks_test.dart b/desktop/test/super_tab_read_marks_test.dart
index f50e375cb..579451256 100644
--- a/desktop/test/super_tab_read_marks_test.dart
+++ b/desktop/test/super_tab_read_marks_test.dart
@@ -2,6 +2,7 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:harness/super_tabs/controller.dart';
import 'package:harness/super_tabs/model.dart';
import 'package:harness/super_tabs/parts.dart';
+import 'package:harness/super_tabs/read_marks.dart';
import 'support/super_tab_fixture.dart';
@@ -163,6 +164,43 @@ void main() {
expect(c.unread(c.selected!), isFalse);
});
+ test('a refused thread mark waits before it is sent again; a higher one '
+ 'goes at once', () async {
+ var now = DateTime(2026, 10, 30, 7);
+ final sent = <int>[];
+ var refuse = true;
+ final sender = SuperTabReadMarkSender<(String, String)>(
+ (thread, sequence) async {
+ sent.add(sequence);
+ if (refuse) throw StateError('no thread marks here');
+ },
+ retryAfter: const Duration(minutes: 1),
+ now: () => now,
+ );
+ addTearDown(sender.dispose);
+ const thread = ('lead', 'root');
+ // A thread open through several reads asks for the same mark each time.
+ for (var i = 0; i < 3; i++) {
+ sender.mark(thread, 3);
+ await pumpEventQueue();
+ }
+ expect(sent, [3]);
+ now = now.add(const Duration(seconds: 59));
+ sender.mark(thread, 3);
+ await pumpEventQueue();
+ expect(sent, [3]);
+ sender.mark(thread, 4);
+ await pumpEventQueue();
+ expect(sent, [3, 4]);
+ now = now.add(const Duration(minutes: 1));
+ refuse = false;
+ sender.mark(thread, 4);
+ await pumpEventQueue();
+ sender.mark(thread, 4);
+ await pumpEventQueue();
+ expect(sent, [3, 4, 4]);
+ });
+
group('the parts held on this device', () {
Map<String, dynamic> group(
String id,
@@ -212,5 +250,54 @@ void main() {
parts.apply(changes({}, groups: [group('web', 2, deleted: true)]));
expect(parts.state['readMarks'], {'lead': 4});
});
+
+ test('keep the highest thread mark each read sent, across a restart, '
+ 'and drop those of a conversation that is gone', () {
+ final parts = SuperTabParts.firstLoad({
+ ...load({}),
+ 'derived': {
+ 'threadMarks': {
+ 'lead': {'root-a': 4},
+ 'web': {'root-b': 2},
+ },
+ },
+ });
+ Map<String, dynamic> moved(Map<String, dynamic> threadMarks) => {
+ ...changes({}),
+ 'derived': {'threadMarks': threadMarks},
+ };
+ expect(
+ parts
+ .apply(
+ moved({
+ 'web': {'root-b': 9},
+ }),
+ )
+ .readMarksOnly,
+ isTrue,
+ );
+ expect(
+ parts
+ .apply(
+ moved({
+ 'lead': {'root-a': 3},
+ }),
+ )
+ .changed,
+ isFalse,
+ );
+ expect(parts.state['threadMarks'], {
+ 'lead': {'root-a': 4},
+ 'web': {'root-b': 9},
+ });
+ final restored = SuperTabParts.fromJson(
+ SuperTabParts.drawnOnly(parts.toJson()),
+ );
+ expect(restored.state['threadMarks'], parts.state['threadMarks']);
+ restored.apply(changes({}, groups: [group('web', 2, deleted: true)]));
+ expect(restored.state['threadMarks'], {
+ 'lead': {'root-a': 4},
+ });
+ });
});
}
diff --git a/desktop/test/super_tab_threads_test.dart b/desktop/test/super_tab_threads_test.dart
index f02d859d5..c987f023b 100644
--- a/desktop/test/super_tab_threads_test.dart
+++ b/desktop/test/super_tab_threads_test.dart
@@ -585,6 +585,25 @@ void main() {
expect(countWeight(tester, 'message-1'), isNot(FontWeight.w700));
});
+ testWidgets('a thread opened only on another device draws its reply line '
+ 'unbolded here, and bold again for a newer reply', (tester) async {
+ final server = threaded();
+ server.markThreadReadElsewhere('lead', 'message-1', 3);
+ final c = await show(tester, server);
+ expect(countWeight(tester, 'message-1'), isNot(FontWeight.w700));
+ server.append('Another thought.');
+ server.messages.last
+ ..['thread'] = 'message-1'
+ ..['replyTo'] = 'message-1'
+ ..['author'] = peer;
+ await c.refresh();
+ await c.loadMessages('lead');
+ await tester.pumpAndSettle();
+ expect(countWeight(tester, 'message-1'), FontWeight.w700);
+ // The device's save waits on a timer.
+ await tester.pump(const Duration(minutes: 1));
+ });
+
testWidgets('a thread the reader neither wrote in nor was tagged in is '
'never bold', (tester) async {
final server = threaded();
@@ -656,6 +675,134 @@ void main() {
},
);
+ group('thread marks kept on the server', () {
+ Future<SuperTabController> read(ConversationServer server) async {
+ final c = server.controller();
+ addTearDown(() async {
+ await pumpEventQueue();
+ c.dispose();
+ });
+ await c.refresh();
+ await c.loadMessages('lead');
+ return c;
+ }
+
+ /// A reply in message-1's thread from the peer, as a poll brings it.
+ Future<void> peerReplies(
+ ConversationServer server,
+ SuperTabController c,
+ ) async {
+ server.append('One more thing.');
+ server.messages.last
+ ..['thread'] = 'message-1'
+ ..['replyTo'] = 'message-1'
+ ..['author'] = peer;
+ await c.refresh();
+ await c.loadMessages('lead');
+ }
+
+ test('a thread opened only on another device reads as read here; a later '
+ 'reply past it is unread', () async {
+ final server = threaded();
+ // The chat itself was not read anywhere past the root.
+ server.markReadElsewhere('lead', 1);
+ server.markThreadReadElsewhere('lead', 'message-1', 3);
+ final c = await read(server);
+ expect(c.threadReadMark('message-1'), 3);
+ expect(c.threadUnread(c.thread('lead', 'message-1')!), isFalse);
+ await peerReplies(server, c);
+ expect(c.thread('lead', 'message-1')!.lastSequence, 5);
+ expect(c.threadUnread(c.thread('lead', 'message-1')!), isTrue);
+ });
+
+ test(
+ 'another person\'s thread mark never reads the reader\'s thread',
+ () async {
+ final server = threaded();
+ server.markReadElsewhere('lead', 1);
+ server.markThreadReadElsewhere('lead', 'message-1', 3, reader: 'peer');
+ final c = await read(server);
+ expect(c.snapshot!.threadMarks, isEmpty);
+ expect(c.threadUnread(c.thread('lead', 'message-1')!), isTrue);
+ },
+ );
+
+ test('opening a thread sends its mark once, forward only', () async {
+ final server = threaded();
+ server.markReadElsewhere('lead', 1);
+ final c = await read(server);
+ c.select('lead');
+ c.openThread('message-1');
+ await pumpEventQueue();
+ c.closeThread();
+ c.openThread('message-1');
+ await pumpEventQueue();
+ expect(server.threadMarkPosts, [
+ {'conversationId': 'lead', 'root': 'message-1', 'sequence': 3},
+ ]);
+ c.closeThread();
+ await peerReplies(server, c);
+ c.openThread('message-1');
+ await pumpEventQueue();
+ expect(server.threadMarkPosts.last, {
+ 'conversationId': 'lead',
+ 'root': 'message-1',
+ 'sequence': 5,
+ });
+ expect(server.threadMarkPosts, hasLength(2));
+ // Read back on the next poll as every device gets it.
+ await c.refresh();
+ expect(c.snapshot!.threadMarks['lead'], {'message-1': 5});
+ });
+
+ test('a lower server mark never makes a thread read here unread', () async {
+ final server = threaded();
+ server.markReadElsewhere('lead', 1);
+ final c = await read(server);
+ c.select('lead');
+ server.failThreadMarks = true;
+ c.openThread('message-1');
+ await pumpEventQueue();
+ c.closeThread();
+ server.failThreadMarks = false;
+ // Another device marked it read only through its first reply.
+ server.markThreadReadElsewhere('lead', 'message-1', 2);
+ await c.refresh();
+ expect(c.threadReadMark('message-1'), 3);
+ expect(c.threadUnread(c.thread('lead', 'message-1')!), isFalse);
+ });
+
+ test('on a server without thread marks, a thread opened here still reads '
+ 'as read here, and the next one opened is tried again', () async {
+ final server = threaded()..oldServer = true;
+ server.markReadElsewhere('lead', 1);
+ final c = await read(server);
+ c.select('lead');
+ c.openThread('message-1');
+ await pumpEventQueue();
+ expect(server.threadMarkPosts, hasLength(1));
+ expect(c.threadUnread(c.thread('lead', 'message-1')!), isFalse);
+ c.closeThread();
+ await peerReplies(server, c);
+ c.openThread('message-1');
+ await pumpEventQueue();
+ expect(server.threadMarkPosts, hasLength(2));
+ expect(c.snapshot!.threadMarks, isEmpty);
+ });
+
+ test('once access is lost, an open thread sends no mark', () async {
+ final server = threaded();
+ server.markReadElsewhere('lead', 1);
+ final c = await read(server);
+ c.select('lead');
+ c.accessRevoked = true;
+ c.openThread('message-1');
+ await peerReplies(server, c);
+ await pumpEventQueue();
+ expect(server.threadMarkPosts, isEmpty);
+ });
+ });
+
testWidgets('the read mark passes thread replies after the last row shown', (
tester,
) async {
diff --git a/desktop/test/support/super_tab_fixture.dart b/desktop/test/support/super_tab_fixture.dart
index e0ef7b264..b9c545770 100644
--- a/desktop/test/support/super_tab_fixture.dart
+++ b/desktop/test/support/super_tab_fixture.dart
@@ -85,6 +85,7 @@ class SwarmPartsBackend {
'otherProjectsWork',
'githubApp',
'readMarks',
+ 'threadMarks',
'teamAccounts',
}.contains(entry.key))
entry.key: entry.value,
@@ -167,6 +168,13 @@ class SwarmPartsBackend {
// The reader's own read marks alone, as backend readMarks.ts sends them.
if ((state['readMarks'] as Map?)?[reader ?? 'owner'] case final Map marks)
'readMarks': Map<String, dynamic>.from(marks),
+ // Their own thread marks alone, by conversation, absent when none.
+ if ((state['threadMarks'] as Map?)?[reader ?? 'owner'] case final Map marks
+ when marks.isNotEmpty)
+ 'threadMarks': {
+ for (final MapEntry(:key, :value) in marks.entries)
+ key: Map<String, dynamic>.from(value as Map),
+ },
};
List<MapEntry<String, _HeldPart>> _ordered() =>
@@ -599,6 +607,77 @@ class ConversationServer {
return marks[conversationId] = kept;
}
+ /// The thread marks this device sent, in order: {conversationId, root,
+ /// sequence}.
+ final threadMarkPosts = <Map<String, dynamic>>[];
+
+ /// Every thread-mark write fails: as offline, or with [oldServer] as a
+ /// backend without thread marks answers (no such route).
+ bool failThreadMarks = false;
+ bool oldServer = false;
+
+ /// While set, thread-mark writes wait for it: an answer that arrives late.
+ Completer<void>? holdThreadMarks;
+
+ /// A thread mark [reader] made on another device, as the backend keeps it.
+ void markThreadReadElsewhere(
+ String conversationId,
+ String root,
+ int sequence, {
+ String reader = 'owner',
+ }) => _keepThreadMark(conversationId, root, sequence, reader);
+
+ int _keepThreadMark(
+ String conversationId,
+ String root,
+ int sequence,
+ String reader,
+ ) {
+ final marks =
+ (((state['threadMarks'] ??= <String, dynamic>{}) as Map).putIfAbsent(
+ reader,
+ () => <String, dynamic>{},
+ ) as Map).putIfAbsent(conversationId, () => <String, dynamic>{}) as Map;
+ final kept = max(marks[root] as int? ?? 0, sequence);
+ return marks[root] = kept;
+ }
+
+ Future<Map<String, dynamic>> _markThreadRead(
+ String conversationId,
+ String root,
+ int sequence,
+ String? reader,
+ ) async {
+ threadMarkPosts.add({
+ 'conversationId': conversationId,
+ 'root': root,
+ 'sequence': sequence,
+ });
+ if (holdThreadMarks case final hold?) await hold.future;
+ if (oldServer) {
+ throw const SuperTabFailure('NOT_FOUND', 'Not found.', statusCode: 404);
+ }
+ if (failThreadMarks) {
+ throw const SuperTabFailure('OFFLINE', 'No connection.', statusCode: 503);
+ }
+ final newest =
+ (state['conversations'] as List)
+ .cast<Map<String, dynamic>>()
+ .firstWhere(
+ (g) => g['id'] == conversationId,
+ )['lastMessageSequence']
+ as int? ??
+ 0;
+ return {
+ 'sequence': _keepThreadMark(
+ conversationId,
+ root,
+ min(sequence, newest),
+ reader ?? 'owner',
+ ),
+ };
+ }
+
/// The backend's read-mark route (readMarks.ts): forward only, never past
/// the conversation's newest message.
Map<String, dynamic> _markRead(
@@ -805,6 +884,18 @@ class ConversationServer {
if (read != null) {
return _markRead(read.group(1)!, body!['sequence'] as int, reader);
}
+ final threadRead = method == 'POST'
+ ? RegExp(r'/conversations/([^/]+)/threads/([^/]+)/read$')
+ .firstMatch(path)
+ : null;
+ if (threadRead != null) {
+ return _markThreadRead(
+ Uri.decodeComponent(threadRead.group(1)!),
+ Uri.decodeComponent(threadRead.group(2)!),
+ body!['sequence'] as int,
+ reader,
+ );
+ }
if (method == 'POST') {
posts.add(Map<String, dynamic>.from(body!));
if (holdCommands case final hold?) await hold.future;
@@ -1255,7 +1346,11 @@ class ConversationServer {
leaderboardReads.add(path);
if (failLeaderboardReads > 0) {
failLeaderboardReads--;
- throw const SuperTabFailure('OFFLINE', 'No connection.', statusCode: 503);
+ throw const SuperTabFailure(
+ 'OFFLINE',
+ 'No connection.',
+ statusCode: 503,
+ );
}
final board = leaderboard;
if (board == null) {
diff --git a/mobile/lib/swarm/model.dart b/mobile/lib/swarm/model.dart
index 667acfc45..6fab975b1 100644
--- a/mobile/lib/swarm/model.dart
+++ b/mobile/lib/swarm/model.dart
@@ -449,6 +449,15 @@ class SuperTabSnapshot {
if (key is String && value is num) key: value.toInt(),
}),
buddiesEnabled = row['buddies'] == true,
+ threadMarks = Map.unmodifiable({
+ if (row['threadMarks'] case final Map marks)
+ for (final MapEntry(key: id, value: threads) in marks.entries)
+ if (id is String && threads is Map)
+ id: Map<String, int>.unmodifiable({
+ for (final MapEntry(:key, :value) in threads.entries)
+ if (key is String && value is num) key: value.toInt(),
+ }),
+ }),
buddyStarts = List.unmodifiable([
for (final start
in row['starts'] is List ? row['starts'] as List : const [])
@@ -527,6 +536,11 @@ class SuperTabSnapshot {
/// The reader's own read marks kept on the server (backend readMarks.ts): the newest message sequence read, by
/// conversation, on any of their devices. Never another person's.
final Map<String, int> readMarks;
+
+ /// The reader's own thread marks kept on the server (backend readMarks.ts): by conversation, the newest message
+ /// sequence read in each thread, by its root, on any of their devices. Empty from a backend before them.
+ final Map<String, Map<String, int>> threadMarks;
+
// Built on first use: a read is never changed, and lookups run per row on
// every build.
late final _conversationsById = _byId(conversations, (c) => c.id);
diff --git a/mobile/lib/swarm/parts.dart b/mobile/lib/swarm/parts.dart
index a70932576..16b46af98 100644
--- a/mobile/lib/swarm/parts.dart
+++ b/mobile/lib/swarm/parts.dart
@@ -60,6 +60,7 @@ class SuperTabParts {
// Read marks are held apart from the other derived values (_readMarks).
final derived = Map.of(superTabMap(answer['derived'] ?? const {}));
final marks = derived.remove('readMarks');
+ final threadMarks = derived.remove('threadMarks');
final parts = SuperTabParts._(
_envelope(answer['head'], live: true),
derived,
@@ -79,6 +80,7 @@ class SuperTabParts {
}
}
parts._takeReadMarks(marks);
+ parts._takeThreadMarks(threadMarks);
return parts;
}
@@ -101,6 +103,7 @@ class SuperTabParts {
}
}
parts._takeReadMarks(json['readMarks']);
+ parts._takeThreadMarks(json['threadMarks']);
return parts;
}
@@ -145,6 +148,27 @@ class SuperTabParts {
return moved;
}
+ /// The reader's own thread marks (backend readMarks.ts): by conversation, the newest message sequence read in each
+ /// thread, by its root. Kept as read marks are: the higher of each, and gone with their conversation.
+ final _threadMarks = <String, Map<String, int>>{};
+
+ /// Keeps the higher of each thread mark in [marks] and the one held; whether any moved.
+ bool _takeThreadMarks(Object? marks) {
+ var moved = false;
+ if (marks is! Map) return moved;
+ for (final MapEntry(key: id, value: threads) in marks.entries) {
+ if (id is! String || threads is! Map) continue;
+ for (final MapEntry(key: root, value: sequence) in threads.entries) {
+ if (root is! String || sequence is! num) continue;
+ final held = _threadMarks[id] ??= {};
+ if ((held[root] ?? 0) >= sequence) continue;
+ held[root] = sequence.toInt();
+ moved = true;
+ }
+ }
+ return moved;
+ }
+
/// Dormant groups held with their data from before a stub closed them: a read by id at the stub's version is
/// their current data.
final _stale = <String>{};
@@ -199,6 +223,7 @@ class SuperTabParts {
_dormant.remove(key);
_stale.remove(key);
_readMarks.remove(key);
+ _threadMarks.remove(key);
removed.add(key);
}
} else if (kind == 'groups' && part['dormant'] == true) {
@@ -226,7 +251,10 @@ class SuperTabParts {
}
}
final derived = Map.of(superTabMap(changes['derived'] ?? const {}));
- final marksMoved = _takeReadMarks(derived.remove('readMarks'));
+ final readMarksMoved = _takeReadMarks(derived.remove('readMarks'));
+ // Both run: one moving never skips the other.
+ final marksMoved =
+ _takeThreadMarks(derived.remove('threadMarks')) | readMarksMoved;
if (!const DeepCollectionEquality().equals(derived, _derived)) {
_derived = derived;
changed = true;
@@ -319,6 +347,12 @@ class SuperTabParts {
// Sent only where the server does not try Harness Team accounts yet.
if (_derived['teamAccounts'] == false) state['teamAccounts'] = false;
if (_readMarks.isNotEmpty) state['readMarks'] = Map.of(_readMarks);
+ if (_threadMarks.isNotEmpty) {
+ state['threadMarks'] = {
+ for (final MapEntry(:key, :value) in _threadMarks.entries)
+ key: Map<String, int>.of(value),
+ };
+ }
return state;
}
@@ -339,6 +373,10 @@ class SuperTabParts {
'seen': Map<String, int>.of(_seen),
'stale': _stale.toList(),
'readMarks': Map<String, int>.of(_readMarks),
+ 'threadMarks': {
+ for (final MapEntry(:key, :value) in _threadMarks.entries)
+ key: Map<String, int>.of(value),
+ },
};
/// [json] (from [toJson]) without what the screens never draw: agent starts carry their tasks, and project
diff --git a/mobile/lib/swarm/swarm_activity.dart b/mobile/lib/swarm/swarm_activity.dart
index 0106c50e7..f849cc6f3 100644
--- a/mobile/lib/swarm/swarm_activity.dart
+++ b/mobile/lib/swarm/swarm_activity.dart
@@ -279,10 +279,16 @@ class SwarmActivity extends ChangeNotifier {
_ => false,
};
final read = reader.readMarks?.readThrough(item.chatId);
+ // Read through on any device: the thread's own mark (server or phone).
+ final threadRead = reader.readMarks?.threadThrough(
+ item.chatId,
+ thread.id,
+ );
final opened = marks.values['t:${thread.id}'];
return mine ||
read == null ||
thread.lastSequence <= read ||
+ threadRead != null && thread.lastSequence <= threadRead ||
opened is int && thread.lastSequence <= opened
? 'Read'
: null;
@@ -334,6 +340,11 @@ class SwarmActivity extends ChangeNotifier {
_acted[item.key] = (swarmId: item.swarmId, word: 'Opened');
case SwarmActivityThread(:final thread):
_marks[item.swarmId]?.put('t:${thread.id}', thread.lastSequence);
+ // Read on every device too (backend readMarks.ts thread marks).
+ inbox
+ .reader(item.swarmId)
+ ?.readMarks
+ ?.markThreadRead(item.chatId, thread.id, thread.lastSequence);
case SwarmActivityReaction():
_marks[item.swarmId]?.put('r:${item.message.id}', item.signature);
}
diff --git a/mobile/lib/swarm/swarm_read_marks.dart b/mobile/lib/swarm/swarm_read_marks.dart
index 16b7e9454..26e18be91 100644
--- a/mobile/lib/swarm/swarm_read_marks.dart
+++ b/mobile/lib/swarm/swarm_read_marks.dart
@@ -8,48 +8,74 @@ import '../core/local_key_value_store.dart';
import 'model.dart';
import 'swarm_reader.dart';
-/// Sends the reader's read marks to the server (backend `superTabs/readMarks.ts`, #2131), so a chat read on this phone
-/// reads as read on every other device. The desktop's `SuperTabReadMarkSender`: one request per chat at a time; a
-/// higher mark asked for while one is on its way goes after it, and a lower or equal one is never sent. A failed send
-/// is forgotten, so the next mark asked for tries again; meanwhile this phone's own mark keeps the chat read here.
-class SwarmReadMarkSender {
- SwarmReadMarkSender(this._send);
+/// Sends the reader's read marks to the server (backend `superTabs/readMarks.ts`, #2131), so a chat, or a thread
+/// ([K] names which), read on this phone reads as read on every other device. The desktop's
+/// `SuperTabReadMarkSender`: one request per chat or thread at a time; a higher mark asked for while one is on its way
+/// goes after it, and a lower or equal one is never sent. A failed send is forgotten, so the next mark asked for tries
+/// again (after [retryAfter] when it is no higher); meanwhile this phone's own mark keeps it read here.
+class SwarmReadMarkSender<K> {
+ SwarmReadMarkSender(
+ this._send, {
+ this.retryAfter = Duration.zero,
+ DateTime Function()? now,
+ }) : _now = now ?? DateTime.now;
/// Writes [sequence] as the mark of [conversationId]; completes once the server kept it, or throws.
- final Future<void> Function(String conversationId, int sequence) _send;
+ final Future<void> Function(K conversationId, int sequence) _send;
+
+ /// How long a mark whose send failed waits before it is sent again: a
+ /// thread asks for its mark on every change while it is open, and a server
+ /// without thread marks refuses each.
+ final Duration retryAfter;
+ final DateTime Function() _now;
/// The highest mark sent and kept, by chat.
- final _sent = <String, int>{};
+ final _sent = <K, int>{};
/// The mark waiting for the one on its way, by chat.
- final _next = <String, int>{};
- final _sending = <String>{};
+ final _next = <K, int>{};
+ final _sending = <K>{};
+
+ /// The mark on its way, by conversation: one asked for again meanwhile is not sent twice.
+ final _inFlight = <K, int>{};
+
+ /// The last mark that failed, and when, by chat.
+ final _failed = <K, (int, DateTime)>{};
bool _disposed = false;
/// Asks for [sequence] to be [conversationId]'s mark on the server, unless [kept] (what the server already holds)
/// or a mark sent from here is as high.
- void mark(String conversationId, int sequence, {int kept = 0}) {
+ void mark(K conversationId, int sequence, {int kept = 0}) {
if (_disposed || sequence <= kept) return;
if (sequence <= (_sent[conversationId] ?? 0)) return;
if (sequence <= (_next[conversationId] ?? 0)) return;
+ if (sequence <= (_inFlight[conversationId] ?? 0)) return;
+ if (_failed[conversationId] case (final failed, final at)
+ when sequence <= failed && _now().difference(at) < retryAfter) {
+ return;
+ }
_next[conversationId] = sequence;
if (_sending.add(conversationId)) unawaited(_drain(conversationId));
}
- Future<void> _drain(String conversationId) async {
+ Future<void> _drain(K conversationId) async {
while (!_disposed) {
final sequence = _next.remove(conversationId);
if (sequence == null) break;
+ _inFlight[conversationId] = sequence;
try {
await _send(conversationId, sequence);
+ _failed.remove(conversationId);
if (sequence > (_sent[conversationId] ?? 0)) {
_sent[conversationId] = sequence;
}
} catch (_) {
+ _failed[conversationId] = (sequence, _now());
// Offline, or a server without read marks: this phone's mark stands,
// and the next one asked for is sent afresh.
}
}
+ _inFlight.remove(conversationId);
_sending.remove(conversationId);
}
@@ -77,7 +103,7 @@ class SwarmReadMarks extends ChangeNotifier {
required this.store,
this.saveAfter = Duration.zero,
}) {
- _sender = SwarmReadMarkSender(
+ _sender = SwarmReadMarkSender<String>(
(id, sequence) => reader.request(
'POST',
'/api/super-tabs/${Uri.encodeComponent(reader.tabId)}'
@@ -85,6 +111,16 @@ class SwarmReadMarks extends ChangeNotifier {
body: {'sequence': sequence},
),
);
+ _threadSender = SwarmReadMarkSender<(String, String)>(
+ (thread, sequence) => reader.request(
+ 'POST',
+ '/api/super-tabs/${Uri.encodeComponent(reader.tabId)}'
+ '/conversations/${Uri.encodeComponent(thread.$1)}'
+ '/threads/${Uri.encodeComponent(thread.$2)}/read',
+ body: {'sequence': sequence},
+ ),
+ retryAfter: const Duration(minutes: 1),
+ );
reader.addListener(_swarmChanged);
reader.reads.addListener(_swarmChanged);
unawaited(_load());
@@ -94,6 +130,15 @@ class SwarmReadMarks extends ChangeNotifier {
/// the Swarm's read no longer has go first.
static const kept = 1000;
+ /// The most threads whose marks the phone keeps per Swarm, as the server
+ /// does (backend readMarks.ts THREAD_MARK_CAP): those of chats the Swarm's
+ /// read no longer has go first, then those of the chat kept longest, its
+ /// oldest first (not the oldest overall: no mark keeps a time). A thread
+ /// without one reads
+ /// as far as the phone's other marks say (Activity's opened mark, the
+ /// chat's).
+ static const keptThreads = 500;
+
final SwarmReader reader;
/// How long marks wait to be written: at once by default, after what moved
@@ -102,11 +147,15 @@ class SwarmReadMarks extends ChangeNotifier {
/// Where the phone keeps its marks; null keeps them in memory only.
final LocalKeyValueStore? store;
- late final SwarmReadMarkSender _sender;
+ late final SwarmReadMarkSender<String> _sender;
+ late final SwarmReadMarkSender<(String, String)> _threadSender;
/// The phone's own marks, by chat.
final _marks = <String, int>{};
+ /// The phone's own thread marks, by chat then thread root.
+ final _threads = <String, Map<String, int>>{};
+
/// The marks were read back from the phone (or there is no store).
bool _loaded = false;
@@ -140,6 +189,39 @@ class SwarmReadMarks extends ChangeNotifier {
)
: null;
+ /// How far the person has read [root]'s thread in [conversationId]: the
+ /// higher of the server's thread mark, from any of their devices, and this
+ /// phone's; null until the phone's are read back, and once access is lost.
+ /// Zero for a thread with no mark: the chat's own mark is the caller's.
+ int? threadThrough(String conversationId, String root) =>
+ _loaded && !reader.accessRevoked
+ ? max(
+ reader.snapshot?.threadMarks[conversationId]?[root] ?? 0,
+ _threads[conversationId]?[root] ?? 0,
+ )
+ : null;
+
+ /// The person saw [root]'s thread in [conversationId] through [sequence]:
+ /// sent to the server, and kept on the phone when it moves the phone's mark
+ /// on.
+ void markThreadRead(String conversationId, String root, int sequence) {
+ if (_disposed || reader.accessRevoked || sequence <= 0) return;
+ // A chat the Swarm's read no longer has is not the person's to mark.
+ if (reader.snapshot?.conversation(conversationId) == null) return;
+ _threadSender.mark(
+ (conversationId, root),
+ sequence,
+ kept: reader.snapshot?.threadMarks[conversationId]?[root] ?? 0,
+ );
+ final held = _threads[conversationId] ??= {};
+ if (sequence <= (held[root] ?? 0)) return;
+ // Moved to the end: the newest are the ones a trim keeps.
+ held.remove(root);
+ held[root] = sequence;
+ _saveSoon();
+ notifyListeners();
+ }
+
/// The person saw [conversationId] through [sequence]: sent to the server,
/// and kept on the phone when it moves the phone's mark on.
void markRead(String conversationId, int sequence) {
@@ -157,24 +239,44 @@ class SwarmReadMarks extends ChangeNotifier {
Future<void> _load() async {
final held = <String, int>{};
+ final threads = <String, Map<String, int>>{};
var found = false;
try {
if (await store?.read(_key) case final raw?) {
- if (jsonDecode(raw) case {'marks': final Map marks}) {
+ final value = jsonDecode(raw);
+ if (value case {'marks': final Map marks}) {
for (final MapEntry(:key, :value) in marks.entries) {
if (key is String && value is num) held[key] = value.toInt();
}
found = true;
}
+ // Absent from what a phone before thread marks wrote.
+ if (value case {'threads': final Map kept}) {
+ for (final MapEntry(key: id, value: roots) in kept.entries) {
+ if (id is! String || roots is! Map) continue;
+ for (final MapEntry(:key, :value) in roots.entries) {
+ if (key is String && value is num) {
+ (threads[id] ??= {})[key] = value.toInt();
+ }
+ }
+ }
+ }
}
} catch (_) {
// An unreadable value is no marks: the next first read starts afresh.
}
if (_disposed || reader.accessRevoked) return;
- final marked = _marks.isNotEmpty;
+ final marked = _marks.isNotEmpty || _threads.isNotEmpty;
for (final MapEntry(:key, :value) in held.entries) {
if (value > (_marks[key] ?? 0)) _marks[key] = value;
}
+ // A thread read before the marks were back keeps the higher of the two.
+ for (final MapEntry(key: id, value: roots) in threads.entries) {
+ final now = _threads[id] ??= {};
+ for (final MapEntry(:key, :value) in roots.entries) {
+ if (value > (now[key] ?? 0)) now[key] = value;
+ }
+ }
_loaded = true;
_started |= found;
// A chat read before the marks were back is kept with them.
@@ -211,8 +313,9 @@ class SwarmReadMarks extends ChangeNotifier {
void _forget() {
_save?.cancel();
_save = null;
- final had = _marks.isNotEmpty || _started;
+ final had = _marks.isNotEmpty || _threads.isNotEmpty || _started;
_marks.clear();
+ _threads.clear();
_started = false;
unawaited(store?.delete(_key).catchError((Object _) {}));
if (had) notifyListeners();
@@ -227,13 +330,17 @@ class SwarmReadMarks extends ChangeNotifier {
void _write() {
_save = null;
_trim();
- final value = jsonEncode({'marks': _marks});
+ final value = jsonEncode({
+ 'marks': _marks,
+ if (_threads.isNotEmpty) 'threads': _threads,
+ });
unawaited(store?.write(_key, value).catchError((Object _) {}));
}
/// At most [kept] marks: those of chats the Swarm's read no longer has go
/// first, then the oldest kept.
void _trim() {
+ _trimThreads();
if (_marks.length <= kept) return;
final read = {
for (final group in reader.snapshot?.conversations ?? const []) group.id,
@@ -250,6 +357,24 @@ class SwarmReadMarks extends ChangeNotifier {
}
}
+ /// At most [keptThreads] thread marks: those of chats the Swarm's read no
+ /// longer has go first, then those of the chat kept longest, its oldest
+ /// first.
+ void _trimThreads() {
+ int count() => _threads.values.fold(0, (n, roots) => n + roots.length);
+ if (count() <= keptThreads) return;
+ final read = {
+ for (final group in reader.snapshot?.conversations ?? const []) group.id,
+ };
+ _threads.removeWhere((id, _) => !read.contains(id));
+ // Then the chat kept longest (the first key), its oldest mark first.
+ while (count() > keptThreads) {
+ final id = _threads.keys.first;
+ final roots = _threads[id]!..remove(_threads[id]!.keys.first);
+ if (roots.isEmpty) _threads.remove(id);
+ }
+ }
+
@override
void dispose() {
_disposed = true;
@@ -261,6 +386,7 @@ class SwarmReadMarks extends ChangeNotifier {
reader.removeListener(_swarmChanged);
reader.reads.removeListener(_swarmChanged);
_sender.dispose();
+ _threadSender.dispose();
super.dispose();
}
}
diff --git a/mobile/lib/swarm/swarm_thread.dart b/mobile/lib/swarm/swarm_thread.dart
index 6ae805399..0cd270853 100644
--- a/mobile/lib/swarm/swarm_thread.dart
+++ b/mobile/lib/swarm/swarm_thread.dart
@@ -149,6 +149,16 @@ class SwarmThread extends ChangeNotifier implements SwarmFilesBox {
/// The person may reply: as they may post in the chat.
bool get canPost => chat.canPost;
+ /// The person sees the thread: read through its newest row on every device
+ /// (backend readMarks.ts thread marks), once its own read brought it.
+ void markRead() {
+ if (_disposed || reader.accessRevoked || state != SwarmChatState.loaded) {
+ return;
+ }
+ final last = messages.lastOrNull?.sequence ?? 0;
+ if (last > 0) reader.readMarks?.markThreadRead(chat.id, root, last);
+ }
+
/// Tells the reply box what changed in it — the draft, Also send, a reply
/// leaving it — while this notifier itself tells the messages.
Listenable get composer => _composer;
diff --git a/mobile/lib/swarm/swarm_thread_page.dart b/mobile/lib/swarm/swarm_thread_page.dart
index 6adc4308f..e787adba3 100644
--- a/mobile/lib/swarm/swarm_thread_page.dart
+++ b/mobile/lib/swarm/swarm_thread_page.dart
@@ -91,9 +91,15 @@ class _SwarmThreadPageState extends State<SwarmThreadPage> {
super.initState();
_text.addListener(_typed);
_thread.composer.addListener(_drafted);
+ _thread.addListener(_seen);
unawaited(_thread.load());
}
+ /// While on screen, what the thread shows is read ([SwarmThread.markRead]).
+ void _seen() {
+ if (_watching) _thread.markRead();
+ }
+
@override
void didChangeDependencies() {
super.didChangeDependencies();
@@ -106,12 +112,14 @@ class _SwarmThreadPageState extends State<SwarmThreadPage> {
_watching = watch;
if (watch) session.watch(widget.swarmId);
}
+ _seen();
}
@override
void dispose() {
if (_watching) _session?.unwatch(widget.swarmId);
_thread.composer.removeListener(_drafted);
+ _thread.removeListener(_seen);
_thread.dispose();
_attachNote.dispose();
_farBack.dispose();
diff --git a/mobile/test/swarm/swarm_activity_fixture.dart b/mobile/test/swarm/swarm_activity_fixture.dart
index 359bf6d95..e92dd1fba 100644
--- a/mobile/test/swarm/swarm_activity_fixture.dart
+++ b/mobile/test/swarm/swarm_activity_fixture.dart
@@ -46,10 +46,12 @@ Map<String, dynamic> activityThread(
/// One Swarm, project Mobile: decision d1 (recommending "Merge"), River's
/// tag m1, work g1 waiting on the person, and a project chat whose newest
-/// page [page] answers. The server's read mark of that chat is [readMark].
+/// page [page] answers. The server's read mark of that chat is [readMark],
+/// and its thread marks by root [threadMarks] (none from an older server).
FakeSwarm activityBackend({
Map<String, dynamic> Function()? page,
int readMark = 10,
+ Map<String, int>? threadMarks,
int chatSequence = 12,
bool decision = true,
List<Map<String, dynamic>> groups = const [],
@@ -65,6 +67,7 @@ FakeSwarm activityBackend({
'decisionsWaiting': decision ? 1 : 0,
'mentionsWaiting': 1,
'readMarks': {leadChat: readMark},
+ 'threadMarks': ?(threadMarks == null ? null : {leadChat: threadMarks}),
};
fake.routes['/api/super-tabs/$tab/parts'] = (_) {
final load = firstLoad(
diff --git a/mobile/test/swarm/swarm_activity_test.dart b/mobile/test/swarm/swarm_activity_test.dart
index 130ea2281..74362114d 100644
--- a/mobile/test/swarm/swarm_activity_test.dart
+++ b/mobile/test/swarm/swarm_activity_test.dart
@@ -722,6 +722,83 @@ void main() {
);
});
});
+
+ /// Thread marks kept on the server (backend readMarks.ts): a thread opened
+ /// on any device reads as read here, and one opened here reads as read on
+ /// every other.
+ group('Threads read through their own marks', () {
+ const page = activityPage;
+
+ SwarmActivityItem rootOne(SwarmActivity activity) => activity
+ .items(SwarmActivityFilter.threads)
+ .firstWhere((item) => item.key == 'thread:$tab:root-1');
+
+ List<({String path, Object? sequence})> threadWrites(FakeSwarm fake) => [
+ for (final call in fake.calls)
+ if (call.method == 'POST' && call.path.contains('/threads/'))
+ (path: call.path, sequence: call.body?['sequence']),
+ ];
+
+ test('a thread opened only on another device is not news', () async {
+ // The chat itself was read only through 10; root-1's reply is at 12.
+ final session = await start(
+ backend(page: page, threadMarks: {'root-1': 12}),
+ );
+ final activity = session.activity..watch();
+ await settle();
+ expect(activity.handled(rootOne(activity)), 'Read');
+ expect(activity.count(SwarmActivityFilter.threads), 0);
+ });
+
+ test("a reply past the thread's mark is news", () async {
+ final session = await start(
+ backend(page: page, threadMarks: {'root-1': 11}),
+ );
+ final activity = session.activity..watch();
+ await settle();
+ expect(activity.handled(rootOne(activity)), isNull);
+ expect(activity.count(SwarmActivityFilter.threads), 1);
+ });
+
+ test('Reply sends the thread\'s mark to the server once', () async {
+ final fake = backend(page: page);
+ fake.routes['/api/super-tabs/$tab/conversations/$leadChat/threads/'] = (
+ body,
+ ) => {'sequence': body!['sequence']};
+ final session = await start(fake);
+ final activity = session.activity..watch();
+ await settle();
+ activity.replied(rootOne(activity));
+ activity.replied(rootOne(activity));
+ await settle();
+ expect(threadWrites(fake), [
+ (
+ path:
+ '/api/super-tabs/$tab/conversations/$leadChat/threads/root-1/read',
+ sequence: 12,
+ ),
+ ]);
+ expect(activity.handled(rootOne(activity)), 'Read');
+ });
+
+ test('on a server without thread marks, Reply still reads the thread on '
+ 'this phone, across a restart', () async {
+ final store = MemoryKeyValueStore();
+ final first = await start(backend(page: page), store: store);
+ final activity = first.activity..watch();
+ await settle();
+ activity.replied(rootOne(activity));
+ await settle();
+ expect(
+ store.values['swarm_read_marks.$me.$tab'],
+ contains('"root-1":12'),
+ );
+
+ final again = await start(backend(page: page), store: store);
+ final reader = again.inbox.reader(tab)!;
+ expect(reader.readMarks!.threadThrough(leadChat, 'root-1'), 12);
+ });
+ });
}
/// A phone store whose read of [slow] waits for [release].
diff --git a/mobile/test/swarm/swarm_read_marks_test.dart b/mobile/test/swarm/swarm_read_marks_test.dart
index ba57dfba6..90b449c79 100644
--- a/mobile/test/swarm/swarm_read_marks_test.dart
+++ b/mobile/test/swarm/swarm_read_marks_test.dart
@@ -6,6 +6,7 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:harness_mobile/swarm/model.dart';
import 'package:harness_mobile/swarm/swarm_read_marks.dart';
import 'package:harness_mobile/swarm/swarm_reader.dart';
+import 'package:harness_mobile/swarm/swarm_request.dart';
import '../voice_fakes.dart' show MemoryKeyValueStore;
import 'swarm_fixture.dart';
@@ -413,6 +414,175 @@ void main() {
expect(SuperTabSnapshot.fromJson(state).readMarks, isEmpty);
},
);
+
+ group('thread marks (backend readMarks.ts)', () {
+ List<({String path, Object? sequence})> threadWrites(FakeSwarm swarm) => [
+ for (final call in swarm.calls)
+ if (call.method == 'POST' && call.path.contains('/threads/'))
+ (path: call.path, sequence: call.body?['sequence']),
+ ];
+ const threadPath =
+ '/api/super-tabs/$tab/conversations/$work/threads/root-a/read';
+
+ test("the server's thread mark reads the thread; a lower one read later "
+ 'never lowers it', () async {
+ final r = rig();
+ r.swarm.routes['/api/super-tabs/$tab/parts'] = (_) => firstLoad(
+ derived: {
+ 'threadMarks': {
+ work: {'root-a': 3},
+ },
+ },
+ groups: [groupData(work, title: 'Work', lastMessageSequence: 4)],
+ );
+ await r.reader.refresh();
+ await settle();
+ expect(r.marks.threadThrough(work, 'root-a'), 3);
+ r.swarm.routes['/api/super-tabs/$tab/changes'] = (_) => changes(
+ cursor: 'c3',
+ derived: {
+ 'threadMarks': {
+ work: {'root-a': 2},
+ },
+ },
+ );
+ await r.reader.refresh();
+ await settle();
+ expect(r.marks.threadThrough(work, 'root-a'), 3);
+ expect(r.marks.threadThrough(work, 'root-b'), 0);
+ });
+
+ test('a thread read here is sent once, and kept on the phone across a '
+ 'restart', () async {
+ final r = rig();
+ await r.reader.refresh();
+ await settle();
+ r.marks.markThreadRead(work, 'root-a', 3);
+ r.marks.markThreadRead(work, 'root-a', 3);
+ r.marks.markThreadRead(work, 'root-a', 2);
+ await settle();
+ expect(threadWrites(r.swarm), [(path: threadPath, sequence: 3)]);
+ expect(r.marks.threadThrough(work, 'root-a'), 3);
+
+ final again = rig(store: r.store);
+ await again.reader.refresh();
+ await settle();
+ expect(again.marks.threadThrough(work, 'root-a'), 3);
+ });
+
+ test("another account's thread marks on this phone never count", () async {
+ final store = MemoryKeyValueStore()
+ ..values['swarm_read_marks.person-other.$tab'] = jsonEncode({
+ 'marks': {work: 9},
+ 'threads': {
+ work: {'root-a': 9},
+ },
+ });
+ final r = rig(store: store);
+ await r.reader.refresh();
+ await settle();
+ expect(r.marks.threadThrough(work, 'root-a'), 0);
+ });
+
+ test('a chat the Swarm does not read is never marked', () async {
+ final r = rig();
+ await r.reader.refresh();
+ await settle();
+ r.marks.markThreadRead('group-private', 'root-a', 3);
+ await settle();
+ expect(threadWrites(r.swarm), isEmpty);
+ expect(r.marks.threadThrough('group-private', 'root-a'), 0);
+ });
+
+ test(
+ 'losing access forgets the thread marks and sends none after',
+ () async {
+ final r = rig();
+ await r.reader.refresh();
+ await settle();
+ r.marks.markThreadRead(work, 'root-a', 3);
+ await settle();
+ expect(r.store.values[key], contains('root-a'));
+ r.reader.revoke();
+ await settle();
+ expect(r.store.values[key], isNull);
+ expect(r.marks.threadThrough(work, 'root-a'), isNull);
+ r.marks.markThreadRead(work, 'root-a', 4);
+ await settle();
+ expect(threadWrites(r.swarm), hasLength(1));
+ },
+ );
+
+ test('a server without thread marks: the phone keeps the mark, and the '
+ 'same one is not sent again at once', () async {
+ final r = rig();
+ r.swarm.routes['/api/super-tabs/$tab/conversations/$work/threads/'] =
+ (_) => throw const SuperTabFailure(
+ 'NOT_FOUND',
+ 'No such route.',
+ statusCode: 404,
+ );
+ await r.reader.refresh();
+ await settle();
+ for (var i = 0; i < 3; i++) {
+ r.marks.markThreadRead(work, 'root-a', 3);
+ await settle();
+ }
+ expect(threadWrites(r.swarm), hasLength(1));
+ expect(r.marks.threadThrough(work, 'root-a'), 3);
+ // A later reply's mark goes at once.
+ r.marks.markThreadRead(work, 'root-a', 4);
+ await settle();
+ expect(threadWrites(r.swarm), hasLength(2));
+ });
+
+ test('thread marks are kept for at most a bound of threads, those of '
+ 'chats the Swarm no longer reads going first', () async {
+ final store = MemoryKeyValueStore()
+ ..values[key] = jsonEncode({
+ 'marks': <String, int>{},
+ 'threads': {
+ 'gone': {'old': 1},
+ work: {
+ for (var i = 0; i < SwarmReadMarks.keptThreads; i++) 't$i': 1,
+ },
+ },
+ });
+ final r = rig(store: store);
+ await r.reader.refresh();
+ await settle();
+ r.marks.markThreadRead(work, 'newest', 4);
+ await settle();
+ final kept = jsonDecode(store.values[key]!)['threads'] as Map;
+ expect(kept.keys, [work]);
+ expect(kept[work] as Map, hasLength(SwarmReadMarks.keptThreads));
+ expect((kept[work] as Map).keys, contains('newest'));
+ expect((kept[work] as Map).keys, isNot(contains('t0')));
+ });
+
+ test("a snapshot reads the server's thread marks, and none from an "
+ 'older server', () {
+ final state = {
+ ...headData(),
+ 'people': [person(me)],
+ 'agents': <Object>[],
+ 'conversations': [groupData(work, lastMessageSequence: 4)],
+ };
+ expect(
+ SuperTabSnapshot.fromJson({
+ ...state,
+ 'threadMarks': {
+ work: {'root-a': 3, 'bad': 'x'},
+ 'junk': 4,
+ },
+ }).threadMarks,
+ {
+ work: {'root-a': 3},
+ },
+ );
+ expect(SuperTabSnapshot.fromJson(state).threadMarks, isEmpty);
+ });
+ });
}
/// A store whose read answers only when told.
diff --git a/mobile/test/swarm/swarm_thread_test.dart b/mobile/test/swarm/swarm_thread_test.dart
index 3bd4c49c6..53410af59 100644
--- a/mobile/test/swarm/swarm_thread_test.dart
+++ b/mobile/test/swarm/swarm_thread_test.dart
@@ -5,6 +5,7 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:harness_mobile/swarm/model.dart';
import 'package:harness_mobile/swarm/swarm_conversation.dart';
import 'package:harness_mobile/swarm/swarm_outgoing_files.dart';
+import 'package:harness_mobile/swarm/swarm_read_marks.dart';
import 'package:harness_mobile/swarm/swarm_reader.dart';
import 'package:harness_mobile/swarm/swarm_request.dart';
import 'package:harness_mobile/swarm/swarm_thread.dart';
@@ -118,6 +119,53 @@ void main() {
return t;
}
+ group('read on every device (backend readMarks.ts thread marks)', () {
+ List<Object?> threadWrites(FakeSwarm swarm) => [
+ for (final call in swarm.calls)
+ if (call.method == 'POST' && call.path.contains('/threads/'))
+ (call.path, call.body?['sequence']),
+ ];
+
+ Future<({FakeSwarm swarm, SwarmReader reader, SwarmThread thread})>
+ marked() async {
+ final r = await rig();
+ r.swarm.routes['/api/super-tabs/$tab/conversations/g1/threads/'] = (
+ body,
+ ) => {'sequence': body!['sequence']};
+ final marks = SwarmReadMarks(r.reader, store: null);
+ addTearDown(marks.dispose);
+ r.reader.readMarks = marks;
+ return (swarm: r.swarm, reader: r.reader, thread: open(r.chat));
+ }
+
+ test('a thread seen once its read is back marks it through its newest '
+ 'row, once', () async {
+ final r = await marked();
+ // Nothing of it read yet: nothing to mark.
+ r.thread.markRead();
+ await settle();
+ expect(threadWrites(r.swarm), isEmpty);
+ await r.thread.load();
+ r.thread.markRead();
+ r.thread.markRead();
+ await settle();
+ expect(threadWrites(r.swarm), [
+ ('/api/super-tabs/$tab/conversations/g1/threads/m1/read', 2),
+ ]);
+ expect(r.reader.readMarks!.threadThrough('g1', 'm1'), 2);
+ });
+
+ test('once access is lost the thread marks nothing', () async {
+ final r = await marked();
+ await r.thread.load();
+ r.reader.revoke();
+ await settle();
+ r.thread.markRead();
+ await settle();
+ expect(threadWrites(r.swarm), isEmpty);
+ });
+ });
+
test('opening reads the thread through the chat\'s own path: its root, '
'then its replies', () async {
final r = await rig();