Skip to content

Commit 0016e90

Browse files
authored
Merge pull request #21 from d3cker/codex/preserve-commit-approvals
Preserve approval of the same commit across publication recovery
2 parents 5c65b50 + 0776d74 commit 0016e90

10 files changed

Lines changed: 51 additions & 16 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ include the full version, for example `## 0.7.0-beta.1`.
1818

1919
### Fixed
2020

21+
- Honor formal approvals for the exact verified PR commit even when publication
22+
or description recovery finishes later. Retain review revocation, authorization,
23+
and merge-readiness checks; keep timestamp gating for unbound merge comments.
24+
2125
- Retry PR head propagation after a successful push when the remote branch still
2226
matches the verified commit. Preserve a durable push checkpoint across restarts,
2327
avoid repeating acknowledged pushes, and distinguish closed PRs from changed

‎docs/advanced.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ session and verification/report snapshots. `controlVersion` increases on operato
297297
transitions, so stale TUI events cannot undo explicit resumption.
298298

299299
Watching continues PR-state discovery and accepts new authorized issue comments.
300-
`publishedHead` retains the successfully published SHA and approval-window time
300+
`publishedHead` retains the successfully published SHA and merge-comment window time
301301
across rounds; merge checks use it while watching. No saved published head means
302302
no automatic merge until the next successful publication. Old round checkpoints
303303
are not treated as a new successful publication. Explicit resumption validates

‎docs/architecture.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ loads a generic scheduler, a GitHub dispatcher, and a terminal UI component.
4545
preserve manual notes outside the managed description.
4646
7. Merge only after eligible approval of the published head, repository permission
4747
checks, and GitHub merge readiness checks. Post a signed acknowledgement.
48+
Formal reviews are bound to the exact commit and survive later description
49+
recovery; unbound merge comments must follow the latest publication.
4850

4951
A failure retains the current phase and retry state. An eligible `running` task
5052
with a saved session takes priority over other ready tasks. Unknown prompt

‎docs/bot-workflow.md‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ flowchart TD
557557
Idle[Worker has no eligible execution task] --> Eligible{Auto-merge enabled and done or watching task with published head eligible?}
558558
Eligible -->|No| Later[Wait for a later worker pass]
559559
Eligible -->|Yes| Since{publishedAt exists?}
560-
Since -->|No| Window[Record current time as fresh approval window]
560+
Since -->|No| Window[Record current time as fresh merge-comment window]
561561
Window --> Later
562562
Since -->|Yes| Scan[Scan again before considering merge]
563563
Scan --> Fresh{Pending feedback or closed PR?}
@@ -567,7 +567,7 @@ flowchart TD
567567
Already -->|Yes| Ack[Post or reconcile signed merge acknowledgement, persist merged and closed PR]
568568
Already -->|No| Head{Open, non-draft PR with saved published head?}
569569
Head -->|No| Poll[Clear mergeError, set mergeNextAt at least 60 seconds later]
570-
Head -->|Yes| Review[Evaluate latest decisive reviews and exact approval comments]
570+
Head -->|Yes| Review[Match latest decisive reviews to exact SHA, match merge comments after publication]
571571
Review --> Author{No outstanding changes request and eligible approver has write, maintain or admin access?}
572572
Author -->|No| Poll
573573
Author -->|Yes| Ready{mergeable and mergeable_state clean?}
@@ -602,23 +602,27 @@ flowchart TD
602602
Merge eligibility requires `done`, or `watching` with a saved `publishedHead`,
603603
a tracked nonclosed PR, a saved published commit, no
604604
merged flag, no pending feedback, and an elapsed `mergeNextAt`. Missing
605-
`publishedAt` in an older queue starts a fresh approval window rather than using
606-
historical approval. Merge checks run when the worker has no execution task to
605+
`publishedAt` in an older queue starts a fresh window for merge comments, which
606+
have no commit binding. Formal reviews still require the exact verified SHA.
607+
Merge checks run when the worker has no execution task to
607608
advance, rather than immediately after every publication.
608609

