From 89126166be9087ce6b13c97d1a9b5c0bf0341000 Mon Sep 17 00:00:00 2001 From: Ivan Sekovanikj Date: Thu, 19 Nov 2026 14:32:37 +0100 Subject: [PATCH 1/2] fix(router): keep a tool's question when the app that was asked is closed A call waiting for the person to allow it reached the channel only in live updates, and the call itself only the socket of the app that asked, so an app reopened while it waited had no question to show and nothing to run. The question and the answer are now stored on the step at once, as a connector call's are, and a call still waiting for its answer is sent again, with its command, to a watcher that attaches later. AI-3392 --- CHANGELOG.md | 3 ++ .../internal/conversation/approvals_test.go | 18 +++++++ .../internal/conversation/conversation.go | 13 +++-- acceleration/internal/session/command_test.go | 48 +++++++++++++++++++ acceleration/internal/session/session.go | 34 +++++++++++++ 5 files changed, 113 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5030f63e4..68a86c4f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -740,6 +740,9 @@ A folder whose `agent.yaml` names a `plugins` list now fails to load with `plugi ### One `Sync` drops an agent's sandbox and a skill's scripts together The Go SDK's `Agent.Sync` and the Python SDK's `stream.define_agent` stored the config before its skills, so a sync that removed the sandbox and a skill's scripts at once was refused: the router still held the skill with its scripts. Each retry was refused the same way. When the stored config has a sandbox, the skills are now written first, then the config. Adding a sandbox and a skill's scripts in one sync still works (AI-3363). When the stored config has a sandbox and the router refuses the new config, the skill edits stay applied; a corrected Sync converges. +### A tool's question survives the app being closed while it waits + +A tool call waiting for the person to allow it went to the channel only in live updates, and the call itself only to the socket of the app that asked. An app killed while the question was up and reopened showed "Thinking…" until the tool's timeout failed the call. The question and the person's answer are now stored on the call's step as soon as they happen, as a connector call's are, and a call still waiting for its answer is sent again, with its command, to a watcher that attaches later. A reopened app shows the question and can run the call when the person allows it, with each SDK's `decide` as it is (AI-3392). ### The Go SDK's `Agent.Sync` works for an agent with skills diff --git a/acceleration/internal/conversation/approvals_test.go b/acceleration/internal/conversation/approvals_test.go index 025c334f5..9de8f60c8 100644 --- a/acceleration/internal/conversation/approvals_test.go +++ b/acceleration/internal/conversation/approvals_test.go @@ -52,6 +52,24 @@ func (s *DisplaySuite) TestAHeldCallsQuestionIsWrittenToChat() { s.Contains(s.raw(receipt.AssistantMessageID), "awaiting_approval") } +// The question of a tool that asks first is written to Chat as soon as the call waits, as a +// held connector call's is, and so is the answer: an app reopened while the call waits reads the +// question there, and one reopened after the answer is not asked again. +func (s *DisplaySuite) TestAToolsQuestionAndAnswerAreWrittenToChat() { + c := s.open("on_call") + c.DescribeTools(map[string]ToolDisplay{"issue_refund": {Approval: &ToolApproval{Title: "Refund this order?"}}}) + receipt, err := c.BeginCommand("command-a", "Refund order 42", "") + s.Require().NoError(err) + + c.Observe(agent.ToolStarted{ID: "call-1", Tool: "issue_refund", Arguments: `{}`, StartedAt: time.Now().UTC()}) + saved(s.T(), c) + s.Contains(s.raw(receipt.AssistantMessageID), "awaiting_approval") + + c.Observe(agent.ToolApprovalDecided{ID: "call-1", Allowed: true}) + saved(s.T(), c) + s.NotContains(s.raw(receipt.AssistantMessageID), "awaiting_approval") +} + // A call the config shows already has its step, which then waits rather than a second one. func (s *DisplaySuite) TestAShownCallWaitsOnTheStepItHas() { c := s.open("on_call") diff --git a/acceleration/internal/conversation/conversation.go b/acceleration/internal/conversation/conversation.go index 914982fdc..c60bdbad9 100644 --- a/acceleration/internal/conversation/conversation.go +++ b/acceleration/internal/conversation/conversation.go @@ -1797,8 +1797,10 @@ func (c *Conversation) Observe(event agent.Event) { return } // A tool's progress reaches watchers through live updates; Stream stores it once the - // reply settles. - unsaved := false + // reply settles. A question for a person, and their answer, are stored at once, as a held + // connector call's are (AwaitApproval): a client that opens the conversation while the call + // waits, such as an app reopened after it was closed, reads the question there. + unsaved, stored := false, false wasState := m.State coalesce := false switch e := event.(type) { @@ -1895,6 +1897,8 @@ func (c *Conversation) Observe(event agent.Event) { c.called(m, toolCall{id: e.ID, name: e.Tool, arguments: e.Arguments, startedAt: e.StartedAt}) c.state("tools") unsaved = true + step := toolStep(m, e.ID) + stored = step != nil && step.Status == "awaiting_approval" case agent.ToolApprovalDecided: if !c.acceptTurn(e.TurnID, false) { return @@ -1902,7 +1906,7 @@ func (c *Conversation) Observe(event agent.Event) { if !decided(m, e.ID, e.Allowed, e.Summary, time.Now()) { return } - unsaved = true + unsaved, stored = true, true case agent.ToolUpdated: if e.Verbatim || !c.acceptTurn(e.TurnID, false) || !reported(m, e.ID, e.Text) { return @@ -2042,6 +2046,9 @@ func (c *Conversation) Observe(event agent.Event) { if unsaved { m.Saved = false } + if stored { + c.save() + } now := time.Now() c.unpublished = coalesce && now.Sub(c.lastPublished) < answerEvery if !c.unpublished { diff --git a/acceleration/internal/session/command_test.go b/acceleration/internal/session/command_test.go index 293801820..f02e0a318 100644 --- a/acceleration/internal/session/command_test.go +++ b/acceleration/internal/session/command_test.go @@ -162,6 +162,54 @@ func (s *SessionSuite) TestAnApprovalIsRecordedOnlyForACallWaitingOnItsTurn() { s.False(running.DecideCommandTool("call-a", "command-a", "turn-a", true, ""), "the call was answered already") } +// A call waiting for the person to allow it is sent again, with its command, to a watcher that +// attaches later, such as an app reopened after the one that was asked was closed: it has the +// arguments to run it, and its result is taken. Once the person has answered, it is not. +func (s *SessionSuite) TestACallWaitingForThePersonIsSentAgainToALaterWatcher() { + s.persists() + running := s.commands() + receipt, err := running.persisted.BeginCommand("command-a", "Refund order A-1", "") + s.Require().NoError(err) + running.persisted.BindTurn(receipt.CommandID, "turn-a") + running.persisted.DescribeTools(map[string]persistent.ToolDisplay{ + "issue_refund": {Approval: &persistent.ToolApproval{Title: "Refund this order?"}}, + }) + first, detach := running.Watch() + + resolved := make(chan error, 1) + call := llm.ToolCall{ID: "call-a", TurnID: "turn-a", Name: "issue_refund", Arguments: `{"order":"A-1"}`} + go func() { + _, err := running.tools.Run(context.Background(), call) + resolved <- err + }() + s.Require().NotNil(awaitToolCall(first)) + running.persisted.Observe(agent.ToolStarted{ID: call.ID, TurnID: call.TurnID, Tool: call.Name, + Arguments: call.Arguments, StartedAt: time.Now()}) + detach() + + reopened, stop := running.Watch() + defer stop() + asked := awaitToolCall(reopened) + s.Require().NotNil(asked) + s.Equal(call.ID, asked.ID) + s.Equal(call.Arguments, asked.Arguments) + s.Equal("command-a", asked.CommandID) + s.Equal("turn-a", asked.TurnID) + s.True(asked.Run) + + s.True(running.DecideCommandTool(call.ID, "command-a", "turn-a", true, "")) + answered, stopAnswered := running.Watch() + defer stopAnswered() + select { + case event := <-answered: + _, tool := event.(ToolCall) + s.False(tool, "a call the person answered is not sent again") + default: + } + s.True(running.ResolveCommandTool(call.ID, "command-a", "turn-a", llm.TextParts("refunded"), "")) + s.Require().NoError(<-resolved) +} + // A call whose result came in, or that stopped, waits for nothing, though it is not yet gone. func (s *SessionSuite) TestAResolvedCallWaitsOnNoTurn() { tools := newBridge(time.Second, nil, func(ToolCall) error { return nil }) diff --git a/acceleration/internal/session/session.go b/acceleration/internal/session/session.go index 9d16ce1a0..14e2ca7aa 100644 --- a/acceleration/internal/session/session.go +++ b/acceleration/internal/session/session.go @@ -573,6 +573,7 @@ func (s *Session) WatchAs(seat Seat) (<-chan Event, func()) { // (WatchFrom), sent resumed and then what it missed after cursor. func (s *Session) watch(seat Seat, cursor string, resumed *Resumed) (<-chan Event, func()) { attached := &watcher{events: make(chan Event, watcherBuffer), seat: seat} + asking := s.askingPerson() s.mu.Lock() if s.state == Ended { @@ -616,6 +617,21 @@ func (s *Session) watch(seat Seat, cursor string, resumed *Resumed) (<-chan Even for _, unavailable := range s.connectorsUnavailable { attached.send(unavailable) } + // A call still waiting for the person to allow it is asked again of a watcher that comes + // later, such as an app reopened after the one that was asked was closed: the question is + // on the reply's step, and only a watcher that has the call can answer it and run it. + for _, pending := range asking { + if resent[pending.ID] { + continue + } + if resent == nil { + resent = map[string]bool{} + } + resent[pending.ID] = true + replayed := s.placed(pending) + replayed.Run = s.seatRuns(seat, replayed.Name) + attached.send(replayed) + } if seat.ReplayPendingVoiceTools && s.Voicing() && s.tools != nil { for _, pending := range s.tools.Pending() { if resent[pending.ID] { @@ -2363,6 +2379,24 @@ func (s *Session) broadcast(event Event) { } } +// askingPerson is the calls still waiting for the person to allow them, on the step of the +// conversation's reply, each with its command as askTool sends it. It is read without s.mu, +// since the conversation broadcasts to the session under its own lock. +func (s *Session) askingPerson() []ToolCall { + if s.persisted == nil || s.tools == nil { + return nil + } + var asking []ToolCall + for _, pending := range s.tools.Pending() { + if !s.persisted.AwaitsApproval(pending.ID) { + continue + } + pending.CommandID, _ = s.persisted.CommandForTurn(pending.TurnID) + asking = append(asking, pending) + } + return asking +} + // placed is call with what it is part of filled in: this session, its conversation, and // the session's own user when the agent did not say whose turn asked for it. func (s *Session) placed(call ToolCall) ToolCall { From b7c830da0f06d2ab5f9cf8dfc1fa3d528eb290d1 Mon Sep 17 00:00:00 2001 From: Ivan Sekovanikj Date: Thu, 19 Nov 2026 15:34:47 +0100 Subject: [PATCH 2/2] fix(router): don't re-send a call answered while a watcher attaches The waiting calls are read before s.mu, so an answer could land between the read and the send. DecideCommandTool now records the answer on the call before the conversation hears it, and the replay re-checks under s.mu that the call still waits for one. Watch's comment says what it replays. AI-3392 --- acceleration/internal/session/command_test.go | 13 ++++++++++++ acceleration/internal/session/session.go | 14 ++++++++----- acceleration/internal/session/tools.go | 20 +++++++++++++++++++ 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/acceleration/internal/session/command_test.go b/acceleration/internal/session/command_test.go index f02e0a318..424d417fc 100644 --- a/acceleration/internal/session/command_test.go +++ b/acceleration/internal/session/command_test.go @@ -197,6 +197,19 @@ func (s *SessionSuite) TestACallWaitingForThePersonIsSentAgainToALaterWatcher() s.Equal("turn-a", asked.TurnID) s.True(asked.Run) + // Answered between the read of what is asked and the send: the call carries the answer + // before the conversation does, and the watcher is not asked. + running.tools.decide(call.ID, call.TurnID) + s.True(running.persisted.AwaitsApproval(call.ID), "the conversation has not heard the answer yet") + racing, stopRacing := running.Watch() + defer stopRacing() + select { + case event := <-racing: + _, tool := event.(ToolCall) + s.False(tool, "a call answered before the send is not sent") + default: + } + s.True(running.DecideCommandTool(call.ID, "command-a", "turn-a", true, "")) answered, stopAnswered := running.Watch() defer stopAnswered() diff --git a/acceleration/internal/session/session.go b/acceleration/internal/session/session.go index 14e2ca7aa..1fc8f3fcf 100644 --- a/acceleration/internal/session/session.go +++ b/acceleration/internal/session/session.go @@ -550,9 +550,10 @@ func (w *watcher) send(event Event) bool { // Watch attaches a consumer and returns it along with the way to detach. // -// Every watcher sees everything from the moment it attached. Nothing is replayed: a caller -// that connects late has missed the conversation, and a control channel that opened with a -// backlog would have it answering tool calls that timed out before it arrived. +// Every watcher sees everything from the moment it attached. Nothing else is replayed, but a +// call still waiting for the person to allow it is asked again: a caller that connects late has +// missed the conversation, and a control channel that opened with a backlog would have it +// answering tool calls that timed out before it arrived. func (s *Session) Watch() (<-chan Event, func()) { return s.WatchAs(Seat{Owner: OwnerOf(s.Spec())}) } @@ -619,9 +620,11 @@ func (s *Session) watch(seat Seat, cursor string, resumed *Resumed) (<-chan Even } // A call still waiting for the person to allow it is asked again of a watcher that comes // later, such as an app reopened after the one that was asked was closed: the question is - // on the reply's step, and only a watcher that has the call can answer it and run it. + // on the reply's step, and only a watcher that has the call can answer it and run it. One + // answered or finished since asking was read is not: the answer is recorded on the call + // before the conversation hears it (DecideCommandTool). for _, pending := range asking { - if resent[pending.ID] { + if resent[pending.ID] || !s.tools.asking(pending.ID) { continue } if resent == nil { @@ -1838,6 +1841,7 @@ func (s *Session) DecideCommandTool(id, commandID, turnID string, allowed bool, if !bound || expected != commandID || !s.tools.waitsOn(id, turnID) { return false } + s.tools.decide(id, turnID) s.persisted.Observe(agent.ToolApprovalDecided{ID: id, TurnID: turnID, Allowed: allowed, Summary: summary}) return true } diff --git a/acceleration/internal/session/tools.go b/acceleration/internal/session/tools.go index b3c5eb004..098f2a720 100644 --- a/acceleration/internal/session/tools.go +++ b/acceleration/internal/session/tools.go @@ -75,6 +75,8 @@ type pendingTool struct { ctx context.Context answer chan toolResult resolved bool + // decided is set once the person answered the call's question (decide). + decided bool } // toolResult is what the caller said happened. @@ -217,6 +219,24 @@ func (b *bridge) turnOf(id string) (string, bool) { return pending.turnID, waiting } +// decide records that the person answered the question of the call id, waiting on the turn. +func (b *bridge) decide(id, turnID string) { + b.mu.Lock() + defer b.mu.Unlock() + if pending, waiting := b.pending[id]; waiting && pending.turnID == turnID { + pending.decided = true + b.pending[id] = pending + } +} + +// asking reports whether the call id still waits for the person's answer to its question. +func (b *bridge) asking(id string) bool { + b.mu.Lock() + defer b.mu.Unlock() + pending, waiting := b.pending[id] + return waiting && !b.closed && !pending.resolved && !pending.decided && pending.ctx.Err() == nil +} + // Waiting reports whether the call is still waiting for its result. func (b *bridge) Waiting(id string) bool { b.mu.Lock()