diff --git a/server/src/services/native-runtime/native-restart-recovery.ts b/server/src/services/native-runtime/native-restart-recovery.ts index d6454f9f533..0d379910cfe 100644 --- a/server/src/services/native-runtime/native-restart-recovery.ts +++ b/server/src/services/native-runtime/native-restart-recovery.ts @@ -1657,6 +1657,17 @@ export async function claimNativeRestartRecoveries(input: { && !durableAuthority && stagedAttachment === null && providerEvents.length === 0; const inheritedRunnerIdentity = inheritedCheckpointOnly && Boolean(row.run.processPid || row.run.processGroupId) && await carriesInheritedRunnerIdentity(tx, row.run); + const pendingInheritedRequests = Array.isArray(checkpointRecord.pendingRuntimeRequests) + && checkpointRecord.pendingRuntimeRequests.length > 0; + const continuedSessionOnly = hasCheckpoint && checkpointIdentityMatches && providerEvents.length === 0 + && !pendingInheritedRequests && checkpointRecord.activeTurnId == null && checkpointRecord.semanticResult == null + && checkpointRecord.terminal == null; + // Admission of a continued-session follow-up records the previous turn's + // provider session as sessionIdBefore and rebinds that turn's checkpoint + // to this run. Before any executor claims the run, both are inherited, + // not this run's provider work. + const unclaimedContinuedSessionCandidate = Boolean(row.run.sessionIdBefore) && continuedSessionOnly + && inheritedCheckpointOnly && checkpointRecord.providerSessionId === row.run.sessionIdBefore; let unclaimedBootstrap = remoteWorkspace?.state === "prepared" && row.run.runnerProfileJson?.nativePreparationFencingVersion === NATIVE_PREPARATION_FENCING_VERSION && @@ -1672,11 +1683,16 @@ export async function claimNativeRestartRecoveries(input: { (!row.coordinator.leaseExpiresAt || row.coordinator.leaseExpiresAt <= now) && !row.coordinator.failureDetail?.successorRunId && (inheritedRunnerIdentity || (!row.run.processPid && !row.run.processGroupId && !row.run.processStartedAt)) && - !row.run.sessionIdBefore && + (!row.run.sessionIdBefore || unclaimedContinuedSessionCandidate) && !row.run.sessionIdAfter && Boolean(row.run.nativeSessionId && row.run.runnerInstanceId) && (!hasCheckpoint || inheritedCheckpointOnly) && (!hasProviderEvidence || inheritedCheckpointOnly); + // Only a session that a settled earlier run of this session saved is + // inherited; any other carried session keeps its existing recovery decision. + if (unclaimedBootstrap && row.run.sessionIdBefore) { + unclaimedBootstrap = await carriesSettledPredecessorSession(tx, row.run, checkpointRecord.providerSessionId); + } if (unclaimedBootstrap) { if (activeNativeExecutionRunIds.has(row.run.id) && controller.bootId === (await currentNativeControllerIdentity()).bootId) { @@ -1690,11 +1706,6 @@ export async function claimNativeRestartRecoveries(input: { // backend may spawn a runner, and a remote runner exchanges commands only // through a Postgres controller checkpoint. With neither, nothing reached // a runner. The previous controller is gone (takeover was allowed above). - const pendingInheritedRequests = Array.isArray(checkpointRecord.pendingRuntimeRequests) - && checkpointRecord.pendingRuntimeRequests.length > 0; - const continuedSessionOnly = hasCheckpoint && checkpointIdentityMatches && providerEvents.length === 0 - && !pendingInheritedRequests && checkpointRecord.activeTurnId == null && checkpointRecord.semanticResult == null - && checkpointRecord.terminal == null; const portablePreparation = pluginTask?.unlaunchedPortable === true && row.run.status === "running" && row.coordinator.phase === "observed" && !durableAuthority && !row.run.sessionIdAfter && row.run.runnerProfileJson?.nativePreparationFencingVersion === NATIVE_PREPARATION_FENCING_VERSION && diff --git a/server/src/services/native-runtime/native-runner-restart-recovery.integration.test.ts b/server/src/services/native-runtime/native-runner-restart-recovery.integration.test.ts index 740a6b740ac..f4240b74e1a 100644 --- a/server/src/services/native-runtime/native-runner-restart-recovery.integration.test.ts +++ b/server/src/services/native-runtime/native-runner-restart-recovery.integration.test.ts @@ -3463,6 +3463,46 @@ process.on("SIGTERM", () => child.kill("SIGTERM")); expect(coordinator.recoveryHistory.at(-1)).toMatchObject({ reason: "remote_execution_never_claimed" }); }); + // A hard restart reached a continued-session follow-up on a new Daytona + // sandbox before any executor claimed it. Admission had + // already rebound the previous turn's checkpoint and recorded that provider + // session as sessionIdBefore. Reattaching found no runner state in the new + // sandbox (remote_reattach_root_missing), and the failed run then became the + // next follow-up's metadata_format source. + it.each([ + { label: "restarts", mode: "rebound" }, + { label: "keeps the reattach decision when no settled earlier run saved the provider session in", mode: "no_predecessor" }, + { label: "keeps the reattach decision for a carried active turn in", mode: "active_turn" }, + { label: "keeps the reattach decision for a projected provider event in", mode: "provider_event" }, + { label: "keeps the reattach decision when sessionIdBefore differs from the checkpoint in", mode: "other_session" }, + ] as const)("$label an unclaimed continued-session follow-up", async ({ mode }) => { + const fixture = await seedUnlaunchedRemoteClaim(`UNCLAIMED-CONTINUED-${mode}`); + await reboundFollowUpCheckpoint(fixture, mode === "active_turn" ? { activeTurnId: "turn-in-flight" } : {}, + mode !== "no_predecessor"); + if (mode === "other_session") await fixture.db.update(heartbeatRuns).set({ sessionIdBefore: "other-thread" }) + .where(eq(heartbeatRuns.id, fixture.runId)); + await fixture.db.update(nativeRunFinalizations).set({ attempt: 0, controllerGeneration: 0, controllerBootId: null, + controllerPid: null, controllerProcessStartedAt: null, leaseOwner: null, leaseExpiresAt: null }) + .where(eq(nativeRunFinalizations.runId, fixture.runId)); + if (mode === "provider_event") await fixture.db.insert(heartbeatRunEvents).values({ companyId, runId: fixture.runId, + agentId, seq: 901, stream: "system", level: "info", eventType: "session.started" }); + const [claim] = await claimNativeRestartRecoveries({ db: fixture.db, controller: successor, + restartKind: "hard", runIds: [fixture.runId] }); + const [coordinator] = await fixture.db.select().from(nativeRunFinalizations) + .where(eq(nativeRunFinalizations.runId, fixture.runId)); + if (mode !== "rebound") { + // Excluded shapes must reach the pre-existing reattach decision, not a + // recovery error that would also avoid bootstrap_incomplete. + expect(claim).toMatchObject({ kind: "reattach_remote_runner", runId: fixture.runId, providerAttempt: 0, + controllerGeneration: 1 }); + expect(coordinator.recoveryHistory.at(-1)).toMatchObject({ reason: "remote_runner_requires_sandbox_verification", + disposition: "reattach_remote_runner" }); + return; + } + expect(claim).toMatchObject({ kind: "bootstrap_incomplete", runId: fixture.runId, providerAttempt: 0, controllerGeneration: 1 }); + expect(coordinator.recoveryHistory.at(-1)).toMatchObject({ reason: "remote_execution_never_claimed" }); + }); + it.each([ { label: "bootstraps a follow-up carrying its predecessor's Runner identity", mode: "inherited", restarts: true }, { label: "bootstraps a follow-up that requested its warm Runner on its predecessor's identity", mode: "inherited_requested", restarts: true },