feat(agentexec): publish agent-side logs to the task's system subject

Part of #453 (task-observability epic #450). archie-agent logged to
os.Stderr only, and its container is created with AutoRemove: true
(internal/container/pool.go), so all of it disappeared the moment a
task's container exited -- there was no way to see why, say, a gate
command failed inside one.

Adds agentexec.SystemLogHandler: a slog.Handler that tees to whatever
it wraps (kept for an operator attached to a live container) and
additionally publishes each record, JSON-encoded as a logging.Entry,
to SubjectForSystem(taskID) -- a subject that already existed,
documented for exactly this ("log dumps, health... observability"),
with zero publishers anywhere in the codebase.

cmd/archie-agent/main.go wires it in only when this container was
spawned for one specific task (bootTaskID / .git/task.json present);
shared queue-group mode serves many unrelated tasks per process, so
there's no single task subject to ship to and keeps the stderr-only
logger.

Building this surfaced a real pre-existing bug: FeedHandler's attr
flattening applied the group chain active at Handle-time to *all*
accumulated attrs, including ones added via WithAttrs before a later
WithGroup call -- nesting them under a group they were never
actually inside, contrary to slog's own documented Handler contract.
It went unnoticed because nothing in the codebase called WithGroup
until this change needed to. Extracting the shared flattening logic
into exported FlattenAttrs/PrefixAttrs (so SystemLogHandler doesn't
duplicate it) is what surfaced it; fixed in the same change since I
was the one making it public API.

Docs on the wire format's own trap for the daemon-side consumer
(#454, not yet written): the published bytes are a marshaled
logging.Entry, meant for json.Unmarshal, not logging.decode() --
decode() parses raw slog JSON *log lines* off disk, a different and
incompatible shape. See the doc comment on SystemLogHandler.
This commit is contained in:
sam committed 2026-08-09 16:49:39 +10:00
1 parent b56f07fe73
commit 53da52ecd1
5 files changed
+509 -13

No files matched your search

+28 -4
View File
@@ -106,8 +106,30 @@ func run() int {
return 1
}
// A dedicated per-task subscription (below) means this container was
// spawned for one task, which is also when its stderr is otherwise
// unrecoverable: container.Pool creates it with AutoRemove: true. Once
// nc exists, ship this process's own logs to the daemon over
// SubjectForSystem(taskID) so they survive the container's exit. Shared
// queue-group mode serves many unrelated tasks per process, so there is
// no single task subject to ship to -- it keeps the stderr-only logger.
//
// This only covers logging done through the `log` variable from here on.
// bus (arnats.Client) was constructed above with the pre-wrap logger and
// keeps it for its own lifetime, so its own wire-activity Debug lines
// (e.g. "published" on every bus.Respond) stay stderr-only regardless.
// That's a real, known gap, not an oversight: it's the bus's internal
// chatter about NATS itself, not the agent's own reasoning or tool
// output, and reordering construction to close it would mean connecting
// to NATS before knowing whether NATS is even needed for this call.
taskID, hasTaskID := bootTaskID(storage.WorktreeMountDir, log)
if hasTaskID {
log = slog.New(agentexec.NewSystemLogHandler(log.Handler(), nc, taskID))
log.Info("system log publisher attached", "task", taskID)
}
// Subscribe to task run messages.
taskRunSub, err := subscribeTaskRuns(ctx, nc, log)
taskRunSub, err := subscribeTaskRuns(ctx, nc, log, taskID, hasTaskID)
if err != nil {
log.Error("taskrun subscribe failed", "err", err)
return 1
@@ -124,9 +146,11 @@ func run() int {
// subscribeTaskRuns prefers a dedicated per-task subscription over the
// shared queue group whenever this container was spawned for a specific
// task (.git/task.json present at the worktree mount).
func subscribeTaskRuns(ctx context.Context, nc *nats.Conn, log *slog.Logger) (*nats.Subscription, error) {
if taskID, ok := bootTaskID(storage.WorktreeMountDir, log); ok {
// task. taskID/hasTaskID come from bootTaskID (.git/task.json present at the
// worktree mount) -- the caller resolves it once, since it also decides
// whether to attach a per-task log publisher to log before this is called.
func subscribeTaskRuns(ctx context.Context, nc *nats.Conn, log *slog.Logger, taskID int64, hasTaskID bool) (*nats.Subscription, error) {
if hasTaskID {
log.Info("taskrun: dedicated per-task subscription", "task", taskID)
return nc.Subscribe(taskrun.SubjectForTask(taskID), func(msg *nats.Msg) {
handleTaskRun(ctx, msg, nc, log)