fix(gateway): let a streamed chat turn finish after its stream closes (#1848)
This commit is contained in:
3 files changed
+76
-10
No files matched your search
@@ -24,6 +24,7 @@ release's per-component sections are labelled beneath its heading.
|
|||||||
|
|
||||||
### archie-ui
|
### archie-ui
|
||||||
|
|
||||||
|
- A chat reply is no longer lost when you switch conversation or reload mid-answer: the turn finishes and its reply is in the conversation when you come back. Stop is the only way to cancel it.
|
||||||
- The task page offers every action the task list does.
|
- The task page offers every action the task list does.
|
||||||
- Workflows have row actions, enabling or disabling one confirms with a toast, and the editor toolbar wraps on narrow screens.
|
- Workflows have row actions, enabling or disabling one confirms with a toast, and the editor toolbar wraps on narrow screens.
|
||||||
- The dashboard lists five tasks needing attention, not every one.
|
- The dashboard lists five tasks needing attention, not every one.
|
||||||
|
|||||||
@@ -220,8 +220,12 @@ func (a *LocalChatAdapter) stream(ctx context.Context, in Inbound, id string, ev
|
|||||||
|
|
||||||
// runStreamedTurn runs one prepared turn on its session lane and pumps its
|
// runStreamedTurn runs one prepared turn on its session lane and pumps its
|
||||||
// events to the caller. The terminal event comes after every queued event.
|
// events to the caller. The terminal event comes after every queued event.
|
||||||
|
// The turn outlives its stream: a caller that disconnects (a conversation
|
||||||
|
// switch, a reload) stops watching, and the turn still finishes and saves its
|
||||||
|
// reply. Only Cancel stops it.
|
||||||
func (a *LocalChatAdapter) runStreamedTurn(ctx context.Context, in Inbound, id string, events chan ChatEvent) {
|
func (a *LocalChatAdapter) runStreamedTurn(ctx context.Context, in Inbound, id string, events chan ChatEvent) {
|
||||||
pending := make(chan ChatEvent, 32)
|
pending := make(chan ChatEvent, 32)
|
||||||
|
// Once the caller has gone, done is closed and the turn's events drop.
|
||||||
stream := localChatStream{done: ctx.Done(), events: pending, sessionID: id}
|
stream := localChatStream{done: ctx.Done(), events: pending, sessionID: id}
|
||||||
run := func(turnCtx context.Context) ChatEvent {
|
run := func(turnCtx context.Context) ChatEvent {
|
||||||
reply, err := a.Router.streamTurn(turnCtx, in, stream)
|
reply, err := a.Router.streamTurn(turnCtx, in, stream)
|
||||||
@@ -229,25 +233,25 @@ func (a *LocalChatAdapter) runStreamedTurn(ctx context.Context, in Inbound, id s
|
|||||||
return ChatEvent{Kind: "error", Text: err.Error(), SessionID: id}
|
return ChatEvent{Kind: "error", Text: err.Error(), SessionID: id}
|
||||||
}
|
}
|
||||||
resolvedID := id
|
resolvedID := id
|
||||||
if resolved, err := a.Router.ResolveSessionKey(ctx, in); err == nil {
|
if resolved, err := a.Router.ResolveSessionKey(turnCtx, in); err == nil {
|
||||||
resolvedID = resolved
|
resolvedID = resolved
|
||||||
}
|
}
|
||||||
return ChatEvent{Kind: "done", Text: reply, SessionID: resolvedID}
|
return ChatEvent{Kind: "done", Text: reply, SessionID: resolvedID}
|
||||||
}
|
}
|
||||||
|
turnCtx := context.WithoutCancel(ctx)
|
||||||
result := make(chan ChatEvent, 1)
|
result := make(chan ChatEvent, 1)
|
||||||
if a.Turns == nil {
|
if a.Turns == nil {
|
||||||
go func() { result <- run(ctx) }()
|
go func() { result <- run(turnCtx) }()
|
||||||
} else {
|
} else {
|
||||||
a.Turns.Submit(ctx, id, func(turnCtx context.Context) { result <- run(turnCtx) })
|
a.Turns.Submit(turnCtx, id, func(laneCtx context.Context) { result <- run(laneCtx) })
|
||||||
}
|
}
|
||||||
a.pumpTurnEvents(ctx, id, events, pending, result)
|
pumpTurnEvents(ctx, events, pending, result)
|
||||||
}
|
}
|
||||||
|
|
||||||
// pumpTurnEvents streams one turn's events into its caller's channel: every
|
// pumpTurnEvents streams one turn's events into its caller's channel: every
|
||||||
// delta and tool event as it arrives, then the terminal event after them.
|
// delta and tool event as it arrives, then the terminal event after them.
|
||||||
// A caller whose context ends mid-stream has stopped the turn, and the lane
|
// It returns when the caller's context ends, leaving the turn running.
|
||||||
// it occupies is stopped so the turn cannot keep running unobserved.
|
func pumpTurnEvents(ctx context.Context, events chan ChatEvent, pending, result <-chan ChatEvent) {
|
||||||
func (a *LocalChatAdapter) pumpTurnEvents(ctx context.Context, id string, events chan ChatEvent, pending, result <-chan ChatEvent) {
|
|
||||||
emit := func(event ChatEvent) {
|
emit := func(event ChatEvent) {
|
||||||
select {
|
select {
|
||||||
case events <- event:
|
case events <- event:
|
||||||
@@ -269,9 +273,6 @@ func (a *LocalChatAdapter) pumpTurnEvents(ctx context.Context, id string, events
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
if a.Turns != nil {
|
|
||||||
a.Turns.Stop(id)
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,64 @@
|
|||||||
|
package gateway
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"log/slog"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
||||||
|
)
|
||||||
|
|
||||||
|
// A streamed turn outlives its caller: closing the stream must not cancel
|
||||||
|
// the turn, and only Cancel stops it.
|
||||||
|
func TestStreamedTurnSurvivesCallerDisconnect(t *testing.T) {
|
||||||
|
for _, tc := range []struct {
|
||||||
|
name string
|
||||||
|
stop bool
|
||||||
|
cancelled bool
|
||||||
|
}{
|
||||||
|
{name: "caller disconnects", stop: false, cancelled: false},
|
||||||
|
{name: "caller disconnects then stops", stop: true, cancelled: true},
|
||||||
|
} {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
release := make(chan struct{})
|
||||||
|
finished := make(chan error, 1)
|
||||||
|
router := &Router{LLMStream: func(ctx context.Context, _ Inbound, stream TurnStream) (string, error) {
|
||||||
|
stream.Delta("working")
|
||||||
|
select {
|
||||||
|
case <-release:
|
||||||
|
case <-ctx.Done():
|
||||||
|
}
|
||||||
|
finished <- ctx.Err()
|
||||||
|
return "reply", ctx.Err()
|
||||||
|
}}
|
||||||
|
turns := messaging.NewTurns(slog.New(slog.DiscardHandler))
|
||||||
|
adapter := &LocalChatAdapter{Router: router, Turns: turns}
|
||||||
|
|
||||||
|
ctx, disconnect := context.WithCancel(context.Background())
|
||||||
|
in := Inbound{Message: messaging.Message{Text: "hello"}}
|
||||||
|
done := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
adapter.runStreamedTurn(ctx, in, "s1", make(chan ChatEvent, 32))
|
||||||
|
close(done)
|
||||||
|
}()
|
||||||
|
time.Sleep(10 * time.Millisecond)
|
||||||
|
disconnect()
|
||||||
|
<-done
|
||||||
|
|
||||||
|
if tc.stop {
|
||||||
|
turns.Stop("s1")
|
||||||
|
} else {
|
||||||
|
close(release)
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case err := <-finished:
|
||||||
|
if got := err != nil; got != tc.cancelled {
|
||||||
|
t.Fatalf("turn cancelled = %v, want %v (err %v)", got, tc.cancelled, err)
|
||||||
|
}
|
||||||
|
case <-time.After(2 * time.Second):
|
||||||
|
t.Fatal("turn never finished")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user