@davidbyoung/software-factory 0.0.0-stage → 0.1.1
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 +209 -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/deps.mjs
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// The open Dependabot pull requests, oldest first, and what each needs, for
|
|
3
|
+
// the factory-deps skill's steps 0 and 1.
|
|
4
|
+
//
|
|
5
|
+
// npx --no-install factory deps
|
|
6
|
+
// npx --no-install factory deps --any # exits 0 when one is open, 1 when none, 2 when it could not tell
|
|
7
|
+
//
|
|
8
|
+
// A failed read exits 1 with the error, or 2 with `--any`.
|
|
9
|
+
import { reachOf } from "./config.mjs";
|
|
10
|
+
import { openCodeHost } from "./code-host/index.mjs";
|
|
11
|
+
const SHEPHERD_MARK = "<!-- factory-deps -->";
|
|
12
|
+
const PASSED = ["success", "skipped", "neutral"];
|
|
13
|
+
/** A major bump changes the leading number. A grouped bump is minor and
|
|
14
|
+
* patch only. */
|
|
15
|
+
export function isMajor(title) {
|
|
16
|
+
const match = /from v?(\d+)\S* to v?(\d+)/.exec(title);
|
|
17
|
+
return match !== null && match[1] !== match[2];
|
|
18
|
+
}
|
|
19
|
+
/** What the skill does with a bump, in the order its steps take them. */
|
|
20
|
+
export function nextStep(bump) {
|
|
21
|
+
if (bump.mergeableState === "unknown")
|
|
22
|
+
return "leave: state not computed yet";
|
|
23
|
+
if (bump.checks.some((c) => c.status !== "completed"))
|
|
24
|
+
return "leave: checks still running";
|
|
25
|
+
if (bump.shepherdComments >= 2 && failing(bump).length > 0)
|
|
26
|
+
return "stop: two earlier fixes did not turn CI green";
|
|
27
|
+
const steps = [];
|
|
28
|
+
if (bump.mergeableState === "dirty")
|
|
29
|
+
steps.push("merge main (step 2)");
|
|
30
|
+
if (failing(bump).length > 0)
|
|
31
|
+
steps.push(`fix ${failing(bump).join(", ")} (step 3)`);
|
|
32
|
+
if (bump.major)
|
|
33
|
+
steps.push("migrate (step 4)");
|
|
34
|
+
return steps.length > 0 ? steps.join("; ") : "nothing: waits on its merge";
|
|
35
|
+
}
|
|
36
|
+
/** Each check's newest run, in the order the checks first appear. */
|
|
37
|
+
function newest(checks) {
|
|
38
|
+
const byName = new Map();
|
|
39
|
+
for (const c of checks) {
|
|
40
|
+
const seen = byName.get(c.name);
|
|
41
|
+
if (!seen || c.id > seen.id)
|
|
42
|
+
byName.set(c.name, c);
|
|
43
|
+
}
|
|
44
|
+
return [...byName.values()];
|
|
45
|
+
}
|
|
46
|
+
function failing(bump) {
|
|
47
|
+
return newest(bump.checks)
|
|
48
|
+
.filter((c) => c.status === "completed" && !PASSED.includes(c.conclusion ?? ""))
|
|
49
|
+
.map((c) => c.name);
|
|
50
|
+
}
|
|
51
|
+
/** Every check on the bump's commit, not a fixed list of names: passed, or
|
|
52
|
+
* the names that failed and the names still running. */
|
|
53
|
+
export function checksLine(bump) {
|
|
54
|
+
const runs = newest(bump.checks);
|
|
55
|
+
if (runs.length === 0)
|
|
56
|
+
return "checks: none started";
|
|
57
|
+
const failed = failing(bump);
|
|
58
|
+
const running = runs
|
|
59
|
+
.filter((c) => c.status !== "completed")
|
|
60
|
+
.map((c) => c.name);
|
|
61
|
+
if (failed.length === 0 && running.length === 0)
|
|
62
|
+
return "checks: passed";
|
|
63
|
+
const parts = [
|
|
64
|
+
failed.length ? `failed ${failed.join(", ")}` : "",
|
|
65
|
+
running.length ? `running ${running.join(", ")}` : "",
|
|
66
|
+
].filter(Boolean);
|
|
67
|
+
return `checks: ${parts.join("; ")}`;
|
|
68
|
+
}
|
|
69
|
+
export function formatBump(bump) {
|
|
70
|
+
return [
|
|
71
|
+
`#${bump.number} ${bump.title}`,
|
|
72
|
+
` ${bump.major ? "major" : "minor or patch"}; head ${bump.head} at ${bump.sha.slice(0, 7)}; mergeable ${bump.mergeableState}; auto-merge ${bump.autoMerge ? "on" : "off"}`,
|
|
73
|
+
` ${checksLine(bump)}`,
|
|
74
|
+
` earlier shepherd comments: ${bump.shepherdComments}`,
|
|
75
|
+
` next: ${nextStep(bump)}`,
|
|
76
|
+
];
|
|
77
|
+
}
|
|
78
|
+
async function readBump(p, number) {
|
|
79
|
+
const pull = await p.get(number);
|
|
80
|
+
const [checks, comments] = await Promise.all([
|
|
81
|
+
p.checkRuns(pull.head.sha),
|
|
82
|
+
p.comments(number),
|
|
83
|
+
]);
|
|
84
|
+
return {
|
|
85
|
+
number,
|
|
86
|
+
title: pull.title,
|
|
87
|
+
head: pull.head.branch,
|
|
88
|
+
sha: pull.head.sha,
|
|
89
|
+
major: isMajor(pull.title),
|
|
90
|
+
mergeableState: pull.mergeableState,
|
|
91
|
+
autoMerge: pull.autoMerge ?? false,
|
|
92
|
+
checks,
|
|
93
|
+
shepherdComments: comments.filter((c) => c.body.includes(SHEPHERD_MARK))
|
|
94
|
+
.length,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
export async function main(p, out, argv = []) {
|
|
98
|
+
try {
|
|
99
|
+
const pulls = (await p.listOpen())
|
|
100
|
+
.filter((pull) => pull.author === "dependabot[bot]")
|
|
101
|
+
.sort((a, b) => (a.createdAt ?? "").localeCompare(b.createdAt ?? ""));
|
|
102
|
+
if (argv.includes("--any")) {
|
|
103
|
+
out(pulls.length > 0
|
|
104
|
+
? `${pulls.length} open Dependabot pull request${pulls.length === 1 ? "" : "s"}`
|
|
105
|
+
: "no open Dependabot pull requests");
|
|
106
|
+
return pulls.length > 0 ? 0 : 1;
|
|
107
|
+
}
|
|
108
|
+
if (pulls.length === 0) {
|
|
109
|
+
out("no open Dependabot pull requests");
|
|
110
|
+
return 0;
|
|
111
|
+
}
|
|
112
|
+
for (const pull of pulls) {
|
|
113
|
+
formatBump(await readBump(p, pull.number)).forEach((l) => out(l));
|
|
114
|
+
}
|
|
115
|
+
return 0;
|
|
116
|
+
}
|
|
117
|
+
catch (error) {
|
|
118
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
119
|
+
return argv.includes("--any") ? 2 : 1;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/** `factory deps`. */
|
|
123
|
+
export async function run(argv = []) {
|
|
124
|
+
const { repository, codeHost } = await reachOf(process.env);
|
|
125
|
+
return main(openCodeHost(repository, codeHost), (line) => console.log(line), argv);
|
|
126
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type CodeHost } from "./code-host/index.mts";
|
|
2
|
+
import { type Settings } from "./config.mts";
|
|
3
|
+
import { type Runner } from "./runner/index.mts";
|
|
4
|
+
import { type Tickets } from "./tickets/index.mts";
|
|
5
|
+
export type Check = {
|
|
6
|
+
state: "pass" | "fail" | "off";
|
|
7
|
+
text: string;
|
|
8
|
+
/** What to do about a failure. */
|
|
9
|
+
fix?: string;
|
|
10
|
+
};
|
|
11
|
+
export type DoctorDeps = {
|
|
12
|
+
settings: () => Promise<Settings>;
|
|
13
|
+
/** The paths a sync would change. */
|
|
14
|
+
outOfStep: (s: Settings) => string[];
|
|
15
|
+
/** The version installed, and the one the repository's lock records. */
|
|
16
|
+
versions: () => {
|
|
17
|
+
installed: string;
|
|
18
|
+
locked: string | null;
|
|
19
|
+
};
|
|
20
|
+
open: (s: Settings) => {
|
|
21
|
+
codeHost: CodeHost;
|
|
22
|
+
runner: Runner;
|
|
23
|
+
tickets: Tickets | Promise<Tickets>;
|
|
24
|
+
};
|
|
25
|
+
out: (line: string) => void;
|
|
26
|
+
};
|
|
27
|
+
export declare function checks(d: DoctorDeps): Promise<Check[]>;
|
|
28
|
+
/** One check as a line: a tick, a cross with what to do, or a dash for a
|
|
29
|
+
* part that is off. */
|
|
30
|
+
export declare function line(c: Check): string;
|
|
31
|
+
export declare function main(_argv: string[], d: DoctorDeps): Promise<number>;
|
|
32
|
+
/** The checks, run in the repository this command runs in. */
|
|
33
|
+
export declare function doctorDeps(root?: string): DoctorDeps;
|
|
34
|
+
export declare function run(argv: string[]): Promise<number>;
|
package/dist/doctor.mjs
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
// `factory doctor`: whether the repository's setup works, one line per
|
|
2
|
+
// check, each failure with what to do about it. `factory init` ends with the
|
|
3
|
+
// same checks.
|
|
4
|
+
//
|
|
5
|
+
// A stored secret cannot be read back, so a secret is checked only for
|
|
6
|
+
// being set.
|
|
7
|
+
import { readFileSync } from "node:fs";
|
|
8
|
+
import { join } from "node:path";
|
|
9
|
+
import { boardConfigOf, inspectBoard } from "./board.mjs";
|
|
10
|
+
import { openCodeHost } from "./code-host/index.mjs";
|
|
11
|
+
import { defaultDeps, loadSettings, repoRoot, } from "./config.mjs";
|
|
12
|
+
import { neededSecrets, PART_TEXT, PARTS, partsOn } from "./parts.mjs";
|
|
13
|
+
import { openRunner } from "./runner/index.mjs";
|
|
14
|
+
import { differences, LOCK, PACKAGE, plan } from "./sync.mjs";
|
|
15
|
+
import { openTickets } from "./tickets/index.mjs";
|
|
16
|
+
const SYNC = "Run npx --no-install factory sync";
|
|
17
|
+
const INIT = "Run npx --no-install factory init";
|
|
18
|
+
const message = (e) => (e instanceof Error ? e.message : String(e));
|
|
19
|
+
/** -1, 0, or 1 as version `a` is older than, the same as, or newer than `b`. */
|
|
20
|
+
function compareVersions(a, b) {
|
|
21
|
+
const pa = a.split(/[.-]/).map(Number);
|
|
22
|
+
const pb = b.split(/[.-]/).map(Number);
|
|
23
|
+
for (let i = 0; i < Math.max(pa.length, pb.length); i++) {
|
|
24
|
+
const x = pa[i] ?? 0;
|
|
25
|
+
const y = pb[i] ?? 0;
|
|
26
|
+
if (x !== y)
|
|
27
|
+
return x < y ? -1 : 1;
|
|
28
|
+
}
|
|
29
|
+
return 0;
|
|
30
|
+
}
|
|
31
|
+
export async function checks(d) {
|
|
32
|
+
const out = [];
|
|
33
|
+
let s;
|
|
34
|
+
try {
|
|
35
|
+
s = await d.settings();
|
|
36
|
+
}
|
|
37
|
+
catch (e) {
|
|
38
|
+
return [{ state: "fail", text: "The settings are valid", fix: message(e) }];
|
|
39
|
+
}
|
|
40
|
+
out.push({ state: "pass", text: "The settings are valid" });
|
|
41
|
+
const differ = d.outOfStep(s);
|
|
42
|
+
out.push(differ.length === 0
|
|
43
|
+
? { state: "pass", text: "The factory's files match the settings" }
|
|
44
|
+
: {
|
|
45
|
+
state: "fail",
|
|
46
|
+
text: "The factory's files match the settings",
|
|
47
|
+
fix: `${differ.length} differ. ${SYNC}`,
|
|
48
|
+
});
|
|
49
|
+
const { installed, locked } = d.versions();
|
|
50
|
+
if (locked === null)
|
|
51
|
+
out.push({
|
|
52
|
+
state: "fail",
|
|
53
|
+
text: `Version ${installed} is installed, but the repository records none`,
|
|
54
|
+
fix: SYNC,
|
|
55
|
+
});
|
|
56
|
+
else if (compareVersions(installed, locked) === 0)
|
|
57
|
+
out.push({
|
|
58
|
+
state: "pass",
|
|
59
|
+
text: `Version ${installed} is installed, as the repository records`,
|
|
60
|
+
});
|
|
61
|
+
else
|
|
62
|
+
out.push({
|
|
63
|
+
state: "fail",
|
|
64
|
+
text: `Version ${installed} is installed, but the repository records ${locked}`,
|
|
65
|
+
fix: compareVersions(installed, locked) < 0 ? "Run npm ci" : SYNC,
|
|
66
|
+
});
|
|
67
|
+
const { codeHost, runner, tickets } = d.open(s);
|
|
68
|
+
try {
|
|
69
|
+
out.push({
|
|
70
|
+
state: "pass",
|
|
71
|
+
text: `Signed in to the code host as ${await codeHost.signedInAs()}`,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
catch (e) {
|
|
75
|
+
out.push({
|
|
76
|
+
state: "fail",
|
|
77
|
+
text: "Signed in to the code host",
|
|
78
|
+
fix: message(e),
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
let secrets;
|
|
82
|
+
try {
|
|
83
|
+
secrets = await runner.secretNames();
|
|
84
|
+
}
|
|
85
|
+
catch (e) {
|
|
86
|
+
secrets = null;
|
|
87
|
+
out.push({
|
|
88
|
+
state: "fail",
|
|
89
|
+
text: "The secrets can be listed",
|
|
90
|
+
fix: message(e),
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
const on = partsOn(s, secrets ?? []);
|
|
94
|
+
const needed = neededSecrets(on);
|
|
95
|
+
for (const part of PARTS) {
|
|
96
|
+
if (!on.has(part)) {
|
|
97
|
+
out.push({ state: "off", text: `${PART_TEXT[part].name}: off` });
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
if (secrets === null)
|
|
101
|
+
continue;
|
|
102
|
+
const forPart = PART_TEXT[part].name.replace(/^./, (c) => c.toLowerCase());
|
|
103
|
+
for (const need of needed.filter((n) => n.part === part)) {
|
|
104
|
+
const found = need.anyOf.find((n) => secrets.includes(n));
|
|
105
|
+
out.push(found
|
|
106
|
+
? { state: "pass", text: `${found} is set, for ${forPart}` }
|
|
107
|
+
: {
|
|
108
|
+
state: "fail",
|
|
109
|
+
text: `${need.anyOf.join(" or ")} is not set, for ${forPart}`,
|
|
110
|
+
fix: INIT,
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (s.tickets.board !== null) {
|
|
115
|
+
const number = s.tickets.board;
|
|
116
|
+
try {
|
|
117
|
+
const board = await (await tickets).board.read();
|
|
118
|
+
const { problems } = inspectBoard(board.fields, boardConfigOf(s));
|
|
119
|
+
out.push(problems.length === 0
|
|
120
|
+
? {
|
|
121
|
+
state: "pass",
|
|
122
|
+
text: `Board ${number} has every column the settings name`,
|
|
123
|
+
}
|
|
124
|
+
: {
|
|
125
|
+
state: "fail",
|
|
126
|
+
text: `Board ${number}: ${problems.join("; ")}`,
|
|
127
|
+
fix: INIT,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
catch (e) {
|
|
131
|
+
out.push({
|
|
132
|
+
state: "fail",
|
|
133
|
+
text: `Board ${number} can be read`,
|
|
134
|
+
fix: message(e),
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return out;
|
|
139
|
+
}
|
|
140
|
+
/** One check as a line: a tick, a cross with what to do, or a dash for a
|
|
141
|
+
* part that is off. */
|
|
142
|
+
export function line(c) {
|
|
143
|
+
if (c.state === "off")
|
|
144
|
+
return `- ${c.text}`;
|
|
145
|
+
if (c.state === "pass")
|
|
146
|
+
return `✓ ${c.text}`;
|
|
147
|
+
const fix = c.fix ?? "";
|
|
148
|
+
// An instruction follows as its own sentence; a reason, after a colon.
|
|
149
|
+
return `✗ ${c.text}${!fix ? "" : /^Run /.test(fix) ? `. ${fix}` : `: ${fix}`}`;
|
|
150
|
+
}
|
|
151
|
+
export async function main(_argv, d) {
|
|
152
|
+
const found = await checks(d);
|
|
153
|
+
for (const c of found)
|
|
154
|
+
d.out(line(c));
|
|
155
|
+
return found.some((c) => c.state === "fail") ? 1 : 0;
|
|
156
|
+
}
|
|
157
|
+
/** The checks, run in the repository this command runs in. */
|
|
158
|
+
export function doctorDeps(root = repoRoot()) {
|
|
159
|
+
const settings = () => loadSettings(defaultDeps(root));
|
|
160
|
+
return {
|
|
161
|
+
settings,
|
|
162
|
+
outOfStep: (s) => differences(root, plan(PACKAGE, root, s)),
|
|
163
|
+
versions: () => {
|
|
164
|
+
const installed = JSON.parse(readFileSync(join(PACKAGE, "package.json"), "utf8")).version;
|
|
165
|
+
let locked = null;
|
|
166
|
+
try {
|
|
167
|
+
locked =
|
|
168
|
+
/^version: (.+)$/m.exec(readFileSync(join(root, LOCK), "utf8"))?.[1] ?? null;
|
|
169
|
+
}
|
|
170
|
+
catch {
|
|
171
|
+
locked = null;
|
|
172
|
+
}
|
|
173
|
+
return { installed, locked };
|
|
174
|
+
},
|
|
175
|
+
open: (s) => ({
|
|
176
|
+
codeHost: openCodeHost(s.repository, s.codeHost.type),
|
|
177
|
+
runner: openRunner(s.repository, s.builds.runner),
|
|
178
|
+
tickets: openTickets(s, {
|
|
179
|
+
repository: s.repository,
|
|
180
|
+
board: s.tickets.board,
|
|
181
|
+
}).then((o) => o.tickets),
|
|
182
|
+
}),
|
|
183
|
+
out: (l) => console.log(l),
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
export async function run(argv) {
|
|
187
|
+
return main(argv, doctorDeps());
|
|
188
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export type HookInput = {
|
|
2
|
+
cwd?: string;
|
|
3
|
+
tool_input?: {
|
|
4
|
+
command?: string;
|
|
5
|
+
};
|
|
6
|
+
};
|
|
7
|
+
/** Heredoc bodies and quoted strings blanked, so a commit message, grep
|
|
8
|
+
* pattern, or file body that mentions "git push -f" or "main" is not read as
|
|
9
|
+
* part of a command. A quoted single word is unquoted instead: the shell
|
|
10
|
+
* passes "main" to git as main. */
|
|
11
|
+
export declare function blankQuoted(cmd: string): string;
|
|
12
|
+
/** "verdict: override" is the owner's approval of one commit, and agents act
|
|
13
|
+
* through the owner's account, so GitHub cannot tell their override from the
|
|
14
|
+
* owner's. This is a tripwire, not a lock: it stops the ordinary gh ways of
|
|
15
|
+
* adding it, not a script written to get around it. It reads each command of
|
|
16
|
+
* the chain as typed, since the spaced name is always quoted, and blocks one
|
|
17
|
+
* that adds the override as --add-label's value or a labels[]= field, or that
|
|
18
|
+
* writes to the labels API with the override anywhere in the command, as a
|
|
19
|
+
* body sent with --input does. Removing the override, alone or beside adding
|
|
20
|
+
* another label, is allowed. */
|
|
21
|
+
export declare function addsOverride(raw: string): boolean;
|
|
22
|
+
export type GitCall = {
|
|
23
|
+
kind: "push" | "commit";
|
|
24
|
+
segment: string;
|
|
25
|
+
dir: string;
|
|
26
|
+
};
|
|
27
|
+
/** Each git push and commit in `cmd`, already blanked, with the directory it
|
|
28
|
+
* runs in. A `cd` moves the directory the commands after it run in, and
|
|
29
|
+
* `git -C DIR` moves its own, so the branch is read where the commit or push
|
|
30
|
+
* runs, not where the session sits. Git's global options, such as
|
|
31
|
+
* `-c key=value`, come before the subcommand and are skipped. A subshell's
|
|
32
|
+
* `cd` ends at its `)`. */
|
|
33
|
+
export declare function gitCalls(cmd: string, cwd: string): GitCall[];
|
|
34
|
+
/** The launcher of the factory in the checkout `top`, where this copy sits
|
|
35
|
+
* in its own checkout, or null when there is none or it is this copy. A
|
|
36
|
+
* session's hook runs the copy in the folder the session opened, whichever
|
|
37
|
+
* worktree the commit is in, and each copy reads only the settings it knows. */
|
|
38
|
+
export declare function ownLauncher(top: string): string | null;
|
|
39
|
+
/** The block message for this Bash call, or null to let it through. Writes
|
|
40
|
+
* the failing gate command's output to `err` first. */
|
|
41
|
+
export declare function checkCommit(input: HookInput, err: (text: string) => void): Promise<string | null>;
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
// PreToolUse hook for Bash, run as `factory guard commit` or within
|
|
2
|
+
// `factory guard`. Blocks commits and pushes on main, force pushes, adding
|
|
3
|
+
// the owner's override label, and any commit the fast half of the gate would
|
|
4
|
+
// fail: the before-commit commands in `.claude/factory.yaml`. A block is
|
|
5
|
+
// exit 2, which stops the call and shows the message to Claude.
|
|
6
|
+
import { execFileSync, spawnSync } from "node:child_process";
|
|
7
|
+
import { existsSync, realpathSync } from "node:fs";
|
|
8
|
+
import { homedir } from "node:os";
|
|
9
|
+
import { dirname, isAbsolute, join, relative } from "node:path";
|
|
10
|
+
import { fileURLToPath } from "node:url";
|
|
11
|
+
import { defaultDeps, loadSettings, settingsPath } from "./config.mjs";
|
|
12
|
+
/** Heredoc bodies and quoted strings blanked, so a commit message, grep
|
|
13
|
+
* pattern, or file body that mentions "git push -f" or "main" is not read as
|
|
14
|
+
* part of a command. A quoted single word is unquoted instead: the shell
|
|
15
|
+
* passes "main" to git as main. */
|
|
16
|
+
export function blankQuoted(cmd) {
|
|
17
|
+
return `${cmd}\n`
|
|
18
|
+
.replace(/<<-?[ \t]*(['"]?)(\w+)\1[\s\S]*?\n[ \t]*\2(?=\n|$)/g, "")
|
|
19
|
+
.replace(/(["'])([^"'\s;&|<>()$`\\]+)\1/g, "$2")
|
|
20
|
+
.replace(/"(?:[^"\\]|\\[\s\S])*"/g, '""')
|
|
21
|
+
.replace(/'[^']*'/g, "''");
|
|
22
|
+
}
|
|
23
|
+
const OVERRIDE = String.raw `verdict(:|%3A)( |%20|\+)override`;
|
|
24
|
+
/** "verdict: override" is the owner's approval of one commit, and agents act
|
|
25
|
+
* through the owner's account, so GitHub cannot tell their override from the
|
|
26
|
+
* owner's. This is a tripwire, not a lock: it stops the ordinary gh ways of
|
|
27
|
+
* adding it, not a script written to get around it. It reads each command of
|
|
28
|
+
* the chain as typed, since the spaced name is always quoted, and blocks one
|
|
29
|
+
* that adds the override as --add-label's value or a labels[]= field, or that
|
|
30
|
+
* writes to the labels API with the override anywhere in the command, as a
|
|
31
|
+
* body sent with --input does. Removing the override, alone or beside adding
|
|
32
|
+
* another label, is allowed. */
|
|
33
|
+
export function addsOverride(raw) {
|
|
34
|
+
const segments = raw
|
|
35
|
+
.split(/\|\||&&|[;&|()]|\n/)
|
|
36
|
+
.filter((s) => /^\s*gh\s/.test(s));
|
|
37
|
+
const adds = new RegExp(String.raw `--add-label(=|\s+)["']?[^"']*${OVERRIDE}|labels\[\]=[^"']*${OVERRIDE}`, "i");
|
|
38
|
+
if (segments.some((s) => adds.test(s)))
|
|
39
|
+
return true;
|
|
40
|
+
return (new RegExp(OVERRIDE, "i").test(raw) &&
|
|
41
|
+
segments.some((s) => /\/labels(\s|$)/.test(s) &&
|
|
42
|
+
/--input|-X\s*(POST|PUT)|--method\s+(POST|PUT)/.test(s)));
|
|
43
|
+
}
|
|
44
|
+
function resolvePath(dir, to) {
|
|
45
|
+
if (isAbsolute(to))
|
|
46
|
+
return to;
|
|
47
|
+
if (to === "~")
|
|
48
|
+
return homedir();
|
|
49
|
+
if (to.startsWith("~/"))
|
|
50
|
+
return join(homedir(), to.slice(2));
|
|
51
|
+
return `${dir}/${to}`;
|
|
52
|
+
}
|
|
53
|
+
const isWorkTree = (dir) => spawnSync("git", ["-C", dir, "rev-parse", "--is-inside-work-tree"], {
|
|
54
|
+
stdio: "ignore",
|
|
55
|
+
}).status === 0;
|
|
56
|
+
/** Each git push and commit in `cmd`, already blanked, with the directory it
|
|
57
|
+
* runs in. A `cd` moves the directory the commands after it run in, and
|
|
58
|
+
* `git -C DIR` moves its own, so the branch is read where the commit or push
|
|
59
|
+
* runs, not where the session sits. Git's global options, such as
|
|
60
|
+
* `-c key=value`, come before the subcommand and are skipped. A subshell's
|
|
61
|
+
* `cd` ends at its `)`. */
|
|
62
|
+
export function gitCalls(cmd, cwd) {
|
|
63
|
+
const calls = [];
|
|
64
|
+
const stack = [];
|
|
65
|
+
let dir = cwd;
|
|
66
|
+
const lines = cmd
|
|
67
|
+
.replace(/\|\||&&|[;&|]/g, "\n")
|
|
68
|
+
.replace(/([()])/g, "\n$1\n")
|
|
69
|
+
.split("\n");
|
|
70
|
+
for (const segment of lines) {
|
|
71
|
+
const w = segment.split(/\s+/).filter(Boolean);
|
|
72
|
+
if (w.length === 0)
|
|
73
|
+
continue;
|
|
74
|
+
if (w[0] === "(") {
|
|
75
|
+
stack.push(dir);
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
if (w[0] === ")") {
|
|
79
|
+
if (stack.length)
|
|
80
|
+
dir = stack.pop();
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
if (w[0] === "cd") {
|
|
84
|
+
dir = resolvePath(dir, w[1] ?? homedir());
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (w[0] !== "git")
|
|
88
|
+
continue;
|
|
89
|
+
let gdir = dir;
|
|
90
|
+
let i = 1;
|
|
91
|
+
while (i < w.length) {
|
|
92
|
+
if (w[i] === "-C") {
|
|
93
|
+
gdir = resolvePath(gdir, w[i + 1] ?? ".");
|
|
94
|
+
i += 2;
|
|
95
|
+
}
|
|
96
|
+
else if (["-c", "--git-dir", "--work-tree", "--namespace"].includes(w[i])) {
|
|
97
|
+
i += 2;
|
|
98
|
+
}
|
|
99
|
+
else if (w[i].startsWith("-")) {
|
|
100
|
+
i += 1;
|
|
101
|
+
}
|
|
102
|
+
else
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
// A directory the hook cannot follow, such as `cd "$D"` with its quoted
|
|
106
|
+
// text blanked, or `cd -`, falls back to the session's rather than
|
|
107
|
+
// letting the call through.
|
|
108
|
+
if (!isWorkTree(gdir))
|
|
109
|
+
gdir = cwd;
|
|
110
|
+
if (w[i] === "push" || w[i] === "commit")
|
|
111
|
+
calls.push({ kind: w[i], segment, dir: gdir });
|
|
112
|
+
}
|
|
113
|
+
return calls;
|
|
114
|
+
}
|
|
115
|
+
const lastLines = (text) => text.trimEnd().split("\n").slice(-40);
|
|
116
|
+
const toplevel = (dir) => execFileSync("git", ["rev-parse", "--show-toplevel"], {
|
|
117
|
+
cwd: dir,
|
|
118
|
+
encoding: "utf8",
|
|
119
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
120
|
+
}).trim();
|
|
121
|
+
/** The launcher of the factory in the checkout `top`, where this copy sits
|
|
122
|
+
* in its own checkout, or null when there is none or it is this copy. A
|
|
123
|
+
* session's hook runs the copy in the folder the session opened, whichever
|
|
124
|
+
* worktree the commit is in, and each copy reads only the settings it knows. */
|
|
125
|
+
export function ownLauncher(top) {
|
|
126
|
+
try {
|
|
127
|
+
const pkg = realpathSync(join(dirname(fileURLToPath(import.meta.url)), ".."));
|
|
128
|
+
const own = join(top, relative(toplevel(pkg), pkg), "bin", "factory.js");
|
|
129
|
+
return existsSync(own) && realpathSync(dirname(dirname(own))) !== pkg
|
|
130
|
+
? own
|
|
131
|
+
: null;
|
|
132
|
+
}
|
|
133
|
+
catch {
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
/** The block message for this Bash call, or null to let it through. Writes
|
|
138
|
+
* the failing gate command's output to `err` first. */
|
|
139
|
+
export async function checkCommit(input, err) {
|
|
140
|
+
const raw = input.tool_input?.command ?? "";
|
|
141
|
+
const cwd = input.cwd ?? ".";
|
|
142
|
+
if (addsOverride(raw))
|
|
143
|
+
return '"verdict: override" is the owner\'s label. Ask them to add it.';
|
|
144
|
+
const calls = gitCalls(blankQuoted(raw), cwd);
|
|
145
|
+
if (calls.length === 0)
|
|
146
|
+
return null;
|
|
147
|
+
for (const { dir } of calls) {
|
|
148
|
+
const branch = spawnSync("git", ["-C", dir, "branch", "--show-current"], {
|
|
149
|
+
encoding: "utf8",
|
|
150
|
+
}).stdout;
|
|
151
|
+
if (branch?.trim() === "main")
|
|
152
|
+
return "main is checked out. Create a branch from origin/main first.";
|
|
153
|
+
}
|
|
154
|
+
const pushes = calls.filter((c) => c.kind === "push").map((c) => c.segment);
|
|
155
|
+
if (pushes.some((p) => /\s(--force|-f)(\s|$)|\s\+\S/.test(p)))
|
|
156
|
+
return "force push. Use --force-with-lease if a rewrite is really intended.";
|
|
157
|
+
if (pushes.some((p) => /\s(\S*:)?(refs\/heads\/)?main(\s|$)/.test(p)))
|
|
158
|
+
return "pushing to main. main only changes through a merged pull request.";
|
|
159
|
+
// The fast half of the gate: the before-commit commands in the commit's
|
|
160
|
+
// own checkout's settings, in order, stopping at the first that fails.
|
|
161
|
+
// Settings that cannot be read block the commit, as a failing command
|
|
162
|
+
// would.
|
|
163
|
+
const commit = calls.find((c) => c.kind === "commit");
|
|
164
|
+
if (!commit)
|
|
165
|
+
return null;
|
|
166
|
+
let top = commit.dir;
|
|
167
|
+
try {
|
|
168
|
+
top = toplevel(commit.dir);
|
|
169
|
+
}
|
|
170
|
+
catch {
|
|
171
|
+
// Not a checkout: read the settings where the commit runs.
|
|
172
|
+
}
|
|
173
|
+
const own = ownLauncher(top);
|
|
174
|
+
if (own) {
|
|
175
|
+
const r = spawnSync("node", [own, "guard", "commit"], {
|
|
176
|
+
cwd: top,
|
|
177
|
+
input: JSON.stringify(input),
|
|
178
|
+
encoding: "utf8",
|
|
179
|
+
});
|
|
180
|
+
if (r.status === 0)
|
|
181
|
+
return null;
|
|
182
|
+
err(lastLines(`${r.stdout ?? ""}${r.stderr ?? ""}`).join("\n"));
|
|
183
|
+
return `the factory in ${top} blocked it, as above.`;
|
|
184
|
+
}
|
|
185
|
+
// A repository with no settings is not one the factory governs, such as
|
|
186
|
+
// another repository a session works in: it has no gate to run.
|
|
187
|
+
if (!existsSync(settingsPath(top)))
|
|
188
|
+
return null;
|
|
189
|
+
let commands;
|
|
190
|
+
try {
|
|
191
|
+
commands = (await loadSettings(defaultDeps(top))).beforeCommit;
|
|
192
|
+
}
|
|
193
|
+
catch (e) {
|
|
194
|
+
err(lastLines(e instanceof Error ? e.message : String(e)).join("\n"));
|
|
195
|
+
return "the gate failed: the factory's settings could not be read. Fix the error above, then commit.";
|
|
196
|
+
}
|
|
197
|
+
for (const command of commands) {
|
|
198
|
+
const r = spawnSync("bash", ["-c", `exec 2>&1\n${command}`], {
|
|
199
|
+
cwd: top,
|
|
200
|
+
encoding: "utf8",
|
|
201
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
202
|
+
});
|
|
203
|
+
if (r.status !== 0) {
|
|
204
|
+
err(lastLines(r.stdout ?? "").join("\n"));
|
|
205
|
+
return `the gate failed on: ${command}. Fix the errors above, then commit.`;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return null;
|
|
209
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type PermissionInput = {
|
|
2
|
+
cwd?: string;
|
|
3
|
+
tool_name?: string;
|
|
4
|
+
tool_input?: {
|
|
5
|
+
file_path?: string;
|
|
6
|
+
};
|
|
7
|
+
};
|
|
8
|
+
/** Whether a build may write `file`: a path inside `.claude/` of the project
|
|
9
|
+
* but `.claude/settings.json` and `.claude/settings.local.json`. The path is
|
|
10
|
+
* resolved first, so one that climbs out with `..` is judged where it lands. */
|
|
11
|
+
export declare function buildMayWrite(file: string, projectDir: string): boolean;
|
|
12
|
+
/** The hook's answer: the JSON that approves the call, or null for no
|
|
13
|
+
* answer, which leaves the decision to whoever it was left to before. */
|
|
14
|
+
export declare function permission(input: PermissionInput, env: Record<string, string | undefined>): string | null;
|
|
15
|
+
export declare function run(): Promise<number>;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// The PermissionRequest hook, run as `factory guard permission`. Claude Code
|
|
2
|
+
// protects `.claude/`, and an unattended `claude -p` run denies any write
|
|
3
|
+
// there that nothing approves. In a run the factory's action started
|
|
4
|
+
// (`FACTORY_UNATTENDED=1`), this approves a write to the repository's own
|
|
5
|
+
// files under `.claude/`, and says nothing otherwise, so a person at a
|
|
6
|
+
// keyboard is still asked, and so is any other workflow that runs Claude.
|
|
7
|
+
import { readFileSync } from "node:fs";
|
|
8
|
+
import { relative, resolve, sep } from "node:path";
|
|
9
|
+
const WRITES = new Set(["Write", "Edit", "MultiEdit"]);
|
|
10
|
+
// The hooks that guard the run itself.
|
|
11
|
+
const NEVER = new Set(["settings.json", "settings.local.json"]);
|
|
12
|
+
/** Whether a build may write `file`: a path inside `.claude/` of the project
|
|
13
|
+
* but `.claude/settings.json` and `.claude/settings.local.json`. The path is
|
|
14
|
+
* resolved first, so one that climbs out with `..` is judged where it lands. */
|
|
15
|
+
export function buildMayWrite(file, projectDir) {
|
|
16
|
+
const claude = resolve(projectDir, ".claude");
|
|
17
|
+
const inside = relative(claude, resolve(projectDir, file));
|
|
18
|
+
if (inside === "" || inside === ".." || inside.startsWith(`..${sep}`))
|
|
19
|
+
return false;
|
|
20
|
+
return !NEVER.has(inside);
|
|
21
|
+
}
|
|
22
|
+
/** The hook's answer: the JSON that approves the call, or null for no
|
|
23
|
+
* answer, which leaves the decision to whoever it was left to before. */
|
|
24
|
+
export function permission(input, env) {
|
|
25
|
+
if (env.FACTORY_UNATTENDED !== "1")
|
|
26
|
+
return null;
|
|
27
|
+
const file = input.tool_input?.file_path;
|
|
28
|
+
if (!input.tool_name || !WRITES.has(input.tool_name) || !file)
|
|
29
|
+
return null;
|
|
30
|
+
const projectDir = env.CLAUDE_PROJECT_DIR || input.cwd;
|
|
31
|
+
if (!projectDir || !buildMayWrite(file, projectDir))
|
|
32
|
+
return null;
|
|
33
|
+
return JSON.stringify({
|
|
34
|
+
hookSpecificOutput: {
|
|
35
|
+
hookEventName: "PermissionRequest",
|
|
36
|
+
decision: { behavior: "allow" },
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
export async function run() {
|
|
41
|
+
let input = {};
|
|
42
|
+
try {
|
|
43
|
+
input = JSON.parse(readFileSync(0, "utf8") || "{}");
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return 0;
|
|
47
|
+
}
|
|
48
|
+
const answer = permission(input, process.env);
|
|
49
|
+
if (answer)
|
|
50
|
+
process.stdout.write(`${answer}\n`);
|
|
51
|
+
return 0;
|
|
52
|
+
}
|