Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions apps/server/src/sandbox/SandboxProvisioner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7634,11 +7634,13 @@ describe("SandboxProvisioner per-thread launches", () => {
"local wasn't copied. Runs a program on this machine, so it can't be copied.",
]);

// An hour-long token for this repository alone, kept as a server secret.
// An hour-long token for every repository the installation covers, found through this
// one and kept as a server secret.
const tokenRequest = services.state.calls.find((call) =>
call.url.endsWith("/access_tokens"),
);
expect(tokenRequest?.body).toEqual({ repositories: ["hello"] });
expect(tokenRequest).toBeDefined();
expect(tokenRequest?.body ?? {}).not.toHaveProperty("repositories");
const record = repository.records.get(instanceId)!;
expect(record.githubApp).toMatchObject({ installationId: 7, repository: "octo/hello" });
expect(new TextDecoder().decode(secrets.values.get(record.githubApp!.secret))).toBe(
Expand Down Expand Up @@ -8038,6 +8040,10 @@ describe("SandboxProvisioner per-thread launches", () => {
"POST https://api.github.com/app/installations/7/access_tokens",
"DELETE https://api.github.com/installation/token",
]);
// The renewed token covers the whole installation too, and the record keeps the
// repository it finds the installation through.
expect(services.state.calls[0]?.body ?? {}).not.toHaveProperty("repositories");
expect(renewed.githubApp?.repository).toBe("octo/hello");
expect(services.state.calls[1]?.auth).toBe("Bearer ghs_OLD");
expect(secretsFileWith(fake, "T3_GITHUB_TOKEN")).toContain("ghs_SANDBOX1");
// A stopped sandbox is not woken for it.
Expand Down
10 changes: 4 additions & 6 deletions apps/server/src/sandbox/SandboxProvisioner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,9 @@ const make = Effect.gen(function* () {
const githubInstallUrl = (slug: string) =>
`https://github.com/apps/${encodeURIComponent(slug)}/installations/new`;

// Null when the app is not installed on the repository, which only the user can change.
// Finds the installation through the sandbox's repository; the token then covers every repository
// that installation does. Null when the app is not installed on the repository, which only the
// user can change.
const mintGithubToken = Effect.fn("SandboxProvisioner.mintGithubToken")(function* (
app: GithubAppCredentials,
repository: { readonly owner: string; readonly name: string },
Expand All @@ -841,11 +843,7 @@ const make = Effect.gen(function* () {
const installationId =
knownInstallation ?? (yield* githubAppApi.repositoryInstallation(app, repository));
if (installationId === null) return null;
const minted = yield* githubAppApi.createInstallationToken(
app,
installationId,
repository.name,
);
const minted = yield* githubAppApi.createInstallationToken(app, installationId);
const secret = `sandbox-github-token-${yield* secureRandom.uuidV4()}`;
yield* secrets.set(secret, new TextEncoder().encode(Redacted.value(minted.token))).pipe(
Effect.tapError(() => githubAppApi.revokeInstallationToken(minted.token).pipe(Effect.ignore)),
Expand Down
12 changes: 6 additions & 6 deletions apps/server/src/sandbox/githubAppApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,21 +143,21 @@ export const makeGithubAppApi = Effect.gen(function* () {
);
});

/** An hour-long token for one repository of the installation, with the app's permissions. */
/**
* An hour-long token with the app's permissions on every repository the installation covers. The
* user picks those repositories in the installation's settings on GitHub, so an agent can work on
* more than the sandbox's own repository.
*/
const createInstallationToken = Effect.fn("GithubAppApi.createInstallationToken")(function* (
app: GithubAppCredentials,
installationId: number,
repositoryName: string,
) {
const jwt = yield* appJwt(app);
const body = yield* send(
"making a token for the sandbox",
HttpClientRequest.post(
`${GITHUB_API}/app/installations/${installationId}/access_tokens`,
).pipe(
HttpClientRequest.bearerToken(Redacted.value(jwt)),
HttpClientRequest.bodyJsonUnsafe({ repositories: [repositoryName] }),
),
).pipe(HttpClientRequest.bearerToken(Redacted.value(jwt))),
HttpClientResponse.schemaBodyJson(InstallationToken),
);
return { token: Redacted.make(body.token), expiresAt: body.expires_at };
Expand Down
7 changes: 6 additions & 1 deletion apps/server/src/sandbox/githubAppManifestRoute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ describe("GitHub App manifest page", () => {
name: "T3 Code sandboxes",
public: false,
redirect_url: SANDBOX_GITHUB_APP_PASTE_REDIRECT_URL,
default_permissions: { contents: "write", pull_requests: "write", metadata: "read" },
default_permissions: {
contents: "write",
pull_requests: "write",
metadata: "read",
workflows: "write",
},
});
expect(body).toContain('<button type="submit">Continue to GitHub</button>');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ describe("SandboxAccessRows", () => {
checks: "read",
statuses: "read",
actions: "read",
workflows: "write",
},
}),
},
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/components/settings/SandboxAccessRows.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ const INTEGRATIONS: Record<
anchor: "sandbox-github-app",
name: "GitHub App",
about:
"Gives each sandbox an hour-long token for its own repository, revoked when it's deleted. Create the app once, then install it on the repositories sandboxes may use.",
"Gives each sandbox an hour-long token for every repository the app is installed on, revoked when it's deleted. Create the app once, then install it on the repositories sandboxes may use.",
docs: { href: GITHUB_APP_DOCS, label: "How it works" },
},
northflank: {
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/components/settings/SandboxToolingFields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ export function SandboxAgentToolingFields({
draft.githubMode === "app"
? host?.githubApp === undefined
? "The GitHub App isn't connected on this machine"
: `A token for its repository only, as ${host.githubApp.slug}`
: `A token for the repositories ${host.githubApp.slug} is installed on`
: draft.githubMode === "legacy"
? host?.github === true
? "This machine's token, not revoked on delete"
Expand Down
11 changes: 11 additions & 0 deletions docs/internals/sandbox-environments.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,17 @@ server except into the sandbox.
the machine, and the environment only when nothing is left in it, since it is the only way to
find a fork again. The sweep's abandoned-launch delete never touches forks. The driver hides
`t3-sandbox-*` environments from `listEnvironments`, so they never show as credential choices.
- **A GitHub App token covers the whole installation.** `mintGithubToken` finds the installation
through the sandbox's repository (`GET /repos/{owner}/{repo}/installation`; a 404 is "not
installed", a launch warning with the install link), then mints with no `repositories` in the
body, so the token reaches every repository the user installed the app on and an agent can push
to a second repository from one sandbox. The record keeps `{ installationId, repository, secret,
expiresAt }`; the sweep renews a running sandbox's token 20 minutes before it expires (and a
resume renews it at once) with the known installation id, writes it with the login script, then
revokes the old one. The manifest
asks for `workflows: "write"` because GitHub refuses a push that touches `.github/workflows/`
without it; an app made from an older manifest needs it added on GitHub and approved by each
installation.
- **A Railway token belongs to one sandbox.** The saved workspace token mints
`t3-sandbox-<sandboxId>` in its workspace ([`railwayApi.ts`](../../apps/server/src/sandbox/railwayApi.ts)).
Railway returns only the token's value, so every revoke lists the workspace's tokens and deletes
Expand Down
27 changes: 26 additions & 1 deletion docs/user/remote-access.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ With a hub, **Pi** also installs the [Pi agent](./providers-pi.md)
in each sandbox with the hub's non-Claude models, on that same key, and **Start new chats on** can
make new chats on the config start on Pi with one of them instead of Claude.
**GitHub** lets `gh` and `git` in each sandbox clone private repositories and push, with a
token from the GitHub App or this machine's own. Without it a sandbox can clone public repositories only.
token from the [GitHub App](#github-in-a-sandbox) or this machine's own. Without it a sandbox can
clone public repositories only.
[**Let the agent use Boat**](#let-the-agent-use-boat) signs the `boat` CLI in on each Boat sandbox
with a key scoped to it. T3 Code also
installs git, the GitHub CLI, and Claude Code when the provider's image lacks them; sign Codex in
Expand Down Expand Up @@ -230,6 +231,30 @@ have been idle for the config's **Stop when idle** time (30 minutes by default).
up, the setup card or the resume says so, and that sandbox stops at its running limit as before.
- On a Boat trial the idle time is capped at two hours, Boat's longest trial auto-stop.

### GitHub in a sandbox

With **Sign in with → GitHub App (recommended)**, each sandbox gets an hour-long token from the
app, which T3 Code renews while the sandbox runs and revokes when you delete it.

- The token covers **every repository the app is installed on**, not just the sandbox's own, so an
agent can clone, push, and open pull requests on another of your repositories in the same
thread. It is the app's installation that decides this, so the app must still be installed on the
sandbox's own repository, or the launch warns with a link to install it.
- Choose those repositories on GitHub: **Settings → Applications → Installed GitHub Apps →
Configure** next to the app (for an organization, the organization's **Settings → GitHub
Apps**), under **Repository access**. **Install on repositories** in the app's menu under
**Settings → Connections → Sandboxes** opens the same page. A running sandbox picks up a
change with its next token, at most about 40 minutes later; a resumed sandbox gets a new token
as it starts. Because every sandbox reaches every repository on that list, keep it to the
repositories you want agents to push to.
- The app asks to write contents, pull requests and workflows, and to read checks, statuses and
actions. Without Workflows, GitHub refuses a push that changes `.github/workflows/`. An app
created before T3 Code asked for it lacks it: open the app's settings on GitHub
(**Settings → Developer settings → GitHub Apps → Edit**), set **Permissions → Repository
permissions → Workflows** to **Read and write**, and save. GitHub then asks each installation to
approve the change; until you accept it under **Installed GitHub Apps → Configure**, tokens keep
the old permissions.

### Let the agent use Boat

Off by default, and offered only for Boat. With it on, the agent in each sandbox can run `boat`
Expand Down
5 changes: 4 additions & 1 deletion packages/contracts/src/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1255,6 +1255,8 @@ const SANDBOX_GITHUB_APP_PERMISSIONS = {
checks: "read",
statuses: "read",
actions: "read",
// A push that changes `.github/workflows/` needs it, or GitHub refuses the push.
workflows: "write",
} as const;

/** The sandboxes' GitHub App's homepage on github.com. */
Expand All @@ -1280,7 +1282,8 @@ export const sandboxGithubAppManifest = (input: {
name: "T3 Code sandboxes",
url: input.homepageUrl,
redirect_url: input.redirectUrl,
description: "Gives each T3 Code sandbox an hour-long token for one repository.",
description:
"Gives each T3 Code sandbox an hour-long token for every repository this app is installed on.",
public: false,
default_permissions: SANDBOX_GITHUB_APP_PERMISSIONS,
});
Expand Down
Loading