fix: unset CLAUDECODE env var when spawning worker daemon from hooks#1354
fix: unset CLAUDECODE env var when spawning worker daemon from hooks#1354Blevene wants to merge 1 commit intothedotmack:mainfrom
Conversation
The worker-service daemon is spawned by hook commands that run inside Claude Code's process tree, so it inherits the CLAUDECODE environment variable. This causes the worker to behave as if it's running inside a Claude Code session, interfering with subprocess spawning in nested environments (see thedotmack#1163). Wrapping the start command with `env -u CLAUDECODE` ensures the worker daemon launches as a clean, independent process. Fixes thedotmack#1352 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
xkonjin
left a comment
There was a problem hiding this comment.
Code Review
This PR addresses an environment variable issue by unsetting CLAUDECODE before spawning the worker daemon from hooks. This prevents potential configuration leaks.
What's Good
- Targeted fix: addresses the root cause without unnecessary changes
- Consistent application across all hook configurations (workspace, agent, chat, server)
- Uses standard env -u which is portable and well-understood
Considerations
Documentation
- The PR description doesn't explain why CLAUDECODE needs to be unset. Consider adding what problems occur if CLAUDECODE is inherited and why this matters for the worker daemon specifically.
Error Handling
- If env -u fails (unlikely but possible), the command won't run at all. Consider whether the worker should still start even if unsetting the variable fails.
Alternatives
- Could also consider using CLAUDECODE="" bun ... which explicitly sets it to empty instead of removing it from the environment entirely. However, env -u is cleaner if the intent is to completely remove it.
Testing
- Verify that the worker daemon starts correctly with this change
- Test that CLAUDECODE is actually unset in the worker process
- Ensure hooks still function as expected after this change
Summary
LGTM with the above considerations addressed. This is a clean fix that prevents environment variable leakage. Adding documentation to explain the rationale would make the PR more self-contained and help future maintainers understand the change.
Status: Looks good, suggest adding rationale to PR description
xkonjin
left a comment
There was a problem hiding this comment.
Code Review — Unset CLAUDECODE env var in hooks
Overall: Targeted, low-risk fix. The diagnosis is correct — hook commands inheriting CLAUDECODE from the parent Claude Code session would cause the worker daemon to misidentify its execution context.
✅ Strengths
- Minimal diff, surgical fix across all 4 hook invocations
env -u CLAUDECODEis POSIX-compliant and reliable- Correctly addresses the root cause rather than working around it in worker-service.cjs
🟡 Portability concern
env -u works on Linux and macOS but is not available on Windows (even in Git Bash). If any users run Claude Code hooks on Windows, this will break. Depending on the user base, a cross-platform alternative in worker-service.cjs itself (e.g. delete process.env.CLAUDECODE at startup) might be more robust.
💡 Suggestion: add a comment
Since the env -u prefix is non-obvious, a one-line comment in hooks.json (if JSON5/comments are supported) or in the PR description explaining why would help future maintainers. The PR description already covers this well, so this is mainly for discoverability.
|
Superseded by the embedded Process Supervisor (PR #1370, v10.5.6). Env sanitization is now centralized in |
Summary
worker-service.cjs starthook commands withenv -u CLAUDECODEto prevent the worker daemon from inheriting the Claude Code session environment variableProblem
Hook commands run inside Claude Code's process tree, so
bun worker-service.cjs startinherits theCLAUDECODEenvironment variable. This causes the worker daemon to behave as if it's running inside a Claude Code session, interfering with subprocess spawning in nested environments.Related: #1163 (broader env-leaking issue with
CLAUDECODE,CLAUDE_CODE_ENTRYPOINT, proxy vars)Test plan
worker-service.cjsspawnscat /proc/<pid>/environ | tr '\0' '\n' | grep CLAUDE— verifyCLAUDECODEis not setFixes #1352
🤖 Generated with Claude Code