@mnaimfaizy/agent-kit 1.0.0-beta.14 → 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 +22 -4
- package/custom-harness.schema.json +243 -0
- package/harnesses.json +244 -6
- package/package.json +3 -2
- package/src/checks/ci-workflow.mjs +105 -0
- package/src/checks/claude-app.mjs +11 -2
- package/src/checks/custom-harness.mjs +157 -0
- package/src/checks/github-app.mjs +214 -0
- package/src/checks/rulesets.mjs +9 -8
- package/src/checks/secrets-and-variables.mjs +27 -8
- package/src/checks/stages.mjs +705 -0
- package/src/cli.mjs +583 -36
- package/src/deep-scan/agent.mjs +88 -21
- package/src/deep-scan/credential.mjs +81 -32
- package/src/deep-scan/docker-driver.mjs +105 -42
- package/src/deep-scan/harness-text.mjs +99 -0
- package/src/deep-scan/lifecycle.mjs +68 -178
- package/src/deep-scan/limits.mjs +13 -7
- package/src/deep-scan/model-proxy.mjs +21 -10
- package/src/deep-scan/preflight.mjs +19 -6
- package/src/deep-scan/recipe.mjs +4 -0
- package/src/deep-scan/runtime.mjs +54 -6
- package/src/deep-scan/sandbox-run.mjs +232 -0
- package/src/deep-scan/sbx-driver.mjs +224 -39
- package/src/deep-scan/tar.mjs +69 -22
- package/src/deep-scan/verify.mjs +6 -1
- package/src/deep-scan.mjs +73 -13
- package/src/doctor.mjs +166 -5
- 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/harness/adapters.mjs +30 -0
- package/src/harness/antigravity.mjs +187 -0
- package/src/harness/claude-code.mjs +8 -1
- package/src/harness/codex.mjs +219 -0
- package/src/harness/cursor.mjs +199 -0
- package/src/harness/custom/registry.mjs +21 -0
- package/src/harness/custom/schema.mjs +90 -0
- package/src/harness/custom.mjs +299 -0
- package/src/harness/facts.mjs +44 -7
- package/src/harness/gemini.mjs +164 -0
- package/src/harness/install-layer.mjs +86 -3
- package/src/harness/opencode.mjs +289 -0
- package/src/harness/resolve.mjs +156 -0
- package/src/harness/support-levels.mjs +151 -3
- package/src/harness-live/assess.mjs +29 -6
- package/src/harness-live/cursor-real.mjs +146 -0
- package/src/harness-live/cursor-version.mjs +70 -0
- package/src/harness-live/cursor.mjs +80 -0
- package/src/harness-live/custom.mjs +91 -0
- package/src/harness-live/fixture-harness.mjs +45 -0
- package/src/harness-live/harnesses.mjs +263 -0
- package/src/harness-live/host.mjs +113 -4
- package/src/harness-live/opencode.mjs +289 -0
- package/src/harness-live/probes.mjs +87 -7
- package/src/harness-live/run.mjs +101 -20
- package/src/harness-live/setup.mjs +126 -0
- package/src/harness-live/stage.mjs +25 -15
- package/src/harness-live/suite.mjs +276 -95
- package/src/harness-test.mjs +116 -0
- package/src/init.mjs +374 -36
- package/src/kit-owned-paths.mjs +1 -1
- package/src/model-proxy/core.mjs +128 -20
- package/src/model-proxy/profiles/anthropic.mjs +54 -10
- package/src/model-proxy/profiles/gemini-api.mjs +311 -0
- package/src/model-proxy/profiles/openai.mjs +327 -0
- package/src/model-proxy/stub-dialects/gemini-api.mjs +178 -0
- package/src/model-proxy/stub-dialects/openai.mjs +173 -0
- package/src/model-proxy/stub.mjs +9 -1
- package/src/stage/audit.mjs +79 -0
- package/src/stage/egress-route.mjs +207 -0
- package/src/stage/implement/carry.mjs +308 -0
- package/src/stage/implement/sandbox.mjs +271 -0
- package/src/stage/implement/verify.mjs +70 -0
- package/src/stage/implement.mjs +218 -0
- package/src/stage/plan.mjs +115 -0
- package/src/stage/review-response.mjs +172 -0
- package/src/stage/review.mjs +185 -0
- package/src/stage/workspace.mjs +274 -0
- package/src/stage.mjs +1087 -0
- package/src/upgrade.mjs +39 -7
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// The merge gate's trip (spec #500, Merge gate specifics; ADR 0041). A revert of a
|
|
2
|
+
// pull request the gate merged, or a `regression` issue that links one, within 7
|
|
3
|
+
// days of the merge, makes the gate act as `shadow`. The trip is remembered as an
|
|
4
|
+
// engine comment on the Factory control issue, its marker:
|
|
5
|
+
//
|
|
6
|
+
// <!-- factory:trip v=1 at=<ISO time of the revert or regression> state=tripped -->
|
|
7
|
+
//
|
|
8
|
+
// A person clears it by editing `state=tripped` to `state=cleared` in that comment.
|
|
9
|
+
// The gate leaves the trip only when the marker is cleared by someone with write
|
|
10
|
+
// access AND the shadow bar is met again, counted from the trip (`at`). Pure: the
|
|
11
|
+
// shell reads the control issue's comments and the evidence, this decides.
|
|
12
|
+
//
|
|
13
|
+
// no evidence, no marker -> none
|
|
14
|
+
// evidence newer than any marker -> tripped, and a new marker is written
|
|
15
|
+
// the newest marker says tripped -> tripped
|
|
16
|
+
// the newest marker says cleared -> cleared, with `barMet`; a clearing by
|
|
17
|
+
// the engine or by someone without write
|
|
18
|
+
// access does not count: tripped
|
|
19
|
+
|
|
20
|
+
export const TRIP_PREFIX = "<!-- factory:trip ";
|
|
21
|
+
const TRIP_MARKER =
|
|
22
|
+
/^<!-- factory:trip v=1 at=(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z) state=(tripped|cleared) -->$/m;
|
|
23
|
+
|
|
24
|
+
/** The marker's fields, or null when the text holds no readable one. */
|
|
25
|
+
export function parseTripComment(body) {
|
|
26
|
+
const m = TRIP_MARKER.exec(String(body ?? ""));
|
|
27
|
+
return m ? { at: Date.parse(m[1]), atIso: m[1], state: m[2] } : null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const tripCode = (iso) => `\`${iso}\``;
|
|
31
|
+
|
|
32
|
+
/** The comment the gate writes on the control issue for a new trip. */
|
|
33
|
+
export function renderTripComment({ atIso, pull = null }) {
|
|
34
|
+
const named = pull ? `pull request #${pull}` : "a pull request";
|
|
35
|
+
return `<!-- factory:trip v=1 at=${atIso} state=tripped -->
|
|
36
|
+
**Merge gate tripped** at ${tripCode(atIso)}: a revert or a regression names ${named}, which the gate merged, within 7 days of its merge. The gate acts as \`shadow\` and merges nothing.
|
|
37
|
+
|
|
38
|
+
To leave the trip, edit this comment and change \`state=tripped\` to \`state=cleared\` in the marker line (a person with write access). The gate then returns to \`on\` only once the shadow bar is met again, counted from the trip. \`agent-kit factory report --gate\` shows the bar.
|
|
39
|
+
`;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const tripLower = (login) =>
|
|
43
|
+
String(login ?? "")
|
|
44
|
+
.toLowerCase()
|
|
45
|
+
.replace(/\[bot\]$/, "");
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @param {{
|
|
49
|
+
* evidenceAt: number | null | undefined,
|
|
50
|
+
* comments: Array<{ id: number, author: string, editor?: string | null, body: string }> | null | undefined,
|
|
51
|
+
* writers: string[] | null | undefined,
|
|
52
|
+
* isEngine: (login: string) => boolean,
|
|
53
|
+
* barMet: boolean | null | undefined,
|
|
54
|
+
* }} facts
|
|
55
|
+
* `evidenceAt` is the latest revert or regression time (ms), null for none, undefined when unread.
|
|
56
|
+
* `comments` are the control issue's comments, null when unread. `writers` are the logins with write
|
|
57
|
+
* access among the editors of those comments. `barMet` is the shadow bar on every auto tier since `at`.
|
|
58
|
+
* @returns {{ state: "none" | "tripped" | "cleared" | "unreadable", at: number | null, barMet?: boolean | null, write?: boolean, why?: string }}
|
|
59
|
+
*/
|
|
60
|
+
export function tripState({ evidenceAt, comments, writers, isEngine, barMet }) {
|
|
61
|
+
if (evidenceAt === undefined || comments === null || comments === undefined) {
|
|
62
|
+
return { state: "unreadable", at: null, why: "the trip could not be read" };
|
|
63
|
+
}
|
|
64
|
+
const markers = comments
|
|
65
|
+
.filter((c) => isEngine(c.author))
|
|
66
|
+
.map((c) => ({ comment: c, marker: parseTripComment(c.body) }))
|
|
67
|
+
.filter((x) => x.marker)
|
|
68
|
+
.sort((a, b) => a.marker.at - b.marker.at || a.comment.id - b.comment.id);
|
|
69
|
+
const newest = markers.at(-1) ?? null;
|
|
70
|
+
if (evidenceAt !== null && (!newest || newest.marker.at < evidenceAt)) {
|
|
71
|
+
return { state: "tripped", at: evidenceAt, write: true };
|
|
72
|
+
}
|
|
73
|
+
if (!newest) return { state: "none", at: null };
|
|
74
|
+
const at = newest.marker.at;
|
|
75
|
+
if (newest.marker.state === "tripped") return { state: "tripped", at };
|
|
76
|
+
// Cleared: by a person with write access, never by the engine's own edit.
|
|
77
|
+
const editor = newest.comment.editor;
|
|
78
|
+
if (
|
|
79
|
+
!editor ||
|
|
80
|
+
isEngine(editor) ||
|
|
81
|
+
!Array.isArray(writers) ||
|
|
82
|
+
!writers.map(tripLower).includes(tripLower(editor))
|
|
83
|
+
) {
|
|
84
|
+
return { state: "tripped", at, why: "cleared by someone without write access" };
|
|
85
|
+
}
|
|
86
|
+
return { state: "cleared", at, barMet: barMet ?? null };
|
|
87
|
+
}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
// The Factory blueprint `init --factory` writes (ADR 0043): pure functions from
|
|
2
|
+
// the tag's `examples/factory/` and the Consumer's answers to file contents.
|
|
3
|
+
// Nothing here touches the disk or GitHub. The files are the Consumer's after
|
|
4
|
+
// the first copy; the Kit-owned bundle is copied by init's own Kit-owned step.
|
|
5
|
+
|
|
6
|
+
export const FACTORY_DIR = ".github/factory";
|
|
7
|
+
export const CONFIG_PATH = `${FACTORY_DIR}/config.json`;
|
|
8
|
+
export const RISK_RULES_PATH = `${FACTORY_DIR}/risk-rules.json`;
|
|
9
|
+
export const RULESET_PATH = `${FACTORY_DIR}/ruleset.json`;
|
|
10
|
+
export const BUNDLE_PATH = ".github/agent-factory/";
|
|
11
|
+
export const CONFIG_EXAMPLE = "examples/factory/factory-config.json";
|
|
12
|
+
export const RISK_RULES_EXAMPLE = "examples/factory/risk-rules.json";
|
|
13
|
+
export const CALLERS_DIR = "examples/factory/";
|
|
14
|
+
export const RULESET_NAME = "Agent-kit Factory: default branch";
|
|
15
|
+
export const CONTROL_MARKER = "<!-- factory:control v=1 -->";
|
|
16
|
+
/** The control issue's body marker, of this version or the previous one (no `v`). */
|
|
17
|
+
export const CONTROL_MARKER_PATTERN = /<!-- factory:control(?: v=1)? -->/;
|
|
18
|
+
export const isControlBody = (body) => CONTROL_MARKER_PATTERN.test(String(body ?? ""));
|
|
19
|
+
|
|
20
|
+
const CALLER_NAME = /^factory-[a-z][a-z0-9-]*\.yml$/;
|
|
21
|
+
const KIT_PIN =
|
|
22
|
+
/(\/agent-kit\/\.github\/workflows\/[A-Za-z0-9._-]+\.ya?ml)@v\d+\.\d+\.\d+(?:-(?:alpha|beta|rc)\.\d+)?(?=$|[\s"'#])/gm;
|
|
23
|
+
|
|
24
|
+
export class BlueprintError extends Error {}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* A Caller's text with every `uses:` pin of the kit's reusable workflows moved
|
|
28
|
+
* to `tag`. A Caller that calls none (the QA Caller runs its steps inline) has
|
|
29
|
+
* nothing to pin and is copied as it is.
|
|
30
|
+
*/
|
|
31
|
+
export function pinCaller(text, tag) {
|
|
32
|
+
return text.replace(KIT_PIN, (_, path) => `${path}@${tag}`);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The blueprint Callers the tag ships: `factory-*.yml` in `examples/factory/`. */
|
|
36
|
+
export function callerNames(files) {
|
|
37
|
+
return files.map((f) => f.replace(/^.*\//, "")).filter((n) => CALLER_NAME.test(n));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** The factory config: the tag's blueprint (Phase 1 values), `budget.notify` set to the owners to mention. */
|
|
41
|
+
export function renderConfig(text, { notify }) {
|
|
42
|
+
let config;
|
|
43
|
+
try {
|
|
44
|
+
config = JSON.parse(text);
|
|
45
|
+
} catch {
|
|
46
|
+
throw new BlueprintError(`${CONFIG_EXAMPLE} is not JSON.`);
|
|
47
|
+
}
|
|
48
|
+
if (config === null || typeof config !== "object" || Array.isArray(config) || !config.budget) {
|
|
49
|
+
throw new BlueprintError(`${CONFIG_EXAMPLE} is not a factory config.`);
|
|
50
|
+
}
|
|
51
|
+
config.budget.notify = notify;
|
|
52
|
+
return `${JSON.stringify(config, null, 2)}\n`;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Whether `text` is JSON that is an object (the risk rules are copied as they are). */
|
|
56
|
+
export function checkRiskRules(text) {
|
|
57
|
+
try {
|
|
58
|
+
const rules = JSON.parse(text);
|
|
59
|
+
if (!Array.isArray(rules?.rules)) throw new Error("no rules");
|
|
60
|
+
} catch {
|
|
61
|
+
throw new BlueprintError(`${RISK_RULES_EXAMPLE} is not a risk-rules file.`);
|
|
62
|
+
}
|
|
63
|
+
return text;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The default-branch ruleset: pull requests and the Consumer's CI required,
|
|
68
|
+
* force-push and deletion blocked, no bypass actors. It never requires a
|
|
69
|
+
* `factory/*` context: the merge gate checks the Stage checks itself, by
|
|
70
|
+
* creator, and a ruleset would block every pull request that is not a
|
|
71
|
+
* factory one (ADR 0041).
|
|
72
|
+
*/
|
|
73
|
+
export function buildRuleset({ ciChecks }) {
|
|
74
|
+
if (!ciChecks.length) throw new BlueprintError("the ruleset needs at least one CI check.");
|
|
75
|
+
return {
|
|
76
|
+
name: RULESET_NAME,
|
|
77
|
+
target: "branch",
|
|
78
|
+
enforcement: "active",
|
|
79
|
+
conditions: { ref_name: { include: ["~DEFAULT_BRANCH"], exclude: [] } },
|
|
80
|
+
bypass_actors: [],
|
|
81
|
+
rules: [
|
|
82
|
+
{ type: "deletion" },
|
|
83
|
+
{ type: "non_fast_forward" },
|
|
84
|
+
{
|
|
85
|
+
type: "pull_request",
|
|
86
|
+
parameters: {
|
|
87
|
+
required_approving_review_count: 0,
|
|
88
|
+
dismiss_stale_reviews_on_push: false,
|
|
89
|
+
require_code_owner_review: false,
|
|
90
|
+
require_last_push_approval: false,
|
|
91
|
+
required_review_thread_resolution: false,
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
type: "required_status_checks",
|
|
96
|
+
parameters: {
|
|
97
|
+
strict_required_status_checks_policy: false,
|
|
98
|
+
required_status_checks: ciChecks.map((context) => ({ context })),
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
],
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export const renderRuleset = (answers) => `${JSON.stringify(buildRuleset(answers), null, 2)}\n`;
|
|
106
|
+
|
|
107
|
+
/** The Guarded paths a person must approve (the gate never auto-merges a change to them). */
|
|
108
|
+
export const GUARDED_PATTERNS = [
|
|
109
|
+
"/.github/workflows/",
|
|
110
|
+
"/.github/factory/",
|
|
111
|
+
"/.github/agent-factory/",
|
|
112
|
+
];
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* `CODEOWNERS` with a line for each Guarded path that has none. Existing
|
|
116
|
+
* lines are never changed. Returns the new text, or null when nothing is added.
|
|
117
|
+
*/
|
|
118
|
+
export function addCodeowners(existing, owners) {
|
|
119
|
+
const have = new Set(
|
|
120
|
+
String(existing ?? "")
|
|
121
|
+
.split("\n")
|
|
122
|
+
.filter((l) => l.trim() && !l.trim().startsWith("#"))
|
|
123
|
+
.map((l) => l.trim().split(/\s+/)[0]),
|
|
124
|
+
);
|
|
125
|
+
const missing = GUARDED_PATTERNS.filter((p) => !have.has(p));
|
|
126
|
+
if (!missing.length) return null;
|
|
127
|
+
const base = String(existing ?? "");
|
|
128
|
+
const lead = base === "" ? "" : base.endsWith("\n") ? "\n" : "\n\n";
|
|
129
|
+
return (
|
|
130
|
+
`${base}${lead}# Agent-kit Factory: Guarded paths. The merge gate never auto-merges a change here.\n` +
|
|
131
|
+
`${missing.map((p) => `${p} ${owners.join(" ")}`).join("\n")}\n`
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const OWNER = /^@[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?(?:\/[A-Za-z0-9_.-]+)?$/;
|
|
136
|
+
|
|
137
|
+
/** Owners as `--codeowners` takes them: `@login` or `@org/team`, separated by commas or spaces. */
|
|
138
|
+
export function parseOwners(raw) {
|
|
139
|
+
const owners = [
|
|
140
|
+
...new Set(
|
|
141
|
+
String(raw)
|
|
142
|
+
.split(/[\s,]+/)
|
|
143
|
+
.filter(Boolean),
|
|
144
|
+
),
|
|
145
|
+
];
|
|
146
|
+
const bad = owners.find((o) => !OWNER.test(o));
|
|
147
|
+
if (!owners.length || bad !== undefined) {
|
|
148
|
+
return { error: `--codeowners takes @login or @org/team, got ${JSON.stringify(bad ?? raw)}` };
|
|
149
|
+
}
|
|
150
|
+
return { value: owners };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const CONTROL = /[\p{Cc}\p{Zl}\p{Zp}]/u;
|
|
154
|
+
|
|
155
|
+
/** CI check names as `--ci-checks` takes them: comma-separated, none of them a `factory/*` context. */
|
|
156
|
+
export function parseCiChecks(raw) {
|
|
157
|
+
const names = [
|
|
158
|
+
...new Set(
|
|
159
|
+
String(raw)
|
|
160
|
+
.split(",")
|
|
161
|
+
.map((s) => s.trim())
|
|
162
|
+
.filter(Boolean),
|
|
163
|
+
),
|
|
164
|
+
];
|
|
165
|
+
const bad = names.find((n) => CONTROL.test(n) || n.length > 200 || /^factory\//i.test(n));
|
|
166
|
+
if (!names.length || bad !== undefined) {
|
|
167
|
+
return {
|
|
168
|
+
error:
|
|
169
|
+
`--ci-checks takes your CI's check names, comma-separated, never a factory/* context ` +
|
|
170
|
+
`(the merge gate checks those itself), got ${JSON.stringify(bad ?? raw)}`,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
return { value: names };
|
|
174
|
+
}
|
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
// `agent-kit init --factory [--engine workflow|orchestrate]` (ADR 0043): the
|
|
2
|
+
// Factory blueprint as a reviewable diff. It writes only local files: the
|
|
3
|
+
// blueprint Callers (Workflow engine), the factory config with Phase 1's
|
|
4
|
+
// values, the risk rules, `CODEOWNERS` lines for the Guarded paths, a ruleset
|
|
5
|
+
// JSON, and the Kit-owned paths (the bundle `.github/agent-factory/` among
|
|
6
|
+
// them, on both engines: the Orchestrate engine reads its prompt files). The GitHub side is `factory setup`; no
|
|
7
|
+
// command sets the Kill switch, which is the last step printed.
|
|
8
|
+
//
|
|
9
|
+
// Plan first, then write, as `init` does: every file is read and every
|
|
10
|
+
// refusal checked before anything on disk changes.
|
|
11
|
+
|
|
12
|
+
import { readFileSync } from "node:fs";
|
|
13
|
+
import { join } from "node:path";
|
|
14
|
+
import { command } from "../../checks/result.mjs";
|
|
15
|
+
import { DEFAULT_KIT_REPO } from "../../callers.mjs";
|
|
16
|
+
import { fileAndPinChecks, printFileAndPinChecks } from "../../doctor.mjs";
|
|
17
|
+
import { firstLine } from "../../exec.mjs";
|
|
18
|
+
import { PreManifestTagError, serverFromEnv } from "../../kit-tag.mjs";
|
|
19
|
+
import {
|
|
20
|
+
applyPlan,
|
|
21
|
+
checkLocalPath,
|
|
22
|
+
listed,
|
|
23
|
+
lstatOrNull,
|
|
24
|
+
Refusal,
|
|
25
|
+
reportRefusal,
|
|
26
|
+
stageFileModes,
|
|
27
|
+
} from "../../local-writes.mjs";
|
|
28
|
+
import { caseCollisions } from "../../safe-paths.mjs";
|
|
29
|
+
import { isReleaseTag } from "../../tags.mjs";
|
|
30
|
+
import {
|
|
31
|
+
addCodeowners,
|
|
32
|
+
BlueprintError,
|
|
33
|
+
CALLERS_DIR,
|
|
34
|
+
callerNames,
|
|
35
|
+
checkRiskRules,
|
|
36
|
+
CONFIG_EXAMPLE,
|
|
37
|
+
CONFIG_PATH,
|
|
38
|
+
parseCiChecks,
|
|
39
|
+
parseOwners,
|
|
40
|
+
pinCaller,
|
|
41
|
+
renderConfig,
|
|
42
|
+
renderRuleset,
|
|
43
|
+
RISK_RULES_EXAMPLE,
|
|
44
|
+
RISK_RULES_PATH,
|
|
45
|
+
RULESET_PATH,
|
|
46
|
+
} from "./blueprint.mjs";
|
|
47
|
+
|
|
48
|
+
export const ENGINES = ["workflow", "orchestrate"];
|
|
49
|
+
const CODEOWNERS_FILES = [".github/CODEOWNERS", "CODEOWNERS", "docs/CODEOWNERS"];
|
|
50
|
+
|
|
51
|
+
/** The answers `init --factory` asks for, as flags name them. */
|
|
52
|
+
export const FACTORY_QUESTIONS = [
|
|
53
|
+
{ key: "tag", flag: "--tag <vX>", ask: "Agent-kit tag to adopt (for example v1.0.0)" },
|
|
54
|
+
{
|
|
55
|
+
key: "codeowners",
|
|
56
|
+
flag: "--codeowners <@login,...>",
|
|
57
|
+
ask: "CODEOWNERS: who approves changes to the Guarded paths (@login or @org/team)",
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
key: "ciChecks",
|
|
61
|
+
flag: "--ci-checks <name,...>",
|
|
62
|
+
ask: "Your CI's check names, which the ruleset requires (comma-separated)",
|
|
63
|
+
},
|
|
64
|
+
];
|
|
65
|
+
|
|
66
|
+
/** Check and normalise one answer. Returns { value } or { error }. */
|
|
67
|
+
export function parseFactoryAnswer(key, raw) {
|
|
68
|
+
const s = String(raw ?? "");
|
|
69
|
+
if (key === "tag") {
|
|
70
|
+
return isReleaseTag(s)
|
|
71
|
+
? { value: s }
|
|
72
|
+
: {
|
|
73
|
+
error: `--tag must be a Release-line tag (vX.Y.Z or vX.Y.Z-stage.N), got ${JSON.stringify(s)}`,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
if (key === "codeowners") return parseOwners(s);
|
|
77
|
+
if (key === "ciChecks") return parseCiChecks(s);
|
|
78
|
+
throw new Error(`unknown answer ${key}`);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async function collect({ flags, prompt, yes }) {
|
|
82
|
+
const interactive = Boolean(prompt) && !yes;
|
|
83
|
+
const answers = {};
|
|
84
|
+
const missing = [];
|
|
85
|
+
for (const q of FACTORY_QUESTIONS) {
|
|
86
|
+
let raw = flags[q.key];
|
|
87
|
+
if (raw === undefined && interactive) {
|
|
88
|
+
raw = await prompt(`${q.ask}: `);
|
|
89
|
+
if (raw === "") return { error: `${q.ask} is required (${q.flag}).` };
|
|
90
|
+
}
|
|
91
|
+
if (raw === undefined || raw === "") {
|
|
92
|
+
missing.push(q.flag);
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
const parsed = parseFactoryAnswer(q.key, raw);
|
|
96
|
+
if (parsed.error) return { error: parsed.error };
|
|
97
|
+
answers[q.key] = parsed.value;
|
|
98
|
+
}
|
|
99
|
+
return missing.length ? { missing } : { answers };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Everything `init --factory` would write, or a Refusal. Touches nothing on disk.
|
|
104
|
+
* @param {{ runner: any, root: string, snap: any, answers: any, engine: string, force: boolean, planKitOwned: Function }} opts
|
|
105
|
+
*/
|
|
106
|
+
export function planFactoryInit({ runner, root, snap, answers, engine, force, planKitOwned }) {
|
|
107
|
+
const { tag } = snap;
|
|
108
|
+
const text = (path) => {
|
|
109
|
+
const t = snap.readFile(path);
|
|
110
|
+
if (t === null) throw new Refusal(`${tag} has no ${path}.`);
|
|
111
|
+
if (/[\r\ufffd\u0085]|[\p{Cc}\p{Zl}\p{Zp}]/u.test(t.replace(/[\n\t]/g, ""))) {
|
|
112
|
+
throw new Refusal(`${path} at ${tag} is not plain UTF-8 text with LF line endings.`);
|
|
113
|
+
}
|
|
114
|
+
return t;
|
|
115
|
+
};
|
|
116
|
+
const writes = [];
|
|
117
|
+
const conflicts = [];
|
|
118
|
+
const overwrites = [];
|
|
119
|
+
const fileTarget = (rel, what) => {
|
|
120
|
+
checkLocalPath(root, rel);
|
|
121
|
+
const st = lstatOrNull(join(root, rel));
|
|
122
|
+
if (st && !st.isFile()) {
|
|
123
|
+
throw new Refusal(`${rel} exists and is not a file, so init cannot write the ${what} there.`);
|
|
124
|
+
}
|
|
125
|
+
if (st) {
|
|
126
|
+
conflicts.push(`${rel} (${what})`);
|
|
127
|
+
overwrites.push(rel);
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
const add = (path, what, content) => {
|
|
131
|
+
fileTarget(path, what);
|
|
132
|
+
writes.push({ path, bytes: Buffer.from(content, "utf8") });
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
try {
|
|
136
|
+
// Both engines get every Kit-owned path, the bundle folder included: the Orchestrate engine
|
|
137
|
+
// runs the sweep from the CLI, but it reads `planner-header-prompt.md` (and the other files
|
|
138
|
+
// of `.github/agent-factory/`) from the checked-out commit, so a Consumer cannot plan
|
|
139
|
+
// without them. `upgrade` replaces the same folder whole.
|
|
140
|
+
const kit = planKitOwned({ runner, root, snap, force });
|
|
141
|
+
conflicts.push(...kit.conflicts);
|
|
142
|
+
writes.push(...kit.writes);
|
|
143
|
+
|
|
144
|
+
if (engine === "workflow") {
|
|
145
|
+
const names = callerNames(snap.listFiles(CALLERS_DIR).map((f) => f.path));
|
|
146
|
+
if (!names.length) throw new Refusal(`${tag} ships no Factory Callers in ${CALLERS_DIR}.`);
|
|
147
|
+
for (const name of names) {
|
|
148
|
+
add(`.github/workflows/${name}`, "Caller", pinCaller(text(`${CALLERS_DIR}${name}`), tag));
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
const mention = answers.codeowners.filter((o) => !o.includes("/"));
|
|
152
|
+
add(CONFIG_PATH, "factory config", renderConfig(text(CONFIG_EXAMPLE), { notify: mention }));
|
|
153
|
+
add(RISK_RULES_PATH, "risk rules", checkRiskRules(text(RISK_RULES_EXAMPLE)));
|
|
154
|
+
add(RULESET_PATH, "ruleset", renderRuleset(answers));
|
|
155
|
+
|
|
156
|
+
const codeowners = CODEOWNERS_FILES.find((p) => lstatOrNull(join(root, p)) !== null);
|
|
157
|
+
const target = codeowners ?? CODEOWNERS_FILES[0];
|
|
158
|
+
checkLocalPath(root, target);
|
|
159
|
+
const st = lstatOrNull(join(root, target));
|
|
160
|
+
if (st && !st.isFile()) throw new Refusal(`${target} exists and is not a file.`);
|
|
161
|
+
const current = st ? readFileSync(join(root, target), "utf8") : "";
|
|
162
|
+
const next = addCodeowners(current, answers.codeowners);
|
|
163
|
+
const rewrites = next === null ? [] : [{ path: target, bytes: Buffer.from(next, "utf8") }];
|
|
164
|
+
|
|
165
|
+
if (conflicts.length && !force) {
|
|
166
|
+
throw new Refusal(
|
|
167
|
+
`${conflicts.length} path(s) init --factory would write already exist, and init does not overwrite them ` +
|
|
168
|
+
"(they are yours after the first copy). Move to another tag with " +
|
|
169
|
+
`\`npx @mnaimfaizy/agent-kit upgrade --tag ${tag}\`, or run init --factory again with --force.`,
|
|
170
|
+
listed(conflicts),
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
const clash = caseCollisions(writes.map((w) => w.path));
|
|
174
|
+
if (clash.length)
|
|
175
|
+
throw new Refusal(`Two files differ only in case: ${clash[0].join(" and ")}.`);
|
|
176
|
+
return { deletes: kit.deletes, writes, rewrites, conflicts, unchanged: kit.unchanged };
|
|
177
|
+
} catch (e) {
|
|
178
|
+
if (e instanceof BlueprintError) throw new Refusal(e.message);
|
|
179
|
+
throw e;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** The steps init leaves, as POSIX-shell commands built with `command()`. */
|
|
184
|
+
export function factorySteps({ engine, tag, kitRepo, serverUrl }) {
|
|
185
|
+
const docs = `${serverUrl}/${kitRepo}/blob/${tag}/docs/factory`;
|
|
186
|
+
const orchestrate = engine === "orchestrate";
|
|
187
|
+
const steps = [
|
|
188
|
+
`1. Review \`git status\` and \`git diff\`, then commit and push these files to the default branch (through a pull request if it is protected).`,
|
|
189
|
+
`2. The GitHub side (${orchestrate ? "labels, the control issue, the ruleset" : "labels, the control issue, the App, the ruleset"}), with an admin \`gh\` login, in a terminal:`,
|
|
190
|
+
` $ ${command("npx", "@mnaimfaizy/agent-kit", "factory", "setup", "--engine", engine)}`,
|
|
191
|
+
`3. Read ${docs}/setup.md, then the Rollout phases.`,
|
|
192
|
+
];
|
|
193
|
+
if (orchestrate) {
|
|
194
|
+
steps.push(
|
|
195
|
+
"4. The Orchestrate engine runs from your machine with a fine-grained PAT for this one repository: Contents, Issues, Pull requests and Statuses write, Variables read. `factory setup` prints the steps. Give it to the engine as AGENT_KIT_GITHUB_TOKEN (a value or an op:// reference), never `gh auth token`.",
|
|
196
|
+
`5. Run it unattended from the host recipe (Linux, systemd, the proven one; Windows and macOS are unproven): ${docs}/host.md`,
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
steps.push(
|
|
200
|
+
`Last, only when you have reviewed and pushed ${orchestrate ? "these files" : "the Callers"}, turn the Factory on (the Kill switch; no agent-kit command ever sets it):`,
|
|
201
|
+
` $ ${command("gh", "variable", "set", "AGENT_KIT_FACTORY_ENABLED", "--body", "true")}`,
|
|
202
|
+
);
|
|
203
|
+
return steps;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* @param {{
|
|
208
|
+
* cwd: string, runner: any, flags: Record<string, string | undefined>, engine?: string,
|
|
209
|
+
* force?: boolean, yes?: boolean, prompt?: Function | null, confirm?: Function | null,
|
|
210
|
+
* out: Function, err: Function, openTag: Function, env?: NodeJS.ProcessEnv, planKitOwned: Function,
|
|
211
|
+
* }} opts
|
|
212
|
+
* @returns {Promise<number>} exit code
|
|
213
|
+
*/
|
|
214
|
+
export async function runFactoryInit({
|
|
215
|
+
cwd,
|
|
216
|
+
runner,
|
|
217
|
+
flags,
|
|
218
|
+
engine = "workflow",
|
|
219
|
+
force = false,
|
|
220
|
+
yes = false,
|
|
221
|
+
prompt = null,
|
|
222
|
+
confirm = null,
|
|
223
|
+
out,
|
|
224
|
+
err,
|
|
225
|
+
openTag,
|
|
226
|
+
env = process.env,
|
|
227
|
+
planKitOwned,
|
|
228
|
+
}) {
|
|
229
|
+
const refuse = (e) => reportRefusal(out, e);
|
|
230
|
+
if (!ENGINES.includes(engine)) {
|
|
231
|
+
err(`--engine takes workflow or orchestrate, got ${JSON.stringify(engine)}`);
|
|
232
|
+
return 2;
|
|
233
|
+
}
|
|
234
|
+
const collected = await collect({ flags, prompt, yes });
|
|
235
|
+
if (collected.error) {
|
|
236
|
+
err(collected.error);
|
|
237
|
+
return 2;
|
|
238
|
+
}
|
|
239
|
+
if (collected.missing) {
|
|
240
|
+
const why = prompt && yes ? "--yes asks nothing" : "there is no terminal to ask on";
|
|
241
|
+
const needed = [...collected.missing, ...(prompt || yes ? [] : ["--yes"])];
|
|
242
|
+
err(`agent-kit init --factory: ${why}, so pass ${needed.join(" and ")}.`);
|
|
243
|
+
return 2;
|
|
244
|
+
}
|
|
245
|
+
if (!prompt && !yes) {
|
|
246
|
+
err("agent-kit init --factory: there is no terminal to confirm on, so pass --yes.");
|
|
247
|
+
return 2;
|
|
248
|
+
}
|
|
249
|
+
const { answers } = collected;
|
|
250
|
+
const top = runner("git", ["-C", cwd, "rev-parse", "--show-toplevel"]);
|
|
251
|
+
if (top.status !== 0) return refuse(new Refusal(`${cwd} is not a git repository.`));
|
|
252
|
+
const root = top.stdout.trim();
|
|
253
|
+
const serverUrl = serverFromEnv(env);
|
|
254
|
+
out(
|
|
255
|
+
`Adopting the Factory (${engine} engine) from ${DEFAULT_KIT_REPO}@${answers.tag} in ${root}.`,
|
|
256
|
+
);
|
|
257
|
+
|
|
258
|
+
let snap;
|
|
259
|
+
try {
|
|
260
|
+
snap = openTag({
|
|
261
|
+
runner,
|
|
262
|
+
repo: DEFAULT_KIT_REPO,
|
|
263
|
+
tag: answers.tag,
|
|
264
|
+
serverUrl,
|
|
265
|
+
forDeletion: true,
|
|
266
|
+
});
|
|
267
|
+
} catch (e) {
|
|
268
|
+
if (e instanceof PreManifestTagError) return refuse(e);
|
|
269
|
+
return refuse(new Refusal(firstLine(e?.message ?? e)));
|
|
270
|
+
}
|
|
271
|
+
try {
|
|
272
|
+
let plan;
|
|
273
|
+
try {
|
|
274
|
+
plan = planFactoryInit({ runner, root, snap, answers, engine, force, planKitOwned });
|
|
275
|
+
} catch (e) {
|
|
276
|
+
if (e instanceof Refusal) return refuse(e);
|
|
277
|
+
return refuse(new Refusal(firstLine(e?.message ?? e)));
|
|
278
|
+
}
|
|
279
|
+
out("");
|
|
280
|
+
out(`Write (from ${answers.tag}):`);
|
|
281
|
+
const kitPaths = snap.kitOwnedPaths.filter(
|
|
282
|
+
(p) => !plan.unchanged.includes(p) && plan.writes.some((w) => w.path.startsWith(p)),
|
|
283
|
+
);
|
|
284
|
+
for (const p of kitPaths) {
|
|
285
|
+
const n = plan.writes.filter((w) => w.path.startsWith(p)).length;
|
|
286
|
+
out(` ${p} (${n} file${n === 1 ? "" : "s"})`);
|
|
287
|
+
}
|
|
288
|
+
for (const w of plan.writes.filter(
|
|
289
|
+
(x) => !snap.kitOwnedPaths.some((p) => x.path.startsWith(p)),
|
|
290
|
+
)) {
|
|
291
|
+
out(` ${w.path}`);
|
|
292
|
+
}
|
|
293
|
+
for (const w of plan.rewrites) out(` ${w.path} (Guarded-path lines added)`);
|
|
294
|
+
if (plan.conflicts.length) {
|
|
295
|
+
out(`--force: overwriting ${plan.conflicts.length} existing path(s):`);
|
|
296
|
+
for (const c of listed(plan.conflicts)) out(` ${c}`);
|
|
297
|
+
}
|
|
298
|
+
out("");
|
|
299
|
+
if (!yes && !(confirm && (await confirm("Go ahead? [y/N] ")))) {
|
|
300
|
+
out("Stopped. Nothing was changed.");
|
|
301
|
+
return 1;
|
|
302
|
+
}
|
|
303
|
+
try {
|
|
304
|
+
applyPlan(root, plan);
|
|
305
|
+
} catch (e) {
|
|
306
|
+
if (e instanceof Refusal) return refuse(e);
|
|
307
|
+
out(`FAILED init stopped part-way: ${firstLine(e?.message ?? e)}`);
|
|
308
|
+
out(" See `git status`; remove what was written and run init --factory again.");
|
|
309
|
+
return 1;
|
|
310
|
+
}
|
|
311
|
+
out(`Wrote ${plan.writes.length + plan.rewrites.length} file(s).`);
|
|
312
|
+
const note = stageFileModes({ runner, root, writes: plan.writes });
|
|
313
|
+
if (note) out(note);
|
|
314
|
+
out("");
|
|
315
|
+
const failed =
|
|
316
|
+
engine === "workflow"
|
|
317
|
+
? printFileAndPinChecks(out, fileAndPinChecks({ runner, root, snap }))
|
|
318
|
+
: false;
|
|
319
|
+
out("");
|
|
320
|
+
out(
|
|
321
|
+
"Still to do (init changed nothing on GitHub). Lines starting with $ are POSIX shell commands for you to run:",
|
|
322
|
+
);
|
|
323
|
+
for (const line of factorySteps({
|
|
324
|
+
engine,
|
|
325
|
+
tag: answers.tag,
|
|
326
|
+
kitRepo: DEFAULT_KIT_REPO,
|
|
327
|
+
serverUrl,
|
|
328
|
+
})) {
|
|
329
|
+
out(line);
|
|
330
|
+
}
|
|
331
|
+
out("");
|
|
332
|
+
out(failed ? "Result: a check failed (exit 1)." : "Result: done.");
|
|
333
|
+
return failed ? 1 : 0;
|
|
334
|
+
} finally {
|
|
335
|
+
snap.cleanup();
|
|
336
|
+
}
|
|
337
|
+
}
|