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
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
bump: patch
---

### Fixed

- `/work-items:work` CI mode reads the trusted comments the issue lane now passes in `issue.json`: a listed human's `**Decision recorded.**` comment, marked `decision: true` by the runner, settles the matching open question in the body, so only a question no such comment answers is a reason to push back, and a recorded keep-`Refs` decision sets `"link": "refs"` in the outcome.
- `/work-items:attend-queue` updates the item body's open-question section after writing an escalation answer back, so the issue states its own decision.
25 changes: 18 additions & 7 deletions .github/actions/check-trusted-trigger/check-trusted-trigger.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,21 @@ const ISSUE_EDITS_QUERY = `query($owner: String!, $name: String!, $number: Int!)
}
}`;

// The issue's body edit and title change times (null when never). A failed
// read or an unexpected shape throws.
export async function readIssueEdits(github, repository, number) {
const [owner, name] = repository.split("/");
const { issue } = (await github("POST", "/graphql", { query: ISSUE_EDITS_QUERY, variables: { owner, name, number: Number(number) } })).data.repository;
if (!isPlainObject(issue)) {
throw new Error("issue edits have an unexpected shape");
}
return [issue.lastEditedAt, ...(issue.timelineItems?.nodes ?? []).map((node) => node?.createdAt)];
}

// True when every time in `editedAt` is null or no later than `at`; NaN
// compares false, so an unparseable time is a change.
export const unchangedSince = (editedAt, at) => editedAt.every((time) => time == null || timeOf(time) <= at);

