Installs it from ask-form.xyz/install.sh into ~/.local/bin when missing (not the cli/ask-form hint), runs ask-form setup --events-url at the sidecar's bind:port only when serve's events URL differs, and puts its dir on the service PATH. WORKER_ASKFORM=0 opts out (kept for update). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
121 lines
4.6 KiB
Python
121 lines
4.6 KiB
Python
"""Write the worker's service definition (called by install.sh, which passes
|
|
everything through the environment): a launchd agent plist on macOS, a
|
|
systemd --user unit + its EnvironmentFile on Linux. Same environment either
|
|
way — that is what the installer reads back on ``update``.
|
|
|
|
KeepAlive / Restart=always restarts the worker if it dies — including at login,
|
|
when Tailscale may not be up yet and binding its address fails for the first
|
|
few seconds. AbandonProcessGroup / KillMode=process keep the service manager
|
|
from killing in-flight `claude -p` runs (and a running auto-update) when the
|
|
worker restarts: they're daemons (their own session), and a restarted worker
|
|
re-discovers them from its pidfiles, exactly like the host sidecar under
|
|
systemd's KillMode=process."""
|
|
|
|
import os
|
|
import pathlib
|
|
import plistlib
|
|
|
|
e = os.environ
|
|
logs = e["LOGS"]
|
|
env = {
|
|
"HOME": os.path.expanduser("~"),
|
|
"PATH": e["RUN_PATH"],
|
|
"SIDECAR_BIND": e["BIND"],
|
|
"SIDECAR_PORT": e["PORT"],
|
|
"SIDECAR_CWD": e["CWD"],
|
|
# This machine's own `claude` login is the only account here; the id is
|
|
# what the hub calls it (`personal`, or `work` on a work seat). Remote
|
|
# Control stays off on every worker: org-disabled on a work seat (and a
|
|
# work run must never list itself there anyway), and a worker run is
|
|
# already watched live by the hub.
|
|
"SIDECAR_DEFAULT_ACCOUNT": e["ACCOUNT"],
|
|
"SIDECAR_REMOTE_CONTROL": "0",
|
|
"SIDECAR_LOG_DIR": os.path.join(logs, "runs"),
|
|
"CLAUDE_BIN": e["CLAUDE_BIN"],
|
|
"WORKER_STATE_DIR": e["STATE"],
|
|
"WORKER_LOG_DIR": logs,
|
|
"PYTHONUNBUFFERED": "1",
|
|
# Whether the opencode question was answered yes (1) or no (0) — so
|
|
# `update` keeps the answer instead of asking again.
|
|
"WORKER_OPENCODE": e.get("WORKER_OPENCODE", "0"),
|
|
# …and the agy question's.
|
|
"WORKER_AGY": e.get("WORKER_AGY", "0"),
|
|
# …and whether ask-form is set up here (WORKER_ASKFORM=0 opts out).
|
|
"WORKER_ASKFORM": e.get("WORKER_ASKFORM", "1"),
|
|
}
|
|
# The installer's own clone: POST /update runs this to follow the hub
|
|
# (sidecar/selfupdate.py). Unset for a dev checkout — it never self-updates.
|
|
if e.get("UPDATER"):
|
|
env["WORKER_UPDATER"] = e["UPDATER"]
|
|
# The opencode harness, when the installer set it up: its binary, and this
|
|
# machine's opencode account under the hub's id (opencode's own data dir, so a
|
|
# terminal `opencode` here shares the login and its sessions show up too).
|
|
if e.get("OPENCODE_ACCOUNTS"):
|
|
env.update({
|
|
"OPENCODE_BIN": e["OPENCODE_BIN"],
|
|
"SIDECAR_OPENCODE_ACCOUNTS": e["OPENCODE_ACCOUNTS"],
|
|
})
|
|
|
|
# The agy harness, when the installer set it up: its binary and this machine's
|
|
# Google login under the hub's id (agy's own ~/.gemini, shared with a terminal
|
|
# `agy` here, whose conversations show up too).
|
|
if e.get("AGY_ACCOUNTS"):
|
|
env.update({
|
|
"AGY_BIN": e["AGY_BIN"],
|
|
"SIDECAR_AGY_ACCOUNTS": e["AGY_ACCOUNTS"],
|
|
})
|
|
|
|
program = [e["PY"], os.path.join(e["SIDECAR"], "sidecar.py")]
|
|
|
|
if e["OS"] == "macos":
|
|
plist = {
|
|
"Label": e["LABEL"],
|
|
"ProgramArguments": program,
|
|
"WorkingDirectory": e["SIDECAR"],
|
|
"RunAtLoad": True,
|
|
"KeepAlive": True,
|
|
"ThrottleInterval": 10,
|
|
"AbandonProcessGroup": True,
|
|
"ProcessType": "Interactive",
|
|
"StandardOutPath": os.path.join(logs, "worker.log"),
|
|
"StandardErrorPath": os.path.join(logs, "worker.log"),
|
|
"EnvironmentVariables": env,
|
|
}
|
|
with open(e["PLIST"], "wb") as f:
|
|
plistlib.dump(plist, f)
|
|
else:
|
|
def quote(v: str) -> str:
|
|
# systemd EnvironmentFile: double quotes, with \ and " escaped.
|
|
return '"' + v.replace("\\", "\\\\").replace('"', '\\"') + '"'
|
|
|
|
env_file = pathlib.Path(e["ENV_FILE"])
|
|
env_file.parent.mkdir(parents=True, exist_ok=True)
|
|
fd = os.open(env_file, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
|
|
with os.fdopen(fd, "w") as f:
|
|
f.write("# written by worker/install.sh — re-run it rather than editing\n")
|
|
f.writelines(f"{k}={quote(v)}\n" for k, v in env.items())
|
|
log = os.path.join(logs, "worker.log")
|
|
unit = pathlib.Path(e["UNIT_FILE"])
|
|
unit.parent.mkdir(parents=True, exist_ok=True)
|
|
unit.write_text(f"""\
|
|
# written by worker/install.sh — re-run it rather than editing
|
|
[Unit]
|
|
Description=ai-agent worker (sidecar on the tailnet)
|
|
After=network-online.target
|
|
Wants=network-online.target
|
|
|
|
[Service]
|
|
Type=simple
|
|
EnvironmentFile={env_file}
|
|
WorkingDirectory={e["SIDECAR"]}
|
|
ExecStart={" ".join(program)}
|
|
Restart=always
|
|
RestartSec=10
|
|
KillMode=process
|
|
StandardOutput=append:{log}
|
|
StandardError=append:{log}
|
|
|
|
[Install]
|
|
WantedBy=default.target
|
|
""")
|