Multiplayer agent harness for work.
Give sandbox-facing turn tokens a 48h TTL (#1518)
Joshua France0103052c9e37
diff --git a/src/auth/capability-token.ts b/src/auth/capability-token.tsindex 021812f3f..f23ff5c49 100644--- a/src/auth/capability-token.ts+++ b/src/auth/capability-token.ts@@ -3,6 +3,7 @@ import type { CandidateDestination, Destination, EgressPolicy, Principal, ScopeIimport { mintSignedPayload, verifySignedPayload } from "./signed-token.ts";export const CAPABILITY_TTL_MS = 60 * 60_000;+export const SANDBOX_CAPABILITY_TTL_MS = 48 * 60 * 60_000;export const DEPLOYMENT_CREDENTIAL_TTL_MS = 10 * 365 * 24 * 60 * 60_000;export const CONTROL_PLANE_AUD = "control-plane";diff --git a/src/core/orchestrator.ts b/src/core/orchestrator.tsindex d5bbd866b..c8e172824 100644--- a/src/core/orchestrator.ts+++ b/src/core/orchestrator.ts@@ -79,6 +79,7 @@ import { estimateCostUsd } from "../ratelimit/budget.ts";import {mintCapabilityToken,CAPABILITY_TTL_MS,+ SANDBOX_CAPABILITY_TTL_MS,CONTROL_PLANE_AUD,OAUTH_CONSENT_AUD,CREDENTIAL_BROKER_AUD,@@ -1538,7 +1539,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {controlClaims = {...scopeAttestation,aud: CONTROL_PLANE_AUD,- exp: Date.now() + CAPABILITY_TTL_MS,+ exp: Date.now() + SANDBOX_CAPABILITY_TTL_MS,...(turnTimezone ? { timezone: turnTimezone } : {}),...(destination ? { destination } : {}),...(delivery.candidates.length > 0 ? { destinations: delivery.candidates } : {}),@@ -1570,7 +1571,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {{...scopeAttestation,aud: OAUTH_CONSENT_AUD,- exp: Date.now() + CAPABILITY_TTL_MS,+ exp: Date.now() + SANDBOX_CAPABILITY_TTL_MS,},deps.capabilitySecret ?? deps.signingSecret,);@@ -1587,7 +1588,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {...scopeAttestation,aud: CREDENTIAL_BROKER_AUD,credentials: slugs,- exp: Date.now() + CAPABILITY_TTL_MS,+ exp: Date.now() + SANDBOX_CAPABILITY_TTL_MS,},deps.capabilitySecret ?? deps.signingSecret,);@@ -1637,7 +1638,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {deps.apiBaseUrl ?? "",securityPolicy.denyPrivateNetworks,),- exp: Date.now() + CAPABILITY_TTL_MS,+ exp: Date.now() + SANDBOX_CAPABILITY_TTL_MS,},egressSecret,);diff --git a/src/harness/agent-tools.ts b/src/harness/agent-tools.tsindex 329c6cd97..4c8427f70 100644--- a/src/harness/agent-tools.ts+++ b/src/harness/agent-tools.ts@@ -28,7 +28,7 @@ import {type ToolResultScreen,type ToolResultScreenInput,} from "../security/security-posture.ts";-import { CAPABILITY_TTL_MS } from "../auth/capability-token.ts";+import { SANDBOX_CAPABILITY_TTL_MS } from "../auth/capability-token.ts";import { CRON_FIRE_NOTE_MAX_CHARS } from "../api/control-service.ts";import { utcMinute } from "../util/time.ts";@@ -397,7 +397,7 @@ export function createAgentTools(ref: ToolContextRef, opts?: AgentToolsOptions):const bgTtlMaxSec = Math.round((opts?.backgroundJobTtlMaxMs ?? CONFIG_DEFAULTS.backgroundJobTtlMaxSec * 1000) / 1000);const bgTtlMin = Math.round(bgTtlSec / 60);const bgTtlMaxMin = Math.round(bgTtlMaxSec / 60);- const capabilityTtlMin = Math.round(CAPABILITY_TTL_MS / 60_000);+ const capabilityTtlHours = Math.round(SANDBOX_CAPABILITY_TTL_MS / 3_600_000);const log = async (type: EntryType, payload: unknown, sourceScopeId?: ScopeId | null): Promise<void> => {if (!ref.emit || !ref.scopeLabel) return;const scopeLabel = classifyScopeLabel({@@ -1630,7 +1630,7 @@ export function createAgentTools(ref: ToolContextRef, opts?: AgentToolsOptions):"it's stopped automatically (a watch survives just long enough to tell you) — for anything " +`that finishes within ${execCeilingSec}s, just use \`execute\`. Available on the default sandbox or an authorized explicit sandbox_id; ` +"elsewhere, use `execute`. A background job carries the same environment a foreground `execute` " +- `does — $AGENT_API_URL, $AGENT_API_TOKEN and $AGENT_CREDENTIAL_TOKEN all work, so self-API calls and shared-credential broker calls run fine from background work. Two limits: those turn tokens expire ${capabilityTtlMin} minutes after the turn that launched the job started (past that they 401 — checkpoint your progress to the workspace and continue from a later turn or a cron), and a background job cannot deliver a file itself, so write results to ordinary workspace paths and attach them from a live turn after polling.\n` ++ `does — $AGENT_API_URL, $AGENT_API_TOKEN and $AGENT_CREDENTIAL_TOKEN all work, so self-API calls and shared-credential broker calls run fine from background work. Two limits: those turn tokens expire ${capabilityTtlHours} hours after the turn that launched the job started (past that they 401 — checkpoint your progress to the workspace and continue from a later turn or a cron), and a background job cannot deliver a file itself, so write results to ordinary workspace paths and attach them from a live turn after polling.\n` +"INTERACTIVE LOGINS: device-flow logins (`gh auth login`, " +"`glab auth login`, `gcloud auth login`, and anything that prints a verification URL/code then " +"blocks waiting on a human) belong here, NOT in `execute`. Run them with action=start, read the " +diff --git a/test/agent-tools.test.ts b/test/agent-tools.test.tsindex 69853b179..8a002fb02 100644--- a/test/agent-tools.test.ts+++ b/test/agent-tools.test.ts@@ -1996,7 +1996,7 @@ test("background dispatches each action and emits tool_call/tool_result", asyncassert.ok(background);assert.match(background.description, /same environment a foreground `execute` does/);assert.match(background.description, /\$AGENT_CREDENTIAL_TOKEN all work/);- assert.match(background.description, /expire 60 minutes after the turn/);+ assert.match(background.description, /expire 48 hours after the turn/);const started = textOf(await call(background, { action: "start", command: "npm run build" }));assert.match(started, /started bg-1/);diff --git a/test/orchestrator.test.ts b/test/orchestrator.test.tsindex 371a40d5e..412fad62e 100644--- a/test/orchestrator.test.ts+++ b/test/orchestrator.test.ts@@ -10,7 +10,12 @@ import { TEST_CAPABILITY_SECRET, testConfig } from "./support/test-config.ts";import { runNowSettled } from "./support/settle.ts";import { loadConfig, type Config } from "../src/config.ts";import type { ProvisionOptions, Sandbox } from "../src/sandbox/sandbox.ts";-import { verifyCapabilityToken, EGRESS_PROXY_AUD } from "../src/auth/capability-token.ts";+import {+ verifyCapabilityToken,+ EGRESS_PROXY_AUD,+ CAPABILITY_TTL_MS,+ SANDBOX_CAPABILITY_TTL_MS,+} from "../src/auth/capability-token.ts";import { egressClaimAllowingControlPlane } from "../src/core/orchestrator.ts";import { SESSION_BUSY_USER_TEXT } from "../src/core/failure-copy.ts";import { TURN_FILES_DIR, turnFileId } from "../src/core/attachments.ts";@@ -1008,6 +1013,40 @@ test("turn timezone rides the prompt and control-plane capability token", asyncassert.equal(invalidClaims!.timezone, undefined, "invalid surface timezones are omitted from the token");});+test("sandbox-facing turn tokens carry the long sandbox TTL, not the hour-long capability TTL", async () => {+ const config = testConfig({+ dataDir: mkdtempSync(join(tmpdir(), "ap-")),+ signingSecret: "test-secret",+ apiBaseUrl: "https://core.example.com",+ });+ const { app, sandbox } = buildApp(config);+ let captured: ProvisionOptions | undefined;+ const realProvision = sandbox.provision.bind(sandbox);+ sandbox.provision = (layers, opts) => {+ captured = opts;+ return realProvision(layers, opts);+ };++ const before = Date.now();+ const res = await app.turn(dm("!run echo ttl", { conversation: { kind: "dm", threadRef: "dm:U1:ttl" } }));+ assert.equal(res.status, "ok");+ const tokens = {+ AGENT_API_TOKEN: captured!.env!.AGENT_API_TOKEN!,+ AGENT_OAUTH_CONSENT_TOKEN: captured!.env!.AGENT_OAUTH_CONSENT_TOKEN!,+ egressToken: captured!.egressToken!,+ };+ for (const [name, token] of Object.entries(tokens)) {+ const claims = await verifyCapabilityToken(token, TEST_CAPABILITY_SECRET);+ assert.ok(claims, `${name} verifies`);+ assert.ok(claims.exp > before + CAPABILITY_TTL_MS, `${name} outlives the hour-long TTL`);+ assert.ok(claims.exp <= Date.now() + SANDBOX_CAPABILITY_TTL_MS, `${name} is capped at the sandbox TTL`);+ assert.ok(+ await verifyCapabilityToken(token, TEST_CAPABILITY_SECRET, before + 9 * 3_600_000),+ `${name} still verifies nine hours in`,+ );+ }+});+test("unattended grants enter capability claims only on non-live turns", async () => {const config = testConfig({dataDir: mkdtempSync(join(tmpdir(), "ap-")),