Skip to content

Wire a Lifecycle Hook

Lifecycle hooks let you react to RaiSE events — session start, graph build, pattern added, work lifecycle, and more. They follow the same extension model as adapters: a Python Protocol contract discovered via entry points.

Every hook implements this contract:

from typing import ClassVar, Protocol, runtime_checkable
from rai_cli.hooks.events import HookEvent
from rai_cli.hooks.protocol import HookResult
@runtime_checkable
class LifecycleHook(Protocol):
events: ClassVar[list[str]] # Events to subscribe to
priority: ClassVar[int] # Higher runs first (default 0)
def handle(self, event: HookEvent) -> HookResult: ...

Hooks are @runtime_checkable — no explicit inheritance needed, just implement the interface.

RaiSE emits 18 typed events as frozen dataclasses:

EventFires when
session:startSession begins
session:closeSession ends
graph:buildKnowledge graph rebuilt
pattern:addedNew pattern recorded
discover:scanCodebase scan completes
init:completeProject initialized
adapter:loaded / adapter:failedAdapter discovery
release:publishRelease published
work:start / work:closeStory/epic lifecycle
mcp:callMCP tool invocation
before:session:closeBefore session close (can abort)
before:release:publishBefore release (can abort)

before: events can return HookResult.abort("reason") to cancel the operation.

my_package/hooks.py
from typing import ClassVar
from rai_cli.hooks.events import HookEvent, PatternAddedEvent
from rai_cli.hooks.protocol import HookResult
class NotifyOnPatternHook:
"""Send a notification when a new pattern is added."""
events: ClassVar[list[str]] = ["pattern:added"]
priority: ClassVar[int] = 0
def handle(self, event: HookEvent) -> HookResult:
if isinstance(event, PatternAddedEvent):
print(f"New pattern: {event.pattern_id}")
return HookResult.ok()

Register your hook in pyproject.toml:

[project.entry-points."rai.hooks"]
my-notify = "my_package.hooks:NotifyOnPatternHook"

RaiSE discovers hooks at runtime from the rai.hooks entry point group — the same mechanism used for adapters.

RaiSE ships with 5 built-in hooks:

HookEventsPurpose
TelemetryHookAll 9 after-eventsRecords CommandUsage signals
MemoryMdSyncHookgraph:buildRegenerates MEMORY.md
JiraSyncHookwork:start, work:closeAuto-transitions Jira issues
BacklogHookWork lifecycleCreates/transitions backlog items
GateBridgeHookbefore:release:publishRuns quality gates before release

Hooks follow strict error isolation:

  • A failing hook never crashes the CLI
  • Exceptions are caught, logged, and skipped
  • Each hook has a 5-second timeout (configurable)
  • Even if one hook aborts, all others still run (all-notify semantics)

Verify your hook is discovered:

Terminal window
rai adapter list # hooks appear in rai.hooks entry point group

See also: rai adapter check, Create a Custom Adapter