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
A task counts verdicts at its own pull request, and the PM can unlink a wrong one (#3953) (#3435)
harness-for-autonomous[bot]e0e75b3fe83a
diff --git a/backend/integration/superTabPullRequestEvents.test.ts b/backend/integration/superTabPullRequestEvents.test.tsindex 3d4892374..60bc9ca7c 100644--- a/backend/integration/superTabPullRequestEvents.test.ts+++ b/backend/integration/superTabPullRequestEvents.test.ts@@ -242,6 +242,21 @@ describe('pull request events', () => {return { f, HEAD2, HEAD3, step, read }}+ // Task #3953 (Hugo's review of #3435): closing a reviewed pull request and opening its replacement leaves no Yes behind.+ it('stops counting at a pull request closed unmerged once another is linked and open, on the task and its steps, and counts again when it reopens', async () => {+ const { f, read } = await reviewed()+ const next = `https://github.com/acme/app/pull/${9700 + f.number}`, HEAD4 = 'dddddd4444444dddddd4444444dddddd4444444d'+ f.pulls.set(next, { ...f.pull({ head: HEAD4 }), url: next, number: 9700 + f.number })+ await f.execute({ type: 'work.status', conversationId: f.task, assignmentId: f.assignment, status: 'working', result: `${f.PR} then ${next}` }, host, f.worker)+ await f.pass()+ f.pulls.set(f.PR, f.pull({ state: 'closed' }))+ await f.pass()+ for (const check of await read()) expect(check.passedBy).toBeNull()+ f.pulls.set(f.PR, f.pull())+ await f.pass()+ for (const check of await read()) expect(check.passedBy).not.toBeNull()+ })+it('counts at its head, and its patch is read once a head', async () => {const { f, read } = await reviewed()for (const check of await read()) {@@ -580,3 +595,34 @@ describe('a pull request\'s check state', () => {})})++// Task #3953: a pull request GitHub shows closed no longer captures the task's checks while another link is open.+describe('a closed pull request is marked on the task', () => {+ it('marks a linked pull request the watch read open and GitHub now shows closed, and clears the mark when it opens again', async () => {+ const f = await swarm()+ const duplicate = `https://github.com/acme/app/pull/${9500 + f.number}`, other = 'd'.repeat(40)+ f.pulls.set(f.PR, f.pull())+ f.pulls.set(duplicate, { ...f.pull({ head: other }), url: duplicate, number: 9500 + f.number })+ await f.execute({ type: 'work.status', conversationId: f.task, assignmentId: f.assignment, status: 'working', result: `${f.PR} and ${duplicate}` }, host, f.worker)+ const heads = async () => ((await db.swarmGroup.findFirstOrThrow({ where: { tabId: f.tab, key: f.task } })).data as { pullRequestHeads: Record<string, { closed?: boolean }> }).pullRequestHeads+ await f.pass()+ expect(Object.keys(await heads()).sort()).toEqual([f.PR, duplicate].sort())+ f.pulls.set(duplicate, { ...f.pulls.get(duplicate)!, state: 'closed' })+ await f.pass()+ expect((await heads())[duplicate]).toMatchObject({ closed: true })+ expect((await heads())[f.PR]!.closed).toBeUndefined()+ f.pulls.set(duplicate, { ...f.pulls.get(duplicate)!, state: 'open' })+ await f.pass()+ expect((await heads())[duplicate]!.closed).toBeUndefined()+ })++ it('never marks a pull request GitHub shows merged', async () => {+ const f = await swarm()+ f.pulls.set(f.PR, f.pull())+ await f.pass()+ f.pulls.set(f.PR, f.pull({ state: 'closed', merged: true }))+ await f.pass()+ const heads = ((await db.swarmGroup.findFirstOrThrow({ where: { tabId: f.tab, key: f.task } })).data as { pullRequestHeads: Record<string, { closed?: boolean }> }).pullRequestHeads+ expect(heads[f.PR]!.closed).toBeUndefined()+ })+})diff --git a/backend/src/github/app.ts b/backend/src/github/app.tsindex 792db6bdf..5346cd8e2 100644--- a/backend/src/github/app.ts+++ b/backend/src/github/app.ts@@ -670,7 +670,7 @@ export class GitHubApp {const list = (value: unknown) => Array.isArray(value) ? value as Array<Record<string, unknown>> : []return {url: text(pull.html_url) || `https://github.com/${repository.owner}/${repository.name}/pull/${number}`, number,- state: pull.state, draft: pull.draft === true, head, base: text((pull.base as { ref?: unknown } | undefined)?.ref) || 'main',+ state: pull.state, ...typeof pull.merged_at === 'string' ? { merged: true as const } : {}, draft: pull.draft === true, head, base: text((pull.base as { ref?: unknown } | undefined)?.ref) || 'main',mergeable: typeof pull.mergeable === 'boolean' ? pull.mergeable : null,checkRuns: list((runs as Record<string, unknown>).check_runs).map(run => ({ name: text(run.name), status: text(run.status),conclusion: typeof run.conclusion === 'string' ? run.conclusion : null, completedAt: at(run.completed_at) })),diff --git a/backend/src/superTabs/cards.ts b/backend/src/superTabs/cards.tsindex 9b30ff9cf..c7879a8b3 100644--- a/backend/src/superTabs/cards.ts+++ b/backend/src/superTabs/cards.ts@@ -34,7 +34,7 @@ export function bindTaskCard(group: Conversation, brief: MessageDraft): void {export function bindPullRequestCards(group: Conversation, message: MessageDraft, repositoryUrl?: string | null): void {if (message.thread || agentOnly(message.source, message.recipients) || !cardable(message)) returnconst known = new Set(group.cards?.pullRequests?.map(card => card.url))- const fresh = pullRequestLinks(message.text, repositoryUrl).filter(url => !known.has(url))+ const fresh = pullRequestLinks(message.text, repositoryUrl).filter(url => !known.has(url) && !group.unlinkedPullRequests?.some(entry => entry.url === url))if (fresh.length) group.cards = { ...group.cards, pullRequests: [...group.cards?.pullRequests ?? [], ...fresh.map(url => ({ url, messageId: message.id }))] }}diff --git a/backend/src/superTabs/checkVerdicts.test.ts b/backend/src/superTabs/checkVerdicts.test.tsindex 00addbdb1..5355733f8 100644--- a/backend/src/superTabs/checkVerdicts.test.ts+++ b/backend/src/superTabs/checkVerdicts.test.ts@@ -3,7 +3,7 @@* request's own patch is the same (a plain rebase, or a main merge that leaves the PR's diff unchanged).*/import { describe, expect, it } from 'vitest'-import { HEADS_KEPT, deriveCheck, keepHead, recordCheckVerdict, refreshChecks, verdictCounts, watchedLinks, type PullRequestHeads } from './checkVerdicts.js'+import { HEADS_KEPT, checkedPullRequest, deriveCheck, keepHead, recordCheckVerdict, refreshChecks, verdictCounts, watchedLinks, type PullRequestHeads } from './checkVerdicts.js'import type { Check, CheckVerdict } from './model.js'const PR = 'https://github.com/acme/app/pull/7'@@ -227,3 +227,56 @@ describe('a verdict at a head a clean main merge carries', () => {expect(verdictCounts(read(A.slice(0, 7)), on(B, [A]))).toBe(false)})})++// Task #3953: the pull request a task's checks count at is its own, not simply its newest link.+describe('checkedPullRequest', () => {+ const OWN = 'https://github.com/acme/app/pull/20', OTHER = 'https://github.com/acme/app/pull/19'+ const read = (extra: Partial<PullRequestHeads[string]> = {}) => ({ head: A, patch: 'p', at: 1, base: 'main', ...extra })+ const task = (over: Partial<Parameters<typeof checkedPullRequest>[0]> = {}) => ({ pullRequestUrls: [OWN, OTHER], pullRequestHeads: { [OWN]: read(), [OTHER]: read({ head: B }) }, ...over })+ const repository = 'https://github.com/acme/app'+ const said = (result: string) => ({ assignment: { result } as never })++ it('prefers the pull request its Engineer\'s latest status names over a newer link', () => {+ expect(checkedPullRequest(task(said(`Ready: ${OWN} at aaaaaaa`)), repository)).toBe(OWN)+ })+ it('then the code list, then the newest open link', () => {+ expect(checkedPullRequest(task({ codePullRequestUrls: [OWN] }), repository)).toBe(OWN)+ expect(checkedPullRequest(task(), repository)).toBe(OTHER)+ })+ it('never picks a closed pull request while another is open, even when its Engineer names it', () => {+ const heads = { [OWN]: read(), [OTHER]: read({ closed: true as const }) }+ expect(checkedPullRequest(task({ pullRequestHeads: heads, ...said(`Ready: ${OTHER}`) }), repository)).toBe(OWN)+ expect(checkedPullRequest(task({ pullRequestHeads: heads }), repository)).toBe(OWN)+ })+ it('keeps the newest link when every link is closed or none is read', () => {+ const closed = { [OWN]: read({ closed: true as const }), [OTHER]: read({ closed: true as const }) }+ expect(checkedPullRequest(task({ pullRequestHeads: closed }), repository)).toBe(OTHER)+ expect(checkedPullRequest(task({ pullRequestHeads: undefined }), repository)).toBe(OTHER)+ expect(checkedPullRequest({}, repository)).toBeUndefined()+ })+})++// Task #3953 (Hugo's review of #3435): a Yes recorded at a pull request later closed unmerged stops counting once another+// linked pull request is open; a merged one is never marked closed, and a task with nothing else open keeps its Yes.+describe('a verdict at a pull request closed unmerged', () => {+ const NEXT = 'https://github.com/acme/app/pull/8'+ const heads = (closed: boolean, next = true): PullRequestHeads => ({ [PR]: { head: A, patch: 'p1', at: 1, ...closed ? { closed: true as const } : {} },+ ...next ? { [NEXT]: { head: B, patch: 'p2', at: 2 } } : {} })++ it('stops counting while another linked pull request is open, and the check is no longer passed', () => {+ const c = check()+ recordCheckVerdict(c, verdict('r', true, A, 'p1'), undefined, heads(false))+ expect(c.passedBy).toBe('r')+ deriveCheck(c, heads(true))+ expect(c.passedBy).toBeNull()+ expect(verdictCounts(verdict('r', true, A, 'p1'), heads(true))).toBe(false)+ })+ it('counts again once the pull request is open again', () => {+ expect(verdictCounts(verdict('r', true, A, 'p1'), heads(false))).toBe(true)+ })+ it('keeps counting when no other pull request is open', () => {+ expect(verdictCounts(verdict('r', true, A, 'p1'), heads(true, false))).toBe(true)+ const bothClosed: PullRequestHeads = { [PR]: { head: A, patch: 'p1', at: 1, closed: true }, [NEXT]: { head: B, patch: 'p2', at: 2, closed: true } }+ expect(verdictCounts(verdict('r', true, A, 'p1'), bothClosed)).toBe(true)+ })+})diff --git a/backend/src/superTabs/checkVerdicts.ts b/backend/src/superTabs/checkVerdicts.tsindex a64ff7700..e3052bfb7 100644--- a/backend/src/superTabs/checkVerdicts.ts+++ b/backend/src/superTabs/checkVerdicts.ts@@ -7,7 +7,7 @@* request's own patch is the same (a plain rebase, or a main merge that leaves its diff unchanged). It replaces reading a* verdict's text: a stale pass passes nothing, and a stale failure fails nothing, until someone checks the new head.*/-import type { Check, CheckVerdict, PullRequestHead } from './model.js'+import { codePullRequests, pullRequestLinks, type Check, type CheckVerdict, type Conversation, type PullRequestHead } from './model.js'/** Each linked pull request's head as the PR watch last read it (Conversation.pullRequestHeads). */export type PullRequestHeads = Readonly<Record<string, PullRequestHead>>@@ -39,6 +39,9 @@ export function verdictCounts(verdict: CheckVerdict, heads: PullRequestHeads | uconst current = currentHead(verdict, heads)if (current === undefined) return trueif (!current || !verdict.head) return false+ // A pull request closed unmerged (task #3953) counts nothing while another linked one is open: closing a reviewed pull request+ // and opening its replacement leaves no Yes behind.+ if (current.closed && heads && Object.values(heads).some(other => other !== current && !other.closed)) return false// Its head counts only on the base it names (security review of #3218, round 4; review round 7): naming none, on none once a// base is watched. A patch id names its base. A head that is git's own merge of its head and main carries it, on that same// base only (mergeCarry.ts, task #3553).@@ -46,6 +49,22 @@ export function verdictCounts(verdict: CheckVerdict, heads: PullRequestHeads | ureturn (read && verdict.base === current.base) || (!!verdict.patch && current.patch === verdict.patch)}+/**+ * The pull request [task]'s checks and verdicts count at (task #3953): not simply its newest link, which a wrong link (another+ * task's pull request, a closed duplicate) captured, so its reviewers' Yes on the real one counted nowhere. In order, the+ * newest linked, not closed one that its Engineer's latest status names (the Ready line), else the newest in its code list+ * (codePullRequests), else the newest not closed; each preferring one the watch has read. Only when every link is closed, the+ * newest link, as before.+ */+export function checkedPullRequest(task: Partial<Pick<Conversation, 'pullRequestUrls' | 'pullRequestHeads' | 'codePullRequestUrls' | 'assignment'>>,+ repositoryUrl: string | null): string | undefined {+ const urls = task.pullRequestUrls ?? [], heads = task.pullRequestHeads+ const open = urls.filter(url => !heads?.[url]?.closed)+ const newest = (list: readonly string[]) => list.findLast(url => heads?.[url]) ?? list.at(-1)+ const named = pullRequestLinks(task.assignment?.result ?? '', repositoryUrl), code = codePullRequests(task)+ return newest(open.filter(url => named.includes(url))) ?? newest(open.filter(url => code.includes(url))) ?? newest(open) ?? newest(urls)+}+/** The links of a task its PR watch reads: its newest HEADS_KEPT (review of #3218: one task links 369 pull requests in* production), the same each pass until it links another, so the heads kept are exactly the heads read (keepHead). */export const watchedLinks = (links: readonly string[]): string[] => links.slice(-HEADS_KEPT)diff --git a/backend/src/superTabs/commands.ts b/backend/src/superTabs/commands.tsindex 1e4189fe1..623714fd7 100644--- a/backend/src/superTabs/commands.ts+++ b/backend/src/superTabs/commands.ts@@ -19,7 +19,7 @@ import { agentWrittenNameOf, setName, shownNameOf } from './personNames.js'import { z } from 'zod'import { tokensByModelSchema } from './costs/tokens.js'import {- activeWork, occupiesAgent, actorKey, agent, appendCodePullRequestUrls, appendPullRequestUrls, author, projectPullRequestUrl, pullRequestLinks, conversation, crossProjectActive, fail, isLeads, person, stableId, numberConversations, nextConversationNumber, renderWorkTask,+ activeWork, occupiesAgent, actorKey, agent, appendCodePullRequestUrls, appendPullRequestUrls, author, projectPullRequestUrl, UNLINKS_KEPT, pullRequestLinks, conversation, crossProjectActive, fail, isLeads, person, stableId, numberConversations, nextConversationNumber, renderWorkTask,finishWorker, seenMachineIds, SuperTabError, swarmGridName, visibleState, AGENT_ONLY, STATUS_LABELS, forRecipients,type Actor, type AgentConfiguration, type AgentStart, type AgentStartRequest, type BuddySessionEntry, type WorkTask, type Agent, type Author, type Check, type CommandEffects, type Conversation, type SuperTabState,} from './model.js'@@ -29,7 +29,7 @@ import { closeMerged } from './mergedTasks.js'import { noteWriter } from './stuck.js'import { reviewPlan, stepBrief, stepCheck, refreshStepBrief, type PlannedStep, type StepKind } from './reviewTable.js'import { builtBy, reviewerCandidates, STRENGTH_RULE } from './reviewerPick.js'-import { recordCheckVerdict } from './checkVerdicts.js'+import { checkedPullRequest, deriveCheck, recordCheckVerdict } from './checkVerdicts.js'import { routeRecordSchema, routeTitle, startedAs, type KeptCandidate, type RouteDecision, type RouteFinal, type RouteOverride } from './routeRecord.js'import { routingOverrideSaveSchema } from './routingPolicy.js'import { askedInSchema, RULE_MESSAGE_MS, ruleSchema, ruleTextSchema } from './rules.js'@@ -346,6 +346,10 @@ export const commandSchema = z.discriminatedUnion('type', [z.object({ type: z.literal('work.lane'), conversationId: id, lane: z.string().trim().min(1).max(80).nullable() }).strict(),// The Lead's record that a request's pull request merged (leaderboard/record.ts recordMerged); `mergedAt` for a backfill.// `fixes`: the merged PR this one fixes, for its model's fixes after merge (leaderboard/record.ts recordFix).+ // A wrong link taken off a task, with its reason kept in the task's history (task #3953): by the project's coordinator or the+ // task's Engineer, never a pull request whose merge the task recorded.+ z.object({ type: z.literal('conversation.unlink'), conversationId: id, pullRequestUrl: z.string().max(400).regex(PULL_REQUEST_URL),+ reason: z.string().trim().min(1).max(300) }).strict(),z.object({ type: z.literal('conversation.merged'), conversationId: id, pullRequestUrl: z.string().max(400).regex(PULL_REQUEST_URL),mergedAt: z.iso.datetime().optional(), fixes: z.string().max(400).regex(PULL_REQUEST_URL).optional() }).strict(),// `launchRefused`: the model and effort a Product Manager's own-machine launch asked for, which that machine refused before@@ -538,6 +542,7 @@ const WORDS: Partial<Record<Command['type'], readonly string[]>> = {'work.assign': ['overrideReason'],'work.status': ['result'],'work.cancel': ['reason'],+ 'conversation.unlink': ['reason'],'work.check': ['evidence'],'project.create': ['name', 'brief'],'project.rename': ['name'],@@ -2983,6 +2988,34 @@ function applyCommandOnce(original: SuperTabState, actor: Actor, input: Command,}break}+ case 'conversation.unlink': {+ chatOnly(command.conversationId)+ const group = open(state, command.conversationId), project = groupProject(state, group)+ if (isStep(group) || group.id === project.leadConversationId) fail('UNLINK_TASK_ONLY', 'Only a task has links to take off: name a task, not a chat or a step.', 400)+ // Only the project's coordinator and the task's Engineer; a person is neither (the PM acts for them).+ if (actor.kind !== 'agent' || (ledProject(state, actor.agentId)?.id !== project.id && group.assignment?.agentId !== actor.agentId)) {+ fail('FORBIDDEN', 'Only the project\'s coordinator or the task\'s Engineer takes a pull request off a task.', 403)+ }+ const repositoryUrl = groupRepositoryUrl(state, group)+ const pullRequestUrl = projectPullRequestUrl(command.pullRequestUrl, repositoryUrl)+ ?? fail('PULL_REQUEST_REPOSITORY', 'This pull request is not in the project\'s repository.', 400)+ if (!group.pullRequestUrls?.includes(pullRequestUrl)) fail('NOT_LINKED', 'This task does not link that pull request.', 404)+ if (pullRequestCard(group, pullRequestUrl)?.mergedAt !== undefined) fail('MERGED_LINK', 'This task recorded that pull request as merged: its merge stays on the task.', 409)+ const without = <T>(record: Record<string, T> | undefined) => record && Object.fromEntries(Object.entries(record).filter(([url]) => url !== pullRequestUrl))+ group.pullRequestUrls = group.pullRequestUrls.filter(url => url !== pullRequestUrl)+ if (group.codePullRequestUrls) group.codePullRequestUrls = group.codePullRequestUrls.filter(url => url !== pullRequestUrl)+ if (group.pullRequestHeads) group.pullRequestHeads = without(group.pullRequestHeads)+ if (group.pullRequestEvents) group.pullRequestEvents = without(group.pullRequestEvents)+ if (group.cards) group.cards = { ...group.cards, pullRequests: group.cards.pullRequests?.filter(card => card.url !== pullRequestUrl),+ recorded: group.cards.recorded?.filter(url => url !== pullRequestUrl) }+ group.unlinkedPullRequests = [...(group.unlinkedPullRequests ?? []).filter(entry => entry.url !== pullRequestUrl),+ { url: pullRequestUrl, by: actorKey(actor), reason: command.reason, at: context.now }].slice(-UNLINKS_KEPT)+ // A verdict on it names a pull request the task no longer links: it counts nowhere.+ for (const checked of [group, ...stepsOf(state, group.id)]) for (const check of checked.checks) if (check.verdicts?.length) deriveCheck(check, group.pullRequestHeads)+ const by = agentDisplayName(state.agents, actor.agentId) ?? agent(state, actor.agentId).name+ message(group.id, `${by} unlinked ${pullRequestUrl} from this task: ${command.reason}`, [], null, SYSTEM_ROW)+ break+ }case 'conversation.merged': {chatOnly(command.conversationId)projectChatWork(command.conversationId)@@ -4730,8 +4763,8 @@ function applyCommandOnce(original: SuperTabState, actor: Actor, input: Command,// agent's verdict naming none is recorded and counts at no watched head (security review of #3218: a push during the// review would get its pass). Nothing is refused: the result says how the verdict counts and what makes it count.const build = group.reviewOf ? state.conversations.find(c => c.id === group.reviewOf) : group- const heads = build?.pullRequestHeads, urls = build?.pullRequestUrls ?? []- const pullRequest = urls.findLast(url => heads?.[url]) ?? urls.at(-1)+ const heads = build?.pullRequestHeads+ const pullRequest = build ? checkedPullRequest(build, groupRepositoryUrl(state, build)) : undefinedconst watched = pullRequest ? heads?.[pullRequest]?.head : undefinedconst head = command.head ?? (actor.kind === 'person' ? watched : undefined)// The base it read against, named as the head is (review of #3218, round 7): no inferred base is known to be the one read.diff --git a/backend/src/superTabs/model.ts b/backend/src/superTabs/model.tsindex 82b224308..d1b85d8c4 100644--- a/backend/src/superTabs/model.ts+++ b/backend/src/superTabs/model.ts@@ -292,7 +292,10 @@ export interface PullRequestHead { head: string; patch: string | null; at: numbebase?: string/** The heads whose verdicts this one carries, newest first, at most CARRIES_KEPT: it is git's own merge of the first and* main, the first of the next, and so on (mergeCarry.ts, task #3553). Absent on any other head. */- carries?: string[] }+ carries?: string[]+ /** Set by the PR watch once GitHub shows the pull request closed (merged or not): a task's checks never count at it while+ * another linked pull request is open (checkVerdicts.ts checkedPullRequest, task #3953). Cleared when it is open again. */+ closed?: true }export interface Assignment {id: stringagentId: string@@ -438,6 +441,9 @@ export interface Conversation {* #3554): those its brief, title, assignment's result, checks' evidence and merge record name, never links in posts. [] from* its creation; absent on a conversation from before, which reads what it recorded instead (codePullRequests). */codePullRequestUrls?: string[]+ /** Pull requests the PM or the task's Engineer unlinked from it, newest last, at most UNLINKS_KEPT: the task's history of+ * them, and what keeps a later post's link from linking one again (commands.ts conversation.unlink, task #3953). */+ unlinkedPullRequests?: Array<{ url: string; by: string; reason: string; at: number }>/** 'server' on a step the server opened for a handed task (commands.ts openReviewSteps, task #3602): an agent's step of its* kind is then refused as a double. Absent on a step an agent or a person opened, and on older ones. */openedBy?: 'server'@@ -591,14 +597,19 @@ export const codePullRequests = (group: Pick<Conversation, 'codePullRequestUrls'export function appendCodePullRequestUrls(group: Conversation, texts: Iterable<string | null | undefined>, repositoryUrl?: string | null): void {if (group.codePullRequestUrls === undefined) returnconst urls = new Set(group.codePullRequestUrls.flatMap(url => projectPullRequestUrl(url, repositoryUrl) ?? []))- for (const text of texts) if (text) for (const url of pullRequestLinks(text, repositoryUrl)) urls.add(url)+ for (const text of texts) if (text) for (const url of pullRequestLinks(text, repositoryUrl)) if (!unlinked(group, url)) urls.add(url)group.codePullRequestUrls = [...urls]}+/** The most unlinked pull requests a task remembers. */+export const UNLINKS_KEPT = 50+/** Whether [url] was unlinked from [group]: no later text links it again. */+const unlinked = (group: Pick<Conversation, 'unlinkedPullRequests'>, url: string): boolean => !!group.unlinkedPullRequests?.some(entry => entry.url === url)+/** Adds the pull request links of [texts] to [group] and corrects the ones it holds, by the project's [repositoryUrl]. */export function appendPullRequestUrls(group: Conversation, texts: Iterable<string | null | undefined>, repositoryUrl?: string | null): void {const urls = new Set((group.pullRequestUrls ?? []).flatMap(url => projectPullRequestUrl(url, repositoryUrl) ?? []))- for (const text of texts) if (text) for (const url of pullRequestLinks(text, repositoryUrl)) urls.add(url)+ for (const text of texts) if (text) for (const url of pullRequestLinks(text, repositoryUrl)) if (!unlinked(group, url)) urls.add(url)group.pullRequestUrls = [...urls]}export interface Invitation {diff --git a/backend/src/superTabs/ownPullRequest.test.ts b/backend/src/superTabs/ownPullRequest.test.tsnew file mode 100644index 000000000..5399184ad--- /dev/null+++ b/backend/src/superTabs/ownPullRequest.test.ts@@ -0,0 +1,104 @@+// Task #3953: a task's checks count at the pull request it is about, not at its newest link, and the PM or the task's+// Engineer can take a wrong link off it. #3820 had #3819's and #3817's pull requests linked, and #3872 a closed duplicate.+import { describe, expect, it } from 'vitest'+import type { Command } from './commands.js'+import { appendPullRequestUrls } from './model.js'+import { owner, stepsFixture } from './steps.test-support.js'++const OWN = 'https://github.com/acme/app/pull/20', OTHER = 'https://github.com/acme/app/pull/19', DUPLICATE = 'https://github.com/acme/app/pull/9'+const OWN_HEAD = 'a'.repeat(40), OTHER_HEAD = 'b'.repeat(40), DUPLICATE_HEAD = 'c'.repeat(40)+const head = (sha: string, over: { closed?: true } = {}) => ({ head: sha, base: 'main', patch: `patch-${sha[0]}`, at: 1, ...over })++function linked(urls: string[], heads: Record<string, ReturnType<typeof head>>, result: string, code?: string[]) {+ const f = stepsFixture()+ const assignment = f.group(f.task).assignment!.id+ f.run({ type: 'work.status', conversationId: f.task, assignmentId: assignment, status: 'review', result } as Command, f.as(f.dev))+ const reviewer = f.add('rev', f.task)+ // After every command: each one replaces the state, so the group is read again.+ const group = f.group(f.task)+ group.pullRequestUrls = urls+ group.pullRequestHeads = heads+ if (code) group.codePullRequestUrls = code+ const check = (actor = f.as(f.dev), sha = OWN_HEAD) => f.run({ type: 'work.check', conversationId: f.task, assignmentId: assignment, checkId: f.group(f.task).checks[0]!.id,+ passed: true, evidence: 'Read the diff', head: sha, base: 'main' } as Command, actor)+ return Object.assign(f, { assignment, check, reviewer })+}++describe('a task\'s checks count at its own pull request (#3953)', () => {+ it('records a verdict at the pull request its Engineer\'s Ready line names, though another task\'s is linked after it', () => {+ const f = linked([OWN, OTHER], { [OWN]: head(OWN_HEAD), [OTHER]: head(OTHER_HEAD) }, `Ready for review: ${OWN} at aaaaaaa`)+ const effects = f.check(f.as(f.reviewer))+ expect(f.group(f.task).checks[0]!.verdicts!.at(-1)).toMatchObject({ pullRequest: OWN, head: OWN_HEAD })+ expect(effects.result.counts).toBe(true)+ })++ it('does not let a closed duplicate capture the checks', () => {+ const f = linked([OWN, DUPLICATE], { [OWN]: head(OWN_HEAD), [DUPLICATE]: head(DUPLICATE_HEAD, { closed: true }) }, 'Ready for review.')+ const effects = f.check(f.as(f.reviewer))+ expect(f.group(f.task).checks[0]!.verdicts!.at(-1)).toMatchObject({ pullRequest: OWN })+ expect(effects.result.counts).toBe(true)+ })++ it('falls back to the pull request in the task\'s code list when its status names none', () => {+ const f = linked([OWN, OTHER], { [OWN]: head(OWN_HEAD), [OTHER]: head(OTHER_HEAD) }, 'Ready for review.', [OWN])+ f.check(f.as(f.reviewer))+ expect(f.group(f.task).checks[0]!.verdicts!.at(-1)).toMatchObject({ pullRequest: OWN })+ })++ it('still uses the newest linked pull request for a task that names none and has no code list', () => {+ const f = linked([OWN, OTHER], { [OWN]: head(OWN_HEAD), [OTHER]: head(OTHER_HEAD) }, 'Ready for review.')+ f.check(f.as(f.reviewer), OTHER_HEAD)+ expect(f.group(f.task).checks[0]!.verdicts!.at(-1)).toMatchObject({ pullRequest: OTHER })+ })+})++describe('conversation.unlink (#3953)', () => {+ const unlink = (f: ReturnType<typeof linked>, actor = f.as(f.lead), url = OTHER, reason = 'It is #3819\'s pull request.') =>+ f.run({ type: 'conversation.unlink', conversationId: f.task, pullRequestUrl: url, reason } as Command, actor)+ const fixture = () => linked([OWN, OTHER], { [OWN]: head(OWN_HEAD), [OTHER]: head(OTHER_HEAD) }, `Ready for review: ${OWN} at aaaaaaa`, [OWN, OTHER])++ it('takes the link, its head and its code entry off the task for the PM, and records who, why and when in its history', () => {+ const f = fixture()+ const effects = unlink(f)+ const group = f.group(f.task)+ expect(group.pullRequestUrls).toEqual([OWN])+ expect(group.codePullRequestUrls).toEqual([OWN])+ expect(Object.keys(group.pullRequestHeads!)).toEqual([OWN])+ expect(group.unlinkedPullRequests).toEqual([{ url: OTHER, by: `agent:${f.lead}`, reason: 'It is #3819\'s pull request.', at: f.now }])+ expect(effects.messages.some(m => m.conversationId === f.task && m.text.includes(`unlinked ${OTHER}`) && m.text.includes('#3819'))).toBe(true)+ })++ it('works for the task\'s Engineer too', () => {+ const f = fixture()+ unlink(f, f.as(f.dev))+ expect(f.group(f.task).pullRequestUrls).toEqual([OWN])+ })++ it('refuses another agent and a person, and changes nothing', () => {+ const f = fixture()+ expect(() => unlink(f, f.as(f.reviewer))).toThrowError(expect.objectContaining({ code: 'FORBIDDEN' }))+ expect(() => unlink(f, owner)).toThrowError(expect.objectContaining({ code: 'FORBIDDEN' }))+ expect(f.group(f.task).pullRequestUrls).toEqual([OWN, OTHER])+ expect(f.group(f.task).unlinkedPullRequests).toBeUndefined()+ })++ it('refuses a pull request whose merge the task recorded, and one it does not link', () => {+ const f = fixture()+ f.group(f.task).cards = { pullRequests: [{ url: OTHER, messageId: 'm1', mergedAt: 5 }] }+ expect(() => unlink(f)).toThrowError(expect.objectContaining({ code: 'MERGED_LINK' }))+ expect(() => unlink(f, f.as(f.lead), DUPLICATE)).toThrowError(expect.objectContaining({ code: 'NOT_LINKED' }))+ expect(f.group(f.task).pullRequestUrls).toEqual([OWN, OTHER])+ })++ it('leaves a verdict on the unlinked pull request counting nowhere, and keeps a later post from linking it again', () => {+ const f = fixture()+ f.check(f.as(f.reviewer), OTHER_HEAD)+ expect(f.group(f.task).checks[0]!.verdicts!.at(-1)).toMatchObject({ pullRequest: OWN })+ f.group(f.task).checks[0]!.verdicts![0]!.pullRequest = OTHER+ f.group(f.task).checks[0]!.verdicts![0]!.head = OTHER_HEAD+ unlink(f)+ expect(f.group(f.task).checks[0]!.verdicts![0]!.stale).toBeDefined()+ appendPullRequestUrls(f.group(f.task), [`See ${OTHER}`])+ expect(f.group(f.task).pullRequestUrls).toEqual([OWN])+ })+})diff --git a/backend/src/superTabs/parts/light.test.ts b/backend/src/superTabs/parts/light.test.tsindex d22faa85b..bc5db71be 100644--- a/backend/src/superTabs/parts/light.test.ts+++ b/backend/src/superTabs/parts/light.test.ts@@ -95,6 +95,7 @@ const conversation: Required<Conversation> = {teamWritten: true,teamWrittenBy: { kind: 'assigned' },title: 't',+ unlinkedPullRequests: [],writers: [],}diff --git a/backend/src/superTabs/parts/light.ts b/backend/src/superTabs/parts/light.tsindex a035c4257..42e140f53 100644--- a/backend/src/superTabs/parts/light.ts+++ b/backend/src/superTabs/parts/light.ts@@ -115,6 +115,7 @@ export const GROUP_FIELDS = {'talkedAt','teamWritten','teamWrittenBy',+ 'unlinkedPullRequests','writers',],} as constdiff --git a/backend/src/superTabs/parts/touches.ts b/backend/src/superTabs/parts/touches.tsindex b8693341e..1f4fd3bf0 100644--- a/backend/src/superTabs/parts/touches.ts+++ b/backend/src/superTabs/parts/touches.ts@@ -162,6 +162,7 @@ export function touchesFor(command: Command, actor: Actor, before: SuperTabStatebreak}case 'conversation.merged':+ case 'conversation.unlink':leadCheck(projectOfGroup(command.conversationId)?.id)break// Whether an agent may change a priority is whether it is still its project's PM (commands.ts coordinatorOrPerson).diff --git a/backend/src/superTabs/pullRequestEvents.ts b/backend/src/superTabs/pullRequestEvents.tsindex 066aba87d..91b7ebce2 100644--- a/backend/src/superTabs/pullRequestEvents.ts+++ b/backend/src/superTabs/pullRequestEvents.ts@@ -17,6 +17,8 @@ export interface PullRequestWatch {url: stringnumber: numberstate: 'open' | 'closed'+ /** True once GitHub says it merged: a closed pull request without it was closed unmerged (task #3953). */+ merged?: truedraft: boolean/** The head commit's SHA. */head: string@@ -154,6 +156,8 @@ export const PATCH_RETRY_MS = 15 * 60_000* A new base branch is read again like a new head (security review of #3218, round 3): the id names the branch. */const patchNeeded = (known: PullRequestHead | undefined, watch: PullRequestWatch, now: number) =>known?.head !== watch.head || known.base !== watch.base || (known.patch === null && now - known.at >= PATCH_RETRY_MS)+ // Open again after it was read closed: the task's steps are loaded to count their verdicts at it once more (task #3953).+ || known.closed === true/** Reads with the Harness GitHub App: one read-only token per repository for the whole pass, revoked when it is done (as the* archive pass's, archive.ts). A repository the app is not installed on, or any GitHub failure, reads as null. */@@ -206,7 +210,7 @@ export function appPullRequestWatch(app: GitHubApp): PullRequestReader {* pullRequestSeen). One group's failure is logged and skips that group alone. A pass that posted, or could not read a pull* request, says so in one line: the measure of this feature (AGENTS.md standards 6 and 8). The messages posted.*/-export async function watchPullRequests(db: PrismaClient, store: Pick<SuperTabStore, 'pullRequestSeen' | 'pullRequestHeadsWatched'>, reader: PullRequestReader, now: number,+export async function watchPullRequests(db: PrismaClient, store: Pick<SuperTabStore, 'pullRequestSeen' | 'pullRequestHeadsWatched' | 'pullRequestClosed'>, reader: PullRequestReader, now: number,limit = WATCH_GROUPS, doneLimit = WATCH_DONE_GROUPS): Promise<number> {try {// The finished mark's index, [workFinished, archived, lastActivityAt]: equality, equality, then the range, which also@@ -243,9 +247,12 @@ export async function watchPullRequests(db: PrismaClient, store: Pick<SuperTabStreturn merges.get(key)!}const seen: Array<{ row: typeof rows[number]; watch: PullRequestWatch; patch: string | null | undefined; merged: string | null | undefined }> = []+ const closed: Array<{ row: typeof rows[number]; url: string }> = []for (const row of rows) {for (const url of watchedLinks(row.urls)) {const watch = await watchOf(url)+ // A pull request the watch read before and GitHub now shows closed unmerged stops capturing the task's checks (task #3953).+ if (watch?.state === 'closed' && !watch.merged && row.heads?.[url] && !row.heads[url]!.closed) closed.push({ row, url })if (watch?.state !== 'open') continueconst known = row.heads?.[url] ?? undefined, newHead = known?.head !== watch.head || known.base !== watch.baseseen.push({ row, watch, patch: patchNeeded(known, watch, now) ? await patchOf(url, watch.head, watch.base) : undefined,@@ -259,7 +266,7 @@ export async function watchPullRequests(db: PrismaClient, store: Pick<SuperTabStreturn Object.keys(row.heads ?? {}).some(url => !window.has(url))})// A task from before steps were listed, at a new head or dropping one: its dormant steps are looked up once, then listed.- const unlisted = [...seen.filter(s => s.patch !== undefined).map(s => s.row), ...moved].filter(row => !Array.isArray(row.steps))+ const unlisted = [...seen.filter(s => s.patch !== undefined).map(s => s.row), ...moved, ...closed.map(c => c.row)].filter(row => !Array.isArray(row.steps))const listed = await listedSteps(db, unlisted)for (const row of moved) {try {@@ -269,6 +276,14 @@ export async function watchPullRequests(db: PrismaClient, store: Pick<SuperTabStlogger.warn('[swarm-pr-events] group skipped', { tabId: row.tabId, group: row.key, error: error instanceof Error ? error.message : String(error) })}}+ for (const { row, url } of closed) {+ try {+ const lookedUp = listed.get(`${row.tabId}:${row.key}`)+ await store.pullRequestClosed(row.tabId, row.key, url, row.steps ?? lookedUp ?? [], lookedUp !== undefined)+ } catch (error) {+ logger.warn('[swarm-pr-events] group skipped', { tabId: row.tabId, group: row.key, error: error instanceof Error ? error.message : String(error) })+ }+ }let posted = 0for (const { row, watch, patch, merged } of seen) {try {@@ -337,7 +352,7 @@ export async function listedSteps(db: PrismaClient, tasks: ReadonlyArray<{ tabId}/** Runs the pass every minute in the singleton worker, one at a time. */-export function startPullRequestWatch(db: PrismaClient, store: Pick<SuperTabStore, 'pullRequestSeen' | 'pullRequestHeadsWatched'>, reader: () => PullRequestReader, everyMs = 60_000): () => void {+export function startPullRequestWatch(db: PrismaClient, store: Pick<SuperTabStore, 'pullRequestSeen' | 'pullRequestHeadsWatched' | 'pullRequestClosed'>, reader: () => PullRequestReader, everyMs = 60_000): () => void {let running = falseconst run = () => {if (running) returndiff --git a/backend/src/superTabs/store.ts b/backend/src/superTabs/store.tsindex b64d045f0..53bf29cec 100644--- a/backend/src/superTabs/store.ts+++ b/backend/src/superTabs/store.ts@@ -96,7 +96,7 @@ import { commitParts, indexesOf, machineContentChanged, numberMessages, type Touimport { loadDeliveryParts, ms } from './parts/light.js'import { endSteps, ownerOf, stepEndedRows, stepsOf } from './steps.js'import { checksStateOf, pullRequestEvents, readPullRequestsNow, type PullRequestReader, type PullRequestWatch, type TaskHeads } from './pullRequestEvents.js'-import { deriveCheck, keepHead, refreshChecks, watchedLinks } from './checkVerdicts.js'+import { checkedPullRequest, deriveCheck, keepHead, refreshChecks, watchedLinks } from './checkVerdicts.js'import { reopenOnEvents } from './handed.js'import type { TrainTask } from './mergeTrain/plan.js'import type { TrainTaskPull } from './mergeTrain/train.js'@@ -2529,9 +2529,9 @@ export class SuperTabStore {await this.headsReadNow(user, tabId, command, (state) => {const group = state.conversations.find(c => c.id === command.conversationId)const task = group?.reviewOf ? state.conversations.find(c => c.id === group.reviewOf) : group- const urls = task?.pullRequestUrls ?? [], heads = task?.pullRequestHeads+ const heads = task?.pullRequestHeads// The pull request the command records the verdict on (commands.ts work.check).- const url = urls.findLast(u => heads?.[u]) ?? urls.at(-1)+ const url = task ? checkedPullRequest(task, groupProject(state, task).repository?.repositoryUrl ?? null) : undefinedconst known = url ? heads?.[url] : undefinedconst same = (sha: string) => sha.toLowerCase() === head.toLowerCase()if (!task || !url || (known && (same(known.head) || known.carries?.some(same)) && (base === undefined || base === known.base))) return null@@ -5422,6 +5422,15 @@ export class SuperTabStore {// A new base branch counts as a new head (security review of #3218, round 3): the patch id names the branch.const known = group.pullRequestHeads?.[watch.url], newHead = known?.head !== watch.head || known.base !== watch.baseconst repatched = !newHead && patch !== undefined && known!.patch === null+ // Open again after it was read closed: it may capture the task's checks once more (task #3953).+ const reopened = !newHead && !!known?.closed+ if (reopened) {+ const { closed: _closed, ...open } = known!+ group.pullRequestHeads = { ...group.pullRequestHeads, [watch.url]: open }+ for (const checked of [group, ...state.conversations.filter(c => c.reviewOf === group.id)]) {+ for (const check of checked.checks) if (check.verdicts?.length) deriveCheck(check, group.pullRequestHeads)+ }+ }if (newHead || repatched) {// A task from before steps were listed lists the ones its watch looked up, with its awake ones (#3394).if (lookedUp && !group.stepIds) {@@ -5445,13 +5454,13 @@ export class SuperTabStore {if (checksMoved) group.pullRequestChecks = Object.fromEntries(Object.entries({ ...group.pullRequestChecks, [watch.url]: checks }).filter(([url]) => group.pullRequestUrls!.includes(url)))if (done) {- if (!newHead && !repatched && !checksMoved) return 0+ if (!newHead && !repatched && !reopened && !checksMoved) return 0await this.save(tx, loaded, state, clock.next())return 0}const seen = group.pullRequestEvents?.[watch.url] ?? []const { events: seenEvents, keys } = pullRequestEvents(watch, seen, botLogin, now)- if (!newHead && !repatched && !checksMoved && keys.length === seen.length && keys.every((key, i) => key === seen[i])) return 0+ if (!newHead && !repatched && !reopened && !checksMoved && keys.length === seen.length && keys.every((key, i) => key === seen[i])) return 0group.pullRequestEvents = { ...group.pullRequestEvents, [watch.url]: keys }// CI failing or a conflict brings a handed task back to its Engineer, in the message that tells it (handed.ts, #3399).const events = reopenOnEvents(group, seenEvents, now)@@ -5478,6 +5487,28 @@ export class SuperTabStore {})}+ /** GitHub shows [url], a pull request the PR watch read before, closed: its head is marked, and the task's checks no longer+ * count at it while another linked pull request is open (checkVerdicts.ts checkedPullRequest, task #3953). Whether it wrote. */+ async pullRequestClosed(tabId: string, groupId: string, url: string, stepIds: readonly string[] = [], lookedUp = false): Promise<boolean> {+ return transaction(this.db, async tx => {+ const row = await tx.superTab.findUnique({ where: { id: tabId }, select: SWARM_ROW })+ if (!row || row.deletedAt) return false+ const loaded = await loadRow(tx, requireParts(row), { groupIds: [groupId, ...stepIds] }), state = cloneData(loaded.state)+ const group = state.conversations.find(c => c.id === groupId), known = group?.pullRequestHeads?.[url]+ if (!group || group.reviewOf || !known || known.closed) return false+ group.pullRequestHeads = { ...group.pullRequestHeads, [url]: { ...known, closed: true } }+ if (lookedUp && !group.stepIds) {+ group.stepIds = [...new Set([...state.conversations.filter(c => c.reviewOf === group.id).map(c => c.id), ...stepIds])]+ }+ // The verdicts of the task and its steps count against the heads that are left (checkVerdicts.ts verdictCounts).+ for (const checked of [group, ...state.conversations.filter(c => c.reviewOf === group.id)]) {+ for (const check of checked.checks) if (check.verdicts?.length) deriveCheck(check, group.pullRequestHeads)+ }+ await this.save(tx, loaded, state, clock.next())+ return true+ })+ }+/** The task's links moved past a pull request whose head it keeps (review of #3218, round 3): the heads it no longer reads* (watchedLinks) are dropped, and the checks of the task and its steps, [stepIds] loaded even when dormant, count against* what is left: a verdict on a dropped one is unverified (checkVerdicts.ts verdictCounts), even with no head left. Whether