SPB Git

Repository Public

Reconnect.swift 5.4 KB 133 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : Claude Remote / Codex Remote9// File             : Packages/ClaudeRemoteKit/Sources/ClaudeRemoteAPI/Reconnect.swift10// Module           : ClaudeRemoteAPI11// Repository       : https://git.spboucher.ai/claude-remote-apple12// Project Website  : https://claude.spboucher.ai13// Created          : 2026-11-2914// Last Modified    : 2026-11-2915//16// =============================================================================17// AUTHOR & PROFESSIONAL AFFILIATION18// =============================================================================19//20// Author           : Simon-Pierre Boucher21// Position         : Professor of Finance22// Institution      : Université du Québec en Outaouais (UQO)23// Department       : Département des sciences administratives24// Organization     : SP Boucher25//26// Website          : https://spboucher.ai27// Git              : https://git.spboucher.ai28//29// =============================================================================30// OFFICIAL CONTACT DIRECTORY31// =============================================================================32//33// General          : info@spboucher.ai34// Professional     : contact@spboucher.ai35// Direct           : simon@spboucher.ai36// Research         : research@spboucher.ai37// Development      : dev@spboucher.ai38// Support          : support@spboucher.ai39// Security/Admin   : admin@spboucher.ai40// Notifications    : notifications@spboucher.ai41//42// Preferred Contact: support@spboucher.ai43//44// =============================================================================45// COPYRIGHT & INTELLECTUAL PROPERTY46// =============================================================================47//48// Copyright (c) 202649// Simon-Pierre Boucher50//51// All rights reserved where applicable.52//53// This file is governed by the applicable project license,54// copyright notices, institutional agreements and relevant law.55//56// Unless expressly authorized by the applicable license or57// rights holder, copying, redistribution, sublicensing,58// publication and commercial exploitation are not permitted.59//60// Existing third-party rights and notices remain unaffected.61//62// =============================================================================63// CONFIDENTIALITY & SECURITY64// =============================================================================65//66// Classification   : Public67// Distribution     : Public repository; use is governed by the project license68// Security Contact : admin@spboucher.ai69//70// Access to this source does not independently grant permission71// to redistribute or disclose confidential materials.72//73// Do not embed credentials, private keys, access tokens,74// certificates or confidential personal information.75//76// =============================================================================77// LICENSE & MAINTENANCE78// =============================================================================79//80// License          : None specified (no license file in this repository)81// Maintainer       : Simon-Pierre Boucher82// Technical Contact: dev@spboucher.ai83//84// =============================================================================85// END OF SP BOUCHER SOURCE HEADER86// =============================================================================8788import Foundation8990/// Exponential backoff with jitter, identical to the web client: `min(max, base · 2^attempt)`91/// multiplied by a random factor in `jitter` (0.5 s → 10 s, ×0.75…1.25).92public struct ReconnectPolicy: Sendable {93    public var base: TimeInterval94    public var maximum: TimeInterval95    public var jitter: ClosedRange<Double>96    /// Source of randomness in 0..<1 (injectable for tests).97    public var random: @Sendable () -> Double9899    public init(base: TimeInterval = 0.5, maximum: TimeInterval = 10, jitter: ClosedRange<Double> = 0.75...1.25,100                random: @escaping @Sendable () -> Double = { Double.random(in: 0..<1) }) {101        self.base = base102        self.maximum = maximum103        self.jitter = jitter104        self.random = random105    }106107    public static let standard = ReconnectPolicy()108    /// Slower policy for the shared event stream (1 s → 30 s).109    public static let events = ReconnectPolicy(base: 1, maximum: 30)110111    /// Delay before retry number `attempt` (0-based).112    public func delay(attempt: Int) -> TimeInterval {113        let exp = min(maximum, base * pow(2, Double(min(max(attempt, 0), 30))))114        return exp * (jitter.lowerBound + (jitter.upperBound - jitter.lowerBound) * random())115    }116}117118/// Time source of the reconnecting components, replaceable by a manual clock in tests.119public protocol Sleeper: Sendable {120    /// Suspends for `seconds`; throws `CancellationError` when the task is cancelled.121    func sleep(seconds: TimeInterval) async throws122    /// Monotonic time in seconds.123    func now() -> TimeInterval124}125126public struct SystemSleeper: Sleeper {127    public init() {}128    public func sleep(seconds: TimeInterval) async throws {129        try await Task.sleep(for: .milliseconds(Int(max(0, seconds) * 1000)))130    }131    public func now() -> TimeInterval { ProcessInfo.processInfo.systemUptime }132}133
Files main
Menu

Theme