Skip to content

Make the CLI's uninstall and logs commands follow NODESPACE_HOME - #3634

Merged
mstomar125 merged 4 commits into
mainfrom
issue-3612-cli-nodespace-home
Oct 5, 2026
Merged

mstomar125 merged 4 commits into
mainfrom
issue-3612-cli-nodespace-home

Conversation

@malibio

@malibio malibio commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator

Closes #3612

What changed

nodespace uninstall and nodespace logs built their paths from $HOME. Both now take the state directory from nodespace_daemon::nodespace_dir(), so NODESPACE_HOME isolates them.

  • uninstall removes <nodespace_dir>/bin and the sockets and lock files in <nodespace_dir>. The closing message prints the real data path instead of a fixed ~/.nodespace/database/.
  • logs takes its home candidate from the resolver. It already ignored the Homebrew prefixes under NODESPACE_HOME, but through its own reading of the variable.
  • nodespace_daemon::nodespace_home_override() is new: the redirected home, or None. nodespace_home() is built on it, so the variable is read in one place.

A behaviour decision to check

Under NODESPACE_HOME, uninstall now skips two steps and says so in one line:

  • stopping the daemon service and removing its registration (launchctl bootout, the LaunchAgents plist, the systemd unit);
  • removing the skill from agent harnesses.

Both live in the user's own home, not the NodeSpace home, and belong to the install there. Without this, an isolated uninstall would stop the user's real daemon. With NODESPACE_HOME unset, nothing changes.

Tests

New tests/it/home_isolation.rs runs the compiled binary with HOME at a temp "real" home holding a full install (binaries, sockets, lock files, database, log, plist, systemd unit) and NODESPACE_HOME at another:

  • uninstall leaves the real home byte-for-byte unchanged, and leaves only the database and log in the redirected one;
  • logs --path-only and logs resolve and read the redirected log;
  • with no redirected log, logs fails naming only the redirected path, with no fallback to the real home or a Homebrew prefix.

The tests set PATH to an empty directory, so no run can reach this machine's service manager.

Mutation-checked, each reverted afterwards:

Mutation Result
uninstall state directory from $HOME uninstall test fails
uninstall ignores the redirect uninstall test fails (plist and systemd unit removed)
logs state directory from $HOME both passing-path logs tests fail
logs ignores the redirect fallback test fails

bun run test:changed passes (scripts and the Rust workspace).

Notes

  • Install coexistence checks and other-product notices (ADR-084 §4.3, §5) #3606 also edits uninstall.rs, in run() and just above it. Whichever lands second needs a small rebase there.
  • Not changed here: the daemon's default socket path (default_socket_path_for in packages/daemon/src/main.rs) and the CLI's resolve_socket_path still build from $HOME, though daemon-cli-protocol.md says the socket follows NODESPACE_HOME. They agree with each other and with the app, so changing one alone would break the pairing.

🤖 Generated with Claude Code

@malibio

malibio commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator Author

Pragmatic code review: PR #3634

Review Type: Initial Review

Summary

The change does what the issue asks and is a net improvement. Both commands now take their state directory from nodespace_daemon::nodespace_dir(), the variable is read in one place, and the new integration tests pass here (3 of 3, run with nextest on home_isolation::). Nothing blocks the merge. Two points are worth fixing in this PR: how an unusable NODESPACE_HOME value is handled, and one half of the redirect decision that no test covers.

On the design choice (skip the service manager and skill removal under NODESPACE_HOME): it is correct. The launchd label app.nodespace.daemon, the plist, the systemd unit and the agent skill directories are per-user, not per-NodeSpace-home. A redirected uninstall that ran launchctl bootout would stop the user's real daemon, which is the harm the issue is about. The one-line notice says what was left in place.

Requirements Check

  • ✅ Both commands resolve their home through the shared resolver, so NODESPACE_HOME isolates them. uninstall.rs and logs.rs both call nodespace_dir(). The private home_dir() in uninstall.rs is deleted, not kept beside the new path. The only $HOME reads left in uninstall.rs are in stop_daemon, which is skipped under a redirect.
  • ✅ Tests with HOME at a temp "real" home and NODESPACE_HOME at another prove the real home is never read or modified. Mutation-checked. The byte-for-byte snapshot proves "not modified". The logs tests prove "not read" by content (isolated) and by the error text. I did not re-run the mutations; the table in the PR description is the author's. One gap is listed below: the skill-removal skip is not covered by any assertion.

Code Review Findings

🟡 Important: an unusable NODESPACE_HOME falls back to the real home, or to the working directory

📁 packages/daemon/src/lib.rs:48-50

std::env::var("NODESPACE_HOME").ok().map(PathBuf::from)
  • Not valid UTF-8: env::var returns Err, so the override is None and every caller uses the real home. logs.rs read the variable with var_os before this PR and honoured such a value, so this is a small regression there. For uninstall it means a run that set the isolation variable stops the real daemon and deletes the real bin.
  • Empty string (NODESPACE_HOME= in a script): the override is Some(""), nodespace_dir() is the relative path .nodespace, and uninstall runs remove_dir_all(".nodespace/bin") in whatever directory it was started from.

nodespace_home() had the same reading before this PR, so the resolver's behaviour is not new. What is new is that a destructive command now depends on it.

Recommendation: read with std::env::var_os, and treat an empty value as unset. It is one function, so every caller gets the fix. A unit test for each of the two cases is enough.

Principle: a variable whose job is isolation should never fall back to the thing it isolates from without saying so.

🟡 Important: the skill-removal skip and the notice line are not tested

📁 packages/cli/tests/it/home_isolation.rs:113-152, packages/cli/src/commands/uninstall.rs:27-33

The PR's mutation "uninstall ignores the redirect" is caught by the plist (macOS) or the systemd unit (Linux) disappearing. That proves the stop_daemon skip only. If a later change called report_skill_removal under a redirect, the test would still pass: the real home holds no skill files, and with an empty PATH and no sidecar beside the test binary the installer is not found at all.

Recommendation: assert the output. Under a redirect, stdout should hold the "left in place" line and none of report_skill_removal's lines ("Could not locate the skill installer", "No installed NodeSpace skills found.", "removed"). Every branch of report_skill_removal prints something, so this catches the mutation without needing an installer.

Principle: each half of a behaviour decision needs an assertion that fails when that half is reverted.

🟢 Suggestion: the default socket path still ignores NODESPACE_HOME, so a redirected uninstall cleans a directory no default socket is in

📁 packages/cli/src/lib.rs:227, packages/daemon/src/main.rs:160, packages/cli/src/commands/uninstall.rs:144-151

I confirmed the PR's note: resolve_socket_path and the daemon's default socket both build from $HOME. Under a redirect, remove_sock(&state_dir) therefore looks in <NODESPACE_HOME>/.nodespace, where a socket only exists if NODESPACED_SOCKET put it there. The integration test seeds sockets in the redirected state directory, which is a layout the product does not produce by default today.

This PR's behaviour is still the right one (the sockets in the real home belong to the real daemon), and the issue's scope is the two commands. But the issue says these were "the remaining ones", and daemon-cli-protocol.md says the socket resolves under nodespace_dir(). Neither is true yet.

Recommendation: your call whether to fold it in. If so, change the CLI, the daemon and the app's resolver together. If not, correct the sentence in daemon-cli-protocol.md so the doc matches the code.

Principle: one resolver for one concept; a doc that describes behaviour the code lacks misleads the next reader.

🟢 Suggestion: no test for the unredirected path

📁 packages/cli/tests/it/home_isolation.rs

"With NODESPACE_HOME unset, nothing changes" is stated in the PR but not tested. The same harness can show it: remove NODESPACE_HOME, keep HOME at the temp home and PATH empty, and assert that bin, the sockets, the lock files and the platform's service file are gone while the database and log remain. It stays hermetic for the same reasons the current tests are.

Principle: a refactor of a destructive command should pin the default path as well as the new one.

🟢 Suggestion: document the redirect behaviour

📁 nodespace-docs/architecture/daemon-cli-protocol.md (the NODESPACE_HOME paragraph)

Add one sentence: uninstall and logs follow NODESPACE_HOME, and a redirected uninstall leaves the service registration and agent skills alone. This is a user-visible rule that today lives only in a code comment.

Checked and found sound

  • Hermeticity: HOME is a temp directory, PATH is an empty directory, and NODESPACED_SOCKET and NODESPACE_DATABASE are removed. Even with the redirect check reverted, launchctl and systemctl cannot be found, and the plist or unit that would be deleted is the temp one. The tests cannot reach this machine's daemon or skills.
  • Flake risk: none seen. No timing, no network, no daemon; each test owns its temp directories.
  • Platform cfg: PathBuf is no longer used in uninstall.rs and its import is removed; Path is used on every platform. Command is unused on Windows, where stop_daemon is empty, but that import was there before this PR. The lock-file writes in the test are #[cfg(unix)], matching remove_sock.
  • Unset behaviour: the home now comes from dirs::home_dir() rather than $HOME alone, which only adds a fallback when $HOME is unset. nodespace_dir()? now runs before stop_daemon(), so a machine with no resolvable home fails before touching the service; that is the safer order.
  • Project standards: no compatibility code, no issue numbers in comments, no Pro-boundary wording, no lint suppression. The test file is registered as a module in tests/it/main.rs.
  • Security: no new input reaches a shell or a path outside the resolved state directory.

Recommendation

APPROVE. The two 🟡 items are small and worth doing in this PR; neither is a blocker.

