carlos / platform Public

console: per-instance hold races through the helpers (Task 4 R2 P2)

Astra round 2 on Task 4: READY AFTER FIXES; its P2 asked for the races
exercised through the production console helpers. A hook store writes a
newer hold right after resume's read, a day's hold right after the
hour's enforcement reads, and a replacement that makes the clear's own
CAS conflict; each survives. Task 3's round 4 findings committed.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 0b5fc2dbc8dadd2b332a87486078ba8d3346db69 parent 37f1d59
3 files changed, +161 −0
  • docs/superpowers/plans/CODEX-FINDINGS-instance-senders-1-T3-R4.md +27 −0
  • docs/superpowers/plans/CODEX-FINDINGS-instance-senders-1-T4-R2.md +21 −0
  • internal/console/planmail_instance_test.go +113 −0

CI cancelled — superseded by a newer push — run details

CI log
=== gate ===
go: downloading go1.26.10 (linux/arm64)
go version go1.26.10 linux/arm64
opentofu: arm64, tofu 1.13.9, aws 6.73.0, archive 2.8.1
  tofu: downloading https://carlos-ci-mirror-640012953448.s3.eu-west-1.amazonaws.com/opentofu/tofu_1.13.9_linux_arm64.tar.gz
  tofu: downloaded, digest verified
  aws: downloading https://carlos-ci-mirror-640012953448.s3.eu-west-1.amazonaws.com/opentofu/terraform-provider-aws_6.73.0_linux_arm64.zip
  aws: downloaded, digest verified
  archive: downloading https://carlos-ci-mirror-640012953448.s3.eu-west-1.amazonaws.com/opentofu/terraform-provider-archive_2.8.1_linux_arm64.zip
  archive: downloaded, digest verified
  installed in /tmp/carlos-opentofu-0ZIkMx
.amadan/test-gate.sh
go: downloading github.com/aws/aws-sdk-go-v2 v1.44.0
go: downloading github.com/aws/aws-sdk-go-v2/service/costexplorer v1.67.5
go: downloading github.com/aws/aws-sdk-go-v2/service/s3 v1.105.0
go: downloading github.com/aws/smithy-go v1.28.1
go: downloading github.com/aws/aws-sdk-go-v2/service/cloudwatch v1.68.0
go: downloading golang.org/x/crypto/x509roots/fallback v0.0.0-20260209214922-2f26647a795e
go: downloading golang.org/x/net v0.62.0
go: downloading modernc.org/sqlite v1.55.0
go: downloading github.com/prometheus/client_golang v1.17.0
go: downloading github.com/aws/aws-sdk-go-v2/service/sns v1.43.0
go: downloading github.com/aws/aws-sdk-go-v2/service/ec2 v1.324.0
go: downloading github.com/aws/aws-sdk-go-v2/service/iam v1.58.2
go: downloading golang.org/x/sys v0.49.0
go: downloading golang.org/x/crypto v0.58.0
go: downloading golang.org/x/mod v0.41.0
go: downloading github.com/pion/turn/v4 v4.1.4
go: downloading github.com/aws/aws-sdk-go-v2/service/sesv2 v1.66.6
go: downloading github.com/pierrec/lz4/v4 v4.1.22
go: downloading github.com/beorn7/perks v1.0.1
go: downloading github.com/cespare/xxhash/v2 v2.3.0
go: downloading github.com/prometheus/client_model v0.5.0
go: downloading github.com/prometheus/common v0.45.0
go: downloading github.com/prometheus/procfs v0.12.0
go: downloading google.golang.org/protobuf v1.36.10
go: downloading github.com/pion/dtls/v3 v3.1.4
go: downloading github.com/klauspost/compress v1.20.2
go: downloading github.com/wlynxg/anet v0.0.5
go: downloading golang.org/x/text v0.43.0
go: downloading github.com/klauspost/compress v1.20.2
go: downloading modernc.org/sqlite v1.55.0
go: downloading github.com/aws/aws-sdk-go-v2/service/ecs v1.91.0
go: downloading github.com/pierrec/lz4/v4 v4.1.22
go: downloading modernc.org/libc v1.74.1
go: downloading modernc.org/libc v1.74.1

amadan: job cancelled — a newer push superseded it
diff --git a/docs/superpowers/plans/CODEX-FINDINGS-instance-senders-1-T3-R4.md b/docs/superpowers/plans/CODEX-FINDINGS-instance-senders-1-T3-R4.md
new file mode 100644
index 000000000..0bb3f032b
--- /dev/null
+++ b/docs/superpowers/plans/CODEX-FINDINGS-instance-senders-1-T3-R4.md
@@ -0,0 +1,27 @@
+# T3 review, round R4 (Astra, gpt-6-astra, on cf32b998b)
+
+Round-3 P1 (stale nonce consuming a later reservation): **Resolved** for the listed address operations: the app mutex spans recovery through finalization.
+Round-3 P2 (interleaving guard): **Partly resolved**: the exported-operation test detects missing serialization under normal scheduling, but lacks a signal proving the second operation attempted entry before its 300 ms timeout.
+Round-3 P2 (permanent domain exclusivity): **Not resolved**: `AddressApp` still persists after the last revocation; `addresses.go:664–669` still refuses sharing with zero dependencies.
+
+New finding in `7fdd2ffcf..cf32b998b`:
+
+1. **P1 — Submission-off bypasses the app mutex and can strand a newly created sender.**
+
+ **Evidence:** The new inventory check at [submission.go:368](internal/mail/submission.go:368) runs through `SwitchOff`, which takes no app mutex (`:464–467`). `CreateInstanceSender` takes that mutex at [instancesender.go:98](internal/mail/instancesender.go:98), but it cannot exclude an unlocked caller.
+
+ **Failure scenario:** Start with submission on and no instance senders. `SwitchOff` passes its empty-inventory check and pauses. `CreateInstanceSender` then sees submission on and successfully creates a sender with addresses. `SwitchOff` resumes, rereads the app record and sets submission off (`submission.go:393–400`). Both calls succeed; the instance sender has lost its required transport. The final app-record CAS does not detect creation of a separate sender record.
+
+ **Invariant:** Decision 1—instance senders exist only while submission is on; switching off must refuse while any exists.
+
+ **Fix:** Hold the same `(account, app)` mutex throughout exported `SwitchOff`, including its inventory check and final switch write. Add a signal-controlled create-versus-switch-off test.
+
+All nine operations named in the brief acquire the same app lock. Internal recovery/removal helpers avoid re-entry. The domain pin and EnsureDomain app-add acquire the same domain lock; I found no app/domain lock-order cycle. Within those paths, rounds 1–3’s schedules cannot proceed while the original grant remains paused.
+
+`TestExportedAddressOpsSerialize` uses `other(base)`, bypassing the paused store wrapper. Removing the mutex therefore lets the second operation finish and triggers the failure at `addresses_interleave_test.go:263`, provided it runs within 300 ms. This is meaningful coverage, but not deterministic proof; it also omits create, mode/cap changes, teardown, domain removal and domain joining.
+
+Reviewed the requested commit directly because the checkout is newer. No files changed; no tests or mutation experiments run in the read-only checkout. Line references above refer to the reviewed tip.
+
+There is one new P1, caused by incomplete serialization coverage. The previous nonce P1 is resolved; the previous domain-sharing P2 remains outstanding.
+
+**NOT READY**
\ No newline at end of file
diff --git a/docs/superpowers/plans/CODEX-FINDINGS-instance-senders-1-T4-R2.md b/docs/superpowers/plans/CODEX-FINDINGS-instance-senders-1-T4-R2.md
new file mode 100644
index 000000000..00b6f9243
--- /dev/null
+++ b/docs/superpowers/plans/CODEX-FINDINGS-instance-senders-1-T4-R2.md
@@ -0,0 +1,21 @@
+# T4 review, round R2 (Astra, gpt-6-astra, on 891562cf9)
+
+- R1 P1-1 — **Partly resolved:** eligibility now runs inside each CAS attempt; the requested interleaving regression tests remain missing.
+- R1 P1-2 — **Resolved:** daily enforcement preserves rate holds, and automatic resume excludes them.
+- R1 P1-3 — **Resolved:** switching modes no longer carries the app sender’s cap hold into `InstancesPause`; instance senders retain their own caps.
+
+**P2 — The new tests do not exercise the repaired races.**
+
+Evidence: [perinstance_test.go:145](internal/mail/perinstance_test.go:145) installs the replacement before calling the conditional setter and supplies a test-written predicate. It never exercises `resumePlanInstancesPause`. Similarly, [planmail_instance_test.go:132](internal/console/planmail_instance_test.go:132) installs the daily hold before hourly enforcement begins.
+
+**Failure scenario:** moving the console’s eligibility checks outside the mutation again, or failing to recheck after a CAS conflict, could restore the original races while these tests still pass.
+
+**Fix:** add deterministic store-hook tests through the production console helpers: replace the hold after resume’s initial read; install a daily hold after hourly enforcement’s initial read; force a CAS conflict and verify the retry preserves the replacement.
+
+**Invariant:** automatic mutations preserve newer or stronger holds, including across CAS retries.
+
+No remaining P1 found in the fix diff. Apply the P2 without reopening review under the stopping rule.
+
+Formatting checks passed for the changed Go files. Focused tests could not start because Go attempted to update `go.mod` in the read-only sandbox. No files were edited or full suites run.
+
+**READY AFTER FIXES**
\ No newline at end of file
diff --git a/internal/console/planmail_instance_test.go b/internal/console/planmail_instance_test.go
index b38ccb8ea..ad0026202 100644
--- a/internal/console/planmail_instance_test.go
+++ b/internal/console/planmail_instance_test.go
@@ -2,6 +2,7 @@ package console
import (
"context"
+ "encoding/json"
"testing"
"time"
@@ -151,3 +152,115 @@ func TestPlanPausePreservesStrongerHolds(t *testing.T) {
t.Fatalf("hour not upgraded to day: %+v", h)
}
}
+
+// holdHookStore runs fn once, the first time op hits key, before passing the
+// call through. It stands in for a concurrent writer at an exact point.
+type holdHookStore struct {
+ release.Store
+ op, key string
+ skip int // matching calls to let through first
+ fn func()
+ done bool
+}
+
+func (h *holdHookStore) fire(op, key string) {
+ if h.done || op != h.op || key != h.key {
+ return
+ }
+ if h.skip > 0 {
+ h.skip--
+ return
+ }
+ h.done = true
+ h.fn()
+}
+
+func (h *holdHookStore) Get(ctx context.Context, key string) ([]byte, string, error) {
+ b, e, err := h.Store.Get(ctx, key)
+ h.fire("Get-after", key)
+ return b, e, err
+}
+
+func (h *holdHookStore) Put(ctx context.Context, key string, body []byte, ifMatch string) (string, error) {
+ h.fire("Put-before", key)
+ return h.Store.Put(ctx, key, body, ifMatch)
+}
+
+// Task 4 review R2 P2: the races, through the production console helpers.
+// (a) A newer hold written right after resume's first read survives.
+// (b) A day's hold written right after an hour's enforcement first reads
+// survives. (c) A replacement that makes the clear's own CAS conflict is
+// re-checked on the retry and survives.
+func TestPerInstanceHoldRacesThroughConsoleHelpers(t *testing.T) {
+ ctx := context.Background()
+ appKey := mail.SenderKey(release.OpsAccount, "shop", mail.SenderAppID)
+ stale := &mailwire.Pause{Kind: mail.PauseKindPlan, At: planMailNow.Add(-24 * time.Hour)}
+ today := &mailwire.Pause{Kind: mail.PauseKindPlan, At: planMailNow}
+ cases := []struct {
+ name string
+ start *mailwire.Pause
+ hook func(base release.Store) *holdHookStore
+ run func(fx *consoleFixture, acct *Account) error
+ want *mailwire.Pause
+ }{
+ {"newer hold after resume's read", stale, func(base release.Store) *holdHookStore {
+ return &holdHookStore{Store: base, op: "Get-after", key: appKey}
+ }, func(fx *consoleFixture, acct *Account) error {
+ return fx.srv.resumePlanInstancesPause(ctx, acct, "shop", planMailNow)
+ }, today},
+ {"day's hold after the hour's read", nil, func(base release.Store) *holdHookStore {
+ return &holdHookStore{Store: base, op: "Get-after", key: appKey}
+ }, func(fx *consoleFixture, acct *Account) error {
+ _, err := fx.srv.planPausePerInstanceApp(ctx, acct, "shop", mail.PauseKindPlanHour, "hour", planMailNow)
+ return err
+ }, today},
+ {"replacement makes the clear's CAS conflict", stale, func(base release.Store) *holdHookStore {
+ return &holdHookStore{Store: base, op: "Put-before", key: appKey}
+ }, func(fx *consoleFixture, acct *Account) error {
+ return fx.srv.resumePlanInstancesPause(ctx, acct, "shop", planMailNow)
+ }, today},
+ }
+ for _, c := range cases {
+ fx := planMailFixture(t)
+ perInstanceShop(t, fx)
+ if c.start != nil {
+ if _, err := mail.SetInstancesPause(ctx, fx.rel, release.OpsAccount, "shop", c.start); err != nil {
+ t.Fatal(err)
+ }
+ }
+ base := fx.srv.cfg.Releases
+ h := c.hook(base)
+ // The concurrent writer writes the record directly (another console
+ // handler's CAS, below the process lock this helper may hold).
+ h.fn = func() {
+ raw, etag, err := base.Get(ctx, appKey)
+ if err != nil {
+ t.Errorf("%s: concurrent writer: %v", c.name, err)
+ return
+ }
+ var rec map[string]any
+ if err := json.Unmarshal(raw, &rec); err != nil {
+ t.Errorf("%s: %v", c.name, err)
+ return
+ }
+ rec["instances_pause"] = today
+ body, _ := json.Marshal(rec)
+ if _, err := base.Put(ctx, appKey, body, etag); err != nil {
+ t.Errorf("%s: concurrent writer: %v", c.name, err)
+ }
+ }
+ fx.srv.cfg.Releases = h
+ acct, _ := fx.st.AccountBySqid(fx.sqid)
+ if err := c.run(fx, acct); err != nil {
+ t.Fatalf("%s: %v", c.name, err)
+ }
+ fx.srv.cfg.Releases = base
+ if !h.done {
+ t.Fatalf("%s: the concurrent write never ran", c.name)
+ }
+ got := instancesHold(t, fx)
+ if got == nil || got.Kind != c.want.Kind || !got.At.Equal(c.want.At) {
+ t.Fatalf("%s: hold = %+v, want %+v", c.name, got, c.want)
+ }
+ }
+}