fix(gateway): let a streamed chat turn finish after its stream closes (#1848)
deploy / plan (push) Successful in 7s
deploy / gate (push) Successful in 7m49s
deploy / agent-image (push) Successful in 12s
deploy / dist-zip (push) Skipped
deploy / archied-image (push) Successful in 2m24s

This commit is contained in:
sam committed 2026-11-10 20:14:04 +11:00
1 parent e3ffefdc7d
commit 13ec693726
3 files changed
+76 -10

No files matched your search

+1
View File
@@ -24,6 +24,7 @@ release's per-component sections are labelled beneath its heading.
### 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.
- 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.
+11 -10
View File
@@ -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
// 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) {
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}
run := func(turnCtx context.Context) ChatEvent {
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}
}
resolvedID := id
if resolved, err := a.Router.ResolveSessionKey(ctx, in); err == nil {
if resolved, err := a.Router.ResolveSessionKey(turnCtx, in); err == nil {
resolvedID = resolved
}
return ChatEvent{Kind: "done", Text: reply, SessionID: resolvedID}
}
turnCtx := context.WithoutCancel(ctx)
result := make(chan ChatEvent, 1)
if a.Turns == nil {
go func() { result <- run(ctx) }()
go func() { result <- run(turnCtx) }()
} 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
// 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 occupies is stopped so the turn cannot keep running unobserved.
func (a *LocalChatAdapter) pumpTurnEvents(ctx context.Context, id string, events chan ChatEvent, pending, result <-chan ChatEvent) {
// It returns when the caller's context ends, leaving the turn running.
func pumpTurnEvents(ctx context.Context, events chan ChatEvent, pending, result <-chan ChatEvent) {
emit := func(event ChatEvent) {
select {
case events <- event:
@@ -269,9 +273,6 @@ func (a *LocalChatAdapter) pumpTurnEvents(ctx context.Context, id string, events
}
}
case <-ctx.Done():
if a.Turns != nil {
a.Turns.Stop(id)
}
return
}
}
+64
View File
@@ -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")
}
})
}
}