Skip to content

Event System

Type: Implementation guide. Normative spec: PROTOCOL_SPEC §9.16 Event Type Naming Convention and Canonical Definitions.

Overview

The event system is a bus for framework lifecycle events — module registration, health thresholds, runtime control, governance decisions, delivery failures. Subscribers receive events asynchronously: emit() never blocks module execution and never raises into the emitting code. Each subscriber gets its own retry policy, and an event that cannot be delivered produces a dead-letter event instead of disappearing.

With sys_modules.enabled and sys_modules.events.enabled, APCore creates the emitter, wires the framework's emitters to it, and exposes it as client.events (APCore Unified Client).

Requirements

  • Provide an EventEmitter with thread-safe subscriber management and non-blocking dispatch.
  • Define ApCoreEvent as the immutable event envelope.
  • Define the EventSubscriber protocol: an async on_event() plus optional event_pattern, subscriber_id, subscriber_type, retry and on_failure members.
  • Errors in one subscriber MUST NOT propagate to other subscribers or to the emitter's caller.
  • Retry failed deliveries per subscriber and emit apcore.event.delivery_failed when retries are exhausted.
  • Provide built-in webhook, file, stdout, and filter subscribers, and a host-owned registry of factories for configured subscribers.

Technical Design

ApCoreEvent

from dataclasses import dataclass
from typing import Any


@dataclass(frozen=True)
class ApCoreEvent:
    event_type: str               # e.g. "apcore.module.toggled"
    module_id: str | None         # Associated module (None for global events)
    timestamp: str                # ISO 8601 UTC
    severity: str                 # "info" | "warn" | "error" | "fatal"
    data: dict[str, Any]          # Event-specific payload
export interface ApCoreEvent {
    readonly eventType: string;
    readonly moduleId: string | null;       // null for global events
    readonly timestamp: string;             // ISO 8601 UTC
    readonly severity: string;              // "info" | "warn" | "error" | "fatal"
    readonly data: Record<string, unknown>;
}

// Build one with createEvent(eventType, moduleId, severity, data) — it stamps the timestamp.
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApCoreEvent {
    pub event_type: String,
    pub timestamp: String,                  // ISO 8601 UTC
    pub data: serde_json::Value,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub module_id: Option<String>,
    pub severity: String,                   // "info" | "warn" | "error" | "fatal"
}

// Construct with ApCoreEvent::new(event_type, data) (severity "info", no module)
// or ApCoreEvent::with_module(event_type, data, module_id, severity).

Events are immutable once emitted (Python frozen=True, TypeScript readonly, Rust subscribers receive &ApCoreEvent). On the wire (webhook, A2A, file) an event is a JSON object with the snake_case keys event_type, module_id, timestamp, severity, data.

EventSubscriber

from typing import Protocol, runtime_checkable

from apcore.events import ApCoreEvent


@runtime_checkable
class EventSubscriber(Protocol):
    async def on_event(self, event: ApCoreEvent) -> None: ...

# Optional attributes read by the emitter:
#   event_pattern: str              (default "*")
#   subscriber_id: str              (generated as "<type>-<n>" when absent)
#   subscriber_type: str            (default derived from the class name)
#   retry: EventRetryConfig | dict  (default policy below)
#   async on_failure(event, error, attempt_count)
import type { ApCoreEvent, EventRetryConfig } from "apcore-js/full";

export interface EventSubscriber {
    onEvent(event: ApCoreEvent): void | Promise<void>;
    readonly subscriberId?: string;
    readonly subscriberType?: string;
    readonly eventPattern?: string;          // default "*"
    readonly retry?: EventRetryConfig;
    onFailure?(event: ApCoreEvent, error: Error, attemptCount: number): void | Promise<void>;
}
use apcore::errors::ModuleError;
use apcore::events::{ApCoreEvent, EventRetryConfig};
use async_trait::async_trait;

