diff --git a/changelog/2026-12-03-board-claim-own-pull-request.md b/changelog/2026-12-03-board-claim-own-pull-request.md new file mode 100644 index 0000000000..03f93b7616 --- /dev/null +++ b/changelog/2026-12-03-board-claim-own-pull-request.md @@ -0,0 +1 @@ +Development: a maintainer finishing a contributor's pull request no longer has `npm run pr:ready` (and `npm run check`'s board step) stop on the contributor's own agent-board claim for it. Locally the check now asks `gh pr view` which pull request the checked-out branch belongs to, as CI already knows from its event: a claim tied to that pull request, or its author's claim on its branch that names no pull request, counts as the pull request's own and stays with its holder. A claim tied to a different pull request still stops it. diff --git a/docs/AGENT_BOARD.md b/docs/AGENT_BOARD.md index 69dfcabcbd..d6025a0c64 100644 --- a/docs/AGENT_BOARD.md +++ b/docs/AGENT_BOARD.md @@ -19,6 +19,7 @@ A claim has a kind and a key, and each key has one holder at a time: A claim also records the holder's GitHub login, the branch, the pull request once there is one, and a note of at most 120 characters of plain prose: the server refuses links, paths, addresses, hashes, code and anything token-like in it, and quotes what it found. - **Lifetime.** Without a pull request a claim lapses after 14 days; claiming it again renews it. Once a pull request is linked it holds while that pull request is open. The Worker's hourly run links a claim to the pull request on its branch (`:`), open or merged since the claim was made, releases claims whose pull request was merged or closed and those of suspended accounts, and records why. +- **Finishing someone's pull request.** `pr:ready` and the board check count a claim tied to the pull request being checked (CI's, or the one `gh pr view` finds for the checked-out branch) as that pull request's own, whoever runs them, and leave it with its holder. A claim tied to another pull request still stops them. - **Expiry reminders.** `npm run board -- inbox` and `board_inbox` list your active claims expiring within three days. `pr:ready` alerts you when any need review. Review them with the developer before renewing or releasing a claim. - **Limits.** At most 10 active claims per person (the owner has no limit) and 30 claim writes an hour. For the approved MS-70CDR import, the owner runs `npm run board -- claim-collection zoom-multistomp` to reserve its 28 named effects together on `codex/zoom-multistomp-effects`. Each has an exclusive key. - **Numbers.** The board does not know which numbers `main` uses. Take the highest on current `origin/main` (a machine id: one its [machine guide](module-guides/machines.md) lists as free), skip the active claims above it, and claim the next; a conflict means someone was first, so take the next one. diff --git a/scripts/agent-board.mjs b/scripts/agent-board.mjs index ea6429a895..ba372bf5a7 100644 --- a/scripts/agent-board.mjs +++ b/scripts/agent-board.mjs @@ -221,7 +221,8 @@ function hookRuns(items) { } /** Compare what a branch needs with the board. Without a login it can name holders but not tell whether they are you. - * A claim tied to `pullRequest` belongs to that pull request: a maintainer finishing a contributor's PR holds it for them. */ + * A claim tied to `pullRequest` ({number, branch, author}, from branchPullRequest) belongs to that pull request: a maintainer + * finishing a contributor's PR holds it for them. So does its author's claim on its branch that names no pull request. */ export async function compareClaims(needed, login, { pullRequest, ...options } = {}) { if (!needed.length) return { mine: [], others: [], free: [], unknown: [] } const claims = await listClaims({}, options), byKey = new Map(claims.map(item => [item.kind + ' ' + item.key, item])) @@ -230,7 +231,7 @@ export async function compareClaims(needed, login, { pullRequest, ...options } = // Hook claims overlap by index; the holder's own inside a run give way to it when the run is claimed. A run claimed // ahead of the work (tables/4-6 while the branch adds 4-5 so far) covers the part a branch adds. const near = item.kind === 'hook' ? claims.filter(claim => claim.kind === 'hook' && overlaps(claim.key, item.key)) : [byKey.get(item.kind + ' ' + item.key)].filter(Boolean) - const theirs = login && near.find(claim => claim.holder !== login.toLowerCase() && !(pullRequest && claim.pullRequest === pullRequest)), held = near.find(claim => claim.key === item.key) ?? (item.kind === 'hook' ? near.find(claim => covers(claim.key, item.key)) : undefined) + const theirs = login && near.find(claim => claim.holder !== login.toLowerCase() && !forPull(claim, pullRequest)), held = near.find(claim => claim.key === item.key) ?? (item.kind === 'hook' ? near.find(claim => covers(claim.key, item.key)) : undefined) if (!near.length) result.free.push(item) else if (!login) result.unknown.push({ ...item, claim: near[0] }) else if (theirs) result.others.push({ ...item, claim: theirs }) @@ -239,6 +240,9 @@ export async function compareClaims(needed, login, { pullRequest, ...options } = } return result } +// A claim tied to another pull request never counts, whatever its branch. +const forPull = (claim, pull) => Boolean(pull) && (claim.pullRequest ? claim.pullRequest === pull.number + : Boolean(claim.branch) && claim.branch === pull.branch && claim.holder === pull.author?.toLowerCase()) export const describeClaim = claim => '@' + claim.holder + (claim.branch ? ' on ' + claim.branch : '') + (claim.pullRequest ? ' (#' + claim.pullRequest + ')' : '') + (claim.expiresAt ? ' until ' + claim.expiresAt.slice(0, 10) : '') export const currentBranch = root => { try { const name = git(root, 'rev-parse', '--abbrev-ref', 'HEAD'); return name === 'HEAD' ? null : name } catch { return null } } @@ -381,14 +385,24 @@ function pullRequestEvent() { try { return JSON.parse(readFileSync(process.env.GITHUB_EVENT_PATH, 'utf8')).pull_request ?? null } catch { return null } } const pullRequestAuthor = () => pullRequestEvent()?.user?.login ?? null +/** The pull request a check runs for: CI's event, else the one gh finds for the checked-out branch (a maintainer finishing a + * contributor's PR has their branch checked out). null when there is none or gh cannot tell. */ +export function branchPullRequest(root) { + const event = pullRequestEvent() + if (event) return { number: event.number, branch: event.head?.ref, author: event.user?.login } + try { + const pull = JSON.parse(execFileSync('gh', ['pr', 'view', '--json', 'number,headRefName,author'], { cwd: root, encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'], timeout: 30000 })) + return { number: pull.number, branch: pull.headRefName, author: pull.author?.login } + } catch { return null } +} /** npm run board -- check: refuse what someone else holds; name what is unclaimed. Never fails on an outage. * A pull request's CI run (requireClaims) also refuses what nobody claimed: reviewers asked for the claim on one module PR in seven. */ -export async function checkBranch({ root, base, login, requireClaims = false, pullRequest = null }) { +export async function checkBranch({ root, base, login, requireClaims = false }) { const needed = neededClaims({ root, base }) if (!needed.length) return { ok: true, lines: ['Board: this branch adds nothing that needs a claim.'] } let compared - try { compared = await compareClaims(needed, login, { pullRequest }) } catch (error) { + try { compared = await compareClaims(needed, login, { pullRequest: branchPullRequest(root) }) } catch (error) { if (error instanceof BoardUnavailable) return { ok: true, lines: ['Board: not checked. ' + error.message] } throw error } @@ -536,7 +550,7 @@ async function main(argv) { let name = pullRequestAuthor() if (!name) try { name = await whoami() } catch (error) { if (!(error instanceof BoardUnavailable)) throw error } // In a pull request's CI run the author is known from the event, and an unclaimed module, hook or migration fails. - const result = await checkBranch({ root, base, login: name, requireClaims: Boolean(pullRequestAuthor()), pullRequest: pullRequestEvent()?.number ?? null }) + const result = await checkBranch({ root, base, login: name, requireClaims: Boolean(pullRequestAuthor()) }) result.lines.forEach(line => console.log(line)) return result.ok ? 0 : 1 } diff --git a/scripts/agent-board.test.mjs b/scripts/agent-board.test.mjs index f3e3716400..bcac1ae5c3 100644 --- a/scripts/agent-board.test.mjs +++ b/scripts/agent-board.test.mjs @@ -80,10 +80,15 @@ describe('the board client', () => { it('counts a claim tied to the pull request as its own, whoever holds it', async () => { const needed = [{ kind: 'module', key: 'octatrack/grain' }] const held = { ...claimRow('module', 'octatrack/grain', 'maintainer'), pullRequest: 1858 } - const compare = pullRequest => board.compareClaims(needed, 'Author', { fetch: answer(200, { claims: [held] }), pullRequest }) - expect((await compare(1858)).mine).toHaveLength(1) - expect((await compare(1859)).others).toHaveLength(1) + const compare = (pullRequest, claim = held) => board.compareClaims(needed, 'Author', { fetch: answer(200, { claims: [claim] }), pullRequest }) + expect((await compare({ number: 1858 })).mine).toHaveLength(1) + expect((await compare({ number: 1859 })).others).toHaveLength(1) expect((await compare(undefined)).others).toHaveLength(1) + // A claim naming no pull request is this one's when its holder wrote it on this PR's branch; one tied to another PR never is. + const pull = { number: 1858, branch: 'work', author: 'Maintainer' } + expect((await compare(pull, { ...held, pullRequest: null })).mine).toHaveLength(1) + expect((await compare({ ...pull, author: 'someone' }, { ...held, pullRequest: null })).others).toHaveLength(1) + expect((await compare({ ...pull, number: 1859 }, held)).others).toHaveLength(1) }) it('compares a run of hooks with every claim inside it', async () => { const needed = [{ kind: 'hook', key: 'octatrack/38-51' }] @@ -97,7 +102,7 @@ describe('the board client', () => { const needed = [{ kind: 'hook', key: 'digitakt-ii/tables/4-5' }] const compare = (claims, pullRequest) => board.compareClaims(needed, 'Ada', { fetch: answer(200, { claims }), pullRequest }) expect((await compare([claimRow('hook', 'digitakt-ii/tables/4-6', 'ada')])).mine[0].claim.key).toBe('digitakt-ii/tables/4-6') - expect((await compare([{ ...claimRow('hook', 'digitakt-ii/tables/4-6', 'claptom'), pullRequest: 1999 }], 1999)).mine).toHaveLength(1) + expect((await compare([{ ...claimRow('hook', 'digitakt-ii/tables/4-6', 'claptom'), pullRequest: 1999 }], { number: 1999 })).mine).toHaveLength(1) expect((await compare([claimRow('hook', 'digitakt-ii/tables/4-6', 'bo')])).others).toHaveLength(1) expect((await compare([claimRow('hook', 'digitakt-ii/tables/4', 'ada')])).free).toHaveLength(1) expect((await compare([claimRow('hook', 'digitakt-ii/tables/5-6', 'ada')])).free).toHaveLength(1) @@ -207,10 +212,10 @@ it('requests weekly board activity only when a week count is supplied', async () describe('pr:ready and the board', () => { const needed = [{ kind: 'migration', key: '0084', why: 'new migration' }, { kind: 'hook', key: 'octatrack/37', why: 'new entry' }] - const fake = ({ login = 'ada', claims = [], fail, refuseNotes } = {}) => { + const fake = ({ login = 'ada', claims = [], fail, refuseNotes, pull = null } = {}) => { const taken = [] return { taken, module: { ...board, neededClaims: () => needed, whoami: async () => { if (fail) throw new board.BoardUnavailable('down'); return login }, currentBranch: () => 'work', - compareClaims: (items, name) => board.compareClaims(items, name, { fetch: async () => Response.json({ claims }) }), + branchPullRequest: () => pull, compareClaims: (items, name, { pullRequest } = {}) => board.compareClaims(items, name, { fetch: async () => Response.json({ claims }), pullRequest }), claim: async item => { taken.push(item); if (refuseNotes && item.note) throw new board.BoardError('Leave file paths out of the note.', 400); return { claim: item } } } } } it('claims what the branch adds for the signed-in developer', async () => { @@ -226,6 +231,16 @@ describe('pr:ready and the board', () => { const signedOut = await boardClaims('.', 'main', fake({ login: null }).module) expect(signedOut.problems[0]).toContain('npm run board -- login') }) + it('lets a maintainer finish the claim holder\'s pull request, and still stops on a claim tied to another', async () => { + const pull = { number: 2352, branch: 'prop/hooks', author: 'ilia' }, theirs = { ...claimRow('hook', 'octatrack/30-40', 'ilia'), branch: 'prop/hooks', pullRequest: 2352 } + const { taken, module } = fake({ login: 'repeat98', claims: [theirs], pull }) + const result = await boardClaims('.', 'main', module) + expect(readiness({ board: result }).ok).toBe(true) + expect(taken.map(item => item.key)).toEqual(['0084']) + expect(result.notes).toContain('Board: hook octatrack/37 is held for this pull request by @ilia on prop/hooks (#2352) until 2026-11-12; left with them.') + const other = await boardClaims('.', 'main', fake({ login: 'repeat98', claims: [{ ...theirs, pullRequest: 2353 }], pull }).module) + expect(other.problems[0]).toMatch(/hook octatrack\/37 .* claimed by @ilia/) + }) it('sends a module\'s summary as its claim note, keeps one written by hand, and claims without it when the note is refused', async () => { const withNote = [{ kind: 'module', key: 'octatrack/slicer', why: 'new module folder', note: 'Slices a loop.' }, { kind: 'module', key: 'octatrack/echo', why: 'new module folder', note: 'An echo.' }] const run = async options => { const { taken, module } = fake(options); await boardClaims('.', 'main', { ...module, neededClaims: () => withNote }); return taken } diff --git a/scripts/pr-ready.mjs b/scripts/pr-ready.mjs index 82db9db72e..5f2420a9ea 100644 --- a/scripts/pr-ready.mjs +++ b/scripts/pr-ready.mjs @@ -179,12 +179,15 @@ export async function boardClaims(root, base, board) { if (!needed.length) return null const problems = [], notes = [] let login, compared - try { login = await board.whoami(); compared = await board.compareClaims(needed, login) } catch (error) { + try { login = await board.whoami(); compared = await board.compareClaims(needed, login, { pullRequest: board.branchPullRequest(root) }) } catch (error) { if (error instanceof board.BoardUnavailable) return { problems, notes: ['Board: claims not checked. ' + error.message + ' Run this again before you push once it answers.'] } throw error } for (const item of compared.others) problems.push(`${item.kind} ${item.key} (${item.why}) is claimed by ${board.describeClaim(item.claim)} on the agent board. Never work around a claim: ask them yourself (npm run board -- ask ${item.kind} ${item.key} "…"), or renumber or rename yours (docs/AGENT_BOARD.md).`) - const open = [...compared.free, ...compared.unknown, ...compared.mine] + // A claim this pull request's holder keeps for it (a maintainer finishing their PR) stays theirs: claiming it would be refused. + const forPull = compared.mine.filter(item => item.claim.holder !== login?.toLowerCase()) + for (const item of forPull) notes.push('Board: ' + item.kind + ' ' + item.key + ' is held for this pull request by ' + board.describeClaim(item.claim) + '; left with them.') + const open = [...compared.free, ...compared.unknown, ...compared.mine.filter(item => !forPull.includes(item))] if (!login && open.length) problems.push('This branch adds ' + open.map(item => item.kind + ' ' + item.key).join(', ') + ', which need your claim on the agent board. Sign in once with npm run board -- login, then run this again.') else for (const item of open) { try {