Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/789-live-artifact-decision.changed.md
Original file line number Diff line number Diff line change
@@ -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.
11 changes: 11 additions & 0 deletions docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
29 changes: 28 additions & 1 deletion src/jobs/continuity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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<NextJob> {
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))
Expand All @@ -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
Expand Down
33 changes: 25 additions & 8 deletions src/jobs/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<EnqueueResult> {
// #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
Expand Down Expand Up @@ -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 ?? ''
Expand Down Expand Up @@ -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,
Expand All @@ -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.
Expand Down Expand Up @@ -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',
Expand Down
8 changes: 4 additions & 4 deletions test/automationHealth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,21 @@ function state(overrides: Partial<SchedulerState> = {}): 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')
})
Expand Down
Loading
Loading