@mnaimfaizy/agent-kit 1.0.0-beta.15 → 1.0.0-beta.17
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 +2 -1
- package/package.json +1 -1
- package/src/cli.mjs +332 -5
- package/src/deep-scan/docker-driver.mjs +70 -3
- package/src/deep-scan/lifecycle.mjs +2 -2
- package/src/deep-scan/sbx-driver.mjs +34 -47
- package/src/doctor.mjs +53 -1
- package/src/exec.mjs +18 -5
- package/src/factory/budget/budget.mjs +260 -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 +349 -0
- package/src/factory/doctor/index.mjs +56 -0
- package/src/factory/doctor/local.mjs +513 -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 +181 -0
- package/src/factory/init/index.mjs +338 -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 +131 -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 +301 -0
- package/src/factory/qa/decide.mjs +86 -0
- package/src/factory/qa/report.mjs +50 -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/brief.mjs +195 -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 +556 -0
- package/src/factory/run/gh.mjs +46 -0
- package/src/factory/run/launch.mjs +66 -0
- package/src/factory/run/pipelines.mjs +694 -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 +70 -0
- package/src/factory/run/worktree.mjs +160 -0
- package/src/factory/security/audit.mjs +353 -0
- package/src/factory/security/command.mjs +157 -0
- package/src/factory/setup/app.mjs +122 -0
- package/src/factory/setup/command.mjs +472 -0
- package/src/factory/setup/labels.mjs +25 -0
- package/src/factory/setup/reads.mjs +159 -0
- package/src/factory/setup/writes.mjs +138 -0
- package/src/factory/snapshot/write.mjs +176 -0
- package/src/factory/stop/command.mjs +136 -0
- package/src/factory/sweep/actions-main.mjs +119 -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 +398 -0
- package/src/factory/sweep/split.mjs +104 -0
- package/src/factory/sweep/states.mjs +87 -0
- package/src/factory/sweep/sweep-core.mjs +2009 -0
- package/src/factory/tracker/github.mjs +713 -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/build.mjs +100 -0
- package/src/stage/factory-live.mjs +288 -0
- package/src/stage.mjs +100 -4
- package/src/upgrade.mjs +39 -7
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
// The risk rules (spec #500, Contracts): `{ id, tier, when: paths | size | label }`,
|
|
2
|
+
// read from the base branch alone. The Risk tier of a plan is the higher of the
|
|
3
|
+
// planner's proposal and what the rules give for its Allowed paths, size and the
|
|
4
|
+
// issue's labels, and the rules that raised it are named in `risk_raised_by`, so
|
|
5
|
+
// a planner cannot talk a risky change down. A missing or unreadable file makes
|
|
6
|
+
// every plan `high`. Pure: the trusted step reads the file and hands its text in.
|
|
7
|
+
// Contract suite: scripts/factory-risk-rules.test.mjs.
|
|
8
|
+
|
|
9
|
+
const TIER_ORDER = Object.freeze(["low", "medium", "high"]);
|
|
10
|
+
const ID = /^[a-z0-9][a-z0-9._-]{0,63}$/;
|
|
11
|
+
const WHEN_KEYS = Object.freeze(["paths", "size", "label"]);
|
|
12
|
+
const SIZE_NAMES = Object.freeze(["s", "m", "l"]);
|
|
13
|
+
|
|
14
|
+
/** The ids the trusted step records when the file cannot give any rule. */
|
|
15
|
+
export const RULES_MISSING = "risk-rules-missing";
|
|
16
|
+
export const RULES_UNREADABLE = "risk-rules-unreadable";
|
|
17
|
+
|
|
18
|
+
const rankOf = (tier) => TIER_ORDER.indexOf(tier);
|
|
19
|
+
const isListOf = (v) =>
|
|
20
|
+
Array.isArray(v) && v.length > 0 && v.every((s) => typeof s === "string" && s.trim() !== "");
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The rules in a file's text.
|
|
24
|
+
* @param {string | null | undefined} text null or undefined when the file is missing
|
|
25
|
+
* @returns {{ status: "ok", rules: Array<{ id: string, tier: string, when: object }> } | { status: "missing" | "unreadable" }}
|
|
26
|
+
*/
|
|
27
|
+
export function parseRiskRules(text) {
|
|
28
|
+
if (text === null || text === undefined) return { status: "missing" };
|
|
29
|
+
let data;
|
|
30
|
+
try {
|
|
31
|
+
data = JSON.parse(String(text));
|
|
32
|
+
} catch {
|
|
33
|
+
return { status: "unreadable" };
|
|
34
|
+
}
|
|
35
|
+
if (data === null || typeof data !== "object" || Array.isArray(data)) {
|
|
36
|
+
return { status: "unreadable" };
|
|
37
|
+
}
|
|
38
|
+
if (Object.keys(data).some((k) => k !== "rules") || !Array.isArray(data.rules)) {
|
|
39
|
+
return { status: "unreadable" };
|
|
40
|
+
}
|
|
41
|
+
const ids = new Set();
|
|
42
|
+
const rules = [];
|
|
43
|
+
for (const rule of data.rules) {
|
|
44
|
+
if (rule === null || typeof rule !== "object" || Array.isArray(rule)) {
|
|
45
|
+
return { status: "unreadable" };
|
|
46
|
+
}
|
|
47
|
+
if (Object.keys(rule).some((k) => !["id", "tier", "when"].includes(k))) {
|
|
48
|
+
return { status: "unreadable" };
|
|
49
|
+
}
|
|
50
|
+
const { id, tier, when } = rule;
|
|
51
|
+
if (typeof id !== "string" || !ID.test(id) || ids.has(id) || rankOf(tier) === -1) {
|
|
52
|
+
return { status: "unreadable" };
|
|
53
|
+
}
|
|
54
|
+
if (when === null || typeof when !== "object" || Array.isArray(when)) {
|
|
55
|
+
return { status: "unreadable" };
|
|
56
|
+
}
|
|
57
|
+
const keys = Object.keys(when);
|
|
58
|
+
if (keys.length !== 1 || !WHEN_KEYS.includes(keys[0]) || !isListOf(when[keys[0]])) {
|
|
59
|
+
return { status: "unreadable" };
|
|
60
|
+
}
|
|
61
|
+
if (keys[0] === "size" && !when.size.every((s) => SIZE_NAMES.includes(s))) {
|
|
62
|
+
return { status: "unreadable" };
|
|
63
|
+
}
|
|
64
|
+
ids.add(id);
|
|
65
|
+
rules.push({ id, tier, when });
|
|
66
|
+
}
|
|
67
|
+
return { status: "ok", rules };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const hasWildcard = (segment) => /[*?]/.test(segment);
|
|
71
|
+
|
|
72
|
+
/** One path segment's glob (`*` and `?` never cross a `/`). */
|
|
73
|
+
function segmentPattern(segment) {
|
|
74
|
+
const source = segment
|
|
75
|
+
.replace(/[.+^${}()|[\]\\]/g, "\\$&")
|
|
76
|
+
.replace(/\*+/g, "[^/]*")
|
|
77
|
+
.replace(/\?/g, "[^/]");
|
|
78
|
+
return new RegExp(`^${source}$`);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Whether the glob segments `g` match the path segments `p`, `**` crossing folders. */
|
|
82
|
+
function matches(g, p) {
|
|
83
|
+
if (g.length === 0) return p.length === 0;
|
|
84
|
+
if (g[0] === "**") return matches(g.slice(1), p) || (p.length > 0 && matches(g, p.slice(1)));
|
|
85
|
+
return p.length > 0 && segmentPattern(g[0]).test(p[0]) && matches(g.slice(1), p.slice(1));
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Whether the glob matches the whole path: `*` and `?` stay in one segment, `**` crosses folders. */
|
|
89
|
+
export const pathMatchesGlob = (glob, path) => matches(segments(glob), segments(path));
|
|
90
|
+
|
|
91
|
+
const segments = (glob) =>
|
|
92
|
+
String(glob)
|
|
93
|
+
.replace(/^\.\//, "")
|
|
94
|
+
.split("/")
|
|
95
|
+
.filter((s) => s !== "");
|
|
96
|
+
|
|
97
|
+
/** The leading segments of a glob that hold no wildcard. */
|
|
98
|
+
function staticPrefix(segs) {
|
|
99
|
+
const at = segs.findIndex(hasWildcard);
|
|
100
|
+
return at === -1 ? segs : segs.slice(0, at);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Whether an Allowed path and a rule's glob name the same files: the rule's
|
|
105
|
+
* glob matches the Allowed path as written, or the Allowed path ends in `**`
|
|
106
|
+
* and the rule's folder lies at or under it (`.github/**` covers
|
|
107
|
+
* `.github/agent-factory/**`). An Allowed path with no trailing `**` may be a
|
|
108
|
+
* folder, so it also counts as that folder's `**`. A rule reads the Allowed
|
|
109
|
+
* path as written: a broad `src/**` does not fire a rule for `**` + `/auth/`
|
|
110
|
+
* + `**`. The gate later measures the real diff.
|
|
111
|
+
*/
|
|
112
|
+
export function pathsOverlap(allowed, ruleGlob) {
|
|
113
|
+
const a = segments(allowed);
|
|
114
|
+
const g = segments(ruleGlob);
|
|
115
|
+
const covers = (asked) => {
|
|
116
|
+
if (asked.at(-1) !== "**") return false;
|
|
117
|
+
const base = staticPrefix(asked);
|
|
118
|
+
const rule = staticPrefix(g);
|
|
119
|
+
return base.every((seg, i) => rule[i] === seg);
|
|
120
|
+
};
|
|
121
|
+
return matches(g, a) || covers(a) || (a.at(-1) !== "**" && covers([...a, "**"]));
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* The rules that fire for a plan.
|
|
126
|
+
* @returns {Array<{ id: string, tier: string }>}
|
|
127
|
+
*/
|
|
128
|
+
export function firedRules(rules, { allowedPaths = [], size, labels = [] }) {
|
|
129
|
+
return rules.filter((rule) => {
|
|
130
|
+
const { paths, size: sizes, label } = rule.when;
|
|
131
|
+
if (paths) return allowedPaths.some((p) => paths.some((g) => pathsOverlap(p, g)));
|
|
132
|
+
if (sizes) return sizes.includes(size);
|
|
133
|
+
return labels.some((l) => label.includes(l));
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* The plan's Risk tier: the higher of the planner's and the rules'.
|
|
139
|
+
* `raisedBy` names the rules whose tier is above the planner's, or, when the
|
|
140
|
+
* file cannot give any rule, the one id that says so (and the tier is `high`).
|
|
141
|
+
* @param {{ planner: string, parsed: ReturnType<typeof parseRiskRules>, allowedPaths: string[], size: string, labels?: string[] }} input
|
|
142
|
+
* @returns {{ risk: string, raisedBy: string[] }}
|
|
143
|
+
*/
|
|
144
|
+
export function riskTier({ planner, parsed, allowedPaths, size, labels = [] }) {
|
|
145
|
+
if (parsed.status !== "ok") {
|
|
146
|
+
const id = parsed.status === "missing" ? RULES_MISSING : RULES_UNREADABLE;
|
|
147
|
+
return planner === "high" ? { risk: "high", raisedBy: [] } : { risk: "high", raisedBy: [id] };
|
|
148
|
+
}
|
|
149
|
+
const fired = firedRules(parsed.rules, { allowedPaths, size, labels });
|
|
150
|
+
const raising = fired.filter((r) => rankOf(r.tier) > rankOf(planner));
|
|
151
|
+
const top = raising.reduce((best, r) => (rankOf(r.tier) > rankOf(best) ? r.tier : best), planner);
|
|
152
|
+
return { risk: top, raisedBy: raising.map((r) => r.id) };
|
|
153
|
+
}
|
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
// The QA Stage's trusted steps, as commands of the Kit-owned bundle
|
|
2
|
+
// (`node .github/agent-factory/sweep.mjs <command>`), run by the `factory-qa`
|
|
3
|
+
// Caller (examples/factory/factory-qa.yml). No agent runs in these jobs. The
|
|
4
|
+
// QA Report they read is a pull request comment the engine's identity wrote,
|
|
5
|
+
// and the status they write is the Stage check (ADR 0035).
|
|
6
|
+
//
|
|
7
|
+
// qa-target --issue <n>
|
|
8
|
+
// finds the open pull request of the task's newest build Attempt
|
|
9
|
+
// (branch factory/<issue>/attempt-<n>, from this repository) and
|
|
10
|
+
// prints `pr_number=` and `head_sha=` to GITHUB_OUTPUT. None: exit 1.
|
|
11
|
+
// qa-start --sha <head> [--run-url <url>]
|
|
12
|
+
// writes `factory/qa` as `pending` on the head.
|
|
13
|
+
// qa-check --pr <n> --sha <head> [--run-url <url>]
|
|
14
|
+
// reads the newest QA Report comment (first line `<!-- qa-report -->`)
|
|
15
|
+
// the engine's identity wrote, and writes `factory/qa` on the head
|
|
16
|
+
// from its strict header: `success` or `failure`, or `error` when
|
|
17
|
+
// there is no valid report for this head. Matches no run link.
|
|
18
|
+
// Prints `state=`. Exit 0 whatever the state (the status is the
|
|
19
|
+
// result); 1 when the status cannot be written.
|
|
20
|
+
// qa-declare --issue <n> --pr <n> --sha <head>
|
|
21
|
+
// the QA reusable's one step (factory-qa-reusable.yml), no agent:
|
|
22
|
+
// decides from the latest valid plan comment's `qa` and the pull
|
|
23
|
+
// request's final diff under the factory config's `qa.app_paths`
|
|
24
|
+
// rule (decide.mjs), and posts the QA Report (report.mjs) as the
|
|
25
|
+
// engine: "no manual QA needed", or a held report. Edits the
|
|
26
|
+
// engine's own report for this head, never anyone else's comment.
|
|
27
|
+
// Exit 0 when a report is in place; 1 when none could be (no
|
|
28
|
+
// report, so `qa-check` writes `error`).
|
|
29
|
+
// Exit 2 is a usage error. The write token is the job's GITHUB_TOKEN, with
|
|
30
|
+
// `statuses: write`, so the creator is the Workflow engine's identity.
|
|
31
|
+
|
|
32
|
+
import { checkPlanHeader } from "../checks/plan-header.mjs";
|
|
33
|
+
import { parseQaHeader, qaStageCheck, QA_REPORT_MARKER } from "../checks/qa.mjs";
|
|
34
|
+
import { STAGE_REPO, STAGE_SHA, writeStageCheck } from "../checks/stage-check.mjs";
|
|
35
|
+
import { WORKFLOW_IDENTITY } from "../engine/workflow-engine.mjs";
|
|
36
|
+
import { latestPlan } from "../plan/comment.mjs";
|
|
37
|
+
import { newestBuildPull } from "../review/command.mjs";
|
|
38
|
+
import { SWEEP_CONFIG_PATH } from "../sweep/run.mjs";
|
|
39
|
+
import { createGithubTracker } from "../tracker/github.mjs";
|
|
40
|
+
import { decideQa, qaAppPathsFromConfig } from "./decide.mjs";
|
|
41
|
+
import { renderQaReport } from "./report.mjs";
|
|
42
|
+
|
|
43
|
+
const qaBare = (login) =>
|
|
44
|
+
String(login ?? "")
|
|
45
|
+
.replace(/\[bot\]$/, "")
|
|
46
|
+
.toLowerCase();
|
|
47
|
+
|
|
48
|
+
const qaOptionReader = (args) => (name) => {
|
|
49
|
+
const at = args.indexOf(`--${name}`);
|
|
50
|
+
return at === -1 ? undefined : args[at + 1];
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
function qaContext({ env, args, need }) {
|
|
54
|
+
const option = qaOptionReader(args);
|
|
55
|
+
const repo = env.GITHUB_REPOSITORY ?? "";
|
|
56
|
+
if (!STAGE_REPO.test(repo)) return { usage: "GITHUB_REPOSITORY is not set" };
|
|
57
|
+
for (const name of need) {
|
|
58
|
+
if (!option(name)) return { usage: `--${name} is missing` };
|
|
59
|
+
}
|
|
60
|
+
return { option, repo };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const qaRunUrl = (option, env, repo) => {
|
|
64
|
+
const given = option("run-url");
|
|
65
|
+
if (given) return given;
|
|
66
|
+
return env.GITHUB_RUN_ID
|
|
67
|
+
? `${env.GITHUB_SERVER_URL ?? "https://github.com"}/${repo}/actions/runs/${env.GITHUB_RUN_ID}`
|
|
68
|
+
: undefined;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const qaFailure = (error, title = "Stage check not written") =>
|
|
72
|
+
`::error title=${title}::${String(error.message).replace(/\s+/g, " ")}`;
|
|
73
|
+
|
|
74
|
+
export function runQaTarget({ gh, env, args, out, err, appendOutput }) {
|
|
75
|
+
const c = qaContext({ env, args, need: ["issue"] });
|
|
76
|
+
if (c.usage) {
|
|
77
|
+
err(`qa-target: ${c.usage}`);
|
|
78
|
+
return 2;
|
|
79
|
+
}
|
|
80
|
+
const issue = Number(c.option("issue"));
|
|
81
|
+
if (!Number.isInteger(issue) || issue < 1) {
|
|
82
|
+
err("qa-target: --issue is not an issue number");
|
|
83
|
+
return 2;
|
|
84
|
+
}
|
|
85
|
+
try {
|
|
86
|
+
const result = gh([
|
|
87
|
+
"api",
|
|
88
|
+
"--paginate",
|
|
89
|
+
"--slurp",
|
|
90
|
+
`repos/${c.repo}/pulls?state=open&per_page=100`,
|
|
91
|
+
]);
|
|
92
|
+
if (result.status !== 0) throw new Error(String(result.stderr).trim().slice(0, 300));
|
|
93
|
+
const pull = newestBuildPull(JSON.parse(result.stdout || "[]").flat(), { repo: c.repo, issue });
|
|
94
|
+
if (!pull || !STAGE_SHA.test(pull.head?.sha ?? "")) {
|
|
95
|
+
err(
|
|
96
|
+
"::error title=No pull request::The task has no open pull request on a build Attempt's branch to check.",
|
|
97
|
+
);
|
|
98
|
+
return 1;
|
|
99
|
+
}
|
|
100
|
+
appendOutput(`pr_number=${Number(pull.number)}`);
|
|
101
|
+
appendOutput(`head_sha=${pull.head.sha}`);
|
|
102
|
+
out(`Checking QA of pull request #${pull.number} at ${pull.head.sha.slice(0, 7)}.`);
|
|
103
|
+
return 0;
|
|
104
|
+
} catch (error) {
|
|
105
|
+
err(qaFailure(error, "Pull request not found"));
|
|
106
|
+
return 1;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function runQaStart({ gh, env, args, out, err }) {
|
|
111
|
+
const c = qaContext({ env, args, need: ["sha"] });
|
|
112
|
+
if (c.usage) {
|
|
113
|
+
err(`qa-start: ${c.usage}`);
|
|
114
|
+
return 2;
|
|
115
|
+
}
|
|
116
|
+
const sha = c.option("sha");
|
|
117
|
+
if (!STAGE_SHA.test(sha)) {
|
|
118
|
+
err("qa-start: --sha is not a commit sha");
|
|
119
|
+
return 2;
|
|
120
|
+
}
|
|
121
|
+
try {
|
|
122
|
+
writeStageCheck({
|
|
123
|
+
gh,
|
|
124
|
+
repo: c.repo,
|
|
125
|
+
sha,
|
|
126
|
+
check: qaStageCheck({ started: true }),
|
|
127
|
+
targetUrl: qaRunUrl(c.option, env, c.repo),
|
|
128
|
+
});
|
|
129
|
+
out("factory/qa: pending");
|
|
130
|
+
return 0;
|
|
131
|
+
} catch (error) {
|
|
132
|
+
err(qaFailure(error));
|
|
133
|
+
return 1;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export function runQaCheck({ gh, env, args, out, err, appendOutput, tracker }) {
|
|
138
|
+
const c = qaContext({ env, args, need: ["pr", "sha"] });
|
|
139
|
+
if (c.usage) {
|
|
140
|
+
err(`qa-check: ${c.usage}`);
|
|
141
|
+
return 2;
|
|
142
|
+
}
|
|
143
|
+
const pr = Number(c.option("pr"));
|
|
144
|
+
const sha = c.option("sha");
|
|
145
|
+
if (!Number.isInteger(pr) || pr < 1 || !STAGE_SHA.test(sha)) {
|
|
146
|
+
err("qa-check: --pr or --sha is malformed");
|
|
147
|
+
return 2;
|
|
148
|
+
}
|
|
149
|
+
const identity = env.AGENT_KIT_FACTORY_IDENTITY ?? WORKFLOW_IDENTITY;
|
|
150
|
+
try {
|
|
151
|
+
const comments = (tracker ?? createGithubTracker({ gh, repo: c.repo })).readComments(pr);
|
|
152
|
+
// The newest QA Report the engine's identity wrote: anyone can type a
|
|
153
|
+
// header, so a comment of anyone else's is never read. A report for an
|
|
154
|
+
// older head is the newest when nothing newer exists: it reads as another commit.
|
|
155
|
+
const report = comments
|
|
156
|
+
.filter(
|
|
157
|
+
(comment) =>
|
|
158
|
+
qaBare(comment.author) === qaBare(identity) &&
|
|
159
|
+
String(comment.body).split("\n", 1)[0].trim() === QA_REPORT_MARKER,
|
|
160
|
+
)
|
|
161
|
+
.at(-1);
|
|
162
|
+
const check = qaStageCheck({ report: report?.body ?? null, head: sha });
|
|
163
|
+
writeStageCheck({
|
|
164
|
+
gh,
|
|
165
|
+
repo: c.repo,
|
|
166
|
+
sha,
|
|
167
|
+
check,
|
|
168
|
+
targetUrl: qaRunUrl(c.option, env, c.repo),
|
|
169
|
+
});
|
|
170
|
+
appendOutput(`state=${check.state}`);
|
|
171
|
+
out(`factory/qa: ${check.state} (${check.description})`);
|
|
172
|
+
return 0;
|
|
173
|
+
} catch (error) {
|
|
174
|
+
err(qaFailure(error));
|
|
175
|
+
return 1;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** The `qa` sentence of the latest valid engine-authored plan comment, or null. */
|
|
180
|
+
function qaPlanSentence(comments, isEngine) {
|
|
181
|
+
const plan = latestPlan(comments, isEngine);
|
|
182
|
+
if (!plan || plan.data === undefined) return null;
|
|
183
|
+
const checked = checkPlanHeader(plan.data, { trusted: true });
|
|
184
|
+
return checked.ok ? checked.header.qa : null;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Posts `body` as the engine's QA Report for `sha`: the newest report comment
|
|
189
|
+
* the engine wrote, when it names this head, is edited in place (only if the
|
|
190
|
+
* text differs); otherwise a new comment is posted.
|
|
191
|
+
*/
|
|
192
|
+
function postQaReport({ tracker, pr, sha, body, isEngine, onlyOver }) {
|
|
193
|
+
const newest = tracker
|
|
194
|
+
.readComments(pr)
|
|
195
|
+
.filter(
|
|
196
|
+
(comment) =>
|
|
197
|
+
isEngine(comment.author) &&
|
|
198
|
+
String(comment.body).split("\n", 1)[0].trim() === QA_REPORT_MARKER,
|
|
199
|
+
)
|
|
200
|
+
.at(-1);
|
|
201
|
+
if (onlyOver) {
|
|
202
|
+
// Only replaces this head's engine report of that kind; posts nothing new.
|
|
203
|
+
const header = newest ? parseQaHeader(newest.body) : null;
|
|
204
|
+
if (header?.commit === sha && header.kind === onlyOver) tracker.editComment(newest.id, body);
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
if (newest && parseQaHeader(newest.body)?.commit === sha) {
|
|
208
|
+
if (newest.body !== body) tracker.editComment(newest.id, body);
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
tracker.postComment(pr, body);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Gathers what the decision reads, each from a trusted place: the plan comment
|
|
216
|
+
* (the engine's), the pull request's final diff, and the path rule of the
|
|
217
|
+
* default branch's config. This is the seam's input side: a later QA that runs
|
|
218
|
+
* replaces this and `decideQa`, and nothing after the verdict.
|
|
219
|
+
*/
|
|
220
|
+
function readQaInputs({ tracker, issue, pr, sha, isEngine, out }) {
|
|
221
|
+
const pull = tracker.readPull(pr);
|
|
222
|
+
// The final diff must be the head's: a head that moved since `qa-target` is no report here.
|
|
223
|
+
if (pull.head !== sha) throw new Error(`the pull request's head is no longer ${sha}`);
|
|
224
|
+
let configText = null;
|
|
225
|
+
try {
|
|
226
|
+
configText = tracker.readFile(SWEEP_CONFIG_PATH, tracker.defaultBranch());
|
|
227
|
+
} catch (error) {
|
|
228
|
+
out(
|
|
229
|
+
`notice: ${SWEEP_CONFIG_PATH} could not be read (${error.message}): every path counts as an app path`,
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
const rule = qaAppPathsFromConfig(configText);
|
|
233
|
+
if (rule.problem) out(`notice: ${rule.problem}: every path counts as an app path`);
|
|
234
|
+
return {
|
|
235
|
+
planQa: qaPlanSentence(tracker.readComments(issue), isEngine),
|
|
236
|
+
changedPaths: pull.files,
|
|
237
|
+
appPaths: rule.value,
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export function runQaDeclare({ gh, env, args, out, err, tracker }) {
|
|
242
|
+
const c = qaContext({ env, args, need: ["issue", "pr", "sha"] });
|
|
243
|
+
if (c.usage) {
|
|
244
|
+
err(`qa-declare: ${c.usage}`);
|
|
245
|
+
return 2;
|
|
246
|
+
}
|
|
247
|
+
const issue = Number(c.option("issue"));
|
|
248
|
+
const pr = Number(c.option("pr"));
|
|
249
|
+
const sha = c.option("sha");
|
|
250
|
+
if (!Number.isInteger(issue) || issue < 1 || !Number.isInteger(pr) || pr < 1) {
|
|
251
|
+
err("qa-declare: --issue or --pr is not a number");
|
|
252
|
+
return 2;
|
|
253
|
+
}
|
|
254
|
+
if (!STAGE_SHA.test(sha)) {
|
|
255
|
+
err("qa-declare: --sha is not a commit sha");
|
|
256
|
+
return 2;
|
|
257
|
+
}
|
|
258
|
+
const identity = env.AGENT_KIT_FACTORY_IDENTITY ?? WORKFLOW_IDENTITY;
|
|
259
|
+
const isEngine = (login) => qaBare(login) === qaBare(identity);
|
|
260
|
+
// Built before the try, so the catch can reach the same tracker.
|
|
261
|
+
let t;
|
|
262
|
+
try {
|
|
263
|
+
t = tracker ?? createGithubTracker({ gh, repo: c.repo });
|
|
264
|
+
const verdict = decideQa(readQaInputs({ tracker: t, issue, pr, sha, isEngine, out }));
|
|
265
|
+
postQaReport({ tracker: t, pr, sha, body: renderQaReport({ head: sha, verdict }), isEngine });
|
|
266
|
+
out(
|
|
267
|
+
verdict.kind === "none-needed"
|
|
268
|
+
? "QA Report: no manual QA needed"
|
|
269
|
+
: "QA Report: held for a person",
|
|
270
|
+
);
|
|
271
|
+
return 0;
|
|
272
|
+
} catch (error) {
|
|
273
|
+
err(qaFailure(error, "QA Report not posted"));
|
|
274
|
+
if (t) holdStaleReport({ tracker: t, pr, sha, isEngine });
|
|
275
|
+
return 1;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* A run that could not decide must not leave an earlier "no manual QA needed" for
|
|
281
|
+
* this head standing for `check` to read: it is turned into a held report, best
|
|
282
|
+
* effort (a tracker that cannot be written leaves it as it is).
|
|
283
|
+
*/
|
|
284
|
+
function holdStaleReport({ tracker, pr, sha, isEngine }) {
|
|
285
|
+
try {
|
|
286
|
+
const verdict = {
|
|
287
|
+
kind: "held",
|
|
288
|
+
text: "QA could not be declared again for this commit, so QA needs a person",
|
|
289
|
+
};
|
|
290
|
+
postQaReport({
|
|
291
|
+
tracker,
|
|
292
|
+
pr,
|
|
293
|
+
sha,
|
|
294
|
+
body: renderQaReport({ head: sha, verdict }),
|
|
295
|
+
isEngine,
|
|
296
|
+
onlyOver: "none-needed",
|
|
297
|
+
});
|
|
298
|
+
} catch {
|
|
299
|
+
// The error above already says why no report was written.
|
|
300
|
+
}
|
|
301
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// The QA Stage's decision, with no agent (ADR 0048): a pure function from what
|
|
2
|
+
// the trusted step read to a verdict, the one thing the report is written from.
|
|
3
|
+
//
|
|
4
|
+
// { kind: "none-needed" } no manual QA is needed;
|
|
5
|
+
// { kind: "held", text } a person must review: `text` is the plan's `qa`
|
|
6
|
+
// sentence, with the reason when the path rule held it.
|
|
7
|
+
//
|
|
8
|
+
// "No manual QA needed" takes two things to agree: the latest valid plan says
|
|
9
|
+
// `qa: none`, and the maintainer's path rule (`qa.app_paths` in the factory
|
|
10
|
+
// config on the default branch, a Guarded path) finds no app path in the pull
|
|
11
|
+
// request's final diff. Anything else is held, and a rule that is missing or
|
|
12
|
+
// unreadable means an app path was touched. The decision never fails QA and
|
|
13
|
+
// never passes QA it did not run: a held report is how QA asks for a person.
|
|
14
|
+
//
|
|
15
|
+
// This is the seam a later headless `qa-plan` or `manual-qa` on a QA Stack
|
|
16
|
+
// replaces: it hands the renderer (report.mjs) and the post (command.mjs) a
|
|
17
|
+
// verdict, and nothing after the verdict changes. Fixtures:
|
|
18
|
+
// scripts/fixtures/factory/qa/decide/; suite: scripts/factory-qa-declare.test.mjs.
|
|
19
|
+
|
|
20
|
+
import { pathMatchesGlob } from "../plan/risk.mjs";
|
|
21
|
+
|
|
22
|
+
/** The Place a held item is tagged with: the default Place that holds a merge (`manual-qa`'s config). */
|
|
23
|
+
export const QA_PLACE = "local";
|
|
24
|
+
|
|
25
|
+
/** The path-rule key of the factory config: a list of globs, the app paths. */
|
|
26
|
+
export const QA_RULE_KEY = "qa.app_paths";
|
|
27
|
+
|
|
28
|
+
const qaOneLine = (text) => String(text).replace(/\s+/g, " ").trim();
|
|
29
|
+
|
|
30
|
+
/** Whether a plan's `qa` sentence says no QA: `none` alone, or `none` then a non-word character. */
|
|
31
|
+
export const planSaysNoQa = (text) =>
|
|
32
|
+
typeof text === "string" && /^none(?![A-Za-z0-9])/i.test(text.trim());
|
|
33
|
+
|
|
34
|
+
const qaIsObject = (v) => v !== null && typeof v === "object" && !Array.isArray(v);
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The app-path rule of the factory config's text. `value` is the list of globs,
|
|
38
|
+
* or null when the rule is missing or wrong, which the decision reads as "an
|
|
39
|
+
* app path was touched" (fail closed). An empty list is the maintainer's word
|
|
40
|
+
* that no path is an app path.
|
|
41
|
+
* @param {string | null | undefined | symbol} text the config file's text; null when it is missing
|
|
42
|
+
* @returns {{ value: string[] | null, problem: string | null }}
|
|
43
|
+
*/
|
|
44
|
+
export function qaAppPathsFromConfig(text) {
|
|
45
|
+
const missing = (why) => ({ value: null, problem: `${QA_RULE_KEY} ${why}` });
|
|
46
|
+
if (typeof text !== "string") return missing("cannot be read: the factory config is missing");
|
|
47
|
+
let data;
|
|
48
|
+
try {
|
|
49
|
+
data = JSON.parse(text);
|
|
50
|
+
} catch {
|
|
51
|
+
return missing("cannot be read: the factory config is not JSON");
|
|
52
|
+
}
|
|
53
|
+
if (!qaIsObject(data) || !qaIsObject(data.qa) || !("app_paths" in data.qa)) {
|
|
54
|
+
return missing("is not set in the factory config");
|
|
55
|
+
}
|
|
56
|
+
const paths = data.qa.app_paths;
|
|
57
|
+
if (!Array.isArray(paths) || !paths.every((p) => typeof p === "string" && p.trim() !== "")) {
|
|
58
|
+
return missing("is not a list of path globs");
|
|
59
|
+
}
|
|
60
|
+
return { value: paths, problem: null };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* @param {{ planQa: string | null, changedPaths: string[], appPaths: string[] | null }} input
|
|
65
|
+
* `planQa`: the `qa` sentence of the latest valid engine-authored plan, null when there is none.
|
|
66
|
+
* `changedPaths`: every path of the pull request's final diff (a rename lists both names).
|
|
67
|
+
* `appPaths`: the maintainer's rule, null when the factory config has none.
|
|
68
|
+
* @returns {{ kind: "none-needed" } | { kind: "held", text: string }}
|
|
69
|
+
*/
|
|
70
|
+
export function decideQa({ planQa, changedPaths, appPaths }) {
|
|
71
|
+
const sentence = typeof planQa === "string" ? qaOneLine(planQa) : "";
|
|
72
|
+
if (!sentence)
|
|
73
|
+
return { kind: "held", text: "no valid plan comment to read, so QA needs a person" };
|
|
74
|
+
if (!planSaysNoQa(sentence)) return { kind: "held", text: sentence };
|
|
75
|
+
if (appPaths === null) {
|
|
76
|
+
return {
|
|
77
|
+
kind: "held",
|
|
78
|
+
text: `${sentence} (held: the factory config has no QA path rule, so every path counts as an app path)`,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
const touched = changedPaths.find((path) => appPaths.some((glob) => pathMatchesGlob(glob, path)));
|
|
82
|
+
if (touched !== undefined) {
|
|
83
|
+
return { kind: "held", text: `${sentence} (held: the diff touches an app path, ${touched})` };
|
|
84
|
+
}
|
|
85
|
+
return { kind: "none-needed" };
|
|
86
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// The one renderer of the QA Report the engine posts (ADR 0048): a verdict from
|
|
2
|
+
// decide.mjs to the comment text. Its header is exactly one of the two forms
|
|
3
|
+
// `checks/qa.mjs` (`parseQaHeader`) and `.claude/skills/manual-qa/report.mjs`
|
|
4
|
+
// (`parseHeader`, `parseSummary`) read, and a test holds it to both:
|
|
5
|
+
//
|
|
6
|
+
// QA Report: no manual QA needed; commit=<head>
|
|
7
|
+
// QA Report: result=passed; commit=<head>; left-for-person=1; deferred=0
|
|
8
|
+
//
|
|
9
|
+
// It writes no other result: never `failed`, and `passed` only with the one held
|
|
10
|
+
// item that asks a person, so a header never says QA ran when it did not. Pure.
|
|
11
|
+
|
|
12
|
+
import { untrustedText } from "../checks/plan-header.mjs";
|
|
13
|
+
import { QA_REPORT_MARKER } from "../checks/qa.mjs";
|
|
14
|
+
import { QA_PLACE } from "./decide.mjs";
|
|
15
|
+
|
|
16
|
+
const QA_ITEM_CAP = 700;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @param {{ head: string, verdict: { kind: "none-needed" } | { kind: "held", text: string } }} input
|
|
20
|
+
* `head` is the full commit the report is for.
|
|
21
|
+
* @returns {string} the comment body, the marker on its first line
|
|
22
|
+
*/
|
|
23
|
+
export function renderQaReport({ head, verdict }) {
|
|
24
|
+
if (verdict.kind === "none-needed") {
|
|
25
|
+
return [
|
|
26
|
+
QA_REPORT_MARKER,
|
|
27
|
+
`QA Report: no manual QA needed; commit=${head}`,
|
|
28
|
+
"",
|
|
29
|
+
"## QA Report",
|
|
30
|
+
"",
|
|
31
|
+
"No agent ran QA. The plan says no QA is needed and the factory config's path rule finds no app path in this pull request's diff, so there is nothing to run.",
|
|
32
|
+
"",
|
|
33
|
+
].join("\n");
|
|
34
|
+
}
|
|
35
|
+
// One line, no mention, no marker: the plan's words cannot add a header or a second item.
|
|
36
|
+
const text = untrustedText(String(verdict.text).replace(/\s+/g, " "), QA_ITEM_CAP);
|
|
37
|
+
return [
|
|
38
|
+
QA_REPORT_MARKER,
|
|
39
|
+
`QA Report: result=passed; commit=${head}; left-for-person=1; deferred=0`,
|
|
40
|
+
"",
|
|
41
|
+
"## QA Report",
|
|
42
|
+
"",
|
|
43
|
+
"No agent ran QA on this commit and none was run here. `passed` means no QA failed, not that QA ran: a person must review before this merges.",
|
|
44
|
+
"",
|
|
45
|
+
"### Left for a person",
|
|
46
|
+
"",
|
|
47
|
+
`- [${QA_PLACE}] Review, for a person: ${text}`,
|
|
48
|
+
"",
|
|
49
|
+
].join("\n");
|
|
50
|
+
}
|