@@ -351,6 +351,7 @@ final class ExecApprovalsPromptServer {
351351 static let shared = ExecApprovalsPromptServer ( )
352352
353353 private let retryDelay : Duration
354+ private let maximumRetryDelay : Duration
354355 private let resolveSocketCredentials : @Sendable ( ) -> ( socketPath : String , token : String )
355356 private let onPrompt : @Sendable ( ExecApprovalPromptRequest ) async -> ExecApprovalDecision ?
356357 private var server : ExecApprovalsSocketServer ?
@@ -360,6 +361,7 @@ final class ExecApprovalsPromptServer {
360361
361362 init (
362363 retryDelay : Duration = . seconds ( 1 ) ,
364+ maximumRetryDelay : Duration = . seconds ( 30 ) ,
363365 resolveSocketCredentials : @escaping @Sendable ( ) -> ( socketPath : String , token : String ) = {
364366 let approvals = ExecApprovalsStore . resolve ( agentId : nil )
365367 return ( approvals . socketPath , approvals . token )
@@ -371,6 +373,7 @@ final class ExecApprovalsPromptServer {
371373 } )
372374 {
373375 self . retryDelay = retryDelay
376+ self . maximumRetryDelay = maximumRetryDelay
374377 self . resolveSocketCredentials = resolveSocketCredentials
375378 self . onPrompt = onPrompt
376379 }
@@ -380,6 +383,7 @@ final class ExecApprovalsPromptServer {
380383 self . startupGeneration &+= 1
381384 let generation = self . startupGeneration
382385 let retryDelay = self . retryDelay
386+ let maximumRetryDelay = self . maximumRetryDelay
383387 let resolveSocketCredentials = self . resolveSocketCredentials
384388 let onPrompt = self . onPrompt
385389 let previousStartupTask = self . previousStartupTask
@@ -394,12 +398,15 @@ final class ExecApprovalsPromptServer {
394398 guard ! Task . isCancelled , self ? . startupGeneration == generation else { return }
395399
396400 var isFirstAttempt = true
401+ var retryBackoff = ExecApprovalsPromptRetryBackoff (
402+ initialDelay : retryDelay ,
403+ maximumDelay : maximumRetryDelay )
397404 while ! Task . isCancelled {
398405 if isFirstAttempt {
399406 isFirstAttempt = false
400407 } else {
401408 do {
402- try await Task . sleep ( for : retryDelay )
409+ try await Task . sleep ( for : retryBackoff . nextDelay ( ) )
403410 } catch {
404411 return
405412 }
@@ -1181,6 +1188,24 @@ private final class ExecApprovalsSocketLifecycleLease: @unchecked Sendable {
11811188 }
11821189}
11831190
1191+ struct ExecApprovalsPromptRetryBackoff {
1192+ private var currentDelay : Duration
1193+ private let maximumDelay : Duration
1194+
1195+ init ( initialDelay : Duration , maximumDelay : Duration ) {
1196+ self . currentDelay = initialDelay
1197+ self . maximumDelay = max ( initialDelay , maximumDelay )
1198+ }
1199+
1200+ mutating func nextDelay ( ) -> Duration {
1201+ let delay = self . currentDelay
1202+ // A second app can hold the lifecycle lease indefinitely. Back off the
1203+ // SQLite credential read and bind attempt instead of polling every second.
1204+ self . currentDelay = min ( self . currentDelay * 2 , self . maximumDelay )
1205+ return delay
1206+ }
1207+ }
1208+
11841209private final class ExecApprovalsSocketServer : @unchecked Sendable {
11851210 private struct OpenedSocket {
11861211 let fd : Int32
0 commit comments