From 3e7f7a3aa62cac621a137d6daf28181ee9235c2b Mon Sep 17 00:00:00 2001 From: Day Dreamer <87640074+HugoXK@users.noreply.github.com> Date: Mon, 16 Nov 2026 11:45:43 +0800 Subject: [PATCH] feat: merge a work item through the bridge adapter with a reconcile probe (#540) --- changelog.d/540-merge-command.fixed.md | 3 + docs/architecture.md | 19 ++ src/app.ts | 67 +++- src/identity/authorization.ts | 7 +- src/jobs/service.ts | 60 +++- src/management/catalog.ts | 57 +++- src/management/service.ts | 9 +- src/repos/github.ts | 4 + src/views/pages.ts | 2 +- test/management-merge.test.ts | 438 +++++++++++++++++++++++++ test/management-task.test.ts | 2 +- 11 files changed, 642 insertions(+), 26 deletions(-) create mode 100644 changelog.d/540-merge-command.fixed.md create mode 100644 test/management-merge.test.ts diff --git a/changelog.d/540-merge-command.fixed.md b/changelog.d/540-merge-command.fixed.md new file mode 100644 index 00000000..1f85dc8f --- /dev/null +++ b/changelog.d/540-merge-command.fixed.md @@ -0,0 +1,3 @@ +Written-by: HugoXK + +The console merge button now runs through the management contract: a merge whose answer from GitHub is lost leaves an `unknown` receipt instead of a 500, and submitting the same form again reads the pull request back, records the merge once (work item, PR state, audit) and never merges it twice. diff --git a/docs/architecture.md b/docs/architecture.md index 6d22b719..dc38e08a 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -2281,6 +2281,25 @@ effect is what moved the revision), `absent` resends exactly once under the same revision fence, and `still-unknown` — or an operation with no hook at all — answers `management_unknown` and writes nothing. Nothing is resent blind. +`task.merge` (#540 slice 6) is the first command whose durable effect is not in +SQLite: GitHub performs the merge. The gates and the merge call stay in +`Jobs.tryMerge`, which the auto-merge path uses too, so the two paths cannot +disagree about what may merge. The operation calls `Jobs.manualMerge` with the +transport failure rethrown, which is what turns a lost answer into an `unknown` +receipt instead of a refusal. The stable marker is the remote pull request, read +back by `Jobs.probePullRequestMerged`: `merged` settles the receipt with +`reconciled: true`, and the verdict's `settle` callback writes the work item, the +PR state, the `job.manually_merged` audit row and the job event that the lost +answer skipped — inside the receipt transaction, exactly where `commit` runs on +the accepted path. The merge already moved the revision, so the settle does not +bump it. A probe that finds the pull request not merged proves the effect absent +and the command runs once more under the same fence. A fresh key that finds the +pull request already merged is a success (`alreadyMerged`), not the old failure +sentence, and `recordMerge` skips the counter, audit and event when the merge is +already recorded. Merging requires the new `job.merge` capability, held by +`maintainer` only; `release.manage` is also maintainer-only, but cutting a +release is not merging this pull request. + `ManagementService.markUnknown` is for an adapter that learns about a lost response itself, such as a future API or chat channel: only the principal on the receipt may call it, it applies only while the receipt is still `accepted`, and a diff --git a/src/app.ts b/src/app.ts index 07951d61..aae87c1a 100644 --- a/src/app.ts +++ b/src/app.ts @@ -2197,6 +2197,13 @@ export async function buildApp(options: BuildOptions): Promise { commands[`work-scope:${item.id}`] = fields() commands[`work-verify:${item.id}`] = fields() } + // #540 slice 6: each delivery row's merge button carries its own job + // revision and key, so a double click replays one receipt and a page + // rendered before the job moved on conflicts instead of merging blind. + const prJobs = store.listPrJobsForRepo(repo.id, 10) + for (const prJob of prJobs) { + commands[`merge:${prJob.id}`] = { revision: management.revisionOf('job', prJob.id), idempotencyKey: randomId('cmd') } + } return reply.type('text/html').send( repoPage({ config, @@ -2214,7 +2221,7 @@ export async function buildApp(options: BuildOptions): Promise { }, jobs: store.listJobsPageForRepo(repo.id, 10, (jobsPage - 1) * 10), queued: store.listQueuedJobs(), - prJobs: store.listPrJobsForRepo(repo.id, 10), + prJobs, workItems, // #379: the repository page lists every state the ledger can hold. These // are the ids jobs reported, not GitHub's alert store. @@ -3616,18 +3623,64 @@ export async function buildApp(options: BuildOptions): Promise { return reply.redirect(`/repos/${repo.id}/jobs/${job.id}?${key}=${encodeURIComponent(message)}`) }) + // #540 slice 6: the merge button is a command whose durable effect is on + // GitHub. The gates and the merge call stay in `Jobs.tryMerge`; the route + // resolves the actor, the job and the revision the page rendered, and turns the + // receipt into a redirect. A lost answer leaves the receipt `unknown`, and + // resubmitting the same form reconciles against the pull request first. app.post('/repos/:id/jobs/:jobId/merge', async (request, reply) => { // #708: expensive — a merge writes to the upstream repository. if (!limits.take('expensive', `job-merge:${rateLimitKey(request)}`)) return refuseTooMany(request, reply) const { viewer, repo } = ownedRepo(request, String((request.params as Record).id), true) const job = store.getJob(String((request.params as Record).jobId)) if (job === undefined || job.repo_id !== repo.id) throw httpError(404, '任务不存在。', 'JOB_NOT_FOUND') - if (repo.repo_role !== 'maintainer') return reply.redirect(`/repos/${repo.id}/jobs/${job.id}?error=${encodeURIComponent('只有 maintainer 可以合并 PR。')}`) - const result = await jobs.manualMerge(repo, job) - store.audit(viewer.user.id, 'job.merge_requested', `${job.id}: ${result.ok ? 'merged' : (result.message ?? 'failed')}`) - const key = result.ok ? 'notice' : 'error' - const message = result.ok ? 'PR 已合并。' : (result.message ?? '合并失败。') - return reply.redirect(`/repos/${repo.id}/jobs/${job.id}?${key}=${encodeURIComponent(message)}`) + const back = `/repos/${repo.id}/jobs/${job.id}` + const t = translator(request.locale ?? 'zh') + const today = async (): Promise<{ notice: string } | { error: string }> => { + if (repo.repo_role !== 'maintainer') return { error: t('只有 maintainer 可以合并 PR。', 'Only a maintainer can merge a pull request.') } + const result = await jobs.manualMerge(repo, job) + const merged = result.ok || result.alreadyMerged === true + store.audit(viewer.user.id, 'job.merge_requested', `${job.id}: ${merged ? 'merged' : (result.message ?? 'failed')}`) + return merged ? { notice: t('PR 已合并。', 'The pull request is merged.') } : { error: result.message ?? t('合并失败。', 'The merge failed.') } + } + const answer = (result: { notice: string } | { error: string }) => ('notice' in result + ? reply.redirect(`${back}?notice=${encodeURIComponent(result.notice)}`) + : reply.redirect(`${back}?error=${encodeURIComponent(result.error)}`)) + + if (config.managementService === 'off') return answer(await today()) + + const parts = commandParts(form(request), management.revisionOf('job', job.id)) + const actor = managementActor({ store, userId: viewer.user.id, repo, channel: 'web', requestId: request.id }) + const envelope = { + operation: 'task.merge', + target: { projectId: repo.id, jobId: job.id }, + arguments: {}, + expectedRevision: parts.expectedRevision, + idempotencyKey: parts.idempotencyKey, + } + + if (config.managementService === 'shadow') { + const begun = management.begin(actor, envelope) + const result = await today() + // The receipt records what the contract would have done with the same form; + // a replay never rewrites a receipt that already answered. + if (begun.ok && !begun.replayed) { + management.settle(begun.receipt.id, 'notice' in result + ? { ok: true, result: { jobId: job.id, shadow: true } } + : { ok: false, code: 'job_not_merged', message: 'the route refused the merge' }) + } + return answer(result) + } + + const outcome = await management.run(actor, envelope) + if (outcome.ok) return answer({ notice: t('PR 已合并。', 'The pull request is merged.') }) + // A gate refusal is not a management code: its sentence is the reason the + // service wrote (a stale check, a missing approval), which is what the job + // page showed before this slice. + const sentence = outcome.code === 'job_not_merged' + ? (outcome.message === '' ? t('合并失败。', 'The merge failed.') : outcome.message) + : managementFailure(t, outcome, t('合并失败。', 'The merge failed.')) + return reply.redirect(`${back}?error=${encodeURIComponent(sentence)}&code=${outcome.code}`) }) app.post('/repos/:id/jobs/:jobId/approve-plan', async (request, reply) => { diff --git a/src/identity/authorization.ts b/src/identity/authorization.ts index 42beb342..af74ee75 100644 --- a/src/identity/authorization.ts +++ b/src/identity/authorization.ts @@ -25,6 +25,10 @@ export type Capability = // (maintainer only) so moving the write behind the contract does not take the // action away from the role that held it before. | 'repo.policy' + // #540 slice 6: merging a pull request. `release.manage` is also maintainer + // only, but "may cut a release" is not "may merge this PR", and the capability + // is the only place that distinction can live. + | 'job.merge' export const ALL_CAPABILITIES: readonly Capability[] = [ 'tasks.read', @@ -41,13 +45,14 @@ export const ALL_CAPABILITIES: readonly Capability[] = [ 'job.resume', 'job.retry', 'repo.policy', + 'job.merge', ] export const REPO_ROLES = ['maintainer', 'developer', 'viewer'] as const export type RepoRole = (typeof REPO_ROLES)[number] const ROLE_CAPABILITIES: Readonly> = { - maintainer: ['tasks.read', 'tasks.write', 'config.write', 'repo.policy', 'release.manage', 'knowledge.read', 'knowledge.write', 'schedules.manage', 'job.cancel', 'job.pause', 'job.create', 'job.resume', 'job.retry'], + maintainer: ['tasks.read', 'tasks.write', 'config.write', 'repo.policy', 'release.manage', 'knowledge.read', 'knowledge.write', 'schedules.manage', 'job.cancel', 'job.pause', 'job.create', 'job.resume', 'job.retry', 'job.merge'], // #624 added `job.create`, `job.resume` and `job.retry` as commands the // connection owner holds. `deriveRepoRole` maps a write-permission connection to // `developer`, so that role must carry them or an owner with a write token loses diff --git a/src/jobs/service.ts b/src/jobs/service.ts index b88b4f5b..c56189ec 100644 --- a/src/jobs/service.ts +++ b/src/jobs/service.ts @@ -2343,7 +2343,7 @@ export class Jobs { return { block, reused: false, refusal: null } } - private async tryMerge(job: JobRow, prUrl: string, manual: boolean): Promise<{ ok: boolean; message?: string }> { + private async tryMerge(job: JobRow, prUrl: string, manual: boolean, rethrowTransport = false): Promise<{ ok: boolean; message?: string; alreadyMerged?: boolean }> { if (job.kind !== 'iterate') return { ok: false, message: '只有 iterate 任务可以合并。' } let repo = this.store.getRepo(job.repo_id) if (repo === undefined) return { ok: false, message: '仓库不存在。' } @@ -2423,8 +2423,12 @@ export class Jobs { } } if (pr.merged) { + // #540 slice 6: a merge command that finds the pull request already + // merged did not fail — the effect it wanted is there. The caller records + // the local rows the lost answer skipped; the auto-merge path keeps + // treating it as "nothing to do" and is unchanged. this.store.updateJobsPrState(prUrl, 'merged') - return { ok: false, message: 'PR 已合并。' } + return { ok: false, message: 'PR 已合并。', alreadyMerged: true } } if (pr.state !== 'open' || pr.draft) return { ok: false, message: 'PR 不是 open 状态。' } const guard = await this.pathGuardFor(client, owner, name, number, job, repo, pr.changedFiles) @@ -2534,15 +2538,7 @@ export class Jobs { const requestedMethod = repo.merge_method === 'merge' || repo.merge_method === 'rebase' ? repo.merge_method : 'squash' const merged = await client.mergePullRequest(owner, name, number, { sha: pr.headSha, mergeMethod: requestedMethod }) if (merged.merged) { - const workItem = job.work_item_id === null ? undefined : this.store.getWorkItem(job.work_item_id) - if (workItem !== undefined) { - this.store.updateWorkItem(workItem.id, { state: 'merged', pr_state: 'merged', merged_at: nowIso(), merge_commit: merged.sha ?? null }) - } - incCounter('fledge_merges_total', { result: 'merged' }, 1, 'Pull request merge results') - this.store.updateJobsPrState(prUrl, 'merged') - this.store.audit(job.user_id, manual ? 'job.manually_merged' : 'job.auto_merged', `${job.id}: PR ${number} ${merged.sha ?? ''}`) - this.store.event('job_auto_merged', { userId: job.user_id, repoId: job.repo_id, jobId: job.id, meta: `pr=${number}` }) - this.store.recordJobEvent({ jobId: job.id, repoId: job.repo_id, userId: job.user_id, kind: 'job.merged', step: `pr=${number}`, meta: merged.sha ?? null }) + this.recordMerge(job, merged.sha, manual) return { ok: true, message: '已合并。' } } this.store.audit(job.user_id, 'job.merge_skipped', `${job.id}: ${merged.message ?? 'merge rejected'}`) @@ -2550,10 +2546,48 @@ export class Jobs { } catch (error) { this.store.audit(job.user_id, 'job.merge_failed', `${job.id}: ${String(error).slice(0, 300)}`) log.warn('merge failed', { trace: job.id, pr: number, error: String(error) }) + // #540 slice 6: a transport failure may have crossed the commit boundary — + // GitHub can have merged while the answer was lost. The command path needs + // the error to reach the service so the receipt becomes `unknown`; every + // other caller keeps today's refusal sentence. + if (rethrowTransport) throw error return { ok: false, message: error instanceof Error ? error.message : String(error) } } } + // #540 slice 6: the local record of a merged pull request, in one place so the + // auto-merge path, the console command and a reconciled `unknown` write the + // same rows. `already` is read before the write: a merge recorded by this job's + // own answer or by the webhook is not counted or announced a second time. + recordMerge(job: JobRow, sha: string | undefined, manual = true): void { + const prUrl = job.pr_url + if (prUrl === null) return + const number = /\/pull\/(\d+)/.exec(prUrl)?.[1] ?? '' + const already = this.store.getJob(job.id)?.pr_state === 'merged' + const workItem = job.work_item_id === null ? undefined : this.store.getWorkItem(job.work_item_id) + if (workItem !== undefined) { + this.store.updateWorkItem(workItem.id, { state: 'merged', pr_state: 'merged', merged_at: nowIso(), merge_commit: sha ?? null }) + } + this.store.updateJobsPrState(prUrl, 'merged') + if (already) return + incCounter('fledge_merges_total', { result: 'merged' }, 1, 'Pull request merge results') + this.store.audit(job.user_id, manual ? 'job.manually_merged' : 'job.auto_merged', `${job.id}: PR ${number} ${sha ?? ''}`) + this.store.event('job_auto_merged', { userId: job.user_id, repoId: job.repo_id, jobId: job.id, meta: `pr=${number}` }) + this.store.recordJobEvent({ jobId: job.id, repoId: job.repo_id, userId: job.user_id, kind: 'job.merged', step: `pr=${number}`, meta: sha ?? null }) + } + + // #540 slice 6: the reconcile probe for `task.merge`. It reads the one remote + // fact the command needs — whether the pull request is merged — and never calls + // the merge endpoint. A probe that throws leaves the receipt unknown. + async probePullRequestMerged(repo: RepoRow, job: JobRow): Promise<{ merged: boolean; sha?: string }> { + if (job.pr_url === null) return { merged: false } + const parsed = parsePrUrl(repo, job.pr_url, this.config.github.apiBase) + if (!parsed.ok) return { merged: false } + const token = await repoGithubToken(repo, this.config, this.githubApp, this.store) + const pr = await new GithubClient(token, this.config.github.apiBase).getPullRequest(parsed.ref.owner, parsed.ref.name, parsed.ref.number) + return { merged: pr.merged, ...(pr.mergeCommitSha === undefined ? {} : { sha: pr.mergeCommitSha }) } + } + private async verifyAndMaybeMerge(jobId: string): Promise { const current = this.store.getJob(jobId) if (current === undefined || current.pr_url === null) return @@ -2616,10 +2650,10 @@ export class Jobs { return { merged, skipped } } - async manualMerge(repo: RepoRow, job: JobRow): Promise<{ ok: boolean; message?: string }> { + async manualMerge(repo: RepoRow, job: JobRow, options: { rethrowTransport?: boolean } = {}): Promise<{ ok: boolean; message?: string; alreadyMerged?: boolean }> { if (job.repo_id !== repo.id) return { ok: false, message: '任务不属于这个仓库。' } if (job.pr_url === null) return { ok: false, message: '这个任务没有 PR。' } - return this.tryMerge(job, job.pr_url, true) + return this.tryMerge(job, job.pr_url, true, options.rethrowTransport === true) } private async resolveHeadSha(repo: RepoRow, prUrl: string): Promise { diff --git a/src/management/catalog.ts b/src/management/catalog.ts index 591fbb66..080519f5 100644 --- a/src/management/catalog.ts +++ b/src/management/catalog.ts @@ -57,7 +57,11 @@ export interface OperationResult { // it provably did not happen, so resending is safe; `still-unknown` means the // probe itself could not decide, and the receipt has to stay `unknown`. export type ReconcileVerdict = - | { state: 'applied'; result?: Record } + // `settle` is for an effect whose local record depends on the external answer: + // a merge that GitHub performed but whose response was lost left the work item, + // the PR state and the audit row unwritten. The service runs it inside the + // receipt transaction, exactly where `commit` runs on the accepted path. + | { state: 'applied'; result?: Record; settle?: () => void } | { state: 'absent' } | { state: 'still-unknown' } @@ -354,9 +358,60 @@ const repoUpdate: Operation = { }, } +// #540 slice 6: the console merge button, whose durable effect is not in SQLite. +// The gates and the merge call stay in `Jobs.tryMerge`, which the auto-merge path +// uses too; this operation only names the command. A transport failure is +// rethrown so the receipt becomes `unknown`, and the probe reads the pull request +// back before anything is resent. +const taskMerge: Operation = { + kind: 'command', + capabilities: ['job.merge'], + requiredTarget: ['projectId', 'jobId'], + revisionTarget: 'jobId', + revisionType: 'job', + async execute(context, target) { + const repo = context.store.getRepo(target.projectId ?? '') + if (repo === undefined) return { status: 'failed', errorCode: 'project_not_found', errorMessage: 'the project does not exist' } + const job = context.store.getJob(target.jobId ?? '') + if (job === undefined || job.repo_id !== repo.id) return { status: 'failed', errorCode: 'job_not_found', errorMessage: 'the job does not exist in this project' } + const result = await context.jobs.manualMerge(repo, job, { rethrowTransport: true }) + if (!result.ok) { + // A fresh key after a merge that landed elsewhere is not a failure: the + // effect this command wanted is there. The commit writes the local rows the + // earlier attempt may have skipped, and `recordMerge` keeps it idempotent. + if (result.alreadyMerged === true) { + return { + status: 'succeeded', + result: { jobId: job.id, merged: true, alreadyMerged: true }, + commit: () => context.jobs.recordMerge(job, undefined), + } + } + return { status: 'failed', errorCode: 'job_not_merged', errorMessage: result.message ?? 'the pull request was not merged' } + } + return { status: 'succeeded', result: { jobId: job.id, merged: true } } + }, + // The stable marker is the remote pull request, read back. `merged` means the + // effect happened whoever performed it, and the settle writes the local record + // the lost answer skipped; anything else proves the merge did not happen, so a + // resend is safe. + async reconcile(context, target) { + const repo = context.store.getRepo(target.projectId ?? '') + const job = context.store.getJob(target.jobId ?? '') + if (repo === undefined || job === undefined) return { state: 'applied', result: { jobId: target.jobId ?? '', reconciled: true } } + const probe = await context.jobs.probePullRequestMerged(repo, job) + if (!probe.merged) return { state: 'absent' } + return { + state: 'applied', + result: { jobId: job.id, merged: true, ...(probe.sha === undefined ? {} : { sha: probe.sha }) }, + settle: () => context.jobs.recordMerge(job, probe.sha), + } + }, +} + export const OPERATIONS: Readonly> = Object.freeze({ 'repo.update': repoUpdate, 'task.cancel': taskCancel, + 'task.merge': taskMerge, 'task.pause': taskPause, 'task.create': taskCreate, 'task.resume': taskResume, diff --git a/src/management/service.ts b/src/management/service.ts index 46c3a4d9..cff23253 100644 --- a/src/management/service.ts +++ b/src/management/service.ts @@ -40,6 +40,7 @@ export type ManagementCode = | 'job_not_pausable' | 'job_not_resumable' | 'job_not_retryable' + | 'job_not_merged' | 'task_not_queued' | 'invalid_arguments' | 'work_item_not_found' @@ -306,7 +307,7 @@ export class ManagementService { } if (verdict.state === 'still-unknown') return this.unknownOutcome(receipt, `the effect of ${receipt.operation} is still unknown`) if (verdict.state === 'applied') { - return this.settleReconciled(receipt, operation, target, verdict.result ?? {}) + return this.settleReconciled(receipt, operation, target, verdict.result ?? {}, verdict.settle) } // Absent: the effect provably did not happen, so this is the one case where // the same command may run again. @@ -326,10 +327,14 @@ export class ManagementService { // The probe found the effect already there. The receipt records the answer but // does not bump the revision: whoever wrote the effect already moved it, and a // second bump would fence a page that read the real change. - private settleReconciled(receipt: ManagementCommandRow, operation: Operation, target: CommandTarget, result: Record): CommandOutcome { + private settleReconciled(receipt: ManagementCommandRow, operation: Operation, target: CommandTarget, result: Record, settle?: () => void): CommandOutcome { const merged = { ...result, reconciled: true } const resourceId = String(target[operation.revisionTarget] ?? '') this.store.transaction(() => { + // #540 slice 6: an effect that happened remotely may still owe its local + // record. The settle runs here, in the same transaction as the receipt, so + // a crash cannot leave the merge recorded with the receipt still unknown. + settle?.() const revision = this.store.revisionOf(operation.revisionType, resourceId) this.store.updateCommandReceipt(receipt.id, { status: 'succeeded', diff --git a/src/repos/github.ts b/src/repos/github.ts index 3df526ce..c6672bb4 100644 --- a/src/repos/github.ts +++ b/src/repos/github.ts @@ -60,6 +60,8 @@ export interface PullRequestInfo { mergeableState: string draft: boolean merged: boolean + // #540 slice 6: a reconciled merge records the commit GitHub actually made. + mergeCommitSha?: string changedFiles: number // #373: the body is what the agent already wrote about the change. The job // page shows it; nothing here interprets it. @@ -600,6 +602,7 @@ export class GithubClient { mergeable_state: string draft: boolean merged: boolean + merge_commit_sha?: string | null changed_files?: number body?: string | null }>( @@ -618,6 +621,7 @@ export class GithubClient { mergeableState: pr.mergeable_state, draft: pr.draft, merged: pr.merged, + ...(typeof pr.merge_commit_sha === 'string' ? { mergeCommitSha: pr.merge_commit_sha } : {}), changedFiles: Number(pr.changed_files ?? 0), body: typeof pr.body === 'string' ? pr.body : '', } diff --git a/src/views/pages.ts b/src/views/pages.ts index 29008c7e..62fb9d00 100644 --- a/src/views/pages.ts +++ b/src/views/pages.ts @@ -1686,7 +1686,7 @@ export function repoPage(input: { config: Config; locale: Locale; viewer: Viewer const number = /\/pull\/(\d+)/.exec(job.pr_url ?? '')?.[1] ?? '' const readiness = mergeReadiness(job) const reasons = readiness.reasons.slice(0, 2).join(' · ') - return `#${escapeHtml(number)}${badge(t, job.status)}${readinessBadge(t, readiness.state)}${reasons === '' ? '' : `
${escapeHtml(reasons)}`}${job.review_score === null ? '—' : job.review_score}${escapeHtml(quotaMoney(job.cost_usd * config.quota.usdCny))}${escapeHtml(humanTimeShort(job.created_at))}
${csrf(viewer)}
${repo.repo_role === 'maintainer' ? `
${csrf(viewer)}
` : ''}
` + return `#${escapeHtml(number)}${badge(t, job.status)}${readinessBadge(t, readiness.state)}${reasons === '' ? '' : `
${escapeHtml(reasons)}`}${job.review_score === null ? '—' : job.review_score}${escapeHtml(quotaMoney(job.cost_usd * config.quota.usdCny))}${escapeHtml(humanTimeShort(job.created_at))}
${csrf(viewer)}
${repo.repo_role === 'maintainer' ? `
${csrf(viewer)}${commandHidden(commands[`merge:${job.id}`])}
` : ''}
` }).join('') if (repo.auto_merge === 0 && prJobs.length >= 3) { pageMessages.push({ diff --git a/test/management-merge.test.ts b/test/management-merge.test.ts new file mode 100644 index 00000000..ff23886b --- /dev/null +++ b/test/management-merge.test.ts @@ -0,0 +1,438 @@ +import test from 'node:test' +import assert from 'node:assert/strict' +import type { IncomingMessage, ServerResponse } from 'node:http' +import { Store, type JobRow, type RepoRow, type UserRow } from '../src/db.js' +import { buildApp, type BuiltApp } from '../src/app.js' +import { Jobs } from '../src/jobs/service.js' +import { MockRunner } from '../src/jobs/mock.js' +import { capabilitiesForRole } from '../src/identity/authorization.js' +import { managementActor } from '../src/management/actor.js' +import { ManagementService, type CommandEnvelope } from '../src/management/service.js' +import { encryptSecret } from '../src/lib/secrets.js' +import { flashOf, formBody, MemoryMailer, makeUser, sessionFor, startFakeServer, testConfig } from './helpers.js' + +// #540 slice 6: merging is the first command whose durable effect is not in +// SQLite. GitHub performs the merge and the answer can be lost, so the receipt +// enters `unknown`; the next attempt reads the pull request back and either +// settles the local record the lost answer skipped or resends, never blindly. + +function json(response: ServerResponse, status: number, body: unknown): void { + response.writeHead(status, { 'content-type': 'application/json' }) + response.end(JSON.stringify(body)) +} + +interface GithubState { + merged: boolean + mergeAttempts: number + mergeSha: string + draft: boolean + // 'lost' performs the merge and drops the answer; 'refuse' drops the answer + // without merging, which is the case the probe must read as absent. + mode: 'none' | 'lost' | 'refuse' +} + +interface Harness { + store: Store + jobs: Jobs + service: ManagementService + repo: RepoRow + job: JobRow + state: GithubState + user: string + close: () => Promise +} + +// #540 slice 6: both suites below need the same fake GitHub. The merge route +// and the command service read the pull request, its checks and its merge +// answer from here. +function fakeGithub(state: GithubState): (request: IncomingMessage, response: ServerResponse) => void { + return (request, response) => { + const method = request.method ?? 'GET' + const url = request.url ?? '' + if (method === 'GET' && url === '/repos/o/r/pulls/1') { + json(response, 200, { + number: 1, + html_url: 'https://github.com/o/r/pull/1', + title: 'fix', + state: state.merged ? 'closed' : 'open', + head: { ref: 'fledge/fix', sha: 'abc' }, + base: { ref: 'main' }, + mergeable: true, + mergeable_state: 'clean', + draft: state.draft, + merged: state.merged, + ...(state.merged ? { merge_commit_sha: state.mergeSha } : {}), + }) + return + } + if (method === 'GET' && url === '/repos/o/r/commits/abc/check-runs?per_page=100') { + json(response, 200, { check_runs: [{ name: 'ci', status: 'completed', conclusion: 'success' }] }) + return + } + if (method === 'GET' && url === '/repos/o/r/commits/abc/status') { + json(response, 200, { state: 'success', statuses: [] }) + return + } + if (method === 'GET' && url === '/repos/o/r/pulls/1/reviews?per_page=100') { + json(response, 200, []) + return + } + if (method === 'GET' && url === '/repos/o/r/branches/main/protection') { + json(response, 200, { required_status_checks: { contexts: ['ci'] }, required_pull_request_reviews: { required_approving_review_count: 0 } }) + return + } + if (method === 'PUT' && url === '/repos/o/r/pulls/1/merge') { + state.mergeAttempts += 1 + if (state.mode === 'lost') { + state.merged = true + request.socket.destroy() + return + } + if (state.mode === 'refuse') { + request.socket.destroy() + return + } + state.merged = true + json(response, 200, { merged: true, sha: state.mergeSha }) + return + } + json(response, 404, { message: 'Not Found' }) + } +} + +async function harness(id: string): Promise { + const state: GithubState = { merged: false, mergeAttempts: 0, mergeSha: 'def', draft: false, mode: 'none' } + const github = await startFakeServer(fakeGithub(state)) + + const base = testConfig() + const config = testConfig({ github: { ...base.github, apiBase: github.url }, managementService: 'on' }) + const store = new Store(':memory:') + const jobs = new Jobs(store, config, { name: 'noop', async run() { return { status: 'running' as const } } }, new MemoryMailer()) + const user = makeUser(store, { login: `merge-${id}`, status: 'approved' }) + const repo = store.createRepo({ + id: `repo_merge_${id}`, + userId: user.id, + owner: 'o', + name: 'r', + defaultBranch: 'main', + tokenEnc: encryptSecret('gh-token', config.tokenEncryptionKey), + permission: 'admin', + repoRole: 'maintainer', + upstreamHtmlUrl: 'https://github.com/o/r', + forkHtmlUrl: 'https://github.com/o/r', + goal: '', + autoIterate: false, + intervalMinutes: 1440, + }) + const item = store.createWorkItem({ id: `work_merge_${id}`, repoId: repo.id, userId: user.id, title: 'fix', prUrl: 'https://github.com/o/r/pull/1', state: 'pr_ready' }) + const job = store.createJob({ id: `job_merge_${id}`, repoId: repo.id, userId: user.id, trigger: 'manual', goal: 'g', callbackHash: `cb-merge-${id}`, platformTokenHash: '', kind: 'iterate', baseBranch: 'main', workItemId: item.id }) + store.updateJob(job.id, { status: 'succeeded', branch: 'fledge/fix', pr_url: 'https://github.com/o/r/pull/1', finished_at: new Date().toISOString() }) + const service = new ManagementService(store, jobs) + return { + store, + jobs, + service, + repo, + job: store.getJob(job.id)!, + state, + user: user.id, + close: async () => { + store.close() + await github.close() + }, + } +} + +function envelope(h: Harness, overrides: Partial = {}): CommandEnvelope { + return { + operation: 'task.merge', + target: { projectId: h.repo.id, jobId: h.job.id }, + arguments: {}, + expectedRevision: h.service.revisionOf('job', h.job.id), + idempotencyKey: 'key-merge-1', + ...overrides, + } +} + +test('the same key and target replay one merge and one receipt', async () => { + const h = await harness('replay') + try { + const actor = managementActor({ store: h.store, userId: h.user, repo: h.repo, channel: 'web', requestId: 'req-m1' }) + // The form carries the revision it rendered, so a replay sends that same + // revision — recomputing it after the merge would read as a different request. + const env = envelope(h) + const first = await h.service.run(actor, env) + assert.equal(first.ok, true) + if (!first.ok) return + assert.equal(first.replayed, false) + assert.equal(h.state.mergeAttempts, 1) + + const again = await h.service.run(actor, env) + assert.equal(again.ok, true) + if (!again.ok) return + assert.equal(again.replayed, true, 'a second submit answers the first receipt') + assert.equal(again.receipt.id, first.receipt.id) + assert.equal(h.state.mergeAttempts, 1, 'a replay never reaches GitHub') + assert.equal(h.store.listAudit(100).filter((row) => row.action === 'job.manually_merged').length, 1) + assert.equal(h.store.getWorkItem(`work_merge_replay`)?.state, 'merged') + assert.equal(h.store.getJob(h.job.id)?.pr_state, 'merged') + } finally { + await h.close() + } +}) + +test('the same key with a different target is an idempotency conflict', async () => { + const h = await harness('conflict') + try { + const actor = managementActor({ store: h.store, userId: h.user, repo: h.repo, channel: 'web', requestId: 'req-m2' }) + const other = h.store.createJob({ id: 'job_merge_conflict_2', repoId: h.repo.id, userId: h.user, trigger: 'manual', goal: 'g', callbackHash: 'cb-merge-conflict-2', platformTokenHash: '', kind: 'iterate', baseBranch: 'main' }) + h.store.updateJob(other.id, { status: 'succeeded', branch: 'fledge/fix2', pr_url: 'https://github.com/o/r/pull/1' }) + + const first = await h.service.run(actor, envelope(h)) + assert.equal(first.ok, true) + const conflicted = await h.service.run(actor, envelope(h, { target: { projectId: h.repo.id, jobId: other.id }, expectedRevision: h.service.revisionOf('job', other.id) })) + assert.equal(conflicted.ok, false) + if (conflicted.ok) return + assert.equal(conflicted.code, 'idempotency_conflict') + assert.equal(h.state.mergeAttempts, 1, 'the conflict never reaches GitHub') + } finally { + await h.close() + } +}) + +test('a stale expected revision writes nothing', async () => { + const h = await harness('stale') + try { + const actor = managementActor({ store: h.store, userId: h.user, repo: h.repo, channel: 'web', requestId: 'req-m3' }) + const stale = await h.service.run(actor, envelope(h, { expectedRevision: h.service.revisionOf('job', h.job.id) + 7 })) + assert.equal(stale.ok, false) + if (stale.ok) return + assert.equal(stale.code, 'management_revision_conflict') + assert.equal(h.state.mergeAttempts, 0, 'a stale page never merges') + assert.equal(h.store.getWorkItem('work_merge_stale')?.state, 'pr_ready') + } finally { + await h.close() + } +}) + +test('a lost merge answer stays unknown, and the retry settles the local record without a second merge', async () => { + const h = await harness('lost') + try { + const actor = managementActor({ store: h.store, userId: h.user, repo: h.repo, channel: 'web', requestId: 'req-m4' }) + h.state.mode = 'lost' + const lost = await h.service.run(actor, envelope(h)) + assert.equal(lost.ok, false) + if (lost.ok) return + assert.equal(lost.code, 'management_unknown', 'a transport failure is unknown, not a refusal') + assert.equal(h.state.mergeAttempts, 1) + assert.equal(h.state.merged, true, 'GitHub merged; only the answer was lost') + assert.equal(h.store.commandReceipt(lost.receipt?.id ?? '')?.status, 'unknown') + assert.equal(h.store.getWorkItem('work_merge_lost')?.state, 'pr_ready', 'the local record was not written yet') + + h.state.mode = 'none' + const settled = await h.service.run(actor, envelope(h)) + assert.equal(settled.ok, true) + if (!settled.ok) return + assert.equal(settled.result.reconciled, true, 'the receipt says the effect was found, not performed') + assert.equal(h.state.mergeAttempts, 1, 'the reconcile probe is a read; no second merge') + assert.equal(h.store.getWorkItem('work_merge_lost')?.state, 'merged', 'the settle writes what the lost answer skipped') + assert.equal(h.store.getWorkItem('work_merge_lost')?.merge_commit, 'def', 'the settle records the commit GitHub made') + assert.equal(h.store.getJob(h.job.id)?.pr_state, 'merged') + assert.equal(h.store.listAudit(100).filter((row) => row.action === 'job.manually_merged').length, 1, 'one merge, one audit row') + } finally { + await h.close() + } +}) + +test('an unconfirmed merge that did not happen is resent exactly once', async () => { + const h = await harness('absent') + try { + const actor = managementActor({ store: h.store, userId: h.user, repo: h.repo, channel: 'web', requestId: 'req-m5' }) + h.state.mode = 'refuse' + const lost = await h.service.run(actor, envelope(h)) + assert.equal(lost.ok, false) + if (lost.ok) return + assert.equal(lost.code, 'management_unknown') + assert.equal(h.state.merged, false, 'GitHub did not merge, so the probe can prove absence') + + h.state.mode = 'none' + const resent = await h.service.run(actor, envelope(h)) + assert.equal(resent.ok, true) + if (!resent.ok) return + assert.equal(resent.replayed, false, 'a reconciled resend is a real execution') + assert.equal(resent.result.reconciled, undefined) + assert.equal(h.state.mergeAttempts, 2, 'the command ran again exactly once') + assert.equal(h.store.getWorkItem('work_merge_absent')?.state, 'merged') + } finally { + await h.close() + } +}) + +test('a caller without job.merge cannot merge', async () => { + const h = await harness('capability') + try { + assert.equal(capabilitiesForRole('maintainer').includes('job.merge'), true) + assert.equal(capabilitiesForRole('developer').includes('job.merge'), false, 'merging stays maintainer-only') + const stranger = makeUser(h.store, { login: 'merge-stranger', status: 'approved' }) + const actor = managementActor({ store: h.store, userId: stranger.id, repo: h.repo, channel: 'web', requestId: 'req-m6' }) + const refused = await h.service.run(actor, envelope(h)) + assert.equal(refused.ok, false) + if (refused.ok) return + assert.equal(refused.code, 'authorization_revoked') + assert.equal(h.state.mergeAttempts, 0, 'the refusal happens before the merge call') + assert.equal(h.store.getWorkItem('work_merge_capability')?.state, 'pr_ready') + } finally { + await h.close() + } +}) + +// #540 slice 6: the command service covers the contract; these two cover what +// the merge button is for a maintainer — a fresh key after the merge already +// landed still succeeds and writes the rows, and the route runs the same command +// in `on`, `shadow` and `off` without changing what it does. + +test('a fresh key after the merge landed succeeds and writes the skipped rows', async () => { + const h = await harness('freshafter') + try { + const actor = managementActor({ store: h.store, userId: h.user, repo: h.repo, channel: 'web', requestId: 'req-m7' }) + const first = await h.service.run(actor, envelope(h)) + assert.equal(first.ok, true) + assert.equal(h.state.mergeAttempts, 1) + + // A second attempt with a new key does not replay the old receipt: it reads + // the pull request back, finds the effect already there and records it. + const second = await h.service.run(actor, envelope(h, { idempotencyKey: 'key-merge-2' })) + assert.equal(second.ok, true, 'the effect is there, so the command succeeds') + if (!second.ok) return + assert.equal(second.replayed, false) + assert.equal(second.result.alreadyMerged, true) + assert.equal(h.state.mergeAttempts, 1, 'the probe is a read; no second merge') + assert.equal(h.store.getWorkItem(`work_merge_freshafter`)?.state, 'merged') + assert.equal(h.store.getJob(h.job.id)?.pr_state, 'merged') + } finally { + await h.close() + } +}) + +interface AppHarness { + built: BuiltApp + store: Store + state: GithubState + user: UserRow + repo: RepoRow + job: JobRow + close: () => Promise +} + +async function appHarness(id: string, mode: 'on' | 'shadow' | 'off'): Promise { + const state: GithubState = { merged: false, mergeAttempts: 0, mergeSha: 'def', draft: false, mode: 'none' } + const github = await startFakeServer(fakeGithub(state)) + const base = testConfig() + const config = testConfig({ github: { ...base.github, apiBase: github.url }, managementService: mode }) + const store = new Store(':memory:') + const mailer = new MemoryMailer() + const runner = new MockRunner() + const built = await buildApp({ config, store, mailer, runner, startScheduler: false }) + const user = makeUser(store, { login: `route-merge-${id}`, status: 'approved' }) + const repo = store.createRepo({ + id: `repo_route_${id}`, + userId: user.id, + owner: 'o', + name: 'r', + defaultBranch: 'main', + tokenEnc: encryptSecret('gh-token', config.tokenEncryptionKey), + permission: 'admin', + repoRole: 'maintainer', + upstreamHtmlUrl: 'https://github.com/o/r', + forkHtmlUrl: 'https://github.com/o/r', + goal: '', + autoIterate: false, + intervalMinutes: 1440, + }) + const item = store.createWorkItem({ id: `work_route_${id}`, repoId: repo.id, userId: user.id, title: 'fix', prUrl: 'https://github.com/o/r/pull/1', state: 'pr_ready' }) + const job = store.createJob({ id: `job_route_${id}`, repoId: repo.id, userId: user.id, trigger: 'manual', goal: 'g', callbackHash: `cb-route-${id}`, platformTokenHash: '', kind: 'iterate', baseBranch: 'main', workItemId: item.id }) + store.updateJob(job.id, { status: 'succeeded', branch: 'fledge/fix', pr_url: 'https://github.com/o/r/pull/1', finished_at: new Date().toISOString() }) + return { + built, + store, + state, + user, + repo, + job: store.getJob(job.id)!, + close: async () => { + // `built.close()` closes the store it was given; closing it twice throws. + await built.close() + await github.close() + }, + } +} + +function mergeForm(h: AppHarness, fields: Record = {}) { + const session = sessionFor(h.built, h.user) + return h.built.app.inject({ + method: 'POST', + url: `/repos/${h.repo.id}/jobs/${h.job.id}/merge`, + headers: { cookie: session.cookie, 'content-type': 'application/x-www-form-urlencoded' }, + payload: formBody({ csrf: session.csrf, ...fields }), + }) +} + +test('the merge route runs the command in on mode, replays its key, and refuses a stale page', async () => { + const h = await appHarness('on', 'on') + try { + const first = await mergeForm(h, { idempotency_key: 'route-key-1' }) + assert.equal(first.statusCode, 302) + assert.match(flashOf(first)?.notice?.text ?? '', /已合并/) + assert.equal(h.state.mergeAttempts, 1) + + const replay = await mergeForm(h, { idempotency_key: 'route-key-1', expected_revision: '1' }) + assert.equal(replay.statusCode, 302) + assert.match(flashOf(replay)?.notice?.text ?? '', /已合并/) + assert.equal(h.state.mergeAttempts, 1, 'the same key replays the receipt; GitHub is not called again') + + // The page rendered before the merge carried the old revision. + const stale = await mergeForm(h, { idempotency_key: 'route-key-2', expected_revision: '1' }) + assert.equal(stale.statusCode, 302) + assert.match(flashOf(stale)?.error?.key ?? '', /^management_revision_conflict:/) + assert.equal(h.state.mergeAttempts, 1, 'a stale page never merges') + } finally { + await h.close() + } +}) + +test('the merge route reports a gate refusal with the job code', async () => { + const h = await appHarness('gate', 'on') + try { + // A draft is a gate `tryMerge` applies to a manual merge too. + h.state.draft = true + const response = await mergeForm(h, { idempotency_key: 'route-key-gate' }) + assert.equal(response.statusCode, 302) + assert.match(flashOf(response)?.error?.key ?? '', /^job_not_merged:/) + assert.match(flashOf(response)?.error?.text ?? '', /open/) + assert.equal(h.state.mergeAttempts, 0, 'the gate refuses before the merge call') + } finally { + await h.close() + } +}) + +test('the shadow route merges and settles the receipt, and off has no command', async () => { + const shadow = await appHarness('shadow', 'shadow') + try { + const response = await mergeForm(shadow, { idempotency_key: 'route-key-shadow' }) + assert.equal(response.statusCode, 302) + assert.match(flashOf(response)?.notice?.text ?? '', /已合并/) + assert.equal(shadow.state.mergeAttempts, 1, 'shadow mode does not stop the merge') + } finally { + await shadow.close() + } + + const off = await appHarness('off', 'off') + try { + const response = await mergeForm(off, { idempotency_key: 'route-key-off' }) + assert.equal(response.statusCode, 302) + assert.match(flashOf(response)?.notice?.text ?? '', /已合并/) + assert.equal(off.state.mergeAttempts, 1, 'off runs the direct path unchanged') + } finally { + await off.close() + } +}) diff --git a/test/management-task.test.ts b/test/management-task.test.ts index 962a974f..bbe65e4f 100644 --- a/test/management-task.test.ts +++ b/test/management-task.test.ts @@ -268,7 +268,7 @@ test('a create refusal is recorded with its code so the banner can translate it' test('the catalog closes over the task commands and the settings command', async () => { const { operationNames } = await import('../src/management/catalog.js') - assert.deepEqual(operationNames().sort(), ['repo.update', 'task.cancel', 'task.create', 'task.pause', 'task.resume', 'task.retry']) + assert.deepEqual(operationNames().sort(), ['repo.update', 'task.cancel', 'task.create', 'task.merge', 'task.pause', 'task.resume', 'task.retry']) }) test('a caller cannot put an identity field into a create command', async () => {