"""Environment variable cleanup for running inside Claude Code sessions.

When launched from Claude Code (or a tmux session that inherited its env),
vars like CLAUDECODE=1 and CLAUDE_CODE_ENTRYPOINT=cli leak into the process.
The SDK's SubprocessCLITransport passes these through to the `claude` CLI
subprocess, which detects them and refuses to start with:
"Error: Claude Code cannot be launched inside another Claude Code session."

The SDK itself sets CLAUDE_CODE_ENTRYPOINT=sdk-py when it calls query(),
so clearing these vars doesn't break the SDK's own protocol.
"""

from __future__ import annotations

import os


def clean_claude_env() -> list[str]:
    """Remove all CLAUDE* env vars except ANTHROPIC_API_KEY.

    Call this at program startup, before any SDK imports or usage.
    Returns the list of variable names that were removed.
    """
    removed: list[str] = []
    for var in list(os.environ):
        if var.startswith("CLAUDE") and var != "ANTHROPIC_API_KEY":
            os.environ.pop(var, None)
            removed.append(var)
    return removed
