aphrodite startup splits into two halves.
The binary half resolves configuration (env > TOML > default), binds every listener before spawning any server, builds one AppState per listener with a CCR store backend, and arms a config-file reload watcher.
The Hermes plugin half runs once per Hermes home inside register(): it loads and smoke-tests the dylib, checks the runtime layout (report-only), materializes the built-in directives, registers hooks and tools, and launches the proxy when it is not already healthy.
Startup sequence run_single config watcher task CcrStore backend proxy::build_state bind loop MultiConfig run() tokio runtime main() run_single config watcher task CcrStore backend proxy::build_state bind loop MultiConfig run() tokio runtime main() env > TOML(proxy>defaults) > default API-key chain, per-mode port override, timeout clamp ≤600s, max_output<max_context alt [config file exists (multi-proxy)] [no config (CLI fallback)] alt [mode == Token && !no_ccr_marker] [mode == Cache] [Token && no_ccr_marker] loop [each (name, cli) in proxies] on Modify(aphrodite.toml): debounce 500ms → MultiConfig::load → resolve_thresholds → store into every live AppState's 4 atomics loop [each bound listener] OS / shell exec `aphrodite [args]` 1 --version / --help early-exit checks 2 `aphrodite setup` subcommand handled before the runtime 3 worker_threads = env APHRODITE_WORKER_THREADS or (cpus*4).max(32) 4 Builder::new_multi_thread().worker_threads(n).enable_all() 5 block_on(run()) 6 resolve config_path: APHRODITE_CONFIG_PATH → ./aphrodite.toml → ~/.hermes/aphrodite/aphrodite.toml 7 MultiConfig::load(path) 8 proxies[], [compression], [defaults] 9 for each proxy: config.resolve(p) → Cli 10 Cli::parse() (requires --api-key) 11 init tracing subscriber 12 resolve relative ccr_db_path vs exe dir · mkdir -p parent 13 TcpListener::bind(cli.listen) - a bind failure aborts startup 14 build_state(&cli, compression) 15 resolve_thresholds(compression) 16 SqliteCcrStore::open(db_path, ccr_ttl_seconds) 17 InMemoryCcrStore::with_capacity_and_ttl(10_000, ttl) 18 None (no CCR backend) 19 Arc<AppState> (atomics seeded: ema=200, fill=9000, thresholds) 20 bound.push((name, cli, listener, state)) 21 spawn notify watcher on aphrodite.toml dir 22 spawn run_single(name, cli, listener, state, shutdown_rx) 23 warn if APHRODITE_MGMT_TOKEN unset 24 build restricted Router (/stats,/retrieve,/ccr/*,/reload,...) + catch_all /{*path} → proxy_handler + /health (public) 25 axum::serve(...).with_graceful_shutdown(shutdown_rx.changed()) 26 SIGINT / SIGTERM 27 shutdown_tx.send(true) → all listeners drain 28 select: drain done | 5s timeout | 2nd Ctrl+C → abort remaining 29 OS / shell STARTUP SEQUENCE Port override (env pierces TOML per mode) Some s None cache token other Ok port present, malformed absent ProxyConfig (from aphrodite.toml) MultiConfig::resolve cfg.listen set? s.parse::<SocketAddr> (fail→error) default 127.0.0.1:9797 mode / name apply_port_override(listen, APHRODITE_CACHE_PORT) apply_port_override(listen, APHRODITE_TOKEN_PORT) listen unchanged env var parses as u16? addr.set_port(port) · info! log warn! · keep listen Cli.listen PORT OVERRIDE (ENV PIERCES TOML PER MODE) Plugin startup (register() - the Hermes-side half) register() runs once per Hermes home (one PluginManager per home) and is a pure loader : all logic lives in the dylib.
Runtime artifacts (binary, dylib, aphrodite.toml, ccr.db, logs) live under the canonical runtime home $HERMES_HOME/aphrodite (else ~/.hermes/aphrodite), never inside the plugin tree.
_start_proxy aphrodite_hermes_get_hooks / get_schemas aphrodite_hermes_materialize_directives layout_check.check_and_heal _probe_dylib (subprocess) _load_dylib register() Hermes host (PluginManager) _start_proxy aphrodite_hermes_get_hooks / get_schemas aphrodite_hermes_materialize_directives layout_check.check_and_heal _probe_dylib (subprocess) _load_dylib register() Hermes host (PluginManager) canonical layout per layout_schema.json v2 (report-only): detect + log deviations, quarantine stale plugin-source copies never moves in a git checkout, never touches plugins/aphrodite (Hermes-owned) embeds the built-in directives from the binary (include_str!) → writes ~/.hermes/aphrodite/directives/ idempotent, never overwrites user-modified files loop [each hook (per-hook try/except - one failure never aborts)] loop [each tool (per-tool try/except)] skills are not shipped - they live dev-side context engine is opt-in (APHRODITE_CONTEXT_ENGINE=1) alt [both healthy] [launch needed] register(ctx) 1 _load_dylib() 2 _ensure_binaries(): presence check - NEVER downloads missing → log explicit setup command (download.sh / aphrodite setup) legacy APHRODITE_AUTO_DOWNLOAD=1 opt-in runs download.sh 3 subprocess smoke-test (once per unique path) 4 rc==0 → CDLL on the resolved canonical path (once per process, no hot-reload) 5 _configure_ffi: _bindings.py bind_to → manual fallback → _REQUIRED_VOID_P assertion 6 check_and_heal() (best-effort, never raises) 7 materialize_directives(home_dir=b"") 8 {"status":"ok","written":[...],"skipped":[...],"warnings":[...]} 9 hooks[] + schemas[] 10 ctx.register_hook(name, _hook_dispatch) (fresh dylib per call) 11 ctx.register_tool(name, "aphrodite", schema, _make_handler(name)) 12 _start_proxy() 13 pre-launch probe: GET /health on :9797/:9798 (direct-only opener, confirmed "healthy" body) → reuse running instance? 14 skip launch 15 Popen(aphrodite binary) · stderr → ~/.hermes/aphrodite/proxy-stderr.log 16 immediate-death check (stderr tail + API-key hint) · poll both health endpoints ≤5s 17 PLUGIN STARTUP (REGISTER() - THE HERMES-SIDE HALF) The layout check and the directives materialize are best-effort by design: either failing degrades to a warning and the plugin still registers (never a raise).
The dylib load itself is the one hard gate - if the smoke test or the FFI assertion fails, register() logs “plugin disabled” and returns.
Call sites Concern Module Runtime + subcommand dispatch crates/aphrodite/src/main.rsConfig path resolution + bind-before-spawn crates/aphrodite/src/main.rsConfig reload watcher crates/aphrodite/src/main.rsPer-listener router + serve crates/aphrodite/src/main.rsMultiConfig::resolve / apply_port_overridecrates/aphrodite/src/config/proxy::build_state (CCR backend selection)crates/aphrodite/src/proxy.rsresolve_thresholdscrates/aphrodite/src/proxy.rsSqliteCcrStore / InMemoryCcrStorevendor/headroom/crates/headroom-core/src/ccr/backends/{sqlite,in_memory}.rsPlugin register() / _load_dylib / _probe_dylib / _start_proxy plugins/aphrodite/__init__.pyLayout check (report-only) plugins/aphrodite/layout_check.py (+ layout_schema.json)Directives materialize FFI crates/aphrodite-hermes/src/lib.rs