Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions Resources/bin/cmux-claude-wrapper
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,13 @@ reconcile_claude_config_dir_for_resume() {
done
}

reconcile_claude_config_dir_for_resume_unless_preserved() {
should_preserve_claude_auth_selection_key "CLAUDE_CONFIG_DIR" && return 0
local resume_session_id
resume_session_id="$(extract_claude_resume_session_id "$@" || true)"
reconcile_claude_config_dir_for_resume "$resume_session_id"
}

clear_inherited_claude_auth_selection_env() {
if [[ "${IN_CMUX:-0}" == "1" ]]; then
local key
Expand Down Expand Up @@ -585,7 +592,7 @@ if [[ "$CMUX_CLAUDE_HOOKS_DISABLED" == "1" ]]; then
fi
clear_inherited_claude_auth_selection_env
if [[ "$IN_CMUX" == "1" ]]; then
reconcile_claude_config_dir_for_resume "$(extract_claude_resume_session_id "$@")"
reconcile_claude_config_dir_for_resume_unless_preserved "$@"
fi
REAL_CLAUDE="$(find_real_claude)" || { echo "Error: claude not found in PATH" >&2; exit 127; }
cmux_claude_wrapper_exec_resolved_claude "$REAL_CLAUDE" "$@"
Expand All @@ -599,7 +606,7 @@ if [[ "$IN_CMUX" == "0" ]] || ! cmux_socket_available; then
fi
clear_inherited_claude_auth_selection_env
if [[ "$IN_CMUX" == "1" ]]; then
reconcile_claude_config_dir_for_resume "$(extract_claude_resume_session_id "$@")"
reconcile_claude_config_dir_for_resume_unless_preserved "$@"
fi
REAL_CLAUDE="$(find_real_claude)" || { echo "Error: claude not found in PATH" >&2; exit 127; }
exec_real_claude_passthrough "$@"
Expand Down Expand Up @@ -851,7 +858,9 @@ fi
# For an explicit `--resume <id>`, point CLAUDE_CONFIG_DIR at the config root that
# actually holds the transcript so an inherited/foreign config dir cannot turn the
# resume into "No conversation found". https://github.com/manaflow-ai/cmux/issues/6194
reconcile_claude_config_dir_for_resume "$(extract_claude_resume_session_id "$@")"
# Account managers such as `sr claude` can intentionally pin CLAUDE_CONFIG_DIR to
# a different auth/profile root and ask cmux to preserve it.
reconcile_claude_config_dir_for_resume_unless_preserved "$@"

# Export the wrapper's PID. Because we exec claude below, this PID becomes
# the actual claude process PID, which hooks use for stale-session detection.
Expand Down
40 changes: 40 additions & 0 deletions tests/test_claude_wrapper_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1410,6 +1410,45 @@ def test_live_socket_preserves_claude_auth_for_resume_launch(failures: list[str]
expect("--session-id" not in real_argv, f"resume auth env: expected no injected session id, got {real_argv}", failures)


def test_preserved_claude_config_dir_skips_resume_self_heal(failures: list[str]) -> None:
session_id = "582054f9-2f87-4e90-ad8d-2f532ed8c61b"

for socket_state in ("live", "stale"):
expected: dict[str, str] = {}

def setup_env(tmp: Path) -> dict[str, str]:
home = tmp / "home"
default_root = home / ".claude"
(default_root / "projects" / "-work").mkdir(parents=True)
(default_root / "projects" / "-work" / f"{session_id}.jsonl").write_text(
"{}\n", encoding="utf-8"
)
selected_root = home / ".subrouter" / "codex" / "claude" / "aziz-claude-1"
(selected_root / "projects").mkdir(parents=True)
expected["path"] = str(selected_root)
return {
"HOME": str(home),
"CLAUDE_CONFIG_DIR": str(selected_root),
"CMUX_PRESERVE_CLAUDE_AUTH_SELECTION_ENV": "1",
"CMUX_PRESERVE_CLAUDE_AUTH_SELECTION_ENV_KEYS": "CLAUDE_CONFIG_DIR",
}

code, auth_env, real_argv, stderr = run_wrapper_auth_env(
argv=["--resume", session_id, "--fork-session"],
inherited_env={},
socket_state=socket_state,
setup_env=setup_env,
)
expect(code == 0, f"preserved resume config dir {socket_state}: wrapper exited {code}: {stderr}", failures)
expect(
auth_env.get("CLAUDE_CONFIG_DIR") == expected["path"],
f"preserved resume config dir {socket_state}: expected CLAUDE_CONFIG_DIR to remain on the selected profile root "
f"{expected['path']!r}, got {auth_env.get('CLAUDE_CONFIG_DIR')!r}",
failures,
)
expect(real_argv[-3:] == ["--resume", session_id, "--fork-session"], f"preserved resume config dir {socket_state}: expected resume+fork argv, got {real_argv}", failures)
Comment on lines +1413 to +1449

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Bind expected in the setup_env closure to satisfy Ruff B023.

Static analysis flags the closure at Line 1428 capturing the loop variable expected by reference (B023). It's functionally safe here since setup_env is invoked synchronously within the same loop iteration before expected is reassigned, but binding it explicitly avoids relying on that ordering and silences the lint warning.

🧹 Proposed fix to bind the loop variable
-        def setup_env(tmp: Path) -> dict[str, str]:
+        def setup_env(tmp: Path, expected: dict[str, str] = expected) -> dict[str, str]:
             home = tmp / "home"
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def test_preserved_claude_config_dir_skips_resume_self_heal(failures: list[str]) -> None:
session_id = "582054f9-2f87-4e90-ad8d-2f532ed8c61b"
for socket_state in ("live", "stale"):
expected: dict[str, str] = {}
def setup_env(tmp: Path) -> dict[str, str]:
home = tmp / "home"
default_root = home / ".claude"
(default_root / "projects" / "-work").mkdir(parents=True)
(default_root / "projects" / "-work" / f"{session_id}.jsonl").write_text(
"{}\n", encoding="utf-8"
)
selected_root = home / ".subrouter" / "codex" / "claude" / "aziz-claude-1"
(selected_root / "projects").mkdir(parents=True)
expected["path"] = str(selected_root)
return {
"HOME": str(home),
"CLAUDE_CONFIG_DIR": str(selected_root),
"CMUX_PRESERVE_CLAUDE_AUTH_SELECTION_ENV": "1",
"CMUX_PRESERVE_CLAUDE_AUTH_SELECTION_ENV_KEYS": "CLAUDE_CONFIG_DIR",
}
code, auth_env, real_argv, stderr = run_wrapper_auth_env(
argv=["--resume", session_id, "--fork-session"],
inherited_env={},
socket_state=socket_state,
setup_env=setup_env,
)
expect(code == 0, f"preserved resume config dir {socket_state}: wrapper exited {code}: {stderr}", failures)
expect(
auth_env.get("CLAUDE_CONFIG_DIR") == expected["path"],
f"preserved resume config dir {socket_state}: expected CLAUDE_CONFIG_DIR to remain on the selected profile root "
f"{expected['path']!r}, got {auth_env.get('CLAUDE_CONFIG_DIR')!r}",
failures,
)
expect(real_argv[-3:] == ["--resume", session_id, "--fork-session"], f"preserved resume config dir {socket_state}: expected resume+fork argv, got {real_argv}", failures)
def test_preserved_claude_config_dir_skips_resume_self_heal(failures: list[str]) -> None:
session_id = "582054f9-2f87-4e90-ad8d-2f532ed8c61b"
for socket_state in ("live", "stale"):
expected: dict[str, str] = {}
def setup_env(tmp: Path, expected: dict[str, str] = expected) -> dict[str, str]:
home = tmp / "home"
default_root = home / ".claude"
(default_root / "projects" / "-work").mkdir(parents=True)
(default_root / "projects" / "-work" / f"{session_id}.jsonl").write_text(
"{}\n", encoding="utf-8"
)
selected_root = home / ".subrouter" / "codex" / "claude" / "aziz-claude-1"
(selected_root / "projects").mkdir(parents=True)
expected["path"] = str(selected_root)
return {
"HOME": str(home),
"CLAUDE_CONFIG_DIR": str(selected_root),
"CMUX_PRESERVE_CLAUDE_AUTH_SELECTION_ENV": "1",
"CMUX_PRESERVE_CLAUDE_AUTH_SELECTION_ENV_KEYS": "CLAUDE_CONFIG_DIR",
}
code, auth_env, real_argv, stderr = run_wrapper_auth_env(
argv=["--resume", session_id, "--fork-session"],
inherited_env={},
socket_state=socket_state,
setup_env=setup_env,
)
expect(code == 0, f"preserved resume config dir {socket_state}: wrapper exited {code}: {stderr}", failures)
expect(
auth_env.get("CLAUDE_CONFIG_DIR") == expected["path"],
f"preserved resume config dir {socket_state}: expected CLAUDE_CONFIG_DIR to remain on the selected profile root "
f"{expected['path']!r}, got {auth_env.get('CLAUDE_CONFIG_DIR')!r}",
failures,
)
expect(real_argv[-3:] == ["--resume", session_id, "--fork-session"], f"preserved resume config dir {socket_state}: expected resume+fork argv, got {real_argv}", failures)
🧰 Tools
🪛 Ruff (0.15.20)

[warning] 1428-1428: Function definition does not bind loop variable expected

(B023)

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/test_claude_wrapper_hooks.py` around lines 1413 - 1449, The setup_env
closure in test_preserved_claude_config_dir_skips_resume_self_heal captures the
loop variable expected by reference, triggering Ruff B023. Bind the current
expected value inside the loop before defining setup_env, and have the closure
use that bound local instead so each iteration uses its own dictionary
consistently. Keep the rest of the test logic the same, including the auth_env
assertion against the selected_root path.

Source: Linters/SAST tools



def test_live_socket_preserves_only_listed_claude_auth_keys(failures: list[str]) -> None:
inherited = {
"CLAUDE_CONFIG_DIR": "/tmp/claude-config",
Expand Down Expand Up @@ -1883,6 +1922,7 @@ def main() -> int:
test_live_socket_resume_keeps_correct_claude_config_dir(failures)
test_live_socket_resume_self_heal_ignores_prompt_text_after_double_dash(failures)
test_live_socket_preserves_claude_auth_for_resume_launch(failures)
test_preserved_claude_config_dir_skips_resume_self_heal(failures)
test_live_socket_preserves_only_listed_claude_auth_keys(failures)
test_live_socket_auto_preserves_vertex_auth_when_truthy(failures)
test_live_socket_auto_preserves_bedrock_auth_when_truthy(failures)
Expand Down
Loading