Skip to content

AlgoPhase phase-write can hit the wrong session: reads CLAUDE_SESSION_ID, which Claude Code doesn't expose #1521

Description

@MatiasBarboza

Symptom

bun ~/.claude/LIFEOS/TOOLS/AlgoPhase.ts <phase> (without --slug) can write the Algorithm phase to a different session's row in work.json than the active one — corrupting the Pulse dashboard's phase / mode-history for an unrelated session.

Root cause

Slug resolution priority (in AlgoPhase.ts):

  1. --slug
  2. row whose sessionUUID === process.env.CLAUDE_SESSION_ID
  3. --uuid
  4. most-recent algorithm-mode row across all sessions

Priority 2 reads CLAUDE_SESSION_ID. Per the official env-vars doc, Claude Code exposes neither CLAUDE_SESSION_ID nor CLAUDE_CODE_SESSION_ID as a general Bash env var — the documented session id is CLAUDE_CODE_BRIDGE_SESSION_ID (Remote Control) / CLAUDE_CODE_REMOTE_SESSION_ID (cloud). So priority 2 never matches, and every bare invocation falls through to priority 4, which guesses the most-recently-active algorithm row. With more than one live session, that guess can be wrong — a silent write to the wrong session.

Docs discrepancy worth surfacing

The changelog (v2.1.163) says stdio MCP servers "receive the same CLAUDE_CODE_SESSION_ID as hooks/Bash on --resume" — naming a variable that env-vars.md says isn't exposed (it documents CLAUDE_CODE_BRIDGE_SESSION_ID). The correct session var for a Bash subprocess to read is ambiguous across the official docs.

Suggested fix (two parts; the second needs no env var)

  1. Read the documented session var(s) — CLAUDE_CODE_BRIDGE_SESSION_ID / CLAUDE_CODE_REMOTE_SESSION_ID — with fallbacks, instead of the non-exposed CLAUDE_SESSION_ID.
  2. Fail closed when resolution is ambiguous (no --slug/--uuid/env match and >1 live algorithm-mode row) instead of guessing the most-recent row. The silent wrong-write is the real harm; refusing + printing an error is strictly safer for a tool that writes state.

I have a tested patch (env-var fallback + fail-closed layering + unit tests) if it's useful.

Environment: reproduced on Linux. The exact session env var differs by surface (CLI vs Agent SDK), so part 2 (surface-independent) is the durable half.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions