Repository navigation
Closed
Labels
pro-boundaryPro/core boundary: Pro code moves to nodespace-sync (ADR-081..085)
Description
Summary
Core's side of ADR-084 §4.1 and §4.2 (proposed):
- A single-instance lock in the daemon library. Add
nodespace_daemon::single_instance. It takes an exclusive advisory lock on the lock file beside the daemon's socket: the socket path with the extensionlock. For the default sockets that gives~/.nodespace/daemon.lockanddaemon-dev.lock. Both daemons call this library function. nodespacedtakes the lock before anything else. It does so before it loads the registry, opens a database, loads the model, or deletes and binds the socket. If another daemon holds the lock, it logs the holder's pid (if one was recorded), writes nothing, and exits 0.- One exported launcher argument list. Add
nodespace_proto::socket::LAUNCHER_ARGS, today["--tray"], and re-export it asnodespace_daemon::LAUNCHER_ARGS.- The app's launchers pass exactly that list.
- Today two literals are kept in step by hand: the daemon's
TRAY_FLAGand the app'sDAEMON_TRAY_FLAG. Both become the one shared constant. - A community-daemon test asserts that the daemon accepts every entry. The Pro daemon's tests parse the same list (ADR-084 §4.2 item 1).
- Uninstall cleans up locks.
nodespace uninstallalso removes stale lock files next to the sockets it removes.
Context
- Nothing stops a second daemon on Unix today. Both
serve_headless(1236) andserve_grpc(1315) delete an existing socket before binding. A second daemon therefore silently takes over the socket while the first keeps its databases open. Two ways this happens:- the
nodespace-cliformula'sbrew servicesjob, which useskeep_alive true(scripts/update-homebrew-formula.ts330-340); - a daemon started by hand.
- the
- Windows gets no lock. Its named pipe already admits only one server, through
first_pipe_instance(true). - Why the lock sits beside the socket, not at a fixed path. Isolated daemons that set their own
NODESPACED_SOCKETthen never contend with the user's daemon. That coverssrc-tauri/test-support, the e2edaemon-harness.ts,packages/daemon/tests/itand the two-window demo. - Exiting 0 is deliberate. A
KeepAlive { SuccessfulExit = false }registration then leaves the daemon stopped. A Homebrew service restarts it on its throttle interval, and every restart exits the same way. ADR-084 §4.1 accepts that. - How to take the lock. The toolchain is 1.95, so use
std::fs::File::try_lock(stable since 1.89).- On Unix it is
flock, and it returnsErr(TryLockError::WouldBlock)when the lock is held. - The OS releases the lock when the process exits, so a crash leaves no stale lock.
- It is advisory only, not a security boundary (ADR-052).
- On Unix it is
- When to take it. Take the lock before
stdio_log_rotation::spawn()and before the runtime exists. A daemon that loses the lock must not touch the log file the two daemons share. - Contract test. It uses Remove the pro feature and --edition from nodespaced and reject unknown arguments #3348's
first_unrecognized_arg.
Scope
packages/proto/src/socket.rs
- Add
pub const TRAY_FLAG: &str = "--tray";andpub const LAUNCHER_ARGS: [&str; 1] = [TRAY_FLAG];. - Their docs say:
- these are the arguments every launcher passes to the daemon it registers;
- a daemon registered under core's service identity must accept each one;
- adding an argument is an extension-API change (ADR-082 §6).
- In the module doc, add the lock rule:
<socket stem>.lock, beside the socket.
packages/daemon
- New
src/single_instance.rs(#[cfg(unix)]). Register it withpub mod single_instance;insrc/lib.rs, and addpub use nodespace_proto::socket::LAUNCHER_ARGS;there too.pub fn lock_path_for(socket: &Path) -> PathBuf, which returnssocket.with_extension("lock").pub enum Acquire { Held(InstanceLock), HeldByAnother { holder_pid: Option<u32> } }.pub struct InstanceLock, which holds the openFile. Dropping it releases the lock.pub fn acquire(socket: &Path) -> Result<Acquire>, which is synchronous. In order, it:- creates the parent directory with the same owner-only rule as
create_dir_owner_only(src/lib.rs104-138), factored into a sync helper that both use; - opens or creates the lock file with mode 0o600, without truncating it;
- calls
try_lock(); - on success, truncates the file and writes this process's pid;
- on
WouldBlock, reads the pid (best effort) and returnsHeldByAnother; - returns any other error.
- creates the parent directory with the same owner-only rule as
src/main.rsmain(). After argument handling and tracing init, and beforestdio_log_rotation::spawn()and the runtime, add a#[cfg(unix)]block that takes the lock:- On
Held(l), keeplas_instance_lockfor the rest ofmain. - On
HeldByAnother { holder_pid }, log atinfo!that another daemon already serves this socket, and returnOk(()), which exits 0. - Replace
const TRAY_FLAG(1130-1134) with the proto constant.ACCEPTED_ARGSkeeps listing what the daemon handles. - Add the contract test:
first_unrecognized_argreturnsNonefor an argv built fromLAUNCHER_ARGS. sync mirrors this test.
- On
packages/desktop-app/src-tauri/src/daemon_setup.rs
-
Delete
DAEMON_TRAY_FLAG(107-115). Each launcher passes every entry ofLAUNCHER_ARGS:write_plist: one<string>per entry.write_systemd_service:ExecStartjoins the entries with spaces.spawn_daemon_windows:.args(LAUNCHER_ARGS).register_autorun_windows: the entries, joined, after the quoted binary.
The output stays byte-identical while the list is
["--tray"]. -
plist_opts_the_daemon_into_tray_modeandpkg_plist_opts_the_daemon_into_tray_modeassert each entry ofLAUNCHER_ARGS. The second test then also guardsscripts/pkg-resources/app.nodespace.daemon.plistagainst drift.
packages/cli/src/commands/uninstall.rs
- In
remove_sock, also removelock_path_for(dir.join(name))for each socket name. The CLI already depends onnodespace-daemon.
Out of scope
- The product check (Check the running daemon's executable against the app's DaemonProfile at startup #3353) and the notices (Tell the user when another NodeSpace product's daemon is running or registered #3356).
- The Pro daemon's side of the contract (sync).
Steps
- Run the startup sequence, and assign the issue at pickup.
- Add the proto constants, then
single_instanceand its unit tests. - Wire the lock into
main(), replace the tray constant, and add the contract test. - Switch the app's launchers to
LAUNCHER_ARGS. - Update
remove_sock. - Add the integration test.
- Run
bun run scripts/check-pro-boundary.tsand pasteBASELINES. - Run
bun run test:changedandbun run quality:fix, push, and open the PR. Code comments must not cite GitHub issue numbers.
Acceptance criteria
- When a second
nodespacedstarts on the socket a running one serves, it exits 0 within 10 s and never deletes or binds the socket. The first keeps serving. - Daemons on different sockets never block each other.
-
LAUNCHER_ARGSis exported from bothnodespace_proto::socketandnodespace_daemon. - No other
--trayliteral remains inpackages/daemon/srcorpackages/desktop-app/src-tauri/src:git grep -n -F -e '"--tray"' -- packages/daemon/src packages/desktop-app/src-tauri/srcshows only the proto definition's re-use sites. - The community plist, systemd unit and Windows command lines are byte-identical to before.
-
bun run scripts/check-pro-boundary.tspasses, withBASELINESpasted. No change is expected.
Tests and verification
- Unit tests for
single_instance:- acquiring A gives
Held; - acquiring B on the same path gives
HeldByAnother, withholder_pidequal to this process's pid (flock locks belong to the open file description, so two opens in one process conflict); - after dropping A, acquiring C gives
Held; lock_path_formapsdaemon.socktodaemon.lock, anddaemon-dev.socktodaemon-dev.lock;- the parent directory is created with mode 0o700.
- acquiring A gives
- Integration test: add
tests/it/single_instance.rsand register it intests/it/main.rs.- Spawn daemon A with a FIFO model path and a tempdir socket, following
sigterm_during_model_load.rs, and wait for the socket. - Spawn B with the same environment. Assert that B exits 0 within 10 s, A is still alive, and the socket still accepts a connection.
- Send SIGTERM to A.
- Spawn daemon A with a FIFO model path and a tempdir socket, following
- Mutation check: remove the lock step, and the integration test must fail.
- Commands:
bun run test:changedcargo clippy -p nodespace-proto -p nodespace-daemon -p nodespace-cli -p nodespace-app --all-targets -- -D warnings -D clippy::unused-async.tools/bin/cargo-nextest nextest run -p nodespace-daemon --test it single_instance::
Dependencies
- Remove the pro feature and --edition from nodespaced and reject unknown arguments #3348:
first_unrecognized_arg, and the settled argv handling inmain(). - NodeSpaceAI/nodespace-docs#94 (ADR-084): ADR-084 §4.1 and §4.2 (proposed).
Risks and rollback
- Two dev daemons on one socket. A developer running dev daemons from two worktrees on the default dev socket now sees the second exit 0. Until now, the two fought over the socket.
- Log noise from Homebrew. A
brew servicesdaemon that loses the lock is restarted on every throttle interval. - Network home directories. flock over NFS is best effort.
- Rollback: revert the PR.
Plan key: C8.9 (pro-boundary DAG).
🤖 Generated with Claude Code
Metadata
Metadata
Assignees
Labels
pro-boundaryPro/core boundary: Pro code moves to nodespace-sync (ADR-081..085)
Activity
mstomar125 commentedon Sep 30, 2026
Completed in #3375 (merged). Auto-close did not fire; closing manually.
mstomar125 commentedon Sep 30, 2026
Correction to the closing comment above: this issue was completed in #3388 (merged), not the PR named there.