paperclip/server/src/services/workspace-realization.ts

272 lines
9.7 KiB
TypeScript
Raw Normal View History

Add sandbox environment support (#4415) ## Thinking Path > - Paperclip orchestrates AI agents for zero-human companies. > - The environment/runtime layer decides where agent work executes and how the control plane reaches those runtimes. > - Today Paperclip can run locally and over SSH, but sandboxed execution needs a first-class environment model instead of one-off adapter behavior. > - We also want sandbox providers to be pluggable so the core does not hardcode every provider implementation. > - This branch adds the Sandbox environment path, the provider contract, and a deterministic fake provider plugin. > - That required synchronized changes across shared contracts, plugin SDK surfaces, server runtime orchestration, and the UI environment/workspace flows. > - The result is that sandbox execution becomes a core control-plane capability while keeping provider implementations extensible and testable. ## What Changed - Added sandbox runtime support to the environment execution path, including runtime URL discovery, sandbox execution targeting, orchestration, and heartbeat integration. - Added plugin-provider support for sandbox environments so providers can be supplied via plugins instead of hardcoded server logic. - Added the fake sandbox provider plugin with deterministic behavior suitable for local and automated testing. - Updated shared types, validators, plugin protocol definitions, and SDK helpers to carry sandbox provider and workspace-runtime contracts across package boundaries. - Updated server routes and services so companies can create sandbox environments, select them for work, and execute work through the sandbox runtime path. - Updated the UI environment and workspace surfaces to expose sandbox environment configuration and selection. - Added test coverage for sandbox runtime behavior, provider seams, environment route guards, orchestration, and the fake provider plugin. ## Verification - Ran locally before the final fixture-only scrub: - `pnpm -r typecheck` - `pnpm test:run` - `pnpm build` - Ran locally after the final scrub amend: - `pnpm vitest run server/src/__tests__/runtime-api.test.ts` - Reviewer spot checks: - create a sandbox environment backed by the fake provider plugin - run work through that environment - confirm sandbox provider execution does not inherit host secrets implicitly ## Risks - This touches shared contracts, plugin SDK plumbing, server runtime orchestration, and UI environment/workspace flows, so regressions would likely show up as cross-layer mismatches rather than isolated type errors. - Runtime URL discovery and sandbox callback selection are sensitive to host/bind configuration; if that logic is wrong, sandbox-backed callbacks may fail even when execution succeeds. - The fake provider plugin is intentionally deterministic and test-oriented; future providers may expose capability gaps that this branch does not yet cover. ## Model Used - OpenAI Codex coding agent on a GPT-5-class backend in the Paperclip/Codex harness. Exact backend model ID is not exposed in-session. Tool-assisted workflow with shell execution, file editing, git history inspection, and local test execution. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [ ] If this change affects the UI, I have included before/after screenshots - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] I will address all Greptile and reviewer comments before requesting merge
2026-04-24 12:15:53 -07:00
import type {
Environment,
EnvironmentLease,
ExecutionWorkspaceConfig,
WorkspaceRealizationRecord,
WorkspaceRealizationRequest,
} from "@paperclipai/shared";
import type { RealizedExecutionWorkspace } from "./workspace-runtime.js";
function parseObject(value: unknown): Record<string, unknown> {
return value && typeof value === "object" && !Array.isArray(value)
? (value as Record<string, unknown>)
: {};
}
function readString(value: unknown): string | null {
return typeof value === "string" && value.trim().length > 0 ? value.trim() : null;
}
function readNumber(value: unknown): number | null {
return typeof value === "number" && Number.isFinite(value) ? value : null;
}
function readWorkspaceRealizationRequest(value: unknown): WorkspaceRealizationRequest | null {
const parsed = parseObject(value);
if (parsed.version !== 1) return null;
const source = parseObject(parsed.source);
const runtimeOverlay = parseObject(parsed.runtimeOverlay);
const localPath = readString(source.localPath);
const companyId = readString(parsed.companyId);
const environmentId = readString(parsed.environmentId);
const heartbeatRunId = readString(parsed.heartbeatRunId);
const adapterType = readString(parsed.adapterType);
if (!localPath || !companyId || !environmentId || !heartbeatRunId || !adapterType) return null;
return {
version: 1,
adapterType,
companyId,
environmentId,
executionWorkspaceId: readString(parsed.executionWorkspaceId),
issueId: readString(parsed.issueId),
heartbeatRunId,
requestedMode: readString(parsed.requestedMode),
source: {
kind:
source.kind === "task_session" || source.kind === "agent_home"
? source.kind
: "project_primary",
localPath,
projectId: readString(source.projectId),
projectWorkspaceId: readString(source.projectWorkspaceId),
repoUrl: readString(source.repoUrl),
repoRef: readString(source.repoRef),
strategy: source.strategy === "git_worktree" ? "git_worktree" : "project_primary",
branchName: readString(source.branchName),
worktreePath: readString(source.worktreePath),
},
runtimeOverlay: {
provisionCommand: readString(runtimeOverlay.provisionCommand),
teardownCommand: readString(runtimeOverlay.teardownCommand),
cleanupCommand: readString(runtimeOverlay.cleanupCommand),
workspaceRuntime: Object.keys(parseObject(runtimeOverlay.workspaceRuntime)).length > 0
? parseObject(runtimeOverlay.workspaceRuntime)
: null,
},
};
}
export function buildWorkspaceRealizationRequest(input: {
adapterType: string;
companyId: string;
environmentId: string;
executionWorkspaceId: string | null;
issueId: string | null;
heartbeatRunId: string;
requestedMode: string | null;
workspace: RealizedExecutionWorkspace;
workspaceConfig: ExecutionWorkspaceConfig | null;
}): WorkspaceRealizationRequest {
return {
version: 1,
adapterType: input.adapterType,
companyId: input.companyId,
environmentId: input.environmentId,
executionWorkspaceId: input.executionWorkspaceId,
issueId: input.issueId,
heartbeatRunId: input.heartbeatRunId,
requestedMode: input.requestedMode,
source: {
kind: input.workspace.source,
localPath: input.workspace.cwd,
projectId: input.workspace.projectId,
projectWorkspaceId: input.workspace.workspaceId,
repoUrl: input.workspace.repoUrl,
repoRef: input.workspace.repoRef,
strategy: input.workspace.strategy,
branchName: input.workspace.branchName,
worktreePath: input.workspace.worktreePath,
},
runtimeOverlay: {
provisionCommand: input.workspaceConfig?.provisionCommand ?? null,
teardownCommand: input.workspaceConfig?.teardownCommand ?? null,
cleanupCommand: input.workspaceConfig?.cleanupCommand ?? null,
workspaceRuntime: input.workspaceConfig?.workspaceRuntime ?? null,
},
};
}
export function buildWorkspaceRealizationRecord(input: {
environment: Environment;
lease: EnvironmentLease;
request: WorkspaceRealizationRequest;
realizedCwd?: string | null;
providerMetadata?: Record<string, unknown> | null;
}): WorkspaceRealizationRecord {
const leaseMetadata = input.lease.metadata ?? {};
const providerMetadata = input.providerMetadata ?? {};
const transport =
input.environment.driver === "ssh" || input.environment.driver === "sandbox" || input.environment.driver === "plugin"
? input.environment.driver
: "local";
const remotePath =
readString(providerMetadata.remoteCwd) ??
readString(leaseMetadata.remoteCwd) ??
readString(providerMetadata.remotePath) ??
null;
const host = readString(leaseMetadata.host);
const port = readNumber(leaseMetadata.port);
const username = readString(leaseMetadata.username);
const sandboxId = readString(leaseMetadata.sandboxId) ?? readString(providerMetadata.sandboxId);
const sync = (() => {
if (transport === "local") {
return {
strategy: "none" as const,
prepare: "Use the realized local execution workspace directly.",
syncBack: null,
};
}
if (transport === "ssh") {
return {
strategy: "ssh_git_import_export" as const,
prepare: "Import the local git workspace to the remote SSH workspace before adapter execution.",
syncBack: "Export remote SSH workspace changes back to the local execution workspace after adapter execution.",
};
}
if (transport === "sandbox") {
return {
strategy: "sandbox_archive_upload_download" as const,
prepare: "Upload a workspace archive into the sandbox filesystem before adapter execution.",
syncBack: "Download a workspace archive from the sandbox and mirror it back locally after adapter execution.",
};
}
return {
strategy: "provider_defined" as const,
prepare: "Delegate workspace materialization to the plugin environment driver.",
syncBack: "Delegate result synchronization to the plugin environment driver.",
};
})();
const provider =
input.lease.provider ??
(transport === "ssh" ? "ssh" : transport === "local" ? "local" : null);
const localPath = input.request.source.localPath;
const summary =
transport === "local"
? `Local workspace realized at ${localPath}.`
: transport === "ssh"
? `SSH workspace realized at ${username ?? "user"}@${host ?? "host"}:${port ?? 22}:${remotePath ?? input.request.source.localPath}.`
: transport === "sandbox"
? `Sandbox workspace realized at ${remotePath ?? "/"}${sandboxId ? ` in ${sandboxId}` : ""}.`
: `Plugin workspace realized at ${input.realizedCwd ?? remotePath ?? localPath}.`;
return {
version: 1,
transport,
provider,
environmentId: input.environment.id,
leaseId: input.lease.id,
providerLeaseId: input.lease.providerLeaseId,
local: {
path: localPath,
source: input.request.source.kind,
strategy: input.request.source.strategy,
projectId: input.request.source.projectId,
projectWorkspaceId: input.request.source.projectWorkspaceId,
repoUrl: input.request.source.repoUrl,
repoRef: input.request.source.repoRef,
branchName: input.request.source.branchName,
worktreePath: input.request.source.worktreePath,
},
remote: {
path: remotePath,
...(host ? { host } : {}),
...(port ? { port } : {}),
...(username ? { username } : {}),
...(sandboxId ? { sandboxId } : {}),
},
sync,
bootstrap: {
command: input.request.runtimeOverlay.provisionCommand,
},
rebuild: {
executionWorkspaceId: input.request.executionWorkspaceId,
mode: input.request.requestedMode,
repoUrl: input.request.source.repoUrl,
repoRef: input.request.source.repoRef,
localPath,
remotePath,
providerLeaseId: input.lease.providerLeaseId,
metadata: {
source: input.request.source,
runtimeOverlay: input.request.runtimeOverlay,
environmentDriver: input.environment.driver,
provider,
providerMetadata,
},
},
summary,
};
}
export function buildWorkspaceRealizationRecordFromDriverInput(input: {
environment: Environment;
lease: EnvironmentLease;
workspace: {
localPath?: string;
remotePath?: string;
mode?: string;
metadata?: Record<string, unknown>;
};
cwd?: string | null;
providerMetadata?: Record<string, unknown> | null;
}): WorkspaceRealizationRecord {
const request =
readWorkspaceRealizationRequest(input.workspace.metadata?.workspaceRealizationRequest) ??
readWorkspaceRealizationRequest(input.workspace.metadata?.request) ??
buildWorkspaceRealizationRequest({
adapterType: "unknown",
companyId: input.lease.companyId,
environmentId: input.environment.id,
executionWorkspaceId: input.lease.executionWorkspaceId,
issueId: input.lease.issueId,
heartbeatRunId: input.lease.heartbeatRunId ?? "unknown",
requestedMode: input.workspace.mode ?? null,
workspace: {
baseCwd: input.workspace.localPath ?? input.cwd ?? input.workspace.remotePath ?? "/",
source: "task_session",
projectId: null,
workspaceId: null,
repoUrl: null,
repoRef: null,
strategy: "project_primary",
cwd: input.workspace.localPath ?? input.cwd ?? input.workspace.remotePath ?? "/",
branchName: null,
worktreePath: null,
warnings: [],
created: false,
},
workspaceConfig: null,
});
return buildWorkspaceRealizationRecord({
environment: input.environment,
lease: input.lease,
request,
realizedCwd: input.cwd ?? null,
providerMetadata: input.providerMetadata,
});
}