agent-ssh 0.3.6

Arrange agent terminal layouts and reconnect through provider attachment plans
Documentation
"""Convert a version 1 file after checking both plans with the real CLI."""
import argparse
import json
from pathlib import Path
import subprocess
import tempfile


def plan(binary, path):
    result = subprocess.run(
        [binary, "load", str(path), "--window", "migration-check", "--plan"],
        capture_output=True, text=True, check=True,
    )
    return json.loads(result.stdout)


def migrate(binary, source, destination):
    before = plan(binary, source)
    old = json.loads(source.read_text(encoding="utf-8-sig"))
    if old.get("version") != 1:
        raise ValueError("source must be a version 1 layout")
    panes = []
    for pane in old["panes"]:
        entry = {key: pane[key] for key in ("title", "cwd", "profile") if key in pane}
        entry["attachment"] = {"kind": "configured_command", "command": pane["command"]}
        panes.append(entry)
    modern = {"version": 2, "panes_per_tab": old["panes_per_tab"], "panes": panes}
    encoded = json.dumps(modern, indent=2) + "\n"
    with tempfile.TemporaryDirectory(prefix="agent-layout-migration-") as directory:
        candidate = Path(directory) / "candidate.json"
        candidate.write_text(encoded, encoding="utf-8")
        after = plan(binary, candidate)
        if before["terminal_args"] != after["terminal_args"]:
            raise ValueError("migration changed terminal commands or placement")
    with destination.open("x", encoding="utf-8") as output:
        output.write(encoded)
    print(f"Saved {len(panes)} panes to {destination}. Terminal actions are unchanged.")


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("source", type=Path)
    parser.add_argument("destination", type=Path)
    parser.add_argument("--binary", default="agent-ssh")
    arguments = parser.parse_args()
    migrate(arguments.binary, arguments.source, arguments.destination)