nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Bug] run --json prints nothing and session-end is skipped when the provider fails after a tool ran

Open diyaavirmani opened this issue 1 month ago • 2 comments

Description

If the provider call fails partway through a nanocoder run, for example with an HTTP 500 or a dropped connection after a tool has already run, the run exits 1 (which is right). But two things go wrong:

  • With --json, stdout is empty. CI gets no report at all, and no record that the tool already ran.
  • The session-end hook never runs, in text mode too.

The docs promise otherwise. docs/features/commands.md:209 says that on error the JSON object "still includes whatever toolCalls were captured before the failure, so partial progress isn't silently dropped". docs/features/hooks.md:124 says session-end fires in run / --plain. The comment at source/plain/shell.ts:245-247 says session-end runs on every exit path.

Environment

  • OS: macOS (Darwin 25.6.0, x86_64)
  • Node version: 22.23.3
  • Nanocoder version: main at 0925b881, and also v1.31.0
  • Provider: a local OpenAI-compatible test server with scripted responses
  • Model: n/a (scripted)

Steps to Reproduce

  1. Add hooks to agents.config.json: "hooks": {"session-start": [{"command": "echo start >> hooks.log"}], "session-end": [{"command": "echo end >> hooks.log"}]}
  2. Use a provider that answers the first request with a tool call (for example execute_bash with echo hi > marker.txt) and the second request with HTTP 500. I used a small local test server. Stopping a local server between the two requests should give the same result.
  3. Run nanocoder run --mode yolo --json "do it" > out.json.

Expected Behavior

  • The run exits 1.
  • out.json contains {"kind": "error", ...} with the execute_bash call in toolCalls.
  • hooks.log has both start and end.

Actual Behavior

  • The run exits 1, but out.json is empty.
  • marker.txt exists, so the tool did run.
  • hooks.log only has start.
  • stderr shows the provider's error message.

Controls: a normal successful run and a run that gives up after repeated empty responses both print the JSON report and run session-end. Only this path is affected. Same result on v1.31.0.

Logs/Screenshots

What I think is going on:

  • The error thrown by client.chat in source/plain/conversation.ts:324 isn't caught inside the conversation loop.
  • runPlainShell only has a try/finally around the conversation (source/plain/shell.ts:328-344). So the error reaches main().catch in source/cli.tsx:1047, which calls process.exit(1) directly and skips the graceful shutdown that runs session-end.

Additional Context

Proposed fix (happy to adjust):

  • Catch provider errors in the plain conversation loop and return the normal error report (kind: "error", the message, and the captured toolCalls).
  • Let the fatal path go through the graceful shutdown so session-end still runs.
  • The exit code stays 1.

For tests, I'd add specs for --json and text mode with a provider that fails after a tool call.

  • [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 could you take a look when you have a moment? I'd like to work on this one, so please assign it to me if that works for you.

diyaavirmani avatar Oct 18 '26 10:10 diyaavirmani

hi @addyCooks done with my prev pr can you assign me this

Vishesh-Verma-07 avatar Oct 19 '26 02:10 Vishesh-Verma-07

Thanks @diyaavirmani, confirmed. client.chat() in plain/conversation.ts has no error handling, and runPlainShell has only a try/finally. A provider error after a tool call therefore reaches main().catch in cli.tsx, which calls process.exit(1) directly. The --json report is never emitted and session-end never runs. I reproduced it at unit level: runPlainConversation rejects instead of returning an error outcome with the captured toolCalls.

Your plan is right: return the normal error outcome from the loop (keeping toolCalls), add a catch in runPlainShell as a backstop, and route the fatal path through gracefulShutdown(1). Please add a conversation spec and a shell spec (--json and text) for a provider that fails after a tool call.

Go ahead and open a PR. I'll assign this to you.

@Vishesh-Verma-07 thanks for the offer, but the reporter asked first, so this one is taken.

Thanks : )

addyCooks avatar Oct 19 '26 09:10 addyCooks