Expand description
Daemon runtime — orchestration shell for the ai-memory binary.
W6 lifted serve() and the top-level dispatch out of main.rs so the
production HTTP daemon, the integration test harness, and the
coverage-instrumented tests in this module all share one source of
truth. main.rs keeps its #[tokio::main] entry point but immediately
delegates here for every subcommand.
§Public surface (post-W6)
run— top-level CLI dispatch (called frommain()).serve— full HTTP daemon body (TLS or plain).bootstrap_serve— testable struct-returning state builder.build_router— composition wrapper aroundlib::build_router.build_embedder,build_vector_index— single canonical builders used by bothserve()andcli::recall::run.spawn_gc_loop,spawn_wal_checkpoint_loop— daemon background tasks, returning aJoinHandleso callers can abort on shutdown.is_write_command— write-command predicate driving the post-write WAL checkpoint.passphrase_from_file,apply_anonymize_default— startup helpers.
§Pre-W6 helpers retained
serve_http_with_shutdown,serve_http_with_shutdown_future— the in-process HTTP harness the integration suite drives.run_sync_daemon_with_shutdown,run_sync_daemon_with_shutdown_using_client,sync_cycle_once— the sync-daemon body.run_curator_daemon_with_shutdown,run_curator_daemon_with_primitives— the curator-daemon body.
Structs§
- Bench
Args - Cli
- Completions
Args - Serve
Args - Serve
Bootstrap - Aggregated state produced by
bootstrap_serve.
Enums§
Functions§
- apply_
anonymize_ default - Apply the configured
anonymize_defaultto the runtime env: when the config asks for anonymization but the user hasn’t already setAI_MEMORY_ANONYMIZE, set it to"1". Idempotent — repeated calls are a no-op once the env var is set. - bootstrap_
serve - Build all daemon state and spawn background tasks. Returns the aggregated state without binding any sockets — testable in isolation.
- build_
embedder - Construct the
Embedderfor a given tier. ReturnsNonefor the keyword tier (no embedder requested) and on load failure (caller degrades to keyword fallback). On failure the diagnostic is emitted viatracing::error!so operators see it injournalctl. - build_
router - Compose the production HTTP router. Thin wrapper around
crate::build_router(the W3-vintage source of truth for the route table).daemon_runtime::build_routerexists so test code in this module can build the router without namingcrate::build_routerdirectly, and so future router-composition logic (e.g. middleware reorder, custom layers) lives in one place. - build_
vector_ index - Build the in-memory
VectorIndexfromconn. Whenembedder_presentis false, returnsNone(the keyword-only path doesn’t need an index). When the embedder is present but the DB is empty (or query errors), returnsSome(VectorIndex::empty())so write paths can populate it in-place. - is_
write_ command - Returns true if
cmdis a write-class subcommand. The post-run WAL checkpoint inrunruns only when this returnstrue. - passphrase_
from_ file - Read the
SQLCipherpassphrase frompath. Strips a single trailing newline / CRLF; rejects an empty passphrase (post-strip) with an error; preserves all other internal whitespace. - run
- Top-level CLI dispatch. Called from
main()afterCli::parse(). - run_
curator_ daemon_ with_ primitives - Curator-daemon loop body, primitive-arg flavour for the binary.
- run_
curator_ daemon_ with_ shutdown - Run the curator daemon with a programmable shutdown.
- run_
sync_ daemon_ with_ shutdown - Run the sync-daemon main loop with a programmable shutdown.
- run_
sync_ daemon_ with_ shutdown_ using_ client - Variant of
run_sync_daemon_with_shutdownthat takes a caller-builtreqwest::Client. The productioncmd_sync_daemon()constructs an mTLS-aware client (viabuild_rustls_client_config) and threads it in here so the helper drives the same loop body the test version drives — keepingdaemon_runtimeas the single source of truth for the sync-daemon loop while preserving the production TLS contract. - serve
- Run the HTTP memory daemon. Loads TLS state, builds
AppState, spawns the GC + WAL-checkpoint loops, and binds a listener (TLS or plain HTTP). - serve_
http_ with_ shutdown - Run the HTTP daemon (plain HTTP, no TLS) with a programmable shutdown.
- serve_
http_ with_ shutdown_ future - Variant of
serve_http_with_shutdownthat takes an arbitrary shutdown future. The productionserve()needs to run a WAL checkpoint after the OS signal but before tearing down the listener; that cleanup work is awkward to express through aNotifyalone. Accepting aFuturelets the caller embed any async cleanup into the shutdown future itself, while the helper keeps thebuild_router+TcpListener::bind+axum::servebody it already owns. - spawn_
gc_ loop - Spawn the periodic GC loop. Sleeps
interval, then runsdb::gcanddb::auto_purge_archiveagainst the daemon’s shared connection. The returnedJoinHandleis owned by the caller;serve()aborts it on shutdown. - spawn_
wal_ checkpoint_ loop - Spawn the periodic WAL checkpoint loop. First checkpoint runs
interval / 2after start (staggered from the GC loop to avoid lock-contention bursts on cold start), then on a fixed cadence. - sync_
cycle_ once - Run a single sync cycle against one peer — pull then push.