Skip to content

Commit a0b5206

Browse files
mchwangclaude
andcommitted
Address Copilot round 6 on F2d: branch lookup always, draft first, docs, v6→v7 test
- Look up the branch's open PR on every publish, even with no known markers, so a PR codeboost did not open is refused before the push can move it. - For a needs-human task with a ready PR, make it a draft before the push or any description change; a "drafts unsupported" refusal now leaves the PR exactly as it was. A failure later in the refresh stays recorded in flight and is redone by the next publish. - Correct the already-fixed module summary to the implemented policy. - Add a v6-to-v7 upgrade test on a populated file database. - AGENTS.md: refusable steps come before writes that cannot be undone. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent f5859f4 commit a0b5206

6 files changed

Lines changed: 82 additions & 29 deletions

File tree

‎AGENTS.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ Every reproduced race requires a failing-before and passing-after regression. As
9696
- If the external lifecycle mechanism or mode changes between validation passes, abort before the irreversible command. Create durable lifecycle ownership from the final stable mode, never from an earlier observation.
9797
- When an irreversible command has an ambiguous timeout, cancellation, transport, or unknown outcome, retain durable in-flight ownership and reconcile external state before enabling retry. Only a confirmed refusal may become retryable failure.
9898
- Record in-flight ownership before the first external write of an operation, not before a later step: when a push moves an open PR's head, the refresh it belongs to is already in flight.
99+
- Order an operation's external writes so that any step the remote can definitely refuse comes before the writes that cannot be taken back. A refusal after a push or a description change leaves a half-updated record.
99100
- Correlate retry observations to the current attempt with an immutable external identity or event boundary, and fail closed when multiple post-boundary action sequences appear. Matching only the resource or commit identity can replay another attempt's terminal event.
100101
- When recovery looks for the result of an earlier attempt, recognise the identities of all earlier attempts, abandoned ones included. A result that appears after its attempt was abandoned must be adopted, not treated as foreign, or recovery can never settle.
101102
- Make an idempotency key required at the API boundary for every replayable action, and look up its saved outcome before any other guard, including in-flight, validation and coordinator shutdown guards. The one exception is the server's HTTP 503 during shutdown, which applies nothing; the client must keep the key and resend it. Save every definite outcome under the key (refusals before admission too), keep the saved response current with the durable outcome it reports, and replay failures as failures with complete result fields. Only a passing, nothing-applied outcome (shutdown, abort, deadline, storage error) stays resendable.

‎docs/implementation/pull-request-opening.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Only one publish runs per task at a time; a second one is refused. A publish who
5252

