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.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
fix(backend): a worker's repeated question answered once counts once on the leaderboard (#1936)
0xken15e1471dbccd49
diff --git a/backend/integration/superTabs.test.ts b/backend/integration/superTabs.test.tsindex 0cee5ff63..5844c787d 100644--- a/backend/integration/superTabs.test.ts+++ b/backend/integration/superTabs.test.ts@@ -2960,6 +2960,25 @@ describe('leaderboard credits written by the store (leaderboard/record.ts)', ()await run(tab, { type: 'decision.answer', decisionId, choice: 1 }, peer)expect(await unblocks(tab, 'lead')).toEqual([expect.objectContaining({ personId: peer.sub, kind: 'decision', item: decisionId })])})++ it('credits a worker\'s question asked again after it moved on once, when its decision is answered', async () => {+ const { tab } = await fixture()+ await invite(tab)+ const group = (await run(tab, { type: 'conversation.create', title: 'Release', brief: 'Ship it.', checks: [{ description: 'Shipped.', independent: false }] })).result.conversationId as string+ const worker = (await run(tab, { type: 'agent.add', conversationId: group, machineId: 'test-host', harnessId: randomUUID(),+ name: 'Builder', configuration: config, lead: false })).result.agentId as string+ const assignmentId = (await run(tab, { type: 'work.assign', conversationId: group, agentId: worker })).result.assignmentId as string+ const ask = { type: 'work.status', conversationId: group, assignmentId, status: 'needs_human', result: 'Ship how?',+ ask: { personId: peer.sub, options: ['Behind a flag', 'To everyone'], recommended: 0, unblocks: 'The release' } } as Command+ await run(tab, ask, host, worker)+ await run(tab, { type: 'work.status', conversationId: group, assignmentId, status: 'working', result: null } as Command, host, worker)+ await run(tab, ask, host, worker)+ const [decision] = await db.superTabDecision.findMany({ where: { tabId: tab } })+ await run(tab, { type: 'decision.answer', decisionId: decision!.id, choice: 0 }, peer)+ // The requester's next post answers nothing more: the repeated question was that decision again.+ await run(tab, { type: 'message.send', conversationId: group, text: 'Behind a flag, then.', replyTo: null, recipientIds: [] })+ expect(await unblocks(tab, group)).toEqual([expect.objectContaining({ personId: peer.sub, kind: 'decision', item: decision!.id })])+ })})describe('credentials in Swarm chat', () => {diff --git a/backend/src/superTabs/commands.ts b/backend/src/superTabs/commands.tsindex 21b07e33a..9c7c8123b 100644--- a/backend/src/superTabs/commands.ts+++ b/backend/src/superTabs/commands.ts@@ -277,6 +277,9 @@ export interface CommandContext {/** For a worker's needs_human that asks: the decisions it has open for that person when its question would pass the cap,* read by the store (decisions.ts fullFor). */openDecisions?: number | null+ /** For a worker's needs_human that asks: the open decision its question exactly repeats, which it asks again rather than+ * anew, read by the store (decisions.ts repeatOf). */+ repeatedDecision?: string | null/** The tab's contributed machines whose account row is signed in as a Harness Team account (`Machine.sessionKind`),* read by the store for the commands that start, assign or route work (machines.ts teamWorkCheck). */teamMachines?: string[]@@ -1754,6 +1757,7 @@ export function applyCommand(original: SuperTabState, actor: Actor, input: Commaif (command.status === 'done' && (!command.result || !group.checks.every(c => c.passedBy && c.evidence))) {fail('CHECKS_PENDING', 'Completion needs a result and evidence for every acceptance check.')}+ let asked: string | undefinedif (command.ask) {if (command.status !== 'needs_human' || !command.result || actor.kind !== 'agent') {fail('INVALID_DECISION', 'An agent asks a person for a decision only when its work needs a human, with the question as its result.', 400)@@ -1762,13 +1766,16 @@ export function applyCommand(original: SuperTabState, actor: Actor, input: Commaconst drafts = askDecisions(state, group, personId, [{ question: command.result, ...choices }],{ operationId, askerAgentId: actor.agentId, assignmentId: assignment.id })// At the cap the status still counts, reaches the Lead and renews the lease; only the question waits (review of #1859).- if (context.openDecisions == null) effects.decisions = drafts+ if (context.openDecisions == null) {+ effects.decisions = drafts+ asked = context.repeatedDecision ?? drafts[0]!.id+ }else effects.result.notAsked = `Not asked ${person(state, personId).email}: ${context.openDecisions} decisions you asked them are still open, and at most ${MAX_OPEN_DECISIONS} can be. Wait for answers before asking more.`}const changed = assignment.status !== command.status || assignment.result !== command.result || !!command.ask, previous = assignment.statusassignment.status = command.statusassignment.result = command.result- recordStatus(group, assignment, previous, context.now, changed ? operationId : null)+ recordStatus(group, assignment, previous, context.now, changed ? operationId : null, asked)if (['done', 'stopped'].includes(command.status)) finishWorker(state, assignment.agentId, group)// Finished work shows the worker recovered from a task it lost before.if (command.status === 'done') delete state.agents.find(a => a.id === assignment.agentId)?.lostTaskdiff --git a/backend/src/superTabs/decisions.ts b/backend/src/superTabs/decisions.tsindex 79771fa19..ad890d2a0 100644--- a/backend/src/superTabs/decisions.ts+++ b/backend/src/superTabs/decisions.ts@@ -117,15 +117,20 @@ export async function writeDecisions(tx: Tx, tabId: string, drafts: DecisionDrafreturn drafts.map(d => held.get(repeatKey(d)) ?? d.id)}+/** The open decision an ask exactly repeats, which writeDecisions keeps as that decision again. Read before a worker's+ * needs_human that asks, so its wait names the decision it is waiting on (leaderboard/record.ts recordStatus). */+export async function repeatOf(tx: Tx, tabId: string, ask: Parameters<typeof repeatKey>[0]): Promise<string | undefined> {+ const same = await tx.superTabDecision.findMany({ where: { tabId, personId: ask.personId, askerAgentId: ask.askerAgentId,+ conversationId: ask.conversationId, question: ask.question, ...waiting } })+ return same.find(o => repeatKey(o) === repeatKey(ask))?.id+}+/** How many decisions this asker has open for this person when one more question would pass MAX_OPEN_DECISIONS, or null when- * it fits: an exact repeat of an open one is that decision again and always fits. Read before a worker's needs_human, which- * at the cap still records its status and only skips the question (review of #1859). */-export async function fullFor(tx: Tx, tabId: string, ask: Parameters<typeof repeatKey>[0]): Promise<number | null> {- const { personId, askerAgentId } = ask- const asked = await tx.superTabDecision.count({ where: { tabId, personId, askerAgentId, ...waiting } })- if (asked < MAX_OPEN_DECISIONS) return null- const same = await tx.superTabDecision.findMany({ where: { tabId, personId, askerAgentId, conversationId: ask.conversationId, question: ask.question, ...waiting } })- return same.some(o => repeatKey(o) === repeatKey(ask)) ? null : asked+ * it fits. Read before a worker's needs_human asks something new (an exact repeat of an open one, repeatOf, always fits),+ * which at the cap still records its status and only skips the question (review of #1859). */+export async function fullFor(tx: Tx, tabId: string, ask: { personId: string; askerAgentId: string }): Promise<number | null> {+ const asked = await tx.superTabDecision.count({ where: { tabId, personId: ask.personId, askerAgentId: ask.askerAgentId, ...waiting } })+ return asked < MAX_OPEN_DECISIONS ? null : asked}export async function readAskedDecision(tx: Tx, tabId: string, id: string): Promise<DecisionTarget | undefined> {diff --git a/backend/src/superTabs/leaderboard/record.ts b/backend/src/superTabs/leaderboard/record.tsindex 7153b22b5..e6cf63af4 100644--- a/backend/src/superTabs/leaderboard/record.ts+++ b/backend/src/superTabs/leaderboard/record.ts@@ -25,9 +25,10 @@ export interface GroupLedger {shippedAt?: number/** The earliest merge the Lead recorded (recordMerged); once there is one, it is the ship time. */mergedAt?: number- /** The item waiting on a person: the attempt, since when, the "Needs a human" row it posted, and once a person answered it,- * when. An answered item no longer waits, though its worker has not moved on yet. */- waiting?: { assignmentId: string; since: number; messageId: string; answeredAt?: number }+ /** 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. */@@ -48,13 +49,14 @@ export function recordAsked(group: Conversation, requesterId: string | undefinedgroup.ledger = { askedAt: now }}-/** A status change opens, closes or ships: needs_human starts a wait (again, when the agent asks anew after an answer), any- * other status ends it, done ships once. */-export function recordStatus(group: Conversation, assignment: Assignment, previous: WorkStatus, now: number, messageId: string | null): void {+/** 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 == nullif (assignment.status === 'needs_human' && messageId && (previous !== 'needs_human' || !open)) {- ledger.waiting = { assignmentId: assignment.id, since: now, messageId }+ ledger.waiting = { assignmentId: assignment.id, since: now, messageId, ...decisionId ? { decisionId } : {} }} else if (assignment.status !== 'needs_human') delete ledger.waitingif (assignment.status === 'done') ledger.shippedAt ??= now}@@ -102,12 +104,14 @@ export function approves(text: string): boolean {}/** 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, asked in the same command (same time): answering it answers the wait, and once the wait is answered,- * by a post or this decision, the other earns nothing more. */+ * 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. */export function recordDecision(group: Conversation, decision: { id: string; personId: string; createdAt: number; assignmentId: string | null }, now: number): void {if (group.audience || isLeads(group)) returnconst waiting = group.ledger?.waiting- if (decision.assignmentId && waiting?.assignmentId === decision.assignmentId && waiting.since === decision.createdAt) {+ if (waiting && (waiting.decisionId ? waiting.decisionId === decision.id+ : !!decision.assignmentId && waiting.assignmentId === decision.assignmentId && waiting.since === decision.createdAt)) {if (waiting.answeredAt != null) returnwaiting.answeredAt = now}diff --git a/backend/src/superTabs/store.ts b/backend/src/superTabs/store.tsindex 94d3a0af2..522572402 100644--- a/backend/src/superTabs/store.ts+++ b/backend/src/superTabs/store.ts@@ -42,7 +42,7 @@ import { accrueLent } from './leaderboard/lent.js'import { mayDrive, TERMINAL_ANSWER_MS, TERMINAL_EXPIRY_MS, TERMINAL_PICKUP_MS, TERMINAL_RECHECK_MS, type TerminalEffect } from './terminal.js'import { closeAssignment, closeUnassignedStart, readDecision, recentDecisions, timedDecisionOf, writeRouteEffects } from './routeDecisions.js'import { listMentions, markDone, mentionsWaiting, openTags, readMention, resolveByReply, writeMentions } from './mentions.js'-import { answerable, fullFor, listDecisions, readAskedDecision, recordAnswer, waitingIn, waitingInProject, writeDecisions } from './decisions.js'+import { answerable, fullFor, listDecisions, readAskedDecision, recordAnswer, repeatOf, waitingIn, waitingInProject, writeDecisions } from './decisions.js'import { DEFAULT_ROUTING_POLICY, effectivePolicy, routingPolicySchema, type RoutingPolicy } from './routingPolicy.js'import { authenticatedEmail, authorizeSwarmJoin, configureSharing, joinSwarmSchema, requireSharingOwner,saveSharingSchema, sharingDetails, type JoinSwarm, type SaveSharing } from './sharing.js'@@ -927,8 +927,10 @@ export class SuperTabStore {if (command.type === 'mention.done') context.mention = await readMention(tx, tabId, command.mentionId)if (command.type === 'decision.answer') context.decision = await readAskedDecision(tx, tabId, command.decisionId)if (command.type === 'work.status' && command.ask && command.result && actor.kind === 'agent') {- context.openDecisions = await fullFor(tx, tabId, { ...command.ask, askerAgentId: actor.agentId, assignmentId: command.assignmentId,- conversationId: command.conversationId, question: command.result })+ const ask = { ...command.ask, askerAgentId: actor.agentId, assignmentId: command.assignmentId, conversationId: command.conversationId,+ question: command.result }+ context.repeatedDecision = await repeatOf(tx, tabId, ask) ?? null+ context.openDecisions = context.repeatedDecision ? null : await fullFor(tx, tabId, ask)}if (command.type === 'message.send' && command.attachmentIds?.length) context.attachments = await this.sendable(tx, tabId, actor, command)if (command.type === 'message.react') context.reaction = await this.reactionTarget(tx, tabId, actor, command)