import Foundation public struct StepLog: Codable, Sendable { public var stepId: String public var attempt: Int64 public var tail: String /// The server bounds stored logs; also bound unexpected older responses. public var boundedTail: String { let bytes = tail.utf8.suffix(32 * 1024).drop(while: { $0 & 0xc0 == 0x80 }) return String(decoding: bytes, as: UTF8.self).split(separator: "\n", omittingEmptySubsequences: false).suffix(200).joined(separator: "\n") } } struct TaskAccepted: Decodable, Sendable { let attempt: Int64 } struct ResumeConnection: Codable, Sendable { let id: String let runnerId: String let localId: String let provider: String let spend: Spend? struct Spend: Codable, Sendable { let unknown: Bool let pauseRevision: Int64 } } extension EquipoClient { public func stepLog(_ id: String) async throws -> StepLog { try await get("/steps/\(escape(id))/log") } func retryTask(_ id: String) async throws -> TaskAccepted { try await post("/tasks/\(escape(id))/retry", Self.empty()) } func recoverTask(_ id: String) async throws -> TaskAccepted { try await post("/tasks/\(escape(id))/recover", Self.empty()) } func resumeConnections() async throws -> ThinList { try await get("/connections") } func resumeConnection(_ connection: ResumeConnection) async throws { guard let spend = connection.spend, spend.unknown else { return } let _: EmptyResponse = try await post("/connections/\(escape(connection.id))/resume", try .json(["pause_revision": spend.pauseRevision])) } } enum StoppedWorkActions { static func retry(client: EquipoClient, task: String) async -> String? { do { _ = try await client.retryTask(task); return nil } catch let error as EquipoAPIError where error.status == 409 && error.code == "not_retryable" { return EquipoStrings.text("api.not_retryable") } catch { return EquipoStrings.text("card.failed") } } static func recover(client: EquipoClient, task: String) async -> String? { do { _ = try await client.recoverTask(task); return nil } catch { return EquipoStrings.text("card.failed") } } static func runNow(client: EquipoClient, task: String, run: TaskPause.RunNow) async -> String? { do { _ = try await client.runNow(task: task, step: run.step, revision: run.revision, offer: run.offer); return nil } catch let error as EquipoAPIError where error.status == 409 { switch error.code { case "offer_changed": return EquipoStrings.text("api.offer_changed") case "already_running": return EquipoStrings.text("api.already_running") case "hold_bar": return EquipoStrings.text("api.hold_bar") default: return EquipoStrings.text("card.failed") } } catch { return EquipoStrings.text("card.failed") } } static func logFailure(_ error: Error) -> String { if let error = error as? EquipoAPIError { if error == .undecodable { return EquipoStrings.text("read.failed") } if error == .offline { return EquipoStrings.text("card.offline") } } if error is URLError { return EquipoStrings.text("card.offline") } return EquipoStrings.text("read.failed_title") } static func connection(for step: ThinStep, in connections: [ResumeConnection]) -> ResumeConnection? { guard let runner = step.runnerId, let hold = step.hold else { return nil } return connections.first { $0.runnerId == runner && $0.localId == hold.connection && $0.provider == hold.provider && $0.spend?.unknown == true } } }