feat: queue demo builds through app change indexes
Add the signed job wire contract and protocol refusal tests. Task 5 remains incomplete: integration needs clarification of the prohibition on editing Task 3 files, including the required agent.go and apidemo.go wiring. No queue, worker or publication path is enabled by this commit.
internal/demobuild/job.go+132 −0internal/demobuild/job_test.go+100 −0
Intent
<task-notification> <task-id>bqtoc6lu5</task-id> <tool-use-id>toolu_01CPxv9LPnEVK8Eibwtd2Eiv</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/cd1b6de2-ac77-426f-a77d-fc5436969c88/tasks/bqtoc6lu5.output</output-file> <status>completed</status> <summary>Background command "Wait for Task 4 fix" completed (exit code 0)</summary> </task-notification>
<task-notification> <task-id>aa76a746e742bb467</task-id> <tool-use-id>toolu_019rv17BNKrZF1g1MS172Hcn</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/cd1b6de2-ac77-426f-a77d-fc5436969c88/tasks/aa76a746e742bb467.output</output-file> <status>completed</status> <summary>Agent "Re-review Task 4 fix round 1" finished</summary> <note>A task-notification fires each time this agent stops with no live background children of its own. The user can send it another message and resume it, so the same task-id may notify more than once.</note> <result>- **P2-1: ADDRESSED.** `internal/activator/wakeidentity.go:240` now refuses a wake when the row or the record carries a demo descriptor and the two do not match. `wakeidentity.go:112` adds the four Demo fields to the under-lease route comparison. - **P2-2: NOT ADDRESSED** (`internal/activator/demo_durability.go:128-136`, `:170-174`, `:189-191`). - **What is fixed:** a transient bucket or disk error during first init now retries cleanly, and a lost disk still refuses. - **Not left behind:** after a successful init, `finishDemoInit` (`:212`, `:219-228`) removes the pending file and syncs the removal before any start. - **But the app can recreate it.** The pending file sits in the instance directory, which the tenant can write (`instancectl/gen.go:70` ReadWritePaths). The app can learn both proof values: - the DemoID, which the console API returns to the demo's creator; - the assignment, which is in plain text in the marker file next to the database. - **How a reseed happens:** the app writes a valid pending file and deletes its database. On the next wake the remote create-only marker is accepted (`:189`) and an empty database is placed. Only `rt.GoodVersion != ""` (`:167`) would stop this, and I could not confirm from the diff that demo rows ever get GoodVersion set. - **P3-3: ADDRESSED.** `demo_durability.go:167` drops the `rt.Moved` check, and `demoDataShape` still refuses moved rows at `:63`. **New findings:** - **N-1 (P2): a tenant-written pending file reseeds an established demo.** This is the P2-2 gap above. Fault family: trust boundary. A tenant-writable input now grants an agent decision, so the remote marker no longer fails closed. The damage stays inside the demo's own data. - **Fix:** keep the pending proof where only the agent can write and which a lost disk also loses. Options: a carlos-owned 0700 directory outside `/data/instances/<iid>`, or a column on the agent's registry row. - **Test to add:** finish init, plant a valid pending file, delete the database, assert DEMO-DATA. - **N-2 (P3): a garbage pending file planted by the tenant blocks its own demo's wakes.** It harms only that demo, and the N-1 fix removes it. **Census:** there is one new read from tenant-writable input, the `<db>.demo-init-pending` file. It goes through `instancefs.ReadBeneathLimitOrAbsent` (no symlinks, bounded size) in the agent process (carlos-edge.service, `User=carlos`). No root unit gains a read. The problem is the decision that read authorizes, not root exposure. **Verdict: READY AFTER FIXES** Review written to `/home/paulca/amadan.net/carlos/platform/.claude/worktrees/agent-start-demo-1/.superpowers/sdd/2026-11-02-agent-start-demo-1-anonymous-apps/task-4-review-r2.md`.</result> <usage><subagent_tokens>73023</subagent_tokens><tool_uses>12</tool_uses><duration_ms>172787</duration_ms></usage> </task-notification>
<task-notification> <task-id>a670daa9d6b27b001</task-id> <tool-use-id>toolu_0117NjNXjUqQK6gz6x4xU49C</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/cd1b6de2-ac77-426f-a77d-fc5436969c88/tasks/a670daa9d6b27b001.output</output-file> <status>completed</status> <summary>Agent "Re-review Task 3 fix round 1" finished</summary> <note>A task-notification fires each time this agent stops with no live background children of its own. The user can send it another message and resume it, so the same task-id may notify more than once.</note> <result>- P1-1: ADDRESSED in intent (`demo.conf:8`, `instancectl/demo_policy.go:55-75`). The adversarial pass found no other path a demo could use to resolve names or relay traffic: - Abstract sockets are closed by the private network namespace (read back at `:50-54`). - Host `/proc/<pid>/root` is closed by `PrivatePIDs`, `ProtectProc` and `ProcSubset`. - The instance's own control socket serves only purge, schedule and updates. - Inherited fds are `/dev/null` and the log socket. - The protection does not work yet, because of P1-A. - P2-1: ADDRESSED (`host/demo_policy.go:64-67`; `AssertDemoRoute` returns early at `provision/demo_policy.go:77-79`). - P2-2: ADDRESSED (`host/demo_policy_linux_test.go:43-340`). Gaps go in P3-B. - R14: ADDRESSED (`demo/descriptor.go:44-57`, `host/claimunit.go:32,81`). Every box-mode check goes through `BoxConfig`. - P3-1: ADDRESSED (`instancectl/demo_policy_test.go:24`). - P3-2: ADDRESSED (`instancectl/demo_policy.go:37,50-54`, `demo.conf:9`). - P3-3: ADDRESSED (`host/claimunit.go:35-38,164-172`). - P3-4: ADDRESSED (`host/demo_policy.go:35-42`). - P3-5: ADDRESSED as asked, but the fix creates P2-A (`hostenv/hostenv.go:78-81`). - P3-6: ADDRESSED (`assertDemoPolicy` and its calls removed from `server.go` and `gen.go`). New findings in the fix diff: - **P1-A:** The new `TemporaryFileSystem=/run:ro` hides `/run/carlos-control`. The templates still list that path in `InaccessiblePaths` with no `-` prefix (`carlos-instance@.service:96`, gen `:69`). That very likely fails namespace setup, so every demo unit would fail to start (226/NAMESPACE). This comes from reasoning about systemd, not a run: this sandbox has no systemd I could drive. Fix: in the drop-in, reset the list and restate it as `InaccessiblePaths=/etc/carlos -/run/carlos-control`, then run the gated witness once. - **P2-A:** Adding `CARLOS_DEMO_BOX` and the tuple to `hostenv.Optional` breaks that package's own rule that keys selecting a box's authority stay out of the list (`hostenv.go:52-55`, the same reason `CARLOS_FLEET` is absent). Set at region or shared scope, the switch turns every box there into a demo box, which is a region-wide outage. Removing it at that scope silently turns demo boxes ordinary. - **P3-A:** Ordinary boxes now try to delete two fixed drop-in paths on every bootstrap, unit repair and apply. ENOENT is harmless, but any other error fails an install that used to be a no-op. Apart from that, ordinary boxes are byte-for-byte unchanged: same units, same control unit, same behaviour for routes with no demo marker. - **P3-B:** The witness does not test abstract sockets, the fixture's open fds, or sockets outside `/run` and `/var`. - **P3-C:** The readback does not catch a later `BindPaths`/`BindReadOnlyPaths` drop-in that remounts `/run/dbus` or `/run/systemd/resolve`. Only root writes drop-ins, so this is defence in depth. - **P3-D:** No defect. `ConfigGate` is now always a non-nil wrapper, and the outcome for ordinary routes is the same. Census: the fix diff gives root no new input from anything a tenant, app or box agent can write. The one widening is P2-A, which lets operator-written parameters set a box-authority key. Whether the box role can write those parameter paths is unverified. Review written to `/home/paulca/amadan.net/carlos/platform/.claude/worktrees/agent-start-demo-1/.superpowers/sdd/2026-11-02-agent-start-demo-1-anonymous-apps/task-3-review-r2.md`. ### Verdict: NOT READY (P1s listed) - P1-A: the `/run` tmpfs plus the non-optional `InaccessiblePaths=/run/carlos-control` very likely stops every demo unit from starting.</result> <usage><subagent_tokens>136665</subagent_tokens><tool_uses>21</tool_uses><duration_ms>479300</duration_ms></usage> </task-notification>
Attributed to this commit: 10.9 g CO₂e (1,297,688 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-1, 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/demobuild/job.go b/internal/demobuild/job.go| new file mode 100644 |
| index 000000000..0e395c859 |
| --- /dev/null |
| +++ b/internal/demobuild/job.go |
| @@ -0,0 +1,132 @@ |
| +package demobuild |
| + |
| +import ( |
| + "bytes" |
| + "crypto/ed25519" |
| + "crypto/rand" |
| + "encoding/base64" |
| + "encoding/hex" |
| + "encoding/json" |
| + "errors" |
| + "io" |
| + "strings" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/demo" |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +const ( |
| + QueueLimit = 16 |
| + PickupWindow = 30 * time.Second |
| + BuildWindow = 180 * time.Second |
| + ResultGrace = 15 * time.Second |
| + MaxJobWire = 8192 |
| + jobDomain = "carlos demo-build v1\n" |
| +) |
| + |
| +var ErrJob = errors.New("demo-build: invalid or expired job") |
| + |
| +type Job struct { |
| + ID string `json:"id"` |
| + DemoID string `json:"demo_id"` |
| + Account string `json:"account"` |
| + App string `json:"app"` |
| + Host string `json:"host"` |
| + Lifetime string `json:"lifetime"` |
| + Fleet string `json:"fleet"` |
| + Box string `json:"box"` |
| + SourceSHA256 string `json:"source_sha256"` |
| + BundleSHA256 string `json:"bundle_sha256"` |
| + Generation uint64 `json:"generation"` |
| + Attempt uint32 `json:"attempt"` |
| + Deadline time.Time `json:"deadline"` |
| +} |
| + |
| +type Result struct { |
| + JobID string `json:"job_id"` |
| + Attempt uint32 `json:"attempt"` |
| + SourceSHA256 string `json:"source_sha256"` |
| + BundleSHA256 string `json:"bundle_sha256"` |
| + BinaryPath string `json:"-"` |
| + BinarySHA256 string `json:"binary_sha256"` |
| + BinarySize int64 `json:"binary_size"` |
| + Diagnostics []Diagnostic `json:"diagnostics"` |
| +} |
| + |
| +func NewJobID() (string, error) { |
| + var b [32]byte |
| + if _, err := rand.Read(b[:]); err != nil { |
| + return "", err |
| + } |
| + return base64.RawURLEncoding.EncodeToString(b[:]), nil |
| +} |
| + |
| +func ValidJobID(id string) bool { |
| + b, err := base64.RawURLEncoding.Strict().DecodeString(id) |
| + return err == nil && len(b) == 32 && base64.RawURLEncoding.EncodeToString(b) == id |
| +} |
| + |
| +func ValidDigest(s string) bool { |
| + b, err := hex.DecodeString(s) |
| + return err == nil && len(b) == 32 && hex.EncodeToString(b) == s |
| +} |
| + |
| +func (j Job) validate() error { |
| + d := demo.Descriptor{ID: j.DemoID, Host: j.Host, Lifetime: j.Lifetime, ExpiresAt: j.Deadline.Unix()} |
| + if !ValidJobID(j.ID) || demo.ValidateBinding(d, j.Account, j.App, j.Host, j.Lifetime, j.Fleet) != nil || |
| + !registry.ValidTunnelName(j.Box) || !ValidDigest(j.SourceSHA256) || !ValidDigest(j.BundleSHA256) || |
| + j.Generation == 0 || j.Attempt == 0 || j.Deadline.IsZero() || len(j.Lifetime) > 128 || strings.ContainsAny(j.Lifetime, "/\\\x00") { |
| + return ErrJob |
| + } |
| + return nil |
| +} |
| + |
| +type signedJob struct { |
| + Payload string `json:"payload"` |
| + Sig string `json:"sig"` |
| +} |
| + |
| +func SignJob(priv ed25519.PrivateKey, j Job) ([]byte, error) { |
| + if len(priv) != ed25519.PrivateKeySize || j.validate() != nil { |
| + return nil, ErrJob |
| + } |
| + b, err := json.Marshal(j) |
| + if err != nil { |
| + return nil, err |
| + } |
| + p := base64.StdEncoding.EncodeToString(b) |
| + return json.Marshal(signedJob{Payload: p, Sig: base64.StdEncoding.EncodeToString(ed25519.Sign(priv, []byte(jobDomain+p)))}) |
| +} |
| + |
| +func VerifyJob(pub ed25519.PublicKey, wire []byte, now time.Time) (Job, error) { |
| + var envelope signedJob |
| + if len(pub) != ed25519.PublicKeySize || len(wire) > MaxJobWire || strictJobJSON(wire, &envelope) != nil { |
| + return Job{}, ErrJob |
| + } |
| + sig, err := base64.StdEncoding.Strict().DecodeString(envelope.Sig) |
| + if err != nil || !ed25519.Verify(pub, []byte(jobDomain+envelope.Payload), sig) { |
| + return Job{}, ErrJob |
| + } |
| + payload, err := base64.StdEncoding.Strict().DecodeString(envelope.Payload) |
| + if err != nil { |
| + return Job{}, ErrJob |
| + } |
| + var j Job |
| + if strictJobJSON(payload, &j) != nil || j.validate() != nil || !now.Before(j.Deadline) { |
| + return Job{}, ErrJob |
| + } |
| + return j, nil |
| +} |
| + |
| +func strictJobJSON(b []byte, v any) error { |
| + if !uniqueJSONKeys(json.NewDecoder(bytes.NewReader(b))) { |
| + return ErrJob |
| + } |
| + d := json.NewDecoder(bytes.NewReader(b)) |
| + d.DisallowUnknownFields() |
| + if d.Decode(v) != nil || d.Decode(new(any)) != io.EOF { |
| + return ErrJob |
| + } |
| + return nil |
| +} |
diff --git a/internal/demobuild/job_test.go b/internal/demobuild/job_test.go| new file mode 100644 |
| index 000000000..625f27939 |
| --- /dev/null |
| +++ b/internal/demobuild/job_test.go |
| @@ -0,0 +1,100 @@ |
| +package demobuild |
| + |
| +import ( |
| + "bytes" |
| + "crypto/ed25519" |
| + "crypto/rand" |
| + "encoding/base64" |
| + "encoding/json" |
| + "strings" |
| + "testing" |
| + "time" |
| +) |
| + |
| +func TestDemoJobSignature(t *testing.T) { |
| + pub, priv, err := ed25519.GenerateKey(rand.Reader) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + now := time.Now().UTC() |
| + j := Job{ID: base64.RawURLEncoding.EncodeToString(bytes.Repeat([]byte{1}, 32)), DemoID: base64.RawURLEncoding.EncodeToString(bytes.Repeat([]byte{2}, 16)), Account: "shop", App: "tmp-demo-bright-otter-42", Host: "bright-otter-42.demo.example.com", Lifetime: "abcdef0123456789", Fleet: "demo", Box: "box-1", SourceSHA256: strings.Repeat("a", 64), BundleSHA256: strings.Repeat("b", 64), Generation: 1, Attempt: 1, Deadline: now.Add(30 * time.Second)} |
| + wire, err := SignJob(priv, j) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + got, err := VerifyJob(pub, wire, now) |
| + if err != nil || got != j { |
| + t.Fatalf("round trip: %+v %v", got, err) |
| + } |
| + if _, err := VerifyJob(pub, wire, j.Deadline); err == nil { |
| + t.Fatal("accepted at deadline") |
| + } |
| + if _, err := VerifyJob(nil, wire, now); err == nil { |
| + t.Fatal("accepted without key") |
| + } |
| + for _, mutate := range []func(*Job){ |
| + func(j *Job) { j.ID = "../escape" }, |
| + func(j *Job) { j.DemoID = "invalid" }, |
| + func(j *Job) { j.Account = "../shop" }, |
| + func(j *Job) { j.App = "shop" }, |
| + func(j *Job) { j.Host = "other.demo.example.com" }, |
| + func(j *Job) { j.Box = "../box" }, |
| + func(j *Job) { j.Fleet = "../demo" }, |
| + func(j *Job) { j.Lifetime = "" }, |
| + func(j *Job) { j.Generation = 0 }, |
| + func(j *Job) { j.Attempt = 0 }, |
| + func(j *Job) { j.SourceSHA256 = strings.Repeat("A", 64) }, |
| + func(j *Job) { j.BundleSHA256 = "" }, |
| + func(j *Job) { j.Deadline = time.Time{} }, |
| + } { |
| + bad := j |
| + mutate(&bad) |
| + if _, err := SignJob(priv, bad); err == nil { |
| + t.Fatalf("signed invalid job: %+v", bad) |
| + } |
| + } |
| + var envelope map[string]string |
| + if err := json.Unmarshal(wire, &envelope); err != nil { |
| + t.Fatal(err) |
| + } |
| + // Signatures from other platform protocols cannot authorize a build. |
| + envelope["sig"] = base64.StdEncoding.EncodeToString(ed25519.Sign(priv, []byte("carlos other v1\n"+envelope["payload"]))) |
| + bad, _ := json.Marshal(envelope) |
| + if _, err := VerifyJob(pub, bad, now); err == nil { |
| + t.Fatal("accepted wrong signature domain") |
| + } |
| + // Even a valid signature cannot add an executable input to the schema. |
| + payload, _ := json.Marshal(j) |
| + payload = append(payload[:len(payload)-1], []byte(`,"shell":"sh"}`)...) |
| + envelope["payload"] = base64.StdEncoding.EncodeToString(payload) |
| + envelope["sig"] = base64.StdEncoding.EncodeToString(ed25519.Sign(priv, []byte("carlos demo-build v1\n"+envelope["payload"]))) |
| + bad, _ = json.Marshal(envelope) |
| + if _, err := VerifyJob(pub, bad, now); err == nil { |
| + t.Fatal("accepted shell field") |
| + } |
| + for _, bad := range [][]byte{ |
| + append(append([]byte{}, wire...), []byte(` {}`)...), |
| + append([]byte(`{"payload":"duplicate",`), wire[1:]...), |
| + append([]byte(`{"Payload":"duplicate",`), wire[1:]...), |
| + append([]byte(`{"path":"/bin/sh",`), wire[1:]...), |
| + bytes.Repeat([]byte{' '}, MaxJobWire+1), |
| + } { |
| + if _, err := VerifyJob(pub, bad, now); err == nil { |
| + t.Fatal("accepted ambiguous, extended or oversized envelope") |
| + } |
| + } |
| +} |
| + |
| +func TestDemoJobResultHasNoWirePath(t *testing.T) { |
| + r := Result{JobID: "job", BinaryPath: "/console/local/spool"} |
| + b, err := json.Marshal(r) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if bytes.Contains(b, []byte(r.BinaryPath)) { |
| + t.Fatal("spool path leaked onto wire") |
| + } |
| + if strictJobJSON([]byte(`{"job_id":"job","binary_path":"/chosen/path"}`), new(Result)) == nil { |
| + t.Fatal("accepted a remote spool path") |
| + } |
| +} |