Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

Swarm leaderboard: read only the fields the board uses of dormant groups, and each person's records once (#1852)

0xken15f3c3909df838
diff --git a/backend/src/superTabs/leaderboard/read.test.ts b/backend/src/superTabs/leaderboard/read.test.ts
new file mode 100644
index 000000000..ca8106a90
--- /dev/null
+++ b/backend/src/superTabs/leaderboard/read.test.ts
@@ -0,0 +1,32 @@
+import { describe, expect, it } from 'vitest'
+import type { Prisma } from '@prisma/client'
+import { recentDormantGroups } from './read.js'
+
+// Diego's issue file (2026-10-27): the board loaded every dormant group of the last 5 weeks whole, once a minute per open
+// column. It reads only what the board and its read check use (stats.ts, projects.ts canReadGroup).
+describe('the dormant groups the leaderboard sums', () => {
+ const since = Date.UTC(2026, 9, 1)
+ const reading = (rows: unknown[]) => {
+ const asked: unknown[] = []
+ const tx = { swarmGroup: {
+ aggregateRaw: async (args: unknown) => { asked.push(args); return rows },
+ findMany: async () => { throw new Error('the whole group was loaded') },
+ } } as unknown as Prisma.TransactionClient
+ return { tx, asked }
+ }
+
+ it('reads only the fields the board uses, never a group whole', async () => {
+ const { tx, asked } = reading([{ data: { id: 'g1', requesterId: 'peer', ledger: { shippedAt: since + 1 }, assignment: { id: 'a1', status: 'done' } } }])
+ expect(await recentDormantGroups(tx, 'tab', 'web', since)).toEqual([{ id: 'g1', requesterId: 'peer', ledger: { shippedAt: since + 1 }, assignment: { id: 'a1', status: 'done' } }])
+ const [{ pipeline }] = asked as [{ pipeline: Array<Record<string, Record<string, unknown>>> }]
+ expect(pipeline[0]!.$match).toEqual({ tabId: 'tab', deleted: false, dormant: true, lastActivityAt: { $gte: { $date: new Date(since).toISOString() } }, projectId: 'web' })
+ expect(Object.keys(pipeline[1]!.$project!).sort()).toEqual(['_id', ...['archived', 'assignment.id', 'assignment.status', 'audience', 'id', 'kind', 'ledger',
+ 'projectId', 'pullRequestUrls', 'requesterId', 'reviewOf'].map(field => `data.${field}`)].sort())
+ })
+
+ it('reads the default project\'s groups stored with no project too', async () => {
+ const { tx, asked } = reading([])
+ await recentDormantGroups(tx, 'tab', 'lead', since)
+ expect((asked[0] as { pipeline: Array<Record<string, Record<string, unknown>>> }).pipeline[0]!.$match).toMatchObject({ $or: [{ projectId: 'lead' }, { projectId: null }] })
+ })
+})
diff --git a/backend/src/superTabs/leaderboard/read.ts b/backend/src/superTabs/leaderboard/read.ts
index 11d537d86..0a75f0626 100644
--- a/backend/src/superTabs/leaderboard/read.ts
+++ b/backend/src/superTabs/leaderboard/read.ts
@@ -14,11 +14,19 @@ export function readableWeek(week: number | undefined, now: number): number {
return week
}
-/** Dormant groups of one project active since [since]: a request shipped in a week the board sums wrote its Done row then. */
+/** What the board reads of a group (stats.ts, record.ts waitsNow) and its read check (projects.ts canReadGroup). */
+const BOARD_FIELDS = ['id', 'kind', 'projectId', 'audience', 'requesterId', 'reviewOf', 'archived', 'pullRequestUrls', 'ledger',
+ 'assignment.id', 'assignment.status'] as const
+
+/** Dormant groups of one project active since [since]: a request shipped in a week the board sums wrote its Done row then.
+ * Only the fields the board reads: every open board reads them once a minute (Diego's issue file, 2026-10-27). */
export async function recentDormantGroups(tx: Prisma.TransactionClient, tabId: string, projectId: string, since: number): Promise<Conversation[]> {
- // A group stored with no project belongs to the default one (projects.ts groupProject), as in a Swarm from before projects.
- const project = projectId === 'lead' ? { OR: [{ projectId: 'lead' }, { projectId: null }, { projectId: { isSet: false } }] } : { projectId }
- const rows = await tx.swarmGroup.findMany({ where: { tabId, deleted: false, dormant: true, lastActivityAt: { gte: new Date(since) }, ...project },
- select: { data: true } })
- return rows.map(row => row.data as unknown as Conversation)
+ // A group stored with no project belongs to the default one (projects.ts groupProject), as in a Swarm from before projects;
+ // a null match is a missing field too.
+ const project = projectId === 'lead' ? { $or: [{ projectId: 'lead' }, { projectId: null }] } : { projectId }
+ const rows = await tx.swarmGroup.aggregateRaw({ pipeline: [
+ { $match: { tabId, deleted: false, dormant: true, lastActivityAt: { $gte: { $date: new Date(since).toISOString() } }, ...project } },
+ { $project: { _id: 0, ...Object.fromEntries(BOARD_FIELDS.map(field => [`data.${field}`, 1])) } },
+ ] }) as unknown as Array<{ data: Conversation }>
+ return rows.map(row => row.data)
}
diff --git a/backend/src/superTabs/leaderboard/stats.ts b/backend/src/superTabs/leaderboard/stats.ts
index 439014891..55ec4c6ee 100644
--- a/backend/src/superTabs/leaderboard/stats.ts
+++ b/backend/src/superTabs/leaderboard/stats.ts
@@ -37,10 +37,14 @@ export function leaderboard({ state, groups, machines: visibleMachines, projectI
const landed = requests.filter(g => g.requesterId && g.ledger?.shippedAt != null && (g.pullRequestUrls?.length ?? 0) > 0)
const players = state.people.filter(p => canReadProject(state, { kind: 'person', userId: p.id, email: p.email }, project))
const inWeek = (at: number, start = week) => at >= start && at < start + WEEK_MS
+ // Each person's records, gathered once: the board scores every player three times (Diego's issue file, 2026-10-27).
+ const landedBy = byKey(landed, g => g.requesterId!)
+ const answersBy = byKey(shared.flatMap(g => g.ledger?.unblocks ?? []), u => u.personId)
+ const machinesBy = byKey(visibleMachines, m => m.contributorId)
const score = (personId: string, start: number) => {
- const shippedHere = landed.filter(g => g.requesterId === personId && inWeek(g.ledger!.shippedAt!, start))
- const answers = shared.flatMap(g => g.ledger?.unblocks ?? []).filter(u => u.personId === personId && inWeek(u.at, start))
- const machines = visibleMachines.filter(m => m.contributorId === personId)
+ const shippedHere = (landedBy.get(personId) ?? []).filter(g => inWeek(g.ledger!.shippedAt!, start))
+ const answers = (answersBy.get(personId) ?? []).filter(u => inWeek(u.at, start))
+ const machines = (machinesBy.get(personId) ?? [])
.map(m => m.lent?.find(l => l.week === start)?.agentMs ?? 0).filter(ms => ms > 0)
const freedMs = sum(answers.map(u => u.waitedMs)), lentMs = sum(machines)
const points = shippedHere.length * SHIPPED_POINTS + answers.length * UNBLOCK_POINTS
@@ -58,7 +62,7 @@ export function leaderboard({ state, groups, machines: visibleMachines, projectI
}
const today = Math.floor(Math.min(now, week + WEEK_MS - 1) / DAY_MS)
const scored = players.map(p => ({ personId: p.id, email: p.email, ...score(p.id, week),
- streakDays: streak(new Set(landed.filter(g => g.requesterId === p.id).map(g => Math.floor(g.ledger!.shippedAt! / DAY_MS))), today) }))
+ streakDays: streak(new Set((landedBy.get(p.id) ?? []).map(g => Math.floor(g.ledger!.shippedAt! / DAY_MS))), today) }))
.sort((a, b) => b.points - a.points || a.email.localeCompare(b.email))
const people: PersonScore[] = scored.map(({ email: _email, ...entry }) => {
const rank = 1 + scored.filter(other => other.points > entry.points).length
@@ -109,6 +113,15 @@ function streak(days: Set<number>, today: number): number {
const askToShip = (groups: Conversation[]): number[] =>
groups.flatMap(g => g.ledger?.askedAt != null ? [g.ledger.shippedAt! - g.ledger.askedAt] : [])
+function byKey<T>(items: T[], key: (item: T) => string): Map<string, T[]> {
+ const out = new Map<string, T[]>()
+ for (const item of items) {
+ const list = out.get(key(item))
+ if (list) list.push(item)
+ else out.set(key(item), [item])
+ }
+ return out
+}
const sum = (values: number[]): number => values.reduce((total, value) => total + value, 0)
function median(values: number[]): number | null {
if (!values.length) return null