carlos / platform Public

replicas: hold a kept answer's fence until its ACK (#511)

Drill 1 review round 1, Astra A1.

A permitted step that answers kept for want of the shared secrets
lifted its stop fence before the answer was acknowledged. If the escrow
became readable while the ACK was delayed, a resident wake could start
the target under the still-open permit, and the kept ACK then cleared
the permit without the box in Completed: a running target the record
says the box never switched to.

The fence of a kept answer is now owned by its permit
(replicaKeptOwner) and kept after the proven stop. SetReplicaDeploy
lifts it once the record no longer holds that permit open for this box
(the ACK committed, or the permit changed). A repeated step for the
same permit finds its fence held and answers kept again. Every other
answer lifts its fence at once, as before.

Witness: TestReplicaKeptFenceHeldUntilAck (kept answer, escrow repaired
while the ACK is paused, cold wake refused by the fence and nothing
started; ACK committed, fence lifted, the record refuses the target).
Without the fix the cold wake starts the target.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 7e27ab0b2042dc6f23c9c425e509e1b17cf61a37 parent f32f470
2 files changed, +120 −5
  • internal/activator/replica_drill1_test.go +69 −0
  • internal/activator/replicadeploy.go +51 −5

Intent

Human

<task-notification> <task-id>a3447eef93b8e73dc</task-id> <tool-use-id>toolu_01Kp3SwdeDcvQV5zoPNCXy6r</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/72128d15-58b8-452f-9f7e-8e864e8854d2/tasks/a3447eef93b8e73dc.output</output-file> <status>completed</status> <summary>Agent "Fix drill-1 findings on Plan 4" 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>Status: DONE. R21b is built. A joining copy now gets exactly the shared files that exist, and both waits I flagged are bounded. **Commits** (on 66095ed9, not pushed; tip f32f4704): - 78966b17 escrow: the coordinator records instance.key and report.token as absent when the app never wrote them, and a joining copy gets exactly what exists - 341d712e replicas: bound the config wait and the shared-secrets wait - 3dc323fb copy: SR-4-C83 reworded to cover both cases, SR-4-C89 updated, new SR-4-C90 - f32f4704 census: re-proposed as stateless-replicas-4-drill1; `make census-card` is clean **Escrows from older builds:** they stay incomplete, as today, until the coordinator's next publish under its term. That is its next launch, which covers the release pass, or, if nothing changed on disk, its next 30-second escrow sweep. I added the sweep so a coordinator that is already running upgrades without waiting for a deploy. **Bounds:** - **Config wait:** `releaseConfigBound` = `residentPoll` + `workerStartBound` (about 32 s), the same bound a copy that cannot start already gets. Past it, the attempt is recorded failed with nothing run, with reason SR-4-C90. - **New box after a done deploy:** `replicaRestartBound`, counted from the box's first refusal. Past it, the box refuses with the R21 message (SR-4-C83) and asks again every 30 s. **Tests:** `TestStatelessEscrowRecordsAbsence` covers your three cases (app never writes the files, app writes them, listed file unreadable) plus the upgrade of an older escrow. `TestReleaseConfigWaitHeldAsFailed` and `TestDoneCopyRefusesWithoutSecrets` cover the bounds. Each new test fails without its fix. The targeted suites, the publication guard, the census tests, the HA guards and the three cross-compiles all pass. **Concerns:** - **No single test reaches 2 of 2 for the never-writes app.** The escrow test shows the joining copy gets complete secrets; the earlier `TestReplicaJoinAfterCopiesRaised` shows the permit-and-ACK half reaching 2 of 2. No test runs both together. - **A file written after it was recorded absent:** a copy that joins within the next 30 s gets no instance.key, and its app may write its own. The existing mismatch check then stops that copy's next launch with an alert for an operator. This window comes with the ruling. - **The 32 s config bound is short:** a slow config pass on a loaded box could hold a first deploy as failed. A retry recovers it. Report (addendum appended): /home/paulca/amadan.net/carlos/platform/.claude/worktrees/stateless-replicas-4/.superpowers/sdd/2026-11-01-stateless-replicas-4-deploy-and-drill/task-drill1-report.md</result> <usage><subagent_tokens>673280</subagent_tokens><tool_uses>286</tool_uses><duration_ms>5121509</duration_ms></usage> </task-notification>

