Run do-epic <EPIC-KEY>: a tmux window per chunk, under the epic's cap and each repo's parallel-chunks, never beside loop-claude
This commit is contained in:
+35
-5
@@ -19,6 +19,8 @@ LOOP = "loop"
|
||||
EPIC = "epic"
|
||||
# The session option that names the checkout whose chunks the session runs, since two checkouts' keys may map to one session name.
|
||||
OWNER = "@loop-root"
|
||||
# The window option that names the folder a window's chunk works in, so that loop-claude and do-epic each see the chunks the other runs in a checkout.
|
||||
ROOT = "@chunk-root"
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
@@ -47,6 +49,15 @@ class Listed:
|
||||
dead: bool
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Placed:
|
||||
"""A window of any session, as tmux lists it."""
|
||||
session: str
|
||||
window: Listed
|
||||
# The folder its chunk works in, as ROOT names it; empty for a window started before ROOT was set.
|
||||
root: str
|
||||
|
||||
|
||||
def unusable(run: Tmux, driver: str) -> str | None:
|
||||
"""Why driver, the entry point, cannot use this machine's tmux, naming the version found and the one needed; None where it is NEEDED or later."""
|
||||
found = run(["-V"]).stdout.strip()
|
||||
@@ -68,8 +79,15 @@ def listed(session: str, run: Tmux) -> list[Listed]:
|
||||
return [Listed(row[0].removesuffix(WAITS), row[2], row[1] == "1") for row in rows if len(row) == 3]
|
||||
|
||||
|
||||
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 mark a session it made as this checkout's."""
|
||||
def everywhere(run: Tmux) -> list[Placed]:
|
||||
"""The windows of every session; none where no tmux server runs."""
|
||||
done = run(["list-windows", "-a", "-F", f"#{{session_name}}\t#{{window_name}}\t#{{pane_dead}}\t#{{window_id}}\t#{{{ROOT}}}"])
|
||||
rows = [line.split("\t") for line in done.stdout.splitlines()] if done.returncode == 0 else []
|
||||
return [Placed(row[0], Listed(row[1].removesuffix(WAITS), row[3], row[2] == "1"), row[4]) for row in rows if len(row) == 5]
|
||||
|
||||
|
||||
def start_window(window: Window, cwd: str, env: typing.Mapping[str, str], command: list[str], first: bool, owned: 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, and owned marks a session it makes as running the chunks of the checkout cwd. A Failure says why tmux started none, or why it could not mark the session."""
|
||||
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.
|
||||
@@ -78,15 +96,27 @@ def start_window(window: Window, cwd: str, env: typing.Mapping[str, str], comman
|
||||
if done.returncode:
|
||||
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
|
||||
marked = run(["set-option", "-t", f"={window.session}:", OWNER, cwd]) if first and owned else None
|
||||
# A failure leaves the window unseen by the other entry point, which then may start a chunk beside it; the window still runs.
|
||||
run(["set-option", "-w", "-t", done.stdout.strip(), ROOT, cwd])
|
||||
# By the ID, since a window of the same name may still be closing. A failed remain-on-exit doesn't matter: a loop that fails before it is set closes its window, and its stop is still sent as a DM.
|
||||
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}'}")
|
||||
if marked is not None and marked.returncode:
|
||||
return Failure(f"tmux started the session {window.session} but could not mark it as this checkout's: {marked.stderr.strip() or f'it exited {marked.returncode}'}")
|
||||
return None
|
||||
|
||||
|
||||
def start_chunk(window: Window, cwd: str, env: typing.Mapping[str, str], command: list[str], windows: list[Listed], owned: bool, run: Tmux) -> list[Listed] | Failure:
|
||||
"""Starts a chunk's command in window, in place of each dead window of its name among windows, the session's; the session's windows afterwards, or why tmux started none. start_window says what cwd, env and owned do."""
|
||||
stale = [listed for listed in windows if listed.name == window.name and listed.dead]
|
||||
for listed in stale:
|
||||
close(listed, run)
|
||||
kept = [listed for listed in windows if listed not in stale]
|
||||
failed = start_window(window, cwd, env, command, not kept, owned, run)
|
||||
return failed or [*kept, Listed(window.name, "", False)]
|
||||
|
||||
|
||||
def owner(session: str, run: Tmux) -> str:
|
||||
"""The checkout whose chunks session runs, as OWNER names it; empty where it names none."""
|
||||
return run(["show-options", "-qv", "-t", f"={session}:", OWNER]).stdout.strip()
|
||||
|
||||
Reference in New Issue
Block a user