diff --git a/server/src/services/native-runtime/antigravity-release.test.ts b/server/src/services/native-runtime/antigravity-release.test.ts index b99029caf78..3ff7e7a30b0 100644 --- a/server/src/services/native-runtime/antigravity-release.test.ts +++ b/server/src/services/native-runtime/antigravity-release.test.ts @@ -1,5 +1,7 @@ -import { readFileSync } from "node:fs"; -import { afterEach, describe, expect, it, vi } from "vitest"; +import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { basename, join } from "node:path"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; // Both release states are exercised against the same build: the pending // candidate declaration that ships today, and the qualified declaration the @@ -26,7 +28,7 @@ import { assertAntigravityExecutionAdmissible, } from "./antigravity-release.js"; import { buildNativeExecutionInput } from "./native-execution-input.js"; -import { nativeSessionFailureDisposition, nativeSessionFailureSourceCode, scopedRunnerdStateRoot } from "./native-session-executor.js"; +import { nativeSessionFailureDisposition, nativeSessionFailureSourceCode, retireNeverOpenedCandidateRunRoot, scopedRunnerdStateRoot } from "./native-session-executor.js"; import { NATIVE_PROVIDER_STATE_INCOMPATIBLE } from "./native-session-resume.js"; import { nativeRuntimeContextFixture } from "./runtime-context.test-fixture.js"; @@ -195,6 +197,68 @@ describe("Antigravity recovery policy and state root", () => { }); }); +describe("Antigravity candidate same-run startup retry", () => { + let stateBase: string; + beforeEach(() => { + stateBase = mkdtempSync(join(tmpdir(), "antigravity-candidate-root-")); + vi.stubEnv("PAPERCLIP_RUNNER_STATE_DIR", stateBase); + }); + afterEach(() => rmSync(stateBase, { recursive: true, force: true })); + + /** What a candidate runner leaves after its session.open failed and it suspended. */ + function writeFailedOpenRoot(execution: NativeExecutionInput, provider: Record = {}) { + const root = scopedRunnerdStateRoot(execution); + mkdirSync(join(root, "control-plane"), { recursive: true, mode: 0o700 }); + mkdirSync(join(root, "runner"), { recursive: true, mode: 0o700 }); + writeFileSync(join(root, "control-plane", "control-plane-state.json"), JSON.stringify({ + schema: "paperclip.runner.durable.control-plane-state.v2", + identity: { runnerInstanceId: "80000000-0000-4000-8000-000000000008", environmentLeaseId: execution.binding.executionWorkspaceId, + runId: execution.binding.runId, normalizedSessionId: execution.session.normalizedSessionId }, + })); + writeFileSync(join(root, "runner", "acpx-provider-state.json"), JSON.stringify({ + schema: "paperclip.runner.acpx-provider-state.v3", lifecycle: "closed", identity: null, activeTurnId: null, + providerExitUnconfirmed: false, semanticResult: null, pendingEvents: [], pendingRuntimeRequests: {}, openSemanticCalls: [], + ...provider, + })); + return root; + } + const quarantined = (root: string) => existsSync(join(stateBase, "quarantine")) + && readdirSync(join(stateBase, "quarantine")).some(name => name.startsWith(`${basename(root)}.identity_indeterminate.`)); + + it("retires a root whose session.open never opened a provider session, so the retry starts fresh", async () => { + const execution = antigravityExecution(); + const root = writeFailedOpenRoot(execution); + expect(await retireNeverOpenedCandidateRunRoot(execution, root)).toBe(true); + expect(existsSync(root)).toBe(false); + expect(quarantined(root)).toBe(true); + }); + + it("keeps a root that opened a provider session or started a turn, and every root once released", async () => { + for (const provider of [ + { lifecycle: "suspended", identity: { kind: "acpx", acpxRecordId: "record" } }, + { lifecycle: "closed", identity: { kind: "acpx", acpxRecordId: "record" } }, + { lifecycle: "turn_active", activeTurnId: "turn" }, + { providerExitUnconfirmed: true }, + ]) { + const execution = antigravityExecution(); + const root = writeFailedOpenRoot(execution, provider); + expect(await retireNeverOpenedCandidateRunRoot(execution, root)).toBe(false); + expect(existsSync(root)).toBe(true); + rmSync(root, { recursive: true, force: true }); + } + // Another run's root is never this run's to retire. + const other = antigravityExecution({ runId: "50000000-0000-4000-8000-000000000005" }); + const otherRoot = writeFailedOpenRoot(other); + expect(await retireNeverOpenedCandidateRunRoot(antigravityExecution(), otherRoot)).toBe(false); + expect(existsSync(otherRoot)).toBe(true); + release.pending = false; + const released = antigravityExecution(); + const releasedRoot = writeFailedOpenRoot(released); + expect(await retireNeverOpenedCandidateRunRoot(released, releasedRoot)).toBe(false); + expect(existsSync(releasedRoot)).toBe(true); + }); +}); + describe("Antigravity state saved by the pending candidate, resumed by the qualified release", () => { it("refuses the candidate's in-flight state permanently and by name, never as a retryable interruption", () => { // Saved before the deploy under the candidate's profile version. diff --git a/server/src/services/native-runtime/native-session-executor.ts b/server/src/services/native-runtime/native-session-executor.ts index a572abcb2a7..5e550176719 100644 --- a/server/src/services/native-runtime/native-session-executor.ts +++ b/server/src/services/native-runtime/native-session-executor.ts @@ -6678,6 +6678,9 @@ async function migrateRunnerdStateRootForExecution(input: { return; } if (durableIdentityMatchesExecution(identity, input.execution)) { + if (!input.restartRecovery && !input.retainedWarmOwnerRunId + && (!input.runnerExecutionTarget || input.runnerExecutionTarget.kind === "local") + && await retireNeverOpenedCandidateRunRoot(input.execution, scoped)) return; if (!input.restartRecovery && !input.retainedWarmOwnerRunId && input.bootstrapRetryLeaseOwner && input.runnerExecutionTarget?.kind === "remote" && runnerdAuthorityLifecycle(scoped, identity) === "absent" @@ -7172,6 +7175,34 @@ export function prepareNativeSessionOpenTimeoutRetry(execution: NativeExecutionI return true; } +/** + * The pending Antigravity candidate's runner refuses every prior activation in + * its run-scoped root. A same-run startup retry after a session.open that never + * recorded a provider identity has nothing to adopt, so its root is retired and + * the retry bootstraps fresh. A root whose provider state recorded an identity, + * a turn, or anything unreadable is kept, and the runner's gate refuses it. + */ +export async function retireNeverOpenedCandidateRunRoot(execution: NativeExecutionInput, root: string): Promise { + if (!antigravityUsesRunScopedState(execution) || !isSafeNativeStateDirectory(root)) return false; + const identity = readRunnerdDurableIdentity(root); + if (!durableIdentityMatchesExecution(identity, execution)) return false; + let state: Record; + try { + state = record(JSON.parse(readBoundedNativeFile(resolve(root, "runner", "acpx-provider-state.json"), + NATIVE_RUNNER_STATE_MAX_BYTES, "native_runner_checkpoint_invalid").toString("utf8"))); + } catch { + return false; + } + const empty = (value: unknown) => value == null || (Array.isArray(value) ? value.length === 0 : Object.keys(record(value)).length === 0); + if (state.schema !== ACPX_PROVIDER_STATE_SCHEMA || !["prepared", "closed"].includes(String(state.lifecycle)) + || state.identity !== null || state.activeTurnId !== null || state.providerExitUnconfirmed !== false + || state.semanticResult != null || !empty(state.pendingEvents) || !empty(state.pendingRuntimeRequests) + || !empty(state.openSemanticCalls)) return false; + if (!(await noLocalRunnerProcessForRun(execution.binding.runId))) return false; + quarantineRunnerdStateRoot(root, "identity_indeterminate"); + return true; +} + export function runnerdStateProvesIncompleteBootstrap( root: string, options: { localProcesses: boolean } = { localProcesses: false },