test: recover born-HA sidecar timers on the root runner
Create a stamped standalone sidecar through provisioning and run its signed worker through the real nested unit. Verify live wake-state sync, release, departure import into a separate registry and unattended due delivery after restore. Use the production record reader in the root harness. Validated by root run 20261105T104231-1f79e9; its instance was terminated.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+241
−10
internal/host/hadepartures_root_linux_test.go+18 −0internal/host/haplacement_root_linux_test.go+7 −10internal/host/sidecar_departure_root_linux_test.go+216 −0
CI passed — run details
Intent
Attributed to this commit: 31.2 g CO₂e (3,678,329 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on region-ha-p1b3b-s-build, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.
diff --git a/internal/host/hadepartures_root_linux_test.go b/internal/host/hadepartures_root_linux_test.go| index e56c76608..f0f8fa66e 100644 |
| --- a/internal/host/hadepartures_root_linux_test.go |
| +++ b/internal/host/hadepartures_root_linux_test.go |
| @@ -63,6 +63,7 @@ func departureRootIdentities(t *testing.T, store release.Store) { |
| } |
| type departureRootState struct { |
| + Sidecar *registry.Route |
| Timers map[string][]registry.Timer |
| Owned map[string]bool |
| Pending int |
| @@ -148,6 +149,18 @@ func departureRootWire(t *testing.T, ref string, svc *activator.Service, eb *act |
| return false |
| } |
| switch strings.TrimPrefix(r.URL.Path, "/departure/") { |
| + case "sidecar-start": |
| + row, e := reg.Get(departureSidecarHost) |
| + if fail(e) { |
| + return |
| + } |
| + if row == nil { |
| + http.Error(w, "sidecar absent", 500) |
| + return |
| + } |
| + if fail(svc.WakeWorker(*row)) { |
| + return |
| + } |
| case "register": |
| due, e := strconv.ParseInt(r.URL.Query().Get("due"), 10, 64) |
| if fail(e) { |
| @@ -229,6 +242,11 @@ func departureRootWire(t *testing.T, ref string, svc *activator.Service, eb *act |
| } |
| state.Owned[host] = svc.Owns(host) |
| } |
| + state.Sidecar, err = reg.Get(departureSidecarHost) |
| + if fail(err) { |
| + return |
| + } |
| + state.Owned[departureSidecarHost] = svc.Owns(departureSidecarHost) |
| summary, e := reg.DepartureSummary(r.Context()) |
| if fail(e) { |
| return |
diff --git a/internal/host/haplacement_root_linux_test.go b/internal/host/haplacement_root_linux_test.go| index 87929d138..d6ede07c9 100644 |
| --- a/internal/host/haplacement_root_linux_test.go |
| +++ b/internal/host/haplacement_root_linux_test.go |
| @@ -139,16 +139,7 @@ func TestColdPlacementRootAgent(t *testing.T) { |
| reg, err := registry.Open(filepath.Join(data, "registry.db")) |
| lifecycleCheck(t, "edge registry", err) |
| defer reg.Close() |
| - cfg := activator.Config{Bucket: "data", Region: "eu-west-1", Prefix: "instances", Idle: time.Hour, Record: func(ctx context.Context, rt registry.Route) (activator.RecordFacts, error) { |
| - r, _, err := provision.FindRecordByHost(ctx, store, rt.Host) |
| - if err != nil { |
| - return activator.RecordFacts{}, err |
| - } |
| - if r == nil { |
| - return activator.RecordFacts{}, os.ErrNotExist |
| - } |
| - return activator.RecordFactsOf(*r) |
| - }} |
| + cfg := activator.Config{Bucket: "data", Region: "eu-west-1", Prefix: "instances", Idle: time.Hour, Record: haRecordFacts(func(context.Context) (release.Store, error) { return store, nil }, nil)} |
| lease.SetSelf(hafresh.Self{Ref: ref, IID: "i-" + ref, Account: "111122223333", Region: "eu-west-1"}) |
| if strings.HasPrefix(os.Getenv("COLD_ROOT_MODE"), "departure-") { |
| lease.SetSelf(departureRootSelf(ref)) |
| @@ -742,6 +733,9 @@ func newColdRootHarness(t *testing.T, mode string) *coldRootHarness { |
| lifecycleCheck(t, "sibling record", err) |
| cfg := provision.ReconcileConfig{DataDir: "/data", Layout: func(context.Context, release.Store) (string, error) { return release.LayoutModeNested, nil }} |
| lifecycleCheck(t, "provision real HA record", provision.Reconcile(t.Context(), store, reg, cfg)) |
| + if mode == "departure-sidecar" { |
| + departureSidecarCreate(t, store, reg, cfg) |
| + } |
| manifest := release.Manifest{App: "lifecycle", Version: "v1", Kind: "binary", Artifacts: map[string]release.Artifact{release.AdoptPlatform(): {}}} |
| lifecycleCheck(t, "ship", release.Ship(t.Context(), store, "bab", manifest, map[string][]byte{release.AdoptPlatform(): image})) |
| @@ -751,6 +745,9 @@ func newColdRootHarness(t *testing.T, mode string) *coldRootHarness { |
| lifecycleCheck(t, "stage signed app", err) |
| lifecycleCheck(t, "point live", release.PointLive("/opt/carlos", lifecycleHost, "bab", "v1", "lifecycle")) |
| lifecycleCheck(t, "point sibling live", release.PointLive("/opt/carlos", lifecycleSibling, "bab", "v1", "lifecycle")) |
| + if mode == "departure-sidecar" { |
| + lifecycleCheck(t, "point sidecar live", release.PointLive("/opt/carlos", departureSidecarHost, "bab", "v1", "lifecycle")) |
| + } |
| for _, path := range []string{"/data", "/run/carlos", lifecycleHome} { |
| lifecycleCheck(t, "fixture service owner", filepath.Walk(path, func(path string, _ os.FileInfo, err error) error { |
| if err != nil { |
diff --git a/internal/host/sidecar_departure_root_linux_test.go b/internal/host/sidecar_departure_root_linux_test.go| new file mode 100644 |
| index 000000000..f48e7cb8d |
| --- /dev/null |
| +++ b/internal/host/sidecar_departure_root_linux_test.go |
| @@ -0,0 +1,216 @@ |
| +//go:build linux && rootrunner |
| + |
| +package host |
| + |
| +import ( |
| + "context" |
| + "database/sql" |
| + "encoding/json" |
| + "fmt" |
| + "net" |
| + "net/http" |
| + "os" |
| + "path/filepath" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/hafresh" |
| + "github.com/carlosframework/platform/internal/hostfiles" |
| + "github.com/carlosframework/platform/internal/lease" |
| + "github.com/carlosframework/platform/internal/provision" |
| + "github.com/carlosframework/platform/internal/registry" |
| + "github.com/carlosframework/platform/internal/release" |
| +) |
| + |
| +const departureSidecarHost = "bot.lifecycle.bab.sidecar.internal" |
| + |
| +type departureSidecarDelivery struct { |
| + Starts int |
| + Due, Delivered int64 |
| +} |
| + |
| +func departureSidecarReceipt() string { |
| + return filepath.Join(provision.InstanceDir("/data", departureSidecarHost), departureSidecarHost, "delivery.json") |
| +} |
| + |
| +func init() { |
| + if len(os.Args) != 2 || os.Args[1] != "b6-sidecar" { |
| + return |
| + } |
| + if os.Getenv("CARLOS_SIDECAR_HOST") != departureSidecarHost { |
| + panic("wrong sidecar identity") |
| + } |
| + // This executable is the signed tenant payload, launched by the real worker unit. |
| + db, err := sql.Open("sqlite", provision.NestedDBPath("/data", departureSidecarHost)) |
| + if err != nil { |
| + panic(err) |
| + } |
| + if _, err = db.Exec("PRAGMA journal_mode=WAL; CREATE TABLE IF NOT EXISTS delivery (at INTEGER)"); err != nil { |
| + panic(err) |
| + } |
| + path := departureSidecarReceipt() |
| + var receipt departureSidecarDelivery |
| + raw, err := os.ReadFile(path) |
| + if err == nil { |
| + if err = json.Unmarshal(raw, &receipt); err != nil { |
| + panic(err) |
| + } |
| + } else if !os.IsNotExist(err) { |
| + panic(err) |
| + } |
| + receipt.Starts++ |
| + if receipt.Starts == 1 { |
| + receipt.Due = time.Now().Add(90 * time.Second).Unix() |
| + } else { |
| + if time.Now().Unix() < receipt.Due { |
| + panic("sidecar delivered before due") |
| + } |
| + receipt.Delivered = time.Now().Unix() |
| + } |
| + if err = os.MkdirAll(filepath.Dir(path), 0700); err != nil { |
| + panic(err) |
| + } |
| + raw, err = json.Marshal(receipt) |
| + if err != nil { |
| + panic(err) |
| + } |
| + if err = os.WriteFile(path, raw, 0600); err != nil { |
| + panic(err) |
| + } |
| + tr := &http.Transport{DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) { |
| + return (&net.Dialer{}).DialContext(ctx, "unix", os.Getenv("CARLOS_AGENT_SOCKET")) |
| + }} |
| + client := &http.Client{Transport: tr, Timeout: 5 * time.Second} |
| + post := func(verb, body string) { |
| + req, err := http.NewRequest(http.MethodPost, "http://carlos/v1/"+verb, strings.NewReader(body)) |
| + if err != nil { |
| + panic(err) |
| + } |
| + req.Header.Set("Carlos-Sidecar-Host", departureSidecarHost) |
| + req.Header.Set("Authorization", "Bearer "+os.Getenv("CARLOS_SIDECAR_TOKEN")) |
| + resp, err := client.Do(req) |
| + if err != nil { |
| + panic(err) |
| + } |
| + resp.Body.Close() |
| + if resp.StatusCode != http.StatusNoContent { |
| + panic(fmt.Sprintf("worker %s: %d", verb, resp.StatusCode)) |
| + } |
| + } |
| + post("ready", `{"version":"v1"}`) |
| + if receipt.Starts == 1 { |
| + post("next-due", fmt.Sprintf(`{"at":%d}`, receipt.Due)) |
| + } |
| + for { |
| + time.Sleep(time.Hour) |
| + } |
| +} |
| + |
| +func departureSidecarCreate(t *testing.T, store release.Store, reg *registry.Registry, cfg provision.ReconcileConfig) { |
| + t.Helper() |
| + _, err := store.Put(t.Context(), "accounts/bab/account.json", []byte(`{"id":1,"sqid":"bab","name":"Shop","created_at":"2026-11-05T00:00:00Z"}`), "") |
| + lifecycleCheck(t, "sidecar account", err) |
| + lifecycleCheck(t, "sidecar manifest", provision.PutSidecarManifest(t.Context(), store, "bab", "lifecycle", provision.SidecarManifest{Schema: 1, App: "lifecycle", Args: []string{"b6-sidecar"}})) |
| + rec := provision.SidecarRecord{Schema: 1, Name: "bot", Host: departureSidecarHost, Account: "bab", App: "lifecycle", Channel: "stable", HA: true, CreatedAt: time.Now().UTC()} |
| + lifecycleCheck(t, "create born HA sidecar record", provision.CreateSidecarRecord(t.Context(), store, rec)) |
| + stored, err := provision.GetSidecarRecord(t.Context(), store, rec.Account, rec.App, rec.Name) |
| + lifecycleCheck(t, "read created sidecar", err) |
| + if stored == nil || !stored.HA || stored.Region != "" { |
| + t.Fatal("sidecar birth stamp or home scope", stored) |
| + } |
| + lifecycleCheck(t, "reconcile born HA sidecar", provision.ReconcileSidecars(t.Context(), store, reg, cfg)) |
| + row, err := reg.Get(departureSidecarHost) |
| + lifecycleCheck(t, "sidecar route", err) |
| + if row == nil || !row.HA || !row.IsSidecar() || !row.Provisioned || row.Socket != "" || row.Addr != "" { |
| + t.Fatal("sidecar route not derived from created record", row) |
| + } |
| +} |
| + |
| +func TestSidecarHADepartureRootLifecycle(t *testing.T) { |
| + if os.Geteuid() != 0 { |
| + t.Fatal("rootrunner requires real root") |
| + } |
| + f := newColdRootHarness(t, "departure-sidecar") |
| + state := departureRootCall(t, "/data", "sidecar-start") |
| + if !state.Owned[departureSidecarHost] { |
| + t.Fatal("real source worker not acquired", state) |
| + } |
| + files, err := lease.NewFilesStore(t.Context(), "data", "eu-west-1") |
| + lifecycleCheck(t, "sidecar files store", err) |
| + prefix := "instances/" + departureSidecarHost + "/files/0/" |
| + var receipt departureSidecarDelivery |
| + var wake hostfiles.WakeState |
| + synced := false |
| + deadline := time.Now().Add(10 * time.Second) |
| + for time.Now().Before(deadline) { |
| + raw, err := os.ReadFile(departureSidecarReceipt()) |
| + if err == nil { |
| + lifecycleCheck(t, "worker receipt", json.Unmarshal(raw, &receipt)) |
| + } |
| + m, found, err := hostfiles.Load(t.Context(), files, prefix) |
| + lifecycleCheck(t, "sidecar manifest", err) |
| + if found { |
| + var ok bool |
| + wake, ok, err = hostfiles.LoadWake(t.Context(), files, prefix, m, nil) |
| + lifecycleCheck(t, "sidecar wake", err) |
| + if ok && receipt.Starts == 1 && wake.NextDue == receipt.Due && receipt.Due > time.Now().Unix() { |
| + synced = true |
| + break |
| + } |
| + } |
| + time.Sleep(100 * time.Millisecond) |
| + } |
| + if !synced { |
| + t.Fatal("real worker's next_due did not sync while awake", receipt, wake) |
| + } |
| + departureRootCall(t, "/data", "shutdown") |
| + // As in T, the stopped service process stays only as the trusted control |
| + // transport relay for the independent survivor on this one-kernel runner. |
| + v, err := lease.ReadLock(t.Context(), "data", "instances/"+departureSidecarHost, "eu-west-1") |
| + lifecycleCheck(t, "sidecar sleep marker", err) |
| + if v.Lock.Release == nil || v.Lock.Release.Box != "leo" || v.Lock.Release.Lifetime == "" { |
| + t.Fatal("missing source release marker", v) |
| + } |
| + // Erase the local receipt: the survivor must restore it from published files. |
| + lifecycleCheck(t, "remove source receipt", os.Remove(departureSidecarReceipt())) |
| + lifecycleCheck(t, "confirmed fixture termination", os.WriteFile(filepath.Join(lifecycleHome, "ec2-state"), []byte("terminated"), 0644)) |
| + lifecycleCheck(t, "retire fixed source publication", f.store.Delete(t.Context(), edgesFileKey("leo"))) |
| + departureRootCall(t, f.ingress, "event?ref=leo") |
| + for i := 0; i < 5; i++ { |
| + state = departureRootCall(t, f.ingress, "scan") |
| + if state.Pending == 0 { |
| + break |
| + } |
| + } |
| + if state.Pending != 0 || state.Imported != 1 || state.Owned[departureSidecarHost] { |
| + t.Fatal("sidecar import/finalization", state) |
| + } |
| + if state.Sidecar == nil || state.Sidecar.NextDue != receipt.Due { |
| + t.Fatal("next_due not imported into survivor registry", state) |
| + } |
| + if _, err = os.Stat(departureSidecarReceipt()); !os.IsNotExist(err) { |
| + t.Fatal("adoption restored or started sidecar before due", err) |
| + } |
| + deadline = time.Now().Add(2 * time.Minute) |
| + for time.Now().Before(deadline) { |
| + raw, err := os.ReadFile(departureSidecarReceipt()) |
| + var delivered departureSidecarDelivery |
| + if err == nil && json.Unmarshal(raw, &delivered) == nil && delivered.Starts == 2 && delivered.Delivered >= receipt.Due { |
| + state = departureRootCall(t, f.ingress, "state") |
| + if state.Owned[departureSidecarHost] && state.Sidecar != nil && state.Sidecar.NextDue == 0 { |
| + v, err = lease.ReadLock(t.Context(), "data", "instances/"+departureSidecarHost, "eu-west-1") |
| + lifecycleCheck(t, "survivor writer", err) |
| + owner, valid := hafresh.ParseOwner(v.Lock.Owner) |
| + if !valid || owner.Ref != "sophie" || owner.IID != departureRootSelf("sophie").IID { |
| + t.Fatal("survivor does not own lease", v) |
| + } |
| + t.Logf("sidecar timer delivered unattended: due=%d delivered=%d starts=%d imported=%d", receipt.Due, delivered.Delivered, delivered.Starts, state.Imported) |
| + departureRootCall(t, f.ingress, "shutdown") |
| + return |
| + } |
| + } |
| + time.Sleep(200 * time.Millisecond) |
| + } |
| + t.Fatal("survivor did not restore and deliver the sidecar timer without a visitor", state) |
| +} |