Skip to content

Commit 83464d0

Browse files
committed
fix(pm): mailbox_branch_delete fences on the ref's sha, and a refused close-stroke delete names its bare re-send
Contract review should-fixes on #30782: - execute.mjs: mailboxBranchPreflight reads GET /git/ref/heads/{branch} as the last read before the DELETE and refuses unless the ref points at the closed pull's head.sha, so the op deletes that PR's head and never a commit pushed after the close. A ref already absent fences nothing (the DELETE's 422 lands idempotent). Self-test: a moved ref and an unread ref each refused with nothing sent; the good case reads the ref before the DELETE. - mailbox-open.mjs: when a --close stroke that carried the delete fails, closeRemedy prints the one bare mailbox_branch_delete action to send through fleet-write/dispatch.mjs, since --close refuses a closed pull. Self-test pins the action and that the validator accepts it. - The fence list now lives only in ops.mjs's header; execute.mjs and mailbox-open.mjs point at it. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016WV4cwSv11c8FvyR8ZuWhv
1 parent 1465a85 commit 83464d0

3 files changed

Lines changed: 54 additions & 35 deletions

File tree

‎scripts/pm/fleet-write/execute.mjs‎

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -190,15 +190,10 @@
190190
* read the parent and ⛔ never re-send. Every read is an unpaced GET, and the
191191
* one write is paced like every write verb.
192192
*
193-
* ## The mailbox branch delete — `mailbox_branch_delete`, fenced on the PULL and read back
193+
* ## The mailbox branch delete — `mailbox_branch_delete`
194194
*
195-
* The fences (`ops.mjs` header, "The one row that deletes a ref") are facts
196-
* about the pull, so they are AUTHORITATIVE here (`mailboxBranchPreflight`):
197-
* the pull's own `GET` and the open pulls on `{owner}:{branch}`, the last two
198-
* reads before the DELETE, each fence that fails named, the DELETE NOT SENT.
199-
* The 204 is not the landing: `confirmMailboxBranch` reads the ref back, and
200-
* only an absent ref lands — a ref already gone (the DELETE's 422) is an
201-
* idempotent landing, a ref still there is a FAILED action.
195+
* Its fences and its landing are listed once, in `ops.mjs`'s header ("The one
196+
* row that deletes a ref"); they are AUTHORITATIVE here, each that fails named.
202197
*
203198
* ## The advisory rows that write — fenced on the ADVISORY, printing none of its text
204199
*
@@ -906,9 +901,9 @@ export async function confirmSubIssue(req, answer, pre, payload, api, t, { sleep
906901

907902
/**
908903
* The mailbox branch delete's fences on the reads (header): `[]` to send, else one sentence per fence that fails —
909-
* an unread pull is one, judged alone. `pull` and `open` are `rest` answers. Pure.
904+
* an unread pull is one, judged alone. `pull`, `open` and `ref` are `rest` answers; a 404 ref fences nothing. Pure.
910905
*/
911-
export function mailboxBranchFenceFailures({ repo, branch, pull, open }) {
906+
export function mailboxBranchFenceFailures({ repo, branch, pull, open, ref }) {
912907
if (pull?.status !== 200 || !pull.json || typeof pull.json !== 'object') return [`${pull?.call} -> HTTP ${pull?.status}: the pull could not be read`];
913908
const pr = pull.json;
914909
const out = [];
@@ -918,16 +913,20 @@ export function mailboxBranchFenceFailures({ repo, branch, pull, open }) {
918913
if (String(pr.head?.repo?.full_name ?? '').toLowerCase() !== repo.toLowerCase()) out.push(`the pull's head lives on ${pr.head?.repo?.full_name ?? 'no repository'}, not ${repo}`);
919914
if (open?.status !== 200 || !Array.isArray(open.json)) out.push(`${open?.call} -> HTTP ${open?.status}: the open pulls on ${branch} could not be read`);
920915
else if (open.json.length) out.push(`open pull(s) ${open.json.map((p) => `#${p?.number}`).join(', ')} have ${branch} as head — the DELETE would close them`);
916+
const at = ref?.status === 200 && ref.json?.ref === `refs/heads/${branch}` ? ref.json.object?.sha : undefined;
917+
if (ref?.status !== 404 && typeof at !== 'string') out.push(`${ref?.call} -> HTTP ${ref?.status}: the ref ${branch} could not be read`);
918+
else if (ref?.status !== 404 && at !== pr.head?.sha) out.push(`${branch} is at ${at.slice(0, 12)}, not the pull's head ${String(pr.head?.sha ?? 'unread').slice(0, 12)} — a commit pushed after the close is not the mailbox's`);
921919
return out;
922920
}
923921

924-
/** The fences, AUTHORITATIVE (header): the two reads, then `{}` to send or `{ fail }`, the DELETE NOT SENT. Never throws. */
922+
/** The fences, AUTHORITATIVE (header): the three reads, then `{}` to send or `{ fail }`, the DELETE NOT SENT. Never throws. */
925923
export async function mailboxBranchPreflight(req, payload, api, t) {
926924
const { pull, branch } = req.mailboxBranch;
927925
const repo = payload.repo;
928926
const pr = await rest(api, `/repos/${repo}/pulls/${pull}`, {}, t);
929927
const open = await rest(api, `/repos/${repo}/pulls?state=open&head=${encodeURIComponent(`${repo.split('/')[0]}:${branch}`)}`, {}, t);
930-
const failures = mailboxBranchFenceFailures({ repo, branch, pull: pr, open });
928+
const ref = await rest(api, `/repos/${repo}/git/ref/heads/${branch}`, {}, t);
929+
const failures = mailboxBranchFenceFailures({ repo, branch, pull: pr, open, ref });
931930
return failures.length ? { fail: `REFUSED at the relay's fence on #${pull}'s head ${branch}, nothing sent — ${failures.join('; ')}` } : {};
932931
}
933932

@@ -1643,7 +1642,7 @@ const SELF_TEST_BATTERIES = Object.freeze({
16431642
"the run_rerun_failed: the five fences judged HERE on the run and its pull, read immediately before ONE bodiless POST — the pull's current head by its own GET, never the run's embedded copy, and a run naming no pull refused by name; each refused with the POST not sent and accepted on the measured run; a run, pull, baseline or fence that fails is zero writes; the 201 confirmed only by reading attempt 2 back with its failed jobs queued or in progress, naming their job ids; attempt 2 never seen, a failed job completed, or a job list unread is FAILED; no annotation": 21,
16441643
"the hierarchy link: sub_issue_add and sub_issue_remove read the parent's and the child's own answers, then the child's parent (add) or the parent's whole list (remove), immediately before ONE write whose body is the child's REST id from its own answer — never its number, never replace_parent; a pull, a transferred or unread card, a child under another parent, one the parent does not list, or a read that is neither answer is zero writes; a child already under this parent is a no-op landed on the list; the answer confirmed only when the parent's list reads the child back (or no longer does) inside the window; a refused write, a list that never settles or never reads whole is FAILED; no annotation, and the rows ride a stroke beside other ops": 20,
16451644
"the advisory rows: advisory_report and advisory_create are ONE POST of their body each, landed on a 2xx carrying an id — a draft's reading state draft — that the row and a notice annotation name; advisory_collaborators_add, advisory_versions and advisory_state read the advisory first and send ONE PATCH — every existing login kept with the missing ones added, an all-present list a no-op, the versions entries alone, or the one state a legal transition reaches, the reopen and the publish among them — or nothing at all on a fence: an unread, other or wrong-state advisory, an illegal move, a list the versions may not replace or a publish whose versions are not set; each lands only when its answer shows the result, a publish only read back as published with the versions it was sent, and a landed publish alone is annotated with its id; ⛔ the marker in a report's or a draft's summary, description and packages and in every advisory answer's summary, description and vulnerabilities reaches no log line, row, step summary or annotation": 41,
1646-
"the mailbox branch delete: the pull's own answer and the open pulls on its head read immediately before ONE DELETE of the ref under the App token — an open, unlabelled, other-headed or forked pull, an open pull on that head, or a read that does not answer is zero writes with every failing fence named; the DELETE lands only when the ref reads back absent, a ref already gone an idempotent landing and one still there FAILED; and mailbox-open's close stroke — comment, close, delete — leaves no branch, while a head outside claude/mailbox-* sends no DELETE": 7,
1645+
"the mailbox branch delete: the pull's own answer, the open pulls on its head and the ref read immediately before ONE DELETE of the ref under the App token — an open, unlabelled, other-headed or forked pull, an open pull on that head, a ref moved off the pull's head, or a read that does not answer is zero writes with every failing fence named; the DELETE lands only when the ref reads back absent, a ref already gone an idempotent landing and one still there FAILED; and mailbox-open's close stroke — comment, close, delete — leaves no branch, while a head outside claude/mailbox-* sends no DELETE": 7,
16471646
"advisory_read: ONE GET after the sender gate and nothing written; the read, advisory and sender fences judged on that answer — target-write by the maintainer's ruling, a sender off the advisory's collaborators sealed for, one without write refused before the GET, any other gate closed — then the answer sealed to the seat's key and emitted as notice parts the reader parses back, opening byte for byte for that key and read alone; a failed or unsealed seal and an over-cap answer FAILED with nothing emitted; ⛔ the marker in the summary, description and vulnerabilities, and the advisory's id, reach no log line, row, step summary or annotation, and the sealed answer alone carries no marker": 18,
16481647
});
16491648
// Raised 17 → 18 with the governed-review follow-up battery, 18 → 19 with the run_rerun_failed battery, 19 → 20 with the
@@ -2510,37 +2509,41 @@ export async function selfTest() {
25102509
}
25112510

25122511
// ── the mailbox branch delete ───────────────────────────────────────────
2513-
battery("the mailbox branch delete: the pull's own answer and the open pulls on its head read immediately before ONE DELETE of the ref under the App token — an open, unlabelled, other-headed or forked pull, an open pull on that head, or a read that does not answer is zero writes with every failing fence named; the DELETE lands only when the ref reads back absent, a ref already gone an idempotent landing and one still there FAILED; and mailbox-open's close stroke — comment, close, delete — leaves no branch, while a head outside claude/mailbox-* sends no DELETE");
2512+
battery("the mailbox branch delete: the pull's own answer, the open pulls on its head and the ref read immediately before ONE DELETE of the ref under the App token — an open, unlabelled, other-headed or forked pull, an open pull on that head, a ref moved off the pull's head, or a read that does not answer is zero writes with every failing fence named; the DELETE lands only when the ref reads back absent, a ref already gone an idempotent landing and one still there FAILED; and mailbox-open's close stroke — comment, close, delete — leaves no branch, while a head outside claude/mailbox-* sends no DELETE");
25142513
{
25152514
const PR = 30141;
25162515
const BR = 'claude/mailbox-ui-3';
25172516
const PULL = `GET /repos/${REPO}/pulls/${PR}`;
25182517
const OPEN = `GET /repos/${REPO}/pulls`;
25192518
const DEL = `DELETE /repos/${REPO}/git/refs/heads/${BR}`;
25202519
const BACK = `GET /repos/${REPO}/git/ref/heads/${BR}`;
2521-
const box = (o = {}) => ({ number: PR, state: 'closed', labels: [{ name: 'pm:mailbox' }, { name: 'pinned' }], head: { ref: BR, repo: { full_name: REPO } }, ...o });
2522-
const good = { ...allowed, [PULL]: { status: 200, json: box() }, [OPEN]: { status: 200, json: [] }, [DEL]: { status: 204, json: null }, [BACK]: { status: 404, json: { message: 'Not Found' } } };
2520+
const HEAD = 'a'.repeat(40);
2521+
const at = (sha) => ({ status: 200, json: { ref: `refs/heads/${BR}`, object: { sha, type: 'commit' } } });
2522+
const box = (o = {}) => ({ number: PR, state: 'closed', labels: [{ name: 'pm:mailbox' }, { name: 'pinned' }], head: { ref: BR, sha: HEAD, repo: { full_name: REPO } }, ...o });
2523+
const good = { ...allowed, [PULL]: { status: 200, json: box() }, [OPEN]: { status: 200, json: [] }, [DEL]: { status: 204, json: null }, [BACK]: [at(HEAD), { status: 404, json: { message: 'Not Found' } }] };
25232524
const drop = base([{ op: 'mailbox_branch_delete', pull: PR, branch: BR }]);
25242525
const ok = await run(drop, good);
2525-
t('the good case: the pull, then the open pulls on its head, then ONE DELETE of the ref under the App token, then the ref read back absent — exit 0, the row naming it deleted', [ok.exit, ok.seen.slice(1).map((s) => s.call), writes(ok.seen).map((s) => [s.call, s.body, s.auth]), ok.rows[0].result, ok.annotations], [EXIT_OK, [PULL, OPEN, DEL, BACK], [[DEL, null, `Bearer ${TOKEN}`]], `deleted ${BR} (read back absent)`, []]);
2526+
t("the good case: the pull, the open pulls on its head, the ref at the pull's head, then ONE DELETE of the ref under the App token, then the ref read back absent — exit 0, the row naming it deleted", [ok.exit, ok.seen.slice(1).map((s) => s.call), writes(ok.seen).map((s) => [s.call, s.body, s.auth]), ok.rows[0].result, ok.annotations], [EXIT_OK, [PULL, OPEN, BACK, DEL, BACK], [[DEL, null, `Bearer ${TOKEN}`]], `deleted ${BR} (read back absent)`, []]);
25262527
const urls = [];
25272528
await mailboxBranchPreflight(OPS.mailbox_branch_delete.requests({ op: 'mailbox_branch_delete', pull: PR, branch: BR }, REPO)[0], { repo: REPO }, 'https://api.test', { token: TOKEN, fetch: async (url) => (urls.push(url), { status: 200, headers: new Headers(), json: async () => [] }) });
25282529
t("the open-pulls read is filtered to this repository's owner and the branch — an unfiltered or mis-owned read would answer for other heads", urls[1], `https://api.test/repos/${REPO}/pulls?state=open&head=objectstack-ai%3Aclaude%2Fmailbox-ui-3`);
25292530
const fences = [
25302531
[{ [PULL]: { status: 200, json: box({ state: 'open' }) } }, 'the pull is open, not closed'],
25312532
[{ [PULL]: { status: 200, json: box({ labels: [{ name: 'pinned' }] }) } }, 'does not carry pm:mailbox'],
2532-
[{ [PULL]: { status: 200, json: box({ head: { ref: 'claude/mailbox-ui-2', repo: { full_name: REPO } } }) } }, "the pull's head is claude/mailbox-ui-2, not claude/mailbox-ui-3"],
2533-
[{ [PULL]: { status: 200, json: box({ head: { ref: BR, repo: { full_name: 'someone/objectstack' } } }) } }, "head lives on someone/objectstack, not objectstack-ai/objectstack"],
2533+
[{ [PULL]: { status: 200, json: box({ head: { ref: 'claude/mailbox-ui-2', sha: HEAD, repo: { full_name: REPO } } }) } }, "the pull's head is claude/mailbox-ui-2, not claude/mailbox-ui-3"],
2534+
[{ [PULL]: { status: 200, json: box({ head: { ref: BR, sha: HEAD, repo: { full_name: 'someone/objectstack' } } }) } }, "head lives on someone/objectstack, not objectstack-ai/objectstack"],
2535+
[{ [BACK]: at('b'.repeat(40)) }, `${BR} is at bbbbbbbbbbbb, not the pull's head aaaaaaaaaaaa — a commit pushed after the close is not the mailbox's`],
2536+
[{ [BACK]: { status: 503, json: { message: 'down' } } }, 'the ref claude/mailbox-ui-3 could not be read'],
25342537
[{ [OPEN]: { status: 200, json: [{ number: 30999 }] } }, 'open pull(s) #30999 have claude/mailbox-ui-3 as head — the DELETE would close them'],
25352538
[{ [PULL]: { status: 404, json: { message: 'Not Found' } } }, 'the pull could not be read'],
25362539
[{ [OPEN]: { status: 503, json: { message: 'down' } } }, 'the open pulls on claude/mailbox-ui-3 could not be read'],
25372540
];
25382541
const fenced = await Promise.all(fences.map(async ([over, phrase]) => { const r = await run(drop, { ...good, ...over }); return [r.exit, writes(r.seen).length, r.rows[0]?.status, r.rows[0]?.result.includes(phrase)]; }));
2539-
t('⛔ each fence refuses with the DELETE NOT SENT and names itself: an open pull, no pm:mailbox, another head, a forked head, an open pull on the head, an unread pull, an unread open-pulls list', fenced, fences.map(() => [EXIT_ACTION_FAILED, 0, 'not sent', true]));
2540-
const kept = await run(drop, { ...good, [BACK]: { status: 200, json: { ref: `refs/heads/${BR}` } } });
2542+
t('⛔ each fence refuses with the DELETE NOT SENT and names itself: an open pull, no pm:mailbox, another head, a forked head, an open pull on the head, a ref moved past the head, an unread ref, an unread pull, an unread open-pulls list', fenced, fences.map(() => [EXIT_ACTION_FAILED, 0, 'not sent', true]));
2543+
const kept = await run(drop, { ...good, [BACK]: at(HEAD) });
25412544
t('⛔ a 204 whose ref still reads back is a FAILED action, never a landing', [kept.exit, kept.rows[0].status, kept.rows[0].result.includes('the branch is still there')], [EXIT_ACTION_FAILED, 204, true]);
2542-
const gone = await run(drop, { ...good, [DEL]: { status: 422, json: { message: 'Reference does not exist' } } });
2543-
t('a ref already gone — the DELETE answers 422, the ref reads back absent — is an idempotent landing', [gone.exit, gone.rows[0].result], [EXIT_OK, 'already absent (idempotent)']);
2545+
const gone = await run(drop, { ...good, [BACK]: { status: 404, json: { message: 'Not Found' } }, [DEL]: { status: 422, json: { message: 'Reference does not exist' } } });
2546+
t('a ref already gone — absent before (no sha to fence), the DELETE answering 422, absent after — is an idempotent landing', [gone.exit, gone.rows[0].result], [EXIT_OK, 'already absent (idempotent)']);
25442547
const { planClose } = await import('../mailbox-open.mjs');
25452548
const closing = { ...good, [`POST /repos/${REPO}/issues/${PR}/comments`]: { status: 201, json: { id: 9 } }, [`PATCH /repos/${REPO}/issues/${PR}`]: { status: 200, json: { number: PR, state: 'closed' } } };
25462549
const closed = await run(base(planClose({ repo: REPO, pull: PR, head: BR }).actions), closing);

‎scripts/pm/fleet-write/ops.mjs‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,16 @@
6666
* widens nothing. The row creates no ref and writes no content: the `/git/`
6767
* family of `REFUSED_PATH_FAMILIES` refuses every path but exactly one
6868
* `refs/heads/` ref of `MAILBOX_BRANCH_SHAPE` (`claude/mailbox-*`), held there
69-
* before the dispatch too. The fences are facts about the PULL, so the
70-
* executor judges them on the pull's own `GET` and the open pulls on that
71-
* head, the last reads before the DELETE (`mailboxBranchPreflight`): the pull
72-
* is closed, carries `pm:mailbox`, its head is `branch` on this repository,
73-
* and no OPEN pull has that head (the DELETE would close it). Anything else is
74-
* refused, the DELETE not sent. It lands only when the ref reads back absent.
69+
* before the dispatch too. The fences, listed HERE only, are facts about the
70+
* pull and the ref, judged by the executor on the pull's own `GET`, the open
71+
* pulls on that head and the ref, the last three reads before the DELETE
72+
* (`mailboxBranchPreflight`): the pull is closed, carries `pm:mailbox`, its
73+
* head is `branch` on this repository, no OPEN pull has that head (the DELETE
74+
* would close it), and the ref points at the pull's `head.sha` (a commit pushed
75+
* after the close is not the mailbox's; a ref already absent fences nothing).
76+
* Anything else is refused, the DELETE not sent. It lands only when the ref
77+
* reads back absent (`confirmMailboxBranch`): a ref already gone (the DELETE's
78+
* 422) is an idempotent landing, one still there FAILED.
7579
*
7680
* ## The rows whose LATER requests read the FIRST's answer — `pr_create` with `assignees` or `labels`
7781
*

0 commit comments

Comments
 (0)