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.

24.2 KB
Raw Blame
/**
 * What the leaderboard counts, recorded as it happens on the group it happened in (docs/swarms.md, Leaderboard): when a
 * request was asked and first shipped, which item waits on a person, and who answered it. Only facts and times, never text.
 */
import { isLeads, type Actor, type Agent, type AgentConfiguration, type Assignment, type Author, type Check, type Conversation, type SuperTabState, type WorkStatus } from '../model.js'
import { groupProject, ledProject } from '../projects.js'
import { HOUR_MS } from './rolling.js'
import { WEEK_MS, weekStart } from './week.js'
import { agentTarget } from '../rules.js'
import type { UnblockKind } from './contract.js'

/** How a person unblocked work (Dee, 2026-10-28): answered a decision, approved the Lead's plan, answered work waiting on them,
 *  or replied to a tag. `item`: what was answered (the decision, the Lead's ask, the waiting question or the tag message),
 *  credited once. Both are absent from answers recorded before kinds, which were all answers to waiting work. */
export interface Unblock { personId: string; at: number; waitedMs: number; kind?: UnblockKind; item?: string }
/** One attempt at a group's work: what it ran on and whether its first review passed (roadmap, Dec: about 30% of work on
 *  cheaper models without lower quality). `grid`: a model served on the Swarm's grid, named by its start, since the engine
 *  reports none of its own. `firstVerdicts`: each check's first verdicts during this attempt by anyone but its assignee, by
 *  check id: false once any checker's first verdict on it failed (#2311). `firstReview`: false once any of them failed, true
 *  while every one so far passed, absent until the first. */
export interface Run extends RanOn { assignmentId: string; at: number
  /** The agent and its terminal that ran it, so the model that terminal reports later fills this attempt only (record
   *  ReportedModel). Absent from attempts recorded before them. */
  agentId?: string; harnessId?: string
  /** The machine it ran on, so a person's card can say what their machine shipped (contributor view, #2444). Absent from
   *  attempts recorded before it; the board then finds it by the agent while that agent exists (brought.ts runMachine). */
  machineId?: string
  firstVerdicts?: Record<string, boolean>; firstReview?: boolean
  /** The step up from a cheaper model this attempt is, on a task stepped up before project instructions: its attempt number and why. */
  stepUp?: { attempt: number; reason: string }
  /** Its agent was launched on the engine's own model, which Codex reports by a slug the policy may not name: the board rates
   *  it by the engine's `default` row (review of ee4ee6c9a). */
  engineDefault?: true
  /** Re-reviews after a failed review during this attempt (evidence routing, Ken 2026-10-29): the most on any one check, so
   *  a review that judges two checks counts once. Absent until a review fails; 0 while a failure waits for its
   *  re-review. `reworks`: each checker's last verdict on a check and its re-reviews after its own failure, by check id and
   *  checker's actor key (`${checkId} ${by}`); one checker passing what another failed is no re-review (#2311). Attempts
   *  recorded before kept them by check id alone. */
  reworkRounds?: number
  reworks?: Record<string, { last: boolean; rounds: number }> }
/** What an agent ran on: its engine, the model (a grid model's own name, `grid` set), and its effort when it names one;
 *  absent from attempts recorded before efforts were (Lead, 2026-10-29: never guessed afterwards). */
export interface RanOn { engine: string; model: string | null; effort?: string; grid?: true }
/** How many review verdicts agents running on one model gave on the group's work in one week (Monday 00:00 UTC): a reviewer
 *  model's points (Lead, 2026-10-29). Counted, not listed, so no cap ever drops an earned point (review of #2097). A
 *  person's verdict is no model's. */
