@mnaimfaizy/agent-kit 1.0.0-beta.15 → 1.0.0-beta.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/package.json +1 -1
- package/src/cli.mjs +314 -4
- package/src/doctor.mjs +53 -1
- package/src/exec.mjs +14 -5
- package/src/factory/budget/budget.mjs +259 -0
- package/src/factory/build/command.mjs +321 -0
- package/src/factory/build/factory.mjs +473 -0
- package/src/factory/build/findings.mjs +55 -0
- package/src/factory/build/host.mjs +230 -0
- package/src/factory/build/notes.mjs +191 -0
- package/src/factory/build/pull-request.mjs +74 -0
- package/src/factory/bundle.mjs +146 -0
- package/src/factory/checks/allowed-paths.mjs +50 -0
- package/src/factory/checks/plan-header.mjs +296 -0
- package/src/factory/checks/qa.mjs +85 -0
- package/src/factory/checks/review.mjs +73 -0
- package/src/factory/checks/security.mjs +50 -0
- package/src/factory/checks/stage-check.mjs +89 -0
- package/src/factory/claims/command.mjs +187 -0
- package/src/factory/claims/release.mjs +111 -0
- package/src/factory/decision/answers.mjs +103 -0
- package/src/factory/decision/command.mjs +64 -0
- package/src/factory/decision/plan.mjs +99 -0
- package/src/factory/decision/request.mjs +116 -0
- package/src/factory/doctor/files.mjs +329 -0
- package/src/factory/doctor/index.mjs +56 -0
- package/src/factory/doctor/local.mjs +497 -0
- package/src/factory/doctor/orchestrate.mjs +146 -0
- package/src/factory/engine/lease.mjs +146 -0
- package/src/factory/engine/local-engine.mjs +177 -0
- package/src/factory/engine/lockfile.mjs +64 -0
- package/src/factory/engine/workflow-engine.mjs +124 -0
- package/src/factory/gate/comment.mjs +88 -0
- package/src/factory/gate/config.mjs +57 -0
- package/src/factory/gate/gate-core.mjs +250 -0
- package/src/factory/gate/guarded.mjs +17 -0
- package/src/factory/gate/reads.mjs +105 -0
- package/src/factory/gate/task.mjs +193 -0
- package/src/factory/gate/trip-reads.mjs +82 -0
- package/src/factory/gate/trip.mjs +87 -0
- package/src/factory/init/blueprint.mjs +174 -0
- package/src/factory/init/index.mjs +337 -0
- package/src/factory/labels/evidence.mjs +162 -0
- package/src/factory/labels/moves.mjs +32 -0
- package/src/factory/labels/reconcile.mjs +267 -0
- package/src/factory/labels/revert.mjs +87 -0
- package/src/factory/labels/timeline.mjs +72 -0
- package/src/factory/labels/voided.mjs +59 -0
- package/src/factory/limits/harness.mjs +39 -0
- package/src/factory/limits/limits.mjs +123 -0
- package/src/factory/merge/command.mjs +172 -0
- package/src/factory/outbox/check.mjs +240 -0
- package/src/factory/outbox/rulings.mjs +195 -0
- package/src/factory/plan/command.mjs +66 -0
- package/src/factory/plan/comment.mjs +156 -0
- package/src/factory/plan/publish.mjs +149 -0
- package/src/factory/plan/risk.mjs +153 -0
- package/src/factory/qa/command.mjs +163 -0
- package/src/factory/records/engine-record.mjs +133 -0
- package/src/factory/records/main.mjs +96 -0
- package/src/factory/records/record.mjs +226 -0
- package/src/factory/records/sink.mjs +57 -0
- package/src/factory/report/build.mjs +92 -0
- package/src/factory/report/command.mjs +80 -0
- package/src/factory/report/facts.mjs +226 -0
- package/src/factory/report/gate-section.mjs +122 -0
- package/src/factory/report/metrics.mjs +111 -0
- package/src/factory/report/reads.mjs +246 -0
- package/src/factory/report/render.mjs +121 -0
- package/src/factory/report/rollout.mjs +154 -0
- package/src/factory/review/command.mjs +182 -0
- package/src/factory/run/awake.mjs +70 -0
- package/src/factory/run/collect.mjs +77 -0
- package/src/factory/run/command.mjs +490 -0
- package/src/factory/run/gh.mjs +46 -0
- package/src/factory/run/launch.mjs +47 -0
- package/src/factory/run/pipelines.mjs +261 -0
- package/src/factory/run/raw.mjs +96 -0
- package/src/factory/run/restart.mjs +83 -0
- package/src/factory/run/s3.mjs +193 -0
- package/src/factory/run/secrets.mjs +142 -0
- package/src/factory/run/teardown.mjs +242 -0
- package/src/factory/run/token.mjs +68 -0
- package/src/factory/security/command.mjs +157 -0
- package/src/factory/setup/app.mjs +122 -0
- package/src/factory/setup/command.mjs +409 -0
- package/src/factory/setup/labels.mjs +25 -0
- package/src/factory/setup/reads.mjs +129 -0
- package/src/factory/setup/writes.mjs +126 -0
- package/src/factory/snapshot/write.mjs +176 -0
- package/src/factory/stop/command.mjs +136 -0
- package/src/factory/sweep/actions-main.mjs +117 -0
- package/src/factory/sweep/command.mjs +65 -0
- package/src/factory/sweep/knobs.mjs +78 -0
- package/src/factory/sweep/marker-version.mjs +15 -0
- package/src/factory/sweep/markers.mjs +112 -0
- package/src/factory/sweep/overlap.mjs +24 -0
- package/src/factory/sweep/run.mjs +198 -0
- package/src/factory/sweep/shell.mjs +394 -0
- package/src/factory/sweep/split.mjs +104 -0
- package/src/factory/sweep/states.mjs +87 -0
- package/src/factory/sweep/sweep-core.mjs +1781 -0
- package/src/factory/tracker/github.mjs +674 -0
- package/src/factory/tracker/operations.mjs +153 -0
- package/src/factory/upgrade/index.mjs +83 -0
- package/src/init.mjs +62 -24
- package/src/kit-owned-paths.mjs +1 -1
- package/src/stage.mjs +19 -0
- package/src/upgrade.mjs +39 -7
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
// The Rollout phases (spec #500, Rollout phases) as documented sets of config
|
|
2
|
+
// values, which phase the effective config matches, and whether the next phase's
|
|
3
|
+
// bar is met. Pure. A phase is never a config key: it is read off these values.
|
|
4
|
+
//
|
|
5
|
+
// Phase | Kill switch | plan_approval | merge.mode | auto_tiers
|
|
6
|
+
// 0 | off | low | shadow | [low]
|
|
7
|
+
// 1 | on | low | shadow | [low]
|
|
8
|
+
// 2 | on | high | shadow | [low]
|
|
9
|
+
// 3 | on | high | on | [low]
|
|
10
|
+
// 4 | on | high | on | [low, medium]
|
|
11
|
+
//
|
|
12
|
+
// The Daily cap is the Consumer's choice in every phase, so it is not compared.
|
|
13
|
+
|
|
14
|
+
import { DAY } from "./facts.mjs";
|
|
15
|
+
|
|
16
|
+
export const PHASES = Object.freeze([
|
|
17
|
+
{ phase: 0, name: "Installed", kill: "false", plan: "low", mode: "shadow", tiers: ["low"] },
|
|
18
|
+
{ phase: 1, name: "Staged", kill: "true", plan: "low", mode: "shadow", tiers: ["low"] },
|
|
19
|
+
{
|
|
20
|
+
phase: 2,
|
|
21
|
+
name: "Unsupervised plans",
|
|
22
|
+
kill: "true",
|
|
23
|
+
plan: "high",
|
|
24
|
+
mode: "shadow",
|
|
25
|
+
tiers: ["low"],
|
|
26
|
+
},
|
|
27
|
+
{ phase: 3, name: "Auto-merge low", kill: "true", plan: "high", mode: "on", tiers: ["low"] },
|
|
28
|
+
{
|
|
29
|
+
phase: 4,
|
|
30
|
+
name: "Auto-merge medium",
|
|
31
|
+
kill: "true",
|
|
32
|
+
plan: "high",
|
|
33
|
+
mode: "on",
|
|
34
|
+
tiers: ["low", "medium"],
|
|
35
|
+
},
|
|
36
|
+
]);
|
|
37
|
+
|
|
38
|
+
/** The 1 to 2 bar's defaults; a Consumer may tighten them. */
|
|
39
|
+
export const STAGED_BAR = Object.freeze({
|
|
40
|
+
terminal: 10,
|
|
41
|
+
days: 7,
|
|
42
|
+
merge_rate: 0.7,
|
|
43
|
+
first_pass: 0.5,
|
|
44
|
+
escalation: 0.3,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const sameSet = (a, b) => a.length === b.length && a.every((x) => b.includes(x));
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @param {{ killSwitch: "true" | "false" | null, planApproval: string, mergeMode: string, autoTiers: string[] }} effective
|
|
51
|
+
* `killSwitch` is null when the variable could not be read.
|
|
52
|
+
* @returns {{ phase: number | null, candidates: number[], name: string | null }}
|
|
53
|
+
*/
|
|
54
|
+
export function matchPhase(effective) {
|
|
55
|
+
const matches = PHASES.filter(
|
|
56
|
+
(p) =>
|
|
57
|
+
p.plan === effective.planApproval &&
|
|
58
|
+
p.mode === effective.mergeMode &&
|
|
59
|
+
sameSet(p.tiers, effective.autoTiers) &&
|
|
60
|
+
(effective.killSwitch === null || p.kill === effective.killSwitch),
|
|
61
|
+
);
|
|
62
|
+
const candidates = matches.map((p) => p.phase);
|
|
63
|
+
if (candidates.length === 1) return { phase: candidates[0], candidates, name: matches[0].name };
|
|
64
|
+
return { phase: null, candidates, name: null };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const check = (name, value, met, want) => ({ name, value, met, want });
|
|
68
|
+
|
|
69
|
+
/** 1 to 2: over `low` and `medium` terminal tasks. */
|
|
70
|
+
function stagedBar(tasks, bar = STAGED_BAR) {
|
|
71
|
+
const mine = tasks.filter((t) => ["low", "medium"].includes(t.tier));
|
|
72
|
+
const n = mine.length;
|
|
73
|
+
const merged = mine.filter((t) => t.merged);
|
|
74
|
+
const rate = (k) => (n === 0 ? null : k / n);
|
|
75
|
+
const mergeRate = rate(merged.length);
|
|
76
|
+
const firstPass = rate(mine.filter((t) => t.firstPass).length);
|
|
77
|
+
const escalation = rate(mine.filter((t) => t.escalated).length);
|
|
78
|
+
const reverted = merged.filter((t) => t.reverted).length;
|
|
79
|
+
const starts = mine.map((t) => t.queuedAt).filter((t) => t !== null);
|
|
80
|
+
const days =
|
|
81
|
+
starts.length === 0
|
|
82
|
+
? 0
|
|
83
|
+
: (Math.max(...mine.map((t) => t.closedAt)) - Math.min(...starts)) / DAY;
|
|
84
|
+
return [
|
|
85
|
+
check("terminal tasks", n, n >= bar.terminal, `at least ${bar.terminal}`),
|
|
86
|
+
check("days covered", Math.floor(days), days >= bar.days, `at least ${bar.days}`),
|
|
87
|
+
check(
|
|
88
|
+
"merge rate",
|
|
89
|
+
mergeRate,
|
|
90
|
+
mergeRate !== null && mergeRate >= bar.merge_rate,
|
|
91
|
+
`at least ${bar.merge_rate}`,
|
|
92
|
+
),
|
|
93
|
+
check(
|
|
94
|
+
"first-pass success",
|
|
95
|
+
firstPass,
|
|
96
|
+
firstPass !== null && firstPass >= bar.first_pass,
|
|
97
|
+
`at least ${bar.first_pass}`,
|
|
98
|
+
),
|
|
99
|
+
check(
|
|
100
|
+
"escalation rate",
|
|
101
|
+
escalation,
|
|
102
|
+
escalation !== null && escalation <= bar.escalation,
|
|
103
|
+
`at most ${bar.escalation}`,
|
|
104
|
+
),
|
|
105
|
+
check(
|
|
106
|
+
"revert rate",
|
|
107
|
+
merged.length ? reverted / merged.length : null,
|
|
108
|
+
// With nothing merged there is no revert rate to call 0.
|
|
109
|
+
merged.length > 0 && reverted === 0,
|
|
110
|
+
"0",
|
|
111
|
+
),
|
|
112
|
+
];
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** 2 to 3 and 3 to 4: the shadow bar on a tier, and that tier's revert rate of 0. */
|
|
116
|
+
function shadowBar(gate, tier, tasks) {
|
|
117
|
+
const g = gate.tiers[tier];
|
|
118
|
+
const merged = tasks.filter((t) => t.merged && t.tier === tier);
|
|
119
|
+
const reverted = merged.filter((t) => t.reverted).length;
|
|
120
|
+
return [
|
|
121
|
+
...g.checks.map((c) => check(`${tier}: ${c.name}`, c.value, c.met, "")),
|
|
122
|
+
check(
|
|
123
|
+
`${tier}: revert rate`,
|
|
124
|
+
merged.length ? reverted / merged.length : null,
|
|
125
|
+
merged.length > 0 && reverted === 0,
|
|
126
|
+
"0",
|
|
127
|
+
),
|
|
128
|
+
];
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Which phase the config matches and whether the next bar is met.
|
|
133
|
+
* @param {{ effective: object, tasks: object[], gate: object }} input
|
|
134
|
+
*/
|
|
135
|
+
export function rolloutOf({ effective, tasks, gate }) {
|
|
136
|
+
const match = matchPhase(effective);
|
|
137
|
+
const out = { phase: match.phase, name: match.name, candidates: match.candidates, effective };
|
|
138
|
+
if (match.phase === null) return { ...out, next: null };
|
|
139
|
+
const to = match.phase + 1;
|
|
140
|
+
const tier = to === 3 ? "low" : "medium";
|
|
141
|
+
if (match.phase === 0) {
|
|
142
|
+
return {
|
|
143
|
+
...out,
|
|
144
|
+
next: { phase: 1, bar: "none: the owner turns the Kill switch on", met: null, checks: [] },
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
if (match.phase === 4) return { ...out, next: null };
|
|
148
|
+
const checks = match.phase === 1 ? stagedBar(tasks) : shadowBar(gate, tier, tasks);
|
|
149
|
+
const text =
|
|
150
|
+
match.phase === 1
|
|
151
|
+
? "10 terminal tasks over 7 days on low and medium; merge rate 70%, first-pass 50%, escalation 30%, revert 0"
|
|
152
|
+
: `the shadow bar on ${tier}, and a ${tier} revert rate of 0`;
|
|
153
|
+
return { ...out, next: { phase: to, bar: text, met: checks.every((c) => c.met), checks } };
|
|
154
|
+
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
// The review Stage's trusted steps, as commands of the Kit-owned bundle
|
|
2
|
+
// (`node .github/agent-factory/sweep.mjs <command>`), run by the `factory-review`
|
|
3
|
+
// Caller (examples/factory/factory-review.yml). No agent runs in these jobs: the
|
|
4
|
+
// header they read was written by the reusable's publish job, from threads
|
|
5
|
+
// GitHub holds, and the status they write is the Stage check (ADR 0035).
|
|
6
|
+
//
|
|
7
|
+
// review-target --issue <n>
|
|
8
|
+
// finds the open pull request of the task's newest build
|
|
9
|
+
// Attempt (branch factory/<issue>/attempt-<n>, from this
|
|
10
|
+
// repository) and prints `pr_number=`, `head_sha=`,
|
|
11
|
+
// `base_sha=` and `base_ref=` to GITHUB_OUTPUT. None: exit 1.
|
|
12
|
+
// review-start --sha <head> [--run-url <url>]
|
|
13
|
+
// writes `factory/review` as `pending` on the head.
|
|
14
|
+
// review-check --pr <n> --sha <head> --run-id <id> [--run-url <url>]
|
|
15
|
+
// reads the Review report this run posted, by the engine's
|
|
16
|
+
// identity, and writes `factory/review` on the head from its
|
|
17
|
+
// strict header: `success` or `failure`, or `error` when the
|
|
18
|
+
// run left no valid report. Prints `state=`. Exit 0 whatever
|
|
19
|
+
// the state (the status is the result); 1 when the status
|
|
20
|
+
// cannot be written.
|
|
21
|
+
// Exit 2 is a usage error. The write token is the job's GITHUB_TOKEN, with
|
|
22
|
+
// `statuses: write`, so the creator is the Workflow engine's identity.
|
|
23
|
+
|
|
24
|
+
import { reviewStageCheck } from "../checks/review.mjs";
|
|
25
|
+
import { STAGE_REPO, STAGE_SHA, writeStageCheck } from "../checks/stage-check.mjs";
|
|
26
|
+
import { WORKFLOW_IDENTITY } from "../engine/workflow-engine.mjs";
|
|
27
|
+
import { createGithubTracker } from "../tracker/github.mjs";
|
|
28
|
+
|
|
29
|
+
const REVIEW_REPORT_TITLE = "**Agent-kit review report**";
|
|
30
|
+
|
|
31
|
+
const reviewBare = (login) =>
|
|
32
|
+
String(login ?? "")
|
|
33
|
+
.replace(/\[bot\]$/, "")
|
|
34
|
+
.toLowerCase();
|
|
35
|
+
|
|
36
|
+
const reviewOptionReader = (args) => (name) => {
|
|
37
|
+
const at = args.indexOf(`--${name}`);
|
|
38
|
+
return at === -1 ? undefined : args[at + 1];
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
function reviewContext({ env, args, need }) {
|
|
42
|
+
const option = reviewOptionReader(args);
|
|
43
|
+
const repo = env.GITHUB_REPOSITORY ?? "";
|
|
44
|
+
if (!STAGE_REPO.test(repo)) return { usage: "GITHUB_REPOSITORY is not set" };
|
|
45
|
+
for (const name of need) {
|
|
46
|
+
if (!option(name)) return { usage: `--${name} is missing` };
|
|
47
|
+
}
|
|
48
|
+
return { option, repo };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const reviewRunUrl = (option, env, repo) => {
|
|
52
|
+
const given = option("run-url");
|
|
53
|
+
if (given) return given;
|
|
54
|
+
return env.GITHUB_RUN_ID
|
|
55
|
+
? `${env.GITHUB_SERVER_URL ?? "https://github.com"}/${repo}/actions/runs/${env.GITHUB_RUN_ID}`
|
|
56
|
+
: undefined;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/** The task's newest build Attempt's open pull request, from this repository. */
|
|
60
|
+
export function newestBuildPull(pulls, { repo, issue }) {
|
|
61
|
+
const branch = new RegExp(`^factory/${Number(issue)}/attempt-([1-9][0-9]*)$`);
|
|
62
|
+
let best = null;
|
|
63
|
+
for (const pull of pulls ?? []) {
|
|
64
|
+
if (pull?.state !== "open") continue;
|
|
65
|
+
if (pull.head?.repo?.full_name !== repo) continue;
|
|
66
|
+
const match = branch.exec(String(pull.head?.ref ?? ""));
|
|
67
|
+
if (!match) continue;
|
|
68
|
+
const attempt = Number(match[1]);
|
|
69
|
+
if (!best || attempt > best.attempt) best = { attempt, pull };
|
|
70
|
+
}
|
|
71
|
+
return best?.pull ?? null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function runReviewTarget({ gh, env, args, out, err, appendOutput }) {
|
|
75
|
+
const c = reviewContext({ env, args, need: ["issue"] });
|
|
76
|
+
if (c.usage) {
|
|
77
|
+
err(`review-target: ${c.usage}`);
|
|
78
|
+
return 2;
|
|
79
|
+
}
|
|
80
|
+
const issue = Number(c.option("issue"));
|
|
81
|
+
if (!Number.isInteger(issue) || issue < 1) {
|
|
82
|
+
err("review-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 ?? "") || !STAGE_SHA.test(pull.base?.sha ?? "")) {
|
|
95
|
+
err(
|
|
96
|
+
"::error title=No pull request::The task has no open pull request on a build Attempt's branch to review.",
|
|
97
|
+
);
|
|
98
|
+
return 1;
|
|
99
|
+
}
|
|
100
|
+
const baseRef = String(pull.base?.ref ?? "");
|
|
101
|
+
if (!/^[A-Za-z0-9._/-]+$/.test(baseRef)) throw new Error("the base branch name is not plain");
|
|
102
|
+
appendOutput(`pr_number=${Number(pull.number)}`);
|
|
103
|
+
appendOutput(`head_sha=${pull.head.sha}`);
|
|
104
|
+
appendOutput(`base_sha=${pull.base.sha}`);
|
|
105
|
+
appendOutput(`base_ref=${baseRef}`);
|
|
106
|
+
out(`Reviewing pull request #${pull.number} at ${pull.head.sha.slice(0, 7)}.`);
|
|
107
|
+
return 0;
|
|
108
|
+
} catch (error) {
|
|
109
|
+
err(`::error title=Pull request not found::${String(error.message).replace(/\s+/g, " ")}`);
|
|
110
|
+
return 1;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function runReviewStart({ gh, env, args, out, err }) {
|
|
115
|
+
const c = reviewContext({ env, args, need: ["sha"] });
|
|
116
|
+
if (c.usage) {
|
|
117
|
+
err(`review-start: ${c.usage}`);
|
|
118
|
+
return 2;
|
|
119
|
+
}
|
|
120
|
+
const sha = c.option("sha");
|
|
121
|
+
if (!STAGE_SHA.test(sha)) {
|
|
122
|
+
err("review-start: --sha is not a commit sha");
|
|
123
|
+
return 2;
|
|
124
|
+
}
|
|
125
|
+
try {
|
|
126
|
+
writeStageCheck({
|
|
127
|
+
gh,
|
|
128
|
+
repo: c.repo,
|
|
129
|
+
sha,
|
|
130
|
+
check: reviewStageCheck({ started: true }),
|
|
131
|
+
targetUrl: reviewRunUrl(c.option, env, c.repo),
|
|
132
|
+
});
|
|
133
|
+
out("factory/review: pending");
|
|
134
|
+
return 0;
|
|
135
|
+
} catch (error) {
|
|
136
|
+
err(`::error title=Stage check not written::${String(error.message).replace(/\s+/g, " ")}`);
|
|
137
|
+
return 1;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function runReviewCheck({ gh, env, args, out, err, appendOutput, tracker }) {
|
|
142
|
+
const c = reviewContext({ env, args, need: ["pr", "sha", "run-id"] });
|
|
143
|
+
if (c.usage) {
|
|
144
|
+
err(`review-check: ${c.usage}`);
|
|
145
|
+
return 2;
|
|
146
|
+
}
|
|
147
|
+
const pr = Number(c.option("pr"));
|
|
148
|
+
const sha = c.option("sha");
|
|
149
|
+
const runId = c.option("run-id");
|
|
150
|
+
if (!Number.isInteger(pr) || pr < 1 || !STAGE_SHA.test(sha) || !/^[1-9][0-9]*$/.test(runId)) {
|
|
151
|
+
err("review-check: --pr, --sha or --run-id is malformed");
|
|
152
|
+
return 2;
|
|
153
|
+
}
|
|
154
|
+
const identity = env.AGENT_KIT_FACTORY_IDENTITY ?? WORKFLOW_IDENTITY;
|
|
155
|
+
try {
|
|
156
|
+
const comments = (tracker ?? createGithubTracker({ gh, repo: c.repo })).readComments(pr);
|
|
157
|
+
// The newest report of this run that the engine's identity wrote: anyone can
|
|
158
|
+
// type a header, so a comment of anyone else's is never read.
|
|
159
|
+
const report = comments
|
|
160
|
+
.filter(
|
|
161
|
+
(comment) =>
|
|
162
|
+
reviewBare(comment.author) === reviewBare(identity) &&
|
|
163
|
+
comment.body.startsWith(REVIEW_REPORT_TITLE) &&
|
|
164
|
+
comment.body.includes(`/actions/runs/${runId})`),
|
|
165
|
+
)
|
|
166
|
+
.at(-1);
|
|
167
|
+
const check = reviewStageCheck({ report: report?.body ?? null, head: sha });
|
|
168
|
+
writeStageCheck({
|
|
169
|
+
gh,
|
|
170
|
+
repo: c.repo,
|
|
171
|
+
sha,
|
|
172
|
+
check,
|
|
173
|
+
targetUrl: reviewRunUrl(c.option, env, c.repo),
|
|
174
|
+
});
|
|
175
|
+
appendOutput(`state=${check.state}`);
|
|
176
|
+
out(`factory/review: ${check.state} (${check.description})`);
|
|
177
|
+
return 0;
|
|
178
|
+
} catch (error) {
|
|
179
|
+
err(`::error title=Stage check not written::${String(error.message).replace(/\s+/g, " ")}`);
|
|
180
|
+
return 1;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// Keep-awake (spec #500, story 95): while the engine holds a Claim it asks the OS not to
|
|
2
|
+
// sleep, best effort and logged. macOS: `caffeinate -i -s` for as long as the hold lasts;
|
|
3
|
+
// Linux: `systemd-inhibit` around a `sleep infinity`. Elsewhere it says so once. A hold is
|
|
4
|
+
// a child process on the engine's allowlisted runner, ended by an abort signal, so the
|
|
5
|
+
// OS lets go the moment the last Claim is released or the engine exits.
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @param {{ runner: { start?: Function }, platform: string, log: (line: string) => void }} deps
|
|
9
|
+
* @returns {{ update: (running: number) => void, release: () => void }}
|
|
10
|
+
*/
|
|
11
|
+
export function createKeepAwake({ runner, platform, log }) {
|
|
12
|
+
let hold = null;
|
|
13
|
+
let said = false;
|
|
14
|
+
const note = (line) => {
|
|
15
|
+
if (said) return;
|
|
16
|
+
said = true;
|
|
17
|
+
log(line);
|
|
18
|
+
};
|
|
19
|
+
const begin = () => {
|
|
20
|
+
const [cmd, args] =
|
|
21
|
+
platform === "darwin"
|
|
22
|
+
? ["caffeinate", ["-i", "-s"]]
|
|
23
|
+
: platform === "linux"
|
|
24
|
+
? [
|
|
25
|
+
"systemd-inhibit",
|
|
26
|
+
[
|
|
27
|
+
"--what=idle:sleep",
|
|
28
|
+
"--who=agent-kit",
|
|
29
|
+
"--why=Factory Claim held",
|
|
30
|
+
"--mode=block",
|
|
31
|
+
"sleep",
|
|
32
|
+
"infinity",
|
|
33
|
+
],
|
|
34
|
+
]
|
|
35
|
+
: [null, null];
|
|
36
|
+
if (!cmd) {
|
|
37
|
+
note(`keep-awake: not available on ${platform}; the machine may sleep while a Claim is held`);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
const controller = new AbortController();
|
|
41
|
+
let started;
|
|
42
|
+
try {
|
|
43
|
+
started = runner.start(cmd, args, { signal: controller.signal });
|
|
44
|
+
} catch (error) {
|
|
45
|
+
note(`keep-awake: ${cmd} could not start (${error.message}); the machine may sleep`);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
hold = { controller };
|
|
49
|
+
Promise.resolve(started).then((result) => {
|
|
50
|
+
if (!result?.aborted && hold?.controller === controller) {
|
|
51
|
+
note(`keep-awake: ${cmd} ended early (exit ${result?.status}); the machine may sleep`);
|
|
52
|
+
hold = null;
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
log(`keep-awake: asked the OS to stay awake (${cmd})`);
|
|
56
|
+
};
|
|
57
|
+
const release = () => {
|
|
58
|
+
if (!hold) return;
|
|
59
|
+
hold.controller.abort();
|
|
60
|
+
hold = null;
|
|
61
|
+
log("keep-awake: let go");
|
|
62
|
+
};
|
|
63
|
+
return {
|
|
64
|
+
update(running) {
|
|
65
|
+
if (running > 0 && !hold) begin();
|
|
66
|
+
else if (running === 0) release();
|
|
67
|
+
},
|
|
68
|
+
release,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// Collecting a run folder (#523, spec #500 "Local storage"): at every teardown the engine
|
|
2
|
+
// moves a Stage run's working folder, `<state dir>/runs/<claim>/`, to
|
|
3
|
+
// `<state dir>/runs/<run id>/` (run id `orc-<claim>`), user-only (directories 0700, files
|
|
4
|
+
// 0600), and each Delegation's folder to its own `runs/<its run id>/`, so a Delegation's
|
|
5
|
+
// transcript sits under its own run id. Collected folders expire: the engine removes those
|
|
6
|
+
// older than `COLLECTED_DAYS` when it starts. A link is never followed or copied.
|
|
7
|
+
// Everything goes through the injected `fs`.
|
|
8
|
+
|
|
9
|
+
import { join } from "node:path";
|
|
10
|
+
|
|
11
|
+
export const COLLECTED_DAYS = 30;
|
|
12
|
+
const DAY_MS = 86_400_000;
|
|
13
|
+
|
|
14
|
+
function copyTree({ fs, from, to, skip = () => false }) {
|
|
15
|
+
fs.mkdirSync(to, { recursive: true, mode: 0o700 });
|
|
16
|
+
fs.chmodSync(to, 0o700);
|
|
17
|
+
for (const entry of fs.readdirSync(from, { withFileTypes: true })) {
|
|
18
|
+
const source = join(from, entry.name);
|
|
19
|
+
const target = join(to, entry.name);
|
|
20
|
+
if (entry.isSymbolicLink() || skip(entry.name, source)) continue;
|
|
21
|
+
if (entry.isDirectory()) copyTree({ fs, from: source, to: target });
|
|
22
|
+
else if (entry.isFile()) {
|
|
23
|
+
fs.writeFileSync(target, fs.readFileSync(source), { mode: 0o600 });
|
|
24
|
+
fs.chmodSync(target, 0o600);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Collects one Stage run: the folder under `runId`, each Delegation under its own run id.
|
|
31
|
+
* The working folder is removed once everything is copied. Returns what it collected.
|
|
32
|
+
*
|
|
33
|
+
* @param {{ fs: object, root: string, source: string, runId: string,
|
|
34
|
+
* delegations?: { runId: string, dir: string }[] }} deps `root` is the `runs` folder
|
|
35
|
+
* @returns {{ folder: string | null, delegations: string[] }}
|
|
36
|
+
*/
|
|
37
|
+
export function collectRunFolder({ fs, root, source, runId, delegations = [] }) {
|
|
38
|
+
const out = { folder: null, delegations: [] };
|
|
39
|
+
if (!fs.existsSync(source)) return out;
|
|
40
|
+
const dest = join(root, runId);
|
|
41
|
+
const delegationsDir = join(source, "delegations");
|
|
42
|
+
// A Delegation's folder is not part of the Stage's own: it moves under its own run id.
|
|
43
|
+
copyTree({ fs, from: source, to: dest, skip: (_name, path) => path === delegationsDir });
|
|
44
|
+
out.folder = dest;
|
|
45
|
+
for (const d of delegations) {
|
|
46
|
+
if (!fs.existsSync(d.dir)) continue;
|
|
47
|
+
copyTree({ fs, from: d.dir, to: join(root, d.runId) });
|
|
48
|
+
out.delegations.push(join(root, d.runId));
|
|
49
|
+
}
|
|
50
|
+
fs.rmSync(source, { recursive: true, force: true });
|
|
51
|
+
return out;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The working folders a dead run left: every entry of `runs/` that is not a collected `orc-` one. */
|
|
55
|
+
export function leftoverFolders({ fs, root }) {
|
|
56
|
+
if (!fs.existsSync(root)) return [];
|
|
57
|
+
return fs
|
|
58
|
+
.readdirSync(root, { withFileTypes: true })
|
|
59
|
+
.filter((e) => e.isDirectory() && !e.isSymbolicLink() && !e.name.startsWith("orc-"))
|
|
60
|
+
.map((e) => e.name)
|
|
61
|
+
.sort();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Removes collected folders (`orc-*`) not touched for `days`. Returns their names. */
|
|
65
|
+
export function pruneCollected({ fs, root, now, days = COLLECTED_DAYS }) {
|
|
66
|
+
if (!fs.existsSync(root)) return [];
|
|
67
|
+
const removed = [];
|
|
68
|
+
for (const entry of fs.readdirSync(root, { withFileTypes: true })) {
|
|
69
|
+
if (!entry.isDirectory() || entry.isSymbolicLink() || !entry.name.startsWith("orc-")) continue;
|
|
70
|
+
const path = join(root, entry.name);
|
|
71
|
+
if (now - fs.statSync(path).mtimeMs > days * DAY_MS) {
|
|
72
|
+
fs.rmSync(path, { recursive: true, force: true });
|
|
73
|
+
removed.push(entry.name);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return removed;
|
|
77
|
+
}
|