Multiplayer agent harness for work.
Decide tool-result provenance from the egress proxy, not command text
Josh France54d2ad6061ef
diff --git a/scripts/monitor-smoke.ts b/scripts/monitor-smoke.tsindex 4cea093db..469c5cb80 100644--- a/scripts/monitor-smoke.ts+++ b/scripts/monitor-smoke.ts@@ -119,7 +119,7 @@ try {await sb.writeFile(h, "diffusion.py", DIFFUSION_PY);console.log(`[${ts()}] background-start the diffusion job (ttl 60min) …`);- const s = await broker.start(h, "bash job.sh", 60 * 60_000);+ const s = await broker.start(h, "bash job.sh", { ttlMs: 60 * 60_000 });ok(!!s.processId && s.status.state === "running", `job ${s.processId} is running`);console.log(" early output:", JSON.stringify(s.output.trim().split("\n").slice(0, 2)));diff --git a/src/admin/egress-stamp-store.ts b/src/admin/egress-stamp-store.tsnew file mode 100644index 000000000..65c4a35dc--- /dev/null+++ b/src/admin/egress-stamp-store.ts@@ -0,0 +1,25 @@+import type { ScopeId } from "../types.ts";+import type { DurableMap } from "../persistence/durable-map.ts";++export interface EgressStamp {+ at: number;+ scopeLabel: ScopeId;+ principalId: string;+ host: string;+}++export interface EgressStampStore {+ stamp(execId: string, rec: Omit<EgressStamp, "at">): Promise<void>;+ has(execId: string): Promise<boolean>;+}++export function createEgressStampStore(map: DurableMap<EgressStamp>): EgressStampStore {+ return {+ async stamp(execId, rec) {+ await map.putIfAbsent(execId, { at: Date.now(), ...rec });+ },+ async has(execId) {+ return (await map.get(execId)) !== null;+ },+ };+}diff --git a/src/api/deps.ts b/src/api/deps.tsindex 8e38d4f82..51bf1bde1 100644--- a/src/api/deps.ts+++ b/src/api/deps.ts@@ -16,6 +16,7 @@ import type { OrgBranding, ScopedConfigStore } from "../resolution/config-store.import type { AclStore } from "../acl/acl-store.ts";import type { CredentialUsageSink } from "../admin/credential-usage-sink.ts";import type { EgressAuditSink } from "../admin/egress-audit-sink.ts";+import type { EgressStampStore } from "../admin/egress-stamp-store.ts";import type { BrokerFetch } from "./credential-broker.ts";import type { GitHttpFetch } from "./git-http-broker.ts";import type { AdminService } from "../admin/admin-service.ts";@@ -96,6 +97,7 @@ export interface ServerDeps {deviceFlowCutover?: DeviceFlowCutoverStore;featureFlags?: FeatureFlagStore;egressAudit?: EgressAuditSink;+ egressStamps?: EgressStampStore;brokerFetch?: BrokerFetch;gitHttpFetch?: GitHttpFetch;baseModelDefault?: string;diff --git a/src/api/routes/egress-audit.ts b/src/api/routes/egress-audit.tsindex cae4c19c4..861ce24f6 100644--- a/src/api/routes/egress-audit.ts+++ b/src/api/routes/egress-audit.ts@@ -53,6 +53,25 @@ async function ingestEgressAudit(ctx: ApiCtx): Promise<void> {return sendJson(res, 200, { accepted, rejected: records.length - accepted });}+async function ingestEgressStamp(ctx: ApiCtx): Promise<void> {+ const { res, deps, body } = ctx;+ if (!deps.egressStamps)+ return sendJson(res, 501, { error: "not_configured", message: "no egress stamp store wired" });+ const r = (body ?? {}) as Record<string, unknown>;+ const execId = str(r.execId);+ const host = str(r.host);+ if (!execId || !host) {+ return sendJson(res, 400, { error: "bad_request", message: "execId and host are required" });+ }+ await deps.egressStamps.stamp(execId, {+ host,+ scopeLabel: (str(r.scopeLabel) ?? "unknown") as EgressAuditRecord["scopeLabel"],+ principalId: str(r.principalId) ?? "unknown",+ });+ return sendJson(res, 200, { ok: true });+}+export const egressAuditRoutes: ReadonlyArray<Route<ApiCtx>> = [{ method: "POST", path: "/v1/egress-audit", auth: "source", handle: ingestEgressAudit },+ { method: "POST", path: "/v1/egress-stamp", auth: "source", handle: ingestEgressStamp },];diff --git a/src/auth/capability-token.ts b/src/auth/capability-token.tsindex 255fe76a2..195382813 100644--- a/src/auth/capability-token.ts+++ b/src/auth/capability-token.ts@@ -38,6 +38,7 @@ export interface CapabilityClaims {memory?: { write?: ScopeId; orgWrite?: ScopeId; read: ScopeId[] };liveActor?: boolean;runId?: string;+ execId?: string;deployment?: string;botActor?: boolean;liveAuthor?: boolean;diff --git a/src/connectors/background-exec-broker.ts b/src/connectors/background-exec-broker.tsindex db2b5103e..782294bb7 100644--- a/src/connectors/background-exec-broker.ts+++ b/src/connectors/background-exec-broker.ts@@ -27,7 +27,7 @@ export interface BackgroundStartResult {export interface BackgroundPollResult {processId: string;- command: string;+ egressId?: string;chunks: string;cursor: number;status: ProcessState;@@ -54,7 +54,11 @@ export interface BackgroundWriteResult {}export interface BackgroundExecBroker {- start(handle: SandboxHandle, command: string, ttlMs?: number): Promise<BackgroundStartResult>;+ start(+ handle: SandboxHandle,+ command: string,+ opts?: { ttlMs?: number; egressId?: string },+ ): Promise<BackgroundStartResult>;poll(handle: SandboxHandle,processId: string,@@ -84,8 +88,8 @@ export function createBackgroundBroker(deps: BackgroundExecBrokerDeps): Backgrouconst killGraceMs = deps.killGraceMs ?? DEFAULT_KILL_GRACE_MS;return {- async start(handle, command, ttlMs): Promise<BackgroundStartResult> {- const ttl = Math.min(ttlMs ?? defaultTtlMs, maxTtlMs);+ async start(handle, command, opts): Promise<BackgroundStartResult> {+ const ttl = Math.min(opts?.ttlMs ?? defaultTtlMs, maxTtlMs);const normalized = `bg: ${command.replace(/\s+/g, " ").trim()}`;const redacted = redactCommand(normalized, handle.env);@@ -136,6 +140,7 @@ export function createBackgroundBroker(deps: BackgroundExecBrokerDeps): Backgroucommand: redacted,ttlMs: ttl,...(deps.sessionRef ? { sessionRef: deps.sessionRef } : {}),+ ...(opts?.egressId ? { egressId: opts.egressId } : {}),});const { output, cursor, status } = await pollProcess(deps.sandbox, handle, processId, { deadlineMs: POLL_MS });@@ -154,7 +159,13 @@ export function createBackgroundBroker(deps: BackgroundExecBrokerDeps): BackgrouwaitMs: opts?.waitMs ?? 0,});if (read.status.state === "exited") await deps.registry.markStatus(processId, "exited");- return { processId, command: rec.command, chunks: read.chunks, cursor: read.cursor, status: read.status };+ return {+ processId,+ ...(rec.egressId ? { egressId: rec.egressId } : {}),+ chunks: read.chunks,+ cursor: read.cursor,+ status: read.status,+ };},async write(handle, processId, data): Promise<BackgroundWriteResult> {diff --git a/src/core/orchestrator.ts b/src/core/orchestrator.tsindex 42e9477eb..3b224a79f 100644--- a/src/core/orchestrator.ts+++ b/src/core/orchestrator.ts@@ -1435,20 +1435,20 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {}}const egressSecret = deps.capabilitySecret ?? deps.signingSecret;+ let egressTokenFor: ((execId: string) => Promise<string>) | undefined;if (!strictReadOnly && egressSecret) {- egressTokenForTurn = await mintCapabilityToken(- {- ...scopeAttestation,- aud: EGRESS_PROXY_AUD,- egress: egressClaimAllowingControlPlane(- resolution.egress,- deps.apiBaseUrl ?? "",- securityPolicy.inboundScreening === "external",- ),- exp: Date.now() + CAPABILITY_TTL_MS,- },- egressSecret,- );+ const egressClaims = {+ ...scopeAttestation,+ aud: EGRESS_PROXY_AUD,+ egress: egressClaimAllowingControlPlane(+ resolution.egress,+ deps.apiBaseUrl ?? "",+ securityPolicy.inboundScreening === "external",+ ),+ exp: Date.now() + CAPABILITY_TTL_MS,+ };+ egressTokenForTurn = await mintCapabilityToken(egressClaims, egressSecret);+ egressTokenFor = (execId) => mintCapabilityToken({ ...egressClaims, execId }, egressSecret);}if (!strictReadOnly && actor.type === "internal") {for (const tool of brokeredTools) {@@ -2097,6 +2097,9 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {files: deps.files,auditLog: deps.auditLog,createdBy: actor.id,+ ...(egressTokenFor && deps.egressStamps+ ? { egress: { tokenFor: egressTokenFor, stamps: deps.egressStamps } }+ : {}),...(() => {const available =strictReadOnly || actor.type !== "internal"diff --git a/src/core/orchestrator/types.ts b/src/core/orchestrator/types.tsindex 4f55bce12..14f1b991b 100644--- a/src/core/orchestrator/types.ts+++ b/src/core/orchestrator/types.ts@@ -30,6 +30,7 @@ import type { ConnectorTokenStore, Keychain, ServiceCredentialStore } from "../.import type { DeviceFlowCutoverStore } from "../../credentials/device-flow-cutover.ts";import type { FeatureFlagStore } from "../../feature-flags.ts";import type { CredentialUsageSink } from "../../admin/credential-usage-sink.ts";+import type { EgressStampStore } from "../../admin/egress-stamp-store.ts";import type { LivenessCache } from "../../credentials/resident-auth.ts";import type { ConnectorStatusCache } from "../../credentials/connector-status.ts";import type { ModelGateway } from "../../model/model-gateway.ts";@@ -169,6 +170,7 @@ export interface OrchestratorDeps {deviceFlowCutover?: DeviceFlowCutoverStore;featureFlags?: FeatureFlagStore;credentialUsage?: CredentialUsageSink;+ egressStamps?: EgressStampStore;keychain?: Keychain;serviceCreds?: ServiceCredentialStore;deliveries?: DeliveryStore;diff --git a/src/egress-authz-main.ts b/src/egress-authz-main.tsindex e484e80c9..33e1b6230 100644--- a/src/egress-authz-main.ts+++ b/src/egress-authz-main.ts@@ -5,6 +5,8 @@ import { EGRESS_PROXY_AUD, verifyCapabilityToken, type CapabilityClaims } from "import { egressDecision, hostMatches, isHostDenied, type EgressVerdict } from "./resolution/egress-policy.ts";import { createEgressAuditSink, type EgressAuditRecord, type EgressAuditSink } from "./admin/egress-audit-sink.ts";import { createPostgresEgressAuditSink } from "./admin/postgres-egress-audit-sink.ts";+import { createEgressStampStore, type EgressStamp, type EgressStampStore } from "./admin/egress-stamp-store.ts";+import { createMemoryMap, createPostgresMapFactory } from "./persistence/durable-map.ts";import { signedRequestHeaders } from "./auth/source-auth-sign.ts";import { createSweeper } from "./util/sweeper.ts";import { errMessage } from "./util/errors.ts";@@ -73,10 +75,12 @@ export function hostFromAuthority(authority: string): string | null {}export type EgressAuditRecorder = Pick<EgressAuditSink, "record">;+export type EgressStamper = Pick<EgressStampStore, "stamp">;export interface EgressAuthzDeps {capabilitySecret?: string;audit: EgressAuditRecorder;+ stamps?: EgressStamper;tokenless?: "open" | "deny";now?: () => number;lookup?: (host: string) => Promise<string[]>;@@ -122,8 +126,11 @@ async function decide(return { allow: true, verdict: "ok", address: ips[0] };}+const STAMPED_MEMORY = 10_000;+export function buildEgressAuthzServer(deps: EgressAuthzDeps): Server {const lookup = deps.lookup ?? defaultLookup;+ const stamped = new Set<string>();async function checkStatus(req: IncomingMessage,authority: string,@@ -140,7 +147,21 @@ export function buildEgressAuthzServer(deps: EgressAuthzDeps): Server {let policy: EgressPolicy | undefined = DENY_ALL;if (claims) policy = claims.egress;else if (!token && deps.tokenless === "open") policy = OPEN;- const d = await decide(host, policy, lookup);+ let d = await decide(host, policy, lookup);+ if (d.allow && claims?.execId && deps.stamps && !stamped.has(claims.execId)) {+ try {+ await deps.stamps.stamp(claims.execId, {+ scopeLabel: claims.scopeId,+ principalId: claims.actorId,+ host,+ });+ stamped.add(claims.execId);+ if (stamped.size > STAMPED_MEMORY) stamped.delete(stamped.values().next().value!);+ } catch (e) {+ console.warn(`[egress-authz] egress stamp for ${claims.execId} failed; denying ${host}: ${errMessage(e)}`);+ d = { allow: false, verdict: "denied" };+ }+ }try {deps.audit.record({source: "proxy",@@ -228,6 +249,31 @@ export function createRelayAuditSink(};}+const STAMP_PATH = "/v1/egress-stamp";++export function createRelayStamper(+ coreApiUrl: string,+ signingSecret: string,+ fetchImpl: typeof fetch = fetch,+): EgressStamper {+ const url = coreApiUrl.replace(/\/$/, "") + STAMP_PATH;+ const pathWithQuery = new URL(url).pathname;+ return {+ async stamp(execId, rec) {+ const body = JSON.stringify({ execId, ...rec });+ const res = await fetchImpl(url, {+ method: "POST",+ headers: signedRequestHeaders(signingSecret, "POST", pathWithQuery, body, {+ "content-type": "application/json",+ }),+ body,+ signal: AbortSignal.timeout(5_000),+ });+ if (!res.ok) throw new Error(`core responded ${res.status}`);+ },+ };+}+function main(): void {const port = numEnv(process.env.AUTHZ_PORT) ?? 48081;const capabilitySecret = process.env.CAPABILITY_SECRET;@@ -241,9 +287,22 @@ function main(): void {relay?.start();const audit: EgressAuditRecorder =relay ?? (databaseUrl ? createPostgresEgressAuditSink(databaseUrl) : createEgressAuditSink());+ const stamps: EgressStamper =+ coreApiUrl && relaySecret+ ? createRelayStamper(coreApiUrl, relaySecret)+ : createEgressStampStore(+ databaseUrl+ ? createPostgresMapFactory(databaseUrl).map<EgressStamp>("egress_stamps")+ : createMemoryMap<EgressStamp>(),+ );const tokenless = process.env.EGRESS_TOKENLESS === "open" ? ("open" as const) : ("deny" as const);- const server = buildEgressAuthzServer({ ...(capabilitySecret ? { capabilitySecret } : {}), audit, tokenless });+ const server = buildEgressAuthzServer({+ ...(capabilitySecret ? { capabilitySecret } : {}),+ audit,+ stamps,+ tokenless,+ });server.listen(port, "127.0.0.1", () => console.log(`[egress-authz] listening on 127.0.0.1:${port}`));for (const sig of ["SIGTERM", "SIGINT"] as const) {process.on(sig, () =>diff --git a/src/harness/agent-tools.ts b/src/harness/agent-tools.tsindex 0e48c6e26..65afb168b 100644--- a/src/harness/agent-tools.ts+++ b/src/harness/agent-tools.ts@@ -10,13 +10,12 @@ import type { McpToolDescriptor } from "../mcp/mcp-tool-service.ts";import { splitToScope } from "../api/artifact-share.ts";import { errMessage } from "../util/errors.ts";import { computerVerdict } from "../sandbox/sandbox.ts";-import { REDACTED_COMMAND_MAX_CHARS } from "../sandbox/exec-process-session.ts";import { isObj } from "../util/objects.ts";import { BOT_MODES } from "../surface-cache/channel-policy-store.ts";import { headSlice, tailSlice } from "../util/text.ts";import { GOAL_BLOCKED_MIN_ROUNDS, createGoalRecord, goalFloorMeter, goalReport, type GoalRecord } from "./goal.ts";import {- commandProvenance,+ egressProvenance,toolResultProvenance,unscreenedNotice,UNSCREENED_PREFIX,@@ -695,9 +694,7 @@ export function createAgentTools(ref: ToolContextRef, opts?: AgentToolsOptions):undefined,false,undefined,- r.reached- ? { provenance: "external", source: "reached room" }- : { provenance: commandProvenance(params.command) },+ r.reached ? { provenance: "external", source: "reached room" } : { provenance: egressProvenance(r.egressed) },);} catch (e) {if (e instanceof NeedsApproval) return blockOnApproval(callId, e, params.purpose);@@ -1414,7 +1411,7 @@ export function createAgentTools(ref: ToolContextRef, opts?: AgentToolsOptions):undefined,false,undefined,- { provenance: commandProvenance(params.command) },+ { provenance: egressProvenance(r.egressed) },);}case "poll": {@@ -1443,9 +1440,7 @@ export function createAgentTools(ref: ToolContextRef, opts?: AgentToolsOptions):undefined,false,undefined,- {- provenance: r.command.length >= REDACTED_COMMAND_MAX_CHARS ? "external" : commandProvenance(r.command),- },+ { provenance: egressProvenance(r.egressed) },);}case "stop": {diff --git a/src/harness/mock-harness.ts b/src/harness/mock-harness.tsindex feb39f75b..9e3e527a8 100644--- a/src/harness/mock-harness.ts+++ b/src/harness/mock-harness.ts@@ -12,7 +12,7 @@ import { NeedsApproval } from "../tools/primitives.ts";import { deterministicCompactSummary, estimateHistoryTokens } from "./context-compaction.ts";import { countTokens } from "../util/tokens.ts";import {- commandProvenance,+ egressProvenance,SECURITY_SCREEN_STEP,SECURITY_SCREEN_SYSTEM_PROMPT,type ToolResultScreen,@@ -400,7 +400,7 @@ export function createMockHarness(): Harness {tool: "execute",result: output,unscreenable: false,- provenance: commandProvenance(command),+ provenance: egressProvenance(result.egressed),}).catch((): ToolResultScreen => ({ outcome: "unscreened" })): ({ outcome: "allow" } as ToolResultScreen);diff --git a/src/processes/process-registry.ts b/src/processes/process-registry.tsindex 14be1c820..90b5325a2 100644--- a/src/processes/process-registry.ts+++ b/src/processes/process-registry.ts@@ -20,6 +20,7 @@ export interface ProcessRecord {status: ProcessStatus;sessionRef?: string;runId?: string;+ egressId?: string;}interface NewProcessRecord {@@ -30,6 +31,7 @@ interface NewProcessRecord {ttlMs: number;sessionRef?: string;runId?: string;+ egressId?: string;}export interface ProcessRegistry {@@ -56,6 +58,7 @@ function newRecord(rec: NewProcessRecord, now: number): ProcessRecord {status: "running",...(rec.sessionRef ? { sessionRef: rec.sessionRef } : {}),...(rec.runId ? { runId: rec.runId } : {}),+ ...(rec.egressId ? { egressId: rec.egressId } : {}),};}@@ -108,6 +111,7 @@ function pgRowToRecord(r: Record<string, unknown>): ProcessRecord {status: r.status as ProcessStatus,...(r.session_ref ? { sessionRef: r.session_ref as string } : {}),...(r.run_id ? { runId: r.run_id as string } : {}),+ ...(r.egress_id ? { egressId: r.egress_id as string } : {}),};}@@ -120,6 +124,7 @@ export function createPostgresProcessRegistry(connectionString: string): Process)`,`ALTER TABLE process_sessions ADD COLUMN IF NOT EXISTS session_ref TEXT`,`ALTER TABLE process_sessions ADD COLUMN IF NOT EXISTS run_id TEXT`,+ `ALTER TABLE process_sessions ADD COLUMN IF NOT EXISTS egress_id TEXT`,`CREATE INDEX IF NOT EXISTS idx_proc_scope_status ON process_sessions(scope_id, status)`,]);@@ -127,8 +132,8 @@ export function createPostgresProcessRegistry(connectionString: string): Processasync register(rec) {const row = newRecord(rec, Date.now());await q(- `INSERT INTO process_sessions(process_id, scope_id, kind, command, started_at, expires_at, status, session_ref, run_id)- VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)`,+ `INSERT INTO process_sessions(process_id, scope_id, kind, command, started_at, expires_at, status, session_ref, run_id, egress_id)+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)`,[row.processId,row.scopeId,@@ -139,6 +144,7 @@ export function createPostgresProcessRegistry(connectionString: string): Processrow.status,row.sessionRef ?? null,row.runId ?? null,+ row.egressId ?? null,],);return row;diff --git a/src/sandbox/exec-process-session.ts b/src/sandbox/exec-process-session.tsindex ddbb2a96c..5b9ba2e8f 100644--- a/src/sandbox/exec-process-session.ts+++ b/src/sandbox/exec-process-session.ts@@ -60,8 +60,6 @@ function redactPipedIntoWithToken(command: string): string {return `${upstream.slice(0, producer.index)}echo <redacted>${upstream.slice(upstream.trimEnd().length)}|${consumed}${rest}`;}-export const REDACTED_COMMAND_MAX_CHARS = 500;-export function redactCommand(command: string, env?: Record<string, string>): string {const flagsRedacted = createSecretValueMasker(env)(command).replace(/(--?(?:token|password|secret|client[-_]?secret|api[-_]?key)[ =])\S+/gi,@@ -72,7 +70,7 @@ export function redactCommand(command: string, env?: Record<string, string>): st/(export\s+\w*(?:PASS|PASSWORD|SECRET|TOKEN|KEY|IDENTIFIER|CREDENTIAL|PROXY_USER)\w*=')[^']*'/gi,"$1<redacted>'",)- .slice(0, REDACTED_COMMAND_MAX_CHARS);+ .slice(0, 500);}export function createExecProcessSessions(io: ExecProcessIo): ExecProcessSessions {diff --git a/src/security/security-posture.ts b/src/security/security-posture.tsindex 8f623188f..8deba3e13 100644--- a/src/security/security-posture.ts+++ b/src/security/security-posture.ts@@ -114,21 +114,8 @@ export function toolResultProvenance(tool: string): ToolResultProvenance {return "external";}-const NETWORK_COMMAND = new RegExp(- [- String.raw`[a-z][a-z0-9+.-]*://`,- String.raw`\b(curl|wget|gh|ssh|scp|sftp|rsync|nc|ncat|netcat|telnet|socat|aws|gcloud|az|fly|flyctl|psql|mysql|mongosh|redis-cli)\b`,- String.raw`\bgit\s+(clone|fetch|pull|ls-remote|submodule)\b`,- String.raw`\b(npm|npx|pnpm|yarn|bun|pip3?|uv|pipx|cargo|go)\s+(install|add|i|exec|x|dlx|get)\b`,- String.raw`\bopenssl\s+s_client\b`,- String.raw`\bfetch\(|\burllib\b|\brequests\.|\bhttpx\b|\bhttp\.client\b|\baiohttp\b`,- String.raw`\brequire\(['"](https?|net|dns|tls)['"]\)|\bfrom\s+['"]node:(https?|net|tls)['"]`,- ].join("|"),- "i",-);--export function commandProvenance(command: string): ToolResultProvenance {- return NETWORK_COMMAND.test(command) ? "external" : "workspace";+export function egressProvenance(egressed: boolean | undefined): ToolResultProvenance {+ return egressed === false ? "workspace" : "external";}export const UNSCREENED_REASON = "screen_unavailable";diff --git a/src/tools/primitives.ts b/src/tools/primitives.tsindex bdeeafb7c..18013081c 100644--- a/src/tools/primitives.ts+++ b/src/tools/primitives.ts@@ -1,5 +1,8 @@import { join } from "node:path";+import { randomUUID } from "node:crypto";import { interpolateSplitEnv } from "../deployment/deployment-layer.ts";+import { forceThroughProxyEnv } from "../sandbox/sandbox-env.ts";+import type { EgressStampStore } from "../admin/egress-stamp-store.ts";import type { CredentialPathSpec } from "../credentials/resident-paths.ts";import type { ComputerStatus, ExecResult, Sandbox, SandboxHandle } from "../sandbox/sandbox.ts";import { ROUTE_CACHE_TTL_MS, type SandboxBackendName } from "../sandbox/sandbox-routing.ts";@@ -196,7 +199,7 @@ export interface ToolContext extends SurfaceToolDeps {signal?: AbortSignal;credentials?: string[];},- ): Promise<ExecResult & { reached?: ReachedProvenance }>;+ ): Promise<ExecResult & { reached?: ReachedProvenance; egressed?: boolean }>;computerStatus(): Promise<ComputerStatus>;restartComputer(): Promise<void>;migrateComputer(to: string): Promise<{ from: string; to: string }>;@@ -212,11 +215,14 @@ export interface ToolContext extends SurfaceToolDeps {historyOpen(seq: number): Promise<string | null>;mcpToolDefs(): McpToolDescriptor[];callMcpTool(name: string, args: Record<string, unknown>): Promise<string>;- backgroundStart(command: string, opts?: { ttlSeconds?: number }): Promise<BackgroundStartResult>;+ backgroundStart(+ command: string,+ opts?: { ttlSeconds?: number },+ ): Promise<BackgroundStartResult & { egressed?: boolean }>;backgroundPoll(processId: string,opts?: { sinceCursor?: number; maxBytes?: number; waitSeconds?: number },- ): Promise<BackgroundPollResult>;+ ): Promise<BackgroundPollResult & { egressed?: boolean }>;backgroundStop(processId: string, signal?: string): Promise<BackgroundStopResult>;backgroundWrite(processId: string, data: string): Promise<BackgroundWriteResult>;backgroundList(): Promise<BackgroundJobSummary[]>;@@ -466,6 +472,7 @@ export interface ToolContextDeps {ledger?: ToolLedger;runId?: string;attempt?: number;+ egress?: { tokenFor(execId: string): Promise<string>; stamps: Pick<EgressStampStore, "has"> };backgroundBroker?: BackgroundExecBroker;monitorBroker?: MonitorBroker;persistWritesToStore?: { excludeDirs: readonly string[] };@@ -501,6 +508,14 @@ export function createToolContext(deps: ToolContextDeps): ToolContext {}}+ async function perExecEgress(handle: SandboxHandle): Promise<{ execId: string; env: Record<string, string> } | null> {+ const proxy = handle.env?.HTTPS_PROXY ?? handle.env?.https_proxy;+ if (!deps.egress || !proxy) return null;+ const u = new URL(proxy);+ const execId = randomUUID();+ return { execId, env: forceThroughProxyEnv(`${u.protocol}//${u.host}`, await deps.egress.tokenFor(execId)) };+ }+async function once<T>(produce: () => Promise<T>, shouldCache: (r: T) => boolean = () => true): Promise<T> {callIndex += 1;if (runId === undefined) return produce();@@ -656,7 +671,7 @@ export function createToolContext(deps: ToolContextDeps): ToolContext {signal?: AbortSignal;credentials?: string[];},- ): Promise<ExecResult & { reached?: ReachedProvenance }> {+ ): Promise<ExecResult & { reached?: ReachedProvenance; egressed?: boolean }> {const scratch = execOpts?.scratch === true;const ownerAuth = execOpts?.ownerAuth === true;const requestedCredentials = execOpts?.credentials ?? [];@@ -759,11 +774,16 @@ export function createToolContext(deps: ToolContextDeps): ToolContext {const sandboxCommand = ownerAuth? (deps.ownerAuthCommand?.(command) ?? command): (deps.scopedCommand?.(command) ?? command);- const commandHandle = Object.keys(commandEnv).length- ? { ...handle, env: { ...handle.env, ...commandEnv } }- : handle;+ const egress = await perExecEgress(handle);+ const env = { ...commandEnv, ...egress?.env };+ const commandHandle = Object.keys(env).length ? { ...handle, env: { ...handle.env, ...env } } : handle;const r = await deps.sandbox.run(commandHandle, sandboxCommand, opts);- return reached ? { ...r, reached } : r;+ const egressed = egress ? await deps.egress!.stamps.has(egress.execId) : undefined;+ return {+ ...r,+ ...(reached ? { reached } : {}),+ ...(egressed !== undefined ? { egressed } : {}),+ };});});},@@ -1080,7 +1100,10 @@ export function createToolContext(deps: ToolContextDeps): ToolContext {return deps.mcp.call(name, args, deps.createdBy);},- async backgroundStart(command: string, opts?: { ttlSeconds?: number }): Promise<BackgroundStartResult> {+ async backgroundStart(+ command: string,+ opts?: { ttlSeconds?: number },+ ): Promise<BackgroundStartResult & { egressed?: boolean }> {if (!deps.backgroundBroker) throw new Error(BACKGROUND_UNAVAILABLE_MESSAGE);const handle = await deps.provision();const { decision, reason, matched, approvalKey } = evaluateCommandWithLayer(@@ -1095,30 +1118,33 @@ export function createToolContext(deps: ToolContextDeps): ToolContext {if (deps.ensureSkillTree) {for (const skillDir of skillTreeDirsInCommand(command)) await deps.ensureSkillTree(skillDir);}- return once(- () =>- deps.backgroundBroker!.start(- handle,- deps.scopedCommand?.(command) ?? command,- opts?.ttlSeconds ? opts.ttlSeconds * 1000 : undefined,- ),- () => true,- );+ return once(async () => {+ const egress = await perExecEgress(handle);+ const processHandle = egress ? { ...handle, env: { ...handle.env, ...egress.env } } : handle;+ const r = await deps.backgroundBroker!.start(processHandle, deps.scopedCommand?.(command) ?? command, {+ ...(opts?.ttlSeconds ? { ttlMs: opts.ttlSeconds * 1000 } : {}),+ ...(egress ? { egressId: egress.execId } : {}),+ });+ return egress ? { ...r, egressed: await deps.egress!.stamps.has(egress.execId) } : r;+ });},async backgroundPoll(processId: string,opts?: { sinceCursor?: number; maxBytes?: number; waitSeconds?: number },- ): Promise<BackgroundPollResult> {+ ): Promise<BackgroundPollResult & { egressed?: boolean }> {if (!deps.backgroundBroker) throw new Error(BACKGROUND_UNAVAILABLE_MESSAGE);const handle = await deps.provision();return once(- () =>- deps.backgroundBroker!.poll(handle, processId, {+ async () => {+ const r = await deps.backgroundBroker!.poll(handle, processId, {...(opts?.sinceCursor !== undefined ? { sinceCursor: opts.sinceCursor } : {}),...(opts?.maxBytes !== undefined ? { maxBytes: opts.maxBytes } : {}),...(opts?.waitSeconds !== undefined ? { waitMs: opts.waitSeconds * 1000 } : {}),- }),+ });+ if (!deps.egress || !r.egressId) return r;+ return { ...r, egressed: await deps.egress.stamps.has(r.egressId) };+ },(r) => r.status.state === "exited",);},diff --git a/src/wiring.ts b/src/wiring.tsindex 0a0608673..20736c220 100644--- a/src/wiring.ts+++ b/src/wiring.ts@@ -57,6 +57,7 @@ import { createSkillBundleStore, type SkillBundle, type SkillBundleStore } fromimport { createGitFetcher, resolvePackAuth, type SkillPackFetcher } from "./skills/pack-fetcher.ts";import { installSeedSkills } from "./skills/seed.ts";import { createMemoryMap, createPostgresMapFactory, type DurableMap } from "./persistence/durable-map.ts";+import { createEgressStampStore, type EgressStamp, type EgressStampStore } from "./admin/egress-stamp-store.ts";import type { PersistedUiState, UiStateStore } from "./surfaces/ui-state.ts";import { slackUserClientFactory } from "./loops/sources/slack.ts";import { configurePgCaTrust } from "./persistence/pg-pool.ts";@@ -430,6 +431,7 @@ export interface BuiltApp {crons: CronStore;credentialUsage: CredentialUsageSink;egressAudit: EgressAuditSink;+ egressStamps: EgressStampStore;identity: IdentityService;keychain?: Keychain;serviceCreds: ServiceCredentialStore;@@ -1170,6 +1172,7 @@ export function buildApp(? createPostgresCredentialUsageSink(config.databaseUrl): createCredentialUsageSink();const egressAudit = config.databaseUrl ? createPostgresEgressAuditSink(config.databaseUrl) : createEgressAuditSink();+ const egressStamps = createEgressStampStore(artifactMap<EgressStamp>("egress_stamps"));const turnStream = createTurnStream();const sessionStateBus: SessionStateBus = config.databaseUrl? createPostgresSessionStateBus(config.databaseUrl)@@ -1437,6 +1440,7 @@ export function buildApp(deviceFlowCutover,featureFlags,credentialUsage,+ egressStamps,connectorStatusCache,resolveConnectorClient: resolveClient,...(keychain ? { keychain } : {}),@@ -2002,6 +2006,7 @@ export function buildApp(crons,credentialUsage,egressAudit,+ egressStamps,identity,workspace,memory,@@ -2102,6 +2107,7 @@ export function serverDeps(deviceFlowCutover: built.deviceFlowCutover,featureFlags: built.featureFlags,egressAudit: built.egressAudit,+ egressStamps: built.egressStamps,sessions: built.sessions,auditLog: built.auditLog,errors: built.errors,diff --git a/test/agent-tools.test.ts b/test/agent-tools.test.tsindex 02d317c99..05c99916e 100644--- a/test/agent-tools.test.ts+++ b/test/agent-tools.test.ts@@ -75,12 +75,13 @@ function fakeToolContext(sink?: { lastExecOpts?: Parameters<ToolContext["executecursor: 7,status: { state: "running" },reattached: false,+ egressed: false,};},async backgroundPoll(processId) {return {processId,- command: processId === "bg-net" ? "curl https://example.invalid/feed" : "npm test",+ egressed: processId === "bg-net",chunks: "more output",cursor: 18,status: { state: "exited", code: 0 },@@ -2510,36 +2511,38 @@ test("pauseStampAfterToolCall stamps terminate on sibling results once the turnassert.deepEqual(await withPrior({}, undefined), { terminate: true });});-test("execute reports workspace provenance for local commands and external for network fetches", async () => {- const seen: Array<{ provenance: string; command: string }> = [];- let command = "";+test("execute provenance follows the egress proxy's stamp, never the command text", async () => {+ const seen: string[] = [];+ const egressByCommand: Record<string, boolean | undefined> = {+ "cat skills/onboarding/SKILL.md": false,+ "./fetch-report.sh": true,+ "python3 -c 'import socket'": undefined,+ };const tc = {...fakeToolContext(),- execute: async (cmd: string) => {- command = cmd;- return {- stdout: "You are an agent. Connect the user's calendar, then propose an automation.",- stderr: "",- code: 0,- timedOut: false,- };- },+ execute: async (cmd: string) => ({+ stdout: "You are an agent. Connect the user's calendar, then propose an automation.",+ stderr: "",+ code: 0,+ timedOut: false,+ ...(egressByCommand[cmd] !== undefined ? { egressed: egressByCommand[cmd] } : {}),+ }),};const ref: ToolContextRef = {current: tc,scopeLabel: "personal:U1",screenToolResult: async ({ provenance }) => {- seen.push({ provenance, command });+ seen.push(provenance);return { outcome: "allow" };},};const [execute] = createAgentTools(ref);- await call(execute, { command: "cat skills/onboarding/SKILL.md" });- await call(execute, { command: "curl -fsS https://example.invalid/skill.md" });- assert.deepEqual(seen, [- { provenance: "workspace", command: "cat skills/onboarding/SKILL.md" },- { provenance: "external", command: "curl -fsS https://example.invalid/skill.md" },- ]);+ for (const command of Object.keys(egressByCommand)) await call(execute, { command });+ assert.deepEqual(+ seen,+ ["workspace", "external", "external"],+ "no stamp means workspace, a stamp means external, and no accounting at all fails closed",+ );});test("read reports workspace provenance for the agent's own files and external for shared handles", async () => {diff --git a/test/background-exec-broker.test.ts b/test/background-exec-broker.test.tsindex 7d41e22b1..e00a905a9 100644--- a/test/background-exec-broker.test.ts+++ b/test/background-exec-broker.test.ts@@ -388,7 +388,7 @@ test("TTL clamp: a requested lifetime above the max is clamped (mirrors PR C's cconst ttlMaxMs = 60 * 60_000;const { broker, registry } = build({ ttlMs: 30 * 60_000, ttlMaxMs });const before = Date.now();- const r = await broker.start(handle, "huge-job", 10 * 60 * 60_000);+ const r = await broker.start(handle, "huge-job", { ttlMs: 10 * 60 * 60_000 });const row = await registry.get(r.processId);assert.ok(row);assert.ok(row!.expiresAt <= before + ttlMaxMs + 1000);diff --git a/test/background-tool-context.test.ts b/test/background-tool-context.test.tsindex a405daad1..7ef1227a6 100644--- a/test/background-tool-context.test.ts+++ b/test/background-tool-context.test.ts@@ -21,7 +21,6 @@ function recordingBroker() {calls.poll += 1;return {processId,- command: "sleep 5",chunks: pollState === "exited" ? "final" : "partial",cursor: 10,status: pollState === "exited" ? { state: "exited", code: 0 } : { state: "running" },diff --git a/test/egress-stamp.test.ts b/test/egress-stamp.test.tsnew file mode 100644index 000000000..d5bad56d5--- /dev/null+++ b/test/egress-stamp.test.ts@@ -0,0 +1,189 @@+import "./support/auto-fake-sprites.ts";+import { test } from "node:test";+import assert from "node:assert/strict";+import { request, type Server } from "node:http";+import type { AddressInfo } from "node:net";+import { mkdtempSync } from "node:fs";+import { tmpdir } from "node:os";+import { join } from "node:path";+import { EGRESS_PROXY_AUD, mintCapabilityToken } from "../src/auth/capability-token.ts";+import { signedRequestHeaders } from "../src/auth/source-auth-sign.ts";+import { scopeId } from "../src/types.ts";+import { buildEgressAuthzServer, createRelayStamper, type EgressStamper } from "../src/egress-authz-main.ts";+import { createEgressStampStore, type EgressStamp } from "../src/admin/egress-stamp-store.ts";+import { createMemoryMap } from "../src/persistence/durable-map.ts";+import { createInsecureTestServer } from "../src/api/server.ts";+import { buildApp } from "../src/wiring.ts";+import { testConfig } from "./support/test-config.ts";++const CAPABILITY_SECRET = "test-egress-capability-secret";+const OPEN_EGRESS = { allowedHosts: ["example.com"], deniedHosts: [] };++const listen = (s: Server): Promise<number> =>+ new Promise((r) => s.listen(0, () => r((s.address() as AddressInfo).port)));+const close = (s: Server): Promise<void> => new Promise((r) => s.close(() => r()));++function token(execId?: string): Promise<string> {+ return mintCapabilityToken(+ {+ actorId: "U_actor",+ scopeId: scopeId("personal", "U_actor"),+ aud: EGRESS_PROXY_AUD,+ exp: Date.now() + 60_000,+ egress: OPEN_EGRESS,+ ...(execId ? { execId } : {}),+ },+ CAPABILITY_SECRET,+ );+}++function check(port: number, authority: string, bearer: string): Promise<number> {+ return new Promise((resolve, reject) => {+ const req = request(+ {+ port,+ host: "127.0.0.1",+ path: "/",+ headers: { "x-egress-authority": authority, "proxy-authorization": `Bearer ${bearer}` },+ },+ (res) => {+ res.resume();+ resolve(res.statusCode ?? 0);+ },+ );+ req.on("error", reject);+ req.end();+ });+}++function boot(stamps: EgressStamper) {+ const records: Array<{ host: string; allowed: boolean }> = [];+ const server = buildEgressAuthzServer({+ capabilitySecret: CAPABILITY_SECRET,+ audit: { record: (r) => void records.push({ host: r.host, allowed: r.allowed }) },+ lookup: async () => ["93.184.216.34"],+ stamps,+ });+ return { server, records };+}++test("the first allowed connection of an execution stamps it once; later connections and other executions are independent", async () => {+ const stamped: Array<{ execId: string; host: string; principalId: string }> = [];+ const { server } = boot({+ async stamp(execId, rec) {+ stamped.push({ execId, host: rec.host, principalId: rec.principalId });+ },+ });+ const port = await listen(server);+ try {+ const a = await token("exec-a");+ assert.equal(await check(port, "example.com:443", a), 200);+ assert.equal(await check(port, "example.com:443", a), 200);+ assert.equal(await check(port, "example.com:443", await token("exec-b")), 200);+ assert.equal(await check(port, "example.com:443", await token()), 200, "a turn token without execId still works");+ assert.deepEqual(stamped, [+ { execId: "exec-a", host: "example.com", principalId: "U_actor" },+ { execId: "exec-b", host: "example.com", principalId: "U_actor" },+ ]);+ } finally {+ await close(server);+ }+});++test("a denied host never stamps, and a stamp that cannot be recorded fails the connection closed", async () => {+ let calls = 0;+ const { server, records } = boot({+ async stamp() {+ calls++;+ throw new Error("core unreachable");+ },+ });+ const port = await listen(server);+ try {+ assert.equal(await check(port, "evil.invalid:443", await token("exec-denied")), 403);+ assert.equal(calls, 0, "policy denial happens before any stamp");+ assert.equal(await check(port, "example.com:443", await token("exec-c")), 403);+ assert.equal(calls, 1);+ assert.deepEqual(records.at(-1), { host: "example.com", allowed: false });+ } finally {+ await close(server);+ }+});++test("the stamp store answers has() only after a stamp and ignores duplicate stamps", async () => {+ const store = createEgressStampStore(createMemoryMap<EgressStamp>());+ assert.equal(await store.has("exec-1"), false);+ await store.stamp("exec-1", { scopeLabel: scopeId("personal", "U1"), principalId: "U1", host: "a.example" });+ await store.stamp("exec-1", { scopeLabel: scopeId("personal", "U1"), principalId: "U1", host: "b.example" });+ assert.equal(await store.has("exec-1"), true);+ assert.equal(await store.has("exec-2"), false);+});++test("the relay stamper posts a signed stamp to the core and surfaces a non-2xx as a failure", async () => {+ const seen: Array<{ url: string; body: unknown; signed: boolean }> = [];+ let status = 200;+ const stamper = createRelayStamper("https://core.test/", "relay-secret", (async (url: string, init: RequestInit) => {+ const headers = init.headers as Record<string, string>;+ seen.push({+ url,+ body: JSON.parse(String(init.body)),+ signed: Object.keys(headers).some((k) => /signature|source/i.test(k)),+ });+ return new Response(null, { status });+ }) as unknown as typeof fetch);+ await stamper.stamp("exec-9", { scopeLabel: scopeId("personal", "U1"), principalId: "U1", host: "api.example" });+ assert.equal(seen[0]!.url, "https://core.test/v1/egress-stamp");+ assert.deepEqual(seen[0]!.body, {+ execId: "exec-9",+ scopeLabel: "personal:U1",+ principalId: "U1",+ host: "api.example",+ });+ assert.equal(seen[0]!.signed, true);+ status = 503;+ await assert.rejects(+ stamper.stamp("exec-10", { scopeLabel: scopeId("personal", "U1"), principalId: "U1", host: "api.example" }),+ /503/,+ );+});++test("the core ingests a signed stamp and rejects unsigned or malformed ones", async () => {+ const config = testConfig({ dataDir: mkdtempSync(join(tmpdir(), "egress-stamp-")) });+ const built = buildApp(config);+ const server = createInsecureTestServer(built.app, {+ admin: built.admin,+ sessions: built.sessions,+ auditLog: built.auditLog,+ egressStamps: built.egressStamps,+ });+ server.listen(0);+ const base = `http://localhost:${(server.address() as AddressInfo).port}`;+ try {+ const body = JSON.stringify({+ execId: "exec-42",+ host: "api.example",+ scopeLabel: "personal:U1",+ principalId: "U1",+ });+ const signed = await fetch(base + "/v1/egress-stamp", {+ method: "POST",+ headers: signedRequestHeaders(config.signingSecret!, "POST", "/v1/egress-stamp", body, {+ "content-type": "application/json",+ }),+ body,+ });+ assert.equal(signed.status, 200);+ assert.equal(await built.egressStamps.has("exec-42"), true);++ const malformed = await fetch(base + "/v1/egress-stamp", {+ method: "POST",+ headers: signedRequestHeaders(config.signingSecret!, "POST", "/v1/egress-stamp", "{}", {+ "content-type": "application/json",+ }),+ body: "{}",+ });+ assert.equal(malformed.status, 400);+ } finally {+ await new Promise<void>((r) => server.close(() => r()));+ }+});diff --git a/test/orchestrator.test.ts b/test/orchestrator.test.tsindex db4c13cc1..238144944 100644--- a/test/orchestrator.test.ts+++ b/test/orchestrator.test.ts@@ -22,12 +22,31 @@ import type { AclStore } from "../src/acl/acl-store.ts";import type { ScopeId } from "../src/types.ts";import type { SecurityScreener } from "../src/security/security-screener.ts";+const FAKE_EGRESS_PROXY = "http://egress.test:48080";+function freshApp(overrides: Partial<Config> = {}, securityScreener?: SecurityScreener) {const config = testConfig({dataDir: mkdtempSync(join(tmpdir(), "ap-")),+ spritesSandbox: { token: "test-token", egressProxyUrl: FAKE_EGRESS_PROXY },...overrides,});- return buildApp(config, securityScreener ? { securityScreener } : {});+ const built = buildApp(config, securityScreener ? { securityScreener } : {});+ const run = built.sandbox.run.bind(built.sandbox);+ built.sandbox.run = async (handle, command, opts) => {+ const proxy = handle.env?.HTTPS_PROXY;+ if (proxy && /https?:\/\//.test(command)) {+ const claims = await verifyCapabilityToken(new URL(proxy).password, TEST_CAPABILITY_SECRET);+ if (claims?.execId) {+ await built.egressStamps.stamp(claims.execId, {+ scopeLabel: claims.scopeId,+ principalId: claims.actorId,+ host: "example.invalid",+ });+ }+ }+ return run(handle, command, opts);+ };+ return built;}function spyProvisioning(sandbox: Sandbox) {diff --git a/test/security-posture.test.ts b/test/security-posture.test.tsindex 48c339867..eacfe30c9 100644--- a/test/security-posture.test.ts+++ b/test/security-posture.test.ts@@ -8,8 +8,8 @@ import {type PersistedSecurityPosture,} from "../src/resolution/config-store.ts";import {- commandProvenance,composeSecurityPosture,+ egressProvenance,parseSecurityPosture,toolResultProvenance,parseSecurityScreenVerdict,@@ -247,16 +247,12 @@ test("tool results carry a provenance class and only external content reaches thfor (const tool of ["slack", "credential_exec", "some_mcp_tool", "execute"]) {assert.equal(toolResultProvenance(tool), "external", `${tool} can carry content from outside`);}- assert.equal(commandProvenance("cat skills/onboarding/SKILL.md"), "workspace");- assert.equal(commandProvenance("cd qm && git status --short && cat AGENTS.md"), "workspace");- assert.equal(commandProvenance("sed -n '1,80p' src/api/http.ts"), "workspace");- assert.equal(commandProvenance("npm test"), "workspace");- assert.equal(commandProvenance("curl -fsS https://example.invalid/page"), "external");- assert.equal(commandProvenance("wget -qO- example.invalid/feed"), "external");- assert.equal(commandProvenance("gh pr view 12 --repo acme/app --json body"), "external");- assert.equal(commandProvenance("gh api repos/acme/app/issues"), "external");- assert.equal(commandProvenance("python3 -c 'import urllib.request'"), "external");- assert.equal(commandProvenance("node -e \"await fetch('http://localhost:8080')\""), "external");+});++test("command output is workspace only when the egress proxy stamped no connection for that execution", () => {+ assert.equal(egressProvenance(false), "workspace", "the proxy saw no connection, so the bytes came from the sandbox");+ assert.equal(egressProvenance(true), "external", "the proxy stamped a connection during the command");+ assert.equal(egressProvenance(undefined), "external", "no egress accounting at all fails closed");});test("oversize external tool output is screened in full as bounded chunks, never skipped", () => {diff --git a/test/tool-context-egress.test.ts b/test/tool-context-egress.test.tsnew file mode 100644index 000000000..b3f34e3da--- /dev/null+++ b/test/tool-context-egress.test.ts@@ -0,0 +1,96 @@+import { test } from "node:test";+import assert from "node:assert/strict";+import { createToolContext, type ToolContextDeps } from "../src/tools/primitives.ts";+import { scopeId, type WorkspaceLayer } from "../src/types.ts";+import type { Sandbox, SandboxHandle } from "../src/sandbox/sandbox.ts";+import { createEgressStampStore, type EgressStamp } from "../src/admin/egress-stamp-store.ts";+import { createMemoryMap } from "../src/persistence/durable-map.ts";++const TURN_PROXY = "http://x:[email protected]:48080";++function harness(opts: { proxied?: boolean; egress?: boolean } = {}) {+ const runs: Array<{ command: string; proxy: string | undefined }> = [];+ const minted: string[] = [];+ const stamps = createEgressStampStore(createMemoryMap<EgressStamp>());+ const handle: SandboxHandle = {+ id: "h",+ rootDir: "/workspace",+ ...(opts.proxied === false ? {} : { env: { HTTPS_PROXY: TURN_PROXY, https_proxy: TURN_PROXY } }),+ };+ const sandbox = {+ async run(h: SandboxHandle, command: string) {+ runs.push({ command, proxy: h.env?.HTTPS_PROXY });+ return { stdout: "out", stderr: "", code: 0, timedOut: false };+ },+ } as unknown as Sandbox;+ const scope = scopeId("personal", "U1");+ const layers: WorkspaceLayer[] = [{ scopeId: scope, mountPath: "", mode: "rw" }];+ const deps: ToolContextDeps = {+ sandbox,+ provision: async () => handle,+ layers,+ commandPolicy: () => ({ mode: "denylist", rules: [] }),+ authorizeCommand: () => false,+ grantedHandles: [],+ workspace: {} as never,+ deploy: {} as never,+ acl: {} as never,+ createdBy: "U1",+ ...(opts.egress === false+ ? {}+ : {+ egress: {+ async tokenFor(execId: string) {+ minted.push(execId);+ return `tok-${execId}`;+ },+ stamps,+ },+ }),+ };+ return { ctx: createToolContext(deps), runs, minted, stamps };+}++const tokenIn = (proxy: string | undefined) => (proxy ? new URL(proxy).password : undefined);++test("every execute runs under its own egress credential carrying a fresh execution id", async () => {+ const { ctx, runs, minted } = harness();+ await ctx.execute("cat README.md");+ await ctx.execute("./fetch-report.sh");+ assert.equal(minted.length, 2);+ assert.notEqual(minted[0], minted[1]);+ assert.deepEqual(+ runs.map((r) => tokenIn(r.proxy)),+ minted.map((id) => `tok-${id}`),+ "the sandbox sees the per-command token, not the turn token",+ );+ assert.ok(+ runs.every((r) => new URL(r.proxy!).host === "egress.test:48080"),+ "the proxy host is unchanged",+ );+});++test("execute reports egressed from the proxy's stamp for that execution, not from the command text", async () => {+ const { ctx, minted, stamps } = harness();+ const quiet = await ctx.execute("curl https://looks-like-network.example");+ assert.equal(quiet.egressed, false, "a command that never reached the proxy is not external, whatever it says");++ const original = stamps.has.bind(stamps);+ stamps.has = async (execId) => {+ if (execId === minted.at(-1)) {+ await stamps.stamp(execId, { scopeLabel: scopeId("personal", "U1"), principalId: "U1", host: "api.example" });+ }+ return original(execId);+ };+ const noisy = await ctx.execute("./innocent-looking.sh");+ assert.equal(noisy.egressed, true, "the stamp, not the command, decides");+});++test("without an egress proxy on the handle or without egress accounting, execute reports nothing", async () => {+ const unproxied = harness({ proxied: false });+ assert.equal((await unproxied.ctx.execute("ls")).egressed, undefined);+ assert.equal(unproxied.minted.length, 0, "no token is minted when the sandbox has no proxy to present it to");+ const unaccounted = harness({ egress: false });+ assert.equal((await unaccounted.ctx.execute("ls")).egressed, undefined);+ assert.equal(unaccounted.runs[0]!.proxy, TURN_PROXY, "the turn credential is left alone");+});