@davidbyoung/software-factory 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/plugin.json +5 -0
- package/LICENSE +21 -0
- package/README.md +135 -2
- package/actions/factory-agent/action.yml +218 -0
- package/agents/factory-test-reviewer.md +129 -0
- package/agents/factory-ticket-readiness.md +107 -0
- package/agents/factory-ui-reviewer.md +160 -0
- package/bin/factory.js +45 -0
- package/bin/launch.js +67 -0
- package/dist/board.d.mts +128 -0
- package/dist/board.mjs +456 -0
- package/dist/build.d.mts +18 -0
- package/dist/build.mjs +60 -0
- package/dist/cli.d.mts +17 -0
- package/dist/cli.mjs +264 -0
- package/dist/code-host/github.d.mts +118 -0
- package/dist/code-host/github.mjs +419 -0
- package/dist/code-host/index.d.mts +9 -0
- package/dist/code-host/index.mjs +18 -0
- package/dist/code-host/types.d.mts +205 -0
- package/dist/code-host/types.mjs +12 -0
- package/dist/config.d.mts +166 -0
- package/dist/config.mjs +548 -0
- package/dist/deps.d.mts +32 -0
- package/dist/deps.mjs +126 -0
- package/dist/doctor.d.mts +34 -0
- package/dist/doctor.mjs +188 -0
- package/dist/guard-commit.d.mts +41 -0
- package/dist/guard-commit.mjs +205 -0
- package/dist/guard-permission.d.mts +15 -0
- package/dist/guard-permission.mjs +52 -0
- package/dist/guard-ticket-text.d.mts +27 -0
- package/dist/guard-ticket-text.mjs +288 -0
- package/dist/guard.d.mts +4 -0
- package/dist/guard.mjs +37 -0
- package/dist/init.d.mts +58 -0
- package/dist/init.mjs +556 -0
- package/dist/lib/clock.d.mts +14 -0
- package/dist/lib/clock.mjs +36 -0
- package/dist/lib/gh.d.mts +28 -0
- package/dist/lib/gh.mjs +92 -0
- package/dist/lib/glob.d.mts +2 -0
- package/dist/lib/glob.mjs +41 -0
- package/dist/lib/pricing.d.mts +25 -0
- package/dist/lib/pricing.mjs +77 -0
- package/dist/lib/usage.d.mts +34 -0
- package/dist/lib/usage.mjs +128 -0
- package/dist/lib/work-status.d.mts +251 -0
- package/dist/lib/work-status.mjs +510 -0
- package/dist/merge-policy.d.mts +66 -0
- package/dist/merge-policy.mjs +146 -0
- package/dist/parts.d.mts +39 -0
- package/dist/parts.mjs +138 -0
- package/dist/poller.d.mts +61 -0
- package/dist/poller.mjs +247 -0
- package/dist/pr-watch.d.mts +45 -0
- package/dist/pr-watch.mjs +244 -0
- package/dist/pr.d.mts +65 -0
- package/dist/pr.mjs +571 -0
- package/dist/review-cost.d.mts +18 -0
- package/dist/review-cost.mjs +95 -0
- package/dist/review-tools.d.mts +5 -0
- package/dist/review-tools.mjs +46 -0
- package/dist/review-verdict.d.mts +50 -0
- package/dist/review-verdict.mjs +171 -0
- package/dist/reviewers.d.mts +54 -0
- package/dist/reviewers.mjs +312 -0
- package/dist/runner/github-actions.d.mts +12 -0
- package/dist/runner/github-actions.mjs +97 -0
- package/dist/runner/index.d.mts +9 -0
- package/dist/runner/index.mjs +22 -0
- package/dist/runner/types.d.mts +60 -0
- package/dist/runner/types.mjs +7 -0
- package/dist/session-start.d.mts +20 -0
- package/dist/session-start.mjs +115 -0
- package/dist/site/capture.d.mts +44 -0
- package/dist/site/capture.mjs +114 -0
- package/dist/site/measure.d.mts +83 -0
- package/dist/site/measure.mjs +225 -0
- package/dist/site/pages.d.mts +10 -0
- package/dist/site/pages.mjs +71 -0
- package/dist/site/pixels.d.mts +47 -0
- package/dist/site/pixels.mjs +157 -0
- package/dist/site/servers.d.mts +47 -0
- package/dist/site/servers.mjs +141 -0
- package/dist/site.d.mts +60 -0
- package/dist/site.mjs +341 -0
- package/dist/status.d.mts +100 -0
- package/dist/status.mjs +514 -0
- package/dist/sync.d.mts +59 -0
- package/dist/sync.mjs +292 -0
- package/dist/ticket.d.mts +61 -0
- package/dist/ticket.mjs +562 -0
- package/dist/tickets/github.d.mts +128 -0
- package/dist/tickets/github.mjs +491 -0
- package/dist/tickets/index.d.mts +15 -0
- package/dist/tickets/index.mjs +27 -0
- package/dist/tickets/transport.d.mts +42 -0
- package/dist/tickets/transport.mjs +158 -0
- package/dist/tickets/types.d.mts +191 -0
- package/dist/tickets/types.mjs +76 -0
- package/docs/builds.md +227 -0
- package/docs/cli.md +221 -0
- package/docs/configuration.md +809 -0
- package/docs/design.md +106 -0
- package/docs/developing.md +400 -0
- package/docs/existing-repo.md +138 -0
- package/docs/github/reference.md +288 -0
- package/docs/github/setup.md +312 -0
- package/docs/glossary.md +106 -0
- package/docs/limitations.md +115 -0
- package/docs/plugins.md +192 -0
- package/docs/quick-start.md +222 -0
- package/docs/removing.md +76 -0
- package/docs/skills.md +270 -0
- package/docs/updating.md +108 -0
- package/docs/workflow.md +195 -0
- package/package.json +67 -4
- package/poller.Dockerfile +21 -0
- package/review.md +170 -0
- package/rules.md +313 -0
- package/settings.schema.json +411 -0
- package/skills/factory-address-review/SKILL.md +547 -0
- package/skills/factory-address-review/patch-coverage.py +68 -0
- package/skills/factory-build/SKILL.md +412 -0
- package/skills/factory-deps/SKILL.md +185 -0
- package/skills/factory-design/SKILL.md +159 -0
- package/skills/factory-diagnose/SKILL.md +93 -0
- package/skills/factory-mutate/SKILL.md +76 -0
- package/skills/factory-refine/SKILL.md +417 -0
- package/skills/factory-resolve-conflicts/SKILL.md +235 -0
- package/workflows/factory-board-sync.yml +91 -0
- package/workflows/factory-build.yml +72 -0
- package/workflows/factory-check.yml +37 -0
- package/workflows/factory-claude-review.yml +133 -0
- package/workflows/factory-conflicts.yml +69 -0
- package/workflows/factory-deps-auto-merge.yml +43 -0
- package/workflows/factory-deps.yml +59 -0
- package/workflows/factory-merge-policy.yml +121 -0
- package/workflows/factory-review-verdict.yml +54 -0
- package/workflows/factory-review.yml +75 -0
package/dist/pr.mjs
ADDED
|
@@ -0,0 +1,571 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// A pull request's fixed steps, through GitHub's GraphQL and REST APIs.
|
|
3
|
+
//
|
|
4
|
+
// npx --no-install factory pr state [N] [--wait] # what factory-address-review steps 0 to 1b read
|
|
5
|
+
// npx --no-install factory pr threads N [--all] # unresolved review threads, or every one
|
|
6
|
+
// npx --no-install factory pr reply N COMMENT_ID --body-file FILE
|
|
7
|
+
// npx --no-install factory pr resolve N COMMENT_ID
|
|
8
|
+
// npx --no-install factory pr ready N # refuses an unverified commit when git signs
|
|
9
|
+
// npx --no-install factory pr closes N # issues the pull request closes
|
|
10
|
+
// npx --no-install factory pr wait-checks N # every check on HEAD
|
|
11
|
+
// npx --no-install factory pr review-pending N REVIEW # exits 0 when the owner's review needs an answer
|
|
12
|
+
// npx --no-install factory pr conflicts # the pull requests the conflict sweep takes
|
|
13
|
+
// npx --no-install factory pr open --title TITLE --body-file FILE [--base BRANCH] [--ready]
|
|
14
|
+
// npx --no-install factory pr view N --field FIELD # one field of a pull request
|
|
15
|
+
// npx --no-install factory pr review N REVIEW # a review's author, body, and line comments
|
|
16
|
+
// npx --no-install factory pr unverified N # the commits not shown as signed
|
|
17
|
+
// npx --no-install factory pr failed N # each failed job's log, and a failed check's own report
|
|
18
|
+
// npx --no-install factory pr review-again N # a fresh review of the same commit
|
|
19
|
+
//
|
|
20
|
+
// COMMENT_ID is a thread's first comment, as `threads` prints it. A failed
|
|
21
|
+
// read exits 1 with the error; `review-pending` and `conflicts`, which a
|
|
22
|
+
// workflow's only-if gate reads 0 as work and 1 as none, exit 2 on a failed read.
|
|
23
|
+
import { execFileSync } from "node:child_process";
|
|
24
|
+
import { readFileSync } from "node:fs";
|
|
25
|
+
import { setTimeout as sleep } from "node:timers/promises";
|
|
26
|
+
import { reachOf } from "./config.mjs";
|
|
27
|
+
import { AGENT_MARK } from "./lib/work-status.mjs";
|
|
28
|
+
import { toChecks } from "./pr-watch.mjs";
|
|
29
|
+
import { openCodeHost, } from "./code-host/index.mjs";
|
|
30
|
+
import { openRunner } from "./runner/index.mjs";
|
|
31
|
+
import { refuseBareTags, refuseOverride } from "./guard-ticket-text.mjs";
|
|
32
|
+
export function withAgentMark(body) {
|
|
33
|
+
return body.includes(AGENT_MARK)
|
|
34
|
+
? body
|
|
35
|
+
: `${body.trimEnd()}\n\n${AGENT_MARK}`;
|
|
36
|
+
}
|
|
37
|
+
const SKIP_AS_LAST = /<!-- (conflict-fix|session-handoff) -->|conflict-stop/;
|
|
38
|
+
/** The owner's review bodies still to answer: submitted after the last
|
|
39
|
+
* conversation comment carrying the agent mark, a sweep's own comments
|
|
40
|
+
* aside. */
|
|
41
|
+
export function ownerReviewsToWork(r) {
|
|
42
|
+
const answered = r.conversation
|
|
43
|
+
.filter((c) => c.body.includes(AGENT_MARK) && !SKIP_AS_LAST.test(c.body))
|
|
44
|
+
.map((c) => c.createdAt)
|
|
45
|
+
.sort()
|
|
46
|
+
.at(-1);
|
|
47
|
+
return r.reviews.filter((review) => review.author === r.ownerLogin &&
|
|
48
|
+
review.body.trim() !== "" &&
|
|
49
|
+
!review.body.includes(AGENT_MARK) &&
|
|
50
|
+
(!answered || review.submittedAt > answered));
|
|
51
|
+
}
|
|
52
|
+
/** A review is coming for this head only on a pull request the Claude review
|
|
53
|
+
* workflow runs on. */
|
|
54
|
+
export function reviewExpected(pull) {
|
|
55
|
+
return (!pull.isDraft &&
|
|
56
|
+
pull.author !== "dependabot[bot]" &&
|
|
57
|
+
pull.head.repository === pull.base.repository &&
|
|
58
|
+
pull.mergeableState !== "dirty");
|
|
59
|
+
}
|
|
60
|
+
export function isSettled(r) {
|
|
61
|
+
const reviewDone = !reviewExpected(r.pull) ||
|
|
62
|
+
r.summary !== null ||
|
|
63
|
+
(r.reviewRun?.status === "completed" &&
|
|
64
|
+
r.reviewRun.conclusion !== "success");
|
|
65
|
+
return (reviewDone &&
|
|
66
|
+
r.checks.length > 0 &&
|
|
67
|
+
r.checks.every((check) => check.state !== "pending"));
|
|
68
|
+
}
|
|
69
|
+
const firstLine = (body) => body.replace(AGENT_MARK, "").trim().split("\n")[0].slice(0, 100);
|
|
70
|
+
export function formatReport(r) {
|
|
71
|
+
const p = r.pull;
|
|
72
|
+
const lines = [
|
|
73
|
+
`#${p.number} ${p.title}`,
|
|
74
|
+
`state: ${p.state}${p.isDraft ? ", draft" : ", ready"}; head ${p.head.sha.slice(0, 7)} on ${p.head.branch}; base ${p.base.branch}; author ${p.author}`,
|
|
75
|
+
`mergeable: ${p.mergeableState}`,
|
|
76
|
+
];
|
|
77
|
+
if (!reviewExpected(p))
|
|
78
|
+
lines.push("Claude review: none expected on this head");
|
|
79
|
+
else if (!r.reviewRun)
|
|
80
|
+
lines.push("Claude review: no run yet for this head");
|
|
81
|
+
else
|
|
82
|
+
lines.push(`Claude review: ${r.reviewRun.status}${r.reviewRun.conclusion ? `, ${r.reviewRun.conclusion}` : ""}`);
|
|
83
|
+
if (r.summary) {
|
|
84
|
+
const verdict = /review-verdict: (\w+)/.exec(r.summary.body)?.[1] ?? "unknown";
|
|
85
|
+
lines.push(`summary: comment ${r.summary.id}, verdict ${verdict}`);
|
|
86
|
+
}
|
|
87
|
+
else
|
|
88
|
+
lines.push("summary: none for this head");
|
|
89
|
+
const owner = ownerReviewsToWork(r);
|
|
90
|
+
lines.push(`owner review bodies to answer: ${owner.length}`);
|
|
91
|
+
for (const review of owner)
|
|
92
|
+
lines.push(` - review ${review.id} at ${review.submittedAt}: ${firstLine(review.body)}`);
|
|
93
|
+
const open = r.threads.filter((t) => !t.isResolved);
|
|
94
|
+
lines.push(`unresolved threads: ${open.length}`);
|
|
95
|
+
for (const t of open) {
|
|
96
|
+
const last = t.comments.at(-1);
|
|
97
|
+
const ownerTurn = last?.author === r.ownerLogin && !last.body.includes(AGENT_MARK);
|
|
98
|
+
lines.push(` - ${t.commentId} on ${t.path}:${t.line ?? "?"}${t.isOutdated ? " (outdated)" : ""} by ${t.comments[0]?.author ?? "?"}${ownerTurn ? ", owner's turn" : ""}: ${firstLine(t.comments[0]?.body ?? "")}`);
|
|
99
|
+
}
|
|
100
|
+
lines.push("checks:");
|
|
101
|
+
if (r.checks.length === 0)
|
|
102
|
+
lines.push(" (none yet)");
|
|
103
|
+
for (const check of r.checks)
|
|
104
|
+
lines.push(` - ${check.name}: ${check.state}`);
|
|
105
|
+
const codecov = r.conversation.filter((c) => /codecov/i.test(c.author));
|
|
106
|
+
lines.push(codecov.length
|
|
107
|
+
? `codecov comment: ${codecov.at(-1).id}`
|
|
108
|
+
: "codecov comment: none (it comments only when coverage moved)");
|
|
109
|
+
return lines;
|
|
110
|
+
}
|
|
111
|
+
async function readPull(p, pr, d) {
|
|
112
|
+
for (let attempt = 1;; attempt++) {
|
|
113
|
+
const pull = await p.get(pr);
|
|
114
|
+
if (pull.mergeableState !== "unknown" || attempt >= 5)
|
|
115
|
+
return pull;
|
|
116
|
+
await d.sleep(10_000);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
async function readReport(p, pr, d) {
|
|
120
|
+
const pull = await readPull(p, pr, d);
|
|
121
|
+
const sha = pull.head.sha;
|
|
122
|
+
const [runs, conversation, reviews, threads, checkRuns, statuses] = await Promise.all([
|
|
123
|
+
d.runner.reviewRuns(sha),
|
|
124
|
+
p.comments(pr),
|
|
125
|
+
p.reviews(pr),
|
|
126
|
+
p.threads(pr),
|
|
127
|
+
p.checkRuns(sha),
|
|
128
|
+
p.statuses(sha),
|
|
129
|
+
]);
|
|
130
|
+
const summary = conversation
|
|
131
|
+
.filter((c) => c.author === "github-actions[bot]" &&
|
|
132
|
+
c.body.includes(`claude-review sha=${sha}`))
|
|
133
|
+
.at(-1) ?? null;
|
|
134
|
+
return {
|
|
135
|
+
pull,
|
|
136
|
+
// The owner is the part of the repository before the slash.
|
|
137
|
+
ownerLogin: d.repository.split("/")[0],
|
|
138
|
+
reviewRun: runs[0] ?? null,
|
|
139
|
+
summary,
|
|
140
|
+
conversation,
|
|
141
|
+
reviews,
|
|
142
|
+
threads,
|
|
143
|
+
checks: toChecks(checkRuns, statuses),
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
const POLL_MS = 30_000;
|
|
147
|
+
/** How long `wait-checks` waits for a first run before it decides the
|
|
148
|
+
* repository runs no CI on this commit. */
|
|
149
|
+
const NO_CHECKS_MS = 120_000;
|
|
150
|
+
/** The events whose runs `wait-checks` waits on. The factory's own
|
|
151
|
+
* workflows start on pull_request_target and workflow_run, and a failed
|
|
152
|
+
* board sync should not stop a pull request being marked ready. */
|
|
153
|
+
const WAITED_EVENTS = ["pull_request", "push"];
|
|
154
|
+
const PASSED = ["success", "skipped", "neutral"];
|
|
155
|
+
const passed = (conclusion) => PASSED.includes(conclusion ?? "");
|
|
156
|
+
/** The newest run of each workflow, among the waited-on events. */
|
|
157
|
+
export function newestRuns(runs) {
|
|
158
|
+
const newest = new Map();
|
|
159
|
+
for (const r of runs) {
|
|
160
|
+
if (!WAITED_EVENTS.includes(r.event))
|
|
161
|
+
continue;
|
|
162
|
+
const seen = newest.get(r.jobs);
|
|
163
|
+
if (!seen || r.id > seen.id)
|
|
164
|
+
newest.set(r.jobs, r);
|
|
165
|
+
}
|
|
166
|
+
return [...newest.values()];
|
|
167
|
+
}
|
|
168
|
+
/** Waits until GitHub shows the pushed commit as the pull request's head,
|
|
169
|
+
* then until every workflow run for that commit has completed, and says how
|
|
170
|
+
* they ended. It goes by runs, not check runs alone, since a job that waits
|
|
171
|
+
* on another has no check run until that one finishes. */
|
|
172
|
+
async function waitChecks(p, d, pr) {
|
|
173
|
+
const now = d.now ?? Date.now;
|
|
174
|
+
const sha = d.git(["rev-parse", "HEAD"]).trim();
|
|
175
|
+
for (;;) {
|
|
176
|
+
const pull = await p.get(pr);
|
|
177
|
+
if (pull.head.sha === sha)
|
|
178
|
+
break;
|
|
179
|
+
await d.sleep(2000);
|
|
180
|
+
}
|
|
181
|
+
const headAt = now();
|
|
182
|
+
for (;;) {
|
|
183
|
+
const runs = newestRuns(await d.runner.runs(sha));
|
|
184
|
+
if (runs.length === 0 && now() - headAt >= NO_CHECKS_MS)
|
|
185
|
+
return { line: "no checks", code: 0 };
|
|
186
|
+
if (runs.length > 0 && runs.every((r) => r.status === "completed")) {
|
|
187
|
+
const failed = runs.filter((r) => !PASSED.includes(r.conclusion ?? ""));
|
|
188
|
+
if (failed.length === 0)
|
|
189
|
+
return { line: "success", code: 0 };
|
|
190
|
+
const reported = new Set(failed.map((r) => r.checksId));
|
|
191
|
+
const names = (await p.checkRuns(sha))
|
|
192
|
+
.filter((c) => reported.has(c.checksId ?? -1) &&
|
|
193
|
+
!PASSED.includes(c.conclusion ?? ""))
|
|
194
|
+
.map((c) => c.name);
|
|
195
|
+
return { line: `failure: ${names.join(", ")}`, code: 3 };
|
|
196
|
+
}
|
|
197
|
+
await d.sleep(runs.length === 0 ? 15_000 : POLL_MS);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
/** What `view --field` prints, by the field's name. */
|
|
201
|
+
const FIELDS = {
|
|
202
|
+
head: (p) => p.head.sha,
|
|
203
|
+
branch: (p) => p.head.branch,
|
|
204
|
+
base: (p) => p.base.branch,
|
|
205
|
+
title: (p) => p.title,
|
|
206
|
+
body: (p) => p.body,
|
|
207
|
+
state: (p) => p.state,
|
|
208
|
+
mergeable: (p) => p.mergeableState,
|
|
209
|
+
};
|
|
210
|
+
/** How many lines of a failed job's log `failed` prints, from its end. */
|
|
211
|
+
const LOG_TAIL = 200;
|
|
212
|
+
const USAGE = `Usage: npx --no-install factory pr state [N] [--wait] | threads N [--all] | reply N COMMENT_ID --body-file FILE | resolve N COMMENT_ID | ready N | closes N | wait-checks N | comments N [--author LOGIN] [--contains TEXT] [--json] | comment N --body-file FILE | comment-view ID | label N --add LABEL | --remove LABEL | review-pending N REVIEW | conflicts | open --title TITLE --body-file FILE [--base BRANCH] [--ready] | view N --field ${Object.keys(FIELDS).join("|")} | review N REVIEW | unverified N | failed N | review-again N`;
|
|
213
|
+
function option(args, name) {
|
|
214
|
+
const at = args.indexOf(name);
|
|
215
|
+
if (at === -1)
|
|
216
|
+
return undefined;
|
|
217
|
+
const value = args[at + 1];
|
|
218
|
+
if (value === undefined)
|
|
219
|
+
throw new Error(USAGE);
|
|
220
|
+
return value;
|
|
221
|
+
}
|
|
222
|
+
/** A conversation comment as `pr comments` lists it: ID, author, time, and
|
|
223
|
+
* its first line. */
|
|
224
|
+
export function commentLine(c) {
|
|
225
|
+
const first = c.body.replace(AGENT_MARK, "").trim().split("\n")[0];
|
|
226
|
+
return `${c.id} ${c.author} ${c.createdAt} ${first}`.trimEnd();
|
|
227
|
+
}
|
|
228
|
+
/** A conversation comment under GitHub's own field names, which
|
|
229
|
+
* `comments --json` has always printed. */
|
|
230
|
+
const inGithubNames = (c) => ({
|
|
231
|
+
id: c.id,
|
|
232
|
+
user: { login: c.author },
|
|
233
|
+
body: c.body,
|
|
234
|
+
created_at: c.createdAt,
|
|
235
|
+
});
|
|
236
|
+
/** Whether git is set to sign commits here. An unset value exits non-zero. */
|
|
237
|
+
function gitSigns(d) {
|
|
238
|
+
try {
|
|
239
|
+
return d.git(["config", "--get", "commit.gpgsign"]).trim() === "true";
|
|
240
|
+
}
|
|
241
|
+
catch {
|
|
242
|
+
return false;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
/** Whether the owner's review has something in it to work, and if not,
|
|
246
|
+
* why: "closed", "someone else's review", or "the factory's own reply". An
|
|
247
|
+
* empty body counts for nothing, since the factory's reply is a review with
|
|
248
|
+
* an empty body and one comment carrying the marker. */
|
|
249
|
+
export async function reviewPending(p, pr, reviewId, owner) {
|
|
250
|
+
const pull = await p.get(pr);
|
|
251
|
+
if (pull.state !== "open")
|
|
252
|
+
return {
|
|
253
|
+
pending: false,
|
|
254
|
+
why: `#${pr} is ${pull.merged ? "merged" : "closed"}`,
|
|
255
|
+
};
|
|
256
|
+
const review = (await p.reviews(pr)).find((r) => r.id === reviewId);
|
|
257
|
+
if (!review)
|
|
258
|
+
throw new Error(`No review ${reviewId} on #${pr}.`);
|
|
259
|
+
if (review.author !== owner)
|
|
260
|
+
return { pending: false, why: `review ${reviewId} is someone else's` };
|
|
261
|
+
const { body } = review;
|
|
262
|
+
if (body.trim() !== "" && !body.includes(AGENT_MARK))
|
|
263
|
+
return { pending: true };
|
|
264
|
+
const comments = await p.reviewCommentsOf(pr, reviewId);
|
|
265
|
+
if (comments.some((c) => !c.body.includes(AGENT_MARK)))
|
|
266
|
+
return { pending: true };
|
|
267
|
+
return {
|
|
268
|
+
pending: false,
|
|
269
|
+
why: `review ${reviewId} is the factory's own reply: nothing of the owner's in it`,
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
const CONFLICT_READS = 5;
|
|
273
|
+
const CONFLICT_WAIT_MS = 10_000;
|
|
274
|
+
/** The numbers of the pull requests the conflict sweep takes, oldest first:
|
|
275
|
+
* not a draft, from this repository, not Dependabot's, based on the default
|
|
276
|
+
* branch, `dirty`, and with no stop comment for the current head. GitHub
|
|
277
|
+
* computes the state lazily, so an `unknown` one is read up to 5 times, 10
|
|
278
|
+
* seconds apart. */
|
|
279
|
+
export async function conflicting(p, sleepFor) {
|
|
280
|
+
const base = await p.defaultBranch();
|
|
281
|
+
const queue = (await p.listOpen())
|
|
282
|
+
.filter((pull) => !pull.isDraft &&
|
|
283
|
+
pull.base.branch === base &&
|
|
284
|
+
pull.author !== "dependabot[bot]" &&
|
|
285
|
+
pull.head.repository === pull.base.repository)
|
|
286
|
+
.sort((a, b) => (a.createdAt ?? "").localeCompare(b.createdAt ?? "") ||
|
|
287
|
+
a.number - b.number);
|
|
288
|
+
const found = [];
|
|
289
|
+
for (const { number } of queue) {
|
|
290
|
+
let pull = await p.get(number);
|
|
291
|
+
for (let read = 1; pull.mergeableState === "unknown" && read < CONFLICT_READS; read++) {
|
|
292
|
+
await sleepFor(CONFLICT_WAIT_MS);
|
|
293
|
+
pull = await p.get(number);
|
|
294
|
+
}
|
|
295
|
+
if (pull.mergeableState !== "dirty")
|
|
296
|
+
continue;
|
|
297
|
+
const stop = `conflict-stop head=${pull.head.sha}`;
|
|
298
|
+
if ((await p.comments(number)).some((c) => c.body.includes(stop)))
|
|
299
|
+
continue;
|
|
300
|
+
found.push(number);
|
|
301
|
+
}
|
|
302
|
+
return found;
|
|
303
|
+
}
|
|
304
|
+
export async function main(argv, d) {
|
|
305
|
+
const [command, ...rest] = argv;
|
|
306
|
+
const numbers = rest.filter((a) => /^\d+$/.test(a)).map(Number);
|
|
307
|
+
try {
|
|
308
|
+
const p = d.codeHost;
|
|
309
|
+
switch (command) {
|
|
310
|
+
case "state": {
|
|
311
|
+
const pr = numbers[0] ??
|
|
312
|
+
(await p.forBranch(d.git(["branch", "--show-current"]).trim()));
|
|
313
|
+
if (pr === null) {
|
|
314
|
+
d.out("no open pull request for this branch");
|
|
315
|
+
return 3;
|
|
316
|
+
}
|
|
317
|
+
let report = await readReport(p, pr, d);
|
|
318
|
+
while (rest.includes("--wait") && !isSettled(report)) {
|
|
319
|
+
await d.sleep(POLL_MS);
|
|
320
|
+
report = await readReport(p, pr, d);
|
|
321
|
+
}
|
|
322
|
+
formatReport(report).forEach((line) => d.out(line));
|
|
323
|
+
return 0;
|
|
324
|
+
}
|
|
325
|
+
case "threads": {
|
|
326
|
+
const [pr] = numbers;
|
|
327
|
+
if (!pr)
|
|
328
|
+
throw new Error(USAGE);
|
|
329
|
+
const threads = await p.threads(pr);
|
|
330
|
+
const listed = rest.includes("--all")
|
|
331
|
+
? threads
|
|
332
|
+
: threads.filter((t) => !t.isResolved);
|
|
333
|
+
d.out(JSON.stringify(listed, null, 2));
|
|
334
|
+
return 0;
|
|
335
|
+
}
|
|
336
|
+
case "reply": {
|
|
337
|
+
const [pr, commentId] = numbers;
|
|
338
|
+
const file = rest[rest.indexOf("--body-file") + 1];
|
|
339
|
+
if (!pr || !commentId || !rest.includes("--body-file") || !file)
|
|
340
|
+
throw new Error(USAGE);
|
|
341
|
+
const id = await p.reply(pr, commentId, withAgentMark(d.readFile(file)));
|
|
342
|
+
d.out(`replied ${id}`);
|
|
343
|
+
return 0;
|
|
344
|
+
}
|
|
345
|
+
case "resolve": {
|
|
346
|
+
const [pr, commentId] = numbers;
|
|
347
|
+
if (!pr || !commentId)
|
|
348
|
+
throw new Error(USAGE);
|
|
349
|
+
await p.resolve(pr, commentId);
|
|
350
|
+
d.out("resolved");
|
|
351
|
+
return 0;
|
|
352
|
+
}
|
|
353
|
+
case "ready": {
|
|
354
|
+
const [pr] = numbers;
|
|
355
|
+
if (!pr)
|
|
356
|
+
throw new Error(USAGE);
|
|
357
|
+
if (gitSigns(d)) {
|
|
358
|
+
const bad = await p.unverifiedCommits(pr);
|
|
359
|
+
if (bad.length > 0) {
|
|
360
|
+
d.out(`Not marked ready: git is set to sign, and GitHub shows these commits as not verified:`);
|
|
361
|
+
bad.forEach((line) => d.out(line));
|
|
362
|
+
return 1;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
await p.ready(pr);
|
|
366
|
+
d.out("ready");
|
|
367
|
+
return 0;
|
|
368
|
+
}
|
|
369
|
+
case "closes": {
|
|
370
|
+
const [pr] = numbers;
|
|
371
|
+
if (!pr)
|
|
372
|
+
throw new Error(USAGE);
|
|
373
|
+
d.out((await p.closes(pr)).join(" "));
|
|
374
|
+
return 0;
|
|
375
|
+
}
|
|
376
|
+
case "wait-checks": {
|
|
377
|
+
const [pr] = numbers;
|
|
378
|
+
if (!pr)
|
|
379
|
+
throw new Error(USAGE);
|
|
380
|
+
const result = await waitChecks(p, d, pr);
|
|
381
|
+
d.out(result.line);
|
|
382
|
+
return result.code;
|
|
383
|
+
}
|
|
384
|
+
case "review-pending": {
|
|
385
|
+
const [pr, review] = numbers;
|
|
386
|
+
if (!pr || !review)
|
|
387
|
+
throw new Error(USAGE);
|
|
388
|
+
const result = await reviewPending(p, pr, review,
|
|
389
|
+
// The owner is the part of the repository before the slash.
|
|
390
|
+
d.repository.split("/")[0]);
|
|
391
|
+
if (result.pending) {
|
|
392
|
+
d.out("pending");
|
|
393
|
+
return 0;
|
|
394
|
+
}
|
|
395
|
+
d.out(`nothing to do: ${result.why}`);
|
|
396
|
+
return 1;
|
|
397
|
+
}
|
|
398
|
+
case "conflicts": {
|
|
399
|
+
const found = await conflicting(p, d.sleep);
|
|
400
|
+
if (found.length === 0) {
|
|
401
|
+
d.out("no pull request conflicts with the default branch");
|
|
402
|
+
return 1;
|
|
403
|
+
}
|
|
404
|
+
found.forEach((n) => d.out(String(n)));
|
|
405
|
+
return 0;
|
|
406
|
+
}
|
|
407
|
+
case "comments": {
|
|
408
|
+
const pr = Number(rest[0]);
|
|
409
|
+
if (!Number.isInteger(pr) || pr <= 0)
|
|
410
|
+
throw new Error(USAGE);
|
|
411
|
+
const author = option(rest, "--author");
|
|
412
|
+
const contains = option(rest, "--contains");
|
|
413
|
+
const kept = (await p.comments(pr)).filter((c) => (author === undefined || c.author === author) &&
|
|
414
|
+
(contains === undefined || c.body.includes(contains)));
|
|
415
|
+
if (rest.includes("--json"))
|
|
416
|
+
d.out(JSON.stringify(kept.map(inGithubNames), null, 2));
|
|
417
|
+
else
|
|
418
|
+
kept.forEach((c) => d.out(commentLine(c)));
|
|
419
|
+
return 0;
|
|
420
|
+
}
|
|
421
|
+
case "comment": {
|
|
422
|
+
const pr = Number(rest[0]);
|
|
423
|
+
const file = option(rest, "--body-file");
|
|
424
|
+
if (!Number.isInteger(pr) || pr <= 0 || !file)
|
|
425
|
+
throw new Error(USAGE);
|
|
426
|
+
const body = d.readFile(file);
|
|
427
|
+
refuseBareTags(body);
|
|
428
|
+
d.out(String(await p.comment(pr, body)));
|
|
429
|
+
return 0;
|
|
430
|
+
}
|
|
431
|
+
case "comment-view": {
|
|
432
|
+
const id = Number(rest[0]);
|
|
433
|
+
if (!Number.isInteger(id) || id <= 0)
|
|
434
|
+
throw new Error(USAGE);
|
|
435
|
+
d.out((await p.commentView(id)).body);
|
|
436
|
+
return 0;
|
|
437
|
+
}
|
|
438
|
+
case "label": {
|
|
439
|
+
const pr = Number(rest[0]);
|
|
440
|
+
const add = rest.includes("--add") ? option(rest, "--add") : undefined;
|
|
441
|
+
const remove = rest.includes("--remove")
|
|
442
|
+
? option(rest, "--remove")
|
|
443
|
+
: undefined;
|
|
444
|
+
if (!Number.isInteger(pr) ||
|
|
445
|
+
pr <= 0 ||
|
|
446
|
+
(add === undefined) === (remove === undefined))
|
|
447
|
+
throw new Error(USAGE);
|
|
448
|
+
if (add !== undefined) {
|
|
449
|
+
refuseOverride(add);
|
|
450
|
+
await p.addLabel(pr, add);
|
|
451
|
+
}
|
|
452
|
+
else
|
|
453
|
+
await p.removeLabel(pr, remove);
|
|
454
|
+
d.out(add !== undefined ? "added" : "removed");
|
|
455
|
+
return 0;
|
|
456
|
+
}
|
|
457
|
+
case "open": {
|
|
458
|
+
const title = option(rest, "--title");
|
|
459
|
+
const file = option(rest, "--body-file");
|
|
460
|
+
if (!title || !file)
|
|
461
|
+
throw new Error(USAGE);
|
|
462
|
+
const body = d.readFile(file);
|
|
463
|
+
refuseBareTags(body);
|
|
464
|
+
const number = await p.create({
|
|
465
|
+
title,
|
|
466
|
+
body,
|
|
467
|
+
head: d.git(["branch", "--show-current"]).trim(),
|
|
468
|
+
base: option(rest, "--base") ?? (await p.defaultBranch()),
|
|
469
|
+
draft: !rest.includes("--ready"),
|
|
470
|
+
});
|
|
471
|
+
d.out(String(number));
|
|
472
|
+
return 0;
|
|
473
|
+
}
|
|
474
|
+
case "view": {
|
|
475
|
+
const pr = Number(rest[0]);
|
|
476
|
+
const field = option(rest, "--field");
|
|
477
|
+
if (!Number.isInteger(pr) || pr <= 0 || !field)
|
|
478
|
+
throw new Error(USAGE);
|
|
479
|
+
const read = FIELDS[field];
|
|
480
|
+
if (!read)
|
|
481
|
+
throw new Error(`No field "${field}". A pull request's fields are ${Object.keys(FIELDS).join(", ")}.`);
|
|
482
|
+
d.out(read(await p.get(pr)));
|
|
483
|
+
return 0;
|
|
484
|
+
}
|
|
485
|
+
case "review": {
|
|
486
|
+
const [pr, id] = numbers;
|
|
487
|
+
if (!pr || !id)
|
|
488
|
+
throw new Error(USAGE);
|
|
489
|
+
const review = (await p.reviews(pr)).find((r) => r.id === id);
|
|
490
|
+
if (!review)
|
|
491
|
+
throw new Error(`review ${id} on #${pr} was not found`);
|
|
492
|
+
const comments = await p.reviewCommentsOf(pr, id);
|
|
493
|
+
d.out(JSON.stringify({
|
|
494
|
+
author: review.author,
|
|
495
|
+
body: review.body,
|
|
496
|
+
comments: comments.map((c) => ({
|
|
497
|
+
id: c.id,
|
|
498
|
+
author: c.author,
|
|
499
|
+
replyTo: c.replyTo,
|
|
500
|
+
path: c.path,
|
|
501
|
+
line: c.line,
|
|
502
|
+
body: c.body,
|
|
503
|
+
})),
|
|
504
|
+
}, null, 2));
|
|
505
|
+
return 0;
|
|
506
|
+
}
|
|
507
|
+
case "unverified": {
|
|
508
|
+
const [pr] = numbers;
|
|
509
|
+
if (!pr)
|
|
510
|
+
throw new Error(USAGE);
|
|
511
|
+
(await p.unverifiedCommits(pr)).forEach((line) => d.out(line));
|
|
512
|
+
return 0;
|
|
513
|
+
}
|
|
514
|
+
case "failed": {
|
|
515
|
+
const [pr] = numbers;
|
|
516
|
+
if (!pr)
|
|
517
|
+
throw new Error(USAGE);
|
|
518
|
+
const sha = (await p.get(pr)).head.sha;
|
|
519
|
+
const failedRuns = newestRuns(await d.runner.runs(sha)).filter((r) => r.status === "completed" && !passed(r.conclusion));
|
|
520
|
+
for (const run of failedRuns)
|
|
521
|
+
for (const job of await d.runner.failedJobs(run.id)) {
|
|
522
|
+
d.out(`== ${job.name}`);
|
|
523
|
+
d.out(job.log.trimEnd().split("\n").slice(-LOG_TAIL).join("\n"));
|
|
524
|
+
}
|
|
525
|
+
for (const check of await p.checkRuns(sha)) {
|
|
526
|
+
if (check.status !== "completed" || passed(check.conclusion))
|
|
527
|
+
continue;
|
|
528
|
+
if (!check.report)
|
|
529
|
+
continue;
|
|
530
|
+
d.out(`== ${check.name}, in its own words`);
|
|
531
|
+
d.out(check.report);
|
|
532
|
+
}
|
|
533
|
+
return 0;
|
|
534
|
+
}
|
|
535
|
+
case "review-again": {
|
|
536
|
+
const [pr] = numbers;
|
|
537
|
+
if (!pr)
|
|
538
|
+
throw new Error(USAGE);
|
|
539
|
+
const sha = (await p.get(pr)).head.sha;
|
|
540
|
+
if (!(await d.runner.reviewAgain(sha))) {
|
|
541
|
+
d.out(`no review has run on ${sha}`);
|
|
542
|
+
return 1;
|
|
543
|
+
}
|
|
544
|
+
d.out(`asked for a fresh review of ${sha}`);
|
|
545
|
+
return 0;
|
|
546
|
+
}
|
|
547
|
+
default:
|
|
548
|
+
throw new Error(USAGE);
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
catch (error) {
|
|
552
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
553
|
+
// The only-if gate reads 1 as "nothing to do", so a check that could not
|
|
554
|
+
// tell exits 2.
|
|
555
|
+
return command === "review-pending" || command === "conflicts" ? 2 : 1;
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
/** `factory pr ...`. */
|
|
559
|
+
export async function run(argv) {
|
|
560
|
+
const { repository, codeHost, runner } = await reachOf(process.env);
|
|
561
|
+
return main(argv, {
|
|
562
|
+
repository,
|
|
563
|
+
codeHost: openCodeHost(repository, codeHost),
|
|
564
|
+
runner: openRunner(repository, runner),
|
|
565
|
+
sleep,
|
|
566
|
+
out: (line) => console.log(line),
|
|
567
|
+
readFile: (path) => readFileSync(path, "utf8"),
|
|
568
|
+
git: (args) => execFileSync("git", args, { encoding: "utf8" }),
|
|
569
|
+
env: process.env,
|
|
570
|
+
});
|
|
571
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { type CodeHost } from "./code-host/index.mts";
|
|
3
|
+
/** The review's cost in dollars, or why there is none. */
|
|
4
|
+
export declare function reviewCost(text: string): {
|
|
5
|
+
dollars: number;
|
|
6
|
+
} | {
|
|
7
|
+
reason: string;
|
|
8
|
+
};
|
|
9
|
+
export type Deps = {
|
|
10
|
+
/** The repository's code host. */
|
|
11
|
+
codeHost: (repository: string) => CodeHost;
|
|
12
|
+
env: Record<string, string | undefined>;
|
|
13
|
+
readFile: (path: string) => string;
|
|
14
|
+
log: (line: string) => void;
|
|
15
|
+
};
|
|
16
|
+
export declare function main(d: Deps): Promise<number>;
|
|
17
|
+
/** `factory review cost`. */
|
|
18
|
+
export declare function run(): Promise<number>;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Adds what the CI review cost to its summary comment, after the review
|
|
3
|
+
// step of .github/workflows/factory-claude-review.yml:
|
|
4
|
+
//
|
|
5
|
+
// Review cost: $0.03 of the subscription ($0.92 at API list prices).
|
|
6
|
+
// <!-- cost: 0.92 -->
|
|
7
|
+
//
|
|
8
|
+
// The figure is the execution file's total_cost_usd when it has one, and
|
|
9
|
+
// otherwise its usage priced with lib/pricing.mts. The board sync
|
|
10
|
+
// adds the marker to the card's Cost. Nothing here fails the job: with no
|
|
11
|
+
// summary, no figure, or a failed call, it logs why and exits 0.
|
|
12
|
+
//
|
|
13
|
+
// EXECUTION_FILE=... PR=N SHA=... GITHUB_REPOSITORY=owner/name \
|
|
14
|
+
// npx --no-install factory review cost
|
|
15
|
+
import { readFileSync } from "node:fs";
|
|
16
|
+
import { openCodeHost } from "./code-host/index.mjs";
|
|
17
|
+
import { CI_BOT, hasCostMarker } from "./lib/work-status.mjs";
|
|
18
|
+
import { bothPrices, spendIn } from "./lib/usage.mjs";
|
|
19
|
+
/** The review's cost in dollars, or why there is none. */
|
|
20
|
+
export function reviewCost(text) {
|
|
21
|
+
let parsed;
|
|
22
|
+
try {
|
|
23
|
+
parsed = JSON.parse(text);
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return { reason: "the execution file is not JSON" };
|
|
27
|
+
}
|
|
28
|
+
const messages = (Array.isArray(parsed) ? parsed : [parsed]);
|
|
29
|
+
const total = messages.findLast((m) => m?.type === "result")?.total_cost_usd;
|
|
30
|
+
if (typeof total === "number" && Number.isFinite(total))
|
|
31
|
+
return { dollars: total };
|
|
32
|
+
const lines = messages
|
|
33
|
+
.filter((m) => m?.type === "assistant" && m.message)
|
|
34
|
+
.map((m) => JSON.stringify({
|
|
35
|
+
type: "assistant",
|
|
36
|
+
requestId: m.message.id,
|
|
37
|
+
message: m.message,
|
|
38
|
+
}));
|
|
39
|
+
const spend = spendIn([lines.join("\n")]);
|
|
40
|
+
if (spend.priced === 0)
|
|
41
|
+
return {
|
|
42
|
+
reason: spend.unpriced.size > 0
|
|
43
|
+
? `its usage is on models with no list price (${[...spend.unpriced.keys()].join(", ")})`
|
|
44
|
+
: "the execution file has no total_cost_usd and no usage",
|
|
45
|
+
};
|
|
46
|
+
return { dollars: spend.dollars };
|
|
47
|
+
}
|
|
48
|
+
export async function main(d) {
|
|
49
|
+
const { EXECUTION_FILE: file, PR: pr, SHA: sha } = d.env;
|
|
50
|
+
const repo = d.env.GITHUB_REPOSITORY;
|
|
51
|
+
try {
|
|
52
|
+
if (!file || !pr || !sha || !repo)
|
|
53
|
+
return (d.log("No review cost: EXECUTION_FILE, PR, SHA, or GITHUB_REPOSITORY is not set."),
|
|
54
|
+
0);
|
|
55
|
+
let text;
|
|
56
|
+
try {
|
|
57
|
+
text = d.readFile(file);
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
return (d.log(`No review cost: ${file} could not be read.`), 0);
|
|
61
|
+
}
|
|
62
|
+
const cost = reviewCost(text);
|
|
63
|
+
if ("reason" in cost)
|
|
64
|
+
return (d.log(`No review cost: ${cost.reason}.`), 0);
|
|
65
|
+
const pulls = d.codeHost(repo);
|
|
66
|
+
const comments = await pulls.comments(Number(pr));
|
|
67
|
+
const summary = comments
|
|
68
|
+
.filter((c) => c.author === CI_BOT &&
|
|
69
|
+
c.body.includes(`<!-- claude-review sha=${sha} -->`))
|
|
70
|
+
.at(-1);
|
|
71
|
+
if (!summary)
|
|
72
|
+
return (d.log(`No review cost: no summary comment for ${sha}.`), 0);
|
|
73
|
+
const { body } = summary;
|
|
74
|
+
// A rerun on a commit whose summary already has a cost adds none.
|
|
75
|
+
if (hasCostMarker(body))
|
|
76
|
+
return (d.log("The summary already has a cost; adding none."), 0);
|
|
77
|
+
const amount = cost.dollars.toFixed(2);
|
|
78
|
+
await pulls.editComment(summary.id, `${body.trimEnd()}\n\nReview cost: ${bothPrices(cost.dollars)}.\n\n<!-- cost: ${amount} -->`);
|
|
79
|
+
d.log(`Added the review cost, $${amount}, to comment ${summary.id}.`);
|
|
80
|
+
return 0;
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
d.log(`::warning::No review cost: ${error instanceof Error ? error.message : String(error)}`);
|
|
84
|
+
return 0;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/** `factory review cost`. */
|
|
88
|
+
export async function run() {
|
|
89
|
+
return main({
|
|
90
|
+
codeHost: (repository) => openCodeHost(repository),
|
|
91
|
+
env: process.env,
|
|
92
|
+
readFile: (path) => readFileSync(path, "utf8"),
|
|
93
|
+
log: (line) => console.log(line),
|
|
94
|
+
});
|
|
95
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/** What every review may do: read, edit its scratch copy, read the history
|
|
2
|
+
* and the pull request, and comment on it. */
|
|
3
|
+
export declare const FACTORY_REVIEW_TOOLS: string[];
|
|
4
|
+
export declare function allowedTools(commands: string[]): string;
|
|
5
|
+
export declare function run(): Promise<number>;
|