Commit 0176f95
committed
fix(#2457): retry MCP toolsets after tool calls within the same turn
When an MCP server is configured but unavailable at session start, the
agent now retries automatically after every tool-call batch — within the
same user turn — without requiring a new user message.
## Core changes
### MCP double-watcher race (pkg/tools/mcp/mcp.go)
Added watcherAlive bool to Toolset. Toolset.Start() only spawns
go watchConnection(...) when !watcherAlive. The goroutine clears the
flag on all exit paths via defer. This prevents reprobe() from spawning
a second watcher while an existing one is mid-backoff (ts.started==false
but goroutine alive), which would cause racing doStart() calls and unsafe
close/recreate of ts.restarted.
### Failure deduplication + recovery notices (pkg/tools/startable.go)
ShouldReportFailure() returns true exactly once per failure streak,
suppressing repeated 'start failed' warnings on every retry.
ConsumeRecovery() returns true exactly once when a previously-failed
toolset successfully starts, triggering a 'now available' warning.
Both surface via WarningEvent -> notification.WarningCmd() (persistent
TUI notifications that stay until dismissed).
### Reprobe after each tool-call batch (pkg/runtime/loop.go)
reprobe() is called after every tool-call batch. It re-runs
ensureToolSetsAreStarted() without emitting MCPInitStarted/Finished
events (no TUI spinner flicker), emits any pending warnings, and emits
a ToolsetInfo event when new tools appear. The updated tool list is
picked up by the top-of-loop getTools() on the next iteration, so the
model sees new tools in its very next response within the same user turn.
### TUI (pkg/agent/agent.go, pkg/runtime/loop.go, pkg/runtime/event.go)
DrainWarnings() now includes both failure and recovery messages.
WarningEvent used for all toolset lifecycle notifications.
## Tests
- pkg/tools/startable_test.go: ShouldReportFailure/ConsumeRecovery
behaviour (one warning per streak, recovery fires once, Stop resets)
- pkg/agent/agent_test.go: TestAgentReProbeEmitsWarningThenNotice,
TestAgentNoDuplicateStartWarnings
- pkg/runtime/runtime_test.go: TestReprobe_NewToolsAvailableAfterToolCall,
TestReprobe_NoChangeMeansNoExtraEvents
Fixes #2457
Assisted-By: docker-agent1 parent 15945f4 commit 0176f95
7 files changed
Lines changed: 596 additions & 31 deletions
File tree
- pkg
- agent
- runtime
- tools
- mcp
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
260 | 260 | | |
261 | 261 | | |
262 | 262 | | |
263 | | - | |
264 | | - | |
265 | | - | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
266 | 273 | | |
267 | 274 | | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
268 | 281 | | |
269 | 282 | | |
270 | 283 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
47 | 75 | | |
48 | 76 | | |
49 | 77 | | |
| |||
210 | 238 | | |
211 | 239 | | |
212 | 240 | | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
100 | 100 | | |
101 | 101 | | |
102 | 102 | | |
103 | | - | |
| 103 | + | |
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
| |||
163 | 163 | | |
164 | 164 | | |
165 | 165 | | |
166 | | - | |
| 166 | + | |
167 | 167 | | |
168 | 168 | | |
169 | 169 | | |
| |||
382 | 382 | | |
383 | 383 | | |
384 | 384 | | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
385 | 399 | | |
386 | 400 | | |
387 | 401 | | |
| |||
575 | 589 | | |
576 | 590 | | |
577 | 591 | | |
578 | | - | |
579 | | - | |
580 | | - | |
581 | | - | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
582 | 597 | | |
| 598 | + | |
583 | 599 | | |
584 | | - | |
585 | | - | |
586 | | - | |
587 | | - | |
588 | | - | |
589 | 600 | | |
590 | 601 | | |
591 | 602 | | |
| |||
616 | 627 | | |
617 | 628 | | |
618 | 629 | | |
619 | | - | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
620 | 633 | | |
621 | 634 | | |
622 | | - | |
623 | | - | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
624 | 638 | | |
625 | | - | |
626 | | - | |
627 | | - | |
628 | 639 | | |
629 | 640 | | |
630 | 641 | | |
| |||
669 | 680 | | |
670 | 681 | | |
671 | 682 | | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
0 commit comments