Add SSH environment support (#4358)
## Thinking Path
> - Paperclip orchestrates AI agents for zero-human companies
> - The environments subsystem already models execution environments,
but before this branch there was no end-to-end SSH-backed runtime path
for agents to actually run work against a remote box
> - That meant agents could be configured around environment concepts
without a reliable way to execute adapter sessions remotely, sync
workspace state, and preserve run context across supported adapters
> - We also need environment selection to participate in normal
Paperclip control-plane behavior: agent defaults, project/issue
selection, route validation, and environment probing
> - Because this capability is still experimental, the UI surface should
be easy to hide and easy to remove later without undoing the underlying
implementation
> - This pull request adds SSH environment execution support across the
runtime, adapters, routes, schema, and tests, then puts the visible
environment-management UI behind an experimental flag
> - The benefit is that we can validate real SSH-backed agent execution
now while keeping the user-facing controls safely gated until the
feature is ready to come out of experimentation
## What Changed
- Added SSH-backed execution target support in the shared adapter
runtime, including remote workspace preparation, skill/runtime asset
sync, remote session handling, and workspace restore behavior after
runs.
- Added SSH execution coverage for supported local adapters, plus remote
execution tests across Claude, Codex, Cursor, Gemini, OpenCode, and Pi.
- Added environment selection and environment-management backend support
needed for SSH execution, including route/service work, validation,
probing, and agent default environment persistence.
- Added CLI support for SSH environment lab verification and updated
related docs/tests.
- Added the `enableEnvironments` experimental flag and gated the
environment UI behind it on company settings, agent configuration, and
project configuration surfaces.
## Verification
- `pnpm exec vitest run
packages/adapters/claude-local/src/server/execute.remote.test.ts
packages/adapters/cursor-local/src/server/execute.remote.test.ts
packages/adapters/gemini-local/src/server/execute.remote.test.ts
packages/adapters/opencode-local/src/server/execute.remote.test.ts
packages/adapters/pi-local/src/server/execute.remote.test.ts`
- `pnpm exec vitest run server/src/__tests__/environment-routes.test.ts`
- `pnpm exec vitest run
server/src/__tests__/instance-settings-routes.test.ts`
- `pnpm exec vitest run ui/src/lib/new-agent-hire-payload.test.ts
ui/src/lib/new-agent-runtime-config.test.ts`
- `pnpm -r typecheck`
- `pnpm build`
- Manual verification on a branch-local dev server:
- enabled the experimental flag
- created an SSH environment
- created a Linux Claude agent using that environment
- confirmed a run executed on the Linux box and synced workspace changes
back
## Risks
- Medium: this touches runtime execution flow across multiple adapters,
so regressions would likely show up in remote session setup, workspace
sync, or environment selection precedence.
- The UI flag reduces exposure, but the underlying runtime and route
changes are still substantial and rely on migration correctness.
- The change set is broad across adapters, control-plane services,
migrations, and UI gating, so review should pay close attention to
environment-selection precedence and remote workspace lifecycle
behavior.
## Model Used
- OpenAI Codex via Paperclip's local Codex adapter, GPT-5-class coding
model with tool use and code execution in the local repo workspace. The
local adapter does not surface a more specific public model version
string in this branch workflow.
## 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-23 19:15:22 -07:00
|
|
|
import { execFile } from "node:child_process";
|
Harden remote workspace sync and restore flows (#5444)
## Thinking Path
> - Paperclip orchestrates AI agents for zero-human companies
> - When an agent runs against a remote target, Paperclip syncs the
workspace out to the remote at run start and restores changes back to
the local workspace at run end
> - The previous restore flow naïvely overwrote local files with
whatever the remote returned, so files that the remote run never touched
but had timestamp/mode drift could be needlessly rewritten — and a
single static `refs/paperclip/ssh-sync/imported` ref made concurrent SSH
workspace exports race on the same git ref
> - This pull request adds a `workspace-restore-merge` module that diffs
a pre-run snapshot against the post-run remote state and only writes
back files the remote actually changed; SSH workspace exports now use a
per-import unique ref so concurrent runs can't trample each other
> - Every adapter's execute path threads the snapshot through
`prepareAdapterExecutionTargetRuntime` so the merge has the baseline it
needs
> - The benefit is workspace restores no longer churn untouched files,
and concurrent SSH runs no longer collide on the import ref
## What Changed
- `packages/adapter-utils/src/workspace-restore-merge.{ts,test.ts}`: new
module — directory snapshot (kind/mode/sha256/symlink target) plus
snapshot-aware merge that writes only the files the remote changed
- `packages/adapter-utils/src/ssh.ts`: SSH workspace export uses a
per-import unique ref (`refs/paperclip/ssh-sync/imported/<uuid>`);
restore goes through the new merge helper; `ssh-fixture.test.ts` covers
the unique-ref + merge paths
- `packages/adapter-utils/src/sandbox-managed-runtime.ts` +
`remote-managed-runtime.ts`: thread the snapshot/merge through the
sandbox and SSH paths
- `packages/adapter-utils/src/server-utils.{ts,test.ts}` +
`execution-target.ts`: helpers for capturing the pre-run snapshot;
`prepareAdapterExecutionTargetRuntime` gains required `runId` and
optional `workspaceRemoteDir`, and returns the realized
`workspaceRemoteDir`
- Each adapter's `execute.ts` (acpx, claude, codex, cursor, gemini,
opencode, pi) takes the snapshot at run start and passes it through to
the runtime restore
- Remote execute test mocks updated to match the new
`prepareWorkspaceForSshExecution` return shape and the per-run
`${managedRemoteWorkspace}` cwd subdirectory
## Verification
- `pnpm vitest run --no-coverage --project @paperclipai/adapter-utils
--project @paperclipai/adapter-acpx-local --project
@paperclipai/adapter-claude-local --project
@paperclipai/adapter-codex-local --project
@paperclipai/adapter-cursor-local --project
@paperclipai/adapter-gemini-local --project
@paperclipai/adapter-opencode-local --project
@paperclipai/adapter-pi-local` — 196/196 passing
- `pnpm typecheck` clean across the workspace
## Risks
Medium. The restore path now writes a strict subset of what it
previously did — files the remote did not touch are no longer rewritten.
If any flow was relying on a touch-without-content-change being copied
back (timestamp or permission propagation only), that behavior is now
skipped. Snapshot capture adds an O(N-files-in-workspace) hash pass at
run start; the cost is bounded by the existing exclude list. The `runId`
parameter on `prepareAdapterExecutionTargetRuntime` is now required —
every in-tree caller is updated; out-of-tree adapter authors need to
pass it.
## Model Used
Claude Opus 4.7 (1M context)
## 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 — new module +
every adapter execute path covered
- [x] If this change affects the UI, I have included before/after
screenshots — N/A (no UI)
- [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-05-07 14:44:45 -07:00
|
|
|
import { mkdir, mkdtemp, readFile, rm, symlink, writeFile } from "node:fs/promises";
|
Add SSH environment support (#4358)
## Thinking Path
> - Paperclip orchestrates AI agents for zero-human companies
> - The environments subsystem already models execution environments,
but before this branch there was no end-to-end SSH-backed runtime path
for agents to actually run work against a remote box
> - That meant agents could be configured around environment concepts
without a reliable way to execute adapter sessions remotely, sync
workspace state, and preserve run context across supported adapters
> - We also need environment selection to participate in normal
Paperclip control-plane behavior: agent defaults, project/issue
selection, route validation, and environment probing
> - Because this capability is still experimental, the UI surface should
be easy to hide and easy to remove later without undoing the underlying
implementation
> - This pull request adds SSH environment execution support across the
runtime, adapters, routes, schema, and tests, then puts the visible
environment-management UI behind an experimental flag
> - The benefit is that we can validate real SSH-backed agent execution
now while keeping the user-facing controls safely gated until the
feature is ready to come out of experimentation
## What Changed
- Added SSH-backed execution target support in the shared adapter
runtime, including remote workspace preparation, skill/runtime asset
sync, remote session handling, and workspace restore behavior after
runs.
- Added SSH execution coverage for supported local adapters, plus remote
execution tests across Claude, Codex, Cursor, Gemini, OpenCode, and Pi.
- Added environment selection and environment-management backend support
needed for SSH execution, including route/service work, validation,
probing, and agent default environment persistence.
- Added CLI support for SSH environment lab verification and updated
related docs/tests.
- Added the `enableEnvironments` experimental flag and gated the
environment UI behind it on company settings, agent configuration, and
project configuration surfaces.
## Verification
- `pnpm exec vitest run
packages/adapters/claude-local/src/server/execute.remote.test.ts
packages/adapters/cursor-local/src/server/execute.remote.test.ts
packages/adapters/gemini-local/src/server/execute.remote.test.ts
packages/adapters/opencode-local/src/server/execute.remote.test.ts
packages/adapters/pi-local/src/server/execute.remote.test.ts`
- `pnpm exec vitest run server/src/__tests__/environment-routes.test.ts`
- `pnpm exec vitest run
server/src/__tests__/instance-settings-routes.test.ts`
- `pnpm exec vitest run ui/src/lib/new-agent-hire-payload.test.ts
ui/src/lib/new-agent-runtime-config.test.ts`
- `pnpm -r typecheck`
- `pnpm build`
- Manual verification on a branch-local dev server:
- enabled the experimental flag
- created an SSH environment
- created a Linux Claude agent using that environment
- confirmed a run executed on the Linux box and synced workspace changes
back
## Risks
- Medium: this touches runtime execution flow across multiple adapters,
so regressions would likely show up in remote session setup, workspace
sync, or environment selection precedence.
- The UI flag reduces exposure, but the underlying runtime and route
changes are still substantial and rely on migration correctness.
- The change set is broad across adapters, control-plane services,
migrations, and UI gating, so review should pay close attention to
environment-selection precedence and remote workspace lifecycle
behavior.
## Model Used
- OpenAI Codex via Paperclip's local Codex adapter, GPT-5-class coding
model with tool use and code execution in the local repo workspace. The
local adapter does not surface a more specific public model version
string in this branch workflow.
## 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-23 19:15:22 -07:00
|
|
|
import os from "node:os";
|
|
|
|
|
import path from "node:path";
|
|
|
|
|
import { afterEach, describe, expect, it } from "vitest";
|
|
|
|
|
import {
|
|
|
|
|
buildSshSpawnTarget,
|
|
|
|
|
buildSshEnvLabFixtureConfig,
|
|
|
|
|
getSshEnvLabSupport,
|
|
|
|
|
prepareWorkspaceForSshExecution,
|
|
|
|
|
readSshEnvLabFixtureStatus,
|
|
|
|
|
restoreWorkspaceFromSshExecution,
|
|
|
|
|
runSshCommand,
|
|
|
|
|
syncDirectoryToSsh,
|
|
|
|
|
startSshEnvLabFixture,
|
|
|
|
|
stopSshEnvLabFixture,
|
|
|
|
|
} from "./ssh.js";
|
Harden remote workspace sync and restore flows (#5444)
## Thinking Path
> - Paperclip orchestrates AI agents for zero-human companies
> - When an agent runs against a remote target, Paperclip syncs the
workspace out to the remote at run start and restores changes back to
the local workspace at run end
> - The previous restore flow naïvely overwrote local files with
whatever the remote returned, so files that the remote run never touched
but had timestamp/mode drift could be needlessly rewritten — and a
single static `refs/paperclip/ssh-sync/imported` ref made concurrent SSH
workspace exports race on the same git ref
> - This pull request adds a `workspace-restore-merge` module that diffs
a pre-run snapshot against the post-run remote state and only writes
back files the remote actually changed; SSH workspace exports now use a
per-import unique ref so concurrent runs can't trample each other
> - Every adapter's execute path threads the snapshot through
`prepareAdapterExecutionTargetRuntime` so the merge has the baseline it
needs
> - The benefit is workspace restores no longer churn untouched files,
and concurrent SSH runs no longer collide on the import ref
## What Changed
- `packages/adapter-utils/src/workspace-restore-merge.{ts,test.ts}`: new
module — directory snapshot (kind/mode/sha256/symlink target) plus
snapshot-aware merge that writes only the files the remote changed
- `packages/adapter-utils/src/ssh.ts`: SSH workspace export uses a
per-import unique ref (`refs/paperclip/ssh-sync/imported/<uuid>`);
restore goes through the new merge helper; `ssh-fixture.test.ts` covers
the unique-ref + merge paths
- `packages/adapter-utils/src/sandbox-managed-runtime.ts` +
`remote-managed-runtime.ts`: thread the snapshot/merge through the
sandbox and SSH paths
- `packages/adapter-utils/src/server-utils.{ts,test.ts}` +
`execution-target.ts`: helpers for capturing the pre-run snapshot;
`prepareAdapterExecutionTargetRuntime` gains required `runId` and
optional `workspaceRemoteDir`, and returns the realized
`workspaceRemoteDir`
- Each adapter's `execute.ts` (acpx, claude, codex, cursor, gemini,
opencode, pi) takes the snapshot at run start and passes it through to
the runtime restore
- Remote execute test mocks updated to match the new
`prepareWorkspaceForSshExecution` return shape and the per-run
`${managedRemoteWorkspace}` cwd subdirectory
## Verification
- `pnpm vitest run --no-coverage --project @paperclipai/adapter-utils
--project @paperclipai/adapter-acpx-local --project
@paperclipai/adapter-claude-local --project
@paperclipai/adapter-codex-local --project
@paperclipai/adapter-cursor-local --project
@paperclipai/adapter-gemini-local --project
@paperclipai/adapter-opencode-local --project
@paperclipai/adapter-pi-local` — 196/196 passing
- `pnpm typecheck` clean across the workspace
## Risks
Medium. The restore path now writes a strict subset of what it
previously did — files the remote did not touch are no longer rewritten.
If any flow was relying on a touch-without-content-change being copied
back (timestamp or permission propagation only), that behavior is now
skipped. Snapshot capture adds an O(N-files-in-workspace) hash pass at
run start; the cost is bounded by the existing exclude list. The `runId`
parameter on `prepareAdapterExecutionTargetRuntime` is now required —
every in-tree caller is updated; out-of-tree adapter authors need to
pass it.
## Model Used
Claude Opus 4.7 (1M context)
## 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 — new module +
every adapter execute path covered
- [x] If this change affects the UI, I have included before/after
screenshots — N/A (no UI)
- [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-05-07 14:44:45 -07:00
|
|
|
import { prepareRemoteManagedRuntime } from "./remote-managed-runtime.js";
|
Add SSH environment support (#4358)
## Thinking Path
> - Paperclip orchestrates AI agents for zero-human companies
> - The environments subsystem already models execution environments,
but before this branch there was no end-to-end SSH-backed runtime path
for agents to actually run work against a remote box
> - That meant agents could be configured around environment concepts
without a reliable way to execute adapter sessions remotely, sync
workspace state, and preserve run context across supported adapters
> - We also need environment selection to participate in normal
Paperclip control-plane behavior: agent defaults, project/issue
selection, route validation, and environment probing
> - Because this capability is still experimental, the UI surface should
be easy to hide and easy to remove later without undoing the underlying
implementation
> - This pull request adds SSH environment execution support across the
runtime, adapters, routes, schema, and tests, then puts the visible
environment-management UI behind an experimental flag
> - The benefit is that we can validate real SSH-backed agent execution
now while keeping the user-facing controls safely gated until the
feature is ready to come out of experimentation
## What Changed
- Added SSH-backed execution target support in the shared adapter
runtime, including remote workspace preparation, skill/runtime asset
sync, remote session handling, and workspace restore behavior after
runs.
- Added SSH execution coverage for supported local adapters, plus remote
execution tests across Claude, Codex, Cursor, Gemini, OpenCode, and Pi.
- Added environment selection and environment-management backend support
needed for SSH execution, including route/service work, validation,
probing, and agent default environment persistence.
- Added CLI support for SSH environment lab verification and updated
related docs/tests.
- Added the `enableEnvironments` experimental flag and gated the
environment UI behind it on company settings, agent configuration, and
project configuration surfaces.
## Verification
- `pnpm exec vitest run
packages/adapters/claude-local/src/server/execute.remote.test.ts
packages/adapters/cursor-local/src/server/execute.remote.test.ts
packages/adapters/gemini-local/src/server/execute.remote.test.ts
packages/adapters/opencode-local/src/server/execute.remote.test.ts
packages/adapters/pi-local/src/server/execute.remote.test.ts`
- `pnpm exec vitest run server/src/__tests__/environment-routes.test.ts`
- `pnpm exec vitest run
server/src/__tests__/instance-settings-routes.test.ts`
- `pnpm exec vitest run ui/src/lib/new-agent-hire-payload.test.ts
ui/src/lib/new-agent-runtime-config.test.ts`
- `pnpm -r typecheck`
- `pnpm build`
- Manual verification on a branch-local dev server:
- enabled the experimental flag
- created an SSH environment
- created a Linux Claude agent using that environment
- confirmed a run executed on the Linux box and synced workspace changes
back
## Risks
- Medium: this touches runtime execution flow across multiple adapters,
so regressions would likely show up in remote session setup, workspace
sync, or environment selection precedence.
- The UI flag reduces exposure, but the underlying runtime and route
changes are still substantial and rely on migration correctness.
- The change set is broad across adapters, control-plane services,
migrations, and UI gating, so review should pay close attention to
environment-selection precedence and remote workspace lifecycle
behavior.
## Model Used
- OpenAI Codex via Paperclip's local Codex adapter, GPT-5-class coding
model with tool use and code execution in the local repo workspace. The
local adapter does not surface a more specific public model version
string in this branch workflow.
## 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-23 19:15:22 -07:00
|
|
|
|
|
|
|
|
async function git(cwd: string, args: string[]): Promise<string> {
|
|
|
|
|
return await new Promise((resolve, reject) => {
|
|
|
|
|
execFile("git", ["-C", cwd, ...args], (error, stdout, stderr) => {
|
|
|
|
|
if (error) {
|
|
|
|
|
reject(new Error((stderr || stdout || error.message).trim()));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
resolve(stdout.trim());
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe("ssh env-lab fixture", () => {
|
|
|
|
|
const cleanupDirs: string[] = [];
|
|
|
|
|
|
|
|
|
|
afterEach(async () => {
|
|
|
|
|
while (cleanupDirs.length > 0) {
|
|
|
|
|
const dir = cleanupDirs.pop();
|
|
|
|
|
if (!dir) continue;
|
|
|
|
|
await rm(dir, { recursive: true, force: true }).catch(() => undefined);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("starts an isolated sshd fixture and executes commands through it", async () => {
|
|
|
|
|
const support = await getSshEnvLabSupport();
|
|
|
|
|
if (!support.supported) {
|
|
|
|
|
console.warn(
|
|
|
|
|
`Skipping SSH env-lab fixture test: ${support.reason ?? "unsupported environment"}`,
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const rootDir = await mkdtemp(path.join(os.tmpdir(), "paperclip-ssh-fixture-"));
|
|
|
|
|
cleanupDirs.push(rootDir);
|
|
|
|
|
const statePath = path.join(rootDir, "state.json");
|
|
|
|
|
|
|
|
|
|
const started = await startSshEnvLabFixture({ statePath });
|
|
|
|
|
const config = await buildSshEnvLabFixtureConfig(started);
|
|
|
|
|
const quotedWorkspace = JSON.stringify(started.workspaceDir);
|
|
|
|
|
const result = await runSshCommand(
|
|
|
|
|
config,
|
|
|
|
|
`sh -lc 'cd ${quotedWorkspace} && pwd'`,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(result.stdout.trim()).toBe(started.workspaceDir);
|
|
|
|
|
const status = await readSshEnvLabFixtureStatus(statePath);
|
|
|
|
|
expect(status.running).toBe(true);
|
|
|
|
|
|
|
|
|
|
await stopSshEnvLabFixture(statePath);
|
|
|
|
|
|
|
|
|
|
const stopped = await readSshEnvLabFixtureStatus(statePath);
|
|
|
|
|
expect(stopped.running).toBe(false);
|
|
|
|
|
});
|
|
|
|
|
|
Serialize sandbox callback bridge against concurrent heartbeats (#5326)
> **Stacked PR.** This PR's branch carries cumulative content from #5324
(bridge allowlist expand) and #5325 (env sanitization) — the
mutex/sha256 logic in this PR sits on top of both. Reviewers should
focus on the files this PR's commit touches:
`packages/adapter-utils/src/sandbox-callback-bridge.{ts,test.ts}`,
`packages/adapter-utils/src/ssh.ts`, and
`packages/adapter-utils/src/ssh-fixture.test.ts`. Will rebase onto
`master` and force-push once both prerequisite PRs are merged.
## Thinking Path
> - Paperclip orchestrates AI agents for zero-human companies
> - Each agent that runs in a sandbox or via SSH talks back to the
Paperclip server through a per-lease callback bridge whose entrypoint
script is uploaded to the remote
> - When two heartbeats target the same agent on the same machine
concurrently, both upload the bridge entrypoint and both write to the
same response files — producing torn-write races: `SyntaxError:
Identifier 'randomUUID' has already been declared` from a concatenated
upload, `mv: cannot stat …` from colliding `.json.tmp` writes, and
0-byte commits from a truncated stdin
> - This pull request serializes those operations with a POSIX
`mkdir`-mutex (PID liveness check + atomic rename) at the bridge
entrypoint upload, applies the same lock to the bridge response writer,
forwards stdin into remote ssh commands so the entrypoint payload
arrives intact, and verifies a sha256 of the upload before promoting it
> - The benefit is concurrent heartbeats no longer corrupt each other's
bridge state
## What Changed
- `packages/adapter-utils/src/sandbox-callback-bridge.ts`: serialize
entrypoint upload and response writes via POSIX `mkdir`-mutex with PID
liveness; sha256 the upload before promoting via `mv`; content-skip when
the existing entrypoint already matches
- `packages/adapter-utils/src/ssh.ts`: forward stdin into remote ssh
commands through the SSH managed runtime so `cat > "$remote_upload"`
actually receives the base64-encoded entrypoint
- `packages/adapter-utils/src/ssh-fixture.test.ts`: cover the
stdin-forwarded SSH path
- `packages/adapter-utils/src/sandbox-callback-bridge.test.ts`: cover
the mutex, content-skip, sha256-verify, and atomic-rename paths
## Verification
- `pnpm vitest run --no-coverage --project @paperclipai/adapter-utils`
- `pnpm typecheck` clean
- Manual: two parallel heartbeats targeting the same SSH agent no longer
race on the bridge entrypoint or response files
## Risks
Medium. Serializing previously-parallel operations adds latency on the
contended path (one heartbeat waits on another), bounded by the
entrypoint upload time. The mutex includes PID liveness so a crashed
heartbeat doesn't deadlock subsequent ones. Sha256-verify gives a clear
"torn upload" failure mode instead of silent 0-byte commits.
## Model Used
Claude Opus 4.7 (1M context)
## 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 — tests cover mutex
+ sha256-verify + stdin-forwarded ssh
- [x] If this change affects the UI, I have included before/after
screenshots — N/A (no UI)
- [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-05-05 20:01:04 -07:00
|
|
|
it("forwards stdin to remote SSH commands", async () => {
|
|
|
|
|
const support = await getSshEnvLabSupport();
|
|
|
|
|
if (!support.supported) {
|
|
|
|
|
console.warn(
|
|
|
|
|
`Skipping SSH stdin forwarding test: ${support.reason ?? "unsupported environment"}`,
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const rootDir = await mkdtemp(path.join(os.tmpdir(), "paperclip-ssh-fixture-"));
|
|
|
|
|
cleanupDirs.push(rootDir);
|
|
|
|
|
const statePath = path.join(rootDir, "state.json");
|
|
|
|
|
|
|
|
|
|
const started = await startSshEnvLabFixture({ statePath });
|
|
|
|
|
const config = await buildSshEnvLabFixtureConfig(started);
|
|
|
|
|
const remotePath = path.posix.join(started.workspaceDir, "stdin-forwarded.txt");
|
|
|
|
|
|
|
|
|
|
await runSshCommand(
|
|
|
|
|
config,
|
|
|
|
|
`sh -lc 'cat > ${JSON.stringify(remotePath)}'`,
|
|
|
|
|
{
|
|
|
|
|
stdin: "hello over ssh stdin\n",
|
|
|
|
|
timeoutMs: 30_000,
|
|
|
|
|
maxBuffer: 256 * 1024,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const result = await runSshCommand(
|
|
|
|
|
config,
|
|
|
|
|
`sh -lc 'cat ${JSON.stringify(remotePath)}'`,
|
|
|
|
|
{ timeoutMs: 30_000, maxBuffer: 256 * 1024 },
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(result.stdout).toBe("hello over ssh stdin\n");
|
|
|
|
|
});
|
|
|
|
|
|
Add SSH environment support (#4358)
## Thinking Path
> - Paperclip orchestrates AI agents for zero-human companies
> - The environments subsystem already models execution environments,
but before this branch there was no end-to-end SSH-backed runtime path
for agents to actually run work against a remote box
> - That meant agents could be configured around environment concepts
without a reliable way to execute adapter sessions remotely, sync
workspace state, and preserve run context across supported adapters
> - We also need environment selection to participate in normal
Paperclip control-plane behavior: agent defaults, project/issue
selection, route validation, and environment probing
> - Because this capability is still experimental, the UI surface should
be easy to hide and easy to remove later without undoing the underlying
implementation
> - This pull request adds SSH environment execution support across the
runtime, adapters, routes, schema, and tests, then puts the visible
environment-management UI behind an experimental flag
> - The benefit is that we can validate real SSH-backed agent execution
now while keeping the user-facing controls safely gated until the
feature is ready to come out of experimentation
## What Changed
- Added SSH-backed execution target support in the shared adapter
runtime, including remote workspace preparation, skill/runtime asset
sync, remote session handling, and workspace restore behavior after
runs.
- Added SSH execution coverage for supported local adapters, plus remote
execution tests across Claude, Codex, Cursor, Gemini, OpenCode, and Pi.
- Added environment selection and environment-management backend support
needed for SSH execution, including route/service work, validation,
probing, and agent default environment persistence.
- Added CLI support for SSH environment lab verification and updated
related docs/tests.
- Added the `enableEnvironments` experimental flag and gated the
environment UI behind it on company settings, agent configuration, and
project configuration surfaces.
## Verification
- `pnpm exec vitest run
packages/adapters/claude-local/src/server/execute.remote.test.ts
packages/adapters/cursor-local/src/server/execute.remote.test.ts
packages/adapters/gemini-local/src/server/execute.remote.test.ts
packages/adapters/opencode-local/src/server/execute.remote.test.ts
packages/adapters/pi-local/src/server/execute.remote.test.ts`
- `pnpm exec vitest run server/src/__tests__/environment-routes.test.ts`
- `pnpm exec vitest run
server/src/__tests__/instance-settings-routes.test.ts`
- `pnpm exec vitest run ui/src/lib/new-agent-hire-payload.test.ts
ui/src/lib/new-agent-runtime-config.test.ts`
- `pnpm -r typecheck`
- `pnpm build`
- Manual verification on a branch-local dev server:
- enabled the experimental flag
- created an SSH environment
- created a Linux Claude agent using that environment
- confirmed a run executed on the Linux box and synced workspace changes
back
## Risks
- Medium: this touches runtime execution flow across multiple adapters,
so regressions would likely show up in remote session setup, workspace
sync, or environment selection precedence.
- The UI flag reduces exposure, but the underlying runtime and route
changes are still substantial and rely on migration correctness.
- The change set is broad across adapters, control-plane services,
migrations, and UI gating, so review should pay close attention to
environment-selection precedence and remote workspace lifecycle
behavior.
## Model Used
- OpenAI Codex via Paperclip's local Codex adapter, GPT-5-class coding
model with tool use and code execution in the local repo workspace. The
local adapter does not surface a more specific public model version
string in this branch workflow.
## 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-23 19:15:22 -07:00
|
|
|
it("does not treat an unrelated reused pid as the running fixture", async () => {
|
|
|
|
|
const support = await getSshEnvLabSupport();
|
|
|
|
|
if (!support.supported) {
|
|
|
|
|
console.warn(
|
|
|
|
|
`Skipping SSH env-lab fixture test: ${support.reason ?? "unsupported environment"}`,
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const rootDir = await mkdtemp(path.join(os.tmpdir(), "paperclip-ssh-fixture-"));
|
|
|
|
|
cleanupDirs.push(rootDir);
|
|
|
|
|
const statePath = path.join(rootDir, "state.json");
|
|
|
|
|
|
|
|
|
|
const started = await startSshEnvLabFixture({ statePath });
|
|
|
|
|
await stopSshEnvLabFixture(statePath);
|
|
|
|
|
await mkdir(path.dirname(statePath), { recursive: true });
|
|
|
|
|
|
|
|
|
|
await writeFile(
|
|
|
|
|
statePath,
|
|
|
|
|
JSON.stringify({ ...started, pid: process.pid }, null, 2),
|
|
|
|
|
{ mode: 0o600 },
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const staleStatus = await readSshEnvLabFixtureStatus(statePath);
|
|
|
|
|
expect(staleStatus.running).toBe(false);
|
|
|
|
|
|
|
|
|
|
const restarted = await startSshEnvLabFixture({ statePath });
|
|
|
|
|
expect(restarted.pid).not.toBe(process.pid);
|
|
|
|
|
|
|
|
|
|
await stopSshEnvLabFixture(statePath);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("rejects invalid environment variable keys when constructing SSH spawn targets", async () => {
|
|
|
|
|
await expect(
|
|
|
|
|
buildSshSpawnTarget({
|
|
|
|
|
spec: {
|
|
|
|
|
host: "ssh.example.test",
|
|
|
|
|
port: 22,
|
|
|
|
|
username: "ssh-user",
|
|
|
|
|
remoteCwd: "/srv/paperclip/workspace",
|
|
|
|
|
remoteWorkspacePath: "/srv/paperclip/workspace",
|
|
|
|
|
privateKey: null,
|
|
|
|
|
knownHosts: null,
|
|
|
|
|
strictHostKeyChecking: true,
|
|
|
|
|
},
|
|
|
|
|
command: "env",
|
|
|
|
|
args: [],
|
|
|
|
|
env: {
|
|
|
|
|
"BAD KEY": "value",
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
).rejects.toThrow("Invalid SSH environment variable key: BAD KEY");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("syncs a local directory into the remote fixture workspace", async () => {
|
|
|
|
|
const support = await getSshEnvLabSupport();
|
|
|
|
|
if (!support.supported) {
|
|
|
|
|
console.warn(
|
|
|
|
|
`Skipping SSH env-lab fixture test: ${support.reason ?? "unsupported environment"}`,
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const rootDir = await mkdtemp(path.join(os.tmpdir(), "paperclip-ssh-fixture-"));
|
|
|
|
|
cleanupDirs.push(rootDir);
|
|
|
|
|
const statePath = path.join(rootDir, "state.json");
|
|
|
|
|
const localDir = path.join(rootDir, "local-overlay");
|
|
|
|
|
|
|
|
|
|
await mkdir(localDir, { recursive: true });
|
|
|
|
|
await writeFile(path.join(localDir, "message.txt"), "hello from paperclip\n", "utf8");
|
|
|
|
|
await writeFile(path.join(localDir, "._message.txt"), "should never sync\n", "utf8");
|
|
|
|
|
|
|
|
|
|
const started = await startSshEnvLabFixture({ statePath });
|
|
|
|
|
const config = await buildSshEnvLabFixtureConfig(started);
|
|
|
|
|
const remoteDir = path.posix.join(started.workspaceDir, "overlay");
|
|
|
|
|
|
|
|
|
|
await syncDirectoryToSsh({
|
|
|
|
|
spec: {
|
|
|
|
|
...config,
|
|
|
|
|
remoteCwd: started.workspaceDir,
|
|
|
|
|
},
|
|
|
|
|
localDir,
|
|
|
|
|
remoteDir,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const result = await runSshCommand(
|
|
|
|
|
config,
|
|
|
|
|
`sh -lc 'cat ${JSON.stringify(path.posix.join(remoteDir, "message.txt"))} && if [ -e ${JSON.stringify(path.posix.join(remoteDir, "._message.txt"))} ]; then echo appledouble-present; fi'`,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(result.stdout).toContain("hello from paperclip");
|
|
|
|
|
expect(result.stdout).not.toContain("appledouble-present");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("can dereference local symlinks while syncing to the remote fixture", async () => {
|
|
|
|
|
const support = await getSshEnvLabSupport();
|
|
|
|
|
if (!support.supported) {
|
|
|
|
|
console.warn(
|
|
|
|
|
`Skipping SSH symlink sync test: ${support.reason ?? "unsupported environment"}`,
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const rootDir = await mkdtemp(path.join(os.tmpdir(), "paperclip-ssh-fixture-"));
|
|
|
|
|
cleanupDirs.push(rootDir);
|
|
|
|
|
const statePath = path.join(rootDir, "state.json");
|
|
|
|
|
const sourceDir = path.join(rootDir, "source");
|
|
|
|
|
const localDir = path.join(rootDir, "local-overlay");
|
|
|
|
|
|
|
|
|
|
await mkdir(sourceDir, { recursive: true });
|
|
|
|
|
await mkdir(localDir, { recursive: true });
|
|
|
|
|
await writeFile(path.join(sourceDir, "auth.json"), "{\"token\":\"secret\"}\n", "utf8");
|
|
|
|
|
await symlink(path.join(sourceDir, "auth.json"), path.join(localDir, "auth.json"));
|
|
|
|
|
|
|
|
|
|
const started = await startSshEnvLabFixture({ statePath });
|
|
|
|
|
const config = await buildSshEnvLabFixtureConfig(started);
|
|
|
|
|
const remoteDir = path.posix.join(started.workspaceDir, "overlay-follow-links");
|
|
|
|
|
|
|
|
|
|
await syncDirectoryToSsh({
|
|
|
|
|
spec: {
|
|
|
|
|
...config,
|
|
|
|
|
remoteCwd: started.workspaceDir,
|
|
|
|
|
},
|
|
|
|
|
localDir,
|
|
|
|
|
remoteDir,
|
|
|
|
|
followSymlinks: true,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const result = await runSshCommand(
|
|
|
|
|
config,
|
|
|
|
|
`sh -lc 'if [ -L ${JSON.stringify(path.posix.join(remoteDir, "auth.json"))} ]; then echo symlink; else echo regular; fi && cat ${JSON.stringify(path.posix.join(remoteDir, "auth.json"))}'`,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(result.stdout).toContain("regular");
|
|
|
|
|
expect(result.stdout).toContain("{\"token\":\"secret\"}");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("round-trips a git workspace through the SSH fixture", async () => {
|
|
|
|
|
const support = await getSshEnvLabSupport();
|
|
|
|
|
if (!support.supported) {
|
|
|
|
|
console.warn(
|
|
|
|
|
`Skipping SSH workspace round-trip test: ${support.reason ?? "unsupported environment"}`,
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const rootDir = await mkdtemp(path.join(os.tmpdir(), "paperclip-ssh-fixture-"));
|
|
|
|
|
cleanupDirs.push(rootDir);
|
|
|
|
|
const statePath = path.join(rootDir, "state.json");
|
|
|
|
|
const localRepo = path.join(rootDir, "local-workspace");
|
|
|
|
|
|
|
|
|
|
await mkdir(localRepo, { recursive: true });
|
|
|
|
|
await git(localRepo, ["init", "-b", "main"]);
|
|
|
|
|
await git(localRepo, ["config", "user.name", "Paperclip Test"]);
|
|
|
|
|
await git(localRepo, ["config", "user.email", "test@paperclip.dev"]);
|
|
|
|
|
await writeFile(path.join(localRepo, "tracked.txt"), "base\n", "utf8");
|
|
|
|
|
await writeFile(path.join(localRepo, "._tracked.txt"), "should stay local only\n", "utf8");
|
|
|
|
|
await git(localRepo, ["add", "tracked.txt"]);
|
|
|
|
|
await git(localRepo, ["commit", "-m", "initial"]);
|
|
|
|
|
const originalHead = await git(localRepo, ["rev-parse", "HEAD"]);
|
|
|
|
|
await writeFile(path.join(localRepo, "tracked.txt"), "dirty local\n", "utf8");
|
|
|
|
|
await writeFile(path.join(localRepo, "untracked.txt"), "from local\n", "utf8");
|
|
|
|
|
|
|
|
|
|
const started = await startSshEnvLabFixture({ statePath });
|
|
|
|
|
const config = await buildSshEnvLabFixtureConfig(started);
|
|
|
|
|
const spec = {
|
|
|
|
|
...config,
|
|
|
|
|
remoteCwd: started.workspaceDir,
|
|
|
|
|
} as const;
|
|
|
|
|
|
|
|
|
|
await prepareWorkspaceForSshExecution({
|
|
|
|
|
spec,
|
|
|
|
|
localDir: localRepo,
|
|
|
|
|
remoteDir: started.workspaceDir,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const remoteStatus = await runSshCommand(
|
|
|
|
|
config,
|
|
|
|
|
`sh -lc 'cd ${JSON.stringify(started.workspaceDir)} && git status --short'`,
|
|
|
|
|
);
|
|
|
|
|
expect(remoteStatus.stdout).toContain("M tracked.txt");
|
|
|
|
|
expect(remoteStatus.stdout).toContain("?? untracked.txt");
|
|
|
|
|
expect(remoteStatus.stdout).not.toContain("._tracked.txt");
|
|
|
|
|
|
|
|
|
|
await runSshCommand(
|
|
|
|
|
config,
|
|
|
|
|
`sh -lc 'cd ${JSON.stringify(started.workspaceDir)} && git config user.name "Paperclip SSH" && git config user.email "ssh@paperclip.dev" && git add tracked.txt untracked.txt && git commit -m "remote update" >/dev/null && printf "remote dirty\\n" > tracked.txt && printf "remote extra\\n" > remote-only.txt'`,
|
|
|
|
|
{ timeoutMs: 30_000, maxBuffer: 256 * 1024 },
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await restoreWorkspaceFromSshExecution({
|
|
|
|
|
spec,
|
|
|
|
|
localDir: localRepo,
|
|
|
|
|
remoteDir: started.workspaceDir,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const restoredHead = await git(localRepo, ["rev-parse", "HEAD"]);
|
|
|
|
|
expect(restoredHead).not.toBe(originalHead);
|
|
|
|
|
expect(await git(localRepo, ["log", "-1", "--pretty=%s"])).toBe("remote update");
|
|
|
|
|
expect(await git(localRepo, ["status", "--short"])).toContain("M tracked.txt");
|
|
|
|
|
expect(await git(localRepo, ["status", "--short"])).not.toContain("._tracked.txt");
|
|
|
|
|
});
|
Harden remote workspace sync and restore flows (#5444)
## Thinking Path
> - Paperclip orchestrates AI agents for zero-human companies
> - When an agent runs against a remote target, Paperclip syncs the
workspace out to the remote at run start and restores changes back to
the local workspace at run end
> - The previous restore flow naïvely overwrote local files with
whatever the remote returned, so files that the remote run never touched
but had timestamp/mode drift could be needlessly rewritten — and a
single static `refs/paperclip/ssh-sync/imported` ref made concurrent SSH
workspace exports race on the same git ref
> - This pull request adds a `workspace-restore-merge` module that diffs
a pre-run snapshot against the post-run remote state and only writes
back files the remote actually changed; SSH workspace exports now use a
per-import unique ref so concurrent runs can't trample each other
> - Every adapter's execute path threads the snapshot through
`prepareAdapterExecutionTargetRuntime` so the merge has the baseline it
needs
> - The benefit is workspace restores no longer churn untouched files,
and concurrent SSH runs no longer collide on the import ref
## What Changed
- `packages/adapter-utils/src/workspace-restore-merge.{ts,test.ts}`: new
module — directory snapshot (kind/mode/sha256/symlink target) plus
snapshot-aware merge that writes only the files the remote changed
- `packages/adapter-utils/src/ssh.ts`: SSH workspace export uses a
per-import unique ref (`refs/paperclip/ssh-sync/imported/<uuid>`);
restore goes through the new merge helper; `ssh-fixture.test.ts` covers
the unique-ref + merge paths
- `packages/adapter-utils/src/sandbox-managed-runtime.ts` +
`remote-managed-runtime.ts`: thread the snapshot/merge through the
sandbox and SSH paths
- `packages/adapter-utils/src/server-utils.{ts,test.ts}` +
`execution-target.ts`: helpers for capturing the pre-run snapshot;
`prepareAdapterExecutionTargetRuntime` gains required `runId` and
optional `workspaceRemoteDir`, and returns the realized
`workspaceRemoteDir`
- Each adapter's `execute.ts` (acpx, claude, codex, cursor, gemini,
opencode, pi) takes the snapshot at run start and passes it through to
the runtime restore
- Remote execute test mocks updated to match the new
`prepareWorkspaceForSshExecution` return shape and the per-run
`${managedRemoteWorkspace}` cwd subdirectory
## Verification
- `pnpm vitest run --no-coverage --project @paperclipai/adapter-utils
--project @paperclipai/adapter-acpx-local --project
@paperclipai/adapter-claude-local --project
@paperclipai/adapter-codex-local --project
@paperclipai/adapter-cursor-local --project
@paperclipai/adapter-gemini-local --project
@paperclipai/adapter-opencode-local --project
@paperclipai/adapter-pi-local` — 196/196 passing
- `pnpm typecheck` clean across the workspace
## Risks
Medium. The restore path now writes a strict subset of what it
previously did — files the remote did not touch are no longer rewritten.
If any flow was relying on a touch-without-content-change being copied
back (timestamp or permission propagation only), that behavior is now
skipped. Snapshot capture adds an O(N-files-in-workspace) hash pass at
run start; the cost is bounded by the existing exclude list. The `runId`
parameter on `prepareAdapterExecutionTargetRuntime` is now required —
every in-tree caller is updated; out-of-tree adapter authors need to
pass it.
## Model Used
Claude Opus 4.7 (1M context)
## 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 — new module +
every adapter execute path covered
- [x] If this change affects the UI, I have included before/after
screenshots — N/A (no UI)
- [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-05-07 14:44:45 -07:00
|
|
|
|
|
|
|
|
it("preserves both concurrent SSH restores in a shared git workspace", async () => {
|
|
|
|
|
const support = await getSshEnvLabSupport();
|
|
|
|
|
if (!support.supported) {
|
|
|
|
|
console.warn(
|
|
|
|
|
`Skipping concurrent SSH restore test: ${support.reason ?? "unsupported environment"}`,
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const rootDir = await mkdtemp(path.join(os.tmpdir(), "paperclip-ssh-fixture-"));
|
|
|
|
|
cleanupDirs.push(rootDir);
|
|
|
|
|
const statePath = path.join(rootDir, "state.json");
|
|
|
|
|
const localRepo = path.join(rootDir, "local-workspace");
|
|
|
|
|
|
|
|
|
|
await mkdir(localRepo, { recursive: true });
|
|
|
|
|
await git(localRepo, ["init", "-b", "main"]);
|
|
|
|
|
await git(localRepo, ["config", "user.name", "Paperclip Test"]);
|
|
|
|
|
await git(localRepo, ["config", "user.email", "test@paperclip.dev"]);
|
|
|
|
|
await writeFile(path.join(localRepo, "tracked.txt"), "base\n", "utf8");
|
|
|
|
|
await git(localRepo, ["add", "tracked.txt"]);
|
|
|
|
|
await git(localRepo, ["commit", "-m", "initial"]);
|
|
|
|
|
|
|
|
|
|
const started = await startSshEnvLabFixture({ statePath });
|
|
|
|
|
const config = await buildSshEnvLabFixtureConfig(started);
|
|
|
|
|
const spec = {
|
|
|
|
|
...config,
|
|
|
|
|
remoteCwd: started.workspaceDir,
|
|
|
|
|
} as const;
|
|
|
|
|
|
|
|
|
|
const preparedA = await prepareRemoteManagedRuntime({
|
|
|
|
|
spec,
|
|
|
|
|
runId: "run-a",
|
|
|
|
|
adapterKey: "test-adapter",
|
|
|
|
|
workspaceLocalDir: localRepo,
|
|
|
|
|
});
|
|
|
|
|
const preparedB = await prepareRemoteManagedRuntime({
|
|
|
|
|
spec,
|
|
|
|
|
runId: "run-b",
|
|
|
|
|
adapterKey: "test-adapter",
|
|
|
|
|
workspaceLocalDir: localRepo,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(preparedA.workspaceRemoteDir).not.toBe(preparedB.workspaceRemoteDir);
|
|
|
|
|
|
|
|
|
|
await runSshCommand(
|
|
|
|
|
config,
|
|
|
|
|
`sh -lc 'printf "from run a\\n" > ${JSON.stringify(path.posix.join(preparedA.workspaceRemoteDir, "run-a.txt"))}'`,
|
|
|
|
|
{ timeoutMs: 30_000, maxBuffer: 256 * 1024 },
|
|
|
|
|
);
|
|
|
|
|
await runSshCommand(
|
|
|
|
|
config,
|
|
|
|
|
`sh -lc 'printf "from run b\\n" > ${JSON.stringify(path.posix.join(preparedB.workspaceRemoteDir, "run-b.txt"))}'`,
|
|
|
|
|
{ timeoutMs: 30_000, maxBuffer: 256 * 1024 },
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
|
preparedA.restoreWorkspace(),
|
|
|
|
|
preparedB.restoreWorkspace(),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
await expect(readFile(path.join(localRepo, "run-a.txt"), "utf8")).resolves.toBe("from run a\n");
|
|
|
|
|
await expect(readFile(path.join(localRepo, "run-b.txt"), "utf8")).resolves.toBe("from run b\n");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("preserves nested per-run files across sequential SSH restores with stale baselines", async () => {
|
|
|
|
|
const support = await getSshEnvLabSupport();
|
|
|
|
|
if (!support.supported) {
|
|
|
|
|
console.warn(
|
|
|
|
|
`Skipping sequential nested SSH restore test: ${support.reason ?? "unsupported environment"}`,
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const rootDir = await mkdtemp(path.join(os.tmpdir(), "paperclip-ssh-fixture-"));
|
|
|
|
|
cleanupDirs.push(rootDir);
|
|
|
|
|
const statePath = path.join(rootDir, "state.json");
|
|
|
|
|
const localRepo = path.join(rootDir, "local-workspace");
|
|
|
|
|
|
|
|
|
|
await mkdir(localRepo, { recursive: true });
|
|
|
|
|
await git(localRepo, ["init", "-b", "main"]);
|
|
|
|
|
await git(localRepo, ["config", "user.name", "Paperclip Test"]);
|
|
|
|
|
await git(localRepo, ["config", "user.email", "test@paperclip.dev"]);
|
|
|
|
|
await writeFile(path.join(localRepo, "tracked.txt"), "base\n", "utf8");
|
|
|
|
|
await git(localRepo, ["add", "tracked.txt"]);
|
|
|
|
|
await git(localRepo, ["commit", "-m", "initial"]);
|
|
|
|
|
|
|
|
|
|
const started = await startSshEnvLabFixture({ statePath });
|
|
|
|
|
const config = await buildSshEnvLabFixtureConfig(started);
|
|
|
|
|
const spec = {
|
|
|
|
|
...config,
|
|
|
|
|
remoteCwd: started.workspaceDir,
|
|
|
|
|
} as const;
|
|
|
|
|
|
|
|
|
|
const preparedA = await prepareRemoteManagedRuntime({
|
|
|
|
|
spec,
|
|
|
|
|
runId: "run-a",
|
|
|
|
|
adapterKey: "test-adapter",
|
|
|
|
|
workspaceLocalDir: localRepo,
|
|
|
|
|
});
|
|
|
|
|
const preparedB = await prepareRemoteManagedRuntime({
|
|
|
|
|
spec,
|
|
|
|
|
runId: "run-b",
|
|
|
|
|
adapterKey: "test-adapter",
|
|
|
|
|
workspaceLocalDir: localRepo,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await runSshCommand(
|
|
|
|
|
config,
|
|
|
|
|
`sh -lc 'mkdir -p ${JSON.stringify(path.posix.join(preparedA.workspaceRemoteDir, "manual-qa/environment-matrix/ssh"))} && printf "from run a\\n" > ${JSON.stringify(path.posix.join(preparedA.workspaceRemoteDir, "manual-qa/environment-matrix/ssh/claude_local.md"))}'`,
|
|
|
|
|
{ timeoutMs: 30_000, maxBuffer: 256 * 1024 },
|
|
|
|
|
);
|
|
|
|
|
await runSshCommand(
|
|
|
|
|
config,
|
|
|
|
|
`sh -lc 'mkdir -p ${JSON.stringify(path.posix.join(preparedB.workspaceRemoteDir, "manual-qa/environment-matrix/ssh"))} && printf "from run b\\n" > ${JSON.stringify(path.posix.join(preparedB.workspaceRemoteDir, "manual-qa/environment-matrix/ssh/codex_local.md"))}'`,
|
|
|
|
|
{ timeoutMs: 30_000, maxBuffer: 256 * 1024 },
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await preparedA.restoreWorkspace();
|
|
|
|
|
await preparedB.restoreWorkspace();
|
|
|
|
|
|
|
|
|
|
await expect(readFile(path.join(localRepo, "manual-qa/environment-matrix/ssh/claude_local.md"), "utf8")).resolves
|
|
|
|
|
.toBe("from run a\n");
|
|
|
|
|
await expect(readFile(path.join(localRepo, "manual-qa/environment-matrix/ssh/codex_local.md"), "utf8")).resolves
|
|
|
|
|
.toBe("from run b\n");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("round-trips remote git commits through the managed runtime restore path", async () => {
|
|
|
|
|
const support = await getSshEnvLabSupport();
|
|
|
|
|
if (!support.supported) {
|
|
|
|
|
console.warn(
|
|
|
|
|
`Skipping managed-runtime SSH git round-trip test: ${support.reason ?? "unsupported environment"}`,
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const rootDir = await mkdtemp(path.join(os.tmpdir(), "paperclip-ssh-fixture-"));
|
|
|
|
|
cleanupDirs.push(rootDir);
|
|
|
|
|
const statePath = path.join(rootDir, "state.json");
|
|
|
|
|
const localRepo = path.join(rootDir, "local-workspace");
|
|
|
|
|
|
|
|
|
|
await mkdir(localRepo, { recursive: true });
|
|
|
|
|
await git(localRepo, ["init", "-b", "main"]);
|
|
|
|
|
await git(localRepo, ["config", "user.name", "Paperclip Test"]);
|
|
|
|
|
await git(localRepo, ["config", "user.email", "test@paperclip.dev"]);
|
|
|
|
|
await writeFile(path.join(localRepo, "tracked.txt"), "base\n", "utf8");
|
|
|
|
|
await git(localRepo, ["add", "tracked.txt"]);
|
|
|
|
|
await git(localRepo, ["commit", "-m", "initial"]);
|
|
|
|
|
|
|
|
|
|
const started = await startSshEnvLabFixture({ statePath });
|
|
|
|
|
const config = await buildSshEnvLabFixtureConfig(started);
|
|
|
|
|
const spec = {
|
|
|
|
|
...config,
|
|
|
|
|
remoteCwd: started.workspaceDir,
|
|
|
|
|
} as const;
|
|
|
|
|
|
|
|
|
|
const prepared = await prepareRemoteManagedRuntime({
|
|
|
|
|
spec,
|
|
|
|
|
runId: "run-commit",
|
|
|
|
|
adapterKey: "test-adapter",
|
|
|
|
|
workspaceLocalDir: localRepo,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await runSshCommand(
|
|
|
|
|
config,
|
|
|
|
|
`sh -lc 'cd ${JSON.stringify(prepared.workspaceRemoteDir)} && git config user.name "Paperclip SSH" && git config user.email "ssh@paperclip.dev" && printf "committed\\n" > tracked.txt && git add tracked.txt && git commit -m "remote update" >/dev/null && printf "dirty remote\\n" > tracked.txt'`,
|
|
|
|
|
{ timeoutMs: 30_000, maxBuffer: 256 * 1024 },
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await prepared.restoreWorkspace();
|
|
|
|
|
|
|
|
|
|
expect(await git(localRepo, ["log", "-1", "--pretty=%s"])).toBe("remote update");
|
|
|
|
|
await expect(readFile(path.join(localRepo, "tracked.txt"), "utf8")).resolves.toBe("dirty remote\n");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("merges concurrent remote commits through the managed runtime restore path", async () => {
|
|
|
|
|
const support = await getSshEnvLabSupport();
|
|
|
|
|
if (!support.supported) {
|
|
|
|
|
console.warn(
|
|
|
|
|
`Skipping concurrent managed-runtime SSH git merge test: ${support.reason ?? "unsupported environment"}`,
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const rootDir = await mkdtemp(path.join(os.tmpdir(), "paperclip-ssh-fixture-"));
|
|
|
|
|
cleanupDirs.push(rootDir);
|
|
|
|
|
const statePath = path.join(rootDir, "state.json");
|
|
|
|
|
const localRepo = path.join(rootDir, "local-workspace");
|
|
|
|
|
|
|
|
|
|
await mkdir(localRepo, { recursive: true });
|
|
|
|
|
await git(localRepo, ["init", "-b", "main"]);
|
|
|
|
|
await git(localRepo, ["config", "user.name", "Paperclip Test"]);
|
|
|
|
|
await git(localRepo, ["config", "user.email", "test@paperclip.dev"]);
|
|
|
|
|
await writeFile(path.join(localRepo, "tracked.txt"), "base\n", "utf8");
|
|
|
|
|
await git(localRepo, ["add", "tracked.txt"]);
|
|
|
|
|
await git(localRepo, ["commit", "-m", "initial"]);
|
|
|
|
|
|
|
|
|
|
const started = await startSshEnvLabFixture({ statePath });
|
|
|
|
|
const config = await buildSshEnvLabFixtureConfig(started);
|
|
|
|
|
const spec = {
|
|
|
|
|
...config,
|
|
|
|
|
remoteCwd: started.workspaceDir,
|
|
|
|
|
} as const;
|
|
|
|
|
|
|
|
|
|
const preparedA = await prepareRemoteManagedRuntime({
|
|
|
|
|
spec,
|
|
|
|
|
runId: "run-commit-a",
|
|
|
|
|
adapterKey: "test-adapter",
|
|
|
|
|
workspaceLocalDir: localRepo,
|
|
|
|
|
});
|
|
|
|
|
const preparedB = await prepareRemoteManagedRuntime({
|
|
|
|
|
spec,
|
|
|
|
|
runId: "run-commit-b",
|
|
|
|
|
adapterKey: "test-adapter",
|
|
|
|
|
workspaceLocalDir: localRepo,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await runSshCommand(
|
|
|
|
|
config,
|
|
|
|
|
`sh -lc 'cd ${JSON.stringify(preparedA.workspaceRemoteDir)} && git config user.name "Paperclip SSH" && git config user.email "ssh@paperclip.dev" && printf "from run a\\n" > run-a.txt && git add run-a.txt && git commit -m "remote update a" >/dev/null'`,
|
|
|
|
|
{ timeoutMs: 30_000, maxBuffer: 256 * 1024 },
|
|
|
|
|
);
|
|
|
|
|
await runSshCommand(
|
|
|
|
|
config,
|
|
|
|
|
`sh -lc 'cd ${JSON.stringify(preparedB.workspaceRemoteDir)} && git config user.name "Paperclip SSH" && git config user.email "ssh@paperclip.dev" && printf "from run b\\n" > run-b.txt && git add run-b.txt && git commit -m "remote update b" >/dev/null'`,
|
|
|
|
|
{ timeoutMs: 30_000, maxBuffer: 256 * 1024 },
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
|
preparedA.restoreWorkspace(),
|
|
|
|
|
preparedB.restoreWorkspace(),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
await expect(readFile(path.join(localRepo, "run-a.txt"), "utf8")).resolves.toBe("from run a\n");
|
|
|
|
|
await expect(readFile(path.join(localRepo, "run-b.txt"), "utf8")).resolves.toBe("from run b\n");
|
|
|
|
|
expect(await git(localRepo, ["log", "-1", "--pretty=%s"])).toContain("Paperclip SSH sync merge");
|
|
|
|
|
|
|
|
|
|
const recentSubjects = await git(localRepo, ["log", "--pretty=%s", "-3"]);
|
|
|
|
|
expect(recentSubjects).toContain("remote update a");
|
|
|
|
|
expect(recentSubjects).toContain("remote update b");
|
|
|
|
|
});
|
Add SSH environment support (#4358)
## Thinking Path
> - Paperclip orchestrates AI agents for zero-human companies
> - The environments subsystem already models execution environments,
but before this branch there was no end-to-end SSH-backed runtime path
for agents to actually run work against a remote box
> - That meant agents could be configured around environment concepts
without a reliable way to execute adapter sessions remotely, sync
workspace state, and preserve run context across supported adapters
> - We also need environment selection to participate in normal
Paperclip control-plane behavior: agent defaults, project/issue
selection, route validation, and environment probing
> - Because this capability is still experimental, the UI surface should
be easy to hide and easy to remove later without undoing the underlying
implementation
> - This pull request adds SSH environment execution support across the
runtime, adapters, routes, schema, and tests, then puts the visible
environment-management UI behind an experimental flag
> - The benefit is that we can validate real SSH-backed agent execution
now while keeping the user-facing controls safely gated until the
feature is ready to come out of experimentation
## What Changed
- Added SSH-backed execution target support in the shared adapter
runtime, including remote workspace preparation, skill/runtime asset
sync, remote session handling, and workspace restore behavior after
runs.
- Added SSH execution coverage for supported local adapters, plus remote
execution tests across Claude, Codex, Cursor, Gemini, OpenCode, and Pi.
- Added environment selection and environment-management backend support
needed for SSH execution, including route/service work, validation,
probing, and agent default environment persistence.
- Added CLI support for SSH environment lab verification and updated
related docs/tests.
- Added the `enableEnvironments` experimental flag and gated the
environment UI behind it on company settings, agent configuration, and
project configuration surfaces.
## Verification
- `pnpm exec vitest run
packages/adapters/claude-local/src/server/execute.remote.test.ts
packages/adapters/cursor-local/src/server/execute.remote.test.ts
packages/adapters/gemini-local/src/server/execute.remote.test.ts
packages/adapters/opencode-local/src/server/execute.remote.test.ts
packages/adapters/pi-local/src/server/execute.remote.test.ts`
- `pnpm exec vitest run server/src/__tests__/environment-routes.test.ts`
- `pnpm exec vitest run
server/src/__tests__/instance-settings-routes.test.ts`
- `pnpm exec vitest run ui/src/lib/new-agent-hire-payload.test.ts
ui/src/lib/new-agent-runtime-config.test.ts`
- `pnpm -r typecheck`
- `pnpm build`
- Manual verification on a branch-local dev server:
- enabled the experimental flag
- created an SSH environment
- created a Linux Claude agent using that environment
- confirmed a run executed on the Linux box and synced workspace changes
back
## Risks
- Medium: this touches runtime execution flow across multiple adapters,
so regressions would likely show up in remote session setup, workspace
sync, or environment selection precedence.
- The UI flag reduces exposure, but the underlying runtime and route
changes are still substantial and rely on migration correctness.
- The change set is broad across adapters, control-plane services,
migrations, and UI gating, so review should pay close attention to
environment-selection precedence and remote workspace lifecycle
behavior.
## Model Used
- OpenAI Codex via Paperclip's local Codex adapter, GPT-5-class coding
model with tool use and code execution in the local repo workspace. The
local adapter does not surface a more specific public model version
string in this branch workflow.
## 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-23 19:15:22 -07:00
|
|
|
});
|