Daemon client protocol
The daemon client is a small read-only bridge between a process manager and a running fnox daemon. It finds the daemon for one resolution context, sends one bounded request over a local Unix socket, and returns either a cache-backed environment document or a result that tells the caller to use the normal CLI path.
Client boundary
The client gives external callers a narrow contract for using daemon state without starting a daemon or contacting a secret provider. It does not write values back to the daemon, so a miss remains the caller's responsibility to resolve through fnox env --json.
The client uses only the environment passed in the request. This matters because that environment affects both daemon selection and the cache result. Its public outcome separates a usable cache hit from a miss, a policy rejection, a disabled daemon, an absent daemon, a protocol mismatch, and another unavailable result. crates/…/client.rs• L12-21 crates/…/client.rs• L61-81
References: crates/…/lib.rs• L1-10 crates/…/README.md• L11-24
Peer discovery
Peer discovery maps the caller's effective resolution settings to one user-scoped socket. The client normalizes profiles and includes the profile, default behavior, missing-value policy, age key path, wire version, and runtime location in that choice. This prevents requests with different settings from sharing a cache by accident.
The runtime location uses XDG_RUNTIME_DIR when available and otherwise a user-specific temporary path. The client only computes and probes the path. It does not create the runtime directory or start a missing daemon. The daemon side can scan the same runtime area when an operation must address all running contexts.
The client and daemon verify the peer's user identity before exchanging data. The supported Unix platforms are Linux, macOS, FreeBSD, and OpenBSD; other platforms report no daemon availability.
References: crates/…/path.rs• L42-143 crates/…/path.rs• L170-207 crates/…/path.rs• L269-323 crates/…/peer.rs• L7-15 crates/…/lib.rs• L57-77
Request flow
The client sends the same context that the CLI fallback would use. A request carries the working directory, config path, normalized profile, resolution flags, requested scope or keys, and the complete input environment. This lets the daemon load the right project and apply the caller's view of settings instead of relying on its own process state.
The request flow turns a local cache lookup into a clear caller decision.
flowchart TD
A[Caller builds context] --> B[Client selects socket]
B --> C{Daemon available?}
C -- no --> D[Absent]
C -- yes --> E[Peer check]
E --> F[Send one JSON request]
F --> G{Daemon response}
G -- cached document --> H[Validate and return Hit]
G -- uncached keys --> I[Return Miss]
G -- policy or config rejection --> J[Return Rejected or Disabled]
G -- protocol or transport problem --> K[Return VersionMismatch or Unavailable]
resolve_env is a cache-only request for the environment of an execution scope or an interactive shell scope. A hit returns the document that the CLI would print. The caller applies its actions in order: remove inherited names, set values, then write file-backed values. A miss covers uncached keys and lease work because those paths must not run through this read-only client.
References: crates/…/client.rs• L33-47 crates/…/client.rs• L127-180 crates/…/document.rs• L113-134
Profile and document requests
The client protocol has two request families. resolve_env serves consumers that need the final environment document, while the daemon's batch and single-key resolution requests serve fnox's own command paths. All request families carry the context needed to make resolution consistent with the originating command.
The environment request has two important key meanings:
Noneasks for every key in the selected scope.Some(&[])asks for no keys and is distinct from an empty CLI key expression.
The returned environment document has schema version 1. It identifies the scope and profile and separates values to set, files to create, names to remove, unresolved names, and leases. Secret values are redacted from debug output, and request environments are represented only by their count.
The client validates a successful document before exposing it. The schema and scope must match the request, and a restricted key request must not receive a key outside that request. This turns an incorrect but well-formed reply into an unavailable protocol error rather than a false cache hit.
References: crates/…/wire.rs• L74-142 crates/…/document.rs• L13-24 crates/…/document.rs• L113-159 crates/…/client.rs• L192-211
Wire protocol
The wire protocol is version 6 newline-delimited JSON. Each connection carries one request line and one response line. The shared request and response types define operations such as environment lookup, batch and single-key resolution, cache writes, status, clearing, shutdown, and version handshake.
The protocol uses a bounded line size of 16 MiB. The client checks the peer before it writes, sets read and write limits for its normal calls, and rejects oversized or malformed replies. The daemon uses the same line format and limit so both sides agree on framing.
Protocol compatibility is explicit. A hello call reports the daemon protocol range, fnox version, and process ID. A daemon that cannot accept the request returns its supported range. The wire version also participates in socket selection, so a client normally does not reach a daemon with an incompatible format.
References: crates/…/wire.rs• L1-21 crates/…/wire.rs• L56-187 crates/…/wire.rs• L341-413 crates/…/lib.rs• L57-67 src/daemon.rs• L701-750
Error handling
Error handling preserves the difference between “there is no usable cached answer” and “the client contract is broken.” The high-level environment API maps low-level call results into outcomes that guide the caller's fallback behavior.
| Result | Meaning | Caller direction |
|---|---|---|
Hit | Every requested value is cached and the document passed validation | Use the document |
Miss | A key is not cached or a lease is needed | Run the CLI environment request |
Rejected | The request names an unknown or non-injectable key | Report the same request error |
Disabled | The effective project setting or request environment disables the daemon | Use direct CLI resolution |
Absent | No daemon serves the socket, or the platform has no support | Use the CLI path, which may start one |
VersionMismatch | The peer rejects the protocol or closes without a reply | Use the CLI path |
Unavailable | Transport, timeout, decoding, daemon, or protocol failure | Treat as a miss and use the CLI path |
FNOX_DAEMON=off is handled before socket I/O and returns Disabled. A missing or refused socket becomes Absent for environment lookup. A timeout, oversized line, bad JSON, wrong response shape, rejected peer, or invalid document becomes Unavailable; an empty response is treated as a possible older or incompatible daemon and becomes VersionMismatch.
References: crates/…/client.rs• L131-180 crates/…/error.rs• L7-43 crates/…/error.rs• L46-79 crates/…/README.md• L77-93
Fake-daemon boundary
The fake-daemon tests define the client boundary without involving provider logic. A temporary Unix listener accepts one connection, reads one request line, and sends a scripted reply or failure. This makes the request shape and every client-side outcome testable as a protocol contract.
The boundary tests cover the cases that callers must rely on: correct context and key transmission, document validation, cache misses, policy rejection, disabled mode without I/O, protocol negotiation, absent and stale sockets, timeouts, size limits, malformed JSON, unexpected replies, daemon errors, and the hello response. A real-daemon integration test then checks that a document produced by the CLI can be read back as the same document by the client.
This split keeps unit tests focused on the wire boundary and integration tests focused on compatibility with a live daemon. It also shows that a fake daemon should send one newline-terminated JSON response and should not need to understand provider behavior.
References: crates/…/fake_daemon.rs• L13-74 crates/…/fake_daemon.rs• L97-161 crates/…/fake_daemon.rs• L163-331 crates/…/fake_daemon.rs• L333-363 tests/daemon_resolve_env.rs• L137-215
Change checklist
When these files or sections are changed, remember to consider these:
- Keep socket-key inputs aligned with the context fields sent in requests. A mismatch can route a request to a daemon with a different cache context.
- Keep the wire version, supported protocol range, socket naming, and request or response variants aligned. A stale daemon must fail as a mismatch rather than return a false hit.
- Keep the environment sent to the daemon identical to the CLI fallback environment. Cache fingerprints and global config selection depend on it.
- Keep environment document schema, scope, key filtering, and application order aligned between the client and all document consumers.
- Keep every new response shape covered by the high-level outcome mapping and by the scripted fake-daemon boundary.
- Keep debug formatting redacted when request fields or response fields gain new environment or secret data.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| The caller builds the socket key from a different environment than the request | It can select the wrong daemon or silently turn a valid cache into a miss | Build the socket key, runtime path, and request from the same input environment |
| A daemon returns a document with the wrong schema, scope, or key set | The caller could apply data for the wrong request | Validate the document before returning Hit |
| A request reaches a peer owned by another user | Local secret data could cross a user boundary | Check peer credentials before writing and reject the peer on both sides |
| A response line is too large, malformed, or incomplete | The call cannot safely identify a valid result | Bound one newline-delimited reply and classify size, decode, and empty-response errors |
| A cache miss is treated as a resolved document | The caller may skip provider resolution and run with incomplete environment data | Preserve Miss and fall back to fnox env --json |
FNOX_DAEMON=off is checked after connecting | A disabled request still performs daemon I/O | Return Disabled before opening the socket |
Key files
| File | Why to read it |
|---|---|
| crates/…/client.rs | Defines the read-only client contract, environment request, outcomes, and document validation. |
| crates/…/path.rs | Explains how profiles, settings, runtime paths, and wire version select a daemon socket. |
| crates/…/wire.rs | Defines the newline-delimited JSON requests, responses, framing limit, and call behavior. |
| crates/…/document.rs | Defines the environment document and its schema and scope rules. |
| crates/…/error.rs | Defines low-level call failures and missing-socket detection. |
| crates/…/peer.rs | Defines the same-user socket peer check. |
| crates/…/fake_daemon.rs | Shows the isolated scripted-daemon contract and client failure coverage. |
| tests/daemon_resolve_env.rs | Shows compatibility between the client and a real daemon and CLI-produced documents. |
| src/daemon.rs | Shows the server-side request processing that gives the client protocol its meaning. |