Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 18 additions & 0 deletions acceleration/internal/conversation/approvals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
13 changes: 10 additions & 3 deletions acceleration/internal/conversation/conversation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -1895,14 +1897,16 @@ 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
}
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
Expand Down Expand Up @@ -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 {
Expand Down
61 changes: 61 additions & 0 deletions acceleration/internal/session/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,67 @@ 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)

// 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()
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 })
Expand Down
44 changes: 41 additions & 3 deletions acceleration/internal/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())})
}
Expand All @@ -573,6 +574,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 {
Expand Down Expand Up @@ -616,6 +618,23 @@ 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. 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 {
Comment thread
isekovanic marked this conversation as resolved.
Resolved
if resent[pending.ID] || !s.tools.asking(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] {
Expand Down Expand Up @@ -1822,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
}
Expand Down Expand Up @@ -2363,6 +2383,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 {
Expand Down
20 changes: 20 additions & 0 deletions acceleration/internal/session/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()
Expand Down
Loading