carlos / platform Public

sizes: prove an empty size column is byte-identical to main

Task 12 (#329): golden literals from main's own code at 953b9fda (ruling
C9), confirmed unchanged at origin/main 6bead9d5. Root: sliceTier, the
generation drop-in, /start run and sidecar, the generation start on both
of genView's paths, and /parent for each tier and overlap sum. Agent: no
size or readsize call across a wake, a swap and ten sweeps; spawns at the
Tier/TierOf seams' tier; a children-to-default swap's full order log.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev a3236d5d1837eb82836dcd6fabd7d50fc53c1256 parent fdacfa4
2 files changed, +442 −0
  • internal/activator/size_identical_test.go +245 −0
  • internal/instancectl/size_identical_test.go +197 −0
diff --git a/internal/activator/size_identical_test.go b/internal/activator/size_identical_test.go
new file mode 100644
index 000000000..6ec3600b3
--- /dev/null
+++ b/internal/activator/size_identical_test.go
@@ -0,0 +1,245 @@
+package activator
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "slices"
+ "sort"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/carlosframework/platform/internal/cgroup"
+ "github.com/carlosframework/platform/internal/registry"
+)
+
+// The byte-identical proof for the agent (#329, plan 3 Task 12): with no
+// size column the activator makes no live size call, spawns every child at
+// the tier main's Tier and TierOf seams give, and swaps exactly as main
+// does (spec §5.4 rev 8: no exception). Every literal below was produced by
+// main's own code at 953b9fda (the plan's merge base, ruling C9) through
+// the same fixtures, and was confirmed unchanged at origin/main 6bead9d5.
+
+// A wake, a swap and ten sweeps (with the backstop due and egress-only
+// policy notes among them) make no size or readsize call, and every spawn
+// runs at the seam's tier.
+func TestNoSizeColumnSendsNoLiveCall(t *testing.T) {
+ t.Parallel()
+ t.Run("the seams", func(t *testing.T) {
+ t.Parallel()
+ // With an empty column the live tier is Tier's answer and a
+ // generation's is TierOf's, verbatim, whatever they are: main's
+ // ExecBackend.tier and GenTier (953b9fda). 3 GiB is no row of the
+ // size table and still passes through.
+ for _, c := range []struct{ live, build cgroup.Limit }{
+ {cgroup.TierDefault, cgroup.TierChildren},
+ {cgroup.TierChildren, cgroup.TierDefault},
+ {cgroup.Limit{MemoryMax: 3 << 30}, cgroup.Limit{MemoryMax: 3 << 30}},
+ } {
+ eb := &ExecBackend{
+ Tier: func(registry.Route) cgroup.Limit { return c.live },
+ TierOf: func(registry.Route, string) cgroup.Limit { return c.build },
+ }
+ rt := registry.Route{Host: "shop-a.example.com"}
+ if got := eb.tier(rt); got != c.live {
+ t.Errorf("tier = %+v, want Tier's %+v", got, c.live)
+ }
+ for name, gb := range map[string]interface {
+ GenTier(registry.Route, string) cgroup.Limit
+ }{"exec": eb, "split": &SplitBackend{Exec: eb}} {
+ if got := gb.GenTier(rt, "v2"); got != c.build {
+ t.Errorf("%s: GenTier(v2) = %+v, want TierOf's %+v", name, got, c.build)
+ }
+ if got := gb.GenTier(rt, ""); got != c.live {
+ t.Errorf("%s: GenTier with no build = %+v, want Tier's %+v", name, got, c.live)
+ }
+ }
+ }
+ // No seam at all: main's default tier (953b9fda: 536870912).
+ var bare ExecBackend
+ if got := bare.tier(registry.Route{Host: "shop-a.example.com"}); got.MemoryMax != 536870912 {
+ t.Errorf("nil Tier: %+v, want main's 536870912", got)
+ }
+ })
+ t.Run("a spawn's cgroup", func(t *testing.T) {
+ t.Parallel()
+ // The files spawnInCgroup writes for the Tier seam's tier, as main
+ // writes them (953b9fda; cgroup.procs holds pids and is left out).
+ for _, c := range []struct {
+ tier cgroup.Limit
+ want []string
+ }{
+ {cgroup.TierDefault, []string{
+ "carlos-exec@shop-a.example.com/memory.high=483183819",
+ "carlos-exec@shop-a.example.com/memory.max=536870912",
+ "carlos-exec@shop-a.example.com/memory.oom.group=1",
+ "carlos-exec@shop-a.example.com/memory.swap.max=536870912",
+ "cgroup.subtree_control=+cpu +memory +io +pids",
+ }},
+ {cgroup.TierChildren, []string{
+ "carlos-exec@shop-a.example.com/memory.high=966367638",
+ "carlos-exec@shop-a.example.com/memory.max=1073741824",
+ "carlos-exec@shop-a.example.com/memory.oom.group=1",
+ "carlos-exec@shop-a.example.com/memory.swap.max=1073741824",
+ "cgroup.subtree_control=+cpu +memory +io +pids",
+ }},
+ } {
+ if got := spawnCgroupFiles(t, c.tier); !slices.Equal(got, c.want) {
+ t.Errorf("spawn at %d wrote\n%s\nmain (953b9fda) writes\n%s", c.tier.MemoryMax, strings.Join(got, "\n"), strings.Join(c.want, "\n"))
+ }
+ }
+ })
+ t.Run("a wake, a swap and ten sweeps", func(t *testing.T) {
+ t.Parallel()
+ // The fixture's host is woken (arrangeAwake) and its route, in the
+ // entry and in the local registry, has no size.
+ f := newSizeFixture(t, "", 0)
+ f.b.tiers = map[string]cgroup.Limit{"v1": cgroup.TierChildren, "v2": cgroup.TierDefault}
+ sweeps := 0
+ sweep := func() { t.Helper(); f.sweep(t); sweeps++ }
+ sweep()
+ sweep()
+ f.a.NoteSize(f.host) // an egress-only policy converge
+ sweep()
+ res := f.a.Swap(context.Background(), f.attempt(ModeOverlap), f.newest)
+ if res.Outcome != SwapDone {
+ t.Fatalf("Swap = %+v", res)
+ }
+ f.routes.mu.Lock()
+ rt := f.routes.rows[f.host]
+ f.routes.mu.Unlock()
+ seam := f.b.GenTier(rt, "v2")
+ if g, _ := f.b.lastGen(); res.Gen.Tier != seam || g.Tier != seam || seam.MemoryMax != 536870912 {
+ t.Fatalf("successor: result tier %+v, spawned at %+v; want the seam's %+v (main: 536870912)", res.Gen.Tier, g.Tier, seam)
+ }
+ sweep()
+ f.clk.advance(sizeBackstop)
+ sweep()
+ f.a.NoteSize(f.host)
+ sweep()
+ // The host sleeps on its idle timeout and wakes again.
+ f.clk.advance(2 * time.Hour)
+ f.a.sweepOnce()
+ sweeps++
+ waitFor(t, "the host asleep", func() bool { return f.a.entryCount() == 0 })
+ arrangeAwake(t, f.a, rt)
+ if got := f.b.lastStartedRoute(); got.Size != "" {
+ t.Fatalf("the wake spawned a route with size %q", got.Size)
+ }
+ sweep()
+ f.clk.advance(sizeBackstop)
+ sweep()
+ f.a.NoteSize(f.host)
+ sweep()
+ if sweeps != 10 {
+ t.Fatalf("ran %d sweeps, want 10", sweeps)
+ }
+ if ev := f.sizeEvents(); len(ev) != 0 {
+ t.Fatalf("size calls %v with no size column", ev)
+ }
+ })
+}
+
+// spawnCgroupFiles spawns /bin/true through spawnInCgroup at tier, on a
+// fake cgroup root, and lists every file written with its content, but
+// cgroup.procs, which holds pids.
+func spawnCgroupFiles(t *testing.T, tier cgroup.Limit) []string {
+ t.Helper()
+ root := t.TempDir()
+ for _, f := range []string{"cgroup.procs", "cgroup.subtree_control"} {
+ if err := os.WriteFile(filepath.Join(root, f), nil, 0o644); err != nil {
+ t.Fatal(err)
+ }
+ }
+ m, err := cgroup.SetupAt(root, t.Logf)
+ if err != nil {
+ t.Fatal(err)
+ }
+ b := ExecBackend{Cgroups: m, Tier: func(registry.Route) cgroup.Limit { return tier }}
+ cmd, _, err := b.spawnInCgroup(registry.Route{Host: "shop-a.example.com"}, func() *exec.Cmd { return exec.Command("/bin/true") })
+ if err != nil {
+ t.Fatal(err)
+ }
+ _ = cmd.Wait()
+ var files []string
+ err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
+ if err != nil || info.IsDir() || info.Name() == "cgroup.procs" {
+ return err
+ }
+ raw, err := os.ReadFile(path)
+ if err != nil {
+ return err
+ }
+ rel, _ := filepath.Rel(root, path)
+ files = append(files, rel+"="+string(raw))
+ return nil
+ })
+ if err != nil {
+ t.Fatal(err)
+ }
+ sort.Strings(files)
+ return files
+}
+
+// A swap from a build that declares children (v1, 1 GiB) to one that does
+// not (v2, 512 MiB), with no size: the raise is 1 GiB + 512 MiB, the
+// successor starts at 512 MiB and the parent is restored to 512 MiB, or,
+// when the successor fails, to the incumbent's 1 GiB. Exactly main's order
+// log (953b9fda), every event.
+func TestNoSizeColumnSwapIsMains(t *testing.T) {
+ t.Parallel()
+ t.Run("switched", func(t *testing.T) {
+ t.Parallel()
+ f := newSwapFixture(t)
+ f.b.tiers = map[string]cgroup.Limit{"v1": cgroup.TierChildren, "v2": cgroup.TierDefault}
+ res := f.a.Swap(context.Background(), f.attempt(ModeOverlap), f.newest)
+ if res.Outcome != SwapDone || res.Gen.Tier.MemoryMax != 536870912 {
+ t.Fatalf("Swap = %+v; main (953b9fda): done at 536870912", res)
+ }
+ // 953b9fda: the fixture's wake, then the swap's events.
+ want := []string{
+ "restore",
+ "start:instance",
+ "raise:1073741824+536870912",
+ "startGen:" + f.staged,
+ fmt.Sprintf("switch:%d", res.Gen.N),
+ "stop:instance",
+ "restore:536870912",
+ }
+ if got := f.b.events(); !slices.Equal(got, want) {
+ t.Fatalf("events\n%s\nmain (953b9fda)\n%s", strings.Join(got, "\n"), strings.Join(want, "\n"))
+ }
+ if g, _ := f.b.lastGen(); g.Tier.MemoryMax != 536870912 {
+ t.Fatalf("the successor spawned at %+v; main (953b9fda): 536870912", g.Tier)
+ }
+ })
+ t.Run("failed", func(t *testing.T) {
+ t.Parallel()
+ f := newSwapFixture(t)
+ f.b.tiers = map[string]cgroup.Limit{"v1": cgroup.TierChildren, "v2": cgroup.TierDefault}
+ f.a.genHealth = func(context.Context, Gen) error { return errors.New("unhealthy: HTTP 503") }
+ if res := f.a.Swap(context.Background(), f.attempt(ModeOverlap), f.newest); res.Outcome != SwapFailed {
+ t.Fatalf("Swap = %+v; main (953b9fda): failed", res)
+ }
+ g, _ := f.b.lastGen()
+ // 953b9fda: the fixture's wake, then the failed swap's events.
+ want := []string{
+ "restore",
+ "start:instance",
+ "raise:1073741824+536870912",
+ "startGen:" + f.staged,
+ fmt.Sprintf("stop:gen:%d", g.N),
+ "restore:1073741824",
+ }
+ if got := f.b.events(); !slices.Equal(got, want) {
+ t.Fatalf("events\n%s\nmain (953b9fda)\n%s", strings.Join(got, "\n"), strings.Join(want, "\n"))
+ }
+ if g.Tier.MemoryMax != 536870912 {
+ t.Fatalf("the successor spawned at %+v; main (953b9fda): 536870912", g.Tier)
+ }
+ })
+}
diff --git a/internal/instancectl/size_identical_test.go b/internal/instancectl/size_identical_test.go
new file mode 100644
index 000000000..3a51da034
--- /dev/null
+++ b/internal/instancectl/size_identical_test.go
@@ -0,0 +1,197 @@
+package instancectl
+
+import (
+ "context"
+ "os"
+ "path/filepath"
+ "strconv"
+ "testing"
+
+ "github.com/carlosframework/platform/internal/cgroup"
+)
+
+// The byte-identical proof for root (#329, plan 3 Task 12): with no size
+// column every start, slice and parent line root writes is main's. Every
+// literal below was produced by main's own code at 953b9fda (the plan's
+// merge base, ruling C9), driven through the same fakes as here, and was
+// confirmed unchanged at origin/main 6bead9d5. Main's startGen (236753a7,
+// genView) skips the slice's set-property when it already carries the
+// tier, so the generation start is pinned on both of its paths: a cold
+// slice, and a slice already at the tier. The egress asserter is nil here,
+// as in main's own golden tests; plan 2 proves its part separately.
+
+const identicalHost = "shop-a.example.com"
+
+// identicalTier is one tier's main output (953b9fda).
+type identicalTier struct {
+ name string
+ tier cgroup.Limit
+ // memory is the tier's memory as main writes it: MemoryMax, MemoryHigh
+ // and MemorySwapMax in the show answer the fakes give back.
+ max, high string
+ // cpuShown is CPUQuotaPerSecUSec as systemd shows main's CPUQuota.
+ cpuShown string
+ // sliceTier is sliceTier's one set-property line (953b9fda).
+ sliceTier string
+ // dropIn is GenDropIn(identicalHost, 3, tier) (953b9fda).
+ dropIn string
+}
+
+var identicalTiers = []identicalTier{
+ {
+ name: "TierDefault", tier: cgroup.TierDefault,
+ max: "536870912", high: "483183819", cpuShown: "1s",
+ // 953b9fda: sliceTier(ctx, systemctl, "shop-a.example.com", cgroup.TierDefault)
+ sliceTier: `set-property --runtime carlos-instances-shop\x2da.example.com.slice MemoryMax=536870912 MemoryHigh=483183819 MemorySwapMax=536870912 CPUWeight=100 CPUQuota=100% IOWeight=100`,
+ // 953b9fda: GenDropIn("shop-a.example.com", 3, cgroup.TierDefault)
+ dropIn: `# Written by carlos instance-control for one generation; removed when it stops (zero-downtime plan 2).
+[Service]
+Slice=carlos-instances-shop\x2da.example.com.slice
+BindPaths=/data/instances/shop-a.example.com /run/carlos/instances/shop-a.example.com
+ReadWritePaths=/data/instances/shop-a.example.com /run/carlos/instances/shop-a.example.com
+WorkingDirectory=/data/instances/shop-a.example.com
+BindReadOnlyPaths=/run/carlos-control/exe/shop-a.example.com@3/app:/opt/carlos/app /run/carlos-control/exe/shop-a.example.com@3/live:/opt/carlos/live
+StandardOutput=file:/run/carlos/logs/shop-a.example.com@3.run.sock
+MemoryMax=536870912
+MemoryHigh=483183819
+MemorySwapMax=536870912
+`,
+ },
+ {
+ name: "TierChildren", tier: cgroup.TierChildren,
+ max: "1073741824", high: "966367638", cpuShown: "2s",
+ // 953b9fda: sliceTier(ctx, systemctl, "shop-a.example.com", cgroup.TierChildren)
+ sliceTier: `set-property --runtime carlos-instances-shop\x2da.example.com.slice MemoryMax=1073741824 MemoryHigh=966367638 MemorySwapMax=1073741824 CPUWeight=100 CPUQuota=200% IOWeight=100`,
+ // 953b9fda: GenDropIn("shop-a.example.com", 3, cgroup.TierChildren)
+ dropIn: `# Written by carlos instance-control for one generation; removed when it stops (zero-downtime plan 2).
+[Service]
+Slice=carlos-instances-shop\x2da.example.com.slice
+BindPaths=/data/instances/shop-a.example.com /run/carlos/instances/shop-a.example.com
+ReadWritePaths=/data/instances/shop-a.example.com /run/carlos/instances/shop-a.example.com
+WorkingDirectory=/data/instances/shop-a.example.com
+BindReadOnlyPaths=/run/carlos-control/exe/shop-a.example.com@3/app:/opt/carlos/app /run/carlos-control/exe/shop-a.example.com@3/live:/opt/carlos/live
+StandardOutput=file:/run/carlos/logs/shop-a.example.com@3.run.sock
+MemoryMax=1073741824
+MemoryHigh=966367638
+MemorySwapMax=1073741824
+`,
+ },
+}
+
+// The unit and slice names main uses for identicalHost (953b9fda).
+const (
+ identicalRunUnit = `carlos-instance@shop\x2da.example.com.service`
+ identicalSidecarUnit = `carlos-instance-sidecar@shop\x2da.example.com.service`
+ identicalGenUnit = `carlos-instance-gen@shop\x2da.example.com:3.service`
+ identicalSlice = `carlos-instances-shop\x2da.example.com.slice`
+ // identicalGenShow is main's startGen look at systemd, genView (236753a7,
+ // as at 953b9fda).
+ identicalGenShow = "show -p Slice,NeedDaemonReload,MemoryMax,MemoryHigh,MemorySwapMax,CPUWeight,CPUQuotaPerSecUSec,IOWeight " + identicalGenUnit + " " + identicalSlice
+)
+
+func TestNoSizeColumnIsByteIdenticalToMain(t *testing.T) {
+ for _, tc := range identicalTiers {
+ t.Run(tc.name, func(t *testing.T) {
+ memoryMax := strconv.FormatInt(tc.tier.MemoryMax, 10)
+
+ // sliceTier's set-property line (953b9fda).
+ ctl := &fakeCtl{}
+ if err := sliceTier(context.Background(), ctl.run, identicalHost, tc.tier); err != nil {
+ t.Fatal(err)
+ }
+ if got := ctl.joined(); got != tc.sliceTier {
+ t.Errorf("sliceTier =\n%s\nmain (953b9fda) =\n%s", got, tc.sliceTier)
+ }
+
+ // The generation drop-in (953b9fda).
+ if got := GenDropIn(identicalHost, 3, tc.tier); got != tc.dropIn {
+ t.Errorf("GenDropIn =\n%s\nmain (953b9fda) =\n%s", got, tc.dropIn)
+ }
+
+ // A run and a sidecar start: main's /start (953b9fda) is the tier,
+ // the start and the invocation read, nothing else.
+ for role, unit := range map[string]string{"run": identicalRunUnit, "sidecar": identicalSidecarUnit} {
+ ctl := &fakeCtl{answers: map[string]string{"show -p InvocationID --value": "abc123\n"}}
+ _, c := newServer(t, ctl)
+ resp := post(t, c, "/start?host="+identicalHost+"&role="+role+"&memory_max="+memoryMax)
+ resp.Body.Close()
+ if resp.StatusCode != 200 {
+ t.Fatalf("%s start: status %d, main (953b9fda) answers 200", role, resp.StatusCode)
+ }
+ // 953b9fda: the systemctl calls of POST /start?role=<role>.
+ want := tc.sliceTier + "\n" +
+ "start " + unit + "\n" +
+ "show -p InvocationID --value " + unit
+ if got := ctl.joined(); got != want {
+ t.Errorf("%s start calls =\n%s\nmain (953b9fda) =\n%s", role, got, want)
+ }
+ }
+
+ // A generation start on each of main's two paths (236753a7 genView,
+ // as at 953b9fda): the slice is cold and gets the tier, or it
+ // already carries the tier and set-property is skipped.
+ unitShow := "Slice=" + identicalSlice + "\nNeedDaemonReload=no\nMemoryMax=" + tc.max + "\nMemoryHigh=" + tc.high + "\nMemorySwapMax=" + tc.max + "\n"
+ sliceAtTier := "\nSlice=carlos.slice\nNeedDaemonReload=no\nMemoryMax=" + tc.max + "\nMemoryHigh=" + tc.high + "\nMemorySwapMax=" + tc.max +
+ "\nCPUWeight=100\nCPUQuotaPerSecUSec=" + tc.cpuShown + "\nIOWeight=100\n"
+ for _, path := range []struct {
+ name, show, want string
+ }{
+ // 953b9fda: StartGen on a slice that does not carry the tier.
+ {"cold slice", unitShow, identicalGenShow + "\n" +
+ tc.sliceTier + "\n" +
+ "start " + identicalGenUnit + "\n" +
+ "show -p InvocationID --value " + identicalGenUnit},
+ // 953b9fda: StartGen on a slice already at the tier.
+ {"slice at the tier", unitShow + sliceAtTier, identicalGenShow + "\n" +
+ "start " + identicalGenUnit + "\n" +
+ "show -p InvocationID --value " + identicalGenUnit},
+ } {
+ ctl := genCtl()
+ ctl.answers["show -p Slice,NeedDaemonReload,MemoryMax,MemoryHigh,MemorySwapMax"] = path.show
+ _, sock, dropins := genServer(t, ctl)
+ inv, err := NewClient(sock).StartGen(context.Background(), identicalHost, 3, tc.tier.MemoryMax)
+ if err != nil || inv != "abc123" {
+ t.Fatalf("%s: StartGen = %q, %v; main (953b9fda) answers abc123", path.name, inv, err)
+ }
+ if got := ctl.joined(); got != path.want {
+ t.Errorf("%s: generation start calls =\n%s\nmain (953b9fda) =\n%s", path.name, got, path.want)
+ }
+ raw, err := os.ReadFile(filepath.Join(dropins, identicalGenUnit+".d", DropInName))
+ if err != nil || string(raw) != tc.dropIn {
+ t.Errorf("%s: drop-in on disk = %q, %v; main (953b9fda) writes\n%s", path.name, raw, err, tc.dropIn)
+ }
+ }
+ })
+ }
+ t.Run("parent", identicalParent)
+}
+
+// identicalParent pins /parent's line for each tier and each overlap sum
+// main's swap asks for (953b9fda): one tier, two of the same, and children
+// plus default.
+func identicalParent(t *testing.T) {
+ for _, tc := range []struct {
+ memoryMax int64
+ want string
+ }{
+ // 953b9fda: POST /parent?memory_max=536870912 (TierDefault)
+ {536870912, `set-property --runtime carlos-instances-shop\x2da.example.com.slice MemoryMax=536870912 MemoryHigh=483183819 MemorySwapMax=536870912`},
+ // 953b9fda: POST /parent?memory_max=1073741824 (TierChildren, or TierDefault twice)
+ {1073741824, `set-property --runtime carlos-instances-shop\x2da.example.com.slice MemoryMax=1073741824 MemoryHigh=966367638 MemorySwapMax=1073741824`},
+ // 953b9fda: POST /parent?memory_max=1610612736 (TierChildren plus TierDefault)
+ {1610612736, `set-property --runtime carlos-instances-shop\x2da.example.com.slice MemoryMax=1610612736 MemoryHigh=1449551457 MemorySwapMax=1610612736`},
+ // 953b9fda: POST /parent?memory_max=2147483648 (TierChildren twice)
+ {2147483648, `set-property --runtime carlos-instances-shop\x2da.example.com.slice MemoryMax=2147483648 MemoryHigh=1932735276 MemorySwapMax=2147483648`},
+ } {
+ ctl := genCtl()
+ _, c := newServer(t, ctl)
+ resp := post(t, c, "/parent?host="+identicalHost+"&memory_max="+strconv.FormatInt(tc.memoryMax, 10))
+ resp.Body.Close()
+ if resp.StatusCode != 204 {
+ t.Fatalf("/parent %d: status %d, main (953b9fda) answers 204", tc.memoryMax, resp.StatusCode)
+ }
+ if got := ctl.joined(); got != tc.want {
+ t.Errorf("/parent %d =\n%s\nmain (953b9fda) =\n%s", tc.memoryMax, got, tc.want)
+ }
+ }
+}