Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 28 additions & 14 deletions .github/actions/apply-review-hold/hold.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,20 @@ const PULL_QUERY = `query($owner: String!, $name: String!, $number: Int!) {
const EDITED_QUERY =
"query($id: ID!) { node(id: $id) { ... on IssueComment { lastEditedAt } } }";

// Whether comment `c` was ever edited: "unedited" when GraphQL answers
// lastEditedAt null, "edited" when it answers a time, else "unknown" (a
// failed read, no node, or no lastEditedAt).
async function editState(github, c) {
try {
const node = (await github.graphql(EDITED_QUERY, { id: String(c.node_id) }))
?.node;
if (!node || !("lastEditedAt" in node)) return "unknown";
return node.lastEditedAt === null ? "unedited" : "edited";
} catch {
return "unknown";
}
}

// The issue one plain `Refs: #<n>` line names, or null: claude-code-plugins@eda837d4c8
// plugins/source-control/skills/babysit-prs/scripts/babysit_merge_policy.py:1851-1895
// refs_issue. A line counts from its first column, outside an HTML comment
Expand Down Expand Up @@ -161,20 +175,11 @@ async function costDecided(github, owner, repo, number, env, prComments) {
c.performed_via_github_app == null &&
trusted.has(c.user.id) &&
DECISION_LINE.test(String(c.body ?? "").trimStart());
const unedited = async (c) => {
try {
const node = (await github.graphql(EDITED_QUERY, { id: String(c.node_id) }))
?.node;
return Boolean(node) && "lastEditedAt" in node && node.lastEditedAt === null;
} catch {
return false;
}
};
const newest = async (list) => {
for (const c of list
.filter(candidate)
.sort((a, b) => Date.parse(b.created_at) - Date.parse(a.created_at)))
if (await unedited(c)) return c;
if ((await editState(github, c)) === "unedited") return c;
return null;
};
const own = await newest(prComments);
Expand Down Expand Up @@ -217,15 +222,17 @@ const MAX_COMPARE_FILES = 300;
const BRANCH = /^(?!-)(?!.*\.\.)[A-Za-z0-9._/-]{1,255}$/u;

// Whether `decision` covers the head: { carry, why }. The decided head is
// the sha of this lane's newest cost hold marker posted before the decision.
// the sha of this lane's newest cost hold marker posted before the decision,
// and only while that marker is unedited: an edited marker, or one whose
// edit state cannot be read, carries nothing, and no older marker is used.
// The decision carries when the head is that sha, or when the pull request's
// net diff against its base branch is the same at both: compare
// base...decided and base...head (three-dot, so each is the diff from its
// merge base) list the same files, each with the same status,
// previous_filename and patch text. So a base-only merge or a rebase that
// leaves every patch as it was carries, and any other change does not. At
// most two compare reads; any failure, a missing patch, or a list of 300 or
// more files carries nothing.
// leaves every patch as it was carries, and any other change does not. One
// edit read and at most two compare reads; any failure, a missing patch, or
// a list of 300 or more files carries nothing.
async function carryDecision(github, owner, repo, env, prComments, decision) {
const cutoff = Date.parse(decision.created_at);
if (!Number.isFinite(cutoff)) return { carry: false, why: "the decision has no time" };
Expand All @@ -234,6 +241,7 @@ async function carryDecision(github, owner, repo, env, prComments, decision) {
"u",
);
let decided = null;
let held = null;
let at = -Infinity;
for (const c of prComments) {
if (c?.user?.login !== "github-actions[bot]") continue;
Expand All @@ -242,10 +250,16 @@ async function carryDecision(github, owner, repo, env, prComments, decision) {
if (!m || !m[2].split(",").includes("cost")) continue;
if (!(t < cutoff) || t <= at) continue;
decided = m[1];
held = c;
at = t;
}
if (!decided)
return { carry: false, why: "no cost hold marker of this lane precedes the decision" };
const state = await editState(github, held);
if (state === "edited")
return { carry: false, why: "the decided-head marker was edited" };
if (state !== "unedited")
return { carry: false, why: "could not confirm the marker is unedited" };
if (decided === env.HEAD_SHA)
return { carry: true, why: `the head is the decided head ${decided}` };
const base = String(env.BASE_REF ?? "");
Expand Down
78 changes: 71 additions & 7 deletions .github/scripts/apply-review-hold.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ const TRUST_PATHS = [

// `comments` are the pull request's (#7); `issueComments` maps another
// issue number to its comments; `pull` is the GraphQL pull request;
// `edited` holds the node ids of edited comments; `pullNumbers` the numbers
// `edited` holds the node ids of edited comments; `failEdited` those whose
// edit read fails; `pullNumbers` the numbers
// that are pull requests, not issues; `misnumbered` the numbers whose read
// answers another number; `compares` maps a basehead to its compare
// response data.
Expand All @@ -59,6 +60,7 @@ function fakeGithub({
closingIssuesReferences: { totalCount: 0, nodes: [] },
},
edited = new Set(),
failEdited = new Set(),
pullNumbers = new Set([7]),
misnumbered = new Set(),
compares = {},
Expand All @@ -71,6 +73,8 @@ function fakeGithub({
calls,
graphql: async (query, variables) => {
calls.push(["graphql", variables]);
if (/lastEditedAt/u.test(query) && failEdited.has(variables.id))
throw new Error("HTTP 502");
if (/lastEditedAt/u.test(query))
return {
node: {
Expand Down Expand Up @@ -471,12 +475,16 @@ test("a cost decision on the pull request keeps the cost hold from coming back",
assert.equal(error, undefined);
assert.equal(core.outputs.hold, "false");
assert.deepEqual(writes, []);
// The lookup's reads, the pull request's comments and one edit check,
// then hold-paths' own file list: the head is the decided head, so no
// compare.
// The lookup's reads, the pull request's comments and the decision's edit
// check, then the marker's edit check, then hold-paths' own file list: the
// head is the decided head, so no compare.
assert.deepEqual(
github.calls.map(([name]) => name),
["listComments", "graphql", "listFiles", "get"],
["listComments", "graphql", "graphql", "listFiles", "get"],
);
assert.deepEqual(
github.calls.filter(([name]) => name === "graphql").map(([, v]) => v.id),
["IC_1", "IC_100"],
);
});

Expand All @@ -500,6 +508,7 @@ test("a cost decision on the Refs issue that names the pull request keeps the co
"issues.get",
"listComments",
"graphql",
"graphql",
"listFiles",
"get",
],
Expand Down Expand Up @@ -889,14 +898,69 @@ test("an unusable base branch name reads no compare and keeps the cost hold", as
test("the carry check reads at most two compares and nothing else", async () => {
const { github } = await carryCase(atDecided);
const names = github.calls.map(([name]) => name);
// The pull request's comments, one edit check, two compares, then
// hold-paths' own file list.
// The pull request's comments, the decision's and the marker's edit
// checks, two compares, then hold-paths' own file list.
assert.deepEqual(names, [
"listComments",
"graphql",
"graphql",
"compare",
"compare",
"listFiles",
"get",
]);
});

// The decided head comes from a marker comment, which a writer can edit:
// an edited marker is not trusted.
test("an edited marker at the head's sha keeps the cost hold, though the decision exists", async () => {
const { core, writes, github } = await apply(costEnv(), {
comments: [marker(), decision()],
edited: new Set(["IC_100"]),
});
assert.equal(core.outputs.hold, "true");
assert.equal(core.outputs.criteria, "cost");
// The label comes back; the marker for this head and criteria exists.
assert.deepEqual(
writes.map(([name]) => name),
["addLabels"],
);
assert.equal(compares(github).length, 0);
assert.match(
core.infos.join("\n"),
/no recorded cost decision carries \(the decided-head marker was edited\)/u,
);
});

test("an edited newest marker keeps the cost hold, with no fallback to an older unedited one", async () => {
const { core, github } = await carryCase(atDecided, {
comments: [
// Would carry: at the head, unedited, before the decision.
marker({ id: 101, sha: LATER, createdAt: "2026-10-23T10:30:00Z" }),
marker({ id: 102 }),
decision(),
],
edited: new Set(["IC_102"]),
});
assert.equal(core.outputs.hold, "true");
assert.equal(compares(github).length, 0);
assert.deepEqual(
github.calls.filter(([name]) => name === "graphql").map(([, v]) => v.id),
["IC_1", "IC_102"],
);
assert.match(core.infos.join("\n"), /the decided-head marker was edited/u);
});

test("a failed marker edit check keeps the cost hold", async () => {
const { core, error, github } = await apply(costEnv(), {
comments: [marker(), decision()],
failEdited: new Set(["IC_100"]),
});
assert.equal(error, undefined);
assert.equal(core.outputs.hold, "true");
assert.equal(compares(github).length, 0);
assert.match(
core.infos.join("\n"),
/no recorded cost decision carries \(could not confirm the marker is unedited\)/u,
);
});
12 changes: 7 additions & 5 deletions .github/workflows/pr-review-cli.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,13 @@ name: pr-review-cli
# closing issue (else its one `Refs:` issue) naming `#<number>` after
# the pull request opened, and that decision covers the head. The
# decided head is the sha of this lane's newest cost hold marker posted
# before the decision; with none, nothing carries. It covers the head
# when the head is that sha, or when the pull request's net diff is the
# same at both: the three-dot compares base...decided and base...head
# list the same files with the same status, previous_filename and patch
# text. A base-only merge or a rebase that leaves every patch alone
# before the decision; with none, nothing carries. That marker must be
# unedited (GraphQL lastEditedAt null): an edited marker, or one whose
# edit state cannot be read, carries nothing, and no older marker is
# used. It covers the head when the head is that sha, or when the pull
# request's net diff is the same at both: the three-dot compares
# base...decided and base...head list the same files with the same
# status, previous_filename and patch text. A base-only merge or a rebase that leaves every patch alone
# carries; any edit of the pull request's own diff, or a base change
# that shifts a patch's context, does not. Two compare reads at most;
# a failed read, a file with no patch or a list of 300 or more files
Expand Down
Loading