oficina / equipo Public

runner: stamp readings; refresh Codex by account/rateLimits/read

Every reading is stamped with when it was taken, and staleness reads
that time. In production a Codex reading older than 15 minutes is
refreshed through the login probe's app-server in the source home under
its lock (account/read, then account/rateLimits/read, never a thread or
a turn). Claude stays as last reported. The first hello withholds the
reading times; a protocol 15 welcome brings a prompt hello with them.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev e52eb7528b094a783a41db73d5f871de9f6e8442 parent 399029a
8 files changed, +382 −7
  • internal/runner/provider/login_probe.go +62 −1
  • internal/runner/provider/reading_test.go +67 −0
  • internal/runner/thin/conns.go +28 −5
  • internal/runner/thin/link.go +4 −1
  • internal/runner/thin/readings.go +59 −0
  • internal/runner/thin/readings_test.go +149 −0
  • internal/runner/thin/run.go +8 −0
  • internal/runner/thin/schedule.go +5 −0
diff --git a/internal/runner/provider/login_probe.go b/internal/runner/provider/login_probe.go
index 3013978e..d3f4d983 100644
--- a/internal/runner/provider/login_probe.go
+++ b/internal/runner/provider/login_probe.go
@@ -33,6 +33,31 @@ func RefreshLogin(ctx context.Context, b Binding, options LoginOptions) (Identit
}
func (d *terminalLoginDriver) probe(ctx context.Context, b Binding, refresh bool) (Identity, error) {
+ return d.probeReading(ctx, b, refresh, nil)
+}
+
+// ReadCodexLimits is the reading probe for a Codex home (connectors spec
+// rev 3.2 §6, the controller's ruling #7): the same unconfined app-server
+// in the source home, under the caller's lock, with the same environment
+// and egress as the hourly refresh, asked account/read and then
+// account/rateLimits/read. It starts no thread and no turn. It returns
+// the account's identity and the rateLimits object as the app-server
+// sent it.
+func ReadCodexLimits(ctx context.Context, b Binding, options LoginOptions) (Identity, json.RawMessage, error) {
+ if b.Provider != "codex" {
+ return Identity{}, nil, ErrLoginUnavailable
+ }
+ var limits json.RawMessage
+ id, err := (&terminalLoginDriver{options: options}).probeReading(ctx, b, false, &limits)
+ if err != nil {
+ return Identity{}, nil, err
+ }
+ return id, limits, nil
+}
+
+// probeReading is probe, and for a Codex home with limits set, the
+// account's rate limits read after its identity.
+func (d *terminalLoginDriver) probeReading(ctx context.Context, b Binding, refresh bool, limits *json.RawMessage) (Identity, error) {
if b.provenance != nil {
return Identity{}, ErrPolicy
}
@@ -161,7 +186,43 @@ func (d *terminalLoginDriver) probe(ctx context.Context, b Binding, refresh bool
return Identity{}, ErrLoginRequired
}
}
- return CodexLoginIdentity(raw)
+ id, err := CodexLoginIdentity(raw)
+ if err != nil || limits == nil {
+ return id, err
+ }
+ // The rate limits: one request, its answer; notifications the
+ // app-server sends meanwhile (a rate-limit update among them) are
+ // passed over, a request it makes of us ends the read.
+ if json.NewEncoder(in).Encode(map[string]any{"id": "3", "method": "account/rateLimits/read", "params": nil}) != nil {
+ return Identity{}, ErrIdentity
+ }
+ for frames := 0; frames < 64; frames++ {
+ if !scan.Scan() {
+ return Identity{}, ErrIdentity
+ }
+ var r struct {
+ Method string
+ ID, Params, Result, Error json.RawMessage
+ }
+ if json.Unmarshal(scan.Bytes(), &r) != nil {
+ return Identity{}, ErrIdentity
+ }
+ if len(r.ID) == 0 && r.Method != "" {
+ continue
+ }
+ if string(r.ID) != `"3"` || r.Method != "" || len(r.Error) != 0 {
+ return Identity{}, ErrIdentity
+ }
+ var v struct {
+ RateLimits json.RawMessage `json:"rateLimits"`
+ }
+ if json.Unmarshal(r.Result, &v) != nil || len(v.RateLimits) == 0 || string(v.RateLimits) == "null" {
+ return Identity{}, ErrIdentity
+ }
+ *limits = v.RateLimits
+ return id, nil
+ }
+ return Identity{}, ErrIdentity
}
func ClaudeLoginIdentity(raw []byte) (Identity, error) {
diff --git a/internal/runner/provider/reading_test.go b/internal/runner/provider/reading_test.go
new file mode 100644
index 00000000..21d8d1c2
--- /dev/null
+++ b/internal/runner/provider/reading_test.go
@@ -0,0 +1,67 @@
+package provider
+
+import (
+ "context"
+ "encoding/json"
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+)
+
+// The controller's ruling #7 (connectors spec rev 3.2 §6): a Codex
+// reading is refreshed by the app-server's account/rateLimits/read, in
+// the source home with the login probe's environment, after account/read
+// and never with a thread or a turn. The fake app-server answers only
+// those requests and fails on anything else; a notification it sends in
+// between is passed over.
+func TestReadCodexLimitsWithoutTurn(t *testing.T) {
+ root := t.TempDir()
+ binary := filepath.Join(root, "codex")
+ script := `#!/bin/sh
+case "$*" in
+ '--version') echo 'codex-cli 0.165.0'; exit 0;;
+ 'login --device-auth --help') exit 0;;
+esac
+[ "$PWD" = "$HOME" ] || exit 20
+[ "$CODEX_HOME" = "$HOME" ] || exit 22
+IFS= read -r line
+printf '{"id":"1","result":{"userAgent":"equipo/0.165.0 fixture","codexHome":"%s"}}\n' "$CODEX_HOME"
+IFS= read -r line
+case "$line" in *'"initialized"'*) ;; *) exit 23;; esac
+IFS= read -r line
+case "$line" in *'"account/read"'*'"refreshToken":false'*) ;; *) exit 24;; esac
+echo '{"id":"2","result":{"account":{"type":"chatgpt","email":"fixture@example.invalid"}}}'
+IFS= read -r line
+case "$line" in *'"account/rateLimits/read"'*) ;; *) echo "$line" > "$HOME/other"; exit 25;; esac
+echo '{"method":"account/rateLimits/updated","params":{"rateLimits":{}}}'
+echo '{"id":"3","result":{"rateLimits":{"primary":{"usedPercent":61,"windowDurationMins":300,"resetsAt":1762941600},"secondary":{"usedPercent":12,"windowDurationMins":10080,"resetsAt":1763287200},"planType":"pro","rateLimitReachedType":null}}}'
+echo done > "$HOME/read"
+IFS= read -r line && echo "$line" > "$HOME/other"
+exit 0
+`
+ if err := os.WriteFile(binary, []byte(script), 0700); err != nil {
+ t.Fatal(err)
+ }
+ id, raw, err := ReadCodexLimits(context.Background(), Binding{Home: root, Provider: "codex"}, LoginOptions{CodexPath: binary})
+ if err != nil || id.AccountID != "fixture@example.invalid" {
+ t.Fatal(id, err)
+ }
+ var v struct {
+ Primary struct {
+ UsedPercent float64 `json:"usedPercent"`
+ } `json:"primary"`
+ }
+ if json.Unmarshal(raw, &v) != nil || v.Primary.UsedPercent != 61 {
+ t.Fatalf("limits %s", raw)
+ }
+ if _, err := os.Stat(filepath.Join(root, "read")); err != nil {
+ t.Fatal("the rate limits were not read", err)
+ }
+ if other, err := os.ReadFile(filepath.Join(root, "other")); err == nil && strings.TrimSpace(string(other)) != "" {
+ t.Fatalf("the probe sent more: %s", other)
+ }
+ if _, _, err := ReadCodexLimits(context.Background(), Binding{Home: root, Provider: "claude"}, LoginOptions{CodexPath: binary}); err == nil {
+ t.Fatal("a Claude home was read: Claude has no reading without a turn")
+ }
+}
diff --git a/internal/runner/thin/conns.go b/internal/runner/thin/conns.go
index 8171b4cc..6603cc4a 100644
--- a/internal/runner/thin/conns.go
+++ b/internal/runner/thin/conns.go
@@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"log/slog"
+ "slices"
"sync"
"time"
@@ -387,7 +388,7 @@ func (c *Conns) Limit(id string, l connect.Limit) time.Time {
until, wait := connect.LimitedUntil(l.ResetsAt, cs.CoolOff, c.clock())
cs.State, cs.ResetsAt, cs.CoolOff, cs.Window = wire.ConnLimited, until, wait, l.Window
if l.Headroom != nil {
- cs.Headroom = l.Headroom
+ cs.Headroom = c.stamped(l.Headroom)
}
c.save(id, cs)
c.mu.Unlock()
@@ -413,10 +414,9 @@ func (c *Conns) Observe(id string, l connect.Limit, plan string) {
was := cs.State
read := false
if l.Headroom != nil {
- before, _ := json.Marshal(cs.Headroom)
- after, _ := json.Marshal(l.Headroom)
- read = string(before) != string(after)
- cs.Headroom = l.Headroom
+ h := c.stamped(l.Headroom)
+ read = reported(cs.Headroom) != reported(h)
+ cs.Headroom = h
}
if plan != "" {
cs.Plan = plan
@@ -434,6 +434,29 @@ func (c *Conns) Observe(id string, l connect.Limit, plan string) {
}
}
+// stamped is a reading as taken now: the provider's figures and the
+// time the runner read them (connectors spec rev 3.2 §6), so the age of
+// every reading can be shown. A copy: the caller's is untouched.
+func (c *Conns) stamped(h *wire.Headroom) *wire.Headroom {
+ out := *h
+ out.Windows = slices.Clone(h.Windows)
+ at := c.clock().UTC()
+ out.CheckedAt = &at
+ return &out
+}
+
+// reported is what a reading says, without when it was taken: a reading
+// that says the same again is no new reading for held steps.
+func reported(h *wire.Headroom) string {
+ if h == nil {
+ return ""
+ }
+ v := *h
+ v.CheckedAt = nil
+ raw, _ := json.Marshal(v)
+ return string(raw)
+}
+
// LimitedFrom is what a switched line says about a connection the
// provider refused: its provider, the window and the reset it reported.
func (c *Conns) LimitedFrom(id string) *wire.Switched {
diff --git a/internal/runner/thin/link.go b/internal/runner/thin/link.go
index 1f1c14f4..77b40248 100644
--- a/internal/runner/thin/link.go
+++ b/internal/runner/thin/link.go
@@ -500,6 +500,9 @@ func (l *Link) handshake(ctx context.Context, offer int) (*websocket.Conn, *wire
// Nor the Research addresses, which an instance below protocol 13
// refuses the same way.
heldResearch := withoutResearch(&hello)
+ // Nor a reading's time, which an instance below protocol 15 refuses
+ // the same way.
+ heldReadings := wire.WithoutReadingTimes(&hello)
l.mu.Lock()
hello.Generation, hello.Protocol, l.proto = l.generation, offer, offer
l.mu.Unlock()
@@ -548,7 +551,7 @@ func (l *Link) handshake(ctx context.Context, offer int) (*websocket.Conn, *wire
}
l.mu.Lock()
l.proto, l.role = min(spoken, offer), role
- if held && l.proto >= wire.ProtocolEmptyRepos || heldPreviews && l.proto >= wire.ProtocolExplore || heldResearch && l.proto >= wire.ProtocolResearch {
+ if held && l.proto >= wire.ProtocolEmptyRepos || heldPreviews && l.proto >= wire.ProtocolExplore || heldResearch && l.proto >= wire.ProtocolResearch || heldReadings && l.proto >= wire.ProtocolReadings {
l.helloSoon = true
}
l.mu.Unlock()
diff --git a/internal/runner/thin/readings.go b/internal/runner/thin/readings.go
new file mode 100644
index 00000000..e1bd3bbc
--- /dev/null
+++ b/internal/runner/thin/readings.go
@@ -0,0 +1,59 @@
+package thin
+
+import (
+ "context"
+ "encoding/json"
+ "errors"
+ "time"
+
+ "amadan.net/oficina/equipo/internal/runner"
+ "amadan.net/oficina/equipo/internal/runner/connect"
+ "amadan.net/oficina/equipo/internal/runner/provider"
+ "amadan.net/oficina/equipo/internal/wire"
+)
+
+// errNoReading: the app-server answered, but with no rate limits Equipo
+// can read.
+var errNoReading = errors.New("no reading")
+
+// CodexReading is the production reading probe (connectors spec rev 3.2
+// §6, the controller's ruling #7): a Codex subscription's headroom is
+// refreshed with the app-server's account/rateLimits/read, run as the
+// hourly refresh runs (the source home under its lock, the login
+// probe's environment, no namespace and no egress relay, never a thread
+// or a turn). Claude has no reading without a model turn: it stays as
+// last reported, and so does every other connection (nil, nil).
+func CodexReading(c runner.Config, read func(context.Context, provider.Binding) (provider.Identity, json.RawMessage, error), wait time.Duration) func(context.Context, runner.AccountBinding) (*connect.Limit, error) {
+ if read == nil {
+ options := provider.LoginOptions{ClaudePath: c.ClaudePath, CodexPath: c.CodexPath}
+ read = func(ctx context.Context, b provider.Binding) (provider.Identity, json.RawMessage, error) {
+ return provider.ReadCodexLimits(ctx, b, options)
+ }
+ }
+ return func(ctx context.Context, b runner.AccountBinding) (*connect.Limit, error) {
+ if b.Provider != wire.ProviderCodex || b.Kind == wire.KindToken || b.Kind == wire.KindAPIKey {
+ return nil, nil
+ }
+ direct, err := c.DirectBinding(b)
+ if err != nil {
+ return nil, err
+ }
+ var raw json.RawMessage
+ lease, err := provider.OpenRuntimeCopy(ctx, direct, func(ctx context.Context, pb provider.Binding) (provider.Identity, error) {
+ id, limits, err := read(ctx, pb)
+ raw = limits
+ return id, err
+ }, wait)
+ if lease != nil {
+ lease.Close()
+ }
+ if err != nil {
+ return nil, err
+ }
+ l, _, ok := connect.CodexRateLimits(raw)
+ if !ok {
+ return nil, errNoReading
+ }
+ return &l, nil
+ }
+}
diff --git a/internal/runner/thin/readings_test.go b/internal/runner/thin/readings_test.go
new file mode 100644
index 00000000..d5c73bf8
--- /dev/null
+++ b/internal/runner/thin/readings_test.go
@@ -0,0 +1,149 @@
+package thin
+
+import (
+ "context"
+ "encoding/json"
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+ "time"
+
+ "amadan.net/oficina/equipo/internal/runner"
+ "amadan.net/oficina/equipo/internal/runner/connect"
+ "amadan.net/oficina/equipo/internal/runner/provider"
+ "amadan.net/oficina/equipo/internal/wire"
+)
+
+// The controller's ruling #7: in production a stale Codex reading is
+// refreshed by the app-server's account/rateLimits/read, run as the
+// hourly refresh runs (the source home under its lock); Claude has no
+// such read and stays as last reported. The reading is stamped with the
+// time it was taken, and a reading that says the same again is no new
+// reading for held steps.
+func TestCodexReadingInProduction(t *testing.T) {
+ r := newConnectorRig(t)
+ r.cfg.ScratchRoot = filepath.Join(t.TempDir(), "scratch")
+ reads := 0
+ limits := json.RawMessage(`{"primary":{"usedPercent":61,"windowDurationMins":300,"resetsAt":1762941600},"secondary":{"usedPercent":12,"windowDurationMins":10080,"resetsAt":1763287200},"planType":"pro","rateLimitReachedType":null}`)
+ read := func(_ context.Context, b provider.Binding) (provider.Identity, json.RawMessage, error) {
+ reads++
+ if b.Home != r.home || b.Provider != wire.ProviderCodex {
+ t.Fatalf("read %+v", b)
+ }
+ return provider.Identity{AccountID: "a@example.com", ProviderVersion: "1"}, limits, nil
+ }
+ reading := CodexReading(r.cfg, read, time.Second)
+ b, ok := r.conns.Binding("codex-1")
+ if !ok {
+ t.Fatal("no binding")
+ }
+ // The home as a sign-in leaves it: its identity and its credential.
+ identity, _ := json.Marshal(b.ExpectedIdentity)
+ for name, body := range map[string][]byte{provider.IdentityFile: identity, "auth.json": []byte(`{"tokens":{}}`)} {
+ if err := os.WriteFile(filepath.Join(r.home, name), body, 0600); err != nil {
+ t.Fatal(err)
+ }
+ }
+ l, err := reading(context.Background(), b)
+ if err != nil || l == nil || reads != 1 || len(l.Headroom.Windows) != 2 || l.Headroom.Windows[0].UsedPercent != 61 {
+ t.Fatalf("codex: %+v %v (%d reads)", l, err, reads)
+ }
+ for _, other := range []runner.AccountBinding{{ID: "claude-1", Provider: wire.ProviderClaude}, {ID: "codex-t", Provider: wire.ProviderCodex, Kind: wire.KindToken}, {ID: "tensorx-1", Provider: wire.ProviderTensorX, Kind: wire.KindAPIKey}} {
+ if l, err := reading(context.Background(), other); l != nil || err != nil {
+ t.Fatalf("%s read: %+v %v", other.ID, l, err)
+ }
+ }
+ if reads != 1 {
+ t.Fatalf("%d reads: only Codex subscriptions are read", reads)
+ }
+
+ // Observe stamps the reading's time; the same figures again move the
+ // time only, and wake no held step.
+ woken := 0
+ r.conns.Readings = func() { woken++ }
+ at := time.Date(2026, 11, 12, 10, 0, 0, 0, time.UTC)
+ r.conns.now = func() time.Time { return at }
+ r.conns.Observe("codex-1", *l, "")
+ held := func() *wire.Headroom {
+ for _, h := range r.conns.Held() {
+ if h.LocalID == "codex-1" {
+ return h.Headroom
+ }
+ }
+ return nil
+ }
+ if h := held(); h == nil || h.CheckedAt == nil || !h.CheckedAt.Equal(at) {
+ t.Fatalf("reading time %+v", h)
+ }
+ at = at.Add(20 * time.Minute)
+ r.conns.Observe("codex-1", *l, "")
+ if h := held(); h.CheckedAt == nil || !h.CheckedAt.Equal(at) || woken != 0 {
+ t.Fatalf("the same reading again: time %v, woke %d", h.CheckedAt, woken)
+ }
+ if l.Headroom.CheckedAt != nil {
+ t.Fatal("Observe changed the caller's reading")
+ }
+ l.Headroom.Windows[0].UsedPercent = 70
+ r.conns.Observe("codex-1", *l, "")
+ if woken != 1 {
+ t.Fatalf("a new reading woke %d", woken)
+ }
+}
+
+// A reading's age is its own time: a connection whose row was written
+// just now (a state change) but whose reading is sixteen minutes old is
+// refreshed before the scheduler decides.
+func TestScheduleRefreshUsesReadingTime(t *testing.T) {
+ r := newSchedRig(t, "claude-a")
+ base := time.Now()
+ r.conns.now = func() time.Time { return base.Add(-16 * time.Minute) }
+ r.reading("claude-a", 0.9, base.Add(time.Hour))
+ r.conns.now = func() time.Time { return base }
+ // The row is written again now, the reading unchanged.
+ r.conns.mu.Lock()
+ cs := r.conns.live["claude-a"]
+ r.conns.save("claude-a", cs)
+ r.conns.mu.Unlock()
+ probes := 0
+ r.conns.Reading = func(context.Context, runner.AccountBinding) (*connect.Limit, error) {
+ probes++
+ at := base.Add(time.Hour)
+ return &connect.Limit{Headroom: &wire.Headroom{Windows: []wire.Window{{Name: "five_hour", UsedPercent: 10, Minutes: 300, ResetsAt: &at}}}}, nil
+ }
+ row := stepRow{DispatchCommand: wire.DispatchCommand{Person: connPerson, TaskID: "tk", StepID: "s", Attempt: 1, Specialist: wire.SpecialistEngineering}}
+ if d, _ := r.conns.decide(r.ctx, row, connect.RoleEngineering, "", nil, nil); !d.d.Run || probes != 1 {
+ t.Fatalf("a sixteen-minute-old reading: %+v, %d probes", d.d, probes)
+ }
+}
+
+// TestHandshakeHelloWithholdsReadingTimes: the first hello goes out
+// before the instance says its protocol, so no headroom in it says when
+// it was read; after a protocol 15 welcome the next hello, sent at once,
+// does; at 14 none ever does.
+func TestHandshakeHelloWithholdsReadingTimes(t *testing.T) {
+ at := time.Date(2026, 11, 12, 9, 58, 0, 0, time.UTC)
+ hello := func() wire.Hello {
+ return wire.Hello{Repos: []string{}, CreateIn: []string{}, Catalog: []wire.RepoInfo{}, Versions: map[string]string{"runner": "test", "claude": "2.1.312"},
+ Connections: []wire.HeldConnection{{LocalID: "codex-1", Person: connPerson, Provider: wire.ProviderCodex, Kind: wire.KindSubscription, State: wire.ConnHealthy, Headroom: &wire.Headroom{Windows: []wire.Window{}, CheckedAt: &at}}}}
+ }
+ for _, protocol := range []int{wire.ProtocolScheduler, wire.ProtocolReadings} {
+ inst := newFakeInstance(t)
+ link, _, _, _ := startLinkWith(t, inst.url, &stubCoordinator{}, hello)
+ c := <-inst.conns
+ _, raw := read(t, c, wire.FrameHello)
+ if strings.Contains(string(raw), `"checked_at"`) || !strings.Contains(string(raw), `"codex-1"`) {
+ t.Fatalf("handshake hello: %s", raw)
+ }
+ welcome, _ := json.Marshal(wire.Frame{ID: "w-r", Type: wire.FrameWelcome, Data: json.RawMessage(`{"generation":3,"runner_id":"RUNNER1","protocol":` + itoa(int64(protocol)) + `,"role":"primary"}`)})
+ write(t, c, welcome)
+ read(t, c, wire.FrameSync)
+ if protocol < wire.ProtocolReadings {
+ link.HelloSoon()
+ }
+ _, raw = read(t, c, wire.FrameHello)
+ if got := strings.Contains(string(raw), `"checked_at":"2026-11-12T09:58:00Z"`); got != (protocol >= wire.ProtocolReadings) {
+ t.Fatalf("protocol %d hello: %s", protocol, raw)
+ }
+ }
+}
diff --git a/internal/runner/thin/run.go b/internal/runner/thin/run.go
index 55f64bcc..b3093e47 100644
--- a/internal/runner/thin/run.go
+++ b/internal/runner/thin/run.go
@@ -5,6 +5,7 @@ import (
"context"
"crypto/sha256"
"encoding/hex"
+ "encoding/json"
"errors"
"fmt"
"io/fs"
@@ -96,6 +97,9 @@ type Options struct {
// the environment.
ResearchFetch func(context.Context, webread.LaunchConfig, webread.Plan) error
ResearchAddresses func() ([]netip.Addr, error)
+ // ReadCodexLimits stands in for the Codex reading probe's app-server
+ // read (tests); nil: provider.ReadCodexLimits with the pinned CLI.
+ ReadCodexLimits func(context.Context, provider.Binding) (provider.Identity, json.RawMessage, error)
}
// OptionsFromEnv reads the runner's optional thin settings.
@@ -186,6 +190,10 @@ func Run(ctx context.Context, c runner.Config, o Options) error {
tools := &bridge{State: state, Link: link, Forge: fg, Clones: clones, Index: index, Logger: o.Logger, SessionRoot: o.Claude.SessionRoot, Activity: link.Activity}
o.Claude.Tools, o.Claude.Catalog = tools.Call, index.Catalog
conns := &Conns{Runner: c, State: state, Logger: o.Logger, Refresh: o.Claude.Refresh, LockWait: o.Claude.LockWait}
+ // A Codex reading older than fifteen minutes is refreshed without a
+ // model turn before the scheduler decides; Claude's stays as last
+ // reported (connectors spec rev 3.2 §6).
+ conns.Reading = CodexReading(c, o.ReadCodexLimits, o.Claude.LockWait)
// A token check is a real request; its usage counts like any other,
// and the rate limit the provider reported on it is recorded as a
// turn's is: headroom, or limited until its reset. Not for a refused
diff --git a/internal/runner/thin/schedule.go b/internal/runner/thin/schedule.go
index aa1484fc..c6f1cce5 100644
--- a/internal/runner/thin/schedule.go
+++ b/internal/runner/thin/schedule.go
@@ -259,9 +259,14 @@ func (c *Conns) refreshReadings(ctx context.Context, person, role string) {
if c.Reading == nil || !known {
continue
}
+ // A reading's age is its own time (CheckedAt); a row from before
+ // readings were stamped falls back to when the row was written.
var updated string
_ = c.State.db.QueryRowContext(ctx, "SELECT updated_at FROM conn_state WHERE id=?", b.ID).Scan(&updated)
at, err := time.Parse(time.RFC3339Nano, updated)
+ if cs.Headroom != nil && cs.Headroom.CheckedAt != nil {
+ at, err = *cs.Headroom.CheckedAt, nil
+ }
if err == nil && now.Sub(at) < staleAfter {
continue
}