Files
Pulse/cmd/pulse-agent/runtime_health.go
rcourtman f4c2fd0c38 Fail closed on unknown remediation lock state for autonomous dispatches
The AI action broker treated an unreadable operator lock as unlocked:
isResourceRemediationLocked returned (false, nil) with no audit store
wired, and the caller logged store errors then dispatched anyway. An
operator's NeverAutoRemediate=true could be silently ignored whenever
the policy store was missing or erroring, which is unacceptable while
Patrol and Assistant run at assisted or full autonomy.

Posture change at the dispatch decision point:
- isResourceRemediationLocked now reports unknown state (nil store or
  lookup failure) as an ErrRemediationLockStateUnknown-wrapped error
  instead of silently defaulting to unlocked.
- New checkRemediationLockForDispatch gate: dispatches without an
  approved human decision fail CLOSED on unknown lock state and
  surface "remediation lock state unknown; operator approval
  required". Human-approved dispatches keep the historical fail-open
  behavior with a warning log. A confirmed lock still refuses even
  approved dispatches, as before.
- executeNativeActionWithAudit (TrueNAS app start/stop/restart) now
  enforces the lock too; it previously skipped the check entirely.
- Refusals persist Failed audit records with stable
  remediation_lock_state_unknown: / resource_remediation_locked:
  ErrorMessage prefixes.
- ai-runtime subsystem contract updated to pin the new posture.

Tests cover store-error and nil-store at both autonomy postures on
both dispatch paths; routing/control tests now wire an in-memory
audit store since autonomous dispatch without one is refused.
2026-07-10 00:14:01 +01:00

134 lines
3.1 KiB
Go

package main
import (
"sort"
"sync"
"sync/atomic"
"time"
agentshost "github.com/rcourtman/pulse-go-rewrite/pkg/agents/host"
)
const (
moduleStateDisabled = "disabled"
moduleStateStarting = "starting"
moduleStateRetrying = "retrying"
moduleStateRunning = "running"
)
type moduleHealth struct {
Name string `json:"name"`
Enabled bool `json:"enabled"`
State string `json:"state"`
LastError string `json:"lastError,omitempty"`
UpdatedAt time.Time `json:"updatedAt"`
}
type runtimeHealthSnapshot struct {
Ready bool `json:"ready"`
Modules []moduleHealth `json:"modules"`
}
type runtimeHealth struct {
mu sync.RWMutex
ready *atomic.Bool
modules map[string]moduleHealth
now func() time.Time
}
func newRuntimeHealth(ready *atomic.Bool, enabled map[string]bool) *runtimeHealth {
r := &runtimeHealth{
ready: ready,
modules: make(map[string]moduleHealth, len(enabled)),
now: func() time.Time { return time.Now().UTC() },
}
for name, isEnabled := range enabled {
state := moduleStateDisabled
if isEnabled {
state = moduleStateStarting
}
r.modules[name] = moduleHealth{
Name: name,
Enabled: isEnabled,
State: state,
UpdatedAt: r.now(),
}
agentModuleEnabled.WithLabelValues(name).Set(boolGauge(isEnabled))
}
r.reconcileReadyLocked()
return r
}
func (r *runtimeHealth) setState(name, state string, err error) {
if r == nil {
return
}
r.mu.Lock()
defer r.mu.Unlock()
module := r.modules[name]
module.Name = name
module.Enabled = state != moduleStateDisabled
module.State = state
module.LastError = ""
if err != nil {
module.LastError = err.Error()
}
module.UpdatedAt = r.now()
r.modules[name] = module
r.reconcileReadyLocked()
}
func (r *runtimeHealth) reconcileReadyLocked() {
ready := true
for _, module := range r.modules {
moduleReady := module.Enabled && module.State == moduleStateRunning
agentModuleReady.WithLabelValues(module.Name).Set(boolGauge(moduleReady))
if module.Enabled && module.State != moduleStateRunning {
ready = false
}
}
if r.ready != nil {
r.ready.Store(ready)
}
}
func boolGauge(value bool) float64 {
if value {
return 1
}
return 0
}
func (r *runtimeHealth) snapshot() runtimeHealthSnapshot {
if r == nil {
return runtimeHealthSnapshot{}
}
r.mu.RLock()
defer r.mu.RUnlock()
modules := make([]moduleHealth, 0, len(r.modules))
ready := true
for _, module := range r.modules {
modules = append(modules, module)
if module.Enabled && module.State != moduleStateRunning {
ready = false
}
}
sort.Slice(modules, func(i, j int) bool { return modules[i].Name < modules[j].Name })
return runtimeHealthSnapshot{Ready: ready, Modules: modules}
}
func (r *runtimeHealth) moduleStatuses() []agentshost.ModuleStatus {
snapshot := r.snapshot()
statuses := make([]agentshost.ModuleStatus, 0, len(snapshot.Modules))
for _, module := range snapshot.Modules {
statuses = append(statuses, agentshost.ModuleStatus{
Name: module.Name,
Enabled: module.Enabled,
State: module.State,
LastError: module.LastError,
UpdatedAt: module.UpdatedAt,
})
}
return statuses
}