mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-01 18:07:00 +01:00
## Thinking Path > - Paperclip manages agents and governs their work. > - The Runner owns execution and task authority. > - Dot executes at OpenAI and uses Paperclip through MCP. > - Dot previously required a self-hosted controller. > - Cloud must admit Dot work in a managed sandbox and keep protocol traffic in the tenant. > - This pull request adds managed Dot execution and dedicated pairing routes. > - The same Runner path can later host sandboxed workspace tools. ## Linked Issues or Issue Description **Agent or provider** OpenAI Dot, through the existing dedicated MCP adapter. Related changes: #15402, #15414, and #15581. **Why this adapter is useful** Cloud users need to assign work to Dot and let Dot request work while idle. Neither path should execute agent commands on the control-plane host or require an idle sandbox. **How the agent is invoked** The tenant serves OAuth, signed mailbox events, and scoped tools. Normal admission creates a managed sandbox for each assignment. A verified Rust Runner owns that assignment and relays its tools and results. ## What Changed - Enable the external-agent invitation in Cloud behind the standalone Dot flag. - Launch Dot through the existing managed process launcher and authenticated PRP transport. - Verify the artifact digest and Dot capability before dispatch. Read provider checkpoints from the execution target for recovery. - Reject local execution in Cloud and reject managed workspace access for this release. - Add dedicated public Dot pairing aliases. Preserve the personal assistant consent paths. - Keep Dot events in the tenant when a personal Cloud broker is configured. - Use the app-configured heartbeat for Dot-initiated work. Report terminal admission failures and fence stranded offers before new admission. - Offer normal comments through the active assignment's mailbox. Keep their wakes deferred until a successful history receipt contains the exact IDs; preserve unread comments through finish, batching and recovery without replaying consumed input. - Require a sandbox process fingerprint before dispatch and allow a bounded five-minute controller reconnect gap. Preserve remote process birth without inspecting a colliding controller PID. - Recognize authenticated remote shutdown receipts even without an OS exit code. - Document dedicated pairing aliases and teach route coverage to inspect literal path arrays. - Add opt-in private Daytona qualification and focused contract tests. ## Verification - Workspace typecheck, production build, and UI token gates passed. - Focused onboarding, OAuth, managed environment, artifact metadata, and terminal-fence tests passed. One environment-test timeout passed on an isolated repeat. - A private Daytona fixture passed with the real Rust Runner, signed readiness, assigned app tools, document writes, duplicate receipts, authority fences, completion, and sandbox deletion. - A real OpenAI Dot completed both an operator-assigned task and a Dot-initiated task in managed Daytona sandboxes. Both leases were released. - A real accepted Dot assignment survived a controller gap longer than one minute with the same sandbox process, Runner, session, and assignment. Dot consumed the normal task-comment follow-up and completed exactly once; the lease was released. - A real cancellation fenced an accepted assignment, released its lease, and received Dot's acknowledgement without claiming a global provider stop. - Final local checks: 74 Dot execution, OAuth onboarding and API contract tests after integrating the current agent-lifecycle changes, 9 real Rust driver tests, 21 remote-runtime tests plus the 3 detached-process cleanup tests, and 17 lifecycle tests passed. The final live Daytona fixture passed and deleted its sandbox. - A final real Dot follow-up stayed pending until its exact comment appeared in a successful history receipt. Dot completed the original managed assignment once; its lease was released. - The broad local suite crossed a rebase and was stopped; its failures are not a qualification of this final source. Focused repeats passed. The final head passed all CI gates: workspace types, build, general and serialized test suites, Rust and Runner verification, all browser shards, and the canary dry run. Greptile is 5/5 with both findings resolved. ## Risks - Deploy the tenant app with its current Linux Runner artifact and the corresponding front-door protocol routes together. - This branch has not been deployed to a Cloud tenant. A deployed tenant still needs an OAuth and assignment smoke test. - Dot provider usage and cost are unmetered. Revocation fences Paperclip tools and does not prove a global OpenAI stop. - Missing or changed recovery identity fails closed. No replacement Dot thread is invented. - No schema migration is added. Managed Dot workspace access remains unavailable. ## Model Used OpenAI Codex, based on GPT-6, with reasoning, tool use, code execution, and browser testing. The exact runtime model identifier and context-window size are not exposed in this session. ## 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 searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes: #` / `Refs: #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip <noreply@paperclip.ing>
1069 lines
46 KiB
TypeScript
1069 lines
46 KiB
TypeScript
import express from "express";
|
|
import request from "supertest";
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import type { ServerAdapterModule } from "../adapters/index.js";
|
|
|
|
const mockAgentService = vi.hoisted(() => ({
|
|
getById: vi.fn(),
|
|
getChainOfCommand: vi.fn(async () => []),
|
|
}));
|
|
|
|
const mockAccessService = vi.hoisted(() => ({
|
|
canUser: vi.fn(),
|
|
decide: vi.fn(),
|
|
hasPermission: vi.fn(),
|
|
getMembership: vi.fn(async () => null),
|
|
listPrincipalGrants: vi.fn(async () => []),
|
|
}));
|
|
|
|
const mockSecretService = vi.hoisted(() => ({
|
|
normalizeAdapterConfigForPersistence: vi.fn(async (_companyId: string, config: Record<string, unknown>) => config),
|
|
resolveAdapterConfigForRuntime: vi.fn(async (_companyId: string, config: Record<string, unknown>) => ({ config })),
|
|
collectMissingRuntimeBindings: vi.fn(async () => [] as Array<Record<string, unknown>>),
|
|
resolveEnvBindings: vi.fn(async () => ({
|
|
env: {} as Record<string, string>,
|
|
secretKeys: new Set<string>(),
|
|
manifest: [],
|
|
})),
|
|
}));
|
|
|
|
const mockEnvironmentService = vi.hoisted(() => ({
|
|
getById: vi.fn(),
|
|
releaseLease: vi.fn(),
|
|
listBoundCompanyIds: vi.fn(async () => [] as string[]),
|
|
findManagedSandboxEnvironment: vi.fn(async () => null as Record<string, unknown> | null),
|
|
}));
|
|
|
|
const mockReleaseRunLease = vi.hoisted(() => vi.fn(async () => undefined));
|
|
const mockEnvironmentRuntime = vi.hoisted(() => ({
|
|
acquireRunLease: vi.fn(),
|
|
realizeWorkspace: vi.fn(),
|
|
getDriver: vi.fn(() => ({
|
|
releaseRunLease: mockReleaseRunLease,
|
|
})),
|
|
}));
|
|
|
|
const mockResolveEnvironmentExecutionTarget = vi.hoisted(() => vi.fn());
|
|
const mockInstanceSettingsService = vi.hoisted(() => ({
|
|
get: vi.fn(async () => ({ defaultEnvironmentId: null as string | null })),
|
|
getGeneral: vi.fn(async () => ({ censorUsernameInLogs: false })),
|
|
getExperimental: vi.fn(async () => ({ enableManagedSandboxOnly: false })),
|
|
}));
|
|
|
|
vi.mock("../services/index.js", () => ({
|
|
agentService: () => mockAgentService,
|
|
agentInstructionsService: () => ({}),
|
|
accessService: () => mockAccessService,
|
|
approvalService: () => ({}),
|
|
builtInAgentService: () => ({ ensureCompanyDefaultAgentGrants: vi.fn() }),
|
|
companySkillService: () => ({
|
|
listRuntimeSkillEntries: vi.fn(async () => []),
|
|
resolveRequestedSkillKeys: vi.fn(async () => []),
|
|
}),
|
|
budgetService: () => ({}),
|
|
heartbeatService: () => ({
|
|
wakeup: vi.fn(),
|
|
cancelActiveForAgent: vi.fn(),
|
|
}),
|
|
ISSUE_LIST_DEFAULT_LIMIT: 50,
|
|
issueApprovalService: () => ({}),
|
|
issueService: () => ({}),
|
|
logActivity: vi.fn(),
|
|
syncInstructionsBundleConfigFromFilePath: vi.fn((_agent, config) => config),
|
|
workspaceOperationService: () => ({}),
|
|
}));
|
|
|
|
vi.mock("../services/environments.js", () => ({
|
|
environmentService: () => mockEnvironmentService,
|
|
}));
|
|
|
|
vi.mock("../services/secrets.js", () => ({
|
|
secretService: () => mockSecretService,
|
|
}));
|
|
|
|
vi.mock("../services/environment-runtime.js", () => ({
|
|
environmentRuntimeService: () => mockEnvironmentRuntime,
|
|
}));
|
|
|
|
vi.mock("../services/environment-execution-target.js", () => ({
|
|
resolveEnvironmentExecutionTarget: mockResolveEnvironmentExecutionTarget,
|
|
}));
|
|
|
|
vi.mock("../services/instance-settings.js", () => ({
|
|
instanceSettingsService: () => mockInstanceSettingsService,
|
|
}));
|
|
|
|
vi.mock("../services/dot-runner-broker.js", () => ({
|
|
dotRunnerBroker: () => ({ enabled: async () => true, bindingForAgent: async () => null }),
|
|
}));
|
|
|
|
const testEnvironmentSpy = vi.fn();
|
|
|
|
const externalAdapter: ServerAdapterModule = {
|
|
type: "external_test",
|
|
execute: async () => ({ exitCode: 0, signal: null, timedOut: false }),
|
|
testEnvironment: testEnvironmentSpy,
|
|
};
|
|
|
|
// Only the runtime preparation is replaced: it needs a database and a stored
|
|
// grant, and these tests exercise the route's verdict, not credential
|
|
// resolution. Everything else in the module stays real. The same goes for
|
|
// validateAiApiKey — it calls the provider's real endpoint, and these tests
|
|
// direct its verdict instead of the network.
|
|
const mockPrepareManagedAiRuntime = vi.hoisted(() => vi.fn());
|
|
vi.mock("../services/ai-connection-runtime.js", async (importOriginal) => ({
|
|
...(await importOriginal<typeof import("../services/ai-connection-runtime.js")>()),
|
|
prepareManagedAiRuntime: mockPrepareManagedAiRuntime,
|
|
withManagedAiProbe: async (_db: unknown, input: unknown, probe: (runtime: unknown) => Promise<unknown>) => {
|
|
const runtime = await mockPrepareManagedAiRuntime(_db, input);
|
|
try { return await probe(runtime); } finally { await runtime.cleanup(); }
|
|
},
|
|
}));
|
|
const mockValidateAiApiKey = vi.hoisted(() => vi.fn(async () => undefined));
|
|
vi.mock("../services/ai-api-key-test.js", () => ({ validateAiApiKey: mockValidateAiApiKey }));
|
|
const mockMarkAuthenticationFailed = vi.hoisted(() => vi.fn(async () => undefined));
|
|
vi.mock("../services/ai-connections.js", async (importOriginal) => ({
|
|
...(await importOriginal<typeof import("../services/ai-connections.js")>()),
|
|
aiConnectionService: () => ({ markAuthenticationFailed: mockMarkAuthenticationFailed }),
|
|
}));
|
|
|
|
function mockManagedRuntime(method: "api_key" | "subscription") {
|
|
mockPrepareManagedAiRuntime.mockImplementation(
|
|
async (_db: unknown, input: { config: Record<string, unknown> }) => ({
|
|
config: {
|
|
...input.config,
|
|
// The real preparation injects the credential under the provider's
|
|
// env key; the adoption re-verification reads it from there.
|
|
env: { ...(method === "api_key" ? { ANTHROPIC_API_KEY: "sk-ant-test-key" } : {}) },
|
|
managedAiConnection: {
|
|
connectionId: "conn-1",
|
|
grantId: "grant-1",
|
|
provider: "anthropic",
|
|
method,
|
|
mode: "responsible_user",
|
|
responsibleUserId: "local-board",
|
|
identity: "grant-1:local-board:0000000000000000",
|
|
},
|
|
},
|
|
attribution: {
|
|
connectionId: "conn-1",
|
|
grantId: "grant-1",
|
|
provider: "anthropic",
|
|
method,
|
|
mode: "responsible_user",
|
|
responsibleUserId: "local-board",
|
|
},
|
|
accountName: "My Claude Account",
|
|
accountOwnerUserId: "local-board",
|
|
identity: "grant-1:local-board:0000000000000000",
|
|
cleanup: vi.fn(async () => {}),
|
|
}),
|
|
);
|
|
}
|
|
|
|
async function createApp() {
|
|
const [{ agentRoutes }, { errorHandler }] = await Promise.all([
|
|
vi.importActual<typeof import("../routes/agents.js")>("../routes/agents.js"),
|
|
vi.importActual<typeof import("../middleware/index.js")>("../middleware/index.js"),
|
|
]);
|
|
const app = express();
|
|
app.use(express.json());
|
|
app.use((req, _res, next) => {
|
|
(req as any).actor = {
|
|
type: "board",
|
|
userId: "local-board",
|
|
companyIds: ["company-1"],
|
|
source: "local_implicit",
|
|
isInstanceAdmin: false,
|
|
};
|
|
next();
|
|
});
|
|
app.use("/api", agentRoutes({} as any));
|
|
app.use(errorHandler);
|
|
return app;
|
|
}
|
|
|
|
async function unregisterTestAdapter(type: string) {
|
|
const { unregisterServerAdapter } = await import("../adapters/index.js");
|
|
unregisterServerAdapter(type);
|
|
}
|
|
|
|
describe("agent test-environment route", () => {
|
|
beforeEach(async () => {
|
|
vi.resetModules();
|
|
vi.clearAllMocks();
|
|
mockInstanceSettingsService.get.mockResolvedValue({ defaultEnvironmentId: null });
|
|
mockInstanceSettingsService.getExperimental.mockResolvedValue({ enableManagedSandboxOnly: false });
|
|
mockEnvironmentService.findManagedSandboxEnvironment.mockResolvedValue(null);
|
|
mockAccessService.decide.mockResolvedValue({
|
|
allowed: true,
|
|
reason: "allow_explicit_grant",
|
|
explanation: "Allowed by test grant",
|
|
});
|
|
mockEnvironmentService.getById.mockResolvedValue({
|
|
id: "11111111-1111-4111-8111-111111111111",
|
|
companyId: "company-1",
|
|
name: "Sandbox QA",
|
|
driver: "sandbox",
|
|
config: { provider: "fake-plugin" },
|
|
});
|
|
// Default to an instance-global environment with no company binding, so the
|
|
// tenant-binding guard passes unless a test overrides it.
|
|
mockEnvironmentService.listBoundCompanyIds.mockResolvedValue([]);
|
|
mockEnvironmentRuntime.acquireRunLease.mockResolvedValue({
|
|
lease: {
|
|
id: "lease-1",
|
|
provider: "daytona",
|
|
providerLeaseId: "provider-lease-1",
|
|
metadata: {
|
|
remoteCwd: "/home/user/paperclip-workspace",
|
|
sandboxId: "sandbox-1",
|
|
sandboxName: "paperclip-probe",
|
|
templateKind: "snapshot",
|
|
templateRef: "snapshot-1",
|
|
},
|
|
},
|
|
leaseContext: {
|
|
executionWorkspaceId: null,
|
|
executionWorkspaceMode: null,
|
|
},
|
|
});
|
|
mockEnvironmentRuntime.realizeWorkspace.mockResolvedValue({
|
|
cwd: "/home/user/paperclip-workspace",
|
|
});
|
|
mockResolveEnvironmentExecutionTarget.mockResolvedValue(null);
|
|
testEnvironmentSpy.mockResolvedValue({
|
|
adapterType: "external_test",
|
|
status: "pass",
|
|
checks: [
|
|
{
|
|
code: "host_probe_ran",
|
|
level: "info",
|
|
message: "host probe should not run",
|
|
},
|
|
],
|
|
testedAt: new Date(0).toISOString(),
|
|
});
|
|
await unregisterTestAdapter("external_test");
|
|
const { registerServerAdapter } = await import("../adapters/index.js");
|
|
registerServerAdapter(externalAdapter);
|
|
});
|
|
|
|
afterEach(async () => {
|
|
await unregisterTestAdapter("external_test");
|
|
});
|
|
|
|
it("accepts a managed Dot environment without allocating an idle sandbox", async () => {
|
|
vi.stubEnv("PAPERCLIP_PUBLIC_URL", "https://paperclip.example");
|
|
mockInstanceSettingsService.get.mockResolvedValue({ defaultEnvironmentId: "11111111-1111-4111-8111-111111111111" });
|
|
const app = await createApp();
|
|
try {
|
|
const res = await request(app).post("/api/companies/company-1/adapters/paperclip_runner/test-environment")
|
|
.send({ adapterConfig: { provider: "openai_dot", allowUnmeteredProvider: true } });
|
|
expect(res.status, JSON.stringify(res.body)).toBe(200);
|
|
expect(res.body.checks).toContainEqual(expect.objectContaining({ code: "dot_controller", level: "info" }));
|
|
expect(mockEnvironmentRuntime.acquireRunLease).not.toHaveBeenCalled();
|
|
expect(testEnvironmentSpy).not.toHaveBeenCalled();
|
|
} finally { vi.unstubAllEnvs(); }
|
|
});
|
|
|
|
it("fails the Cloud Dot check without a managed sandbox instead of probing the host", async () => {
|
|
vi.stubEnv("PAPERCLIP_PUBLIC_URL", "https://paperclip.example");
|
|
vi.stubEnv("PAPERCLIP_CLOUD_TENANT_SERVER_TOKEN", "test-cloud");
|
|
const app = await createApp();
|
|
try {
|
|
const res = await request(app).post("/api/companies/company-1/adapters/paperclip_runner/test-environment")
|
|
.send({ adapterConfig: { provider: "openai_dot", allowUnmeteredProvider: true } });
|
|
expect(res.status, JSON.stringify(res.body)).toBe(200);
|
|
expect(res.body.status).toBe("fail");
|
|
expect(res.body.checks).toContainEqual(expect.objectContaining({ code: "dot_controller", level: "error" }));
|
|
expect(mockEnvironmentRuntime.acquireRunLease).not.toHaveBeenCalled();
|
|
expect(testEnvironmentSpy).not.toHaveBeenCalled();
|
|
} finally { vi.unstubAllEnvs(); }
|
|
});
|
|
|
|
it("tests the instance default sandbox when the agent inherits its environment", async () => {
|
|
const environmentId = "11111111-1111-4111-8111-111111111111";
|
|
mockInstanceSettingsService.get.mockResolvedValue({ defaultEnvironmentId: environmentId });
|
|
const target = {
|
|
kind: "remote", transport: "sandbox", remoteCwd: "/workspace",
|
|
providerKey: "daytona", runner: { execute: vi.fn() },
|
|
};
|
|
mockResolveEnvironmentExecutionTarget.mockResolvedValue(target);
|
|
const app = await createApp();
|
|
const res = await request(app)
|
|
.post("/api/companies/company-1/adapters/external_test/test-environment")
|
|
.send({ adapterConfig: {}, environmentId: null });
|
|
expect(res.status, JSON.stringify(res.body)).toBe(200);
|
|
expect(mockEnvironmentRuntime.acquireRunLease).toHaveBeenCalled();
|
|
expect(testEnvironmentSpy).toHaveBeenCalledWith(expect.objectContaining({ executionTarget: target }));
|
|
expect(mockReleaseRunLease).toHaveBeenCalled();
|
|
});
|
|
|
|
it.each([
|
|
{ selection: "omitted", environmentId: undefined, expectedId: "22222222-2222-4222-8222-222222222222" },
|
|
{ selection: "cleared", environmentId: null, expectedId: "11111111-1111-4111-8111-111111111111" },
|
|
{ selection: "replaced", environmentId: "33333333-3333-4333-8333-333333333333", expectedId: "33333333-3333-4333-8333-333333333333" },
|
|
])("resolves the saved agent environment when the request selection is $selection", async ({ environmentId, expectedId }) => {
|
|
const agentId = "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa";
|
|
mockAgentService.getById.mockResolvedValue({
|
|
id: agentId, companyId: "company-1", adapterType: "external_test",
|
|
adapterConfig: {}, runtimeConfig: {}, defaultEnvironmentId: "22222222-2222-4222-8222-222222222222",
|
|
});
|
|
mockInstanceSettingsService.get.mockResolvedValue({ defaultEnvironmentId: "11111111-1111-4111-8111-111111111111" });
|
|
mockEnvironmentService.getById.mockImplementation(async (id) => ({
|
|
id, name: "Selected sandbox", driver: "sandbox", status: "active", config: { provider: "fake-plugin" },
|
|
}));
|
|
const target = { kind: "remote", transport: "sandbox", remoteCwd: "/workspace", providerKey: "daytona", runner: { execute: vi.fn() } };
|
|
mockResolveEnvironmentExecutionTarget.mockResolvedValue(target);
|
|
const app = await createApp();
|
|
const res = await request(app)
|
|
.post("/api/companies/company-1/adapters/external_test/test-environment")
|
|
.send({ agentId, adapterConfig: {}, ...(environmentId === undefined ? {} : { environmentId }) });
|
|
expect(res.status, JSON.stringify(res.body)).toBe(200);
|
|
expect(mockEnvironmentRuntime.acquireRunLease).toHaveBeenCalledWith(expect.objectContaining({
|
|
environment: expect.objectContaining({ id: expectedId }),
|
|
}));
|
|
expect(mockSecretService.resolveAdapterConfigForRuntime).toHaveBeenCalledWith(
|
|
"company-1", {}, expect.objectContaining({ consumerType: "environment", consumerId: expectedId }),
|
|
expect.any(Object),
|
|
);
|
|
expect(testEnvironmentSpy).toHaveBeenCalledWith(expect.objectContaining({ executionTarget: target }));
|
|
});
|
|
|
|
it("keeps an explicit local override ahead of the instance sandbox default", async () => {
|
|
mockInstanceSettingsService.get.mockResolvedValue({ defaultEnvironmentId: "11111111-1111-4111-8111-111111111111" });
|
|
mockEnvironmentService.getById.mockResolvedValue({ id: "33333333-3333-4333-8333-333333333333", name: "Local", driver: "local", status: "active", config: {} });
|
|
const app = await createApp();
|
|
const res = await request(app)
|
|
.post("/api/companies/company-1/adapters/external_test/test-environment")
|
|
.send({ environmentId: "33333333-3333-4333-8333-333333333333" });
|
|
expect(res.status).toBe(200);
|
|
expect(mockInstanceSettingsService.get).not.toHaveBeenCalled();
|
|
expect(mockEnvironmentRuntime.acquireRunLease).not.toHaveBeenCalled();
|
|
expect(testEnvironmentSpy).toHaveBeenCalledWith(expect.objectContaining({ executionTarget: null }));
|
|
});
|
|
|
|
it("uses the managed sandbox when managed-only execution has no configured default", async () => {
|
|
mockInstanceSettingsService.getExperimental.mockResolvedValue({ enableManagedSandboxOnly: true });
|
|
mockEnvironmentService.findManagedSandboxEnvironment.mockResolvedValue({ id: "11111111-1111-4111-8111-111111111111" });
|
|
const target = { kind: "remote", transport: "sandbox", remoteCwd: "/workspace", providerKey: "daytona", runner: { execute: vi.fn() } };
|
|
mockResolveEnvironmentExecutionTarget.mockResolvedValue(target);
|
|
const app = await createApp();
|
|
const res = await request(app)
|
|
.post("/api/companies/company-1/adapters/external_test/test-environment")
|
|
.send({ environmentId: null });
|
|
expect(res.status, JSON.stringify(res.body)).toBe(200);
|
|
expect(testEnvironmentSpy).toHaveBeenCalledWith(expect.objectContaining({ executionTarget: target }));
|
|
});
|
|
|
|
it("checks the inherited environment's company before resolving credentials", async () => {
|
|
mockInstanceSettingsService.get.mockResolvedValue({ defaultEnvironmentId: "11111111-1111-4111-8111-111111111111" });
|
|
mockEnvironmentService.listBoundCompanyIds.mockResolvedValue(["company-2"]);
|
|
const app = await createApp();
|
|
const res = await request(app)
|
|
.post("/api/companies/company-1/adapters/external_test/test-environment")
|
|
.send({ adapterConfig: {} });
|
|
expect(res.status).toBe(403);
|
|
expect(mockSecretService.normalizeAdapterConfigForPersistence).not.toHaveBeenCalled();
|
|
expect(testEnvironmentSpy).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("never probes the host when an inherited environment is missing", async () => {
|
|
mockInstanceSettingsService.get.mockResolvedValue({ defaultEnvironmentId: "11111111-1111-4111-8111-111111111111" });
|
|
mockEnvironmentService.getById.mockResolvedValue(null);
|
|
const app = await createApp();
|
|
const res = await request(app)
|
|
.post("/api/companies/company-1/adapters/external_test/test-environment")
|
|
.send({ adapterConfig: {} });
|
|
expect(res.body.checks).toEqual([expect.objectContaining({ code: "environment_not_found" })]);
|
|
expect(testEnvironmentSpy).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("does not probe locally when managed-only execution has no default or sandbox", async () => {
|
|
mockInstanceSettingsService.getExperimental.mockResolvedValue({ enableManagedSandboxOnly: true });
|
|
const app = await createApp();
|
|
const res = await request(app)
|
|
.post("/api/companies/company-1/adapters/external_test/test-environment")
|
|
.send({ adapterConfig: {} });
|
|
expect(res.status).toBe(422);
|
|
expect(res.body.details.code).toBe("managed_sandbox_unavailable");
|
|
expect(testEnvironmentSpy).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("tests a prospective adapter switch before saving the agent", async () => {
|
|
mockAgentService.getById.mockResolvedValue({
|
|
id: "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa", companyId: "company-1",
|
|
adapterType: "codex_local", adapterConfig: {}, runtimeConfig: {},
|
|
});
|
|
const app = await createApp();
|
|
const res = await request(app)
|
|
.post("/api/companies/company-1/adapters/external_test/test-environment")
|
|
.send({ agentId: "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa", adapterConfig: { model: "prospective-model" } });
|
|
expect(res.status, JSON.stringify(res.body)).toBe(200);
|
|
expect(testEnvironmentSpy).toHaveBeenCalledWith(expect.objectContaining({ config: { model: "prospective-model" } }));
|
|
});
|
|
|
|
it("requires agent update permission to test a prospective adapter switch", async () => {
|
|
mockAgentService.getById.mockResolvedValue({ id: "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa", companyId: "company-1", adapterType: "codex_local", adapterConfig: {} });
|
|
mockAccessService.decide.mockImplementation(async ({ action }) => ({ allowed: action !== "agent_config:update", reason: "test", explanation: "Update denied" }));
|
|
const app = await createApp();
|
|
const res = await request(app)
|
|
.post("/api/companies/company-1/adapters/external_test/test-environment")
|
|
.send({ agentId: "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa", adapterConfig: {} });
|
|
expect(res.status).toBe(403);
|
|
expect(testEnvironmentSpy).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it.each(["CURSOR_API_KEY", "KIMI_MODEL_API_KEY", "ZAI_API_KEY", "KIMI_API_KEY", "MINIMAX_API_KEY"])("accepts %s as a probe-only credential", async (key) => {
|
|
const app = await createApp();
|
|
const res = await request(app)
|
|
.post("/api/companies/company-1/adapters/external_test/test-environment")
|
|
.send({ testCredentials: { [key]: "probe-only-key" } });
|
|
expect(res.status).toBe(200);
|
|
expect(mockSecretService.normalizeAdapterConfigForPersistence.mock.calls[0]?.[1]).toEqual({});
|
|
expect(testEnvironmentSpy.mock.calls[0]?.[0].config.env).toEqual({ [key]: "probe-only-key" });
|
|
});
|
|
|
|
it("maps the Hermes gateway probe key without passing it to persistence", async () => {
|
|
const { registerServerAdapter, getServerAdapter, unregisterServerAdapter } = await import("../adapters/index.js");
|
|
const previous = getServerAdapter("hermes_gateway");
|
|
unregisterServerAdapter("hermes_gateway");
|
|
registerServerAdapter({ ...externalAdapter, type: "hermes_gateway" });
|
|
try {
|
|
const app = await createApp();
|
|
const res = await request(app)
|
|
.post("/api/companies/company-1/adapters/hermes_gateway/test-environment")
|
|
.send({ adapterConfig: { apiBaseUrl: "https://hermes.example.com" }, testCredentials: { API_SERVER_KEY: "gateway-probe-key" } });
|
|
expect(res.status).toBe(200);
|
|
expect(mockSecretService.normalizeAdapterConfigForPersistence.mock.calls[0]?.[1]).toEqual({ apiBaseUrl: "https://hermes.example.com" });
|
|
expect(testEnvironmentSpy.mock.calls[0]?.[0].config.apiKey).toBe("gateway-probe-key");
|
|
expect(JSON.stringify(res.body)).not.toContain("gateway-probe-key");
|
|
} finally {
|
|
unregisterServerAdapter("hermes_gateway");
|
|
if (previous) registerServerAdapter(previous);
|
|
}
|
|
});
|
|
|
|
// The managed-adoption verdict, all three ways. An api_key account is
|
|
// re-verified against the provider's endpoint at adoption — the same check
|
|
// its save performed, catching a key revoked since — but never through the
|
|
// CLI-lane hello probe, which a clean machine without a provider CLI can
|
|
// never pass (the regression that walled off onboarding's API-key path in
|
|
// the nightly release smoke). A stored subscription login still needs the
|
|
// hello probe: only a real turn proves the runtime lane can consume it.
|
|
it("adopts an api_key connection on the engine's verdict plus a live key check, without a CLI hello probe", async () => {
|
|
mockManagedRuntime("api_key");
|
|
// A sentinel in claude_local's slot: the forced CLI-lane fallback would
|
|
// land here, so the fix is proven by this never being consulted — not by
|
|
// whatever verdict the host machine's real adapter would return.
|
|
const { registerServerAdapter, getServerAdapter, unregisterServerAdapter } = await import("../adapters/index.js");
|
|
const previous = getServerAdapter("claude_local");
|
|
unregisterServerAdapter("claude_local");
|
|
const cliProbeSpy = vi.fn(async () => ({
|
|
adapterType: "claude_local",
|
|
status: "fail" as const,
|
|
checks: [{ code: "adapter_command_missing", level: "error" as const, message: 'Command not found in PATH: "claude"' }],
|
|
testedAt: new Date(0).toISOString(),
|
|
}));
|
|
registerServerAdapter({ ...externalAdapter, type: "claude_local", testEnvironment: cliProbeSpy });
|
|
try {
|
|
const app = await createApp();
|
|
const res = await request(app)
|
|
.post("/api/companies/company-1/adapters/external_test/test-environment")
|
|
.send({
|
|
adapterConfig: { cwd: "/" },
|
|
aiConnection: { provider: "anthropic", method: "api_key", mode: "responsible_user" },
|
|
});
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.status).toBe("pass");
|
|
expect(JSON.stringify(res.body)).not.toContain("ai_connection_validation_incomplete");
|
|
expect(res.body.checks.map((check: { code: string }) => check.code)).toContain("ai_connection_api_key_reverified");
|
|
expect(mockValidateAiApiKey).toHaveBeenCalledWith("anthropic", "sk-ant-test-key");
|
|
expect(testEnvironmentSpy).toHaveBeenCalledTimes(1);
|
|
expect(cliProbeSpy).not.toHaveBeenCalled();
|
|
} finally {
|
|
unregisterServerAdapter("claude_local");
|
|
if (previous) registerServerAdapter(previous);
|
|
}
|
|
});
|
|
|
|
it("fails adoption of an api_key connection the provider no longer accepts", async () => {
|
|
mockManagedRuntime("api_key");
|
|
const { unprocessable } = await import("../errors.js");
|
|
mockValidateAiApiKey.mockRejectedValueOnce(unprocessable("The provider rejected this API key.", {
|
|
code: "ai_connection_api_key_rejected",
|
|
}));
|
|
const app = await createApp();
|
|
const res = await request(app)
|
|
.post("/api/companies/company-1/adapters/external_test/test-environment")
|
|
.send({
|
|
adapterConfig: { cwd: "/" },
|
|
aiConnection: { provider: "anthropic", method: "api_key", mode: "responsible_user" },
|
|
});
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.status).toBe("fail");
|
|
expect(res.body.checks.map((check: { code: string }) => check.code)).toContain("ai_connection_api_key_rejected");
|
|
expect(mockMarkAuthenticationFailed).toHaveBeenCalledWith(expect.objectContaining({
|
|
companyId: "company-1",
|
|
attribution: expect.objectContaining({ connectionId: "conn-1", grantId: "grant-1" }),
|
|
}));
|
|
});
|
|
|
|
it("still fails subscription adoption when no hello probe can run", async () => {
|
|
mockManagedRuntime("subscription");
|
|
const { registerServerAdapter, getServerAdapter, unregisterServerAdapter } = await import("../adapters/index.js");
|
|
const previous = getServerAdapter("claude_local");
|
|
unregisterServerAdapter("claude_local");
|
|
const cliProbeSpy = vi.fn(async () => ({
|
|
adapterType: "claude_local",
|
|
status: "fail" as const,
|
|
checks: [{ code: "adapter_command_missing", level: "error" as const, message: 'Command not found in PATH: "claude"' }],
|
|
testedAt: new Date(0).toISOString(),
|
|
}));
|
|
registerServerAdapter({ ...externalAdapter, type: "claude_local", testEnvironment: cliProbeSpy });
|
|
try {
|
|
const app = await createApp();
|
|
const res = await request(app)
|
|
.post("/api/companies/company-1/adapters/external_test/test-environment")
|
|
.send({
|
|
adapterConfig: { cwd: "/" },
|
|
aiConnection: { provider: "anthropic", method: "subscription", mode: "responsible_user" },
|
|
});
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.status).toBe("fail");
|
|
expect(res.body.checks.map((check: { code: string }) => check.code)).toContain("ai_connection_validation_incomplete");
|
|
expect(cliProbeSpy).toHaveBeenCalledTimes(1);
|
|
expect(cliProbeSpy.mock.calls[0]?.[0]?.config.engine).toBe("cli");
|
|
} finally {
|
|
unregisterServerAdapter("claude_local");
|
|
if (previous) registerServerAdapter(previous);
|
|
}
|
|
});
|
|
|
|
it("passes one-shot provider credentials only to the probe, never persistence normalization", async () => {
|
|
const app = await createApp();
|
|
const res = await request(app)
|
|
.post("/api/companies/company-1/adapters/external_test/test-environment")
|
|
.send({ adapterConfig: { env: { KEEP: "value" } }, testCredentials: { OPENROUTER_API_KEY: "probe-only-key" } });
|
|
expect(res.status).toBe(200);
|
|
expect(mockSecretService.normalizeAdapterConfigForPersistence.mock.calls[0]?.[1]).toEqual({ env: { KEEP: "value" } });
|
|
expect(testEnvironmentSpy.mock.calls[0]?.[0].config.env).toEqual({ KEEP: "value", OPENROUTER_API_KEY: "probe-only-key" });
|
|
expect(JSON.stringify(res.body)).not.toContain("probe-only-key");
|
|
});
|
|
|
|
it("rejects non-provider variables in one-shot credentials", async () => {
|
|
const app = await createApp();
|
|
const res = await request(app)
|
|
.post("/api/companies/company-1/adapters/external_test/test-environment")
|
|
.send({ testCredentials: { NODE_OPTIONS: "--require unsafe" } });
|
|
expect(res.status).toBe(400);
|
|
expect(testEnvironmentSpy).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("does not fall back to a host probe when a requested environment cannot produce an execution target", async () => {
|
|
const app = await createApp();
|
|
|
|
const res = await request(app)
|
|
.post("/api/companies/company-1/adapters/external_test/test-environment")
|
|
.send({
|
|
adapterConfig: {},
|
|
environmentId: "11111111-1111-4111-8111-111111111111",
|
|
});
|
|
|
|
expect(res.status, JSON.stringify(res.body)).toBe(200);
|
|
expect(testEnvironmentSpy).not.toHaveBeenCalled();
|
|
expect(res.body).toMatchObject({
|
|
adapterType: "external_test",
|
|
status: "warn",
|
|
checks: [
|
|
{
|
|
code: "environment_target_unsupported",
|
|
level: "warn",
|
|
message: 'Adapter "external_test" is not allowed in "Sandbox QA" environments.',
|
|
},
|
|
],
|
|
});
|
|
expect(mockReleaseRunLease).toHaveBeenCalledWith({
|
|
environment: expect.objectContaining({
|
|
id: "11111111-1111-4111-8111-111111111111",
|
|
name: "Sandbox QA",
|
|
driver: "sandbox",
|
|
}),
|
|
lease: expect.objectContaining({
|
|
id: "lease-1",
|
|
}),
|
|
status: "failed",
|
|
});
|
|
});
|
|
|
|
it("returns a diagnostic result instead of probing the host when the requested environment is missing", async () => {
|
|
// The route reads the environment more than once: the tenant-binding guard
|
|
// loads it, then the execution-context resolver loads it. Return null for
|
|
// every read so the missing-environment path is stable.
|
|
mockEnvironmentService.getById.mockResolvedValue(null);
|
|
const app = await createApp();
|
|
|
|
const res = await request(app)
|
|
.post("/api/companies/company-1/adapters/external_test/test-environment")
|
|
.send({
|
|
adapterConfig: {},
|
|
environmentId: "22222222-2222-4222-8222-222222222222",
|
|
});
|
|
|
|
expect(res.status, JSON.stringify(res.body)).toBe(200);
|
|
expect(testEnvironmentSpy).not.toHaveBeenCalled();
|
|
expect(mockEnvironmentRuntime.acquireRunLease).not.toHaveBeenCalled();
|
|
expect(res.body).toMatchObject({
|
|
adapterType: "external_test",
|
|
status: "warn",
|
|
checks: [
|
|
{
|
|
code: "environment_not_found",
|
|
level: "warn",
|
|
message: "Selected environment was not found. The test did not run.",
|
|
},
|
|
],
|
|
});
|
|
});
|
|
|
|
it("runs the adapter probe against the resolved sandbox target on the happy path and releases the lease on success", async () => {
|
|
mockResolveEnvironmentExecutionTarget.mockResolvedValueOnce({
|
|
kind: "remote",
|
|
transport: "sandbox",
|
|
remoteCwd: "/home/user/paperclip-workspace",
|
|
providerKey: "fake-plugin",
|
|
runner: { execute: vi.fn() },
|
|
});
|
|
testEnvironmentSpy.mockResolvedValueOnce({
|
|
adapterType: "external_test",
|
|
status: "pass",
|
|
checks: [
|
|
{
|
|
code: "external_test_hello_probe_passed",
|
|
level: "info",
|
|
message: "OK",
|
|
},
|
|
],
|
|
testedAt: new Date(0).toISOString(),
|
|
});
|
|
const app = await createApp();
|
|
|
|
const res = await request(app)
|
|
.post("/api/companies/company-1/adapters/external_test/test-environment")
|
|
.send({
|
|
adapterConfig: {},
|
|
environmentId: "11111111-1111-4111-8111-111111111111",
|
|
});
|
|
|
|
expect(res.status, JSON.stringify(res.body)).toBe(200);
|
|
expect(testEnvironmentSpy).toHaveBeenCalledTimes(1);
|
|
// Test leases boot fresh and stay debuggable: never resume a retained
|
|
// agent lease, archive (not delete) the sandbox on release.
|
|
expect(mockEnvironmentRuntime.acquireRunLease).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
applyCustomImageTemplate: true,
|
|
// The Test lease re-checks the company binding, so a binding change
|
|
// between the route guard and the lease cannot open a foreign sandbox.
|
|
assertCompanyBinding: true,
|
|
environment: expect.objectContaining({
|
|
config: expect.objectContaining({
|
|
reuseLease: false,
|
|
archiveOnRelease: true,
|
|
}),
|
|
}),
|
|
}),
|
|
);
|
|
expect(testEnvironmentSpy.mock.calls[0]?.[0]).toMatchObject({
|
|
executionTarget: expect.objectContaining({
|
|
kind: "remote",
|
|
transport: "sandbox",
|
|
}),
|
|
environmentName: "Sandbox QA",
|
|
});
|
|
expect(res.body).toMatchObject({ adapterType: "external_test", status: "pass" });
|
|
expect(res.body.checks).toEqual([
|
|
expect.objectContaining({
|
|
code: "sandbox_test_identity",
|
|
level: "info",
|
|
message: 'Environment test identity for "Sandbox QA".',
|
|
detail: expect.stringContaining("paperclipLeaseId=lease-1"),
|
|
}),
|
|
expect.objectContaining({
|
|
code: "external_test_hello_probe_passed",
|
|
level: "info",
|
|
message: "OK",
|
|
}),
|
|
]);
|
|
expect(res.body.checks[0].detail).toContain("providerLeaseId=provider-lease-1");
|
|
expect(res.body.checks[0].detail).toContain("provider=daytona");
|
|
expect(res.body.checks[0].detail).toContain("sandboxId=sandbox-1");
|
|
expect(res.body.checks[0].detail).toContain("sandboxName=paperclip-probe");
|
|
expect(res.body.checks[0].detail).toContain("snapshotRef=snapshot-1");
|
|
expect(mockReleaseRunLease).toHaveBeenCalledWith({
|
|
environment: expect.objectContaining({ id: "11111111-1111-4111-8111-111111111111" }),
|
|
lease: expect.objectContaining({ id: "lease-1" }),
|
|
status: "released",
|
|
});
|
|
});
|
|
|
|
it("releases the lease as failed and returns a diagnostic when realizeWorkspace throws", async () => {
|
|
mockEnvironmentRuntime.realizeWorkspace.mockRejectedValueOnce(
|
|
new Error("workspace realization failed"),
|
|
);
|
|
const app = await createApp();
|
|
|
|
const res = await request(app)
|
|
.post("/api/companies/company-1/adapters/external_test/test-environment")
|
|
.send({
|
|
adapterConfig: {},
|
|
environmentId: "11111111-1111-4111-8111-111111111111",
|
|
});
|
|
|
|
expect(res.status, JSON.stringify(res.body)).toBe(200);
|
|
expect(testEnvironmentSpy).not.toHaveBeenCalled();
|
|
expect(res.body).toMatchObject({
|
|
adapterType: "external_test",
|
|
status: "fail",
|
|
checks: [
|
|
expect.objectContaining({
|
|
code: "environment_workspace_realize_failed",
|
|
level: "error",
|
|
}),
|
|
],
|
|
});
|
|
expect(mockReleaseRunLease).toHaveBeenCalledWith({
|
|
environment: expect.objectContaining({ id: "11111111-1111-4111-8111-111111111111" }),
|
|
lease: expect.objectContaining({ id: "lease-1" }),
|
|
status: "failed",
|
|
});
|
|
});
|
|
|
|
describe("environment envVars merge", () => {
|
|
const environmentId = "11111111-1111-4111-8111-111111111111";
|
|
const sandboxExecutionTarget = {
|
|
kind: "remote",
|
|
transport: "sandbox",
|
|
remoteCwd: "/home/user/paperclip-workspace",
|
|
providerKey: "fake-plugin",
|
|
runner: { execute: vi.fn() },
|
|
};
|
|
|
|
it("merges resolved environment envVars under the agent adapterConfig env", async () => {
|
|
mockEnvironmentService.getById.mockResolvedValue({
|
|
id: environmentId,
|
|
companyId: "company-1",
|
|
name: "Sandbox QA",
|
|
driver: "sandbox",
|
|
config: { provider: "fake-plugin" },
|
|
envVars: {
|
|
CLAUDE_CODE_OAUTH_TOKEN: { type: "secret_ref", secretId: "secret-1" },
|
|
FOO: { type: "plain", value: "env-foo" },
|
|
PAPERCLIP_API_KEY: { type: "plain", value: "must-not-flow" },
|
|
},
|
|
});
|
|
mockResolveEnvironmentExecutionTarget.mockResolvedValueOnce(sandboxExecutionTarget);
|
|
mockSecretService.resolveEnvBindings.mockResolvedValueOnce({
|
|
env: { CLAUDE_CODE_OAUTH_TOKEN: "resolved-token", FOO: "env-foo" },
|
|
secretKeys: new Set(["CLAUDE_CODE_OAUTH_TOKEN"]),
|
|
manifest: [],
|
|
});
|
|
const app = await createApp();
|
|
|
|
const res = await request(app)
|
|
.post("/api/companies/company-1/adapters/external_test/test-environment")
|
|
.send({
|
|
adapterConfig: { env: { FOO: "agent-foo" } },
|
|
environmentId,
|
|
});
|
|
|
|
expect(res.status, JSON.stringify(res.body)).toBe(200);
|
|
expect(mockSecretService.resolveEnvBindings).toHaveBeenCalledWith(
|
|
"company-1",
|
|
{
|
|
CLAUDE_CODE_OAUTH_TOKEN: { type: "secret_ref", secretId: "secret-1" },
|
|
FOO: { type: "plain", value: "env-foo" },
|
|
},
|
|
expect.objectContaining({
|
|
consumerType: "environment",
|
|
consumerId: environmentId,
|
|
}),
|
|
);
|
|
expect(testEnvironmentSpy).toHaveBeenCalledTimes(1);
|
|
// Environment env is the base layer; the agent's own env wins on conflicts.
|
|
expect(testEnvironmentSpy.mock.calls[0]?.[0]?.config?.env).toEqual({
|
|
CLAUDE_CODE_OAUTH_TOKEN: "resolved-token",
|
|
FOO: "agent-foo",
|
|
});
|
|
expect(res.body.status).toBe("pass");
|
|
});
|
|
|
|
it("skips env vars with missing secret bindings and fails the test", async () => {
|
|
mockEnvironmentService.getById.mockResolvedValue({
|
|
id: environmentId,
|
|
companyId: "company-1",
|
|
name: "Sandbox QA",
|
|
driver: "sandbox",
|
|
config: { provider: "fake-plugin" },
|
|
envVars: {
|
|
MISSING_TOKEN: { type: "secret_ref", secretId: "secret-gone" },
|
|
GOOD: { type: "plain", value: "ok" },
|
|
},
|
|
});
|
|
mockResolveEnvironmentExecutionTarget.mockResolvedValueOnce(sandboxExecutionTarget);
|
|
mockSecretService.collectMissingRuntimeBindings.mockResolvedValueOnce([
|
|
{
|
|
consumerType: "environment",
|
|
consumerId: environmentId,
|
|
configPath: "env.MISSING_TOKEN",
|
|
envKey: "MISSING_TOKEN",
|
|
secretId: "secret-gone",
|
|
secretName: "Gone",
|
|
},
|
|
]);
|
|
mockSecretService.resolveEnvBindings.mockResolvedValueOnce({
|
|
env: { GOOD: "ok" },
|
|
secretKeys: new Set<string>(),
|
|
manifest: [],
|
|
});
|
|
const app = await createApp();
|
|
|
|
const res = await request(app)
|
|
.post("/api/companies/company-1/adapters/external_test/test-environment")
|
|
.send({ adapterConfig: {}, environmentId });
|
|
|
|
expect(res.status, JSON.stringify(res.body)).toBe(200);
|
|
// The unresolved key is excluded from resolution; the rest still flows.
|
|
expect(mockSecretService.resolveEnvBindings).toHaveBeenCalledWith(
|
|
"company-1",
|
|
{ GOOD: { type: "plain", value: "ok" } },
|
|
expect.objectContaining({ consumerType: "environment" }),
|
|
);
|
|
expect(testEnvironmentSpy).toHaveBeenCalledTimes(1);
|
|
expect(testEnvironmentSpy.mock.calls[0]?.[0]?.config?.env).toEqual({ GOOD: "ok" });
|
|
// A missing binding blocks real dispatch, so the test reports fail even
|
|
// though the adapter probe itself passed.
|
|
expect(res.body.status).toBe("fail");
|
|
expect(res.body.checks).toEqual(
|
|
expect.arrayContaining([
|
|
expect.objectContaining({
|
|
code: "environment_env_binding_missing",
|
|
level: "error",
|
|
message: expect.stringContaining("MISSING_TOKEN"),
|
|
}),
|
|
]),
|
|
);
|
|
});
|
|
|
|
it("includes environment env checks when the environment cannot produce an execution target", async () => {
|
|
mockEnvironmentService.getById.mockResolvedValue({
|
|
id: environmentId,
|
|
companyId: "company-1",
|
|
name: "Sandbox QA",
|
|
driver: "sandbox",
|
|
config: { provider: "fake-plugin" },
|
|
envVars: {
|
|
MISSING_TOKEN: { type: "secret_ref", secretId: "secret-gone" },
|
|
},
|
|
});
|
|
mockSecretService.collectMissingRuntimeBindings.mockResolvedValueOnce([
|
|
{
|
|
consumerType: "environment",
|
|
consumerId: environmentId,
|
|
configPath: "env.MISSING_TOKEN",
|
|
envKey: "MISSING_TOKEN",
|
|
secretId: "secret-gone",
|
|
secretName: "Gone",
|
|
},
|
|
]);
|
|
const app = await createApp();
|
|
|
|
const res = await request(app)
|
|
.post("/api/companies/company-1/adapters/external_test/test-environment")
|
|
.send({ adapterConfig: {}, environmentId });
|
|
|
|
expect(res.status, JSON.stringify(res.body)).toBe(200);
|
|
expect(testEnvironmentSpy).not.toHaveBeenCalled();
|
|
expect(res.body.status).toBe("fail");
|
|
expect(res.body.checks).toEqual([
|
|
expect.objectContaining({ code: "environment_target_unsupported", level: "warn" }),
|
|
expect.objectContaining({ code: "environment_env_binding_missing", level: "error" }),
|
|
]);
|
|
});
|
|
});
|
|
|
|
describe("tenant-binding guard", () => {
|
|
async function postForeignEnvironmentTest() {
|
|
const app = await createApp();
|
|
return request(app)
|
|
.post("/api/companies/company-1/adapters/external_test/test-environment")
|
|
.send({
|
|
adapterConfig: { env: { FOO: "bar" } },
|
|
environmentId: "11111111-1111-4111-8111-111111111111",
|
|
});
|
|
}
|
|
|
|
function expectCompanyMismatch(res: request.Response) {
|
|
expect(res.status, JSON.stringify(res.body)).toBe(403);
|
|
expect(JSON.stringify(res.body)).toContain("environment_company_mismatch");
|
|
// The guard rejects before any secret resolution, target resolution,
|
|
// sandbox lease, or adapter test runs.
|
|
expect(mockSecretService.normalizeAdapterConfigForPersistence).not.toHaveBeenCalled();
|
|
expect(mockSecretService.resolveAdapterConfigForRuntime).not.toHaveBeenCalled();
|
|
expect(mockEnvironmentRuntime.acquireRunLease).not.toHaveBeenCalled();
|
|
expect(testEnvironmentSpy).not.toHaveBeenCalled();
|
|
}
|
|
|
|
it("rejects an active environment bound to another company", async () => {
|
|
mockEnvironmentService.listBoundCompanyIds.mockResolvedValue(["company-2"]);
|
|
expectCompanyMismatch(await postForeignEnvironmentTest());
|
|
});
|
|
|
|
it("rejects an archived environment bound to another company without revealing its status", async () => {
|
|
mockEnvironmentService.getById.mockResolvedValue({
|
|
id: "11111111-1111-4111-8111-111111111111",
|
|
companyId: "company-2",
|
|
name: "Sandbox QA",
|
|
driver: "sandbox",
|
|
status: "archived",
|
|
config: { provider: "fake-plugin" },
|
|
});
|
|
mockEnvironmentService.listBoundCompanyIds.mockResolvedValue(["company-2"]);
|
|
expectCompanyMismatch(await postForeignEnvironmentTest());
|
|
});
|
|
|
|
it("rejects a disallowed-driver environment bound to another company without revealing its driver", async () => {
|
|
mockEnvironmentService.getById.mockResolvedValue({
|
|
id: "11111111-1111-4111-8111-111111111111",
|
|
companyId: "company-2",
|
|
name: "Plugin Env",
|
|
driver: "plugin",
|
|
status: "active",
|
|
config: {},
|
|
});
|
|
mockEnvironmentService.listBoundCompanyIds.mockResolvedValue(["company-2"]);
|
|
expectCompanyMismatch(await postForeignEnvironmentTest());
|
|
});
|
|
|
|
it("allows an instance-global environment with no company binding", async () => {
|
|
mockEnvironmentService.listBoundCompanyIds.mockResolvedValue([]);
|
|
const res = await postForeignEnvironmentTest();
|
|
// The guard passes and the route proceeds to secret resolution.
|
|
expect(res.status, JSON.stringify(res.body)).toBe(200);
|
|
expect(mockSecretService.normalizeAdapterConfigForPersistence).toHaveBeenCalled();
|
|
});
|
|
|
|
it("allows an environment bound to the caller company", async () => {
|
|
mockEnvironmentService.listBoundCompanyIds.mockResolvedValue(["company-1"]);
|
|
const res = await postForeignEnvironmentTest();
|
|
// The guard passes and the route proceeds to secret resolution.
|
|
expect(res.status, JSON.stringify(res.body)).toBe(200);
|
|
expect(mockSecretService.normalizeAdapterConfigForPersistence).toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe("managed-sandbox-only redirect", () => {
|
|
const localEnvironmentId = "33333333-3333-4333-8333-333333333333";
|
|
const managedSandboxEnvironment = {
|
|
id: "44444444-4444-4444-8444-444444444444",
|
|
companyId: null,
|
|
name: "Managed sandbox",
|
|
driver: "sandbox",
|
|
status: "active",
|
|
config: { provider: "fake-plugin" },
|
|
};
|
|
const localEnvironment = {
|
|
id: localEnvironmentId,
|
|
companyId: null,
|
|
name: "Local host",
|
|
driver: "local",
|
|
status: "active",
|
|
config: {},
|
|
};
|
|
|
|
it("redirects a local-environment Test onto the managed sandbox and never probes the host", async () => {
|
|
mockEnvironmentService.getById.mockResolvedValue(localEnvironment);
|
|
mockInstanceSettingsService.getExperimental.mockResolvedValue({
|
|
enableManagedSandboxOnly: true,
|
|
});
|
|
mockEnvironmentService.findManagedSandboxEnvironment.mockResolvedValue(
|
|
managedSandboxEnvironment,
|
|
);
|
|
mockResolveEnvironmentExecutionTarget.mockResolvedValueOnce({
|
|
kind: "remote",
|
|
transport: "sandbox",
|
|
remoteCwd: "/home/user/paperclip-workspace",
|
|
providerKey: "fake-plugin",
|
|
runner: { execute: vi.fn() },
|
|
});
|
|
const app = await createApp();
|
|
|
|
const res = await request(app)
|
|
.post("/api/companies/company-1/adapters/external_test/test-environment")
|
|
.send({ adapterConfig: {}, environmentId: localEnvironmentId });
|
|
|
|
expect(res.status, JSON.stringify(res.body)).toBe(200);
|
|
// The Test leases and probes the managed sandbox the real run uses, not
|
|
// the local host that the agent default still names.
|
|
expect(mockEnvironmentRuntime.acquireRunLease).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
environment: expect.objectContaining({ id: managedSandboxEnvironment.id }),
|
|
}),
|
|
);
|
|
expect(testEnvironmentSpy).toHaveBeenCalledTimes(1);
|
|
expect(testEnvironmentSpy.mock.calls[0]?.[0]).toMatchObject({
|
|
executionTarget: expect.objectContaining({ kind: "remote", transport: "sandbox" }),
|
|
environmentName: "Managed sandbox",
|
|
});
|
|
});
|
|
|
|
it("fails closed when the policy is on and no managed sandbox environment exists", async () => {
|
|
mockEnvironmentService.getById.mockResolvedValue(localEnvironment);
|
|
mockInstanceSettingsService.getExperimental.mockResolvedValue({
|
|
enableManagedSandboxOnly: true,
|
|
});
|
|
mockEnvironmentService.findManagedSandboxEnvironment.mockResolvedValue(null);
|
|
const app = await createApp();
|
|
|
|
const res = await request(app)
|
|
.post("/api/companies/company-1/adapters/external_test/test-environment")
|
|
.send({ adapterConfig: {}, environmentId: localEnvironmentId });
|
|
|
|
expect(res.status, JSON.stringify(res.body)).toBe(200);
|
|
// No fall back to a host probe: the Test reports fail-closed.
|
|
expect(testEnvironmentSpy).not.toHaveBeenCalled();
|
|
expect(mockEnvironmentRuntime.acquireRunLease).not.toHaveBeenCalled();
|
|
expect(res.body.status).toBe("fail");
|
|
expect(res.body.checks).toEqual([
|
|
expect.objectContaining({ code: "managed_sandbox_unavailable", level: "error" }),
|
|
]);
|
|
});
|
|
|
|
it("probes the local host when the managed-sandbox-only policy is off", async () => {
|
|
mockEnvironmentService.getById.mockResolvedValue(localEnvironment);
|
|
mockInstanceSettingsService.getExperimental.mockResolvedValue({
|
|
enableManagedSandboxOnly: false,
|
|
});
|
|
const app = await createApp();
|
|
|
|
const res = await request(app)
|
|
.post("/api/companies/company-1/adapters/external_test/test-environment")
|
|
.send({ adapterConfig: {}, environmentId: localEnvironmentId });
|
|
|
|
expect(res.status, JSON.stringify(res.body)).toBe(200);
|
|
// Legacy behavior: a local environment probes the host with no redirect
|
|
// and no sandbox lease.
|
|
expect(mockEnvironmentService.findManagedSandboxEnvironment).not.toHaveBeenCalled();
|
|
expect(mockEnvironmentRuntime.acquireRunLease).not.toHaveBeenCalled();
|
|
expect(testEnvironmentSpy).toHaveBeenCalledTimes(1);
|
|
expect(testEnvironmentSpy.mock.calls[0]?.[0]?.executionTarget ?? null).toBeNull();
|
|
});
|
|
});
|
|
});
|
|
|
|
vi.mock("../services/agent-lifecycle.js", async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import("../services/agent-lifecycle.js")>();
|
|
return { ...actual, createAgentLifecycle: () => ({
|
|
}) };
|
|
});
|