# Specialists

A specialist is a Pi subagent that is the only way to operate one area of a site. It has one tool, one skill, and one Vaultwarden note. Every other agent is blocked from the area's CLIs and credentials.

pi-astro ships the framework: the gate, the sudo entry point, the root installer, the shared wrapper library, and `provision`. The specialists come from a signed site pack, installed per host; the 37 and Mira pack is `homelab/astro-site/` (`SETUP.md` there lists its specialists and the host setup).

Call one with `/run astro.<name> -- <task>` or through the `subagent` tool (both from the bundled `astro-subagents` extension). `/run` saves the specialist's session under `~/.pi/agent/subagent-sessions/<agent>/`; `/run astro.<name> --continue -- <task>` resumes the last one started from the same directory, so the specialist keeps what it saw. Sessions older than 7 days are deleted. Specialists may call each other once (`maxSubagentDepth: 1`). They run on `openai-codex/gpt-6-astra` with `xhigh` thinking, set by `model` and `thinking` in each `agents/<name>.md`; an agent without these fields inherits the caller's model and thinking. Each tool takes `args`, an array of strings; `["--help"]` lists the subcommands.

## How it is secured

- Credentials and CLIs belong to a dedicated macOS user `specialist` on the host. Agents run as the main user and reach the wrappers only through `sudo -n -u specialist -H /usr/local/bin/specialist-cli`, the single passwordless sudo rule. General sudo asks for a password.
- Credentials come from Vaultwarden (`homelab` / `specialists`, one Secure Note per specialist) through `specialist-cli provision`, into `0600` files. Wrappers never call `bw`.
- The wrappers run on a root-owned Node in `/usr/local/libexec/specialist-node/` (latest LTS from nodejs.org), not on Homebrew's, because the main user owns all of `/opt/homebrew` and could otherwise swap the interpreter that reads every secret. `/usr/local/sbin/specialist-install` (root, one passwordless sudo rule) installs that Node, the signed pack's wrappers, and the tools the wrappers run with secrets from downloads it checks against the publisher's checksums; it refuses older versions and keeps the previous one for `rollback`.
- Services use short-lived tokens, API keys, or SSH keys that the targets pin to forced commands.
- A pack installs only with an SSH signature from a key in the root-owned `/usr/local/etc/astro/allowed_signers`, which the owner installs with their own sudo.
- Every call is logged in `/Users/specialist/.specialists/audit.log`.
- The `specialist-gate` extension registers the tool only inside the matching specialist child and blocks other agents from calling the CLIs or reading `/Users/specialist`. `security-guard` carries the same blocks.

## Enable specialists on a host

Steps for macOS; `<main>` is the user that runs Pi. The pack's own setup lists its Vaultwarden notes, config files, and SSH targets.

1. **User and sudo**, as an administrator:
   ```sh
   printf '<main> ALL=(specialist) NOPASSWD: /usr/local/bin/specialist-cli\n' | sudo tee /etc/sudoers.d/specialists >/dev/null
   printf '<main> ALL=(root) NOPASSWD: /usr/local/sbin/specialist-install\n' | sudo tee /etc/sudoers.d/specialist-install >/dev/null
   sudo chmod 0440 /etc/sudoers.d/specialists /etc/sudoers.d/specialist-install && sudo visudo -c
   sudo sysadminctl -addUser specialist -fullName "Specialists" -home /Users/specialist -shell /bin/zsh -password -
   sudo dscl . create /Users/specialist IsHidden 1 && sudo createhomedir -c -u specialist && sudo chmod 700 /Users/specialist
   sudo install -d -m 0755 -o root -g wheel /usr/local/sbin /usr/local/etc /usr/local/etc/astro
   sudo install -m 0755 -o root -g wheel <package>/specialists/entry/specialist-install.sh /usr/local/sbin/specialist-install
   sudo install -m 0755 -o root -g wheel <package>/specialists/entry/specialist-cli.sh /usr/local/bin/specialist-cli
   sudo install -m 0644 -o root -g wheel <pack>/allowed_signers /usr/local/etc/astro/allowed_signers
   sudo /usr/local/sbin/specialist-install node <LTS version>
   sudo /usr/local/sbin/specialist-install jq <version>
   sudo /usr/local/sbin/specialist-install bw <version>
   ```
   `<package>` is the installed pi-astro directory, `~/.pi/agent/npm/node_modules/@astrofoundry/pi-astro`.
2. **Bootstrap**, as `specialist` (`sudo -u specialist -H zsh`, `umask 077`): `~/.config/specialists/client.env` with `BW_SERVER`, `BW_CLIENTID`, `BW_CLIENTSECRET`; `~/.config/specialists/master-password`. Both `0600`. Then `specialist-cli provision` writes each note's fields into `0600` files.
3. **Pack**: the pack's release copies `pack-<version>.tgz` and its signature to `/private/var/tmp/astro-pack/` and runs `specialist-install pack <version>`; it installs the agents and skills into `~/.pi/agent/pack/` and the gate entries into `~/.pi/agent/specialists.json` (`entry` `/usr/local/bin/specialist-cli`, `user` `specialist`).

## Installer

