mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-05 14:35:17 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - The Paperclip Runner runs each agent in an environment: the host, a sandbox provider, or a plugin task environment > - An environment can set `runnerLifecycleMode: "warm"` (and `reuseLease: true` for sandboxes). Then the run keeps its task or sandbox lease alive and reuses it between runs > - Some harnesses are qualified only for per-turn sessions: Google Antigravity, Dot, Muse, Grok Bot, and Cursor Cloud > - The native session policy already keeps these harnesses per-turn. The environment view did not: on a warm plugin environment it leased a warm task, and on a warm sandbox environment it could lease or retain a reusable sandbox (`reuseLease: true`) > - The run-time Antigravity defense refuses a warm lifecycle (`antigravity_warm_unsupported`). An agent on a warm environment saved without error, but its run lifecycle disagreed with what the harness supports > - This pull request makes the environment view use the same per-turn-only declaration as the session policy, clears `reuseLease` on reusable sandboxes for per-turn-only harnesses, and enforces per-turn even over a persisted warm admission > - The benefit is that an agent with a per-turn-only harness saves and runs per-turn on every environment, with no failed runs and no manual fix ## Linked Issues or Issue Description **What happened?** A plugin environment with `runnerLifecycleMode: "warm"` (for example a managed Daytona or Unikraft task environment) or a sandbox environment with `runnerLifecycleMode: "warm"` / `reuseLease: true` overrode the agent's own lifecycle. For a Google Antigravity agent (or Dot, Grok Bot, Muse, Cursor Cloud), the run could lease a warm task or reusable sandbox kept between runs. The native session policy is qualified only per-turn, and Antigravity's run-time check refuses warm. The save-time check did not see the environment lifecycle, so the agent saved without error. **Expected behavior** A harness that supports only per-turn sessions runs per-turn on every environment, including when the environment asks for `warm` or `reuseLease: true`, and even if a pre-fix persisted warm admission exists. The user never saves a configuration that fails at run start. **Steps to reproduce** 1. Create a plugin environment with `runnerLifecycleMode: "warm"`, or a sandbox environment with `runnerLifecycleMode: "warm"` and `reuseLease: true`. 2. Save a Google Antigravity agent with a Google API key on that environment. The save succeeds. 3. Resolve the run environment for that agent (`resolveRunnerEnvironmentForRun`). On master the plugin task lifecycle is `warm` and a reusable sandbox retains `reuseLease: true`. **Deployment mode** Both local (`local_trusted`) and hosted/cloud (`authenticated`) Paperclip Runner deployments using plugin task or remote sandbox environments. ## What Changed - `requiresPerTurnNativeSession` in `server/src/services/native-runtime/native-warm-capability.ts` is now the single declaration of per-turn-only harnesses: Google Antigravity (`acpx_runtime` / `antigravity`), plus `openai_dot_mcp`, `grok_bot_mcp`, `muse_external`, and `cursor_cloud_api`, with an updated doc comment covering all five harnesses and both rationales. - Added `requiresPerTurnNativeSessionForConfig` in `native-warm-capability.ts` so the environment lifecycle view resolves per-turn-only harnesses through the same module, including when full provider-profile validation refuses a config (such as a pending Antigravity rollout without a company grant or an incomplete/invalid external bot config). - The native session policy (`server/src/services/heartbeat/native-lifecycle.ts`) uses `requiresPerTurnNativeSession` instead of duplicating the backend list. - The run environment view (`resolveRunnerEnvironmentForRun`, `resolveWarmRunnerIdleTimeoutMsForRun`, and `runnerLifecycleDefaultNeedsLeaseProbe`) now enforces per-turn-only harnesses ahead of `admittedLifecycleMode`, environment settings, and agent settings (matching `resolveHeartbeatNativeLifecyclePolicy`), and explicitly sets `reuseLease: false` on run-scoped sandbox views when the shared sandbox config has `reuseLease: true`. - Other harnesses continue to honor environment warm settings and admitted warm recovery. ## Verification - `server/src/services/runner-environment-lifecycle.test.ts`: covers all five per-turn-only harnesses (Google Antigravity, Dot, Grok Bot, Muse, Cursor Cloud) and refused external bot configs across warm plugin environments, warm sandboxes with `reuseLease: true`, and persisted `admittedLifecycleMode: "warm"`, plus unaffected harnesses keeping warm/admitted recovery. - `server/src/__tests__/antigravity-save-refusal.test.ts`: verifies saving a Google Antigravity agent with a Google API key on a warm plugin task environment resolves per-turn across the run environment, session policy, and run-time credential check. - `server/src/services/heartbeat/native-lifecycle.test.ts`: passes without changes. - `pnpm --filter @paperclipai/server typecheck` (all layers including `test-tree-0`) and targeted Vitest suites pass locally; full suite verified by CI. ## Risks - Low risk. The change affects only the five per-turn-only harnesses. Their sessions already ran per-turn, so only plugin task and sandbox lease retention change from warm/reusable to per-turn/ephemeral. - A per-turn-only harness no longer keeps a warm task or reusable sandbox between runs on a warm environment, including if an older run had a persisted warm admission. That retention was never qualified for these harnesses. - In the session policy, a saved warm policy for Dot, Muse, Grok Bot, or Cursor Cloud is now ignored; their configuration validators never allowed warm. > For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and discuss it in `#dev` before opening the PR. Feature PRs that overlap with planned core work may need to be redirected — check the roadmap first. See `CONTRIBUTING.md`. ## Model Used - Initial implementation: Claude Opus 5.5 (`claude-opus-5-5`) in Claude Code. - Review fixes & CI verification: Antigravity with tool use and code execution. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Paperclip <noreply@paperclip.ing>
161 lines
7.3 KiB
TypeScript
161 lines
7.3 KiB
TypeScript
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, requiresPerTurnNativeSessionForConfig, resolveWarmProviderSelection,
|
|
} from "./native-runtime/native-warm-capability.js";
|
|
|
|
/** Facts the remote-sandbox warm default needs beyond the configuration. */
|
|
export interface RunnerLifecycleDefaultContext {
|
|
/** The sandbox provider keeps reusable leases (probed before acquisition). */
|
|
reusableLeaseSupported?: boolean;
|
|
/** The run uses a managed AI account. */
|
|
managedAiCredentialIdentity?: string;
|
|
}
|
|
|
|
function explicitLifecycleMode(
|
|
config: Record<string, unknown>,
|
|
adapterConfig: Record<string, unknown>,
|
|
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;
|
|
return undefined;
|
|
}
|
|
|
|
/**
|
|
* The remote-sandbox warm default for a run whose lifecycle nobody chose,
|
|
* through the same rule as the native session policy. The harness resolves
|
|
* exactly as the runtime resolves it (an omitted provider is Codex); a config
|
|
* the runtime would refuse gets no default here.
|
|
*/
|
|
function sandboxRunDefaultsToWarm(
|
|
adapterConfig: Record<string, unknown>,
|
|
context: RunnerLifecycleDefaultContext,
|
|
): boolean {
|
|
const profile = resolveWarmProviderSelection(adapterConfig);
|
|
if (!profile) return false;
|
|
return defaultsRemoteSandboxRunToWarm({
|
|
profile,
|
|
reusableLeaseSupported: context.reusableLeaseSupported === true,
|
|
// The attribution is not known here; a harness that needs it for a managed
|
|
// warm session (Copilot) is not a measured default, so this is exact.
|
|
managedAi: context.managedAiCredentialIdentity ? {} : null,
|
|
});
|
|
}
|
|
|
|
function resolveRunnerLifecycleModeForRun(
|
|
environment: Pick<Environment, "driver" | "config">,
|
|
adapterConfig: Record<string, unknown>,
|
|
admittedLifecycleMode: "warm" | "per_turn" | undefined,
|
|
context: RunnerLifecycleDefaultContext,
|
|
): unknown {
|
|
const config = environment.config ?? {};
|
|
const explicit = explicitLifecycleMode(config, adapterConfig, admittedLifecycleMode);
|
|
if (explicit) return explicit;
|
|
if (environment.driver === "sandbox" && sandboxRunDefaultsToWarm(adapterConfig, context)) return "warm";
|
|
return adapterConfig.lifecycleMode;
|
|
}
|
|
|
|
/**
|
|
* True when only the provider's reusable-lease support is left to decide this
|
|
* run's sandbox lifecycle, so the caller probes it only for these runs.
|
|
*/
|
|
export function runnerLifecycleDefaultNeedsLeaseProbe(
|
|
environment: Pick<Environment, "driver" | "config">,
|
|
adapterType: string,
|
|
adapterConfig: Record<string, unknown> = {},
|
|
admittedLifecycleMode?: "warm" | "per_turn",
|
|
managedAiCredentialIdentity?: string,
|
|
): boolean {
|
|
if (adapterType !== "paperclip_runner" || environment.driver !== "sandbox") return false;
|
|
if (explicitLifecycleMode(environment.config ?? {}, adapterConfig, admittedLifecycleMode)) return false;
|
|
return sandboxRunDefaultsToWarm(adapterConfig, { reusableLeaseSupported: true, managedAiCredentialIdentity });
|
|
}
|
|
|
|
/** Resolve runner retention before acquiring a sandbox, not after it is ephemeral.
|
|
* This is a run-scoped view; never persist it over a shared environment's config.
|
|
* The driver still verifies provider support and chooses the actual lease policy.
|
|
*/
|
|
export function resolveRunnerEnvironmentForRun<T extends Pick<Environment, "driver" | "config">>(
|
|
environment: T,
|
|
adapterType: string,
|
|
adapterConfig: Record<string, unknown> = {},
|
|
admittedLifecycleMode?: "warm" | "per_turn",
|
|
managedAiCredentialIdentity?: string,
|
|
defaultContext: Pick<RunnerLifecycleDefaultContext, "reusableLeaseSupported"> = {},
|
|
): T {
|
|
if (adapterType !== "paperclip_runner" || !["sandbox", "plugin"].includes(environment.driver)) return environment;
|
|
const config = environment.config ?? {};
|
|
const perTurnOnly = requiresPerTurnNativeSessionForConfig(adapterConfig);
|
|
const lifecycleMode = resolveRunnerLifecycleModeForRun(environment, adapterConfig, admittedLifecycleMode,
|
|
{ ...defaultContext, managedAiCredentialIdentity });
|
|
// Warm tasks need a managed credential identity; recovery keeps its admitted policy,
|
|
// except per-turn-only harnesses which never retain a warm task or reusable sandbox.
|
|
if (environment.driver === "plugin") return { ...environment, config: { ...config,
|
|
runnerLifecycleMode: perTurnOnly
|
|
? "per_turn"
|
|
: (admittedLifecycleMode ?? (managedAiCredentialIdentity && lifecycleMode !== "per_turn" ? "warm" : "per_turn")),
|
|
runnerIdleTimeoutMs: resolvePaperclipRunnerIdleTimeoutMs(config.runnerIdleTimeoutMs ?? adapterConfig.idleTimeoutMs),
|
|
} };
|
|
if (lifecycleMode !== "warm") {
|
|
if (perTurnOnly && config.reuseLease === true) {
|
|
return {
|
|
...environment,
|
|
config: {
|
|
...config,
|
|
reuseLease: false,
|
|
},
|
|
};
|
|
}
|
|
return environment;
|
|
}
|
|
return {
|
|
...environment,
|
|
config: {
|
|
...config,
|
|
reuseLease: true,
|
|
},
|
|
};
|
|
}
|
|
|
|
/**
|
|
* The warm runner idle window (ms) a sandbox-provider lease must outlive for
|
|
* this run, or undefined when the run keeps no warm runner on the sandbox. The
|
|
* host forwards it to the provider so a provider idle auto-stop (for example
|
|
* Daytona's `autoStopInterval`) is derived from the same value the host uses
|
|
* for its warm idle timer, instead of an independent default that can match it.
|
|
*/
|
|
export function resolveWarmRunnerIdleTimeoutMsForRun(
|
|
environment: Pick<Environment, "driver" | "config">,
|
|
adapterType: string,
|
|
adapterConfig: Record<string, unknown> = {},
|
|
admittedLifecycleMode?: "warm" | "per_turn",
|
|
defaultContext: RunnerLifecycleDefaultContext = {},
|
|
): number | undefined {
|
|
if (adapterType !== "paperclip_runner" || environment.driver !== "sandbox") return undefined;
|
|
const config = environment.config ?? {};
|
|
if (resolveRunnerLifecycleModeForRun(environment, adapterConfig, admittedLifecycleMode, defaultContext) !== "warm") return undefined;
|
|
return resolvePaperclipRunnerIdleTimeoutMs(config.runnerIdleTimeoutMs ?? adapterConfig.idleTimeoutMs);
|
|
}
|
|
|
|
/**
|
|
* The remote runner reconnect grace (ms) a sandbox-provider lease must outlive
|
|
* for this run, or undefined when the run places no remote runner on a sandbox.
|
|
* A remote runner keeps working while the controller is offline, and the host
|
|
* reattaches it within this grace. The host forwards it to the provider so a
|
|
* provider idle auto-stop (for example Daytona's `autoStopInterval`) cannot stop
|
|
* the sandbox, and kill the runner, during an outage the host still recovers.
|
|
*/
|
|
export function resolveRunnerReconnectGraceMsForRun(
|
|
environment: Pick<Environment, "driver">,
|
|
adapterType: string,
|
|
): number | undefined {
|
|
if (adapterType !== "paperclip_runner" || environment.driver !== "sandbox") return undefined;
|
|
return REMOTE_RUNNER_RECONNECT_GRACE_MS;
|
|
}
|