Skip to content

fix(systemd): apply dotenv changes on gateway restart - #119441

Merged
sallyom merged 5 commits into
openclaw:mainfrom
sallyom:fix/issue-118503-systemd-dotenv
Aug 7, 2026
Merged

sallyom merged 5 commits into
openclaw:mainfrom
sallyom:fix/issue-118503-systemd-dotenv

Conversation

@sallyom

@sallyom sallyom commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

Closes #118503

What Problem This Solves

Fixes an issue where operators editing a managed value in the state-directory .env would continue to get the old value after restarting a systemd-managed gateway. The generated systemd environment file captured a snapshot during installation, so later .env changes could not take effect without reinstalling or restaging the service.

Why This Change Was Made

The gateway already loads the state-directory .env when it starts. This change keeps those values owned by that startup path instead of copying them into the generated systemd environment file, while preserving operator-owned environment-file values and shell-backed secret references.

For services installed before this fix, gateway bootstrap now lets the state .env replace only inherited keys explicitly recorded in OPENCLAW_SERVICE_MANAGED_ENV_KEYS. Restaging also carries forward the prior ownership record, so deleting a managed dotenv key cannot reclassify its stale generated-file value as operator-owned.

User Impact

Edits to managed values in ~/.openclaw/.env, including rotated credentials, take effect on the next gateway restart without requiring the service to be reinstalled. A later install/restage removes deleted legacy managed values while preserving operator-owned environment-file entries.

Evidence

  • node scripts/run-vitest.mjs src/infra/dotenv.test.ts — 30 passed
  • node scripts/run-vitest.mjs src/daemon/systemd.test.ts — 142 passed
  • node scripts/run-vitest.mjs src/cli/gateway-cli/run.option-collisions.test.ts — 71 passed
  • node scripts/run-vitest.mjs src/config/types.secrets.test.ts — 7 passed
  • git diff --check — clean
  • pnpm tsgo:core — passed
  • targeted node scripts/run-oxlint.mjs across all 9 changed files — passed
  • autoreview --mode branch --base origin/main --engine codex — clean; patch correct (0.98 confidence)

Exact-head Linux/systemd behavior proof: Crabbox run run_d80913a0dff8, provider aws, clean exact head 980826939c206cc14a2a9bd7a2b6ed02ab767d9f. The run used genuine PID 1 and user systemd, with manager-generated invocation IDs. Each scenario ran in a separate transient unit and fresh Node process through the exact built selectGatewayRunEnvironment owner path.

RESULT: PASS
- rotating the managed dotenv value updated both HASS_TOKEN and hass_token to the new value
- deleting the managed dotenv value cleared both HASS_TOKEN and hass_token
- an unrelated operator-owned variable remained preserved in both scenarios
- exact-head focused tests passed: 407/407
- cleanup completed and the Crabbox lease was released

Proof limitation: the live systemd scenarios used --setenv, not a literal EnvironmentFile; focused tests cover systemd staging and restaging. A providerless raw {source,id} SecretRef has no schema-valid runtime location, so that case is covered by the focused collector regression rather than live runtime proof. This is focused owner-path behavior proof, not a literal installed-service end-to-end test.

Current-main merge-result proof

  • Current main base: 01cc71060d6c57eff142cfc210685c651ccb6397
  • Frozen PR source head: 980826939c206cc14a2a9bd7a2b6ed02ab767d9f
  • Local-only merge-result commit: 4ee78a08c104fb695b6c63674a1bacba8d388d71 (parents exactly the base and source above); merge completed without conflicts and was never pushed.
  • Merge-result command: node scripts/run-vitest.mjs src/infra/supervisor-markers.test.ts src/daemon/systemd.test.ts src/daemon/launchd.test.ts src/daemon/schtasks.install.test.ts src/cli/gateway-cli/run.option-collisions.test.ts src/infra/dotenv.test.ts src/config/types.secrets.test.ts — 7 files, 417 tests passed across 5 shards in 40.31s.
  • Fresh frozen-head autoreview --mode branch --base origin/main --engine codex --model gpt-5.6-sol --thinking high — clean; no accepted/actionable findings, patch correct (0.98 confidence).

Maintainer decision: accept the documented systemd-only restart-time dotenv precedence and compatibility contract, including managed credential values. This does not change public API, config, or protocol contracts; launchd and Scheduled Tasks retain their documented reinstall behavior.

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime size: XS maintainer Maintainer-authored PR labels Aug 5, 2026
@sallyom
sallyom marked this pull request as ready for review August 5, 2026 02:54
@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P1 High-priority user-facing bug, regression, or broken workflow. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. merge-risk: 🚨 auth-provider 🚨 May break OAuth, tokens, provider routing, model choice, or credentials. labels Aug 5, 2026
@clawsweeper

