Skip to content

Commit 0be7147

Browse files
mchwangclaude
andcommitted
Address Copilot round 5 on F2d: verified draft state, re-read before drafting, title mentions, deadline budget
- markDraft and refresh poll the read-back for the requested draft or ready state and fail if GitHub never shows it, so a publish stays retryable instead of recording a change that did not apply. - Re-read the task, its review and its head right before drafting the earlier PR; a stale publish no longer touches the current PR. - Neutralise @-mentions in the unfenced PR title. - The check's runner waits 1 s before SIGKILL and 0.5 s for pipes, so the whole check settles in 13.5 s, below the 15 s request budget. - AGENTS.md: grace periods count inside deadlines; read back external state changes; neutralise mentions in unfenced titles. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent dcc2b0c commit 0be7147

9 files changed

Lines changed: 77 additions & 20 deletions

File tree

‎AGENTS.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ Every reproduced race requires a failing-before and passing-after regression. As
7373
- Check an operation's source-state preconditions before any shortcut or early return that writes state or reports success, not only on the main path.
7474
- Honour a cancellation signal that is already aborted before the first durable write, not only after awaits: a path with no await otherwise writes after the caller cancelled.
7575
- Batch and briefly cache read-only status probes, and give the combined operation an overall deadline below the serving request timeout.
76+
- Count the subprocess shutdown grace periods (SIGTERM-to-SIGKILL wait, pipe drain) inside that overall deadline: the operation ends when its processes have settled, not when the abort fires.
77+
- After an external state change (ready, draft, close), read the record back and require the new state before recording success; a command that exits 0 does not prove the change applied.
7678
- Budget a multi-stage validation across all sequential stages; giving each stage the full request allowance does not create an overall deadline.
7779
- Preserve the distinction between an explicit unbound identity and missing or malformed authorization metadata. Missing or malformed identities must fail closed.
7880
- Validate every field used to classify an external record as clear, including enum values and required nullable fields. Partial records and malformed policy objects must fail closed.
@@ -109,7 +111,7 @@ Every reproduced race requires a failing-before and passing-after regression. As
109111

110112
- When a subprocess reports a problem only as a warning and carries on, decide pass or fail by what each message means for the result, not by whether anything was printed: fail on messages that mean it did less than it should (for example could not read a path), and let through messages about harmless input the agent controls. Test both a benign case and a failing case, and filter the output as it arrives so that no volume of benign messages can push a failure out of a bounded buffer.
111113
- Quote or escape agent-controlled text (file names, paths, branch names) wherever it lands in output that people or tools parse, such as diffs, notices, logs or reports, so it cannot forge that output's structure.
112-
- Neutralise issue references (`#N`, `GH-N`, `owner/repo#N`, issue URLs) in any text codeboost writes that can become a commit message, such as a PR title or description. Code fences do not protect commit messages, and GitHub closes issues from closing keywords in default-branch commits.
114+
- Neutralise issue references (`#N`, `GH-N`, `owner/repo#N`, issue URLs) in any text codeboost writes that can become a commit message, such as a PR title or description, and neutralise @-mentions in any of that text that is not fenced (a title). Code fences do not protect commit messages, and GitHub closes issues from closing keywords in default-branch commits.
113115
- Never spread a collection whose size follows unbounded input into function arguments (`Math.max(...runs)`); engines limit the argument count, so use a loop.
114116
- A hardened Git invocation must also keep Git out of nested repositories and populated submodules, whose own config and hooks are the agent's: pass `--ignore-submodules` on the command line (the config default does not bind plumbing or override `.gitmodules`), and never run Git with a nested repository as its working directory.
115117

‎core/pull-request-body.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,14 @@ function planText(plan: Plan, full: boolean): string {
4949
}
5050
const planTextSafe = (plan: Plan, full: boolean) => neutralizeReferences(planText(plan, full));
5151

