From c9e990089c6c87cb96e227e998edbada1df82861 Mon Sep 17 00:00:00 2001 From: Morgan Lunt Date: Thu, 24 Sep 2026 18:07:14 -0700 Subject: 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. --- .../code-modernization/hooks/reader/modernized.ts | 384 +++++++++++++++++++++ 1 file changed, 384 insertions(+) create mode 100644 plugins/code-modernization/hooks/reader/modernized.ts (limited to 'plugins/code-modernization/hooks/reader/modernized.ts') diff --git a/plugins/code-modernization/hooks/reader/modernized.ts b/plugins/code-modernization/hooks/reader/modernized.ts new file mode 100644 index 00000000..600e1d39 --- /dev/null +++ b/plugins/code-modernization/hooks/reader/modernized.ts @@ -0,0 +1,384 @@ +import { join } from '../paths' +import { listOrEmpty, readOrNull, type ReaderFs } from './fs' + +/** + * The state of each transformed module, read from `modernized//`: + * what exists on disk, what the test reports say, what the notes record. + * No inference: a fact that is not on disk is absent. + */ + +export type TestTotals = { + tests: number + failures: number + errors: number + skipped: number + /** How many report files were read. */ + reports: number +} + +export type ModuleState = + | 'scaffolded' + | 'tests-written' + | 'tests-red' + | 'tests-green' + | 'reviewed' + | 'ported' + | 'switched' + +export type ModernizedModule = { + /** The directory's name under `modernized//`. */ + dir: string + /** Path relative to the working directory. */ + path: string + hasMain: boolean + hasTests: boolean + hasNotes: boolean + /** The date in the notes' "Architecture review" section; absent when none. */ + reviewDate?: string + hasReviewSection: boolean + /** The notes say "ported, not switched". */ + isPortedNotSwitched: boolean + /** The notes say the route was switched. */ + isSwitched: boolean + /** Open follow-ups the notes list (bullets under a Follow-ups heading). */ + followUps: number + tests: TestTotals | null + state: ModuleState + /** Newest mtime among the notes and test reports; 0 when unknown. */ + mtimeMs: number +} + +/** States a unit of work counts as finished in: the review is done, or the module is ported or switched. */ +const DONE_STATES: ReadonlySet = new Set(['reviewed', 'ported', 'switched']) + +/** + * Whether a module's work is finished. Green tests with notes written count: the notes are where the plugin's + * review step lists what the critic found, and how they headline it varies, so a missing headline is not unfinished work. + */ +export const isDone = (module: Pick): boolean => + DONE_STATES.has(module.state) || (module.state === 'tests-green' && module.hasNotes) + +const EMPTY: TestTotals = { tests: 0, failures: 0, errors: 0, skipped: 0, reports: 0 } + +/** Totals of one Visual Studio test result file (`.trx`), the report `dotnet test --logger trx` leaves. */ +export function totalsOfTrx(xml: string): TestTotals | null { + const counters = /]*>/.exec(xml)?.[0] + + if (counters === undefined || !/\btotal="\d+"/.test(counters)) { + return null + } + + return { + tests: attr(counters, 'total'), + failures: attr(counters, 'failed'), + errors: attr(counters, 'error') + attr(counters, 'timeout') + attr(counters, 'aborted'), + skipped: attr(counters, 'notExecuted') + attr(counters, 'inconclusive'), + reports: 1, + } +} + +const attr = (tag: string, name: string): number => { + const match = new RegExp(`\\b${name}="(\\d+)"`).exec(tag) + + return match?.[1] !== undefined ? Number(match[1]) : 0 +} + +/** Totals of one JUnit-style XML report (`` / ``). */ +export function totalsOfJunitXml(xml: string): TestTotals | null { + const suites = /]*>/.exec(xml)?.[0] + + if (suites !== undefined && /\btests="\d+"/.test(suites)) { + return { + tests: attr(suites, 'tests'), + failures: attr(suites, 'failures'), + errors: attr(suites, 'errors'), + skipped: attr(suites, 'skipped') + attr(suites, 'disabled'), + reports: 1, + } + } + + const all = [...xml.matchAll(/]*>/g)].map(match => match[0]) + + if (all.length === 0) { + return null + } + + return all.reduce( + (sum, tag) => ({ + tests: sum.tests + attr(tag, 'tests'), + failures: sum.failures + attr(tag, 'failures'), + errors: sum.errors + attr(tag, 'errors'), + skipped: sum.skipped + attr(tag, 'skipped') + attr(tag, 'disabled'), + reports: 1, + }), + { ...EMPTY, reports: 1 }, + ) +} + +const add = (a: TestTotals, b: TestTotals): TestTotals => ({ + tests: a.tests + b.tests, + failures: a.failures + b.failures, + errors: a.errors + b.errors, + skipped: a.skipped + b.skipped, + reports: a.reports + b.reports, +}) + +/** Where test runners leave JUnit XML, relative to a module's directory. */ +const REPORT_DIRS = [ + 'target/surefire-reports', + 'target/failsafe-reports', + 'build/test-results/test', + 'build/test-results', + 'build/logs', + 'test-results', + 'test-reports', + 'reports/junit', + 'TestResults', +] + +const REPORT_FILES = ['junit.xml', 'report.xml', 'test-report.xml', 'pytest.xml'] + +/** The test totals a directory's report files hold: a custom JSON report, JUnit XML, or a `.trx`. */ +export async function readTests(fs: ReaderFs, dir: string): Promise { + const custom = await readOrNull(fs, join(dir, '.modernize/test-report.json')) + + if (custom !== null) { + try { + const raw = JSON.parse(custom) as Record + const num = (key: string) => (typeof raw[key] === 'number' ? (raw[key] as number) : 0) + + return { + tests: num('tests'), + failures: num('failures'), + errors: num('errors'), + skipped: num('skipped'), + reports: 1, + } + } catch { + // fall through to the XML reports + } + } + + let total: TestTotals | null = null + + for (const sub of REPORT_DIRS) { + const entries = await listOrEmpty(fs, join(dir, sub)) + + for (const entry of entries) { + const isTrx = entry.name.endsWith('.trx') + + if (entry.kind !== 'file' || !(entry.name.endsWith('.xml') || isTrx) || entry.size > 3_500_000) { + continue + } + + const xml = await readOrNull(fs, join(dir, sub, entry.name)) + const totals = xml === null ? null : isTrx ? totalsOfTrx(xml) : totalsOfJunitXml(xml) + + if (totals !== null) { + total = total === null ? totals : add(total, totals) + } + } + + if (total !== null) { + return total + } + } + + for (const name of REPORT_FILES) { + const xml = await readOrNull(fs, join(dir, name)) + const totals = xml !== null ? totalsOfJunitXml(xml) : null + + if (totals !== null) { + return totals + } + } + + return null +} + +const TEST_DIRS = ['src/test', 'tests', 'test', 'spec', '__tests__'] +const MAIN_DIRS = ['src/main', 'src', 'app', 'lib'] + +async function anyDir(fs: ReaderFs, dir: string, names: readonly string[]): Promise { + for (const name of names) { + if ((await listOrEmpty(fs, join(dir, name))).length > 0) { + return true + } + } + + return false +} + +/** What the notes record, read by heading and phrase. */ +export function readNotes(notes: string): { + hasReviewSection: boolean + reviewDate?: string + isPortedNotSwitched: boolean + isSwitched: boolean + followUps: number +} { + const lines = notes.split('\n') + // The plugin's review step spawns the architecture critic and lists what it found in the notes; how the notes + // headline that varies, so any heading that says review or critic counts, and so does naming the critic. + const named = lines.findIndex(line => /^#{1,4}\s+.*\b(?:review|critic)/i.test(line)) + const start = named >= 0 ? named : -1 + let reviewDate: string | undefined + + if (start >= 0) { + const level = /^(#+)/.exec(lines[start] ?? '')?.[1]?.length ?? 2 + + const end = lines.findIndex( + (line, index) => index > start && new RegExp(`^#{1,${level}}\\s`).test(line), + ) + + const section = lines.slice(start, end < 0 ? undefined : end).join('\n') + + reviewDate = /\b(\d{4}-\d{2}-\d{2})\b/.exec(section)?.[1] + } + + const followStart = lines.findIndex(line => /^#{1,4}\s+.*follow-?ups?/i.test(line)) + let followUps = 0 + + if (followStart >= 0) { + for (const line of lines.slice(followStart + 1)) { + if (/^#{1,4}\s/.test(line)) { + break + } + + if (/^\s*[-*]\s+(?!\[[xX]\])/.test(line) || /^\s*\d+\.\s+/.test(line)) { + followUps += 1 + } + } + } + + const isPortedNotSwitched = /ported,?\s+not\s+switched/i.test(notes) + + return { + hasReviewSection: start >= 0 || /architecture[- ]critic/i.test(notes), + ...(reviewDate !== undefined && { reviewDate }), + isPortedNotSwitched, + isSwitched: + !isPortedNotSwitched && + /\b(?:route|traffic)\b[^\n]{0,40}\bswitched\b|\bswitched\b[^\n]{0,20}\b\d{4}-\d{2}-\d{2}\b/i.test( + notes, + ), + followUps, + } +} + +/** The state a module's facts add up to, furthest first. */ +export function stateOf(module: Omit): ModuleState { + const tests = module.tests + const isRed = tests !== null && tests.failures + tests.errors > 0 + const isGreen = tests !== null && tests.tests > 0 && !isRed + + if (module.isSwitched && isGreen && module.hasReviewSection) { + return 'switched' + } + + if (module.isPortedNotSwitched && isGreen) { + return 'ported' + } + + if (isGreen && module.hasReviewSection) { + return 'reviewed' + } + + if (isRed) { + return 'tests-red' + } + + if (isGreen) { + return 'tests-green' + } + + if (module.hasTests) { + return 'tests-written' + } + + return 'scaffolded' +} + +/** + * The state of one unit of an uplift's working copy: worse than its baseline, matching or exceeding it + * (every test that passed on the source runtime still passes, none is missing), tested but not comparable + * to a baseline row, changed and not yet tested, or not touched at all. + */ +export function stateOfUplift( + tests: TestTotals | null, + baseline: { pass: number; fail: number; error: number; skip: number } | null, + isChanged: boolean, +): ModuleState | 'untouched' { + if (tests !== null && tests.tests > 0) { + const bad = tests.failures + tests.errors + const allowed = baseline === null ? 0 : baseline.fail + baseline.error + + if (bad > allowed) { + return 'tests-red' + } + + const passed = tests.tests - bad - tests.skipped + const isCovered = baseline !== null && tests.tests >= baseline.pass + baseline.fail + baseline.error + baseline.skip && passed >= baseline.pass + + return isCovered ? 'reviewed' : 'tests-green' + } + + return isChanged ? 'scaffolded' : 'untouched' +} + +/** Reads every code directory under `modernized//`. */ +export async function readModernized( + fs: ReaderFs, + system: string, +): Promise { + return readModernizedIn(fs, join('modernized', system)) +} + +/** Reads every code directory directly under `root`: a transformed system's modules, or a reimagined system's services. */ +export async function readModernizedIn( + fs: ReaderFs, + root: string, + observed: ReadonlyMap = new Map(), +): Promise { + const entries = await listOrEmpty(fs, root) + const out: ModernizedModule[] = [] + + for (const entry of entries) { + if (entry.kind !== 'dir' || entry.name.startsWith('.')) { + continue + } + + const path = join(root, entry.name) + const notes = await readOrNull(fs, join(path, 'TRANSFORMATION_NOTES.md')) + const read = notes !== null ? readNotes(notes) : null + const tests = (await readTests(fs, path)) ?? observed.get(path) ?? null + let mtimeMs = 0 + + if (notes !== null) { + try { + mtimeMs = (await fs.stat(join(path, 'TRANSFORMATION_NOTES.md'))).mtimeMs + } catch { + mtimeMs = 0 + } + } + + const facts = { + dir: entry.name, + path, + hasMain: await anyDir(fs, path, MAIN_DIRS), + hasTests: await anyDir(fs, path, TEST_DIRS), + hasNotes: notes !== null, + hasReviewSection: read?.hasReviewSection ?? false, + ...(read?.reviewDate !== undefined && { reviewDate: read.reviewDate }), + isPortedNotSwitched: read?.isPortedNotSwitched ?? false, + isSwitched: read?.isSwitched ?? false, + followUps: read?.followUps ?? 0, + tests, + mtimeMs, + } + + out.push({ ...facts, state: stateOf(facts) }) + } + + return out +} -- cgit From 51b1ae9e20486d62b839ed9f37e1cfc7d55bf85c Mon Sep 17 00:00:00 2001 From: Morgan Lunt Date: Thu, 24 Sep 2026 20:52:27 -0700 Subject: 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/ 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. --- .../code-modernization/hooks/reader/modernized.ts | 71 +++++++++++++++------- 1 file changed, 48 insertions(+), 23 deletions(-) (limited to 'plugins/code-modernization/hooks/reader/modernized.ts') diff --git a/plugins/code-modernization/hooks/reader/modernized.ts b/plugins/code-modernization/hooks/reader/modernized.ts index 600e1d39..d60b8f66 100644 --- a/plugins/code-modernization/hooks/reader/modernized.ts +++ b/plugins/code-modernization/hooks/reader/modernized.ts @@ -1,4 +1,5 @@ import { join } from '../paths' +import { linesOf } from '../text' import { listOrEmpty, readOrNull, type ReaderFs } from './fs' /** @@ -20,6 +21,8 @@ export type ModuleState = | 'scaffolded' | 'tests-written' | 'tests-red' + /** An uplift's tests fail and there is no baseline row to say whether the source runtime failed them too. */ + | 'tests-failing' | 'tests-green' | 'reviewed' | 'ported' @@ -138,9 +141,23 @@ const REPORT_DIRS = [ const REPORT_FILES = ['junit.xml', 'report.xml', 'test-report.xml', 'pytest.xml'] -/** The test totals a directory's report files hold: a custom JSON report, JUnit XML, or a `.trx`. */ -export async function readTests(fs: ReaderFs, dir: string): Promise { - const custom = await readOrNull(fs, join(dir, '.modernize/test-report.json')) +/** When a file last changed; 0 when it cannot be said. */ +async function changedAt(fs: ReaderFs, path: string): Promise { + try { + return (await fs.stat(path)).mtimeMs + } catch { + return 0 + } +} + +/** + * The test totals a directory's report files hold: a custom JSON report, JUnit XML, or a `.trx`, and when the newest of + * the files read last changed (what a verdict about the module is compared with). + */ +export async function readTestsAt(fs: ReaderFs, dir: string, withTime = true): Promise<{ totals: TestTotals; atMs: number } | null> { + const stamp = (path: string) => (withTime ? changedAt(fs, path) : Promise.resolve(0)) + const customPath = join(dir, '.modernize/test-report.json') + const custom = await readOrNull(fs, customPath) if (custom !== null) { try { @@ -148,11 +165,14 @@ export async function readTests(fs: ReaderFs, dir: string): Promise (typeof raw[key] === 'number' ? (raw[key] as number) : 0) return { - tests: num('tests'), - failures: num('failures'), - errors: num('errors'), - skipped: num('skipped'), - reports: 1, + totals: { + tests: num('tests'), + failures: num('failures'), + errors: num('errors'), + skipped: num('skipped'), + reports: 1, + }, + atMs: await stamp(customPath), } } catch { // fall through to the XML reports @@ -160,6 +180,7 @@ export async function readTests(fs: ReaderFs, dir: string): Promise { + return (await readTestsAt(fs, dir, false))?.totals ?? null +} + const TEST_DIRS = ['src/test', 'tests', 'test', 'spec', '__tests__'] const MAIN_DIRS = ['src/main', 'src', 'app', 'lib'] @@ -217,7 +244,7 @@ export function readNotes(notes: string): { isSwitched: boolean followUps: number } { - const lines = notes.split('\n') + const lines = linesOf(notes) // The plugin's review step spawns the architecture critic and lists what it found in the notes; how the notes // headline that varies, so any heading that says review or critic counts, and so does naming the critic. const named = lines.findIndex(line => /^#{1,4}\s+.*\b(?:review|critic)/i.test(line)) @@ -311,9 +338,13 @@ export function stateOfUplift( ): ModuleState | 'untouched' { if (tests !== null && tests.tests > 0) { const bad = tests.failures + tests.errors - const allowed = baseline === null ? 0 : baseline.fail + baseline.error - if (bad > allowed) { + // With no baseline row there is nothing to be worse than: the failures are said, not judged. + if (baseline === null && bad > 0) { + return 'tests-failing' + } + + if (baseline !== null && bad > baseline.fail + baseline.error) { return 'tests-red' } @@ -351,16 +382,10 @@ export async function readModernizedIn( const path = join(root, entry.name) const notes = await readOrNull(fs, join(path, 'TRANSFORMATION_NOTES.md')) const read = notes !== null ? readNotes(notes) : null - const tests = (await readTests(fs, path)) ?? observed.get(path) ?? null - let mtimeMs = 0 - - if (notes !== null) { - try { - mtimeMs = (await fs.stat(join(path, 'TRANSFORMATION_NOTES.md'))).mtimeMs - } catch { - mtimeMs = 0 - } - } + const reported = await readTestsAt(fs, path) + const tests = reported?.totals ?? observed.get(path) ?? null + // The newest of the notes and the test reports: what a verdict about the module is checked against. + const mtimeMs = Math.max(notes !== null ? await changedAt(fs, join(path, 'TRANSFORMATION_NOTES.md')) : 0, reported?.atMs ?? 0) const facts = { dir: entry.name, -- cgit