Merge remote-tracking branch 'origin/main' into changelog-catchup-v0-40-1
12 files changed,
+309
−11
docs/superpowers/notes/census-proposal-instance-control-memory-1291.md+23 −0docs/superpowers/reviews/CODEX-FINDINGS-instance-control-memory-1291-R1.md+38 −0docs/superpowers/reviews/CODEX-FINDINGS-instance-control-memory-1291-R2.md+33 −0docs/superpowers/reviews/CODEX-REVIEW-BRIEF-instance-control-memory-1291.md+13 −0internal/host/claimunit.go+27 −4internal/host/claimunit_test.go+148 −1internal/host/plan9fix1_journal_linux_test.go+4 −3internal/host/systemunits.go+10 −1internal/host/units/carlos-instance-control.service+3 −1internal/host/units_isolation_test.go+1 −1internal/provision/testdata/plan14-root-reads-chain.tsv+1 −0internal/provision/testdata/plan14-root-reads-instance-control-memory-1291.tsv+8 −0
CI passed — run details
diff --git a/docs/superpowers/notes/census-proposal-instance-control-memory-1291.md b/docs/superpowers/notes/census-proposal-instance-control-memory-1291.md| new file mode 100644 |
| index 000000000..ece30269c |
| --- /dev/null |
| +++ b/docs/superpowers/notes/census-proposal-instance-control-memory-1291.md |
| @@ -0,0 +1,23 @@ |
| +# Census approval: instance-control-memory-1291 |
| + |
| +Ruling `census-ruling-instance-control-memory-1291`. The independent reviewer approves or rejects every change below, 0 with a new disposition; nothing goes to the census owner (Paul, #280, 2026-10-26). |
| + |
| +| unit | change | read sites | what changed | |
| +|---|---|---|---| |
| +| `internal/host/claimunit.go` `claimUnitContent` | body | unchanged (1) | The demo build worker and claims memory rewrites go through replaceClaimControlMemory, which matches this build line (claimControlMemoryMax, MemoryMax=256M) or v0.40.1 MemoryMax=64M (#1291, R1): system apply renders the candidate template with the installed renderer, so both versions must render. Build worker and claims renderings are unchanged from main on either template; off and demo-box renderings carry the template own value. No read, path, input or permission change. | |
| +| `internal/host/systemunits.go` `ensureSystemUnitsWithGuard` | body | unchanged (7) | The adopt pass controlChanged expression moved verbatim into claimControlStale so tests can drive it (#1291 R1): same inputs, same decision, same rewrite and restart. No read, path, input or permission change. | |
| +| `internal/host/units/carlos-instance-control.service` `external` | body | unchanged (1) | MemoryMax=64M raised to 256M with a two-line comment (#1291): registry-read children share this cgroup and four overlapping starts OOM-killed control at 64M. A ceiling only; no ExecStart, path, environment, capability or protection change. No new root read. | |
| + |
| +Envelopes re-pinned: `internal/host`, `internal/host/units`. |
| + |
| +Review: a model from the other family than the author reads the branch's diff against main on the amadan.net remote (`origin` or `amadan`, whichever points at amadan.net) and gives a verdict for every unit and every re-pinned envelope above. For a unit, compare the change with its recorded disposition and basis below; for an envelope, read every changed function in the package and what it reaches. Reject if the change lets root open, run, query or follow anything it did not before from a path or input a tenant, an app or a box agent can create, write or name; if it weakens a check, refusal, validation, argument or environment the basis relies on; or if the diff cannot settle either. Otherwise approve. Record each verdict on #280 with the tip. A rejected unit goes back to the author to correct; nothing escalates to the census owner. |
| + |
| +Recorded dispositions, which each change must still satisfy: |
| + |
| +- `internal/host/claimunit.go` `claimUnitContent`: authority, task E1,E3. Basis: rev3=PROPOSED-p3;identity=root claim/adoption controller uid 0;basis=adds write permission for exactly the public retry directory only when the root settings reader has verified a complete census and prepared that directory;rev3=census-ruling-agent-start-demo-1;basis=Demo-only ReadWritePaths for instance data/runtime, staged files and root release lock; ordinary rendering unchanged.;rev3=census-ruling-agent-start-demo-1-followups;basis=In demo box mode, declare StateDirectory=carlos-release/private with mode 0700 so systemd creates root-owned release state before namespace setup and keeps it writable. Existing ReadWritePaths and ordinary/claims rendering stay unchanged. No new read site or caller-controlled path.;rev3=census-ruling-agent-start-demo-3;basis=Declare /var/lib/carlos-demo-snapshots as root-owned StateDirectory with mode 0700 only for configured demo boxes, including recovery with admission off. Ordinary claims units retain their original directories. The shared unit template is unchanged from main; no file read is added.;rev3=census-ruling-gvisor-demos;basis=Mark only claim/demo renderings explicitly; common host.env remains loaded. Ordinary rendering masks CARLOS_HOST_CLAIM with UnsetEnvironment, removed only for a successfully parsed enabled claim rendering. Existing template read unchanged. Task 7: render aggregate build slice through SlicePropertiesFor using the resolved host mode, with 32 MiB and 64-task gVisor overhead per slot. Existing control permissions unchanged.;rev3=census-ruling-same-box-edge-fence;basis=Root unit rendering (claims mode). The control unit's families line is now `RestrictAddressFamilies=AF_UNIX AF_NETLINK`; the claims rewrite matches that line (it matched the old one exactly, and would otherwise have silently dropped AF_INET/AF_INET6) and renders `AF_UNIX AF_NETLINK AF_INET AF_INET6`. No read added; no other directive changed. |
| +- `internal/host/systemunits.go` `ensureSystemUnitsWithGuard`: legacy, task accepted-legacy. Basis: accepted-legacy;rev3=census-ruling-region-ha-p3b;basis=Move unit-repair body here with existing lock/unit/host.env reads; under lock require operator repair permit on groups before writes and each systemctl effect, preserve root masks/drop-in and omit automatic update/recovery units. New indirect protected marker/permit reads, never agent receipt; fixed repair unchanged.;rev3=census-ruling-gvisor-demos;basis=Create runtime/rootfs directories and repair installer plus three measured workload templates without arming them. Existing unit/host.env reads unchanged.;rev3=census-ruling-gvisor-update-dirs;basis=Create the two live sandbox directories (the same fixed ensureSandboxDirectories paths) before the apply lock and the early returns on non-group boxes; group members still create them only after the maintenance permit. A creation error still ends the pass at the original point. Then call refreshControlSandboxRootfs. No new path, input or systemctl verb beyond the try-restart below.;rev3=census-ruling-demo-slice-adopt;basis=Compare the installed carlos-demo-builds.slice (fixed root unit path, embedded unit) with its host.env rendering, and ask systemd for the slice's loaded MemoryMax/TasksMax (systemctl show on the fixed name). A definite difference is a reinstall (existing write loop + daemon-reload). No live budget change, no new path, input or write target. |
| +- `internal/host/units/carlos-instance-control.service` `external`: authority, task B3,C,D,E1,J4. Basis: root-input;kept=B3,C,D,E1;scope=protected-decision-only;rev3=census-ruling-agent-start-demo-3;basis=Correct the runtime-write comment to include escaped demo snapshot .mount units alongside per-generation drop-ins; service directives are unchanged.;rev3=census-ruling-region-ha-graceful-terminate-t1;basis=Region HA T1/DR-4 final fix: retain After=nftables.service without Wants or Requires; add Restart=on-failure and RestartSec=1 so a crashed control restarts before a later edge stop can block socket activation and shutdown proofs; rewrap the header and explain this ordering. Same ExecStart and protections; no new root read, input, path, command or capability.;rev3=census-ruling-gvisor-demos;basis=Add Wants/After installer and narrowly writable rootfs parent. Keep one host.env EnvironmentFile and other hardening.;rev3=census-ruling-gvisor-update-dirs;basis=Make the existing /var/lib/carlos/oci-rootfs writable-path entry optional when absent so old updaters can start the new control unit; retain ProtectSystem=strict and the same path and environment. No new root read.;rev3=census-ruling-same-box-edge-fence;basis=RestrictAddressFamilies gains AF_NETLINK (nft talks netlink), with a comment; the group drop-in already granted it on group boxes. No capability, path, environment or ExecStart change. Without it nft fails and the whole #367 deny stays. |
| + |
| +Seal: `aa2c0729fe761d6438d2e8b9ef4ef2c1a20699f5e81dded1783c9a008dfafba9` → `70ca25987f5a18849d7ae880e30628a47475375d6dde2409a505232d78abb872`. |
| + |
| +Overlay: `internal/provision/testdata/plan14-root-reads-instance-control-memory-1291.tsv`, appended to `internal/provision/testdata/plan14-root-reads-chain.tsv`. |
diff --git a/docs/superpowers/reviews/CODEX-FINDINGS-instance-control-memory-1291-R1.md b/docs/superpowers/reviews/CODEX-FINDINGS-instance-control-memory-1291-R1.md| new file mode 100644 |
| index 000000000..cee34c99f |
| --- /dev/null |
| +++ b/docs/superpowers/reviews/CODEX-FINDINGS-instance-control-memory-1291-R1.md |
| @@ -0,0 +1,38 @@ |
| +Reviewed `origin/main...8316ed897130d4dbe601919799d755209b030a9c`, round 1 of 3. |
| + |
| +**P1 — Memory replacements silently fail across release versions.** |
| +Fault family: preserving mode-specific resource settings through upgrades and rollbacks. |
| + |
| +The new template changes the anchor to `MemoryMax=256M`, and the new renderer accepts only that anchor ([claimunit.go:31](/home/paulca/amadan.net/carlos/platform/.claude/worktrees/instance-control-memory-1291/internal/host/claimunit.go:31)). However, `system apply` runs the installed binary’s renderer against the candidate binary’s staged template: staging happens at [systemapply.go:1445](/home/paulca/amadan.net/carlos/platform/.claude/worktrees/instance-control-memory-1291/internal/host/systemapply.go:1445), rendering at line 1643. Renaming the executable does not replace the running updater. |
| + |
| +Replaying the exact replacements against both versions confirmed: |
| + |
| +| Updater → template | Claims result | Demo-worker result | |
| +|---|---|---| |
| +| Old → new | 256M; missing `GOMEMLIMIT=192MiB` | **256M instead of 1 GiB**; missing `GOMEMLIMIT=768MiB` | |
| +| New → old | **64M instead of 256M**; missing GOMEMLIMIT | **64M instead of 1 GiB**; missing GOMEMLIMIT | |
| + |
| +This affects the first upgrade and a subsequent channel rollback, potentially OOM-killing control under workloads previously supported. The new tests exercise only matching renderer/template versions ([claimunit_test.go:410](/home/paulca/amadan.net/carlos/platform/.claude/worktrees/instance-control-memory-1291/internal/host/claimunit_test.go:410)). |
| + |
| +**Fix:** support both template versions and provide a transition that already-installed updaters can handle—for example, deploy a compatible renderer before changing the template. Add mixed-version upgrade and rollback coverage. Merely accepting both anchors in this candidate does not fix the old updater installing it. |
| + |
| +No P2 or P3 findings. |
| + |
| +The other requested checks: |
| + |
| +- **Matching versions:** all four modes render correctly. Claims retain 256M/192MiB; workers retain 1 GiB/768MiB; off and demo-box modes receive 256M. |
| +- **Apply and restart:** installation performs `daemon-reload`, then restarts control before the edge ([systemapply.go:801](/home/paulca/amadan.net/carlos/platform/.claude/worktrees/instance-control-memory-1291/internal/host/systemapply.go:801)). The updater runs in a separate service. In-flight wakes can be interrupted, as already expected by this release sequence; the socket remains available. The compatibility finding above prevents unconditional approval of rollout. |
| +- **Sizing:** 256 MiB is a reasonable incident mitigation on the stated 3.9 GB box. It adds at most 192 MiB to the previous ceiling, without reserving that memory. It does not bound concurrent starts or guarantee twelve starts under every workload. Systemd defines this as an absolute usage limit: [https://www.freedesktop.org/software/systemd/man/latest/systemd.resource-control.html#MemoryMax=](https://www.freedesktop.org/software/systemd/man/latest/systemd.resource-control.html#MemoryMax=). |
| + |
| +Census verdicts concern root-read authority separately from the blocking compatibility defect: |
| + |
| +| Card entry | Verdict | |
| +|---|---| |
| +| `claimunit.go:claimUnitContent`, lines 141–199 | **APPROVE** existing authority disposition: no added reads, caller-controlled paths or expanded permissions. | |
| +| `carlos-instance-control.service:external`, lines 26–65 | **APPROVE** existing authority disposition: the approved ceiling increase preserves execution, environment sources and filesystem protections. | |
| +| `internal/host` envelope | **APPROVE**: reviewed the renderer, its callers and all changed tests; no additional authority change. | |
| +| `internal/host/units` envelope | **APPROVE**: only the control unit’s comment and memory ceiling changed. | |
| + |
| +`git diff --check` passed. Targeted Go tests could not start because Go attempted to update `go.mod` in the read-only workspace. No files changed, reviewers spawned or full suites run. |
| + |
| +**NOT READY** |
| \ No newline at end of file |
diff --git a/docs/superpowers/reviews/CODEX-FINDINGS-instance-control-memory-1291-R2.md b/docs/superpowers/reviews/CODEX-FINDINGS-instance-control-memory-1291-R2.md| new file mode 100644 |
| index 000000000..7d7eaca81 |
| --- /dev/null |
| +++ b/docs/superpowers/reviews/CODEX-FINDINGS-instance-control-memory-1291-R2.md |
| @@ -0,0 +1,33 @@ |
| +Reviewed round **2 of 3** at `c7eaa1c600b26706a206684f99e686dcbcc715ce`, including fix `dde07131`. **No new P1, P2 or P3 findings.** Round 1’s compatibility finding is resolved under the controller’s acceptance of the upgrade transient. |
| + |
| +Both directions behave as stated: |
| + |
| +| Mode | New renderer → old template | Old renderer → new template | After successful adoption | |
| +|---|---|---|---| |
| +| Claims | 256 MiB + `GOMEMLIMIT=192MiB` | 256 MiB, no GOMEMLIMIT | 256 MiB + 192 MiB | |
| +| Build worker | 1 GiB + `GOMEMLIMIT=768MiB` | 256 MiB, no GOMEMLIMIT | 1 GiB + 768 MiB | |
| +| Off / ordinary demo box | Template’s 64 MiB | 256 MiB | Already correct | |
| + |
| +The dual-anchor replacement is correct at [claimunit.go:42](internal/host/claimunit.go#L42). I also confirmed that the reconstructed legacy template matches `origin/main` exactly. |
| + |
| +**The healing path is wired, but is conditional and not immediate.** Normal, `--stage-only` and `--config-only` root adoption reach `ensureUnits()` in both claims and demo modes; `--restart-only` skips it. Claims startup opens its registry and constructs claim wiring first, so failures there prevent repair. See [agent.go:6016](internal/host/agent.go#L6016) and [agent.go:6057](internal/host/agent.go#L6057). |
| + |
| +The stale comparison uses the running binary’s embedded template and renderer. A mismatch defeats the “already armed” return, writes the rendered unit, performs `daemon-reload`, then calls `restartClaimControl`. That uses `try-restart`: running control restarts; inactive control gets the corrected settings on its next activation. See [systemunits.go:200](internal/host/systemunits.go#L200), [systemunits.go:252](internal/host/systemunits.go#L252), and [claimunit.go:224](internal/host/claimunit.go#L224). |
| + |
| +**Timing:** successful apply re-arms the timer and paths. The timer’s configured cadence is one hour plus up to ten minutes of jitter; boot delay is five minutes plus jitter. Config or staging requests can repair sooner, but the agent’s one-minute in-process adoption does not itself repair units. A pass encountering the apply lock skips repair and waits for another trigger. Thus this is an hourly backstop, not an immediate or strict deadline. Group-marked boxes additionally require a `repair` maintenance permit; the automatic-healing claim does not extend unconditionally to them. See [timer](internal/host/units/carlos-adopt.timer#L9) and [repair gates](internal/host/systemunits.go#L98). |
| + |
| +Census verdicts for changed entries: |
| + |
| +| Entry | Verdict | |
| +|---|---| |
| +| `claimUnitContent`, lines 159–217, including helper lines 42–49 | **APPROVE** existing authority disposition. Only memory substitutions change; no additional reads, paths or permissions. | |
| +| `ensureSystemUnitsWithGuard`, lines 69–305; helper lines 312–314 | **APPROVE** existing legacy disposition. The extracted predicate is identical; guards and effects remain unchanged. | |
| +| `internal/host` envelope | **APPROVE**, including both new helpers and changed tests. | |
| + |
| +The service entry and `internal/host/units` envelope are unchanged since round 1; their approvals stand. |
| + |
| +In-memory substitution checks passed for all four modes, and `git diff --check` passed for `dde07131`. The new upgrade test checks stale detection, not a complete update/restart integration. Targeted Go tests could not start because the read-only sandbox denied creation of the build directory. No files changed. |
| + |
| +No third review round is required under the stopping rule. |
| + |
| +**READY** |
| \ No newline at end of file |
diff --git a/docs/superpowers/reviews/CODEX-REVIEW-BRIEF-instance-control-memory-1291.md b/docs/superpowers/reviews/CODEX-REVIEW-BRIEF-instance-control-memory-1291.md| new file mode 100644 |
| index 000000000..db7a007e5 |
| --- /dev/null |
| +++ b/docs/superpowers/reviews/CODEX-REVIEW-BRIEF-instance-control-memory-1291.md |
| @@ -0,0 +1,13 @@ |
| +# Review brief: branch instance-control-memory-1291 (code + census), round 1 of 3 |
| + |
| +Repo carlos/platform, branch `instance-control-memory-1291`, commit 8316ed897130d4dbe601919799d755209b030a9c. Under review: `git diff origin/main...8316ed897130d4dbe601919799d755209b030a9c` and the census card docs/superpowers/notes/census-proposal-instance-control-memory-1291.md. |
| + |
| +Incident #1291 (2026-11-19): on a flagship US HA member, carlos-instance-control was OOM-killed in its own cgroup (MemoryMax=64M) while taking four hosts at once; a customer app got 6 x 503. Cause from the code: each generation start makes root registry reads, and each starts a fresh /opt/carlos/carlos __registry-child (about 10-17 MB) inside that cgroup, with no limit on how many overlap. Paul approved raising the cap; a runtime MemoryMax=256M is already set on the affected members. This branch makes 256M permanent and makes claimunit.go's two MemoryMax replacements key on one shared constant, which tests check against the shipped unit. |
| + |
| +Check: |
| +1. Do the claimunit.go replacements still produce exactly what they did before, for every mode (claims, the demo build worker's MemoryMax=1073741824 plus GOMEMLIMIT=768MiB, claim mode's GOMEMLIMIT=192MiB, off, demo box)? Can any mode now silently skip? |
| +2. Will boxes pick up the unit change through carlos system apply (unit install, daemon-reload, restartInstanceControl), with nothing else needed? Does a restart of instance control during apply interrupt anything the release path relies on? |
| +3. Is 256M sound, as a ceiling and not a reservation, on the smallest box that runs this unit (t4g.medium, 3.9 GB, no swap)? |
| +4. Census: a verdict for every unit and envelope on the card. |
| + |
| +Severity: P1 blocks merge; P2 is fixed without another round; P3 goes on a list. End with READY, READY AFTER FIXES or NOT READY. Do not edit files, spawn reviewers, or run full suites. |
diff --git a/internal/host/claimunit.go b/internal/host/claimunit.go| index 8eba28c5d..c7fde5a17 100644 |
| --- a/internal/host/claimunit.go |
| +++ b/internal/host/claimunit.go |
| @@ -25,6 +25,29 @@ const claimControlEnvironment = "EnvironmentFile=-/etc/carlos/host.env\n" |
| const claimControlMarker = "# Carlos claim/demo control rendering\n" |
| const claimControlUnset = "UnsetEnvironment=CARLOS_HOST_CLAIM\n" |
| +// claimControlMemoryMax is the shipped control unit's memory line (#1291). |
| +// The demo build worker and claim mode replace it; TestClaimUnitAnchors* |
| +// fails if the unit's line and this constant ever drift apart. |
| +const claimControlMemoryMax = "MemoryMax=256M\n" |
| + |
| +// claimControlMemoryLines are the memory lines the renderer replaces: this |
| +// build's and v0.40.1's 64M. system apply renders the candidate's staged |
| +// template with the installed binary's renderer, so a rollback hands this |
| +// renderer the 64M template. On upgrade an older updater looks only for |
| +// 64M and leaves 256M as is (claims lose GOMEMLIMIT, a build worker its |
| +// 1 GiB): transient, as the new binary's adopt pass (ensureSystemUnits) |
| +// re-renders, sees the difference and rewrites and restarts control (#1291). |
| +var claimControlMemoryLines = []string{claimControlMemoryMax, "MemoryMax=64M\n"} |
| + |
| +func replaceClaimControlMemory(content []byte, with string) []byte { |
| + for _, line := range claimControlMemoryLines { |
| + if bytes.Contains(content, []byte(line)) { |
| + return bytes.Replace(content, []byte(line), []byte(with), 1) |
| + } |
| + } |
| + return content |
| +} |
| + |
| // Older rendered units have state directories but no explicit marker. |
| func claimControlRendered(content []byte) bool { |
| lines := append([]byte("\n"), content...) |
| @@ -163,7 +186,7 @@ func claimUnitContent(name string, content []byte, cfg claimUnitSettings) []byte |
| content = bytes.Replace(content, []byte("[Service]\n"), []byte("[Service]\nReadWritePaths=/data /run/carlos/instances /opt/carlos/live /opt/carlos/versions /var/lib/carlos-release/private\n"), 1) |
| if cfg.demo.BuildWorker { |
| content = bytes.Replace(content, []byte("[Service]\n"), []byte("[Service]\nStateDirectory=carlos-demo-builds\nStateDirectoryMode=0700\nReadWritePaths=/var/lib/carlos-demo-builds\n"), 1) |
| - content = bytes.Replace(content, []byte("MemoryMax=64M\n"), []byte("MemoryMax=1073741824\nEnvironment=GOMEMLIMIT=768MiB\n"), 1) |
| + content = replaceClaimControlMemory(content, "MemoryMax=1073741824\nEnvironment=GOMEMLIMIT=768MiB\n") |
| } |
| return content |
| } |
| @@ -189,9 +212,9 @@ func claimUnitContent(name string, content []byte, cfg claimUnitSettings) []byte |
| content = bytes.Replace(content, []byte("RestrictAddressFamilies=AF_UNIX AF_NETLINK\n"), []byte("RestrictAddressFamilies=AF_UNIX AF_NETLINK AF_INET AF_INET6\n"), 1) |
| // Claim mode adds the root journal service, the offline legacy migration |
| // (up to 16 MiB of decoded, validated and re-marshalled rows) and the AWS |
| - // fence pass to this process. 64M leaves no headroom over the Go runtime |
| - // and SQLite caches; GOMEMLIMIT makes the collector work before the cap. |
| - return bytes.Replace(content, []byte("MemoryMax=64M\n"), []byte("MemoryMax=256M\nEnvironment=GOMEMLIMIT=192MiB\n"), 1) |
| + // fence pass to this process. GOMEMLIMIT makes the collector work before |
| + // the 256M cap. |
| + return replaceClaimControlMemory(content, claimControlMemoryMax+"Environment=GOMEMLIMIT=192MiB\n") |
| } |
| func claimControlChanged(before, after []byte) bool { |
diff --git a/internal/host/claimunit_test.go b/internal/host/claimunit_test.go| index f03ad4e91..bfabb17a6 100644 |
| --- a/internal/host/claimunit_test.go |
| +++ b/internal/host/claimunit_test.go |
| @@ -294,7 +294,7 @@ func TestClaimBadUnitSettingsStillRepairSystemUnits(t *testing.T) { |
| } |
| func TestPlan14RootRetryUnitDirectoryScope(t *testing.T) { |
| - raw := []byte("[Service]\nReadWritePaths=/run/carlos-control /run/systemd/system\nRestrictAddressFamilies=AF_UNIX AF_NETLINK\nMemoryMax=64M\n") |
| + raw := []byte("[Service]\nReadWritePaths=/run/carlos-control /run/systemd/system\nRestrictAddressFamilies=AF_UNIX AF_NETLINK\n" + claimControlMemoryMax) |
| legacy := claimUnitContent(claimControlUnit, raw, claimUnitSettings{enabled: true, data: "/data", opt: "/opt/carlos"}) |
| if bytes.Contains(legacy, []byte(rootRetryProjection)) { |
| t.Fatal("no-census unit received E3 write access") |
| @@ -370,3 +370,150 @@ func TestClaimUnitSettingsSandboxOnlyForBuildWorker(t *testing.T) { |
| }) |
| } |
| } |
| + |
| +// #1291: registry-read children share the control unit's cgroup, so the |
| +// shipped unit's ceiling is 256M, not 64M. |
| +func TestClaimUnitShippedControlMemoryMax(t *testing.T) { |
| + raw, err := unitsFS.ReadFile("units/" + claimControlUnit) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var got []string |
| + for _, line := range strings.Split(string(raw), "\n") { |
| + if strings.HasPrefix(line, "MemoryMax=") { |
| + got = append(got, line) |
| + } |
| + } |
| + if !reflect.DeepEqual(got, []string{"MemoryMax=256M"}) { |
| + t.Fatalf("control unit memory lines = %q, want exactly MemoryMax=256M", got) |
| + } |
| +} |
| + |
| +// Every line claimUnitContent keys a replacement on must be in the shipped |
| +// unit exactly once: bytes.Replace skips a missing line silently, which would |
| +// drop a build worker's 1 GiB cap or claim mode's write paths unnoticed. |
| +func TestClaimUnitAnchorsMatchShippedUnit(t *testing.T) { |
| + raw, err := unitsFS.ReadFile("units/" + claimControlUnit) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, anchor := range []string{"[Service]\n", claimControlUnset, "ReadWritePaths=/run/carlos-control /run/systemd/system\n", "RestrictAddressFamilies=AF_UNIX AF_NETLINK\n", claimControlMemoryMax} { |
| + if n := bytes.Count(raw, []byte("\n"+anchor)); n != 1 { |
| + t.Errorf("shipped control unit has %d copies of %q, want 1", n, anchor) |
| + } |
| + } |
| +} |
| + |
| +// Each memory replacement must land on the shipped unit, not just on a |
| +// fixture: a demo build worker keeps its 1 GiB cap and GOMEMLIMIT, and claim |
| +// mode keeps its GOMEMLIMIT. |
| +func TestClaimUnitMemoryReplacementsApplyToShippedUnit(t *testing.T) { |
| + raw, err := unitsFS.ReadFile("units/" + claimControlUnit) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + worker := claimUnitSettings{demo: demo.Config{Account: "shop", Fleet: "demo", Domain: "example.com", BuildWorker: true, BuildConcurrency: 1}} |
| + box := claimUnitSettings{demo: demo.Config{Account: "shop", Fleet: "demo", Domain: "example.com"}} |
| + claims := claimUnitSettings{enabled: true, data: "/data", opt: "/opt/carlos"} |
| + for _, tc := range []struct { |
| + name string |
| + cfg claimUnitSettings |
| + want []string |
| + }{ |
| + {"off", claimUnitSettings{}, []string{"MemoryMax=256M"}}, |
| + {"demo box", box, []string{"MemoryMax=256M"}}, |
| + {"build worker", worker, []string{"MemoryMax=1073741824", "Environment=GOMEMLIMIT=768MiB"}}, |
| + {"claims", claims, []string{"MemoryMax=256M", "Environment=GOMEMLIMIT=192MiB"}}, |
| + } { |
| + var lines []string |
| + for _, line := range strings.Split(string(claimUnitContent(claimControlUnit, raw, tc.cfg)), "\n") { |
| + if strings.HasPrefix(line, "MemoryMax=") || strings.HasPrefix(line, "Environment=GOMEMLIMIT=") { |
| + lines = append(lines, line) |
| + } |
| + } |
| + if !reflect.DeepEqual(lines, tc.want) { |
| + t.Errorf("%s: memory lines = %q, want %q", tc.name, lines, tc.want) |
| + } |
| + } |
| + got := string(claimUnitContent(claimControlUnit, raw, claims)) |
| + for _, want := range []string{"\nRestrictAddressFamilies=AF_UNIX AF_NETLINK AF_INET AF_INET6\n", "\nReadWritePaths=/run/carlos-control /run/carlos /var/lib/carlos-claims "} { |
| + if !strings.Contains(got, want) { |
| + t.Errorf("claim mode lacks %q", want) |
| + } |
| + } |
| +} |
| + |
| +// claimControlMemoryComment is the comment #1291 added above the memory |
| +// line; it and 64M are the only differences from v0.40.1's control unit. |
| +const claimControlMemoryComment = "# Registry-read children share this cgroup, about 10-17 MB each, so 256M\n# covers roughly twelve concurrent starts (#1291). A ceiling, not a reservation.\n" |
| + |
| +// legacyControlTemplate returns v0.40.1's control unit (origin/main 40d7a9cf), |
| +// rebuilt from the shipped one by undoing exactly the #1291 change. |
| +func legacyControlTemplate(t *testing.T) (shipped, legacy []byte) { |
| + t.Helper() |
| + shipped, err := unitsFS.ReadFile("units/" + claimControlUnit) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + current := claimControlMemoryComment + "MemoryMax=256M\n" |
| + if bytes.Count(shipped, []byte(current)) != 1 { |
| + t.Fatal("shipped control unit no longer carries the #1291 memory block; update this fixture") |
| + } |
| + return shipped, bytes.Replace(shipped, []byte(current), []byte("MemoryMax=64M\n"), 1) |
| +} |
| + |
| +func claimUnitMemoryModes() map[string]claimUnitSettings { |
| + return map[string]claimUnitSettings{ |
| + "off": {}, |
| + "demo box": {demo: demo.Config{Account: "shop", Fleet: "demo", Domain: "example.com"}}, |
| + "build worker": {demo: demo.Config{Account: "shop", Fleet: "demo", Domain: "example.com", BuildWorker: true, BuildConcurrency: 1}}, |
| + "claims": {enabled: true, data: "/data", opt: "/opt/carlos"}, |
| + } |
| +} |
| + |
| +// Rollback direction (#1291 R1): system apply renders the candidate's staged |
| +// template with the installed binary's renderer, so this renderer can meet |
| +// v0.40.1's 64M template. Claims and the build worker must render exactly |
| +// what they do from this build's template; off and demo box keep the old |
| +// template's own 64M. |
| +func TestClaimUnitRendererAcceptsLegacyMemoryTemplate(t *testing.T) { |
| + shipped, legacy := legacyControlTemplate(t) |
| + for name, cfg := range claimUnitMemoryModes() { |
| + want := strings.Replace(string(claimUnitContent(claimControlUnit, shipped, cfg)), claimControlMemoryComment, "", 1) |
| + if !cfg.enabled && !cfg.demo.BuildWorker { |
| + want = strings.Replace(want, "\nMemoryMax=256M\n", "\nMemoryMax=64M\n", 1) |
| + } |
| + if got := string(claimUnitContent(claimControlUnit, legacy, cfg)); got != want { |
| + t.Errorf("%s: legacy template rendered\n%s\nwant\n%s", name, got, want) |
| + } |
| + } |
| +} |
| + |
| +// Upgrade direction (#1291 R1): a v0.40.1 updater renders this build's |
| +// template with its own renderer, which looks only for 64M. Claims and the |
| +// build worker come out wrong for a while; the new binary's adopt pass must |
| +// then see the installed unit as stale so it rewrites and restarts control. |
| +func TestClaimUnitLegacyRendererUpgradeHealsAtAdopt(t *testing.T) { |
| + shipped, _ := legacyControlTemplate(t) |
| + lines := claimControlMemoryLines |
| + t.Cleanup(func() { claimControlMemoryLines = lines }) |
| + transient := map[string][]byte{} |
| + claimControlMemoryLines = []string{"MemoryMax=64M\n"} // v0.40.1's single anchor |
| + for name, cfg := range claimUnitMemoryModes() { |
| + transient[name] = claimUnitContent(claimControlUnit, shipped, cfg) |
| + } |
| + claimControlMemoryLines = lines |
| + for name, cfg := range claimUnitMemoryModes() { |
| + wrong := cfg.enabled || cfg.demo.BuildWorker |
| + got := string(transient[name]) |
| + if wrong && (strings.Contains(got, "GOMEMLIMIT=") || !strings.Contains(got, "\nMemoryMax=256M\n")) { |
| + t.Errorf("%s: legacy renderer did not leave the 256M line unreplaced:\n%s", name, got) |
| + } |
| + if stale := claimControlStale(transient[name], nil, shipped, cfg, nil); stale != wrong { |
| + t.Errorf("%s: adopt pass stale = %v, want %v", name, stale, wrong) |
| + } |
| + if claimControlStale(claimUnitContent(claimControlUnit, shipped, cfg), nil, shipped, cfg, nil) { |
| + t.Errorf("%s: a current render is stale", name) |
| + } |
| + } |
| +} |
diff --git a/internal/host/plan9fix1_journal_linux_test.go b/internal/host/plan9fix1_journal_linux_test.go| index 47829fb72..f6b38efcf 100644 |
| --- a/internal/host/plan9fix1_journal_linux_test.go |
| +++ b/internal/host/plan9fix1_journal_linux_test.go |
| @@ -244,8 +244,9 @@ func TestPlan9Fix1QuiesceChecksEdgeBeforeMasking(t *testing.T) { |
| } |
| } |
| -// M6: the root unit gets memory for the journal service, migration and fence |
| -// pass only when the claim flag renders it; flag-off bytes are unchanged. |
| +// M6: the root unit gets a GOMEMLIMIT under its 256M cap (#1291) for the |
| +// journal service, migration and fence pass only when the claim flag renders |
| +// it; flag-off bytes are unchanged. |
| func TestPlan9Fix1ClaimControlUnitMemory(t *testing.T) { |
| raw, err := os.ReadFile("units/carlos-instance-control.service") |
| if err != nil { |
| @@ -255,7 +256,7 @@ func TestPlan9Fix1ClaimControlUnitMemory(t *testing.T) { |
| t.Fatal("flag-off unit changed") |
| } |
| got := string(claimUnitContent(claimControlUnit, raw, claimUnitSettings{enabled: true, data: "/data", opt: "/opt/carlos"})) |
| - if strings.Contains(got, "MemoryMax=64M") || !strings.Contains(got, "MemoryMax=256M") || !strings.Contains(got, "GOMEMLIMIT=") { |
| + if strings.Count(got, "\nMemoryMax=") != 1 || !strings.Contains(got, "\nMemoryMax=256M\nEnvironment=GOMEMLIMIT=192MiB\n") { |
| t.Fatalf("flag-on root unit memory not raised:\n%s", got) |
| } |
| } |
diff --git a/internal/host/systemunits.go b/internal/host/systemunits.go| index 8974d711f..de81c39b0 100644 |
| --- a/internal/host/systemunits.go |
| +++ b/internal/host/systemunits.go |
| @@ -207,7 +207,7 @@ func ensureSystemUnitsWithGuard(etcDir, lockPath string, systemctl, probe func(a |
| logf("adopt: instance-control unit: %v", err) |
| return |
| } |
| - controlChanged := settingsErr == nil && (claimSettings.enabled || claimSettings.demo.BoxMode() || claimControlRendered(installed)) && (err != nil || !bytes.Equal(installed, claimUnitContent(claimControlUnit, control, claimSettings))) |
| + controlChanged := claimControlStale(installed, err, control, claimSettings, settingsErr) |
| // The demo build budget slice is rendered from host.env too: the build |
| // concurrency and the effective exec sandbox size it. A host.env change |
| // alone (a box switched to gVisor) touches no other unit, so without this |
| @@ -304,6 +304,15 @@ func ensureSystemUnitsWithGuard(etcDir, lockPath string, systemctl, probe func(a |
| logf("adopt: self-update units installed and armed") |
| } |
| +// claimControlStale reports whether the adopt pass must rewrite the |
| +// installed control unit: it is claim- or demo-rendered (or should be) and |
| +// differs from what this binary renders from its own template, or is |
| +// missing. This is what heals an older updater's render of a newer |
| +// template (#1291). |
| +func claimControlStale(installed []byte, installedErr error, control []byte, settings claimUnitSettings, settingsErr error) bool { |
| + return settingsErr == nil && (settings.enabled || settings.demo.BoxMode() || claimControlRendered(installed)) && (installedErr != nil || !bytes.Equal(installed, claimUnitContent(claimControlUnit, control, settings))) |
| +} |
| + |
| // parseIsEnabled maps `systemctl is-enabled`'s vocabulary onto "armed for |
| // the next boot, or not", as probes.go's parseIsActive maps is-active's onto |
| // "running now". enabled and enabled-runtime are the wants-symlink states; |
diff --git a/internal/host/units/carlos-instance-control.service b/internal/host/units/carlos-instance-control.service| index e5bb95044..39ff48642 100644 |
| --- a/internal/host/units/carlos-instance-control.service |
| +++ b/internal/host/units/carlos-instance-control.service |
| @@ -60,4 +60,6 @@ ProtectKernelLogs=yes |
| # ProtectKernelModules=: the kernel's helper loads them outside this unit. |
| RestrictAddressFamilies=AF_UNIX AF_NETLINK |
| MemoryDenyWriteExecute=yes |
| -MemoryMax=64M |
| +# Registry-read children share this cgroup, about 10-17 MB each, so 256M |
| +# covers roughly twelve concurrent starts (#1291). A ceiling, not a reservation. |
| +MemoryMax=256M |
diff --git a/internal/host/units_isolation_test.go b/internal/host/units_isolation_test.go| index c1b745a1f..1a0feeca3 100644 |
| --- a/internal/host/units_isolation_test.go |
| +++ b/internal/host/units_isolation_test.go |
| @@ -336,7 +336,7 @@ func TestControlUnitsShape(t *testing.T) { |
| } |
| u := readUnit(t, "carlos-instance-control.service") |
| for _, want := range []string{"ExecStart=/opt/carlos/carlos instance-control", "Requires=carlos-instance-control.socket", |
| - "data-instances.mount", "ProtectSystem=strict", "ReadWritePaths=/run/carlos-control /run/systemd/system", "RestrictAddressFamilies=AF_UNIX", "MemoryMax=64M"} { |
| + "data-instances.mount", "ProtectSystem=strict", "ReadWritePaths=/run/carlos-control /run/systemd/system", "RestrictAddressFamilies=AF_UNIX", "MemoryMax=256M"} { |
| if !strings.Contains(u, want) { |
| t.Errorf("control service lacks %q", want) |
| } |
diff --git a/internal/provision/testdata/plan14-root-reads-chain.tsv b/internal/provision/testdata/plan14-root-reads-chain.tsv| index 87f1ac29c..8119dbae5 100644 |
| --- a/internal/provision/testdata/plan14-root-reads-chain.tsv |
| +++ b/internal/provision/testdata/plan14-root-reads-chain.tsv |
| @@ -257,3 +257,4 @@ plan14-root-reads-ha-generations.tsv 5bf2d00767807be812b004a474352ec184788be3ce6 |
| plan14-root-reads-named-units-param-114.tsv 8daea28ba9b419e8680e8eb0d44a577c68ec01e02663a878288441dc579cc9f7 |
| plan14-root-reads-race-activator-1249.tsv 879cc401365966a2ca23962245ce9dff4b7879fe7a27b7691c3d91a4e70ff6e8 |
| plan14-root-reads-named-units-guard-fix.tsv aa2c0729fe761d6438d2e8b9ef4ef2c1a20699f5e81dded1783c9a008dfafba9 |
| +plan14-root-reads-instance-control-memory-1291.tsv 70ca25987f5a18849d7ae880e30628a47475375d6dde2409a505232d78abb872 |
diff --git a/internal/provision/testdata/plan14-root-reads-instance-control-memory-1291.tsv b/internal/provision/testdata/plan14-root-reads-instance-control-memory-1291.tsv| new file mode 100644 |
| index 000000000..4ec91776d |
| --- /dev/null |
| +++ b/internal/provision/testdata/plan14-root-reads-instance-control-memory-1291.tsv |
| @@ -0,0 +1,8 @@ |
| +# Census overlay instance-control-memory-1291, written by census-propose (#369). census-ruling-instance-control-memory-1291, approved on #280 before the merge; the approval card is docs/superpowers/notes/census-proposal-instance-control-memory-1291.md. |
| +# 3 body, 0 sites, 2 envelope: each changed unit re-frozen at its existing disposition with its claim appended. |
| +@legacy f3f002d480ed0795879a3bf57eb48d676a525ffd0060885557c674b166921a84 internal/host/systemunits.go ensureSystemUnitsWithGuard legacy accepted-legacy accepted-legacy;rev3=census-ruling-region-ha-p3b;basis=Move unit-repair body here with existing lock/unit/host.env reads; under lock require operator repair permit on groups before writes and each systemctl effect, preserve root masks/drop-in and omit automatic update/recovery units. New indirect protected marker/permit reads, never agent receipt; fixed repair unchanged.;rev3=census-ruling-gvisor-demos;basis=Create runtime/rootfs directories and repair installer plus three measured workload templates without arming them. Existing unit/host.env reads unchanged.;rev3=census-ruling-gvisor-update-dirs;basis=Create the two live sandbox directories (the same fixed ensureSandboxDirectories paths) before the apply lock and the early returns on non-group boxes; group members still create them only after the maintenance permit. A creation error still ends the pass at the original point. Then call refreshControlSandboxRootfs. No new path, input or systemctl verb beyond the try-restart below.;rev3=census-ruling-demo-slice-adopt;basis=Compare the installed carlos-demo-builds.slice (fixed root unit path, embedded unit) with its host.env rendering, and ask systemd for the slice's loaded MemoryMax/TasksMax (systemctl show on the fixed name). A definite difference is a reinstall (existing write loop + daemon-reload). No live budget change, no new path, input or write target.;rev3=census-ruling-instance-control-memory-1291;basis=The adopt pass controlChanged expression moved verbatim into claimControlStale so tests can drive it (#1291 R1): same inputs, same decision, same rewrite and restart. No read, path, input or permission change. file:OpenFile@985,file:ReadFile@5626,file:ReadFile@5759,file:ReadFile@6502,file:ReadFile@6644,file:ReadFile@8403,file:Stat@5535 4ec15721391b0165591a749932b27f2d2afe642c47e1c383be7bde419b510d8c |
| +internal/host/claimunit.go claimUnitContent authority E1,E3 rev3=PROPOSED-p3;identity=root claim/adoption controller uid 0;basis=adds write permission for exactly the public retry directory only when the root settings reader has verified a complete census and prepared that directory;rev3=census-ruling-agent-start-demo-1;basis=Demo-only ReadWritePaths for instance data/runtime, staged files and root release lock; ordinary rendering unchanged.;rev3=census-ruling-agent-start-demo-1-followups;basis=In demo box mode, declare StateDirectory=carlos-release/private with mode 0700 so systemd creates root-owned release state before namespace setup and keeps it writable. Existing ReadWritePaths and ordinary/claims rendering stay unchanged. No new read site or caller-controlled path.;rev3=census-ruling-agent-start-demo-3;basis=Declare /var/lib/carlos-demo-snapshots as root-owned StateDirectory with mode 0700 only for configured demo boxes, including recovery with admission off. Ordinary claims units retain their original directories. The shared unit template is unchanged from main; no file read is added.;rev3=census-ruling-gvisor-demos;basis=Mark only claim/demo renderings explicitly; common host.env remains loaded. Ordinary rendering masks CARLOS_HOST_CLAIM with UnsetEnvironment, removed only for a successfully parsed enabled claim rendering. Existing template read unchanged. Task 7: render aggregate build slice through SlicePropertiesFor using the resolved host mode, with 32 MiB and 64-task gVisor overhead per slot. Existing control permissions unchanged.;rev3=census-ruling-same-box-edge-fence;basis=Root unit rendering (claims mode). The control unit's families line is now `RestrictAddressFamilies=AF_UNIX AF_NETLINK`; the claims rewrite matches that line (it matched the old one exactly, and would otherwise have silently dropped AF_INET/AF_INET6) and renders `AF_UNIX AF_NETLINK AF_INET AF_INET6`. No read added; no other directive changed.;rev3=census-ruling-instance-control-memory-1291;basis=The demo build worker and claims memory rewrites go through replaceClaimControlMemory, which matches this build line (claimControlMemoryMax, MemoryMax=256M) or v0.40.1 MemoryMax=64M (#1291, R1): system apply renders the candidate template with the installed renderer, so both versions must render. Build worker and claims renderings are unchanged from main on either template; off and demo-box renderings carry the template own value. No read, path, input or permission change. data-literal@1504 02d0f82424f04cc7059d5261003a3d3bcc972f73cdbf38e41a7e5536d1dedf4d |
| +internal/host/systemunits.go ensureSystemUnitsWithGuard legacy accepted-legacy accepted-legacy;rev3=census-ruling-region-ha-p3b;basis=Move unit-repair body here with existing lock/unit/host.env reads; under lock require operator repair permit on groups before writes and each systemctl effect, preserve root masks/drop-in and omit automatic update/recovery units. New indirect protected marker/permit reads, never agent receipt; fixed repair unchanged.;rev3=census-ruling-gvisor-demos;basis=Create runtime/rootfs directories and repair installer plus three measured workload templates without arming them. Existing unit/host.env reads unchanged.;rev3=census-ruling-gvisor-update-dirs;basis=Create the two live sandbox directories (the same fixed ensureSandboxDirectories paths) before the apply lock and the early returns on non-group boxes; group members still create them only after the maintenance permit. A creation error still ends the pass at the original point. Then call refreshControlSandboxRootfs. No new path, input or systemctl verb beyond the try-restart below.;rev3=census-ruling-demo-slice-adopt;basis=Compare the installed carlos-demo-builds.slice (fixed root unit path, embedded unit) with its host.env rendering, and ask systemd for the slice's loaded MemoryMax/TasksMax (systemctl show on the fixed name). A definite difference is a reinstall (existing write loop + daemon-reload). No live budget change, no new path, input or write target.;rev3=census-ruling-instance-control-memory-1291;basis=The adopt pass controlChanged expression moved verbatim into claimControlStale so tests can drive it (#1291 R1): same inputs, same decision, same rewrite and restart. No read, path, input or permission change. file:OpenFile@985,file:ReadFile@5626,file:ReadFile@5759,file:ReadFile@6502,file:ReadFile@6644,file:ReadFile@8403,file:Stat@5535 4ec15721391b0165591a749932b27f2d2afe642c47e1c383be7bde419b510d8c |
| +internal/host/units/carlos-instance-control.service external authority B3,C,D,E1,J4 root-input;kept=B3,C,D,E1;scope=protected-decision-only;rev3=census-ruling-agent-start-demo-3;basis=Correct the runtime-write comment to include escaped demo snapshot .mount units alongside per-generation drop-ins; service directives are unchanged.;rev3=census-ruling-region-ha-graceful-terminate-t1;basis=Region HA T1/DR-4 final fix: retain After=nftables.service without Wants or Requires; add Restart=on-failure and RestartSec=1 so a crashed control restarts before a later edge stop can block socket activation and shutdown proofs; rewrap the header and explain this ordering. Same ExecStart and protections; no new root read, input, path, command or capability.;rev3=census-ruling-gvisor-demos;basis=Add Wants/After installer and narrowly writable rootfs parent. Keep one host.env EnvironmentFile and other hardening.;rev3=census-ruling-gvisor-update-dirs;basis=Make the existing /var/lib/carlos/oci-rootfs writable-path entry optional when absent so old updaters can start the new control unit; retain ProtectSystem=strict and the same path and environment. No new root read.;rev3=census-ruling-same-box-edge-fence;basis=RestrictAddressFamilies gains AF_NETLINK (nft talks netlink), with a comment; the group drop-in already granted it on group boxes. No capability, path, environment or ExecStart change. Without it nft fails and the whole #367 deny stays.;rev3=census-ruling-instance-control-memory-1291;basis=MemoryMax=64M raised to 256M with a two-line comment (#1291): registry-read children share this cgroup and four overlapping starts OOM-killed control at 64M. A ceiling only; no ExecStart, path, environment, capability or protection change. No new root read. file:external@0 4a09e4936244d92b52ca9014cc9e5b6bfe3f3fb7dd30ea357097e959f493f62b |
| +@coverage internal/host 25bba4d425aa44f37ede40bb37ec95e1bdae284d6889457cae456dbc7bc6727e |
| +@coverage internal/host/units 73ac9551af5c339f0217789cad60c4661e4e2d101573ad80a1dbcca896084ebf |