@mnaimfaizy/agent-kit 1.0.0-beta.15 → 1.0.0-beta.16
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/README.md +1 -1
- package/package.json +1 -1
- package/src/cli.mjs +314 -4
- package/src/doctor.mjs +53 -1
- package/src/exec.mjs +14 -5
- package/src/factory/budget/budget.mjs +259 -0
- package/src/factory/build/command.mjs +321 -0
- package/src/factory/build/factory.mjs +473 -0
- package/src/factory/build/findings.mjs +55 -0
- package/src/factory/build/host.mjs +230 -0
- package/src/factory/build/notes.mjs +191 -0
- package/src/factory/build/pull-request.mjs +74 -0
- package/src/factory/bundle.mjs +146 -0
- package/src/factory/checks/allowed-paths.mjs +50 -0
- package/src/factory/checks/plan-header.mjs +296 -0
- package/src/factory/checks/qa.mjs +85 -0
- package/src/factory/checks/review.mjs +73 -0
- package/src/factory/checks/security.mjs +50 -0
- package/src/factory/checks/stage-check.mjs +89 -0
- package/src/factory/claims/command.mjs +187 -0
- package/src/factory/claims/release.mjs +111 -0
- package/src/factory/decision/answers.mjs +103 -0
- package/src/factory/decision/command.mjs +64 -0
- package/src/factory/decision/plan.mjs +99 -0
- package/src/factory/decision/request.mjs +116 -0
- package/src/factory/doctor/files.mjs +329 -0
- package/src/factory/doctor/index.mjs +56 -0
- package/src/factory/doctor/local.mjs +497 -0
- package/src/factory/doctor/orchestrate.mjs +146 -0
- package/src/factory/engine/lease.mjs +146 -0
- package/src/factory/engine/local-engine.mjs +177 -0
- package/src/factory/engine/lockfile.mjs +64 -0
- package/src/factory/engine/workflow-engine.mjs +124 -0
- package/src/factory/gate/comment.mjs +88 -0
- package/src/factory/gate/config.mjs +57 -0
- package/src/factory/gate/gate-core.mjs +250 -0
- package/src/factory/gate/guarded.mjs +17 -0
- package/src/factory/gate/reads.mjs +105 -0
- package/src/factory/gate/task.mjs +193 -0
- package/src/factory/gate/trip-reads.mjs +82 -0
- package/src/factory/gate/trip.mjs +87 -0
- package/src/factory/init/blueprint.mjs +174 -0
- package/src/factory/init/index.mjs +337 -0
- package/src/factory/labels/evidence.mjs +162 -0
- package/src/factory/labels/moves.mjs +32 -0
- package/src/factory/labels/reconcile.mjs +267 -0
- package/src/factory/labels/revert.mjs +87 -0
- package/src/factory/labels/timeline.mjs +72 -0
- package/src/factory/labels/voided.mjs +59 -0
- package/src/factory/limits/harness.mjs +39 -0
- package/src/factory/limits/limits.mjs +123 -0
- package/src/factory/merge/command.mjs +172 -0
- package/src/factory/outbox/check.mjs +240 -0
- package/src/factory/outbox/rulings.mjs +195 -0
- package/src/factory/plan/command.mjs +66 -0
- package/src/factory/plan/comment.mjs +156 -0
- package/src/factory/plan/publish.mjs +149 -0
- package/src/factory/plan/risk.mjs +153 -0
- package/src/factory/qa/command.mjs +163 -0
- package/src/factory/records/engine-record.mjs +133 -0
- package/src/factory/records/main.mjs +96 -0
- package/src/factory/records/record.mjs +226 -0
- package/src/factory/records/sink.mjs +57 -0
- package/src/factory/report/build.mjs +92 -0
- package/src/factory/report/command.mjs +80 -0
- package/src/factory/report/facts.mjs +226 -0
- package/src/factory/report/gate-section.mjs +122 -0
- package/src/factory/report/metrics.mjs +111 -0
- package/src/factory/report/reads.mjs +246 -0
- package/src/factory/report/render.mjs +121 -0
- package/src/factory/report/rollout.mjs +154 -0
- package/src/factory/review/command.mjs +182 -0
- package/src/factory/run/awake.mjs +70 -0
- package/src/factory/run/collect.mjs +77 -0
- package/src/factory/run/command.mjs +490 -0
- package/src/factory/run/gh.mjs +46 -0
- package/src/factory/run/launch.mjs +47 -0
- package/src/factory/run/pipelines.mjs +261 -0
- package/src/factory/run/raw.mjs +96 -0
- package/src/factory/run/restart.mjs +83 -0
- package/src/factory/run/s3.mjs +193 -0
- package/src/factory/run/secrets.mjs +142 -0
- package/src/factory/run/teardown.mjs +242 -0
- package/src/factory/run/token.mjs +68 -0
- package/src/factory/security/command.mjs +157 -0
- package/src/factory/setup/app.mjs +122 -0
- package/src/factory/setup/command.mjs +409 -0
- package/src/factory/setup/labels.mjs +25 -0
- package/src/factory/setup/reads.mjs +129 -0
- package/src/factory/setup/writes.mjs +126 -0
- package/src/factory/snapshot/write.mjs +176 -0
- package/src/factory/stop/command.mjs +136 -0
- package/src/factory/sweep/actions-main.mjs +117 -0
- package/src/factory/sweep/command.mjs +65 -0
- package/src/factory/sweep/knobs.mjs +78 -0
- package/src/factory/sweep/marker-version.mjs +15 -0
- package/src/factory/sweep/markers.mjs +112 -0
- package/src/factory/sweep/overlap.mjs +24 -0
- package/src/factory/sweep/run.mjs +198 -0
- package/src/factory/sweep/shell.mjs +394 -0
- package/src/factory/sweep/split.mjs +104 -0
- package/src/factory/sweep/states.mjs +87 -0
- package/src/factory/sweep/sweep-core.mjs +1781 -0
- package/src/factory/tracker/github.mjs +674 -0
- package/src/factory/tracker/operations.mjs +153 -0
- package/src/factory/upgrade/index.mjs +83 -0
- package/src/init.mjs +62 -24
- package/src/kit-owned-paths.mjs +1 -1
- package/src/stage.mjs +19 -0
- package/src/upgrade.mjs +39 -7
|
@@ -0,0 +1,497 @@
|
|
|
1
|
+
// `doctor`'s Factory local checks (spec #500, #520): what GitHub says about the
|
|
2
|
+
// Factory, read with an admin `gh` login. Every read is a GET through the shared
|
|
3
|
+
// `github` reader, but one: the pinned issues have no REST endpoint, so they are
|
|
4
|
+
// one GraphQL query (a read, as `factory setup` makes it). Nothing here writes.
|
|
5
|
+
// In CI mode `doctor` lists these checks as skipped; a skipped check is never a pass.
|
|
6
|
+
//
|
|
7
|
+
// factory-labels the labels `factory setup` creates exist
|
|
8
|
+
// factory-control the control issue exists and is pinned; a live Engine lease is shown
|
|
9
|
+
// factory-app the App is installed on only this repository with exactly Contents and
|
|
10
|
+
// Pull requests write (Workflow engine)
|
|
11
|
+
// factory-environment the `factory-publish` environment and the App key secret
|
|
12
|
+
// factory-client-id the repository variable `AGENT_KIT_APP_ID`
|
|
13
|
+
// factory-ruleset an active default-branch ruleset, with no App bypass
|
|
14
|
+
// factory-required-context no required status check named `factory/*`
|
|
15
|
+
// factory-kill-switch reports the Kill switch; never fails
|
|
16
|
+
// factory-cap-override reports the owner's Daily cap override; never fails
|
|
17
|
+
|
|
18
|
+
import { LABEL_PATTERN, result } from "../../checks/result.mjs";
|
|
19
|
+
import { coversRef, loadRulesets } from "../../checks/rulesets.mjs";
|
|
20
|
+
import { readMode } from "../../checks/review-response.mjs";
|
|
21
|
+
import { listNames } from "../../checks/secrets-and-variables.mjs";
|
|
22
|
+
import { firstLine } from "../../exec.mjs";
|
|
23
|
+
import { BUDGET_SOURCE_OVERRIDE } from "../budget/budget.mjs";
|
|
24
|
+
import { standingLease } from "../engine/lease.mjs";
|
|
25
|
+
import { isControlBody } from "../init/blueprint.mjs";
|
|
26
|
+
import { APP_PERMISSIONS } from "../setup/app.mjs";
|
|
27
|
+
import { SETUP_LABELS } from "../setup/labels.mjs";
|
|
28
|
+
import { orchestrateLocalChecks } from "./orchestrate.mjs";
|
|
29
|
+
import {
|
|
30
|
+
APP_SECRET,
|
|
31
|
+
APP_VARIABLE,
|
|
32
|
+
CONTROL_TITLE,
|
|
33
|
+
ENVIRONMENT,
|
|
34
|
+
KILL_SWITCH,
|
|
35
|
+
} from "../setup/command.mjs";
|
|
36
|
+
|
|
37
|
+
export const FACTORY_LOCAL_CHECKS = Object.freeze([
|
|
38
|
+
["factory-labels", "Factory labels exist"],
|
|
39
|
+
["factory-control", "Factory control issue is pinned"],
|
|
40
|
+
["factory-app", "Factory App is installed on this repository only, with two permissions"],
|
|
41
|
+
["factory-environment", "Factory environment and key secret are set"],
|
|
42
|
+
["factory-client-id", "Factory App client ID is set"],
|
|
43
|
+
["factory-ruleset", "Default branch has a ruleset with no App bypass"],
|
|
44
|
+
["factory-required-context", "No ruleset requires a factory/* check"],
|
|
45
|
+
["factory-kill-switch", "Kill switch (reported, never failed)"],
|
|
46
|
+
["factory-cap-override", "Daily cap override (reported, never failed)"],
|
|
47
|
+
]);
|
|
48
|
+
const TITLE = Object.fromEntries(FACTORY_LOCAL_CHECKS);
|
|
49
|
+
const make = (id, status, summary, opts) => result(id, TITLE[id], status, summary, opts);
|
|
50
|
+
|
|
51
|
+
const notFound = (err) => err?.httpStatus === 404;
|
|
52
|
+
|
|
53
|
+
/** The pinned issues of the repository: one GraphQL query, a read. */
|
|
54
|
+
export function readPinnedNumbers({ runner, cwd, repo, host }) {
|
|
55
|
+
const [owner, name] = repo.split("/");
|
|
56
|
+
const hostArgs = host && host !== "github.com" ? ["--hostname", host] : [];
|
|
57
|
+
const r = runner(
|
|
58
|
+
"gh",
|
|
59
|
+
[
|
|
60
|
+
"api",
|
|
61
|
+
...hostArgs,
|
|
62
|
+
"graphql",
|
|
63
|
+
"-f",
|
|
64
|
+
"query=query($o:String!,$n:String!){repository(owner:$o,name:$n){pinnedIssues(first:3){nodes{issue{number}}}}}",
|
|
65
|
+
"-f",
|
|
66
|
+
`o=${owner}`,
|
|
67
|
+
"-f",
|
|
68
|
+
`n=${name}`,
|
|
69
|
+
],
|
|
70
|
+
{ cwd },
|
|
71
|
+
);
|
|
72
|
+
if (r.status !== 0) throw new Error(`read pinned issues: ${firstLine(r.stderr) || "gh failed"}`);
|
|
73
|
+
const data = JSON.parse(r.stdout || "null");
|
|
74
|
+
return (data?.data?.repository?.pinnedIssues?.nodes ?? []).map((n) => n.issue.number);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** The open issue that carries the control marker, with its comments. */
|
|
78
|
+
function readControl(github, repo) {
|
|
79
|
+
const issues = github.getLines(
|
|
80
|
+
`repos/${repo}/issues?state=open&per_page=100`,
|
|
81
|
+
'.[] | select(.pull_request == null) | [.number, (.body // "")] | @json',
|
|
82
|
+
);
|
|
83
|
+
for (const line of issues) {
|
|
84
|
+
const [number, body] = JSON.parse(line);
|
|
85
|
+
if (isControlBody(body)) return number;
|
|
86
|
+
}
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function readComments(github, repo, number) {
|
|
91
|
+
return github
|
|
92
|
+
.getLines(
|
|
93
|
+
`repos/${repo}/issues/${number}/comments?per_page=100`,
|
|
94
|
+
'.[] | [.id, (.user.login // ""), (.body // "")] | @json',
|
|
95
|
+
)
|
|
96
|
+
.map((line) => {
|
|
97
|
+
const [id, author, body] = JSON.parse(line);
|
|
98
|
+
return { id, author, body };
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Whether `login` has write or admin access to `repo`; any failed read is "no". */
|
|
103
|
+
function hasWrite(github, repo, login) {
|
|
104
|
+
if (!/^[A-Za-z0-9](?:[A-Za-z0-9-]{0,38})$/.test(login)) return false;
|
|
105
|
+
try {
|
|
106
|
+
const [permission] = github.getLines(
|
|
107
|
+
`repos/${repo}/collaborators/${login}/permission`,
|
|
108
|
+
".permission",
|
|
109
|
+
);
|
|
110
|
+
return permission === "admin" || permission === "write";
|
|
111
|
+
} catch {
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function checkLabels({ github, repo }) {
|
|
117
|
+
const have = new Set(
|
|
118
|
+
github.getLines(`repos/${repo}/labels`, ".[].name").map((n) => n.toLowerCase()),
|
|
119
|
+
);
|
|
120
|
+
const missing = SETUP_LABELS.map((l) => l.name).filter((n) => !have.has(n.toLowerCase()));
|
|
121
|
+
if (missing.length) {
|
|
122
|
+
return make("factory-labels", "fail", `${missing.length} Factory label(s) do not exist.`, {
|
|
123
|
+
details: missing.map((n) => (LABEL_PATTERN.test(n) ? n : JSON.stringify(n))),
|
|
124
|
+
fix: ["agent-kit factory setup"],
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
return make("factory-labels", "pass", `All ${SETUP_LABELS.length} Factory labels exist.`);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function checkControl({ github, runner, cwd, repo, host, now }) {
|
|
131
|
+
const number = readControl(github, repo);
|
|
132
|
+
if (number === null) {
|
|
133
|
+
return make(
|
|
134
|
+
"factory-control",
|
|
135
|
+
"fail",
|
|
136
|
+
`No open "${CONTROL_TITLE}" issue carries the control marker.`,
|
|
137
|
+
{
|
|
138
|
+
details: ["It holds the Engine lease, the gate's trip marker and the cap notices."],
|
|
139
|
+
fix: ["agent-kit factory setup"],
|
|
140
|
+
},
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
const pinned = readPinnedNumbers({ runner, cwd, repo, host });
|
|
144
|
+
const details = [];
|
|
145
|
+
// `doctor` does not know the Orchestrate engine's identity, so it counts a lease only when its
|
|
146
|
+
// author has write access to the repository, and says who wrote it.
|
|
147
|
+
const comments = readComments(github, repo, number);
|
|
148
|
+
const writers = new Set(
|
|
149
|
+
[...new Set(comments.map((c) => c.author))].filter((login) => hasWrite(github, repo, login)),
|
|
150
|
+
);
|
|
151
|
+
const lease = standingLease(
|
|
152
|
+
comments.filter((c) => writers.has(c.author)),
|
|
153
|
+
now(),
|
|
154
|
+
);
|
|
155
|
+
if (lease)
|
|
156
|
+
details.push(
|
|
157
|
+
`Engine lease live: ${lease.host} until ${lease.expires} (id ${lease.id}, written by ${lease.author}).`,
|
|
158
|
+
);
|
|
159
|
+
else details.push("No live Engine lease.");
|
|
160
|
+
if (!pinned.includes(number)) {
|
|
161
|
+
return make("factory-control", "fail", `The control issue #${number} is not pinned.`, {
|
|
162
|
+
details: [...details, "Pin it from the issue's sidebar, or run `agent-kit factory setup`."],
|
|
163
|
+
fix: ["agent-kit factory setup"],
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
return make("factory-control", "pass", `The control issue #${number} is open and pinned.`, {
|
|
167
|
+
details,
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const sameWrites = (permissions) => {
|
|
172
|
+
const have = Object.entries(permissions ?? {}).filter(([k]) => k !== "metadata");
|
|
173
|
+
const want = Object.entries(APP_PERMISSIONS);
|
|
174
|
+
return have.length === want.length && want.every(([k, v]) => permissions?.[k] === v);
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
function checkApp({ github, repo, owner, ownerType, engine }) {
|
|
178
|
+
if (engine === "orchestrate") {
|
|
179
|
+
return make("factory-app", "skip", "The Orchestrate engine uses a PAT: it has no App.");
|
|
180
|
+
}
|
|
181
|
+
if (ownerType !== "Organization") {
|
|
182
|
+
return make(
|
|
183
|
+
"factory-app",
|
|
184
|
+
"skip",
|
|
185
|
+
"GitHub does not show a personal account's app installations to a gh login.",
|
|
186
|
+
{
|
|
187
|
+
details: [
|
|
188
|
+
"Check https://github.com/settings/installations: Contents and Pull requests write, this repository only.",
|
|
189
|
+
],
|
|
190
|
+
},
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
const id = readMode(github, repo, ownerType, APP_VARIABLE)?.trim();
|
|
194
|
+
if (!id) return make("factory-app", "skip", `Needs ${APP_VARIABLE} to know which App is yours.`);
|
|
195
|
+
const rows = github
|
|
196
|
+
.getLines(
|
|
197
|
+
`orgs/${owner}/installations`,
|
|
198
|
+
'.installations[] | [(.app_id | tostring), (.client_id // ""), .repository_selection, (.permissions // {})] | @json',
|
|
199
|
+
)
|
|
200
|
+
.map((l) => JSON.parse(l));
|
|
201
|
+
const found = rows.find(([app, client]) => [app, client].includes(id));
|
|
202
|
+
const settings = `https://github.com/organizations/${owner}/settings/installations`;
|
|
203
|
+
if (!found) {
|
|
204
|
+
return make(
|
|
205
|
+
"factory-app",
|
|
206
|
+
"fail",
|
|
207
|
+
`No app with the id in ${APP_VARIABLE} is installed on ${owner}.`,
|
|
208
|
+
{
|
|
209
|
+
details: [`Install your App on ${repo} only: ${settings}`],
|
|
210
|
+
},
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
const [, , selection, permissions] = found;
|
|
214
|
+
const problems = [];
|
|
215
|
+
if (selection === "all")
|
|
216
|
+
problems.push(`It is installed on all of ${owner}'s repositories, not on ${repo} only.`);
|
|
217
|
+
if (!sameWrites(permissions)) {
|
|
218
|
+
const given = Object.entries(permissions ?? {})
|
|
219
|
+
.filter(([k]) => k !== "metadata")
|
|
220
|
+
.map(([k, v]) => `${k}: ${v}`)
|
|
221
|
+
.join(", ");
|
|
222
|
+
problems.push(
|
|
223
|
+
`Its permissions are ${given || "none"}; they must be exactly contents: write and pull_requests: write (Workflows stays off).`,
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
if (problems.length) {
|
|
227
|
+
return make("factory-app", "fail", "The Factory App is wider or narrower than it should be.", {
|
|
228
|
+
details: [...problems, `Edit it at ${settings}`],
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
return make(
|
|
232
|
+
"factory-app",
|
|
233
|
+
"pass",
|
|
234
|
+
"The Factory App holds exactly Contents and Pull requests write, on selected repositories.",
|
|
235
|
+
{
|
|
236
|
+
details: [
|
|
237
|
+
`gh cannot list which repositories are selected: check that only ${repo} is, at ${settings}`,
|
|
238
|
+
],
|
|
239
|
+
},
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function checkEnvironment({ github, repo, ownerType, engine }) {
|
|
244
|
+
if (engine === "orchestrate") {
|
|
245
|
+
return make(
|
|
246
|
+
"factory-environment",
|
|
247
|
+
"skip",
|
|
248
|
+
"The Orchestrate engine uses a PAT: it has no environment or key secret.",
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
let environment = null;
|
|
252
|
+
try {
|
|
253
|
+
environment = github.getJson(`repos/${repo}/environments/${ENVIRONMENT}`);
|
|
254
|
+
} catch (err) {
|
|
255
|
+
if (!notFound(err)) throw err;
|
|
256
|
+
}
|
|
257
|
+
const repoSecret = listNames(github, repo, "secret", ownerType).names.has(APP_SECRET);
|
|
258
|
+
if (!environment) {
|
|
259
|
+
if (repoSecret) {
|
|
260
|
+
return make(
|
|
261
|
+
"factory-environment",
|
|
262
|
+
"pass",
|
|
263
|
+
`The key is a repository secret (${APP_SECRET}); there is no ${ENVIRONMENT} environment.`,
|
|
264
|
+
{
|
|
265
|
+
details: [
|
|
266
|
+
"An environment secret limited to the default branch is safer; `factory setup` stores it there where the plan allows.",
|
|
267
|
+
],
|
|
268
|
+
},
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
return make(
|
|
272
|
+
"factory-environment",
|
|
273
|
+
"fail",
|
|
274
|
+
`The ${ENVIRONMENT} environment does not exist, and ${APP_SECRET} is not a repository secret.`,
|
|
275
|
+
{
|
|
276
|
+
fix: ["agent-kit factory setup"],
|
|
277
|
+
},
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
const inEnv = github.getLines(
|
|
281
|
+
`repos/${repo}/environments/${ENVIRONMENT}/secrets`,
|
|
282
|
+
".secrets[].name",
|
|
283
|
+
);
|
|
284
|
+
if (!inEnv.includes(APP_SECRET) && !repoSecret) {
|
|
285
|
+
return make("factory-environment", "fail", `${APP_SECRET} is not set in ${ENVIRONMENT}.`, {
|
|
286
|
+
fix: ["agent-kit factory setup"],
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
const details = [];
|
|
290
|
+
let status = "pass";
|
|
291
|
+
if (!environment.deployment_branch_policy) {
|
|
292
|
+
status = "warn";
|
|
293
|
+
details.push(
|
|
294
|
+
`${ENVIRONMENT} is open to every branch: limit its deployment branches to the default branch.`,
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
return make("factory-environment", status, `${ENVIRONMENT} exists and ${APP_SECRET} is set.`, {
|
|
298
|
+
details,
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function checkClientId({ github, repo, ownerType, engine }) {
|
|
303
|
+
if (engine === "orchestrate") {
|
|
304
|
+
return make(
|
|
305
|
+
"factory-client-id",
|
|
306
|
+
"skip",
|
|
307
|
+
"The Orchestrate engine uses a PAT: it has no App client ID.",
|
|
308
|
+
);
|
|
309
|
+
}
|
|
310
|
+
const names = listNames(github, repo, "variable", ownerType);
|
|
311
|
+
if (names.names.has(APP_VARIABLE)) {
|
|
312
|
+
return make("factory-client-id", "pass", `${APP_VARIABLE} is set.`);
|
|
313
|
+
}
|
|
314
|
+
if (names.orgNote) return make("factory-client-id", "skip", names.orgNote);
|
|
315
|
+
return make("factory-client-id", "fail", `${APP_VARIABLE} is not set.`, {
|
|
316
|
+
details: ["The build's publish job and the merge job read it to mint the App token."],
|
|
317
|
+
fix: ["agent-kit factory setup"],
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
function defaultBranchRulesets(rulesets, defaultBranch) {
|
|
322
|
+
return rulesets.filter(
|
|
323
|
+
(r) => r.target === "branch" && coversRef(r, `refs/heads/${defaultBranch}`, { defaultBranch }),
|
|
324
|
+
);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
function checkRuleset({ rulesets, defaultBranch }) {
|
|
328
|
+
const mine = defaultBranchRulesets(rulesets, defaultBranch);
|
|
329
|
+
if (!mine.length) {
|
|
330
|
+
return make(
|
|
331
|
+
"factory-ruleset",
|
|
332
|
+
"fail",
|
|
333
|
+
`No active ruleset covers the default branch (${defaultBranch}).`,
|
|
334
|
+
{
|
|
335
|
+
details: ["Import .github/factory/ruleset.json: `agent-kit factory setup` does."],
|
|
336
|
+
fix: ["agent-kit factory setup"],
|
|
337
|
+
},
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
const label = (r) => `"${r.name ?? `ruleset ${r.id}`}"`;
|
|
341
|
+
const bypassed = mine.filter((r) =>
|
|
342
|
+
(r.bypass_actors ?? []).some((a) => a.actor_type === "Integration"),
|
|
343
|
+
);
|
|
344
|
+
if (bypassed.length) {
|
|
345
|
+
return make(
|
|
346
|
+
"factory-ruleset",
|
|
347
|
+
"fail",
|
|
348
|
+
"A GitHub App can bypass the default branch's ruleset.",
|
|
349
|
+
{
|
|
350
|
+
details: [
|
|
351
|
+
...bypassed.map((r) => `${label(r)} lists an App (Integration) as a bypass actor.`),
|
|
352
|
+
"The App pushes factory branches and merges: it must not be able to bypass the branch rules.",
|
|
353
|
+
],
|
|
354
|
+
},
|
|
355
|
+
);
|
|
356
|
+
}
|
|
357
|
+
const hidden = mine.filter((r) => !Array.isArray(r.bypass_actors));
|
|
358
|
+
if (hidden.length) {
|
|
359
|
+
return make(
|
|
360
|
+
"factory-ruleset",
|
|
361
|
+
"warn",
|
|
362
|
+
"A ruleset's bypass list is not visible to this login.",
|
|
363
|
+
{
|
|
364
|
+
details: hidden.map((r) => `${label(r)}: check by hand that no App can bypass it.`),
|
|
365
|
+
},
|
|
366
|
+
);
|
|
367
|
+
}
|
|
368
|
+
return make(
|
|
369
|
+
"factory-ruleset",
|
|
370
|
+
"pass",
|
|
371
|
+
`${mine.length} active ruleset${mine.length === 1 ? "" : "s"} cover ${defaultBranch}, with no App bypass.`,
|
|
372
|
+
);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
const FACTORY_CONTEXT = /^factory\//i;
|
|
376
|
+
|
|
377
|
+
function checkRequiredContext({ github, repo, rulesets, defaultBranch }) {
|
|
378
|
+
const found = [];
|
|
379
|
+
for (const r of rulesets) {
|
|
380
|
+
for (const rule of r.rules ?? []) {
|
|
381
|
+
if (rule.type !== "required_status_checks") continue;
|
|
382
|
+
for (const c of rule.parameters?.required_status_checks ?? []) {
|
|
383
|
+
if (FACTORY_CONTEXT.test(String(c.context)))
|
|
384
|
+
found.push(`ruleset "${r.name ?? r.id}" requires ${c.context}.`);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
const details = [];
|
|
389
|
+
try {
|
|
390
|
+
const protection = github.getJson(
|
|
391
|
+
`repos/${repo}/branches/${defaultBranch}/protection/required_status_checks`,
|
|
392
|
+
);
|
|
393
|
+
const contexts = [
|
|
394
|
+
...(protection.contexts ?? []),
|
|
395
|
+
...(protection.checks ?? []).map((c) => c.context),
|
|
396
|
+
];
|
|
397
|
+
for (const c of contexts) {
|
|
398
|
+
if (FACTORY_CONTEXT.test(String(c)))
|
|
399
|
+
found.push(`branch protection on ${defaultBranch} requires ${c}.`);
|
|
400
|
+
}
|
|
401
|
+
} catch (err) {
|
|
402
|
+
if (!notFound(err))
|
|
403
|
+
details.push(`Branch protection could not be read: ${firstLine(err.message)}`);
|
|
404
|
+
}
|
|
405
|
+
if (found.length) {
|
|
406
|
+
return make("factory-required-context", "fail", "A factory/* check is required for merging.", {
|
|
407
|
+
details: [
|
|
408
|
+
...found,
|
|
409
|
+
"The merge gate checks the Stage checks itself, by creator (ADR 0041): a required context would block every pull request that is not a factory one.",
|
|
410
|
+
],
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
return make(
|
|
414
|
+
"factory-required-context",
|
|
415
|
+
"pass",
|
|
416
|
+
"No ruleset or branch protection requires a factory/* check.",
|
|
417
|
+
{ details },
|
|
418
|
+
);
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
function reportKillSwitch({ github, repo, ownerType }) {
|
|
422
|
+
const value = readMode(github, repo, ownerType, KILL_SWITCH);
|
|
423
|
+
if (value === null) {
|
|
424
|
+
return make(
|
|
425
|
+
"factory-kill-switch",
|
|
426
|
+
"pass",
|
|
427
|
+
`${KILL_SWITCH} is not set: the Factory is drained (off).`,
|
|
428
|
+
);
|
|
429
|
+
}
|
|
430
|
+
if (value === "true")
|
|
431
|
+
return make("factory-kill-switch", "pass", `${KILL_SWITCH} is true: the Factory is on.`);
|
|
432
|
+
return make(
|
|
433
|
+
"factory-kill-switch",
|
|
434
|
+
"pass",
|
|
435
|
+
`${KILL_SWITCH} is ${JSON.stringify(value.slice(0, 40))}, not exactly true: the Factory is drained (off).`,
|
|
436
|
+
);
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
function reportCapOverride({ github, repo, ownerType }) {
|
|
440
|
+
const value = readMode(github, repo, ownerType, BUDGET_SOURCE_OVERRIDE);
|
|
441
|
+
if (value === null || value.trim() === "") {
|
|
442
|
+
return make(
|
|
443
|
+
"factory-cap-override",
|
|
444
|
+
"pass",
|
|
445
|
+
`${BUDGET_SOURCE_OVERRIDE} is not set: budget.daily_tokens is the Daily cap.`,
|
|
446
|
+
);
|
|
447
|
+
}
|
|
448
|
+
return make(
|
|
449
|
+
"factory-cap-override",
|
|
450
|
+
"pass",
|
|
451
|
+
`${BUDGET_SOURCE_OVERRIDE} is ${JSON.stringify(value.trim().slice(0, 40))}: it overrides budget.daily_tokens.`,
|
|
452
|
+
);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* The Factory's local checks. `guard(id, run, title)` turns a throw into a failed check.
|
|
457
|
+
* @param {{ github: object, runner: Function, cwd: string, info: { repo: string, owner: string, ownerType: string, host?: string|null, defaultBranch: string }, engine: "workflow"|"orchestrate", guard: Function, now?: () => number, env?: Record<string, string|undefined> }} input
|
|
458
|
+
*/
|
|
459
|
+
export function factoryLocalChecks({
|
|
460
|
+
github,
|
|
461
|
+
runner,
|
|
462
|
+
cwd,
|
|
463
|
+
info,
|
|
464
|
+
engine,
|
|
465
|
+
guard,
|
|
466
|
+
now = Date.now,
|
|
467
|
+
env = process.env,
|
|
468
|
+
}) {
|
|
469
|
+
const { repo, owner, ownerType, defaultBranch } = info;
|
|
470
|
+
const host = info.host ?? null;
|
|
471
|
+
const base = { github, runner, cwd, repo, owner, ownerType, host, engine, now, defaultBranch };
|
|
472
|
+
let rulesets = null;
|
|
473
|
+
let rulesetError = null;
|
|
474
|
+
try {
|
|
475
|
+
rulesets = loadRulesets(github, repo);
|
|
476
|
+
} catch (err) {
|
|
477
|
+
rulesetError = `Could not read rulesets: ${firstLine(err.message)}`;
|
|
478
|
+
}
|
|
479
|
+
const g = (id, run) => guard(id, run, TITLE[id]);
|
|
480
|
+
const skip = (id, why) => make(id, "skip", why);
|
|
481
|
+
return [
|
|
482
|
+
g("factory-labels", () => checkLabels(base)),
|
|
483
|
+
g("factory-control", () => checkControl(base)),
|
|
484
|
+
g("factory-app", () => checkApp(base)),
|
|
485
|
+
g("factory-environment", () => checkEnvironment(base)),
|
|
486
|
+
g("factory-client-id", () => checkClientId(base)),
|
|
487
|
+
rulesets
|
|
488
|
+
? g("factory-ruleset", () => checkRuleset({ rulesets, defaultBranch }))
|
|
489
|
+
: skip("factory-ruleset", rulesetError),
|
|
490
|
+
rulesets
|
|
491
|
+
? g("factory-required-context", () => checkRequiredContext({ ...base, rulesets }))
|
|
492
|
+
: skip("factory-required-context", rulesetError),
|
|
493
|
+
g("factory-kill-switch", () => reportKillSwitch(base)),
|
|
494
|
+
g("factory-cap-override", () => reportCapOverride(base)),
|
|
495
|
+
...(engine === "orchestrate" ? orchestrateLocalChecks({ runner, cwd, info, guard, env }) : []),
|
|
496
|
+
];
|
|
497
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
// `doctor`'s checks that are the Orchestrate engine's own (spec #500, #524): the
|
|
2
|
+
// engine holds a fine-grained PAT instead of an App, so what `doctor` can say about
|
|
3
|
+
// it is the token's reach and what it can read. Both read through the token itself,
|
|
4
|
+
// GET only, and only where `doctor` can read the token: a literal in its environment.
|
|
5
|
+
// An `op://` reference is not resolved here (`doctor` runs no `op`; `factory run`
|
|
6
|
+
// resolves it at start), and a token that is not in this environment is not seen.
|
|
7
|
+
// The token is never printed. Docs: docs/factory/doctor.md.
|
|
8
|
+
//
|
|
9
|
+
// factory-token the token is fine-grained and reaches this repository only
|
|
10
|
+
// factory-token-reads the token can read this repository's Contents, Issues,
|
|
11
|
+
// Pull requests, Statuses and Variables
|
|
12
|
+
//
|
|
13
|
+
// A write permission cannot be tested without writing, so a read that works shows the
|
|
14
|
+
// permission is there at least to read; Variables read, the one with no public
|
|
15
|
+
// fallback, is the one that proves itself.
|
|
16
|
+
|
|
17
|
+
import { result } from "../../checks/result.mjs";
|
|
18
|
+
import { firstLine } from "../../exec.mjs";
|
|
19
|
+
import { checkToken } from "../run/token.mjs";
|
|
20
|
+
import { TOKEN_VAR } from "../run/secrets.mjs";
|
|
21
|
+
|
|
22
|
+
export const ORCHESTRATE_LOCAL_CHECKS = Object.freeze([
|
|
23
|
+
["factory-token", "Orchestrate token reaches this repository only"],
|
|
24
|
+
["factory-token-reads", "Orchestrate token can read this repository's Factory data"],
|
|
25
|
+
]);
|
|
26
|
+
const TITLE = Object.fromEntries(ORCHESTRATE_LOCAL_CHECKS);
|
|
27
|
+
const make = (id, status, summary, opts) => result(id, TITLE[id], status, summary, opts);
|
|
28
|
+
|
|
29
|
+
const SEE = "docs/factory/host.md";
|
|
30
|
+
|
|
31
|
+
/** What `doctor` can do with the token in its environment: use it, or why not. */
|
|
32
|
+
export function readToken(env) {
|
|
33
|
+
const raw = env[TOKEN_VAR];
|
|
34
|
+
if (raw === undefined || raw === "") {
|
|
35
|
+
return {
|
|
36
|
+
why: `${TOKEN_VAR} is not set in this environment: the engine reads it from where it runs.`,
|
|
37
|
+
details: [`Run doctor from the engine's environment (the unit's environment file, ${SEE}).`],
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
if (String(raw).startsWith("op://")) {
|
|
41
|
+
return {
|
|
42
|
+
why: `${TOKEN_VAR} is an op:// reference: doctor runs no op, and factory run resolves it at start.`,
|
|
43
|
+
details: ["To check the token's reach, run `agent-kit factory run --once` where it runs."],
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
if (!/^[\x21-\x7e]+$/.test(raw)) {
|
|
47
|
+
return { bad: `${TOKEN_VAR} is not a token (its value is not shown).` };
|
|
48
|
+
}
|
|
49
|
+
return { token: String(raw) };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** `gh` with the token in the call's environment only, never an argument. */
|
|
53
|
+
function ghWith({ runner, cwd, token, host }) {
|
|
54
|
+
const env = { GH_TOKEN: token, GH_PROMPT_DISABLED: "1" };
|
|
55
|
+
if (host && host !== "github.com") env.GH_HOST = host;
|
|
56
|
+
return (args) => runner("gh", args, { cwd, env });
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function checkReach({ gh, repo, read }) {
|
|
60
|
+
if (read.bad) return make("factory-token", "fail", read.bad);
|
|
61
|
+
if (!read.token) return make("factory-token", "skip", read.why, { details: read.details });
|
|
62
|
+
const reach = checkToken({ gh, repo, token: read.token });
|
|
63
|
+
if (reach.refused) {
|
|
64
|
+
return make("factory-token", "fail", `The engine would refuse this token: ${reach.refused}.`, {
|
|
65
|
+
details: [`Make a fine-grained token for ${repo} alone: agent-kit factory setup prints how.`],
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
if (reach.notice) {
|
|
69
|
+
return make("factory-token", "warn", `${reach.login}'s token: ${reach.notice}.`);
|
|
70
|
+
}
|
|
71
|
+
return make(
|
|
72
|
+
"factory-token",
|
|
73
|
+
"pass",
|
|
74
|
+
`${reach.login}'s token is fine-grained and reaches ${repo} only.`,
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const READS = (repo, branch) => [
|
|
79
|
+
["Contents", `repos/${repo}/commits/${branch}`],
|
|
80
|
+
["Issues", `repos/${repo}/issues?per_page=1`],
|
|
81
|
+
["Pull requests", `repos/${repo}/pulls?per_page=1`],
|
|
82
|
+
["Commit statuses", `repos/${repo}/commits/${branch}/statuses?per_page=1`],
|
|
83
|
+
["Variables", `repos/${repo}/actions/variables?per_page=1`],
|
|
84
|
+
];
|
|
85
|
+
|
|
86
|
+
function checkReads({ gh, repo, defaultBranch, host, read, reach }) {
|
|
87
|
+
if (!read.token || reach.status !== "pass") {
|
|
88
|
+
const why = !read.token
|
|
89
|
+
? (read.why ?? read.bad)
|
|
90
|
+
: "its reach is not shown to be right, so nothing is read through it.";
|
|
91
|
+
return make("factory-token-reads", "skip", why);
|
|
92
|
+
}
|
|
93
|
+
const hostArgs = host && host !== "github.com" ? ["--hostname", host] : [];
|
|
94
|
+
const lacking = [];
|
|
95
|
+
for (const [name, endpoint] of READS(repo, defaultBranch)) {
|
|
96
|
+
const r = gh(["api", ...hostArgs, "--method", "GET", endpoint]);
|
|
97
|
+
if (r.status === 0) continue;
|
|
98
|
+
const text = `${r.stderr}\n${r.stdout}`;
|
|
99
|
+
if (/HTTP (?:401|403|404)/.test(text)) lacking.push(name);
|
|
100
|
+
else throw new Error(`read ${name}: ${firstLine(r.stderr) || "gh failed"}`);
|
|
101
|
+
}
|
|
102
|
+
if (lacking.length) {
|
|
103
|
+
return make(
|
|
104
|
+
"factory-token-reads",
|
|
105
|
+
"fail",
|
|
106
|
+
`The token cannot read ${lacking.join(", ")} on ${repo}.`,
|
|
107
|
+
{
|
|
108
|
+
details: [
|
|
109
|
+
"It needs Contents, Issues, Pull requests and Statuses write, Variables read.",
|
|
110
|
+
"Edit the token's repository permissions on GitHub, or make a new one.",
|
|
111
|
+
],
|
|
112
|
+
},
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
return make(
|
|
116
|
+
"factory-token-reads",
|
|
117
|
+
"pass",
|
|
118
|
+
`The token reads ${repo}'s Contents, Issues, Pull requests, Statuses and Variables.`,
|
|
119
|
+
{
|
|
120
|
+
details: [
|
|
121
|
+
"A write permission cannot be tested without writing. On a public repository GitHub lets any token read all but Variables.",
|
|
122
|
+
],
|
|
123
|
+
},
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* The Orchestrate engine's own local checks.
|
|
129
|
+
* @param {{ runner: Function, cwd: string, info: { repo: string, host?: string|null, defaultBranch: string }, guard: Function, env: Record<string, string|undefined> }} input
|
|
130
|
+
*/
|
|
131
|
+
export function orchestrateLocalChecks({ runner, cwd, info, guard, env }) {
|
|
132
|
+
const read = readToken(env);
|
|
133
|
+
const host = info.host ?? null;
|
|
134
|
+
const gh = read.token ? ghWith({ runner, cwd, token: read.token, host }) : null;
|
|
135
|
+
const reach = guard(
|
|
136
|
+
"factory-token",
|
|
137
|
+
() => checkReach({ gh, repo: info.repo, read }),
|
|
138
|
+
TITLE["factory-token"],
|
|
139
|
+
);
|
|
140
|
+
const reads = guard(
|
|
141
|
+
"factory-token-reads",
|
|
142
|
+
() => checkReads({ gh, repo: info.repo, defaultBranch: info.defaultBranch, host, read, reach }),
|
|
143
|
+
TITLE["factory-token-reads"],
|
|
144
|
+
);
|
|
145
|
+
return [reach, reads];
|
|
146
|
+
}
|