From 11ee50e9c7a5ae0f8a70c75a3d009e23447c8d1b Mon Sep 17 00:00:00 2001 From: Paul O'Reilly Date: Tue, 13 Oct 2026 10:01:31 +1300 Subject: [PATCH] claude-sessions: default new session to cwd project, attach after new/resume Claude-Session: https://claude.ai/code/session_01SCd4vYV3ig6CpcPV9PCsC6 --- scripts/claude-sessions | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/scripts/claude-sessions b/scripts/claude-sessions index 21eaf9e..581ce7a 100755 --- a/scripts/claude-sessions +++ b/scripts/claude-sessions @@ -333,6 +333,18 @@ def spawn_session(project_dir: str, profile: str, mode: str, return True +def find_claude_md_dir(start: Path) -> Optional[Path]: + """Nearest directory at or above start (stopping at $HOME) containing a CLAUDE.md.""" + for d in [start, *start.parents]: + if d == HOME.parent or d == Path("/"): + break + if (d / "CLAUDE.md").is_file(): + return d + if d == HOME: + break + return None + + # ── Commands ───────────────────────────────────────────────────────────────── def cmd_list(args) -> None: @@ -437,6 +449,8 @@ def cmd_recover(args) -> None: profiles_cfg = load_profiles() print() + # Land in the session only when resuming exactly one; several stay detached. + attach = len(selected) == 1 for s in selected: if not s["project_dir"]: warn(f"Skipping: project directory not found for {s['project_hash']}") @@ -444,7 +458,7 @@ def cmd_recover(args) -> None: profile_cfg = profiles_cfg.get(s["profile"], {}) mode = getattr(args, "mode", None) or profile_cfg.get("default_mode", "quick") spawn_session(project_dir=s["project_dir"], profile=s["profile"], - mode=mode, resume_id=s["session_id"]) + mode=mode, resume_id=s["session_id"], attach=attach) print() @@ -615,18 +629,26 @@ def cmd_menu(_args=None) -> None: input("\nPress Enter to return to menu…") elif raw == "n": + default_dir = find_claude_md_dir(Path.cwd()) try: - project = input(" Project name: ").strip() + if default_dir: + print(f" Found CLAUDE.md in {BOLD}{default_dir}{NC}") + project = input( + f" Project [{BOLD}{default_dir.name}{NC}] (Enter = use this, or type another): " + ).strip() + if not project: + project = str(default_dir) + else: + project = input(" Project name: ").strip() except (KeyboardInterrupt, EOFError): print() continue if not project: continue import types - a = types.SimpleNamespace(project=project, mode=None, profile=None, name=None, attach=False) + a = types.SimpleNamespace(project=project, mode=None, profile=None, name=None, attach=True) print() - cmd_spawn(a) # this exec's — won't return - input("\nPress Enter to return to menu…") + cmd_spawn(a) # exec's claude-tmux --attach; replaces this process elif raw.isdigit(): idx = int(raw) - 1 @@ -656,8 +678,7 @@ def cmd_menu(_args=None) -> None: continue print() spawn_session(project_dir=project_dir, profile=profile, - mode=mode, resume_id=session_id) - input("\nPress Enter to return to menu…") + mode=mode, resume_id=session_id, attach=True) else: print(f" {YELLOW}Unknown choice: {raw!r}{NC}") time.sleep(1)