mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-10-23 00:53:38 +02:00
## Thinking Path > - Paperclip manages AI agents and their work. > - Agent creation, approval, pause, and termination change whether an agent can work. > - These changes currently use separate service and database paths. > - Some agents need setup after their record exists and before their harness can run. > - This PR adds one module that owns those state changes and their transactions. > - The module records progress and retries incomplete work after a server restart. ## Linked Issues or Issue Description **Problem or motivation** A hire can become active before its execution environment is ready. Direct agent state writes also bypass one common lifecycle boundary. Termination does not have a durable step for resource cleanup. **Proposed solution** Use agent lifecycle commands for hiring, approval, pause, resume, and termination. Keep proposed hires pending until approval. Prepare each approved hire, test its saved harness configuration, and then permit work. Wait for required plugin work before completing a step. **Alternatives considered** A general database commit hook would expose transaction details to callers. A timer alone would delay each new hire. This module owns each command transaction and starts its worker after commit. A periodic scan recovers work after a process stops. **Roadmap alignment** This change adds the lifecycle boundary for the existing agent and plugin systems. It does not add a user interface or a provider implementation. It replaces the lifecycle delivery approach in #15391. Related proposals include #11475 and #13099; this change does not add their external executor protocols. ## What Changed - Add durable approval, preparation, verification, pause, resume, stop, and cleanup states. - Keep agent creation, lifecycle state, configuration writes, and agent record deletion behind `modules/agent-lifecycle`. Keep validation, credential rules, reads, onboarding, invitations, general approvals, and company data deletion in their services. - Commit a hire before the remaining onboarding or invitation work. Serialize requests with the existing dedicated database connection helper. Reuse the hire after a failed attempt. - Keep accepted work in the run queue during setup. Require execution and cleanup evidence before completing pause or termination. - Retain the original credential owner when approving migrated hires. - Start lifecycle work after each command commits. Use database leases and revision checks for retries and late results. - Add a generic required plugin method and capability. Store the required plugin IDs for each agent. Track host and plugin completion separately. - Keep harness verification and credential rules in services. Supply their results to lifecycle through explicit ports. Reject failed logins and incomplete probes. - Remove reverse imports from lifecycle adapters into services. Share record queries and validation below both layers. - Use one hire decision path for agent IDs and approval IDs. Resolve the approval and agent change in one transaction. Expose policy reconciliation on the command object. - Save configuration and verification invalidation through one module operation. Ordinary commands own their transaction. Restrict the shared transaction integration to its listed callers. Reject identity, company, accounting, timestamp, and unknown fields. - Require completed termination before deleting agent records. The company deletion service calls the module through a separate deletion interface. - Save an approved secret before applying its agent binding. A failed binding leaves the secret approved. A retry reuses it. - Remove old permission and metadata export files. Move hire configuration rules out of lifecycle persistence. - Report failed worker steps and scans with operation context and known error codes. Omit provider messages, stacks, and configuration values. Document ordinary application use and extension rules. - Recover expired native owners only after execution stops. Retry cleanup of failed retained leases. Wait for setup in the live smoke script. - Keep manual, budget, and company pause reasons separate. Guard execution status writes with the ready state. - Add a migration, boundary checks, regression tests, and a lifecycle guide. Keep the current UI. - Limit periodic policy checks to 100 eligible agents per minute. Continue from the last agent ID. Exclude pending hires and agents in termination or a final state. Keep explicit company and budget updates immediate. ## Verification - Latest head `ccc3c86b0` aligns the shared status update schema with the PATCH route. All 15 shared tests, 104 server route/OpenAPI tests, 2 CLI tests, and shared/server/CLI TypeScript checks pass. The Greptile comment is fixed. CI is running; no new Apex review was requested for this small fix. - Rebase `75708ac94` rebases onto master at `2eb5187e4`. The backup test uses the shared 90-second timeout from master; the older timeout commit was removed. The affected backup test passes. - Agent budget edits now pass the target agent ID through enforcement. All 50 affected tests, server TypeScript, and 11 boundary tests pass. Both configuration update paths have regression coverage. CI is running on the latest head. No new Apex review was requested for these small fixes. - Rebase `9b568666a` preserves the run-control extraction and delivery-work startup changes from master. All 559 affected tests, server TypeScript, and 11 boundary tests pass. CI and Apex review are running on the final head. - The policy scan fix passes 56 lifecycle and diagnostic tests, 24 policy and caller tests, and 11 boundary tests. A regression test checks the page limit, interval, excluded states, and cursor wraparound. - Rebase `fd4698750` passes all 611 tests in eight affected lifecycle, route, and heartbeat suites. Server TypeScript and all 11 module boundary tests pass. The company deletion rollback and concurrent hire tests are included. - Follow-up `00aac7646` fixes the failed decision deletion test and the public update schema. All 126 affected server tests, 8 shared tests, 2 CLI tests, and 11 boundary tests pass. Shared, server, CLI, and UI TypeScript checks pass. - Server TypeScript passes: `pnpm --filter @paperclipai/server exec tsc --noEmit`. - All 11 boundary tests pass: `node --test scripts/check-module-boundaries.test.mjs`. The repository boundary scan also passes. - All 68 lifecycle, failure diagnostic, and harness verification tests pass. Tests reject forbidden fields through the root command, combined transition command, and restricted transaction integration. They also check invalid fields returned by configuration preparation. - Failure tests cover host work, plugin work, claims, renewals, deferrals, and scans. Log tests check known error codes and exclusion of private error contents. - All 355 tests in 10 existing caller suites pass. These tests cover secret approval, pairing, pending hires, credentials, company import, built-in agents, managed agents, instructions, and skills. - The branch is rebased onto `origin/master` at `2eb5187e4`. The queue guards and cancellation lock fix use the extracted heartbeat queue. Agent cancellation uses the extracted run-control file. The source diff scan found no secrets or private URLs. - Earlier checks cover approval retries, rollback, late verification results, policy holds, deletion rollback, concurrent company deletion, and queue admission during setup. - Before the rebase, CI passed on `00aac7646` and Greptile Apex rated it 5/5 with no open comments. CI and a new Apex review are pending for the rebased commit. The branch has no merge conflicts. - The full local build and native Dot suite were not run. The host has no Cargo. No live provider test was run. ## Risks - Background policy recovery takes more than one minute when more than 100 agents are eligible. Explicit company and budget updates still reconcile immediately. - The restricted configuration integration keeps an agent reference and its dependent records in one transaction. It cannot change a budget or invoke a lifecycle transition. A periodic scan starts pending setup after that transaction commits. - Secret approval and binding use separate transactions. A failed binding can leave an approved secret with no agent binding. - New approved hires remain unavailable until setup and verification complete. Existing clients see `paused` during these steps. This PR does not change the UI. - Migration `0320_daily_onslaught.sql` maps existing statuses and pause reasons. It does not create external resources or claim that an existing harness passed verification. - Use a coordinated server update for this migration. Older server versions can write legacy status without the new lifecycle checks. - A failed onboarding or invitation request can leave a committed hire. A retry reuses that hire and completes the remaining records. - Deletion requires completed termination. Company deletion can require another request while cleanup is pending. - Required plugins must make repeated calls safe and reject older revisions. A disabled or removed required plugin blocks completion until it is restored. - A new hire can run a second harness test after the existing UI test. Some adapters can charge for this test. - No live provider test was run. Provider implementation and resource backfill remain separate work. ## Model Used OpenAI GPT-6 through Codex, with reasoning, code tools, and local test execution. ## 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 described the issue in this PR with the feature request field labels - [x] I have not referenced internal or instance-local issues or links - [x] My branch name describes the change - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation - [x] I have considered and documented risks - [x] Targeted local tests pass - [x] All Paperclip CI gates are green on the new commit - [x] Greptile Apex is 5/5 with no open comments on the new commit - [x] I will address review comments before requesting merge --------- Co-authored-by: Paperclip <noreply@paperclip.ing>
534 lines
23 KiB
TypeScript
534 lines
23 KiB
TypeScript
import type { Db } from "@paperclipai/db";
|
|
import type { AdapterExecutionTarget } from "@paperclipai/adapter-utils/execution-target";
|
|
import type { AdapterEnvironmentCheck } from "@paperclipai/adapter-utils";
|
|
import { parseObject } from "@paperclipai/adapter-utils/server-utils";
|
|
import { aiRoutingHarness, ADAPTER_AUTH_MISSING_CHECK_CODE, AI_CONNECTION_CAPABILITIES, type AiConnectionBinding } from "@paperclipai/shared";
|
|
import { requireServerAdapter } from "../adapters/index.js";
|
|
import { forbidden, HttpError, unprocessable } from "../errors.js";
|
|
import { instanceSettingsService } from "./instance-settings.js";
|
|
import { environmentService } from "./environments.js";
|
|
import { environmentRuntimeService } from "./environment-runtime.js";
|
|
import { resolveEnvironmentExecutionTarget } from "./environment-execution-target.js";
|
|
import { prepareManagedAiRuntime, assertManagedAiProjectAuth } from "./ai-connection-runtime.js";
|
|
import { aiConnectionService } from "./ai-connections.js";
|
|
import { validateAiApiKey } from "./ai-api-key-test.js";
|
|
import type { PluginWorkerManager } from "./plugin-worker-manager.js";
|
|
import { assertEnvironmentSelectionForCompany } from "./environment-selection.js";
|
|
const asRecord = (value: unknown): Record<string, unknown> | null => value && typeof value === "object" && !Array.isArray(value) ? value as Record<string, unknown> : null;
|
|
export function agentEnvironmentTestService(db: Db, pluginWorkerManager?: PluginWorkerManager) {
|
|
const instanceSettings = instanceSettingsService(db);
|
|
const environmentsSvc = environmentService(db);
|
|
const environmentRuntime = environmentRuntimeService(db, { pluginWorkerManager });
|
|
// A null agent override inherits the instance default, just like dispatch.
|
|
// Resolve this before secrets or probes so a default remote environment can
|
|
// never accidentally validate the account on the control-plane host.
|
|
async function resolveAdapterTestEnvironmentId(companyId: string, environmentId: string | null | undefined) {
|
|
if (environmentId) return environmentId;
|
|
const settings = await instanceSettings.get();
|
|
if (settings.defaultEnvironmentId) return settings.defaultEnvironmentId;
|
|
if ((await instanceSettings.getExperimental()).enableManagedSandboxOnly === true) {
|
|
const managed = await environmentsSvc.findManagedSandboxEnvironment(companyId);
|
|
if (!managed) {
|
|
throw unprocessable("The managed sandbox is unavailable. Restore Paperclip Computer and retry.", {
|
|
code: "managed_sandbox_unavailable",
|
|
});
|
|
}
|
|
return managed.id;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Resolve the execution target the adapter should run its test probes against.
|
|
*
|
|
* - No environmentId / local environment → returns a local target so the
|
|
* adapter probes the Paperclip host (legacy behavior).
|
|
* - SSH environment → builds an SSH execution target from the environment
|
|
* config so the adapter probes the remote box. No lease is required:
|
|
* the SSH spec is fully derived from the saved environment config.
|
|
* - Sandbox / plugin environments → acquires an ad-hoc lease, realizes the
|
|
* workspace, and resolves a sandbox execution target wired to the runtime
|
|
* so the adapter probe runs inside the sandbox the same way a heartbeat
|
|
* would. The returned `release` callback rolls the lease back when the
|
|
* route is done.
|
|
*
|
|
* The caller MUST always invoke `release()` (typically in a `finally` block).
|
|
*/
|
|
async function resolveAdapterTestExecutionContext(input: {
|
|
agentId?: string;
|
|
companyId: string;
|
|
adapterType: string;
|
|
environmentId: string | null;
|
|
}): Promise<{
|
|
executionTarget: AdapterExecutionTarget | null;
|
|
environmentName: string | null;
|
|
fallbackChecks: AdapterEnvironmentCheck[];
|
|
sandboxIdentityCheck?: AdapterEnvironmentCheck | null;
|
|
release: (status?: "released" | "failed") => Promise<void>;
|
|
}> {
|
|
const noopRelease = async () => {};
|
|
|
|
if (!input.environmentId) {
|
|
return {
|
|
executionTarget: null,
|
|
environmentName: null,
|
|
fallbackChecks: [],
|
|
release: noopRelease,
|
|
};
|
|
}
|
|
|
|
const requestedEnvironment = await environmentsSvc.getById(input.environmentId);
|
|
if (!requestedEnvironment) {
|
|
return {
|
|
executionTarget: null,
|
|
environmentName: null,
|
|
fallbackChecks: [
|
|
{
|
|
code: "environment_not_found",
|
|
level: "warn",
|
|
message: "Selected environment was not found. The test did not run.",
|
|
},
|
|
],
|
|
release: noopRelease,
|
|
};
|
|
}
|
|
|
|
// Managed-sandbox-only policy: redirect a Test that would run on the local
|
|
// host onto the platform-managed sandbox, the same as a real run does
|
|
// (resolveExecutionWorkspaceEnvironmentId in heartbeat). Without this
|
|
// redirect the Test probes the local host while the run executes in the
|
|
// managed sandbox, so a passing Test validates the wrong execution target.
|
|
// With no active managed sandbox the Test fails closed — never local.
|
|
let environment = requestedEnvironment;
|
|
if (requestedEnvironment.driver === "local") {
|
|
const managedSandboxOnly =
|
|
(await instanceSettings.getExperimental()).enableManagedSandboxOnly === true;
|
|
if (managedSandboxOnly) {
|
|
const managedSandboxEnvironment = await environmentsSvc.findManagedSandboxEnvironment(
|
|
input.companyId,
|
|
);
|
|
if (!managedSandboxEnvironment) {
|
|
return {
|
|
executionTarget: null,
|
|
environmentName: requestedEnvironment.name,
|
|
fallbackChecks: [
|
|
{
|
|
code: "managed_sandbox_unavailable",
|
|
level: "error",
|
|
message:
|
|
"This instance runs agents only in its platform-managed sandbox, but no active managed sandbox environment exists. The test did not run.",
|
|
hint: "Restore the managed sandbox environment, then test again.",
|
|
},
|
|
],
|
|
release: noopRelease,
|
|
};
|
|
}
|
|
environment = managedSandboxEnvironment;
|
|
}
|
|
}
|
|
|
|
if (environment.driver === "local") {
|
|
return {
|
|
executionTarget: null,
|
|
environmentName: environment.name,
|
|
fallbackChecks: [],
|
|
release: noopRelease,
|
|
};
|
|
}
|
|
|
|
if (environment.driver === "ssh") {
|
|
try {
|
|
const target = await resolveEnvironmentExecutionTarget({
|
|
db,
|
|
companyId: input.companyId,
|
|
adapterType: input.adapterType,
|
|
environment: {
|
|
id: environment.id,
|
|
driver: environment.driver,
|
|
config: environment.config ?? null,
|
|
},
|
|
leaseMetadata: null,
|
|
});
|
|
if (target) {
|
|
return {
|
|
executionTarget: target,
|
|
environmentName: environment.name,
|
|
fallbackChecks: [],
|
|
release: noopRelease,
|
|
};
|
|
}
|
|
return {
|
|
executionTarget: null,
|
|
environmentName: environment.name,
|
|
fallbackChecks: [
|
|
{
|
|
code: "environment_target_unavailable",
|
|
level: "warn",
|
|
message:
|
|
`Could not resolve an execution target for environment "${environment.name}". The test did not run.`,
|
|
},
|
|
],
|
|
release: noopRelease,
|
|
};
|
|
} catch (err) {
|
|
return {
|
|
executionTarget: null,
|
|
environmentName: environment.name,
|
|
fallbackChecks: [
|
|
{
|
|
code: "environment_target_failed",
|
|
level: "warn",
|
|
message:
|
|
`Could not connect to environment "${environment.name}" to run the test.`,
|
|
detail: err instanceof Error ? err.message : String(err),
|
|
},
|
|
],
|
|
release: noopRelease,
|
|
};
|
|
}
|
|
}
|
|
|
|
// sandbox / plugin / other remote drivers: spin up an ad-hoc lease, realize
|
|
// the workspace inside the box, and run the same probe SSH uses against
|
|
// a sandbox execution target wired to the environment runtime.
|
|
//
|
|
// We pass `heartbeatRunId: null` because there's no heartbeat run for an
|
|
// operator-initiated `Test` invocation — the leases table FKs heartbeat
|
|
// run id to heartbeat_runs.id, and we don't want to manufacture a fake
|
|
// run row. Cleanup goes through the driver's `releaseRunLease` directly
|
|
// (by lease record), since the batch helper queries by heartbeatRunId.
|
|
//
|
|
// Sandbox tests boot a fresh throwaway sandbox (never resume a retained
|
|
// agent lease) and archive it on release instead of deleting it, so the
|
|
// operator can inspect the exact sandbox from the provider dashboard while
|
|
// provider-side expiry reaps it later.
|
|
const testEnvironment = environment.driver === "sandbox"
|
|
? {
|
|
...environment,
|
|
config: {
|
|
...(environment.config ?? {}),
|
|
reuseLease: false,
|
|
archiveOnRelease: true,
|
|
},
|
|
}
|
|
: environment;
|
|
let leaseRecord: Awaited<ReturnType<typeof environmentRuntime.acquireRunLease>>;
|
|
try {
|
|
leaseRecord = await environmentRuntime.acquireRunLease({
|
|
companyId: input.companyId,
|
|
agentId: input.agentId,
|
|
environment: testEnvironment,
|
|
issueId: null,
|
|
heartbeatRunId: null,
|
|
persistedExecutionWorkspace: null,
|
|
// Re-check the company binding atomically at lease time. The route
|
|
// guard already rejected a foreign environment, but the binding could
|
|
// change between the guard check and the lease acquire. This closes
|
|
// that check-to-lease race so a foreign sandbox never gets a lease.
|
|
assertCompanyBinding: true,
|
|
// Apply the active custom-image template so the Test boots with the
|
|
// operator's captured sandbox customizations and prepared image state,
|
|
// matching what real agent runs use. Without this the test would
|
|
// silently fall back to the base image.
|
|
applyCustomImageTemplate: true,
|
|
});
|
|
} catch (err) {
|
|
return {
|
|
executionTarget: null,
|
|
environmentName: environment.name,
|
|
fallbackChecks: [
|
|
{
|
|
code: "environment_lease_acquire_failed",
|
|
level: "error",
|
|
message: `Could not acquire a lease for environment "${environment.name}".`,
|
|
detail: err instanceof Error ? err.message : String(err),
|
|
hint: "Check the environment's provider credentials and quota.",
|
|
},
|
|
],
|
|
release: noopRelease,
|
|
};
|
|
}
|
|
|
|
const driver = environmentRuntime.getDriver(environment.driver);
|
|
const releaseLease = async (status: "released" | "failed" = "released") => {
|
|
try {
|
|
if (driver) {
|
|
await driver.releaseRunLease({
|
|
environment: testEnvironment,
|
|
lease: leaseRecord.lease,
|
|
status,
|
|
});
|
|
} else {
|
|
await environmentsSvc.releaseLease(leaseRecord.lease.id, status);
|
|
}
|
|
} catch (err) {
|
|
// Cleanup failures must not mask the test result.
|
|
// eslint-disable-next-line no-console
|
|
console.warn(
|
|
`[adapter-test] Failed to release lease ${leaseRecord.lease.id}: ${err instanceof Error ? err.message : String(err)}`,
|
|
);
|
|
}
|
|
};
|
|
|
|
let realizedCwd: string | null = null;
|
|
try {
|
|
const realized = await environmentRuntime.realizeWorkspace({
|
|
environment: testEnvironment,
|
|
lease: leaseRecord.lease,
|
|
// No host workspace to copy for a Test invocation; sandbox/plugin
|
|
// realize implementations use the lease metadata's remoteCwd to
|
|
// create the working directory inside the box.
|
|
workspace: {},
|
|
});
|
|
realizedCwd =
|
|
typeof realized.cwd === "string" && realized.cwd.trim().length > 0
|
|
? realized.cwd.trim()
|
|
: null;
|
|
} catch (err) {
|
|
await releaseLease("failed");
|
|
return {
|
|
executionTarget: null,
|
|
environmentName: environment.name,
|
|
fallbackChecks: [
|
|
{
|
|
code: "environment_workspace_realize_failed",
|
|
level: "error",
|
|
message: `Could not realize a workspace inside "${environment.name}".`,
|
|
detail: err instanceof Error ? err.message : String(err),
|
|
},
|
|
],
|
|
release: noopRelease,
|
|
};
|
|
}
|
|
|
|
let target: AdapterExecutionTarget | null;
|
|
try {
|
|
// Prefer the cwd the realize step returned; fall back to lease metadata.
|
|
const leaseMetadataForTarget: Record<string, unknown> | null =
|
|
realizedCwd
|
|
? { ...(leaseRecord.lease.metadata ?? {}), remoteCwd: realizedCwd }
|
|
: (leaseRecord.lease.metadata as Record<string, unknown> | null) ?? null;
|
|
|
|
target = await resolveEnvironmentExecutionTarget({
|
|
db,
|
|
companyId: input.companyId,
|
|
adapterType: input.adapterType,
|
|
environment: {
|
|
id: testEnvironment.id,
|
|
driver: testEnvironment.driver,
|
|
config: testEnvironment.config ?? null,
|
|
},
|
|
leaseId: leaseRecord.lease.id,
|
|
leaseMetadata: leaseMetadataForTarget,
|
|
lease: leaseRecord.lease,
|
|
environmentRuntime,
|
|
});
|
|
} catch (err) {
|
|
await releaseLease("failed");
|
|
return {
|
|
executionTarget: null,
|
|
environmentName: environment.name,
|
|
fallbackChecks: [
|
|
{
|
|
code: "environment_target_failed",
|
|
level: "error",
|
|
message: `Could not resolve an execution target for "${environment.name}".`,
|
|
detail: err instanceof Error ? err.message : String(err),
|
|
},
|
|
],
|
|
release: noopRelease,
|
|
};
|
|
}
|
|
|
|
if (!target) {
|
|
await releaseLease("failed");
|
|
return {
|
|
executionTarget: null,
|
|
environmentName: environment.name,
|
|
fallbackChecks: [
|
|
{
|
|
code: "environment_target_unsupported",
|
|
level: "warn",
|
|
message:
|
|
`Adapter "${input.adapterType}" is not allowed in "${environment.name}" environments.`,
|
|
},
|
|
],
|
|
release: noopRelease,
|
|
};
|
|
}
|
|
|
|
return {
|
|
executionTarget: target,
|
|
environmentName: environment.name,
|
|
fallbackChecks: [],
|
|
sandboxIdentityCheck: buildSandboxIdentityCheck({
|
|
environmentName: environment.name,
|
|
lease: leaseRecord.lease,
|
|
}),
|
|
release: releaseLease,
|
|
};
|
|
}
|
|
|
|
function readMetadataString(metadata: Record<string, unknown>, keys: string[]): string | null {
|
|
for (const key of keys) {
|
|
const value = metadata[key];
|
|
if (typeof value === "string" && value.trim().length > 0) return value.trim();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function buildSandboxIdentityCheck(input: {
|
|
environmentName: string;
|
|
lease: {
|
|
id: string;
|
|
provider?: string | null;
|
|
providerLeaseId?: string | null;
|
|
metadata?: Record<string, unknown> | null;
|
|
};
|
|
}): AdapterEnvironmentCheck {
|
|
const metadata = input.lease.metadata ?? {};
|
|
const provider = input.lease.provider ?? readMetadataString(metadata, ["provider"]);
|
|
const sandboxId = readMetadataString(metadata, ["sandboxId", "sandboxID", "sandbox_id", "id"]);
|
|
const sandboxName = readMetadataString(metadata, ["sandboxName", "sandbox_name", "name"]);
|
|
const snapshotRef = readMetadataString(metadata, [
|
|
"snapshot",
|
|
"snapshotId",
|
|
"snapshotID",
|
|
"snapshotRef",
|
|
"snapshot_ref",
|
|
"templateRef",
|
|
"template_ref",
|
|
"templateId",
|
|
"templateID",
|
|
"image",
|
|
"imageId",
|
|
"imageID",
|
|
"imageRef",
|
|
"image_ref",
|
|
]);
|
|
const templateKind = readMetadataString(metadata, [
|
|
"templateKind",
|
|
"template_kind",
|
|
"templateRefKind",
|
|
"template_ref_kind",
|
|
]);
|
|
const detailParts = [
|
|
`paperclipLeaseId=${input.lease.id}`,
|
|
input.lease.providerLeaseId ? `providerLeaseId=${input.lease.providerLeaseId}` : null,
|
|
provider ? `provider=${provider}` : null,
|
|
sandboxId ? `sandboxId=${sandboxId}` : null,
|
|
sandboxName ? `sandboxName=${sandboxName}` : null,
|
|
snapshotRef ? `${templateKind ? `${templateKind}Ref` : "snapshotOrTemplateRef"}=${snapshotRef}` : null,
|
|
].filter((part): part is string => Boolean(part));
|
|
|
|
return {
|
|
code: "sandbox_test_identity",
|
|
level: "info",
|
|
message: `Environment test identity for "${input.environmentName}".`,
|
|
detail: detailParts.join("; "),
|
|
hint: "Use these provider-neutral IDs when comparing model-test output with provider logs or refreshed environment snapshots.",
|
|
};
|
|
}
|
|
|
|
|
|
// The environment drivers the adapter Test route accepts. A local, SSH, or
|
|
// sandbox environment can host a probe; a plugin environment cannot.
|
|
const ADAPTER_TEST_ALLOWED_ENVIRONMENT_DRIVERS = ["local", "ssh", "sandbox"];
|
|
|
|
// The fail-closed tenant-binding guard for the adapter Test route. A caller
|
|
// may name any instance environment by id, so the route must reject an
|
|
// environment that binds to another company before it resolves secrets,
|
|
// merges env, resolves the target, leases a sandbox, or runs the adapter
|
|
// test. The guard checks the company binding BEFORE it validates the status
|
|
// or the driver, so it never reveals the status or the driver of a foreign
|
|
// environment. A same-company or an instance-global environment then gets the
|
|
// shared driver and status validation.
|
|
async function assertAdapterTestEnvironmentForCompany(
|
|
companyId: string,
|
|
environmentId: string,
|
|
): Promise<void> {
|
|
const environment = await environmentsSvc.getById(environmentId);
|
|
if (!environment) {
|
|
// A missing environment leaks no tenant state. The execution-context
|
|
// resolver surfaces the existing environment_not_found check.
|
|
return;
|
|
}
|
|
const boundCompanyIds = await environmentsSvc.listBoundCompanyIds(environmentId);
|
|
if (boundCompanyIds.length > 0 && !boundCompanyIds.includes(companyId)) {
|
|
throw forbidden("The selected environment belongs to another company.", {
|
|
code: "environment_company_mismatch",
|
|
});
|
|
}
|
|
await assertEnvironmentSelectionForCompany(environmentsSvc, companyId, environmentId, {
|
|
allowedDrivers: ADAPTER_TEST_ALLOWED_ENVIRONMENT_DRIVERS,
|
|
});
|
|
}
|
|
|
|
async function testManagedEnvironment(adapterType: string, context: Parameters<ReturnType<typeof requireServerAdapter>["testEnvironment"]>[0], binding: AiConnectionBinding, managed: Awaited<ReturnType<typeof prepareManagedAiRuntime>>, agentId?: string) {
|
|
const startedAt = new Date();
|
|
const result = await probeManagedEnvironment(adapterType, context, binding);
|
|
// A provider rejection invalidates the tested credential generation. A
|
|
// missing CLI, unavailable environment, or other runtime error does not.
|
|
if (result.status === "fail" && result.checks.some(check =>
|
|
check.code === ADAPTER_AUTH_MISSING_CHECK_CODE || /_hello_probe_auth_required$/.test(check.code)
|
|
|| check.code === "ai_connection_api_key_rejected",
|
|
)) {
|
|
await aiConnectionService(db).markAuthenticationFailed({
|
|
companyId: context.companyId, agentId, runStartedAt: startedAt,
|
|
attribution: { ...managed.attribution, identity: managed.identity },
|
|
});
|
|
}
|
|
return result;
|
|
}
|
|
|
|
async function probeManagedEnvironment(adapterType: string, context: Parameters<ReturnType<typeof requireServerAdapter>["testEnvironment"]>[0], binding: AiConnectionBinding) {
|
|
await assertManagedAiProjectAuth(context.config, binding.provider, context.executionTarget);
|
|
const result = await requireServerAdapter(adapterType).testEnvironment(context);
|
|
if (result.status === "fail") return result;
|
|
// The resolved method, not binding.method — on a responsible_user binding
|
|
// that field is wire-compat only and the default connection decides.
|
|
const resolvedMethod = (context.config as { managedAiConnection?: { method?: string } }).managedAiConnection?.method;
|
|
// An api_key account does not take the CLI hello probe below: a key
|
|
// travels as an env var any engine understands, and the engine's own test
|
|
// above judged whether this runtime can execute with it — the ACP lane
|
|
// deliberately runs no hello probe when a key is configured. Demanding one
|
|
// anyway forced the CLI lane, whose probe needs a provider CLI on PATH,
|
|
// and a clean install has none: that walled off onboarding's API-key path
|
|
// on exactly the machines the release smoke exists to guard. The account
|
|
// is still verified live here — the same provider-endpoint check the save
|
|
// performed — so a key revoked since its save fails adoption rather than
|
|
// producing an agent that cannot authenticate at runtime. The hello-probe
|
|
// requirement stays for subscriptions: a stored login is a file layout
|
|
// only a provider CLI reads, so proving the runtime lane can consume it
|
|
// takes a real hello turn.
|
|
if (resolvedMethod === "api_key" && !context.config.managedAiRouting) {
|
|
const envKey = AI_CONNECTION_CAPABILITIES[binding.provider].methods.api_key?.envKey;
|
|
const key = envKey ? parseObject(context.config.env)[envKey] : undefined;
|
|
try {
|
|
if (typeof key !== "string" || !key) throw unprocessable("The selected account's API key was not available to verify.");
|
|
await validateAiApiKey(binding.provider, key);
|
|
result.checks.push({ code: "ai_connection_api_key_reverified", level: "info", message: "The provider verified this API key for adoption." });
|
|
} catch (error) {
|
|
result.status = "fail";
|
|
const rejected = error instanceof HttpError && asRecord(error.details)?.code === "ai_connection_api_key_rejected";
|
|
result.checks.push({ code: rejected ? "ai_connection_api_key_rejected" : "ai_connection_verification_failed", level: "error", message: error instanceof HttpError ? error.message : "Could not verify the account. Try again." });
|
|
}
|
|
return result;
|
|
}
|
|
if (!result.checks.some(check => check.code.includes("hello_probe"))) {
|
|
const providerAdapter = context.config.managedAiRouting ? aiRoutingHarness(adapterType, context.config.provider, context.config.acpxAgent) : { anthropic: "claude_local", openai: "codex_local", openrouter: "opencode_local", xai: "grok_local", google: "gemini_local" }[binding.provider];
|
|
const probe = await requireServerAdapter(providerAdapter).testEnvironment({ ...context, adapterType: providerAdapter, config: { ...context.config, engine: "cli" } });
|
|
result.checks.push(...probe.checks);
|
|
result.status = probe.status === "fail" ? "fail" : result.status === "warn" || probe.status === "warn" ? "warn" : "pass";
|
|
}
|
|
if (!result.checks.some(check => /hello_probe_(passed|succeeded)$/.test(check.code))) {
|
|
result.status = "fail";
|
|
result.checks.push({ code: "ai_connection_validation_incomplete", level: "error", message: "The selected account has not completed a provider hello test. Retry before adopting it." });
|
|
}
|
|
return result;
|
|
}
|
|
|
|
|
|
return { resolveAdapterTestEnvironmentId, resolveAdapterTestExecutionContext, assertAdapterTestEnvironmentForCompany, testManagedEnvironment };
|
|
}
|