Expand description
Plain gh inside an agent process must not act as a human’s gh login, when
the agent has a GitHub identity of its own.
An agent can reach GitHub through an identity of its own: gh-agent, which
hands gh a per-call token, or the AgentMux App’s broker, which does the same
for git and gh. The gh CLI, run directly, instead reads whatever login a
human made on the host — so an agent that types gh where it meant its own
identity silently acts as that human, with nothing failing. gh finds its
accounts through $GH_CONFIG_DIR/hosts.yml; pointing that variable at a
directory with no hosts.yml leaves gh with no accounts, so it reports
“not logged in” instead. The agent’s own identity is unaffected: it passes its
own token.
An agent with no such identity has nothing to be protected into using: the
person’s own gh login is the only GitHub access it can have, so it is left
alone (has_other_github_identity). A guard directory an earlier start set
is dropped then, so a person who logged in with gh auth login and runs agents
gets agents that can use gh.
apply_gh_guard is called on every path that spawns an agent process or
runs a command on an agent’s behalf. Where it applies, GH_CONFIG_DIR is a
RESERVED variable: it always overwrites whatever the env already carries (a
persisted cmd:env, a caller’s override), because a single user-supplied
value would re-open the hole. Containers are not given it (host-only in agent_env_scope)
deliberately — the host path means nothing inside the image.
Constants§
- GH_
CONFIG_ DIR - META_
KEY_ PTYSHELL_ GH_ CONFIG_ DIR - Block meta on an agent’s PtyShell: the agent’s guard directory, which the
PTY spawn sets as
GH_CONFIG_DIR(blockcontroller::shell::lifecycle). A PTY shell’s env is built from its own block, not from the agent’s, so the guard has to travel on that block.
Functions§
- agent_
gh_ config_ dir <config_home>/gh-<slug>, the same locationagent.openhas always used.- apply_
gh_ guard apply_gh_guard_forunder this instance’s config dir, plus the publish guard’score.hooksPath(crate::backend::publish_guard): every path that spawns an agent process or runs a command for one comes through here.- apply_
gh_ guard_ for - Guard the agent when it has another GitHub identity; otherwise leave
ghto the person’s own login, and drop a guard directory an earlier start put in the env (aGH_CONFIG_DIRof the person’s own choosing stays). - apply_
gh_ guard_ in - Point
GH_CONFIG_DIRat this agent’s login-free directory, underconfig_home. The agent is read from the env itself (AGENTMUX_AGENT_SLUG, thenAGENTMUX_AGENT_ID). - clear_
gh_ login - Create
dirif needed and remove ahosts.ymlfrom it. - gh_
agent_ on_ this_ computer - Whether this computer has gh-agent (its hooks written, or the command on this process’s PATH): the part
of
has_other_github_identitythat is about the machine, not about the AgentMux App. Connectors says it as the GitHub section’s status (github.connection). - guard_
config_ home - Where guard dirs live. Under test, a per-process temp dir: every test that builds a spawn env reaches this, and none of them should create or clear anything in the developer’s real config home.
- guard_
dir_ for - The guard directory for an agent whose stored
cmd:envisagent_env(the slug is read from it, as for the agent’s own spawns), prepared the same way: created, with any login in it cleared. Empty when the agent is not guarded (it has no other GitHub identity), so the shell sets nothing. - guard_
dir_ for_ target guard_dir_forfor a shell that may run elsewhere.remote: a WSL distro or an SSH host, whose own GitHub identity is not known from here, so the person’s login is kept from it as every guarded spawn did before the guard became conditional.- has_
other_ github_ identity - Whether the agent whose spawn env is
envhas a GitHub identity besides the person’sghlogin:gh-agenton this machine (its hooks written, or the command on the PATH), or the AgentMux App’s broker. Only then doesapply_gh_guardkeep the person’s login from it.