clawsweeper Bot commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed August 7, 2026, 9:48 AM ET / 13:48 UTC.

ClawSweeper review

What this changes

This PR stops systemd service staging from snapshotting state-directory dotenv values so managed credential edits take effect on gateway restart while operator-owned service values remain preserved.

Merge readiness

⚠️ Ready for maintainer review - 4 items remain

No blocking patch finding: current main still snapshots state dotenv values into systemd environment files, while this PR removes that stale owner boundary and scopes refresh to recorded systemd-managed keys. The protected maintainer label and intentional credential-precedence change require maintainer approval before merge.

Priority: P1
Reviewed head: 980826939c206cc14a2a9bd7a2b6ed02ab767d9f
Owner decision: Required. See Decision needed.

Review scores

Measure Result What it means
Overall readiness 🦞 diamond lobster (5/6) A focused owner-boundary repair with strong exact-head systemd proof and no actionable correctness or security finding.
Proof confidence 🦞 diamond lobster (5/6) ✨ media proof bonus Sufficient (linked_artifact): The PR provides exact-head Crabbox evidence using genuine user systemd and fresh gateway processes for rotated, deleted, case-variant managed keys and preserved operator values; focused tests cover literal staging and restaging.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (linked_artifact): The PR provides exact-head Crabbox evidence using genuine user systemd and fresh gateway processes for rotated, deleted, case-variant managed keys and preserved operator values; focused tests cover literal staging and restaging.
Evidence reviewed 7 items Current-main defect: Current main passes state dotenv values into the generated systemd environment file, while gateway dotenv loading retains already inherited process values; later dotenv edits therefore remain shadowed after a restart.
Owner-boundary repair: The PR stops persisting state dotenv values in systemd files, retains prior managed-key ownership across restage, and preserves operator-owned entries.
Scoped runtime refresh: Gateway bootstrap permits replacement only for keys recorded as systemd-managed, then clears deleted managed keys while preserving active configuration SecretRefs.
Findings None None.
Security None None.

How this fits together

Systemd starts the gateway with an inherited service environment, then gateway bootstrap loads the state-directory dotenv and configuration secrets. The change moves state-dotenv ownership out of generated systemd environment files and applies a narrow startup override for recorded managed keys.

flowchart LR
  A[State directory dotenv] --> B[Systemd service staging]
  B --> C[Gateway process environment]
  A --> D[Gateway bootstrap]
  C --> D
  D --> E[Managed key decision]
  E --> F[Gateway runtime]
Loading

Decision needed

Question Recommendation
Should OpenClaw adopt restart-time state-dotenv precedence for keys it explicitly recorded as systemd-managed, replacing the prior reinstall-required snapshot behavior? Approve the scoped refresh contract: Merge after an exact-merge refresh so recorded systemd-managed dotenv keys update on restart while operator-owned values and other supervisors retain their existing precedence.

Why: This is a deliberate upgrade-visible credential-precedence contract, not a mechanical correctness question, and the PR carries the protected maintainer label.

Before merge

  • Resolve merge risk (P1) - Existing systemd installations carrying the managed-key marker will intentionally see state-dotenv values replace their inherited snapshots on restart, including credential values.
  • Resolve merge risk (P1) - The branch is behind current main, so maintainers should refresh the merge result and rerun focused systemd coverage before landing.
  • Complete next step (P2) - A protected maintainer label and an intentional auth-sensitive upgrade contract require maintainer approval; no narrow mechanical repair remains.
Agent review details

Security

None.

PR surface

Source +106, Tests +192, Docs 0. Total +298 across 12 files.

View PR surface stats
Area Files Added Removed Net
Source 5 115 9 +106
Tests 4 213 21 +192
Docs 3 4 4 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 12 332 34 +298

Review metrics

Metric Value Why it matters
Production versus test delta production +119/-13; tests +213/-21 The net production growth implements the systemd ownership and compatibility boundary, with substantial focused regression coverage.

Stored data model

Persistent data-model change detected: serialized state: src/daemon/systemd.test.ts. Confirm migration or upgrade compatibility proof before merge.

Root-cause cluster

Relationship: fixed_by_candidate
Canonical: #118503
Summary: This PR is the active candidate repair for the canonical managed-systemd-dotenv issue; the earlier alternate PR was closed unmerged.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Merge-risk options

