docs/cmd/ez-agent-service.md

ez-agent-service

The long-lived EzAgent daemon: it owns the local Unix-socket service, persistent session startup, and external capability-process lifecycle. For the user-facing client and TUI, see ez-agent; for service internals, see pkg/service; for the wire contract, see the JSONL API.

What does this executable own?

RoleCode owner
Executablecmd/ez-agent-service/main.go initializes logging, then calls service.RunDaemon(nil) to audit and load global config before opening storage.
Startup and shutdownpkg/service/daemon.go owns readiness probing, socket binding, startup handoff, and stop requests.
Ready serverpkg/service/server.go accepts peers, completes the service hello exchange, dispatches operations, and coordinates shutdown.
Child servicespkg/service/process_pool.go and service_process.go spawn and reap configured external services.

The executable accepts no service-specific command-line flags; startup behavior is controlled by configuration and environment paths.

When is it ready?

Startup first creates and locks the state directory, binds the Unix socket, and runs a bootstrap listener. Until durable maintenance finishes, that listener answers hello with Ready: false and closes the connection. The daemon then performs durable maintenance in order: open the session store → clean incomplete leaves → mark interrupted streaming turns → optional vacuum → reconcile stored content → hand the same listener to the ready server. Warnings from maintenance may be reported without preventing readiness. Stop requests are only honored by the ready server; StopDaemonContext refuses a bootstrap daemon.

Do not move normal server startup ahead of this maintenance or socket binding: clients rely on the phase-specific not-ready response, stable listener handoff, and cleanup of visible lifecycle resources on failure or interruption.

Where are its local resources?

  • The runtime socket is $XDG_RUNTIME_DIR/ez-agent/ez-agent.sock.
  • The state directory ($XDG_STATE_HOME/ez-agent) holds ez-agent.db and is guarded by a lock that permits one daemon per state directory; it is acquired before the database opens or the socket binds.
  • Logging uses EZ_AGENT_LOG_LEVEL, EZ_AGENT_LOG_FORMAT, and EZ_AGENT_LOG_FILE. A usable configured file is opened append-only; otherwise logging goes to stderr. Startup reports the actual destination as file or stderr, the process pid, and the usable log_file, which is empty for stderr. log_file_configured distinguishes an unset filename from fallback. If the configured file cannot be opened, an error with fixed reason open_failed reports fallback to stderr without exposing the rejected path or raw OS error. Startup info remains subject to the configured log level.
  • Client auto-launch captures daemon stdout/stderr in ez-agent-service.log under the state directory. This capture is truncated on each auto-launch; save relevant logs before restarting. A directly launched or supervised service writes stderr wherever its launcher routes it. Redirecting stderr to /dev/null discards those records; it does not create the client launcher's capture file. Check process PID and launch configuration when locating logs. Incident tombstones have their own storage and may survive even when normal logging was discarded.

Configuration paths, layering, and service startup settings belong in pkg/service/config; persistence and its startup maintenance belong in pkg/service/session.

RunDaemon initializes the incident-only recorder before state locking and DB open. Slow startup and orderly shutdown phases can therefore leave safe evidence even while the database is unavailable. See service wait thresholds for budgets and acknowledgement semantics, and incident storage for path/permission requirements and bounded best-effort delivery. Healthy execution creates no additional audit log.

How do configured services participate?

The daemon brokers agents and external tools, plugins, and macros. Lazy JSONL routing requires a Direct: true service. OnStart services start after socket readiness; non-direct background services do not advertise configured capabilities. Project configuration is localized later for a client/leaf working directory, separately from the global configuration loaded at daemon startup. See pkg/service/config for its layering rules.

An optional Hub can be an explicit non-direct on_start background service. See deployment and supervision and the existing-schema example. The process pool starts it after application socket readiness but does not wait for Hub readiness or automatically restart it. Pool shutdown kills background children; use the documented exact-child SIGTERM sequence or independent supervision when graceful Hub cleanup is required. Hub never starts the application daemon.

A service path is resolved as ~/~/... from the daemon user's home, retained when absolute, or otherwise located beside the physical daemon executable—not through PATH or the project working directory. A relative path may not escape the daemon executable's directory; a ../-style path fails resolution with an installation-directory error. Ruby: true uses the sibling bundled Ruby launcher. See pkg/service and ez-agent-builtin for configuration and capabilities.

Where does the detail live?