feat(webui): page through task logs
This commit is contained in:
11 files changed
+376
-93
No files matched your search
@@ -8307,7 +8307,11 @@ type ReadTaskLogRequest struct {
|
||||
// stage filters on an entry's own "stage" field. Only entries a stage
|
||||
// tagged carry one -- agent and tool output is logged without a stage, so a
|
||||
// stage filter narrows the file rather than covering it. Empty means any.
|
||||
Stage string `protobuf:"bytes,9,opt,name=stage,proto3" json:"stage,omitempty"`
|
||||
Stage string `protobuf:"bytes,9,opt,name=stage,proto3" json:"stage,omitempty"`
|
||||
// before_id is the byte-offset cursor a previous page returned as cursor: the
|
||||
// read returns the most recent matching entries ending at or before it. Zero
|
||||
// reads the tail, which is the page the dashboard opens on.
|
||||
BeforeId int64 `protobuf:"varint,10,opt,name=before_id,json=beforeId,proto3" json:"before_id,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
@@ -8405,6 +8409,13 @@ func (x *ReadTaskLogRequest) GetStage() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *ReadTaskLogRequest) GetBeforeId() int64 {
|
||||
if x != nil {
|
||||
return x.BeforeId
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// found=false is not an error: the task has no log file for that attempt.
|
||||
// That is a different condition from a process that cannot read logs at all
|
||||
// (codes.Unavailable), and the dashboard reports them differently.
|
||||
@@ -8418,7 +8429,13 @@ type ReadTaskLogResponse struct {
|
||||
// file is the path read, so the page can name where the log came from. It
|
||||
// is the state directory's own layout, reported to an already-authenticated
|
||||
// operator; no caller is expected to open it.
|
||||
File string `protobuf:"bytes,6,opt,name=file,proto3" json:"file,omitempty"`
|
||||
File string `protobuf:"bytes,6,opt,name=file,proto3" json:"file,omitempty"`
|
||||
// more_available reports matching entries did not fit in this page; cursor
|
||||
// is the before_id for the next older one. truncated is separate: it means
|
||||
// the log is larger than the readable scan window, so its oldest entries
|
||||
// cannot be reached by paging at all.
|
||||
MoreAvailable bool `protobuf:"varint,7,opt,name=more_available,json=moreAvailable,proto3" json:"more_available,omitempty"`
|
||||
Cursor int64 `protobuf:"varint,8,opt,name=cursor,proto3" json:"cursor,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
@@ -8495,6 +8512,20 @@ func (x *ReadTaskLogResponse) GetFile() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *ReadTaskLogResponse) GetMoreAvailable() bool {
|
||||
if x != nil {
|
||||
return x.MoreAvailable
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (x *ReadTaskLogResponse) GetCursor() int64 {
|
||||
if x != nil {
|
||||
return x.Cursor
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// StreamTaskLogContent returns one attempt's log verbatim, for a download. The
|
||||
// bytes are the file's own JSONL, not a re-encoding of it: this contract
|
||||
// transports the log, it does not restate the format. found=false, with no
|
||||
@@ -15085,7 +15116,7 @@ const file_state_v1_state_proto_rawDesc = "" +
|
||||
"\x05level\x18\x03 \x01(\tR\x05level\x12\x10\n" +
|
||||
"\x03msg\x18\x04 \x01(\tR\x03msg\x12\x1f\n" +
|
||||
"\vfields_json\x18\x05 \x01(\tR\n" +
|
||||
"fieldsJson\"\xa9\x02\n" +
|
||||
"fieldsJson\"\xc6\x02\n" +
|
||||
"\x12ReadTaskLogRequest\x12\x17\n" +
|
||||
"\atask_id\x18\x01 \x01(\x03R\x06taskId\x12\x18\n" +
|
||||
"\aattempt\x18\x02 \x01(\x03R\aattempt\x12\x14\n" +
|
||||
@@ -15095,7 +15126,9 @@ const file_state_v1_state_proto_rawDesc = "" +
|
||||
"\bcontains\x18\x06 \x01(\tR\bcontains\x120\n" +
|
||||
"\x05since\x18\a \x01(\v2\x1a.google.protobuf.TimestampR\x05since\x120\n" +
|
||||
"\x05until\x18\b \x01(\v2\x1a.google.protobuf.TimestampR\x05until\x12\x14\n" +
|
||||
"\x05stage\x18\t \x01(\tR\x05stage\"\xc9\x01\n" +
|
||||
"\x05stage\x18\t \x01(\tR\x05stage\x12\x1b\n" +
|
||||
"\tbefore_id\x18\n" +
|
||||
" \x01(\x03R\bbeforeId\"\x88\x02\n" +
|
||||
"\x13ReadTaskLogResponse\x120\n" +
|
||||
"\aentries\x18\x01 \x03(\v2\x16.state.v1.TaskLogEntryR\aentries\x12\x1c\n" +
|
||||
"\ttruncated\x18\x02 \x01(\bR\ttruncated\x12\x18\n" +
|
||||
@@ -15104,7 +15137,9 @@ const file_state_v1_state_proto_rawDesc = "" +
|
||||
"\n" +
|
||||
"components\x18\x05 \x03(\tR\n" +
|
||||
"components\x12\x12\n" +
|
||||
"\x04file\x18\x06 \x01(\tR\x04file\"P\n" +
|
||||
"\x04file\x18\x06 \x01(\tR\x04file\x12%\n" +
|
||||
"\x0emore_available\x18\a \x01(\bR\rmoreAvailable\x12\x16\n" +
|
||||
"\x06cursor\x18\b \x01(\x03R\x06cursor\"P\n" +
|
||||
"\x1bStreamTaskLogContentRequest\x12\x17\n" +
|
||||
"\atask_id\x18\x01 \x01(\x03R\x06taskId\x12\x18\n" +
|
||||
"\aattempt\x18\x02 \x01(\x03R\aattempt\"d\n" +
|
||||
|
||||
@@ -775,11 +775,13 @@ func (c *Client) TaskLog(ctx context.Context, taskID int64, attempt int, q loggi
|
||||
components = []string{}
|
||||
}
|
||||
return logging.TaskLogPage{
|
||||
Entries: mapValues(r.Entries, taskLogEntryValue),
|
||||
Truncated: r.Truncated,
|
||||
File: r.File,
|
||||
Found: true,
|
||||
Components: components,
|
||||
Entries: mapValues(r.Entries, taskLogEntryValue),
|
||||
Truncated: r.Truncated,
|
||||
MoreAvailable: r.MoreAvailable,
|
||||
Cursor: r.Cursor,
|
||||
File: r.File,
|
||||
Found: true,
|
||||
Components: components,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -1128,12 +1128,14 @@ func (s *server) ReadTaskLog(ctx context.Context, r *pb.ReadTaskLogRequest) (*pb
|
||||
return &pb.ReadTaskLogResponse{Attempt: r.Attempt}, nil
|
||||
}
|
||||
return &pb.ReadTaskLogResponse{
|
||||
Entries: mapValues(page.Entries, taskLogEntryProto),
|
||||
Truncated: page.Truncated,
|
||||
Attempt: r.Attempt,
|
||||
Found: true,
|
||||
Components: page.Components,
|
||||
File: page.File,
|
||||
Entries: mapValues(page.Entries, taskLogEntryProto),
|
||||
Truncated: page.Truncated,
|
||||
MoreAvailable: page.MoreAvailable,
|
||||
Cursor: page.Cursor,
|
||||
Attempt: r.Attempt,
|
||||
Found: true,
|
||||
Components: page.Components,
|
||||
File: page.File,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -661,7 +661,7 @@ func taskLogRequestProto(taskID int64, attempt int, q logging.Query) *pb.ReadTas
|
||||
return &pb.ReadTaskLogRequest{
|
||||
TaskId: taskID, Attempt: int64(attempt), Limit: int64(q.Limit),
|
||||
Levels: q.Levels, Component: q.Component, Stage: q.Stage, Contains: q.Contains,
|
||||
Since: timestamp(q.Since), Until: timestamp(q.Until),
|
||||
Since: timestamp(q.Since), Until: timestamp(q.Until), BeforeId: q.BeforeID,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -674,5 +674,6 @@ func taskLogQueryValue(r *pb.ReadTaskLogRequest) logging.Query {
|
||||
Limit: int(r.Limit),
|
||||
Since: timeValue(r.Since),
|
||||
Until: timeValue(r.Until),
|
||||
BeforeID: r.BeforeId,
|
||||
}
|
||||
}
|
||||
@@ -46,6 +46,10 @@ type Query struct {
|
||||
// Both bounds are inclusive. The zero time disables its bound.
|
||||
Since time.Time
|
||||
Until time.Time
|
||||
// BeforeID is the byte-offset cursor a previous page returned as Cursor: a
|
||||
// read returns the most recent matching entries whose end offset is at or
|
||||
// before it. Zero reads the tail of the scan window.
|
||||
BeforeID int64
|
||||
}
|
||||
|
||||
// Read bounds. A log file is unbounded input, so a request must not be able to
|
||||
@@ -77,6 +81,10 @@ type Result struct {
|
||||
type TaskLogPage struct {
|
||||
Result
|
||||
Components []string `json:"components"`
|
||||
// MoreAvailable reports matching entries did not fit in this page; Cursor
|
||||
// is the byte offset to pass back as Query.BeforeID for the next older one.
|
||||
MoreAvailable bool `json:"more_available"`
|
||||
Cursor int64 `json:"cursor"`
|
||||
}
|
||||
|
||||
// ErrTaskLogsUnavailable reports that this process cannot read task logs.
|
||||
@@ -93,11 +101,16 @@ func (r *TaskRegistry) TaskLog(_ context.Context, taskID int64, attempt int, q Q
|
||||
if r == nil {
|
||||
return TaskLogPage{}, ErrTaskLogsUnavailable
|
||||
}
|
||||
res, err := Tail(TaskLogPath(r.baseDir, taskID, attempt), q)
|
||||
res, err := PageBefore(TaskLogPath(r.baseDir, taskID, attempt), q, q.BeforeID)
|
||||
if err != nil {
|
||||
return TaskLogPage{}, err
|
||||
}
|
||||
page := TaskLogPage{Result: res, Components: []string{}}
|
||||
page := TaskLogPage{
|
||||
Entries: res.Entries, Truncated: res.Truncated, File: res.File, Found: res.Found,
|
||||
Components: []string{},
|
||||
MoreAvailable: res.MoreAvailable,
|
||||
Cursor: res.Cursor,
|
||||
}
|
||||
// The filter list is a convenience: failing to build it must not cost the
|
||||
// caller their entries. Components only reads the tail this page already
|
||||
// read, so its error is the same "treat as empty" case the daemon-wide
|
||||
@@ -138,6 +151,9 @@ type PageResult struct {
|
||||
MoreAvailable bool `json:"more_available"`
|
||||
Cursor int64 `json:"cursor"`
|
||||
File string `json:"file"`
|
||||
// Found reports whether the log file exists; false with no entries is an
|
||||
// attempt with no log, not an error.
|
||||
Found bool `json:"found"`
|
||||
}
|
||||
|
||||
// logWindow is the region of a log file one read examines: the
|
||||
@@ -196,7 +212,7 @@ func readWindow(path string) (w logWindow, found bool, err error) {
|
||||
// readLines calls step for each matching entry in the last maxScanBytes of
|
||||
// path, with the offset after its line. It returns whether the window
|
||||
// excluded older data and the next cursor. Older data is never read.
|
||||
func readLines(path string, q Query, cursor int64, step func(e Entry, endOff int64) bool) (truncated bool, cursorOut int64, ok bool, err error) {
|
||||
func readLines(path string, q Query, cursor int64, step func(e Entry, startOff, endOff int64) bool) (truncated bool, cursorOut int64, ok bool, err error) {
|
||||
w, found, err := readWindow(path)
|
||||
if err != nil || !found {
|
||||
return false, 0, false, err
|
||||
@@ -236,7 +252,7 @@ func readLines(path string, q Query, cursor int64, step func(e Entry, endOff int
|
||||
// walkWindow calls step for each matching entry from offset from, skipping
|
||||
// undecodable lines, and returns the file offset to resume at. step returns
|
||||
// false to stop.
|
||||
func walkWindow(data []byte, windowStart, from int64, q Query, step func(e Entry, endOff int64) bool) int64 {
|
||||
func walkWindow(data []byte, windowStart, from int64, q Query, step func(e Entry, startOff, endOff int64) bool) int64 {
|
||||
pos := from
|
||||
for pos < int64(len(data)) {
|
||||
// lineEnd indexes the line's terminating '\n'; when the final
|
||||
@@ -253,7 +269,7 @@ func walkWindow(data []byte, windowStart, from int64, q Query, step func(e Entry
|
||||
if len(raw) > 0 && raw[len(raw)-1] == '\r' {
|
||||
raw = raw[:len(raw)-1] // CRLF
|
||||
}
|
||||
if entry, decoded := decode(raw); decoded && q.matches(entry) && !step(entry, endOff) {
|
||||
if entry, decoded := decode(raw); decoded && q.matches(entry) && !step(entry, windowStart+pos, endOff) {
|
||||
return endOff
|
||||
}
|
||||
if nl < 0 {
|
||||
@@ -287,7 +303,7 @@ func Tail(path string, q Query) (Result, error) {
|
||||
}
|
||||
|
||||
matches := make([]Entry, 0, limit)
|
||||
truncated, _, ok, err := readLines(path, q, 0, func(e Entry, _ int64) bool {
|
||||
truncated, _, ok, err := readLines(path, q, 0, func(e Entry, _, _ int64) bool {
|
||||
if len(matches) == limit {
|
||||
matches = append(matches[:0], matches[1:]...)
|
||||
res.Truncated = true
|
||||
@@ -321,12 +337,22 @@ func Page(path string, q Query, cursor int64) (PageResult, error) {
|
||||
limit = MaxTailLines
|
||||
}
|
||||
|
||||
// Stat for Found without reading the window twice. readLines cannot report
|
||||
// the difference between a missing file and a cursor at EOF.
|
||||
if strings.TrimSpace(path) != "" {
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
res.Found = true
|
||||
} else if !os.IsNotExist(err) {
|
||||
return PageResult{}, fmt.Errorf("logging: stat %s: %w", path, err)
|
||||
}
|
||||
}
|
||||
|
||||
type lineAt struct {
|
||||
entry Entry
|
||||
endOff int64
|
||||
}
|
||||
matches := make([]lineAt, 0, limit)
|
||||
truncated, endOff, ok, err := readLines(path, q, cursor, func(e Entry, end int64) bool {
|
||||
truncated, endOff, ok, err := readLines(path, q, cursor, func(e Entry, _, end int64) bool {
|
||||
if len(matches) < limit {
|
||||
matches = append(matches, lineAt{entry: e, endOff: end})
|
||||
return true
|
||||
@@ -362,6 +388,70 @@ func Page(path string, q Query, cursor int64) (PageResult, error) {
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// PageBefore returns up to Limit matching entries whose end offset is at or
|
||||
// before before (the whole scan window when before is zero), oldest first. It
|
||||
// is the backward counterpart of Page: Cursor is the before value for the next
|
||||
// older page, and MoreAvailable reports older matches beyond this page.
|
||||
func PageBefore(path string, q Query, before int64) (PageResult, error) {
|
||||
res := PageResult{Entries: []Entry{}, File: path}
|
||||
limit := q.Limit
|
||||
if limit <= 0 {
|
||||
limit = DefaultTailLines
|
||||
}
|
||||
if limit > MaxTailLines {
|
||||
limit = MaxTailLines
|
||||
}
|
||||
|
||||
// Stat for Found without reading the window twice. readLines cannot report
|
||||
// the difference between a missing file and a cursor at EOF.
|
||||
if strings.TrimSpace(path) != "" {
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
res.Found = true
|
||||
} else if !os.IsNotExist(err) {
|
||||
return PageResult{}, fmt.Errorf("logging: stat %s: %w", path, err)
|
||||
}
|
||||
}
|
||||
|
||||
type lineAt struct {
|
||||
entry Entry
|
||||
start, end int64
|
||||
}
|
||||
matches := make([]lineAt, 0, limit)
|
||||
truncated, _, ok, err := readLines(path, q, 0, func(e Entry, start, end int64) bool {
|
||||
// File order is oldest to newest: once past the bound, nothing older
|
||||
// follows.
|
||||
if before > 0 && end > before {
|
||||
return false
|
||||
}
|
||||
if len(matches) == limit {
|
||||
// Drop the oldest so the page holds the limit entries closest to
|
||||
// before, and remember older entries exist beyond it.
|
||||
matches = append(matches[:0], matches[1:]...)
|
||||
res.MoreAvailable = true
|
||||
}
|
||||
matches = append(matches, lineAt{entry: e, start: start, end: end})
|
||||
return true
|
||||
})
|
||||
if err != nil {
|
||||
return PageResult{}, err
|
||||
}
|
||||
if !ok {
|
||||
res.Truncated = truncated
|
||||
return res, nil
|
||||
}
|
||||
res.Truncated = truncated
|
||||
res.Entries = make([]Entry, len(matches))
|
||||
for i, m := range matches {
|
||||
res.Entries[i] = m.entry
|
||||
}
|
||||
// The oldest returned entry's start is the before value that returns the
|
||||
// entries strictly older than this page. No matches means no older page.
|
||||
if len(matches) > 0 {
|
||||
res.Cursor = matches[0].start
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// decode parses one slog JSON line, lifting the known keys out and keeping the
|
||||
// rest as structured fields. A line that is not JSON is skipped: a log file may
|
||||
// contain output from a subprocess that does not share our format.
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/logging"
|
||||
)
|
||||
@@ -29,6 +30,18 @@ func (s *Server) handleTaskLogs(w http.ResponseWriter, r *http.Request) {
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
beforeID, ok := taskLogBeforeID(w, q.Get("before"))
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
since, ok := taskLogTime(w, "since", q.Get("since"))
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
until, ok := taskLogTime(w, "until", q.Get("until"))
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
page, err := reader.TaskLog(r.Context(), id, attempt, logging.Query{
|
||||
Levels: splitCSV(q.Get("level")),
|
||||
Component: strings.TrimSpace(q.Get("component")),
|
||||
@@ -38,6 +51,9 @@ func (s *Server) handleTaskLogs(w http.ResponseWriter, r *http.Request) {
|
||||
Stage: strings.TrimSpace(q.Get("stage")),
|
||||
Contains: strings.TrimSpace(q.Get("q")),
|
||||
Limit: limit,
|
||||
BeforeID: beforeID,
|
||||
Since: since,
|
||||
Until: until,
|
||||
})
|
||||
if err != nil {
|
||||
// A reader that exists but cannot read here is still a statement about
|
||||
@@ -52,11 +68,13 @@ func (s *Server) handleTaskLogs(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
writeJSON(w, map[string]any{
|
||||
"entries": page.Entries,
|
||||
"truncated": page.Truncated,
|
||||
"file": page.File,
|
||||
"components": page.Components,
|
||||
"attempt": attempt,
|
||||
"entries": page.Entries,
|
||||
"truncated": page.Truncated,
|
||||
"more_available": page.MoreAvailable,
|
||||
"cursor": page.Cursor,
|
||||
"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,
|
||||
@@ -138,6 +156,35 @@ func taskLogLimit(w http.ResponseWriter, raw string) (int, bool) {
|
||||
}
|
||||
}
|
||||
|
||||
// taskLogBeforeID parses the byte-offset cursor a previous page returned: the
|
||||
// read returns the entries older than it.
|
||||
func taskLogBeforeID(w http.ResponseWriter, raw string) (int64, bool) {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
return 0, true
|
||||
}
|
||||
parsed, err := strconv.ParseInt(raw, 10, 64)
|
||||
if err != nil || parsed < 0 {
|
||||
http.Error(w, "bad before", http.StatusBadRequest)
|
||||
return 0, false
|
||||
}
|
||||
return parsed, true
|
||||
}
|
||||
|
||||
// taskLogTime parses an RFC 3339 time bound; empty disables it.
|
||||
func taskLogTime(w http.ResponseWriter, name, raw string) (time.Time, bool) {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
return time.Time{}, true
|
||||
}
|
||||
parsed, err := time.Parse(time.RFC3339, raw)
|
||||
if err != nil {
|
||||
http.Error(w, "bad "+name, http.StatusBadRequest)
|
||||
return time.Time{}, false
|
||||
}
|
||||
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
|
||||
|
||||
Reference in new issue
Block a user