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/state.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/state.ts')
| -rw-r--r-- | plugins/code-modernization/hooks/state.ts | 180 |
1 files changed, 180 insertions, 0 deletions
diff --git a/plugins/code-modernization/hooks/state.ts b/plugins/code-modernization/hooks/state.ts new file mode 100644 index 00000000..2ce9b3f8 --- /dev/null +++ b/plugins/code-modernization/hooks/state.ts @@ -0,0 +1,180 @@ +import type { PluginOptions, Timer } from 'claude-code' + +import { newFleet, type Fleet } from './fleet/fleet' +import type { Host } from './host' +import type { Tile, Touch } from './map/estate' +import type { TestTotals } from './reader/modernized' +import type { ReaderCache, ReviewLedger, Snapshot } from './reader/progress' +import type { TrackKey } from './reader/tracks' +import type { Rule } from './reader/rules' +import type { DeckScope } from './review/deck' + +export const PANE_ID = 'modernize' +export const DECK_ID = 'modernize-review' +export const SIGN_ID = 'modernize-sign' +export const RASTER_KEY = 'estate' +export const PLUGIN_NAME = 'code-modernization' + +export type Options = { + system: string + /** Which way to follow the system: the one whose artifacts are newest (`auto`), or one named. */ + track: TrackKey | 'auto' + panel: 'auto' | 'command' | 'off' + isXrayOn: boolean + commandPrefix: string + legacyDir: string +} + +const pick = <T extends string>(value: unknown, allowed: readonly T[], fallback: T): T => + typeof value === 'string' && (allowed as readonly string[]).includes(value) ? (value as T) : fallback + +/** The options with their defaults applied, whatever the settings held. */ +export function optionsOf(raw: PluginOptions): Options { + const str = (key: string, fallback: string) => { + const value = raw[key] + + return typeof value === 'string' && value.trim() !== '' ? value.trim() : fallback + } + + return { + system: str('system', ''), + track: pick(raw.track, ['auto', 'transform', 'uplift', 'reimagine'] as const, 'auto'), + panel: pick(raw.panel, ['auto', 'command', 'off'] as const, 'auto'), + isXrayOn: raw.xray !== false, + commandPrefix: str('commandPrefix', '/code-modernization:modernize-'), + legacyDir: str('legacyDir', 'legacy').replace(/^\.?\/+|\/+$/g, ''), + } +} + +export type RunningCall = { + id: string + tool: string + subject: string + startMs: number + agentId?: string +} + +export type FinishedCall = { + tool: string + subject: string + isOk: boolean + ms: number + note?: string +} + +export type Activity = { + isWorking: boolean + turnId: string | null + turnStartMs: number + /** The `/…modernize-<verb>` the current or last turn began with. */ + step: string | null + running: Map<string, RunningCall> + finished: FinishedCall[] + /** Milliseconds the main loop spent in tool calls, and in those that ran tests. */ + toolMs: number + testMs: number + testRuns: number + xrays: number + xraysThisTurn: number +} + +export type DeckState = { + isOpen: boolean + scope: DeckScope + filter: string + queue: Rule[] + index: number + ledger: ReviewLedger + /** The source lines of the card in view, once asked for. */ + source: { ruleId: string; path: string; startLine: number; text: string } | null + isSourceShown: boolean +} + +export type SignState = { + isOpen: boolean + name: string + covers: 'phase-1' | 'full' + error: string | null +} + +export type State = { + host: Host | null + cwd: string + /** The system a person switched to with the pane's button, over the `system` option. */ + pickedSystem: string | null + options: Options + cache: ReaderCache + snapshot: Snapshot | null + readError: string | null + isRefreshing: boolean + isRefreshQueued: boolean + timers: Map<string, Timer> + pane: { + isOpen: boolean + isClosedByPerson: boolean + bodyColumns: number + bodyRows: number + placement: 'dock' | 'inline' + } + viewport: { columns: number | null; isFullscreen: boolean | null } + activity: Activity + touches: Map<string, Touch> + /** Test totals seen in commands this session, by the module path each was run in. */ + observed: Map<string, TestTotals> + /** Units of an uplift's working copy written during this session. */ + writtenUnits: Set<string> + estate: { tiles: Tile[]; columns: number; rows: number; isMounted: boolean } | null + fleet: Fleet + deck: DeckState + sign: SignState + lastContextLine: string +} + +export const newActivity = (): Activity => ({ + isWorking: false, + turnId: null, + turnStartMs: 0, + step: null, + running: new Map(), + finished: [], + toolMs: 0, + testMs: 0, + testRuns: 0, + xrays: 0, + xraysThisTurn: 0, +}) + +export function newState(raw: PluginOptions): State { + return { + host: null, + cwd: '', + pickedSystem: null, + options: optionsOf(raw), + cache: new Map(), + snapshot: null, + readError: null, + isRefreshing: false, + isRefreshQueued: false, + timers: new Map(), + pane: { isOpen: false, isClosedByPerson: false, bodyColumns: 48, bodyRows: 30, placement: 'dock' }, + viewport: { columns: null, isFullscreen: null }, + activity: newActivity(), + touches: new Map(), + observed: new Map(), + writtenUnits: new Set(), + estate: null, + fleet: newFleet(), + deck: { + isOpen: false, + scope: 'flagged', + filter: '', + queue: [], + index: 0, + ledger: {}, + source: null, + isSourceShown: false, + }, + sign: { isOpen: false, name: '', covers: 'phase-1', error: null }, + lastContextLine: '', + } +} |
