From e5c55093ef9ab67213d75969bf21141ca34dd014 Mon Sep 17 00:00:00 2001 From: noxire-dev Date: Thu, 19 Nov 2026 10:08:04 +0000 Subject: [PATCH 1/4] crew: take the issues handed to it on the Rinai board A crew label on a Rinai issue hands it to this machine's crew. The daemon pulls once a minute; nothing on the web reaches it. It takes an open labelled issue only when the gh-signed-in user applied the label (GitHub's issue events), and only once, a failed plan included. The label is the go: the tickets start when planned. Claude-Session: https://claude.ai/code/session_01TKXiuCo2xF7FNBByzeZ9HV --- docs/yap.md | 9 +++++++++ src/crew.check.ts | 29 +++++++++++++++++++++++++++-- src/crew.ts | 39 +++++++++++++++++++++++++++++++++++---- src/ports.ts | 4 ++++ src/rinai.ts | 26 ++++++++++++++++++++++++++ src/server.ts | 4 ++++ 6 files changed, 105 insertions(+), 6 deletions(-) diff --git a/docs/yap.md b/docs/yap.md index e195f36..cc75d50 100644 --- a/docs/yap.md +++ b/docs/yap.md @@ -238,6 +238,15 @@ what Claude Code reads. `SessionEnd` removes it. Another agent is one more adapter like it, or none (above): a harness can call `/yap` directly. +## Handed on the board + +A Rinai board hands an issue to the crew with one label, `crew`. Nothing on the web reaches the machine: the +daemon reads each registered project's board once a minute (`Crew.watch`), and for an open issue carrying the +label it reads who put it there (GitHub's issue events, the latest `labeled`). It takes the issue only if that +login is the one `gh` is signed in as, and only once: an issue that has ever been on the rail, a failed one +included, waits for `yap take`. The label is the go for that issue, so its tickets start as soon as they are +planned. Progress goes back as it already does: claimed, commented, In review or Blocked. + ## Remote A web client (Rinai first) reaches a machine through a **relay**: the machine's daemon dials out and holds one diff --git a/src/crew.check.ts b/src/crew.check.ts index bd5bac4..f71e60a 100644 --- a/src/crew.check.ts +++ b/src/crew.check.ts @@ -7,7 +7,7 @@ import { Bridge, remote } from './bridge' import { Crew, ROUNDS } from './crew' import { MAIL_MS, threadChips } from './kodrChips' import { childEnv } from './env' -import type { AgentRunner, Tracker, Workspace } from './ports' +import type { AgentRunner, Card, Tracker, Workspace } from './ports' import { Sessions, gitRisk, norm, patchFiles } from './sessions' import { resolve } from 'node:path' import { SqliteStore } from './store' @@ -21,7 +21,7 @@ import { rpc } from './mcp' const ok = { ok: true, log: '' } const TWO = { tickets: [{ title: 'first', body: '', criteria: ['a', 'b'], deps: [] }, { title: 'second', body: '', criteria: ['a', 'b'], deps: [0] }] } -function rig(reviews: boolean[], o: { gateOk?: boolean; plan?: object; hang?: boolean; report?: string; diff?: string; landed?: string; baseBroken?: boolean } = {}) { +function rig(reviews: boolean[], o: { gateOk?: boolean; plan?: object; hang?: boolean; report?: string; diff?: string; landed?: string; baseBroken?: boolean; handed?: (Card & { by: string | null })[] } = {}) { let atBase = false const store = new SqliteStore(new Database(':memory:')) const merged: string[] = [] @@ -94,6 +94,8 @@ function rig(reviews: boolean[], o: { gateOk?: boolean; plan?: object; hang?: bo claim: async (_r, n) => void told.push(`claim ${n}`), move: async (_r, n, column) => void told.push(`move ${n} ${column}`), comment: async (_r, n, text) => void told.push(`comment ${n} ${text.split('\n')[0]}`, ...(text.includes('The crew did') ? ['the reporter summary leaked'] : [])), + handed: async () => o.handed ?? [], + me: async () => 'you', } const crew = new Crew(store, ws, agent, tracker, '/tmp/yap') return { store, crew, merged, squashed, prompts, told, ws, dirty } @@ -201,6 +203,29 @@ const settled = (store: SqliteStore, id: number) => until(() => store.tickets(). assert.equal(store.tickets()[0].summary, 'did the thing', 'the ticket carries its author\'s report') } +// Handed on the board: the crew label from the person this machine works for is the go for that issue. Anyone +// else's label is left alone, and an issue already on the rail is not taken twice. +{ + const card = { number: 7, title: 'from the board', column: 'todo', priority: 'normal', cycle: false } + const { store, crew } = rig([true, true], { handed: [{ ...card, by: 'You' }, { ...card, number: 8, by: 'stranger' }, { ...card, number: 9, by: null }] }) + await crew.openProject('.') + await crew.watch() + await until(() => store.tickets().length === 2 && store.tickets().every(t => t.status !== 'draft')) + assert.deepEqual(store.goals().map(g => g.issue), [7], 'only the issue its own person handed') + await crew.watch() + assert.equal(store.goals().length, 1, 'not taken twice') +} +// A handed issue whose plan failed is not retaken every minute: `yap take` is the retry. +{ + const card = { number: 7, title: 'from the board', column: 'todo', priority: 'normal', cycle: false, by: 'you' } + const { store, crew } = rig([], { plan: { tickets: [] }, handed: [card] }) + await crew.openProject('.') + await crew.watch() + await until(() => store.goals()[0]?.status === 'failed') + await crew.watch() + assert.equal(store.goals().length, 1, 'a failed plan waits for a human') +} + // An author that reports BLOCKED goes to a human, not to the reviewer. { const { store, crew, prompts } = rig([], { report: 'Status: BLOCKED\nVerified: nothing; the API needs a key I do not have' }) diff --git a/src/crew.ts b/src/crew.ts index 451093e..36d1930 100644 --- a/src/crew.ts +++ b/src/crew.ts @@ -256,6 +256,9 @@ function receipt(t: Ticket, checks: Check[] | null, ref: string) { ].join('\n') } +/** The label that hands an issue to the crew on a Rinai board (noxire-dev/issues src/crew.ts). */ +export const CREW_LABEL = 'crew' + export class Crew { private live = new Map }>() private gates = new Map>() @@ -357,13 +360,40 @@ export class Crew { return (await this.tracker.cards(p.tracker)).map(c => ({ ...c, onRail: taken.has(c.number) })) } - /** Bring a tracker issue onto the rail: the planner turns its brief into draft tickets. */ - async take(project_id: number, issue: number, title: string) { + /** Bring a tracker issue onto the rail: the planner turns its brief into draft tickets. `go` starts them once planned. */ + async take(project_id: number, issue: number, title: string, go = false) { const p = await this.tracked(project_id) if (!p.tracker) throw new Error('this repo has no board') if (this.store.goals().some(g => g.project_id === p.id && g.issue === issue && g.status !== 'failed')) throw new Error(`#${issue} is already on the rail`) - return this.plan(p.id, `#${issue} ${title}`, issue, await this.tracker.brief(p.tracker, issue)) + return this.plan(p.id, `#${issue} ${title}`, issue, await this.tracker.brief(p.tracker, issue), go) + } + + private watching = false + /** + * Issues handed to the crew on the board: the `crew` label, put there by the person this machine works for. The + * board only asks; this machine decides, so a label from anyone else, or an issue that ever came onto the rail + * (a failed one included: `yap take` again is the retry), is left alone. The label is the go for that issue. + */ + async watch() { + if (this.watching) return + this.watching = true + try { + for (const p of this.store.projects()) { + if (!p.tracker) continue + const handed = await this.tracker.handed(p.tracker, CREW_LABEL).catch(() => []) + if (!handed.length) continue + const me = (await this.tracker.me().catch(() => '')).toLowerCase() + const seen = new Set(this.store.goals().filter(g => g.project_id === p.id).map(g => g.issue)) + for (const c of handed) { + if (!me || seen.has(c.number) || c.by?.toLowerCase() !== me) continue + this.store.log(p.id, null, 'tracker', { did: 'handed', issue: c.number, by: c.by }) + await this.take(p.id, c.number, c.title, true).catch(() => {}) + } + } + } finally { + this.watching = false + } } /** A goal typed by the human, optionally filed on the tracker first so the board knows about it. */ @@ -454,7 +484,7 @@ export class Crew { } /** Turn a goal into draft tickets. Returns at once; the planner runs behind it. */ - plan(project_id: number, text: string, issue: number | null = null, brief = '') { + plan(project_id: number, text: string, issue: number | null = null, brief = '', go = false) { const p = this.project(project_id) const goal = this.store.addGoal({ project_id, text: text.trim(), status: 'planning', note: '', issue, brief, summary: '' }) this.changed() @@ -470,6 +500,7 @@ export class Crew { } this.store.patchGoal(goal.id, { status: 'ready' }) this.store.log(project_id, null, 'plan', { goal: goal.text, tickets: ids }) + if (go) this.startGoal(goal.id) }) .catch(e => this.store.patchGoal(goal.id, { status: 'failed', note: String(e?.message ?? e) })) .finally(this.changed) diff --git a/src/ports.ts b/src/ports.ts index 2117cfb..a807d7a 100644 --- a/src/ports.ts +++ b/src/ports.ts @@ -186,6 +186,10 @@ export interface Tracker { claim(repo: string, n: number): Promise move(repo: string, n: number, column: 'in-review' | 'blocked'): Promise comment(repo: string, n: number, text: string): Promise + /** Open cards carrying `label`, each with who last put it there (null when that can't be read). */ + handed(repo: string, label: string): Promise<(Card & { by: string | null })[]> + /** The login the tracker acts as: the person this machine works for. */ + me(): Promise } /** The terminal multiplexer the Claude Code sessions run in, when there is one. */ diff --git a/src/rinai.ts b/src/rinai.ts index 7393124..4927772 100644 --- a/src/rinai.ts +++ b/src/rinai.ts @@ -2,6 +2,7 @@ import type { Card, Tracker } from './ports' // A constant, as in gh-rinai: nothing an issue says can send the token anywhere else. const HOST = process.env.RINAI_URL ?? 'https://rinai.dev' +const GITHUB = 'https://api.github.com' const ORDER = ['ongoing', 'todo', 'in-review', 'blocked', 'future'] /** Rinai (rinai.dev), a board over GitHub issues. Acts as the user, through gh's own token. */ @@ -61,4 +62,29 @@ export class Rinai implements Tracker { async comment(repo: string, n: number, text: string) { await this.call('POST', `/api/${repo}/issues/${n}/comments`, text) } + + /** GitHub itself, for the two facts Rinai does not answer: who applied a label, and who we are. */ + private async github(path: string) { + const res = await fetch(GITHUB + path, { headers: { authorization: `Bearer ${await this.auth()}`, 'user-agent': 'yap', accept: 'application/vnd.github+json' } }) + if (!res.ok) throw new Error(`github answered ${res.status}`) + return res.json() as Promise + } + + async handed(repo: string, label: string) { + const cards = (JSON.parse(await this.call('GET', `/api/${repo}/issues?label=${encodeURIComponent(label)}`)).issues as Card[]).filter(c => c.column !== 'done') + return Promise.all( + cards.map(async c => { + // ponytail: the first 100 events, oldest first. On a busier issue the latest `labeled` may be past them, + // and then nobody is known to have handed it: it waits for `yap take`. Page by the Link header if that bites. + const events: any[] = await this.github(`/repos/${repo}/issues/${c.number}/events?per_page=100`).catch(() => []) + const last = events.filter(e => e.event === 'labeled' && e.label?.name === label).pop() + return { ...c, by: (last?.actor?.login as string | undefined) ?? null } + }), + ) + } + + private login?: string + async me() { + return (this.login ??= (await this.github('/user')).login as string) + } } diff --git a/src/server.ts b/src/server.ts index 8fead96..4ca7840 100644 --- a/src/server.ts +++ b/src/server.ts @@ -42,6 +42,10 @@ setInterval(ping, 30_000) const crew = new Crew(store, ws, new ClaudeCli(), new Rinai(), home, changed) await crew.recover() +// Tasks handed to the crew on the board. A pull, once a minute: nothing on the web can reach this machine. +const watch = () => void crew.watch().catch(() => {}) +watch() +setInterval(watch, 60_000) const sessions = new Sessions(ws, store, Date.now, new Herdr(), new Agents({ claude: new ClaudeCli(), codex: new CodexCli() }), () => settings().spawns) // The remote bridge, when `yap pair` left a pairing: one line out to the relay, answering what may be done from From 3e7b1ec97322553b0e2520c22d9420ac097ea659 Mon Sep 17 00:00:00 2001 From: noxire-dev Date: Fri, 20 Nov 2026 14:55:37 +0000 Subject: [PATCH 2/4] crew: act on the kumi review of the board hand-off An auto-started brief is the body and the hander's own comments, so a stranger's comment never runs unread. A take reserves its issue before the first await. Taken issues are counted per board, not per project, and a paused project is skipped. Taking the crew label off drops the handed goal's unstarted tickets. Handed is logged after the take succeeds, a failure once. The labeller is read only for issues not already on the rail, and from the last events page too. me() is cached with the token it was read with. Claude-Session: https://claude.ai/code/session_01TKXiuCo2xF7FNBByzeZ9HV --- docs/yap.md | 5 +++ src/crew.check.ts | 55 +++++++++++++++++++++++++---- src/crew.ts | 90 ++++++++++++++++++++++++++++++++++++++--------- src/ports.ts | 10 +++--- src/rinai.ts | 46 +++++++++++++++--------- 5 files changed, 162 insertions(+), 44 deletions(-) diff --git a/docs/yap.md b/docs/yap.md index cc75d50..d625ac0 100644 --- a/docs/yap.md +++ b/docs/yap.md @@ -247,6 +247,11 @@ login is the one `gh` is signed in as, and only once: an issue that has ever bee included, waits for `yap take`. The label is the go for that issue, so its tickets start as soon as they are planned. Progress goes back as it already does: claimed, commented, In review or Blocked. +Because nobody reads those drafts first, the planner's brief for a handed issue is the issue body and the comments +of the person who handed it, nothing else: on a public repo anyone can comment. A running project reads a board +once, however many checkouts of that repo are registered. Taking the label off (or closing the issue) is the take +back: the handed goal's tickets that have not started are dropped, and a running one is left for a human to stop. + ## Remote A web client (Rinai first) reaches a machine through a **relay**: the machine's daemon dials out and holds one diff --git a/src/crew.check.ts b/src/crew.check.ts index f71e60a..717b2a0 100644 --- a/src/crew.check.ts +++ b/src/crew.check.ts @@ -21,7 +21,7 @@ import { rpc } from './mcp' const ok = { ok: true, log: '' } const TWO = { tickets: [{ title: 'first', body: '', criteria: ['a', 'b'], deps: [] }, { title: 'second', body: '', criteria: ['a', 'b'], deps: [0] }] } -function rig(reviews: boolean[], o: { gateOk?: boolean; plan?: object; hang?: boolean; report?: string; diff?: string; landed?: string; baseBroken?: boolean; handed?: (Card & { by: string | null })[] } = {}) { +function rig(reviews: boolean[], o: { gateOk?: boolean; plan?: object; hang?: boolean; report?: string; diff?: string; landed?: string; baseBroken?: boolean; handed?: (Card & { by: string | null })[]; briefFails?: boolean; slowBrief?: boolean } = {}) { let atBase = false const store = new SqliteStore(new Database(':memory:')) const merged: string[] = [] @@ -89,12 +89,17 @@ function rig(reviews: boolean[], o: { gateOk?: boolean; plan?: object; hang?: bo const told: string[] = [] const tracker: Tracker = { cards: async () => [{ number: 7, title: 'from the board', column: 'todo', priority: 'normal', cycle: true }], - brief: async (_r, n) => `# issue ${n}\nthe brief`, + brief: async (_r, n, by) => { + if (o.briefFails) throw new Error('offline') + if (o.slowBrief) await Bun.sleep(50) + return `# issue ${n}\nthe brief${by ? ` by ${by} only` : ''}` + }, file: async (_r, title) => (told.push(`file ${title}`), { number: 9, title, column: 'todo', priority: 'normal', cycle: false }), claim: async (_r, n) => void told.push(`claim ${n}`), move: async (_r, n, column) => void told.push(`move ${n} ${column}`), comment: async (_r, n, text) => void told.push(`comment ${n} ${text.split('\n')[0]}`, ...(text.includes('The crew did') ? ['the reporter summary leaked'] : [])), - handed: async () => o.handed ?? [], + handed: async () => (o.handed ?? []).map(({ by, ...c }) => c), + labeler: async (_r, n) => (told.push(`events ${n}`), o.handed?.find(c => c.number === n)?.by ?? null), me: async () => 'you', } const crew = new Crew(store, ws, agent, tracker, '/tmp/yap') @@ -204,18 +209,35 @@ const settled = (store: SqliteStore, id: number) => until(() => store.tickets(). } // Handed on the board: the crew label from the person this machine works for is the go for that issue. Anyone -// else's label is left alone, and an issue already on the rail is not taken twice. +// else's label is left alone, and an issue already on the rail is not taken twice, nor its labeller read again. { const card = { number: 7, title: 'from the board', column: 'todo', priority: 'normal', cycle: false } - const { store, crew } = rig([true, true], { handed: [{ ...card, by: 'You' }, { ...card, number: 8, by: 'stranger' }, { ...card, number: 9, by: null }] }) + const { store, crew, prompts, told } = rig([true, true], { handed: [{ ...card, by: 'You' }, { ...card, number: 8, by: 'stranger' }, { ...card, number: 9, by: null }] }) await crew.openProject('.') + await crew.openProject('/elsewhere') await crew.watch() await until(() => store.tickets().length === 2 && store.tickets().every(t => t.status !== 'draft')) - assert.deepEqual(store.goals().map(g => g.issue), [7], 'only the issue its own person handed') + assert.deepEqual(store.goals().map(g => g.issue), [7], 'only the issue its own person handed, in one project of the two on that board') + assert.match(prompts[0], /the brief by You only/, 'an auto-started brief is the body and my own comments') + told.length = 0 await crew.watch() assert.equal(store.goals().length, 1, 'not taken twice') + assert.deepEqual(told.filter(t => t.startsWith('events')), ['events 8', 'events 9'], 'the issue on the rail is not read again') } -// A handed issue whose plan failed is not retaken every minute: `yap take` is the retry. +// A take in flight holds the issue: a manual `yap take` racing the poll does not put it on the rail twice. +{ + const card = { number: 7, title: 'from the board', column: 'todo', priority: 'normal', cycle: false, by: 'you' } + const { store, crew, told } = rig([true, true], { handed: [card], slowBrief: true }) + const p = await crew.openProject('.') + const polling = crew.watch() + await until(() => told.includes('events 7')) + await Bun.sleep(5) // the poll is now inside its take, waiting on the brief + await assert.rejects(crew.take(p.id, 7, 'from the board'), /already on the rail/) + await polling + assert.equal(store.goals().length, 1, 'one goal for one issue') +} +// A handed issue whose plan failed is not retaken every minute: `yap take` is the retry. A take that throws is +// tried again, and logged once. { const card = { number: 7, title: 'from the board', column: 'todo', priority: 'normal', cycle: false, by: 'you' } const { store, crew } = rig([], { plan: { tickets: [] }, handed: [card] }) @@ -224,6 +246,25 @@ const settled = (store: SqliteStore, id: number) => until(() => store.tickets(). await until(() => store.goals()[0]?.status === 'failed') await crew.watch() assert.equal(store.goals().length, 1, 'a failed plan waits for a human') + + const down = rig([], { handed: [card], briefFails: true }) + const q = await down.crew.openProject('.') + await down.crew.watch() + await down.crew.watch() + const tracked = down.store.events({ project_id: q.id }).filter(e => e.kind === 'tracker').map(e => e.data.did) + assert.deepEqual(tracked, ['hand failed'], 'one failure line, no handed line for what was not taken') +} +// Taking the label off is the take back: the handed goal's tickets that have not started are dropped. +{ + const card = { number: 7, title: 'from the board', column: 'todo', priority: 'normal', cycle: false, by: 'you' } + const o = { handed: [card], hang: true } + const { store, crew } = rig([true, true], o) + await crew.openProject('.') + await crew.watch() + await until(() => store.tickets().some(t => t.status === 'working')) + o.handed = [] + await crew.watch() + assert.deepEqual(store.tickets().map(t => t.status), ['working', 'dropped'], 'the running one is left to a human; the waiting one goes') } // An author that reports BLOCKED goes to a human, not to the reviewer. diff --git a/src/crew.ts b/src/crew.ts index 36d1930..139ca30 100644 --- a/src/crew.ts +++ b/src/crew.ts @@ -1,6 +1,6 @@ import { homedir } from 'node:os' import { basename, join, resolve } from 'node:path' -import type { AgentRunner, Goal, Project, Store, Ticket, Tracker, Workspace } from './ports' +import type { AgentRunner, Card, Goal, Project, Store, Ticket, Tracker, Workspace } from './ports' /** Review rounds a ticket gets before it lands in front of a human. */ export const ROUNDS = 4 @@ -360,35 +360,76 @@ export class Crew { return (await this.tracker.cards(p.tracker)).map(c => ({ ...c, onRail: taken.has(c.number) })) } - /** Bring a tracker issue onto the rail: the planner turns its brief into draft tickets. `go` starts them once planned. */ - async take(project_id: number, issue: number, title: string, go = false) { + /** Issues being taken right now, `tracker#n`: claimed before the first await, so two takes can't both pass the check. */ + private taking = new Set() + + /** + * Bring a tracker issue onto the rail: the planner turns its brief into draft tickets. `go` starts them once + * planned, and then the brief is the body and `by`'s own comments only: on a public repo anyone can comment, + * and nothing a stranger wrote runs without a human reading the drafts first. + */ + async take(project_id: number, issue: number, title: string, go = false, by?: string) { const p = await this.tracked(project_id) if (!p.tracker) throw new Error('this repo has no board') - if (this.store.goals().some(g => g.project_id === p.id && g.issue === issue && g.status !== 'failed')) - throw new Error(`#${issue} is already on the rail`) - return this.plan(p.id, `#${issue} ${title}`, issue, await this.tracker.brief(p.tracker, issue), go) + const key = `${p.tracker}#${issue}` + if (this.taking.has(key) || this.onRail(p.tracker).has(issue)) throw new Error(`#${issue} is already on the rail`) + this.taking.add(key) + try { + const brief = await this.tracker.brief(p.tracker, issue, go ? by : undefined) + return this.plan(p.id, `#${issue} ${title}`, issue, brief, go) + } finally { + this.taking.delete(key) + } + } + + /** Issues on the rail from this board, in any project that reads it; a failed one is not. */ + private onRail(tracker: string, failed = false) { + const ids = new Set(this.store.projects().filter(p => p.tracker === tracker).map(p => p.id)) + return new Set(this.store.goals().filter(g => ids.has(g.project_id) && g.issue !== null && (failed || g.status !== 'failed')).map(g => g.issue!)) } private watching = false + private failed = new Set() /** * Issues handed to the crew on the board: the `crew` label, put there by the person this machine works for. The * board only asks; this machine decides, so a label from anyone else, or an issue that ever came onto the rail - * (a failed one included: `yap take` again is the retry), is left alone. The label is the go for that issue. + * (a failed one included: `yap take` again is the retry), is left alone. The label is the go for that issue, and + * taking it off is the take back: tickets of a handed goal that have not started are dropped. */ async watch() { if (this.watching) return this.watching = true try { + const boards = new Set() for (const p of this.store.projects()) { - if (!p.tracker) continue - const handed = await this.tracker.handed(p.tracker, CREW_LABEL).catch(() => []) - if (!handed.length) continue - const me = (await this.tracker.me().catch(() => '')).toLowerCase() - const seen = new Set(this.store.goals().filter(g => g.project_id === p.id).map(g => g.issue)) - for (const c of handed) { - if (!me || seen.has(c.number) || c.by?.toLowerCase() !== me) continue - this.store.log(p.id, null, 'tracker', { did: 'handed', issue: c.number, by: c.by }) - await this.take(p.id, c.number, c.title, true).catch(() => {}) + // One project per board: two checkouts of one repo would both take it. + if (!p.tracker || !p.running || boards.has(p.tracker)) continue + boards.add(p.tracker) + let labelled: Card[] + try { + labelled = await this.tracker.handed(p.tracker, CREW_LABEL) + } catch { + continue // not an empty answer: a failed read must not look like every label came off + } + await this.takeBack(p, new Set(labelled.map(c => c.number))) + const seen = this.onRail(p.tracker, true) + const fresh = labelled.filter(c => !seen.has(c.number)) + if (!fresh.length) continue + const me = await this.tracker.me().catch(() => '') + if (!me) continue + for (const c of fresh) { + const by = await this.tracker.labeler(p.tracker, c.number, CREW_LABEL).catch(() => null) + if (by?.toLowerCase() !== me.toLowerCase()) continue + const key = `${p.tracker}#${c.number}` + try { + const goal = await this.take(p.id, c.number, c.title, true, by) + this.store.log(p.id, null, 'tracker', { did: 'handed', issue: c.number, by, goal: goal.id }) + this.failed.delete(key) + } catch (e: any) { + // Tried again next minute, logged once. + if (!this.failed.has(key)) this.store.log(p.id, null, 'tracker', { did: 'hand failed', issue: c.number, error: String(e?.message ?? e) }) + this.failed.add(key) + } } } } finally { @@ -396,6 +437,23 @@ export class Crew { } } + /** The label came off a handed issue (or it closed): drop the goal's tickets that have not started. */ + private async takeBack(p: Project, labelled: Set) { + const handed = this.store.events({ project_id: p.id, limit: 100_000 }).filter(e => e.kind === 'tracker' && e.data.did === 'handed') + for (const e of handed) { + if (labelled.has(e.data.issue as number)) continue + const idle = this.store.tickets().filter(t => t.goal_id === e.data.goal && (t.status === 'draft' || t.status === 'todo')) + if (!idle.length) continue + for (const t of idle) { + this.store.patchTicket(t.id, { status: 'dropped' }) + // A retried ticket is back in todo with its worktree from the earlier rounds. + await this.ws.cleanup(p.path, this.dir(p, t.id), this.branchOf(t.id)) + } + this.store.log(p.id, null, 'tracker', { did: 'taken back', issue: e.data.issue, dropped: idle.map(t => t.id) }) + this.changed() + } + } + /** A goal typed by the human, optionally filed on the tracker first so the board knows about it. */ async compose(project_id: number, text: string, file: boolean) { const p = await this.tracked(project_id) diff --git a/src/ports.ts b/src/ports.ts index a807d7a..984de5a 100644 --- a/src/ports.ts +++ b/src/ports.ts @@ -179,15 +179,17 @@ export interface Card { export interface Tracker { /** Open cards, what the board is doing now first. */ cards(repo: string): Promise - /** One issue in full, as markdown. */ - brief(repo: string, n: number): Promise + /** One issue in full, as markdown. With `by`: the body and only `by`'s own comments. */ + brief(repo: string, n: number, by?: string): Promise file(repo: string, title: string, body: string): Promise /** Take the issue: in progress, assigned to the user. */ claim(repo: string, n: number): Promise move(repo: string, n: number, column: 'in-review' | 'blocked'): Promise comment(repo: string, n: number, text: string): Promise - /** Open cards carrying `label`, each with who last put it there (null when that can't be read). */ - handed(repo: string, label: string): Promise<(Card & { by: string | null })[]> + /** Open cards carrying `label`. Throws when the board can't be read: an empty answer means none. */ + handed(repo: string, label: string): Promise + /** Who last put `label` on the issue, or null when that can't be read. */ + labeler(repo: string, n: number, label: string): Promise /** The login the tracker acts as: the person this machine works for. */ me(): Promise } diff --git a/src/rinai.ts b/src/rinai.ts index 4927772..7d8e488 100644 --- a/src/rinai.ts +++ b/src/rinai.ts @@ -47,7 +47,16 @@ export class Rinai implements Tracker { .sort((a, b) => rank(a) - rank(b)) } - brief = (repo: string, n: number) => this.call('GET', `/${repo}/issues/${n}.md`) + async brief(repo: string, n: number, by?: string) { + if (!by) return this.call('GET', `/${repo}/issues/${n}.md`) + // ponytail: the first 100 comments. A longer thread loses by's later ones; page by the Link header if that bites. + const [issue, comments] = await Promise.all([ + this.json(`/repos/${repo}/issues/${n}`), + this.json(`/repos/${repo}/issues/${n}/comments?per_page=100`), + ]) + const own = (comments as any[]).filter(c => c.user?.login?.toLowerCase() === by.toLowerCase()) + return [`# #${n} ${issue.title}`, '', issue.body ?? '', ...own.flatMap(c => ['', `## ${by}, ${c.created_at}`, '', c.body ?? ''])].join('\n') + } file = async (repo: string, title: string, body: string) => JSON.parse(await this.call('POST', `/api/${repo}/issues`, { title, body })) as Card @@ -63,28 +72,31 @@ export class Rinai implements Tracker { await this.call('POST', `/api/${repo}/issues/${n}/comments`, text) } - /** GitHub itself, for the two facts Rinai does not answer: who applied a label, and who we are. */ - private async github(path: string) { - const res = await fetch(GITHUB + path, { headers: { authorization: `Bearer ${await this.auth()}`, 'user-agent': 'yap', accept: 'application/vnd.github+json' } }) + /** GitHub itself, for what Rinai does not answer: who applied a label, who commented, and who we are. */ + private async github(path: string, token?: string) { + const res = await fetch(GITHUB + path, { headers: { authorization: `Bearer ${token ?? (await this.auth())}`, 'user-agent': 'yap', accept: 'application/vnd.github+json' } }) if (!res.ok) throw new Error(`github answered ${res.status}`) - return res.json() as Promise + return res } + private json = async (path: string) => (await this.github(path)).json() as Promise async handed(repo: string, label: string) { - const cards = (JSON.parse(await this.call('GET', `/api/${repo}/issues?label=${encodeURIComponent(label)}`)).issues as Card[]).filter(c => c.column !== 'done') - return Promise.all( - cards.map(async c => { - // ponytail: the first 100 events, oldest first. On a busier issue the latest `labeled` may be past them, - // and then nobody is known to have handed it: it waits for `yap take`. Page by the Link header if that bites. - const events: any[] = await this.github(`/repos/${repo}/issues/${c.number}/events?per_page=100`).catch(() => []) - const last = events.filter(e => e.event === 'labeled' && e.label?.name === label).pop() - return { ...c, by: (last?.actor?.login as string | undefined) ?? null } - }), - ) + return (JSON.parse(await this.call('GET', `/api/${repo}/issues?label=${encodeURIComponent(label)}`)).issues as Card[]).filter(c => c.column !== 'done') + } + + async labeler(repo: string, n: number, label: string) { + // Events come oldest first, and the latest `labeled` is what counts: read page 1, then the last page if there is one. + const first = await this.github(`/repos/${repo}/issues/${n}/events?per_page=100`) + const last = first.headers.get('link')?.match(/<([^>]+)>;\s*rel="last"/)?.[1] + const events: any[] = [...(await first.json()), ...(last ? await (await this.github(last.slice(GITHUB.length))).json() : [])] + return (events.filter(e => e.event === 'labeled' && e.label?.name === label).pop()?.actor?.login as string | undefined) ?? null } - private login?: string + /** Cached with the token it was read with: `gh auth switch` is a different person. */ + private login?: { token: string; login: string } async me() { - return (this.login ??= (await this.github('/user')).login as string) + const token = await this.auth() + if (this.login?.token !== token) this.login = { token, login: (await (await this.github('/user', token)).json()).login } + return this.login.login } } From 71810480cae85305864e1b3fa36a992977019f58 Mon Sep 17 00:00:00 2001 From: noxire-dev Date: Fri, 20 Nov 2026 15:50:49 +0000 Subject: [PATCH 3/4] crew: a handed issue comes onto the rail as drafts The owner's call after the second kumi review: the label brings the issue in, a human starts it. Its title, body and comments are anyone's words, so nothing auto-runs, and take back only ever drops drafts, synchronously, with no worktree to clean. A capped board read throws instead of looking like every label came off. The labeller is found walking back from the newest events page. One board failing no longer skips the others; cards() marks the rail per board like take does; the event log is read only when an unlabelled issue still has drafts. Claude-Session: https://claude.ai/code/session_01TKXiuCo2xF7FNBByzeZ9HV --- docs/yap.md | 20 ++++----- src/crew.check.ts | 51 +++++++++++++++-------- src/crew.ts | 104 ++++++++++++++++++++++------------------------ src/ports.ts | 6 +-- src/rinai.ts | 34 +++++++-------- 5 files changed, 112 insertions(+), 103 deletions(-) diff --git a/docs/yap.md b/docs/yap.md index d625ac0..1643476 100644 --- a/docs/yap.md +++ b/docs/yap.md @@ -241,16 +241,16 @@ Another agent is one more adapter like it, or none (above): a harness can call ` ## Handed on the board A Rinai board hands an issue to the crew with one label, `crew`. Nothing on the web reaches the machine: the -daemon reads each registered project's board once a minute (`Crew.watch`), and for an open issue carrying the -label it reads who put it there (GitHub's issue events, the latest `labeled`). It takes the issue only if that -login is the one `gh` is signed in as, and only once: an issue that has ever been on the rail, a failed one -included, waits for `yap take`. The label is the go for that issue, so its tickets start as soon as they are -planned. Progress goes back as it already does: claimed, commented, In review or Blocked. - -Because nobody reads those drafts first, the planner's brief for a handed issue is the issue body and the comments -of the person who handed it, nothing else: on a public repo anyone can comment. A running project reads a board -once, however many checkouts of that repo are registered. Taking the label off (or closing the issue) is the take -back: the handed goal's tickets that have not started are dropped, and a running one is left for a human to stop. +daemon reads each running project's board once a minute (`Crew.watch`), one project per board however many +checkouts of the repo are registered. For an open issue carrying the label it reads who put it there (GitHub's +issue events, walking back from the newest page to the latest `labeled`). It takes the issue only if that login is +the one `gh` is signed in as, and only once: an issue that has ever been on the rail, a failed one included, waits +for `yap take`. + +The label brings the issue onto the rail as drafts, never as started work: its title, body and comments are +whoever wrote them, so a human reads the plan and starts it. Taking the label off (or closing the issue) drops the +handed goal's drafts; anything already started is the human's to stop. A board read that is capped or fails drops +nothing. Progress goes back as it already does: claimed, commented, In review or Blocked. ## Remote diff --git a/src/crew.check.ts b/src/crew.check.ts index 717b2a0..5392363 100644 --- a/src/crew.check.ts +++ b/src/crew.check.ts @@ -21,7 +21,7 @@ import { rpc } from './mcp' const ok = { ok: true, log: '' } const TWO = { tickets: [{ title: 'first', body: '', criteria: ['a', 'b'], deps: [] }, { title: 'second', body: '', criteria: ['a', 'b'], deps: [0] }] } -function rig(reviews: boolean[], o: { gateOk?: boolean; plan?: object; hang?: boolean; report?: string; diff?: string; landed?: string; baseBroken?: boolean; handed?: (Card & { by: string | null })[]; briefFails?: boolean; slowBrief?: boolean } = {}) { +function rig(reviews: boolean[], o: { gateOk?: boolean; plan?: object; hang?: boolean; report?: string; diff?: string; landed?: string; baseBroken?: boolean; handed?: (Card & { by: string | null })[]; briefFails?: boolean; briefGate?: Promise; handedFails?: boolean } = {}) { let atBase = false const store = new SqliteStore(new Database(':memory:')) const merged: string[] = [] @@ -89,16 +89,20 @@ function rig(reviews: boolean[], o: { gateOk?: boolean; plan?: object; hang?: bo const told: string[] = [] const tracker: Tracker = { cards: async () => [{ number: 7, title: 'from the board', column: 'todo', priority: 'normal', cycle: true }], - brief: async (_r, n, by) => { + brief: async (_r, n) => { + if (o.briefGate) told.push(`brief ${n}`) if (o.briefFails) throw new Error('offline') - if (o.slowBrief) await Bun.sleep(50) - return `# issue ${n}\nthe brief${by ? ` by ${by} only` : ''}` + await o.briefGate + return `# issue ${n}\nthe brief` }, file: async (_r, title) => (told.push(`file ${title}`), { number: 9, title, column: 'todo', priority: 'normal', cycle: false }), claim: async (_r, n) => void told.push(`claim ${n}`), move: async (_r, n, column) => void told.push(`move ${n} ${column}`), comment: async (_r, n, text) => void told.push(`comment ${n} ${text.split('\n')[0]}`, ...(text.includes('The crew did') ? ['the reporter summary leaked'] : [])), - handed: async () => (o.handed ?? []).map(({ by, ...c }) => c), + handed: async () => { + if (o.handedFails) throw new Error('capped') + return (o.handed ?? []).map(({ by, ...c }) => c) + }, labeler: async (_r, n) => (told.push(`events ${n}`), o.handed?.find(c => c.number === n)?.by ?? null), me: async () => 'you', } @@ -208,17 +212,18 @@ const settled = (store: SqliteStore, id: number) => until(() => store.tickets(). assert.equal(store.tickets()[0].summary, 'did the thing', 'the ticket carries its author\'s report') } -// Handed on the board: the crew label from the person this machine works for is the go for that issue. Anyone -// else's label is left alone, and an issue already on the rail is not taken twice, nor its labeller read again. +// Handed on the board: the crew label from the person this machine works for brings the issue onto the rail as +// drafts that wait for a human. Anyone else's label is left alone, and an issue already on the rail is not taken +// twice, nor its labeller read again. { const card = { number: 7, title: 'from the board', column: 'todo', priority: 'normal', cycle: false } - const { store, crew, prompts, told } = rig([true, true], { handed: [{ ...card, by: 'You' }, { ...card, number: 8, by: 'stranger' }, { ...card, number: 9, by: null }] }) + const { store, crew, told } = rig([true, true], { handed: [{ ...card, by: 'You' }, { ...card, number: 8, by: 'stranger' }, { ...card, number: 9, by: null }] }) await crew.openProject('.') await crew.openProject('/elsewhere') await crew.watch() - await until(() => store.tickets().length === 2 && store.tickets().every(t => t.status !== 'draft')) + await until(() => store.goals()[0]?.status === 'ready') assert.deepEqual(store.goals().map(g => g.issue), [7], 'only the issue its own person handed, in one project of the two on that board') - assert.match(prompts[0], /the brief by You only/, 'an auto-started brief is the body and my own comments') + assert.deepEqual(store.tickets().map(t => t.status), ['draft', 'draft'], 'drafts: a human starts them') told.length = 0 await crew.watch() assert.equal(store.goals().length, 1, 'not taken twice') @@ -227,12 +232,14 @@ const settled = (store: SqliteStore, id: number) => until(() => store.tickets(). // A take in flight holds the issue: a manual `yap take` racing the poll does not put it on the rail twice. { const card = { number: 7, title: 'from the board', column: 'todo', priority: 'normal', cycle: false, by: 'you' } - const { store, crew, told } = rig([true, true], { handed: [card], slowBrief: true }) + let open!: () => void + const o = { handed: [card], briefGate: new Promise(r => (open = r)) } + const { store, crew, told } = rig([true, true], o) const p = await crew.openProject('.') const polling = crew.watch() - await until(() => told.includes('events 7')) - await Bun.sleep(5) // the poll is now inside its take, waiting on the brief + await until(() => told.includes('brief 7')) await assert.rejects(crew.take(p.id, 7, 'from the board'), /already on the rail/) + open() await polling assert.equal(store.goals().length, 1, 'one goal for one issue') } @@ -254,17 +261,25 @@ const settled = (store: SqliteStore, id: number) => until(() => store.tickets(). const tracked = down.store.events({ project_id: q.id }).filter(e => e.kind === 'tracker').map(e => e.data.did) assert.deepEqual(tracked, ['hand failed'], 'one failure line, no handed line for what was not taken') } -// Taking the label off is the take back: the handed goal's tickets that have not started are dropped. +// Taking the label off is the take back: the handed goal's drafts are dropped. A board that can't be read whole +// drops nothing, and an issue a human took with `yap take` is theirs, label or none. { const card = { number: 7, title: 'from the board', column: 'todo', priority: 'normal', cycle: false, by: 'you' } - const o = { handed: [card], hang: true } + const o: { handed: (typeof card)[]; handedFails?: boolean } = { handed: [card] } const { store, crew } = rig([true, true], o) - await crew.openProject('.') + const p = await crew.openProject('.') await crew.watch() - await until(() => store.tickets().some(t => t.status === 'working')) + await crew.take(p.id, 5, 'taken by hand') + await until(() => store.goals().every(g => g.status === 'ready') && store.tickets().length === 4) o.handed = [] + o.handedFails = true + await crew.watch() + assert.ok(store.tickets().every(t => t.status === 'draft'), 'a failed read is not a label coming off') + o.handedFails = false await crew.watch() - assert.deepEqual(store.tickets().map(t => t.status), ['working', 'dropped'], 'the running one is left to a human; the waiting one goes') + const by = (issue: number) => store.tickets().filter(t => store.goals().find(g => g.id === t.goal_id)!.issue === issue).map(t => t.status) + assert.deepEqual(by(7), ['dropped', 'dropped'], 'the handed drafts go') + assert.deepEqual(by(5), ['draft', 'draft'], 'the hand-taken ones stay') } // An author that reports BLOCKED goes to a human, not to the reviewer. diff --git a/src/crew.ts b/src/crew.ts index 139ca30..6651014 100644 --- a/src/crew.ts +++ b/src/crew.ts @@ -356,33 +356,28 @@ export class Crew { async cards(project_id: number) { const p = await this.tracked(project_id) if (!p.tracker) throw new Error('this repo has no GitHub remote, so there is no board to read') - const taken = new Set(this.store.goals().filter(g => g.project_id === p.id && g.status !== 'failed').map(g => g.issue)) + const taken = this.onRail(p.tracker) return (await this.tracker.cards(p.tracker)).map(c => ({ ...c, onRail: taken.has(c.number) })) } /** Issues being taken right now, `tracker#n`: claimed before the first await, so two takes can't both pass the check. */ private taking = new Set() - /** - * Bring a tracker issue onto the rail: the planner turns its brief into draft tickets. `go` starts them once - * planned, and then the brief is the body and `by`'s own comments only: on a public repo anyone can comment, - * and nothing a stranger wrote runs without a human reading the drafts first. - */ - async take(project_id: number, issue: number, title: string, go = false, by?: string) { + /** Bring a tracker issue onto the rail: the planner turns its brief into draft tickets, which wait for a human. */ + async take(project_id: number, issue: number, title: string) { const p = await this.tracked(project_id) if (!p.tracker) throw new Error('this repo has no board') const key = `${p.tracker}#${issue}` if (this.taking.has(key) || this.onRail(p.tracker).has(issue)) throw new Error(`#${issue} is already on the rail`) this.taking.add(key) try { - const brief = await this.tracker.brief(p.tracker, issue, go ? by : undefined) - return this.plan(p.id, `#${issue} ${title}`, issue, brief, go) + return this.plan(p.id, `#${issue} ${title}`, issue, await this.tracker.brief(p.tracker, issue)) } finally { this.taking.delete(key) } } - /** Issues on the rail from this board, in any project that reads it; a failed one is not. */ + /** Issues on the rail from this board, in any project that reads it; a failed one only when asked. */ private onRail(tracker: string, failed = false) { const ids = new Set(this.store.projects().filter(p => p.tracker === tracker).map(p => p.id)) return new Set(this.store.goals().filter(g => ids.has(g.project_id) && g.issue !== null && (failed || g.status !== 'failed')).map(g => g.issue!)) @@ -393,8 +388,9 @@ export class Crew { /** * Issues handed to the crew on the board: the `crew` label, put there by the person this machine works for. The * board only asks; this machine decides, so a label from anyone else, or an issue that ever came onto the rail - * (a failed one included: `yap take` again is the retry), is left alone. The label is the go for that issue, and - * taking it off is the take back: tickets of a handed goal that have not started are dropped. + * (a failed one included: `yap take` again is the retry), is left alone. A handed issue is planned into drafts + * that wait for a human to start them: its title, body and comments are anyone's words. Taking the label off is + * the take back: the handed goal's drafts are dropped. */ async watch() { if (this.watching) return @@ -402,58 +398,59 @@ export class Crew { try { const boards = new Set() for (const p of this.store.projects()) { - // One project per board: two checkouts of one repo would both take it. + // One project per board: two checkouts of one repo would both read it. if (!p.tracker || !p.running || boards.has(p.tracker)) continue boards.add(p.tracker) - let labelled: Card[] - try { - labelled = await this.tracker.handed(p.tracker, CREW_LABEL) - } catch { - continue // not an empty answer: a failed read must not look like every label came off - } - await this.takeBack(p, new Set(labelled.map(c => c.number))) - const seen = this.onRail(p.tracker, true) - const fresh = labelled.filter(c => !seen.has(c.number)) - if (!fresh.length) continue - const me = await this.tracker.me().catch(() => '') - if (!me) continue - for (const c of fresh) { - const by = await this.tracker.labeler(p.tracker, c.number, CREW_LABEL).catch(() => null) - if (by?.toLowerCase() !== me.toLowerCase()) continue - const key = `${p.tracker}#${c.number}` - try { - const goal = await this.take(p.id, c.number, c.title, true, by) - this.store.log(p.id, null, 'tracker', { did: 'handed', issue: c.number, by, goal: goal.id }) - this.failed.delete(key) - } catch (e: any) { - // Tried again next minute, logged once. - if (!this.failed.has(key)) this.store.log(p.id, null, 'tracker', { did: 'hand failed', issue: c.number, error: String(e?.message ?? e) }) - this.failed.add(key) - } - } + // One board failing to read leaves the others to be read. + await this.watchBoard(p, p.tracker).catch(() => {}) } } finally { this.watching = false } } - /** The label came off a handed issue (or it closed): drop the goal's tickets that have not started. */ - private async takeBack(p: Project, labelled: Set) { - const handed = this.store.events({ project_id: p.id, limit: 100_000 }).filter(e => e.kind === 'tracker' && e.data.did === 'handed') - for (const e of handed) { - if (labelled.has(e.data.issue as number)) continue - const idle = this.store.tickets().filter(t => t.goal_id === e.data.goal && (t.status === 'draft' || t.status === 'todo')) - if (!idle.length) continue - for (const t of idle) { - this.store.patchTicket(t.id, { status: 'dropped' }) - // A retried ticket is back in todo with its worktree from the earlier rounds. - await this.ws.cleanup(p.path, this.dir(p, t.id), this.branchOf(t.id)) + private async watchBoard(p: Project, tracker: string) { + // Throws on a failed or capped read: an empty answer must mean no label, never "could not tell". + const labelled = await this.tracker.handed(tracker, CREW_LABEL) + this.takeBack(p, tracker, new Set(labelled.map(c => c.number))) + const seen = this.onRail(tracker, true) + const fresh = labelled.filter(c => !seen.has(c.number)) + if (!fresh.length) return + const me = (await this.tracker.me()).toLowerCase() + for (const c of fresh) { + const by = await this.tracker.labeler(tracker, c.number, CREW_LABEL).catch(() => null) + if (by?.toLowerCase() !== me) continue + const key = `${tracker}#${c.number}` + try { + const goal = await this.take(p.id, c.number, c.title) + this.store.log(p.id, null, 'tracker', { did: 'handed', issue: c.number, by, goal: goal.id }) + this.failed.delete(key) + } catch (e: any) { + // Tried again next minute, logged once. + if (!this.failed.has(key)) this.store.log(p.id, null, 'tracker', { did: 'hand failed', issue: c.number, error: String(e?.message ?? e) }) + this.failed.add(key) } - this.store.log(p.id, null, 'tracker', { did: 'taken back', issue: e.data.issue, dropped: idle.map(t => t.id) }) - this.changed() } } + /** The label came off a handed issue (or it closed): drop that goal's drafts. Synchronous: nothing can start one meanwhile. */ + private takeBack(p: Project, tracker: string, labelled: Set) { + const projects = new Set(this.store.projects().filter(q => q.tracker === tracker).map(q => q.id)) + const off = new Set(this.store.goals().filter(g => projects.has(g.project_id) && g.issue !== null && !labelled.has(g.issue)).map(g => g.id)) + const drafts = this.store.tickets().filter(t => t.goal_id !== null && off.has(t.goal_id) && t.status === 'draft') + if (!drafts.length) return + // Only goals the label brought: a `yap take` of an unlabelled issue is the human's own. + // ponytail: reads the project's event log, only when an unlabelled issue has drafts. A handed flag on the goal if it grows. + const handed = new Set( + [...projects].flatMap(id => this.store.events({ project_id: id, limit: 100_000 })).filter(e => e.kind === 'tracker' && e.data.did === 'handed').map(e => e.data.goal), + ) + const dropped = drafts.filter(t => handed.has(t.goal_id)) + if (!dropped.length) return + for (const t of dropped) this.store.patchTicket(t.id, { status: 'dropped' }) + this.store.log(p.id, null, 'tracker', { did: 'taken back', dropped: dropped.map(t => t.id) }) + this.changed() + } + /** A goal typed by the human, optionally filed on the tracker first so the board knows about it. */ async compose(project_id: number, text: string, file: boolean) { const p = await this.tracked(project_id) @@ -542,7 +539,7 @@ export class Crew { } /** Turn a goal into draft tickets. Returns at once; the planner runs behind it. */ - plan(project_id: number, text: string, issue: number | null = null, brief = '', go = false) { + plan(project_id: number, text: string, issue: number | null = null, brief = '') { const p = this.project(project_id) const goal = this.store.addGoal({ project_id, text: text.trim(), status: 'planning', note: '', issue, brief, summary: '' }) this.changed() @@ -558,7 +555,6 @@ export class Crew { } this.store.patchGoal(goal.id, { status: 'ready' }) this.store.log(project_id, null, 'plan', { goal: goal.text, tickets: ids }) - if (go) this.startGoal(goal.id) }) .catch(e => this.store.patchGoal(goal.id, { status: 'failed', note: String(e?.message ?? e) })) .finally(this.changed) diff --git a/src/ports.ts b/src/ports.ts index 984de5a..285e5db 100644 --- a/src/ports.ts +++ b/src/ports.ts @@ -179,14 +179,14 @@ export interface Card { export interface Tracker { /** Open cards, what the board is doing now first. */ cards(repo: string): Promise - /** One issue in full, as markdown. With `by`: the body and only `by`'s own comments. */ - brief(repo: string, n: number, by?: string): Promise + /** One issue in full, as markdown. */ + brief(repo: string, n: number): Promise file(repo: string, title: string, body: string): Promise /** Take the issue: in progress, assigned to the user. */ claim(repo: string, n: number): Promise move(repo: string, n: number, column: 'in-review' | 'blocked'): Promise comment(repo: string, n: number, text: string): Promise - /** Open cards carrying `label`. Throws when the board can't be read: an empty answer means none. */ + /** Open cards carrying `label`. Throws when the board can't be read whole: an empty answer means none. */ handed(repo: string, label: string): Promise /** Who last put `label` on the issue, or null when that can't be read. */ labeler(repo: string, n: number, label: string): Promise diff --git a/src/rinai.ts b/src/rinai.ts index 7d8e488..17d7ed8 100644 --- a/src/rinai.ts +++ b/src/rinai.ts @@ -47,16 +47,7 @@ export class Rinai implements Tracker { .sort((a, b) => rank(a) - rank(b)) } - async brief(repo: string, n: number, by?: string) { - if (!by) return this.call('GET', `/${repo}/issues/${n}.md`) - // ponytail: the first 100 comments. A longer thread loses by's later ones; page by the Link header if that bites. - const [issue, comments] = await Promise.all([ - this.json(`/repos/${repo}/issues/${n}`), - this.json(`/repos/${repo}/issues/${n}/comments?per_page=100`), - ]) - const own = (comments as any[]).filter(c => c.user?.login?.toLowerCase() === by.toLowerCase()) - return [`# #${n} ${issue.title}`, '', issue.body ?? '', ...own.flatMap(c => ['', `## ${by}, ${c.created_at}`, '', c.body ?? ''])].join('\n') - } + brief = (repo: string, n: number) => this.call('GET', `/${repo}/issues/${n}.md`) file = async (repo: string, title: string, body: string) => JSON.parse(await this.call('POST', `/api/${repo}/issues`, { title, body })) as Card @@ -72,24 +63,31 @@ export class Rinai implements Tracker { await this.call('POST', `/api/${repo}/issues/${n}/comments`, text) } - /** GitHub itself, for what Rinai does not answer: who applied a label, who commented, and who we are. */ + /** GitHub itself, for what Rinai does not answer: who applied a label, and who we are. */ private async github(path: string, token?: string) { const res = await fetch(GITHUB + path, { headers: { authorization: `Bearer ${token ?? (await this.auth())}`, 'user-agent': 'yap', accept: 'application/vnd.github+json' } }) if (!res.ok) throw new Error(`github answered ${res.status}`) return res } - private json = async (path: string) => (await this.github(path)).json() as Promise async handed(repo: string, label: string) { - return (JSON.parse(await this.call('GET', `/api/${repo}/issues?label=${encodeURIComponent(label)}`)).issues as Card[]).filter(c => c.column !== 'done') + const body = JSON.parse(await this.call('GET', `/api/${repo}/issues?label=${encodeURIComponent(label)}`)) + // A capped board would make a label look like it came off. + if (body.truncated) throw new Error(`${repo} has more tasks than the board reads at once`) + return (body.issues as Card[]).filter(c => c.column !== 'done') } async labeler(repo: string, n: number, label: string) { - // Events come oldest first, and the latest `labeled` is what counts: read page 1, then the last page if there is one. - const first = await this.github(`/repos/${repo}/issues/${n}/events?per_page=100`) - const last = first.headers.get('link')?.match(/<([^>]+)>;\s*rel="last"/)?.[1] - const events: any[] = [...(await first.json()), ...(last ? await (await this.github(last.slice(GITHUB.length))).json() : [])] - return (events.filter(e => e.event === 'labeled' && e.label?.name === label).pop()?.actor?.login as string | undefined) ?? null + // Events come oldest first and the latest `labeled` counts: walk back from the last page to the first that has one. + const path = `/repos/${repo}/issues/${n}/events?per_page=100` + const first = await this.github(path) + const pages = Number(first.headers.get('link')?.match(/[?&]page=(\d+)[^>]*>;\s*rel="last"/)?.[1] ?? 1) + for (let page = pages; page >= 1; page--) { + const events: any[] = await (page === 1 ? first : await this.github(`${path}&page=${page}`)).json() + const last = events.filter(e => e.event === 'labeled' && e.label?.name === label).pop() + if (last) return (last.actor?.login as string | undefined) ?? null + } + return null } /** Cached with the token it was read with: `gh auth switch` is a different person. */ From e904193aaacc1e3019a85931705667cca7ea1996 Mon Sep 17 00:00:00 2001 From: noxire-dev Date: Fri, 20 Nov 2026 15:55:01 +0000 Subject: [PATCH 4/4] crew: take back drops the goal too, and the poll stops re-reading Third kumi review. A taken-back goal is marked dropped once nothing of it is left, so the issue is not locked out: `yap take` or the label again brings it back. Handed goals carry a flag instead of being found in the event log. Who labelled an issue is cached per issue against its updated_at. A board that can't be read whole is logged once until it reads again. The label matches whatever its case, and an issue a `yap take` is holding is skipped, not logged as a failure. Claude-Session: https://claude.ai/code/session_01TKXiuCo2xF7FNBByzeZ9HV --- src/crew.check.ts | 32 +++++++++++++++++------- src/crew.ts | 63 ++++++++++++++++++++++++++++++++--------------- src/ports.ts | 7 +++++- src/rinai.ts | 2 +- 4 files changed, 73 insertions(+), 31 deletions(-) diff --git a/src/crew.check.ts b/src/crew.check.ts index 5392363..651c298 100644 --- a/src/crew.check.ts +++ b/src/crew.check.ts @@ -21,7 +21,7 @@ import { rpc } from './mcp' const ok = { ok: true, log: '' } const TWO = { tickets: [{ title: 'first', body: '', criteria: ['a', 'b'], deps: [] }, { title: 'second', body: '', criteria: ['a', 'b'], deps: [0] }] } -function rig(reviews: boolean[], o: { gateOk?: boolean; plan?: object; hang?: boolean; report?: string; diff?: string; landed?: string; baseBroken?: boolean; handed?: (Card & { by: string | null })[]; briefFails?: boolean; briefGate?: Promise; handedFails?: boolean } = {}) { +function rig(reviews: boolean[], o: { gateOk?: boolean; plan?: object; hang?: boolean; report?: string; diff?: string; landed?: string; baseBroken?: boolean; handed?: (Card & { by: string | null })[] | never[]; briefFails?: boolean; briefGate?: Promise; handedFails?: boolean } = {}) { let atBase = false const store = new SqliteStore(new Database(':memory:')) const merged: string[] = [] @@ -214,9 +214,9 @@ const settled = (store: SqliteStore, id: number) => until(() => store.tickets(). // Handed on the board: the crew label from the person this machine works for brings the issue onto the rail as // drafts that wait for a human. Anyone else's label is left alone, and an issue already on the rail is not taken -// twice, nor its labeller read again. +// twice; who labelled an unchanged issue is not read again. { - const card = { number: 7, title: 'from the board', column: 'todo', priority: 'normal', cycle: false } + const card = { number: 7, title: 'from the board', column: 'todo', priority: 'normal', cycle: false, updated_at: 't1' } const { store, crew, told } = rig([true, true], { handed: [{ ...card, by: 'You' }, { ...card, number: 8, by: 'stranger' }, { ...card, number: 9, by: null }] }) await crew.openProject('.') await crew.openProject('/elsewhere') @@ -227,7 +227,7 @@ const settled = (store: SqliteStore, id: number) => until(() => store.tickets(). told.length = 0 await crew.watch() assert.equal(store.goals().length, 1, 'not taken twice') - assert.deepEqual(told.filter(t => t.startsWith('events')), ['events 8', 'events 9'], 'the issue on the rail is not read again') + assert.deepEqual(told.filter(t => t.startsWith('events')), [], 'an unchanged issue is not read again') } // A take in flight holds the issue: a manual `yap take` racing the poll does not put it on the rail twice. { @@ -244,7 +244,7 @@ const settled = (store: SqliteStore, id: number) => until(() => store.tickets(). assert.equal(store.goals().length, 1, 'one goal for one issue') } // A handed issue whose plan failed is not retaken every minute: `yap take` is the retry. A take that throws is -// tried again, and logged once. +// tried again, and logged once; so is a board that can't be read whole. { const card = { number: 7, title: 'from the board', column: 'todo', priority: 'normal', cycle: false, by: 'you' } const { store, crew } = rig([], { plan: { tickets: [] }, handed: [card] }) @@ -258,11 +258,21 @@ const settled = (store: SqliteStore, id: number) => until(() => store.tickets(). const q = await down.crew.openProject('.') await down.crew.watch() await down.crew.watch() - const tracked = down.store.events({ project_id: q.id }).filter(e => e.kind === 'tracker').map(e => e.data.did) - assert.deepEqual(tracked, ['hand failed'], 'one failure line, no handed line for what was not taken') + const tracked = () => down.store.events({ project_id: q.id }).filter(e => e.kind === 'tracker').map(e => e.data.did) + assert.deepEqual(tracked(), ['hand failed'], 'one failure line, no handed line for what was not taken') + } -// Taking the label off is the take back: the handed goal's drafts are dropped. A board that can't be read whole -// drops nothing, and an issue a human took with `yap take` is theirs, label or none. +{ + const o = { handed: [], handedFails: true } + const { store, crew } = rig([], o) + const p = await crew.openProject('.') + await crew.watch() + await crew.watch() + assert.deepEqual(store.events({ project_id: p.id }).filter(e => e.kind === 'tracker').map(e => e.data.did), ['board unreadable'], 'said once') +} +// Taking the label off is the take back: the handed goal's drafts are dropped, and the goal with them, so a +// label put back brings it in again. A board that can't be read whole drops nothing, and an issue a human took +// with `yap take` is theirs, label or none. { const card = { number: 7, title: 'from the board', column: 'todo', priority: 'normal', cycle: false, by: 'you' } const o: { handed: (typeof card)[]; handedFails?: boolean } = { handed: [card] } @@ -280,6 +290,10 @@ const settled = (store: SqliteStore, id: number) => until(() => store.tickets(). const by = (issue: number) => store.tickets().filter(t => store.goals().find(g => g.id === t.goal_id)!.issue === issue).map(t => t.status) assert.deepEqual(by(7), ['dropped', 'dropped'], 'the handed drafts go') assert.deepEqual(by(5), ['draft', 'draft'], 'the hand-taken ones stay') + o.handed = [card] + await crew.watch() + await until(() => store.goals().filter(g => g.issue === 7).length === 2 && store.goals().every(g => g.status !== 'planning')) + assert.deepEqual(store.goals().filter(g => g.issue === 7).map(g => g.status), ['dropped', 'ready'], 'labelled again, it comes back') } // An author that reports BLOCKED goes to a human, not to the reviewer. diff --git a/src/crew.ts b/src/crew.ts index 6651014..7216bc5 100644 --- a/src/crew.ts +++ b/src/crew.ts @@ -256,7 +256,7 @@ function receipt(t: Ticket, checks: Check[] | null, ref: string) { ].join('\n') } -/** The label that hands an issue to the crew on a Rinai board (noxire-dev/issues src/crew.ts). */ +/** The label that hands an issue to the crew on a Rinai board: Rinai's "Hand to the crew" puts it there. */ export const CREW_LABEL = 'crew' export class Crew { @@ -377,14 +377,18 @@ export class Crew { } } - /** Issues on the rail from this board, in any project that reads it; a failed one only when asked. */ + /** Issues on the rail from this board, in any project that reads it; a failed one only when asked, a taken-back one never. */ private onRail(tracker: string, failed = false) { const ids = new Set(this.store.projects().filter(p => p.tracker === tracker).map(p => p.id)) - return new Set(this.store.goals().filter(g => ids.has(g.project_id) && g.issue !== null && (failed || g.status !== 'failed')).map(g => g.issue!)) + return new Set( + this.store.goals().filter(g => ids.has(g.project_id) && g.issue !== null && g.status !== 'dropped' && (failed || g.status !== 'failed')).map(g => g.issue!), + ) } private watching = false private failed = new Set() + /** Who labelled an issue, as of its `updated_at`: relabelling changes that, so a stale answer is never used. */ + private labellers = new Map() /** * Issues handed to the crew on the board: the `crew` label, put there by the person this machine works for. The * board only asks; this machine decides, so a label from anyone else, or an issue that ever came onto the rail @@ -401,8 +405,16 @@ export class Crew { // One project per board: two checkouts of one repo would both read it. if (!p.tracker || !p.running || boards.has(p.tracker)) continue boards.add(p.tracker) - // One board failing to read leaves the others to be read. - await this.watchBoard(p, p.tracker).catch(() => {}) + // One board failing to read leaves the others to be read. Logged once until it reads again: a board too big + // to read whole would otherwise do nothing, silently, every minute. + const key = `board ${p.tracker}` + try { + await this.watchBoard(p, p.tracker) + this.failed.delete(key) + } catch (e: any) { + if (!this.failed.has(key)) this.store.log(p.id, null, 'tracker', { did: 'board unreadable', error: String(e?.message ?? e) }) + this.failed.add(key) + } } } finally { this.watching = false @@ -418,11 +430,17 @@ export class Crew { if (!fresh.length) return const me = (await this.tracker.me()).toLowerCase() for (const c of fresh) { - const by = await this.tracker.labeler(tracker, c.number, CREW_LABEL).catch(() => null) - if (by?.toLowerCase() !== me) continue const key = `${tracker}#${c.number}` + // A `yap take` of it is in flight: that one wins, and losing the race is not a failure. + if (this.taking.has(key)) continue + const known = this.labellers.get(key) + const by = known && c.updated_at && known.at === c.updated_at ? known.by : await this.tracker.labeler(tracker, c.number, CREW_LABEL).catch(() => undefined) + if (by === undefined) continue // could not read: ask again next minute + if (c.updated_at) this.labellers.set(key, { at: c.updated_at, by }) + if (by?.toLowerCase() !== me) continue try { const goal = await this.take(p.id, c.number, c.title) + this.store.patchGoal(goal.id, { handed: true }) this.store.log(p.id, null, 'tracker', { did: 'handed', issue: c.number, by, goal: goal.id }) this.failed.delete(key) } catch (e: any) { @@ -433,22 +451,27 @@ export class Crew { } } - /** The label came off a handed issue (or it closed): drop that goal's drafts. Synchronous: nothing can start one meanwhile. */ + /** + * The label came off a handed issue (or it closed): drop that goal's drafts, and the goal with them once nothing + * of it is left, so labelling it again brings it back. Synchronous: nothing can start a draft meanwhile. + */ private takeBack(p: Project, tracker: string, labelled: Set) { const projects = new Set(this.store.projects().filter(q => q.tracker === tracker).map(q => q.id)) - const off = new Set(this.store.goals().filter(g => projects.has(g.project_id) && g.issue !== null && !labelled.has(g.issue)).map(g => g.id)) - const drafts = this.store.tickets().filter(t => t.goal_id !== null && off.has(t.goal_id) && t.status === 'draft') - if (!drafts.length) return // Only goals the label brought: a `yap take` of an unlabelled issue is the human's own. - // ponytail: reads the project's event log, only when an unlabelled issue has drafts. A handed flag on the goal if it grows. - const handed = new Set( - [...projects].flatMap(id => this.store.events({ project_id: id, limit: 100_000 })).filter(e => e.kind === 'tracker' && e.data.did === 'handed').map(e => e.data.goal), - ) - const dropped = drafts.filter(t => handed.has(t.goal_id)) - if (!dropped.length) return - for (const t of dropped) this.store.patchTicket(t.id, { status: 'dropped' }) - this.store.log(p.id, null, 'tracker', { did: 'taken back', dropped: dropped.map(t => t.id) }) - this.changed() + const off = this.store.goals().filter(g => g.handed && g.status !== 'dropped' && projects.has(g.project_id) && g.issue !== null && !labelled.has(g.issue)) + let changed = false + for (const g of off) { + const mine = this.store.tickets().filter(t => t.goal_id === g.id) + const drafts = mine.filter(t => t.status === 'draft') + for (const t of drafts) this.store.patchTicket(t.id, { status: 'dropped' }) + // Still planning: its drafts are not there yet, and the next minute drops them. + const gone = g.status !== 'planning' && mine.every(t => t.status === 'draft' || t.status === 'dropped') + if (gone) this.store.patchGoal(g.id, { status: 'dropped' }) + if (!drafts.length && !gone) continue + this.store.log(p.id, null, 'tracker', { did: 'taken back', issue: g.issue, dropped: drafts.map(t => t.id) }) + changed = true + } + if (changed) this.changed() } /** A goal typed by the human, optionally filed on the tracker first so the board knows about it. */ diff --git a/src/ports.ts b/src/ports.ts index 285e5db..2d7cb78 100644 --- a/src/ports.ts +++ b/src/ports.ts @@ -19,12 +19,15 @@ export interface Goal { id: number project_id: number text: string - status: 'planning' | 'ready' | 'failed' + /** `dropped`: handed on the board, then taken back before any of it started. */ + status: 'planning' | 'ready' | 'failed' | 'dropped' note: string /** The tracker issue this goal came from or was filed as. */ issue: number | null /** The issue in full, for the planner. */ brief: string + /** Brought in by the board's crew label, not by a human's `yap take`: taking the label off drops it. */ + handed?: boolean /** What the crew did, written for the person who asked, once every ticket has merged. */ summary: string } @@ -173,6 +176,8 @@ export interface Card { column: string priority: string cycle: boolean + /** When the issue last changed, as the tracker says it. */ + updated_at?: string } /** An issue board the crew can read from, file to, and report back on. */ diff --git a/src/rinai.ts b/src/rinai.ts index 17d7ed8..63bafef 100644 --- a/src/rinai.ts +++ b/src/rinai.ts @@ -84,7 +84,7 @@ export class Rinai implements Tracker { const pages = Number(first.headers.get('link')?.match(/[?&]page=(\d+)[^>]*>;\s*rel="last"/)?.[1] ?? 1) for (let page = pages; page >= 1; page--) { const events: any[] = await (page === 1 ? first : await this.github(`${path}&page=${page}`)).json() - const last = events.filter(e => e.event === 'labeled' && e.label?.name === label).pop() + const last = events.filter(e => e.event === 'labeled' && e.label?.name?.toLowerCase() === label.toLowerCase()).pop() if (last) return (last.actor?.login as string | undefined) ?? null } return null