# Wiring the proactive layer into the code

The design is in [Proactive layer](https://fzkuji.github.io/OpenProgram/docs/reference/design/proactive/README.html) and wins on any conflict. `openprogram/runtime/events/bus.py` is the event layer; taps sit in the source files that already know when something happened. `Event` has three core fields plus open `metadata`, so `turn` and `session` travel there and a source with no turn invents none.

## Reused mechanisms

| Design role | Mechanism | Location |
| --- | --- | --- |
| Event fan-out | `EventBus` | `runtime/events/bus.py` |
| Gate's `ask` | `wrap_with_approval`; a denial returns an `is_error` tool result | `runtime/permissions/approvals/approval.py` |
| `Prepare` background task | `JobRunner.spawn_job` | `runtime/jobs/runner/` |
| `Inject` landing | Memory prefetch and steering messages | `runtime/agent/loop/` |
| Causality, rewind, branching | Session DAG, nodes carry `parent_id` and caller | `runtime/context/git/` |
| Hard enforcement point | `_execute_tool_calls` | `runtime/agent/loop/tool_dispatch.py` |

## Event taps

| Event | Source |
| --- | --- |
| `user.prompt_submitted` | `runtime/conversation/turn_runtime/prep.py` |
| `model.response_started`, `model.response_completed` | `runtime/agent/loop/provider_lifecycle.py` |
| `tool.before` | `tool_dispatch.py`, before every execute. One event feeds the notify emit and the gate query |
| `tool.after` | `tool_dispatch.py`, after every call |
| `subagent.started`, `subagent.ended` | `runtime/jobs/runner/shared.py` |
| `file.changed` | After a successful write in `programs/tools/files/`: `write.py`, `edit.py`, `apply_patch.py`, `_remote.py` |
| External sources | `runtime/context/engine.py` (compaction), `interfaces/channels/inbound/_conversation.py`, `state/memory/session_watcher.py`, web server skills and plugins routes; auth through `runtime/events/bridges.py`, installed idempotently at worker start |

`emit_ws_frame` carries web frames over the bus and `interfaces/server/server.py` forwards them, so the job runner, sub-agent run, worktree manager, functions watcher and channel broadcast do not import the web UI.

## Gate mounts

|  | Chat path | Agentic nested path |
| --- | --- | --- |
| Mount | `decide_tool_gate` in `_execute_tool_calls`, ahead of `tool.execute` | `_pre_invocation_hooks` in `runtime/agent/call_state.py`, beside the cancel check |
| Guarantee | Hard: every chat tool call passes here | Optional: a mount point, with no claim of total coverage |

The gate query does not read `permission_mode`, so it also applies to sub-agent turns in any mode ([invariants](https://fzkuji.github.io/OpenProgram/docs/reference/design/proactive/invariants.html), [execution model](https://fzkuji.github.io/OpenProgram/docs/reference/design/proactive/execution-model.html)).

## Prepare

Designed to use `JobRunner.spawn_job` with an allowlist excluding bash, write and network tools, in a separate pool of 1–2, preemptible by user tasks, yielding on 429. Today the engine emits `proactive.prepare_requested` and `proactive.inject` events; nothing yet spawns the task or lands the steering message.

## Known gap

`@function` tool execution writes no DAG node; only `Agent` methods do. The DAG is therefore incomplete as a causal record, and audit relies on the full event set in `events.jsonl` instead.

## Checking a wiring change

`py_compile`, relevant unit tests, a deploy to the development instance, healthy `/healthz`, one real message through the Web UI (`npm --prefix apps/web run build` first for frontend changes). For ordering, run a turn with a tool call and read the session's `events.jsonl`:

```
user.prompt_submitted → model.response_started → tool.before → tool.after → model.response_completed
```

Each entry carries session and turn in metadata.

## Implementation status

| Part | State |
| --- | --- |
| Bus, taps, bridges, Web UI as subscriber | Implemented |
| `runtime/proactive/`, `engine.py::install_proactive`, `policies/` | Implemented, not installed: `operations/worker/runner.py` installs only the event bridges |
| Sync query point | `register_tool_gate`, `decide_tool_gate`, `ToolGateDenied` in `runtime/events/tool_gate.py` |
| Tests | `tests/component/agent/turns/test_event_bus.py`, `tests/unit/agent/execution/test_tool_gate.py`, `tests/unit/agent/behavior/test_event_bridges.py`, `test_proactive_engine.py`, `test_proactive_policies.py` |
| Not claimed | Installed-App and live WebSocket acceptance |

Project skills resolve from the process working directory; a worker started in the home directory reads `~/skills`.
