@feigi/fleet-ctl 3.50.2 → 3.52.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@feigi/fleet-ctl",
3
- "version": "3.50.2",
3
+ "version": "3.52.0",
4
4
  "description": "Agent fleet: run-team controller, merge bot, PR reviewer, and the ticket pipeline they share",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -21,6 +21,15 @@
21
21
 
22
22
  import { createHash } from "node:crypto";
23
23
 
24
+ // How an archive beside the ledger is named. `ledger.mjs rotate` files the
25
+ // ledger as `ledger.<stamp>.md` and the phase-0 receipt as `phase0.<stamp>.json`
26
+ // under one stamp, and `phase0.mjs` files a receipt the rotation left behind
27
+ // the same way, so the format lives here once.
28
+ const RECEIPT_STEM = "phase0";
29
+ export const RECEIPT = `${RECEIPT_STEM}.json`;
30
+ export const archiveStamp = () => new Date().toISOString().replace(/\.\d{3}Z$/, "Z").replaceAll(":", "");
31
+ export const receiptArchiveName = (stamp) => `${RECEIPT_STEM}.${stamp}.json`;
32
+
24
33
  // The outcome vocabulary, verbatim from the spec. A word is matched exactly,
25
34
  // except the three that carry a value, which match OUTCOME_PATTERNS below.
