The EVOX2 auto-suspends on input idle and a busy GPU doesn't count, so a
long run there died with the box. sidecar/inhibit.py holds a
`systemd-inhibit --what=sleep --mode=block` lock while pids.list_live() is
non-empty: poked on /spawn, /resume and /fork, re-checked by a small loop
(5 s held, 15 s idle) so it drops right after the last run ends, released
at lifespan teardown. The lock child is `cat` on a pipe we hold, so it also
dies if the sidecar is killed (KillMode=process would otherwise leak it).
On by default on a Linux worker (SIDECAR_INHIBIT_SLEEP=0|1), a no-op on
macOS, without the binary, or when logind/polkit refuses (reported, retried
every minute). /health reports sleepInhibitor {enabled, available, engaged}.
worker/README documents the polkit rule a service-started worker needs.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
385 lines
23 KiB
Python
385 lines
23 KiB
Python
"""
|
||
Sidecar configuration: every environment variable and constant, in one place.
|
||
|
||
Each value keeps the comment that documents it — read these before changing a
|
||
default. The other modules import what they need from here; nothing in this
|
||
file is reassigned at runtime.
|
||
"""
|
||
|
||
import os
|
||
import pathlib
|
||
import re
|
||
import sys
|
||
|
||
TOKEN = os.environ.get("SIDECAR_TOKEN", "")
|
||
CLAUDE_BIN = os.environ.get("CLAUDE_BIN", "claude")
|
||
# The pi-harness runner (runner/cli.mjs): wraps `pi --mode
|
||
# json` and mirrors the session as a Claude-Code-schema transcript the viewer
|
||
# watches. Launched with the exact same flag shape as `claude`.
|
||
RUNNER_BIN = os.environ.get(
|
||
"RUNNER_BIN",
|
||
str(pathlib.Path(__file__).resolve().parent.parent / "runner" / "cli.mjs"))
|
||
DEFAULT_CWD = os.environ.get("SIDECAR_CWD", str(pathlib.Path.home() / "homelab"))
|
||
DEFAULT_MODEL = os.environ.get("SIDECAR_MODEL", "opus")
|
||
# What a new pi-harness run uses when no model tag was picked — the EVOX2's
|
||
# Inkling 2 Mini on llm-inkling (since 2026-11-23; Qwen 3.8 27B before): free,
|
||
# and no key to expire. The runner resolves each id's provider from
|
||
# ~/.pi/agent/models.json (a locally-served id may carry a vendor prefix like
|
||
# OpenRouter's — see runner/cli.mjs). A resume or fork keeps its session's own
|
||
# model (pi_runs.session_model).
|
||
PI_DEFAULT_MODEL = os.environ.get("SIDECAR_PI_MODEL", "inkling-2-mini")
|
||
# ---- the opencode harness (opencode_cli.py, opencode_mirror.py) --------------
|
||
OPENCODE_BIN = os.environ.get("OPENCODE_BIN", "opencode")
|
||
# The provider a bare model id runs on: `openai`, i.e. the ChatGPT login.
|
||
OPENCODE_PROVIDER = os.environ.get("SIDECAR_OPENCODE_PROVIDER", "openai")
|
||
OPENCODE_DEFAULT_MODEL = os.environ.get("SIDECAR_OPENCODE_MODEL", "gpt-6-sol")
|
||
# `provider/model` or a bare id (`gpt-5.5`, `openrouter/qwen/qwen3.8-27b`).
|
||
OPENCODE_MODEL_RE = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._/:-]{0,127}$")
|
||
# opencode's reasoning variants (models.dev `reasoning_options`), sent as the
|
||
# `#variant` of the model ref. Unset sends none: the model's own default.
|
||
# `think` is the local models' own: thinking on (their default is off).
|
||
OPENCODE_EFFORTS = ("none", "minimal", "low", "medium", "high", "xhigh", "max", "think")
|
||
# What a run with no effort picked reasons at (every listed model takes it).
|
||
OPENCODE_DEFAULT_EFFORT = os.environ.get("SIDECAR_OPENCODE_EFFORT", "high")
|
||
|
||
|
||
def _parse_opencode_accounts(raw: str) -> dict[str, "pathlib.Path | None"]:
|
||
"""``SIDECAR_OPENCODE_ACCOUNTS`` → {account id: data dir | None}. ``default``
|
||
(or empty) = opencode's own data dir, shared with a terminal `opencode`."""
|
||
out: dict[str, pathlib.Path | None] = {}
|
||
for part in (raw or "").split(","):
|
||
acct, _, d = part.partition("=")
|
||
acct, d = acct.strip().lower(), d.strip()
|
||
if re.fullmatch(r"[a-z][a-z0-9_-]{0,31}", acct):
|
||
out[acct] = None if d in ("", "default") else pathlib.Path(os.path.expanduser(d))
|
||
return out
|
||
|
||
|
||
# opencode accounts (one ChatGPT login each): `chatgpt-work=default`.
|
||
OPENCODE_ACCOUNTS = _parse_opencode_accounts(os.environ.get("SIDECAR_OPENCODE_ACCOUNTS", ""))
|
||
# The ones with no ChatGPT login at all (`opencode-lab`): they run only models
|
||
# that bring their own credentials — the local providers pi's models.json
|
||
# lists — so they read as signed in and never get the default GPT effort.
|
||
OPENCODE_KEYLESS = {a.strip().lower() for a in os.environ.get(
|
||
"SIDECAR_OPENCODE_KEYLESS", "").split(",") if a.strip()}
|
||
# What a keyless account runs when a spawn names no model: the default local
|
||
# model's twin (Inkling 2 Mini on llm-inkling since 2026-11-23; the Qwen 3.6
|
||
# MoE before).
|
||
OPENCODE_LOCAL_MODEL = os.environ.get("SIDECAR_OPENCODE_LOCAL_MODEL", "evox2-inkling/inkling-2-mini")
|
||
# pi's provider registry: its local providers (`evox2*`) are mirrored into every
|
||
# opencode run's config, so both harnesses serve the same local models.
|
||
PI_MODELS_FILE = pathlib.Path(os.path.expanduser(
|
||
os.environ.get("PI_MODELS_FILE", "~/.pi/agent/models.json")))
|
||
# Everything the sidecar keeps for the opencode harness: the mirrored
|
||
# transcripts (the backend's source dirs, one per account), the mirror's state,
|
||
# fork id map, and each account's plan usage snapshot.
|
||
OPENCODE_RUNNER_DIR = pathlib.Path(os.path.expanduser(
|
||
os.environ.get("SIDECAR_OPENCODE_DIR", "~/.opencode-runner")))
|
||
OPENCODE_MIRROR = os.environ.get("SIDECAR_OPENCODE_MIRROR", "1") not in ("0", "false", "")
|
||
# opencode V2 reads AGENTS.md, not CLAUDE.md: these go out as the run's config
|
||
# `instructions` (relative = searched upward from the cwd to the project root),
|
||
# so an opencode run follows the same conventions as a Claude one. "" = none.
|
||
OPENCODE_INSTRUCTIONS = [x.strip() for x in os.environ.get(
|
||
"SIDECAR_OPENCODE_INSTRUCTIONS", "CLAUDE.md,CLAUDE.local.md,~/.claude/CLAUDE.md").split(",")
|
||
if x.strip()]
|
||
# What a run on a local (EVOX2) model is told on top (opencode: an extra
|
||
# `instructions` entry; pi: the runner's --append-system-prompt): it streams at
|
||
# ~17 tok/s and nothing shows until a tool call completes, so small calls and
|
||
# big files in sections. Missing file = nothing added.
|
||
LOCAL_MODEL_GUIDE = pathlib.Path(os.path.expanduser(os.environ.get(
|
||
"SIDECAR_LOCAL_MODEL_GUIDE", str(pathlib.Path(__file__).resolve().parent / "local-model.md"))))
|
||
# How often each logged-in account's ChatGPT usage (% of its windows) is read.
|
||
OPENCODE_USAGE_POLL_S = float(os.environ.get("SIDECAR_OPENCODE_USAGE_POLL_S", "300"))
|
||
# …and each guest's own Claude login's /usage (claude_usage.py; 0 = never).
|
||
CLAUDE_USAGE_POLL_S = float(os.environ.get("SIDECAR_CLAUDE_USAGE_POLL_S", "300"))
|
||
|
||
# ---- the agy harness (agy_cli.py, agy_mirror.py, agy_usage.py) ---------------
|
||
# Google's Antigravity CLI: Gemini models on a Google AI Pro / Ultra login.
|
||
AGY_BIN = os.path.expanduser(os.environ.get("AGY_BIN", "~/.local/bin/agy"))
|
||
|
||
|
||
def _parse_agy_accounts(raw: str) -> dict[str, "pathlib.Path | None"]:
|
||
"""``SIDECAR_AGY_ACCOUNTS`` → {account id: HOME | None}. agy has no config-dir
|
||
switch (its data is always ``$HOME/.gemini``): an account other than the
|
||
host user's own login runs with its own ``HOME``. ``default`` (or empty) =
|
||
the host user's ``~/.gemini``, shared with a terminal ``agy``."""
|
||
out: dict[str, pathlib.Path | None] = {}
|
||
for part in (raw or "").split(","):
|
||
acct, _, d = part.partition("=")
|
||
acct, d = acct.strip().lower(), d.strip()
|
||
if re.fullmatch(r"[a-z][a-z0-9_-]{0,31}", acct):
|
||
out[acct] = None if d in ("", "default") else pathlib.Path(os.path.expanduser(d))
|
||
return out
|
||
|
||
|
||
# agy accounts (one Google login each): `google-personal` (= `google-personal=default`).
|
||
AGY_ACCOUNTS = _parse_agy_accounts(os.environ.get("SIDECAR_AGY_ACCOUNTS", ""))
|
||
# What a run with no model / effort picked uses (agy's own default is Neon high too).
|
||
AGY_DEFAULT_MODEL = os.environ.get("SIDECAR_AGY_MODEL", "gemini-4-neon")
|
||
AGY_DEFAULT_EFFORT = os.environ.get("SIDECAR_AGY_EFFORT", "high")
|
||
# agy's `--effort` scale (a model takes a subset: Neon low…xhigh, Flash low…high).
|
||
AGY_EFFORTS = ("low", "medium", "high", "xhigh", "max")
|
||
# The mirrored transcripts (one source dir per account), the mirror's state, the
|
||
# hub-uuid ⇄ agy-conversation map and each account's plan usage snapshot.
|
||
AGY_RUNNER_DIR = pathlib.Path(os.path.expanduser(
|
||
os.environ.get("SIDECAR_AGY_DIR", "~/.agy-runner")))
|
||
AGY_MIRROR = os.environ.get("SIDECAR_AGY_MIRROR", "1") not in ("0", "false", "")
|
||
# How often each logged-in account's quota (`agy -p /usage`, no model call) is read.
|
||
AGY_USAGE_POLL_S = float(os.environ.get("SIDECAR_AGY_USAGE_POLL_S", "300"))
|
||
# agy reads AGENTS.md / GEMINI.md, never CLAUDE.md or .claude/skills: the sidecar
|
||
# keeps a managed global rule (~/.gemini/config/rules/ai-agent-claude-md.md) that
|
||
# points it at the CLAUDE.md files, and a skills manifest naming these folders.
|
||
AGY_SKILL_DIRS = [os.path.expanduser(x.strip()) for x in os.environ.get(
|
||
"SIDECAR_AGY_SKILL_DIRS", "~/.claude/skills,~/homelab/.claude/skills").split(",")
|
||
if x.strip()]
|
||
|
||
# What `claude --model` accepts: a family alias (`opus`) or a full model id
|
||
# (`claude-opus-4-8`, `claude-haiku-4-5-20251001`). The viewer picks from the
|
||
# live Anthropic model list, so we don't pin an allow-list here — we only reject
|
||
# shapes that aren't a model at all, since the value goes onto a command line.
|
||
MODEL_RE = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$")
|
||
# pi model ids additionally carry a vendor prefix and optional thinking suffix
|
||
# (`qwen/qwen3.6-35b-a3b`, `…:thinking`).
|
||
PI_MODEL_RE = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._/:-]{0,127}$")
|
||
# Turning thinking off is the single thinking control the composer exposes, and
|
||
# each harness spells it its own way: the claude CLI takes `--thinking disabled`
|
||
# (its scale is enabled|adaptive|disabled), pi takes `--thinking off` (its scale
|
||
# is off|minimal|low|medium|high|xhigh|max). Thinking *on* emits no flag at all,
|
||
# so each CLI keeps whatever its own default level is — we only ever say "off".
|
||
THINKING_OFF = {"claude": "disabled", "pi": "off"}
|
||
# The claude CLI's `--effort <level>` — how hard the model works a turn. It is
|
||
# the *claude-only* replacement for the thinking toggle: the composer shows an
|
||
# effort select for claude runs and keeps the on/off toggle for pi, which has no
|
||
# equivalent flag. Unset emits nothing, so the CLI keeps its own default.
|
||
EFFORT_LEVELS = ("low", "medium", "high", "xhigh", "max")
|
||
# Headless sessions can't answer permission prompts, so they run with
|
||
# permissions bypassed (same as `--dangerously-skip-permissions`). This is the
|
||
# owner's own box acting on its own repo; keep it behind the bearer token.
|
||
PERMISSION_MODE = os.environ.get("SIDECAR_PERMISSION_MODE", "bypassPermissions")
|
||
# Tools a sidecar-spawned claude run must not use. AskUserQuestion is the
|
||
# interactive terminal chooser — headless runs have no terminal to answer it
|
||
# on, and the ai-agent's own ask channel is the `ask-form` skill (a form the
|
||
# PWA renders inline in the conversation). Space-separated env to override;
|
||
# set SIDECAR_DISALLOWED_TOOLS="" to disable.
|
||
DISALLOWED_TOOLS = os.environ.get(
|
||
"SIDECAR_DISALLOWED_TOOLS", "AskUserQuestion").split()
|
||
# Enable Claude Code Remote Control by default so every sidecar-spawned run
|
||
# shows up in the Claude app and can be driven from the phone. `claude
|
||
# --remote-control` with no name auto-generates one from the hostname prefix.
|
||
# Set SIDECAR_REMOTE_CONTROL=0 to opt out.
|
||
REMOTE_CONTROL = os.environ.get("SIDECAR_REMOTE_CONTROL", "1") not in ("0", "false", "")
|
||
LOG_DIR = pathlib.Path(
|
||
os.environ.get("SIDECAR_LOG_DIR", pathlib.Path(__file__).parent / "logs"))
|
||
# Where the claude CLI keeps its session transcripts (one dir per cwd slug).
|
||
# /fork does its transcript surgery here — it's the host user's own tree.
|
||
# This is the *default* account's tree; other accounts keep theirs under their
|
||
# own config dir (see ACCOUNTS).
|
||
CLAUDE_PROJECTS = pathlib.Path(os.environ.get(
|
||
"CLAUDE_PROJECTS_DIR", pathlib.Path.home() / ".claude" / "projects"))
|
||
|
||
|
||
def _parse_accounts(raw: str) -> dict[str, pathlib.Path | None]:
|
||
"""``SIDECAR_ACCOUNTS`` → {account id: config dir}.
|
||
|
||
Format: comma-separated ``id=dir`` pairs (``work=~/.claude-work``). The
|
||
default account (``SIDECAR_DEFAULT_ACCOUNT``, ``personal``) is always
|
||
present and maps to None: it runs on the CLI's own default config — never
|
||
on ``CLAUDE_CONFIG_DIR=~/.claude``, which would move ``.claude.json`` into
|
||
the dir and make the logged-in account look brand new."""
|
||
out: dict[str, pathlib.Path | None] = {}
|
||
for part in (raw or "").split(","):
|
||
if "=" not in part:
|
||
continue
|
||
acct, _, d = part.partition("=")
|
||
acct, d = acct.strip().lower(), d.strip()
|
||
if re.fullmatch(r"[a-z][a-z0-9_-]{0,31}", acct) and d:
|
||
out[acct] = pathlib.Path(os.path.expanduser(d))
|
||
return out
|
||
|
||
|
||
# Claude accounts a run can be launched on. An account *is* a config dir:
|
||
# CLAUDE_CONFIG_DIR moves the login, .claude.json, settings and the projects/
|
||
# transcripts together, so exporting it per run is the whole switch.
|
||
DEFAULT_ACCOUNT = os.environ.get("SIDECAR_DEFAULT_ACCOUNT", "personal")
|
||
_ACCOUNTS_RAW = os.environ.get("SIDECAR_ACCOUNTS", "")
|
||
ACCOUNTS: dict[str, pathlib.Path | None] = {
|
||
DEFAULT_ACCOUNT: None, **_parse_accounts(_ACCOUNTS_RAW)}
|
||
ACCOUNTS[DEFAULT_ACCOUNT] = None
|
||
# Guests' own Claude logins (backend users/): account ``guest-<uid>`` is the
|
||
# config dir ``<GUEST_ACCOUNTS_DIR>/<uid>``, made on the guest's first sign-in
|
||
# (the harness-claude plugin's ``accounts.provision_guest``). Not declared anywhere — any id of
|
||
# that shape is one; it is a login once the dir holds credentials. A worker
|
||
# (a Mac) has one login of its own and never hosts guests.
|
||
GUEST_ACCOUNTS = os.environ.get("SIDECAR_GUEST_ACCOUNTS", "1") not in ("0", "false", "")
|
||
GUEST_ACCOUNTS_DIR = pathlib.Path(os.path.expanduser(
|
||
os.environ.get("SIDECAR_GUEST_ACCOUNTS_DIR", "~/.claude-guests")))
|
||
GUEST_PREFIX = "guest-"
|
||
_GUEST_RE = re.compile(r"guest-([a-z0-9][a-z0-9-]{0,39})")
|
||
|
||
|
||
def guest_uid(account: str | None) -> str | None:
|
||
"""The guest id behind a ``guest-<uid>`` account, else None."""
|
||
m = _GUEST_RE.fullmatch((account or "").strip().lower())
|
||
return m.group(1) if m and GUEST_ACCOUNTS and not IS_WORKER else None
|
||
|
||
|
||
def guest_shaped(account: str | None) -> bool:
|
||
"""``guest-<uid>``, wherever this sidecar runs (``guest_uid`` is the host's
|
||
config-dir reading; a guest's own computer reads it as its own login —
|
||
``claude_env.machine_login``)."""
|
||
return bool(_GUEST_RE.fullmatch((account or "").strip().lower()))
|
||
|
||
|
||
def config_dir(account: str | None) -> pathlib.Path | None:
|
||
"""The account's ``CLAUDE_CONFIG_DIR`` — None for the default account (the
|
||
CLI's own ``~/.claude``) and for an unknown id."""
|
||
uid = guest_uid(account)
|
||
if uid:
|
||
return GUEST_ACCOUNTS_DIR / uid
|
||
return ACCOUNTS.get(account or DEFAULT_ACCOUNT)
|
||
|
||
|
||
def known_account(account: str | None) -> bool:
|
||
return (account or "") in ACCOUNTS or guest_uid(account) is not None
|
||
|
||
|
||
# With accounts declared, every run authenticates with a subscription login —
|
||
# so an ANTHROPIC_API_KEY that leaks into the sidecar's env would silently
|
||
# override BOTH accounts and bill the API key instead. Strip it from every run.
|
||
# (Unset — the standalone in-container runner — the key may be the only auth,
|
||
# so it passes through unchanged.)
|
||
STRIP_API_KEY = bool(_ACCOUNTS_RAW.strip())
|
||
# Never handed to a run, whatever the account: the sidecar's own bearer (and
|
||
# the in-container runner's copy of it) would let a session drive this sidecar.
|
||
RUN_ENV_STRIP = ("SIDECAR_TOKEN",)
|
||
# …and never handed to a scoped run (a guest's): the owner's SSH agent, D-Bus
|
||
# session and systemd user manager are keys to other machines and to the host.
|
||
# Where a guest's scheduled jobs run when they have no project (backend
|
||
# cron/guests.py): one folder per guest, created on its first run.
|
||
GUEST_JOBS_DIR = os.path.expanduser(os.environ.get(
|
||
"SIDECAR_GUEST_JOBS_DIR", "~/.local/share/ai-agent/guest-jobs"))
|
||
SCOPED_ENV_STRIP = ("SSH_AUTH_SOCK", "SSH_AGENT_PID", "GPG_AGENT_INFO",
|
||
"DBUS_SESSION_BUS_ADDRESS", "XDG_RUNTIME_DIR")
|
||
# `claude auth status` per account for GET /accounts — cached, it spawns a CLI.
|
||
ACCOUNT_STATUS_TTL_S = float(os.environ.get("SIDECAR_ACCOUNT_STATUS_TTL_S", "60"))
|
||
# How long a freshly launched run is watched before we answer the caller. A
|
||
# `claude` that can't start at all (bad `--resume` id, bad cwd, broken install)
|
||
# exits non-zero within ~2.5s; anything still alive at the end of the window is
|
||
# a real run. Without this the launch is fire-and-forget and a dead-on-arrival
|
||
# run looks exactly like a healthy one — the viewer then waits forever for turns
|
||
# that never come ("sending…" with no error).
|
||
LAUNCH_PROBE_S = float(os.environ.get("SIDECAR_LAUNCH_PROBE_S", "3.5"))
|
||
# How long a force-resume waits for the SIGINT'd run to die before escalating
|
||
# to SIGKILL. `claude` handles SIGINT by writing the interrupt marker and
|
||
# exiting — usually well under a second. The total force-resume worst case
|
||
# (STOP_WAIT_S + 2s kill wait + LAUNCH_PROBE_S) must stay under the backend's
|
||
# 15s sidecar call timeout.
|
||
STOP_WAIT_S = float(os.environ.get("SIDECAR_STOP_WAIT_S", "6"))
|
||
|
||
# ---- background work in a headless run ---------------------------------------
|
||
# A `claude -p` process ends when the model ends its turn. Before exiting it
|
||
# waits for the run's background *subagents and Monitors* — but only up to
|
||
# `CLAUDE_CODE_PRINT_BG_WAIT_CEILING_MS` (the CLI's default is 600000: 10 min,
|
||
# which is what silently killed every hour-long subagent, with "Background tasks
|
||
# still running after 600s; terminating" in the run log). Exported into every
|
||
# run; "" leaves the CLI's default. 0 = wait indefinitely (the Stop button
|
||
# still kills the run).
|
||
BG_WAIT_CEILING_MS = os.environ.get("SIDECAR_BG_WAIT_CEILING_MS", "7200000")
|
||
# Plain `Bash run_in_background` tasks are *excluded* from that wait (the CLI's
|
||
# `local_bash` type) and die the instant the turn ends. stop_guard.py — a Stop
|
||
# hook injected via `--settings` — holds the turn until such a task has exited
|
||
# (its output file ends with "[exited with code N]"), without any API call.
|
||
# (the hook itself is the harness-claude plugin's sidecar/stop_guard.py)
|
||
STOP_GUARD_ENABLED = os.environ.get("SIDECAR_STOP_GUARD", "1") not in ("0", "false", "")
|
||
# The hook's own timeout (what the CLI enforces); the guard hands the question
|
||
# back to the model well before it (STOP_GUARD_MAX_WAIT_S in stop_guard.py).
|
||
STOP_GUARD_TIMEOUT_S = int(os.environ.get("SIDECAR_STOP_GUARD_TIMEOUT_S", "3600"))
|
||
# A no-op Stop hook in every account's settings.json (turn_hook.py), so a run
|
||
# outside the hub writes its turn-end record too. `SIDECAR_TURN_HOOK=0` disables.
|
||
TURN_HOOK_ENABLED = os.environ.get("SIDECAR_TURN_HOOK", "1") not in ("0", "false", "")
|
||
# The conv-ref prompt hook in every account's settings.json (conv_ref_hook.py):
|
||
# a prompt naming a past conversation gets where conversations live and a card
|
||
# for each one it names. `SIDECAR_CONV_REF_HOOK=0` disables (and removes it).
|
||
CONV_REF_HOOK_ENABLED = os.environ.get("SIDECAR_CONV_REF_HOOK", "1") not in ("0", "false", "")
|
||
# What a Bash call changed on disk: bash_diff.py, a PreToolUse + PostToolUse
|
||
# hook pair on Bash injected via `--settings` like the guard. The post hook's
|
||
# stdout lands in the transcript (never in the model's context) and the viewer
|
||
# shows it as the card's +N −M and diff. `SIDECAR_BASH_DIFF=0` disables.
|
||
# Live plugins' harnessHooks (plugin_hooks.py; backend plugins/harness_hooks.py):
|
||
# the hooks the hub placed on this machine, read at each claude launch.
|
||
# `SIDECAR_PLUGIN_HOOKS=0` disables.
|
||
PLUGIN_HOOKS_ENABLED = os.environ.get("SIDECAR_PLUGIN_HOOKS", "1") not in ("0", "false", "")
|
||
BASH_DIFF = pathlib.Path(__file__).resolve().parent / "bash_diff.py"
|
||
BASH_DIFF_ENABLED = os.environ.get("SIDECAR_BASH_DIFF", "1") not in ("0", "false", "")
|
||
# Per hook call; the script gives up on a slow repo well before (GIT_TIMEOUT_S).
|
||
BASH_DIFF_TIMEOUT_S = 30
|
||
# The guard (guard.py): a PreToolUse hook on every run that checks a tool call
|
||
# against the security rules — a scoped run's hit is refused, an owner run's
|
||
# goes through; both land in the hub's security log (docs/security.md).
|
||
# `SIDECAR_GUARD=0` disables.
|
||
GUARD = pathlib.Path(__file__).resolve().parent / "guard.py"
|
||
GUARD_ENABLED = os.environ.get("SIDECAR_GUARD", "1") not in ("0", "false", "")
|
||
GUARD_TIMEOUT_S = 10
|
||
GUARD_BROKER_TIMEOUT_S = 300 # a scoped run's guard also runs its `ai-agent` CLI calls
|
||
# A follow-up sent into a *live* run is delivered through the CLI's per-session
|
||
# inbox socket (`/message`) instead of killing the run; the session only takes
|
||
# such messages with `crossSessionInbound: accept`. Claude Code wraps them as
|
||
# "Another Claude session sent a message … not typed by your user", so the text
|
||
# is prefixed to say whose it really is — the backend's parser strips both.
|
||
INBOX_PREFIX = ("[Follow-up from your user, sent from the ai-agent composer — "
|
||
"this IS your user's own message, not a peer agent's.]\n\n")
|
||
# …unless the hub itself wrote it: a prompt opening on an app author tag
|
||
# (``<ai-agent-from … role="app"/>`` — a child conversation's report back, an
|
||
# approval, the New project hand-over) is the app's, and must not be passed off
|
||
# as the user's own words: a child's report is another model's output.
|
||
APP_INBOX_PREFIX = ("[Message from the ai-agent hub ({name}) — sent by the app, "
|
||
"not typed by your user.]\n\n")
|
||
_APP_TAG = re.compile(r'\A<ai-agent-from user="[^"]*" name="([^"]*)" role="app"\s*/>')
|
||
|
||
|
||
def inbox_prefix(prompt: str) -> str:
|
||
"""The note a ``/message`` follow-up is prefixed with: whose it really is."""
|
||
m = _APP_TAG.match(prompt or "")
|
||
return APP_INBOX_PREFIX.format(name=m.group(1) or "app") if m else INBOX_PREFIX
|
||
|
||
|
||
# The `notify` CLI (cli/notify.py — push / ask / form through the hub) ships
|
||
# with this repo so every runner has it: appended to each run's PATH (the lab
|
||
# host's own skill aliases come first and resolve to the same file).
|
||
CLI_DIR = pathlib.Path(__file__).resolve().parent.parent / "cli"
|
||
# A paired remote worker has no hub URL or credential, so its runs reach the
|
||
# hub through *this* sidecar's outbox (outbox.py): the hub pulls it. The host
|
||
# sidecar (SIDECAR_TOKEN set) is next to the hub — its runs talk to it directly.
|
||
IS_WORKER = not TOKEN
|
||
# Keep the machine awake while an agent run is live (inhibit.py): a logind
|
||
# `sleep` block lock, held only while at least one run is going. On by default
|
||
# on a Linux worker (the EVOX2 auto-suspends on input idle, mid-run); off on the
|
||
# host sidecar, which never sleeps, and a no-op on macOS. `1` / `0` overrides.
|
||
INHIBIT_SLEEP = os.environ.get(
|
||
"SIDECAR_INHIBIT_SLEEP",
|
||
"1" if IS_WORKER and sys.platform == "linux" else "0") not in ("0", "false", "")
|
||
# Skills the owner shares with guests (backend users/store ``sharedSkills``)
|
||
# arrive in a guest run's scope as ``name`` (one of this repo's — the default
|
||
# cwd's — ``.claude/skills``) or ``<project>/<name>`` (a project's). They are
|
||
# symlinked into a generated plugin under this cache and handed to the run with
|
||
# ``--plugin-dir`` (see shared_skills.py).
|
||
SHARED_SKILLS_REPO = pathlib.Path(os.environ.get(
|
||
"SIDECAR_SHARED_SKILLS_DIR", str(pathlib.Path(DEFAULT_CWD) / ".claude" / "skills")))
|
||
SHARED_SKILLS_PROJECTS = pathlib.Path(os.path.expanduser(
|
||
os.environ.get("SIDECAR_PROJECTS_DIR", "~/projects")))
|
||
SHARED_SKILLS_CACHE = pathlib.Path(os.path.expanduser(
|
||
os.environ.get("SIDECAR_SHARED_SKILLS_CACHE", "~/.cache/ai-agent/shared-skills")))
|
||
|
||
|
||
def claude_config_dirs() -> list[pathlib.Path]:
|
||
"""Every Claude account's config dir that exists here, once each — where
|
||
the account-wide hooks go (harness-claude's turn_hook / conv_ref_hook,
|
||
callprobe's agent-probe hooks)."""
|
||
out, seen = [], set()
|
||
for d in ACCOUNTS.values():
|
||
d = d or CLAUDE_PROJECTS.parent
|
||
if d.is_dir() and d.resolve() not in seen:
|
||
seen.add(d.resolve())
|
||
out.append(d)
|
||
return out
|