diff --git a/server/src/__tests__/antigravity-save-refusal.test.ts b/server/src/__tests__/antigravity-save-refusal.test.ts index 1310b006335..2616f96c3be 100644 --- a/server/src/__tests__/antigravity-save-refusal.test.ts +++ b/server/src/__tests__/antigravity-save-refusal.test.ts @@ -14,6 +14,9 @@ import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } fro import { readFileSync } from "node:fs"; import { QUALIFIED_ACPX_PROFILES } from "../vendor/paperclip-runner/index.js"; import { ANTIGRAVITY_CANDIDATE_ADMISSION_ENV } from "../services/native-runtime/acpx-qualification.js"; +import { prepareAntigravityRunnerCredentials } from "../services/native-runtime/antigravity-runner-credentials.js"; +import { resolveHeartbeatNativeLifecyclePolicy } from "../services/heartbeat/native-lifecycle.js"; +import { resolveRunnerEnvironmentForRun } from "../services/runner-environment-lifecycle.js"; const distributions = JSON.parse(readFileSync(new URL("../../../packages/paperclip-runner/antigravity-distributions.json", import.meta.url), "utf8")); /** The operator's pending grant for one company, model and platform. */ @@ -225,4 +228,27 @@ describePostgres("Google Antigravity connection refusal at agent save", () => { expect(response.body.adapterConfig).toMatchObject({ provider: "acpx", acpxAgent: "antigravity", model: "gemini-3.8-flash-high" }); expect(allocate).not.toHaveBeenCalled(); expect(probe).not.toHaveBeenCalled(); }); + + it("saves an agent on a warm cloud task environment that then runs every turn per-turn", async () => { + release.pending = false; + const [warmTasks] = await db.insert(environments).values({ name: `Warm tasks ${agent.companyId}`, driver: "plugin", + config: { pluginKey: "fixture", driverKey: "tasks", driverConfig: {}, runnerLifecycleMode: "warm" } }).returning(); + await db.update(agents).set({ adapterConfig: { ...agent.adapterConfig, lifecycleMode: "warm" } }).where(eq(agents.id, agent.id)); + const binding = await saveGoogleConnection("api_key"); + const response = await request(app).patch(`/api/agents/${agent.id}`).send({ defaultEnvironmentId: warmTasks.id, runtimeConfig: { aiConnection: binding } }); + expect(response.status, JSON.stringify(response.body)).toBe(200); + const saved = await readAgent(); + expect(saved.defaultEnvironmentId).toBe(warmTasks.id); + + const runEnvironment = resolveRunnerEnvironmentForRun({ ...warmTasks, driver: "plugin" as const }, saved.adapterType, saved.adapterConfig, undefined, "google:api-key:1"); + expect(runEnvironment.config.runnerLifecycleMode).toBe("per_turn"); + const session = resolveHeartbeatNativeLifecyclePolicy({ adapterType: saved.adapterType, backend: "acpx_runtime", acpxAgent: "antigravity", + agentMode: saved.adapterConfig.lifecycleMode, agentIdleTimeoutMs: undefined, managedAiSelected: true, taskOwned: true, + environmentPolicy: runEnvironment.config.runnerLifecycleMode === "warm" ? { mode: "warm", idleTimeoutMs: 300_000 } : { mode: "per_turn", idleTimeoutMs: null } }); + expect(session.mode).toBe("per_turn"); + for (const lifecycleMode of [runEnvironment.config.runnerLifecycleMode as "warm" | "per_turn", session.mode]) { + await expect(prepareAntigravityRunnerCredentials({ environment: { GEMINI_API_KEY: "fixture-gemini-key" }, remote: true, lifecycleMode })) + .resolves.toMatchObject({ home: null }); + } + }); }); diff --git a/server/src/services/heartbeat/native-lifecycle.ts b/server/src/services/heartbeat/native-lifecycle.ts index 07fa42c103a..22733638ded 100644 --- a/server/src/services/heartbeat/native-lifecycle.ts +++ b/server/src/services/heartbeat/native-lifecycle.ts @@ -55,8 +55,7 @@ export function resolveHeartbeatNativeLifecyclePolicy(input: { // says warm: no release admitted a warm turn for it, so none can be live. if (requiresPerTurnNativeSession(profile)) return { mode: "per_turn", idleTimeoutMs: null }; return input.persistedPolicy ?? - (["openai_dot_mcp", "grok_bot_mcp", "muse_external", "cursor_cloud_api"].includes(input.backend) - || input.managedAiSelected && !supportsManagedWarm + (input.managedAiSelected && !supportsManagedWarm ? { mode: "per_turn", idleTimeoutMs: null } : input.environmentPolicy ?? agentPolicy); } diff --git a/server/src/services/native-runtime/native-warm-capability.ts b/server/src/services/native-runtime/native-warm-capability.ts index 5a884eeb52b..37473c7047b 100644 --- a/server/src/services/native-runtime/native-warm-capability.ts +++ b/server/src/services/native-runtime/native-warm-capability.ts @@ -22,11 +22,58 @@ export function supportsNativeWarmFiles(profile: WarmProviderSelection): boolean (profile.backend === "acpx_runtime" && profile.acpxAgent === "copilot"); } -/** Google Antigravity runs turn by turn with any credential, as Kimi does with a - * managed key. A warm owner needs its own proof that a credential change retires - * it and refreshes its native home on attach, so no choice selects warm yet. */ +const PER_TURN_ONLY_BACKENDS: ReadonlySet = new Set([ + "openai_dot_mcp", + "grok_bot_mcp", + "muse_external", + "cursor_cloud_api", +]); + +const DECLARED_PROVIDER_BACKENDS: ReadonlyMap = new Map([ + ["openai_dot", "openai_dot_mcp"], + ["grok_bot", "grok_bot_mcp"], + ["muse", "muse_external"], + ["cursor_cloud", "cursor_cloud_api"], +]); + +function resolveDeclaredWarmProviderSelection(adapterConfig: unknown): WarmProviderSelection | null { + if (typeof adapterConfig !== "object" || adapterConfig === null || Array.isArray(adapterConfig)) return null; + const config = adapterConfig as Record; + if (config.provider === "acpx") { + return { backend: "acpx_runtime", acpxAgent: config.acpxAgent ?? "claude" }; + } + const backend = typeof config.provider === "string" ? DECLARED_PROVIDER_BACKENDS.get(config.provider) : undefined; + return backend ? { backend } : null; +} + +/** + * Harnesses whose native sessions must always run per-turn (never warm), shared + * by both the heartbeat native session policy and the run-scoped environment + * lifecycle view (plugin task and sandbox lease retention): + * - Google Antigravity (`acpx_runtime` / `antigravity`): runs turn by turn with + * any credential, as Kimi does with a managed key; a warm owner needs its own + * proof that a credential change retires it and refreshes its native home on + * attach, so no choice selects warm yet. + * - `openai_dot_mcp`, `grok_bot_mcp`, `muse_external`, and `cursor_cloud_api`: + * external/cloud-hosted backends whose configuration validators accept only a + * per-turn lifecycle. + */ export function requiresPerTurnNativeSession(profile: WarmProviderSelection): boolean { - return profile.backend === "acpx_runtime" && profile.acpxAgent === "antigravity"; + return (profile.backend === "acpx_runtime" && profile.acpxAgent === "antigravity") + || PER_TURN_ONLY_BACKENDS.has(profile.backend); +} + +/** + * True when an adapter configuration targets a per-turn-only harness, including + * when full provider-profile resolution refuses the configuration (for example, + * a pending Antigravity rollout without a company grant, or an incomplete/invalid + * external backend config). Keeps plugin task and sandbox lease retention aligned + * with `requiresPerTurnNativeSession`. + */ +export function requiresPerTurnNativeSessionForConfig(adapterConfig: unknown, companyId?: string): boolean { + const profile = resolveWarmProviderSelection(adapterConfig, companyId) + ?? resolveDeclaredWarmProviderSelection(adapterConfig); + return profile !== null && requiresPerTurnNativeSession(profile); } /** Copilot carries a token in its fenced session environment, not a temporary auth file. diff --git a/server/src/services/runner-environment-lifecycle.test.ts b/server/src/services/runner-environment-lifecycle.test.ts index aa8b011a731..1015d6af293 100644 --- a/server/src/services/runner-environment-lifecycle.test.ts +++ b/server/src/services/runner-environment-lifecycle.test.ts @@ -93,3 +93,72 @@ describe("remote-sandbox warm default", () => { }); }); +describe("per-turn-only harnesses on a warm environment", () => { + const antigravity = { provider: "acpx", acpxAgent: "antigravity", model: "gemini-3.8-flash-high" }; + const dot = { provider: "openai_dot", dotBindingId: "00000000-0000-4000-8000-000000000001", allowUnmeteredProvider: true }; + const grokBot = { provider: "grok_bot", externalBindingId: "00000000-0000-4000-8000-000000000002", allowUnmeteredProvider: true }; + const muse = { provider: "muse", museBindingId: "00000000-0000-4000-8000-000000000003", allowUnmeteredProvider: true }; + const cursorCloud = { provider: "cursor_cloud", model: "claude-4.5-sonnet", repoUrl: "https://github.com/paperclipai/paperclip" }; + const pluginWarm = { driver: "plugin" as const, config: { pluginKey: "fixture", driverKey: "tasks", driverConfig: {}, runnerLifecycleMode: "warm" } }; + const sandboxWarm = { driver: "sandbox" as const, config: { provider: "daytona", runnerLifecycleMode: "warm" } }; + const sandboxWarmReusable = { driver: "sandbox" as const, config: { provider: "daytona", runnerLifecycleMode: "warm", reuseLease: true } }; + + const perTurnHarnesses = [ + ["Google Antigravity", antigravity], + ["Google Antigravity with the agent set to warm", { ...antigravity, lifecycleMode: "warm" }], + ["Dot", dot], + ["Grok Bot", grokBot], + ["Muse", muse], + ["Cursor Cloud", cursorCloud], + // Even if profile validation refuses the config (for example missing binding or lifecycleMode: "warm"), + // the environment view still keeps it per-turn rather than falling through to warm. + ["refused Dot config", { provider: "openai_dot", lifecycleMode: "warm" }], + ["refused Grok Bot config", { provider: "grok_bot", lifecycleMode: "warm" }], + ["refused Muse config", { provider: "muse", lifecycleMode: "warm" }], + ["refused Cursor Cloud config", { provider: "cursor_cloud", lifecycleMode: "warm" }], + ] as const; + + it.each(perTurnHarnesses)( + "%s keeps a per-turn plugin task", (_name, adapterConfig) => { + expect(resolveRunnerEnvironmentForRun(pluginWarm, "paperclip_runner", adapterConfig, undefined, "account:generation")) + .toMatchObject({ config: { runnerLifecycleMode: "per_turn" } }); + expect(resolveRunnerEnvironmentForRun({ ...pluginWarm, config: { ...pluginWarm.config, runnerLifecycleMode: undefined } }, + "paperclip_runner", adapterConfig, undefined, "account:generation")).toMatchObject({ config: { runnerLifecycleMode: "per_turn" } }); + expect(pluginWarm.config.runnerLifecycleMode).toBe("warm"); + }); + + it.each(perTurnHarnesses)( + "%s keeps an ephemeral sandbox lease even when the environment sets reuseLease: true", (_name, adapterConfig) => { + const resolved = resolveRunnerEnvironmentForRun(sandboxWarm, "paperclip_runner", adapterConfig, undefined, "account:generation", { reusableLeaseSupported: true }); + expect(resolved).toBe(sandboxWarm); + expect(resolved.config).not.toHaveProperty("reuseLease"); + + const resolvedReusable = resolveRunnerEnvironmentForRun(sandboxWarmReusable, "paperclip_runner", adapterConfig, undefined, "account:generation", { reusableLeaseSupported: true }); + expect(resolvedReusable.config.reuseLease).toBe(false); + // The shared environment itself is never rewritten. + expect(sandboxWarmReusable.config.reuseLease).toBe(true); + + expect(resolveWarmRunnerIdleTimeoutMsForRun(sandboxWarmReusable, "paperclip_runner", { ...adapterConfig, lifecycleMode: "warm" })).toBeUndefined(); + expect(runnerLifecycleDefaultNeedsLeaseProbe({ driver: "sandbox", config: { provider: "daytona" } }, "paperclip_runner", adapterConfig)).toBe(false); + }); + + it.each(perTurnHarnesses)( + "%s overrides a persisted warm admission to per-turn on plugin and sandbox environments", (_name, adapterConfig) => { + expect(resolveRunnerEnvironmentForRun(pluginWarm, "paperclip_runner", adapterConfig, "warm", "account:generation")) + .toMatchObject({ config: { runnerLifecycleMode: "per_turn" } }); + expect(resolveRunnerEnvironmentForRun(sandboxWarmReusable, "paperclip_runner", adapterConfig, "warm", "account:generation", { reusableLeaseSupported: true }).config.reuseLease) + .toBe(false); + expect(resolveWarmRunnerIdleTimeoutMsForRun(sandboxWarmReusable, "paperclip_runner", adapterConfig, "warm")) + .toBeUndefined(); + }); + + it("still lets other harnesses use the environment's warm setting and admitted warm recovery", () => { + expect(resolveRunnerEnvironmentForRun(pluginWarm, "paperclip_runner", { provider: "codex" }, undefined, "account:generation")) + .toMatchObject({ config: { runnerLifecycleMode: "warm" } }); + expect(resolveRunnerEnvironmentForRun(pluginWarm, "paperclip_runner", { provider: "codex" }, "warm")) + .toMatchObject({ config: { runnerLifecycleMode: "warm" } }); + expect(resolveRunnerEnvironmentForRun(sandboxWarm, "paperclip_runner", { provider: "codex" }).config).toMatchObject({ reuseLease: true }); + expect(resolveRunnerEnvironmentForRun(sandboxWarmReusable, "paperclip_runner", { provider: "codex" }, "warm").config).toMatchObject({ reuseLease: true }); + }); +}); + diff --git a/server/src/services/runner-environment-lifecycle.ts b/server/src/services/runner-environment-lifecycle.ts index 30b63b9cb48..0b8d55f2c56 100644 --- a/server/src/services/runner-environment-lifecycle.ts +++ b/server/src/services/runner-environment-lifecycle.ts @@ -1,7 +1,9 @@ import { resolvePaperclipRunnerIdleTimeoutMs } from "@paperclipai/adapter-utils"; import type { Environment } from "@paperclipai/shared"; import { REMOTE_RUNNER_RECONNECT_GRACE_MS } from "./native-runtime/native-runner-recovery-contract.js"; -import { defaultsRemoteSandboxRunToWarm, resolveWarmProviderSelection } from "./native-runtime/native-warm-capability.js"; +import { + defaultsRemoteSandboxRunToWarm, requiresPerTurnNativeSessionForConfig, resolveWarmProviderSelection, +} from "./native-runtime/native-warm-capability.js"; /** Facts the remote-sandbox warm default needs beyond the configuration. */ export interface RunnerLifecycleDefaultContext { @@ -16,6 +18,9 @@ function explicitLifecycleMode( adapterConfig: Record, admittedLifecycleMode?: "warm" | "per_turn", ): "warm" | "per_turn" | undefined { + // Matches resolveHeartbeatNativeLifecyclePolicy: harnesses qualified only per-turn + // never keep a warm task or reusable sandbox lease, even over a saved warm admission. + if (requiresPerTurnNativeSessionForConfig(adapterConfig)) return "per_turn"; if (admittedLifecycleMode) return admittedLifecycleMode; if (config.runnerLifecycleMode === "warm" || config.runnerLifecycleMode === "per_turn") return config.runnerLifecycleMode; if (adapterConfig.lifecycleMode === "warm" || adapterConfig.lifecycleMode === "per_turn") return adapterConfig.lifecycleMode; @@ -86,14 +91,29 @@ export function resolveRunnerEnvironmentForRun