Introducing a new way to review your PRs.

Read the announcement

jdx / fnox

encrypted/remote secret manager

View on GitHub

27 documents · Updated Nov 23, 2026

docs / daemon-client-protocol

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:

  • None asks 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.

ResultMeaningCaller direction
HitEvery requested value is cached and the document passed validationUse the document
MissA key is not cached or a lease is neededRun the CLI environment request
RejectedThe request names an unknown or non-injectable keyReport the same request error
DisabledThe effective project setting or request environment disables the daemonUse direct CLI resolution
AbsentNo daemon serves the socket, or the platform has no supportUse the CLI path, which may start one
VersionMismatchThe peer rejects the protocol or closes without a replyUse the CLI path
UnavailableTransport, timeout, decoding, daemon, or protocol failureTreat 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

TriggerConsequenceGuard
The caller builds the socket key from a different environment than the requestIt can select the wrong daemon or silently turn a valid cache into a missBuild the socket key, runtime path, and request from the same input environment
A daemon returns a document with the wrong schema, scope, or key setThe caller could apply data for the wrong requestValidate the document before returning Hit
A request reaches a peer owned by another userLocal secret data could cross a user boundaryCheck peer credentials before writing and reject the peer on both sides
A response line is too large, malformed, or incompleteThe call cannot safely identify a valid resultBound one newline-delimited reply and classify size, decode, and empty-response errors
A cache miss is treated as a resolved documentThe caller may skip provider resolution and run with incomplete environment dataPreserve Miss and fall back to fnox env --json
FNOX_DAEMON=off is checked after connectingA disabled request still performs daemon I/OReturn Disabled before opening the socket

Key files

FileWhy to read it
crates/…/client.rsDefines the read-only client contract, environment request, outcomes, and document validation.
crates/…/path.rsExplains how profiles, settings, runtime paths, and wire version select a daemon socket.
crates/…/wire.rsDefines the newline-delimited JSON requests, responses, framing limit, and call behavior.
crates/…/document.rsDefines the environment document and its schema and scope rules.
crates/…/error.rsDefines low-level call failures and missing-socket detection.
crates/…/peer.rsDefines the same-user socket peer check.
crates/…/fake_daemon.rsShows the isolated scripted-daemon contract and client failure coverage.
tests/daemon_resolve_env.rsShows compatibility between the client and a real daemon and CLI-produced documents.
src/daemon.rsShows the server-side request processing that gives the client protocol its meaning.