Human

<task-notification> <task-id>blfqev3nx</task-id> <tool-use-id>toolu_01DSPQbm2sn2TM2ukFfHSeUN</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/72128d15-58b8-452f-9f7e-8e864e8854d2/tasks/blfqev3nx.output</output-file> <status>completed</status> <summary>Background command "Astra census review of drill-1 card" completed (exit code 0)</summary> </task-notification>

Human

<task-notification> <task-id>b8nx61anl</task-id> <tool-use-id>toolu_011vULmHcGu5euqpUundHLXs</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/72128d15-58b8-452f-9f7e-8e864e8854d2/tasks/b8nx61anl.output</output-file> <status>completed</status> <summary>Background command "Fable review of drill-1 fixes" completed (exit code 0)</summary> </task-notification>

Human

<task-notification> <task-id>bxr60723o</task-id> <tool-use-id>toolu_019em8DZAPCyytHyqbBEoFzm</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/72128d15-58b8-452f-9f7e-8e864e8854d2/tasks/bxr60723o.output</output-file> <status>completed</status> <summary>Background command "Astra review of drill-1 fixes" completed (exit code 0)</summary> </task-notification>

Human

<task-notification> <task-id>bfsq0b8qw</task-id> <tool-use-id>toolu_01FsRcgUUCUiY5cFZ8mEEN44</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/72128d15-58b8-452f-9f7e-8e864e8854d2/tasks/bfsq0b8qw.output</output-file> <status>completed</status> <summary>Background command "Wait for CI on f32f4704" completed (exit code 0)</summary> </task-notification>