Updates go through the installer, one component at a time, each checked before and after the switch:
```sh
sudo /usr/local/sbin/specialist-install node <LTS version>
sudo /usr/local/sbin/specialist-install pack <pack version>
sudo /usr/local/sbin/specialist-install wrappers <pi-astro version>
sudo /usr/local/sbin/specialist-install arcane-cli <Arcane server version>
sudo /usr/local/sbin/specialist-install trendarr <version>
sudo /usr/local/sbin/specialist-install jq|bw|gcloud <version>
sudo /usr/local/sbin/specialist-install python <x.y.z+YYYYMMDD>
sudo /usr/local/sbin/specialist-install rollback node|wrappers|arcane-cli|trendarr|python|gcloud|bw|jq
sudo /usr/local/sbin/specialist-install status
```
   - `node` accepts only an LTS release listed in `https://nodejs.org/dist/index.json`, checks the tarball against `SHASUMS256.txt`, and switches `current` only after every installed wrapper answers `--help` with it; `previous` keeps the last one.
   - `pack` takes `/private/var/tmp/astro-pack/pack-<version>.tgz` only when `pack-<version>.tgz.sig` is an SSH signature (`ssh-keygen -Y sign -n astro-pack`) from a key in `/usr/local/etc/astro/allowed_signers`, as identity `astro-pack`. That file and its folders must be root-owned and writable by nobody else; the owner installs it with their own sudo, so neither the `cortex` user nor a specialist can add a key. The installer copies both files into its own work folder before it checks them, reads the services from the pack's `pack.json` (whose `version` must match), installs them into the wrappers' slot, and keeps `pack.json` beside them. `specialist-cli` accepts any installed service name, so a new specialist in a pack needs no new `specialist-cli`.
- `wrappers` takes the pi-astro tarball from the npm registry, checks its `sha512` integrity, and installs the twelve services pi-astro 0.40.105 and older shipped; later versions carry none, so it refuses them. A pack's version (1.x) is newer than every pi-astro 0.40.x, so `wrappers` cannot replace an installed pack.
   - `arcane-cli` checks the release tarball against `arcane_<version>_checksums.txt`; it follows the Arcane server's version.
   - `python` installs a python-build-standalone `install_only_stripped` build (astral-sh), checked against the release's `SHA256SUMS`; before the switch it imports `ssl` and `sqlite3` and runs the current gcloud with the new Python.
   - `gcloud` assembles the SDK from Google's component snapshot `components-v<version>.json` (core, core-nix, gcloud-deps, and their darwin-arm parts), each tarball checked against the snapshot's sha256, because Google lists the full archive's sha256 only in the table on its versioned-archives page, for the current release; it needs the installed Python and answers `gcloud version` before the switch.
   - `bw` takes `bw-bit-macos-arm64-<version>.zip` (`bw-macos-arm64-<version>.zip` before 2026.11.0) from the bitwarden/clients release `cli-v<version>`, checked against the asset digest from the GitHub API; `jq` takes `jq-macos-arm64` from the jqlang/jq release, checked against its `sha256sum.txt`.
   - `trendarr` installs with the root-owned Node's npm, without install scripts, into `trendarr-<version>`.
   - A version older than the installed one is refused; `rollback` swaps back to the kept previous copy. `status` prints the installed versions as JSON.

## Discord

The `astro-discord` extension gives Discord users access to the specialists from a channel. It runs in a headless Pi host on Cortex (LaunchAgent `com.astrofoundry.astro-discord`, user `cortex`), never in interactive sessions.

