# Event reference

Source of truth: `EVENTS` in `openprogram/runtime/events/registry.py`. Paths are under `openprogram/`. The label under an event is its origin, after its kind unless it is notify.

## Turns and tools

| Event | Payload | When and from where |
| --- | --- | --- |
| `tool.before` (gate · agent) | `{tool, tool_call_id, args}` | Before each tool executes. A deny reason becomes the model's error tool result. `runtime/agent/loop/tool_dispatch.py` |
| `tool.result` (transform · tool) | `{tool, tool_call_id, args, is_error}; value: result text` | Before the result enters the model's context; only when a rewriter exists. Images and audio are kept. `runtime/agent/loop/tool_dispatch.py` |
| `tool.after` (tool) | `{tool, tool_call_id, is_error, result_text}` | After a tool call finishes. Text channel only. `runtime/agent/loop/tool_dispatch.py` |
| `turn.start` (system) | `{session_id, user_msg_id, assistant_msg_id}` | User message persisted, Agent loop about to run. `runtime/conversation/turn_runtime/__init__.py` |
| `turn.end` (system) | `{session_id, user_msg_id, assistant_msg_id, usage}` | Finalize completed. `runtime/conversation/turn_runtime/__init__.py` |
| `turn.stop` (gate · system) | `{session_id, user_msg_id, assistant_msg_id, last_text (≤4000), stop_hook_active}` | After a completed top-level chat turn. A denial starts a continuation turn. `runtime/conversation/turn_runtime/stop_hook.py` |
| `user.prompt_submitted` (user) | `{msg_id, chars}` | User message row persisted. `runtime/conversation/turn_runtime/prep.py` |
| `model.response_started` (agent) | `{request_id}` | One provider request dispatched within the loop. `runtime/agent/loop/provider_lifecycle.py` |
| `model.response_completed` (agent) | `{request_id, status, is_error}` | One response ended, on every exit. Per loop iteration, not per turn. `runtime/agent/loop/provider_lifecycle.py` |
| `file.changed` (tool) | `{path, op}` | A file tool changed the tree. `op`: write, edit, patch; `apply_patch` also sends add, update, delete. `programs/tools/files/{write,edit,apply_patch,_remote}.py` |

## Sessions and interaction

| Event | Payload | When and from where |
| --- | --- | --- |
| `session.start` (system) | `{session_id, agent_id, channel}` | WebUI created a session. `interfaces/server/server_runtime/session_lifecycle.py` |
| `chat.before_send` (user) | `{session_id, msg_id, text, agent_id, attachments}` | Chat message persisted, about to enter the runtime. `interfaces/server/ws_actions/chat/turn.py` |
| `question.asked` (agent) | `{session_id, question, …}` | The Agent asked through the question channel. Also sends a `ws.frame`. `runtime/agent/questions.py` |
| `mcp.request.cancelled` (system) | `{request_id, execution_id, session_id, client_id, reason}` | An MCP prompt request was cancelled and cleaned up. `extensions/mcp/server/service.py` |
| `ws.frame` (system) | `{frame}` | Carries a rendered WebSocket frame to the WebUI without importing the server. `runtime/events/bus.py (emit_ws_frame)` |

## Plugins, goals and scheduling

| Event | Payload | When and from where |
| --- | --- | --- |
| `plugin.enable` (system) | `{plugin}` | Plugin loaded, contributions and hooks registered. `extensions/plugins/loader.py` |
| `plugin.disable` (system) | `{plugin}` | About to unload; subscriptions still live. `extensions/plugins/loader.py` |
| `goal.update` (system) | `{session_id, goal: {goal_id, revision, text, status, phase, checklist, budget, usage, pause_reason, last_reason}}` | Goal state changed. `programs/tools/planning/goal/state.py` |
| `reminder.fired` (system) | `{task_id, title, message, session_id, agent_id, wake, scheduled_for, fired_at}` | A reminder came due; shown, and wakes the Agent when wake is true. `runtime/scheduler/reminders.py` |
| `skills.changed` (system) | `{}` | The skills watcher saw an install or update on disk. `interfaces/server/server.py` |
| `plugins.update_available` (system) | `{count}` | The update checker found newer versions. The registry documents `{plugin, current, latest}`. `interfaces/server/server.py` |

## Context, memory and security

| Event | Payload | When and from where |
| --- | --- | --- |
| `context.compacted` (system) | `{ok, tokens_before, tokens_after, reason}` | A compaction run finished. `runtime/context/engine.py` |
| `memory.degraded` (system) | `{reason_code}` | Optional memory is unavailable for a turn. `runtime/conversation/memory_execution.py, state/memory/policy.py` |
| `memory.ingest_started` (system) | `{messages}` | The session watcher began a batch. `state/memory/session_watcher.py` |
| `memory.ingest_ended` (system) | `{ok, retryable, reason, attempt?, next_attempt_at?}` | Ingest finished. `retryable=false` stops offering that session until it changes; a retry carries its attempt and when it comes. `state/memory/session_watcher.py` |
| `security.risky_tool_delegated` (system) | `{session_id, tool, source, authority}` | A delegated risky-tool check was allowed by bypass or an allow rule. No argument content; not an execution receipt. `runtime/permissions/audit.py` |
| `security.path_violation` (system) | `{session_id, tool, reason}` | A delegated write-target check denied access. Fixed reason, no path. `runtime/permissions/audit.py` |

## Channels and collaboration

| Event | Payload | When and from where |
| --- | --- | --- |
| `channel.message_inbound` (user) | `{channel, peer_kind, chars}` | An external message arrived. `interfaces/channels/inbound/_conversation.py` |
| `branch.message_sent` (agent) | `{from, to}` | A message went to another session branch. `programs/tools/agents/send_message/send_message/send_message.py, programs/tools/agents/agent/agent/agent.py` |
| `agents.listed` (agent) | `{sessions, branches}` | `list_agents` enumerated targets. `programs/tools/agents/send_message/list_agents/list_agents.py` |
| `subagent.started` (system) | `{job_id, label}` | A background Agent job entered running. `runtime/jobs/runner/shared.py` |
| `subagent.ended` (system) | `{job_id, status, error}` | A background Agent job reached a terminal state. `runtime/jobs/runner/shared.py` |
| `branches.listed` (agent) | `{session, count}` | Registry only: no emitter. `runtime/events/registry.py` |
| `sessions.listed` (agent) | `{count}` | Registry only: no emitter. `runtime/events/registry.py` |

## Bus calls

| Call | Note |
| --- | --- |
| `get_event_bus() → EventBus` | Worker singleton. |
| `create_event_bus()` | Independent instance; no disk writes. |
| `make_event(type, origin, payload, metadata) → Event` | Fills `id`, `ts`, session and turn. |
| `emit_safe(type, origin, payload=None, metadata=None)` | Builds and emits; event-layer failures are swallowed. |
| `emit_ws_frame(frame)` | Sends a `ws.frame`. |
| `subscribe(handler, *, types=None) → unsubscribe` | `types=None` receives every typed event. |
| `subscribe_gate(type, fn) → unregister` | Python gate. |
| `emit_gate(event, timeout_s=None) → GateOutcome` | Asks every gate. |
| `subscribe_transform(type, fn) → unregister` | Rewriter. |
| `emit_transform(event, value)` | Returns the final value. |
| `register_tool_gate(gate) → unregister` | Shortcut for `tool.before`. |

**Examples**

```
from openprogram.runtime.events import get_event_bus

def on_file_changed(event):
    print(event.payload["path"], event.metadata.get("session"))
unsubscribe = get_event_bus().subscribe(on_file_changed, types={"file.changed"})

def guard(event):
    if event.payload.get("tool") == "bash":
        return "policy denied this tool call"
    return None
remove = get_event_bus().subscribe_gate("tool.before", guard)
```

## Unregistered types

| Type | State |
| --- | --- |
| `credential.cooldown`, `credential.rotated`, `credential.exhausted` | Translated from AuthEvent by `bridges.py`, payload `{provider, credential, detail}`. The bus tolerates them and warns once per type. |
| `proactive.inject`, `proactive.prepare_requested` | Derived events of the proactive engine, which is not installed. |
| `question.replied`, `question.rejected`, `programs:changed` | Frame types inside `ws.frame`, not typed events. Spawns, attach cards, reports, notices, merges and deletions reach an open chat as `transcript_update`. |

## Reading the log

```
tail -f ~/.openprogram/logs/events.jsonl
```

A top-level turn usually shows `user.prompt_submitted`, `turn.start`, `model.response_started`, `tool.before`, `tool.after`, `model.response_completed`, `turn.end`. Failure, cancellation, tool calls and stop hooks change the order.

[Event layer](https://fzkuji.github.io/OpenProgram/docs/reference/design/proactive/event-layer.html)
