52 lines
1.5 KiB
Python
52 lines
1.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Register this PID as anchor, then exec the real `mosaic yolo` launcher."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
import os
|
|
import socket
|
|
import sys
|
|
|
|
|
|
def request(socket_path: str, payload: dict[str, object]) -> dict[str, object]:
|
|
conn = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
|
conn.connect(socket_path)
|
|
conn.sendall((json.dumps(payload) + "\n").encode())
|
|
response = json.loads(conn.makefile("r", encoding="utf-8").readline())
|
|
conn.close()
|
|
return response
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--socket", required=True)
|
|
parser.add_argument("runtime", choices=["pi", "claude"])
|
|
parser.add_argument("args", nargs=argparse.REMAINDER)
|
|
ns = parser.parse_args()
|
|
|
|
response = request(ns.socket, {"action": "register-anchor", "runtime": ns.runtime})
|
|
if response.get("decision") != "ACCEPT":
|
|
raise SystemExit("anchor registration refused")
|
|
os.environ["GATE0_SESSION_ID"] = str(response["session_id"])
|
|
argv = ["mosaic", "yolo", ns.runtime, *ns.args]
|
|
print(
|
|
json.dumps(
|
|
{
|
|
"event": "anchor-exec",
|
|
"pid": os.getpid(),
|
|
"argv": ["mosaic", "yolo", ns.runtime, f"<{len(ns.args)} runtime args>"],
|
|
"note": "os.execvpe retains pid and /proc starttime",
|
|
},
|
|
sort_keys=True,
|
|
),
|
|
file=sys.stderr,
|
|
flush=True,
|
|
)
|
|
os.execvpe("mosaic", argv, os.environ)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|