#[async_trait]
pub trait EventSubscriber: Send + Sync + std::fmt::Debug {
    fn subscriber_id(&self) -> &str { "default" }
    fn event_pattern(&self) -> &str { "*" }
    fn subscriber_type(&self) -> &str { "subscriber" }
    fn retry(&self) -> EventRetryConfig { EventRetryConfig::default() }
    async fn on_failure(&self, _event: &ApCoreEvent, _error: &ModuleError, _attempt_count: u32) {}
    async fn on_event(&self, event: &ApCoreEvent) -> Result<(), ModuleError>;
}

Python's EventEmitter.subscribe() raises TypeError when on_event is not a coroutine function. In Rust, give every subscriber a distinct subscriber_id(): the default "default" makes subscribers indistinguishable to unsubscribe_by_id.

EventEmitter

from apcore.events import ApCoreEvent, EventSubscriber


class EventEmitter:
    def __init__(self, max_workers: int = 4) -> None: ...
    def subscribe(self, subscriber: EventSubscriber) -> None: ...
    def unsubscribe(self, subscriber: EventSubscriber) -> None: ...
    def emit(self, event: ApCoreEvent) -> None: ...
    def flush(self, timeout: float = 5.0) -> None: ...     # seconds
    def close(self, timeout: float = 5.0) -> None: ...     # seconds
    def shutdown(self, timeout: float = 5.0) -> None: ...  # seconds
import type { ApCoreEvent, EventSubscriber } from "apcore-js/full";

export declare class EventEmitter {
    constructor(maxPending?: number);                // default 1000
    subscribe(subscriber: EventSubscriber): void;
    unsubscribe(subscriber: EventSubscriber): void;
    emit(event: ApCoreEvent): void;
    tryEmit(event: ApCoreEvent): boolean;
    flush(timeoutMs?: number): Promise<void>;        // default 5000; 0 waits indefinitely
    shutdown(timeoutMs?: number): Promise<void>;
}
Method Signature
new EventEmitter::new() -> EventEmitter
subscribe fn subscribe(&self, Box<dyn EventSubscriber>) -> SubscriberHandle
unsubscribe_handle fn unsubscribe_handle(&self, SubscriberHandle) -> bool
unsubscribe / unsubscribe_by_id fn unsubscribe(&self, &dyn EventSubscriber) -> bool / fn unsubscribe_by_id(&self, &str) -> bool — match by subscriber_id()
emit async fn emit(&self, &ApCoreEvent) — spawns the deliveries and returns
emit_spawn fn emit_spawn(&self, ApCoreEvent) — the same, from synchronous code
flush async fn flush(&self, timeout_ms: u64) -> Result<(), ModuleError> (0 waits indefinitely); flush_default() uses 5000 ms
shutdown async fn shutdown(&self, timeout_ms: u64) -> Result<(), ModuleError>

Dispatch model:

  • emit() snapshots the subscribers and returns. Delivery runs on a private event loop on a daemon thread in Python, as pending promises in TypeScript, or as spawned tasks in Rust. Subscribing or unsubscribing during delivery is safe.
  • Each matching subscriber is delivered to independently, with its own retry loop. Python runs one event batch at a time on its private loop and accepts the max_workers constructor argument for compatibility.
  • flush() waits for pending deliveries up to its timeout and returns without error when the timeout elapses. The unit is per-SDK and named by the parameter: Python takes seconds, TypeScript and Rust milliseconds. Pass a positive value — 0 returns immediately in Python and waits indefinitely in TypeScript and Rust.
  • After shutdown(), emit() drops events silently and flush() returns immediately.

Python delivery lifecycle. close() and shutdown() stop admission and drain previously submitted batches within one timeout. Batches still queued at expiration are skipped. A batch already running may finish on the daemon thread, which then releases the skipped batches and closes the loop. Python can exit while that thread is still running. Subscriber-owned resources follow their own lifecycle.

close() keeps subscriptions. It drops new emissions during draining, and the first emission after the worker exits starts a fresh worker. shutdown() makes the emitter terminal, including when invoked during a concurrent close(). A callback can request either operation on its own emitter and returns immediately, with draining handled by the delivery worker.

Subscribing

client.on(event_type, handler) delivers an event only when its type equals event_type. There is no glob matching: client.on("apcore.registry.*", …) subscribes to the literal type apcore.registry.* and receives nothing. To receive several types, subscribe once per type:

