Files
archie/internal/webui/api_tasks_logs.go
T
sam 862ac46e8b feat(tasks): attribute events, changes and config to the attempt that produced them
The events table had no attempt column, so two attempts of the same task were
indistinguishable: a retried task's stage events interleaved into one timeline and
nothing could say which run produced a given result.

- events gains an `attempt` column through the existing presence-gated
  migrateTasks loop rather than a version bump, so user_version stays 3 and an
  existing archie.db is migrated in place. A legacy-database test builds the
  pre-change schema and proves both that existing rows survive unattributed
  (attempt = 0) and that a new value round-trips.
- Event.attempt = 11 and ReadTaskLogRequest.stage = 9 are field additions to
  existing messages: no new RPC, no new table, and no change to any canonical error
  message that crosses the state-store wire.
- Every task event the workflow engine emits now carries the attempt. Producers
  that have no attempt yet (task_queued, binding_dispatch_failure) stay
  unattributed rather than guessing one.
- Changed files are captured as a durable changes_captured event at commit and push
  time, reusing the existing three-dot diff primitive: paths from FilePatches(),
  counts from Stats(), base_sha recorded as the merge base actually diffed rather
  than the base tip, a 200-file cap with totals kept complete. Consumption goes
  through an unexported optional interface so workflow.Trees -- the yaegi capability
  surface for interpreted stage plugins -- is not widened. A capture is also taken
  once the pull request exists, so the capture can link it.
- The per-attempt configuration is captured as a durable config_captured event
  carrying the non-secret config.TaskConfig, instead of a new table plus two new
  state-store RPCs: events are already this system's per-attempt provenance stream,
  so a table would have given one feature two provenance mechanisms. Redaction is
  proven against canary secrets. Measured payload: 539 bytes typical, 2333 bytes at
  20 models.
- Captures carry a schema marker and are refused unless their key set matches the
  declared payload shape, so drift surfaces as "recorded but could not be read"
  rather than as a capture of zeroes.
- Three read endpoints serve the attempt rail, the changed-file captures and a raw
  debug view, mapping to UI-owned response types rather than dumping store types.
- The task-log read gains a stage filter, bound to the stage logger for that
  stage's own lines; agent and tool output records no stage.

A task-scoped grant may already insert events on its own task, so this provenance
is exactly as trustworthy as the rest of that task's event stream. That limit is
documented rather than papered over.
2026-09-18 18:32:59 +10:00

220 lines
8.3 KiB
Go

