Skip to content

Add a DaemonProfile for the app's daemon binary and service env (no behavior change) - #3391

Merged
mstomar125 merged 2 commits into
mainfrom
issue-3346-daemon-profile
Sep 30, 2026
Merged

mstomar125 merged 2 commits into
mainfrom
issue-3346-daemon-profile

Conversation

@mstomar125

Copy link
Copy Markdown
Contributor

Closes #3346

Summary

Adds DaemonProfile (ADR-082 section 3.6, proposed) and routes every edition-dependent choice of which daemon binary the app runs and what extra environment its launchd registration carries through the process's active profile. Community and Pro builds behave exactly as they do today; this is a pure refactor.

  • New app-lib/src/daemon_profile.rs: DaemonProfile { binary_name, service_env, product }, community(), image_name() (cfg(any(windows, test))), and active() (a OnceLock initialised from daemon_setup::profile_for_this_build). Docs use generic wording only.
  • daemon_setup.rs: profile_for_build_env / profile_for_this_build are the only code that picks binary, env or product by edition. daemon_binary_name() reads active().binary_name. daemon_binary_name_for, daemon_image_name_for and the pro_env block are deleted. write_plist takes &DaemonProfile and renders service_env as escaped <key>/<string> pairs after core's own two variables.
  • Identity (label, socket, UI pid file, marker) and update_check still branch on is_pro_build(), as the issue specifies. Arguments are untouched.

Paths follow the crate split: the issue's packages/desktop-app/src-tauri/src is now packages/desktop-app/app-lib/src (crate nodespace-app-lib).

Deviations from the issue text

  • profile_for_build_env's first parameter is named endpoint_url, not supabase_url. The issue's name is a cloudWording hit on two lines; the neutral name keeps that marker flat. Signature and behaviour are otherwise as specified.
  • DAEMON_BINARY_NAME became pub(crate) so community() can use it (it now has no other reader).
  • Extra test beyond the issue's list: the_active_profile_starts_as_the_build_default, the only guard on active()'s initialisation (detectable only in a build with the edition variables set; see mutation table).
  • The acceptance grep for is_pro_build also lists two lines in daemon_setup.rs's inline tests (the import and the agreement test the_profile_and_the_identity_selection_agree_on_the_edition), which pin that the profile selection and the identity selection agree.
  • The manual tauri dev plist comparison was replaced by a stronger scratch check (below).

Ratchet (scripts/check-pro-boundary.ts), BASELINES untouched

Measured on origin/main 1a0f437 versus this branch. No count rose.

marker main this PR delta
proCommands 275 271 -4
editionBranching 113 104 -9
cloudWording 72 72 0
proWording 280 275 -5
all others unchanged unchanged 0

Class under CLAUDE.md "Pro / Sync Boundary": a planned move of Pro code already in core (class 3), adding no behaviour. The Pro values that lived in write_plist's pro_env now live in profile_for_build_env, and their pinning test (edition_values_select_the_edition_daemon_environment_and_product and the (Some, None) case) is new. That test and function carry nodespaced-pro, NODESPACED_PRO_* and is_pro_build text (the editionBranching lines in the new test module, offset by the deleted pro_edition_image_name_matches_pro_binary and the pro_env block). They are the code that #3354 deletes whole.

Windows hunks (not compiled on macOS): please read each

