Skip to content

Commit b1f0509

Browse files
committed
fix(agent): run gh attribution/whoami as the current actor
fetchPrAttribution and fetchGhLogin called gh with no env, inheriting the process env — the actor's token frozen at launch. After a mid-session actor transition that reports the wrong identity, and once the backend stops baking the token into the process env these calls would run unauthenticated. Resolve the live sandbox token (same file-first path as the signed-commit tools) and pass it explicitly; fall back to the process env only when unmanaged.
1 parent fbfffb6 commit b1f0509

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

packages/agent/src/server/agent-server.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
import { type ServerType, serve } from "@hono/node-server";
1717
import { execGh } from "@posthog/git/gh";
1818
import { getCurrentBranch } from "@posthog/git/queries";
19+
import { ghTokenEnv } from "@posthog/git/signed-commit";
1920
import {
2021
type Adapter,
2122
buildPrOutput,
@@ -82,6 +83,7 @@ import {
8283
resolveGatewayProduct,
8384
resolveLlmGatewayUrl,
8485
} from "../utils/gateway";
86+
import { resolveGithubToken } from "../utils/github-token";
8587
import { Logger } from "../utils/logger";
8688
import { logAgentshRuntimeInfo } from "./agentsh-runtime";
8789
import {
@@ -3786,6 +3788,15 @@ ${signedCommitInstructions}${prLinkInstructions}${shellEfficiencyInstructions}
37863788
}
37873789
}
37883790

3791+
/** Env for a `gh` call that must run as the *current* actor. Prefers the live
3792+
* sandbox token (rewritten on an actor transition) over the process env
3793+
* (frozen at launch); returns undefined when unmanaged (local/desktop) so
3794+
* execGh falls back to the process env. */
3795+
private ghActorEnv(): Record<string, string> | undefined {
3796+
const token = resolveGithubToken();
3797+
return token === undefined ? undefined : ghTokenEnv(token);
3798+
}
3799+
37893800
private async fetchPrAttribution(
37903801
prUrl: string,
37913802
): Promise<{ createdAt: string | null; author: string | null }> {
@@ -3794,6 +3805,7 @@ ${signedCommitInstructions}${prLinkInstructions}${shellEfficiencyInstructions}
37943805
{
37953806
cwd: this.config.repositoryPath,
37963807
timeoutMs: 10_000,
3808+
env: this.ghActorEnv(),
37973809
},
37983810
);
37993811
if (res.exitCode !== 0) return { createdAt: null, author: null };
@@ -3817,6 +3829,7 @@ ${signedCommitInstructions}${prLinkInstructions}${shellEfficiencyInstructions}
38173829
this.ghLoginPromise ??= execGh(["api", "user", "--jq", ".login"], {
38183830
cwd: this.config.repositoryPath,
38193831
timeoutMs: 10_000,
3832+
env: this.ghActorEnv(),
38203833
})
38213834
.then((res) => {
38223835
const login = res.exitCode === 0 ? res.stdout.trim() : "";

0 commit comments

Comments
 (0)