diff --git a/deps/workers/README.md b/deps/workers/README.md index 25ef9f9b95..9fbef17a10 100644 --- a/deps/workers/README.md +++ b/deps/workers/README.md @@ -190,6 +190,11 @@ Cloudflare runtime flow: | OPENAI_BASE_URL | Passed into Cloudflare sandbox runtime env (if set) | | ANTHROPIC_BASE_URL | Passed into Cloudflare sandbox runtime env (if set) | +For agent tasks with a GitHub repo configured, the worker also injects a short-lived GitHub App +installation token into sandbox runtime env as `GITHUB_TOKEN`, `GH_TOKEN`, and +`GITHUB_APP_INSTALLATION_TOKEN`. +It also configures git credentials inside sandbox runtimes so plain `git push` can authenticate. + ## M14 Publish Endpoint Agent sessions now expose (from the agents worker): diff --git a/deps/workers/src/logseq/agents/runtime_provider.cljs b/deps/workers/src/logseq/agents/runtime_provider.cljs index c56a5dbe6b..62aeeaa7d1 100644 --- a/deps/workers/src/logseq/agents/runtime_provider.cljs +++ b/deps/workers/src/logseq/agents/runtime_provider.cljs @@ -314,6 +314,37 @@ (str "mkdir -p ~/.codex; " "printf \"%s\" \"" encoded "\" | base64 -d > ~/.codex/auth.json; "))) +(def ^:private github-installation-token-env-keys + ["GITHUB_TOKEN" + "GH_TOKEN" + "GITHUB_APP_INSTALLATION_TOKEN"]) + +(defn- assoc-github-installation-token + [env-vars token] + (if-not (string? token) + env-vars + (reduce (fn [acc k] (assoc acc k token)) + env-vars + github-installation-token-env-keys))) + +(defn- shell-export-command + [env-vars] + (->> env-vars + (sort-by key) + (keep (fn [[k v]] + (when (and (string? k) (string? v)) + (str "export " k "='" (escape-shell-single v) "'; ")))) + (apply str))) + +(defn- github-auth-setup-command + [token] + (if-not (string? token) + "" + (let [credentials-url (str "https://x-access-token:" token "@github.com")] + (str "git config --global credential.helper store; " + "printf '%s\\n' '" (escape-shell-single credentials-url) "' > ~/.git-credentials; " + "chmod 600 ~/.git-credentials; ")))) + (defn- get-repo-dir [session-id] (let [session-id (some-> session-id str)] (when (string? session-id) @@ -336,17 +367,26 @@ install-script (str "https://releases.rivet.dev/sandbox-agent/" version "/install.sh")] (str "(curl -fsSL " install-script " | sh);"))) +(declare /tmp/sandbox-agent.log 2>&1 &"))] - (sprites-exec-post! env sprite-name ["bash" "-lc" bootstrap]))) + (p/let [github-token (/tmp/sandbox-agent.log 2>&1 &"))] + (sprites-exec-post! env sprite-name ["bash" "-lc" bootstrap])))) (declare sprites-exec-output) (defn- (get-in task [:project :repo-url]) str string/trim not-empty)) +(defn- (source-control/ (get-in task [:project :base-branch]) source-control/sanitize-branch-name) @@ -742,7 +794,7 @@ "OPENAI_BASE_URL" "ANTHROPIC_BASE_URL"]) -(defn- cloudflare-agent-env-vars [^js env task] +(defn- (get-in task [:agent :api-token]) str string/trim not-empty)] - (cond-> (merge base task-env) - (and (string? api-token) (= "codex" agent-id)) (assoc "OPENAI_API_KEY" api-token) - (and (string? api-token) (= "claude" agent-id)) (assoc "ANTHROPIC_API_KEY" api-token)))) + (p/let [github-token ( (merge base task-env) + (and (string? api-token) (= "codex" agent-id)) (assoc "OPENAI_API_KEY" api-token) + (and (string? api-token) (= "claude" agent-id)) (assoc "ANTHROPIC_API_KEY" api-token) + (string? github-token) (assoc-github-installation-token github-token))))) (defn- cloudflare-server-command [^js env task port agent-token] (let [auth-json (get-in task [:agent :auth-json]) @@ -946,13 +1000,19 @@ (when-let [cmd (repo-clone-command env session-id task "cloudflare")] (promise (promise ( /etc/apt/sources.list.d/github-cli.list \ + && apt-get update \ + && apt-get install -y --no-install-recommends gh \ + && gh --version \ + && rm -rf /var/lib/apt/lists/* + # Prefer corepack over "npm i -g yarn" RUN corepack enable # Optionally pin yarn (pick a version you want)