# kcode-k1-daemon-lib
`kcode-k1-daemon-lib` is the library-only composition root for the private K1 loopback daemon.
## Public API
The public entry point is `pub fn run(k1_root: PathBuf) -> ExitCode`.
`run`:
1. creates a multithreaded Tokio runtime;
2. prompts for the K1 Vault passphrase without echoing it;
3. opens all durable state beneath `<k1_root>/state`;
4. opens one shared Codex app-server process;
5. binds `127.0.0.1:4450`;
6. prints one JSON readiness record to stdout;
7. serves until SIGINT or SIGTERM; and
8. performs graceful listener shutdown.
Startup failures return exit code 1. Provider diagnostics are not printed through safe startup errors.
## HTTP boundary
The daemon advertises:
- public origin: `http://localhost:4450`;
- authority: `localhost:4450`;
- protocol: `K1-HTTP-1`;
- configuration: `GET /config.json`.
Every request must carry exactly the expected authority. Authenticated routes remain beneath the existing signed K1 HTTP boundary. Error responses are contextualized without discarding their stable error code or safe child message. Responses generated by this crate use `Cache-Control: no-store`.
Version 0.7.5 preserves the existing Accounts, People, Persons, Audio, Terms, replay, invite-stock, and fallback route composition. It does not add Chat HTTP routes.
## Durable state
The daemon opens process-owned Ordering and Peering handles, then the existing Vault, Persons, Invites, Accounts, Users, Groups, Access Profiles, Objects, Audio Classification, Access, replay, and related composition.
Chat backend state is added beneath:
- `<k1_root>/state/chat/history`
- `<k1_root>/state/chat/persistence`
The process retains `K1ChatService` for the entire serving lifetime. The service owns AccessChat composition, lazy durable chat actors, and current authorization checks. Failure to open Chat state aborts startup before the listener is bound and preserves the safe child error under daemon startup context.
## Vault and providers
The daemon reads `gemini-api-key` from the encrypted K1 Vault. The passphrase and provider secret are held through protected secret types.
The Codex executable is selected from `K1_CODEX_EXECUTABLE`, defaulting to `codex-safe`. The current working directory is supplied to the adapter.
Exactly one `Adapter::open` call starts one Codex app-server process. `Adapter::with_config` derives a second profile on that same process:
- Audio: model `gpt-5.6-terra`, reasoning effort `medium`, empty base instructions, and only `record_speaker_labels` plus `record_speaker_analysis`.
- Chat: model `gpt-5.6-sol`, reasoning effort `xhigh`, empty base instructions, and only `CurrentTime`.
Conversation keys remain bound to their exact profile until successful close, so Audio and Chat multiplex through one process without sharing model, effort, instructions, or dynamic-tool allowlists.
The existing Gemini 3.1 Pro Audio path remains unchanged.
## Audio
The daemon preserves the existing two-model Audio composition:
1. Gemini 3.1 Pro
2. GPT-5.6 Terra through the shared Codex process
The existing Audio HTTP routes, authorization, persistence, classification behavior, speaker tools, and model ordering remain unchanged.
## Chat backend boundary
Version 0.7.5 composes and retains the non-HTTP Chat backend only. It does not expose an external way to create a conversation, submit a message, poll a snapshot, restart, rename, or close. Those routes belong to a later HTTP-adapter release.
Accordingly, package checks prove configuration and composition, but this release alone cannot prove a live Chat turn through daemon ingress.
## Startup and shutdown
The listener is bound only after durable subsystems, providers, replay state, invite stock, and Chat service construction succeed. The daemon emits a slow-start warning when startup exceeds the fixed bound.
On SIGINT or SIGTERM, Axum performs graceful shutdown. The retained Chat service and Vault are dropped only after serving ends.
## Version history
### 0.7.5
- Selects `kcode-k1-codex-adapter` 0.4.1.
- Selects `kcode-k1-chat-service` 0.1.0.
- Selects `kcode-k1-http-persons` 0.1.1.
- Selects `kcode-speaker-v3-analysis` 0.3.2.
- Selects `kcode-speaker-v3-terra-analysis` 0.2.1.
- Uses compatible, non-exact lower bounds for all internal `kcode-*` dependencies.
- Opens one Codex process and derives isolated Audio Terra/medium and Chat Sol/xhigh profiles.
- Opens and retains the durable authorization-aware Chat backend beneath `state/chat`.
- Adds no Chat HTTP route.
- Package-level checks do not constitute live daemon, provider, Audio, or cold-restart proof.
### 0.7.4
Selected the current Persons HTTP adapter and related Access Persons composition while preserving existing routes and startup behavior.
### 0.7.3
Integrated the current Audio HTTP and Access Full Audio releases.
### 0.7.2
Added the earlier Audio adapter candidate and retained the daemon’s loopback HTTP composition.