litestream: restart on a changed unit; config logs warnings only
Human
Prompt
Agent manager 8a3d302: #802 follow-up. The v0.34.40 roll rewrote carlos-litestream.service with LOG_LEVEL=warn and reloaded systemd but never restarted it. The update path should try-restart a running carlos-litestream.service when its unit changes. Please fix it on a small branch with a test. / annie's binary is v0.3.13, which doesn't read LOG_LEVEL: propose a config logging level or a newer binary, delivered the platform's way.
From this commit's own message. The prompt ledger has no entries for it — which is what a squashed, rebased or amended commit looks like, and what every commit looks like in a repo without a ledger.
Summary
An apply that changes carlos-litestream.service now try-restarts the running replicator after daemon-reload, and the generated /etc/litestream.yml sets logging.level warn, which litestream 0.3.13 reads (it ignores LOG_LEVEL).
Agent notes
Follow-up to #802 from 8a3d302: v0.34.40 reloaded the new unit on annie but never restarted it; annie's binary is 0.3.13. Census overlay litestream-unit-restart awaits cross-model approval.
7 files changed,
+194
−0
docs/superpowers/notes/census-proposal-litestream-unit-restart.md+23 −0internal/host/systemapply.go+34 −0internal/host/systemapply_litestream_test.go+108 −0internal/ops/litestream.go+6 −0internal/ops/ops_test.go+13 −0internal/provision/testdata/plan14-root-reads-chain.tsv+1 −0internal/provision/testdata/plan14-root-reads-litestream-unit-restart.tsv+9 −0
CI passed — run details
diff --git a/docs/superpowers/notes/census-proposal-litestream-unit-restart.md b/docs/superpowers/notes/census-proposal-litestream-unit-restart.md| new file mode 100644 |
| index 000000000..1d6a770f0 |
| --- /dev/null |
| +++ b/docs/superpowers/notes/census-proposal-litestream-unit-restart.md |
| @@ -0,0 +1,23 @@ |
| +# Census approval: litestream-unit-restart |
| + |
| +Ruling `census-ruling-litestream-unit-restart`. The independent reviewer approves or rejects every change below, 1 with a new disposition; nothing goes to the census owner (Paul, #280, 2026-10-26). |
| + |
| +| unit | change | read sites | what changed | |
| +|---|---|---|---| |
| +| `internal/host/systemapply.go` `systemApplier.installUnits` | sites | +file:ReadFile | reads the installed carlos-litestream.service before and after the copy and try-restarts the replicator when it changed (#802 follow-up); root-owned unit dir only | |
| + |
| +Envelopes re-pinned: `internal/host`, `internal/ops`. |
| + |
| +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/systemapply.go` `systemApplier.installUnits`: legacy, task accepted-legacy. Basis: accepted-legacy;rev3=J4;basis=root-input;rev3=census-ruling-agent-start-demo-1;basis=Renders the staged build slice against live root host configuration; preserves it when settings cannot be parsed.;rev3=census-ruling-region-ha-p3b;basis=Require group apply/rollback permit, preserve root Restart=no/nftables drop-in and masks, and skip automatic update/recovery units. Existing staged unit and host.env reads remain; new indirect marker/permit reads are protected and agent receipts are excluded.;rev3=census-ruling-gvisor-demos;basis=Create rootfs/runtime parent directories before controller restart. Existing staged unit and root-owned host.env reads unchanged. |
| + |
| +New dispositions, which census-propose wrote and the reviewer checks against the unit's reads and the diff: |
| + |
| +- new `internal/host/systemapply.go:systemApplier.restartChangedLitestream`: reads +file:ReadFile; written legacy, accepted-legacy, frozen |
| + |
| +Seal: `b60c83f8ba4311222174284420b5327e92002882b4a1c6f291e74e69234c6f59` → `e650a3ceb286df4457850d74ded9229d94ed9932057ae5b45b1d6c3d0d843ce2`. |
| + |
| +Overlay: `internal/provision/testdata/plan14-root-reads-litestream-unit-restart.tsv`, appended to `internal/provision/testdata/plan14-root-reads-chain.tsv`. |
diff --git a/internal/host/systemapply.go b/internal/host/systemapply.go| index 0d1814c2d..ee95b2d36 100644 |
| --- a/internal/host/systemapply.go |
| +++ b/internal/host/systemapply.go |
| @@ -1,6 +1,7 @@ |
| package host |
| import ( |
| + "bytes" |
| "context" |
| "crypto/ed25519" |
| "encoding/json" |
| @@ -1517,6 +1518,10 @@ func (a *systemApplier) installUnits(ctx context.Context, stagedRoot string, rem |
| if err != nil && !errors.Is(err, os.ErrNotExist) { |
| return err |
| } |
| + beforeLitestream, err := os.ReadFile(filepath.Join(unitDir, litestreamUnit)) |
| + if err != nil && !errors.Is(err, os.ErrNotExist) { |
| + return err |
| + } |
| // A host.env this build cannot parse says nothing about the claim |
| // (6b-N2). A control unit already rendered for an enabled box stays as |
| // it is, unrestarted, as adopt leaves it; a flag-off one is rendered as |
| @@ -1611,6 +1616,7 @@ func (a *systemApplier) installUnits(ctx context.Context, stagedRoot string, rem |
| if _, err = a.ops.systemctl("daemon-reload"); err != nil { |
| return err |
| } |
| + a.restartChangedLitestream(filepath.Join(unitDir, litestreamUnit), beforeLitestream) |
| afterControl, err := os.ReadFile(filepath.Join(unitDir, claimControlUnit)) |
| if err != nil && !errors.Is(err, os.ErrNotExist) { |
| return err |
| @@ -1618,6 +1624,34 @@ func (a *systemApplier) installUnits(ctx context.Context, stagedRoot string, rem |
| return restartClaimControl(filepath.Join(unitDir, claimControlUnit), beforeControl, afterControl, a.ops.systemctl) |
| } |
| +// litestreamUnit is the box-wide replicator. Nothing else in an apply |
| +// restarts it, so without restartChangedLitestream a new unit file is |
| +// loaded by daemon-reload and then ignored by the process that is already |
| +// running (#802: v0.34.40 shipped LOG_LEVEL=warn and the flagship kept |
| +// logging at INFO until somebody restarted the unit by hand). |
| +const litestreamUnit = "carlos-litestream.service" |
| + |
| +// restartChangedLitestream try-restarts the replicator when this apply |
| +// changed its unit file. try-restart, not restart: a box where the unit is |
| +// installed but not running (no generated /etc/litestream.yml, or stopped |
| +// on purpose) must stay that way. A failure is logged and the apply goes |
| +// on: the unit's own Restart=always keeps trying, its restarts alert |
| +// reports a loop, and rolling a good build back over a replicator restart |
| +// would cost more than it saves. |
| +func (a *systemApplier) restartChangedLitestream(path string, before []byte) { |
| + after, err := os.ReadFile(path) |
| + if err != nil && !errors.Is(err, os.ErrNotExist) { |
| + a.ops.logf("system apply: %s: %v (not restarted)", litestreamUnit, err) |
| + return |
| + } |
| + if after == nil || bytes.Equal(before, after) { |
| + return |
| + } |
| + if _, err := a.ops.systemctl("try-restart", litestreamUnit); err != nil { |
| + a.ops.logf("system apply: try-restart %s: %v (continuing; the running replicator keeps its old unit settings until it restarts)", litestreamUnit, err) |
| + } |
| +} |
| + |
| // restartEdge resets the failure counter FIRST — a unit that hit its start |
| // limit refuses restart, and so would the rollback — then restarts. |
| func (a *systemApplier) restartEdge() error { |
diff --git a/internal/host/systemapply_litestream_test.go b/internal/host/systemapply_litestream_test.go| new file mode 100644 |
| index 000000000..226c781ee |
| --- /dev/null |
| +++ b/internal/host/systemapply_litestream_test.go |
| @@ -0,0 +1,108 @@ |
| +package host |
| + |
| +import ( |
| + "errors" |
| + "os" |
| + "path/filepath" |
| + "strings" |
| + "testing" |
| +) |
| + |
| +// An apply that changes the replicator's unit file restarts a running |
| +// replicator, so the new settings take effect; one that leaves the file as |
| +// it was does not. Before this, daemon-reload loaded the new unit and the |
| +// old process kept running with the old one (#802: LOG_LEVEL=warn shipped |
| +// in v0.34.40, and the flagship kept logging at INFO). |
| +func TestApplyRestartsTheReplicatorWhenItsUnitChanges(t *testing.T) { |
| + stage := t.TempDir() |
| + if err := bootstrap(stage, nil); err != nil { |
| + t.Fatal(err) |
| + } |
| + staged, err := os.ReadFile(filepath.Join(stage, "etc/systemd/system", litestreamUnit)) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + const restart = "systemctl try-restart " + litestreamUnit |
| + restarts := func(calls []string) int { |
| + n := 0 |
| + for _, c := range calls { |
| + if c == restart { |
| + n++ |
| + } |
| + } |
| + return n |
| + } |
| + |
| + f := newApplyFixture(t, "stable", "v1.0.0") |
| + installed := filepath.Join(f.a.etcDir, "systemd/system", litestreamUnit) |
| + if err := os.MkdirAll(filepath.Dir(installed), 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + old := strings.Replace(string(staged), "Environment=LOG_LEVEL=warn\n", "", 1) |
| + if old == string(staged) { |
| + t.Fatal("the staged unit carries no LOG_LEVEL line to take out; pick another difference") |
| + } |
| + if err := os.WriteFile(installed, []byte(old), 0o644); err != nil { |
| + t.Fatal(err) |
| + } |
| + |
| + if err := f.a.installUnits(t.Context(), stage, nil); err != nil { |
| + t.Fatal(err) |
| + } |
| + if got := restarts(f.calls); got != 1 { |
| + t.Fatalf("changed unit: %d try-restarts of %s, want 1; calls:\n%s", got, litestreamUnit, strings.Join(f.calls, "\n")) |
| + } |
| + reload := -1 |
| + for i, c := range f.calls { |
| + if c == "systemctl daemon-reload" { |
| + reload = i |
| + } |
| + if c == restart && (reload < 0 || i < reload) { |
| + t.Fatalf("try-restart before daemon-reload would restart onto the old unit; calls:\n%s", strings.Join(f.calls, "\n")) |
| + } |
| + } |
| + |
| + f.calls = nil |
| + if err := f.a.installUnits(t.Context(), stage, nil); err != nil { |
| + t.Fatal(err) |
| + } |
| + if got := restarts(f.calls); got != 0 { |
| + t.Fatalf("unchanged unit: %d try-restarts of %s, want 0", got, litestreamUnit) |
| + } |
| +} |
| + |
| +// A failed restart does not fail the apply: the build is good, and the |
| +// unit's own Restart=always keeps trying. |
| +func TestApplyGoesOnWhenTheReplicatorRestartFails(t *testing.T) { |
| + stage := t.TempDir() |
| + if err := bootstrap(stage, nil); err != nil { |
| + t.Fatal(err) |
| + } |
| + f := newApplyFixture(t, "stable", "v1.0.0") |
| + installed := filepath.Join(f.a.etcDir, "systemd/system", litestreamUnit) |
| + if err := os.MkdirAll(filepath.Dir(installed), 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.WriteFile(installed, []byte("[Service]\nExecStart=/bin/true\n"), 0o644); err != nil { |
| + t.Fatal(err) |
| + } |
| + ctl := f.a.ops.systemctl |
| + f.a.ops.systemctl = func(args ...string) (string, error) { |
| + if len(args) == 2 && args[0] == "try-restart" && args[1] == litestreamUnit { |
| + ctl(args...) |
| + return "", errors.New("Job for carlos-litestream.service failed") |
| + } |
| + return ctl(args...) |
| + } |
| + var logged []string |
| + f.a.ops.logf = func(format string, a ...any) { logged = append(logged, format) } |
| + if err := f.a.installUnits(t.Context(), stage, nil); err != nil { |
| + t.Fatalf("a failed replicator restart failed the apply: %v", err) |
| + } |
| + if !strings.Contains(strings.Join(f.calls, "\n"), "systemctl try-restart "+litestreamUnit) { |
| + t.Fatal("the changed unit was not try-restarted") |
| + } |
| + if !strings.Contains(strings.Join(logged, "\n"), "try-restart %s") { |
| + t.Fatalf("the failed restart was not logged: %q", logged) |
| + } |
| +} |
diff --git a/internal/ops/litestream.go b/internal/ops/litestream.go| index 5e8850d2c..caa4873ca 100644 |
| --- a/internal/ops/litestream.go |
| +++ b/internal/ops/litestream.go |
| @@ -120,6 +120,12 @@ func LitestreamConfig(dbPaths []string, bucket, prefix, region string) string { |
| var b strings.Builder |
| b.WriteString("# GENERATED by `carlos ops litestream` — do not hand-edit; re-run the verb\n") |
| b.WriteString("# after `carlos add`/`remove` so the replication set follows the fleet.\n") |
| + // Warnings and errors only (#802). At INFO litestream logs a line per |
| + // database per sync and pushes everything else out of a capped journal. |
| + // The unit's LOG_LEVEL=warn says the same to 0.5, but 0.3.13 (still on |
| + // the flagship's Irish box) reads only this stanza; both honour it. |
| + b.WriteString("logging:\n") |
| + b.WriteString(" level: warn\n") |
| // Global snapshot policy: applies to every database below. |
| b.WriteString("snapshot:\n") |
| fmt.Fprintf(&b, " interval: %s\n", SnapshotInterval) |
diff --git a/internal/ops/ops_test.go b/internal/ops/ops_test.go| index 4f9b05d01..1c35f36b3 100644 |
| --- a/internal/ops/ops_test.go |
| +++ b/internal/ops/ops_test.go |
| @@ -1105,3 +1105,16 @@ func TestWriteMemoryDropInSizesFromTheConfigUnderTheLock(t *testing.T) { |
| t.Fatal("a count that fails must fail the cap, not size it for zero databases") |
| } |
| } |
| + |
| +// The generated config holds litestream to warnings (#802). It is the |
| +// config stanza, not the unit's LOG_LEVEL, that 0.3.13 reads, so it has to |
| +// be here, at the top level, for every box this verb writes. |
| +func TestLitestreamConfigLogsWarningsOnly(t *testing.T) { |
| + cfg := LitestreamConfig([]string{"/data/registry.db"}, "b", "p", "eu-west-1") |
| + if !strings.Contains(cfg, "\nlogging:\n level: warn\n") { |
| + t.Fatalf("generated config does not set logging.level to warn at the top level:\n%s", cfg) |
| + } |
| + if got := ParseLitestreamDBs(cfg); len(got) != 1 || got[0] != "/data/registry.db" { |
| + t.Fatalf("the logging stanza confused ParseLitestreamDBs: %v", got) |
| + } |
| +} |
diff --git a/internal/provision/testdata/plan14-root-reads-chain.tsv b/internal/provision/testdata/plan14-root-reads-chain.tsv| index 32b36c4fa..7f01373e2 100644 |
| --- a/internal/provision/testdata/plan14-root-reads-chain.tsv |
| +++ b/internal/provision/testdata/plan14-root-reads-chain.tsv |
| @@ -157,3 +157,4 @@ plan14-root-reads-held-move-speed.tsv 837f27a6b7b6ed272f8c68aca07650fa32cf3ec92c |
| plan14-root-reads-litestream-quiet-802.tsv 6d3ea7fccce1a7f4bc507b6eb8d368ed0b62f83022504aa04c7684c72c93aa9a |
| plan14-root-reads-group-roll-dr4-lock-refusal.tsv 6d3ea7fccce1a7f4bc507b6eb8d368ed0b62f83022504aa04c7684c72c93aa9a |
| plan14-root-reads-held-floor-capability.tsv b60c83f8ba4311222174284420b5327e92002882b4a1c6f291e74e69234c6f59 |
| +plan14-root-reads-litestream-unit-restart.tsv e650a3ceb286df4457850d74ded9229d94ed9932057ae5b45b1d6c3d0d843ce2 |
diff --git a/internal/provision/testdata/plan14-root-reads-litestream-unit-restart.tsv b/internal/provision/testdata/plan14-root-reads-litestream-unit-restart.tsv| new file mode 100644 |
| index 000000000..415ec88d8 |
| --- /dev/null |
| +++ b/internal/provision/testdata/plan14-root-reads-litestream-unit-restart.tsv |
| @@ -0,0 +1,9 @@ |
| +# Census overlay litestream-unit-restart, written by census-propose (#369). census-ruling-litestream-unit-restart, approved on #280 before the merge; the approval card is docs/superpowers/notes/census-proposal-litestream-unit-restart.md. |
| +# 0 body, 1 sites, 2 envelope: each changed unit re-frozen at its existing disposition with its claim appended. |
| +# 1 new dispositions: 0 reads re-frozen at their disposition with the claim, 1 new units frozen accepted-legacy, 0 quiet units retired while they read nothing, 0 other. |
| +@legacy 4757e956499c1406c7b011d70ddc526eccdd767b21a62cef7ca4ba4039b180c8 internal/host/systemapply.go systemApplier.restartChangedLitestream legacy accepted-legacy accepted-legacy;rev3=census-ruling-litestream-unit-restart;basis=new helper split out of installUnits: one ReadFile of /etc/systemd/system/carlos-litestream.service (root-owned unit dir installUnits writes) and systemctl try-restart of that fixed unit name; no tenant-, app- or agent-named input file:ReadFile@94 892d8f2820e995d2f9fa0363239a1b3b445d551812b799b8e2cf407a9b44167f |
| +@legacy 86d1c743a50538ea97c73a0831aceb08de5083526360e5520c131cd402195cc3 internal/host/systemapply.go systemApplier.installUnits legacy accepted-legacy accepted-legacy;rev3=J4;basis=root-input;rev3=census-ruling-agent-start-demo-1;basis=Renders the staged build slice against live root host configuration; preserves it when settings cannot be parsed.;rev3=census-ruling-region-ha-p3b;basis=Require group apply/rollback permit, preserve root Restart=no/nftables drop-in and masks, and skip automatic update/recovery units. Existing staged unit and host.env reads remain; new indirect marker/permit reads are protected and agent receipts are excluded.;rev3=census-ruling-gvisor-demos;basis=Create rootfs/runtime parent directories before controller restart. Existing staged unit and root-owned host.env reads unchanged.;rev3=census-ruling-litestream-unit-restart;basis=reads the installed carlos-litestream.service before and after the copy and try-restarts the replicator when it changed (#802 follow-up); root-owned unit dir only file:ReadDir@1655,file:ReadFile@1060,file:ReadFile@1208,file:ReadFile@2246,file:ReadFile@5367,file:Stat@2904,file:Stat@4197 18da69ea4a85aea7fb057718997999c23fab67460fbea20bb0425252619ef89e |
| +internal/host/systemapply.go systemApplier.restartChangedLitestream legacy accepted-legacy accepted-legacy;rev3=census-ruling-litestream-unit-restart;basis=new helper split out of installUnits: one ReadFile of /etc/systemd/system/carlos-litestream.service (root-owned unit dir installUnits writes) and systemctl try-restart of that fixed unit name; no tenant-, app- or agent-named input file:ReadFile@94 892d8f2820e995d2f9fa0363239a1b3b445d551812b799b8e2cf407a9b44167f |
| +internal/host/systemapply.go systemApplier.installUnits legacy accepted-legacy accepted-legacy;rev3=J4;basis=root-input;rev3=census-ruling-agent-start-demo-1;basis=Renders the staged build slice against live root host configuration; preserves it when settings cannot be parsed.;rev3=census-ruling-region-ha-p3b;basis=Require group apply/rollback permit, preserve root Restart=no/nftables drop-in and masks, and skip automatic update/recovery units. Existing staged unit and host.env reads remain; new indirect marker/permit reads are protected and agent receipts are excluded.;rev3=census-ruling-gvisor-demos;basis=Create rootfs/runtime parent directories before controller restart. Existing staged unit and root-owned host.env reads unchanged.;rev3=census-ruling-litestream-unit-restart;basis=reads the installed carlos-litestream.service before and after the copy and try-restarts the replicator when it changed (#802 follow-up); root-owned unit dir only file:ReadDir@1655,file:ReadFile@1060,file:ReadFile@1208,file:ReadFile@2246,file:ReadFile@5367,file:Stat@2904,file:Stat@4197 18da69ea4a85aea7fb057718997999c23fab67460fbea20bb0425252619ef89e |
| +@coverage internal/host c3d6414f383750880f41be0cd9935ecc417cf90ec91cbb6a5828a641941c6a6f |
| +@coverage internal/ops 5552d9c6994f457b7f1e9b2fe35c67d3e62feaa1df5e6a1e28be9a66fbd0940f |