diff --git a/changelog.d/366-temporal-delta.added.md b/changelog.d/366-temporal-delta.added.md new file mode 100644 index 00000000..dc992fb5 --- /dev/null +++ b/changelog.d/366-temporal-delta.added.md @@ -0,0 +1 @@ +An iterate job's goal now carries one line of temporal context — `Since 4f2c1a9: 3 commits ahead, 2 merged PRs (#812, #815)` — built from the previous finished run's base SHA, and the job page shows the same line. A first run says `Since: no previous run`, and a compare that failed or a page walk that hit its cap is marked `partial` instead of reporting a smaller number. diff --git a/docs/architecture.md b/docs/architecture.md index ad24b6a7..6e5569fc 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -673,8 +673,8 @@ recorded". `scripts/restore-drill.mjs --status-file` is the writer. terminal state (`merged` or `closed`) is left exactly as it is: the expiry is still recorded in the audit row and in the `plan_expired:` settings key, but no "已过期关闭" notice is sent for an item the pass did not end (#408). - Iterate jobs preflight the target branch, record the base SHA - and warn when the fork is behind. An open work item changes what the next + Iterate jobs preflight the target branch, record the base SHA, + warn when the fork is behind, and add the temporal delta line described below. An open work item changes what the next scheduled run continues; it never blocks a due repository (#219). Work-item states do not gate the scheduler; see "Scheduled work, continuity and work items" below. @@ -1326,7 +1326,29 @@ request — so a closed issue or a merged pull request is dropped from the block and counted instead of being carried into the next run. The block states what it did in its header, `carry-over: 1 used, 1 dropped (closed/merged)`, with an `, N unverified` segment when a read failed; a failed read keeps its line rather -than dropping it silently, and the job page shows the same header line. Each finished run is classified as `pr`, `issue`, `review`, `zero`, +than dropping it silently, and the job page shows the same header line. + +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: +`compareCommits()`, `getCommitMeta()` for that base's `committedAt`, and then +`listMergedPullRequestsSince()` for that window. The base comes from +`store.latestFinishedBaseSha()`, one indexed question rather than a page of the +newest jobs, so a repository whose newest rows are all unfinished is not read as +a first run. The line names the short +previous base, the commit count and the merged pull requests, for example +`Since 4f2c1a9: 3 commits ahead, 2 merged PRs (#812, #815)`; a first run says +`Since: no previous run` and makes neither read. A failed compare prints +`delta unavailable (partial)` and a capped page walk prints `merged PRs partial` +— never the smaller number that happened to fit — because a delta that +understates the change is worse than one that admits it is incomplete. The +result is stored in `jobs.temporal_json` (`sinceSha`, `status`, `aheadBy`, +`mergedPrs`, `complete`), the additive column that lets a later slice compare two +deltas, and the job page shows the same line — and renders nothing for a row +whose fields are not the shape the summariser reads. The line is placed before +the 4000-character cap is applied to the goal body, so a long goal cannot squeeze +it out. Nothing reads the delta to make a decision yet; slice 1 only records and +displays it. Each finished run is classified as `pr`, `issue`, `review`, `zero`, `meta` or `failed`. A meta issue (`存量/盘点/清账/reconcile/inventory/backlog audit`) is closed on the tracking board and notifies the owner instead of counting as a productive run. Two diff --git a/src/db.ts b/src/db.ts index ea19cf7b..c15b530d 100644 --- a/src/db.ts +++ b/src/db.ts @@ -427,6 +427,9 @@ export interface JobRow { plan_allow_paths: string | null plan_deny_paths: string | null base_sha: string | null + // #366: one run's delta against the previous run's base, as JSON. Additive; + // NULL for every run that predates it and for a first run on a repository. + temporal_json: string | null callback_fingerprint: string | null path_guard_json: string | null verification_json: string | null @@ -1267,6 +1270,7 @@ CREATE TABLE IF NOT EXISTS jobs ( plan_allow_paths TEXT, plan_deny_paths TEXT, base_sha TEXT, + temporal_json TEXT, callback_fingerprint TEXT, path_guard_json TEXT, verification_json TEXT, @@ -2631,6 +2635,7 @@ export class Store { if (!jobColumns.has('plan_allow_paths')) this.run('ALTER TABLE jobs ADD COLUMN plan_allow_paths TEXT') if (!jobColumns.has('plan_deny_paths')) this.run('ALTER TABLE jobs ADD COLUMN plan_deny_paths TEXT') if (!jobColumns.has('base_sha')) this.run('ALTER TABLE jobs ADD COLUMN base_sha TEXT') + if (!jobColumns.has('temporal_json')) this.run('ALTER TABLE jobs ADD COLUMN temporal_json TEXT') if (!jobColumns.has('callback_fingerprint')) this.run('ALTER TABLE jobs ADD COLUMN callback_fingerprint TEXT') if (!jobColumns.has('path_guard_json')) this.run('ALTER TABLE jobs ADD COLUMN path_guard_json TEXT') if (!jobColumns.has('verification_json')) this.run('ALTER TABLE jobs ADD COLUMN verification_json TEXT') @@ -5615,6 +5620,17 @@ export class Store { return this.all('SELECT * FROM jobs WHERE repo_id = ? ORDER BY created_at DESC, rowid DESC LIMIT ?', repoId, limit) } + // #366: the base SHA of the newest finished job that recorded one. A window + // over `listJobsForRepo` would answer "no previous run" on a busy repository + // whose newest twenty rows are unfinished, so the question is asked directly. + latestFinishedBaseSha(repoId: string): string | null { + const row = this.get<{ base_sha: string | null }>( + "SELECT base_sha FROM jobs WHERE repo_id = ? AND finished_at IS NOT NULL AND base_sha IS NOT NULL AND base_sha != '' ORDER BY finished_at DESC, rowid DESC LIMIT 1", + repoId, + ) + return row?.base_sha ?? null + } + // #360 review: a work item's job is looked up by the item, not through a window // of the repository's newest jobs. The window said "no job is attached" for a // work item whose job was older than the limit — a false statement, and a @@ -5833,7 +5849,7 @@ export class Store { } - updateJob(id: string, patch: Partial>): void { + updateJob(id: string, patch: Partial>): void { const entries = Object.entries(redactJobText(patch as Record)).filter(([, value]) => value !== undefined) if (entries.length === 0) return const sets: string[] = [] diff --git a/src/jobs/service.ts b/src/jobs/service.ts index 8a7eaebe..d213ffb8 100644 --- a/src/jobs/service.ts +++ b/src/jobs/service.ts @@ -50,6 +50,7 @@ import { GithubClient, GithubError, prepareRepo, prepareRepoWithInstallation, re import { GithubApp } from '../repos/app.js' import { parseIssueUrl, parsePrUrl } from './prUrl.js' import { buildCarryOver, buildRunDigest, decideNextJob, failureStreak, lastDurationMs, MIN_DISCOVERY_INTERVAL_MINUTES, parseCarryOverLink, type CarryOverReader } from './continuity.js' +import { isTemporalStatus, summariseDelta, type TemporalDelta, type TemporalStatus } from '../lib/temporal.js' import { planTick, scheduleFromRow, type TickPlan } from '../schedules/tick.js' import { utcDayEnd, utcDayStart } from '../schedules/budget.js' import type { ScheduleRow } from '../db.js' @@ -896,7 +897,7 @@ export class Jobs { } } - private async preflight(repo: RepoRow, baseBranch: string, trigger: string): Promise<{ ok: boolean; baseSha?: string; warning?: string; error?: string; code?: EnqueueRefusalCode; detail?: string }> { + private async preflight(repo: RepoRow, baseBranch: string, trigger: string): Promise<{ ok: boolean; baseSha?: string; warning?: string; temporal?: TemporalDelta; error?: string; code?: EnqueueRefusalCode; detail?: string }> { if (this.runner.name !== 'flybot') return { ok: true } try { const token = await repoGithubToken(repo, this.config, this.githubApp, this.store) @@ -918,7 +919,8 @@ export class Jobs { else if (forkBranch.sha !== branch.sha) warning = 'fork is behind upstream' } } - return { ok: true, baseSha: branch.sha, warning } + const temporal = await this.temporalDelta(client, repo.id, upstreamOwner, upstreamName, branch.sha) + return { ok: true, baseSha: branch.sha, warning, temporal } } catch (error) { if (error instanceof GithubError && (error.status === 401 || error.status === 403) && !/rate limit/i.test(error.message)) { this.markCredentialInvalid(repo, error) @@ -928,6 +930,38 @@ export class Jobs { } } + // #366 slice 1: what changed between the previous finished run's base and this + // one. Two bounded reads, and every way they can end short is `partial` rather + // than a number the goal would present as complete. + private async temporalDelta(client: GithubClient, repoId: string, owner: string, name: string, baseSha: string): Promise { + // The newest finished job that recorded a base, asked as one query rather + // than as a page of the newest rows: on a busy repository the newest twenty + // can all be unfinished, and that must not read as a first run. A rerun at + // the same base is still a previous run and says `no new commits`, which is + // a fact worth having; only a repository with no such job has none. + const sinceSha = this.store.latestFinishedBaseSha(repoId) + if (sinceSha === null) return { sinceSha: null, status: 'ahead', aheadBy: 0, mergedPrs: [], complete: true } + let status: TemporalStatus = 'unknown' + let aheadBy = 0 + try { + const compare = await client.compareCommits(owner, name, sinceSha, baseSha) + status = isTemporalStatus(compare.status) ? compare.status : 'unknown' + aheadBy = status === 'unknown' ? 0 : Math.max(0, compare.aheadBy) + } catch (error) { + log.warn('temporal compare failed', { repo: repoId, error: String(error) }) + return { sinceSha, status: 'unknown', aheadBy: 0, mergedPrs: [], complete: false } + } + try { + const previous = await client.getCommitMeta(owner, name, sinceSha) + if (previous === null) return { sinceSha, status, aheadBy, mergedPrs: [], complete: false } + const merged = await client.listMergedPullRequestsSince(owner, name, previous.committedAt) + return { sinceSha, status, aheadBy, mergedPrs: merged.pulls.map((pull) => pull.number), complete: merged.complete } + } catch (error) { + log.warn('temporal merged-PR read failed', { repo: repoId, error: String(error) }) + return { sinceSha, status, aheadBy, mergedPrs: [], complete: false } + } + } + private async checkCredential(repo: RepoRow): Promise<{ ok: boolean; code?: EnqueueRefusalCode; error?: string }> { const cached = this.credentialCache.get(repo.id) if (cached !== undefined && Date.now() - cached.at < 10 * 60_000) return { ok: true } @@ -1074,11 +1108,13 @@ export class Jobs { } let preflightSha: string | null = null let preflightWarning = '' + let temporal: TemporalDelta | undefined if (kind === 'iterate') { const preflight = await this.preflight(repo, baseBranch, trigger) if (!preflight.ok) return { ok: false, error: preflight.error, code: preflight.code, detail: preflight.detail } preflightSha = preflight.baseSha ?? null preflightWarning = preflight.warning ?? '' + temporal = preflight.temporal } const baseGoal = kind === 'review' ? DEFAULT_REVIEW_GOAL : kind === 'discover' ? DEFAULT_DISCOVER_GOAL : kind === 'triage' ? DEFAULT_TRIAGE_GOAL : kind === 'scope' ? DEFAULT_SCOPE_GOAL : kind === 'verify' ? DEFAULT_VERIFY_GOAL : (repo.goal !== '' ? repo.goal : DEFAULT_GOAL) const rawGoal = goalOverride ?? baseGoal @@ -1092,8 +1128,14 @@ export class Jobs { read: (this.carryOverReaderOverride ?? ((target: RepoRow) => this.carryOverReader(target)))(repo), })).text : '' - const goal = ((preflightWarning === '' ? rawGoal : `${rawGoal}\n\nPreflight: ${preflightWarning}. Sync the fork before creating the working branch.`) - + (carryOver === '' ? '' : `\n\n${carryOver}`)).slice(0, 4000) + // #366 slice 1: exactly one data line, and only when a previous run exists. + // The line is placed before the goal is capped: appending it after the cap + // would let a goal that is already near 4000 characters swallow it silently, + // so the body is what gets truncated and the data lines keep their room. + const temporalLine = temporal === undefined ? '' : summariseDelta(temporal) + const dataTail = (temporalLine === '' ? '' : `\n\n${temporalLine}`) + (carryOver === '' ? '' : `\n\n${carryOver}`) + const goalBody = preflightWarning === '' ? rawGoal : `${rawGoal}\n\nPreflight: ${preflightWarning}. Sync the fork before creating the working branch.` + const goal = `${goalBody.slice(0, Math.max(0, 4000 - dataTail.length))}${dataTail}`.slice(0, 4000) const continuityUsed = carryOver !== '' const jobId = `job_${randomToken(10)}` const callbackSecret = randomToken(24) @@ -1194,6 +1236,10 @@ export class Jobs { throw error } if (continuityUsed) this.store.updateJob(job.id, { continuity_used: 1 }) + if (temporal !== undefined) { + job.temporal_json = JSON.stringify(temporal) + this.store.updateJob(job.id, { temporal_json: job.temporal_json }) + } if (rules !== undefined) { this.store.saveJobContext({ jobId: job.id, diff --git a/src/lib/temporal.ts b/src/lib/temporal.ts new file mode 100644 index 00000000..3949bc28 --- /dev/null +++ b/src/lib/temporal.ts @@ -0,0 +1,68 @@ +// #366 slice 1: the temporal contract. A job's goal carries one line saying what +// happened between the previous run's base and this run's base. The wording is +// decided here, in a pure function, so the same inputs always produce the same +// line and the reads cannot quietly change it. +// +// The rule the line exists for: a read that failed or hit its page cap is +// reported as `partial`, never as the smaller number it managed to see. A job +// that is told "2 merged PRs" when the walk stopped at two pages would rather +// not be told anything than be told a number it can trust. + +export type TemporalStatus = 'ahead' | 'identical' | 'behind' | 'diverged' | 'unknown' + +export interface TemporalDelta { + // `base_sha` of the newest finished job for the same repository, `null` for a + // first run. Never the current job's own base. + sinceSha: string | null + // `compareCommits(sinceSha, baseSha).status`, or `unknown` when that read failed. + status: TemporalStatus + aheadBy: number + // Merged pull request numbers inside the window, deduplicated by the caller or + // here; `complete` is false when the page cap was reached or a read failed. + mergedPrs: number[] + complete: boolean +} + +const STATUSES: readonly TemporalStatus[] = ['ahead', 'identical', 'behind', 'diverged', 'unknown'] + +// GitHub returns `status: 'unknown'` for a compare it could not resolve, so the +// name alone does not prove the read worked; anything not in the list is treated +// the same way rather than guessed at. +export function isTemporalStatus(value: string): value is TemporalStatus { + return (STATUSES as readonly string[]).includes(value) +} + +const SHORT_SHA = 7 +const LISTED_PRS = 5 + +export function summariseSince(sinceSha: string | null): string { + return sinceSha === null ? 'Since' : `Since ${sinceSha.slice(0, SHORT_SHA)}` +} + +function plural(count: number, one: string, many: string): string { + return count === 1 ? one : many +} + +function describeCommits(delta: TemporalDelta): string { + if (delta.status === 'identical') return 'no new commits' + if (delta.status === 'behind') return 'base is behind the previous run' + if (delta.status === 'diverged') return 'diverged from the previous run' + return `${String(delta.aheadBy)} ${plural(delta.aheadBy, 'commit', 'commits')} ahead` +} + +function describePullRequests(delta: TemporalDelta): string { + if (!delta.complete) return 'merged PRs partial' + const numbers = [...new Set(delta.mergedPrs)].sort((a, b) => a - b) + const count = numbers.length + const head = `${String(count)} ${plural(count, 'merged PR', 'merged PRs')}` + if (count === 0) return head + const shown = numbers.slice(0, LISTED_PRS).map((number) => `#${String(number)}`) + const tail = count > LISTED_PRS ? `${shown.join(', ')}, …` : shown.join(', ') + return `${head} (${tail})` +} + +export function summariseDelta(delta: TemporalDelta): string { + if (delta.sinceSha === null) return 'Since: no previous run' + if (delta.status === 'unknown') return `${summariseSince(delta.sinceSha)}: delta unavailable (partial)` + return `${summariseSince(delta.sinceSha)}: ${describeCommits(delta)}, ${describePullRequests(delta)}` +} diff --git a/src/views/pages.ts b/src/views/pages.ts index 8f0bd5c7..49cf3042 100644 --- a/src/views/pages.ts +++ b/src/views/pages.ts @@ -17,6 +17,7 @@ import { escapeHtml, FLEDGE_GITHUB_URL, layout, money, quotaMoney, type PageNoti import { renderState, stateCell, type StateAction } from './states.js' import { redact } from '../lib/secrets.js' import { REVIEW_INSTRUCTION_LABEL, reviewInstructionFromGoal } from '../lib/followUpInstruction.js' +import { isTemporalStatus, summariseDelta, type TemporalDelta } from '../lib/temporal.js' import { firstRunOutcome, setupChecklist, type SetupState, type SetupStepKey } from './checklist.js' import { oauthConfigured, tokenLoginEnabled } from '../auth/login.js' import { allowedRepoRoles, REPO_ROLE_LABELS, type RepoRole } from '../lib/repoRole.js' @@ -3097,6 +3098,27 @@ function changeStoryLabel(t: Translate, key: ChangeStoryFact['key']): string { return t('改动规模', 'Diff') } +// #366 slice 1: the stored delta is data, so a row written by an older build or a +// hand-edited value renders nothing instead of failing the page. +function temporalLine(job: JobRow): string { + if (typeof job.temporal_json !== 'string' || job.temporal_json === '') return '' + try { + const delta = JSON.parse(job.temporal_json) as TemporalDelta + // A row can parse and still be unusable: the page only renders a delta whose + // every field is the shape the summariser reads, so a hand-edited or older + // row shows nothing instead of `undefined commits ahead`. + if (typeof delta !== 'object' || delta === null) return '' + if (delta.sinceSha !== null && typeof delta.sinceSha !== 'string') return '' + if (!isTemporalStatus(delta.status)) return '' + if (typeof delta.aheadBy !== 'number' || !Number.isFinite(delta.aheadBy)) return '' + if (!Array.isArray(delta.mergedPrs) || !delta.mergedPrs.every((number) => typeof number === 'number')) return '' + if (typeof delta.complete !== 'boolean') return '' + return summariseDelta(delta) + } catch { + return '' + } +} + export function jobPage(input: { config: Config locale: Locale @@ -3445,7 +3467,7 @@ export function jobPage(input: {
${escapeHtml(t('任务', 'JOB'))}

