import Foundation /// One in-memory diagnostic entry. It never contains selected text or a translation. public struct DiagnosticEvent: Sendable, Equatable, Codable { /// When the event happened. public let timestamp: Date /// A short kind such as `translation`, `error` or `capture`. public let kind: String /// A duration in milliseconds, when relevant. public let durationMs: Int? /// A non-text detail such as a warning code or error kind. public let detail: String? /// Creates an event. public init(timestamp: Date, kind: String, durationMs: Int? = nil, detail: String? = nil) { self.timestamp = timestamp self.kind = kind self.durationMs = durationMs self.detail = detail } } /// A bounded in-memory diagnostics log (`docs/ARCHITECTURE.md` ยง15). public actor DiagnosticsStore { /// The maximum number of retained events. public static let capacity = 50 private var events: [DiagnosticEvent] = [] private let clock: @Sendable () -> Date /// Creates a store. public init(clock: @escaping @Sendable () -> Date = { Date() }) { self.clock = clock } /// Records one non-text event, dropping the oldest beyond the capacity. public func record(kind: String, durationMs: Int? = nil, detail: String? = nil) { events.append( DiagnosticEvent(timestamp: clock(), kind: kind, durationMs: durationMs, detail: detail) ) if events.count > Self.capacity { events.removeFirst(events.count - Self.capacity) } } /// The current events, oldest first. public func snapshot() -> [DiagnosticEvent] { events } /// Clears the in-memory list. public func clear() { events.removeAll() } }