Skip to content

Instantly share code, notes, and snippets.

@roktas
Created August 7, 2026 23:18
Show Gist options
  • Select an option

  • Save roktas/52760edfe0cc246c9a6c8becf63b7357 to your computer and use it in GitHub Desktop.

Select an option

Save roktas/52760edfe0cc246c9a6c8becf63b7357 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import json
import os
import selectors
import subprocess
import sys
from pathlib import Path
ALL_HOSTS = ("newton", "spinoza", "kant", "gauss", "aristo")
HOST_COLORS = {
"newton": 109,
"spinoza": 110,
"kant": 139,
"gauss": 143,
"aristo": 174,
}
PALETTE = (109, 110, 139, 143, 174)
RESET = "\033[0m"
BOLD = "\033[1m"
DIM = "\033[2m"
def ansi_color(code):
return f"\033[38;5;{code}m"
def host_color(host, index):
name = host[4:] if host.startswith("ssh:") else host
return ansi_color(HOST_COLORS.get(name, PALETTE[index % len(PALETTE)]))
def worker_prompt(host):
return f"""Use the Tilde skill and run one host workflow now.
Execute the Tilde update workflow for host "{host}" as an authorized, fully autonomous operation. This worker owns only "{host}"; do not expand the target to ALL or touch any other host. Perform this host's complete reachability and freshness preflight, target-local status, plan, apply, final verification, and required cleanup according to the Tilde skill.
If the host is unreachable, report it as skipped. If it produces conflict, deferred, notok, a sudo or authentication failure, a host-key or trust failure, or a lost observation, stop this host without blind retries. Never use raw SSH or run implementation routes from the controller. Return a compact plain-text report for "{host}"; do not emit JSON, JSONL, or code fences."""
def worker_command(host, prompt):
return [
"codex",
"exec",
"--model",
"gpt-5.6-luna",
"--config",
'model_reasoning_effort="high"',
"--cd",
str(Path.home()),
"--skip-git-repo-check",
"--ephemeral",
"--dangerously-bypass-approvals-and-sandbox",
"--json",
"--color",
"never",
prompt,
]
def classify(line):
try:
event = json.loads(line)
except json.JSONDecodeError:
return "C", None
event_type = event.get("type")
item = event.get("item") or {}
if event_type == "item.completed" and item.get("type") == "agent_message":
return "M", item.get("text", "")
if event_type == "turn.completed":
return "F", None
if event_type in {"item.started", "thread.started", "turn.started"}:
return "S", None
return "C", None
def run(host_args):
selected = list(ALL_HOSTS) if not host_args else []
for host in host_args:
selected.extend(ALL_HOSTS if host == "ALL" else (host,))
hosts = []
for host in selected:
if host not in hosts:
hosts.append(host)
ui = {"progress_open": False}
selector = selectors.DefaultSelector()
workers = []
overall_status = 0
def write(text):
sys.stdout.write(text)
sys.stdout.flush()
def show_progress(worker, glyph):
write(f"{DIM}{worker['color']}{glyph}{RESET}")
ui["progress_open"] = True
def show_message(worker, text, bold=False):
if ui["progress_open"]:
write("\n")
ui["progress_open"] = False
style = BOLD if bold else ""
write(f"{style}{worker['color']}{text}{RESET}\n")
def flush_pending(worker):
if worker["pending"] is not None:
show_message(worker, worker["pending"])
worker["pending"] = None
def handle_line(worker, line):
kind, text = classify(line)
if kind == "M":
flush_pending(worker)
worker["pending"] = str(text)
elif kind == "F":
show_progress(worker, "·")
if worker["pending"] is not None:
show_message(worker, worker["pending"], bold=True)
worker["pending"] = None
elif kind == "S":
flush_pending(worker)
show_progress(worker, "*")
else:
flush_pending(worker)
show_progress(worker, "·")
header = " ".join(
f"{BOLD}{host_color(host, index)}{host}{RESET}"
for index, host in enumerate(hosts)
)
write(f"{header}\n\n")
try:
for index, host in enumerate(hosts):
prompt = worker_prompt(host)
worker = {
"color": host_color(host, index),
"pending": None,
"process": None,
"buffer": b"",
"start_error": None,
}
try:
process = subprocess.Popen(
worker_command(host, prompt),
stdin=subprocess.DEVNULL,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
bufsize=0,
)
except OSError as error:
worker["start_error"] = error
worker["returncode"] = 127
overall_status = 1
else:
worker["process"] = process
selector.register(process.stdout, selectors.EVENT_READ, worker)
workers.append(worker)
while selector.get_map():
for key, _ in selector.select():
worker = key.data
stream = key.fileobj
chunk = os.read(stream.fileno(), 4096)
if chunk:
worker["buffer"] += chunk
while b"\n" in worker["buffer"]:
line, worker["buffer"] = worker["buffer"].split(b"\n", 1)
handle_line(
worker,
line.rstrip(b"\r").decode("utf-8", "replace"),
)
continue
selector.unregister(stream)
stream.close()
process = worker["process"]
worker["returncode"] = process.wait()
if worker["buffer"]:
handle_line(
worker,
worker["buffer"].rstrip(b"\r").decode("utf-8", "replace"),
)
worker["buffer"] = b""
if worker["pending"] is not None:
show_message(
worker,
worker["pending"],
bold=worker["returncode"] == 0,
)
worker["pending"] = None
if worker["returncode"] != 0:
show_message(
worker,
f"worker exited with status {worker['returncode']}",
)
overall_status = 1
for worker in workers:
if worker["start_error"] is not None:
show_message(
worker,
f"worker could not start: {worker['start_error']}",
)
except KeyboardInterrupt:
overall_status = 130
for worker in workers:
process = worker["process"]
if process is not None and process.poll() is None:
process.terminate()
for worker in workers:
process = worker["process"]
if process is not None:
process.wait()
finally:
selector.close()
if ui["progress_open"]:
write("\n")
return overall_status
def main():
return run(sys.argv[1:])
if __name__ == "__main__":
raise SystemExit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment