HOOKS // THE CHAIN

Six hooks, one turn

A tool call's output becomes part of the model's context, and the hooks are the points where Aphrodite sees that output first.

Every hook is a thin dispatch that forwards its arguments to the Rust dylib and hands the JSON reply back to Hermes.

It is the reason the shim registers one callback per name: on plugin load it asks the dylib for its hook names, and the six hooks are always registered together.

The chain is the whole integration: each seam fires in turn order, and nothing reaches the model that a seam did not first see.

01 // SEQUENCE // PER TURN

The loop a turn walks

  1. session starts
  2. → on_session_start · reset per-session state
  3. ↓
  4. user turn
  5. → pre_tool_call · auto-background, chain split
  6. ↓
  7. tool or terminal runs
  8. → transform_tool_result / transform_terminal_output · compress to marker
  9. ↓
  10. → pre_llm_call · inject directives, nudges, recall catalog
  11. ↓
  12. LLM responds
  13. → post_llm_call · archive turn, advance counter, expire
  14. ↓
  15. ⟲ next turn

The loop closes on itself, so the state a turn leaves behind is the state the next turn reads.

That is why the reset seam exists: a new session starts from a clean baseline instead of inheriting the previous session's counters and markers.

H1 on_session_startresets per-session stateinjects nothinguser turnH2 pre_tool_callauto-backgrounds long terminaland process callspoll-actions never backgroundedchain split is opt-inthe tool or terminal runsH3 transform_tool_resultskips: empty, essential tools,self tools, under 4,096 charsH4 transform_terminal_outputunder 1,024 passes untouchedchain-split segments unconditionalH5 pre_llm_callfixed order under 4,000 chars:first-turn orientationdirectives never droppednudges max 2 never droppedbackground-task statusrecall catalog dropped firstthe LLM respondsH6 post_llm_callarchives the last markeradvances the turnpurges expired nudgesexpires stale poll tasks
M3 // HOOK CHAIN

02 // THE SIX // ONE SECTION EACH

What each seam does

  1. 01 on_session_start RESET
  2. 02 transform_tool_result COMPRESS
  3. 03 pre_llm_call ASSEMBLE
  4. 04 transform_terminal_output COMPRESS
  5. 05 post_llm_call ARCHIVE
  6. 06 pre_tool_call PRE-FLIGHT

03 // ON_SESSION_START

The seam that starts a clean session

ORDER
01 / 06
DISPATCH
args → libaphrodite_hermes.dylib → JSON reply
REGISTERED
provides_hooks · plugin.yaml
RESET

Resets every piece of per-session state to a clean baseline: turn counter, conversation index, recent markers, referenced files, tool-event telemetry, ephemeral directives.

Injects nothing - the one-shot first-turn orientation renders later, on the first pre_llm_call.

04 // TRANSFORM_TOOL_RESULT

The seam that compresses a tool result

ORDER
02 / 06
DISPATCH
args → libaphrodite_hermes.dylib → JSON reply
REGISTERED
provides_hooks · plugin.yaml
COMPRESS

After every tool call: record telemetry, run the skip gates, classify the content type, hash (BLAKE3), store, and replace the output with <<<CCR:hash|type|size>>> plus a compact preview.

The hook tracks file references before the gates, so aphrodite_files sees every read even when nothing was compressed.

05 // PRE_LLM_CALL

The seam that assembles the context

ORDER
03 / 06
DISPATCH
args → libaphrodite_hermes.dylib → JSON reply
REGISTERED
provides_hooks · plugin.yaml
ASSEMBLE

Runs the poll-worker checkpoint, then the context assembler: first-turn orientation (turn 0 only), [directives:] (never dropped), [nudge:] one-shots at most two (never dropped), background-task status, and the [recall] catalog - the first section dropped under pressure.

One hard budget: 4,000 chars.

06 // TRANSFORM_TERMINAL_OUTPUT

The seam that compresses terminal output

ORDER
04 / 06
DISPATCH
args → libaphrodite_hermes.dylib → JSON reply
REGISTERED
provides_hooks · plugin.yaml
COMPRESS

Same pipeline for shell stdout/stderr.

The hook records non-zero exit codes as failures for the error-loop detector; chain-split segments compress unconditionally, before the threshold gate, so marked output never leaks raw; everything else passes the 1,024-char gate first.

07 // POST_LLM_CALL

The seam that closes the turn

ORDER
05 / 06
DISPATCH
args → libaphrodite_hermes.dylib → JSON reply
REGISTERED
provides_hooks · plugin.yaml
ARCHIVE

Archives the last marker of the turn into the conversation index (so aphrodite_diff can report per-turn compressions), advances the turn counter, purges expired nudges after the counter advances - a one-shot nudge renders exactly once - and expires stale poll-worker tasks.

08 // PRE_TOOL_CALL

The seam before a tool runs

ORDER
06 / 06
DISPATCH
args → libaphrodite_hermes.dylib → JSON reply
REGISTERED
provides_hooks · plugin.yaml
PRE-FLIGHT

Two interventions before a tool executes.

Auto-backgrounding: the hook rewrites long-running terminal/process calls to run in the background (poll worker, default on).

Chain splitting (opt-in): the hook splits chained commands with invisible segment markers so each segment compresses into its own CCR marker.

Otherwise the call passes through unchanged.

09 // BYPASS // THE ESSENTIAL-TOOL GATE

What never gets compressed

A result is left untouched when it is empty, below threshold, or from the essential set - tools the agent needs raw:

skill_view · skills_list · skill_manage · memory · session_search · read_file · read_terminal

Aphrodite also skips its own tools and headroom helpers - compressing aphrodite_retrieve would just replace resolved content with another marker.

The gate is the reason retrieval stays cheap: a marker the model resolves comes back whole, because nothing downstream of the gate rewrites it a second time.

10 // THRESHOLDS // GATES AND BYPASSES

The numbers that decide

HOOKSETTINGDEFAULTBYPASS
transform_tool_resulttool_threshold_token4,096empty · essential tools · self tools · 0 = always compress
transform_terminal_outputterminal_threshold1,024empty · below threshold · 0 = always compress
pre_llm_callflow budget_chars4,000recall catalog drops first; directives and nudges never drop
pre_tool_call (chain split)chain_split_min_segments-feature off by default (chain_split = false)

PRECEDENCE: ENVIRONMENT > TOML > DEFAULT (APHRODITE_TOOL_THRESHOLD_TOKEN · APHRODITE_TERMINAL_THRESHOLD · APHRODITE_FLOW_BUDGET_CHARS · APHRODITE_CHAIN_SPLIT).

SRC: docs/plugin/hooks.md; plugins/aphrodite/plugin.yaml (provides_hooks)

SRC: docs/architecture/04-hook-ffi.md