detached b25b860

fix(core): repair dangling tool_use on every request, not just load

A turn aborted mid-flight (cancel, connection drop, dev-server
ECONNREFUSED) can leave an assistant tool_use block with no matching
tool_result in the live transcript. repairToolUseResultPairs already
fixes this, but only ran in OpenSession (load time), so an in-process
abort left the transcript broken until restart. The next request was
then rejected by Anthropic/OpenAI with 'tool_use ids were found without
tool_result blocks'.

Run the same repair on the outbound messages in oneTurn. It is pure and
a no-op on valid transcripts, so there is no hot-path cost beyond a
single linear scan and no behavior change for healthy sessions.
commit
b25b860b0988dad734f065f9ab1d337ccec357b4
author
patriceckhart · · 2026-06-09 10:56 UTC
parent
f7bf4a9
signature
unsigned
1 file · +11 -3
M packages/core/agent.go +11 -3
@@ -502,9 +502,17 @@ func (a *Agent) dropLastAssistantMessage() {
502502 // and the assembled assistant message (already appended to the transcript).
503503 func (a *Agent) oneTurn(ctx context.Context, sink func(AgentEvent)) (provider.StopReason, provider.Message, error) {
504504 req := provider.Request{
505- Model: a.Model,
506- System: a.System,
507- Messages: a.Messages(),
505+ Model: a.Model,
506+ System: a.System,
507+ // Repair any dangling tool_use blocks before sending. A turn
508+ // aborted mid-flight (cancel, connection drop, ECONNREFUSED to a
509+ // dev server, etc.) can leave an assistant tool_use with no
510+ // matching tool_result in the live transcript. The load-time
511+ // repair in OpenSession only runs on restart, so without this the
512+ // next in-process request is rejected by providers like Anthropic
513+ // with "tool_use ids were found without tool_result blocks". The
514+ // repair is pure and a no-op on already-valid transcripts.
515+ Messages: repairToolUseResultPairs(a.Messages()),
508516 Tools: a.Tools.Specs(),
509517 Reasoning: a.Reasoning,
510518 }
@@ -502,9 +502,17 @@ func (a *Agent) dropLastAssistantMessage() {
502 // and the assembled assistant message (already appended to the transcript).502 // and the assembled assistant message (already appended to the transcript).
503 func (a *Agent) oneTurn(ctx context.Context, sink func(AgentEvent)) (provider.StopReason, provider.Message, error) {503 func (a *Agent) oneTurn(ctx context.Context, sink func(AgentEvent)) (provider.StopReason, provider.Message, error) {
504 req := provider.Request{504 req := provider.Request{
505- Model: a.Model,505+ Model: a.Model,
506- System: a.System,506+ System: a.System,
507- Messages: a.Messages(),507+ // Repair any dangling tool_use blocks before sending. A turn
508+ // aborted mid-flight (cancel, connection drop, ECONNREFUSED to a
509+ // dev server, etc.) can leave an assistant tool_use with no
510+ // matching tool_result in the live transcript. The load-time
511+ // repair in OpenSession only runs on restart, so without this the
512+ // next in-process request is rejected by providers like Anthropic
513+ // with "tool_use ids were found without tool_result blocks". The
514+ // repair is pure and a no-op on already-valid transcripts.
515+ Messages: repairToolUseResultPairs(a.Messages()),
508 Tools: a.Tools.Specs(),516 Tools: a.Tools.Specs(),
509 Reasoning: a.Reasoning,517 Reasoning: a.Reasoning,
510 }518 }

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 476

↑↓ moveenter openesc close