Merge pull request #461 from samcharles93/feat/455-task-logs-api-and-chat-tool

feat(webui,gateway): add /api/tasks/{id}/logs endpoint and task_logs chat tool
This commit is contained in:
sam authored and GitHub committed 2026-08-09 19:16:43 +10:00
commit aef7d48df2
11 files changed
+749 -14

No files matched your search

+74 -2
View File
@@ -499,7 +499,11 @@ func run() int {
webSetup := telegramSetup{
Cfg: cfg, St: st, LLM: llm, ChatModels: chatModels, ToolReg: toolReg,
Personas: personas, ChatTasks: chatTasks, ChatController: chatController,
ChatTaskLister: chatTaskListerAdapter{tasks: st.Tasks},
ChatTaskLister: chatTaskListerAdapter{tasks: st.Tasks},
ChatTaskLogs: chatTaskLogReaderAdapter{
tasks: st.TaskByID,
taskLogs: taskLogs,
},
DefaultChatIdentity: defaultChatIdentity, SessionStore: chatSessionStore,
Bus: bus, Log: log,
}
@@ -521,7 +525,11 @@ func run() int {
Cfg: cfg, CfgPath: *cfgPath, OverlayPath: *overlayPath,
St: st, LLM: llm, ChatModels: chatModels, ToolReg: toolReg,
Personas: personas, ChatTasks: chatTasks, ChatController: chatController,
ChatTaskLister: chatTaskListerAdapter{tasks: st.Tasks},
ChatTaskLister: chatTaskListerAdapter{tasks: st.Tasks},
ChatTaskLogs: chatTaskLogReaderAdapter{
tasks: st.TaskByID,
taskLogs: taskLogs,
},
DefaultChatIdentity: defaultChatIdentity, SessionStore: chatSessionStore, Updates: updateService,
Bus: bus,
RegisterRestart: func(request func() error) { restartTelegram = request }, Log: log,
@@ -1074,6 +1082,70 @@ func (a chatTaskControllerAdapter) CancelChatTask(ctx context.Context, taskID in
return a.transition(ctx, taskID, task.Status, store.StatusClosedWontDo, reason)
}
// chatTaskLogReaderAdapter gives the gateway a read view of a task's
// persisted log history without importing internal/logging or internal/store
// into the gateway package. Each identity's reader is scoped to its own
// tasks: the identity bound at construction is used for authorization, so a
// model cannot read another identity's task logs by passing a different
// identity through the tool input.
type chatTaskLogReaderAdapter struct {
tasks func(context.Context, int64) (*store.Task, error)
taskLogs *logging.TaskRegistry
}
func (a chatTaskLogReaderAdapter) ReadChatTaskLogs(
ctx context.Context, identity string, taskID int64, attempt int, q gateway.ChatTaskLogQuery,
) (gateway.ChatTaskLogResult, error) {
task, err := a.tasks(ctx, taskID)
if err != nil {
return gateway.ChatTaskLogResult{}, err
}
if task == nil {
return gateway.ChatTaskLogResult{}, fmt.Errorf("task %d not found", taskID)
}
// Match the filter chatTaskListerAdapter already applies: a model
// bound to one identity must not read logs for another identity's
// tasks, and tasks with no identity (forge-sourced) are not
// readable through a chat tool at all — they belong to the daemon,
// not a particular identity. "The empty string MUST NOT retain
// special meaning" (docs/architecture/identity.md).
if task.Identity != identity {
return gateway.ChatTaskLogResult{}, fmt.Errorf("task %d belongs to %q, not %q", taskID, task.Identity, identity)
}
if attempt <= 0 {
attempt = task.Attempt
}
path := a.taskLogs.Path(taskID, attempt)
if path == "" {
return gateway.ChatTaskLogResult{Attempt: attempt, Entries: []gateway.ChatTaskLogEntry{}}, nil
}
result, err := logging.Tail(path, logging.Query{
Component: q.Component,
Contains: q.Contains,
Limit: q.Limit,
})
if err != nil {
return gateway.ChatTaskLogResult{}, err
}
entries := make([]gateway.ChatTaskLogEntry, len(result.Entries))
for i, e := range result.Entries {
entries[i] = gateway.ChatTaskLogEntry{
Time: e.Time,
Level: e.Level,
Message: e.Message,
Fields: e.Fields,
}
}
return gateway.ChatTaskLogResult{
Entries: entries,
Attempt: attempt,
Truncated: result.Truncated,
}, nil
}
func chatTaskProfiles(cfg config.Config) ([]gateway.TaskProfile, string) {
if len(cfg.Identities) > 0 {
profiles := make([]gateway.TaskProfile, 0, len(cfg.Identities))
+114
View File
@@ -22,6 +22,7 @@ import (
"github.com/samcharles93/archie-core/internal/forge"
"github.com/samcharles93/archie-core/internal/forgerpc"
"github.com/samcharles93/archie-core/internal/gateway"
"github.com/samcharles93/archie-core/internal/logging"
"github.com/samcharles93/archie-core/internal/nell"
"github.com/samcharles93/archie-core/internal/secret"
"github.com/samcharles93/archie-core/internal/store"
@@ -430,6 +431,119 @@ func TestChatTaskControllerAdapterTransitions(t *testing.T) {
}
}
func TestChatTaskLogReaderAdapterIdentityEnforcement(t *testing.T) {
t.Parallel()
tests := []struct {
name string
identity string
task *store.Task
wantErr bool
}{
{
name: "own chat task allowed",
identity: "archie",
task: &store.Task{ID: 42, Identity: "archie", Attempt: 1, Source: store.SourceChat},
wantErr: false,
},
{
name: "other identity chat task denied",
identity: "archie",
task: &store.Task{ID: 42, Identity: "other-bot", Attempt: 1, Source: store.SourceChat},
wantErr: true,
},
{
name: "forge task (empty identity) denied",
identity: "archie",
task: &store.Task{ID: 42, Identity: "", Attempt: 1, Source: store.SourceForge},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
adapter := chatTaskLogReaderAdapter{
tasks: func(_ context.Context, _ int64) (*store.Task, error) { return tt.task, nil },
}
_, err := adapter.ReadChatTaskLogs(context.Background(), tt.identity, tt.task.ID, 0, gateway.ChatTaskLogQuery{})
if tt.wantErr && err == nil {
t.Error("expected error, got nil")
}
if !tt.wantErr && err != nil {
t.Errorf("unexpected error: %v", err)
}
})
}
}
func TestChatTaskLogReaderAdapterDefaultAttempt(t *testing.T) {
task := &store.Task{ID: 42, Identity: "archie", Attempt: 3, Source: store.SourceChat}
adapter := chatTaskLogReaderAdapter{
tasks: func(_ context.Context, _ int64) (*store.Task, error) { return task, nil },
}
result, err := adapter.ReadChatTaskLogs(context.Background(), "archie", task.ID, 0, gateway.ChatTaskLogQuery{})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result.Attempt != 3 {
t.Errorf("attempt = %d, want 3 (resolved from task.Attempt when 0 is passed)", result.Attempt)
}
}
func TestChatTaskLogReaderAdapterEmptyResultWhenNoRegistry(t *testing.T) {
task := &store.Task{ID: 42, Identity: "archie", Attempt: 1, Source: store.SourceChat}
adapter := chatTaskLogReaderAdapter{
tasks: func(_ context.Context, _ int64) (*store.Task, error) { return task, nil },
taskLogs: nil,
}
result, err := adapter.ReadChatTaskLogs(context.Background(), "archie", task.ID, 1, gateway.ChatTaskLogQuery{})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result.Entries == nil {
t.Error("Entries is nil, want empty slice (non-nil avoids model retrying)")
}
if len(result.Entries) != 0 {
t.Errorf("Entries = %+v, want empty", result.Entries)
}
}
func TestChatTaskLogReaderAdapterRoundTrip(t *testing.T) {
baseDir := t.TempDir()
taskLogs := logging.NewTaskRegistry(baseDir, logging.NewFeed(10), logging.TaskSinkOptions{})
t.Cleanup(func() { _ = taskLogs.Remove(42) })
task := &store.Task{ID: 42, Identity: "archie", Attempt: 1, Source: store.SourceChat}
adapter := chatTaskLogReaderAdapter{
tasks: func(_ context.Context, _ int64) (*store.Task, error) { return task, nil },
taskLogs: taskLogs,
}
// Write through the registry (same path the daemon's NATS consumer takes).
if err := taskLogs.Open(task.ID, task.Attempt); err != nil {
t.Fatal(err)
}
taskLogs.Write(task.ID, logging.Entry{Level: "ERROR", Message: "gate failed", Fields: map[string]any{"component": "gate"}})
if err := taskLogs.Close(task.ID); err != nil {
t.Fatal(err)
}
// Read back through the adapter (same path the chat tool takes).
result, err := adapter.ReadChatTaskLogs(context.Background(), "archie", task.ID, 1, gateway.ChatTaskLogQuery{})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(result.Entries) != 1 {
t.Fatalf("got %d entries, want 1", len(result.Entries))
}
entry := result.Entries[0]
if entry.Message != "gate failed" || entry.Level != "ERROR" {
t.Errorf("entry = %+v, want message=gate failed level=ERROR", entry)
}
}
func TestChatTaskCommandsEndToEnd(t *testing.T) {
ctx := context.Background()
st, err := nell.OpenStore(filepath.Join(t.TempDir(), "tasks.db"), "test-node")
+2
View File
@@ -40,6 +40,7 @@ type telegramSetup struct {
ChatTasks gateway.TaskCreator
ChatController gateway.TaskController
ChatTaskLister gateway.ChatTaskLister
ChatTaskLogs gateway.ChatTaskLogReader
DefaultChatIdentity string
SessionStore gateway.SessionStore
Updates telegram.UpdateService
@@ -209,6 +210,7 @@ func newChatTurnRunner(
Model: newChatTurnModel(s.LLM, s.ToolReg, s.Cfg.Chat.MaxSteps, toolLimits(s.Cfg)),
TaskLister: s.ChatTaskLister,
Tasks: s.ChatTasks,
TaskLogs: s.ChatTaskLogs,
TaskIdentity: s.DefaultChatIdentity,
Bus: s.Bus,
BotUser: s.Cfg.BotUser,
+132 -1
View File
@@ -4,6 +4,7 @@ import (
"context"
"fmt"
"strings"
"time"
"github.com/samcharles93/archie-core/internal/tools"
)
@@ -15,6 +16,12 @@ const (
// maxTaskListLimit bounds what the model can ask for. A queue of a few
// hundred tasks would otherwise land in the context window whole.
maxTaskListLimit = 100
// defaultTaskLogLimit is how many log entries a bare task_logs returns.
defaultTaskLogLimit = 200
// maxTaskLogLimit bounds what the model can ask for in one call.
maxTaskLogLimit = 2000
)
// ChatTaskSummary is the per-task view the task tools return. It is
@@ -48,6 +55,49 @@ type TaskSpawnResult struct {
Message string `json:"message"`
}
// ChatTaskLogEntry is one log line as task_logs returns it. It mirrors
// internal/logging.Entry's shape without importing that package: gateway
// keeps its independence from internal/logging the same way it already does
// from internal/store, with the daemon adapting between them.
type ChatTaskLogEntry struct {
Time time.Time `json:"time"`
Level string `json:"level"`
Message string `json:"message"`
Fields map[string]any `json:"fields,omitempty"`
}
// ChatTaskLogQuery filters a task_logs read.
type ChatTaskLogQuery struct {
// Component matches the log entry's component field case-insensitively.
// Empty means any.
Component string
// Contains matches the message or any field value, case-insensitively.
Contains string
// Limit caps returned entries.
Limit int
}
// ChatTaskLogResult is what task_logs returns.
type ChatTaskLogResult struct {
Entries []ChatTaskLogEntry `json:"entries"`
// Attempt is the attempt number actually read, so the model can tell
// the caller which run it looked at when none was requested explicitly.
Attempt int `json:"attempt"`
// Truncated reports that older matching entries exist beyond what this
// page examined.
Truncated bool `json:"truncated"`
}
// ChatTaskLogReader is the read surface task_logs needs. It mirrors the
// ChatTaskLister pattern: gateway states what it needs and the daemon
// supplies an adapter, so this package keeps its independence from
// internal/store and internal/logging.
type ChatTaskLogReader interface {
// ReadChatTaskLogs returns taskID's log entries for attempt (0 selects
// the task's current/latest attempt), scoped to identity's own tasks.
ReadChatTaskLogs(ctx context.Context, identity string, taskID int64, attempt int, q ChatTaskLogQuery) (ChatTaskLogResult, error)
}
// TaskTools builds the chat tools that let Archie see and add to its own work
// queue. Until these existed, /spawn and the task list were slash commands only
// a human could type, so "what are you working on?" had no tool path at all.
@@ -65,7 +115,7 @@ type TaskSpawnResult struct {
// inbound email can be quoted into it -- so a tool that approves Archie's own
// work would remove the only human checkpoint in the lifecycle. Cancel is
// destructive on the same untrusted path. Both remain slash commands.
func TaskTools(lister ChatTaskLister, creator TaskCreator, identity string) []tools.ToolEntry {
func TaskTools(lister ChatTaskLister, creator TaskCreator, logs ChatTaskLogReader, identity string) []tools.ToolEntry {
var entries []tools.ToolEntry
if lister != nil {
entries = append(entries, taskListTool(lister, identity))
@@ -73,6 +123,9 @@ func TaskTools(lister ChatTaskLister, creator TaskCreator, identity string) []to
if creator != nil {
entries = append(entries, taskSpawnTool(creator, identity))
}
if logs != nil {
entries = append(entries, taskLogsTool(logs, identity))
}
return entries
}
@@ -174,6 +227,84 @@ func taskSpawnTool(creator TaskCreator, identity string) tools.ToolEntry {
}
}
func taskLogsTool(reader ChatTaskLogReader, identity string) tools.ToolEntry {
return tools.ToolEntry{
Name: "task_logs",
Toolset: "tasks",
Description: "Read a task's persisted log history -- gate output, agent activity, errors -- " +
"to answer questions like \"why did task N park?\" or \"what happened on task N?\" " +
"without an operator needing to open the dashboard. " +
"Defaults to the task's latest attempt when attempt is omitted.",
Classification: tools.ClassIdempotent,
Schema: tools.JSONSchema{
"type": "object",
"properties": map[string]any{
"task_id": map[string]any{
"type": "integer",
"description": "ID of the task to read logs for.",
},
"attempt": map[string]any{
"type": "integer",
"description": "Which attempt to read. Omit for the task's current/latest attempt.",
},
"component": map[string]any{
"type": "string",
"description": "Only return entries from this component (e.g. \"gate\"). Omit for all components.",
},
"q": map[string]any{
"type": "string",
"description": "Only return entries whose message or fields contain this text (case-insensitive).",
},
"limit": map[string]any{
"type": "integer",
"description": fmt.Sprintf(
"Maximum entries to return. Defaults to %d, capped at %d.",
defaultTaskLogLimit, maxTaskLogLimit),
},
},
"required": []any{"task_id"},
},
Handler: func(ctx context.Context, input map[string]any) (any, error) {
taskID, ok := asInt64(input["task_id"])
if !ok {
return nil, fmt.Errorf("task_logs: task_id is required")
}
attempt := 0
if v, ok := asInt64(input["attempt"]); ok {
attempt = int(v)
}
limit := tools.ListLimit(input, defaultTaskLogLimit, maxTaskLogLimit)
// The bound identity, never input["identity"].
result, err := reader.ReadChatTaskLogs(ctx, identity, taskID, attempt, ChatTaskLogQuery{
Component: strings.TrimSpace(asString(input["component"])),
Contains: strings.TrimSpace(asString(input["q"])),
Limit: limit,
})
if err != nil {
return nil, fmt.Errorf("task_logs: %w", err)
}
return result, nil
},
}
}
// asInt64 reads an integer field. JSON numbers decode as float64, but a
// model that emits an integer through a different provider path can arrive
// as int or int64, so all three are accepted.
func asInt64(v any) (int64, bool) {
switch n := v.(type) {
case float64:
return int64(n), true
case int64:
return n, true
case int:
return int64(n), true
default:
return 0, false
}
}
// taskListLimit resolves the requested limit, falling back to the default for
// anything absent or non-positive and clamping the rest.
//
+138 -10
View File
@@ -48,7 +48,7 @@ func toolNamed(t *testing.T, entries []tools.ToolEntry, name string) tools.ToolE
}
func TestTaskToolsValidate(t *testing.T) {
entries := TaskTools(&fakeLister{}, &fakeCreator{}, "archie")
entries := TaskTools(&fakeLister{}, &fakeCreator{}, nil, "archie")
if len(entries) == 0 {
t.Fatal("TaskTools() returned nothing")
}
@@ -76,7 +76,7 @@ func TestTaskToolsOmitNilBackends(t *testing.T) {
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
entries := TaskTools(tc.lister, tc.creator, "archie")
entries := TaskTools(tc.lister, tc.creator, nil, "archie")
var got []string
for _, e := range entries {
got = append(got, e.Name)
@@ -93,7 +93,7 @@ func TestTaskToolsOmitNilBackends(t *testing.T) {
// that could name an identity could read another instance's work.
func TestTaskListUsesBoundIdentity(t *testing.T) {
lister := &fakeLister{tasks: []ChatTaskSummary{{ID: 7, Title: "wire the thing", Status: "running"}}}
entry := toolNamed(t, TaskTools(lister, nil, "archie"), "task_list")
entry := toolNamed(t, TaskTools(lister, nil, nil, "archie"), "task_list")
// The model supplies an identity; it must be ignored.
if _, err := entry.Handler(context.Background(), map[string]any{"identity": "someone-else"}); err != nil {
@@ -120,7 +120,7 @@ func TestTaskListLimit(t *testing.T) {
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
lister := &fakeLister{}
entry := toolNamed(t, TaskTools(lister, nil, "archie"), "task_list")
entry := toolNamed(t, TaskTools(lister, nil, nil, "archie"), "task_list")
if _, err := entry.Handler(context.Background(), tc.input); err != nil {
t.Fatal(err)
}
@@ -137,7 +137,7 @@ func TestTaskListFiltersByStatus(t *testing.T) {
{ID: 2, Status: "queued"},
{ID: 3, Status: "running"},
}}
entry := toolNamed(t, TaskTools(lister, nil, "archie"), "task_list")
entry := toolNamed(t, TaskTools(lister, nil, nil, "archie"), "task_list")
out, err := entry.Handler(context.Background(), map[string]any{"status": "running"})
if err != nil {
@@ -160,7 +160,7 @@ func TestTaskListFiltersByStatus(t *testing.T) {
// TestTaskListEmptyIsNotAnError guards against the model reading an empty
// queue as a broken tool and retrying.
func TestTaskListEmptyIsNotAnError(t *testing.T) {
entry := toolNamed(t, TaskTools(&fakeLister{}, nil, "archie"), "task_list")
entry := toolNamed(t, TaskTools(&fakeLister{}, nil, nil, "archie"), "task_list")
out, err := entry.Handler(context.Background(), map[string]any{})
if err != nil {
@@ -177,7 +177,7 @@ func TestTaskListEmptyIsNotAnError(t *testing.T) {
func TestTaskListPropagatesStoreError(t *testing.T) {
lister := &fakeLister{err: errors.New("database is gone")}
entry := toolNamed(t, TaskTools(lister, nil, "archie"), "task_list")
entry := toolNamed(t, TaskTools(lister, nil, nil, "archie"), "task_list")
if _, err := entry.Handler(context.Background(), map[string]any{}); err == nil {
t.Fatal("handler error = nil, want the store failure surfaced")
@@ -228,7 +228,7 @@ func TestTaskSpawn(t *testing.T) {
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
entry := toolNamed(t, TaskTools(nil, tc.creator, "archie"), "task_spawn")
entry := toolNamed(t, TaskTools(nil, tc.creator, nil, "archie"), "task_spawn")
out, err := entry.Handler(context.Background(), tc.input)
if tc.wantErr {
if err == nil {
@@ -264,7 +264,7 @@ func TestTaskSpawn(t *testing.T) {
// model must not be able to file work under another instance's identity.
func TestTaskSpawnIgnoresModelIdentity(t *testing.T) {
creator := &fakeCreator{id: 1}
entry := toolNamed(t, TaskTools(nil, creator, "archie"), "task_spawn")
entry := toolNamed(t, TaskTools(nil, creator, nil, "archie"), "task_spawn")
if _, err := entry.Handler(context.Background(), map[string]any{
"title": "sneaky",
@@ -282,7 +282,7 @@ func TestTaskSpawnIgnoresModelIdentity(t *testing.T) {
// an untrusted input path. If these are ever added they must be a considered
// decision, not an accident.
func TestTaskToolsExcludeApprovalTools(t *testing.T) {
entries := TaskTools(&fakeLister{}, &fakeCreator{}, "archie")
entries := TaskTools(&fakeLister{}, &fakeCreator{}, nil, "archie")
for _, e := range entries {
switch e.Name {
case "task_approve", "task_cancel":
@@ -290,3 +290,131 @@ func TestTaskToolsExcludeApprovalTools(t *testing.T) {
}
}
}
// ── task_logs tests ──────────────────────────────────────────────────────
type fakeLogReader struct {
result ChatTaskLogResult
err error
gotIdentity string
gotTaskID int64
gotAttempt int
gotQuery ChatTaskLogQuery
}
func (f *fakeLogReader) ReadChatTaskLogs(_ context.Context, identity string, taskID int64, attempt int, q ChatTaskLogQuery) (ChatTaskLogResult, error) {
f.gotIdentity = identity
f.gotTaskID = taskID
f.gotAttempt = attempt
f.gotQuery = q
return f.result, f.err
}
func TestTaskLogsUsesBoundIdentity(t *testing.T) {
reader := &fakeLogReader{result: ChatTaskLogResult{Attempt: 1}}
entry := toolNamed(t, TaskTools(nil, nil, reader, "archie"), "task_logs")
if _, err := entry.Handler(context.Background(), map[string]any{
"task_id": float64(7), "identity": "someone-else",
}); err != nil {
t.Fatal(err)
}
if reader.gotIdentity != "archie" {
t.Errorf("identity = %q, want the bound %q", reader.gotIdentity, "archie")
}
}
func TestTaskLogsRequiresTaskID(t *testing.T) {
reader := &fakeLogReader{}
entry := toolNamed(t, TaskTools(nil, nil, reader, "archie"), "task_logs")
if _, err := entry.Handler(context.Background(), map[string]any{}); err == nil {
t.Error("expected error for missing task_id, got nil")
}
}
func TestTaskLogsDefaultsAttemptToZero(t *testing.T) {
reader := &fakeLogReader{result: ChatTaskLogResult{Attempt: 0}}
entry := toolNamed(t, TaskTools(nil, nil, reader, "archie"), "task_logs")
if _, err := entry.Handler(context.Background(), map[string]any{
"task_id": float64(7),
}); err != nil {
t.Fatal(err)
}
if reader.gotAttempt != 0 {
t.Errorf("attempt = %d, want 0 (zero = 'latest', resolved by the adapter)", reader.gotAttempt)
}
}
func TestTaskLogsHonoursExplicitAttempt(t *testing.T) {
reader := &fakeLogReader{result: ChatTaskLogResult{Attempt: 2}}
entry := toolNamed(t, TaskTools(nil, nil, reader, "archie"), "task_logs")
if _, err := entry.Handler(context.Background(), map[string]any{
"task_id": float64(7), "attempt": float64(2),
}); err != nil {
t.Fatal(err)
}
if reader.gotAttempt != 2 {
t.Errorf("attempt = %d, want 2", reader.gotAttempt)
}
}
func TestTaskLogsLimit(t *testing.T) {
tests := []struct {
name string
input map[string]any
want int
}{
{name: "default when absent", input: map[string]any{"task_id": float64(1)}, want: defaultTaskLogLimit},
{name: "explicit", input: map[string]any{"task_id": float64(1), "limit": float64(5)}, want: 5},
{name: "clamped to the maximum", input: map[string]any{"task_id": float64(1), "limit": float64(10_000)}, want: maxTaskLogLimit},
{name: "non-positive falls back to the default", input: map[string]any{"task_id": float64(1), "limit": float64(0)}, want: defaultTaskLogLimit},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
reader := &fakeLogReader{result: ChatTaskLogResult{}}
entry := toolNamed(t, TaskTools(nil, nil, reader, "archie"), "task_logs")
if _, err := entry.Handler(context.Background(), tc.input); err != nil {
t.Fatal(err)
}
if reader.gotQuery.Limit != tc.want {
t.Errorf("limit = %d, want %d", reader.gotQuery.Limit, tc.want)
}
})
}
}
func TestTaskLogsForwardsComponentAndContains(t *testing.T) {
reader := &fakeLogReader{result: ChatTaskLogResult{}}
entry := toolNamed(t, TaskTools(nil, nil, reader, "archie"), "task_logs")
if _, err := entry.Handler(context.Background(), map[string]any{
"task_id": float64(7), "component": "gate", "q": "build failed",
}); err != nil {
t.Fatal(err)
}
if reader.gotQuery.Component != "gate" {
t.Errorf("Component = %q, want gate", reader.gotQuery.Component)
}
if reader.gotQuery.Contains != "build failed" {
t.Errorf("Contains = %q, want build failed", reader.gotQuery.Contains)
}
}
func TestTaskLogsOmittedWhenReaderIsNil(t *testing.T) {
entries := TaskTools(&fakeLister{}, &fakeCreator{}, nil, "archie")
for _, e := range entries {
if e.Name == "task_logs" {
t.Fatal("task_logs is in the tool set when reader is nil -- a tool that always fails must not be advertised")
}
}
}
func TestTaskLogsAppearsWhenReaderIsNonNil(t *testing.T) {
entries := TaskTools(nil, nil, &fakeLogReader{}, "archie")
toolNamed(t, entries, "task_logs") // fails the test if absent
}
+2 -1
View File
@@ -71,6 +71,7 @@ type TurnRunnerConfig struct {
PersistenceTimeout time.Duration
TaskLister ChatTaskLister
Tasks TaskCreator
TaskLogs ChatTaskLogReader
TaskIdentity string
Bus TurnEventPublisher
BotUser string
@@ -219,7 +220,7 @@ func (r *TurnRunner) Run(ctx context.Context, msg Message, onDelta func(string))
compressed := compressTurnHistory(history, r.BotUser)
extraTools := append(
TaskTools(r.TaskLister, r.Tasks, r.TaskIdentity),
TaskTools(r.TaskLister, r.Tasks, r.TaskLogs, r.TaskIdentity),
SessionTools(r.Sessions, r.Router.SessionTracker(), r.Channel, msg)...,
)
modelName := r.Models.ActiveModel()
+12
View File
@@ -109,6 +109,18 @@ func (r *TaskRegistry) Write(taskID int64, entry Entry) bool {
return true
}
// Path returns the on-disk path for one task attempt's log file under this
// registry's baseDir, for callers that only need to read a task's history
// (the API handler, chat tools) rather than write to it. Safe to call on a
// nil registry -- returns "" the same way every write method reports "task
// logging is not configured" on a nil receiver.
func (r *TaskRegistry) Path(taskID int64, attempt int) string {
if r == nil {
return ""
}
return TaskLogPath(r.baseDir, taskID, attempt)
}
// Remove deletes taskID's entire log directory -- every attempt, every
// rotated generation -- so task logs don't accumulate forever once a task
// is archived. Safe to call whether or not a sink is currently open (Close
+26
View File
@@ -169,6 +169,32 @@ func TestTaskRegistryNilReceiverIsSafe(t *testing.T) {
// TestTaskRegistryConcurrentTasksDoNotRace exercises the registry the way
// the daemon actually will: many tasks' Open/Write/Close interleaved from
// different goroutines (task dispatch vs. the NATS subscription handler),
// TestTaskRegistryPathMirrorsTaskLogPath guards the reason a query surface
// (the API handler, the chat tool) can hold only a *TaskRegistry rather than
// also needing baseDir threaded through separately: Path must compute
// exactly what TaskLogPath would from the same baseDir.
func TestTaskRegistryPathMirrorsTaskLogPath(t *testing.T) {
baseDir := t.TempDir()
reg := NewTaskRegistry(baseDir, nil, TaskSinkOptions{})
got := reg.Path(42, 3)
want := TaskLogPath(baseDir, 42, 3)
if got != want {
t.Errorf("Path(42, 3) = %q, want %q", got, want)
}
}
// TestNilTaskRegistryPathIsEmpty guards the "nil means task logging is not
// configured" convention every other TaskRegistry method already follows --
// a query surface must be able to call Path on a possibly-nil registry
// without a separate nil check.
func TestNilTaskRegistryPathIsEmpty(t *testing.T) {
var reg *TaskRegistry
if got := reg.Path(42, 1); got != "" {
t.Errorf("Path() on nil registry = %q, want empty", got)
}
}
// none sharing a task ID. Run with -race.
func TestTaskRegistryConcurrentTasksDoNotRace(t *testing.T) {
reg := NewTaskRegistry(t.TempDir(), NewFeed(100), TaskSinkOptions{})
+84
View File
@@ -0,0 +1,84 @@
package webui
import (
"net/http"
"strconv"
"strings"
"github.com/samcharles93/archie-core/internal/logging"
)
// handleTaskLogs serves one task attempt's persisted log history, the same
// way handleLogs serves the daemon-wide log -- transport only, parsing
// belongs to the logging package.
//
// A request with no "attempt" query param defaults to the task's current
// Attempt from the store, since that is what a human or Archie's own chat
// tool means by "why did task N park?" almost every time: the most recent
// run, not an arbitrary earlier retry.
func (s *Server) handleTaskLogs(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
http.Error(w, "bad id", http.StatusBadRequest)
return
}
task, err := s.Store.TaskByID(r.Context(), id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if task == nil {
http.Error(w, "task not found", http.StatusNotFound)
return
}
q := r.URL.Query()
attempt := task.Attempt
if raw := strings.TrimSpace(q.Get("attempt")); raw != "" {
attempt, err = strconv.Atoi(raw)
if err != nil {
http.Error(w, "bad attempt", http.StatusBadRequest)
return
}
}
path := s.TaskLogs.Path(id, attempt)
if path == "" {
// Not an error: task logging is optional, same convention as
// handleLogs for the daemon-wide log.
writeJSON(w, map[string]any{
"entries": []logging.Entry{},
"file": "",
"disabled": true,
"attempt": attempt,
})
return
}
limit, _ := strconv.Atoi(q.Get("limit"))
result, err := logging.Tail(path, logging.Query{
Levels: splitCSV(q.Get("level")),
Component: strings.TrimSpace(q.Get("component")),
Contains: strings.TrimSpace(q.Get("q")),
Limit: limit,
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
components, err := logging.Components(path)
if err != nil {
s.Log.Warn("task log components unavailable", "task_id", id, "err", err)
components = []string{}
}
writeJSON(w, map[string]any{
"entries": result.Entries,
"truncated": result.Truncated,
"file": result.File,
"components": components,
"attempt": attempt,
})
}
+164
View File
@@ -0,0 +1,164 @@
package webui
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"github.com/samcharles93/archie-core/internal/logging"
)
func TestHandleTaskLogsReturnsEntriesForLatestAttemptByDefault(t *testing.T) {
srv := newTestServer(t)
ctx := t.Context()
baseDir := t.TempDir()
srv.TaskLogs = logging.NewTaskRegistry(baseDir, logging.NewFeed(10), logging.TaskSinkOptions{})
if _, err := srv.Store.EnqueueIssue(ctx, "acme", "widget", 1, "task", "", "", ""); err != nil {
t.Fatal(err)
}
task, err := srv.Store.TaskByIssue(ctx, "acme", "widget", 1)
if err != nil {
t.Fatal(err)
}
if err := srv.TaskLogs.Open(task.ID, task.Attempt); err != nil {
t.Fatal(err)
}
srv.TaskLogs.Write(task.ID, logging.Entry{
Level: "ERROR", Message: "gate failed", Fields: map[string]any{"component": "gate"},
})
if err := srv.TaskLogs.Close(task.ID); err != nil {
t.Fatal(err)
}
w := getTaskLogs(t, srv, task.ID, nil)
if w.Code != http.StatusOK {
t.Fatalf("status = %d, body %s", w.Code, w.Body)
}
var body struct {
Entries []logging.Entry `json:"entries"`
Attempt int `json:"attempt"`
File string `json:"file"`
}
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
t.Fatalf("decode response: %v", err)
}
if body.Attempt != task.Attempt {
t.Errorf("attempt = %d, want %d (task's current attempt, resolved without a query param)", body.Attempt, task.Attempt)
}
if len(body.Entries) != 1 || body.Entries[0].Message != "gate failed" {
t.Fatalf("entries = %+v, want one entry with message %q", body.Entries, "gate failed")
}
}
func TestHandleTaskLogsHonoursExplicitAttempt(t *testing.T) {
srv := newTestServer(t)
ctx := t.Context()
baseDir := t.TempDir()
srv.TaskLogs = logging.NewTaskRegistry(baseDir, logging.NewFeed(10), logging.TaskSinkOptions{})
if _, err := srv.Store.EnqueueIssue(ctx, "acme", "widget", 1, "task", "", "", ""); err != nil {
t.Fatal(err)
}
task, err := srv.Store.TaskByIssue(ctx, "acme", "widget", 1)
if err != nil {
t.Fatal(err)
}
// Write directly to attempt 1's file on disk -- a prior, already-closed
// attempt that the task's current Attempt field no longer points at.
sink, err := logging.NewTaskSink(baseDir, task.ID, 1, logging.TaskSinkOptions{})
if err != nil {
t.Fatal(err)
}
sink.Logger().Warn("first attempt failed")
if err := sink.Close(); err != nil {
t.Fatal(err)
}
w := getTaskLogs(t, srv, task.ID, map[string]string{"attempt": "1"})
if w.Code != http.StatusOK {
t.Fatalf("status = %d, body %s", w.Code, w.Body)
}
var body struct {
Entries []logging.Entry `json:"entries"`
}
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
t.Fatalf("decode response: %v", err)
}
if len(body.Entries) != 1 || body.Entries[0].Message != "first attempt failed" {
t.Fatalf("entries = %+v, want one entry with message %q", body.Entries, "first attempt failed")
}
}
func TestHandleTaskLogsBadID(t *testing.T) {
srv := newTestServer(t)
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/api/tasks/not-a-number/logs", nil)
req.SetPathValue("id", "not-a-number")
w := httptest.NewRecorder()
srv.handleTaskLogs(w, req)
if w.Code != http.StatusBadRequest {
t.Errorf("status = %d, want %d", w.Code, http.StatusBadRequest)
}
}
func TestHandleTaskLogsUnknownTask(t *testing.T) {
srv := newTestServer(t)
srv.TaskLogs = logging.NewTaskRegistry(t.TempDir(), logging.NewFeed(10), logging.TaskSinkOptions{})
w := getTaskLogs(t, srv, 999, nil)
if w.Code != http.StatusNotFound {
t.Errorf("status = %d, want %d", w.Code, http.StatusNotFound)
}
}
// TestHandleTaskLogsTaskLoggingDisabled guards the same "optional feature,
// not an error" behaviour handleLogs already has for the daemon-wide log:
// a deployment that never configured task logging must not turn "why did
// this task park?" into a 500 for every task.
func TestHandleTaskLogsTaskLoggingDisabled(t *testing.T) {
srv := newTestServer(t)
ctx := t.Context()
if _, err := srv.Store.EnqueueIssue(ctx, "acme", "widget", 1, "task", "", "", ""); err != nil {
t.Fatal(err)
}
task, err := srv.Store.TaskByIssue(ctx, "acme", "widget", 1)
if err != nil {
t.Fatal(err)
}
w := getTaskLogs(t, srv, task.ID, nil)
if w.Code != http.StatusOK {
t.Fatalf("status = %d, body %s", w.Code, w.Body)
}
var body struct {
Disabled bool `json:"disabled"`
}
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
t.Fatalf("decode response: %v", err)
}
if !body.Disabled {
t.Error("disabled = false, want true when TaskLogs is not configured")
}
}
func getTaskLogs(t *testing.T, srv *Server, taskID int64, query map[string]string) *httptest.ResponseRecorder {
t.Helper()
url := "/api/tasks/" + strconv.FormatInt(taskID, 10) + "/logs"
if len(query) > 0 {
q := make([]byte, 0)
sep := "?"
for k, v := range query {
q = append(q, []byte(sep+k+"="+v)...)
sep = "&"
}
url += string(q)
}
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, url, nil)
req.SetPathValue("id", strconv.FormatInt(taskID, 10))
w := httptest.NewRecorder()
srv.handleTaskLogs(w, req)
return w
}
+1
View File
@@ -146,6 +146,7 @@ func (s *Server) Handler() http.Handler {
mux.HandleFunc("POST /api/channels/{id}/reload", s.handleChannelReload)
mux.HandleFunc("GET /api/config", s.handleConfig)
mux.HandleFunc("GET /api/logs", s.handleLogs)
mux.HandleFunc("GET /api/tasks/{id}/logs", s.handleTaskLogs)
mux.HandleFunc("GET /api/logs/stream", s.handleLogStream)
mux.HandleFunc("GET /api/memory", s.handleMemory)
mux.HandleFunc("GET /api/chat/sessions", s.handleChatSessions)