Maintainer options:

  1. Approve the scoped systemd refresh (recommended)
    Accept the documented upgrade behavior after refreshing the merge result and rerunning the focused systemd and dotenv regressions.
  2. Retain reinstall-required snapshots
    Pause the PR if maintainer policy requires generated systemd values to continue taking precedence until an explicit restage.

Technical review

Best possible solution:

Approve the documented systemd-only, marker-scoped dotenv refresh contract, then rebase or refresh the exact merge result and retain the focused ownership regressions.

Do we have a high-confidence way to reproduce the issue?

Yes, from source: current main serializes state dotenv values into systemd service environment files and gateway startup then retains those inherited values instead of later dotenv edits.

Is this the best way to solve the issue?

Yes, conditionally: removing the stale snapshot at service staging and allowing override only for recorded systemd-managed keys is cleaner than restaging on every restart, subject to maintainer acceptance of the precedence contract.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 01cc71060d6c.

Labels

Label justifications:

  • P1: Stale dotenv values can leave a restarted gateway using an old managed credential until an operator manually restages the service.
  • merge-risk: 🚨 compatibility: The PR intentionally changes restart-time precedence for existing systemd services carrying managed-key metadata.
  • merge-risk: 🚨 auth-provider: The changed precedence applies to managed provider credentials and secret-backed environment values.
  • rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (linked_artifact): The PR provides exact-head Crabbox evidence using genuine user systemd and fresh gateway processes for rotated, deleted, case-variant managed keys and preserved operator values; focused tests cover literal staging and restaging.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR provides exact-head Crabbox evidence using genuine user systemd and fresh gateway processes for rotated, deleted, case-variant managed keys and preserved operator values; focused tests cover literal staging and restaging.

Evidence

What I checked:

  • Current-main defect: Current main passes state dotenv values into the generated systemd environment file, while gateway dotenv loading retains already inherited process values; later dotenv edits therefore remain shadowed after a restart. (src/daemon/systemd.ts:1177, 01cc71060d6c)
  • Owner-boundary repair: The PR stops persisting state dotenv values in systemd files, retains prior managed-key ownership across restage, and preserves operator-owned entries. (src/daemon/systemd.ts:1160, 980826939c20)
  • Scoped runtime refresh: Gateway bootstrap permits replacement only for keys recorded as systemd-managed, then clears deleted managed keys while preserving active configuration SecretRefs. (src/cli/gateway-cli/pre-bootstrap.ts:272, 980826939c20)
  • Regression coverage: Focused tests cover fresh staging, restaging with operator-key preservation, case aliases, systemd-only scope, deletion, and providerless env SecretRefs. (src/daemon/systemd.test.ts:1647, 980826939c20)
  • Exact-head behavior proof: The PR body links Crabbox run run_d80913a0dff8 on this exact head, reporting real user-systemd restart scenarios for rotating and deleting managed keys while retaining an operator key. (980826939c20)
  • Feature history: The systemd dotenv-file approach appears to date to the service-env change in this commit; later managed-key work refined the same ownership boundary. (src/daemon/systemd.ts, a2ab9e6a8e4)

Likely related people:

  • Darren2030: Added managed-service key handling that this PR preserves during startup cleanup. (role: managed-key and SecretRef contributor; confidence: high; commits: e79865569c5; files: src/daemon/service-managed-env.ts, src/config/types.secrets.ts)
  • tmimmanuel: Introduced the file-staged dotenv approach whose stale snapshot behavior this PR replaces. (role: systemd environment-file introducer; confidence: high; commits: a2ab9e6a8e4; files: src/daemon/systemd.ts)
  • steipete: Recent managed-service startup audit is adjacent to the repair boundary. (role: recent service-lifecycle contributor; confidence: medium; commits: 6416baef839; files: src/daemon/systemd.ts, src/cli/gateway-cli/pre-bootstrap.ts)
  • VincentKoc: Recent environment-source lookup work is adjacent to the marker-gated precedence path. (role: environment-source refactor contributor; confidence: medium; commits: adf78cefbd7; files: src/infra/dotenv-global.ts)

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

History

