nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Bug] nanocoder run hides config, hook, formatter and plugin errors, and reports invalid JSON as "No providers configured"

Open addyCooks opened this issue 1 month ago • 2 comments

Description

In run / --plain mode, config problems are only written to the log file, so the user sees nothing. logError and logWarning in source/utils/message-queue.tsx fall back to the file logger when no Ink queue is registered, and the plain shell never registers one. On top of that, source/plain/initialize.ts rewrites every config error into "No providers configured", so a broken agents.config.json hides its real cause. nanocoder config list on the same file does report the real error.

Environment

  • OS: Windows 11 (10.0.26300)
  • Node version: 22.22.3
  • Nanocoder version: 1.31.0 (main at 5c2e693)
  • Provider: Ollama
  • Model: qwen2.5:1.5b

Steps to Reproduce

  1. Put invalid JSON in the global agents.config.json: {"nanocoder":{"providers":[{"name":"Ollama",}]}} (trailing comma).
  2. Run nanocoder --trust-directory run "say hi".
  3. Run nanocoder config list against the same file.
  4. Fix the provider. Then add these mistakes to a project agents.config.json: "autoCommit": "yes", a hook named post-tool-uze, and a formatter with no command. Also add .nanocoder/plugins/bad.mjs containing throw new Error("boom").
  5. Run nanocoder --trust-directory run "Reply with OK".
  6. Run step 5 again with NANOCODER_LOG_LEVEL=warn.

Expected Behavior

Each problem is printed on stderr, and included in the --json output. For invalid JSON, the error names the problem, like config list does.

Actual Behavior

Step 2 prints only this and exits 1 (the --json output says the same):

No providers configured. Run nanocoder interactively to set them up.

Step 3 shows the real cause:

unreadable: Expected double-quoted property name in JSON at position 44 (line 1 column 45)

Step 5 exits 0 and prints only the banner and [plain] done. None of the four mistakes is shown.

Step 6 prints them, so they are only being swallowed:

Invalid hooks config: unknown lifecycle event 'post-tool-uze'.
Invalid formatters config: entries need a 'command' string and a non-empty 'match' glob list.
nanocoder.autoCommit must be true or false (got "yes"); treating as off
Plugin bad.mjs failed to load: boom from plugin

Suggested Fix

  • In runPlainShell (source/plain/shell.ts), register a message-queue sink that writes errors and warnings to stderr, and adds them to the --json report as warnings[].
  • In source/plain/initialize.ts (the catch around createLLMClient, about lines 84-92), only use "No providers configured" when the config really is empty. Otherwise pass the real error through.
  • Add a test that runs the CLI with a broken config and checks stderr.

Additional Context

  • [x] I have searched existing issues to ensure this is not a duplicate
  • [x] I can reproduce this issue consistently
  • [x] This issue occurs with the latest version of nanocoder

addyCooks avatar Oct 08 '26 16:10 addyCooks

Taking this issue will propose my plan after dignosing the issue properly :)

rishu685 avatar Oct 08 '26 16:10 rishu685

Here is my approach as promised let me know what you think about this

Implementation Approach

  1. Pluggable Message Sink (source/utils/message-queue.tsx):

    • Add setMessageQueueSink(sink: MessageQueueSink | null) allowing non-Ink/headless runners to intercept typed log messages (error, warning, info).
    • In addTypedMessage, route messages to the registered sink when globalAddToChatQueue is not active.
  2. Plain Shell Warning Collection & Stderr Forwarding (source/plain/shell.ts):

    • Register a sink at startup in runPlainShell that prints errors and warnings directly to process.stderr (leaving stdout clean for conversation output and JSON).
    • Buffer warnings in memory and include warnings: string[] in the --json output payload for both success and error exits.
    • Clean up the sink upon exit / shutdown.
  3. Preserve Accurate Config Errors (source/plain/initialize.ts):

    • Differentiate truly empty configs from JSON syntax errors or file read errors so exact parse diagnostics are passed through to stderr (matching nanocoder config list).
  4. Testing & Verification:

    • Add integration tests asserting stderr and --json warnings[] output for:
      • Malformed JSON in agents.config.json
      • Unknown hook lifecycle events
      • Formatters with missing command
      • Type mismatches in config (e.g., autoCommit: "yes")
      • Failing plugins (bad.mjs)
    • Run full verification suite (test:ava, test:types, test:format, test:knip, test:security).

rishu685 avatar Oct 08 '26 17:10 rishu685