diff options
| author | Morgan Lunt <morganl@anthropic.com> | 2026-09-24 20:52:27 -0700 |
|---|---|---|
| committer | Morgan Lunt <morganl@anthropic.com> | 2026-09-24 20:52:27 -0700 |
| commit | 51b1ae9e20486d62b839ed9f37e1cfc7d55bf85c (patch) | |
| tree | eb31cc4964d84b4a82b8032452739fe1e9b37d3f /plugins/code-modernization/hooks/reader/verification.ts | |
| parent | 5b8ec4b30f45b5e8b4c8a6efd63719730cf1a5ad (diff) | |
code-modernization: make the live pane work below 144 columns and show the proof
Found by exercising the pane against the real outputs of finished runs:
- Below 144 columns the engine holds an unasked pane back; the pane ignored
that, so it drew no pane, no show bar, and the panel command "hid" a pane
that was never there.
- A symlinked legacy/<name> lists as another kind of entry, so the source
link that preflight now creates was never recognized as a system.
- Uplift and reimagine next steps were placeholders with no button.
- The refresh debounce was starved while agents ran, so the map froze.
- Hostile lines in the brief, rules and baseline could take seconds to match;
lines are now capped. The rule-review ledger is validated on read and
merged on save, and keeps a reviewer's note.
- A baseline with no row read as "the baseline had 0"; one-module baseline
totals now count as that module's row.
New: a verdict chip with words on every built module, a verify-first next
step, and a readable palette that follows the terminal theme. 140 engine
tests pass and the type check is clean.
Diffstat (limited to 'plugins/code-modernization/hooks/reader/verification.ts')
| -rw-r--r-- | plugins/code-modernization/hooks/reader/verification.ts | 150 |
1 files changed, 150 insertions, 0 deletions
diff --git a/plugins/code-modernization/hooks/reader/verification.ts b/plugins/code-modernization/hooks/reader/verification.ts new file mode 100644 index 00000000..c1d6e556 --- /dev/null +++ b/plugins/code-modernization/hooks/reader/verification.ts @@ -0,0 +1,150 @@ +import { plain } from '../text' +import type { TrackKey } from './tracks' + +/** + * `analysis/<system>/VERIFICATION.json`, written by the proof pack (`scripts/proof_pack.py`, run by the verify + * command): one verdict per built module, computed by a script from the evidence files. The pane shows the verdicts + * as they are and never computes one. + * + * The file is text from a run on an untrusted tree, and its shape may move between releases, so it is read + * tolerantly: a field of the wrong type is ignored, a verdict is only ever one of the three exact words, a name or a + * reason is cut to one short plain line, and how much is read is capped. + */ + +export type Verdict = 'PROVEN' | 'PARTLY PROVEN' | 'NOT PROVEN' + +const VERDICTS: readonly string[] = ['PROVEN', 'PARTLY PROVEN', 'NOT PROVEN'] + +/** What one module's verdict is, as the file says it. */ +export type ProofEntry = { + /** The module: a folder under `modernized/<system>/`, or the uplift's whole working copy. */ + name: string + track: TrackKey + verdict: Verdict + /** The first reason the verdict is not PROVEN, as one plain line; empty when the file gives none. */ + reason: string + /** When the module was checked; null when the file gives no time this can read. */ + atMs: number | null +} + +export type Verification = { + overall: Verdict | null + entries: ProofEntry[] +} + +/** How the module is standing with the proof, for a chip and for the next step. */ +export type ProofState = { + /** `none`: built, and nothing checked it yet. `changed`: checked, and its code or tests are newer than that check. */ + state: 'proven' | 'partly' | 'not' | 'changed' | 'none' + verdict?: Verdict + reason: string +} + +const MAX_TEXT = 4_000_000 +const MAX_ENTRIES = 1_000 +const MAX_NAME = 80 +const MAX_REASON = 160 + +/** The proof pack's own spelling of a track: a rewrite is `rewrite` there. */ +const TRACK_OF: Record<string, TrackKey> = { rewrite: 'transform', transform: 'transform', uplift: 'uplift', reimagine: 'reimagine' } + +/** `2026-09-24 20:31 UTC` (the pack's stamp) as milliseconds since the epoch; null when it is anything else. */ +function timeOf(value: unknown): number | null { + if (typeof value !== 'string') { + return null + } + + const match = /^(\d{4})-(\d{2})-(\d{2})[ T](\d{2}):(\d{2})(?::(\d{2}))?\s*(?:UTC|Z)?$/.exec(value.trim()) + + if (match === null) { + return null + } + + const ms = Date.UTC(Number(match[1]), Number(match[2]) - 1, Number(match[3]), Number(match[4]), Number(match[5]), Number(match[6] ?? 0)) + + return Number.isFinite(ms) ? ms : null +} + +const isRecord = (value: unknown): value is Record<string, unknown> => typeof value === 'object' && value !== null && !Array.isArray(value) + +/** The file's text as a Verification; null when it is not one. */ +export function parseVerification(text: string): Verification | null { + if (text.length > MAX_TEXT) { + return null + } + + let raw: unknown + + try { + raw = JSON.parse(text) + } catch { + return null + } + + if (!isRecord(raw) || !Array.isArray(raw.modules)) { + return null + } + + const entries: ProofEntry[] = [] + + for (const item of raw.modules.slice(0, MAX_ENTRIES) as unknown[]) { + if (!isRecord(item)) { + continue + } + + const name = typeof item.name === 'string' ? plain(item.name, MAX_NAME) : '' + const track = typeof item.track === 'string' ? TRACK_OF[item.track] : undefined + + // Only the exact words count: a verdict with anything added to it is not one. + if (name === '' || track === undefined || typeof item.verdict !== 'string' || !VERDICTS.includes(item.verdict)) { + continue + } + + const first = Array.isArray(item.reasons) ? (item.reasons as unknown[]).find(reason => typeof reason === 'string' && reason.trim() !== '') : undefined + + entries.push({ + name, + track, + verdict: item.verdict as Verdict, + reason: typeof first === 'string' ? plain(first, MAX_REASON) : '', + atMs: timeOf(item.verifiedAt), + }) + } + + const overall = isRecord(raw.overall) && typeof raw.overall.verdict === 'string' && VERDICTS.includes(raw.overall.verdict) ? (raw.overall.verdict as Verdict) : null + + return { overall, entries } +} + +/** The pack stamps minutes; a file written in the same minute as the check is not newer than it. */ +const SLACK_MS = 90_000 + +/** + * Where a built module stands with the proof. Null when it has no notes and no verdict, so nothing is claimed of a + * module still being built. + * + * @param verification the file, or null when there is none + * @param track the track the pane follows + * @param name the module's folder under `modernized/` + * @param changedAtMs when the module's notes or test reports last changed (0 when unknown) + * @param hasNotes whether the module has notes: the sign a module was finished being built + */ +export function proofOfModule( + verification: Verification | null, + track: TrackKey, + name: string, + changedAtMs: number, + hasNotes: boolean, +): ProofState | null { + const entry = verification?.entries.find(candidate => candidate.track === track && candidate.name.toLowerCase() === plain(name, MAX_NAME).toLowerCase()) + + if (entry === undefined) { + return hasNotes ? { state: 'none', reason: '' } : null + } + + if (entry.atMs !== null && changedAtMs > entry.atMs + SLACK_MS) { + return { state: 'changed', verdict: entry.verdict, reason: entry.reason } + } + + return { state: entry.verdict === 'PROVEN' ? 'proven' : entry.verdict === 'PARTLY PROVEN' ? 'partly' : 'not', verdict: entry.verdict, reason: entry.reason } +} |
