@mnaimfaizy/agent-kit 1.0.0-beta.15 → 1.0.0-beta.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -1
- package/package.json +1 -1
- package/src/cli.mjs +332 -5
- package/src/deep-scan/docker-driver.mjs +70 -3
- package/src/deep-scan/lifecycle.mjs +2 -2
- package/src/deep-scan/sbx-driver.mjs +34 -47
- package/src/doctor.mjs +53 -1
- package/src/exec.mjs +18 -5
- package/src/factory/budget/budget.mjs +260 -0
- package/src/factory/build/command.mjs +321 -0
- package/src/factory/build/factory.mjs +473 -0
- package/src/factory/build/findings.mjs +55 -0
- package/src/factory/build/host.mjs +230 -0
- package/src/factory/build/notes.mjs +191 -0
- package/src/factory/build/pull-request.mjs +74 -0
- package/src/factory/bundle.mjs +146 -0
- package/src/factory/checks/allowed-paths.mjs +50 -0
- package/src/factory/checks/plan-header.mjs +296 -0
- package/src/factory/checks/qa.mjs +85 -0
- package/src/factory/checks/review.mjs +73 -0
- package/src/factory/checks/security.mjs +50 -0
- package/src/factory/checks/stage-check.mjs +89 -0
- package/src/factory/claims/command.mjs +187 -0
- package/src/factory/claims/release.mjs +111 -0
- package/src/factory/decision/answers.mjs +103 -0
- package/src/factory/decision/command.mjs +64 -0
- package/src/factory/decision/plan.mjs +99 -0
- package/src/factory/decision/request.mjs +116 -0
- package/src/factory/doctor/files.mjs +349 -0
- package/src/factory/doctor/index.mjs +56 -0
- package/src/factory/doctor/local.mjs +513 -0
- package/src/factory/doctor/orchestrate.mjs +146 -0
- package/src/factory/engine/lease.mjs +146 -0
- package/src/factory/engine/local-engine.mjs +177 -0
- package/src/factory/engine/lockfile.mjs +64 -0
- package/src/factory/engine/workflow-engine.mjs +124 -0
- package/src/factory/gate/comment.mjs +88 -0
- package/src/factory/gate/config.mjs +57 -0
- package/src/factory/gate/gate-core.mjs +250 -0
- package/src/factory/gate/guarded.mjs +17 -0
- package/src/factory/gate/reads.mjs +105 -0
- package/src/factory/gate/task.mjs +193 -0
- package/src/factory/gate/trip-reads.mjs +82 -0
- package/src/factory/gate/trip.mjs +87 -0
- package/src/factory/init/blueprint.mjs +181 -0
- package/src/factory/init/index.mjs +338 -0
- package/src/factory/labels/evidence.mjs +162 -0
- package/src/factory/labels/moves.mjs +32 -0
- package/src/factory/labels/reconcile.mjs +267 -0
- package/src/factory/labels/revert.mjs +87 -0
- package/src/factory/labels/timeline.mjs +72 -0
- package/src/factory/labels/voided.mjs +59 -0
- package/src/factory/limits/harness.mjs +39 -0
- package/src/factory/limits/limits.mjs +131 -0
- package/src/factory/merge/command.mjs +172 -0
- package/src/factory/outbox/check.mjs +240 -0
- package/src/factory/outbox/rulings.mjs +195 -0
- package/src/factory/plan/command.mjs +66 -0
- package/src/factory/plan/comment.mjs +156 -0
- package/src/factory/plan/publish.mjs +149 -0
- package/src/factory/plan/risk.mjs +153 -0
- package/src/factory/qa/command.mjs +301 -0
- package/src/factory/qa/decide.mjs +86 -0
- package/src/factory/qa/report.mjs +50 -0
- package/src/factory/records/engine-record.mjs +133 -0
- package/src/factory/records/main.mjs +96 -0
- package/src/factory/records/record.mjs +226 -0
- package/src/factory/records/sink.mjs +57 -0
- package/src/factory/report/build.mjs +92 -0
- package/src/factory/report/command.mjs +80 -0
- package/src/factory/report/facts.mjs +226 -0
- package/src/factory/report/gate-section.mjs +122 -0
- package/src/factory/report/metrics.mjs +111 -0
- package/src/factory/report/reads.mjs +246 -0
- package/src/factory/report/render.mjs +121 -0
- package/src/factory/report/rollout.mjs +154 -0
- package/src/factory/review/brief.mjs +195 -0
- package/src/factory/review/command.mjs +182 -0
- package/src/factory/run/awake.mjs +70 -0
- package/src/factory/run/collect.mjs +77 -0
- package/src/factory/run/command.mjs +556 -0
- package/src/factory/run/gh.mjs +46 -0
- package/src/factory/run/launch.mjs +66 -0
- package/src/factory/run/pipelines.mjs +694 -0
- package/src/factory/run/raw.mjs +96 -0
- package/src/factory/run/restart.mjs +83 -0
- package/src/factory/run/s3.mjs +193 -0
- package/src/factory/run/secrets.mjs +142 -0
- package/src/factory/run/teardown.mjs +242 -0
- package/src/factory/run/token.mjs +70 -0
- package/src/factory/run/worktree.mjs +160 -0
- package/src/factory/security/audit.mjs +353 -0
- package/src/factory/security/command.mjs +157 -0
- package/src/factory/setup/app.mjs +122 -0
- package/src/factory/setup/command.mjs +472 -0
- package/src/factory/setup/labels.mjs +25 -0
- package/src/factory/setup/reads.mjs +159 -0
- package/src/factory/setup/writes.mjs +138 -0
- package/src/factory/snapshot/write.mjs +176 -0
- package/src/factory/stop/command.mjs +136 -0
- package/src/factory/sweep/actions-main.mjs +119 -0
- package/src/factory/sweep/command.mjs +65 -0
- package/src/factory/sweep/knobs.mjs +78 -0
- package/src/factory/sweep/marker-version.mjs +15 -0
- package/src/factory/sweep/markers.mjs +112 -0
- package/src/factory/sweep/overlap.mjs +24 -0
- package/src/factory/sweep/run.mjs +198 -0
- package/src/factory/sweep/shell.mjs +398 -0
- package/src/factory/sweep/split.mjs +104 -0
- package/src/factory/sweep/states.mjs +87 -0
- package/src/factory/sweep/sweep-core.mjs +2009 -0
- package/src/factory/tracker/github.mjs +713 -0
- package/src/factory/tracker/operations.mjs +153 -0
- package/src/factory/upgrade/index.mjs +83 -0
- package/src/init.mjs +62 -24
- package/src/kit-owned-paths.mjs +1 -1
- package/src/stage/build.mjs +100 -0
- package/src/stage/factory-live.mjs +288 -0
- package/src/stage.mjs +100 -4
- package/src/upgrade.mjs +39 -7
|
@@ -0,0 +1,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
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// The shadow-to-on bar, per Risk tier, from the merge gate's own comments (spec
|
|
2
|
+
// #500, Merge gate specifics): at least 20 "would merge" decisions on the tier
|
|
3
|
+
// over at least 14 days, each merged by a person unchanged, none reverted or
|
|
4
|
+
// fixed within 7 days. Pure: the gate's decisions in, the bar out.
|
|
5
|
+
//
|
|
6
|
+
// A pull request carries one gate comment, edited in place, so its text is the
|
|
7
|
+
// gate's last word. For a tier the decisions are those pull requests whose last
|
|
8
|
+
// comment says "Would merge at <sha>" (the tier is in `auto_tiers`) or "Held: tier
|
|
9
|
+
// <t> only (would merge at <sha>)" (it is not): the second is how a tier builds its
|
|
10
|
+
// record while the gate is `on` for the one below it. A decision is **confirmed**
|
|
11
|
+
// when a person merged the pull request at that very head. One that a person
|
|
12
|
+
// closed, or merged at another head, is **diverged**. One still open is pending.
|
|
13
|
+
//
|
|
14
|
+
// Counting starts at the latest **trip**: a revert, or a `regression` issue, within
|
|
15
|
+
// 7 days of a merge the gate made. Decisions confirmed before it do not count.
|
|
16
|
+
|
|
17
|
+
import { DAY, gateDecisions, isoOf, REVERT_DAYS } from "./facts.mjs";
|
|
18
|
+
|
|
19
|
+
export const SHADOW_BAR = Object.freeze({ decisions: 20, days: 14, revert_days: REVERT_DAYS });
|
|
20
|
+
export const GATE_TIERS = Object.freeze(["low", "medium"]);
|
|
21
|
+
|
|
22
|
+
/** The latest trip's time, from the gate's own merges that a revert or regression named; or null. */
|
|
23
|
+
export function latestTrip(decisions) {
|
|
24
|
+
const times = decisions
|
|
25
|
+
.filter((d) => d.state === "merged" && d.mergePath === "gate" && d.revertedAt !== null)
|
|
26
|
+
.map((d) => d.revertedAt);
|
|
27
|
+
return times.length ? Math.max(...times) : null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The latest trip with the pull request that caused it: `{ at, pull }`, or null. */
|
|
31
|
+
export function latestTripOf(decisions) {
|
|
32
|
+
const at = latestTrip(decisions);
|
|
33
|
+
if (at === null) return null;
|
|
34
|
+
const hit = decisions.find(
|
|
35
|
+
(d) => d.state === "merged" && d.mergePath === "gate" && d.revertedAt === at,
|
|
36
|
+
);
|
|
37
|
+
return { at, pull: hit?.pull ?? null };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** The tier a decision is a record for: its tier-only hold's tier, else the comment's own. */
|
|
41
|
+
const tierOf = (d) => d.gate?.tierOnly ?? d.gate?.tier ?? null;
|
|
42
|
+
|
|
43
|
+
const isDecision = (d) => d.gate && ["would-merge", "tier-only-hold"].includes(d.gate.kind);
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @param {object[]} decisions from `gateDecisions`, for every task in the window
|
|
47
|
+
* @param {{ now: number, bar?: typeof SHADOW_BAR, tripAt?: number | null }} input
|
|
48
|
+
* `tripAt` is a trip the gate remembers on the control issue: counting starts at the later of it and the
|
|
49
|
+
* evidence's own latest trip, so a trip older than the window still resets the bar.
|
|
50
|
+
*/
|
|
51
|
+
export function gateSection(decisions, { now, bar = SHADOW_BAR, tripAt = null }) {
|
|
52
|
+
const seen = latestTrip(decisions);
|
|
53
|
+
const trip = seen === null ? tripAt : tripAt === null ? seen : Math.max(seen, tripAt);
|
|
54
|
+
const tiers = {};
|
|
55
|
+
for (const tier of GATE_TIERS) {
|
|
56
|
+
const mine = decisions.filter((d) => isDecision(d) && tierOf(d) === tier);
|
|
57
|
+
const since = (d) => trip === null || (d.mergedAt ?? Number.POSITIVE_INFINITY) > trip;
|
|
58
|
+
const confirmed = [];
|
|
59
|
+
let diverged = 0;
|
|
60
|
+
let pending = 0;
|
|
61
|
+
let reverted = 0;
|
|
62
|
+
for (const d of mine) {
|
|
63
|
+
if (d.state === "open") pending += 1;
|
|
64
|
+
else if (d.state === "merged" && d.mergePath === "gate") continue;
|
|
65
|
+
else if (d.state === "merged" && d.head === d.gate.head) {
|
|
66
|
+
if (!since(d)) continue;
|
|
67
|
+
confirmed.push(d);
|
|
68
|
+
if (d.revertedAt !== null) reverted += 1;
|
|
69
|
+
} else if (since({ mergedAt: d.mergedAt ?? now })) diverged += 1;
|
|
70
|
+
}
|
|
71
|
+
const times = confirmed.map((d) => d.mergedAt).sort((a, b) => a - b);
|
|
72
|
+
const days = times.length ? (times.at(-1) - times[0]) / DAY : 0;
|
|
73
|
+
const checks = [
|
|
74
|
+
{
|
|
75
|
+
name: `at least ${bar.decisions} decisions`,
|
|
76
|
+
value: confirmed.length,
|
|
77
|
+
met: confirmed.length >= bar.decisions,
|
|
78
|
+
},
|
|
79
|
+
{ name: `over at least ${bar.days} days`, value: Math.floor(days), met: days >= bar.days },
|
|
80
|
+
{ name: "each merged by a person unchanged", value: diverged, met: diverged === 0 },
|
|
81
|
+
{
|
|
82
|
+
name: `none reverted or fixed within ${bar.revert_days} days`,
|
|
83
|
+
value: reverted,
|
|
84
|
+
met: reverted === 0,
|
|
85
|
+
},
|
|
86
|
+
];
|
|
87
|
+
tiers[tier] = {
|
|
88
|
+
would_merge: mine.filter((d) => d.gate.kind === "would-merge").length,
|
|
89
|
+
tier_only_holds: mine.filter((d) => d.gate.kind === "tier-only-hold").length,
|
|
90
|
+
confirmed: confirmed.length,
|
|
91
|
+
pending,
|
|
92
|
+
diverged,
|
|
93
|
+
reverted,
|
|
94
|
+
days: Math.floor(days),
|
|
95
|
+
met: checks.every((c) => c.met),
|
|
96
|
+
checks,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
bar: { ...bar },
|
|
101
|
+
latest_trip: trip === null ? null : isoOf(trip),
|
|
102
|
+
tiers,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* The gate section for the tasks the report reads: the one function `factory report --gate` and the merge
|
|
108
|
+
* gate's trip both call (ADR 0041). `issues` are the tracker's closed Factory issues; the terminal ones in the
|
|
109
|
+
* window are judged, as the report judges them.
|
|
110
|
+
* @param {object[]} issues
|
|
111
|
+
* @param {{ isEngine: (login: string) => boolean }} ctx
|
|
112
|
+
* @param {{ now: number, days: number, tripAt?: number | null }} options
|
|
113
|
+
*/
|
|
114
|
+
export function shadowBarOf(issues, ctx, { now, days, tripAt = null }) {
|
|
115
|
+
const since = now - days * DAY;
|
|
116
|
+
const terminal = issues.filter((i) => {
|
|
117
|
+
const closed = Date.parse(i.closedAt);
|
|
118
|
+
return i.state === "CLOSED" && closed >= since && closed <= now;
|
|
119
|
+
});
|
|
120
|
+
const decisions = terminal.flatMap((i) => gateDecisions(i, ctx));
|
|
121
|
+
return { section: gateSection(decisions, { now, tripAt }), trip: latestTripOf(decisions) };
|
|
122
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// The six metrics (ADR 0044), over the facts of tasks that reached a terminal
|
|
2
|
+
// state in the window. Pure: facts in, numbers out. A rate with no tasks under
|
|
3
|
+
// it is `null`, never zero, so an empty tier is not read as a perfect one.
|
|
4
|
+
//
|
|
5
|
+
// merge rate merged / terminal
|
|
6
|
+
// first-pass exactly one build Stage run and no failed Stage check on any head / terminal
|
|
7
|
+
// lead time first evidence-backed `queued` to the merge; median and p90, in seconds
|
|
8
|
+
// escalation rate entered `needs-decision` at least once / terminal
|
|
9
|
+
// revert rate merged tasks a revert or `regression` named within 7 days / merged
|
|
10
|
+
// tokens per merged pull request, median and total, a missing total at its cap
|
|
11
|
+
|
|
12
|
+
import { DAY, REVERT_DAYS, TIERS, UNPLANNED } from "./facts.mjs";
|
|
13
|
+
|
|
14
|
+
const ratio = (n, d) => (d === 0 ? null : n / d);
|
|
15
|
+
|
|
16
|
+
/** The median: the middle value, the mean of the two middle values for an even count, rounded. */
|
|
17
|
+
export function median(values) {
|
|
18
|
+
if (!values.length) return null;
|
|
19
|
+
const s = [...values].sort((a, b) => a - b);
|
|
20
|
+
const mid = s.length >> 1;
|
|
21
|
+
return s.length % 2 ? s[mid] : Math.round((s[mid - 1] + s[mid]) / 2);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** The nearest-rank percentile: the smallest value with at least `p` of the values at or below it. */
|
|
25
|
+
export function percentile(values, p) {
|
|
26
|
+
if (!values.length) return null;
|
|
27
|
+
const s = [...values].sort((a, b) => a - b);
|
|
28
|
+
return s[Math.max(0, Math.ceil(p * s.length) - 1)];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function leadTimes(merged) {
|
|
32
|
+
const seconds = [];
|
|
33
|
+
let unknown = 0;
|
|
34
|
+
for (const t of merged) {
|
|
35
|
+
if (t.queuedAt === null || Number.isNaN(t.mergedAt) || t.mergedAt < t.queuedAt) unknown += 1;
|
|
36
|
+
else seconds.push(Math.round((t.mergedAt - t.queuedAt) / 1000));
|
|
37
|
+
}
|
|
38
|
+
return { tasks: seconds.length, unknown, median: median(seconds), p90: percentile(seconds, 0.9) };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function revertOf(merged, now) {
|
|
42
|
+
const reverted = merged.filter((t) => t.reverted).length;
|
|
43
|
+
return {
|
|
44
|
+
merged: merged.length,
|
|
45
|
+
reverted,
|
|
46
|
+
rate: ratio(reverted, merged.length),
|
|
47
|
+
// Merged less than 7 days ago: a revert may still come.
|
|
48
|
+
window_open: merged.filter((t) => now - t.mergedAt < REVERT_DAYS * DAY).length,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** All six for one group of terminal tasks. */
|
|
53
|
+
export function metricsOf(tasks, now) {
|
|
54
|
+
const merged = tasks.filter((t) => t.merged);
|
|
55
|
+
const tokens = merged.map((t) => t.tokens);
|
|
56
|
+
const escalated = tasks.filter((t) => t.escalated).length;
|
|
57
|
+
const firstPass = tasks.filter((t) => t.firstPass).length;
|
|
58
|
+
return {
|
|
59
|
+
terminal: tasks.length,
|
|
60
|
+
merged: merged.length,
|
|
61
|
+
not_merged: tasks.length - merged.length,
|
|
62
|
+
merge_rate: ratio(merged.length, tasks.length),
|
|
63
|
+
first_pass: { tasks: firstPass, rate: ratio(firstPass, tasks.length) },
|
|
64
|
+
lead_time_seconds: leadTimes(merged),
|
|
65
|
+
escalation: { tasks: escalated, rate: ratio(escalated, tasks.length) },
|
|
66
|
+
revert: revertOf(merged, now),
|
|
67
|
+
tokens: {
|
|
68
|
+
merged_tasks: merged.length,
|
|
69
|
+
median: median(tokens),
|
|
70
|
+
total: tokens.reduce((a, b) => a + b, 0),
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* The whole report's numbers: every task, each Risk tier, and the merge paths.
|
|
77
|
+
* @param {object[]} tasks the facts of the terminal tasks in the window
|
|
78
|
+
*/
|
|
79
|
+
export function computeMetrics(tasks, now) {
|
|
80
|
+
const tiers = {};
|
|
81
|
+
for (const tier of [...TIERS, UNPLANNED]) {
|
|
82
|
+
tiers[tier] = metricsOf(
|
|
83
|
+
tasks.filter((t) => t.tier === tier),
|
|
84
|
+
now,
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
const paths = {};
|
|
88
|
+
for (const path of ["person", "gate"]) {
|
|
89
|
+
const merged = tasks.filter((t) => t.merged && t.mergePath === path);
|
|
90
|
+
paths[path] = {
|
|
91
|
+
merged: merged.length,
|
|
92
|
+
lead_time_seconds: leadTimes(merged),
|
|
93
|
+
revert: revertOf(merged, now),
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
const merged = tasks.filter((t) => t.merged);
|
|
97
|
+
const runs = merged.reduce((n, t) => n + t.stageRuns, 0);
|
|
98
|
+
const missing = merged.reduce((n, t) => n + t.tokensMissing, 0);
|
|
99
|
+
const edited = merged.reduce((n, t) => n + t.tokensEdited, 0);
|
|
100
|
+
return {
|
|
101
|
+
overall: metricsOf(tasks, now),
|
|
102
|
+
tiers,
|
|
103
|
+
merge_paths: paths,
|
|
104
|
+
token_totals: {
|
|
105
|
+
stage_runs: runs,
|
|
106
|
+
counted: runs - missing - edited,
|
|
107
|
+
missing,
|
|
108
|
+
edited,
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
}
|