diff --git a/doc/plugins/PLUGIN_ENVIRONMENT_TASKS.md b/doc/plugins/PLUGIN_ENVIRONMENT_TASKS.md index b3d50a724c3..ae680b573ed 100644 --- a/doc/plugins/PLUGIN_ENVIRONMENT_TASKS.md +++ b/doc/plugins/PLUGIN_ENVIRONMENT_TASKS.md @@ -69,6 +69,34 @@ authorized project IDs, and optional launch limits. It can also contain a saved task ID for session reuse and a typed Codex or Claude credential. Core does not supply an executable command or a guest path. +## Configured environment + +An agent can have environment variables from its execution environment, its +own configuration, its project, and its routine. Core resolves secret +references for each run. It sends the result only to a provider whose +`description` result has `configuredEnvironmentVersion: 1`. An older provider +gets no new fields, and the run continues without the variables. Core writes a +run log line that names the variables it did not set. + +- `submit` has `configuredEnvironment`. Set the variables in the Runner's + process environment. Add their names to `PAPERCLIP_CONFIGURED_ENV_KEYS`. The + Runner then gives them to every provider process. +- If a variable has the same name as a variable that the provider sets for its + own launch, use the provider's value. Do not fail the task. +- The values are transient secret input. Do not save them in task state, lease + metadata, journals, or logs. +- A retained Runner keeps the environment that it started with. Core includes + the values in its process reuse key. When a value changes, Core starts a new + task. It does not resume the retained Runner. +- A session handoff starts a new Runner, so its `resume` has + `configuredEnvironment`. + +A driver can declare `protectedEnvironmentKeys` in the manifest. These are the +variables that the driver always sets on its tasks. Core rejects a protected +variable in the configuration of an agent or environment that uses the driver. +In other places, such as a project or routine, Core saves the variable and +returns a `protected_environment_variable` notice that names the environments. + ## Release and resume `release` ends Core's active use of an exact, settled Runner session. Core calls diff --git a/docs/api/agents.md b/docs/api/agents.md index 511caee35bc..ce510a4b5d3 100644 --- a/docs/api/agents.md +++ b/docs/api/agents.md @@ -88,6 +88,21 @@ PATCH /api/agents/{agentId} } ``` +### Protected environment variables + +A plugin environment driver can declare `protectedEnvironmentKeys`: variables +that it sets on its own tasks. Create, hire, update and configuration rollback +check `adapterConfig.env` against them: + +- If the agent's environment (its `defaultEnvironmentId`, or the instance + default when it has none) uses a driver that protects a key, the request + fails with `422` and code `protected_environment_variable`. The response + details name the `keys` and the `environmentId`. +- If another active environment protects the key, the save succeeds. The + response includes `configurationWarnings` entries with code + `protected_environment_variable`, a `field` such as `adapterConfig.env.KEY`, + and a `message` that names those environments. + ## Pause Agent ``` diff --git a/docs/api/goals-and-projects.md b/docs/api/goals-and-projects.md index e508f997a72..86b59d83cb2 100644 --- a/docs/api/goals-and-projects.md +++ b/docs/api/goals-and-projects.md @@ -109,6 +109,13 @@ Workspace storage is fixed at creation. A change of `executionWorkspacePolicy.storageProviderPluginId` returns `409` (`project_storage_locked`). Runs use the agent's environment, not a project setting. +When `env` contains a variable that an active plugin environment sets on its +own tasks (`protectedEnvironmentKeys`), create and update still save it. The +response then includes `configurationWarnings` entries with code +`protected_environment_variable`, a `field` such as `env.KEY`, and a `message` +that names those environments. Agents that run there may see the environment's +value instead. + ## Project Workspaces Workspaces link a project to a repository and directory: diff --git a/docs/api/routines.md b/docs/api/routines.md index 8e2815bfdfb..6a2d208ae81 100644 --- a/docs/api/routines.md +++ b/docs/api/routines.md @@ -89,6 +89,12 @@ PATCH /api/routines/{routineId} All fields from create are updatable. `baseRevisionId` is optional for backward compatibility; when provided, stale values return `409 Conflict` with the current revision id. **Agents can only update routines assigned to themselves and cannot reassign a routine to another agent.** +When `env` contains a variable that an active plugin environment sets on its +own tasks (`protectedEnvironmentKeys`), create and update still save it. The +response then includes `configurationWarnings` entries with code +`protected_environment_variable`, a `field` such as `env.KEY`, and a `message` +that names those environments. + ## List Revisions ``` diff --git a/packages/paperclip-runner/runner/crates/runner-core/src/codex_provider.rs b/packages/paperclip-runner/runner/crates/runner-core/src/codex_provider.rs index dece2dd0d50..afec93b4f3d 100644 --- a/packages/paperclip-runner/runner/crates/runner-core/src/codex_provider.rs +++ b/packages/paperclip-runner/runner/crates/runner-core/src/codex_provider.rs @@ -30,26 +30,26 @@ pub(crate) fn runtime_notification_codex_args( paths: &[PathBuf; 4], ) -> Vec { let prefix = "shell_environment_policy.include_only="; - let Some(keys) = args.iter().rev().find_map(|arg| arg.strip_prefix(prefix)) else { - return vec![]; - }; - let Ok(mut keys) = serde_json::from_str::>(keys) else { - return vec![]; - }; - for key in [ - crate::runtime_notifications::CONTEXT_ENV, - crate::runtime_notifications::HELPER_ENV, - ] { - if !keys.iter().any(|existing| existing == key) { - keys.push(key.into()); + let mut extra = Vec::new(); + if let Some(keys) = args.iter().rev().find_map(|arg| arg.strip_prefix(prefix)) { + let Ok(mut keys) = serde_json::from_str::>(keys) else { + return vec![]; + }; + for key in [ + crate::runtime_notifications::CONTEXT_ENV, + crate::runtime_notifications::HELPER_ENV, + ] { + if !keys.iter().any(|existing| existing == key) { + keys.push(key.into()); + } } + extra.extend([ + "-c".into(), + format!("{prefix}{}", serde_json::to_string(&keys).unwrap()), + "-c".into(), + "shell_environment_policy.inherit=\"all\"".into(), + ]); } - let mut extra = vec![ - "-c".into(), - format!("{prefix}{}", serde_json::to_string(&keys).unwrap()), - "-c".into(), - "shell_environment_policy.inherit=\"all\"".into(), - ]; for profile in BTreeSet::from([profile, "paperclip-runner-workspace-read-only"]) { let prefix = format!("permissions.{profile}.filesystem="); let Some(value) = args.iter().rev().find_map(|arg| arg.strip_prefix(&prefix)) else { @@ -5200,6 +5200,48 @@ mod tests { assert!(!proves(page(json!([{"status": "completed"}]), Value::Null))); } + #[test] + #[cfg(unix)] + fn runtime_notification_grants_file_access_without_an_environment_allowlist() { + let profile = "paperclip-runner-workspace-read-only"; + let args = vec![ + "shell_environment_policy.inherit=\"all\"".to_string(), + "shell_environment_policy.exclude=[\"OPENAI_API_KEY\"]".to_string(), + format!("permissions.{profile}.filesystem={{\":root\"=\"none\"}}"), + ]; + let paths = [ + PathBuf::from("/runtime/runnerd"), + PathBuf::from("/state/private/runtime-notifications.json"), + PathBuf::from("/tmp/prn-channel/activity.lock"), + PathBuf::from("/tmp/prn-channel/activity.fifo"), + ]; + let extra = runtime_notification_codex_args(&args, profile, &paths); + assert!(!extra + .iter() + .any(|arg| arg.starts_with("shell_environment_policy.include_only="))); + let prefix = format!("permissions.{profile}.filesystem="); + let value = extra + .iter() + .find_map(|arg| arg.strip_prefix(&prefix)) + .unwrap(); + let config = format!("filesystem={value}") + .parse::() + .unwrap(); + let filesystem = config["filesystem"].as_inline_table().unwrap(); + assert_eq!( + filesystem + .get("/tmp/prn-channel") + .and_then(toml_edit::Value::as_str), + Some("write") + ); + assert_eq!( + filesystem + .get("/runtime/runnerd") + .and_then(toml_edit::Value::as_str), + Some("read") + ); + } + #[test] #[cfg(unix)] fn runtime_notification_policy_grants_only_discovery_files_and_ipc() { diff --git a/packages/paperclip-runner/src/drivers/codex/codex-security-config.test.ts b/packages/paperclip-runner/src/drivers/codex/codex-security-config.test.ts index 4adb6dccfd0..177609ccf7d 100644 --- a/packages/paperclip-runner/src/drivers/codex/codex-security-config.test.ts +++ b/packages/paperclip-runner/src/drivers/codex/codex-security-config.test.ts @@ -1,6 +1,6 @@ import { createRequire } from "node:module"; import { execFileSync } from "node:child_process"; -import { mkdtempSync, rmSync } from "node:fs"; +import { mkdtempSync, readFileSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { dirname, join, resolve } from "node:path"; import { resolvePinnedCodexCommand } from "./codex-command.js"; @@ -8,6 +8,7 @@ import { evalProviderTransportOptions } from "../../cli/eval-provider-runtime.js import { describe, expect, it } from "vitest"; import { + CODEX_PROVIDER_CREDENTIAL_ENVIRONMENT_KEYS, CODEX_UPDATE_PLAN_TOOL_CONFIG, codexExecutableReadOnlyRoots, codexNetworkReadOnlyRoots, @@ -509,6 +510,37 @@ describe("Codex security configuration", () => { }); }); + it("lets externally sandboxed commands inherit the Runner environment except Codex credentials", () => { + const source = { PAPERCLIP_RUNNER_EXTERNAL_SANDBOX: "1", PAPERCLIP_CONFIGURED_ENV_KEYS: '["STRIPE_SECRET_KEY"]', STRIPE_SECRET_KEY: "fixture-secret-value" }; + const args = createIsolatedCodexAppServerArgs(source); + const serialized = args.join("\n"); + expect(serialized).not.toContain("shell_environment_policy.include_only="); + expect(args).toContain('shell_environment_policy.inherit="all"'); + expect(args).toContain("shell_environment_policy.ignore_default_excludes=true"); + const exclude = JSON.parse(args.find(arg => arg.startsWith("shell_environment_policy.exclude="))!.split("=").slice(1).join("=")); + expect(exclude).toEqual([...CODEX_PROVIDER_CREDENTIAL_ENVIRONMENT_KEYS]); + expect(serialized).not.toContain("fixture-secret-value"); + expect(createIsolatedCodexAppServerArgs({ PAPERCLIP_CONFIGURED_ENV_KEYS: '["STRIPE_SECRET_KEY"]', STRIPE_SECRET_KEY: "fixture-secret-value" }).join("\n")) + .toContain('shell_environment_policy.include_only=["STRIPE_SECRET_KEY"]'); + }); + + it("excludes every credential the Runner passes to Codex", () => { + const source = readFileSync(resolve(import.meta.dirname, "../../../runner/crates/runner-core/src/codex_provider.rs"), "utf8"); + const block = /const CODEX_PROVIDER_ENVIRONMENT_KEYS: &\[&str\] = &\[([^\]]*)\];/u.exec(source)?.[1]; + expect(block).toBeDefined(); + const runnerKeys = [...block!.matchAll(/"([A-Z0-9_]+)"/gu)].map(match => match[1]); + const commandVisible = new Set([ + "OTEL_EXPORTER_OTLP_LOGS_TIMEOUT", "OTEL_EXPORTER_OTLP_TRACES_TIMEOUT", + "PAPERCLIP_AGENT_KEY_ID", "PAPERCLIP_AGENT_PUBLIC_KEY", "PAPERCLIP_AGENT_PRIVATE_KEY", + "PAPERCLIP_RUNNER_HOME_MODE", "XDG_CONFIG_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", "CODEX_HOME", + "PAPERCLIP_RUNNER_EXTERNAL_SANDBOX", + ]); + expect(runnerKeys.length).toBeGreaterThan(0); + for (const key of runnerKeys) { + expect(commandVisible.has(key) || CODEX_PROVIDER_CREDENTIAL_ENVIRONMENT_KEYS.includes(key), `${key} must be classified`).toBe(true); + } + }); + it("uses the outer sandbox for default-mode commands only when the controller authorizes it", () => { const source = { PAPERCLIP_RUNNER_EXTERNAL_SANDBOX: "1", PAPERCLIP_RUNNER_NETWORK_ACCESS: "enabled" }; const externalArgs = createIsolatedCodexAppServerArgs(source); diff --git a/packages/paperclip-runner/src/drivers/codex/codex-security-config.ts b/packages/paperclip-runner/src/drivers/codex/codex-security-config.ts index 98d51aa1cc9..8e91cefac71 100644 --- a/packages/paperclip-runner/src/drivers/codex/codex-security-config.ts +++ b/packages/paperclip-runner/src/drivers/codex/codex-security-config.ts @@ -178,6 +178,11 @@ function gitFilesystemRoots(source: NodeJS.ProcessEnv): { read: string[]; write: return { read: [...new Set(read)], write: [...new Set(write)] }; } +export const CODEX_PROVIDER_CREDENTIAL_ENVIRONMENT_KEYS: readonly string[] = [ + "OPENAI_API_KEY", "CODEX_API_KEY", "PAPERCLIP_AI_PROVIDER_KEY", + "OTEL_EXPORTER_OTLP_LOGS_HEADERS", "OTEL_EXPORTER_OTLP_TRACES_HEADERS", +]; + export function usesExternalRunnerSandbox(source: NodeJS.ProcessEnv): boolean { return source.PAPERCLIP_RUNNER_EXTERNAL_SANDBOX === "1"; } @@ -361,15 +366,16 @@ export function createIsolatedCodexAppServerArgs( `permissions.${CODEX_PLANNING_PERMISSION_PROFILE}.filesystem={${planningFilesystemRules}}`, "-c", `permissions.${CODEX_PLANNING_PERMISSION_PROFILE}.network.enabled=${networkAccess}`, - "-c", - `shell_environment_policy.inherit=${tomlString(hasProjectedEnvironment ? "all" : "none")}`, - "-c", - `shell_environment_policy.ignore_default_excludes=${hasProjectedEnvironment}`, - // Codex applies include_only after inheritance. Always emit the bounded - // allowlist: neither host mode nor a broker enables ambient secret access. - // Keep values in the process environment, never in argv/config diagnostics. - "-c", - `shell_environment_policy.include_only=${JSON.stringify(shellEnvironmentKeys)}`, + ...(externalRunnerSandbox + ? ["-c", 'shell_environment_policy.inherit="all"', + "-c", "shell_environment_policy.ignore_default_excludes=true", + "-c", `shell_environment_policy.exclude=${JSON.stringify(CODEX_PROVIDER_CREDENTIAL_ENVIRONMENT_KEYS)}`] + : ["-c", `shell_environment_policy.inherit=${tomlString(hasProjectedEnvironment ? "all" : "none")}`, + "-c", `shell_environment_policy.ignore_default_excludes=${hasProjectedEnvironment}`, + // Codex applies include_only after inheritance. Always emit the bounded + // allowlist: neither host mode nor a broker enables ambient secret access. + // Keep values in the process environment, never in argv/config diagnostics. + "-c", `shell_environment_policy.include_only=${JSON.stringify(shellEnvironmentKeys)}`]), ...(commandEnv.length > 0 ? ["-c", `shell_environment_policy.set={${commandEnv}}`] : []), diff --git a/packages/plugins/sdk/src/environment-tasks.ts b/packages/plugins/sdk/src/environment-tasks.ts index de583d80918..318915dd6f2 100644 --- a/packages/plugins/sdk/src/environment-tasks.ts +++ b/packages/plugins/sdk/src/environment-tasks.ts @@ -28,6 +28,34 @@ const githubBroker = z.object({ url: z.string().url().max(2000).refine(value => const claudeCredentialEnvironment = z.object({ CLAUDE_CODE_OAUTH_TOKEN: credentialValue.optional(), ANTHROPIC_API_KEY: credentialValue.optional(), }).strict().refine(env => Object.values(env).filter(Boolean).length === 1, "Select one Claude credential"); +export const MAX_CONFIGURED_ENVIRONMENT_KEYS = 128; +export const MAX_CONFIGURED_ENVIRONMENT_ENTRY_BYTES = 65_536; +export const MAX_CONFIGURED_ENVIRONMENT_BYTES = 262_144; +const utf8 = new TextEncoder(); +const utf8Bytes = (value: string) => utf8.encode(value).byteLength; +export const environmentTaskConfiguredEnvironmentSchema = z.record( + z.string().regex(/^[A-Za-z_][A-Za-z0-9_]{0,127}$/), + z.string().refine(value => !value.includes("\0"), "Expected a NUL-free value"), +).superRefine((environment, ctx) => { + const entries = Object.entries(environment); + if (entries.length > MAX_CONFIGURED_ENVIRONMENT_KEYS) { + ctx.addIssue({ code: z.ZodIssueCode.custom, message: "Too many configured environment variables" }); + return; + } + let total = 0; + for (const [name, value] of entries) { + const size = utf8Bytes(name) + utf8Bytes(value); + total += size; + if (size > MAX_CONFIGURED_ENVIRONMENT_ENTRY_BYTES) { + ctx.addIssue({ code: z.ZodIssueCode.custom, message: "Configured environment variable is too large", path: [name] }); + } + } + if (total > MAX_CONFIGURED_ENVIRONMENT_BYTES) { + ctx.addIssue({ code: z.ZodIssueCode.custom, message: "Configured environment is too large" }); + } +}); +export type PluginEnvironmentTaskConfiguredEnvironment = z.infer; + /** Only provider authentication is transferred. Paths and executable settings remain provider-owned. */ export const environmentTaskCredentialSchema = z.discriminatedUnion("type", [ z.object({ type: z.literal("codex-auth-json"), authJson: credentialValue }).strict(), @@ -89,6 +117,7 @@ export const environmentTaskOperationSchema = z.discriminatedUnion("kind", [ /** Stable account selection; distinct from its current credential revision. */ accountIdentity: z.string().min(1).max(1024).optional(), credential: environmentTaskCredentialSchema.optional(), + configuredEnvironment: environmentTaskConfiguredEnvironmentSchema.optional(), /** Core supplies these protected launch inputs for native agent runs. */ launch: z.object({ runnerVersion: z.string().min(1), runnerDigest: z.string().regex(/^sha256:[a-f0-9]{64}$/), @@ -117,6 +146,7 @@ export const environmentTaskOperationSchema = z.discriminatedUnion("kind", [ z.object({ kind: z.literal("release"), identity: durableIdentity }).strict(), z.object({ kind: z.literal("resume"), identity: durableIdentity, bootstrapTicket: credentialValue, handoffCredential: z.object({ accountIdentity: z.string().min(1).max(1024), environment: claudeCredentialEnvironment }).strict().optional(), + configuredEnvironment: environmentTaskConfiguredEnvironmentSchema.optional(), }).strict(), z.object({ kind: z.literal("ensure_ownership"), @@ -172,6 +202,7 @@ export const environmentTaskResultSchema = z.discriminatedUnion("kind", [ projectAuthCheckVersion: z.literal(1).optional(), workspaceFileReadVersion: z.literal(1).optional(), githubCredentialBrokerVersion: z.literal(1).optional(), + configuredEnvironmentVersion: z.literal(1).optional(), }).strict(), z.object({ kind: z.literal("status"), taskId: providerId, diff --git a/packages/plugins/sdk/src/index.ts b/packages/plugins/sdk/src/index.ts index 4e573c18119..06137786115 100644 --- a/packages/plugins/sdk/src/index.ts +++ b/packages/plugins/sdk/src/index.ts @@ -489,8 +489,8 @@ export type { PluginEnvironmentTaskErrorDiagnostic } from "./environment-task-er export type { AiConnectionPool, AiConnectionPoolConfig, AiConnectionPoolMember, AiConnectionRouterRequest, AiConnectionRouterResult, AiConnectionRouterSelection } from "@paperclipai/shared"; -export { MAX_ENVIRONMENT_TASK_FILE_BYTES, environmentRunnerLaunchRequirementsSchema, environmentTaskOperationSchema, environmentTaskResultSchema, environmentTaskCredentialSchema, parseEnvironmentTaskResult } from "./environment-tasks.js"; -export type { PluginEnvironmentTaskOperation, PluginEnvironmentTaskParams, PluginEnvironmentTaskResult, PluginEnvironmentTaskCredential, PluginEnvironmentTaskExecutionContext } from "./environment-tasks.js"; +export { MAX_ENVIRONMENT_TASK_FILE_BYTES, environmentRunnerLaunchRequirementsSchema, environmentTaskOperationSchema, environmentTaskResultSchema, environmentTaskCredentialSchema, environmentTaskConfiguredEnvironmentSchema, MAX_CONFIGURED_ENVIRONMENT_KEYS, MAX_CONFIGURED_ENVIRONMENT_ENTRY_BYTES, MAX_CONFIGURED_ENVIRONMENT_BYTES, parseEnvironmentTaskResult } from "./environment-tasks.js"; +export type { PluginEnvironmentTaskOperation, PluginEnvironmentTaskParams, PluginEnvironmentTaskResult, PluginEnvironmentTaskCredential, PluginEnvironmentTaskConfiguredEnvironment, PluginEnvironmentTaskExecutionContext } from "./environment-tasks.js"; export type { AgentLifecycleRequest, AgentLifecycleResult } from "@paperclipai/shared"; export type { PluginEnvironmentRunnerLaunchRequirements, PluginEnvironmentPreparedLaunch } from "./environment-tasks.js"; diff --git a/packages/plugins/sdk/tests/environment-tasks.test.ts b/packages/plugins/sdk/tests/environment-tasks.test.ts index 43521e36ae7..bd52cb8ccc7 100644 --- a/packages/plugins/sdk/tests/environment-tasks.test.ts +++ b/packages/plugins/sdk/tests/environment-tasks.test.ts @@ -96,3 +96,31 @@ describe("implementation-independent GitHub credential broker", () => { url: "https://core.example.test", token: "runtime-capability", script: "run-this" } }).success).toBe(false); }); }); + +describe("configured environment", () => { + const configured = { ...submit, configuredEnvironment: { STRIPE_SECRET_KEY: "fixture-value", EMPTY_OK: "" } }; + it("accepts bounded variables on submit and on a handoff resume", () => { + expect(environmentTaskOperationSchema.parse(configured)).toEqual(configured); + const identity = { runnerInstanceId: "runner", environmentLeaseId: "lease", runId: "run", normalizedSessionId: "session", turnId: "turn", itemId: "item" }; + const resume = { kind: "resume", identity, bootstrapTicket: "synthetic", configuredEnvironment: { A: "1" } }; + expect(environmentTaskOperationSchema.parse(resume)).toEqual(resume); + }); + it.each([ + ["an invalid name", { "1BAD": "x" }], + ["a dashed name", { "BAD-NAME": "x" }], + ["a NUL byte", { GOOD: "a\0b" }], + ["too many variables", Object.fromEntries(Array.from({ length: 129 }, (_, index) => [`K${index}`, "x"]))], + ["an oversized value", { BIG: "x".repeat(65_536) }], + ["an oversized total", Object.fromEntries(Array.from({ length: 5 }, (_, index) => [`K${index}`, "x".repeat(60_000)]))], + ])("rejects %s", (_label, configuredEnvironment) => { + expect(environmentTaskOperationSchema.safeParse({ ...configured, configuredEnvironment }).success).toBe(false); + }); + it("has no digest field", () => { + expect(environmentTaskOperationSchema.safeParse({ ...configured, configuredEnvironmentDigest: `hmac-sha256:${"b".repeat(64)}` }).success).toBe(false); + }); + it("lets a provider advertise support", () => { + const description = { kind: "description", taskId: "task", runner: { version: "fixture", digest: `sha256:${"a".repeat(64)}` }, configuredEnvironmentVersion: 1 }; + expect(environmentTaskResultSchema.parse(description)).toEqual(description); + expect(environmentTaskResultSchema.safeParse({ ...description, configuredEnvironmentVersion: 2 }).success).toBe(false); + }); +}); diff --git a/packages/shared/src/runner-configuration-warnings.ts b/packages/shared/src/runner-configuration-warnings.ts index 81db25b1b3a..7d8db761ee5 100644 --- a/packages/shared/src/runner-configuration-warnings.ts +++ b/packages/shared/src/runner-configuration-warnings.ts @@ -1,5 +1,5 @@ export interface AgentConfigurationWarning { - code: "runner_option_ignored"; + code: "runner_option_ignored" | "protected_environment_variable"; field: string; message: string; } diff --git a/packages/shared/src/types/plugin.ts b/packages/shared/src/types/plugin.ts index 22e1e35307f..d009ee12f56 100644 --- a/packages/shared/src/types/plugin.ts +++ b/packages/shared/src/types/plugin.ts @@ -202,6 +202,7 @@ export interface PluginEnvironmentDriverDeclaration { /** Codex task retention and authorized run attachment, version 1. */ warmTaskVersion?: 1; sessionHandoffVersion?: 1; + protectedEnvironmentKeys?: string[]; /** Stable driver key, unique within the plugin. Namespaced by plugin ID at runtime. */ driverKey: string; /** diff --git a/packages/shared/src/types/project.ts b/packages/shared/src/types/project.ts index 78eeea3694c..50b9d9c51cb 100644 --- a/packages/shared/src/types/project.ts +++ b/packages/shared/src/types/project.ts @@ -129,6 +129,7 @@ export interface Project { id: string; companyId: string; urlKey: string; + configurationWarnings?: import("../runner-configuration-warnings.js").AgentConfigurationWarning[]; /** @deprecated Use goalIds / goals instead */ goalId: string | null; goalIds: string[]; diff --git a/packages/shared/src/types/routine.ts b/packages/shared/src/types/routine.ts index e5e5a93d632..850fdeb686d 100644 --- a/packages/shared/src/types/routine.ts +++ b/packages/shared/src/types/routine.ts @@ -73,6 +73,7 @@ export interface Routine { id: string; companyId: string; projectId: string | null; + configurationWarnings?: import("../runner-configuration-warnings.js").AgentConfigurationWarning[]; folderId?: string | null; goalId: string | null; parentIssueId: string | null; diff --git a/packages/shared/src/validators/plugin.test.ts b/packages/shared/src/validators/plugin.test.ts index 23bc443850d..d125614df25 100644 --- a/packages/shared/src/validators/plugin.test.ts +++ b/packages/shared/src/validators/plugin.test.ts @@ -55,6 +55,13 @@ describe("plugin manifest validators", () => { } expect(pluginManifestV1Schema.safeParse({ ...manifest, capabilities: ["environment.drivers.register"] }).success).toBe(false); }); + it("accepts protected environment keys only as variable names", () => { + expect(pluginManifestV1Schema.parse(buildSandboxProviderManifest({ protectedEnvironmentKeys: ["DESKTOP_SESSION"] })) + .environmentDrivers?.[0]?.protectedEnvironmentKeys).toEqual(["DESKTOP_SESSION"]); + for (const keys of [["BAD-NAME"], ["1BAD"], "DESKTOP_SESSION", Array.from({ length: 65 }, (_, index) => `K${index}`)]) { + expect(pluginManifestV1Schema.safeParse(buildSandboxProviderManifest({ protectedEnvironmentKeys: keys })).success).toBe(false); + } + }); it("retains the agent prerequisite on environment drivers", () => { const manifest = { ...buildSandboxProviderManifest({ requiresAgent: true }), agentLifecycle: true, capabilities: ["environment.drivers.register", "agents.lifecycle.manage"] }; diff --git a/packages/shared/src/validators/plugin.ts b/packages/shared/src/validators/plugin.ts index 782e0515677..37dd518f4ad 100644 --- a/packages/shared/src/validators/plugin.ts +++ b/packages/shared/src/validators/plugin.ts @@ -184,6 +184,7 @@ export const pluginEnvironmentDriverDeclarationSchema = z.object({ creationOperationVersion: z.literal(1).optional(), warmTaskVersion: z.literal(1).optional(), sessionHandoffVersion: z.literal(1).optional(), + protectedEnvironmentKeys: z.array(z.string().regex(/^[A-Za-z_][A-Za-z0-9_]{0,127}$/)).max(64).optional(), driverKey: z.string().min(1).regex( /^[a-z0-9][a-z0-9._-]*$/, "Environment driver key must start with a lowercase alphanumeric and contain only lowercase letters, digits, dots, hyphens, or underscores", diff --git a/server/src/__tests__/agent-adapter-validation-routes.test.ts b/server/src/__tests__/agent-adapter-validation-routes.test.ts index 446465b75f9..bd0c194715f 100644 --- a/server/src/__tests__/agent-adapter-validation-routes.test.ts +++ b/server/src/__tests__/agent-adapter-validation-routes.test.ts @@ -65,6 +65,7 @@ const mockApprovalService = vi.hoisted(() => ({ getById: vi.fn(), })); +const mockCheckProtectedKeys = vi.hoisted(() => vi.fn(async (..._args: unknown[]) => [] as unknown[])); const mockInstanceSettingsService = vi.hoisted(() => ({ get: vi.fn(async () => ({ defaultEnvironmentId: null })), getGeneral: vi.fn(async () => ({ censorUsernameInLogs: false })), @@ -117,6 +118,10 @@ vi.mock("../services/remote-agent-profiles.js", () => ({ })); function registerModuleMocks() { + vi.doMock("../services/protected-environment-keys.js", async () => ({ + ...await vi.importActual("../services/protected-environment-keys.js"), + checkAgentProtectedEnvironmentKeys: mockCheckProtectedKeys, + })); vi.doMock("../services/antigravity-connection-models.js", () => ({ antigravityConnectionModels: mockAntigravityModels })); vi.doMock("../services/index.js", () => ({ agentService: () => mockAgentService, @@ -269,6 +274,8 @@ describe("agent routes adapter validation", () => { mockLogActivity.mockResolvedValue(undefined); mockSecretService.syncEnvBindingsForTarget.mockResolvedValue(undefined); mockInstanceSettingsService.get.mockResolvedValue({ defaultEnvironmentId: null }); + mockCheckProtectedKeys.mockReset(); + mockCheckProtectedKeys.mockResolvedValue([]); mockInstanceSettingsService.getExperimental.mockResolvedValue({ enableNativeRunner: false }); mockManagedAgentProfileService.requireQualified.mockResolvedValue({ id: "managed-primary", @@ -1772,6 +1779,34 @@ describe("agent routes adapter validation", () => { expect(mockAgentService.rollbackConfigRevision).not.toHaveBeenCalled(); }); + it.each(["rejected", "warned"] as const)("checks a rollback's restored env and environment for protected keys (%s)", async outcome => { + const existing = await mockAgentService.getById(); + const adapterConfig = { ...(existing.adapterConfig as Record), env: { DESKTOP_SESSION: { type: "plain", value: "mine" } } }; + mockAgentService.getConfigRevision.mockResolvedValue({ afterConfig: { + name: existing.name, role: existing.role, adapterType: existing.adapterType, adapterConfig, + runtimeConfig: {}, defaultEnvironmentId: "computer-env", budgetMonthlyCents: existing.budgetMonthlyCents, + } }); + mockAgentService.rollbackConfigRevision.mockResolvedValue({ ...existing, adapterConfig }); + const warning = { code: "protected_environment_variable", field: "adapterConfig.env.DESKTOP_SESSION", message: "notice" }; + if (outcome === "rejected") { + const { unprocessable } = await import("../errors.js"); + mockCheckProtectedKeys.mockRejectedValue(unprocessable("protected", { code: "protected_environment_variable" })); + } else mockCheckProtectedKeys.mockResolvedValue([warning]); + const app = await createApp(); + const response = await requestApp(app, (baseUrl) => + request(baseUrl).post("/api/agents/11111111-1111-4111-8111-111111111111/config-revisions/33333333-3333-4333-8333-333333333333/rollback").send({}), + ); + expect(mockCheckProtectedKeys).toHaveBeenCalledWith(expect.anything(), expect.objectContaining({ env: adapterConfig.env }), "computer-env"); + if (outcome === "rejected") { + expect(response.status, JSON.stringify(response.body)).toBe(422); + expect(response.body.details).toMatchObject({ code: "protected_environment_variable" }); + expect(mockAgentService.rollbackConfigRevision).not.toHaveBeenCalled(); + } else { + expect(response.status, JSON.stringify(response.body)).toBe(200); + expect(response.body.configurationWarnings).toEqual([warning]); + } + }); + it("keeps an existing paperclip_runner agent editable after the flag is disabled", async () => { const existing = await mockAgentService.getById(); mockAgentService.getById.mockResolvedValue({ diff --git a/server/src/__tests__/project-routes-env.test.ts b/server/src/__tests__/project-routes-env.test.ts index 4877bdff41b..c7bb3349bc5 100644 --- a/server/src/__tests__/project-routes-env.test.ts +++ b/server/src/__tests__/project-routes-env.test.ts @@ -22,6 +22,7 @@ const mockEnvironmentService = vi.hoisted(() => ({ })); const mockWorkspaceOperationService = vi.hoisted(() => ({})); const mockLogActivity = vi.hoisted(() => vi.fn()); +const mockProtectedKeyHolders = vi.hoisted(() => vi.fn()); const mockGetTelemetryClient = vi.hoisted(() => vi.fn()); const mockAccessService = vi.hoisted(() => ({ decide: vi.fn(), @@ -54,6 +55,10 @@ vi.mock("../services/workspace-runtime.js", () => ({ })); function registerModuleMocks() { + vi.doMock("../services/protected-environment-keys.js", async () => ({ + ...await vi.importActual("../services/protected-environment-keys.js"), + listProtectedEnvironmentKeyHolders: mockProtectedKeyHolders, + })); vi.doMock("../services/activity-log.js", async () => ({ ...await vi.importActual("../services/activity-log.js"), persistActivity: async (db: unknown, input: unknown) => { await mockLogActivity(db, input); return { activity: { id: "activity" }, publication: null }; }, @@ -168,6 +173,31 @@ describe("project env routes", () => { mockProjectService.listWorkspaces.mockResolvedValue([]); mockEnvironmentService.getById.mockReset(); mockSecretService.normalizeEnvBindingsForPersistence.mockImplementation(async (_companyId, env) => env); + mockProtectedKeyHolders.mockResolvedValue([]); + }); + + it("saves a protected env key and returns a notice that names the environment", async () => { + const env = { DESKTOP_SESSION: { type: "plain", value: "mine" }, OTHER: { type: "plain", value: "ok" } }; + mockProtectedKeyHolders.mockResolvedValue([{ environmentId: "computer", environmentName: "Team Computer", keys: new Set(["DESKTOP_SESSION"]) }]); + mockProjectService.getById.mockResolvedValue(buildProject()); + mockProjectService.update.mockResolvedValue(buildProject({ env })); + const app = await createApp(); + const res = await request(app).patch("/api/projects/project-1").send({ env }); + expect(res.status, JSON.stringify(res.body)).toBe(200); + expect(mockProjectService.update).toHaveBeenCalledWith("project-1", expect.objectContaining({ env })); + expect(res.body.configurationWarnings).toEqual([{ code: "protected_environment_variable", field: "env.DESKTOP_SESSION", + message: "DESKTOP_SESSION is set by Team Computer on its tasks. Agents running there may see the platform's value instead of this one." }]); + }); + + it("returns no notice when the update does not change env", async () => { + mockProtectedKeyHolders.mockResolvedValue([{ environmentId: "computer", environmentName: "Team Computer", keys: new Set(["DESKTOP_SESSION"]) }]); + mockProjectService.getById.mockResolvedValue(buildProject()); + mockProjectService.update.mockResolvedValue(buildProject({ name: "Renamed" })); + const app = await createApp(); + const res = await request(app).patch("/api/projects/project-1").send({ name: "Renamed" }); + expect(res.status, JSON.stringify(res.body)).toBe(200); + expect(res.body).not.toHaveProperty("configurationWarnings"); + expect(mockProtectedKeyHolders).not.toHaveBeenCalled(); }); it('requires workspace-write authority to attach a repository despite project read access', async () => { diff --git a/server/src/routes/agents.ts b/server/src/routes/agents.ts index 94c9de65372..26946de51ad 100644 --- a/server/src/routes/agents.ts +++ b/server/src/routes/agents.ts @@ -1,4 +1,5 @@ import { resolveLoginEnvironmentTarget } from "../services/login-environment.js"; +import { checkAgentProtectedEnvironmentKeys } from "../services/protected-environment-keys.js"; import { assertProviderApiPlacementPolicy, managedCursorApiDefaultEnabled, resolveProviderApiPlacement } from "../services/native-runtime/provider-api-placement.js"; import { isObjectJournalBusyError } from "../services/object-byte-journal.js"; import { readTaskPanelActivations } from "../services/plugin-task-panels.js"; @@ -4378,6 +4379,9 @@ export function agentRoutes( ); } + const rollbackWarnings = await checkAgentProtectedEnvironmentKeys(db, rollbackAdapterConfig, + typeof rollbackConfig.defaultEnvironmentId === "string" ? rollbackConfig.defaultEnvironmentId : null); + const actor = getActorInfo(req); const updated = await svc.rollbackConfigRevision(id, revisionId, { agentId: actor.agentId, @@ -4401,7 +4405,7 @@ export function agentRoutes( details: { revisionId }, }); - res.json(redactAgentRowForResponse(updated)); + res.json({ ...redactAgentRowForResponse(updated), ...(rollbackWarnings.length ? { configurationWarnings: rollbackWarnings } : {}) }); }); router.get("/agents/:id/runtime-state", async (req, res) => { @@ -4643,6 +4647,7 @@ export function agentRoutes( allowedDrivers: allowedEnvironmentDriversForAgent(hireInput.adapterType), allowedSandboxProviders: allowedSandboxProvidersForAgent(hireInput.adapterType), }); + configurationWarnings.push(...await checkAgentProtectedEnvironmentKeys(db, normalizedAdapterConfig, hireInput.defaultEnvironmentId)); // Idempotency within a run: if this run already created a hire from this // exact request, return that hire instead of creating a duplicate. The @@ -4963,6 +4968,7 @@ export function agentRoutes( allowedDrivers: allowedEnvironmentDriversForAgent(createInput.adapterType), allowedSandboxProviders: allowedSandboxProvidersForAgent(createInput.adapterType), }); + configurationWarnings.push(...await checkAgentProtectedEnvironmentKeys(db, normalizedAdapterConfig, createInput.defaultEnvironmentId)); const managedBinding = normalizedRuntimeConfig.aiConnection ? aiRuntimeConnectionBindingSchema.parse(normalizedRuntimeConfig.aiConnection) : undefined; const managedConnection = managedBinding && !setupId ? await validateManagedAgentBinding(req, companyId, agentId, createInput.adapterType, normalizedAdapterConfig, managedBinding, createInput.defaultEnvironmentId, "none", true) : undefined; @@ -5828,16 +5834,19 @@ export function agentRoutes( } if (requestedRuntimeConfig) patchData.runtimeConfig = requestedRuntimeConfig; if (touchesAdapterConfiguration || Object.prototype.hasOwnProperty.call(patchData, "defaultEnvironmentId")) { + const nextDefaultEnvironmentId = Object.prototype.hasOwnProperty.call(patchData, "defaultEnvironmentId") + ? (typeof patchData.defaultEnvironmentId === "string" ? patchData.defaultEnvironmentId : null) + : existing.defaultEnvironmentId; await assertAgentDefaultEnvironmentSelection( existing.companyId, - Object.prototype.hasOwnProperty.call(patchData, "defaultEnvironmentId") - ? (typeof patchData.defaultEnvironmentId === "string" ? patchData.defaultEnvironmentId : null) - : existing.defaultEnvironmentId, + nextDefaultEnvironmentId, { allowedDrivers: allowedEnvironmentDriversForAgent(requestedAdapterType), allowedSandboxProviders: allowedSandboxProvidersForAgent(requestedAdapterType), }, ); + const protectedKeyWarnings = await checkAgentProtectedEnvironmentKeys(db, patchData.adapterConfig ?? existing.adapterConfig, nextDefaultEnvironmentId); + configurationWarnings.push(...protectedKeyWarnings.filter(warning => !configurationWarnings.some(existing => existing.field === warning.field))); } const touchesProfileFields = touchesAgentProfileChangeConsentFields(patchData); const profileOnlyChange = touchesProfileFields && Object.keys(patchData).every((key) => diff --git a/server/src/routes/environments.ts b/server/src/routes/environments.ts index 10bbbf54610..06bfe2d46a3 100644 --- a/server/src/routes/environments.ts +++ b/server/src/routes/environments.ts @@ -1,4 +1,5 @@ import { Router, type Request } from "express"; +import { assertEnvironmentVariablesNotProtected } from "../services/protected-environment-keys.js"; import type { Db } from "@paperclipai/db"; import { AGENT_ADAPTER_TYPES, @@ -1097,6 +1098,7 @@ export function environmentRoutes( pluginWorkerManager: options.pluginWorkerManager, }), }; + await assertEnvironmentVariablesNotProtected(db, { name: input.name, driver: input.driver, config: input.config }, input.envVars); // Create the row and its binding rows atomically so an invalid secret // ref cannot leave an environment persisted without its bindings. const environment = await db.transaction(async (tx) => { @@ -1262,6 +1264,10 @@ export function environmentRoutes( // Persist the config change and its binding rows atomically: a binding // ref that fails validation (e.g. a deleted secret) must roll the whole // save back instead of leaving the config re-pointed with stale bindings. + if (patch.envVars !== undefined || patch.config !== undefined || patch.driver !== undefined) { + await assertEnvironmentVariablesNotProtected(db, { id: existing.id, name: nextName, driver: nextDriver, + config: patch.config ?? existing.config }, patch.envVars ?? existing.envVars); + } if (patch.config !== undefined || patch.driver !== undefined) { const instanceDefault = (await instanceSettingsService(db).get()).defaultEnvironmentId; await assertAgentsReadyForEnvironmentChange(db, existing, { driver: nextDriver, config: patch.config ?? existing.config }, diff --git a/server/src/routes/projects.ts b/server/src/routes/projects.ts index aa4c7475b26..5790d76502c 100644 --- a/server/src/routes/projects.ts +++ b/server/src/routes/projects.ts @@ -1,4 +1,5 @@ import { projectEnvironmentOptions } from "../services/project-environments.js"; +import { listProtectedEnvironmentKeyHolders, protectedEnvironmentKeyWarnings } from "../services/protected-environment-keys.js"; import { projectGitAuthorizationService } from "../services/project-git-authorization.js"; import { skillAccessRequestContext } from "../services/skill-access.js"; import { createHash } from "node:crypto"; @@ -419,6 +420,8 @@ export function projectRoutes(db: Db) { { strictMode: strictSecretsMode, fieldPath: "env" }, ); } + const configurationWarnings = projectData.env !== undefined + ? protectedEnvironmentKeyWarnings(await listProtectedEnvironmentKeyHolders(db), projectData.env, "env") : []; if (workspace && (repositoryIds || repositoryUrls)) throw unprocessable("Use either workspace or repositoryIds/repositoryUrls when creating a project"); const urlRepositories = (repositoryUrls ?? []).map(normalizeProjectRepositoryUrl); const repositories = repositoryIds ? await selectedRepositories(req, companyId, repositoryIds) : null; @@ -512,7 +515,7 @@ export function projectRoutes(db: Db) { if (telemetryClient) { trackProjectCreated(telemetryClient); } - res.status(result.duplicate ? 200 : 201).json(result.project); + res.status(result.duplicate ? 200 : 201).json({ ...result.project, ...(configurationWarnings.length ? { configurationWarnings } : {}) }); }); router.patch("/projects/:id", validate(updateProjectSchema), async (req, res) => { @@ -539,6 +542,8 @@ export function projectRoutes(db: Db) { fieldPath: "env", }); } + const configurationWarnings = body.env !== undefined + ? protectedEnvironmentKeyWarnings(await listProtectedEnvironmentKeyHolders(db), body.env, "env") : []; const project = await svc.update(id, body); if (!project) { res.status(404).json({ error: "Project not found" }); @@ -570,7 +575,7 @@ export function projectRoutes(db: Db) { }, }); - res.json(project); + res.json({ ...project, ...(configurationWarnings.length ? { configurationWarnings } : {}) }); }); router.get("/projects/:id/access-members", async (req, res) => { diff --git a/server/src/routes/routines.ts b/server/src/routes/routines.ts index 07b14ee585b..33945816cd6 100644 --- a/server/src/routes/routines.ts +++ b/server/src/routes/routines.ts @@ -1,4 +1,5 @@ import { Router, type Request } from "express"; +import { listProtectedEnvironmentKeyHolders, protectedEnvironmentKeyWarnings } from "../services/protected-environment-keys.js"; import type { Db } from "@paperclipai/db"; import { createRoutineSchema, @@ -200,7 +201,9 @@ export function routineRoutes( changeSummary: "Created routine", triggerCount: 0, }); - res.status(201).json(created); + const configurationWarnings = req.body.env !== undefined + ? protectedEnvironmentKeyWarnings(await listProtectedEnvironmentKeyHolders(db), req.body.env, "env") : []; + res.status(201).json({ ...created, ...(configurationWarnings.length ? { configurationWarnings } : {}) }); }); router.get("/routines/:id", async (req, res) => { @@ -441,7 +444,9 @@ export function routineRoutes( } if (updated) { const { previousResponsibleUserId: _previousResponsibleUserId, ...response } = updated; - res.json(response); + const configurationWarnings = req.body.env !== undefined + ? protectedEnvironmentKeyWarnings(await listProtectedEnvironmentKeyHolders(db), req.body.env, "env") : []; + res.json({ ...response, ...(configurationWarnings.length ? { configurationWarnings } : {}) }); } else { res.json(null); } diff --git a/server/src/services/native-runtime/native-session-executor.test.ts b/server/src/services/native-runtime/native-session-executor.test.ts index 52518b89559..fdd1f03c1bf 100644 --- a/server/src/services/native-runtime/native-session-executor.test.ts +++ b/server/src/services/native-runtime/native-session-executor.test.ts @@ -16683,10 +16683,11 @@ describe("runnerd provider runtime wiring", () => { }, ); - it.each((["codex", "claude"] as const).flatMap(provider => (["unchanged", "external-checkpoint", "suspension-receipt", "suspension-active-owner", "changed", "changed-external-checkpoint", "runtime-context", "retirement-failed", "stale-owner", "active-owner", "missing-owner", "foreign-environment", "plugin-replaced", "plugin-managed-github"] as const).filter(scenario => provider === "codex" || scenario.startsWith("plugin-")).map(scenario => [provider, scenario] as const)))("rotates an owned warm %s remote runner before checking changed configuration: %s", async (provider, scenario) => { - const plugin = scenario === "plugin-replaced" || scenario === "plugin-managed-github"; + it.each((["codex", "claude"] as const).flatMap(provider => (["unchanged", "external-checkpoint", "suspension-receipt", "suspension-active-owner", "changed", "changed-external-checkpoint", "runtime-context", "retirement-failed", "stale-owner", "active-owner", "missing-owner", "foreign-environment", "plugin-replaced", "plugin-managed-github", "plugin-environment-upgrade", "plugin-environment-delivered"] as const).filter(scenario => provider === "codex" || scenario.startsWith("plugin-")).map(scenario => [provider, scenario] as const)))("rotates an owned warm %s remote runner before checking changed configuration: %s", async (provider, scenario) => { + const plugin = scenario.startsWith("plugin-"); + const configuredScenario = scenario === "plugin-environment-upgrade" || scenario === "plugin-environment-delivered"; const suspensionReceipt = scenario.startsWith("suspension-"); - const reusable = suspensionReceipt || ["unchanged", "external-checkpoint", "plugin-managed-github"].includes(scenario); + const reusable = suspensionReceipt || ["unchanged", "external-checkpoint", "plugin-managed-github", "plugin-environment-delivered"].includes(scenario); const { permissionPolicy: _permissionPolicy, ...claudeProfile } = QUALIFIED_ACPX_PROFILES.claude; const first = { ...scopedNativeExecution(`warm-admission-${provider}-${scenario}`), schema: "paperclip.native-execution-input.v5" as const, @@ -16695,7 +16696,7 @@ describe("runnerd provider runtime wiring", () => { if (provider === "claude") first.session.driverKind = "acpx_runtime"; first.session.lifecyclePolicy = { mode: "warm", idleTimeoutMs: 60_000 }; const next = { ...first, binding: { ...first.binding, runId: `${first.binding.runId}-next` }, - session: { ...first.session, lifecyclePolicy: { mode: "warm" as const, idleTimeoutMs: suspensionReceipt || ["unchanged", "external-checkpoint", "runtime-context", "stale-owner", "missing-owner", "plugin-replaced", "plugin-managed-github"].includes(scenario) ? 60_000 : 90_000 } } }; + session: { ...first.session, lifecyclePolicy: { mode: "warm" as const, idleTimeoutMs: suspensionReceipt || ["unchanged", "external-checkpoint", "runtime-context", "stale-owner", "missing-owner", "plugin-replaced", "plugin-managed-github", "plugin-environment-upgrade", "plugin-environment-delivered"].includes(scenario) ? 60_000 : 90_000 } } }; if (scenario === "runtime-context") { next.runtimeContext = { ...first.runtimeContext, mcp: { ...first.runtimeContext.mcp, digest: "1".repeat(64) } }; @@ -16706,11 +16707,12 @@ describe("runnerd provider runtime wiring", () => { remoteCwd: "/home/daytona/workspace", runner: { execute: vi.fn() } }; const identity = { runId: first.binding.runId, normalizedSessionId: first.session.normalizedSessionId!, runnerInstanceId: `runner-${scenario}`, environmentLeaseId: first.binding.executionWorkspaceId }; - let replacementPending=false; + let replacementPending=false, advertiseEnvironment=false; const acquisition={outcome:"resumed" as "resumed"|"replacement",providerLeaseId:"task-first"}; const task=vi.fn(async()=>{ if(replacementPending) {acquisition.outcome="replacement";acquisition.providerLeaseId="task-next";replacementPending=false;} return {kind:"description",taskId:acquisition.providerLeaseId,launchPreparationVersion:1,projectAuthCheckVersion:1,warmTaskVersion:1, + ...(advertiseEnvironment ? {configuredEnvironmentVersion:1} : {}), runner:{version:"fixture",digest:`sha256:${"a".repeat(64)}`}}; }); const pluginTarget=plugin ? {...target,remoteCwd:".",leaseId:identity.environmentLeaseId, @@ -16719,13 +16721,15 @@ describe("runnerd provider runtime wiring", () => { if(plugin) { await mkdir(home,{recursive:true});await writeFile(join(home,"auth.json"),JSON.stringify({OPENAI_API_KEY:"synthetic-fixture"}),{mode:0o600}); } - const pluginOptions=plugin ? {runnerIngressAuthorized:true,runnerEnvironment:provider === "claude" ? {ANTHROPIC_API_KEY:"synthetic-fixture"} : {},managedGitHub:scenario === "plugin-managed-github",managedAiCredentialHome:home, + const configuredVariables=configuredScenario ? {PAPERCLIP_CONFIGURED_ENV_KEYS:'["FEATURE_FLAG"]',FEATURE_FLAG:"on"} : {}; + const pluginOptions=plugin ? {runnerIngressAuthorized:true,runnerEnvironment:{...(provider === "claude" ? {ANTHROPIC_API_KEY:"synthetic-fixture"} : {}),...configuredVariables},managedGitHub:scenario === "plugin-managed-github",managedAiCredentialHome:home, managedAiCredentialIdentity:"revision",managedAiAccountIdentity:"account"} : {}; - const resume=scenario === "plugin-replaced" ? vi.spyOn(environmentTaskRuntime,"resolvePluginTaskResume").mockResolvedValue("task-first") : undefined; + const resume=scenario === "plugin-replaced" || scenario === "plugin-environment-upgrade" ? vi.spyOn(environmentTaskRuntime,"resolvePluginTaskResume").mockResolvedValue("task-first") : undefined; const createOwner = pluginTaskRuntime.createPluginRunnerTaskOwner; const owners=plugin ? vi.spyOn(pluginTaskRuntime,"createPluginRunnerTaskOwner") : undefined; const rebind = vi.fn(); if (scenario === "plugin-managed-github") owners!.mockImplementation(input => ({...createOwner(input),rebind})); + if (scenario === "plugin-environment-delivered") owners!.mockImplementation(input => ({...createOwner(input),rebind,configuredEnvironmentDelivered:() => true})); const root = await initializeScopedRunnerRoot(first, identity.runnerInstanceId); await writeRunnerAuthority(root, identity, "ready"); @@ -16834,6 +16838,7 @@ describe("runnerd provider runtime wiring", () => { firstSession.close.mockClear(); } if(scenario === "plugin-replaced") replacementPending=true; + if(configuredScenario) advertiseEnvironment=true; const continuation = executePaperclipNativeSession({ db: nextDb, execution: next, runnerInstanceId: `runner-next-${scenario}`, useRunnerd: true, runnerExecutionTarget: (scenario === "foreign-environment" ? { ...target, environmentId: `${environmentId}-other` } @@ -16857,7 +16862,11 @@ describe("runnerd provider runtime wiring", () => { expect(owners!.mock.calls.at(-1)![0]).toMatchObject({resumeFrom:"task-first"}); expect(owners!.mock.calls.at(-1)![0].adoptIdentity).toBeUndefined(); } - if (scenario === "plugin-managed-github") expect(rebind).toHaveBeenCalledOnce(); + if (scenario === "plugin-managed-github" || scenario === "plugin-environment-delivered") expect(rebind).toHaveBeenCalledOnce(); + if (scenario === "plugin-environment-upgrade") { + expect(firstSession.close).toHaveBeenCalledWith({ reason: "warm native session configuration changed" }); + expect(owners!.mock.calls.at(-1)![0]).toMatchObject({ resumeFrom: "task-first", configuredEnvironmentVersion: 1, configuredEnvironment: { FEATURE_FLAG: "on" } }); + } expect(order).toEqual(reusable ? ["execute-next"] : ["retire", "execute-next"]); if (scenario === "changed-external-checkpoint") { expect(launchedJournal?.slice(-2)).toEqual(["runner.drain", "runner.suspend"]); @@ -18345,7 +18354,7 @@ describe("runnerd provider runtime wiring", () => { state.toolAuthorityDefinitions.mockImplementation(async binding => binding.readRemoteTaskFile ? [{ name: "register_deliverable" }] as never : []); const readWorkspaceFile = vi.fn(async () => Buffer.from("proof")); const release = vi.fn(async () => {}), resume = vi.fn(async () => "resumed" as const); - const owner = vi.spyOn(pluginTaskRuntime, "createPluginRunnerTaskOwner").mockReturnValue({ launcher, registration, driver, readWorkspaceFile, release, resume, isRetired: () => false, retireSupersededTask: async () => false, retiredBeforeClaim: () => false, credentialsUpdated: vi.fn(), rebind:vi.fn() }); + const owner = vi.spyOn(pluginTaskRuntime, "createPluginRunnerTaskOwner").mockReturnValue({ launcher, registration, driver, readWorkspaceFile, release, resume, isRetired: () => false, retireSupersededTask: async () => false, retiredBeforeClaim: () => false, configuredEnvironmentDelivered: () => false, credentialsUpdated: vi.fn(), rebind:vi.fn() }); const description = { kind: "description" as const, launchPreparationVersion: 1 as const, projectAuthCheckVersion: 1 as const, releaseResumeVersion: 1 as const, ...(kind === "codex" ? { workspaceFileReadVersion: 1 as const } : {}), taskId: "task", runner: { version: "fixture", digest: `sha256:${"a".repeat(64)}` } }; diff --git a/server/src/services/native-runtime/native-session-executor.ts b/server/src/services/native-runtime/native-session-executor.ts index f74002866cd..74185833543 100644 --- a/server/src/services/native-runtime/native-session-executor.ts +++ b/server/src/services/native-runtime/native-session-executor.ts @@ -77,7 +77,7 @@ import { isKnownNativeSessionRootEntry, } from "./native-session-root-entries.js"; import { createRunPreparationTiming } from "../execution-preparation-timing.js"; -import { configuredEnvironment, PROVIDER_CREDENTIAL_ENVIRONMENT_KEYS } from "../../vendor/paperclip-runner/index.js"; +import { CONFIGURED_ENVIRONMENT_KEYS, configuredEnvironment, PROVIDER_CREDENTIAL_ENVIRONMENT_KEYS } from "../../vendor/paperclip-runner/index.js"; import { parseProviderPerformanceSpans } from "../../vendor/paperclip-runner/index.js"; import { apiKeyBillingType } from "@paperclipai/adapter-utils/api-billing-type"; import { agents } from "@paperclipai/db"; @@ -8557,6 +8557,11 @@ function needsCoreGitHubAccess(input: Pick { + return Object.fromEntries(Object.entries(configuredEnvironment(environment)).flatMap(([key, value]) => + key !== CONFIGURED_ENVIRONMENT_KEYS && typeof value === "string" ? [[key, value]] : [])); +} + function nativeProcessReuseKey(input: Parameters[0]): string | undefined { const environment = input.runnerEnvironment; if (!input.managedGitHub && environment?.PAPERCLIP_GITHUB_BROKER_TOKEN) return undefined; @@ -11954,6 +11959,9 @@ async function executePaperclipNativeSessionAttempt( entry.closeOnReleaseReason !== undefined || entry.configDigest !== warmConfigDigest || !canReuseNativeProcess(input, entry.processConfigDigest) || + (entry.pluginTaskId !== undefined && pluginTaskDescription?.configuredEnvironmentVersion === 1 + && pluginTaskSessionOwners.get(entry.session)?.configuredEnvironmentDelivered() === false + && Object.keys(pluginTaskConfiguredEnvironment(input.runnerEnvironment)).length > 0) || Boolean(entry.githubAccess) !== needsCoreGitHubAccess(input) || entry.githubAccess?.ready === false || (entry.githubAccess !== undefined && entry.githubAccess.duplexRequested !== adapterExecutionTargetEnablesSandboxDuplexBridge(input.runnerExecutionTarget)) @@ -16377,6 +16385,11 @@ async function createRunnerdBackendWithinSessionClaim( await registerPluginTaskCredentialHandoff(input.db, { companyId: input.execution.binding.companyId, leaseId: remoteTarget.leaseId }, credentials.accountIdentity, input.managedAiCredentialWriteback); } + const taskConfiguredEnvironment = pluginTaskConfiguredEnvironment(input.runnerEnvironment); + if (description.configuredEnvironmentVersion !== 1 && Object.keys(taskConfiguredEnvironment).length > 0) { + await input.onLog?.("stderr", `[paperclip] The environment plugin does not support configured environment variables. These variables were not set: ${ + Object.keys(taskConfiguredEnvironment).sort().join(", ")}. Update the plugin to deliver them.\n`); + } const ownerInput: Parameters[0] = { ...credentials, taskId: description.taskId, executionContext: input.executionContext?.pluginSupported ? input.executionContext : undefined, // Warm retirement can outlive the run's temporary credential home; complete uses the durable handoff. @@ -16396,6 +16409,8 @@ async function createRunnerdBackendWithinSessionClaim( }), warmTaskVersion: description.warmTaskVersion, githubCredentialBrokerVersion: description.githubCredentialBrokerVersion, + configuredEnvironmentVersion: description.configuredEnvironmentVersion, + configuredEnvironment: taskConfiguredEnvironment, releaseResumeVersion: description.releaseResumeVersion, authorityRunId: description.releaseResumeVersion === 1 ? input.execution.binding.runId : undefined, releasedSource: input.remoteWarmRecovery?.releasedSource, diff --git a/server/src/services/native-runtime/plugin-runner-task.test.ts b/server/src/services/native-runtime/plugin-runner-task.test.ts index 2b7f08efdf9..3da4ca5cd4b 100644 --- a/server/src/services/native-runtime/plugin-runner-task.test.ts +++ b/server/src/services/native-runtime/plugin-runner-task.test.ts @@ -914,3 +914,38 @@ it("routes credential notifications to the current warm lease after rebind", asy expect(successor).toHaveBeenCalledExactlyOnceWith(event); expect(task).not.toHaveBeenCalled(); }); + + +describe("plugin task configured environment", () => { + const configuredEnvironment = { STRIPE_SECRET_KEY: "fixture-secret", FEATURE_FLAG: "on" }; + + it.each([1, undefined] as const)("submits the variables only to a provider that supports them (%s)", async version => { + const { task } = provider(); + const owner = createPluginRunnerTaskOwner({ taskId: "task", projectAuthCheckVersion: 1, task, projectIds: [], harness: "codex", pollMs: 1, + configuredEnvironmentVersion: version, configuredEnvironment }); + await prepare(owner); + ingress.connect.mockReturnValue({ ready: Promise.resolve(), close: async () => {} }); + const registration = await owner.registration({} as never, "run"); + const handle = owner.launcher(launchSpec()); + registration.activate(); await registration.ready(); + await registration.release(); await registration.checkpoint("unsettled"); await handle.completion; + const submit = task.mock.calls.find(([op]) => op.kind === "submit")?.[0]; + if (version === 1) expect(submit).toMatchObject({ configuredEnvironment }); + else expect(submit).not.toHaveProperty("configuredEnvironment"); + expect(owner.configuredEnvironmentDelivered()).toBe(version === 1); + expect(task.mock.calls.find(([op]) => op.kind === "prepare")?.[0]).not.toHaveProperty("configuredEnvironment"); + }); + + it.each([true, false])("sends the variables on resume only for a session handoff (%s)", async sessionHandoff => { + const task = vi.fn(async (_operation: any): Promise => ({ kind: "resume", taskId: "task", outcome: "resumed" })); + const owner = createPluginRunnerTaskOwner({ taskId: "task", projectIds: ["project"], harness: "codex", task, + warmTaskVersion: 1, releaseResumeVersion: 1, accountIdentity: "selected-account", adoptIdentity: requirements.identity, + authorityRunId: "run", sessionHandoff, configuredEnvironmentVersion: 1, configuredEnvironment }); + await expect(owner.resume(requirements.identity, "transient-ticket")).resolves.toBe("resumed"); + expect(task).toHaveBeenCalledExactlyOnceWith({ + kind: "resume", identity: requirements.identity, bootstrapTicket: "transient-ticket", + ...(sessionHandoff ? { configuredEnvironment } : {}), + }, { expectedTaskId: "task" }); + expect(owner.configuredEnvironmentDelivered()).toBe(sessionHandoff ? true : undefined); + }); +}); diff --git a/server/src/services/native-runtime/plugin-runner-task.ts b/server/src/services/native-runtime/plugin-runner-task.ts index 9c9a8d2a3e7..3df6ce70472 100644 --- a/server/src/services/native-runtime/plugin-runner-task.ts +++ b/server/src/services/native-runtime/plugin-runner-task.ts @@ -55,6 +55,8 @@ export function createPluginRunnerTaskOwner(input: { onRetired?: (identity: DurableRecoveryIdentity) => Promise; githubBroker?: { url: string; token: string }; githubCredentialBrokerVersion?: 1; + configuredEnvironmentVersion?: 1; + configuredEnvironment?: Readonly>; adoptIdentity?: DurableRecoveryIdentity; }) { const taskId = input.taskId; @@ -83,6 +85,9 @@ export function createPluginRunnerTaskOwner(input: { const running = new Promise((resolve, reject) => { markReady = resolve; failReady = reject; }); void running.catch(() => undefined); if (input.adoptIdentity) markReady(); + const configuredEnvironmentFields = (launchesRunner: boolean) => + input.configuredEnvironmentVersion === 1 && launchesRunner ? { configuredEnvironment: { ...input.configuredEnvironment } } : {}; + let configuredEnvironmentDelivered: boolean | undefined = input.adoptIdentity ? undefined : false; const call = async (operation: PluginEnvironmentTaskOperation) => { const phase = ({ prepare: "execution.plugin_prepare", check_project_auth: "execution.plugin_project_auth", submit: "execution.plugin_submit", connection: "execution.plugin_connection" } as const)[operation.kind as "prepare" | "check_project_auth" | "submit" | "connection"]; @@ -204,6 +209,7 @@ export function createPluginRunnerTaskOwner(input: { ...(input.credentialIdentity ? { credentialIdentity: input.credentialIdentity } : {}), ...(input.accountIdentity ? { accountIdentity: input.accountIdentity } : {}), ...(input.credential ? { credential: input.credential } : {}), + ...configuredEnvironmentFields(true), runner: { protocolMin: RUNNER_PRP_PROTOCOL_MIN, protocolMax: RUNNER_PRP_PROTOCOL_MAX, harness: input.harness, runnerId: native.identity.runnerInstanceId, leaseId: native.identity.environmentLeaseId, @@ -217,6 +223,7 @@ export function createPluginRunnerTaskOwner(input: { }); launchIdentity = native.identity; launched = true; + configuredEnvironmentDelivered = "configuredEnvironment" in operation; const child: RunnerProcessHandle["child"] = { exitCode: null, signalCode: null, kill: () => { stopRequested = true; @@ -426,9 +433,11 @@ export function createPluginRunnerTaskOwner(input: { : undefined; const result = await call(environmentTaskOperationSchema.parse({ kind: "resume", identity, bootstrapTicket, ...(handoffCredential ? { handoffCredential } : {}), + ...configuredEnvironmentFields(!!input.sessionHandoff), })); requireReleaseResume(); if (result.kind !== "resume") throw new Error("Plugin Runner resume result unavailable"); + if (result.outcome === "resumed" && input.sessionHandoff) configuredEnvironmentDelivered = input.configuredEnvironmentVersion === 1; if (result.outcome === "unavailable") { const state = await call({ kind: "status" }).catch(() => undefined); retiredBeforeClaim = state?.kind === "status" && state.executionStopped === true && state.sessionStateAvailable === true; @@ -462,6 +471,7 @@ export function createPluginRunnerTaskOwner(input: { // stop request. Completion also proves credential/writeback obligations ran. isRetired: () => stoppedState?.executionStopped === true && completed && !detached, retiredBeforeClaim: () => retiredBeforeClaim, + configuredEnvironmentDelivered: () => configuredEnvironmentDelivered, rebind(next: typeof input) { if (!launched || stopRequested || stoppedState || detached || next.taskId !== taskId || next.harness !== input.harness || input.accountIdentity !== next.accountIdentity || input.credentialIdentity !== next.credentialIdentity || diff --git a/server/src/services/protected-environment-keys.test.ts b/server/src/services/protected-environment-keys.test.ts new file mode 100644 index 00000000000..3b19647b926 --- /dev/null +++ b/server/src/services/protected-environment-keys.test.ts @@ -0,0 +1,105 @@ +import { beforeEach, describe, expect, it, vi } from "vitest"; + +const driverFor = vi.hoisted(() => vi.fn()); +vi.mock("./plugin-runner-ai-connection.js", () => ({ pluginEnvironmentRunnerTaskDriver: driverFor })); +const instanceDefault = vi.hoisted(() => ({ value: null as string | null })); +vi.mock("./instance-settings.js", () => ({ instanceSettingsService: () => ({ get: async () => ({ defaultEnvironmentId: instanceDefault.value }) }) })); + +const { + assertEnvironmentVariablesNotProtected, + checkAgentProtectedEnvironmentKeys, + assertNoProtectedEnvironmentKeys, + listProtectedEnvironmentKeyHolders, + protectedEnvironmentKeyWarnings, +} = await import("./protected-environment-keys.js"); + +const holders = [ + { environmentId: "computer-a", environmentName: "Computer A", keys: new Set(["DESKTOP_SESSION"]) }, + { environmentId: "computer-b", environmentName: "Computer B", keys: new Set(["DESKTOP_SESSION", "DESKTOP_DISPLAY"]) }, +]; + +function fakeDb(...results: unknown[][]) { + const selects = { count: 0 }; + const db = { select: () => ({ from: () => ({ where: async () => results[selects.count++] ?? [] }) }) }; + return Object.assign(db, { selects }) as never as { selects: { count: number } } & Parameters[0]; +} + +beforeEach(() => { driverFor.mockReset(); instanceDefault.value = null; }); + +describe("protected environment keys", () => { + it("rejects a key that the selected environment sets itself", () => { + expect(() => assertNoProtectedEnvironmentKeys(holders, "computer-a", { DESKTOP_SESSION: "mine", OTHER: "ok" })) + .toThrow(expect.objectContaining({ status: 422, message: expect.stringContaining("DESKTOP_SESSION is set by Computer A on its tasks"), + details: { code: "protected_environment_variable", keys: ["DESKTOP_SESSION"], environmentId: "computer-a" } })); + }); + + it("allows the key on another environment, or with none selected", () => { + expect(() => assertNoProtectedEnvironmentKeys(holders, "local", { DESKTOP_SESSION: "mine" })).not.toThrow(); + expect(() => assertNoProtectedEnvironmentKeys(holders, null, { DESKTOP_SESSION: "mine" })).not.toThrow(); + expect(() => assertNoProtectedEnvironmentKeys(holders, "computer-a", { OTHER: "ok" })).not.toThrow(); + }); + + it("names every environment that protects a key in a friendly notice", () => { + expect(protectedEnvironmentKeyWarnings(holders, { DESKTOP_DISPLAY: ":1", DESKTOP_SESSION: "x", OTHER: "ok" }, "env")).toEqual([ + { code: "protected_environment_variable", field: "env.DESKTOP_DISPLAY", + message: "DESKTOP_DISPLAY is set by Computer B on its tasks. Agents running there may see the platform's value instead of this one." }, + { code: "protected_environment_variable", field: "env.DESKTOP_SESSION", + message: "DESKTOP_SESSION is set by Computer A, Computer B on their tasks. Agents running there may see the platform's value instead of this one." }, + ]); + expect(protectedEnvironmentKeyWarnings(holders, null, "env")).toEqual([]); + }); + + it("lists only plugin environments whose task driver declares keys, reading the registry once", async () => { + const drivers = [ + { driverKey: "computer", supportsTasks: true, protectedEnvironmentKeys: ["DESKTOP_SESSION"] }, + { driverKey: "plain", supportsTasks: true }, + { driverKey: "storage", protectedEnvironmentKeys: ["IGNORED"] }, + ]; + const db = fakeDb([ + { id: "a", name: "Computer", config: { pluginKey: "p", driverKey: "computer" } }, + { id: "b", name: "Plain", config: { pluginKey: "p", driverKey: "plain" } }, + { id: "c", name: "Second computer", config: { pluginKey: "p", driverKey: "computer" } }, + { id: "d", name: "Storage", config: { pluginKey: "p", driverKey: "storage" } }, + { id: "e", name: "Unregistered", config: { pluginKey: "unregistered", driverKey: "computer" } }, + { id: "f", name: "Missing", config: { pluginKey: "missing", driverKey: "computer" } }, + ], [ + { pluginKey: "p", manifestJson: { capabilities: ["environment.drivers.register"], environmentDrivers: drivers } }, + { pluginKey: "unregistered", manifestJson: { capabilities: [], environmentDrivers: drivers } }, + ]); + expect(await listProtectedEnvironmentKeyHolders(db)).toEqual([ + { environmentId: "a", environmentName: "Computer", keys: new Set(["DESKTOP_SESSION"]) }, + { environmentId: "c", environmentName: "Second computer", keys: new Set(["DESKTOP_SESSION"]) }, + ]); + expect(db.selects.count).toBe(2); + const empty = fakeDb([{ id: "x", name: "No plugin key", config: {} }]); + expect(await listProtectedEnvironmentKeyHolders(empty)).toEqual([]); + expect(empty.selects.count).toBe(1); + }); + + it("rejects an environment's own variable when its driver protects it", async () => { + driverFor.mockResolvedValue({ protectedEnvironmentKeys: ["DESKTOP_SESSION"] }); + await expect(assertEnvironmentVariablesNotProtected(fakeDb([]), { id: "a", name: "Computer", driver: "plugin", + config: { pluginKey: "p", driverKey: "computer" } }, { DESKTOP_SESSION: "x" })) + .rejects.toMatchObject({ status: 422, details: { code: "protected_environment_variable", keys: ["DESKTOP_SESSION"] } }); + await expect(assertEnvironmentVariablesNotProtected(fakeDb([]), { name: "Local", driver: "local", config: {} }, { DESKTOP_SESSION: "x" })) + .resolves.toBeUndefined(); + expect(driverFor).toHaveBeenCalledTimes(1); + }); + + it("checks an agent without its own environment against the instance default", async () => { + const environmentRows = [{ id: "computer-env", name: "Team Computer", config: { pluginKey: "p", driverKey: "computer" } }]; + const pluginRows = [{ pluginKey: "p", manifestJson: { capabilities: ["environment.drivers.register"], + environmentDrivers: [{ driverKey: "computer", supportsTasks: true, protectedEnvironmentKeys: ["DESKTOP_SESSION"] }] } }]; + const db = () => fakeDb(environmentRows, pluginRows); + const config = { env: { DESKTOP_SESSION: { type: "plain", value: "mine" } } }; + instanceDefault.value = "computer-env"; + await expect(checkAgentProtectedEnvironmentKeys(db(), config, null)) + .rejects.toMatchObject({ status: 422, details: { code: "protected_environment_variable", environmentId: "computer-env" } }); + await expect(checkAgentProtectedEnvironmentKeys(db(), config, "computer-env")).rejects.toMatchObject({ status: 422 }); + expect(await checkAgentProtectedEnvironmentKeys(db(), config, "local-env")) + .toEqual([expect.objectContaining({ field: "adapterConfig.env.DESKTOP_SESSION" })]); + instanceDefault.value = "local-env"; + expect(await checkAgentProtectedEnvironmentKeys(db(), config, undefined)).toHaveLength(1); + expect(await checkAgentProtectedEnvironmentKeys(db(), { env: {} }, null)).toEqual([]); + }); +}); diff --git a/server/src/services/protected-environment-keys.ts b/server/src/services/protected-environment-keys.ts new file mode 100644 index 00000000000..0c73b5cc547 --- /dev/null +++ b/server/src/services/protected-environment-keys.ts @@ -0,0 +1,99 @@ +import { and, eq, inArray, ne } from "drizzle-orm"; +import { environments, plugins as pluginsTable, type Db } from "@paperclipai/db"; +import type { AgentConfigurationWarning } from "@paperclipai/shared"; +import { unprocessable } from "../errors.js"; +import { pluginEnvironmentRunnerTaskDriver } from "./plugin-runner-ai-connection.js"; +import { instanceSettingsService } from "./instance-settings.js"; + +export interface ProtectedEnvironmentKeyHolder { + environmentId: string; + environmentName: string; + keys: ReadonlySet; +} + +export async function listProtectedEnvironmentKeyHolders(db: Db): Promise { + const rows = await db.select({ id: environments.id, name: environments.name, config: environments.config }) + .from(environments).where(and(eq(environments.driver, "plugin"), eq(environments.status, "active"))); + const pluginKeys = [...new Set(rows.flatMap(row => typeof row.config?.pluginKey === "string" ? [row.config.pluginKey] : []))]; + if (pluginKeys.length === 0) return []; + const plugins = new Map((await db.select({ pluginKey: pluginsTable.pluginKey, manifestJson: pluginsTable.manifestJson }).from(pluginsTable) + .where(and(inArray(pluginsTable.pluginKey, pluginKeys), ne(pluginsTable.status, "uninstalled")))).map(plugin => [plugin.pluginKey, plugin])); + const holders: ProtectedEnvironmentKeyHolder[] = []; + for (const row of rows) { + const config = row.config ?? {}; + const plugin = typeof config.pluginKey === "string" ? plugins.get(config.pluginKey) : undefined; + const driver = plugin?.manifestJson?.capabilities?.includes("environment.drivers.register") + ? plugin.manifestJson.environmentDrivers?.find(candidate => candidate.driverKey === config.driverKey && candidate.supportsTasks === true) + : undefined; + const keys = driver?.protectedEnvironmentKeys ?? []; + if (keys.length > 0) holders.push({ environmentId: row.id, environmentName: row.name, keys: new Set(keys) }); + } + return holders; +} + +function configuredKeys(env: unknown): string[] { + return env !== null && typeof env === "object" && !Array.isArray(env) ? Object.keys(env) : []; +} + +function rejectProtectedKeys(holder: { environmentId?: string; environmentName: string; keys: ReadonlySet }, env: unknown): void { + const keys = configuredKeys(env).filter(key => holder.keys.has(key)).sort(); + if (keys.length === 0) return; + throw unprocessable( + `${keys.join(", ")} ${keys.length === 1 ? "is" : "are"} set by ${holder.environmentName} on its tasks. ` + + `Remove ${keys.length === 1 ? "it" : "them"} from the environment variables, or choose another environment.`, + { code: "protected_environment_variable", keys, ...(holder.environmentId ? { environmentId: holder.environmentId } : {}) }, + ); +} + +export function assertNoProtectedEnvironmentKeys( + holders: readonly ProtectedEnvironmentKeyHolder[], + environmentId: string | null | undefined, + env: unknown, +): void { + const holder = environmentId ? holders.find(candidate => candidate.environmentId === environmentId) : undefined; + if (holder) rejectProtectedKeys(holder, env); +} + +export async function assertEnvironmentVariablesNotProtected( + db: Db, + environment: { id?: string; name: string; driver: string; config: unknown }, + envVars: unknown, +): Promise { + if (environment.driver !== "plugin" || configuredKeys(envVars).length === 0) return; + const config = environment.config !== null && typeof environment.config === "object" && !Array.isArray(environment.config) + ? environment.config as Record : {}; + const driver = await pluginEnvironmentRunnerTaskDriver(db, { driver: "plugin", config }); + const keys = driver?.protectedEnvironmentKeys ?? []; + if (keys.length > 0) rejectProtectedKeys({ environmentId: environment.id, environmentName: environment.name, keys: new Set(keys) }, envVars); +} + +export async function checkAgentProtectedEnvironmentKeys( + db: Db, + adapterConfig: unknown, + environmentId: string | null | undefined, +): Promise { + const env = adapterConfig !== null && typeof adapterConfig === "object" ? (adapterConfig as { env?: unknown }).env : undefined; + if (configuredKeys(env).length === 0) return []; + const holders = await listProtectedEnvironmentKeyHolders(db); + if (holders.length === 0) return []; + const effectiveEnvironmentId = environmentId ?? (await instanceSettingsService(db).get()).defaultEnvironmentId ?? null; + assertNoProtectedEnvironmentKeys(holders, effectiveEnvironmentId, env); + return protectedEnvironmentKeyWarnings(holders, env, "adapterConfig.env"); +} + +export function protectedEnvironmentKeyWarnings( + holders: readonly ProtectedEnvironmentKeyHolder[], + env: unknown, + field: string, +): AgentConfigurationWarning[] { + return configuredKeys(env).sort().flatMap(key => { + const names = holders.filter(holder => holder.keys.has(key)).map(holder => holder.environmentName); + if (names.length === 0) return []; + return [{ + code: "protected_environment_variable" as const, + field: `${field}.${key}`, + message: `${key} is set by ${names.join(", ")} on ${names.length === 1 ? "its" : "their"} tasks. ` + + "Agents running there may see the platform's value instead of this one.", + }]; + }); +} diff --git a/tests/runner-e2e/source-sdk-public-inputs.json b/tests/runner-e2e/source-sdk-public-inputs.json index 284e4959589..9ed06f6f09e 100644 --- a/tests/runner-e2e/source-sdk-public-inputs.json +++ b/tests/runner-e2e/source-sdk-public-inputs.json @@ -13,11 +13,11 @@ "packages/plugins/sdk/src/environment-stop-error.ts": "b0e03f8aa2b2170ba095da5c7138050fced3f3cb", "packages/plugins/sdk/src/environment-sync-error.ts": "7d6a51a95da49a7da4525aebbab6382dc5c44ae8", "packages/plugins/sdk/src/environment-task-error.ts": "98aae8a06743b38884b68cb9a3ec44c57b34c0e4", - "packages/plugins/sdk/src/environment-tasks.ts": "de583d809187f0677e25b95f22c3c42961a9d2e2", + "packages/plugins/sdk/src/environment-tasks.ts": "318915dd6f2f6c86e875bfcb3a87a52b21afba1d", "packages/plugins/sdk/src/host-client-factory.ts": "e0301c390ea13f2340b669383f07af2455e48c8a", "packages/plugins/sdk/src/idle-drain.test.ts": "cc59f7881e7d46c89395a1d93d74f6c6b9b0a64a", "packages/plugins/sdk/src/idle-drain.ts": "ae6e4c6be4657a3d541396922b3bc551b8db557f", - "packages/plugins/sdk/src/index.ts": "4e573c181198ea98442321cc136341741e856ff7", + "packages/plugins/sdk/src/index.ts": "061377861158a1021f74d4101d0453e6751c0cb0", "packages/plugins/sdk/src/invocation-tracer.ts": "b28ac4aa4bc3e82658abac4a8081ed4508008696", "packages/plugins/sdk/src/protocol.duplex-channel.test.ts": "18255eb3d63038dc459240c111cbcb29fcba25cc", "packages/plugins/sdk/src/protocol.ts": "6cbc642412650a511c60b23f5625ac018d189f9d", @@ -331,7 +331,7 @@ "packages/shared/src/routine-variables.ts": "43d57551aaf8fcd32c10733af3198b6932b0d5d4", "packages/shared/src/run-failure-cause.test.ts": "68a726ac3e3b9be394ffa32b65acc6119e06424c", "packages/shared/src/run-failure-cause.ts": "6f7c646d715f384ee0a01805d5507b0c7a0669a2", - "packages/shared/src/runner-configuration-warnings.ts": "81db25b1b3ac273ba9b21911672a659b10129917", + "packages/shared/src/runner-configuration-warnings.ts": "7d8db761ee51bec746c425320d5900ef3505de1b", "packages/shared/src/runner-goal.test.ts": "05814124d8f592d6bf4ca78541474e6f354411d1", "packages/shared/src/runner-goal.ts": "dc61b33fd36d2ed9f68fc3a5630a81d1e001333b", "packages/shared/src/runtime-exposure/loopback-bind.test.ts": "91e49c143f2ec84d369699fbec71d63d763a1ec8", @@ -412,11 +412,11 @@ "packages/shared/src/types/issue.ts": "725bd6f677b9d461460697d1ce10dee739a66ebe", "packages/shared/src/types/live.ts": "b90e55e4de8600c6fc17d7fa8d89079d4ae655ec", "packages/shared/src/types/native-finalization.ts": "c026a73003ae232fea4f7624d561493b383df358", - "packages/shared/src/types/plugin.ts": "22e1e35307f91fdf5c210b86bae7746594669516", - "packages/shared/src/types/project.ts": "78eeea3694ce860d5d0be59c158c399c03368078", + "packages/shared/src/types/plugin.ts": "d009ee12f56e0e168d420cd46bdb8dbea323824c", + "packages/shared/src/types/project.ts": "50b9d9c51cb38d5862e287c1a47726716603f21c", "packages/shared/src/types/quota.ts": "a0686a9afb3111a4de093f66200dc0c9152e883e", "packages/shared/src/types/resource-memberships.ts": "9eab6fad50facf3e489f8c40e5bedd92e1d1faf8", - "packages/shared/src/types/routine.ts": "e5e5a93d63233029b9bf258558ad155677a4eca7", + "packages/shared/src/types/routine.ts": "850fdeb686d2dfcab190811cede79ee5d66be689", "packages/shared/src/types/runtime-exposure.ts": "785f053a7ad5448cde94f36738cdb90585bbc92e", "packages/shared/src/types/search.ts": "4e208a9b1942991bfe5f29ef58ec71625e2693e9", "packages/shared/src/types/secrets.ts": "05b07bce0550c1b3a160615bed661bc7f9c26156", @@ -493,8 +493,8 @@ "packages/shared/src/validators/native-finalization.ts": "d4e674de6ac7d36385e6f6c2172fa16ffc9b1830", "packages/shared/src/validators/onboarding-seed.ts": "de302a8400d4199053f379d9ed853522a3ac6d32", "packages/shared/src/validators/partial.ts": "fa60ec7481fc14175365e9e500c814b159f1ab53", - "packages/shared/src/validators/plugin.test.ts": "23bc443850d46d815877a71a44625fd42aebb00a", - "packages/shared/src/validators/plugin.ts": "782e051567729c2eb91a3b80e883b4a8ed996646", + "packages/shared/src/validators/plugin.test.ts": "d125614df25bf325e5627cebb82dd917c3931e77", + "packages/shared/src/validators/plugin.ts": "37dd518f4adcb65bdb9504fae9dfb9df787096cc", "packages/shared/src/validators/project.ts": "6ea168f5faf3161e6004ea47274f60634c77b219", "packages/shared/src/validators/provider-trace.ts": "b38b6200a2e51513f5dca0f8b008f2fe68175e82", "packages/shared/src/validators/resource-memberships.ts": "b124ac8a8f24b4dbb03ae82bcc87541f6b4ecdfd", diff --git a/ui/src/lib/configuration-warnings.ts b/ui/src/lib/configuration-warnings.ts new file mode 100644 index 00000000000..a7cc1f14ea6 --- /dev/null +++ b/ui/src/lib/configuration-warnings.ts @@ -0,0 +1,8 @@ +import type { AgentConfigurationWarning } from "@paperclipai/shared"; + +export function configurationWarningMessages( + result: { configurationWarnings?: AgentConfigurationWarning[] } | null | undefined, +): string | null { + const warnings = result?.configurationWarnings ?? []; + return warnings.length > 0 ? warnings.map(warning => warning.message).join("\n") : null; +} diff --git a/ui/src/pages/ProjectDetail.tsx b/ui/src/pages/ProjectDetail.tsx index f22f9e207b9..246f9030eaa 100644 --- a/ui/src/pages/ProjectDetail.tsx +++ b/ui/src/pages/ProjectDetail.tsx @@ -1,4 +1,5 @@ import { useShowAgentCreatedTasks } from "../hooks/useShowAgentCreatedTasks"; +import { configurationWarningMessages } from "../lib/configuration-warnings"; import { useCallback, useEffect, useMemo, useState, useRef } from "react"; import { Link, useParams, useNavigate, useLocation, Navigate } from "@/lib/router"; import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; @@ -467,7 +468,9 @@ export function ProjectDetail() { const updateProject = useMutation({ mutationFn: (data: Record) => projectsApi.update(projectLookupRef, data, resolvedCompanyId ?? lookupCompanyId), - onSuccess: (_project, data) => { + onSuccess: (updated, data) => { + const notice = configurationWarningMessages(updated); + if (notice) pushToast({ title: "Project saved", body: notice, tone: "warn" }); invalidateProject(); if (data.visibility !== undefined) queryClient.invalidateQueries({ queryKey: ["issues"] }); }, @@ -599,7 +602,9 @@ export function ProjectDetail() { fieldSaveRequestIds.current[field] = requestId; setFieldState(field, "saving"); try { - await projectsApi.update(projectLookupRef, data, resolvedCompanyId ?? lookupCompanyId); + const updated = await projectsApi.update(projectLookupRef, data, resolvedCompanyId ?? lookupCompanyId); + const notice = configurationWarningMessages(updated); + if (notice) pushToast({ title: "Project saved", body: notice, tone: "warn" }); invalidateProject(); if (data.visibility !== undefined) queryClient.invalidateQueries({ queryKey: ["issues"] }); if (fieldSaveRequestIds.current[field] !== requestId) return; @@ -611,7 +616,7 @@ export function ProjectDetail() { scheduleFieldReset(field, 3000); throw error; } - }, [invalidateProject, lookupCompanyId, projectLookupRef, queryClient, resolvedCompanyId, scheduleFieldReset, setFieldState]); + }, [invalidateProject, lookupCompanyId, projectLookupRef, pushToast, queryClient, resolvedCompanyId, scheduleFieldReset, setFieldState]); const projectBudgetSummary = useMemo(() => { const matched = budgetOverview?.policies.find( diff --git a/ui/src/pages/RoutineDetail.production.tsx b/ui/src/pages/RoutineDetail.production.tsx index a16ca2a9060..be5e1b67ac6 100644 --- a/ui/src/pages/RoutineDetail.production.tsx +++ b/ui/src/pages/RoutineDetail.production.tsx @@ -1,4 +1,5 @@ import { companyProjectsQuery } from "@/lib/company-projects-query"; +import { configurationWarningMessages } from "../lib/configuration-warnings"; import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { Navigate, useNavigate, useParams } from "@/lib/router"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; @@ -397,8 +398,10 @@ export function RoutineDetail() { ...(baseRevisionId ? { baseRevisionId } : {}), }); }, - onSuccess: async () => { + onSuccess: async (updated) => { setSaveConflict(false); + const notice = configurationWarningMessages(updated); + if (notice) pushToast({ title: "Routine saved", body: notice, tone: "warn" }); await Promise.all([ queryClient.invalidateQueries({ queryKey: queryKeys.routines.detail(routineId!) }), queryClient.invalidateQueries({ queryKey: queryKeys.routines.list(selectedCompanyId!) }), diff --git a/ui/src/pages/RoutineDetail.tsx b/ui/src/pages/RoutineDetail.tsx index f4a81ed7d7b..0698a0788b8 100644 --- a/ui/src/pages/RoutineDetail.tsx +++ b/ui/src/pages/RoutineDetail.tsx @@ -1,4 +1,5 @@ import { companyProjectsQuery } from "@/lib/company-projects-query"; +import { configurationWarningMessages } from "../lib/configuration-warnings"; import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { Navigate, useNavigate, useParams, useSearchParams } from "@/lib/router"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; @@ -376,8 +377,10 @@ export function RoutineDetail() { ...(baseRevisionId ? { baseRevisionId } : {}), }); }, - onSuccess: async () => { + onSuccess: async (updated) => { setSaveConflict(false); + const notice = configurationWarningMessages(updated); + if (notice) pushToast({ title: "Routine saved", body: notice, tone: "warn" }); await Promise.all([ queryClient.invalidateQueries({ queryKey: queryKeys.routines.detail(routineId!) }), queryClient.invalidateQueries({ queryKey: queryKeys.routines.list(selectedCompanyId!) }),