diff --git a/internal/activator/replica_drill1_test.go b/internal/activator/replica_drill1_test.go
index b1b8ae416..8550a434a 100644
--- a/internal/activator/replica_drill1_test.go
+++ b/internal/activator/replica_drill1_test.go
@@ -454,3 +454,72 @@ func TestDoneCopyRefusesWithoutSecrets(t *testing.T) {
t.Fatalf("the refusal is asked again after %s", d)
}
}
+
+// TestReplicaKeptFenceHeldUntilAck (#511 drill 1 round 1, Astra A1): the
+// fence of a kept answer is owned by its permit and held until the ACK
+// commits. Between the answer and the ACK the escrow is repaired and a
+// cold wake tries the target: it is refused and nothing starts. Once the
+// ACK commits (the record no longer holds the permit open for b) the
+// fence is lifted, and b, not switched, is not allowed the target.
+func TestReplicaKeptFenceHeldUntilAck(t *testing.T) {
+ join := func(inc string) *replicas.Deploy {
+ return &replicas.Deploy{ID: "deploy-v2", Incarnation: inc, Target: replicaTarget, BaselineSet: true, LastGood: replicaTarget, Phase: "released", Term: "term", Revision: 5,
+ Swap: replicas.Swap{Order: []string{"a", "b"}, Completed: []string{"a"}, ActiveBox: "b", Permit: 5}}
+ }
+ r := newReplicaSetRig(t, "b", join)
+ r.rt.Hibernate = false
+ r.routes.set(r.rt)
+ r.a.backend = &secretsRefusingBackend{r.b}
+ wake := func() error {
+ t.Helper()
+ // The resident backoff of the earlier refusals is not under test.
+ r.a.mu.Lock()
+ if st := r.a.residents[r.rt.Host]; st != nil {
+ st.retryAt, st.waitUntil = time.Time{}, time.Time{}
+ }
+ r.a.mu.Unlock()
+ r.setPlan(WakePlan{Boot: r.next, Mode: ModeOverlap, Token: 2})
+ err := waitErr(t, r.wake(r.a))
+ if r.started("v2") || r.a.hasEntry(r.rt.Host) {
+ t.Fatal("the target started")
+ }
+ return err
+ }
+ at := Attempt{Host: r.rt.Host, Cause: CauseRelease, Build: "v2", StagedPath: r.next.StagedPath, Mode: ModeOverlap, SchedRev: 1, ProofOnly: true}
+ step := func() SwapResult {
+ return r.a.ReplicaSwap(context.Background(), at, *r.store.snapshot(), "b", newestOne)
+ }
+ _ = wake()
+ _ = step()
+ r.clk.advance(replicaRestartBound + time.Second)
+ _ = wake()
+ if res := step(); !res.Proven || res.WebFailed || res.Reason != replicaSecretsRefusal {
+ t.Fatalf("not answered kept: %+v", res)
+ }
+ // The ACK is delayed. The escrow is repaired meanwhile.
+ r.a.backend = r.b
+ if err := wake(); !errors.Is(err, ErrFenced) {
+ t.Fatalf("a cold wake before the ACK was not refused by the kept fence: %v", err)
+ }
+ if res := step(); !res.Proven || res.Reason != replicaSecretsRefusal {
+ t.Fatalf("the repeated step did not keep its answer: %+v", res)
+ }
+ cur := *r.store.snapshot()
+ next, err := replicas.NextDeploy(cur, replicas.DeployChange{Op: "ack", ID: cur.ID, Box: "b", Revision: cur.Revision, Target: cur.Target, Permit: cur.Swap.Permit}, replicas.Coordinator{}, time.Now())
+ if err != nil {
+ t.Fatal(err)
+ }
+ r.store.mu.Lock()
+ r.store.d = &next
+ r.store.mu.Unlock()
+ r.a.SetReplicaDeploy(r.rt.Host, next, "b")
+ r.a.mu.Lock()
+ fence := r.a.fences[r.rt.Host]
+ r.a.mu.Unlock()
+ if fence != 0 {
+ t.Fatalf("the kept fence outlived its ACK: %d", fence)
+ }
+ if err := wake(); !errors.Is(err, errReplicaTurn) {
+ t.Fatalf("after the ACK b is not refused the target by the record: %v", err)
+ }
+}
diff --git a/internal/activator/replicadeploy.go b/internal/activator/replicadeploy.go
index acf8d42e3..3ef26c6f8 100644
--- a/internal/activator/replicadeploy.go
+++ b/internal/activator/replicadeploy.go
@@ -40,6 +40,7 @@ import (
"errors"
"fmt"
"os"
+ "strings"
"time"
"github.com/carlosframework/platform/internal/registry"
@@ -77,7 +78,20 @@ type replicaPolicy struct {
// the host. An older revision of the same attempt never replaces a newer.
func (a *Service) SetReplicaDeploy(host string, deploy replicas.Deploy, box string) {
a.mu.Lock()
- defer a.mu.Unlock()
+ // A kept answer's fence stays until its ACK commits or its permit
+ // changes (#511 drill 1 round 1, Astra A1): lifted once the record no
+ // longer holds that permit open for this box.
+ var keptGen uint64
+ keptOwner := a.fenceOwners[host]
+ if strings.HasPrefix(keptOwner, replicaKeptFencePrefix) && (deploy.Swap.ActiveBox != box || keptOwner != replicaKeptOwner(deploy.ID, deploy.Swap.Permit)) {
+ keptGen = a.fences[host]
+ }
+ defer func() {
+ a.mu.Unlock()
+ if keptGen != 0 {
+ a.LiftFenceAs(host, keptGen, keptOwner)
+ }
+ }()
if deploy.ID == "" || box == "" {
delete(a.replicaPolicies, host)
return
@@ -450,6 +464,11 @@ func (a *Service) replicaRestartedFailure(ctx context.Context, at Attempt, deplo
return SwapResult{}, false
}
a.mu.Lock()
+ if a.fences[at.Host] == key.permit && a.fenceOwners[at.Host] == replicaKeptOwner(key.id, key.permit) {
+ // Already answered kept for this permit, its fence held since.
+ a.mu.Unlock()
+ return replicaKeptAnswer(), true
+ }
if a.entries[at.Host] != nil {
a.mu.Unlock()
return SwapResult{}, false
@@ -473,17 +492,29 @@ func (a *Service) replicaRestartedFailure(ctx context.Context, at Attempt, deplo
// FenceAndStopAs proves, or no generation, no release command and no
// stop left unproven (StopUncertain), which is all a restarted agent
// with no stop history can show, and all there is when nothing runs.
+ // A copy whose wakes were refused for the instance's shared secrets
+ // answers kept (below), and its fence is kept with it: owned by its
+ // permit, held until its ACK commits or the permit changes
+ // (SetReplicaDeploy), so the target cannot start between the answer
+ // and the ACK (#511 drill 1 round 1, Astra A1).
+ a.mu.Lock()
+ st := a.residents[at.Host]
+ secrets := st != nil && !st.secretsSince.IsZero()
+ a.mu.Unlock()
owner := fmt.Sprintf("replica-restart:%s:%d", key.id, key.permit)
+ if secrets {
+ owner = replicaKeptOwner(key.id, key.permit)
+ }
sctx, cancel := context.WithTimeout(ctx, replicaWebFailBound)
_, err = a.FenceAndStopAs(sctx, at.Host, key.permit, owner)
cancel()
a.mu.Lock()
releasing := a.releaseOps[at.Host] != nil
- st := a.residents[at.Host]
- secrets := st != nil && !st.secretsSince.IsZero()
a.mu.Unlock()
proven := err == nil || errors.Is(err, ErrStopUnproven) && !releasing && !a.StopUncertain(at.Host)
- a.LiftFenceAs(at.Host, key.permit, owner)
+ if !proven || !secrets {
+ a.LiftFenceAs(at.Host, key.permit, owner)
+ }
if !proven {
res := abandon(err)
res.Reason = "this copy holds the permit with nothing running; its stop is not proven yet, so the step stays open"
@@ -495,11 +526,26 @@ func (a *Service) replicaRestartedFailure(ctx context.Context, at Attempt, deplo
// the target never started here. The answer is kept, not
// switched, so the ACK resolves the permit and halts the deploy,
// and this copy stops asking for a build it cannot start.
- return SwapResult{Outcome: SwapAbandoned, Proven: true, Reason: replicaSecretsRefusal}, true
+ return replicaKeptAnswer(), true
}
return SwapResult{Outcome: SwapAbandoned, Proven: true, WebFailed: true, Reason: "the new build failed on this copy after it switched and did not come back; nothing of it runs"}, true
}
+// replicaKeptFencePrefix owns the fence of a kept answer (replicaKeptOwner).
+const replicaKeptFencePrefix = "replica-kept:"
+
+// replicaKeptOwner is the owner of the kept answer's fence for permit of
+// attempt id.
+func replicaKeptOwner(id string, permit uint64) string {
+ return fmt.Sprintf("%s%s:%d", replicaKeptFencePrefix, id, permit)
+}
+
+// replicaKeptAnswer is a permitted step's proven kept answer for want of
+// the shared secrets.
+func replicaKeptAnswer() SwapResult {
+ return SwapResult{Outcome: SwapAbandoned, Proven: true, Reason: replicaSecretsRefusal}
+}
+
// replicaSecretsRefusal is the answer of a copy that never started for
// want of the instance's shared secrets: its permitted step's, which halts
// the deploy, and, past replicaRestartBound, its wakes' (secretsRefusal).