@malibio malibio left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated pragmatic-code-review: APPROVE (posted as a comment — GitHub blocks self-approval). Full findings in the review comment above.

@malibio

malibio commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator Author

Address-review summary

Commit 477a51c06.

Addressed

  • 🟡 Unusable NODESPACE_HOME values. nodespace_home_override() now reads with var_os: a path that is not UTF-8 is honoured, and an empty value is unset. Unit tests cover set, unset, empty and non-UTF-8. An integration test runs uninstall with NODESPACE_HOME= from a working directory holding a .nodespace/bin and checks that directory is untouched.
  • 🟡 Skill-removal skip and notice untested. The redirected uninstall test now asserts the whole stdout, the two expected lines and nothing else. Skill removal prints on every path, so its absence is the proof.
  • 🟢 Unredirected path untested. New test: with no NODESPACE_HOME, uninstall removes the binaries, sockets, lock files and this platform's service file from the user's home, and keeps the database and log.

Mutation-checked, each reverted: dropping the empty-value filter fails the empty-NODESPACE_HOME test; calling skill removal under a redirect fails the redirected uninstall test.

bun run test:changed passes.

Skipped

  • 🟢 Default socket path under NODESPACE_HOME. The CLI, the daemon and the app's resolver each build it from $HOME and must agree, so moving it is a change across three crates and outside this PR.

Pending

  • 🟢 Docs sentence in daemon-cli-protocol.md: it goes to the docs repository directly, after this lands.

Re-Review Decision

Decision: RE-REVIEW NEEDED

Rationale: the fix changes how the shared resolver reads NODESPACE_HOME, which every daemon path depends on, and adds new tests.

@malibio

malibio commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator Author

Pragmatic code review: PR #3634

Review Type: Re-Review (scope: 92bcd904b..477a51c06)

Summary

The commit fixes both 🟡 findings from the first review and adds the unredirected-path test. The resolver change is sound for every caller inside the daemon and CLI. It leaves one thing behind: the desktop app keeps its own copy of the NODESPACE_HOME rule, and that copy now disagrees with the daemon on the two values this commit changed. It is a small fix worth folding in; it does not block the merge.

home_isolation:: passes here, 5 of 5, with nextest.

Previous Review Summary

Count
Previous recommendations 5 (2 🟡, 3 🟢)
Addressed 3
Not addressed 2 (both 🟢: one skipped with a reason, one pending in the docs repository)
New issues found 2 (1 🟡, 1 🟢)
  • ✅ 🟡 Unusable NODESPACE_HOME values. nodespace_home_override() reads var_os and filters an empty value. The logic is in a pure home_override_from, with unit tests for set, unset, empty and non-UTF-8. An integration test runs uninstall with NODESPACE_HOME= from a working directory holding a .nodespace install and checks it is unchanged.
  • ✅ 🟡 Skill-removal skip and notice untested. The redirected test now asserts the whole stdout. Every branch of report_skill_removal prints a line, so the exact match fails if it runs.
  • ✅ 🟢 Unredirected path untested. New test: binaries, sockets, lock files and this platform's service file go; the database, the log and the other platform's service file stay.
  • ⏭ 🟢 Default socket path under NODESPACE_HOME. Skipped. The reason holds: the CLI, the daemon and the app build it separately and must change together.
  • ⏳ 🟢 Docs sentence in daemon-cli-protocol.md. Pending. When it is written, it should also say that an empty NODESPACE_HOME counts as unset.

Requirements Check

  • ✅ Both commands resolve their home through the shared resolver, so NODESPACE_HOME isolates them. Unchanged from the first review; this commit only changes how the resolver reads the variable.
  • ✅ Tests with HOME at a temp "real" home and NODESPACE_HOME at another prove the real home is never read or modified. Mutation-checked. The byte-for-byte snapshot and the exact-stdout assertion cover both halves of the redirect decision. I did not re-run the mutations; the two listed in the address-review comment are the author's.

Code Review Findings

🟡 Important: the app's own copy of the NODESPACE_HOME rule now disagrees with the daemon

📁 packages/desktop-app/app-lib/src/bundled_model.rs:120-134

/// [`nodespace_home`] from this process's environment. `std::env::var`, as the
/// daemon reads it, so a value that is not valid UTF-8 counts as unset on both
/// sides.
fn nodespace_home_from_env() -> Option<PathBuf> {
    nodespace_home(std::env::var("NODESPACE_HOME").ok(), dirs::home_dir())
}

The comment was true before this commit and is now false. The app copies the bundled embedding model to where it expects the daemon to look, and the two now differ:

NODESPACE_HOME App copies the model to Daemon looks in
not valid UTF-8 the user's real ~/.nodespace/models the redirected home
empty .nodespace/models under the working directory the user's real home

