diff options
| author | Morgan Lunt <morganl@anthropic.com> | 2026-09-24 18:07:14 -0700 |
|---|---|---|
| committer | Morgan Lunt <morganl@anthropic.com> | 2026-09-24 18:11:07 -0700 |
| commit | c9e990089c6c87cb96e227e998edbada1df82861 (patch) | |
| tree | 623eb2b217930fd1e56558946443cd2ddeb5b849 /plugins/code-modernization/hooks/reader/uplift.ts | |
| parent | 70caddcb673ac8995d6a5191e02ac51de579d6c3 (diff) | |
code-modernization: add an optional live progress pane
Adds a hooks module, off unless the CLI's early-access function hooks
are enabled, that draws a pane beside the transcript while the commands
run: the system, which steps are done, a map of its code, each unit of
work and where it stands, what needs attention, and the next command.
It reads only the artifacts the commands write and the legacy tree, so
it works for any language and for a rewrite, a same-stack uplift or a
greenfield rebuild. The map comes from topology.json when there is one
and from the source tree when there is not. In an uplift each module is
set against the baseline; tests are read from JUnit XML, .trx reports or
the runs a session sees; a button shows and hides the pane, and
/modernize-panel does the same from the keyboard.
The same module adds x-ray context on legacy reads, a review deck for
business rules (/modernize-review), a sign-off dialog (/modernize-sign)
and a fleet view that spots one failure repeating across agents. It
only watches: it never blocks, rewrites or restores an edit, and it runs
no program inside the trees it reads. An uplift's changed modules are
found by comparing the working copy with the tree by file name and size
through the file API. Its options are the plugin's userConfig. Without
the flag nothing loads and the commands, agents and workflows behave as
before.
91 tests in tests/ run with "claude plugin test"; plugin validate
passes on the release build with the flag on and off.
Diffstat (limited to 'plugins/code-modernization/hooks/reader/uplift.ts')
| -rw-r--r-- | plugins/code-modernization/hooks/reader/uplift.ts | 238 |
1 files changed, 238 insertions, 0 deletions
diff --git a/plugins/code-modernization/hooks/reader/uplift.ts b/plugins/code-modernization/hooks/reader/uplift.ts new file mode 100644 index 00000000..58e99060 --- /dev/null +++ b/plugins/code-modernization/hooks/reader/uplift.ts @@ -0,0 +1,238 @@ +import { join } from '../paths' +import { unitOfPath, type EstateModel, type EstateUnit } from './estate-model' +import type { ReaderFs } from './fs' +import { readTests, stateOfUplift, type ModernizedModule, type ModuleState, type TestTotals } from './modernized' + +/** + * What a same-stack uplift leaves on disk, read without knowing the stack: the + * baseline's per-module results, the delta catalog's size, and which modules of the + * working copy differ from the legacy tree. + */ + +export type BaselineRow = { pass: number; fail: number; error: number; skip: number } + +export type Baseline = { + /** The file says the source runtime could not run here, so there is no per-test table. */ + isTargetOnly: boolean + /** Test results in all, from the table or the file's own totals line. */ + results: number | null + /** One row per module, keyed by the module path lower-cased with no trailing slash. */ + rows: Map<string, BaselineRow> +} + +const int = (text: string | undefined): number => { + const value = Number((text ?? '').replace(/[,\s_]/g, '')) + + return Number.isFinite(value) ? value : 0 +} + +const cellsOf = (line: string): string[] => + line + .trim() + .replace(/^\|/, '') + .replace(/\|$/, '') + .split('|') + .map(cell => cell.trim()) + +const keyOf = (text: string): string => + text + .replace(/[`*]/g, '') + .replace(/^\.\//, '') + .replace(/\/+$/, '') + .trim() + .toLowerCase() + +/** The module key a working-copy path is looked up by. */ +export const baselineKeyOf = keyOf + +/** Reads a baseline file: any table with pass and fail columns gives a row per module. */ +export function parseBaseline(text: string): Baseline { + const rows = new Map<string, BaselineRow>() + const lines = text.split('\n') + + for (let index = 0; index < lines.length - 1; index += 1) { + const head = lines[index] ?? '' + + if (!head.trim().startsWith('|') || !/^\s*\|?\s*:?-{2,}/.test(lines[index + 1] ?? '')) { + continue + } + + const names = cellsOf(head).map(name => name.replace(/[`*]/g, '').toLowerCase()) + const passAt = names.findIndex(name => /^pass/.test(name)) + const failAt = names.findIndex(name => /^fail/.test(name)) + + if (passAt < 0 || failAt < 0) { + continue + } + + const errorAt = names.findIndex(name => /^err/.test(name)) + const skipAt = names.findIndex(name => /^(skip|ignor|pending)/.test(name)) + const found = names.findIndex(name => /^(module|project|unit|suite|package|assembly|component|name)/.test(name)) + const keyAt = found >= 0 ? found : 0 + + for (let row = index + 2; row < lines.length && (lines[row] ?? '').trim().startsWith('|'); row += 1) { + const cells = cellsOf(lines[row] ?? '') + const key = keyOf(cells[keyAt] ?? '') + + if (key === '' || /^(total|totals|sum|all|overall)\b/.test(key)) { + continue + } + + rows.set(key, { + pass: int(cells[passAt]), + fail: int(cells[failAt]), + error: errorAt >= 0 ? int(cells[errorAt]) : 0, + skip: skipAt >= 0 ? int(cells[skipAt]) : 0, + }) + } + } + + const totals = /([\d,]+)\s+test\s+results?/i.exec(text)?.[1] ?? /\btotal\b[^\n]{0,40}?([\d,]{2,})\s+tests?\b/i.exec(text)?.[1] + const summed = [...rows.values()].reduce((sum, row) => sum + row.pass + row.fail + row.error + row.skip, 0) + + return { + isTargetOnly: /^\s*target-only:/im.test(text), + results: totals !== undefined ? int(totals) : rows.size > 0 ? summed : null, + rows, + } +} + +export type Catalog = { + count: number + /** Lower-cased base name of a file the catalog cites, to the delta ids it is cited under. */ + byFileBase: Map<string, string[]> +} + +/** + * What the delta catalog holds: how many deltas (its own statement of the count, else its `D<n>` ids), and which + * delta each file it names is cited under: the delta a table row or a heading names last, before the file. + */ +export function parseCatalog(text: string): Catalog | null { + const stated = /\b(\d+)\s+(?:distinct\s+)?deltas\b/i.exec(text)?.[1] + const ids = new Set<string>() + const byFileBase = new Map<string, string[]>() + let current: string | null = null + + for (const line of text.split('\n')) { + const opens = /^\s*(?:\|\s*\**|#{1,6}\s+\**)(D-?\d{1,3})\b/.exec(line)?.[1] + + if (opens !== undefined) { + current = opens + ids.add(opens) + } + + for (const found of line.matchAll(/D-?\d{1,3}\b/g)) { + ids.add(found[0]) + } + + if (current === null) { + continue + } + + for (const cited of line.matchAll(/`([\w./@+-]+\.[A-Za-z0-9]{1,8})(?::\d+(?:[-–]\d+)?)?`/g)) { + const base = (cited[1] ?? '').split('/').at(-1)?.toLowerCase() ?? '' + const held = byFileBase.get(base) ?? [] + + if (base !== '' && !held.includes(current)) { + held.push(current) + byFileBase.set(base, held) + } + } + } + + const count = stated !== undefined && int(stated) > 0 ? int(stated) : ids.size + + return count > 0 ? { count, byFileBase } : null +} + +/** + * The paths that differ between a tree and the working copy made from it: a file present in both with another size, + * one only the copy has, and one only the tree has. It compares names and sizes, so an edit that keeps a file's size + * exactly is not seen here; a test run in that module, or an edit made during the session, shows it. + */ +export function changedPathsOf(before: ReadonlyMap<string, number>, after: ReadonlyMap<string, number>): string[] { + const out: string[] = [] + + for (const [path, size] of after) { + if (before.get(path) !== size) { + out.push(path) + } + } + + for (const path of before.keys()) { + if (!after.has(path)) { + out.push(path) + } + } + + return out +} + +/** The estate's unit ids that hold at least one of the changed paths. */ +export function changedUnitsOf(estate: EstateModel, changed: readonly string[]): Set<string> { + const ids = new Set<string>() + + for (const path of changed) { + const unit = unitOfPath(estate, null, path) + + if (unit !== null) { + ids.add(unit.id) + } + } + + return ids +} + +/** + * Each unit of the working copy that the uplift has touched or tested, with its state: + * changed, tested, matching the baseline, or worse than it. `observed` holds the totals of test + * runs seen this session, used where no report file was left. + */ +export async function readUpliftUnits( + fs: ReaderFs, + workingRoot: string, + estate: EstateModel, + changed: ReadonlySet<string>, + baseline: Baseline | null, + observed: ReadonlyMap<string, TestTotals>, +): Promise<{ unit: EstateUnit; module: ModernizedModule }[]> { + const out: { unit: EstateUnit; module: ModernizedModule }[] = [] + + for (const unit of estate.units) { + if (unit.dir === undefined) { + if (changed.has(unit.id)) { + out.push({ unit, module: moduleOf(unit.name, join(workingRoot, unit.file ?? unit.id), null, 'scaffolded') }) + } + + continue + } + + const path = unit.dir === '' ? workingRoot : join(workingRoot, unit.dir) + const tests = (await readTests(fs, path)) ?? observed.get(path) ?? null + const row = baseline?.rows.get(keyOf(unit.dir)) ?? null + const state = stateOfUplift(tests, row, changed.has(unit.id)) + + if (state !== 'untouched') { + out.push({ unit, module: moduleOf(unit.dir === '' ? unit.name : unit.dir, path, tests, state) }) + } + } + + return out +} + +function moduleOf(dir: string, path: string, tests: TestTotals | null, state: ModuleState): ModernizedModule { + return { + dir, + path, + hasMain: true, + hasTests: tests !== null, + hasNotes: false, + hasReviewSection: false, + isPortedNotSwitched: false, + isSwitched: false, + followUps: 0, + tests, + state, + mtimeMs: 0, + } +} |
