Remote legs: running agent legs on another machine¶
A Studio daemon started in leg-host mode runs agent legs for a caller on another machine. It accepts one kind of work: an agent launch naming a repository it serves and a full commit. It runs that work in a detached worktree it checks out itself, and keeps the leg's final output for the caller. li remote is the client.
Leg-host mode trusts the caller with the daemon's bearer, and with nothing else. Everything a leg-host daemon holds (its login, its configuration, its other repositories) stays out of the leg's reach, and every request outside that one shape is refused.
Setting up a leg host¶
On the machine that runs the legs, under the account they run as:
- Install lionagi, and log in to the Claude Code CLI there.
-
Configure the repositories it serves in
~/.lionagi/leg_repos.yaml. Both paths are absolute. The clone'soriginis the only remote the host fetches from.lionagi: clone: /home/agent/repos/lionagi worktrees: /home/agent/leg-worktrees/lionagi -
Put the agent profiles legs may run as in
~/.lionagi/agents/. They must be Claude Code profiles (model: claude_code/...). -
Start Studio with:
LIONAGI_STUDIO_LEG_HOST=1 \ LIONAGI_STUDIO_AUTH_TOKEN=<a long random token> \ li studio --host <the host's tailnet address>The address goes in
--host.li studiobinds127.0.0.1unless--hostnames another address, whateverLIONAGI_STUDIO_HOSTholds.
| Environment variable | Leg-host admission |
|---|---|
LIONAGI_STUDIO_LEG_HOST_MAX_LAUNCHES | Maximum concurrent legs (default 2). A launch beyond it receives HTTP 429. |
LIONAGI_STUDIO_LEG_HOST_HOLD_LOCK | Optional absolute path to a regular advisory lock file coordinated with a benchmark runner. An empty or relative value, or a host without POSIX advisory locking, refuses startup. Leave it unset to disable the check. |
When a hold lock is configured, admission tries a nonblocking shared lock before taking a launch slot. If an exclusive holder is present, a launch receives HTTP 503 with the path. A missing file is logged and admission continues. Other file or lock errors receive HTTP 503 with the path and errno. Studio opens the file read-only and releases the probe immediately, so it never creates the lock file or holds it for the life of a leg. The authenticated launch capabilities response includes the boolean hold_lock, which reports whether a leg host has configured the check.
Calling it¶
On the caller's machine, name the remote in ~/.lionagi/remotes.yaml. The file holds the URL and the name of the variable that carries the token, never the token itself:
legbox:
url: http://legbox:8765
token_env: LIONAGI_REMOTE_LEGBOX_TOKEN
export LIONAGI_REMOTE_LEGBOX_TOKEN=<the leg host's token>
li remote run legbox -a reviewer --repo lionagi --head <40-hex commit> "Review the change at HEAD."
Over plain http the remote must answer from a loopback or tailnet address, read off the connected socket before the token is sent. li remote sends that address as the Host header, the one name a leg host bound to it admits. Proxies and redirects are never followed.
li remote run waits for the leg and prints its result. Ctrl-C cancels the remote leg. --account <name> runs the leg on that subscription account on the leg host instead of the one its profile names; a launch whose response does not confirm the account is cancelled. li remote status, li remote output and li remote cancel take an invocation id.
What a leg host refuses¶
Each of these is refused before an invocation is recorded or a process starts.
At start-up. The daemon does not start in leg-host mode when:
LIONAGI_STUDIO_AUTH_TOKENis unset;- it would listen on an address that is neither loopback nor a tailnet address (100.64.0.0/10, fd7a:115c:a1e0::/48);
~/.lionagi/.mcp.jsonor~/.claude.jsonnames an MCP server;- the Codex
config.tomlunder the leg's default home ($HOME/.codex) or a registered[codex.<name>]home names an MCP server, including a disabled one. An unreadable config or account registry also prevents startup. A missing config declares no servers. The daemon's ownCODEX_HOMEis outside the leg environment. leg_repos.yamlconfigures no repository;- the child sandbox is disabled in
~/.lionagi/settings.yaml, or its settings do not resolve.
At start-up it also removes worktrees left behind by an earlier run.
For a Codex request with an explicitly empty MCP grant, the final command must ignore the user configuration or explicitly disable each configured server. If neither can be confirmed, the request fails before the provider process starts.
On a launch (answered 422):
- any
action_kindother thanagent; - any request field other than
action_kind,action_agent,action_prompt,action_repo,action_headandaction_account. That coversaction_cwd(only a worktree the daemon made is a working directory there),action_model(the profile decides the model), extra arguments and a project; - an
action_agentthat is not a bare name ([a-z0-9_-]+). The name is resolved only in the host's own~/.lionagi/agents; a repository's own.lionagi/agentsis never searched; - a profile whose provider is not
claude_code, or that names no model. Other providers' child processes have no measured read denial for the account's credential files; - a profile the child sandbox exempts from any default read denial (
allow_read); - an
action_headthat is not exactly 40 lowercase hex characters. Branch names, tags and short hashes are refused; - an
action_repothe host does not serve; - an
action_accountthat is not a valid account name, or that has no entry for the profile's provider in the host's accounts directory (seedocs/internals/subscription-accounts.md). The refusal names no path on the host. The credential is read only by the leg, so an entry whose token cannot be read fails the leg rather than the launch. A launch that names no account runs on the account its role names, if any. Any caller holding the bearer may name any account in the host's accounts directory, including one no profile names.
When the leg starts (the invocation fails with the reason):
- a head the clone cannot fetch from its
origin; - a worktree whose checkout is not at the requested head;
- a tree that carries configuration a leg would load:
.lionagi/,.mcp.jsonor.claude/.
Every other write route answers 403. A caller holding the bearer can launch, read and cancel, nothing more. A caller without it gets 401 from every route except /health.
What a leg is handed¶
- A working tree:
<worktrees>/<invocation id>, checked out detached at the requested head and removed when the leg ends, whether it completed, failed, timed out or was cancelled. Removal touches only that path, throughgit worktree remove. Before every leg launch the daemon also removes leftover worktrees whose invocation is no longer running. -
An environment built from an allow-list:
PATH,HOME,USER,LOGNAME,SHELL,LANG,LC_ALL,LC_CTYPE,TERM,TMPDIR,CLAUDE_CODE_OAUTH_TOKENwhen the daemon has it, andLIONAGI_HOMEandLIONAGI_LOGINS_DIRwhen the daemon has them, so the leg finds its accounts in the directory the daemon checked a named account against. The daemon's bearer, anyLIONAGI_REMOTE_*variable and every other credential variable stay with the daemon. lionagi then removes every credential-named variable other than the provider's own login from the Claude Code process it starts, on any machine. Claude Code withholdsCLAUDE_CODE_OAUTH_TOKENfrom the processes it starts (observed with Claude Code 2.1.325), so the Claude login is the one credential the leg's Claude Code holds, and it is not in the environment of the commands it runs. Measured with Claude Code 2.1.325 on a leg host started with bothCLAUDE_CODE_OAUTH_TOKENandLIONAGI_STUDIO_AUTH_TOKENset: -
The Claude login reaches the leg's Claude Code through that variable. The same host started without it ran a leg that answered "Not logged in", and with it the leg completed.
-
From the leg's own Bash tool, each of these commands, run as written, printed the value shown:
/usr/bin/python3 -I -c "import os; print('CLAUDE_CODE_OAUTH_TOKEN' in os.environ)" False /usr/bin/python3 -I -c "import os; print('LIONAGI_STUDIO_AUTH_TOKEN' in os.environ)" False /usr/bin/python3 -I -c "import os; print('HOME' in os.environ)" TrueThe leg's profile allowed these commands with
allowed_tools: ["Bash(/usr/bin/python3 -I -c:*)"]; a non-interactive leg otherwise asks approval for them. That rule matches by prefix, so it admits any Python program passed to-c. Put it on a probe profile used only for this check, never on a working leg profile.These commands read the environment only. Whether a leg can read a login from a file or the keychain depends on the child sandbox below, which this measurement does not cover. - In that leg, Claude Code refused
test -n "$CLAUDE_CODE_OAUTH_TOKEN"before running it ("A variable in this command can't be checked before it runs"), so a check in that form records no reading at all, neither present nor absent. - No MCP servers: the leg runs with--no-mcp-config, and the start-up check above refuses a host that configures any. - The child sandbox from the host's global~/.lionagi/settings.yaml, which must be enabled. Its default read denials (credential files,~/.secrets, the Claude and Codex logins, and the other locations listed underchild_sandboxin the CLI reference) apply to every leg; a repository's settings cannot relax them, and no profile exemption is accepted.
What a leg host does not run¶
A leg-host daemon starts neither the scheduler engine nor the transcript mirror. The account's schedules do not fire there, and its other Claude Code and Codex sessions are not copied into the host's store. The boot log says so in one line each:
Leg host: the scheduler engine is not started
Leg host: the transcript mirror is not started
Without the scheduler engine its periodic sweeps do not run either. A leg records its own end when it exits, fails or is cancelled.
What a leg host does not return¶
- The result is the leg's final output only: the last 1 MiB of its standard output, served by
GET /api/launches/{id}/result, withtruncatedset when more was written. - Files a leg writes die with its worktree. Nothing a leg leaves in its tree is kept or sent back.
- Run records stay on the leg host. The caller's own run history and
li monitordo not show a remote leg.
Protocol¶
| Route | Purpose |
|---|---|
GET /api/launches/capabilities | {"leg_host": true, "pinned_head": true, "named_account": true, "max_concurrent": N}; li remote refuses a daemon that is not a leg host, and refuses --account on one without named_account |
POST /api/launches/ | start a leg; the 202 echoes action_head, and action_account when the launch named one |
GET /api/invocations/{id} | status |
GET /api/launches/{id}/result | the result once the leg has exited |
POST /api/invocations/{id}/cancel | stop the leg |