carlos / platform Public

demobuild: a pending budget waits; a cut job list defers

Fixes for the two P2s from Astra R3:

- A budget deferral (errBudgetPending, still a DEMO-BUILD) is no
  longer a terminal build failure. Build retries admission every 2 s
  until the job deadline, before taking a slot or the build lock. Any
  other admission error fails at once.
- reconcileBudget treats a list-jobs output at the 128 KiB capture
  limit as incomplete and defers instead of stopping the slice.

Tests: pending-then-admitted, pending at the deadline, no retry for a
real failure, and a truncated job list. Census re-proposed.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 39de51e4e195e10ad9090e627617c4952bbf98b5 parent b0c4d51
6 files changed, +94 −13
  • docs/superpowers/notes/census-proposal-demo-slice-adopt.md +2 −2
  • internal/demobuild/budget.go +18 −3
  • internal/demobuild/budget_test.go +53 −0
  • internal/demobuild/worker.go +17 −4
  • internal/provision/testdata/plan14-root-reads-chain.tsv +1 −1
  • internal/provision/testdata/plan14-root-reads-demo-slice-adopt.tsv +3 −3

CI passed — run details

diff --git a/docs/superpowers/notes/census-proposal-demo-slice-adopt.md b/docs/superpowers/notes/census-proposal-demo-slice-adopt.md
index 7ffe363fe..c605ce2e2 100644
--- a/docs/superpowers/notes/census-proposal-demo-slice-adopt.md
+++ b/docs/superpowers/notes/census-proposal-demo-slice-adopt.md
@@ -4,7 +4,7 @@ Ruling `census-ruling-demo-slice-adopt`. The independent reviewer approves or re
| unit | change | read sites | what changed |
|---|---|---|---|
-| `internal/demobuild/worker.go` `Worker.Build` | body | unchanged (1) | Before admission, call the admit hook (nil = reconcileBudget). reconcileBudget reads the fixed build slice's systemctl show budget, its cgroup limit files and cgroup.events under the fixed root cgroup path, and list-jobs; only with buildPurge held exclusively, the slice empty and no build or slice job queued does it run systemctl stop on the fixed slice name. Otherwise it defers with DEMO-BUILD. No tenant, app or agent input names a path or unit. |
+| `internal/demobuild/worker.go` `Worker.Build` | body | unchanged (1) | Before admission, call the admit hook (nil = reconcileBudget); a pending budget change (errBudgetPending) is retried every 2 s until the job deadline, any other error fails at once. reconcileBudget reads the fixed build slice's systemctl show budget, its cgroup limit files and cgroup.events under the fixed root cgroup path, and list-jobs (a listing at the capture limit defers); only with buildPurge held exclusively, the slice empty and no build or slice job queued does it run systemctl stop on the fixed slice name. No tenant, app or agent input names a path or unit. |
| `internal/host/systemunits.go` `ensureSystemUnitsWithGuard` | sites | +file:ReadFile ×2 | 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. |
Envelopes re-pinned: `internal/demobuild`, `internal/host`.
@@ -21,6 +21,6 @@ New dispositions, which census-propose wrote and the reviewer checks against the
- new `internal/demobuild/budget.go:budgetSlicePopulated`: reads +file:ReadFile; written legacy, accepted-legacy, frozen
- new `internal/demobuild/budget.go:kernelBudgetMatches`: reads +file:Lstat, +file:ReadFile; written legacy, accepted-legacy, frozen
-Seal: `0f7153571df8c1b4b50c31f57efcfdf60c71a12c80fd935fb3ea202fa0201dd5` → `762577dd5e472a076779f5e9dfd697034293a4f52667c42002671f5f887e474a`.
+Seal: `0f7153571df8c1b4b50c31f57efcfdf60c71a12c80fd935fb3ea202fa0201dd5` → `459de6e3054d1a93c07db305951e42ac3a3dcdf08ddd1d320a840ed835ef7b51`.
Overlay: `internal/provision/testdata/plan14-root-reads-demo-slice-adopt.tsv`, appended to `internal/provision/testdata/plan14-root-reads-chain.tsv`.
diff --git a/internal/demobuild/budget.go b/internal/demobuild/budget.go
index caba9407e..f30ebc63d 100644
--- a/internal/demobuild/budget.go
+++ b/internal/demobuild/budget.go
@@ -8,6 +8,7 @@ import (
"path/filepath"
"strconv"
"strings"
+ "time"
"github.com/carlosframework/platform/internal/execsandbox"
)
@@ -15,6 +16,15 @@ import (
// budgetSlice holds every native and gVisor build unit.
const budgetSlice = "carlos-demo-builds.slice"
+// errBudgetPending marks a deferral: the budget change waits for running
+// work. Build retries it until the job's deadline (budgetRetry apart); every
+// other ErrBuild stays a build failure.
+var errBudgetPending = errors.New("build budget change pending")
+
+// budgetRetry is how long Build waits between admission attempts while a
+// budget change is pending.
+var budgetRetry = 2 * time.Second
+
// reconcileBudget runs before a build is admitted. It is the only place a
// build budget reaches the kernel after the slice was first started.
//
@@ -44,7 +54,7 @@ func (w *Worker) reconcileBudget(ctx context.Context) error {
// outright. Holding it here means none of them is in flight in this
// process, and processLock means no other worker process exists.
if !w.buildPurge.TryLock() {
- return fmt.Errorf("%w: build budget change waits for running builds", ErrBuild)
+ return fmt.Errorf("%w: %w: waits for running builds", ErrBuild, errBudgetPending)
}
defer w.buildPurge.Unlock()
if ok, err := kernelBudgetMatches(n, family); err != nil || ok {
@@ -55,15 +65,20 @@ func (w *Worker) reconcileBudget(ctx context.Context) error {
return err
}
if populated {
- return fmt.Errorf("%w: build budget change waits for an empty build slice", ErrBuild)
+ return fmt.Errorf("%w: %w: waits for an empty build slice", ErrBuild, errBudgetPending)
}
jobs, err := systemctl(ctx, "list-jobs", "--no-legend", "--no-pager")
if err != nil {
return err
}
+ // Absence needs the whole job list: one the capture may have cut at its
+ // limit could hide a queued build start, so it defers like a busy slice.
+ if len(jobs) >= systemctlCaptureLimit {
+ return fmt.Errorf("%w: %w: job list too long to prove nothing is queued", ErrBuild, errBudgetPending)
+ }
for _, line := range strings.Split(jobs, "\n") {
if f := strings.Fields(line); len(f) >= 2 && (f[1] == budgetSlice || strings.HasPrefix(f[1], "carlos-demo-build@") || strings.HasPrefix(f[1], "carlos-demo-build-gvisor@")) {
- return fmt.Errorf("%w: build budget change waits for queued %s", ErrBuild, f[1])
+ return fmt.Errorf("%w: %w: waits for queued %s", ErrBuild, errBudgetPending, f[1])
}
}
if _, err := systemctl(ctx, "stop", budgetSlice); err != nil {
diff --git a/internal/demobuild/budget_test.go b/internal/demobuild/budget_test.go
index 4cd50b0fc..ef4c07e7e 100644
--- a/internal/demobuild/budget_test.go
+++ b/internal/demobuild/budget_test.go
@@ -1,13 +1,16 @@
package demobuild
import (
+ "bytes"
"context"
"errors"
+ "fmt"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
+ "time"
"github.com/carlosframework/platform/internal/execsandbox"
)
@@ -102,6 +105,7 @@ func TestReconcileBudget(t *testing.T) {
{name: "populated slice defers", family: gv, loaded: gv, kn: sd, populated: true, wantErr: true},
{name: "queued build defers", family: gv, loaded: gv, kn: sd, jobs: "12 carlos-demo-build@aa_1.service start waiting\n", wantErr: true},
{name: "queued slice job defers", family: gv, loaded: gv, kn: sd, jobs: "13 carlos-demo-builds.slice stop running\n", wantErr: true},
+ {name: "truncated job list defers", family: gv, loaded: gv, kn: sd, jobs: strings.Repeat("1 other.service start waiting\n", systemctlCaptureLimit/30+1), wantErr: true},
{name: "stop fails", family: gv, loaded: gv, kn: sd, stopErr: errors.New("bus"), wantErr: true, wantStop: true},
{name: "still active after stop", family: gv, loaded: gv, kn: sd, active: "active", wantErr: true, wantStop: true},
} {
@@ -122,6 +126,10 @@ func TestReconcileBudget(t *testing.T) {
if tc.wantErr && !errors.Is(err, ErrBuild) && tc.stopErr == nil {
t.Fatalf("a deferral must be a retryable DEMO-BUILD: %v", err)
}
+ pending := tc.busy || tc.populated || tc.jobs != ""
+ if errors.Is(err, errBudgetPending) != pending {
+ t.Fatalf("pending=%v for %v", errors.Is(err, errBudgetPending), err)
+ }
if f.stopped() != tc.wantStop {
t.Fatalf("stopped=%v, calls %v", f.stopped(), f.calls)
}
@@ -135,3 +143,48 @@ func TestReconcileBudget(t *testing.T) {
})
}
}
+
+// A pending budget change keeps the job waiting, not failed: Build retries
+// admission until the job's deadline. Any other admission error fails at once.
+func TestBuildWaitsOutPendingBudget(t *testing.T) {
+ old := budgetRetry
+ budgetRetry = 5 * time.Millisecond
+ t.Cleanup(func() { budgetRetry = old })
+ pending := fmt.Errorf("%w: %w: test", ErrBuild, errBudgetPending)
+
+ w, j, priv, src := workerFixture(t)
+ tries := 0
+ w.admit = func(context.Context) error {
+ if tries++; tries < 3 {
+ return pending
+ }
+ return nil
+ }
+ ran := false
+ w.run = func(context.Context, string, Lock) (BuildOutput, error) { ran = true; return BuildOutput{}, nil }
+ wire, _ := SignJob(priv, j)
+ if _, err := w.Build(context.Background(), wire, bytes.NewReader(src)); err != nil || !ran || tries != 3 {
+ t.Fatalf("err=%v ran=%v tries=%d", err, ran, tries)
+ }
+
+ w, j, priv, src = workerFixture(t)
+ w.admit = func(context.Context) error { return pending }
+ w.run = func(context.Context, string, Lock) (BuildOutput, error) {
+ t.Fatal("ran while pending")
+ return BuildOutput{}, nil
+ }
+ wire, _ = SignJob(priv, j)
+ ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
+ defer cancel()
+ if _, err := w.Build(ctx, wire, bytes.NewReader(src)); !errors.Is(err, errBudgetPending) {
+ t.Fatalf("a budget still pending at the deadline: %v", err)
+ }
+
+ w, j, priv, src = workerFixture(t)
+ calls := 0
+ w.admit = func(context.Context) error { calls++; return ErrBuild }
+ wire, _ = SignJob(priv, j)
+ if _, err := w.Build(context.Background(), wire, bytes.NewReader(src)); !errors.Is(err, ErrBuild) || calls != 1 {
+ t.Fatalf("a real admission failure must not retry: %v after %d", err, calls)
+ }
+}
diff --git a/internal/demobuild/worker.go b/internal/demobuild/worker.go
index fc27ac43f..29c3c4cf2 100644
--- a/internal/demobuild/worker.go
+++ b/internal/demobuild/worker.go
@@ -215,17 +215,30 @@ func (w *Worker) Build(ctx context.Context, wire []byte, source io.Reader) (out
}
// Before admission: a budget the kernel does not have yet reaches the
// slice only while nothing runs in it (budget.go).
+ ctx, cancel := context.WithDeadline(ctx, j.Deadline)
+ defer cancel()
admit := w.admit
if admit == nil {
admit = w.reconcileBudget
}
- if err := admit(ctx); err != nil {
- return out, err
+ // A pending budget change waits for running work to finish; the job
+ // keeps its place until its own deadline rather than failing.
+ for {
+ err := admit(ctx)
+ if err == nil {
+ break
+ }
+ if !errors.Is(err, errBudgetPending) {
+ return out, err
+ }
+ select {
+ case <-ctx.Done():
+ return out, err
+ case <-time.After(budgetRetry):
+ }
}
w.buildPurge.RLock()
defer w.buildPurge.RUnlock()
- ctx, cancel := context.WithDeadline(ctx, j.Deadline)
- defer cancel()
select {
case w.slots <- struct{}{}:
defer func() { <-w.slots }()
diff --git a/internal/provision/testdata/plan14-root-reads-chain.tsv b/internal/provision/testdata/plan14-root-reads-chain.tsv
index 7d1eba50c..7d6f863ff 100644
--- a/internal/provision/testdata/plan14-root-reads-chain.tsv
+++ b/internal/provision/testdata/plan14-root-reads-chain.tsv
@@ -139,4 +139,4 @@ plan14-root-reads-dns-fresh-lookup-724.tsv 7ac2bde8301c37c7546e9aa19d375f1468fc8
plan14-root-reads-gvisor-update-dirs.tsv 1f1b2fdb569b7be42e75e10d1943d7e512708299364f912c2f72dccf1bd5b678
plan14-root-reads-region-ha-held-build.tsv beb085e4c6cdb3d430d12a8dc3c93f25cc3faf1aa7d62be37698ff9c9c82ecce
plan14-root-reads-fixedbox-health-alarms.tsv 0f7153571df8c1b4b50c31f57efcfdf60c71a12c80fd935fb3ea202fa0201dd5
-plan14-root-reads-demo-slice-adopt.tsv 762577dd5e472a076779f5e9dfd697034293a4f52667c42002671f5f887e474a
+plan14-root-reads-demo-slice-adopt.tsv 459de6e3054d1a93c07db305951e42ac3a3dcdf08ddd1d320a840ed835ef7b51
diff --git a/internal/provision/testdata/plan14-root-reads-demo-slice-adopt.tsv b/internal/provision/testdata/plan14-root-reads-demo-slice-adopt.tsv
index 4f484b755..bca83f8b8 100644
--- a/internal/provision/testdata/plan14-root-reads-demo-slice-adopt.tsv
+++ b/internal/provision/testdata/plan14-root-reads-demo-slice-adopt.tsv
@@ -1,13 +1,13 @@
# Census overlay demo-slice-adopt, written by census-propose (#369). census-ruling-demo-slice-adopt, approved on #280 before the merge; the approval card is docs/superpowers/notes/census-proposal-demo-slice-adopt.md.
# 1 body, 1 sites, 2 envelope: each changed unit re-frozen at its existing disposition with its claim appended.
# 2 new dispositions: 0 reads re-frozen at their disposition with the claim, 2 new units frozen accepted-legacy, 0 quiet units retired while they read nothing, 0 other.
-@legacy 3dbd76d6a8ba1381c12f564779a73a78b8221b4beebc16b67617f148fb047e54 internal/demobuild/worker.go Worker.Build legacy accepted-legacy accepted-legacy;rev3=census-ruling-agent-start-demo-1;basis=Holds the build/purge read lock through cleanup; existing bounded source read remains.;rev3=census-ruling-demo-slice-adopt;basis=Before admission, call the admit hook (nil = reconcileBudget). reconcileBudget reads the fixed build slice's systemctl show budget, its cgroup limit files and cgroup.events under the fixed root cgroup path, and list-jobs; only with buildPurge held exclusively, the slice empty and no build or slice job queued does it run systemctl stop on the fixed slice name. Otherwise it defers with DEMO-BUILD. No tenant, app or agent input names a path or unit. file:ReadAll@796 dc3a42e025fa666182ba082ac7af6866e2ed755979ea15da3f5412971e4bd4c6
+@legacy 43550b6e48cc4c57755d999ad44c7a7ed9369b6c53ce89fc59eb00c0e187ddab internal/demobuild/worker.go Worker.Build legacy accepted-legacy accepted-legacy;rev3=census-ruling-agent-start-demo-1;basis=Holds the build/purge read lock through cleanup; existing bounded source read remains.;rev3=census-ruling-demo-slice-adopt;basis=Before admission, call the admit hook (nil = reconcileBudget); a pending budget change (errBudgetPending) is retried every 2 s until the job deadline, any other error fails at once. reconcileBudget reads the fixed build slice's systemctl show budget, its cgroup limit files and cgroup.events under the fixed root cgroup path, and list-jobs (a listing at the capture limit defers); only with buildPurge held exclusively, the slice empty and no build or slice job queued does it run systemctl stop on the fixed slice name. No tenant, app or agent input names a path or unit. file:ReadAll@1087 be01e7c25a520d79132c28cb1bc6ac274139643981a5f7d415cd876e333c2da8
@legacy bf0fab994b0fd42b2af18b70e21c4b8cbe3b4dfe8e0e9988f246f0ea9275afb6 internal/demobuild/budget.go budgetSlicePopulated legacy accepted-legacy accepted-legacy;rev3=census-ruling-demo-slice-adopt;basis=New. Reads cgroup.events under the fixed build slice cgroup path (buildCgroupRoot); absent = empty; a file without a populated line refuses. file:ReadFile@55 60e72229ce907ec9eb953caf4d35c56416153a10f9e73520b55571c2e11b7ca2
@legacy 4b52c494b53f26d262697e10cfd31bad182f2c54c1b56e529b9b930fa439c936 internal/demobuild/budget.go kernelBudgetMatches legacy accepted-legacy accepted-legacy;rev3=census-ruling-demo-slice-adopt;basis=New. Reads memory.max, memory.swap.max, pids.max and cpu.max under the fixed build slice cgroup path and compares them with SlicePropertiesFor; absent cgroup = slice not running = match; read errors refuse. file:Lstat@168,file:ReadFile@591 fc138916d7510dd62066c8bafa97c8c4b7bf9a108537862f73f9bfbdcd2853c8
@legacy 55e6adc18f848f6049ffc4504debbacd1f5704dc1f9eac798bc74d2d09b83196 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. file:OpenFile@985,file:ReadFile@5626,file:ReadFile@5759,file:ReadFile@6647,file:ReadFile@6789,file:ReadFile@8548,file:Stat@5535 134e8105086fb29df32933f0ef26e664c0f08b7b8667a909838f2bde0c0fecfb
-internal/demobuild/worker.go Worker.Build legacy accepted-legacy accepted-legacy;rev3=census-ruling-agent-start-demo-1;basis=Holds the build/purge read lock through cleanup; existing bounded source read remains.;rev3=census-ruling-demo-slice-adopt;basis=Before admission, call the admit hook (nil = reconcileBudget). reconcileBudget reads the fixed build slice's systemctl show budget, its cgroup limit files and cgroup.events under the fixed root cgroup path, and list-jobs; only with buildPurge held exclusively, the slice empty and no build or slice job queued does it run systemctl stop on the fixed slice name. Otherwise it defers with DEMO-BUILD. No tenant, app or agent input names a path or unit. file:ReadAll@796 dc3a42e025fa666182ba082ac7af6866e2ed755979ea15da3f5412971e4bd4c6
+internal/demobuild/worker.go Worker.Build legacy accepted-legacy accepted-legacy;rev3=census-ruling-agent-start-demo-1;basis=Holds the build/purge read lock through cleanup; existing bounded source read remains.;rev3=census-ruling-demo-slice-adopt;basis=Before admission, call the admit hook (nil = reconcileBudget); a pending budget change (errBudgetPending) is retried every 2 s until the job deadline, any other error fails at once. reconcileBudget reads the fixed build slice's systemctl show budget, its cgroup limit files and cgroup.events under the fixed root cgroup path, and list-jobs (a listing at the capture limit defers); only with buildPurge held exclusively, the slice empty and no build or slice job queued does it run systemctl stop on the fixed slice name. No tenant, app or agent input names a path or unit. file:ReadAll@1087 be01e7c25a520d79132c28cb1bc6ac274139643981a5f7d415cd876e333c2da8
internal/demobuild/budget.go budgetSlicePopulated legacy accepted-legacy accepted-legacy;rev3=census-ruling-demo-slice-adopt;basis=New. Reads cgroup.events under the fixed build slice cgroup path (buildCgroupRoot); absent = empty; a file without a populated line refuses. file:ReadFile@55 60e72229ce907ec9eb953caf4d35c56416153a10f9e73520b55571c2e11b7ca2
internal/demobuild/budget.go kernelBudgetMatches legacy accepted-legacy accepted-legacy;rev3=census-ruling-demo-slice-adopt;basis=New. Reads memory.max, memory.swap.max, pids.max and cpu.max under the fixed build slice cgroup path and compares them with SlicePropertiesFor; absent cgroup = slice not running = match; read errors refuse. file:Lstat@168,file:ReadFile@591 fc138916d7510dd62066c8bafa97c8c4b7bf9a108537862f73f9bfbdcd2853c8
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. file:OpenFile@985,file:ReadFile@5626,file:ReadFile@5759,file:ReadFile@6647,file:ReadFile@6789,file:ReadFile@8548,file:Stat@5535 134e8105086fb29df32933f0ef26e664c0f08b7b8667a909838f2bde0c0fecfb
-@coverage internal/demobuild e00826f4e089661ac629756bc8daabd9cb7642ac6ca25790f30f58f98ac6a3d0
+@coverage internal/demobuild 64b77e1e6fd5dfa470fd4ab98f82b8b1df689ffde04eedbb757cb6ad627fac52
@coverage internal/host 88889b9888f3bec0a97e22581c486a81cd33951a8b2148299788ce5abe0512f6