openclaw - ✅(Solved) Fix service-env regenerator lists OPENCLAW_SERVICE_MANAGED_ENV_KEYS but does not export the actual values (gateway boots without auth token) [1 pull requests, 1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openclaw/openclaw#75374Fetched 2026-05-02 05:35:32
View on GitHub
Comments
1
Participants
2
Timeline
2
Reactions
2
Author
Timeline (top)
commented ×1cross-referenced ×1

The service-env regenerator (LaunchAgent gateway env loader) writes OPENCLAW_SERVICE_MANAGED_ENV_KEYS='KEY1,KEY2,...' listing keys that are supposed to be managed/exported, but the resulting ai.openclaw.gateway.env file does not contain the actual export FOO='value' lines for those keys.

Result: the gateway process starts with none of the managed keys in its environment. Any config field using ${OPENCLAW_GATEWAY_AUTH_TOKEN} (or any other managed key) resolves to an empty/literal placeholder. Authenticated clients get token_mismatch and the gateway looks broken even though everything else is fine.

Root Cause

The service-env regenerator (LaunchAgent gateway env loader) writes OPENCLAW_SERVICE_MANAGED_ENV_KEYS='KEY1,KEY2,...' listing keys that are supposed to be managed/exported, but the resulting ai.openclaw.gateway.env file does not contain the actual export FOO='value' lines for those keys.

Result: the gateway process starts with none of the managed keys in its environment. Any config field using ${OPENCLAW_GATEWAY_AUTH_TOKEN} (or any other managed key) resolves to an empty/literal placeholder. Authenticated clients get token_mismatch and the gateway looks broken even though everything else is fine.

Fix Action

Workaround

Inline literal values into openclaw.json for gateway.auth.token and hooks.token. Loses the benefit of env-ref-only secrets but unblocks the gateway.

PR fix notes

PR #75507: fix(gateway): keep launchd managed env values

Description (problem / solution / changelog)

Summary

  • Problem: macOS gateway service installs could write OPENCLAW_SERVICE_MANAGED_ENV_KEYS without the corresponding managed export KEY='value' lines in the LaunchAgent env file.
  • Why it matters: env-backed gateway tokens and other durable service env refs can resolve empty after gateway install or gateway restart, causing token mismatch failures.
  • What changed: launchd install plans now keep durable managed values for the owner-only env file while preserving the existing systemd behavior.
  • What did NOT change (scope boundary): managed values are still not serialized into LaunchAgent plist EnvironmentVariables.

Change Type (select all)

  • Bug fix
  • Feature
  • Refactor required for the fix
  • Docs
  • Security hardening
  • Chore/infra

Scope (select all touched areas)

  • Gateway / orchestration
  • Skills / tool execution
  • Auth / tokens
  • Memory / storage
  • Integrations
  • API / contracts
  • UI / DX
  • CI/CD / infra

Linked Issue/PR

  • Closes #75374
  • Related #
  • This PR fixes a bug or regression

Root Cause (if applicable)

  • Root cause: the install plan collected durable service env values, converted them into OPENCLAW_SERVICE_MANAGED_ENV_KEYS, and removed the actual values before the macOS launchd writer generated the owner-only env file.
  • Missing detection / guardrail: tests covered the metadata-only install plan shape and launchd env-file serialization separately, but not the macOS handoff where managed values must remain available to the env-file writer.
  • Contributing context (if known): reported for gateway.auth.token = "${OPENCLAW_GATEWAY_AUTH_TOKEN}" on a launchd-managed macOS gateway.

Regression Test Plan (if applicable)

  • Coverage level that should have caught this:
    • Unit test
    • Seam / integration test
    • End-to-end test
    • Existing coverage already sufficient
  • Target test or file: src/commands/daemon-install-helpers.test.ts, src/daemon/launchd.test.ts, src/daemon/systemd.test.ts
  • Scenario the test should lock in: darwin install plans retain durable managed env values for the launchd owner-only env file, while linux/systemd plans continue keeping those values out of inline service metadata.
  • Why this is the smallest reliable guardrail: the bug is in install-plan-to-service-writer handoff, so focused daemon install and launchd tests cover the affected seam directly.
  • Existing test that already covers this (if any): N/A
  • If no new test is added, why not: N/A

User-visible / Behavior Changes

On macOS, openclaw gateway install and restart paths keep managed service environment values available in the owner-only LaunchAgent env file, so env-backed tokens and API keys are present when the service starts.

Diagram (if applicable)

Before:
.env/config durable key -> OPENCLAW_SERVICE_MANAGED_ENV_KEYS only -> launchd env file misses export KEY='value'

After:
.env/config durable key -> managed key list + value retained for launchd -> owner-only env file exports KEY='value'

Security Impact (required)

  • New permissions/capabilities? (No)
  • Secrets/tokens handling changed? (Yes)
  • New/changed network calls? (No)
  • Command/tool execution surface changed? (No)
  • Data access scope changed? (No)
  • If any Yes, explain risk + mitigation: managed values are written only to the existing owner-only launchd env file path, not to LaunchAgent plist EnvironmentVariables; tests assert the plist does not contain the secret value.

Repro + Verification

Environment

  • OS: macOS behavior under unit tests
  • Runtime/container: local pnpm workspace
  • Model/provider: N/A
  • Integration/channel (if any): gateway launchd service install
  • Relevant config (redacted): env-backed gateway/service values via state-dir .env and config env vars

Steps

  1. Put a durable managed key in state-dir .env.
  2. Build a darwin gateway install plan.
  3. Install/stage a LaunchAgent using that environment.
  4. Inspect the generated owner-only env file and plist behavior.

Expected

  • The env file contains both OPENCLAW_SERVICE_MANAGED_ENV_KEYS and the managed export KEY='value' lines.
  • The LaunchAgent plist does not embed those values in EnvironmentVariables.

Actual

  • Covered by the new regression tests.

Evidence

  • Passing focused tests after the change

Human Verification (required)

What you personally verified (not just CI), and how:

  • Verified scenarios: darwin install plan retains durable managed values; launchd writes managed values to the env file and not the plist; linux/systemd metadata-only plan behavior remains intact.
  • Edge cases checked: service env precedence over managed keys and stale shell/env-profile values losing to durable .env values on darwin.
  • What you did not verify: live launchd install on a real macOS service; CI will run broader platform checks.

Validated locally:

  • pnpm test src/commands/daemon-install-helpers.test.ts src/daemon/launchd.test.ts src/daemon/systemd.test.ts
  • pnpm exec oxfmt --check --threads=1 src/commands/daemon-install-helpers.ts src/commands/daemon-install-helpers.test.ts src/daemon/launchd.test.ts CHANGELOG.md
  • git diff --check
  • pnpm check:changed (local; Blacksmith/Testbox unavailable)
  • codex review --base origin/main (no actionable findings; it first tried an invalid Jest-style --runInBand flag, then reran the targeted tests with valid Vitest flags)

Review Conversations

  • I replied to or resolved every bot review conversation I addressed in this PR.
  • I left unresolved only the conversations that still need reviewer or maintainer judgment.

Compatibility / Migration

  • Backward compatible? (Yes)
  • Config/env changes? (No)
  • Migration needed? (No)
  • If yes, exact upgrade steps: N/A

Risks and Mitigations

  • Risk: managed values could accidentally be reintroduced into launchd plist metadata.
    • Mitigation: the change only affects darwin install-plan environment passed to the existing owner-only env-file writer, and launchd tests assert the plist does not contain the secret value.

AI-assisted disclosure

AI-assisted. I reviewed the diff, ran focused local validation and codex review --base origin/main, and kept the change scoped to the launchd service-env handoff.

Changed files

  • CHANGELOG.md (modified, +1/-0)
  • src/commands/daemon-install-helpers.test.ts (modified, +43/-1)
  • src/commands/daemon-install-helpers.ts (modified, +16/-1)
  • src/daemon/launchd.test.ts (modified, +9/-1)

Code Example

export OPENCLAW_SERVICE_MANAGED_ENV_KEYS='ANTHROPIC_API_KEY,...,OPENCLAW_GATEWAY_AUTH_TOKEN,...'
export OPENCLAW_GATEWAY_AUTH_TOKEN='<actual value from .env>'
export ANTHROPIC_API_KEY='<actual value>'
...

---

export OPENCLAW_GATEWAY_PORT='18789'
export OPENCLAW_SERVICE_MANAGED_ENV_KEYS='ANTHROPIC_API_KEY,DISCORD_TOKEN_FORGE,ELEVENLABS_API_KEY,GEMINI_API_KEY,OPENAI_API_KEY,OPENCLAW_GATEWAY_AUTH_TOKEN,OPENCLAW_HOOKS_TOKEN,TELEGRAM_BOT_TOKEN,VOYAGE_API_KEY'
# (no further export lines for any of the listed keys)

---

#!/bin/sh
set -eu
env_file="$1"
shift
if [ -f "$env_file" ]; then
  . "$env_file"
fi
exec "$@"
RAW_BUFFERClick to expand / collapse

Summary

The service-env regenerator (LaunchAgent gateway env loader) writes OPENCLAW_SERVICE_MANAGED_ENV_KEYS='KEY1,KEY2,...' listing keys that are supposed to be managed/exported, but the resulting ai.openclaw.gateway.env file does not contain the actual export FOO='value' lines for those keys.

Result: the gateway process starts with none of the managed keys in its environment. Any config field using ${OPENCLAW_GATEWAY_AUTH_TOKEN} (or any other managed key) resolves to an empty/literal placeholder. Authenticated clients get token_mismatch and the gateway looks broken even though everything else is fine.

Environment

  • OpenClaw: 2026.4.29 (a448042)
  • macOS: Darwin 25.4.0 arm64 (Mac Studio M4 Max)
  • Install: npm install -g [email protected]
  • Service: launchd LaunchAgent ai.openclaw.gateway

Reproduction

  1. Have OPENCLAW_GATEWAY_AUTH_TOKEN (or any other managed key) defined in ~/.openclaw/.env.
  2. Configure gateway.auth.token = "${OPENCLAW_GATEWAY_AUTH_TOKEN}" in ~/.openclaw/openclaw.json.
  3. Run openclaw gateway install --force (or any command that triggers service-env regeneration).
  4. Inspect ~/.openclaw/service-env/ai.openclaw.gateway.env.

Expected

File contains:

export OPENCLAW_SERVICE_MANAGED_ENV_KEYS='ANTHROPIC_API_KEY,...,OPENCLAW_GATEWAY_AUTH_TOKEN,...'
export OPENCLAW_GATEWAY_AUTH_TOKEN='<actual value from .env>'
export ANTHROPIC_API_KEY='<actual value>'
...

Actual

File contains the metadata key list but no export FOO='value' lines for any of the managed keys:

export OPENCLAW_GATEWAY_PORT='18789'
export OPENCLAW_SERVICE_MANAGED_ENV_KEYS='ANTHROPIC_API_KEY,DISCORD_TOKEN_FORGE,ELEVENLABS_API_KEY,GEMINI_API_KEY,OPENAI_API_KEY,OPENCLAW_GATEWAY_AUTH_TOKEN,OPENCLAW_HOOKS_TOKEN,TELEGRAM_BOT_TOKEN,VOYAGE_API_KEY'
# (no further export lines for any of the listed keys)

Wrapper script (for reference)

~/.openclaw/service-env/ai.openclaw.gateway-env-wrapper.sh:

#!/bin/sh
set -eu
env_file="$1"
shift
if [ -f "$env_file" ]; then
  . "$env_file"
fi
exec "$@"

The wrapper sources the env file correctly — the bug is in whatever produces that file.

Impact

Caused a full Forge agent outage on 2026-04-30. UI showed:

Health check failed: Gateway token mismatch. Check gateway.auth.token or OPENCLAW_GATEWAY_TOKEN on the gateway host.

Recovery required SSH from a sibling machine and inlining the literal token value into openclaw.json. Every gateway restart regenerates the broken env file, so the env-ref form (${...}) cannot be safely used until this is fixed.

Possible fixes

  1. (Preferred) The regenerator should source ~/.openclaw/.env and emit the actual export FOO='value' lines for every key in OPENCLAW_SERVICE_MANAGED_ENV_KEYS.
  2. Alternatively, patch the wrapper script to source ~/.openclaw/.env directly so values are present even if the env file regen is incomplete. (Wrapper is regenerated by gateway install, env file by gateway restart — fixing the regenerator is the cleaner fix.)

Workaround

Inline literal values into openclaw.json for gateway.auth.token and hooks.token. Loses the benefit of env-ref-only secrets but unblocks the gateway.

extent analysis

TL;DR

The most likely fix is to modify the service-env regenerator to source ~/.openclaw/.env and emit actual export lines for managed keys.

Guidance

  • Verify that the ~/.openclaw/.env file contains the expected key-value pairs, such as OPENCLAW_GATEWAY_AUTH_TOKEN.
  • Check the service-env regenerator code to ensure it correctly reads the ~/.openclaw/.env file and generates the ai.openclaw.gateway.env file with the required export lines.
  • Consider patching the wrapper script to source ~/.openclaw/.env directly as a temporary workaround.
  • Test the fix by running openclaw gateway install --force and inspecting the generated ai.openclaw.gateway.env file.

Example

No code snippet is provided as the issue does not contain sufficient information about the regenerator code.

Notes

The fix may require modifications to the OpenClaw codebase, and it is recommended to test the changes thoroughly to ensure they do not introduce any regressions.

Recommendation

Apply the preferred fix by modifying the regenerator to source ~/.openclaw/.env and emit actual export lines for managed keys, as this is the cleaner and more robust solution.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING