Parallel tool results race when writing completedToolCalls
What happened
sessionAgent.Run records successful tool names in completedToolCalls from OnToolResult. The comment says those streaming callbacks run sequentially. They do not.
fantasy v0.45.1 runs tools with Parallel set on separate goroutines (a semaphore of 5) and calls OnToolResult from each one, inside executeSingleTool. Concurrent writes to that map are a fatal concurrent map writes crash.
The map is read only after Stream returns, in sendChannelReply. Stream waits for the tool goroutines before returning, so the reads do not race with the writes. The writes race with each other.
How to reproduce
Finish several parallel tools at the same time. go test -race on the regression test reports a data race at the completedToolCalls write in internal/agent/agent.go, with both stacks in (*sessionAgent).Run OnToolResult, called from fantasy.(*agent).executeSingleTool.
Without the race detector, the same overlap can abort the process.
Expected
Recording a completed tool name is safe when several parallel tools finish together.
Source: charmbracelet/crush