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(swarm): cap open decisions at 20 per asker per person and remove answered ones after 5 weeks (#1859)
0xken15443893bd28d2
diff --git a/backend/integration/superTabRead.test.ts b/backend/integration/superTabRead.test.tsindex fd8606de1..9409ded79 100644--- a/backend/integration/superTabRead.test.ts+++ b/backend/integration/superTabRead.test.ts@@ -821,6 +821,66 @@ describe('decisions asked of one person (v2)', () => {expect(await decisions(s.tabId, peer)).toEqual([expect.objectContaining({ id: first })])})+ it('refuses a 21st open decision from one asker to one person, still taking a repeat, and takes it once one is answered', async () => {+ const s = await scene()+ clock = Date.now()+ const ask = (texts: string[]) => post(s.tabId, { type: 'decision.ask', conversationId: s.web, personId: peer.sub, questions: texts.map(question) }, owner, s.lead)+ const batch = (from: number) => Array.from({ length: 10 }, (_, i) => `Question ${from + i}?`)+ const ids = [...(await ask(batch(1))).body.data.result.decisionIds as string[], ...(await ask(batch(11))).body.data.result.decisionIds as string[]]+ expect(ids).toHaveLength(20)+ const refused = await ask(['Question 21?'])+ expect(refused).toMatchObject({ status: 409, body: { error: { code: 'DECISIONS_FULL' } } })+ expect(await decisions(s.tabId, peer)).toHaveLength(20)+ // A repeat of an open one is that decision again, not a new one.+ expect((await ask(['Question 1?'])).body.data.result.decisionIds).toEqual([ids[0]])+ expect((await post(s.tabId, { type: 'decision.answer', decisionId: ids[0], choice: 0 }, peer)).status).toBe(200)+ expect((await ask(['Question 21?'])).status).toBe(200)+ })++ it('at the cap, a worker\'s needs-human update still records its status, tells the Lead and renews its lease, asking nothing new', async () => {+ const s = await scene()+ clock = Date.now()+ const assignment = async () => (await get(`/api/super-tabs/${s.tabId}/parts`, owner)).body.data.groups.find((g: { key: string }) => g.key === s.web).data.assignment+ const assignmentId = (await assignment()).id as string+ await db.superTabDecision.createMany({ data: Array.from({ length: 20 }, (_, i) => ({ id: `full-${s.tabId}-${i}`, tabId: s.tabId, batchId: 'full',+ projectId: 'lead', conversationId: s.web, assignmentId, personId: peer.sub, askerAgentId: s.worker, question: `Question ${i + 1}?`,+ options: ['Behind a flag', 'To everyone'], recommended: 0, unblocks: 'The release', createdAt: new Date() })) })+ const before = (await assignment()).leaseUntil as number+ const needs = await post(s.tabId, { type: 'work.status', conversationId: s.web, assignmentId, status: 'needs_human', result: 'Ship how?',+ ask: { personId: peer.sub, options: ['Behind a flag', 'To everyone'], recommended: 0, unblocks: 'The release' } }, owner, s.worker)+ expect(needs.status, JSON.stringify(needs.body)).toBe(200)+ const notAsked = `Not asked ${peer.email}: 20 decisions you asked them are still open, and at most 20 can be. Wait for answers before asking more.`+ expect(needs.body.data.result.notAsked).toBe(notAsked)+ const after = await assignment()+ expect(after).toMatchObject({ status: 'needs_human', result: 'Ship how?' })+ expect(after.leaseUntil).toBeGreaterThan(before)+ const notice = await db.superTabMessage.findFirst({ where: { tabId: s.tabId, conversationId: s.web }, orderBy: { sequence: 'desc' } })+ expect(notice).toMatchObject({ text: `Needs a human\n\nShip how?\n\n${notAsked}`, recipientIds: [s.lead] })+ expect(await db.superTabDecision.count({ where: { tabId: s.tabId, askerAgentId: s.worker } })).toBe(20)+ // A repeat of one still open is that decision again, so it is asked as before.+ const repeat = await post(s.tabId, { type: 'work.status', conversationId: s.web, assignmentId, status: 'needs_human', result: 'Question 1?',+ ask: { personId: peer.sub, options: ['Behind a flag', 'To everyone'], recommended: 0, unblocks: 'The release' } }, owner, s.worker)+ expect(repeat.body.data.result.notAsked).toBeUndefined()+ expect((await db.superTabMessage.findFirst({ where: { tabId: s.tabId, conversationId: s.web }, orderBy: { sequence: 'desc' } }))!.text)+ .toBe(`Needs a human\n\nQuestion 1?\n\nAsked ${peer.email}.`)+ expect(await db.superTabDecision.count({ where: { tabId: s.tabId, askerAgentId: s.worker } })).toBe(20)+ })++ it('removes answered decisions older than the leaderboard\'s 5 weeks when it writes new ones, keeping newer and open ones', async () => {+ const s = await scene()+ clock = Date.now()+ const now = Date.now(), weeks = (n: number) => new Date(now - n * 7 * DAY)+ const row = (id: string, createdAt: Date, answeredAt?: Date) => ({ id, tabId: s.tabId, batchId: 'old', projectId: 'lead', conversationId: s.web,+ assignmentId: null, personId: peer.sub, askerAgentId: s.lead, question: `${id}?`, options: ['Yes', 'No'], recommended: 0, unblocks: 'The release',+ createdAt, ...answeredAt ? { choice: 0, answeredAt } : {} })+ await db.superTabDecision.createMany({ data: [row('expired', weeks(8), new Date(weeks(5).getTime() - DAY)),+ row('recent', weeks(8), new Date(weeks(5).getTime() + DAY)), row('waiting', weeks(8))] })+ expect((await post(s.tabId, { type: 'decision.ask', conversationId: s.web, personId: peer.sub, questions: [question('Ship how?')] }, owner, s.lead)).status).toBe(200)+ const left = await db.superTabDecision.findMany({ where: { tabId: s.tabId }, select: { id: true } })+ expect(left.map(d => d.id)).not.toContain('expired')+ expect(left.map(d => d.id)).toEqual(expect.arrayContaining(['recent', 'waiting']))+ })+it('keeps a decision of its own for another asker, another task, or other options with the same question text', async () => {const s = await scene()clock = Date.now()diff --git a/backend/src/superTabs/commands.ts b/backend/src/superTabs/commands.tsindex f81992d49..c350dd077 100644--- a/backend/src/superTabs/commands.ts+++ b/backend/src/superTabs/commands.ts@@ -11,7 +11,7 @@ import { routingOverrideSchema } from './routingPolicy.js'import { agentTarget, askedInSchema, broken, MAX_RULES, RULE_MESSAGE_MS, ruleSchema, ruleTextSchema, startTarget, unmet, type RunTarget } from './rules.js'import { MAX_ATTACHMENTS, type Attachment } from './attachments.js'import { checkMentions, MAX_MENTIONS, type MentionTarget } from './mentions.js'-import { ANSWERED_NOTICE, answerDecision, askDecisions, checkAsker, decisionChoices, decisionQuestion, MAX_QUESTIONS, type DecisionTarget } from './decisions.js'+import { ANSWERED_NOTICE, answerDecision, askDecisions, checkAsker, decisionChoices, decisionQuestion, MAX_OPEN_DECISIONS, MAX_QUESTIONS, type DecisionTarget } from './decisions.js'import { TERMINAL_KEYS, terminalAlert, terminalRequest, terminalResult, toLead, type TerminalRequest } from './terminal.js'import { loseTasks, renewLease, renewOwnTask } from './liveness.js'import { ABANDONED, failureNotice, MACHINE_REMOVED } from './startFailure.js'@@ -268,6 +268,9 @@ export interface CommandContext {mention?: MentionTarget/** The decision `decision.answer` names, read from SuperTabDecision by the store. */decision?: DecisionTarget+ /** 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/** 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[]@@ -1737,8 +1740,11 @@ export function applyCommand(original: SuperTabState, actor: Actor, input: Commafail('INVALID_DECISION', 'An agent asks a person for a decision only when its work needs a human, with the question as its result.', 400)}const { personId, ...choices } = command.ask- effects.decisions = askDecisions(state, group, personId, [{ question: command.result, ...choices }],+ const 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+ 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.status@@ -1756,7 +1762,7 @@ export function applyCommand(original: SuperTabState, actor: Actor, input: Comma// Ready for review also wakes the group's Reviewers, whoever marked it.const reviewers = command.status !== 'review' ? [] : group.agentIds.filter(id => id !== lead &&(actor.kind !== 'agent' || id !== actor.agentId) && state.agents.some(a => a.id === id && agentRole(a) === 'reviewer' && a.workDecision !== 'stop'))- const asked = command.ask ? `\n\nAsked ${person(state, command.ask.personId).email}.` : ''+ const asked = !command.ask ? '' : effects.result.notAsked ? `\n\n${effects.result.notAsked}` : `\n\nAsked ${person(state, command.ask.personId).email}.`message(group.id, `${label}${command.result ? `\n\n${command.result}` : ''}${asked}`, [...(tellLead ? [lead] : []), ...reviewers], null, SYSTEM_ROW)// Shipped before this command: the leaderboard marks this Done as shipped just above (recordStatus).if (command.status === 'done' && shippedAt(conversation(original, group.id)) == null) {diff --git a/backend/src/superTabs/decisions.ts b/backend/src/superTabs/decisions.tsindex 531de519b..db2b0a424 100644--- a/backend/src/superTabs/decisions.ts+++ b/backend/src/superTabs/decisions.ts@@ -8,9 +8,14 @@ import type { Prisma } from '@prisma/client'import { z } from 'zod'import { AGENT_ONLY, fail, isLeads, person, stableId, type Actor, type Conversation, type SuperTabState } from './model.js'import { canReadGroup, groupProject, ledProject, projectOf } from './projects.js'+import { LEDGER_WEEKS } from './leaderboard/record.js'+import { WEEK_MS } from './leaderboard/week.js'type Tx = Prisma.TransactionClientexport const MAX_QUESTIONS = 10+/** Open decisions one asker may hold for one person: an asker whose question changes a little every turn would otherwise+ * write without end, and push real decisions out of the person's list. */+export const MAX_OPEN_DECISIONS = 20const line = (max: number) => z.string().trim().min(1).max(max)/** What a question offers besides itself: 2–5 distinct options, the index of the recommended one, and what an answer unblocks. */@@ -88,18 +93,39 @@ const repeatKey = (d: Pick<DecisionDraft, 'askerAgentId' | 'assignmentId' | 'perJSON.stringify([d.askerAgentId, d.assignmentId ?? null, d.personId, d.conversationId, d.question, d.options, d.recommended, d.unblocks])/** Writes the questions asked and returns the decision each one is: an open decision that this ask exactly repeats is that- * decision again, not a second one. Read and written in the command's transaction; two identical asks committing at the- * same moment can still both be written. */+ * decision again, not a second one. New ones beyond MAX_OPEN_DECISIONS from this asker to this person are refused, and+ * writing them removes the tab's decisions answered before the leaderboard's weeks. Read and written in the command's+ * transaction; asks committing at the same moment each pass the check, so together they can still be written twice or+ * go past the cap by what they add. */export async function writeDecisions(tx: Tx, tabId: string, drafts: DecisionDraft[], now: number): Promise<string[]> {if (!drafts.length) return []- const open = await tx.superTabDecision.findMany({ where: { tabId, personId: drafts[0]!.personId, conversationId: drafts[0]!.conversationId,- askerAgentId: drafts[0]!.askerAgentId, question: { in: drafts.map(d => d.question) }, ...waiting } })+ const { personId, askerAgentId } = drafts[0]!+ const open = await tx.superTabDecision.findMany({ where: { tabId, personId, conversationId: drafts[0]!.conversationId,+ askerAgentId, question: { in: drafts.map(d => d.question) }, ...waiting } })const held = new Map(open.map(o => [repeatKey(o), o.id]))const fresh = drafts.filter(d => !held.has(repeatKey(d)))- if (fresh.length) await tx.superTabDecision.createMany({ data: fresh.map(d => ({ ...d, tabId, createdAt: new Date(now) })) })+ if (fresh.length) {+ const asked = await tx.superTabDecision.count({ where: { tabId, personId, askerAgentId, ...waiting } })+ if (asked + fresh.length > MAX_OPEN_DECISIONS) {+ fail('DECISIONS_FULL', `${asked} decisions you asked this person are still open, and at most ${MAX_OPEN_DECISIONS} can be. Wait for answers before asking more.`)+ }+ await tx.superTabDecision.createMany({ data: fresh.map(d => ({ ...d, tabId, createdAt: new Date(now) })) })+ await tx.superTabDecision.deleteMany({ where: { tabId, answeredAt: { lt: new Date(now - LEDGER_WEEKS * WEEK_MS) } } })+ }return drafts.map(d => held.get(repeatKey(d)) ?? d.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+}+export async function readAskedDecision(tx: Tx, tabId: string, id: string): Promise<DecisionTarget | undefined> {const row = await tx.superTabDecision.findFirst({ where: { id, tabId },select: { id: true, personId: true, conversationId: true, askerAgentId: true, question: true, options: true, answeredAt: true } })diff --git a/backend/src/superTabs/store.ts b/backend/src/superTabs/store.tsindex 4d1cb2314..f9e059af4 100644--- a/backend/src/superTabs/store.ts+++ b/backend/src/superTabs/store.ts@@ -37,7 +37,7 @@ import { accrueLent } from './leaderboard/lent.js'import { mayDrive, TERMINAL_ANSWER_MS, TERMINAL_EXPIRY_MS, TERMINAL_PICKUP_MS, type TerminalEffect } from './terminal.js'import { closeAssignment, closeUnassignedStart, readDecision, timedDecisionOf, writeRouteEffects } from './routeDecisions.js'import { listMentions, markDone, mentionsWaiting, readMention, resolveByReply, writeMentions } from './mentions.js'-import { answerable, listDecisions, readAskedDecision, recordAnswer, waitingIn, waitingInProject, writeDecisions } from './decisions.js'+import { answerable, fullFor, listDecisions, readAskedDecision, recordAnswer, 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'@@ -747,6 +747,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 })+ }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)if ((command.type === 'member.avatar' || command.type === 'agent.avatar') && command.avatar?.kind === 'upload') {