Skip to content

Sandboxed claude -p summarizer leaks CLAUDE_JOB_DIR / CLAUDE_CODE_SESSION_ID into nested sessions #95

Closed

Description

@FrankLedo

Summary

The sandboxed claude -p calls already strip CLAUDECODE from the child env "to allow nested sessions", but they don't strip the harness's job-identity vars CLAUDE_JOB_DIR and CLAUDE_CODE_SESSION_ID. As a result, the headless summarizer/compressor sub-sessions inherit the parent session's job identity and masquerade as part of the parent's "agents view" background job.

Combined with cwd=tempfile.gettempdir(), this surfaces as phantom temp-dir sessions in the Claude Code agents view / /resume picker. On my machine 457 of them accumulated under ~/.claude/projects/-private-tmp/, each a remember summarizer/compressor transcript.

This isn't a functional break in remember — the summarization works fine — it's an env-hygiene leak in the same class as the CLAUDECODE stripping you already do.

Root cause

Two launch sites strip only CLAUDECODE:

pipeline/haiku.py (~line 69):

# CLAUDECODE env var blocks nested sessions — must strip it
env = {k: v for k, v in os.environ.items() if k != "CLAUDECODE"}
result = subprocess.run(cmd, ..., env=env, cwd=tempfile.gettempdir())

scripts/save-session.sh (lines 175 and 241):

HAIKU_JSON=$(cd /tmp && env -u CLAUDECODE claude -p ...)
NDC_JSON=$(cd /tmp && env -u CLAUDECODE claude -p ...)

Because CLAUDE_JOB_DIR (and CLAUDE_CODE_SESSION_ID) survive into the child, anything keyed off "is this a background/agents-view job" treats the throwaway summarizer as a real, resumable session belonging to the parent job.

Impact

  • Phantom tmp : … sessions pile up in the agents view / resume picker (hundreds over time).
  • Tools that key off CLAUDE_JOB_DIR can write into the parent job's dir from the child, e.g. clobbering a shared per-job state/marker file.

Proposed fix

Extend the existing env strip to also drop the harness job-identity vars at both sites — same intent as the CLAUDECODE handling, since these sandboxed children are explicitly not part of the parent job:

pipeline/haiku.py:

_STRIP = {"CLAUDECODE", "CLAUDE_JOB_DIR", "CLAUDE_CODE_SESSION_ID"}
env = {k: v for k, v in os.environ.items() if k not in _STRIP}

scripts/save-session.sh:

env -u CLAUDECODE -u CLAUDE_JOB_DIR -u CLAUDE_CODE_SESSION_ID claude -p ...

Notes

  • Repro: with an agents-view (background) session active, trigger consolidation (e.g. exit and relaunch claude --agents); the summarizer sub-sessions show up named in the picker.
  • I also added a defensive guard downstream in my own session-naming hook (skip naming any session whose cwd is a temp dir), but the cleanest place to stop the leak is here at the source. Happy to send a PR if you'd like.

🤖 Filed via Claude Code

Activity

  1. FrankLedo commented on Jun 9, 2026

    @FrankLedo
    Author

    Cross-referencing #87 ("isolate the headless claude -p summarization subprocess") — these overlap. The leak described here is a consequence of the summarizer subprocess inheriting the parent's full environment: CLAUDE_JOB_DIR / CLAUDE_CODE_SESSION_ID flow through unchanged, so the nested session looks like a resumable agents-view session to anything keying off those vars.

    Isolating the subprocess environment as proposed in #87 would very likely resolve this as a side effect — stripping/overriding the inherited Claude Code session vars (not just CLAUDECODE) when spawning the headless claude -p summarizer/compressor would stop the leak at the source. Filing separately for visibility, but they can probably be triaged together.

  2. fdaviddpt commented on Jun 18, 2026

    @fdaviddpt
    Contributor

    Thank you for filing this separately with the root-cause analysis, @FrankLedo — spot on. 🙏

    Status: #93's --no-session-persistence removed the most visible symptom (the nested session is no longer written as a resumable record, so it stops flooding Recents / the agents view). But you're right that the underlying leak remains — the claude -p subprocess still inherits CLAUDE_JOB_DIR and CLAUDE_CODE_SESSION_ID, because we only strip CLAUDECODE.

    Fixing that properly now: stripping the parent Claude Code session vars (CLAUDE_JOB_DIR, CLAUDE_CODE_SESSION_ID, and the rest of CLAUDE_CODE_*) from the child env. PR incoming — keeping this open until it lands.

  3. added a commit that references this issue on Jun 19, 2026
  4. added 5 commits that reference this issue on Oct 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions