oficina / equipo Public

Housekeeping: guard the team id and clock, registry at construction

Concurrent transactions read the cached team id and the loop's clock;
both are now behind a mutex, and the registry is set in NewStore rather
than lazily. Race-clean under go test -race for chat and the contracts.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev fa96734975706676f9155944b3e4a24d8f8acc88 parent ea04ea3
3 files changed, +16 −13
  • internal/equipo/chat/housekeeping.go +2 −0
  • internal/equipo/chat/housekeeping_control.go +11 −12
  • internal/equipo/chat/store.go +3 −1
diff --git a/internal/equipo/chat/housekeeping.go b/internal/equipo/chat/housekeeping.go
index c9a4f120..3398ef80 100644
--- a/internal/equipo/chat/housekeeping.go
+++ b/internal/equipo/chat/housekeeping.go
@@ -123,6 +123,8 @@ func (r hkRecord) open() bool { return r.State == wire.ActionClaimed || r.Releas
// team is the instance's team: one instance belongs to one team.
func (t *txn) team() (string, error) {
+ t.s.hkMu.Lock()
+ defer t.s.hkMu.Unlock()
if t.s.teamID != "" {
return t.s.teamID, nil
}
diff --git a/internal/equipo/chat/housekeeping_control.go b/internal/equipo/chat/housekeeping_control.go
index af3cea9b..20b239ab 100644
--- a/internal/equipo/chat/housekeeping_control.go
+++ b/internal/equipo/chat/housekeeping_control.go
@@ -58,21 +58,20 @@ func (s hkSettings) disabledFor(id string) bool { return s.disabled[id].Off }
// SetHousekeepingClock moves the loop's clock (quiet hours, the day's
// budget, the digest and the sweep); nil restores the store's own.
-func (s *Store) SetHousekeepingClock(now func() time.Time) { s.clock = now }
-
-// SetDetectors replaces the registry, for a test declaring its own.
-func (s *Store) SetDetectors(r *housekeeping.Registry) { s.detectors = r }
-
-func (s *Store) registry() *housekeeping.Registry {
- if s.detectors == nil {
- s.detectors = housekeeping.Default()
- }
- return s.detectors
+func (s *Store) SetHousekeepingClock(now func() time.Time) {
+ s.hkMu.Lock()
+ defer s.hkMu.Unlock()
+ s.clock = now
}
+func (s *Store) registry() *housekeeping.Registry { return s.detectors }
+
func (t *txn) hkNow() time.Time {
- if t.s.clock != nil {
- return t.s.clock().UTC()
+ t.s.hkMu.Lock()
+ clock := t.s.clock
+ t.s.hkMu.Unlock()
+ if clock != nil {
+ return clock().UTC()
}
return t.now()
}
diff --git a/internal/equipo/chat/store.go b/internal/equipo/chat/store.go
index ffa9d53f..eb1b73f0 100644
--- a/internal/equipo/chat/store.go
+++ b/internal/equipo/chat/store.go
@@ -8,6 +8,7 @@ import (
"encoding/json"
"errors"
"strings"
+ "sync"
"time"
"unicode/utf8"
@@ -70,6 +71,7 @@ type Store struct {
// declares others), the team's id once read, and the loop's clock
// (quiet hours, the day's budget, the digest), nil for now.
detectors *housekeeping.Registry
+ hkMu sync.Mutex
teamID string
clock func() time.Time
}
@@ -78,7 +80,7 @@ func NewStore(d *db.DB, a Authority, hub *Hub) *Store {
if hub == nil {
hub = NewHub()
}
- return &Store{db: d, authority: a, hub: hub, now: func() time.Time { return time.Now().UTC() }, process: newID("p")}
+ return &Store{db: d, authority: a, hub: hub, now: func() time.Time { return time.Now().UTC() }, process: newID("p"), detectors: housekeeping.Default()}
}
// Process identifies this instance process in runner leases.