diff --git a/changelog.d/789-live-artifact-decision.changed.md b/changelog.d/789-live-artifact-decision.changed.md new file mode 100644 index 00000000..d025b66a --- /dev/null +++ b/changelog.d/789-live-artifact-decision.changed.md @@ -0,0 +1 @@ +A scheduled run now continues an open work item only when its artifact is still live: before the scheduler chooses a follow-up (or a review), it reads the item's pull request or issue once, and a merged pull request or closed issue turns that choice into a fresh iterate on the repository's own goal instead — the stale row is marked with the state that was observed, so the next tick does not choose it again. An open artifact, a failed read and an item that names no link all keep the previous behaviour, and a held-back repository costs no read. diff --git a/docs/architecture.md b/docs/architecture.md index ffb155fe..2d0ff058 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -1447,6 +1447,17 @@ without merging is `closed`, not `open`, so the header's `(closed/merged)` is true as written; a job row written before that column says the observation was not recorded rather than borrowing a time. +Since #789 the decision that produces the run is evidence-linked too: +`decideNextJob()` is async and takes the same reader, so the chosen item's link +is read after due-ness (a held-back repository costs no read) before it can +become a `followup` or a `review`. A `closed` or `merged` answer turns the +decision into a fresh `iterate` on the repository's own goal — no goal override +and no `work_item_id` — and the stale row is marked with the state that was +observed once the run exists, so the next tick does not choose it again. An +`open` answer, a read that failed and an item that names no link all keep the +previous behaviour: a failed read saw nothing, so it is not evidence that the +work is finished. + The goal also carries one **temporal delta** line (#366), built by `summariseDelta()` in `src/lib/temporal.ts` from two reads plus the commit lookup that dates them, all against the previous finished run's base: diff --git a/src/jobs/continuity.ts b/src/jobs/continuity.ts index 658aa981..a4e41938 100644 --- a/src/jobs/continuity.ts +++ b/src/jobs/continuity.ts @@ -331,6 +331,10 @@ export type NextJob = | { kind: 'discover' } | { kind: 'followup'; workItemId: string; prUrl?: string; branch?: string } | { kind: 'review'; workItemId: string; prUrl: string; branch?: string } + // #789: the chosen row's artifact is finished, so the run does not continue it. + // The row is named so the dispatcher can mark it; the run itself is a fresh + // iterate on the repository's own goal. + | { kind: 'iterate'; staleWorkItemId: string; artifactState: 'closed' | 'merged' } // #807: a ready item that does not start names what held it back, and when it // became dispatchable, so the dispatcher can publish why instead of a gap. | { kind: 'none'; reason: 'not due'; blockedBy: 'cooldown' | 'failure_backoff'; readyAt: number | null } @@ -359,7 +363,14 @@ export const MIN_DISCOVERY_INTERVAL_MINUTES = 60 //: Pick the next scheduled job kind. This never blocks a due repo (the #219 //: lesson): when work is open it changes *what* runs, not whether a run happens. -export function decideNextJob(state: SchedulerState, options: { cooldownMs?: number } = {}): NextJob { +//: #789: the run must also be supported by the item's live artifact, so the +//: reader is consulted after due-ness (a held-back repo costs no read) and only +//: for an item that names a GitHub link. A read that fails keeps the item: a +//: failed read saw nothing, so it is not evidence that the work is finished. +export async function decideNextJob( + state: SchedulerState, + options: { cooldownMs?: number; read?: CarryOverReader } = {}, +): Promise { const work = state.openWork .filter((item) => item.state !== 'merged' && item.state !== 'closed') .sort((a, b) => b.priority - a.priority || a.created_at.localeCompare(b.created_at)) @@ -384,6 +395,22 @@ export function decideNextJob(state: SchedulerState, options: { cooldownMs?: num } const item = work[0] if (item === undefined) return { kind: 'none', reason: 'idle' } + // #789: the pull request is the finished state of the work when there is one, + // so it governs; only an item with no pull request is judged by its issue. + const link = cleanLink(item.pr_url) ?? cleanLink(item.issue_url) + if (link !== null && options.read !== undefined) { + let artifactState: CarryOverLinkState | 'unverified' + try { + artifactState = await options.read.read(link) + } catch { + artifactState = 'unverified' + } + // `unverified` keeps the item: the read saw no state, which is exactly + // slice 1's rule for a thrown line read. + if (artifactState === 'closed' || artifactState === 'merged') { + return { kind: 'iterate', staleWorkItemId: item.id, artifactState } + } + } if (item.pr_url) { const canReview = state.reviewerEnabled && state.repo.repo_role === 'maintainer' // A review that is not due never skips a due repo (#219). Continue the same diff --git a/src/jobs/service.ts b/src/jobs/service.ts index 74a7736d..b9f958ad 100644 --- a/src/jobs/service.ts +++ b/src/jobs/service.ts @@ -1170,6 +1170,12 @@ export class Jobs { } } + // One live-state source for both readers: the scheduler's evidence gate on the + // chosen item (#789) and the carry-over block it is about to build (#365). + private liveArtifactReader(repo: RepoRow): CarryOverReader { + return (this.carryOverReaderOverride ?? ((target: RepoRow) => this.carryOverReader(target)))(repo) + } + async enqueue(repo: RepoRow, trigger: 'manual' | 'schedule' | 'followup', goalOverride?: string, kind: JobKind = 'iterate', options: { branch?: string; prUrl?: string; baseBranch?: string; workItemId?: string; priority?: number; retryAttempt?: number; retryOf?: string; ownerStarted?: boolean; workType?: string } = {}): Promise { // #283: the platform stop outranks every other refusal. It is checked first, // above the work-type resolution and the per-repository and per-role gates, so @@ -1281,7 +1287,7 @@ export class Jobs { ? await buildCarryOver({ recent: this.store.listJobsForRepo(repo.id, 3), openWork: this.store.listOpenWorkItems(repo.id), - read: (this.carryOverReaderOverride ?? ((target: RepoRow) => this.carryOverReader(target)))(repo), + read: this.liveArtifactReader(repo), }) : null const carryOver = carryOverBlock?.text ?? '' @@ -4483,7 +4489,10 @@ export class Jobs { continue } const recent = this.store.listJobsForRepo(repo.id, 6) - const next = decideNextJob({ + // #789: the scheduler decides on evidence too. The same reader that checks + // the carry-over lines checks the chosen item, and only after due-ness, so + // a held-back repository costs no read. + const next = await decideNextJob({ repo, now, lastRunAt: repo.last_run_at, @@ -4499,7 +4508,7 @@ export class Jobs { reviewDue: this.dueReview(repo), lastDiscoverAt: this.store.getSetting(`discover:${repo.id}`), discoverIntervalMinutes: Math.max(MIN_DISCOVERY_INTERVAL_MINUTES, repo.interval_minutes), - }, plan === null ? {} : { cooldownMs: 0 }) + }, { ...(plan === null ? {} : { cooldownMs: 0 }), read: this.liveArtifactReader(repo) }) if (next.kind === 'none') { // #807: a held-back item names its holder; an empty backlog clears the // idle reading rather than leaving the last wait on the dashboard. @@ -4527,12 +4536,20 @@ export class Jobs { ? await this.enqueue(repo, 'schedule', undefined, 'review', { workItemId: next.workItemId, prUrl: next.prUrl }) : next.kind === 'discover' ? await this.enqueue(repo, 'schedule', undefined, 'discover') - : await this.enqueue(repo, 'followup', undefined, 'iterate', { - workItemId: next.workItemId, - ...(next.prUrl === undefined ? {} : { prUrl: next.prUrl }), - ...(next.branch === undefined ? {} : { branch: next.branch }), - }) + : next.kind === 'iterate' + // #789: the row's artifact is finished, so this is a fresh iterate on + // the repository's own goal, not a continuation: no goal override and + // no work-item id, and the due run still happens (the #219 rule). + ? await this.enqueue(repo, 'schedule', undefined, 'iterate') + : await this.enqueue(repo, 'followup', undefined, 'iterate', { + workItemId: next.workItemId, + ...(next.prUrl === undefined ? {} : { prUrl: next.prUrl }), + ...(next.branch === undefined ? {} : { branch: next.branch }), + }) if (result.ok) { + // #789: mark the stale row only once the run exists, so a refused enqueue + // leaves it for the next tick instead of dropping the fresh iterate. + if (next.kind === 'iterate') this.store.updateWorkItem(next.staleWorkItemId, { state: next.artifactState }) recordDispatched({ repo, kind: next.kind === 'review' ? 'review' : next.kind === 'discover' ? 'discover' : 'iterate', diff --git a/test/automationHealth.test.ts b/test/automationHealth.test.ts index c2094483..845ae121 100644 --- a/test/automationHealth.test.ts +++ b/test/automationHealth.test.ts @@ -46,21 +46,21 @@ function state(overrides: Partial = {}): SchedulerState { } } -test('a not-due repository names the cooldown it waits for, and a failing one names the backoff', () => { - const cooling = decideNextJob(state({ lastRunAt: '2026-11-16T11:59:30.000Z' })) +test('a not-due repository names the cooldown it waits for, and a failing one names the backoff', async () => { + const cooling = await decideNextJob(state({ lastRunAt: '2026-11-16T11:59:30.000Z' })) assert.equal(cooling.kind, 'none') if (cooling.kind !== 'none' || cooling.reason !== 'not due') throw new Error('expected a blocked decision') assert.equal(cooling.blockedBy, 'cooldown') assert.equal(cooling.readyAt, Date.parse('2026-11-16T11:59:30.000Z')) - const failing = decideNextJob(state({ lastRunAt: '2026-11-16T11:59:30.000Z', failureStreak: 2 })) + const failing = await decideNextJob(state({ lastRunAt: '2026-11-16T11:59:30.000Z', failureStreak: 2 })) assert.equal(failing.kind, 'none') if (failing.kind !== 'none' || failing.reason !== 'not due') throw new Error('expected a blocked decision') assert.equal(failing.blockedBy, 'failure_backoff') // A failure that is not yet backing off is still the plain cooldown, because // `isDue` only multiplies the wait from the second consecutive failure. - const oneFailure = decideNextJob(state({ lastRunAt: '2026-11-16T11:59:30.000Z', failureStreak: 1 })) + const oneFailure = await decideNextJob(state({ lastRunAt: '2026-11-16T11:59:30.000Z', failureStreak: 1 })) if (oneFailure.kind !== 'none' || oneFailure.reason !== 'not due') throw new Error('expected a blocked decision') assert.equal(oneFailure.blockedBy, 'cooldown') }) diff --git a/test/jobs.test.ts b/test/jobs.test.ts index d9c6f08e..dc970169 100644 --- a/test/jobs.test.ts +++ b/test/jobs.test.ts @@ -6,6 +6,7 @@ import { InstanceManager } from '../src/flybot/instances.js' import { PLAYBOOK_VERSION, playbookFingerprint } from '../src/flybot/playbook.js' import { Jobs } from '../src/jobs/service.js' import { buildCarryOver, buildRunDigest, classifyOutput, decideNextJob, failureStreak, isMetaTitle, lastDurationMs } from '../src/jobs/continuity.js' +import type { CarryOverLinkState, CarryOverReader, SchedulerState } from '../src/jobs/continuity.js' import type { Runner, RunnerContext } from '../src/jobs/runner.js' import { encryptSecret, sha256Hex } from '../src/lib/secrets.js' import { metricsSnapshot } from '../src/lib/metrics.js' @@ -2438,8 +2439,8 @@ test('carry-over is bounded, names the open work and is explicitly data', async assert.match(text, /https:\/\/x\/pr\/3/) }) -test('a due repo with no open work stays idle until discovery is due', () => { - const next = decideNextJob({ +test('a due repo with no open work stays idle until discovery is due', async () => { + const next = await decideNextJob({ repo: repo(), now, lastRunAt: iso(-2 * 3_600_000), intervalMinutes: 60, lastDurationMs: 1_000, failureStreak: 0, openWork: [], reviewerEnabled: false, reviewDue: true, lastDiscoverAt: iso(-60 * 60_000), discoverIntervalMinutes: 720, @@ -2447,8 +2448,8 @@ test('a due repo with no open work stays idle until discovery is due', () => { assert.deepEqual(next, { kind: 'none', reason: 'idle' }) }) -test('an idle repo runs one discovery session when the discovery interval is due', () => { - const next = decideNextJob({ +test('an idle repo runs one discovery session when the discovery interval is due', async () => { + const next = await decideNextJob({ repo: repo(), now, lastRunAt: iso(-2 * 3_600_000), intervalMinutes: 60, lastDurationMs: 1_000, failureStreak: 0, openWork: [], reviewerEnabled: false, reviewDue: true, lastDiscoverAt: iso(-13 * 60 * 60_000), discoverIntervalMinutes: 720, @@ -2456,30 +2457,112 @@ test('an idle repo runs one discovery session when the discovery interval is due assert.deepEqual(next, { kind: 'discover' }) }) -test('open work continues instead of starting another isolated iterate', () => { +test('open work continues instead of starting another isolated iterate', async () => { const pr = workItem({ pr_url: 'https://x/pr/3', state: 'pr_ready' }) - const followup = decideNextJob({ + const followup = await decideNextJob({ repo: repo({ repo_role: 'developer' }), now, lastRunAt: iso(-2 * 3_600_000), intervalMinutes: 60, lastDurationMs: 0, failureStreak: 0, openWork: [pr], reviewerEnabled: true, reviewDue: true, }) assert.equal(followup.kind, 'followup') assert.equal(followup.kind === 'followup' ? followup.workItemId : null, 'work_1') - const review = decideNextJob({ + const review = await decideNextJob({ repo: repo({ repo_role: 'maintainer' }), now, lastRunAt: iso(-2 * 3_600_000), intervalMinutes: 60, lastDurationMs: 0, failureStreak: 0, openWork: [pr], reviewerEnabled: true, reviewDue: true, }) assert.equal(review.kind, 'review') assert.equal(review.kind === 'review' ? review.prUrl : null, 'https://x/pr/3') - const scoped = decideNextJob({ + const scoped = await decideNextJob({ repo: repo(), now, lastRunAt: iso(-2 * 3_600_000), intervalMinutes: 60, lastDurationMs: 0, failureStreak: 0, openWork: [workItem()], reviewerEnabled: false, reviewDue: false, }) assert.equal(scoped.kind, 'followup') }) -test('the interval respects the last duration and backs off after failures', () => { +// -- #789: the decision that produces the run is evidence-linked too --------- + +function fakeArtifactReader(outcome: CarryOverLinkState | 'throw'): CarryOverReader & { reads: string[] } { + const reads: string[] = [] + return { + reads, + async read(link: string) { + reads.push(link) + if (outcome === 'throw') throw new Error('the state could not be read') + return outcome + }, + } +} + +function dueState(overrides: Partial = {}): SchedulerState { + return { + repo: repo({ repo_role: 'developer' }), now, lastRunAt: iso(-2 * 3_600_000), + intervalMinutes: 60, lastDurationMs: 0, failureStreak: 0, + openWork: [workItem()], reviewerEnabled: false, reviewDue: false, ...overrides, + } +} + +test('a merged artifact turns the chosen item into a fresh iterate, not a continuation', async () => { + const read = fakeArtifactReader('merged') + const next = await decideNextJob(dueState({ openWork: [workItem({ pr_url: 'https://x/pr/3', state: 'pr_ready' })] }), { read }) + assert.equal(next.kind, 'iterate') + assert.equal(next.kind === 'iterate' ? next.staleWorkItemId : null, 'work_1') + assert.equal(next.kind === 'iterate' ? next.artifactState : null, 'merged') + assert.deepEqual(read.reads, ['https://x/pr/3']) + + // A merged pull request is the finished state of the work; a due review + // window does not turn it back into a review job. + const reviewing = fakeArtifactReader('merged') + const reviewNext = await decideNextJob(dueState({ + repo: repo({ repo_role: 'maintainer' }), reviewerEnabled: true, reviewDue: true, + openWork: [workItem({ pr_url: 'https://x/pr/3', state: 'pr_ready' })], + }), { read: reviewing }) + assert.equal(reviewNext.kind, 'iterate') +}) + +test('a closed issue is not continued either', async () => { + const read = fakeArtifactReader('closed') + const next = await decideNextJob(dueState(), { read }) + assert.equal(next.kind, 'iterate') + assert.equal(next.kind === 'iterate' ? next.artifactState : null, 'closed') + assert.deepEqual(read.reads, ['https://github.com/octo/demo/issues/1']) +}) + +test('an open artifact still continues the item with one read', async () => { + const read = fakeArtifactReader('open') + const next = await decideNextJob(dueState(), { read }) + assert.equal(next.kind, 'followup') + assert.equal(next.kind === 'followup' ? next.workItemId : null, 'work_1') + assert.deepEqual(read.reads, ['https://github.com/octo/demo/issues/1']) +}) + +test('a failed read keeps the item and continues it', async () => { + const read = fakeArtifactReader('throw') + const next = await decideNextJob(dueState(), { read }) + assert.equal(next.kind, 'followup') + assert.equal(read.reads.length, 1) +}) + +test('a held-back, link-less or idle repository costs no read', async () => { + const notDue = fakeArtifactReader('merged') + const held = await decideNextJob(dueState({ lastRunAt: iso(-30 * 60_000) }), { read: notDue }) + assert.equal(held.kind, 'none') + assert.deepEqual(notDue.reads, []) + + const noLink = fakeArtifactReader('merged') + const continued = await decideNextJob(dueState({ openWork: [workItem({ issue_url: null })] }), { read: noLink }) + assert.equal(continued.kind, 'followup') + assert.deepEqual(noLink.reads, []) + + const idle = fakeArtifactReader('merged') + const none = await decideNextJob(dueState({ + openWork: [], lastDiscoverAt: iso(-60 * 60_000), discoverIntervalMinutes: 720, + }), { read: idle }) + assert.equal(none.kind, 'none') + assert.deepEqual(idle.reads, []) +}) + +test('the interval respects the last duration and backs off after failures', async () => { const recent = [job({ status: 'failed' }), job({ id: 'job_0', status: 'failed' })] assert.equal(failureStreak(recent), 2) assert.equal(lastDurationMs(recent), 600_000) @@ -2497,12 +2580,12 @@ test('the interval respects the last duration and backs off after failures', () ]), 0, ) - const notDue = decideNextJob({ + const notDue = await decideNextJob({ repo: repo(), now, lastRunAt: iso(-30 * 60_000), intervalMinutes: 60, lastDurationMs: 60 * 60_000, failureStreak: 1, openWork: [workItem()], reviewerEnabled: false, reviewDue: true, }) assert.equal(notDue.kind, 'none') - const backedOff = decideNextJob({ + const backedOff = await decideNextJob({ repo: repo(), now, lastRunAt: iso(-90 * 60_000), intervalMinutes: 60, lastDurationMs: 0, failureStreak: 3, openWork: [workItem()], reviewerEnabled: false, reviewDue: true, }) @@ -2510,14 +2593,14 @@ test('the interval respects the last duration and backs off after failures', () }) -test('discovery never runs more often than every 60 minutes', () => { - const early = decideNextJob({ +test('discovery never runs more often than every 60 minutes', async () => { + const early = await decideNextJob({ repo: repo(), now, lastRunAt: null, intervalMinutes: 15, lastDurationMs: 0, failureStreak: 0, openWork: [], reviewerEnabled: false, reviewDue: false, lastDiscoverAt: iso(-30 * 60_000), discoverIntervalMinutes: 15, }) assert.equal(early.kind, 'none') - const due = decideNextJob({ + const due = await decideNextJob({ repo: repo(), now, lastRunAt: null, intervalMinutes: 15, lastDurationMs: 0, failureStreak: 0, openWork: [], reviewerEnabled: false, reviewDue: false, lastDiscoverAt: iso(-61 * 60_000), discoverIntervalMinutes: 15, @@ -2552,6 +2635,65 @@ test('the scheduler runs one discovery session when idle, then idles', async () } }) +test('the scheduler starts a fresh iterate when the chosen item is stale', async () => { + const config = testConfig() + const store = new Store(':memory:') + const jobs = new Jobs(store, config, { name: 'capture', async run() { return { status: 'running' } } }, new MemoryMailer()) + try { + const user = makeUser(store, { login: 'scheduler-stale', status: 'approved' }) + const repo = store.createRepo({ + id: 'repo_scheduler_stale', userId: user.id, owner: 'o', name: 'stale', + defaultBranch: 'main', tokenEnc: 'x', permission: 'admin', + repoRole: 'developer', goal: 'keep the parser honest', autoIterate: true, intervalMinutes: 60, + }) + store.createWorkItem({ + id: 'work_stale', repoId: repo.id, userId: user.id, title: 'fix parser', + issueUrl: 'https://github.com/o/stale/issues/4', state: 'triaged', + }) + jobs.carryOverReaderOverride = () => fakeArtifactReader('closed') + + await jobs.tick() + const created = store.listJobsForRepo(repo.id) + assert.equal(created.length, 1) + assert.equal(created[0]?.kind, 'iterate') + assert.equal(created[0]?.trigger, 'schedule') + assert.equal(created[0]?.work_item_id, null, 'the run is fresh, not owned by the stale row') + assert.match(created[0]?.goal ?? '', /keep the parser honest/) + assert.doesNotMatch(created[0]?.goal ?? '', /work_stale/) + assert.equal(store.getWorkItem('work_stale')?.state, 'closed') + assert.equal(store.listOpenWorkItems(repo.id).length, 0) + } finally { + store.close() + } +}) + +test('the scheduler still continues a live item through its work item id', async () => { + const config = testConfig() + const store = new Store(':memory:') + const jobs = new Jobs(store, config, { name: 'capture', async run() { return { status: 'running' } } }, new MemoryMailer()) + try { + const user = makeUser(store, { login: 'scheduler-live', status: 'approved' }) + const repo = store.createRepo({ + id: 'repo_scheduler_live', userId: user.id, owner: 'o', name: 'live', + defaultBranch: 'main', tokenEnc: 'x', permission: 'admin', + repoRole: 'developer', goal: 'fix bugs', autoIterate: true, intervalMinutes: 60, + }) + store.createWorkItem({ + id: 'work_live', repoId: repo.id, userId: user.id, title: 'fix parser', + issueUrl: 'https://github.com/o/live/issues/4', state: 'triaged', + }) + jobs.carryOverReaderOverride = () => fakeArtifactReader('open') + + await jobs.tick() + const created = store.listJobsForRepo(repo.id) + assert.equal(created.length, 1) + assert.equal(created[0]?.work_item_id, 'work_live') + assert.equal(store.getWorkItem('work_live')?.state, 'in_progress') + } finally { + store.close() + } +}) + test('a discover callback cannot open a branch or pull request', async () => { const config = testConfig() const store = new Store(':memory:') @@ -2754,23 +2896,23 @@ test('the scheduler idles with no work and starts once a work item appears', asy } }) -test('an open work item starts after the short work cooldown, not the legacy interval', () => { +test('an open work item starts after the short work cooldown, not the legacy interval', async () => { const item = workItem() - const justDone = decideNextJob({ + const justDone = await decideNextJob({ repo: repo(), now, lastRunAt: iso(-9 * 60_000), intervalMinutes: 15, lastDurationMs: 0, failureStreak: 0, openWork: [item], reviewerEnabled: false, reviewDue: true, }) assert.equal(justDone.kind, 'none') - const ready = decideNextJob({ + const ready = await decideNextJob({ repo: repo(), now, lastRunAt: iso(-16 * 60_000), intervalMinutes: 15, lastDurationMs: 0, failureStreak: 0, openWork: [item], reviewerEnabled: false, reviewDue: true, }) assert.equal(ready.kind, 'followup') }) -test('a due maintainer continues the open PR when the review interval is not due', () => { +test('a due maintainer continues the open PR when the review interval is not due', async () => { const pr = workItem({ pr_url: 'https://x/pr/3', state: 'pr_ready' }) - const next = decideNextJob({ + const next = await decideNextJob({ repo: repo({ repo_role: 'maintainer' }), now, lastRunAt: iso(-2 * 3_600_000), intervalMinutes: 60, lastDurationMs: 0, failureStreak: 0, openWork: [pr], reviewerEnabled: true, reviewDue: false,