forked from farhoodlabs/paperclip
e4995bbb1c
## 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
184 lines
5.9 KiB
TypeScript
184 lines
5.9 KiB
TypeScript
import { readFile } from "node:fs/promises";
|
|
import path from "node:path";
|
|
import { afterAll, describe, expect, it } from "vitest";
|
|
import {
|
|
buildSshEnvLabFixtureConfig,
|
|
ensureSshWorkspaceReady,
|
|
readSshEnvLabFixtureStatus,
|
|
runSshCommand,
|
|
startSshEnvLabFixture,
|
|
stopSshEnvLabFixture,
|
|
type SshConnectionConfig,
|
|
} from "@paperclipai/adapter-utils/ssh";
|
|
|
|
async function readOptionalSecret(
|
|
value: string | undefined,
|
|
filePath: string | undefined,
|
|
): Promise<string | null> {
|
|
if (value && value.trim().length > 0) {
|
|
return value;
|
|
}
|
|
if (filePath && filePath.trim().length > 0) {
|
|
return await readFile(filePath, "utf8");
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Resolve the env-lab state path for this instance. Falls back to a temp
|
|
* directory scoped to the test run so parallel runs don't collide.
|
|
*/
|
|
function resolveEnvLabStatePath(): string {
|
|
const instanceRoot =
|
|
process.env.PAPERCLIP_INSTANCE_ROOT?.trim() ||
|
|
path.join(process.env.HOME ?? "/tmp", ".paperclip-worktrees", "instances", "live-ssh-test");
|
|
return path.join(instanceRoot, "env-lab", "ssh-fixture", "state.json");
|
|
}
|
|
|
|
/** Attempt to build config from explicit PAPERCLIP_ENV_LIVE_SSH_* env vars. */
|
|
function tryExplicitConfig(): {
|
|
host: string;
|
|
port: number;
|
|
username: string;
|
|
remoteWorkspacePath: string;
|
|
} | null {
|
|
const host = process.env.PAPERCLIP_ENV_LIVE_SSH_HOST?.trim() ?? "";
|
|
const username = process.env.PAPERCLIP_ENV_LIVE_SSH_USERNAME?.trim() ?? "";
|
|
const remoteWorkspacePath =
|
|
process.env.PAPERCLIP_ENV_LIVE_SSH_REMOTE_WORKSPACE_PATH?.trim() ?? "";
|
|
const port = Number.parseInt(process.env.PAPERCLIP_ENV_LIVE_SSH_PORT ?? "22", 10);
|
|
|
|
if (!host || !username || !remoteWorkspacePath || !Number.isInteger(port) || port < 1 || port > 65535) {
|
|
return null;
|
|
}
|
|
return { host, port, username, remoteWorkspacePath };
|
|
}
|
|
|
|
/** Try to use an already-running env-lab fixture. */
|
|
async function tryEnvLabFixture(): Promise<SshConnectionConfig | null> {
|
|
const statePath = resolveEnvLabStatePath();
|
|
const status = await readSshEnvLabFixtureStatus(statePath);
|
|
if (status.running && status.state) {
|
|
return buildSshEnvLabFixtureConfig(status.state);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Start a fresh env-lab SSH fixture for this test run. Returns the config
|
|
* and a cleanup function to stop it afterwards.
|
|
*/
|
|
async function startEnvLabForTest(): Promise<{
|
|
config: SshConnectionConfig;
|
|
cleanup: () => Promise<void>;
|
|
} | null> {
|
|
const statePath = resolveEnvLabStatePath();
|
|
try {
|
|
const state = await startSshEnvLabFixture({ statePath });
|
|
const config = await buildSshEnvLabFixtureConfig(state);
|
|
return {
|
|
config,
|
|
cleanup: async () => {
|
|
await stopSshEnvLabFixture(statePath);
|
|
},
|
|
};
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
let envLabCleanup: (() => Promise<void>) | null = null;
|
|
|
|
/**
|
|
* Resolve an SSH connection config from (in order):
|
|
* 1. Explicit PAPERCLIP_ENV_LIVE_SSH_* env vars
|
|
* 2. An already-running env-lab fixture
|
|
* 3. Auto-starting an env-lab fixture
|
|
*/
|
|
async function resolveSshConfig(): Promise<SshConnectionConfig | null> {
|
|
// 1. Explicit env vars
|
|
const explicit = tryExplicitConfig();
|
|
if (explicit) {
|
|
return {
|
|
...explicit,
|
|
privateKey: await readOptionalSecret(
|
|
process.env.PAPERCLIP_ENV_LIVE_SSH_PRIVATE_KEY,
|
|
process.env.PAPERCLIP_ENV_LIVE_SSH_PRIVATE_KEY_PATH,
|
|
),
|
|
knownHosts: await readOptionalSecret(
|
|
process.env.PAPERCLIP_ENV_LIVE_SSH_KNOWN_HOSTS,
|
|
process.env.PAPERCLIP_ENV_LIVE_SSH_KNOWN_HOSTS_PATH,
|
|
),
|
|
strictHostKeyChecking:
|
|
(process.env.PAPERCLIP_ENV_LIVE_SSH_STRICT_HOST_KEY_CHECKING ?? "true").toLowerCase() !== "false",
|
|
};
|
|
}
|
|
|
|
// 2. Already-running env-lab
|
|
const running = await tryEnvLabFixture();
|
|
if (running) return running;
|
|
|
|
// 3. Auto-start env-lab
|
|
if (process.env.PAPERCLIP_ENV_LIVE_SSH_NO_AUTO_FIXTURE !== "true") {
|
|
const started = await startEnvLabForTest();
|
|
if (started) {
|
|
envLabCleanup = started.cleanup;
|
|
return started.config;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
let resolvedConfig: SshConnectionConfig | null | undefined;
|
|
|
|
const describeLiveSsh = (() => {
|
|
// Eagerly check explicit env vars for sync skip decision.
|
|
// If explicit vars are set, use them. Otherwise, we'll attempt env-lab in beforeAll.
|
|
if (tryExplicitConfig()) return describe;
|
|
// If NO_AUTO_FIXTURE is set and no explicit config, skip immediately
|
|
if (process.env.PAPERCLIP_ENV_LIVE_SSH_NO_AUTO_FIXTURE === "true") {
|
|
console.warn(
|
|
"Skipping live SSH smoke test. Set PAPERCLIP_ENV_LIVE_SSH_HOST, PAPERCLIP_ENV_LIVE_SSH_USERNAME, and PAPERCLIP_ENV_LIVE_SSH_REMOTE_WORKSPACE_PATH to enable it, or remove PAPERCLIP_ENV_LIVE_SSH_NO_AUTO_FIXTURE to auto-start env-lab.",
|
|
);
|
|
return describe.skip;
|
|
}
|
|
// Will attempt env-lab — don't skip yet
|
|
return describe;
|
|
})();
|
|
|
|
describeLiveSsh("live SSH environment smoke", () => {
|
|
afterAll(async () => {
|
|
if (envLabCleanup) {
|
|
await envLabCleanup();
|
|
envLabCleanup = null;
|
|
}
|
|
});
|
|
|
|
it("connects to the configured SSH environment and verifies basic runtime tools", async () => {
|
|
if (resolvedConfig === undefined) {
|
|
resolvedConfig = await resolveSshConfig();
|
|
}
|
|
|
|
if (!resolvedConfig) {
|
|
throw new Error(
|
|
"Live SSH smoke test could not resolve SSH config from env vars or env-lab fixture. Set PAPERCLIP_ENV_LIVE_SSH_NO_AUTO_FIXTURE=true to mark this suite skipped intentionally.",
|
|
);
|
|
}
|
|
|
|
const config = resolvedConfig;
|
|
const ready = await ensureSshWorkspaceReady(config);
|
|
const quotedRemoteWorkspacePath = JSON.stringify(config.remoteWorkspacePath);
|
|
const result = await runSshCommand(
|
|
config,
|
|
`sh -lc "cd ${quotedRemoteWorkspacePath} && which git && which tar && pwd"`,
|
|
{ timeoutMs: 30000, maxBuffer: 256 * 1024 },
|
|
);
|
|
|
|
expect(ready.remoteCwd).toBe(config.remoteWorkspacePath);
|
|
expect(result.stdout).toContain(config.remoteWorkspacePath);
|
|
expect(result.stdout).toContain("git");
|
|
expect(result.stdout).toContain("tar");
|
|
});
|
|
});
|