import Foundation // Paused work (spec 2026-11-24-paused-work-design.md), the minimum the // apps need so a held step is never a false green: the web's pauseOf // (pause_view.go) for the reasons that carry on by themselves, a stopped // step, and the amber ones only the person can move. Times are this // device's own zone and locale; the words are the web's, by key // (check-parity.py holds them to copy.go). /// Why a task waits or stopped, and the one action. public struct TaskPause: Sendable, Equatable { /// paused (indigo, carries on by itself), warning (only you can move /// it) or negative (it failed). public var tone: StageTone /// The stepper's current stop: Paused, Needs you or Stopped. public var stop: String /// The lead's bold part and the rest of it; what happens next. public var strong: String public var rest: String public var next: String /// Run now, as the instance predicts it, for the task's creator. public var runNow: RunNow? public var note: String? public var canRetry = false public var canRecover = false public var settingsPath: String? public var settingsLabel: String? public var resumeStep: ThinStep? public struct RunNow: Sendable, Equatable { public var label: String public var step: String public var revision: Int64 /// What the button shows (the offer's connection, or "plain"): the /// instance refuses the tap if Run now would now do anything else. public var offer: String = "" } } extension TaskPause { /// The task's pause at now in zone, nil while nothing waits. mine: the /// person is the task's creator (only they are offered Run now). public init?(_ task: ThinTask, mine: Bool, now: Date = Date(), zone: TimeZone = .current, locale: Locale = .current) { if let triage = task.triage, ["retrying", "rebasing", "rechecking"].contains(triage.kind) || triage.kind == "waiting" && triage.reason == "merge_unborn_default" { return nil } let final = ["done", "cancelled"].contains(task.stage) guard !final, task.moving != true, task.stopping != true, task.copyHold != true, task.copyCanSkip != true else { return nil } let step = (task.steps ?? []).last(where: { !["preview", "research", "publish"].contains($0.specialist) }) if !task.needsYou.isEmpty && !["retry", "held"].contains(task.needsYou) { let key = "work.needs." + task.needsYou let lead = String(localized: String.LocalizationValue(key), bundle: .module) guard lead != key else { return nil } self.init(tone: .warning, stop: EquipoStrings.text("work.stage.needs_you"), lead: Self.format(lead), next: "") canRetry = mine && step?.state == "failed" && step?.terminated == true canRecover = mine && step?.state == "failed" && step?.reason == "machine_removed" && step?.terminated != true return } guard let step else { return nil } let words = TimeWords(now: now, zone: zone, locale: locale) if step.state == "failed" { // A runner too old for the step waits for its update (amber), // as on the web; anything else stopped, in red. The stop // reasons' words are the web's; the task page there says which. if ["runner_outdated", "runner_too_old", "copy_update_required", "research_unsupported"].contains(step.reason), let machine = step.runnerLabel, !machine.isEmpty { self.init(tone: .warning, stop: EquipoStrings.text("work.stage.needs_you"), lead: Self.format(EquipoStrings.text("work.pause.lead.too_old"), machine), next: EquipoStrings.text("work.pause.next.update")) if mine { settingsPath = "/conversation/settings/machines"; settingsLabel = EquipoStrings.text("work.pause.update_runner") } return } if step.reason == "machine_removed" && step.terminated != true { self.init(tone: .warning, stop: EquipoStrings.text("work.stage.needs_you"), lead: EquipoStrings.text("work.needs.machine_removed"), next: "") canRecover = mine return } var reason = step.reason if reason == "no_changes", (task.steps ?? []).prefix(while: { $0.id != step.id }).contains(where: { $0.pushed == true && !($0.candidateSha ?? "").isEmpty && ($0.specialist == "engineering" || $0.specialist == "copy" && $0.state == "done") }) { reason = "no_further_changes" } self.init(tone: .negative, stop: EquipoStrings.text("work.stage.stopped"), lead: Self.format(EquipoStrings.text("work.pause.lead.stopped"), words.clockOrDay(step.finishedAt ?? now), StoppedReason.words(reason)), next: EquipoStrings.text("work.pause.next.try_again")) canRetry = mine && step.terminated == true return } guard ["queued", "running"].contains(step.state) else { return nil } if step.paused?.offline == true { defer { if mine { settingsPath = "/conversation/settings/machines"; settingsLabel = EquipoStrings.text("work.pause.check_machine") } } guard let machine = step.runnerLabel, !machine.isEmpty else { self.init(tone: .paused, stop: EquipoStrings.text("work.stage.paused"), lead: "", next: "") return } let reason = String(format: EquipoStrings.text("work.pause.reason.offline"), machine) let lead = step.paused?.since.map { Self.format(EquipoStrings.text("work.pause.since"), words.clockOrDay($0), reason) } ?? Self.format(EquipoStrings.text("work.pause.moment"), reason) self.init(tone: .paused, stop: EquipoStrings.text("work.stage.paused"), lead: lead, next: String(format: EquipoStrings.text("work.pause.next.online"), machine)) return } guard let hold = step.hold else { return nil } let name = ProviderNames.name(hold.provider ?? "") let paused = EquipoStrings.text("work.stage.paused"), needs = EquipoStrings.text("work.stage.needs_you") switch hold.reason { case _ where hold.needsPerson == "no_connection" || hold.reason == "no_connection": self.init(tone: .warning, stop: needs, lead: EquipoStrings.text("work.pause.lead.no_connection"), next: EquipoStrings.text("work.pause.next.choose")) if mine { settingsPath = "/conversation/settings/connections/add"; settingsLabel = EquipoStrings.text("work.pause.connect") } case "spend_unknown": let next = String(format: EquipoStrings.text("work.pause.next.spend_unknown"), name, name) self.init(tone: .warning, stop: needs, lead: Self.format(EquipoStrings.text("work.pause.lead.spend_unknown"), name), next: next) if mine { resumeStep = step } return case _ where !(hold.needsPerson ?? "").isEmpty: self.init(tone: .warning, stop: needs, lead: Self.splitLead(HeldLead.words(hold, name: name, due: task.priority?.by, words: words)), next: EquipoStrings.text("work.pause.next.choose")) if hold.reason == "budget" { if mine { settingsPath = "/conversation/settings/connections"; settingsLabel = EquipoStrings.text("work.hold.raise_budget") } return } case "budget": self.init(tone: .paused, stop: paused, lead: Self.until(hold.wakeAt, since: step.paused?.since, reason: String(format: EquipoStrings.text("work.pause.reason.budget"), name), words: words), next: EquipoStrings.text("work.pause.next.budget")) if mine { settingsPath = "/conversation/settings/connections"; settingsLabel = EquipoStrings.text("work.hold.raise_budget") } return case "busy": self.init(tone: .paused, stop: paused, lead: Self.format(EquipoStrings.text("work.pause.moment"), String(format: EquipoStrings.text("work.pause.reason.busy"), name)), next: EquipoStrings.text("work.pause.next.busy")) return default: var reason = String(format: EquipoStrings.text("work.pause.reason.headroom_out"), name) if let used = step.paused?.usedPercent, used < 100 { reason = String(format: EquipoStrings.text("work.pause.reason.headroom_saving"), name, Int32(used)) } let at = hold.resetsAt ?? hold.wakeAt let next = at.map { $0 > now } == true ? EquipoStrings.text("work.pause.next.by_itself") : "" self.init(tone: .paused, stop: paused, lead: Self.until(at, since: step.paused?.since, reason: reason, words: words), next: next) } if mine, let offer = step.paused?.runNow { let label: String switch (offer.billing ?? "", offer.same ?? false) { case _ where offer.plain == true: // An older runner cannot be held to one connection: no promise, // and said when a connection that bills per use could take it. label = EquipoStrings.text("work.hold.run_now") if offer.mayBill == true { note = EquipoStrings.text("work.pause.run_now_may_bill") } case ("metered", _): label = String(format: EquipoStrings.text("work.pause.run_now_with"), offer.name ?? "") note = String(format: EquipoStrings.text("work.pause.run_now_metered"), offer.name ?? "") case (_, true): label = String(format: EquipoStrings.text("work.pause.run_now_on"), offer.name ?? "") if let left = offer.leftPercent, left > 0 { note = String(format: EquipoStrings.text("work.pause.run_now_last"), Int32(left)) } default: label = String(format: EquipoStrings.text("work.pause.run_now_with"), offer.name ?? "") note = String(format: EquipoStrings.text("work.pause.run_now_room"), offer.name ?? "") } runNow = RunNow(label: label, step: step.id, revision: hold.stepRevision, offer: offer.plain == true ? "plain" : offer.connection ?? "") } } static func splitLead(_ words: String) -> (String, String) { guard let colon = words.firstIndex(of: ":") else { return ("", words) } return (String(words[...colon]), String(words[words.index(after: colon)...])) } private init(tone: StageTone, stop: String, lead: (String, String), next: String) { self.init(tone: tone, stop: stop, strong: lead.0, rest: lead.1, next: next) } private init(tone: StageTone, stop: String, lead: String, next: String) { self.init(tone: tone, stop: stop, strong: "", rest: lead, next: next) } /// "Paused until 21:00 tonight (in 9 hours): …", else since when it /// was held, else for a moment. private static func until(_ at: Date?, since: Date?, reason: String, words: TimeWords) -> (String, String) { if let at, at > words.now { return format(EquipoStrings.text("work.pause.until"), words.when(at), words.inWords(at.timeIntervalSince(words.now)), reason) } if let since { return format(EquipoStrings.text("work.pause.since"), words.clockOrDay(since), reason) } return format(EquipoStrings.text("work.pause.moment"), reason) } /// The copy at key with args, split where its bold part ends: at its /// first colon (kept) or its first " (" (not kept), as pause.Lead does. static func format(_ template: String, _ args: String...) -> (String, String) { var cut = template.endIndex, keep = false if let colon = template.firstIndex(of: ":") { cut = colon; keep = true } if let paren = template.range(of: " (")?.lowerBound, paren < cut { cut = paren; keep = false } guard cut != template.endIndex else { return ("", String(format: template, arguments: args)) } let split = keep ? template.index(after: cut) : cut let head = String(template[.. String { switch provider { case "claude": return "Claude" case "codex": return "Codex" case "tensorx": return "TensorX" case "opencode_go": return "OpenCode Go" default: return provider.prefix(1).uppercased() + provider.dropFirst() } } } /// When paused work carries on, in words (internal/equipo/pause): the /// device's zone and locale, relative to now. struct TimeWords { let now: Date let zone: TimeZone let locale: Locale static let evening = 18 private var calendar: Calendar { var c = Calendar(identifier: .gregorian); c.timeZone = zone; return c } private func clock(_ at: Date) -> String { var style = Date.FormatStyle(date: .omitted, time: .shortened, locale: locale, calendar: calendar, timeZone: zone) style.timeZone = zone let hourPattern = DateFormatter.dateFormat(fromTemplate: "j", options: 0, locale: locale) ?? "" if hourPattern.contains("H") || hourPattern.contains("k") { style = style.hour(.twoDigits(amPM: .omitted)).minute(.twoDigits) } return at.formatted(style) } private func days(_ at: Date) -> Int { calendar.dateComponents([.day], from: calendar.startOfDay(for: now), to: calendar.startOfDay(for: at)).day ?? 0 } /// "today at 15:00", "21:00 tonight", "midnight", "tomorrow at 09:00", /// else the day and the time. func when(_ at: Date) -> String { let parts = calendar.dateComponents([.hour, .minute], from: at) switch days(at) { case 1 where parts.hour == 0 && parts.minute == 0: return EquipoStrings.text("time.midnight") case 0 where (parts.hour ?? 0) >= Self.evening: return String(format: EquipoStrings.text("time.tonight"), clock(at)) case 0: return String(format: EquipoStrings.text("time.today_at"), clock(at)) case 1: return String(format: EquipoStrings.text("time.tomorrow_at"), clock(at)) default: var style = Date.FormatStyle(locale: locale, calendar: calendar, timeZone: zone).weekday(.abbreviated) style.timeZone = zone return at.formatted(style) + " " + clock(at) } } /// "in 40 minutes", "in 9 hours", "in 2 days". func inWords(_ seconds: TimeInterval) -> String { let minutes = Int((seconds / 60).rounded(.up)) if minutes <= 1 { return EquipoStrings.text("time.in_minutes.one") } if minutes < 60 { return String(format: EquipoStrings.text("time.in_minutes"), Int32(minutes)) } if seconds < 24 * 3600 { let hours = Int((seconds / 3600).rounded()) return hours > 1 ? String(format: EquipoStrings.text("time.in_hours"), Int32(hours)) : EquipoStrings.text("time.in_hours.one") } let days = Int((seconds / 86400).rounded()) return days > 1 ? String(format: EquipoStrings.text("time.in_days"), Int32(days)) : EquipoStrings.text("time.in_days.one") } /// The clock today, the day and the clock before. func clockOrDay(_ at: Date) -> String { if days(at) == 0 { return clock(at) } var style = Date.FormatStyle(locale: locale, calendar: calendar, timeZone: zone).weekday(.abbreviated) style.timeZone = zone return at.formatted(style) + " " + clock(at) } }