carlos / platform Public

restart: baseline apps a box gains after arming, not at the first request

A fresh box's agent arms before reconcile mints its routes, so
restartSeed saw no apps. The restart leg runs only on a serial move, so
on a quiet fleet its first look at the app was the move that carried
the first `carlos restart`, and a first sight is a seed: the request
became the baseline and the box cycled nothing (G2 drill on dev,
2026-11-05: replacement holder i-00fcda37 armed with 0 routes at
22:36:07Z, minted hapair-a1 at 22:36:11Z, no serial move until the
23:31:13Z narrowed restart, which it skipped).

fastTick now runs restartGainSeed on every tick: an app with no entry
in the agent's restart memory is baselined (restartSeed's sweep,
factored out as restartBaseline) as soon as its route appears. Entries
already held are never overwritten; read errors back off a minute. At
rest it reads nothing. The wiring lives in armRestartLegs so the
witness test runs the production hooks through fastTick.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev d4196e8874a1aea34b490cb542db1c8c4eaf546b parent 40e1990
4 files changed, +375 −14
  • internal/host/agent.go +24 −12
  • internal/host/restart.go +123 −1
  • internal/host/restartgain_test.go +222 −0
  • internal/host/restartscope.go +6 −1
diff --git a/internal/host/agent.go b/internal/host/agent.go
index c19058384..580fe602f 100644
--- a/internal/host/agent.go
+++ b/internal/host/agent.go
@@ -2652,6 +2652,11 @@ type fastDeps struct {
// read returned (canaryHeads). nil: every pointer is read, as before.
canaryGate *release.ReadGate
canaryHeads map[[3]string]canaryRead // (account, app, channel) -> its group's last good read
+
+ // restartGain baselines, on every tick, the apps this box gained since
+ // the agent armed (restartGainSeed), so the first restart request after
+ // a fresh box comes up is acted on. nil = not armed.
+ restartGain func(ctx context.Context, now time.Time)
}
// canaryRead is one canary group's last good pointer read: the head it
@@ -3104,6 +3109,14 @@ func fastTick(ctx context.Context, d *fastDeps, now time.Time) {
// at least what was observed and indexActed can take it afterwards.
obs, indexOK := d.observeIndex(ctx)
armedBoxRoleReader.Load().pass(ctx, now)
+ // Before the serial read, on every tick: an app this box gained since the
+ // agent armed gets its restart baseline now, while nobody has asked for
+ // anything, rather than on the serial move that carries the first request
+ // (restartGainSeed). It reads the store only for an app it has no entry
+ // for.
+ if d.restartGain != nil {
+ d.restartGain(ctx, now)
+ }
fullRan := false
doc, _, err := release.ReadSerial(ctx, d.store)
switch {
@@ -3433,8 +3446,8 @@ func fastConvergeLoop(reg *registry.Registry, svc *activator.Service, sd *schedu
// The restart check's memory (issue #113, narrowed requests added by
// discussion #16), seeded ONCE here — two reads per app at arming (the
// app-wide generation and the narrowed request log) and never again except
- // on a serial move, which is what keeps an idle box at exactly one serial
- // GET per tick. See restartSeed for why the baseline cannot wait for the
+ // on a serial move or for an app the box gains later (restartGainSeed),
+ // which is what keeps an idle box at exactly one serial GET per tick. See restartSeed for why the baseline cannot wait for the
// first request to establish itself.
restartMem := newRestartMemory()
// `carlos instances retry` (zero-downtime spec §8.4) reaches the rollout
@@ -3456,8 +3469,13 @@ func fastConvergeLoop(reg *registry.Registry, svc *activator.Service, sd *schedu
})
}
restartSeed(ctx, reg, store, dd, restartMem)
- restartBounce := bounceFn(svc)
- restartWake := wakeFn(svc)
+ // The seed alone is not enough on a fresh box: its agent arms before
+ // reconcile has minted a route, so the seed sees no apps, and the leg
+ // runs only on a serial move. The gain hook baselines each app as its
+ // first route appears (restartGainSeed), so the first `carlos restart`
+ // after a replacement box comes up is acted on, not taken as the
+ // baseline.
+ restartGain, restartLeg := armRestartLegs(reg, store, dd, restartMem, bounceFn(svc), wakeFn(svc))
// The sidecar wake leg's own memory, seeded the same way and for the
// same reason: one silent read per served sidecar at arming so the
// FIRST wake request after a boot is acted on rather than swallowed as
@@ -3520,14 +3538,8 @@ func fastConvergeLoop(reg *registry.Registry, svc *activator.Service, sd *schedu
// The restart leg: this process's own children bounced here, everything
// unit-shaped relayed to a root pass. Deliberately NOT inside fullPass
// above — see fastTick's comment for both reasons.
- d.restartLeg = func(ctx context.Context) {
- routes, err := reg.List()
- if err != nil {
- log.Printf("agent: restart check: list routes: %v", err)
- return
- }
- restartRequestLeg(ctx, routes, store, dd, restartBounce, restartWake, restartMem)
- }
+ d.restartLeg = restartLeg
+ d.restartGain = restartGain
// The sidecar wake leg: apps/<acct>/<app>/sidecar-wake/<name>.json
// (release.SidecarWakeKey) compared and dispatched here, riding the
// same serial move as the restart leg immediately above it — see
diff --git a/internal/host/restart.go b/internal/host/restart.go
index aa068d862..5e9bf0a0b 100644
--- a/internal/host/restart.go
+++ b/internal/host/restart.go
@@ -939,6 +939,15 @@ func restartSeed(ctx context.Context, reg *registry.Registry, store release.Stor
log.Printf("agent: restart baseline: list routes: %v (the first request may need a second try)", err)
return
}
+ restartBaseline(ctx, routes, store, dd, mem)
+}
+
+// restartBaseline is restartSeed's sweep over a given route set, and every
+// rule in restartSeed's comment is this function's: record what every app's
+// two objects say, cycle nothing, apply retry grants, and give the root pass
+// its baseline (or ask it to complete one). restartGainSeed runs it again,
+// later, over just the apps a box gained after arming.
+func restartBaseline(ctx context.Context, routes []registry.Route, store release.Store, dd string, mem *restartMemory) {
_, readErrs := restartGenPass(ctx, routes, store, mem.gen)
// A first sight cycles nothing, but it hands over the retry grants in
// both logs: they are not seed-skipped (Astra plan 9), so a grant
@@ -978,5 +987,118 @@ func restartSeed(ctx context.Context, reg *registry.Registry, store release.Stor
return
}
log.Printf("agent: restart baseline: the root pass has no narrowed-restart mark for at least one app on this box " +
- "(a marks file written before narrowed restarts existed) — asked for one root pass to record it")
+ "(a marks file written before narrowed restarts existed, or an app the root pass has not seen yet) — asked for one root pass to record it")
+}
+
+// restartGainRetry is how long restartGainSeed leaves an app alone after one
+// of its objects could not be read: long enough that a bucket refusing every
+// read costs a few GETs a minute per app rather than a few every tick, short
+// enough that a blip is over before anybody types `carlos restart`.
+const restartGainRetry = time.Minute
+
+// restartGainSeed baselines every app this box serves that the agent's memory
+// has no entry for yet: the apps whose first route landed AFTER restartSeed
+// ran. fastTick runs it on every tick, and on almost every tick it reads
+// nothing, because the memory already holds every app in the route set.
+//
+// The case it exists for is a fresh box. Its agent arms before reconcile has
+// minted a single route (the G2 replacement box on dev, 2026-11-05, logged
+// "reloaded (0 route(s))" at arming and minted its instance two seconds
+// later), so restartSeed sees no apps and records nothing. The restart leg
+// runs only on a serial MOVE, so on a quiet fleet the first time that box
+// ever reads the app's objects is the move a `carlos restart` makes, and a
+// first sight is a seed. The console answered "restart requested", every
+// other box cycled, and the new box took the request as its baseline and did
+// nothing. Any other serial bump in between closes the gap by luck; a quiet
+// fleet leaves it open for as long as it stays quiet.
+//
+// Seeding when the route appears applies the seed-don't-replay rule at the
+// right moment rather than changing it: whatever the objects say when a box
+// gains an app is history its freshly started process does not owe anybody,
+// and anything recorded after that is movement. A request that lands inside
+// the one tick between a route's mint and this sweep is still taken as the
+// baseline: the same window restartSeed leaves at boot, now a couple of
+// seconds wide instead of open-ended.
+//
+// The sweep is restartBaseline over the gained apps' routes, into a scratch
+// memory, merged back only where the memory has no entry. An entry the
+// memory already holds is never overwritten, because a later value there is
+// exactly the movement the leg exists to see. An app with an object that did
+// not read keeps what did read and is tried again after restartGainRetry.
+func restartGainSeed(ctx context.Context, routes []registry.Route, store release.Store, dd string, mem *restartMemory, now time.Time) (seeded int) {
+ gained := map[[2]string]bool{}
+ for _, k := range restartApps(routes) {
+ _, haveGen := mem.gen[k]
+ _, haveScope := mem.scope[k]
+ if (haveGen && haveScope) || now.Before(mem.notBefore[k]) {
+ continue
+ }
+ gained[k] = true
+ }
+ if len(gained) == 0 {
+ return 0
+ }
+ var sub []registry.Route
+ for _, rt := range routes {
+ if gained[[2]string{release.RouteAccount(rt), rt.App}] {
+ sub = append(sub, rt)
+ }
+ }
+ scratch := newRestartMemory()
+ scratch.retryHost, scratch.pub = mem.retryHost, mem.pub
+ restartBaseline(ctx, sub, store, dd, scratch)
+ for k := range gained {
+ gen, genRead := scratch.gen[k]
+ if _, have := mem.gen[k]; !have && genRead {
+ mem.gen[k] = gen
+ }
+ id, scopeRead := scratch.scope[k]
+ if _, have := mem.scope[k]; !have && scopeRead {
+ mem.scope[k] = id
+ }
+ if ids, ok := scratch.retries[k]; ok {
+ if mem.retries[k] == nil {
+ mem.retries[k] = map[string]bool{}
+ }
+ for id := range ids {
+ mem.retries[k][id] = true
+ }
+ }
+ if !genRead || !scopeRead {
+ mem.notBefore[k] = now.Add(restartGainRetry)
+ continue
+ }
+ delete(mem.notBefore, k)
+ seeded++
+ }
+ return seeded
+}
+
+// armRestartLegs builds the agent's two restart hooks over ONE memory: gain,
+// which fastTick runs on every tick (restartGainSeed), and leg, which it runs
+// on a serial move (restartRequestLeg). The memory is passed in already
+// seeded (restartSeed), because fastConvergeLoop arms more than this file
+// knows about on it first. fastConvergeLoop takes both hooks from here, and
+// so do the tests, so the wiring a test exercises is the wiring production
+// runs.
+func armRestartLegs(reg *registry.Registry, store release.Store, dd string, mem *restartMemory,
+ bounce func(host string) bool, wake func(rt registry.Route) error) (gain func(ctx context.Context, now time.Time), leg func(ctx context.Context)) {
+ gain = func(ctx context.Context, now time.Time) {
+ routes, err := reg.List()
+ if err != nil {
+ return // the leg reports a registry it cannot list; once is enough
+ }
+ if n := restartGainSeed(ctx, routes, store, dd, mem, now); n > 0 {
+ log.Printf("agent: restart baseline: recorded %d app(s) this box gained since arming", n)
+ }
+ }
+ leg = func(ctx context.Context) {
+ routes, err := reg.List()
+ if err != nil {
+ log.Printf("agent: restart check: list routes: %v", err)
+ return
+ }
+ restartRequestLeg(ctx, routes, store, dd, bounce, wake, mem)
+ }
+ return gain, leg
}
diff --git a/internal/host/restartgain_test.go b/internal/host/restartgain_test.go
new file mode 100644
index 000000000..6be5a3da9
--- /dev/null
+++ b/internal/host/restartgain_test.go
@@ -0,0 +1,222 @@
+package host
+
+import (
+ "context"
+ "errors"
+ "os"
+ "path/filepath"
+ "slices"
+ "testing"
+ "time"
+
+ "github.com/carlosframework/platform/internal/registry"
+ "github.com/carlosframework/platform/internal/release"
+)
+
+// TestFastTickActsOnTheFirstRestartAfterAFreshBoxGainsItsRoute is the G2
+// replacement box on dev (2026-11-05), through the production wiring:
+// restartSeed and armRestartLegs exactly as fastConvergeLoop calls them,
+// fastTick, a real registry and a real store.
+//
+// The agent arms with an empty registry, reconcile mints the instance a
+// moment later, and the fleet stays quiet: no serial bump until somebody
+// types `carlos restart --host`. That bump is the first serial move this box
+// has seen, so before the gain seed the restart leg met the app for the first
+// time on the very move that carried the request, took the request as its
+// baseline, and bounced nothing, while every other box cycled.
+func TestFastTickActsOnTheFirstRestartAfterAFreshBoxGainsItsRoute(t *testing.T) {
+ ctx := context.Background()
+ f := newFastFixture(t)
+ dd := f.dataDir
+
+ mem := newRestartMemory()
+ restartSeed(ctx, f.reg, f.store, dd, mem) // arming: the registry is still empty
+ var bounced []string
+ bounce := func(h string) bool { bounced = append(bounced, h); return true }
+ gain, leg := armRestartLegs(f.reg, f.store, dd, mem, bounce, nil)
+ d := f.deps(func() {})
+ d.restartLeg, d.restartGain = leg, gain
+
+ now := time.Unix(1762382167, 0)
+ fastTick(ctx, d, now) // seeds the serial
+
+ // Reconcile mints the instance off the serial path, two seconds after arming.
+ host := "one.example.com"
+ if err := f.reg.Upsert(execRoute(host, "bab", "shop")); err != nil {
+ t.Fatalf("upsert: %v", err)
+ }
+ for i := 1; i <= 3; i++ { // quiet fleet: ticks, no serial move
+ fastTick(ctx, d, now.Add(time.Duration(i)*2*time.Second))
+ }
+
+ askScoped(t, ctx, f.store, "bab", "shop", release.RestartScopeHost, host)
+ if err := release.BumpSerial(ctx, f.store, now.Add(time.Hour)); err != nil {
+ t.Fatalf("bump serial: %v", err)
+ }
+ fastTick(ctx, d, now.Add(time.Hour))
+
+ if !slices.Equal(bounced, []string{host}) {
+ t.Fatalf("bounced %v after the first narrowed restart, want [%s]: the request was taken as the baseline", bounced, host)
+ }
+ if _, err := os.Stat(filepath.Join(dd, restartRequestMarker)); err != nil {
+ t.Fatalf("no root-pass marker after the request: %v", err)
+ }
+}
+
+// TestFastTickActsOnTheFirstAppWideRestartAfterAFreshBoxGainsItsRoute is the
+// same gap through the app-wide signal: `carlos restart --app` on a quiet
+// fleet, with a box that gained the app after arming.
+func TestFastTickActsOnTheFirstAppWideRestartAfterAFreshBoxGainsItsRoute(t *testing.T) {
+ ctx := context.Background()
+ f := newFastFixture(t)
+ mem := newRestartMemory()
+ restartSeed(ctx, f.reg, f.store, f.dataDir, mem)
+ var bounced []string
+ gain, leg := armRestartLegs(f.reg, f.store, f.dataDir, mem, func(h string) bool { bounced = append(bounced, h); return true }, nil)
+ d := f.deps(func() {})
+ d.restartLeg, d.restartGain = leg, gain
+
+ now := time.Unix(1762382167, 0)
+ fastTick(ctx, d, now)
+ if err := f.reg.Upsert(execRoute("one.example.com", "bab", "shop")); err != nil {
+ t.Fatalf("upsert: %v", err)
+ }
+ fastTick(ctx, d, now.Add(2*time.Second))
+
+ if err := release.TouchRestartGen(ctx, f.store, "bab", "shop"); err != nil {
+ t.Fatalf("touch restart gen: %v", err)
+ }
+ if err := release.BumpSerial(ctx, f.store, now.Add(time.Hour)); err != nil {
+ t.Fatalf("bump serial: %v", err)
+ }
+ fastTick(ctx, d, now.Add(time.Hour))
+ if !slices.Equal(bounced, []string{"one.example.com"}) {
+ t.Fatalf("bounced %v, want the gained app's instance", bounced)
+ }
+}
+
+// TestRestartGainSeedNeverOverwritesAnEntry: an app the memory already holds
+// keeps its entry even when its object has moved since, because that later
+// value is the movement the leg exists to see. Only the app with no entry is
+// read and recorded.
+func TestRestartGainSeedNeverOverwritesAnEntry(t *testing.T) {
+ ctx, s := genKit(t)
+ dd := t.TempDir()
+ known := []registry.Route{execRoute("a.example.com", "bab", "shop")}
+ mem := newRestartMemory()
+ restartBaseline(ctx, known, s, dd, mem)
+ before := mem.gen[[2]string{"bab", "shop"}]
+
+ if err := release.TouchRestartGen(ctx, s, "bab", "shop"); err != nil {
+ t.Fatalf("touch: %v", err)
+ }
+ both := append(known, execRoute("b.example.com", "bab", "cafe"))
+ if n := restartGainSeed(ctx, both, s, dd, mem, time.Now()); n != 1 {
+ t.Fatalf("seeded %d apps, want 1 (only the gained one)", n)
+ }
+ if got := mem.gen[[2]string{"bab", "shop"}]; got != before {
+ t.Fatalf("the gain seed overwrote a known app's mark: %q -> %q", before, got)
+ }
+ if _, ok := mem.scope[[2]string{"bab", "cafe"}]; !ok {
+ t.Fatal("the gained app has no narrowed mark")
+ }
+ var bounced []string
+ if !restartRequestLeg(ctx, both, s, dd, func(h string) bool { bounced = append(bounced, h); return true }, nil, mem) {
+ t.Fatal("the known app's restart was lost")
+ }
+ if !slices.Equal(bounced, []string{"a.example.com"}) {
+ t.Fatalf("bounced %v, want only the app that was asked", bounced)
+ }
+}
+
+// TestRestartGainSeedReadsNothingAtRest: with every app in memory a tick
+// costs no store read, which is what keeps an idle box at one serial GET per
+// tick.
+func TestRestartGainSeedReadsNothingAtRest(t *testing.T) {
+ ctx, base := genKit(t)
+ dd := t.TempDir()
+ routes := []registry.Route{execRoute("a.example.com", "bab", "shop")}
+ mem := newRestartMemory()
+ restartBaseline(ctx, routes, base, dd, mem)
+ cs := &countingStore{Store: base}
+ if n := restartGainSeed(ctx, routes, cs, dd, mem, time.Now()); n != 0 {
+ t.Fatalf("seeded %d apps at rest", n)
+ }
+ if len(cs.gets) != 0 {
+ t.Fatalf("store reads at rest: %v", cs.gets)
+ }
+}
+
+// TestRestartGainSeedBacksOffAnUnreadableApp: an object that will not read is
+// tried again after restartGainRetry, not on every tick, and the half that
+// did read is kept.
+func TestRestartGainSeedBacksOffAnUnreadableApp(t *testing.T) {
+ ctx, base := genKit(t)
+ dd := t.TempDir()
+ routes := []registry.Route{execRoute("a.example.com", "bab", "shop")}
+ fs := &failingKeyStore{Store: base, failKey: release.RestartScopeKey("bab", "shop"), err: errors.New("503 slow down")}
+ cs := &countingStore{Store: fs}
+ mem := newRestartMemory()
+ now := time.Now()
+ k := [2]string{"bab", "shop"}
+
+ if n := restartGainSeed(ctx, routes, cs, dd, mem, now); n != 0 {
+ t.Fatalf("seeded %d apps with the log unreadable", n)
+ }
+ if _, ok := mem.gen[k]; !ok {
+ t.Fatal("the generation that did read was dropped")
+ }
+ if _, ok := mem.scope[k]; ok {
+ t.Fatal("an unreadable log was recorded")
+ }
+ reads := cs.gets[release.RestartScopeKey("bab", "shop")]
+ restartGainSeed(ctx, routes, cs, dd, mem, now.Add(2*time.Second))
+ if got := cs.gets[release.RestartScopeKey("bab", "shop")]; got != reads {
+ t.Fatalf("retried inside the back-off: %d reads, want %d", got, reads)
+ }
+
+ fs.failKey = ""
+ if n := restartGainSeed(ctx, routes, cs, dd, mem, now.Add(restartGainRetry)); n != 1 {
+ t.Fatalf("seeded %d apps after the back-off, want 1", n)
+ }
+ if mem.scope[k] != release.RestartScopeNone {
+ t.Fatalf("narrowed mark %q, want none", mem.scope[k])
+ }
+}
+
+// TestRestartGainSeedKeepsTheHalfItAlreadyHas: an app whose generation was
+// recorded and whose request log was not is read again for the log alone. A
+// generation that moved in between is a request, and must reach the leg as
+// one rather than being re-read into the baseline.
+func TestRestartGainSeedKeepsTheHalfItAlreadyHas(t *testing.T) {
+ ctx, base := genKit(t)
+ dd := t.TempDir()
+ routes := []registry.Route{execRoute("a.example.com", "bab", "shop")}
+ fs := &failingKeyStore{Store: base, failKey: release.RestartScopeKey("bab", "shop"), err: errors.New("503 slow down")}
+ mem := newRestartMemory()
+ now := time.Now()
+ restartGainSeed(ctx, routes, fs, dd, mem, now) // generation recorded, log not
+ k := [2]string{"bab", "shop"}
+ before, ok := mem.gen[k]
+ if !ok {
+ t.Fatal("no generation mark after the first try")
+ }
+
+ if err := release.TouchRestartGen(ctx, base, "bab", "shop"); err != nil {
+ t.Fatalf("touch: %v", err)
+ }
+ fs.failKey = ""
+ if n := restartGainSeed(ctx, routes, fs, dd, mem, now.Add(restartGainRetry)); n != 1 {
+ t.Fatalf("seeded %d apps, want 1", n)
+ }
+ if mem.gen[k] != before {
+ t.Fatalf("the retry re-read the generation into the baseline: %q -> %q", before, mem.gen[k])
+ }
+ var bounced []string
+ if !restartRequestLeg(ctx, routes, fs, dd, func(h string) bool { bounced = append(bounced, h); return true }, nil, mem) {
+ t.Fatal("the app-wide request made between the two tries was lost")
+ }
+ if !slices.Equal(bounced, []string{"a.example.com"}) {
+ t.Fatalf("bounced %v", bounced)
+ }
+}
diff --git a/internal/host/restartscope.go b/internal/host/restartscope.go
index 0256d01bb..11bd319d2 100644
--- a/internal/host/restartscope.go
+++ b/internal/host/restartscope.go
@@ -70,10 +70,15 @@ type restartMemory struct {
// every retry's grant is verified against (spec §11). Nil drops every
// retry: a box that cannot tell a grant from a forgery clears nothing.
pub ed25519.PublicKey
+ // notBefore throttles restartGainSeed's retries after a read error, per
+ // app, so an object that will not read is not asked for on every tick.
+ // Nothing else reads it.
+ notBefore map[[2]string]time.Time
}
func newRestartMemory() *restartMemory {
- return &restartMemory{gen: map[[2]string]string{}, scope: map[[2]string]string{}, retries: map[[2]string]map[string]bool{}}
+ return &restartMemory{gen: map[[2]string]string{}, scope: map[[2]string]string{}, retries: map[[2]string]map[string]bool{},
+ notBefore: map[[2]string]time.Time{}}
}
// scopedRestart is one app's outstanding narrowed work: the requests this box