fix: poison demo builds only while a snapshot helper may run
Separate snapshot reaping from root and template readiness checks. Clear build poison after successful reap even when snapshot claims remain unavailable, and retain poison for unproven helper stops or scratch cleanup. Cover missing roots, template mismatches, listed helper failures, unmount failures and repair. Targeted demobuild and instancectl tests, go vet, gofmt and diff checks pass.
internal/instancectl/demo_snapshot.go+44 −25internal/instancectl/demo_snapshot_fix_linux_test.go+125 −1
CI failed — run details
Intent
<task-notification> <task-id>bvsqz7z4o</task-id> <tool-use-id>toolu_01SoNuLpzB1E21WCNcELxob8</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/22215d20-2b53-4378-8426-ae55e2362d13/tasks/bvsqz7z4o.output</output-file> <status>completed</status> <summary>Background command "S=/home/paulca/.cache/tmp/claude-1001/agent-start-demo-3; until [ -f $S/t3fix2.done ]; do sleep 30; done; tail -1 $S/t3fix2.log" completed (exit code 0)</summary> </task-notification>
Attributed to this commit: 7.33 g CO₂e (863,233 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on agent-start-demo-3, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.
diff --git a/internal/instancectl/demo_snapshot.go b/internal/instancectl/demo_snapshot.go| index eaff27c8a..f40633fdd 100644 |
| --- a/internal/instancectl/demo_snapshot.go |
| +++ b/internal/instancectl/demo_snapshot.go |
| @@ -416,6 +416,10 @@ func (p *demoSnapshotSupervisor) cleanup(id string) error { |
| return err |
| } |
| root, err := instancefs.OpenRootBeneath(p.root, ".") |
| + if errors.Is(err, os.ErrNotExist) { |
| + // Stop and unmount proof above still apply when scratch is absent. |
| + return nil |
| + } |
| if err != nil { |
| p.ready.Store(false) |
| return err |
| @@ -481,15 +485,7 @@ func (p *demoSnapshotSupervisor) ensureReady(ctx context.Context) bool { |
| defer func() { p.nextInit = time.Now().Add(5 * time.Second) }() |
| ctx, cancel := context.WithTimeout(ctx, p.stopWindow) |
| defer cancel() |
| - var err error |
| - if recovery, ok := p.s.DemoBuilder.(interface { |
| - RecoverSnapshots(context.Context, func(context.Context) error) error |
| - }); ok { |
| - err = recovery.RecoverSnapshots(ctx, p.initialize) |
| - } else { |
| - err = p.initialize(ctx) |
| - } |
| - if err != nil { |
| + if err := p.initialize(ctx); err != nil { |
| p.s.logf("instance-control: demo snapshot: %v", err) |
| return false |
| } |
| @@ -498,15 +494,23 @@ func (p *demoSnapshotSupervisor) ensureReady(ctx context.Context) bool { |
| func (p *demoSnapshotSupervisor) initialize(ctx context.Context) error { |
| p.ready.Store(false) |
| - root, err := instancefs.OpenRootBeneath(p.root, ".") |
| + var err error |
| + if recovery, ok := p.s.DemoBuilder.(interface { |
| + RecoverSnapshots(context.Context, func(context.Context) error) error |
| + }); ok { |
| + err = recovery.RecoverSnapshots(ctx, p.reap) |
| + } else { |
| + err = p.reap(ctx) |
| + } |
| if err != nil { |
| return err |
| } |
| - defer root.Close() |
| - st, err := root.Stat(".") |
| - if err != nil || st.Mode().Perm() != 0700 || !snapshotRootOwned(st) { |
| - return democlaim.ErrSnapshot |
| - } |
| + // Missing installation prerequisites refuse claims without blocking builds |
| + // once recovery has proved no helper or mounted scratch remains. |
| + return p.checkReadiness(ctx) |
| +} |
| + |
| +func (p *demoSnapshotSupervisor) reap(ctx context.Context) error { |
| raw, err := p.s.Systemctl(ctx, "list-units", "--all", "--plain", "--no-legend", "carlos-demo-snapshot@*.service") |
| if err != nil { |
| return err |
| @@ -528,19 +532,21 @@ func (p *demoSnapshotSupervisor) initialize(ctx context.Context) error { |
| ids[id] = true |
| } |
| dir, err := instancefs.OpenDirBeneath(p.root, ".") |
| - if err != nil { |
| - return err |
| - } |
| - entries, err := dir.ReadDir(-1) |
| - dir.Close() |
| - if err != nil { |
| + if err != nil && !errors.Is(err, os.ErrNotExist) { |
| return err |
| } |
| - for _, entry := range entries { |
| - if !demobuild.ValidJobID(entry.Name()) || !entry.IsDir() { |
| - return democlaim.ErrSnapshot |
| + if err == nil { |
| + entries, err := dir.ReadDir(-1) |
| + dir.Close() |
| + if err != nil { |
| + return err |
| + } |
| + for _, entry := range entries { |
| + if !demobuild.ValidJobID(entry.Name()) || !entry.IsDir() { |
| + return democlaim.ErrSnapshot |
| + } |
| + ids[entry.Name()] = true |
| } |
| - ids[entry.Name()] = true |
| } |
| for id := range ids { |
| if ctx.Err() != nil { |
| @@ -550,6 +556,19 @@ func (p *demoSnapshotSupervisor) initialize(ctx context.Context) error { |
| return err |
| } |
| } |
| + return ctx.Err() |
| +} |
| + |
| +func (p *demoSnapshotSupervisor) checkReadiness(ctx context.Context) error { |
| + root, err := instancefs.OpenRootBeneath(p.root, ".") |
| + if err != nil { |
| + return err |
| + } |
| + defer root.Close() |
| + st, err := root.Stat(".") |
| + if err != nil || st.Mode().Perm() != 0700 || !snapshotRootOwned(st) { |
| + return democlaim.ErrSnapshot |
| + } |
| // A valid, never-started instance resolves the installed template. Refuse |
| // capability when its effective restrictions or entry point are missing. |
| if err = p.assertUnit(ctx, strings.Repeat("A", 43)); err != nil { |
diff --git a/internal/instancectl/demo_snapshot_fix_linux_test.go b/internal/instancectl/demo_snapshot_fix_linux_test.go| index 3fe692d3f..207376907 100644 |
| --- a/internal/instancectl/demo_snapshot_fix_linux_test.go |
| +++ b/internal/instancectl/demo_snapshot_fix_linux_test.go |
| @@ -172,7 +172,7 @@ func TestDemoClaimSnapshotCleanStartup(t *testing.T) { |
| // Startup must recover even when no claim or readiness request arrives. |
| select { |
| case err := <-b.recovered: |
| - if err != nil || !f.p.ready.Load() { |
| + if err != nil { |
| t.Fatalf("clean startup recovery failed: %v", err) |
| } |
| case <-time.After(time.Second): |
| @@ -184,6 +184,10 @@ func TestDemoClaimSnapshotCleanStartup(t *testing.T) { |
| case <-time.After(time.Second): |
| t.Fatal("startup did not initialize builder") |
| } |
| + // Readiness checks run after the reap callback and before builder startup. |
| + if !f.p.ready.Load() { |
| + t.Fatal("clean startup did not establish snapshot readiness") |
| + } |
| c := &Client{Dial: func(ctx context.Context, _ string) (net.Conn, error) { |
| return (&net.Dialer{}).DialContext(ctx, "tcp", ln.Addr().String()) |
| }} |
| @@ -193,6 +197,126 @@ func TestDemoClaimSnapshotCleanStartup(t *testing.T) { |
| f.noScratch(t) |
| } |
| +// This spy makes build readiness depend on the exact callback result passed to |
| +// RecoverSnapshots. The demobuild tests exercise the real worker's admission. |
| +type snapshotRecoveryBuilder struct { |
| + *snapshotBuilderFake |
| + reapErr error |
| +} |
| + |
| +func (b *snapshotRecoveryBuilder) RecoverSnapshots(ctx context.Context, reap func(context.Context) error) error { |
| + b.reapErr = reap(ctx) |
| + return b.reapErr |
| +} |
| + |
| +func (b *snapshotRecoveryBuilder) Readiness(ctx context.Context) (demobuild.WorkerReady, error) { |
| + if b.reapErr != nil { |
| + return demobuild.WorkerReady{}, b.reapErr |
| + } |
| + return b.snapshotBuilderFake.Readiness(ctx) |
| +} |
| + |
| +func TestDemoClaimSnapshotRecoverySeparatesReadiness(t *testing.T) { |
| + for _, fault := range []string{"missingRoot", "rootMode", "unitProperties", "listedStop", "listedStopMissingRoot", "listedStopWrongMode", "unmount", "listUnits"} { |
| + t.Run(fault, func(t *testing.T) { |
| + f := newSnapshotFixture(t) |
| + b := &snapshotRecoveryBuilder{snapshotBuilderFake: f.builder, reapErr: errors.New("startup not reaped")} |
| + f.s.DemoBuilder = b |
| + f.p.ready.Store(false) |
| + f.p.stopWindow = 100 * time.Millisecond |
| + root := f.p.root |
| + missing := fault == "missingRoot" || fault == "listedStopMissingRoot" |
| + wrongMode := fault == "rootMode" || fault == "listedStopWrongMode" |
| + listed := strings.HasPrefix(fault, "listedStop") || fault == "unmount" |
| + if missing { |
| + f.p.root = filepath.Join(root, "missing") |
| + } |
| + if wrongMode { |
| + if err := os.Chmod(root, 0755); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + if fault == "unitProperties" { |
| + f.p.assertUnit = func(context.Context, string) error { return errors.New("unit property mismatch") } |
| + } |
| + if listed { |
| + f.failStop = fault != "unmount" |
| + } |
| + if fault == "unmount" { |
| + if err := os.Mkdir(filepath.Join(root, f.op), 0700); err != nil { |
| + t.Fatal(err) |
| + } |
| + f.p.unmount = func(context.Context, string) error { return errors.New("scratch still mounted") } |
| + } |
| + systemctl := f.s.Systemctl |
| + f.s.Systemctl = func(ctx context.Context, args ...string) (string, error) { |
| + if args[0] == "list-units" { |
| + if fault == "listUnits" { |
| + return "", errors.New("unit discovery failed") |
| + } |
| + if listed { |
| + return snapshotUnit(f.op) + " loaded active running", nil |
| + } |
| + } |
| + return systemctl(ctx, args...) |
| + } |
| + check := func(builds, snapshots bool) { |
| + t.Helper() |
| + out := httptest.NewRecorder() |
| + f.s.Handler().ServeHTTP(out, httptest.NewRequest("GET", "/demo-build/ready", nil)) |
| + if (out.Code == 200) != builds || (b.reapErr == nil) != builds { |
| + t.Fatalf("build readiness HTTP %d, reap error %v; want builds=%t", out.Code, b.reapErr, builds) |
| + } |
| + if f.p.ready.Load() != snapshots { |
| + t.Fatalf("snapshot ready=%t, want %t", f.p.ready.Load(), snapshots) |
| + } |
| + if builds { |
| + var ready demobuild.WorkerReady |
| + if err := json.Unmarshal(out.Body.Bytes(), &ready); err != nil { |
| + t.Fatal(err) |
| + } |
| + if (ready.ClaimCapability != "") != snapshots { |
| + t.Fatalf("claim capability %q, snapshot ready=%t", ready.ClaimCapability, snapshots) |
| + } |
| + } |
| + if !snapshots { |
| + out = httptest.NewRecorder() |
| + f.s.Handler().ServeHTTP(out, httptest.NewRequest("POST", "/demo-snapshot", nil)) |
| + if out.Code != 503 { |
| + t.Fatalf("snapshot HTTP %d, want 503", out.Code) |
| + } |
| + } |
| + } |
| + check(!listed && fault != "listUnits", false) |
| + if listed && f.stopped == 0 { |
| + t.Fatal("listed helper was not stopped before checking readiness") |
| + } |
| + f.mu.Lock() |
| + f.failStop = false |
| + f.mu.Unlock() |
| + f.p.unmount = func(context.Context, string) error { return nil } |
| + if fault == "listUnits" { |
| + f.s.Systemctl = systemctl |
| + } |
| + // Repair cleanup first, while any root/template readiness fault remains. |
| + f.p.nextInit = time.Time{} |
| + check(true, !missing && !wrongMode && fault != "unitProperties") |
| + if missing { |
| + if err := os.Mkdir(f.p.root, 0700); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + if err := os.Chmod(f.p.root, 0700); err != nil { |
| + t.Fatal(err) |
| + } |
| + f.p.assertUnit = func(context.Context, string) error { return nil } |
| + f.p.nextInit = time.Time{} |
| + check(true, true) |
| + f.noScratch(t) |
| + }) |
| + } |
| +} |
| + |
| func TestDemoClaimSnapshotSoftInitialization(t *testing.T) { |
| for _, fault := range []string{"missingRoot", "rootMode", "unexpectedEntry", "cleanup", "unitProperties"} { |
| t.Run(fault, func(t *testing.T) { |