mirror of
https://github.com/alkimake/paperclip.git
synced 2026-06-14 01:50:39 +09:00
## 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
308 lines
9.5 KiB
TypeScript
308 lines
9.5 KiB
TypeScript
import type { Server } from "node:http";
|
|
import express from "express";
|
|
import request from "supertest";
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const mockAccessService = vi.hoisted(() => ({
|
|
isInstanceAdmin: vi.fn(),
|
|
hasPermission: vi.fn(),
|
|
canUser: vi.fn(),
|
|
}));
|
|
|
|
const mockAgentService = vi.hoisted(() => ({
|
|
getById: vi.fn(),
|
|
}));
|
|
|
|
const mockBoardAuthService = vi.hoisted(() => ({
|
|
createCliAuthChallenge: vi.fn(),
|
|
describeCliAuthChallenge: vi.fn(),
|
|
approveCliAuthChallenge: vi.fn(),
|
|
cancelCliAuthChallenge: vi.fn(),
|
|
resolveBoardAccess: vi.fn(),
|
|
resolveBoardActivityCompanyIds: vi.fn(),
|
|
assertCurrentBoardKey: vi.fn(),
|
|
revokeBoardApiKey: vi.fn(),
|
|
}));
|
|
|
|
const mockLogActivity = vi.hoisted(() => vi.fn());
|
|
|
|
vi.mock("../services/index.js", () => ({
|
|
accessService: () => mockAccessService,
|
|
agentService: () => mockAgentService,
|
|
boardAuthService: () => mockBoardAuthService,
|
|
logActivity: mockLogActivity,
|
|
notifyHireApproved: vi.fn(),
|
|
deduplicateAgentName: vi.fn((name: string) => name),
|
|
}));
|
|
|
|
let currentServer: Server | null = null;
|
|
|
|
async function closeCurrentServer() {
|
|
if (!currentServer) return;
|
|
const server = currentServer;
|
|
currentServer = null;
|
|
await new Promise<void>((resolve, reject) => {
|
|
server.close((err) => {
|
|
if (err) reject(err);
|
|
else resolve();
|
|
});
|
|
});
|
|
}
|
|
|
|
function registerModuleMocks() {
|
|
vi.doMock("../routes/authz.js", async () => vi.importActual("../routes/authz.js"));
|
|
|
|
vi.doMock("../services/index.js", () => ({
|
|
accessService: () => mockAccessService,
|
|
agentService: () => mockAgentService,
|
|
boardAuthService: () => mockBoardAuthService,
|
|
logActivity: mockLogActivity,
|
|
notifyHireApproved: vi.fn(),
|
|
deduplicateAgentName: vi.fn((name: string) => name),
|
|
}));
|
|
}
|
|
|
|
async function createApp(actor: any, db: any = {} as any) {
|
|
await closeCurrentServer();
|
|
const [{ accessRoutes }, { errorHandler }] = await Promise.all([
|
|
vi.importActual<typeof import("../routes/access.js")>("../routes/access.js"),
|
|
vi.importActual<typeof import("../middleware/index.js")>("../middleware/index.js"),
|
|
]);
|
|
const app = express();
|
|
app.use(express.json());
|
|
app.use((req, _res, next) => {
|
|
req.actor = actor;
|
|
next();
|
|
});
|
|
app.use(
|
|
"/api",
|
|
accessRoutes(db, {
|
|
deploymentMode: "authenticated",
|
|
deploymentExposure: "private",
|
|
bindHost: "127.0.0.1",
|
|
allowedHostnames: [],
|
|
}),
|
|
);
|
|
app.use(errorHandler);
|
|
currentServer = app.listen(0);
|
|
return currentServer;
|
|
}
|
|
|
|
describe("cli auth routes", () => {
|
|
afterEach(closeCurrentServer);
|
|
|
|
beforeEach(() => {
|
|
vi.resetModules();
|
|
vi.doUnmock("../services/index.js");
|
|
vi.doUnmock("../routes/authz.js");
|
|
vi.doUnmock("../routes/access.js");
|
|
vi.doUnmock("../middleware/index.js");
|
|
registerModuleMocks();
|
|
vi.resetAllMocks();
|
|
});
|
|
|
|
it("creates a CLI auth challenge with approval metadata", async () => {
|
|
mockBoardAuthService.createCliAuthChallenge.mockResolvedValue({
|
|
challenge: {
|
|
id: "challenge-1",
|
|
expiresAt: new Date("2026-03-23T13:00:00.000Z"),
|
|
},
|
|
challengeSecret: "pcp_cli_auth_secret",
|
|
pendingBoardToken: "pcp_board_token",
|
|
});
|
|
|
|
const app = await createApp({ type: "none", source: "none" });
|
|
const res = await request(app)
|
|
.post("/api/cli-auth/challenges")
|
|
.send({
|
|
command: "paperclipai company import",
|
|
clientName: "paperclipai cli",
|
|
requestedAccess: "board",
|
|
});
|
|
|
|
expect(res.status).toBe(201);
|
|
expect(res.body).toMatchObject({
|
|
id: "challenge-1",
|
|
token: "pcp_cli_auth_secret",
|
|
approvalPath: "/cli-auth/challenge-1?token=pcp_cli_auth_secret",
|
|
pollPath: "/cli-auth/challenges/challenge-1",
|
|
expiresAt: "2026-03-23T13:00:00.000Z",
|
|
});
|
|
expect(res.body.boardApiToken).toBe("pcp_board_token");
|
|
expect(res.body.approvalUrl).toContain("/cli-auth/challenge-1?token=pcp_cli_auth_secret");
|
|
});
|
|
|
|
it("rejects anonymous access to generic skill documents", async () => {
|
|
const app = await createApp({ type: "none", source: "none" });
|
|
const [indexRes, skillRes] = await Promise.all([
|
|
request(app).get("/api/skills/index"),
|
|
request(app).get("/api/skills/paperclip"),
|
|
]);
|
|
|
|
expect(indexRes.status).toBe(401);
|
|
expect(skillRes.status).toBe(401);
|
|
});
|
|
|
|
it("serves the invite-scoped paperclip skill anonymously for active invites", async () => {
|
|
const invite = {
|
|
id: "invite-1",
|
|
companyId: "company-1",
|
|
inviteType: "company_join",
|
|
allowedJoinTypes: "agent",
|
|
tokenHash: "hash",
|
|
defaultsPayload: null,
|
|
expiresAt: new Date(Date.now() + 60_000),
|
|
invitedByUserId: null,
|
|
revokedAt: null,
|
|
acceptedAt: null,
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
};
|
|
const db = {
|
|
select: vi.fn(() => ({
|
|
from: vi.fn(() => ({
|
|
where: vi.fn().mockResolvedValue([invite]),
|
|
})),
|
|
})),
|
|
};
|
|
|
|
const app = await createApp({ type: "none", source: "none" }, db);
|
|
const res = await request(app).get("/api/invites/token-123/skills/paperclip");
|
|
|
|
expect(res.status).toBe(200);
|
|
expect(res.headers["content-type"]).toContain("text/markdown");
|
|
expect(res.text).toContain("# Paperclip Skill");
|
|
});
|
|
|
|
it("marks challenge status as requiring sign-in for anonymous viewers", async () => {
|
|
mockBoardAuthService.describeCliAuthChallenge.mockResolvedValue({
|
|
id: "challenge-1",
|
|
status: "pending",
|
|
command: "paperclipai company import",
|
|
clientName: "paperclipai cli",
|
|
requestedAccess: "board",
|
|
requestedCompanyId: null,
|
|
requestedCompanyName: null,
|
|
approvedAt: null,
|
|
cancelledAt: null,
|
|
expiresAt: "2026-03-23T13:00:00.000Z",
|
|
approvedByUser: null,
|
|
});
|
|
|
|
const app = await createApp({ type: "none", source: "none" });
|
|
const res = await request(app).get("/api/cli-auth/challenges/challenge-1?token=pcp_cli_auth_secret");
|
|
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.requiresSignIn).toBe(true);
|
|
expect(res.body.canApprove).toBe(false);
|
|
});
|
|
|
|
it("approves a CLI auth challenge for a signed-in board user", async () => {
|
|
mockBoardAuthService.approveCliAuthChallenge.mockResolvedValue({
|
|
status: "approved",
|
|
challenge: {
|
|
id: "challenge-1",
|
|
boardApiKeyId: "board-key-1",
|
|
requestedAccess: "board",
|
|
requestedCompanyId: "company-1",
|
|
expiresAt: new Date("2026-03-23T13:00:00.000Z"),
|
|
},
|
|
});
|
|
mockBoardAuthService.resolveBoardAccess.mockResolvedValue({
|
|
user: { id: "user-1", name: "User One", email: "user@example.com" },
|
|
companyIds: ["company-1"],
|
|
isInstanceAdmin: false,
|
|
});
|
|
mockBoardAuthService.resolveBoardActivityCompanyIds.mockResolvedValue(["company-1"]);
|
|
|
|
const app = await createApp({
|
|
type: "board",
|
|
userId: "user-1",
|
|
source: "session",
|
|
isInstanceAdmin: false,
|
|
companyIds: ["company-1"],
|
|
});
|
|
const res = await request(app)
|
|
.post("/api/cli-auth/challenges/challenge-1/approve")
|
|
.send({ token: "pcp_cli_auth_secret" });
|
|
|
|
expect(res.status).toBe(200);
|
|
expect(mockBoardAuthService.approveCliAuthChallenge).toHaveBeenCalledWith(
|
|
"challenge-1",
|
|
"pcp_cli_auth_secret",
|
|
"user-1",
|
|
);
|
|
expect(mockLogActivity).toHaveBeenCalledTimes(1);
|
|
expect(mockLogActivity).toHaveBeenCalledWith(
|
|
expect.anything(),
|
|
expect.objectContaining({
|
|
companyId: "company-1",
|
|
action: "board_api_key.created",
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("logs approve activity for instance admins without company memberships", async () => {
|
|
mockBoardAuthService.approveCliAuthChallenge.mockResolvedValue({
|
|
status: "approved",
|
|
challenge: {
|
|
id: "challenge-2",
|
|
boardApiKeyId: "board-key-2",
|
|
requestedAccess: "instance_admin_required",
|
|
requestedCompanyId: null,
|
|
expiresAt: new Date("2026-03-23T13:00:00.000Z"),
|
|
},
|
|
});
|
|
mockBoardAuthService.resolveBoardActivityCompanyIds.mockResolvedValue(["company-a", "company-b"]);
|
|
|
|
const app = await createApp({
|
|
type: "board",
|
|
userId: "admin-1",
|
|
source: "session",
|
|
isInstanceAdmin: true,
|
|
companyIds: [],
|
|
});
|
|
const res = await request(app)
|
|
.post("/api/cli-auth/challenges/challenge-2/approve")
|
|
.send({ token: "pcp_cli_auth_secret" });
|
|
|
|
expect(res.status).toBe(200);
|
|
expect(mockBoardAuthService.resolveBoardActivityCompanyIds).toHaveBeenCalledWith({
|
|
userId: "admin-1",
|
|
requestedCompanyId: null,
|
|
boardApiKeyId: "board-key-2",
|
|
});
|
|
expect(mockLogActivity).toHaveBeenCalledTimes(2);
|
|
});
|
|
|
|
it("logs revoke activity with resolved audit company ids", async () => {
|
|
mockBoardAuthService.assertCurrentBoardKey.mockResolvedValue({
|
|
id: "board-key-3",
|
|
userId: "admin-2",
|
|
});
|
|
mockBoardAuthService.resolveBoardActivityCompanyIds.mockResolvedValue(["company-z"]);
|
|
|
|
const app = await createApp({
|
|
type: "board",
|
|
userId: "admin-2",
|
|
keyId: "board-key-3",
|
|
source: "board_key",
|
|
isInstanceAdmin: true,
|
|
companyIds: [],
|
|
});
|
|
const res = await request(app).post("/api/cli-auth/revoke-current").send({});
|
|
|
|
expect(res.status).toBe(200);
|
|
expect(mockBoardAuthService.resolveBoardActivityCompanyIds).toHaveBeenCalledWith({
|
|
userId: "admin-2",
|
|
boardApiKeyId: "board-key-3",
|
|
});
|
|
expect(mockLogActivity).toHaveBeenCalledWith(
|
|
expect.anything(),
|
|
expect.objectContaining({
|
|
companyId: "company-z",
|
|
action: "board_api_key.revoked",
|
|
}),
|
|
);
|
|
});
|
|
});
|