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:
@@ -322,6 +322,26 @@ func (c *Chat) Interrupt(ctx context.Context, id string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// StopTasks terminates background shell operations, without interrupting the
|
||||
// session's foreground turn.
|
||||
func (c *Chat) StopTasks(ctx context.Context, id string) (int, error) {
|
||||
shells, err := c.Shells(ctx, id)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
stopped := 0
|
||||
for _, shell := range shells {
|
||||
if shell.Status != protocol.ChatShellRunning {
|
||||
continue
|
||||
}
|
||||
if _, err := c.call(ctx, "delete", "/api/shell/"+url.PathEscape(shell.ID), nil); err != nil {
|
||||
return stopped, fmt.Errorf("stop shell %s: %w", shell.ID, err)
|
||||
}
|
||||
stopped++
|
||||
}
|
||||
return stopped, nil
|
||||
}
|
||||
|
||||
// ReleaseSession stops active and background work for an explicit operator
|
||||
// close or /new without deleting the persisted native conversation. OpenCode
|
||||
// interrupt only stops the foreground drain; running shells are separate native
|
||||
|
||||
@@ -410,6 +410,42 @@ func TestChatReleaseSessionStopsRunningShells(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatStopTasksDeletesOnlyRunningShells(t *testing.T) {
|
||||
var requests []string
|
||||
var mu sync.Mutex
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
mu.Lock()
|
||||
requests = append(requests, r.Method+" "+r.URL.Path)
|
||||
mu.Unlock()
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
switch r.URL.Path {
|
||||
case "/api/shell":
|
||||
_, _ = w.Write([]byte(`{"data":[{"id":"sh_running","status":"running","metadata":{"sessionID":"ses_1"}},{"id":"sh_done","status":"exited","metadata":{"sessionID":"ses_1"}}]}`))
|
||||
case "/api/shell/sh_running":
|
||||
_, _ = w.Write([]byte(`{}`))
|
||||
default:
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
chat := NewChat(&Connection{Backend: "v2", URL: srv.URL})
|
||||
stopped, err := chat.StopTasks(context.Background(), "ses_1")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if stopped != 1 {
|
||||
t.Fatalf("stopped = %d, want 1", stopped)
|
||||
}
|
||||
mu.Lock()
|
||||
got := append([]string(nil), requests...)
|
||||
mu.Unlock()
|
||||
want := []string{"GET /api/shell", "DELETE /api/shell/sh_running"}
|
||||
if !slices.Equal(got, want) {
|
||||
t.Fatalf("stop requests = %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// TestDecodeChatMessages loads a seeded user + assistant session in both
|
||||
// response envelopes the two surfaces return and asserts the turns come back
|
||||
// with role, id, part order, part ids and the completed tool's input, content
|
||||
|
||||
Reference in New Issue
Block a user