export interface ReviewCount extends RanOn { week: number; count: number }
/** The same verdicts by hour, for the rolling periods (rolling.ts): one row per hour and model that had one. */
export interface ReviewHour extends RanOn { hour: number; count: number }
/** A group's leaderboard bookkeeping; readers never get it whole (read/views.ts groupView). */
export interface GroupLedger {
  askedAt?: number
  /** Why nobody is named as its requester, said by the agent that opened work no person asked for (taskContract.ts). */
  noRequesterReason?: string
  /** When it shipped: its earliest recorded merge, or without one the first time its work was done. A group is one request,
   *  so finishing it again ships nothing new. */
  shippedAt?: number
  /** The earliest merge the Lead recorded (recordMerged); once there is one, it is the ship time. */
  mergedAt?: number
  /** When the first follow-up fix of its merged PR merged, within FIX_WINDOW_MS of its merge (recordFix): it needed a fix
   *  after merge (evidence routing, Ken 2026-10-29). */
  fixedAfterMergeAt?: number
  /** The group whose merged PR this group's merge fixed. */
  fixOf?: string
  /** The item waiting on a person: the attempt, since when, the "Needs a human" row it posted, the decision it asked with it
   *  (the same one again when the worker repeats an open question), and once a person answered it, when. An answered item no
   *  longer waits, though its worker has not moved on yet. A wait recorded before decisions were named has none. */
  waiting?: { assignmentId: string; since: number; messageId: string; decisionId?: string; answeredAt?: number }
  /** Answers within the last LEDGER_WEEKS, oldest first. */
  unblocks?: Unblock[]
  /** The last MAX_RUNS attempts, oldest first. */
  runs?: Run[]
  /** When its first attempt was assigned: kept apart, as that attempt rolls off [runs] after MAX_RUNS (review of #2097). */
  firstAssignedAt?: number
  /** The attempt it shipped on, and what that ran on, kept from the moment it shipped: a later attempt or older attempts
   *  rolling off never move its model's credit (review of #2097). */
  shippedBy?: RanOn & { assignmentId: string; at: number; machineId?: string }
  /** Who led the group when it shipped, null for nobody: the PM's credit, kept whoever leads it later (review of #2097). */
  shippedLeadId?: string | null
  /** Review verdicts by week and model, for the last LEDGER_WEEKS weeks, oldest first. */
  reviews?: ReviewCount[]
  /** The same by hour, for the last LEDGER_WEEKS weeks. */
  reviewHours?: ReviewHour[]
  /** The Buddy's proposal the group was opened from: the Buddy earns when it ships (Lead, 2026-10-29). */
  proposal?: { buddyId: string; messageId: string }
}

/** The leaderboard reads this week and last; a little more keeps a streak and week boundaries safe. */
export const LEDGER_WEEKS = 5
/** A group is one request; more attempts than this are a stuck task, and the oldest say nothing new. */
export const MAX_RUNS = 20
/** Credits one conversation keeps, newest: a busy Lead conversation collects approvals and tag replies for weeks. */
export const MAX_UNBLOCKS = 200

/** A shared group is asked when it is opened. A private conversation is chat only and never counts. */
export function recordAsked(group: Conversation, requesterId: string | undefined, now: number): void {
  if (group.audience) return
  if (requesterId) group.requesterId = requesterId
  group.ledger = { askedAt: now }
}

/** A status change opens, closes or ships: needs_human starts a wait (again, when the agent asks anew after an answer), naming
 *  the decision it asked, any other status ends it, done ships once. */
export function recordStatus(group: Conversation, assignment: Assignment, previous: WorkStatus, now: number, messageId: string | null,
  decisionId?: string): void {
  const ledger = group.ledger ??= {}
  const open = ledger.waiting?.assignmentId === assignment.id && ledger.waiting.answeredAt == null
  if (assignment.status === 'needs_human' && messageId && (previous !== 'needs_human' || !open)) {
    ledger.waiting = { assignmentId: assignment.id, since: now, messageId, ...decisionId ? { decisionId } : {} }
  } else if (assignment.status !== 'needs_human') delete ledger.waiting
  if (assignment.status === 'done' && ledger.shippedAt == null) {
    ledger.shippedAt = now
    shipOn(group, ledger.runs?.find(r => r.assignmentId === assignment.id))
  }
}

/** Keeps who led a request when it first shipped, and the attempt it shipped on: the given one, else the last one
 *  assigned by the time it shipped. Once kept, never rewritten. */
