Skip to content

fix(cli): keep operator-added service environment on start/restart - #16629

Open
kevin9327 wants to merge 3 commits into
paperclipai:masterfrom
kevin9327:fix/preserve-service-environment
Open

kevin9327 wants to merge 3 commits into
paperclipai:masterfrom
kevin9327:fix/preserve-service-environment

Conversation

@kevin9327

@kevin9327 kevin9327 commented Oct 17, 2026 •

Copy link
Copy Markdown

Thinking Path

  • Paperclip is the open source app people use to manage AI agents for work
  • The CLI can run an instance as a background service with launchd (macOS) or systemd (Linux)
  • service start, restart, install, and update re-render the service definition from a fixed template
  • The template has a fixed environment list, so the CLI deletes variables that the operator added (PATH, ENABLE_CLAUDEAI_MCP_SERVERS, bind hosts)
  • The CLI deletes them silently, and this can change agent security behavior after a routine upgrade
  • This pull request reads the installed definition and keeps every environment key that the CLI does not manage
  • The benefit is that operator settings survive start, restart, and update

Linked Issues or Issue Description

Fixes #16435

What Changed

  • Add extractExtraEnvironmentFromLaunchdPlist and extractExtraEnvironmentFromSystemdUnit. They return the environment keys in an installed definition, except the three keys that the CLI manages.
  • renderLaunchdPlist and renderSystemdUnit accept an optional extraEnvironment map and render it after the managed keys, with the same escaping as the managed values.
  • ServiceManager.renderDefinition(existingDefinition?) takes the installed definition. install, start, and restart on both managers render from the installed file before writeIfChanged.
  • The service health check compares the installed file with the rendering of that same file, so a customized definition is not reported as stale.
  • Regression tests for launchd (start + restart) and systemd (restart, quoted and unquoted Environment= lines).

Verification

  • vitest run cli/src/__tests__/service-manager.test.ts: the two new tests fail on master (custom keys are gone after the re-render) and pass with this change. All 21 tests in the file pass.
  • I ran vitest with a minimal config, not the full workspace install. I did not run service-health-check.test.ts locally because it needs the full workspace deps. CI runs it.

Risks

  • Low risk. When no custom keys exist, the output is byte-identical to before.
  • The CLI never copies a managed key (PAPERCLIP_HOME and the others) from the old file, so it still controls those values.

Model Used

  • AI-assisted: an AI coding agent (Grok Bot, with tool use and code execution) wrote the change and tests under my direction. I reviewed the diff.

Checklist

  • I have included a thinking path that traces from project context to this change
  • I have specified the model used (with version and capability details)
  • I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work
  • I have searched GitHub for duplicate or related PRs and linked them above
  • I have either (a) linked existing issues with Fixes: # / Closes # / Refs # OR (b) described the issue in-PR following the relevant issue template
  • I have not referenced internal/instance-local Paperclip issues or links
  • My branch name describes the change and contains no internal Paperclip ticket id
  • I have run tests locally and they pass
  • I have added or updated tests where applicable
  • I have updated relevant documentation to reflect my changes
  • I have considered and documented any risks above
  • All Paperclip CI gates are green
  • Greptile is 5/5 with no open P2s, recommendations, or follow-ups
  • I will address all Greptile and reviewer comments before requesting merge

paperclipai service start/restart/install and update re-rendered the
launchd plist and systemd unit from a fixed template, so any variable an
operator had added (PATH, ENABLE_CLAUDEAI_MCP_SERVERS, bind hosts) was
silently dropped. Read the installed definition, keep every non-managed
environment key, and render it back. The health check compares against
the same rendering so customized definitions are not reported as stale.

Fixes paperclipai#16435
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-17T03:08:09.259823Z 0ca8ca2 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@greptile-apps

greptile-apps Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] Changes how service definitions preserve custom environment variables.

The PR still needs the existing service-reload issue fixed before merging.

Findings

  1. P1 Restart keeps old settings ▶
Fix with agent prompt
### Issue 1
cli/src/services/service-manager.ts:undefined-306
If an operator adds `Environment="PATH=/opt/bin:/usr/bin"` immediately before `WorkingDirectory`, preserving it leaves the file unchanged. `ensureCurrent` then skips `daemon-reload`, so restart uses systemd's previously loaded settings. The same issue affects launchd install/start: an unchanged file skips `bootout`, and the fallback `kickstart` uses the loaded job. Reload based on whether the supervisor has the current settings, not just whether this call rewrites the file.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

The PR keeps operator-added environment settings when the CLI rewrites service definitions.

  • Service start and restart keep environment settings operators added.

Changes since the last review only remove code comments.

Reviews (3) · Last reviewed commit: "chore(cli): drop code comments added in ..." · Reviewed by Greptile

Comment thread cli/src/services/service-manager.ts Resolved
Comment thread cli/src/services/service-manager.ts Resolved

private async ensureCurrent(): Promise<boolean> {
const changed = await writeIfChanged(this.definitionPath, this.renderDefinition());
const changed = await writeIfChanged(this.definitionPath, await this.renderCurrentDefinition());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 Restart keeps old settings

If an operator adds Environment="PATH=/opt/bin:/usr/bin" immediately before WorkingDirectory, preserving it leaves the file unchanged. ensureCurrent then skips daemon-reload, so restart uses systemd's previously loaded settings. The same issue affects launchd install/start: an unchanged file skips bootout, and the fallback kickstart uses the loaded job. Reload based on whether the supervisor has the current settings, not just whether this call rewrites the file.

Prompt To Fix With AI
This is a comment left during a code review.
Path: cli/src/services/service-manager.ts
Line: 281

Comment:
**Restart keeps old settings**

If an operator adds `Environment="PATH=/opt/bin:/usr/bin"` immediately before `WorkingDirectory`, preserving it leaves the file unchanged. `ensureCurrent` then skips `daemon-reload`, so restart uses systemd's previously loaded settings. The same issue affects launchd install/start: an unchanged file skips `bootout`, and the fallback `kickstart` uses the loaded job. Reload based on whether the supervisor has the current settings, not just whether this call rewrites the file.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Comment thread cli/src/services/service-manager.ts Resolved
Comment thread cli/src/services/service-manager.ts Resolved
Re-escaping a decoded Environment= value doubled %-specifiers (%h became %%h) and dropped multi-assignment and single-quoted lines. Keep each operator assignment exactly as written and drop only the CLI-managed keys. Also decode numeric XML character references when reading launchd values.
@kevin9327

Copy link
Copy Markdown
Author

Pushed 7f9362d for the review findings:

  • systemd specifiers, multi-assignment, single quotes. Operator Environment= lines are now kept exactly as written. Only the CLI-managed keys are removed. %h stays %h, Environment="A=1" "B=2" and 'X=a b' survive, and a managed key on an operator line is dropped without losing the rest of that line.
  • launchd numeric character references. &#32; and &#x2F; are now decoded, so they don't come back as &amp;#32;.
  • Added regression tests for both. They fail before this commit, and service-manager.test.ts passes 23/23.

Not changed: an unchanged file still skips daemon-reload/bootout. That matches how ensureCurrent already behaves. If you'd like restart to reload unconditionally, or to check NeedDaemonReload, I can add that.

Comment thread cli/src/services/service-manager.ts Resolved

This branch has not been deployed

No deployments
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.

paperclipai service start/restart/install rewrites the launchd plist and drops custom EnvironmentVariables

1 participant