52-
/** Single line, no control characters, no issue references except its own, bounded in code points. */
52+
/** A title is not fenced, so an @-mention in it would notify: `@name` becomes `@name`. */
53+
export function neutralizeMentions(text: string): string {
54+
return text.replace(/@(?=[A-Za-z0-9])/g, '@');
55+
}
56+
57+
/** Single line, no control characters, no issue references except its own, no mentions, bounded in UTF-16 units. */
5358
export function pullRequestTitle(plan: Plan): string {
54-
const summary = neutralizeReferences(plan.summary.replace(/[\u0000-\u001f\u007f]+/g, ' ').replace(/\s+/g, ' ').trim()) || 'codeboost plan';
59+
const summary = neutralizeMentions(neutralizeReferences(plan.summary.replace(/[\u0000-\u001f\u007f]+/g, ' ').replace(/\s+/g, ' ').trim())) || 'codeboost plan';
5560
const suffix = ` (#${plan.issue})`;
5661
return cut(summary, MAX_TITLE - suffix.length) + suffix;
5762
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ A commit mentions the issue with `#12`, `GH-12`, `owner/repo#12`, or the issue U
4242
- a closed issue with no close event;
4343
- a linked item that is missing, of an unknown type, or in a malformed response;
4444
- a GitHub error or invalid JSON;
45-
- the whole check running past its single deadline (12 seconds by default, below the 15-second request budget). Reaching the deadline stops the running `gh` call.
45+
- the whole check running past its single deadline (12 seconds by default). For the check the runner waits 1 second after SIGTERM before SIGKILL, and half a second for inherited pipes, so the whole check settles within 13.5 seconds, below the 15-second request budget. Reaching the deadline stops the running `gh` call.
4646

4747
A cancelled check throws. It does not return `unknown`.
4848

@@ -69,7 +69,7 @@ Each opening or refresh owns the task state version at the moment it passed step
6969

7070
After an update, the read-back polls up to 5 times, half a second apart, until GitHub shows the pushed head, because GitHub updates a PR's head a moment after a push.
7171

72-
When the check matches (or is unknown) and the task's earlier PR is open and ready for review, publish turns it back into a draft. A task that is not being published as ready never leaves its PR ready for review. This happens before the check result is recorded: if it fails, the task is still running, and a retry checks again and repeats it. A PR-number mismatch is refused before any GitHub change.
72+
When the check matches (or is unknown) and the task's earlier PR is open and ready for review, publish turns it back into a draft. A task that is not being published as ready never leaves its PR ready for review. This happens before the check result is recorded, after re-reading the task, its review and its head: if the task changed during the check, nothing is drafted. If the draft change fails, or GitHub does not show the PR as a draft afterwards, the task is still running, and a retry checks again and repeats it. A refresh likewise fails when GitHub does not show the requested draft or ready state. A PR-number mismatch is refused before any GitHub change.
7373

7474
The adapter refuses any answer for a PR that is not open. A PR closed between the lookup and the update is never recorded as the task's review PR.
7575

@@ -83,7 +83,7 @@ The description starts with the marker and `Fixes #<issue>`. The plan follows, i
8383

8484
GitHub ignores closing keywords and @-mentions inside code. So plan text or agent output cannot notify people from the description, and cannot end the block: the fence is longer than any run of backticks in the text.
8585

86-
Fences do not protect commit messages. A squash or merge commit can carry the PR title and description, and GitHub acts on closing keywords in default-branch commit messages. So every issue reference in the title's summary, the plan and the problems is neutralised: `#7` becomes `#7`, `GH-7` gets a non-breaking hyphen, and `/issues/7` or `/pull/7` gets a division slash. Only the task's own `Fixes #<issue>` line and the title's `(#<issue>)` remain real references.
86+
Fences do not protect commit messages. A squash or merge commit can carry the PR title and description, and GitHub acts on closing keywords in default-branch commit messages. So every issue reference in the title's summary, the plan and the problems is neutralised: `#7` becomes `#7`, `GH-7` gets a non-breaking hyphen, and `/issues/7` or `/pull/7` gets a division slash. Only the task's own `Fixes #<issue>` line and the title's `(#<issue>)` remain real references. The title is not fenced, so an @-mention in it would notify: `@name` becomes `@name` there.
8787

8888
Titles and problems are cut by code point, never inside a surrogate pair. An empty summary becomes `codeboost plan`.
8989

‎github/already-fixed.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ export const MAX_TIMELINE_ITEMS = 100;
3232
export const MAX_BASE_COMMITS = 250;
3333
/** One deadline for the whole check, below the 15-second serving request budget (`web/server.ts`). */
3434
export const DEFAULT_CHECK_DEADLINE_MS = 12_000;
35+
/**
36+
* After the deadline aborts a `gh` call, the runner may wait this long for SIGTERM, then this long for inherited pipes.
37+
* The deadline plus both stays below the 15-second serving request budget (12 + 1 + 0.5 = 13.5 s).
38+
*/
39+
export const CHECK_KILL_GRACE_MS = 1_000, CHECK_PIPE_GRACE_MS = 500;
3540
const PAGE = 100;
3641

3742
const TIMELINE_QUERY = `query($owner: String!, $name: String!, $number: Int!) {
@@ -96,7 +101,7 @@ export class GhAlreadyFixedGateway implements AlreadyFixedGateway {
96101
this.repository = config.repository;
97102
this.deadlineMs = config.deadlineMs ?? DEFAULT_CHECK_DEADLINE_MS;
98103
// runWithInput escalates to SIGKILL, so an aborted stage always settles and the check's single deadline holds.
99-
this.run = run ?? ((args, options) => runWithInput('gh', args, { timeout: 30_000, maxBuffer: 8 * 1024 * 1024, signal: options?.signal, env: ghEnvironment() }));
104+
this.run = run ?? ((args, options) => runWithInput('gh', args, { timeout: 30_000, maxBuffer: 8 * 1024 * 1024, signal: options?.signal, env: ghEnvironment(), killGraceMs: CHECK_KILL_GRACE_MS, pipeGraceMs: CHECK_PIPE_GRACE_MS }));
100105
}
101106

102107
async #json(args: readonly string[], signal?: AbortSignal): Promise<unknown> {

‎github/pull-requests.ts‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,23 +104,30 @@ export class GhPullRequestGateway implements PullRequestGateway {
104104
// A ready PR whose task went back to needs human becomes a draft again; a draft whose task is ready leaves draft.
105105
if (input.ready && patched.draft) await this.run(['pr', 'ready', String(number), '--repo', this.repository], { signal });
106106
else if (input.draft && !patched.draft) await this.run(['pr', 'ready', String(number), '--undo', '--repo', this.repository], { signal });
107-
return this.#readBack(number, input, input.headSha, signal);
107+
const wantDraft = input.ready ? false : input.draft ? true : undefined;
108+
const pr = await this.#readBack(number, input, found => (input.headSha === undefined || found.headSha === input.headSha) && (wantDraft === undefined || found.draft === wantDraft), signal);
109+
// A draft change GitHub has not applied fails the refresh, so it stays unconfirmed and the next publish repeats it.
110+
if (wantDraft !== undefined && pr.draft !== wantDraft) throw new Error(`GitHub did not ${wantDraft ? 'turn the pull request into a draft' : 'mark the pull request ready'}.`);
111+
return pr;
108112
}
109113

110114
async markDraft(number: number, input: { base: string; headBranch: string; marker: string }, signal?: AbortSignal): Promise<OpenedPullRequest> {
111115
this.#validate(input);
112116
if (!Number.isSafeInteger(number) || number < 1) throw new Error('Invalid pull request number.');
113-
const current = await this.#readBack(number, input, undefined, signal);
114-
if (!current.draft) await this.run(['pr', 'ready', String(number), '--undo', '--repo', this.repository], { signal });
115-
return current.draft ? current : this.#readBack(number, input, undefined, signal);
117+
const current = await this.#readBack(number, input, () => true, signal);
118+
if (current.draft) return current;
119+
await this.run(['pr', 'ready', String(number), '--undo', '--repo', this.repository], { signal });
120+
const pr = await this.#readBack(number, input, found => found.draft, signal);
121+
if (!pr.draft) throw new Error('GitHub did not turn the pull request into a draft.');
122+
return pr;
116123
}
117124

118-
/** Reads the PR back; when `headSha` is given, polls briefly until GitHub shows it, then returns the last answer. */
119-
async #readBack(number: number, input: { base: string; headBranch: string; marker: string }, headSha: string | undefined, signal?: AbortSignal): Promise<OpenedPullRequest> {
125+
/** Reads the PR back, polling briefly until `done` holds (GitHub applies pushes and draft changes a moment later); returns the last answer. */
126+
async #readBack(number: number, input: { base: string; headBranch: string; marker: string }, done: (pr: OpenedPullRequest) => boolean, signal?: AbortSignal): Promise<OpenedPullRequest> {
120127
for (let poll = 1; ; poll++) {
121128
const { body, ...pr } = this.#pull(await this.#json(['api', '-H', 'Accept: application/vnd.github+json', `repos/${this.repository}/pulls/${number}`], signal), input);
122129
if (pr.number !== number || !body.includes(input.marker)) throw new Error('GitHub returned a different pull request.');
123-
if (headSha === undefined || pr.headSha === headSha || poll >= HEAD_POLLS) return pr;
130+
if (done(pr) || poll >= HEAD_POLLS) return pr;
124131
await new Promise<void>((resolve, reject) => {
125132
signal?.throwIfAborted();
126133
const onAbort = () => { clearTimeout(timer); reject(signal!.reason); };

‎runner/publish.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export class PullRequestPublisher {
7474
const recovered = await this.#recover(identity, signal);
7575
if (recovered) return recovered;
7676
const task = this.#store.getTask(identity), snapshot = this.#store.getSnapshot(identity), plan = this.#store.getPlan(identity);
77+
const reviewVersion = this.#store.reviewVersion(identity);
7778
if (task.status !== (draft ? 'needs human' : 'running')) throw new GuardRefusal(`A ${draft ? 'draft ' : ''}pull request cannot be opened while the task is ${task.status}.`);
7879
if (snapshot.head === snapshot.base) {
7980
if (!draft) this.#store.transitionTask(identity, task.stateVersion, 'needs human');
@@ -99,8 +100,11 @@ export class PullRequestPublisher {
99100
if (earlier && live && earlier.state === 'opened' && live.number !== earlier.number) throw new GuardRefusal('GitHub returned a different pull request for this branch.');
100101
// A task that is not published as ready never leaves its PR ready for review: on a match its earlier ready PR becomes
101102
// a draft. This comes before the result is recorded, so if it fails the task is still running and a retry repeats it.
102-
// Making a PR a draft only lowers what it offers, so it needs no re-read of the task first.
103-
const drafted = result.outcome !== 'clear' && earlier && live && !live.draft
103+
// It is still a GitHub change for this task, so the task is re-read first: a reassignment or review during the check
104+
// means this publish is stale and must not touch the current generation's PR.
105+
const needsDraft = result.outcome !== 'clear' && earlier && live && !live.draft;
106+
if (needsDraft) this.#store.assertUnchangedSince(identity, { stateVersion: task.stateVersion, reviewVersion, snapshotId: snapshot.id, draft });
107+
const drafted = needsDraft
104108
? await this.#pulls.markDraft(live.number, { base: this.#config.baseBranch, headBranch: branch, marker: marker(earlier.openingId) }, signal) : null;
105109
signal?.throwIfAborted();
106110
const check = this.#store.recordAlreadyFixed(identity, task.stateVersion, { snapshotId: snapshot.id, draft, result });

‎runner/store.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,16 @@ export class Store {
10341034
return this.#task(key).status as TaskStatus;
10351035
});
10361036
}
1037+
/** The task, its review and its head are exactly as a publish read them before its last await. */
1038+
assertUnchangedSince(identity: PlanIdentity, input: { stateVersion: number; reviewVersion: number; snapshotId: string; draft: boolean }): void {
1039+
const key = identityKey(identity);
1040+
this.#transaction(() => {
1041+
this.#assertPublishable(key, this.#task(key), input.stateVersion, input.draft);
1042+
const plan = this.#current(key);
1043+
if (plan.review_version !== input.reviewVersion) throw new GuardRefusal('The review changed after the check. Reload before writing.');
1044+
if (plan.snapshot_id !== input.snapshotId) throw new GuardRefusal('The task head changed during the check.');
1045+
});
1046+
}
10371047
/** Records that the task's open PR is now a draft (after a check matched). The task status does not change. */
10381048
recordPullRequestDraft(identity: PlanIdentity, openingId: string, number: number, draft: boolean): void {
10391049
const key = identityKey(identity);

‎test/already-fixed.test.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from 'vitest';
2-
import { DEFAULT_CHECK_DEADLINE_MS, GhAlreadyFixedGateway, MAX_BASE_COMMITS, mentionsIssue, type AlreadyFixedInput } from '../github/already-fixed.ts';
2+
import { CHECK_KILL_GRACE_MS, CHECK_PIPE_GRACE_MS, DEFAULT_CHECK_DEADLINE_MS, GhAlreadyFixedGateway, MAX_BASE_COMMITS, mentionsIssue, type AlreadyFixedInput } from '../github/already-fixed.ts';
33

44
const sha = (n: number) => n.toString(16).padStart(40, '0');
55
const repo = 'Owner/Repo';
@@ -123,7 +123,8 @@ describe('the pre-PR already-fixed check', () => {
123123
expect(aborted).toBe(true);
124124
});
125125
it('defaults to a deadline below the 15-second serving request budget', () => {
126-
expect(DEFAULT_CHECK_DEADLINE_MS).toBeLessThan(15_000);
126+
// The deadline plus the runner's SIGTERM and pipe grace periods stays below the budget.
127+
expect(DEFAULT_CHECK_DEADLINE_MS + CHECK_KILL_GRACE_MS + CHECK_PIPE_GRACE_MS).toBeLessThan(15_000);
127128
expect(new GhAlreadyFixedGateway({ repository: repo }).deadlineMs).toBe(DEFAULT_CHECK_DEADLINE_MS);
128129
});
129130
it('returns only after the stopped stage has settled', async () => {

0 commit comments

Comments
 (0)