function shipOn(group: Conversation, run?: Run): void {
  const ledger = group.ledger!
  if (ledger.shippedLeadId !== undefined) return
  ledger.shippedLeadId = group.leadId ?? null
  shipAttempt(ledger, run)
}

/** The attempt a request shipped on: the given one, else the latest started by the time it shipped, else (merged before any
 *  attempt started) the earliest. */
function shipAttempt(ledger: GroupLedger, run?: Run): void {
  const shipping = run ?? ledger.runs?.filter(r => r.at <= ledger.shippedAt!).at(-1) ?? ledger.runs?.[0]
  if (!shipping) return
  const { firstVerdicts: _verdicts, firstReview: _review, reworkRounds: _rounds, reworks: _reworks, agentId: _agent, harnessId: _harness,
    ...shippedBy } = shipping
  ledger.shippedBy = shippedBy
}

/** One GitHub pull request link, as a merge record names it (model.ts appendPullRequestUrls finds the same in text). */
export const PULL_REQUEST_URL = /^https:\/\/github\.com\/[A-Za-z0-9_-]+\/[A-Za-z0-9_.-]+\/pull\/[1-9][0-9]*$/

/** The Lead's record that a request's PR merged (Lead, 2026-10-27): the request ships at its earliest recorded merge, even
 *  when Done came first, and whatever its work does next, so work lost or cancelled after the merge still counts. */
export function recordMerged(group: Conversation, mergedAt: number): void {
  const ledger = group.ledger ??= {}
  const before = ledger.shippedAt
  ledger.mergedAt = Math.min(ledger.mergedAt ?? mergedAt, mergedAt)
  ledger.shippedAt = ledger.mergedAt
  // Who led it and which attempt shipped are kept from the first time it shipped. A merge that moves the ship earlier
  // takes the attempt running then; the lead stays, as no history of leads is kept (Lead, 2026-10-29).
  if (ledger.shippedLeadId !== undefined && before != null && ledger.shippedAt < before) shipAttempt(ledger)
  else shipOn(group)
}

/** How long after a merge a follow-up fix still counts against it (evidence routing spec, PM call 10). */
export const FIX_WINDOW_MS = 14 * 86_400_000

/** The merged group a fix PR merged at [mergedAt] fixes: a shared group of the same project, not [fix] itself nor a review,
 *  that links [url] and merged at most FIX_WINDOW_MS before. Undefined for anything else, which its caller refuses alike, so
 *  a link names nothing outside the project. */
export function fixedOriginal(state: SuperTabState, fix: Conversation, url: string, mergedAt: number): Conversation | undefined {
  const project = groupProject(state, fix).id
  return state.conversations.find(g => g.id !== fix.id && !g.reviewOf && !g.audience && !isLeads(g) && g.pullRequestUrls?.includes(url)
    && g.ledger?.mergedAt !== undefined && mergedAt >= g.ledger.mergedAt && mergedAt - g.ledger.mergedAt <= FIX_WINDOW_MS
    && groupProject(state, g).id === project)
}

/** Records that [fix]'s merge at [mergedAt] fixed [original]'s merged PR. Only the first fix counts against it; true when this
 *  one is that first. */
export function recordFix(original: Conversation, fix: Conversation, mergedAt: number): boolean {
  const ledger = original.ledger!
  ;(fix.ledger ??= {}).fixOf = original.id
  if (ledger.fixedAfterMergeAt !== undefined && ledger.fixedAfterMergeAt <= mergedAt) return false
  ledger.fixedAfterMergeAt = mergedAt
  return true
}

/** The person a waiting item waits on: its requester while still a member, otherwise the project's owner. */
export function waitedOn(state: SuperTabState, group: Conversation): string {
  const requester = group.requesterId && state.people.some(p => p.id === group.requesterId) ? group.requesterId : undefined
  return requester ?? groupProject(state, group).ownerId
}

/** What a person's post can answer besides waiting work: the message it replies to, and the tags waiting on the poster in
 *  this conversation (mentions.ts), both read by the store. */
