feat(agent): stamp archie-agent's build and report it back to the daemon
archied's own build has carried a compiled-in gatewayVersion and an internal/installtype stamp since archie-core-522/b5d08d0, but archie-agent's build had neither -- its Taskfile.yml and Dockerfile build steps passed no -ldflags at all, so even a daemon that wanted to verify what agent build is actually in service had nothing to check against. daemonRunningVersions said as much explicitly: the agent was left out of every verification on purpose, because nothing self-reported a real value. Every archie-agent process is task-scoped and ephemeral (spawned per task, dies after), not a long-lived process the daemon can poll on demand the way it polls Telegram's gateway. So there's no "ask the agent its version" request -- the only channel back is whatever a worker includes in the taskrun.Response it already sends on every completed task. Stamp archie-agent's build the same way archied's is (installtype, plus a new agentworker.Version()), have executeTaskRequest report both in its Response, and have the daemon record the latest observation in a new mutex-guarded AgentStatus, fed from runViaAgent on every successful response only -- never off a request that never reached an agent, or one describing a failed/parked task. daemonRunningVersions now includes the agent component once (and only once) something has actually been observed, so Report.Verify gets a real value to check future update claims against instead of omitting the component entirely. Part 1/3 of archie-core-1786751948782-3-837a8fd0 point 4. Closes archie-core-1786751948782-3-837a8fd0.1.
This commit is contained in:
1 parent
8809d44505
commit
ed33ab5380
15 files changed
+313
-30
No files matched your search
@@ -14,6 +14,7 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/skillbuild"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/wfeval"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/installtype"
|
||||
"github.com/samcharles93/archie-core/internal/storage"
|
||||
"github.com/samcharles93/archie-core/internal/store"
|
||||
"github.com/samcharles93/archie-core/internal/taskrun"
|
||||
@@ -200,7 +201,9 @@ func runTask(ctx context.Context, req taskrun.Request, dependencies taskDependen
|
||||
workflow.Run(ctx, wf, tc)
|
||||
|
||||
return &taskrun.Response{
|
||||
Task: tc.Task,
|
||||
Status: tc.Outcome.Status,
|
||||
Task: tc.Task,
|
||||
Status: tc.Outcome.Status,
|
||||
AgentVersion: Version(),
|
||||
AgentInstallType: installtype.Type(),
|
||||
}, nil
|
||||
}
|
||||
@@ -21,6 +21,7 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/forge"
|
||||
"github.com/samcharles93/archie-core/internal/forgerpc"
|
||||
agentnats "github.com/samcharles93/archie-core/internal/infrastructure/agenttransport/nats"
|
||||
"github.com/samcharles93/archie-core/internal/installtype"
|
||||
"github.com/samcharles93/archie-core/internal/store"
|
||||
"github.com/samcharles93/archie-core/internal/storerpc"
|
||||
"github.com/samcharles93/archie-core/internal/taskrun"
|
||||
@@ -413,6 +414,15 @@ func TestExecuteTaskRequestUsesInfrastructureRPCDependencies(t *testing.T) {
|
||||
if response.Status != store.StatusPROpen || len(forge.prs) != 1 {
|
||||
t.Fatalf("response status/PRs = (%q, %d), want (%q, 1)", response.Status, len(forge.prs), store.StatusPROpen)
|
||||
}
|
||||
// archie-core-1786751948782-3-837a8fd0 point 4: every response reports
|
||||
// what this worker actually is, since it's the only channel the daemon
|
||||
// has to learn what's really running (see daemon.AgentStatus).
|
||||
if response.AgentVersion != Version() {
|
||||
t.Errorf("response.AgentVersion = %q, want Version() = %q", response.AgentVersion, Version())
|
||||
}
|
||||
if response.AgentInstallType != installtype.Type() {
|
||||
t.Errorf("response.AgentInstallType = %q, want installtype.Type() = %q", response.AgentInstallType, installtype.Type())
|
||||
}
|
||||
stored, err := st.TaskByIssue(ctx, "acme", "rpc-widget", 2)
|
||||
if err != nil || stored == nil || stored.Status != store.StatusPROpen || stored.PRNumber != 5 {
|
||||
t.Fatalf("stored task = (%+v, %v), want RPC-persisted PR-open task #5", stored, err)
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package agentworker
|
||||
|
||||
// version is stamped per release artifact via
|
||||
// "-ldflags -X github.com/samcharles93/archie-core/internal/app/agentworker.version=<value>",
|
||||
// the same mechanism internal/installtype uses for buildType -- see that
|
||||
// package's doc comment for why this is baked in at build time rather than
|
||||
// detected. Reported back to the daemon in every taskrun.Response
|
||||
// (see task_execution.go) so it can observe the build actually running,
|
||||
// not the one its own release pipeline expected.
|
||||
var version = "dev"
|
||||
|
||||
// Version reports the archie-agent build this worker process was built
|
||||
// from, or "dev" for an unstamped build (a local `go build`, for instance).
|
||||
func Version() string {
|
||||
if version == "" {
|
||||
return "dev"
|
||||
}
|
||||
return version
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package agentworker
|
||||
|
||||
import "testing"
|
||||
|
||||
// TestVersionReportsDevWhenUnstamped guards the unstamped-build fallback:
|
||||
// an empty ldflags value (e.g. a local `go build`, or a release pipeline
|
||||
// that forgot -X for this package) must read as "dev", the same
|
||||
// placeholder cmd/archied's own version vars use, not as an empty string
|
||||
// that would silently sail through daemon.AgentStatus as a real version.
|
||||
func TestVersionReportsDevWhenUnstamped(t *testing.T) {
|
||||
orig := version
|
||||
t.Cleanup(func() { version = orig })
|
||||
version = ""
|
||||
|
||||
if got := Version(); got != "dev" {
|
||||
t.Errorf("Version() = %q, want %q", got, "dev")
|
||||
}
|
||||
}
|
||||
|
||||
func TestVersionReportsStampedValue(t *testing.T) {
|
||||
orig := version
|
||||
t.Cleanup(func() { version = orig })
|
||||
version = "1.9.11"
|
||||
|
||||
if got := Version(); got != "1.9.11" {
|
||||
t.Errorf("Version() = %q, want %q", got, "1.9.11")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package daemon
|
||||
|
||||
import "sync"
|
||||
|
||||
// AgentStatus tracks the most recent version and install type an
|
||||
// archie-agent worker reported about itself. It is observed passively, off
|
||||
// ordinary taskrun.Response traffic in runViaAgent -- there is no dedicated
|
||||
// poll, since every archie-agent process is task-scoped and ephemeral (see
|
||||
// docs/architecture and internal/app/agentworker), not a long-lived process
|
||||
// the daemon could query on demand the way it can Telegram's gateway.
|
||||
//
|
||||
// Before the first task completes, nothing is known: Snapshot's ok return
|
||||
// reports that honestly rather than a caller guessing a value.
|
||||
type AgentStatus struct {
|
||||
mu sync.RWMutex
|
||||
version string
|
||||
installType string
|
||||
observed bool
|
||||
}
|
||||
|
||||
// Observe records the version/install-type an archie-agent worker reported
|
||||
// in a task response. Called for every completed task, so the value always
|
||||
// reflects whichever agent build most recently ran -- the same one that is
|
||||
// actually in service, not the version archied's own release pipeline
|
||||
// stamped for it (those two can diverge, which is exactly what this exists
|
||||
// to catch).
|
||||
func (s *AgentStatus) Observe(version, installType string) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.version = version
|
||||
s.installType = installType
|
||||
s.observed = true
|
||||
}
|
||||
|
||||
// Snapshot returns the most recently observed version/install-type, and
|
||||
// whether any task has reported one yet.
|
||||
func (s *AgentStatus) Snapshot() (version, installType string, ok bool) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
return s.version, s.installType, s.observed
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package daemon
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestAgentStatusSnapshotBeforeObserveIsUnverified(t *testing.T) {
|
||||
var s AgentStatus
|
||||
if version, installType, ok := s.Snapshot(); ok || version != "" || installType != "" {
|
||||
t.Fatalf("Snapshot() = (%q, %q, %v), want zero values and ok=false before any Observe", version, installType, ok)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAgentStatusObserveThenSnapshot(t *testing.T) {
|
||||
var s AgentStatus
|
||||
s.Observe("1.9.11", "container")
|
||||
|
||||
version, installType, ok := s.Snapshot()
|
||||
if !ok || version != "1.9.11" || installType != "container" {
|
||||
t.Fatalf("Snapshot() = (%q, %q, %v), want (\"1.9.11\", \"container\", true)", version, installType, ok)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAgentStatusObserveOverwritesPrevious(t *testing.T) {
|
||||
var s AgentStatus
|
||||
s.Observe("1.9.10", "container")
|
||||
s.Observe("1.9.11", "container")
|
||||
|
||||
version, _, _ := s.Snapshot()
|
||||
if version != "1.9.11" {
|
||||
t.Fatalf("Snapshot() version = %q, want the most recent observation %q", version, "1.9.11")
|
||||
}
|
||||
}
|
||||
@@ -97,6 +97,14 @@ type Daemon struct {
|
||||
// the host with the daemon's own authority.
|
||||
SandboxRequired bool
|
||||
|
||||
// AgentStatus records the version/install-type the most recently
|
||||
// completed archie-agent task response reported about itself, for
|
||||
// releaseupdate.Report.Verify to check an update claim against. Nil
|
||||
// disables observation -- runViaAgent skips the Observe call rather
|
||||
// than reporting the agent as unverifiable via a zero value, which
|
||||
// would read as a checked "unknown" rather than "never wired".
|
||||
AgentStatus *AgentStatus
|
||||
|
||||
// ContainerPool manages Docker container lifecycle. Nil when [containers]
|
||||
// is not configured. When non-nil, every task gets a fresh container.
|
||||
ContainerPool *container.Pool
|
||||
@@ -1085,6 +1093,14 @@ func (d *Daemon) runViaAgent(ctx context.Context, task *store.Task, repo config.
|
||||
return
|
||||
}
|
||||
|
||||
// Reached only past every failure return above, so this is always a
|
||||
// fully decoded, error-free response -- a version is never recorded off
|
||||
// a request that never reached an agent, or a response describing a
|
||||
// parked/errored task.
|
||||
if d.AgentStatus != nil && resp.AgentVersion != "" {
|
||||
d.AgentStatus.Observe(resp.AgentVersion, resp.AgentInstallType)
|
||||
}
|
||||
|
||||
d.Log.Info("taskrun complete", "task", task.ID, "status", resp.Status)
|
||||
}
|
||||
|
||||
|
||||
@@ -645,6 +645,79 @@ func TestRunViaAgentSendsExpectedRequest(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunViaAgentObservesReportedAgentVersion is the regression proof for
|
||||
// daemon.AgentStatus actually being fed: every archie-agent process is
|
||||
// task-scoped and ephemeral, so a completed taskrun.Response is the only
|
||||
// channel the daemon has to learn what build is really running (see
|
||||
// cmd/archied's daemonRunningVersions, which reads this back out via
|
||||
// Snapshot for releaseupdate.Report.Verify).
|
||||
func TestRunViaAgentObservesReportedAgentVersion(t *testing.T) {
|
||||
d, s, busClient := daemonWithNATS(t)
|
||||
var status AgentStatus
|
||||
d.AgentStatus = &status
|
||||
ctx := context.Background()
|
||||
|
||||
if _, err := s.EnqueueIssue(ctx, "acme", "widget", 4, "t", "b", "", ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
task, err := s.ClaimNext(ctx)
|
||||
if err != nil || task == nil {
|
||||
t.Fatalf("claim: (%v, %v)", task, err)
|
||||
}
|
||||
|
||||
sub, err := mustCoreConn(t, busClient).Subscribe(agentnats.SubjectForTask(task.ID), func(msg *natsio.Msg) {
|
||||
data, _ := json.Marshal(taskrun.Response{
|
||||
Status: store.StatusPROpen, AgentVersion: "1.9.11", AgentInstallType: "container",
|
||||
})
|
||||
_ = msg.Respond(data)
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("subscribe: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = sub.Unsubscribe() })
|
||||
|
||||
d.runViaAgent(ctx, task, config.Repo{Owner: "acme", Name: "widget"})
|
||||
|
||||
version, installType, ok := status.Snapshot()
|
||||
if !ok || version != "1.9.11" || installType != "container" {
|
||||
t.Fatalf("Snapshot() = (%q, %q, %v), want (\"1.9.11\", \"container\", true)", version, installType, ok)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunViaAgentIgnoresEmptyAgentVersion: a worker older than this field,
|
||||
// or one whose build was never stamped, reports an empty AgentVersion.
|
||||
// Recording that as observed would read as a confirmed "unknown" version
|
||||
// rather than as nothing having been reported yet.
|
||||
func TestRunViaAgentIgnoresEmptyAgentVersion(t *testing.T) {
|
||||
d, s, busClient := daemonWithNATS(t)
|
||||
var status AgentStatus
|
||||
d.AgentStatus = &status
|
||||
ctx := context.Background()
|
||||
|
||||
if _, err := s.EnqueueIssue(ctx, "acme", "widget", 5, "t", "b", "", ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
task, err := s.ClaimNext(ctx)
|
||||
if err != nil || task == nil {
|
||||
t.Fatalf("claim: (%v, %v)", task, err)
|
||||
}
|
||||
|
||||
sub, err := mustCoreConn(t, busClient).Subscribe(agentnats.SubjectForTask(task.ID), func(msg *natsio.Msg) {
|
||||
data, _ := json.Marshal(taskrun.Response{Status: store.StatusPROpen})
|
||||
_ = msg.Respond(data)
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("subscribe: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = sub.Unsubscribe() })
|
||||
|
||||
d.runViaAgent(ctx, task, config.Repo{Owner: "acme", Name: "widget"})
|
||||
|
||||
if _, _, ok := status.Snapshot(); ok {
|
||||
t.Fatal("Snapshot() reports observed after an empty AgentVersion response")
|
||||
}
|
||||
}
|
||||
|
||||
// testForge implements forge.Forge for daemon tests.
|
||||
type testForge struct {
|
||||
comments []commentCall
|
||||
|
||||
@@ -58,4 +58,13 @@ type Response struct {
|
||||
Task *store.Task `json:"task"`
|
||||
Status string `json:"status"`
|
||||
Error string `json:"error,omitempty"`
|
||||
// AgentVersion and AgentInstallType report the build of archie-agent
|
||||
// that actually ran this task -- self-reported by the worker, not the
|
||||
// version archied's release pipeline expected to be running. Every
|
||||
// archie-agent process is task-scoped and ephemeral, so this response
|
||||
// is the only channel the daemon has to observe what is really in
|
||||
// service; see daemon.AgentStatus. Empty AgentVersion means an
|
||||
// unstamped build ("dev") or a worker older than this field.
|
||||
AgentVersion string `json:"agent_version,omitempty"`
|
||||
AgentInstallType string `json:"agent_install_type,omitempty"`
|
||||
}
|
||||
Reference in new issue
Block a user