Files
archie/internal/agentexec/subjects.go
T
sam 951ddbc9f4 feat(daemon): consume agent system logs into per-task sinks and the dashboard feed
Closes the loop #451-#453 opened. archie-agent (#453) now publishes its
own logs to agentexec.SubjectForSystem(taskID) over NATS, and a task
log sink (#452) exists to persist them, but nothing on the daemon side
subscribed, demuxed, or wrote anything -- the messages had nowhere to
land.

- agentexec: SubjectSystemWildcard + TaskIDFromSystemSubject, the
  inverse of SubjectForSystem, for a daemon subscribing to every
  task's system subject with one wildcard subscription.
- logging: TaskRegistry manages one open TaskSink per running task
  attempt, mirroring every write onto the live dashboard Feed the same
  way a locally-written log would appear. Nil-receiver-safe, matching
  every other optional composition-wired Daemon field.
- daemon: Daemon.process opens a task's log sink for its own duration
  (covering both the sandboxed-container and in-process dispatch
  paths uniformly) and closes it on every return path.
- cmd/archied: constructs the registry, subscribes core NATS to
  SubjectSystemWildcard, and wires TaskLogs into both Daemon and
  webui.Server.
- webui: archiving a task now removes its log directory too, so
  archived tasks' logs don't accumulate forever. Documented as a
  known, accepted race: a task can reach terminal status (satisfying
  ArchiveTask's precondition) before process() itself returns and
  closes the sink, so a very late system-log write can race an
  archive's Remove. Not corrupting -- the write lands on a
  soon-to-be-unlinked inode and is discarded -- and not worth fixing
  here: it would mean tying Close to the store transition instead of
  to process()'s return, a larger change than this issue's scope.

Adversarial review (fresh subagent) found and fixed two real issues in
the first draft: a doc comment accidentally spliced between two
functions during an extraction meant to fix a lint regression, and a
genuine test gap on openTaskLog (added, using TaskRegistry.Write's own
true/false contract as the observable -- no Store/Forge/Trees fakes
needed, since openTaskLog only touches TaskLogs and Log).
2026-08-09 18:42:54 +10:00

72 lines
2.5 KiB
Go

package agentexec
import (
"strconv"
"strings"
)
// Agent execution subjects.
//
// These belong to this package because it defines what an agent request and
// response mean. They previously sat in the NATS package, which made the
// transport the owner of Archie's agent protocol; see
// docs/architecture/dependencies-and-contracts.md -- message schemas and the
// subjects addressing them stay with the domain that defines them.
//
// The stage travels in the payload, not the subject: one subject per task
// carrying sequential stage messages.
const (
// SubjectAgentWildcard matches every agent subject.
SubjectAgentWildcard = "archie.agent.>"
// SubjectSystemWildcard matches every task's system subject, for a
// daemon subscribing to all of them at once with a single core NATS
// subscription rather than one per running task.
SubjectSystemWildcard = subjectAgentPrefix + "*.system"
subjectAgentPrefix = "archie.agent."
subjectSystemSuffix = ".system"
)
// SubjectForRequest returns the subject carrying stage execution requests for
// a task.
func SubjectForRequest(taskID int64) string {
return subjectForTask(taskID, "request")
}
// SubjectForResponse returns the subject for agent output destined for human
// channels. The daemon reviews these before forwarding to issue comments,
// labels, or pull requests.
func SubjectForResponse(taskID int64) string {
return subjectForTask(taskID, "response")
}
// SubjectForSystem returns the subject for internal agent messages such as
// log dumps, health, and PII warnings. The daemon reads these for
// observability and never forwards them.
func SubjectForSystem(taskID int64) string {
return subjectForTask(taskID, "system")
}
// subjectForTask builds a per-task agent subject.
func subjectForTask(taskID int64, kind string) string {
return subjectAgentPrefix + strconv.FormatInt(taskID, 10) + "." + kind
}
// TaskIDFromSystemSubject extracts the task ID from a subject produced by
// SubjectForSystem, for a daemon demuxing a SubjectSystemWildcard
// subscription. Rejects (0, false) for anything that isn't exactly that
// shape, rather than parsing a partial or wrong-kind subject into a
// misleading task ID.
func TaskIDFromSystemSubject(subject string) (int64, bool) {
if !strings.HasPrefix(subject, subjectAgentPrefix) || !strings.HasSuffix(subject, subjectSystemSuffix) {
return 0, false
}
middle := strings.TrimSuffix(strings.TrimPrefix(subject, subjectAgentPrefix), subjectSystemSuffix)
id, err := strconv.ParseInt(middle, 10, 64)
if err != nil || id <= 0 {
return 0, false
}
return id, true
}