Architecture

Hermes Hook & FFI Path

replacement_frominline_store + recent_markers (AphroditeState)transform_tool_result_innerhooks::transform_tool_result_with_metatools::unwrap_hermes_resultaphrodite_hermes_call_hook_call_json (forced restype + same-handle free)_load_dylib (probe + load-once-per-process)__init__.py _hook_dispatchHermes hostreplacement_frominline_store + recent_markers (AphroditeState)transform_tool_result_innerhooks::transform_tool_result_with_metatools::unwrap_hermes_resultaphrodite_hermes_call_hook_call_json (forced restype + same-handle free)_load_dylib (probe + load-once-per-process)__init__.py _hook_dispatchHermes hostunwraps {output,exit_code}/{diff}/{error}/{matches}/{content}original content still hashed verbatim - only type/preview affectedalt[over threshold, compressible][skipped]transform_tool_result(tool_name, result, status, error_*, duration_ms)1_load_dylib() (probe + load-once-per-process)2_probe_dylib in subprocess once per unique path -SIGSEGV stays in the child, never the gateway3aphrodite_hermes_call_hook("transform_tool_result", args_json)4fn.restype = c_void_p (forced - default c_int truncates 64-bit ptr)5extern "C" (guarded - panic → {"error":...})6unwrap_hermes_result(tool_content) → (classify_content, type)7transform_tool_result_with_meta(state, content, tool, classify, meta)8inner pipeline9record_tool_event_from_meta (always - telemetry)10gates: empty / essential-tool / self-tool / below tool_threshold11inline_store_put(hash, content) · record_marker12{compressed:true, hash, type, size, preview, marker}13{compressed:false, reason}14replacement_from(result)15marker string (or Value::Null if not compressed)16_read_str(ptr) - NULL guarded BEFORE the cast17aphrodite_hermes_free_string(ptr) - through the SAME dylibhandle that produced the pointer (never a reloaded image)18json19string → Hermes swaps tool output for marker (or leaves it on null)20
TOOL-RESULT / TERMINAL-OUTPUT TRANSFORM (BRIDGE PATH)
Three entry points convergeyesnobridge: call_hook('pre_llm_call')core FFI: aphrodite_dispatch('pre_llm_call') →hooks::pre_llm_callcontext_engine_pre_llm toolflow::build_turn_context(state, est_bytes)directives = build_directive_context - NEVERdroppednudges = render_nudges - ≤2 inline [nudge:...],never droppedrecall = session::catalog_summary - droppableFIRSTassemble top-down under state.flow_budget_chars(default 4000)joined len > budget?pop last section (recall block) -directives+nudges surviveemit contextinjected into model turn:[directives]...[nudge:...][recall]...retrieve hint
PRE_LLM_CALL - THE DIRECTIVE-INJECTION CHOKE POINT
aphrodite core (handle-based ABI)aphrodite-hermes (process-global state)Python shimrestype/argtypesreplayedrestype/argtypesreplayed_make_handler → dispatch_tool_hook_dispatch → call_hook_configure_ffi: generated _bindings.pybind_to(dylib) → manual fallback →_REQUIRED_VOID_P assertionaphrodite_hermes_dispatch_toolaphrodite_hermes_call_hookaphrodite_hermes_get_schemas / get_hooksschemas + hook listaphrodite_dispatchhooks::{transform_*, pre_llm_call, post_llm_call}tools::dispatch registrywith_shared → AphroditeState (OnceLock)pre_llm_call: bridge + core +context_engine_pre_llmALL call flow::build_turn_context - singleassembler
PARITY: BRIDGE PATH VS CORE (HANDLE-BASED) PATH
IN THIS DOCUMENT

Crimson marks the heading currently in view; every entry is one of the document's own headings.

The Hermes plugin path: Hermes fires a hook → the Python ctypes shim resolves the dylib fresh for each call → the C ABI in crates/aphrodite-hermes (aphrodite_hermes_call_hook, panic-guarded) → the core aphrodite crate hook bodies.

The pre_llm_call arm is the choke point: the bridge, the core hooks::pre_llm_call, and the context_engine_pre_llm tool all converge on the single assembler flow::build_turn_context, so directive injection can never fork between paths.

FFI declarations come from the generated _bindings.py (cbindgen → ctypesgen, regenerated by the build script whenever the C ABI surface changes): bind_to(dylib) replays restype/argtypes onto the plugin’s own CDLL handle - the generated module never loads a library itself.

When the artifact is absent (fresh checkout, aphrodite setup install) or fails to apply, the shim degrades to the manual _manual_ffi_setup fallback; on every load, _REQUIRED_VOID_P asserts each pointer-returning export reads at full 64-bit width (a truncated default c_int restype would SIGSEGV in _read_str).

_probe_dylib smoke-tests each unique dylib path in a subprocess first, so a faulting image degrades to a graceful “plugin disabled” instead of killing the gateway.

Tool-result / terminal-output transform (bridge path)

replacement_frominline_store + recent_markers (AphroditeState)transform_tool_result_innerhooks::transform_tool_result_with_metatools::unwrap_hermes_resultaphrodite_hermes_call_hook_call_json (forced restype + same-handle free)_load_dylib (probe + load-once-per-process)__init__.py _hook_dispatchHermes hostreplacement_frominline_store + recent_markers (AphroditeState)transform_tool_result_innerhooks::transform_tool_result_with_metatools::unwrap_hermes_resultaphrodite_hermes_call_hook_call_json (forced restype + same-handle free)_load_dylib (probe + load-once-per-process)__init__.py _hook_dispatchHermes hostunwraps {output,exit_code}/{diff}/{error}/{matches}/{content}original content still hashed verbatim - only type/preview affectedalt[over threshold, compressible][skipped]transform_tool_result(tool_name, result, status, error_*, duration_ms)1_load_dylib() (probe + load-once-per-process)2_probe_dylib in subprocess once per unique path -SIGSEGV stays in the child, never the gateway3aphrodite_hermes_call_hook("transform_tool_result", args_json)4fn.restype = c_void_p (forced - default c_int truncates 64-bit ptr)5extern "C" (guarded - panic → {"error":...})6unwrap_hermes_result(tool_content) → (classify_content, type)7transform_tool_result_with_meta(state, content, tool, classify, meta)8inner pipeline9record_tool_event_from_meta (always - telemetry)10gates: empty / essential-tool / self-tool / below tool_threshold11inline_store_put(hash, content) · record_marker12{compressed:true, hash, type, size, preview, marker}13{compressed:false, reason}14replacement_from(result)15marker string (or Value::Null if not compressed)16_read_str(ptr) - NULL guarded BEFORE the cast17aphrodite_hermes_free_string(ptr) - through the SAME dylibhandle that produced the pointer (never a reloaded image)18json19string → Hermes swaps tool output for marker (or leaves it on null)20
TOOL-RESULT / TERMINAL-OUTPUT TRANSFORM (BRIDGE PATH)

_call_json frees every pointer through the exact dylib object that allocated it, never through a cached or reloaded handle - benign today while both generations use the system allocator, undefined behavior the day a custom global allocator lands.

pre_llm_call - the directive-injection choke point

Three entry points convergeyesnobridge: call_hook('pre_llm_call')core FFI: aphrodite_dispatch('pre_llm_call') →hooks::pre_llm_callcontext_engine_pre_llm toolflow::build_turn_context(state, est_bytes)directives = build_directive_context - NEVERdroppednudges = render_nudges - ≤2 inline [nudge:...],never droppedrecall = session::catalog_summary - droppableFIRSTassemble top-down under state.flow_budget_chars(default 4000)joined len > budget?pop last section (recall block) -directives+nudges surviveemit contextinjected into model turn:[directives]...[nudge:...][recall]...retrieve hint
PRE_LLM_CALL - THE DIRECTIVE-INJECTION CHOKE POINT

post_llm_call is the mirror choke point on both paths: archive_turn (last marker of the turn → conv_index) → next_turn (turn++) → flow::purge_expired_nudges (after the counter advances, so a ttl=1 nudge renders exactly once) → poll_worker::expire_stale_tasks.

Parity: bridge path vs core (handle-based) path

aphrodite core (handle-based ABI)aphrodite-hermes (process-global state)Python shimrestype/argtypesreplayedrestype/argtypesreplayed_make_handler → dispatch_tool_hook_dispatch → call_hook_configure_ffi: generated _bindings.pybind_to(dylib) → manual fallback →_REQUIRED_VOID_P assertionaphrodite_hermes_dispatch_toolaphrodite_hermes_call_hookaphrodite_hermes_get_schemas / get_hooksschemas + hook listaphrodite_dispatchhooks::{transform_*, pre_llm_call, post_llm_call}tools::dispatch registrywith_shared → AphroditeState (OnceLock)pre_llm_call: bridge + core +context_engine_pre_llmALL call flow::build_turn_context - singleassembler
PARITY: BRIDGE PATH VS CORE (HANDLE-BASED) PATH

Both guarded() wrappers (core and bridge) catch panics across the FFI boundary and return {"error":"internal error: panicked..."} instead of aborting the process.

The _with_meta variants exist only on the bridge, because the handle-based core ABI has no Hermes status/error_type telemetry to plumb.

The bridge exposes no skill export; the plugin registers tools and hooks only, and skills live dev-side.

Call sites

ConcernModule
_load_dylib (probe + load-once), _call_json, _hook_dispatch, registerplugins/aphrodite/__init__.py
FFI setup: _configure_ffi / _manual_ffi_setup / _REQUIRED_VOID_Pplugins/aphrodite/__init__.py
_probe_dylib (subprocess smoke test) / _read_str (NULL guard)plugins/aphrodite/__init__.py
aphrodite_hermes_call_hook (bridge hooks) / replacement_fromcrates/aphrodite-hermes/src/lib.rs
tools::unwrap_hermes_result / tools::dispatchcrates/aphrodite-hermes/src/tools.rs
hooks::transform_tool_result_inner / pre_llm_call / post_llm_callcrates/aphrodite/src/hooks.rs
flow::build_turn_context (assembler)crates/aphrodite/src/flow.rs
Core C ABI guarded / aphrodite_dispatchcrates/aphrodite/src/lib.rs