export interface PostContext { replyTo: string | null; text: string; reply?: { id: string; author: Author; createdAt: number }
  openTags?: Array<{ messageId: string; createdAt: number }> }

/** A person's post earns one credit at most, for the first it answers of: the work waiting on them, the Lead's ask it approves,
 *  the oldest tag waiting on them here (Lead's rulings, 2026-10-28). Private chats never count. */
export function recordPost(state: SuperTabState, group: Conversation, actor: Actor, post: PostContext, now: number): void {
  if (actor.kind !== 'person' || group.audience || isLeads(group)) return
  if (recordAnswer(state, group, actor.userId, post.replyTo, now)) return
  const reply = post.reply
  if (reply && reply.author.kind === 'agent' && approves(post.text) && ledProject(state, reply.author.id)?.id === groupProject(state, group).id) {
    if (credit(group, { personId: actor.userId, at: now, waitedMs: now - reply.createdAt, kind: 'approval', item: reply.id })) return
  }
  const oldest = [...post.openTags ?? []].sort((a, b) => a.createdAt - b.createdAt)[0]
  if (oldest) credit(group, { personId: actor.userId, at: now, waitedMs: now - oldest.createdAt, kind: 'tag_reply', item: oldest.messageId })
}

/** A reply that starts with GO, approve(d) or duyệt, in any case, with or without accents; a bare "ok" is too ambiguous
 *  (Lead, 2026-10-28). */
export function approves(text: string): boolean {
  const plain = text.normalize('NFD').replace(/\p{M}/gu, '').replace(/đ/giu, 'd').trim().toLowerCase()
  return /^(go|approved?|duyet)(?![\p{L}\p{N}])/u.test(plain)
}

/** The person asked answered a decision: credited from when it was asked. A decision a worker asked through needs_human is
 *  that wait's question, named by the wait, also when the worker asked it again after moving on: answering it answers the
 *  wait, and once the wait is answered, by a post or this decision, the other earns nothing more. A wait from before
 *  decisions were named is matched by its time, as it was asked in the same command. A decision left to the PM closes its
 *  wait and is credited to nobody. */
export function recordDecision(group: Conversation, decision: { id: string; personId: string; createdAt: number; assignmentId: string | null }, now: number,
  delegated = false): void {
  if (group.audience || isLeads(group)) return
  const waiting = group.ledger?.waiting
  if (waiting && (waiting.decisionId ? waiting.decisionId === decision.id
    : !!decision.assignmentId && waiting.assignmentId === decision.assignmentId && waiting.since === decision.createdAt)) {
    if (waiting.answeredAt != null) return
    waiting.answeredAt = now
  }
  // Left to the PM: the wait is over, but the person did not choose, so it earns them nothing.
  if (delegated) return
  credit(group, { personId: decision.personId, at: now, waitedMs: now - decision.createdAt, kind: 'decision', item: decision.id })
}

/** A person's post answers the waiting item when they are the one it waits on, or when it replies to the question itself;
 *  a passer-by's chat earns nothing (Lead, 2026-10-27). One answer per wait. */
function recordAnswer(state: SuperTabState, group: Conversation, personId: string, replyTo: string | null, now: number): boolean {
  const waiting = group.ledger?.waiting
  if (!waitsNow(group) || waiting?.assignmentId !== group.assignment!.id) return false
  if (personId !== waitedOn(state, group) && replyTo !== waiting.messageId) return false
  waiting.answeredAt = now
  return credit(group, { personId, at: now, waitedMs: now - waiting.since, kind: 'needs_human', item: waiting.messageId })
}

/** Adds a credit unless its item already has one; keeps the last LEDGER_WEEKS, at most MAX_UNBLOCKS. */
function credit(group: Conversation, unblock: Unblock & { item: string }): boolean {
  const ledger = group.ledger ??= {}
  if (ledger.unblocks?.some(u => u.item === unblock.item)) return false
  const kept = (ledger.unblocks ?? []).filter(u => unblock.at - u.at < LEDGER_WEEKS * WEEK_MS)
  ledger.unblocks = [...kept, { ...unblock, waitedMs: Math.max(0, unblock.waitedMs) }].slice(-MAX_UNBLOCKS)
  return true
}