All are one-line swaps to daemon_profile::active().image_name():

  • signal_daemon_to_stop (#[cfg(windows)])
  • kill_running_daemon (#[cfg(windows)])
    image_name returns format!("{}.exe", binary_name), the deleted daemon_image_name_for body with binary_name in place of the edition lookup. windows_taskkill_image_name_tests now guards that the image name cannot drift from daemon_binary_name().

Verification

  • cargo clippy -p nodespace-app-lib --all-targets -- -D warnings -D clippy::unused-async: clean. cargo fmt -p nodespace-app-lib --check: clean.
  • cargo test -p nodespace-app-lib --lib -- daemon_ macos_plist windows_taskkill sidecar_: 72 pass, in a community build and again in a build with NODESPACE_PRO_SUPABASE_URL=https://example.invalid NODESPACE_PRO_ANON_KEY=test.
  • NODESPACE_PRO_SUPABASE_URL=... NODESPACE_PRO_ANON_KEY=test cargo check -p nodespace-app-lib --all-targets and cargo check -p nodespace-app: compile.
  • bun run test:changed: passed (tooling tests and the Rust workspace). bun run quality:scripts:fix: no changes.
  • Byte-identical plist (scratch test, removed afterwards): a verbatim copy of origin/main's write_plist (with its pro_env block) versus the new one, for community (1313 bytes), the Pro values (1486 bytes) and Pro values containing <, &, > (1507 bytes): identical bytes in all three.
  • New plist tests parse the file with plutil -convert json and plutil -lint, so they assert what launchd will read, not just the text.

Mutation results (each new test, one mutation per run; all killed)

mutation killed by
community product changed community_profile_carries_the_community_daemon_and_no_service_env
community service_env non-empty same test, plus community_plist_environment_holds_only_the_two_core_variables
community binary_name changed community_profile_carries_... (and image_name_is_...)
image_name drops .exe image_name_is_the_binary_name_with_the_windows_extension, active_profile_image_name_matches_the_installed_binary_name
image_name ignores binary_name image_name_is_the_binary_name_with_the_windows_extension (custom-daemon case)
daemon_binary_name() returns another name active_profile_image_name_matches_the_installed_binary_name
a key alone selects the edition a_key_without_a_url_still_selects_the_community_profile
edition binary name -> community edition_values_select_the_edition_daemon_environment_and_product
edition env order swapped edition_values_... and a_missing_anon_key_defaults_to_empty
edition product -> community edition_values_...
missing key defaults to non-empty a_missing_anon_key_defaults_to_empty
xml_escape dropped on key service_env_keys_and_values_are_xml_escaped
xml_escape dropped on value service_env_keys_and_values_are_xml_escaped
env indent changed service_env_renders_after_the_core_variables_in_order
service_env not rendered service_env_renders_... and service_env_keys_and_values_are_xml_escaped
is_pro_build() forced true the_profile_and_the_identity_selection_agree_on_the_edition
profile_for_this_build ignores the baked values (Pro-env build) the_profile_and_the_identity_selection_agree_on_the_edition
active() ignores the build default (Pro-env build) the_active_profile_starts_as_the_build_default

Not verified

The two Windows hunks are not compiled here. The optional real-device tauri dev plist comparison was not run; the scratch byte comparison above stands in for it.

🤖 Generated with Claude Code

@mstomar125

Copy link
Copy Markdown
Contributor Author

Pragmatic code review (initial, medium effort)

Reviewed via the inlined pragmatic-code-reviewer agent (the repo's review command files are not registered as skills in this environment).

Review Type: Initial Review

Requirements Check

  • ✅ daemon_binary_name_for, daemon_image_name_for and the pro_env block are gone. git grep -nE "daemon_binary_name_for|daemon_image_name_for|pro_env" packages scripts finds only the word pro_env in the proCommands marker summary text in scripts/check-pro-boundary.ts (lines 119 and 205). No code or doc reference remains.
  • ✅ is_pro_build readers: the definition (85), daemon_socket_relative (160), launch_agent_label (166), ui_pid_relative (358), grpc_client.rs:521, incompatible_database.rs:37 and update_check.rs:146. Two extra lines, both in the new test module (import at 2819, agreement test at 2889), are disclosed in the PR body and sound. PRO_DAEMON_BINARY_NAME, PRO_SUPABASE_URL and PRO_ANON_KEY are now read only inside profile_for_build_env and profile_for_this_build.
  • ✅ Community plist is byte-identical. The community pro_env was "" and the new service_env collects an empty iterator into "". The Pro \ continuation plus \x20 plus 7 spaces gives 8 spaces of indent, which matches the new per-pair format. Key order is URL then key, and xml_escape is applied to both as before. community_plist_environment_holds_only_the_two_core_variables reads EnvironmentVariables back through plutil -convert json; NODESPACED_SOCKET sorts before NODESPACE_UI_BINARY, so the assertion is correct.
  • ✅ Every field of profile_for_build_env(Some, Some) is pinned: binary_name, service_env in order, and product. The (Some, None) and (None, Some) cases are pinned too.
  • ✅ Escaping test covers <, &, > in both key and value: raw text, then plutil -lint, then the plutil JSON round-trip of the original value.
  • ⚠️ Pro-env cargo check: not verifiable by the reviewer (no cargo allowed); the author ran it (nodespace-app-lib --all-targets and nodespace-app, both compile) and by reading, profile_for_build_env(PRO_SUPABASE_URL, PRO_ANON_KEY) type-checks.
  • ✅ Ratchet: exits 0. proCommands 271 (baseline 275), editionBranching 104 (113), cloudWording 72 (72), proWording 275 (281), matching the PR table. BASELINES are untouched by the coordinator's rule, so the issue's "update BASELINES" clause is deliberately not applied.
  • ✅ Every added line that carries Pro, Supabase or tenant wording is either the moved pro_env comment, moved Pro values, or the pinning test: a planned class-3 relocation. No new prose introduces such wording. daemon_profile.rs has none. No issue numbers appear in added lines.

Code Review Findings

No Critical or Important issues.

  1. Suggestion daemon_profile.rs:20-26 (service_env doc). A key that repeats NODESPACED_SOCKET or NODESPACE_UI_BINARY produces a duplicate key in the plist dict. Tested with plutil: -lint says OK, and -convert json keeps the last value, so a profile entry would silently override core's socket. Not blocking: the only producer of service_env in this PR is profile_for_build_env, with two fixed non-colliding keys, and nothing can install another profile until the extension point lands. Enforcement here would be new behavior in a "no behavior change" PR. Recommendation: enforce at the point that accepts an external profile (the AppExtensions::daemon_profile install path), rejecting reserved names from a single shared list that write_plist also uses. Principle: validate at the trust boundary, not at the sink.
  2. Nit daemon_profile.rs:1-13 (module doc). The first sentence says every choice that depends on the daemon's identity goes through the profile; the last paragraph says label, socket, pid file and marker are not in the profile. Scope the first sentence.
  3. Nit windows_taskkill_image_name_tests. active_profile_image_name_matches_the_installed_binary_name re-derives the same formula as image_name; it only discriminates in an edition-env build and does not verify the #[cfg(windows)] call sites use active(). The doc's "cannot drift" wording slightly overstates it.
  4. Nit daemon_setup.rs:98-102 (profile_for_build_env doc). "Both values are XML-safe" is a carried-over rationale; write_plist now escapes every entry regardless and this function renders no XML.
  5. Nit (follow-up, out of scope) scripts/check-pro-boundary.ts:119 and :205 still name pro_env in the proCommands summary. Fix in the follow-up that tightens BASELINES, because the summary is mirrored into the printed block.

Checks that came back clean

  • OnceLock: active() is get_or_init(profile_for_this_build); the init path never re-enters active(), so no re-entrancy deadlock. The daemon_profile / daemon_setup module cycle is only a type and const import.
  • Behavior: daemon_binary_name() returns nodespaced / nodespaced-pro as before; Some("") for the URL is still an edition. The systemd unit and Windows spawn still carry no service_env.
  • Windows hunks: both are one-line swaps to daemon_profile::active().image_name() (still a String; &image_name and %image_name uses unchanged). image_name is under cfg(any(windows, test)); no missing import or unused-symbol problem on macOS, Linux or Windows.
  • Tests: correct cfg gates (plutil tests are macOS-only), unique temp-dir tags removed at the end, no OnceLock cross-talk, pins are real values.
  • Docs: the kill_running_daemon, sidecar_install_path, should_restart_for_log_rotation and kickstart comments are accurate after the change.

Recommendation: APPROVE. No blocking issues; the only substantive follow-up is the reserved-key check, which belongs at the extension install point.

@mstomar125 mstomar125 left a comment

Copy link
Copy Markdown
Contributor 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.

@mstomar125

Copy link
Copy Markdown
Contributor Author

Address-review summary

Addressed in e28ffd4 (documentation only, no code change):

  • ✅ Nit 2 (daemon_profile.rs module doc): the opening sentence is now scoped to binary, Windows image name and service environment, so it no longer contradicts the note that label, socket, pid file and marker are not part of a profile.
  • ✅ Nit 3 (windows_taskkill_image_name_tests doc): reworded to say the test ties image_name to daemon_binary_name(), and that it does not cover the Windows call sites reading the active profile (compile-only, as before).
  • ✅ Nit 4 (profile_for_build_env doc): dropped the carried-over "XML-safe" rationale, since write_plist escapes every entry and this function renders no XML.

Skipped, with reasons:

  • ⏭️ Suggestion 1 (reject service_env keys that repeat NODESPACED_SOCKET / NODESPACE_UI_BINARY): enforcing it here would add behavior to a no-behavior-change refactor, and the only producer of service_env in this PR is profile_for_build_env with two fixed, non-colliding keys. The check belongs where an external profile is accepted (the AppExtensions::daemon_profile install path), ideally from one shared reserved-name list that write_plist also uses. I suggest adding that to that issue's acceptance criteria. Revisit if anything other than profile_for_build_env can produce a profile before that lands.
  • ⏭️ Nit 5 (pro_env still named in the proCommands marker summary in scripts/check-pro-boundary.ts): out of scope. The coordinator's rule is that parallel PRs do not touch that file, and the summary is mirrored into the printed BASELINES block, so it belongs with the final tightening.

Verification after the fix: cargo fmt -p nodespace-app-lib --check clean, cargo clippy -p nodespace-app-lib --all-targets -- -D warnings -D clippy::unused-async clean (0 warnings), ratchet counts unchanged (proCommands 271, editionBranching 104, cloudWording 72, proWording 275).

Re-Review Decision

Decision: NO RE-REVIEW NEEDED

Rationale: The follow-up commit changes three comment blocks only, with no logic, cfg gate or test change. Compilation, lint and ratchet were re-verified after the edit. The reviewer's verdict on the code was APPROVE with no Critical or Important findings.

mstomar125 and others added 2 commits September 30, 2026 13:56
… DaemonProfile

Add DaemonProfile (binary_name, service_env, product) and a process-wide
active() profile. The build's default profile is chosen in one place,
daemon_setup::profile_for_build_env / profile_for_this_build; daemon_binary_name,
the Windows taskkill image name and the launchd plist environment now read the
active profile instead of branching on is_pro_build.

write_plist renders profile.service_env as escaped key/string pairs after core's
own two variables, byte-identical to the previous Pro block for the same values
and unchanged for a profile with none. Identity selections (label, socket, pid
file, marker) are untouched.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
- daemon_profile module doc: scope the opening sentence to binary, Windows
  image name and service environment, so it no longer contradicts the note that
  label, socket, pid file and marker are not part of a profile.
- windows_taskkill_image_name_tests doc: say the test ties image_name to
  daemon_binary_name(), not that the Windows call sites read the active profile.
- profile_for_build_env doc: drop the carried-over XML-safe rationale, since
  write_plist escapes every service_env entry and this function renders no XML.

Documentation only; no code change.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@mstomar125
mstomar125 force-pushed the issue-3346-daemon-profile branch from e28ffd4 to 30eb943 Compare September 30, 2026 11:56
@mstomar125
mstomar125 merged commit 698246c into main Sep 30, 2026
@mstomar125
mstomar125 deleted the issue-3346-daemon-profile branch September 30, 2026 11:56
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.

Add a DaemonProfile for the app's daemon binary and service env (no behavior change)

1 participant