[Feature] Keep Ink, React and highlight.js out of the --plain runtime
Summary
--plain is the light runtime for CI and non-TTY runs, but it still loads Ink, React, the reconciler, Yoga and highlight.js, because tool files bundle their React formatters and the logging helper imports message components. That is about 260 modules the plain run never renders.
Steps to reproduce
pnpm run build- From the repo root run:
node --input-type=module -e "
import {registerHooks} from 'node:module';
const seen = new Set();
registerHooks({load(url, ctx, next) { seen.add(url); return next(url, ctx); }});
await import('./dist/plain/shell.js');
const count = re => [...seen].filter(u => re.test(u)).length;
console.log('total modules:', seen.size);
console.log('ink + react + yoga:', count(/node_modules\/(ink|react|react-reconciler|yoga-layout)\//), ' highlight.js:', count(/node_modules\/highlight\.js\//));"
Expected behaviour
A plain run that needs no UI loads none of Ink, React or highlight.js.
Actual behaviour
total modules: 1034
ink + react + yoga: 66 highlight.js: 193
Import chains: plain/shell.js -> config/index.js -> utils/message-queue.js -> components/message-box.js -> ink, and plain/initialize.js -> tools/tool-manager.js -> tools/index.js -> tools/file-ops/write-file.js -> cli-highlight -> highlight.js. Tool files such as source/tools/read-file.tsx import ink directly for their formatters. Blocking the Ink stack removes 110 modules, highlight.js and parse5 another 227, both 337 of 1,003 (34%).
Environment
- OS: Windows 11 (10.0.26300)
- Node: 22.22.3
- Nanocoder: 1.31.0 (main at be428fc7)
Suggested fix
- First step (smaller): lazy-load
cli-highlight(see #1776), which removes 227 modules in--plainalone. - Second step (larger): split each tool into a handler/definition module without React and a formatter module that only the TUI loads. Make
source/utils/message-queue.tsxtake render callbacks instead of importingcomponents/message-box. Do it tool by tool. - Verify with the snippet above and that
--plain --jsonoutput is unchanged. Tests:tool-registry.spec.ts,tool-manager.spec.ts, plain shell specs.
Taking this too will propose my plan soon
Proposed Implementation Plan
To ensure --plain remains truly lightweight for non-TTY and CI environments, here is the structured step-by-step approach to eliminate Ink, React, and highlight.js from the plain runtime:
Phase 1: Lazy-load syntax highlighting (cli-highlight / highlight.js)
- Decouple
cli-highlight: Defer loadingcli-highlight/highlight.jsuntil formatting is explicitly invoked by the TUI. - Scope: In tool formatters (e.g.,
write-file,file-op), ensure highlight utilities are dynamically imported or gated behind interactive UI formatters so--plainnever touches them. - Impact: Removes ~227 modules immediately from
--plain.
Phase 2: Decouple Core Tool Execution from React/Ink Formatters
- Separate Tool Definition from UI Presentation:
- Currently, tool files (
source/tools/*) exportNanocoderToolExportwhich combines the core schema/execution handler (tool) with the React/Inkformatter. - Split each tool into:
- Core definition (
*.tool.tsor pure handler/schema export) containing only Node.js logic and JSON schema validations (zero React/Ink dependencies). - UI formatter (
*.formatter.tsx) loaded exclusively by the interactive TUI layer.
- Core definition (
- Currently, tool files (
- Tool Registry: Update
ToolManager/source/plain/initialize.tsto consume the headless tool definitions, while the TUI registry binds the React formatters.
Phase 3: Decouple Message Queue and Logging from UI Components
- Message Queue Refactor:
source/utils/message-queue.tsxcurrently imports message components (ErrorMessage,InfoMessage, etc.) from@/components/message-box, pulling Ink into the module graph during config initialization. - Callback Injection: Refactor
message-queueto handle raw message payloads/events and use injected renderer callbacks for React components only when the interactive UI is active.
Phase 4: Automated Verification & Regression Tests
- Module Graph Test: Add an automated test using Node's
module.registerHooks(as demonstrated in the issue description) to assert that importingplain/shell.jsloads0modules matchingink|react|react-reconciler|yoga-layout|highlight.js. - Output Parity: Verify that
--plainand--plain --jsonoutput remains strictly identical.
I will proceed with Phase 1 and Phase 2 in targeted PRs. Feedback and thoughts are welcome!
Hey @rishu685, thanks for the detailed implementation plan! I really appreciate the thorough breakdown.
This is a pretty major architectural change that touches multiple core systems (tools, message queue, plain runtime), so I'm going to take this one on myself to ensure we maintain consistency across the codebase. Your analysis is spot-on though, and I'll definitely reference your proposed phases as I work through it. Feel free to pick up any of the other performance issues if you're interested in contributing to the optimization work!
Thanks again for taking the time to think through this.