Skip to content

Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.

fix: cancel Live-started tasks the way Live does

LeoXinga7cc4553a5a0
diff --git a/apps/teamhub/internal/store/queries/task_delegation.sql b/apps/teamhub/internal/store/queries/task_delegation.sql
index 8000a7f3..2b1ef02d 100644
--- a/apps/teamhub/internal/store/queries/task_delegation.sql
+++ b/apps/teamhub/internal/store/queries/task_delegation.sql
@@ -118,8 +118,10 @@ LIMIT 50;
-- name: ListRoomRunningTasks :many
-- The Tasks running for this Room, oldest first: the background Tasks its Meta
--- delegated and the tasks Live started with it as their home Room.
-SELECT item.id, item.title, item.description, item.created_at
+-- delegated and the tasks Live started with it as their home Room; live
+-- marks the latter, which have no Meta.
+SELECT item.id, item.title, item.description, item.created_at,
+ EXISTS (SELECT 1 FROM voice_live_tasks voice WHERE voice.work_item_id=item.id)::boolean AS live
FROM work_items item
WHERE item.id IN (
SELECT origin.work_item_id FROM work_item_room_links origin
diff --git a/apps/teamhub/internal/store/task_delegation.sql.go b/apps/teamhub/internal/store/task_delegation.sql.go
index 655eea9b..70d532da 100644
--- a/apps/teamhub/internal/store/task_delegation.sql.go
+++ b/apps/teamhub/internal/store/task_delegation.sql.go
@@ -324,7 +324,8 @@ func (q *Queries) ListRoomBackgroundTasks(ctx context.Context, arg ListRoomBackg
}
const listRoomRunningTasks = `-- name: ListRoomRunningTasks :many
-SELECT item.id, item.title, item.description, item.created_at
+SELECT item.id, item.title, item.description, item.created_at,
+ EXISTS (SELECT 1 FROM voice_live_tasks voice WHERE voice.work_item_id=item.id)::boolean AS live
FROM work_items item
WHERE item.id IN (
SELECT origin.work_item_id FROM work_item_room_links origin
@@ -348,10 +349,12 @@ type ListRoomRunningTasksRow struct {
Title string `json:"title"`
Description string `json:"description"`
CreatedAt pgtype.Timestamptz `json:"created_at"`
+ Live bool `json:"live"`
}
// The Tasks running for this Room, oldest first: the background Tasks its Meta
-// delegated and the tasks Live started with it as their home Room.
+// delegated and the tasks Live started with it as their home Room; live
+// marks the latter, which have no Meta.
func (q *Queries) ListRoomRunningTasks(ctx context.Context, arg ListRoomRunningTasksParams) ([]ListRoomRunningTasksRow, error) {
rows, err := q.db.Query(ctx, listRoomRunningTasks, arg.RoomID, arg.TenantID)
if err != nil {
@@ -366,6 +369,7 @@ func (q *Queries) ListRoomRunningTasks(ctx context.Context, arg ListRoomRunningT
&i.Title,
&i.Description,
&i.CreatedAt,
+ &i.Live,
); err != nil {
return nil, err
}
diff --git a/apps/teamhub/internal/taskdelegation/manage.go b/apps/teamhub/internal/taskdelegation/manage.go
index d4559e79..f58d70f6 100644
--- a/apps/teamhub/internal/taskdelegation/manage.go
+++ b/apps/teamhub/internal/taskdelegation/manage.go
@@ -196,18 +196,29 @@ func (s *Service) Stop(
}
return s.change(ctx, token, stopTaskTool, invocationID, taskID, requestHash,
func(tx pgx.Tx, queries *store.Queries, authorization agentauth.Authorization) (ChangeResult, error) {
- revision, err := taskexecution.StopTask(ctx, queries, taskID, authorization.AgentActorID)
+ revision, err := StopTask(ctx, tx, queries, authorization.TenantID, taskID, authorization.AgentActorID)
if err != nil {
return ChangeResult{}, err
}
- // Stopping expires the cards the AlterEgo was waiting on.
- if err := jobs.InsertTx(ctx, tx, typedcard.StoppedTaskApprovalsArgs{TenantID: authorization.TenantID, TaskID: taskID}, nil); err != nil {
- return ChangeResult{}, err
- }
return ChangeResult{TaskID: taskID, Status: "canceled", ExecutionRevision: revision}, nil
})
}
+// StopTask stops a delegated Task's current turn and expires the approval
+// cards its AlterEgo was waiting on; every stop, by a Meta, Live or a Human,
+// goes through here so the two never come apart. Stopping a canceled Task
+// changes nothing and returns revision 0.
+func StopTask(ctx context.Context, tx pgx.Tx, queries *store.Queries, tenantID, taskID, actorID string) (int64, error) {
+ revision, err := taskexecution.StopTask(ctx, queries, taskID, actorID)
+ if err != nil {
+ return 0, err
+ }
+ if err := jobs.InsertTx(ctx, tx, typedcard.StoppedTaskApprovalsArgs{TenantID: tenantID, TaskID: taskID}, nil); err != nil {
+ return 0, err
+ }
+ return revision, nil
+}
+
// SendSystemInput wakes a delegated Task's AlterEgo with a system notice,
// such as the result of one of its approval cards, through the same input
// path as a Meta message. The Agent must be the Task's participant.
diff --git a/apps/teamhub/internal/taskdelegation/room_cancel.go b/apps/teamhub/internal/taskdelegation/room_cancel.go
index be912913..bb43c9fa 100644
--- a/apps/teamhub/internal/taskdelegation/room_cancel.go
+++ b/apps/teamhub/internal/taskdelegation/room_cancel.go
@@ -7,10 +7,8 @@ import (
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
- "github.com/simplexai-labs/linger/apps/teamhub/internal/jobs"
"github.com/simplexai-labs/linger/apps/teamhub/internal/store"
"github.com/simplexai-labs/linger/apps/teamhub/internal/taskexecution"
- "github.com/simplexai-labs/linger/apps/teamhub/internal/typedcard"
)
// Statuses CancelFromRoom reports.
@@ -21,8 +19,10 @@ const (
// CancelFromRoom lets a Human member of a Room cancel one of the Tasks
// running for it. The Task is stopped as its Meta's task stop does, so it
-// stays resumable, and the Meta is told who canceled it. A Task that is no
-// longer running is left alone and reported not_running.
+// stays resumable. A Task its Meta delegated tells the Meta who canceled it;
+// a task a Live call started is canceled as Live's own cancel does, since
+// its callback stream carries its reports and it has no Meta. A Task that is
+// not running for this Room is left alone and reported not_running.
func (s *Service) CancelFromRoom(ctx context.Context, accountID, roomID, taskID string) (string, error) {
if s == nil || s.pool == nil {
return "", ErrUnavailable
@@ -49,15 +49,22 @@ func (s *Service) CancelFromRoom(ctx context.Context, accountID, roomID, taskID
if err != nil {
return "", err
}
- if !slices.ContainsFunc(running, func(row store.ListRoomRunningTasksRow) bool { return row.ID == taskID }) {
+ index := slices.IndexFunc(running, func(row store.ListRoomRunningTasksRow) bool { return row.ID == taskID })
+ if index < 0 {
return RoomTaskNotRunning, nil
}
- if _, err := taskexecution.CancelTaskByHuman(ctx, queries, taskID, human); err != nil {
+ revision, err := StopTask(ctx, tx, queries, room.TenantID, taskID, human.ID)
+ if err != nil {
return "", mapTaskExecutionError(err)
}
- // Canceling expires the cards the AlterEgo was waiting on.
- if err := jobs.InsertTx(ctx, tx, typedcard.StoppedTaskApprovalsArgs{TenantID: room.TenantID, TaskID: taskID}, nil); err != nil {
- return "", err
+ if running[index].Live {
+ if _, err := queries.CancelVoiceLiveTask(ctx, taskID); err != nil {
+ return "", err
+ }
+ } else if revision > 0 {
+ if err := taskexecution.NotifyMetaOfHumanCancel(ctx, queries, taskID, human); err != nil {
+ return "", mapTaskExecutionError(err)
+ }
}
if err := tx.Commit(ctx); err != nil {
return "", err
diff --git a/apps/teamhub/internal/taskexecution/delegated.go b/apps/teamhub/internal/taskexecution/delegated.go
index e21b0952..86fa15b6 100644
--- a/apps/teamhub/internal/taskexecution/delegated.go
+++ b/apps/teamhub/internal/taskexecution/delegated.go
@@ -268,26 +268,21 @@ func StopTask(ctx context.Context, queries *store.Queries, taskID, actorID strin
return setDelegatedTaskStatus(ctx, queries, snapshot, status, "canceled", "stopped", "stopped", actorID, payload)
}
-// CancelTaskByHuman stops a delegated Task's current turn exactly as
-// StopTask does, so the Task stays resumable, then tells its coordinating
-// Meta who canceled it. Canceling a canceled Task changes nothing and
-// returns revision 0.
-func CancelTaskByHuman(ctx context.Context, queries *store.Queries, taskID string, human store.Actor) (int64, error) {
+// NotifyMetaOfHumanCancel tells a stopped Task's coordinating Meta which
+// Human canceled it, so the Meta can tell the user and later resume the Task
+// or do something else. Call it in the transaction that stopped the Task.
+func NotifyMetaOfHumanCancel(ctx context.Context, queries *store.Queries, taskID string, human store.Actor) error {
if human.ID == "" || human.Kind != "human" {
- return 0, ErrInvalid
- }
- revision, err := StopTask(ctx, queries, taskID, human.ID)
- if err != nil || revision == 0 {
- return revision, err
+ return ErrInvalid
}
snapshot, err := queries.LockTaskExecutionSnapshot(ctx, taskID)
if err != nil {
- return 0, err
+ return err
}
notice := human.DisplayName + " canceled Task " + snapshot.Title + " (" + taskID + "). " +
"Its current turn was stopped; its session and ./playground/" + taskID + "/ are kept, so it can be resumed. " +
"Tell the user it was canceled, then follow what they want next."
- return revision, appendTaskCallback(ctx, queries, snapshot, snapshot.TaskExecutionGeneration.Int64, human.ID, notice)
+ return appendTaskCallback(ctx, queries, snapshot, snapshot.TaskExecutionGeneration.Int64, human.ID, notice)
}
// setDelegatedTaskStatus records a Meta-driven change outside the Worker
diff --git a/apps/teamhub/internal/voice/voice_live_tasks.go b/apps/teamhub/internal/voice/voice_live_tasks.go
index 068c893d..132e77c7 100644
--- a/apps/teamhub/internal/voice/voice_live_tasks.go
+++ b/apps/teamhub/internal/voice/voice_live_tasks.go
@@ -14,7 +14,6 @@ import (
"github.com/jackc/pgx/v5/pgtype"
"github.com/simplexai-labs/linger/apps/teamhub/internal/agentruntimestate"
idgen "github.com/simplexai-labs/linger/apps/teamhub/internal/ids"
- "github.com/simplexai-labs/linger/apps/teamhub/internal/jobs"
"github.com/simplexai-labs/linger/apps/teamhub/internal/reminder"
"github.com/simplexai-labs/linger/apps/teamhub/internal/store"
"github.com/simplexai-labs/linger/apps/teamhub/internal/taskdelegation"
@@ -346,10 +345,7 @@ func (s *Service) stopVoiceLiveTask(ctx context.Context, tenantID, workItemID, a
}
defer tx.Rollback(ctx)
queries := store.New(tx)
- if _, err := taskexecution.StopTask(ctx, queries, workItemID, actorID); err != nil && !errors.Is(err, taskexecution.ErrNotFound) {
- return err
- }
- if err := jobs.InsertTx(ctx, tx, typedcard.StoppedTaskApprovalsArgs{TenantID: tenantID, TaskID: workItemID}, nil); err != nil {
+ if _, err := taskdelegation.StopTask(ctx, tx, queries, tenantID, workItemID, actorID); err != nil && !errors.Is(err, taskexecution.ErrNotFound) {
return err
}
if _, err := queries.CancelVoiceLiveTask(ctx, workItemID); err != nil {
diff --git a/apps/teamhub/tests/integration/task_delegation_lifecycle_test.go b/apps/teamhub/tests/integration/task_delegation_lifecycle_test.go
index 0a95b963..5d93383c 100644
--- a/apps/teamhub/tests/integration/task_delegation_lifecycle_test.go
+++ b/apps/teamhub/tests/integration/task_delegation_lifecycle_test.go
@@ -483,9 +483,35 @@ func TestHumanCancelsBackgroundTask(t *testing.T) {
t.Fatalf("resume send=%v", started)
}
- // Only a member of the Room reaches it.
+ // Only a member of the Room reaches it, and only through its own Room.
code, _, body = env.request(alice, http.MethodPost, "/v0/rooms/room-missing/background-tasks/"+created.TaskID+"/cancel", nil, "")
if code != http.StatusNotFound {
t.Fatalf("cancel in unknown Room=%d %s", code, body)
}
+ otherRoomID := createPrivateRoom(t, env, alice, "Other room", "human-cancel-other-room")
+ code, _, body = env.request(alice, http.MethodPost, "/v0/rooms/"+otherRoomID+"/background-tasks/"+created.TaskID+"/cancel", nil, "")
+ if code != http.StatusOK || !strings.Contains(string(body), `"status":"not_running"`) {
+ t.Fatalf("cancel through another Room=%d %s", code, body)
+ }
+ assertDelegatedTaskState(t, env, created.TaskID, "running", "")
+
+ // A task a Live call started is canceled as Live cancels it: no Meta
+ // notice in its report stream, and Live sees it canceled.
+ live := createDelegatedTask(t, env, issued, "human-cancel-live", "Book the table", "Book the table.", roomID)
+ if _, err := env.pool.Exec(t.Context(), `INSERT INTO voice_live_tasks(work_item_id,room_id,owner_actor_id,home_room_id,ordinal,goal,surface)
+ SELECT $1,$2,author_actor_id,$2,1,'Book the table','web' FROM task_meta_callback_messages WHERE task_id=$3`,
+ live.TaskID, roomID, created.TaskID); err != nil {
+ t.Fatal(err)
+ }
+ code, _, body = env.request(alice, http.MethodPost, "/v0/rooms/"+roomID+"/background-tasks/"+live.TaskID+"/cancel", nil, "")
+ if code != http.StatusOK || !strings.Contains(string(body), `"status":"canceled"`) {
+ t.Fatalf("cancel Live task=%d %s", code, body)
+ }
+ assertDelegatedTaskState(t, env, live.TaskID, "canceled", "stopped")
+ if got := countRows(t, env, `SELECT count(*) FROM voice_live_tasks WHERE work_item_id=$1 AND canceled_at IS NOT NULL`, live.TaskID); got != 1 {
+ t.Fatal("Room cancel left the Live task uncanceled")
+ }
+ if got := countRows(t, env, `SELECT count(*) FROM task_meta_callback_messages WHERE task_id=$1`, live.TaskID); got != 0 {
+ t.Fatalf("Room cancel wrote %d callback messages for a Live task", got)
+ }
}
diff --git a/contracts/generated/typescript/src/generated/teamhub-public/types.ts b/contracts/generated/typescript/src/generated/teamhub-public/types.ts
index aea4d77d..96570ffa 100644
--- a/contracts/generated/typescript/src/generated/teamhub-public/types.ts
+++ b/contracts/generated/typescript/src/generated/teamhub-public/types.ts
@@ -750,7 +750,7 @@ export interface paths {
put?: never;
/**
* Cancel one background Task's current turn
- * @description Stops the Task as its Meta's task stop does: the current turn ends and its approval cards expire, but its session and playground stay, so the Meta can resume it later. The Room's Meta is told who canceled it. Canceling a Task that is no longer running changes nothing.
+ * @description Stops the Task as its Meta's task stop does: the current turn ends and its approval cards expire, but its session and playground stay, so the Meta can resume it later. The Room's Meta is told who canceled it; a task a Live call started is canceled as Live's own cancel does instead. Canceling a Task that is not running for this Room changes nothing.
*/
post: operations["cancelRoomBackgroundTask"];
delete?: never;
diff --git a/contracts/openapi/paths/room-run-control.yaml b/contracts/openapi/paths/room-run-control.yaml
index f6b3402d..6f7e07d2 100644
--- a/contracts/openapi/paths/room-run-control.yaml
+++ b/contracts/openapi/paths/room-run-control.yaml
@@ -47,8 +47,9 @@ cancelBackgroundTask:
description: >-
Stops the Task as its Meta's task stop does: the current turn ends and
its approval cards expire, but its session and playground stay, so the
- Meta can resume it later. The Room's Meta is told who canceled it.
- Canceling a Task that is no longer running changes nothing.
+ Meta can resume it later. The Room's Meta is told who canceled it; a
+ task a Live call started is canceled as Live's own cancel does instead.
+ Canceling a Task that is not running for this Room changes nothing.
parameters:
- name: roomId
in: path
diff --git a/docs/task-delegation.md b/docs/task-delegation.md
index 773f3e9e..562ead99 100644
--- a/docs/task-delegation.md
+++ b/docs/task-delegation.md
@@ -23,8 +23,10 @@ conversation goes on.
| You press Cancel on it under _tasks running in the background_ | `canceled`, with its files and session kept | The Meta, told that you canceled it |
<sub>A canceled Task is paused, not closed: the Meta can resume it with your
-next request or do something else. An AlterEgo never asks you anything; a
-question goes into its report, and the Meta decides whether to ask.</sub>
+next request or do something else. A task a voice call started has no Meta, so
+canceling it from the list cancels it the way the call does. An AlterEgo never
+asks you anything; a question goes into its report, and the Meta decides
+whether to ask.</sub>
## On the computer