Skip to main content

Module ensure_up

Module ensure_up 

Source
Expand description

Background-process bootstrapper for the MCP path.

Post-pair, an agent shouldn’t have to ask the user “start the daemon?” — wire_pair_confirm invokes ensure_daemon_running + ensure_notify_running so push/pull and OS toasts are already armed by the time the agent surfaces “paired ✓” back to chat.

§Idempotency

Each subcommand writes its pid record to $WIRE_HOME/state/wire/<name>.pid on spawn. The next call reads the record and skips spawning if the pid is still alive. Stale pid files (process died) are silently overwritten.

§Pid-file shape (P0.4, 0.5.11)

The pid file used to be a raw integer (12345\n). Today’s debug surfaced a process running an OLD binary text in memory under a current symlink, and wire status had no way to detect that. The pid file is now a versioned JSON record:

{
  "schema": "wire-daemon-pid-v1",
  "pid": 12345,
  "bin_path": "/usr/local/bin/wire",
  "version": "0.5.11",
  "started_at": "2026-05-16T01:23:45Z",
  "did": "did:wire:paul-mac",
  "relay_url": "https://wireup.net"
}

Readers are TOLERANT of the legacy int form for one transition cycle — read_daemon_pid falls through to raw-int parse when JSON decode fails and reports version: None so callers can degrade gracefully.

§Wait-until-alive

On spawn, we wait briefly for the child to be alive before persisting the pid file. A concurrent CLI seeing the file pointing at a not-yet-bound PID is the “daemon reports running but can’t accept connections” race spark flagged in our P0.4 design call.

§Detachment (Unix)

Spawned with stdio nulled. Since wire mcp runs without a controlling TTY (it’s a stdio MCP server, not a login shell), the spawned children inherit no TTY → no SIGHUP arrives when the parent exits, so they survive a Claude Code restart cycle. PIDs are reaped by init.

Worst case: a child dies; the next wire_pair_confirm call respawns it. No data is lost (outbox/inbox is on disk, content-addressed dedupe).

Structs§

DaemonPid
Versioned daemon pid record — the JSON form written by 0.5.11+.

Enums§

PidRecord
Result of reading a pid file. Distinguishes legacy-int (no metadata) from JSON (full metadata) so callers can degrade gracefully.

Constants§

DAEMON_PID_SCHEMA
Schema string written into every JSON pid file. Bumped if the pid-file shape ever changes incompatibly. Readers warn on unknown schema.

Functions§

daemon_version_mismatch
Check the running daemon’s version against the CLI’s CARGO_PKG_VERSION. Returns Some(stale_version) if they disagree, None if they match (or no daemon, or legacy-int pidfile without version info).
ensure_daemon_running
Ensure a wire daemon --interval 5 process is alive. Returns Ok(true) if a fresh process was spawned, Ok(false) if one was already running.
ensure_notify_running
Ensure a wire notify --interval 2 process is alive (OS toasts on every new verified inbox event). Returns true if newly spawned.
read_pid_record
Read a pid file, tolerating both JSON and legacy-int forms. Never panics — corrupt input becomes PidRecord::Corrupt.