nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Feature] Keep Ink, React and highlight.js out of the --plain runtime

Open addyCooks opened this issue 1 month ago • 3 comments

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

  1. pnpm run build
  2. 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 --plain alone.
  • 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.tsx take render callbacks instead of importing components/message-box. Do it tool by tool.
  • Verify with the snippet above and that --plain --json output is unchanged. Tests: tool-registry.spec.ts, tool-manager.spec.ts, plain shell specs.

addyCooks avatar Oct 18 '26 06:10 addyCooks

Taking this too will propose my plan soon

rishu685 avatar Oct 18 '26 06:10 rishu685

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 loading cli-highlight / highlight.js until 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 --plain never 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/*) export NanocoderToolExport which combines the core schema/execution handler (tool) with the React/Ink formatter.
    • Split each tool into:
      1. Core definition (*.tool.ts or pure handler/schema export) containing only Node.js logic and JSON schema validations (zero React/Ink dependencies).
      2. UI formatter (*.formatter.tsx) loaded exclusively by the interactive TUI layer.
  • Tool Registry: Update ToolManager / source/plain/initialize.ts to 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.tsx currently imports message components (ErrorMessage, InfoMessage, etc.) from @/components/message-box, pulling Ink into the module graph during config initialization.
  • Callback Injection: Refactor message-queue to 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 importing plain/shell.js loads 0 modules matching ink|react|react-reconciler|yoga-layout|highlight.js.
  • Output Parity: Verify that --plain and --plain --json output remains strictly identical.

I will proceed with Phase 1 and Phase 2 in targeted PRs. Feedback and thoughts are welcome!

rishu685 avatar Oct 18 '26 06:10 rishu685

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.

addyCooks avatar Oct 18 '26 07:10 addyCooks