[Bug] run --json prints nothing and session-end is skipped when the provider fails after a tool ran
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-endhook 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
- Add hooks to
agents.config.json:"hooks": {"session-start": [{"command": "echo start >> hooks.log"}], "session-end": [{"command": "echo end >> hooks.log"}]} - Use a provider that answers the first request with a tool call (for example
execute_bashwithecho 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. - Run
nanocoder run --mode yolo --json "do it" > out.json.
Expected Behavior
- The run exits 1.
out.jsoncontains{"kind": "error", ...}with theexecute_bashcall intoolCalls.hooks.loghas bothstartandend.
Actual Behavior
- The run exits 1, but
out.jsonis empty. marker.txtexists, so the tool did run.hooks.logonly hasstart.- 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.chatinsource/plain/conversation.ts:324isn't caught inside the conversation loop. runPlainShellonly has atry/finallyaround the conversation (source/plain/shell.ts:328-344). So the error reachesmain().catchinsource/cli.tsx:1047, which callsprocess.exit(1)directly and skips the graceful shutdown that runssession-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 capturedtoolCalls). - Let the fatal path go through the graceful shutdown so
session-endstill 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.
hi @addyCooks done with my prev pr can you assign me this
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 : )