// ratified-app-label: the lanes App applying exactly that label to an issue
// starts the lane only while the issue carries the owner's ratification marker
// (findRatification), read now; any other App event, or a failed read, stops.
Expand Down Expand Up @@ -496,17 +511,13 @@ async function checkRatifiedAppLabel({ event, repository, list, deniedIds, trigg
if (ratifying === undefined) {
throw new Stop("untrusted-actor");
}
const [owner, name] = repository.split("/");
let issue;
let editedAt;
try {
({ issue } = (await github("POST", "/graphql", { query: ISSUE_EDITS_QUERY, variables: { owner, name, number: Number(number) } })).data.repository);
editedAt = await readIssueEdits(github, repository, number);
} catch {
throw new Stop("untrusted-actor");
}
const ratifiedAt = timeOf(ratifying.created_at);
const editedAt = [issue?.lastEditedAt, ...(issue?.timelineItems?.nodes ?? []).map((node) => node?.createdAt)];
// Comparisons with NaN are false, so an unparseable time stops too.
if (!isPlainObject(issue) || !editedAt.every((time) => time == null || timeOf(time) <= ratifiedAt)) {
if (!unchangedSince(editedAt, timeOf(ratifying.created_at))) {
throw new Stop("untrusted-actor");
}
console.log(
Expand Down
3 changes: 3 additions & 0 deletions .github/actions/select-trusted-text/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ That query covers bodies only, so the PR title has its own rule: the PR's `timel
null or not listed, the title is withheld (empty) and counted once under `edited-by-untrusted`;
the body still follows the edit rule above.

`selectTrustedIssueComments` applies the same author and edit rules to one issue's comments; the
issue lane's `write-issue.mjs` uses it to give the model only trusted comments beside the issue.

## Output

The file at `output-path` holds `pr` (number, head and base SHA, author id, title, body), `items`
Expand Down
45 changes: 43 additions & 2 deletions .github/actions/select-trusted-text/select-trusted-text.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,51 @@ export async function buildTrustedContext({ github, repository, prNumber, ids })
};
}

// An issue's comments, oldest first, kept under the same author and edit rules
// as a PR's, each as { comment, lastEditedAt, lastEditorId } (the REST
// comment, and the last edit time and editor id, or null when never edited).
// The issue lane's write-issue.mjs shapes them.
export async function selectTrustedIssueComments({ github, repository, issueNumber, ids }) {
const trusted = createTrustCheck(github, ids);
const dropped = { "issue-comment": 0, "edited-by-untrusted": 0 };
const authored = [];
for (const comment of await paginate(github, `/repos/${repository}/issues/${issueNumber}/comments`)) {
if (await trusted(comment)) {
authored.push(comment);
} else {
dropped["issue-comment"] += 1;
}
}
const editedByListed = await readEditors(
github,
ids,
authored.map((comment) => comment.node_id),
);
const kept = [];
for (const comment of authored) {
if (!editedByListed(comment.node_id)) {
dropped["edited-by-untrusted"] += 1;
continue;
}
kept.push({
comment,
lastEditedAt: editedByListed.lastEditedAt.get(comment.node_id) ?? null,
lastEditorId: editedByListed.lastEditorId.get(comment.node_id) ?? null,
});
}
return { kept, dropped };
}

// Returns a predicate over node ids: true when the node was never edited or
// its last editor is listed. A node missing from the answer, or an edit with
// no resolvable editor, is false. GraphQL errors raise.
// no resolvable editor, is false. Its `lastEditedAt` and `lastEditorId` maps
// hold each kept node's last edit time and editor id (null when never
// edited). GraphQL errors raise.
async function readEditors(github, ids, nodeIds) {
const unique = [...new Set(nodeIds.filter((id) => typeof id === "string" && id !== ""))];
const trustedNodes = new Set();
const lastEditedAt = new Map();
const lastEditorId = new Map();
for (let start = 0; start < unique.length; start += 100) {
const answer = await github("POST", "/graphql", {
query: LAST_EDITORS,
Expand All @@ -234,10 +273,12 @@ async function readEditors(github, ids, nodeIds) {
}
if (node.lastEditedAt === null || isListed(ids, { id: node.editor?.databaseId })) {
trustedNodes.add(node.id);
lastEditedAt.set(node.id, node.lastEditedAt);
lastEditorId.set(node.id, node.lastEditedAt === null ? null : node.editor.databaseId);
}
}
}
return (nodeId) => trustedNodes.has(nodeId);
return Object.assign((nodeId) => trustedNodes.has(nodeId), { lastEditedAt, lastEditorId });
}

// Returns the process exit code. On any failure nothing is written, so a lane
Expand Down
13 changes: 10 additions & 3 deletions .github/scripts/work-resolve-issue/report-outcome.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
// what the resolve-issue run left and acts on the issue, with no model:
//
// done opens a ready PR from the run's branch: the body starts
// `Closes #<issue>`, the title is the run's when it is Conventional
// Commits (else `chore: work on #<issue>`), and the body carries
// `Closes #<issue>`, or `Refs: #<issue>` when the run reports
// `"link": "refs"` (a recorded decision keeps the issue open),
// which also holds the PR for a human, since the merge gate
// reads the work class from a closing issue. The title is the
// run's when it is Conventional Commits (else
// `chore: work on #<issue>`), and the body carries
// the hold line when a human must merge it. The PR gets no label:
// the work class lives on the issue only, and the merge gate reads
// it from the PR's closing issue. The body fills the PR body contract's
Expand Down Expand Up @@ -154,6 +158,7 @@ export function readOutcome(path) {
verification: list(parsed.verification),
notDone: notDoneList(parsed.not_done),
steps: stepList(parsed.steps),
link: parsed.link === "refs" ? "refs" : "closes",
};
}

Expand Down Expand Up @@ -302,7 +307,9 @@ export function decide({
runUrl,
});
}
const refs = outcome.link === "refs";
const hold =
refs ||
issueLabels.some((name) => HOLD_CLASSES.has(name)) ||
changedFiles.length >= COMPARE_FILES_CAP ||
holdPaths === null ||
Expand All @@ -311,7 +318,7 @@ export function decide({
// in the org PR template's order, with the run's gaps above the work.
const gaps = outcome.notDone.map(({ what }) => what);
const body = [
`Closes #${issueNumber}`,
refs ? `Refs: #${issueNumber}` : `Closes #${issueNumber}`,
...(hold ? ["", HOLD_LINE] : []),
"",
"## Summary",
Expand Down
18 changes: 18 additions & 0 deletions .github/scripts/work-resolve-issue/report-outcome.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,24 @@ test("done opens a PR whose body starts with Closes #N", () => {
assert.ok(plan.body.startsWith("Closes #77\n"), plan.body);
});

// A recorded keep-Refs decision (#7741): the PR links without closing, and a
// human merges it because the merge gate finds no closing issue to read.
test('done with "link": "refs" opens a PR that starts Refs: #N and carries the hold line', () => {
withDir({ "work-outcome.json": JSON.stringify({ ...done, link: "refs" }) }, (dir) => {
const plan = decideDone({ outcome: readOutcome(join(dir, "work-outcome.json")) });
assert.ok(plan.body.startsWith("Refs: #77\n"), plan.body);
assert.ok(!plan.body.includes("Closes #77"), plan.body);
assert.ok(plan.body.split("\n").includes("Hold: a human merges this"), plan.body);
});
});

test("an unknown link value still closes the issue", () => {
withDir({ "work-outcome.json": JSON.stringify({ ...done, link: "fixes" }) }, (dir) => {
const plan = decideDone({ outcome: readOutcome(join(dir, "work-outcome.json")) });
assert.ok(plan.body.startsWith("Closes #77\n"), plan.body);
});
});

test("done keeps a Conventional Commits title", () => {
assert.equal(decideDone().title, "docs(work-items): fix a typo");
});
Expand Down
147 changes: 147 additions & 0 deletions .github/scripts/work-resolve-issue/write-issue.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#!/usr/bin/env node
// write-issue.mjs: writes issue.json for the resolve-issue skill. The title,
// body and labels go in as written (anyone can write them; the skill reads
// them as data). Comments go in only when select-trusted-text's author and
// edit rules keep them, oldest first, each tagged with its author's kind from
// the trusted-actor list. A human's comment whose first line is
// `**Decision recorded.**`, not posted through an App, and unedited or last
// edited by a human, is marked `decision: true` with `decided_at`, its latest
// create or edit time. The lanes App's own comments never go in: its
// pushback quotes the issue body. On any failure no file is left, so the
// skill never reads a partial issue.
//
// When the lanes App applied the label (LABEL_SENDER_ID is LANES_APP_ID),
// the trigger gate admitted the issue as it stood at the newest ratification
// marker, so a comment created or edited after that marker is left out too,
// and a title or body changed since that marker fails the run.
//
// Environment:
// GITHUB_TOKEN the lane token, which can read the issue
// GITHUB_REPOSITORY owner/repo
// ISSUE_NUMBER the issue the lane works
// TRUSTED_ACTORS_PATH the base's trusted-actor list
// ISSUE_FILE where to write issue.json
// LANES_APP_ID the lanes App bot's user id
// LABEL_SENDER_ID the account that applied the label
// GITHUB_API_URL
import { rmSync, writeFileSync } from "node:fs";
import process from "node:process";
import { pathToFileURL } from "node:url";

import {
createGitHub,
loadTrustedList,
readIssueEdits,
unchangedSince,
} from "../../actions/check-trusted-trigger/check-trusted-trigger.mjs";
import { findRatification } from "../../actions/resolve-config/ratification.mjs";
import { selectTrustedIssueComments } from "../../actions/select-trusted-text/select-trusted-text.mjs";

const DECISION = /^\*\*Decision recorded\.\*\*(\s|$)/;
// Comments other than decisions, newest kept, so a pile of them cannot crowd
// out the body.
export const MAX_OTHER_COMMENTS = 20;

const timeOf = (value) => (typeof value === "string" ? Date.parse(value) : Number.NaN);
const firstLine = (body) => (body ?? "").trimStart().split("\n", 1)[0].trimEnd();

export async function main({ env = process.env, github, log = console.log } = {}) {
const file = env.ISSUE_FILE;
try {
if (!file) {
throw new Error("ISSUE_FILE is empty");
}
rmSync(file, { force: true });
if (!/^[1-9][0-9]{0,9}$/.test(env.ISSUE_NUMBER ?? "") || !/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(env.GITHUB_REPOSITORY ?? "")) {
throw new Error("ISSUE_NUMBER or GITHUB_REPOSITORY is malformed");
}
if (!/^[1-9][0-9]*$/.test(env.LANES_APP_ID ?? "")) {
throw new Error("LANES_APP_ID is not a user id");
}
const lanesAppId = Number(env.LANES_APP_ID);
const { ids, humans } = loadTrustedList(env.TRUSTED_ACTORS_PATH);
const client = github ?? createGitHub({ token: env.GITHUB_TOKEN, apiUrl: env.GITHUB_API_URL || undefined });
const repository = env.GITHUB_REPOSITORY;
const data = await client("GET", `/repos/${repository}/issues/${env.ISSUE_NUMBER}`);
const { kept, dropped } = await selectTrustedIssueComments({
github: client,
repository,
issueNumber: env.ISSUE_NUMBER,
ids,
});
dropped["lanes-app"] = 0;
dropped["after-ratification"] = 0;
dropped["over-cap"] = 0;

let frozenAt = Number.POSITIVE_INFINITY;
if (env.LABEL_SENDER_ID === env.LANES_APP_ID) {
const ratifiers = new Set([...humans].filter((id) => id !== lanesAppId));
const markers = kept.map(({ comment }) => comment).filter((comment) => findRatification([comment], ratifiers));
if (markers.length === 0) {
throw new Error("the lanes App applied the label but no ratification marker is kept");
}
frozenAt = Math.max(...markers.map((comment) => timeOf(comment.created_at)));
// The gate checked this before the run; the author can edit since.
if (!unchangedSince(await readIssueEdits(client, repository, env.ISSUE_NUMBER), frozenAt)) {
throw new Error("the issue title or body changed after the newest ratification marker");
}
}

const comments = [];
for (const { comment, lastEditedAt, lastEditorId } of kept) {
if (comment.user.id === lanesAppId) {
dropped["lanes-app"] += 1;
continue;
}
const createdAt = timeOf(comment.created_at);
const editedAt = lastEditedAt === null ? createdAt : timeOf(lastEditedAt);
// NaN compares false, so an unparseable time is left out.
if (!(createdAt <= frozenAt && editedAt <= frozenAt)) {
dropped["after-ratification"] += 1;
continue;
}
const human = humans.has(comment.user.id);
// A decision is a human's own text: not posted through an App, and
// never edited or last edited by a human.
const decision =
human &&
comment.performed_via_github_app == null &&
(lastEditedAt === null || humans.has(lastEditorId)) &&
DECISION.test(firstLine(comment.body));
comments.push({
author_login: comment.user.login,
author_kind: human ? "human" : "bot",
created_at: comment.created_at,
url: comment.html_url,
body: comment.body ?? "",
decision,
...(decision ? { decided_at: new Date(Math.max(createdAt, editedAt)).toISOString() } : {}),
});
}
const others = comments.filter((comment) => !comment.decision);
const cut = new Set(others.slice(0, Math.max(0, others.length - MAX_OTHER_COMMENTS)));
dropped["over-cap"] = cut.size;

const issue = {
number: data.number,
title: data.title,
body: data.body ?? "",
labels: (data.labels ?? []).map((label) => (typeof label === "string" ? label : label.name)),
comments: comments.filter((comment) => !cut.has(comment)),
};
writeFileSync(file, `${JSON.stringify(issue, null, 2)}\n`);
log(`write-issue: kept ${issue.comments.length} comment(s), dropped ${JSON.stringify(dropped)}`);
return 0;
} catch (error) {
if (file) {
rmSync(file, { force: true });
}
const detail = error?.name === "GitHubError" ? error.message : (error?.message ?? "error");
log(`::error title=write-issue::${String(detail).replaceAll(/[\r\n%]/g, " ")}; no issue file written`);
return 1;
}
}

if (import.meta.url === pathToFileURL(process.argv[1] ?? "").href) {
process.exitCode = await main();
}
Loading
Loading