Skip to main content

Module gh_guard

Module gh_guard 

Source
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 location agent.open has always used.
apply_gh_guard
apply_gh_guard_for under this instance’s config dir, plus the publish guard’s core.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 gh to the person’s own login, and drop a guard directory an earlier start put in the env (a GH_CONFIG_DIR of the person’s own choosing stays).
apply_gh_guard_in
Point GH_CONFIG_DIR at this agent’s login-free directory, under config_home. The agent is read from the env itself (AGENTMUX_AGENT_SLUG, then AGENTMUX_AGENT_ID).
clear_gh_login
Create dir if needed and remove a hosts.yml from 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_identity that 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:env is agent_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_for for 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 env has a GitHub identity besides the person’s gh login: gh-agent on this machine (its hooks written, or the command on the PATH), or the AgentMux App’s broker. Only then does apply_gh_guard keep the person’s login from it.