from apcore import APCore
from apcore.config import Config

client = APCore(config=Config.load("apcore.yaml"))  # sys_modules.events.enabled: true

def on_registry_event(event) -> None:
    print(f"registry: {event.event_type} {event.module_id}")

subs = [
    client.on(event_type, on_registry_event)
    for event_type in ("apcore.registry.module_registered", "apcore.registry.module_unregistered")
]
import { APCore, Config, type ApCoreEvent } from "apcore-js/full";

const client = new APCore({ config: Config.load("apcore.yaml") }); // sys_modules.events.enabled: true

const onRegistryEvent = (event: ApCoreEvent): void => {
    console.log(`registry: ${event.eventType} ${event.moduleId}`);
};

const subs = ["apcore.registry.module_registered", "apcore.registry.module_unregistered"].map(
    (eventType) => client.on(eventType, onRegistryEvent),
);
use apcore::errors::ModuleError;
use apcore::APCore;

fn main() -> Result<(), ModuleError> {
    let mut client = APCore::from_path("apcore.yaml")?; // sys_modules.events.enabled: true

    let mut ids = Vec::new();
    for event_type in ["apcore.registry.module_registered", "apcore.registry.module_unregistered"] {
        ids.push(client.on(event_type, |event| {
            println!("registry: {} {:?}", event.event_type, event.module_id);
        })?);
    }
    Ok(())
}

Pattern subscriptions use a subscriber's event_pattern, subscribed directly on the emitter. Patterns are matched with algorithm A25 (§9.16.3) against the event type, case-sensitively: * matches any run of characters, ? exactly one, and every other character is literal. A pattern is never rejected — one that matches nothing delivers nothing.

from apcore import APCore
from apcore.config import Config
from apcore.events import ApCoreEvent

client = APCore(config=Config.load("apcore.yaml"))

class RegistryWatcher:
    event_pattern = "apcore.registry.*"
    subscriber_id = "registry-watcher"

    async def on_event(self, event: ApCoreEvent) -> None:
        print(f"registry: {event.event_type} {event.module_id}")

if client.events is not None:
    client.events.subscribe(RegistryWatcher())
import { APCore, Config, type EventSubscriber } from "apcore-js/full";

const client = new APCore({ config: Config.load("apcore.yaml") });

const registryWatcher: EventSubscriber = {
    subscriberId: "registry-watcher",
    eventPattern: "apcore.registry.*",
    onEvent: (event) => {
        console.log(`registry: ${event.eventType} ${event.moduleId}`);
    },
};

client.events?.subscribe(registryWatcher);
use apcore::errors::ModuleError;
use apcore::events::{ApCoreEvent, EventSubscriber};
use apcore::APCore;
use async_trait::async_trait;

#[derive(Debug)]
struct RegistryWatcher;

#[async_trait]
impl EventSubscriber for RegistryWatcher {
    fn subscriber_id(&self) -> &str { "registry-watcher" }
    fn event_pattern(&self) -> &str { "apcore.registry.*" }

    async fn on_event(&self, event: &ApCoreEvent) -> Result<(), ModuleError> {
        println!("registry: {} {:?}", event.event_type, event.module_id);
        Ok(())
    }
}

fn main() -> Result<(), ModuleError> {
    let client = APCore::from_path("apcore.yaml")?;
    if let Some(events) = client.events() {
        let _handle = events.subscribe(Box::new(RegistryWatcher));
    }
    Ok(())
}

A subscriber whose pattern is "*" receives every event except apcore.event.delivery_failed, which is delivered only to subscribers with a non-wildcard pattern that matches it — so a failing catch-all subscriber cannot recurse on reports about itself.

Built-in Subscribers

