plugins
The plugins package hosts ARES plugins. The first and currently only plugin is
resurrection (internal/plugins/resurrection), a supervisor that monitors
agents through a HealthChecker and automatically recreates them when they
fail. It depends only on small interfaces, so any agent type that satisfies
base.Agent can be supervised.
Responsibility
- Watch agents registered via
Watch, each paired with anAgentFactorythat builds a fresh instance on resurrection. - Run a periodic heartbeat loop: signal liveness for non-offline agents and
invoke
HealthChecker.CheckHealthto detect timeouts. - On failure, resurrect the agent with bounded retries and exponential backoff, deduplicating concurrent resurrection attempts per agent.
- Recover agent state by preferring a
SnapshotStore(full state) and falling back to event replay fromares_events.EventStore(semantic reconstruction of session, task, and status fields). - Periodically capture snapshots of stateful agents so resurrection can restore the latest committed state.
- Verify revived agents immediately; if still unhealthy, re-trigger failure detection instead of waiting for the next tick.
Architecture
flowchart LR
Watch[Watch agent + factory] --> Supervisor
subgraph Supervisor
HB[Heartbeat Loop]
Res[Resurrect Worker]
Snap[Snapshot Loop]
end
Health[HealthChecker] --> HB
HB -->|OnFailure| Res
Res --> Factory[AgentFactory]
Factory --> NewAgent[New Agent Instance]
Snap --> Store[(SnapshotStore)]
Store --> Res
EStore[(EventStore)] --> Res
NewAgent --> Restore[RestoreState]
Restore --> Verify[Verify Health]
Verify -->|unhealthy| HB
The Supervisor runs two background goroutines (heartbeat and optional
snapshot) under an errgroup. The HealthChecker reports failures through an
OnFailure callback; the supervisor dispatches a resurrection worker that
calls the factory, recovers state from the snapshot store or by replaying
events, starts the new instance, stops the old one, and verifies the result.
A HeartbeatAdapter bridges ahp.HeartbeatMonitor to the HealthChecker
interface so the plugin stays decoupled from the AHP package.
External interfaces
These interfaces are implemented by callers and injected into the supervisor. Signatures are extracted verbatim from the source.
// HealthChecker abstracts health detection. Implementations include
// heartbeat monitors, HTTP probes, or process watchers.
type HealthChecker interface {
RegisterAgent(agentID string)
UnregisterAgent(agentID string)
RecordAlive(agentID string)
CheckHealth() []string
OnFailure(fn func(agentID string))
}
// AgentFactory creates a fresh agent instance. Must return a new instance
// each time -- reusing old instances may carry stale state.
type AgentFactory func() base.Agent
The supervisor also consumes two external interfaces from agents/base:
base.Agent (with ID, Type, Status, Start, Stop), and optionally
base.StatefulAgent (with Snapshot, RestoreState), base.Heartbeater
(with IsAlive), and base.SnapshotStore (with Save, Load, Delete).
Key types and methods
| Type | Purpose | Key methods |
|---|---|---|
Supervisor | Monitors agents and resurrects them on failure | New, Watch, Unwatch, Start, Stop, Agent, Stats, SetSnapshotStore, WithSnapshotStore |
Config | Plugin configuration | (struct with CheckInterval, ResurrectTimeout, MaxAttempts, HeartbeatInterval, MaxBackoff, InitialBackoff, SnapshotInterval) |
Stats | Supervisor statistics | (struct with Watched, Alive, Resurrects, Statuses) |
HeartbeatAdapter | Adapts ahp.HeartbeatMonitor to HealthChecker | NewHeartbeatAdapter, RegisterAgent, UnregisterAgent, RecordAlive, CheckHealth, OnFailure |
MemorySnapshotStore | In-memory base.SnapshotStore | NewMemorySnapshotStore, Save, Load, Delete |
HealthChecker | Health detection contract | RegisterAgent, UnregisterAgent, RecordAlive, CheckHealth, OnFailure |
AgentFactory | Builds a fresh agent instance | (function type func() base.Agent) |
Module collaboration
- agents/base:
Supervisoroperates onbase.Agent; stateful agents implementbase.StatefulAgentfor snapshot/restore, and heartbeater agents implementbase.Heartbeaterfor the post-revivalIsAlivecheck. - ares_events: when no snapshot exists,
replayEventsreads the agent’s event stream, verifies integrity withVerifyStreamIntegrity, checks for truncation againstStreamVersion, and reconstructs session/task/status state fromEventSessionCreated,EventTaskCreated,EventAgentStarted, andEventAgentStopped. - ares_runtime:
ares_runtime.RecoverSnapshotOrEventsorchestrates the snapshot-first, events-fallback recovery strategy used during resurrection. - ares_protocol/ahp:
HeartbeatAdapterwrapsahp.HeartbeatMonitorand registers a failure callback so the supervisor stays decoupled from AHP. - ares_ctxutil:
Stopon the old agent usesares_ctxutil.WithDetachedTimeoutso the shutdown is not cancelled by the supervisor’s own context.
Extension points
- Implement
HealthChecker(or reuseHeartbeatAdapterwrapping anahp.HeartbeatMonitor) and pass it toNewalong with aConfig. - For each agent, call
Watch(agent, factory)wherefactoryreturns a brand-newbase.Agentinstance; the supervisor registers it with the health checker. - To preserve state across revivals, implement
base.StatefulAgent(Snapshot/RestoreState) on your agent and attach aSnapshotStoreviaSetSnapshotStoreorWithSnapshotStorebeforeStart; configureConfig.SnapshotIntervalto enable periodic snapshots. - Optionally pass an
ares_events.EventStoretoNewso resurrection can fall back to event replay when no snapshot is available. - Tune retry behavior through
Config:MaxAttempts,InitialBackoff, andMaxBackoffcontrol the exponential backoff;ResurrectTimeoutbounds a single attempt;HeartbeatIntervalandCheckIntervaldrive the background loop. - Start the supervisor with
Start(ctx)and stop it withStop(), which cancels the context and waits for all background goroutines to exit.
Bilingual status
This page is maintained in both English and Chinese with identical technical content. Code identifiers, type names, and configuration fields remain in English in both translations.
Maturity
Production. The package has extensive tests (resurrection_test.go,
resurrection_extra_test.go, snapshot_store_test.go) covering resurrection,
snapshots, event replay, backoff, and the heartbeat adapter, and it is
integrated with the runtime through the base.Agent and ares_runtime
recovery APIs. There are no experimental markers; deprecated paths are limited
to MetricsCollector deprecations in the sibling ares_arena package.