Files
Pulse/internal/agentcapabilities/events.go
rcourtman 182067027f Push alert and status changes to the agent event stream
An agent holding /api/agent/events open heard about findings, approvals
and action completions, but not about the facts it most often has to
react to: an alert firing or clearing, or a resource going down. Pulse
already records those as resource changes that list_recent_changes
reads; this publishes them live as alert.fired, alert.resolved and
resource.status_changed, so agents and Pulse MCP --emit-notifications
clients can react without polling.

The monitor adapter tells an observer about each change after the store
accepts it, with the resource ID resolved to the canonical identity it
was stored under. Registry diffs are announced after the new generation
is swapped in. Alert rows are announced only from the alert manager's
live lifecycle callback through RecordLiveChange; lifecycle replay,
active-alert reconciliation and imported history keep using RecordChange,
so a restart never re-announces old alerts. Nothing new is persisted and
nothing is read back from the store, so there are no extra disk writes.

Payloads mirror the timeline record (changeId, kind, canonical
resourceId, current name and type, from/to or alert identifier, type,
level and resolution) and never carry alert messages, change reasons or
command metadata. Only state transitions whose status field changed
count as status events. Status changes are coalesced per resource over
30 seconds: the first is immediate, later ones fold into at most one
event per window, and a flap that returns to the announced status
publishes nothing. Change events carry the recording monitor's
organization and reach only subscribers in that organization.
2026-10-29 07:09:15 +00:00

84 lines
4.0 KiB
Go

package agentcapabilities
import "time"
// EventKind is the stable Pulse Intelligence event identifier used by API SSE
// streams and external-agent adapters.
type EventKind string
const (
// EventKindFindingCreated fires when Patrol records a new finding.
EventKindFindingCreated EventKind = "finding.created"
// EventKindApprovalPending fires when a governed action waits on operator
// approval.
EventKindApprovalPending EventKind = "approval.pending"
// EventKindActionCompleted fires when an action audit reaches a terminal
// state.
EventKindActionCompleted EventKind = "action.completed"
// EventKindAlertFired fires when Pulse records an alert firing (or
// re-firing) on a resource.
EventKindAlertFired EventKind = "alert.fired"
// EventKindAlertResolved fires when Pulse records an alert clearing.
EventKindAlertResolved EventKind = "alert.resolved"
// EventKindResourceStatusChanged fires when Pulse records a resource's
// status changing, coalesced per resource so flaps cannot flood the
// stream.
EventKindResourceStatusChanged EventKind = "resource.status_changed"
// EventKindStreamConnected is the one-shot SSE synchronization marker for a
// newly opened stream.
EventKindStreamConnected EventKind = "stream.connected"
// EventKindHeartbeat is the stream-local SSE keepalive.
EventKindHeartbeat EventKind = "heartbeat"
)
var agentActionableEventKinds = []string{
string(EventKindFindingCreated),
string(EventKindApprovalPending),
string(EventKindActionCompleted),
string(EventKindAlertFired),
string(EventKindAlertResolved),
string(EventKindResourceStatusChanged),
}
// ResourceStatusEventCoalesceWindow bounds resource.status_changed volume: a
// resource's first status change is announced at once, and further changes
// inside the window fold into at most one event at the window's end.
const ResourceStatusEventCoalesceWindow = 30 * time.Second
// AgentActionableEventKinds returns the non-transport event kinds external
// agents should expect to handle as product notifications.
func AgentActionableEventKinds() []string {
return append([]string(nil), agentActionableEventKinds...)
}
// IsTransportEventKind reports whether an event kind is stream plumbing rather
// than a product notification.
func IsTransportEventKind(kind string) bool {
switch EventKind(kind) {
case EventKindStreamConnected, EventKindHeartbeat:
return true
default:
return false
}
}
// SubscribeEventsDescription returns the manifest-owned description for the
// streaming capability. Event names are built from the shared constants so the
// API manifest, SSE producer, and external-agent adapters cannot drift apart.
func SubscribeEventsDescription() string {
return "Subscribe to the SSE event stream for real-time notifications: " +
string(EventKindFindingCreated) + " when a new finding is raised, " +
string(EventKindApprovalPending) + " when a remediation request enters StatusPending and waits on operator decision, " +
string(EventKindActionCompleted) + " when an action audit reaches a terminal state (Completed or Failed, including refused-before-dispatch failures with stable error-token prefixes; carries a verification block with the read-after-write probe outcome so agents close the certainty loop without polling /api/actions/{id}), " +
string(EventKindAlertFired) + " and " + string(EventKindAlertResolved) + " when an alert fires or clears on a resource (alert identifier, type and level), " +
string(EventKindResourceStatusChanged) + " when a resource's status changes (from/to; further changes within " + ResourceStatusEventCoalesceWindow.String() + " fold into one event carrying coalescedTransitions). Change events carry the canonical resourceId, resource name and type, and the changeId and kind that " + ListRecentChangesCapabilityName + " returns, so agents can read the surrounding history; replayed or imported history is not announced. Stream-local " +
string(EventKindHeartbeat) + " every 15s. Command fields are redacted for monitoring-read API tokens unless the token also has ai:execute. Long-lived connection; agents listen instead of polling."
}