trading_agent.extensions.hook_runner
¶
Runs the user's hook scripts when events happen.
A hook is any executable file in a folder named after an event, under workspace/shared/hooks/ or under an installed package's hooks/ folder:
workspace/shared/hooks/order_placed/notify.sh workspace/shared/hooks/kill_switch/page-me.py
Each matching hook runs as its own process with the event as JSON on its standard input, the event's name in TRADING_AGENT_EVENT and the desk in TRADING_AGENT_DESK. The folder name can be any event kind, such as order, risk_decision, kill_switch, session_started, desk_stopped or error, or one of two narrower names: order_placed (an order event whose action is "placed") and risk_rejected (a risk decision that refused an order).
Hooks run outside the gateway's process, at most four at a time, each for at most 30 seconds. A hook that fails, hangs or is missing is logged and has no effect on any desk. Secrets are masked in the JSON a hook receives, and a hook gets only a few safe environment variables, as MCP servers do.
Typical usage example:
runner = HookRunner(event_bus, Path('workspace/shared/hooks'), packages, redactor) task = asyncio.create_task(runner.run())
HookRunner
¶
Starts hook processes for events.
Subscribes to the bus at once, so no event is missed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_bus
|
EventBus
|
The bus to listen to. |
required |
hooks_directory
|
Path
|
The user's hooks folder, holding one subfolder per event. |
required |
packages
|
InstalledPackages | None
|
The installed packages, whose hooks/ folders are read for every event so a new package counts at once, or None. |
None
|
redactor
|
SecretRedactor | None
|
Masks secrets in the JSON given to hooks, or None to pass events unchanged. |
None
|
Source code in src/trading_agent/extensions/hook_runner.py
run
async
¶
Starts hooks for each event until cancelled, then stops the ones still running.
Source code in src/trading_agent/extensions/hook_runner.py
programs_for
¶
The hook programs an event should start.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
published_event
|
Event
|
The event. |
required |
Returns:
| Type | Description |
|---|---|
list[tuple[str, Path]]
|
Tuples (name, program), where name is the hook folder's event name and program an executable file in it. |