package webui
import (
"context"
"errors"
"io"
"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.
//
// Two outcomes are kept apart on purpose, because only one of them is about
// the operator's configuration. A process with no task-log reader cannot say
// anything about an attempt, so it reports disabled and the page explains that
// this process cannot read logs. A process WITH a reader and an attempt with
// no log file reports found=false, which the page states as no log recorded
// for the attempt. Collapsing the second into the first is what made the
// dashboard claim "task logging is optional and was not enabled for this run"
// for every attempt of every task in a deployment where logging is
// unconditional.
func (s *Server) handleTaskLogs(w http.ResponseWriter, r *http.Request) {
id, attempt, ok := s.taskLogTarget(w, r)
if !ok {
return
}
reader := s.taskLogReader()
if reader == nil {
writeDisabledTaskLogs(w, attempt)
return
}
q := r.URL.Query()
limit, ok := taskLogLimit(w, q.Get("limit"))
if !ok {
return
}
page, err := reader.TaskLog(r.Context(), id, attempt, logging.Query{
Levels: splitCSV(q.Get("level")),
Component: strings.TrimSpace(q.Get("component")),
// Stage is a narrowing filter, not a scoping one: only entries a stage
// tagged carry the field, so the UI states that a stage filter matches
// the lines that record a stage and nothing else.
Stage: strings.TrimSpace(q.Get("stage")),
Contains: strings.TrimSpace(q.Get("q")),
Limit: limit,
})
if err != nil {
// A reader that exists but cannot read here is still a statement about
// this process rather than about the attempt, so it keeps the disabled
// answer instead of claiming the attempt has no log.
if errors.Is(err, logging.ErrTaskLogsUnavailable) {
writeDisabledTaskLogs(w, attempt)
return
}
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
writeJSON(w, map[string]any{
"entries": page.Entries,
"truncated": page.Truncated,
"file": page.File,
"components": page.Components,
"attempt": attempt,
// found is the distinction the page needs: a reader answering with no
// entries because the file is absent is not a reader that is absent.
"found": page.Found,
})
}
// handleTaskLogDownload serves one task attempt's log verbatim as a file
// download -- the log itself rather than a decoded view of it, because an
// operator who asks for a download wants to read it in their own tooling or
// hand it to someone else.
//
// The conditions stay apart here too. A process with no reader answers 503,
// not 404: "there is no log for this attempt" is a claim only a process that
// can look is in a position to make.
func (s *Server) handleTaskLogDownload(w http.ResponseWriter, r *http.Request) {
id, attempt, ok := s.taskLogTarget(w, r)
if !ok {
return
}
reader := s.taskLogReader()
if reader == nil {
http.Error(w, logging.ErrTaskLogsUnavailable.Error(), http.StatusServiceUnavailable)
return
}
// Headers are set before the read because the body streams straight from
// the reader into the response: a log file is unbounded input, so
// buffering it to size the response would defeat the reason the reader
// streams at all. The attempt's own name is known up front.
//
// Everything after this point must therefore CLEAR them before writing an
// error, never add to them: a 404 still carrying `Content-Disposition:
// attachment` is a browser download of the error text under a .log
// filename, and the NDJSON type would misdescribe it too.
w.Header().Set("Content-Type", "application/x-ndjson")
w.Header().Set("Content-Disposition", `attachment; filename="`+taskLogFilename(id, attempt)+`"`)
clearAttachment := func() {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Header().Del("Content-Disposition")
}
found, err := reader.TaskLogContent(r.Context(), id, attempt, w)
if err != nil {
if errors.Is(err, logging.ErrTaskLogsUnavailable) {
clearAttachment()
http.Error(w, err.Error(), http.StatusServiceUnavailable)
return
}
// Once a 200 and a body are on the wire there is no status left to
// send, so a failure partway through is logged where an operator can
// find it rather than silently truncating the file.
s.logf("task log download failed", "task", id, "attempt", attempt, "err", err)
return
}
if !found {
clearAttachment()
http.Error(w, "no log recorded for this attempt", http.StatusNotFound)
}
}
// taskLogTarget resolves the {id} path value and the task it names, returning
// the attempt to read: the "attempt" query parameter when the request names
// one, otherwise the task's current Attempt. It answers the request itself and
// reports false when either step fails.
//
// The resolution itself is taskAttemptTarget's, shared with the attempt rail,
// the changed-files read and the debug view, so every per-attempt read of one
// task selects the same attempt for the same request.
func (s *Server) taskLogTarget(w http.ResponseWriter, r *http.Request) (id int64, attempt int, ok bool) {
t, attempt, ok := s.taskAttemptTarget(w, r)
if !ok {
return 0, 0, false
}
return t.ID, attempt, true
}
// taskLogLimit bounds a page the way handleLogs bounds the daemon-wide read:
// the limit is caller-supplied and a log file is unbounded, so a request must
// not be able to pull an arbitrary amount of it into memory.
func taskLogLimit(w http.ResponseWriter, raw string) (int, bool) {
raw = strings.TrimSpace(raw)
if raw == "" {
return logging.DefaultTailLines, true
}
parsed, err := strconv.Atoi(raw)
if err != nil {
http.Error(w, "bad limit", http.StatusBadRequest)
return 0, false
}
switch {
case parsed <= 0:
return logging.DefaultTailLines, true
case parsed > logging.MaxTailLines:
return logging.MaxTailLines, true
default:
return parsed, true
}
}
// writeDisabledTaskLogs is the "this process cannot read task logs" answer,
// the same convention handleLogs uses for the daemon-wide log: an optional
// capability that is absent here must not turn "why did this task park?" into
// a 500 for every task.
func writeDisabledTaskLogs(w http.ResponseWriter, attempt int) {
writeJSON(w, map[string]any{
"entries": []logging.Entry{},
"file": "",
"disabled": true,
"attempt": attempt,
})
}
// taskLogFilename names a downloaded attempt's log. It carries the task and
// the attempt because an operator downloading two attempts of the same task
// must not end up with one file overwriting the other, and the attempt is the
// one identifier the log's own path would have given them.
func taskLogFilename(taskID int64, attempt int) string {
return "task-" + strconv.FormatInt(taskID, 10) + "-attempt-" + strconv.Itoa(attempt) + ".log"
}
// taskLogReader is the task-log read capability this process has, or nil when
// it has none. The seam is a narrow interface rather than a concrete registry
// so the same handlers serve both processes: the daemon holds a registry over
// its own state directory, the dashboard holds the State Store client. A
// consumer-owned seam is the difference between the dashboard degrading
// honestly and the dashboard opening another process's files.
func (s *Server) taskLogReader() TaskLogSource {
if s == nil {
return nil
}
return s.TaskLogs
}
// TaskLogSource is the read seam the task-log handlers consume. It is the
// logging package's own reader contract, so a registry and a remote client
// satisfy it without either one restating the log format -- the package that
// owns the format also owns the shape of a read of it.
type TaskLogSource interface {
// TaskLog returns a page of one attempt's decoded log.
TaskLog(ctx context.Context, taskID int64, attempt int, q logging.Query) (logging.TaskLogPage, error)
// TaskLogContent writes one attempt's log verbatim to w, for a download.
TaskLogContent(ctx context.Context, taskID int64, attempt int, w io.Writer) (bool, error)
}