nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Bug] A session saved with an unanswered tool call makes every request after resume fail

Open addyCooks opened this issue 2 weeks ago • 1 comments

Description

If the saved history ends with an assistant message that has tool_calls but no matching tool result, the next request fails with "Tool result is missing for tool call ...". The session is then unusable until the user starts a new one. The history can end up like this when Nanocoder quits while a tool call is waiting for confirmation: the loop pairs unfinished calls with cancellation results on Escape and on repeated-call stops (conversation-loop.tsx around lines 836-900), but not on exit, and nothing repairs the history when it is loaded.

I reproduced the failing request with a history of that shape. I did not run the full "quit while a confirmation prompt is open" scenario, because it needs a TTY and a live model.

Environment

  • OS: Windows 11 Home 10.0.26300
  • Node version: v22.22.3
  • Nanocoder version: 1.32.0 (built from main at 5df35b3a)
  • Provider: OpenAI-compatible (local mock server)
  • Model: Not model-specific

Steps to Reproduce

  1. Start a local OpenAI-compatible mock server that answers any request.
  2. Create a client: AISDKClient.create({name: 'mock', type: 'openai', models: ['m'], config: {baseURL: 'http://127.0.0.1:<port>/v1', apiKey: 'k'}}).
  3. Call client.chat([...]) with these messages: system, user "write a file", assistant with tool_calls [{id: "call_1", name: "write_file"}] and no tool message, then user "continue".

Likely real-world trigger (not run): in normal mode ask for a file write, quit while the confirmation prompt is open, then run nanocoder --continue and send a prompt.

Expected Behavior

The history is repaired when it is saved or loaded (for example, each unanswered call gets a "cancelled" result) and the request goes through.

Actual Behavior

The request fails before it reaches the server (server hits: 0).

Logs/Screenshots

Tool result is missing for tool call call_1.

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 (main)

Suggested fix:

  • Pair unanswered calls with cancellation results in the exit flush: source/hooks/useSessionAutosave.ts:379 (flush) can use createCancellationResults from source/tools/runtime/tool-cancellation.ts, as conversation-loop.tsx does.
  • Also repair on load, for sessions that are already saved: add a helper next to dropOrphanedToolResults in source/ai-sdk-client/converters/message-converter.ts:48 that adds a cancellation result for any assistant tool_calls without a result, and call it where a session is resumed (source/hooks/useAppHandlers.tsx, resumeSession).
  • Add a test with [user, assistant{tool_calls}, user].

addyCooks avatar Nov 16 '26 09:11 addyCooks

I'd like to work on this. Could you assign it to me?

Plan: pair unanswered tool calls with cancellation results in the autosave exit flush (createCancellationResults, as the conversation loop already does), and repair histories that are already saved when a session is resumed, with a helper next to dropOrphanedToolResults, plus a test with [user, assistant{tool_calls}, user]. I'll wait for the assignment before opening a PR.

PloneMraz avatar Nov 18 '26 03:11 PloneMraz