@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,226 @@
|
|
|
1
|
+
// The Run Record of one Workflow-engine Stage run (#517, ADR 0036): `run-record/2`,
|
|
2
|
+
// `run.kind: "stage"`. Pure functions of data the kit produced, never of the
|
|
3
|
+
// agent's transcript: the Caller's inputs, the Launcher's exit code and usage
|
|
4
|
+
// file, and the head's Stage check. The record has no cost field and no free text.
|
|
5
|
+
//
|
|
6
|
+
// The orchestrate skill's safe-field check (`checkRecord` in
|
|
7
|
+
// .claude/skills/orchestrate/records.mjs) is the authority on what may be kept;
|
|
8
|
+
// `checkStageRecord` below is this job's own copy, run before every write, and
|
|
9
|
+
// scripts/factory-record.test.mjs holds the two in step. The job installs the
|
|
10
|
+
// published CLI and cannot import the skill.
|
|
11
|
+
|
|
12
|
+
import { parseUsage } from "../claims/release.mjs";
|
|
13
|
+
|
|
14
|
+
export const RECORD_SCHEMA = "run-record/2";
|
|
15
|
+
/** The engine's identity in the Sink path: GITHUB_TOKEN's own login, the one that writes the Stage check. */
|
|
16
|
+
export const ENGINE_LOGIN = "github-actions";
|
|
17
|
+
const STATUS_CREATOR = "github-actions[bot]";
|
|
18
|
+
|
|
19
|
+
export const STAGES = Object.freeze(["plan", "build", "review", "qa", "security"]);
|
|
20
|
+
const CHECKS = Object.freeze(["pending", "success", "failure", "error"]);
|
|
21
|
+
const EXITS = Object.freeze([
|
|
22
|
+
"completed",
|
|
23
|
+
"turn-cap",
|
|
24
|
+
"wall-clock",
|
|
25
|
+
"token-cap",
|
|
26
|
+
"crashed",
|
|
27
|
+
"no-result",
|
|
28
|
+
]);
|
|
29
|
+
/** The Stage checks that exist: plan and build have none. */
|
|
30
|
+
const CHECK_CONTEXT = Object.freeze({
|
|
31
|
+
review: "factory/review",
|
|
32
|
+
qa: "factory/qa",
|
|
33
|
+
security: "factory/security",
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const REPO = /^github\.com\/[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/;
|
|
37
|
+
/** An Orchestrate-engine run id: the skill's id pattern, behind `orc-`. */
|
|
38
|
+
export const ORC_ID = /^orc-[A-Za-z0-9][A-Za-z0-9._:-]{0,58}$/;
|
|
39
|
+
const WHOLE = /^(?:0|[1-9][0-9]{0,15})$/;
|
|
40
|
+
const SHA = /^[0-9a-f]{40}$/;
|
|
41
|
+
const ISO = /^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\dZ$/;
|
|
42
|
+
|
|
43
|
+
const whole = (value, what, min = 0) => {
|
|
44
|
+
if (!WHOLE.test(String(value)) || Number(value) < min)
|
|
45
|
+
throw new Error(`not a whole number${min ? ` of ${min} or more` : ""}: ${what}`);
|
|
46
|
+
return Number(value);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/** What a run ended as, from the Launcher's exit code (a string; empty when it never ran) and its usage file text. */
|
|
50
|
+
export function exitOf({ stageCode, usageText }) {
|
|
51
|
+
const code = String(stageCode ?? "").trim();
|
|
52
|
+
if (code === "") return "no-result";
|
|
53
|
+
if (code === "0") return "completed";
|
|
54
|
+
if (code === "3") {
|
|
55
|
+
const stopped = usageText ? parseUsage(usageText)?.stopped : null;
|
|
56
|
+
if (stopped === "turns") return "turn-cap";
|
|
57
|
+
if (stopped === "minutes") return "wall-clock";
|
|
58
|
+
if (stopped === "tokens") return "token-cap";
|
|
59
|
+
}
|
|
60
|
+
return "crashed";
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* The record's path in the Sink. Throws on a repository, run id or attempt that
|
|
65
|
+
* is not plain, so nothing outside `records/github.com/<owner>/<repo>/` is named.
|
|
66
|
+
*/
|
|
67
|
+
export function recordPath({ repo, runId, runAttempt }) {
|
|
68
|
+
if (!REPO.test(String(repo))) throw new Error(`not a github.com/<owner>/<repo> name: ${repo}`);
|
|
69
|
+
whole(runId, "run id", 1);
|
|
70
|
+
whole(runAttempt, "run attempt", 1);
|
|
71
|
+
return `records/${repo.toLowerCase()}/${ENGINE_LOGIN}/gha-${runId}-${runAttempt}.json`;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* The Stage check on a head: the newest status of the Stage's context that the
|
|
76
|
+
* engine's own identity wrote (ADR 0035). `statuses` is the commit's status list,
|
|
77
|
+
* newest first. Null for a Stage with no check, no such status, or data that is
|
|
78
|
+
* not a list.
|
|
79
|
+
*/
|
|
80
|
+
export function stageCheckOf({ statuses, stage }) {
|
|
81
|
+
const context = CHECK_CONTEXT[stage];
|
|
82
|
+
if (!context || !Array.isArray(statuses)) return null;
|
|
83
|
+
const mine = statuses.find(
|
|
84
|
+
(s) =>
|
|
85
|
+
s?.context === context && s?.creator?.login === STATUS_CREATOR && CHECKS.includes(s?.state),
|
|
86
|
+
);
|
|
87
|
+
return mine ? mine.state : null;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* One Stage run's record. Every input is a string from the workflow (or empty
|
|
92
|
+
* for none) and is checked as plain data; a value that is not throws, so
|
|
93
|
+
* nothing malformed is ever written.
|
|
94
|
+
*/
|
|
95
|
+
export function buildStageRecord({
|
|
96
|
+
repo,
|
|
97
|
+
runId,
|
|
98
|
+
runAttempt,
|
|
99
|
+
stage,
|
|
100
|
+
task,
|
|
101
|
+
attempt,
|
|
102
|
+
headSha = "",
|
|
103
|
+
parentRunId = "",
|
|
104
|
+
stageCheck = "",
|
|
105
|
+
stageCode = "",
|
|
106
|
+
usageText = "",
|
|
107
|
+
now,
|
|
108
|
+
}) {
|
|
109
|
+
recordPath({ repo, runId, runAttempt });
|
|
110
|
+
if (!STAGES.includes(stage)) throw new Error(`not a Stage: ${stage}`);
|
|
111
|
+
if (headSha !== "" && !SHA.test(headSha)) throw new Error("head is not a commit sha");
|
|
112
|
+
if (stageCheck !== "" && !CHECKS.includes(stageCheck))
|
|
113
|
+
throw new Error("Stage check is not a known state");
|
|
114
|
+
if (parentRunId !== "") whole(parentRunId, "parent run id", 1);
|
|
115
|
+
const usage = usageText ? parseUsage(usageText) : null;
|
|
116
|
+
// QA has no agent yet: it ends as its check says, and carries no count.
|
|
117
|
+
const exit =
|
|
118
|
+
stage === "qa"
|
|
119
|
+
? stageCheck === "success" || stageCheck === "failure"
|
|
120
|
+
? "completed"
|
|
121
|
+
: "no-result"
|
|
122
|
+
: exitOf({ stageCode, usageText });
|
|
123
|
+
const record = {
|
|
124
|
+
schema: RECORD_SCHEMA,
|
|
125
|
+
run: {
|
|
126
|
+
id: `gha-${runId}-${runAttempt}`,
|
|
127
|
+
repo: repo.toLowerCase(),
|
|
128
|
+
kind: "stage",
|
|
129
|
+
engine: "workflow",
|
|
130
|
+
stage,
|
|
131
|
+
task: whole(task, "task", 1),
|
|
132
|
+
head_sha: headSha === "" ? null : headSha,
|
|
133
|
+
attempt: whole(attempt, "attempt", 1),
|
|
134
|
+
parent_run_id: parentRunId === "" ? null : `gha-${parentRunId}`,
|
|
135
|
+
stage_check: stageCheck === "" ? null : stageCheck,
|
|
136
|
+
recorded_at: (now instanceof Date ? now : new Date(now))
|
|
137
|
+
.toISOString()
|
|
138
|
+
.replace(/\.\d+Z$/, "Z"),
|
|
139
|
+
exit,
|
|
140
|
+
...(usage && stage !== "qa" ? { tokens: usage.tokens } : {}),
|
|
141
|
+
},
|
|
142
|
+
};
|
|
143
|
+
const problems = checkStageRecord(record);
|
|
144
|
+
if (problems.length) throw new Error(`not a safe Run Record: ${problems.join("; ")}`);
|
|
145
|
+
return record;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const RUN_KEYS = Object.freeze([
|
|
149
|
+
"id",
|
|
150
|
+
"repo",
|
|
151
|
+
"kind",
|
|
152
|
+
"engine",
|
|
153
|
+
"stage",
|
|
154
|
+
"task",
|
|
155
|
+
"head_sha",
|
|
156
|
+
"attempt",
|
|
157
|
+
"parent_run_id",
|
|
158
|
+
"stage_check",
|
|
159
|
+
"recorded_at",
|
|
160
|
+
"exit",
|
|
161
|
+
"tokens",
|
|
162
|
+
"model_requests",
|
|
163
|
+
"denials",
|
|
164
|
+
"duration_seconds",
|
|
165
|
+
"raw_upload",
|
|
166
|
+
]);
|
|
167
|
+
const OPTIONAL = Object.freeze([
|
|
168
|
+
"tokens",
|
|
169
|
+
"model_requests",
|
|
170
|
+
"denials",
|
|
171
|
+
"duration_seconds",
|
|
172
|
+
"raw_upload",
|
|
173
|
+
]);
|
|
174
|
+
const REQUIRED = RUN_KEYS.filter((key) => !OPTIONAL.includes(key));
|
|
175
|
+
|
|
176
|
+
/** This job's copy of the safe-field check for a Stage record: a closed shape of ids, numbers and enums. */
|
|
177
|
+
export function checkStageRecord(record) {
|
|
178
|
+
const problems = [];
|
|
179
|
+
const isObject = (v) => v !== null && typeof v === "object" && !Array.isArray(v);
|
|
180
|
+
if (!isObject(record)) return ["record: must be an object"];
|
|
181
|
+
for (const key of Object.keys(record))
|
|
182
|
+
if (!["schema", "run"].includes(key)) problems.push(`${key}: is outside the schema`);
|
|
183
|
+
if (record.schema !== RECORD_SCHEMA) problems.push(`schema: must be ${RECORD_SCHEMA}`);
|
|
184
|
+
const run = record.run;
|
|
185
|
+
if (!isObject(run)) return [...problems, "run: must be an object"];
|
|
186
|
+
for (const key of Object.keys(run))
|
|
187
|
+
if (!RUN_KEYS.includes(key)) problems.push(`run.${key}: is outside the schema`);
|
|
188
|
+
for (const key of REQUIRED) if (!Object.hasOwn(run, key)) problems.push(`run.${key}: is missing`);
|
|
189
|
+
const bad = (key, ok) => {
|
|
190
|
+
if (Object.hasOwn(run, key) && !ok) problems.push(`run.${key}: is not valid`);
|
|
191
|
+
};
|
|
192
|
+
const int = (v) => Number.isSafeInteger(v) && v >= 0;
|
|
193
|
+
// A Workflow-engine id is `gha-<run>-<attempt>`; an Orchestrate-engine id is `orc-<Claim nonce>`
|
|
194
|
+
// (and `orc-<nonce>-<delegation>` for a Delegation's run). The id says which engine wrote it.
|
|
195
|
+
bad(
|
|
196
|
+
"id",
|
|
197
|
+
typeof run.id === "string" &&
|
|
198
|
+
(run.engine === "orchestrate" ? ORC_ID : /^gha-[1-9][0-9]*-[1-9][0-9]*$/).test(run.id),
|
|
199
|
+
);
|
|
200
|
+
bad(
|
|
201
|
+
"repo",
|
|
202
|
+
typeof run.repo === "string" && REPO.test(run.repo) && run.repo === run.repo.toLowerCase(),
|
|
203
|
+
);
|
|
204
|
+
bad("kind", run.kind === "stage");
|
|
205
|
+
bad("engine", run.engine === "workflow" || run.engine === "orchestrate");
|
|
206
|
+
bad("stage", STAGES.includes(run.stage));
|
|
207
|
+
bad("task", int(run.task) && run.task >= 1);
|
|
208
|
+
bad(
|
|
209
|
+
"head_sha",
|
|
210
|
+
run.head_sha === null || (typeof run.head_sha === "string" && SHA.test(run.head_sha)),
|
|
211
|
+
);
|
|
212
|
+
bad("attempt", int(run.attempt) && run.attempt >= 1);
|
|
213
|
+
bad(
|
|
214
|
+
"parent_run_id",
|
|
215
|
+
run.parent_run_id === null ||
|
|
216
|
+
(typeof run.parent_run_id === "string" &&
|
|
217
|
+
(run.engine === "orchestrate" ? ORC_ID : /^gha-[1-9][0-9]*$/).test(run.parent_run_id)),
|
|
218
|
+
);
|
|
219
|
+
bad("raw_upload", typeof run.raw_upload === "boolean");
|
|
220
|
+
bad("stage_check", run.stage_check === null || CHECKS.includes(run.stage_check));
|
|
221
|
+
bad("recorded_at", typeof run.recorded_at === "string" && ISO.test(run.recorded_at));
|
|
222
|
+
bad("exit", EXITS.includes(run.exit));
|
|
223
|
+
for (const key of ["tokens", "model_requests", "denials", "duration_seconds"])
|
|
224
|
+
bad(key, int(run[key]));
|
|
225
|
+
return problems;
|
|
226
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// The Sink write of a Stage's Run Record (#517): one create-only file in a
|
|
2
|
+
// private GitHub repository, with the Caller's secret, over the contents API.
|
|
3
|
+
// `fetch` is injected, so the tests never reach a network. The token goes only
|
|
4
|
+
// in the Authorization header: no result, error or log line ever holds it.
|
|
5
|
+
|
|
6
|
+
const API = "https://api.github.com";
|
|
7
|
+
const SINK_REPO = /^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/;
|
|
8
|
+
|
|
9
|
+
const headers = (token) => ({
|
|
10
|
+
authorization: `Bearer ${token}`,
|
|
11
|
+
accept: "application/vnd.github+json",
|
|
12
|
+
"x-github-api-version": "2022-11-28",
|
|
13
|
+
"user-agent": "agent-kit-factory-record",
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Write `text` at `path` in the sink repo, never over anything. A sink that is
|
|
18
|
+
* not private gets nothing (there is no override). Identical bytes already there
|
|
19
|
+
* count as written; different bytes stop the write.
|
|
20
|
+
*
|
|
21
|
+
* @returns {Promise<{status: "created" | "already" | "different" | "not-private" | "error", detail?: string}>}
|
|
22
|
+
*/
|
|
23
|
+
export async function writeRecord({ sink, path, text, fetch = globalThis.fetch }) {
|
|
24
|
+
if (!SINK_REPO.test(String(sink?.repo ?? "")))
|
|
25
|
+
return { status: "error", detail: "the sink is not an owner/repo name" };
|
|
26
|
+
const base = `${API}/repos/${sink.repo}`;
|
|
27
|
+
const init = { headers: headers(sink.token) };
|
|
28
|
+
try {
|
|
29
|
+
const repo = await fetch(base, init);
|
|
30
|
+
if (!repo.ok) return { status: "error", detail: `the sink could not be read (${repo.status})` };
|
|
31
|
+
if ((await repo.json()).private !== true) return { status: "not-private" };
|
|
32
|
+
|
|
33
|
+
const url = `${base}/contents/${path.split("/").map(encodeURIComponent).join("/")}`;
|
|
34
|
+
const there = await fetch(url, init);
|
|
35
|
+
if (there.ok) {
|
|
36
|
+
const file = await there.json();
|
|
37
|
+
const old = Buffer.from(String(file.content ?? ""), "base64").toString("utf8");
|
|
38
|
+
return { status: old === text ? "already" : "different" };
|
|
39
|
+
}
|
|
40
|
+
if (there.status !== 404)
|
|
41
|
+
return { status: "error", detail: `the sink path could not be read (${there.status})` };
|
|
42
|
+
|
|
43
|
+
const made = await fetch(url, {
|
|
44
|
+
method: "PUT",
|
|
45
|
+
headers: { ...headers(sink.token), "content-type": "application/json" },
|
|
46
|
+
body: JSON.stringify({
|
|
47
|
+
message: `record: ${path.split("/").at(-1)}`,
|
|
48
|
+
content: Buffer.from(text, "utf8").toString("base64"),
|
|
49
|
+
}),
|
|
50
|
+
});
|
|
51
|
+
return made.ok
|
|
52
|
+
? { status: "created" }
|
|
53
|
+
: { status: "error", detail: `the write was refused (${made.status})` };
|
|
54
|
+
} catch (error) {
|
|
55
|
+
return { status: "error", detail: `the sink was not reachable (${error?.name ?? "error"})` };
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
// The whole report as one plain object (`factory-report/1`), from what the reads
|
|
2
|
+
// found. Pure: the tracker's evidence, the identity of the engine and the clock
|
|
3
|
+
// in, the report out, so a suite drives it over fixtures and `--json` prints it.
|
|
4
|
+
|
|
5
|
+
import { planApprovalFromConfig, UNREADABLE } from "../decision/plan.mjs";
|
|
6
|
+
import { WORKFLOW_ENGINE_NAME } from "../engine/workflow-engine.mjs";
|
|
7
|
+
import { mergeFromConfig } from "../gate/config.mjs";
|
|
8
|
+
import { DEFAULT_STAGE_LIMITS, stageLimits } from "../limits/limits.mjs";
|
|
9
|
+
import { bare, DAY, gateDecisions, isoOf, taskFacts } from "./facts.mjs";
|
|
10
|
+
import { gateSection } from "./gate-section.mjs";
|
|
11
|
+
import { computeMetrics } from "./metrics.mjs";
|
|
12
|
+
import { rolloutOf } from "./rollout.mjs";
|
|
13
|
+
|
|
14
|
+
export const REPORT_SCHEMA = "factory-report/1";
|
|
15
|
+
|
|
16
|
+
/** The tokens of a Stage's cap: the configured limit, else the starting one. */
|
|
17
|
+
function capOf(configText) {
|
|
18
|
+
return (stage, size) => {
|
|
19
|
+
const read = stageLimits({ configText, stage, size });
|
|
20
|
+
if (read.limits) return read.limits.tokens;
|
|
21
|
+
const fallback = DEFAULT_STAGE_LIMITS[stage];
|
|
22
|
+
return (stage === "build" ? fallback[size] : fallback).tokens;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @param {{ issues: object[], writers?: string[], configText?: string | null, configProblem?: string | null, killSwitch?: "true" | "false" | null, warnings?: string[] }} read
|
|
28
|
+
* @param {{ repo: string, identity: string, now: number, days: number, gate?: boolean }} options
|
|
29
|
+
*/
|
|
30
|
+
export function buildReport(read, { repo, identity, now, days, gate = false }) {
|
|
31
|
+
const since = now - days * DAY;
|
|
32
|
+
const ctx = {
|
|
33
|
+
isEngine: (login) => bare(login) === bare(identity),
|
|
34
|
+
writers: new Set((read.writers ?? []).map((w) => String(w).toLowerCase())),
|
|
35
|
+
engineName: WORKFLOW_ENGINE_NAME,
|
|
36
|
+
cap: capOf(read.configText ?? null),
|
|
37
|
+
};
|
|
38
|
+
const terminal = (read.issues ?? []).filter((i) => {
|
|
39
|
+
const closed = Date.parse(i.closedAt);
|
|
40
|
+
return i.state === "CLOSED" && closed >= since && closed <= now;
|
|
41
|
+
});
|
|
42
|
+
const tasks = terminal.map((i) => taskFacts(i, ctx));
|
|
43
|
+
const metrics = computeMetrics(tasks, now);
|
|
44
|
+
const gateRead = gateSection(
|
|
45
|
+
terminal.flatMap((i) => gateDecisions(i, ctx)),
|
|
46
|
+
{ now },
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
const problems = [];
|
|
50
|
+
const text = read.configProblem ? UNREADABLE : (read.configText ?? null);
|
|
51
|
+
const approval = planApprovalFromConfig(text);
|
|
52
|
+
const merge = mergeFromConfig(text);
|
|
53
|
+
if (read.configProblem) problems.push("the factory config could not be read");
|
|
54
|
+
else {
|
|
55
|
+
if (approval.problem) problems.push(approval.problem);
|
|
56
|
+
if (merge.problem) problems.push(merge.problem);
|
|
57
|
+
}
|
|
58
|
+
const effective = {
|
|
59
|
+
kill_switch: read.killSwitch ?? null,
|
|
60
|
+
plan_approval: approval.value,
|
|
61
|
+
merge_mode: merge.value.mode,
|
|
62
|
+
auto_tiers: merge.value.auto_tiers,
|
|
63
|
+
};
|
|
64
|
+
const rollout = problems.length
|
|
65
|
+
? { phase: null, name: null, candidates: [], effective, next: null, problems }
|
|
66
|
+
: {
|
|
67
|
+
...rolloutOf({
|
|
68
|
+
effective: {
|
|
69
|
+
killSwitch: effective.kill_switch,
|
|
70
|
+
planApproval: effective.plan_approval,
|
|
71
|
+
mergeMode: effective.merge_mode,
|
|
72
|
+
autoTiers: effective.auto_tiers,
|
|
73
|
+
},
|
|
74
|
+
tasks,
|
|
75
|
+
gate: gateRead,
|
|
76
|
+
}),
|
|
77
|
+
problems,
|
|
78
|
+
};
|
|
79
|
+
// `effective` is spelled the same way in both branches.
|
|
80
|
+
rollout.effective = effective;
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
schema: REPORT_SCHEMA,
|
|
84
|
+
repo,
|
|
85
|
+
generated_at: isoOf(now),
|
|
86
|
+
window: { since: isoOf(since), until: isoOf(now), days },
|
|
87
|
+
...metrics,
|
|
88
|
+
rollout,
|
|
89
|
+
...(gate ? { gate: gateRead } : {}),
|
|
90
|
+
warnings: read.warnings ?? [],
|
|
91
|
+
};
|
|
92
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// `agent-kit factory report [--since 30d] [--json] [--gate]`: how the Factory is
|
|
2
|
+
// doing, from the tracker alone and the maintainer's own `gh` (ADR 0044). It
|
|
3
|
+
// reads and never posts: every `gh` call is a GET or a GraphQL query.
|
|
4
|
+
//
|
|
5
|
+
// Who the engine is: `github-actions[bot]` (the Workflow engine's identity), or
|
|
6
|
+
// AGENT_KIT_FACTORY_IDENTITY when the Factory runs as another login. This is not
|
|
7
|
+
// the maintainer's own login, which the report never needs.
|
|
8
|
+
|
|
9
|
+
import { WORKFLOW_IDENTITY } from "../engine/workflow-engine.mjs";
|
|
10
|
+
import { buildReport } from "./build.mjs";
|
|
11
|
+
import { DAY } from "./facts.mjs";
|
|
12
|
+
import { readTracker } from "./reads.mjs";
|
|
13
|
+
import { renderJson, renderText } from "./render.mjs";
|
|
14
|
+
|
|
15
|
+
const SINCE = /^([1-9][0-9]{0,3})d$/;
|
|
16
|
+
const REPO = /^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/;
|
|
17
|
+
|
|
18
|
+
/** `30d` is 30 days; anything else is not a window. */
|
|
19
|
+
export function parseSince(value) {
|
|
20
|
+
const m = SINCE.exec(String(value ?? "30d"));
|
|
21
|
+
return m ? Number(m[1]) : null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @param {{
|
|
26
|
+
* runner: (cmd: string, args: string[]) => { status: number | null, stdout: string, stderr: string },
|
|
27
|
+
* env: Record<string, string | undefined>,
|
|
28
|
+
* repo?: string, since?: string, json?: boolean, gate?: boolean,
|
|
29
|
+
* clock?: () => number,
|
|
30
|
+
* out: (line: string) => void, err: (line: string) => void,
|
|
31
|
+
* }} io
|
|
32
|
+
* @returns {number} 0 reported, 1 a read failed, 2 not runnable
|
|
33
|
+
*/
|
|
34
|
+
export function runFactoryReport({
|
|
35
|
+
runner,
|
|
36
|
+
env,
|
|
37
|
+
repo,
|
|
38
|
+
since,
|
|
39
|
+
json = false,
|
|
40
|
+
gate = false,
|
|
41
|
+
clock = Date.now,
|
|
42
|
+
out,
|
|
43
|
+
err,
|
|
44
|
+
}) {
|
|
45
|
+
const days = parseSince(since);
|
|
46
|
+
if (days === null) {
|
|
47
|
+
err(`factory report: --since is a number of days such as 30d, got ${JSON.stringify(since)}`);
|
|
48
|
+
return 2;
|
|
49
|
+
}
|
|
50
|
+
const gh = (args) => runner("gh", args);
|
|
51
|
+
let slug = repo ?? env.GITHUB_REPOSITORY;
|
|
52
|
+
if (!slug) {
|
|
53
|
+
const view = gh(["repo", "view", "--json", "nameWithOwner", "--jq", ".nameWithOwner"]);
|
|
54
|
+
if (view.status === 0) slug = view.stdout.trim();
|
|
55
|
+
}
|
|
56
|
+
if (!slug || !REPO.test(slug)) {
|
|
57
|
+
err(`factory report: no repository (pass --repo owner/name), got ${JSON.stringify(slug)}`);
|
|
58
|
+
return 2;
|
|
59
|
+
}
|
|
60
|
+
const identity = env.AGENT_KIT_FACTORY_IDENTITY || WORKFLOW_IDENTITY;
|
|
61
|
+
const now = clock();
|
|
62
|
+
let read;
|
|
63
|
+
try {
|
|
64
|
+
// With --json the one thing on stdout is the report, so progress goes nowhere.
|
|
65
|
+
read = readTracker({
|
|
66
|
+
gh,
|
|
67
|
+
repo: slug,
|
|
68
|
+
since: now - days * DAY,
|
|
69
|
+
env,
|
|
70
|
+
identity,
|
|
71
|
+
log: json ? () => {} : out,
|
|
72
|
+
});
|
|
73
|
+
} catch (error) {
|
|
74
|
+
err(`factory report: ${error.message}`);
|
|
75
|
+
return 1;
|
|
76
|
+
}
|
|
77
|
+
const report = buildReport(read, { repo: slug, identity, now, days, gate });
|
|
78
|
+
out(json ? renderJson(report) : renderText(report));
|
|
79
|
+
return 0;
|
|
80
|
+
}
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
// What `factory report` knows of one task, from the tracker's evidence alone
|
|
2
|
+
// (ADR 0044). Pure: a task is one issue, as the reads hand it over, and a context
|
|
3
|
+
// (who the engine is, who may write, what a Stage's cap is, the clock), and the
|
|
4
|
+
// answer is a plain object of facts. A restart is the same issue, so it is the
|
|
5
|
+
// same task: nothing here counts queued periods.
|
|
6
|
+
//
|
|
7
|
+
// An issue, as the report reads it (a superset of the sweep's read, so a sweep
|
|
8
|
+
// fixture's issue is a valid, if bare, one):
|
|
9
|
+
//
|
|
10
|
+
// { number, state, closedAt, labels: [name],
|
|
11
|
+
// comments: [{ id, author, body, createdAt, editedAt? }],
|
|
12
|
+
// events: [{ type: "labeled" | "unlabeled", label, actor, createdAt }],
|
|
13
|
+
// refs: [reference], // things that mention this issue
|
|
14
|
+
// pulls: [{ number, state, merged, mergedAt, mergedBy, branch, head,
|
|
15
|
+
// comments: [...], statuses: [{ head, context, state, creator }],
|
|
16
|
+
// refs: [reference] }] }
|
|
17
|
+
// reference = { kind: "pull" | "issue", number, title, body, merged, mergedAt, createdAt, labels }
|
|
18
|
+
|
|
19
|
+
import { parseReleaseComment } from "../claims/release.mjs";
|
|
20
|
+
import { checkPlanHeader } from "../checks/plan-header.mjs";
|
|
21
|
+
import { parseGateComment } from "../gate/comment.mjs";
|
|
22
|
+
import { latestPlan } from "../plan/comment.mjs";
|
|
23
|
+
import { parseClaim } from "../sweep/markers.mjs";
|
|
24
|
+
|
|
25
|
+
export const TIERS = Object.freeze(["low", "medium", "high"]);
|
|
26
|
+
/** A task that never had a valid plan has no Risk tier. */
|
|
27
|
+
export const UNPLANNED = "unplanned";
|
|
28
|
+
export const REVERT_DAYS = 7;
|
|
29
|
+
export const DAY = 86_400_000;
|
|
30
|
+
/** An ISO time without fractions, from milliseconds. */
|
|
31
|
+
export const isoOf = (t) => new Date(t).toISOString().replace(/\.\d+Z$/, "Z");
|
|
32
|
+
const STAGE_CHECKS = Object.freeze(["factory/review", "factory/qa", "factory/security"]);
|
|
33
|
+
|
|
34
|
+
/** A login without its `[bot]` suffix, lower case: how the sweep compares authors. */
|
|
35
|
+
export const bare = (login) =>
|
|
36
|
+
String(login ?? "")
|
|
37
|
+
.toLowerCase()
|
|
38
|
+
.replace(/\[bot\]$/, "");
|
|
39
|
+
|
|
40
|
+
const ms = (iso) => (iso ? Date.parse(iso) : Number.NaN);
|
|
41
|
+
const higher = (a, b) => (TIERS.indexOf(b) > TIERS.indexOf(a) ? b : a);
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The engine's gate comment on a pull request, read as data: the newest comment of
|
|
45
|
+
* the engine's that holds a readable marker.
|
|
46
|
+
* @returns {{ head: string, tier: string | null, kind: string, tierOnly: string | null, mergedAt: string | null, createdAt: string } | null}
|
|
47
|
+
*/
|
|
48
|
+
export function gateOf(pull, isEngine) {
|
|
49
|
+
const mine = (pull.comments ?? []).filter((c) => isEngine(c.author) && parseGateComment(c.body));
|
|
50
|
+
const comment = mine.at(-1);
|
|
51
|
+
if (!comment) return null;
|
|
52
|
+
const marker = parseGateComment(comment.body);
|
|
53
|
+
const body = String(comment.body);
|
|
54
|
+
const tier = /^Risk tier: (low|medium|high)\b/m.exec(body)?.[1] ?? null;
|
|
55
|
+
const merged = /\bmerged at `([0-9a-f]{7,40})`/.exec(body)?.[1] ?? null;
|
|
56
|
+
const tierOnly = /^Held: tier (low|medium|high) only \(would merge at `[0-9a-f]{40}`\)/m.exec(
|
|
57
|
+
body,
|
|
58
|
+
)?.[1];
|
|
59
|
+
let kind = "hold";
|
|
60
|
+
if (merged) kind = "merged";
|
|
61
|
+
else if (tierOnly) kind = "tier-only-hold";
|
|
62
|
+
else if (/^Would merge at `[0-9a-f]{40}`/m.test(body)) kind = "would-merge";
|
|
63
|
+
return {
|
|
64
|
+
head: marker.head,
|
|
65
|
+
tier,
|
|
66
|
+
kind,
|
|
67
|
+
tierOnly: tierOnly ?? null,
|
|
68
|
+
mergedAt: merged,
|
|
69
|
+
createdAt: comment.createdAt,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** `gate` when the engine's gate comment says it merged this pull request's head, else `person`. */
|
|
74
|
+
function mergePathOf(pull, gate) {
|
|
75
|
+
const sha = gate?.kind === "merged" ? gate.mergedAt : null;
|
|
76
|
+
return sha && String(pull.head ?? "").startsWith(sha) ? "gate" : "person";
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** The pull request that merged the task, newest merge first, or null. */
|
|
80
|
+
function mergedPull(issue) {
|
|
81
|
+
return (
|
|
82
|
+
(issue.pulls ?? [])
|
|
83
|
+
.filter((p) => p.merged === true || p.state === "MERGED")
|
|
84
|
+
.sort((a, b) => ms(b.mergedAt) - ms(a.mergedAt))[0] ?? null
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** The first time a revert or a regression names this merge, within 7 days of it, or null. */
|
|
89
|
+
function revertOf(issue, pull) {
|
|
90
|
+
const merged = ms(pull.mergedAt);
|
|
91
|
+
if (Number.isNaN(merged)) return null;
|
|
92
|
+
const within = (t) => t >= merged && t <= merged + REVERT_DAYS * DAY;
|
|
93
|
+
const times = [];
|
|
94
|
+
for (const ref of [...(pull.refs ?? []), ...(issue.refs ?? [])]) {
|
|
95
|
+
if (ref.kind === "pull") {
|
|
96
|
+
const revert =
|
|
97
|
+
/^revert\b/i.test(String(ref.title ?? "")) || /^Reverts\s/m.test(String(ref.body ?? ""));
|
|
98
|
+
const at = ms(ref.mergedAt);
|
|
99
|
+
if (revert && ref.merged === true && within(at)) times.push(at);
|
|
100
|
+
} else if (ref.kind === "issue" && (ref.labels ?? []).includes("regression")) {
|
|
101
|
+
const at = ms(ref.createdAt);
|
|
102
|
+
if (within(at)) times.push(at);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return times.length ? Math.min(...times) : null;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* The Stage runs of a task and their tokens: one run per Claim of the engine's
|
|
110
|
+
* (a lost race ran nothing), its tokens from the engine's unedited release comment
|
|
111
|
+
* for that Claim. A run with no release, an edited one or `tokens=unknown` is
|
|
112
|
+
* charged at the Stage's cap, as the Daily cap does (ADR 0042).
|
|
113
|
+
*/
|
|
114
|
+
function runsOf(issue, ctx, size) {
|
|
115
|
+
const comments = issue.comments ?? [];
|
|
116
|
+
const claims = comments
|
|
117
|
+
.filter((c) => ctx.isEngine(c.author))
|
|
118
|
+
.map((c) => ({ comment: c, claim: parseClaim(c.body) }))
|
|
119
|
+
.filter((x) => x.claim && x.claim.engine === ctx.engineName && x.claim.released !== "lost-race")
|
|
120
|
+
.map((x) => x.claim);
|
|
121
|
+
const releases = new Map();
|
|
122
|
+
for (const c of comments) {
|
|
123
|
+
if (!ctx.isEngine(c.author)) continue;
|
|
124
|
+
const release = parseReleaseComment(c.body);
|
|
125
|
+
if (release && !releases.has(release.claim)) releases.set(release.claim, { release, c });
|
|
126
|
+
}
|
|
127
|
+
let total = 0;
|
|
128
|
+
let missing = 0;
|
|
129
|
+
let edited = 0;
|
|
130
|
+
for (const claim of claims) {
|
|
131
|
+
const found = releases.get(claim.claim);
|
|
132
|
+
if (found && !found.c.editedAt && found.release.stage === claim.stage) {
|
|
133
|
+
if (found.release.tokens !== "unknown") {
|
|
134
|
+
total += found.release.tokens;
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
missing += 1;
|
|
138
|
+
} else if (found?.c.editedAt) edited += 1;
|
|
139
|
+
else missing += 1;
|
|
140
|
+
total += ctx.cap(claim.stage, size);
|
|
141
|
+
}
|
|
142
|
+
return {
|
|
143
|
+
builds: claims.filter((c) => c.stage === "build").length,
|
|
144
|
+
runs: claims.length,
|
|
145
|
+
tokens: total,
|
|
146
|
+
missing,
|
|
147
|
+
edited,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* The facts of one terminal task.
|
|
153
|
+
* @param {object} issue
|
|
154
|
+
* @param {{ isEngine: (login: string) => boolean, writers: Set<string>, engineName: string, cap: (stage: string, size: string) => number }} ctx
|
|
155
|
+
*/
|
|
156
|
+
export function taskFacts(issue, ctx) {
|
|
157
|
+
const pull = mergedPull(issue);
|
|
158
|
+
const plan = latestPlan(issue.comments ?? [], ctx.isEngine);
|
|
159
|
+
const header = plan ? checkPlanHeader(plan.data, { trusted: true }) : null;
|
|
160
|
+
const planned = header?.ok ? header.header : null;
|
|
161
|
+
|
|
162
|
+
const gates = (issue.pulls ?? []).map((p) => ({ p, gate: gateOf(p, ctx.isEngine) }));
|
|
163
|
+
const gate = (pull ? gates.find((g) => g.p === pull)?.gate : null) ?? null;
|
|
164
|
+
|
|
165
|
+
// The tier is the highest of the plan's, a person's `risk:<tier>` label and the gate's.
|
|
166
|
+
let tier = planned ? planned.risk : null;
|
|
167
|
+
for (const label of issue.labels ?? []) {
|
|
168
|
+
const m = /^risk:(low|medium|high)$/.exec(label);
|
|
169
|
+
if (m && tier) tier = higher(tier, m[1]);
|
|
170
|
+
}
|
|
171
|
+
if (gate?.tier) tier = tier ? higher(tier, gate.tier) : gate.tier;
|
|
172
|
+
|
|
173
|
+
// A label move counts when the engine or someone with write access made it.
|
|
174
|
+
const trusted = (e) => ctx.isEngine(e.actor) || ctx.writers.has(String(e.actor).toLowerCase());
|
|
175
|
+
const events = (issue.events ?? []).filter((e) => e.type === "labeled" && trusted(e));
|
|
176
|
+
const first = (label) =>
|
|
177
|
+
events
|
|
178
|
+
.filter((e) => e.label === label)
|
|
179
|
+
.map((e) => ms(e.createdAt))
|
|
180
|
+
.sort((a, b) => a - b)[0] ?? null;
|
|
181
|
+
|
|
182
|
+
const runs = runsOf(issue, ctx, planned?.size ?? "l");
|
|
183
|
+
const failedCheck = (issue.pulls ?? []).some((p) =>
|
|
184
|
+
(p.statuses ?? []).some(
|
|
185
|
+
(s) => STAGE_CHECKS.includes(s.context) && s.state === "failure" && ctx.isEngine(s.creator),
|
|
186
|
+
),
|
|
187
|
+
);
|
|
188
|
+
const revertAt = pull ? revertOf(issue, pull) : null;
|
|
189
|
+
return {
|
|
190
|
+
number: issue.number,
|
|
191
|
+
closedAt: ms(issue.closedAt),
|
|
192
|
+
tier: tier ?? UNPLANNED,
|
|
193
|
+
merged: Boolean(pull),
|
|
194
|
+
mergedAt: pull ? ms(pull.mergedAt) : null,
|
|
195
|
+
mergePath: pull ? mergePathOf(pull, gate) : null,
|
|
196
|
+
queuedAt: first("factory:queued"),
|
|
197
|
+
escalated: first("factory:needs-decision") !== null,
|
|
198
|
+
firstPass: runs.builds === 1 && !failedCheck,
|
|
199
|
+
builds: runs.builds,
|
|
200
|
+
failedCheck,
|
|
201
|
+
revertedAt: revertAt,
|
|
202
|
+
reverted: revertAt !== null,
|
|
203
|
+
tokens: runs.tokens,
|
|
204
|
+
stageRuns: runs.runs,
|
|
205
|
+
tokensMissing: runs.missing,
|
|
206
|
+
tokensEdited: runs.edited,
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** The gate's view of every pull request of a task, for the gate section. */
|
|
211
|
+
export function gateDecisions(issue, ctx) {
|
|
212
|
+
return (issue.pulls ?? []).map((p) => {
|
|
213
|
+
const gate = gateOf(p, ctx.isEngine);
|
|
214
|
+
const merged = p.merged === true || p.state === "MERGED";
|
|
215
|
+
return {
|
|
216
|
+
task: issue.number,
|
|
217
|
+
pull: p.number,
|
|
218
|
+
gate,
|
|
219
|
+
state: merged ? "merged" : p.state === "OPEN" ? "open" : "closed",
|
|
220
|
+
mergedAt: merged ? ms(p.mergedAt) : null,
|
|
221
|
+
head: p.head ?? null,
|
|
222
|
+
mergePath: merged ? mergePathOf(p, gate) : null,
|
|
223
|
+
revertedAt: merged ? revertOf(issue, p) : null,
|
|
224
|
+
};
|
|
225
|
+
});
|
|
226
|
+
}
|