26
35
  const FAMILIES = {
@@ -47,7 +47,7 @@ import { fileURLToPath } from "node:url";
47
47
  import { spawnSync, execFileSync } from "node:child_process";
48
48
  import { makeDie, isFlagLike, hasEqualsForm, isDigits } from "./arg.mjs";
49
49
  import { fleetFile, FleetDirUnresolvable } from "./fleet-dir.mjs";
50
- import { parseMember, parseToken, memberTokens, nextMergeBot, nextRetry, liveOnLedger, retiredOnLedger, isRetirable, agentDefinition, CELL, tierValues, rowPr, rowWriteRefusal, isReviewToken, addedReviewToken, newSplitRow, splitRowClause, added, premisesOf, MEMBER_FAMILIES, ROWS, DISPATCHED, FILED, RULED, DRAIN, RETIRED, headerRe, escapeText, unescapeText } from "./ledger-grammar.mjs";
50
+ import { parseMember, parseToken, memberTokens, nextMergeBot, nextRetry, liveOnLedger, retiredOnLedger, isRetirable, agentDefinition, CELL, tierValues, rowPr, rowWriteRefusal, isReviewToken, addedReviewToken, newSplitRow, splitRowClause, added, premisesOf, MEMBER_FAMILIES, ROWS, DISPATCHED, FILED, RULED, DRAIN, RETIRED, RECEIPT, archiveStamp, receiptArchiveName, headerRe, escapeText, unescapeText } from "./ledger-grammar.mjs";
51
51
  import { deriveRun, LedgerError, labelOffMember, HELD, CONFLICT_HOLD } from "./fleet-tick.mjs";
52
52
  import { stateFileIn, readState, writeState, assessBeat, stallsAt } from "./fleet-state.mjs";
53
53
  import { processSource, ancestry, controllerOf, judgeController, ProcUnreadable } from "./proc.mjs";
@@ -1446,7 +1446,10 @@ function runCheck() {
1446
1446
  // hard-linked to its stamped name, then unlinked or, when a successor is
1447
1447
  // carried, replaced by it — so its bytes are untouched, and the link refuses
1448
1448
  // atomically when that name is already taken rather than overwriting an older
1449
- // archive.
1449
+ // archive. A `phase0.json` receipt beside the ledger belongs to the same run,
1450
+ // so it is archived as `phase0.<stamp>.json` under the ledger archive's own
1451
+ // stamp — a warning-only move after the ledger move, refused up front only
1452
+ // when that name is taken.
1450
1453
  //
1451
1454
  // What follows depends on the verdict below. After a `dead` or `none` one
1452
1455
  // nothing is carried: the next write subcommand finds no file and starts the
@@ -1564,9 +1567,16 @@ function runRotate() {
1564
1567
  carry = carryOf(text);
1565
1568
  }
1566
1569
  const carrying = carry.rows.length > 0 || carry.dispatched.length > 0 || carry.retired.length > 0;
1570
+ // The phase-0 receipt beside the ledger belongs to the run being rotated
1571
+ // away, so it takes the archive's own stamp: `phase0.<stamp>.json` next to
1572
+ // `ledger.<stamp>.md`. A name already taken refuses before anything moves,
1573
+ // as the ledger's own does.
1574
+ const stamp = archiveStamp();
1575
+ const receipt = join(dirname(file), RECEIPT);
1576
+ const receiptArchive = existsSync(receipt) ? join(dirname(file), receiptArchiveName(stamp)) : null;
1577
+ if (receiptArchive !== null && existsSync(receiptArchive)) die(`refusing to rotate ${file}: ${receiptArchive} already exists`);
1567
1578
  let archive = null;
1568
1579
  if (exists) {
1569
- const stamp = new Date().toISOString().replace(/\.\d{3}Z$/, "Z").replaceAll(":", "");
1570
1580
  archive = join(dirname(file), `${basename(file, ".md")}.${stamp}.md`);
1571
1581
  try {
1572
1582
  linkSync(file, archive);
@@ -1588,6 +1598,17 @@ function runRotate() {
1588
1598
  } else {
1589
1599
  console.error(` nothing to rotate: ${file} does not exist`);
1590
1600
  }
1601
+ // After the ledger has moved, and a warning rather than a refusal: the
1602
+ // ledger is already archived, and a receipt left where it is is read as a
1603
+ // previous run's by the next phase 0, which moves it aside itself.
1604
+ if (receiptArchive !== null) {
1605
+ try {
1606
+ renameSync(receipt, receiptArchive);
1607
+ console.error(` archived ${receipt} -> ${receiptArchive}`);
1608
+ } catch (e) {
1609
+ console.error(` could not archive ${receipt} -> ${receiptArchive}: ${e.message}`);
1610
+ }
1611
+ }
1591
1612
  // Nothing to carry writes nothing, so the next write starts the skeleton
1592
1613
  // exactly as after any other verdict.
1593
1614
  if (carrying) {
@@ -1,5 +1,7 @@
1
1
  // Scraper for per-member model/effort facts. Pure over omp's own subagent
2
- // transcripts: no clock, no network, no gh.
2
+ // transcripts: no clock, no network, no gh. The one column that looks beside
3
+ // them is `report`, which asks whether the member's report file is in the
4
+ // scratch root (`--scratch <dir>`, default the root scratch-root.mjs prints).
3
5
  //
4
6
  // The transcript parsing (fold, model/effort extraction, ticket/pr naming,
5
7
  // and the `gh pr create` result an implementer's `pr` comes from)
@@ -11,6 +13,8 @@ import { readdirSync, readFileSync, statSync } from "node:fs";
11
13
  import { join, basename } from "node:path";
12
14
 
13
15
  import { readOmpSession, normalizeModel, parseMemberName, isOmpSessionDirName } from "./member-record.mjs";
16
+ import { reportFamily, reportStatus, defaultScratch } from "./report-files.mjs";
17
+ import { FleetDirUnresolvable } from "./fleet-dir.mjs";
14
18
 
15
19
  export { normalizeModel, parseMemberName };
16
20
 
@@ -84,19 +88,34 @@ export function rowsForSession(sessionDir, stats = {}) {
84
88
  }));
85
89
  }
86
90
 
87
- // APPENDED TO, never inserted into: every read-out in the file's header and
88
- // in the design specs indexes by position, so a column added anywhere but the
89
- // end silently repoints every `$n` a reader already wrote down.
90
- // `subagent_type` therefore follows `harness`, and the four priced columns
91
- // follow it. `cost` is the provider's own per-turn `usage.cost.total`, summed
92
- // to the micro-dollar — the $ of record; `tokens_cache_write_1h` and `cost`
93
- // are blank where the transcript recorded no figure, never 0.
91
+ // A column is APPENDED, never inserted, as a rule: a read-out that indexes by
92
+ // position (`$5`, in the file's header) silently repoints when one is added
93
+ // ahead of it. `report` is placed after `subagent_type` regardless, which
94
+ // moves the four priced columns one place right; every script that reads this
95
+ // file through parseTsv takes those four by name, and the header's
96
+ // position-indexed read-outs name columns ahead of them. `subagent_type`
97
+ // follows `harness`. `cost` is the provider's own per-turn `usage.cost.total`,
98
+ // summed to the micro-dollar — the $ of record; `tokens_cache_write_1h` and
99
+ // `cost` are blank where the transcript recorded no figure, never 0.
94
100
  export const COLUMNS = [
95
101
  "session", "run_date", "role", "member", "model", "effort", "ticket", "pr",
96
102
  "tokens_cache_create", "tokens_out", "wall_s", "turns", "agent", "harness",
97
- "subagent_type", "tokens_in", "tokens_cache_read", "tokens_cache_write_1h", "cost",
103
+ "subagent_type", "report", "tokens_in", "tokens_cache_read", "tokens_cache_write_1h", "cost",
98
104
  ];
99
105
 
106
+ // `report`: what reportStatus() says of the row's member under `scratch` — a
107
+ // pure function of the member name and the file system, never of the
108
+ // transcript, so it is stamped here on the rows rowsForSession() returned.
109
+ // With no `scratch` (null: none could be resolved) a member a schema covers
110
+ // reads blank — unknown, the file's own spelling of it — and one none covers
111
+ // still reads `-`, which needs no file to look at.
112
+ export function withReports(rows, scratch) {
113
+ return rows.map((r) => ({
114
+ ...r,
115
+ report: scratch === null && reportFamily(r.member) !== null ? "" : reportStatus(scratch, r.member),
116
+ }));
117
+ }
118
+
100
119
  // Row objects use camelCase; the file uses snake_case. One map, one direction
101
120
  // each, so a rename cannot silently drop a column.
102
121
  const FIELD = {
@@ -164,21 +183,27 @@ if (isCLI(import.meta.url)) {
164
183
  const argv = process.argv.slice(2);
165
184
  const fileIdx = argv.indexOf("--file");
166
185
  const file = fileIdx >= 0 ? argv[fileIdx + 1] : "docs/metrics/member-outcomes.tsv";
186
+ const scratchIdx = argv.indexOf("--scratch");
187
+ const scratchArg = scratchIdx >= 0 ? argv[scratchIdx + 1] : null;
167
188
  // An unrecognised `--flag` is silently dropped by the dirs filter below
168
189
  // rather than refused — worst case `--file=/tmp/x.tsv` (no such flag;
169
190
  // --file wants a SPACE, not `=`) never sets fileIdx, so this would silently
170
191
  // write the production metrics file instead of the path the operator asked
171
192
  // for.
172
- const bad = argv.find((a) => a.startsWith("--") && a !== "--file");
173
- if (bad) die(`unknown option ${bad} (did you mean --file <path>?)`);
193
+ const bad = argv.find((a) => a.startsWith("--") && a !== "--file" && a !== "--scratch");
194
+ if (bad) die(`unknown option ${bad} (did you mean --file <path> or --scratch <dir>?)`);
174
195
 
175
196
  // fileIdx is -1 when --file is absent, which makes fileIdx + 1 equal 0 — the
176
197
  // FIRST positional argument, not a real index into argv. Without the
177
198
  // fileIdx < 0 guard this filters out the session dir itself, and the bare
178
199
  // form (the one the spec, the backfill loop and run-team's phase-3
179
- // instruction all use) always exits 2.
180
- const dirs = argv.filter((a, i) => !a.startsWith("--") && (fileIdx < 0 || i !== fileIdx + 1));
181
- if (dirs.length !== 1) die("usage: member-outcomes.mjs <session-dir> [--file <tsv>]");
200
+ // instruction all use) always exits 2. `--scratch`'s value is skipped the
201
+ // same way.
202
+ const valueOf = (idx) => (i) => idx >= 0 && i === idx + 1;
203
+ const isFileValue = valueOf(fileIdx);
204
+ const isScratchValue = valueOf(scratchIdx);
205
+ const dirs = argv.filter((a, i) => !a.startsWith("--") && !isFileValue(i) && !isScratchValue(i));
206
+ if (dirs.length !== 1) die("usage: member-outcomes.mjs <session-dir> [--file <tsv>] [--scratch <dir>]");
182
207
  // arg.mjs's rule, consumed rather than copied. This line used to
183
208
  // hand-write `!file || file.startsWith("--")`, which was that rule with the
184
209
  // blank spelling missing — so `--file " "` was accepted here and wrote the
@@ -187,6 +212,16 @@ if (isCLI(import.meta.url)) {
187
212
  // is imported. The refusal WORDING stays this script's own: `--file` is the
188
213
  // flag the operator typed.
189
214
  if (isFlagLike(file)) die("--file needs a path");
215
+ if (scratchIdx >= 0 && isFlagLike(scratchArg)) die("--scratch needs a path");
216
+ // An explicit root that is not a directory is a mistyped path, and reading it
217
+ // as a root would stamp `absent` on every covered row and, by the replace-by-key
218
+ // merge, over a recorded `ok`/`invalid` — indistinguishable from "wrote no report".
219
+ if (scratchIdx >= 0) {
220
+ let isDir = false;
221
+ try { isDir = statSync(scratchArg).isDirectory(); }
222
+ catch (e) { die(`cannot read --scratch ${scratchArg}: ${e.code ?? e.message}`); }
223
+ if (!isDir) die(`--scratch ${scratchArg} is not a directory`);
224
+ }
190
225
 
191
226
  const sessionDir = dirs[0].replace(/\/+$/, "");
192
227
  // A wrong guess used to exit 0 having scraped and written nothing, because
@@ -223,8 +258,17 @@ if (isCLI(import.meta.url)) {
223
258
  try { existing = parseTsv(prev); }
224
259
  catch (e) { die(`${file} is not readable as this schema: ${e.message}`); }
225
260
 
261
+ let scratch = scratchArg;
262
+ if (scratch === null) {
263
+ try { scratch = defaultScratch(); }
264
+ catch (e) {
265
+ if (!(e instanceof FleetDirUnresolvable)) throw e;
266
+ process.stderr.write(`${NAME}: WARNING no scratch root to look for report files in (${e.message}) — \`report\` is left blank where a schema covers the member; pass --scratch <dir>\n`);
267
+ }
268
+ }
269
+
226
270
  const stats = {};
227
- const merged = mergeRows(existing, rowsForSession(sessionDir, stats));
271
+ const merged = mergeRows(existing, withReports(rowsForSession(sessionDir, stats), scratch));
228
272
  // Write-then-rename: writeFileSync truncates before it writes, so an interrupt
229
273
  // over the committed corpus left a torn file that the NEXT scrape read, merged
230
274
  // and rewrote — losing every other session's rows for good.
@@ -0,0 +1,562 @@
1
+ #!/usr/bin/env node
2
+ // Phase 0 as one command: the run's start sequence, in order, with a receipt.
3
+ //
4
+ // phase0.mjs --session <dir> [--implementer-cap n] [--reviewer-cap n]
5
+ // [--plugin-checkout <dir>] [--spend-dir <dir>]
6
+ // [--workflow <name> | --declare-no-ci]
7
+ //
8
+ // Reached through the Resolver and run from the MAIN checkout (a linked
9
+ // worktree refuses: the checks below judge the checkout the controller reads
10
+ // its rules from). The steps, each a named entry in the receipt, run in
11
+ // this order because the order is the point — the pin certifies the tree the
12
+ // fast-forward left, the rotation comes before the fold-in, the scratch
13
+ // reclaim straight after the rotation, the metrics refresh before the first
14
+ // Pull is routed:
15
+ //
16
+ // branch `git symbolic-ref -q HEAD` is refs/heads/main. A checkout left
17
+ // on a branch whose PR has merged would fast-forward that branch
18
+ // and the baseline would record it as the run's ref.
19
+ // fast-forward `git fetch origin`, `rev-list --count main..origin/main`,
20
+ // `merge --ff-only origin/main`. HEAD moving means the rules this
21
+ // script came from are superseded: prints `SUPERSEDED <n>
22
+ // commits; re-run phase0.mjs` and fails, so the re-run executes
23
+ // from the new tree.
24
+ // pin `instruments.sh --pin`, with `--audit <plugin-checkout>` when
25
+ // given (a workspace that tracks no `plugin/…` of its own).
26
+ // baseline `main-checkout.mjs --record`; its refusals fail the step.
27
+ // preflight `preflight.mjs`. A non-zero exit fails the step; the FAIL, WARN
28
+ // and PREFLIGHT lines of an exit 1 are printed.
29
+ // ruleset `.github/scripts/apply-ruleset.sh --check`, when the workspace
30
+ // checks a ruleset spec in. Exit 3 is `drift` (the diff is the
31
+ // output), exit 2 or anything else `unanswered`; neither stops
32
+ // the run — reconciling is a write to the gate and the
33
+ // maintainer's.
34
+ // tier-roles `tier-roles.mjs --check`.
35
+ // metrics `metrics-refresh.mjs`. A non-zero exit fails the step.
36
+ // cockpit `board.mjs serve --open` launched detached, `--spend-dir`
37
+ // passed through. Recorded; never fails the run.
38
+ // rotate `ledger.mjs rotate`. Exit 2 is a refusal and fails the step.
39
+ // reclaim `reclaim-scratch.mjs --root <scratch> --after-rotate --apply`.
40
+ // Recorded; never fails the run.
41
+ // recipe `derive-testcmd.sh . install` and `. test --at origin/main`.
42
+ // Either exiting 1 is a cache with nothing valid in it: pending,
43
+ // `NEED derive-recipe`. Exit 3 (a tool the reader could not run)
44
+ // fails the step — deriving would not help.
45
+ // fold-in every open PR no carried ledger row names: one that closes an
46
+ // issue gets a `#<pr>` row from `ledger.mjs row` (the tick queues
47
+ // its review off it); one that closes none is listed as `NEED
48
+ // review PR#<pr> (closes no issue)`.
49
+ // stranded `stranded.mjs`. A `resume` entry whose ticket has no ledger row
50
+ // yet is pending: `NEED resume #<n> <worktree> <branch>`. A
51
+ // `report` entry is printed. Resuming a claim writes its row, so
52
+ // a re-run after the dispatch finds the need met.
53
+ // shortlist `shortlist.mjs`. A non-zero exit fails the step.
54
+ //
55
+ // THE RECEIPT is `.fleet/phase0.json` (resolved through fleet-dir.mjs, so a
56
+ // worktree and the main checkout name one file):
57
+ //
58
+ // { run, session, scratch, caps: {impl, review},
59
+ // steps: { <name>: { status, at, output } } }
60
+ //
61
+ // `status` is ok, pending, failed, drift or unanswered; `at` an ISO time;
62
+ // `output` the step's own words — the refusal for a failure, the NEED lines for
63
+ // a pending step. Every step name is always present: one not yet run is
64
+ // `pending`. `run` is the `at` that `ledger.mjs rotate` wrote into the
65
+ // controller record in `.fleet/heartbeat.json`, null until the rotation has
66
+ // run. `session` is `--session`, the one place the controller states its
67
+ // session directory. `scratch` is what `scratch-root.mjs` prints. `caps` are
68
+ // the flags as given, null for one not given: the tick's own defaults apply to
69
+ // a cap nobody set, and a number here would pretend somebody had.
70
+ //
71
+ // RESUMABLE. A re-run reads the receipt. It continues it — skipping every `ok`
72
+ // step and re-running the rest — only when its `run` equals the `at` the
73
+ // controller record holds now AND its `session` equals `--session`. That
74
+ // comparison alone does not separate two runs before the new run's rotation:
75
+ // the record still carries the previous run's `at` until `ledger.mjs rotate`
76
+ // writes the next, so the previous run's receipt still matches it, and a
77
+ // re-run that trusted `run` alone would skip `rotate`. The session rule closes
78
+ // a new session. A receipt of any other run (a `run` that is set and does not
79
+ // match, or a different session) is held as `phase0.previous.json` and the
80
+ // script starts at `branch`; this run's own receipt is written at
81
+ // `phase0.json` after every step. A receipt whose rotation has not run is
82
+ // simply overwritten: every step before `rotate` is idempotent and is re-run
83
+ // on every invocation until the rotation has run.
84
+ //
85
+ // THE ROTATION FILES THE PREVIOUS RUN'S RECEIPT. The `rotate` step takes this
86
+ // run's partial receipt off disk, puts the held one back at `phase0.json` and
87
+ // runs `ledger.mjs rotate`, which archives it as `phase0.<UTC stamp>.json`
88
+ // beside `ledger.<UTC stamp>.md`, one stamp for both. A rotation that refused
89
+ // leaves it held for the next attempt; one that archived the ledger and not
90
+ // the receipt files it under a stamp of this script's own. With no omp process
91
+ // above the run the rotation writes no controller record and `run` stays
92
+ // null; a receipt whose `rotate` step is `ok` is then resumed on the session
93
+ // alone.
94
+ //
95
+ // A step that fails stops the script; so does a pending one — nothing after
96
+ // the recipe may run before it is proven, and the shortlist waits for the
97
+ // stranded claims. The steps after it stay `pending` in the receipt.
98
+ //
99
+ // `--workflow` and `--declare-no-ci` name how a repository without a workflow
100
+ // called CI runs. `preflight.mjs` takes no arguments, so they are not passed
101
+ // to it: they are checked against each other here and recorded in the
102
+ // `preflight` step's output.
103
+ //
104
+ // One `phase0: <step>: <status> …` line per step on stdout, a step's further
105
+ // output indented under it when there is any, and the `NEED` lines last.
106
+ // Exit 0 every step ok, or `drift`/`unanswered` — advice the run goes on past;
107
+ // 2 a usage error; 3 a step failed, or the run could not start or record itself
108
+ // (the scratch root, the controller record, the receipt); 4 a step needs the
109
+ // controller — dispatch what the NEED line names, then run this again. A NEED
110
+ // the fold-in prints is advice too and changes no exit status: a PR with no
111
+ // issue behind it is the controller's to queue or leave, and nothing a re-run
112
+ // could meet.
113
+ //
114
+ // Zero deps: node builtins and sibling scripts only.
115
+
116
+ import { spawn, spawnSync } from "node:child_process";
117
+ import { existsSync, mkdirSync, readFileSync, realpathSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
118
+ import { dirname, join, resolve } from "node:path";
119
+ import { fileURLToPath } from "node:url";
120
+ import { makeDie, defineFlags, isDigits } from "./arg.mjs";
121
+ import { FleetDirUnresolvable, fleetFile } from "./fleet-dir.mjs";
122
+ import { readState, stateFileIn } from "./fleet-state.mjs";
123
+ import { gitEnv } from "./git-env.mjs";
124
+ import { isCLI } from "./is-cli.mjs";
125
+ import { RECEIPT, archiveStamp, receiptArchiveName, rowNums } from "./ledger-grammar.mjs";
126
+
127
+ const NAME = "phase0";
128
+ const SCRIPTS = dirname(fileURLToPath(import.meta.url));
129
+ const MAX_BUFFER = 64 * 1024 * 1024;
130
+ const NETWORK_TIMEOUT_MS = 120_000;
131
+ const PR_LIMIT = 1000;
132
+ // The tail of a child's output is the part that says why it stopped.
133
+ const OUTPUT_MAX = 16_000;
134
+ // The previous run's receipt, set aside at startup until the rotation files it.
135
+ const HELD = "phase0.previous.json";
136
+ const RULESET_SPEC = join(".github", "rulesets", "main.json");
137
+ const RULESET_CHECK = join(".github", "scripts", "apply-ruleset.sh");
138
+
139
+ const script = (name) => join(SCRIPTS, name);
140
+
141
+ function how(r) {
142
+ if (r.error) return r.error.code ?? r.error.message;
143
+ if (r.signal) return `killed by ${r.signal}`;
144
+ return `exit ${r.status}`;
145
+ }
146
+
147
+ const trimmed = (s) => String(s ?? "").trim();
148
+ const lines = (s) => trimmed(s).split("\n").filter((l) => l.trim() !== "");
149
+
150
+ /**
151
+ * Runs a child to completion from `cwd`. `status` is the exit status, null
152
+ * when the child never ran or was killed (`how` says which); `text` is its
153
+ * stdout then stderr, trimmed.
154
+ */
155
+ function run(cmd, args, { cwd, env, timeoutMs } = {}) {
156
+ const r = spawnSync(cmd, args, {
157
+ cwd, encoding: "utf8", maxBuffer: MAX_BUFFER, env: gitEnv(env), timeout: timeoutMs,
158
+ });
159
+ const stdout = trimmed(r.stdout);
160
+ const stderr = trimmed(r.stderr);
161
+ return { status: r.error ? null : r.status, stdout, stderr, text: [stdout, stderr].filter(Boolean).join("\n"), how: how(r) };
162
+ }
163
+
164
+ const node = (name, args, opts) => run(process.execPath, [script(name), ...args], opts);
165
+ const sh = (name, args, opts) => run("sh", [script(name), ...args], opts);
166
+
167
+ const ok = (output) => ({ status: "ok", output });
168
+ const failed = (output) => ({ status: "failed", output });
169
+ const withText = (head, r) => (r.text ? `${head}\n${r.text}` : head);
170
+ const refusal = (what, r) => failed(withText(`${what} ${r.how}`, r));
171
+
172
+ // ── the steps ────────────────────────────────────────────────────────────────
173
+
174
+ function branch({ cwd }) {
175
+ const r = run("git", ["symbolic-ref", "-q", "HEAD"], { cwd });
176
+ if (r.status === 0 && r.stdout === "refs/heads/main") return ok("HEAD is refs/heads/main");
177
+ const now = r.status === 0 ? r.stdout : r.status === 1 ? "detached" : `unreadable (${r.how})`;
178
+ return failed(`HEAD is ${now}, not refs/heads/main — switch the checkout to main before the run starts`);
179
+ }
180
+
181
+ function fastForward({ cwd }) {
182
+ const fetch = run("git", ["fetch", "origin"], { cwd, timeoutMs: NETWORK_TIMEOUT_MS });
183
+ if (fetch.status !== 0) return refusal("git fetch origin failed:", fetch);
184
+ const count = run("git", ["rev-list", "--count", "main..origin/main"], { cwd });
185
+ if (count.status !== 0 || !isDigits(count.stdout)) return refusal("git rev-list --count main..origin/main failed:", count);
186
+ const behind = Number(count.stdout);
187
+ if (behind === 0) return ok("main is level with origin/main");
188
+ const merge = run("git", ["merge", "--ff-only", "origin/main"], { cwd });
189
+ if (merge.status !== 0) return refusal("git merge --ff-only origin/main failed:", merge);
190
+ return failed(`SUPERSEDED ${behind} commits; re-run phase0.mjs`);
191
+ }
192
+
193
+ function pin({ cwd, pluginCheckout }) {
194
+ const r = sh("instruments.sh", ["--pin", ...(pluginCheckout ? ["--audit", pluginCheckout] : [])], { cwd });
195
+ if (r.status !== 0) return refusal("instruments.sh --pin failed:", r);
196
+ return ok(pluginCheckout ? `instruments pinned, auditing ${pluginCheckout}` : "instruments pinned");
197
+ }
198
+
199
+ function baseline({ cwd }) {
200
+ const r = node("main-checkout.mjs", ["--record"], { cwd });
201
+ if (r.status !== 0) return refusal("main-checkout.mjs --record refused:", r);
202
+ return ok("main-checkout baseline recorded");
203
+ }
204
+
205
+ function preflight({ cwd, ci }) {
206
+ const r = node("preflight.mjs", [], { cwd });
207
+ if (r.status !== 0) {
208
+ // Exit 1 is a row that failed, and its FAIL, WARN and PREFLIGHT lines are
209
+ // the report; exit 2 is a script that could not run, whose own words are
210
+ // all there is, as are an exit 1 that printed none of those lines.
211
+ const fails = r.status === 1 ? lines(r.text).filter((l) => /^(FAIL|WARN|PREFLIGHT)/.test(l)) : [];
212
+ const keep = fails.length > 0 ? fails : lines(r.text);
213
+ return failed([`preflight.mjs ${r.how}`, ...keep].join("\n"));
214
+ }
215
+ return ok([`preflight.mjs passed (${lines(r.stdout).at(-1) ?? "no output"})`, ...(ci === null ? [] : [ci])].join("\n"));
216
+ }
217
+
218
+ function ruleset({ cwd }) {
219
+ if (!existsSync(join(cwd, RULESET_SPEC))) return ok(`no ruleset spec at ${RULESET_SPEC} — nothing to check`);
220
+ const r = run("bash", [join(cwd, RULESET_CHECK), "--check"], { cwd, timeoutMs: NETWORK_TIMEOUT_MS });
221
+ if (r.status === 0) return ok("the live gate matches the ruleset spec");
222
+ if (r.status === 3) return { status: "drift", output: withText("the live gate differs from the ruleset spec", r) };
223
+ return { status: "unanswered", output: withText(`apply-ruleset.sh --check could not answer (${r.how})`, r) };
224
+ }
225
+
226
+ function tierRoles({ cwd }) {
227
+ const r = node("tier-roles.mjs", ["--check"], { cwd });
228
+ if (r.status !== 0) return refusal("tier-roles.mjs --check failed:", r);
229
+ return ok("every tier route resolves");
230
+ }
231
+
232
+ function metrics({ cwd }) {
233
+ const r = node("metrics-refresh.mjs", [], { cwd });
234
+ if (r.status !== 0) return refusal("metrics-refresh.mjs failed:", r);
235
+ return ok(withText("metrics-refresh.mjs ran", r));
236
+ }
237
+
238
+ function cockpit({ cwd, spendDir }) {
239
+ const args = [script("board.mjs"), "serve", "--open", ...(spendDir ? ["--spend-dir", spendDir] : [])];
240
+ let child;
241
+ try {
242
+ child = spawn(process.execPath, args, { cwd, detached: true, stdio: "ignore", env: gitEnv() });
243
+ } catch (e) {
244
+ return ok(`not launched: ${e.message}`);
245
+ }
246
+ // A launch that fails does so after this returns; nobody reads the event.
247
+ child.on("error", () => {});
248
+ child.unref();
249
+ return ok(child.pid === undefined ? "not launched" : `launched board.mjs serve, pid ${child.pid}`);
250
+ }
251
+
252
+ function rotate(ctx) {
253
+ // This run's own partial receipt is off disk while the rotation runs, so the
254
+ // archive cannot file it under the previous run's stamp. What the rotation
255
+ // finds beside the ledger is the previous run's receipt, held since startup.
256
+ rmSync(ctx.receiptPath, { force: true });
257
+ const restored = existsSync(ctx.held);
258
+ if (restored) renameSync(ctx.held, ctx.receiptPath);
259
+ const r = node("ledger.mjs", ["rotate"], { cwd: ctx.cwd });
260
+ if (restored && existsSync(ctx.receiptPath)) {
261
+ // Not archived. After a refusal it waits, held, for the next attempt; after
262
+ // a rotation that moved the ledger and not the receipt it is filed here,
263
+ // unless that name is taken.
264
+ const filed = join(ctx.fleetDir, receiptArchiveName(archiveStamp()));
265
+ renameSync(ctx.receiptPath, r.status === 0 && !existsSync(filed) ? filed : ctx.held);
266
+ }
267
+ if (r.status !== 0) return refusal("ledger.mjs rotate refused:", r);
268
+ const { controller } = readState(stateFileIn(ctx.fleetDir), NAME);
269
+ ctx.receipt.run = controller?.at ?? null;
270
+ const head = ctx.receipt.run === null ? "ledger rotated; no omp process above this run, so no controller record — a re-run is keyed on the session alone" : `ledger rotated, run ${ctx.receipt.run}`;
271
+ // What rotate says on stderr is the only account of rows it did not carry
272
+ // and a receipt it could not archive.
273
+ return ok([head, ...lines(r.stderr).map((l) => l.trim())].join("\n"));
274
+ }
275
+
276
+ function reclaim({ cwd, scratch }) {
277
+ const r = node("reclaim-scratch.mjs", ["--root", scratch, "--after-rotate", "--apply"], { cwd });
278
+ return ok(r.status === 0 ? "scratch reclaimed" : withText(`reclaim-scratch.mjs ${r.how}, not enforced`, r));
279
+ }
280
+
281
+ function recipe({ cwd }) {
282
+ const reads = [["install"], ["test", "--at", "origin/main"]].map((a) => ({ a, r: sh("derive-testcmd.sh", [".", ...a], { cwd }) }));
283
+ const broken = reads.filter(({ r }) => r.status === 3 || (r.status !== 0 && r.status !== 1));
284
+ if (broken.length > 0) {
285
+ return failed(broken.map(({ a, r }) => withText(`derive-testcmd.sh . ${a.join(" ")} ${r.how} — the cache's state is unknown, and deriving would not help`, r)).join("\n"));
286
+ }
287
+ const refused = reads.filter(({ r }) => r.status === 1);
288
+ if (refused.length > 0) {
289
+ return { status: "pending", output: [...refused.map(({ a, r }) => withText(`derive-testcmd.sh . ${a.join(" ")} refused:`, r)), "NEED derive-recipe"].join("\n") };
290
+ }
291
+ return ok(`Recipe cache valid: ${reads.map(({ r }) => lines(r.stdout).at(-1) ?? "").join(" ; ")}`);
292
+ }
293
+
294
+ // The ledger's rows, read through `ledger.mjs read` so the ledger keeps one
295
+ // parser. `{ error }` when the read did not answer, `{ rows }` otherwise.
296
+ function ledgerRows(cwd) {
297
+ const r = node("ledger.mjs", ["read"], { cwd });
298
+ if (r.status !== 0) return { error: withText(`ledger.mjs read ${r.how}`, r) };
299
+ try {
300
+ const { rows } = JSON.parse(r.stdout);
301
+ if (Array.isArray(rows) && rows.every((x) => typeof x === "string")) return { rows };
302
+ } catch {
303
+ // Falls through to the refusal below.
304
+ }
305
+ return { error: "ledger.mjs read printed no {rows}" };
306
+ }
307
+
308
+ const rowKey = (row) => row.split(/\s/)[0];
309
+
310
+ function foldIn({ cwd }) {
311
+ const list = run("gh", ["pr", "list", "--state", "open", "--json", "number,closingIssuesReferences", "--limit", String(PR_LIMIT)], {
312
+ cwd, env: { GH_REPO: "" }, timeoutMs: NETWORK_TIMEOUT_MS,
313
+ });
314
+ if (list.status !== 0) return refusal("gh pr list failed:", list);
315
+ let prs;
316
+ try {
317
+ prs = JSON.parse(list.stdout);
318
+ } catch (e) {
319
+ return failed(`could not parse gh pr list: ${e.message}`);
320
+ }
321
+ if (!Array.isArray(prs)) return failed("gh pr list did not print a JSON array");
322
+ if (prs.length >= PR_LIMIT) return failed(`gh pr list came back at its cap of ${PR_LIMIT} — it may hold more PRs than it showed, so none is folded in`);
323
+ const ledger = ledgerRows(cwd);
324
+ if (ledger.error) return failed(`${ledger.error} — which PRs a carried row names is unknown`);
325
+
326
+ // A PR a carried row names is queued by that row already, and a `row` write
327
+ // replaces the whole line, wiping the live tokens carried on it.
328
+ const named = new Set();
329
+ for (const row of ledger.rows) {
330
+ const { keyNum, pr } = rowNums(row);
331
+ if (keyNum !== null) named.add(keyNum);
332
+ if (pr !== null) named.add(pr);
333
+ }
334
+ const queued = [];
335
+ const advice = [];
336
+ let carried = 0;
337
+ for (const pr of [...prs].sort((a, b) => a.number - b.number)) {
338
+ if (named.has(pr.number)) {
339
+ carried += 1;
340
+ continue;
341
+ }
342
+ const issues = (pr.closingIssuesReferences ?? []).map((i) => i.number).filter(Number.isInteger);
343
+ if (issues.length === 0) {
344
+ advice.push(`NEED review PR#${pr.number} (closes no issue)`);
345
+ continue;
346
+ }
347
+ const closes = `closes ${issues.map((n) => `#${n}`).join(", ")}`;
348
+ const w = node("ledger.mjs", ["row", String(pr.number), `PR#${pr.number} · inherited (${closes})`], { cwd });
349
+ if (w.status !== 0) return refusal(`ledger.mjs row ${pr.number} failed:`, w);
350
+ queued.push(`queued PR#${pr.number} for review (${closes})`);
351
+ }
352
+ const head = `${queued.length} PR(s) queued, ${carried} already on a carried row, ${advice.length} closing no issue`;
353
+ return ok([head, ...queued, ...advice].join("\n"));
354
+ }
355
+
356
+ function stranded({ cwd }) {
357
+ const r = node("stranded.mjs", [], { cwd });
358
+ if (r.status !== 0) return refusal("stranded.mjs could not answer:", r);
359
+ let verdict;
360
+ try {
361
+ verdict = JSON.parse(r.stdout);
362
+ } catch (e) {
363
+ return failed(`could not parse stranded.mjs output: ${e.message}`);
364
+ }
365
+ if (!Array.isArray(verdict?.claims) || !Array.isArray(verdict?.carried)) return failed("stranded.mjs printed no {claims, carried}");
366
+ const ledger = ledgerRows(cwd);
367
+ if (ledger.error) return failed(`${ledger.error} — which claims are already resumed is unknown`);
368
+ const rowed = new Set(ledger.rows.map(rowKey));
369
+
370
+ const body = [];
371
+ const needs = [];
372
+ for (const c of verdict.claims) {
373
+ if (c.action !== "resume") body.push(`report #${c.n} — ${c.why}`);
374
+ else if (rowed.has(`#${c.n}`)) body.push(`#${c.n} resumed: its ledger row is there`);
375
+ else needs.push(`NEED resume #${c.n} ${c.worktree} ${c.branch}`);
376
+ }
377
+ for (const t of verdict.carried) body.push(`carried ${t.name} ${t.token} (row ${t.row ?? "none"})`);
378
+ const head = `prior ${verdict.prior}: ${verdict.claims.length} claim(s), ${verdict.carried.length} live token(s) carried`;
379
+ return { status: needs.length > 0 ? "pending" : "ok", output: [head, ...body, ...needs].join("\n") };
380
+ }
381
+
382
+ function shortlist({ cwd }) {
383
+ const r = node("shortlist.mjs", [], { cwd });
384
+ if (r.status !== 0) return refusal("shortlist.mjs refused:", r);
385
+ return ok("shortlist built");
386
+ }
387
+
388
+ const STEPS = {
389
+ "branch": branch, "fast-forward": fastForward, "pin": pin, "baseline": baseline, "preflight": preflight,
390
+ "ruleset": ruleset, "tier-roles": tierRoles, "metrics": metrics, "cockpit": cockpit, "rotate": rotate,
391
+ "reclaim": reclaim, "recipe": recipe, "fold-in": foldIn, "stranded": stranded, "shortlist": shortlist,
392
+ };
393
+
394
+ const STEP_NAMES = Object.keys(STEPS);
395
+
396
+ // ── the receipt ──────────────────────────────────────────────────────────────
397
+
398
+ // Null when there is no receipt; a receipt that is there and cannot be read or
399
+ // is not one throws, naming the file and why.
400
+ function readReceipt(path) {
401
+ let text;
402
+ try {
403
+ text = readFileSync(path, "utf8");
404
+ } catch (e) {
405
+ if (e.code === "ENOENT") return null;
406
+ throw new Error(`cannot read the receipt ${path} (${e.code ?? e.message}) — move it aside and re-run`);
407
+ }
408
+ let r;
409
+ try {
410
+ r = JSON.parse(text);
411
+ } catch (e) {
412
+ throw new Error(`the receipt ${path} is not JSON (${e.message}) — move it aside and re-run`);
413
+ }
414
+ const record = (x) => x !== null && typeof x === "object" && !Array.isArray(x);
415
+ if (!(record(r) && record(r.steps) && Object.hasOwn(r, "run") && typeof r.session === "string")) {
416
+ throw new Error(`the receipt ${path} has no steps, run and session — move it aside and re-run`);
417
+ }
418
+ return r;
419
+ }
420
+
421
+ function writeReceipt(path, receipt) {
422
+ const tmp = `${path}.${process.pid}.tmp`;
423
+ mkdirSync(dirname(path), { recursive: true });
424
+ writeFileSync(tmp, `${JSON.stringify(receipt, null, 2)}\n`);
425
+ renameSync(tmp, path);
426
+ }
427
+
428
+ const capped = (s) => (s.length > OUTPUT_MAX ? `…${s.slice(-(OUTPUT_MAX - 1))}` : s);
429
+
430
+ function main() {
431
+ const die = makeDie(NAME);
432
+ const F = defineFlags(die, {
433
+ flags: {
434
+ session: "value", "implementer-cap": "value", "reviewer-cap": "value", "plugin-checkout": "value",
435
+ "spend-dir": "value", workflow: "value", "declare-no-ci": "bool",
436
+ },
437
+ });
438
+ F.sweep();
439
+ F.stray();
440
+ const absent = (name) => F.arg(name) === null;
441
+ if (absent("session")) die("--session is required: the controller's session directory, `~/.omp/agent/sessions/<encoded-cwd>/<ISO>_<uuid>`");
442
+ const session = resolve(F.arg("session"));
443
+ try {
444
+ if (!statSync(session).isDirectory()) die(`--session ${session} is not a directory`);
445
+ } catch (e) {
446
+ if (e.code === undefined) throw e;
447
+ die(`--session ${session} is not readable (${e.code})`);
448
+ }
449
+ const positive = (name) => {
450
+ const n = F.numArg(name);
451
+ if (n !== null && n < 1) die(`--${name} needs a positive number, got ${n}`);
452
+ return n;
453
+ };
454
+ const caps = { impl: positive("implementer-cap"), review: positive("reviewer-cap") };
455
+ if (F.has("declare-no-ci") && !absent("workflow")) die("--workflow and --declare-no-ci are alternatives, not a pair");
456
+ const ci = F.has("declare-no-ci") ? "CI: declared absent (--declare-no-ci)" : absent("workflow") ? null : `CI: workflow ${F.arg("workflow")} (--workflow)`;
457
+ const pathOf = (name) => (absent(name) ? null : resolve(F.arg(name)));
458
+ const pluginCheckout = pathOf("plugin-checkout");
459
+ const spendDir = pathOf("spend-dir");
460
+
461
+ let fleetDir;
462
+ try {
463
+ fleetDir = fleetFile(null);
464
+ } catch (e) {
465
+ if (!(e instanceof FleetDirUnresolvable)) throw e;
466
+ die(e.message);
467
+ }
468
+ const cwd = dirname(fleetDir);
469
+ const top = run("git", ["rev-parse", "--show-toplevel"], { cwd: process.cwd() });
470
+ if (top.status !== 0 || realpathSync(top.stdout) !== realpathSync(cwd)) {
471
+ die(`run this from the main checkout ${cwd}${top.status === 0 ? `, not ${top.stdout}` : ""}`);
472
+ }
473
+ const root = node("scratch-root.mjs", [], { cwd });
474
+ if (root.status !== 0) {
475
+ process.stderr.write(`${NAME}: scratch-root.mjs ${root.how}\n${root.text}\n`);
476
+ process.exitCode = 3;
477
+ return;
478
+ }
479
+ const scratch = root.stdout;
480
+
481
+ const receiptPath = join(fleetDir, RECEIPT);
482
+ const held = join(fleetDir, HELD);
483
+ const state = readState(stateFileIn(fleetDir), NAME);
484
+ if (state.degraded) {
485
+ // The record decides whether a receipt is the previous run's, and a record
486
+ // that could not be read says nothing about that.
487
+ process.stderr.write(`${NAME}: ${stateFileIn(fleetDir)} ${state.degraded} — repair it so the controller record is intact; a receipt is not set aside on a record that could not be read\n`);
488
+ process.exitCode = 3;
489
+ return;
490
+ }
491
+ const record = state.controller;
492
+ const now = new Date().toISOString();
493
+ let previous;
494
+ try {
495
+ previous = readReceipt(receiptPath);
496
+ } catch (e) {
497
+ // A receipt that is there and cannot be used may record a rotation already
498
+ // done; starting over would run it a second time.
499
+ process.stderr.write(`${NAME}: ${e.message}\n`);
500
+ process.exitCode = 3;
501
+ return;
502
+ }
503
+ // A receipt belongs to a rotated run when the rotation wrote a controller
504
+ // record it can be keyed on, or, with no omp process above the run to write
505
+ // one, when its rotation is recorded `ok`. Either way it is this run's only
506
+ // if the record now holds the `at` it names and the session is the same.
507
+ const rotated = previous !== null && (previous.run !== null || previous.steps.rotate?.status === "ok");
508
+ const resumed = rotated && previous.run === (record?.at ?? null) && previous.session === session;
509
+ if (rotated && !resumed) {
510
+ if (existsSync(held)) {
511
+ process.stderr.write(`${NAME}: cannot hold the previous run's receipt for the rotation: ${held} already holds another\n`);
512
+ process.exitCode = 3;
513
+ return;
514
+ }
515
+ renameSync(receiptPath, held);
516
+ }
517
+
518
+ const receipt = { run: resumed ? previous.run : null, session, scratch, caps, steps: {} };
519
+ for (const name of STEP_NAMES) {
520
+ const was = resumed ? previous.steps[name] : undefined;
521
+ receipt.steps[name] = was?.status === "ok" ? was : { status: "pending", at: now, output: "not run" };
522
+ }
523
+
524
+ const ctx = { cwd, fleetDir, held, receipt, receiptPath, scratch, pluginCheckout, spendDir, ci };
525
+ const say = (line) => process.stdout.write(`${line}\n`);
526
+ let stop = null;
527
+ for (const name of STEP_NAMES) {
528
+ if (receipt.steps[name].status === "ok") {
529
+ say(`phase0: ${name}: ok (done earlier in this run)`);
530
+ continue;
531
+ }
532
+ let result;
533
+ try {
534
+ result = STEPS[name](ctx);
535
+ } catch (e) {
536
+ result = failed(`${name} threw: ${e.message}`);
537
+ }
538
+ receipt.steps[name] = { status: result.status, at: new Date().toISOString(), output: capped(result.output) };
539
+ try {
540
+ writeReceipt(receiptPath, receipt);
541
+ } catch (e) {
542
+ say(`phase0: ${name}: ${result.status}`);
543
+ process.stderr.write(`${NAME}: cannot write ${receiptPath}: ${e.message}\n`);
544
+ process.exitCode = 3;
545
+ return;
546
+ }
547
+ const [head, ...rest] = lines(result.output).filter((l) => !l.startsWith("NEED "));
548
+ say(`phase0: ${name}: ${result.status}${head ? ` — ${head}` : ""}`);
549
+ for (const l of rest) say(` ${l}`);
550
+ if (result.status === "failed" || result.status === "pending") {
551
+ stop = result.status;
552
+ break;
553
+ }
554
+ }
555
+
556
+ for (const name of STEP_NAMES) {
557
+ for (const l of lines(receipt.steps[name].output)) if (l.startsWith("NEED ")) say(l);
558
+ }
559
+ process.exitCode = stop === "failed" ? 3 : stop === "pending" ? 4 : 0;
560
+ }
561
+
562
+ if (isCLI(import.meta.url)) main();
@@ -24,6 +24,17 @@
24
24
  // removed when issue #N is CLOSED, or OPEN with no live
25
25
  // impl-<N>[-x] token and idle past the retention, so a bailed or
26
26
  // released ticket's partition is not inherited by its next claim.
27
+ // reports/<member>.json
28
+ // a member's report file. `reports/` itself is never removed, and
29
+ // is not aged out whole: each file is judged on its own, by the
30
+ // entry it goes with. A fix-pr-<M>[-x], finisher-pr-<M>[-x] or
31
+ // review-pr-<M>[-x] report goes with pr<M>/ — the same verdict,
32
+ // the same live-token holds, though no pr<M>/ need exist; an
33
+ // impl-<N>[-x] report goes with impl-<N>/ the same way; any other
34
+ // file in it (a merge-bot-<n> report, which names no PR and no
35
+ // ticket) is judged as "anything else" below, so it is removed
36
+ // only when idle past the retention and its member's token is not
37
+ // live.
27
38
  // anything else, and a keyed name whose number GitHub resolves to the
28
39
  // other type (a pr<N> that is an issue, an impl-<N> that is a PR) or to
29
40
  // nothing
@@ -114,6 +125,11 @@ const REMOVE = [
114
125
  const PR_ENTRY = /^pr([1-9][0-9]*)$/;
115
126
  const IMPL_ENTRY = /^impl-([1-9][0-9]*)$/;
116
127
  const DISPOSITIONS_ENTRY = /^dispositions-fix-pr-([1-9][0-9]*)(?:-[a-z])?\.json$/;
128
+ // A member's report file, `reports/<member>.json`: one that names a PR goes
129
+ // with that PR's entries, one that names a ticket with that ticket's.
130
+ const REPORTS_DIR = "reports";
131
+ const REPORT_PR_ENTRY = /^(?:fix-pr|finisher-pr|review-pr)-([1-9][0-9]*)(?:-[a-z])?\.json$/;
132
+ const REPORT_IMPL_ENTRY = /^impl-([1-9][0-9]*)(?:-[a-z])?\.json$/;
117
133
  const RUN_ROOT = /^run-/;
118
134
 
119
135
  function how(r) {
@@ -279,12 +295,50 @@ function judgeRoot(root, names, live, now) {
279
295
  kept.push({ path, reason: young ? `${young}; modified within 7 days` : "modified within 7 days" });
280
296
  };
281
297
 
282
- const keyed = [];
283
- const numbers = new Set();
298
+ // `reports/` is not an entry to age out whole: each file in it is judged as
299
+ // the member's own, so a live member's report is never swept along with an
300
+ // idle directory.
301
+ const entries = [];
284
302
  for (const name of names) {
285
303
  const path = join(root, name);
304
+ let isDir = false;
305
+ try {
306
+ isDir = name === REPORTS_DIR && lstatSync(path).isDirectory();
307
+ } catch (e) {
308
+ // raced away: judged below as any other entry. Anything else (an
309
+ // unsearchable root) leaves `reports` unclassified, and judging it as an
310
+ // ordinary entry would age the whole directory out.
311
+ if (e.code !== "ENOENT") {
312
+ kept.push({ path, reason: `its type cannot be read: ${e.message}` });
313
+ continue;
314
+ }
315
+ }
316
+ if (!isDir) {
317
+ entries.push({ path, name, report: false });
318
+ continue;
319
+ }
320
+ let inner;
321
+ try {
322
+ inner = readdirSync(path).sort();
323
+ } catch (e) {
324
+ kept.push({ path, reason: `its entries cannot be listed: ${e.message}` });
325
+ continue;
326
+ }
327
+ for (const f of inner) entries.push({ path: join(path, f), name: f, report: true });
328
+ }
329
+
330
+ const keyed = [];
331
+ const numbers = new Set();
332
+ for (const { path, name, report } of entries) {
286
333
  let m;
287
- if ((m = PR_ENTRY.exec(name))) keyed.push({ path, name, kind: "pr", n: Number(m[1]) });
334
+ if (report) {
335
+ if ((m = REPORT_PR_ENTRY.exec(name))) keyed.push({ path, name, kind: "pr", n: Number(m[1]) });
336
+ else if ((m = REPORT_IMPL_ENTRY.exec(name))) keyed.push({ path, name, kind: "impl", n: Number(m[1]) });
337
+ else {
338
+ byAge(path, name, null);
339
+ continue;
340
+ }
341
+ } else if ((m = PR_ENTRY.exec(name))) keyed.push({ path, name, kind: "pr", n: Number(m[1]) });
288
342
  else if ((m = IMPL_ENTRY.exec(name))) keyed.push({ path, name, kind: "impl", n: Number(m[1]) });
289
343
  else if ((m = DISPOSITIONS_ENTRY.exec(name))) keyed.push({ path, name, kind: "pr", n: Number(m[1]) });
290
344
  else {
@@ -0,0 +1,94 @@
1
+ // Where a member's report file lives and what state it is in: the reading
2
+ // side of report-schema.mjs, shared by the scripts that look a report up
3
+ // rather than write one.
4
+ //
5
+ // The readers here take a member's report at `<scratch>/reports/<member>.json`
6
+ // (flat: no per-run directory), `<member>` being its ledger name (`impl-412`,
7
+ // `impl-412-b`, `fix-pr-88`, `finisher-pr-88`, `merge-bot-3`). The file is
8
+ // checked against the schema of the member's family, report-schema.mjs's
9
+ // SCHEMAS. The review runner and every member outside those four families
10
+ // have no schema, so no file to look up.
11
+ //
12
+ // This module only looks files up: it writes none, and creates no scratch
13
+ // root.
14
+
15
+ import { readdirSync, readFileSync } from "node:fs";
16
+ import { join } from "node:path";
17
+ import { SCHEMAS, validate } from "./report-schema.mjs";
18
+ import { parseMember } from "./ledger-grammar.mjs";
19
+ import { scratchRoot } from "./fleet-dir.mjs";
20
+
21
+ // The ledger's family name to report-schema.mjs's.
22
+ const SCHEMA_FAMILY = { impl: "impl", "fix-pr": "fix-pr", "finisher-pr": "finisher", "merge-bot": "merge-bot" };
23
+
24
+ /** The report-schema family of a ledger member name, or null for a name no
25
+ * schema covers (a specialist, a review runner, a memory member). */
26
+ export function reportFamily(member) {
27
+ const m = parseMember(member);
28
+ return m === null ? null : SCHEMA_FAMILY[m.family];
29
+ }
30
+
31
+ export const reportsDir = (scratch) => join(scratch, "reports");
32
+
33
+ export const reportPath = (scratch, member) => join(reportsDir(scratch), `${member}.json`);
34
+
35
+ /** The report file of `member` under `scratch`, read and checked against its
36
+ * family's schema: `{state: "ok", path, report}`, `{state: "invalid", path,
37
+ * violation}` for a file that exists and is unreadable, not JSON, or the
38
+ * wrong shape, or `{state: "absent", path}`. Throws on a member no schema
39
+ * covers: ask reportFamily() first. */
40
+ export function readReport(scratch, member) {
41
+ const family = reportFamily(member);
42
+ if (family === null) throw new Error(`no report schema for '${member}'`);
43
+ const path = reportPath(scratch, member);
44
+ let text;
45
+ try {
46
+ text = readFileSync(path, "utf8");
47
+ } catch (e) {
48
+ if (e.code === "ENOENT" || e.code === "ENOTDIR") return { state: "absent", path };
49
+ return { state: "invalid", path, violation: `$: unreadable (${e.code ?? e.message})` };
50
+ }
51
+ let report;
52
+ try {
53
+ report = JSON.parse(text);
54
+ } catch (e) {
55
+ return { state: "invalid", path, violation: `$: not JSON (${e.message})` };
56
+ }
57
+ const violation = validate(SCHEMAS[family], report);
58
+ return violation === null ? { state: "ok", path, report } : { state: "invalid", path, violation };
59
+ }
60
+
61
+ /** `ok`, `invalid`, `absent`, or `-` when no schema covers the member. */
62
+ export function reportStatus(scratch, member) {
63
+ return reportFamily(member) === null ? "-" : readReport(scratch, member).state;
64
+ }
65
+
66
+ /** The implementer report of `ticket` that wins: `{member, path}` for
67
+ * `impl-<ticket>` or `impl-<ticket>-<letter>` with the highest retry letter
68
+ * (the name with no letter is the first attempt, so it ranks lowest), or null
69
+ * when none exists. */
70
+ export function latestImplReport(scratch, ticket) {
71
+ let names;
72
+ try {
73
+ names = readdirSync(reportsDir(scratch));
74
+ } catch (e) {
75
+ if (e.code === "ENOENT" || e.code === "ENOTDIR") return null;
76
+ throw e;
77
+ }
78
+ const mine = new RegExp(`^impl-${ticket}(-[a-z])?\\.json$`);
79
+ let best = null;
80
+ for (const name of names) {
81
+ const m = mine.exec(name);
82
+ if (m === null) continue;
83
+ const letter = m[1] ? m[1].slice(1) : "";
84
+ if (best === null || letter > best.letter) best = { letter, member: name.slice(0, -".json".length) };
85
+ }
86
+ return best === null ? null : { member: best.member, path: reportPath(scratch, best.member) };
87
+ }
88
+
89
+ /** The scratch root this repository's members write under: the path
90
+ * scratch-root.mjs prints, computed the same way, but never created. Throws
91
+ * fleet-dir.mjs's FleetDirUnresolvable where no repository is there to key on. */
92
+ export function defaultScratch() {
93
+ return scratchRoot();
94
+ }
@@ -7,8 +7,10 @@
7
7
  //
8
8
  // append <pr> --closed-own-ticket yes|no --minted-false-claim yes|no
9
9
  // --note <text> [--sizing light|heavy] [--profile <p>] [--loc <n>] [--files <n>]
10
+ // [--scratch <dir>]
10
11
  // Fills `run_date` (today's UTC date, the ruling date), `pr`, `ticket` (off
11
- // `gh pr view --json closingIssuesReferences`) and `tier`. Idempotent by
12
+ // `gh pr view --json closingIssuesReferences`), `tier` and, unless
13
+ // `--sizing` names it, `sizing` (see below). Idempotent by
12
14
  // PR: a row already present is named and nothing is written, exit 0.
13
15
  // check [--live]
14
16
  // Post-switch rows only: a filled `tier` must equal the short name of the
@@ -43,6 +45,17 @@
43
45
  // `opus`/`sonnet` spelling and are never checked; the header records the
44
46
  // switch and how the old values map.
45
47
  //
48
+ // `sizing` resolution, in order:
49
+ // 1. `--sizing`, as given — it wins, and stderr names it as the source;
50
+ // 2. else the `sizing.verdict` of the ticket's implementer report,
51
+ // `<scratch>/reports/impl-<ticket>[-x].json` (`--scratch`, default the
52
+ // root scratch-root.mjs prints), the highest retry letter's when several
53
+ // exist — and only when the report checks against its schema, its
54
+ // `sizing.ranBeforePr` is true and its `sizing.signal` is non-empty: a
55
+ // verdict typed after the PR was opened measured nothing, and one with no
56
+ // signal named is a guess;
57
+ // 3. else blank, with a WARNING naming each condition that failed.
58
+ //
46
59
  // A row's position in the file is its order: rows are appended, never inserted
47
60
  // mid-file or reordered. An in-place edit (a re-judged `class`, a backfilled
48
61
  // verdict) keeps the row's position; a ruling meant to supersede an earlier one
@@ -60,6 +73,8 @@ import { makeDie, defineFlags, isDigits } from "./arg.mjs";
60
73
  import { parseTsv as parseMemberTsv } from "./member-outcomes.mjs";
61
74
  import { parseMember, parseToken, memberTokens, rowPr } from "./ledger-grammar.mjs";
62
75
  import { rowText } from "./tier-check.mjs";
76
+ import { latestImplReport, readReport, reportsDir, defaultScratch } from "./report-files.mjs";
77
+ import { FleetDirUnresolvable } from "./fleet-dir.mjs";
63
78
 
64
79
  const NAME = "tier-outcomes";
65
80
  const SCRIPT_DIR = dirname(fileURLToPath(import.meta.url));
@@ -274,6 +289,25 @@ export function resolveTier({ ledger, memberRows, ticket, pr }) {
274
289
  return tierFromLedger(ledger, ticket, pr) ?? tierFromMembers(memberRows, ticket);
275
290
  }
276
291
 
292
+ // The `sizing` covariate off the ticket's implementer report under `scratch`:
293
+ // `{sizing, source, warning: null}`, or `{sizing: "", source: null, warning}`
294
+ // naming every condition that failed. The report must check against its
295
+ // schema first: a verdict read off a malformed file is not a measurement.
296
+ function sizingFromReport(scratch, ticket) {
297
+ const blank = (warning) => ({ sizing: "", source: null, warning });
298
+ const found = latestImplReport(scratch, ticket);
299
+ const report = found === null ? null : readReport(scratch, found.member);
300
+ if (report === null || report.state === "absent") return blank(`no implementer report for ticket #${ticket} under ${reportsDir(scratch)}`);
301
+ if (report.state === "invalid") return blank(`${report.path} does not check against its schema: ${report.violation}`);
302
+ const s = report.report.sizing;
303
+ if (s === null) return blank(`${report.path} carries no sizing (sizing is null)`);
304
+ const failed = [];
305
+ if (s.ranBeforePr !== true) failed.push("sizing.ranBeforePr is not true");
306
+ if (s.signal.trim() === "") failed.push("sizing.signal is empty");
307
+ if (failed.length) return blank(`${report.path}: ${failed.join(" and ")}`);
308
+ return { sizing: s.verdict, source: report.path, warning: null };
309
+ }
310
+
277
311
  // `check` over the committed files. Returns failure lines and skip counts.
278
312
  export function checkRows(rows, memberRows) {
279
313
  const failures = [];
@@ -338,13 +372,13 @@ export function unrecordedReviewedPrs(ledger, rows) {
338
372
  // ---------------------------------------------------------------------------
339
373
 
340
374
  const USAGE = "usage: tier-outcomes.mjs append <pr> --closed-own-ticket yes|no --minted-false-claim yes|no --note <text> "
341
- + "[--sizing light|heavy] [--profile <p>] [--loc <n>] [--files <n>] [--file <tsv>] [--member-outcomes <tsv>] [--ledger <path>]\n"
375
+ + "[--sizing light|heavy] [--profile <p>] [--loc <n>] [--files <n>] [--scratch <dir>] [--file <tsv>] [--member-outcomes <tsv>] [--ledger <path>]\n"
342
376
  + " tier-outcomes.mjs check [--live] [--file <tsv>] [--member-outcomes <tsv>] [--ledger <path>]";
343
377
 
344
378
  const die = makeDie(NAME);
345
379
  const APPEND_FLAGS = {
346
380
  "closed-own-ticket": "value", "minted-false-claim": "value", note: "value",
347
- sizing: "value", profile: "value", loc: "value", files: "value",
381
+ sizing: "value", profile: "value", loc: "value", files: "value", scratch: "value",
348
382
  };
349
383
  const FLAGS = { ...APPEND_FLAGS, file: "value", "member-outcomes": "value", ledger: "value", live: "bool" };
350
384
  const { arg, has, sweep } = defineFlags(die, { flags: FLAGS });
@@ -442,9 +476,10 @@ function append(pr, paths) {
442
476
  if (!isDigits(pr) || Number(pr) === 0) die(`append needs a PR number, got '${pr}'`);
443
477
  pr = String(Number(pr));
444
478
  if (has("live")) die("--live is a check flag");
479
+ const sizingFlag = arg("sizing");
445
480
  const fields = {
446
481
  closed_own_ticket: arg("closed-own-ticket"), minted_false_claim: arg("minted-false-claim"),
447
- note: arg("note"), sizing: arg("sizing") ?? "", profile: arg("profile") ?? "", loc: arg("loc") ?? "", files: arg("files") ?? "",
482
+ note: arg("note"), sizing: sizingFlag ?? "", profile: arg("profile") ?? "", loc: arg("loc") ?? "", files: arg("files") ?? "",
448
483
  };
449
484
  for (const col of ["closed_own_ticket", "minted_false_claim", "note"]) {
450
485
  if (fields[col] === null) die(`append needs --${col.replaceAll("_", "-")}\n${USAGE}`);
@@ -474,6 +509,25 @@ function append(pr, paths) {
474
509
  if (resolved.warning) console.error(`${NAME}: WARNING tier left blank for PR #${pr} (ticket #${ticket}): ${resolved.warning}`);
475
510
  else console.error(`${NAME}: tier=${resolved.tier} from ${resolved.source}`);
476
511
 
512
+ if (sizingFlag !== null) {
513
+ console.error(`${NAME}: sizing=${fields.sizing} from --sizing`);
514
+ } else {
515
+ let scratch = paths.scratch;
516
+ let unresolvable = null;
517
+ if (scratch === null) {
518
+ try {
519
+ scratch = defaultScratch();
520
+ } catch (e) {
521
+ if (!(e instanceof FleetDirUnresolvable)) throw e;
522
+ unresolvable = e.message;
523
+ }
524
+ }
525
+ const sized = unresolvable === null ? sizingFromReport(scratch, ticket) : { sizing: "", source: null, warning: `no scratch root to look in: ${unresolvable}` };
526
+ fields.sizing = sized.sizing;
527
+ if (sized.warning) console.error(`${NAME}: WARNING sizing left blank for PR #${pr} (ticket #${ticket}): ${sized.warning}`);
528
+ else console.error(`${NAME}: sizing=${sized.sizing} from ${sized.source}`);
529
+ }
530
+
477
531
  const line = formatRow({ ...fields, run_date: today(), pr, ticket, tier: resolved.tier });
478
532
  if (text === null) writeFileSync(paths.file, `# ${COLUMNS.join("\t")}\n${line}\n`);
479
533
  else appendFileSync(paths.file, `${text === "" || text.endsWith("\n") ? "" : "\n"}${line}\n`);
@@ -507,6 +561,7 @@ function main() {
507
561
  file: arg("file") ?? "docs/metrics/tier-outcomes.tsv",
508
562
  members: arg("member-outcomes") ?? "docs/metrics/member-outcomes.tsv",
509
563
  ledger: arg("ledger"),
564
+ scratch: arg("scratch"),
510
565
  };
511
566
  // Below every flag read, per arg.mjs's ordering contract, and above the
512
567
  // subcommand checks, so a stray flag is refused by name before anything
@@ -1462,32 +1462,37 @@ untouched, so re-running it is safe. That append is the whole duty.
1462
1462
  The row's last four fields are the ticket's difficulty, and they are what lets a
1463
1463
  tier comparison condition on the thing that swamps it. `sizing` is the
1464
1464
  **member's** own `light`/`heavy` verdict from its phase-2 `sizing-a-ticket`
1465
- run, read back off the PR body's `Sizing:` line (`gh pr view <pr> --json body`),
1466
- which names the signal the verdict turned on beside the verdict itself;
1465
+ run, which `append` reads itself off the member's report file
1466
+ (`<scratch>/reports/impl-<ticket>[-x].json`, its `sizing.verdict`, the highest
1467
+ retry letter's when there are several), so you pass no `--sizing`; the member
1468
+ also puts it in the PR body's `Sizing:` line, with the signal it turned on;
1467
1469
  `profile`, `loc` and `files` all come from `diff-stats.mjs` over the merged
1468
1470
  diff. **Phase 0 does not size anything** — it shortlists, a Pull claims, and
1469
1471
  the sizing run happens inside the member after both, which is why the verdict
1470
- has to travel in the PR body rather than being something you already hold.
1472
+ has to travel in the member's report rather than being something you already hold.
1471
1473
 
1472
1474
  **A verdict is a measurement only if it post-dates the run that produced it.** A
1473
1475
  `Sizing:` line typed in before the skill ever ran is byte-identical to one the
1474
- skill produced, so the body cannot settle this alone and the member's report
1475
- carries the second source: when it ran `sizing-a-ticket` relative to opening the
1476
- PR (measured on a member that wrote the line first, caught itself, and
1477
- corrected it). **Record `sizing` only when the report places that run before the
1478
- PR AND the line carries its signal.** Ordering clause missing, verdict authored
1479
- before the run with no corrected value reported, or body and report naming
1480
- different verdicts — leave the field BLANK and say which in `note`. Do not
1481
- re-derive the verdict from the diff to fill the gap: `sizing-a-ticket` weighs
1482
- more than file count, and a substituted proxy reads as measured too. **A value
1483
- not in hand is left BLANK, never estimated** — blank reads as unknown and drops
1484
- the row from a stratified comparison, while a guess reads as measured and
1485
- poisons one. Blank still means the field is WRITTEN and empty: omit the flag
1486
- (`--sizing`, `--profile`, `--loc`, `--files`) and the script writes that field
1487
- empty at full width, because a row of some in-between width cannot be told
1488
- apart from a shifted one. The `note` field is free text and sits before those
1489
- four, so the script refuses a tab or newline in it: one tab would shift all
1490
- four for that row alone.
1476
+ skill produced, so the body cannot settle this alone, and neither can the
1477
+ member's prose. The report file carries the second source: `sizing.ranBeforePr`,
1478
+ whether `sizing-a-ticket` ran before the PR was opened (measured on a member
1479
+ that wrote the line first, caught itself, and corrected it), and
1480
+ `sizing.signal`, what the verdict turned on. `append` applies the rule itself:
1481
+ it records `sizing` only when `ranBeforePr` is true AND `signal` is non-empty.
1482
+ Any other state — no report file, one that fails its schema, `sizing` null,
1483
+ `ranBeforePr` not true, an empty `signal` — leaves the field BLANK with a
1484
+ WARNING naming which, and you say which in `note`. A `--sizing` you do pass
1485
+ wins over the file, and stderr names the flag as the source: do not use it to
1486
+ fill that gap, and do not re-derive the verdict from the diff to fill it
1487
+ either — `sizing-a-ticket` weighs more than file count, and a substituted proxy
1488
+ reads as measured too. **A value not in hand is left BLANK, never estimated** —
1489
+ blank reads as unknown and drops the row from a stratified comparison, while a
1490
+ guess reads as measured and poisons one. Blank still means the field is
1491
+ WRITTEN and empty: omit the flag (`--sizing`, `--profile`, `--loc`, `--files`)
1492
+ and the script writes that field empty at full width, because a row of some
1493
+ in-between width cannot be told apart from a shifted one. The `note` field is
1494
+ free text and sits before those four, so the script refuses a tab or newline in
1495
+ it: one tab would shift all four for that row alone.
1491
1496
 
1492
1497
  **Then record the run's member facts — do not author them.** Scrape EVERY session
1493
1498
  directory for this cwd, not one: