Stop background tasks separately from chat turns

Keep Esc and /stop scoped to the foreground turn while exposing an explicit stop-tasks operation for supported chat backends. Add the protocol, server routing, app command, and coverage for task stopping and unsupported harnesses.
This commit is contained in:
2026-11-13 16:47:41 +00:00
parent c3c2c1bbc9
commit 2a182d7f17
19 changed files with 331 additions and 2 deletions
+7
View File
@@ -49,6 +49,10 @@ type chatPromptWithMessageID interface {
PromptWithMessageID(context.Context, string, string, string) error
}
type chatTaskStopper interface {
StopTasks(context.Context, string) (int, error)
}
// chatPromptWithFiles is implemented by a broker that can hand local files to
// the agent with a prompt. The server writes browser attachment bytes to private
// temporary files first; a browser URI is not readable by the agent service.
@@ -195,6 +199,9 @@ func handleChat(ctx context.Context, request protocol.Message, broker chatBroker
case protocol.ChatInterrupt:
handleChatSimple(ctx, request, broker, hub, write)
return true
case protocol.ChatStopTasks:
handleChatStopTasks(ctx, request, broker, write)
return true
case protocol.ChatReply:
handleChatSimple(ctx, request, broker, hub, write)
return true
+12
View File
@@ -746,6 +746,18 @@ func (m *chatManager) Shells(ctx context.Context, sessionID string) ([]protocol.
return nil, nil
}
func (m *chatManager) StopTasks(ctx context.Context, sessionID string) (int, error) {
adapter, err := m.brokerFor(ctx, sessionID)
if err != nil {
return 0, err
}
stopper, ok := adapter.(chatTaskStopper)
if !ok {
return 0, errors.New("this chat backend cannot stop background tasks separately")
}
return stopper.StopTasks(ctx, sessionID)
}
func (m *chatManager) Reply(ctx context.Context, request protocol.ChatReplyRequest) error {
adapter, err := m.brokerFor(ctx, request.SessionID)
if err != nil {
+39
View File
@@ -0,0 +1,39 @@
package server
import (
"context"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
func handleChatStopTasks(ctx context.Context, request protocol.Message, broker chatBroker, write func(protocol.Message) error) {
query := request.ChatQuery
if query == nil || !validChatSessionID(query.SessionID) {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid chat session id is required"})
return
}
if broker == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat is not configured on this server"})
return
}
stopper, ok := broker.(chatTaskStopper)
if !ok {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "this chat backend cannot stop background tasks separately"})
return
}
stopped, err := stopper.StopTasks(ctx, query.SessionID)
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
message := "No background tasks are running in this chat."
if stopped > 0 {
message = "Stopped background tasks."
}
_ = write(protocol.Message{
Type: protocol.ChatStopTasks, ID: request.ID,
ChatStopTasks: &protocol.ChatStopTasksResult{
SessionID: query.SessionID, Stopped: stopped, Message: message,
},
})
}
+31
View File
@@ -211,6 +211,16 @@ func (f *fakeChat) Interrupt(context.Context, string) error {
return f.interruptErr
}
type fakeTaskStopChat struct {
*fakeChat
stopped int
}
func (f *fakeTaskStopChat) StopTasks(context.Context, string) (int, error) {
f.stopped++
return f.stopped, nil
}
func (f *fakeChat) ReleaseSession(_ context.Context, sessionID string) error {
f.mu.Lock()
defer f.mu.Unlock()
@@ -850,6 +860,27 @@ func TestChatInterruptAndReply(t *testing.T) {
}
}
func TestChatStopTasksRepliesWithStoppedCount(t *testing.T) {
broker := &fakeTaskStopChat{fakeChat: scriptedChat()}
ctx, _, conn := dialChat(t, broker)
if err := wsjson.Write(ctx, conn, protocol.Message{
Type: protocol.ChatStopTasks, ID: "st1",
ChatQuery: &protocol.ChatQuery{SessionID: "ses_1"},
}); err != nil {
t.Fatal(err)
}
var response protocol.Message
if err := readTestFrame(ctx, conn, &response); err != nil {
t.Fatal(err)
}
if response.Type != protocol.ChatStopTasks || response.ID != "st1" || response.ChatStopTasks == nil {
t.Fatalf("stop tasks response = %+v", response)
}
if response.ChatStopTasks.Stopped != 1 || response.ChatStopTasks.Message != "Stopped background tasks." {
t.Fatalf("stop tasks result = %+v", response.ChatStopTasks)
}
}
// TestChatInterruptEndsRunningPrompt is the task evidence: it starts a
// long-running turn through the real opencode.Chat adapter over an httptest SSE
// fixture, sends chat_interrupt, and asserts the run ends. The fixture records