In the first case an isolated run writes into the real home, which is the leak this PR exists to close, and the daemon finds no model.

Recommendation: apply the same rule in bundled_model.rs: take Option<OsString> from var_os, filter the empty value, and correct the comment and its unit tests. nodespace-app-lib does not depend on nodespace-daemon, so it cannot call the resolver; matching the rule is the fix in this PR.

Principle: one rule for one concept. Where a second copy has to exist, a change to one must change the other in the same commit.

🟢 Suggestion: an empty NODESPACE_HOME now runs uninstall against the real install

📁 packages/daemon/src/lib.rs:57-59, packages/cli/tests/it/home_isolation.rs:198-220

This is what the first review asked for, and it matches common practice (XDG treats an empty variable as unset). It has one cost. A script that runs NODESPACE_HOME="$DIR" nodespace uninstall with DIR unset now stops the user's daemon and removes the real bin, with no message. The new test asserts exactly that outcome.

Recommendation: your call. The stricter option is for nodespace_home() to return an error on a set-but-empty value ("NODESPACE_HOME is set but empty"); it already returns Result. If the current rule stays, the docs sentence should state it.

Principle: a destructive command should fail closed on a malformed isolation setting.

Checked and found sound

  • Every caller of nodespace_home() / nodespace_dir(). The callers are the database registry, daemon.toml (settings, local agent, capture), the model path, the incompatible-database marker, resolve_db_path, and the CLI's mcp, uninstall and logs. All go through the one function, so they move together. For a non-UTF-8 value they now honour the redirect instead of dropping to the real home. For an empty value they use the real home instead of a path relative to the working directory. No caller converts the path to a string in a way that would newly fail.
  • Test harnesses. Every harness that sets the variable (daemon-harness.ts, app-lib test-support, the daemon and CLI integration tests) passes a temp directory. None passes an empty value, so none changes behaviour.
  • Hermeticity of the new tests. HOME is a temp directory and PATH names a directory that does not exist. The CLI spawns launchctl, systemctl, bun and node by bare name without changing its own environment, so the lookup uses that PATH and finds nothing; a set PATH gets no default fallback. The plist and unit paths are built from HOME, so only the temp ones are removed. In this checkout the skill installer resolves to packages/skill/dist/install.js and then fails for want of a runtime. If a compiled installer were staged beside the test binary it would run, but against the temp HOME. After the run, this machine's daemon was still registered and running and ~/.nodespace/bin was intact.
  • The PATH change. command() returns a Command, so a temp directory created inside it would be deleted on return. A nonexistent path under the temp home avoids that and does not appear in the snapshot.
  • The unredirected test's stdout check. It uses ends_with because the skill-removal line differs by checkout ("Could not locate the skill installer" without a built dist, "Skill uninstall failed" with one). That is the right looseness.
  • Platform cfg. The two new integration tests are limited to macOS and Linux, where stop_daemon does something and HOME is what dirs::home_dir() reads. The non-UTF-8 unit test is #[cfg(unix)].
  • Project standards. No compatibility code, no issue numbers in comments, no lint suppression, no Pro-boundary wording.

Recommendation

APPROVE. Fold the bundled_model.rs fix into this PR before merging; it is the same rule in a second place.

@malibio malibio left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated pragmatic-code-review (re-review): APPROVE (posted as a comment — GitHub blocks self-approval). Full findings in the review comment above.

@malibio

malibio commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator Author

Address-review summary (round 2)

Addressed

  • 🟡 The app's copy of the NODESPACE_HOME rule disagreed with the daemon. bundled_model.rs now reads var_os, treats an empty value as unset and honours a path that is not UTF-8, as the daemon does. The comment that said otherwise is corrected. Two unit tests cover the empty and non-UTF-8 cases.

bun run test:changed passes.

Skipped

  • 🟢 Make a set-but-empty NODESPACE_HOME an error. Empty-as-unset is the common convention for an environment override and is what the first review asked for. The override is read as an Option by callers that have no error path (logs, the app's model copy), so an error would need a second way to report it. The docs sentence will state the rule.

Re-Review Decision

Decision: NO RE-REVIEW NEEDED

Rationale: the change applies the rule the re-review specified to one small pure function, with unit tests for both cases, and nothing else changed.

malibio and others added 4 commits October 5, 2026 18:04
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
… as unset, test the skill-removal skip and the unredirected uninstall

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…emon's rule

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@mstomar125
mstomar125 force-pushed the issue-3612-cli-nodespace-home branch from 900ef8f to 46e5276 Compare October 5, 2026 16:04
@mstomar125
mstomar125 merged commit 0e1cee0 into main Oct 5, 2026
@mstomar125
mstomar125 deleted the issue-3612-cli-nodespace-home branch October 5, 2026 16:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make the CLI's uninstall and logs commands follow NODESPACE_HOME

2 participants