- Channels are configured one by one (`channels.<id>`): `trigger` is `always` (every message that names a specialist counts) or `mention` (only messages that start with `@Cortex` or reply to the bot); `specialists` is `*` or a list. A channel with one specialist needs no prefix: every message there is a task for it. In a channel with several, `dns list the zones` names the specialist; an addressed message without a name gets a reply asking for one. Threads inherit their parent channel's entry; unlisted channels are ignored. The bot replies at once with "<specialist> is working on your task" and shows the typing indicator until the answer replaces that reply. Reactions on your message: ⏳ running, 🔒 waiting for an owner's approval, ✅ done, ❌ failed. `help` shows the specialists you may use in that channel and the commands (`status` lists running tasks; owners in the admin channel also see the `config` commands). Each channel and specialist has one conversation: a reply to the bot's answer, or another task for that specialist within 30 minutes, continues it with everything the specialist saw before; otherwise a new one starts, and a task beginning with `new` forces that. Replied-to and forwarded messages (text, embeds, attachment names) travel with the task, so "check this" on a forwarded alert works. Image attachments on the message or on what it quotes (up to 4, 8 MiB each) are downloaded and passed to the specialist, which sees them; other file types are named only. Sessions live under `~/.config/astro-discord/sessions/<specialist>/` and are deleted after 7 days. Long answers arrive as a `.md` attachment. When the bridge restarts, open approval requests are marked cancelled and running tasks interrupted in Discord; ask again. Scheduled reports are a list in the config, one entry per report: `"reports": [{ "report": "arcane", "channelId": "<id>", "time": "06:00", "timeZone": "Europe/Madrid" }, { "report": "tools", "channelId": "<admin channel id>", "time": "06:00", "timeZone": "Europe/Madrid" }]` (the earlier single `dailyReport` key is refused; move it into the list). A `daily` entry (on Cortex: 05:00 Europe/Paris, the daily channel) starts the morning run and replaces the other reports' own times, except `media-weekly`: it posts the Security, Cortex Tools, and Arcane reports in their own channels, each applying its automatic updates; then it reads every specialist's area (backups, the front door, both Proxmox hosts with their package upgrades and a Reboot button when one is needed, guest updates, RADIUS and both Tailscale routers, the DMZ, DNS with one Technitium node upgraded per morning, Nexus, downloads, and macOS on Cortex and Nexus), tags the owners in the related channel for each problem, and posts a summary without mentions in the daily channel. The Arcane report posts an "Arcane Daily Report:" message from a fresh `update-report`: one section per stack with updates, each service as current to target version with the kind of change, and a release-notes verdict on every pending update. A stack without a major version and without notes that need attention updates on its own after the post and is checked again five minutes later; a failed check rolls back a Compose change, tags the owners, and starts the foreman in a thread. A major whose notes say nothing relevant gets an Update button; anything else gets Upgrade carefully, which opens a thread where the arcane specialist posts a plan and waits for "go". Only owners can press the buttons (Update runs `arcane stack-update <stack>`; the button turns into the outcome and the reason arrives as a note only the clicker sees). Stacks held in `update-holds.json` at the root of the arcane GitOps repository (`{ "supabase": "coordinated release: review first" }`) show the reason and get no button. Replying to the report asks the arcane specialist with the report as context. The tools report posts "Cortex Tools Report:": every tool on Cortex with a newer version (Homebrew packages installed on request and casks, Pi, pi-astro, and through `specialist-install` the wrappers, arcane-cli at the Arcane server's version, trendarr, and the specialist Node at the latest LTS), a release-notes verdict from `astro.release-reviewer` for the tools the specialists build on, an Update button per line, and one Upgrade everything for Homebrew. Lines without a major version or notes that need attention update on their own after the post; Pi always waits for its button, and casks that need a password stay by hand. Only owners can press the buttons. After an update that needs it, the bridge, herdr, or Astrogate restarts as soon as nothing runs on it; after a day of waiting, a Restart now button appears. The bridge calls `sudo -n /usr/local/sbin/specialist-install` through the sudoers rule from step 4.
- Site plugin: the bridge loads `~/.pi/agent/pack/bridge/index.js` from the installed pack, which adds the site's scheduled reports, slash commands, and buttons (contract in `extensions/astro-discord/plugin.ts`). Without it the bridge runs specialist tasks and approvals only.
- Access: `owners` may use every specialist and decide approvals; `access.<specialist>` lists extra users for that specialist; everyone else gets no reply. Owners change the configuration from the admin channel (`adminChannelId`) without a shell: `config show`, `config channel <#channel> <always|mention> <*|dns,edge>`, `config channel <#channel> remove`, `config access <specialist> add|remove <@user>`; the bot writes `~/.config/astro-discord/config.json` on Cortex, which is also editable by hand and re-read on every message. Deny the bot's role View Channel on channels it must never read; that is the boundary the map cannot provide.
- Approvals: a risky call (anything with `--confirm`, plus writes such as record or zone deletes, Zitadel changes, every UniFi write, Pomerium deploys, service restarts, and on Arcane destructive actions: `down`, deletes, prunes, image, volume, or network removal, `system upgrade`, `gitops delete`, plus `manager-upgrade`, `manager-rollback`, `host db-restore`, and the Radarr host-config reads; `stack-update` asks only across a major version, through its `--confirm`; an owner's Update click on the report needs no approval; `--help` as the first argument never asks, anywhere else it does not exempt the call) pauses the specialist and posts Approve/Deny buttons; only owners' clicks count; no decision within `approvalTimeoutMinutes` denies it. The specialist then reports the denial. Ambiguous tasks get a question back instead of an action.
- Setup, as `cortex`: `d=$(mktemp -d) && cp ~/.pi/agent/npm/node_modules/@astrofoundry/pi-astro/extensions/astro-discord/*.ts "$d" && node --disable-warning=ExperimentalWarning "$d/setup.ts"` (Node does not strip types inside `node_modules`, so the files are copied first) prompts for the bot token and the ids, checks each against Discord, and writes `~/.config/astro-discord/{token,config.json,run.sh}` and the LaunchAgent; it prints the `launchctl bootstrap` line. After a pi-astro update restart the bridge with `launchctl kickstart -k gui/$(id -u)/com.astrofoundry.astro-discord`. Log: `~/Library/Logs/astro-discord.log`.
- Discord side: application `Cortex` with a bot, Message Content Intent enabled, invited with scopes `bot` and `applications.commands` and permissions View Channel, Send Messages, Attach Files, Read Message History, Add Reactions.

## Rotation and operations

A pack documents its Vaultwarden fields, rotation, and forced-command scripts. After any change in Vaultwarden run `specialist-cli provision`. See `AGENTS.md` for the framework's implementation rules.