/** Whether the group's work waits on a person now: needs a human and not yet answered. An item from before answers were
 *  recorded has no wait on record and still waits. */
export function waitsNow(group: Conversation): boolean {
  const waiting = group.ledger?.waiting
  return group.assignment?.status === 'needs_human' && !(waiting?.assignmentId === group.assignment.id && waiting.answeredAt != null)
}

/** An assignment records what its agent runs on (ranOn), for the board's models. */
export function recordRun(state: SuperTabState, group: Conversation, assignment: Assignment, worker: Agent, now: number): void {
  const run: Run = { assignmentId: assignment.id, ...ranOn(state, worker, assignment.configuration), at: now,
    agentId: worker.id, harnessId: worker.harnessId, machineId: assignment.execution.machineId,
    ...(group.stepUp ? { stepUp: { attempt: group.stepUp.attempt, reason: group.stepUp.reason } } : {}),
    ...(agentTarget(state, worker).launchedDefault ? { engineDefault: true as const } : {}) }
  const ledger = group.ledger ??= {}
  ledger.firstAssignedAt ??= firstAssignedAt(ledger) ?? now
  ledger.runs = [...ledger.runs ?? [], run].slice(-MAX_RUNS)
}

/** An agent started on its engine's own model reports which one only once it runs (Codex): its attempts recorded before
 *  then, with none, take the model it reports, so the board names it and step-ups rate it by it rather than by the engine
 *  alone (Dee, 2026-10-30). That holds for a finished or handed-on attempt too (Scholes, 2026-10-30: a review done before
 *  its agent's first report stayed "model not reported"). Only this agent's own attempts in its current terminal: a
 *  replacement terminal (agent.rebind) may run another model. An attempt recorded before attempts named their agent is
 *  matched as its group's current one. A model on record is never rewritten, nor a grid model's, nor another engine's. */
export function recordReportedModel(state: SuperTabState, worker: Agent): void {
  const model = worker.configuration.model
  if (!model || worker.gridModel) return
  const fill = (on: RanOn | undefined) => {
    if (on && on.model === null && !on.grid && on.engine === worker.configuration.engine) on.model = model
  }
  for (const group of state.conversations) {
    const ledger = group.ledger
    if (!ledger?.runs?.length) continue
    const current = group.assignment
    const legacy = current?.agentId === worker.id && current.execution.harnessId === worker.harnessId ? current.id : null
    for (const run of ledger.runs) {
      const own = run.agentId !== undefined ? run.agentId === worker.id && run.harnessId === worker.harnessId : run.assignmentId === legacy
      if (!own) continue
      fill(run)
      // The copy kept when its merge was recorded while it still ran.
      if (ledger.shippedBy?.assignmentId === run.assignmentId) fill(ledger.shippedBy)
    }
  }
}

/** When a request's first attempt was assigned: kept since that was recorded. A request recorded before has its first
 *  attempt in [runs] while fewer than MAX_RUNS are kept; once that many are, the first may have rolled off, so it takes
 *  the time the request was asked (full review of #2097). Undefined for a request never assigned. */
export function firstAssignedAt(ledger: GroupLedger): number | undefined {
  if (ledger.firstAssignedAt != null) return ledger.firstAssignedAt
  const runs = ledger.runs ?? []
  if (!runs.length) return undefined
  return runs.length < MAX_RUNS ? runs[0]!.at : ledger.askedAt ?? runs[0]!.at
}

/** An agent's verdict on a check of another agent's task is one finished review by the model it runs on; the assignee's own
 *  verdicts are no review (checks are information, project instructions phase 4: `independent` is read by nothing).
 *  [first]: the checker gave no verdict on this check during this attempt before. */
