hub: box presence from the wait channel + delete a switched-off box at once (R-30, D2)
Slice 1: intent.Hub records one start/end per GET /api/v1/wait request and answers Presence(customer, now): connected (hold open, or one started < 333 s = 243 s cadence + 90 s grace ago), not connected since T, or unknown (hub up < 333 s; in memory only). The host page shows "Box connection". One DEBUG line per presence change. Slice 2 (operator ruling D2, 2026-10-08, 09 §3 decision 186): a host that is online by its report clock but whose box has had no wait-channel connection for >= 360 s may be deleted at once after the tick "I checked: the box is off". Presence is re-read at POST time; the tick alone, unknown presence, a connected box or a shorter gap keep today's 409. The delete logs the operator channel and saves one host_deleted_box_off event. RESET and the customer-delete cascade are unchanged. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0159rPz1ZhFKsS53msqPYxtS
This commit is contained in:
1 parent
e6184353db
commit
d604e624a3
9 files changed
+685
-12
No files matched your search
@@ -32,14 +32,24 @@ type Hub struct {
|
||||
waiters map[string][]chan struct{} // customerID -> registered wake channels (buffered, cap 1)
|
||||
closed chan struct{} // closed by Close() to wake every waiter
|
||||
isClosed bool
|
||||
|
||||
// R-30 box presence (presence.go): per-customer wait-request bookkeeping, the hub's own start
|
||||
// time (a hub younger than PresenceConnectedWindow answers "unknown"), a clock seam and an
|
||||
// optional debug logger.
|
||||
presence map[string]*presenceRec
|
||||
born time.Time
|
||||
now func() time.Time
|
||||
logf func(format string, args ...any)
|
||||
}
|
||||
|
||||
// New builds an empty intent hub.
|
||||
func New() *Hub {
|
||||
return &Hub{
|
||||
gen: make(map[string]uint64),
|
||||
waiters: make(map[string][]chan struct{}),
|
||||
closed: make(chan struct{}),
|
||||
gen: make(map[string]uint64),
|
||||
waiters: make(map[string][]chan struct{}),
|
||||
closed: make(chan struct{}),
|
||||
presence: make(map[string]*presenceRec),
|
||||
born: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,170 @@
|
||||
package intent
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Box presence from the wait channel (R-30, design `audits/day-2026-10-08/design-R-30.md` option B).
|
||||
//
|
||||
// A healthy controller holds GET /api/v1/wait for waitMaxHold (240 s) and starts the next hold at
|
||||
// once; measured on the ingress 2026-10-08: one new wait per box every 241–243 s. So "a hold is open,
|
||||
// or one STARTED less than PresenceConnectedWindow ago" means the box's controller is running and
|
||||
// reaching the hub. A box that loses power ends its hold normally (the hub ends it at 240 s) and then
|
||||
// no new wait arrives — that gap is the signal.
|
||||
//
|
||||
// IN MEMORY ONLY, like the generation counter. A hub that started less than PresenceConnectedWindow
|
||||
// ago has not had the chance to see a healthy box's next wait, so it answers PresenceUnknown — never
|
||||
// PresenceNotConnected. Callers fall back to the report clock on Unknown (the safe direction).
|
||||
// Pinned by presence_test.go TestPresence_HubRestartIsUnknown.
|
||||
const (
|
||||
// PresenceWaitCadence is the measured interval between two wait starts from one healthy box.
|
||||
PresenceWaitCadence = 243 * time.Second
|
||||
// PresenceGrace absorbs a slow reconnect (TLS, DNS, a busy box) on top of the cadence.
|
||||
PresenceGrace = 90 * time.Second
|
||||
// PresenceConnectedWindow = cadence + grace (333 s): a wait started within it means "connected".
|
||||
PresenceConnectedWindow = PresenceWaitCadence + PresenceGrace
|
||||
)
|
||||
|
||||
// Presence states.
|
||||
const (
|
||||
PresenceConnected = "connected"
|
||||
PresenceNotConnected = "not_connected"
|
||||
PresenceUnknown = "unknown"
|
||||
)
|
||||
|
||||
// Presence is one customer's box-connection verdict at a moment.
|
||||
type Presence struct {
|
||||
State string
|
||||
// Since is the start of the not-connected span (the last hold's end, or the hub's own start when
|
||||
// no wait was seen since). Zero unless State == PresenceNotConnected.
|
||||
Since time.Time
|
||||
// NeverSeen: no wait from this customer since the hub started (Since is then the hub's start).
|
||||
NeverSeen bool
|
||||
}
|
||||
|
||||
// NotConnectedFor is how long the box has been without a connection at now (0 unless not connected).
|
||||
func (p Presence) NotConnectedFor(now time.Time) time.Duration {
|
||||
if p.State != PresenceNotConnected {
|
||||
return 0
|
||||
}
|
||||
if d := now.Sub(p.Since); d > 0 {
|
||||
return d
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
type presenceRec struct {
|
||||
open int
|
||||
lastStart time.Time
|
||||
lastEnd time.Time
|
||||
lastState string // for the debug line on a change
|
||||
}
|
||||
|
||||
func (h *Hub) clock() time.Time {
|
||||
if h.now != nil {
|
||||
return h.now()
|
||||
}
|
||||
return time.Now()
|
||||
}
|
||||
|
||||
// SetClock replaces the presence clock (tests). Must be called before use; also resets the hub's
|
||||
// start time to the new clock's now.
|
||||
func (h *Hub) SetClock(now func() time.Time) {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
h.now = now
|
||||
h.born = h.clock()
|
||||
}
|
||||
|
||||
// SetLogger wires a Printf-style logger for the presence debug lines. Nil = silent.
|
||||
func (h *Hub) SetLogger(logf func(format string, args ...any)) {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
h.logf = logf
|
||||
}
|
||||
|
||||
func (h *Hub) rec(customerID string) *presenceRec {
|
||||
r := h.presence[customerID]
|
||||
if r == nil {
|
||||
r = &presenceRec{}
|
||||
h.presence[customerID] = r
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// MarkWaitStart records that a wait request from the customer's box began. Called ONCE per HTTP
|
||||
// request by the wait handler (not inside Wait, which runs once per heartbeat window).
|
||||
func (h *Hub) MarkWaitStart(customerID string) {
|
||||
if customerID == "" {
|
||||
return
|
||||
}
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
now := h.clock()
|
||||
r := h.rec(customerID)
|
||||
r.open++
|
||||
r.lastStart = now
|
||||
h.noteStateLocked(customerID, r, now)
|
||||
}
|
||||
|
||||
// MarkWaitEnd records that a wait request ended (any exit path).
|
||||
func (h *Hub) MarkWaitEnd(customerID string) {
|
||||
if customerID == "" {
|
||||
return
|
||||
}
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
r := h.rec(customerID)
|
||||
if r.open > 0 {
|
||||
r.open--
|
||||
}
|
||||
r.lastEnd = h.clock()
|
||||
}
|
||||
|
||||
// Presence answers the customer's box-connection verdict at now.
|
||||
func (h *Hub) Presence(customerID string, now time.Time) Presence {
|
||||
if customerID == "" {
|
||||
return Presence{State: PresenceUnknown}
|
||||
}
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
r := h.presence[customerID]
|
||||
p := h.presenceLocked(r, now)
|
||||
if r != nil {
|
||||
h.noteStateLocked(customerID, r, now)
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func (h *Hub) presenceLocked(r *presenceRec, now time.Time) Presence {
|
||||
if r != nil {
|
||||
if r.open > 0 || (!r.lastStart.IsZero() && now.Sub(r.lastStart) < PresenceConnectedWindow) {
|
||||
return Presence{State: PresenceConnected}
|
||||
}
|
||||
}
|
||||
// The hub itself has not been up long enough to have seen a healthy box's next wait.
|
||||
if now.Sub(h.born) < PresenceConnectedWindow {
|
||||
return Presence{State: PresenceUnknown}
|
||||
}
|
||||
if r == nil || r.lastStart.IsZero() {
|
||||
return Presence{State: PresenceNotConnected, Since: h.born, NeverSeen: true}
|
||||
}
|
||||
since := r.lastEnd
|
||||
if since.Before(r.lastStart) {
|
||||
since = r.lastStart
|
||||
}
|
||||
return Presence{State: PresenceNotConnected, Since: since}
|
||||
}
|
||||
|
||||
// noteStateLocked writes one DEBUG line when a customer's presence verdict changes.
|
||||
func (h *Hub) noteStateLocked(customerID string, r *presenceRec, now time.Time) {
|
||||
st := h.presenceLocked(r, now).State
|
||||
if st == r.lastState {
|
||||
return
|
||||
}
|
||||
prev := r.lastState
|
||||
r.lastState = st
|
||||
if h.logf != nil && prev != "" {
|
||||
h.logf("[DEBUG] box presence %s: %s -> %s (open holds %d)", customerID, prev, st, r.open)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
package intent
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// fakeClock is a settable clock for the presence seam.
|
||||
type fakeClock struct{ t time.Time }
|
||||
|
||||
func (c *fakeClock) now() time.Time { return c.t }
|
||||
|
||||
// A hub that has been up long enough to judge (born well before t0).
|
||||
func judgingHub(t0 time.Time) (*Hub, *fakeClock) {
|
||||
c := &fakeClock{t: t0.Add(-time.Hour)}
|
||||
h := New()
|
||||
h.SetClock(c.now)
|
||||
c.t = t0
|
||||
return h, c
|
||||
}
|
||||
|
||||
// R-30 §4 test 1: a wait starts at t0 and ends at t0+240 s, and none follows → at t0+300 s the box is
|
||||
// connected (a wait started < 333 s ago), at t0+334 s it is "not connected since t0+240 s".
|
||||
func TestPresence_WindowsAfterOneHold(t *testing.T) {
|
||||
t0 := time.Date(2026, 10, 8, 12, 0, 0, 0, time.UTC)
|
||||
h, c := judgingHub(t0)
|
||||
h.MarkWaitStart("c1")
|
||||
if p := h.Presence("c1", t0.Add(100*time.Second)); p.State != PresenceConnected {
|
||||
t.Fatalf("during the hold: %+v, want connected", p)
|
||||
}
|
||||
c.t = t0.Add(240 * time.Second)
|
||||
h.MarkWaitEnd("c1")
|
||||
if p := h.Presence("c1", t0.Add(300*time.Second)); p.State != PresenceConnected {
|
||||
t.Fatalf("t0+300 s: %+v, want connected (started < 333 s ago)", p)
|
||||
}
|
||||
p := h.Presence("c1", t0.Add(334*time.Second))
|
||||
if p.State != PresenceNotConnected || !p.Since.Equal(t0.Add(240*time.Second)) || p.NeverSeen {
|
||||
t.Fatalf("t0+334 s: %+v, want not_connected since t0+240 s", p)
|
||||
}
|
||||
if got := p.NotConnectedFor(t0.Add(600 * time.Second)); got != 360*time.Second {
|
||||
t.Fatalf("NotConnectedFor at t0+600 s = %s, want 6m0s", got)
|
||||
}
|
||||
}
|
||||
|
||||
// R-30 §4 test 2: a new start at t0+242 s keeps the box connected well past t0+334 s.
|
||||
func TestPresence_NextHoldKeepsConnected(t *testing.T) {
|
||||
t0 := time.Date(2026, 10, 8, 12, 0, 0, 0, time.UTC)
|
||||
h, c := judgingHub(t0)
|
||||
h.MarkWaitStart("c1")
|
||||
c.t = t0.Add(240 * time.Second)
|
||||
h.MarkWaitEnd("c1")
|
||||
c.t = t0.Add(242 * time.Second)
|
||||
h.MarkWaitStart("c1")
|
||||
for _, at := range []time.Duration{334 * time.Second, 500 * time.Second, 574 * time.Second} {
|
||||
if p := h.Presence("c1", t0.Add(at)); p.State != PresenceConnected {
|
||||
t.Fatalf("t0+%s: %+v, want connected (a hold is open)", at, p)
|
||||
}
|
||||
}
|
||||
// Two overlapping requests: one ending does not close the other.
|
||||
h.MarkWaitStart("c1")
|
||||
h.MarkWaitEnd("c1")
|
||||
if p := h.Presence("c1", t0.Add(time.Hour)); p.State != PresenceConnected {
|
||||
t.Fatalf("one of two holds still open: %+v, want connected", p)
|
||||
}
|
||||
}
|
||||
|
||||
// R-30 §4 test 3: a new Hub (a hub restart) answers unknown — never not_connected — until it has
|
||||
// been up for the connected window; after that, a customer never seen reads not_connected since the
|
||||
// hub's start, flagged NeverSeen.
|
||||
func TestPresence_HubRestartIsUnknown(t *testing.T) {
|
||||
t0 := time.Date(2026, 10, 8, 12, 0, 0, 0, time.UTC)
|
||||
c := &fakeClock{t: t0}
|
||||
h := New()
|
||||
h.SetClock(c.now)
|
||||
for _, at := range []time.Duration{0, 100 * time.Second, PresenceConnectedWindow - time.Second} {
|
||||
if p := h.Presence("c1", t0.Add(at)); p.State != PresenceUnknown {
|
||||
t.Fatalf("hub up %s: %+v, want unknown", at, p)
|
||||
}
|
||||
}
|
||||
p := h.Presence("c1", t0.Add(PresenceConnectedWindow))
|
||||
if p.State != PresenceNotConnected || !p.NeverSeen || !p.Since.Equal(t0) {
|
||||
t.Fatalf("hub up 333 s, box never seen: %+v, want not_connected since hub start", p)
|
||||
}
|
||||
if p := h.Presence("", t0.Add(time.Hour)); p.State != PresenceUnknown {
|
||||
t.Fatalf("no customer: %+v, want unknown", p)
|
||||
}
|
||||
}
|
||||
|
||||
// The window is the measured cadence plus the grace (pins the constants the delete guard leans on).
|
||||
func TestPresence_WindowConstants(t *testing.T) {
|
||||
if PresenceConnectedWindow != 333*time.Second {
|
||||
t.Fatalf("PresenceConnectedWindow = %s, want 5m33s (243 s + 90 s)", PresenceConnectedWindow)
|
||||
}
|
||||
}
|
||||
|
||||
// One DEBUG line per change, none on a repeat read.
|
||||
func TestPresence_DebugLineOnChange(t *testing.T) {
|
||||
t0 := time.Date(2026, 10, 8, 12, 0, 0, 0, time.UTC)
|
||||
h, c := judgingHub(t0)
|
||||
var lines []string
|
||||
h.SetLogger(func(f string, a ...any) { lines = append(lines, f) })
|
||||
h.MarkWaitStart("c1")
|
||||
c.t = t0.Add(240 * time.Second)
|
||||
h.MarkWaitEnd("c1")
|
||||
h.Presence("c1", t0.Add(400*time.Second))
|
||||
h.Presence("c1", t0.Add(500*time.Second))
|
||||
if len(lines) != 1 || !strings.Contains(lines[0], "[DEBUG] box presence") {
|
||||
t.Fatalf("debug lines = %q, want exactly one change line", lines)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user