Type Delivers to Config fields (besides id, retry)
webhook HTTP POST of the event JSON url, headers, timeout_ms (default 5000); retry_count (retries after the first attempt, i.e. max_attempts = retry_count + 1; ignored when retry is present)
file Appends to a local file path, append (default true), format (json default / text), rotate_bytes
stdout Standard output format (text default / json), level_filter
filter Another subscriber, for matching events only delegate_type, delegate_config, include_events, exclude_events
  • Webhook delivery. Sends Content-Type: application/json. A successful 2xx response completes delivery. The emitter applies retry policy to 5xx, connection failures, and timeouts. Install Python's events extra or enable Rust's events feature to use the built-in HTTP transport.
  • filter. When include_events is present it is decisive: an event is forwarded if it matches any entry and discarded otherwise, and exclude_events is not consulted. Otherwise an event matching any exclude_events entry is discarded. Both lists use A25. Because a pattern that fails to match means the event is delivered, exclude_events fails open — keep its patterns simple and test them.
  • id. Every subscriber has a stable subscriber_id; when the config omits id, the SDK generates "<type>-<n>". It appears in dead-letter and circuit events.

Subscriber Factories

The host's factory registry builds subscribers keyed by type, including filter delegates. The factory receives the configuration for one subscriber. Use the A2A adapter to add delivery to an A2A receiver.

from apcore.events import SubscriberRegistry, StdoutSubscriber
from apcore.full import APCore, Config

subscribers = SubscriberRegistry()
subscribers.register("console", lambda config: StdoutSubscriber(output_format=config.get("format", "text")))
client = APCore(config=Config.load("apcore.yaml"), subscriber_registry=subscribers)
import { Config } from "apcore-js/config";
import { StdoutSubscriber } from "apcore-js/events";
import { APCore } from "apcore-js/full";
import { createSysModulesPlugin } from "apcore-js/sys-modules";

const client = new APCore({
  config: Config.load("apcore.yaml"),
  plugins: [createSysModulesPlugin({
    subscriberFactories: { console: () => new StdoutSubscriber() },
  })],
});
use std::path::Path;

use apcore::{APCore, APCoreOptions, Config, StdoutSubscriber, SubscriberRegistry};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut subscribers = SubscriberRegistry::new();
    subscribers.register("console", |_| Ok(Box::new(StdoutSubscriber::default())));
    let options = APCoreOptions {
        config: Some(Config::load(Path::new("apcore.yaml"))?),
        ..Default::default()
    };
    let _client = APCore::try_start_with_subscribers(options, &subscribers)?;
    Ok(())
}

Configuration

sys_modules:
  enabled: true
  events:
    enabled: true
    thresholds:
      error_rate: 0.1              # apcore.health.error_threshold_exceeded at >= 10%
      latency_p99_ms: 5000.0       # apcore.health.latency_threshold_exceeded at p99 >= 5 s
    subscribers:
      - type: "webhook"
        id: "platform-hook"
        url: "https://platform.example.com/events"
        headers:
          Authorization: "Bearer ${PLATFORM_TOKEN}"
        timeout_ms: 5000
        retry:
          max_attempts: 5
          initial_backoff_ms: 250

      - type: "file"
        path: "/var/log/apcore/events.jsonl"
        format: "json"
        rotate_bytes: 10485760     # 10 MB

      - type: "filter"
        id: "health-pager"
        delegate_type: "webhook"
        delegate_config:
          url: "https://events.example.com/health"
        include_events:
          - "apcore.health.*"

      - type: "console"           # supplied by this host's factory registry
        format: "text"

Event Naming Convention

Event types use dot-namespaced names; the prefix identifies the owner (§9.16.1). The apcore.* prefix is reserved for the core framework — ecosystem packages use their own (apcore-mcp.*, apcore-a2a.*, …) and applications any other prefix (billing.invoice_generated).

Framework events have the form apcore.<subsystem>.<event>:

  • <subsystem> is the emitting subsystem, such as registry, client, module, config, health, approval, policy, acl, stream, circuit, subscriber, or event.
  • <event> is a snake_case description of the state transition (module_registered, error_threshold_exceeded, updated).

Module lifecycle events

Lifecycle events include operation_id to correlate with host operation handles and governed reload reports. The event envelope carries the affected module_id for Registry events. Subscribe through EventEmitter.

Event type Transition
apcore.registry.module_drain_started The generation's admission closes.
apcore.registry.module_drain_timed_out The drain deadline elapses.
apcore.registry.module_drain_recovered Recovery reaches a ready or unavailable outcome.
apcore.registry.module_reload_failed Replacement fails and compensation reaches its outcome.
apcore.client.shutdown_resources_completed A participating client observes Registry teardown and is about to close its own emitter.

