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/791-carry-over-items.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The job page lists every carry-over item it dropped — each with its link and whether it is a closed issue or a merged pull request — and every line it could not verify, naming the failed read or the spent read budget, instead of only the counts in the header. A job whose goal happens to contain a carry-over line no longer prints counts nobody computed.
103 changes: 84 additions & 19 deletions src/jobs/continuity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,20 @@ export interface CarryOverReport {
droppedLinks: string[]
unverifiedLinks: string[]
observations: CarryOverObservation[]
/** #791: one row per dropped or unverified item, so the job page lists what
* the header counts instead of recounting it. `reason` is null only on a
* value written before this field existed. */
items: CarryOverItem[]
}

/** Why one item is not carried: the read answered `closed`/`merged`, or the
* read failed, or the run's read ceiling was already spent. */
export type CarryOverItemReason = 'closed' | 'merged' | 'failed read' | 'budget spent'

export interface CarryOverItem {
link: string
kind: 'dropped' | 'unverified'
reason: CarryOverItemReason | null
}

//: #788: the state a read returned, when it returned one. `observedAt` is null
Expand All @@ -135,14 +149,21 @@ export interface CarryOverBlock {
report: CarryOverReport
}

//: The header line a block states, or '' when the goal carries no block. The job
//: page reads this back from the stored goal instead of counting the text again,
//: so the page and the prompt cannot spell the same counts two ways.
export function carryOverHeaderFromGoal(goal: string): string {
for (const line of goal.split('\n')) {
if (line.startsWith('carry-over: ')) return line.trim()
}
return ''
//: The block's first line. The generated block is appended after the owner's own
//: goal, so the last occurrence is the one this deployment wrote; a marker typed
//: into the goal cannot move it.
export const CARRY_OVER_MARKER = '[Carry-over - auto-generated state, not a new goal]'

//: The header line a block states, or '' when the goal carries no block. It is
//: read from the marker line and the line after it, never from a `carry-over:`
//: line the owner typed into the goal, so the page cannot print counts nobody
//: computed (#791).
export function carryOverHeaderFromBlock(goal: string): string {
const lines = goal.split('\n')
const marker = lines.lastIndexOf(CARRY_OVER_MARKER)
if (marker === -1) return ''
const header = (lines[marker + 1] ?? '').trim()
return header.startsWith('carry-over: ') ? header : ''
}

//: The one spelling of the counts, so the goal and the stored report cannot
Expand All @@ -161,16 +182,43 @@ export function parseCarryOverReport(value: string | null): CarryOverReport | nu
const parsed = JSON.parse(value) as Partial<CarryOverReport>
if (typeof parsed.used !== 'number' || typeof parsed.dropped !== 'number' || typeof parsed.reads !== 'number') return null
const observations = Array.isArray(parsed.observations) ? parsed.observations : []
const kept = observations.filter((entry): entry is CarryOverObservation =>
typeof entry?.link === 'string'
&& ['open', 'closed', 'merged', 'unverified'].includes(entry.state))
const droppedLinks = Array.isArray(parsed.droppedLinks)
? parsed.droppedLinks.filter((entry): entry is string => typeof entry === 'string')
: []
const unverifiedLinks = Array.isArray(parsed.unverifiedLinks)
? parsed.unverifiedLinks.filter((entry): entry is string => typeof entry === 'string')
: []
const storedItems = Array.isArray(parsed.items)
? parsed.items.filter((entry): entry is CarryOverItem =>
typeof entry?.link === 'string'
&& (entry.kind === 'dropped' || entry.kind === 'unverified')
&& (entry.reason === null || ['closed', 'merged', 'failed read', 'budget spent'].includes(entry.reason as string)))
: []
// #791: a value written before this field still lists what it dropped, from
// the links and observations it already stored. A reason it never recorded
// stays null so the page can say "not recorded" instead of naming one.
const observedState = new Map(kept.map((entry) => [entry.link, entry.state]))
const items: CarryOverItem[] = storedItems.length > 0
? storedItems
: [
...droppedLinks.map((link): CarryOverItem => {
const state = observedState.get(link)
return { link, kind: 'dropped', reason: state === 'closed' || state === 'merged' ? state : null }
}),
...unverifiedLinks.map((link): CarryOverItem => ({ link, kind: 'unverified', reason: null })),
]
return {
used: parsed.used,
dropped: parsed.dropped,
unverified: typeof parsed.unverified === 'number' ? parsed.unverified : 0,
reads: parsed.reads,
droppedLinks: Array.isArray(parsed.droppedLinks) ? parsed.droppedLinks : [],
unverifiedLinks: Array.isArray(parsed.unverifiedLinks) ? parsed.unverifiedLinks : [],
observations: observations.filter((entry): entry is CarryOverObservation =>
typeof entry?.link === 'string'
&& ['open', 'closed', 'merged', 'unverified'].includes(entry.state)),
droppedLinks,
unverifiedLinks,
observations: kept,
items,
}
} catch {
return null
Expand Down Expand Up @@ -213,7 +261,10 @@ export async function buildCarryOver(input: {
const reader = input.read
const clock = input.now ?? (() => new Date().toISOString())
const states = new Map<string, CarryOverLinkState | 'unverified'>()
const report: CarryOverReport = { used: 0, dropped: 0, unverified: 0, reads: 0, droppedLinks: [], unverifiedLinks: [], observations: [] }
//: #791: a failed read and a spent read ceiling are different answers, so the
//: item keeps which one it was instead of the page guessing.
const unverifiedReasons = new Map<string, 'failed read' | 'budget spent'>()
const report: CarryOverReport = { used: 0, dropped: 0, unverified: 0, reads: 0, droppedLinks: [], unverifiedLinks: [], observations: [], items: [] }

const observationOf = (link: string | null): CarryOverObservation | undefined =>
link === null ? undefined : report.observations.find((entry) => entry.link === link)
Expand All @@ -229,6 +280,7 @@ export async function buildCarryOver(input: {
// The ceiling is a bound on a job's GitHub budget, not a reason to drop:
// an item nobody had budget to read is unverified, and it stays.
state = 'unverified'
unverifiedReasons.set(link, 'budget spent')
} else {
report.reads += 1
try {
Expand All @@ -238,6 +290,7 @@ export async function buildCarryOver(input: {
observedAt = clock()
} catch {
state = 'unverified'
unverifiedReasons.set(link, 'failed read')
}
}
states.set(link, state)
Expand All @@ -254,12 +307,18 @@ export async function buildCarryOver(input: {
const state = await stateOf(link)
if (state === 'closed' || state === 'merged') {
report.dropped += 1
if (link !== null && !report.droppedLinks.includes(link)) report.droppedLinks.push(link)
if (link !== null && !report.droppedLinks.includes(link)) {
report.droppedLinks.push(link)
report.items.push({ link, kind: 'dropped', reason: state })
}
continue
}
if (state === 'unverified') {
report.unverified += 1
if (link !== null && !report.unverifiedLinks.includes(link)) report.unverifiedLinks.push(link)
if (link !== null && !report.unverifiedLinks.includes(link)) {
report.unverifiedLinks.push(link)
report.items.push({ link, kind: 'unverified', reason: unverifiedReasons.get(link) ?? null })
}
}
report.used += 1
const summary = redact(digest?.summary ?? job.summary ?? job.error ?? '').replace(/\s+/g, ' ').slice(0, 220)
Expand All @@ -277,12 +336,18 @@ export async function buildCarryOver(input: {
const state = await stateOf(link)
if (state === 'closed' || state === 'merged') {
report.dropped += 1
if (link !== null && !report.droppedLinks.includes(link)) report.droppedLinks.push(link)
if (link !== null && !report.droppedLinks.includes(link)) {
report.droppedLinks.push(link)
report.items.push({ link, kind: 'dropped', reason: state })
}
continue
}
if (state === 'unverified') {
report.unverified += 1
if (link !== null && !report.unverifiedLinks.includes(link)) report.unverifiedLinks.push(link)
if (link !== null && !report.unverifiedLinks.includes(link)) {
report.unverifiedLinks.push(link)
report.items.push({ link, kind: 'unverified', reason: unverifiedReasons.get(link) ?? null })
}
}
report.used += 1
workLines.push(
Expand All @@ -293,7 +358,7 @@ export async function buildCarryOver(input: {
)
}

const lines: string[] = ['[Carry-over - auto-generated state, not a new goal]']
const lines: string[] = [CARRY_OVER_MARKER]
if (reader !== undefined) lines.push(carryOverHeaderText(report))
lines.push(
'Continue the open work item when one exists; do not create a duplicate issue/PR.',
Expand Down
21 changes: 19 additions & 2 deletions src/views/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import type { PreviewResult } from '../schedules/preview.js'
import { intervalFieldText, type ScheduleConsoleState } from '../schedules/console.js'
import { contractOf, resolveWorkType, workTypeLabel, workTypePurpose } from '../lib/workTypes.js'
import { parseTriageJson, triageSummaryLine } from '../lib/triage.js'
import { MIN_DISCOVERY_INTERVAL_MINUTES, carryOverHeaderFromGoal, carryOverHeaderText, parseCarryOverReport } from '../jobs/continuity.js'
import { MIN_DISCOVERY_INTERVAL_MINUTES, carryOverHeaderFromBlock, carryOverHeaderText, parseCarryOverReport } from '../jobs/continuity.js'
import { parseModelRun } from '../lib/modelRun.js'
import { mergeReadiness, parsePathGuard, parseVerification } from '../lib/verification.js'
import { reviewMergeCard, reviewMergeState, type PullTip } from './reviewMerge.js'
Expand Down Expand Up @@ -3372,11 +3372,27 @@ export function jobPage(input: {
const carryOverHeader = job.continuity_used !== 1
? ''
: carryOverReport === null
? carryOverHeaderFromGoal(job.goal)
? carryOverHeaderFromBlock(job.goal)
: carryOverHeaderText(carryOverReport)
const carryOverLine = carryOverHeader === ''
? ''
: `<p class="fine" data-carry-over>${escapeHtml(carryOverHeader)}</p>`
// #791: the header counts, the rows name. Each dropped item carries its link
// and why it went, each unverified one the class of the failure, all read from
// the stored report so the list cannot spell a count the header does not.
const carryOverItems = carryOverReport === null || carryOverHeader === '' ? [] : carryOverReport.items
const carryOverItemRows = carryOverItems.length === 0
? ''
: `<div class="list" data-carry-over-items>${carryOverItems.map((entry) => {
const reason = entry.reason === 'closed' ? t('已关闭的 issue', 'Closed issue')
: entry.reason === 'merged' ? t('已合并的 PR', 'Merged pull request')
: entry.reason === 'failed read' ? t('读取失败', 'Read failed')
: entry.reason === 'budget spent' ? t('读取额度已用完', 'Read budget spent')
: entry.kind === 'dropped'
? t('已丢弃(原因未记录)', 'Dropped (reason not recorded)')
: t('未验证(原因未记录)', 'Unverified (reason not recorded)')
return `<div class="list-row" data-carry-over-item data-kind="${escapeHtml(entry.kind)}" data-reason="${escapeHtml(entry.reason ?? '')}"><div class="list-main"><strong>${escapeHtml(reason)}</strong><small><code>${escapeHtml(entry.link)}</code></small></div></div>`
}).join('')}</div>`
// Nothing to attach an observation to when the page shows no carry-over line
// at all (a pre-#365 row, or a goal that lost the block), so nothing is claimed.
const carryOverObservations = job.continuity_used !== 1 || carryOverHeader === ''
Expand Down Expand Up @@ -3665,6 +3681,7 @@ export function jobPage(input: {
${untrustedLine}
${providerLine}
${carryOverLine}
${carryOverItemRows}
${carryOverObservations}
${job.checkpoint === null || job.checkpoint === '' ? '' : `<p class="fine"><strong>${escapeHtml(t('断点', 'Checkpoint'))}:</strong> ${escapeHtml(job.checkpoint)}</p>`}
<p class="fine">${jobLinks(job)}</p>
Expand Down
116 changes: 114 additions & 2 deletions test/continuity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { tmpdir } from 'node:os'
import { join } from 'node:path'
import type { JobRow, WorkItemRow } from '../src/db.js'
import { Store } from '../src/db.js'
import { buildCarryOver, carryOverHeaderFromGoal, CARRY_OVER_MAX_READS, parseCarryOverLink, type CarryOverLinkState } from '../src/jobs/continuity.js'
import { buildCarryOver, carryOverHeaderFromBlock, CARRY_OVER_MAX_READS, parseCarryOverLink, type CarryOverLinkState } from '../src/jobs/continuity.js'
import { Jobs } from '../src/jobs/service.js'
import type { RunnerContext } from '../src/jobs/runner.js'
import { encryptSecret } from '../src/lib/secrets.js'
Expand Down Expand Up @@ -164,7 +164,7 @@ test('a link is parsed only when it is one issue or pull request URL', () => {

test('with no reader the block claims no counts at all', async () => {
const block = await buildCarryOver({ recent: [job({ id: 'job_a', continuity_json: digest({ issue_url: 'https://github.com/octo/demo/issues/9' }) })] })
assert.equal(carryOverHeaderFromGoal(block.text), '')
assert.equal(carryOverHeaderFromBlock(block.text), '')
assert.match(block.text, /not a new goal/)
})

Expand Down Expand Up @@ -194,6 +194,118 @@ test('the job page shows the carry-over line only for a job that carried one', (
}
})

// #791: the page lists what the block dropped instead of only counting it. The
// rows come from the stored report — the value the prompt's own header counts —
// so a maintainer can see which line went and why, and a `carry-over:` line the
// owner typed into the goal can no longer supply the header.
const CLOSED_ISSUE = 'https://github.com/octo/demo/issues/11'
const MERGED_PR = 'https://github.com/octo/demo/pull/12'
const FAILED_LINK = 'https://github.com/octo/demo/issues/13'

function pageReport(overrides: Partial<Record<string, unknown>> = {}): string {
return JSON.stringify({
used: 1, dropped: 2, unverified: 0, reads: 3,
droppedLinks: [CLOSED_ISSUE, MERGED_PR], unverifiedLinks: [],
observations: [
{ link: CLOSED_ISSUE, state: 'closed', observedAt: OBSERVED_AT },
{ link: MERGED_PR, state: 'merged', observedAt: OBSERVED_AT },
],
items: [
{ link: CLOSED_ISSUE, kind: 'dropped', reason: 'closed' },
{ link: MERGED_PR, kind: 'dropped', reason: 'merged' },
],
...overrides,
})
}

// The row names its link: the code element is read out and compared exactly, so
// the assertion says the URL *is* the row's link instead of that the URL appears
// somewhere in the page (CodeQL's incomplete-url-substring check).
function rowLink(row: string): string | null {
return /<code>([^<]*)<\/code>/.exec(row)?.[1] ?? null
}

function jobPageFor(jobOverrides: Partial<JobRow>): string {
const config = testConfig()
const store = new Store(':memory:')
try {
const user = makeUser(store)
const repo = store.createRepo({
id: 'repo_items', userId: user.id, owner: 'octo', name: 'demo', defaultBranch: 'main',
tokenEnc: 'x', permission: 'admin', repoRole: 'maintainer', goal: 'fix bugs',
autoIterate: false, intervalMinutes: 1440,
})
const viewer = { user: store.userById(user.id)!, csrfToken: 'csrf' }
return jobPage({ config, locale: 'en', viewer, repo: store.getRepo(repo.id)!, job: job(jobOverrides), events: [] })
} finally {
store.close()
}
}

test('the job page lists each dropped item with its link and reason from the stored report', () => {
const html = jobPageFor({ id: 'job_items', goal: 'fix bugs', continuity_used: 1, carry_over_json: pageReport() })
// It does not stop at the count line.
assert.match(html, /data-carry-over>carry-over: 1 used, 2 dropped \(closed\/merged\)</)
const rows = html.split('data-carry-over-item data-kind').slice(1)
assert.equal(rows.length, 2, 'two dropped items render two rows, not a single count')
assert.equal(rowLink(rows[0]!), CLOSED_ISSUE, 'the closed issue row names its link')
assert.match(rows[0]!, /Closed issue/, 'the closed issue row names its reason')
assert.equal(rowLink(rows[1]!), MERGED_PR, 'the merged pull request row names its link')
assert.match(rows[1]!, /Merged pull request/, 'the merged pull request row names its reason')
})

test('the page names the class of an unverified item it could not read', () => {
const html = jobPageFor({
id: 'job_unverified',
goal: 'fix bugs',
continuity_used: 1,
carry_over_json: pageReport({
dropped: 0, unverified: 1, droppedLinks: [], unverifiedLinks: [FAILED_LINK],
observations: [{ link: FAILED_LINK, state: 'unverified', observedAt: null }],
items: [{ link: FAILED_LINK, kind: 'unverified', reason: 'failed read' }],
}),
})
const rows = html.split('data-carry-over-item data-kind').slice(1)
assert.equal(rows.length, 1, 'the unverified item is listed, not only counted')
assert.equal(rowLink(rows[0]!), FAILED_LINK, 'the unverified row names its link')
assert.match(rows[0]!, /Read failed/, 'the unverified row names the class of the failure')
})

test('a report written before the item list still lists what it dropped', () => {
const html = jobPageFor({ id: 'job_old_report', goal: 'fix bugs', continuity_used: 1, carry_over_json: pageReport({ items: [] }) })
const rows = html.split('data-carry-over-item data-kind').slice(1)
assert.equal(rows.length, 2, 'the stored links and observations still render rows')
assert.equal(rowLink(rows[0]!), CLOSED_ISSUE, 'the old row names its link')
assert.match(rows[0]!, /Closed issue/, 'the observation supplies the recorded reason')
assert.match(rows[1]!, /Merged pull request/, 'a merged pull request keeps its own reason')
})

test('the header comes from the generated marker line, never from the owner-written goal', () => {
const typed = [
'carry-over: 0 used, 0 dropped (closed/merged)',
'fix the bug',
'',
'[Carry-over - auto-generated state, not a new goal]',
'carry-over: 3 used, 1 dropped (closed/merged)',
'Open work:',
'- work_1 state=triaged',
].join('\n')
const html = jobPageFor({ id: 'job_typed_header', goal: typed, continuity_used: 1 })
assert.match(html, /data-carry-over>carry-over: 3 used, 1 dropped \(closed\/merged\)</)
assert.doesNotMatch(html, /data-carry-over>carry-over: 0 used/)
})

test('a job that carried nothing renders no carry-over section at all', () => {
const html = jobPageFor({
id: 'job_pre_carry',
goal: 'carry-over: 0 used, 0 dropped (closed/merged)',
continuity_used: 0,
carry_over_json: pageReport(),
})
assert.doesNotMatch(html, /data-carry-over/)
assert.doesNotMatch(html, /data-carry-over-item/)
})

// #788: an observation is a fact about a moment. These cases pin the moment, so
// a line can state what was seen and when, and the page can say plainly when
// nothing was.
Expand Down
Loading