5353
1. **Recover.** If an opening is still `opening`, look for the open PR from the task branch, with the markers of all the task's openings for that branch. If it has this opening's marker, record it as opened. If it has another opening's marker, this opening created nothing (one open PR per branch): mark it `abandoned` and continue. If none exists, the request may still be in flight or not yet visible. So publish stops with `OpeningUnsettled` until the opening is 10 minutes old (`settleMs`). After that, it marks the opening `abandoned` and continues.
5454
2. **Status and no changes.** Refuse unless the task is running (or in needs human, for a draft). If the task head is its base, open nothing. A running task moves to needs human.
55-
3. **Find the earlier PR.** If the task has an opened or abandoned opening on the same branch, ask GitHub for the branch's open PR and which opening's marker it carries. A PR with an opened record's marker but another number is refused here, before the push. This comes before the check: an abandoned opening's PR links the issue and has no recorded number, so only its marker shows it is the task's own, and its number is added to the own PRs for the check.
55+
3. **Find the branch PR.** Ask GitHub for the branch's open PR every time, with the markers of the task's opened and abandoned openings for that branch (none if the task has no PR yet). An open PR that carries none of them was not opened by codeboost: publish refuses before anything is pushed. A PR with an opened record's marker but another number is refused too. This comes before the check: an abandoned opening's PR links the issue and has no recorded number, so only its marker shows it is the task's own, and its number is added to the own PRs for the check.
5656
4. **Check.** Run the check. Record the result, and any status change, in one transaction. That transaction refuses if the task changed during the check.
5757
5. **Push.** When the task already has an open PR, the refresh is recorded first (the push moves that PR's head), then the push runs; otherwise the push runs straight after the check's transaction with no await in between. Push the task head to `codeboost/issue-<issue>-<task slug>-<hash>`. The slug is readable but can collide. The hash is 16 hex characters of SHA-256 over the exact task identity, so two tasks never share a branch.
5858
6. **Re-read.** Right before the GitHub call, the Store confirms that the latest check is clear and that nothing changed since that check: same task state version, same review version (approvals, choices and notes), same snapshot, same head.
@@ -96,7 +96,7 @@ Some repositories do not support draft PRs (for example private repositories on
9696
| Case | Result |
9797
|---|---|
9898
| A needs-human task has no PR yet | No PR is opened; the opening is marked `abandoned`; publish returns `draft unsupported`. A ready PR is never opened instead, because it would invite review of work that needs a person. |
99-
| A needs-human task has an open ready PR | The update is dropped; the PR is left as it was; publish returns `draft unsupported` with its number. |
99+
| A needs-human task has an open ready PR | Turning it into a draft is the first step, before the push or any description change, so the refusal leaves the PR exactly as it was; publish returns `draft unsupported` with its number. |
100100
| The check matches and the earlier PR is ready | The result is recorded as usual; publish reports the PR it could not make a draft as `leftReady`. |
101101

102102
## What this slice does not do

‎github/already-fixed.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import { BRANCH, REPOSITORY, SHA } from './validate.ts';
55
import { cutText } from '../core/text.ts';
66

77
/**
8-
* The pre-PR "already fixed" check (design, "Checking whether the issue is already fixed"). It reports a match when
9-
* something other than this task closed the issue, when another open or merged PR links to the issue, or when a new
10-
* commit on the base branch mentions it. The task's own PRs and commits are excluded by repository and number or by
11-
* SHA only. Every read is bounded; a response past a bound, or one that cannot be read, is `unknown`, never clear.
8+
* The pre-PR "already fixed" check (design, "Checking whether the issue is already fixed"). It reports a match when the
9+
* issue is closed (by anything, including this task's own merged PR or own commit, which mean the fix is already in),
10+
* when another open PR or any merged PR links to the issue (this task's own merged PR included), or when a new commit
11+
* on the base branch mentions it. Only this task's own open PRs (by repository and number) and its own commits in the
12+
* base comparison (by SHA) are excluded. Every read is bounded; a response past a bound, or one that cannot be read,
13+
* is `unknown`, never clear.
1214
*/
1315
export type AlreadyFixedMatch =
1416
| { kind: 'closed'; by: string }

‎github/pull-requests.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export interface PullRequestGateway {
1919
open(input: OpenPullRequestInput, signal?: AbortSignal): Promise<OpenedPullRequest>;
2020
/**
2121
* The open PR from `headBranch` into `base`, with the one of `markers` its description carries, or null when there is
22-
* no open PR. An open PR that carries none of them was not opened by codeboost, and is refused.
22+
* no open PR. An open PR that carries none of them (always the case with no markers) was not opened by codeboost, and
23+
* is refused.
2324
*/
2425
findOpened(input: { base: string; headBranch: string; markers: readonly string[] }, signal?: AbortSignal): Promise<(OpenedPullRequest & { marker: string }) | null>;
2526
/** Replaces the title and description of an open PR codeboost opened; marks it ready when `ready`, or a draft when `draft`. */
@@ -79,8 +80,9 @@ export class GhPullRequestGateway implements PullRequestGateway {
7980

8081
#validate(input: { base: string; headBranch: string; marker?: string; markers?: readonly string[] }): void {
8182
if (!BRANCH.test(input.base) || !BRANCH.test(input.headBranch)) throw new Error('Invalid branch name.');
83+
// A lookup may carry no markers (the task has no PR yet); every other call names the PR's own marker.
8284
const markers = input.markers ?? [input.marker];
83-
if (!markers.length || markers.some(marker => typeof marker !== 'string' || !/^<!-- codeboost:[a-z-]+=[0-9a-f-]{36} -->$/.test(marker))) throw new Error('Invalid pull request marker.');
85+
if ((input.markers === undefined && !markers.length) || markers.some(marker => typeof marker !== 'string' || !/^<!-- codeboost:[a-z-]+=[0-9a-f-]{36} -->$/.test(marker))) throw new Error('Invalid pull request marker.');
8486
}
8587

8688
async open(input: OpenPullRequestInput, signal?: AbortSignal): Promise<OpenedPullRequest> {

‎runner/publish.ts‎

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ export class PullRequestPublisher {
9191
// it is still open: GitHub allows one open PR per branch. It is looked up before the check, because an abandoned
9292
// opening's PR links the issue too and has no recorded number; the marker proves it is the task's own.
9393
const candidates = this.#branchRows(prs, branch, this.#config.baseBranch).filter(pr => pr.state !== 'opening');
94-
const live = candidates.length ? await this.#pulls.findOpened({ base: this.#config.baseBranch, headBranch: branch, markers: candidates.map(pr => marker(pr.openingId)) }, signal) : null;
94+
// Always asked, even with no known markers: an open PR on this branch that codeboost did not open is refused here,
95+
// before the push could move it.
96+
const live = await this.#pulls.findOpened({ base: this.#config.baseBranch, headBranch: branch, markers: candidates.map(pr => marker(pr.openingId)) }, signal);
9597
signal?.throwIfAborted();
9698
const earlier = live ? candidates.find(pr => marker(pr.openingId) === live.marker)! : undefined;
9799
const own = new Set(prs.filter(pr => pr.number !== null && pr.repository.toLowerCase() === this.#config.repository.toLowerCase()).map(pr => pr.number!));
@@ -121,23 +123,25 @@ export class PullRequestPublisher {
121123
const ready = leftReady === undefined ? {} : { leftReady };
122124
if (result.outcome !== 'clear') return draft ? { kind: 'draft skipped', result, ...ready } : { kind: 'possibly already fixed', result, ...ready };
123125
if (earlier && live) {
124-
// The push is a refresh's first GitHub write (it moves the open PR's head), so the refresh is recorded before it,
125-
// with no await since recordAlreadyFixed. A task change during the push then cannot strand a half-updated PR.
126+
// A needs-human task's ready PR becomes a draft first, before anything else about it changes: if the repository
127+
// has no drafts, the refusal comes while the PR is still exactly as it was (no push, no new description).
128+
if (draft && !live.draft) {
129+
try { await this.#pulls.markDraft(live.number, { base: this.#config.baseBranch, headBranch: branch, marker: marker(earlier.openingId) }, signal); }
130+
catch (error) { if (error instanceof DraftsUnsupported) return { kind: 'draft unsupported', number: live.number }; throw error; }
131+
signal?.throwIfAborted();
132+
}
133+
// The push is a refresh's first content write (it moves the open PR's head), so the refresh is recorded before it;
134+
// beginRefresh re-reads the task after the draft change's await. A task change during the push cannot strand it.
126135
const stateVersion = this.#store.beginRefresh(identity, { checkId: check.id, openingId: earlier.openingId, headSha: snapshot.head, draft,
127136
...(earlier.state === 'abandoned' ? { adopt: { number: live.number, url: live.url } } : {}) });
128137
await this.#pusher.push(identity, { head: snapshot.head, branch }, signal);
129138
signal?.throwIfAborted();
130-
let pr;
131-
try {
132-
pr = await this.#pulls.refresh(live.number, {
133-
base: earlier.base, headBranch: branch, draft, ready: !draft, headSha: snapshot.head, marker: marker(earlier.openingId),
134-
title: pullRequestTitle(plan), body: pullRequestBody({ plan, marker: marker(earlier.openingId), problems: input.problems }),
135-
}, signal);
136-
} catch (error) {
137-
if (!(error instanceof DraftsUnsupported)) throw error;
138-
this.#store.abandonRefresh(identity, earlier.openingId);
139-
return { kind: 'draft unsupported', number: live.number };
140-
}
139+
// Any failure here, including a draft refusal after the PR was made ready again meanwhile, leaves the update
140+
// recorded as in flight; the next publish drops it and starts again from the draft step above.
141+
const pr = await this.#pulls.refresh(live.number, {
142+
base: earlier.base, headBranch: branch, draft, ready: !draft, headSha: snapshot.head, marker: marker(earlier.openingId),
143+
title: pullRequestTitle(plan), body: pullRequestBody({ plan, marker: marker(earlier.openingId), problems: input.problems }),
144+
}, signal);
141145
const status = this.#store.recordRefreshConfirmed(identity, earlier.openingId, pr, { head: snapshot.head, stateVersion });
142146
return { kind: 'opened', number: pr.number, url: pr.url, draft: pr.draft, status };
143147
}

0 commit comments

Comments
 (0)