@@ -33,6 +33,12 @@ import { UsageJwtRefresher } from "./usageJwtRefresh.js";
3333
3434const CREDENTIAL_RESUME_RETRY_DELAYS_MS = [ 0 , 1_000 , 3_000 ] ;
3535
36+ // Only for a platform whose continue response doesn't carry the new snapshot. Gaps before each
37+ // fetch of it: snapshots/since can be served from a read replica that hasn't got it yet. Replica
38+ // lag is usually well under 100ms, so the first fetch nearly always finds it; the second, 600ms
39+ // after continue, covers a slow replica. Then the poll or a notification takes over.
40+ const CONTINUED_SNAPSHOT_FETCH_DELAYS_MS = [ 100 , 500 ] ;
41+
3642class ExecutionAbortError extends Error {
3743 constructor ( message : string ) {
3844 super ( message ) ;
@@ -892,6 +898,8 @@ export class RunExecution {
892898 throw new Error ( "Cannot restore: missing run or snapshot manager" ) ;
893899 }
894900
901+ const continuedFromSnapshotId = this . snapshotManager . snapshotId ;
902+
895903 // Short delay to give websocket time to reconnect
896904 await sleep ( 100 ) ;
897905
@@ -903,7 +911,7 @@ export class RunExecution {
903911 // No billing report here: the restored runner does not own the suspended snapshot yet.
904912 if ( this . runToken ?. isDue ) await this . sendHeartbeat ( "restore" , { reportUsageJwt : false } ) ;
905913
906- const continuationResult = await this . httpClient . continueRunExecution (
914+ let continuationResult = await this . httpClient . continueRunExecution (
907915 this . runFriendlyId ,
908916 this . snapshotManager . snapshotId
909917 ) ;
@@ -923,13 +931,51 @@ export class RunExecution {
923931 if ( ! retryResult . success ) {
924932 throw new Error ( retryResult . error ) ;
925933 }
934+
935+ continuationResult = retryResult ;
926936 } else {
927937 throw new Error ( continuationResult . error ) ;
928938 }
929939 }
930940
931941 // Track restore count
932942 this . restoreCount ++ ;
943+
944+ // Continuing created the EXECUTING snapshot that carries the completed waitpoints. Take it in
945+ // now instead of leaving it to the next poll, which can be seconds away, or to a notification
946+ // a restored runner may not be subscribed to yet. Not awaited: we are inside the snapshot
947+ // queue, which processes it once this change is done.
948+ const { executionData } = continuationResult . data ;
949+
950+ if ( executionData ) {
951+ this . enqueueSnapshotChangesAndWait ( [ executionData ] ) . catch ( ( error ) => {
952+ this . sendDebugLog ( "restore: failed to process the continued snapshot" , {
953+ error : error instanceof Error ? error . message : String ( error ) ,
954+ } ) ;
955+ } ) ;
956+ return ;
957+ }
958+
959+ // An older platform doesn't return it, so fetch it.
960+ this . fetchContinuedSnapshot ( continuedFromSnapshotId ) . catch ( ( ) => { } ) ;
961+ }
962+
963+ /**
964+ * Fetches snapshot changes until the snapshot manager moves past the snapshot the run was
965+ * continued from. snapshots/since can be served from a read replica that doesn't have the
966+ * snapshot continue created yet, and then returns an empty list. After the last try the poll or
967+ * a notification takes over, as for any other snapshot change.
968+ */
969+ private async fetchContinuedSnapshot ( continuedFromSnapshotId : string ) {
970+ for ( const delayMs of CONTINUED_SNAPSHOT_FETCH_DELAYS_MS ) {
971+ await sleep ( delayMs ) ;
972+
973+ if ( this . isShuttingDown || this . currentSnapshotFriendlyId !== continuedFromSnapshotId ) {
974+ return ;
975+ }
976+
977+ await this . fetchAndProcessSnapshotChanges ( "continue" ) ;
978+ }
933979 }
934980
935981 private async exitTaskRunProcessWithoutFailingRun ( {
@@ -945,6 +991,15 @@ export class RunExecution {
945991 this . shutdownExecution ( `exitTaskRunProcessWithoutFailingRun: ${ reason } ` ) ;
946992 }
947993
994+ /**
995+ * Called by the RunController when it replaces its socket after a restore under a different
996+ * supervisor, so run notifications are received on the new socket.
997+ */
998+ public updateSupervisorSocket ( supervisorSocket : SupervisorSocket ) {
999+ this . supervisorSocket = supervisorSocket ;
1000+ this . notifier ?. updateSocket ( supervisorSocket ) ;
1001+ }
1002+
9481003 /**
9491004 * Processes env overrides from the metadata service. Generally called when we're resuming from a suspended state.
9501005 */
0 commit comments