-
Notifications
You must be signed in to change notification settings - Fork 32
feat(cloud-agent): sandbox gh token refresh via refresh-session #2450
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import { mkdtempSync, writeFileSync } from "node:fs"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
| import { | ||
| readGithubTokenFromSandboxEnvFile, | ||
| resolveGithubToken, | ||
| } from "./github-token"; | ||
|
|
||
| function writeEnvFile(contents: string): string { | ||
| const dir = mkdtempSync(join(tmpdir(), "agent-env-")); | ||
| const path = join(dir, "agent-env"); | ||
| writeFileSync(path, contents); | ||
| return path; | ||
| } | ||
|
|
||
| describe("github-token", () => { | ||
| describe("readGithubTokenFromSandboxEnvFile", () => { | ||
| it.each([ | ||
| { | ||
| name: "GH_TOKEN", | ||
| contents: "PATH=/usr/bin\0GH_TOKEN=ghs_fresh123\0HOME=/root\0", | ||
| expected: "ghs_fresh123", | ||
| }, | ||
| { | ||
| name: "GITHUB_TOKEN when GH_TOKEN is absent", | ||
| contents: "GITHUB_TOKEN=ghu_user456\0PATH=/usr/bin\0", | ||
| expected: "ghu_user456", | ||
| }, | ||
| ])( | ||
| "reads $name from the NUL-delimited env file", | ||
| ({ contents, expected }) => { | ||
| expect(readGithubTokenFromSandboxEnvFile(writeEnvFile(contents))).toBe( | ||
| expected, | ||
| ); | ||
| }, | ||
| ); | ||
|
|
||
| it("reflects an updated file (live read, not cached)", () => { | ||
| const path = writeEnvFile("GH_TOKEN=ghs_old\0"); | ||
| expect(readGithubTokenFromSandboxEnvFile(path)).toBe("ghs_old"); | ||
| writeFileSync(path, "GH_TOKEN=ghs_new\0"); | ||
| expect(readGithubTokenFromSandboxEnvFile(path)).toBe("ghs_new"); | ||
| }); | ||
|
|
||
| it("returns undefined when the file is missing", () => { | ||
| expect( | ||
| readGithubTokenFromSandboxEnvFile("/nonexistent/agent-env"), | ||
| ).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("ignores an empty token value", () => { | ||
| const path = writeEnvFile("GH_TOKEN=\0GITHUB_TOKEN=ghs_real\0"); | ||
| expect(readGithubTokenFromSandboxEnvFile(path)).toBe("ghs_real"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("resolveGithubToken", () => { | ||
| afterEach(() => { | ||
| vi.unstubAllEnvs(); | ||
| }); | ||
|
|
||
| it("prefers the sandbox env file over the process env", () => { | ||
| vi.stubEnv("GH_TOKEN", "ghs_fromprocess"); | ||
| const path = writeEnvFile("GH_TOKEN=ghs_fromfile\0"); | ||
| expect(resolveGithubToken(path)).toBe("ghs_fromfile"); | ||
| }); | ||
|
|
||
| it("falls back to the process env when the sandbox file is absent", () => { | ||
| vi.stubEnv("GH_TOKEN", "ghs_fromprocess"); | ||
| expect(resolveGithubToken("/nonexistent/agent-env")).toBe( | ||
| "ghs_fromprocess", | ||
| ); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { readFileSync } from "node:fs"; | ||
| import { readGithubTokenFromEnv } from "@posthog/git/signed-commit"; | ||
|
|
||
| // helpers for resolving the in-sandbox GitHub token | ||
| // agentsh env file (NUL-delimited `key=value` pairs) that the PostHog backend | ||
| // rewrites in place when it refreshes the sandbox's GitHub credentials | ||
| // mid-session. The agent-server process env is frozen at launch, so reading | ||
| // this live file is how in-process tools pick up a refreshed token without a | ||
| // process restart. | ||
| export const SANDBOX_ENV_FILE = "/tmp/agent-env"; | ||
|
|
||
| export function readGithubTokenFromSandboxEnvFile( | ||
| envFilePath: string = SANDBOX_ENV_FILE, | ||
| ): string | undefined { | ||
| try { | ||
| const raw = readFileSync(envFilePath, "utf8"); | ||
| const env: Record<string, string> = {}; | ||
| for (const entry of raw.split("\0")) { | ||
| const eq = entry.indexOf("="); | ||
| if (eq > 0) { | ||
| env[entry.slice(0, eq)] = entry.slice(eq + 1); | ||
| } | ||
| } | ||
| // Reuse the shared token-var allowlist + precedence instead of hardcoding. | ||
| return readGithubTokenFromEnv(env); | ||
| } catch { | ||
| // No env file (local/desktop or test) — fall back to the process env. | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| /** The GitHub token available to the sandbox, if any. | ||
| * | ||
| * Prefers the live agentsh env file (refreshed in place mid-session) over the | ||
| * process env (frozen at launch) so long-running in-process tools — e.g. the | ||
| * signed-commit tool — pick up a refreshed token without a restart. | ||
| */ | ||
| export function resolveGithubToken( | ||
| envFilePath: string = SANDBOX_ENV_FILE, | ||
| ): string | undefined { | ||
| return ( | ||
| readGithubTokenFromSandboxEnvFile(envFilePath) ?? readGithubTokenFromEnv() | ||
| ); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.