Review history (12 earlier review cycles; latest 8 shown)
  • reviewed 2026-08-05T16:36:56.353Z sha 5149a74dcac8babb20aa12a0574a638c5a09b2e7 :: needs maintainer review before merge. :: none
  • reviewed 2026-08-05T21:39:35.889Z sha a6f6c983c6793dfb190308797775b08d54abad7d :: needs changes before merge. :: [P1] Preserve provider-less env SecretRefs during startup cleanup
  • reviewed 2026-08-05T22:31:14.892Z sha a6f6c983c6793dfb190308797775b08d54abad7d :: found issues before merge. :: [P1] Preserve provider-less env SecretRefs during startup cleanup
  • reviewed 2026-08-05T22:38:34.734Z sha a6f6c983c6793dfb190308797775b08d54abad7d :: found issues before merge. :: [P1] Preserve provider-less env SecretRefs during startup cleanup
  • reviewed 2026-08-06T04:38:45.032Z sha b8b10e894ce911f473419eab2171ae37c01d5f7e :: found issues before merge. :: [P2] Document the managed systemd dotenv override
  • reviewed 2026-08-06T04:44:40.585Z sha b8b10e894ce911f473419eab2171ae37c01d5f7e :: found issues before merge. :: [P2] Document the marker-scoped systemd dotenv override
  • reviewed 2026-08-06T13:45:32.078Z sha 940bbbaaa123d174b90abdad77f8642aaa3e6ccb :: found issues before merge. :: [P2] Align supervisor scope with the documented contract
  • reviewed 2026-08-06T23:26:19.786Z sha 9808269 :: needs maintainer review before merge. :: none

@openclaw-barnacle openclaw-barnacle Bot added cli CLI command changes size: S and removed size: XS labels Aug 5, 2026
@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. labels Aug 5, 2026
@sallyom
sallyom requested a review from a team as a code owner August 5, 2026 05:12
@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. labels Aug 5, 2026
@clawsweeper clawsweeper Bot added rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Aug 5, 2026
@sallyom

sallyom commented Aug 5, 2026

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: when the review finishes, ClawSweeper will create the durable review comment if needed or update the existing comment in place.

Re-review progress:

@clawsweeper clawsweeper Bot added the proof: sufficient ClawSweeper judged the real behavior proof convincing. label Aug 5, 2026
@sallyom
sallyom force-pushed the fix/issue-118503-systemd-dotenv branch from 5149a74 to a6f6c98 Compare August 5, 2026 20:48
@clawsweeper clawsweeper Bot added the rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. label Aug 6, 2026
@sallyom
sallyom force-pushed the fix/issue-118503-systemd-dotenv branch from b8b10e8 to 940bbba Compare August 6, 2026 13:18
@openclaw-barnacle openclaw-barnacle Bot added the docs Improvements or additions to documentation label Aug 6, 2026
@sallyom

sallyom commented Aug 6, 2026

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: when the review finishes, ClawSweeper will create the durable review comment if needed or update the existing comment in place.

Re-review progress:

@clawsweeper clawsweeper Bot removed the proof: sufficient ClawSweeper judged the real behavior proof convincing. label Aug 6, 2026
@sallyom
sallyom force-pushed the fix/issue-118503-systemd-dotenv branch from 940bbba to 5aed188 Compare August 6, 2026 15:25
@sallyom
sallyom force-pushed the fix/issue-118503-systemd-dotenv branch from 5aed188 to 9808269 Compare August 6, 2026 18:04
@sallyom

sallyom commented Aug 6, 2026

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: when the review finishes, ClawSweeper will create the durable review comment if needed or update the existing comment in place.

@clawsweeper clawsweeper Bot added rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. proof: sufficient ClawSweeper judged the real behavior proof convincing. and removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Aug 6, 2026
@sallyom
sallyom merged commit 4cbbfc2 into openclaw:main Aug 7, 2026
141 checks passed
@sallyom

sallyom commented Aug 7, 2026

Copy link
Copy Markdown
Contributor Author

Merged via squash.

@sallyom
sallyom deleted the fix/issue-118503-systemd-dotenv branch August 7, 2026 14:04
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Aug 8, 2026
* fix(systemd): load state dotenv at gateway startup

Signed-off-by: sallyom <[email protected]>

* fix(systemd): refresh managed dotenv values on restart

Signed-off-by: sallyom <[email protected]>

* fix(gateway): clear removed managed dotenv values

Signed-off-by: sallyom <[email protected]>

* fix(dotenv): canonicalize managed override keys

Signed-off-by: sallyom <[email protected]>

* fix(secrets): preserve providerless env refs

Signed-off-by: sallyom <[email protected]>

---------

Signed-off-by: sallyom <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cli CLI command changes docs Improvements or additions to documentation gateway Gateway runtime maintainer Maintainer-authored PR merge-risk: 🚨 auth-provider 🚨 May break OAuth, tokens, provider routing, model choice, or credentials. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. P1 High-priority user-facing bug, regression, or broken workflow. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. size: M status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Managed env keys in systemd env files don't sync when .env is edited

1 participant