export function recordVerdict(state: SuperTabState, group: Conversation, check: Check, checker: Agent, now: number, first: boolean): void {
  if (checker.id === group.assignment?.agentId) return
  const ledger = group.ledger ??= {}
  // A check given again on the same attempt by the same checker is the same review (full review of #2097): only each
  // checker's first verdict counts, so a Reviewer after a Tester earns its own (#2311). An attempt recorded before attempts
  // were has no run to tell a repeat by, so it earns no model credit (review of #2128).
  const run = ledger.runs?.find(r => r.assignmentId === group.assignment?.id)
  if (!run || !first) return
  const on = ranOn(state, checker, checker.configuration), week = weekStart(now)
  // Only the weeks the board reads: one row per week and model, however many verdicts.
  const kept = (ledger.reviews ?? []).filter(r => week - r.week < LEDGER_WEEKS * WEEK_MS)
  const same = kept.find(r => r.week === week && r.engine === on.engine && r.model === on.model && r.effort === on.effort && r.grid === on.grid)
  if (same) same.count++
  else kept.push({ week, ...on, count: 1 })
  ledger.reviews = kept
  const hour = Math.floor(now / HOUR_MS) * HOUR_MS
  const hours = (ledger.reviewHours ?? []).filter(r => now - r.hour < LEDGER_WEEKS * WEEK_MS)
  const sameHour = hours.find(r => r.hour === hour && r.engine === on.engine && r.model === on.model && r.effort === on.effort && r.grid === on.grid)
  if (sameHour) sameHour.count++
  else hours.push({ hour, ...on, count: 1 })
  ledger.reviewHours = hours
}

/** A group opened from a Buddy's proposal remembers it, for the Buddy's points when it ships. */
export function recordProposal(group: Conversation, proposal: { buddyId: string; messageId: string }): void {
  (group.ledger ??= {}).proposal = proposal
}

/** What [worker] runs on: the grid model its start named, else what its configuration reports. An agent started before it
 *  kept its grid model has it only on its start, which is here while that start is not finished. */
function ranOn(state: SuperTabState, worker: Agent, configuration: AgentConfiguration): RanOn {
  const gridModel = worker.gridModel
    ?? state.starts?.find(s => s.harnessId === worker.harnessId && s.request.machineId === worker.machineId)?.request.gridModel
  const effort = namedEffort(configuration.effort)
  return { engine: configuration.engine, model: gridModel ?? configuration.model, ...(effort ? { effort } : {}), ...(gridModel ? { grid: true as const } : {}) }
}

/** An effort that says which one runs: 'auto' is what Claude Code reports for one it was launched with, and 'default' what a
 *  start asks when it leaves the engine its own (desktop agent_line.dart reads them the same way). */
const namedEffort = (effort: string | null): string | undefined =>
  effort && effort !== 'auto' && effort !== 'default' ? effort : undefined

/** Each checker's first verdict on a check during an attempt, by anyone but its assignee, is a first review; its later
 *  verdicts are repairs and never rewrite it (#2311: each checker's, not only the check's first). The attempt's first review
 *  fails once any first verdict failed, and passes while every one so far passed (review of #1808: one check passing first
 *  used to count as a pass while another failed). [by]: the checker's actor key; [first]: as recordVerdict's. */
export function recordReview(group: Conversation, check: Check, by: string, passed: boolean, byAssignee: boolean, first: boolean): void {
  const run = group.ledger?.runs?.find(r => r.assignmentId === group.assignment?.id)
  if (byAssignee || !run) return
  const verdicts = run.firstVerdicts ??= {}
  if (first) verdicts[check.id] = (verdicts[check.id] ?? true) && passed
  recordRework(run, `${check.id} ${by}`, passed)
  run.firstReview = !group.checks.some(c => verdicts[c.id] === false)
}

/** A checker's verdict on a check whose last verdict from it in this attempt failed is a rework round. */
function recordRework(run: Run, key: string, passed: boolean): void {
  const trail = run.reworks?.[key]
  if (!trail && passed) return
  const reworks = run.reworks ??= {}
  reworks[key] = { last: passed, rounds: (trail?.rounds ?? 0) + (trail && !trail.last ? 1 : 0) }
  run.reworkRounds = Math.max(...Object.values(reworks).map(r => r.rounds))
}