diff --git a/bin/loop_claude/tmux.py b/bin/loop_claude/tmux.py index 9b4f1a63..8eb1653e 100644 --- a/bin/loop_claude/tmux.py +++ b/bin/loop_claude/tmux.py @@ -66,7 +66,7 @@ def listed(session: str, run: Tmux) -> list[Listed]: def start_window(window: Window, cwd: str, env: typing.Mapping[str, str], command: list[str], first: bool, run: Tmux) -> Failure | None: - """Starts command in a new window of window's session, in cwd with env added to its environment, and leaves the client where it is. first makes the session too. A Failure says why tmux started none, or why it could not set the options of the window it started, which then runs without them.""" + """Starts command in a new window of window's session, in cwd with env added to its environment, and leaves the client where it is. first makes the session too. A Failure says why tmux started none, or why it could not mark a session it made as this checkout's.""" make = ["new-session", "-d", "-s", window.session] if first else ["new-window", "-d", "-t", f"={window.session}:"] variables = [flag for name, value in env.items() for flag in ("-e", f"{name}={value}")] # tmux reads a `;` that ends an argument as the end of the command, unless a backslash precedes it. @@ -76,10 +76,12 @@ def start_window(window: Window, cwd: str, env: typing.Mapping[str, str], comman return Failure(f"tmux could not start the window {window.target}: {done.stderr.strip() or f'it exited {done.returncode}'}") # set-option and show-options read a target as a pane's, so `=session` alone names no session. owned = run(["set-option", "-t", f"={window.session}:", OWNER, cwd]) if first else None - # By the ID, since a window of the same name may still be closing. A loop that fails before the option is set closes its window, and its stop is still sent as a DM. - kept = run(["set-option", "-w", "-t", done.stdout.strip(), "remain-on-exit", "failed"]) - unset = next((option for option in (owned, kept) if option is not None and option.returncode), None) - return Failure(f"tmux started the window {window.target} but could not set its options: {unset.stderr.strip() or f'it exited {unset.returncode}'}") if unset else None + # By the ID, since a window of the same name may still be closing. A loop that fails before the option is set closes its window, and its stop is still sent as a DM, so a failure here doesn't matter. + run(["set-option", "-w", "-t", done.stdout.strip(), "remain-on-exit", "failed"]) + # Without the owner, start_chunks cannot tell this checkout's session from another's, so no further chunk may start in it. + if owned is not None and owned.returncode: + return Failure(f"tmux started the session {window.session} but could not mark it as this checkout's: {owned.stderr.strip() or f'it exited {owned.returncode}'}") + return None def owner(session: str, run: Tmux) -> str: