merge: task_action chat tool (crew/taskact)

This commit is contained in:
sam committed 2026-08-25 02:05:25 +10:00
commit 901864a9df
8 files changed
+779 -31

No files matched your search

+10
View File
@@ -485,6 +485,11 @@ func (b *boot) setupLLMAndChat(ctx context.Context) {
tasks: b.st.TaskByID,
taskLogs: b.taskLogs,
},
ChatTaskActor: chatTaskActorAdapter{
tasks: b.st.TaskByID,
handler: b.web.Handler(),
token: func() string { return b.web.Token },
},
DefaultChatIdentity: b.defaultChatIdentity, SessionStore: b.chatSessionStore,
Bus: b.bus, Log: log,
}
@@ -517,6 +522,11 @@ func (b *boot) setupGateways(ctx context.Context, cfgPath, overlayPath string) b
tasks: b.st.TaskByID,
taskLogs: b.taskLogs,
},
ChatTaskActor: chatTaskActorAdapter{
tasks: b.st.TaskByID,
handler: b.web.Handler(),
token: func() string { return b.web.Token },
},
DefaultChatIdentity: b.defaultChatIdentity, SessionStore: b.chatSessionStore, Updates: b.updateService,
Bus: b.bus,
RegisterRestart: func(request func() error) { b.restartTelegram = request }, Log: log,
+71
View File
@@ -5,6 +5,7 @@
package main
import (
"bytes"
"context"
"crypto/sha256"
"encoding/json"
@@ -13,6 +14,8 @@ import (
"log/slog"
"maps"
"net"
"net/http"
"net/http/httptest"
"os"
"os/signal"
"path/filepath"
@@ -43,6 +46,7 @@ import (
"github.com/samcharles93/archie-core/internal/storage"
"github.com/samcharles93/archie-core/internal/store"
"github.com/samcharles93/archie-core/internal/storerpc"
"github.com/samcharles93/archie-core/internal/taskstate"
"github.com/samcharles93/archie-core/internal/tools"
"github.com/samcharles93/archie-core/internal/tools/mcp"
toolprovider "github.com/samcharles93/archie-core/internal/tools/provider"
@@ -597,6 +601,73 @@ func (a chatTaskLogReaderAdapter) ReadChatTaskLogs(
}, nil
}
// chatTaskActorAdapter executes operator actions on a task via the same
// action path the dashboard uses, ensuring chat and webui cannot diverge.
// It enforces identity scoping so an identity cannot mutate another
// identity's tasks.
type chatTaskActorAdapter struct {
tasks func(context.Context, int64) (*store.Task, error)
handler http.Handler
token func() string
}
func (a chatTaskActorAdapter) ApplyChatTaskAction(
ctx context.Context, identity string, taskID int64, action taskstate.Action,
) (gateway.TaskActionResult, error) {
if a.tasks == nil || a.handler == nil {
return gateway.TaskActionResult{}, fmt.Errorf("task actions are not configured")
}
task, err := a.tasks(ctx, taskID)
if err != nil {
return gateway.TaskActionResult{}, err
}
if task == nil {
return gateway.TaskActionResult{}, fmt.Errorf("task %d not found", taskID)
}
// Match the filter chatTaskListerAdapter and chatTaskLogReaderAdapter apply:
// a model bound to one identity must not mutate another identity's tasks,
// and tasks with no identity (forge-sourced) are not actionable through a
// chat tool at all — they belong to the daemon, not a particular identity.
if task.Identity != identity {
return gateway.TaskActionResult{}, fmt.Errorf("task %d belongs to %q, not %q", taskID, task.Identity, identity)
}
if err := taskstate.CheckAction(task.Status, action); err != nil {
return gateway.TaskActionResult{}, err
}
body, err := json.Marshal(map[string]any{"action": string(action)})
if err != nil {
return gateway.TaskActionResult{}, fmt.Errorf("marshal action request: %w", err)
}
req := httptest.NewRequestWithContext(ctx, http.MethodPost,
fmt.Sprintf("/api/tasks/%d/action", taskID),
bytes.NewReader(body),
)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Archie-CSRF", "1")
if a.token != nil {
if tok := a.token(); tok != "" {
req.Header.Set("Authorization", "Bearer "+tok)
}
}
rec := httptest.NewRecorder()
a.handler.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
errMsg := strings.TrimSpace(rec.Body.String())
if errMsg == "" {
errMsg = fmt.Sprintf("HTTP %d", rec.Code)
}
return gateway.TaskActionResult{}, fmt.Errorf("action %s on task %d failed: %s", action, taskID, errMsg)
}
return gateway.TaskActionResult{
TaskID: taskID,
Action: string(action),
Message: fmt.Sprintf("Applied %s to task %d.", action, taskID),
}, nil
}
func chatTaskProfiles(cfg config.Config) ([]gateway.TaskProfile, string) {
if len(cfg.Identities) > 0 {
profiles := make([]gateway.TaskProfile, 0, len(cfg.Identities))
+340
View File
@@ -0,0 +1,340 @@
package main
import (
"context"
"path/filepath"
"strings"
"testing"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/store"
"github.com/samcharles93/archie-core/internal/taskstate"
"github.com/samcharles93/archie-core/internal/webui"
)
func TestChatTaskActorAdapterCrossIdentityRefused(t *testing.T) {
ctx := context.Background()
st, err := store.Open(ctx, filepath.Join(t.TempDir(), "tasks.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = st.Close() })
task, err := st.EnqueueChatTask(ctx, "acme", "widget", "parked job", "", "", "identity-owner")
if err != nil {
t.Fatal(err)
}
if err := st.Transition(ctx, task.ID, store.StatusQueued, store.StatusParked, "needs help"); err != nil {
t.Fatal(err)
}
srv := &webui.Server{Store: st, Cfg: config.NewHolder(config.Config{})}
adapter := chatTaskActorAdapter{
tasks: st.TaskByID,
handler: srv.Handler(),
token: func() string { return srv.Token },
}
tests := []struct {
name string
actorIdentity string
action taskstate.Action
}{
{
name: "different identity cannot abandon",
actorIdentity: "identity-attacker",
action: taskstate.ActionAbandon,
},
{
name: "different identity cannot retry",
actorIdentity: "identity-attacker",
action: taskstate.ActionRetry,
},
{
name: "different identity cannot archive",
actorIdentity: "identity-attacker",
action: taskstate.ActionArchive,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
_, err := adapter.ApplyChatTaskAction(ctx, tc.actorIdentity, task.ID, tc.action)
if err == nil {
t.Fatalf("ApplyChatTaskAction() allowed cross-identity action %s by %q on task owned by %q",
tc.action, tc.actorIdentity, task.Identity)
}
if !strings.Contains(err.Error(), "identity-owner") || !strings.Contains(err.Error(), tc.actorIdentity) {
t.Errorf("error = %q, want mention of both identities", err.Error())
}
// Confirm store state was NOT mutated.
current, err := st.TaskByID(ctx, task.ID)
if err != nil || current == nil {
t.Fatalf("TaskByID = (%+v, %v)", current, err)
}
if current.Status != store.StatusParked {
t.Errorf("task status mutated to %q despite cross-identity refusal", current.Status)
}
})
}
}
func TestChatTaskActorAdapterRefusesDisallowedStateAction(t *testing.T) {
ctx := context.Background()
st, err := store.Open(ctx, filepath.Join(t.TempDir(), "tasks.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = st.Close() })
srv := &webui.Server{Store: st, Cfg: config.NewHolder(config.Config{})}
adapter := chatTaskActorAdapter{
tasks: st.TaskByID,
handler: srv.Handler(),
token: func() string { return srv.Token },
}
tests := []struct {
name string
initialStatus string
attemptAction taskstate.Action
}{
{
name: "cannot abandon running task",
initialStatus: store.StatusRunning,
attemptAction: taskstate.ActionAbandon,
},
{
name: "cannot archive parked task",
initialStatus: store.StatusParked,
attemptAction: taskstate.ActionArchive,
},
{
name: "cannot retry queued task",
initialStatus: store.StatusQueued,
attemptAction: taskstate.ActionRetry,
},
{
name: "cannot approve running task",
initialStatus: store.StatusRunning,
attemptAction: taskstate.ActionApprove,
},
{
name: "cannot reject queued task",
initialStatus: store.StatusQueued,
attemptAction: taskstate.ActionReject,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
task, err := st.EnqueueChatTask(ctx, "acme", "widget", "test task", "", "", "archie")
if err != nil {
t.Fatal(err)
}
if tc.initialStatus != store.StatusQueued {
if err := st.Transition(ctx, task.ID, store.StatusQueued, tc.initialStatus, "setup"); err != nil {
t.Fatal(err)
}
}
_, err = adapter.ApplyChatTaskAction(ctx, "archie", task.ID, tc.attemptAction)
if err == nil {
t.Fatalf("expected error attempting %s on %s task, got nil", tc.attemptAction, tc.initialStatus)
}
// Assert error matches taskstate rules.
wantErr := taskstate.CheckAction(tc.initialStatus, tc.attemptAction)
if wantErr != nil && !strings.Contains(err.Error(), wantErr.Error()) {
t.Errorf("error = %q, want substring %q", err.Error(), wantErr.Error())
}
// Ensure task status did not change.
current, err := st.TaskByID(ctx, task.ID)
if err != nil || current == nil {
t.Fatalf("TaskByID = (%+v, %v)", current, err)
}
if current.Status != tc.initialStatus {
t.Errorf("task status changed to %q, want %q", current.Status, tc.initialStatus)
}
})
}
}
func TestChatTaskActorAdapterTaskNotFound(t *testing.T) {
ctx := context.Background()
st, err := store.Open(ctx, filepath.Join(t.TempDir(), "tasks.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = st.Close() })
srv := &webui.Server{Store: st, Cfg: config.NewHolder(config.Config{})}
adapter := chatTaskActorAdapter{
tasks: st.TaskByID,
handler: srv.Handler(),
token: func() string { return srv.Token },
}
_, err = adapter.ApplyChatTaskAction(ctx, "archie", 999999, taskstate.ActionAbandon)
if err == nil {
t.Fatal("expected error for non-existent task, got nil")
}
if !strings.Contains(err.Error(), "not found") {
t.Errorf("error = %q, want 'not found'", err.Error())
}
}
func TestChatTaskActorAdapterAppliesActionsToStore(t *testing.T) {
tests := []struct {
name string
initialStatus string
action taskstate.Action
wantStatus string
wantArchived bool
}{
{
name: "abandon parked task closes it won't do",
initialStatus: store.StatusParked,
action: taskstate.ActionAbandon,
wantStatus: store.StatusClosedWontDo,
},
{
name: "retry parked task requeues it",
initialStatus: store.StatusParked,
action: taskstate.ActionRetry,
wantStatus: store.StatusQueued,
},
{
name: "cancel queued task closes it",
initialStatus: store.StatusQueued,
action: taskstate.ActionCancel,
wantStatus: store.StatusClosedWontDo,
},
{
name: "approve waiting_human task queues it",
initialStatus: store.StatusWaitingHuman,
action: taskstate.ActionApprove,
wantStatus: store.StatusQueued,
},
{
name: "reject waiting_human task closes it",
initialStatus: store.StatusWaitingHuman,
action: taskstate.ActionReject,
wantStatus: store.StatusClosedWontDo,
},
{
name: "archive dead task removes it from store",
initialStatus: store.StatusDead,
action: taskstate.ActionArchive,
wantArchived: true,
},
{
name: "archive closed_wont_do task removes it from store",
initialStatus: store.StatusClosedWontDo,
action: taskstate.ActionArchive,
wantArchived: true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
ctx := context.Background()
st, err := store.Open(ctx, filepath.Join(t.TempDir(), "tasks.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = st.Close() })
srv := &webui.Server{Store: st, Cfg: config.NewHolder(config.Config{MaxRetries: 3})}
adapter := chatTaskActorAdapter{
tasks: st.TaskByID,
handler: srv.Handler(),
token: func() string { return srv.Token },
}
task, err := st.EnqueueChatTask(ctx, "acme", "widget", "actionable task", "", "", "archie")
if err != nil {
t.Fatal(err)
}
if tc.initialStatus != store.StatusQueued {
if err := st.Transition(ctx, task.ID, store.StatusQueued, tc.initialStatus, "setup"); err != nil {
t.Fatal(err)
}
}
result, err := adapter.ApplyChatTaskAction(ctx, "archie", task.ID, tc.action)
if err != nil {
t.Fatalf("ApplyChatTaskAction(%s) error = %v", tc.action, err)
}
if result.TaskID != task.ID {
t.Errorf("result.TaskID = %d, want %d", result.TaskID, task.ID)
}
if result.Action != string(tc.action) {
t.Errorf("result.Action = %q, want %q", result.Action, tc.action)
}
current, err := st.TaskByID(ctx, task.ID)
if err != nil {
t.Fatal(err)
}
if tc.wantArchived {
if current != nil {
t.Errorf("task %d still exists in store after archive: %+v", task.ID, current)
}
} else {
if current == nil {
t.Fatalf("task %d not found in store", task.ID)
}
if current.Status != tc.wantStatus {
t.Errorf("task status = %q, want %q", current.Status, tc.wantStatus)
}
}
})
}
}
func TestChatTaskActorAdapterWithTokenAuth(t *testing.T) {
ctx := context.Background()
st, err := store.Open(ctx, filepath.Join(t.TempDir(), "tasks.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = st.Close() })
srv := &webui.Server{
Store: st,
Cfg: config.NewHolder(config.Config{MaxRetries: 3}),
Token: "secret-token-12345",
}
adapter := chatTaskActorAdapter{
tasks: st.TaskByID,
handler: srv.Handler(),
token: func() string { return srv.Token },
}
task, err := st.EnqueueChatTask(ctx, "acme", "widget", "auth task", "", "", "archie")
if err != nil {
t.Fatal(err)
}
if err := st.Transition(ctx, task.ID, store.StatusQueued, store.StatusParked, "park"); err != nil {
t.Fatal(err)
}
result, err := adapter.ApplyChatTaskAction(ctx, "archie", task.ID, taskstate.ActionAbandon)
if err != nil {
t.Fatalf("ApplyChatTaskAction with token auth failed: %v", err)
}
if result.TaskID != task.ID {
t.Errorf("result.TaskID = %d, want %d", result.TaskID, task.ID)
}
current, err := st.TaskByID(ctx, task.ID)
if err != nil || current == nil {
t.Fatalf("TaskByID = (%+v, %v)", current, err)
}
if current.Status != store.StatusClosedWontDo {
t.Errorf("status = %q, want %q", current.Status, store.StatusClosedWontDo)
}
}
+2
View File
@@ -46,6 +46,7 @@ type telegramSetup struct {
ChatController gateway.TaskController
ChatTaskLister gateway.ChatTaskLister
ChatTaskLogs gateway.ChatTaskLogReader
ChatTaskActor gateway.ChatTaskActor
DefaultChatIdentity string
SessionStore gateway.SessionStore
Updates telegram.UpdateService
@@ -301,6 +302,7 @@ func newChatTurnRunner(
TaskLister: s.ChatTaskLister,
Tasks: s.ChatTasks,
TaskLogs: s.ChatTaskLogs,
TaskActor: s.ChatTaskActor,
TaskIdentity: s.DefaultChatIdentity,
Bus: s.Bus,
BotUser: cfg.BotUser,