fix: refuse a retry replay when unreached instances changed
Task 1 fix round 3 (re-review 2, #2). A retry request that already reached a record is a replay. Replaying it is refused whole, with nothing written (SR-4-C42, pending copy review), when any record it has not reached carries retry intent or an attempt from another request, or is deploying a build other than the one the request named. A replay therefore finishes only what it would have done, and never arms an attempt that began after it. Records it already armed stay as they are; no validated-ID snapshot or reconcile path is kept.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
9 files changed,
+163
−2
docs/superpowers/notes/census-proposal-stateless-replicas-4-task1-fix3.md+11 −0docs/superpowers/plans/2026-11-01-stateless-replicas-copy.md+1 −0internal/console/apicli.go+3 −0internal/console/pendinghandlers.go+1 −1internal/console/replicadeploy_test.go+20 −0internal/provision/replicadeploy.go+31 −1internal/provision/replicadeploy_test.go+92 −0internal/provision/testdata/plan14-root-reads-chain.tsv+1 −0internal/provision/testdata/plan14-root-reads-stateless-replicas-4-task1-fix3.tsv+3 −0
diff --git a/docs/superpowers/notes/census-proposal-stateless-replicas-4-task1-fix3.md b/docs/superpowers/notes/census-proposal-stateless-replicas-4-task1-fix3.md| new file mode 100644 |
| index 000000000..a9dbbf752 |
| --- /dev/null |
| +++ b/docs/superpowers/notes/census-proposal-stateless-replicas-4-task1-fix3.md |
| @@ -0,0 +1,11 @@ |
| +# Census approval: stateless-replicas-4-task1-fix3 |
| + |
| +Ruling `census-ruling-stateless-replicas-4-task1-fix3`. The independent reviewer approves or rejects every change below, 0 with a new disposition; nothing goes to the census owner (Paul, #280, 2026-10-26). |
| + |
| +Envelopes re-pinned: `internal/provision`. |
| + |
| +Review: a model from the other family than the author reads the branch's diff against main on the amadan.net remote (`origin` or `amadan`, whichever points at amadan.net) and gives a verdict for every unit and every re-pinned envelope above. For a unit, compare the change with its recorded disposition and basis below; for an envelope, read every changed function in the package and what it reaches. Reject if the change lets root open, run, query or follow anything it did not before from a path or input a tenant, an app or a box agent can create, write or name; if it weakens a check, refusal, validation, argument or environment the basis relies on; or if the diff cannot settle either. Otherwise approve. Record each verdict on #280 with the tip. A rejected unit goes back to the author to correct; nothing escalates to the census owner. |
| + |
| +Seal: `3f066208f5e5fa2b9854f30118a9a2707f3a5a51bcf55ae6b0949cb0307804f5` → `3f066208f5e5fa2b9854f30118a9a2707f3a5a51bcf55ae6b0949cb0307804f5`. |
| + |
| +Overlay: `internal/provision/testdata/plan14-root-reads-stateless-replicas-4-task1-fix3.tsv`, appended to `internal/provision/testdata/plan14-root-reads-chain.tsv`. |
diff --git a/docs/superpowers/plans/2026-11-01-stateless-replicas-copy.md b/docs/superpowers/plans/2026-11-01-stateless-replicas-copy.md| index 1d5e656ab..769c441ec 100644 |
| --- a/docs/superpowers/plans/2026-11-01-stateless-replicas-copy.md |
| +++ b/docs/superpowers/plans/2026-11-01-stateless-replicas-copy.md |
| @@ -113,3 +113,4 @@ copy too. Internal wire enums are not an excuse to expose unreviewed text. |
| | SR-4-C39 | internal/console/replicadeploy.go | release complete; no copy was running, so the first copy is starting on its own | pending (added by Plan 4 Task 1 for a bootstrap order; awaiting Paul's copy review) | |
| | SR-4-C40 | cli.go | this app runs stateless replicas, so retry it through the console: carlos instances retry <host>, or carlos promote --retry while logged in to the console | pending (added by Plan 4 Task 1 fix round 1 for the direct-bucket retry refusal; awaiting Paul's copy review) | |
| | SR-4-C41 | internal/provision/replicadeploy.go | an instance record could not be read, so nothing was retried. try again | pending (added by Plan 4 Task 1 fix round 2 for retries that fail closed on unreadable records; awaiting Paul's copy review) | |
| +| SR-4-C42 | internal/provision/replicadeploy.go | the instances changed since this retry was requested, so nothing was retried. run the retry again | pending (added by Plan 4 Task 1 fix round 3 for a refused replay of a retry request; awaiting Paul's copy review) | |
diff --git a/internal/console/apicli.go b/internal/console/apicli.go| index b17040b2b..3a06e848b 100644 |
| --- a/internal/console/apicli.go |
| +++ b/internal/console/apicli.go |
| @@ -1148,6 +1148,9 @@ func (s *Server) handleAPIPromote(w http.ResponseWriter, r *http.Request) { |
| } else if errors.Is(err, provision.ErrReplicaRetryUnreadable) { |
| jsonError(w, http.StatusBadGateway, err.Error()) |
| return |
| + } else if errors.Is(err, provision.ErrReplicaRetryStale) { |
| + jsonError(w, http.StatusConflict, err.Error()) |
| + return |
| } else if err != nil { |
| log.Printf("console: api promote %s %s -> %s: replica retry: %v", app, version, channel, err) |
| jsonError(w, http.StatusBadGateway, apiRetryStore) |
diff --git a/internal/console/pendinghandlers.go b/internal/console/pendinghandlers.go| index 4d68088b7..b3d8abe6c 100644 |
| --- a/internal/console/pendinghandlers.go |
| +++ b/internal/console/pendinghandlers.go |
| @@ -576,7 +576,7 @@ func (s *Server) executePendingPromote(ctx context.Context, acct *Account, app, |
| var retryTargets []provision.Record |
| if p.Retry { |
| targets, err := s.channelReplicaRetryTargets(ctx, acct.Sqid, app, p.Channel, p.Version, "proposal:"+pc.ID) |
| - if errors.Is(err, provision.ErrReplicaSwitchOpen) || errors.Is(err, provision.ErrReplicaRetryUnreadable) { |
| + if errors.Is(err, provision.ErrReplicaSwitchOpen) || errors.Is(err, provision.ErrReplicaRetryUnreadable) || errors.Is(err, provision.ErrReplicaRetryStale) { |
| return "", err |
| } else if err != nil { |
| log.Printf("console: execute promote %s %s -> %s: replica retry: %v", app, p.Version, p.Channel, err) |
diff --git a/internal/console/replicadeploy_test.go b/internal/console/replicadeploy_test.go| index 385a82de8..e74a61ec8 100644 |
| --- a/internal/console/replicadeploy_test.go |
| +++ b/internal/console/replicadeploy_test.go |
| @@ -477,3 +477,23 @@ func TestReplicaPromoteRetryReplayIsKeyed(t *testing.T) { |
| t.Fatalf("a new request did not retry A: %+v", a) |
| } |
| } |
| + |
| +// TestReplicaPromoteRetryStaleReplayRefused: the reviewer's case through |
| +// the promote door. r1 reached A but not B; r2 then retried B and B2 |
| +// failed. Replaying r1 is refused with SR-4-C42 and writes nothing. |
| +func TestReplicaPromoteRetryStaleReplayRefused(t *testing.T) { |
| + fx, token, url, put := promoteRetryWorld(t) |
| + build := replicas.Build{Version: "abc1234", Digest: "d"} |
| + r1 := promoteRetryRequest("canary/x", "abc1234", "r1") |
| + r2 := promoteRetryRequest("canary/x", "abc1234", "r2") |
| + put("a.titogo.example.com", replicas.Deploy{ID: "a1", Target: build, Phase: "failed", Term: "t", Revision: 3, RetryOf: "a1", RetryRequest: r1}) |
| + put("b.titogo.example.com", replicas.Deploy{ID: "b2", Target: build, Phase: "failed", Term: "t2", Revision: 6, RetryOf: "b1", RetryRequest: r2}) |
| + before := bucketSnapshot(t, fx.rel) |
| + status, out := postJSON(t, url, map[string]any{"version": "abc1234", "channel": "canary/x", "retry": true, "request_id": "r1"}, token) |
| + if status != http.StatusConflict || !strings.Contains(str(t, out, "error"), "the instances changed since this retry was requested") { |
| + t.Fatalf("stale replay: %d %v", status, out) |
| + } |
| + if !reflect.DeepEqual(bucketSnapshot(t, fx.rel), before) { |
| + t.Fatal("a refused replay wrote") |
| + } |
| +} |
diff --git a/internal/provision/replicadeploy.go b/internal/provision/replicadeploy.go| index 04bd35d55..e2fbd2549 100644 |
| --- a/internal/provision/replicadeploy.go |
| +++ b/internal/provision/replicadeploy.go |
| @@ -216,6 +216,12 @@ var ErrReplicaSwitchOpen = errors.New("another deploy is still finishing a copy' |
| // copy SR-4-C41 (provisional) |
| var ErrReplicaRetryUnreadable = errors.New("an instance record could not be read, so nothing was retried. try again") |
| +// ErrReplicaRetryStale refuses a replay of a retry request when the |
| +// instances it has not reached changed since it was made: another request |
| +// retried them, or a new build is deploying. Nothing is written. |
| +// copy SR-4-C42 (provisional) |
| +var ErrReplicaRetryStale = errors.New("the instances changed since this retry was requested, so nothing was retried. run the retry again") |
| + |
| // ReplicaRetryTargets is the read-only half of the explicit retry request |
| // named request, over build: the records among recs (on channel and |
| // unpinned, unless channel is "") whose replicated attempt for build can be |
| @@ -224,14 +230,38 @@ var ErrReplicaRetryUnreadable = errors.New("an instance record could not be read |
| // refuses. It refuses with ErrReplicaSwitchOpen, having written nothing, |
| // when any record it would retry has an unresolved permit, so a refused |
| // retry never leaves part of its intent behind. |
| +// |
| +// A request that already reached a record is being replayed. A replay is |
| +// refused whole, with ErrReplicaRetryStale, when any record it has not |
| +// reached carries retry intent or an attempt from another request, or is |
| +// deploying another build: it finishes only what it would have done, or |
| +// nothing. Records it reached stay as they are. |
| func ReplicaRetryTargets(recs []Record, unreadable map[string]bool, channel, build, request string) ([]Record, error) { |
| if len(unreadable) != 0 { |
| return nil, ErrReplicaRetryUnreadable |
| } |
| + inScope := func(rec Record) bool { |
| + return rec.ReplicaDeploy != nil && (channel == "" || (rec.Channel == channel && rec.Pin == "")) |
| + } |
| + replay := false |
| + for _, rec := range recs { |
| + if request != "" && inScope(rec) && rec.ReplicaDeploy.RetryRequest == request { |
| + replay = true |
| + } |
| + } |
| var out []Record |
| for _, rec := range recs { |
| + if !inScope(rec) { |
| + continue |
| + } |
| d := rec.ReplicaDeploy |
| - if d == nil || d.Target.Version != build || (channel != "" && (rec.Channel != channel || rec.Pin != "")) || (request != "" && d.RetryRequest == request) { |
| + if request != "" && d.RetryRequest == request { |
| + continue |
| + } |
| + if replay && (d.RetryRequest != "" || d.Target.Version != build) { |
| + return nil, ErrReplicaRetryStale |
| + } |
| + if d.Target.Version != build { |
| continue |
| } |
| if d.PermitOutstanding() { |
diff --git a/internal/provision/replicadeploy_test.go b/internal/provision/replicadeploy_test.go| index 1a86a5a5d..35329caad 100644 |
| --- a/internal/provision/replicadeploy_test.go |
| +++ b/internal/provision/replicadeploy_test.go |
| @@ -5,6 +5,7 @@ import ( |
| "context" |
| "encoding/json" |
| "errors" |
| + "reflect" |
| "testing" |
| "time" |
| @@ -841,3 +842,94 @@ func TestReplicaRetryReplayFinishesOnlyUnreached(t *testing.T) { |
| t.Fatalf("unreadable record: %v", err) |
| } |
| } |
| + |
| +// TestReplicaRetryReplayRefusedWhenChanged (fix round 3): r1 arms A and |
| +// fails writing B. If another request r2 then retries B and B's new attempt |
| +// fails, or a new build starts an attempt on B, replaying r1 is refused |
| +// whole with ErrReplicaRetryStale and writes nothing. |
| +func TestReplicaRetryReplayRefusedWhenChanged(t *testing.T) { |
| + for _, change := range []string{"other-request", "new-target"} { |
| + t.Run(change, func(t *testing.T) { |
| + ctx := WithRecordSequence(context.Background()) |
| + base := release.NewDirStore(t.TempDir()) |
| + v1 := replicas.Build{Version: "v1", Digest: "canonical-1"} |
| + put := func(host string, d replicas.Deploy) { |
| + t.Helper() |
| + key := RecordKey("acme", "shop", host) |
| + raw, etag, err := base.Get(ctx, key) |
| + if err != nil { |
| + rec := Record{Schema: 1, Account: "acme", App: "shop", Host: host, Slug: host[:1], Channel: "edge", CreatedAt: time.Date(2026, 11, 1, 0, 0, 0, 0, time.UTC)} |
| + if err := CreateRecord(ctx, base, rec); err != nil { |
| + t.Fatal(err) |
| + } |
| + if raw, etag, err = base.Get(ctx, key); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + rec, err := DecodeRecord(raw) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + d.Incarnation, _ = Incarnation(rec.Account, rec.App, rec.Host, rec.CreatedAt) |
| + rec.ReplicaDeploy = &d |
| + raw, _ = json.Marshal(rec) |
| + if _, err := base.Put(ctx, key, raw, etag); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + targets := func(request string) ([]Record, error) { |
| + t.Helper() |
| + recs, unreadable, err := ListRecordsStrict(ctx, base, "acme", "shop", 100) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return ReplicaRetryTargets(recs, unreadable, "edge", "v1", request) |
| + } |
| + snapshot := func() map[string]string { |
| + t.Helper() |
| + keys, err := base.List(ctx, "") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + out := map[string]string{} |
| + for _, k := range keys { |
| + raw, _, _ := base.Get(ctx, k) |
| + out[k] = string(raw) |
| + } |
| + return out |
| + } |
| + put("a.example.com", replicas.Deploy{ID: "a1", Target: v1, Phase: "failed", Term: "t", Revision: 3}) |
| + put("b.example.com", replicas.Deploy{ID: "b1", Target: v1, Phase: "failed", Term: "t", Revision: 3}) |
| + r1, err := targets("r1") |
| + if err != nil || len(r1) != 2 { |
| + t.Fatalf("r1 targets: %d %v", len(r1), err) |
| + } |
| + failing := &failingPutStore{Store: base, key: RecordKey("acme", "shop", "b.example.com"), fail: true} |
| + if err := RequestReplicaRetries(ctx, failing, r1, "r1"); err == nil { |
| + t.Fatal("B's failed write was not reported") |
| + } |
| + switch change { |
| + case "other-request": |
| + // r2, a legitimate new request, retries B; B2 fails. |
| + r2, err := targets("r2") |
| + if err != nil || len(r2) != 1 || r2[0].Host != "b.example.com" { |
| + t.Fatalf("r2 targets: %+v %v", r2, err) |
| + } |
| + if err := RequestReplicaRetries(ctx, base, r2, "r2"); err != nil { |
| + t.Fatal(err) |
| + } |
| + put("b.example.com", replicas.Deploy{ID: "b2", Target: v1, Phase: "failed", Term: "t2", RetryOf: "b1", RetryRequest: "r2", Revision: 6}) |
| + case "new-target": |
| + // A new pointer: B starts the new build without any retry. |
| + put("b.example.com", replicas.Deploy{ID: "b-v2", Target: replicas.Build{Version: "v2", Digest: "canonical-2"}, Phase: "failed", Term: "t2", Revision: 6}) |
| + } |
| + before := snapshot() |
| + if _, err := targets("r1"); !errors.Is(err, ErrReplicaRetryStale) { |
| + t.Fatalf("replay of r1: %v", err) |
| + } |
| + if !reflect.DeepEqual(snapshot(), before) { |
| + t.Fatal("the refused replay wrote") |
| + } |
| + }) |
| + } |
| +} |
diff --git a/internal/provision/testdata/plan14-root-reads-chain.tsv b/internal/provision/testdata/plan14-root-reads-chain.tsv| index 49c2611a3..ad9b154a5 100644 |
| --- a/internal/provision/testdata/plan14-root-reads-chain.tsv |
| +++ b/internal/provision/testdata/plan14-root-reads-chain.tsv |
| @@ -171,3 +171,4 @@ plan14-root-reads-group-read-transport-retry.tsv 9c74b9876a13a3a3802860fe4440689 |
| plan14-root-reads-stateless-replicas-4-task1.tsv 3f066208f5e5fa2b9854f30118a9a2707f3a5a51bcf55ae6b0949cb0307804f5 |
| plan14-root-reads-stateless-replicas-4-task1-fix1.tsv 3f066208f5e5fa2b9854f30118a9a2707f3a5a51bcf55ae6b0949cb0307804f5 |
| plan14-root-reads-stateless-replicas-4-task1-fix2.tsv 3f066208f5e5fa2b9854f30118a9a2707f3a5a51bcf55ae6b0949cb0307804f5 |
| +plan14-root-reads-stateless-replicas-4-task1-fix3.tsv 3f066208f5e5fa2b9854f30118a9a2707f3a5a51bcf55ae6b0949cb0307804f5 |
diff --git a/internal/provision/testdata/plan14-root-reads-stateless-replicas-4-task1-fix3.tsv b/internal/provision/testdata/plan14-root-reads-stateless-replicas-4-task1-fix3.tsv| new file mode 100644 |
| index 000000000..049fea3a8 |
| --- /dev/null |
| +++ b/internal/provision/testdata/plan14-root-reads-stateless-replicas-4-task1-fix3.tsv |
| @@ -0,0 +1,3 @@ |
| +# Census overlay stateless-replicas-4-task1-fix3, written by census-propose (#369). census-ruling-stateless-replicas-4-task1-fix3, approved on #280 before the merge; the approval card is docs/superpowers/notes/census-proposal-stateless-replicas-4-task1-fix3.md. |
| +# 0 body, 0 sites, 1 envelope: each changed unit re-frozen at its existing disposition with its claim appended. |
| +@coverage internal/provision 64ac1ca21d55d3a26f11d3a5b5ac4b0e120df224d3236a342a4d74d4c70edc64 |