feat(dashboard): spawn claude in new tmux window when \$TMUX is set
test / unit (pull_request) Successful in 18s
test / integration (pull_request) Successful in 1m10s

Option 3 from `docs/research/claude-code-pane-in-dashboard.md`,
opt-in by environment. When the dashboard runs inside tmux
(\$TMUX is set), both the new-agent (`n`) attach AND the
re-attach (Enter) paths spawn claude with
`tmux new-window -n <slug> docker exec -it … claude …` instead
of taking over the terminal via `curses.endwin`. The dashboard
keeps rendering in its current tmux pane; the operator switches
to the new window via tmux's normal nav.

Outside tmux the existing handoff path is unchanged — the
dispatch is a single `_in_tmux()` check per attach.

Mechanics:

  - `DockerBottle.claude_docker_argv` extracted from `exec_claude`,
    so both subprocess.run AND `tmux new-window` can build on the
    same docker-exec argv (preserving `--append-system-prompt-file`).
  - `_attach_via_tmux` in dashboard.py wraps the docker argv with
    `tmux new-window -n <slug> …` and returns immediately. Status
    line: `[slug] opened in new tmux window`.
  - `_build_tmux_attach_argv` split out as a pure helper so the
    wrapping shape is unit-tested without shelling out.

467 unit tests pass (2 new for `_build_tmux_attach_argv`).
This commit is contained in:
2026-05-26 14:00:03 -04:00
parent c8c72debff
commit 62ceab7485
3 changed files with 95 additions and 11 deletions
+57 -9
View File
@@ -690,18 +690,60 @@ def _stop_bottle_flow(
return f"[{slug}] stopped"
def _in_tmux() -> bool:
"""True when the dashboard is running inside a tmux session.
Tmux sets `$TMUX` to the path of its server socket; if it's
set, we can shell out to `tmux new-window` to spawn claude
sessions as siblings of the dashboard pane."""
return bool(os.environ.get("TMUX"))
def _build_tmux_attach_argv(docker_argv: list[str], slug: str) -> list[str]:
"""Wrap a docker-exec argv with `tmux new-window -n <slug>` so
the spawned claude session lives in its own tmux window
named after the bottle. Pure function — easy to unit-test
without shelling out to tmux."""
return ["tmux", "new-window", "-n", slug, *docker_argv]
def _attach_via_tmux(
bottle,
slug: str,
*,
resume: bool,
remote_control: bool = False,
) -> str:
"""Spawn claude inside `bottle` in a new tmux window. Returns
immediately — the dashboard stays running in its current
tmux pane and the operator switches to the new window via
tmux's normal nav (default `C-b n`)."""
claude_args = ["--dangerously-skip-permissions"]
if remote_control:
claude_args.append("--remote-control")
if resume:
claude_args.append("--continue")
docker_argv = bottle.claude_docker_argv(claude_args)
argv = _build_tmux_attach_argv(docker_argv, slug)
result = subprocess.run(
argv, capture_output=True, text=True, check=False,
)
if result.returncode != 0:
err = (result.stderr or "").strip() or "unknown tmux error"
return f"tmux new-window failed: {err}"
return f"[{slug}] opened in new tmux window"
def _attach_to_bottle(
stdscr: "curses._CursesWindow",
bottle,
slug: str,
) -> str:
"""Handoff: curses.endwin → attach claude → curses refresh.
Re-entry into a running bottle from the dashboard always
passes `--resume` so claude picks up its prior conversation
rather than starting a fresh transcript — the first attach
happens via `_new_agent_flow` which sets up the transcript
in the first place. Returns the post-attach status-line
message."""
"""Re-attach to a running bottle. In tmux mode the claude
session opens in a new tmux window (dashboard keeps running
in this pane); without tmux it's a curses-endwin handoff that
blocks until the operator exits claude."""
if _in_tmux():
return _attach_via_tmux(bottle, slug, resume=True)
curses.endwin()
try:
exit_code = attach_claude(bottle, remote_control=False, resume=True)
@@ -761,8 +803,7 @@ def _new_agent_flow(
backend = get_bottle_backend()
# Launch step writes to stderr (image build, network create,
# compose up). Get out of curses' way for the duration so
# the lines render cleanly. The handoff stays endwin'd until
# claude exits, then we refresh.
# the lines render cleanly.
curses.endwin()
try:
cm = backend.launch(plan)
@@ -773,6 +814,13 @@ def _new_agent_flow(
raise
bottles[plan.slug] = (cm, bottle, identity)
if _in_tmux():
# Bottle is up; spawn claude in a new tmux window and
# let the dashboard come back to the foreground in this
# pane. First-attach so no `--continue`.
stdscr.refresh()
return _attach_via_tmux(bottle, plan.slug, resume=False)
try:
exit_code = attach_claude(bottle, remote_control=False)
capture_session_state(identity, exit_code)