The transport could carry an actor and nothing filled it. It does now, so a dashboard approval is recorded as the person whose credential verified, and an agent's token is recorded as the agent's action -- for an agent, agent_approved; for a person, human_approved; and for a caller whose identity archie could not verify, task_approved, which claims nothing about who did it. The actor travels the path the action takes: webui reads it from the request context the middleware resolved, carries it in the gateway request, the daemon passes it through the in-process adapter, and the NATS request body carries it to the service that records it. Scope stays separate from actor at every hop, because scope is which task a caller may touch and the actor is who touched it. A chat channel's bound identity is an actor too: a bot acting on its own channel's task is recorded as an agent's action rather than a person's, via ActorFromScope, whose empty kind is what keeps it out of human_approved. The agent-facing task_action tool passes that identity and never input["identity"]. The UI process's credential check is now filled by endpoint references rather than by configuration: -oidc-issuer and -oidc-audience on archie-ui, two URLs the process already understands the shape of, with the verifier built in Run because cmd/ parses flags and nothing else. The UI package still never reads config, and a configured provider that cannot be reached fails boot rather than falling back to the gate that names nobody. Evidence for this change, stated as measured: go build ./... and go vet ./... are clean, and every package this touches passes -- webui, domain/taskactions, infrastructure/taskactions, infrastructure/gatewayrpc, gateway, app/archieui, app/archied, domain/messaging, channels/telegram, cmd/archie-ui. task check could not be run to completion: it fails in cmd/archie-messaging on an untracked internal/buildinfo package another lane has in flight, which is not this change and was left untouched. The end-to-end flip is covered in internal/webui/api_tasks_actor_test.go: a person's credential produces a record naming that person with human_approved, an agent's produces agent_approved, an unverified request produces task_approved with no actor, and a request carrying forged actor fields cannot name one.
276 lines
8.1 KiB
Go
276 lines
8.1 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/taskactions"
|
|
"github.com/samcharles93/archie-core/internal/taskstate"
|
|
"github.com/samcharles93/archie-core/internal/tools"
|
|
)
|
|
|
|
type fakeActor struct {
|
|
result TaskActionResult
|
|
err error
|
|
|
|
gotIdentity string
|
|
gotScoped bool
|
|
gotTaskID int64
|
|
gotAction taskstate.Action
|
|
}
|
|
|
|
func (f *fakeActor) ApplyChatTaskAction(_ context.Context, identity *string, actor taskactions.Actor, taskID int64, action taskstate.Action) (TaskActionResult, error) {
|
|
if identity != nil {
|
|
f.gotIdentity = *identity
|
|
}
|
|
f.gotScoped = identity != nil
|
|
f.gotTaskID = taskID
|
|
f.gotAction = action
|
|
return f.result, f.err
|
|
}
|
|
|
|
func TestTaskActionToolProperties(t *testing.T) {
|
|
actor := &fakeActor{}
|
|
entry := toolNamed(t, TaskTools(nil, nil, nil, actor, "archie"), "task_action")
|
|
|
|
if err := entry.Validate(); err != nil {
|
|
t.Fatalf("entry.Validate() = %v", err)
|
|
}
|
|
if entry.Name != "task_action" {
|
|
t.Errorf("Name = %q, want task_action", entry.Name)
|
|
}
|
|
if entry.Toolset != "tasks" {
|
|
t.Errorf("Toolset = %q, want tasks", entry.Toolset)
|
|
}
|
|
wantClass := tools.ClassMutating | tools.RequiresApproval
|
|
if entry.Classification != wantClass {
|
|
t.Errorf("Classification = %v, want %v", entry.Classification, wantClass)
|
|
}
|
|
if entry.BuildApprovalDescription == nil {
|
|
t.Fatal("BuildApprovalDescription is nil, want a description builder for confirmation")
|
|
}
|
|
}
|
|
|
|
func TestTaskActionOmittedWhenActorIsNil(t *testing.T) {
|
|
entries := TaskTools(&fakeLister{}, &fakeCreator{}, &fakeLogReader{}, nil, "archie")
|
|
for _, e := range entries {
|
|
if e.Name == "task_action" {
|
|
t.Fatal("task_action is present when actor is nil -- a tool that cannot run must not be advertised")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestTaskActionAppearsWhenActorIsNonNil(t *testing.T) {
|
|
entries := TaskTools(nil, nil, nil, &fakeActor{}, "archie")
|
|
toolNamed(t, entries, "task_action") // fails if absent
|
|
}
|
|
|
|
func TestTaskActionUsesBoundIdentity(t *testing.T) {
|
|
actor := &fakeActor{result: TaskActionResult{TaskID: 42, Action: "abandon", Message: "abandoned"}}
|
|
entry := toolNamed(t, TaskTools(nil, nil, nil, actor, "archie"), "task_action")
|
|
|
|
_, err := entry.Handler(context.Background(), map[string]any{
|
|
"task_id": float64(42),
|
|
"action": "abandon",
|
|
"identity": "other-identity",
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if actor.gotIdentity != "archie" {
|
|
t.Errorf("identity = %q, want bound %q", actor.gotIdentity, "archie")
|
|
}
|
|
// Scoped, always: an unscoped action is the dashboard operator's
|
|
// authorization, and a chat tool must never reach for it.
|
|
if !actor.gotScoped {
|
|
t.Error("chat tool action arrived unscoped, which authorizes it across every identity")
|
|
}
|
|
}
|
|
|
|
func TestTaskActionInputValidation(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input map[string]any
|
|
actor *fakeActor
|
|
wantErr bool
|
|
wantTaskID int64
|
|
wantAction taskstate.Action
|
|
}{
|
|
{
|
|
name: "valid float64 task_id",
|
|
input: map[string]any{"task_id": float64(10), "action": "abandon"},
|
|
actor: &fakeActor{result: TaskActionResult{TaskID: 10, Action: "abandon"}},
|
|
wantTaskID: 10,
|
|
wantAction: taskstate.ActionAbandon,
|
|
},
|
|
{
|
|
name: "valid int task_id",
|
|
input: map[string]any{"task_id": 11, "action": "archive"},
|
|
actor: &fakeActor{result: TaskActionResult{TaskID: 11, Action: "archive"}},
|
|
wantTaskID: 11,
|
|
wantAction: taskstate.ActionArchive,
|
|
},
|
|
{
|
|
name: "valid int64 task_id",
|
|
input: map[string]any{"task_id": int64(12), "action": "retry"},
|
|
actor: &fakeActor{result: TaskActionResult{TaskID: 12, Action: "retry"}},
|
|
wantTaskID: 12,
|
|
wantAction: taskstate.ActionRetry,
|
|
},
|
|
{
|
|
name: "normalizes action casing and whitespace",
|
|
input: map[string]any{"task_id": float64(13), "action": " ABANDON "},
|
|
actor: &fakeActor{result: TaskActionResult{TaskID: 13, Action: "abandon"}},
|
|
wantTaskID: 13,
|
|
wantAction: taskstate.ActionAbandon,
|
|
},
|
|
{
|
|
name: "missing task_id",
|
|
input: map[string]any{"action": "abandon"},
|
|
actor: &fakeActor{},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "invalid task_id type",
|
|
input: map[string]any{"task_id": "not-a-number", "action": "abandon"},
|
|
actor: &fakeActor{},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "missing action",
|
|
input: map[string]any{"task_id": float64(10)},
|
|
actor: &fakeActor{},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "blank action",
|
|
input: map[string]any{"task_id": float64(10), "action": " "},
|
|
actor: &fakeActor{},
|
|
wantErr: true,
|
|
},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
entry := toolNamed(t, TaskTools(nil, nil, nil, tc.actor, "archie"), "task_action")
|
|
out, err := entry.Handler(context.Background(), tc.input)
|
|
if tc.wantErr {
|
|
if err == nil {
|
|
t.Fatalf("Handler(%v) = nil error, want error", tc.input)
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("Handler(%v) = %v, want nil error", tc.input, err)
|
|
}
|
|
if tc.actor.gotTaskID != tc.wantTaskID {
|
|
t.Errorf("task_id = %d, want %d", tc.actor.gotTaskID, tc.wantTaskID)
|
|
}
|
|
if tc.actor.gotAction != tc.wantAction {
|
|
t.Errorf("action = %q, want %q", tc.actor.gotAction, tc.wantAction)
|
|
}
|
|
res, ok := out.(TaskActionResult)
|
|
if !ok {
|
|
t.Fatalf("Handler output = %T, want TaskActionResult", out)
|
|
}
|
|
if res.TaskID != tc.wantTaskID {
|
|
t.Errorf("res.TaskID = %d, want %d", res.TaskID, tc.wantTaskID)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestTaskActionAllActions(t *testing.T) {
|
|
actions := []struct {
|
|
action taskstate.Action
|
|
input string
|
|
}{
|
|
{action: taskstate.ActionAbandon, input: "abandon"},
|
|
{action: taskstate.ActionArchive, input: "archive"},
|
|
{action: taskstate.ActionRetry, input: "retry"},
|
|
{action: taskstate.ActionStop, input: "stop"},
|
|
{action: taskstate.ActionCancel, input: "cancel"},
|
|
{action: taskstate.ActionApprove, input: "approve"},
|
|
{action: taskstate.ActionReject, input: "reject"},
|
|
}
|
|
|
|
for _, tc := range actions {
|
|
t.Run(string(tc.action), func(t *testing.T) {
|
|
actor := &fakeActor{result: TaskActionResult{
|
|
TaskID: 99,
|
|
Action: string(tc.action),
|
|
Message: fmt.Sprintf("Applied %s to task 99.", tc.action),
|
|
}}
|
|
entry := toolNamed(t, TaskTools(nil, nil, nil, actor, "archie"), "task_action")
|
|
out, err := entry.Handler(context.Background(), map[string]any{
|
|
"task_id": float64(99),
|
|
"action": tc.input,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("Handler(%s) error = %v", tc.input, err)
|
|
}
|
|
res, ok := out.(TaskActionResult)
|
|
if !ok {
|
|
t.Fatalf("Handler output = %T, want TaskActionResult", out)
|
|
}
|
|
if res.Action != string(tc.action) {
|
|
t.Errorf("res.Action = %q, want %q", res.Action, tc.action)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestTaskActionPropagatesActorError(t *testing.T) {
|
|
actor := &fakeActor{err: errors.New("action not available while task is running")}
|
|
entry := toolNamed(t, TaskTools(nil, nil, nil, actor, "archie"), "task_action")
|
|
|
|
_, err := entry.Handler(context.Background(), map[string]any{
|
|
"task_id": float64(42),
|
|
"action": "abandon",
|
|
})
|
|
if err == nil {
|
|
t.Fatal("expected error, got nil")
|
|
}
|
|
if !strings.Contains(err.Error(), "action not available while task is running") {
|
|
t.Errorf("error = %v, want substring %q", err, "action not available while task is running")
|
|
}
|
|
}
|
|
|
|
func TestTaskActionBuildApprovalDescription(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input map[string]any
|
|
want string
|
|
}{
|
|
{
|
|
name: "abandon parked task",
|
|
input: map[string]any{"task_id": float64(42), "action": "abandon"},
|
|
want: "Apply action \"abandon\" to task 42.",
|
|
},
|
|
{
|
|
name: "archive dead task",
|
|
input: map[string]any{"task_id": int64(101), "action": "archive"},
|
|
want: "Apply action \"archive\" to task 101.",
|
|
},
|
|
{
|
|
name: "missing fields fallback cleanly",
|
|
input: map[string]any{},
|
|
want: "Apply action \"\" to task 0.",
|
|
},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
actor := &fakeActor{}
|
|
entry := toolNamed(t, TaskTools(nil, nil, nil, actor, "archie"), "task_action")
|
|
got := entry.BuildApprovalDescription(tc.input)
|
|
if got != tc.want {
|
|
t.Errorf("BuildApprovalDescription(%v) = %q, want %q", tc.input, got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|