Skip to main content

agentmux_common/
github_broker_shim.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The `gh` and `git` shell functions of an agent whose GitHub access goes through AgentMux Cloud.
5//!
6//! srv puts them in the agent's environment under the name bash imports exported functions from,
7//! so they reach every bash the agent runs, whichever provider CLI started it. Both do their work
8//! through `agentmux-bashwrap` (`crates/bashwrap/src/github.rs`, `git_hooks.rs`).
9
10/// The name bash gives an exported `gh` function.
11pub const GH_FUNCTION_NAME: &str = "BASH_FUNC_gh%%";
12
13/// `gh`, for an agent whose person connected GitHub through AgentMux Cloud (srv sets
14/// `AGENTMUX_GITHUB_BROKER` for those): the command runs with the GitHub App's token for the
15/// repository it is about, or with no token at all, never with one the environment happened to
16/// carry. What a `gh pr`, `gh issue`, `gh release` or `gh api` command sends is scanned for
17/// credentials first (`github-text-guard`: a hit refuses the command, a stdin body reaches `gh` as a
18/// file), and `gh pr create` and `gh pr edit` carry the owner tag in the PR's body. `gh auth status`
19/// says so, instead of "not logged into any GitHub hosts" (the bare command only: with flags such as
20/// `--json` it is gh's own output a caller parses). A container's `gh pr merge`, `gh release` and `gh api` first go through
21/// `github-container`: srv does the merge for it, and the API writes its token cannot make are refused with what to do
22/// (`crates/bashwrap/src/container_verbs.rs`). The command word is read after a leading `-R`/`--repo` (`gh -R o/r pr merge 7`),
23/// for the container step, the text check and the owner tag alike. Without the variable it is plain `gh`.
24/// The real `gh` it runs is told (`AGENTMUX_GH_SHIM_DONE`) that these steps are done: in a container's image
25/// `gh` on PATH is `agentmux-bashwrap` too (`crates/bashwrap/src/gh_shim.rs`, for `timeout gh`, `xargs gh`, scripts),
26/// and it does the same steps, once, for a caller that did not come through this function.
27pub const GH_FUNCTION_BODY: &str = r#"() {  if [ -n "${AGENTMUX_GITHUB_BROKER:-}" ]; then
28    local _amx_t _amx_x _amx_n _amx_r _amx_c0 _amx_c1 _amx_a=();
29    if [ "$1" = auth ] && [ "$2" = status ] && [ "$#" -eq 2 ]; then
30      printf '%s
31' 'github.com' '  AgentMux signs gh in per repository: each gh command in a repository gets a short-lived token for it' '  (the AgentMux GitHub App, acting for your user). There is no stored login, so gh auth does not list one.';
32      return 0;
33    fi;
34    _amx_c0=$1; _amx_c1=${2:-};
35    case "$1" in
36      -R|--repo) _amx_c0=${3:-}; _amx_c1=${4:-};;
37      -R?*|--repo=*) _amx_c0=${2:-}; _amx_c1=${3:-};;
38    esac;
39    if [ "${AGENTMUX_IN_CONTAINER:-}" = 1 ] && { [ "$_amx_c0" = api ] || [ "$_amx_c0" = release ] || { [ "$_amx_c0" = pr ] && [ "$_amx_c1" = merge ]; }; }; then
40      if agentmux-bashwrap github-container -- "$@"; then return 0; else _amx_r=$?; fi;
41      [ "$_amx_r" -eq 3 ] || return "$_amx_r";
42    fi;
43    if [ "$_amx_c0" = pr ] || [ "$_amx_c0" = issue ] || [ "$_amx_c0" = release ] || [ "$_amx_c0" = api ]; then
44      while IFS= read -r -d '' _amx_x; do _amx_a+=("$_amx_x"); done < <(agentmux-bashwrap github-text-guard -- "$@");
45      _amx_n=${#_amx_a[@]};
46      if [ "$_amx_n" -eq 0 ]; then
47        echo 'agentmux: this gh command was not run: the text it sends could not be checked.' >&2;
48        return 1;
49      fi;
50      _amx_x="${_amx_a[$((_amx_n - 1))]}";
51      [ "$_amx_x" = ok ] || return 1;
52      unset "_amx_a[$((_amx_n - 1))]";
53      set -- "${_amx_a[@]}";
54      _amx_a=();
55    fi;
56    _amx_t="$(agentmux-bashwrap github-token -- "$@")";
57    if [ "$_amx_c0" = pr ] && { [ "$_amx_c1" = create ] || [ "$_amx_c1" = new ] || [ "$_amx_c1" = edit ]; }; then
58      while IFS= read -r -d '' _amx_x; do _amx_a+=("$_amx_x"); done < <(agentmux-bashwrap github-owner-tag -- "$@" 2>/dev/null);
59      if [ "${#_amx_a[@]}" -gt 0 ]; then set -- "${_amx_a[@]}"; fi;
60    fi;
61    if [ -n "$_amx_t" ]; then
62      GH_TOKEN="$_amx_t" GITHUB_TOKEN="$_amx_t" AGENTMUX_GH_SHIM_DONE=1 command gh "$@";
63    else
64      GH_TOKEN= GITHUB_TOKEN= AGENTMUX_GH_SHIM_DONE=1 command gh "$@";
65    fi;
66  else
67    command gh "$@";
68  fi
69}"#;
70
71/// The name bash gives an exported `git` function.
72pub const GIT_FUNCTION_NAME: &str = "BASH_FUNC_git%%";
73
74/// `git`, for the same agents: a push or a command that makes commits first asks
75/// `agentmux-bashwrap git-env`, which refuses what would skip the hooks (`git push --no-verify`) and
76/// names the App's bot as the commit's author. Any other git command is plain `git`.
77pub const GIT_FUNCTION_BODY: &str = r#"() {  if [ -n "${AGENTMUX_GITHUB_BROKER:-}" ]; then
78    case " $* " in
79      *" push "*|*" commit "*|*" merge "*|*" rebase "*|*" cherry-pick "*|*" am "*|*" revert "*|*" pull "*|*" stash "*)
80        local _amx_e _amx_v=();
81        _amx_e="$(agentmux-bashwrap git-env -- "$@")" || return $?;
82        if [ -n "$_amx_e" ]; then
83          IFS=$'\n' read -r -d '' -a _amx_v <<< "$_amx_e" || true;
84          ( export "${_amx_v[@]}"; command git "$@" );
85          return $?;
86        fi;;
87    esac;
88  fi;
89  command git "$@"
90}"#;