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:
@@ -426,6 +426,44 @@ func (c *Chat) Interrupt(ctx context.Context, id string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// StopTasks uses Claude Code's separate stop_task control request. It does not
|
||||
// send interrupt, so the foreground turn and its queued prompts remain alive.
|
||||
func (c *Chat) StopTasks(ctx context.Context, id string) (int, error) {
|
||||
s := c.session(id)
|
||||
if s == nil {
|
||||
return 0, nil
|
||||
}
|
||||
s.awaitReady(ctx)
|
||||
s.mu.Lock()
|
||||
proc := s.proc
|
||||
var tasks []string
|
||||
for toolID, snapshot := range s.shells {
|
||||
if !snapshot.background || snapshot.shell.Status != protocol.ChatShellRunning {
|
||||
continue
|
||||
}
|
||||
taskID := snapshot.taskID
|
||||
if taskID == "" {
|
||||
taskID = toolID
|
||||
}
|
||||
tasks = append(tasks, taskID)
|
||||
}
|
||||
s.mu.Unlock()
|
||||
if proc == nil {
|
||||
return 0, nil
|
||||
}
|
||||
stopped := 0
|
||||
for _, taskID := range tasks {
|
||||
requestCtx, cancel := context.WithTimeout(ctx, controlRequestTimeout)
|
||||
_, err := proc.request(requestCtx, "stop_task", map[string]any{"task_id": taskID})
|
||||
cancel()
|
||||
if err != nil {
|
||||
return stopped, err
|
||||
}
|
||||
stopped++
|
||||
}
|
||||
return stopped, nil
|
||||
}
|
||||
|
||||
// ActivateSkill has no out-of-band equivalent in Claude Code; skills resolve
|
||||
// through the CLI's own /skill mechanism.
|
||||
func (c *Chat) ActivateSkill(context.Context, string, string) error {
|
||||
|
||||
@@ -257,6 +257,57 @@ func TestInterruptSettlesTheTurn(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestInterruptLeavesBackgroundTaskAndStopTasksEndsIt(t *testing.T) {
|
||||
chat := testChat(t)
|
||||
session := newCreatedChat(t, chat)
|
||||
if err := chat.Prompt(context.Background(), session.ID, "background task"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
eventually(t, "background shell", func() bool {
|
||||
shells, err := chat.Shells(context.Background(), session.ID)
|
||||
return err == nil && len(shells) == 1 && shells[0].Status == protocol.ChatShellRunning
|
||||
})
|
||||
|
||||
// The fake turn has already yielded its result, so mark the session running
|
||||
// to exercise the same control path used by an active foreground turn.
|
||||
s := chat.session(session.ID)
|
||||
s.mu.Lock()
|
||||
s.running = true
|
||||
s.mu.Unlock()
|
||||
if err := chat.Interrupt(context.Background(), session.ID); err != nil {
|
||||
t.Fatalf("interrupt: %v", err)
|
||||
}
|
||||
if shells, err := chat.Shells(context.Background(), session.ID); err != nil || len(shells) != 1 || shells[0].Status != protocol.ChatShellRunning {
|
||||
t.Fatalf("interrupt ended background task: shells=%+v err=%v", shells, err)
|
||||
}
|
||||
|
||||
// A foreground tool may still be running while the operator stops
|
||||
// background work. It must not be included in the stop_task requests.
|
||||
s.mu.Lock()
|
||||
s.shells["toolu_foreground"] = &shellSnapshot{
|
||||
shell: protocol.ChatShell{
|
||||
ID: "toolu_foreground",
|
||||
Status: protocol.ChatShellRunning,
|
||||
Command: "foreground command",
|
||||
},
|
||||
}
|
||||
s.mu.Unlock()
|
||||
stopped, err := chat.StopTasks(context.Background(), session.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("stop tasks: %v", err)
|
||||
}
|
||||
if stopped != 1 {
|
||||
t.Fatalf("stopped tasks = %d, want only the background task", stopped)
|
||||
}
|
||||
eventually(t, "stopped shell", func() bool {
|
||||
shells, err := chat.Shells(context.Background(), session.ID)
|
||||
if err != nil || len(shells) != 1 {
|
||||
return false
|
||||
}
|
||||
return shells[0].ID == "toolu_foreground" && shells[0].Status == protocol.ChatShellRunning
|
||||
})
|
||||
}
|
||||
|
||||
func TestModelChoiceIsAppliedAndProcessResumes(t *testing.T) {
|
||||
chat := testChat(t)
|
||||
session := newCreatedChat(t, chat)
|
||||
|
||||
+5
@@ -243,6 +243,11 @@ def handle(line):
|
||||
state["pending"] = None
|
||||
send({"type": "result", "subtype": "error_during_execution", "is_error": True,
|
||||
"result": "Interrupted", "session_id": state["session"]})
|
||||
elif subtype == "stop_task":
|
||||
respond(value.get("request_id"), {})
|
||||
send({"type": "system", "subtype": "task_notification",
|
||||
"task_id": request.get("task_id"), "tool_use_id": "toolu_bg_fake",
|
||||
"status": "stopped", "summary": "Background task stopped"})
|
||||
elif subtype == "get_usage":
|
||||
respond(value.get("request_id"), {"session": {
|
||||
"total_cost_usd": 0.25,
|
||||
|
||||
Reference in New Issue
Block a user