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
1486 lines
44 KiB
TypeScript
1486 lines
44 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 agentId = "11111111-1111-4111-8111-111111111111";
|
|
const companyId = "22222222-2222-4222-8222-222222222222";
|
|
|
|
const baseAgent = {
|
|
id: agentId,
|
|
companyId,
|
|
name: "Builder",
|
|
urlKey: "builder",
|
|
role: "engineer",
|
|
title: "Builder",
|
|
icon: null,
|
|
status: "idle",
|
|
reportsTo: null,
|
|
capabilities: null,
|
|
adapterType: "process",
|
|
adapterConfig: {},
|
|
runtimeConfig: {},
|
|
defaultEnvironmentId: null,
|
|
budgetMonthlyCents: 0,
|
|
spentMonthlyCents: 0,
|
|
pauseReason: null,
|
|
pausedAt: null,
|
|
permissions: { canCreateAgents: false },
|
|
lastHeartbeatAt: null,
|
|
metadata: null,
|
|
createdAt: new Date("2026-03-19T00:00:00.000Z"),
|
|
updatedAt: new Date("2026-03-19T00:00:00.000Z"),
|
|
};
|
|
|
|
const mockAgentService = vi.hoisted(() => ({
|
|
getById: vi.fn(),
|
|
list: vi.fn(),
|
|
create: vi.fn(),
|
|
activatePendingApproval: vi.fn(),
|
|
update: vi.fn(),
|
|
updatePermissions: vi.fn(),
|
|
getChainOfCommand: vi.fn(),
|
|
resolveByReference: vi.fn(),
|
|
}));
|
|
|
|
const mockAccessService = vi.hoisted(() => ({
|
|
canUser: vi.fn(),
|
|
hasPermission: vi.fn(),
|
|
getMembership: vi.fn(),
|
|
ensureMembership: vi.fn(),
|
|
listPrincipalGrants: vi.fn(),
|
|
setPrincipalPermission: vi.fn(),
|
|
}));
|
|
|
|
const mockApprovalService = vi.hoisted(() => ({
|
|
create: vi.fn(),
|
|
getById: vi.fn(),
|
|
}));
|
|
|
|
const mockBudgetService = vi.hoisted(() => ({
|
|
upsertPolicy: vi.fn(),
|
|
}));
|
|
|
|
const mockEnvironmentService = vi.hoisted(() => ({
|
|
getById: vi.fn(),
|
|
}));
|
|
|
|
const mockHeartbeatService = vi.hoisted(() => ({
|
|
listTaskSessions: vi.fn(),
|
|
resetRuntimeSession: vi.fn(),
|
|
getRun: vi.fn(),
|
|
cancelRun: vi.fn(),
|
|
}));
|
|
|
|
const mockIssueApprovalService = vi.hoisted(() => ({
|
|
linkManyForApproval: vi.fn(),
|
|
}));
|
|
|
|
const mockIssueService = vi.hoisted(() => ({
|
|
list: vi.fn(),
|
|
}));
|
|
|
|
const mockSecretService = vi.hoisted(() => ({
|
|
normalizeAdapterConfigForPersistence: vi.fn(),
|
|
resolveAdapterConfigForRuntime: vi.fn(),
|
|
}));
|
|
|
|
const mockAgentInstructionsService = vi.hoisted(() => ({
|
|
materializeManagedBundle: vi.fn(),
|
|
}));
|
|
const mockCompanySkillService = vi.hoisted(() => ({
|
|
listRuntimeSkillEntries: vi.fn(),
|
|
resolveRequestedSkillKeys: vi.fn(),
|
|
}));
|
|
const mockWorkspaceOperationService = vi.hoisted(() => ({}));
|
|
const mockLogActivity = vi.hoisted(() => vi.fn());
|
|
const mockTrackAgentCreated = vi.hoisted(() => vi.fn());
|
|
const mockGetTelemetryClient = vi.hoisted(() => vi.fn());
|
|
const mockSyncInstructionsBundleConfigFromFilePath = vi.hoisted(() => vi.fn());
|
|
const mockEnsureOpenCodeModelConfiguredAndAvailable = vi.hoisted(() => vi.fn());
|
|
|
|
const mockInstanceSettingsService = vi.hoisted(() => ({
|
|
getGeneral: vi.fn(),
|
|
}));
|
|
|
|
function registerModuleMocks() {
|
|
vi.doMock("../routes/agents.js", async () => vi.importActual("../routes/agents.js"));
|
|
vi.doMock("../routes/authz.js", async () => vi.importActual("../routes/authz.js"));
|
|
vi.doMock("../adapters/index.js", async () => vi.importActual("../adapters/index.js"));
|
|
vi.doMock("../middleware/index.js", async () => vi.importActual("../middleware/index.js"));
|
|
vi.doMock("@paperclipai/adapter-opencode-local/server", async () => {
|
|
const actual = await vi.importActual<typeof import("@paperclipai/adapter-opencode-local/server")>("@paperclipai/adapter-opencode-local/server");
|
|
return {
|
|
...actual,
|
|
ensureOpenCodeModelConfiguredAndAvailable: mockEnsureOpenCodeModelConfiguredAndAvailable,
|
|
};
|
|
});
|
|
|
|
vi.doMock("@paperclipai/shared/telemetry", () => ({
|
|
trackAgentCreated: mockTrackAgentCreated,
|
|
trackErrorHandlerCrash: vi.fn(),
|
|
}));
|
|
|
|
vi.doMock("../telemetry.js", () => ({
|
|
getTelemetryClient: mockGetTelemetryClient,
|
|
}));
|
|
|
|
vi.doMock("../services/agents.js", () => ({
|
|
agentService: () => mockAgentService,
|
|
}));
|
|
|
|
vi.doMock("../services/access.js", () => ({
|
|
accessService: () => mockAccessService,
|
|
}));
|
|
|
|
vi.doMock("../services/approvals.js", () => ({
|
|
approvalService: () => mockApprovalService,
|
|
}));
|
|
|
|
vi.doMock("../services/company-skills.js", () => ({
|
|
companySkillService: () => mockCompanySkillService,
|
|
}));
|
|
|
|
vi.doMock("../services/budgets.js", () => ({
|
|
budgetService: () => mockBudgetService,
|
|
}));
|
|
|
|
vi.doMock("../services/heartbeat.js", () => ({
|
|
heartbeatService: () => mockHeartbeatService,
|
|
}));
|
|
|
|
vi.doMock("../services/issue-approvals.js", () => ({
|
|
issueApprovalService: () => mockIssueApprovalService,
|
|
}));
|
|
|
|
vi.doMock("../services/issues.js", () => ({
|
|
issueService: () => mockIssueService,
|
|
}));
|
|
|
|
vi.doMock("../services/secrets.js", () => ({
|
|
secretService: () => mockSecretService,
|
|
}));
|
|
|
|
vi.doMock("../services/agent-instructions.js", () => ({
|
|
agentInstructionsService: () => mockAgentInstructionsService,
|
|
syncInstructionsBundleConfigFromFilePath: mockSyncInstructionsBundleConfigFromFilePath,
|
|
}));
|
|
|
|
vi.doMock("../services/workspace-operations.js", () => ({
|
|
workspaceOperationService: () => mockWorkspaceOperationService,
|
|
}));
|
|
|
|
vi.doMock("../services/activity-log.js", () => ({
|
|
logActivity: mockLogActivity,
|
|
}));
|
|
|
|
vi.doMock("../services/instance-settings.js", () => ({
|
|
instanceSettingsService: () => mockInstanceSettingsService,
|
|
}));
|
|
|
|
vi.doMock("../services/index.js", () => ({
|
|
agentService: () => mockAgentService,
|
|
agentInstructionsService: () => mockAgentInstructionsService,
|
|
accessService: () => mockAccessService,
|
|
approvalService: () => mockApprovalService,
|
|
companySkillService: () => mockCompanySkillService,
|
|
budgetService: () => mockBudgetService,
|
|
heartbeatService: () => mockHeartbeatService,
|
|
ISSUE_LIST_DEFAULT_LIMIT: 500,
|
|
issueApprovalService: () => mockIssueApprovalService,
|
|
issueService: () => mockIssueService,
|
|
logActivity: mockLogActivity,
|
|
secretService: () => mockSecretService,
|
|
syncInstructionsBundleConfigFromFilePath: mockSyncInstructionsBundleConfigFromFilePath,
|
|
workspaceOperationService: () => mockWorkspaceOperationService,
|
|
environmentService: () => mockEnvironmentService,
|
|
}));
|
|
}
|
|
|
|
function createDbStub(options: { requireBoardApprovalForNewAgents?: boolean } = {}) {
|
|
return {
|
|
select: vi.fn().mockReturnValue({
|
|
from: vi.fn().mockReturnValue({
|
|
where: vi.fn().mockReturnValue({
|
|
then: vi.fn((resolve) =>
|
|
Promise.resolve(resolve([{
|
|
id: companyId,
|
|
name: "Paperclip",
|
|
requireBoardApprovalForNewAgents: options.requireBoardApprovalForNewAgents ?? false,
|
|
}])),
|
|
),
|
|
}),
|
|
}),
|
|
}),
|
|
};
|
|
}
|
|
|
|
let sharedServer: Server | null = null;
|
|
|
|
async function closeSharedServer() {
|
|
if (!sharedServer) return;
|
|
const server = sharedServer;
|
|
sharedServer = null;
|
|
await new Promise<void>((resolve, reject) => {
|
|
server.close((err) => {
|
|
if (err) reject(err);
|
|
else resolve();
|
|
});
|
|
});
|
|
}
|
|
|
|
async function createApp(actor: Record<string, unknown>, dbOptions: { requireBoardApprovalForNewAgents?: boolean } = {}) {
|
|
await closeSharedServer();
|
|
const [{ errorHandler }, { agentRoutes }] = await Promise.all([
|
|
import("../middleware/index.js"),
|
|
import("../routes/agents.js"),
|
|
]);
|
|
const app = express();
|
|
app.use(express.json());
|
|
app.use((req, _res, next) => {
|
|
(req as any).actor = actor;
|
|
next();
|
|
});
|
|
app.use("/api", agentRoutes(createDbStub(dbOptions) as any));
|
|
app.use(errorHandler);
|
|
sharedServer = app.listen(0, "127.0.0.1");
|
|
await new Promise<void>((resolve) => {
|
|
sharedServer?.once("listening", resolve);
|
|
});
|
|
return sharedServer;
|
|
}
|
|
|
|
describe.sequential("agent permission routes", () => {
|
|
afterEach(closeSharedServer);
|
|
|
|
beforeEach(() => {
|
|
vi.resetModules();
|
|
vi.doUnmock("@paperclipai/shared/telemetry");
|
|
vi.doUnmock("../telemetry.js");
|
|
vi.doUnmock("../services/access.js");
|
|
vi.doUnmock("../services/activity-log.js");
|
|
vi.doUnmock("../services/agent-instructions.js");
|
|
vi.doUnmock("../services/agents.js");
|
|
vi.doUnmock("../services/approvals.js");
|
|
vi.doUnmock("../services/budgets.js");
|
|
vi.doUnmock("../services/company-skills.js");
|
|
vi.doUnmock("../services/heartbeat.js");
|
|
vi.doUnmock("../services/index.js");
|
|
vi.doUnmock("../services/instance-settings.js");
|
|
vi.doUnmock("../services/issue-approvals.js");
|
|
vi.doUnmock("../services/issues.js");
|
|
vi.doUnmock("../services/secrets.js");
|
|
vi.doUnmock("../services/workspace-operations.js");
|
|
vi.doUnmock("../adapters/index.js");
|
|
vi.doUnmock("../routes/agents.js");
|
|
vi.doUnmock("../routes/authz.js");
|
|
vi.doUnmock("../middleware/index.js");
|
|
vi.doUnmock("@paperclipai/adapter-opencode-local/server");
|
|
registerModuleMocks();
|
|
vi.resetAllMocks();
|
|
mockAgentService.getById.mockReset();
|
|
mockAgentService.list.mockReset();
|
|
mockAgentService.create.mockReset();
|
|
mockAgentService.activatePendingApproval.mockReset();
|
|
mockAgentService.update.mockReset();
|
|
mockAgentService.updatePermissions.mockReset();
|
|
mockAgentService.getChainOfCommand.mockReset();
|
|
mockAgentService.resolveByReference.mockReset();
|
|
mockAccessService.canUser.mockReset();
|
|
mockAccessService.hasPermission.mockReset();
|
|
mockAccessService.getMembership.mockReset();
|
|
mockAccessService.ensureMembership.mockReset();
|
|
mockAccessService.listPrincipalGrants.mockReset();
|
|
mockAccessService.setPrincipalPermission.mockReset();
|
|
mockApprovalService.create.mockReset();
|
|
mockApprovalService.getById.mockReset();
|
|
mockBudgetService.upsertPolicy.mockReset();
|
|
mockHeartbeatService.listTaskSessions.mockReset();
|
|
mockHeartbeatService.resetRuntimeSession.mockReset();
|
|
mockHeartbeatService.getRun.mockReset();
|
|
mockHeartbeatService.cancelRun.mockReset();
|
|
mockIssueApprovalService.linkManyForApproval.mockReset();
|
|
mockIssueService.list.mockReset();
|
|
mockSecretService.normalizeAdapterConfigForPersistence.mockReset();
|
|
mockSecretService.resolveAdapterConfigForRuntime.mockReset();
|
|
mockAgentInstructionsService.materializeManagedBundle.mockReset();
|
|
mockCompanySkillService.listRuntimeSkillEntries.mockReset();
|
|
mockCompanySkillService.resolveRequestedSkillKeys.mockReset();
|
|
mockLogActivity.mockReset();
|
|
mockTrackAgentCreated.mockReset();
|
|
mockGetTelemetryClient.mockReset();
|
|
mockSyncInstructionsBundleConfigFromFilePath.mockReset();
|
|
mockInstanceSettingsService.getGeneral.mockReset();
|
|
mockEnsureOpenCodeModelConfiguredAndAvailable.mockReset();
|
|
mockSyncInstructionsBundleConfigFromFilePath.mockImplementation((_agent, config) => config);
|
|
mockGetTelemetryClient.mockReturnValue({ track: vi.fn() });
|
|
mockAgentService.getById.mockResolvedValue(baseAgent);
|
|
mockAgentService.list.mockResolvedValue([baseAgent]);
|
|
mockAgentService.getChainOfCommand.mockResolvedValue([]);
|
|
mockAgentService.resolveByReference.mockResolvedValue({ ambiguous: false, agent: baseAgent });
|
|
mockAgentService.create.mockResolvedValue(baseAgent);
|
|
mockAgentService.activatePendingApproval.mockResolvedValue({
|
|
agent: baseAgent,
|
|
activated: false,
|
|
});
|
|
mockAgentService.update.mockResolvedValue(baseAgent);
|
|
mockAgentService.updatePermissions.mockResolvedValue(baseAgent);
|
|
mockAccessService.canUser.mockResolvedValue(true);
|
|
mockAccessService.hasPermission.mockResolvedValue(false);
|
|
mockAccessService.getMembership.mockResolvedValue({
|
|
id: "membership-1",
|
|
companyId,
|
|
principalType: "agent",
|
|
principalId: agentId,
|
|
status: "active",
|
|
membershipRole: "member",
|
|
createdAt: new Date("2026-03-19T00:00:00.000Z"),
|
|
updatedAt: new Date("2026-03-19T00:00:00.000Z"),
|
|
});
|
|
mockAccessService.listPrincipalGrants.mockResolvedValue([]);
|
|
mockAccessService.ensureMembership.mockResolvedValue(undefined);
|
|
mockAccessService.setPrincipalPermission.mockResolvedValue(undefined);
|
|
mockCompanySkillService.listRuntimeSkillEntries.mockResolvedValue([]);
|
|
mockCompanySkillService.resolveRequestedSkillKeys.mockImplementation(async (_companyId, requested) => requested);
|
|
mockBudgetService.upsertPolicy.mockResolvedValue(undefined);
|
|
mockEnvironmentService.getById.mockResolvedValue(null);
|
|
mockAgentInstructionsService.materializeManagedBundle.mockImplementation(
|
|
async (agent: Record<string, unknown>, files: Record<string, string>) => ({
|
|
bundle: null,
|
|
adapterConfig: {
|
|
...((agent.adapterConfig as Record<string, unknown> | undefined) ?? {}),
|
|
instructionsBundleMode: "managed",
|
|
instructionsRootPath: `/tmp/${String(agent.id)}/instructions`,
|
|
instructionsEntryFile: "AGENTS.md",
|
|
instructionsFilePath: `/tmp/${String(agent.id)}/instructions/AGENTS.md`,
|
|
promptTemplate: files["AGENTS.md"] ?? "",
|
|
},
|
|
}),
|
|
);
|
|
mockCompanySkillService.listRuntimeSkillEntries.mockResolvedValue([]);
|
|
mockCompanySkillService.resolveRequestedSkillKeys.mockImplementation(
|
|
async (_companyId: string, requested: string[]) => requested,
|
|
);
|
|
mockSecretService.normalizeAdapterConfigForPersistence.mockImplementation(async (_companyId, config) => config);
|
|
mockSecretService.resolveAdapterConfigForRuntime.mockImplementation(async (_companyId, config) => ({ config }));
|
|
mockInstanceSettingsService.getGeneral.mockResolvedValue({
|
|
censorUsernameInLogs: false,
|
|
});
|
|
mockEnsureOpenCodeModelConfiguredAndAvailable.mockResolvedValue([
|
|
{ id: "opencode/gpt-5-nano", label: "opencode/gpt-5-nano" },
|
|
]);
|
|
mockLogActivity.mockResolvedValue(undefined);
|
|
});
|
|
|
|
it("redacts agent detail for authenticated company members without agent admin permission", async () => {
|
|
mockAccessService.canUser.mockResolvedValue(false);
|
|
|
|
const app = await createApp({
|
|
type: "board",
|
|
userId: "member-user",
|
|
source: "session",
|
|
isInstanceAdmin: false,
|
|
companyIds: [companyId],
|
|
});
|
|
|
|
const res = await request(app).get(`/api/agents/${agentId}`);
|
|
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.adapterConfig).toEqual({});
|
|
expect(res.body.runtimeConfig).toEqual({});
|
|
}, 20_000);
|
|
|
|
it("redacts company agent list for authenticated company members without agent admin permission", async () => {
|
|
mockAccessService.canUser.mockResolvedValue(false);
|
|
|
|
const app = await createApp({
|
|
type: "board",
|
|
userId: "member-user",
|
|
source: "session",
|
|
isInstanceAdmin: false,
|
|
companyIds: [companyId],
|
|
});
|
|
|
|
const res = await request(app).get(`/api/companies/${companyId}/agents`);
|
|
|
|
expect(res.status).toBe(200);
|
|
expect(res.body).toEqual([
|
|
expect.objectContaining({
|
|
id: agentId,
|
|
adapterConfig: {},
|
|
runtimeConfig: {},
|
|
}),
|
|
]);
|
|
});
|
|
|
|
it("blocks agent updates for authenticated company members without agent admin permission", async () => {
|
|
mockAccessService.canUser.mockResolvedValue(false);
|
|
|
|
const app = await createApp({
|
|
type: "board",
|
|
userId: "member-user",
|
|
source: "session",
|
|
isInstanceAdmin: false,
|
|
companyIds: [companyId],
|
|
});
|
|
|
|
const res = await request(app)
|
|
.patch(`/api/agents/${agentId}`)
|
|
.send({ title: "Compromised" });
|
|
|
|
expect(res.status).toBe(403);
|
|
});
|
|
|
|
it("blocks api key creation for authenticated company members without agent admin permission", async () => {
|
|
mockAccessService.canUser.mockResolvedValue(false);
|
|
|
|
const app = await createApp({
|
|
type: "board",
|
|
userId: "member-user",
|
|
source: "session",
|
|
isInstanceAdmin: false,
|
|
companyIds: [companyId],
|
|
});
|
|
|
|
const res = await request(app)
|
|
.post(`/api/agents/${agentId}/keys`)
|
|
.send({ name: "backdoor" });
|
|
|
|
expect(res.status).toBe(403);
|
|
});
|
|
|
|
it("blocks wakeups for authenticated company members without agent admin permission", async () => {
|
|
mockAccessService.canUser.mockResolvedValue(false);
|
|
|
|
const app = await createApp({
|
|
type: "board",
|
|
userId: "member-user",
|
|
source: "session",
|
|
isInstanceAdmin: false,
|
|
companyIds: [companyId],
|
|
});
|
|
|
|
const res = await request(app)
|
|
.post(`/api/agents/${agentId}/wakeup`)
|
|
.send({});
|
|
|
|
expect(res.status).toBe(403);
|
|
});
|
|
|
|
it("blocks agent-authenticated self-updates that set host-executed workspace commands", async () => {
|
|
const app = await createApp({
|
|
type: "agent",
|
|
agentId,
|
|
companyId,
|
|
source: "agent_key",
|
|
runId: "run-1",
|
|
});
|
|
|
|
const res = await request(app)
|
|
.patch(`/api/agents/${agentId}`)
|
|
.send({
|
|
adapterConfig: {
|
|
workspaceStrategy: {
|
|
type: "git_worktree",
|
|
provisionCommand: "touch /tmp/paperclip-rce",
|
|
},
|
|
},
|
|
});
|
|
|
|
expect(res.status).toBe(403);
|
|
expect(res.body.error).toContain("host-executed workspace commands");
|
|
expect(mockLogActivity).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("blocks agent-authenticated self-updates that set instructions bundle roots", async () => {
|
|
const app = await createApp({
|
|
type: "agent",
|
|
agentId,
|
|
companyId,
|
|
source: "agent_key",
|
|
runId: "run-1",
|
|
});
|
|
|
|
const res = await request(app)
|
|
.patch(`/api/agents/${agentId}`)
|
|
.send({
|
|
adapterConfig: {
|
|
instructionsRootPath: "/etc",
|
|
instructionsEntryFile: "passwd",
|
|
},
|
|
});
|
|
|
|
expect(res.status).toBe(403);
|
|
expect(res.body.error).toContain("instructions path or bundle configuration");
|
|
expect(mockLogActivity).not.toHaveBeenCalled();
|
|
}, 15_000);
|
|
|
|
it("blocks agent-authenticated instructions-path updates", async () => {
|
|
const app = await createApp({
|
|
type: "agent",
|
|
agentId,
|
|
companyId,
|
|
source: "agent_key",
|
|
runId: "run-1",
|
|
});
|
|
|
|
const res = await request(app)
|
|
.patch(`/api/agents/${agentId}/instructions-path`)
|
|
.send({ path: "/etc/passwd" });
|
|
|
|
expect(res.status).toBe(403);
|
|
expect(res.body.error).toContain("instructions path or bundle configuration");
|
|
expect(mockLogActivity).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("blocks agent-authenticated hires that set instructions bundle config", async () => {
|
|
mockAccessService.hasPermission.mockResolvedValue(true);
|
|
|
|
const app = await createApp({
|
|
type: "agent",
|
|
agentId,
|
|
companyId,
|
|
source: "agent_key",
|
|
runId: "run-1",
|
|
});
|
|
|
|
const res = await request(app)
|
|
.post(`/api/companies/${companyId}/agent-hires`)
|
|
.send({
|
|
name: "Injected",
|
|
role: "engineer",
|
|
adapterType: "codex_local",
|
|
adapterConfig: {
|
|
instructionsRootPath: "/etc",
|
|
instructionsEntryFile: "passwd",
|
|
},
|
|
});
|
|
|
|
expect(res.status).toBe(403);
|
|
expect(res.body.error).toContain("instructions path or bundle configuration");
|
|
expect(mockAgentService.create).not.toHaveBeenCalled();
|
|
expect(mockLogActivity).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("blocks direct agent creation for authenticated company members without agent create permission", async () => {
|
|
mockAccessService.canUser.mockResolvedValue(false);
|
|
|
|
const app = await createApp({
|
|
type: "board",
|
|
userId: "member-user",
|
|
source: "session",
|
|
isInstanceAdmin: false,
|
|
companyIds: [companyId],
|
|
});
|
|
|
|
const res = await request(app)
|
|
.post(`/api/companies/${companyId}/agents`)
|
|
.send({
|
|
name: "Backdoor",
|
|
role: "engineer",
|
|
adapterType: "process",
|
|
adapterConfig: {},
|
|
});
|
|
|
|
expect(res.status).toBe(403);
|
|
expect(res.body.error).toContain("agents:create");
|
|
expect(mockAgentService.create).not.toHaveBeenCalled();
|
|
expect(mockLogActivity).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("allows direct agent creation for authenticated board users with agent create permission when approval is not required", async () => {
|
|
mockAccessService.canUser.mockResolvedValue(true);
|
|
|
|
const app = await createApp({
|
|
type: "board",
|
|
userId: "agent-admin-user",
|
|
source: "session",
|
|
isInstanceAdmin: false,
|
|
companyIds: [companyId],
|
|
});
|
|
|
|
const res = await request(app)
|
|
.post(`/api/companies/${companyId}/agents`)
|
|
.send({
|
|
name: "Builder",
|
|
role: "engineer",
|
|
adapterType: "process",
|
|
adapterConfig: {},
|
|
});
|
|
|
|
expect([200, 201]).toContain(res.status);
|
|
expect(mockAgentService.create).toHaveBeenCalledWith(
|
|
companyId,
|
|
expect.objectContaining({
|
|
status: "idle",
|
|
}),
|
|
);
|
|
expect(mockAccessService.setPrincipalPermission).toHaveBeenCalledWith(
|
|
companyId,
|
|
"agent",
|
|
agentId,
|
|
"tasks:assign",
|
|
true,
|
|
"agent-admin-user",
|
|
);
|
|
});
|
|
|
|
it("rejects direct agent creation when new agents require board approval", async () => {
|
|
const app = await createApp(
|
|
{
|
|
type: "board",
|
|
userId: "board-user",
|
|
source: "local_implicit",
|
|
isInstanceAdmin: true,
|
|
companyIds: [companyId],
|
|
},
|
|
{ requireBoardApprovalForNewAgents: true },
|
|
);
|
|
|
|
const res = await request(app)
|
|
.post(`/api/companies/${companyId}/agents`)
|
|
.send({
|
|
name: "Builder",
|
|
role: "engineer",
|
|
adapterType: "process",
|
|
adapterConfig: {},
|
|
});
|
|
|
|
expect(res.status).toBe(409);
|
|
expect(res.body.error).toContain("/agent-hires");
|
|
expect(mockAgentService.create).not.toHaveBeenCalled();
|
|
expect(mockApprovalService.create).not.toHaveBeenCalled();
|
|
expect(mockLogActivity).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("grants tasks:assign by default when board creates a new agent", async () => {
|
|
const app = await createApp({
|
|
type: "board",
|
|
userId: "board-user",
|
|
source: "local_implicit",
|
|
isInstanceAdmin: true,
|
|
companyIds: [companyId],
|
|
});
|
|
|
|
const res = await request(app)
|
|
.post(`/api/companies/${companyId}/agents`)
|
|
.send({
|
|
name: "Builder",
|
|
role: "engineer",
|
|
adapterType: "process",
|
|
adapterConfig: {},
|
|
});
|
|
|
|
expect([200, 201]).toContain(res.status);
|
|
expect(mockAccessService.ensureMembership).toHaveBeenCalledWith(
|
|
companyId,
|
|
"agent",
|
|
agentId,
|
|
"member",
|
|
"active",
|
|
);
|
|
expect(mockAccessService.setPrincipalPermission).toHaveBeenCalledWith(
|
|
companyId,
|
|
"agent",
|
|
agentId,
|
|
"tasks:assign",
|
|
true,
|
|
"board-user",
|
|
);
|
|
}, 15_000);
|
|
|
|
it("rejects unsupported query parameters on the agent list route", async () => {
|
|
const app = await createApp({
|
|
type: "board",
|
|
userId: "board-user",
|
|
source: "local_implicit",
|
|
isInstanceAdmin: true,
|
|
companyIds: [companyId],
|
|
});
|
|
|
|
const res = await request(app)
|
|
.get(`/api/companies/${companyId}/agents`)
|
|
.query({ urlKey: "builder" });
|
|
|
|
expect(res.status).toBe(400);
|
|
expect(res.body.error).toContain("urlKey");
|
|
expect(mockAgentService.list).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("normalizes direct agent creation to disable timer heartbeats by default", async () => {
|
|
const app = await createApp({
|
|
type: "board",
|
|
userId: "board-user",
|
|
source: "local_implicit",
|
|
isInstanceAdmin: true,
|
|
companyIds: [companyId],
|
|
});
|
|
|
|
const res = await request(app)
|
|
.post(`/api/companies/${companyId}/agents`)
|
|
.send({
|
|
name: "Builder",
|
|
role: "engineer",
|
|
adapterType: "process",
|
|
adapterConfig: {},
|
|
runtimeConfig: {
|
|
heartbeat: {
|
|
intervalSec: 3600,
|
|
},
|
|
},
|
|
});
|
|
|
|
expect([200, 201]).toContain(res.status);
|
|
expect(mockAgentService.create).toHaveBeenCalledWith(
|
|
companyId,
|
|
expect.objectContaining({
|
|
runtimeConfig: {
|
|
heartbeat: {
|
|
enabled: false,
|
|
intervalSec: 3600,
|
|
maxConcurrentRuns: 5,
|
|
},
|
|
},
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("normalizes hire requests to disable timer heartbeats by default", async () => {
|
|
const app = await createApp({
|
|
type: "board",
|
|
userId: "board-user",
|
|
source: "local_implicit",
|
|
isInstanceAdmin: true,
|
|
companyIds: [companyId],
|
|
});
|
|
|
|
const res = await request(app)
|
|
.post(`/api/companies/${companyId}/agent-hires`)
|
|
.send({
|
|
name: "Builder",
|
|
role: "engineer",
|
|
adapterType: "process",
|
|
adapterConfig: {},
|
|
runtimeConfig: {
|
|
heartbeat: {
|
|
intervalSec: 3600,
|
|
},
|
|
},
|
|
});
|
|
|
|
expect(res.status).toBe(201);
|
|
expect(mockAgentService.create).toHaveBeenCalledWith(
|
|
companyId,
|
|
expect.objectContaining({
|
|
runtimeConfig: {
|
|
heartbeat: {
|
|
enabled: false,
|
|
intervalSec: 3600,
|
|
maxConcurrentRuns: 5,
|
|
},
|
|
},
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("allows board users to directly approve pending agents", async () => {
|
|
const pendingAgent = {
|
|
...baseAgent,
|
|
status: "pending_approval",
|
|
};
|
|
const approvedAgent = {
|
|
...baseAgent,
|
|
status: "idle",
|
|
};
|
|
mockAgentService.getById.mockResolvedValue(pendingAgent);
|
|
mockAgentService.activatePendingApproval.mockResolvedValue({
|
|
agent: approvedAgent,
|
|
activated: true,
|
|
});
|
|
|
|
const app = await createApp({
|
|
type: "board",
|
|
userId: "board-user",
|
|
source: "local_implicit",
|
|
isInstanceAdmin: true,
|
|
companyIds: [companyId],
|
|
});
|
|
|
|
const res = await request(app)
|
|
.post(`/api/agents/${agentId}/approve`)
|
|
.send({});
|
|
|
|
expect(res.status).toBe(200);
|
|
expect(mockAgentService.activatePendingApproval).toHaveBeenCalledWith(agentId);
|
|
expect(mockLogActivity).toHaveBeenCalledWith(expect.anything(), expect.objectContaining({
|
|
companyId,
|
|
actorType: "user",
|
|
actorId: "board-user",
|
|
action: "agent.approved",
|
|
entityType: "agent",
|
|
entityId: agentId,
|
|
details: { source: "agent_detail" },
|
|
}));
|
|
});
|
|
|
|
it("rejects direct approval for agents that are not pending approval", async () => {
|
|
const app = await createApp({
|
|
type: "board",
|
|
userId: "board-user",
|
|
source: "local_implicit",
|
|
isInstanceAdmin: true,
|
|
companyIds: [companyId],
|
|
});
|
|
|
|
const res = await request(app)
|
|
.post(`/api/agents/${agentId}/approve`)
|
|
.send({});
|
|
|
|
expect(res.status).toBe(409);
|
|
expect(mockAgentService.activatePendingApproval).not.toHaveBeenCalled();
|
|
expect(mockLogActivity).not.toHaveBeenCalledWith(expect.anything(), expect.objectContaining({
|
|
action: "agent.approved",
|
|
}));
|
|
});
|
|
|
|
it("rejects creating an agent with an environment from another company", async () => {
|
|
mockEnvironmentService.getById.mockResolvedValue({
|
|
id: "33333333-3333-4333-8333-333333333333",
|
|
companyId: "other-company",
|
|
});
|
|
|
|
const app = await createApp({
|
|
type: "board",
|
|
userId: "board-user",
|
|
source: "local_implicit",
|
|
isInstanceAdmin: true,
|
|
companyIds: [companyId],
|
|
});
|
|
|
|
const res = await request(app)
|
|
.post(`/api/companies/${companyId}/agents`)
|
|
.send({
|
|
name: "Builder",
|
|
role: "engineer",
|
|
adapterType: "process",
|
|
adapterConfig: {},
|
|
defaultEnvironmentId: "33333333-3333-4333-8333-333333333333",
|
|
});
|
|
|
|
expect(res.status).toBe(422);
|
|
expect(res.body.error).toContain("Environment not found");
|
|
});
|
|
|
|
it("rejects creating an agent with an unsupported non-local default environment", async () => {
|
|
mockEnvironmentService.getById.mockResolvedValue({
|
|
id: "33333333-3333-4333-8333-333333333333",
|
|
companyId,
|
|
driver: "ssh",
|
|
});
|
|
|
|
const app = await createApp({
|
|
type: "board",
|
|
userId: "board-user",
|
|
source: "local_implicit",
|
|
isInstanceAdmin: true,
|
|
companyIds: [companyId],
|
|
});
|
|
|
|
const res = await request(app)
|
|
.post(`/api/companies/${companyId}/agents`)
|
|
.send({
|
|
name: "Builder",
|
|
role: "engineer",
|
|
adapterType: "process",
|
|
adapterConfig: {},
|
|
defaultEnvironmentId: "33333333-3333-4333-8333-333333333333",
|
|
});
|
|
|
|
expect(res.status).toBe(422);
|
|
expect(res.body.error).toContain('Environment driver "ssh" is not allowed here');
|
|
});
|
|
|
|
it("allows creating a codex agent with an SSH default environment", async () => {
|
|
mockEnvironmentService.getById.mockResolvedValue({
|
|
id: "33333333-3333-4333-8333-333333333333",
|
|
companyId,
|
|
driver: "ssh",
|
|
});
|
|
mockAgentService.create.mockResolvedValue({
|
|
...baseAgent,
|
|
adapterType: "codex_local",
|
|
defaultEnvironmentId: "33333333-3333-4333-8333-333333333333",
|
|
});
|
|
|
|
const app = await createApp({
|
|
type: "board",
|
|
userId: "board-user",
|
|
source: "local_implicit",
|
|
isInstanceAdmin: true,
|
|
companyIds: [companyId],
|
|
});
|
|
|
|
const res = await request(app)
|
|
.post(`/api/companies/${companyId}/agents`)
|
|
.send({
|
|
name: "Codex Builder",
|
|
role: "engineer",
|
|
adapterType: "codex_local",
|
|
adapterConfig: {},
|
|
defaultEnvironmentId: "33333333-3333-4333-8333-333333333333",
|
|
});
|
|
|
|
expect([200, 201]).toContain(res.status);
|
|
});
|
|
|
|
it("allows creating a claude agent with an SSH default environment", async () => {
|
|
mockEnvironmentService.getById.mockResolvedValue({
|
|
id: "33333333-3333-4333-8333-333333333333",
|
|
companyId,
|
|
driver: "ssh",
|
|
});
|
|
mockAgentService.create.mockResolvedValue({
|
|
...baseAgent,
|
|
adapterType: "claude_local",
|
|
defaultEnvironmentId: "33333333-3333-4333-8333-333333333333",
|
|
});
|
|
|
|
const app = await createApp({
|
|
type: "board",
|
|
userId: "board-user",
|
|
source: "local_implicit",
|
|
isInstanceAdmin: true,
|
|
companyIds: [companyId],
|
|
});
|
|
|
|
const res = await request(app)
|
|
.post(`/api/companies/${companyId}/agents`)
|
|
.send({
|
|
name: "Claude Builder",
|
|
role: "engineer",
|
|
adapterType: "claude_local",
|
|
adapterConfig: {},
|
|
defaultEnvironmentId: "33333333-3333-4333-8333-333333333333",
|
|
});
|
|
|
|
expect(res.status).toBe(201);
|
|
});
|
|
|
|
it("allows creating a gemini agent with an SSH default environment", async () => {
|
|
mockEnvironmentService.getById.mockResolvedValue({
|
|
id: "33333333-3333-4333-8333-333333333333",
|
|
companyId,
|
|
driver: "ssh",
|
|
});
|
|
mockAgentService.create.mockResolvedValue({
|
|
...baseAgent,
|
|
adapterType: "gemini_local",
|
|
defaultEnvironmentId: "33333333-3333-4333-8333-333333333333",
|
|
});
|
|
|
|
const app = await createApp({
|
|
type: "board",
|
|
userId: "board-user",
|
|
source: "local_implicit",
|
|
isInstanceAdmin: true,
|
|
companyIds: [companyId],
|
|
});
|
|
|
|
const res = await request(app)
|
|
.post(`/api/companies/${companyId}/agents`)
|
|
.send({
|
|
name: "Gemini Builder",
|
|
role: "engineer",
|
|
adapterType: "gemini_local",
|
|
adapterConfig: {},
|
|
defaultEnvironmentId: "33333333-3333-4333-8333-333333333333",
|
|
});
|
|
|
|
expect(res.status).toBe(201);
|
|
});
|
|
|
|
it("allows creating an opencode agent with an SSH default environment", async () => {
|
|
mockEnvironmentService.getById.mockResolvedValue({
|
|
id: "33333333-3333-4333-8333-333333333333",
|
|
companyId,
|
|
driver: "ssh",
|
|
});
|
|
mockAgentService.create.mockResolvedValue({
|
|
...baseAgent,
|
|
adapterType: "opencode_local",
|
|
defaultEnvironmentId: "33333333-3333-4333-8333-333333333333",
|
|
});
|
|
|
|
const app = await createApp({
|
|
type: "board",
|
|
userId: "board-user",
|
|
source: "local_implicit",
|
|
isInstanceAdmin: true,
|
|
companyIds: [companyId],
|
|
});
|
|
|
|
const res = await request(app)
|
|
.post(`/api/companies/${companyId}/agents`)
|
|
.send({
|
|
name: "OpenCode Builder",
|
|
role: "engineer",
|
|
adapterType: "opencode_local",
|
|
adapterConfig: {
|
|
model: "opencode/gpt-5-nano",
|
|
},
|
|
defaultEnvironmentId: "33333333-3333-4333-8333-333333333333",
|
|
});
|
|
|
|
expect(res.status).toBe(201);
|
|
});
|
|
|
|
it("allows creating a cursor agent with an SSH default environment", async () => {
|
|
mockEnvironmentService.getById.mockResolvedValue({
|
|
id: "33333333-3333-4333-8333-333333333333",
|
|
companyId,
|
|
driver: "ssh",
|
|
});
|
|
mockAgentService.create.mockResolvedValue({
|
|
...baseAgent,
|
|
adapterType: "cursor",
|
|
defaultEnvironmentId: "33333333-3333-4333-8333-333333333333",
|
|
});
|
|
|
|
const app = await createApp({
|
|
type: "board",
|
|
userId: "board-user",
|
|
source: "local_implicit",
|
|
isInstanceAdmin: true,
|
|
companyIds: [companyId],
|
|
});
|
|
|
|
const res = await request(app)
|
|
.post(`/api/companies/${companyId}/agents`)
|
|
.send({
|
|
name: "Cursor Builder",
|
|
role: "engineer",
|
|
adapterType: "cursor",
|
|
adapterConfig: {},
|
|
defaultEnvironmentId: "33333333-3333-4333-8333-333333333333",
|
|
});
|
|
|
|
expect(res.status).toBe(201);
|
|
});
|
|
|
|
it("allows creating a pi agent with an SSH default environment", async () => {
|
|
mockEnvironmentService.getById.mockResolvedValue({
|
|
id: "33333333-3333-4333-8333-333333333333",
|
|
companyId,
|
|
driver: "ssh",
|
|
});
|
|
mockAgentService.create.mockResolvedValue({
|
|
...baseAgent,
|
|
adapterType: "pi_local",
|
|
defaultEnvironmentId: "33333333-3333-4333-8333-333333333333",
|
|
});
|
|
|
|
const app = await createApp({
|
|
type: "board",
|
|
userId: "board-user",
|
|
source: "local_implicit",
|
|
isInstanceAdmin: true,
|
|
companyIds: [companyId],
|
|
});
|
|
|
|
const res = await request(app)
|
|
.post(`/api/companies/${companyId}/agents`)
|
|
.send({
|
|
name: "Pi Builder",
|
|
role: "engineer",
|
|
adapterType: "pi_local",
|
|
adapterConfig: {
|
|
model: "openai/gpt-5.4-mini",
|
|
},
|
|
defaultEnvironmentId: "33333333-3333-4333-8333-333333333333",
|
|
});
|
|
|
|
expect([200, 201]).toContain(res.status);
|
|
});
|
|
|
|
it("rejects updating an agent with an unsupported non-local default environment", async () => {
|
|
mockEnvironmentService.getById.mockResolvedValue({
|
|
id: "33333333-3333-4333-8333-333333333333",
|
|
companyId,
|
|
driver: "ssh",
|
|
});
|
|
|
|
const app = await createApp({
|
|
type: "board",
|
|
userId: "board-user",
|
|
source: "local_implicit",
|
|
isInstanceAdmin: true,
|
|
companyIds: [companyId],
|
|
});
|
|
|
|
const res = await request(app)
|
|
.patch(`/api/agents/${agentId}`)
|
|
.send({
|
|
defaultEnvironmentId: "33333333-3333-4333-8333-333333333333",
|
|
});
|
|
|
|
expect(res.status).toBe(422);
|
|
expect(res.body.error).toContain('Environment driver "ssh" is not allowed here');
|
|
});
|
|
|
|
it("allows updating a codex agent with an SSH default environment", async () => {
|
|
mockEnvironmentService.getById.mockResolvedValue({
|
|
id: "33333333-3333-4333-8333-333333333333",
|
|
companyId,
|
|
driver: "ssh",
|
|
});
|
|
mockAgentService.getById.mockResolvedValue({
|
|
...baseAgent,
|
|
adapterType: "codex_local",
|
|
defaultEnvironmentId: null,
|
|
});
|
|
mockAgentService.update.mockResolvedValue({
|
|
...baseAgent,
|
|
adapterType: "codex_local",
|
|
defaultEnvironmentId: "33333333-3333-4333-8333-333333333333",
|
|
});
|
|
|
|
const app = await createApp({
|
|
type: "board",
|
|
userId: "board-user",
|
|
source: "local_implicit",
|
|
isInstanceAdmin: true,
|
|
companyIds: [companyId],
|
|
});
|
|
|
|
const res = await request(app)
|
|
.patch(`/api/agents/${agentId}`)
|
|
.send({
|
|
defaultEnvironmentId: "33333333-3333-4333-8333-333333333333",
|
|
});
|
|
|
|
expect(res.status).toBe(200);
|
|
});
|
|
|
|
it("allows updating a claude agent with an SSH default environment", async () => {
|
|
mockEnvironmentService.getById.mockResolvedValue({
|
|
id: "33333333-3333-4333-8333-333333333333",
|
|
companyId,
|
|
driver: "ssh",
|
|
});
|
|
mockAgentService.getById.mockResolvedValue({
|
|
...baseAgent,
|
|
adapterType: "claude_local",
|
|
defaultEnvironmentId: null,
|
|
});
|
|
mockAgentService.update.mockResolvedValue({
|
|
...baseAgent,
|
|
adapterType: "claude_local",
|
|
defaultEnvironmentId: "33333333-3333-4333-8333-333333333333",
|
|
});
|
|
|
|
const app = await createApp({
|
|
type: "board",
|
|
userId: "board-user",
|
|
source: "local_implicit",
|
|
isInstanceAdmin: true,
|
|
companyIds: [companyId],
|
|
});
|
|
|
|
const res = await request(app)
|
|
.patch(`/api/agents/${agentId}`)
|
|
.send({
|
|
defaultEnvironmentId: "33333333-3333-4333-8333-333333333333",
|
|
});
|
|
|
|
expect(res.status).toBe(200);
|
|
});
|
|
|
|
it("allows updating a gemini agent with an SSH default environment", async () => {
|
|
mockEnvironmentService.getById.mockResolvedValue({
|
|
id: "33333333-3333-4333-8333-333333333333",
|
|
companyId,
|
|
driver: "ssh",
|
|
});
|
|
mockAgentService.getById.mockResolvedValue({
|
|
...baseAgent,
|
|
adapterType: "gemini_local",
|
|
defaultEnvironmentId: null,
|
|
});
|
|
mockAgentService.update.mockResolvedValue({
|
|
...baseAgent,
|
|
adapterType: "gemini_local",
|
|
defaultEnvironmentId: "33333333-3333-4333-8333-333333333333",
|
|
});
|
|
|
|
const app = await createApp({
|
|
type: "board",
|
|
userId: "board-user",
|
|
source: "local_implicit",
|
|
isInstanceAdmin: true,
|
|
companyIds: [companyId],
|
|
});
|
|
|
|
const res = await request(app)
|
|
.patch(`/api/agents/${agentId}`)
|
|
.send({
|
|
defaultEnvironmentId: "33333333-3333-4333-8333-333333333333",
|
|
});
|
|
|
|
expect(res.status).toBe(200);
|
|
});
|
|
|
|
it("allows updating an opencode agent with an SSH default environment", async () => {
|
|
mockEnvironmentService.getById.mockResolvedValue({
|
|
id: "33333333-3333-4333-8333-333333333333",
|
|
companyId,
|
|
driver: "ssh",
|
|
});
|
|
mockAgentService.getById.mockResolvedValue({
|
|
...baseAgent,
|
|
adapterType: "opencode_local",
|
|
defaultEnvironmentId: null,
|
|
});
|
|
mockAgentService.update.mockResolvedValue({
|
|
...baseAgent,
|
|
adapterType: "opencode_local",
|
|
defaultEnvironmentId: "33333333-3333-4333-8333-333333333333",
|
|
});
|
|
|
|
const app = await createApp({
|
|
type: "board",
|
|
userId: "board-user",
|
|
source: "local_implicit",
|
|
isInstanceAdmin: true,
|
|
companyIds: [companyId],
|
|
});
|
|
|
|
const res = await request(app)
|
|
.patch(`/api/agents/${agentId}`)
|
|
.send({
|
|
defaultEnvironmentId: "33333333-3333-4333-8333-333333333333",
|
|
});
|
|
|
|
expect(res.status).toBe(200);
|
|
});
|
|
|
|
it("allows updating a cursor agent with an SSH default environment", async () => {
|
|
mockEnvironmentService.getById.mockResolvedValue({
|
|
id: "33333333-3333-4333-8333-333333333333",
|
|
companyId,
|
|
driver: "ssh",
|
|
});
|
|
mockAgentService.getById.mockResolvedValue({
|
|
...baseAgent,
|
|
adapterType: "cursor",
|
|
defaultEnvironmentId: null,
|
|
});
|
|
mockAgentService.update.mockResolvedValue({
|
|
...baseAgent,
|
|
adapterType: "cursor",
|
|
defaultEnvironmentId: "33333333-3333-4333-8333-333333333333",
|
|
});
|
|
|
|
const app = await createApp({
|
|
type: "board",
|
|
userId: "board-user",
|
|
source: "local_implicit",
|
|
isInstanceAdmin: true,
|
|
companyIds: [companyId],
|
|
});
|
|
|
|
const res = await request(app)
|
|
.patch(`/api/agents/${agentId}`)
|
|
.send({
|
|
defaultEnvironmentId: "33333333-3333-4333-8333-333333333333",
|
|
});
|
|
|
|
expect(res.status).toBe(200);
|
|
});
|
|
|
|
it("allows updating a pi agent with an SSH default environment", async () => {
|
|
mockEnvironmentService.getById.mockResolvedValue({
|
|
id: "33333333-3333-4333-8333-333333333333",
|
|
companyId,
|
|
driver: "ssh",
|
|
});
|
|
mockAgentService.getById.mockResolvedValue({
|
|
...baseAgent,
|
|
adapterType: "pi_local",
|
|
defaultEnvironmentId: null,
|
|
});
|
|
mockAgentService.update.mockResolvedValue({
|
|
...baseAgent,
|
|
adapterType: "pi_local",
|
|
defaultEnvironmentId: "33333333-3333-4333-8333-333333333333",
|
|
});
|
|
|
|
const app = await createApp({
|
|
type: "board",
|
|
userId: "board-user",
|
|
source: "local_implicit",
|
|
isInstanceAdmin: true,
|
|
companyIds: [companyId],
|
|
});
|
|
|
|
const res = await request(app)
|
|
.patch(`/api/agents/${agentId}`)
|
|
.send({
|
|
defaultEnvironmentId: "33333333-3333-4333-8333-333333333333",
|
|
});
|
|
|
|
expect(res.status).toBe(200);
|
|
});
|
|
|
|
it("rejects switching a codex agent away from SSH-capable runtime without clearing its SSH default", async () => {
|
|
mockEnvironmentService.getById.mockResolvedValue({
|
|
id: "33333333-3333-4333-8333-333333333333",
|
|
companyId,
|
|
driver: "ssh",
|
|
});
|
|
mockAgentService.getById.mockResolvedValue({
|
|
...baseAgent,
|
|
adapterType: "codex_local",
|
|
defaultEnvironmentId: "33333333-3333-4333-8333-333333333333",
|
|
});
|
|
|
|
const app = await createApp({
|
|
type: "board",
|
|
userId: "board-user",
|
|
source: "local_implicit",
|
|
isInstanceAdmin: true,
|
|
companyIds: [companyId],
|
|
});
|
|
|
|
const res = await request(app)
|
|
.patch(`/api/agents/${agentId}`)
|
|
.send({
|
|
adapterType: "process",
|
|
});
|
|
|
|
expect(res.status).toBe(422);
|
|
expect(res.body.error).toContain('Environment driver "ssh" is not allowed here');
|
|
});
|
|
|
|
it("exposes explicit task assignment access on agent detail", async () => {
|
|
mockAccessService.listPrincipalGrants.mockResolvedValue([
|
|
{
|
|
id: "grant-1",
|
|
companyId,
|
|
principalType: "agent",
|
|
principalId: agentId,
|
|
permissionKey: "tasks:assign",
|
|
scope: null,
|
|
grantedByUserId: "board-user",
|
|
createdAt: new Date("2026-03-19T00:00:00.000Z"),
|
|
updatedAt: new Date("2026-03-19T00:00:00.000Z"),
|
|
},
|
|
]);
|
|
|
|
const app = await createApp({
|
|
type: "board",
|
|
userId: "board-user",
|
|
source: "local_implicit",
|
|
isInstanceAdmin: true,
|
|
companyIds: [companyId],
|
|
});
|
|
|
|
const res = await request(app).get(`/api/agents/${agentId}`);
|
|
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.access.canAssignTasks).toBe(true);
|
|
expect(res.body.access.taskAssignSource).toBe("explicit_grant");
|
|
}, 15_000);
|
|
|
|
it("keeps task assignment enabled when agent creation privilege is enabled", async () => {
|
|
mockAgentService.updatePermissions.mockResolvedValue({
|
|
...baseAgent,
|
|
permissions: { canCreateAgents: true },
|
|
});
|
|
|
|
const app = await createApp({
|
|
type: "board",
|
|
userId: "board-user",
|
|
source: "local_implicit",
|
|
isInstanceAdmin: true,
|
|
companyIds: [companyId],
|
|
});
|
|
|
|
const res = await request(app)
|
|
.patch(`/api/agents/${agentId}/permissions`)
|
|
.send({ canCreateAgents: true, canAssignTasks: false });
|
|
|
|
expect(res.status).toBe(200);
|
|
expect(mockAccessService.setPrincipalPermission).toHaveBeenCalledWith(
|
|
companyId,
|
|
"agent",
|
|
agentId,
|
|
"tasks:assign",
|
|
true,
|
|
"board-user",
|
|
);
|
|
expect(res.body.access.canAssignTasks).toBe(true);
|
|
expect(res.body.access.taskAssignSource).toBe("agent_creator");
|
|
});
|
|
|
|
it("exposes a dedicated agent route for the inbox mine view", async () => {
|
|
mockIssueService.list.mockResolvedValue([
|
|
{
|
|
id: "issue-1",
|
|
identifier: "PAP-910",
|
|
title: "Inbox follow-up",
|
|
status: "todo",
|
|
},
|
|
]);
|
|
|
|
const app = await createApp({
|
|
type: "agent",
|
|
agentId,
|
|
companyId,
|
|
runId: "run-1",
|
|
source: "agent_key",
|
|
});
|
|
|
|
const res = await request(app)
|
|
.get("/api/agents/me/inbox/mine")
|
|
.query({ userId: "board-user" });
|
|
|
|
expect(res.status).toBe(200);
|
|
expect(res.body).toEqual([
|
|
{
|
|
id: "issue-1",
|
|
identifier: "PAP-910",
|
|
title: "Inbox follow-up",
|
|
status: "todo",
|
|
},
|
|
]);
|
|
expect(mockIssueService.list).toHaveBeenCalledWith(companyId, {
|
|
touchedByUserId: "board-user",
|
|
inboxArchivedByUserId: "board-user",
|
|
status: "backlog,todo,in_progress,in_review,blocked,done",
|
|
limit: 500,
|
|
});
|
|
});
|
|
|
|
it("rejects heartbeat cancellation outside the caller company scope", async () => {
|
|
mockHeartbeatService.getRun.mockResolvedValue({
|
|
id: "run-1",
|
|
companyId: "33333333-3333-4333-8333-333333333333",
|
|
agentId,
|
|
status: "running",
|
|
});
|
|
|
|
const app = await createApp({
|
|
type: "board",
|
|
userId: "board-user",
|
|
source: "session",
|
|
isInstanceAdmin: false,
|
|
companyIds: [companyId],
|
|
});
|
|
|
|
const res = await request(app).post("/api/heartbeat-runs/run-1/cancel").send({});
|
|
|
|
expect(res.status).toBe(403);
|
|
expect(mockHeartbeatService.cancelRun).not.toHaveBeenCalled();
|
|
});
|
|
});
|