Files
hapi/ios/Packages/HapiKit/Sources/HapiProtocol/Models/AttachedJob.swift
T
weishu 7022eb0045 feat(ios): show session-attached jobs on the home list
iOS ignored SessionSummary.attachedJob, and its strict SessionPatch
decoder rejected every `attachedJob` SSE patch, so each job heartbeat
fell back to a REST list refetch while the row never showed the job.

- Decode AttachedJob on summaries (open-ended status, older rows without
  a watermark gate as 0) and the versioned `attachedJob` patch, applied
  only when strictly newer, as on web, so a lagged heartbeat cannot
  resurrect a cleared job. Full-session events keep the row's job.
- A running job counts as working on the presence badge even when the
  agent is idle or disconnected, keeps the session under Active only and
  out of Archived only (web #1404).
- The row's second line shows the job instead of the project:
  label · remaining or percent · elapsed, amber with "no heartbeat" after
  15 minutes. No progress bar, so row height is unchanged.
- Document the patch in the SSE client contract and the iOS display in
  the session jobs guide.
2026-11-10 17:38:30 +08:00

97 lines
3.5 KiB
Swift

import Foundation
/// `AttachedJobStatus` (`shared/src/schemas.ts`). Open-ended on decode so a
/// status this client does not know cannot fail the whole session list; only
/// `running` means live work.
public struct AttachedJobStatus: RawRepresentable, Codable, Hashable, Sendable {
public var rawValue: String
public init(rawValue: String) { self.rawValue = rawValue }
public static let running = AttachedJobStatus(rawValue: "running")
public static let completed = AttachedJobStatus(rawValue: "completed")
public static let failed = AttachedJobStatus(rawValue: "failed")
}
/// Session-attached long-running work (tiann/hapi#1404): the primary job on a
/// `SessionSummary`, independent of agent `active` / `thinking` — work that
/// outlives the agent. Mirrors `AttachedJobSchema`; supervisor-only fields
/// (`runId`, wake options) are not decoded.
public struct AttachedJob: Codable, Equatable, Sendable {
public var key: String
public var label: String
public var status: AttachedJobStatus
public var done: Double?
public var total: Double?
public var remaining: Double?
public var unit: String?
public var detail: String?
public var heartbeatAt: Int
public var startedAt: Int
public var updatedAt: Int
public init(
key: String,
label: String,
status: AttachedJobStatus = .running,
done: Double? = nil,
total: Double? = nil,
remaining: Double? = nil,
unit: String? = nil,
detail: String? = nil,
heartbeatAt: Int,
startedAt: Int,
updatedAt: Int
) {
self.key = key
self.label = label
self.status = status
self.done = done
self.total = total
self.remaining = remaining
self.unit = unit
self.detail = detail
self.heartbeatAt = heartbeatAt
self.startedAt = startedAt
self.updatedAt = updatedAt
}
/// No heartbeat for this long: the row turns amber but keeps its progress
/// (web `ATTACHED_JOB_STALE_MS`).
public static let staleAfterMs = 15 * 60 * 1000
public var isRunning: Bool { status == .running }
public func isStale(nowMs: Int) -> Bool {
nowMs - heartbeatAt > Self.staleAfterMs
}
/// Whole percent from `done`/`total`, capped at 100; `nil` without a
/// positive total. `remaining` takes precedence in labels (web
/// `formatAttachedJobProgress`), so callers check it first.
public var percent: Int? {
guard let done, let total, total > 0 else { return nil }
return min(100, Int((done / total * 100).rounded()))
}
}
/// Lettered elapsed time for list chrome, at most two units (web
/// `formatCompactElapsed`), never an ETA. Rows tick per minute, so anything
/// under a minute reads `<1m` rather than a seconds count that would freeze.
public func formatCompactElapsed(ms elapsedMs: Int) -> String {
guard elapsedMs >= 60_000 else { return "<1m" }
let totalMinutes = elapsedMs / 60_000
if totalMinutes < 60 { return "\(totalMinutes)m" }
let hours = totalMinutes / 60
let minutes = totalMinutes % 60
if hours < 24 { return minutes > 0 ? "\(hours)h \(minutes)m" : "\(hours)h" }
let days = hours / 24
let remainingHours = hours % 24
return remainingHours > 0 ? "\(days)d \(remainingHours)h" : "\(days)d"
}
/// Progress counts as the hub sent them: integers without a decimal point.
public func formatJobQuantity(_ value: Double) -> String {
if value.rounded() == value, abs(value) < 1e15 { return String(Int(value)) }
return String(value)
}