feat: wire the stateless coordinator into the agent tick
Construct the fleet API client with existing fleet credentials and the trusted-store client on ordinary boxes. Renew held coordination from the existing fast tick and release it under a bounded shutdown context.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+73
−2
internal/host/agent.go+19 −0internal/host/replicacoordinator.go+10 −1internal/host/replicacoordinator_test.go+44 −1
Intent
Human
<task-notification> <task-id>bt3a5q0iz</task-id> <tool-use-id>toolu_01Soe5AGNtkT35nmSo9ZzQcV</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/412bbef6-9468-4e8d-b9fe-5343bad21de8/tasks/bt3a5q0iz.output</output-file> <status>completed</status> <summary>Background command "Wait for Task 2e implementer" completed (exit code 0)</summary> </task-notification>
diff --git a/internal/host/agent.go b/internal/host/agent.go| index aae636c84..ee996fdaa 100644 |
| --- a/internal/host/agent.go |
| +++ b/internal/host/agent.go |
| @@ -415,6 +415,7 @@ func runAgent(args []string, buildClaims ClaimFactory) { |
| var act edge.Activator |
| var svc *activator.Service |
| + var coordinator *activator.CoordinationClient |
| // egressStatus is root's GET /egress for the status tick (#328): set |
| // with the control channel below, and left nil on a box without one. |
| var egressStatus egressStatusReader |
| @@ -589,6 +590,16 @@ func runAgent(args []string, buildClaims ClaimFactory) { |
| // (managed fleets 1b, fleetWakeFor): a fleet box checks the record, |
| // a box on a fleet with an invalid identity refuses every labelled |
| // wake, and only a box not on a fleet skips the check. |
| + if identErr == nil { |
| + coordinator = newCoordinatorClient(nil, os.Getenv("CARLOS_TUNNEL_URL"), identity.Fleet, identity.Label, os.Getenv("CARLOS_TUNNEL_TOKEN")) |
| + } else if errors.Is(identErr, errNotFleetBox) { |
| + if store, err := openDeployStore(context.Background()); err == nil { |
| + coordinator = newCoordinatorClient(store, "", "", metricsBoxLabel(os.Getenv, readMachineID()), "") |
| + } |
| + } |
| + if coordinator != nil { |
| + svc.Coordinator = coordinator |
| + } |
| svc.Fleet = fleetWakeFor(os.Getenv, identity, identErr, fleetStore) |
| if svc.Fleet.Mode == activator.FleetWakeUnknown { |
| log.Printf("agent: ERROR this box is on fleet %q but its fleet identity is invalid: %v. every instance assigned to a fleet box is refused until it is fixed and the agent restarted", svc.Fleet.Fleet, svc.Fleet.Err) |
| @@ -1131,6 +1142,7 @@ func runAgent(args []string, buildClaims ClaimFactory) { |
| if svc != nil { |
| ctx := shutdownCtx |
| svc.Shutdown(ctx) |
| + closeReplicaCoordinator(coordinator) |
| // Then the interactive sessions' worker (rootfs apps Plan 6 Task |
| // 12): every session, whatever its stage, ends and is joined under |
| // the same budget. Shutdown has settled their units, so each sends |
| @@ -2422,6 +2434,7 @@ const ( |
| // fastDeps is one fastConvergeLoop's state and seams — a struct so the tick |
| // is testable without goroutines, env vars, or sleeps. |
| type fastDeps struct { |
| + coordinator interface{ Tick(context.Context) } |
| reg *registry.Registry |
| store release.Store |
| pub ed25519.PublicKey |
| @@ -2905,6 +2918,9 @@ func logSlow(what string, start time.Time, detail string) { |
| // proof wait the sweep left standing; other staged members clear their |
| // backoff state. |
| func fastTick(ctx context.Context, d *fastDeps, now time.Time) { |
| + if d.coordinator != nil { |
| + d.coordinator.Tick(ctx) |
| + } |
| // The index is observed BEFORE any pass this tick runs, so a pass covers |
| // at least what was observed and indexActed can take it afterwards. |
| obs, indexOK := d.observeIndex(ctx) |
| @@ -3216,6 +3232,9 @@ func fastConvergeLoop(reg *registry.Registry, svc *activator.Service, sd *schedu |
| // sweep instead of the sweep re-cycling the service every tick. The |
| // remaining fields are filled in right below. |
| d := &fastDeps{reg: reg, store: store, pub: pub, optDir: optDir, serialNotify: serialNotify} |
| + if svc != nil { |
| + d.coordinator, _ = svc.Coordinator.(interface{ Tick(context.Context) }) |
| + } |
| restart := d.observeRestart(makeRestartRoute(reg, systemctlTryRestart, agentCheckedUnitRestart(systemdUnits{}, optDir, os.Getenv, nil), adoptBounceFn(svc))) |
| // Built once, used by both pass shapes below — see fastPassOpts for the |
| // DO-NOT list and why the stage requester is per-loop, not per-tick. |
diff --git a/internal/host/replicacoordinator.go b/internal/host/replicacoordinator.go| index d5c6a0a35..1d3d04c33 100644 |
| --- a/internal/host/replicacoordinator.go |
| +++ b/internal/host/replicacoordinator.go |
| @@ -16,7 +16,7 @@ import ( |
| "github.com/carlosframework/platform/internal/replicas" |
| ) |
| -func newCoordinatorClient(store release.Store, consoleURL, fleet, box, token string) activator.CoordinatorClient { |
| +func newCoordinatorClient(store release.Store, consoleURL, fleet, box, token string) *activator.CoordinationClient { |
| boot := readBootID(bootIDPath) |
| if fleet != "" { |
| api := &replicaCoordinatorAPI{base: strings.TrimRight(consoleURL, "/"), fleet: fleet, box: box, token: token, client: &http.Client{Timeout: 30 * time.Second}} |
| @@ -126,3 +126,12 @@ func (a *replicaCoordinatorAPI) releaseState(ctx context.Context, id replicas.Id |
| }{id.Incarnation, change}, &reply) |
| return reply, err |
| } |
| + |
| +func closeReplicaCoordinator(client *activator.CoordinationClient) { |
| + if client == nil { |
| + return |
| + } |
| + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| + defer cancel() |
| + client.Close(ctx) |
| +} |
diff --git a/internal/host/replicacoordinator_test.go b/internal/host/replicacoordinator_test.go| index c001b05fc..8469f3a46 100644 |
| --- a/internal/host/replicacoordinator_test.go |
| +++ b/internal/host/replicacoordinator_test.go |
| @@ -72,7 +72,7 @@ func TestReplicaReleaseFleetAPI(t *testing.T) { |
| t.Cleanup(ts.Close) |
| denied := &replicaDeniedStore{} |
| client := newCoordinatorClient(denied, ts.URL, "home", "b1", token) |
| - defer client.(*activator.CoordinationClient).Close(ctx) |
| + defer client.Close(ctx) |
| inc, err := provision.Incarnation(rec.Account, rec.App, rec.Host, rec.CreatedAt) |
| if err != nil { |
| t.Fatal(err) |
| @@ -157,3 +157,46 @@ func TestCoordinatorTrustedStoreAdapter(t *testing.T) { |
| t.Fatal("replay rewrote released evidence") |
| } |
| } |
| + |
| +type coordinatorTickProbe struct{ calls int } |
| + |
| +func (p *coordinatorTickProbe) Tick(context.Context) { p.calls++ } |
| + |
| +func TestCoordinatorAgentTick(t *testing.T) { |
| + f := newFastFixture(t) |
| + d := f.deps(nil) |
| + p := &coordinatorTickProbe{} |
| + d.coordinator = p |
| + for i := 0; i < 2; i++ { |
| + fastTick(context.Background(), d, time.Now()) |
| + } |
| + if p.calls != 2 { |
| + t.Fatalf("coordinator ticks = %d", p.calls) |
| + } |
| +} |
| + |
| +func TestCoordinatorAgentShutdown(t *testing.T) { |
| + ctx := context.Background() |
| + var released bool |
| + client := activator.NewCoordinatorClient("box", "boot", func(ctx context.Context, _ replicas.Identity, c replicas.CoordinatorChange) (replicas.CoordinatorReply, error) { |
| + if c.Op == "release" { |
| + if _, ok := ctx.Deadline(); !ok { |
| + t.Fatal("shutdown release has no deadline") |
| + } |
| + released = true |
| + } |
| + return replicas.CoordinatorReply{Lease: replicas.Coordinator{Term: "term", Box: c.Box, BootID: c.BootID}, RemainingMS: replicas.CoordinatorTTL.Milliseconds()}, nil |
| + }, nil) |
| + id := replicas.Identity{Account: "acme", App: "shop", Host: "shop.example.com", Incarnation: "lifetime"} |
| + a, err := client.Acquire(ctx, id) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + closeReplicaCoordinator(client) |
| + if !released || a.Err() == nil { |
| + t.Fatal("shutdown did not release and invalidate authority") |
| + } |
| + if _, err := client.Acquire(ctx, id); err == nil { |
| + t.Fatal("closed coordinator acquired authority") |
| + } |
| +} |