Files
archie/internal/agentexec/subjects_test.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

50 lines
1.9 KiB
Go

package agentexec
import "testing"
// TestTaskIDFromSystemSubject pins the round trip with SubjectForSystem, and
// that a subject which merely resembles one (wrong kind suffix, malformed
// ID) is rejected rather than parsed into a wrong task ID -- a daemon
// demuxing a wildcard subscription onto SubjectSystemWildcard has no other
// check on what actually arrives.
func TestTaskIDFromSystemSubject(t *testing.T) {
tests := []struct {
name string
subject string
wantID int64
wantOK bool
}{
{"round trip with SubjectForSystem", SubjectForSystem(42), 42, true},
{"round trip with a large ID", SubjectForSystem(9_223_372_036), 9_223_372_036, true},
{"wrong kind suffix is rejected", "archie.agent.42.response", 0, false},
{"missing prefix is rejected", "some.other.42.system", 0, false},
{"non-numeric ID is rejected", "archie.agent.abc.system", 0, false},
{"zero ID is rejected", "archie.agent.0.system", 0, false},
{"negative ID is rejected", "archie.agent.-1.system", 0, false},
{"empty subject is rejected", "", 0, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotID, gotOK := TaskIDFromSystemSubject(tt.subject)
if gotID != tt.wantID || gotOK != tt.wantOK {
t.Errorf("TaskIDFromSystemSubject(%q) = (%d, %v), want (%d, %v)",
tt.subject, gotID, gotOK, tt.wantID, tt.wantOK)
}
})
}
}
func TestSubjectSystemWildcardMatchesEverySystemSubject(t *testing.T) {
tests := []int64{1, 42, 9_223_372_036}
for _, taskID := range tests {
if id, ok := TaskIDFromSystemSubject(SubjectForSystem(taskID)); !ok || id != taskID {
t.Errorf("SubjectForSystem(%d) did not round-trip through TaskIDFromSystemSubject: got (%d, %v)", taskID, id, ok)
}
}
if SubjectSystemWildcard != "archie.agent.*.system" {
t.Errorf("SubjectSystemWildcard = %q, want the single-token wildcard NATS matches against subjectForTask's shape",
SubjectSystemWildcard)
}
}