carlos / platform Public

feat: share stateless secrets through coordinator authority

Keep serving leases authoritative for stateful escrow. Stateless followers restore and verify published secrets without minting or publishing; coordinator loss gates new writes without stopping web or workers.

Targeted escrow regressions, authority-loss mutation, approved copy checks and darwin/windows/linux-arm64 builds pass.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev bef340c738cc0c06870683cd0fbd1ce93d598f06 parent 8183571
6 files changed, +336 −7
  • approved_stateless_replicas_copy_test.go +1 −1
  • internal/activator/escrow.go +38 −6
  • internal/activator/escrow_authority.go +96 −0
  • internal/activator/logsink.go +3 −0
  • internal/activator/stateless.go +2 −0
  • internal/activator/stateless_escrow_test.go +196 −0

Intent

Human

<task-notification> <task-id>byp11fc9d</task-id> <tool-use-id>toolu_01EyX6ptca9K3vma1LjbqpUd</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/412bbef6-9468-4e8d-b9fe-5343bad21de8/tasks/byp11fc9d.output</output-file> <status>completed</status> <summary>Background command "Wait for 3a continuation 3 (Astra)" completed (exit code 0)</summary> </task-notification>

diff --git a/approved_stateless_replicas_copy_test.go b/approved_stateless_replicas_copy_test.go
index fb9b3df36..805589983 100644
--- a/approved_stateless_replicas_copy_test.go
+++ b/approved_stateless_replicas_copy_test.go
@@ -9,7 +9,7 @@ import (
const statelessReplicaCopyDeck = "docs/superpowers/plans/2026-11-01-stateless-replicas-copy.md"
-var statelessReplicaBuiltCopy = []string{"SR-1-C07", "SR-1-C01", "SR-1-C02", "SR-1-C03", "SR-1-C04", "SR-1-C05", "SR-1-C12", "SR-1-C09", "SR-1-C10", "SR-1-C13"}
+var statelessReplicaBuiltCopy = []string{"SR-1-C06", "SR-1-C07", "SR-1-C01", "SR-1-C02", "SR-1-C03", "SR-1-C04", "SR-1-C05", "SR-1-C12", "SR-1-C09", "SR-1-C10", "SR-1-C13"}
func statelessReplicaCopyRows(t *testing.T) []copyRow {
t.Helper()
diff --git a/internal/activator/escrow.go b/internal/activator/escrow.go
index 4820b5437..c163b8df6 100644
--- a/internal/activator/escrow.go
+++ b/internal/activator/escrow.go
@@ -689,7 +689,7 @@ func (b *ExecBackend) publishEscrowAt(ctx context.Context, rt registry.Route, p
if err := b.escrowConflict(rt.Host); err != nil {
return escrowPublish{exists: b.ledgerEscrowed(rt.Host)}, err
}
- if !b.holdsLease(rt.Host) {
+ if !b.holdsEscrowAuthority(rt) {
return escrowPublish{exists: b.ledgerEscrowed(rt.Host)}, errEscrowNoLease
}
lay, ok := pubLayout(p.layout)
@@ -810,7 +810,7 @@ func (b *ExecBackend) publishEscrowLocked(ctx context.Context, rt registry.Route
if escrowHooks.beforeCommit != nil {
escrowHooks.beforeCommit(rt.Host)
}
- if !b.holdsLease(rt.Host) {
+ if !b.holdsEscrowAuthority(rt) {
b.escrow.drop(st)
return out, errEscrowNoLease
}
@@ -996,7 +996,12 @@ func (b *ExecBackend) keepLedger(host string, st *escrowState, after hostfiles.B
// costs none after it (P2-6). The read is the preparation's, which the
// producers' mint decisions then share. A trio file that cannot be checked
// (a link, a FIFO) is not missing: there is nothing to restore into it.
-func (b *ExecBackend) restoreEscrowIfMissing(ctx context.Context, rt registry.Route, p instancePaths) error {
+func (b *ExecBackend) restoreEscrowIfMissing(ctx context.Context, rt registry.Route, p instancePaths) (err error) {
+ defer func() {
+ if rt.Stateless && err != nil {
+ err = fmt.Errorf("%w: %w", errStatelessSecretsWaiting, err)
+ }
+ }()
if err := b.escrowConflict(rt.Host); err != nil {
return err
}
@@ -1129,6 +1134,25 @@ func writeIfAbsent(dir, name string, data []byte) (bool, error) {
// as not ready does. fresh false shares the preparation's read; a mint
// outside a launch (the backfill) passes fresh.
func (b *ExecBackend) escrowMintGuard(ctx context.Context, rt registry.Route, fresh bool) mintGuard {
+ guard := b.escrowMintGuardWithAuthority(ctx, rt, fresh)
+ if !rt.Stateless {
+ return guard
+ }
+ return func(name string) error {
+ if !b.holdsEscrowAuthority(rt) {
+ return fmt.Errorf("%w: %w", errStatelessSecretsWaiting, ErrInstanceKeyDeferred)
+ }
+ if err := b.escrowReady(rt); err != nil {
+ return err
+ }
+ if guard != nil {
+ return guard(name)
+ }
+ return nil
+ }
+}
+
+func (b *ExecBackend) escrowMintGuardWithAuthority(ctx context.Context, rt registry.Route, fresh bool) mintGuard {
switch b.escrowModeOf(rt) {
case escrowNoStore:
return nil
@@ -1156,7 +1180,7 @@ func (b *ExecBackend) escrowMintGuard(ctx context.Context, rt registry.Route, fr
verdict = err
return
}
- if !b.holdsLease(rt.Host) {
+ if !b.holdsEscrowAuthority(rt) {
verdict = fmt.Errorf("%w: %w", ErrInstanceKeyDeferred, errEscrowNoLease)
return
}
@@ -1275,6 +1299,14 @@ func (b *ExecBackend) escrowReread(ctx context.Context, rt registry.Route, p ins
// failed write holds only a first launch (a host never escrowed).
func (b *ExecBackend) escrowedLaunchEnv(ctx context.Context, rt registry.Route) (map[string]string, instancekey.Layer, error) {
defer b.endPrep(rt.Host)
+ if rt.Stateless && !b.holdsEscrowAuthority(rt) {
+ return b.followerEscrowEnv(ctx, rt)
+ }
+ if rt.Stateless {
+ if err := b.escrowReady(rt); err != nil {
+ return nil, instancekey.LayerNone, err
+ }
+ }
for attempt := 0; ; attempt++ {
p := b.pathsFor(rt)
if err := b.escrowConflict(rt.Host); err != nil {
@@ -1299,7 +1331,7 @@ func (b *ExecBackend) escrowedLaunchEnv(ctx context.Context, rt registry.Route)
}
return nil, instancekey.LayerNone, fmt.Errorf("secrets for %s: %w", rt.Host, escrowKeylessRefusal(ready))
}
- if !b.holdsLease(rt.Host) {
+ if !b.holdsEscrowAuthority(rt) {
return nil, instancekey.LayerNone, fmt.Errorf("secrets for %s: %w", rt.Host, errEscrowNoLease)
}
c, err := b.captureLaunchEnv(rt)
@@ -1482,7 +1514,7 @@ func (b *ExecBackend) EscrowSweep(ctx context.Context, rows []registry.Route) Es
if ha, known := b.haKnown(rt); ha || !known {
continue
}
- if !b.holdsLease(rt.Host) {
+ if !b.holdsEscrowAuthority(rt) {
continue
}
seen[rt.Host] = true
diff --git a/internal/activator/escrow_authority.go b/internal/activator/escrow_authority.go
new file mode 100644
index 000000000..8034a1783
--- /dev/null
+++ b/internal/activator/escrow_authority.go
@@ -0,0 +1,96 @@
+package activator
+
+import (
+ "context"
+ "fmt"
+ "path/filepath"
+
+ "github.com/carlosframework/platform/internal/hostfiles"
+ "github.com/carlosframework/platform/internal/instancefs"
+ "github.com/carlosframework/platform/internal/instancekey"
+ "github.com/carlosframework/platform/internal/layout/pubclass"
+ "github.com/carlosframework/platform/internal/registry"
+)
+
+// holdsEscrowAuthority separates publication from the serving lifetime.
+// Stateless callers carry the selected generation's verified mode.
+func (b *ExecBackend) holdsEscrowAuthority(rt registry.Route) bool {
+ if !rt.Stateless {
+ return b.holdsLease(rt.Host)
+ }
+ a := b.coordinationAuthority(rt.Host)
+ if a == nil || a.Err() != nil {
+ return false
+ }
+ select {
+ case <-a.Lost():
+ return false
+ default:
+ return true
+ }
+}
+
+// followerEscrowEnv reads the published trio without opening a publisher.
+// Local files can supply editable env values, but never different secrets.
+func (b *ExecBackend) followerEscrowEnv(ctx context.Context, rt registry.Route) (map[string]string, instancekey.Layer, error) {
+ refuse := func(err error) (map[string]string, instancekey.Layer, error) {
+ return nil, instancekey.LayerNone, fmt.Errorf("%w: %w", errStatelessSecretsWaiting, err)
+ }
+ if err := b.escrowReady(rt); err != nil {
+ return refuse(err)
+ }
+ if err := b.escrowConflict(rt.Host); err != nil {
+ return refuse(err)
+ }
+ m, found, err := b.bucketManifest(ctx, rt, false)
+ if err != nil {
+ return refuse(err)
+ }
+ if !found {
+ return refuse(ErrEscrowNotReady)
+ }
+ p := b.pathsFor(rt)
+ lay, ok := pubLayout(p.layout)
+ if !ok {
+ return refuse(ErrEscrowNotReady)
+ }
+ seal, err := b.sealerFor(rt)
+ if err != nil {
+ return refuse(err)
+ }
+ var reads []escrowRead
+ for _, rel := range pubclass.SealedRels(lay, rt.Host) {
+ // Every member must be published and readable, even if it exists locally.
+ if _, ok := m.Platform[rel.Item()]; !ok {
+ return refuse(ErrEscrowNotReady)
+ }
+ if _, err := hostfiles.ReadSealed(ctx, b.FilesStore, b.filesPrefix(rt.Host), m, rel.Item(), nil, seal); err != nil {
+ return refuse(err)
+ }
+ }
+ if err := b.restoreEscrowIfMissing(ctx, rt, p); err != nil {
+ return refuse(err)
+ }
+ c, err := b.captureLaunchEnv(rt)
+ if err != nil {
+ return refuse(err)
+ }
+ for _, rel := range pubclass.SealedRels(lay, rt.Host) {
+ plain := c.own
+ if rel.Item() != hostfiles.SealedEnvItem {
+ plain, err = instancefs.ReadBeneathLimit(p.dataDir, filepath.FromSlash(rel.Rel()), haEnvReadLimit)
+ if err != nil {
+ return refuse(err)
+ }
+ }
+ reads = append(reads, escrowRead{rel: rel, plain: plain, stage: true})
+ }
+ diverged, err := b.escrowDivergence(ctx, rt, m, reads, seal)
+ if err != nil {
+ return refuse(err)
+ }
+ if len(diverged) > 0 {
+ return refuse(b.escrowConflictFound(rt, p, diverged))
+ }
+ return c.env, c.layer, nil
+}
diff --git a/internal/activator/logsink.go b/internal/activator/logsink.go
index 61d6f8e47..4c07b2449 100644
--- a/internal/activator/logsink.go
+++ b/internal/activator/logsink.go
@@ -77,6 +77,9 @@ var absPathToken = regexp.MustCompile(`(^|[\s"(=])(/[\w@.\-/]+)`)
// text itself originates in our own code (backends, materialize, lease) —
// this is a rendering pass, not a secrecy boundary.
func memberSafeWakeReason(err error) string {
+ if errors.Is(err, errStatelessSecretsWaiting) {
+ return errStatelessSecretsWaiting.Error()
+ }
if errors.Is(err, errStorageDeclaration) {
return errStorageDeclaration.Error()
}
diff --git a/internal/activator/stateless.go b/internal/activator/stateless.go
index 079efa1a1..d90cd2aba 100644
--- a/internal/activator/stateless.go
+++ b/internal/activator/stateless.go
@@ -66,6 +66,8 @@ func (b *ExecBackend) coordinationAuthority(host string) replicas.Authority {
return b.Coordination.HeldForHost(host)
}
+var errStatelessSecretsWaiting = errors.New("the new copy is waiting for the instance's shared secrets. existing copies keep serving")
+
var errStorageDeclaration = errors.New("could not verify this build's storage declaration, so it was not started")
func (a *Service) storageMode(ctx context.Context, rt registry.Route, id release.StagedIdentity) (bool, string, error) {
diff --git a/internal/activator/stateless_escrow_test.go b/internal/activator/stateless_escrow_test.go
new file mode 100644
index 000000000..67b537dbe
--- /dev/null
+++ b/internal/activator/stateless_escrow_test.go
@@ -0,0 +1,196 @@
+package activator
+
+import (
+ "context"
+ "errors"
+ "os"
+ "path/filepath"
+ "testing"
+
+ "github.com/carlosframework/platform/internal/registry"
+ "github.com/carlosframework/platform/internal/release"
+ "github.com/carlosframework/platform/internal/replicas"
+)
+
+func statelessEscrowCoordinator(t *testing.T, b *ExecBackend, rt registry.Route) replicas.Authority {
+ t.Helper()
+ b.Coordination = NewCoordinatorClient("box", "boot", func(context.Context, replicas.Identity, replicas.CoordinatorChange) (replicas.CoordinatorReply, error) {
+ return replicas.CoordinatorReply{Lease: replicas.Coordinator{Term: "term", Box: "box", BootID: "boot"}, RemainingMS: replicas.CoordinatorTTL.Milliseconds()}, nil
+ }, nil)
+ a, err := b.Coordination.Acquire(context.Background(), replicas.Identity{Account: rt.Account, App: rt.App, Host: rt.Host, Incarnation: "one"})
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() { b.Coordination.Close(context.Background()) })
+ return a
+}
+
+func statelessEscrowLaunch(b *ExecBackend, rt registry.Route) (map[string]string, error) {
+ ctx := context.Background()
+ defer b.endPrep(rt.Host)
+ if err := b.ensureAdminTokenFor(ctx, rt, b.pathsFor(rt)); err != nil {
+ return nil, err
+ }
+ if _, err := b.ensureInstanceKeyFor(ctx, rt, b.pathsFor(rt)); err != nil {
+ return nil, err
+ }
+ env, _, err := b.launchEnvLayerForMode(ctx, rt)
+ return env, err
+}
+
+func TestStatelessFollowerNeverMints(t *testing.T) {
+ t.Run("absent", func(t *testing.T) {
+ b, rt := escrowBackend(t, escrowHost)
+ rt.Stateless = true // The fixture still holds a serving lease: it must grant nothing.
+ count := &escrowCountingStore{Store: b.FilesStore}
+ b.FilesStore = count
+ _, err := statelessEscrowLaunch(b, rt)
+ if !errors.Is(err, errStatelessSecretsWaiting) {
+ t.Fatalf("cold follower did not wait for shared secrets: %v", err)
+ }
+ if envFileBytes(t, b, rt) != "" || count.puts.Load() != 0 {
+ t.Fatal("follower wrote secrets")
+ }
+ })
+ for _, scenario := range []string{"restore", "divergent", "failed-read", "partial"} {
+ t.Run(scenario, func(t *testing.T) {
+ b, rt := escrowBackend(t, escrowHost)
+ writeEnv(t, b, rt, envA)
+ if scenario != "partial" {
+ writeFiles(t, b, rt, map[string]string{"instance.key": "ik-secret", "report.token": "rt-secret"})
+ }
+ mustPublish(t, b, rt)
+ follower, frt := otherBox(t, b, rt)
+ frt.Stateless = true
+ if err := os.MkdirAll(follower.pathsFor(frt).dataDir, 0700); err != nil {
+ t.Fatal(err)
+ }
+ count := &escrowCountingStore{Store: follower.FilesStore}
+ follower.FilesStore = count
+ if scenario == "divergent" {
+ writeEnv(t, follower, frt, envA)
+ writeFiles(t, follower, frt, map[string]string{"instance.key": "different"})
+ }
+ if scenario == "failed-read" {
+ follower.FilesStore = &statelessReadFailure{Store: count}
+ }
+ env, err := statelessEscrowLaunch(follower, frt)
+ if scenario != "restore" {
+ if err == nil {
+ t.Fatal("unsafe follower launch accepted")
+ }
+ } else {
+ if err != nil {
+ t.Fatal(err)
+ }
+ if env["CARLOS_ADMIN_TOKEN"] != "tok-a" || env["CARLOS_INSTANCE_KEY"] != "key-a" {
+ t.Fatalf("wrong launch env: %v", env)
+ }
+ if envFileBytes(t, follower, frt) != envA {
+ t.Fatal("env was not restored identically")
+ }
+ for name, want := range map[string]string{"instance.key": "ik-secret", "report.token": "rt-secret"} {
+ raw, err := os.ReadFile(filepath.Join(follower.pathsFor(frt).dataDir, frt.Host, name))
+ if err != nil || string(raw) != want {
+ t.Fatalf("%s restore: %q, %v", name, raw, err)
+ }
+ }
+ }
+ if count.puts.Load() != 0 {
+ t.Fatal("follower published")
+ }
+ })
+ }
+ t.Run("failed-publish", func(t *testing.T) {
+ b, rt := escrowBackend(t, escrowHost)
+ rt.Stateless = true
+ statelessEscrowCoordinator(t, b, rt)
+ writeEnv(t, b, rt, envA)
+ writeFiles(t, b, rt, map[string]string{"instance.key": "ik-secret", "report.token": "rt-secret"})
+ store := &switchingPuts{Store: b.FilesStore}
+ store.failing.Store(true)
+ b.FilesStore = store
+ if _, err := statelessEscrowLaunch(b, rt); err == nil {
+ t.Fatal("first launch escaped failed publication")
+ }
+ })
+}
+
+type statelessReadFailure struct{ release.Store }
+
+func (s *statelessReadFailure) Get(context.Context, string) ([]byte, string, error) {
+ return nil, "", errors.New("read failed")
+}
+
+func TestStatelessEscrowAuthorityLoss(t *testing.T) {
+ t.Run("mint-and-publish", func(t *testing.T) {
+ b, rt := escrowBackend(t, escrowHost)
+ rt.Stateless = true
+ a := statelessEscrowCoordinator(t, b, rt)
+ writeEnv(t, b, rt, envA)
+ writeFiles(t, b, rt, map[string]string{"instance.key": "ik-secret", "report.token": "rt-secret"})
+ mustPublish(t, b, rt)
+ guard := b.escrowMintGuard(context.Background(), rt, false)
+ if err := guard("NEW_SECRET"); err != nil {
+ t.Fatal(err)
+ }
+ b.Coordination.Close(context.Background())
+ select {
+ case <-a.Lost():
+ default:
+ t.Fatal("authority not lost")
+ }
+ count := &escrowCountingStore{Store: b.FilesStore}
+ b.FilesStore = count
+ if err := guard("ANOTHER_SECRET"); err == nil {
+ t.Fatal("cached mint guard allowed mint after loss")
+ }
+ if _, err := b.publishEscrow(context.Background(), rt, nil); err == nil {
+ t.Fatal("publication allowed after loss")
+ }
+ b.EscrowSweep(context.Background(), []registry.Route{rt})
+ if count.puts.Load() != 0 {
+ t.Fatal("lost coordinator wrote escrow")
+ }
+ if _, err := statelessEscrowLaunch(b, rt); err != nil {
+ t.Fatalf("former coordinator cannot read its published secrets: %v", err)
+ }
+ })
+ t.Run("lost-before-commit", func(t *testing.T) {
+ b, rt := escrowBackend(t, escrowHost)
+ rt.Stateless = true
+ statelessEscrowCoordinator(t, b, rt)
+ writeEnv(t, b, rt, envA)
+ escrowHooks.beforeCommit = func(string) { b.Coordination.Close(context.Background()) }
+ t.Cleanup(func() { escrowHooks.beforeCommit = nil })
+ if _, err := statelessEscrowLaunch(b, rt); err == nil {
+ t.Fatal("first launch survived loss before commit")
+ }
+ if m := loadManifest(t, b, rt); len(m.Platform) != 0 {
+ t.Fatal("lost coordinator committed escrow")
+ }
+ })
+ t.Run("serving", func(t *testing.T) {
+ r := newWakeRelRig(t)
+ store := wireStatelessRig(r)
+ workers := &fakeWorkerBackend{killBackend: &killBackend{fakeBackend: r.b.fakeBackend}}
+ r.a.backend = &statelessWakeBackend{r.b, workers}
+ r.b.procs["v2"] = map[string]release.Process{release.ProcessWeb: {Command: []string{"serve"}}, "jobs": {Command: []string{"jobs"}}}
+ if err := waitErr(t, r.wake(r.a)); err != nil {
+ t.Fatal(err)
+ }
+ r.a.rootfsTick()
+ waitFor(t, "worker", func() bool { return workers.alive() == 1 })
+ r.a.mu.Lock()
+ web := r.a.entries[r.rt.Host].proc.(*fakeProc)
+ r.a.mu.Unlock()
+ store.held.lose(replicas.ErrFenced)
+ r.a.rootfsTick()
+ if err := waitErr(t, r.wake(r.a)); err != nil {
+ t.Fatal(err)
+ }
+ if workers.alive() != 1 || web.stopEntered() != 0 || len(r.b.started()) != 1 {
+ t.Fatal("coordination loss stopped serving processes")
+ }
+ })
+}