609610
For each reviewer, the latest `APPROVED`, `CHANGES_REQUESTED`, or `DISMISSED`
610611
review is decisive. Any outstanding changes request suppresses all approval
611612
candidates, including comment approvals. An approval review must reference the
612-
current verified SHA and have been submitted after `publishedAt`. An approval
613-
comment must have been created after that time and match a configured phrase
613+
current verified SHA and have a valid submission timestamp; it may precede
614+
`publishedAt`. Finishing or retrying a PR description update does not invalidate
615+
approval of unchanged code. An approval comment must have been created after
616+
`publishedAt` and match a configured phrase
614617
as a whole message after case, whitespace, and trailing `.`/`!` normalization.
615618
Bot comments and marked automation comments are excluded. Default phrases are
616619
`/merge`, `lgtm, merge`, and `approved, merge`; default merge method is `squash`.
617620
The permission check then requires an allowlisted candidate with repository write,
618621
maintain, or admin access. GitHub still enforces merge requirements.
619622

620-
Every successful round updates `publishedAt`, so old approvals cannot authorize
621-
the next published round. When the approval method returns false (for example,
623+
Every successful round updates `publishedAt`, so old merge comments cannot
624+
authorize the next published round. Formal reviews remain valid for the same
625+
commit only; a different published SHA requires a matching review. When the approval method returns false (for example,
622626
no eligible approval or a mismatched head), the dispatcher clears `mergeError` and schedules another check
623627
after 60 seconds. An approved PR that GitHub says is not ready, a rejected merge,
624628
or a request failure records `mergeError`; error retries also respect GitHub timing.

‎docs/configuration.md‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,12 @@ verified head SHA; a changed branch cannot be merged using an older approval.
156156
The bot does not request a protection bypass. Configure required checks and review
157157
rules on GitHub for your repository's policy.
158158

159-
Approvals must be newer than the bot's latest publication. On upgrade, old tasks
160-
start watching for new approvals; historical approvals do not cause a merge.
159+
Formal **Approve** reviews must reference the exact published commit and remain
160+
the reviewer's latest decisive review. They can precede completion of publication:
161+
retrying a description update does not invalidate approval of unchanged code.
162+
Merge comments have no commit binding, so they must be newer than the bot's latest
163+
publication. Old tasks without a publication timestamp start a fresh comment
164+
window on upgrade; existing reviews still require the exact verified commit.
161165
Pending issue feedback is processed before attempting a merge. Merge failures
162166
are retried at intervals of at least 60 seconds and appear as `mergeError` in
163167
`status` and in `/bot`. Successful merges receive a signed PR comment.

‎docs/runtime.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,12 @@ durable as before.
431431

432432
## PR descriptions
433433

434+
A formal GitHub **Approve** review applies to its exact commit even when submitted
435+
before the bot finishes updating the PR description. Retrying publication does
436+
not invalidate that review. A new commit needs a matching review, and dismissed
437+
approvals or outstanding change requests are not accepted. Plain merge comments
438+
still must follow the latest publication because they do not identify a commit.
439+
434440
After pushing a verified commit, GitHub may briefly show the previous commit in
435441
the PR. If the remote branch already matches the verified commit, the bot waits
436442
and automatically retries publication within its configured attempt limit. It

‎src/approval.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ export function approvalAuthors(reviews: Review[], comments: DatedComment[], hea
1212
}
1313
// An outstanding request for changes takes precedence over a merge comment.
1414
if ([...latest.values()].some(r => r.state === "CHANGES_REQUESTED")) return [];
15-
const authors = [...latest.values()].filter(r => r.state === "APPROVED" && r.commit_id === head && Date.parse(r.submitted_at ?? "") > since).map(r => r.user.login);
15+
// Reviews are bound to an exact commit. Description/publication retries must
16+
// not invalidate an approval already submitted for that same verified head.
17+
// Plain merge comments have no commit binding and still need the time window.
18+
const authors = [...latest.values()].filter(r => r.state === "APPROVED" && r.commit_id === head && Number.isFinite(Date.parse(r.submitted_at ?? ""))).map(r => r.user.login);
1619
for (const comment of comments) {
1720
if (comment.user.type === "Bot" || comment.body.includes("<!-- opencode2:") || Date.parse(comment.created_at) <= since) continue;
1821
// Exact full-message matching avoids interpreting negations, quotes, or embedded instructions as approval.

‎src/dispatcher.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ export class Dispatcher {
447447
if (!["done", "watching"].includes(task.status) || !task.pr || task.pr.state === "closed" || !head || task.merged || task.pendingFeedback?.length || (task.mergeNextAt ?? 0) > this.now()) continue;
448448
const repo = this.options.repositories.find(r => r.repo === task.repo);
449449
if (!repo) continue;
450-
// Older queues start watching now; historical approvals must not trigger an unexpected merge.
450+
// Older queues need a fresh window for merge comments, which have no SHA.
451451
if (!head.at) { await this.update(task, { publishedAt: this.now() }); continue; }
452452
try {
453453
await this.scan(); // Pick up issue feedback before considering a completed task for merge.

‎test/approval.test.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ const since = Date.parse("2026-09-10T10:00:00Z");
77
const review: Review = { id: 1, user: { login: "alice" }, state: "APPROVED", commit_id: "head", submitted_at: "2026-09-10T11:00:00Z" };
88
const comment: DatedComment = { id: 2, user: { login: "alice" }, body: "/merge", created_at: "2026-09-10T11:00:00Z", updated_at: "2026-09-10T11:00:00Z" };
99
const policy = { enabled: true, method: "squash" as const, comments: ["/merge", "jest git, możesz mergować"] };
10-
test("approvals must reference the published head and must not be stale or superseded", () => {
10+
test("reviews must reference the published head and must not be superseded or unsubmitted", () => {
1111
assert.deepEqual(approvalAuthors([review], [], "head", since, policy.comments), ["alice"]);
1212
assert.deepEqual(approvalAuthors([review], [], "new-head", since, policy.comments), []);
13-
assert.deepEqual(approvalAuthors([review], [], "head", since + 9_000_000, policy.comments), []);
13+
assert.deepEqual(approvalAuthors([review], [], "head", since + 9_000_000, policy.comments), ["alice"]);
14+
assert.deepEqual(approvalAuthors([{ ...review, submitted_at: null }], [], "head", since, policy.comments), []);
1415
for (const state of ["CHANGES_REQUESTED", "DISMISSED"]) {
1516
assert.deepEqual(approvalAuthors([review, { ...review, id: 3, state }], [], "head", since, policy.comments), []);
1617
}
@@ -42,6 +43,17 @@ test("merge API requires an allowed writer and pins the verified SHA", async ()
4243
}
4344
const stranger = api(); assert.equal(await stranger.github.mergeApproved("o/r", 2, "head", since, ["bob"], policy), false); assert.equal(stranger.writes.length, 0);
4445
});
46+
test("approval of the same SHA survives delayed publication, but an old merge comment cannot authorize it", async () => {
47+
const publishedLater = since + 9_000_000;
48+
const f = api();
49+
assert.equal(await f.github.mergeApproved("o/r", 2, "head", publishedLater, ["alice"], policy), true);
50+
assert.equal(f.writes.length, 1); assert.equal(f.writes[0]!.body.sha, "head");
51+
assert.deepEqual(approvalAuthors([], [comment], "head", publishedLater, policy.comments), []);
52+
assert.deepEqual(approvalAuthors([review], [], "different-head", publishedLater, policy.comments), []);
53+
for (const state of ["DISMISSED", "CHANGES_REQUESTED"]) {
54+
assert.deepEqual(approvalAuthors([review, { ...review, id: 3, state }], [comment], "head", publishedLater, policy.comments), []);
55+
}
56+
});
4557
test("unready checks defer merging and an already merged PR reconciles without another PUT", async () => {
4658
const f = api({ mergeable_state: "unstable" }); await assert.rejects(f.github.mergeApproved("o/r", 2, "head", since, ["alice"], policy), /not ready/); assert.equal(f.writes.length, 0);
4759
const failed = api({ mergeResult: false }); await assert.rejects(failed.github.mergeApproved("o/r", 2, "head", since, ["alice"], policy), /did not merge/);

‎test/core.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ test("new issue feedback prevents auto-merge and merge failures remain retryable
499499
await d.tick(); assert.equal(merges, 1); assert.equal(d.status()[0]?.pendingFeedback?.length, 1);
500500
});
501501

502-
test("upgraded queues ignore historical approvals until a new watching baseline exists", async () => {
502+
test("upgraded queues establish a fresh time window for unbound merge comments", async () => {
503503
const f = fixture(); const d = f.make(); await d.init(); await d.scan(); await d.tick();
504504
delete f.store.data.tasks[0]!.publishedAt;
505505
let since = 0; f.github.mergeApproved = async (_repo, _number, _commit, baseline) => { since = baseline; return false; };

0 commit comments

Comments
 (0)