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.

feat(backend): hot Swarm reads through indexes, Mongo pool defaults, hot body limit (stability 2.7, 2.8) (#1932)

0xken1577cc59df3e14
diff --git a/backend/integration/bench/mongo.ts b/backend/integration/bench/mongo.ts
index 936636d9f..05e0d87d7 100644
--- a/backend/integration/bench/mongo.ts
+++ b/backend/integration/bench/mongo.ts
@@ -5,6 +5,7 @@ import { join } from 'node:path'
import { tmpdir } from 'node:os'
import { MongoMemoryReplSet } from 'mongodb-memory-server-core'
import { PrismaClient } from '@prisma/client'
+import { POOL_DEFAULTS, withPoolDefaults } from '../../src/lib/databaseUrl.js'
export interface LocalDatabase {
db: PrismaClient
@@ -24,6 +25,7 @@ export async function localDatabase(): Promise<LocalDatabase> {
await promisify(execFile)(join(process.cwd(), 'node_modules/.bin/prisma'), ['db', 'push', '--skip-generate'], {
env: { ...process.env, DATABASE_URL: uri }, timeout: 60000,
})
- const db = new PrismaClient({ datasources: { db: { url: uri } } })
+ // The backend's own pool options (src/lib/prisma.ts), so the run measures the pool production has.
+ const db = new PrismaClient({ datasources: { db: { url: withPoolDefaults(uri, POOL_DEFAULTS) } } })
return { db, version, async stop() { await db.$disconnect(); await replica.stop() } }
}
diff --git a/backend/integration/bench/profile.test.ts b/backend/integration/bench/profile.test.ts
new file mode 100644
index 000000000..511c43a96
--- /dev/null
+++ b/backend/integration/bench/profile.test.ts
@@ -0,0 +1,69 @@
+import { describe, expect, it } from 'vitest'
+import { queryShape, renderProfile, summarizeProfile, type ProfileEntry } from './profile.js'
+
+const find = (filter: Record<string, unknown>, extra: Partial<ProfileEntry> = {}): ProfileEntry => ({ op: 'command', ns: 'bench.SwarmGroup',
+ command: { aggregate: 'SwarmGroup', pipeline: [{ $match: filter }, { $project: { _id: 1 } }] }, planSummary: 'IXSCAN { tabId: 1, key: 1 }',
+ docsExamined: 1, keysExamined: 1, nreturned: 1, millis: 0, ...extra })
+
+describe('queryShape', () => {
+ it('keeps fields and operators, never values', () => {
+ expect(queryShape({ tabId: 't1', status: { $in: ['queued', 'claimed'] }, $or: [{ key: { $in: ['a'] } }, { executionMachineId: { $ne: null } }] }))
+ .toBe('{"$or":[{"key":{"$in":"?"}},{"executionMachineId":{"$ne":"?"}}],"status":{"$in":"?"},"tabId":"?"}')
+ })
+
+ it('keeps the fields Prisma compares inside $expr', () => {
+ expect(queryShape({ $expr: { $and: [{ $eq: ['$tabId', 't1'] }, { $or: [{ $eq: ['$status', 'queued'] }, { $eq: ['$status', 'claimed'] }] }] } }))
+ .toBe('{"$expr":{"$and":[{"$eq":["$tabId","?"]},{"$or":[{"$eq":["$status","?"]},{"$eq":["$status","?"]}]}]}}')
+ })
+
+ it('is the same for the same filter in another field order', () => {
+ expect(queryShape({ b: 1, a: { $gt: 2 } })).toBe(queryShape({ a: { $gt: 9 }, b: 'x' }))
+ })
+})
+
+describe('summarizeProfile', () => {
+ it('groups operations by collection and filter shape, with plans, documents read and time percentiles', () => {
+ const entries = [
+ find({ tabId: 'a', deleted: false }, { millis: 1, docsExamined: 10 }),
+ find({ tabId: 'b', deleted: false }, { millis: 3, docsExamined: 30 }),
+ find({ tabId: 'c', deleted: false }, { millis: 40, docsExamined: 20, planSummary: 'COLLSCAN' }),
+ { op: 'update', ns: 'bench.SuperTabDelivery', command: { q: { _id: { $in: ['d'] } }, u: { $set: { status: 'claimed' } } },
+ planSummary: 'IXSCAN { _id: 1 }', docsExamined: 1, keysExamined: 1, nreturned: 0, millis: 2 },
+ ]
+ const summary = summarizeProfile(entries)
+ expect(summary.operations).toBe(4)
+ expect(summary.collscans).toBe(1)
+ expect(summary.p95Ms).toBe(40)
+ expect(summary.shapes).toEqual([
+ { collection: 'SwarmGroup', op: 'aggregate', shape: '{"deleted":"?","tabId":"?"}', count: 3, collscans: 1,
+ plans: ['COLLSCAN', 'IXSCAN { tabId: 1, key: 1 }'], docsExaminedMax: 30, docsExaminedP95: 30, keysExaminedMax: 1, returnedMax: 1, p50Ms: 3, p95Ms: 40, maxMs: 40 },
+ { collection: 'SuperTabDelivery', op: 'update', shape: '{"_id":{"$in":"?"}}', count: 1, collscans: 0,
+ plans: ['IXSCAN { _id: 1 }'], docsExaminedMax: 1, docsExaminedP95: 1, keysExaminedMax: 1, returnedMax: 0, p50Ms: 2, p95Ms: 2, maxMs: 2 },
+ ])
+ })
+
+ it('leaves out the profiler reading itself and operations that read no collection', () => {
+ const summary = summarizeProfile([
+ { op: 'query', ns: 'bench.system.profile', command: { find: 'system.profile', filter: {} }, planSummary: 'COLLSCAN', docsExamined: 5, millis: 0 },
+ { op: 'command', ns: 'bench.$cmd', command: { endSessions: [] }, millis: 0 },
+ { op: 'command', ns: 'bench.SwarmHead', command: { commitTransaction: 1 }, millis: 0 },
+ ])
+ expect(summary).toEqual({ operations: 0, collscans: 0, p95Ms: null, shapes: [] })
+ })
+})
+
+describe('renderProfile', () => {
+ it('states the KPI lines first, then one row per shape with its plan and what it read', () => {
+ const summary = summarizeProfile([find({ tabId: 'a' }, { millis: 25, docsExamined: 120, planSummary: 'COLLSCAN' })])
+ expect(renderProfile({ ...summary, peakConnections: 7 }).split('\n')).toEqual([
+ '## Database operations (Mongo profiler)',
+ '',
+ 'Operations: 1. Full collection scans: 1. p95: 25 ms. Peak server connections: 7.',
+ '',
+ '| Collection | Op | Filter shape | Count | COLLSCAN | Plans | Docs read p95 | Docs read max | Returned max | p50 ms | p95 ms | max ms |',
+ '|---|---|---|---|---|---|---|---|---|---|---|---|',
+ '| SwarmGroup | aggregate | `{"tabId":"?"}` | 1 | 1 | COLLSCAN | 120 | 120 | 1 | 25 | 25 | 25 |',
+ '',
+ ])
+ })
+})
diff --git a/backend/integration/bench/profile.ts b/backend/integration/bench/profile.ts
new file mode 100644
index 000000000..8c61b55ac
--- /dev/null
+++ b/backend/integration/bench/profile.ts
@@ -0,0 +1,140 @@
+import type { PrismaClient } from '@prisma/client'
+import { percentile } from './metrics.js'
+
+/** One `system.profile` document, with only the fields this reads. */
+export interface ProfileEntry {
+ op: string
+ ns: string
+ command?: Record<string, unknown>
+ planSummary?: string
+ docsExamined?: number
+ keysExamined?: number
+ nreturned?: number
+ millis: number
+}
+
+/** The operations of one collection with one filter shape: their plans, what they read, and how long they took. */
+export interface ShapeRow {
+ collection: string
+ op: string
+ shape: string
+ count: number
+ collscans: number
+ plans: string[]
+ docsExaminedMax: number
+ docsExaminedP95: number
+ keysExaminedMax: number
+ returnedMax: number
+ p50Ms: number
+ p95Ms: number
+ maxMs: number
+}
+
+export interface ProfileSummary { operations: number; collscans: number; p95Ms: number | null; shapes: ShapeRow[] }
+
+/** A filter with every value replaced by `?` and its fields sorted: queries that differ only in their values share it. Field
+ * paths inside `$expr` (Prisma's filters, `{ $eq: ['$tabId', value] }`) stay, and a list of values is one `?`. */
+export function queryShape(filter: unknown): string {
+ const shape = (value: unknown): unknown => {
+ if (typeof value === 'string' && value.startsWith('$')) return value
+ if (Array.isArray(value)) {
+ const items = value.map(shape)
+ return items.length && items.some(item => item !== '?') ? items : '?'
+ }
+ if (!value || typeof value !== 'object') return '?'
+ return Object.fromEntries(Object.keys(value).sort().map(key => [key, shape((value as Record<string, unknown>)[key])]))
+ }
+ return JSON.stringify(shape(filter))
+}
+
+/** What an operation filtered on, by how the profiler records each kind: Prisma reads through `aggregate`. */
+function filterOf(entry: ProfileEntry): { op: string; filter: unknown } | null {
+ const command = entry.command ?? {}
+ if (Array.isArray(command.pipeline)) {
+ const match = (command.pipeline as Array<Record<string, unknown>>).find(stage => '$match' in stage)
+ return { op: 'aggregate', filter: match?.$match ?? {} }
+ }
+ if ('filter' in command) return { op: command.find ? 'find' : entry.op, filter: command.filter }
+ if ('q' in command) return { op: entry.op, filter: command.q }
+ if ('query' in command) return { op: entry.op, filter: command.query }
+ return null
+}
+
+const collectionOf = (ns: string) => ns.slice(ns.indexOf('.') + 1)
+
+/** The profiled operations that read a collection, grouped by collection and filter shape, most frequent first. */
+export function summarizeProfile(entries: readonly ProfileEntry[]): ProfileSummary {
+ const groups = new Map<string, { collection: string; op: string; shape: string; entries: ProfileEntry[] }>()
+ for (const entry of entries) {
+ const collection = collectionOf(entry.ns)
+ if (collection.startsWith('system.') || collection === '$cmd') continue
+ const read = filterOf(entry)
+ if (!read) continue
+ const shape = queryShape(read.filter), key = `${collection}\0${read.op}\0${shape}`
+ const group = groups.get(key) ?? { collection, op: read.op, shape, entries: [] }
+ group.entries.push(entry)
+ groups.set(key, group)
+ }
+ const counted = [...groups.values()].flatMap(g => g.entries)
+ const shapes = [...groups.values()].map(({ collection, op, shape, entries: rows }): ShapeRow => {
+ const ms = rows.map(r => r.millis), docs = rows.map(r => r.docsExamined ?? 0)
+ return { collection, op, shape, count: rows.length, collscans: rows.filter(r => r.planSummary === 'COLLSCAN').length,
+ plans: [...new Set(rows.map(r => r.planSummary ?? 'none'))].sort(), docsExaminedMax: Math.max(...docs), docsExaminedP95: percentile(docs, 95)!,
+ keysExaminedMax: Math.max(...rows.map(r => r.keysExamined ?? 0)), returnedMax: Math.max(...rows.map(r => r.nreturned ?? 0)),
+ p50Ms: percentile(ms, 50)!, p95Ms: percentile(ms, 95)!, maxMs: Math.max(...ms) }
+ }).sort((a, b) => b.count - a.count || a.collection.localeCompare(b.collection) || a.shape.localeCompare(b.shape))
+ return { operations: counted.length, collscans: counted.filter(r => r.planSummary === 'COLLSCAN').length,
+ p95Ms: percentile(counted.map(r => r.millis), 95), shapes }
+}
+
+/** The report section: the KPI line (full scans, p95, peak connections), then every shape, most frequent first. */
+export function renderProfile(profile: ProfileSummary & { peakConnections: number }): string {
+ const header = ['Collection', 'Op', 'Filter shape', 'Count', 'COLLSCAN', 'Plans', 'Docs read p95', 'Docs read max', 'Returned max', 'p50 ms', 'p95 ms', 'max ms']
+ const rows = profile.shapes.map(r => [r.collection, r.op, `\`${r.shape}\``, r.count, r.collscans, r.plans.join(', '), r.docsExaminedP95, r.docsExaminedMax,
+ r.returnedMax, r.p50Ms, r.p95Ms, r.maxMs].map(String))
+ return ['## Database operations (Mongo profiler)', '',
+ `Operations: ${profile.operations}. Full collection scans: ${profile.collscans}. p95: ${profile.p95Ms ?? '–'} ms. Peak server connections: ${profile.peakConnections}.`, '',
+ `| ${header.join(' | ')} |`, `|${header.map(() => '---').join('|')}|`, ...rows.map(r => `| ${r.join(' | ')} |`), ''].join('\n')
+}
+
+/** A `system.profile` aggregation that keeps of each operation only what `ProfileEntry` reads: its filter (a find's, an update's,
+ * or the first `$match` of a pipeline, as `match`) and never its whole command. Session ids, cluster times and a pipeline's
+ * own `$type` expressions are more than Prisma's raw reply can decode. */
+export const PROFILE_STAGES = [{ $project: { _id: 0, op: 1, ns: 1, planSummary: 1, docsExamined: 1, keysExamined: 1, nreturned: 1, millis: 1,
+ 'command.aggregate': 1, 'command.find': 1, 'command.filter': 1, 'command.q': 1, 'command.query': 1,
+ match: { $first: { $map: { input: { $filter: { input: { $ifNull: ['$command.pipeline', []] },
+ cond: { $ne: [{ $type: { $getField: { field: { $literal: '$match' }, input: '$$this' } } }, 'missing'] } } },
+ in: { $getField: { field: { $literal: '$match' }, input: '$$this' } } } } } } }]
+
+/** A row of PROFILE_STAGES as the ProfileEntry it stands for. */
+export function profileEntry(row: ProfileEntry & { match?: unknown }): ProfileEntry {
+ const { match, ...entry } = row
+ return { ...entry, millis: Number(entry.millis),
+ ...match !== undefined || entry.command?.aggregate ? { command: { ...entry.command, pipeline: [{ $match: match ?? {} }] } } : {} }
+}
+
+/** Profiles every operation on the bench's database from now on (level 2: all, whatever their time). The default
+ * `system.profile` keeps 1 MB, a few seconds of the load; this one keeps 256 MB. */
+export async function startProfiling(db: PrismaClient): Promise<void> {
+ await db.$runCommandRaw({ profile: 0 })
+ await db.$runCommandRaw({ drop: 'system.profile' }).catch(() => {})
+ await db.$runCommandRaw({ create: 'system.profile', capped: true, size: 256 * 1024 * 1024 })
+ await db.$runCommandRaw({ profile: 2, slowms: 0 })
+}
+
+/** Stops profiling and summarizes what it recorded. */
+export async function readProfile(db: PrismaClient): Promise<ProfileSummary> {
+ await db.$runCommandRaw({ profile: 0 })
+ // Pages that each fit one batch: a cursor id goes past 2^53 and comes back from Prisma's raw reply rounded.
+ const entries: ProfileEntry[] = []
+ for (;;) {
+ const page = (await db.$runCommandRaw({ aggregate: 'system.profile', pipeline: [{ $skip: entries.length }, { $limit: PAGE }, ...PROFILE_STAGES],
+ cursor: { batchSize: PAGE } }) as unknown as Cursor).cursor.firstBatch ?? []
+ entries.push(...page)
+ if (page.length < PAGE) break
+ }
+ return summarizeProfile(entries.map(profileEntry))
+}
+
+const PAGE = 5000
+interface Cursor { cursor: { firstBatch?: ProfileEntry[] } }
diff --git a/backend/integration/bench/run.ts b/backend/integration/bench/run.ts
index 77158450a..27d52a14a 100644
--- a/backend/integration/bench/run.ts
+++ b/backend/integration/bench/run.ts
@@ -1,23 +1,31 @@
import { mkdir, writeFile } from 'node:fs/promises'
import { join } from 'node:path'
+import type { PrismaClient } from '@prisma/client'
import { runBench } from './bench.js'
import { parseArgs } from './config.js'
import { localDatabase } from './mongo.js'
+import { readProfile, renderProfile, startProfiling } from './profile.js'
import { renderMarkdown } from './report.js'
import { benchStamps } from './stamps.js'
-/** `npm run bench:swarm-state -- [options]` (README.md). Writes `<driver>-<time>.json` and `.md` to the results folder. */
+/** `npm run bench:swarm-state -- [options] [--profile]` (README.md). Writes `<driver>-<time>.json` and `.md` to the results
+ * folder; with `--profile`, every database operation of the run goes through Mongo's profiler and the report adds what each
+ * query shape read and how long it took. */
async function main() {
- const { config, out, driver, cache } = parseArgs(process.argv.slice(2))
+ const argv = process.argv.slice(2), profile = argv.includes('--profile')
+ const { config, out, driver, cache } = parseArgs(argv.filter(arg => arg !== '--profile'))
const folder = out ?? join(process.cwd(), 'integration/bench/results')
const database = await localDatabase()
const stamps = await benchStamps(cache)
try {
+ if (profile) await startProfiling(database.db)
+ const peak = profile ? peakConnections(database.db) : null
const report = { ...await runBench(config, driver, database.db), cache: stamps.label }
+ const profiled = profile ? { ...await readProfile(database.db), peakConnections: await peak!.stop() } : null
const name = `${report.driver}-${report.startedAt.replaceAll(':', '-').replace(/\.\d+Z$/, 'Z')}`
- const markdown = renderMarkdown(report)
+ const markdown = renderMarkdown(report) + (profiled ? `\n${renderProfile(profiled)}` : '')
await mkdir(folder, { recursive: true })
- await writeFile(join(folder, `${name}.json`), `${JSON.stringify(report, null, 2)}\n`)
+ await writeFile(join(folder, `${name}.json`), `${JSON.stringify(profiled ? { ...report, profile: profiled } : report, null, 2)}\n`)
await writeFile(join(folder, `${name}.md`), markdown)
process.stdout.write(`${markdown}\nWrote ${join(folder, name)}.json and .md\n`)
} finally {
@@ -26,6 +34,19 @@ async function main() {
}
}
+/** The most connections the server held at once, sampled every 250 ms: the bench's one client, so its pool's peak. */
+function peakConnections(db: PrismaClient) {
+ let peak = 0, running = true
+ const sampling = (async () => {
+ while (running) {
+ const status = await db.$runCommandRaw({ serverStatus: 1, repl: 0, metrics: 0, locks: 0 }) as { connections?: { current?: number } }
+ peak = Math.max(peak, Number(status.connections?.current ?? 0))
+ await new Promise(resolve => setTimeout(resolve, 250))
+ }
+ })()
+ return { async stop() { running = false; await sampling; return peak } }
+}
+
main().catch(error => {
process.stderr.write(`${error instanceof Error ? error.stack : String(error)}\n`)
process.exitCode = 1
diff --git a/backend/integration/superTabHotReads.test.ts b/backend/integration/superTabHotReads.test.ts
new file mode 100644
index 000000000..9148277aa
--- /dev/null
+++ b/backend/integration/superTabHotReads.test.ts
@@ -0,0 +1,172 @@
+import { randomUUID } from 'node:crypto'
+import { execFile } from 'node:child_process'
+import { promisify } from 'node:util'
+import { join } from 'node:path'
+import { tmpdir } from 'node:os'
+import { afterAll, beforeAll, describe, expect, it } from 'vitest'
+import { MongoMemoryReplSet } from 'mongodb-memory-server-core'
+import { PrismaClient } from '@prisma/client'
+import type { AuthUser } from '../src/lib/ssoAuth.js'
+import { SuperTabStore } from '../src/superTabs/store.js'
+import { migrateSwarm } from '../src/superTabs/migration/migrate.js'
+import { waitingIn } from '../src/superTabs/decisions.js'
+import { HostReads } from '../src/superTabs/read/host.js'
+import type { SuperTabState } from '../src/superTabs/model.js'
+import { writeLegacySwarm } from '../scripts/swarm-parts/legacy.js'
+import { NOW, owner as ownerActor, swarm } from '../src/superTabs/read/fixture.test-support.js'
+import { PROFILE_STAGES, profileEntry, queryShape, type ProfileEntry } from './bench/profile.js'
+
+// The hot reads of the roadmap's 2.7 (indexes) under Mongo's profiler: each must read only the documents it returns, never
+// the rest of its Swarm's rows, and never scan a collection. A fresh loopback replica set; never DATABASE_URL.
+let replica: MongoMemoryReplSet
+let db: PrismaClient
+let store: SuperTabStore
+let clock = NOW
+const owner: AuthUser = { sub: ownerActor.userId, email: ownerActor.email, autonomousEnv: 'prod', role: 'user' }
+const MIGRATED = NOW - 60_000
+const DAY = 86_400_000
+/** Rows of the same Swarm that none of the reads below return: a read that walks the Swarm's rows reads these too. */
+const OTHERS = 8
+
+beforeAll(async () => {
+ replica = await MongoMemoryReplSet.create({ binary: { version: '7.0.24', downloadDir: join(tmpdir(), 'harness-super-tab-mongodb-binaries') },
+ replSet: { count: 1, ip: '127.0.0.1', storageEngine: 'wiredTiger' } })
+ const uri = replica.getUri(`swarm_indexes_test_${randomUUID().replaceAll('-', '_')}`)
+ await promisify(execFile)(join(process.cwd(), 'node_modules/.bin/prisma'), ['db', 'push', '--skip-generate'], {
+ env: { ...process.env, DATABASE_URL: uri }, timeout: 60000,
+ })
+ db = new PrismaClient({ datasources: { db: { url: uri } } })
+ await db.user.create({ data: { id: owner.sub, externalId: `fixture-${owner.sub}`, email: owner.email, autonomousEnv: 'prod' } })
+ store = new SuperTabStore(db, () => clock)
+})
+afterAll(async () => {
+ await db?.$disconnect()
+ await replica?.stop()
+})
+
+/** A Swarm on two machines of the owner's: a Lead, and on the other a worker with running work, finished work, and OTHERS quiet conversations,
+ * closed deliveries of other messages to other agents, and open decisions in other conversations. */
+async function scene() {
+ const [machineId, leadMachine] = [`m-${randomUUID().slice(0, 8)}`, `l-${randomUUID().slice(0, 8)}`]
+ for (const id of [machineId, leadMachine]) {
+ await db.machine.create({ data: { machineId: id, userId: owner.sub, apiKey: 'fixture-only', managerId: 'fixture', autonomousEnv: 'prod', authMode: 'remote' } })
+ }
+ const s = swarm(MIGRATED)
+ s.contribute(machineId, 'swarm')
+ // The Lead is in every conversation, so it runs on another of the owner's machines: the host reads below are this one's.
+ const lead = s.add('lead-harness', 'lead', true, ownerActor, leadMachine, owner.sub)
+ const web = s.group('Web')
+ const worker = s.add('worker-harness', web, false, ownerActor, machineId, owner.sub)
+ s.run({ type: 'work.assign', conversationId: web, agentId: worker })
+ const done = s.group('Done work')
+ const quiet = Array.from({ length: OTHERS }, (_, i) => s.group(`Quiet ${i}`))
+ s.edit(state => {
+ state.conversations.find(c => c.id === done)!.assignment = { ...state.conversations.find(c => c.id === web)!.assignment!, id: 'old-work', status: 'done' }
+ })
+ const tabId = `tab-${randomUUID()}`
+ const state = { ...structuredClone(s.state), id: tabId } as SuperTabState
+ const message = `${tabId}-m1`
+ await writeLegacySwarm(db, { state, revision: 700, messages: [{ id: message, conversationId: done, sequence: 5, text: 'Done',
+ author: { kind: 'person', id: owner.sub, name: owner.email, membershipGeneration: tabId }, createdAt: new Date(MIGRATED - 2 * DAY) }] })
+ const report = await migrateSwarm(db, tabId, { now: () => MIGRATED })
+ if (!report.ok) throw new Error(report.error)
+ await db.swarmGroup.updateMany({ where: { tabId, key: { in: quiet } }, data: { dormant: true } })
+ const delivery = { tabId, conversationId: done, machineId, membershipGeneration: tabId, authorKind: 'person', authorId: owner.sub,
+ authorGeneration: tabId, status: 'confirmed' }
+ await db.superTabDelivery.createMany({ data: Array.from({ length: OTHERS }, (_, i) => ({ ...delivery, id: `${tabId}-d${i}`, agentId: `${tabId}-agent-${i}`, messageId: `${tabId}-other-${i}`, assignmentId: `${tabId}-old-work-${i}` })) })
+ await db.superTabDecision.createMany({ data: quiet.map((conversationId, i) => ({ id: `${tabId}-q${i}`, tabId, batchId: 'b', projectId: 'lead', conversationId,
+ personId: owner.sub, askerAgentId: lead, question: 'Which?', options: ['A', 'B'], recommended: 0, unblocks: 'Work', createdAt: new Date(MIGRATED) })) })
+ return { tabId, machineId, lead, worker, web, done, message }
+}
+
+/** Every operation `run` sends, as the profiler records it. */
+async function profiled(run: () => Promise<unknown>): Promise<ProfileEntry[]> {
+ await db.$runCommandRaw({ profile: 0 })
+ await db.$runCommandRaw({ drop: 'system.profile' }).catch(() => {})
+ await db.$runCommandRaw({ create: 'system.profile', capped: true, size: 64 * 1024 * 1024 })
+ await db.$runCommandRaw({ profile: 2, slowms: 0 })
+ try {
+ await run()
+ } finally {
+ await db.$runCommandRaw({ profile: 0 })
+ }
+ const reply = await db.$runCommandRaw({ aggregate: 'system.profile', pipeline: PROFILE_STAGES, cursor: { batchSize: 100000 } }) as unknown as { cursor: { firstBatch: ProfileEntry[] } }
+ return reply.cursor.firstBatch.map(profileEntry)
+}
+
+/** The profiled reads of one collection whose filter mentions every field of `fields` and none of `not`. */
+function reads(entries: ProfileEntry[], collection: string, fields: string[], not: string[] = []): ProfileEntry[] {
+ const mentions = (shape: string, field: string) => shape.includes(`"$${field}"`) || shape.includes(`"${field}":`)
+ return entries.filter(e => e.ns.endsWith(`.${collection}`)).filter(e => {
+ const filter = (e.command?.pipeline as Array<{ $match?: unknown }> | undefined)?.[0]?.$match ?? e.command?.filter ?? e.command?.q
+ if (filter === undefined) return false
+ const shape = queryShape(filter)
+ return fields.every(f => mentions(shape, f)) && !not.some(f => mentions(shape, f))
+ })
+}
+const plan = (rows: ProfileEntry[]) => rows.map(r => ({ plan: r.planSummary, docsExamined: r.docsExamined, keysExamined: r.keysExamined, returned: r.nreturned }))
+
+describe('hot reads read only what they return (roadmap 2.7)', () => {
+ let entries: ProfileEntry[]
+ let s: Awaited<ReturnType<typeof scene>>
+ beforeAll(async () => {
+ // Another Swarm with running work: a read not bounded by its Swarm reads that one's rows too.
+ await scene()
+ s = await scene()
+ const hosts = new HostReads(store, { now: () => clock })
+ clock = Date.now()
+ entries = await profiled(async () => {
+ const first = await hosts.changes(owner, s.machineId, { full: true })
+ clock = Date.now()
+ await hosts.changes(owner, s.machineId, { cursor: first.cursor })
+ await store.deliveries(owner, s.tabId, s.lead)
+ await db.$transaction(tx => waitingIn(tx, s.tabId, s.web))
+ // Last: ending the running work releases its run record and open deliveries.
+ const work = ((await db.swarmGroup.findFirst({ where: { tabId: s.tabId, key: s.web } }))!.data as { assignment: { id: string } }).assignment.id
+ await store.execute(owner, s.tabId, { operationId: randomUUID(),
+ command: { type: 'work.cancel', conversationId: s.web, assignmentId: work, reason: 'No longer needed.' } })
+ })
+ })
+
+ it('the host signature reads every conversation\'s digest and dormancy from the index alone', () => {
+ const rows = reads(entries, 'SwarmGroup', ['tabId', 'deleted'], ['key', 'dormant', 'changedAt', 'executionMachineId', 'agentIds'])
+ expect(rows).toHaveLength(1)
+ expect(plan(rows)).toEqual([expect.objectContaining({ docsExamined: 0 })])
+ })
+
+ it('the host\'s light load of running work reads only this Swarm\'s running conversations', () => {
+ const rows = reads(entries, 'SwarmGroup', ['tabId', 'executionMachineId'])
+ expect(rows).toHaveLength(1)
+ expect(rows[0]!.docsExamined, JSON.stringify(plan(rows))).toBeLessThanOrEqual(rows[0]!.nreturned!)
+ })
+
+ it('the host\'s wake check reads only machines whose settings changed since the cursor', () => {
+ const rows = reads(entries, 'SwarmMachine', ['tabId', 'hostChangedAt'])
+ expect(rows).toHaveLength(1)
+ expect(rows[0]!.docsExamined, JSON.stringify(plan(rows))).toBeLessThanOrEqual(rows[0]!.nreturned!)
+ })
+
+ it('the Lead\'s poll reads only the Swarm\'s open deliveries, not each agent\'s closed ones', () => {
+ const rows = reads(entries, 'SuperTabDelivery', ['tabId', 'status'], ['agentId', 'messageId', 'assignmentId', 'conversationId'])
+ expect(rows).toHaveLength(1)
+ // One key past the last of each open status at most: never one per agent.
+ expect(rows[0]!.keysExamined, JSON.stringify(plan(rows))).toBeLessThanOrEqual(rows[0]!.nreturned! + 3)
+ })
+
+ it('ending work reads only its own open deliveries, never the Swarm\'s others', () => {
+ const rows = reads(entries, 'SuperTabDelivery', ['tabId', 'assignmentId', 'status'])
+ expect(rows.length).toBeGreaterThan(0)
+ for (const row of rows) expect(row.docsExamined, JSON.stringify(plan(rows))).toBeLessThanOrEqual(row.nreturned!)
+ })
+
+ it('whether a decision waits in a conversation reads only that conversation\'s decisions', () => {
+ const rows = reads(entries, 'SuperTabDecision', ['tabId', 'conversationId'], ['personId'])
+ expect(rows).toHaveLength(1)
+ expect(rows[0]!.docsExamined, JSON.stringify(plan(rows))).toBeLessThanOrEqual(rows[0]!.nreturned!)
+ })
+
+ it('no read of these scans a whole collection', () => {
+ const scans = entries.filter(e => e.planSummary === 'COLLSCAN' && !e.ns.includes('.system.'))
+ expect(scans.map(e => `${e.ns.slice(e.ns.indexOf('.') + 1)} ${queryShape((e.command?.pipeline as Array<{ $match?: unknown }> | undefined)?.[0]?.$match ?? e.command?.filter ?? e.command?.q)}`)).toEqual([])
+ })
+})
diff --git a/backend/prisma/schema.prisma b/backend/prisma/schema.prisma
index ef79a0351..bcc3713d2 100644
--- a/backend/prisma/schema.prisma
+++ b/backend/prisma/schema.prisma
@@ -1122,6 +1122,8 @@ model SuperTabDecision {
@@index([tabId, personId, createdAt])
@@index([tabId, askerAgentId, createdAt])
@@index([tabId, projectId, createdAt])
+ /// Whether a decision waits in a conversation (decisions.ts waitingIn).
+ @@index([tabId, conversationId])
}
/// One uploaded file, which one message of the actor that uploaded it can send once.
@@ -1202,6 +1204,11 @@ model SuperTabDelivery {
@@index([machineId, status])
// A page of messages reads its deliveries by message (pageReads.ts).
@@index([tabId, messageId])
+ /// A Swarm's open deliveries, whoever they are for (the Lead's poll, store.ts fullDeliveries).
+ @@index([tabId, status])
+ /// An ended task's open deliveries, cancelled with it (store.ts releaseRuns and stopped): by [tabId, status] alone the
+ /// planner sometimes walked every delivery of the Swarm (180 read for 1 in the benchmark).
+ @@index([tabId, assignmentId])
}
// One independent harness owns at most one active assignment across all Super Tabs.
@@ -1212,6 +1219,9 @@ model SuperTabRun {
machineId String
harnessId String
createdAt DateTime @default(now())
+
+ /// A Swarm's run records (store.ts staleRuns).
+ @@index([tabId])
}
// One routing policy document per account and environment (superTabs/routingPolicy.ts).
@@ -1469,7 +1479,10 @@ model SwarmGroup {
@@index([tabId, projectId, dormant, reportSeq])
@@index([dormant, lastActivityAt])
@@index([workFinished, archived, lastActivityAt])
- @@index([executionMachineId])
+ /// A Swarm's running work (parts/light.ts, the host's light load).
+ @@index([tabId, executionMachineId])
+ /// The host signature (read/host.ts inputs) reads every conversation's digest and dormancy from this index alone.
+ @@index([tabId, deleted, key, hostDigest, dormant])
}
model SwarmAgent {
@@ -1490,6 +1503,8 @@ model SwarmAgent {
@@index([tabId, changedAt])
@@index([machineId, deleted])
@@index([tabId, contributorId])
+ /// One agent's registrations in every Swarm (confidential.ts, sessionScope.ts): host rows read them on every changed tab.
+ @@index([key, deleted])
}
/// One machine the Swarm references, contributed or not: its contribution and observation, and its slot ledger.
@@ -1516,6 +1531,8 @@ model SwarmMachine {
@@index([tabId, changedAt])
@@index([tabId, readAt])
@@index([machineId, deleted])
+ /// Machines whose settings changed since a host's cursor (read/host.ts woken).
+ @@index([tabId, hostChangedAt])
}
model SwarmStart {
diff --git a/backend/src/lib/databaseUrl.test.ts b/backend/src/lib/databaseUrl.test.ts
new file mode 100644
index 000000000..1b712adf6
--- /dev/null
+++ b/backend/src/lib/databaseUrl.test.ts
@@ -0,0 +1,26 @@
+import { describe, expect, it } from 'vitest'
+import { withPoolDefaults } from './databaseUrl.js'
+
+const defaults = { maxPoolSize: 20, serverSelectionTimeoutMS: 10000 }
+
+describe('withPoolDefaults', () => {
+ it('adds the pool size and server selection timeout to a connection string with other options', () => {
+ expect(withPoolDefaults('mongodb://localhost:27017/harness?replicaSet=rs0', defaults))
+ .toBe('mongodb://localhost:27017/harness?replicaSet=rs0&maxPoolSize=20&serverSelectionTimeoutMS=10000')
+ })
+
+ it('adds them to one with no options, after the database', () => {
+ expect(withPoolDefaults('mongodb+srv://user:p%[email protected]/harness', defaults))
+ .toBe('mongodb+srv://user:p%[email protected]/harness?maxPoolSize=20&serverSelectionTimeoutMS=10000')
+ })
+
+ it('adds the path the options need to one with no database', () => {
+ expect(withPoolDefaults('mongodb://a.example:27017,b.example:27017', defaults))
+ .toBe('mongodb://a.example:27017,b.example:27017/?maxPoolSize=20&serverSelectionTimeoutMS=10000')
+ })
+
+ it('keeps what the connection string already sets, in any letter case', () => {
+ expect(withPoolDefaults('mongodb://h/db?maxpoolsize=50&ServerSelectionTimeoutMS=3000', defaults)).toBe('mongodb://h/db?maxpoolsize=50&ServerSelectionTimeoutMS=3000')
+ expect(withPoolDefaults('mongodb://h/db?maxPoolSize=50', defaults)).toBe('mongodb://h/db?maxPoolSize=50&serverSelectionTimeoutMS=10000')
+ })
+})
diff --git a/backend/src/lib/databaseUrl.ts b/backend/src/lib/databaseUrl.ts
new file mode 100644
index 000000000..3dc928f40
--- /dev/null
+++ b/backend/src/lib/databaseUrl.ts
@@ -0,0 +1,23 @@
+/**
+ * The pool options every backend process connects with, unless its connection string sets its own (roadmap 2.8). Without
+ * them Prisma's MongoDB driver keeps its defaults: a pool of 10 per process, and 30 s waiting for a primary, so a request
+ * during a failover hangs for half a minute before it fails.
+ */
+export interface PoolDefaults { maxPoolSize: number; serverSelectionTimeoutMS: number }
+
+/** The Swarm benchmark's one process reached 10 server connections at its peak, the default pool's size (roadmap 2.8). A
+ * request that finds no primary fails after 10 s instead of 30, so its client retries rather than requests piling up. */
+export const POOL_DEFAULTS: PoolDefaults = { maxPoolSize: 20, serverSelectionTimeoutMS: 10_000 }
+
+/** `url` with each of `defaults` it does not already set (MongoDB option names are case-insensitive). Never parsed as a
+ * URL: a seed list (`h1:27017,h2:27017`) is not one. */
+export function withPoolDefaults(url: string, defaults: PoolDefaults): string {
+ const query = url.indexOf('?')
+ const present = new Set((query < 0 ? '' : url.slice(query + 1)).split('&').map(pair => pair.split('=')[0]!.toLowerCase()))
+ const missing = Object.entries(defaults).filter(([name]) => !present.has(name.toLowerCase())).map(([name, value]) => `${name}=${value}`)
+ if (!missing.length) return url
+ if (query >= 0) return `${url}&${missing.join('&')}`
+ // Options follow the database path; a string with no path needs its slash first.
+ const hosts = url.indexOf('://') + 3
+ return `${url}${url.indexOf('/', hosts) < 0 ? '/' : ''}?${missing.join('&')}`
+}
diff --git a/backend/src/lib/prisma.ts b/backend/src/lib/prisma.ts
index 9ce3110b9..875a290e1 100644
--- a/backend/src/lib/prisma.ts
+++ b/backend/src/lib/prisma.ts
@@ -1,11 +1,12 @@
import { PrismaClient, type Prisma } from '@prisma/client'
import { env } from '../config/env.js'
+import { POOL_DEFAULTS, withPoolDefaults } from './databaseUrl.js'
// The Prisma CLI (db push) reads env("DATABASE_URL"); keep it in sync for any manual run.
process.env.DATABASE_URL ??= env.DATABASE_URL
export const prisma = new PrismaClient({
- datasources: { db: { url: env.DATABASE_URL } },
+ datasources: { db: { url: withPoolDefaults(env.DATABASE_URL, POOL_DEFAULTS) } },
})
/** "Not soft-deleted" filter for Machine findMany where-clauses. Mongo-Prisma gotcha: legacy
diff --git a/backend/src/lib/wsServer.ts b/backend/src/lib/wsServer.ts
index 58a006fe4..6f83beb2b 100644
--- a/backend/src/lib/wsServer.ts
+++ b/backend/src/lib/wsServer.ts
@@ -24,8 +24,13 @@ export const WS_LIMITS = {
web: 1024 * 1024,
/** PCM chunks are ~640 B (20 ms @ 16 kHz); device JSON RPCs are tiny. */
device: 256 * 1024,
- /** App-proxy frames ride these sockets: 8 MiB of body → ~11.2 MiB once base64'd into JSON. */
+ /** App-proxy frames ride the manager socket: an 8 MiB WebSocket message → ~11.2 MiB once base64'd into JSON. Its `up`
+ * frames come from the machine-manager, outside this repo, in the adapter's vocabulary. */
manager: 16 * 1024 * 1024,
+ /** No app-proxy frames: the CLI's events and RPC replies (cli/src/gateway). Measured for roadmap 2.8, these are not
+ * bounded below this: a Claude or Codex history page is up to ~5.4 MiB sealed, and history of the engines the core
+ * reads itself and images inline in a user message have no byte cap at all. A frame past this closes the link (1009)
+ * and drops every remote client of that machine, so it stays until the CLI caps those replies. */
adapter: 16 * 1024 * 1024,
/** Sealed shared-tab frames between daemons are capped at 128 KiB by the relay; leave room for the envelope. */
agentMessages: 256 * 1024,
diff --git a/backend/src/routes/superTabs.test.ts b/backend/src/routes/superTabs.test.ts
index 5407aff82..85dd973f2 100644
--- a/backend/src/routes/superTabs.test.ts
+++ b/backend/src/routes/superTabs.test.ts
@@ -3,7 +3,7 @@ import Fastify, { type FastifyInstance } from 'fastify'
import { Prisma } from '@prisma/client'
import { errorHandler } from '../middlewares/errorHandler.js'
import { SuperTabError } from '../superTabs/model.js'
-import { superTabRoutes } from './superTabs.js'
+import { HOT_BODY_LIMIT, superTabRoutes } from './superTabs.js'
describe('Super Tab route failures', () => {
let app: FastifyInstance
@@ -50,3 +50,51 @@ describe('Super Tab route failures', () => {
expect(res.json().error.code).toBe('SWARM_BUSY')
})
})
+
+describe('Super Tab body limits (roadmap 2.8)', () => {
+ let app: FastifyInstance
+ let reached: string[]
+ beforeEach(async () => {
+ reached = []
+ app = Fastify()
+ app.setErrorHandler(errorHandler)
+ app.addHook('onRequest', async request => { request.user = { sub: 'u1', email: '[email protected]', autonomousEnv: 'prod', role: 'user' } as never })
+ const handled = (name: string) => async () => { reached.push(name); return {} }
+ const store = { native: { append: handled('native'), open: handled('open') }, delivery: handled('delivery'), stopped: handled('stopped') }
+ await app.register(superTabRoutes, { store: store as never, github: null, reads: {} as never, hosts: { changes: handled('changes') } as never })
+ await app.ready()
+ })
+ afterEach(async () => { await app.close() })
+
+ const id = (n: number, size = 160) => `${String(n).padStart(6, '0')}${'a'.repeat(size - 6)}`
+ /** Free text at its longest in characters, each the widest a person types: three UTF-8 bytes ('ệ'). */
+ const text = (length: number) => 'ệ'.repeat(length)
+ /** The largest body each hot route accepts, from its schema's limits. */
+ const largest = {
+ changes: { url: '/api/super-tabs/hosts/m1/changes', name: 'changes',
+ body: { cursor: 'c'.repeat(512), full: false, held: Object.fromEntries(Array.from({ length: 500 }, (_, n) => [id(n), 'e'.repeat(64)])) } },
+ native: { url: '/api/super-tabs/tab-1/native/ctx-1', name: 'native',
+ body: { turnId: id(3), kind: 'answer', previousEventId: id(4), text: text(16_000), truncated: false, interrupted: false } },
+ delivery: { url: '/api/super-tabs/tab-1/agents/agent-1/deliveries/delivery-1', name: 'delivery',
+ body: { type: 'ack', claimId: id(4), receipt: text(2000), uncertain: false } },
+ stopped: { url: '/api/super-tabs/tab-1/stopped', name: 'stopped', body: { assignmentId: id(2), receipt: text(2000) } },
+ }
+ const post = (url: string, payload: string) => app.inject({ method: 'POST', url, payload, headers: { 'content-type': 'application/json' } })
+
+ for (const [route, { url, name, body }] of Object.entries(largest)) {
+ it(`takes the largest valid ${route} body and refuses one past the limit`, async () => {
+ const json = JSON.stringify(body)
+ expect((await post(url, json)).statusCode).toBe(200)
+ expect(reached).toEqual([name])
+ const over = await post(url, `${json}${' '.repeat(HOT_BODY_LIMIT + 1 - Buffer.byteLength(json))}`)
+ expect(over.statusCode).toBe(413)
+ expect(reached).toEqual([name])
+ })
+ }
+
+ it('keeps the limit within the largest valid hot body with room for growth: under twice it', () => {
+ const sizes = Object.values(largest).map(r => Buffer.byteLength(JSON.stringify(r.body)))
+ expect(Math.max(...sizes)).toBeLessThan(HOT_BODY_LIMIT)
+ expect(HOT_BODY_LIMIT).toBeLessThan(2 * Math.max(...sizes))
+ })
+})
diff --git a/backend/src/routes/superTabs.ts b/backend/src/routes/superTabs.ts
index 7d835e3af..89e3221c8 100644
--- a/backend/src/routes/superTabs.ts
+++ b/backend/src/routes/superTabs.ts
@@ -121,6 +121,14 @@ function busyOnDuplicate(error: unknown): never {
* `github` is the Harness GitHub App (github/tokens.ts): read from the environment unless given, null when off. */
/** Full-state reads of clients from before Swarms were stored by parts (design 4.11): every old client reads one of them
* before or right after any action, so it meets this at once. */
+/**
+ * The body limit of the routes hosts and agents call every second or every turn (roadmap 2.8), in place of Fastify's 1 MiB:
+ * a body is parsed whole on the event loop before its schema is checked. Each route's largest valid body, from its schema's
+ * limits with free text in three-byte characters (routes/superTabs.test.ts): host changes with 500 held Swarms 116 KB, a
+ * recorded turn 48 KB, a delivery ack or stop receipt 6 KB. Commands and machine readings keep the default: a task at its
+ * schema's limits reaches 700 KB.
+ */
+export const HOT_BODY_LIMIT = 128 * 1024
const updateRequired = (_req: unknown, reply: FastifyReply) => sendError(reply, UPDATE_REQUIRED.message, UPDATE_REQUIRED.code, UPDATE_REQUIRED.status)
/** The store this server runs on. Loaded only here: importing the bus opens its Redis connections. */
@@ -174,7 +182,7 @@ export async function superTabRoutes(app: FastifyInstance, options: { store?: Su
(req, reply) => respond(reply, () => hosts.changes(req.user!, req.params.machineId, { cursor: req.query.cursor, full: req.query.full === '1' })))
// The same read for a host that holds etags (`held`, design 4.10): too many for a URL, so in a body.
app.post<{ Params: z.infer<typeof hostParams>; Body: z.infer<typeof hostChangesBody> }>('/api/super-tabs/hosts/:machineId/changes',
- { preHandler: [validateParams(hostParams), validateBody(hostChangesBody)] },
+ { bodyLimit: HOT_BODY_LIMIT, preHandler: [validateParams(hostParams), validateBody(hostChangesBody)] },
(req, reply) => respond(reply, () => hosts.changes(req.user!, req.params.machineId, req.body)))
// A read-only token for a queued start's project repository, for its host to clone and fetch the folder before the agent
// exists, so that the machine owner's own git login is never used for another person's project (docs/swarm-github-app.md).
@@ -247,13 +255,13 @@ export async function superTabRoutes(app: FastifyInstance, options: { store?: Su
{ preHandler: [validateParams(machineParams), validateBody(machineObservationSchema)] },
(req, reply) => respond(reply, () => store.observeMachine(req.user!, req.params.tabId, req.params.machineId, req.body)))
app.post<{ Params: z.infer<typeof tabParams>; Body: z.infer<typeof openNativeSchema> }>('/api/super-tabs/:tabId/native',
- { preHandler: [validateParams(tabParams), validateBody(openNativeSchema)] },
+ { bodyLimit: HOT_BODY_LIMIT, preHandler: [validateParams(tabParams), validateBody(openNativeSchema)] },
(req, reply) => respond(reply, () => store.native.open(req.user!, req.params.tabId, req.body)))
app.get<{ Params: z.infer<typeof nativeParams> }>('/api/super-tabs/:tabId/native/:contextId',
{ preHandler: validateParams(nativeParams) },
(req, reply) => respond(reply, () => store.native.read(req.user!, req.params.tabId, req.params.contextId)))
app.post<{ Params: z.infer<typeof nativeParams>; Body: z.infer<typeof nativeEventSchema> }>('/api/super-tabs/:tabId/native/:contextId',
- { preHandler: [validateParams(nativeParams), validateBody(nativeEventSchema)] },
+ { bodyLimit: HOT_BODY_LIMIT, preHandler: [validateParams(nativeParams), validateBody(nativeEventSchema)] },
(req, reply) => respond(reply, () => store.native.append(req.user!, req.params.tabId, req.params.contextId, req.body).catch(busyOnDuplicate)))
app.get('/api/super-tabs/routing-policy', (req, reply) => respond(reply, () => store.accountPolicy(req.user!)))
app.post<{ Body: z.infer<typeof savePolicy> }>('/api/super-tabs/routing-policy', { preHandler: validateBody(savePolicy) },
@@ -441,12 +449,12 @@ export async function superTabRoutes(app: FastifyInstance, options: { store?: Su
{ preHandler: validateParams(deliveryParams) },
(req, reply) => respond(reply, () => store.deliveries(req.user!, req.params.tabId, req.params.agentId)))
app.post<{ Params: z.infer<typeof deliveryActionParams>; Body: z.infer<typeof deliveryAction> }>('/api/super-tabs/:tabId/agents/:agentId/deliveries/:deliveryId',
- { preHandler: [validateParams(deliveryActionParams), validateBody(deliveryAction)] },
+ { bodyLimit: HOT_BODY_LIMIT, preHandler: [validateParams(deliveryActionParams), validateBody(deliveryAction)] },
(req, reply) => respond(reply, () => store.delivery(req.user!, req.params.tabId, req.params.agentId, req.params.deliveryId, req.body)))
app.get<{ Params: z.infer<typeof deliveryActionParams> }>('/api/super-tabs/:tabId/agents/:agentId/deliveries/:deliveryId',
{ preHandler: validateParams(deliveryActionParams) },
(req, reply) => respond(reply, () => store.deliveryReceipt(req.user!, req.params.tabId, req.params.agentId, req.params.deliveryId)))
app.post<{ Params: z.infer<typeof tabParams>; Body: z.infer<typeof stop> }>('/api/super-tabs/:tabId/stopped',
- { preHandler: [validateParams(tabParams), validateBody(stop)] },
+ { bodyLimit: HOT_BODY_LIMIT, preHandler: [validateParams(tabParams), validateBody(stop)] },
(req, reply) => respond(reply, () => store.stopped(req.user!, req.params.tabId, req.body.assignmentId, req.body.receipt)))
}
diff --git a/backend/src/superTabs/read/host.ts b/backend/src/superTabs/read/host.ts
index b7c6dc573..ee931db51 100644
--- a/backend/src/superTabs/read/host.ts
+++ b/backend/src/superTabs/read/host.ts
@@ -98,9 +98,10 @@ export class HostReads {
tx.swarmMachine.findMany({ where: { tabId: { in: ids }, hostChangedAt: { gt: after } }, select: { tabId: true, key: true, data: true } }),
])
const woken = new Set([...ids.filter(id => changed.get(id)?.some(p => plainlyRelevant(p, machineId))), ...groups.map(g => g.tabId)])
- // This machine's own settings, a team account's pool on this Mac, or the Mac this team account follows.
- const follows = new Map((await tx.swarmMachine.findMany({ where: { tabId: { in: [...new Set(machines.map(m => m.tabId))] }, key: machineId },
- select: { tabId: true, data: true } })).map(m => [m.tabId, (m.data as MachineData | null)?.contribution?.hostMachineId]))
+ // This machine's own settings, a team account's pool on this Mac, or the Mac this team account follows. Not read when no
+ // machine changed, as on most polls: Prisma sends an empty list as a filter no index serves, a scan of every Swarm's machines.
+ const follows = new Map(machines.length ? (await tx.swarmMachine.findMany({ where: { tabId: { in: [...new Set(machines.map(m => m.tabId))] }, key: machineId },
+ select: { tabId: true, data: true } })).map(m => [m.tabId, (m.data as MachineData | null)?.contribution?.hostMachineId]) : [])
for (const m of machines) {
if (m.key === machineId || (m.data as MachineData | null)?.contribution?.hostMachineId === machineId || follows.get(m.tabId) === m.key) woken.add(m.tabId)
}
diff --git a/backend/vitest.super-tabs.config.ts b/backend/vitest.super-tabs.config.ts
index 828110263..f45eb61ed 100644
--- a/backend/vitest.super-tabs.config.ts
+++ b/backend/vitest.super-tabs.config.ts
@@ -3,5 +3,5 @@ import { defineConfig } from 'vitest/config'
export default defineConfig({ test: { include: ['integration/superTabs.test.ts', 'integration/superTabSharing.test.ts', 'integration/superTabGithub.test.ts', 'integration/superTabTeam.test.ts', 'integration/superTabWorkspaces.test.ts',
'integration/superTabRead.test.ts', 'integration/superTabLeaderboard.test.ts', 'integration/superTabMigration.test.ts', 'integration/superTabLiveness.test.ts', 'integration/superTabTidy.test.ts',
'integration/superTabProfile.test.ts', 'integration/superTabLegacyDrop.test.ts', 'integration/shareRaces.test.ts',
- 'integration/superTabIndexes.test.ts', 'integration/superTabCache.test.ts', 'integration/superTabObserver.test.ts'], hookTimeout: 120000, testTimeout: 30000, setupFiles: ['integration/cacheOn.test-support.ts'],
+ 'integration/superTabIndexes.test.ts', 'integration/superTabHotReads.test.ts', 'integration/superTabCache.test.ts', 'integration/superTabObserver.test.ts'], hookTimeout: 120000, testTimeout: 30000, setupFiles: ['integration/cacheOn.test-support.ts'],
fileParallelism: false, env: { NODE_ENV: 'test', HARNESS_CREDENTIAL_ENCRYPTION_KEY: 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=' } } })