Files
Pulse/internal/actionlifecycle/availability_projection.go
rcourtman 876ce14ab7 Keep action refusals current on websocket rows
A Docker page open while a host's agent disconnected kept Stop and Restart
enabled, and the click then failed at plan time. After the agent came back,
the old refusal stayed until the page remounted. Only a REST read carried
refusals, and a mounted Docker page reads REST once. The websocket merge
kept that REST answer, so nothing overwrote it; it just never changed.

Every broadcast now runs the same availability check, and a refused row
carries a ref into a new actionReadinessCatalog. A row with no ref refuses
nothing, so the store and the merge take the row's own answer and a refusal
ends with the frame that ends it. A first paint from the websocket is right
too, which closes the moment after in-app navigation when refused actions
looked enabled.

Broadcast rows keep capabilities whole. The drawer's operator policy form
needs what a disconnected host offers, and a stable set means a disconnect
moves one ref per row.

A check runs per capability per resource, host and token lookups scan every
session, and each session check takes config.Mu, so one pass now answers each
lookup once. Planning and dispatch still read live. Refusals leave Detail off
the wire: it names one host's identity and would give every host its own
catalog entry.
2026-11-04 21:22:02 +00:00

93 lines
3.3 KiB
Go

package actionlifecycle
import (
"context"
unified "github.com/rcourtman/pulse-go-rewrite/internal/unifiedresources"
)
// AvailabilityBatcher is implemented by a checker whose per-capability checks
// repeat expensive lookups across resources. The returned context scopes those
// lookups to one projection pass, so a pass sees one answer for each lookup.
// It must be used by one goroutine and dropped when the pass ends.
type AvailabilityBatcher interface {
BeginAvailabilityBatch(ctx context.Context) context.Context
}
// ProjectActionAvailability is the REST projection: refused capabilities move
// out of Capabilities and into ActionReadiness. It writes only top-level fields
// of the given elements, so callers pass a flat copy of shared resources.
func ProjectActionAvailability(ctx context.Context, checker AvailabilityChecker, resources []unified.Resource) {
if checker == nil {
return
}
ctx = beginAvailabilityBatch(ctx, checker)
for i := range resources {
resource := &resources[i]
if len(resource.Capabilities) == 0 {
continue
}
filtered := make([]unified.ResourceCapability, 0, len(resource.Capabilities))
var refusals []unified.ResourceActionReadiness
for _, capability := range resource.Capabilities {
refusal, refused := checkCapability(ctx, checker, resource, capability.Name)
if refused {
refusals = append(refusals, refusal)
continue
}
filtered = append(filtered, capability)
}
if len(filtered) == 0 {
resource.Capabilities = nil
} else {
resource.Capabilities = filtered
}
if len(refusals) > 0 {
resource.ActionReadiness = refusals
}
}
}
// AttachActionRefusals is the broadcast projection: ActionReadiness gains each
// refusal and Capabilities stays whole. A broadcast keeps a stable capability
// set for a row while its readiness moves with the agent's connection, and the
// resource drawer's operator policy needs the capabilities a host offers even
// while the host is disconnected. It writes only top-level fields of the given
// elements and does not allocate for a resource with nothing refused.
func AttachActionRefusals(ctx context.Context, checker AvailabilityChecker, resources []unified.Resource) {
if checker == nil {
return
}
ctx = beginAvailabilityBatch(ctx, checker)
for i := range resources {
resource := &resources[i]
var refusals []unified.ResourceActionReadiness
for _, capability := range resource.Capabilities {
if refusal, refused := checkCapability(ctx, checker, resource, capability.Name); refused {
refusals = append(refusals, refusal)
}
}
if len(refusals) > 0 {
resource.ActionReadiness = refusals
}
}
}
func beginAvailabilityBatch(ctx context.Context, checker AvailabilityChecker) context.Context {
if batcher, ok := checker.(AvailabilityBatcher); ok {
return batcher.BeginAvailabilityBatch(ctx)
}
return ctx
}
func checkCapability(ctx context.Context, checker AvailabilityChecker, resource *unified.Resource, capabilityName string) (unified.ResourceActionReadiness, bool) {
readiness := checker.CheckActionAvailable(ctx, unified.ActionRequest{
RequestID: "resource-capability-availability",
ResourceID: resource.ID,
CapabilityName: capabilityName,
Reason: "Projecting currently executable resource capabilities.",
RequestedBy: "system:resource-api",
}, *resource)
return readiness, readiness.Name != "" && !readiness.Available
}