diff --git a/internal/app/archiegateway/chat_tasks.go b/internal/app/archiegateway/chat_tasks.go index 86d230394..e4344ff1d 100644 --- a/internal/app/archiegateway/chat_tasks.go +++ b/internal/app/archiegateway/chat_tasks.go @@ -4,7 +4,6 @@ import ( "context" "fmt" - "github.com/samcharles93/archie-core/internal/domain/identity" "github.com/samcharles93/archie-core/internal/domain/storecontract" "github.com/samcharles93/archie-core/internal/domain/taskactions" "github.com/samcharles93/archie-core/internal/domain/workflow" @@ -39,7 +38,7 @@ func (a chatTaskControllerAdapter) ApproveChatTask(ctx context.Context, taskID i // /approve carries no selection syntax, so the review gate answer posts // every offered finding. var scope *string - if !seesEveryTask(string(actor.Identity)) { + if !taskactions.ActsForOperator(string(actor.Identity)) { id := string(actor.Identity) scope = &id } @@ -73,12 +72,6 @@ func (a chatTaskControllerAdapter) CancelChatTask(ctx context.Context, taskID in return err } -// seesEveryTask reports whether a chat profile acts for the operator, as the -// dashboard does. Any other identity is confined to its own tasks. -func seesEveryTask(chatIdentity string) bool { - return chatIdentity == string(identity.OperatorID()) -} - // chatTaskListerAdapter gives the gateway a read view of an identity's tasks. // gateway deliberately does not import the task store, so the projection // happens here, as it does for the writer and controller adapters below. @@ -87,7 +80,7 @@ type chatTaskListerAdapter struct { } func (a chatTaskListerAdapter) ListChatTasks(ctx context.Context, identity string, limit int) ([]gateway.ChatTaskSummary, error) { - all := seesEveryTask(identity) + all := taskactions.ActsForOperator(identity) read := limit if !all { // Tasks applies its limit across the whole table, so over-read before @@ -139,7 +132,7 @@ func (a chatTaskLogReaderAdapter) ReadChatTaskLogs( if task == nil { return gateway.ChatTaskLogResult{}, fmt.Errorf("task %d not found", taskID) } - if !seesEveryTask(identity) && task.Identity != identity { + if !taskactions.ActsForOperator(identity) && task.Identity != identity { return gateway.ChatTaskLogResult{}, fmt.Errorf("task %d belongs to another identity", taskID) } if attempt <= 0 { diff --git a/internal/domain/taskactions/service.go b/internal/domain/taskactions/service.go index c4c13092c..8dff837fc 100644 --- a/internal/domain/taskactions/service.go +++ b/internal/domain/taskactions/service.go @@ -216,9 +216,17 @@ type Service struct { Warn func(string, ...any) } +// ActsForOperator reports whether a chat profile acts for the operator, as the +// dashboard does: it may see and act on every task. Any other identity is +// confined to its own tasks. Empty is the daemon's root sentinel and belongs +// to no profile, so only the operator reaches a task that carries it. +func ActsForOperator(id string) bool { + return identity.IdentityID(id) == identity.OperatorID() +} + // Apply runs action on task id. A nil scope is a dashboard caller acting -// across identities; otherwise scope limits which tasks may be acted on. res -// is the action's payload. +// across identities, as is a chat profile acting for the operator; any other +// scope limits which tasks may be acted on. res is the action's payload. func (s Service) Apply(ctx context.Context, scope *string, actor Actor, id int64, action taskstate.Action, res ActionPayload) error { task, err := s.Store.TaskByID(ctx, id) if err != nil { @@ -227,7 +235,7 @@ func (s Service) Apply(ctx context.Context, scope *string, actor Actor, id int64 if task == nil { return fmt.Errorf("task %d: %w", id, ErrNotFound) } - if scope != nil && task.Identity != *scope { + if scope != nil && !ActsForOperator(*scope) && task.Identity != *scope { return fmt.Errorf("task %d belongs to %q, not %q", id, task.Identity, *scope) } if err := taskstate.CheckAction(task.Status, action); err != nil { diff --git a/internal/domain/taskactions/service_test.go b/internal/domain/taskactions/service_test.go index fd635b064..dace8262d 100644 --- a/internal/domain/taskactions/service_test.go +++ b/internal/domain/taskactions/service_test.go @@ -4,8 +4,10 @@ import ( "context" "errors" "slices" + "strings" "testing" + "github.com/samcharles93/archie-core/internal/domain/identity" "github.com/samcharles93/archie-core/internal/events" "github.com/samcharles93/archie-core/internal/taskstate" ) @@ -75,3 +77,43 @@ func TestCancellingActionsStopCallees(t *testing.T) { }) } } + +// A chat profile acting for the operator acts across identities, as the +// dashboard does. Empty identity is the daemon's root sentinel and belongs to +// the operator, so it must not refuse the operator the task it can already see. +func TestActionScope(t *testing.T) { + operator := string(identity.OperatorID()) + other := "e0a0a0a0-0000-4000-8000-000000000001" + third := "e0a0a0a0-0000-4000-8000-000000000002" + tests := []struct { + name string + scope *string + taskIdentity string + allowed bool + }{ + {name: "dashboard acts across identities", taskIdentity: other, allowed: true}, + {name: "operator acts on a task with no identity", scope: &operator, taskIdentity: "", allowed: true}, + {name: "operator acts on another identity's task", scope: &operator, taskIdentity: other, allowed: true}, + {name: "another profile acts on its own task", scope: &other, taskIdentity: other, allowed: true}, + {name: "another profile cannot act on a task with no identity", scope: &other, taskIdentity: "", allowed: false}, + {name: "another profile cannot act on a third identity's task", scope: &other, taskIdentity: third, allowed: false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := Service{ + Store: &cancelStore{task: Task{ID: 1, Status: taskstate.Running, Identity: tt.taskIdentity}}, + CancelTask: func(int64) bool { return true }, + } + err := s.Apply(t.Context(), tt.scope, Actor{}, 1, taskstate.ActionStop, ActionPayload{}) + if tt.allowed { + if err != nil { + t.Fatalf("allowed action refused: %v", err) + } + return + } + if err == nil || !strings.Contains(err.Error(), "belongs to") { + t.Fatalf("err = %v, want a belongs-to refusal", err) + } + }) + } +}