@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,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
|
+
}
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
// What `factory report` reads of the tracker: closed Factory issues with their
|
|
2
|
+
// comments, label events, cross-references and pull requests, the head statuses
|
|
3
|
+
// of those pull requests, the factory config, the Kill switch and who has write
|
|
4
|
+
// access. Every call is a `gh` read: `gh api` GET, or a GraphQL query (a `-f`
|
|
5
|
+
// field on `gh api graphql` is a query variable, not a write). Nothing is posted.
|
|
6
|
+
//
|
|
7
|
+
// `gh(args)` returns `{ status, stdout, stderr }`, as everywhere in the Factory.
|
|
8
|
+
|
|
9
|
+
import { STATES, stateLabel } from "../sweep/states.mjs";
|
|
10
|
+
import { createGithubTracker } from "../tracker/github.mjs";
|
|
11
|
+
import { bare } from "./facts.mjs";
|
|
12
|
+
|
|
13
|
+
export const FACTORY_CONFIG_PATH = ".github/factory/config.json";
|
|
14
|
+
const PAGE = 50;
|
|
15
|
+
const MAX_PAGES = 10;
|
|
16
|
+
|
|
17
|
+
const REF = `source { __typename
|
|
18
|
+
... on Issue { number createdAt labels(first: 20) { nodes { name } } }
|
|
19
|
+
... on PullRequest { number title body merged mergedAt createdAt } }`;
|
|
20
|
+
|
|
21
|
+
const ISSUE_QUERY = `query($q: String!, $cursor: String) {
|
|
22
|
+
search(query: $q, type: ISSUE, first: ${PAGE}, after: $cursor) {
|
|
23
|
+
pageInfo { hasNextPage endCursor }
|
|
24
|
+
nodes { ... on Issue {
|
|
25
|
+
number state closedAt createdAt
|
|
26
|
+
labels(first: 50) { nodes { name } }
|
|
27
|
+
comments(first: 100) { totalCount nodes { databaseId author { login } body createdAt lastEditedAt } }
|
|
28
|
+
timelineItems(first: 100, itemTypes: [LABELED_EVENT, CROSS_REFERENCED_EVENT]) { nodes { __typename
|
|
29
|
+
... on LabeledEvent { createdAt actor { login } label { name } }
|
|
30
|
+
... on CrossReferencedEvent { ${REF} } } }
|
|
31
|
+
closedByPullRequestsReferences(first: 10, includeClosedPrs: true) { nodes {
|
|
32
|
+
number state merged mergedAt mergedBy { login } headRefName headRefOid
|
|
33
|
+
comments(first: 50) { nodes { databaseId author { login } body createdAt lastEditedAt } }
|
|
34
|
+
timelineItems(first: 50, itemTypes: [CROSS_REFERENCED_EVENT]) { nodes { __typename ... on CrossReferencedEvent { ${REF} } } } } }
|
|
35
|
+
} }
|
|
36
|
+
}
|
|
37
|
+
}`;
|
|
38
|
+
|
|
39
|
+
const STATUS_QUERY = `query($owner: String!, $name: String!, $number: Int!) {
|
|
40
|
+
repository(owner: $owner, name: $name) { pullRequest(number: $number) {
|
|
41
|
+
commits(first: 100) { nodes { commit { oid status { contexts { context state creator { login } } } } } } } }
|
|
42
|
+
}`;
|
|
43
|
+
|
|
44
|
+
const comment = (c) => ({
|
|
45
|
+
id: c.databaseId,
|
|
46
|
+
author: c.author?.login ?? "",
|
|
47
|
+
body: c.body ?? "",
|
|
48
|
+
createdAt: c.createdAt,
|
|
49
|
+
editedAt: c.lastEditedAt ?? null,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
function refOf(node) {
|
|
53
|
+
const s = node?.source;
|
|
54
|
+
if (!s) return null;
|
|
55
|
+
if (s.__typename === "PullRequest") {
|
|
56
|
+
return {
|
|
57
|
+
kind: "pull",
|
|
58
|
+
number: s.number,
|
|
59
|
+
title: s.title ?? "",
|
|
60
|
+
body: s.body ?? "",
|
|
61
|
+
merged: s.merged === true,
|
|
62
|
+
mergedAt: s.mergedAt ?? null,
|
|
63
|
+
createdAt: s.createdAt,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
if (s.__typename === "Issue") {
|
|
67
|
+
return {
|
|
68
|
+
kind: "issue",
|
|
69
|
+
number: s.number,
|
|
70
|
+
createdAt: s.createdAt,
|
|
71
|
+
labels: (s.labels?.nodes ?? []).map((l) => l.name),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** An issue, from the GraphQL node, in the shape `facts.mjs` reads. Exported for the suite. */
|
|
78
|
+
export function issueOf(node) {
|
|
79
|
+
const items = node.timelineItems?.nodes ?? [];
|
|
80
|
+
return {
|
|
81
|
+
number: node.number,
|
|
82
|
+
state: node.state,
|
|
83
|
+
closedAt: node.closedAt,
|
|
84
|
+
createdAt: node.createdAt,
|
|
85
|
+
labels: (node.labels?.nodes ?? []).map((l) => l.name),
|
|
86
|
+
commentsTruncated: (node.comments?.totalCount ?? 0) > 100,
|
|
87
|
+
comments: (node.comments?.nodes ?? []).map(comment),
|
|
88
|
+
events: items
|
|
89
|
+
.filter((i) => i.__typename === "LabeledEvent")
|
|
90
|
+
.map((i) => ({
|
|
91
|
+
type: "labeled",
|
|
92
|
+
label: i.label?.name,
|
|
93
|
+
actor: i.actor?.login ?? "",
|
|
94
|
+
createdAt: i.createdAt,
|
|
95
|
+
})),
|
|
96
|
+
refs: items
|
|
97
|
+
.filter((i) => i.__typename === "CrossReferencedEvent")
|
|
98
|
+
.map(refOf)
|
|
99
|
+
.filter(Boolean),
|
|
100
|
+
pulls: (node.closedByPullRequestsReferences?.nodes ?? []).map((p) => ({
|
|
101
|
+
number: p.number,
|
|
102
|
+
state: p.state,
|
|
103
|
+
merged: p.merged === true,
|
|
104
|
+
mergedAt: p.mergedAt ?? null,
|
|
105
|
+
mergedBy: p.mergedBy?.login ?? "",
|
|
106
|
+
branch: p.headRefName ?? "",
|
|
107
|
+
head: p.headRefOid,
|
|
108
|
+
comments: (p.comments?.nodes ?? []).map(comment),
|
|
109
|
+
refs: (p.timelineItems?.nodes ?? []).map(refOf).filter(Boolean),
|
|
110
|
+
statuses: [],
|
|
111
|
+
})),
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* @param {{
|
|
117
|
+
* gh: (args: string[]) => { status: number | null, stdout: string, stderr: string },
|
|
118
|
+
* repo: string, since: number, env: Record<string, string | undefined>,
|
|
119
|
+
* identity: string, log: (line: string) => void,
|
|
120
|
+
* }} input
|
|
121
|
+
*/
|
|
122
|
+
export function readTracker({ gh, repo, since, env, identity, log }) {
|
|
123
|
+
const [owner, name] = repo.split("/");
|
|
124
|
+
const tracker = createGithubTracker({ gh, repo });
|
|
125
|
+
const json = (args) => {
|
|
126
|
+
const r = gh(args);
|
|
127
|
+
if (r.status !== 0) {
|
|
128
|
+
throw new Error(
|
|
129
|
+
`gh ${args.slice(0, 3).join(" ")} failed: ${String(r.stderr).trim().slice(0, 300)}`,
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
return JSON.parse(r.stdout || "null");
|
|
133
|
+
};
|
|
134
|
+
const warnings = [];
|
|
135
|
+
const day = new Date(since).toISOString().slice(0, 10);
|
|
136
|
+
|
|
137
|
+
// Closed issues carrying each Factory state label: a closed task keeps its last one.
|
|
138
|
+
const byNumber = new Map();
|
|
139
|
+
for (const state of STATES) {
|
|
140
|
+
let cursor = null;
|
|
141
|
+
for (let page = 0; page < MAX_PAGES; page++) {
|
|
142
|
+
const q = `repo:${repo} is:issue is:closed closed:>=${day} label:"${stateLabel(state)}"`;
|
|
143
|
+
const data = json([
|
|
144
|
+
"api",
|
|
145
|
+
"graphql",
|
|
146
|
+
"-f",
|
|
147
|
+
`query=${ISSUE_QUERY}`,
|
|
148
|
+
"-f",
|
|
149
|
+
`q=${q}`,
|
|
150
|
+
...(cursor ? ["-f", `cursor=${cursor}`] : []),
|
|
151
|
+
]);
|
|
152
|
+
const search = data?.data?.search;
|
|
153
|
+
if (!search) throw new Error("tracker read: the GraphQL response has no search");
|
|
154
|
+
for (const node of search.nodes ?? []) {
|
|
155
|
+
if (node?.number && !byNumber.has(node.number)) byNumber.set(node.number, issueOf(node));
|
|
156
|
+
}
|
|
157
|
+
if (!search.pageInfo?.hasNextPage) break;
|
|
158
|
+
cursor = search.pageInfo.endCursor;
|
|
159
|
+
if (page === MAX_PAGES - 1)
|
|
160
|
+
warnings.push(`${stateLabel(state)}: only the first ${MAX_PAGES * PAGE} issues were read`);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
const issues = [...byNumber.values()].sort((a, b) => a.number - b.number);
|
|
164
|
+
for (const i of issues) {
|
|
165
|
+
if (i.commentsTruncated) warnings.push(`#${i.number}: only the first 100 comments were read`);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// Each pull request's Stage checks on every head it has had.
|
|
169
|
+
for (const issue of issues) {
|
|
170
|
+
for (const pull of issue.pulls) {
|
|
171
|
+
try {
|
|
172
|
+
const data = json([
|
|
173
|
+
"api",
|
|
174
|
+
"graphql",
|
|
175
|
+
"-f",
|
|
176
|
+
`query=${STATUS_QUERY}`,
|
|
177
|
+
"-f",
|
|
178
|
+
`owner=${owner}`,
|
|
179
|
+
"-f",
|
|
180
|
+
`name=${name}`,
|
|
181
|
+
"-F",
|
|
182
|
+
`number=${Number(pull.number)}`,
|
|
183
|
+
]);
|
|
184
|
+
for (const node of data?.data?.repository?.pullRequest?.commits?.nodes ?? []) {
|
|
185
|
+
for (const c of node.commit?.status?.contexts ?? []) {
|
|
186
|
+
pull.statuses.push({
|
|
187
|
+
head: node.commit.oid,
|
|
188
|
+
context: c.context,
|
|
189
|
+
state: String(c.state).toLowerCase(),
|
|
190
|
+
creator: c.creator?.login ?? "",
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
} catch (error) {
|
|
195
|
+
warnings.push(
|
|
196
|
+
`#${issue.number}: pull request #${pull.number} checks unread (${error.message})`,
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// Who may move a label: the engine, and anyone with write access.
|
|
203
|
+
const actors = new Set();
|
|
204
|
+
for (const i of issues) {
|
|
205
|
+
for (const e of i.events) if (bare(e.actor) !== bare(identity) && e.actor) actors.add(e.actor);
|
|
206
|
+
}
|
|
207
|
+
let writers = [];
|
|
208
|
+
try {
|
|
209
|
+
writers = tracker.readWriters([...actors]);
|
|
210
|
+
} catch (error) {
|
|
211
|
+
warnings.push(`write access unread (${error.message}): label moves by people do not count`);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// The config, from the default branch, and the Kill switch.
|
|
215
|
+
let configText = null;
|
|
216
|
+
let configProblem = null;
|
|
217
|
+
try {
|
|
218
|
+
configText = tracker.readFile(FACTORY_CONFIG_PATH, tracker.defaultBranch());
|
|
219
|
+
} catch (error) {
|
|
220
|
+
configProblem = error.message;
|
|
221
|
+
warnings.push(`${FACTORY_CONFIG_PATH} unread (${error.message})`);
|
|
222
|
+
}
|
|
223
|
+
let killSwitch = env.AGENT_KIT_FACTORY_ENABLED ?? null;
|
|
224
|
+
if (killSwitch === null) {
|
|
225
|
+
const v = gh([
|
|
226
|
+
"api",
|
|
227
|
+
`repos/${repo}/actions/variables/AGENT_KIT_FACTORY_ENABLED`,
|
|
228
|
+
"--jq",
|
|
229
|
+
".value",
|
|
230
|
+
]);
|
|
231
|
+
if (v.status === 0) killSwitch = v.stdout.replace(/\n$/, "");
|
|
232
|
+
else
|
|
233
|
+
warnings.push(
|
|
234
|
+
"the Kill switch variable was not readable: Phase 0 and Phase 1 cannot be told apart",
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
log(`read ${issues.length} closed Factory issues`);
|
|
238
|
+
return {
|
|
239
|
+
issues,
|
|
240
|
+
writers,
|
|
241
|
+
configText,
|
|
242
|
+
configProblem,
|
|
243
|
+
killSwitch: killSwitch === null ? null : killSwitch === "true" ? "true" : "false",
|
|
244
|
+
warnings,
|
|
245
|
+
};
|
|
246
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
// The report as text for a person, and as stable JSON. Pure.
|
|
2
|
+
|
|
3
|
+
const pct = (r) => (r === null ? "n/a" : `${Math.round(r * 1000) / 10}%`);
|
|
4
|
+
const num = (n) => (n === null ? "n/a" : String(n));
|
|
5
|
+
|
|
6
|
+
function duration(seconds) {
|
|
7
|
+
if (seconds === null) return "n/a";
|
|
8
|
+
const h = seconds / 3600;
|
|
9
|
+
return h >= 48 ? `${Math.round((h / 24) * 10) / 10}d` : `${Math.round(h * 10) / 10}h`;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function pad(rows) {
|
|
13
|
+
const widths = rows[0].map((_, i) => Math.max(...rows.map((r) => String(r[i]).length)));
|
|
14
|
+
return rows.map((r) =>
|
|
15
|
+
r
|
|
16
|
+
.map((c, i) => String(c).padEnd(widths[i]))
|
|
17
|
+
.join(" ")
|
|
18
|
+
.trimEnd(),
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function metricRow(label, m) {
|
|
23
|
+
return [
|
|
24
|
+
label,
|
|
25
|
+
m.terminal,
|
|
26
|
+
pct(m.merge_rate),
|
|
27
|
+
pct(m.first_pass.rate),
|
|
28
|
+
`${duration(m.lead_time_seconds.median)} / ${duration(m.lead_time_seconds.p90)}`,
|
|
29
|
+
pct(m.escalation.rate),
|
|
30
|
+
pct(m.revert.rate),
|
|
31
|
+
`${num(m.tokens.median)} / ${m.tokens.total}`,
|
|
32
|
+
];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const mark = (met) => (met === null ? "-" : met ? "met" : "not met");
|
|
36
|
+
|
|
37
|
+
/** The text report. */
|
|
38
|
+
export function renderText(report) {
|
|
39
|
+
const out = [];
|
|
40
|
+
out.push(
|
|
41
|
+
`Factory report for ${report.repo}: ${report.overall.terminal} terminal tasks in the last ${report.window.days} days (${report.window.since} to ${report.window.until})`,
|
|
42
|
+
"",
|
|
43
|
+
);
|
|
44
|
+
const head = [
|
|
45
|
+
"",
|
|
46
|
+
"tasks",
|
|
47
|
+
"merge",
|
|
48
|
+
"first-pass",
|
|
49
|
+
"lead median/p90",
|
|
50
|
+
"escalation",
|
|
51
|
+
"revert 7d",
|
|
52
|
+
"tokens median/total",
|
|
53
|
+
];
|
|
54
|
+
const rows = [head, metricRow("all", report.overall)];
|
|
55
|
+
for (const [tier, m] of Object.entries(report.tiers)) {
|
|
56
|
+
if (m.terminal) rows.push(metricRow(tier, m));
|
|
57
|
+
}
|
|
58
|
+
out.push(...pad(rows), "");
|
|
59
|
+
out.push("By merge path (merged tasks):");
|
|
60
|
+
out.push(
|
|
61
|
+
...pad(
|
|
62
|
+
Object.entries(report.merge_paths).map(([path, p]) => [
|
|
63
|
+
path,
|
|
64
|
+
p.merged,
|
|
65
|
+
`lead ${duration(p.lead_time_seconds.median)} / ${duration(p.lead_time_seconds.p90)}`,
|
|
66
|
+
`revert ${pct(p.revert.rate)} (${p.revert.reverted}/${p.revert.merged})`,
|
|
67
|
+
]),
|
|
68
|
+
),
|
|
69
|
+
"",
|
|
70
|
+
);
|
|
71
|
+
const t = report.token_totals;
|
|
72
|
+
out.push(
|
|
73
|
+
`Token totals over merged tasks: ${t.stage_runs} Stage runs, ${t.counted} counted, ${t.missing} missing, ${t.edited} edited (the last two at the Stage's cap).`,
|
|
74
|
+
"",
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
const r = report.rollout;
|
|
78
|
+
if (r.problems?.length) {
|
|
79
|
+
out.push(`Rollout phase: unknown (${r.problems.join("; ")}).`);
|
|
80
|
+
} else if (r.phase === null) {
|
|
81
|
+
out.push(
|
|
82
|
+
r.candidates.length
|
|
83
|
+
? `Rollout phase: Phase ${r.candidates.join(" or ")} (the Kill switch could not be read).`
|
|
84
|
+
: "Rollout phase: the effective config matches no phase.",
|
|
85
|
+
);
|
|
86
|
+
} else {
|
|
87
|
+
out.push(`Rollout phase: Phase ${r.phase}, ${r.name}.`);
|
|
88
|
+
}
|
|
89
|
+
out.push(
|
|
90
|
+
` Kill switch ${r.effective.kill_switch ?? "unread"}, plan_approval ${r.effective.plan_approval}, merge.mode ${r.effective.merge_mode}, auto_tiers [${r.effective.auto_tiers.join(", ")}].`,
|
|
91
|
+
);
|
|
92
|
+
if (r.next) {
|
|
93
|
+
out.push(`Next, Phase ${r.next.phase}: ${r.next.bar}. Bar ${mark(r.next.met)}.`);
|
|
94
|
+
for (const c of r.next.checks) {
|
|
95
|
+
out.push(
|
|
96
|
+
` ${c.met ? "[x]" : "[ ]"} ${c.name}: ${c.value === null ? "n/a" : c.value}${c.want ? ` (want ${c.want})` : ""}`,
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
if (report.gate) {
|
|
101
|
+
const g = report.gate;
|
|
102
|
+
out.push(
|
|
103
|
+
"",
|
|
104
|
+
`Shadow-to-on bar (${g.bar.decisions} decisions over ${g.bar.days} days, none reverted within ${g.bar.revert_days}):`,
|
|
105
|
+
);
|
|
106
|
+
out.push(
|
|
107
|
+
` Latest trip: ${g.latest_trip ?? "none"}${g.latest_trip ? " (counted from here)" : ""}`,
|
|
108
|
+
);
|
|
109
|
+
for (const [tier, s] of Object.entries(g.tiers)) {
|
|
110
|
+
out.push(
|
|
111
|
+
` ${tier}: ${s.confirmed} confirmed of ${s.would_merge} would-merge and ${s.tier_only_holds} tier-only holds; ${s.diverged} diverged, ${s.reverted} reverted, ${s.pending} pending, ${s.days} days. Bar ${mark(s.met)}.`,
|
|
112
|
+
);
|
|
113
|
+
for (const c of s.checks) out.push(` ${c.met ? "[x]" : "[ ]"} ${c.name}: ${c.value}`);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
for (const w of report.warnings) out.push("", `warning: ${w}`);
|
|
117
|
+
return out.join("\n");
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** The JSON report: two-space indented, keys in the order `buildReport` builds them. */
|
|
121
|
+
export const renderJson = (report) => JSON.stringify(report, null, 2);
|