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.

Merge batch 11-06 j0300: #3229 #3212 #3241 #3238 (#3247)

0xken15981c7a4a3e12
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 723b08416..a3aef33ed 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -175,7 +175,10 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-22.04]
- shard: [1, 2, 3, 4]
+ # Six shards split by measured file times (scripts/desktop-test-durations.json): each needs about 6 of its 10
+ # minutes on a slow runner, so the suite can grow a fifth before one runs short (task #3458;
+ # scripts/tests/test_desktop_tests.py checks the split keeps a quarter of the limit free).
+ shard: [1, 2, 3, 4, 5, 6]
steps:
- uses: actions/checkout@v7
with:
@@ -217,10 +220,13 @@ jobs:
- uses: ./.github/actions/desktop-flutter
with:
version: ${{ steps.toolchain.outputs.version }}
+ - name: Every Desktop shard keeps a quarter of its time limit (task #3458)
+ if: matrix.shard == 1
+ run: python3 -m unittest discover -s scripts/tests -p test_desktop_tests.py -k test_ci_split_leaves_every_shard -v
- name: Run bounded Desktop VM shard
env:
DESKTOP_SHARD: ${{ matrix.shard }}
- run: python3 scripts/test-desktop.py --shard "$DESKTOP_SHARD/4" --workers 4 --timeout 600
+ run: python3 scripts/test-desktop.py --shard "$DESKTOP_SHARD/6" --workers 4 --timeout 600
- name: Preserve complete test logs and receipts
if: ${{ !cancelled() }}
uses: actions/upload-artifact@v4
diff --git a/backend/integration/superTabNotAvailable.test.ts b/backend/integration/superTabNotAvailable.test.ts
index cff2b5622..735dedd95 100644
--- a/backend/integration/superTabNotAvailable.test.ts
+++ b/backend/integration/superTabNotAvailable.test.ts
@@ -96,6 +96,7 @@ function calls(tab: string, chat: string, project: string) {
json('GET', 'GET /api/super-tabs/:tabId/mentions', `${t}/mentions?state=waiting`),
json('GET', 'GET /api/super-tabs/:tabId/decisions', `${t}/decisions?state=open`),
json('GET', 'GET /api/super-tabs/:tabId/leaderboard', `${t}/leaderboard?projectId=${project}`),
+ json('GET', 'GET /api/super-tabs/:tabId/scorecard', `${t}/scorecard?projectId=${project}`),
json('GET', 'GET /api/super-tabs/:tabId/machine-readings', `${t}/machine-readings`),
json('GET', 'GET /api/super-tabs/:tabId/proposals', `${t}/proposals?projectId=${project}`),
json('GET', 'GET /api/super-tabs/:tabId/sharing', `${t}/sharing`),
@@ -210,7 +211,8 @@ describe('a Swarm the caller cannot reach, on every person route', () => {
|| JSON.stringify(c.payload ?? '').includes(f.privateChat) || String(c.payload ?? '').includes(f.privateChat))
expect(named.map(c => c.route)).toEqual(expect.arrayContaining(['POST /api/super-tabs/:tabId/commands', 'GET /api/super-tabs/:tabId/groups/:conversationId',
'GET /api/super-tabs/:tabId/conversations/:conversationId/messages', 'POST /api/super-tabs/:tabId/attachments',
- 'GET /api/super-tabs/:tabId/projects/:projectId/github', 'GET /api/super-tabs/:tabId/leaderboard']))
+ 'GET /api/super-tabs/:tabId/projects/:projectId/github', 'GET /api/super-tabs/:tabId/leaderboard',
+ 'GET /api/super-tabs/:tabId/scorecard']))
// Checked before the chat: a terminal opens for one agent (made up here), and approving needs the server's GitHub App.
const before = ['POST /api/super-tabs/:tabId/native', 'POST /api/super-tabs/:tabId/projects/:projectId/github/approve',
'POST /api/super-tabs/:tabId/projects/:projectId/github/repository']
diff --git a/backend/integration/superTabRead.test.ts b/backend/integration/superTabRead.test.ts
index 7d59ab8e8..24991777d 100644
--- a/backend/integration/superTabRead.test.ts
+++ b/backend/integration/superTabRead.test.ts
@@ -1420,16 +1420,36 @@ describe('what a read costs the database (group 3b)', () => {
const out = ((await db.$runCommandRaw({ serverStatus: 1 })) as { network: { bytesOut: unknown } }).network.bytesOut
return typeof out === 'number' ? out : Number((out as { $numberLong?: string; $numberInt?: string }).$numberLong ?? (out as { $numberInt?: string }).$numberInt)
}
- /** Waits until nothing else is reading or writing: what MongoDB sends in a tenth of a second (the first read's own answer
- * and whatever else lands meanwhile) is the same four times running. Earlier tests' background work (a command's notices
- * and wakes, a read a timed-out test left going) would otherwise land in the next test's count. */
+ /** The model calls this client has begun and not finished, and when it last began or finished one. A read that load holds
+ * back sends nothing yet, so the byte count alone cannot tell it from quiet; the call itself can. Raw commands (the byte
+ * count's own) carry no model and are left out. */
+ let running = 0, lastBusy = 0
+ /** A model call held back before it goes out, as CPU load holds one back: a test stalls a read with it and lets it go. */
+ let hold: Promise<void> | null = null
+ beforeAll(() => {
+ db.$use(async (params, next) => {
+ if (!params.model) return next(params)
+ running++
+ lastBusy = Date.now()
+ try {
+ if (hold) await hold
+ return await next(params)
+ } finally { running--; lastBusy = Date.now() }
+ })
+ })
+ /** Waits until nothing else is reading or writing: for four tenths of a second no model call began, ended or was running,
+ * and what MongoDB sends in a tenth of a second (the first read's own answer and whatever else lands meanwhile) is the
+ * same four times running. Earlier tests' background work (a command's notices and wakes, a read a timed-out test left
+ * going, one that load has not let out yet) would otherwise land in the next test's count. */
const quiet = async () => {
let last = -1, steady = 0
for (let tries = 0; tries < 300 && steady < 4; tries++) {
+ const windowStart = Date.now()
const before = await sent()
await new Promise(done => setTimeout(done, 100))
const sentSince = await sent() - before
- steady = sentSince === last ? steady + 1 : 0
+ const still = running === 0 && lastBusy < windowStart
+ steady = still && sentSince === last ? steady + 1 : 0
last = sentSince
}
if (steady < 4) throw new Error('The database never went quiet: something keeps reading or writing.')
@@ -1454,11 +1474,30 @@ describe('what a read costs the database (group 3b)', () => {
it('counts none of the reads an earlier test left running', async () => {
const s = await scene(f => bulky(f))
// What a test that ends before its own background work does: reads of the whole Swarm, still going into the next one.
- const leftover = (async () => { for (let i = 0; i < 40; i++) await db.$transaction(tx => parts.loadAll(tx, s.tabId)) })()
+ let underway = () => {}
+ const begun = new Promise<void>(done => { underway = done })
+ const leftover = (async () => { for (let i = 0; i < 40; i++) { await db.$transaction(tx => parts.loadAll(tx, s.tabId)); underway() } })()
+ // A transaction that load has not started yet is no model call to see: wait until the reads are going, then for them to end.
+ await begun
expect(await databaseBytes(() => new Promise(done => setTimeout(done, 200)))).toBeLessThan(1_000)
await leftover
})
+ it('counts none of the reads an earlier test left that had not reached the database yet', async () => {
+ const s = await scene(f => bulky(f))
+ // A read held back by load: it sends nothing until it is let go, so the database looks quiet while it is still coming.
+ let release = () => {}
+ hold = new Promise<void>(done => { release = done })
+ const letGo = () => { hold = null; release() }
+ const timer = setTimeout(letGo, 1_500)
+ const leftover = (async () => { for (let i = 0; i < 40; i++) await db.$transaction(tx => parts.loadAll(tx, s.tabId)) })()
+ try {
+ // The test's own call lets the held read go, the moment counting starts: a count that began while it was still held counts it.
+ expect(await databaseBytes(() => { letGo(); return new Promise(done => setTimeout(done, 200)) })).toBeLessThan(1_000)
+ } finally { clearTimeout(timer); letGo() }
+ await leftover
+ })
+
it('a Desktop change read with nothing changed loads a few tens of KB, not the Swarm', async () => {
const s = await scene(f => bulky(f))
const whole = await swarmBytes(s.tabId)
diff --git a/backend/integration/superTabScorecard.test.ts b/backend/integration/superTabScorecard.test.ts
new file mode 100644
index 000000000..b9a4bab2b
--- /dev/null
+++ b/backend/integration/superTabScorecard.test.ts
@@ -0,0 +1,140 @@
+import { randomUUID } from 'node:crypto'
+import { afterAll, beforeAll, describe, expect, it } from 'vitest'
+import type { MongoMemoryReplSet } from 'mongodb-memory-server-core'
+import type { PrismaClient } from '@prisma/client'
+import Fastify, { type FastifyInstance } from 'fastify'
+import type { AuthUser } from '../src/lib/ssoAuth.js'
+import { SuperTabStore } from '../src/superTabs/store.js'
+import type { Command } from '../src/superTabs/commands.js'
+import { superTabRoutes } from '../src/routes/superTabs.js'
+import { errorHandler } from '../src/middlewares/errorHandler.js'
+import { SwarmReads } from '../src/superTabs/read/service.js'
+import { listNamedIndexes } from '../src/superTabs/parts/indexed.js'
+import { scorecardCache, scorecardPass } from '../src/superTabs/scorecard/job.js'
+import { scorecardId } from '../src/superTabs/scorecard/rows.js'
+import type { ScorecardFigures } from '../src/superTabs/scorecard/figures.js'
+import { planDatabase, profiled } from './plans.test-support.js'
+
+let replica: MongoMemoryReplSet
+let db: PrismaClient
+let app: FastifyInstance
+const owner: AuthUser = { sub: '320000000000000000000001', email: '[email protected]', autonomousEnv: 'prod', role: 'user' }
+const outsider: AuthUser = { sub: '320000000000000000000002', email: '[email protected]', autonomousEnv: 'prod', role: 'user' }
+const config = { engine: 'claude', model: 'opus', effort: 'high', contextWindow: 200000, engineVersion: 'fixture' }
+const MIN = 60_000, HOUR = 60 * MIN
+/** 2026-11-05 in Vietnam, the project's default zone: 11-04 17:00 UTC to 11-05 17:00 UTC. */
+const START = Date.UTC(2026, 10, 4, 17)
+const END = START + 24 * HOUR
+const at = (hours: number) => START + hours * HOUR
+let now = at(0)
+
+beforeAll(async () => {
+ ({ replica, db } = await planDatabase('swarm_scorecard_test'))
+ for (const user of [owner, outsider]) await db.user.create({ data: { id: user.sub, externalId: `fixture-${user.sub}`, email: user.email, autonomousEnv: 'prod' } })
+ await db.machine.create({ data: { machineId: 'host', userId: owner.sub, apiKey: 'fixture-host', managerId: 'fixture', autonomousEnv: 'prod', authMode: 'remote' } })
+ const store = new SuperTabStore(db, () => now, null)
+ app = Fastify()
+ app.setErrorHandler(errorHandler)
+ app.addHook('onRequest', async (request, reply) => {
+ const found = [owner, outsider].find(u => `Bearer ${u.sub}` === request.headers.authorization)
+ if (!found) return reply.code(401).send({ success: false })
+ request.user = found
+ })
+ await app.register(superTabRoutes, { store, github: null, reads: new SwarmReads(store, { now: () => now }) })
+ await app.ready()
+})
+afterAll(async () => {
+ await app?.close()
+ await db?.$disconnect()
+ await replica?.stop()
+})
+
+/** A Swarm's day: the owner asks twice and corrects the PM once; one task ships, one waits all evening. The server refuses an
+ * acknowledgement-only post now (acknowledgements.ts), so the PM's post here says something; figures.test.ts counts acks. */
+async function day() {
+ const store = new SuperTabStore(db, () => now, null)
+ const tab = (await store.create(owner, randomUUID(), 'Scorecard', 'public')).id
+ // Rows are stamped by the database's clock: each command's rows get the fixture's time instead.
+ const execute = async (command: Command, asAgentId?: string) => {
+ const began = new Date()
+ const { result } = await store.execute(owner, tab, { operationId: randomUUID(), command }, asAgentId)
+ await db.superTabMessage.updateMany({ where: { tabId: tab, createdAt: { gte: began } }, data: { createdAt: new Date(now) } })
+ return result
+ }
+ await execute({ type: 'machine.contribute', machineId: 'host', name: 'Mac', cwd: '/repo', engines: ['claude'], maxAgents: 4 } as Command)
+ const pm = String((await execute({ type: 'agent.add', conversationId: 'lead', machineId: 'host', harnessId: randomUUID(), name: 'River', configuration: config,
+ lead: true } as Command)).agentId)
+ const post = async (text: string, extra: Record<string, unknown> = {}, asAgentId?: string) =>
+ String((await execute({ type: 'message.send', conversationId: 'lead', text, replyTo: null, recipientIds: [], ...extra } as Command, asAgentId)).messageId)
+
+ now = at(1)
+ const ask = await post('Please add a scorecard.')
+ now = at(1) + 10 * MIN
+ const answer = await post('I will open a task for it now.', { answers: ask }, pm)
+ now = at(2)
+ const task = String((await execute({ type: 'conversation.create', title: 'Scorecard', brief: 'Build it.', checks: [] } as Command)).conversationId)
+ const worker = String((await execute({ type: 'agent.add', conversationId: task, machineId: 'host', harnessId: randomUUID(), name: 'Tally', configuration: config,
+ lead: false } as Command)).agentId)
+ now = at(3)
+ const assignmentId = String((await execute({ type: 'work.assign', conversationId: task, agentId: worker } as Command)).assignmentId)
+ await db.superTabRouteDecision.create({ data: { id: randomUUID(), tabId: tab, conversationId: task, createdAt: new Date(at(3)), record: {} } })
+ now = at(4)
+ await execute({ type: 'message.send', conversationId: task, text: 'Plan: figures first, then the nightly job.', replyTo: null, recipientIds: [] } as Command, pm)
+ now = at(5)
+ await execute({ type: 'work.status', conversationId: task, assignmentId, status: 'done', result: 'Shipped.' } as Command, worker)
+ now = at(6)
+ await execute({ type: 'conversation.create', title: 'Waits all evening', brief: 'Nobody takes it.', checks: [] } as Command)
+ now = at(7)
+ await post('Can you also check the phone?')
+ now = at(8)
+ await post('No, I asked for it daily.', { replyTo: answer })
+ return { store, tab }
+}
+
+describe('the nightly scorecard', () => {
+ it('stores each project\'s day once, from the Swarm\'s records, on indexed reads', async () => {
+ const f = await day()
+ await listNamedIndexes(db)
+ now = END + 31 * MIN
+ const cache = scorecardCache()
+ let pass!: Awaited<ReturnType<typeof scorecardPass>>
+ const entries = await profiled(db, async () => { pass = await scorecardPass(db, db, now, undefined, cache) })
+ // 11-05 and 11-04, which had nothing: both stored; a later pass stores neither again.
+ expect(pass).toMatchObject({ stored: 2, failed: 0, capped: 0 })
+ expect(await scorecardPass(db, db, now + 15 * MIN, undefined, cache)).toMatchObject({ stored: 0, failed: 0 })
+ expect(await scorecardPass(db, db, now + 15 * MIN, undefined, scorecardCache())).toMatchObject({ stored: 0, already: 2 })
+
+ const row = await db.superTabScorecard.findUnique({ where: { id: scorecardId(f.tab, 'lead', '2026-11-05') } })
+ expect(row).toMatchObject({ tabId: f.tab, projectId: 'lead', on: '2026-11-05', timeZone: 'Asia/Ho_Chi_Minh' })
+ expect(row!.figures as unknown as ScorecardFigures).toMatchObject({
+ asksShipped: 1, askToShipMedianMinutes: 180,
+ unansweredAsks: 1, oldestUnansweredAskMinutes: 17 * 60,
+ // 02:00-03:00 the task waited for its agent; from 06:00 the second one waits to midnight.
+ idleMinutes: 60 + 18 * 60,
+ corrections: 1, routeDecisions: 1, ackPosts: 0, agentPosts: 2, lostLeftAlone: 0,
+ })
+ const empty = await db.superTabScorecard.findUnique({ where: { id: scorecardId(f.tab, 'lead', '2026-11-04') } })
+ expect(empty!.figures).toMatchObject({ asksShipped: 0, idleMinutes: 0 })
+
+ // Every read of the pass is served by an index.
+ const collections = ['SuperTabMessage', 'SwarmGroup', 'SwarmStart', 'SuperTabRouteDecision', 'SuperTabDelivery', 'SuperTabScorecard']
+ expect(entries.filter(e => e.ns.endsWith('.SuperTabMessage')).length).toBeGreaterThan(0)
+ const scans = entries.filter(e => collections.some(c => e.ns.endsWith(`.${c}`)) && /COLLSCAN/.test(String(e.planSummary)))
+ expect(scans.map(e => ({ ns: e.ns, plan: e.planSummary }))).toEqual([])
+ })
+
+ it('serves a project\'s days to who reads it, and to nobody else', async () => {
+ const f = await day()
+ now = END + 31 * MIN
+ await scorecardPass(db, db, now)
+ const read = (user: AuthUser) => app.inject({ method: 'GET', url: `/api/super-tabs/${f.tab}/scorecard?projectId=lead`,
+ headers: { authorization: `Bearer ${user.sub}` } })
+ const mine = await read(owner)
+ expect(mine.statusCode).toBe(200)
+ const body = mine.json() as { data?: { days: Array<{ on: string }>; through?: string }; days?: Array<{ on: string }>; through?: string }
+ expect((body.data ?? body).days!.map(d => d.on)).toEqual(['2026-11-05', '2026-11-04'])
+ // The last day there should be a row for: yesterday in the project's zone, whether or not its row exists (review of #3212, 3).
+ expect((body.data ?? body).through).toBe('2026-11-05')
+ expect((await read(outsider)).statusCode).toBeGreaterThanOrEqual(403)
+ })
+})
diff --git a/backend/prisma/schema.prisma b/backend/prisma/schema.prisma
index 4e9a0842f..38a73f21f 100644
--- a/backend/prisma/schema.prisma
+++ b/backend/prisma/schema.prisma
@@ -1594,6 +1594,23 @@ model SuperTabRouteDecision {
@@index([tabId, assignmentId])
}
+/// One project's scorecard for one local day (superTabs/scorecard): counts and minutes only, never text, names or ids. One row
+/// per project and day, about 1 KB; the nightly job deletes a project's rows older than 120 days when it writes (rows.ts
+/// SCORECARD_DAYS_KEPT), and a Swarm's go with its purge (purge.ts).
+model SuperTabScorecard {
+ id String @id @map("_id")
+ tabId String
+ projectId String
+ /// The local date, YYYY-MM-DD, in timeZone.
+ on String
+ timeZone String
+ figures Json
+ computedAt DateTime
+
+ /// A project's days, and the prune of its old ones.
+ @@index([tabId, projectId, on])
+}
+
/// How one model did on one kind of job in one week of one project of a Swarm (superTabs/evidence/store.ts): counts and sums
/// only, never a person, an agent or task text. `id` is the hash of the Swarm, project, engine, model, kind and week. Kept 180 days.
model SuperTabModelEvidence {
diff --git a/backend/scripts/scorecard-baseline.ts b/backend/scripts/scorecard-baseline.ts
new file mode 100644
index 000000000..4bf83e7c9
--- /dev/null
+++ b/backend/scripts/scorecard-baseline.ts
@@ -0,0 +1,75 @@
+// The scorecard's baseline (task #3386): every project's figures for the days given, computed by the nightly job's own code
+// (superTabs/scorecard) against a read-only database, printed as Markdown tables. It writes nothing.
+//
+// Safety: reads only. The URL comes from MONGO_READONLY_URL, is switched to readPreference=secondary, and the run is refused
+// when the login may write. The URL is never printed. Privacy: Swarms and projects are numbered, never named, except the
+// project passed as NAMED_PROJECT (the public repository's own), and no text, name or id leaves the database.
+//
+// Run from backend/: MONGO_READONLY_URL=... NAMED_TAB=<tabId> NAMED_PROJECT=<projectId> npx tsx scripts/scorecard-baseline.ts 2026-11-04 2026-11-05
+import { PrismaClient } from '@prisma/client'
+import { listNamedIndexes } from '../src/superTabs/parts/indexed.js'
+import { projectsOf } from '../src/superTabs/projects.js'
+import type { SuperTabState } from '../src/superTabs/model.js'
+import { scorecardPlan } from '../src/superTabs/scorecard/job.js'
+import { dayBounds, scorecardDay, type ScorecardFigures } from '../src/superTabs/scorecard/figures.js'
+import { scorecardRecords } from '../src/superTabs/scorecard/records.js'
+import { secondaryUrl } from './atlas-snapshot.js'
+
+const days = process.argv.slice(2)
+const url = process.env.MONGO_READONLY_URL
+if (!url || !days.length || days.some(d => !/^\d{4}-\d{2}-\d{2}$/.test(d))) {
+ console.error('Usage: MONGO_READONLY_URL=... npx tsx scripts/scorecard-baseline.ts YYYY-MM-DD [YYYY-MM-DD ...]')
+ process.exit(2)
+}
+const db = new PrismaClient({ datasources: { db: { url: secondaryUrl(url) } } })
+
+const COLUMNS: Array<[keyof ScorecardFigures, string]> = [['asksShipped', 'Asks shipped'], ['askToShipMedianMinutes', 'Ask→ship median (min)'],
+ ['unansweredAsks', 'Unanswered asks'], ['oldestUnansweredAskMinutes', 'Oldest unanswered (min)'], ['idleMinutes', 'Nobody working, work waiting (min)'],
+ ['lostLeftAlone', 'Lost >10 min'], ['fixesAfterMerge', 'Fixes after merge'], ['corrections', 'Corrections of PM'], ['starts', 'Starts'],
+ ['startsFailed', 'Failed starts'], ['routeDecisions', 'Route decisions'], ['typedIntoPm', 'Typed into PM'], ['ackPosts', 'Ack-only posts'],
+ ['agentPosts', 'Agent posts']]
+
+async function main() {
+ const status = await db.$runCommandRaw({ connectionStatus: 1, showPrivileges: true }) as unknown as
+ { authInfo?: { authenticatedUserPrivileges?: Array<{ actions?: string[] }> } }
+ const actions = new Set((status.authInfo?.authenticatedUserPrivileges ?? []).flatMap(p => p.actions ?? []))
+ for (const write of ['insert', 'update', 'remove']) if (actions.has(write)) throw new Error(`Refused: this login may ${write}.`)
+ await listNamedIndexes(db)
+ const began = Date.now()
+ const { targets, failed } = await scorecardPlan(db, Date.parse(`${days.at(-1)}T23:59:59Z`) + 36 * 3600_000)
+ if (failed) console.error(`${failed} Swarms could not be read.`)
+ const named = new Map<string, string>()
+ const namedTab = process.env.NAMED_TAB
+ if (namedTab) {
+ const head = await db.swarmHead.findUnique({ where: { id: `${namedTab}:head` }, select: { data: true } })
+ for (const p of head ? projectsOf(head.data as unknown as SuperTabState) : []) named.set(p.id, p.name)
+ }
+ const tabs = [...new Set(targets.map(t => t.tabId))]
+ for (const on of days) {
+ const rows: Array<{ label: string; figures: ScorecardFigures; capped: number }> = []
+ for (const target of targets) {
+ const { start, end } = dayBounds(on, target.timeZone)
+ const { input, capped } = await scorecardRecords(db, { tabId: target.tabId, projectId: target.projectId, chatId: target.chatId, start, end })
+ const figures = scorecardDay(input)
+ // A project with nothing in the day says nothing about the process.
+ if (!figures.agentPosts && !figures.starts && !figures.routeDecisions && !figures.asksShipped && !figures.unansweredAsks && !figures.idleMinutes) continue
+ const label = target.tabId === namedTab && target.projectId === process.env.NAMED_PROJECT ? named.get(target.projectId) ?? 'project'
+ : `Swarm ${tabs.indexOf(target.tabId) + 1}, project ${targets.filter(t => t.tabId === target.tabId).indexOf(target) + 1}`
+ rows.push({ label, figures, capped })
+ }
+ console.log(`\n### ${on} (each project's own day, ${rows.length} projects with activity)\n`)
+ console.log(`| Project | ${COLUMNS.map(([, title]) => title).join(' | ')} |`)
+ console.log(`|---|${COLUMNS.map(() => '---:').join('|')}|`)
+ const sum = (key: keyof ScorecardFigures) => rows.reduce((total, r) => total + (typeof r.figures[key] === 'number' ? r.figures[key] as number : 0), 0)
+ for (const r of rows.sort((a, b) => a.label.localeCompare(b.label))) {
+ console.log(`| ${r.label}${r.capped ? ' (capped)' : ''} | ${COLUMNS.map(([key]) => r.figures[key] ?? '–').join(' | ')} |`)
+ }
+ console.log(`| **All** | ${COLUMNS.map(([key]) => key.endsWith('Minutes') && key !== 'idleMinutes' ? '' : sum(key)).join(' | ')} |`)
+ }
+ console.error(`Read in ${Math.round((Date.now() - began) / 1000)} s.`)
+}
+
+main().finally(() => db.$disconnect()).catch(error => {
+ console.error(String(error instanceof Error ? error.message : error).replace(/mongodb(\+srv)?:\/\/[^\s]+/g, 'mongodb://***'))
+ process.exit(1)
+})
diff --git a/backend/src/routes/superTabs.ts b/backend/src/routes/superTabs.ts
index ca73717de..97a69155e 100644
--- a/backend/src/routes/superTabs.ts
+++ b/backend/src/routes/superTabs.ts
@@ -90,6 +90,7 @@ const cursor = z.string().min(1).max(512).regex(/^[A-Za-z0-9_-]+$/)
const changesQuery = agentQuery.extend({ cursor, machines: z.literal('light').optional(),
derivedEtag: z.string().min(1).max(64).regex(/^[A-Za-z0-9_-]+$/).optional() })
const costsQuery = z.object({ days: z.coerce.number().int().min(1).max(MAX_DAYS).default(DEFAULT_DAYS), asAgentId: id.optional() }).strict()
+const scorecardQuery = z.object({ projectId: id, scope: z.literal('all').optional() }).strict()
const leaderboardQuery = z.object({ projectId: id, week: z.coerce.number().int().nonnegative().safe().optional(), period: z.enum(PERIODS).optional(), scope: z.literal('all').optional() }).strict()
.refine(query => query.week === undefined || query.period === undefined, 'Ask for a week or a period, not both.')
.refine(query => query.scope === undefined || query.period !== undefined, 'All projects is read by period.')
@@ -675,6 +676,10 @@ export async function superTabRoutes(app: FastifyInstance, options: { store?: Su
app.post<{ Params: z.infer<typeof tabParams>; Querystring: z.infer<typeof noQuery>; Body: z.infer<typeof activityReadBody> }>('/api/super-tabs/:tabId/activity/read',
{ preHandler: [validateParams(tabParams), validateQuery(noQuery), validateBody(activityReadBody)] },
(req, reply) => respond(reply, () => reads.markActivityRead(req.user!, req.params.tabId, req.body.through)))
+ // A project's daily scorecard (superTabs/scorecard), or every readable project's with scope=all.
+ app.get<{ Params: z.infer<typeof tabParams>; Querystring: z.infer<typeof scorecardQuery> }>('/api/super-tabs/:tabId/scorecard',
+ { preHandler: [validateParams(tabParams), validateQuery(scorecardQuery)] },
+ (req, reply) => respond(reply, () => reads.scorecard(req.user!, req.params.tabId, req.query.projectId, req.query.scope === 'all')))
app.get<{ Params: z.infer<typeof tabParams>; Querystring: z.infer<typeof leaderboardQuery> }>('/api/super-tabs/:tabId/leaderboard',
{ preHandler: [validateParams(tabParams), validateQuery(leaderboardQuery)] },
(req, reply) => respond(reply, () => reads.leaderboard(req.user!, req.params.tabId, req.query.projectId, req.query.week, req.query.period, req.query.scope === 'all')))
diff --git a/backend/src/superTabs/commands.test.ts b/backend/src/superTabs/commands.test.ts
index 29b45a960..455d05be0 100644
--- a/backend/src/superTabs/commands.test.ts
+++ b/backend/src/superTabs/commands.test.ts
@@ -2674,6 +2674,16 @@ describe('cancelling work', () => {
f.run(cancel, workerActor)
expect(f.state.conversations.find(c => c.id === group)!.assignment!.status).toBe('cancelled')
})
+ it('marks its cancel row with the status, so the scorecard sees the task close (#3386)', () => {
+ const { f, group, leadActor, cancel } = active()
+ const row = f.run(cancel, leadActor).messages.find(m => m.conversationId === group)!
+ expect(row.source).toMatchObject({ kind: 'system', status: 'cancelled' })
+ })
+ it('marks an unassigned task\'s cancel row with the status too', () => {
+ const f = fixture(), group = f.group()
+ const row = f.run({ type: 'work.cancel', conversationId: group, assignmentId: 'current', reason: 'Not needed' }).messages.find(m => m.conversationId === group)!
+ expect(row.source).toMatchObject({ kind: 'system', status: 'cancelled' })
+ })
it('names the owner when the owner cancels and drops a trailing full stop from the reason', () => {
const { f, cancel } = active()
const text = f.run({ ...cancel, reason: 'Obsolete.' } as Command).messages[0]!.text
diff --git a/backend/src/superTabs/commands.ts b/backend/src/superTabs/commands.ts
index eb149919c..caf432ecc 100644
--- a/backend/src/superTabs/commands.ts
+++ b/backend/src/superTabs/commands.ts
@@ -1,4 +1,5 @@
import { applyPushOrigin, failPushUnknown, failTeamWritten, mainAccountMembers, markTeamWritten, mayName, requireTeamWrittenAccount, requireTeamWrittenMachine, stepMark, TEAM_WRITTEN_REASON, TEAM_WRITTEN_STOP, teamWrittenTask } from './teamWritten.js'
+import { crewMoveFailed, takeOverCrewMember, wakeCrewMember } from './crewKeep.js'
import type { PushOrigin } from '../github/pushOrigin.js'
import { requireRepositoryReach, unknownPushReason, type RepositoryReach } from './repositoryReach.js'
import { admitByRoom, admitMachineStart, noRepositoryStart, failOwnAccountTeamEntry, ownAccountTeamEntry, defaultProfileId, storedProfileId, BUDDY_WINDOWS_REFUSAL, refusesWindowsBuddy, buddyHomeFolder, buddyProjectsOn, SWARM_BUDDY_CAPABILITY, cleanupOverdue, dropIdleBuddyMachines, machineLimitReached, MACHINE_LIMIT_MESSAGE, NOT_SWARM_MACHINE, downSince, expireRemoval, expireRemovals, failTeamWork, machineContribution, machineOffline, machineSlots, contributedEngines, ownedMachine, productManagerOn, projectFolder, projectsOnMachine, requireTeamWork, teamMachineFor, teamAccountPool, teamPoolsOf, teamStartRequest, teamWorkCheck, movedToTeamAccount, type ContributedMachine } from './machines.js'
@@ -1114,6 +1115,8 @@ function applyCommandOnce(original: SuperTabState, actor: Actor, input: Command,
/** Any other row the system writes in the actor's name. Without a source, a notice written as the owner read as the owner
* asking, and a Lead could quote it to change rules (review of #1594). Only `message.send` and an initial message stay plain. */
const SYSTEM_ROW = { kind: 'system' }
+ /** A cancel, with its status as a status row has it: the scorecard's task timelines see the task close (scorecard/figures.ts). */
+ const CANCEL_ROW = { ...SYSTEM_ROW, status: 'cancelled' }
/** [agentId] as a member of [group] who may be told there: joined when it is not one yet and may join; else null. */
const reachable = (group: Conversation, agentId: string | null): string | null => {
if (!agentId || group.agentIds.includes(agentId)) return agentId
@@ -2942,6 +2945,8 @@ function applyCommandOnce(original: SuperTabState, actor: Actor, input: Command,
// Only its first failure counts against its engine: the server may have timed it out before its host said so (liveness.ts).
const counted = !start.error
start.error = command.error
+ // A crew member's move that did not start leaves it where it was, for the keeper's next pass (crewKeep.ts).
+ if (start.request.crewMoveOf) crewMoveFailed(state, start, context.now)
const suspended = counted ? noteStartFailed(state, start, context.now) : null
// A thread session's says so in its thread (buddySessions.ts), and frees its place.
if (start.request.buddySession) {
@@ -3041,6 +3046,8 @@ function applyCommandOnce(original: SuperTabState, actor: Actor, input: Command,
start.harnessId = command.harnessId
noteStartSucceeded(state, start)
result.agentId = agentId
+ // A crew member's move: its new session becomes the same member, and the old one is retired (crewKeep.ts).
+ if (start.request.crewMoveOf) takeOverCrewMember(state, start, agentId, context.now, target => removeAgent(target))
// A thread session joins its chat's record, and hears the person's messages sent while it started, by delivery as any
// other: its host's person check, run: judging and read grant apply to them too (spec: Security).
if (start.request.buddySession) {
@@ -3726,7 +3733,10 @@ function applyCommandOnce(original: SuperTabState, actor: Actor, input: Command,
const worker = agent(state, command.agentId)
// Before anything changes: a task given to a closed session is lost at once, and its agent then stays listed until the
// task is handed on (closedAgents.ts removeClosedAgents). Without a task it leaves on its machine's next report.
- if (sessionClosed(state, worker, context.now)) {
+ // A crew member on its way to another machine takes work once there; an asleep one wakes for it, in place (crewKeep.ts).
+ if (worker.movedTo) fail('CREW_MOVING', `${worker.displayName ?? worker.name} is moving to another machine. Assign it once it has moved.`)
+ const woke = wakeCrewMember(worker, context.now)
+ if (!woke && sessionClosed(state, worker, context.now)) {
fail('AGENT_CLOSED', `${worker.displayName ?? worker.name}'s session was closed. Start a new agent for this task.`)
}
projectChatJoin(group, worker.id, worker.displayName ?? worker.name)
@@ -3901,7 +3911,7 @@ function applyCommandOnce(original: SuperTabState, actor: Actor, input: Command,
const reviewEnded = !!group.reviewOf && !!assignment && ['stopped', 'done'].includes(assignment.status)
&& (command.assignmentId === 'current' || command.assignmentId === assignment.id)
if ((unassigned || reviewEnded) && allowed && group.id !== project.leadConversationId && !group.kind && !group.audience) {
- message(group.id, `${who} cancelled this task: ${command.reason.replace(/[.\s]+$/, '')}.`, [], null, SYSTEM_ROW)
+ message(group.id, `${who} cancelled this task: ${command.reason.replace(/[.\s]+$/, '')}.`, [], null, CANCEL_ROW)
// Its line is a report the coordinator acknowledges first, so the sweep puts it to sleep later (sweeps.ts), as for any archive.
group.archived = true
endStepsOf(group, `${group.title} was archived.`, true)
@@ -3929,7 +3939,7 @@ function applyCommandOnce(original: SuperTabState, actor: Actor, input: Command,
endStepsOf(group, `${group.title} was cancelled.`, false)
message(group.id, `${who} cancelled this task: ${command.reason.replace(/[.\s]+$/, '')}.${gone ? '' : ' Stop working on it.'}`,
// Telling its agent to stop is the agent's alone; people read the cancel and its reason on the task itself.
- gone ? [] : [assignment.agentId], null, gone ? SYSTEM_ROW : forRecipients(SYSTEM_ROW))
+ gone ? [] : [assignment.agentId], null, gone ? CANCEL_ROW : forRecipients(CANCEL_ROW))
break
}
case 'work.resume': {
diff --git a/backend/src/superTabs/crewKeep.apply.test.ts b/backend/src/superTabs/crewKeep.apply.test.ts
new file mode 100644
index 000000000..9358637db
--- /dev/null
+++ b/backend/src/superTabs/crewKeep.apply.test.ts
@@ -0,0 +1,168 @@
+import { describe, expect, it } from 'vitest'
+import { applyCommand, type Command } from './commands.js'
+import { applyCrewKeep } from './crewKeep.js'
+import { config, fixture, machine, NOW, person } from './crewKeep.test-support.js'
+import { activeWork, type SuperTabState } from './model.js'
+
+const everywhere = { admits: () => true }
+const host = { contribution: { machineOwnerId: 'owner', contributorId: 'owner' } }
+const run = (state: SuperTabState, command: Command, operationId: string, now = NOW, extra: Record<string, unknown> = {}) =>
+ applyCommand(state, person, command, { now, operationId, ...extra }).state
+
+/** [f]'s Swarm with a task 'Fix it' its crew member works on. */
+function working(f: ReturnType<typeof fixture>) {
+ f.state = run(f.state, { type: 'conversation.create', title: 'Fix it', brief: 'Fix the thing', checks: [] } as Command, 'task-1', NOW - 3_600_000)
+ const task = f.state.conversations.find(c => c.title === 'Fix it')!
+ f.state = run(f.state, { type: 'work.assign', conversationId: task.id, agentId: f.ada.id } as Command, 'assign-1', NOW - 60_000)
+ return f.state.conversations.find(c => c.id === task.id)!
+}
+
+describe('applying the crew keeper in the Swarm\'s sweep', () => {
+ it('puts an idle crew member to sleep, and says nothing', () => {
+ const f = fixture()
+ expect(applyCrewKeep(f.state, NOW, everywhere)).toEqual([])
+ expect(f.state.agents.find(a => a.id === f.ada.id)!.asleep).toEqual({ since: NOW })
+ })
+
+ it('moves a member off a failing machine: same name, role and engine, a hand-off note, on the best fallback', () => {
+ const f = fixture([machine('mac', { signedIn: false }), machine('mini')])
+ f.ada.roleName = 'Engineer'
+ const drafts = applyCrewKeep(f.state, NOW, everywhere)
+ const ada = f.state.agents.find(a => a.id === f.ada.id)!
+ const start = f.state.starts!.find(s => s.request.crewMoveOf === ada.id)!
+ expect(ada.movedTo).toEqual({ machineId: 'mini', startId: start.id, reason: 'signed-out', at: NOW })
+ expect(start.request).toMatchObject({ machineId: 'mini', name: ada.name, roleName: 'Engineer', engine: 'claude', model: 'opus', effort: 'high',
+ cwd: '/swarms/mini/lead', worktree: true })
+ expect(start.request.handoffNote).toContain('signed out')
+ expect(drafts).toEqual([expect.objectContaining({ conversationId: 'lead', text: expect.stringContaining('moving to mini') })])
+ // Moved once: the next pass leaves it to its start.
+ expect(applyCrewKeep(f.state, NOW + 1000, everywhere)).toEqual([])
+ expect(f.state.starts!.filter(s => s.request.crewMoveOf === ada.id)).toHaveLength(1)
+ })
+
+ it('says once when no machine can take a member, and drops a long-asleep folder', () => {
+ const f = fixture([machine('mac', { signedIn: false })])
+ expect(applyCrewKeep(f.state, NOW, everywhere)).toEqual([expect.objectContaining({ text: expect.stringContaining('no other machine') })])
+ expect(applyCrewKeep(f.state, NOW + 1000, everywhere)).toEqual([])
+ const asleep = fixture()
+ asleep.ada.asleep = { since: NOW - 8 * 86_400_000 }
+ applyCrewKeep(asleep.state, NOW, everywhere)
+ expect(asleep.state.agents.find(a => a.id === asleep.ada.id)!.asleep).toEqual({ since: NOW - 8 * 86_400_000, folderDropped: true })
+ })
+})
+
+describe('waking and moving through commands', () => {
+ it('new work wakes an asleep crew member in place, and a moving one takes none', () => {
+ const f = fixture()
+ f.ada.asleep = { since: NOW - 7_200_000 }
+ const task = working(f)
+ const ada = f.state.agents.find(a => a.id === f.ada.id)!
+ expect(ada.asleep).toBeUndefined()
+ expect(activeWork(task) && task.assignment!.agentId).toBe(ada.id)
+ const moving = fixture()
+ moving.ada.movedTo = { machineId: 'mini', startId: 's', reason: 'offline', at: NOW - 1000 }
+ expect(() => working(moving)).toThrow(/moving/)
+ })
+
+ it('a moved member\'s new session takes over: same name, crew and groups, its task lost to be handed on, the old session closed', () => {
+ const f = fixture([machine('mac', { signedIn: false }), machine('mini')])
+ const task = working(f)
+ const oldId = f.ada.id
+ const name = f.state.agents.find(a => a.id === oldId)!.displayName ?? f.ada.name
+ applyCrewKeep(f.state, NOW, everywhere)
+ const start = f.state.starts!.find(s => s.request.crewMoveOf === oldId)!
+ f.state = run(f.state, { type: 'agent.started', startId: start.id, harnessId: 'ada-2', configuration: config } as Command, 'started-1', NOW + 5000, host)
+ const moved = f.state.agents.find(a => a.harnessId === 'ada-2')!
+ expect(f.state.agents.some(a => a.id === oldId)).toBe(false)
+ expect(moved).toMatchObject({ machineId: 'mini', displayName: name, crew: { role: 'Engineer' }, movedFrom: { agentId: oldId, machineId: 'mac', reason: 'signed-out' } })
+ const after = f.state.conversations.find(c => c.id === task.id)!
+ expect(after.assignment).toMatchObject({ status: 'lost', lost: { reason: 'crew-moved' } })
+ expect(f.state.conversations.find(c => c.id === 'lead')!.agentIds).toContain(moved.id)
+ // The old session is retired as any removed agent's: its host closes it and drops its folder when clean (closingSessions).
+ expect(f.state.closingSessions).toEqual([expect.objectContaining({ machineId: 'mac', harnessId: 'ada', agentId: oldId })])
+ })
+
+ it('a move whose start fails lets the keeper try again', () => {
+ const f = fixture([machine('mac', { signedIn: false }), machine('mini')])
+ applyCrewKeep(f.state, NOW, everywhere)
+ const start = f.state.starts!.find(s => s.request.crewMoveOf === f.ada.id)!
+ f.state = run(f.state, { type: 'agent.startFailed', startId: start.id, error: 'No room' } as Command, 'failed-1', NOW + 5000, host)
+ expect(f.state.agents.find(a => a.id === f.ada.id)!.movedTo).toBeUndefined()
+ })
+})
+
+describe('a take-over never joins a conversation its new session is barred from (security review of #3229)', () => {
+ it('leaves a Team-written task out when the new session runs in the main account', () => {
+ const f = fixture([machine('mac', { signedIn: false }), machine('mini')])
+ f.state = run(f.state, { type: 'conversation.create', title: 'Team task', brief: 'b', checks: [] } as Command, 'tw', NOW - 3_600_000)
+ const task = f.state.conversations.find(c => c.title === 'Team task')!
+ task.agentIds.push(f.ada.id)
+ // Placed by hand, as an older server could have: the take-over itself must still refuse the join.
+ applyCrewKeep(f.state, NOW, everywhere)
+ const start = f.state.starts!.find(s => s.request.crewMoveOf === f.ada.id)!
+ f.state.conversations.find(c => c.id === task.id)!.teamWritten = true
+ f.state = run(f.state, { type: 'agent.started', startId: start.id, harnessId: 'ada-2', configuration: config } as Command, 'started-tw', NOW + 5000, host)
+ const moved = f.state.agents.find(a => a.harnessId === 'ada-2')!
+ expect(f.state.conversations.find(c => c.id === task.id)!.agentIds).not.toContain(moved.id)
+ expect(f.state.conversations.find(c => c.id === 'lead')!.agentIds).toContain(moved.id)
+ })
+})
+
+describe('a refused move start (security review 2 of #3229)', () => {
+ it('records the machine that refused it, so the next pass tries another', () => {
+ const f = fixture([machine('mac', { signedIn: false }), machine('mini')])
+ applyCrewKeep(f.state, NOW, everywhere)
+ const start = f.state.starts!.find(s => s.request.crewMoveOf === f.ada.id)!
+ f.state = run(f.state, { type: 'agent.startFailed', startId: start.id, error: 'No token' } as Command, 'failed-2', NOW + 5000, host)
+ expect(f.state.agents.find(a => a.id === f.ada.id)!.crewMoveRefused).toEqual({ machineId: 'mini', at: NOW + 5000 })
+ })
+})
+
+describe('a moved member\'s task reaches the head of the list (review of #3229)', () => {
+ it('also when the old session lost it before the new one started: offline, or its host closed it', () => {
+ const f = fixture([machine('mac', { signedIn: false }), machine('mini')])
+ f.state = run(f.state, { type: 'conversation.create', title: 'Other', brief: 'Other work', checks: [] } as Command, 'other', NOW - 7_200_000)
+ const task = working(f)
+ const other = f.state.conversations.find(c => c.title === 'Other')!
+ applyCrewKeep(f.state, NOW, everywhere)
+ // The old host's report shows the session stopped (or its machine went quiet): liveness loses the task first.
+ const lost = f.state.conversations.find(c => c.id === task.id)!
+ lost.assignment = { ...lost.assignment!, status: 'lost', lost: { reason: 'session stopped', at: NOW + 2000 } }
+ const start = f.state.starts!.find(s => s.request.crewMoveOf === f.ada.id)!
+ f.state = run(f.state, { type: 'agent.started', startId: start.id, harnessId: 'ada-2', configuration: config } as Command, 'started-lost', NOW + 5000, host)
+ const rank = (id: string) => f.state.conversations.find(c => c.id === id)!.queueRank ?? Number.MAX_SAFE_INTEGER
+ expect(rank(task.id)).toBeLessThan(rank(other.id))
+ })
+})
+
+describe('the head of the list when the move is placed (review of #3229, River)', () => {
+ it('an offline machine: liveness lost the task before the move, which puts it at the head at once', () => {
+ const f = fixture([machine('mac', { receivedAt: NOW - 6 * 60_000 }), machine('mini')])
+ f.state = run(f.state, { type: 'conversation.create', title: 'Other', brief: 'Other work', checks: [] } as Command, 'other-2', NOW - 7_200_000)
+ const task = working(f)
+ const other = f.state.conversations.find(c => c.title === 'Other')!
+ // Lost at 2 minutes offline, before the 5-minute move.
+ const lost = f.state.conversations.find(c => c.id === task.id)!
+ lost.assignment = { ...lost.assignment!, status: 'lost', lost: { reason: 'machine offline', at: NOW - 4 * 60_000 } }
+ f.state.agents.find(a => a.id === f.ada.id)!.lostTask = { conversationId: task.id, assignmentId: lost.assignment.id, reason: 'machine offline', at: NOW - 4 * 60_000 }
+ applyCrewKeep(f.state, NOW, everywhere)
+ const rank = (id: string) => f.state.conversations.find(c => c.id === id)!.queueRank ?? Number.MAX_SAFE_INTEGER
+ expect(rank(task.id)).toBeLessThan(rank(other.id))
+ })
+})
+
+describe('a take-over marks nothing Team-written (security review of #3229, Curlew3)', () => {
+ it('a Team member moved to another Team machine joins its old conversations without marking them', () => {
+ const team = (pool: ReturnType<typeof machine>) => ({ ...pool, hostMachineId: `${pool.machineId}-mac` }) as typeof pool
+ const f = fixture([team(machine('mac', { signedIn: false })), team(machine('mini'))])
+ f.state = run(f.state, { type: 'conversation.create', title: 'Talk', brief: 'A chat it was in', checks: [] } as Command, 'talk', NOW - 3_600_000)
+ const talk = f.state.conversations.find(c => c.title === 'Talk')!
+ talk.agentIds.push(f.ada.id)
+ applyCrewKeep(f.state, NOW, everywhere)
+ const start = f.state.starts!.find(s => s.request.crewMoveOf === f.ada.id)!
+ f.state = run(f.state, { type: 'agent.started', startId: start.id, harnessId: 'ada-2', configuration: config } as Command, 'started-team', NOW + 5000, host)
+ const after = f.state.conversations.find(c => c.id === talk.id)!
+ expect(after.agentIds).toContain(f.state.agents.find(a => a.harnessId === 'ada-2')!.id)
+ expect(after.teamWritten).toBeFalsy()
+ })
+})
diff --git a/backend/src/superTabs/crewKeep.test-support.ts b/backend/src/superTabs/crewKeep.test-support.ts
new file mode 100644
index 000000000..19531d074
--- /dev/null
+++ b/backend/src/superTabs/crewKeep.test-support.ts
@@ -0,0 +1,43 @@
+/** Fake machines and a Swarm with one crew member, for the crew keeper's tests (crewKeep.test.ts, crewKeep.apply.test.ts). */
+import { applyCommand } from './commands.js'
+import { SLEEP_IDLE_MS } from './crewRules.js'
+import { createSuperTab, type Actor, type Agent, type SuperTabState } from './model.js'
+
+export const config = { engine: 'claude', model: 'opus', effort: 'high', contextWindow: null, engineVersion: null }
+export const person: Actor = { kind: 'person', userId: 'owner', email: '[email protected]' }
+export const NOW = Date.parse('2026-11-05T20:00:00Z')
+export const GB = 1024 ** 3
+export type Pool = NonNullable<SuperTabState['machines']>[number]
+
+/** A fresh machine reading: online now, signed in, quota half used resetting in [resetIn], [diskFree] bytes free. */
+export function machine(machineId: string, options: { receivedAt?: number; signedIn?: boolean; usedPercent?: number; resetIn?: number; known?: boolean;
+ diskFree?: number; team?: boolean; hostMachineId?: string } = {}): Pool {
+ return {
+ machineId, name: machineId, cwd: `/swarms/${machineId}`, engines: ['claude'], maxAgents: 8, contributorId: 'owner', membershipGeneration: 'g',
+ generation: `${machineId}-g`, paused: false, scope: 'swarm', ...(options.hostMachineId ? { hostMachineId: options.hostMachineId } : {}),
+ observation: {
+ receivedAt: options.receivedAt ?? NOW - 5_000, runningHarnessIds: [], stoppedHarnessIds: [],
+ resources: { diskFreeBytes: options.diskFree ?? 200 * GB, diskTotalBytes: 500 * GB, memoryUsedBytes: 0, memoryTotalBytes: 64 * GB },
+ readiness: { engines: [{ engine: 'claude', installed: true, signedIn: options.signedIn ?? true }] },
+ quotas: [{ engine: 'claude', known: options.known ?? true, windows: [{ name: 'weekly', usedPercent: options.usedPercent ?? 50,
+ resetsAt: new Date(NOW + (options.resetIn ?? 3 * 86_400_000)).toISOString() }] }],
+ },
+ } as unknown as Pool
+}
+
+/** A Swarm with its PM and one crew member 'Ada' on 'mac', started [startedAgo] ms ago, and machines [pools]. */
+export function fixture(pools: Pool[] = [machine('mac'), machine('mini')], startedAgo = 2 * SLEEP_IDLE_MS) {
+ let state: SuperTabState = createSuperTab('tab', 'Work', { id: 'owner', email: '[email protected]' }, 'prod')
+ const add = (harnessId: string, lead: boolean, machineId = 'mac') => {
+ state = applyCommand(state, person, { type: 'agent.add', conversationId: 'lead', machineId, harnessId, name: harnessId, lead, configuration: config },
+ { now: NOW - startedAgo, operationId: `add-${harnessId}`, contribution: { machineOwnerId: 'owner', contributorId: 'owner' } }).state
+ return state.agents.find(a => a.harnessId === harnessId)!
+ }
+ const lead = add('lead', true)
+ const ada = add('ada', false)
+ ada.startedSession = { creationId: 'c', sessionId: 's', createdAt: new Date(NOW - startedAgo).toISOString() }
+ ;(ada as Agent & { crew?: unknown }).crew = { role: 'Engineer', at: NOW - startedAgo, by: 'owner' }
+ const generation = state.people.find(p => p.id === 'owner')!.generation
+ state.machines = pools.map(pool => ({ ...pool, membershipGeneration: generation }))
+ return { state, lead, ada }
+}
diff --git a/backend/src/superTabs/crewKeep.test.ts b/backend/src/superTabs/crewKeep.test.ts
new file mode 100644
index 000000000..184efdb98
--- /dev/null
+++ b/backend/src/superTabs/crewKeep.test.ts
@@ -0,0 +1,205 @@
+import { describe, expect, it } from 'vitest'
+import { applyCommand } from './commands.js'
+import { crewKeep, crewNeedsMachine, type CrewDeps } from './crewKeep.js'
+import { config, fixture, GB, machine, NOW, person } from './crewKeep.test-support.js'
+import * as crewRules from './crewRules.js'
+import type { MachineRule } from './crewRules.js'
+import { FOLDER_ASLEEP_MS, MOVE_LOW_DISK_BYTES, MOVE_OFFLINE_MS, MOVE_QUOTA_OUT_MS, SLEEP_IDLE_MS, DISK_FLOOR_BYTES } from './crewRules.js'
+import type { Agent, SuperTabState } from './model.js'
+
+const everywhere: CrewDeps = { admits: () => true }
+/** The rules live in the project rules record, never in source (River, 11-05). */
+const MACHINE_RULES_FROM_SOURCE = (crewRules as Record<string, unknown>).MACHINE_RULES
+const kinds = (state: SuperTabState, deps = everywhere) => crewKeep(state, NOW, deps)
+
+describe('crewKeep: sleep', () => {
+ it('sleeps a crew member idle over an hour, not one idle under it, and never one with active work', () => {
+ expect(kinds(fixture(undefined, SLEEP_IDLE_MS + 60_000).state)).toEqual([{ kind: 'sleep', agentId: expect.any(String) }])
+ expect(kinds(fixture(undefined, SLEEP_IDLE_MS - 60_000).state)).toEqual([])
+ const busy = fixture()
+ busy.state = applyCommand(busy.state, person, { type: 'conversation.create', title: 'Fix it', brief: 'b', checks: [] } as never,
+ { now: NOW - 2 * SLEEP_IDLE_MS, operationId: 'g1' }).state
+ const group = busy.state.conversations.find(c => c.title === 'Fix it')!
+ group.agentIds.push(busy.ada.id)
+ group.assignment = { id: 'a1', agentId: busy.ada.id, status: 'working', assignedAt: NOW - 2 * SLEEP_IDLE_MS } as never
+ expect(kinds(busy.state)).toEqual([])
+ })
+
+ it('never touches a PM, a member who is not crew, or one already asleep', () => {
+ const f = fixture()
+ ;(f.lead as Agent & { crew?: unknown }).crew = { role: 'PM', at: 1, by: 'owner' }
+ delete (f.ada as Agent & { crew?: unknown }).crew
+ expect(kinds(f.state)).toEqual([])
+ const asleep = fixture()
+ asleep.ada.asleep = { since: NOW - 60_000 }
+ expect(kinds(asleep.state)).toEqual([])
+ })
+})
+
+describe('crewKeep: move when its machine fails', () => {
+ const moveOf = (state: SuperTabState, deps = everywhere) => crewKeep(state, NOW, deps).find(a => a.kind === 'move')
+ it('moves a member holding work after the machine is offline over 5 minutes, not before', () => {
+ // Holding work: the task its machine took from it, as liveness does at 2 minutes offline.
+ const holding = (pools: ReturnType<typeof machine>[]) => {
+ const f = fixture(pools)
+ f.state = applyCommand(f.state, person, { type: 'conversation.create', title: 'Fix it', brief: 'b', checks: [] } as never, { now: NOW - 7_200_000, operationId: 'held' }).state
+ const group = f.state.conversations.find(c => c.title === 'Fix it')!
+ group.assignment = { id: 'a1', agentId: f.ada.id, status: 'lost', lost: { reason: 'machine offline', at: NOW - 180_000 } } as never
+ f.state.agents.find(a => a.id === f.ada.id)!.lostTask = { conversationId: group.id, assignmentId: 'a1', reason: 'machine offline', at: NOW - 180_000 }
+ return f.state
+ }
+ expect(moveOf(holding([machine('mac', { receivedAt: NOW - MOVE_OFFLINE_MS + 30_000 }), machine('mini')]))).toBeUndefined()
+ expect(moveOf(holding([machine('mac', { receivedAt: NOW - MOVE_OFFLINE_MS - 1 }), machine('mini')])))
+ .toEqual({ kind: 'move', agentId: expect.any(String), toMachineId: 'mini', reason: 'offline' })
+ })
+ it('moves at once when its engine is signed out', () => {
+ expect(moveOf(fixture([machine('mac', { signedIn: false }), machine('mini')]).state)).toMatchObject({ toMachineId: 'mini', reason: 'signed-out' })
+ })
+ it('moves when out of quota for over an hour; not when it resets within the hour, nor when quota is unknown', () => {
+ expect(moveOf(fixture([machine('mac', { usedPercent: 100, resetIn: MOVE_QUOTA_OUT_MS + 60_000 }), machine('mini')]).state))
+ .toMatchObject({ toMachineId: 'mini', reason: 'quota' })
+ expect(moveOf(fixture([machine('mac', { usedPercent: 100, resetIn: MOVE_QUOTA_OUT_MS - 60_000 }), machine('mini')]).state)).toBeUndefined()
+ expect(moveOf(fixture([machine('mac', { usedPercent: 100, known: false }), machine('mini')]).state)).toBeUndefined()
+ })
+ it('moves when low on disk', () => {
+ expect(moveOf(fixture([machine('mac', { diskFree: MOVE_LOW_DISK_BYTES - 1 }), machine('mini')]).state)).toMatchObject({ reason: 'disk' })
+ expect(moveOf(fixture([machine('mac', { diskFree: MOVE_LOW_DISK_BYTES + GB }), machine('mini')]).state)).toBeUndefined()
+ })
+ it('picks the fallback in the route\'s order: same account kind, then the account resetting soonest, then most left, then fewer working', () => {
+ const failing = machine('mac', { signedIn: false })
+ // Soonest reset first among usable machines of the same kind.
+ expect(moveOf(fixture([failing, machine('late', { resetIn: 5 * 86_400_000 }), machine('soon', { resetIn: 86_400_000 })]).state))
+ .toMatchObject({ toMachineId: 'soon' })
+ // Same reset: most left.
+ expect(moveOf(fixture([failing, machine('half', { usedPercent: 50 }), machine('fresh', { usedPercent: 10 })]).state))
+ .toMatchObject({ toMachineId: 'fresh' })
+ // A machine that is failing itself, or that does not admit the start, is never a fallback.
+ expect(moveOf(fixture([failing, machine('down', { receivedAt: NOW - MOVE_OFFLINE_MS - 1 }), machine('ok')]).state)).toMatchObject({ toMachineId: 'ok' })
+ expect(moveOf(fixture([failing, machine('refuses'), machine('ok')]).state, { admits: id => id !== 'refuses' })).toMatchObject({ toMachineId: 'ok' })
+ })
+ it('with no fallback says it once, and moves no member twice', () => {
+ const lonely = fixture([machine('mac', { signedIn: false })])
+ expect(kinds(lonely.state)).toEqual([{ kind: 'stuck', agentId: lonely.ada.id, reason: 'signed-out' }])
+ lonely.ada.crewStuck = 'signed-out'
+ expect(kinds(lonely.state)).toEqual([])
+ const moving = fixture([machine('mac', { signedIn: false }), machine('mini')])
+ moving.ada.movedTo = { machineId: 'mini', startId: 's1', reason: 'signed-out', at: NOW - 1000 }
+ expect(kinds(moving.state)).toEqual([])
+ })
+})
+
+describe('crewKeep: folders', () => {
+ it('drops the folder of a member asleep 7 days, once', () => {
+ const f = fixture()
+ f.ada.asleep = { since: NOW - FOLDER_ASLEEP_MS - 1 }
+ expect(kinds(f.state)).toEqual([{ kind: 'dropFolder', agentId: f.ada.id, why: 'asleep-7d' }])
+ f.ada.asleep.folderDropped = true
+ expect(kinds(f.state)).toEqual([])
+ f.ada.asleep = { since: NOW - FOLDER_ASLEEP_MS + 60_000 }
+ expect(kinds(f.state)).toEqual([])
+ })
+ it('below 10 GB free, drops the folder of the member asleep longest on that machine first, one a pass', () => {
+ const f = fixture([machine('mac', { diskFree: DISK_FLOOR_BYTES - GB }), machine('mini')])
+ f.ada.asleep = { since: NOW - 3_600_000 }
+ f.state = applyCommand(f.state, person, { type: 'agent.add', conversationId: 'lead', machineId: 'mac', harnessId: 'bo', name: 'bo', lead: false, configuration: config },
+ { now: NOW - 2 * SLEEP_IDLE_MS, operationId: 'add-bo', contribution: { machineOwnerId: 'owner', contributorId: 'owner' } }).state
+ const bo = f.state.agents.find(a => a.harnessId === 'bo')!
+ ;(bo as Agent & { crew?: unknown }).crew = { role: 'Engineer', at: 1, by: 'owner' }
+ bo.asleep = { since: NOW - 2 * 3_600_000 }
+ expect(kinds(f.state)).toEqual([{ kind: 'dropFolder', agentId: bo.id, why: 'low-disk' }])
+ bo.asleep.folderDropped = true
+ expect(kinds(f.state)).toEqual([{ kind: 'dropFolder', agentId: f.ada.id, why: 'low-disk' }])
+ })
+})
+
+describe('crewKeep: Team-written work stays in Team accounts (security review of #3229)', () => {
+ it('a Team-account member moves only to a Team-account machine, and is stuck with none', () => {
+ const team = (pool: ReturnType<typeof machine>) => ({ ...pool, hostMachineId: `${pool.machineId}-mac` }) as typeof pool
+ const onlyMain = fixture([team(machine('mac', { signedIn: false })), machine('mini')])
+ expect(kinds(onlyMain.state)).toEqual([{ kind: 'stuck', agentId: onlyMain.ada.id, reason: 'signed-out' }])
+ const withTeam = fixture([team(machine('mac', { signedIn: false })), machine('mini', { resetIn: 86_400_000 }), team(machine('studio'))])
+ expect(kinds(withTeam.state)).toEqual([{ kind: 'move', agentId: withTeam.ada.id, toMachineId: 'studio', reason: 'signed-out' }])
+ })
+ it('a main-account member in a Team-written conversation moves only to a Team-account machine', () => {
+ const f = fixture([machine('mac', { signedIn: false }), machine('mini')])
+ f.state.conversations.find(c => c.id === 'lead')!.teamWritten = true
+ // The project's chat is its coordinator's; a task the member works in is what counts.
+ f.state = applyCommand(f.state, person, { type: 'conversation.create', title: 'Team task', brief: 'b', checks: [] } as never, { now: NOW - 7_200_000, operationId: 'tw' }).state
+ const task = f.state.conversations.find(c => c.title === 'Team task')!
+ task.agentIds.push(f.ada.id)
+ task.teamWritten = true
+ expect(kinds(f.state)).toEqual([{ kind: 'stuck', agentId: f.ada.id, reason: 'signed-out' }])
+ })
+})
+
+describe('crewKeep: account kinds and Team sessions (security review 2 of #3229)', () => {
+ const team = (pool: ReturnType<typeof machine>) => ({ ...pool, hostMachineId: `${pool.machineId}-mac` }) as typeof pool
+ it('a main-account member never moves into a Team account', () => {
+ const f = fixture([machine('mac', { signedIn: false }), team(machine('studio'))])
+ expect(kinds(f.state)).toEqual([{ kind: 'stuck', agentId: f.ada.id, reason: 'signed-out' }])
+ })
+ it('a member in a Team account never sleeps: its close there deletes what a wake would resume', () => {
+ const f = fixture([team(machine('mac')), machine('mini')])
+ expect(kinds(f.state)).toEqual([])
+ })
+ it('a machine that refused the member\'s move start is not picked again for an hour', () => {
+ const f = fixture([machine('mac', { signedIn: false }), machine('mini', { resetIn: 86_400_000 }), machine('late')])
+ f.ada.crewMoveRefused = { machineId: 'mini', at: NOW - 30 * 60_000 }
+ expect(kinds(f.state)).toEqual([{ kind: 'move', agentId: f.ada.id, toMachineId: 'late', reason: 'signed-out' }])
+ f.ada.crewMoveRefused = { machineId: 'mini', at: NOW - 61 * 60_000 }
+ expect(kinds(f.state)).toEqual([{ kind: 'move', agentId: f.ada.id, toMachineId: 'mini', reason: 'signed-out' }])
+ })
+})
+
+describe('crewKeep: an asleep member stays where it sleeps (review of #3229)', () => {
+ it('is not moved off a machine that went offline: it has no work there, and moves once woken if it must', () => {
+ const f = fixture([machine('mac', { receivedAt: NOW - MOVE_OFFLINE_MS - 1 }), machine('mini')])
+ f.ada.asleep = { since: NOW - 3_600_000 }
+ expect(kinds(f.state)).toEqual([])
+ })
+ it('says whether any member needs a machine now, so the sweep reads the Team facts only then', () => {
+ const calm = fixture()
+ expect(crewNeedsMachine(calm.state, NOW)).toBe(false)
+ expect(crewNeedsMachine(fixture([machine('mac', { signedIn: false }), machine('mini')]).state, NOW)).toBe(true)
+ })
+})
+
+describe('crewKeep: machine rules apply to moves, read from the project rules record (review of #3229, River)', () => {
+ const at = (iso: string) => Date.parse(iso)
+ // Generic rules as the record would hold them: never a person's real machines or hours in source (River, 11-05).
+ const rules: MachineRule[] = [
+ { machineIds: ['no-codex'], why: 'no Codex there', noEngines: ['codex'] },
+ { machineIds: ['travels'], why: 'away', away: { days: [1, 2, 3, 4, 5], windows: [['07:30', '09:30'], ['18:00', '21:00']], timeZone: 'Asia/Ho_Chi_Minh' } },
+ ]
+ const withRules: CrewDeps = { admits: () => true, machineRules: rules }
+ it('never moves a member to a machine whose rule refuses its engine; another engine may go there', () => {
+ const codex = fixture([machine('mac', { signedIn: false }), machine('no-codex'), machine('other', { resetIn: 6 * 86_400_000 })])
+ codex.ada.configuration = { ...codex.ada.configuration, engine: 'codex' }
+ for (const pool of codex.state.machines!) {
+ pool.engines = ['claude', 'codex']
+ ;(pool.observation as { readiness: { engines: unknown[] } }).readiness.engines = [{ engine: 'codex', installed: true, signedIn: pool.machineId !== 'mac' }]
+ ;(pool.observation as { quotas: Array<{ engine: string }> }).quotas[0]!.engine = 'codex'
+ }
+ expect(crewKeep(codex.state, NOW, withRules)).toEqual([expect.objectContaining({ kind: 'move', toMachineId: 'other' })])
+ const claude = fixture([machine('mac', { signedIn: false }), machine('no-codex'), machine('other', { resetIn: 6 * 86_400_000 })])
+ expect(crewKeep(claude.state, NOW, withRules)).toEqual([expect.objectContaining({ kind: 'move', toMachineId: 'no-codex' })])
+ })
+ it('never moves to a machine while its rule says it is away, in its own time zone', () => {
+ const pools = () => [machine('mac', { signedIn: false }), machine('travels'), machine('other', { resetIn: 6 * 86_400_000 })]
+ // Monday 2026-11-09 08:00 in Vietnam (UTC+7) is 01:00 UTC; 10:00 there is 03:00 UTC.
+ const away = at('2026-11-09T01:00:00Z'), back = at('2026-11-09T03:00:00Z')
+ const onAway = fixture(pools()); onAway.state.machines!.forEach(p => { p.observation!.receivedAt = away - 5000 })
+ expect(crewKeep(onAway.state, away, withRules)).toEqual([expect.objectContaining({ kind: 'move', toMachineId: 'other' })])
+ const onBack = fixture(pools()); onBack.state.machines!.forEach(p => { p.observation!.receivedAt = back - 5000 })
+ expect(crewKeep(onBack.state, back, withRules)).toEqual([expect.objectContaining({ kind: 'move', toMachineId: 'travels' })])
+ })
+ it('with no rules record, no machine rule applies', () => {
+ const f = fixture([machine('mac', { signedIn: false }), machine('travels'), machine('other', { resetIn: 6 * 86_400_000 })])
+ expect(crewKeep(f.state, at('2026-11-09T01:00:00Z') + 0 * NOW, { admits: () => true })).toBeDefined()
+ expect(MACHINE_RULES_FROM_SOURCE).toBeUndefined()
+ })
+ it('an awake member with no work stays when its machine goes offline: it moves once it holds work', () => {
+ const f = fixture([machine('mac', { receivedAt: NOW - MOVE_OFFLINE_MS - 1 }), machine('mini')], SLEEP_IDLE_MS - 60_000)
+ expect(kinds(f.state)).toEqual([])
+ })
+})
diff --git a/backend/src/superTabs/crewKeep.ts b/backend/src/superTabs/crewKeep.ts
new file mode 100644
index 000000000..77338fd34
--- /dev/null
+++ b/backend/src/superTabs/crewKeep.ts
@@ -0,0 +1,309 @@
+/**
+ * The crew keeper (task #3398): what the Swarm does for its crew members on its own, decided from the state alone so it can
+ * be tested with fake machines. A crew member idle an hour sleeps; one whose machine fails moves to the best fallback in the
+ * route's order (project instructions, Order and quota); folders of long-asleep members, and of asleep members on a machine
+ * short of disk, are dropped. The numbers are in crewRules.ts. Applying the actions is liveness.ts's (sweepLiveness).
+ */
+import type { MachineRule } from './crewRules.js'
+import { activeWork, actorKey, stableId, type Agent, type AgentStart, type AgentStartRequest, type CrewTrouble, type MessageDraft, type SuperTabState } from './model.js'
+import { isBuddy } from './buddies.js'
+import { AGENT_MEMORY_BYTES, projectFolder, type ContributedMachine } from './machines.js'
+import { agentProjectId, groupProject, onTeamAccount, projectsOf, teamAccountPool } from './projects.js'
+import { barredFromTeamWritten, teamWrittenTask } from './teamWritten.js'
+import { placeAfter, queueSiblings } from './taskQueue.js'
+import { quotaOf, resetMs } from './reviewSeats.js'
+import { groupsOf, idleSince } from './tidy.js'
+import { DISK_FLOOR_BYTES, FOLDER_ASLEEP_MS, HELD_TASK_MS, MOVE_LOW_DISK_BYTES, MOVE_OFFLINE_MS, MOVE_QUOTA_OUT_MS, MOVE_REFUSED_WAIT_MS, SLEEP_IDLE_MS } from './crewRules.js'
+
+export type CrewAction =
+ | { kind: 'sleep'; agentId: string }
+ | { kind: 'move'; agentId: string; toMachineId: string; reason: CrewTrouble }
+ | { kind: 'stuck'; agentId: string; reason: CrewTrouble }
+ | { kind: 'dropFolder'; agentId: string; why: 'asleep-7d' | 'low-disk' }
+
+/** What the keeper asks of the rest of the Swarm: whether [machineId] would admit a start of [agent]'s kind now (machines.ts
+ * admitMachineStart, with the store's Team-account facts). */
+export interface CrewDeps {
+ admits(machineId: string, agent: Agent): boolean
+ /** The project's machine rules from its rules record (Ledger's task); none until that is on main (River, 11-05). */
+ machineRules?: readonly MachineRule[]
+}
+
+/** Whether [agent] is crew (#3397's `crew` record), read without its type until that change lands. */
+export const isCrewMember = (agent: Agent): boolean => !!(agent as Agent & { crew?: { role?: unknown } }).crew
+
+/** [machineId]'s contribution while it stands: a withdrawn one (its person left or became a viewer) reads as none, never a
+ * refusal, so a sweep never stops on it. */
+function standingPool(state: SuperTabState, machineId: string): ContributedMachine | undefined {
+ const pool = state.machines?.find(m => m.machineId === machineId)
+ const contributor = pool && state.people.find(p => p.id === pool.contributorId)
+ return pool && contributor && contributor.role !== 'viewer' && contributor.generation === pool.membershipGeneration ? pool : undefined
+}
+
+/** Why [machineId] takes no crew member of [engine] at [now] by the project's machine rules ([rules], from the project rules
+ * record), or null. */
+export function machineRuleRefusal(rules: readonly MachineRule[], machineId: string, engine: string, now: number): string | null {
+ for (const rule of rules) {
+ if (!rule.machineIds.includes(machineId)) continue
+ if (rule.noEngines?.includes(engine)) return rule.why
+ if (rule.away) {
+ const parts = Object.fromEntries(new Intl.DateTimeFormat('en-US', { timeZone: rule.away.timeZone, weekday: 'short', hour: '2-digit',
+ minute: '2-digit', hourCycle: 'h23' }).formatToParts(new Date(now)).map(p => [p.type, p.value]))
+ const day = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'].indexOf(parts.weekday ?? '')
+ const time = `${parts.hour}:${parts.minute}`
+ if (rule.away.days.includes(day) && rule.away.windows.some(([from, to]) => time >= from && time < to)) return rule.why
+ }
+ }
+ return null
+}
+
+/** The task [agent] holds: its active one, or one its machine took from it within HELD_TASK_MS and not handed on since. */
+function heldTasks(state: SuperTabState, agent: Agent, now: number) {
+ const lost = agent.lostTask && now - agent.lostTask.at < HELD_TASK_MS ? agent.lostTask : undefined
+ return state.conversations.filter(c => c.assignment?.agentId === agent.id && (activeWork(c)
+ || (c.assignment.status === 'lost' && !!lost && lost.conversationId === c.id && lost.assignmentId === c.assignment.id)))
+}
+
+/** Puts [group] at the head of its project's Next up. */
+function toHead(state: SuperTabState, group: import('./model.js').Conversation): void {
+ const siblings = queueSiblings(state, groupProject(state, group))
+ if (siblings.some(s => s.id === group.id)) for (const [id, rank] of placeAfter(siblings, group.id, null)) {
+ const sibling = state.conversations.find(c => c.id === id)
+ if (sibling) sibling.queueRank = rank
+ }
+}
+
+const isLead = (state: SuperTabState, agent: Agent) => state.conversations.some(c => c.leadId === agent.id)
+const hasWork = (state: SuperTabState, agent: Agent) => state.conversations.some(c => c.assignment?.agentId === agent.id && activeWork(c))
+const freeDisk = (pool: ContributedMachine) => {
+ const resources = pool.observation?.resources
+ return resources?.projectDiskFreeBytes ?? resources?.diskFreeBytes
+}
+
+/** Why [pool] can't keep a crew member of [engine] at [now], or null. Unknown readings never count as trouble. */
+export function crewTrouble(state: SuperTabState, pool: ContributedMachine | null | undefined, engine: string, now: number): CrewTrouble | null {
+ const reading = pool?.observation
+ if (!pool || !reading || now - reading.receivedAt > MOVE_OFFLINE_MS) return 'offline'
+ if (reading.readiness?.engines?.find(e => e.engine === engine)?.signedIn === false) return 'signed-out'
+ const quota = quotaOf(state, pool, engine, now)
+ if (quota?.known && quota.windows.some(w => {
+ if (!(w.usedPercent >= 100)) return false
+ const back = resetMs(w.resetsAt)
+ return back === null || back - now > MOVE_QUOTA_OUT_MS
+ })) return 'quota'
+ const disk = freeDisk(pool)
+ if (typeof disk === 'number' && disk < MOVE_LOW_DISK_BYTES) return 'disk'
+ return null
+}
+
+/** The fallback machines for [agent], best first, in the route's order: the same account kind as the machine it leaves, then
+ * the account whose weekly quota resets soonest, then the most left, then the fewest agents working, then by name. Only
+ * machines with no trouble that admit its start. */
+/** Whether [agent]'s work must stay in Harness Team accounts, as an agent.start's must (audit H2; security review of #3229):
+ * it runs in one, or it is in a Team-written task or step. Its project's chat is its coordinator's and does not count. */
+export function teamOnly(state: SuperTabState, agent: Agent): boolean {
+ const chats = new Set(projectsOf(state).map(p => p.leadConversationId))
+ return onTeamAccount(state, agent) || groupsOf(state, agent).some(g => !chats.has(g.id) && teamWrittenTask(state, g))
+}
+
+export function fallbacks(state: SuperTabState, agent: Agent, now: number, deps: CrewDeps): ContributedMachine[] {
+ const from = standingPool(state, agent.machineId)
+ const team = teamOnly(state, agent), kind = onTeamAccount(state, agent)
+ const engine = agent.configuration.engine
+ const weekly = (pool: ContributedMachine) => quotaOf(state, pool, engine, now)?.windows.find(w => w.name === 'weekly')
+ const working = (pool: ContributedMachine) => state.agents.filter(a => a.machineId === pool.machineId && hasWork(state, a)).length
+ const key = (pool: ContributedMachine): Array<number | string> => {
+ const w = weekly(pool)
+ return [teamAccountPool(pool) === teamAccountPool(from) ? 0 : 1, (w && resetMs(w.resetsAt)) ?? Number.MAX_SAFE_INTEGER,
+ w ? w.usedPercent : 100, working(pool), pool.name]
+ }
+ return (state.machines ?? [])
+ // The same account kind only (security review 2 of #3229): a Team member's work stays in Team accounts, and a main-account
+ // member never brings its own-account work into one. A machine that just refused its start waits.
+ .filter(pool => pool.machineId !== agent.machineId && !pool.buddyOnly && standingPool(state, pool.machineId) && (!team || teamAccountPool(pool))
+ && teamAccountPool(pool) === kind && !(agent.crewMoveRefused?.machineId === pool.machineId && now - agent.crewMoveRefused.at < MOVE_REFUSED_WAIT_MS)
+ && !machineRuleRefusal(deps.machineRules ?? [], pool.machineId, engine, now) && !crewTrouble(state, pool, engine, now) && deps.admits(pool.machineId, agent))
+ .map(pool => ({ pool, key: key(pool) }))
+ .sort((a, b) => { for (let i = 0; i < a.key.length; i++) if (a.key[i] !== b.key[i]) return a.key[i]! < b.key[i]! ? -1 : 1; return 0 })
+ .map(entry => entry.pool)
+}
+
+/** Whether any crew member needs another machine now, so the sweep reads the store's Team facts only then (review of #3229). */
+export function crewNeedsMachine(state: SuperTabState, now: number): boolean {
+ return crewKeep(state, now, { admits: () => true }).some(a => a.kind === 'move')
+}
+
+/** What the keeper does now for the Swarm's crew: one action per member at most, and one low-disk drop per machine. */
+export function crewKeep(state: SuperTabState, now: number, deps: CrewDeps): CrewAction[] {
+ const actions: CrewAction[] = []
+ const members = state.agents.filter(a => isCrewMember(a) && !isBuddy(a) && !isLead(state, a) && !a.movedTo)
+ for (const agent of members) {
+ const trouble = crewTrouble(state, standingPool(state, agent.machineId), agent.configuration.engine, now)
+ // An asleep member, or an awake one with no work on a machine gone offline (a laptop closed for the night), stays where it
+ // is: it holds nothing to move, and a wake or new work moves it then (review of #3229).
+ const stays = !!agent.asleep || (trouble === 'offline' && !heldTasks(state, agent, now).length)
+ if (trouble && !stays) {
+ const to = fallbacks(state, agent, now, deps)[0]
+ if (to) actions.push({ kind: 'move', agentId: agent.id, toMachineId: to.machineId, reason: trouble })
+ else if (agent.crewStuck !== trouble) actions.push({ kind: 'stuck', agentId: agent.id, reason: trouble })
+ continue
+ }
+ if (trouble && !agent.asleep) continue
+ if (agent.asleep) {
+ if (!agent.asleep.folderDropped && now - agent.asleep.since >= FOLDER_ASLEEP_MS) actions.push({ kind: 'dropFolder', agentId: agent.id, why: 'asleep-7d' })
+ continue
+ }
+ // On a Team account the only close deletes the session's own data (inCore/agents.ts ended), so a wake could not resume it
+ // in place: such a member is kept awake until a close that keeps it exists (security review 2 of #3229).
+ if (hasWork(state, agent) || onTeamAccount(state, agent)) continue
+ const since = idleSince(agent, groupsOf(state, agent))
+ if (since !== undefined && now - since >= SLEEP_IDLE_MS) actions.push({ kind: 'sleep', agentId: agent.id })
+ }
+ // A machine short of disk gives back the folder of its member asleep longest first, one a pass, until a reading shows room.
+ for (const pool of state.machines ?? []) {
+ const disk = freeDisk(pool)
+ if (typeof disk !== 'number' || disk >= DISK_FLOOR_BYTES) continue
+ const asleep = members.filter(a => a.machineId === pool.machineId && a.asleep && !a.asleep.folderDropped
+ && !actions.some(x => x.kind === 'dropFolder' && x.agentId === a.id))
+ .sort((a, b) => a.asleep!.since - b.asleep!.since)[0]
+ if (asleep) actions.push({ kind: 'dropFolder', agentId: asleep.id, why: 'low-disk' })
+ }
+ return actions
+}
+
+const ENGINE = (engine: string) => engine === 'claude' ? 'Claude' : engine === 'codex' ? 'Codex' : engine
+const machineName = (state: SuperTabState, machineId: string) => state.machines?.find(m => m.machineId === machineId)?.name ?? 'its machine'
+/** Why a member leaves [from], in words the project chat and its hand-off note use. */
+export function troubleText(state: SuperTabState, agent: Agent, reason: CrewTrouble): string {
+ const from = machineName(state, agent.machineId), engine = ENGINE(agent.configuration.engine)
+ return reason === 'offline' ? `${from} has been offline over 5 minutes`
+ : reason === 'signed-out' ? `${engine} is signed out on ${from}`
+ : reason === 'quota' ? `${from} is out of ${engine} quota for over an hour`
+ : `${from} is low on disk`
+}
+const nameOf = (agent: Agent) => agent.displayName ?? agent.name
+
+/** The start that moves [agent] to [pool], as an agent.start of the same member there: its project's folder, its name, role,
+ * engine, model and effort, in its project's chat. Null when its project is gone. Checked by admitMachineStart before use. */
+export function crewMoveRequest(state: SuperTabState, agent: Agent, pool: ContributedMachine): AgentStartRequest | null {
+ const project = projectsOf(state).find(p => p.id === agentProjectId(state, agent))
+ if (!project || !state.conversations.some(c => c.id === project.leadConversationId)) return null
+ const { engine, model, effort } = agent.configuration
+ return { conversationId: project.leadConversationId, machineId: pool.machineId, engine, ...(model ? { model } : {}), ...(effort ? { effort } : {}),
+ name: agent.name, ...(agent.roleName ? { roleName: agent.roleName } : {}), cwd: projectFolder(pool, project), worktree: true,
+ requirements: { memoryBytes: AGENT_MEMORY_BYTES, diskBytes: 10 * 1024 ** 3, maxCpuPercent: 100, tools: ['git'], devices: [], quotaRequired: false },
+ crewMoveOf: agent.id }
+}
+
+/** The note the moved member reads first (the host shows it in its start row): why it moved, and that its task was handed back. */
+function handoffNote(state: SuperTabState, agent: Agent, reason: CrewTrouble, to: ContributedMachine, now: number): string {
+ const task = heldTasks(state, agent, now)[0]
+ const pr = task?.pullRequestUrls?.at(-1)
+ return [`You are ${nameOf(agent)}, moved from ${machineName(state, agent.machineId)} to ${to.name} because ${troubleText(state, agent, reason)}.`,
+ 'This folder is new: fetch your branch before you go on.',
+ task ? `Your task "${task.title}" went back to the head of the list${pr ? ` (its pull request: ${pr})` : ''}; it comes back to you or to whoever is free first.` : '']
+ .filter(Boolean).join(' ').slice(0, 1000)
+}
+
+/** Its project's chat, written as a system row by the Swarm's owner, as liveness.ts's notices are. */
+function notice(state: SuperTabState, agent: Agent, id: string, text: string): MessageDraft[] {
+ const owner = state.people.find(p => p.id === state.ownerId)
+ const project = projectsOf(state).find(p => p.id === agentProjectId(state, agent))
+ if (!owner || !project || !state.conversations.some(c => c.id === project.leadConversationId)) return []
+ return [{ id, conversationId: project.leadConversationId, replyTo: null, recipients: [],
+ author: { kind: 'person', id: owner.id, name: owner.email, membershipGeneration: owner.generation }, text, source: { kind: 'system' } }]
+}
+
+/**
+ * Applies the keeper to [state] (store.ts, with the Swarm's time checks): sleeps, moves, says a stuck move once, and marks
+ * folders to drop. A move whose start ended without running is forgotten first, so the member can move again. Returns the
+ * project chat rows to post: one per move and per stuck member, none for a sleep.
+ */
+export function applyCrewKeep(state: SuperTabState, now: number, deps: CrewDeps): MessageDraft[] {
+ for (const agent of state.agents) {
+ if (!agent.movedTo) continue
+ const start = state.starts?.find(s => s.id === agent.movedTo!.startId)
+ if (!start || start.error) delete agent.movedTo
+ }
+ const drafts: MessageDraft[] = []
+ for (const action of crewKeep(state, now, deps)) {
+ const agent = state.agents.find(a => a.id === action.agentId)!
+ if (action.kind === 'sleep') agent.asleep = { since: now }
+ else if (action.kind === 'dropFolder') agent.asleep = { ...agent.asleep!, folderDropped: true }
+ else if (action.kind === 'stuck') {
+ agent.crewStuck = action.reason
+ drafts.push(...notice(state, agent, stableId('crew-stuck', agent.id, action.reason, String(now)),
+ `${nameOf(agent)} can't keep working there: ${troubleText(state, agent, action.reason)}, and no other machine can take it now.`))
+ } else {
+ const pool = state.machines!.find(m => m.machineId === action.toMachineId)!
+ const request = crewMoveRequest(state, agent, pool)
+ state.starts ??= []
+ if (!request || state.starts.filter(s => !s.harnessId && !s.error).length >= 32) continue
+ const contributor = state.people.find(p => p.id === pool.contributorId)
+ if (!contributor) continue
+ const id = stableId('crew-move', agent.id, String(now))
+ const start: AgentStart = { poolGeneration: pool.generation, id, creationId: `super-${id}`,
+ actorKey: actorKey({ kind: 'person', userId: contributor.id, email: contributor.email }), contributorId: contributor.id,
+ membershipGeneration: contributor.generation, machineOwnerId: pool.contributorId, hostGrant: null,
+ request: { ...request, handoffNote: handoffNote(state, agent, action.reason, pool, now) }, harnessId: null, dispatched: false, error: null, requestedAt: now }
+ state.starts.push(start)
+ // At the head of the list now, before the old session's loss of it (offline, or its host closing it) can come first.
+ for (const group of heldTasks(state, agent, now)) toHead(state, group)
+ agent.movedTo = { machineId: pool.machineId, startId: id, reason: action.reason, at: now }
+ delete agent.crewStuck
+ drafts.push(...notice(state, agent, stableId('crew-move', agent.id, id),
+ `${nameOf(agent)} is moving to ${pool.name}: ${troubleText(state, agent, action.reason)}. Its task goes back to the head of the list.`))
+ }
+ }
+ return drafts
+}
+
+/** New work for an asleep crew member wakes it (work.assign): its host reopens the session in place. The reopen grace counts
+ * from now (#3397's CREW_REOPEN_GRACE_MS from downSince), so the task is not lost while it reopens. Whether it was asleep. */
+export function wakeCrewMember(agent: Agent, now: number): boolean {
+ if (!agent.asleep) return false
+ delete agent.asleep
+ agent.downSince = new Date(now).toISOString()
+ return true
+}
+
+/** A move's start failed: the member stays where it was, and the keeper may try another machine on its next pass. */
+export function crewMoveFailed(state: SuperTabState, start: AgentStart, now: number): void {
+ const from = start.request.crewMoveOf ? state.agents.find(a => a.id === start.request.crewMoveOf) : undefined
+ if (from?.movedTo?.startId !== start.id) return
+ delete from.movedTo
+ from.crewMoveRefused = { machineId: start.request.machineId, at: now }
+}
+
+/**
+ * A move's new session started as [agentId] (agent.started): it becomes the same member. It takes the old one's crew record,
+ * name and groups, and records where it came from. The old one's active task is lost with reason crew-moved and goes to the
+ * head of its project's Next up, so whoever is free next (or this member) takes it with its pushed branch. Then the old
+ * agent is removed, which closes its session on its host and drops its folder when clean ([remove]: commands.ts removeAgent).
+ */
+export function takeOverCrewMember(state: SuperTabState, start: AgentStart, agentId: string, now: number, remove: (agent: Agent) => void): void {
+ const from = start.request.crewMoveOf ? state.agents.find(a => a.id === start.request.crewMoveOf) : undefined
+ const moved = state.agents.find(a => a.id === agentId)
+ if (!from || !moved || from.movedTo?.startId !== start.id) return
+ const crew = (from as Agent & { crew?: unknown }).crew
+ if (crew) (moved as Agent & { crew?: unknown }).crew = crew
+ moved.displayName = nameOf(from)
+ moved.movedFrom = { agentId: from.id, machineId: from.machineId, reason: from.movedTo.reason, at: now }
+ for (const group of state.conversations) {
+ // Never into Team-written work from the main account (audit H2): the placement keeps such a member in Team accounts,
+ // and this holds even for a start placed otherwise.
+ if (group.agentIds.includes(from.id) && !group.agentIds.includes(moved.id) && !barredFromTeamWritten(state, group, moved)) {
+ // Joined, not marked: since #3198 a mark is permanent, and its old session only belonged here (Curlew3). The barred
+ // check above keeps main-account sessions out; a push read marks real Team code.
+ group.agentIds.push(moved.id)
+ }
+ if (group.assignment?.agentId !== from.id) continue
+ if (activeWork(group)) {
+ group.assignment.status = 'lost'
+ group.assignment.lost = { reason: 'crew-moved', at: now }
+ group.lostAttempt = { id: group.assignment.id, agentId: from.id, machineId: from.machineId, engine: from.configuration.engine }
+ toHead(state, group)
+ } else if (group.assignment.status === 'lost' && (group.assignment.lost?.at ?? 0) >= from.movedTo.at) toHead(state, group)
+ }
+ remove(from)
+}
diff --git a/backend/src/superTabs/crewRules.ts b/backend/src/superTabs/crewRules.ts
new file mode 100644
index 000000000..f96195795
--- /dev/null
+++ b/backend/src/superTabs/crewRules.ts
@@ -0,0 +1,35 @@
+/**
+ * The crew keeper's numbers (task #3398), in one place until the project rules record (Ledger's task) holds them.
+ * Each is a default a project may change there later.
+ */
+const MINUTE = 60_000, HOUR = 60 * MINUTE, DAY = 24 * HOUR, GB = 1024 ** 3
+
+/** A crew member with nothing to do this long sleeps: its session closes, its folder stays. */
+export const SLEEP_IDLE_MS = HOUR
+/** Its machine silent this long: the member moves to a fallback machine. */
+export const MOVE_OFFLINE_MS = 5 * MINUTE
+/** Its engine's quota used up and not back for at least this long: the member moves. */
+export const MOVE_QUOTA_OUT_MS = HOUR
+/** Less free disk than this where it works: the member moves. */
+export const MOVE_LOW_DISK_BYTES = 5 * GB
+/** Asleep this long: its folder is dropped; a wake then starts it fresh. */
+export const FOLDER_ASLEEP_MS = 7 * DAY
+/** A machine with less free disk than this drops asleep members' folders first, the longest asleep first, one a pass. */
+export const DISK_FLOOR_BYTES = 10 * GB
+/** A machine that refused a member's move start (no token, no room) is not picked for that member again this long. */
+export const MOVE_REFUSED_WAIT_MS = HOUR
+
+/** A machine rule the project's owners gave, as the project rules record holds it (Ledger's task): by machine id, each
+ * machine of a Mac and its Harness Team account. Never written in source: people's machines and hours stay out of this
+ * public repository (River, 11-05). Until the record is on main, no machine rule applies to moves. */
+export interface MachineRule {
+ machineIds: string[]
+ /** Why, in the words the project chat shows. */
+ why: string
+ /** Engines no crew member is moved there with. */
+ noEngines?: string[]
+ /** When it is away: on these weekdays (0 Sunday … 6 Saturday), between these local times, in its owner's time zone. */
+ away?: { days: number[]; windows: Array<[string, string]>; timeZone: string }
+}
+/** How long after losing its task to its machine a crew member still counts as holding it, so it moves with it. */
+export const HELD_TASK_MS = HOUR
diff --git a/backend/src/superTabs/hotFilters.test.ts b/backend/src/superTabs/hotFilters.test.ts
index daf956442..c5c7cdc73 100644
--- a/backend/src/superTabs/hotFilters.test.ts
+++ b/backend/src/superTabs/hotFilters.test.ts
@@ -473,5 +473,6 @@ describe('no new Prisma in/has filter on a hot Swarm collection', () => {
expect(added, 'A Prisma in/has filter on a hot Swarm collection is sent as $expr, which no index bounds. Read through a named index (parts/indexed.ts readBy) with a plan test instead. An `unresolved` call is one whose collection this test cannot tell: name the delegate (db.swarmGroup, or a parameter typed Prisma.XDelegate), or list it after checking it is not a hot collection.').toEqual([])
const fewer = Object.entries(allowed).filter(([key, count]) => count > (current[key] ?? 0)).map(([key, count]) => `${key}: ${current[key] ?? 0} (listed ${count})`)
expect(fewer, 'Fewer uses than listed: lower them in hotFilters.allowed.json (UPDATE_HOT_FILTERS=1 rewrites it).').toEqual([])
- })
+ // It parses every file under src/ (about 1.5 s alone), and a CI runner running the whole suite took over the default 5 s.
+ }, 60_000)
})
diff --git a/backend/src/superTabs/model.ts b/backend/src/superTabs/model.ts
index 14bb6ee9b..43c0ce5aa 100644
--- a/backend/src/superTabs/model.ts
+++ b/backend/src/superTabs/model.ts
@@ -40,6 +40,9 @@ export interface AgentConfiguration {
contextWindow: number | null
engineVersion: string | null
}
+/** Why a crew member must leave its machine (crewKeep.ts). */
+export type CrewTrouble = 'offline' | 'signed-out' | 'quota' | 'disk'
+
export interface Agent {
id: string
projectId?: string
@@ -114,6 +117,19 @@ export interface Agent {
/** Its last task was lost (liveness.ts): routing leaves it out, even once that task is cancelled or reassigned, until it
* finishes a new task, a person assigns it work by hand, or its terminal is replaced (commands.ts). */
lostTask?: { conversationId: string; assignmentId: string; reason: string; at: number; resumeAt?: string }
+ /** A crew member the Swarm put to sleep after an idle hour (crewKeep.ts): its host closes the session and keeps its folder;
+ * work.assign clears it and the host reopens it in place. `folderDropped` once a cleanup rule removed its folder, so a
+ * wake starts it fresh. Set only by the server. */
+ asleep?: { since: number; folderDropped?: boolean }
+ /** A crew member being moved off a failing machine (crewKeep.ts): its start on [machineId] takes over under the same name;
+ * until then nothing is assigned to it and its old host closes the session. Set only by the server. */
+ movedTo?: { machineId: string; startId: string; reason: CrewTrouble; at: number }
+ /** The agent this crew member took over from on a move, so its history reads as one member. */
+ movedFrom?: { agentId: string; machineId: string; reason: CrewTrouble; at: number }
+ /** The machine whose start of this member's move failed, and when: not picked again for MOVE_REFUSED_WAIT_MS (crewKeep.ts). */
+ crewMoveRefused?: { machineId: string; at: number }
+ /** A move needed but no machine could take it: said once in the project chat until the trouble changes. */
+ crewStuck?: CrewTrouble
/** A usage limit seen on its terminal; routing skips the agent until then. Shown as null once passed (terminal.ts). */
limitedUntil?: string | null
/** Terminal alert episodes in progress, so each one posts a single row (terminal.ts). */
@@ -186,6 +202,8 @@ export interface AgentStartRequest {
buddyProjects?: Array<{ id: string; name: string; cwd: string }>
/** The offered account selected for a destination PM switch. */
profileId?: string
+ /** A crew member's move (crewKeep.ts): the agent its start takes over from once it runs. Written by the server alone. */
+ crewMoveOf?: string
/** A bounded handoff note transferred from the source host, visible only in the destination host's start row. */
handoffNote?: string
handoffSourceAgentId?: string
diff --git a/backend/src/superTabs/parts/indexed.ts b/backend/src/superTabs/parts/indexed.ts
index 8b75b1b14..efa843278 100644
--- a/backend/src/superTabs/parts/indexed.ts
+++ b/backend/src/superTabs/parts/indexed.ts
@@ -18,6 +18,8 @@ export const GROUP_INDEX = {
agents: { tabId: 1, agentIds: 1 },
project: { tabId: 1, projectId: 1, dormant: 1, lastActivityAt: 1, key: 1 },
dormancy: { tabId: 1, dormant: 1, changedAt: 1 },
+ /** Groups active since a time, in every Swarm: the scorecard's projects with a day to count (scorecard/job.ts). */
+ activity: { dormant: 1, lastActivityAt: 1 },
/** A command's awake conversations by the columns a held part is compared by (parts/held.ts): the listing reads no document. */
held: { tabId: 1, dormant: 1, deleted: 1, key: 1, version: 1, changedAt: 1 },
changes: { tabId: 1, changedAt: 1 },
@@ -75,6 +77,8 @@ export const NAMED_INDEX = {
SuperTabOpenProposal: { open: { tabId: 1, conversationId: 1, sequence: 1 }, id: { _id: 1 } },
/** A Swarm's newest route decisions: the route-decision list. */
SuperTabRouteDecision: { recent: { tabId: 1, createdAt: 1 } },
+ /** A project's scorecard days (scorecard/rows.ts). */
+ SuperTabScorecard: { day: { tabId: 1, projectId: 1, on: 1 } },
SuperTabDelivery: {
/** An agent's deliveries by status and age: its open ones, oldest first. */
agentOpen: { tabId: 1, agentId: 1, status: 1, createdAt: 1 },
diff --git a/backend/src/superTabs/purge.ts b/backend/src/superTabs/purge.ts
index ee3fc0612..2e21ce44e 100644
--- a/backend/src/superTabs/purge.ts
+++ b/backend/src/superTabs/purge.ts
@@ -22,7 +22,7 @@ export const PURGED_BY_TAB_ID = ['superTabMessage', 'superTabMention', 'superTab
'superTabRun', 'superTabAttempt', 'superTabRouteDecision', 'superTabModelEvidence', 'superTabModelRecent', 'superTabStopReceipt',
'superTabRoleText', 'superTabInstructions', 'superTabInstructionsRevision', 'superTabProjectRules', 'superTabPmNote', 'superTabConfiguration', 'superTabTerminalRequest',
'gitHubRepositoryApproval', 'gitHubApprovalRefusal', 'swarmHead', 'swarmMember', 'swarmGroup', 'swarmAgent', 'swarmMachine', 'swarmStart',
- 'swarmProgress'] as const
+ 'swarmProgress', 'superTabScorecard'] as const
/** Removes one stored object; one already gone counts as removed. */
export interface FileRemover { delete(objectKey: string): Promise<void> }
diff --git a/backend/src/superTabs/read/service.ts b/backend/src/superTabs/read/service.ts
index be4935f8d..c9525b863 100644
--- a/backend/src/superTabs/read/service.ts
+++ b/backend/src/superTabs/read/service.ts
@@ -32,6 +32,8 @@ import { profileNames, readableWeek, recentDormantGroups } from '../leaderboard/
import { LEDGER_WEEKS } from '../leaderboard/record.js'
import type { PeriodName } from '../leaderboard/rolling.js'
import { leaderboard } from '../leaderboard/stats.js'
+import { readScorecards, type ScorecardDay } from '../scorecard/rows.js'
+import { scorecardThrough } from '../scorecard/job.js'
import { WEEK_MS } from '../leaderboard/week.js'
import { derivedEtag, marksOf } from './derivedEtag.js'
import { READ_FORMAT, SWARM_MIGRATING, type Changes, type Derived, type ReaderMarks, type MachineReadings, type DormantGroups, type FirstLoad, type OneGroup, type SwarmList } from './contract.js'
@@ -520,6 +522,20 @@ export class SwarmReads {
})
}
+ /** The stored scorecard days (scorecard/rows.ts) of one project, or of every project the reader reads (`allProjects`),
+ * newest first. Only projects the reader may read; a Team session reads none, as for the leaderboard. */
+ async scorecard(user: AuthUser, tabId: string, projectId: string, allProjects = false): Promise<{ days: ScorecardDay[]; through: string }> {
+ if (isTeamSession(user)) fail(TEAM_SESSION_LIMITED, TEAM_SESSION_MESSAGE, 403)
+ return this.read(user, tabId, async (tx, cached) => {
+ const state = parts.reassemble(await readParts(tx, cached, tabId), { share: true })
+ const actor = await this.store.actor(tx, user, state)
+ const project = readProject(state, actor, projectId)
+ const projects = allProjects ? projectsOf(state).filter(p => canReadProject(state, actor, p)).map(p => p.id) : [projectId]
+ // The asked project's last day, so a day missing at the end shows as missing, not as a shorter table.
+ return { days: await readScorecards(tx, tabId, projects), through: scorecardThrough(project, this.now()) }
+ })
+ }
+
/** The waiting tags of each listed Swarm, as `/:tabId/mentions` and a Swarm's own state count them (readable conversations
* only), so Desktop reads one list instead of one mentions call per Swarm (roadmap 2.5). One match over the page's
* Swarms (swarmsWithTags) finds those with any tag at all; only those load their state to check what the person may still read. A Swarm
diff --git a/backend/src/superTabs/reviewSeats.ts b/backend/src/superTabs/reviewSeats.ts
index 74b0babba..823c234c3 100644
--- a/backend/src/superTabs/reviewSeats.ts
+++ b/backend/src/superTabs/reviewSeats.ts
@@ -24,14 +24,14 @@ export type Standing = typeof STANDING[number]
export const canStart = (s: Standing) => s === 'usable' || s === 'close to running out'
/** The engine's quota reading for [pool]: its own, or for a Team account that spends its Mac's subscription, the Mac's. */
-function quotaOf(state: SuperTabState, pool: ContributedMachine, engine: string, now: number) {
+export function quotaOf(state: SuperTabState, pool: ContributedMachine, engine: string, now: number) {
const own = pool.observation?.quotas.find(q => q.engine === engine)
if (own?.known || !pool.hostMachineId || pool.observation?.quotaReasons?.[engine] !== RELAYED_QUOTA) return own
const mac = state.machines?.find(m => m.machineId === pool.hostMachineId && m.contributorId === pool.contributorId)
return mac?.observation && now - mac.observation.receivedAt <= MACHINE_FRESH_MS ? mac.observation.quotas.find(q => q.engine === engine) : own
}
-function resetMs(value: string | number | null): number | null {
+export function resetMs(value: string | number | null): number | null {
if (typeof value === 'number') return value > 1e10 ? value : value * 1000
if (typeof value === 'string') { const ms = Date.parse(value); return Number.isFinite(ms) ? ms : null }
return null
diff --git a/backend/src/superTabs/scorecard/figures.test.ts b/backend/src/superTabs/scorecard/figures.test.ts
new file mode 100644
index 000000000..c81217933
--- /dev/null
+++ b/backend/src/superTabs/scorecard/figures.test.ts
@@ -0,0 +1,202 @@
+import { describe, expect, it } from 'vitest'
+import { dayBounds, scorecardDay, SCORECARD_LIMITS, type ScorecardInput, type ScorecardMessage, type ScorecardTask } from './figures.js'
+
+const MIN = 60_000
+const HOUR = 60 * MIN
+/** 2026-11-05 in Vietnam (UTC+7): 11-04 17:00 UTC to 11-05 17:00 UTC. */
+const START = Date.UTC(2026, 10, 4, 17)
+const END = START + 24 * HOUR
+const at = (hours: number, minutes = 0) => START + hours * HOUR + minutes * MIN
+
+let seq = 0
+const row = (over: Partial<ScorecardMessage> & Pick<ScorecardMessage, 'conversationId' | 'createdAt'>): ScorecardMessage => ({
+ id: `m${++seq}`, author: { kind: 'agent', id: 'pm' }, source: null, text: 'A real answer with substance.', replyTo: null, thread: null, ...over })
+const person = (conversationId: string, createdAt: number, over: Partial<ScorecardMessage> = {}) =>
+ row({ conversationId, createdAt, author: { kind: 'person', id: 'dee' }, text: 'Please build the scorecard.', ...over })
+const status = (conversationId: string, createdAt: number, value: string) =>
+ row({ conversationId, createdAt, author: { kind: 'agent', id: 'eng' }, source: { kind: 'system', status: value }, text: value })
+const lost = (conversationId: string, createdAt: number) =>
+ row({ conversationId, createdAt, source: { kind: 'work-lost', agentId: 'eng', assignmentId: 'a1', reason: 'no progress' }, text: 'lost' })
+const task = (id: string, over: Partial<ScorecardTask> = {}): ScorecardTask => ({ id, runs: [], requestMessageIds: [], ...over })
+
+const input = (over: Partial<ScorecardInput> = {}): ScorecardInput => ({ start: START, end: END, chatId: 'chat', pmAgentIds: ['pm'], tasks: [],
+ messages: [], answeredDeliveries: [], starts: [], routeDecisions: [], ...over })
+
+describe('dayBounds', () => {
+ it('gives the local day in the project time zone', () => {
+ expect(dayBounds('2026-11-05', 'Asia/Ho_Chi_Minh')).toEqual({ start: START, end: END })
+ })
+ it('gives a 25-hour day where the clock falls back', () => {
+ const { start, end } = dayBounds('2026-11-01', 'America/New_York')
+ expect(start).toBe(Date.UTC(2026, 10, 1, 4))
+ expect(end - start).toBe(25 * HOUR)
+ })
+})
+
+describe('scorecardDay', () => {
+ it('records the limits it measured with', () => {
+ expect(scorecardDay(input()).limits).toEqual(SCORECARD_LIMITS)
+ })
+
+ it('counts asks shipped in the day and their ask-to-ship minutes', () => {
+ const tasks = [
+ task('t1', { number: 1, askedAt: at(1), shippedAt: at(3) }),
+ task('t2', { number: 2, askedAt: at(-10), shippedAt: at(5) }),
+ task('t3', { number: 3, askedAt: at(2), shippedAt: at(4) }),
+ // Shipped the day before, a step, and work with no number: none is an ask shipped today.
+ task('t4', { number: 4, askedAt: at(-30), shippedAt: at(-1) }),
+ task('s1', { number: 5, reviewOf: 't1', askedAt: at(1), shippedAt: at(2) }),
+ task('x', { askedAt: at(1), shippedAt: at(2) }),
+ ]
+ const day = scorecardDay(input({ tasks }))
+ expect(day.asksShipped).toBe(3)
+ expect(day.askToShipMedianMinutes).toBe(120)
+ expect(day.askToShipMaxMinutes).toBe(15 * 60)
+ expect(scorecardDay(input()).askToShipMedianMinutes).toBeNull()
+ })
+
+ it('finds the asks still unanswered at day end and the oldest one', () => {
+ const replied = person('chat', at(1))
+ const threaded = person('chat', at(2))
+ const delivered = person('chat', at(3))
+ const tasked = person('chat', at(4))
+ const open = person('chat', at(6))
+ const older = person('chat', at(-30))
+ const tooOld = person('chat', END - 8 * 24 * HOUR)
+ const lateAnswer = person('chat', at(20))
+ const elsewhere = person('t1', at(1))
+ const answers = [
+ row({ conversationId: 'chat', createdAt: at(1, 5), replyTo: replied.id, thread: replied.id }),
+ row({ conversationId: 'chat', createdAt: at(2, 5), thread: threaded.id }),
+ // A person's own follow-up in the thread is no answer; nor is an answer after day end.
+ person('chat', at(6, 5), { thread: open.id, replyTo: open.id }),
+ row({ conversationId: 'chat', createdAt: END + MIN, replyTo: lateAnswer.id, thread: lateAnswer.id }),
+ ]
+ const day = scorecardDay(input({
+ messages: [replied, threaded, delivered, tasked, open, older, tooOld, lateAnswer, elsewhere, ...answers],
+ answeredDeliveries: [{ messageId: delivered.id, answeredAt: at(3, 10) }],
+ tasks: [task('t9', { number: 9, askedAt: at(4, 1), requestMessageIds: [tasked.id] })],
+ }))
+ expect(day.unansweredAsks).toBe(3)
+ expect(day.oldestUnansweredAskMinutes).toBe((END - at(-30)) / MIN)
+ })
+
+ it('counts a person\'s Buddy proposal as an ask, answered once a task is opened from it', () => {
+ const proposal = (createdAt: number) => person('chat', createdAt, { source: { kind: 'buddy-proposal', personId: 'dee', buddyId: 'b', ownerMessageId: 'o' } })
+ const open = proposal(at(2)), built = proposal(at(3))
+ // A task card's line is the person's too, and asks nothing.
+ const line = person('chat', at(4), { source: { kind: 'task-line', taskId: 't9', number: 9, title: 'Scorecard' } })
+ const day = scorecardDay(input({ messages: [open, built, line], tasks: [task('t9', { number: 9, askedAt: at(3, 5), requestMessageIds: [built.id] })] }))
+ expect(day.unansweredAsks).toBe(1)
+ expect(day.oldestUnansweredAskMinutes).toBe((END - at(2)) / MIN)
+ })
+
+ it('counts minutes with nobody working while open work waits', () => {
+ const tasks = [
+ // Waits from 01:00, assigned (working) at 02:00, done at 05:00.
+ task('t1', { number: 1, askedAt: at(1), runs: [at(2)] }),
+ // Working since yesterday; lost at 03:00, picked up again at 03:20.
+ task('t2', { number: 2, askedAt: at(-20), runs: [at(-19), at(3, 20)] }),
+ // Opened at 06:00 and never assigned: waits to day end.
+ task('t3', { number: 3, askedAt: at(6) }),
+ // Waits on a person: not work the Swarm can do.
+ task('t4', { number: 4, askedAt: at(-5), runs: [at(-4)] }),
+ ]
+ const messages = [
+ status('t1', at(5), 'done'),
+ status('t2', at(-19), 'working'),
+ lost('t2', at(3)),
+ status('t2', at(4), 'done'),
+ status('t4', at(-3), 'needs_human'),
+ ]
+ const day = scorecardDay(input({ tasks, messages }))
+ // 01:00-02:00 t2 works (no idle); 03:00-03:20 t1 works; 04:00-05:00 t1 works; 06:00-24:00 nobody works while t3 waits.
+ expect(day.idleMinutes).toBe(18 * 60)
+ })
+
+ it('closes a step when its task closes, working or waiting (review of #3212, 1)', () => {
+ const tasks = [
+ // Cancelled at 03:00 with its review step waiting for a Reviewer since 01:00: the step's wait ends at the cancel.
+ task('t1', { number: 1, askedAt: at(-5) }),
+ task('s1', { number: 2, reviewOf: 't1', askedAt: at(1) }),
+ // In review since yesterday; its step is worked from 10:00 and the task is cancelled at 11:00: the step stops Working then.
+ task('t2', { number: 3, askedAt: at(-5), runs: [at(-4)] }),
+ task('s2', { number: 4, reviewOf: 't2', askedAt: at(10), runs: [at(10)] }),
+ // Waits from 12:00 until it is assigned at 13:00, done at 14:00.
+ task('t3', { number: 5, askedAt: at(12), runs: [at(13)] }),
+ ]
+ const messages = [status('t1', at(-4), 'needs_human'), status('t1', at(3), 'cancelled'), status('t2', at(-3), 'review'),
+ status('t2', at(11), 'cancelled'), status('t3', at(14), 'done')]
+ // 01:00-03:00 s1 waits and nobody works; 12:00-13:00 t3 waits and nobody works. Left open, s1 would wait all day and s2's
+ // Working would hide t3's wait: 540.
+ expect(scorecardDay(input({ tasks, messages })).idleMinutes).toBe(2 * 60 + 60)
+ })
+
+ it('counts lost tasks left alone longer than the limit', () => {
+ const tasks = [task('t1', { number: 1, askedAt: at(-2), runs: [at(-1), at(2, 5)] }), task('t2', { number: 2, askedAt: at(-2), runs: [at(-1)] }),
+ task('t3', { number: 3, askedAt: at(-2), runs: [at(-1)] })]
+ const messages = [
+ lost('t1', at(2)), // picked up after 5 minutes
+ lost('t2', at(4)), // a start after 40 minutes
+ lost('t2', at(4)), // the same loss's row to the agent: one loss
+ lost('t3', at(23)), // nothing until day end, 60 minutes
+ ]
+ const day = scorecardDay(input({ tasks, messages, starts: [{ conversationId: 't2', requestedAt: at(4, 40), failed: false }] }))
+ expect(day.lostLeftAlone).toBe(2)
+ expect(day.lostLeftMinutes).toBe(30 + 50)
+ })
+
+ it('counts merged work that needed a fix', () => {
+ const tasks = [task('t1', { fixedAfterMergeAt: at(3) }), task('t2', { fixedAfterMergeAt: at(-3) }), task('t3')]
+ expect(scorecardDay(input({ tasks })).fixesAfterMerge).toBe(1)
+ })
+
+ it('counts people correcting an earlier PM answer', () => {
+ const pmAnswer = row({ conversationId: 'chat', createdAt: at(-5) })
+ const engineerPost = row({ conversationId: 't1', createdAt: at(1), author: { kind: 'agent', id: 'eng' } })
+ const messages = [pmAnswer, engineerPost,
+ person('chat', at(2), { replyTo: pmAnswer.id, text: 'No, I asked for daily.' }),
+ person('t1', at(3), { replyTo: engineerPost.id }),
+ person('chat', at(-1), { replyTo: pmAnswer.id })]
+ expect(scorecardDay(input({ messages })).corrections).toBe(1)
+ })
+
+ it('does not count a person answering what the PM asked them, nor replies to rows the Swarm writes as the PM (review of #3212, 2)', () => {
+ const question = row({ conversationId: 'chat', createdAt: at(1), text: '@dee which day should it start?', mentions: ['dee'] })
+ const taskLine = row({ conversationId: 'chat', createdAt: at(1), source: { kind: 'system', ask: 'm1' }, text: 'Opened #9' })
+ const answer = row({ conversationId: 'chat', createdAt: at(1) })
+ const messages = [question, taskLine, answer,
+ person('chat', at(2), { replyTo: question.id, text: 'Monday.' }),
+ person('chat', at(2), { replyTo: taskLine.id, text: 'Thanks' }),
+ // Someone else the question did not tag, correcting it: a correction.
+ person('chat', at(3), { author: { kind: 'person', id: 'ken' }, replyTo: question.id, text: 'No, start today.' }),
+ person('chat', at(3), { replyTo: answer.id, text: 'That is not what I asked.' })]
+ expect(scorecardDay(input({ messages })).corrections).toBe(2)
+ })
+
+ it('counts starts, failed starts and route decisions in the day', () => {
+ const day = scorecardDay(input({
+ starts: [{ conversationId: 't1', requestedAt: at(1), failed: false }, { conversationId: 't1', requestedAt: at(2), failed: true },
+ { conversationId: 't1', requestedAt: at(-1), failed: true }],
+ routeDecisions: [{ conversationId: 't1', createdAt: at(1) }, { conversationId: 't2', createdAt: END }],
+ }))
+ expect([day.starts, day.startsFailed, day.routeDecisions]).toEqual([2, 1, 1])
+ })
+
+ it('counts messages typed into the PM and agents\' acknowledgement-only posts', () => {
+ const typed = (agentId: string, createdAt: number) => row({ conversationId: 'chat', createdAt, author: { kind: 'person', id: 'dee' },
+ source: { kind: 'terminal-input', agentId, requestId: `r${createdAt}` }, text: 'dee typed 1 in River\'s terminal' })
+ const messages = [typed('pm', at(1)), typed('pm', at(2)), typed('eng', at(3)), typed('pm', at(-1)),
+ row({ conversationId: 't1', createdAt: at(1), text: 'On it.' }),
+ row({ conversationId: 't1', createdAt: at(1), text: 'Confirmed!' }),
+ // The same rule as the server's guard (acknowledgements.ts): an emoji alone says nothing, but it is no acknowledgement post.
+ row({ conversationId: 't1', createdAt: at(1), text: '👀' }),
+ row({ conversationId: 't1', createdAt: at(1), text: 'On it: the route needs a machine with a phone, so I picked Tony\'s Mac.' }),
+ row({ conversationId: 't1', createdAt: at(1), text: 'Done', source: { kind: 'system', status: 'done' } }),
+ person('t1', at(1), { text: 'thanks' })]
+ const day = scorecardDay(input({ messages }))
+ expect(day.typedIntoPm).toBe(2)
+ expect(day.ackPosts).toBe(2)
+ expect(day.agentPosts).toBe(4)
+ })
+})
diff --git a/backend/src/superTabs/scorecard/figures.ts b/backend/src/superTabs/scorecard/figures.ts
new file mode 100644
index 000000000..3e4413b62
--- /dev/null
+++ b/backend/src/superTabs/scorecard/figures.ts
@@ -0,0 +1,257 @@
+/**
+ * A project's scorecard for one local day (task #3386, Dee: judge each change by numbers, daily), from the Swarm's own
+ * records: the reader (records.ts) gathers them, this turns them into counts and minutes. Pure: no clock, no database.
+ */
+import { isAcknowledgement } from '../acknowledgements.js'
+
+/** What the figures measure against, written into each row with them so an old row keeps its meaning. */
+export const SCORECARD_LIMITS = { lostAfterMinutes: 10, askLookbackDays: 7 } as const
+export type ScorecardLimits = { lostAfterMinutes: number; askLookbackDays: number }
+
+/** A message row as the scorecard reads it. `source` null: an author's own post; set: a row the system wrote. */
+export interface ScorecardMessage {
+ id: string
+ conversationId: string
+ author: { kind: 'person' | 'agent'; id: string }
+ source: Record<string, unknown> | null
+ text: string
+ replyTo: string | null
+ thread: string | null
+ createdAt: number
+ /** People the post tagged. */
+ mentions?: string[]
+}
+
+/** A group of the project other than its chat: a task or a step (`reviewOf`). */
+export interface ScorecardTask {
+ id: string
+ number?: number
+ reviewOf?: string
+ askedAt?: number
+ shippedAt?: number
+ fixedAfterMergeAt?: number
+ /** When each attempt was assigned (ledger runs): it is Working from then. */
+ runs: number[]
+ /** The project chat messages it was opened from: its task line's and its proposal's. */
+ requestMessageIds: string[]
+}
+
+export interface ScorecardInput {
+ start: number
+ end: number
+ chatId: string
+ /** The project's PM: the project chat's coordinator. */
+ pmAgentIds: string[]
+ tasks: ScorecardTask[]
+ /** The day's rows of the project's conversations, the chat's rows of the ask lookback, each task's last status row before
+ * the day, and the rows people replied to. A row given twice counts once. */
+ messages: ScorecardMessage[]
+ answeredDeliveries: Array<{ messageId: string; answeredAt: number }>
+ starts: Array<{ conversationId: string; requestedAt: number; failed: boolean }>
+ routeDecisions: Array<{ conversationId: string; createdAt: number }>
+ limits?: ScorecardLimits
+}
+
+export interface ScorecardFigures {
+ limits: ScorecardLimits
+ asksShipped: number
+ askToShipMedianMinutes: number | null
+ askToShipMaxMinutes: number | null
+ unansweredAsks: number
+ oldestUnansweredAskMinutes: number | null
+ idleMinutes: number
+ lostLeftAlone: number
+ lostLeftMinutes: number
+ fixesAfterMerge: number
+ corrections: number
+ starts: number
+ startsFailed: number
+ routeDecisions: number
+ typedIntoPm: number
+ ackPosts: number
+ agentPosts: number
+}
+
+const MIN = 60_000
+const DAY = 24 * 60 * MIN
+
+/** UTC milliseconds of [day]'s local midnight and the next one in [timeZone] (a DST day is 23 or 25 hours). */
+export function dayBounds(day: string, timeZone: string): { start: number; end: number } {
+ const [y, m, d] = day.split('-').map(Number) as [number, number, number]
+ return { start: localMidnight(Date.UTC(y, m - 1, d), timeZone), end: localMidnight(Date.UTC(y, m - 1, d + 1), timeZone) }
+}
+
+function localMidnight(wall: number, timeZone: string): number {
+ const first = wall - offset(wall, timeZone)
+ return wall - offset(first, timeZone)
+}
+
+/** How far [timeZone]'s wall clock is ahead of UTC at [at]. */
+function offset(at: number, timeZone: string): number {
+ const parts = Object.fromEntries(new Intl.DateTimeFormat('en-CA', { timeZone, year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit',
+ minute: '2-digit', second: '2-digit', hourCycle: 'h23' }).formatToParts(at).map(p => [p.type, Number(p.value)]))
+ return Date.UTC(parts.year!, parts.month! - 1, parts.day!, parts.hour!, parts.minute!, parts.second!) - Math.floor(at / 1000) * 1000
+}
+
+type Phase = 'working' | 'waiting' | 'other' | 'closed'
+const PHASE_OF_STATUS: Record<string, Phase> = { working: 'working', planned: 'waiting', lost: 'waiting', review: 'other', reconciling: 'other',
+ needs_human: 'other', blocked: 'other', done: 'closed', stopped: 'closed', cancelled: 'closed' }
+
+/** What a row says about its task's phase, or null. */
+function phaseOf(row: ScorecardMessage): Phase | null {
+ const kind = row.source?.kind
+ if (kind === 'system' && typeof row.source!.status === 'string') return PHASE_OF_STATUS[row.source!.status as string] ?? null
+ if (kind === 'work-lost') return 'waiting'
+ if (kind === 'work-resumed') return 'working'
+ return null
+}
+
+/** Rows that move a task on: a status, a loss or resume, a route. */
+const handles = (row: ScorecardMessage): boolean => phaseOf(row) !== null || row.source?.kind === 'route' || row.source?.kind === 'route-override'
+
+/** A person's own post, or the proposal their Buddy wrote for them: what a person asks the project. A task card's line is
+ * the person's too, and asks nothing. */
+export const isAsk = (m: Pick<ScorecardMessage, 'author' | 'source'>): boolean =>
+ m.author.kind === 'person' && (!m.source || m.source.kind === 'buddy-proposal')
+
+const median = (values: number[]): number | null => {
+ if (!values.length) return null
+ const sorted = [...values].sort((a, b) => a - b), mid = sorted.length >> 1
+ return sorted.length % 2 ? sorted[mid]! : (sorted[mid - 1]! + sorted[mid]!) / 2
+}
+const minutes = (ms: number) => Math.round(ms / MIN)
+
+export function scorecardDay(input: ScorecardInput): ScorecardFigures {
+ const { start, end } = input
+ const limits = input.limits ?? { ...SCORECARD_LIMITS }
+ const inDay = (t: number) => t >= start && t < end
+ const messages = [...new Map(input.messages.map(m => [m.id, m])).values()].sort((a, b) => a.createdAt - b.createdAt)
+ const byId = new Map(messages.map(m => [m.id, m]))
+ const pm = new Set(input.pmAgentIds)
+ const dayRows = messages.filter(m => inDay(m.createdAt))
+ const asks = input.tasks.filter(t => t.number !== undefined && !t.reviewOf)
+
+ const shipped = asks.filter(t => t.shippedAt !== undefined && inDay(t.shippedAt))
+ const shipTimes = shipped.filter(t => t.askedAt !== undefined).map(t => minutes(t.shippedAt! - t.askedAt!))
+
+ const unanswered = unansweredAsks(input, messages, limits)
+ const lostLeft = lostLeftAlone(input, messages, limits)
+ const ownPosts = dayRows.filter(m => m.author.kind === 'agent' && !m.source)
+
+ return {
+ limits,
+ asksShipped: shipped.length,
+ askToShipMedianMinutes: median(shipTimes),
+ askToShipMaxMinutes: shipTimes.length ? Math.max(...shipTimes) : null,
+ unansweredAsks: unanswered.length,
+ oldestUnansweredAskMinutes: unanswered.length ? minutes(end - Math.min(...unanswered.map(m => m.createdAt))) : null,
+ idleMinutes: minutes(idleMs(input, messages)),
+ lostLeftAlone: lostLeft.length,
+ lostLeftMinutes: minutes(lostLeft.reduce((sum, ms) => sum + ms, 0)),
+ fixesAfterMerge: input.tasks.filter(t => t.fixedAfterMergeAt !== undefined && inDay(t.fixedAfterMergeAt)).length,
+ // A reply to the PM's own post (not a row the Swarm writes in its name) that the post did not ask of that person.
+ corrections: dayRows.filter(m => {
+ if (m.author.kind !== 'person' || m.source || !m.replyTo) return false
+ const target = byId.get(m.replyTo)
+ return !!target && target.author.kind === 'agent' && pm.has(target.author.id) && !target.source && !target.mentions?.includes(m.author.id)
+ }).length,
+ starts: input.starts.filter(s => inDay(s.requestedAt)).length,
+ startsFailed: input.starts.filter(s => s.failed && inDay(s.requestedAt)).length,
+ routeDecisions: input.routeDecisions.filter(d => inDay(d.createdAt)).length,
+ typedIntoPm: dayRows.filter(m => m.source?.kind === 'terminal-input' && pm.has(m.source.agentId as string)).length,
+ ackPosts: ownPosts.filter(m => isAcknowledgement(m.text)).length,
+ agentPosts: ownPosts.length,
+ }
+}
+
+/** People's top-level asks in the project chat in the lookback (isAsk) that nothing answered by day end: no agent's row in their
+ * thread or replying to them, no delivery answered, no task opened from them. */
+function unansweredAsks(input: ScorecardInput, messages: ScorecardMessage[], limits: ScorecardLimits): ScorecardMessage[] {
+ const { end } = input
+ const answered = new Set<string>()
+ for (const m of messages) {
+ if (m.author.kind !== 'agent' || m.createdAt >= end) continue
+ if (m.replyTo) answered.add(m.replyTo)
+ if (m.thread && m.thread !== m.id) answered.add(m.thread)
+ }
+ for (const d of input.answeredDeliveries) if (d.answeredAt < end) answered.add(d.messageId)
+ for (const t of input.tasks) if (t.askedAt === undefined || t.askedAt < end) t.requestMessageIds.forEach(id => answered.add(id))
+ const from = end - limits.askLookbackDays * DAY
+ return messages.filter(m => m.conversationId === input.chatId && isAsk(m) && !m.replyTo
+ && (!m.thread || m.thread === m.id) && m.createdAt >= from && m.createdAt < end && !answered.has(m.id))
+}
+
+/** Each task's phase changes in time order: opened (waiting), assigned (working), and what its rows say. */
+function timelines(input: ScorecardInput, messages: ScorecardMessage[]): Map<string, Array<{ at: number; phase: Phase }>> {
+ const lines = new Map<string, Array<{ at: number; phase: Phase }>>()
+ for (const t of input.tasks) {
+ lines.set(t.id, [...t.askedAt !== undefined ? [{ at: t.askedAt, phase: 'waiting' as const }] : [], ...t.runs.map(at => ({ at, phase: 'working' as const }))])
+ }
+ for (const m of messages) {
+ const phase = phaseOf(m)
+ if (phase) lines.get(m.conversationId)?.push({ at: m.createdAt, phase })
+ }
+ for (const line of lines.values()) line.sort((a, b) => a.at - b.at)
+ // A step ends with its task: a cancel or archive leaves an unassigned step as it was, and an assigned one's row says no status.
+ for (const t of input.tasks) {
+ // Its task's last word, when that closed it: a task done and then reopened has not ended its steps.
+ const last = t.reviewOf ? lines.get(t.reviewOf)?.at(-1) : undefined
+ const closed = last?.phase === 'closed' ? last : undefined
+ if (closed) {
+ const line = lines.get(t.id)!
+ line.push({ at: closed.at, phase: 'closed' })
+ line.sort((a, b) => a.at - b.at)
+ lines.set(t.id, line.filter(e => e.at <= closed.at))
+ }
+ }
+ return lines
+}
+
+/** Time in the day with no task or step Working while one waits for an agent (unassigned, planned or lost). */
+function idleMs(input: ScorecardInput, messages: ScorecardMessage[]): number {
+ const { start, end } = input
+ const deltas = new Map<number, { working: number; waiting: number }>()
+ const add = (at: number, phase: Phase, by: number) => {
+ if (phase !== 'working' && phase !== 'waiting') return
+ const d = deltas.get(at) ?? { working: 0, waiting: 0 }
+ d[phase] += by
+ deltas.set(at, d)
+ }
+ for (const line of timelines(input, messages).values()) {
+ for (let i = 0; i < line.length; i++) {
+ const from = Math.max(line[i]!.at, start), to = Math.min(line[i + 1]?.at ?? end, end)
+ if (from >= to) continue
+ add(from, line[i]!.phase, 1)
+ add(to, line[i]!.phase, -1)
+ }
+ }
+ let idle = 0, working = 0, waiting = 0, last = start
+ for (const at of [...deltas.keys()].sort((a, b) => a - b)) {
+ if (working === 0 && waiting > 0) idle += at - last
+ const d = deltas.get(at)!
+ working += d.working
+ waiting += d.waiting
+ last = at
+ }
+ return idle
+}
+
+/** For each task lost in the day and left alone past the limit, the time past it: until its next assignment, status,
+ * route or start, or the day's end. */
+function lostLeftAlone(input: ScorecardInput, messages: ScorecardMessage[], limits: ScorecardLimits): number[] {
+ const { start, end } = input
+ const limit = limits.lostAfterMinutes * MIN
+ const next = new Map<string, number[]>()
+ const note = (id: string, at: number) => next.set(id, [...next.get(id) ?? [], at])
+ for (const t of input.tasks) t.runs.forEach(at => note(t.id, at))
+ for (const s of input.starts) note(s.conversationId, s.requestedAt)
+ for (const m of messages) if (handles(m) && m.source?.kind !== 'work-lost') note(m.conversationId, m.createdAt)
+ // One loss writes a row for the PM and one for the agent.
+ const losses = new Map(messages.filter(m => m.source?.kind === 'work-lost' && m.createdAt >= start && m.createdAt < end)
+ .map(m => [JSON.stringify([m.conversationId, m.source!.assignmentId, m.createdAt]), m]))
+ return [...losses.values()].flatMap(m => {
+ const after = (next.get(m.conversationId) ?? []).filter(at => at > m.createdAt)
+ const left = Math.min(after.length ? Math.min(...after) : end, end) - m.createdAt
+ return left > limit ? [left - limit] : []
+ })
+}
diff --git a/backend/src/superTabs/scorecard/job.test.ts b/backend/src/superTabs/scorecard/job.test.ts
new file mode 100644
index 000000000..700ff36fc
--- /dev/null
+++ b/backend/src/superTabs/scorecard/job.test.ts
@@ -0,0 +1,26 @@
+import { describe, expect, it } from 'vitest'
+import { dueDays, pruneCache, scorecardCache } from './job.js'
+
+const HOUR = 3_600_000
+/** 2026-11-06 00:31 in Vietnam. */
+const AFTER_MIDNIGHT = Date.UTC(2026, 10, 5, 17, 31)
+
+describe('the nightly scorecard pass', () => {
+ it('computes yesterday and the day before once yesterday has settled', () => {
+ expect(dueDays({ timeZone: 'Asia/Ho_Chi_Minh' }, AFTER_MIDNIGHT)).toEqual(['2026-11-05', '2026-11-04'])
+ // 00:10: yesterday's late rows may still land, so only the day before is due.
+ expect(dueDays({ timeZone: 'Asia/Ho_Chi_Minh' }, AFTER_MIDNIGHT - 21 * 60_000)).toEqual(['2026-11-04'])
+ })
+
+ it('forgets the days no pass can be due for, so a long-lived worker keeps a few keys', () => {
+ const cache = scorecardCache()
+ for (const on of ['2026-11-01', '2026-11-03', '2026-11-04', '2026-11-05']) {
+ cache.settled.add(JSON.stringify(['tab', 'p', on]))
+ cache.tries.set(JSON.stringify(['tab', 'p', on]), 1)
+ }
+ // 11-08 00:31 in Vietnam is 11-07 in UTC: a zone up to 14 hours either side is due for 11-04 at the earliest.
+ pruneCache(cache, AFTER_MIDNIGHT + 48 * HOUR)
+ expect([...cache.settled].map(k => JSON.parse(k)[2])).toEqual(['2026-11-04', '2026-11-05'])
+ expect([...cache.tries.keys()].map(k => JSON.parse(k)[2])).toEqual(['2026-11-04', '2026-11-05'])
+ })
+})
diff --git a/backend/src/superTabs/scorecard/job.ts b/backend/src/superTabs/scorecard/job.ts
new file mode 100644
index 000000000..20fd80a41
--- /dev/null
+++ b/backend/src/superTabs/scorecard/job.ts
@@ -0,0 +1,157 @@
+/**
+ * The nightly scorecard (task #3386): once a project's local day has ended, the singleton worker computes its figures from
+ * the Swarm's records and stores them, for every project of every Swarm, so the numbers never wait on a PM's poll.
+ *
+ * - Which projects: those with a group active in the last week, from one $group over the activity index, read again every
+ * PLAN_EVERY_MS, not on each pass. Then each Swarm's head, for its projects' chats and time zones.
+ * - When: a pass every 15 minutes. A project's day is due from 30 minutes after its local midnight, for the two days before
+ * today that have no row yet: the first pass after a release fills yesterday and the day before by itself.
+ * - Failure: a day that fails is logged with its Swarm and project and tried again on the next pass, at most
+ * MAX_TRIES times; then it is logged as an error and left missing, never stored as zeros.
+ */
+import type { PrismaClient } from '@prisma/client'
+import type { SuperTabState } from '../model.js'
+import { projectsOf, type SwarmProject } from '../projects.js'
+import { groupsBy } from '../parts/indexed.js'
+import { knownTimeZone, localNow } from '../digest.js'
+import { DEFAULT_TIME_ZONE } from '../costs/reflection.js'
+import { logger } from '../../utils/logger.js'
+import { dayBounds, scorecardDay } from './figures.js'
+import { scorecardRecords } from './records.js'
+import { daysBefore, hasScorecard, writeScorecard } from './rows.js'
+
+export const SCORECARD_EVERY_MS = 15 * 60_000
+export const PLAN_EVERY_MS = 3 * 60 * 60_000
+/** How long after its local midnight a day is computed: late rows of the day have landed. */
+export const SETTLE_MS = 30 * 60_000
+const ACTIVE_MS = 7 * 24 * 60 * 60_000
+/** Days before today a pass fills when they have no row. */
+const DAYS_BACK = 2
+export const MAX_TRIES = 4
+/** (Swarm, project) pairs one plan reads at most. */
+export const MAX_CANDIDATES = 5000
+
+export interface ScorecardTarget { tabId: string; projectId: string; chatId: string; timeZone: string }
+export interface ScorecardPass { stored: number; already: number; failed: number; capped: number }
+export interface ScorecardCache { plan: ScorecardTarget[] | null; readAt: number; settled: Set<string>; tries: Map<string, number> }
+export const scorecardCache = (): ScorecardCache => ({ plan: null, readAt: 0, settled: new Set(), tries: new Map() })
+
+type Log = Pick<typeof logger, 'warn' | 'error' | 'info'>
+
+/** The zone a project's days are counted in: its digest's, else the default (a zone this runtime does not know too). */
+export function scorecardZone(project: Pick<SwarmProject, 'digest'>): string {
+ const zone = project.digest?.timeZone
+ return zone && knownTimeZone(zone) ? zone : DEFAULT_TIME_ZONE
+}
+
+/** The last day a project should have a row for at `now`: yesterday in its zone, stored or not. */
+export const scorecardThrough = (project: Pick<SwarmProject, 'digest'>, now: number): string =>
+ daysBefore(localNow(scorecardZone(project), now).date, 1)
+
+/** Every project with a group active in the last week, with its chat and time zone. A Swarm whose head fails to read is
+ * logged and counted; the next pass reads the plan again. */
+export async function scorecardPlan(db: PrismaClient, now: number, log: Log = logger): Promise<{ targets: ScorecardTarget[]; failed: number }> {
+ const rows = await groupsBy(db, 'activity', [
+ { $match: { dormant: { $in: [false, true] }, lastActivityAt: { $gte: { $date: new Date(now - ACTIVE_MS).toISOString() } } } },
+ { $group: { _id: '$tabId' } }, { $limit: MAX_CANDIDATES },
+ ]) as unknown as Array<{ _id: string }>
+ if (rows.length >= MAX_CANDIDATES) log.warn('[scorecard] read the most Swarms a plan reads; some get no scorecard', { limit: MAX_CANDIDATES })
+ const targets: ScorecardTarget[] = []
+ let failed = 0
+ for (const { _id: tabId } of rows) {
+ try {
+ const head = await db.swarmHead.findUnique({ where: { id: `${tabId}:head` }, select: { deleted: true, data: true } })
+ if (!head || head.deleted) continue
+ for (const project of projectsOf(head.data as unknown as SuperTabState)) {
+ targets.push({ tabId, projectId: project.id, chatId: project.leadConversationId, timeZone: scorecardZone(project) })
+ }
+ } catch (error) {
+ failed++
+ log.warn('[scorecard] reading a Swarm failed; trying again on the next pass', { tabId, error: error instanceof Error ? error.message : String(error) })
+ }
+ }
+ return { targets, failed }
+}
+
+/** Forgets the days no pass at `now` can be due for, in any zone (UTC±14 h): a long-lived worker keeps a few keys a project. */
+export function pruneCache(cache: ScorecardCache, now: number): void {
+ const oldest = daysBefore(new Date(now).toISOString().slice(0, 10), DAYS_BACK + 1)
+ const stale = (key: string) => (JSON.parse(key) as [string, string, string])[2] < oldest
+ for (const key of cache.settled) if (stale(key)) cache.settled.delete(key)
+ for (const key of cache.tries.keys()) if (stale(key)) cache.tries.delete(key)
+}
+
+/** The days of `target` a pass at `now` computes when they have no row: the ended ones of the last DAYS_BACK. */
+export function dueDays(target: Pick<ScorecardTarget, 'timeZone'>, now: number): string[] {
+ const today = localNow(target.timeZone, now).date
+ return Array.from({ length: DAYS_BACK }, (_, i) => daysBefore(today, i + 1)).filter(on => dayBounds(on, target.timeZone).end + SETTLE_MS <= now)
+}
+
+/** Computes and stores one project's day. `read`: the client the records come from (the secondaries in production). */
+export async function computeScorecard(read: PrismaClient, write: PrismaClient, target: ScorecardTarget, on: string, now: number):
+ Promise<{ capped: number }> {
+ const { start, end } = dayBounds(on, target.timeZone)
+ const { input, capped } = await scorecardRecords(read, { tabId: target.tabId, projectId: target.projectId, chatId: target.chatId, start, end })
+ await writeScorecard(write, target.tabId, { projectId: target.projectId, on, timeZone: target.timeZone, figures: scorecardDay(input), computedAt: now })
+ return { capped }
+}
+
+/** One pass: each planned project's due days without a row are computed. A failure is one day's: logged, the others go on. */
+export async function scorecardPass(db: PrismaClient, read: PrismaClient, now: number, log: Log = logger,
+ cache: ScorecardCache = scorecardCache()): Promise<ScorecardPass> {
+ const pass: ScorecardPass = { stored: 0, already: 0, failed: 0, capped: 0 }
+ if (!cache.plan || now - cache.readAt >= PLAN_EVERY_MS) {
+ const { targets, failed } = await scorecardPlan(db, now, log)
+ pruneCache(cache, now)
+ pass.failed += failed
+ Object.assign(cache, { plan: failed ? null : targets, readAt: now })
+ return run(targets)
+ }
+ return run(cache.plan)
+
+ async function run(targets: ScorecardTarget[]): Promise<ScorecardPass> {
+ for (const target of targets) {
+ for (const on of dueDays(target, now)) {
+ const key = JSON.stringify([target.tabId, target.projectId, on])
+ if (cache.settled.has(key)) continue
+ try {
+ if (await hasScorecard(db, target.tabId, target.projectId, on)) { pass.already++; cache.settled.add(key); continue }
+ const { capped } = await computeScorecard(read, db, target, on, now)
+ pass.stored++
+ pass.capped += capped
+ if (capped) log.warn('[scorecard] some groups had more rows than a day reads; their figures may be low', { tabId: target.tabId, projectId: target.projectId, on, capped })
+ cache.settled.add(key)
+ } catch (error) {
+ pass.failed++
+ const tries = (cache.tries.get(key) ?? 0) + 1
+ cache.tries.set(key, tries)
+ const facts = { tabId: target.tabId, projectId: target.projectId, on, tries, error: error instanceof Error ? error.message : String(error) }
+ if (tries >= MAX_TRIES) {
+ cache.settled.add(key)
+ log.error('[scorecard] a day failed every try; it stays missing from the project\'s scorecard', facts)
+ } else log.warn('[scorecard] a day failed; trying again on the next pass', facts)
+ }
+ }
+ }
+ return pass
+ }
+}
+
+/** Runs a pass every [everyMs] in the singleton worker, never two at once. */
+export function startScorecards(db: PrismaClient, read: PrismaClient = db, everyMs = SCORECARD_EVERY_MS, now: () => number = Date.now): () => void {
+ let running = false
+ const cache = scorecardCache()
+ const run = () => {
+ if (running) return
+ running = true
+ const began = Date.now()
+ void scorecardPass(db, read, now(), logger, cache)
+ .then(pass => { if (pass.stored || pass.failed) logger.info('[scorecard] pass', { ...pass, durationMs: Date.now() - began }) })
+ .catch(error => logger.warn('[scorecard] pass failed', { error: error instanceof Error ? error.message : String(error) }))
+ .finally(() => { running = false })
+ }
+ run()
+ const timer = setInterval(run, everyMs)
+ timer.unref()
+ return () => clearInterval(timer)
+}
diff --git a/backend/src/superTabs/scorecard/records.ts b/backend/src/superTabs/scorecard/records.ts
new file mode 100644
index 000000000..7923eec47
--- /dev/null
+++ b/backend/src/superTabs/scorecard/records.ts
@@ -0,0 +1,128 @@
+/**
+ * The records one project's scorecard day is computed from (figures.ts), read from Mongo through named indexes: the
+ * project's open groups and those active in the day, each group's rows back to the day's start (and, for a task, its last
+ * status before it; for the chat, the ask lookback), the rows people replied to, the asks' deliveries, and the day's starts
+ * and route decisions. Every read is bounded: a group stops at its cap and the row says how many did.
+ */
+import type { Prisma, SuperTabMessage } from '@prisma/client'
+import type { Conversation } from '../model.js'
+import { groupsBy, readBy } from '../parts/indexed.js'
+import { ms } from '../parts/light.js'
+import { prismaRow } from '../rawRows.js'
+import { messagesPast } from '../pageReads.js'
+import { isAsk, SCORECARD_LIMITS, type ScorecardInput, type ScorecardMessage, type ScorecardTask } from './figures.js'
+
+type Transaction = Prisma.TransactionClient
+
+/** Rows one task's read takes at most, and the project chat's (seven days of a busy chat). */
+export const GROUP_ROW_CAP = 5_000
+export const CHAT_ROW_CAP = 20_000
+const PAGE = 500
+const DAY = 24 * 60 * 60_000
+/** Route decisions and starts one day reads at most; far above a busy project's day. */
+const DAY_ROW_CAP = 10_000
+
+const GROUP_FIELDS = { _id: 0, key: 1, 'data.id': 1, 'data.kind': 1, 'data.number': 1, 'data.reviewOf': 1, 'data.leadId': 1, 'data.ledger.askedAt': 1,
+ 'data.ledger.shippedAt': 1, 'data.ledger.fixedAfterMergeAt': 1, 'data.ledger.runs.at': 1, 'data.ledger.proposal.messageId': 1,
+ 'data.cards.line.messageId': 1 }
+
+export interface ScorecardRecords { input: ScorecardInput; capped: number }
+
+const date = (at: number) => ({ $date: new Date(at).toISOString() })
+const projectIds = (projectId: string) => projectId === 'lead' ? ['lead', null] : [projectId]
+
+/** The project's groups the day can count: every open one, and the quiet ones active since the day began. */
+async function projectGroups(tx: Transaction, tabId: string, projectId: string, start: number): Promise<Conversation[]> {
+ const base = { tabId, deleted: false, projectId: { $in: projectIds(projectId) } }
+ const rows = [
+ ...await groupsBy(tx, 'project', [{ $match: { ...base, dormant: false } }, { $project: GROUP_FIELDS }]),
+ ...await groupsBy(tx, 'project', [{ $match: { ...base, dormant: true, lastActivityAt: { $gte: date(start) } } }, { $project: GROUP_FIELDS }]),
+ ]
+ return rows.map(row => ({ ...row.data as Conversation, id: (row.data as Conversation | undefined)?.id ?? String(row.key) }))
+}
+
+const asMessage = (row: SuperTabMessage): ScorecardMessage => {
+ const author = row.author as { kind?: string; id?: string } | null
+ return { id: row.id, conversationId: row.conversationId, author: { kind: author?.kind === 'person' ? 'person' : 'agent', id: String(author?.id ?? '') },
+ source: (row.source ?? null) as Record<string, unknown> | null, text: row.text, replyTo: row.replyTo ?? null, thread: row.thread ?? null,
+ createdAt: row.createdAt.getTime(), mentions: row.mentions ?? [] }
+}
+
+/** A row that says a task's phase (figures.ts): a status, a loss or a resume. */
+const phaseRow = (row: SuperTabMessage) => {
+ const source = row.source as { kind?: unknown; status?: unknown } | null
+ return (source?.kind === 'system' && typeof source.status === 'string') || source?.kind === 'work-lost' || source?.kind === 'work-resumed'
+}
+
+/** One group's rows, newest first, back to `from`; a task's go on to its last phase row before it. Stops at `cap`. */
+async function groupRows(tx: Transaction, tabId: string, conversationId: string, from: number, task: boolean, cap: number):
+ Promise<{ rows: ScorecardMessage[]; capped: boolean }> {
+ const rows: ScorecardMessage[] = []
+ let edge: { sequence: number; id: string } | undefined
+ for (let read = 0; read < cap;) {
+ const page = await messagesPast(tx, { tabId, conversationId }, edge, false, Math.min(PAGE, cap - read))
+ read += page.length
+ for (const row of page) {
+ if (row.deletedAt) continue
+ if (row.createdAt.getTime() >= from) { rows.push(asMessage(row)); continue }
+ if (!task) return { rows, capped: false }
+ if (phaseRow(row)) { rows.push(asMessage(row)); return { rows, capped: false } }
+ }
+ if (page.length < PAGE) return { rows, capped: false }
+ const last = page.at(-1)!
+ edge = { sequence: last.sequence, id: last.id }
+ }
+ return { rows, capped: true }
+}
+
+/** One project's records for the local day [start, end). */
+export async function scorecardRecords(tx: Transaction, at: { tabId: string; projectId: string; chatId: string; start: number; end: number }):
+ Promise<ScorecardRecords> {
+ const { tabId, projectId, chatId, start, end } = at
+ const groups = await projectGroups(tx, tabId, projectId, start)
+ const chat = groups.find(g => g.id === chatId)
+ const ids = new Set(groups.map(g => g.id))
+ let capped = 0
+ const messages: ScorecardMessage[] = []
+ for (const group of groups) {
+ const isChat = group.id === chatId
+ // A Buddy chat is a person's own, not the project's work.
+ if (group.kind && !isChat) continue
+ const from = isChat ? end - SCORECARD_LIMITS.askLookbackDays * DAY : start
+ const read = await groupRows(tx, tabId, group.id, from, !isChat, isChat ? CHAT_ROW_CAP : GROUP_ROW_CAP)
+ if (read.capped) capped++
+ messages.push(...read.rows)
+ }
+ // The rows people replied to in the day, wherever they are: whether a PM wrote them. Matched by id alone (pageReads.ts).
+ const have = new Set(messages.map(m => m.id))
+ const wanted = [...new Set(messages.filter(m => m.author.kind === 'person' && m.replyTo && !have.has(m.replyTo) && m.createdAt >= start && m.createdAt < end)
+ .map(m => m.replyTo!))]
+ for (let i = 0; i < wanted.length; i += PAGE) {
+ const found = await readBy(tx, 'SuperTabMessage', 'id', [{ $match: { _id: { $in: wanted.slice(i, i + PAGE) } } }])
+ messages.push(...found.map(row => prismaRow<SuperTabMessage>('SuperTabMessage', row)).filter(row => row.tabId === tabId).map(asMessage))
+ }
+ const asks = messages.filter(m => m.conversationId === chatId && isAsk(m)).map(m => m.id)
+ const answeredDeliveries: ScorecardInput['answeredDeliveries'] = []
+ for (let i = 0; i < asks.length; i += PAGE) {
+ const found = await readBy(tx, 'SuperTabDelivery', 'byMessage', [{ $match: { tabId, messageId: { $in: asks.slice(i, i + PAGE) }, answeredAt: { $ne: null } } },
+ { $project: { messageId: 1, answeredAt: 1 } }])
+ answeredDeliveries.push(...found.flatMap(row => row.answeredAt ? [{ messageId: String(row.messageId), answeredAt: ms(row.answeredAt) }] : []))
+ }
+ const starts = (await readBy(tx, 'SwarmStart', 'changes', [{ $match: { tabId, changedAt: { $gte: date(start) } } }, { $limit: DAY_ROW_CAP },
+ { $project: { conversationId: 1, 'data.requestedAt': 1, 'data.error': 1 } }]))
+ .flatMap(row => {
+ const data = row.data as { requestedAt?: unknown; error?: unknown } | undefined
+ return ids.has(String(row.conversationId)) && typeof data?.requestedAt === 'number'
+ ? [{ conversationId: String(row.conversationId), requestedAt: data.requestedAt, failed: data.error != null }] : []
+ })
+ const routeDecisions = (await readBy(tx, 'SuperTabRouteDecision', 'recent', [{ $match: { tabId, createdAt: { $gte: date(start), $lt: date(end) } } },
+ { $limit: DAY_ROW_CAP }, { $project: { conversationId: 1, createdAt: 1 } }]))
+ .flatMap(row => ids.has(String(row.conversationId)) ? [{ conversationId: String(row.conversationId), createdAt: ms(row.createdAt) }] : [])
+ const tasks: ScorecardTask[] = groups.filter(g => g.id !== chatId && !g.kind).map(g => ({ id: g.id,
+ ...g.number !== undefined ? { number: g.number } : {}, ...g.reviewOf ? { reviewOf: g.reviewOf } : {},
+ ...g.ledger?.askedAt !== undefined ? { askedAt: g.ledger.askedAt } : {}, ...g.ledger?.shippedAt !== undefined ? { shippedAt: g.ledger.shippedAt } : {},
+ ...g.ledger?.fixedAfterMergeAt !== undefined ? { fixedAfterMergeAt: g.ledger.fixedAfterMergeAt } : {},
+ runs: (g.ledger?.runs ?? []).map(r => r.at).filter(at => typeof at === 'number'),
+ requestMessageIds: [g.cards?.line?.messageId, g.ledger?.proposal?.messageId].filter((id): id is string => typeof id === 'string') }))
+ return { capped, input: { start, end, chatId, pmAgentIds: chat?.leadId ? [chat.leadId] : [], tasks, messages, answeredDeliveries, starts, routeDecisions } }
+}
diff --git a/backend/src/superTabs/scorecard/rows.ts b/backend/src/superTabs/scorecard/rows.ts
new file mode 100644
index 000000000..61fff1eca
--- /dev/null
+++ b/backend/src/superTabs/scorecard/rows.ts
@@ -0,0 +1,45 @@
+/** The stored scorecard days (prisma SuperTabScorecard): one row per project and local day. */
+import type { Prisma, PrismaClient } from '@prisma/client'
+import { stableId } from '../model.js'
+import { readBy } from '../parts/indexed.js'
+import { ms } from '../parts/light.js'
+import type { ScorecardFigures } from './figures.js'
+
+/** How long a project's days are kept: enough to compare a quarter of daily changes. */
+export const SCORECARD_DAYS_KEPT = 120
+/** The most days one read returns per project. */
+export const SCORECARD_READ_DAYS = 14
+
+export interface ScorecardDay { projectId: string; on: string; timeZone: string; figures: ScorecardFigures; computedAt: number }
+
+export const scorecardId = (tabId: string, projectId: string, on: string): string => stableId('scorecard', tabId, projectId, on)
+
+/** The local date `days` before `on` (YYYY-MM-DD). */
+export function daysBefore(on: string, days: number): string {
+ const [y, m, d] = on.split('-').map(Number) as [number, number, number]
+ return new Date(Date.UTC(y, m - 1, d - days)).toISOString().slice(0, 10)
+}
+
+/** Whether the project's row for `on` is stored. */
+export async function hasScorecard(db: PrismaClient, tabId: string, projectId: string, on: string): Promise<boolean> {
+ return !!await db.superTabScorecard.findUnique({ where: { id: scorecardId(tabId, projectId, on) }, select: { id: true } })
+}
+
+/** Stores the day by its id, so a pass that runs twice writes one row, and deletes the project's days past the kept ones. */
+export async function writeScorecard(db: PrismaClient, tabId: string, day: ScorecardDay): Promise<void> {
+ const id = scorecardId(tabId, day.projectId, day.on)
+ await db.$runCommandRaw({ update: 'SuperTabScorecard', updates: [{ q: { _id: id }, upsert: true, u: { $set: { tabId, projectId: day.projectId, on: day.on,
+ timeZone: day.timeZone, figures: day.figures as unknown as Prisma.InputJsonValue, computedAt: { $date: new Date(day.computedAt).toISOString() } } } }] })
+ await db.$runCommandRaw({ delete: 'SuperTabScorecard', deletes: [{ q: { tabId, projectId: day.projectId, on: { $lt: daysBefore(day.on, SCORECARD_DAYS_KEPT) } }, limit: 0 }] })
+}
+
+/** These projects' days, newest first, at most `days` each. */
+export async function readScorecards(tx: Prisma.TransactionClient, tabId: string, projectIds: string[], days = SCORECARD_READ_DAYS): Promise<ScorecardDay[]> {
+ const rows: ScorecardDay[] = []
+ for (const projectId of projectIds) {
+ const found = await readBy(tx, 'SuperTabScorecard', 'day', [{ $match: { tabId, projectId } }, { $sort: { on: -1 } }, { $limit: days }])
+ rows.push(...found.map(row => ({ projectId, on: String(row.on), timeZone: String(row.timeZone), figures: row.figures as ScorecardFigures,
+ computedAt: ms(row.computedAt) })))
+ }
+ return rows
+}
diff --git a/backend/src/superTabs/store.ts b/backend/src/superTabs/store.ts
index cddc57f78..22ea126ba 100644
--- a/backend/src/superTabs/store.ts
+++ b/backend/src/superTabs/store.ts
@@ -1,8 +1,9 @@
import { refusalLogFields, type DeclaredClient } from './refusalLog.js'
+import { applyCrewKeep, crewMoveRequest, crewNeedsMachine, isCrewMember, type CrewDeps } from './crewKeep.js'
import { writeReactionActivity, writeReplyActivity } from './activity.js'
import { phoneNotices, type DecisionRow, type ReplyRow, type TagRow } from '../push/notices.js'
import type { PhoneNotice } from '../push/sender.js'
-import { joiningTeamMacs, type TeamMachineRow, accountMachineName, availableSlots, expireRemovals, followAccountName, LASTING_TEAM_REFUSALS, machineContribution, machineObservationSchema, slotCounter, machineTeamWork, preparableStart, readingOnly, refusableStart, recordAgentPresence, renamedBy, removalPreview, startPreparing, storedObservation, swarmGridRow, teamAccountRequired, teamWorkRefusal, ownAccountWork, ownAccountNote, type MachineObservation } from './machines.js'
+import { admitMachineStart, joiningTeamMacs, type TeamMachineRow, accountMachineName, availableSlots, expireRemovals, followAccountName, LASTING_TEAM_REFUSALS, machineContribution, machineObservationSchema, slotCounter, machineTeamWork, preparableStart, readingOnly, refusableStart, recordAgentPresence, renamedBy, removalPreview, startPreparing, storedObservation, swarmGridRow, teamAccountRequired, teamWorkRefusal, ownAccountWork, ownAccountNote, type MachineObservation } from './machines.js'
import { expireAccess } from './machineAccess.js'
import { authorForAgent, lazyUsernames, namesPerson, rowForAgent, shownNameOf } from './personNames.js'
import { machineSuspensionReason, readingClearsSuspensions, suspendedEngines } from './engineHealth.js'
@@ -3613,6 +3614,19 @@ export class SuperTabStore {
})
}
+ /** What the crew keeper asks of the store (crewKeep.ts CrewDeps): whether a machine admits a crew member's move start now,
+ * under the same checks an agent.start meets, Team-account facts read once per sweep. */
+ private async crewDeps(tx: Transaction, state: SuperTabState, now: number): Promise<CrewDeps> {
+ const teamMachines = await this.teamMachines(tx, state), otherWorkspaceMachines = await this.otherWorkspaceMachines(tx, state)
+ // No machine rules until the project rules record is on main (River, 11-05): then they come from it, never from source.
+ return { machineRules: [], admits: (machineId, agent) => {
+ const pool = state.machines?.find(m => m.machineId === machineId)
+ const request = pool && crewMoveRequest(state, agent, pool)
+ if (!request) return false
+ try { return !!admitMachineStart(state, request, now, undefined, teamMachines, otherWorkspaceMachines) } catch { return false }
+ } }
+ }
+
/** One state write moving the GitHub revision of the projects [moves] picks, re-read in its transaction; none picked
* writes nothing. */
private async moveGitHub(tabId: string, moves: (project: SwarmProject) => boolean, also?: (project: SwarmProject) => void): Promise<boolean> {
@@ -3861,8 +3875,13 @@ export class SuperTabStore {
// Its own agents lose their tasks at once (a stopped session); every 30 seconds it runs the Swarm's time checks too, so
// they keep running when the Lead's own machine is the one that died.
// The starts the server refused this machine's own reports for fail now, with the refusal, not 3 minutes on (#2441).
+ // The crew keeper with the Swarm's time checks (crewKeep.ts): sleep, move off a failing machine, drop folders. A move is
+ // placed only where a start would be admitted now, Team rules included.
+ // The Team facts are read only when a member needs another machine (review of #3229); sleeps and folders need none.
+ const crew = sweep && state.agents.some(isCrewMember)
+ ? crewNeedsMachine(state, now) ? await this.crewDeps(tx, state, now) : { admits: () => false } : null
const checked = [...failRefusedStarts(state, machineId, now), ...loseTasks(state, now, worker => worker.machineId === machineId),
- ...sweep ? sweepLiveness(state, now) : []]
+ ...sweep ? sweepLiveness(state, now) : [], ...crew ? applyCrewKeep(state, now, crew) : []]
// Then the agents it shows closed leave, those with no active work left: the Lead is told of their tasks above (closedAgents.ts).
const closedOut = removeClosedAgents(state, state.agents.filter(a => closed.includes(a.id)), now).map(a => a.id)
removedIds.push(...closedOut)
diff --git a/backend/src/superTabs/tidy.ts b/backend/src/superTabs/tidy.ts
index becb402dc..57ce439af 100644
--- a/backend/src/superTabs/tidy.ts
+++ b/backend/src/superTabs/tidy.ts
@@ -21,7 +21,7 @@ export const SWITCH_UNCLAIMED_MS = 2 * 60_000
const time = (value: string | undefined) => value === undefined ? undefined : Date.parse(value)
/** The loaded groups an agent belongs to or worked in. */
-const groupsOf = (state: SuperTabState, target: Agent): Conversation[] =>
+export const groupsOf = (state: SuperTabState, target: Agent): Conversation[] =>
state.conversations.filter(c => c.agentIds.includes(target.id) || c.assignment?.agentId === target.id)
/** When the agent last had anything to do: its start, its coordinator's keep, its own progress, or a message in one of its
@@ -29,7 +29,7 @@ const groupsOf = (state: SuperTabState, target: Agent): Conversation[] =>
* groups, whose messages are about others' work: it is idle from its start or keep (Swarm autonomous-harness, 2026-10-29:
* one sat idle 18+ minutes, its time renewed by every message in a busy chat). One a person added by hand has no start of
* record, so its groups' messages count, from the one it joined in. Undefined when nothing tells when. */
-function idleSince(target: Agent, groups: Conversation[]): number | undefined {
+export function idleSince(target: Agent, groups: Conversation[]): number | undefined {
const worked = !!target.lostTask || groups.some(g => g.assignment?.agentId === target.id) || !target.startedSession
const seen = [time(target.startedSession?.createdAt), target.keptAt, ...(worked ? groups : []).flatMap(g => [time(g.lastActivityAt),
...g.assignment?.agentId === target.id ? [g.assignment.progressAt, g.assignment.lost?.at] : []])]
diff --git a/backend/src/worker.ts b/backend/src/worker.ts
index f1ee1da90..ebc526b1c 100644
--- a/backend/src/worker.ts
+++ b/backend/src/worker.ts
@@ -13,7 +13,7 @@ import {
pruneLegacyDeviceBindings,
} from './lib/db/migrate.js'
import { ensurePlans } from './lib/db/seed.js'
-import { prisma } from './lib/prisma.js'
+import { prisma, prismaSecondary } from './lib/prisma.js'
import { startQueryStats } from './lib/queryStats.js'
import { startRetryStats } from './superTabs/retryStats.js'
import { releaseSchema } from './lib/db/schemaRelease.js'
@@ -42,6 +42,7 @@ import { startSwarmPurge } from './superTabs/purge.js'
import { startAttachmentSweepFrom } from './lib/attachmentSweep.js'
import { attachmentsFromEnv } from './superTabs/attachments.js'
import { startRoutingReflection } from './lib/routingReflection.js'
+import { startScorecards } from './superTabs/scorecard/job.js'
import { logger } from './utils/logger.js'
// Singleton background process. HTTP/WS server.ts runs as a PM2 cluster and must never own schema
@@ -87,6 +88,7 @@ async function start(): Promise<void> {
let stopSwarmPurge = (): void => {}
let stopAttachmentSweep = (): void => {}
let stopReflection = (): void => {}
+ let stopScorecards = (): void => {}
const shutdown = (signal: string): void => {
logger.info(`Received ${signal}, stopping backend worker...`)
const hardExit = setTimeout(() => process.exit(0), 5000)
@@ -106,6 +108,7 @@ async function start(): Promise<void> {
stopSwarmPurge()
stopAttachmentSweep()
stopReflection()
+ stopScorecards()
stopThreadBackfill()
// The last partial minute, before the database closes.
stopQueryStats()
@@ -167,6 +170,8 @@ async function start(): Promise<void> {
// At 23:00 in each project's time zone, what its routed work cost and how it went, with routing changes asked of its owner
// (lib/routingReflection.ts).
stopReflection = startRoutingReflection(prisma, swarms)
+ // Each project's scorecard for its local day, once the day has ended, from the Swarm's own records (superTabs/scorecard/job.ts).
+ stopScorecards = startScorecards(prisma, prismaSecondary)
// PM2 wait_ready gates the singleton worker's "online" state on schema/catalog/backfill startup.
// Plain node/tsx runs do not provide process.send and simply continue.
process.send?.('ready')
diff --git a/backend/vitest.super-tabs.config.ts b/backend/vitest.super-tabs.config.ts
index 0b03060d6..f46608352 100644
--- a/backend/vitest.super-tabs.config.ts
+++ b/backend/vitest.super-tabs.config.ts
@@ -6,7 +6,7 @@ export default defineConfig({ test: { include: ['integration/superTabs.test.ts',
'integration/superTabHotReads.test.ts', 'integration/swarmSweepStarts.test.ts', 'integration/superTabGroupPlans.test.ts', 'integration/superTabInvitationPlans.test.ts', 'integration/superTabCache.test.ts', 'integration/superTabCacheDelta.test.ts', 'integration/superTabHostGroups.test.ts', 'integration/superTabMachineReports.test.ts', 'integration/superTabMachineLiveness.test.ts', 'integration/superTabHeldParts.test.ts',
'integration/superTabObserver.test.ts', 'integration/productUsageConflicts.test.ts', 'integration/productUsageReceipts.test.ts', 'integration/schemaRelease.test.ts', 'integration/superTabBuddies.test.ts', 'integration/superTabNotAvailable.test.ts',
'integration/superTabBuddyProbes.test.ts', 'integration/superTabBuddySessions.test.ts', 'integration/superTabSwarmBuddy.test.ts', 'integration/superTabPersonalLead.test.ts', 'integration/superTabCosts.test.ts', 'integration/harnessSharesList.test.ts', 'integration/harnessSharesIndexPlan.test.ts', 'integration/atlasSnapshot.test.ts', 'integration/queryStats.test.ts',
- 'integration/superTabEvidence.test.ts', 'integration/superTabAttemptModels.test.ts', 'integration/superTabNamedReads.test.ts', 'integration/swarmSweepPlans.test.ts', 'integration/overdueDecisions.test.ts', 'integration/workerIndexPlans.test.ts', 'integration/superTabReflection.test.ts',
+ 'integration/superTabEvidence.test.ts', 'integration/superTabAttemptModels.test.ts', 'integration/superTabNamedReads.test.ts', 'integration/swarmSweepPlans.test.ts', 'integration/overdueDecisions.test.ts', 'integration/workerIndexPlans.test.ts', 'integration/superTabReflection.test.ts', 'integration/superTabScorecard.test.ts',
'integration/superTabMessagePages.test.ts', 'integration/superTabDeliveryCompaction.test.ts', 'integration/superTabNoticeDigest.test.ts', 'integration/superTabPmSchedule.test.ts', 'integration/superTabDigestHour.test.ts', 'integration/superTabCommandLoads.test.ts', 'integration/superTabThreads.test.ts', 'integration/superTabMachineRemovals.test.ts',
'integration/superTabInstructions.test.ts', 'integration/superTabProjectRules.test.ts', 'integration/superTabWorkflow.test.ts', 'integration/superTabTaskLines.test.ts', 'integration/superTabDeletion.test.ts', 'integration/communityReports.test.ts', 'integration/superTabMachineNames.test.ts', 'integration/superTabComputerMoves.test.ts', 'integration/superTabTagsClear.test.ts', 'integration/swarmAgentTombstones.test.ts', 'integration/superTabPullRequestEvents.test.ts', 'integration/superTabProposals.test.ts', 'integration/superTabRequestReads.test.ts', 'integration/superTabVoiceMessages.test.ts', 'integration/superTabThumbnails.test.ts', 'integration/superTabProjectChatWorkers.test.ts', 'integration/prismaQueryTiming.test.ts', 'integration/secondaryReads.test.ts', 'integration/pushDevices.test.ts'], hookTimeout: 120000, testTimeout: 30000, setupFiles: ['integration/noDefaultDigest.test-support.ts', 'integration/cacheOn.test-support.ts', 'src/lib/noRedis.test-support.ts'],
fileParallelism: false, env: { NODE_ENV: 'test', HARNESS_CREDENTIAL_ENCRYPTION_KEY: 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=' } } })
diff --git a/cli/src/superTabs/crewFolders.spec.ts b/cli/src/superTabs/crewFolders.spec.ts
new file mode 100644
index 000000000..0265cd45d
--- /dev/null
+++ b/cli/src/superTabs/crewFolders.spec.ts
@@ -0,0 +1,86 @@
+import { execFileSync } from 'node:child_process'
+import { existsSync, mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from 'node:fs'
+import { tmpdir } from 'node:os'
+import { join } from 'node:path'
+import { afterEach, describe, expect, it, vi } from 'vitest'
+import { CrewFolders, crewRest, pruneBranches } from './crewFolders.js'
+
+const roots: string[] = []
+const root = () => { const dir = realpathSync(mkdtempSync(join(tmpdir(), 'crew-folders-'))); roots.push(dir); return dir }
+afterEach(() => { for (const dir of roots.splice(0)) rmSync(dir, { recursive: true, force: true }) })
+const git = (cwd: string, ...args: string[]) => execFileSync('git', ['-c', '[email protected]', '-c', 'user.name=T', '-c', 'commit.gpgsign=false', ...args],
+ { cwd, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] }).trim()
+
+/** An origin with main, a project clone of it, and a worktree for one crew member. */
+function repos() {
+ const dir = root(), origin = join(dir, 'origin.git'), clone = join(dir, 'clone'), worktree = join(dir, 'worktrees', 'ada')
+ git(dir, 'init', '--bare', '-b', 'main', origin)
+ git(dir, 'clone', origin, clone)
+ writeFileSync(join(clone, 'README.md'), 'hello\n')
+ git(clone, 'add', '.'); git(clone, 'commit', '-m', 'first'); git(clone, 'push', 'origin', 'main')
+ mkdirSync(join(dir, 'worktrees'))
+ git(clone, 'worktree', 'add', '-b', 'super-tab/start-1', worktree, 'origin/main')
+ return { dir, origin, clone, worktree }
+}
+
+describe('what a crew row asks of its host', () => {
+ it('reads asleep, a dropped folder and a move from the agent record', () => {
+ expect(crewRest({ asleep: { since: 1 } })).toBe('sleep')
+ expect(crewRest({ asleep: { since: 1, folderDropped: true } })).toBe('drop')
+ expect(crewRest({ movedTo: { machineId: 'mini' } })).toBe('moving')
+ expect(crewRest({})).toBeNull()
+ })
+})
+
+describe('a crew member\'s folder', () => {
+ const folders = (dataDir: string, worktree: { path: string; source: string } | null, remove = vi.fn(async () => {})) =>
+ ({ folders: new CrewFolders({ dataDir, machineId: 'mac', worktree: () => worktree, removeWorktree: remove, log: () => {} }), remove })
+
+ it('is dropped through the Swarm\'s own worktree removal, and remembered so a wake brings it back at the same path', async () => {
+ const r = repos(), data = root()
+ const { folders: f, remove } = folders(data, { path: r.worktree, source: r.clone }, vi.fn(async () => { git(r.clone, 'worktree', 'remove', r.worktree) }))
+ expect(await f.drop('tab', 'ada')).toBe('dropped')
+ expect(remove).toHaveBeenCalledWith('tab', 'ada')
+ expect(existsSync(r.worktree)).toBe(false)
+ expect(await f.restore('tab', 'ada')).toBe('restored')
+ expect(existsSync(join(r.worktree, 'README.md'))).toBe(true)
+ // Restored once: nothing is remembered any more.
+ expect(await f.restore('tab', 'ada')).toBe('none')
+ })
+
+ it('kept when the removal refuses it (work not committed or pushed), and nothing is remembered', async () => {
+ const r = repos(), data = root()
+ const { folders: f } = folders(data, { path: r.worktree, source: r.clone })
+ expect(await f.drop('tab', 'ada')).toBe('kept')
+ expect(await f.restore('tab', 'ada')).toBe('none')
+ })
+
+ it('none when the Swarm has no record of the folder', async () => {
+ const { folders: f, remove } = folders(root(), null)
+ expect(await f.drop('tab', 'ada')).toBe('none')
+ expect(remove).not.toHaveBeenCalled()
+ })
+})
+
+describe('branch copies', () => {
+ it('drops local branches merged into the default branch or whose remote branch is gone, never the current one or unpushed work', async () => {
+ const r = repos()
+ const work = r.worktree
+ // merged: pushed, then merged into main on the remote.
+ git(work, 'checkout', '-b', 'merged'); writeFileSync(join(work, 'a.txt'), 'a\n'); git(work, 'add', '.'); git(work, 'commit', '-m', 'a')
+ git(work, 'push', '-u', 'origin', 'merged'); git(r.clone, 'fetch'); git(r.clone, 'checkout', 'main'); git(r.clone, 'merge', '--ff-only', 'origin/merged'); git(r.clone, 'push', 'origin', 'main')
+ // gone: pushed, then its remote branch deleted (a closed pull request), every commit pushed before.
+ git(work, 'checkout', '-b', 'gone', 'origin/main'); writeFileSync(join(work, 'b.txt'), 'b\n'); git(work, 'add', '.'); git(work, 'commit', '-m', 'b')
+ git(work, 'push', '-u', 'origin', 'gone'); git(r.dir, '--git-dir', r.origin, 'branch', '-D', 'gone')
+ // unpushed: commits nowhere else.
+ git(work, 'checkout', '-b', 'unpushed', 'origin/main'); writeFileSync(join(work, 'c.txt'), 'c\n'); git(work, 'add', '.'); git(work, 'commit', '-m', 'c')
+ // current: the branch the member works on now.
+ git(work, 'checkout', '-b', 'current', 'origin/main')
+ // someone-else: made in the clone itself (the person's own checkout), pushed and merged: not this member's to prune.
+ git(r.clone, 'checkout', '-b', 'someone-else'); git(r.clone, 'push', '-u', 'origin', 'someone-else'); git(r.clone, 'checkout', 'main')
+ git(work, 'fetch', '--prune')
+ expect((await pruneBranches(work)).sort()).toEqual(['gone', 'merged'])
+ const left = git(work, 'branch', '--format=%(refname:short)').split('\n').sort()
+ expect(left).toEqual(['current', 'main', 'someone-else', 'super-tab/start-1', 'unpushed'].sort())
+ })
+})
diff --git a/cli/src/superTabs/crewFolders.ts b/cli/src/superTabs/crewFolders.ts
new file mode 100644
index 000000000..c3b3b0c1c
--- /dev/null
+++ b/cli/src/superTabs/crewFolders.ts
@@ -0,0 +1,124 @@
+/**
+ * A crew member's folder on its host (task #3398). The server says when a member sleeps, moves or has its folder dropped
+ * (backend superTabs/crewKeep.ts, on the agent record of its row); this keeps the folder in step:
+ * - a dropped folder goes through the Swarm's own worktree removal (machines.ts removeStoppedWorktree), which refuses one
+ * with work not committed or pushed, and is remembered so a wake brings it back at the same path, where the engine's
+ * conversation is filed (claude --resume and codex resume find it by folder);
+ * - finished branch copies go: a local branch whose work merged into the default branch, or whose remote branch was
+ * deleted (a merged or closed pull request). Never the current branch or one never pushed; a deleted branch's tip is
+ * kept under refs/harness/dropped/ so no commit becomes unreachable. Only branches this member's own folder was on: the
+ * clone's other branches (a person's, other agents') are never touched (security reviews of #3229).
+ */
+import { execFile } from 'node:child_process'
+import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs'
+import { dirname, isAbsolute, join } from 'node:path'
+import { promisify } from 'node:util'
+
+const run = promisify(execFile)
+const git = async (cwd: string, ...args: string[]) => (await run('git', args, { cwd, timeout: 60_000, maxBuffer: 4 * 1024 * 1024 })).stdout.trim()
+
+/** What a crew row asks of its host, from its agent record: close it to sleep (folder kept), close it and drop its folder, or
+ * close it because it moves elsewhere. Null for an awake member. */
+export function crewRest(agent: { asleep?: { since?: unknown; folderDropped?: unknown } | null; movedTo?: unknown }): 'sleep' | 'drop' | 'moving' | null {
+ if (agent.movedTo) return 'moving'
+ if (agent.asleep) return agent.asleep.folderDropped === true ? 'drop' : 'sleep'
+ return null
+}
+
+type Remembered = { tabId: string; harnessId: string; path: string; source: string }
+export interface CrewFoldersDeps {
+ dataDir: string
+ machineId: string
+ /** The Swarm's record of this member's worktree (machines.ts swarm-worktrees file): its folder and its project clone. */
+ worktree(tabId: string, harnessId: string): { path: string; source: string } | null
+ /** The Swarm's own removal, with its proofs and refusals (machines.ts removeStoppedWorktree). */
+ removeWorktree(tabId: string, harnessId: string): Promise<void>
+ log(line: string): void
+}
+
+export class CrewFolders {
+ /** This host's record, read once and kept in step with its writes: only this host writes the file. */
+ private cache?: Remembered[]
+ constructor(private readonly deps: CrewFoldersDeps) {}
+ /** Whether [harnessId]'s folder was dropped and waits to come back: read from disk, so a host restart forgets nothing. */
+ remembers(tabId: string, harnessId: string): boolean {
+ return this.read().some(r => r.tabId === tabId && r.harnessId === harnessId)
+ }
+ private get file() { return join(this.deps.dataDir, 'super-tabs', `crew-folders-${this.deps.machineId}.json`) }
+ private read(): Remembered[] {
+ return this.cache ??= this.readFile()
+ }
+ private readFile(): Remembered[] {
+ try {
+ const rows = (JSON.parse(readFileSync(this.file, 'utf8')) as { folders?: unknown }).folders
+ return Array.isArray(rows) ? rows.filter((r): r is Remembered => !!r && typeof r === 'object' && ['tabId', 'harnessId', 'path', 'source']
+ .every(k => typeof (r as Record<string, unknown>)[k] === 'string') && isAbsolute((r as Remembered).path) && isAbsolute((r as Remembered).source)) : []
+ } catch { return [] }
+ }
+ private write(rows: Remembered[]) {
+ this.cache = rows
+ mkdirSync(dirname(this.file), { recursive: true, mode: 0o700 })
+ writeFileSync(`${this.file}.next`, JSON.stringify({ version: 1, folders: rows }), { mode: 0o600 })
+ renameSync(`${this.file}.next`, this.file)
+ }
+ private forget(tabId: string, harnessId: string) {
+ const rows = this.read(), kept = rows.filter(r => !(r.tabId === tabId && r.harnessId === harnessId))
+ if (kept.length !== rows.length) this.write(kept)
+ }
+
+ /** Drops [harnessId]'s folder ('dropped'), or keeps it when the removal refused it ('kept'), or finds no record ('none'). */
+ async drop(tabId: string, harnessId: string): Promise<'dropped' | 'kept' | 'none'> {
+ const worktree = this.deps.worktree(tabId, harnessId)
+ if (!worktree) return 'none'
+ this.write([...this.read().filter(r => !(r.tabId === tabId && r.harnessId === harnessId)), { tabId, harnessId, ...worktree }])
+ await this.deps.removeWorktree(tabId, harnessId)
+ if (!existsSync(worktree.path)) return 'dropped'
+ this.forget(tabId, harnessId)
+ this.deps.log('Kept an asleep crew folder: the Swarm\'s removal refused it')
+ return 'kept'
+ }
+
+ /** Brings back a dropped folder at its own path, on the default branch, before its session reopens there. */
+ async restore(tabId: string, harnessId: string): Promise<'restored' | 'none' | 'failed'> {
+ const remembered = this.read().find(r => r.tabId === tabId && r.harnessId === harnessId)
+ if (!remembered) return 'none'
+ if (existsSync(remembered.path)) { this.forget(tabId, harnessId); return 'none' }
+ try {
+ await git(remembered.source, 'worktree', 'prune')
+ const base = await git(remembered.source, 'symbolic-ref', '--short', 'refs/remotes/origin/HEAD').catch(() => 'HEAD')
+ await git(remembered.source, 'worktree', 'add', '--detach', remembered.path, base)
+ this.forget(tabId, harnessId)
+ return 'restored'
+ } catch (error) {
+ this.deps.log(`Could not bring back a crew folder: ${error instanceof Error ? error.name : typeof error}`)
+ return 'failed'
+ }
+ }
+}
+
+/** Deletes [folder]'s finished branch copies and returns their names (see the file's comment for which). */
+export async function pruneBranches(folder: string): Promise<string[]> {
+ const checkedOut = new Set((await git(folder, 'worktree', 'list', '--porcelain')).split('\n')
+ .filter(l => l.startsWith('branch refs/heads/')).map(l => l.slice('branch refs/heads/'.length)))
+ const base = await git(folder, 'symbolic-ref', '--short', 'refs/remotes/origin/HEAD').catch(() => '')
+ const rows = (await git(folder, 'for-each-ref', 'refs/heads', '--format=%(refname:short)%09%(upstream)%09%(upstream:track)%09%(objectname)'))
+ .split('\n').filter(Boolean).map(line => { const [name, upstream, track, sha] = line.split('\t'); return { name: name!, upstream: upstream ?? '', track: track ?? '', sha: sha ?? '' } })
+ // Only branches this member's own folder was on (its HEAD's history): a worktree shares its clone's branches, which on an
+ // own account is a person's checkout with their branches and other agents' (security review 2 of #3229).
+ const own = new Set((await git(folder, 'reflog', 'show', '--format=%gs', 'HEAD').catch(() => '')).split('\n')
+ .flatMap(line => /^checkout: moving from (\S+) to (\S+)$/.exec(line)?.slice(1, 3) ?? []))
+ const dropped: string[] = []
+ for (const branch of rows) {
+ if (!own.has(branch.name)) continue
+ // A branch tracking the default branch itself (a start's own branch) is no pull request's copy.
+ if (checkedOut.has(branch.name) || !branch.upstream || (base && branch.upstream === `refs/remotes/${base}`)) continue
+ const gone = branch.track === '[gone]'
+ const merged = !gone && !!base && await git(folder, 'merge-base', '--is-ancestor', branch.name, base).then(() => true, () => false)
+ if (!gone && !merged) continue
+ // Its tip stays reachable, so a commit that was only here (a squash-merged branch, a closed one) is never lost.
+ if (gone) await git(folder, 'update-ref', `refs/harness/dropped/${branch.name}`, branch.sha)
+ await git(folder, 'branch', '-D', branch.name)
+ dropped.push(branch.name)
+ }
+ return dropped
+}
diff --git a/cli/src/superTabs/host.spec.ts b/cli/src/superTabs/host.spec.ts
index 9aa3410b1..38dc5f14f 100644
--- a/cli/src/superTabs/host.spec.ts
+++ b/cli/src/superTabs/host.spec.ts
@@ -1,4 +1,5 @@
import { createHash, randomUUID } from 'node:crypto'
+import { CrewFolders } from './crewFolders.js'
import { spawnSync } from 'node:child_process'
import { chmodSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, realpathSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
@@ -5934,3 +5935,98 @@ describe('requests per idle agent and machine per minute while the server pushes
expect(lead.lists).toBe(2)
}, 60_000)
})
+
+// Task #3398: a crew member the server put to sleep, moved, or whose folder it dropped is closed on its host, once, and never
+// kept alive; an awake one is left to the keep-alive.
+describe('a crew member at rest', () => {
+ const crewRow = (agent: Record<string, unknown> = {}) => ({ tabId: 'tab', active: true,
+ agent: { id: 'agent', harnessId: 'harness', machineId: 'machine-1', crew: { role: 'Engineer', at: 1, by: 'owner' }, ...agent } })
+ it('closes an asleep member\'s live session once, keeps its folder, and never reopens it', async () => {
+ const t = lifeFixture([crewRow({ asleep: { since: 1 } })])
+ t.live(true)
+ await t.poll()
+ await t.poll(1_000)
+ await t.poll(60_000)
+ expect(t.closeMember).toHaveBeenCalledTimes(1)
+ expect(t.closeMember).toHaveBeenCalledWith('harness')
+ expect(t.keepLeadAlive).not.toHaveBeenCalled()
+ })
+ it('closes a moving member and one whose folder is dropped; an awake member is not closed', async () => {
+ for (const agent of [{ movedTo: { machineId: 'mini', startId: 's', reason: 'offline', at: 1 } }, { asleep: { since: 1, folderDropped: true } }]) {
+ const t = lifeFixture([crewRow(agent)])
+ t.live(true)
+ await t.poll()
+ expect(t.closeMember).toHaveBeenCalledWith('harness')
+ }
+ const awake = lifeFixture([crewRow()])
+ awake.live(true)
+ await awake.poll()
+ expect(awake.closeMember).not.toHaveBeenCalled()
+ })
+ it('after a host restart, an awake member whose folder was dropped gets it back before it reopens (security review of #3229)', async () => {
+ const t = lifeFixture([crewRow()])
+ mkdirSync(join(t.core.dataDir, 'super-tabs'), { recursive: true })
+ writeFileSync(join(t.core.dataDir, 'super-tabs', 'crew-folders-machine-1.json'),
+ JSON.stringify({ version: 1, folders: [{ tabId: 'tab', harnessId: 'harness', path: '/nowhere/ada', source: '/nowhere/clone' }] }))
+ const restore = vi.spyOn(CrewFolders.prototype, 'restore').mockResolvedValue('restored')
+ await t.poll()
+ expect(restore).toHaveBeenCalledWith('tab', 'harness')
+ restore.mockRestore()
+ })
+ it('a folder drop the removal refused is tried again an hour later (review of #3229)', async () => {
+ const t = lifeFixture([crewRow({ asleep: { since: 1, folderDropped: true } })])
+ const drop = vi.spyOn(CrewFolders.prototype, 'drop').mockResolvedValue('kept')
+ await t.poll()
+ await t.poll(60_000)
+ expect(drop).toHaveBeenCalledTimes(1)
+ await t.poll(60 * 60_000)
+ expect(drop).toHaveBeenCalledTimes(2)
+ drop.mockRestore()
+ })
+ it('a woken member whose folder is coming back is not reopened until it is there (review of #3229)', async () => {
+ const t = lifeFixture([crewRow()])
+ mkdirSync(join(t.core.dataDir, 'super-tabs'), { recursive: true })
+ writeFileSync(join(t.core.dataDir, 'super-tabs', 'crew-folders-machine-1.json'),
+ JSON.stringify({ version: 1, folders: [{ tabId: 'tab', harnessId: 'harness', path: '/nowhere/ada', source: '/nowhere/clone' }] }))
+ let finish!: (value: 'restored') => void, back = false
+ // As the real restore does, a folder brought back is no longer remembered.
+ const remembers = vi.spyOn(CrewFolders.prototype, 'remembers').mockImplementation(() => !back)
+ const restore = vi.spyOn(CrewFolders.prototype, 'restore').mockImplementation(() => new Promise(resolve => { finish = value => { back = true; resolve(value) } }))
+ await t.poll()
+ await t.poll(10_000)
+ expect(restore).toHaveBeenCalledTimes(1)
+ expect(t.keepLeadAlive).not.toHaveBeenCalled()
+ finish('restored')
+ await t.poll(10_000)
+ expect(t.keepLeadAlive).toHaveBeenCalledWith('harness')
+ restore.mockRestore()
+ remembers.mockRestore()
+ })
+ it('a restore that keeps failing never holds the keep-alive back for good (review of #3229)', async () => {
+ const t = lifeFixture([crewRow()])
+ const remembers = vi.spyOn(CrewFolders.prototype, 'remembers').mockReturnValue(true)
+ const restore = vi.spyOn(CrewFolders.prototype, 'restore').mockResolvedValue('failed')
+ await t.poll()
+ await t.poll(10_000)
+ expect(t.keepLeadAlive).toHaveBeenCalledWith('harness')
+ // Tried again after its wait, not on every roster.
+ const tries = restore.mock.calls.length
+ await t.poll(10_000)
+ expect(restore.mock.calls.length).toBe(tries)
+ await t.poll(60_000)
+ expect(restore.mock.calls.length).toBe(tries + 1)
+ restore.mockRestore()
+ remembers.mockRestore()
+ })
+ it('a member woken and put to sleep again is closed again', async () => {
+ const rows = [crewRow({ asleep: { since: 1 } })]
+ const t = lifeFixture(rows)
+ t.live(true)
+ await t.poll()
+ rows[0] = crewRow()
+ await t.poll(1_000)
+ rows[0] = crewRow({ asleep: { since: 2 } })
+ await t.poll(1_000)
+ expect(t.closeMember).toHaveBeenCalledTimes(2)
+ })
+})
diff --git a/cli/src/superTabs/host.ts b/cli/src/superTabs/host.ts
index 20908e1ed..5cfea7d52 100644
--- a/cli/src/superTabs/host.ts
+++ b/cli/src/superTabs/host.ts
@@ -1,4 +1,11 @@
import { OBSERVER_VALID_MS, publishObservers } from './observers.js'
+import { CrewFolders, crewRest, pruneBranches } from './crewFolders.js'
+/** How often a crew member's finished branch copies are pruned (crewFolders.ts). */
+const CREW_PRUNE_EVERY_MS = 60 * 60_000
+/** How long a folder drop the Swarm's removal refused waits before it is tried again. */
+const CREW_DROP_RETRY_MS = 60 * 60_000
+/** How long a folder restore that failed waits before it is tried again. */
+const CREW_RESTORE_RETRY_MS = 60_000
import { ContributedMachines, uncommittedIn, type MachineDeps } from './machines.js'
import { SwarmGrids } from './swarmGrids.js'
import { retainContributions } from './retention.js'
@@ -1167,6 +1174,29 @@ export class SuperTabHost {
void this.releaseWorker(target)
continue
}
+ // A crew member the server put to sleep, is moving, or whose folder it dropped (backend crewKeep.ts): closed here once,
+ // its folder kept or dropped, and never kept alive. An awake one may need its dropped folder back before it reopens.
+ const rest = crewRest(target.agent as Parameters<typeof crewRest>[0])
+ const restKey = `${target.tabId}:${target.agent.harnessId}`
+ if (rest) {
+ // A drop the Swarm's removal refused (work not committed or pushed) is tried again an hour on (review of #3229).
+ if (rest === 'drop' && this.rested.get(restKey) === 'drop' && (this.dropRetry.get(restKey) ?? Infinity) <= this.now()) {
+ this.rested.delete(restKey)
+ this.dropRetry.delete(restKey)
+ }
+ void this.crewRest(target, rest)
+ continue
+ }
+ // Rested here, or dropped before this host restarted (its record is on disk): the folder comes back first.
+ // One restore at a time, and the keep-alive below waits for it, so the session reopens in its folder, not a missing one.
+ // A failed one waits CREW_RESTORE_RETRY_MS, so the keep-alive (which backs off itself) is never held back for good.
+ if (!this.restoring.has(restKey) && (this.restoreRetryAt.get(restKey) ?? 0) <= this.now() && (this.rested.delete(restKey)
+ || ((target.agent as { crew?: unknown }).crew && this.crewFolders().remembers(target.tabId, target.agent.harnessId)))) {
+ this.restoring.set(restKey, this.crewFolders().restore(target.tabId, target.agent.harnessId).catch(() => 'failed' as const)
+ .then(result => { if (result === 'failed') this.restoreRetryAt.set(restKey, this.now() + CREW_RESTORE_RETRY_MS); else this.restoreRetryAt.delete(restKey) })
+ .finally(() => { this.restoring.delete(restKey) }))
+ }
+ if ((target.agent as { crew?: unknown }).crew) this.pruneCrewBranches(target.tabId, target.agent.harnessId)
if (row.lead === true || target.agent.switchLanded) {
if (row.lead === true) leads.add(target.agent.harnessId)
this.keepAlive(target.agent.harnessId)
@@ -1174,7 +1204,7 @@ export class SuperTabHost {
} else if (isCrewRow(row)) {
// A project's crew is kept alive as its PM is (task #3397): reopened in place, then told to continue its task.
leads.add(target.agent.harnessId)
- this.keepAlive(target.agent.harnessId, true)
+ if (!this.restoring.has(restKey)) this.keepAlive(target.agent.harnessId, true)
}
// Never adopt another live session as a replacement for the recorded harness.
if (!this.core.agents.byAgent(target.agent.harnessId) || !this.core.agents.terminalAvailable(target.agent.harnessId)) continue
@@ -1331,6 +1361,48 @@ export class SuperTabHost {
/** The owner, 2026-10-23: the Lead of an active project stays alive. One whose session is gone (a machine restart, a
* kill) is reopened on its own conversation; nothing is typed into it, so no old input is replayed. A failure backs
* off, so an engine that cannot start never spins. */
+ /** Crew members closed for rest, by tab and harness, with what closed them: closed once per rest, and when one wakes, its
+ * dropped folder comes back before its keep-alive reopens it (crewFolders.ts). */
+ private readonly rested = new Map<string, 'sleep' | 'drop' | 'moving'>()
+ /** A dropped folder being brought back, by the same key: its keep-alive waits for it (review of #3229). */
+ private readonly restoring = new Map<string, Promise<unknown>>()
+ /** When a failed folder restore is tried again, by the same key. */
+ private readonly restoreRetryAt = new Map<string, number>()
+ /** When a refused folder drop is tried again, by the same key. */
+ private readonly dropRetry = new Map<string, number>()
+ private crewFoldersInstance?: CrewFolders
+ private crewFolders(): CrewFolders {
+ return this.crewFoldersInstance ??= new CrewFolders({ dataDir: this.core.dataDir, machineId: this.machineId,
+ worktree: (tabId, harnessId) => this.contributed().worktreeOf(tabId, harnessId),
+ removeWorktree: (tabId, harnessId) => this.contributed().removeStoppedWorktree(tabId, harnessId), log: line => this.log(line) })
+ }
+ /** When each crew member's folder last had its finished branch copies pruned (crewFolders.ts pruneBranches): hourly. */
+ private readonly pruned = new Map<string, number>()
+ private pruneCrewBranches(tabId: string, harnessId: string): void {
+ const key = `${tabId}:${harnessId}`, now = this.now()
+ if (now - (this.pruned.get(key) ?? 0) < CREW_PRUNE_EVERY_MS) return
+ this.pruned.set(key, now)
+ const folder = this.contributed().worktreeOf(tabId, harnessId)?.path
+ if (folder) void pruneBranches(folder).then(gone => { if (gone.length) this.log(`Dropped ${gone.length} finished branch copies of a crew member`) },
+ () => this.log('Could not prune a crew member\'s branches'))
+ }
+ private async crewRest(row: HostAgent, rest: 'sleep' | 'drop' | 'moving'): Promise<void> {
+ const key = `${row.tabId}:${row.agent.harnessId}`
+ if (this.rested.get(key) === rest) return
+ this.rested.set(key, rest)
+ try {
+ if (this.core.agents.byAgent(row.agent.harnessId)) {
+ const closed = await this.core.agents.closeMember?.(row.agent.harnessId)
+ if (closed && 'error' in closed && closed.error) throw new Error(closed.error)
+ }
+ if (rest === 'drop' && await this.crewFolders().drop(row.tabId, row.agent.harnessId) === 'kept') this.dropRetry.set(key, this.now() + CREW_DROP_RETRY_MS)
+ } catch (error) {
+ // Tried again on the next roster: the server still says it rests.
+ this.rested.delete(key)
+ this.log(`Crew rest: ${error instanceof Error ? error.message.slice(0, 80) : typeof error}`)
+ }
+ }
+
private keepAlive(harnessId: string, crew = false): void {
const reopen = this.core.agents.keepLeadAlive
if (!reopen) return
diff --git a/cli/src/superTabs/machines.ts b/cli/src/superTabs/machines.ts
index 3ca2b3e01..85932fb45 100644
--- a/cli/src/superTabs/machines.ts
+++ b/cli/src/superTabs/machines.ts
@@ -1105,6 +1105,12 @@ export class ContributedMachines {
* after the weekly sweep's idle week, by that sweep's own rule (lib/worktreeSweep.ts): only a worktree this machine's
* record proves the Swarm made, with no session in it and nothing uncommitted. Its branch keeps every commit and goes
* only when they are all on other branches or the remote, as once its pull request merged. Anything else stays. */
+ /** The worktree a Swarm start made for [harnessId] here, from this machine's record: its folder and its project clone. */
+ worktreeOf(tabId: string, harnessId: string): { path: string; source: string } | null {
+ const record = readWorktrees(swarmWorktreesFile(this.core.dataDir, this.machineId)).find(w => w.tabId === tabId && w.harnessId === harnessId)
+ return record ? { path: record.path, source: record.source } : null
+ }
+
removeStoppedWorktree(tabId: string, harnessId: string): Promise<void> {
// Lead, 10-30: an ended session (host.ts endSession) and a stopped worker (releaseWorker) can ask at the same moment.
// Each waits for the one before it and then checks for itself: after a removal it finds no record; after one that
diff --git a/desktop/lib/super_tabs/backend_transport.dart b/desktop/lib/super_tabs/backend_transport.dart
index 889834777..b36fe0df4 100644
--- a/desktop/lib/super_tabs/backend_transport.dart
+++ b/desktop/lib/super_tabs/backend_transport.dart
@@ -17,7 +17,7 @@ const _id = r'[A-Za-z0-9_-]+';
final _swarmRoute = RegExp(
'^/api/super-tabs(?:'
'|/list|/invitations|/deleted'
- '|/$_tab/(?:parts|changes|commands|mentions|decisions|leaderboard|sharing'
+ '|/$_tab/(?:parts|changes|commands|mentions|decisions|leaderboard|scorecard|sharing'
'|routing-policy|groups|groups/$_id|join|join/preview|native'
'|activity|activity/read|threads|inbox|proposals'
'|reminders'
diff --git a/desktop/lib/super_tabs/manage_page.dart b/desktop/lib/super_tabs/manage_page.dart
index 27757785a..019defac7 100644
--- a/desktop/lib/super_tabs/manage_page.dart
+++ b/desktop/lib/super_tabs/manage_page.dart
@@ -15,6 +15,8 @@ import 'controller.dart';
import 'dialog_scope.dart';
import 'leaderboard/models_section.dart';
import 'leaderboard/reader.dart';
+import 'scorecard.dart';
+import 'scorecard_view.dart';
import 'message_row.dart' show SuperTabMessageRow;
import 'local_models.dart';
import 'machines.dart';
@@ -28,9 +30,9 @@ import 'swarm_switcher.dart' show PersonalSwarm;
/// What Manage shows (docs/plans/2026-11-02-quiet-swarm-design.md, screens 10,
/// 10b, 10c): what used to stand in the sidebar and the Swarm dropdown, one
/// click away. Devices is the devices pane the view already holds.
-enum SuperTabManageTab { machines, models, devices, swarm, backlog }
+enum SuperTabManageTab { machines, models, devices, swarm, backlog, scorecard }
-/// Machines, Models, Devices, Swarm, Backlog, under Manage's heading.
+/// Machines, Models, Devices, Swarm, Backlog, Scorecard, under Manage's heading.
class SuperTabManageTabs extends StatelessWidget {
const SuperTabManageTabs({
super.key,
@@ -69,6 +71,7 @@ class SuperTabManageTabs extends StatelessWidget {
if (devices) tab(SuperTabManageTab.devices, 'Devices'),
tab(SuperTabManageTab.swarm, 'Swarm'),
tab(SuperTabManageTab.backlog, 'Backlog'),
+ tab(SuperTabManageTab.scorecard, 'Scorecard'),
],
),
);
@@ -147,6 +150,7 @@ class SuperTabManagePage extends StatelessWidget {
now: now,
onOpenConversation: (id) => onOpenConversation?.call(id),
),
+ SuperTabManageTab.scorecard => _ManageScorecard(controller: controller),
},
),
);
@@ -411,3 +415,32 @@ class _ManageSwarmState extends State<_ManageSwarm> {
.firstOrNull
?.id;
}
+
+/// Manage's Scorecard: its reader lives while the tab shows.
+class _ManageScorecard extends StatefulWidget {
+ const _ManageScorecard({required this.controller});
+ final SuperTabController controller;
+
+ @override
+ State<_ManageScorecard> createState() => _ManageScorecardState();
+}
+
+class _ManageScorecardState extends State<_ManageScorecard> {
+ late final _reader = SuperTabScorecardReader(
+ tabId: widget.controller.tabId,
+ request: widget.controller.request,
+ );
+
+ @override
+ void dispose() {
+ _reader.dispose();
+ super.dispose();
+ }
+
+ @override
+ Widget build(BuildContext context) => SuperTabScorecardView(
+ projects: widget.controller.snapshot?.projects ?? const [],
+ projectId: widget.controller.selected?.projectId,
+ reader: _reader,
+ );
+}
diff --git a/desktop/lib/super_tabs/scorecard.dart b/desktop/lib/super_tabs/scorecard.dart
new file mode 100644
index 000000000..91810e60a
--- /dev/null
+++ b/desktop/lib/super_tabs/scorecard.dart
@@ -0,0 +1,193 @@
+import 'package:flutter/foundation.dart';
+
+import 'controller.dart';
+
+/// One project's scorecard for one local day, as the server computed it
+/// overnight from the Swarm's own records (backend superTabs/scorecard,
+/// task #3386).
+class SuperTabScorecardDay {
+ SuperTabScorecardDay.fromJson(Map<String, dynamic> row)
+ : projectId = row['projectId'] as String? ?? '',
+ on = row['on'] as String? ?? '',
+ figures = Map.unmodifiable(
+ (row['figures'] as Map?)?.cast<String, dynamic>() ?? const {},
+ );
+
+ final String projectId;
+
+ /// The local date, YYYY-MM-DD.
+ final String on;
+ final Map<String, dynamic> figures;
+
+ int? number(String key) => (figures[key] as num?)?.round();
+}
+
+/// "–" for no figure, "45m" under an hour, "3h10" or "10h" above.
+String scorecardDuration(int? minutes) {
+ if (minutes == null) return '–';
+ if (minutes < 60) return '${minutes}m';
+ final hours = minutes ~/ 60, rest = minutes % 60;
+ return rest == 0
+ ? '${hours}h'
+ : '${hours}h${rest.toString().padLeft(2, '0')}';
+}
+
+/// One row of the table: its label, the figure it reads and how it says it.
+class SuperTabScorecardRow {
+ const SuperTabScorecardRow(this.key, this.label, this._say);
+
+ /// The figure's name in the stored row, which also keys the row's widget.
+ final String key;
+ final String label;
+ final String Function(SuperTabScorecardDay day) _say;
+
+ /// The cell for [day]; a day with no row says "–", never 0.
+ String cell(SuperTabScorecardDay? day) => day == null ? '–' : _say(day);
+}
+
+String _count(SuperTabScorecardDay day, String key) =>
+ day.number(key)?.toString() ?? '–';
+
+/// The table's rows, in the mockup's order (task #3386).
+final scorecardRows = <SuperTabScorecardRow>[
+ SuperTabScorecardRow(
+ 'asksShipped',
+ 'Asks shipped',
+ (d) => _count(d, 'asksShipped'),
+ ),
+ SuperTabScorecardRow(
+ 'askToShipMedianMinutes',
+ 'Ask to ship (median)',
+ (d) => scorecardDuration(d.number('askToShipMedianMinutes')),
+ ),
+ SuperTabScorecardRow(
+ 'oldestUnansweredAskMinutes',
+ 'Oldest unanswered ask',
+ (d) => scorecardDuration(d.number('oldestUnansweredAskMinutes')),
+ ),
+ SuperTabScorecardRow(
+ 'idleMinutes',
+ 'Nobody working, work waiting',
+ (d) => scorecardDuration(d.number('idleMinutes')),
+ ),
+ SuperTabScorecardRow(
+ 'lostLeftAlone',
+ 'Lost tasks left >10 min',
+ (d) => _count(d, 'lostLeftAlone'),
+ ),
+ SuperTabScorecardRow(
+ 'fixesAfterMerge',
+ 'Fixes after merge',
+ (d) => _count(d, 'fixesAfterMerge'),
+ ),
+ SuperTabScorecardRow(
+ 'corrections',
+ 'Corrections of the PM',
+ (d) => _count(d, 'corrections'),
+ ),
+ SuperTabScorecardRow(
+ 'starts',
+ 'Agent starts (failed)',
+ (d) => '${_count(d, 'starts')} (${_count(d, 'startsFailed')})',
+ ),
+ SuperTabScorecardRow(
+ 'routeDecisions',
+ 'Route decisions',
+ (d) => _count(d, 'routeDecisions'),
+ ),
+ SuperTabScorecardRow(
+ 'typedIntoPm',
+ 'Typed into the PM',
+ (d) => _count(d, 'typedIntoPm'),
+ ),
+ SuperTabScorecardRow(
+ 'ackPosts',
+ 'Ack-only posts',
+ (d) => _count(d, 'ackPosts'),
+ ),
+];
+
+/// The table's columns: [newest] and the 6 days before it, newest first.
+List<String> scorecardColumns(String newest) {
+ final day = DateTime.parse('${newest}T00:00:00Z');
+ return [
+ for (var i = 0; i < 7; i++)
+ day.subtract(Duration(days: i)).toIso8601String().substring(0, 10),
+ ];
+}
+
+/// Reads one project's stored days when shown or when the project changes:
+/// they change once a night, so nothing polls.
+class SuperTabScorecardReader extends ChangeNotifier {
+ SuperTabScorecardReader({required this.tabId, required this.request});
+
+ final String tabId;
+ final SuperTabRequest request;
+
+ List<SuperTabScorecardDay> days = const [];
+
+ /// The last day the project should have a row for (yesterday in its zone),
+ /// as the server says it; the table ends there even when that row is
+ /// missing.
+ String? through;
+ String? error;
+ bool loading = false;
+ String? _projectId;
+ int _read = 0;
+ bool _disposed = false;
+
+ String? get projectId => _projectId;
+
+ Future<void> show(String projectId) {
+ if (projectId != _projectId) {
+ _projectId = projectId;
+ days = const [];
+ through = null;
+ error = null;
+ }
+ return refresh();
+ }
+
+ Future<void> refresh() async {
+ final projectId = _projectId;
+ if (projectId == null || _disposed) return;
+ final read = ++_read;
+ loading = true;
+ _notify();
+ try {
+ final answer = await request(
+ 'GET',
+ '/api/super-tabs/${Uri.encodeComponent(tabId)}/scorecard'
+ '?projectId=${Uri.encodeQueryComponent(projectId)}',
+ );
+ if (read != _read) return;
+ days = [
+ for (final row in answer['days'] as List? ?? const [])
+ SuperTabScorecardDay.fromJson((row as Map).cast<String, dynamic>()),
+ ];
+ through = answer['through'] as String?;
+ error = null;
+ } on SuperTabFailure catch (failure) {
+ if (read != _read) return;
+ days = const [];
+ error = failure.statusCode == 404
+ ? 'The scorecard needs a newer Harness server.'
+ : failure.message;
+ } catch (_) {
+ if (read != _read) return;
+ error = "Couldn't read the scorecard.";
+ }
+ loading = false;
+ _notify();
+ }
+
+ void _notify() {
+ if (!_disposed) notifyListeners();
+ }
+
+ @override
+ void dispose() {
+ _disposed = true;
+ super.dispose();
+ }
+}
diff --git a/desktop/lib/super_tabs/scorecard_view.dart b/desktop/lib/super_tabs/scorecard_view.dart
new file mode 100644
index 000000000..477e0758f
--- /dev/null
+++ b/desktop/lib/super_tabs/scorecard_view.dart
@@ -0,0 +1,194 @@
+import 'package:flutter/material.dart';
+
+import '../shared/theme/app_theme.dart' as grid;
+import '../shared/widgets/app_select_field.dart';
+import '../widgets/desktop_chrome.dart';
+import '../widgets/laid_out_list.dart';
+import 'model.dart';
+import 'scorecard.dart';
+
+/// The widest the table grows.
+const _maxWidth = 960.0;
+const _labelWidth = 240.0;
+const _cellWidth = 72.0;
+const _rowHeight = 32.0;
+
+/// Manage ▸ Scorecard (task #3386): one project's figures for its last 7
+/// local days, newest first, so a change to how the Swarm works can be judged
+/// day over day. A day with no row says "–", never zeros.
+class SuperTabScorecardView extends StatefulWidget {
+ const SuperTabScorecardView({
+ super.key,
+ required this.projects,
+ required this.reader,
+ this.projectId,
+ });
+
+ final List<SuperTabProject> projects;
+ final SuperTabScorecardReader reader;
+
+ /// The project shown first: the open chat's, else the first.
+ final String? projectId;
+
+ @override
+ State<SuperTabScorecardView> createState() => _SuperTabScorecardViewState();
+}
+
+class _SuperTabScorecardViewState extends State<SuperTabScorecardView> {
+ String? _project;
+
+ @override
+ void initState() {
+ super.initState();
+ _project = widget.projects.any((p) => p.id == widget.projectId)
+ ? widget.projectId
+ : widget.projects.firstOrNull?.id;
+ if (_project case final project?) widget.reader.show(project);
+ }
+
+ void _choose(String? project) {
+ if (project == null || project == _project) return;
+ setState(() => _project = project);
+ widget.reader.show(project);
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ grid.AppTheme.watch(context);
+ final muted = DesktopChrome.metadata(color: DesktopChrome.muted);
+ return ListenableBuilder(
+ listenable: widget.reader,
+ builder: (context, _) {
+ final reader = widget.reader;
+ final byDay = {for (final d in reader.days) d.on: d};
+ // Yesterday in the project's zone, so a missing last day shows as
+ // "–"; the newest stored day before a server said so.
+ final newest =
+ reader.through ??
+ (reader.days.isEmpty
+ ? null
+ : reader.days
+ .map((d) => d.on)
+ .reduce((a, b) => a.compareTo(b) > 0 ? a : b));
+ final columns = newest == null
+ ? const <String>[]
+ : scorecardColumns(newest);
+ return Align(
+ alignment: Alignment.topCenter,
+ child: ConstrainedBox(
+ constraints: const BoxConstraints(maxWidth: _maxWidth),
+ child: LaidOutListView(
+ padding: const EdgeInsets.symmetric(
+ horizontal: grid.AppDesktop.groupGap,
+ vertical: grid.AppDesktop.controlGap,
+ ),
+ children: [
+ Row(
+ children: [
+ Text('Project', style: DesktopChrome.control()),
+ const SizedBox(width: grid.AppDesktop.controlGap),
+ AppSelectField<String?>(
+ key: const ValueKey('super-scorecard-project'),
+ semanticLabel: 'Project',
+ width: AppSelectField.fitWidth(
+ context,
+ labels: [for (final p in widget.projects) p.name],
+ ),
+ value: _project,
+ options: [
+ for (final p in widget.projects)
+ SelectOption(value: p.id, label: p.name),
+ ],
+ onChanged: _choose,
+ ),
+ ],
+ ),
+ const SizedBox(height: grid.AppDesktop.groupGap),
+ if (reader.error case final error?)
+ Text(
+ error,
+ key: const ValueKey('super-scorecard-error'),
+ style: muted,
+ )
+ else if (columns.isEmpty)
+ Text(
+ reader.loading ? '' : 'No scorecard yet. The first is computed after midnight in the project\'s time zone.',
+ key: const ValueKey('super-scorecard-empty'),
+ style: muted,
+ )
+ else
+ SingleChildScrollView(
+ scrollDirection: Axis.horizontal,
+ child: Column(
+ crossAxisAlignment: CrossAxisAlignment.start,
+ children: [
+ _Line(
+ label: '',
+ cells: [for (final on in columns) on.substring(5)],
+ style: muted,
+ ),
+ for (final row in scorecardRows)
+ _Line(
+ key: ValueKey('scorecard-${row.key}'),
+ label: row.label,
+ cells: [
+ for (final on in columns) row.cell(byDay[on]),
+ ],
+ style: DesktopChrome.control(),
+ divider: true,
+ ),
+ ],
+ ),
+ ),
+ ],
+ ),
+ ),
+ );
+ },
+ );
+ }
+}
+
+class _Line extends StatelessWidget {
+ const _Line({
+ super.key,
+ required this.label,
+ required this.cells,
+ required this.style,
+ this.divider = false,
+ });
+
+ final String label;
+ final List<String> cells;
+ final TextStyle style;
+ final bool divider;
+
+ @override
+ Widget build(BuildContext context) => Container(
+ height: _rowHeight,
+ decoration: divider
+ ? BoxDecoration(
+ border: Border(top: BorderSide(color: grid.AppPalette.divider)),
+ )
+ : null,
+ child: Row(
+ children: [
+ SizedBox(
+ width: _labelWidth,
+ child: Text(label, style: style, overflow: TextOverflow.ellipsis),
+ ),
+ for (final cell in cells)
+ SizedBox(
+ width: _cellWidth,
+ child: Text(
+ cell,
+ style: style.copyWith(
+ fontFeatures: const [FontFeature.tabularFigures()],
+ ),
+ textAlign: TextAlign.end,
+ ),
+ ),
+ ],
+ ),
+ );
+}
diff --git a/desktop/test/super_tab_backend_transport_test.dart b/desktop/test/super_tab_backend_transport_test.dart
index ef599be60..0e96ce0fc 100644
--- a/desktop/test/super_tab_backend_transport_test.dart
+++ b/desktop/test/super_tab_backend_transport_test.dart
@@ -317,6 +317,8 @@ void main() {
('GET', '/api/super-tabs/tab1/mentions?state=waiting'),
('GET', '/api/super-tabs/tab1/decisions?state=open'),
('GET', '/api/super-tabs/tab1/leaderboard'),
+ // Manage ▸ Scorecard (#3386).
+ ('GET', '/api/super-tabs/tab1/scorecard?projectId=p1'),
('GET', '/api/super-tabs/tab1/sharing'),
('POST', '/api/super-tabs/tab1/sharing'),
('GET', '/api/super-tabs/tab1/routing-policy'),
diff --git a/desktop/test/super_tab_scorecard_test.dart b/desktop/test/super_tab_scorecard_test.dart
new file mode 100644
index 000000000..29a3f4198
--- /dev/null
+++ b/desktop/test/super_tab_scorecard_test.dart
@@ -0,0 +1,292 @@
+import 'dart:io';
+import 'dart:ui' as ui;
+
+import 'package:flutter/material.dart';
+import 'package:flutter/rendering.dart';
+import 'package:harness/shared/theme/app_theme.dart' as grid;
+import 'package:flutter_test/flutter_test.dart';
+import 'package:harness/super_tabs/controller.dart';
+import 'package:harness/super_tabs/manage_page.dart';
+import 'package:harness/super_tabs/model.dart';
+import 'package:harness/super_tabs/scorecard.dart';
+import 'package:harness/super_tabs/scorecard_view.dart';
+
+import 'box_render_preview_test.dart' show loadPreviewFonts;
+import 'support/super_tab_fixture.dart';
+
+/// Set to a folder to write the PR's screenshots there.
+final shots = Platform.environment['SUPER_TAB_SCORECARD_SHOTS'];
+
+Map<String, dynamic> _day(String on, Map<String, dynamic> figures) => {
+ 'projectId': 'p0',
+ 'on': on,
+ 'timeZone': 'Asia/Ho_Chi_Minh',
+ 'computedAt': 0,
+ 'figures': {
+ 'limits': {'lostAfterMinutes': 10, 'askLookbackDays': 7},
+ 'asksShipped': 0,
+ 'askToShipMedianMinutes': null,
+ 'askToShipMaxMinutes': null,
+ 'unansweredAsks': 0,
+ 'oldestUnansweredAskMinutes': null,
+ 'idleMinutes': 0,
+ 'lostLeftAlone': 0,
+ 'lostLeftMinutes': 0,
+ 'fixesAfterMerge': 0,
+ 'corrections': 0,
+ 'starts': 0,
+ 'startsFailed': 0,
+ 'routeDecisions': 0,
+ 'typedIntoPm': 0,
+ 'ackPosts': 0,
+ 'agentPosts': 0,
+ ...figures,
+ },
+};
+
+final _projects = [
+ SuperTabProject.fromJson(projectFixture('p0', 'openharness', 'chat-0')),
+ SuperTabProject.fromJson(projectFixture('p1', 'lampos', 'chat-1')),
+];
+
+void main() {
+ group('scorecard figures as the table says them', () {
+ test('minutes, counts with failures, and a day with no row', () {
+ expect(scorecardDuration(null), '–');
+ expect(scorecardDuration(45), '45m');
+ expect(scorecardDuration(190), '3h10');
+ expect(scorecardDuration(600), '10h');
+ final day = SuperTabScorecardDay.fromJson(
+ _day('2026-11-05', {'starts': 31, 'startsFailed': 3}),
+ );
+ expect(
+ scorecardRows
+ .firstWhere((r) => r.label == 'Agent starts (failed)')
+ .cell(day),
+ '31 (3)',
+ );
+ expect(scorecardRows.first.cell(null), '–');
+ });
+
+ test('columns are the 7 days back from the newest row', () {
+ expect(scorecardColumns('2026-11-05'), [
+ '2026-11-05',
+ '2026-11-04',
+ '2026-11-03',
+ '2026-11-02',
+ '2026-11-01',
+ '2026-10-31',
+ '2026-10-30',
+ ]);
+ });
+ });
+
+ group('the reader', () {
+ test('reads one project and says when the server is too old', () async {
+ final asked = <String>[];
+ var answer = <String, dynamic>{
+ 'days': [
+ _day('2026-11-05', {'asksShipped': 9}),
+ ],
+ };
+ Object? failure;
+ final reader = SuperTabScorecardReader(
+ tabId: 'tab',
+ request: (method, path, {body}) async {
+ asked.add('$method $path');
+ if (failure != null) throw failure;
+ return answer;
+ },
+ );
+ await reader.show('p0');
+ expect(asked, ['GET /api/super-tabs/tab/scorecard?projectId=p0']);
+ expect(reader.days.single.figures['asksShipped'], 9);
+ failure = const SuperTabFailure(
+ 'NOT_FOUND',
+ 'Not found',
+ statusCode: 404,
+ );
+ answer = const {};
+ await reader.refresh();
+ expect(reader.days, isEmpty);
+ expect(reader.error, 'The scorecard needs a newer Harness server.');
+ });
+ });
+
+ testWidgets(
+ 'a newest day with no row shows as a dash, not a shorter table (review of #3212, 3)',
+ (tester) async {
+ final reader = SuperTabScorecardReader(
+ tabId: 'tab',
+ request: (method, path, {body}) async => {
+ // Yesterday (11-05) failed every try: only 11-04 is stored.
+ 'through': '2026-11-05',
+ 'days': [
+ _day('2026-11-04', {'asksShipped': 7}),
+ ],
+ },
+ );
+ addTearDown(reader.dispose);
+ await tester.pumpWidget(
+ MaterialApp(
+ home: Scaffold(
+ body: SuperTabScorecardView(
+ projects: _projects,
+ projectId: 'p0',
+ reader: reader,
+ ),
+ ),
+ ),
+ );
+ await tester.pumpAndSettle();
+ final shipped = find.byKey(const ValueKey('scorecard-asksShipped'));
+ final cells = tester
+ .widgetList<Text>(
+ find.descendant(of: shipped, matching: find.byType(Text)),
+ )
+ .map((t) => t.data)
+ .toList();
+ expect(cells.take(3), ['Asks shipped', '–', '7']);
+ expect(find.text('11-05'), findsOneWidget);
+ },
+ );
+
+ testWidgets('Manage has a Scorecard tab', (tester) async {
+ SuperTabManageTab? chosen;
+ await tester.pumpWidget(
+ MaterialApp(
+ home: Scaffold(
+ body: SuperTabManageTabs(
+ selected: SuperTabManageTab.machines,
+ onChoose: (tab) => chosen = tab,
+ ),
+ ),
+ ),
+ );
+ await tester.tap(find.byKey(const ValueKey('super-manage-tab-scorecard')));
+ expect(chosen, SuperTabManageTab.scorecard);
+ });
+
+ testWidgets('shows the open project\'s last days, a missing day as a dash', (
+ tester,
+ ) async {
+ final reader = SuperTabScorecardReader(
+ tabId: 'tab',
+ request: (method, path, {body}) async => {
+ 'days': [
+ _day('2026-11-05', {
+ 'asksShipped': 9,
+ 'askToShipMedianMinutes': 190,
+ 'idleMinutes': 95,
+ }),
+ // 11-04 failed: no row.
+ _day('2026-11-03', {'asksShipped': 7}),
+ ],
+ },
+ );
+ await tester.pumpWidget(
+ MaterialApp(
+ home: Scaffold(
+ body: SuperTabScorecardView(
+ projects: _projects,
+ projectId: 'p0',
+ reader: reader,
+ ),
+ ),
+ ),
+ );
+ await tester.pumpAndSettle();
+ expect(find.text('Asks shipped'), findsOneWidget);
+ expect(find.text('11-05'), findsOneWidget);
+ expect(find.text('3h10'), findsOneWidget);
+ expect(find.text('1h35'), findsOneWidget);
+ final shipped = find.byKey(const ValueKey('scorecard-asksShipped'));
+ final cells = tester
+ .widgetList<Text>(
+ find.descendant(of: shipped, matching: find.byType(Text)),
+ )
+ .map((t) => t.data)
+ .toList();
+ expect(cells.take(4), ['Asks shipped', '9', '–', '7']);
+ reader.dispose();
+ });
+
+ group('screenshots', () {
+ setUpAll(() async {
+ if (shots != null) await loadPreviewFonts();
+ });
+ for (final brightness in Brightness.values) {
+ testWidgets('Manage ▸ Scorecard (${brightness.name})', (tester) async {
+ final picture = GlobalKey();
+ grid.AppTheme.brightness.value = brightness;
+ addTearDown(() => grid.AppTheme.brightness.value = Brightness.light);
+ await tester.binding.setSurfaceSize(const Size(1100, 560));
+ addTearDown(() => tester.binding.setSurfaceSize(null));
+ final reader = SuperTabScorecardReader(
+ tabId: 'tab',
+ request: (method, path, {body}) async => {
+ 'days': [
+ for (final (on, f) in const [
+ ('2026-11-05', [9, 190, 600, 95, 2, 1, 4, 31, 3, 28, 6, 12]),
+ ('2026-11-04', [7, 245, 360, 40, 1, 0, 2, 24, 1, 22, 3, 18]),
+ ('2026-11-02', [4, 300, 120, 0, 0, 0, 1, 12, 0, 10, 1, 9]),
+ ])
+ _day(on, {
+ 'asksShipped': f[0],
+ 'askToShipMedianMinutes': f[1],
+ 'oldestUnansweredAskMinutes': f[2],
+ 'idleMinutes': f[3],
+ 'lostLeftAlone': f[4],
+ 'fixesAfterMerge': f[5],
+ 'corrections': f[6],
+ 'starts': f[7],
+ 'startsFailed': f[8],
+ 'routeDecisions': f[9],
+ 'typedIntoPm': f[10],
+ 'ackPosts': f[11],
+ }),
+ ],
+ },
+ );
+ addTearDown(reader.dispose);
+ await tester.pumpWidget(
+ MaterialApp(
+ theme: grid.buildAppTheme(brightness: brightness),
+ builder: (context, child) =>
+ RepaintBoundary(key: picture, child: child),
+ home: Scaffold(
+ body: Column(
+ crossAxisAlignment: CrossAxisAlignment.start,
+ children: [
+ SuperTabManageTabs(
+ selected: SuperTabManageTab.scorecard,
+ onChoose: (_) {},
+ ),
+ Expanded(
+ child: SuperTabScorecardView(
+ projects: _projects,
+ projectId: 'p0',
+ reader: reader,
+ ),
+ ),
+ ],
+ ),
+ ),
+ ),
+ );
+ await tester.pumpAndSettle();
+ await tester.runAsync(() async {
+ final boundary =
+ picture.currentContext!.findRenderObject()!
+ as RenderRepaintBoundary;
+ final image = await boundary.toImage(pixelRatio: 2);
+ final bytes = await image.toByteData(format: ui.ImageByteFormat.png);
+ await Directory(shots!).create(recursive: true);
+ await File('$shots/scorecard-${brightness.name}.png')
+ .writeAsBytes(bytes!.buffer.asUint8List());
+ image.dispose();
+ });
+ }, skip: shots == null);
+ }
+ });
+}
diff --git a/docs/plans/2026-11-05-crew-sleep-wake-move-plan.md b/docs/plans/2026-11-05-crew-sleep-wake-move-plan.md
new file mode 100644
index 000000000..36831ffe0
--- /dev/null
+++ b/docs/plans/2026-11-05-crew-sleep-wake-move-plan.md
@@ -0,0 +1,236 @@
+# Crew members sleep when idle, wake in seconds, and move when a machine fails (task #3398) Plan
+
+> Worked by the Engineer, test first (project instructions: Engineer). No Execution Handoff.
+
+**Goal:** A crew member idle for an hour sleeps: its session closes and frees memory and its slot, and its folder stays.
+New work wakes it in the same folder in seconds. If its machine is offline over 5 minutes, signed out, out of quota for
+over an hour, or low on disk, it moves to the next fallback machine. It keeps the same name and gets a hand-off note, and
+its task goes back to the head of the queue. Folders and branch copies are cleaned up by four rules.
+
+**Architecture:**
+- **The rules are data.** One pure function, `crewKeep(state, now)` in `backend/src/superTabs/crewKeep.ts`, reads the
+ Swarm state (agents, machines' latest readings, groups) and returns actions: `sleep`, `move`, `dropFolder`,
+ `dropBranch`. Its thresholds are constants in `crewRules.ts` until the project rules record (Ledger's task) lands.
+- **Where it runs.** `sweepLiveness`, the per-Swarm sweep that already runs on machine reports, applies the actions at
+ most every 30 s per Swarm, in the same write. Every machine report triggers it, so a Swarm with any live machine is
+ checked. A Swarm whose machines are all offline has no machine to move to, so `worker.ts` needs no new loop.
+- **Sleep:** the server sets `Agent.asleep = { since }`. The host sees `sleep` on the row and closes the session with
+ `closeMember`, keeping its folder. Keeper's keep-alive skips asleep rows (agreed in #3397).
+- **Wake:** `work.assign` to an asleep member clears `asleep` in its own transaction. The host feed pushes the change
+ within a second, and Keeper's keep-alive resumes the engine's own conversation (`claude --resume`, `codex resume`) in
+ the same folder.
+- **Move:** the server sets `Agent.movedTo = { machineId, startId, reason, at }`. It puts the member's active task back
+ at the head of the queue (`placeAfter(…, null)`) with a lost reason `crew-moved`. It places a start on the fallback
+ machine through `admitMachineStart` (Team rules, room, engines), with the same name, role, engine, model and effort
+ and a `handoffNote`. On `agent.started` for that start, the new agent takes over: the crew flag, the display name, its
+ groups and its `movedFrom`. The old record leaves as a retired entry, which tells the old host (whenever it is back)
+ to close the session and drop the folder.
+- **Cleanup:** the server says what to drop and the host does it, refusing a folder with uncommitted or unpushed work,
+ as `sweepOne` already does.
+
+**Tech Stack:** TypeScript backend (vitest, integration suite in PR CI), CLI (vitest).
+
+**Spec:** task #3398 brief (anh Dee, Swarm redesign approved 11-05). It builds on #3397 (Keeper: `Agent.crew`,
+`isCrew`, crew keep-alive and retention) and is read by #3400 (Budget: dispatch counts an asleep member as free).
+
+## Decisions made while building (2026-11-05)
+
+These change the plan above where they differ.
+
+- **Fallback order (River):** the route's own order, as the project's Order and quota rule applies it: the same account
+ kind as the machine left, then the account whose weekly quota resets soonest, then the most left, then the fewest
+ agents working, then by name (`crewKeep.ts fallbacks`).
+- **"Out of quota for over an hour"** is read without a stored clock: a used-up window that won't reset within the hour.
+ Unknown quota is never trouble. A withdrawn contribution reads as offline, and the sweep never throws on it.
+- **Retiring the old session uses the existing removal.** A move's take-over calls `removeAgent`, whose close record
+ makes the old host close the session and drop its folder when clean, the same as any removed agent. That replaces the
+ plan's `crewRetired` list and its host command, and is the "member's folder goes on retire" rule.
+- **Branch copies are pruned by the host.** The server records no branch names for tasks. Each hour, in each crew
+ folder, the host deletes local branches that were pushed and then merged into the default branch, or whose remote
+ branch is gone. It never deletes the current branch, one tracking the default branch, or one never pushed. A
+ gone-upstream branch's tip is kept under `refs/harness/dropped/`, so no commit is lost. (`crewFolders.ts
+ pruneBranches`)
+- **Wake after a dropped folder:** before the drop, the host remembers the folder's path and clone. On wake, it brings
+ the folder back at the same path from the clone (`git worktree add --detach`) before the keep-alive resumes the
+ engine's conversation there.
+- **Wake grace:** `work.assign` on an asleep member resets `downSince` to now, so #3397's 5-minute reopen grace counts
+ from the wake and the new task is not lost while the session reopens.
+- **No new host row fields:** host rows already carry the whole agent record, so the host reads `asleep` and `movedTo`
+ there, as Keeper's keep-alive does.
+- **Task 4 contract for the Desktop machine row** (River opens it as its own task), from what clients already get:
+ - crew sessions: `state.agents` on the machine with `crew`;
+ - state: `asleep` (since), `movedTo` (moving), or awake;
+ - idle hours: `now - asleep.since` while asleep;
+ - free disk: `machines[].observation.resources.projectDiskFreeBytes ?? diskFreeBytes`.
+
+### After the security reviews (Curlew3, SecKeep)
+
+- **A move stays within its account kind.** A member in a Team account, or one in Team-written work, moves only to a Team
+ account. A main-account member moves only to a main account. With no such machine, it stays put and says so once. A
+ take-over never joins a conversation the new session is barred from, and marks what a Team session joins
+ (`barredFromTeamWritten`, `markTeamWritten`).
+- **A member in a Team account doesn't sleep.** There, the core's only member close deletes the session's own data
+ (`inCore/agents.ts ended`, `teamPurge.ts`), so a wake couldn't resume in place. Such a member stays awake under the
+ crew keep-alive. **Follow-up for the PM:** a member close that keeps the session needs a `cli/src/core` change,
+ outside this task's owned paths.
+- **A machine whose move start failed** (no token, no room) isn't picked for that member again for an hour
+ (`crewMoveRefused`). The move's start skips agent.start's repository-reach and push-origin checks; this wait keeps a
+ machine that can't reach the repository from being retried every sweep.
+- **Branch pruning covers only branches this member's own folder was on** (its HEAD's history). A person's own branches
+ and other agents' in the shared clone are never touched.
+- **A dropped folder is remembered on disk**, so a wake after a host restart still brings it back.
+- **Accepted as is:**
+ - A move's start is recorded as asked by the target machine's contributor, as the PM's own start is (leadStart.ts).
+ No system actor exists for starts, and their grant check at `agent.started` uses it.
+ - Server moves follow the server's rules (Team accounts, room, engines, project scope). They don't read the project's
+ free-text routing instructions, such as "no Codex on this Mac", which the server enforces nowhere. A machine kept
+ from a project (`machine.projects`) is enforced by `admitMachineStart`.
+ - Waking clears `asleep` whatever closed the session meanwhile. For crew, any closed session is reopened by the
+ keep-alive anyway, and removing an agent removes its record.
+ - The old agent removed by a take-over is not brought back when its machine returns, because `removeAgent` drops its
+ restore record.
+
+### After the review (ReviewSleep, River)
+
+- **A moved member's task goes to the head of the list when the move is placed**, before the old session can lose it
+ (liveness loses it after 2 minutes offline, or when the old host closes the session). The task counts if it is
+ active, or if its machine took it from the member within the last hour (`lostTask`). The take-over also puts at the
+ head a task the old session lost after the move began.
+- **Members with no work stay put.** An asleep member never moves. An awake member with no work on a machine that went
+ offline (a laptop closed for the night) stays too. It moves once it holds work.
+- **Machine rules come from the project rules record only** (Ledger's task; River, 11-05). They cover a machine's
+ away hours and the engines it takes no move of, matched by machine id. People's machines and hours never go in this
+ public repository. The keeper takes them as input (`CrewDeps.machineRules`), and until the record is on main, no
+ machine rule applies to moves.
+- **A take-over marks nothing Team-written** (Curlew3). Since #3198 a mark is permanent. The barred check keeps
+ main-account sessions out, and a push read marks real Team code.
+- **The Team-account facts** are read from the database only when some member needs another machine.
+- **A folder drop the removal refused** (work not committed or pushed) is tried again an hour later.
+- **Waits on #3222:** the keep-alive waits for a folder restore before it reopens, which builds on Keeper's crew
+ keep-alive in `host.ts`. This branch merges main once #3222 lands, then reruns the host tests.
+- **Platforms:** the backend runs on the server. The host code runs on macOS, Linux and Windows through core
+ close/resume and git (Git for Windows). It opens no window and starts no new long-lived process.
+
+## Global Constraints
+
+- **Owns:** `backend/src/superTabs/`, `backend/src/worker.ts` (unchanged by this plan) and `cli/src/superTabs/`.
+ Keeper's `crew.ts` files are not edited. Swarm code stays out of `cli/src/core`.
+- **Numbers**, in `crewRules.ts` only:
+
+ | Constant | Value |
+ | --- | --- |
+ | `SLEEP_IDLE_MS` | 1 h |
+ | `MOVE_OFFLINE_MS` | 5 min |
+ | `MOVE_QUOTA_OUT_MS` | 1 h |
+ | `MOVE_LOW_DISK_BYTES` | 5 GB |
+ | `FOLDER_ASLEEP_MS` | 7 days |
+ | `DISK_FLOOR_BYTES` | 10 GB |
+ | `RETIRED_KEEP_MS` | 7 days |
+ | `RETIRED_MAX` | 200 |
+
+- Never a PM (Lead) or a Buddy. A crew member with active work never sleeps.
+- A crew member is moved at most once per `MOVE_OFFLINE_MS`. A move with no admitting fallback changes nothing and
+ says so once in the project chat.
+- Lifecycle code is tested with fake machines and a fake host. No test signs the Mac out of the real CLI.
+- The PR body's first line is "Requested by dee through Harness." It gets one review and one security review.
+
+## Design
+
+- **Growth:** `asleep` and `movedTo`/`movedFrom` are small objects on the agent. `state.crewRetired` holds at most 200
+ entries and drops each after 7 days, or once the host confirms it.
+- **Concurrency:** the actions are written in the liveness sweep's own transaction, which conflicts with any command
+ touching the same parts and retries on the next pass. Wake is inside `work.assign`.
+- **Read cost:** no new read. The function runs on the state the sweep already loaded, and hosts get three optional row
+ fields: `sleep`, `dropFolder`, `dropBranches`.
+- **Scale:** O(crew members × machines) per sweep, a handful each. At 10x, the first warning is the "moved" notices per
+ hour in the project chat.
+- **Compatibility:** an older host ignores the new row fields. A sleeping member then stays open, which is today's
+ behaviour for crew. An older server sends none of them.
+- **Migration and rollback:** none. Absent fields mean awake and not moved.
+- **Audience:** only the server sets these fields; no command lets a client set `asleep` or `movedTo`.
+- **Identity:** a move's start is placed as the crew member's project's start, under the Team rules, never with the
+ machine owner's login.
+- **Failure:** no fallback, a refused start, or a host that can't drop a folder (dirty or unpushed) each say why once,
+ and change nothing else.
+- **Platforms:** the host code is platform-neutral (core close/resume, git). Windows follows the same path without new
+ processes.
+
+## Review Focus
+
+1. A member that wakes while its sleep close is still in flight must end up open: a `work.assign` clears `asleep`, and
+ the host never closes a row whose `sleep` has cleared.
+2. A move while the failed machine comes back: the old session must not keep working the task. Its row reads
+ `movedTo`, its host closes it, and its task was already taken back.
+3. Two moves for one member (flapping machine): `movedTo` set means no second move until that start ends.
+4. A folder with uncommitted or unpushed work is never deleted by any rule.
+5. A quota window read as "unknown" is not "out of quota".
+
+---
+
+### Task 1: Rules and the decision function (backend)
+
+**Files:**
+- Create `backend/src/superTabs/crewRules.ts` and `crewKeep.ts`.
+- Modify `model.ts` (Agent `asleep?`, `movedTo?`, `movedFrom?`; state `crewRetired?`).
+- Test `crewKeep.test.ts`.
+
+**Interfaces:**
+- Produces `crewKeep(state: SuperTabState, now: number): CrewAction[]`, where `CrewAction` is
+ `{ kind: 'sleep', agentId }`, `{ kind: 'move', agentId, toMachineId, reason: 'offline'|'signed-out'|'quota'|'disk' }`,
+ `{ kind: 'dropFolder', agentId, why: 'asleep-7d'|'low-disk' }` or `{ kind: 'dropBranch', agentId, branch }`.
+- Consumes `isCrew(agent)` from #3397. Until #3397 merges, a local `crew?` read guards it.
+
+- [ ] Failing tests, using fake machines built from readings:
+ - idle 59 min stays awake, 61 min sleeps, and active work never sleeps;
+ - a Lead and a Buddy are never touched;
+ - offline 4 min stays, 6 min moves to the first admitting machine;
+ - signed out moves at once;
+ - quota at 100% for 59 min stays, 61 min moves, and unknown quota never moves;
+ - disk under 5 GB moves;
+ - no fallback gives no move;
+ - `movedTo` already set gives no second move;
+ - asleep 7 days drops the folder;
+ - disk under 10 GB drops the oldest asleep folder first, one per pass;
+ - a merged or closed PR of the member's task drops its branch.
+- [ ] Implement, run `npx vitest run src/superTabs/crewKeep.test.ts` green, and commit (test, then code).
+
+### Task 2: Apply the actions in the liveness sweep; wake on assign; move start and take-over (backend)
+
+**Files:**
+- Modify `liveness.ts` (`sweepLiveness` applies `crewKeep`), `commands.ts` (`work.assign` clears `asleep`;
+ `agent.started` takes over for a move start; `agent.crewRetired` from the host), `read/hostRows.ts` (row fields
+ `sleep`, `dropFolder`, `dropBranches`; retired entries for the host).
+- Test `crewKeep.apply.test.ts` (state-level) and `integration/superTabCrew.test.ts`.
+
+- [ ] Failing tests:
+ - the sweep sets `asleep`, and the host row carries `sleep`;
+ - `work.assign` wakes: `asleep` is cleared and the row's `sleep` is gone;
+ - a move re-queues the task at the head with reason `crew-moved`, places a start on the fallback with the same
+ name, role, engine and a hand-off note, and after `agent.started` the new agent has the crew flag, the same display
+ name, the old groups and `movedFrom`, while the old one is a retired entry for its host;
+ - a member is moved at most once until its move start ends;
+ - `agent.crewRetired` from the old host clears the entry.
+- [ ] Implement, run the files green, and commit.
+
+### Task 3: Host sleeps, drops folders and branches (CLI)
+
+**Files:**
+- Create `cli/src/superTabs/crewFolders.ts`.
+- Modify `host.ts` (row `sleep` → `closeMember` without removing the worktree; `dropFolder` and retired entries →
+ drop the folder; `dropBranches` → `git branch -D` when safe).
+- Test `crewFolders.spec.ts` and `host.spec.ts`.
+
+- [ ] Failing tests (fake core, temp git repos in `mktemp -d`):
+ - `sleep` closes the session and keeps the folder;
+ - a cleared `sleep` before the close starts closes nothing;
+ - `dropFolder` removes a clean worktree, and refuses a dirty one or one with unpushed commits, reporting why;
+ - a retired entry closes the session if it is open, drops the folder, and reports `agent.crewRetired`;
+ - `dropBranches` deletes a merged branch that isn't checked out and leaves the current one.
+- [ ] Implement, run the files green, and commit.
+
+### Task 4: Machine rows show crew sessions, free disk and idle hours
+
+- **Backend (this PR):** `machineRows` in the read view carries `crew: [{ name, asleep, idleHours }]` and
+ `diskFreeBytes` per machine. Test in `crewKeep.apply.test.ts`.
+- **Desktop:** the machine row shows them. It needs Flutter on a Mac, so it goes to the PM to give to a Desktop
+ Engineer against this field contract (mockup first).
diff --git a/docs/plans/2026-11-05-desktop-test-shards-plan.md b/docs/plans/2026-11-05-desktop-test-shards-plan.md
new file mode 100644
index 000000000..25c378d37
--- /dev/null
+++ b/docs/plans/2026-11-05-desktop-test-shards-plan.md
@@ -0,0 +1,53 @@
+# Desktop test shards by measured time Implementation Plan
+
+> **For agentic workers:** implemented the project's way (project instructions, Engineer): task by task, each failing test
+> committed before the code that passes it; one review on the pull request.
+
+**Goal:** Every Desktop CI shard finishes with at least 25% of its 10-minute limit left, and a shard that runs out of time
+names the files it did not reach.
+
+**Architecture:** `scripts/test-desktop.py` splits the VM test inventory by measured per-file durations kept in
+`scripts/desktop-test-durations.json` (longest first, each file to the least-loaded shard; unknown files get the median),
+instead of round-robin by name. Equal durations give the old round-robin split, so the shard verifier and its tests keep
+working. CI runs 5 shards instead of 4. `scripts/desktop-test-durations.py` refreshes the durations file from CI runs'
+shard artifacts. A timed-out attempt lists the files it did not finish, in the log, as a GitHub error annotation and in
+the receipt.
+
+**Tech Stack:** Python 3 (stdlib), GitHub Actions.
+
+**Spec:** Task #3458's brief (River, 11-05): shard 3 hit its 10-minute limit three times on 11-05 with tests passing.
+
+## Global Constraints
+
+- PR body first line: "Requested by Ken through Harness."
+- CI workflow and test scripts only, no app code. Owns `.github/workflows/`, `desktop/tool/`, `scripts/`.
+- Headroom: a shard's estimated time (its files' summed durations / 4 workers + startup) at most 75% of 600 s.
+- Job names stay `desktop-tests (ubuntu-22.04, N)`; adding shard 5 adds a job main's merge helper accepts (#2817).
+
+## Review Focus
+
+- A new test file with no measured duration: gets the median, never all piled on one shard.
+- The durations file missing or unreadable: falls back to equal weights (round-robin), never fails the run.
+- Every file in exactly one shard, the same split on every shard's runner (deterministic on inventory + durations).
+- A timed-out run whose last file was mid-way: that file is named as not finished.
+- The suite outgrowing 5 shards: a test fails before CI does.
+
+---
+
+### Task 1: Split by measured durations
+**Files:** `scripts/test-desktop.py` (partition, durations), `scripts/desktop-test-durations.json`,
+`scripts/tests/test_desktop_tests.py`.
+- [ ] Failing tests: weighted split balances heavy files; unknown files get the median; equal weights keep round-robin;
+ the committed durations + current inventory leave every one of 5 shards at most 75% of 600 s.
+- [ ] Code; tests pass; commit.
+
+### Task 2: Name the files a timed-out shard did not reach
+**Files:** `scripts/test-desktop.py`, `scripts/tests/desktop_vm_fixture.py` (a "partial" scenario),
+`scripts/tests/test_desktop_tests.py`.
+- [ ] Failing test: a run that times out after the first file names the second, in stdout and `unreached_files`.
+- [ ] Code; tests pass; commit.
+
+### Task 3: Refresh tool and 5 shards in CI
+**Files:** `scripts/desktop-test-durations.py`, `.github/workflows/ci.yml`, tests.
+- [ ] Failing test: the refresh tool computes per-file durations from a shard log and averages runs.
+- [ ] Code; CI matrix to 5 shards; three green runs with per-shard times in the PR.
diff --git a/docs/plans/2026-11-05-swarm-scorecard-plan.md b/docs/plans/2026-11-05-swarm-scorecard-plan.md
new file mode 100644
index 000000000..ff7514823
--- /dev/null
+++ b/docs/plans/2026-11-05-swarm-scorecard-plan.md
@@ -0,0 +1,85 @@
+# Daily Swarm scorecard — Plan
+
+**Goal:** Each night the server computes, for every project of every Swarm, one scorecard row for the project's local day,
+from the Swarm's own records. It stores the row and Desktop shows the last days as a small table, so each process change
+can be judged by numbers, day over day (anh Dee, task #3386).
+**Architecture:** A pure function turns one project's records for one local day into figures (`scorecard/figures.ts`). A
+reader gathers those records from Mongo on the secondaries (`scorecard/records.ts`). A worker pass every 15 minutes
+computes each finished day that has no row yet, for the last two days, and stores it (`scorecard/job.ts`, collection
+`SuperTabScorecard`). The read route returns rows the reader may see, and Desktop shows them under Manage → Scorecard.
+The first pass after release fills 11-04 and 11-05 by itself; the PR's baseline comes from the same code run read-only
+against production (`backend/scripts/scorecard-baseline.ts`).
+**Tech Stack:** Fastify backend, Prisma on MongoDB (raw finds on indexes), vitest; Flutter Desktop with memory-store tests.
+**Spec:** Task #3386 (Dee, P1). Engineer works this plan test-first itself, task by task, one PR.
+
+## Figures (one row per project and local day)
+
+| Figure | From |
+| --- | --- |
+| `asksShipped`, `askToShipMinutes` (median, max) | tasks (number, no `reviewOf`) whose `ledger.shippedAt` is in the day; `shippedAt − askedAt` |
+| `unansweredAsks`, `oldestUnansweredAskMinutes` (at day end) | people's top-level posts in the project chat in the 7 days before day end with no answer by then: no agent reply in their thread or `replyTo`, no delivery `answeredAt`, no task opened from them (`cards.line` / `ledger.proposal`) |
+| `idleMinutes` | minutes of the day with no task or step Working while one open task waits (no assignment, planned or lost), from each task's timeline: `ledger.runs[].at` (assigned, working), status rows (`source.status`), `work-lost` rows, cancel rows |
+| `lostLeftAlone`, `lostLeftMinutes` | `work-lost` rows of the day whose task got no next event (assignment, status, route, start) within 10 minutes; minutes past those 10 |
+| `fixesAfterMerge` | tasks whose `ledger.fixedAfterMergeAt` is in the day (the Swarm's record of a merged change that needed a fix: reverts) |
+| `corrections` | people's posts in the project's conversations that reply to (or quote) a post of the project's PM |
+| `starts`, `startsFailed` | `SwarmStart` rows of the project's conversations requested in the day; failed = `data.error` |
+| `routeDecisions` | `SuperTabRouteDecision` rows of the project's conversations created in the day |
+| `typedIntoPm` | `terminal-input` rows naming the PM's agent, created in the day |
+| `ackPosts`, `agentPosts` | agents' own posts (no `source`) of the day; ack = text that is only an acknowledgement ("on it", "taken", "confirmed", "thanks", "done", …) |
+
+The limits each figure uses (10 minutes for a lost task, 7 days for an unanswered ask) are written into each row with
+its figures, so a row says what it measured and a later change of limit never reinterprets an old row.
+
+## Design (feature design rules)
+
+- **Growth:** one row per project per local day, about 1 KB. Rows older than 120 days are deleted by the job's own
+ pass (`deleteMany` on `[tabId, projectId, on]`); a Swarm's rows go with its purge (`purge.ts PURGED_BY_TAB_ID`).
+- **Concurrency:** the job runs only in the singleton worker, one pass at a time. The write is an upsert by the row's
+ stable id (`stableId('scorecard', tabId, projectId, on)`), so a pass that runs twice writes the same row once.
+- **Read cost:** the job reads only groups active in the day (`[tabId, projectId, dormant, lastActivityAt, key]`), each
+ group's messages backwards from its newest until the day starts plus one earlier status row (capped at 5,000 rows a
+ task, 20,000 for the project chat's 7-day ask lookback), route decisions by `[tabId, createdAt]`, starts by `[tabId, changedAt]`, deliveries by `[tabId, messageId]`.
+ All on the secondaries. Candidates: one `$group` over `SwarmGroup [dormant, lastActivityAt]` (groups active in the last
+ 7 days), read again every 3 hours. The Desktop read is one
+ indexed find of at most 14 rows per project.
+- **Scale:** 10x projects = 10x one small pass per project per day, spread over the night by time zone. The warning
+ metric is the pass's logged duration and `capped` count (a group that hit the 5,000-row cap).
+- **Compatibility:** new collection, new route, new Desktop tab. An older Desktop never asks; a new Desktop on an older
+ backend gets 404 and shows "The scorecard needs a newer Harness server."
+- **Migration + rollback:** no migration. Rollback: revert the PR; the collection is left and can be dropped.
+- **Audience:** the server returns only projects the reader may read (`canReadProject`); Team sessions are refused, as
+ for the leaderboard. Rows hold counts and minutes only: no text, names or ids.
+- **Identity:** the job reads as the server; no person's login is used.
+- **Failure:** a project whose computation fails is logged with its Swarm and project ids and retried on the next pass
+ (at most 4 times a night); its day then shows as missing in the table, not as zeros.
+- **Platforms:** backend (Linux). Desktop on macOS, Windows, Linux, web through the shared Flutter screen.
+
+## Global Constraints
+
+- Swarm code stays in `backend/src/superTabs/` and `desktop/lib/super_tabs/` (plus the one route line, the worker
+ start, the schema model and the purge list). Nothing in `cli/src/core`.
+- No new refusal of any command; the job only reads and writes its own rows.
+- No switch: it ships on.
+- Desktop tests use a memory store and a fake sign-in; nothing signs the Mac's CLI out.
+
+## Review Focus
+
+- Each figure's definition against fixture records (edge cases at day bounds and time zones).
+- The job reads only indexed paths and has bounded pages; no read of a whole Swarm's history.
+- The read route's audience check.
+
+## Tasks
+
+- [ ] 1. `scorecard/figures.ts` `scorecardDay(input)`: tests in `scorecard/figures.test.ts`, one per figure, from fixture
+ groups, message rows, starts and route decisions (failing first), including day-bound and time-zone cases.
+- [ ] 2. Cancel rows carry `status: 'cancelled'` in their source (`commands.ts work.cancel`) so the timeline sees a cancel:
+ test in `commands.test.ts`.
+- [ ] 3. `scorecard/records.ts` gathers one project's day from Mongo; `scorecard/store.ts` writes, prunes and reads rows;
+ Prisma model `SuperTabScorecard`, purge list entry. Tests against the test database (`scorecard/records.test.ts`).
+- [ ] 4. `scorecard/job.ts` `startScorecards`: due days per project time zone, candidates, settle set, retries; started
+ in `worker.ts`. Tests with a fake clock (`scorecard/job.test.ts`).
+- [ ] 5. Read route `GET /api/super-tabs/:tabId/scorecard?projectId=&days=` through `read/service.ts`: audience test.
+- [ ] 6. Desktop: `super_tabs/scorecard.dart` reader and table, Manage → Scorecard tab; widget tests with a memory
+ store (rows, missing day, older server), light and dark screenshots.
+- [ ] 7. `backend/scripts/scorecard-baseline.ts`: the same code read-only against production for 11-04 and 11-05;
+ table posted in the PR.
diff --git a/scripts/ci-plan.py b/scripts/ci-plan.py
index 642af37d3..6a21061f1 100644
--- a/scripts/ci-plan.py
+++ b/scripts/ci-plan.py
@@ -54,6 +54,9 @@ ALSO = {"cli/src/lib/ptyHost/": "pty", "cli/src/lib/ptyHostBackend": "pty",
# Any other file in scripts/ui-style-allowlist/ stays a shared input.
STYLE_ALLOWLISTS = {"scripts/ui-style-allowlist/desktop.json": "desktop", "scripts/ui-style-allowlist/mobile.json": "mobile"}
ALSO.update(STYLE_ALLOWLISTS)
+# The Desktop VM runner and the measured file times it splits the shards by (task #3458): a change to either runs those
+# shards, so a split that runs short cannot merge on process checks alone.
+ALSO.update({"scripts/test-desktop.py": "desktop", "scripts/desktop-test-durations.json": "desktop"})
# The desktop files the phone's tests read (mobile/test): the Swarm models the phone carries as copies or must parse alike
# (swarm/desktop_parity_test.dart), the words and helpers its other tests compare. A desktop-only change to one runs
# mobile-checks too; it once failed only in a batch, two batches later (River, 2026-11-03). test_ci_plan's
diff --git a/scripts/desktop-test-durations.json b/scripts/desktop-test-durations.json
new file mode 100644
index 000000000..d57c6ba03
--- /dev/null
+++ b/scripts/desktop-test-durations.json
@@ -0,0 +1,1048 @@
+{
+ "schema": 1,
+ "description": "Per-file durations of Desktop VM tests (seconds, median over the runs named), from each shard's JSON reporter log: a file's first test start to its last test end. scripts/test-desktop.py splits shards by them; refresh with scripts/desktop-test-durations.py.",
+ "sourceRuns": [
+ "https://github.com/autonomous-ai/openharness/actions/runs/42055879443",
+ "https://github.com/autonomous-ai/openharness/actions/runs/42062947953",
+ "https://github.com/autonomous-ai/openharness/actions/runs/42062373039",
+ "https://github.com/autonomous-ai/openharness/actions/runs/42061777210"
+ ],
+ "files": {
+ "test/about_section_test.dart": 13.4,
+ "test/account_device_detail_test.dart": 17.3,
+ "test/account_device_history_test.dart": 17.9,
+ "test/account_devices_test.dart": 18.2,
+ "test/account_menu_version_test.dart": 14.6,
+ "test/account_profile_sheet_shots_test.dart": 22.0,
+ "test/account_profile_sheet_test.dart": 26.6,
+ "test/activity_evidence_test.dart": 18.9,
+ "test/add_phone_dialog_test.dart": 27.2,
+ "test/add_phone_transport_test.dart": 22.8,
+ "test/advanced_agent_keyboard_test.dart": 33.3,
+ "test/agent_alert_banner_widget_test.dart": 20.2,
+ "test/agent_alerts_test.dart": 21.1,
+ "test/agent_change_test.dart": 28.8,
+ "test/agent_creation_recovery_test.dart": 43.7,
+ "test/agent_event_invalidation_test.dart": 20.2,
+ "test/agent_fork_state_test.dart": 8.2,
+ "test/agent_handoff_file_test.dart": 6.6,
+ "test/agent_last_used_test.dart": 7.4,
+ "test/agent_messages_commands_test.dart": 13.1,
+ "test/agent_messages_tab_test.dart": 10.6,
+ "test/agent_messages_test.dart": 3.0,
+ "test/agent_messages_view_test.dart": 5.3,
+ "test/agent_model_test.dart": 8.0,
+ "test/agent_pause_state_test.dart": 3.5,
+ "test/agent_picker_test.dart": 6.4,
+ "test/agent_prefetch_test.dart": 5.5,
+ "test/agent_purge_state_test.dart": 8.2,
+ "test/agent_rename_state_test.dart": 2.8,
+ "test/agent_restart_state_test.dart": 2.9,
+ "test/agent_stop_state_test.dart": 2.2,
+ "test/agent_unread_test.dart": 8.4,
+ "test/alert_sounds_test.dart": 1.8,
+ "test/api_client_test.dart": 2.5,
+ "test/api_connections_test.dart": 12.3,
+ "test/api_model_picker_test.dart": 12.6,
+ "test/app_choice_picker_test.dart": 3.0,
+ "test/app_dialog_responsiveness_test.dart": 2.5,
+ "test/app_focus_visible_agents_test.dart": 2.6,
+ "test/app_icon_button_spin_test.dart": 4.0,
+ "test/app_log_test.dart": 1.8,
+ "test/app_menu_test.dart": 8.8,
+ "test/app_select_field_review_test.dart": 5.0,
+ "test/app_select_field_test.dart": 8.9,
+ "test/app_submenu_hover_test.dart": 3.8,
+ "test/appearance_prefs_store_test.dart": 2.1,
+ "test/approval_sign_in_panel_test.dart": 5.0,
+ "test/asset_paths_test.dart": 29.2,
+ "test/attach_intent_test.dart": 8.9,
+ "test/attention_surfaces_test.dart": 2.6,
+ "test/background_pane_opacity_test.dart": 4.0,
+ "test/bearer_auth_interceptor_test.dart": 2.0,
+ "test/bios_navigation_test.dart": 3.3,
+ "test/boot_flow_widget_test.dart": 13.0,
+ "test/bootstrapping_screen_test.dart": 3.3,
+ "test/box_flows_test.dart": 16.6,
+ "test/box_polish_test.dart": 7.0,
+ "test/box_render_preview_test.dart": 3.5,
+ "test/browser_label_test.dart": 1.8,
+ "test/browser_login_test.dart": 1.7,
+ "test/browser_machine_actions_test.dart": 4.5,
+ "test/browser_menu_guard_test.dart": 4.7,
+ "test/buffered_log_test.dart": 3.2,
+ "test/bundled_font_licenses_test.dart": 2.1,
+ "test/button_hover_test.dart": 3.5,
+ "test/cancel_discards_worktree_test.dart": 4.3,
+ "test/cli_login_test.dart": 2.2,
+ "test/cli_transcript_test.dart": 2.5,
+ "test/clone_agent_test.dart": 4.7,
+ "test/clone_repository_dialog_test.dart": 5.4,
+ "test/cloud_agents_test.dart": 6.6,
+ "test/cloud_chat_pane_capture_test.dart": 5.2,
+ "test/codex_profiles_test.dart": 1.8,
+ "test/coding_memory_activity_test.dart": 2.1,
+ "test/coding_memory_connection_test.dart": 2.4,
+ "test/coding_memory_library_test.dart": 2.5,
+ "test/coding_memory_notebooks_test.dart": 2.2,
+ "test/coding_memory_owner_lifecycle_test.dart": 1.8,
+ "test/coding_memory_view_test.dart": 21.5,
+ "test/coding_model_test.dart": 1.8,
+ "test/color_palette_test.dart": 4.7,
+ "test/command_dock_interaction_test.dart": 10.9,
+ "test/command_picker_rendering_test.dart": 15.8,
+ "test/community_fork_storage_test.dart": 2.8,
+ "test/community_fork_test.dart": 3.5,
+ "test/community_hub_contract_test.dart": 2.0,
+ "test/community_output_check_test.dart": 3.4,
+ "test/community_publish_test.dart": 3.0,
+ "test/community_viewer_picture_test.dart": 2.5,
+ "test/companion_dial_test.dart": 3.4,
+ "test/companion_home_test.dart": 12.2,
+ "test/config_daemon_port_test.dart": 4.1,
+ "test/connect_trusted_test.dart": 2.2,
+ "test/connection_review_render_test.dart": 8.3,
+ "test/connectors_section_test.dart": 6.3,
+ "test/copied_files_test.dart": 2.0,
+ "test/crash_log_test.dart": 1.7,
+ "test/custom_background_test.dart": 5.7,
+ "test/daemon_off_test.dart": 21.9,
+ "test/daemon_pipe_record_test.dart": 2.2,
+ "test/daemon_readiness_test.dart": 2.0,
+ "test/daemon_review_render_test.dart": 24.4,
+ "test/daemon_tcp_port_test.dart": 1.8,
+ "test/daemon_trusted_pipe_test.dart": 1.7,
+ "test/daemon_workspace_test.dart": 22.7,
+ "test/daemons/daemon_brain_test.dart": 2.2,
+ "test/daemons/daemon_colors_test.dart": 2.7,
+ "test/daemons/daemon_face_test.dart": 3.0,
+ "test/daemons/daemon_lines_test.dart": 3.1,
+ "test/daemons/daemon_morph_test.dart": 1.9,
+ "test/daemons/illustrated_art_test.dart": 6.1,
+ "test/daemons/illustrated_identity_test.dart": 2.6,
+ "test/daemons/individual_art_test.dart": 3.6,
+ "test/daemons/pair_rules_file_test.dart": 2.4,
+ "test/daemons/plates_test.dart": 5.4,
+ "test/daemons/render_frames_test.dart": 3.0,
+ "test/daemons/zoo_preview_test.dart": 3.7,
+ "test/daemons/zoo_test.dart": 4.9,
+ "test/debug_section_test.dart": 5.5,
+ "test/desk_sync_test.dart": 6.0,
+ "test/desktop_account_transition_test.dart": 2.8,
+ "test/desktop_build_test.dart": 2.3,
+ "test/desktop_compact_launch_test.dart": 16.2,
+ "test/desktop_composer_layout_test.dart": 31.9,
+ "test/desktop_dialog_interaction_test.dart": 52.1,
+ "test/desktop_final_ux_test.dart": 7.3,
+ "test/desktop_guidance_layout_test.dart": 6.7,
+ "test/desktop_history_dialog_test.dart": 11.6,
+ "test/desktop_new_harness_edge_test.dart": 11.8,
+ "test/desktop_palette_layout_test.dart": 6.3,
+ "test/desktop_preview_compatibility_test.dart": 7.8,
+ "test/desktop_remote_folder_picker_test.dart": 17.1,
+ "test/desktop_resource_forms_edge_test.dart": 6.4,
+ "test/desktop_search_edges_test.dart": 8.6,
+ "test/desktop_search_polish_test.dart": 24.9,
+ "test/desktop_task_palette_test.dart": 7.8,
+ "test/desktop_updater_test.dart": 3.5,
+ "test/desktop_welcome_composer_test.dart": 18.4,
+ "test/desktop_window_test.dart": 1.9,
+ "test/desktop_workspace_tab_test.dart": 7.0,
+ "test/device_carry_test.dart": 5.2,
+ "test/device_daemon_wire_test.dart": 2.8,
+ "test/device_finder_test.dart": 8.3,
+ "test/device_form_test.dart": 5.3,
+ "test/device_notice_bands_shell_test.dart": 3.6,
+ "test/device_output_search_test.dart": 5.9,
+ "test/device_passage_test.dart": 6.5,
+ "test/device_pending_sync_test.dart": 2.6,
+ "test/device_viewer_notices_test.dart": 9.2,
+ "test/device_visit_test.dart": 10.6,
+ "test/device_window_activation_test.dart": 2.1,
+ "test/devices_controller_test.dart": 2.8,
+ "test/devices_harness_test.dart": 2.2,
+ "test/devices_hosts_test.dart": 2.3,
+ "test/devices_screen_test.dart": 8.5,
+ "test/devices_tab_test.dart": 6.8,
+ "test/dial_desk_test.dart": 1.7,
+ "test/dial_log_tail_test.dart": 2.0,
+ "test/dial_status_test.dart": 1.9,
+ "test/direct_auth_api_test.dart": 2.2,
+ "test/dsh_catalog_test.dart": 1.8,
+ "test/dsh_install_panel_test.dart": 3.9,
+ "test/e2ee/encrypted_down_types_test.dart": 5.5,
+ "test/e2ee/relay_session_crypto_test.dart": 3.8,
+ "test/e2ee/strict_down_test.dart": 1.8,
+ "test/engine_identity_test.dart": 2.7,
+ "test/entry_surfaces_render_test.dart": 26.5,
+ "test/environment_preflight_screen_test.dart": 3.7,
+ "test/environment_probe_lifecycle_test.dart": 1.6,
+ "test/environment_provisioner_test.dart": 2.5,
+ "test/environment_provisioner_windows_test.dart": 3.4,
+ "test/environment_recheck_timer_test.dart": 2.1,
+ "test/environment_setup_screen_test.dart": 6.2,
+ "test/essential_harness_test.dart": 2.1,
+ "test/experimental_features_test.dart": 3.9,
+ "test/export_logs_dialog_test.dart": 5.0,
+ "test/file_type_icon_test.dart": 2.5,
+ "test/finder/finder_controller_test.dart": 2.8,
+ "test/finder/finder_diff_rows_test.dart": 2.1,
+ "test/finder/finder_file_kind_test.dart": 2.4,
+ "test/finder/finder_language_test.dart": 1.8,
+ "test/finder/finder_new_harness_test.dart": 1.9,
+ "test/finder/finder_pane_panel_test.dart": 7.7,
+ "test/finder/finder_roots_test.dart": 2.7,
+ "test/finder/git_parsing_test.dart": 1.6,
+ "test/finder/local_finder_test.dart": 3.7,
+ "test/fingerprint_text_test.dart": 3.5,
+ "test/first_arrival_test.dart": 2.7,
+ "test/first_connect_test.dart": 14.6,
+ "test/first_task_test.dart": 1.8,
+ "test/first_workspace_test.dart": 20.5,
+ "test/folder_commands_test.dart": 1.9,
+ "test/fork_agent_test.dart": 2.9,
+ "test/fork_keyboard_test.dart": 13.2,
+ "test/friendly_desktop_test.dart": 8.6,
+ "test/generated_project_launch_test.dart": 2.2,
+ "test/git_worktree_failures_test.dart": 14.2,
+ "test/git_worktree_test.dart": 19.9,
+ "test/github_repository_vectors_test.dart": 1.6,
+ "test/grid_model_picker_test.dart": 11.3,
+ "test/grid_on_demand_test.dart": 5.3,
+ "test/grid_picture_test.dart": 4.4,
+ "test/group_a_identity_test.dart": 3.6,
+ "test/guest_window_test.dart": 2.5,
+ "test/harness_activity_test.dart": 2.3,
+ "test/harness_agent_selection_test.dart": 13.7,
+ "test/harness_attachments_test.dart": 3.8,
+ "test/harness_catalog_test.dart": 2.9,
+ "test/harness_cli_runner_test.dart": 1.2,
+ "test/harness_cli_runner_windows_test.dart": 1.7,
+ "test/harness_comments_test.dart": 4.9,
+ "test/harness_customize_test.dart": 26.2,
+ "test/harness_file_store_test.dart": 1.9,
+ "test/harness_join_guide_widget_test.dart": 3.2,
+ "test/harness_monitor_summary_test.dart": 3.0,
+ "test/harness_monitor_test.dart": 9.2,
+ "test/harness_monitor_visibility_test.dart": 1.6,
+ "test/harness_picker_entry_test.dart": 11.8,
+ "test/harness_placement_test.dart": 29.1,
+ "test/harness_resources_test.dart": 3.3,
+ "test/harness_session_picker_test.dart": 12.4,
+ "test/harness_sessions_test.dart": 2.0,
+ "test/harness_share_status_test.dart": 3.0,
+ "test/harness_start_page_test.dart": 6.3,
+ "test/harness_stats_test.dart": 1.9,
+ "test/harness_store_entry_test.dart": 13.2,
+ "test/harness_tab_test.dart": 2.5,
+ "test/harness_titles_test.dart": 2.0,
+ "test/held_creation_follow_test.dart": 5.3,
+ "test/held_launch_sync_test.dart": 2.9,
+ "test/hidden_process_test.dart": 1.5,
+ "test/hidden_widget_pollers_test.dart": 7.4,
+ "test/hidden_window_controllers_test.dart": 1.9,
+ "test/hidden_window_requests_test.dart": 1.9,
+ "test/hidden_window_rereads_test.dart": 2.6,
+ "test/hidden_window_swarm_test.dart": 2.5,
+ "test/home_dir_test.dart": 2.5,
+ "test/hub_usage_contract_test.dart": 1.8,
+ "test/hub_usage_test.dart": 2.7,
+ "test/icon_catalog_render_test.dart": 7.3,
+ "test/ime_echo_hold_test.dart": 1.7,
+ "test/in_window_menu_bar_test.dart": 13.4,
+ "test/installed_fonts_test.dart": 2.5,
+ "test/interactive_viewer_test.dart": 2.3,
+ "test/keyboard_practice_test.dart": 12.5,
+ "test/keymap_dispatch_test.dart": 2.2,
+ "test/keymap_edit_test.dart": 2.5,
+ "test/keymap_host_test.dart": 5.3,
+ "test/keymap_native_test.dart": 2.5,
+ "test/keymap_runtime_test.dart": 35.0,
+ "test/keymap_store_test.dart": 4.0,
+ "test/keymap_test.dart": 2.4,
+ "test/laid_out_list_test.dart": 5.4,
+ "test/launch_preferences_test.dart": 2.8,
+ "test/layout_palette_move_test.dart": 2.3,
+ "test/layout_palette_render_test.dart": 6.2,
+ "test/layout_palette_test.dart": 4.7,
+ "test/lead_placement_test.dart": 1.7,
+ "test/link_another_machine_test.dart": 8.5,
+ "test/link_machine_dialog_widget_test.dart": 7.8,
+ "test/link_machine_widget_test.dart": 4.7,
+ "test/link_prompt_revisit_test.dart": 1.5,
+ "test/link_retry_test.dart": 1.9,
+ "test/link_retry_transport_test.dart": 8.1,
+ "test/linux_app_image_test.dart": 2.2,
+ "test/local_cli_discovery_test.dart": 9.8,
+ "test/local_daemon_transport_test.dart": 2.8,
+ "test/local_git_projects_test.dart": 3.5,
+ "test/local_model_picker_test.dart": 9.1,
+ "test/local_stack_e2e_test.dart": 1.4,
+ "test/log_redact_test.dart": 1.6,
+ "test/log_stream_test.dart": 1.6,
+ "test/login_fleet_map_test.dart": 3.1,
+ "test/login_screen_phone_test.dart": 3.0,
+ "test/login_screen_test.dart": 4.4,
+ "test/loose_sheet_test.dart": 2.7,
+ "test/lucide_weight_test.dart": 3.0,
+ "test/machine_devices_picker_test.dart": 20.0,
+ "test/machine_devices_test.dart": 2.6,
+ "test/machine_edits_test.dart": 2.3,
+ "test/machine_inventory_api_test.dart": 2.0,
+ "test/machine_list_outage_test.dart": 8.2,
+ "test/machine_list_push_test.dart": 2.2,
+ "test/machine_list_sockets_test.dart": 2.1,
+ "test/machine_loading_test.dart": 2.3,
+ "test/machine_profile_test.dart": 1.2,
+ "test/machine_resource_monitor_test.dart": 2.8,
+ "test/machine_resources_test.dart": 2.0,
+ "test/machine_sharing_ended_test.dart": 2.0,
+ "test/machine_sharing_form_test.dart": 5.3,
+ "test/machine_tree_widget_test.dart": 2.0,
+ "test/machines_manager_keyboard_test.dart": 9.1,
+ "test/machines_manager_test.dart": 17.1,
+ "test/machines_menu_stopped_test.dart": 5.2,
+ "test/machines_menu_test.dart": 7.1,
+ "test/machines_panel_test.dart": 20.1,
+ "test/macos_microphone_permission_test.dart": 2.0,
+ "test/manual_update_check_test.dart": 4.9,
+ "test/measurement_controls_test.dart": 4.1,
+ "test/memory_companion_recovery_test.dart": 9.7,
+ "test/memory_review_text_test.dart": 3.6,
+ "test/model_manager_controller_test.dart": 3.2,
+ "test/model_mark_test.dart": 3.3,
+ "test/model_picker_chrome_test.dart": 4.0,
+ "test/model_search_catalog_test.dart": 2.0,
+ "test/model_use_test.dart": 2.5,
+ "test/models_menu_test.dart": 8.4,
+ "test/models_panel_test.dart": 14.9,
+ "test/move_pane_palette_test.dart": 4.2,
+ "test/native_picker_guard_test.dart": 1.5,
+ "test/native_toolbar_sync_test.dart": 11.6,
+ "test/native_webview_host_test.dart": 4.0,
+ "test/native_webview_runtime_test.dart": 2.4,
+ "test/new_agent_choices_layout_test.dart": 13.7,
+ "test/new_agent_codex_profile_test.dart": 14.8,
+ "test/new_agent_dialog_phone_test.dart": 6.5,
+ "test/new_agent_dsh_test.dart": 9.1,
+ "test/new_agent_engine_check_test.dart": 8.1,
+ "test/new_agent_permission_mode_test.dart": 10.2,
+ "test/new_agent_project_folder_test.dart": 6.7,
+ "test/new_agent_project_picker_test.dart": 8.9,
+ "test/new_agent_rows_and_task_test.dart": 12.0,
+ "test/new_agent_terminal_test.dart": 7.4,
+ "test/new_device_notice_test.dart": 5.3,
+ "test/new_harness_accessibility_test.dart": 5.7,
+ "test/new_harness_agent_settings_test.dart": 8.8,
+ "test/new_harness_attachments_test.dart": 2.0,
+ "test/new_harness_box_test.dart": 7.7,
+ "test/new_harness_controller_edges_test.dart": 2.3,
+ "test/new_harness_default_branch_test.dart": 4.2,
+ "test/new_harness_entry_rules_test.dart": 36.6,
+ "test/new_harness_form_test.dart": 16.7,
+ "test/new_harness_git_test.dart": 5.4,
+ "test/new_harness_grid_test.dart": 7.4,
+ "test/new_harness_guest_test.dart": 4.0,
+ "test/new_harness_install_e2e_test.dart": 8.9,
+ "test/new_harness_install_pane_test.dart": 3.6,
+ "test/new_harness_models_test.dart": 8.0,
+ "test/new_harness_paste_test.dart": 5.2,
+ "test/new_harness_phone_test.dart": 4.8,
+ "test/new_harness_project_context_test.dart": 6.1,
+ "test/new_harness_project_paste_test.dart": 4.7,
+ "test/new_harness_scenarios_test.dart": 23.5,
+ "test/new_harness_start_failures_test.dart": 4.3,
+ "test/new_repository_form_test.dart": 8.2,
+ "test/new_repository_launch_test.dart": 3.0,
+ "test/new_repository_test.dart": 1.8,
+ "test/new_tab_actions_test.dart": 6.5,
+ "test/notification_inbox_swarm_test.dart": 3.7,
+ "test/notification_inbox_test.dart": 9.2,
+ "test/notification_link_test.dart": 1.8,
+ "test/notification_parity_test.dart": 2.5,
+ "test/observer_codec_test.dart": 2.4,
+ "test/observer_transport_test.dart": 2.4,
+ "test/offline_machine_poll_test.dart": 2.1,
+ "test/open_in_browser_test.dart": 1.6,
+ "test/open_picker_rendering_test.dart": 11.3,
+ "test/orchestrator_controls_test.dart": 9.2,
+ "test/orchestrator_local_e2e_test.dart": 1.5,
+ "test/orchestrator_review_render_test.dart": 3.8,
+ "test/orchestrator_test.dart": 5.4,
+ "test/own_binding/connected_semantics_test.dart": 7.6,
+ "test/p2p/mobile_parity_test.dart": 1.6,
+ "test/p2p/p2p_sdp_test.dart": 2.1,
+ "test/p2p/terminal_p2p_plugin_edges_test.dart": 3.0,
+ "test/p2p/terminal_p2p_plugin_test.dart": 2.4,
+ "test/p2p/terminal_p2p_policy_test.dart": 1.9,
+ "test/pane_arrangement_test.dart": 1.8,
+ "test/pane_columns_test.dart": 5.0,
+ "test/pane_defaults_test.dart": 11.4,
+ "test/pane_drag_swap_test.dart": 8.1,
+ "test/pane_file_drop_test.dart": 5.6,
+ "test/pane_file_menu_test.dart": 5.2,
+ "test/pane_fixed_width_test.dart": 1.9,
+ "test/pane_focus_test.dart": 8.6,
+ "test/pane_grid_finder_test.dart": 3.2,
+ "test/pane_lattice_test.dart": 4.0,
+ "test/pane_launch_failure_test.dart": 7.2,
+ "test/pane_layout_choices_test.dart": 2.4,
+ "test/pane_machine_unknown_test.dart": 5.8,
+ "test/pane_menu_desktop_test.dart": 6.2,
+ "test/pane_menu_row_test.dart": 3.8,
+ "test/pane_message_mark_test.dart": 3.7,
+ "test/pane_model_status_test.dart": 9.1,
+ "test/pane_modes_test.dart": 1.5,
+ "test/pane_move_terminal_test.dart": 6.7,
+ "test/pane_move_to_tab_test.dart": 2.3,
+ "test/pane_needs_link_test.dart": 5.9,
+ "test/pane_phone_header_test.dart": 4.7,
+ "test/pane_pin_test.dart": 2.4,
+ "test/pane_preset_test.dart": 25.1,
+ "test/pane_reorder_test.dart": 2.2,
+ "test/pane_restart_test.dart": 5.1,
+ "test/pane_retry_test.dart": 2.8,
+ "test/pane_ring_test.dart": 2.0,
+ "test/pane_shortcuts_test.dart": 1.9,
+ "test/pane_status_words_test.dart": 4.7,
+ "test/pane_tab_cleanup_test.dart": 12.7,
+ "test/password_stretch_test.dart": 4.1,
+ "test/pending_question_test.dart": 2.3,
+ "test/personal_project_tabs_test.dart": 2.8,
+ "test/pet_settings_test.dart": 16.4,
+ "test/pet_source_test.dart": 3.1,
+ "test/phone_offer_test.dart": 9.6,
+ "test/phone_sign_in_test.dart": 2.2,
+ "test/pointer_words_test.dart": 2.4,
+ "test/privacy_section_test.dart": 4.3,
+ "test/process_responsibility_test.dart": 1.6,
+ "test/product_delivery_test.dart": 1.5,
+ "test/product_feedback_test.dart": 4.4,
+ "test/product_health_test.dart": 2.4,
+ "test/product_measurement_test.dart": 1.9,
+ "test/profile_cache_test.dart": 1.7,
+ "test/profile_startup_test.dart": 3.2,
+ "test/project_exists_test.dart": 4.7,
+ "test/project_folder_test.dart": 2.9,
+ "test/project_history_test.dart": 1.6,
+ "test/project_preview_test.dart": 2.0,
+ "test/prompt_context_test.dart": 2.9,
+ "test/prompt_editing_test.dart": 4.5,
+ "test/prompt_placeholder_test.dart": 2.2,
+ "test/pull_request_badge_test.dart": 4.4,
+ "test/quick_access_test.dart": 10.3,
+ "test/relative_time_test.dart": 2.0,
+ "test/remote_connect_measurement_test.dart": 4.5,
+ "test/remote_folder_picker_test.dart": 11.6,
+ "test/remote_media_download_test.dart": 2.2,
+ "test/remote_media_smoke_test.dart": 2.0,
+ "test/remote_password_state_test.dart": 2.2,
+ "test/remote_terminal_handoff_test.dart": 2.5,
+ "test/remote_usage_test.dart": 2.2,
+ "test/rename_agent_dialog_test.dart": 3.7,
+ "test/rename_keyboard_test.dart": 9.5,
+ "test/repository_clone_test.dart": 3.7,
+ "test/resource_picker_test.dart": 36.4,
+ "test/resource_preview_actions_test.dart": 6.3,
+ "test/restart_keyboard_test.dart": 10.4,
+ "test/resting_model_picker_test.dart": 7.3,
+ "test/resting_models_new_harness_test.dart": 2.2,
+ "test/resting_models_panel_test.dart": 5.6,
+ "test/resting_models_test.dart": 3.0,
+ "test/retake_taken_over_panes_test.dart": 1.8,
+ "test/retarget_refusal_test.dart": 2.1,
+ "test/route_answer_test.dart": 2.5,
+ "test/route_card_test.dart": 6.0,
+ "test/route_screen_test.dart": 8.3,
+ "test/route_send_test.dart": 3.4,
+ "test/runtime_model_name_test.dart": 1.8,
+ "test/search_result_text_test.dart": 2.1,
+ "test/search_when_test.dart": 2.0,
+ "test/search_workspace_test.dart": 9.8,
+ "test/select_field_typing_test.dart": 4.6,
+ "test/serial_port_lease_test.dart": 1.6,
+ "test/session_activity_test.dart": 4.2,
+ "test/session_close_dialog_test.dart": 5.7,
+ "test/session_close_test.dart": 20.1,
+ "test/session_content_search_test.dart": 3.8,
+ "test/session_git_context_test.dart": 2.1,
+ "test/session_preview_app_test.dart": 2.4,
+ "test/session_preview_test.dart": 2.3,
+ "test/session_search_daemon_e2e_test.dart": 1.6,
+ "test/session_search_rendering_test.dart": 9.2,
+ "test/session_tail_test.dart": 1.6,
+ "test/session_work_dialog_test.dart": 8.4,
+ "test/session_work_navigation_test.dart": 4.2,
+ "test/session_work_state_test.dart": 1.8,
+ "test/settings_account_copy_test.dart": 2.7,
+ "test/settings_close_keys_test.dart": 6.8,
+ "test/settings_compact_test.dart": 3.6,
+ "test/settings_review_render_test.dart": 18.2,
+ "test/settings_saved_super_tabs_test.dart": 4.6,
+ "test/settings_screen_test.dart": 11.2,
+ "test/settings_section_test.dart": 2.8,
+ "test/setup_measurement_transport_test.dart": 2.2,
+ "test/setup_preference_store_test.dart": 2.5,
+ "test/setup_privacy_flow_test.dart": 6.0,
+ "test/setup_progress_test.dart": 1.8,
+ "test/setup_review_render_test.dart": 6.5,
+ "test/setup_step_measurement_test.dart": 2.2,
+ "test/setup_tour_test.dart": 7.2,
+ "test/share_harness_test.dart": 6.3,
+ "test/share_toolbar_test.dart": 12.1,
+ "test/shared_agent_page_test.dart": 5.3,
+ "test/shared_harness_panel_test.dart": 8.7,
+ "test/shared_pane_layout_test.dart": 2.1,
+ "test/shared_tab_dialogs_test.dart": 9.4,
+ "test/shared_tab_invites_test.dart": 7.9,
+ "test/shared_tab_panel_test.dart": 13.7,
+ "test/shared_tab_panes_test.dart": 5.9,
+ "test/shared_tab_placement_test.dart": 1.9,
+ "test/shared_tabs_app_test.dart": 3.2,
+ "test/shared_tabs_controller_test.dart": 2.4,
+ "test/shared_tabs_desk_test.dart": 5.1,
+ "test/shared_terminal_watch_test.dart": 2.4,
+ "test/shortcuts_browser_test.dart": 19.5,
+ "test/shortcuts_deck_test.dart": 4.4,
+ "test/shortcuts_test.dart": 3.9,
+ "test/sign_in_measurement_test.dart": 2.3,
+ "test/sign_in_step_flow_test.dart": 2.5,
+ "test/sign_in_step_machine_line_test.dart": 5.6,
+ "test/sign_in_step_test.dart": 3.5,
+ "test/signed_out_polling_test.dart": 1.8,
+ "test/signin_recovery_test.dart": 4.4,
+ "test/signout_recovery_test.dart": 4.2,
+ "test/skeleton_test.dart": 2.2,
+ "test/sleep_aware_test.dart": 3.4,
+ "test/speed_hooks_test.dart": 5.2,
+ "test/speed_marks_test.dart": 2.2,
+ "test/spoken_task_test.dart": 2.6,
+ "test/ssh_hosts_test.dart": 1.9,
+ "test/started_child_pane_test.dart": 3.9,
+ "test/started_marker_test.dart": 1.6,
+ "test/startup_order_test.dart": 3.1,
+ "test/startup_test.dart": 2.1,
+ "test/status_line_test.dart": 4.0,
+ "test/status_menu_test.dart": 5.5,
+ "test/stop_keyboard_test.dart": 10.1,
+ "test/stopped_harness_test.dart": 9.4,
+ "test/stopped_tab_cleanup_test.dart": 5.7,
+ "test/store_collections_test.dart": 1.9,
+ "test/store_compact_test.dart": 10.4,
+ "test/store_controller_test.dart": 2.0,
+ "test/store_demo_dialog_test.dart": 4.0,
+ "test/store_discover_test.dart": 63.1,
+ "test/store_experiments_test.dart": 8.2,
+ "test/store_harness_actions_test.dart": 4.8,
+ "test/store_open_draft_test.dart": 8.0,
+ "test/store_page_test.dart": 17.0,
+ "test/store_screen_test.dart": 10.3,
+ "test/store_search_test.dart": 4.4,
+ "test/store_showcase_test.dart": 9.7,
+ "test/store_tab_state_test.dart": 5.2,
+ "test/store_update_e2e_test.dart": 1.5,
+ "test/store_viewers_test.dart": 3.6,
+ "test/super_tab_activity_lists_test.dart": 2.9,
+ "test/super_tab_activity_rows_test.dart": 1.8,
+ "test/super_tab_activity_test.dart": 3.6,
+ "test/super_tab_add_computers_test.dart": 8.4,
+ "test/super_tab_agent_group_picker_test.dart": 5.8,
+ "test/super_tab_agent_line_test.dart": 5.0,
+ "test/super_tab_agent_rename_test.dart": 4.2,
+ "test/super_tab_asks_shots_test.dart": 2.9,
+ "test/super_tab_attachment_fetch_test.dart": 2.0,
+ "test/super_tab_attachment_image_test.dart": 2.9,
+ "test/super_tab_attachments_test.dart": 14.3,
+ "test/super_tab_attention_shots_test.dart": 3.8,
+ "test/super_tab_attention_test.dart": 5.2,
+ "test/super_tab_attention_wave2_test.dart": 10.8,
+ "test/super_tab_avatar_test.dart": 4.2,
+ "test/super_tab_avatars_view_test.dart": 4.7,
+ "test/super_tab_backend_transport_test.dart": 14.2,
+ "test/super_tab_backlog_shots_test.dart": 2.5,
+ "test/super_tab_backlog_test.dart": 11.6,
+ "test/super_tab_big_chat_jump_test.dart": 100.3,
+ "test/super_tab_buddies_shots_test.dart": 2.3,
+ "test/super_tab_buddies_test.dart": 25.5,
+ "test/super_tab_buddy_chat_counts_test.dart": 9.4,
+ "test/super_tab_buddy_model_test.dart": 22.0,
+ "test/super_tab_buddy_own_machines_test.dart": 18.4,
+ "test/super_tab_buddy_sessions_shots_test.dart": 2.2,
+ "test/super_tab_buddy_sessions_test.dart": 28.4,
+ "test/super_tab_chat_edge_test.dart": 15.9,
+ "test/super_tab_chat_find_test.dart": 2.5,
+ "test/super_tab_chat_scroll_test.dart": 61.2,
+ "test/super_tab_chat_test.dart": 20.6,
+ "test/super_tab_chosen_names_test.dart": 3.0,
+ "test/super_tab_closed_groups_test.dart": 6.7,
+ "test/super_tab_completions_test.dart": 9.4,
+ "test/super_tab_composer_chips_bottom_test.dart": 5.8,
+ "test/super_tab_composer_error_test.dart": 2.2,
+ "test/super_tab_composer_flow_test.dart": 11.1,
+ "test/super_tab_composer_ime_test.dart": 12.9,
+ "test/super_tab_composer_layout_test.dart": 6.0,
+ "test/super_tab_composer_mic_test.dart": 14.4,
+ "test/super_tab_composer_newline_test.dart": 15.8,
+ "test/super_tab_composer_padding_test.dart": 7.8,
+ "test/super_tab_composer_rebuild_test.dart": 12.2,
+ "test/super_tab_composer_row_shots_test.dart": 2.4,
+ "test/super_tab_composer_row_test.dart": 12.1,
+ "test/super_tab_computer_move_shots_test.dart": 2.6,
+ "test/super_tab_computer_move_test.dart": 4.6,
+ "test/super_tab_controller_test.dart": 2.4,
+ "test/super_tab_day_jump_test.dart": 11.7,
+ "test/super_tab_day_pill_test.dart": 41.0,
+ "test/super_tab_decisions_test.dart": 9.2,
+ "test/super_tab_delight_shots_test.dart": 2.4,
+ "test/super_tab_delivery_test.dart": 11.2,
+ "test/super_tab_detail_dialog_test.dart": 3.8,
+ "test/super_tab_devices_geometry_test.dart": 3.1,
+ "test/super_tab_devices_preview_test.dart": 10.6,
+ "test/super_tab_devices_test.dart": 9.4,
+ "test/super_tab_dial_scroll_test.dart": 14.2,
+ "test/super_tab_dial_test.dart": 8.0,
+ "test/super_tab_dialog_shots_test.dart": 2.5,
+ "test/super_tab_discovery_test.dart": 16.4,
+ "test/super_tab_display_name_test.dart": 6.8,
+ "test/super_tab_dormant_groups_test.dart": 1.7,
+ "test/super_tab_file_links_test.dart": 19.9,
+ "test/super_tab_find_in_chat_shots_test.dart": 2.5,
+ "test/super_tab_find_in_chat_test.dart": 36.9,
+ "test/super_tab_first_read_test.dart": 2.5,
+ "test/super_tab_folder_drop_test.dart": 23.2,
+ "test/super_tab_folder_zip_test.dart": 1.9,
+ "test/super_tab_heartbeat_test.dart": 3.0,
+ "test/super_tab_heartbeat_widgets_test.dart": 5.2,
+ "test/super_tab_humans_shots_test.dart": 2.2,
+ "test/super_tab_humans_test.dart": 18.6,
+ "test/super_tab_identity_card_shots_test.dart": 2.2,
+ "test/super_tab_identity_card_test.dart": 3.3,
+ "test/super_tab_identity_card_view_test.dart": 8.6,
+ "test/super_tab_identity_test.dart": 3.2,
+ "test/super_tab_image_cache_test.dart": 1.8,
+ "test/super_tab_image_viewer_shots_test.dart": 2.4,
+ "test/super_tab_image_viewer_test.dart": 9.5,
+ "test/super_tab_import_agent_test.dart": 9.9,
+ "test/super_tab_inline_image_retry_test.dart": 3.0,
+ "test/super_tab_instructions_model_test.dart": 1.5,
+ "test/super_tab_instructions_rows_test.dart": 6.6,
+ "test/super_tab_instructions_test.dart": 19.5,
+ "test/super_tab_jump_to_latest_shots_test.dart": 13.6,
+ "test/super_tab_jump_to_latest_test.dart": 22.9,
+ "test/super_tab_jump_to_unread_shots_test.dart": 31.6,
+ "test/super_tab_launch_retry_test.dart": 2.7,
+ "test/super_tab_launch_test.dart": 13.4,
+ "test/super_tab_lead_actions_test.dart": 5.4,
+ "test/super_tab_leaderboard_shots_test.dart": 2.0,
+ "test/super_tab_leaderboard_test.dart": 2.1,
+ "test/super_tab_leads_test.dart": 4.5,
+ "test/super_tab_list_mentions_test.dart": 2.8,
+ "test/super_tab_live_card_shots_test.dart": 2.6,
+ "test/super_tab_live_card_test.dart": 7.5,
+ "test/super_tab_load_back_test.dart": 9.0,
+ "test/super_tab_local_backend_test.dart": 2.7,
+ "test/super_tab_machine_access_test.dart": 12.9,
+ "test/super_tab_machine_details_test.dart": 4.8,
+ "test/super_tab_machine_freshness_test.dart": 6.8,
+ "test/super_tab_machine_icon_test.dart": 3.2,
+ "test/super_tab_machine_own_account_test.dart": 6.0,
+ "test/super_tab_machine_projects_test.dart": 3.9,
+ "test/super_tab_machine_subscriptions_test.dart": 5.2,
+ "test/super_tab_machines_test.dart": 18.6,
+ "test/super_tab_manage_page_test.dart": 8.0,
+ "test/super_tab_mark_unread_test.dart": 11.2,
+ "test/super_tab_members_list_test.dart": 8.3,
+ "test/super_tab_members_test.dart": 10.7,
+ "test/super_tab_mentions_test.dart": 31.5,
+ "test/super_tab_menu_bar_notices_shots_test.dart": 2.6,
+ "test/super_tab_menu_bar_notices_test.dart": 19.3,
+ "test/super_tab_message_avatar_test.dart": 3.5,
+ "test/super_tab_message_cap_test.dart": 3.2,
+ "test/super_tab_message_edit_shots_test.dart": 8.9,
+ "test/super_tab_message_edit_test.dart": 18.3,
+ "test/super_tab_message_grouping_test.dart": 9.7,
+ "test/super_tab_message_link_host_test.dart": 3.7,
+ "test/super_tab_message_link_review_test.dart": 8.8,
+ "test/super_tab_message_link_shots_test.dart": 7.7,
+ "test/super_tab_message_link_test.dart": 1.6,
+ "test/super_tab_message_link_widget_test.dart": 34.8,
+ "test/super_tab_message_media_test.dart": 29.8,
+ "test/super_tab_message_paging_test.dart": 2.3,
+ "test/super_tab_message_rows_shots_test.dart": 1.9,
+ "test/super_tab_native_messages_test.dart": 8.4,
+ "test/super_tab_navigation_test.dart": 2.2,
+ "test/super_tab_needs_you_bar_test.dart": 17.1,
+ "test/super_tab_new_messages_shots_test.dart": 8.8,
+ "test/super_tab_new_messages_test.dart": 83.3,
+ "test/super_tab_no_machine_mark_shots_test.dart": 2.5,
+ "test/super_tab_no_machine_mark_test.dart": 6.9,
+ "test/super_tab_one_buddy_shots_test.dart": 2.5,
+ "test/super_tab_one_buddy_test.dart": 16.8,
+ "test/super_tab_outgoing_rows_test.dart": 12.6,
+ "test/super_tab_outgoing_shots_test.dart": 2.2,
+ "test/super_tab_outgoing_test.dart": 1.8,
+ "test/super_tab_pages_model_test.dart": 2.8,
+ "test/super_tab_pages_shots_test.dart": 3.0,
+ "test/super_tab_pages_test.dart": 10.4,
+ "test/super_tab_pane_layout_test.dart": 4.1,
+ "test/super_tab_parts_reading_test.dart": 2.0,
+ "test/super_tab_parts_test.dart": 2.8,
+ "test/super_tab_person_mentions_test.dart": 7.4,
+ "test/super_tab_person_names_test.dart": 5.5,
+ "test/super_tab_personal_lead_card_test.dart": 10.8,
+ "test/super_tab_personal_lead_shots_test.dart": 2.5,
+ "test/super_tab_personal_lead_test.dart": 4.0,
+ "test/super_tab_personal_projects_test.dart": 8.6,
+ "test/super_tab_personal_sidebar_bench_test.dart": 2.5,
+ "test/super_tab_phone_composer_test.dart": 12.9,
+ "test/super_tab_phone_header_test.dart": 7.2,
+ "test/super_tab_phone_targets_test.dart": 5.3,
+ "test/super_tab_picker_test.dart": 18.4,
+ "test/super_tab_pinned_chat_test.dart": 11.1,
+ "test/super_tab_places_view_test.dart": 11.1,
+ "test/super_tab_plain_work_shots_test.dart": 1.9,
+ "test/super_tab_polish_test.dart": 9.9,
+ "test/super_tab_private_legacy_channel_test.dart": 3.5,
+ "test/super_tab_profile_actions_test.dart": 3.0,
+ "test/super_tab_profile_model_test.dart": 2.2,
+ "test/super_tab_profile_sheet_test.dart": 14.2,
+ "test/super_tab_project_add_people_test.dart": 6.7,
+ "test/super_tab_project_chat_agent_post_test.dart": 4.6,
+ "test/super_tab_project_delete_test.dart": 7.2,
+ "test/super_tab_project_github_test.dart": 6.5,
+ "test/super_tab_project_menu_test.dart": 12.4,
+ "test/super_tab_project_number_shots_test.dart": 2.4,
+ "test/super_tab_project_row_bold_shots_test.dart": 2.2,
+ "test/super_tab_project_rows_shots_test.dart": 2.3,
+ "test/super_tab_project_rules_test.dart": 8.8,
+ "test/super_tab_project_settings_shots_test.dart": 3.2,
+ "test/super_tab_project_status_test.dart": 5.6,
+ "test/super_tab_pull_request_state_test.dart": 1.9,
+ "test/super_tab_pull_requests_test.dart": 6.8,
+ "test/super_tab_pulse_shots_test.dart": 6.4,
+ "test/super_tab_pulse_test.dart": 9.5,
+ "test/super_tab_push_frames_test.dart": 2.1,
+ "test/super_tab_pushes_test.dart": 2.2,
+ "test/super_tab_quick_switcher_test.dart": 29.0,
+ "test/super_tab_quiet_chat_test.dart": 10.9,
+ "test/super_tab_quiet_mode_test.dart": 2.2,
+ "test/super_tab_quiet_polish_test.dart": 11.3,
+ "test/super_tab_quiet_requests_test.dart": 2.8,
+ "test/super_tab_quiet_sidebar_test.dart": 4.5,
+ "test/super_tab_reactions_test.dart": 5.7,
+ "test/super_tab_read_marks_test.dart": 2.5,
+ "test/super_tab_reading_client_test.dart": 3.5,
+ "test/super_tab_reading_test.dart": 3.8,
+ "test/super_tab_reading_workspace_test.dart": 6.0,
+ "test/super_tab_receipt_test.dart": 11.2,
+ "test/super_tab_recording_gap_rows_test.dart": 6.0,
+ "test/super_tab_recovers_test.dart": 4.9,
+ "test/super_tab_reminders_shots_test.dart": 9.4,
+ "test/super_tab_reminders_test.dart": 9.7,
+ "test/super_tab_reply_box_shots_test.dart": 2.0,
+ "test/super_tab_reply_box_test.dart": 8.0,
+ "test/super_tab_reply_jump_test.dart": 74.2,
+ "test/super_tab_reply_notices_test.dart": 3.4,
+ "test/super_tab_review_test.dart": 10.8,
+ "test/super_tab_role_names_test.dart": 5.4,
+ "test/super_tab_roster_navigation_test.dart": 6.5,
+ "test/super_tab_roster_scope_test.dart": 11.9,
+ "test/super_tab_roster_test.dart": 29.3,
+ "test/super_tab_route_test.dart": 5.7,
+ "test/super_tab_saved_session_test.dart": 4.8,
+ "test/super_tab_saved_view_test.dart": 2.7,
+ "test/super_tab_saved_view_widget_test.dart": 6.2,
+ "test/super_tab_session_chips_shots_test.dart": 2.7,
+ "test/super_tab_session_project_memory_test.dart": 10.9,
+ "test/super_tab_share_repository_test.dart": 4.3,
+ "test/super_tab_sharing_auto_join_test.dart": 9.5,
+ "test/super_tab_ship_test.dart": 4.8,
+ "test/super_tab_sidebar_cleanup_test.dart": 4.8,
+ "test/super_tab_sidebar_models_test.dart": 10.4,
+ "test/super_tab_sidebar_pages_test.dart": 6.0,
+ "test/super_tab_sidebar_reads_light_shots_test.dart": 2.3,
+ "test/super_tab_sidebar_reads_light_test.dart": 8.5,
+ "test/super_tab_sidebar_rows_test.dart": 9.2,
+ "test/super_tab_sidebar_scale_test.dart": 7.3,
+ "test/super_tab_sidebar_sections_test.dart": 4.3,
+ "test/super_tab_sidebar_shots_test.dart": 2.5,
+ "test/super_tab_sidebar_test.dart": 8.2,
+ "test/super_tab_sign_out_keeps_outgoing_test.dart": 2.5,
+ "test/super_tab_startup_recovery_test.dart": 3.8,
+ "test/super_tab_swarm_accounts_test.dart": 3.4,
+ "test/super_tab_swarm_delete_test.dart": 5.7,
+ "test/super_tab_swarm_switcher_test.dart": 11.8,
+ "test/super_tab_switch_pm_test.dart": 16.1,
+ "test/super_tab_task_activity_test.dart": 8.4,
+ "test/super_tab_task_columns_test.dart": 1.4,
+ "test/super_tab_task_groups_test.dart": 4.2,
+ "test/super_tab_task_line_test.dart": 17.2,
+ "test/super_tab_task_priority_shots_test.dart": 6.7,
+ "test/super_tab_task_priority_test.dart": 8.3,
+ "test/super_tab_task_status_test.dart": 3.9,
+ "test/super_tab_task_steps_test.dart": 2.3,
+ "test/super_tab_tasks_header_test.dart": 7.7,
+ "test/super_tab_tasks_shots_test.dart": 7.4,
+ "test/super_tab_tasks_view_test.dart": 14.0,
+ "test/super_tab_team_machine_test.dart": 8.9,
+ "test/super_tab_teammate_terminal_test.dart": 7.8,
+ "test/super_tab_terminal_prompt_rows_test.dart": 4.8,
+ "test/super_tab_terminal_rows_test.dart": 8.0,
+ "test/super_tab_thread_composer_shots_test.dart": 2.3,
+ "test/super_tab_thread_composer_test.dart": 33.2,
+ "test/super_tab_thread_error_test.dart": 6.6,
+ "test/super_tab_thread_scroll_shots_test.dart": 14.2,
+ "test/super_tab_thread_scroll_test.dart": 23.4,
+ "test/super_tab_thread_tag_bar_shots_test.dart": 7.7,
+ "test/super_tab_thread_tag_bar_test.dart": 6.3,
+ "test/super_tab_thread_tags_test.dart": 24.1,
+ "test/super_tab_threads_page_shots_test.dart": 2.4,
+ "test/super_tab_threads_page_test.dart": 14.9,
+ "test/super_tab_threads_shots_test.dart": 1.6,
+ "test/super_tab_threads_test.dart": 25.3,
+ "test/super_tab_thumbnails_test.dart": 10.2,
+ "test/super_tab_time_zone_test.dart": 1.9,
+ "test/super_tab_tooltip_freeze_test.dart": 5.7,
+ "test/super_tab_typography_shots_test.dart": 8.2,
+ "test/super_tab_typography_test.dart": 8.9,
+ "test/super_tab_unread_keys_test.dart": 2.3,
+ "test/super_tab_unread_lines_test.dart": 2.5,
+ "test/super_tab_update_required_test.dart": 6.7,
+ "test/super_tab_upload_test.dart": 3.2,
+ "test/super_tab_usernames_test.dart": 1.9,
+ "test/super_tab_view_test.dart": 12.7,
+ "test/super_tab_visible_tick_test.dart": 3.0,
+ "test/super_tab_voice_message_shots_test.dart": 2.2,
+ "test/super_tab_voice_message_test.dart": 9.4,
+ "test/super_tab_voice_send_test.dart": 2.4,
+ "test/super_tab_voice_take_test.dart": 3.0,
+ "test/super_tab_waiting_notice_shots_test.dart": 10.7,
+ "test/super_tab_waiting_notice_test.dart": 7.1,
+ "test/super_tab_web_direct_shots_test.dart": 4.7,
+ "test/super_tab_web_direct_test.dart": 5.4,
+ "test/super_tab_web_request_rate_test.dart": 3.7,
+ "test/super_tab_web_sign_out_test.dart": 1.9,
+ "test/super_tab_work_details_test.dart": 3.0,
+ "test/super_tab_workspace_test.dart": 14.6,
+ "test/super_tab_zone_time_test.dart": 2.2,
+ "test/swarm_add_agent_test.dart": 7.0,
+ "test/swarm_arrange_keyboard_test.dart": 10.3,
+ "test/swarm_async_test.dart": 6.2,
+ "test/swarm_attention_picker_test.dart": 11.9,
+ "test/swarm_attention_test.dart": 2.3,
+ "test/swarm_canvas_test.dart": 11.7,
+ "test/swarm_close_race_test.dart": 7.1,
+ "test/swarm_closed_agents_test.dart": 6.7,
+ "test/swarm_commands_test.dart": 8.8,
+ "test/swarm_content_search_test.dart": 6.0,
+ "test/swarm_creation_form_test.dart": 20.0,
+ "test/swarm_dock_badge_test.dart": 4.9,
+ "test/swarm_entry_picker_test.dart": 12.8,
+ "test/swarm_history_test.dart": 7.1,
+ "test/swarm_immediate_input_test.dart": 17.0,
+ "test/swarm_interactions_test.dart": 23.9,
+ "test/swarm_join_account_choice_test.dart": 7.2,
+ "test/swarm_join_any_test.dart": 13.2,
+ "test/swarm_join_error_shots_test.dart": 5.6,
+ "test/swarm_join_machine_test.dart": 4.3,
+ "test/swarm_join_seen_test.dart": 1.6,
+ "test/swarm_launch_recovery_test.dart": 4.8,
+ "test/swarm_local_models_picker_test.dart": 3.5,
+ "test/swarm_navigation_test.dart": 3.0,
+ "test/swarm_navigator_test.dart": 2.3,
+ "test/swarm_needs_you_test.dart": 1.5,
+ "test/swarm_picker_activation_test.dart": 10.6,
+ "test/swarm_projects_test.dart": 9.1,
+ "test/swarm_reconnect_test.dart": 2.6,
+ "test/swarm_rename_dialog_test.dart": 3.0,
+ "test/swarm_reopen_test.dart": 5.7,
+ "test/swarm_resize_test.dart": 11.3,
+ "test/swarm_saved_project_test.dart": 3.2,
+ "test/swarm_screen_test.dart": 12.2,
+ "test/swarm_search_identity_test.dart": 6.3,
+ "test/swarm_search_preview_test.dart": 24.6,
+ "test/swarm_search_render_test.dart": 13.1,
+ "test/swarm_search_test.dart": 7.5,
+ "test/swarm_sharing_test.dart": 11.8,
+ "test/swarm_split_test.dart": 30.6,
+ "test/swarm_state_test.dart": 2.2,
+ "test/swarm_switcher_test.dart": 7.8,
+ "test/swarm_terminal_routing_test.dart": 2.3,
+ "test/swarm_terminal_scroll_test.dart": 6.4,
+ "test/swarm_view_files_test.dart": 4.8,
+ "test/swarm_web_relay_team_test.dart": 1.5,
+ "test/system_notifications_test.dart": 5.5,
+ "test/tab_activity_sidebar_test.dart": 10.8,
+ "test/tab_activity_test.dart": 1.8,
+ "test/tab_leader_test.dart": 3.8,
+ "test/tab_people_test.dart": 6.2,
+ "test/tab_settings_test.dart": 2.2,
+ "test/task_history_test.dart": 1.9,
+ "test/task_step_test.dart": 1.9,
+ "test/team_account_lock_test.dart": 10.9,
+ "test/team_account_offer_test.dart": 5.9,
+ "test/team_setup_background_test.dart": 3.9,
+ "test/terminal_alt_buffer_scroll_test.dart": 2.3,
+ "test/terminal_binary_test.dart": 9.4,
+ "test/terminal_block_glyph_test.dart": 1.9,
+ "test/terminal_byte_consumer_test.dart": 2.2,
+ "test/terminal_catalog_test.dart": 9.1,
+ "test/terminal_clipboard_test.dart": 10.1,
+ "test/terminal_composer_test.dart": 9.8,
+ "test/terminal_control_banner_hints_test.dart": 6.4,
+ "test/terminal_control_banner_test.dart": 9.9,
+ "test/terminal_cursor_idle_test.dart": 4.7,
+ "test/terminal_delete_detection_test.dart": 3.1,
+ "test/terminal_engine_test.dart": 1.9,
+ "test/terminal_find_test.dart": 13.3,
+ "test/terminal_font_store_test.dart": 1.6,
+ "test/terminal_glyph_presentation_test.dart": 2.3,
+ "test/terminal_initial_output_test.dart": 8.9,
+ "test/terminal_input_test.dart": 5.7,
+ "test/terminal_line_paint_cache_test.dart": 2.5,
+ "test/terminal_link_gesture_test.dart": 2.6,
+ "test/terminal_link_opener_test.dart": 2.8,
+ "test/terminal_links_test.dart": 2.0,
+ "test/terminal_offline_view_test.dart": 7.7,
+ "test/terminal_pane_test.dart": 3.0,
+ "test/terminal_panel_activity_test.dart": 4.8,
+ "test/terminal_panel_focus_test.dart": 4.0,
+ "test/terminal_panel_header_test.dart": 8.1,
+ "test/terminal_panel_links_test.dart": 7.3,
+ "test/terminal_panel_presentation_test.dart": 5.0,
+ "test/terminal_passage_test.dart": 2.1,
+ "test/terminal_reconnect_race_test.dart": 1.8,
+ "test/terminal_reconnect_view_test.dart": 8.0,
+ "test/terminal_render_suspension_test.dart": 5.2,
+ "test/terminal_rewritten_line_test.dart": 12.8,
+ "test/terminal_search_test.dart": 2.5,
+ "test/terminal_session_test.dart": 4.3,
+ "test/terminal_settings_section_test.dart": 2.8,
+ "test/terminal_tail_lifecycle_test.dart": 12.2,
+ "test/terminal_theme_test.dart": 2.3,
+ "test/terminal_typography_test.dart": 1.6,
+ "test/terminal_ui_scale_isolation_test.dart": 3.8,
+ "test/terminal_ui_typography_test.dart": 9.4,
+ "test/terminal_view_ime_echo_test.dart": 3.3,
+ "test/terminal_view_interaction_test.dart": 3.4,
+ "test/text_field_editing_keys_test.dart": 9.8,
+ "test/theme_mode_appearance_test.dart": 6.7,
+ "test/theme_mode_test.dart": 2.0,
+ "test/theme_palettes_shots_test.dart": 2.2,
+ "test/theme_palettes_test.dart": 2.7,
+ "test/theme_sidebar_ink_test.dart": 3.2,
+ "test/toolbar_notices_test.dart": 1.8,
+ "test/toolbar_onboarding_test.dart": 23.4,
+ "test/trusted_pipe_test.dart": 4.0,
+ "test/unavailable_harness_picker_test.dart": 4.8,
+ "test/uninstall_marker_test.dart": 1.6,
+ "test/unlinked_machine_load_test.dart": 3.0,
+ "test/update_harness_test.dart": 3.4,
+ "test/update_progress_test.dart": 2.9,
+ "test/usage_account_key_test.dart": 1.9,
+ "test/usage_accounts_test.dart": 2.1,
+ "test/usage_credentials_test.dart": 2.1,
+ "test/usage_jsonl_benchmark_test.dart": 1.9,
+ "test/usage_jsonl_scanners_test.dart": 3.5,
+ "test/usage_ledger_benchmark_test.dart": 2.5,
+ "test/usage_ledger_lifecycle_test.dart": 1.8,
+ "test/usage_ledger_test.dart": 2.7,
+ "test/usage_loading_test.dart": 9.5,
+ "test/usage_report_test.dart": 2.1,
+ "test/usage_scanners_test.dart": 2.7,
+ "test/usage_section_test.dart": 5.2,
+ "test/usage_test.dart": 2.8,
+ "test/user_opened_reach_test.dart": 2.5,
+ "test/verdict_marks_test.dart": 3.5,
+ "test/viewer/device_ack_test.dart": 2.7,
+ "test/viewer/device_history_test.dart": 5.5,
+ "test/viewer/device_log_acks_test.dart": 4.6,
+ "test/viewer/device_log_sync_test.dart": 22.4,
+ "test/viewer/device_log_test.dart": 2.5,
+ "test/viewer/group_sync_test.dart": 2.2,
+ "test/viewer/password_link_version_test.dart": 1.9,
+ "test/viewer/removed_machines_test.dart": 1.8,
+ "test/viewer_auth_lifecycle_test.dart": 2.5,
+ "test/viewer_link_incomplete_test.dart": 3.2,
+ "test/viewer_load_tracker_test.dart": 2.5,
+ "test/viewer_location_test.dart": 1.9,
+ "test/viewer_mode_test.dart": 1.7,
+ "test/viewer_page_test.dart": 4.5,
+ "test/viewer_pane_name_test.dart": 1.8,
+ "test/web_attachment_text_part_test.dart": 3.2,
+ "test/web_build_version_test.dart": 1.9,
+ "test/web_chrome_menus_test.dart": 15.7,
+ "test/web_console_test.dart": 1.8,
+ "test/web_deferred_part_test.dart": 3.6,
+ "test/web_finder_part_test.dart": 2.5,
+ "test/web_first_machine_test.dart": 5.8,
+ "test/web_focus_ring_test.dart": 4.3,
+ "test/web_footer_subscriptions_test.dart": 5.4,
+ "test/web_key_hints_test.dart": 4.3,
+ "test/web_machine_choice_test.dart": 2.3,
+ "test/web_machine_list_watch_test.dart": 14.1,
+ "test/web_menu_items_test.dart": 2.2,
+ "test/web_menus_test.dart": 9.6,
+ "test/web_page_startup_test.dart": 2.4,
+ "test/web_pane_test.dart": 14.1,
+ "test/web_phone_targets_test.dart": 7.2,
+ "test/web_release_script_test.dart": 5.2,
+ "test/web_release_test.dart": 1.6,
+ "test/web_saved_computers_test.dart": 5.2,
+ "test/web_search_drill_down_test.dart": 17.6,
+ "test/web_search_phone_test.dart": 6.6,
+ "test/web_store_banner_test.dart": 3.8,
+ "test/web_tab_switcher_test.dart": 8.1,
+ "test/web_tablet_terminal_test.dart": 5.6,
+ "test/web_update_bar_test.dart": 2.8,
+ "test/web_update_host_test.dart": 4.5,
+ "test/web_update_watcher_test.dart": 1.7,
+ "test/web_ux_app_test.dart": 1.6,
+ "test/web_workspace_part_test.dart": 3.5,
+ "test/welcome_closed_super_tabs_test.dart": 5.2,
+ "test/welcome_entry_layout_test.dart": 9.9,
+ "test/welcome_sessions_test.dart": 6.9,
+ "test/welcome_wallpaper_shortcuts_test.dart": 5.0,
+ "test/widget_test.dart": 1.7,
+ "test/wifi_robots_test.dart": 7.9,
+ "test/windows_app_menu_test.dart": 6.0,
+ "test/windows_boot_test.dart": 1.8,
+ "test/windows_clipboard_fonts_test.dart": 2.0,
+ "test/windows_git_setup_test.dart": 1.6,
+ "test/windows_pipe_auth_test.dart": 1.4,
+ "test/windows_pipe_test.dart": 6.3,
+ "test/windows_runner_contract_test.dart": 1.9,
+ "test/windows_self_install_boot_test.dart": 2.0,
+ "test/windows_self_install_test.dart": 1.9,
+ "test/windows_shortcuts_test.dart": 2.3,
+ "test/windows_status_tray_test.dart": 5.4,
+ "test/windows_system_notifier_test.dart": 1.9,
+ "test/windows_update_swap_test.dart": 3.0,
+ "test/windows_words_test.dart": 2.4,
+ "test/wire_counter_test.dart": 2.5,
+ "test/workspace_account_lifecycle_test.dart": 3.8,
+ "test/workspace_account_test.dart": 10.3,
+ "test/workspace_activity_test.dart": 3.8,
+ "test/workspace_chrome_scope_test.dart": 8.2,
+ "test/workspace_context_navigation_test.dart": 8.3,
+ "test/workspace_event_isolation_test.dart": 8.9,
+ "test/workspace_expiry_screen_test.dart": 3.5,
+ "test/workspace_first_connection_test.dart": 9.4,
+ "test/workspace_learning_test.dart": 1.9,
+ "test/workspace_machine_prompt_test.dart": 4.6,
+ "test/workspace_onboarding_test.dart": 2.5,
+ "test/workspace_pull_request_label_test.dart": 5.8,
+ "test/workspace_pull_request_test.dart": 2.7,
+ "test/workspace_start_guide_test.dart": 14.0,
+ "test/workspace_status_test.dart": 9.9,
+ "test/workspace_tabs_by_mouse_test.dart": 2.1,
+ "test/workspace_welcome_test.dart": 4.5,
+ "test/ws_conn_decode_test.dart": 2.6,
+ "test/ws_conn_fifo_test.dart": 2.1,
+ "test/ws_conn_plugin_test.dart": 2.5,
+ "test/ws_conn_test.dart": 20.9,
+ "test/ws_pool_ownership_test.dart": 1.2,
+ "test/ws_readiness_test.dart": 2.1,
+ "test/ws_reconnect_backoff_test.dart": 1.3
+ }
+}
diff --git a/scripts/desktop-test-durations.py b/scripts/desktop-test-durations.py
new file mode 100755
index 000000000..7058914b1
--- /dev/null
+++ b/scripts/desktop-test-durations.py
@@ -0,0 +1,114 @@
+#!/usr/bin/env python3
+"""Refresh scripts/desktop-test-durations.json, the per-file times Desktop CI shards are balanced by (task #3458).
+
+Give it green CI runs: it downloads each run's Desktop shard artifacts with `gh`, measures every VM test file from its
+first test start to its last test end in the JSON reporter log, and writes each file's median over the runs. Three
+recent runs from different runners is a good sample; runner speed varies by a fifth.
+
+ python3 scripts/desktop-test-durations.py --run 42062947953 --run 42062373039 --run 42061777210
+
+Artifacts already on disk (`gh run download RUN --pattern 'desktop-shard-*' --dir DIR`) are read with --from RUN=DIR,
+which spends none of the account's artifact download allowance again.
+"""
+import argparse
+import json
+from pathlib import Path
+import statistics
+import subprocess
+import sys
+import tempfile
+
+OUTPUT = Path(__file__).with_name("desktop-test-durations.json")
+RUNS = "https://github.com/autonomous-ai/openharness/actions/runs/"
+DESCRIPTION = ("Per-file durations of Desktop VM tests (seconds, median over the runs named), from each shard's JSON reporter "
+ "log: a file's first test start to its last test end. scripts/test-desktop.py splits shards by them; refresh "
+ "with scripts/desktop-test-durations.py.")
+
+
+def log_durations(log, desktop):
+ """Seconds per desktop-relative test file in one reporter log."""
+ paths, suite_of, first, last = {}, {}, {}, {}
+ for line in log.read_text(errors="replace").splitlines():
+ try:
+ event = json.loads(line)
+ except ValueError:
+ continue # Flutter also writes non-JSON build/progress output.
+ if not isinstance(event, dict) or not isinstance(event.get("time"), (int, float)):
+ continue
+ kind, at = event.get("type"), event["time"]
+ if kind == "suite":
+ paths[event["suite"]["id"]] = event["suite"]["path"]
+ elif kind == "testStart":
+ suite = event["test"]["suiteID"]
+ suite_of[event["test"]["id"]] = suite
+ first.setdefault(suite, at)
+ elif kind == "testDone" and event.get("testID") in suite_of:
+ suite = suite_of[event["testID"]]
+ last[suite] = max(last.get(suite, at), at)
+ durations = {}
+ for suite, path in paths.items():
+ if suite in first and suite in last:
+ relative = Path(path).relative_to(desktop).as_posix() if Path(path).is_relative_to(desktop) else path
+ durations[relative] = round((last[suite] - first[suite]) / 1000, 1)
+ return durations
+
+
+def run_durations(folder):
+ """Seconds per file in one run: every shard receipt under [folder] and the reporter logs beside it."""
+ durations = {}
+ receipts = sorted(Path(folder).rglob("receipt.json"))
+ for receipt in receipts:
+ desktop = Path(json.loads(receipt.read_text())["desktop_root"])
+ for log in sorted(receipt.parent.glob("desktop-vm*.log")):
+ durations.update(log_durations(log, desktop))
+ if not durations:
+ raise ValueError(f"no Desktop shard logs under {folder}")
+ return durations
+
+
+def durations_document(runs):
+ """The durations file for {run id: {file: seconds}}: each file's median over the runs that measured it."""
+ seen = {}
+ for measured in runs.values():
+ for path, seconds in measured.items():
+ seen.setdefault(path, []).append(seconds)
+ return {"schema": 1, "description": DESCRIPTION, "sourceRuns": [RUNS + run for run in runs],
+ "files": {path: round(statistics.median(values), 1) for path, values in sorted(seen.items())}}
+
+
+def download(run, folder):
+ """A run's Desktop shard artifacts, through `gh` (signed in to the repository)."""
+ subprocess.run(["gh", "run", "download", run, "--pattern", "desktop-shard-*", "--dir", str(folder)], check=True)
+
+
+def main():
+ parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
+ parser.add_argument("--run", action="append", default=[], help="a green CI run id to download; give three or more")
+ parser.add_argument("--from", dest="local", action="append", default=[], metavar="RUN=DIR",
+ help="a run whose shard artifacts are already downloaded into DIR")
+ parser.add_argument("--output", type=Path, default=OUTPUT)
+ args = parser.parse_args()
+ if not args.run and not args.local:
+ parser.error("give --run or --from")
+ with tempfile.TemporaryDirectory(prefix="desktop-durations.") as folder:
+ runs = {}
+ for item in args.local:
+ run, _, path = item.partition("=")
+ if not run or not path:
+ parser.error("--from takes RUN=DIR")
+ runs[run] = run_durations(Path(path))
+ for run in args.run:
+ target = Path(folder) / run
+ download(run, target)
+ runs[run] = run_durations(target)
+ document = durations_document(runs)
+ args.output.write_text(json.dumps(document, indent=1) + "\n")
+ print(f"{len(document['files'])} files, {sum(document['files'].values()):.0f}s in all, from {len(runs)} runs: {args.output}")
+
+
+if __name__ == "__main__":
+ try:
+ main()
+ except (OSError, ValueError, KeyError, subprocess.CalledProcessError) as error:
+ print(f"desktop durations error: {error}", file=sys.stderr)
+ sys.exit(1)
diff --git a/scripts/test-desktop.py b/scripts/test-desktop.py
index f850c58bc..d35d13a2c 100644
--- a/scripts/test-desktop.py
+++ b/scripts/test-desktop.py
@@ -21,6 +21,12 @@ validation = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(validation)
LOADER_ERROR = "Unable to connect to flutter_tester process: WebSocketException: Invalid WebSocket upgrade request"
+# Measured per-file durations that shards are balanced by (task #3458); refreshed by scripts/desktop-test-durations.py.
+DURATIONS = Path(__file__).with_name("desktop-test-durations.json")
+# A shard's time estimate: its files' durations over its workers, times a slow runner's margin, plus Flutter's startup.
+# Measured on four runs (11-05): the same files summed 6,662 s on one runner and 8,036 s on another.
+SLOW_RUNNER = 1.15
+STARTUP_SECONDS = 20
def default_workers():
@@ -115,6 +121,20 @@ def summarize(log, selected):
return {"files": files, "problems": problems, "reported_success": done[0] if len(done) == 1 else None}
+def unreached_files(paths, report):
+ """The files of an attempt that it never got through: not started, or with tests still to run when it stopped. A file
+ that passed, failed a test or could not load was reached; its own result says what happened."""
+ def reached(path):
+ result = report["files"].get(path)
+ if result is None:
+ return False
+ if result["status"] in {"passed", "startup_error"} or result["errors"]:
+ return True
+ registered = result["registered_cases"]
+ return registered is not None and result["passed"] + result["skipped"] >= registered
+ return [path for path in paths if not reached(path)]
+
+
def retry_files(check, report):
if check.get("status") != "failed" or check.get("exit_code") != 1 or check.get("cleanup_error"):
return []
@@ -158,9 +178,46 @@ def shard_pair(value):
return index, total
-def partition(files, shard):
+def load_durations(path=DURATIONS):
+ """The measured seconds per desktop-relative test file; empty when the file is missing or unreadable, which splits
+ by equal weights (round-robin) rather than failing a run."""
+ try:
+ files = json.loads(Path(path).read_text()).get("files", {})
+ except (OSError, ValueError, AttributeError):
+ return {}
+ if not isinstance(files, dict):
+ return {}
+ return {path: float(seconds) for path, seconds in files.items()
+ if isinstance(path, str) and isinstance(seconds, (int, float)) and not isinstance(seconds, bool) and seconds > 0}
+
+
+def duration_key(path):
+ """A test file as the durations file names it: relative to desktop/, whether given absolute or relative."""
+ text = str(path)
+ at = text.rfind("/desktop/test/")
+ return text[at + len("/desktop/"):] if at >= 0 else text
+
+
+def file_weights(files, durations):
+ """Each file's measured seconds; a file never measured (a new one) counts as the median of the measured ones."""
+ known = sorted(durations.values())
+ median = known[len(known) // 2] if len(known) % 2 else (sum(known[len(known) // 2 - 1:len(known) // 2 + 1]) / 2 if known else 1.0)
+ return {path: durations.get(duration_key(path), median) for path in files}
+
+
+def partition(files, shard, durations=None):
+ """INDEX/TOTAL of [files], balanced by measured durations: longest first, each to the least-loaded shard (the lowest
+ index on a tie). Every runner computes the same split from the same inventory and durations file. With equal weights
+ it is round-robin by the sorted inventory, as before."""
index, total = shard
- selected = files[index - 1::total]
+ weights = file_weights(files, load_durations() if durations is None else durations)
+ order = sorted(range(len(files)), key=lambda i: (-weights[files[i]], i))
+ loads, assigned = [0.0] * total, [[] for _ in range(total)]
+ for i in order:
+ target = min(range(total), key=lambda s: (loads[s], s))
+ loads[target] += weights[files[i]]
+ assigned[target].append(i)
+ selected = [files[i] for i in sorted(assigned[index - 1])]
if not selected:
raise ValueError("shard has no test files; reduce the shard count")
return selected
@@ -238,6 +295,14 @@ def main(argv=None):
receipt["identity"] = identity
print(f"Desktop VM: {len(selected)} files, {workers} workers, {args.timeout:g}s total budget", flush=True)
first, report = attempt("desktop-vm", selected, workers)
+ # A shard out of time names what it never reached (task #3458), as text and as a GitHub error annotation.
+ unreached = unreached_files(selected, report) if first.get("status") == "timeout" else []
+ receipt["unreached_files"] = [str(Path(path).relative_to(desktop)) for path in unreached]
+ if unreached:
+ names = ", ".join(receipt["unreached_files"])
+ count = f"{len(unreached)} file" + ("s" if len(unreached) != 1 else "")
+ print(f"Not reached before the time limit ({count}): {names}", flush=True)
+ print(f"::error title=Desktop shard out of time::{count} not reached: {names}", flush=True)
candidates = retry_files(first, report) if not args.no_loader_retry else []
final_files = dict(report["files"])
receipt["status"] = "passed" if passed(first, report) else "failed"
diff --git a/scripts/tests/desktop_vm_fixture.py b/scripts/tests/desktop_vm_fixture.py
index 404a3202e..6d73752bf 100644
--- a/scripts/tests/desktop_vm_fixture.py
+++ b/scripts/tests/desktop_vm_fixture.py
@@ -60,6 +60,9 @@ def main():
behavior = "assertion"
if scenario == "incomplete":
behavior = "incomplete"
+ if scenario == "partial" and index == 1:
+ # The first file finished; the second is never reached before the time limit.
+ time.sleep(30)
for event in suite(path, index * 10, behavior):
print(json.dumps(event), flush=True)
successful &= behavior == "pass"
diff --git a/scripts/tests/test_ci_plan.py b/scripts/tests/test_ci_plan.py
index 6cac4d6fc..a3fd8df92 100644
--- a/scripts/tests/test_ci_plan.py
+++ b/scripts/tests/test_ci_plan.py
@@ -114,6 +114,14 @@ class SelectionTests(unittest.TestCase):
self.assertEqual(planner.select(["cli/src/lib/windows/__fixtures__/pipe-auth-vectors.json"])[0], {"cli", "desktop"})
self.assertEqual(planner.select(["cli/src/lib/windows/pipeAuth.ts"])[0], {"cli"})
+ def test_the_desktop_runner_and_its_durations_select_the_desktop_suite(self):
+ # Task #3458: the runner splits the Desktop shards by the durations file; a change to either runs those shards,
+ # so a bad split cannot merge on process checks alone.
+ for path in ("scripts/test-desktop.py", "scripts/desktop-test-durations.json"):
+ with self.subTest(path=path):
+ self.assertEqual(planner.select([path])[0], {"desktop"})
+ self.assertEqual(planner.select(["scripts/desktop-test-durations.py"])[0], set())
+
def test_a_style_allowlist_selects_the_suite_whose_job_checks_it(self):
# mobile-checks and desktop-tests run scripts/check-ui-style.py --only against their area's list.
self.assertEqual(planner.select(["scripts/ui-style-allowlist/mobile.json"])[0], {"mobile"})
diff --git a/scripts/tests/test_desktop_test_durations.py b/scripts/tests/test_desktop_test_durations.py
new file mode 100644
index 000000000..f605bb822
--- /dev/null
+++ b/scripts/tests/test_desktop_test_durations.py
@@ -0,0 +1,58 @@
+import importlib.util
+import json
+from pathlib import Path
+import tempfile
+import unittest
+
+SCRIPT = Path(__file__).resolve().parents[1] / "desktop-test-durations.py"
+spec = importlib.util.spec_from_file_location("desktop_test_durations", SCRIPT)
+tool = importlib.util.module_from_spec(spec)
+spec.loader.exec_module(tool)
+
+
+def shard_artifact(folder, desktop, timings):
+ """One shard's artifact as CI uploads it: a receipt naming its checkout and the JSON reporter log, where each file's
+ tests start and end at the times given (milliseconds since the run started)."""
+ run = folder / "20261105T000000Z-desktop-1"
+ run.mkdir(parents=True)
+ (run / "receipt.json").write_text(json.dumps({"schema": 1, "kind": "desktop-vm", "desktop_root": desktop}))
+ events, test_id = [], 100
+ for suite_id, (name, start, end) in enumerate(timings):
+ events.append({"type": "suite", "suite": {"id": suite_id, "platform": "vm", "path": f"{desktop}/test/{name}"}, "time": start})
+ for at, kind in ((start, "testStart"), (end, "testDone")):
+ if kind == "testStart":
+ events.append({"type": kind, "test": {"id": test_id, "suiteID": suite_id, "name": "case"}, "time": at})
+ else:
+ events.append({"type": kind, "testID": test_id, "result": "success", "hidden": False, "skipped": False, "time": at})
+ test_id += 1
+ events.append({"type": "done", "success": True, "time": 99_000})
+ (run / "desktop-vm.log").write_text("Flutter build output\n" + "\n".join(json.dumps(e) for e in events) + "\n")
+
+
+class DurationRefreshTests(unittest.TestCase):
+ """Task #3458: the shard split reads measured file times; this keeps them current from CI runs."""
+
+ def test_measures_each_file_from_its_first_test_start_to_its_last_test_end(self):
+ with tempfile.TemporaryDirectory() as folder:
+ shard_artifact(Path(folder) / "s1", "/runner/work/repo/desktop", [("a_test.dart", 1_000, 11_000), ("b_test.dart", 2_000, 4_500)])
+ self.assertEqual(tool.run_durations(Path(folder)), {"test/a_test.dart": 10.0, "test/b_test.dart": 2.5})
+
+ def test_takes_each_files_median_over_runs_and_records_them(self):
+ with tempfile.TemporaryDirectory() as folder:
+ root = Path(folder)
+ for run, seconds in (("1", 10), ("2", 40), ("3", 20)):
+ shard_artifact(root / run / "s1", "/w/desktop", [("a_test.dart", 0, seconds * 1000)])
+ shard_artifact(root / "3" / "s2", "/w/desktop", [("new_test.dart", 0, 3_000)])
+ document = tool.durations_document({run: tool.run_durations(root / run) for run in ("1", "2", "3")})
+ self.assertEqual(document["files"], {"test/a_test.dart": 20.0, "test/new_test.dart": 3.0})
+ self.assertEqual(document["sourceRuns"], [f"https://github.com/autonomous-ai/openharness/actions/runs/{run}" for run in ("1", "2", "3")])
+ self.assertEqual(document["schema"], 1)
+
+ def test_a_run_with_no_shard_logs_is_refused(self):
+ with tempfile.TemporaryDirectory() as folder:
+ with self.assertRaises(ValueError):
+ tool.run_durations(Path(folder))
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/scripts/tests/test_desktop_tests.py b/scripts/tests/test_desktop_tests.py
index 385a98974..f1e6a0188 100644
--- a/scripts/tests/test_desktop_tests.py
+++ b/scripts/tests/test_desktop_tests.py
@@ -1,6 +1,7 @@
import importlib.util
import json
import os
+import re
from pathlib import Path
import shutil
import subprocess
@@ -36,6 +37,71 @@ class WorkerCountTests(unittest.TestCase):
driver.partition(files[:1], (2, 2))
+class DurationSplitTests(unittest.TestCase):
+ """Task #3458: shard 3 hit its 10-minute limit with tests passing, because round-robin by name ignored file times."""
+
+ def loads(self, groups, durations):
+ return [sum(durations[path] for path in group) for group in groups]
+
+ def test_heavy_files_spread_across_shards_and_every_file_runs_once(self):
+ durations = {"test/a_test.dart": 100, "test/b_test.dart": 90, "test/c_test.dart": 60, "test/d_test.dart": 50,
+ **{f"test/small{i}_test.dart": 5 for i in range(10)}}
+ files = sorted(durations)
+ groups = [driver.partition(files, (i, 2), durations) for i in (1, 2)]
+ self.assertEqual(sorted(p for g in groups for p in g), files)
+ loads = self.loads(groups, durations)
+ # Round-robin by name put a, c and four small files together (190 + 20 against 140 + 30).
+ self.assertLessEqual(max(loads) - min(loads), 10, loads)
+ for group in groups:
+ self.assertEqual(group, sorted(group))
+
+ def test_a_file_with_no_measurement_counts_as_the_median(self):
+ durations = {"test/a_test.dart": 10, "test/b_test.dart": 20, "test/c_test.dart": 30}
+ self.assertEqual(driver.file_weights(["test/a_test.dart", "test/new_test.dart"], durations),
+ {"test/a_test.dart": 10, "test/new_test.dart": 20})
+
+ def test_absolute_paths_use_their_desktop_relative_measurement(self):
+ durations = {"test/a_test.dart": 100, "test/b_test.dart": 1, "test/c_test.dart": 1}
+ absolute = [f"/work/repo/desktop/test/{n}_test.dart" for n in "abc"]
+ self.assertEqual(driver.partition(absolute, (1, 2), durations), [absolute[0]])
+
+ def test_without_measurements_the_split_stays_round_robin(self):
+ files = [f"test/{i:02d}_test.dart" for i in range(11)]
+ for durations in ({}, None):
+ with self.subTest(durations=durations):
+ self.assertEqual([driver.partition(files, (i, 4), durations) for i in range(1, 5)],
+ [files[i - 1::4] for i in range(1, 5)])
+
+ def test_unreadable_durations_file_falls_back_to_equal_weights(self):
+ with tempfile.TemporaryDirectory() as folder:
+ broken = Path(folder) / "durations.json"
+ broken.write_text("{not json")
+ self.assertEqual(driver.load_durations(broken), {})
+ self.assertEqual(driver.load_durations(Path(folder) / "missing.json"), {})
+
+ @unittest.skipUnless((SCRIPT.parents[1] / "desktop" / "test").is_dir(),
+ "needs the Desktop test files; ci.yml runs it in desktop-tests, whose checkout has them")
+ def test_ci_split_leaves_every_shard_a_quarter_of_its_time_limit(self):
+ """The committed durations, CI's shard count, workers and limit, and today's test files: each shard's estimate stays
+ within 75% of its limit, with a slow runner's margin. Fails before CI does when the suite outgrows its shards."""
+ root = SCRIPT.parents[1]
+ workflow = (root / ".github" / "workflows" / "ci.yml").read_text()
+ command = re.search(r'test-desktop\.py --shard "\$DESKTOP_SHARD/(\d+)" --workers (\d+) --timeout (\d+)', workflow)
+ self.assertIsNotNone(command, "ci.yml's Desktop shard command changed shape")
+ shards, workers, limit = (int(value) for value in command.groups())
+ matrix = re.search(r"desktop-tests:.*?shard: \[([0-9, ]+)\]", workflow, re.S)
+ self.assertEqual([int(v) for v in matrix.group(1).split(",")], list(range(1, shards + 1)))
+ durations = driver.load_durations()
+ self.assertGreater(len(durations), 500, "the durations file is missing or nearly empty")
+ inventory = [Path(p).relative_to(root / "desktop").as_posix() for p in driver.select_files(root / "desktop", [])]
+ weights = driver.file_weights(inventory, durations)
+ for index in range(1, shards + 1):
+ group = driver.partition(inventory, (index, shards), durations)
+ estimate = driver.SLOW_RUNNER * sum(weights[p] for p in group) / workers + driver.STARTUP_SECONDS
+ with self.subTest(shard=index):
+ self.assertLessEqual(estimate, 0.75 * limit, f"shard {index}/{shards}: about {estimate:.0f}s of {limit}s")
+
+
class ReporterTests(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
@@ -211,5 +277,22 @@ class DesktopProcessTests(unittest.TestCase):
self.assertLess(receipt["duration_seconds"], 5)
+ def test_a_shard_out_of_time_names_the_files_it_did_not_reach(self):
+ """Task #3458: shard 3 hit its limit with a few files never reached, and nothing said which."""
+ result, receipt, calls = self.run_driver("partial", ["--timeout", "2"])
+ self.assertEqual(result.returncode, 1)
+ self.assertEqual(receipt["checks"][0]["status"], "timeout")
+ self.assertEqual(receipt["unreached_files"], ["test/two_test.dart"])
+ self.assertIn("Not reached before the time limit (1 file): test/two_test.dart", result.stdout)
+ self.assertIn("::error title=Desktop shard out of time::", result.stdout)
+ self.assertNotIn("one_test.dart", result.stdout.split("Not reached", 1)[1].splitlines()[0])
+
+ def test_a_run_that_finishes_names_no_unreached_files(self):
+ result, receipt, calls = self.run_driver()
+ self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
+ self.assertEqual(receipt.get("unreached_files"), [])
+ self.assertNotIn("Not reached", result.stdout)
+
+
if __name__ == "__main__":
unittest.main()