diff --git a/.devflow/features/hud-status-line/KNOWLEDGE.md b/.devflow/features/hud-status-line/KNOWLEDGE.md new file mode 100644 index 000000000..74657e4b1 --- /dev/null +++ b/.devflow/features/hud-status-line/KNOWLEDGE.md @@ -0,0 +1,141 @@ +--- +feature: hud-status-line +name: HUD status line +description: "Use when adding or changing a HUD component, hud.json config, the week forecast, Codex quota, or the devflow hud command. Keywords: HUD, statusLine, hud.json, weekForecast, codexQuota, tokenmeter." +category: architecture +directories: [src/hud, src/hud/components, src/cli/commands/hud.ts, src/cli/commands/init.ts] +created: 2026-11-23 +updated: 2026-11-23 +--- + +# HUD status line + +## Rules + +- **KB-INV-1** The installed HUD runs from `~/.devflow/scripts/hud/index.js` with no `package.json` and no `node_modules` above it: a file in `src/hud` imports only modules that compile into that import closure, never an npm package. +- **KB-INV-2** All I/O happens in the gather step of `src/hud/index.ts`. A component is `(ctx) => Promise` and reads only `ctx`; `null` hides it. +- **KB-INV-3** A new component needs a `ComponentId` in `types.ts`, an entry in `COMPONENT_MAP` and in `LINE_GROUPS` in `render.ts`, and an entry in `HUD_COMPONENTS` in `config.ts` only if it is on by default. +- **KB-INV-4** `render` drops a component that throws or rejects and prints the others. If `run` fails or the `OVERALL_TIMEOUT` timer fires, `main` prints nothing. +- **KB-INV-5** `weekForecastFrom` is a copy of the `forecastFrom` formula of the pinned tokenmeter release. The contract test in `tests/tokenmeter-contract.test.ts` has two parts. One part imports the package `forecastFrom` (test only) and compares the at-reset percent and the run-out timestamp with `weekForecastFrom` at a fixed time on an edge table. The other part runs the pinned `tokenmeter statusline` and compares percent and run-out hours with the HUD. +- **KB-INV-6** The week forecast reads the raw `rate_limits.seven_day` from stdin through `parseWeekPoint`, not `ctx.usage`. Any invalid input gives output byte for byte equal to the output with the forecast off. +- **KB-INV-7** Codex quota comes only through `src/hud/tokenmeter.ts`: it runs the verified local tokenmeter binary read from install state, with no shell, no HUD stdin, an allowlisted env, a byte cap, a deadline and a cache. +- **KB-INV-8** `HudConfig` has three fields (`enabled`, `detail`, `forecast`). `loadConfig` builds the object from these known keys only. `detail` and `forecast` are on only for a strict `true`; `enabled` is off only for a strict `false`. +- **KB-INV-9** Every writer of `hud.json` spreads the loaded config and changes only its own field: `{ ...loadConfig(), field }`. `devflow hud` and both HUD saves in `init.ts` follow this. +- **KB-INV-10** The config path is always `$HOME/.devflow/hud.json`. No environment variable moves it. +- **KB-INV-11** A `statusLine` is devflow's only when its command ends in `/.devflow/scripts/hud.sh` or the legacy `/.devflow/scripts/statusline.sh`. `isDevFlowStatusLine` is the one predicate; every add, remove and check goes through it. +- **KB-INV-12** With the HUD off, `resolveComponents` returns only `versionBadge`, so users still see upgrade notices. +- **KB-AP-1** Adding a field to `HudConfig` without adding it to `loadConfig`: the loader drops it, so the next `devflow hud` or `devflow init` save erases it, with no error. The same applies to a key a user adds by hand. +- **KB-AP-2** Saving a fresh object such as `saveConfig({ enabled: true })`: it resets `detail` and `forecast`. Spread the loaded config. +- **KB-AP-3** Naming `forecast` in `src/hud/index.ts`, or importing anything but colors, types and usage-quota into `week-forecast.ts`: the guard in `tests/guards/hud-usage-source.test.ts` fails. The forecast must stay a no-I/O component. +- **KB-AP-4** Reading `ctx.usage` in the forecast: `index.ts` clamps it to 0-100, so an out-of-range stdin value would show a forecast instead of hiding it. +- **KB-AP-5** Opening a network connection, reading a credential file, looking up `tokenmeter` on PATH or reading Codex logs in `src/hud`: the same guard rejects each one. +- **KB-AP-6** Building a child env by copying `process.env` and deleting keys: use the `ENV_KEYS` allowlist in `tokenmeter.ts` and the filtered PATH, and keep `NODE_OPTIONS` out. +- **KB-AP-7** Adding a component to `COMPONENT_MAP` but not to `LINE_GROUPS`: TypeScript does not catch it and the component never shows, because `render` walks only `LINE_GROUPS`. +- **KB-AP-8** Detecting a user turn in the transcript by type `human`: Claude Code writes `user`. Build parser fixtures from sampled real entries. +- **KB-AP-9** Awaiting the Codex cache write inside the render budget: `index.ts` passes `waitForCache: false`, and the installed-layout test checks that the cache file exists after the process exits. + +## Overview + +The HUD is the status line that Claude Code starts through `~/.devflow/scripts/hud.sh`. That script runs `node hud/index.js`. Claude Code sends one JSON object on stdin (model, cwd, context window, cost, session id, transcript path and `rate_limits`). The HUD prints up to five ANSI lines. + +The flow has three steps. `src/hud/index.ts` reads stdin and `hud.json`, resolves the enabled component ids, and gathers only the data those components need (git, transcript, ledger counts, Codex quota) in parallel. Then it builds a `GatherContext`. `render.ts` then runs every enabled component in parallel and joins the results by line group. The `devflow hud` command (`src/cli/commands/hud.ts`) edits `hud.json` and the `statusLine` entry in `settings.json`. It never renders. + +## Components and config + +`HUD_COMPONENTS` in `config.ts` is the default list, in display order. The test `HUD_COMPONENTS has 15 components` in `tests/hud-render.test.ts` pins its length. A second test pins `codexQuota` right after `usageQuota`. + +Two components are outside the default list: + +- `weekForecast` is opt-in. `resolveComponents` appends it only when the HUD is on and `forecast` is `true`. Run `devflow hud --forecast` or `--no-forecast` to change it. +- `sessionDuration` is in the type, the map and the tests, but no config path enables it. `resolveComponents` never returns it. + +`detail` is saved by `devflow hud --detail` and shown by `--status`. No file in `src/hud` reads it today. + +`LINE_GROUPS` in `render.ts` sets the layout. The first line holds directory and git parts. The second is the capacity line: `contextUsage`, `usageQuota`, `weekForecast`, `codexQuota`, `todoProgress`. The third holds `model`, `configCounts` and `sessionCost`. `curationCounts` and `versionBadge` each get their own line. A component whose text contains a newline gets its own line, and the single-line parts before it flush first. Parts join with a dim dot separator. + +## Week forecast + +`weekForecast` shows `→ N% by reset` and, when the quota runs out before the reset, `, runs out in