diff --git a/PROTOCOL.md b/PROTOCOL.md index 50d8516..42322f9 100644 --- a/PROTOCOL.md +++ b/PROTOCOL.md @@ -122,6 +122,8 @@ Sessions are herdr (https://herdr.dev) terminals, not a diwan-invented session s To resume a `DIWAN_ID`, `diwan` first checks whether its agent is still alive (`herdr agent get `) and just prompts it if so; if the process has exited (normal between ticks), it finds the tab by label and starts a fresh agent in the same pane, in the same working directory — the heading's own content is what carries continuity, not agent conversation history. `diwan` shells out to the real `herdr` CLI (the `diwan` CLI has the exact calls, verified live against herdr 0.9.5; run `herdr --skill` to check this hasn't drifted on a newer version). When `herdr` isn't on PATH, every call prints what it would have run instead of failing, so the pipeline stays testable without herdr installed. +Bare `diwan` (no subcommand) focuses the local `diwan` workspace and attaches you to it interactively. `diwan remote ` does the same against a herdr machine saved via `herdr machine add ` (see herdr's own `--help`/`--skill` for setting one up) — it focuses that machine's `diwan` workspace over the scripted `--machine` API, then execs `herdr --remote ` to open an interactive attach into it. This relies on `` being usable both as a saved machine label (for the focus step) and as a raw `--remote` SSH target (for the attach) — true by default, since `herdr machine add` labels a profile after its SSH target unless `--label` overrides it. + ## The harness is `pi` diwan dispatches `pi`, and only `pi` — not a pluggable choice. It's the only herdr-supported agent kind diwan has ever needed that runs real work fully unattended: it has no per-tool-call approval gate at all (unlike e.g. Claude Code, which otherwise prompts before every edit/bash call and would defeat the "act first, report passively" model), so no permission-mode flag or similar is needed to let it act freely. The one thing that can still block a dispatch is pi's one-time project-trust decision for protected `.pi/*` resources, if a content repo ever has any — `--approve` (always passed, see the `diwan` CLI) makes that a one-shot yes instead of a hang; override with `DIWAN_PI_APPROVE` if a different flag is ever needed. `TICK.md` and `DO.md` are still plain-language procedures with nothing pi-specific baked into their wording — that's just good practice, not a promise of swapping in a different harness later. diff --git a/diwan b/diwan index f6a3f85..ed9496d 100755 --- a/diwan +++ b/diwan @@ -269,22 +269,26 @@ class HerdrError(RuntimeError): pass -def herdr_json(*args): - res = subprocess.run(["herdr", *args], cwd=DIWAN_NOTES_DIR, capture_output=True, text=True) +def herdr_json(*args, machine=None): + cmd = ["herdr"] + if machine: + cmd += ["--machine", machine] + cmd += list(args) + res = subprocess.run(cmd, cwd=DIWAN_NOTES_DIR, capture_output=True, text=True) if res.returncode != 0: - raise HerdrError(f"herdr {' '.join(args)} failed: {res.stderr.strip() or res.stdout.strip()}") + raise HerdrError(f"{' '.join(cmd)} failed: {res.stderr.strip() or res.stdout.strip()}") try: return json.loads(res.stdout) except json.JSONDecodeError: - raise HerdrError(f"herdr {' '.join(args)}: non-JSON output: {res.stdout.strip()}") + raise HerdrError(f"{' '.join(cmd)}: non-JSON output: {res.stdout.strip()}") -def herdr_diwan_workspace_id(): - data = herdr_json("workspace", "list") +def herdr_diwan_workspace_id(machine=None): + data = herdr_json("workspace", "list", machine=machine) for ws in data["result"]["workspaces"]: if ws.get("label") == DIWAN_WORKSPACE_LABEL: return ws["workspace_id"] - created = herdr_json("workspace", "create", "--label", DIWAN_WORKSPACE_LABEL, "--no-focus") + created = herdr_json("workspace", "create", "--label", DIWAN_WORKSPACE_LABEL, "--no-focus", machine=machine) return created["result"]["workspace"]["workspace_id"] @@ -393,6 +397,26 @@ def cmd_bare(args): os.execvp("herdr", ["herdr"]) +def cmd_remote(args): + machine = args.machine + if not herdr_available(): + print("herdr not found on PATH. Install herdr: https://herdr.dev/") + return + # `machine` must be both a saved `herdr machine add` profile (for the + # scripted focus call below, over --machine) and a valid --remote SSH + # target for the actual interactive attach -- true by default, since + # `herdr machine add ` labels the profile after the SSH target + # unless --label overrides it. If you used a custom --label, pass the + # original SSH target here instead, or just run `herdr --remote + # ` yourself and navigate to the 'diwan' workspace by hand. + try: + ws_id = herdr_diwan_workspace_id(machine=machine) + herdr_json("workspace", "focus", ws_id, machine=machine) + except HerdrError as e: + print(f"warning: couldn't focus the '{DIWAN_WORKSPACE_LABEL}' workspace on '{machine}': {e}", file=sys.stderr) + os.execvp("herdr", ["herdr", "--remote", machine]) + + def cmd_tick(args): STATE_DIR.mkdir(exist_ok=True) if git_is_dirty(): @@ -533,6 +557,9 @@ def main(): p_do.add_argument("target", help="file:lineno pointing at a heading, or a free-text prompt") p_do.add_argument("--diwan-id", dest="diwan_id", default=None) + p_remote = sub.add_parser("remote", help="open the 'diwan' workspace on a remote herdr machine") + p_remote.add_argument("machine", help="a machine saved via 'herdr machine add' (label defaults to its SSH target)") + args = parser.parse_args() if args.command is None: cmd_bare(args) @@ -540,6 +567,8 @@ def main(): cmd_tick(args) elif args.command == "do": cmd_do(args) + elif args.command == "remote": + cmd_remote(args) if __name__ == "__main__":