carlos / platform Public

console: kick copies placement by the admitted fleet, no DB read (#1061 r2)

Astra re-review: the round 1 kick read the app's placement row on the
request goroutine after the answer was written. That query has no
context and the console database has one connection, so a held
connection could delay the response.

copiesRefusal already resolves the fleet when it admits a count above
1. It now writes that fleet to a pointer the copies door puts in the
context (copiesFleetKey), and the door kicks by fleet after unlock.
kickReplicaPlacement(fleet) reads no database or bucket. A count of 1
is admitted without a placement read, so lowering to 1 is not kicked;
the FleetRelabel pass retires the extra copies. The census entry for
the removed AppPlacement caller goes.

Tests: enqueue returns with the database's only connection held; a kick
landing after a run read its view makes the same run go round again;
lowering to 1 starts no run.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev d9c2a53b1a796f52cab094905cfe9577fe88b6b6 parent ba04880
5 files changed, +112 −47
  • internal/console/appcopies.go +17 −4
  • internal/console/placement_census_test.go +9 −10
  • internal/console/replicakick_test.go +63 −6
  • internal/console/replicaplacement.go +20 −26
  • internal/console/server.go +3 −1

Intent

Human

<task-notification> <task-id>a17b099a61041ff5d</task-id> <tool-use-id>toolu_01KygsJkjpx9NSp3CTBXRKaT</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/2b517942-2259-467d-a8d0-994fd28c9e84/tasks/a17b099a61041ff5d.output</output-file> <status>completed</status> <summary>Agent "Implement #1061 Task 3" 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 (fix round 1) Commits: c4ae2184 console: place raised copies now, not at the next relabel pass (#1061); ba048801 console: coalesce copies kicks per fleet; bound the lock wait (#1061 r1) Tests: the three kick tests pass (the two kick tests plus the placement-census test); `-run 'Replica|Copies|Placement|FleetRelabel'` passes (214 s); I did not run the full console package, CI covers it. P2 fixed: kicks are combined per fleet. At most one placement run per fleet is in flight, and kicks during it only mark it to run again. The run clears that mark when it takes the fleet lock and reads the fleet view, so a burst of writes gives exactly one placement after the lock frees. The lock is retried every 100 ms until the 2-minute deadline; a run that times out logs once and leaves the work to the 5-minute pass. P3 fixed: the test helper `holdFleet` releases the fleet lock once, on the normal path or at cleanup. Concerns: - The app's fleet is now looked up on the request goroutine (one SQLite row), after the response is written but before the handler returns. - The new combining test has no recorded failing run, because the test hook it uses did not exist before. Report: /home/paulca/amadan.net/carlos/platform/.claude/worktrees/sr-followups/.superpowers/sdd/plan-1061/task-3-report.md</result> <usage><subagent_tokens>130151</subagent_tokens><tool_uses>6</tool_uses><duration_ms>354559</duration_ms></usage> </task-notification>

Human

<task-notification> <task-id>bw7yr4xe9</task-id> <tool-use-id>toolu_01HM5TdZ1JhugEwMjSaJpM2B</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/2b517942-2259-467d-a8d0-994fd28c9e84/tasks/bw7yr4xe9.output</output-file> <status>completed</status> <summary>Background command "S=/home/paulca/.paseo/zerosub/homes/claude-37fe90ef3f56/plugins/cache/claude-plugins-official/superpowers/7.0.2/skills/subagent-driven-development; bash $S/scripts/review-package $PWD/.superpowers/sdd/2026-11-14-sr-followups/plan-1061.md c4ae2184 HEAD &amp;&amp; P=.superpowers/sdd/plan-1061 &amp;&amp; cat &gt; $P/task-3-astra-rereview-1.md &lt;&lt;'EOF' Scoped re-review, read-only (do not modify files, index or branches). Go, CARLOS platform. Your round-1 review: .superpowers/sdd/plan-1061/task-3-ast ... [540 characters truncated] ... the fix diff only. Minors separately. Concise. EOF timeout 1800 codex exec -m gpt-6-astra -c model_reasoning_effort=high -s read-only "Read .superpowers/sdd/plan-1061/task-3-astra-rereview-1.md and do what it says." &lt;/dev/null &gt; $P/task-3-astra-rereview-1.txt 2&gt;&amp;1; echo done" completed (exit code 0)</summary> </task-notification>

Attributed to this commit: 12.1 g CO₂e (1,484,726 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on sr-followups, 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/console/appcopies.go b/internal/console/appcopies.go
index 7e5ceff18..b7057ff7a 100644
--- a/internal/console/appcopies.go
+++ b/internal/console/appcopies.go
@@ -97,15 +97,20 @@ func (s *Server) handleAPICopies(w http.ResponseWriter, r *http.Request) {
return
}
// Registered before the app lock's unlock, so it runs after it: the
- // kick's placement only tryLocks the app (#1061).
- written := false
+ // kick's placement only tryLocks the app (#1061). The fleet is the one
+ // copiesRefusal admitted the count against (copiesFleetKey), so the
+ // kick reads nothing on the request goroutine. A count of 1 is admitted
+ // without reading the placement, so lowering to 1 is not kicked; the
+ // FleetRelabel pass retires the extra copies.
+ written, fleet := false, ""
defer func() {
- if written {
- s.kickReplicaPlacement(acct.ID, app)
+ if written && fleet != "" {
+ s.kickReplicaPlacement(fleet)
}
}()
ctx, cancel := context.WithTimeout(r.Context(), configStoreTimeout)
defer cancel()
+ ctx = context.WithValue(ctx, copiesFleetKey{}, &fleet)
// The existing app lock covers validation and publication. SetCopies joins
// the claim mutation through ctx and does not acquire this mutex itself.
ctx, unlock, lockRef := s.beginCopiesMutation(ctx, acct, app)
@@ -155,6 +160,11 @@ func (s *Server) handleAPICopies(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusOK, map[string]int{"copies": got})
}
+// copiesFleetKey carries a *string through copiesRefusal: set, the fleet
+// a count above 1 was admitted against is written there for the copies
+// door's placement kick.
+type copiesFleetKey struct{}
+
// copiesRefusal runs under the app mutation boundary. A nil target admits
// a count against the complete inventory. A canonical target checks a
// publication for an explicitly configured app, including removal at count 1.
@@ -224,6 +234,9 @@ func (s *Server) copiesRefusal(ctx context.Context, acct *Account, app string, n
if !own {
return refuse(copiesManaged)
}
+ if out, ok := ctx.Value(copiesFleetKey{}).(*string); ok && out != nil {
+ *out = fleet
+ }
recs, err := placeRecords(ctx, s.cfg.Releases, provision.JudgeFrom(ctx), acct.Sqid, app)
if err != nil {
return unread()
diff --git a/internal/console/placement_census_test.go b/internal/console/placement_census_test.go
index 75aab311f..b32140eff 100644
--- a/internal/console/placement_census_test.go
+++ b/internal/console/placement_census_test.go
@@ -69,16 +69,15 @@ func TestEffectivePlacementIsTheOnlyRouteFallback(t *testing.T) {
// Folds: a call inside an effectivePlacement func literal is the rule.
foldInRule := map[string]bool{"legacyFleetFromRoutes": true, "fleetFromRoutes": true}
for where, why := range map[string]string{
- "appcopies.go:copiesRefusal": "a fleet test on the column",
- "claim_placement.go:Start": "the claim operation's recorded source",
- "plancaps.go:onOwnBoxes": "an empty column falls back through appPlacement",
- "replicafloor.go:replicaPolicyBarrier": "a fleet test on the column",
- "policysize.go:sizeAppPlacement": "a console with no registry has no route fallback",
- "replicaplacement.go:reconcileReplicaApp": "matches the column against a fleet target",
- "replicaplacement.go:kickReplicaPlacement": "a fleet test on the column",
- "sealkey.go:sealPlacement": "a console with no registry has no route fallback",
- "sealkey.go:backfillAppPlacements": "picks candidates with an empty column; the write is guarded",
- "sealkey.go:backfillApp": "re-reads the column under its locks; the write is guarded",
+ "appcopies.go:copiesRefusal": "a fleet test on the column",
+ "claim_placement.go:Start": "the claim operation's recorded source",
+ "plancaps.go:onOwnBoxes": "an empty column falls back through appPlacement",
+ "replicafloor.go:replicaPolicyBarrier": "a fleet test on the column",
+ "policysize.go:sizeAppPlacement": "a console with no registry has no route fallback",
+ "replicaplacement.go:reconcileReplicaApp": "matches the column against a fleet target",
+ "sealkey.go:sealPlacement": "a console with no registry has no route fallback",
+ "sealkey.go:backfillAppPlacements": "picks candidates with an empty column; the write is guarded",
+ "sealkey.go:backfillApp": "re-reads the column under its locks; the write is guarded",
} {
census["AppPlacement"][where] = columnOnly + "; " + why
}
diff --git a/internal/console/replicakick_test.go b/internal/console/replicakick_test.go
index 5ac1555cf..1f3c31e3d 100644
--- a/internal/console/replicakick_test.go
+++ b/internal/console/replicakick_test.go
@@ -1,6 +1,7 @@
package console
import (
+ "context"
"slices"
"sync"
"sync/atomic"
@@ -45,16 +46,20 @@ func TestCopiesRaisePlacesWithoutRelabelPass(t *testing.T) {
t.Fatalf("serial %d -> %d; want the door's bump and the placement's", before, after)
}
- // Lowering goes through the same reconcile: the kick writes the lower
- // count without a pass too.
+ // A count of 1 is admitted without reading the placement, so the door
+ // has no fleet to kick: lowering to 1 is left to the relabel pass.
+ var starts atomic.Int32
+ fx.srv.replicaKickHook = func(_, stage string) {
+ if stage == "start" {
+ starts.Add(1)
+ }
+ }
if code, out := copiesPut(t, fx, token, `{"copies":1}`); code != 200 {
t.Fatalf("lower: %d %s", code, out)
}
fx.srv.replicaKicks.Wait()
- for _, host := range []string{"a.example.com", "b.example.com"} {
- if a := shopRec(t, fx, host).ReplicaAssignment; a != nil && a.Desired != 1 {
- t.Fatalf("%s: assignment %+v after lowering; want desired 1", host, a)
- }
+ if n := starts.Load(); n != 0 {
+ t.Fatalf("lowering to 1 started %d kick runs; want 0", n)
}
}
@@ -99,3 +104,55 @@ func TestCopiesKicksCoalescePerFleet(t *testing.T) {
}
}
}
+
+// The kick reads nothing on the request goroutine: with the console
+// database's only connection held, enqueueing still returns at once.
+func TestCopiesKickEnqueueTouchesNoDatabase(t *testing.T) {
+ fx, _ := copiesWorld(t)
+ fx.srv.replicaKickOff = false
+ var starts atomic.Int32
+ fx.srv.replicaKickHook = func(_, stage string) {
+ if stage == "start" {
+ starts.Add(1)
+ }
+ }
+ release := holdFleet(t, fx, "pi")
+ conn, err := fx.st.rawPool.Conn(context.Background())
+ if err != nil {
+ t.Fatal(err)
+ }
+ freeConn := sync.OnceFunc(func() { conn.Close() })
+ t.Cleanup(freeConn)
+ fx.srv.kickReplicaPlacement("pi") // blocks, and the test times out, if it queries
+ if n := starts.Load(); n != 1 {
+ t.Fatalf("%d kick runs started; want 1", n)
+ }
+ freeConn()
+ release()
+ fx.srv.replicaKicks.Wait()
+}
+
+// A kick that lands after the run has cleared its dirty mark and read its
+// view is not lost: the same run goes round once more.
+func TestCopiesKickAfterViewReruns(t *testing.T) {
+ fx, _ := copiesWorld(t)
+ fx.srv.replicaKickOff = false
+ var starts, reconciles, views atomic.Int32
+ fx.srv.replicaKickHook = func(fleet, stage string) {
+ switch stage {
+ case "start":
+ starts.Add(1)
+ case "view":
+ if views.Add(1) == 1 {
+ fx.srv.kickReplicaPlacement(fleet) // a write landing mid-run
+ }
+ case "reconcile":
+ reconciles.Add(1)
+ }
+ }
+ fx.srv.kickReplicaPlacement("pi")
+ fx.srv.replicaKicks.Wait()
+ if s, v, r := starts.Load(), views.Load(), reconciles.Load(); s != 1 || v != 2 || r != 2 {
+ t.Fatalf("starts %d views %d reconciles %d; want 1 run that went round twice", s, v, r)
+ }
+}
diff --git a/internal/console/replicaplacement.go b/internal/console/replicaplacement.go
index 767f1b451..6ed6abd79 100644
--- a/internal/console/replicaplacement.go
+++ b/internal/console/replicaplacement.go
@@ -446,34 +446,25 @@ type replicaKickState struct {
running, dirty bool
}
-// kickReplicaPlacement places app's copies now rather than at the next
-// FleetRelabel pass, up to fleetRelabelInterval away (#1061). The copies
-// door calls it after it has answered and released the app lock:
+// kickReplicaPlacement places a fleet's copies now rather than at the
+// next FleetRelabel pass, up to fleetRelabelInterval away (#1061). The
+// copies door calls it after it has answered and released the app lock:
// reconcileReplicaApp only tryLocks the app, so a kick under the door's
-// lock would do nothing. It reads the app's placement column (one store
-// row) and hands the fleet's work to one background run, coalesced per
-// fleet: a kick while that fleet's run is in flight only marks it dirty,
-// and the run goes round once more if a kick came after it read its view.
-// So a burst of writes behind a slow pass or drain holds at most one
-// goroutine per fleet. Each run does only what the worker's pass does for
-// one fleet (fleetRelabelPass: off on a host-claim console, one admitted
-// unit of record work, the fleet lock, a fresh view, reconcileReplicaFleet
-// and its serial bump), waits for the fleet lock no longer than
+// lock would do nothing. It takes the fleet the door already resolved and
+// touches no database or bucket itself, so it never delays the answer. It
+// hands the fleet's work to one background run, coalesced per fleet: a
+// kick while that fleet's run is in flight only marks it dirty, and the
+// run goes round once more if a kick came after it read its view. So a
+// burst of writes behind a slow pass or drain holds at most one goroutine
+// per fleet. Each run does only what the worker's pass does for one fleet
+// (fleetRelabelPass: off on a host-claim console, one admitted unit of
+// record work, the fleet lock, a fresh view, reconcileReplicaFleet and its
+// serial bump), waits for the fleet lock no longer than
// replicaKickTimeout, and logs its errors; a run that fails, gives up or
-// finds the app busy leaves the work to the next pass. Lowering the count
-// goes through the same reconcile, which retires the extra copies.
-// replicaKicks lets a test wait for the runs.
-func (s *Server) kickReplicaPlacement(account int64, app string) {
- if s.replicaKickOff || s.cfg.HostClaim || s.cfg.Releases == nil || s.cfg.Registry == "" {
- return
- }
- placed, err := s.store.AppPlacement(account, app)
- if err != nil {
- log.Printf("console: replica placement kick %s: placement: %v (the relabel worker places it)", app, err)
- return
- }
- fleet, ok := registry.FleetPlacement(placed)
- if !ok {
+// finds the app busy leaves the work to the next pass. replicaKicks lets
+// a test wait for the runs.
+func (s *Server) kickReplicaPlacement(fleet string) {
+ if fleet == "" || s.replicaKickOff || s.cfg.HostClaim || s.cfg.Releases == nil || s.cfg.Registry == "" {
return
}
s.replicaKickMu.Lock()
@@ -536,6 +527,9 @@ func (s *Server) replicaPlacementKick(ctx context.Context, fleet string, st *rep
s.replicaKickMu.Lock()
st.dirty = false
s.replicaKickMu.Unlock()
+ if s.replicaKickHook != nil {
+ s.replicaKickHook(fleet, "view")
+ }
now := time.Now()
v, err := s.loadFleetViewAt(ctx, fleet, now)
if err != nil {
diff --git a/internal/console/server.go b/internal/console/server.go
index 35c2c4e98..00c875a5c 100644
--- a/internal/console/server.go
+++ b/internal/console/server.go
@@ -1460,7 +1460,9 @@ type Server struct {
replicaKickMu sync.Mutex
replicaKickFleets map[string]*replicaKickState
// replicaKickHook, when non-nil, is called when a fleet's kick run
- // starts ("start") and when it has reconciled ("reconcile"). Tests only.
+ // starts ("start"), when a run holds the fleet lock and has cleared
+ // its dirty mark, just before it reads its view ("view"), and when it
+ // has reconciled ("reconcile"). Tests only.
replicaKickHook func(fleet, stage string)
// replicaKickOff, when set, stops the copies door's placement kick.
// Tests only: worlds that drive placement passes themselves.