fix(daemon): bind the unix socket outside the data dir at a short hashed path (#262)

Merged
KJ
3mo ago
+307-50

Closes #262.

The problem

<data-dir>/relayflowd.sock was the naive location, and it fails hard when the working directory pushes the full socket path past the OS SUN_LEN limit (~104 bytes on macOS, ~108 on Linux). A prospect cloning an example into a nested tree hits Error: bind socket ...: path must be shorter than SUN_LEN before any step can run — the first thing anyone sees on a canonical flows run smoke.

The change

The daemon socket now binds at a short stable path under \$XDG_RUNTIME_DIR / \$TMPDIR / std::env::temp_dir(), keyed by a 12-hex-char SHA-256 of the absolute data-dir path. Both sides derive the same path from the same input:

  • kernel/relayflowd/src/socket_path.rs::derive_socket_path (Rust)
  • packages/sdk/src/daemon-connection.ts::socketPathFor (JS)

Both use lexical absolute-ization (std::path::absolute / path.resolve) — never canonicalize/realpath — so macOS's /var vs /private/var cannot cause a divergence. The DAEMON-LIFECYCLE.md §2 step-3 anti-hijack check is preserved: the daemon writes the derived path into connection.json, and the CLI still refuses attach when the file names anything other than what `socketPathFor(dataDir)` recomputes.

The lock (relayflowd.lock), journal (relayflowd.sqlite3), log (relayflowd.log), and connection.json all still live in the data dir. Only the socket moves.

Upgrade note (backward compat)

A running old daemon binds at <data-dir>/relayflowd.sock and writes that path into connection.json. A new CLI recomputes the derived path, sees the file naming the legacy socket, and treats it as `socket_path_mismatch` — refuses to attach. Because the old daemon holds the flock on <data-dir>/relayflowd.lock, the new CLI cannot spawn a successor either.

Kill the old daemon before upgrading:

pkill relayflowd  # or: kill \$(jq -r .pid <data-dir>/connection.json)

The flock releases on process death and the new CLI then spawns a fresh daemon cleanly. This is the sharpest signal I could design for this transition — bumping PROTOCOL_VERSION would be more invasive and cause the same practical outcome.

Tests

New regression (§262): deep_data_dir_still_binds pads the data-dir path to >200 bytes, spawns the daemon, connects via the published socket, asserts the socket path is under 104 bytes. The property is now proven by construction, not by accident.

New unit tests in socket_path module:

  • Same data-dir → same socket (determinism)
  • Different data-dirs → different sockets (distinctness)
  • Deep data-dir → short socket (invariant)
  • Relative and absolute forms of the same data-dir → same socket (matches JS path.resolve)

Verified passing:

  • cargo test --package relayflowd: 40 root-level unit tests + 6 daemon_lifecycle tests + all crash_resume tests + everything else in the workspace. Anti-hijack (test 4), successor-restart (test 5), and clean-shutdown (test 2) invariants all still hold.
  • npx vitest tests/daemon-lifecycle.test.ts: 42/42 pass.
  • npx vitest tests/daemon-lifecycle-live.test.ts: 9/9 pass — these spawn a real Rust daemon and connect through the derived socket, so this proves the JS and Rust derivations produce identical paths on real machines.

Not touched

  • SOCKET_FILE = 'relayflowd.sock' is left as an unused export in daemon-connection.ts. The constant no longer describes the full socket filename but nothing internal reads it; removing is a follow-up since it is a public export.
  • DAEMON-LIFECYCLE.md §1 path statement is updated to describe the new layout. §2/§3 lifecycle prose is unchanged — everything they say about the file/lock still holds because those still live in the data dir.

[!NOTE]
Medium Risk
Changes where every client connects to relayflowd and how lifecycle residue is cleaned up; behavior is tightly coupled across Rust, SDK, and stubs, with a deliberate break when mixing old daemons and new CLIs.

Overview
Fixes #262 by moving the daemon’s Unix socket out of <data-dir>/relayflowd.sock so deep project paths no longer blow past SUN_LEN and fail at bind(2) before any run can start.

The socket now binds under $XDG_RUNTIME_DIR / $TMPDIR (with the same fallback order on Rust and Node) at relayflowd-<12-hex>.sock, where the hash is the first 12 hex chars of SHA-256 of the lexically absolute data dir (derive_socket_path / socketPathFor). Lock, journal, log, and connection.json stay in the data dir; connection.json still records the real socket path so §2 anti-hijack (recompute vs file) is unchanged. remove_residue and publish take an explicit socket path; server and client code paths that used to join relayflowd.sock under the data dir now call the shared derivation.

CLI entry points (run, tick runner, hn-monitor, demo) and tests/stubs were switched to socketPathFor so loopback and live tests exercise the same path the kernel uses. DAEMON-LIFECYCLE.md §1 documents the split layout. New coverage includes deep_data_dir_still_binds and unit tests on path length and relative/absolute agreement.

Upgrade: a still-running old daemon advertises the legacy in-data-dir socket; a new CLI will see socket_path_mismatch until that process is stopped and the flock is released.

Reviewed by Cursor Bugbot for commit a9ed230e40a51b04d2dd8b199bdca0bb5aaf1b63. Bugbot is set up for automated code reviews on this repo. Configure here.