Files
gstack/lib/review-scope.ts
Garry Tanandcapy-ai[bot] 8966f3620c v1.91.350.0 feat(review): GSTA-167 variant G reviewer stack: blocking floor in both voices, one batched specialist, data-file classifier (#3660)
* feat(review): variant G part 4: data files no code names are docs in classifyDiff, untested state-matrix cells map below the floor per cell (GSTA-167 PR-a)

Part 4a: lib/review-depth.ts owns DATA_PATH (JSON, JSONL, NDJSON, CSV, TSV, raster
images; YAML, TOML and SVG stay code) and applies the data rule only in classifyDiff,
to candidates lib/review-collect.ts finds no non-docs consumer for (git grep of
basenames at head, merge base and, live, the working tree; data-to-data chains four
deep; over 2,000 candidates keeps all code; a grep failure fails closed). classifyPath
and every path-only caller are unchanged; the receipt gate scan decides through
gateScanNeeded (path-only), so it cannot be skipped. gstack-ship-preflight computes
RUNTIME_LINES with classifyDiff over gstack-review-plan --json (now with consumed).

Part 4b: lib/review-scope.ts mapStateMatrixOutput maps an "untested cell" finding to
INFORMATIONAL when its cell has test null in the same output's matrix and it is the
only finding on that cell; gstack-state-matrix findings prints it for Step 9.2 before
the merge. state-machines.md is byte-identical; check still reads the raw output.

Reviewer-brief pins repinned under no-loss replay PR-a (Arm 4: plan identity at the 14
cohort heads; free exact re-score of saved C, D and A' findings).

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* feat(review): variant G parts 1 and 3: blocking floor in both prose voices, specialists at medium (GSTA-167)

Part 1: the native adversarial and outside-voice prompts end with the blocking
floor ("would fail a test, break production, or lose data or secrets") and ask
each finding for Severity and Blocks: yes|no. Additive rule: a finding is
CRITICAL when its label rule says so or it says Blocks: yes; Blocks: no never
lowers one (proseVoiceSeverity, BLOCKING_FLOOR in lib/review-scope.ts).
Part 3: ROLE_EFFORT puts specialists at medium; Red Team, native adversarial
and fix-check stay full; light's batched specialist (carries the Red Team
prompt) stays full; --no-tiers sends specialists to host.

Repinned reviewer briefs with no-loss replay G1 (live Arm G1, not yet run):
ship/sections/review-army.md, ship/sections/adversarial.md,
review/sections/adversarial.md, lib/review-depth.ts. Merge only on G1's pass.
Review-skill parity ratio 1.3136 -> 1.3180 and the gstack-review size pins
+537 for the approved +0.3k tokens per voice.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* feat(review): variant G part 2: lean ensemble, one batched specialist at every depth (GSTA-167)

planReview dispatches one 'specialist batched' at standard and full depth (and
light, which adds the Red Team checklist): Testing and Security always on code
and gate class, plus State-machines, Data-migration, API-contract and Design
when scope asks; a forced checklist always joins. Maintainability, Performance
and Simplification are read only when forced (SKIP names them). LIGHT_FIXED is
[testing, security]; BATCH_SCOPED joins DEPTH_RULES so old receipts re-plan.
The Red Team rule and prompt are unchanged; it reads the batched reader's
merged findings. specialistsFor stays as the legacy selection the dispatch
compare script models.

Repinned reviewer briefs with no-loss replay L (live Arm L, scored L∘G1 vs G1;
not yet run): ship/sections/review-army.md, lib/review-depth.ts. Merge only on
L's pass.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* feat(review): variant G on #3648's dispatch: Blocks: yes collects as CRITICAL, batched checklists by name (GSTA-167)

Integrates parts 1 and 2 with gstack-review-dispatch (#3648):
- lib/review-ledger.ts parseFindings: a prose voice's "Blocks: yes" makes its
  own line, or the finding on the line just before it, CRITICAL in
  merged.jsonl (normalizer v2, certified by round 5's G1), so judge refuses a
  lead that keeps it below the floor; with no finding before it the Blocks
  line is its own CRITICAL finding. "Blocks: no" never lowers a label.
- lib/review-brief.ts: the outside-voice brief no longer appends "Give each
  finding a severity"; the section's prompt now asks for Severity and Blocks,
  so the brief is the certified section prompt plus the branch diff.
- bin/gstack-review-dispatch render --checklists takes the plan's
  checklists= names (review/specialists/<name>.md) as well as paths and
  refuses an unknown one, since the batched reader is now every depth's
  specialist dispatch.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* chore(changelog): GSTA-167 variant G reviewer stack fragment

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix(review): consumer scan survives grep.lineNumber/grep.column; collect reads separate-line Severity/Blocks

/ship pass 1 (PR #3660): the Red Team CRITICAL at lib/review-collect.ts:120 and the
informational findings in the same batch. proseVoiceSeverity had no caller and is
removed; the normalizer v2 table now runs through parseFindings, the path collect uses.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix(review): collect keeps every prose blocker and one id per finding

/ship pass 2 (PR #3660): an unlabeled line carrying an inline Blocks: yes collects as
CRITICAL again; Severity/Blocks continuation needs a colon, stops at a list item, heading,
bold paragraph opener or a repeated field, and never swallows a top-level bullet or its
section label; the native voice's FIXABLE collects as CRITICAL under its role with or
without a dispatch name; the consumer scan adds --no-color.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix(review): a later finding's fields never land on the one before it; explicit Severity wins its line

/ship pass 2 Red Team (PR #3660): prose after a finding's Severity/Blocks field, a
post-blank paragraph or a line in an indented block starts the next finding, and a
line's explicit Severity: label decides it over other label words on the line.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix(review): FIXABLE outranks a Severity label on its line; a blocker keeps its own id and subject

/ship fix-check (PR #3660): labels are named before the strongest is taken, so a one-line
native FIXABLE ... Severity: Medium collects as CRITICAL; a later finding starts at a
file:line after the open finding's fields, field bullets stay on their finding, a field
line may omit the colon, and continuation prose never replaces a list item's subject.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* docs(changelog): GSTA-167 fragment adds round 6 and the collect and consumer-scan fixes

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

---------

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
2026-10-31 05:06:03 +00:00

389 lines
22 KiB
TypeScript

/**
* review-scope — the finding scope of /ship's pre-landing review loop
* (ship/sections/review-army.md, Step 9.4; docs/ship-policy.md#review-scope).
*
* Five Captains hit the same loop: every pass re-read the whole diff, found
* new neighbours in pre-existing code, fixed them, and the fixes drew another
* pass until the cycle cap. Under `review_scope: frozen` (the default) the
* scope is fixed after pass 1: findings in the diff are fixed, findings in
* code the PR did not change are filed as issues (deferred), and later passes
* read only the range since the previous pass and fix only blocking findings.
* Everything below the blocking floor is a named note in the PR body and never
* triggers a pass. `review_scope: all` keeps the old behaviour.
*
* Pure functions here; bin/gstack-review-scope runs git and gh.
*/
import { SPECIALISTS } from './review-depth';
import { parseSpecialistOutput } from './state-matrix';
export type Locus = 'diff' | 'fix-range' | 'outside';
export type Kind = 'defect' | 'test-gap' | 'advisory';
export type ReviewScopePolicy = 'frozen' | 'diff-plus-blocking' | 'all';
export const REVIEW_SCOPE_POLICIES: readonly ReviewScopePolicy[] = ['frozen', 'diff-plus-blocking', 'all'];
export type ScopeAction = 'fix' | 'defer' | 'note';
/** Lines around a changed hunk that still count as the diff (a missing check right after an added line is the diff's fault). */
export const HUNK_CONTEXT_LINES = 3;
export interface ScopeFinding {
fingerprint?: string; path?: string; line?: number; severity?: string; category?: string; summary?: string;
problem?: string; fix?: string; specialist?: string; advisory?: boolean; test_stub?: string;
/** The changed line that causes a defect reported elsewhere (a broken caller, a consumer of a removed export); the locus follows it. */
cause?: { path?: string; line?: number };
[key: string]: unknown;
}
export interface ScopedFinding extends ScopeFinding { locus: Locus; kind: Kind; blocking: boolean; scope: ScopeAction; raised_by?: string }
/** Who can raise a finding: the core checklist, QA probes, a specialist by name and the review-depth roles; anything else is `unknown`. */
const RAISERS: readonly string[] = ['core', 'qa', 'red-team', 'adversarial-native', 'outside-voice', 'fix-check', ...SPECIALISTS];
const RAISER_ALIASES: Record<string, string> = { adversarial: 'adversarial-native' };
/** Reviewers that read after pass 1 finished (Step 11's voices, a fix-check): only a blocking finding of theirs is ever `fix`. */
const AFTER_PASS_RAISERS: readonly string[] = ['adversarial-native', 'outside-voice', 'fix-check'];
/** The reviewer that raised a finding, for attributing passes later (GSTA-152 D6): its `raised_by` when known, else its `specialist` tag, else `unknown`. */
export function raisedBy(finding: ScopeFinding): string {
for (const v of [finding.raised_by, finding.specialist]) {
if (typeof v !== 'string') continue;
const k = v.trim().toLowerCase().replace(/[\s_]+/g, '-');
const name = RAISER_ALIASES[k] ?? k;
if (RAISERS.includes(name)) return name;
}
return 'unknown';
}
/** Per path, the new-side line ranges a diff touched; `'whole'` for an added or untracked file. */
export type HunkMap = Record<string, Array<[number, number]> | 'whole'>;
/**
* Parse `git diff -U0` output (plus untracked paths as whole files) into a hunk
* map. The diff must be produced with `--src-prefix=a/ --dst-prefix=b/` (the
* `diff.mnemonicPrefix` setting prints `c/` and `w/` otherwise, and no path
* would match a finding). A deleted file is a whole changed location too: a
* finding whose `cause` is a removed export anchors there.
*/
export function parseHunks(diff: string, untracked: readonly string[] = []): HunkMap {
const map: HunkMap = {};
let current: string | null = null;
let added = false;
let removed: string | null = null;
for (const raw of diff.split('\n')) {
const line = raw.replace(/\r$/, '');
if (line.startsWith('diff --git ')) { current = null; added = false; removed = null; continue; }
if (line.startsWith('--- ')) {
added = line === '--- /dev/null';
removed = added ? null : line.replace(/^--- (?:a\/)?/, '').replace(/\t.*$/, '');
continue;
}
if (line.startsWith('+++ ')) {
if (line === '+++ /dev/null') { if (removed) map[removed] = 'whole'; current = null; continue; }
current = line.replace(/^\+\+\+ (?:b\/)?/, '').replace(/\t.*$/, '');
if (added) map[current] = 'whole';
else if (!(current in map)) map[current] = [];
continue;
}
if (!current || map[current] === 'whole') continue;
const m = /^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@/.exec(line);
if (!m) continue;
const start = Number(m[1]);
const count = m[2] === undefined ? 1 : Number(m[2]);
// A pure deletion (+N,0) still marks the line where the code used to be.
(map[current] as Array<[number, number]>).push([start, count === 0 ? start : start + count - 1]);
}
for (const p of untracked) if (p) map[normalizePath(p)] = 'whole';
return map;
}
function normalizePath(p: string): string {
return p.replace(/\\/g, '/').replace(/^\.\//, '');
}
function inHunks(hunks: HunkMap, path: string, line: number | undefined, context: number): boolean {
const ranges = hunks[normalizePath(path)];
if (ranges === undefined) return false;
if (ranges === 'whole' || line === undefined || !Number.isFinite(line)) return true;
return ranges.some(([a, b]) => line >= a - context && line <= b + context);
}
/**
* Where a finding sits: in the range since the previous pass, in the branch
* diff, or in code the PR did not change. A `cause` anchor wins over the
* reported path, so a defect the diff causes in an unchanged caller is the
* diff's.
*/
export function locusOf(finding: ScopeFinding, diffHunks: HunkMap, rangeHunks: HunkMap | null, context = HUNK_CONTEXT_LINES): Locus {
const anchor = finding.cause && typeof finding.cause === 'object' && typeof finding.cause.path === 'string' ? finding.cause : finding;
const path = typeof anchor.path === 'string' ? anchor.path : fingerprintPath(finding.fingerprint);
if (!path) return 'outside';
const line = typeof anchor.line === 'number' ? anchor.line : anchor === finding ? fingerprintLine(finding.fingerprint) : undefined;
if (rangeHunks && inHunks(rangeHunks, path, line, context)) return 'fix-range';
if (inHunks(diffHunks, path, line, context)) return 'diff';
return 'outside';
}
function fingerprintPath(fp: unknown): string | undefined {
if (typeof fp !== 'string' || fp.startsWith('shared-libs:')) return undefined;
const [path] = fp.split(':');
return path || undefined;
}
function fingerprintLine(fp: unknown): number | undefined {
if (typeof fp !== 'string') return undefined;
const n = Number(fp.split(':')[1]);
return Number.isInteger(n) && n > 0 ? n : undefined;
}
/** The blocking floor in Garry's words (docs/review-policy.md, CREW rule 23); every reviewer prompt carries it verbatim. */
export const BLOCKING_FLOOR = 'would fail a test, break production, or lose data or secrets';
/** The categories a reviewer uses for a test finding; `testing` is what the Testing specialist's schema emits. */
export const TEST_GAP_CATEGORIES: readonly string[] = ['testing', 'tests', 'test-gap', 'test-coverage', 'coverage', 'missing-test', 'untested'];
/** What mapStateMatrixOutput stamps on an untested state-matrix cell: a test gap, below the floor (docs/review-policy.md). */
export const UNTESTED_CELL = 'untested-cell';
const cellKey = (f: Record<string, any>): string | null => (f.cell && typeof f.cell === 'object' ? `${String(f.cell.kind ?? '').trim().toLowerCase()}\u0000${String(f.cell.action ?? '').trim().toLowerCase()}` : null);
/**
* A reviewer output (JSON lines) with each untested state-matrix cell mapped below the floor, per matrix cell and before
* Step 9.3's fingerprint merge: a `state-matrix` finding the checklist's schema writes as "untested cell: …", whose `cell` is a
* cell with `"test": null` in a matrix record of the same output, and the only finding on that cell, becomes INFORMATIONAL with `state_matrix: "untested-cell"` and its
* `raw_severity`. A second finding on the cell (a wrong row, collapsed kinds) keeps both as they are, so a merge can never
* fold a defect into a demoted record; a cell no matrix lists as untested changes nothing. Every other line, matrix records
* included, is returned byte for byte; `gstack-state-matrix check` reads the raw output, never this one.
*/
export function mapStateMatrixOutput(text: string): { text: string; mapped: number } {
const { matrices, findings } = parseSpecialistOutput(text);
const untested = new Set(matrices.flatMap(m => m.cells.filter(c => c.test === null).map(c => cellKey({ cell: c })!)));
const perCell = new Map<string, number>();
for (const f of findings) { const k = cellKey(f); if (k) perCell.set(k, (perCell.get(k) ?? 0) + 1); }
let mapped = 0;
const lines = text.split('\n').map(line => {
let f: Record<string, any>;
try { f = JSON.parse(line.trim()); } catch { return line; }
const k = f && typeof f === 'object' && !f.matrix && f.category === 'state-matrix' && /^untested cell\b/i.test(String(f.summary ?? '')) ? cellKey(f) : null;
if (!k || !untested.has(k) || perCell.get(k) !== 1 || f.state_matrix === UNTESTED_CELL) return line;
mapped += 1;
return JSON.stringify({ ...f, severity: 'INFORMATIONAL', raw_severity: f.severity ?? null, state_matrix: UNTESTED_CELL });
});
return { text: lines.join('\n'), mapped };
}
/** A finding's kind: optional advice, a test finding (a gap, a weak or flaky test, an untested state-matrix cell still below CRITICAL), or a product defect. */
export function kindOf(finding: ScopeFinding): Kind {
if (finding.advisory === true) return 'advisory';
if (TEST_GAP_CATEGORIES.includes(String(finding.category ?? '').toLowerCase())) return 'test-gap';
if (finding.state_matrix === UNTESTED_CELL && String(finding.severity ?? '').toUpperCase() !== 'CRITICAL') return 'test-gap';
return 'defect';
}
/**
* The blocking floor: a CRITICAL defect (a test that would fail, a production
* break, a security or data-loss defect). Advisories, test gaps, wording and
* INFORMATIONAL items are below it and never trigger another pass.
*/
export function isBlocking(finding: ScopeFinding, kind = kindOf(finding)): boolean {
return kind === 'defect' && String(finding.severity ?? '').toUpperCase() === 'CRITICAL';
}
export interface ScopeInput { policy: ReviewScopePolicy; pass: number; diffHunks: HunkMap; rangeHunks: HunkMap | null; context?: number }
/**
* The scope decision per finding. `all`: fix everything (today's loop).
* An untested state-matrix cell mapped below the floor is a note from pass 1 on
* (docs/review-policy.md, the test-gap ruling) under the other policies.
* `frozen` pass 1: fix the diff, defer blocking findings in unchanged code,
* note the rest. `frozen` pass 2+: fix blocking findings in the range since
* the previous pass or in the diff; defer blocking findings outside; note
* everything else. Step 11's voices and a fix-check read after pass 1 has
* finished, so even at `--pass 1` their below-floor findings are notes and
* never send the loop back for another pass (CREW rules 23 and 30a).
*/
export function scopeFindings(findings: readonly ScopeFinding[], input: ScopeInput): ScopedFinding[] {
return findings.map(finding => {
const kind = kindOf(finding);
const blocking = isBlocking(finding, kind);
const raised = raisedBy(finding);
const locus = locusOf(finding, input.diffHunks, input.pass > 1 ? input.rangeHunks : null, input.context);
// A blocking finding nobody could place cannot be filed against a path; the diff's author resolves it now.
const placed = typeof finding.path === 'string' || fingerprintPath(finding.fingerprint) !== undefined;
let scope: ScopeAction;
if (input.policy === 'all') scope = 'fix';
else if (kind === 'test-gap' && finding.state_matrix === UNTESTED_CELL) scope = 'note';
else if (locus === 'outside') scope = !blocking ? 'note' : input.policy === 'diff-plus-blocking' || !placed ? 'fix' : 'defer';
else if (input.pass <= 1 && !AFTER_PASS_RAISERS.includes(raised)) scope = 'fix';
else scope = blocking ? 'fix' : 'note';
return { ...finding, locus, kind, blocking, scope, raised_by: raised };
});
}
export function scopeCounts(scoped: readonly ScopedFinding[]): { fix: number; defer: number; note: number; blocking: number } {
return {
fix: scoped.filter(f => f.scope === 'fix').length, defer: scoped.filter(f => f.scope === 'defer').length,
note: scoped.filter(f => f.scope === 'note').length, blocking: scoped.filter(f => f.blocking).length,
};
}
export function scopeLine(policy: ReviewScopePolicy, pass: number, scoped: readonly ScopedFinding[]): string {
const c = scopeCounts(scoped);
return `REVIEW_SCOPE: policy=${policy} pass=${pass} findings=${scoped.length} fix=${c.fix} defer=${c.defer} note=${c.note} blocking=${c.blocking}`;
}
// ---------------------------------------------------------------------------
// deferral: one issue per blocking finding in code the PR did not change
// ---------------------------------------------------------------------------
export const DEFERRED_MARKER = 'gstack-deferred-finding';
const oneLine = (s: unknown) => String(s ?? '').replace(/[\x00-\x1f\x7f\u2028\u2029]+/g, ' ').trim();
export function findingIdentity(f: ScopeFinding): string {
if (typeof f.fingerprint === 'string' && f.fingerprint) return f.fingerprint;
return [f.path ?? '?', f.line ?? '', f.category ?? ''].join(':');
}
export function findingWhere(f: ScopeFinding): string {
return f.path ? `${f.path}${typeof f.line === 'number' ? `:${f.line}` : ''}` : fingerprintPath(f.fingerprint) ?? '(no path)';
}
function findingText(f: ScopeFinding): string {
return oneLine(f.summary ?? f.problem ?? f.category ?? 'review finding');
}
export interface DeferContext { repo?: string; pr?: number; branch?: string; head?: string }
/** Issue title and body for a deferred finding; the marker keeps a rerun from filing it twice. */
export function renderDeferredIssue(f: ScopedFinding, ctx: DeferContext): { title: string; body: string; marker: string } {
const text = findingText(f);
const title = `Deferred review finding: ${findingWhere(f)} — ${text}`.slice(0, 120);
const origin = ctx.pr ? `${ctx.repo ? `${ctx.repo}#${ctx.pr}` : `PR #${ctx.pr}`}` : ctx.branch ? `branch \`${ctx.branch}\`` : 'a /ship run';
const marker = `<!-- ${DEFERRED_MARKER}: ${oneLine(findingIdentity(f))} -->`;
const body = [
`Filed by /ship's pre-landing review of ${origin}${ctx.head ? ` (head ${ctx.head.slice(0, 12)})` : ''}. The finding is in code that PR did not change, so it was deferred here instead of being fixed there (review scope frozen after pass 1).`,
'',
`- **Severity:** ${oneLine(f.severity ?? 'unrated')}`,
`- **Where:** \`${findingWhere(f)}\``,
`- **Finding:** ${text}`,
...(f.fix ? [`- **Suggested fix:** ${oneLine(f.fix)}`] : []),
...(f.specialist ? [`- **Found by:** ${oneLine(f.specialist)}`] : []),
'',
marker,
'',
].join('\n');
return { title, body, marker };
}
export interface DeferredRecord { fingerprint: string; path: string; severity: string; summary: string; issue: number | null; url?: string; reason?: string }
/** `defer`'s reason when the marker search found the finding's issue already open; the section renders such a record as a plain `#N`. */
export const ALREADY_FILED_REASON = 'already filed';
/** The PR-body section for deferred findings: references, never closing keywords, so a merge closes nothing. Only `frozen` defers. */
export function renderDeferredSection(deferred: readonly DeferredRecord[], noted: readonly ScopeFinding[] = []): string {
const out: string[] = [];
if (deferred.length) {
out.push('## Deferred findings', '', 'Blocking findings in code this PR did not change (review scope: frozen); filed as issues and not closed by this PR.', '');
for (const d of deferred) {
const ref = d.issue ? `#${d.issue}${d.reason && d.reason !== ALREADY_FILED_REASON ? ` (${oneLine(d.reason)})` : ''}` : `unfiled${d.reason ? ` (${oneLine(d.reason)})` : ''}`;
out.push(`- Deferred: ${ref} — ${oneLine(d.severity)} \`${oneLine(d.path)}\` ${oneLine(d.summary)}`);
}
out.push('');
}
if (noted.length) {
out.push('## Review notes', '', 'Below the blocking floor (advisory, informational, test-gap or wording items); recorded here and not fixed in this PR.', '');
for (const n of noted) out.push(`- ${oneLine(n.severity ?? 'INFORMATIONAL')}${n.advisory ? ' [ADVISORY]' : ''} \`${findingWhere(n)}\` ${findingText(n)}`);
out.push('');
}
return out.join('\n');
}
// ---------------------------------------------------------------------------
// loop summary: what the ship receipt carries for the factory scorecard
// ---------------------------------------------------------------------------
export interface ReviewLoopSummary {
passes: number; blocking_found: number; deferred: number; noted: number; fixed: number; scope: ReviewScopePolicy | 'unknown';
/** One-reviewer re-checks of a fix (docs/review-policy.md#transitions); never passes. */
fix_checks?: number;
/** Reviewer dispatches of this invocation by role (specialist, red-team, adversarial-native, outside-voice, fix-check). */
dispatches?: Record<string, number>;
/** Prompt plus diff bytes the dispatches read, / 4: a relative estimate, never a bill. */
est_input_tokens?: number;
}
/** Review-log rows of one /ship invocation (skill review, via ship, at or after `since`) → the loop summary. */
/**
* When a review-log row's pass started: the logger's own `review_binding.started_at`
* first, and the writer-typed `timestamp` only when the logger stamped nothing. The
* writer fills `timestamp` by hand, so a date-only or guessed value could otherwise
* sort a real pass before `--since` and leave the receipt without a loop to record.
*/
function rowTime(row: Record<string, any>): number {
for (const value of [row.review_binding?.started_at, row.timestamp]) {
const ms = typeof value === 'string' ? Date.parse(value) : NaN;
if (Number.isFinite(ms)) return ms;
}
return NaN;
}
/**
* A Step 11 late defer (a queued adversarial finding scoped `defer` after the
* last Step 9 pass) is logged under this skill, not as a `review` row: its
* deferred findings count here, it is not a pass, and the dashboards and
* readiness gates that pick the newest `review` row never see it (a `review`
* row without a start token would read as UNVERIFIED and displace the pass).
*/
export const LATE_DEFER_SKILL = 'review-late-defer';
/** A fix-check (one reviewer reading the touched hunks and the fix list) logs under its own skill, so it is never a pass. */
export const FIX_CHECK_SKILL = 'review-fix-check';
const DISPATCH_SKILLS = ['review', LATE_DEFER_SKILL, FIX_CHECK_SKILL, 'adversarial-review'];
/** Dispatch records of one row: its `dispatches` array, else the specialists and Red Team its stats say ran (older rows). */
function rowDispatches(row: Record<string, any>): Array<{ role: string; bytes: number | null }> {
if (Array.isArray(row.dispatches)) {
return row.dispatches.filter((d: any) => d && typeof d.role === 'string').map((d: any) => {
const b = [d.prompt_bytes, d.diff_bytes].filter((x: unknown) => typeof x === 'number' && Number.isFinite(x) && x >= 0) as number[];
return { role: String(d.role).slice(0, 40), bytes: b.length ? b.reduce((a, c) => a + c, 0) : null };
});
}
if (row.skill !== 'review' || !row.specialists || typeof row.specialists !== 'object') return [];
return Object.entries(row.specialists as Record<string, any>).filter(([, s]) => s && s.dispatched === true)
.map(([name]) => ({ role: name === 'red-team' ? 'red-team' : 'specialist', bytes: null }));
}
export function summarizeReviewLoop(rows: readonly Record<string, any>[], since: string): ReviewLoopSummary {
const sinceMs = Date.parse(since);
const inRun = (r: Record<string, any>) => r && r.via === 'ship' && Number.isFinite(rowTime(r)) && (!Number.isFinite(sinceMs) || rowTime(r) >= sinceMs);
const loopRows = rows.filter(r => inRun(r) && (r.skill === 'review' || r.skill === LATE_DEFER_SKILL || r.skill === FIX_CHECK_SKILL));
const passes = loopRows.filter(r => r.skill === 'review');
const blocking = new Set<string>();
const deferred = new Set<string>();
const noted = new Set<string>();
const fixed = new Set<string>();
let scope: ReviewLoopSummary['scope'] = 'unknown';
for (const row of loopRows) {
if (REVIEW_SCOPE_POLICIES.includes(row.review_scope)) scope = row.review_scope;
for (const f of Array.isArray(row.findings) ? row.findings : []) {
if (!f || typeof f !== 'object') continue;
const id = findingIdentity(f);
if (f.blocking === true) blocking.add(id);
if (f.action === 'deferred') deferred.add(id);
if (f.action === 'noted') noted.add(id);
if (f.action === 'fixed' || f.action === 'auto-fixed') fixed.add(id);
}
}
const out: ReviewLoopSummary = { passes: passes.length, blocking_found: blocking.size, deferred: deferred.size, noted: noted.size, fixed: fixed.size, scope };
const fixChecks = loopRows.filter(r => r.skill === FIX_CHECK_SKILL).length;
if (fixChecks) out.fix_checks = fixChecks;
const byRole: Record<string, number> = {};
let bytes = 0; let measured = false;
for (const row of rows.filter(r => inRun(r) && DISPATCH_SKILLS.includes(r.skill))) {
for (const d of rowDispatches(row)) { byRole[d.role] = (byRole[d.role] ?? 0) + 1; if (d.bytes !== null) { bytes += d.bytes; measured = true; } }
}
if (Object.keys(byRole).length) out.dispatches = byRole;
if (measured) out.est_input_tokens = Math.round(bytes / 4);
return out;
}
export function dispatchTotal(d: Record<string, number> | undefined): number | undefined {
return d ? Object.values(d).reduce((a, c) => a + c, 0) : undefined;
}
export function reviewLoopLine(s: ReviewLoopSummary): string {
const total = dispatchTotal(s.dispatches);
return `REVIEW_LOOP: passes=${s.passes} blocking_found=${s.blocking_found} deferred=${s.deferred} noted=${s.noted} fixed=${s.fixed} scope=${s.scope}`
+ (s.fix_checks ? ` fix_checks=${s.fix_checks}` : '') + (total !== undefined ? ` dispatches=${total}` : '') + (s.est_input_tokens !== undefined ? ` est_input_tokens=${s.est_input_tokens}` : '');
}