${escapeHtml(job.id)}

-

${escapeHtml(repo.upstream_owner || repo.owner)}/${escapeHtml(repo.upstream_name || repo.name)} · ${badge(t, job.status)} · ${escapeHtml(jobKindLabel(t, job))} · ${escapeHtml(triggerLabel(t, job.trigger))} · ${escapeHtml(t('目标分支', 'Base branch'))} ${escapeHtml(job.base_branch ?? (repo.target_branch !== '' ? repo.target_branch : (repo.upstream_default_branch || repo.default_branch)))}${job.base_sha === null ? '' : ` · ${escapeHtml(t('基于', 'Based on'))} ${escapeHtml(job.base_sha.slice(0, 12))}`}${job.last_callback_at === null ? '' : ` · ${escapeHtml(t('最后心跳', 'Last heartbeat'))} ${escapeHtml(humanTimeShort(job.last_callback_at))}`}

+

${escapeHtml(repo.upstream_owner || repo.owner)}/${escapeHtml(repo.upstream_name || repo.name)} · ${badge(t, job.status)} · ${escapeHtml(jobKindLabel(t, job))} · ${escapeHtml(triggerLabel(t, job.trigger))} · ${escapeHtml(t('目标分支', 'Base branch'))} ${escapeHtml(job.base_branch ?? (repo.target_branch !== '' ? repo.target_branch : (repo.upstream_default_branch || repo.default_branch)))}${job.base_sha === null ? '' : ` · ${escapeHtml(t('基于', 'Based on'))} ${escapeHtml(job.base_sha.slice(0, 12))}`}${job.last_callback_at === null ? '' : ` · ${escapeHtml(t('最后心跳', 'Last heartbeat'))} ${escapeHtml(humanTimeShort(job.last_callback_at))}`}${temporalLine(job) === '' ? '' : ` · ${escapeHtml(temporalLine(job))}`}

${escapeHtml(t('返回仓库', 'Back to repository'))} diff --git a/test/jobs.test.ts b/test/jobs.test.ts index af334b7a..80cb5772 100644 --- a/test/jobs.test.ts +++ b/test/jobs.test.ts @@ -3182,3 +3182,139 @@ test('one blocked tip keeps one audit row and one notification, and a moved head await github.close() } }) + +// #366 slice 1, the dispatcher half: the pure summariser is tested in +// test/temporal.test.ts, but the properties that matter at dispatch time are +// that the previous run's base is the one read, that the line reaches the goal, +// that the delta is stored, and that a first run reads nothing at all. +const TEMPORAL_PREVIOUS_SHA = '4f2c1a9e8d3b2a1c0f9e8d7c6b5a4f3e2d1c0b9a' + +function temporalServer(requests: string[]) { + return startFakeServer((request, response) => { + const path = request.url ?? '' + requests.push(path) + if (path.includes('/pulls?state=open')) return json(response, 200, []) + if (path.includes('/compare/')) return json(response, 200, { status: 'ahead', ahead_by: 3 }) + if (path.includes('/commits/')) return json(response, 200, { sha: TEMPORAL_PREVIOUS_SHA, commit: { committer: { date: '2026-11-10T00:00:00Z' } } }) + if (path.includes('/pulls?state=closed')) { + return json(response, 200, [ + { number: 815, html_url: 'https://github.com/o/temporal/pull/815', title: 'b', merged_at: '2026-11-10T02:00:00Z', updated_at: '2026-11-10T02:00:00Z' }, + { number: 812, html_url: 'https://github.com/o/temporal/pull/812', title: 'a', merged_at: '2026-11-10T01:00:00Z', updated_at: '2026-11-10T01:00:00Z' }, + ]) + } + if (path.includes('/branches/')) return json(response, 200, { name: 'main', commit: { sha: 'base-sha-now' } }) + if (path.includes('/contents/')) return json(response, 200, []) + return json(response, 404, { message: 'Not Found' }) + }) +} + +function temporalRepo(store: Store, userId: string, id: string, key: string): RepoRow { + return store.createRepo({ + id, + userId, + owner: 'o', + name: 'temporal', + defaultBranch: 'main', + tokenEnc: encryptSecret('gh-token', key), + permission: 'admin', + repoRole: 'maintainer', + goal: '', + autoIterate: false, + intervalMinutes: 1440, + }) +} + +test('an iterate job records the delta from the previous finished run, not a fresh read', async () => { + const requests: string[] = [] + const github = await temporalServer(requests) + const config = testConfig({ github: { ...testConfig().github, apiBase: github.url } }) + const store = new Store(':memory:') + const jobs = new Jobs(store, config, { name: 'flybot', async run() { return { status: 'running' } } }, new MemoryMailer()) + try { + const user = makeUser(store, { login: 'temporal-user', status: 'approved' }) + const repo = temporalRepo(store, user.id, 'repo_temporal_dispatch', config.tokenEncryptionKey) + const previous = store.createJob({ + id: 'job_temporal_prev', + repoId: repo.id, + userId: user.id, + trigger: 'manual', + goal: 'previous run', + callbackHash: 'h', + platformTokenHash: '', + baseSha: TEMPORAL_PREVIOUS_SHA, + createdAt: '2026-11-10T00:00:00.000Z', + }) + store.updateJob(previous.id, { status: 'succeeded', finished_at: '2026-11-10T00:05:00.000Z' }) + + const result = await jobs.enqueue(repo, 'manual', 'Fix it', 'iterate', {}) + assert.equal(result.ok, true, result.error ?? 'enqueue failed') + const job = store.getJob(result.job!.id)! + const delta = JSON.parse(job.temporal_json ?? 'null') as { sinceSha: string | null; status: string; aheadBy: number; mergedPrs: number[]; complete: boolean } + assert.equal(delta.sinceSha, TEMPORAL_PREVIOUS_SHA, 'the delta was not built from the previous run base') + assert.equal(delta.status, 'ahead') + assert.equal(delta.aheadBy, 3) + assert.deepEqual(delta.mergedPrs, [815, 812], 'the merged pull requests the walk returned were not stored') + assert.match(job.goal, /Since 4f2c1a9: 3 commits ahead, 2 merged PRs \(#812, #815\)/, 'the goal does not carry the delta line') + assert.equal(requests.filter((path) => path.includes('/compare/')).length, 1, 'the compare read is not made exactly once') + } finally { + store.close() + await github.close() + } +}) + +test('a first iterate job says there is no previous run and makes no compare call', async () => { + const requests: string[] = [] + const github = await temporalServer(requests) + const config = testConfig({ github: { ...testConfig().github, apiBase: github.url } }) + const store = new Store(':memory:') + const jobs = new Jobs(store, config, { name: 'flybot', async run() { return { status: 'running' } } }, new MemoryMailer()) + try { + const user = makeUser(store, { login: 'temporal-first', status: 'approved' }) + const repo = temporalRepo(store, user.id, 'repo_temporal_first', config.tokenEncryptionKey) + const result = await jobs.enqueue(repo, 'manual', 'Fix it', 'iterate', {}) + assert.equal(result.ok, true, result.error ?? 'enqueue failed') + const job = store.getJob(result.job!.id)! + assert.match(job.goal, /Since: no previous run/) + assert.deepEqual(JSON.parse(job.temporal_json ?? 'null'), { sinceSha: null, status: 'ahead', aheadBy: 0, mergedPrs: [], complete: true }) + assert.equal(requests.filter((path) => path.includes('/compare/')).length, 0, 'a first run compared something') + assert.equal(requests.filter((path) => path.includes('/commits/')).length, 0, 'a first run looked up a commit') + } finally { + store.close() + await github.close() + } +}) + +test('a goal near the length cap still carries the temporal line', async () => { + const requests: string[] = [] + const github = await temporalServer(requests) + const config = testConfig({ github: { ...testConfig().github, apiBase: github.url } }) + const store = new Store(':memory:') + const jobs = new Jobs(store, config, { name: 'flybot', async run() { return { status: 'running' } } }, new MemoryMailer()) + try { + const user = makeUser(store, { login: 'temporal-long', status: 'approved' }) + const repo = temporalRepo(store, user.id, 'repo_temporal_long', config.tokenEncryptionKey) + const previous = store.createJob({ + id: 'job_temporal_long_prev', + repoId: repo.id, + userId: user.id, + trigger: 'manual', + goal: 'previous run', + callbackHash: 'h', + platformTokenHash: '', + baseSha: TEMPORAL_PREVIOUS_SHA, + createdAt: '2026-11-10T00:00:00.000Z', + }) + store.updateJob(previous.id, { status: 'succeeded', finished_at: '2026-11-10T00:05:00.000Z' }) + + // A goal that would fill the whole budget: the data line is what has to + // survive the cap, because the caller cannot get it back any other way. + const result = await jobs.enqueue(repo, 'manual', 'x'.repeat(3990), 'iterate', {}) + assert.equal(result.ok, true, result.error ?? 'enqueue failed') + const job = store.getJob(result.job!.id)! + assert.ok(job.goal.length <= 4000, `goal is ${String(job.goal.length)} characters`) + assert.match(job.goal, /Since 4f2c1a9: 3 commits ahead, 2 merged PRs \(#812, #815\)/, 'the cap squeezed the delta line out') + } finally { + store.close() + await github.close() + } +}) diff --git a/test/temporal.test.ts b/test/temporal.test.ts new file mode 100644 index 00000000..96e0ab9b --- /dev/null +++ b/test/temporal.test.ts @@ -0,0 +1,181 @@ +import test from 'node:test' +import assert from 'node:assert/strict' +import type { JobRow } from '../src/db.js' +import { Store } from '../src/db.js' +import { summariseDelta, summariseSince, type TemporalDelta } from '../src/lib/temporal.js' +import { jobPage } from '../src/views/pages.js' +import { makeUser, testConfig } from './helpers.js' + +// #366 slice 1: one line of temporal context in the goal, built by a pure +// function so the wording is decided here and not by whatever the reads +// happened to return. The two rules the issue pins down are tested below: +// a first job says so and calls nothing, and a capped or failed read is marked +// `partial` instead of reporting the smaller number it did get. + +function delta(overrides: Partial = {}): TemporalDelta { + return { sinceSha: '4f2c1a9e8d3b2a1c0f9e8d7c6b5a4f3e2d1c0b9a', status: 'ahead', aheadBy: 3, mergedPrs: [], complete: true, ...overrides } +} + +test('a first job says there is no previous run', () => { + const line = summariseDelta(delta({ sinceSha: null, aheadBy: 0, mergedPrs: [] })) + assert.equal(line, 'Since: no previous run') +}) + +test('a delta names the short previous sha, the commit count and the merged PRs', () => { + const line = summariseDelta(delta({ mergedPrs: [812, 815] })) + assert.equal(line, 'Since 4f2c1a9: 3 commits ahead, 2 merged PRs (#812, #815)') +}) + +test('single commit and single pull request are written in the singular', () => { + assert.equal(summariseDelta(delta({ aheadBy: 1, mergedPrs: [7] })), 'Since 4f2c1a9: 1 commit ahead, 1 merged PR (#7)') +}) + +test('no merged pull requests says zero rather than dropping the clause', () => { + assert.equal(summariseDelta(delta({ aheadBy: 2, mergedPrs: [] })), 'Since 4f2c1a9: 2 commits ahead, 0 merged PRs') +}) + +// The central rule: a capped page walk or a failed read is reported as partial, +// never as the count that happened to fit in the pages we did read. +test('a capped merged-PR walk is marked partial and reports no count at all', () => { + const line = summariseDelta(delta({ mergedPrs: [801, 803], complete: false })) + assert.equal(line, 'Since 4f2c1a9: 3 commits ahead, merged PRs partial') + assert.doesNotMatch(line, /\d+ merged PRs/) + assert.doesNotMatch(line, /#/) +}) + +test('a failed compare is marked partial and does not claim the base is unchanged', () => { + const line = summariseDelta(delta({ status: 'unknown', aheadBy: 0, mergedPrs: [], complete: true })) + assert.equal(line, 'Since 4f2c1a9: delta unavailable (partial)') +}) + +test('an identical base says no new commits and still reports the merged PRs it saw', () => { + assert.equal(summariseDelta(delta({ status: 'identical', aheadBy: 0 })), 'Since 4f2c1a9: no new commits, 0 merged PRs') + assert.equal( + summariseDelta(delta({ status: 'identical', aheadBy: 0, mergedPrs: [900] })), + 'Since 4f2c1a9: no new commits, 1 merged PR (#900)', + ) +}) + +test('a base that moved backwards or diverged is named, not called ahead', () => { + assert.equal(summariseDelta(delta({ status: 'behind', aheadBy: 0 })), 'Since 4f2c1a9: base is behind the previous run, 0 merged PRs') + assert.equal( + summariseDelta(delta({ status: 'diverged', aheadBy: 0, mergedPrs: [1, 2] })), + 'Since 4f2c1a9: diverged from the previous run, 2 merged PRs (#1, #2)', + ) +}) + +test('pull request numbers are sorted, deduplicated and capped with an ellipsis', () => { + const line = summariseDelta(delta({ aheadBy: 9, mergedPrs: [815, 803, 801, 812, 803, 820, 826] })) + assert.equal(line, 'Since 4f2c1a9: 9 commits ahead, 6 merged PRs (#801, #803, #812, #815, #820, …)') +}) + +test('the line is one line and carries no newline', () => { + const line = summariseSince(delta({ mergedPrs: [1] }).sinceSha) + assert.equal(line, 'Since 4f2c1a9') + assert.doesNotMatch(summariseDelta(delta({ mergedPrs: [1, 2, 3] })), /\n/) +}) + +test('the delta survives a store round trip and a job without one reads as null', () => { + const store = new Store(':memory:') + try { + const user = makeUser(store) + const repo = store.createRepo({ + id: 'repo_temporal', userId: user.id, owner: 'octo', name: 'demo', defaultBranch: 'main', + tokenEnc: 'x', permission: 'admin', repoRole: 'maintainer', goal: 'fix bugs', + autoIterate: false, intervalMinutes: 1440, + }) + const job = store.createJob({ id: 'job_temporal', repoId: repo.id, userId: user.id, trigger: 'manual', goal: 'g', callbackHash: 'h', platformTokenHash: '' }) + assert.equal(job.temporal_json, null) + const delta = { sinceSha: '4f2c1a9e8d3b2a1c0f9e8d7c6b5a4f3e2d1c0b9a', status: 'ahead' as const, aheadBy: 3, mergedPrs: [812, 815], complete: true } + store.updateJob(job.id, { temporal_json: JSON.stringify(delta) }) + assert.deepEqual(JSON.parse(store.getJob(job.id)!.temporal_json!), delta) + } finally { + store.close() + } +}) + +test('the job page shows the line for a stored delta and nothing for a missing or broken one', () => { + const config = testConfig() + const store = new Store(':memory:') + try { + const user = makeUser(store) + const repo = store.createRepo({ + id: 'repo_temporal_page', userId: user.id, owner: 'octo', name: 'demo', defaultBranch: 'main', + tokenEnc: 'x', permission: 'admin', repoRole: 'maintainer', goal: 'fix bugs', + autoIterate: false, intervalMinutes: 1440, + }) + // One active job per repository is a partial unique index, so the previous + // fixture has to finish before the next one exists. + const make = (id: string, temporal: string | null): JobRow => { + for (const older of store.listJobsForRepo(repo.id, 10)) { + if (older.finished_at === null) store.updateJob(older.id, { status: 'succeeded', finished_at: new Date().toISOString() }) + } + const created = store.createJob({ id, repoId: repo.id, userId: user.id, trigger: 'manual', goal: 'fix bugs', callbackHash: 'h', platformTokenHash: '' }) + if (temporal !== null) store.updateJob(id, { temporal_json: temporal }) + return store.getJob(created.id)! + } + const viewer = { user: store.userById(user.id)!, csrfToken: 'csrf' } + const page = (job: JobRow): string => jobPage({ config, locale: 'en', viewer, repo: store.getRepo(repo.id)!, job, events: [] }) + const withDelta = page(make('job_t_page', JSON.stringify({ sinceSha: '4f2c1a9e8d3b2a1c0f9e8d7c6b5a4f3e2d1c0b9a', status: 'ahead', aheadBy: 3, mergedPrs: [812, 815], complete: true }))) + assert.match(withDelta, /data-temporal>Since 4f2c1a9: 3 commits ahead, 2 merged PRs \(#812, #815\) { + const store = new Store(':memory:') + try { + const user = makeUser(store) + const repo = store.createRepo({ + id: 'repo_temporal_query', userId: user.id, owner: 'octo', name: 'demo', defaultBranch: 'main', + tokenEnc: 'x', permission: 'admin', repoRole: 'maintainer', goal: 'fix bugs', + autoIterate: false, intervalMinutes: 1440, + }) + assert.equal(store.latestFinishedBaseSha(repo.id), null) + const older = store.createJob({ id: 'job_q_old', repoId: repo.id, userId: user.id, trigger: 'manual', goal: 'old', callbackHash: 'h', platformTokenHash: '', baseSha: 'a'.repeat(40), createdAt: '2026-11-01T00:00:00.000Z' }) + store.updateJob(older.id, { status: 'succeeded', finished_at: '2026-11-01T00:05:00.000Z' }) + const newer = store.createJob({ id: 'job_q_new', repoId: repo.id, userId: user.id, trigger: 'manual', goal: 'new', callbackHash: 'h', platformTokenHash: '', baseSha: 'b'.repeat(40), createdAt: '2026-11-02T00:00:00.000Z' }) + store.updateJob(newer.id, { status: 'succeeded', finished_at: '2026-11-02T00:05:00.000Z' }) + // A finished job with no recorded base is not an answer; the newest job that + // did record one is. + const unbased = store.createJob({ id: 'job_q_none', repoId: repo.id, userId: user.id, trigger: 'manual', goal: 'none', callbackHash: 'h', platformTokenHash: '', createdAt: '2026-11-03T00:00:00.000Z' }) + store.updateJob(unbased.id, { status: 'succeeded', finished_at: '2026-11-03T00:05:00.000Z' }) + assert.equal(store.latestFinishedBaseSha(repo.id), 'b'.repeat(40)) + } finally { + store.close() + } +}) + +test('a parsable but structurally wrong row renders no line at all', () => { + const config = testConfig() + const store = new Store(':memory:') + try { + const user = makeUser(store) + const repo = store.createRepo({ + id: 'repo_temporal_bad', 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' } + const render = (id: string, temporal: string): string => { + for (const older of store.listJobsForRepo(repo.id, 10)) { + if (older.finished_at === null) store.updateJob(older.id, { status: 'succeeded', finished_at: new Date().toISOString() }) + } + store.createJob({ id, repoId: repo.id, userId: user.id, trigger: 'manual', goal: 'g', callbackHash: 'h', platformTokenHash: '' }) + store.updateJob(id, { temporal_json: temporal }) + return jobPage({ config, locale: 'en', viewer, repo: store.getRepo(repo.id)!, job: store.getJob(id)!, events: [] }) + } + // Parsable JSON that is missing the fields the line is built from: the page + // must render nothing rather than `undefined commits ahead`. + const malformed = render('job_bad_shape', JSON.stringify({ sinceSha: 'abc1234', status: 'ahead' })) + assert.doesNotMatch(malformed, /data-temporal/) + assert.doesNotMatch(malformed, /undefined/) + const badStatus = render('job_bad_status', JSON.stringify({ sinceSha: 'abc1234', status: 'sideways', aheadBy: 1, mergedPrs: [], complete: true })) + assert.doesNotMatch(badStatus, /data-temporal/) + } finally { + store.close() + } +})