Managed reload failure details are limited to the failed phase, error code, and compensation outcome. Use the operation ID to associate the event with the reload report.

Clients sharing a Registry emit their resources-completed events once each, using the same operation ID. This event describes Registry teardown. Each client's shutdown report carries the final outcome of its own delivery stage. Canonical severity and payload fields

Deprecation: legacy event names

Implementations emit only the canonical names. These earlier names are not emitted; subscribe to the canonical name instead:

Legacy name Canonical name
module_registered apcore.registry.module_registered
module_unregistered apcore.registry.module_unregistered
apcore.error.threshold_exceeded apcore.health.error_threshold_exceeded
apcore.latency.threshold_exceeded apcore.health.latency_threshold_exceeded
module_health_changed apcore.module.toggled / apcore.health.recovered
config_changed apcore.config.updated / apcore.module.reloaded

Event Types

Events the framework emits. The envelope's module_id carries the associated module; the table lists the keys of data.

Event Type Severity Emitted by data keys
apcore.registry.module_registered info Registry bridge (event-enabled system modules) — (module on the envelope)
apcore.registry.module_unregistered info Registry bridge — (module on the envelope)
apcore.registry.module_load_failed error Registry module_id, callback_name, error_type, error_message
apcore.module.toggled info system.control.toggle_feature module_id, enabled, caller_id, identity (when present)
apcore.module.reloaded info system.control.reload_module module_id, previous_version, new_version, caller_id, identity (when present)
apcore.config.updated info system.control.update_config key, old_value, new_value (sensitive keys redacted), caller_id, identity (when present)
apcore.health.error_threshold_exceeded error PlatformNotifyMiddleware error_rate, threshold
apcore.health.latency_threshold_exceeded warn PlatformNotifyMiddleware p99_latency_ms, threshold
apcore.health.recovered info PlatformNotifyMiddleware (error rate back below half the threshold) status, error_rate
apcore.approval.decision info (approved/pending) / warn (rejected/timeout) Approval gate (§7) module_id, status, approved_by, reason, approval_id (non-secret audit_id or null), trace_id
apcore.policy.override info Approval gate, when an ExecutionPolicy rule applies (§7.9) module_id, pattern, requires_approval, destructive, needs_approval, reason, trace_id
apcore.acl.denied warn ACL check (§6) module_id, caller_id, reason, trace_id
apcore.acl.audit the ACL file's audit.log_level (default info) ACL check — the default audit sink (§6.3.2) the AuditEntry fields (§6.3.1)
apcore.stream.post_validation_failed error Executor (after a stream's last chunk) error_type, message, trace_id
apcore.circuit.opened warn CircuitBreakerMiddleware module_id, caller_id, error_rate
apcore.circuit.closed info CircuitBreakerMiddleware module_id, caller_id, error_rate
apcore.subscriber.circuit_opened warn CircuitBreakerWrapper subscriber_type, consecutive_failures
apcore.subscriber.circuit_closed info CircuitBreakerWrapper subscriber_type, recovery_attempt
apcore.event.delivery_failed error Event bus (dead-letter path) see Dead-Letter Event
  • Governance events (apcore.approval.decision, apcore.policy.override, apcore.acl.denied) make the ACL → policy → approval chain observable. They are emitted only when an emitter is attached to the Executor, never influence the call's outcome, are not emitted for a gate that did not engage, and apcore.acl.denied is not emitted during a validate() preflight. See §7.9.
  • The apcore.subscriber.* payloads above are what all three SDKs emit; §9.16.2 also lists subscriber_id, which no SDK currently includes.
  • apcore-rust additionally emits apcore.config.reloaded when system.control.reload_module re-reads the configuration from disk.

Delivery Semantics

Per-Subscriber Retry Policy

Every subscriber — built-in or custom — has a retry policy; the field names, defaults and backoff formula are the same in all SDKs:

Field Type Default Meaning
max_attempts int ≥ 1 3 Total attempts including the first. 1 disables retry.
initial_backoff_ms int ≥ 0 100 Delay before the first retry.
max_backoff_ms int ≥ initial_backoff_ms 30000 Upper bound on a single delay.
backoff_multiplier float ≥ 1.0 2.0 Growth factor per retry.
delay_ms(attempt) = min(max_backoff_ms, initial_backoff_ms * backoff_multiplier ** attempt)
    attempt is zero-based: attempt = 0 is the first retry after the initial try

Defaults:  try → (100 ms) → retry 1 → (200 ms) → retry 2 → dead-letter event

Set it in configuration with a retry: block on the subscriber entry, or in code on the subscriber (retry = EventRetryConfig(...) or a plain mapping in Python, retry: { maxAttempts, … } in TypeScript, fn retry(&self) -> EventRetryConfig in Rust). The webhook's retry_count shorthand is described under Built-in Subscribers.

Dead-Letter Event (apcore.event.delivery_failed)

When a subscriber's attempts are exhausted, the emitter emits apcore.event.delivery_failed (severity error, no module_id) with this data:

{
  "subscriber_type": "a2a",
  "subscriber_id": "agent-bridge",
  "original_event": {
    "name": "apcore.health.error_threshold_exceeded",
    "payload": { "error_rate": 0.42, "threshold": 0.1 },
    "metadata": {
      "module_id": "billing.charge",
      "timestamp": "2026-05-19T10:14:22.301Z"
    }
  },
  "error": {
    "type": "RuntimeError",
    "message": "A2A delivery to https://agent.example.com failed with status 503"
  },
  "attempt_count": 3,
  "timestamp": "2026-05-19T10:14:28.812Z"
}
  • subscriber_type is the subscriber's declared type (webhook, a2a, …); the circuit events report the same value (D-116).
  • The dead-letter event is delivered once, without retry, and only to subscribers whose non-wildcard event_pattern matches it. A dead-letter subscriber that fails is logged and discarded — there is no second-order dead letter.
  • It is emitted through the same emitter as the original event, so any subscriber can opt in by name — for example a filter around a file subscriber with include_events: ["apcore.event.delivery_failed"].
  • TypeScript bounds normal deliveries with maxPending (default 1000) and reserves one slot to route an overflowing event's deliveries through the dead-letter path with "reason": "pending_overflow". If an event would overflow and the reserve is busy, tryEmit() returns false before accepting any of its deliveries. emit() logs a warning in that case.

on_failure Callback

A subscriber may also define on_failure(event, error, attempt_count). It is invoked once per exhausted delivery, after the dead-letter event, with the same information. An exception it raises is logged. It is independent of any dead-letter subscriber — both fire.

from apcore.events import ApCoreEvent, EventEmitter


class HealthAlertSubscriber:
    event_pattern = "apcore.health.*"
    retry = {"max_attempts": 5, "initial_backoff_ms": 250}

    async def on_event(self, event: ApCoreEvent) -> None:
        print(f"forwarding {event.event_type}")

    async def on_failure(self, event: ApCoreEvent, error: Exception, attempt_count: int) -> None:
        print(f"gave up on {event.event_type} after {attempt_count} attempts: {error}")


emitter = EventEmitter()
emitter.subscribe(HealthAlertSubscriber())
import { EventEmitter, type ApCoreEvent, type EventSubscriber } from "apcore-js/full";

class HealthAlertSubscriber implements EventSubscriber {
    readonly eventPattern = "apcore.health.*";
    readonly retry = { maxAttempts: 5, initialBackoffMs: 250 };

    async onEvent(event: ApCoreEvent): Promise<void> {
        console.log(`forwarding ${event.eventType}`);
    }

    async onFailure(event: ApCoreEvent, error: Error, attemptCount: number): Promise<void> {
        console.log(`gave up on ${event.eventType} after ${attemptCount} attempts: ${error.message}`);
    }
}

const emitter = new EventEmitter();
emitter.subscribe(new HealthAlertSubscriber());
use apcore::errors::ModuleError;
use apcore::events::{ApCoreEvent, EventEmitter, EventRetryConfig, EventSubscriber};
use async_trait::async_trait;

#[derive(Debug)]
struct HealthAlertSubscriber;

#[async_trait]
impl EventSubscriber for HealthAlertSubscriber {
    fn subscriber_id(&self) -> &str { "health-alert" }
    fn event_pattern(&self) -> &str { "apcore.health.*" }

    fn retry(&self) -> EventRetryConfig {
        EventRetryConfig { max_attempts: 5, initial_backoff_ms: 250, ..Default::default() }
    }

    async fn on_event(&self, event: &ApCoreEvent) -> Result<(), ModuleError> {
        println!("forwarding {}", event.event_type);
        Ok(())
    }

    async fn on_failure(&self, event: &ApCoreEvent, error: &ModuleError, attempt_count: u32) {
        println!("gave up on {} after {attempt_count} attempts: {error}", event.event_type);
    }
}

fn main() {
    let emitter = EventEmitter::new();
    let _handle = emitter.subscribe(Box::new(HealthAlertSubscriber));
}

Subscriber Circuit Breaker

CircuitBreakerWrapper wraps a subscriber so that a degraded destination cannot keep consuming delivery work:

CLOSED    → (consecutive_failures >= open_threshold) → OPEN
OPEN      → (recovery_window_ms elapsed since the last failure) → HALF_OPEN
HALF_OPEN → (delivery succeeds) → CLOSED
HALF_OPEN → (delivery fails)    → OPEN
  • Defaults: timeout_ms 5000 (a delivery running longer counts as a failure), open_threshold 5, recovery_window_ms 60 000.
  • While OPEN, the wrapped subscriber is not called and its events are discarded.
  • Transitions emit apcore.subscriber.circuit_opened (with a WARN log) and apcore.subscriber.circuit_closed.

Python wraps every subscriber created from sys_modules.events.subscribers in a CircuitBreakerWrapper, tuned by an optional per-entry circuit_breaker: block (timeout_ms, open_threshold, recovery_window_ms). TypeScript and Rust export CircuitBreakerWrapper for explicit use and do not wrap configured subscribers automatically.

Dependencies

  • Middleware — base of PlatformNotifyMiddleware, which emits the apcore.health.* events.
  • MetricsCollector — read by PlatformNotifyMiddleware for its threshold checks.
Python SDK reference

Not a protocol requirement — the Python SDK's source layout for users of apcore-python.

File Purpose
src/apcore/events/emitter.py EventEmitter, ApCoreEvent, EventSubscriber, retry and dead-letter delivery
src/apcore/events/subscribers.py Webhook, file, stdout, and filter delivery
src/apcore/events/circuit_breaker.py CircuitBreakerWrapper
src/apcore/events/retry.py EventRetryConfig
src/apcore/sys_modules/registration.py Subscriber factory registry, config-driven subscribers
src/apcore/middleware/platform_notify.py PlatformNotifyMiddleware
src/apcore/client.py APCore.on(), APCore.off()

aiohttp (optional, pip install apcore[events]) is required for the HTTP subscribers.

Testing Strategy

  • EventEmitter: subscribe/emit/unsubscribe, snapshot isolation, subscriber error isolation, flush and shutdown.
  • Matching: exact matching for client.on(), A25 patterns for event_pattern, dead-letter delivery only to non-wildcard subscribers.
  • Retry and dead letter: attempt counts and delays, dead-letter payload shape, on_failure invocation.
  • HTTP subscribers: 2xx/4xx/5xx handling, timeouts, header merging, A2A auth modes and payload.
  • Factories: custom type registration, config instantiation, reset.
  • PlatformNotifyMiddleware: threshold crossing, recovery at half the threshold.

Contract: EventEmitter.emit

Inputs

  • event (ApCoreEvent, required) — the event to deliver; Rust takes &ApCoreEvent (emit_spawn takes it by value). The caller SHOULD supply a non-empty event_type; emit() does not validate it.

Returns

  • Nothing.

Overflow

  • An event accepted by emit() MUST eventually be delivered to each matching subscriber or routed through the dead-letter path; it MUST NOT be silently discarded.
  • An implementation MAY bound its pending-delivery buffer. On overflow it MUST either apply backpressure or fail the delivery through the dead-letter path with reason: "pending_overflow". TypeScript reserves capacity for one overflowing event and exposes the admission result through tryEmit().
  • Events for a subscriber whose circuit is OPEN are discarded by design; that is not overflow.

Errors

  • None raised to the caller. Subscriber errors are retried, then reported through the dead-letter path and on_failure.

Properties

  • async: synchronous in Python and TypeScript; async fn in Rust, whose body spawns the deliveries and does not wait on subscribers. In all three, emit() returns without waiting for subscribers and never raises.
  • thread_safe: true
  • pure: false
  • idempotent: false

Contract: EventEmitter.subscribe

Inputs

  • subscriber (EventSubscriber, required) — an object carrying its own on_event and optional members (event_pattern, subscriber_id, retry, …). There is no subscribe(event_type, handler) form on the emitter; APCore.on() provides the callback convenience (D10-016).

Errors

  • None in the cross-language contract. Python raises TypeError when on_event is not a coroutine function — the runtime equivalent of the static check TypeScript and Rust perform (D10-002).

Returns

  • Python/TypeScript: nothing — remove the subscription by passing the same object to unsubscribe. Rust: a SubscriberHandle for unsubscribe_handle.

Properties

  • async: false
  • thread_safe: true
  • idempotent: false (each call adds a subscription)

Contract: EventEmitter.unsubscribe

Inputs

  • subscriber — the object passed to subscribe (Python and TypeScript compare by identity). Python removes the first registration of that object and future emissions use the resulting subscriber list. Rust's subscribe consumes the Box, so Rust removes by SubscriberHandle (unsubscribe_handle) or by subscriber_id (unsubscribe_by_id; unsubscribe(&dyn EventSubscriber) also matches by ID and warns when several subscribers share it).

Errors

  • None. Removing an absent subscriber is a no-op.

Returns

  • Python/TypeScript: nothing. Rust: bool — whether a subscription was removed.

Properties

  • async: false
  • thread_safe: true
  • pure: false
  • idempotent: true

Contract: EventEmitter.flush

Inputs

  • timeout — maximum wait for in-flight deliveries: Python timeout in seconds (default 5.0), TypeScript timeoutMs and Rust timeout_ms in milliseconds (default 5000; Rust flush_default()). Pass a positive value; 0 is not portable.

Errors

  • None raised to the caller. If the timeout elapses, flush returns and the remaining deliveries continue in the background. Python applies the timeout to the whole pending snapshot. Calling flush() from a Python subscriber on the same emitter returns immediately.

Returns

  • Nothing (Rust Result<(), ModuleError>, always Ok).

Properties

  • async: blocking in Python; async in TypeScript and Rust
  • thread_safe: true
  • pure: false
  • idempotent: true

Contract: WebhookSubscriber.on_event

Inputs

  • event (ApCoreEvent, required) — POSTed as JSON to the configured URL with the configured headers.

Errors

  • Raises (Rust: returns Err) on 5xx responses, connection errors and timeouts, so the emitter retries per the subscriber's retry policy and emits apcore.event.delivery_failed when attempts are exhausted. Nothing reaches the code that called emit().
  • A 4xx response is logged as a permanent failure and not retried.

Returns

  • Nothing on 2xx.

Properties

  • async: true
  • thread_safe: true
  • pure: false (outbound HTTP)

Contract: CircuitBreakerWrapper._on_failure

Internal in every SDK (Python _on_failure, TypeScript _onFailure, Rust private on_failure); specified because all three must behave the same.

Inputs

  • error (Exception / Error / &str, required) — the delivery error that just occurred. The wrapper is per-subscriber, so no subscriber ID is passed.

Errors

  • None — it records state and never raises.

Returns

  • The lifecycle event to emit when the call changed the circuit's state (apcore.subscriber.circuit_opened), or the language's empty value when it did not: ApCoreEvent | None (Python), ApCoreEvent | null (TypeScript), Option<ApCoreEvent> (Rust). The current state is read through the wrapper's own accessor.

Properties

  • async: false
  • thread_safe: true (state is lock-protected)
  • pure: false (mutates circuit state)
  • idempotent: false