CodexBar 是一个 macOS 菜单栏应用,用于查看本机 Codex 账号状态、额度、Token 用量、详细统计信息
Swift appkitcodexcodex-clideveloper-toolsmacosmacos-appmenu-bar-appopenai-codexproductivityswiftswiftuiusage-tracker
refactor: 统一历史数据存储与实时任务用量统计
Bob907c8053e646
diff --git a/CodexBar.xcodeproj/project.pbxproj b/CodexBar.xcodeproj/project.pbxprojindex d57e550..7831d95 100644--- a/CodexBar.xcodeproj/project.pbxproj+++ b/CodexBar.xcodeproj/project.pbxproj@@ -369,17 +369,17 @@/* End PBXProject section *//* Begin PBXResourcesBuildPhase section */- F10000000000000000000009 /* Resources */ = {+ C4A0FD932FD9302E000263A7 /* Resources */ = {isa = PBXResourcesBuildPhase;files = ();};- C4A0FD932FD9302E000263A7 /* Resources */ = {+ D10000000000000000000007 /* Resources */ = {isa = PBXResourcesBuildPhase;files = ();};- D10000000000000000000007 /* Resources */ = {+ F10000000000000000000009 /* Resources */ = {isa = PBXResourcesBuildPhase;files = ();diff --git a/CodexBar/Models/ActivityAggregate.swift b/CodexBar/Models/ActivityDataAggregate.swiftsimilarity index 65%rename from CodexBar/Models/ActivityAggregate.swiftrename to CodexBar/Models/ActivityDataAggregate.swiftindex 4f54dca..0256a92 100644--- a/CodexBar/Models/ActivityAggregate.swift+++ b/CodexBar/Models/ActivityDataAggregate.swift@@ -3,39 +3,26 @@ import Foundation// MARK: - 每日事件聚合/// ID 保留期限只决定最终存储形态, 不能影响聚合结果-nonisolated enum ActivityIdentifiers {+nonisolated enum ActivityDataIdentifiers {case retainedcase compacted}/// 从原始事件生成每日聚合的纯内存累加器/// 全量和增量路径都收集 ID, 只有 finalize 时才按保留策略决定是否落盘-nonisolated struct ActivityAccumulator {- private var aggregate: ActivityAggregate+nonisolated struct ActivityDataAccumulator {+ private var aggregate: ActivityDataAggregateprivate var threadIDs: Set<String> = []private var turnIDs: Set<String> = []private var baseThreadCount = 0private var baseTurnCount = 0private var restoresCompactedIdentity = false- init(- rebuilding date: String,- generationID: String?,- eventCountAvailability: ActivityCountAvailability- ) {- aggregate = ActivityAggregate(- date: date,- generationID: generationID,- eventCountAvailability: eventCountAvailability- )+ init(rebuilding date: String) {+ aggregate = ActivityDataAggregate(date: date)}- init(- appending aggregate: ActivityAggregate,- generationID: String?- ) {- var aggregate = aggregate- aggregate.generationID = generationID+ init(appending aggregate: ActivityDataAggregate) {self.aggregate = aggregatethreadIDs = Set(aggregate.threadIDs ?? [])turnIDs = Set(aggregate.turnIDs ?? [])@@ -44,15 +31,17 @@ nonisolated struct ActivityAccumulator {restoresCompactedIdentity = !aggregate.supportsIncrementalAggregation}- mutating func record(_ event: ActivityRecord) {+ mutating func record(_ event: ActivityDataRecord) {Self.increment(&aggregate.eventCount)+ // 一个命令 item 可包含多个动作, 重复类型仍表示独立操作+ let toolCallCount = max(1, event.commandActionTypes?.count ?? 0)switch event.eventKind {case .turnStarted: Self.increment(&aggregate.turnStartedCount)case .turnCompleted: Self.increment(&aggregate.turnCompletedCount)case .turnAborted: Self.increment(&aggregate.turnAbortedCount)- case .toolStarted: Self.increment(&aggregate.toolStartedCount)- case .toolCompleted: Self.increment(&aggregate.toolCompletedCount)+ case .toolStarted: Self.increment(&aggregate.toolStartedCount, by: toolCallCount)+ case .toolCompleted: Self.increment(&aggregate.toolCompletedCount, by: toolCallCount)case .approvalRequested: Self.increment(&aggregate.approvalRequestedCount)case .compactionStarted: Self.increment(&aggregate.compactionStartedCount)case .compactionCompleted: Self.increment(&aggregate.compactionCompletedCount)@@ -78,7 +67,7 @@ nonisolated struct ActivityAccumulator {}}- mutating func restoreIdentity(from event: ActivityRecord) {+ mutating func restoreIdentity(from event: ActivityDataRecord) {if let threadID = event.threadID {threadIDs.insert(threadID)}@@ -87,7 +76,7 @@ nonisolated struct ActivityAccumulator {}}- func finalized(identifierStorage: ActivityIdentifiers) -> ActivityAggregate {+ func finalized(identifierStorage: ActivityDataIdentifiers) -> ActivityDataAggregate {var aggregate = aggregateswitch identifierStorage {case .retained:@@ -104,8 +93,8 @@ nonisolated struct ActivityAccumulator {return aggregate}- private static func increment(_ count: inout Int?) {- count = (count ?? 0) + 1+ private static func increment(_ count: inout Int?, by amount: Int = 1) {+ count = (count ?? 0) + amount}private static func normalizedIdentifiers(_ identifiers: Set<String>) -> [String] {@@ -113,44 +102,14 @@ nonisolated struct ActivityAccumulator {}}-/// 全量重放时保留每个活动计数字段原有的可用性-nonisolated struct ActivityCountAvailability {- static let all = ActivityCountAvailability(- includesEventCount: true,- events: Set(ActivityEventKind.allCases)- )-- private let includesEventCount: Bool- private let events: Set<ActivityEventKind>-- init(aggregate: ActivityAggregate) {- includesEventCount = aggregate.eventCount != nil- events = Set(ActivityEventKind.allCases.filter { aggregate.eventCount(for: $0) != nil })- }-- private init(includesEventCount: Bool, events: Set<ActivityEventKind>) {- self.includesEventCount = includesEventCount- self.events = events- }-- var initialEventCount: Int? {- includesEventCount ? 0 : nil- }-- func initialCount(for event: ActivityEventKind) -> Int? {- events.contains(event) ? 0 : nil- }-}--/// Aggregates/activity.jsonl 中的持久化聚合行, 同时兼容保留 ID 和只保留计数两种形态-nonisolated struct ActivityAggregate: Codable, Equatable {- static let currentVersion = 1- var version = Self.currentVersion- var aggregationVersion = AggregationVersion.activity- var sourceCheckpoint: ActivitySourceCheckpoint?+/// 每日聚合按保留期压缩去重身份, 不改变计数含义+nonisolated struct ActivityDataAggregate: Codable, Equatable {+ static let schemaVersion = 1+ var schemaVersion = Self.schemaVersion+ var aggregationVersion = AggregationVersion.activityData+ var position: Int64 = 0let date: String- var generationID: String?var eventCount: Int?var turnStartedCount: Int?var turnCompletedCount: Int?@@ -174,24 +133,19 @@ nonisolated struct ActivityAggregate: Codable, Equatable {threadCount == nil && turnCount == nil}- init(- date: String,- generationID: String? = nil,- eventCountAvailability: ActivityCountAvailability = .all- ) {+ init(date: String) {self.date = date- self.generationID = generationID- eventCount = eventCountAvailability.initialEventCount- turnStartedCount = eventCountAvailability.initialCount(for: .turnStarted)- turnCompletedCount = eventCountAvailability.initialCount(for: .turnCompleted)- turnAbortedCount = eventCountAvailability.initialCount(for: .turnAborted)- toolStartedCount = eventCountAvailability.initialCount(for: .toolStarted)- toolCompletedCount = eventCountAvailability.initialCount(for: .toolCompleted)- approvalRequestedCount = eventCountAvailability.initialCount(for: .approvalRequested)- compactionStartedCount = eventCountAvailability.initialCount(for: .compactionStarted)- compactionCompletedCount = eventCountAvailability.initialCount(for: .compactionCompleted)- subagentStartedCount = eventCountAvailability.initialCount(for: .subagentStarted)- subagentEndedCount = eventCountAvailability.initialCount(for: .subagentEnded)+ eventCount = 0+ turnStartedCount = 0+ turnCompletedCount = 0+ turnAbortedCount = 0+ toolStartedCount = 0+ toolCompletedCount = 0+ approvalRequestedCount = 0+ compactionStartedCount = 0+ compactionCompletedCount = 0+ subagentStartedCount = 0+ subagentEndedCount = 0threadCount = nilturnCount = nilprojectCounts = [:]@@ -202,13 +156,15 @@ nonisolated struct ActivityAggregate: Codable, Equatable {init(from decoder: Decoder) throws {let container = try decoder.container(keyedBy: CodingKeys.self)- version = try container.decode(Int.self, forKey: .version)- try StorageVersion.require(version, current: Self.currentVersion, name: "ActivityAggregate")- sourceCheckpoint = try container.decodeIfPresent(ActivitySourceCheckpoint.self, forKey: .sourceCheckpoint)+ schemaVersion = try container.decode(Int.self, forKey: .schemaVersion)+ try StorageVersion.require(schemaVersion, current: Self.schemaVersion, name: "ActivityDataAggregate")aggregationVersion = try container.decode(Int.self, forKey: .aggregationVersion)- try AggregationVersion.require(aggregationVersion, current: AggregationVersion.activity, name: "Activity")+ try AggregationVersion.require(aggregationVersion, current: AggregationVersion.activityData, name: "activityData")+ position = try container.decode(Int64.self, forKey: .position)+ guard position >= 0 else {+ throw DecodingError.dataCorruptedError(forKey: .position, in: container, debugDescription: "Invalid aggregate position")+ }date = try container.decode(String.self, forKey: .date)- generationID = try container.decodeIfPresent(String.self, forKey: .generationID)eventCount = try container.decodeIfPresent(Int.self, forKey: .eventCount)turnStartedCount = try container.decodeIfPresent(Int.self, forKey: .turnStartedCount)turnCompletedCount = try container.decodeIfPresent(Int.self, forKey: .turnCompletedCount)@@ -247,15 +203,15 @@ nonisolated struct ActivityAggregate: Codable, Equatable {turnIDs = nil}- var metrics: ActivityMetrics {+ var metrics: ActivityDataMetrics {syncedAggregate.metrics}static func normalized(- aggregates: [ActivityAggregate],+ aggregates: [ActivityDataAggregate],today: Date = Date(),calendar: Calendar = .current- ) -> [ActivityAggregate] {+ ) -> [ActivityDataAggregate] {let retentionCutoffDate = HistoryStorage.retentionCutoffDate(today: today, calendar: calendar)let identifierCutoffDate = HistoryStorage.identifierRetentionCutoffDate(today: today, calendar: calendar)@@ -273,41 +229,10 @@ nonisolated struct ActivityAggregate: Codable, Equatable {.sorted { $0.date < $1.date }}- static func encodeJSONLines(_ aggregates: [ActivityAggregate]) throws -> Data {- try aggregates.reduce(into: Data()) { result, aggregate in- try result.append(aggregate.jsonLineData())- }- }-- func jsonLineData() throws -> Data {- try JSONLines.stableEncoder.encode(self) + Data([JSONLines.newlineByte])- }-- var eventCountAvailability: ActivityCountAvailability {- ActivityCountAvailability(aggregate: self)- }-- func eventCount(for event: ActivityEventKind) -> Int? {- switch event {- case .turnStarted: turnStartedCount- case .turnCompleted: turnCompletedCount- case .turnAborted: turnAbortedCount- case .toolStarted: toolStartedCount- case .toolCompleted: toolCompletedCount- case .approvalRequested: approvalRequestedCount- case .compactionStarted: compactionStartedCount- case .compactionCompleted: compactionCompletedCount- case .subagentStarted: subagentStartedCount- case .subagentEnded: subagentEndedCount- }- }-- var syncedAggregate: SyncedActivity {- SyncedActivity(+ var syncedAggregate: SyncedActivityData {+ SyncedActivityData(aggregationVersion: aggregationVersion,- sourceCheckpoint: sourceCheckpoint,date: date,- generationID: generationID,eventCount: eventCount,turnStartedCount: turnStartedCount,turnCompletedCount: turnCompletedCount,@@ -349,9 +274,8 @@ nonisolated struct ActivityAggregate: Codable, Equatable {}private enum CodingKeys: String, CodingKey {- case version, aggregationVersion, sourceCheckpoint+ case schemaVersion, aggregationVersion, positioncase date- case generationIDcase eventCountcase turnStartedCountcase turnCompletedCountdiff --git a/CodexBar/Models/ActivityEventKind.swift b/CodexBar/Models/ActivityDataEventKind.swiftsimilarity index 85%rename from CodexBar/Models/ActivityEventKind.swiftrename to CodexBar/Models/ActivityDataEventKind.swiftindex acc51fc..a3df946 100644--- a/CodexBar/Models/ActivityEventKind.swift+++ b/CodexBar/Models/ActivityDataEventKind.swift@@ -1,5 +1,5 @@/// app-server 事件映射到业务统计的生命周期阶段, 持久化直接使用枚举名称-nonisolated enum ActivityEventKind: String, CaseIterable, Hashable {+nonisolated enum ActivityDataEventKind: String, CaseIterable, Hashable {case turnStartedcase toolStartedcase toolCompleteddiff --git a/CodexBar/Models/ActivityRecord.swift b/CodexBar/Models/ActivityDataRecord.swiftsimilarity index 84%rename from CodexBar/Models/ActivityRecord.swiftrename to CodexBar/Models/ActivityDataRecord.swiftindex 848df6d..d345e8a 100644--- a/CodexBar/Models/ActivityRecord.swift+++ b/CodexBar/Models/ActivityDataRecord.swift@@ -3,7 +3,7 @@ import Foundation// MARK: - 归一化活动事件/// 事件来源的本地归一化分类, 不保留原始 context 内容-nonisolated enum ActivityOrigin: String, Codable, Sendable {+nonisolated enum ActivityDataOrigin: String, Codable, Sendable {case maincase autoReviewcase auxiliary@@ -17,11 +17,11 @@ nonisolated enum ActivityOrigin: String, Codable, Sendable {}/// app-server 的最小统计事件, 在边界归一化来源与可选元数据-nonisolated struct ActivityRecord: Codable, Equatable {+nonisolated struct ActivityDataRecord: Codable, Equatable {let id: String?let timestamp: Datelet name: String- let origin: ActivityOrigin+ let origin: ActivityDataOriginlet cwd: String?let toolName: String?let commandActionTypes: [String]?@@ -29,13 +29,13 @@ nonisolated struct ActivityRecord: Codable, Equatable {let effort: String?let threadID: String?let turnID: String?- let agentID: String?- var context: ActivityContext?+ let agentThreadID: String?+ var context: ActivityDataContext?init(timestamp: Date,name: String,- origin: ActivityOrigin,+ origin: ActivityDataOrigin,cwd: String?,toolName: String?,commandActionTypes: [String]? = nil,@@ -43,7 +43,7 @@ nonisolated struct ActivityRecord: Codable, Equatable {effort: String?,threadID: String?,turnID: String?,- agentID: String?,+ agentThreadID: String?,id: String? = nil) {self.id = id@@ -57,21 +57,21 @@ nonisolated struct ActivityRecord: Codable, Equatable {self.effort = effortself.threadID = threadIDself.turnID = turnID- self.agentID = agentID+ self.agentThreadID = agentThreadID}init(from decoder: Decoder) throws {let container = try decoder.container(keyedBy: CodingKeys.self)let timestamp = try container.decode(Date.self, forKey: .timestamp)let phase = try container.decode(String.self, forKey: .name)- guard let event = ActivityEventKind(rawValue: phase) else {+ guard let event = ActivityDataEventKind(rawValue: phase) else {throw DecodingError.dataCorruptedError(forKey: .name, in: container, debugDescription: "Unknown activity phase")}let name = event.rawValue- context = try container.decodeIfPresent(ActivityContext.self, forKey: .context)+ context = try container.decodeIfPresent(ActivityDataContext.self, forKey: .context)id = try container.decodeIfPresent(String.self, forKey: .id)- let decodedOrigin = (try? container.decode(ActivityOrigin.self, forKey: .origin)) ?? .unknown+ let decodedOrigin = (try? container.decode(ActivityDataOrigin.self, forKey: .origin)) ?? .unknownlet decodedModel = Self.string(from: container, key: .model)self.timestamp = timestamp@@ -84,11 +84,11 @@ nonisolated struct ActivityRecord: Codable, Equatable {effort = Self.string(from: container, key: .effort)threadID = Self.string(from: container, key: .threadID)turnID = Self.string(from: container, key: .turnID)- agentID = Self.string(from: container, key: .agentID)+ agentThreadID = Self.string(from: container, key: .agentThreadID)}- var eventKind: ActivityEventKind? {- ActivityEventKind(eventName: name)+ var eventKind: ActivityDataEventKind? {+ ActivityDataEventKind(eventName: name)}var projectDisplayName: String? {@@ -129,7 +129,7 @@ nonisolated struct ActivityRecord: Codable, Equatable {try container.encodeIfPresent(effort, forKey: .effort)try container.encodeIfPresent(threadID, forKey: .threadID)try container.encodeIfPresent(turnID, forKey: .turnID)- try container.encodeIfPresent(agentID, forKey: .agentID)+ try container.encodeIfPresent(agentThreadID, forKey: .agentThreadID)try container.encodeIfPresent(context, forKey: .context)}@@ -145,13 +145,13 @@ nonisolated struct ActivityRecord: Codable, Equatable {case effortcase threadIDcase turnID- case agentID+ case agentThreadIDcase context}}/// 任务归属与生命周期事实, 时间统一为 Date 和秒, method 仅用于来源诊断-nonisolated struct ActivityContext: Codable, Equatable {+nonisolated struct ActivityDataContext: Codable, Equatable {let method: Stringlet threadID: Stringvar turnID: String?diff --git a/CodexBar/Models/ActivityDataSyncRecord.swift b/CodexBar/Models/ActivityDataSyncRecord.swiftnew file mode 100644index 0000000..ae26f59--- /dev/null+++ b/CodexBar/Models/ActivityDataSyncRecord.swift@@ -0,0 +1,208 @@+import Foundation++nonisolated struct SyncedActivityData: Codable, Equatable {+ static let schemaVersion = 1+ var schemaVersion = Self.schemaVersion+ var aggregationVersion = AggregationVersion.activityData++ let date: String+ var eventCount: Int?+ var turnStartedCount: Int?+ var turnCompletedCount: Int?+ var turnAbortedCount: Int?+ var toolStartedCount: Int?+ var toolCompletedCount: Int?+ var approvalRequestedCount: Int?+ var compactionStartedCount: Int?+ var compactionCompletedCount: Int?+ var subagentStartedCount: Int?+ var subagentEndedCount: Int?+ var threadCount: Int?+ var turnCount: Int?+ var projectCounts: [String: Int]+ var modelCounts: [String: Int]++ var metrics: ActivityDataMetrics {+ ActivityDataMetrics(+ startDate: date,+ threadCount: threadCount,+ turnCount: turnCount,+ toolStartedCount: toolStartedCount ?? 0,+ toolCompletedCount: toolCompletedCount ?? 0,+ approvalRequestedCount: approvalRequestedCount ?? 0,+ compactionStartedCount: compactionStartedCount ?? 0,+ compactionCompletedCount: compactionCompletedCount ?? 0,+ subagentStartedCount: subagentStartedCount ?? 0,+ subagentEndedCount: subagentEndedCount ?? 0,+ modelCounts: modelCounts,+ turnAbortedCount: turnAbortedCount ?? 0+ )+ }++ private enum CodingKeys: String, CodingKey {+ case schemaVersion, aggregationVersion+ case date+ case eventCount+ case turnStartedCount+ case turnCompletedCount+ case turnAbortedCount+ case toolStartedCount+ case toolCompletedCount+ case approvalRequestedCount+ case compactionStartedCount+ case compactionCompletedCount+ case subagentStartedCount+ case subagentEndedCount+ case threadCount+ case turnCount+ case projectCounts+ case modelCounts+ }+}++extension SyncedActivityData {+ nonisolated init(from decoder: Decoder) throws {+ let container = try decoder.container(keyedBy: CodingKeys.self)+ try self.init(+ schemaVersion: container.decode(Int.self, forKey: .schemaVersion),+ aggregationVersion: container.decode(Int.self, forKey: .aggregationVersion),+ date: container.decode(String.self, forKey: .date), countsFrom: decoder+ )+ }++ nonisolated init(schemaVersion: Int, aggregationVersion: Int, date: String, countsFrom decoder: Decoder) throws {+ self.schemaVersion = schemaVersion+ try StorageVersion.require(schemaVersion, current: Self.schemaVersion, name: "ActivityDataAggregate")+ self.aggregationVersion = aggregationVersion+ try AggregationVersion.require(aggregationVersion, current: AggregationVersion.activityData, name: "activityData")+ self.date = date+ let container = try decoder.container(keyedBy: CodingKeys.self)+ eventCount = try container.decodeIfPresent(Int.self, forKey: .eventCount)+ turnStartedCount = try container.decodeIfPresent(Int.self, forKey: .turnStartedCount)+ turnCompletedCount = try container.decodeIfPresent(Int.self, forKey: .turnCompletedCount)+ turnAbortedCount = try container.decodeIfPresent(Int.self, forKey: .turnAbortedCount)+ toolStartedCount = try container.decodeIfPresent(Int.self, forKey: .toolStartedCount)+ toolCompletedCount = try container.decodeIfPresent(Int.self, forKey: .toolCompletedCount)+ approvalRequestedCount = try container.decodeIfPresent(Int.self, forKey: .approvalRequestedCount)+ compactionStartedCount = try container.decodeIfPresent(Int.self, forKey: .compactionStartedCount)+ compactionCompletedCount = try container.decodeIfPresent(Int.self, forKey: .compactionCompletedCount)+ subagentStartedCount = try container.decodeIfPresent(Int.self, forKey: .subagentStartedCount)+ subagentEndedCount = try container.decodeIfPresent(Int.self, forKey: .subagentEndedCount)+ threadCount = try container.decodeIfPresent(Int.self, forKey: .threadCount)+ turnCount = try container.decodeIfPresent(Int.self, forKey: .turnCount)+ projectCounts = try container.decodeIfPresent([String: Int].self, forKey: .projectCounts) ?? [:]+ modelCounts = try container.decodeIfPresent([String: Int].self, forKey: .modelCounts) ?? [:]+ }+}++nonisolated extension SyncedActivityData {+ func encodeCounts(to encoder: Encoder) throws {+ var container = encoder.container(keyedBy: CodingKeys.self)+ try container.encode(eventCount, forKey: .eventCount)+ try container.encode(turnStartedCount, forKey: .turnStartedCount)+ try container.encode(turnCompletedCount, forKey: .turnCompletedCount)+ try container.encode(turnAbortedCount, forKey: .turnAbortedCount)+ try container.encode(toolStartedCount, forKey: .toolStartedCount)+ try container.encode(toolCompletedCount, forKey: .toolCompletedCount)+ try container.encode(approvalRequestedCount, forKey: .approvalRequestedCount)+ try container.encode(compactionStartedCount, forKey: .compactionStartedCount)+ try container.encode(compactionCompletedCount, forKey: .compactionCompletedCount)+ try container.encode(subagentStartedCount, forKey: .subagentStartedCount)+ try container.encode(subagentEndedCount, forKey: .subagentEndedCount)+ try container.encode(threadCount, forKey: .threadCount)+ try container.encode(turnCount, forKey: .turnCount)+ try container.encode(projectCounts, forKey: .projectCounts)+ try container.encode(modelCounts, forKey: .modelCounts)+ }++ var counts: some Encodable {+ Counts(aggregate: self)+ }++ private struct Counts: Encodable {+ let aggregate: SyncedActivityData++ func encode(to encoder: Encoder) throws {+ try aggregate.encodeCounts(to: encoder)+ }+ }+}++nonisolated struct ActivityDataSyncRecord: SyncCacheRecord {+ let deviceID: String+ let activityData: SyncedActivityData+ var updatedAt: Date?+ let recordName: String++ var id: String {+ recordName+ }++ var date: String {+ activityData.date+ }++ init(deviceID: String, activityData: SyncedActivityData, updatedAt: Date? = nil, recordName: String) throws {+ guard !deviceID.isEmpty, HistoryStorage.isValidDateKey(activityData.date),+ recordName == Self.recordName(deviceID: deviceID, date: activityData.date) else {+ throw ActivityDataSyncError.invalidRecordIdentity+ }+ self.deviceID = deviceID+ self.activityData = activityData+ self.updatedAt = updatedAt+ self.recordName = recordName+ }++ init(from decoder: Decoder) throws {+ let container = try decoder.container(keyedBy: CodingKeys.self)+ do {+ try self.init(+ deviceID: container.decode(String.self, forKey: .deviceID),+ activityData: SyncedActivityData(+ schemaVersion: container.decode(Int.self, forKey: .schemaVersion),+ aggregationVersion: container.decode(Int.self, forKey: .aggregationVersion),+ date: container.decode(String.self, forKey: .date),+ countsFrom: container.superDecoder(forKey: .activityData)+ ),+ updatedAt: container.decodeIfPresent(Date.self, forKey: .updatedAt),+ recordName: container.decode(String.self, forKey: .recordName)+ )+ } catch let error as ActivityDataSyncError {+ throw DecodingError.dataCorrupted(.init(+ codingPath: decoder.codingPath,+ debugDescription: "Invalid activity record identity",+ underlyingError: error+ ))+ }+ }++ func encode(to encoder: Encoder) throws {+ var container = encoder.container(keyedBy: CodingKeys.self)+ try container.encode(activityData.schemaVersion, forKey: .schemaVersion)+ try container.encode(activityData.aggregationVersion, forKey: .aggregationVersion)+ try container.encode(deviceID, forKey: .deviceID)+ try container.encode(date, forKey: .date)+ try container.encode(recordName, forKey: .recordName)+ try container.encode(updatedAt, forKey: .updatedAt)+ try activityData.encodeCounts(to: container.superEncoder(forKey: .activityData))+ }++ private enum CodingKeys: String, CodingKey {+ case schemaVersion, aggregationVersion, deviceID, date, recordName, updatedAt, activityData+ }++ static func recordName(deviceID: String, date: String) -> String {+ "activityData_\(deviceID)_\(date)"+ }+}++nonisolated enum ActivityDataSyncError: LocalizedError {+ case invalidRecordIdentity++ var errorDescription: String? {+ switch self {+ case .invalidRecordIdentity:+ String(localized: "sync.error.activity-data-identity")+ }+ }+}diff --git a/CodexBar/Models/ActivityInput.swift b/CodexBar/Models/ActivityInput.swiftindex 9e13a78..73b1ccc 100644--- a/CodexBar/Models/ActivityInput.swift+++ b/CodexBar/Models/ActivityInput.swift@@ -63,11 +63,17 @@ nonisolated struct ActivityInput {var status: ActivityThreadStatus?var startedAt: Date?var completedAt: Date?- var tokenUsage: ActivityTokenUpdate?+ var tokenUsage: TokenUsageUpdate?var commandActions: [ActivityItem.CommandAction]?}}+nonisolated struct TokenUsageUpdate: Codable, Equatable {+ let total: TokenUsage+ let last: TokenUsage+ var modelContextWindow: Int64?+}+nonisolated enum ActivityTurnStatus: String, Codable {case running, completed, failed, interrupted, unknownvar isTerminal: Bool {@@ -81,8 +87,8 @@ nonisolated struct ActivityThread {var model: String?var reasoningEffort: String?var status: ActivityThreadStatus?- var origin: ActivityOrigin = .unknown- var parentID: String?+ var origin: ActivityDataOrigin = .unknown+ var parentThreadID: String?var createdAt: Date?}@@ -143,11 +149,6 @@ nonisolated struct ActivityItem {}}-nonisolated struct ActivityTokenUpdate {- let total: TokenUsage- let last: TokenUsage-}-nonisolated struct ActivityLiveContext {enum InputMode { case form, external, verification, service }enum HookOutcome: String { case completed, failed, blocked, stopped, unknown }diff --git a/CodexBar/Models/ActivityModels.swift b/CodexBar/Models/ActivityModels.swiftindex 20e3f82..6ec802d 100644--- a/CodexBar/Models/ActivityModels.swift+++ b/CodexBar/Models/ActivityModels.swift@@ -71,7 +71,7 @@ nonisolated struct ActivityTermination: Equatable, Identifiable {var isFailure = false}-/// 实时越过静默阈值时交给通知服务的最小信息, 不包含原始 session 或 turn ID+/// 实时越过静默阈值时交给通知服务的最小信息, 不包含原始 thread 或 turn IDnonisolated struct ProtectionNotice: Equatable, Sendable {let taskID: UUIDlet attemptID: UUID@@ -202,7 +202,7 @@ nonisolated enum CodexPrimaryActivity: Equatable {case idle}-/// 只有 live 事件 或 session 生命周期会发布 transition, bootstrap 永远不会触发历史通知+/// 只有 live 事件 或 turn 生命周期会发布 transition, bootstrap 永远不会触发历史通知nonisolated enum ActivityTransition: Equatable {case waitingApproval(ActivityTaskSnapshot)case completed(ActivityCompletion)diff --git a/CodexBar/Models/ActivitySourceCheckpoint.swift b/CodexBar/Models/ActivitySourceCheckpoint.swiftdeleted file mode 100644index 6cb87ee..0000000--- a/CodexBar/Models/ActivitySourceCheckpoint.swift+++ /dev/null@@ -1,48 +0,0 @@-import CryptoKit-import Foundation--/// 检查点描述原始日志前缀, 不使用统计计数判断来源覆盖-nonisolated struct ActivitySourceCheckpoint: Codable, Equatable {- let byteCount: UInt64- let digest: String- var aggregationRanges: [AggregationRange] = []-- private enum CodingKeys: String, CodingKey { case byteCount, digest, aggregationRanges }-- init(byteCount: UInt64, digest: String) {- self.byteCount = byteCount- self.digest = digest- }-- init(from decoder: Decoder) throws {- let values = try decoder.container(keyedBy: CodingKeys.self)- byteCount = try values.decode(UInt64.self, forKey: .byteCount)- digest = try values.decode(String.self, forKey: .digest)- aggregationRanges = try values.decodeIfPresent([AggregationRange].self, forKey: .aggregationRanges) ?? []- try AggregationRange.validate(aggregationRanges, end: byteCount, current: AggregationVersion.activity)- guard byteCount > 0, digest.count == 64, digest.allSatisfy({ "0123456789abcdef".contains($0) }) else {- throw StorageCompatibilityError.incompleteSource- }- }-- func matchesSource(_ other: Self?) -> Bool {- guard let other else { return false }- return byteCount == other.byteCount && digest == other.digest- }-- static func read(at url: URL, byteCount: UInt64) throws -> Self {- let handle = try FileHandle(forReadingFrom: url)- defer { try? handle.close() }- var remaining = byteCount- var hash = SHA256()- while remaining > 0 {- try Task.checkCancellation()- guard let chunk = try handle.read(upToCount: Int(min(remaining, 64 * 1024))), !chunk.isEmpty else {- throw StorageCompatibilityError.incompleteSource- }- hash.update(data: chunk)- remaining -= UInt64(chunk.count)- }- return Self(byteCount: byteCount, digest: hash.finalize().map { String(format: "%02x", $0) }.joined())- }-}diff --git a/CodexBar/Models/ActivitySyncRecord.swift b/CodexBar/Models/ActivitySyncRecord.swiftdeleted file mode 100644index f2eb593..0000000--- a/CodexBar/Models/ActivitySyncRecord.swift+++ /dev/null@@ -1,169 +0,0 @@-import Foundation--nonisolated struct SyncedActivity: Codable, Equatable {- static let currentVersion = 1- var version = Self.currentVersion- var aggregationVersion = AggregationVersion.activity- var sourceCheckpoint: ActivitySourceCheckpoint?-- let date: String- var generationID: String?- var eventCount: Int?- var turnStartedCount: Int?- var turnCompletedCount: Int?- var turnAbortedCount: Int?- var toolStartedCount: Int?- var toolCompletedCount: Int?- var approvalRequestedCount: Int?- var compactionStartedCount: Int?- var compactionCompletedCount: Int?- var subagentStartedCount: Int?- var subagentEndedCount: Int?- var threadCount: Int?- var turnCount: Int?- var projectCounts: [String: Int]- var modelCounts: [String: Int]-- var metrics: ActivityMetrics {- ActivityMetrics(- startDate: date,- threadCount: threadCount,- turnCount: turnCount,- toolStartedCount: toolStartedCount ?? 0,- toolCompletedCount: toolCompletedCount ?? 0,- approvalRequestedCount: approvalRequestedCount ?? 0,- compactionStartedCount: compactionStartedCount ?? 0,- compactionCompletedCount: compactionCompletedCount ?? 0,- subagentStartedCount: subagentStartedCount ?? 0,- subagentEndedCount: subagentEndedCount ?? 0,- modelCounts: modelCounts,- turnAbortedCount: turnAbortedCount ?? 0- )- }-- func jsonLineData() throws -> Data {- try JSONLines.stableEncoder.encode(self) + Data([JSONLines.newlineByte])- }-- func requiredGenerationID() throws -> String {- guard let generationID, !generationID.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else {- throw ActivitySyncError.missingGeneration(date)- }- return generationID- }-- func matchesRemoteSource(_ remote: SyncedActivity) -> Bool {- generationID != nil && generationID == remote.generationID- }-- private enum CodingKeys: String, CodingKey {- case version, aggregationVersion, sourceCheckpoint- case date- case generationID- case eventCount- case turnStartedCount- case turnCompletedCount- case turnAbortedCount- case toolStartedCount- case toolCompletedCount- case approvalRequestedCount- case compactionStartedCount- case compactionCompletedCount- case subagentStartedCount- case subagentEndedCount- case threadCount- case turnCount- case projectCounts- case modelCounts- }-}--extension SyncedActivity {- nonisolated init(from decoder: Decoder) throws {- let container = try decoder.container(keyedBy: CodingKeys.self)- version = try container.decode(Int.self, forKey: .version)- try StorageVersion.require(version, current: Self.currentVersion, name: "ActivityAggregate")- sourceCheckpoint = try container.decodeIfPresent(ActivitySourceCheckpoint.self, forKey: .sourceCheckpoint)- aggregationVersion = try container.decode(Int.self, forKey: .aggregationVersion)- try AggregationVersion.require(aggregationVersion, current: AggregationVersion.activity, name: "Activity")- date = try container.decode(String.self, forKey: .date)- generationID = try container.decodeIfPresent(String.self, forKey: .generationID)- eventCount = try container.decodeIfPresent(Int.self, forKey: .eventCount)- turnStartedCount = try container.decodeIfPresent(Int.self, forKey: .turnStartedCount)- turnCompletedCount = try container.decodeIfPresent(Int.self, forKey: .turnCompletedCount)- turnAbortedCount = try container.decodeIfPresent(Int.self, forKey: .turnAbortedCount)- toolStartedCount = try container.decodeIfPresent(Int.self, forKey: .toolStartedCount)- toolCompletedCount = try container.decodeIfPresent(Int.self, forKey: .toolCompletedCount)- approvalRequestedCount = try container.decodeIfPresent(Int.self, forKey: .approvalRequestedCount)- compactionStartedCount = try container.decodeIfPresent(Int.self, forKey: .compactionStartedCount)- compactionCompletedCount = try container.decodeIfPresent(Int.self, forKey: .compactionCompletedCount)- subagentStartedCount = try container.decodeIfPresent(Int.self, forKey: .subagentStartedCount)- subagentEndedCount = try container.decodeIfPresent(Int.self, forKey: .subagentEndedCount)- threadCount = try container.decodeIfPresent(Int.self, forKey: .threadCount)- turnCount = try container.decodeIfPresent(Int.self, forKey: .turnCount)- projectCounts = try container.decodeIfPresent([String: Int].self, forKey: .projectCounts) ?? [:]- modelCounts = try container.decodeIfPresent([String: Int].self, forKey: .modelCounts) ?? [:]- }-}--nonisolated struct ActivitySyncRecord: Codable, Equatable, Identifiable {- let deviceID: String- let daily: SyncedActivity- var updatedAt: Date?- let recordName: String-- var id: String {- recordName- }-- var date: String {- daily.date- }-- init(deviceID: String, daily: SyncedActivity, updatedAt: Date? = nil, recordName: String) throws {- let generation = try daily.requiredGenerationID()- guard recordName == Self.recordName(deviceID: deviceID, date: daily.date, generation: generation) else {- throw ActivitySyncError.invalidRecordIdentity- }- self.deviceID = deviceID- self.daily = daily- self.updatedAt = updatedAt- self.recordName = recordName- }-- init(from decoder: Decoder) throws {- let container = try decoder.container(keyedBy: CodingKeys.self)- do {- try self.init(- deviceID: container.decode(String.self, forKey: .deviceID),- daily: container.decode(SyncedActivity.self, forKey: .daily),- updatedAt: container.decodeIfPresent(Date.self, forKey: .updatedAt),- recordName: container.decode(String.self, forKey: .recordName)- )- } catch let error as ActivitySyncError {- throw DecodingError.dataCorrupted(.init(- codingPath: decoder.codingPath,- debugDescription: "Invalid activity record identity",- underlyingError: error- ))- }- }-- static func recordName(deviceID: String, date: String, generation: String) -> String {- "\(deviceID)_\(date)_\(generation)"- }-}--nonisolated enum ActivitySyncError: LocalizedError {- case missingGeneration(String)- case invalidRecordIdentity-- var errorDescription: String? {- switch self {- case let .missingGeneration(date):- String(localized: "sync.error.activity-generation", defaultValue: "\(date)")- case .invalidRecordIdentity:- String(localized: "sync.error.activity-identity")- }- }-}diff --git a/CodexBar/Models/AppServerEventRecord.swift b/CodexBar/Models/AppServerEventRecord.swiftindex 23c404d..a09126a 100644--- a/CodexBar/Models/AppServerEventRecord.swift+++ b/CodexBar/Models/AppServerEventRecord.swift@@ -1,95 +1,66 @@import Foundation-/// 统一日志的版本与类型边界, Token 快照不会作为活动进入计数+/// 数据库仅保存活动事实和 Token 原始观测, 派生快照不能作为重建来源nonisolated struct AppServerEventRecord: Codable, Equatable {- enum Kind: String, Codable { case activity, tokenSnapshot, tokenObservation }+ enum Kind: String, Codable { case activityData, tokenUsage }- static let currentVersion = 1- let version: Int- let kind: Kind+ static let schemaVersion = 1+ let schemaVersion: Intlet recordedAt: Date- let context: ActivityContext?- private let activityPayload: ActivityRecord?- let token: TokenTurn?- let observation: TokenObservation?+ let context: ActivityDataContext?+ private let storedActivityData: ActivityDataRecord?+ let tokenUsage: TokenUsageObservation?++ var kind: Kind {+ storedActivityData == nil ? .tokenUsage : .activityData+ }var deduplicationID: String? {- if let id = activityPayload?.id {- return "activity:" + id+ if let id = storedActivityData?.id {+ return "activityData:" + id}- if let observation {- return "token:\(observation.turn.id):\(observation.streamID):\(observation.sequence)"+ if let tokenUsage {+ return "tokenUsage:" + tokenUsage.id}return nil}- var activity: ActivityRecord? {- guard var activity = activityPayload else { return nil }- activity.context = context- return activity+ var activityData: ActivityDataRecord? {+ guard var activityData = storedActivityData else { return nil }+ activityData.context = context+ return activityData}- init(activity: ActivityRecord, recordedAt: Date = Date()) {- version = Self.currentVersion- kind = .activity+ init(activityData: ActivityDataRecord, recordedAt: Date = Date()) {+ schemaVersion = Self.schemaVersionself.recordedAt = recordedAt- context = activity.context- var payload = activity+ context = activityData.context+ var payload = activityDatapayload.context = nil- activityPayload = payload- token = nil- observation = nil- }-- init(token: TokenTurn, context: ActivityContext? = nil, recordedAt: Date) {- version = Self.currentVersion- kind = .tokenSnapshot- self.recordedAt = recordedAt- self.context = context- activityPayload = nil- self.token = token- observation = nil+ storedActivityData = payload+ tokenUsage = nil}- init(observation: TokenObservation, recordedAt: Date) {- version = Self.currentVersion- kind = .tokenObservation+ init(tokenUsage: TokenUsageObservation, recordedAt: Date) {+ schemaVersion = Self.schemaVersionself.recordedAt = recordedAtcontext = nil- activityPayload = nil- token = nil- self.observation = observation+ storedActivityData = nil+ self.tokenUsage = tokenUsage}init(from decoder: Decoder) throws {let container = try decoder.container(keyedBy: CodingKeys.self)- version = try container.decode(Int.self, forKey: .version)- guard version == Self.currentVersion else {- throw DecodingError.dataCorruptedError(forKey: .version, in: container, debugDescription: "Unsupported event version")+ schemaVersion = try container.decode(Int.self, forKey: .schemaVersion)+ guard schemaVersion == Self.schemaVersion else {+ throw DecodingError.dataCorruptedError(forKey: .schemaVersion, in: container, debugDescription: "Unsupported event schema version")}- kind = try container.decode(Kind.self, forKey: .kind)recordedAt = try container.decode(Date.self, forKey: .recordedAt)- context = try container.decodeIfPresent(ActivityContext.self, forKey: .context)- observation = try container.decodeIfPresent(TokenObservation.self, forKey: .observation)- switch kind {- case .activity:- activityPayload = try container.decode(ActivityRecord.self, forKey: .activityPayload)- token = nil- guard !container.contains(.token), observation == nil else {- throw DecodingError.dataCorruptedError(forKey: .token, in: container, debugDescription: "Activity cannot contain tokens")- }- case .tokenObservation:- activityPayload = nil- token = nil- guard observation != nil, !container.contains(.token), !container.contains(.activityPayload) else {- throw DecodingError.dataCorruptedError(forKey: .observation, in: container, debugDescription: "Missing observation")- }- case .tokenSnapshot:- token = try container.decode(TokenTurn.self, forKey: .token)- activityPayload = nil- guard !container.contains(.activityPayload), observation == nil, token?.usage?.isValid != false else {- throw DecodingError.dataCorruptedError(forKey: .token, in: container, debugDescription: "Invalid token snapshot")- }+ context = try container.decodeIfPresent(ActivityDataContext.self, forKey: .context)+ tokenUsage = try container.decodeIfPresent(TokenUsageObservation.self, forKey: .tokenUsage)+ storedActivityData = try container.decodeIfPresent(ActivityDataRecord.self, forKey: .storedActivityData)+ guard (storedActivityData != nil) != (tokenUsage != nil) else {+ throw DecodingError.dataCorrupted(.init(codingPath: decoder.codingPath, debugDescription: "Expected exactly one event payload"))}}@@ -113,12 +84,10 @@ nonisolated struct AppServerEventRecord: Codable, Equatable {private static let decoder = JSONLines.decoderprivate enum CodingKeys: String, CodingKey {- case version- case kind+ case schemaVersioncase recordedAtcase context- case activityPayload- case token- case observation+ case storedActivityData = "activityData"+ case tokenUsage}}diff --git a/CodexBar/Models/AppServerLogEntry.swift b/CodexBar/Models/AppServerLogEntry.swiftindex 59d993f..aef4830 100644--- a/CodexBar/Models/AppServerLogEntry.swift+++ b/CodexBar/Models/AppServerLogEntry.swift@@ -101,5 +101,3 @@ nonisolated struct AppServerLogEntry: Identifiable, Equatable, Codable, Sendablereturn String(data: data, encoding: .utf8)}}--// 窗口只加载已浏览的历史, 新增记录和响应更新按数据库 revision 增量合并diff --git a/CodexBar/Models/CodexStatusError.swift b/CodexBar/Models/CodexStatusError.swiftindex 2fea780..79ddf25 100644--- a/CodexBar/Models/CodexStatusError.swift+++ b/CodexBar/Models/CodexStatusError.swift@@ -5,6 +5,7 @@ nonisolated enum CodexStatusError: LocalizedError {case serverTimeoutcase serverConnectionClosedcase invalidServerResponse+ case messageTooLarge(bytes: UInt64, limit: Int)case invalidResponsePayloadcase serverError(AppServerRPCError)case unsupportedMethod@@ -20,6 +21,8 @@ nonisolated enum CodexStatusError: LocalizedError {String(localized: "codex-status.app-server.error.connection-closed")case .invalidServerResponse, .invalidResponsePayload:String(localized: "codex-status.app-server.error.invalid-response")+ case let .messageTooLarge(bytes, limit):+ "Codex app-server message too large: \(bytes) bytes, limit \(limit) bytes"case let .serverError(error):error.messagecase .unsupportedMethod:@@ -61,7 +64,7 @@ nonisolated enum CodexStatusError: LocalizedError {return !isAuthenticationRequired && !isUnsupportedMethod && !isProtocolOrParameterFailure}- /// 服务端也用 -32600 表示线程历史不可用, 仅匹配当前线程的已知错误+ /// 服务端也用 -32600 表示会话历史不可用, 仅匹配当前会话的已知错误func isMissingRollout(for threadID: String) -> Bool {guard case let .serverError(error) = self else { return false }return error.code == -32600 && error.message == "no rollout found for thread id \(threadID)"@@ -86,7 +89,7 @@ nonisolated enum CodexStatusError: LocalizedError {/// 连接断开, 超时或 JSON-RPC 信封无效时需要重建 app-server 会话var isTransportFailure: Bool {switch self {- case .serverConnectionClosed, .serverTimeout, .invalidServerResponse:+ case .serverConnectionClosed, .serverTimeout, .invalidServerResponse, .messageTooLarge:truedefault:falsediff --git a/CodexBar/Models/HistorySnapshot.swift b/CodexBar/Models/HistorySnapshot.swiftindex 39c70c8..fdd9d52 100644--- a/CodexBar/Models/HistorySnapshot.swift+++ b/CodexBar/Models/HistorySnapshot.swift@@ -1,7 +1,7 @@// MARK: - 每日聚合指标/// 热力图详情面板直接消费的每日统计-nonisolated struct ActivityMetrics: Equatable {+nonisolated struct ActivityDataMetrics: Equatable {let startDate: Stringlet threadCount: Int?let turnCount: Int?@@ -43,8 +43,8 @@ nonisolated struct ActivityMetrics: Equatable {self.turnAbortedCount = turnAbortedCount}- static func unavailable(startDate: String) -> ActivityMetrics {- ActivityMetrics(+ static func unavailable(startDate: String) -> ActivityDataMetrics {+ ActivityDataMetrics(startDate: startDate,threadCount: nil,turnCount: nil,@@ -55,8 +55,8 @@ nonisolated struct ActivityMetrics: Equatable {)}- static func empty(startDate: String) -> ActivityMetrics {- ActivityMetrics(+ static func empty(startDate: String) -> ActivityDataMetrics {+ ActivityDataMetrics(startDate: startDate,threadCount: 0,turnCount: 0,@@ -92,8 +92,8 @@ nonisolated struct ActivityMetrics: Equatable {self.turnAbortedCount = turnAbortedCount}- func adding(_ other: ActivityMetrics) -> ActivityMetrics {- ActivityMetrics(+ func adding(_ other: ActivityDataMetrics) -> ActivityDataMetrics {+ ActivityDataMetrics(startDate: startDate,threadCount: threadCount.flatMap { lhs in other.threadCount.map { lhs + $0 } },turnCount: turnCount.flatMap { lhs in other.turnCount.map { lhs + $0 } },@@ -120,84 +120,41 @@ nonisolated struct ActivityMetrics: Equatable {/// HistoryService 发布给 UI 的近端快照nonisolated struct HistorySnapshot: Equatable {- let dailyMetrics: [ActivityMetrics]+ let activityData: [ActivityDataMetrics]var tokenUsageByDate: [String: TokenUsage] = [:]- var unavailableActivityDates = Set<String>()- var isActivityComplete = true+ var unavailableActivityDataDates = Set<String>()+ var isActivityDataComplete = true- static let empty = HistorySnapshot(dailyMetrics: [])+ static let empty = HistorySnapshot(activityData: [])- init(dailyMetrics: [ActivityMetrics]) {- self.dailyMetrics = dailyMetrics+ init(activityData: [ActivityDataMetrics]) {+ self.activityData = activityData}- /// 同 generation 只采用一份数据; 确认独立的新 generation 与历史贡献累加+ /// 本机日期使用本地汇总, 其他设备的同日数量相加init(- localAggregates: [ActivityAggregate],- syncedRecords: [ActivitySyncRecord],+ localActivityDataAggregates: [ActivityDataAggregate],+ syncedActivityDataRecords: [ActivityDataSyncRecord],currentDeviceID: String?) {- var metricsByDate = [String: ActivityMetrics]()-- let localByDate = Dictionary(localAggregates.map { ($0.date, $0) }, uniquingKeysWith: { _, newer in newer })- unavailableActivityDates = Set(Dictionary(grouping: localAggregates, by: \.date).filter { $0.value.count > 1 }.keys)- let currentDeviceRecords = syncedRecords.filter { $0.deviceID == currentDeviceID }- let otherDeviceRecords = syncedRecords.filter { $0.deviceID != currentDeviceID }-- Self.merge(otherDeviceRecords, into: &metricsByDate)-- let remoteByDate = Dictionary(grouping: currentDeviceRecords, by: \.date)- let currentDeviceDates = Set(localByDate.keys).union(remoteByDate.keys)- for date in currentDeviceDates {- Self.mergeCurrentDeviceDate(- local: localByDate[date],- remoteRecords: remoteByDate[date] ?? [],- into: &metricsByDate- )- }-- dailyMetrics = metricsByDate.values.sorted { $0.startDate < $1.startDate }- }-- private static func mergeCurrentDeviceDate(- local: ActivityAggregate?,- remoteRecords: [ActivitySyncRecord],- into metricsByDate: inout [String: ActivityMetrics]- ) {- guard let local else {- merge(remoteRecords, into: &metricsByDate)- return- }-- let matchingIndex = remoteRecords.firstIndex {- local.syncedAggregate.matchesRemoteSource($0.daily)- }+ var metricsByDate = [String: ActivityDataMetrics]()- for (index, record) in remoteRecords.enumerated() where index != matchingIndex {- merge(record.daily.metrics, into: &metricsByDate)+ let localByDate = Dictionary(localActivityDataAggregates.map { ($0.date, $0) }, uniquingKeysWith: { _, newer in newer })+ unavailableActivityDataDates = Set(Dictionary(grouping: localActivityDataAggregates, by: \.date).filter { $0.value.count > 1 }.keys)+ let remoteByID = Dictionary(syncedActivityDataRecords.map { ($0.id, $0) }, uniquingKeysWith: { _, newer in newer })+ for record in remoteByID.values where record.deviceID != currentDeviceID || localByDate[record.date] == nil {+ Self.merge(record.activityData.metrics, into: &metricsByDate)}-- if let matchingIndex,- remoteRecords[matchingIndex].daily.aggregationVersion > local.aggregationVersion- || (remoteRecords[matchingIndex].daily.sourceCheckpoint?.byteCount ?? 0) > (local.sourceCheckpoint?.byteCount ?? 0) {- merge(remoteRecords[matchingIndex].daily.metrics, into: &metricsByDate)- } else {- merge(local.metrics, into: &metricsByDate)+ for local in localByDate.values {+ Self.merge(local.metrics, into: &metricsByDate)}- }- private static func merge(- _ records: [ActivitySyncRecord],- into metricsByDate: inout [String: ActivityMetrics]- ) {- for record in records {- merge(record.daily.metrics, into: &metricsByDate)- }+ activityData = metricsByDate.values.sorted { $0.startDate < $1.startDate }}private static func merge(- _ metrics: ActivityMetrics,- into metricsByDate: inout [String: ActivityMetrics]+ _ metrics: ActivityDataMetrics,+ into metricsByDate: inout [String: ActivityDataMetrics]) {if let existing = metricsByDate[metrics.startDate] {metricsByDate[metrics.startDate] = existing.adding(metrics)diff --git a/CodexBar/Models/StorageVersion.swift b/CodexBar/Models/StorageVersion.swiftindex 54cbfcd..10e5bf4 100644--- a/CodexBar/Models/StorageVersion.swift+++ b/CodexBar/Models/StorageVersion.swift@@ -9,56 +9,22 @@ nonisolated enum StorageCompatibilityError: Error, Equatable {}nonisolated enum StorageVersion {- private struct Header: Decodable { let version: Int }+ private struct Header: Decodable { let schemaVersion: Int }static func validate(_ data: Data, current: Int, name: String) throws {- try require(JSONDecoder().decode(Header.self, from: data).version, current: current, name: name)+ try require(JSONDecoder().decode(Header.self, from: data).schemaVersion, current: current, name: name)}- static func require(_ version: Int, current: Int, name: String) throws {- guard version == current else { throw StorageCompatibilityError.unsupportedFormat(name, version) }- }-- static func validateExistingFile(at url: URL, current: Int, name: String) throws {- do {- try validate(Data(contentsOf: url), current: current, name: name)- } catch let error as CocoaError where error.code == .fileReadNoSuchFile {- return- }+ static func require(_ schemaVersion: Int, current: Int, name: String) throws {+ guard schemaVersion == current else { throw StorageCompatibilityError.unsupportedFormat(name, schemaVersion) }}}nonisolated enum AggregationVersion {- static let activity = 1- static let tokens = 1+ static let activityData = 1+ static let tokenUsage = 1static func require(_ version: Int, current: Int, name: String) throws {guard version > 0, version <= current else { throw StorageCompatibilityError.unsupportedAlgorithm(name, version) }}}--/// 每段以排他的源边界结束, 连续使用同一算法时只扩展末段-nonisolated struct AggregationRange: Codable, Equatable {- var version: Int- var end: UInt64-- static func appending(to ranges: [Self], version: Int, end: UInt64) -> [Self] {- var result = ranges- if result.last?.version == version {- result[result.count - 1].end = end- } else {- result.append(Self(version: version, end: end))- }- return result- }-- static func validate(_ ranges: [Self], end: UInt64, current: Int) throws {- var previous: UInt64 = 0- for range in ranges {- try AggregationVersion.require(range.version, current: current, name: "AggregationRange")- guard range.end > previous, range.end <= end else { throw StorageCompatibilityError.incompleteSource }- previous = range.end- }- guard ranges.isEmpty || previous == end else { throw StorageCompatibilityError.incompleteSource }- }-}diff --git a/CodexBar/Models/SyncCache.swift b/CodexBar/Models/SyncCache.swiftnew file mode 100644index 0000000..e20c3b7--- /dev/null+++ b/CodexBar/Models/SyncCache.swift@@ -0,0 +1,55 @@+import Foundation++nonisolated protocol SyncCacheRecord: Codable, Equatable, Identifiable where ID == String {+ var deviceID: String { get }+ var date: String { get }+}++/// 内存按记录 ID 更新, 文件按固定顺序保存数组, 数据与拉取游标共同提交+nonisolated struct SyncCache<Record: SyncCacheRecord, State: Codable & Equatable>: Codable, Equatable {+ static var schemaVersion: Int {+ 1+ }++ var schemaVersion = Self.schemaVersion+ var state: State+ var records: [String: Record]+ var cursor: Data?++ init(state: State, records: [String: Record] = [:], cursor: Data? = nil) {+ self.state = state+ self.records = records+ self.cursor = cursor+ }++ var sortedRecords: [Record] {+ records.values.sorted { ($0.deviceID, $0.date, $0.id) < ($1.deviceID, $1.date, $1.id) }+ }++ init(from decoder: Decoder) throws {+ let container = try decoder.container(keyedBy: CodingKeys.self)+ schemaVersion = try container.decode(Int.self, forKey: .schemaVersion)+ guard schemaVersion == Self.schemaVersion else { throw SyncRecovery.Failure.unsupportedCacheVersion }+ state = try container.decode(State.self, forKey: .state)+ let decoded = try container.decode([Record].self, forKey: .records)+ records = [:]+ for record in decoded {+ guard records.updateValue(record, forKey: record.id) == nil else {+ throw DecodingError.dataCorruptedError(forKey: .records, in: container, debugDescription: "Duplicate sync record identity")+ }+ }+ cursor = try container.decodeIfPresent(Data.self, forKey: .cursor)+ }++ func encode(to encoder: Encoder) throws {+ var container = encoder.container(keyedBy: CodingKeys.self)+ try container.encode(schemaVersion, forKey: .schemaVersion)+ try container.encode(state, forKey: .state)+ try container.encode(sortedRecords, forKey: .records)+ try container.encode(cursor, forKey: .cursor)+ }++ private enum CodingKeys: String, CodingKey {+ case schemaVersion, state, records, cursor+ }+}diff --git a/CodexBar/Models/TokenHistory.swift b/CodexBar/Models/TokenHistory.swiftdeleted file mode 100644index b07d844..0000000--- a/CodexBar/Models/TokenHistory.swift+++ /dev/null@@ -1,424 +0,0 @@-import CryptoKit-import Foundation--/// 一条线程轮次的累计快照, 身份只保存哈希, 不保存对话正文或原始 ID-nonisolated struct TokenTurn: Codable, Equatable, Identifiable {- var aggregationVersion = AggregationVersion.tokens- let id: String- var rootID: String- var startedAt: Date?- var updatedAt: Date- var usage: TokenUsage?- var rebuiltAt: Date?- var generationID: String- var ancestorIDs: Set<String>- var checkpoint: [String: TokenObservationCheckpoint]- var hasConflict: Bool-- init(- id: String,- rootID: String,- startedAt: Date? = nil,- updatedAt: Date,- usage: TokenUsage? = nil,- rebuiltAt: Date? = nil,- generationID: String = "initial",- ancestorIDs: Set<String> = [],- checkpoint: [String: TokenObservationCheckpoint] = [:],- hasConflict: Bool = false,- aggregationVersion: Int = AggregationVersion.tokens- ) {- self.aggregationVersion = aggregationVersion- self.id = id- self.rootID = rootID- self.startedAt = startedAt.map(JSONLines.storageDate)- self.updatedAt = JSONLines.storageDate(updatedAt)- self.usage = usage- self.rebuiltAt = rebuiltAt.map(JSONLines.storageDate)- self.generationID = generationID- self.ancestorIDs = ancestorIDs- self.checkpoint = checkpoint- self.hasConflict = hasConflict- }-- private enum CodingKeys: String, CodingKey {- case id, rootID, startedAt, updatedAt, usage, rebuiltAt, aggregationVersion- case generationID, ancestorIDs, checkpoint, hasConflict- }-- init(from decoder: Decoder) throws {- let container = try decoder.container(keyedBy: CodingKeys.self)- aggregationVersion = try container.decode(Int.self, forKey: .aggregationVersion)- try AggregationVersion.require(aggregationVersion, current: AggregationVersion.tokens, name: "Tokens")- id = try container.decode(String.self, forKey: .id)- rootID = try container.decode(String.self, forKey: .rootID)- startedAt = try container.decodeIfPresent(Date.self, forKey: .startedAt)- updatedAt = try container.decode(Date.self, forKey: .updatedAt)- usage = try container.decodeIfPresent(TokenUsage.self, forKey: .usage)- rebuiltAt = try container.decodeIfPresent(Date.self, forKey: .rebuiltAt)- generationID = try container.decode(String.self, forKey: .generationID)- ancestorIDs = try container.decode(Set<String>.self, forKey: .ancestorIDs)- checkpoint = try container.decode([String: TokenObservationCheckpoint].self, forKey: .checkpoint)- hasConflict = try container.decode(Bool.self, forKey: .hasConflict)- guard usage == nil || !checkpoint.isEmpty else {- throw DecodingError.dataCorruptedError(forKey: .checkpoint, in: container, debugDescription: "Token usage requires an observation checkpoint")- }- }-- func encode(to encoder: Encoder) throws {- var container = encoder.container(keyedBy: CodingKeys.self)- try container.encode(aggregationVersion, forKey: .aggregationVersion)- try container.encode(id, forKey: .id)- try container.encode(rootID, forKey: .rootID)- try container.encodeIfPresent(startedAt, forKey: .startedAt)- try container.encode(updatedAt, forKey: .updatedAt)- try container.encodeIfPresent(usage, forKey: .usage)- try container.encodeIfPresent(rebuiltAt, forKey: .rebuiltAt)- try container.encode(generationID, forKey: .generationID)- try container.encode(ancestorIDs.sorted(), forKey: .ancestorIDs)- try container.encode(checkpoint, forKey: .checkpoint)- try container.encode(hasConflict, forKey: .hasConflict)- }-- static func identifier(thread: String, turn: String) -> String {- // 哈希身份域是固定输入, 不随存储 version 变化, 避免同一轮次生成多个身份- let data = (try? JSONEncoder().encode(["token-turn-v1", thread, turn])) ?? Data()- return hexString(SHA256.hash(data: data))- }-- static func hexString(_ bytes: some Sequence<UInt8>) -> String {- let digits = Array("0123456789abcdef".utf8)- var result: [UInt8] = []- result.reserveCapacity(bytes.underestimatedCount * 2)- for byte in bytes {- result.append(digits[Int(byte >> 4)])- result.append(digits[Int(byte & 0x0F)])- }- return String(bytes: result, encoding: .utf8) ?? ""- }-- func merging(_ other: Self) throws -> Self {- precondition(id == other.id)- try AggregationVersion.require(aggregationVersion, current: AggregationVersion.tokens, name: "Tokens")- try AggregationVersion.require(other.aggregationVersion, current: AggregationVersion.tokens, name: "Tokens")- guard rootID == other.rootID else { throw TokenCacheError.rootIdentityConflict }- if generationID != other.generationID {- if ancestorIDs.contains(other.generationID) {- return incorporatingUncoveredObservations(from: other)- }- if other.ancestorIDs.contains(generationID) {- return other.incorporatingUncoveredObservations(from: self)- }- return conflicting(with: other)- }- if hasConflict || other.hasConflict {- return conflicting(with: other)- }- if !checkpoint.isEmpty || !other.checkpoint.isEmpty {- let coversOther = covers(other.checkpoint)- let otherCovers = other.covers(checkpoint)- if coversOther && !otherCovers {- return self- }- if otherCovers && !coversOther {- return other- }- if !coversOther || usage != other.usage {- return conflicting(with: other)- }- }- var result = self- result.aggregationVersion = max(aggregationVersion, other.aggregationVersion)- if let date = other.startedAt {- result.startedAt = min(startedAt ?? date, date)- }- result.updatedAt = max(updatedAt, other.updatedAt)- return result- }-- func covers(_ boundary: [String: TokenObservationCheckpoint]) -> Bool {- boundary.allSatisfy { id, value in- guard let current = checkpoint[id], current.sequence >= value.sequence else { return false }- return current.sequence != value.sequence || current.usage == value.usage- }- }-- mutating func startNewGeneration(at date: Date) {- ancestorIDs.insert(generationID)- generationID = UUID().uuidString.lowercased()- rebuiltAt = JSONLines.storageDate(date)- aggregationVersion = AggregationVersion.tokens- }-- private func incorporatingUncoveredObservations(from old: Self) -> Self {- var result = self- guard !hasConflict else { return conflicting(with: old) }- for (stream, incoming) in old.checkpoint {- guard let covered = checkpoint[stream] else { return conflicting(with: old) }- if incoming.sequence == covered.sequence, incoming.usage != covered.usage {- return conflicting(with: old)- }- guard incoming.sequence > covered.sequence else { continue }- guard let delta = incoming.usage.subtracting(covered.usage),- let usage = result.usage?.adding(delta), usage.isValid else { return conflicting(with: old) }- result.usage = usage- result.checkpoint[stream] = incoming- result.aggregationVersion = max(result.aggregationVersion, old.aggregationVersion)- result.updatedAt = max(result.updatedAt, old.updatedAt)- }- return result- }-- private func conflicting(with other: Self) -> Self {- var result = self- // 冲突保留可恢复的边界, 不把任意分支的用量当成权威结果- let generations = ancestorIDs.union(other.ancestorIDs)- .union([generationID, other.generationID])- if generationID != other.generationID {- if ancestorIDs.contains(other.generationID) {- result.generationID = generationID- } else if other.ancestorIDs.contains(generationID) {- result.generationID = other.generationID- } else {- let origins = generations.filter { !$0.hasPrefix("conflict-") }.sorted()- result.generationID = "conflict-" + Self.hexString(SHA256.hash(data: Data(origins.joined(separator: ":").utf8)))- }- }- result.ancestorIDs = generations.subtracting([result.generationID])- result.hasConflict = true- result.usage = nil- result.rebuiltAt = [rebuiltAt, other.rebuiltAt].compactMap(\.self).max()- result.updatedAt = max(updatedAt, other.updatedAt)- result.startedAt = [startedAt, other.startedAt].compactMap(\.self).min()- for (stream, incoming) in other.checkpoint {- let current = result.checkpoint[stream]- if incoming.sequence > (current?.sequence ?? 0)- || (incoming.sequence == current?.sequence && Self.usageOrder(current?.usage ?? .zero).lexicographicallyPrecedes(Self.usageOrder(incoming.usage))) {- result.checkpoint[stream] = incoming- }- }- return result- }-- static func merged(_ records: [Self]) throws -> [String: Self] {- try records.reduce(into: [:]) { result, record in- result[record.id] = try result[record.id]?.merging(record) ?? record- }- }-- static func dailyUsage(_ records: [Self], now: Date = Date()) -> [String: TokenUsage] {- // 根身份矛盾时无法证明日期归属, 不展示不完整合计- guard let byID = try? merged(records) else { return [:] }- let cutoff = HistoryStorage.retentionCutoffDate(today: now)- var result: [String: TokenUsage] = [:]- var invalidDates: Set<String> = []- for record in byID.values {- if record.hasConflict, let start = byID[record.rootID]?.startedAt {- invalidDates.insert(CodexDateFormat.dayString(from: start))- }- guard let usage = record.usage, usage.isValid,- let start = byID[record.rootID]?.startedAt, start >= cutoff, start <= now else { continue }- let date = CodexDateFormat.dayString(from: start)- if let current = result[date] {- if let sum = current.adding(usage) {- result[date] = sum- } else {- invalidDates.insert(date)- }- } else {- result[date] = usage- }- }- for date in invalidDates {- result.removeValue(forKey: date)- }- return result- }-- func pseudonymized(salt: Data) -> Self {- let key = SymmetricKey(data: salt)- return pseudonymized { value in- Self.hexString(HMAC<SHA256>.authenticationCode(for: Data(value.utf8), using: key))- }- }-- static func pseudonymized(_ records: [Self], salt: Data) -> [Self] {- let key = SymmetricKey(data: salt)- var identities: [String: String] = [:]- return records.map { record in- record.pseudonymized { value in- if let cached = identities[value] {- return cached- }- let hashed = hexString(HMAC<SHA256>.authenticationCode(for: Data(value.utf8), using: key))- identities[value] = hashed- return hashed- }- }- }-- private func pseudonymized(hash: (String) -> String) -> Self {- let exportedID = hash(id)- return Self(- id: exportedID, rootID: rootID == id ? exportedID : hash(rootID),- startedAt: startedAt, updatedAt: updatedAt, usage: usage, rebuiltAt: rebuiltAt,- generationID: generationID, ancestorIDs: ancestorIDs,- checkpoint: checkpoint, hasConflict: hasConflict, aggregationVersion: aggregationVersion- )- }-- static func syncable(_ records: [Self]) -> [Self] {- let rootIDs = Set(records.filter { $0.usage != nil }.map(\.rootID))- return records.filter { $0.usage != nil || $0.generationID != "initial" || $0.hasConflict || rootIDs.contains($0.id) }- }-- private static func usageOrder(_ usage: TokenUsage) -> [Int64] {- [- usage.totalTokens,- usage.inputTokens,- usage.outputTokens,- usage.cachedInputTokens,- usage.cacheWriteInputTokens,- usage.reasoningOutputTokens- ]- }-}--/// 云端身份只通过账户 salt 与本地已发现的轮次匹配, 不导入其他设备的轮次-nonisolated struct TokenHistoryBaseline {- let salt: Data- private let turns: [String: TokenTurn]- init(salt: Data, turns: [String: TokenTurn]) {- self.salt = salt- self.turns = turns- }-- func contains(_ local: TokenTurn) -> Bool {- let exported = local.pseudonymized(salt: salt)- return turns[exported.id]?.rootID == exported.rootID- }-- func acceptsRecovery(_ local: TokenTurn) -> Bool {- let exported = local.pseudonymized(salt: salt)- guard let remote = turns[exported.id] else { return true }- return remote.rootID == exported.rootID && !local.hasConflict && !remote.hasConflict- && exported.covers(remote.checkpoint) && (remote.usage == nil || local.usage != nil)- }-- func replacement(for local: TokenTurn, recovering: Bool = false, now: Date = Date()) -> TokenTurn? {- guard !recovering || acceptsRecovery(local) else { return nil }- let exported = local.pseudonymized(salt: salt)- guard let remote = turns[exported.id], remote.rootID == exported.rootID else { return nil }- if recovering, !local.hasConflict, !remote.hasConflict,- exported.covers(remote.checkpoint), remote.usage == nil || local.usage != nil,- !local.ancestorIDs.contains(remote.generationID),- local.aggregationVersion != remote.aggregationVersion || local.usage != remote.usage {- var corrected = local- corrected.generationID = remote.generationID- corrected.ancestorIDs.formUnion(remote.ancestorIDs)- corrected.startNewGeneration(at: now)- return corrected- }- guard let merged = try? exported.merging(remote), merged != exported else { return nil }- return TokenTurn(- id: local.id, rootID: local.rootID, startedAt: merged.startedAt ?? local.startedAt,- updatedAt: merged.updatedAt, usage: merged.usage, rebuiltAt: merged.rebuiltAt,- generationID: merged.generationID, ancestorIDs: merged.ancestorIDs,- checkpoint: merged.checkpoint, hasConflict: merged.hasConflict, aggregationVersion: merged.aggregationVersion- )- }-}--nonisolated struct TokenObservationCheckpoint: Codable, Equatable {- var sequence: Int64- var usage: TokenUsage- var aggregationRanges: [AggregationRange] = []-- private enum CodingKeys: String, CodingKey { case sequence, usage, aggregationRanges }-- init(sequence: Int64, usage: TokenUsage, aggregationRanges: [AggregationRange] = []) {- self.sequence = sequence- self.usage = usage- self.aggregationRanges = aggregationRanges- }-- init(from decoder: Decoder) throws {- let values = try decoder.container(keyedBy: CodingKeys.self)- sequence = try values.decode(Int64.self, forKey: .sequence)- usage = try values.decode(TokenUsage.self, forKey: .usage)- aggregationRanges = try values.decodeIfPresent([AggregationRange].self, forKey: .aggregationRanges) ?? []- guard sequence > 0, usage.isValid else { throw StorageCompatibilityError.incompleteSource }- try AggregationRange.validate(aggregationRanges, end: UInt64(sequence), current: AggregationVersion.tokens)- }-}--/// 原始身份与时间不依赖聚合结果, 算法升级后仍可独立重放-nonisolated struct TokenObservationIdentity: Codable, Equatable {- let id: String- let rootID: String- let startedAt: Date?- let updatedAt: Date-- init(_ turn: TokenTurn) {- id = turn.id- rootID = turn.rootID- startedAt = turn.startedAt- updatedAt = turn.updatedAt- }-- var emptyTurn: TokenTurn {- TokenTurn(id: id, rootID: rootID, startedAt: startedAt, updatedAt: updatedAt)- }-}--/// streamID 是每次采集连续区间生成的随机身份, 不包含设备或原始线程信息-nonisolated struct TokenObservation: Codable, Equatable {- let turn: TokenObservationIdentity- let rootStartedAt: Date?- let streamID: String- let sequence: Int64- let previous: TokenUsage- let current: TokenUsage-- init(turn: TokenTurn, rootStartedAt: Date?, streamID: String, sequence: Int64, previous: TokenUsage, current: TokenUsage) {- self.turn = TokenObservationIdentity(turn)- self.rootStartedAt = rootStartedAt- self.streamID = streamID- self.sequence = sequence- self.previous = previous- self.current = current- }-- func applying(to existing: TokenTurn?) throws -> TokenTurn {- var result = existing ?? turn.emptyTurn- guard result.id == turn.id, result.rootID == turn.rootID else { throw TokenCacheError.rootIdentityConflict }- try AggregationVersion.require(result.aggregationVersion, current: AggregationVersion.tokens, name: "Tokens")- let boundary = result.checkpoint[streamID]- if let boundary, boundary.sequence >= sequence {- return result- }- guard sequence == (boundary?.sequence ?? 0) + 1,- let delta = current.subtracting(previous),- let observed = (boundary?.usage ?? .zero).adding(delta), observed.isValid else {- throw TokenCacheError.incompleteJournal- }- if !result.hasConflict {- guard let usage = (result.usage ?? .zero).adding(delta), usage.isValid else {- throw TokenCacheError.incompleteJournal- }- result.usage = usage- }- let previousRanges = boundary.map {- $0.aggregationRanges.isEmpty- ? [AggregationRange(version: result.aggregationVersion, end: UInt64($0.sequence))]- : $0.aggregationRanges- } ?? []- result.checkpoint[streamID] = TokenObservationCheckpoint(- sequence: sequence, usage: observed,- aggregationRanges: AggregationRange.appending(to: previousRanges, version: AggregationVersion.tokens, end: UInt64(sequence))- )- result.aggregationVersion = AggregationVersion.tokens- result.updatedAt = max(result.updatedAt, turn.updatedAt)- return result- }-}diff --git a/CodexBar/Models/TokenUsage.swift b/CodexBar/Models/TokenUsage.swiftindex 3acc4ef..3842b0c 100644--- a/CodexBar/Models/TokenUsage.swift+++ b/CodexBar/Models/TokenUsage.swift@@ -34,6 +34,12 @@ nonisolated struct TokenUsage: Codable, Equatable {func subtracting(_ other: Self) -> Self? {guard isValid, other.isValid else { return nil }+ return subtractingCounters(other)+ }++ /// 上下文上限可能给累计 totalTokens 留下偏移, 只接受各计数器差值一致的真实增量+ func subtractingCounters(_ other: Self) -> Self? {+ guard values.allSatisfy({ $0 >= 0 }), other.values.allSatisfy({ $0 >= 0 }) else { return nil }let differences = zip(values, other.values).map { $0.subtractingReportingOverflow($1) }guard !differences.contains(where: { $0.overflow || $0.partialValue < 0 }) else { return nil }let result = Self(diff --git a/CodexBar/Models/TokenUsageHistory.swift b/CodexBar/Models/TokenUsageHistory.swiftnew file mode 100644index 0000000..15c1fa8--- /dev/null+++ b/CodexBar/Models/TokenUsageHistory.swift@@ -0,0 +1,105 @@+import Foundation++/// 历史直接累计观测增量, 不依赖根任务归属或轮次展示状态+nonisolated struct TokenUsageAggregate: Codable, Equatable {+ static let schemaVersion = 1+ var schemaVersion = Self.schemaVersion+ var aggregationVersion = AggregationVersion.tokenUsage+ var position: Int64 = 0+ let date: String+ var eventCount = 0+ var tokenUsage: TokenUsage = .zero++ private enum CodingKeys: String, CodingKey {+ case schemaVersion, aggregationVersion, position, date, eventCount, tokenUsage+ }++ init(date: String, tokenUsage: TokenUsage = .zero, eventCount: Int = 0) {+ self.date = date+ self.tokenUsage = tokenUsage+ self.eventCount = eventCount+ }++ init(from decoder: Decoder) throws {+ let container = try decoder.container(keyedBy: CodingKeys.self)+ schemaVersion = try container.decode(Int.self, forKey: .schemaVersion)+ try StorageVersion.require(schemaVersion, current: Self.schemaVersion, name: "TokenUsageAggregate")+ aggregationVersion = try container.decode(Int.self, forKey: .aggregationVersion)+ try AggregationVersion.require(aggregationVersion, current: AggregationVersion.tokenUsage, name: "tokenUsage")+ position = try container.decode(Int64.self, forKey: .position)+ date = try container.decode(String.self, forKey: .date)+ eventCount = try container.decode(Int.self, forKey: .eventCount)+ tokenUsage = try container.decode(TokenUsage.self, forKey: .tokenUsage)+ guard HistoryStorage.isValidDateKey(date), position >= 0, eventCount >= 0, tokenUsage.isValid else {+ throw DecodingError.dataCorrupted(.init(codingPath: decoder.codingPath, debugDescription: "Invalid daily token aggregate"))+ }+ }++ mutating func record(_ observation: TokenUsageObservation) throws {+ guard let delta = observation.delta, let total = tokenUsage.adding(delta), total.isValid else {+ throw StorageCompatibilityError.incompleteSource+ }+ tokenUsage = total+ eventCount += 1+ }++ static func tokenUsageByDate(_ aggregates: [Self], now: Date = Date()) -> [String: TokenUsage] {+ let cutoff = HistoryStorage.dateKey(for: HistoryStorage.retentionCutoffDate(today: now))+ let today = HistoryStorage.dateKey(for: now)+ var result: [String: TokenUsage] = [:]+ for aggregate in aggregates where aggregate.date >= cutoff && aggregate.date <= today && aggregate.tokenUsage.isValid {+ result[aggregate.date] = aggregate.tokenUsage+ }+ return result+ }+}++/// 通知先排队, 比较基线由存储事务决定+nonisolated struct TokenUsageSample: Codable {+ var id = UUID().uuidString.lowercased()+ let observedAt: Date+ let threadID: String+ let turnID: String+ let turnStartedAt: Date?+ let current: TokenUsageUpdate+}++nonisolated struct TokenUsageThreadSnapshot {+ let threadID: String+ var position: Int64 = 0+ var turns: [String: TokenUsage] = [:]+}++/// 相邻会话累计值构成独立观测, 重试复用同一个 ID 和采集时间+nonisolated struct TokenUsageObservation: Codable, Equatable {+ let id: String+ let observedAt: Date+ let threadID: String?+ let turnID: String?+ let turnStartedAt: Date?+ let previous: TokenUsage+ let current: TokenUsageUpdate++ init(+ id: String = UUID().uuidString.lowercased(), observedAt: Date,+ threadID: String? = nil, turnID: String? = nil, turnStartedAt: Date? = nil,+ previous: TokenUsage, current: TokenUsageUpdate+ ) {+ self.id = id+ self.observedAt = JSONLines.storageDate(observedAt)+ self.threadID = threadID+ self.turnID = turnID+ self.turnStartedAt = turnStartedAt.map(JSONLines.storageDate)+ self.previous = previous+ self.current = current+ }++ /// 开始时间未知时仍保存用量, 不因轮次元数据缺失丢弃有效消耗+ var aggregationDate: Date {+ turnStartedAt ?? observedAt+ }++ var delta: TokenUsage? {+ current.total.subtractingCounters(previous)+ }+}diff --git a/CodexBar/Models/TokenUsageSyncRecord.swift b/CodexBar/Models/TokenUsageSyncRecord.swiftnew file mode 100644index 0000000..e56068b--- /dev/null+++ b/CodexBar/Models/TokenUsageSyncRecord.swift@@ -0,0 +1,96 @@+import Foundation++/// 云端每台设备每天只保存一份数量, 本地观测不会从云端导入+nonisolated struct TokenUsageSyncRecord: SyncCacheRecord {+ static let schemaVersion = 1+ var schemaVersion = Self.schemaVersion+ var aggregationVersion = AggregationVersion.tokenUsage+ let deviceID: String+ let date: String+ let tokenUsage: TokenUsage?+ var updatedAt: Date?++ var id: String {+ recordName+ }++ var recordName: String {+ "tokenUsage_\(deviceID)_\(date)"+ }++ private enum CodingKeys: String, CodingKey {+ case schemaVersion, aggregationVersion, deviceID, date, recordName, updatedAt, tokenUsage+ }++ init(+ aggregationVersion: Int = AggregationVersion.tokenUsage, deviceID: String, date: String,+ tokenUsage: TokenUsage?, updatedAt: Date? = nil+ ) {+ self.aggregationVersion = aggregationVersion+ self.deviceID = deviceID+ self.date = date+ self.tokenUsage = tokenUsage+ self.updatedAt = updatedAt+ }++ init(from decoder: Decoder) throws {+ let container = try decoder.container(keyedBy: CodingKeys.self)+ schemaVersion = try container.decode(Int.self, forKey: .schemaVersion)+ try StorageVersion.require(schemaVersion, current: Self.schemaVersion, name: "TokenUsageSyncRecord")+ aggregationVersion = try container.decode(Int.self, forKey: .aggregationVersion)+ try AggregationVersion.require(aggregationVersion, current: AggregationVersion.tokenUsage, name: "tokenUsage")+ deviceID = try container.decode(String.self, forKey: .deviceID)+ date = try container.decode(String.self, forKey: .date)+ tokenUsage = try container.decodeIfPresent(TokenUsage.self, forKey: .tokenUsage)+ updatedAt = try container.decodeIfPresent(Date.self, forKey: .updatedAt)+ guard !deviceID.isEmpty, HistoryStorage.isValidDateKey(date), tokenUsage?.isValid != false,+ try container.decode(String.self, forKey: .recordName) == recordName else {+ throw DecodingError.dataCorrupted(.init(codingPath: decoder.codingPath, debugDescription: "Invalid daily token record"))+ }+ }++ func encode(to encoder: Encoder) throws {+ var container = encoder.container(keyedBy: CodingKeys.self)+ try container.encode(schemaVersion, forKey: .schemaVersion)+ try container.encode(aggregationVersion, forKey: .aggregationVersion)+ try container.encode(deviceID, forKey: .deviceID)+ try container.encode(date, forKey: .date)+ try container.encode(recordName, forKey: .recordName)+ try container.encode(updatedAt, forKey: .updatedAt)+ try container.encode(tokenUsage, forKey: .tokenUsage)+ }++ /// 云端更新时间不参与上传判断, 只比较设备日期对应的业务数据+ func hasSameData(as other: Self) -> Bool {+ aggregationVersion == other.aggregationVersion && deviceID == other.deviceID+ && date == other.date && tokenUsage == other.tokenUsage+ }++ static func dailyRecords(local: [TokenUsageAggregate], deviceID: String, now: Date = Date()) throws -> [Self] {+ for aggregate in local {+ try AggregationVersion.require(aggregate.aggregationVersion, current: AggregationVersion.tokenUsage, name: "tokenUsage")+ }+ return TokenUsageAggregate.tokenUsageByDate(local, now: now).sorted { $0.key < $1.key }.map {+ Self(deviceID: deviceID, date: $0.key, tokenUsage: $0.value)+ }+ }++ static func tokenUsageByDate(_ records: [Self], now: Date = Date()) -> [String: TokenUsage] {+ let cutoff = HistoryStorage.dateKey(for: HistoryStorage.retentionCutoffDate(today: now))+ let today = HistoryStorage.dateKey(for: now)+ var result: [String: TokenUsage] = [:]+ var unavailableDates = Set<String>()+ for record in records where record.date >= cutoff && record.date <= today {+ guard let tokenUsage = record.tokenUsage, tokenUsage.isValid,+ let sum = (result[record.date] ?? .zero).adding(tokenUsage), sum.isValid else {+ unavailableDates.insert(record.date)+ continue+ }+ result[record.date] = sum+ }+ for date in unavailableDates {+ result.removeValue(forKey: date)+ }+ return result+ }+}diff --git a/CodexBar/Models/UsageHeatmapDay.swift b/CodexBar/Models/UsageHeatmapDay.swiftindex 572c8aa..12854af 100644--- a/CodexBar/Models/UsageHeatmapDay.swift+++ b/CodexBar/Models/UsageHeatmapDay.swift@@ -18,7 +18,7 @@ nonisolated enum UsageHeatmapTokenState: Equatable {nonisolated struct UsageHeatmapDay: Equatable, Identifiable {let startDate: Stringlet tokenState: UsageHeatmapTokenState- let history: ActivityMetrics+ let activityData: ActivityDataMetricsvar tokenUsage: TokenUsage?var id: String {@@ -41,7 +41,7 @@ nonisolated struct UsageHeatmapDay: Equatable, Identifiable {) -> [UsageHeatmapDay?] {let todayTokenCount = usage?.tokenCount(on: today)let hasDailyUsageBuckets = usage?.hasDailyUsageBuckets == true- let historyByDate = history.dailyMetrics.reduce(into: [String: ActivityMetrics]()) { result, metrics in+ let historyByDate = history.activityData.reduce(into: [String: ActivityDataMetrics]()) { result, metrics inresult[metrics.startDate] = metrics}let todayString = CodexDateFormat.dayString(from: today)@@ -68,8 +68,8 @@ nonisolated struct UsageHeatmapDay: Equatable, Identifiable {return UsageHeatmapDay(startDate: startDate,tokenState: tokenState,- history: history.unavailableActivityDates.contains(startDate) ? .unavailable(startDate: startDate)- : historyByDate[startDate] ?? (history.isActivityComplete ? .empty(startDate: startDate) : .unavailable(startDate: startDate)),+ activityData: history.unavailableActivityDataDates.contains(startDate) ? .unavailable(startDate: startDate)+ : historyByDate[startDate] ?? (history.isActivityDataComplete ? .empty(startDate: startDate) : .unavailable(startDate: startDate)),tokenUsage: history.tokenUsageByDate[startDate])}diff --git a/CodexBar/Resources/Localizable.xcstrings b/CodexBar/Resources/Localizable.xcstringsindex 334aef1..809823e 100644--- a/CodexBar/Resources/Localizable.xcstrings+++ b/CodexBar/Resources/Localizable.xcstrings@@ -465,7 +465,7 @@}}},- "activity.live.actions-listFiles" : {+ "activity.live.actions-listfiles" : {"localizations" : {"en" : {"stringUnit" : {@@ -481,7 +481,7 @@}}},- "activity.live.actions-listFiles-search" : {+ "activity.live.actions-listfiles-search" : {"localizations" : {"en" : {"stringUnit" : {@@ -513,7 +513,7 @@}}},- "activity.live.actions-read-listFiles" : {+ "activity.live.actions-read-listfiles" : {"localizations" : {"en" : {"stringUnit" : {@@ -529,7 +529,7 @@}}},- "activity.live.actions-read-listFiles-search" : {+ "activity.live.actions-read-listfiles-search" : {"localizations" : {"en" : {"stringUnit" : {@@ -881,7 +881,7 @@}}},- "activity.live.command-listFiles" : {+ "activity.live.command-listfiles" : {"localizations" : {"en" : {"stringUnit" : {@@ -897,7 +897,7 @@}}},- "activity.live.command-listFiles-search" : {+ "activity.live.command-listfiles-search" : {"localizations" : {"en" : {"stringUnit" : {@@ -929,7 +929,7 @@}}},- "activity.live.command-read-listFiles" : {+ "activity.live.command-read-listfiles" : {"localizations" : {"en" : {"stringUnit" : {@@ -945,7 +945,7 @@}}},- "activity.live.command-read-listFiles-search" : {+ "activity.live.command-read-listfiles-search" : {"localizations" : {"en" : {"stringUnit" : {@@ -2385,6 +2385,22 @@}}},+ "codex-status.account.initialization-failed" : {+ "localizations" : {+ "en" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "Initialization failed"+ }+ },+ "zh-Hans" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "初始化失败"+ }+ }+ }+ },"codex-status.account.not-signed-in" : {"localizations" : {"en" : {@@ -3281,7 +3297,7 @@}}},- "history.metric.approvals" : {+ "history.activity-data.approvals" : {"localizations" : {"en" : {"stringUnit" : {@@ -3297,7 +3313,7 @@}}},- "history.metric.compactions" : {+ "history.activity-data.compactions" : {"localizations" : {"en" : {"stringUnit" : {@@ -3313,39 +3329,39 @@}}},- "history.metric.sessions" : {+ "history.activity-data.subagents" : {"localizations" : {"en" : {"stringUnit" : {"state" : "translated",- "value" : "Sessions"+ "value" : "Subagents"}},"zh-Hans" : {"stringUnit" : {"state" : "translated",- "value" : "会话总数"+ "value" : "子智能体"}}}},- "history.metric.subagents" : {+ "history.activity-data.threads" : {"localizations" : {"en" : {"stringUnit" : {"state" : "translated",- "value" : "Subagents"+ "value" : "Threads"}},"zh-Hans" : {"stringUnit" : {"state" : "translated",- "value" : "子智能体"+ "value" : "会话总数"}}}},- "history.metric.tool-calls" : {+ "history.activity-data.tool-calls" : {"localizations" : {"en" : {"stringUnit" : {@@ -3361,7 +3377,7 @@}}},- "history.metric.turns" : {+ "history.activity-data.turns" : {"localizations" : {"en" : {"stringUnit" : {@@ -3398,7 +3414,7 @@"en" : {"stringUnit" : {"state" : "translated",- "value" : "Recalculate activity data and session data for %@ from locally collected app-server events\nAfter syncing, local data will be replaced and cloud data updated"+ "value" : "Recalculate activity data and thread data for %@ from locally collected app-server events\nAfter syncing, local data will be replaced and cloud data updated"}},"zh-Hans" : {@@ -3425,38 +3441,6 @@}}},- "history.rebuild.error.incomplete-source" : {- "localizations" : {- "en" : {- "stringUnit" : {- "state" : "translated",- "value" : "The original event data contains unreadable records. Please fix them and retry."- }- },- "zh-Hans" : {- "stringUnit" : {- "state" : "translated",- "value" : "原始事件包含无法读取的记录, 请修复后重试"- }- }- }- },- "history.rebuild.error.source-changed" : {- "localizations" : {- "en" : {- "stringUnit" : {- "state" : "translated",- "value" : "Local data changed; try again"- }- },- "zh-Hans" : {- "stringUnit" : {- "state" : "translated",- "value" : "本地数据发生变化, 请重试"- }- }- }- },"history.rebuild.error.source-unavailable" : {"localizations" : {"en" : {@@ -3569,22 +3553,6 @@}}},- "history.rebuild.summary.request-failed" : {- "localizations" : {- "en" : {- "stringUnit" : {- "state" : "translated",- "value" : "; rebuild could not start, please retry"- }- },- "zh-Hans" : {- "stringUnit" : {- "state" : "translated",- "value" : "; 部分重建未能开始, 请重新尝试"- }- }- }- },"history.rebuild.summary.retry-later" : {"localizations" : {"en" : {@@ -3601,12 +3569,12 @@}}},- "history.rebuild.summary.token-history-incomplete" : {+ "history.rebuild.summary.token-usage-history-incomplete" : {"localizations" : {"en" : {"stringUnit" : {"state" : "translated",- "value" : "; session data rebuild incomplete, please retry"+ "value" : "; thread data rebuild incomplete, please retry"}},"zh-Hans" : {@@ -3617,12 +3585,12 @@}}},- "history.rebuild.summary.token-record-count" : {+ "history.rebuild.summary.token-usage-record-count" : {"localizations" : {"en" : {"stringUnit" : {"state" : "translated",- "value" : "; %lld session data records"+ "value" : "; %lld thread data records"}},"zh-Hans" : {@@ -4876,7 +4844,7 @@"zh-Hans" : {"stringUnit" : {"state" : "translated",- "value" : "异常会话保护"+ "value" : "异常任务保护"}}}@@ -5809,23 +5777,7 @@}}},- "sync.error.activity-generation" : {- "localizations" : {- "en" : {- "stringUnit" : {- "state" : "translated",- "value" : "Activity history for %@ is missing required information and cannot be synced"- }- },- "zh-Hans" : {- "stringUnit" : {- "state" : "translated",- "value" : "%@ 的活动历史信息不完整, 无法同步"- }- }- }- },- "sync.error.activity-identity" : {+ "sync.error.activity-data-identity" : {"localizations" : {"en" : {"stringUnit" : {@@ -6289,7 +6241,7 @@}}},- "usage.heatmap.activity-metrics" : {+ "usage.heatmap.activity-data-metrics" : {"localizations" : {"en" : {"stringUnit" : {@@ -6321,12 +6273,12 @@}}},- "usage.heatmap.token-metrics" : {+ "usage.heatmap.token-usage-metrics" : {"localizations" : {"en" : {"stringUnit" : {"state" : "translated",- "value" : "Session data"+ "value" : "Thread data"}},"zh-Hans" : {@@ -6401,34 +6353,34 @@}}},- "usage.summary.longest-chat" : {+ "usage.summary.longest-streak" : {"localizations" : {"en" : {"stringUnit" : {"state" : "translated",- "value" : "Longest Task"+ "value" : "Longest Streak"}},"zh-Hans" : {"stringUnit" : {"state" : "translated",- "value" : "最长任务"+ "value" : "最长连胜"}}}},- "usage.summary.longest-streak" : {+ "usage.summary.longest-turn" : {"localizations" : {"en" : {"stringUnit" : {"state" : "translated",- "value" : "Longest Streak"+ "value" : "Longest Turn"}},"zh-Hans" : {"stringUnit" : {"state" : "translated",- "value" : "最长连胜"+ "value" : "最长任务"}}}@@ -6449,7 +6401,7 @@}}},- "usage.tokens.cache-hit-rate" : {+ "usage.token-usage.cache-hit-rate" : {"localizations" : {"en" : {"stringUnit" : {@@ -6465,7 +6417,7 @@}}},- "usage.tokens.cache-write-input" : {+ "usage.token-usage.cache-write-input" : {"localizations" : {"en" : {"stringUnit" : {@@ -6481,7 +6433,7 @@}}},- "usage.tokens.cached-input" : {+ "usage.token-usage.cached-input" : {"localizations" : {"en" : {"stringUnit" : {@@ -6497,7 +6449,7 @@}}},- "usage.tokens.input" : {+ "usage.token-usage.input" : {"localizations" : {"en" : {"stringUnit" : {@@ -6513,7 +6465,7 @@}}},- "usage.tokens.output" : {+ "usage.token-usage.output" : {"localizations" : {"en" : {"stringUnit" : {@@ -6529,7 +6481,7 @@}}},- "usage.tokens.reasoning-output" : {+ "usage.token-usage.reasoning-output" : {"localizations" : {"en" : {"stringUnit" : {@@ -6545,7 +6497,7 @@}}},- "usage.tokens.total" : {+ "usage.token-usage.total" : {"localizations" : {"en" : {"stringUnit" : {@@ -6563,4 +6515,4 @@}},"version" : "1.0"-}+}\ No newline at end of filediff --git a/CodexBar/Services/Activity/ActivityLiveLabel+Localization.swift b/CodexBar/Services/Activity/ActivityLiveLabel+Localization.swiftindex 0f3b7f6..4d30e9a 100644--- a/CodexBar/Services/Activity/ActivityLiveLabel+Localization.swift+++ b/CodexBar/Services/Activity/ActivityLiveLabel+Localization.swift@@ -30,16 +30,16 @@ nonisolated extension ActivityLiveLabel {/// 完整键名保留为字面量, 供 Xcode 提取和校验本地化文案var localizedText: String {switch key {- case "actions-listFiles":- String(localized: "activity.live.actions-listFiles")- case "actions-listFiles-search":- String(localized: "activity.live.actions-listFiles-search")+ case "actions-listfiles":+ String(localized: "activity.live.actions-listfiles")+ case "actions-listfiles-search":+ String(localized: "activity.live.actions-listfiles-search")case "actions-read":String(localized: "activity.live.actions-read")- case "actions-read-listFiles":- String(localized: "activity.live.actions-read-listFiles")- case "actions-read-listFiles-search":- String(localized: "activity.live.actions-read-listFiles-search")+ case "actions-read-listfiles":+ String(localized: "activity.live.actions-read-listfiles")+ case "actions-read-listfiles-search":+ String(localized: "activity.live.actions-read-listfiles-search")case "actions-read-search":String(localized: "activity.live.actions-read-search")case "actions-search":@@ -80,16 +80,16 @@ nonisolated extension ActivityLiveLabel {String(localized: "activity.live.calling-tool")case "command":String(localized: "activity.live.command")- case "command-listFiles":- String(localized: "activity.live.command-listFiles")- case "command-listFiles-search":- String(localized: "activity.live.command-listFiles-search")+ case "command-listfiles":+ String(localized: "activity.live.command-listfiles")+ case "command-listfiles-search":+ String(localized: "activity.live.command-listfiles-search")case "command-read":String(localized: "activity.live.command-read")- case "command-read-listFiles":- String(localized: "activity.live.command-read-listFiles")- case "command-read-listFiles-search":- String(localized: "activity.live.command-read-listFiles-search")+ case "command-read-listfiles":+ String(localized: "activity.live.command-read-listfiles")+ case "command-read-listfiles-search":+ String(localized: "activity.live.command-read-listfiles-search")case "command-read-search":String(localized: "activity.live.command-read-search")case "command-search":diff --git a/CodexBar/Services/Activity/ActivityLivePresentation+Items.swift b/CodexBar/Services/Activity/ActivityLivePresentation+Items.swiftindex a1af358..6506c69 100644--- a/CodexBar/Services/Activity/ActivityLivePresentation+Items.swift+++ b/CodexBar/Services/Activity/ActivityLivePresentation+Items.swift@@ -28,7 +28,7 @@ nonisolated extension ActivityItem {let types = Set((commandActions ?? []).map(\.type))let actions = orderedCommandActionsguard !actions.isEmpty else { return ActivityLiveLabel("command") }- let key = "command-" + actions.joined(separator: "-")+ let key = "command-" + actions.map { $0.lowercased() }.joined(separator: "-")if types.isSubset(of: Set(actions)) {return ActivityLiveLabel(key)}@@ -96,7 +96,8 @@ nonisolated extension ActivityDisplayFormat {static func commandActionText(_ types: [String]) -> String? {let actions = orderedCommandActions(types)guard !actions.isEmpty else { return nil }- return ActivityLiveLabel("actions-" + actions.joined(separator: "-")).text+ let key = "actions-" + actions.map { $0.lowercased() }.joined(separator: "-")+ return ActivityLiveLabel(key).text}/// 使用命名类型避免编译器遗漏 switch 元组分支中的本地化引用diff --git a/CodexBar/Services/Activity/ActivityMonitor+Completion.swift b/CodexBar/Services/Activity/ActivityMonitor+Completion.swiftindex 3555678..8f52b04 100644--- a/CodexBar/Services/Activity/ActivityMonitor+Completion.swift+++ b/CodexBar/Services/Activity/ActivityMonitor+Completion.swift@@ -1,258 +1,18 @@-import Combineimport Foundation-import osextension ActivityMonitor {- func finishTask(from event: ActivityRecord, source: ActivityEventSource, into transitions: inout [ActivityTransition]) {- guard let eventKey = ActivityTaskKey(event: event) else { return }- guard recentEndedDate(for: eventKey) == nil else {- discardStaleTerminalTask(for: eventKey)- return- }- let key = eventKey- var task = pendingTerminalTasks[key]?.task ?? tasks[key] ?? ActivityTask(- displayID: UUID(), key: key, event: event, state: .running,- startedAt: event.context?.turnStartedAt, progressGeneration: 0- )- // 服务端轮次时间可能只有整秒精度, 同秒内的工具事件不应阻止明确终态- let timestampTolerance: TimeInterval = event.context?.turnCompletedAt == nil ? 0 : 1- guard event.timestamp.addingTimeInterval(timestampTolerance) >= task.lastMainEventAt else { return }- task.mergeMetadata(from: event)- if task.startedAt == nil, let start = event.context?.turnStartedAt, start <= event.timestamp {- task.startedAt = start- }- tasks.removeValue(forKey: key)- pendingTerminalTasks.removeValue(forKey: key)- let completedAt = event.context == nil ? event.timestamp : event.context?.turnCompletedAt- let terminal: SessionTerminalState- if event.eventKind == .turnCompleted {- terminal = .completed(at: completedAt, duration: event.context?.duration)- } else {- task.terminalFailed = task.terminalFailed || event.context?.turnStatus == .failed- terminal = .aborted(at: completedAt, duration: event.context?.duration)- }- resolveTerminal(- terminal, task: task, key: key, abortFallback: event.timestamp,- publishesEvents: source == .live, into: &transitions- )- recordEndedTask(eventKey, at: event.timestamp)- }-- // MARK: - 终态判定与记录-- /// app-server 终态归类的唯一入口; 活动任务和等待终态确认任务只有 abort 兜底时间不同- /// 终止记录供任务中心和流光展示, 不发布通知 transition- func resolveTerminal(- _ terminal: SessionTerminalState,- task: ActivityTask,- key: ActivityTaskKey,- abortFallback: Date,- publishesEvents: Bool = true,- into transitions: inout [ActivityTransition]- ) {- clearProtection(- for: key,- taskID: task.displayID,- reason: .terminal- )- guard recentEndedDate(for: key) == nil else {- return- }- switch terminal {- case let .aborted(reportedAt, duration):- let terminatedAt = max(reportedAt ?? abortFallback, task.lastActivityAt)- let termination = storeTermination(- task, at: terminatedAt,- duration: duration ?? reportedAt.flatMap { task.preciseDuration(until: $0) }- )- if publishesEvents {- recordTerminalPresentationEvent(.terminated(termination))- }- recordEndedTask(key, at: terminatedAt)- case let .completed(completedAt, duration):- let completion = storeResolvedCompletion(- task,- key: key,- completedAt: completedAt,- observedAt: abortFallback,- reportedDuration: duration- )- guard publishesEvents else { return }- recordTerminalPresentationEvent(.completed(completion))- if canPublishActivityTransitions,- Date().timeIntervalSince(completion.completedAt) <= 10,- let sessionTransitionNotBefore,- completion.completedAt >= sessionTransitionNotBefore {- transitions.append(.completed(completion))- }- }- }-- static func backfilledStartedAt(- for task: ActivityTask,- state: SessionLifecycleState- ) -> Date? {- guard task.startedAt == nil,- let startedAt = state.startedAt,- startedAt <= task.lastActivityAt.addingTimeInterval(1) else {- return nil- }- return startedAt- }-- static func mergeLifecycleBackfill(- from state: SessionLifecycleState,- into task: inout ActivityTask- ) -> Bool {- var didChange = false- if state.readStatus == .complete, let status = state.turnStatus {- task.terminalFailed = status == .failed- }- if let startedAt = backfilledStartedAt(for: task, state: state) {- task.startedAt = startedAt- didChange = true- }- if task.mergeEffort(state.effort) {- didChange = true- }- return didChange- }-- private func storeResolvedCompletion(- _ task: ActivityTask,- key: ActivityTaskKey,- completedAt: Date?,- observedAt: Date,- reportedDuration: TimeInterval?- ) -> ActivityCompletion {- // 轮次时间戳可能是整秒, 避免将完成时间记在同秒的最后活动之前- let recordedCompletedAt = max(completedAt ?? observedAt, task.lastActivityAt)- let completion = ActivityCompletion(- id: UUID(),- taskID: task.displayID,- projectName: task.projectName,- modelName: task.modelName,- effort: task.effort,- completedAt: recordedCompletedAt,- duration: reportedDuration ?? completedAt.flatMap { task.preciseDuration(until: $0) }- )- completions.append(completion)- registerTerminalTokenUsage(id: completion.id, task: task)- recordEndedTask(key, at: recordedCompletedAt)- return completion- }-- private func storeTermination(- _ task: ActivityTask,- at terminatedAt: Date,- duration: TimeInterval?- ) -> ActivityTermination {- let termination = ActivityTermination(- id: UUID(),- taskID: task.displayID,- projectName: task.projectName,- modelName: task.modelName,- effort: task.effort,- terminatedAt: terminatedAt,- duration: duration,- isFailure: task.terminalFailed- )- terminations.append(termination)- registerTerminalTokenUsage(id: termination.id, task: task)- return termination- }-- func recordTerminalPresentationEvent(_ event: ActivityTerminalEvent) {- guard canPublishActivityTransitions, Date().timeIntervalSince(event.endedAt) <= 10,- event.endedAt >= terminalPresentationNotBefore else {- return- }- pendingTerminalPresentationEvents.append(event)- }-- func resetTerminalPresentationEvents() {- pendingTerminalPresentationEvents.removeAll()- // 延迟确认仍使用原始结束时间, 恢复前的旧终态不能在恢复后补播+ func resetTerminalPresentationCutoff() {terminalPresentationNotBefore = Date()}- func recentEndedDate(- for key: ActivityTaskKey,- now: Date = Date()- ) -> Date? {- guard let date = recentlyEndedTaskAt[key] else {- return nil- }- guard date > now.addingTimeInterval(-Self.endedTaskRetention) else {- recentlyEndedTaskAt.removeValue(forKey: key)- return nil- }- return date- }-- func recordEndedTask(_ key: ActivityTaskKey, at date: Date) {- recentlyEndedTaskAt[key] = max(recentlyEndedTaskAt[key] ?? .distantPast, date)- }-func clearCollectedActivityState() {- resetTerminalPresentationEvents()+ resetTerminalPresentationCutoff()cancelAllProtectionAttempts()- let taskIDs = Set(tasks.values.map(\.displayID))- .union(pendingTerminalTasks.values.map(\.task.displayID))+ let taskIDs = Set(tasks.values.map(\.displayID)).union(pendingTerminalTasks.values.map(\.task.displayID))for taskID in taskIDs {invalidateProtectionNotification(for: taskID)}- tasks.removeAll()- pendingTerminalTasks.removeAll()- completions.removeAll()- terminations.removeAll()- recentlyEndedTaskAt.removeAll()- terminalTokenUsageRequests.removeAll()- pendingSubagentEvents.removeAll()- subagentTurnLinks.removeAll()- }-- func finalizeExpiredPendingTerminalTasks(now: Date) {- let expiredKeys = pendingTerminalTasks.compactMap { key, pending in- pending.expiresAt <= now ? key : nil- }- for key in expiredKeys {- guard let pending = pendingTerminalTasks.removeValue(forKey: key) else {- continue- }- clearProtection(- for: key,- taskID: pending.task.displayID,- reason: .terminal- )- }- }-- func publishWaitingApprovalTransitions(_ taskKeys: [ActivityTaskKey]) {- for transition in waitingApprovalTransitions(taskKeys) {- transitionSubject.send(transition)- }- }-- func waitingApprovalTransitions(_ taskKeys: [ActivityTaskKey]) -> [ActivityTransition] {- guard canPublishActivityTransitions else { return [] }- var transitions: [ActivityTransition] = []- var lastWaitingIndexByKey: [ActivityTaskKey: Int] = [:]- for (index, key) in taskKeys.enumerated() {- lastWaitingIndexByKey[key] = index- }-- for (index, key) in taskKeys.enumerated() {- guard lastWaitingIndexByKey[key] == index,- let task = tasks[key],- task.state == .waitingApproval,- let sessionTransitionNotBefore,- task.stateChangedAt >= sessionTransitionNotBefore,- Date().timeIntervalSince(task.stateChangedAt) <= 10 else {- continue- }- transitions.append(.waitingApproval(task.snapshot))- }- return transitions+ suppressedTasks.removeAll()+ graph = ActivityTaskGraph()}}diff --git a/CodexBar/Services/Activity/ActivityMonitor+Protection.swift b/CodexBar/Services/Activity/ActivityMonitor+Protection.swiftindex 27cd637..a4aa0a5 100644--- a/CodexBar/Services/Activity/ActivityMonitor+Protection.swift+++ b/CodexBar/Services/Activity/ActivityMonitor+Protection.swift@@ -45,8 +45,8 @@ extension ActivityMonitor {func beginProtectionRecovery() -> UInt64 {protectionRecoveryGeneration &+= 1isProtectionRecoveryInProgress = true- sessionTransitionNotBefore = nil- resetTerminalPresentationEvents()+ turnTransitionNotBefore = nil+ resetTerminalPresentationCutoff()cancelInactivityCheck()cancelAllProtectionAttempts()return protectionRecoveryGeneration@@ -56,9 +56,9 @@ extension ActivityMonitor {guard generation == protectionRecoveryGeneration else {return}- resetTerminalPresentationEvents()+ resetTerminalPresentationCutoff()isProtectionRecoveryInProgress = false- sessionTransitionNotBefore = Date()+ turnTransitionNotBefore = Date()reconcileProtection(now: Date(), sendsNotification: false)}@@ -75,7 +75,7 @@ extension ActivityMonitor {return}- for (key, var task) in tasks {+ for (key, task) in tasks {guard task.hasFreshLifecycle(at: now),let record = protectionRecords[key.protectionIdentifier] else {continue@@ -90,9 +90,7 @@ extension ActivityMonitor {continue}- task.state = .suppressed- task.stateChangedAt = record.markedAt- tasks[key] = task+ suppressedTasks[key] = record.markedAt}}@@ -113,7 +111,7 @@ extension ActivityMonitor {let threshold = protectionSettings.inactivityDuration.timeIntervallet restorableKeys = tasks.compactMap { key, task -> ActivityTaskKey? in- guard task.state == .suppressed,+ guard suppressedTasks[key] != nil,task.protectionReferenceAt.addingTimeInterval(threshold) > now else {return nil}@@ -124,7 +122,7 @@ extension ActivityMonitor {}let overdueKeys = tasks.compactMap { key, task -> ActivityTaskKey? in- guard task.state == .running,+ guard task.state == .running, suppressedTasks[key] == nil,let deadline = task.protectionDeadline(at: now, inactivityDuration: threshold),deadline <= now else {return nil@@ -145,7 +143,7 @@ extension ActivityMonitor {let threshold = protectionSettings.inactivityDuration.timeIntervallet nextDeadline = tasks.compactMap { key, task -> Date? in- guard task.state == .running,+ guard task.state == .running, suppressedTasks[key] == nil,protectionAttempts[key] == nil else {return nil}@@ -189,7 +187,7 @@ extension ActivityMonitor {let threshold = protectionSettings.inactivityDuration.timeIntervallet candidates = tasks.compactMap { key, task -> ProtectionCandidate? in- guard task.state == .running,+ guard task.state == .running, suppressedTasks[key] == nil,protectionAttempts[key] == nil,let deadline = task.protectionDeadline(at: now, inactivityDuration: threshold),deadline <= now else {@@ -293,12 +291,8 @@ extension ActivityMonitor {)}- guard var task = tasks[key] else {- return- }- task.state = .suppressed- task.stateChangedAt = attempt.markedAt- tasks[key] = task+ guard tasks[key] != nil else { return }+ suppressedTasks[key] = attempt.markedAtAppLog.activity.notice("异常任务已隐藏: thresholdMinutes=\(attempt.candidate.inactivityDuration.loggedMinutes)")@@ -310,7 +304,7 @@ extension ActivityMonitor {markedAt: Date) {cancelProtectionAttempt(for: key)- guard var task = tasks[key],+ guard let task = tasks[key],task.state == .running else {return}@@ -320,9 +314,7 @@ extension ActivityMonitor {lastProgressAt: task.lastProgressAt,markedAt: markedAt)- task.state = .suppressed- task.stateChangedAt = markedAt- tasks[key] = task+ suppressedTasks[key] = markedAtAppLog.activity.notice("异常任务已静默隐藏: reason=reconcile")}@@ -332,7 +324,7 @@ extension ActivityMonitor {) {guard canEvaluateProtection,let task = tasks[key],- task.state == .running,+ task.state == .running, suppressedTasks[key] == nil,let deadline = task.protectionDeadline(at: now, inactivityDuration: protectionSettings.inactivityDuration.timeInterval),deadline <= now else {return@@ -342,15 +334,12 @@ extension ActivityMonitor {func restoreActivityTaskForCurrentThreshold(_ key: ActivityTaskKey,- restoredAt: Date+ restoredAt _: Date) {- guard var task = tasks[key], task.state == .suppressed else {+ guard let task = tasks[key], suppressedTasks[key] != nil else {return}- task.state = .running- task.stateChangedAt = restoredAt- tasks[key] = taskclearProtection(for: key,taskID: task.displayID,@@ -359,17 +348,15 @@ extension ActivityMonitor {AppLog.activity.notice("异常任务已静默恢复: reason=thresholdChange")}- func restoreAllSuppressedActivityTasks(restoredAt: Date) {- let suppressedKeys = tasks.compactMap { key, task in- task.state == .suppressed ? key : nil+ func restoreAllSuppressedActivityTasks(restoredAt _: Date) {+ let suppressedKeys = tasks.compactMap { key, _ in+ suppressedTasks[key] != nil ? key : nil}for key in suppressedKeys {- guard var task = tasks[key] else {+ guard let task = tasks[key] else {continue}- task.state = .running- task.stateChangedAt = restoredAt- tasks[key] = task+ suppressedTasks.removeValue(forKey: key)invalidateProtectionNotification(for: task.displayID)}for (taskID, attemptID) in Array(protectionNoticeAttemptIDs) {@@ -390,7 +377,7 @@ extension ActivityMonitor {protectionSettings.inactivityDuration == candidate.inactivityDuration,let task = tasks[candidate.key],task.displayID == candidate.taskID,- task.state == .running,+ task.state == .running, suppressedTasks[candidate.key] == nil,task.lastProgressAt == candidate.lastProgressAt,task.progressGeneration == candidate.progressGeneration else {return false@@ -428,6 +415,7 @@ extension ActivityMonitor {taskID: UUID?,reason: ProtectionClearReason) {+ suppressedTasks.removeValue(forKey: key)cancelProtectionAttempt(for: key)let identifier = key.protectionIdentifierif let record = protectionRecords.removeValue(forKey: identifier) {diff --git a/CodexBar/Services/Activity/ActivityMonitor.swift b/CodexBar/Services/Activity/ActivityMonitor.swiftindex 8bc396f..cae93d6 100644--- a/CodexBar/Services/Activity/ActivityMonitor.swift+++ b/CodexBar/Services/Activity/ActivityMonitor.swift@@ -22,32 +22,45 @@ final class ActivityMonitor: ObservableObject {let protectionSettings: ProtectionSettingslet protectionStore: ProtectionStore- private let activityDirectoryURL: URL- private var lifecycleCache = SessionLifecycleCache()+ private let activityDataDirectoryURL: URLlet transitionSubject = PassthroughSubject<ActivityTransition, Never>()private let presentationSubject = PassthroughSubject<ActivityPresentationUpdate, Never>()- var pendingTerminalPresentationEvents: [ActivityTerminalEvent] = []var terminalPresentationNotBefore = Date()- var tasks: [ActivityTaskKey: ActivityTask] = [:]- var subagentTurnLinks: [ActivityTurnReference: (root: ActivityTaskKey, retainedAt: Date)] = [:]- var pendingSubagentEvents: [PendingSubagentEvent] = []- var pendingTerminalTasks: [ActivityTaskKey: PendingTerminalTask] = [:]- var completions: [ActivityCompletion] = []- var terminations: [ActivityTermination] = []- var recentlyEndedTaskAt: [ActivityTaskKey: Date] = [:]- var terminalTokenUsageRequests: [UUID: TaskTokenRequest] = [:]+ var graph = ActivityTaskGraph()+ var tasks: [ActivityTaskKey: ActivityTask] {+ graph.tasks+ }++ var completions: [ActivityCompletion] {+ graph.completions+ }++ var terminations: [ActivityTermination] {+ graph.terminations+ }++ var pendingTerminalTasks: [ActivityTaskKey: PendingTerminalTask] {+ graph.pendingTerminalTasks+ }++ var recentlyEndedTaskAt: [ActivityTaskKey: Date] {+ graph.recentlyEndedTaskAt+ }++ private var publishedReaderID: UUID?+ private var publishedRevision: UInt64 = 0var activityReader: AppServerActivityReader?private var activityReaderControlTask: Task<Void, Never>?private var activityReaderGeneration: UInt64 = 0private var recoveryTask: Task<Void, Never>?private var recoveryTaskID: UUID?private var isSystemSleeping = false- private var isReconcilingLifecycles = false- private var sessionLifecyclePollTask: Task<Void, Never>?+ private var recoveryRetryTask: Task<Void, Never>?private var cleanupTask: Task<Void, Never>?private var cleanupDeadline: Date?var inactivityCheckTask: Task<Void, Never>?var inactivityCheckDeadline: Date?+ var suppressedTasks: [ActivityTaskKey: Date] = [:]var protectionAttempts: [ActivityTaskKey: ProtectionAttempt] = [:]var protectionNoticeAttemptIDs: [UUID: UUID] = [:]var protectionRecords: [String: ProtectionRecord] = [:]@@ -71,21 +84,16 @@ final class ActivityMonitor: ObservableObject {private var isPreparingForTermination = falsevar isStarted = falsevar isBootstrapping = false- /// 初始快照跨多个批次到达, 事件数累加到 bootstrapEnd 才一次记完- /// reader 每次重试都会重新发一遍 bootstrapStart, 所以事件数跟着重置, 但耗时要累计- private var bootstrapEventCount = 0- private var bootstrapDuration = LogDuration()- private var bootstrapCompletionGeneration: UInt64 = 0- var sessionTransitionNotBefore: Date?+ var turnTransitionNotBefore: Date?init(protectionSettings: ProtectionSettings,protectionStore: ProtectionStore = ProtectionStore(),- activityDirectoryURL: URL = HistoryStorage.directoryURL()+ activityDataDirectoryURL: URL = HistoryStorage.directoryURL()) {self.protectionSettings = protectionSettingsself.protectionStore = protectionStore- self.activityDirectoryURL = activityDirectoryURL+ self.activityDataDirectoryURL = activityDataDirectoryURL}func start() {@@ -161,8 +169,8 @@ final class ActivityMonitor: ObservableObject {recoveryTask = nilresetProtectionRecovery()cancelInactivityCheck()- sessionLifecyclePollTask?.cancel()- sessionLifecyclePollTask = nil+ recoveryRetryTask?.cancel()+ recoveryRetryTask = nilactivityReaderControlTask?.cancel()activityReaderControlTask = nilisActivitySourceHealthy = false@@ -175,7 +183,7 @@ final class ActivityMonitor: ObservableObject {loadProtectionState()if let reader = activityReader {await reader.start()- startSessionLifecyclePolling(generation: activityReaderGeneration)+ startRecoveryRetrying(generation: activityReaderGeneration)} else {startReaderIfReady()}@@ -233,11 +241,9 @@ final class ActivityMonitor: ObservableObject {activityReaderGeneration &+= 1let generation = activityReaderGeneration- lifecycleCache = SessionLifecycleCache()let reader = AppServerActivityReader(- lifecycleCache: lifecycleCache,- tokenHistory: TokenHistoryStore(directoryURL: activityDirectoryURL),- recorder: ActivityRecorder(directoryURL: activityDirectoryURL),+ tokenUsageHistory: TokenUsageHistoryStore(directoryURL: activityDataDirectoryURL),+ activityDataRecorder: ActivityDataRecorder(directoryURL: activityDataDirectoryURL),onAccountChange: { [weak self] change inguard let self, activityReaderGeneration == generation else { return }onAccountChange?(change)@@ -250,6 +256,8 @@ final class ActivityMonitor: ObservableObject {})activityReader = reader+ publishedReaderID = nil+ publishedRevision = 0activityReaderControlTask = Task { @MainActor [weak self] inawait reader.start()guard let self,@@ -257,7 +265,7 @@ final class ActivityMonitor: ObservableObject {activityReader != nil else {return}- startSessionLifecyclePolling(generation: generation)+ startRecoveryRetrying(generation: generation)}}@@ -275,8 +283,8 @@ final class ActivityMonitor: ObservableObject {await reader.stop()}}- sessionLifecyclePollTask?.cancel()- sessionLifecyclePollTask = nil+ recoveryRetryTask?.cancel()+ recoveryRetryTask = nilcancelInactivityCheck()sourcePresentation = ActivityLiveLabel("unavailable")isActivitySourceHealthy = false@@ -285,161 +293,33 @@ final class ActivityMonitor: ObservableObject {cleanupDeadline = nilclearCollectedActivityState()isBootstrapping = false- sessionTransitionNotBefore = nil+ turnTransitionNotBefore = nilpublishSnapshot(.empty)}- // MARK: - 会话生命周期+ // MARK: - 轮次生命周期- private func startSessionLifecyclePolling(generation: UInt64) {- guard sessionLifecyclePollTask == nil else {+ private func startRecoveryRetrying(generation: UInt64) {+ guard recoveryRetryTask == nil else {return}- sessionLifecyclePollTask = Task { @MainActor [weak self] in+ recoveryRetryTask = Task { @MainActor [weak self] inwhile !Task.isCancelled {- guard let self else {+ guard let self, generation == activityReaderGeneration else {return}if isProtectionRecoveryInProgress {if !isSystemSleeping, recoveryTask == nil {requestActivityRecovery()}- } else {- _ = await reconcileSessionLifecycles(generation: generation, terminalOnly: !isActivitySourceHealthy)}- try? await Task.sleep(for: .seconds(Self.sessionLifecyclePollInterval))- }- }- }-- private func refreshSessionLifecycleNow() {- guard activityReader != nil else {- return- }- let generation = activityReaderGeneration-- Task { @MainActor [weak self] in- guard let self,- activityReaderGeneration == generation else {- return- }- guard !Task.isCancelled,- activityReaderGeneration == generation else {- return- }- await reconcileSessionLifecycles(generation: generation)- }- }-- /// 把一条 app-server 生命周期状态合进当前任务, 返回是否改动过状态- /// 待确认终态的任务与在跑的任务走两条分支, 前者已经从 tasks 里挪走- private func applyLifecycleState(- _ state: SessionLifecycleState,- terminalOnly: Bool = false,- into transitions: inout [ActivityTransition]- ) -> Bool {- guard !terminalOnly || (state.readStatus == .complete && state.terminal != nil) else { return false }- let key = ActivityTaskKey(thread: state.requestedThreadID, turn: state.turnID)- if var pending = pendingTerminalTasks[key] {- let pendingDidChange = Self.mergeLifecycleBackfill(- from: state,- into: &pending.task- )-- guard state.readStatus == .complete, let terminal = state.terminal else {- guard pendingDidChange else {- return false- }- pendingTerminalTasks[key] = pending- return true- }- pendingTerminalTasks.removeValue(forKey: key)- resolveTerminal(- terminal,- task: pending.task,- key: key,- abortFallback: state.terminalObservedAt ?? state.contextObservedAt ?? pending.supersededAt,- publishesEvents: !state.isHistoricalTerminal,- into: &transitions- )- return true- }-- guard var task = tasks[key] else {- return false- }-- let hadLifecycleCoverage = task.lifecycleCoverageCheckedAt != nil- if !terminalOnly {- let now = Date()- task.recordLifecycleRead(state, at: now)- if task.protectionDeadline(at: now, inactivityDuration: protectionSettings.inactivityDuration.timeInterval)- .map({ $0 <= now }) != true {- cancelProtectionAttempt(for: key)- }- }- _ = Self.mergeLifecycleBackfill(from: state, into: &task)- let progressDidChange = !terminalOnly && mergeLifecycleProgress(- from: state,- key: key,- into: &task- )-- if state.readStatus == .complete, let terminal = state.terminal {- tasks.removeValue(forKey: key)- resolveTerminal(- terminal,- task: task,- key: key,- abortFallback: state.terminalObservedAt ?? state.contextObservedAt ?? Date(),- publishesEvents: !state.isHistoricalTerminal,- into: &transitions- )- return true- }-- let wasSuppressed = task.state == .suppressed- task.mergeExecutionLifecycle(state, owner: ActivityExecutionKey(agentID: nil, turnID: state.turnID))- if wasSuppressed, task.state == .waitingApproval {- clearProtection(for: key, taskID: task.displayID, reason: .progress)- }-- tasks[key] = task- if progressDidChange || (!hadLifecycleCoverage && task.lifecycleCoverageCheckedAt != nil) {- suppressBackfilledActivityTaskIfOverdue(key, now: Date())- }- return true- }-- func mergeLifecycleProgress(- from state: SessionLifecycleState,- key: ActivityTaskKey,- into task: inout ActivityTask- ) -> Bool {- guard state.readStatus == .complete,- let lastProgressAt = state.lastProgressAt,- lastProgressAt > task.lastProgressAt else {- return false- }-- let wasSuppressed = task.state == .suppressed- task.recordProgress(at: lastProgressAt)- if shouldRestoreProtection(for: key, progressAt: lastProgressAt) {- if wasSuppressed {- task.state = .running- task.stateChangedAt = lastProgressAt+ try? await Task.sleep(for: .seconds(Self.recoveryRetryInterval))}- clearProtection(- for: key,- taskID: task.displayID,- reason: .progress- )}- return true}- /// 恢复只由读取屏障后的调用推进, 普通 poll 不能绕过恢复代次+ /// 恢复结果携带屏障之后发布的同一份快照, 不再跨 actor 查询第二份状态private func requestActivityRecovery() {guard !isSystemSleeping, let reader = activityReader, !isBootstrapping else { return }recoveryTask?.cancel()@@ -457,493 +337,115 @@ final class ActivityMonitor: ObservableObject {}}guard !Task.isCancelled, generation == activityReaderGeneration,- recoveryGeneration == protectionRecoveryGeneration else { return }- if case .sourceUnavailable = result {- // 连接缺口只阻止完整恢复, 独立读取成功的明确终态仍可静默收敛- _ = await reconcileSessionLifecycles(generation: generation, terminalOnly: true)- return- }- guard case .completed = result, isActivitySourceHealthy, !isBootstrapping else { return }- guard !Task.isCancelled, !isSystemSleeping,- recoveryGeneration == protectionRecoveryGeneration else { return }- let didReconcile = await reconcileSessionLifecycles(generation: generation, recovering: true)- guard didReconcile, !Task.isCancelled, generation == activityReaderGeneration,- recoveryGeneration == protectionRecoveryGeneration else { return }- finishProtectionRecovery(generation: recoveryGeneration)- }- }-- func lifecycleReferences() -> [ActivityTurnReference] {- var references = activeTokenUsageReferences()- references.append(contentsOf: subagentLifecycleReferences())- references.append(contentsOf: terminalTokenUsageReferences())- references.append(contentsOf: pendingTerminalTasks.values.map(\.task.turnReference))- return Array(Set(references))- }-- @discardableResult- private func reconcileSessionLifecycles(- generation: UInt64,- recovering: Bool = false,- terminalOnly: Bool = false- ) async -> Bool {- guard generation == activityReaderGeneration, activityReader != nil, !isBootstrapping,- terminalOnly || isActivitySourceHealthy, !isSystemSleeping,- recovering || terminalOnly || !isProtectionRecoveryInProgress,- !isReconcilingLifecycles else { return false }- isReconcilingLifecycles = true- defer { isReconcilingLifecycles = false }- let bootstrapGeneration = bootstrapCompletionGeneration- let recoveryGeneration = protectionRecoveryGeneration- let references = lifecycleReferences()- let states = await lifecycleCache.lifecycleStates(for: references)- guard !Task.isCancelled, generation == activityReaderGeneration,- bootstrapGeneration == bootstrapCompletionGeneration,- recoveryGeneration == protectionRecoveryGeneration,- activityReader != nil, !isBootstrapping, terminalOnly || isActivitySourceHealthy,- !isSystemSleeping else { return false }- let unavailable = Set(states.filter { $0.readStatus != .complete }.map { ActivityTaskKey(thread: $0.requestedThreadID, turn: $0.turnID) })- let availabilityChanged = unavailableTurns != unavailable- unavailableTurns = unavailable- var didChange = availabilityChanged- var transitions: [ActivityTransition] = []- for state in states {- didChange = applySubagentLifecycle(state, terminalOnly: terminalOnly) || didChange- didChange = applyLifecycleState(state, terminalOnly: terminalOnly, into: &transitions) || didChange- }- if !terminalOnly {- didChange = replayAssociatedSubagentEvents(into: &transitions) || didChange- didChange = reconcileSubagentCounts(states) || didChange- }- if !terminalOnly {- didChange = applyActiveTokenUsage(states) || didChange- }- didChange = applyTerminalTokenUsage(states) || didChange- if didChange {- refreshSnapshot(now: Date())- }- if canPublishActivityTransitions {- for transition in transitions {- if case let .waitingApproval(snapshot) = transition,- !tasks.values.contains(where: { $0.displayID == snapshot.id && $0.state == .waitingApproval }) {- continue- }- transitionSubject.send(transition)+ recoveryGeneration == protectionRecoveryGeneration, !isSystemSleeping else { return }+ switch result {+ case let .completed(update):+ consume(update)+ guard isActivitySourceHealthy, !isBootstrapping else { return }+ finishProtectionRecovery(generation: recoveryGeneration)+ case let .partial(update):+ consume(update)+ case .sourceUnavailable, .cancelled: break}}- return true}var canPublishActivityTransitions: Bool {!isBootstrapping && !isProtectionRecoveryInProgress && isActivitySourceHealthy}- // MARK: - 活动事件消费-- func consume(_ batch: ActivityEventBatch) {+ func consume(_ update: ActivityUpdate) {guard !isPreparingForTermination else { return }- switch batch {- case .lifecycleChanged:- refreshSessionLifecycleNow()+ if let publishedReaderID {+ guard publishedReaderID == update.readerID, update.revision > publishedRevision else { return }+ } else {+ publishedReaderID = update.readerID+ }+ publishedRevision = update.revision+ let previous = graph.tasks+ graph = update.graph+ unavailableTurns = graph.unavailableTurns+ for (key, old) in previous {+ guard let task = tasks[key] else {+ clearProtection(for: key, taskID: old.displayID, reason: .terminal)+ continue+ }+ if task.state == .waitingApproval || task.progressGeneration != old.progressGeneration,+ shouldRestoreProtection(for: key, progressAt: task.lastProgressAt) {+ clearProtection(for: key, taskID: task.displayID, reason: .progress)+ }+ if task.protectionDeadline(at: Date(), inactivityDuration: protectionSettings.inactivityDuration.timeInterval)+ .map({ $0 <= Date() }) != true {+ cancelProtectionAttempt(for: key)+ }+ }+ switch update.phase {case .bootstrapStart:sourcePresentation = ActivityLiveLabel(hasConnectedActivitySource ? "reconnecting" : "connecting")recoveryTask?.cancel()recoveryTask = nil- // 重试会重新查询初始快照, 计时从第一次开始算才是用户等到的总时长- if !isBootstrapping {- bootstrapDuration = LogDuration()- }isBootstrapping = true- bootstrapCompletionGeneration &+= 1- sessionTransitionNotBefore = nil- bootstrapEventCount = 0+ turnTransitionNotBefore = nilisActivitySourceHealthy = false- cancelInactivityCheck()- clearCollectedActivityState()+ resetTerminalPresentationCutoff()+ beginProtectionRecovery()publishSnapshot(.empty)- case let .snapshotEvents(events):+ case .snapshotEvents:if isBootstrapping {sourcePresentation = ActivityLiveLabel("recovering-state")- bootstrapEventCount += events.count- }- var transitions: [ActivityTransition] = []- for event in events {- _ = apply(event, source: .bootstrap, into: &transitions)}case .bootstrapEnd:- sourcePresentation = ActivityLiveLabel("recovering-state")hasConnectedActivitySource = trueisActivitySourceHealthy = true- sessionTransitionNotBefore = Date()- let completionGeneration = bootstrapCompletionGeneration- Task { @MainActor [weak self] in- await self?.finishBootstrap(- completionGeneration: completionGeneration- )- }- case let .live(events):- let activeCountBefore = snapshot.activeCount- var waitingTaskKeys: [ActivityTaskKey] = []- var transitions: [ActivityTransition] = []- for event in events {- if let key = apply(event, source: .live, into: &transitions) {- waitingTaskKeys.append(key)- }- }-- refreshSnapshot(now: Date())- // 活跃数变化覆盖任务起止, waitingTaskKeys 覆盖等待批准- // 只看活跃数会漏掉 running 转 waitingApproval, 那一进一出恒抵消为零- let activeCountAfter = snapshot.activeCount- if activeCountAfter != activeCountBefore || !waitingTaskKeys.isEmpty {- let details = LogFields.joined(- "from=\(activeCountBefore)",- "to=\(activeCountAfter)",- "transitions=\(waitingTaskKeys.count)"- )- AppLog.activity.notice("任务数变化: \(details, privacy: .public)")- }- for transition in transitions {- transitionSubject.send(transition)- }- publishWaitingApprovalTransitions(waitingTaskKeys)- if !pendingTerminalTasks.isEmpty {- // 新进入终态确认窗口的任务立即查询, 不等待下次周期核对- refreshSessionLifecycleNow()- }+ isBootstrapping = false+ sourcePresentation = nil+ turnTransitionNotBefore = Date()+ resetTerminalPresentationCutoff()+ if !isSystemSleeping, update.isComplete, graph.unavailableTurns.isEmpty {+ finishProtectionRecovery(generation: protectionRecoveryGeneration)+ }+ applyPersistedProtection(now: Date())+ reconcileProtection(now: Date(), sendsNotification: false)case .sourceUnavailable:- sourcePresentation = ActivityLiveLabel("unavailable")- guard isActivitySourceHealthy else {- return- }isActivitySourceHealthy = false- resetTerminalPresentationEvents()+ sourcePresentation = ActivityLiveLabel("unavailable")+ resetTerminalPresentationCutoff()beginProtectionRecovery()- publishSnapshot(.empty)cancelInactivityCheck()- cancelAllProtectionAttempts()- AppLog.activity.error(- "异常任务判定已暂停: reason=activitySourceUnavailable"- )- }- }-- private func finishBootstrap(- completionGeneration: UInt64- ) async {- let recoveryGeneration = protectionRecoveryGeneration- let references = lifecycleReferences()- if !references.isEmpty {- let states = await lifecycleCache.lifecycleStates(for: references)- guard !Task.isCancelled, completionGeneration == bootstrapCompletionGeneration,- activityReader != nil else {- return- }- var ignoredTransitions: [ActivityTransition] = []- if recoveryGeneration == protectionRecoveryGeneration, isActivitySourceHealthy, !isSystemSleeping {- for state in states {- _ = applySubagentLifecycle(state, terminalOnly: false)- _ = applyLifecycleState(state, into: &ignoredTransitions)- }- _ = replayAssociatedSubagentEvents(into: &ignoredTransitions)- _ = reconcileSubagentCounts(states)- }- }-- guard completionGeneration == bootstrapCompletionGeneration,- activityReader != nil else {+ publishSnapshot(.empty)return+ case .live, .lifecycleChanged: break}- resetTerminalPresentationEvents()- isBootstrapping = false- sourcePresentation = isActivitySourceHealthy ? nil : ActivityLiveLabel("unavailable")- if isActivitySourceHealthy, !isSystemSleeping {- if recoveryGeneration == protectionRecoveryGeneration {- finishProtectionRecovery(generation: recoveryGeneration)- } else {- requestActivityRecovery()+ if !isSystemSleeping {+ for key in update.backfilledTasks {+ suppressBackfilledActivityTaskIfOverdue(key, now: Date())}}let now = Date()- applyPersistedProtection(now: now)- reconcileProtection(now: now, sendsNotification: false)-- let activeCount = snapshot.activeCount- let eventCount = bootstrapEventCount- let elapsed = bootstrapDuration.elapsed- let details = LogFields.joined(- "attempts=1",- "events=\(eventCount)",- "activeTasks=\(activeCount)",- "elapsed=\(elapsed)"- )- AppLog.activity.notice("历史回放完成: \(details, privacy: .public)")- }-- func apply(- _ event: ActivityRecord,- source: ActivityEventSource,- into transitions: inout [ActivityTransition]- ) -> ActivityTaskKey? {- guard event.origin == .main || event.origin == .auxiliary else {- return nil- }-- if deferUnassociatedSubagentEvent(event, source: source) {- return nil- }- let isTopLevelEvent = event.agentID == nil- switch event.eventKind {- case .turnStarted:- guard isTopLevelEvent else { return nil }- startTask(from: event, source: source)- case .toolStarted, .toolCompleted, .compactionStarted, .compactionCompleted:- resumeTask(- from: event,- allowsRecovery: isTopLevelEvent,- source: source- )- case .subagentStarted:- // 子智能体只更新所属顶层任务, 不自行创建一条并发任务- updateSubagentActivity(from: event, isStarting: true, source: source)- case .subagentEnded:- updateSubagentActivity(from: event, isStarting: false, source: source)- case .approvalRequested:- return waitForApproval(from: event, source: source)- case .turnCompleted, .turnAborted:- guard isTopLevelEvent else { return nil }- finishTask(from: event, source: source, into: &transitions)- case .none:- break+ let terminalEvents = canPublishActivityTransitions ? update.terminalEvents.filter {+ now.timeIntervalSince($0.endedAt) <= 10 && $0.endedAt >= terminalPresentationNotBefore+ } : []+ refreshSnapshot(now: now, terminalEvents: terminalEvents)+ for transition in update.transitions where isCurrentTransition(transition) {+ transitionSubject.send(transition)}- return nil}- // MARK: - 任务状态转换-- private func startTask(- from event: ActivityRecord,- source: ActivityEventSource- ) {- guard let key = ActivityTaskKey(event: event) else { return }- guard pendingTerminalTasks[key] == nil else { return }- let existingTask = tasks[key]- let displayID = existingTask?.displayID ?? UUID()- guard recentEndedDate(for: key) == nil else { return }- if let existing = existingTask, event.timestamp < existing.lastMainEventAt {- return- }-- let threadID = key.threadID- // 当前轮次由 app-server 确认, 旧轮次的毫秒级活动时间不能否定秒级起点- // 旧任务退出活动列表后继续等待明确终态- let supersededTasks = tasks.values.filter {- $0.key != key && $0.key.threadID == threadID- }- for task in supersededTasks {- clearProtection(for: task.key, taskID: task.displayID, reason: .terminal)- pendingTerminalTasks[task.key] = PendingTerminalTask(- task: task,- supersededAt: event.timestamp- )- }- tasks = tasks.filter { taskKey, _ in- taskKey == key || taskKey.threadID != threadID- }- if source == .live {- clearProtection(- for: key,- taskID: displayID,- reason: .progress- )- }-- if var task = existingTask {- task.resumeExecution(from: event)- task.mergeMetadata(from: event)- task.recordProgress(at: event.timestamp)- task.startedAt = task.startedAt ?? event.context?.turnStartedAt- tasks[key] = task- return- }-- tasks[key] = ActivityTask(- displayID: displayID,- key: key,- event: event,- state: .running,- startedAt: event.context?.turnStartedAt,- progressGeneration: 1- )- }-- private func resumeTask(- from event: ActivityRecord,- allowsRecovery: Bool,- source: ActivityEventSource- ) {- guard let eventKey = ActivityTaskKey(event: event) else { return }- let matchedKey = event.agentID == nil- ? eventKey- : matchingSubagentParentTaskKey(for: event)-- if let key = matchedKey, var task = tasks[key] {- guard recentEndedDate(for: key) == nil,- pendingTerminalTasks[key] == nil else {- return- }- guard task.acceptsExecutionEvent(event) else {- return- }-- let wasSuppressed = task.state == .suppressed- task.resumeExecution(from: event)- task.mergeMetadata(from: event)- task.recordProgress(at: event.timestamp)- tasks[key] = task- if wasSuppressed || source == .live {- clearProtection(- for: key,- taskID: task.displayID,- reason: .progress- )- }- return- }-- guard allowsRecovery,- recentEndedDate(for: eventKey) == nil,- pendingTerminalTasks[eventKey] == nil else {- return- }-- let recoveredTask = ActivityTask(- displayID: UUID(),- key: eventKey,- event: event,- state: .running,- startedAt: nil,- progressGeneration: 1- )- tasks[eventKey] = recoveredTask- if source == .live {- clearProtection(- for: eventKey,- taskID: recoveredTask.displayID,- reason: .progress- )- }- }-- private func updateSubagentActivity(- from event: ActivityRecord,- isStarting: Bool,- source: ActivityEventSource- ) {- guard let agentID = event.agentID, let rootThread = event.context?.rootThreadID, let rootTurn = event.context?.rootTurnID else { return }- let key = ActivityTaskKey(thread: rootThread, turn: rootTurn)- guard recentEndedDate(for: key) == nil, pendingTerminalTasks[key] == nil, var task = tasks[key] else { return }- if let startedAt = task.startedAt, event.timestamp < startedAt {- return- }- // 创建活动标识子线程, 不能拿发出通知的父轮次伪造子执行- let executions = task.executions.filter { $0.key.agentID == agentID }- task.recordSubagentActivity(- agentID: event.agentID, isStarting: isStarting,- hasEnded: !executions.isEmpty && executions.values.allSatisfy(\.isTerminal), at: event.timestamp- )- task.mergeMetadata(from: event)- task.recordProgress(at: event.timestamp)- let wasSuppressed = task.state == .suppressed- if wasSuppressed {- task.state = .running- task.stateChangedAt = event.timestamp- }- if wasSuppressed || source == .live {- clearProtection(for: key, taskID: task.displayID, reason: .progress)- }- tasks[key] = task- }-- private func waitForApproval(- from event: ActivityRecord,- source: ActivityEventSource- ) -> ActivityTaskKey? {- guard let eventKey = ActivityTaskKey(event: event) else { return nil }- let matchedKey = event.agentID == nil- ? eventKey- : matchingSubagentParentTaskKey(for: event)-- if let key = matchedKey, var task = tasks[key] {- guard recentEndedDate(for: key) == nil,- pendingTerminalTasks[key] == nil else {- return nil- }- guard task.acceptsExecutionEvent(event) else {- return nil- }-- let wasSuppressed = task.state == .suppressed- task.mergeMetadata(from: event)- task.recordProgress(at: event.timestamp)- let enteredWaiting = task.recordApprovalRequest(from: event)- tasks[key] = task- if wasSuppressed || source == .live {- clearProtection(- for: key,- taskID: task.displayID,- reason: .progress- )- }- return enteredWaiting ? key : nil- }-- guard event.agentID == nil,- recentEndedDate(for: eventKey) == nil,- pendingTerminalTasks[eventKey] == nil else {- return nil- }-- var task = ActivityTask(- displayID: UUID(),- key: eventKey,- event: event,- state: .running,- startedAt: nil,- progressGeneration: 1- )- let enteredWaiting = task.recordApprovalRequest(from: event)- tasks[eventKey] = task- if source == .live {- clearProtection(- for: eventKey,- taskID: task.displayID,- reason: .progress- )- }- return enteredWaiting ? eventKey : nil- }-- func discardStaleTerminalTask(for key: ActivityTaskKey) {- if let task = tasks.removeValue(forKey: key) {- clearProtection(- for: key,- taskID: task.displayID,- reason: .terminal- )- }- if let pending = pendingTerminalTasks.removeValue(forKey: key) {- clearProtection(- for: key,- taskID: pending.task.displayID,- reason: .terminal- )+ func isCurrentTransition(_ transition: ActivityTransition, now: Date = Date()) -> Bool {+ guard canPublishActivityTransitions, let turnTransitionNotBefore else { return false }+ let date: Date+ switch transition {+ case let .completed(completion): date = completion.completedAt+ case let .waitingApproval(snapshot):+ guard tasks.values.contains(where: { $0.displayID == snapshot.id && $0.state == .waitingApproval }) else { return false }+ date = snapshot.stateChangedAt}+ return date >= turnTransitionNotBefore && now.timeIntervalSince(date) <= 10}// MARK: - 快照与过期清理- func refreshSnapshot(now: Date) {+ func refreshSnapshot(now: Date, terminalEvents: [ActivityTerminalEvent] = []) {pruneExpiredState(now: now)guard isActivitySourceHealthy || !isStarted else { return }@@ -959,8 +461,7 @@ final class ActivityMonitor: ObservableObject {recentTerminations: recentTerminations)let retainedTerminalIDs = Set(completions.map(\.id)).union(terminations.map(\.id))- let events = pendingTerminalPresentationEvents.filter { retainedTerminalIDs.contains($0.id) }- pendingTerminalPresentationEvents.removeAll()+ let events = terminalEvents.filter { retainedTerminalIDs.contains($0.id) }publishSnapshot(newSnapshot, terminalEvents: events)scheduleNextCleanup(now: now)@@ -979,7 +480,7 @@ final class ActivityMonitor: ObservableObject {private func sortedTasks(in state: ActivityTaskState) -> [ActivityTask] {tasks.values- .filter { $0.state == state && !unavailableTurns.contains($0.key) }+ .filter { $0.state == state && suppressedTasks[$0.key] == nil && !unavailableTurns.contains($0.key) }.sorted(by: Self.recentFirst(\.lastActivityAt, \.displayID))}@@ -997,20 +498,8 @@ final class ActivityMonitor: ObservableObject {}private func pruneExpiredState(now: Date) {- finalizeExpiredPendingTerminalTasks(now: now)-removeExpiredProtectionRecords(now: now)-- let historyCutoff = now.addingTimeInterval(-Self.recentHistoryRetention)- completions.removeAll { $0.completedAt <= historyCutoff }- terminations.removeAll { $0.terminatedAt <= historyCutoff }- let retainedTerminalIDs = Set(completions.map(\.id)).union(terminations.map(\.id))- terminalTokenUsageRequests = terminalTokenUsageRequests.filter { retainedTerminalIDs.contains($0.key) }-- let endedTaskCutoff = now.addingTimeInterval(-Self.endedTaskRetention)- recentlyEndedTaskAt = recentlyEndedTaskAt.filter {- $0.value > endedTaskCutoff- }+ graph.prune(now: now)}private func scheduleNextCleanup(now: Date) {@@ -1052,5 +541,5 @@ final class ActivityMonitor: ObservableObject {private static let recentHistoryRetention: TimeInterval = 10 * 60static let endedTaskRetention: TimeInterval = 24 * 60 * 60static let protectionNotificationSubmissionGrace: Duration = .seconds(3)- private static let sessionLifecyclePollInterval: TimeInterval = 1+ private static let recoveryRetryInterval: TimeInterval = 1}diff --git a/CodexBar/Services/Activity/ActivityTask.swift b/CodexBar/Services/Activity/ActivityTask.swiftindex 6ac612c..3667b14 100644--- a/CodexBar/Services/Activity/ActivityTask.swift+++ b/CodexBar/Services/Activity/ActivityTask.swift@@ -1,7 +1,7 @@import CryptoKitimport Foundation-struct ActivityTaskKey: Hashable {+nonisolated struct ActivityTaskKey: Hashable {let threadID: Stringlet turnID: String@@ -10,7 +10,7 @@ struct ActivityTaskKey: Hashable {turnID = turn}- init?(event: ActivityRecord) {+ init?(event: ActivityDataRecord) {guard let threadID = event.threadID, !threadID.isEmpty,let turnID = event.turnID, !turnID.isEmpty else { return nil }self.init(thread: threadID, turn: turnID)@@ -26,12 +26,12 @@ struct ActivityTaskKey: Hashable {}}-enum ActivityEventSource {+nonisolated enum ActivityEventSource {case bootstrapcase live}-enum ProtectionClearReason {+nonisolated enum ProtectionClearReason {case progresscase thresholdChangecase terminal@@ -53,13 +53,12 @@ struct ProtectionAttempt {let timeoutTask: Task<Void, Never>}-enum ActivityTaskState: Equatable {+nonisolated enum ActivityTaskState: Equatable {case runningcase waitingApproval- case suppressed}-struct PendingTerminalTask {+nonisolated struct PendingTerminalTask {var task: ActivityTasklet supersededAt: Datevar expiresAt: Date {@@ -67,7 +66,7 @@ struct PendingTerminalTask {}}-struct ActivityTask {+nonisolated struct ActivityTask {let displayID: UUIDlet key: ActivityTaskKeyvar lifecycleCoverageCheckedAt: Date?@@ -81,14 +80,14 @@ struct ActivityTask {var lastProgressAt: Datevar progressGeneration: UInt64var executions: [ActivityExecutionKey: ActivityExecution] = [:]- var subagentsByID: [String: SubagentObservation]+ var subagentsByThreadID: [String: SubagentObservation]var hasCompleteSubagentCoverage = falsevar tokenUsage: TokenUsage?init(displayID: UUID,key: ActivityTaskKey,- event: ActivityRecord,+ event: ActivityDataRecord,state: ActivityTaskState,startedAt: Date?,progressGeneration: UInt64@@ -103,7 +102,7 @@ struct ActivityTask {stateChangedAt = event.timestamplastProgressAt = event.timestampself.progressGeneration = progressGeneration- subagentsByID = [:]+ subagentsByThreadID = [:]recordExecutionEvent(event)}@@ -136,7 +135,7 @@ struct ActivityTask {}guard !live.isEmpty else { return nil }let waiting = live.filter { $0.1.waiting != nil }.min { ($0.1.waiting?.since ?? .distantFuture) < ($1.1.waiting?.since ?? .distantFuture) }- let root = live.first { $0.0.agentID == nil }+ let root = live.first { $0.0.agentThreadID == nil }var result = (waiting ?? root ?? live.max { $0.1.updatedAt < $1.1.updatedAt })!.1result.recent = live.compactMap(\.1.recent).max { $0.at < $1.at }result.toolCount = live.allSatisfy { $0.1.toolCount != nil } ? live.reduce(0) { $0 + ($1.1.toolCount ?? 0) } : nil@@ -158,8 +157,8 @@ struct ActivityTask {lifecycleCoverageCheckedAt.map { now.timeIntervalSince($0) < 5 } == true}- mutating func recordLifecycleRead(_ state: SessionLifecycleState, at now: Date) {- lifecycleCoverageCheckedAt = state.readStatus == .complete ? now : nil+ mutating func recordLifecycleRead(_ state: TurnLifecycleState, at now: Date) {+ lifecycleCoverageCheckedAt = state.readStatus == .complete ? (state.verifiedAt ?? now) : nil}/// 阈值调整与隐藏共用计时起点, 恢复不要求读取结果仍在有效期内@@ -173,54 +172,55 @@ struct ActivityTask {return protectionReferenceAt.addingTimeInterval(inactivityDuration)}- mutating func mergeMetadata(from event: ActivityRecord) {- if event.agentID == nil {+ mutating func mergeMetadata(from event: ActivityDataRecord) {+ if event.agentThreadID == nil {projectName = event.projectDisplayName ?? projectNamemodelName = event.model ?? modelName}- _ = mergeEffort(event.effort)+ mergeEffort(event.effort)}- mutating func recordProgress(at timestamp: Date) {- lastProgressAt = max(lastProgressAt, timestamp)+ @discardableResult+ mutating func recordProgress(at timestamp: Date) -> Bool {+ guard timestamp > lastProgressAt else { return false }+ lastProgressAt = timestampprogressGeneration &+= 1+ return true}- @discardableResult- mutating func mergeEffort(_ incomingEffort: String?) -> Bool {+ mutating func mergeEffort(_ incomingEffort: String?) {guard let incomingEffort = Self.normalizedEffort(incomingEffort) else {- return false+ return}guard let effort else {effort = incomingEffort- return true+ return}guard effort != incomingEffort, effort != "mixed" else {- return false+ return}self.effort = "mixed"- return true}mutating func recordSubagentActivity(- agentID: String?,+ agentThreadID: String?,isStarting: Bool,hasEnded: Bool = false,at timestamp: Date) {- guard let agentID else {+ guard let agentThreadID else {hasCompleteSubagentCoverage = falsereturn}- let previous = subagentsByID[agentID]+ let previous = subagentsByThreadID[agentThreadID]if let previous, timestamp < previous.timestamp {return}if !isStarting, previous == nil {hasCompleteSubagentCoverage = false}- subagentsByID[agentID] = SubagentObservation(+ subagentsByThreadID[agentThreadID] = SubagentObservation(isRunning: isStarting && !hasEnded,timestamp: timestamp)@@ -230,7 +230,7 @@ struct ActivityTask {guard hasCompleteSubagentCoverage else {return nil}- return subagentsByID.values.reduce(into: 0) { count, observation in+ return subagentsByThreadID.values.reduce(into: 0) { count, observation inif observation.isRunning {count += 1}@@ -254,37 +254,37 @@ struct ActivityTask {}}- func executionKey(for event: ActivityRecord) -> ActivityExecutionKey {+ func executionKey(for event: ActivityDataRecord) -> ActivityExecutionKey {ActivityExecutionKey(- agentID: event.agentID,+ agentThreadID: event.agentThreadID,turnID: event.turnID,- isUnattributed: event.agentID == nil && event.origin != .main+ isUnattributed: event.agentThreadID == nil && event.origin != .main)}var lastMainEventAt: Date {- executions.filter { $0.key.agentID == nil && !$0.key.isUnattributed }+ executions.filter { $0.key.agentThreadID == nil && !$0.key.isUnattributed }.values.map(\.lastEventAt).max() ?? startedAt ?? .distantPast}- func acceptsExecutionEvent(_ event: ActivityRecord) -> Bool {+ func acceptsExecutionEvent(_ event: ActivityDataRecord) -> Bool {let execution = executions[executionKey(for: event)]return execution?.isTerminal != true && event.timestamp >= (execution?.lastEventAt ?? .distantPast)}- mutating func recordExecutionEvent(_ event: ActivityRecord) {+ mutating func recordExecutionEvent(_ event: ActivityDataRecord) {let owner = executionKey(for: event)var execution = executions[owner] ?? ActivityExecution()execution.lastEventAt = max(execution.lastEventAt, event.timestamp)executions[owner] = execution}- mutating func resumeExecution(from event: ActivityRecord) {+ mutating func resumeExecution(from event: ActivityDataRecord) {recordExecutionEvent(event)refreshApprovalState(at: event.timestamp, restoresRunning: true)}- mutating func recordApprovalRequest(from event: ActivityRecord) -> Bool {+ mutating func recordApprovalRequest(from event: ActivityDataRecord) -> Bool {let wasWaiting = state == .waitingApprovallet owner = executionKey(for: event)recordExecutionEvent(event)@@ -298,7 +298,7 @@ struct ActivityTask {return !wasWaiting && state == .waitingApproval}- mutating func mergeExecutionLifecycle(_ lifecycle: SessionLifecycleState, owner: ActivityExecutionKey) {+ mutating func mergeExecutionLifecycle(_ lifecycle: TurnLifecycleState, owner: ActivityExecutionKey) {guard lifecycle.readStatus == .complete else { return }var execution = executions[owner] ?? ActivityExecution()execution.presentation = lifecycle.presentation@@ -351,8 +351,8 @@ struct ActivityTask {}}-struct ActivityExecutionKey: Hashable {- let agentID: String?+nonisolated struct ActivityExecutionKey: Hashable {+ let agentThreadID: String?let turnID: String?var isUnattributed = false}@@ -369,14 +369,14 @@ nonisolated struct ActivityApproval {}}-struct ActivityExecution {+nonisolated struct ActivityExecution {var lastEventAt: Date = .distantPastvar approval: ActivityApproval?var isTerminal = falsevar presentation: ActivityLivePresentation?}-struct SubagentObservation {+nonisolated struct SubagentObservation {let isRunning: Boollet timestamp: Date}diff --git a/CodexBar/Services/Activity/ActivityMonitor+Subagents.swift b/CodexBar/Services/Activity/ActivityTaskGraph+Subagents.swiftsimilarity index 66%rename from CodexBar/Services/Activity/ActivityMonitor+Subagents.swiftrename to CodexBar/Services/Activity/ActivityTaskGraph+Subagents.swiftindex c4a9481..20df039 100644--- a/CodexBar/Services/Activity/ActivityMonitor+Subagents.swift+++ b/CodexBar/Services/Activity/ActivityTaskGraph+Subagents.swift@@ -1,26 +1,26 @@import Foundation-struct PendingSubagentEvent {- let event: ActivityRecord+nonisolated struct PendingSubagentEvent {+ let event: ActivityDataRecordlet source: ActivityEventSource}-extension ActivityMonitor {+nonisolated extension ActivityTaskGraph {/// 生命周期和工具事件 都携带子轮次, 已结束或未确认的归属不回退到当前根任务func matchingSubagentParentTaskKey(- for event: ActivityRecord+ for event: ActivityDataRecord) -> ActivityTaskKey? {guard let reference = subagentReference(for: event),let rootKey = subagentTurnLinks[reference]?.root, rootKey.threadID == event.threadID else { return nil }return tasks[rootKey] == nil ? nil : rootKey}- func subagentReference(for event: ActivityRecord) -> ActivityTurnReference? {- guard let agentID = event.agentID, let turnID = event.turnID else { return nil }- return ActivityTurnReference(threadID: agentID, turnID: turnID)+ func subagentReference(for event: ActivityDataRecord) -> ActivityTurnReference? {+ guard let agentThreadID = event.agentThreadID, let turnID = event.turnID else { return nil }+ return ActivityTurnReference(threadID: agentThreadID, turnID: turnID)}- func deferUnassociatedSubagentEvent(_ event: ActivityRecord, source: ActivityEventSource) -> Bool {+ mutating func deferUnassociatedSubagentEvent(_ event: ActivityDataRecord, source: ActivityEventSource) -> Bool {guard let reference = subagentReference(for: event), subagentTurnLinks[reference] == nil else { return false }guard event.threadID != nil else { return true }if !pendingSubagentEvents.contains(where: { $0.event == event }) {@@ -29,7 +29,7 @@ extension ActivityMonitor {return true}- func subagentLifecycleReferences() -> [ActivityTurnReference] {+ mutating func subagentLifecycleReferences() -> [ActivityTurnReference] {let cutoff = Date().addingTimeInterval(-ActivityRetention.window)pendingSubagentEvents.removeAll { $0.event.timestamp <= cutoff }// 结束后的关联也保留一个回放窗口, 用于拒绝旧 Agent 的迟到生命周期事件@@ -37,55 +37,44 @@ extension ActivityMonitor {var references = pendingSubagentEvents.compactMap { subagentReference(for: $0.event) }for task in tasks.values {for (owner, execution) in task.executions where !execution.isTerminal {- guard let agentID = owner.agentID, let turnID = owner.turnID else { continue }- references.append(ActivityTurnReference(threadID: agentID, turnID: turnID))+ guard let agentThreadID = owner.agentThreadID, let turnID = owner.turnID else { continue }+ references.append(ActivityTurnReference(threadID: agentThreadID, turnID: turnID))}}return Array(Set(references))}- func applySubagentLifecycle(- _ state: SessionLifecycleState,- terminalOnly: Bool- ) -> Bool {+ mutating func applySubagentLifecycle(_ state: TurnLifecycleState) {guard state.readStatus == .complete,let rootTurnID = state.rootTurnID,let rootThreadID = state.rootThreadID,- let parentThreadID = state.parentThreadID, parentThreadID != state.requestedThreadID else { return false }+ let parentThreadID = state.parentThreadID, parentThreadID != state.requestedThreadID else { return }let reference = ActivityTurnReference(threadID: state.requestedThreadID, turnID: state.turnID)let eventThreads = Set(pendingSubagentEvents.compactMap { pending -> String? insubagentReference(for: pending.event) == reference ? pending.event.threadID : nil})guard rootThreadID != state.requestedThreadID,- eventThreads.isEmpty || eventThreads == [rootThreadID] else { return false }+ eventThreads.isEmpty || eventThreads == [rootThreadID] else { return }let rootKey = ActivityTaskKey(thread: rootThreadID, turn: rootTurnID)- guard subagentTurnLinks[reference].map({ $0.root == rootKey }) ?? true else { return false }+ guard subagentTurnLinks[reference].map({ $0.root == rootKey }) ?? true else { return }subagentTurnLinks[reference] = (rootKey, state.startedAt ?? subagentTurnLinks[reference]?.retainedAt ?? Date())- guard !terminalOnly || state.terminal != nil,- var task = tasks[rootKey] else { return false }+ guard var task = tasks[rootKey] else { return }let key = rootKey- let owner = ActivityExecutionKey(agentID: state.requestedThreadID, turnID: state.turnID)- let wasWaiting = task.state == .waitingApproval+ let owner = ActivityExecutionKey(agentThreadID: state.requestedThreadID, turnID: state.turnID)task.mergeEffort(state.effort)- task.mergeExecutionLifecycle(state, owner: owner)- if !terminalOnly {- _ = mergeLifecycleProgress(from: state, key: key, into: &task)+ if state.terminal != nil {+ task.finishExecution(owner, at: state.lastProgressAt ?? task.lastProgressAt)}+ _ = mergeLifecycleProgress(from: state, key: key, into: &task)tasks[key] = task- if !terminalOnly, wasWaiting != (task.state == .waitingApproval) {- clearProtection(for: key, taskID: task.displayID, reason: .progress)- }- return true}- @discardableResult- func reconcileSubagentCounts(_ states: [SessionLifecycleState]) -> Bool {- var changed = false+ mutating func reconcileSubagentCounts(_ states: [TurnLifecycleState]) {for (key, var task) in tasks {let root = task.turnReference- let children = SessionLifecycleState.subagentStates(for: [root], in: states)+ let children = TurnLifecycleState.subagentStates(for: [root], in: states)let references = Set(children.map { ActivityTurnReference(threadID: $0.requestedThreadID, turnID: $0.turnID) })- let agentIDs = Set(children.map(\.requestedThreadID))+ let agentThreadIDs = Set(children.map(\.requestedThreadID))let hasRoot = states.contains { $0.requestedThreadID == root.threadID && $0.turnID == root.turnID && $0.readStatus == .complete }let hasChildren = children.allSatisfy {$0.readStatus == .complete && $0.rootThreadID == root.threadID && $0.rootTurnID == root.turnID@@ -93,29 +82,26 @@ extension ActivityMonitor {}let hasExecutions = task.executions.keys.allSatisfy { owner inguard !owner.isUnattributed else { return false }- guard let agent = owner.agentID else { return true }+ guard let agentThreadID = owner.agentThreadID else { return true }guard let turn = owner.turnID else { return false }- return references.contains(ActivityTurnReference(threadID: agent, turnID: turn))+ return references.contains(ActivityTurnReference(threadID: agentThreadID, turnID: turn))}let hasPendingEvents = pendingSubagentEvents.contains { $0.event.threadID == root.threadID }- let previousCount = task.snapshot.activeSubagentCounttask.hasCompleteSubagentCoverage = hasRoot && hasChildren && hasExecutions && !hasPendingEvents- && Set(task.subagentsByID.keys).isSubset(of: agentIDs)+ && Set(task.subagentsByThreadID.keys).isSubset(of: agentThreadIDs)if task.hasCompleteSubagentCoverage {- task.subagentsByID = Dictionary(grouping: children, by: \.requestedThreadID).mapValues { turns in+ task.subagentsByThreadID = Dictionary(grouping: children, by: \.requestedThreadID).mapValues { turns inSubagentObservation(isRunning: turns.contains { $0.terminal == nil },timestamp: turns.compactMap { $0.lastProgressAt ?? $0.startedAt }.max() ?? .distantPast)}}- changed = previousCount != task.snapshot.activeSubagentCount || changedtasks[key] = task}- return changed}- func replayAssociatedSubagentEvents(into transitions: inout [ActivityTransition]) -> Bool {+ mutating func replayAssociatedSubagentEvents(into transitions: inout [ActivityTransition]) -> [ActivityTaskKey] {var remaining: [PendingSubagentEvent] = []var ready: [PendingSubagentEvent] = []for pending in pendingSubagentEvents {@@ -134,7 +120,6 @@ extension ActivityMonitor {waitingKeys.append(key)}}- transitions.append(contentsOf: waitingApprovalTransitions(waitingKeys))- return !ready.isEmpty+ return waitingKeys}}diff --git a/CodexBar/Services/Activity/ActivityMonitor+Tokens.swift b/CodexBar/Services/Activity/ActivityTaskGraph+TokenUsage.swiftsimilarity index 60%rename from CodexBar/Services/Activity/ActivityMonitor+Tokens.swiftrename to CodexBar/Services/Activity/ActivityTaskGraph+TokenUsage.swiftindex dc372e9..254cdd2 100644--- a/CodexBar/Services/Activity/ActivityMonitor+Tokens.swift+++ b/CodexBar/Services/Activity/ActivityTaskGraph+TokenUsage.swift@@ -1,58 +1,58 @@import Foundation-struct TaskTokenRequest {+nonisolated struct TaskTokenUsageRequest {let root: ActivityTurnReference- let expectedAgentIDs: Set<String>+ let expectedAgentThreadIDs: Set<String>var references: Set<ActivityTurnReference>- func usage(from states: [SessionLifecycleState], requiresFinalUsage: Bool = true) -> TokenUsage? {- let agentIDs = Set(references.filter { $0 != root }.map(\.threadID))- guard !requiresFinalUsage || expectedAgentIDs.isSubset(of: agentIDs) else { return nil }+ func tokenUsage(from states: [TurnLifecycleState], requiresFinalTokenUsage: Bool = true) -> TokenUsage? {+ let agentThreadIDs = Set(references.filter { $0 != root }.map(\.threadID))+ guard !requiresFinalTokenUsage || expectedAgentThreadIDs.isSubset(of: agentThreadIDs) else { return nil }var total: TokenUsage?for reference in references {guard let state = states.first(where: { $0.requestedThreadID == reference.threadID && $0.turnID == reference.turnID }),state.readStatus == .complete,state.rootTurnID == root.turnID,(state.rootThreadID ?? state.requestedThreadID) == root.threadID,- !requiresFinalUsage || reference == root || state.terminal != nil,- let usage = state.tokenUsage else {- if requiresFinalUsage {+ !requiresFinalTokenUsage || reference == root || state.terminal != nil,+ let tokenUsage = state.tokenUsage else {+ if requiresFinalTokenUsage {return nil}continue}if let previous = total {- guard let sum = previous.adding(usage) else { return nil }+ guard let sum = previous.adding(tokenUsage) else { return nil }total = sum} else {- total = usage+ total = tokenUsage}}return total}}-extension ActivityMonitor {- func registerTerminalTokenUsage(+nonisolated extension ActivityTaskGraph {+ mutating func registerTerminalTokenUsage(id: UUID, task: ActivityTask) {- // 缺少子 Agent 身份时不能把主线程小计展示为整项任务总量+ // 缺少子 Agent 身份时不能把主会话小计展示为整项任务总量guard !task.executions.keys.contains(where: \.isUnattributed) else { return }terminalTokenUsageRequests[id] = tokenUsageRequest(for: task)}private func tokenUsageRequest(for task: ActivityTask- ) -> TaskTokenRequest {+ ) -> TaskTokenUsageRequest {let root = task.turnReferencevar references: Set = [root]references.formUnion(subagentTurnLinks.filter { $0.value.root == task.key }.map(\.key))for owner in task.executions.keys {- guard let agent = owner.agentID, let turn = owner.turnID else { continue }- references.insert(ActivityTurnReference(threadID: agent, turnID: turn))+ guard let agentThreadID = owner.agentThreadID, let turn = owner.turnID else { continue }+ references.insert(ActivityTurnReference(threadID: agentThreadID, turnID: turn))}- return TaskTokenRequest(- root: root, expectedAgentIDs: Set(task.subagentsByID.keys),+ return TaskTokenUsageRequest(+ root: root, expectedAgentThreadIDs: Set(task.subagentsByThreadID.keys),references: references)}@@ -65,23 +65,19 @@ extension ActivityMonitor {return Array(references)}- @discardableResult- func applyActiveTokenUsage(_ states: [SessionLifecycleState]) -> Bool {- var changed = false+ mutating func applyActiveTokenUsage(_ states: [TurnLifecycleState]) {for (key, var task) in tasks {let request = tokenUsageRequest(for: task)- // 运行中汇总已明确归属的累计用量, 尚未产生记录的线程不伪造零值- let usage = request.usage(from: states, requiresFinalUsage: false)- guard task.tokenUsage != usage else { continue }- task.tokenUsage = usage+ // 运行中汇总已明确归属的累计用量, 尚未产生记录的会话不伪造零值+ let tokenUsage = request.tokenUsage(from: states, requiresFinalTokenUsage: false)+ guard task.tokenUsage != tokenUsage else { continue }+ task.tokenUsage = tokenUsagetasks[key] = task- changed = true}- return changed}/// 完成卡片保留期间继续补齐用量, 轮次终态可能先于最后一条用量更新- func terminalTokenUsageReferences() -> [ActivityTurnReference] {+ mutating func terminalTokenUsageReferences() -> [ActivityTurnReference] {var references: Set<ActivityTurnReference> = []for (id, var request) in terminalTokenUsageRequests {let rootKey = ActivityTaskKey(thread: request.root.threadID, turn: request.root.turnID)@@ -92,25 +88,20 @@ extension ActivityMonitor {return Array(references)}- @discardableResult- func applyTerminalTokenUsage(_ states: [SessionLifecycleState]) -> Bool {- var changed = false+ mutating func applyTerminalTokenUsage(_ states: [TurnLifecycleState]) {for (id, var request) in terminalTokenUsageRequests {guard states.contains(where: { $0.requestedThreadID == request.root.threadID && $0.turnID == request.root.turnID }) else { continue }let rootKey = ActivityTaskKey(thread: request.root.threadID, turn: request.root.turnID)request.references.formUnion(subagentTurnLinks.filter { $0.value.root == rootKey }.map(\.key))terminalTokenUsageRequests[id] = request- // 仅汇总已确认属于本轮的线程, 临时读取不完整时保留上次结果- guard let usage = request.usage(from: states) else { continue }- if let index = completions.firstIndex(where: { $0.id == id }), completions[index].tokenUsage != usage {- completions[index].tokenUsage = usage- changed = true+ // 仅汇总已确认属于本轮的会话, 临时读取不完整时保留上次结果+ guard let tokenUsage = request.tokenUsage(from: states) else { continue }+ if let index = completions.firstIndex(where: { $0.id == id }), completions[index].tokenUsage != tokenUsage {+ completions[index].tokenUsage = tokenUsage}- if let index = terminations.firstIndex(where: { $0.id == id }), terminations[index].tokenUsage != usage {- terminations[index].tokenUsage = usage- changed = true+ if let index = terminations.firstIndex(where: { $0.id == id }), terminations[index].tokenUsage != tokenUsage {+ terminations[index].tokenUsage = tokenUsage}}- return changed}}diff --git a/CodexBar/Services/Activity/ActivityTaskGraph.swift b/CodexBar/Services/Activity/ActivityTaskGraph.swiftnew file mode 100644index 0000000..fcd7e13--- /dev/null+++ b/CodexBar/Services/Activity/ActivityTaskGraph.swift@@ -0,0 +1,345 @@+import Foundation++/// reader 独占任务事实, 发布值快照后 UI 不再消费原始事件或补查另一份缓存+nonisolated struct ActivityTaskGraph {+ var tasks: [ActivityTaskKey: ActivityTask] = [:]+ var pendingTerminalTasks: [ActivityTaskKey: PendingTerminalTask] = [:]+ var subagentTurnLinks: [ActivityTurnReference: (root: ActivityTaskKey, retainedAt: Date)] = [:]+ var pendingSubagentEvents: [PendingSubagentEvent] = []+ var completions: [ActivityCompletion] = []+ var terminations: [ActivityTermination] = []+ var recentlyEndedTaskAt: [ActivityTaskKey: Date] = [:]+ var terminalTokenUsageRequests: [UUID: TaskTokenUsageRequest] = [:]+ var unavailableTurns = Set<ActivityTaskKey>()+ var backfilledTasks = Set<ActivityTaskKey>()+ var terminalEvents: [ActivityTerminalEvent] = []++ mutating func update(+ events: [ActivityDataRecord], source: ActivityEventSource, lifecycle: TurnLifecycleSnapshot+ ) -> [ActivityTransition] {+ let previouslyWaiting = Set(tasks.values.filter { $0.state == .waitingApproval }.map(\.displayID))+ var transitions = consume(events, source: source)+ transitions += reconcile(lifecycle.lifecycleStates(for: lifecycleReferences()))+ for (key, task) in tasks where task.state == .waitingApproval && !previouslyWaiting.contains(task.displayID) {+ if let approval = task.displayedApproval {+ tasks[key]?.stateChangedAt = approval.requestedAt+ }+ }+ let waiting = Dictionary(uniqueKeysWithValues: tasks.values.filter { $0.state == .waitingApproval }.map { ($0.displayID, $0.snapshot) })+ var notified = Set<UUID>()+ // 事件中的等待只是候选, 提醒必须使用本次核对后的最终状态+ return transitions.compactMap { transition in+ guard case let .waitingApproval(snapshot) = transition else { return transition }+ guard !previouslyWaiting.contains(snapshot.id), let current = waiting[snapshot.id],+ notified.insert(snapshot.id).inserted else { return nil }+ return .waitingApproval(current)+ }+ }++ mutating func consume(_ events: [ActivityDataRecord], source: ActivityEventSource) -> [ActivityTransition] {+ var transitions: [ActivityTransition] = []+ var waiting: [ActivityTaskKey] = []+ for event in events {+ if let key = apply(event, source: source, into: &transitions) {+ waiting.append(key)+ }+ }+ if source == .live {+ transitions += waitingApprovalTransitions(waiting)+ }+ return transitions+ }++ mutating func reconcile(_ states: [TurnLifecycleState]) -> [ActivityTransition] {+ unavailableTurns = Set(states.filter { $0.readStatus != .complete }.map { ActivityTaskKey(thread: $0.requestedThreadID, turn: $0.turnID) })+ var transitions: [ActivityTransition] = []+ for state in states {+ applySubagentLifecycle(state)+ applyLifecycleState(state, into: &transitions)+ }+ let waiting = replayAssociatedSubagentEvents(into: &transitions)+ mergeExecutionLifecycles(states)+ transitions += waitingApprovalTransitions(waiting)+ reconcileSubagentCounts(states)+ applyActiveTokenUsage(states)+ applyTerminalTokenUsage(states)+ prune(now: Date())+ return transitions+ }++ mutating func lifecycleReferences() -> [ActivityTurnReference] {+ Array(Set(activeTokenUsageReferences() + subagentLifecycleReferences() + terminalTokenUsageReferences()+ + pendingTerminalTasks.values.map(\.task.turnReference)))+ }++ mutating func prune(now: Date) {+ pendingTerminalTasks = pendingTerminalTasks.filter { $0.value.expiresAt > now }+ let cutoff = now.addingTimeInterval(-600)+ completions.removeAll { $0.completedAt <= cutoff }+ terminations.removeAll { $0.terminatedAt <= cutoff }+ let retained = Set(completions.map(\.id)).union(terminations.map(\.id))+ terminalTokenUsageRequests = terminalTokenUsageRequests.filter { retained.contains($0.key) }+ recentlyEndedTaskAt = recentlyEndedTaskAt.filter { $0.value > now.addingTimeInterval(-86400) }+ let childCutoff = now.addingTimeInterval(-ActivityRetention.window)+ pendingSubagentEvents.removeAll { $0.event.timestamp <= childCutoff }+ subagentTurnLinks = subagentTurnLinks.filter { $0.value.retainedAt > childCutoff }+ }++ mutating func apply(_ event: ActivityDataRecord, source: ActivityEventSource, into transitions: inout [ActivityTransition]) -> ActivityTaskKey? {+ guard event.origin == .main || event.origin == .auxiliary else { return nil }+ if deferUnassociatedSubagentEvent(event, source: source) {+ return nil+ }+ switch event.eventKind {+ case .turnStarted:+ if event.agentThreadID == nil {+ startTask(from: event)+ }+ case .toolStarted, .toolCompleted, .compactionStarted, .compactionCompleted, .approvalRequested:+ return resumeTask(from: event)+ case .subagentStarted, .subagentEnded:+ updateSubagentActivity(from: event)+ case .turnCompleted, .turnAborted:+ if event.agentThreadID == nil {+ finishTask(from: event, source: source, into: &transitions)+ }+ case .none: break+ }+ return nil+ }++ private mutating func startTask(from event: ActivityDataRecord) {+ guard let key = ActivityTaskKey(event: event), pendingTerminalTasks[key] == nil,+ recentEndedDate(for: key) == nil else { return }+ let existing = tasks[key]+ if let existing, event.timestamp < existing.lastMainEventAt {+ return+ }+ for task in tasks.values where task.key != key && task.key.threadID == key.threadID {+ pendingTerminalTasks[task.key] = PendingTerminalTask(task: task, supersededAt: event.timestamp)+ }+ tasks = tasks.filter { $0.key == key || $0.key.threadID != key.threadID }+ if var task = existing {+ task.resumeExecution(from: event)+ task.mergeMetadata(from: event)+ task.recordProgress(at: event.timestamp)+ task.startedAt = task.startedAt ?? event.context?.turnStartedAt+ tasks[key] = task+ } else {+ tasks[key] = ActivityTask(displayID: UUID(), key: key, event: event, state: .running, startedAt: event.context?.turnStartedAt, progressGeneration: 1)+ }+ }++ private mutating func resumeTask(from event: ActivityDataRecord) -> ActivityTaskKey? {+ guard let eventKey = ActivityTaskKey(event: event) else { return nil }+ let matchedKey = event.agentThreadID == nil ? eventKey : matchingSubagentParentTaskKey(for: event)+ guard let key = matchedKey, recentEndedDate(for: key) == nil, pendingTerminalTasks[key] == nil else { return nil }+ guard tasks[key] != nil || event.agentThreadID == nil else { return nil }+ var task = tasks[key] ?? ActivityTask(displayID: UUID(), key: key, event: event, state: .running, startedAt: nil, progressGeneration: 0)+ guard task.acceptsExecutionEvent(event) else { return nil }+ task.mergeMetadata(from: event)+ task.recordProgress(at: event.timestamp)+ let waiting: Bool+ if event.eventKind == .approvalRequested {+ waiting = task.recordApprovalRequest(from: event)+ } else {+ task.resumeExecution(from: event)+ waiting = false+ }+ tasks[key] = task+ return waiting ? key : nil+ }++ private mutating func updateSubagentActivity(from event: ActivityDataRecord) {+ guard let agentThreadID = event.agentThreadID, let rootThread = event.context?.rootThreadID, let rootTurn = event.context?.rootTurnID else { return }+ let key = ActivityTaskKey(thread: rootThread, turn: rootTurn)+ guard recentEndedDate(for: key) == nil, pendingTerminalTasks[key] == nil, var task = tasks[key],+ task.startedAt.map({ event.timestamp >= $0 }) ?? true else { return }+ let executions = task.executions.filter { $0.key.agentThreadID == agentThreadID }+ task.recordSubagentActivity(+ agentThreadID: agentThreadID,+ isStarting: event.eventKind == .subagentStarted,+ hasEnded: !executions.isEmpty && executions.values.allSatisfy(\.isTerminal),+ at: event.timestamp+ )+ task.mergeMetadata(from: event)+ task.recordProgress(at: event.timestamp)+ tasks[key] = task+ }++ private mutating func applyLifecycleState(_ state: TurnLifecycleState, into transitions: inout [ActivityTransition]) {+ let key = ActivityTaskKey(thread: state.requestedThreadID, turn: state.turnID)+ if var pending = pendingTerminalTasks[key] {+ Self.mergeLifecycleBackfill(from: state, into: &pending.task)+ if state.readStatus == .complete, let terminal = state.terminal {+ pendingTerminalTasks.removeValue(forKey: key)+ resolveTerminal(+ terminal,+ task: pending.task,+ key: key,+ abortFallback: state.terminalObservedAt ?? state.contextObservedAt ?? pending.supersededAt,+ publishesEvents: !state.isHistoricalTerminal,+ into: &transitions+ )+ } else {+ pendingTerminalTasks[key] = pending+ }+ return+ }+ guard var task = tasks[key] else { return }+ let hadCoverage = task.lifecycleCoverageCheckedAt != nil+ task.recordLifecycleRead(state, at: Date())+ Self.mergeLifecycleBackfill(from: state, into: &task)+ let progressed = mergeLifecycleProgress(from: state, key: key, into: &task)+ if state.readStatus == .complete, let terminal = state.terminal {+ tasks.removeValue(forKey: key)+ resolveTerminal(+ terminal,+ task: task,+ key: key,+ abortFallback: state.terminalObservedAt ?? state.contextObservedAt ?? Date(),+ publishesEvents: !state.isHistoricalTerminal,+ into: &transitions+ )+ return+ }+ tasks[key] = task+ if progressed || (!hadCoverage && task.lifecycleCoverageCheckedAt != nil) {+ backfilledTasks.insert(key)+ }+ }++ func mergeLifecycleProgress(from state: TurnLifecycleState, key _: ActivityTaskKey, into task: inout ActivityTask) -> Bool {+ guard state.readStatus == .complete, let progress = state.lastProgressAt else { return false }+ return task.recordProgress(at: progress)+ }++ private mutating func mergeExecutionLifecycles(_ states: [TurnLifecycleState]) {+ // 暂存事件补齐后再应用审批状态, 避免已处理的请求恢复等待+ for state in states where state.readStatus == .complete {+ let reference = ActivityTurnReference(threadID: state.requestedThreadID, turnID: state.turnID)+ let key: ActivityTaskKey+ let owner: ActivityExecutionKey+ if state.parentThreadID != nil {+ guard let root = subagentTurnLinks[reference]?.root,+ root.threadID == state.rootThreadID, root.turnID == state.rootTurnID else { continue }+ key = root+ owner = ActivityExecutionKey(agentThreadID: state.requestedThreadID, turnID: state.turnID)+ } else {+ key = ActivityTaskKey(thread: state.requestedThreadID, turn: state.turnID)+ owner = ActivityExecutionKey(agentThreadID: nil, turnID: state.turnID)+ }+ tasks[key]?.mergeExecutionLifecycle(state, owner: owner)+ }+ }++ static func backfilledStartedAt(for task: ActivityTask, state: TurnLifecycleState) -> Date? {+ guard task.startedAt == nil, let start = state.startedAt, start <= task.lastActivityAt.addingTimeInterval(1) else { return nil }+ return start+ }++ static func mergeLifecycleBackfill(from state: TurnLifecycleState, into task: inout ActivityTask) {+ if state.readStatus == .complete, let status = state.turnStatus {+ task.terminalFailed = status == .failed+ }+ if let start = backfilledStartedAt(for: task, state: state) {+ task.startedAt = start+ }+ task.mergeEffort(state.effort)+ }++ mutating func finishTask(from event: ActivityDataRecord, source: ActivityEventSource, into transitions: inout [ActivityTransition]) {+ guard let key = ActivityTaskKey(event: event) else { return }+ guard recentEndedDate(for: key) == nil else {+ tasks.removeValue(forKey: key)+ pendingTerminalTasks.removeValue(forKey: key)+ return+ }+ var task = pendingTerminalTasks[key]?.task ?? tasks[key] ?? ActivityTask(+ displayID: UUID(), key: key, event: event, state: .running, startedAt: event.context?.turnStartedAt, progressGeneration: 0+ )+ let tolerance: TimeInterval = event.context?.turnCompletedAt == nil ? 0 : 1+ guard event.timestamp.addingTimeInterval(tolerance) >= task.lastMainEventAt else { return }+ task.mergeMetadata(from: event)+ if task.startedAt == nil, let start = event.context?.turnStartedAt, start <= event.timestamp {+ task.startedAt = start+ }+ tasks.removeValue(forKey: key)+ pendingTerminalTasks.removeValue(forKey: key)+ let at = event.context == nil ? event.timestamp : event.context?.turnCompletedAt+ task.terminalFailed = task.terminalFailed || event.context?.turnStatus == .failed+ let terminal: TurnTerminalState = event.eventKind == .turnCompleted+ ? .completed(at: at, duration: event.context?.duration) : .aborted(at: at, duration: event.context?.duration)+ resolveTerminal(terminal, task: task, key: key, abortFallback: event.timestamp, publishesEvents: source == .live, into: &transitions)+ recordEndedTask(key, at: event.timestamp)+ }++ mutating func resolveTerminal(+ _ terminal: TurnTerminalState,+ task: ActivityTask,+ key: ActivityTaskKey,+ abortFallback: Date,+ publishesEvents: Bool = true,+ into transitions: inout [ActivityTransition]+ ) {+ guard recentEndedDate(for: key) == nil else { return }+ switch terminal {+ case let .completed(at, duration):+ let completedAt = max(at ?? abortFallback, task.lastActivityAt)+ let completion = ActivityCompletion(+ id: UUID(),+ taskID: task.displayID,+ projectName: task.projectName,+ modelName: task.modelName,+ effort: task.effort,+ completedAt: completedAt,+ duration: duration ?? at.flatMap { task.preciseDuration(until: $0) }+ )+ completions.append(completion)+ registerTerminalTokenUsage(id: completion.id, task: task)+ recordEndedTask(key, at: completedAt)+ if publishesEvents {+ terminalEvents.append(.completed(completion))+ transitions.append(.completed(completion))+ }+ case let .aborted(at, duration):+ let terminatedAt = max(at ?? abortFallback, task.lastActivityAt)+ let termination = ActivityTermination(+ id: UUID(),+ taskID: task.displayID,+ projectName: task.projectName,+ modelName: task.modelName,+ effort: task.effort,+ terminatedAt: terminatedAt,+ duration: duration ?? at.flatMap { task.preciseDuration(until: $0) },+ isFailure: task.terminalFailed+ )+ terminations.append(termination)+ registerTerminalTokenUsage(id: termination.id, task: task)+ recordEndedTask(key, at: terminatedAt)+ if publishesEvents {+ terminalEvents.append(.terminated(termination))+ }+ }+ }++ mutating func recentEndedDate(for key: ActivityTaskKey, now: Date = Date()) -> Date? {+ guard let date = recentlyEndedTaskAt[key] else { return nil }+ guard date > now.addingTimeInterval(-86400) else { recentlyEndedTaskAt.removeValue(forKey: key)+ return nil+ }+ return date+ }++ mutating func recordEndedTask(_ key: ActivityTaskKey, at date: Date) {+ recentlyEndedTaskAt[key] = max(recentlyEndedTaskAt[key] ?? .distantPast, date)+ }++ func waitingApprovalTransitions(_ keys: [ActivityTaskKey]) -> [ActivityTransition] {+ var seen = Set<ActivityTaskKey>()+ return keys.reversed().compactMap { key in+ guard seen.insert(key).inserted, let task = tasks[key], task.state == .waitingApproval else { return nil }+ return .waitingApproval(task.snapshot)+ }.reversed()+ }+}diff --git a/CodexBar/Services/Activity/AppServerActivityBackfill.swift b/CodexBar/Services/Activity/AppServerActivityBackfill.swiftnew file mode 100644index 0000000..408f8d3--- /dev/null+++ b/CodexBar/Services/Activity/AppServerActivityBackfill.swift@@ -0,0 +1,61 @@+import Foundation++/// 历史执行项的接收与解析独立执行, 不能占住实时采集 actor+actor AppServerActivityBackfill {+ struct Page: Sendable {+ let items: [ActivityItemEntry]+ let nextCursor: String?+ let limit: Int+ }++ private let socketURL: URL+ private let logStorage: AppServerLogStore?+ private let maximumMessageSize: Int++ init(socketURL: URL, logStorage: AppServerLogStore?, maximumMessageSize: Int = AppServerSession.defaultMaximumMessageSize) {+ self.socketURL = socketURL+ self.logStorage = logStorage+ self.maximumMessageSize = maximumMessageSize+ }++ func readPage(reference: ActivityTurnReference, cursor: String?, limit: Int, timeout: TimeInterval) throws -> Page {+ let budget = AppServerRequestBudget(deadline: Date().addingTimeInterval(timeout))+ return try AppServerRequestBudget.$current.withValue(budget) {+ var connection: AppServerSession?+ defer { connection?.close() }+ func read(limit: Int) throws -> ActivityItemsPage {+ try budget.check()+ if connection == nil {+ let opened = try AppServerSession(+ socketURL: socketURL, retainsNotifications: false, logStorage: logStorage,+ connectionName: "activity/backfill", maximumMessageSize: maximumMessageSize+ )+ connection = opened+ try opened.initialize(clientName: "codex_bar_activity_backfill", minimumVersion: CodexVersionReader.minimumAppServerVersion)+ }+ let request = ActivityRequests.items(reference, limit: limit, cursor: cursor)+ let page: ActivityItemsPage = try connection!.request(request.method, params: request.params)+ try budget.check()+ guard page.nextCursor == nil || page.nextCursor != cursor else { throw CodexStatusError.invalidServerResponse }+ return page+ }+ var pageLimit = limit+ while true {+ do {+ let page = try read(limit: pageLimit)+ guard page.data.allSatisfy({ $0.turnId == reference.turnID }) else { throw CodexStatusError.invalidServerResponse }+ return Page(items: page.data, nextCursor: page.nextCursor, limit: pageLimit)+ } catch let error as CodexStatusError {+ guard case .messageTooLarge = error else { throw error }+ connection?.close()+ connection = nil+ if pageLimit > 1 {+ pageLimit = max(1, pageLimit / 2)+ continue+ }+ throw error+ }+ }+ }+ }+}diff --git a/CodexBar/Services/Activity/AppServerActivityModels.swift b/CodexBar/Services/Activity/AppServerActivityModels.swiftindex 0f3150f..abadb1e 100644--- a/CodexBar/Services/Activity/AppServerActivityModels.swift+++ b/CodexBar/Services/Activity/AppServerActivityModels.swift@@ -10,12 +10,12 @@ private nonisolated struct WireActivityThread: Decodable {var parentThreadId: String?var createdAt: Double?- var origin: ActivityOrigin {+ var origin: ActivityDataOrigin {source?.origin ?? .unknown}- var parentID: String? {- parentThreadId ?? source?.parentID+ var parentThreadID: String? {+ parentThreadId ?? source?.parentThreadID}}@@ -28,21 +28,21 @@ private nonisolated struct WireActivityThreadStatus: Decodable {}private nonisolated struct WireActivityThreadSource: Decodable {- let origin: ActivityOrigin- let parentID: String?+ let origin: ActivityDataOrigin+ let parentThreadID: String?init(from decoder: Decoder) throws {let value = try ActivityJSONValue(from: decoder)if let name = value.string {origin = ["cli", "vscode", "exec", "appServer"].contains(name) ? .main : .unknown- parentID = nil+ parentThreadID = nil} else if let subagent = value["subAgent"] ?? value["subagent"] {let other = subagent["other"]?.stringorigin = other == "guardian" ? .autoReview : .auxiliary- parentID = (subagent["thread_spawn"] ?? subagent["threadSpawn"])?["parent_thread_id"]?.string+ parentThreadID = (subagent["thread_spawn"] ?? subagent["threadSpawn"])?["parent_thread_id"]?.string} else {origin = value["custom"] == nil ? .unknown : .main- parentID = nil+ parentThreadID = nil}}}@@ -139,7 +139,7 @@ private nonisolated struct WireActivityItem: Decodable {struct AgentState: Decodable { var status: String? }}-private nonisolated struct WireActivityTokenBreakdown: Decodable {+private nonisolated struct WireTokenUsageBreakdown: Decodable {let inputTokens: Int64let cachedInputTokens: Int64var cacheWriteInputTokens: Int64?@@ -147,7 +147,7 @@ private nonisolated struct WireActivityTokenBreakdown: Decodable {let reasoningOutputTokens: Int64let totalTokens: Int64- var usage: TokenUsage {+ var tokenUsage: TokenUsage {TokenUsage(inputTokens: inputTokens, cachedInputTokens: cachedInputTokens, cacheWriteInputTokens: cacheWriteInputTokens ?? 0,outputTokens: outputTokens, reasoningOutputTokens: reasoningOutputTokens, totalTokens: totalTokens@@ -155,9 +155,10 @@ private nonisolated struct WireActivityTokenBreakdown: Decodable {}}-private nonisolated struct WireActivityTokenUpdate: Decodable {- let total: WireActivityTokenBreakdown- let last: WireActivityTokenBreakdown+private nonisolated struct WireTokenUsageUpdate: Decodable {+ let total: WireTokenUsageBreakdown+ let last: WireTokenUsageBreakdown+ var modelContextWindow: Int64?}private nonisolated struct WireActivityInput: Decodable {@@ -173,7 +174,7 @@ private nonisolated struct WireActivityInput: Decodable {var status: WireActivityThreadStatus?var startedAtMs: Double?var completedAtMs: Double?- var tokenUsage: WireActivityTokenUpdate?+ var tokenUsage: WireTokenUsageUpdate?var commandActions: [WireActivityItem.CommandAction]?}@@ -191,7 +192,7 @@ private nonisolated struct WireActivityInput: Decodable {provenanceMethod = try container.decode(String.self, forKey: .method)kind = AppServerActivityProtocol.kind(for: provenanceMethod)live = try container.decode(WireActivityLivePayload.self, forKey: .params)- // 展示通知有不同的 status 类型, 不交给线程生命周期解码器解释+ // 展示通知有不同的 status 类型, 不交给会话生命周期解码器解释if kind.category == .presentation {params = Params(threadId: live.threadId, turnId: live.turnId)} else {@@ -205,6 +206,10 @@ private nonisolated struct WireActivityInput: Decodable {}default: break}+ if kind == .usageUpdated,+ params.tokenUsage == nil || params.threadId?.isEmpty != false {+ throw DecodingError.dataCorruptedError(forKey: .params, in: container, debugDescription: "Missing token tokenUsage or thread identity")+ }if [.commandApprovalRequested, .fileApprovalRequested, .permissionsRequested].contains(kind),params.itemId == nil || params.startedAtMs == nil {throw DecodingError.dataCorruptedError(forKey: .params, in: container, debugDescription: "Missing approval identity")@@ -222,6 +227,32 @@ nonisolated struct ActivityTurnsPage: Decodable { let data: [ActivityTurn]var nextCursor: String?}+nonisolated struct ActivityItemsPage: Decodable {+ let data: [ActivityItemEntry]+ var nextCursor: String?+}++nonisolated struct ActivityItemEntry: Decodable {+ let turnId: String+ let item: ActivityItem+ var startedAtMs: Double?+ var completedAtMs: Double?++ var startedAt: Date? {+ startedAtMs.map { Date(timeIntervalSince1970: $0 / 1000) }+ }++ var completedAt: Date? {+ completedAtMs.map { Date(timeIntervalSince1970: $0 / 1000) }+ }+}++nonisolated extension ActivityItem: Decodable {+ init(from decoder: Decoder) throws {+ self = try WireActivityItem(from: decoder).normalized+ }+}+nonisolated struct ActivityThreadResume: Decodable {let thread: ActivityThread}@@ -266,17 +297,7 @@ nonisolated enum AppServerActivityProtocol {nonisolated extension ActivityThread: Decodable {init(from decoder: Decoder) throws {- let wire = try WireActivityThread(from: decoder)- self.init(- id: wire.id,- cwd: wire.cwd,- model: wire.model,- reasoningEffort: wire.reasoningEffort,- status: wire.status.map(Self.status),- origin: wire.origin,- parentID: wire.parentID,- createdAt: wire.createdAt.map(Date.init(timeIntervalSince1970:))- )+ self = try WireActivityThread(from: decoder).normalized}fileprivate static func status(_ value: WireActivityThreadStatus) -> ActivityThreadStatus {@@ -287,6 +308,21 @@ nonisolated extension ActivityThread: Decodable {}}+private nonisolated extension WireActivityThread {+ var normalized: ActivityThread {+ ActivityThread(+ id: id,+ cwd: cwd,+ model: model,+ reasoningEffort: reasoningEffort,+ status: status.map(ActivityThread.status),+ origin: origin,+ parentThreadID: parentThreadID,+ createdAt: createdAt.map(Date.init(timeIntervalSince1970:))+ )+ }+}+nonisolated extension ActivityTurn: Decodable {init(from decoder: Decoder) throws {self = try WireActivityTurn(from: decoder).normalized@@ -332,18 +368,6 @@ nonisolated extension ActivityInput: Decodable {init(from decoder: Decoder) throws {let wire = try WireActivityInput(from: decoder)let raw = wire.params- let thread = raw.thread.map { value in- ActivityThread(- id: value.id,- cwd: value.cwd,- model: value.model,- reasoningEffort: value.reasoningEffort,- status: value.status.map(ActivityThread.status),- origin: value.origin,- parentID: value.parentID,- createdAt: value.createdAt.map(Date.init(timeIntervalSince1970:))- )- }self.init(id: wire.id?.identifier,kind: wire.kind,@@ -354,13 +378,15 @@ nonisolated extension ActivityInput: Decodable {itemId: raw.itemId,approvalId: raw.approvalId,requestId: raw.requestId?.identifier,- thread: thread,+ thread: raw.thread?.normalized,turn: raw.turn?.normalized,item: raw.item?.normalized,status: raw.status.map(ActivityThread.status),startedAt: raw.startedAtMs.map { Date(timeIntervalSince1970: $0 / 1000) },completedAt: raw.completedAtMs.map { Date(timeIntervalSince1970: $0 / 1000) },- tokenUsage: raw.tokenUsage.map { ActivityTokenUpdate(total: $0.total.usage, last: $0.last.usage) },+ tokenUsage: raw.tokenUsage.map {+ TokenUsageUpdate(total: $0.total.tokenUsage, last: $0.last.tokenUsage, modelContextWindow: $0.modelContextWindow)+ },commandActions: raw.commandActions?.map { .init(type: $0.type) }),live: wire.live.normalizeddiff --git a/CodexBar/Services/Activity/AppServerActivityReader.swift b/CodexBar/Services/Activity/AppServerActivityReader.swiftindex 6eee93e..aa81014 100644--- a/CodexBar/Services/Activity/AppServerActivityReader.swift+++ b/CodexBar/Services/Activity/AppServerActivityReader.swift@@ -1,64 +1,117 @@import Foundation-nonisolated enum ActivityEventBatch {- case bootstrapStart- case snapshotEvents([ActivityRecord])- case bootstrapEnd- case live([ActivityRecord])- case sourceUnavailable- case lifecycleChanged+nonisolated struct ActivityUpdate {+ enum Phase {+ case bootstrapStart+ case snapshotEvents+ case bootstrapEnd+ case live+ case sourceUnavailable+ case lifecycleChanged+ }++ let readerID: UUID+ let revision: UInt64+ let phase: Phase+ let graph: ActivityTaskGraph+ let transitions: [ActivityTransition]+ let terminalEvents: [ActivityTerminalEvent]+ let backfilledTasks: Set<ActivityTaskKey>+ var isComplete = true}nonisolated enum ActivityEventDrainResult: Sendable {- case completed+ case completed(ActivityUpdate)+ case partial(ActivityUpdate)case sourceUnavailablecase cancelled++ enum Status { case completed, partial, sourceUnavailable, cancelled }++ var status: Status {+ switch self {+ case .completed: .completed+ case .partial: .partial+ case .sourceUnavailable: .sourceUnavailable+ case .cancelled: .cancelled+ }+ }}-/// actor 独占连接与解析状态, UI 仅消费归一化事件和有覆盖时限的生命周期快照+/// actor 独占连接与解析状态, UI 仅消费任务图和本次状态变化actor AppServerActivityReader {nonisolated static var defaultSocketURL: URL {CodexPaths.codexHomeDirectory().appendingPathComponent("app-server-control/app-server-control.sock")}- private let lifecycleCache: SessionLifecycleCache+ private var taskGraph = ActivityTaskGraph()+ private let readerID = UUID()+ private var revision: UInt64 = 0+ private var latestUpdate: ActivityUpdate?+ private var isReconciliationComplete = falseprivate let onAccountChange: @MainActor @Sendable (AccountChange) -> Voidprivate var pendingAccountChange: AccountChange?- private let onBatch: @MainActor @Sendable (ActivityEventBatch) -> Void+ private let onBatch: @MainActor @Sendable (ActivityUpdate) -> Voidprivate let socketURL: URLprivate let logStorage: AppServerLogStore?- private let tokenHistory: TokenHistoryStore- private let recorder: ActivityRecorder+ private let tokenUsageHistory: TokenUsageHistoryStore+ private let activityDataRecorder: ActivityDataRecorderprivate var session: AppServerSession?+ private var connectedAt = Date.distantFutureprivate var reducer = AppServerActivityReducer()private var subscribed = Set<String>()private var loop: Task<Void, Never>?private var storageTask: Task<Void, Never>?- private var tokenRefreshTask: Task<[TokenTurn], Error>?+ private var tokenUsageRefreshTask: Task<[TokenUsageAggregate], Error>?private var generation = 0- private var connectedAt = Date.distantFutureprivate var reconciledAt = Date.distantPast- private var lastTokenRefresh = Date.distantPast+ private var lastTokenUsageRefresh = Date.distantPastprivate var isRunning = falseprivate var isStopping = falseprivate var stopWaiters: [CheckedContinuation<Bool, Never>] = []private var stateChanged = falseprivate var lastPublished = Date.distantPast- private var isTokenWriter = false+ private var isTokenUsageWriter = falseprivate var writerRetryAfter = Date.distantPastprivate var drainWaiters: [CheckedContinuation<ActivityEventDrainResult, Never>] = []- private var pendingEvents: [(event: ActivityRecord, bytes: Int)] = []- private var pendingPublication: [ActivityRecord] = []- private var pendingObservations: [(observation: TokenObservation, bytes: Int)] = []+ private var pendingEvents: [(event: ActivityDataRecord, bytes: Int)] = []+ private var pendingPublication: [ActivityDataRecord] = []+ private var pendingSamples: [(sample: TokenUsageSample, bytes: Int)] = []private var pendingStorageBytes = 0private var deferredNotifications: [ActivityInput] = []private var verifiedTurns: [ActivityTurnReference: Date] = [:]- private var turnCursors: [String: String] = [:]+ private var turnScans: [String: TurnScan] = [:]+ private var turnRevisions: [ActivityTurnReference: Int] = [:]+ private let backfill: AppServerActivityBackfill+ private var backfillTask: Task<Void, Never>?+ private var backfillEpoch = UUID()+ private var backfillScans: [ActivityTurnReference: BackfillScan] = [:]+ private var backfillRoots = Set<ActivityTurnReference>()+ private var backfillThreads = Set<String>()+ private var liveStartedTurns = Set<ActivityTurnReference>()+ private var tokenUsageRestorations: [String: Task<Void, Never>] = [:]++ private struct TurnScan {+ var targets: Set<String>+ var cursor: String?+ var exhausted = false+ var visitedCursors = Set<String>()+ }++ private struct ReadCheckpoint {+ let started = Date()+ let threadRevision: Int+ let turns: [ActivityTurnReference: Int]+ }- private struct TurnReadResult {- var turns: [ActivityTurn]- var remaining: Set<String>+ private struct BackfillScan {+ var completed = falsevar cursor: String?+ var limit = 20+ var visitedCursors = Set<String>()+ var failures = 0+ var retryAfter = Date.distantPast+ var checkedAt = Date.distantPast}private var checkedThreads: [String: Date] = [:]@@ -68,31 +121,30 @@ actor AppServerActivityReader {private var storageRetryAfter = Date.distantPastprivate var storageFailed = falseprivate var isHistoryRecordingPaused = false- private var tokenRefreshFailed = false- private var tokenRetryAfter = Date.distantPast+ private var tokenUsageRefreshFailed = false+ private var tokenUsageRetryAfter = Date.distantPastprivate let maximumPendingEvents: Intprivate let maximumPendingBytes: Intprivate let maximumLoadedPages: Intprivate let reconciliationTimeout: TimeIntervalinit(- lifecycleCache: SessionLifecycleCache,socketURL: URL = AppServerActivityReader.defaultSocketURL,logStorage: AppServerLogStore? = .shared,- tokenHistory: TokenHistoryStore = TokenHistoryStore(),- recorder: ActivityRecorder = ActivityRecorder(),+ tokenUsageHistory: TokenUsageHistoryStore = TokenUsageHistoryStore(),+ activityDataRecorder: ActivityDataRecorder = ActivityDataRecorder(),maximumPendingEvents: Int = 4096,maximumPendingBytes: Int = 8 * 1024 * 1024,maximumLoadedPages: Int = 100,reconciliationTimeout: TimeInterval = 30,onAccountChange: @escaping @MainActor @Sendable (AccountChange) -> Void = { _ in },- onBatch: @escaping @MainActor @Sendable (ActivityEventBatch) -> Void+ onBatch: @escaping @MainActor @Sendable (ActivityUpdate) -> Void) {- self.lifecycleCache = lifecycleCacheself.socketURL = socketURL+ backfill = AppServerActivityBackfill(socketURL: socketURL, logStorage: logStorage)self.logStorage = logStorage- self.tokenHistory = tokenHistory- self.recorder = recorder+ self.tokenUsageHistory = tokenUsageHistory+ self.activityDataRecorder = activityDataRecorderself.maximumPendingEvents = maximumPendingEventsself.maximumPendingBytes = maximumPendingBytesself.maximumLoadedPages = maximumLoadedPages@@ -127,7 +179,7 @@ actor AppServerActivityReader {}isRunning = falsegeneration += 1- tokenRefreshTask?.cancel()+ tokenUsageRefreshTask?.cancel()let previous = loopprevious?.cancel()loop = nil@@ -139,10 +191,12 @@ actor AppServerActivityReader {pendingPublication.removeAll()deferredNotifications.removeAll()verifiedTurns.removeAll()- turnCursors.removeAll()+ turnScans.removeAll()+ turnRevisions.removeAll()+ cancelBackfill()checkedThreads.removeAll()invalidThreads.removeAll()- if storageTask == nil, !pendingEvents.isEmpty || !pendingObservations.isEmpty {+ if storageTask == nil, !pendingEvents.isEmpty || !pendingSamples.isEmpty {startStorageWorker()}// 退出只给已接收记录有限的排空时间, 慢文件操作不能卡住 App 退出@@ -150,10 +204,9 @@ actor AppServerActivityReader {while storageTask != nil, ContinuousClock.now < deadline, !Task.isCancelled {try? await Task.sleep(for: .milliseconds(10))}- saved = storageTask == nil && pendingEvents.isEmpty && pendingObservations.isEmpty+ saved = storageTask == nil && pendingEvents.isEmpty && pendingSamples.isEmptystorageTask?.cancel()completeDrains(.cancelled)- await lifecycleCache.invalidate()return saved}@@ -161,7 +214,8 @@ actor AppServerActivityReader {func drainNow() async -> ActivityEventDrainResult {guard isRunning, !Task.isCancelled else { return .cancelled }guard session != nil else { return .sourceUnavailable }- return await withCheckedContinuation { drainWaiters.append($0) }+ let result = await withCheckedContinuation { drainWaiters.append($0) }+ return Task.isCancelled ? .cancelled : result}private func run(generation current: Int) async {@@ -177,8 +231,17 @@ actor AppServerActivityReader {do {let covered = try await reconcile(bootstrap: false, forced: !waiters.isEmpty, generation: current)try await consumeAvailable(generation: current)- let result: ActivityEventDrainResult = isRunning && generation == current && !Task- .isCancelled ? (covered ? .completed : .sourceUnavailable) : .cancelled+ let result: ActivityEventDrainResult = if !isRunning || generation != current || Task.isCancelled {+ .cancelled+ } else if let latestUpdate {+ if covered {+ .completed(latestUpdate)+ } else {+ .partial(latestUpdate)+ }+ } else {+ .sourceUnavailable+ }for waiter in waiters {waiter.resume(returning: result)}@@ -189,6 +252,7 @@ actor AppServerActivityReader {throw error}}+ scheduleBackfill(generation: current)try await Task.sleep(for: .milliseconds(100))} catch is CancellationError {completeDrains(.cancelled)@@ -201,10 +265,13 @@ actor AppServerActivityReader {}private func connect(generation current: Int) async throws {+ // 首批通知到达前确认采集租约, 避免连接和存储任务的启动顺序决定是否记账+ try acquireTokenUsageWriter(generation: current)+ isReconciliationComplete = falsereducer.reconnect()- reducer.setTokenRecordingEnabled(isTokenWriter && !isHistoryRecordingPaused)+ reducer.setTokenUsageRecordingEnabled(isTokenUsageWriter && !isHistoryRecordingPaused)try checkGeneration(current)- await onBatch(.bootstrapStart)+ await publish(.bootstrapStart)try checkGeneration(current)let connection = try AppServerSession(socketURL: socketURL, logStorage: logStorage)do {@@ -217,17 +284,17 @@ actor AppServerActivityReader {connectedAt = Date()_ = try await reconcile(bootstrap: true, forced: true, generation: current)try checkGeneration(current)- await onBatch(.bootstrapEnd)+ await publish(.bootstrapEnd)}- private func acquireTokenWriter(generation current: Int) async throws {- guard !isTokenWriter, Date() >= writerRetryAfter else { return }+ private func acquireTokenUsageWriter(generation current: Int) throws {+ guard !isTokenUsageWriter, Date() >= writerRetryAfter else { return }do {- if try await tokenHistory.acquireRecordingLease() {+ if try tokenUsageHistory.acquireRecordingLease() {try checkGeneration(current)- isTokenWriter = true+ isTokenUsageWriter = truewriterRetryAfter = .distantPast- reducer.setTokenRecordingEnabled(!isHistoryRecordingPaused)+ reducer.setTokenUsageRecordingEnabled(!isHistoryRecordingPaused)} else {writerRetryAfter = Date().addingTimeInterval(1)}@@ -266,11 +333,12 @@ actor AppServerActivityReader {deferredNotifications.removeAll()pendingAccountChange = nilverifiedTurns.removeAll()- turnCursors.removeAll()+ turnScans.removeAll()+ turnRevisions.removeAll()+ cancelBackfill()invalidThreads.removeAll()checkedThreads.removeAll()- await lifecycleCache.invalidate()- await onBatch(.sourceUnavailable)+ await publish(.sourceUnavailable)}private func reconcile(bootstrap: Bool, forced: Bool, generation current: Int) async throws -> Bool {@@ -292,7 +360,7 @@ actor AppServerActivityReader {guard cursors.insert(next).inserted else { throw CodexStatusError.invalidServerResponse }cursor = next}- // 不发布截断列表, 否则未读取的线程会被错误地当作已经卸载+ // 不发布截断列表, 否则未读取的会话会被错误地当作已经卸载throw CodexStatusError.invalidServerResponse}@@ -303,7 +371,7 @@ actor AppServerActivityReader {}.map(\.threadID))let now = Date()let dependencies = Set(reducer.missingRootTurns(liveOnly: forced).keys)- .union(reducer.missingSubagentParents(liveOnly: forced))+ .union(reducer.missingSubagentParents(liveOnly: forced)).union(backfillThreads)let required = loaded.union(knownActive).union(reducer.missingRootTurns(liveOnly: true).keys).union(reducer.missingSubagentParents(liveOnly: true))let candidates = loaded.union(knownActive).union(dependencies).filter { id in@@ -335,22 +403,26 @@ actor AppServerActivityReader {checkedThreads = checkedThreads.filter { retained.contains($0.key) }revisions = revisions.filter { retained.contains($0.key) }verifiedTurns = verifiedTurns.filter { reducer.states[$0.key] != nil && retained.contains($0.key.threadID) }- turnCursors = turnCursors.filter { retained.contains($0.key) }+ turnScans = turnScans.filter { retained.contains($0.key) }+ turnRevisions = turnRevisions.filter { reducer.states[$0.key] != nil && retained.contains($0.key.threadID) }+ backfillScans = backfillScans.filter { !$0.value.completed || reducer.states[$0.key] != nil }+ liveStartedTurns = liveStartedTurns.filter { reducer.states[$0] != nil }invalidThreads.formIntersection(retained)reconciledAt = Date()- await lifecycleCache.replace(reducer.states, verifiedTurns: verifiedTurns)try checkGeneration(current)- if !bootstrap {- await onBatch(.lifecycleChanged)- }- return covered && !reducer.states.values.contains {+ isReconciliationComplete = covered && !reducer.states.values.contains {$0.terminal == nil && ($0.rootThreadID == nil || $0.rootTurnID == nil)}+ if !bootstrap {+ await publish(.lifecycleChanged)+ }+ return isReconciliationComplete}private func reconcileThread(_ id: String, loaded: Set<String>, bootstrap: Bool, forced: Bool, generation current: Int) async throws -> Bool {- let started = Date()- let revision = revisions[id, default: 0]+ let checkpoint = ReadCheckpoint(threadRevision: revisions[id, default: 0], turns: turnRevisions)+ let active = Set(reducer.states.keys.filter { $0.threadID == id && reducer.states[$0]?.terminal == nil }.map(\.turnID))+ var remaining = active.union(reducer.missingRootTurns(liveOnly: forced)[id] ?? [])let newSubscription = !subscribed.contains(id)if newSubscription {preparingThread = id@@ -361,109 +433,251 @@ actor AppServerActivityReader {let read: ActivityThreadRead = try await request(ActivityRequests.thread(id), generation: current)var thread = read.threadif loaded.contains(id), newSubscription, thread.status?.type != .notLoaded {- let joined: ActivityThreadResume = try await request(- ActivityRequests.subscribe(id), generation: current- )+ let joined: ActivityThreadResume = try await request(ActivityRequests.subscribe(id), generation: current)thread = joined.threadsubscribed.insert(id)}if newSubscription {reducer.threads[id] = thread}- let includesItems = newSubscription || !reducer.missingSubagentContexts(in: id, liveOnly: forced).isEmpty- let first: ActivityTurnsPage = try await request(- ActivityRequests.turns(id, limit: 10, includesItems: includesItems), generation: current+ let first = try await request(ActivityRequests.turns(id, limit: 10), generation: current)+ await applySnapshot(thread: thread, turns: first.data, checkpoint: checkpoint, bootstrap: bootstrap || newSubscription)+ remaining.subtract(first.data.map(\.id))+ // 首个小页建立轮次身份后立即处理通知, 深页查询不再延迟实时事件+ preparingThread = nil+ consumeDeferred(for: id)+ if newSubscription {+ restoreTokenUsage(threadID: id, generation: current)+ }+ scheduleHistoryTargets(threadID: id, turns: first.data, joined: newSubscription)+ try await flushPending(generation: current)+ let result = try await readRemainingTurns(+ thread: thread, remaining: remaining, firstCursor: first.nextCursor, checkpoint: checkpoint,+ bootstrap: bootstrap || newSubscription, forced: forced, generation: current)- if newSubscription, first.nextCursor == nil, first.data.count <= 1,- first.data.allSatisfy({ $0.status == .running }),- let created = thread.createdAt, created >= connectedAt {- reducer.markNewThread(id)- }- let result = try await readRemainingTurns(id, first: first, includesItems: includesItems, forced: forced, generation: current)- // 查询期间收到的新状态优先, 不用旧响应倒退实时状态或刷新其验证时间- if revision == revisions[id, default: 0] {- let events = reducer.reconcile(thread: thread, turns: result.turns, now: Date(), bootstrap: bootstrap || newSubscription)- for turn in result.turns {- verifiedTurns[ActivityTurnReference(threadID: id, turnID: turn.id)] = started- }- for turnID in result.remaining {- let key = ActivityTurnReference(threadID: id, turnID: turnID)- verifiedTurns[key] = nil- reducer.states[key]?.readStatus = result.cursor == nil ? .notFound : .unavailable- }- let observations = reducer.takeTokenObservations()- enqueueHistory(events: [], observations: observations)- turnCursors[id] = result.cursor+ remaining = result.remaining+ for turnID in remaining.intersection(active) {+ let key = ActivityTurnReference(threadID: id, turnID: turnID)+ guard checkpoint.turns[key, default: 0] == turnRevisions[key, default: 0], reducer.states[key]?.terminal == nil else { continue }+ verifiedTurns[key] = nil+ reducer.states[key]?.readStatus = result.exhausted ? .notFound : .unavailable+ }+ let unchanged = checkpoint.threadRevision == revisions[id, default: 0]+ if unchanged {invalidThreads.remove(id)- covered = result.remaining.isEmpty && thread.status?.type != .notLoaded- if !events.isEmpty {- await onBatch(.snapshotEvents(events))- }}- preparingThread = nil- consumeDeferred(for: id)if invalidThreads.contains(id) {reducer.invalidateThread(id)}+ covered = unchanged && remaining.isDisjoint(with: active) && thread.status?.type != .notLoadedtry await flushPending(generation: current)} catch CodexStatusError.invalidResponsePayload {consumeDeferred(for: id)- invalidateThread(id, method: "activity/thread", resetTokenBaseline: false)+ invalidateThread(id, method: "activity/thread")} catch let error as CodexStatusError where error.isMissingRollout(for: id)|| (!error.isTransportFailure && !error.isProtocolOrParameterFailure) {reducer.invalidateThread(id)verifiedTurns = verifiedTurns.filter { $0.key.threadID != id }- turnCursors[id] = nil+ turnScans[id] = nilsubscribed.remove(id)consumeDeferred(for: id)} catch {consumeDeferred(for: id)throw error}- checkedThreads[id] = started+ checkedThreads[id] = checkpoint.startedreturn covered}private func readRemainingTurns(- _ id: String, first: ActivityTurnsPage, includesItems: Bool, forced: Bool, generation current: Int- ) async throws -> TurnReadResult {- var turns = first.data- let activeTurns = reducer.states.keys.filter {- $0.threadID == id && reducer.states[$0]?.terminal == nil- }.map(\.turnID)- var remaining = Set(activeTurns)- remaining.formUnion(reducer.missingRootTurns(liveOnly: forced)[id] ?? [])- remaining.subtract(first.data.map(\.id))- var missingAgents = reducer.missingSubagentContexts(in: id, liveOnly: forced)- func foundAgents(in turns: [ActivityTurn]) -> Set<String> {- var agents = Set<String>()- for turn in turns {- for item in turn.items ?? [] where item.type == .subAgentActivity && item.kind == "started" {- if let agent = item.agentThreadId {- agents.insert(agent)- }- }- }- return agents- }- missingAgents.subtract(foundAgents(in: first.data))- var cursor = turnCursors[id] ?? first.nextCursor- // 每轮重读最新页, 深页沿上次游标继续, 避免旧轮次一直被新轮次挤出查询窗口+ thread: ActivityThread, remaining initial: Set<String>, firstCursor: String?, checkpoint: ReadCheckpoint,+ bootstrap: Bool, forced: Bool, generation current: Int+ ) async throws -> (remaining: Set<String>, exhausted: Bool) {+ let id = thread.id+ var remaining = initial+ var scan = turnScans[id] ?? TurnScan(targets: remaining, cursor: firstCursor)+ if !remaining.isSubset(of: scan.targets), scan.exhausted {+ scan = TurnScan(targets: remaining, cursor: firstCursor)+ }+ // 扫描期间新增的目标留给后续一轮, 不将正在推进的游标退回首页+ let pendingTargets = remaining.subtracting(scan.targets)+ turnScans[id] = scanfor _ in 0 ..< (forced ? 8 : 1) {- guard !remaining.isEmpty || !missingAgents.isEmpty, let next = cursor else { break }- let page: ActivityTurnsPage = try await request(- ActivityRequests.turns(id, limit: 100, includesItems: includesItems, cursor: next), generation: current- )- guard page.nextCursor != next else { throw CodexStatusError.invalidServerResponse }- turns += page.data+ guard !remaining.isEmpty, !scan.exhausted, let cursor = scan.cursor else { break }+ let pageCheckpoint = ReadCheckpoint(threadRevision: checkpoint.threadRevision, turns: turnRevisions)+ let page = try await request(ActivityRequests.turns(id, limit: 100, cursor: cursor), generation: current)+ guard page.nextCursor != cursor, page.nextCursor.map({ !scan.visitedCursors.contains($0) }) ?? true else {+ throw CodexStatusError.invalidServerResponse+ }+ scan.visitedCursors.insert(cursor)+ await applySnapshot(thread: thread, turns: page.data, checkpoint: pageCheckpoint, bootstrap: bootstrap)+ scheduleHistoryTargets(threadID: id, turns: page.data, joined: false)remaining.subtract(page.data.map(\.id))- missingAgents.subtract(foundAgents(in: page.data))- cursor = page.nextCursor+ scan.cursor = page.nextCursor+ scan.exhausted = page.nextCursor == nil+ turnScans[id] = scan+ try await flushPending(generation: current)+ }+ if scan.cursor == nil {+ scan.exhausted = true+ }+ if scan.exhausted, !pendingTargets.isDisjoint(with: remaining) {+ scan = TurnScan(targets: remaining, cursor: firstCursor)}- return TurnReadResult(- turns: turns, remaining: remaining.intersection(activeTurns),- cursor: remaining.isEmpty && missingAgents.isEmpty ? nil : cursor+ turnScans[id] = remaining.isEmpty ? nil : scan+ return (remaining, scan.exhausted)+ }++ private func applySnapshot(thread: ActivityThread, turns: [ActivityTurn], checkpoint: ReadCheckpoint, bootstrap: Bool) async {+ let threadChanged = checkpoint.threadRevision != revisions[thread.id, default: 0]+ let changedTurns = Set(turns.filter { turn in+ let key = ActivityTurnReference(threadID: thread.id, turnID: turn.id)+ return checkpoint.turns[key, default: 0] != turnRevisions[key, default: 0]+ }.map(\.id))+ let events = reducer.reconcile(+ thread: thread, turns: turns, now: Date(), bootstrap: bootstrap,+ preservingThreadState: threadChanged, preservingTurns: changedTurns)+ if !threadChanged {+ for turn in turns where !changedTurns.contains(turn.id) {+ verifiedTurns[ActivityTurnReference(threadID: thread.id, turnID: turn.id)] = checkpoint.started+ }+ }+ if !events.isEmpty {+ await publish(.snapshotEvents, events: events)+ }+ }++ private func cancelBackfill() {+ backfillEpoch = UUID()+ backfillTask?.cancel()+ backfillTask = nil+ backfillScans.removeAll()+ backfillRoots.removeAll()+ backfillThreads.removeAll()+ liveStartedTurns.removeAll()+ for task in tokenUsageRestorations.values {+ task.cancel()+ }+ tokenUsageRestorations.removeAll()+ }++ private func restoreTokenUsage(threadID: String, generation current: Int) {+ guard tokenUsageRestorations[threadID] == nil else { return }+ let epoch = backfillEpoch+ tokenUsageRestorations[threadID] = Task { [weak self, tokenUsageHistory] in+ let result = try? await tokenUsageHistory.snapshot(threadID: threadID)+ guard let self else { return }+ await finishTokenUsageRestoration(result, threadID: threadID, epoch: epoch, generation: current)+ }+ }++ private func finishTokenUsageRestoration(_ snapshot: TokenUsageThreadSnapshot?, threadID: String, epoch: UUID, generation current: Int) {+ guard generation == current, backfillEpoch == epoch, isRunning else { return }+ tokenUsageRestorations[threadID] = nil+ if let snapshot {+ reducer.applyTokenUsage(snapshot)+ stateChanged = true+ }+ }++ private func scheduleHistoryTargets(threadID: String, turns: [ActivityTurn], joined: Bool) {+ for turn in turns {+ let reference = ActivityTurnReference(threadID: threadID, turnID: turn.id)+ let state = reducer.states[reference]+ let root = state.flatMap { state -> ActivityTurnReference? in+ guard let thread = state.rootThreadID, let turn = state.rootTurnID else { return nil }+ return ActivityTurnReference(threadID: thread, turnID: turn)+ }+ let existedBeforeConnection = !liveStartedTurns.contains(reference) && (turn.startedAt.map { $0 < connectedAt } ?? true)+ let midTurn = joined && turn.status == .running && existedBeforeConnection+ guard existedBeforeConnection, midTurn || root.map({ backfillRoots.contains($0) }) == true else { continue }+ if midTurn, let root {+ backfillRoots.insert(root)+ }+ guard backfillScans[reference] == nil, backfillScans.count < 10000 else { continue }+ backfillScans[reference] = BackfillScan()+ }+ }++ private func scheduleBackfill(generation current: Int) {+ guard backfillTask == nil, session != nil else { return }+ for reference in backfillScans.keys {+ if let state = reducer.states[reference], let thread = state.rootThreadID, let turn = state.rootTurnID {+ backfillRoots.insert(ActivityTurnReference(threadID: thread, turnID: turn))+ }+ }+ for (reference, state) in reducer.states where backfillScans[reference] == nil && backfillScans.count < 10000 {+ guard !liveStartedTurns.contains(reference), state.startedAt.map({ $0 < connectedAt }) ?? true,+ let thread = state.rootThreadID, let turn = state.rootTurnID,+ backfillRoots.contains(ActivityTurnReference(threadID: thread, turnID: turn)) else { continue }+ backfillScans[reference] = BackfillScan()+ }+ guard let reference = backfillScans.keys.filter({+ guard let scan = backfillScans[$0] else { return false }+ return !scan.completed && scan.retryAfter <= Date()+ }).min(by: {+ (backfillScans[$0]!.checkedAt, backfillRoots.contains($0) ? 0 : 1, $0.threadID, $0.turnID)+ < (backfillScans[$1]!.checkedAt, backfillRoots.contains($1) ? 0 : 1, $1.threadID, $1.turnID)+ }), let scan = backfillScans[reference] else { return }+ backfillScans[reference]?.checkedAt = Date()+ let epoch = backfillEpoch+ backfillTask = Task { [weak self, backfill, reconciliationTimeout] in+ let result: Result<AppServerActivityBackfill.Page, Error>+ do {+ result = try await .success(backfill.readPage(+ reference: reference, cursor: scan.cursor, limit: scan.limit, timeout: reconciliationTimeout+ ))+ } catch {+ result = .failure(error)+ }+ await self?.finishBackfill(result, reference: reference, epoch: epoch, generation: current)+ }+ }++ private func finishBackfill(_ result: Result<AppServerActivityBackfill.Page, Error>, reference: ActivityTurnReference, epoch: UUID, generation current: Int) async {+ guard isRunning, generation == current, backfillEpoch == epoch, !Task.isCancelled,+ var scan = backfillScans[reference] else { return }+ defer {+ if backfillEpoch == epoch {+ backfillTask = nil+ }+ }+ do {+ let page = try result.get()+ guard page.nextCursor.map({ !scan.visitedCursors.contains($0) }) ?? true else { throw CodexStatusError.invalidServerResponse }+ let events = reducer.historicalEvents(page.items, reference: reference, now: Date())+ + reducer.historicalTurnEvents(reference)+ try await activityDataRecorder.record(events: events)+ guard isRunning, generation == current, backfillEpoch == epoch, !Task.isCancelled else { return }+ for entry in page.items where entry.item.type == .subAgentActivity && entry.item.kind == "started" {+ if let child = entry.item.agentThreadId, backfillThreads.count < 10000 {+ backfillThreads.insert(child)+ }+ }+ scan.limit = page.limit+ if let cursor = scan.cursor {+ scan.visitedCursors.insert(cursor)+ }+ scan.cursor = page.nextCursor+ scan.failures = 0+ scan.completed = page.nextCursor == nil+ backfillScans[reference] = scan+ await publish(.lifecycleChanged)+ } catch {+ guard isRunning, generation == current, backfillEpoch == epoch, !Task.isCancelled else { return }+ scan.failures += 1+ scan.retryAfter = Date().addingTimeInterval(scan.failures == 1 ? 5 : 15)+ let status = error as? CodexStatusError+ let oversized = if case .messageTooLarge = status {+ true+ } else {+ false+ }+ scan.completed = scan.failures >= 3 || status?.isProtocolOrParameterFailure == true || oversized+ backfillScans[reference] = scan+ logStorage?.recordFailure(method: "activity/backfill", message: error.localizedDescription, connection: "activity/backfill")+ }}private func consumeDeferred(for id: String) {@@ -520,12 +734,7 @@ actor AppServerActivityReader {notification = try JSONDecoder().decode(ActivityInput.self, from: data)} catch is DecodingError {guard let threadID = AppServerActivityProtocol.recoveryThreadID(from: data) else { throw CodexStatusError.invalidServerResponse }- // 用量或轮次边界缺失时不能把跨轮次消耗归到下一条通知- invalidateThread(- threadID,- method: method,- resetTokenBaseline: [ActivityInput.Kind.usageUpdated, .turnStarted, .turnFinished].contains(AppServerActivityProtocol.kind(for: method))- )+ invalidateThread(threadID, method: method)return}let id = notification.params.threadId ?? notification.params.thread?.id@@ -539,17 +748,30 @@ actor AppServerActivityReader {private func consume(_ notification: ActivityInput) {stateChanged = true- if let id = notification.params.threadId ?? notification.params.thread?.id,- notification.kind.category != .progress {- revisions[id, default: 0] += 1- }+ recordStateChange(notification)let events = reducer.consume(notification, now: Date())pendingPublication += eventsif let id = notification.params.threadId ?? notification.params.thread?.id, invalidThreads.contains(id) {reducer.invalidateThread(id)}- let observations = reducer.takeTokenObservations()- enqueueHistory(events: events, observations: observations)+ let samples = reducer.takeTokenUsageSamples()+ enqueueHistory(events: events, samples: samples)+ }++ private func recordStateChange(_ notification: ActivityInput) {+ guard let id = notification.params.threadId ?? notification.params.thread?.id else { return }+ switch notification.kind {+ case .threadDiscovered, .threadStatusChanged, .turnStarted, .turnFinished,+ .commandApprovalRequested, .fileApprovalRequested, .permissionsRequested, .inputRequested, .requestResolved:+ revisions[id, default: 0] += 1+ default: break+ }+ if notification.kind == .turnStarted, let turn = notification.params.turn, turn.startedAt.map({ $0 >= connectedAt }) ?? true {+ liveStartedTurns.insert(ActivityTurnReference(threadID: id, turnID: turn.id))+ }+ if notification.kind == .turnStarted || notification.kind == .turnFinished, let turn = notification.params.turn {+ turnRevisions[ActivityTurnReference(threadID: id, turnID: turn.id), default: 0] += 1+ }}private func consumeAvailable(generation current: Int) async throws {@@ -576,17 +798,17 @@ actor AppServerActivityReader {private func runStorage(generation current: Int) async {while isRunning, generation == current, !Task.isCancelled {do {- try await acquireTokenWriter(generation: current)+ try acquireTokenUsageWriter(generation: current)_ = await retryStorage()try checkGeneration(current)- await refreshTokenHistory(generation: current)+ await refreshTokenUsageHistory(generation: current)try await Task.sleep(for: .milliseconds(100))} catch {break}}var failures = 0- while !Task.isCancelled, failures < 3, !pendingEvents.isEmpty || !pendingObservations.isEmpty {+ while !Task.isCancelled, failures < 3, !pendingEvents.isEmpty || !pendingSamples.isEmpty {do {try await persistPending()} catch {@@ -595,9 +817,9 @@ actor AppServerActivityReader {try? await Task.sleep(for: .milliseconds(100))}}- await tokenHistory.releaseRecordingLease()- isTokenWriter = false- reducer.setTokenRecordingEnabled(false)+ tokenUsageHistory.releaseRecordingLease()+ isTokenUsageWriter = false+ reducer.setTokenUsageRecordingEnabled(false)storageTask = nil// 上一次排空尚未结束时重新启用, 必须等旧写入确认后再启动下一轮if isRunning {@@ -610,7 +832,7 @@ actor AppServerActivityReader {do {for pending in pendingEvents.prefix(64) {try Task.checkCancellation()- try await recorder.record(event: pending.event)+ try await activityDataRecorder.record(event: pending.event)pendingEvents.removeFirst()pendingStorageBytes -= pending.bytes}@@ -618,10 +840,12 @@ actor AppServerActivityReader {failure = error}do {- for pending in pendingObservations.prefix(64) {+ for pending in pendingSamples.prefix(64) {try Task.checkCancellation()- try await tokenHistory.appendObservation(pending.observation)- pendingObservations.removeFirst()+ let snapshot = try await tokenUsageHistory.record(pending.sample)+ reducer.applyTokenUsage(snapshot)+ stateChanged = true+ pendingSamples.removeFirst()pendingStorageBytes -= pending.bytes}} catch {@@ -632,30 +856,27 @@ actor AppServerActivityReader {}}- private func refreshTokenHistory(generation current: Int) async {- guard isTokenWriter, pendingObservations.isEmpty, Date() >= tokenRetryAfter,- Date().timeIntervalSince(lastTokenRefresh) >= 1 else { return }- let refresh = Task { try await tokenHistory.refresh() }- tokenRefreshTask = refresh- defer { tokenRefreshTask = nil }+ private func refreshTokenUsageHistory(generation current: Int) async {+ guard isTokenUsageWriter, pendingSamples.isEmpty, Date() >= tokenUsageRetryAfter,+ Date().timeIntervalSince(lastTokenUsageRefresh) >= 1 else { return }+ let refresh = Task { try await tokenUsageHistory.refresh() }+ tokenUsageRefreshTask = refresh+ defer { tokenUsageRefreshTask = nil }do {- let committed = try await refresh.value+ _ = try await refresh.valueguard generation == current, isRunning, !Task.isCancelled else { return }- // 此循环刷新期间不写队列, 新观测全部在队列中, 按检查点重放后再发布- try reducer.acceptTokenTurns(committed, replaying: pendingObservations.map(\.observation))- stateChanged = true- tokenRefreshFailed = false- tokenRetryAfter = .distantPast- lastTokenRefresh = Date()+ tokenUsageRefreshFailed = false+ tokenUsageRetryAfter = .distantPast+ lastTokenUsageRefresh = Date()} catch is CancellationError {return} catch {guard generation == current, isRunning else { return }- if !tokenRefreshFailed {+ if !tokenUsageRefreshFailed {logStorage?.recordFailure(method: "activity/storage", message: error.localizedDescription, connection: "activity")}- tokenRefreshFailed = true- tokenRetryAfter = Date().addingTimeInterval(1)+ tokenUsageRefreshFailed = true+ tokenUsageRetryAfter = Date().addingTimeInterval(1)}}@@ -670,31 +891,70 @@ actor AppServerActivityReader {if !force, pendingPublication.isEmpty, Date().timeIntervalSince(lastPublished) < 0.1 {return}- await lifecycleCache.replace(reducer.states, verifiedTurns: verifiedTurns)try checkGeneration(current)stateChanged = falselastPublished = Date()let events = pendingPublicationpendingPublication.removeAll()if !events.isEmpty {- await onBatch(.live(events))- }- await onBatch(.lifecycleChanged)+ await publish(.live, events: events)+ }+ await publish(.lifecycleChanged)+ }++ #if CODEXBAR_TESTING+ func historyTargetsForTesting() -> Set<ActivityTurnReference> {+ Set(backfillScans.keys)+ }++ func lifecycleStatesForTesting(for references: [ActivityTurnReference]) -> [TurnLifecycleState] {+ TurnLifecycleSnapshot(states: reducer.states, verifiedTurns: verifiedTurns).lifecycleStates(for: references)+ }+ #endif++ private func publish(_ phase: ActivityUpdate.Phase, events: [ActivityDataRecord] = []) async {+ if case .bootstrapStart = phase {+ taskGraph = ActivityTaskGraph()+ }+ let source: ActivityEventSource = if case .live = phase {+ .live+ } else {+ .bootstrap+ }+ let lifecycleSnapshot = TurnLifecycleSnapshot(states: reducer.states, verifiedTurns: verifiedTurns)+ let transitions = taskGraph.update(events: events, source: source, lifecycle: lifecycleSnapshot)+ let terminalEvents = taskGraph.terminalEvents+ let backfilledTasks = taskGraph.backfilledTasks+ taskGraph.terminalEvents.removeAll(keepingCapacity: true)+ taskGraph.backfilledTasks.removeAll(keepingCapacity: true)+ revision &+= 1+ let update = ActivityUpdate(+ readerID: readerID,+ revision: revision,+ phase: phase,+ graph: taskGraph,+ transitions: transitions,+ terminalEvents: terminalEvents,+ backfilledTasks: backfilledTasks,+ isComplete: isReconciliationComplete+ )+ latestUpdate = update+ await onBatch(update)}- private func enqueueHistory(events: [ActivityRecord], observations: [TokenObservation]) {+ private func enqueueHistory(events: [ActivityDataRecord], samples: [TokenUsageSample]) {do {for event in events {guard !isHistoryRecordingPaused else { return }- let bytes = try AppServerEventRecord(activity: event).jsonLineData().count+ let bytes = try AppServerEventRecord(activityData: event).jsonLineData().countpendingEvents.append((event, bytes))pendingStorageBytes += bytesenforceStorageLimit()}- for observation in observations where isTokenWriter {+ for sample in samples where isTokenUsageWriter {guard !isHistoryRecordingPaused else { return }- let bytes = try JSONLines.stableEncoder.encode(observation).count- pendingObservations.append((observation, bytes))+ let bytes = try JSONLines.stableEncoder.encode(sample).count+ pendingSamples.append((sample, bytes))pendingStorageBytes += bytesenforceStorageLimit()}@@ -705,26 +965,23 @@ actor AppServerActivityReader {}private func enforceStorageLimit() {- if pendingEvents.count + pendingObservations.count >= maximumPendingEvents || pendingStorageBytes >= maximumPendingBytes {+ if pendingEvents.count + pendingSamples.count >= maximumPendingEvents || pendingStorageBytes >= maximumPendingBytes {pauseHistoryRecording()}}private func pauseHistoryRecording() {isHistoryRecordingPaused = true- reducer.setTokenRecordingEnabled(false)+ reducer.setTokenUsageRecordingEnabled(false)}- private func invalidateThread(_ id: String, method: String, resetTokenBaseline: Bool) {+ private func invalidateThread(_ id: String, method: String) {let firstFailure = invalidThreads.insert(id).insertedverifiedTurns = verifiedTurns.filter { $0.key.threadID != id }- turnCursors[id] = nil+ turnScans[id] = nilrevisions[id, default: 0] += 1stateChanged = truereducer.invalidateThread(id)- if resetTokenBaseline {- reducer.invalidateTokenBaseline(id)- }if firstFailure {logStorage?.recordFailure(method: method, message: "Invalid payload for thread \(id)", connection: "activity")}@@ -736,9 +993,9 @@ actor AppServerActivityReader {try await persistPending()storageFailed = falsestorageRetryAfter = .distantPast- if isHistoryRecordingPaused, pendingEvents.isEmpty, pendingObservations.isEmpty, isRunning, !Task.isCancelled {+ if isHistoryRecordingPaused, pendingEvents.isEmpty, pendingSamples.isEmpty, isRunning, !Task.isCancelled {isHistoryRecordingPaused = false- reducer.setTokenRecordingEnabled(isTokenWriter)+ reducer.setTokenUsageRecordingEnabled(isTokenUsageWriter)}return true} catch {diff --git a/CodexBar/Services/Activity/AppServerActivityReducer.swift b/CodexBar/Services/Activity/AppServerActivityReducer.swiftindex 8a0a587..a4e2715 100644--- a/CodexBar/Services/Activity/AppServerActivityReducer.swift+++ b/CodexBar/Services/Activity/AppServerActivityReducer.swift@@ -3,58 +3,34 @@ import Foundation/// 将协议事件映射为既有业务阶段, 快照核对与实时统计分别处理nonisolated struct AppServerActivityReducer {var threads: [String: ActivityThread] = [:]- var states: [ActivityTurnReference: SessionLifecycleState] = [:]+ var states: [ActivityTurnReference: TurnLifecycleState] = [:]private struct SubagentContext {- let parent: ActivityTurnReference+ let parentTurn: ActivityTurnReferencelet model: String?let effort: String?}private var subagentContexts: [String: SubagentContext] = [:]- private struct PendingUsage {- var usage: TokenUsage- var updatedAt: Date- }-- private var pendingUsage: [ActivityTurnReference: PendingUsage] = [:]- private var threadTotals: [String: TokenUsage] = [:]- /// 淘汰内存序列后重新观察时使用新流身份, 防止与磁盘检查点冲突- private struct TokenStream {- let id = UUID().uuidString.lowercased()- var sequence: Int64 = 0- }-- private var tokenStreams: [String: TokenStream] = [:]+ private var tokenUsagePositions: [String: Int64] = [:]private var threadLastObserved: [String: Date] = [:]private var loadedThreads = Set<String>()- private var lastHistoryCutoff: Date?private var lastTransientPrune = Date.distantPast- private(set) var tokenObservations: [TokenObservation] = []+ private(set) var tokenUsageSamples: [TokenUsageSample] = []private var seenEvents: [String: Date] = [:]private var approvals: [String: ActivityTurnReference] = [:]private var waitingSince: [String: Date] = [:]- private var observingSince = Date.distantPast- private(set) var tokenTurns: [String: TokenTurn] = [:]- private var isTokenRecordingEnabled = true-- mutating func setTokenRecordingEnabled(_ enabled: Bool) {- guard enabled != isTokenRecordingEnabled else { return }- isTokenRecordingEnabled = enabled- // 丢弃采集中断前的累计基线与待关联增量, 新观测使用独立流身份- threadTotals.removeAll()- tokenStreams.removeAll()- pendingUsage.removeAll()- tokenObservations.removeAll()- }-- mutating func reconnect(now: Date = Date()) {- // 断线期间的累计变化没有完整轮次归属, 重新建立基线, 不归入重连后的轮次- threadTotals.removeAll()- tokenStreams.removeAll()- pendingUsage.removeAll()+ private(set) var tokenUsageTurns: [ActivityTurnReference: TokenUsage] = [:]+ private var isTokenUsageRecordingEnabled = true++ mutating func setTokenUsageRecordingEnabled(_ enabled: Bool) {+ guard enabled != isTokenUsageRecordingEnabled else { return }+ isTokenUsageRecordingEnabled = enabled+ tokenUsageSamples.removeAll()+ }++ mutating func reconnect() {approvals.removeAll()waitingSince.removeAll()- observingSince = nowfor key in states.keys {states[key]?.presentation = nilstates[key]?.pendingApprovals.removeAll()@@ -69,63 +45,62 @@ nonisolated struct AppServerActivityReducer {}}- mutating func invalidateTokenBaseline(_ id: String) {- threadTotals.removeValue(forKey: id)+ mutating func takeTokenUsageSamples() -> [TokenUsageSample] {+ defer { tokenUsageSamples.removeAll(keepingCapacity: true) }+ return tokenUsageSamples}- mutating func markNewThread(_ id: String) {- if isTokenRecordingEnabled, threadTotals[id] == nil {- threadTotals[id] = .zero+ mutating func applyTokenUsage(_ snapshot: TokenUsageThreadSnapshot) {+ guard snapshot.position >= tokenUsagePositions[snapshot.threadID, default: 0] else { return }+ tokenUsagePositions[snapshot.threadID] = snapshot.position+ for (turnID, usage) in snapshot.turns {+ let key = reference(snapshot.threadID, turnID)+ tokenUsageTurns[key] = usage+ states[key]?.tokenUsage = usage}}- mutating func takeTokenObservations() -> [TokenObservation] {- defer { tokenObservations.removeAll(keepingCapacity: true) }- return tokenObservations- }-- mutating func acceptTokenTurns(_ turns: [TokenTurn], replaying observations: [TokenObservation] = []) throws {- var records = Dictionary(uniqueKeysWithValues: turns.map { ($0.id, $0) })- for observation in observations {- let turn = observation.turn- records[turn.id] = try observation.applying(to: records[turn.id])- if turn.rootID != turn.id, records[turn.rootID] == nil {- records[turn.rootID] = TokenTurn(id: turn.rootID, rootID: turn.rootID, startedAt: observation.rootStartedAt, updatedAt: turn.updatedAt)- }- }- tokenTurns = records- lastHistoryCutoff = nil- for key in states.keys {- states[key]?.tokenUsage = records[TokenTurn.identifier(thread: key.threadID, turn: key.turnID)]?.usage+ mutating func reconcile(+ thread: ActivityThread, turns: [ActivityTurn], now: Date, bootstrap: Bool = false,+ preservingThreadState: Bool = false, preservingTurns: Set<String> = []+ ) -> [ActivityDataRecord] {+ if preservingThreadState, var current = threads[thread.id] {+ current.cwd = current.cwd ?? thread.cwd+ current.model = current.model ?? thread.model+ current.reasoningEffort = current.reasoningEffort ?? thread.reasoningEffort+ current.parentThreadID = current.parentThreadID ?? thread.parentThreadID+ current.createdAt = current.createdAt ?? thread.createdAt+ threads[thread.id] = current+ } else {+ threads[thread.id] = thread}- }-- mutating func reconcile(thread: ActivityThread, turns: [ActivityTurn], now: Date, bootstrap: Bool = false) -> [ActivityRecord] {- threads[thread.id] = threadthreadLastObserved[thread.id] = nowlet previousReferences = Set(states.keys)for turn in turns {- for item in turn.items ?? [] {- recordSubagentContext(item, threadID: thread.id, turnID: turn.id)+ if preservingTurns.contains(turn.id) {+ mergeTurnMetadata(turn, threadID: thread.id)+ } else {+ updateTurn(turn, threadID: thread.id, now: now, historical: true)}- updateTurn(turn, threadID: thread.id, now: now, historical: true)}- resolveRoots()- var events: [ActivityRecord] = []- for turn in turns where turn.status == .running && thread.status?.type == .active {+ mergeSubagentContexts(threadID: thread.id, turns: turns, now: now)+ var events: [ActivityDataRecord] = []+ for turn in turns where turn.status == .running && threads[thread.id]?.status?.type == .active && !preservingTurns.contains(turn.id) {let key = reference(thread.id, turn.id)guard !previousReferences.contains(key) || bootstrap else { continue }let state = states[key]var value = event(.turnStarted, threadID: thread.id, turnID: turn.id, at: state?.startedAt ?? now)- value.context = ActivityContext(+ value.context = ActivityDataContext(method: "thread/turns/list", threadID: thread.id, turnID: turn.id,- parentThreadID: thread.parentID, rootThreadID: state?.rootThreadID,+ parentThreadID: thread.parentThreadID, rootThreadID: state?.rootThreadID,rootTurnID: state?.rootTurnID, turnStartedAt: state?.startedAt)events.append(value)}- updateWaiting(threadID: thread.id, status: thread.status, now: now)- if let key = activeReference(thread.id) {+ if !preservingThreadState {+ updateWaiting(threadID: thread.id, status: thread.status, now: now)+ }+ if !preservingThreadState, let key = activeReference(thread.id) {var live = states[key]?.presentation ?? ActivityLivePresentation()live.reconcile(status: thread.status)states[key]?.presentation = live@@ -133,12 +108,31 @@ nonisolated struct AppServerActivityReducer {return events}- mutating func consume(_ notification: ActivityInput, now: Date) -> [ActivityRecord] {+ /// 补查只填缺失资料, 不刷新生命周期验证时间或制造进展事件+ mutating func mergeSubagentContexts(threadID: String, turns: [ActivityTurn], now: Date) {+ for turn in turns {+ mergeTurnMetadata(turn, threadID: threadID)+ for item in turn.items ?? [] where item.agentThreadId.map({ subagentContexts[$0] == nil }) == true {+ recordSubagentContext(item, threadID: threadID, turnID: turn.id)+ }+ }+ resolveRoots(now: now)+ }++ private mutating func mergeTurnMetadata(_ turn: ActivityTurn, threadID: String) {+ let key = reference(threadID, turn.id)+ guard var state = states[key] else { return }+ state.startedAt = state.startedAt ?? turn.startedAt+ state.rootTurnID = state.rootTurnID ?? turn.rootTurnId.flatMap { $0.isEmpty ? nil : $0 }+ state.parentThreadID = state.parentThreadID ?? threads[threadID]?.parentThreadID+ states[key] = state+ }++ mutating func consume(_ notification: ActivityInput, now: Date) -> [ActivityDataRecord] {let category = notification.kind.categoryguard category != .ignored else { return [] }let params = notification.paramsguard let threadID = params.threadId ?? params.thread?.id else { return [] }- let wasKnown = threads[threadID] != nilif let thread = params.thread {threads[threadID] = thread}@@ -146,12 +140,10 @@ nonisolated struct AppServerActivityReducer {threadLastObserved[threadID] = nowlet date = (params.completedAt ?? params.startedAt) ?? nowlet turnID = params.turnId ?? params.turn?.id- var events: [ActivityRecord] = []+ var events: [ActivityDataRecord] = []switch notification.kind {case .threadDiscovered:- if !wasKnown, let created = params.thread?.createdAt, created >= Date(timeIntervalSince1970: floor(observingSince.timeIntervalSince1970)) {- markNewThread(threadID)- }+ breakcase .threadStatusChanged:threads[threadID]?.status = params.statusupdateWaiting(threadID: threadID, status: params.status, now: now)@@ -193,8 +185,8 @@ nonisolated struct AppServerActivityReducer {states[reference]?.approvalChangedAt = now}case .usageUpdated:- if let turnID, let usage = params.tokenUsage {- updateUsage(threadID: threadID, turnID: turnID, update: usage, now: now)+ if let tokenUsage = params.tokenUsage, isTokenUsageRecordingEnabled {+ recordTokenUsage(threadID: threadID, turnID: turnID, update: tokenUsage, now: now)}default:if category == .progress, let turnID {@@ -203,7 +195,7 @@ nonisolated struct AppServerActivityReducer {}updatePresentation(notification, threadID: threadID, turnID: turnID, at: date)if params.thread != nil || params.item?.type == .subAgentActivity {- resolveRoots()+ resolveRoots(now: now)}prune(now: now)return finish(events, notification: notification, threadID: threadID, turnID: turnID, now: now)@@ -229,9 +221,9 @@ nonisolated struct AppServerActivityReducer {}private mutating func finish(- _ events: [ActivityRecord], notification: ActivityInput,+ _ events: [ActivityDataRecord], notification: ActivityInput,threadID: String, turnID: String?, now: Date- ) -> [ActivityRecord] {+ ) -> [ActivityDataRecord] {let context = context(notification, threadID: threadID, turnID: turnID)return events.map { value invar value = value@@ -245,13 +237,13 @@ nonisolated struct AppServerActivityReducer {}}- private func context(_ notification: ActivityInput, threadID: String, turnID: String?) -> ActivityContext {+ private func context(_ notification: ActivityInput, threadID: String, turnID: String?) -> ActivityDataContext {let params = notification.paramslet state = turnID.flatMap { states[reference(threadID, $0)] }let item = params.item ?? approvalItem(notification)- return ActivityContext(+ return ActivityDataContext(method: notification.provenanceMethod, threadID: threadID, turnID: turnID,- parentThreadID: threads[threadID]?.parentID,+ parentThreadID: threads[threadID]?.parentThreadID,rootThreadID: state?.rootThreadID, rootTurnID: state?.rootTurnID,itemID: params.item?.id ?? params.itemId,itemType: item?.type.rawValue, itemStatus: params.item?.status,@@ -264,14 +256,10 @@ nonisolated struct AppServerActivityReducer {)}- private mutating func turnEvent(_ turn: ActivityTurn, threadID: String, now: Date) -> ActivityRecord {+ private mutating func turnEvent(_ turn: ActivityTurn, threadID: String, now: Date) -> ActivityDataRecord {updateTurn(turn, threadID: threadID, now: now, historical: false)- resolveRoots()- if turn.status != .running, threadTotals[threadID] == .zero {- // 未观察到本轮用量就已结束, 零基线不能把漏掉的消耗算到下一轮- threadTotals.removeValue(forKey: threadID)- }- let phase: ActivityEventKind = turn.status == .running ? .turnStarted : turn.status == .completed ? .turnCompleted : .turnAborted+ resolveRoots(now: now)+ let phase: ActivityDataEventKind = turn.status == .running ? .turnStarted : turn.status == .completed ? .turnCompleted : .turnAbortedlet timestamp = (turn.status == .running ? turn.startedAt : turn.completedAt) ?? nowreturn event(phase, threadID: threadID, turnID: turn.id, at: timestamp)}@@ -282,12 +270,6 @@ nonisolated struct AppServerActivityReducer {}private mutating func prune(now: Date) {- let retainedDate = HistoryStorage.retentionCutoffDate(today: now)- if lastHistoryCutoff != retainedDate {- let retainedRoots = Set(tokenTurns.values.filter { $0.updatedAt >= retainedDate }.map(\.rootID))- tokenTurns = tokenTurns.filter { $0.value.updatedAt >= retainedDate || retainedRoots.contains($0.key) }- lastHistoryCutoff = retainedDate- }guard now.timeIntervalSince(lastTransientPrune) >= 60 else { return }lastTransientPrune = nowlet cutoff = now.addingTimeInterval(-ActivityRetention.window)@@ -297,7 +279,7 @@ nonisolated struct AppServerActivityReducer {return (state.lastProgressAt ?? .distantPast) > cutoff|| (state.terminal == nil && state.readStatus != .notFound && (loadedThreads.contains(key.threadID) || state.readStatus == .complete))})- // 子线程仍在使用的祖先状态必须保留, 否则迟到用量会失去根轮次归属+ // 子会话仍在使用的祖先状态必须保留, 否则迟到用量会失去根轮次归属var changed = truewhile changed {let previous = retained@@ -306,25 +288,22 @@ nonisolated struct AppServerActivityReducer {if let root = state.rootThreadID, let turn = state.rootTurnID {retained.insert(reference(root, turn))}- if let parent = subagentContexts[key.threadID]?.parent {- retained.insert(parent)+ if let parentTurn = subagentContexts[key.threadID]?.parentTurn {+ retained.insert(parentTurn)}}changed = previous != retained}states = states.filter { retained.contains($0.key) }- pendingUsage = pendingUsage.filter { retained.contains($0.key) }let retainedThreads = loadedThreads.union(retained.flatMap { ancestorThreads(of: $0.threadID) }).union(threadLastObserved.filter { $0.value > cutoff }.keys)threads = threads.filter { retainedThreads.contains($0.key) }threadLastObserved = threadLastObserved.filter { retainedThreads.contains($0.key) }- subagentContexts = subagentContexts.filter { retainedThreads.contains($0.key) || retained.contains($0.value.parent) }- threadTotals = threadTotals.filter { retainedThreads.contains($0.key) }+ subagentContexts = subagentContexts.filter { retainedThreads.contains($0.key) || retained.contains($0.value.parentTurn) }+ tokenUsagePositions = tokenUsagePositions.filter { retainedThreads.contains($0.key) }+ tokenUsageTurns = tokenUsageTurns.filter { retained.contains($0.key) }waitingSince = waitingSince.filter { retainedThreads.contains($0.key) }approvals = approvals.filter { states[$0.value]?.terminal == nil && states[$0.value] != nil }- let retainedTokens = Set(retained.map { TokenTurn.identifier(thread: $0.threadID, turn: $0.turnID) })- .union(tokenObservations.map(\.turn.id))- tokenStreams = tokenStreams.filter { retainedTokens.contains($0.key) }}private func approvalIdentity(_ notification: ActivityInput) -> String? {@@ -334,11 +313,14 @@ nonisolated struct AppServerActivityReducer {return [notification.kind.rawValue, item, callback].joined(separator: ":")}- private mutating func itemEvents(_ item: ActivityItem, method: ActivityInput.Kind, threadID: String, turnID: String, at date: Date) -> [ActivityRecord] {- var events: [ActivityRecord] = []+ private mutating func itemEvents(_ item: ActivityItem, method: ActivityInput.Kind, threadID: String, turnID: String, at date: Date) -> [ActivityDataRecord] {recordSubagentContext(item, threadID: threadID, turnID: turnID)progress(threadID, turnID, at: date)- let starting = method == .itemStarted+ return itemFacts(item, starting: method == .itemStarted, threadID: threadID, turnID: turnID, at: date)+ }++ private func itemFacts(_ item: ActivityItem, starting: Bool, threadID: String, turnID: String, at date: Date) -> [ActivityDataRecord] {+ var events: [ActivityDataRecord] = []if item.isToolCall {events.append(event(starting ? .toolStarted : .toolCompleted, threadID: threadID, turnID: turnID, at: date, item: item))} else if item.type == .contextCompaction {@@ -356,6 +338,56 @@ nonisolated struct AppServerActivityReducer {return events}+ mutating func historicalEvents(_ entries: [ActivityItemEntry], reference: ActivityTurnReference, now: Date) -> [ActivityDataRecord] {+ let threadID = reference.threadID+ let turnID = reference.turnID+ let turn = ActivityTurn(id: turnID, status: .unknown, items: entries.map(\.item))+ mergeSubagentContexts(threadID: threadID, turns: [turn], now: now)+ let state = states[reference]+ return entries.flatMap { entry -> [ActivityDataRecord] in+ let started = entry.startedAt ?? state?.startedAt ?? now+ let completed = entry.completedAt ?? state?.terminal?.timestamp ?? started+ let item = entry.item+ var events = itemFacts(item, starting: true, threadID: threadID, turnID: turnID, at: started)+ let isCompleted = entry.completedAt != nil || ["completed", "failed", "declined", "interrupted"].contains(item.status ?? "")+ || (item.type == .subAgentActivity && ["started", "completed", "interrupted"].contains(item.kind ?? ""))+ if isCompleted {+ events += itemFacts(item, starting: false, threadID: threadID, turnID: turnID, at: completed)+ }+ return events.map { event in+ var event = event+ event.context = ActivityDataContext(+ method: "thread/items/list", threadID: threadID, turnID: turnID,+ parentThreadID: threads[threadID]?.parentThreadID, rootThreadID: state?.rootThreadID, rootTurnID: state?.rootTurnID,+ itemID: item.id, itemType: item.type.rawValue, itemStatus: item.status,+ agentThreadID: item.agentThreadId, itemKind: item.kind, turnStartedAt: state?.startedAt+ )+ return event+ }+ }+ }++ func historicalTurnEvents(_ reference: ActivityTurnReference) -> [ActivityDataRecord] {+ guard let state = states[reference] else { return [] }+ var phases: [(ActivityDataEventKind, Date)] = []+ if let started = state.startedAt {+ phases.append((.turnStarted, started))+ }+ if let terminal = state.terminal, let completed = terminal.timestamp {+ phases.append((state.turnStatus == .completed ? .turnCompleted : .turnAborted, completed))+ }+ return phases.map { phase, date in+ var value = event(phase, threadID: reference.threadID, turnID: reference.turnID, at: date)+ value.context = ActivityDataContext(+ method: "thread/turns/list", threadID: reference.threadID, turnID: reference.turnID,+ parentThreadID: threads[reference.threadID]?.parentThreadID,+ rootThreadID: state.rootThreadID, rootTurnID: state.rootTurnID, turnStatus: state.turnStatus,+ turnStartedAt: state.startedAt+ )+ return value+ }+ }+private func approvalItem(_ notification: ActivityInput) -> ActivityItem? {guard let id = notification.params.itemId else { return nil }switch notification.kind {@@ -372,7 +404,7 @@ nonisolated struct AppServerActivityReducer {let start = turn.startedAtlet reference = reference(threadID, turn.id)let previous = states[reference]- let terminal: SessionTerminalState?+ let terminal: TurnTerminalState?let isTerminal = turn.status.isTerminallet isLoaded = [.active, .idle].contains(threads[threadID]?.status?.type ?? .unknown)if isTerminal, turn.completedAt != nil || !historical || isLoaded {@@ -397,14 +429,14 @@ nonisolated struct AppServerActivityReducer {} else {terminal = previous?.terminal}- var state = SessionLifecycleState(+ var state = TurnLifecycleState(requestedThreadID: threadID, turnID: turn.id, startedAt: start ?? previous?.startedAt,effort: threads[threadID]?.reasoningEffort, lastProgressAt: previous?.lastProgressAt ?? start,terminal: terminal, readStatus: terminal == nil && (!isLoaded || turn.status != .running) ? .unavailable : .complete,contextObservedAt: now, rootTurnID: turn.rootTurnId.flatMap { $0.isEmpty ? nil : $0 } ?? previous?.rootTurnID,rootThreadID: nil,- parentThreadID: threads[threadID]?.parentID,- tokenUsage: previous?.tokenUsage ?? tokenTurns[TokenTurn.identifier(thread: threadID, turn: turn.id)]?.usage,+ parentThreadID: threads[threadID]?.parentThreadID,+ tokenUsage: previous?.tokenUsage ?? tokenUsageTurns[reference],isHistoricalTerminal: previous?.terminal == nil ? historical : previous?.isHistoricalTerminal ?? historical,isWaitingApproval: previous?.isWaitingApproval, approvalChangedAt: previous?.approvalChangedAt)@@ -450,8 +482,8 @@ nonisolated struct AppServerActivityReducer {states[key]?.lastProgressAt = max(lastProgress, date)}- /// 只匹配服务端给出的根轮次, 不根据创建时间或父线程当前轮次推断- private mutating func resolveRoots() {+ /// 只匹配服务端给出的根轮次, 不根据创建时间或父会话当前轮次推断+ private mutating func resolveRoots(now _: Date) {let byTurn = Dictionary(grouping: states.keys, by: \.turnID)for key in states.keys {guard let root = states[key]?.rootTurnID else { continue }@@ -465,17 +497,13 @@ nonisolated struct AppServerActivityReducer {let resolvedEffort = effort(for: key)states[key]?.effort = resolvedEffort}- for (key, pending) in pendingUsage where states[key]?.rootThreadID != nil {- recordUsage(key, previous: .zero, current: pending.usage, now: pending.updatedAt)- pendingUsage.removeValue(forKey: key)- }}private func ancestorThreads(of id: String) -> Set<String> {var result = Set<String>()var current: String? = idwhile let thread = current, result.insert(thread).inserted {- current = threads[thread]?.parentID ?? subagentContexts[thread]?.parent.threadID+ current = threads[thread]?.parentThreadID ?? subagentContexts[thread]?.parentTurn.threadID}return result}@@ -492,32 +520,25 @@ nonisolated struct AppServerActivityReducer {return result}- func missingSubagentContexts(in threadID: String, liveOnly: Bool = false) -> Set<String> {- let liveThreads = Set(states.values.filter { $0.terminal == nil }.map(\.requestedThreadID))- return Set(threads.values.filter {- $0.parentID == threadID && subagentContexts[$0.id] == nil && (!liveOnly || liveThreads.contains($0.id))- }.map(\.id))- }-func missingSubagentParents(liveOnly: Bool = false) -> Set<String> {let liveThreads = Set(states.values.filter { $0.terminal == nil }.map(\.requestedThreadID))return Set(threads.values.filter {subagentContexts[$0.id] == nil && (!liveOnly || liveThreads.contains($0.id))- }.compactMap(\.parentID))+ }.compactMap(\.parentThreadID))}private mutating func recordSubagentContext(_ item: ActivityItem, threadID: String, turnID: String) {- guard item.type == .subAgentActivity, item.kind == "started", let agent = item.agentThreadId else { return }- subagentContexts[agent] = SubagentContext(- parent: reference(threadID, turnID), model: item.model, effort: item.reasoningEffort+ guard item.type == .subAgentActivity, item.kind == "started", let agentThreadID = item.agentThreadId else { return }+ subagentContexts[agentThreadID] = SubagentContext(+ parentTurn: reference(threadID, turnID), model: item.model, effort: item.reasoningEffort)}private func effort(for key: ActivityTurnReference) -> String? {let current = threads[key.threadID]?.reasoningEffort- // 创建参数只属于创建时的根工作, 子线程参与其他根轮次时不能继续带入+ // 创建参数只属于创建时的根工作, 子会话参与其他根轮次时不能继续带入guard let context = subagentContexts[key.threadID], let root = states[key]?.rootTurnID,- states[context.parent]?.rootTurnID == root else { return current }+ states[context.parentTurn]?.rootTurnID == root else { return current }if let created = context.effort, let current, created != current {return "mixed"}@@ -535,74 +556,34 @@ nonisolated struct AppServerActivityReducer {}private func event(- _ phase: ActivityEventKind, threadID: String, turnID: String, at date: Date,+ _ phase: ActivityDataEventKind, threadID: String, turnID: String, at date: Date,item: ActivityItem? = nil, identity: String? = nil- ) -> ActivityRecord {+ ) -> ActivityDataRecord {let thread = threads[threadID]let state = states[reference(threadID, turnID)]- let isChild = thread?.parentID != nil- let agentID = item?.agentThreadId ?? (isChild ? threadID : nil)+ let isChild = thread?.parentThreadID != nil+ let agentThreadID = item?.agentThreadId ?? (isChild ? threadID : nil)let isAgentActivity = item?.type == .subAgentActivity- let agentThread = agentID.flatMap { threads[$0] }- let context = agentID.flatMap { subagentContexts[$0] }+ let agentThread = agentThreadID.flatMap { threads[$0] }+ let context = agentThreadID.flatMap { subagentContexts[$0] }let agentModel = item?.kind == "started" ? item?.model : agentThread?.model ?? context?.modellet agentEffort = item?.kind == "started" ? item?.reasoningEffort : agentThread?.reasoningEffort ?? context?.effort- return ActivityRecord(+ return ActivityDataRecord(timestamp: date, name: phase.rawValue, origin: thread?.origin ?? .unknown,cwd: thread?.cwd, toolName: item?.type == .commandExecution ? nil : item?.tool,commandActionTypes: item?.type == .commandExecution ? item?.commandActions?.map(\.type) : nil,model: isAgentActivity ? agentModel : thread?.model,effort: isAgentActivity ? agentEffort : thread?.reasoningEffort,- threadID: isChild ? state?.rootThreadID : threadID, turnID: isAgentActivity ? nil : turnID, agentID: agentID,+ threadID: isChild ? state?.rootThreadID : threadID, turnID: isAgentActivity ? nil : turnID, agentThreadID: agentThreadID,id: [threadID, turnID, identity ?? item?.id ?? "turn", phase.rawValue].joined(separator: ":"))}- private mutating func updateUsage(threadID: String, turnID: String, update: ActivityTokenUpdate, now: Date) {- guard isTokenRecordingEnabled else { return }- let total = update.total- guard total.isValid else { return }- defer { threadTotals[threadID] = total }- guard let previous = threadTotals[threadID], let delta = total.subtracting(previous), delta != .zero else { return }- let key = reference(threadID, turnID)- guard let state = states[key] else { return }- guard state.rootThreadID != nil, state.rootTurnID != nil else {- if let usage = (pendingUsage[key]?.usage ?? .zero).adding(delta), usage.isValid {- pendingUsage[key] = PendingUsage(usage: usage, updatedAt: now)- }- return- }- recordUsage(key, previous: previous, current: total, now: now)- }-- private mutating func recordUsage(_ key: ActivityTurnReference, previous: TokenUsage, current: TokenUsage, now: Date) {- guard let state = states[key], let rootThread = state.rootThreadID, let rootTurn = state.rootTurnID else { return }- let id = TokenTurn.identifier(thread: key.threadID, turn: key.turnID)- let existing = tokenTurns[id]- var stream = tokenStreams[id] ?? TokenStream()- stream.sequence += 1- tokenStreams[id] = stream- let rootKey = reference(rootThread, rootTurn)- let metadata = TokenTurn(- id: id,- rootID: TokenTurn.identifier(thread: rootThread, turn: rootTurn),- startedAt: state.startedAt,- updatedAt: now- )- let observation = TokenObservation(- turn: metadata,- rootStartedAt: states[rootKey]?.startedAt,- streamID: stream.id,- sequence: stream.sequence,- previous: previous,- current: current- )- tokenObservations.append(observation)- guard let record = try? observation.applying(to: existing) else { return }- states[key]?.tokenUsage = record.usage- tokenTurns[id] = record- if record.rootID != id, tokenTurns[record.rootID] == nil, let root = states[rootKey] {- tokenTurns[record.rootID] = TokenTurn(id: record.rootID, rootID: record.rootID, startedAt: root.startedAt, updatedAt: now, usage: nil)- }+ private mutating func recordTokenUsage(threadID: String, turnID: String?, update: TokenUsageUpdate, now: Date) {+ guard let turnID else { return }+ tokenUsageSamples.append(TokenUsageSample(+ observedAt: now, threadID: threadID, turnID: turnID,+ turnStartedAt: states[reference(threadID, turnID)]?.startedAt, current: update+ ))}}diff --git a/CodexBar/Services/Activity/ProtectionStore.swift b/CodexBar/Services/Activity/ProtectionStore.swiftindex 8767b1b..3407b05 100644--- a/CodexBar/Services/Activity/ProtectionStore.swift+++ b/CodexBar/Services/Activity/ProtectionStore.swift@@ -121,7 +121,7 @@ actor ProtectionStore {return [:]}let data = try Data(contentsOf: stateURL)- try StorageVersion.validate(data, current: ProtectionState.currentVersion, name: "Protection")+ try StorageVersion.validate(data, current: ProtectionState.schemaVersion, name: "Protection")let state = try JSONDecoder().decode(ProtectionState.self, from: data)return Dictionary(@@ -136,7 +136,7 @@ actor ProtectionStore {_ records: [String: ProtectionRecord]) throws {let state = ProtectionState(- version: ProtectionState.currentVersion,+ schemaVersion: ProtectionState.schemaVersion,records: records.values.sorted {$0.taskIdentifier < $1.taskIdentifier}@@ -150,8 +150,8 @@ actor ProtectionStore {}private nonisolated struct ProtectionState: Codable {- static let currentVersion = 1+ static let schemaVersion = 1- let version: Int+ let schemaVersion: Intlet records: [ProtectionRecord]}diff --git a/CodexBar/Services/Activity/SessionLifecycleCache.swift b/CodexBar/Services/Activity/TurnLifecycleSnapshot.swiftsimilarity index 81%rename from CodexBar/Services/Activity/SessionLifecycleCache.swiftrename to CodexBar/Services/Activity/TurnLifecycleSnapshot.swiftindex a8f87a5..f28fc54 100644--- a/CodexBar/Services/Activity/SessionLifecycleCache.swift+++ b/CodexBar/Services/Activity/TurnLifecycleSnapshot.swift@@ -6,30 +6,22 @@ nonisolated struct ActivityTurnReference: Hashable {}/// 运行状态按轮次验证有效期, 已确认终态不随连接或刷新期限失效-actor SessionLifecycleCache {- private var states: [ActivityTurnReference: SessionLifecycleState] = [:]- private var verifiedTurns: [ActivityTurnReference: Date] = [:]+nonisolated struct TurnLifecycleSnapshot {+ let states: [ActivityTurnReference: TurnLifecycleState]+ let verifiedTurns: [ActivityTurnReference: Date]- func replace(_ states: [ActivityTurnReference: SessionLifecycleState], verifiedTurns: [ActivityTurnReference: Date]) {- self.states = states- self.verifiedTurns = verifiedTurns- }-- func invalidate() {- verifiedTurns.removeAll()- }-- func lifecycleStates(for references: [ActivityTurnReference], now: Date = Date()) -> [SessionLifecycleState] {+ func lifecycleStates(for references: [ActivityTurnReference], now: Date = Date()) -> [TurnLifecycleState] {let requested = Set(references)- let childReferences = SessionLifecycleState.subagentStates(for: requested, in: Array(states.values)).map {+ let childReferences = TurnLifecycleState.subagentStates(for: requested, in: Array(states.values)).map {ActivityTurnReference(threadID: $0.requestedThreadID, turnID: $0.turnID)}var result = Array(requested.union(childReferences)).map { reference in- var state = states[reference] ?? SessionLifecycleState(+ var state = states[reference] ?? TurnLifecycleState(requestedThreadID: reference.threadID, turnID: reference.turnID, startedAt: nil,effort: nil, lastProgressAt: nil, terminal: nil, readStatus: .notFound)let verified = verifiedTurns[reference] ?? .distantPast+ state.verifiedAt = verifiedTurns[reference]// 已确认终态是事实, 不依赖运行状态的刷新期限if state.terminal == nil, now.timeIntervalSince(verified) > 15 {state.readStatus = .unavailable@@ -57,14 +49,14 @@ actor SessionLifecycleCache {}}-nonisolated struct SessionLifecycleState {+nonisolated struct TurnLifecycleState {let requestedThreadID: Stringlet turnID: String- let startedAt: Date?+ var startedAt: Date?var effort: String?var lastProgressAt: Date?- let terminal: SessionTerminalState?- var readStatus: SessionReadStatus = .complete+ let terminal: TurnTerminalState?+ var readStatus: TurnReadStatus = .completevar contextObservedAt: Date?var rootTurnID: String?var rootThreadID: String?@@ -77,8 +69,9 @@ nonisolated struct SessionLifecycleState {var terminalObservedAt: Date?var presentation: ActivityLivePresentation?var turnStatus: ActivityTurnStatus?+ var verifiedAt: Date?- /// 保留父线程已知但根轮次尚未确定的子任务, 不能把这部分缺口当成零+ /// 保留父会话已知但根轮次尚未确定的子任务, 不能把这部分缺口当成零static func subagentStates(for roots: Set<ActivityTurnReference>, in states: [Self]) -> [Self] {var result: [ActivityTurnReference: Self] = [:]for root in roots {@@ -109,13 +102,19 @@ nonisolated struct SessionLifecycleState {}}-nonisolated enum SessionReadStatus {+nonisolated enum TurnReadStatus {case completecase unavailablecase notFound}-nonisolated enum SessionTerminalState {+nonisolated enum TurnTerminalState {case completed(at: Date?, duration: TimeInterval?)case aborted(at: Date?, duration: TimeInterval?)++ var timestamp: Date? {+ switch self {+ case let .completed(at, _), let .aborted(at, _): at+ }+ }}diff --git a/CodexBar/Services/AppServer/AppServerLogStore.swift b/CodexBar/Services/AppServer/AppServerLogStore.swiftindex f5d7457..f506fcc 100644--- a/CodexBar/Services/AppServer/AppServerLogStore.swift+++ b/CodexBar/Services/AppServer/AppServerLogStore.swift@@ -22,7 +22,7 @@ final nonisolated class AppServerLogStore: @unchecked Sendable {AppStorage.directoryURL().appendingPathComponent("Logs", isDirectory: true)}- static let databaseName = "logs.sqlite"+ static let databaseName = "app.db"private static let schemaVersion = 1struct Limits: Sendable {@@ -144,7 +144,7 @@ final nonisolated class AppServerLogStore: @unchecked Sendable {let stats = try storage.stats()let entries = try storage.read("SELECT position, payload, id FROM entries WHERE position < ? ORDER BY position DESC LIMIT ?",- values: [position ?? Int64.max, Int64(max(1, limit) + 1)]+ values: [position ?? Int64.max, Int64(limit + 1)])return AppServerLogPage(entries: Array(entries.prefix(max(1, limit))), revision: stats.revision,@@ -294,11 +294,11 @@ final nonisolated class AppServerLogStore: @unchecked Sendable {}database = openeddo {- let version = try scalar("PRAGMA user_version")- if version != 0 {- try StorageVersion.require(version, current: Self.schemaVersion, name: "Logs")+ let schemaVersion = try scalar("PRAGMA user_version")+ if schemaVersion != 0 {+ try StorageVersion.require(schemaVersion, current: Self.schemaVersion, name: "Logs")} else if try scalar("SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name = 'entries'") != 0 {- throw StorageCompatibilityError.unsupportedFormat("Logs", version)+ throw StorageCompatibilityError.unsupportedFormat("Logs", schemaVersion)}try FileManager.default.setAttributes([.posixPermissions: 0o600], ofItemAtPath: path)sqlite3_busy_timeout(database, 3000)diff --git a/CodexBar/Services/AppServer/AppServerLogViewModel.swift b/CodexBar/Services/AppServer/AppServerLogViewModel.swiftindex 5308e73..395b423 100644--- a/CodexBar/Services/AppServer/AppServerLogViewModel.swift+++ b/CodexBar/Services/AppServer/AppServerLogViewModel.swift@@ -30,7 +30,6 @@ final class AppServerLogViewModel: ObservableObject {func start() {guard observation == nil else { return }- observationGeneration += 1observation = Task { [weak self] inwhile !Task.isCancelled {guard let self else { return }@@ -41,6 +40,7 @@ final class AppServerLogViewModel: ObservableObject {}func stop() {+ // 只在停止观察时使在途读取失效, 启动轮询继续复用当前读取observationGeneration += 1observation?.cancel()observation = nildiff --git a/CodexBar/Services/AppServer/AppServerRequests.swift b/CodexBar/Services/AppServer/AppServerRequests.swiftindex 1e177b0..fb9d662 100644--- a/CodexBar/Services/AppServer/AppServerRequests.swift+++ b/CodexBar/Services/AppServer/AppServerRequests.swift@@ -50,11 +50,19 @@ nonisolated enum ActivityRequests {AppServerRequest(method: "thread/resume", params: ["threadId": id, "excludeTurns": true])}- static func turns(_ id: String, limit: Int, includesItems: Bool, cursor: String? = nil) -> AppServerRequest<ActivityTurnsPage> {- var params: [String: Any] = ["threadId": id, "limit": limit, "itemsView": includesItems ? "full" : "notLoaded"]+ static func turns(_ id: String, limit: Int, cursor: String? = nil) -> AppServerRequest<ActivityTurnsPage> {+ var params: [String: Any] = ["threadId": id, "limit": limit, "itemsView": "notLoaded"]if let cursor {params["cursor"] = cursor}return AppServerRequest(method: "thread/turns/list", params: params)}++ static func items(_ reference: ActivityTurnReference, limit: Int, cursor: String?) -> AppServerRequest<ActivityItemsPage> {+ var params: [String: Any] = ["threadId": reference.threadID, "turnId": reference.turnID, "limit": limit, "sortDirection": "desc"]+ if let cursor {+ params["cursor"] = cursor+ }+ return AppServerRequest(method: "thread/items/list", params: params)+ }}diff --git a/CodexBar/Services/AppServer/AppServerSession.swift b/CodexBar/Services/AppServer/AppServerSession.swiftindex 8df78e9..36c3c6f 100644--- a/CodexBar/Services/AppServer/AppServerSession.swift+++ b/CodexBar/Services/AppServer/AppServerSession.swift@@ -30,13 +30,21 @@ final nonisolated class AppServerSession {descriptor >= 0}- private static let maximumMessageSize = 16 * 1024 * 1024-- init(socketURL: URL, retainsNotifications: Bool = true, logStorage: AppServerLogStore? = .shared, connectionName: String = "activity") throws {+ static let defaultMaximumMessageSize = 64 * 1024 * 1024+ private let maximumMessageSize: Int++ init(+ socketURL: URL,+ retainsNotifications: Bool = true,+ logStorage: AppServerLogStore? = .shared,+ connectionName: String = "activity",+ maximumMessageSize: Int = AppServerSession.defaultMaximumMessageSize+ ) throws {socketPath = socketURL.pathself.logStorage = logStorageself.connectionName = connectionNameself.retainsNotifications = retainsNotifications+ self.maximumMessageSize = maximumMessageSizelet connectionLogID = logStorage?.recordConnection(method: "connection/open", detail: socketPath, connection: connectionName, status: .pending)do {descriptor = socket(AF_UNIX, SOCK_STREAM, 0)@@ -347,8 +355,15 @@ final nonisolated class AppServerSession {if length == 126 || length == 127 {length = try read(length == 126 ? 2 : 8, before: frameDeadline).reduce(UInt64(0)) { ($0 << 8) | UInt64($1) }}- guard length <= Self.maximumMessageSize, message.count + Int(length) <= Self.maximumMessageSize,- header[1] & 128 == 0 else { throw CodexStatusError.invalidServerResponse }+ guard header[1] & 128 == 0, header[0] & 0x70 == 0, length <= UInt64(Int64.max),+ opcode >= 8 ? ([8, 9, 10].contains(opcode) && header[0] & 128 != 0 && length <= 125)+ : opcode == (fragmented ? 0 : 1) else { throw CodexStatusError.invalidServerResponse }+ let messageBytes = UInt64(message.count) + length+ guard opcode >= 8 || messageBytes <= maximumMessageSize else {+ // 正文尚未读取, 必须关闭连接, 不能把残留正文当作下一条消息+ close()+ throw CodexStatusError.messageTooLarge(bytes: messageBytes, limit: maximumMessageSize)+ }let payload = try read(Int(length), before: frameDeadline)if opcode >= 8 {logStorage?.recordReceived(method: controlMethod(opcode), payload: framePayload(payload), connection: connectionName)@@ -357,7 +372,6 @@ final nonisolated class AppServerSession {throw CodexStatusError.serverConnectionClosed}if opcode == 9 {- guard header[0] & 128 != 0, length <= 125 else { throw CodexStatusError.invalidServerResponse }try sendFrame(payload, opcode: 10)if !fragmented, try !ready(Int16(POLLIN), before: deadline) {return nil@@ -365,13 +379,11 @@ final nonisolated class AppServerSession {continue}if opcode == 10 {- guard header[0] & 128 != 0, length <= 125 else { throw CodexStatusError.invalidServerResponse }if !fragmented, try !ready(Int16(POLLIN), before: deadline) {return nil}continue}- guard header[0] & 0x70 == 0, opcode == (fragmented ? 0 : 1) else { throw CodexStatusError.invalidServerResponse }message.append(payload)if header[0] & 128 != 0 {if !retainsNotifications,@@ -403,7 +415,7 @@ final nonisolated class AppServerSession {/// 此判断不参与消息路由, 被过滤的消息仍交给业务消费private static func shouldRecordIncoming(_ object: [String: Any]?) -> Bool {guard let object, let method = object["method"] as? String else { return true }- if method == "error" || method == "warning" || AccountChange(method: method) != nil {+ if method == "error" || method == "warning" || method == "thread/tokenUsage/updated" || AccountChange(method: method) != nil {return true}switch AppServerActivityProtocol.kind(for: method).category {diff --git a/CodexBar/Services/Codex/CodexPaths.swift b/CodexBar/Services/Codex/CodexPaths.swiftindex 656856e..37e2b68 100644--- a/CodexBar/Services/Codex/CodexPaths.swift+++ b/CodexBar/Services/Codex/CodexPaths.swift@@ -18,7 +18,7 @@ nonisolated enum CodexExecutableSource: String, Equatable {nonisolated struct CodexServerConnectionInfo: Equatable {let socketPath: String/// 来自 initialize 握手, 代表当前 app-server 进程真实运行的版本- let version: String?+ let version: String}/// 一次 PATH 扫描得到的安装结果, 供版本检测使用diff --git a/CodexBar/Services/CodexStatus/CodexStatusService.swift b/CodexBar/Services/CodexStatus/CodexStatusService.swiftindex 00e7019..339e297 100644--- a/CodexBar/Services/CodexStatus/CodexStatusService.swift+++ b/CodexBar/Services/CodexStatus/CodexStatusService.swift@@ -47,32 +47,6 @@ nonisolated struct CodexFetchResult {let trace: CodexFetchTrace}-private nonisolated enum ConnectionResolution {- case ready(connection: AppServerConnection, reused: Bool)- case authenticationRequired- case unsupportedVersion(minimum: String)- case initializationFailed-}--// 单接口读取结果按后续动作分类: 跳过, 刷新认证, 重建连接-private nonisolated enum ReadResult<Value> {- case value(Value)- case skipped(ReadSkipReason)- case authRequired- case broken-}--private nonisolated enum ReadSkipReason {- case requestFailed(CodexStatusError)- case methodUnsupported-}--private nonisolated enum FetchFailure: Error {- case notLoggedIn- case authenticationRequired- case needsRebuild-}-private nonisolated struct CachedSupplementalRead<Value> {let value: Value?let step: CodexFetchTrace.StepResult@@ -99,35 +73,6 @@ private nonisolated struct SupplementalDataCache {}}-private nonisolated extension ReadResult {- var isAuthenticationRequired: Bool {- if case .authRequired = self {- return true- }- return false- }-- var value: Value? {- if case let .value(value) = self {- return value- }- return nil- }-- /// 认证刷新后仍是 authRequired/broken 则上抛- /// 其余原样返回交给调用方按缓存策略处理- func resultAfterAuthAttempt() throws -> ReadResult<Value> {- switch self {- case .value, .skipped:- return self- case .authRequired:- throw FetchFailure.authenticationRequired- case .broken:- throw FetchFailure.needsRebuild- }- }-}-/// 独立持有账户连接, 与活动监听连接同一个 Codex 后台服务actor CodexStatusService {private var connection: AppServerConnection?@@ -185,25 +130,19 @@ actor CodexStatusService {}/// 只重建当前客户端连接, 不重启 Codex 后台服务- func reconnect(minimumVersion: String) async throws -> CodexServerConnectionInfo {+ func reconnect() async throws -> CodexServerConnectionInfo {teardownConnection()supplementalDataCache = SupplementalDataCache()try await startup?.ensureStarted(trigger: .manual)- let candidate = try readyConnection()- guard let version = candidate.connectionInfo.version,- CodexVersionReader.isVersion(version, atLeast: minimumVersion) == true else {- teardownConnection()- throw CodexStatusError.unsupportedVersion(minimum: minimumVersion)- }- return candidate.connectionInfo+ return try ensureConnection().connection.connectionInfo}func areTUINotificationsEnabled() async throws -> Bool {- try readyConnection().session.perform(AccountRequests.configuration).areTUINotificationsEnabled+ try ensureConnection().connection.session.perform(AccountRequests.configuration).areTUINotificationsEnabled}func setTUINotificationsEnabled(_ enabled: Bool) async throws {- _ = try readyConnection().session.perform(AccountRequests.setTUINotifications(enabled))+ _ = try ensureConnection().connection.session.perform(AccountRequests.setTUINotifications(enabled))}func readCreditsForAutoReset(budget: AppServerRequestBudget) throws -> AutoResetRead {@@ -266,59 +205,49 @@ actor CodexStatusService {allowRebuild: Bool,trace: inout CodexFetchTrace) -> CodexFetchOutcome {- switch ensureConnection() {- case .authenticationRequired:+ let connection: AppServerConnection+ let reused: Bool+ do {+ (connection, reused) = try ensureConnection()+ } catch CodexStatusError.authenticationRequired {trace.failureStage = .accountreturn .authenticationRequired- case let .unsupportedVersion(minimum):+ } catch let CodexStatusError.unsupportedVersion(minimum) {trace.failureStage = .connectreturn .unsupportedVersion(minimum: minimum)- case .initializationFailed:+ } catch {trace.failureStage = .connectreturn .initializationFailed- case let .ready(connection, reused):- trace.connection = reused ? .reused : .new- do {- let snapshot = try fetchData(- using: connection,- refreshAccountInfo: reused,- trace: &trace- )- return .data(snapshot)- } catch FetchFailure.notLoggedIn {- supplementalDataCache = SupplementalDataCache()- connection.accountResponse = AccountReadResponse(account: nil)- return .notLoggedIn- } catch FetchFailure.authenticationRequired {- return .authenticationRequired- } catch FetchFailure.needsRebuild {- teardownConnection()- if reused, allowRebuild {- AppLog.app.notice("codex 连接已失效: reason=transportError")- return resolveOutcome(allowRebuild: false, trace: &trace)- }- return .initializationFailed- } catch {- return .initializationFailed+ }++ trace.connection = reused ? .reused : .new+ do {+ let snapshot = try fetchData(+ using: connection,+ refreshAccountInfo: reused,+ trace: &trace+ )+ return .data(snapshot)+ } catch CodexStatusError.notLoggedIn {+ supplementalDataCache = SupplementalDataCache()+ connection.accountResponse = AccountReadResponse(account: nil)+ return .notLoggedIn+ } catch CodexStatusError.authenticationRequired {+ return .authenticationRequired+ } catch let error as CodexStatusError where error.isTransportFailure {+ teardownConnection()+ if reused, allowRebuild {+ AppLog.app.notice("codex 连接已失效: reason=transportError")+ return resolveOutcome(allowRebuild: false, trace: &trace)}+ return .initializationFailed+ } catch {+ return .initializationFailed}}// MARK: - 连接复用与重建- private func readyConnection() throws -> AppServerConnection {- switch ensureConnection() {- case let .ready(connection, _):- return connection- case .authenticationRequired:- throw CodexStatusError.authenticationRequired- case let .unsupportedVersion(minimum):- throw CodexStatusError.unsupportedVersion(minimum: minimum)- case .initializationFailed:- throw CodexStatusError.serverConnectionClosed- }- }-/// 自动重置链路在传输故障后重建一次连接/// 每次逻辑操作最多刷新一次认证, 不在业务错误上重建连接private func withAutoResetConnection<Value>(@@ -328,7 +257,7 @@ actor CodexStatusService {while true {try AppServerRequestBudget.checkCurrent()- let activeConnection = try readyConnection()+ let activeConnection = try ensureConnection().connectiondo {return try performAutoResetOperation(@@ -354,15 +283,7 @@ actor CodexStatusService {throw error}try AppServerRequestBudget.checkCurrent()- do {- try Self.refreshAccount(using: connection)- } catch FetchFailure.notLoggedIn {- throw CodexStatusError.notLoggedIn- } catch FetchFailure.authenticationRequired {- throw CodexStatusError.authenticationRequired- } catch FetchFailure.needsRebuild {- throw CodexStatusError.serverConnectionClosed- }+ try Self.refreshAccount(using: connection)do {return try operation(connection)@@ -375,16 +296,14 @@ actor CodexStatusService {}}- private func ensureConnection() -> ConnectionResolution {+ private func ensureConnection() throws -> (connection: AppServerConnection, reused: Bool) {if let connection, connection.session.isOpen {- return .ready(connection: connection, reused: true)+ return (connection, true)}teardownConnection()- let resolution = Self.openConnection(socketURL: socketURL, logStorage: logStorage)- if case let .ready(newConnection, _) = resolution {- connection = newConnection- }- return resolution+ let connection = try Self.openConnection(socketURL: socketURL, logStorage: logStorage)+ self.connection = connection+ return (connection, false)}private func teardownConnection() {@@ -429,31 +348,41 @@ actor CodexStatusService {var didRefresh = falsefunc refreshTokenIfNeeded() throws {- guard !didRefresh else { throw FetchFailure.authenticationRequired }+ guard !didRefresh else { throw CodexStatusError.authenticationRequired }didRefresh = truetry Self.refreshAccount(using: connection)}- func readResultWithAuthRefresh<Value>(_ read: () -> ReadResult<Value>) throws -> ReadResult<Value> {- let firstAttempt = read()- guard firstAttempt.isAuthenticationRequired else {- return try firstAttempt.resultAfterAuthAttempt()+ func readWithAuthRefresh<Value>(_ read: () throws -> Value) throws -> Value {+ do {+ return try read()+ } catch let error as CodexStatusError where error.isAuthenticationRequired {+ try refreshTokenIfNeeded()+ return try read()}-- try refreshTokenIfNeeded()- return try read().resultAfterAuthAttempt()}func readSupplemental<Value: Decodable>(_ request: AppServerRequest<Value>,cache: inout Value?) throws -> CachedSupplementalRead<Value> {- try cachedRead(- readResultWithAuthRefresh {- Self.read(request, using: connection)- },- cache: &cache- )+ // 缓存回退只包住接口读取, 认证刷新失败必须继续上抛+ try readWithAuthRefresh {+ do {+ let value = try Self.read(request, using: connection)+ cache = value+ return CachedSupplementalRead(value: value, step: .ok)+ } catch let error as CodexStatusError where error.isUnsupportedMethod {+ return CachedSupplementalRead(value: nil, step: .skipped)+ } catch let error as CodexStatusError where error.isAuthenticationRequired || error.isTransportFailure {+ throw error+ } catch {+ guard let cachedValue = cache else {+ return CachedSupplementalRead(value: nil, step: .failed)+ }+ return CachedSupplementalRead(value: cachedValue, step: .cached)+ }+ }}// 新建连接已读过 account; 复用连接才刷新账户状态@@ -461,24 +390,18 @@ actor CodexStatusService {// 失败定位随流程推进, 不靠事后从别的字段反推trace.failureStage = .accountif refreshAccountInfo {- let accountResult: ReadResult<AccountReadResponse> = try readResultWithAuthRefresh {- Self.read(AccountRequests.account(refreshToken: false), using: connection)+ let response = try readWithAuthRefresh {+ try Self.read(AccountRequests.account(refreshToken: false), using: connection)}- guard let response = accountResult.value else {- if case let .skipped(.requestFailed(error)) = accountResult {- throw error- }- throw CodexStatusError.unsupportedMethod- }- guard response.account != nil else { throw FetchFailure.notLoggedIn }+ guard response.account != nil else { throw CodexStatusError.notLoggedIn }connection.accountResponse = response- trace.account = Self.accountStepResult(response: accountResult.value, didRefresh: didRefresh)+ trace.account = didRefresh ? .refreshed : .ok} else {trace.account = .skipped}guard let account = connection.accountResponse.account else {- throw FetchFailure.notLoggedIn+ throw CodexStatusError.notLoggedIn}trace.failureStage = .snapshot@@ -505,15 +428,13 @@ actor CodexStatusService {// rateLimits/usage 都可为空, 只要账户有效就让 UI 展示"暂无数据"- guard let snapshot = try? CodexQuotaSnapshot(+ let snapshot = try CodexQuotaSnapshot(accountResponse: connection.accountResponse,rateLimitsResponse: rateLimitsRead.value,usageResponse: usageRead.value,isRateLimitsStale: rateLimitsRead.isStale,isUsageStale: usageRead.isStale- ) else {- throw FetchFailure.notLoggedIn- }+ )trace.failureStage = nilreturn snapshot@@ -536,66 +457,26 @@ actor CodexStatusService {return rateLimitsStep == .cached ? .cached : .ok}- /// 读不到账号时后面会沿用连接上缓存的那份- /// 这种一轮记成 ok 的话, UI 展示的账号其实来自缓存这件事就无从发现- private static func accountStepResult(- response: AccountReadResponse?,- didRefresh: Bool- ) -> CodexFetchTrace.StepResult {- guard response != nil else {- return .cached- }- return didRefresh ? .refreshed : .ok- }-- /// 新值更新缓存- /// 本轮请求失败则回退到缓存并标记陈旧, 没有缓存才算这一步失败- /// step 在这里判定而不是事后从 value 与 isStale 反推: 只有这里还看得到原始的 ReadResult- /// 反推会把"请求失败"和"codex 不支持这个方法"压成同一个值, 而两者的处置完全不同- private func cachedRead<Value>(- _ result: ReadResult<Value>,- cache: inout Value?- ) -> CachedSupplementalRead<Value> {- switch result {- case let .value(value):- cache = value- return CachedSupplementalRead(value: value, step: .ok)- case .skipped(.requestFailed):- guard let cachedValue = cache else {- return CachedSupplementalRead(value: nil, step: .failed)- }- return CachedSupplementalRead(value: cachedValue, step: .cached)- case .skipped(.methodUnsupported):- return CachedSupplementalRead(value: nil, step: .skipped)- case .authRequired, .broken:- // resultAfterAuthAttempt 已经把这两种上抛, 到不了这里, 写出来只为穷尽- return CachedSupplementalRead(value: nil, step: .missing)- }- }-private static func read<Value: Decodable>(_ request: AppServerRequest<Value>,using connection: AppServerConnection- ) -> ReadResult<Value> {+ ) throws -> Value {do {- return try .value(connection.session.perform(request))+ return try connection.session.perform(request)} catch let error as CodexStatusError {- return classify(error)+ if error.isAuthenticationRequired {+ throw CodexStatusError.authenticationRequired+ }+ if error.isUnsupportedMethod {+ throw CodexStatusError.unsupportedMethod+ }+ if error.isTransportFailure {+ throw CodexStatusError.serverConnectionClosed+ }+ throw error} catch {- return .broken- }- }-- private static func classify<Value>(_ error: CodexStatusError) -> ReadResult<Value> {- if error.isAuthenticationRequired {- return .authRequired- }- if error.isUnsupportedMethod {- return .skipped(.methodUnsupported)+ throw CodexStatusError.serverConnectionClosed}- // 重试后仍失败的非认证业务错误不阻断整轮刷新-- return error.isTransportFailure ? .broken : .skipped(.requestFailed(error))}private static func refreshAccount(using connection: AppServerConnection) throws {@@ -604,38 +485,32 @@ actor CodexStatusService {response = try connection.session.perform(AccountRequests.account(refreshToken: true))} catch let error as CodexStatusError {if error.isTransportFailure {- throw FetchFailure.needsRebuild+ throw CodexStatusError.serverConnectionClosed}if error.isAuthenticationRequired {- throw FetchFailure.authenticationRequired+ throw CodexStatusError.authenticationRequired}throw error} catch {- throw FetchFailure.needsRebuild+ throw CodexStatusError.serverConnectionClosed}connection.accountResponse = responseif response.account == nil {- throw FetchFailure.notLoggedIn+ throw CodexStatusError.notLoggedIn}}/// 握手和账户读取成功即可保留连接, 账户为空时继续监听登录变化- private static func openConnection(socketURL: URL, logStorage: AppServerLogStore?) -> ConnectionResolution {+ private static func openConnection(socketURL: URL, logStorage: AppServerLogStore?) throws -> AppServerConnection {+ let session: AccountSessiondo {- let session = try AccountSession(socketURL: socketURL, logStorage: logStorage)- return initializeConnection(session: session, socketURL: socketURL, logStorage: logStorage)+ session = try AccountSession(socketURL: socketURL, logStorage: logStorage)} catch {logStorage?.recordFailure(message: error.localizedDescription)- return .initializationFailed+ throw CodexStatusError.serverConnectionClosed}- }- private static func initializeConnection(- session: AccountSession,- socketURL: URL,- logStorage: AppServerLogStore?- ) -> ConnectionResolution {do {let initialized = try session.initializeAccount()let connectionInfo = CodexServerConnectionInfo(@@ -643,20 +518,17 @@ actor CodexStatusService {version: initialized.version)- return .ready(- connection: AppServerConnection(- session: session,- accountResponse: initialized.account,- connectionInfo: connectionInfo- ),- reused: false+ return AppServerConnection(+ session: session,+ accountResponse: initialized.account,+ connectionInfo: connectionInfo)} catch let error as CodexStatusError where error.isAuthenticationRequired {session.close()- return .authenticationRequired+ throw CodexStatusError.authenticationRequired} catch let CodexStatusError.unsupportedVersion(minimum) {session.close()- return .unsupportedVersion(minimum: minimum)+ throw CodexStatusError.unsupportedVersion(minimum: minimum)} catch {// app-server 链路的细节按既有分工进日志窗口, 不重复写系统日志logStorage?.recordFailure(@@ -666,7 +538,7 @@ actor CodexStatusService {))session.close()- return .initializationFailed+ throw CodexStatusError.serverConnectionClosed}}}diff --git a/CodexBar/Services/CodexStatus/CodexStatusViewModel.swift b/CodexBar/Services/CodexStatus/CodexStatusViewModel.swiftindex bc2817d..4501606 100644--- a/CodexBar/Services/CodexStatus/CodexStatusViewModel.swift+++ b/CodexBar/Services/CodexStatus/CodexStatusViewModel.swift@@ -360,9 +360,7 @@ final class CodexStatusViewModel: ObservableObject {}do {- codexConnectionInfo = try await service.reconnect(- minimumVersion: CodexVersionReader.minimumAppServerVersion- )+ codexConnectionInfo = try await service.reconnect()didReconnect = true} catch {loadState = switch error {diff --git a/CodexBar/Services/History/ActivityDataRecorder.swift b/CodexBar/Services/History/ActivityDataRecorder.swiftnew file mode 100644index 0000000..70f974a--- /dev/null+++ b/CodexBar/Services/History/ActivityDataRecorder.swift@@ -0,0 +1,19 @@+import Foundation++/// 实时通知和历史执行项共用数据库事件身份去重+actor ActivityDataRecorder {+ private let directoryURL: URL++ init(directoryURL: URL = HistoryStorage.directoryURL()) {+ self.directoryURL = directoryURL+ }++ func record(event: ActivityDataRecord) throws {+ guard event.id != nil else { return }+ try HistoryDatabase(directoryURL: directoryURL).append(AppServerEventRecord(activityData: event))+ }++ func record(events: [ActivityDataRecord]) throws {+ try HistoryDatabase(directoryURL: directoryURL).appendActivityData(events)+ }+}diff --git a/CodexBar/Services/History/ActivityRecorder.swift b/CodexBar/Services/History/ActivityRecorder.swiftdeleted file mode 100644index 3725aec..0000000--- a/CodexBar/Services/History/ActivityRecorder.swift+++ /dev/null@@ -1,20 +0,0 @@-import Foundation--/// 采集器只写已观察到的活动, 初始快照不进入历史-actor ActivityRecorder {- private let directoryURL: URL- private var journal = AppServerEventJournal()-- init(directoryURL: URL = HistoryStorage.directoryURL()) {- self.directoryURL = directoryURL- }-- func record(event: ActivityRecord) throws {- guard event.id != nil else { return }- try HistoryStorage.withExclusiveLock(in: directoryURL) {- try journal.append(AppServerEventRecord(activity: event), in: directoryURL)- }- }-}--// 调用方持有 events.lock, 活动与 Token 共用追加和维护状态事务diff --git a/CodexBar/Services/History/AppServerEventJournal.swift b/CodexBar/Services/History/AppServerEventJournal.swiftdeleted file mode 100644index edee40f..0000000--- a/CodexBar/Services/History/AppServerEventJournal.swift+++ /dev/null@@ -1,197 +0,0 @@-import Foundation--nonisolated struct AppServerEventJournal {- private var identities: [String: Set<String>] = [:]- private var sizes: [String: UInt64] = [:]- private var inodes: [String: UInt64] = [:]-- mutating func append(_ entry: AppServerEventRecord, in directoryURL: URL) throws {- let dateKey = HistoryStorage.dateKey(for: entry.activity?.timestamp ?? entry.recordedAt)- let eventsDirectory = directoryURL.appendingPathComponent("Events")- let eventLogURL = HistoryStorage.eventLogURL(for: dateKey, in: eventsDirectory)- let maintenanceURL = HistoryStorage.maintenanceURL(in: directoryURL)- var maintenanceState = try JSONFileStorage.load(HistoryMaintenanceState.self, from: maintenanceURL) ?? HistoryMaintenanceState()- if !FileManager.default.fileExists(atPath: eventLogURL.path) {- let header = Header(version: Header.currentVersion, date: dateKey, generationID: UUID().uuidString.lowercased())- try FileManager.default.createDirectory(at: eventsDirectory, withIntermediateDirectories: true)- try (JSONLines.stableEncoder.encode(header) + Data([JSONLines.newlineByte])).write(to: eventLogURL, options: .atomic)- }- let header = try Self.header(at: eventLogURL)- guard header.date == dateKey else { throw StorageCompatibilityError.sourceConflict }- let existingStat = HistoryStorage.fileStat(at: eventLogURL)- var stateChanged = false- if entry.deduplicationID != nil, sizes[dateKey] != existingStat?.size || inodes[dateKey] != existingStat?.identifier {- let offset = sizes[dateKey].flatMap { size in- inodes[dateKey] == existingStat?.identifier && size < (existingStat?.size ?? 0) ? size : nil- } ?? 0- var previous = offset > 0 ? identities[dateKey] ?? [] : []- if existingStat != nil {- try Self.read(at: eventLogURL, from: offset) { entry in- if let id = entry.deduplicationID {- previous.insert(id)- }- }- }- identities[dateKey] = previous- }- if let identity = entry.deduplicationID, identities[dateKey]?.contains(identity) == true {- if maintenanceState.markPending(dateKey) {- try JSONFileStorage.save(maintenanceState, to: maintenanceURL)- }- return- }-- if maintenanceState.days[dateKey]?.generationID != header.generationID {- maintenanceState.days[dateKey] = HistoryDayMaintenanceState(- generationID: header.generationID, fileIdentifier: existingStat?.identifier- )- maintenanceState.markDirty(dateKey)- stateChanged = true- }-- try append(entry.jsonLineData(), to: eventLogURL)- if let identity = entry.deduplicationID {- identities[dateKey, default: []].insert(identity)- }- sizes[dateKey] = entry.deduplicationID != nil ? HistoryStorage.fileSize(at: eventLogURL) : nil- inodes[dateKey] = HistoryStorage.fileStat(at: eventLogURL)?.identifier- let oldest = HistoryStorage.dateKey(for: Date().addingTimeInterval(-2 * 86400))- identities = identities.filter { $0.key >= oldest }- sizes = sizes.filter { $0.key >= oldest }- inodes = inodes.filter { $0.key >= oldest }-- if var day = maintenanceState.days[dateKey],- day.fileIdentifier == nil,- let identifier = HistoryStorage.fileStat(at: eventLogURL)?.identifier {- day.fileIdentifier = identifier- maintenanceState.days[dateKey] = day- stateChanged = true- }-- // 稳态下当天早已 pending, 跳过无变化的全量重写以缩短持锁时间- if maintenanceState.markPending(dateKey) || stateChanged {- try JSONFileStorage.save(maintenanceState, to: maintenanceURL)- }- }-- /// 分块扫描并限制单行大小, 截断或损坏的行不影响其他完整记录- static func read(- at url: URL,- from offset: UInt64 = 0,- upTo endOffset: UInt64? = nil,- onInvalidLine: (() -> Void)? = nil,- consume: (AppServerEventRecord) throws -> Void- ) throws {- if let endOffset, endOffset <= offset {- return- }- let header = try Self.header(at: url)- let handle = try FileHandle(forReadingFrom: url)- defer { try? handle.close() }- try handle.seek(toOffset: offset)- var remaining = endOffset.map { $0 > offset ? $0 - offset : 0 } ?? UInt64.max- var buffer = Data()- var droppingLine = false- let maximumLineSize = 1024 * 1024- var firstLine = offset == 0- func consumeLine(_ data: Data) throws {- try Task.checkCancellation()- if firstLine {- firstLine = false- let decoded = try JSONLines.decoder.decode(Header.self, from: data)- guard decoded == header else { throw StorageCompatibilityError.sourceConflict }- return- }- if let text = String(bytes: data, encoding: .utf8), text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {- return- }- let entry: AppServerEventRecord- do {- try StorageVersion.validate(data, current: AppServerEventRecord.currentVersion, name: "Event")- entry = try AppServerEventRecord.decode(from: data)- } catch is DecodingError {- onInvalidLine?()- return- }- try consume(entry)- }- while remaining > 0 {- try Task.checkCancellation()- let readSize = Int(min(remaining, 64 * 1024))- guard let chunk = try handle.read(upToCount: readSize), !chunk.isEmpty else { break }- remaining -= UInt64(chunk.count)- buffer.append(chunk)- while let newline = buffer.firstIndex(of: JSONLines.newlineByte) {- let line = Data(buffer[..<newline])- if !droppingLine {- if line.count <= maximumLineSize {- try consumeLine(line)- } else {- onInvalidLine?()- }- }- droppingLine = false- buffer.removeSubrange(...newline)- }- if buffer.count > maximumLineSize || droppingLine {- if !droppingLine {- onInvalidLine?()- }- buffer.removeAll(keepingCapacity: true)- droppingLine = true- }- }- if !droppingLine, !buffer.isEmpty {- try consumeLine(buffer)- }- }-- struct Header: Codable, Equatable {- static let currentVersion = 1- let version: Int- let date: String- let generationID: String- }-- static func header(at url: URL) throws -> Header {- let handle = try FileHandle(forReadingFrom: url)- defer { try? handle.close() }- guard let data = try handle.read(upToCount: 4096), let end = data.firstIndex(of: JSONLines.newlineByte) else {- throw StorageCompatibilityError.incompleteSource- }- let line = Data(data[..<end])- try StorageVersion.validate(line, current: Header.currentVersion, name: "EventJournal")- let header = try JSONLines.decoder.decode(Header.self, from: line)- guard HistoryStorage.isValidDateKey(header.date), !header.generationID.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else {- throw StorageCompatibilityError.sourceConflict- }- return header- }-- private func append(_ data: Data, to url: URL) throws {- try FileManager.default.createDirectory(- at: url.deletingLastPathComponent(),- withIntermediateDirectories: true- )-- if !FileManager.default.fileExists(atPath: url.path) {- FileManager.default.createFile(atPath: url.path, contents: nil, attributes: [.posixPermissions: 0o600])- }-- let fileHandle = try FileHandle(forUpdating: url)- defer {- try? fileHandle.close()- }-- let length = try fileHandle.seekToEnd()- if length > 0 {- try fileHandle.seek(toOffset: length - 1)- let last = try fileHandle.read(upToCount: 1)- try fileHandle.seekToEnd()- if last != Data([0x0A]) {- try fileHandle.write(contentsOf: Data([0x0A]))- }- }- try fileHandle.write(contentsOf: data)- }-}diff --git a/CodexBar/Services/History/HistoryDatabase.swift b/CodexBar/Services/History/HistoryDatabase.swiftnew file mode 100644index 0000000..2ca5e6c--- /dev/null+++ b/CodexBar/Services/History/HistoryDatabase.swift@@ -0,0 +1,408 @@+import Foundation+import SQLite3++/// 连接只在所属采集或历史 actor 的同步调用内使用, 事务不跨 await+final nonisolated class HistoryDatabase {+ static let filename = "event.db"+ private static let schemaVersion: Int32 = 1+ private static let transient = unsafeBitCast(-1, to: sqlite3_destructor_type.self)+ private var connection: OpaquePointer?++ enum Value {+ case integer(Int64)+ case text(String)+ case data(Data)+ case null+ }++ struct Failure: Error {+ let code: Int32+ let message: String+ }++ struct Entry {+ let position: Int64+ let record: AppServerEventRecord+ }++ init(directoryURL: URL) throws {+ try FileManager.default.createDirectory(at: directoryURL, withIntermediateDirectories: true)+ let url = directoryURL.appendingPathComponent(Self.filename)+ let result = sqlite3_open_v2(url.path, &connection, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX, nil)+ guard result == SQLITE_OK else {+ let error = failure(result)+ sqlite3_close(connection)+ connection = nil+ throw error+ }+ sqlite3_busy_timeout(connection, 2000)+ do {+ let schemaVersion = try scalar("PRAGMA user_version")+ guard schemaVersion == 0 || schemaVersion == Int64(Self.schemaVersion) else {+ throw StorageCompatibilityError.unsupportedFormat("HistoryDatabase", Int(schemaVersion))+ }+ try execute("PRAGMA foreign_keys = ON")+ try execute("PRAGMA synchronous = FULL")+ if schemaVersion == 0 {+ try transaction {+ try execute("""+ CREATE TABLE IF NOT EXISTS days (+ date TEXT PRIMARY KEY+ )+ """)+ try execute("""+ CREATE TABLE IF NOT EXISTS events (+ position INTEGER PRIMARY KEY AUTOINCREMENT,+ identity TEXT UNIQUE,+ date TEXT NOT NULL REFERENCES days(date) ON DELETE CASCADE,+ kind TEXT NOT NULL,+ payload BLOB NOT NULL+ )+ """)+ try execute("CREATE INDEX IF NOT EXISTS events_date ON events(date, position)")+ try execute("""+ CREATE TABLE IF NOT EXISTS aggregates (+ date TEXT PRIMARY KEY REFERENCES days(date) ON DELETE CASCADE,+ activityData BLOB,+ tokenUsage BLOB+ )+ """)+ try execute("PRAGMA user_version = \(Self.schemaVersion)")+ }+ }+ try execute("""+ CREATE INDEX IF NOT EXISTS events_token_usage_thread+ ON events(CASE WHEN json_valid(CAST(payload AS TEXT)) THEN json_extract(CAST(payload AS TEXT), '$.tokenUsage.threadID') END, position)+ WHERE kind = 'tokenUsage'+ """)+ try FileManager.default.setAttributes([.posixPermissions: 0o600], ofItemAtPath: url.path)+ } catch {+ sqlite3_close(connection)+ connection = nil+ throw error+ }+ }++ deinit {+ sqlite3_close(connection)+ }++ func transaction<T>(isolation _: isolated (any Actor)? = #isolation, _ body: () throws -> T) throws -> T {+ try execute("BEGIN IMMEDIATE")+ do {+ let value = try body()+ try Task.checkCancellation()+ try execute("COMMIT")+ return value+ } catch {+ sqlite3_exec(connection, "ROLLBACK", nil, nil, nil)+ throw error+ }+ }++ @discardableResult+ func append(_ record: AppServerEventRecord) throws -> Bool {+ try transaction { try insert(record) }+ }++ @discardableResult+ private func insert(_ record: AppServerEventRecord) throws -> Bool {+ let date = HistoryStorage.dateKey(for: record.activityData?.timestamp ?? record.tokenUsage?.aggregationDate ?? record.recordedAt)+ let data = try JSONLines.stableEncoder.encode(record)+ guard data.count <= 1024 * 1024 else { throw StorageCompatibilityError.incompleteSource }+ try execute("INSERT OR IGNORE INTO days(date) VALUES (?)", [.text(date)])+ try execute(+ "INSERT OR IGNORE INTO events(identity, date, kind, payload) VALUES (?, ?, ?, ?)",+ [record.deduplicationID.map(Value.text) ?? .null, .text(date), .text(record.kind.rawValue), .data(data)]+ )+ return sqlite3_changes(connection) > 0+ }++ func appendTokenUsage(_ sample: TokenUsageSample) throws {+ try transaction {+ var exists = false+ try query("SELECT 1 FROM events WHERE identity = ?", [.text("tokenUsage:" + sample.id)]) { _ in exists = true }+ guard !exists else { return }+ var latest: TokenUsageObservation?+ try query("""+ SELECT payload FROM events+ WHERE kind = 'tokenUsage' AND CASE WHEN json_valid(CAST(payload AS TEXT)) THEN json_extract(CAST(payload AS TEXT), '$.tokenUsage.threadID') END = ?+ ORDER BY position DESC LIMIT 1+ """, [.text(sample.threadID)]) {+ latest = try AppServerEventRecord.decode(from: Self.data($0, at: 0)).tokenUsage+ }+ guard latest?.current.total != sample.current.total || latest?.turnID != sample.turnID else { return }+ // 不可比较的累计值只建立新基线, 不将上下文估算或回退当成消耗+ var previous = latest?.current.total ?? sample.current.total+ if sample.current.total.subtractingCounters(previous) == nil {+ previous = sample.current.total+ }+ guard sample.current.total.subtractingCounters(previous) != nil else { return }+ let observation = TokenUsageObservation(+ id: sample.id, observedAt: sample.observedAt, threadID: sample.threadID, turnID: sample.turnID,+ turnStartedAt: sample.turnStartedAt, previous: previous, current: sample.current+ )+ try insert(AppServerEventRecord(tokenUsage: observation, recordedAt: sample.observedAt))+ }+ }++ func tokenUsageSnapshot(_ previous: TokenUsageThreadSnapshot) throws -> TokenUsageThreadSnapshot {+ var snapshot = previous+ try query("""+ SELECT position, payload FROM events+ WHERE kind = 'tokenUsage' AND CASE WHEN json_valid(CAST(payload AS TEXT)) THEN json_extract(CAST(payload AS TEXT), '$.tokenUsage.threadID') END = ? AND position > ?+ ORDER BY position+ """, [.text(previous.threadID), .integer(previous.position)]) { row in+ guard let observation = try AppServerEventRecord.decode(from: Self.data(row, at: 1)).tokenUsage,+ let turn = observation.turnID, let delta = observation.delta,+ let total = (snapshot.turns[turn] ?? .zero).adding(delta), total.isValid else {+ throw StorageCompatibilityError.incompleteSource+ }+ // 首次非零累计值只建立比较基线, 不代表本轮已确认消耗为零+ let isInitialBaseline = snapshot.position == 0 && delta == .zero && observation.current.total != .zero+ if !isInitialBaseline {+ snapshot.turns[turn] = total+ }+ snapshot.position = sqlite3_column_int64(row, 0)+ }+ return snapshot+ }++ func appendActivityData(_ events: [ActivityDataRecord]) throws {+ try transaction {+ for event in events where event.id != nil {+ try insert(AppServerEventRecord(activityData: event))+ }+ }+ }++ func dates() throws -> [String] {+ var result: [String] = []+ try query("SELECT date FROM days ORDER BY date") { result.append(Self.text($0, at: 0)) }+ return result+ }++ func entries(after position: Int64 = 0, date: String? = nil, kind: AppServerEventRecord.Kind? = nil, consume: (Entry) throws -> Void) throws {+ let sql = "SELECT position, payload, kind FROM events WHERE position > ?" + (date == nil ? "" : " AND date = ?")+ + (kind == nil ? "" : " AND kind = ?") + " ORDER BY position"+ var values: [Value] = [.integer(position)]+ if let date {+ values.append(.text(date))+ }+ if let kind {+ values.append(.text(kind.rawValue))+ }+ try query(sql, values) { row in+ let record = try AppServerEventRecord.decode(from: Self.data(row, at: 1))+ guard record.kind.rawValue == Self.text(row, at: 2) else { throw StorageCompatibilityError.incompleteSource }+ try consume(Entry(position: sqlite3_column_int64(row, 0), record: record))+ }+ }++ func activityDataAggregates() throws -> [ActivityDataAggregate] {+ var values: [ActivityDataAggregate] = []+ try query("SELECT activityData FROM aggregates WHERE activityData IS NOT NULL ORDER BY date") {+ if let value = try? JSONLines.decoder.decode(ActivityDataAggregate.self, from: Self.data($0, at: 0)) {+ values.append(value)+ }+ }+ return values+ }++ func pendingActivityDataDates() throws -> [String] {+ var dates: [String] = []+ try query("""+ SELECT days.date, aggregates.activityData,+ (SELECT COALESCE(MAX(position), 0) FROM events WHERE events.date = days.date AND events.kind = 'activityData')+ FROM days+ LEFT JOIN aggregates ON aggregates.date = days.date+ ORDER BY days.date+ """) { row in+ let aggregate = try? JSONLines.decoder.decode(ActivityDataAggregate.self, from: Self.data(row, at: 1))+ if let aggregate, aggregate.aggregationVersion == AggregationVersion.activityData,+ aggregate.position >= sqlite3_column_int64(row, 2) {+ return+ }+ dates.append(Self.text(row, at: 0))+ }+ return dates+ }++ /// 聚合和源位置在同一事务提交, 失败不会留下已推进的检查点+ func aggregateActivityData(date: String, rebuilding: Bool = false, now: Date = Date()) throws -> ActivityDataAggregate {+ try transaction {+ var hasSource = false+ try query("SELECT 1 FROM days WHERE date = ?", [.text(date)]) { _ in hasSource = true }+ guard hasSource else { throw HistoryDataRebuildError.sourceUnavailable }+ var base: ActivityDataAggregate?+ var position: Int64 = 0+ if !rebuilding {+ try query("SELECT activityData FROM aggregates WHERE date = ?", [.text(date)]) { row in+ guard let aggregate = try? JSONLines.decoder.decode(ActivityDataAggregate.self, from: Self.data(row, at: 0)),+ aggregate.aggregationVersion == AggregationVersion.activityData else { return }+ base = aggregate+ position = aggregate.position+ }+ }+ var accumulator = base.map { ActivityDataAccumulator(appending: $0) }+ ?? ActivityDataAccumulator(rebuilding: date)+ if let base, !base.supportsIncrementalAggregation {+ try entries(date: date, kind: .activityData) { entry in+ if entry.position <= position, let event = entry.record.activityData {+ accumulator.restoreIdentity(from: event)+ }+ }+ }+ try entries(after: position, date: date, kind: .activityData) { entry in+ if let event = entry.record.activityData {+ accumulator.record(event)+ }+ position = entry.position+ }+ let cutoff = HistoryStorage.dateKey(for: HistoryStorage.identifierRetentionCutoffDate(today: now))+ var aggregate = accumulator.finalized(identifierStorage: date >= cutoff ? .retained : .compacted)+ aggregate.position = position+ try execute("""+ INSERT INTO aggregates(date, activityData) VALUES (?, ?)+ ON CONFLICT(date) DO UPDATE SET activityData = excluded.activityData+ """, [.text(date), .data(JSONLines.stableEncoder.encode(aggregate))])+ return aggregate+ }+ }++ func tokenUsageDates() throws -> [String] {+ var result: [String] = []+ try query("SELECT DISTINCT date FROM events WHERE kind = 'tokenUsage' ORDER BY date") { result.append(Self.text($0, at: 0)) }+ return result+ }++ func tokenUsageAggregates() throws -> [TokenUsageAggregate] {+ var result: [TokenUsageAggregate] = []+ try query("SELECT tokenUsage FROM aggregates WHERE tokenUsage IS NOT NULL ORDER BY date") {+ if let value = try? JSONLines.decoder.decode(TokenUsageAggregate.self, from: Self.data($0, at: 0)) {+ result.append(value)+ }+ }+ return result+ }++ func pendingTokenUsageDates() throws -> [String] {+ var dates: [String] = []+ try query("""+ SELECT days.date, aggregates.tokenUsage,+ (SELECT COALESCE(MAX(position), 0) FROM events WHERE events.date = days.date AND events.kind = 'tokenUsage')+ FROM days+ LEFT JOIN aggregates ON aggregates.date = days.date+ WHERE aggregates.tokenUsage IS NOT NULL+ OR EXISTS (SELECT 1 FROM events WHERE events.date = days.date AND events.kind = 'tokenUsage')+ ORDER BY days.date+ """) { row in+ let aggregate = try? JSONLines.decoder.decode(TokenUsageAggregate.self, from: Self.data(row, at: 1))+ if let aggregate, aggregate.aggregationVersion == AggregationVersion.tokenUsage,+ aggregate.position >= sqlite3_column_int64(row, 2) {+ return+ }+ dates.append(Self.text(row, at: 0))+ }+ return dates+ }++ /// 日期结果和读取位置共同提交, 重放失败时保留之前的日总量+ func aggregateTokenUsage(date: String, rebuilding: Bool = false) throws -> TokenUsageAggregate {+ try transaction {+ var baseline: TokenUsageAggregate?+ var position: Int64 = 0+ try query("SELECT tokenUsage FROM aggregates WHERE date = ?", [.text(date)]) {+ guard let aggregate = try? JSONLines.decoder.decode(TokenUsageAggregate.self, from: Self.data($0, at: 0)),+ aggregate.aggregationVersion == AggregationVersion.tokenUsage else { return }+ baseline = aggregate+ if !rebuilding {+ position = aggregate.position+ }+ }+ var aggregate = rebuilding ? TokenUsageAggregate(date: date) : baseline ?? TokenUsageAggregate(date: date)+ try entries(after: position, date: date, kind: .tokenUsage) { entry in+ guard let observation = entry.record.tokenUsage, !observation.id.isEmpty,+ HistoryStorage.dateKey(for: observation.aggregationDate) == date else {+ throw StorageCompatibilityError.incompleteSource+ }+ try aggregate.record(observation)+ position = entry.position+ }+ guard aggregate.eventCount > 0,+ !rebuilding || aggregate.eventCount >= (baseline?.eventCount ?? 0) else {+ throw StorageCompatibilityError.incompleteSource+ }+ aggregate.position = position+ try execute("""+ INSERT INTO aggregates(date, tokenUsage) VALUES (?, ?)+ ON CONFLICT(date) DO UPDATE SET tokenUsage = excluded.tokenUsage+ """, [.text(date), .data(JSONLines.stableEncoder.encode(aggregate))])+ return aggregate+ }+ }++ func prune(now: Date) throws -> Int {+ let cutoff = HistoryStorage.dateKey(for: HistoryStorage.retentionCutoffDate(today: now))+ return try transaction {+ try execute("DELETE FROM days WHERE date < ?", [.text(cutoff)])+ let removed = Int(sqlite3_changes(connection))+ for aggregate in try activityDataAggregates() {+ guard let normalized = ActivityDataAggregate.normalized(aggregates: [aggregate], today: now).first,+ normalized != aggregate else { continue }+ try execute("UPDATE aggregates SET activityData = ? WHERE date = ?", [.data(JSONLines.stableEncoder.encode(normalized)), .text(aggregate.date)])+ }+ return removed+ }+ }++ func execute(_ sql: String, _ values: [Value] = []) throws {+ try query(sql, values) { _ in }+ }++ private func scalar(_ sql: String) throws -> Int64 {+ var value: Int64 = 0+ try query(sql) { value = sqlite3_column_int64($0, 0) }+ return value+ }++ private func query(_ sql: String, _ values: [Value] = [], consume: (OpaquePointer) throws -> Void) throws {+ try Task.checkCancellation()+ var statement: OpaquePointer?+ let prepared = sqlite3_prepare_v2(connection, sql, -1, &statement, nil)+ guard prepared == SQLITE_OK, let statement else { throw failure(prepared) }+ defer { sqlite3_finalize(statement) }+ for (index, value) in values.enumerated() {+ let parameter = Int32(index + 1)+ let bound: Int32 = switch value {+ case let .integer(value): sqlite3_bind_int64(statement, parameter, value)+ case let .text(value): sqlite3_bind_text(statement, parameter, value, -1, Self.transient)+ case let .data(value): value.withUnsafeBytes { sqlite3_bind_blob(statement, parameter, $0.baseAddress, Int32($0.count), Self.transient) }+ case .null: sqlite3_bind_null(statement, parameter)+ }+ guard bound == SQLITE_OK else { throw failure(bound) }+ }+ while true {+ try Task.checkCancellation()+ let step = sqlite3_step(statement)+ if step == SQLITE_DONE {+ return+ }+ guard step == SQLITE_ROW else { throw failure(step) }+ try consume(statement)+ }+ }++ private func failure(_ code: Int32) -> Failure {+ Failure(code: code, message: connection.map { String(cString: sqlite3_errmsg($0)) } ?? "Cannot open history database")+ }++ private static func text(_ statement: OpaquePointer, at index: Int32) -> String {+ sqlite3_column_text(statement, index).map { String(cString: $0) } ?? ""+ }++ private static func data(_ statement: OpaquePointer, at index: Int32) -> Data {+ guard let bytes = sqlite3_column_blob(statement, index) else { return Data() }+ return Data(bytes: bytes, count: Int(sqlite3_column_bytes(statement, index)))+ }+}diff --git a/CodexBar/Services/History/HistoryMaintenance.swift b/CodexBar/Services/History/HistoryMaintenance.swiftdeleted file mode 100644index 594d224..0000000--- a/CodexBar/Services/History/HistoryMaintenance.swift+++ /dev/null@@ -1,153 +0,0 @@-import Foundation--/// State/aggregation.json 的全局状态, pending 表示可增量, dirty 表示需全量重建-nonisolated struct HistoryMaintenanceState: Codable, Equatable {- private enum CodingKeys: String, CodingKey {- case version- case pending- case dirty- case days- }-- /// 维护状态的格式版本, 与每条聚合实际使用的算法版本独立- static let currentVersion = 1-- var version: Int- var pending: [String]- var dirty: [String]- var days: [String: HistoryDayMaintenanceState]-- init(- version: Int = Self.currentVersion,- pending: [String] = [],- dirty: [String] = [],- days: [String: HistoryDayMaintenanceState] = [:]- ) {- self.version = version- self.pending = Self.normalizedDates(pending)- self.dirty = Self.normalizedDates(dirty)- self.days = days- }-- init(from decoder: Decoder) throws {- let container = try decoder.container(keyedBy: CodingKeys.self)- version = try container.decode(Int.self, forKey: .version)- try StorageVersion.require(version, current: Self.currentVersion, name: "AggregationState")- pending = try Self.normalizedDates(container.decodeIfPresent([String].self, forKey: .pending) ?? [])- dirty = try Self.normalizedDates(container.decodeIfPresent([String].self, forKey: .dirty) ?? [])- days = try container.decodeIfPresent([String: HistoryDayMaintenanceState].self, forKey: .days) ?? [:]- }-- /// 返回状态是否有实际变化, 便于调用方跳过无谓的落盘- @discardableResult- mutating func markPending(_ dateKey: String) -> Bool {- let newPending = Self.inserting(dateKey, into: pending)- let changed = newPending != pending || days[dateKey] == nil- pending = newPending- ensureDayState(for: dateKey)- return changed- }-- @discardableResult- mutating func markDirty(_ dateKey: String) -> Bool {- markDirty(contentsOf: [dateKey])- }-- /// 批量标脏一次性归一化, 避免逐个插入的重复排序与校验- @discardableResult- mutating func markDirty(contentsOf dateKeys: [String]) -> Bool {- guard !dateKeys.isEmpty else {- return false- }-- let newDirty = Self.normalizedDates(dirty + dateKeys)- var changed = newDirty != dirty- dirty = newDirty-- for dateKey in dateKeys where days[dateKey] == nil {- days[dateKey] = HistoryDayMaintenanceState()- changed = true- }-- return changed- }-- mutating func removePending(_ dateKey: String) {- pending.removeAll { $0 == dateKey }- }-- mutating func removeDirty(_ dateKey: String) {- dirty.removeAll { $0 == dateKey }- }-- mutating func remove(_ dateKey: String) {- removePending(dateKey)- removeDirty(dateKey)- days.removeValue(forKey: dateKey)- }-- mutating func normalize() -> Bool {- let previousPending = pending- let previousDirty = dirty- let previousDays = days-- pending = Self.normalizedDates(pending)- dirty = Self.normalizedDates(dirty)- days = days.filter { HistoryStorage.isValidDateKey($0.key) }-- return previousPending != pending || previousDirty != dirty || previousDays != days- }-- private mutating func ensureDayState(for dateKey: String) {- if days[dateKey] == nil {- days[dateKey] = HistoryDayMaintenanceState()- }- }-- private static func inserting(_ dateKey: String, into dates: [String]) -> [String] {- normalizedDates(dates + [dateKey])- }-- private static func normalizedDates(_ dates: [String]) -> [String] {- Set(dates.filter(HistoryStorage.isValidDateKey)).sorted()- }-}--nonisolated struct HistoryDayMaintenanceState: Codable, Equatable {- private enum CodingKeys: String, CodingKey {- case offset- case size- case corrupt- case generationID- case fileIdentifier- }-- var offset: UInt64- var size: UInt64- var corrupt: Int- var generationID: String?- var fileIdentifier: UInt64?-- init(- offset: UInt64 = 0,- size: UInt64 = 0,- corrupt: Int = 0,- generationID: String? = nil,- fileIdentifier: UInt64? = nil- ) {- self.offset = offset- self.size = size- self.corrupt = corrupt- self.generationID = generationID- self.fileIdentifier = fileIdentifier- }-- init(from decoder: Decoder) throws {- let container = try decoder.container(keyedBy: CodingKeys.self)- offset = try container.decodeIfPresent(UInt64.self, forKey: .offset) ?? 0- size = try container.decodeIfPresent(UInt64.self, forKey: .size) ?? 0- corrupt = try container.decodeIfPresent(Int.self, forKey: .corrupt) ?? 0- generationID = try container.decodeIfPresent(String.self, forKey: .generationID)- fileIdentifier = try container.decodeIfPresent(UInt64.self, forKey: .fileIdentifier)- }-}diff --git a/CodexBar/Services/History/HistoryService.swift b/CodexBar/Services/History/HistoryService.swiftindex 98f7c2b..9c71524 100644--- a/CodexBar/Services/History/HistoryService.swift+++ b/CodexBar/Services/History/HistoryService.swift@@ -1,15 +1,6 @@import Foundationimport os-/// 汇总失败时定位到哪一步, 只用于日志-private nonisolated enum MaintenanceStage: String {- case prepare- case write- case prune-}--/// 一轮汇总的计数, 成功路径压进 SyncScheduler 的收尾那一条日志, 不为每天单独记一行-/// dates 从各处理结果的计数求和得出, 不额外保存nonisolated struct HistoryMaintenanceCounts {var events = 0var written = 0@@ -30,942 +21,138 @@ nonisolated struct HistoryMaintenanceCounts {}}-/// 从 归一化活动事件维护每日聚合, 对外只发布 UI 需要的统计快照+/// 原始事件与聚合进度由数据库事务维护, 同步只读取已提交的聚合actor HistoryService {private let directoryURL: URL- private let eventsDirectoryURL: URL- private let dailyLogURL: URLprivate let syncService: SyncService- private let tokenHistory: TokenHistoryStore- /// 上次归一化时 Aggregates/activity.jsonl 的 stat 与当天日期键- private var lastNormalizedDailyLog: HistoryDailyLogStamp?- /// 上一条维护日志之后连续空转的轮数, 记出去就清零+ private let tokenUsageHistory: TokenUsageHistoryStoreprivate var idleMaintenanceRounds = 0- private var verifiedBoundaries: [String: HistoryBoundaryVerification] = [:]private var lastRetentionCutoff: Date?- init(- directoryURL: URL = HistoryStorage.directoryURL(),- syncService: SyncService? = nil,- tokenHistory: TokenHistoryStore? = nil- ) {+ init(directoryURL: URL = HistoryStorage.directoryURL(), syncService: SyncService? = nil, tokenUsageHistory: TokenUsageHistoryStore? = nil) {self.directoryURL = directoryURL- eventsDirectoryURL = HistoryStorage.eventsDirectoryURL(in: directoryURL)- dailyLogURL = HistoryStorage.dailyURL(in: directoryURL)self.syncService = syncService ?? SyncService(directoryURL: HistoryStorage.syncDirectoryURL(in: directoryURL))- self.tokenHistory = tokenHistory ?? TokenHistoryStore(directoryURL: directoryURL)- }-- // MARK: - 快照读取-- func loadSnapshot(- synchronize: Bool = false,- trigger: LogTrigger = .auto- ) async -> HistorySnapshot {- let decoded = try? loadDailyAggregatesWithFailures()- var snapshot = await makeSnapshot(- localAggregates: decoded?.compatibilityError == nil ? ActivityAggregate.normalized(aggregates: decoded?.values ?? []) : [],- synchronize: synchronize,- trigger: trigger- )- snapshot.isActivityComplete = snapshot.isActivityComplete && decoded != nil- && decoded?.compatibilityError == nil && decoded?.failedLineCount == 0- return snapshot- }-- /// 先跑一轮维护再取快照- /// counts 为 nil 表示这一轮空转, 由调用方决定记不记日志- func loadSnapshotWithMaintenance(- synchronize: Bool,- trigger: LogTrigger,- now: Date = Date()- ) async -> (snapshot: HistorySnapshot, counts: HistoryMaintenanceCounts?) {- await syncService.pruneLocalCaches(now: now)- let counts = performMaintenanceIfNeeded(now: now)- let snapshot = await loadSnapshot(synchronize: synchronize, trigger: trigger)- return (snapshot, counts)+ self.tokenUsageHistory = tokenUsageHistory ?? TokenUsageHistoryStore(directoryURL: directoryURL)}- private func makeSnapshot(- localAggregates: [ActivityAggregate],- synchronize: Bool,- trigger: LogTrigger,- localTokenTurns: [TokenTurn]? = nil- ) async -> HistorySnapshot {- let tokenTurns: [TokenTurn]- if let localTokenTurns {- tokenTurns = localTokenTurns- } else {- do {- let baseline = await syncService.tokenRecoveryBaselineIfEnabled()- tokenTurns = try await tokenHistory.refresh(baseline: baseline)- } catch {- let error = error as NSError- AppLog.history.error("Token 历史读取失败: domain=\(error.domain, privacy: .public) code=\(error.code)")- tokenTurns = await (try? tokenHistory.persistedTurns()) ?? []- }- }- let maintenance = try? HistoryStorage.loadMaintenanceState(in: directoryURL)- let uploadable = localAggregates.filter { aggregate in- guard let state = maintenance, !state.dirty.contains(aggregate.date),- let day = state.days[aggregate.date] else { return false }- return day.generationID == aggregate.generationID && day.offset == aggregate.sourceCheckpoint?.byteCount- }- let syncSnapshot: SyncSnapshot = if synchronize {- await syncService.synchronizeIfEnabled(- localAggregates: uploadable, localTokenTurns: tokenTurns,- recoveredTokenIDs: tokenHistory.pendingRecoveryIDs(), trigger: trigger- )- } else {- await syncService.snapshotFromCacheIfEnabled(localTokenTurns: tokenTurns)- }- var snapshot = HistorySnapshot(- localAggregates: localAggregates,- syncedRecords: syncSnapshot.records,- currentDeviceID: syncSnapshot.currentDeviceID- )- snapshot.tokenUsageByDate = syncSnapshot.tokenUsageByDate ?? TokenTurn.dailyUsage(tokenTurns)- snapshot.isActivityComplete = snapshot.isActivityComplete && syncSnapshot.isActivityComplete && maintenance != nil- let knownDates = Set(localAggregates.map(\.date)).union(- syncSnapshot.records.filter { $0.deviceID == syncSnapshot.currentDeviceID }.map(\.date)- )- snapshot.unavailableActivityDates.formUnion(Set(maintenance?.dirty ?? []).subtracting(knownDates))- return snapshot- }-- /// 从指定日期的原始事件重建, 同一来源继续使用同一云端记录- func rebuildData(- for dateKeys: [String],- synchronize: Bool- ) async throws -> HistoryDataRebuildOutcome {- let duration = LogDuration()- let normalizedDateKeys = Set(dateKeys).sorted()- guard !normalizedDateKeys.isEmpty else {- throw HistoryDataRebuildError.sourceUnavailable- }-- // 已保存请求的失败日期交给常规维护重试, 单日失败不中止整批- // 中止会让「前几天已改写, 后几天没碰」这个事实完全不被上报- var rebuildResults = [HistoryMaintenanceResult]()- var failedDateKeys = [String]()- var failedRequestDateKeys = [String]()- var firstFailure: Error?- let eventDateKeysWithData = normalizedDateKeys.filter { HistoryStorage.fileSize(at: eventLogURL(for: $0)) > 0 }- for dateKey in eventDateKeysWithData {- var requestWasSaved = false- do {- try rebuildResults.append(rebuildLocalData(for: dateKey, requestWasSaved: &requestWasSaved))- } catch {- // 整批成功时这个原因不会往上抛, 摘要只带得走日期- let details = LogFields.joined(- "stage=date",- "date=\(dateKey)",- "detail=\(error.localizedDescription)"- )- AppLog.history.error("数据重建失败: \(details, privacy: .public)")- failedDateKeys.append(dateKey)- if !requestWasSaved {- failedRequestDateKeys.append(dateKey)- }- firstFailure = firstFailure ?? error- }- }-- var tokenResult = TokenRebuildResult.empty- var didFailTokenRebuild = false+ func loadSnapshot(synchronize: Bool = false, trigger: LogTrigger = .auto) async -> HistorySnapshot {+ var aggregates: [ActivityDataAggregate] = []+ var pending = Set<String>()+ var readable = truedo {- tokenResult = try await tokenHistory.rebuild(for: normalizedDateKeys)- if !tokenResult.failedDateKeys.isEmpty {- firstFailure = firstFailure ?? TokenCacheError.incompleteJournal+ let database = try HistoryDatabase(directoryURL: directoryURL)+ try database.transaction {+ aggregates = try database.activityDataAggregates()+ pending = try Set(database.pendingActivityDataDates())}} catch {- didFailTokenRebuild = true- firstFailure = firstFailure ?? error- let error = error as NSError- AppLog.history.error("Token 重建失败: domain=\(error.domain, privacy: .public) code=\(error.code)")+ readable = false+ AppLog.history.error("活动历史读取失败: \(error.localizedDescription, privacy: .public)")}-- // 一天都没成功才算整体失败, 并保留首个真实原因而非笼统报「数据发生变化」- guard !rebuildResults.isEmpty || !tokenResult.dateKeys.isEmpty else {- throw firstFailure ?? HistoryDataRebuildError.sourceUnavailable- }-- // 重建只由设置页的用户操作发起- let localTurns = if let turns = tokenResult.turns {- turns- } else {- await (try? tokenHistory.currentTurns()) ?? []- }- let snapshot = await makeSnapshot(- localAggregates: loadDailyAggregates() ?? [],- synchronize: synchronize,- trigger: .manual,- localTokenTurns: localTurns- )- let tokenSyncPending = await syncService.hasPendingTokenUpdates(local: localTurns)- let activitySyncPending = await syncService.hasPendingActivityUpdates(local: loadDailyAggregates() ?? [])- let tokenFailures = didFailTokenRebuild ? normalizedDateKeys : tokenResult.failedDateKeys- let summary = HistoryDataRebuildSummary(- rebuiltDateCount: Set(rebuildResults.map(\.dateKey)).union(tokenResult.dateKeys)- .subtracting(failedDateKeys).subtracting(tokenFailures).count,- eventCount: rebuildResults.reduce(0) { $0 + ($1.aggregate.eventCount ?? 0) },- isSyncPending: activitySyncPending || tokenSyncPending,- failedDateKeys: failedDateKeys,- failedRequestDateKeys: failedRequestDateKeys,- failedTokenDateKeys: tokenFailures,- tokenTurnCount: tokenResult.turnCount,- didFailTokenRebuild: didFailTokenRebuild || !tokenResult.failedDateKeys.isEmpty- )- let elapsed = duration.elapsed- let details = LogFields.joined(- "dates=\(summary.rebuiltDateCount)",- "events=\(summary.eventCount)",- "tokenTurns=\(summary.tokenTurnCount)",- "failedDates=\(failedDateKeys.count)",- "elapsed=\(elapsed)"- )- AppLog.history.notice("数据重建完成: \(details, privacy: .public)")- return HistoryDataRebuildOutcome(snapshot: snapshot, summary: summary)- }-- private func loadDailyAggregates() -> [ActivityAggregate]? {- guard let decoded = try? loadDailyAggregatesWithFailures(), decoded.compatibilityError == nil else { return nil }- return ActivityAggregate.normalized(aggregates: decoded.values)- }-- // MARK: - 重建与维护调度-- private func rebuildLocalData(for dateKey: String, requestWasSaved: inout Bool) throws -> HistoryMaintenanceResult {- var aggregates = loadDailyAggregates() ?? []- let eventCountAvailability = aggregates- .first(where: { $0.date == dateKey })?- .eventCountAvailability ?? .all- let task = try prepareRebuildTask(- for: dateKey,- eventCountAvailability: eventCountAvailability- )- requestWasSaved = true-+ let tokenUsageSnapshot: TokenUsageHistorySnapshotdo {- let result = try buildDailyAggregate(for: task)- guard try commit(result, aggregates: &aggregates),- rebuildWasCommitted(result) else {- throw HistoryDataRebuildError.sourceChanged- }- return result+ tokenUsageSnapshot = try await tokenUsageHistory.loadSnapshot()} catch {- markDirty(dateKey)- throw error- }- }-- private func prepareRebuildTask(- for dateKey: String,- eventCountAvailability: ActivityCountAvailability- ) throws -> HistoryMaintenanceTask {- let retentionCutoffKey = HistoryStorage.dateKey(for: HistoryStorage.retentionCutoffDate())- guard HistoryStorage.isValidDateKey(dateKey), dateKey >= retentionCutoffKey else {- throw HistoryDataRebuildError.sourceUnavailable- }-- return try HistoryStorage.withExclusiveLock(in: directoryURL) {- guard let stat = HistoryStorage.fileStat(at: eventLogURL(for: dateKey)),- stat.size > 0 else {- throw HistoryDataRebuildError.sourceUnavailable- }-- var state = try HistoryStorage.loadMaintenanceState(in: directoryURL)- let header = try AppServerEventJournal.header(at: eventLogURL(for: dateKey))- guard header.date == dateKey else { throw StorageCompatibilityError.sourceConflict }- if state.days[dateKey]?.generationID != header.generationID {- state.days[dateKey] = HistoryDayMaintenanceState(generationID: header.generationID, fileIdentifier: stat.identifier)- }- state.markDirty(dateKey)- try HistoryStorage.saveMaintenanceState(state, in: directoryURL)- guard let day = state.days[dateKey] else { throw HistoryDataRebuildError.sourceUnavailable }- return dirtyTask(for: dateKey, day: day, size: stat.size, eventCountAvailability: eventCountAvailability)+ tokenUsageSnapshot = await TokenUsageHistorySnapshot(aggregates: (try? tokenUsageHistory.persistedAggregates()) ?? [], coverage: .unavailable)+ AppLog.history.error("Token 历史读取失败: \(error.localizedDescription, privacy: .public)")}- }-- private func rebuildWasCommitted(_ result: HistoryMaintenanceResult) -> Bool {- let dailyGeneration = loadDailyAggregates()?- .first(where: { $0.date == result.dateKey })?- .generationID- guard dailyGeneration == result.aggregate.generationID else {- return false+ let tokenUsageAggregates = tokenUsageSnapshot.aggregates+ let reliable = tokenUsageSnapshot.coverage.isComplete+ let sync: SyncSnapshot = if synchronize {+ await syncService.synchronizeIfEnabled(+ localActivityDataAggregates: aggregates.filter { !pending.contains($0.date) }, localTokenUsageAggregates: tokenUsageAggregates,+ isTokenUsageSourceComplete: reliable, trigger: trigger+ )+ } else {+ await syncService.snapshotFromCacheIfEnabled(localTokenUsageAggregates: reliable ? tokenUsageAggregates : [])}-- return (try? HistoryStorage.withExclusiveLock(in: directoryURL) {- guard let day = try HistoryStorage.loadMaintenanceState(in: directoryURL).days[result.dateKey] else {- return false- }- return day.generationID == result.aggregate.generationID- && day.offset == result.size- }) ?? false+ var snapshot = HistorySnapshot(localActivityDataAggregates: aggregates, syncedActivityDataRecords: sync.activityData, currentDeviceID: sync.currentDeviceID)+ snapshot.tokenUsageByDate = sync.tokenUsageByDate ?? TokenUsageAggregate.tokenUsageByDate(tokenUsageAggregates)+ snapshot.isActivityDataComplete = snapshot.isActivityDataComplete && readable && sync.isActivityDataComplete+ let known = Set(aggregates.map(\.date)).union(sync.activityData.filter { $0.deviceID == sync.currentDeviceID }.map(\.date))+ snapshot.unavailableActivityDataDates.formUnion(pending.subtracting(known))+ return snapshot}- /// 空转的一轮返回 nil- /// 维护跟随额度刷新执行, 只记录有实际工作的轮次, 避免空闲时持续产生重复日志- /// 收尾日志由 SyncScheduler 统一记, 这里只负责判断有没有值得记的东西- private func performMaintenanceIfNeeded(now: Date) -> HistoryMaintenanceCounts? {- let duration = LogDuration()+ func loadSnapshotWithMaintenance(+ synchronize: Bool, trigger: LogTrigger, now: Date = Date()+ ) async -> (snapshot: HistorySnapshot, counts: HistoryMaintenanceCounts?) {+ await syncService.pruneLocalCaches(now: now)var counts = HistoryMaintenanceCounts()- var stage = MaintenanceStage.preparedo {- stage = .prune- counts.pruned = try pruneExpiredEventFiles(now: now)- stage = .prepare- let tasks = try prepareMaintenanceTasks()- stage = .write- let didCommitDailyLog = perform(tasks, counts: &counts)- // 成功提交前已整体归一化, 没有完整提交时再做一次稳态检查- if !didCommitDailyLog {- try normalizeDailyAggregatesIfNeeded()- }- } catch {- let elapsed = duration.elapsed- let details = LogFields.joined(- "stage=\(stage.rawValue)",- "elapsed=\(elapsed)",- "detail=\(error.localizedDescription)"- )- AppLog.history.error("事件汇总失败: \(details, privacy: .public)")- idleMaintenanceRounds = 0- return nil- }-- guard counts.hasWork else {- idleMaintenanceRounds += 1- return nil- }-- counts.idle = idleMaintenanceRounds- idleMaintenanceRounds = 0- return counts- }-- /// 返回是否至少完整提交一个聚合与维护状态- /// 批次内共享同一份内存聚合, 避免每个任务重新读盘- private func perform(- _ tasks: [HistoryMaintenanceTask],- counts: inout HistoryMaintenanceCounts- ) -> Bool {- // 任务是 dirty 与 pending 两段拼接, 只在各自半边有序, 取首尾会得到反向区间- let dateKeys = tasks.map(\.dateKey)- guard let oldestDateKey = dateKeys.min(),- let newestDateKey = dateKeys.max() else {- return false- }-- counts.dateRange = oldestDateKey == newestDateKey- ? oldestDateKey- : oldestDateKey + ".." + newestDateKey-- var aggregates = loadDailyAggregates() ?? []- var didCommit = false- for task in tasks {- do {- let result = try buildDailyAggregate(for: task)- if try commit(result, aggregates: &aggregates) {- didCommit = true+ let database = try HistoryDatabase(directoryURL: directoryURL)+ let cutoff = HistoryStorage.retentionCutoffDate(today: now)+ if lastRetentionCutoff != cutoff {+ counts.pruned = try database.prune(now: now)+ lastRetentionCutoff = cutoff+ }+ let dates = try database.pendingActivityDataDates()+ counts.dateRange = dates.first.map { $0 == dates.last ? $0 : $0 + ".." + (dates.last ?? $0) } ?? "-"+ let previous = try database.activityDataAggregates().reduce(into: [String: Int]()) { $0[$1.date] = $1.eventCount ?? 0 }+ for date in dates {+ do {+ let aggregate = try database.aggregateActivityData(date: date, now: now)+ counts.events += (aggregate.eventCount ?? 0) - (previous[date] ?? 0)counts.written += 1- // 记本轮新摄入的量; 累加 eventCount 会变成当日总数, 事件停写也看不出来- // dirty 任务没有 base, 差值即整天重算的量, 与"这轮处理了多少"仍然一致- counts.events += (result.aggregate.eventCount ?? 0) - task.baseEventCount- } else {- counts.skipped += 1- }- } catch {- counts.failed += 1- let details = LogFields.joined(- "stage=daily",- "date=\(task.dateKey)",- "detail=\(error.localizedDescription)",- "action=retry"- )- AppLog.history.error("事件汇总失败: \(details, privacy: .public)")- if case .rebuild = task.mode {- markDirty(task.dateKey)+ } catch {+ counts.failed += 1+ AppLog.history.error("活动聚合失败: date=\(date, privacy: .public) detail=\(error.localizedDescription, privacy: .public)")}}+ } catch {+ counts.failed += 1+ AppLog.history.error("历史维护失败: \(error.localizedDescription, privacy: .public)")}-- return didCommit- }-- private func prepareMaintenanceTasks() throws -> [HistoryMaintenanceTask] {- let eventDateKeys = try eventDateKeys()- let dailyDecodeResult = try loadDailyAggregatesWithFailures()- if let error = dailyDecodeResult.compatibilityError {- throw error- }-- return try HistoryStorage.withExclusiveLock(in: directoryURL) {- var state = try HistoryStorage.loadMaintenanceState(in: directoryURL)- var changedState = state.normalize()- let dailyByDate = dailyDecodeResult.values.reduce(into: [String: ActivityAggregate]()) { result, aggregate in- result[aggregate.date] = aggregate- }-- let changedByRebuild = markRebuildDates(- eventDateKeys: eventDateKeys,- dailyByDate: dailyByDate,- state: &state- )- let changedByReconcile = try reconcileEventFiles(eventDateKeys: eventDateKeys, dailyByDate: dailyByDate, state: &state)- changedState = changedState || changedByRebuild || changedByReconcile-- let tasks = makeMaintenanceTasks(- state: &state,- dailyByDate: dailyByDate,- changedState: &changedState- )-- if changedState {- try HistoryStorage.saveMaintenanceState(state, in: directoryURL)- }-- return tasks- }- }-- private func markRebuildDates(- eventDateKeys: [String],- dailyByDate: [String: ActivityAggregate],- state: inout HistoryMaintenanceState- ) -> Bool {- var changed = false-- changed = state.markDirty(contentsOf: eventDateKeys.filter { dailyByDate[$0] == nil }) || changed- changed = state.markDirty(contentsOf: state.pending.filter { dailyByDate[$0] == nil }) || changed-- return changed- }-- private func reconcileEventFiles(- eventDateKeys: [String],- dailyByDate: [String: ActivityAggregate],- state: inout HistoryMaintenanceState- ) throws -> Bool {- verifiedBoundaries = verifiedBoundaries.filter { state.days[$0.key] != nil }- var changed = false-- for dateKey in eventDateKeys {- guard let stat = HistoryStorage.fileStat(at: eventLogURL(for: dateKey)) else {- continue- }-- let header: AppServerEventJournal.Header+ counts.idle = idleMaintenanceRounds+ idleMaintenanceRounds = counts.hasWork ? 0 : idleMaintenanceRounds + 1+ return await (loadSnapshot(synchronize: synchronize, trigger: trigger), counts.hasWork ? counts : nil)+ }++ func rebuildData(for dateKeys: [String], synchronize: Bool) async throws -> HistoryDataRebuildOutcome {+ let dates = Set(dateKeys.filter(HistoryStorage.isValidDateKey))+ guard !dates.isEmpty else { throw HistoryDataRebuildError.sourceUnavailable }+ let database = try HistoryDatabase(directoryURL: directoryURL)+ let available = try Set(database.dates())+ var rebuilt: [ActivityDataAggregate] = []+ var failed: [String] = []+ for date in dates.intersection(available).sorted() {do {- header = try AppServerEventJournal.header(at: eventLogURL(for: dateKey))- guard header.date == dateKey else { throw StorageCompatibilityError.sourceConflict }+ try rebuilt.append(database.aggregateActivityData(date: date, rebuilding: true))} catch {- changed = state.markDirty(dateKey) || changed- continue- }- if state.days[dateKey] == nil, let aggregate = dailyByDate[dateKey],- aggregate.generationID == header.generationID, let checkpoint = aggregate.sourceCheckpoint,- !hasBoundaryChanged(dateKey: dateKey, checkpoint: checkpoint, stat: stat) {- state.days[dateKey] = HistoryDayMaintenanceState(- offset: checkpoint.byteCount, size: checkpoint.byteCount,- generationID: header.generationID, fileIdentifier: stat.identifier- )- changed = true- } else if state.days[dateKey]?.generationID != header.generationID {- state.days[dateKey] = HistoryDayMaintenanceState(generationID: header.generationID, fileIdentifier: stat.identifier)- changed = state.markDirty(dateKey) || changed- }- guard let day = state.days[dateKey] else {- continue- }-- let identifierChanged = day.fileIdentifier != nil- && day.fileIdentifier != stat.identifier- let boundaryChanged = hasBoundaryChanged(dateKey: dateKey, checkpoint: dailyByDate[dateKey]?.sourceCheckpoint, stat: stat)-- if identifierChanged || stat.size < day.offset || boundaryChanged {- state.markDirty(dateKey)- changed = true- } else if day.offset != day.size {- state.markDirty(dateKey)- changed = true- } else if stat.size > day.offset,- !state.pending.contains(dateKey),- !state.dirty.contains(dateKey) {- state.markPending(dateKey)- changed = true- }- }-- return changed- }-- /// 完整前缀校验避免日志中部改写被误认成普通追加, 文件未变时复用校验结果- private func hasBoundaryChanged(dateKey: String, checkpoint: ActivitySourceCheckpoint?, stat: HistoryFileStat) -> Bool {- guard let checkpoint else { return true }- if let verified = verifiedBoundaries[dateKey],- verified.offset == checkpoint.byteCount, verified.digest == checkpoint.digest, verified.stat == stat {- return false- }- guard checkpoint.matchesSource(try? ActivitySourceCheckpoint.read(at: eventLogURL(for: dateKey), byteCount: checkpoint.byteCount)) else {- verifiedBoundaries.removeValue(forKey: dateKey)- return true- }- verifiedBoundaries[dateKey] = HistoryBoundaryVerification(offset: checkpoint.byteCount, digest: checkpoint.digest, stat: stat)- return false- }-- private func makeMaintenanceTasks(- state: inout HistoryMaintenanceState,- dailyByDate: [String: ActivityAggregate],- changedState: inout Bool- ) -> [HistoryMaintenanceTask] {- let cutoff = HistoryStorage.dateKey(for: HistoryStorage.retentionCutoffDate())- let dirty = Set(state.dirty.filter { $0 >= cutoff })- var tasks: [HistoryMaintenanceTask] = state.dirty.filter { $0 >= cutoff }.compactMap { dateKey in- guard let day = state.days[dateKey] else {- return nil- }- return dirtyTask(- for: dateKey,- day: day,- eventCountAvailability: dailyByDate[dateKey]?- .eventCountAvailability ?? .all- )- }-- for dateKey in state.pending where dateKey >= cutoff && !dirty.contains(dateKey) {- let day = state.days[dateKey] ?? HistoryDayMaintenanceState()- let size = eventLogSize(for: dateKey)- guard size > day.offset else {- state.removePending(dateKey)- changedState = true- continue- }-- let existingAggregate = dailyByDate[dateKey]- guard let baseAggregate = existingAggregate,- baseAggregate.generationID == day.generationID,- baseAggregate.sourceCheckpoint?.byteCount == day.offset else {- state.markDirty(dateKey)- changedState = true- tasks.append(dirtyTask(- for: dateKey,- day: day,- size: size,- eventCountAvailability: existingAggregate?- .eventCountAvailability ?? .all- ))- continue- }-- tasks.append(- pendingTask(- for: dateKey,- day: day,- size: size,- baseAggregate: baseAggregate- )- )- }-- return tasks- }-- private func dirtyTask(- for dateKey: String,- day: HistoryDayMaintenanceState,- size: UInt64? = nil,- eventCountAvailability: ActivityCountAvailability = .all- ) -> HistoryMaintenanceTask {- let stat = HistoryStorage.fileStat(at: eventLogURL(for: dateKey))- return HistoryMaintenanceTask(- dateKey: dateKey,- startOffset: 0,- size: size ?? stat?.size ?? 0,- mode: .rebuild(eventCountAvailability),- existingCorrupt: 0,- generationID: day.generationID,- fileIdentifier: stat?.identifier- )- }-- private func pendingTask(- for dateKey: String,- day: HistoryDayMaintenanceState,- size: UInt64,- baseAggregate: ActivityAggregate- ) -> HistoryMaintenanceTask {- HistoryMaintenanceTask(- dateKey: dateKey,- startOffset: day.offset,- size: size,- mode: .append(baseAggregate),- existingCorrupt: day.corrupt,- generationID: day.generationID,- fileIdentifier: day.fileIdentifier- )- }-- // MARK: - 事件文件读取与聚合-- private func eventLogURL(for dateKey: String) -> URL {- HistoryStorage.eventLogURL(for: dateKey, in: eventsDirectoryURL)- }-- private func eventLogSize(for dateKey: String) -> UInt64 {- HistoryStorage.fileSize(at: eventLogURL(for: dateKey))- }-- private func loadDailyAggregatesWithFailures() throws -> JSONLinesDecodeResult<ActivityAggregate> {- do {- return try JSONLines.decodeWithFailures(from: Data(contentsOf: dailyLogURL))- } catch let error as CocoaError where error.code == .fileReadNoSuchFile {- return JSONLinesDecodeResult(values: [], failedLineCount: 0)- }- }-- private func eventDateKeys() throws -> [String] {- let cutoff = HistoryStorage.dateKey(for: HistoryStorage.retentionCutoffDate())- return try HistoryStorage.readEventLogDateKeys(in: eventsDirectoryURL).filter { $0 >= cutoff }- }-- private func buildDailyAggregate(- for task: HistoryMaintenanceTask- ) throws -> HistoryMaintenanceResult {- var accumulator = switch task.mode {- case let .rebuild(eventCountAvailability):- ActivityAccumulator(- rebuilding: task.dateKey,- generationID: task.generationID,-- eventCountAvailability: eventCountAvailability- )- case let .append(baseAggregate):- ActivityAccumulator(- appending: baseAggregate,- generationID: task.generationID- )- }- var corrupt = task.existingCorrupt- if case let .append(base) = task.mode, !base.supportsIncrementalAggregation {- // 恢复去重身份不重新计算旧计数, 追加到压缩日期也不会改变历史口径- corrupt += try readEvents(at: eventLogURL(for: task.dateKey), from: 0, upTo: task.startOffset) {- accumulator.restoreIdentity(from: $0)- }- }-- corrupt += try readEvents(- at: eventLogURL(for: task.dateKey),- from: task.startOffset,- upTo: task.size- ) { event in- accumulator.record(event)- }-- let identifierStorage: ActivityIdentifiers = retainsIdentifiers(for: task.dateKey)- ? .retained- : .compacted- var aggregate = accumulator.finalized(identifierStorage: identifierStorage)- var checkpoint = try ActivitySourceCheckpoint.read(at: eventLogURL(for: task.dateKey), byteCount: task.size)- let ranges: [AggregationRange] = switch task.mode {- case .rebuild:- []- case let .append(base):- base.sourceCheckpoint?.aggregationRanges.isEmpty == false- ? base.sourceCheckpoint?.aggregationRanges ?? []- : [AggregationRange(version: base.aggregationVersion, end: task.startOffset)]- }- checkpoint.aggregationRanges = AggregationRange.appending(to: ranges, version: AggregationVersion.activity, end: task.size)- aggregate.aggregationVersion = AggregationVersion.activity- aggregate.sourceCheckpoint = checkpoint- return HistoryMaintenanceResult(- dateKey: task.dateKey,- aggregate: aggregate,- size: task.size,- corrupt: corrupt,- fileIdentifier: task.fileIdentifier- )- }-- private func readEvents(- at url: URL,- from startOffset: UInt64,- upTo size: UInt64,- record: (ActivityRecord) -> Void- ) throws -> Int {- var corrupt = 0- try AppServerEventJournal.read(at: url, from: startOffset, upTo: size, onInvalidLine: { corrupt += 1 }, consume: { entry in- if let event = entry.activity {- record(event)- }- })- return corrupt- }-- // MARK: - 落盘与状态提交-- /// 返回聚合与维护状态是否都基于同一份事件源完成提交- private func commit(- _ result: HistoryMaintenanceResult,- aggregates: inout [ActivityAggregate]- ) throws -> Bool {- try HistoryStorage.withExclusiveLock(in: directoryURL) {- var state = try HistoryStorage.loadMaintenanceState(in: directoryURL)- guard let currentSize = try validatedEventLogSize(for: result, state: &state) else { return false }- if result.corrupt > 0 {- throw HistoryDataRebuildError.incompleteSource- }- // 两个文件提交前先标脏, 中途退出会从事件重建, 不沿旧偏移重复追加- state.markDirty(result.dateKey)- try HistoryStorage.saveMaintenanceState(state, in: directoryURL)- aggregates = loadDailyAggregates() ?? []- try writeDailyAggregate(result.aggregate, into: &aggregates)- state.days[result.dateKey] = HistoryDayMaintenanceState(- offset: result.size, size: result.size, corrupt: result.corrupt,- generationID: result.aggregate.generationID,- fileIdentifier: result.fileIdentifier- )- state.removeDirty(result.dateKey)- if currentSize == result.size {- state.removePending(result.dateKey)- } else {- state.markPending(result.dateKey)- }- try HistoryStorage.saveMaintenanceState(state, in: directoryURL)- return true- }- }-- /// 锁内确认读取期间仍是同一份追加源, 发生改写则等待重建- private func validatedEventLogSize(- for result: HistoryMaintenanceResult,- state: inout HistoryMaintenanceState- ) throws -> UInt64? {- guard state.days[result.dateKey]?.generationID == result.aggregate.generationID else {- return nil- }-- let stat = HistoryStorage.fileStat(at: eventLogURL(for: result.dateKey))- let identifierMatches = result.fileIdentifier == nil- || result.fileIdentifier == stat?.identifier- let boundaryMatches = result.aggregate.sourceCheckpoint?.matchesSource(try? ActivitySourceCheckpoint.read(- at: eventLogURL(for: result.dateKey), byteCount: result.size- )) == true-- guard let stat,- stat.size >= result.size,- identifierMatches,- boundaryMatches else {- state.markDirty(result.dateKey)- try HistoryStorage.saveMaintenanceState(state, in: directoryURL)- return nil- }-- return stat.size- }-- private func writeDailyAggregate(- _ aggregate: ActivityAggregate,- into aggregates: inout [ActivityAggregate]- ) throws {- if let error = try loadDailyAggregatesWithFailures().compatibilityError {- throw error- }- try FileManager.default.createDirectory(- at: dailyLogURL.deletingLastPathComponent(),- withIntermediateDirectories: true- )-- upsert(aggregate, into: &aggregates)- aggregates = ActivityAggregate.normalized(aggregates: aggregates)- let data = try ActivityAggregate.encodeJSONLines(aggregates)- try data.write(to: dailyLogURL, options: .atomic)- }-- func normalizeDailyAggregatesIfNeeded() throws {- try HistoryStorage.withExclusiveLock(in: directoryURL) {- guard let stat = HistoryStorage.fileStat(at: dailyLogURL), stat.size > 0 else {- return- }-- // 稳态下文件与日期都没变, 跳过全量解码与重编码比对- let dayKey = HistoryStorage.dateKey(for: Date())- if lastNormalizedDailyLog == HistoryDailyLogStamp(size: stat.size, identifier: stat.identifier, dayKey: dayKey) {- return- }-- guard let data = try? Data(contentsOf: dailyLogURL), !data.isEmpty else {- return- }-- let decodeResult = JSONLines.decodeWithFailures(ActivityAggregate.self, from: data)- if let error = decodeResult.compatibilityError {- throw error- }- guard decodeResult.failedLineCount == 0 else {- return- }-- let normalizedAggregates = ActivityAggregate.normalized(aggregates: decodeResult.values)- let normalizedData = try ActivityAggregate.encodeJSONLines(normalizedAggregates)- if normalizedData != data {- try FileManager.default.createDirectory(- at: dailyLogURL.deletingLastPathComponent(),- withIntermediateDirectories: true- )- try normalizedData.write(to: dailyLogURL, options: .atomic)- }-- if let latestStat = HistoryStorage.fileStat(at: dailyLogURL) {- lastNormalizedDailyLog = HistoryDailyLogStamp(- size: latestStat.size,- identifier: latestStat.identifier,- dayKey: dayKey- )+ failed.append(date)}}- }-- private func upsert(_ aggregate: ActivityAggregate, into aggregates: inout [ActivityAggregate]) {- if let index = aggregates.firstIndex(where: { $0.date == aggregate.date }) {- aggregates[index] = aggregate- } else {- aggregates.append(aggregate)- }- }-- private func markDirty(_ dateKey: String) {+ var tokenUsageRebuild = TokenUsageRebuildResult.empty+ var tokenUsageFailed = falsedo {- try HistoryStorage.withExclusiveLock(in: directoryURL) {- var state = try HistoryStorage.loadMaintenanceState(in: directoryURL)- state.markDirty(dateKey)- try HistoryStorage.saveMaintenanceState(state, in: directoryURL)- }+ tokenUsageRebuild = try await tokenUsageHistory.rebuild(for: Array(dates))} catch {- // 标记丢了下一轮就不会重建这天, 数据会静默缺一块- let details = LogFields.joined(- "stage=markDirty",- "date=\(dateKey)",- "detail=\(error.localizedDescription)"- )- AppLog.history.error("事件汇总失败: \(details, privacy: .public)")- }- }-- // MARK: - 保留期清理-- @discardableResult- private func pruneExpiredEventFiles(now: Date) throws -> Int {- let cutoffDate = HistoryStorage.retentionCutoffDate(today: now)- guard lastRetentionCutoff != cutoffDate else { return 0 }- let expiredDateKeys = try HistoryStorage.withExclusiveLock(in: directoryURL) {- let expiredDateKeys = try HistoryStorage.readEventLogDateKeys(in: eventsDirectoryURL).filter { dateKey in- dateKey < HistoryStorage.dateKey(for: cutoffDate)- }- var state = try HistoryStorage.loadMaintenanceState(in: directoryURL)- var failedDates = Set<String>()- for dateKey in expiredDateKeys {- do {- try AppServerEventJournal.read(at: eventLogURL(for: dateKey)) { _ in }- try FileManager.default.removeItem(at: eventLogURL(for: dateKey))- } catch CocoaError.fileNoSuchFile {- continue- } catch {- failedDates.insert(dateKey)- AppLog.history.error("过期事件清理失败: date=\(dateKey, privacy: .public) detail=\(error.localizedDescription, privacy: .public)")- }- }- let cutoffKey = HistoryStorage.dateKey(for: cutoffDate)- let expiredStateKeys = Set(state.days.keys).union(state.pending).union(state.dirty)- .filter { $0 < cutoffKey && !failedDates.contains($0) }- for dateKey in expiredStateKeys {- state.remove(dateKey)- }- if !expiredStateKeys.isEmpty {- try HistoryStorage.saveMaintenanceState(state, in: directoryURL)- }- return expiredDateKeys.filter { !failedDates.contains($0) }- }- // 单文件失败留待下一天或重启后重试, 避免刷新时反复扫描同一异常文件- lastRetentionCutoff = cutoffDate- guard !expiredDateKeys.isEmpty else { return 0 }- // 保留期到点会真的删掉原始事件文件, 数据对不上时要能查到哪些日期被清掉了- let details = LogFields.joined(- "dates=\(expiredDateKeys.count)",- "oldest=\(expiredDateKeys.first ?? "-")"+ tokenUsageFailed = true+ }+ guard !rebuilt.isEmpty || !tokenUsageRebuild.dateKeys.isEmpty else { throw HistoryDataRebuildError.sourceUnavailable }+ let snapshot = await loadSnapshot(synchronize: synchronize, trigger: .manual)+ let tokenUsageAggregates = await (try? tokenUsageHistory.persistedAggregates()) ?? []+ let tokenUsagePending = await syncService.hasPendingTokenUsageUpdates(local: tokenUsageAggregates)+ let activityDataPending = await syncService.hasPendingActivityDataUpdates(local: (try? database.activityDataAggregates()) ?? [])+ let tokenUsageFailures = tokenUsageFailed ? Array(dates).sorted() : tokenUsageRebuild.failedDateKeys+ let summary = HistoryDataRebuildSummary(+ rebuiltDateCount: Set(rebuilt.map(\.date)).union(tokenUsageRebuild.dateKeys).subtracting(failed).subtracting(tokenUsageFailures).count,+ eventCount: rebuilt.reduce(0) { $0 + ($1.eventCount ?? 0) }, isSyncPending: tokenUsagePending || activityDataPending,+ failedDateKeys: failed, failedTokenUsageDateKeys: tokenUsageFailures,+ tokenUsageEventCount: tokenUsageRebuild.eventCount, didFailTokenUsageRebuild: tokenUsageFailed || !tokenUsageRebuild.failedDateKeys.isEmpty)- AppLog.history.notice("过期事件已清理: \(details, privacy: .public)")- return expiredDateKeys.count- }-- private func retainsIdentifiers(for dateKey: String) -> Bool {- guard let date = CodexDateFormat.dayDate(from: dateKey) else {- return true- }-- return date >= HistoryStorage.identifierRetentionCutoffDate()- }-}--/// 完整精度的校验结果只在当前进程复用-private nonisolated struct HistoryBoundaryVerification {- let offset: UInt64- let digest: String- let stat: HistoryFileStat-}--/// Aggregates/activity.jsonl 某一时刻的 stat 快照与当天日期键, 用于跳过稳态下的重复归一化-private nonisolated struct HistoryDailyLogStamp: Equatable {- let size: UInt64- let identifier: UInt64?- let dayKey: String-}--/// 全量重建与安全增量使用不同初始状态, 避免用 nil 隐含任务语义-private nonisolated enum HistoryMaintenanceMode {- case rebuild(ActivityCountAvailability)- case append(ActivityAggregate)-}--// 单次维护任务: 从 startOffset 读到 size, 可基于已有聚合继续追加-private nonisolated struct HistoryMaintenanceTask {- let dateKey: String- let startOffset: UInt64- let size: UInt64- let mode: HistoryMaintenanceMode- let existingCorrupt: Int- let generationID: String?- let fileIdentifier: UInt64?-- var baseEventCount: Int {- switch mode {- case .rebuild:- 0- case let .append(aggregate):- aggregate.eventCount ?? 0- }+ return HistoryDataRebuildOutcome(snapshot: snapshot, summary: summary)}}-/// 维护任务的提交结果, corrupt 统计保留给后续诊断-private nonisolated struct HistoryMaintenanceResult {- let dateKey: String- let aggregate: ActivityAggregate- let size: UInt64- let corrupt: Int- let fileIdentifier: UInt64?-}-nonisolated struct HistoryDataRebuildSummary: Equatable, Sendable {let rebuiltDateCount: Intlet eventCount: Intlet isSyncPending: Boollet failedDateKeys: [String]- let failedRequestDateKeys: [String]- let failedTokenDateKeys: [String]- let tokenTurnCount: Int- let didFailTokenRebuild: Bool+ let failedTokenUsageDateKeys: [String]+ let tokenUsageEventCount: Int+ let didFailTokenUsageRebuild: Bool}nonisolated struct HistoryDataRebuildOutcome {@@ -973,19 +160,13 @@ nonisolated struct HistoryDataRebuildOutcome {let summary: HistoryDataRebuildSummary}-private nonisolated enum HistoryDataRebuildError: LocalizedError {+nonisolated enum HistoryDataRebuildError: LocalizedError {case sourceUnavailable- case sourceChanged- case incompleteSourcevar errorDescription: String? {switch self {case .sourceUnavailable:String(localized: "history.rebuild.error.source-unavailable")- case .sourceChanged:- String(localized: "history.rebuild.error.source-changed")- case .incompleteSource:- String(localized: "history.rebuild.error.incomplete-source")}}}diff --git a/CodexBar/Services/History/HistoryStorage.swift b/CodexBar/Services/History/HistoryStorage.swiftindex cfa2c9f..72adff3 100644--- a/CodexBar/Services/History/HistoryStorage.swift+++ b/CodexBar/Services/History/HistoryStorage.swift@@ -1,137 +1,21 @@-import Darwinimport Foundation-/// 活动统计文件路径, 保留期和跨进程 flock 都集中在这里+/// 历史数据的位置与保留期, SQLite 负责跨进程事务nonisolated enum HistoryStorage {private static let retentionDayCount = 210private static let identifierRetentionDayCount = 3- // MARK: - 存储路径-- static func eventsDirectoryURL(in root: URL = directoryURL()) -> URL {- root- .appendingPathComponent("Events", isDirectory: true)- }-- static func eventLogURL(for dateKey: String, in directoryURL: URL = eventsDirectoryURL()) -> URL {- directoryURL.appendingPathComponent("\(dateKey).jsonl", isDirectory: false)- }-- static func dailyURL(in root: URL = directoryURL()) -> URL {- root.appendingPathComponent("Aggregates/activity.jsonl", isDirectory: false)- }-- static func lockURL(in root: URL = directoryURL()) -> URL {- root.appendingPathComponent("Locks/events.lock", isDirectory: false)- }-- static func maintenanceURL(in root: URL = directoryURL()) -> URL {- root.appendingPathComponent("State/aggregation.json", isDirectory: false)- }-- static func syncDirectoryURL(in root: URL = directoryURL()) -> URL {- root.appendingPathComponent("Sync", isDirectory: true)- }-static func directoryURL() -> URL {AppStorage.directoryURL()}- static func withExclusiveLock<T>(- in root: URL = directoryURL(),- _ work: () throws -> T- ) throws -> T {- // 采集与维护使用独立服务实例, 文件事务必须共享同一把锁- let url = lockURL(in: root)- let deadline = ContinuousClock.now.advanced(by: .seconds(2))- while true {- try Task.checkCancellation()- if let descriptor = try JSONFileStorage.acquireLock(in: url.deletingLastPathComponent(), name: url.lastPathComponent, nonblocking: true) {- defer { JSONFileStorage.releaseLock(descriptor) }- return try work()- }- guard ContinuousClock.now < deadline else { throw POSIXError(.ETIMEDOUT) }- Thread.sleep(forTimeInterval: 0.01)- }- }-- static func loadMaintenanceState(in root: URL = directoryURL()) throws -> HistoryMaintenanceState {- let url = maintenanceURL(in: root)- do {- let data = try Data(contentsOf: url)- try StorageVersion.validate(data, current: HistoryMaintenanceState.currentVersion, name: "AggregationState")- return try JSONLines.decoder.decode(HistoryMaintenanceState.self, from: data)- } catch let error as CocoaError where error.code == .fileReadNoSuchFile {- return HistoryMaintenanceState()- }- }-- static func saveMaintenanceState(_ state: HistoryMaintenanceState, in root: URL = directoryURL()) throws {- let url = maintenanceURL(in: root)- try FileManager.default.createDirectory(- at: url.deletingLastPathComponent(),- withIntermediateDirectories: true- )-- let data = try JSONLines.stableEncoder.encode(state)- try data.write(to: url, options: .atomic)- }-- static func fileSize(at url: URL) -> UInt64 {- fileStat(at: url)?.size ?? 0- }-- /// 单次 stat 同时返回大小与 inode 标识, 文件缺失或不可读时为 nil- /// 维护流程需要批量读取文件状态, 直接使用 stat(2) 避免逐个构造属性字典- /// 内存保留纳秒精度用于检测改写, 落盘时间统一使用整数毫秒- /// 沿用 attributesOfItem 的语义跟随符号链接, 因此用 stat 而非 lstat- static func fileStat(at url: URL) -> HistoryFileStat? {- var info = Darwin.stat()- guard stat(url.path, &info) == 0 else {- return nil- }-- return HistoryFileStat(- size: UInt64(clamping: info.st_size),- identifier: UInt64(info.st_ino),- modifiedAtNanoseconds: Int64(info.st_mtimespec.tv_sec) * 1000000000- + Int64(info.st_mtimespec.tv_nsec)- )- }-- /// 枚举目录中文件名为合法日期键的 .jsonl 事件日志, 返回升序日期键- static func eventLogDateKeys(in directoryURL: URL = eventsDirectoryURL()) -> [String] {- (try? readEventLogDateKeys(in: directoryURL)) ?? []- }-- static func readEventLogDateKeys(in directoryURL: URL) throws -> [String] {- let contents: [URL]- do {- contents = try FileManager.default.contentsOfDirectory(- at: directoryURL, includingPropertiesForKeys: nil, options: [.skipsHiddenFiles]- )- } catch CocoaError.fileReadNoSuchFile {- return []- }-- return contents- .filter { $0.pathExtension == "jsonl" }- .map { $0.deletingPathExtension().lastPathComponent }- .filter { isValidDateKey($0) }- .sorted()+ static func syncDirectoryURL(in root: URL = directoryURL()) -> URL {+ root.appendingPathComponent("Sync", isDirectory: true)}- /// 返回保留窗口内且本机原始事件文件非空的日期, 供设置页标记和筛选实际重建项- static func rebuildableEventDateKeys(- in directoryURL: URL = eventsDirectoryURL()- ) -> [String] {- let cutoffKey = dateKey(for: retentionCutoffDate())- return eventLogDateKeys(in: directoryURL)- .filter { dateKey in- dateKey >= cutoffKey- && fileSize(at: eventLogURL(for: dateKey, in: directoryURL)) > 0- }- .sorted(by: >)+ static func rebuildableEventDateKeys(in directoryURL: URL = directoryURL()) -> [String] {+ let cutoff = dateKey(for: retentionCutoffDate())+ return ((try? HistoryDatabase(directoryURL: directoryURL).dates()) ?? []).filter { $0 >= cutoff }}static func retentionCutoffDate(today: Date = Date(), calendar: Calendar = .current) -> Date {@@ -159,13 +43,3 @@ nonisolated enum HistoryStorage {CodexDateFormat.dayDate(from: dateKey) != nil}}--nonisolated struct HistoryFileStat: Equatable {- let size: UInt64- let identifier: UInt64- let modifiedAtNanoseconds: Int64-- var modificationTime: Int64 {- modifiedAtNanoseconds / 1000000- }-}diff --git a/CodexBar/Services/History/TokenHistoryStore.swift b/CodexBar/Services/History/TokenHistoryStore.swiftdeleted file mode 100644index 94b1fed..0000000--- a/CodexBar/Services/History/TokenHistoryStore.swift+++ /dev/null@@ -1,430 +0,0 @@-import Foundation-import os--/// 保存订阅期间的原始 Token 观测, 聚合缓存可独立重建-actor TokenHistoryStore {- private let directoryURL: URL- private var recordingLease: Int32?- private var journal = AppServerEventJournal()- private var journalFiles: [String: TokenJournalCheckpoint] = [:]- private var scannedFiles: [String: HistoryFileStat] = [:]- private var recoveredTurnIDs = Set<String>()-- init(directoryURL: URL = HistoryStorage.directoryURL()) {- self.directoryURL = directoryURL- }-- deinit {- if let recordingLease {- JSONFileStorage.releaseLock(recordingLease)- }- }-- /// 同机多个 App 可以旁观, 但只有一个采集器累计 Token, 避免不同基线覆盖彼此- func acquireRecordingLease() throws -> Bool {- if recordingLease != nil {- return true- }- recordingLease = try JSONFileStorage.acquireLock(in: directoryURL.appendingPathComponent("Locks"), name: "tokens.lock", nonblocking: true)- return recordingLease != nil- }-- func releaseRecordingLease() {- if let recordingLease {- JSONFileStorage.releaseLock(recordingLease)- }- recordingLease = nil- }-- /// 原始记录不依赖聚合缓存可读, 聚合失败后仍可按日志检查点恢复- func appendObservation(_ observation: TokenObservation, now: Date = Date()) throws {- try HistoryStorage.withExclusiveLock(in: directoryURL) {- try journal.append(AppServerEventRecord(observation: observation, recordedAt: now), in: directoryURL)- }- }-- func refresh(now: Date = Date(), baseline: TokenHistoryBaseline? = nil) throws -> [TokenTurn] {- try HistoryStorage.withExclusiveLock(in: directoryURL) {- var records = try load(now: now)- var replacements: [TokenTurn] = []- if let baseline {- for (id, turn) in records {- if let replacement = baseline.replacement(for: turn, recovering: recoveredTurnIDs.contains(id), now: now) {- records[id] = replacement- if replacement != turn {- replacements.append(replacement)- }- }- if baseline.contains(turn), baseline.acceptsRecovery(turn) {- recoveredTurnIDs.remove(id)- }- }- }- let roots = Set(replacements.map(\.rootID))- try saveObservedTurns(replacements + roots.compactMap { records[$0] }, now: now)- records = retained(records, now: now)- try save(records)- return Array(records.values)- }- }-- func currentTurns(now: Date = Date()) throws -> [TokenTurn] {- try HistoryStorage.withExclusiveLock(in: directoryURL) { try Array(load(now: now).values) }- }-- func pendingRecoveryIDs() -> Set<String> {- recoveredTurnIDs- }-- func persistedTurns(now: Date = Date()) throws -> [TokenTurn] {- try HistoryStorage.withExclusiveLock(in: directoryURL) {- try Array(retained(readCache()?.turns ?? [:], now: now).values)- }- }-- func rebuildableDateKeys(now: Date = Date()) throws -> [String] {- try Array(TokenTurn.dailyUsage(currentTurns(now: now), now: now).keys).sorted()- }-- func rebuild(for dateKeys: [String], now: Date = Date()) throws -> TokenRebuildResult {- try HistoryStorage.withExclusiveLock(in: directoryURL) {- var ledger = try load(now: now)- let replay = try journalTurns(now: now, baseline: ledger)- let dates = Set(dateKeys)- let roots = try replay.selectedRoots(in: dates)- var rebuiltDates = Set<String>()- var failedDates = Set<String>()- var rebuiltTurns: [TokenTurn] = []- let existingByRoot = Dictionary(grouping: ledger.values, by: \.rootID)- let recordedByRoot = Dictionary(grouping: replay.records.values, by: \.rootID)- for (root, date) in roots.sorted(by: { $0.key < $1.key }) {- let candidates = recordedByRoot[root] ?? []- let coversExisting = (existingByRoot[root] ?? []).allSatisfy { previous in- guard let candidate = replay.records[previous.id] else { return false }- return candidate.covers(previous.checkpoint)- && (previous.usage == nil || candidate.usage != nil)- }- guard !replay.failedRoots.contains(root), !candidates.isEmpty, coversExisting else {- failedDates.insert(date)- continue- }- // 同一根轮次的全部成员先通过检查, 再生成修正记录- for var turn in candidates {- let previous = ledger[turn.id]- turn.generationID = previous?.generationID ?? turn.generationID- turn.ancestorIDs = previous?.ancestorIDs ?? []- turn.startNewGeneration(at: now)- ledger[turn.id] = turn- rebuiltTurns.append(turn)- }- rebuiltDates.insert(date)- }- try saveObservedTurns(rebuiltTurns, now: now)- try save(ledger)- return TokenRebuildResult(- dateKeys: rebuiltDates.sorted(), turnCount: rebuiltTurns.count,- failedDateKeys: failedDates.sorted(), turns: Array(ledger.values)- )- }- }-- private func saveObservedTurns(_ records: [TokenTurn], now: Date) throws {- for record in try TokenTurn.merged(records).values.sorted(by: { $0.id < $1.id }) {- try journal.append(AppServerEventRecord(token: record, recordedAt: now), in: directoryURL)- }- }-- private func journalTurns(now: Date, baseline: [String: TokenTurn]) throws -> TokenJournalReplay {- var replay = try TokenJournalReplay(baseline: baseline, replayingFromEmpty: true)- for date in try journalDates(now: now) {- let url = HistoryStorage.eventLogURL(for: date, in: HistoryStorage.eventsDirectoryURL(in: directoryURL))- var invalid = false- try AppServerEventJournal.read(at: url, onInvalidLine: { invalid = true }, consume: { entry in- try replay.consume(entry, includesSnapshots: false)- })- if invalid {- throw TokenCacheError.incompleteJournal- }- }- return replay- }-- private func journalDates(now: Date) throws -> [String] {- let directory = HistoryStorage.eventsDirectoryURL(in: directoryURL)- let cutoff = HistoryStorage.dateKey(for: HistoryStorage.retentionCutoffDate(today: now))- return try HistoryStorage.readEventLogDateKeys(in: directory).filter { $0 >= cutoff }- }-- private var ledgerURL: URL {- directoryURL.appendingPathComponent("Aggregates/tokens.json")- }-- private func readCache() throws -> TokenAggregateCache? {- let cache: TokenAggregateCache?- do {- let data = try Data(contentsOf: ledgerURL)- // 版本先于完整解码检查, 不把其他版本的数据当成损坏缓存覆盖- struct Version: Decodable { let version: Int }- let version = try JSONDecoder().decode(Version.self, from: data).version- guard version == TokenAggregateCache.currentVersion else { throw TokenCacheError.unsupportedVersion(version) }- cache = try AppServerEventRecord.decode(TokenAggregateCache.self, from: data)- } catch let error as CocoaError where error.code == .fileReadNoSuchFile {- cache = nil- } catch is DecodingError {- AppLog.history.error("Token 缓存损坏, 从原始事件恢复")- cache = nil- } catch StorageCompatibilityError.incompleteSource {- AppLog.history.error("Token 缓存检查点损坏, 从原始事件恢复")- cache = nil- }- return cache- }-- private func load(now: Date) throws -> [String: TokenTurn] {- let cache = try readCache()- recoveredTurnIDs = Set(cache?.recoveredTurnIDs ?? [])- if cache == nil {- journalFiles.removeAll()- scannedFiles.removeAll()- }- var baseline = cache?.turns ?? [:]- var replay = try TokenJournalReplay(baseline: baseline)- journalFiles = cache?.files ?? [:]- let directory = HistoryStorage.eventsDirectoryURL(in: directoryURL)- let dates = try journalDates(now: now)- let previousFiles = journalFiles- var rootsByFile: [String: Set<String>] = [:]- journalFiles = journalFiles.filter { dates.contains($0.key) }- scannedFiles = scannedFiles.filter { dates.contains($0.key) }- for date in dates {- try Task.checkCancellation()- let url = HistoryStorage.eventLogURL(for: date, in: directory)- guard let stat = HistoryStorage.fileStat(at: url) else { throw TokenCacheError.incompleteJournal }- let previous = journalFiles[date]- if previous?.size == stat.size, previous?.identifier == stat.identifier,- previous?.modificationTime == stat.modificationTime, scannedFiles[date] == stat {- continue- }- let offset = previous.flatMap { $0.identifier == stat.identifier && $0.size < stat.size ? $0.size : nil } ?? 0- var invalid = false- try AppServerEventJournal.read(at: url, from: offset, onInvalidLine: { invalid = true }, consume: { entry in- if let root = entry.observation?.turn.rootID ?? entry.token?.rootID {- rootsByFile[date, default: []].insert(root)- }- try replay.consume(entry, includesSnapshots: true, rebuildingCache: cache == nil)- })- if invalid {- guard cache != nil else { throw TokenCacheError.incompleteJournal }- // 日常恢复可沿用已有缓存处理完整记录, 显式重建仍要求原始日志完整- scannedFiles.removeValue(forKey: date)- continue- }- journalFiles[date] = TokenJournalCheckpoint(size: stat.size, identifier: stat.identifier, modificationTime: stat.modificationTime)- scannedFiles[date] = stat- }- if cache == nil {- let replacements = replay.recoverSnapshots(now: now)- // 原始观测不足时只沿用可信旧快照, 不把它标记为重算结果- let conflictedRoots = Set(replay.snapshots.values.filter(\.hasConflict).map(\.rootID))- baseline = replay.snapshots.filter { !conflictedRoots.contains($0.value.rootID) }- try saveObservedTurns(replacements, now: now)- }- // 回滚失败根轮次在本轮的所有改动, 涉及的文件保留原游标以便下次重试- for (date, roots) in rootsByFile where !roots.isDisjoint(with: replay.failedRoots) {- journalFiles[date] = previousFiles[date]- scannedFiles.removeValue(forKey: date)- }- var records = replay.records.filter { !replay.failedRoots.contains($0.value.rootID) }- for (id, turn) in baseline where replay.failedRoots.contains(turn.rootID) {- records[id] = turn- }- if cache == nil {- recoveredTurnIDs.formUnion(replay.records.values.filter { !replay.failedRoots.contains($0.rootID) }.map(\.id))- }- return retained(records, now: now)- }-- private func save(_ records: [String: TokenTurn]) throws {- // 日志与缓存共用时间编码, 避免 epoch 换算精度差异影响重建标记比较- let data = try AppServerEventRecord.encode(TokenAggregateCache(turns: records, files: journalFiles, recoveredTurnIDs: recoveredTurnIDs.intersection(records.keys).sorted()))- guard (try? Data(contentsOf: ledgerURL)) != data else { return }- try FileManager.default.createDirectory(at: ledgerURL.deletingLastPathComponent(), withIntermediateDirectories: true)- try data.write(to: ledgerURL, options: .atomic)- }-- private func retained(_ records: [String: TokenTurn], now: Date) -> [String: TokenTurn] {- let cutoff = HistoryStorage.retentionCutoffDate(today: now)- let roots = Set(records.values.filter { $0.updatedAt >= cutoff }.map(\.rootID))- return records.filter { $0.value.updatedAt >= cutoff || roots.contains($0.key) }- }-}--/// 重放错误以根轮次为边界, 归属不明时不能证明错误与选区无关-private nonisolated struct TokenJournalReplay {- var records: [String: TokenTurn]- var failedRoots = Set<String>()- private var rootByTurn: [String: String] = [:]- private var startsByRoot: [String: Date] = [:]- var snapshots: [String: TokenTurn] = [:]-- init(baseline: [String: TokenTurn], replayingFromEmpty: Bool = false) throws {- records = replayingFromEmpty ? [:] : baseline- for turn in baseline.values {- try register(turn, rootStartedAt: baseline[turn.rootID]?.startedAt)- }- }-- mutating func consume(_ entry: AppServerEventRecord, includesSnapshots: Bool, rebuildingCache: Bool = false) throws {- if let observation = entry.observation {- let turn = observation.turn- try register(turn.emptyTurn, rootStartedAt: observation.rootStartedAt)- guard !failedRoots.contains(turn.rootID) else { return }- do {- records[turn.id] = try observation.applying(to: records[turn.id])- if turn.id != turn.rootID, records[turn.rootID] == nil {- records[turn.rootID] = TokenTurn(- id: turn.rootID, rootID: turn.rootID,- startedAt: startsByRoot[turn.rootID], updatedAt: turn.updatedAt- )- }- if records[turn.rootID]?.startedAt == nil {- records[turn.rootID]?.startedAt = startsByRoot[turn.rootID]- }- } catch {- failedRoots.insert(turn.rootID)- }- } else if let turn = entry.token {- try register(turn, rootStartedAt: nil)- if rebuildingCache {- snapshots[turn.id] = try snapshots[turn.id]?.merging(turn) ?? turn- } else if includesSnapshots, !failedRoots.contains(turn.rootID) {- records[turn.id] = try records[turn.id]?.merging(turn) ?? turn- }- }- }-- /// 重算值只来自原始观测, 快照用于校验覆盖范围与继承替换关系- mutating func recoverSnapshots(now: Date) -> [TokenTurn] {- var replacements: [TokenTurn] = []- for (root, previousTurns) in Dictionary(grouping: snapshots.values, by: \.rootID) {- let coversPrevious = previousTurns.allSatisfy { previous in- guard let candidate = records[previous.id] else { return false }- return candidate.covers(previous.checkpoint)- && (previous.usage == nil || candidate.usage != nil)- }- guard !failedRoots.contains(root), coversPrevious else {- failedRoots.insert(root)- continue- }- // 主轮次与子 Agent 全部通过检查后再生成替换, 不提交半个任务- for previous in previousTurns {- guard var candidate = records[previous.id] else { continue }- candidate.startedAt = candidate.startedAt ?? previous.startedAt- candidate.updatedAt = max(candidate.updatedAt, previous.updatedAt)- candidate.generationID = previous.generationID- candidate.ancestorIDs = previous.ancestorIDs- candidate.rebuiltAt = previous.rebuiltAt- if previous.hasConflict || candidate.usage != previous.usage- || candidate.aggregationVersion != previous.aggregationVersion || candidate.checkpoint != previous.checkpoint {- candidate.startNewGeneration(at: now)- }- records[candidate.id] = candidate- if candidate != previous {- replacements.append(candidate)- }- }- }- return replacements- }-- func selectedRoots(in dates: Set<String>) throws -> [String: String] {- var result: [String: String] = [:]- for root in Set(rootByTurn.values) {- guard let start = startsByRoot[root] else { throw TokenCacheError.incompleteJournal }- let date = HistoryStorage.dateKey(for: start)- if dates.contains(date) {- result[root] = date- }- }- return result- }-- private mutating func register(_ turn: TokenTurn, rootStartedAt: Date?) throws {- if let previous = rootByTurn[turn.id], previous != turn.rootID {- throw TokenCacheError.incompleteJournal- }- rootByTurn[turn.id] = turn.rootID- let start = rootStartedAt ?? (turn.id == turn.rootID ? turn.startedAt : nil)- if let start {- if let previous = startsByRoot[turn.rootID],- HistoryStorage.dateKey(for: previous) != HistoryStorage.dateKey(for: start) {- throw TokenCacheError.incompleteJournal- }- startsByRoot[turn.rootID] = start- }- }-}--private nonisolated struct TokenAggregateCache: Codable {- static let currentVersion = 1- var version = Self.currentVersion- var turns: [String: TokenTurn]- var files: [String: TokenJournalCheckpoint]- var recoveredTurnIDs: [String]?-}--private nonisolated struct TokenJournalCheckpoint: Codable {- let size: UInt64- let identifier: UInt64- let modificationTime: Int64-}--nonisolated struct TokenRebuildResult {- let dateKeys: [String]- let turnCount: Int- var failedDateKeys: [String] = []- var turns: [TokenTurn]?- static let empty = Self(dateKeys: [], turnCount: 0)-}--nonisolated struct TokenFileCache<Value: Codable & Equatable> {- let url: URL- private var data: Data?- private var value: Value?-- init(url: URL) {- self.url = url- }-- mutating func load(validate: (Data) throws -> Void = { _ in }) throws -> Value? {- guard FileManager.default.fileExists(atPath: url.path) else {- data = nil- value = nil- return nil- }- let current = try Data(contentsOf: url)- if current == data {- return value- }- try validate(current)- let decoded = try JSONLines.decoder.decode(Value.self, from: current)- data = current- value = decoded- return decoded- }-- mutating func save(_ updated: Value) throws {- let current = try? Data(contentsOf: url)- if let current, current == data, updated == value {- return- }- let encoded = try JSONLines.stableEncoder.encode(updated)- if current != encoded {- try encoded.write(to: url, options: .atomic)- }- data = encoded- value = updated- }-}--nonisolated enum TokenCacheError: Error { case unsupportedVersion(Int), incompleteJournal, rootIdentityConflict }diff --git a/CodexBar/Services/History/TokenUsageHistoryStore.swift b/CodexBar/Services/History/TokenUsageHistoryStore.swiftnew file mode 100644index 0000000..14b59aa--- /dev/null+++ b/CodexBar/Services/History/TokenUsageHistoryStore.swift@@ -0,0 +1,180 @@+import Foundation+import os++/// 原始观测与派生 Token 状态分别提交, 采集租约独立于数据库写锁+actor TokenUsageHistoryStore {+ private let directoryURL: URL+ private var threadSnapshots: [String: TokenUsageThreadSnapshot] = [:]+ private var snapshotAccess: [String: Date] = [:]+ private nonisolated let recordingLease = OSAllocatedUnfairLock<Int32?>(initialState: nil)++ init(directoryURL: URL = HistoryStorage.directoryURL()) {+ self.directoryURL = directoryURL+ }++ deinit {+ recordingLease.withLock {+ if let lease = $0 {+ JSONFileStorage.releaseLock(lease)+ }+ }+ }++ /// 租约不进入历史读写队列, 慢聚合不能阻塞连接启动和退出+ nonisolated func acquireRecordingLease() throws -> Bool {+ try recordingLease.withLock { lease in+ if lease != nil {+ return true+ }+ lease = try JSONFileStorage.acquireLock(in: directoryURL.appendingPathComponent("Locks"), name: "tokenUsage.lock", nonblocking: true)+ return lease != nil+ }+ }++ nonisolated func releaseRecordingLease() {+ recordingLease.withLock { lease in+ if let lease {+ JSONFileStorage.releaseLock(lease)+ }+ lease = nil+ }+ }++ func appendObservation(_ observation: TokenUsageObservation, now: Date = Date()) throws {+ try HistoryDatabase(directoryURL: directoryURL).append(AppServerEventRecord(tokenUsage: observation, recordedAt: now))+ }++ func record(_ sample: TokenUsageSample) throws -> TokenUsageThreadSnapshot {+ let database = try HistoryDatabase(directoryURL: directoryURL)+ try database.appendTokenUsage(sample)+ return try snapshot(threadID: sample.threadID, database: database)+ }++ func snapshot(threadID: String) throws -> TokenUsageThreadSnapshot {+ try snapshot(threadID: threadID, database: HistoryDatabase(directoryURL: directoryURL))+ }++ private func snapshot(threadID: String, database: HistoryDatabase) throws -> TokenUsageThreadSnapshot {+ let result = try database.tokenUsageSnapshot(threadSnapshots[threadID] ?? TokenUsageThreadSnapshot(threadID: threadID))+ threadSnapshots[threadID] = result+ snapshotAccess[threadID] = Date()+ if threadSnapshots.count > 128, let oldest = snapshotAccess.min(by: { $0.value < $1.value })?.key {+ threadSnapshots[oldest] = nil+ snapshotAccess[oldest] = nil+ }+ return result+ }++ func refresh(now: Date = Date()) throws -> [TokenUsageAggregate] {+ try loadSnapshot(now: now).aggregates+ }++ func loadSnapshot(now: Date = Date()) throws -> TokenUsageHistorySnapshot {+ try readSnapshot(now: now)+ }++ private nonisolated func readSnapshot(now: Date) throws -> TokenUsageHistorySnapshot {+ let database = try HistoryDatabase(directoryURL: directoryURL)+ for date in try database.pendingTokenUsageDates() {+ do {+ _ = try database.aggregateTokenUsage(date: date)+ } catch is CancellationError {+ throw CancellationError()+ } catch {+ // 单日失败保留上次提交结果, 其他日期继续处理+ }+ }+ return try database.transaction {+ try TokenUsageHistorySnapshot(+ aggregates: retained(database.tokenUsageAggregates(), now: now),+ coverage: database.pendingTokenUsageDates().isEmpty ? .complete : .partial+ )+ }+ }++ func persistedAggregates(now: Date = Date()) throws -> [TokenUsageAggregate] {+ try retained(HistoryDatabase(directoryURL: directoryURL).tokenUsageAggregates(), now: now)+ }++ func rebuild(for dateKeys: [String]) throws -> TokenUsageRebuildResult {+ let database = try HistoryDatabase(directoryURL: directoryURL)+ let available = try Set(database.tokenUsageDates())+ var result = TokenUsageRebuildResult.empty+ for date in Set(dateKeys).intersection(available).sorted() {+ do {+ let aggregate = try database.aggregateTokenUsage(date: date, rebuilding: true)+ result.dateKeys.append(date)+ result.eventCount += aggregate.eventCount+ } catch is CancellationError {+ throw CancellationError()+ } catch {+ result.failedDateKeys.append(date)+ }+ }+ return result+ }++ private nonisolated func retained(_ aggregates: [TokenUsageAggregate], now: Date) -> [TokenUsageAggregate] {+ let cutoff = HistoryStorage.dateKey(for: HistoryStorage.retentionCutoffDate(today: now))+ return aggregates.filter { $0.date >= cutoff }+ }+}++nonisolated struct TokenUsageHistorySnapshot {+ let aggregates: [TokenUsageAggregate]+ let coverage: TokenUsageReplayCoverage+}++nonisolated enum TokenUsageReplayCoverage: Equatable {+ case complete, partial, unavailable+ var isComplete: Bool {+ self == .complete+ }+}++nonisolated struct TokenUsageRebuildResult {+ var dateKeys: [String] = []+ var eventCount = 0+ var failedDateKeys: [String] = []+ static let empty = Self()+}++nonisolated struct TokenUsageFileCache<Value: Codable & Equatable> {+ let url: URL+ private var data: Data?+ private var value: Value?++ init(url: URL) {+ self.url = url+ }++ mutating func load(validate: (Data) throws -> Void = { _ in }) throws -> Value? {+ guard FileManager.default.fileExists(atPath: url.path) else {+ data = nil+ value = nil+ return nil+ }+ let current = try Data(contentsOf: url)+ if current == data {+ return value+ }+ try validate(current)+ let decoded = try JSONLines.decoder.decode(Value.self, from: current)+ data = current+ value = decoded+ return decoded+ }++ mutating func save(_ updated: Value) throws {+ let current = try? Data(contentsOf: url)+ if let current, current == data, updated == value {+ return+ }+ let encoded = try JSONLines.stableEncoder.encode(updated)+ if current != encoded {+ try encoded.write(to: url, options: .atomic)+ }+ data = encoded+ value = updated+ }+}diff --git a/CodexBar/Services/Support/CountResolution.swift b/CodexBar/Services/Support/CountResolution.swiftindex 9541bdf..7f62e4d 100644--- a/CodexBar/Services/Support/CountResolution.swift+++ b/CodexBar/Services/Support/CountResolution.swift@@ -13,12 +13,4 @@ nonisolated enum CountResolution {return compactedCount == 0 ? 0 : nil}-- static func resolvedCount(- compactedCount: Int?,- identifiers: [String]? = nil,- fallback: Int- ) -> Int {- preferredCount(compactedCount: compactedCount, identifiers: identifiers) ?? fallback- }}diff --git a/CodexBar/Services/Support/JSONLines.swift b/CodexBar/Services/Support/JSONLines.swiftindex a2d9a4c..fec2ed7 100644--- a/CodexBar/Services/Support/JSONLines.swift+++ b/CodexBar/Services/Support/JSONLines.swift@@ -1,16 +1,9 @@import Foundation-/// 简单 JSONL 工具, 读取时跳过坏行以保护历史统计可用性+/// 落盘 JSON 与事件行共用的序列化配置nonisolated enum JSONLines {static let newlineByte: UInt8 = 0x0A- /// RFC 8259 允许出现在值前后的空白, 用于识别只含空白的空行- private static let jsonWhitespaceBytes: Set<UInt8> = [0x20, 0x09, 0x0A, 0x0D]-- static func isBlankLine(_ data: Data) -> Bool {- !data.contains(where: { !jsonWhitespaceBytes.contains($0) })- }-/// 落盘 JSON 统一的稳定输出配置, 保证文件可 diff 且格式一致/// 配置后不再修改, encode 可重入, 可跨并发域缓存共享static let stableEncoder: JSONEncoder = {@@ -40,46 +33,4 @@ nonisolated enum JSONLines {static func storageDate(_ date: Date) -> Date {Date(timeIntervalSince1970: (date.timeIntervalSince1970 * 1000).rounded(.down) / 1000)}-- /// 按行解码 JSONL: 切分换行, 跳过空行与解码失败的行- static func decode<T: Decodable>(_ type: T.Type = T.self, from data: Data) -> [T] {- decodeWithFailures(type, from: data).values- }-- static func decodeWithFailures<T: Decodable>(- _: T.Type = T.self,- from data: Data- ) -> JSONLinesDecodeResult<T> {- var values = [T]()- var failedLineCount = 0- var compatibilityError: StorageCompatibilityError?-- // 逐行独立解码: 进程被杀或断电会留下截断的多字节序列- // 整块处理时一处损坏会连带丢掉同一次读取里的所有完好事件, 且调用方仍会推进 offset- // 直接把字节切片交给 JSONDecoder: 它自己跳过首尾空白并拒收非法 UTF-8,- // 省掉每行 String 转换 + trim + 再编码回 Data 的三次拷贝 (单次读取可达上万行)- for line in data.split(separator: newlineByte) {- guard !isBlankLine(line) else {- continue- }- do {- try values.append(decoder.decode(T.self, from: Data(line)))- } catch StorageCompatibilityError.incompleteSource {- failedLineCount += 1- } catch let error as StorageCompatibilityError {- compatibilityError = error- } catch {- failedLineCount += 1- }- }-- return JSONLinesDecodeResult(values: values, failedLineCount: failedLineCount, compatibilityError: compatibilityError)- }-}--/// 解码结果同时返回坏行数量, 维护流程据此触发重建-nonisolated struct JSONLinesDecodeResult<T> {- let values: [T]- let failedLineCount: Int- var compatibilityError: StorageCompatibilityError?}diff --git a/CodexBar/Services/Sync/ActivityDataRecordCodec.swift b/CodexBar/Services/Sync/ActivityDataRecordCodec.swiftnew file mode 100644index 0000000..95e2f1c--- /dev/null+++ b/CodexBar/Services/Sync/ActivityDataRecordCodec.swift@@ -0,0 +1,119 @@+import CloudKit+import Foundation++// MARK: - CloudKit 记录编解码++nonisolated enum ActivityDataRecordCodec {+ static let schemaVersion = 1+ static let metadataSchemaVersion = 1++ enum RecordTypes {+ static let metadata = "Metadata"+ static let activityData = "ActivityData"+ }++ enum FieldKeys {+ static let salt = "salt"+ static let schemaVersion = "schemaVersion"+ static let aggregationVersion = "aggregationVersion"+ static let deviceID = "deviceID"+ static let date = "date"+ static let activityData = "activityData"+ static let updatedAt = "updatedAt"+ }++ static func apply(+ _ aggregate: SyncedActivityData,+ deviceID: String,+ to record: CKRecord+ ) throws {+ record[FieldKeys.schemaVersion] = schemaVersion as CKRecordValue+ record[FieldKeys.aggregationVersion] = aggregate.aggregationVersion as CKRecordValue+ record[FieldKeys.deviceID] = deviceID as CKRecordValue+ record[FieldKeys.date] = aggregate.date as CKRecordValue+ record[FieldKeys.activityData] = try JSONLines.stableEncoder.encode(aggregate.counts) as CKRecordValue+ record[FieldKeys.updatedAt] = Date() as CKRecordValue+ }++ static func remoteDailyRecord(from record: CKRecord) throws -> ActivityDataSyncRecord? {+ guard record.recordType == RecordTypes.activityData else { return nil }+ try StorageVersion.require(optionalIntValue(record[FieldKeys.schemaVersion]) ?? -1, current: schemaVersion, name: "CloudActivityData")+ try AggregationVersion.require(optionalIntValue(record[FieldKeys.aggregationVersion]) ?? -1, current: AggregationVersion.activityData, name: "activityData")+ guard+ let deviceID = record[FieldKeys.deviceID] as? String,+ let date = record[FieldKeys.date] as? String,+ HistoryStorage.isValidDateKey(date) else {+ throw ActivityDataSyncError.invalidRecordIdentity+ }++ guard let activityDataPayload = record[FieldKeys.activityData] as? Data else {+ throw StorageCompatibilityError.incompleteSource+ }+ let activityData = try JSONDecoder().decode(ActivityDataCounts.self, from: activityDataPayload)+ let quantities = [+ activityData.eventCount, activityData.turnStartedCount, activityData.turnCompletedCount, activityData.turnAbortedCount,+ activityData.toolStartedCount, activityData.toolCompletedCount, activityData.approvalRequestedCount,+ activityData.compactionStartedCount, activityData.compactionCompletedCount, activityData.subagentStartedCount,+ activityData.subagentEndedCount, activityData.threadCount, activityData.turnCount+ ]+ guard quantities.compactMap(\.self).allSatisfy({ $0 >= 0 }),+ activityData.projectCounts.values.allSatisfy({ $0 >= 0 }),+ activityData.modelCounts.values.allSatisfy({ $0 >= 0 }) else {+ throw StorageCompatibilityError.incompleteSource+ }+ let aggregate = SyncedActivityData(+ aggregationVersion: optionalIntValue(record[FieldKeys.aggregationVersion]) ?? -1,+ date: date,+ eventCount: activityData.eventCount,+ turnStartedCount: activityData.turnStartedCount,+ turnCompletedCount: activityData.turnCompletedCount,+ turnAbortedCount: activityData.turnAbortedCount,+ toolStartedCount: activityData.toolStartedCount,+ toolCompletedCount: activityData.toolCompletedCount,+ approvalRequestedCount: activityData.approvalRequestedCount,+ compactionStartedCount: activityData.compactionStartedCount,+ compactionCompletedCount: activityData.compactionCompletedCount,+ subagentStartedCount: activityData.subagentStartedCount,+ subagentEndedCount: activityData.subagentEndedCount,+ threadCount: activityData.threadCount,+ turnCount: activityData.turnCount,+ projectCounts: activityData.projectCounts,+ modelCounts: activityData.modelCounts+ )++ return try ActivityDataSyncRecord(+ deviceID: deviceID,+ activityData: aggregate,+ updatedAt: record[FieldKeys.updatedAt] as? Date ?? record.modificationDate,+ recordName: record.recordID.recordName+ )+ }++ static func optionalIntValue(_ value: CKRecordValue?) -> Int? {+ if let number = value as? NSNumber {+ return number.intValue+ }+ if let value = value as? Int {+ return value+ }+ return nil+ }++ private struct ActivityDataCounts: Decodable {+ let eventCount: Int?+ let turnStartedCount: Int?+ let turnCompletedCount: Int?+ let turnAbortedCount: Int?+ let toolStartedCount: Int?+ let toolCompletedCount: Int?+ let approvalRequestedCount: Int?+ let compactionStartedCount: Int?+ let compactionCompletedCount: Int?+ let subagentStartedCount: Int?+ let subagentEndedCount: Int?+ let threadCount: Int?+ let turnCount: Int?+ let projectCounts: [String: Int]+ let modelCounts: [String: Int]+ }+}diff --git a/CodexBar/Services/Sync/ActivitySyncStore.swift b/CodexBar/Services/Sync/ActivityDataSyncStore.swiftsimilarity index 54%rename from CodexBar/Services/Sync/ActivitySyncStore.swiftrename to CodexBar/Services/Sync/ActivityDataSyncStore.swiftindex 9fee059..3eb8238 100644--- a/CodexBar/Services/Sync/ActivitySyncStore.swift+++ b/CodexBar/Services/Sync/ActivityDataSyncStore.swift@@ -1,51 +1,51 @@import CloudKitimport Foundation-nonisolated struct ActivitySyncStore {+nonisolated struct ActivityDataSyncStore {let directoryURL: URLprivate var cacheDirectoryURL: URL {- directoryURL.appendingPathComponent("Activity", isDirectory: true)+ directoryURL.appendingPathComponent("activityData", isDirectory: true)}private var cacheURL: URL {cacheDirectoryURL.appendingPathComponent("cache.json", isDirectory: false)}- func load() throws -> ActivitySyncCache {+ func load() throws -> ActivityDataSyncCache {try JSONFileStorage.withLock(in: cacheDirectoryURL) { try read() }}- func loadState() -> SyncState {- (try? load().state) ?? SyncState()+ func loadState() -> ActivityDataSyncState {+ (try? load().state) ?? ActivityDataSyncState()}- func saveState(_ state: SyncState) throws {+ func saveState(_ state: ActivityDataSyncState) throws {try update { $0.state = state }}- func loadCachedRecords() -> [ActivitySyncRecord] {- (try? load().records) ?? []- }-- func saveCachedRecords(_ records: [ActivitySyncRecord], state: SyncState) throws {- try update {- $0.records = Self.sorted(records)- $0.state = state+ func saveConfirmedRecords(_ records: [ActivityDataSyncRecord], lastUploadAt: Date?) throws {+ try update { cache in+ for record in records {+ cache.records[record.id] = record+ }+ if let lastUploadAt {+ cache.state.lastUploadAt = lastUploadAt+ }}}/// 数据和对应游标必须一起提交, 不能让新游标指向尚未保存的数据- func saveFetchedRecords(_ records: [ActivitySyncRecord], cursor: CKServerChangeToken?) throws {+ func saveFetchedRecords(_ activityData: [String: ActivityDataSyncRecord], cursor: CKServerChangeToken?) throws {let data = try cursor.map { try NSKeyedArchiver.archivedData(withRootObject: $0, requiringSecureCoding: true) }try update {- $0.records = Self.sorted(records)+ $0.records = activityData$0.cursor = data}}- func reset(state: SyncState) throws {- try update { $0 = ActivitySyncCache(state: state) }+ func reset(state: ActivityDataSyncState) throws {+ try update { $0 = ActivityDataSyncCache(state: state) }}func prune(now: Date) throws {@@ -57,18 +57,16 @@ nonisolated struct ActivitySyncStore {}}- private func read() throws -> ActivitySyncCache {- let cache: ActivitySyncCache+ private func read() throws -> ActivityDataSyncCache {+ let cache: ActivityDataSyncCachedo {- guard let data = try readData() else { return ActivitySyncCache() }- let header = try JSONLines.decoder.decode(ActivitySyncCacheHeader.self, from: data)- guard header.version == ActivitySyncCache.currentVersion else { throw SyncRecovery.Failure.unsupportedCacheVersion }- cache = try JSONLines.decoder.decode(ActivitySyncCache.self, from: data)+ guard let data = try readData() else { return ActivityDataSyncCache(state: ActivityDataSyncState()) }+ cache = try JSONLines.decoder.decode(ActivityDataSyncCache.self, from: data)} catch is DecodingError {- // 损坏时整体失效, 不保留脱离缓存数据的游标或上传摘要- return ActivitySyncCache()+ // 损坏时整体失效, 不保留脱离缓存数据的游标+ return ActivityDataSyncCache(state: ActivityDataSyncState())}- guard cache.state.containerIdentifier == SyncCloudKit.containerIdentifier else { return ActivitySyncCache() }+ guard cache.state.containerIdentifier == SyncCloudKit.containerIdentifier else { return ActivityDataSyncCache(state: ActivityDataSyncState()) }return cache}@@ -80,7 +78,7 @@ nonisolated struct ActivitySyncStore {}}- private func update(_ body: (inout ActivitySyncCache) throws -> Void) throws {+ private func update(_ body: (inout ActivityDataSyncCache) throws -> Void) throws {try JSONFileStorage.withLock(in: cacheDirectoryURL) {var cache = try read()try body(&cache)@@ -88,48 +86,34 @@ nonisolated struct ActivitySyncStore {try JSONFileStorage.save(cache, to: cacheURL)}}-- private static func sorted(_ records: [ActivitySyncRecord]) -> [ActivitySyncRecord] {- records.sorted { ($0.deviceID, $0.date, $0.id) < ($1.deviceID, $1.date, $1.id) }- }}-private nonisolated struct ActivitySyncCacheHeader: Decodable { let version: Int }--nonisolated struct ActivitySyncCache: Codable, Equatable {- static let currentVersion = 1- var version = currentVersion- var state = SyncState()- var records: [ActivitySyncRecord] = []- var cursor: Data?+typealias ActivityDataSyncCache = SyncCache<ActivityDataSyncRecord, ActivityDataSyncState>+nonisolated extension SyncCache where Record == ActivityDataSyncRecord, State == ActivityDataSyncState {@discardableResultmutating func prune(now: Date = Date()) -> Bool {let cutoff = HistoryStorage.dateKey(for: HistoryStorage.retentionCutoffDate(today: now))- let previousCount = records.count + state.hashByDate.count- records.removeAll { $0.date < cutoff }- state.hashByDate = state.hashByDate.filter { $0.key >= cutoff }- return previousCount != records.count + state.hashByDate.count+ let previousCount = records.count+ records = records.filter { $0.value.date >= cutoff }+ return previousCount != records.count}}-nonisolated struct SyncState: Codable, Equatable {+nonisolated struct ActivityDataSyncState: Codable, Equatable {var containerIdentifier: String?var deviceID: String?- var hashByDate: [String: String]var lastUploadAt: Date?var lastPrunedDate: String?init(containerIdentifier: String? = SyncCloudKit.containerIdentifier,deviceID: String? = nil,- hashByDate: [String: String] = [:],lastUploadAt: Date? = nil,lastPrunedDate: String? = nil) {self.containerIdentifier = containerIdentifierself.deviceID = deviceID- self.hashByDate = hashByDateself.lastUploadAt = lastUploadAtself.lastPrunedDate = lastPrunedDate}@@ -139,8 +123,6 @@ nonisolated struct SyncState: Codable, Equatable {containerIdentifier = try container.decodeIfPresent(String.self, forKey: .containerIdentifier)deviceID = try container.decodeIfPresent(String.self, forKey: .deviceID)- hashByDate = try container.decodeIfPresent([String: String].self, forKey: .hashByDate)- ?? [:]lastUploadAt = try container.decodeIfPresent(Date.self, forKey: .lastUploadAt)lastPrunedDate = try container.decodeIfPresent(String.self, forKey: .lastPrunedDate)}@@ -148,7 +130,6 @@ nonisolated struct SyncState: Codable, Equatable {private enum CodingKeys: String, CodingKey {case containerIdentifiercase deviceID- case hashByDatecase lastUploadAtcase lastPrunedDate}diff --git a/CodexBar/Services/Sync/ActivityRecordCodec.swift b/CodexBar/Services/Sync/ActivityRecordCodec.swiftdeleted file mode 100644index ab6c8c3..0000000--- a/CodexBar/Services/Sync/ActivityRecordCodec.swift+++ /dev/null@@ -1,135 +0,0 @@-import CloudKit-import Foundation--// MARK: - CloudKit 记录编解码--nonisolated enum ActivityRecordCodec {- static let version = 1- static let metadataVersion = 1-- enum RecordTypes {- static let metadata = "Metadata"- static let activity = "Activity"- }-- enum FieldKeys {- static let salt = "salt"- static let version = "version"- static let aggregationVersion = "aggregationVersion"- static let sourceCheckpoint = "sourceCheckpoint"- static let deviceID = "deviceID"- static let date = "date"- static let generationID = "generationID"- static let eventCount = "eventCount"- static let turnStartedCount = "turnStartedCount"- static let turnCompletedCount = "turnCompletedCount"- static let turnAbortedCount = "turnAbortedCount"- static let toolStartedCount = "toolStartedCount"- static let toolCompletedCount = "toolCompletedCount"- static let approvalRequestedCount = "approvalRequestedCount"- static let compactionStartedCount = "compactionStartedCount"- static let compactionCompletedCount = "compactionCompletedCount"- static let subagentStartedCount = "subagentStartedCount"- static let subagentEndedCount = "subagentEndedCount"- static let threadCount = "threadCount"- static let turnCount = "turnCount"- static let projectCounts = "projectCounts"- static let modelCounts = "modelCounts"- static let updatedAt = "updatedAt"- }-- static func apply(- _ aggregate: SyncedActivity,- deviceID: String,- to record: CKRecord- ) {- record[FieldKeys.version] = version as CKRecordValue- record[FieldKeys.aggregationVersion] = aggregate.aggregationVersion as CKRecordValue- record[FieldKeys.deviceID] = deviceID as CKRecordValue- record[FieldKeys.date] = aggregate.date as CKRecordValue- record[FieldKeys.generationID] = aggregate.generationID as CKRecordValue?- record[FieldKeys.eventCount] = aggregate.eventCount as CKRecordValue?- record[FieldKeys.turnStartedCount] = aggregate.turnStartedCount as CKRecordValue?- record[FieldKeys.turnCompletedCount] = aggregate.turnCompletedCount as CKRecordValue?- record[FieldKeys.turnAbortedCount] = aggregate.turnAbortedCount as CKRecordValue?- record[FieldKeys.toolStartedCount] = aggregate.toolStartedCount as CKRecordValue?- record[FieldKeys.toolCompletedCount] = aggregate.toolCompletedCount as CKRecordValue?- record[FieldKeys.approvalRequestedCount] = aggregate.approvalRequestedCount as CKRecordValue?- record[FieldKeys.compactionStartedCount] = aggregate.compactionStartedCount as CKRecordValue?- record[FieldKeys.compactionCompletedCount] = aggregate.compactionCompletedCount as CKRecordValue?- record[FieldKeys.subagentStartedCount] = aggregate.subagentStartedCount as CKRecordValue?- record[FieldKeys.subagentEndedCount] = aggregate.subagentEndedCount as CKRecordValue?- record[FieldKeys.threadCount] = aggregate.threadCount as CKRecordValue?- record[FieldKeys.turnCount] = aggregate.turnCount as CKRecordValue?- record[FieldKeys.projectCounts] = countsData(aggregate.projectCounts) as CKRecordValue- record[FieldKeys.modelCounts] = countsData(aggregate.modelCounts) as CKRecordValue- record[FieldKeys.sourceCheckpoint] = aggregate.sourceCheckpoint.flatMap { try? JSONLines.stableEncoder.encode($0) } as CKRecordValue?- record[FieldKeys.updatedAt] = Date() as CKRecordValue- }-- static func remoteDailyRecord(from record: CKRecord) throws -> ActivitySyncRecord? {- guard record.recordType == RecordTypes.activity else { return nil }- try StorageVersion.require(optionalIntValue(record[FieldKeys.version]) ?? -1, current: version, name: "CloudActivity")- try AggregationVersion.require(optionalIntValue(record[FieldKeys.aggregationVersion]) ?? -1, current: AggregationVersion.activity, name: "Activity")- guard- let deviceID = record[FieldKeys.deviceID] as? String,- let date = record[FieldKeys.date] as? String,- HistoryStorage.isValidDateKey(date) else {- throw ActivitySyncError.invalidRecordIdentity- }-- guard let checkpointData = record[FieldKeys.sourceCheckpoint] as? Data else {- throw StorageCompatibilityError.incompleteSource- }- let checkpoint = try JSONDecoder().decode(ActivitySourceCheckpoint.self, from: checkpointData)- let aggregate = try SyncedActivity(- aggregationVersion: optionalIntValue(record[FieldKeys.aggregationVersion]) ?? -1,- sourceCheckpoint: checkpoint,- date: date,- generationID: record[FieldKeys.generationID] as? String,- eventCount: optionalIntValue(record[FieldKeys.eventCount]),- turnStartedCount: optionalIntValue(record[FieldKeys.turnStartedCount]),- turnCompletedCount: optionalIntValue(record[FieldKeys.turnCompletedCount]),- turnAbortedCount: optionalIntValue(record[FieldKeys.turnAbortedCount]),- toolStartedCount: optionalIntValue(record[FieldKeys.toolStartedCount]),- toolCompletedCount: optionalIntValue(record[FieldKeys.toolCompletedCount]),- approvalRequestedCount: optionalIntValue(record[FieldKeys.approvalRequestedCount]),- compactionStartedCount: optionalIntValue(record[FieldKeys.compactionStartedCount]),- compactionCompletedCount: optionalIntValue(record[FieldKeys.compactionCompletedCount]),- subagentStartedCount: optionalIntValue(record[FieldKeys.subagentStartedCount]),- subagentEndedCount: optionalIntValue(record[FieldKeys.subagentEndedCount]),- threadCount: optionalIntValue(record[FieldKeys.threadCount]),- turnCount: optionalIntValue(record[FieldKeys.turnCount]),- projectCounts: counts(from: record[FieldKeys.projectCounts]),- modelCounts: counts(from: record[FieldKeys.modelCounts])- )-- return try ActivitySyncRecord(- deviceID: deviceID,- daily: aggregate,- updatedAt: record[FieldKeys.updatedAt] as? Date ?? record.modificationDate,- recordName: record.recordID.recordName- )- }-- static func optionalIntValue(_ value: CKRecordValue?) -> Int? {- if let number = value as? NSNumber {- return number.intValue- }- if let value = value as? Int {- return value- }- return nil- }-- static func counts(from value: CKRecordValue?) throws -> [String: Int] {- guard let data = value as? Data else { throw StorageCompatibilityError.incompleteSource }- let counts = try JSONDecoder().decode([String: Int].self, from: data)- guard counts.values.allSatisfy({ $0 >= 0 }) else { throw StorageCompatibilityError.incompleteSource }- return counts- }-- static func countsData(_ counts: [String: Int]) -> Data {- (try? JSONLines.stableEncoder.encode(counts)) ?? Data("{}".utf8)- }-}diff --git a/CodexBar/Services/Sync/SyncService.swift b/CodexBar/Services/Sync/SyncService.swiftindex 582afe4..9db0d3c 100644--- a/CodexBar/Services/Sync/SyncService.swift+++ b/CodexBar/Services/Sync/SyncService.swift@@ -21,41 +21,36 @@ private nonisolated enum SyncStage: String {case devicecase fetchcase upload- case tokens+ case tokenUsagecase prune}-/// 将本机 daily.jsonl 中不含 threadIDs 和 turnIds 的聚合行同步到 CloudKit private database+/// 同步各设备的每日汇总, 本机数量只覆盖本设备记录actor SyncService {private let container: CKContainer?private let suppliedDatabase: (any SyncDatabase)?private lazy var database: any SyncDatabase = suppliedDatabase ?? (container ?? SyncCloudKit.makeContainer()).privateCloudDatabaseprivate let directoryURL: URL- private let activityEventsDirectoryURL: URLprivate let isEnabled: @Sendable () -> Bool- private lazy var tokenSync = TokenSync(- database: database, directoryURL: directoryURL.appendingPathComponent("Tokens"), isEnabled: isEnabled+ private lazy var tokenUsageSync = TokenUsageSync(+ database: database, directoryURL: directoryURL.appendingPathComponent("tokenUsage"), isEnabled: isEnabled)// zone 存在性跨轮缓存, 共享故障后重新确认- // salt 只在单轮内复用, 每轮确认同名 zone 是否已经被其他设备重建private var isSyncZoneConfirmed = false- private var cachedAccountSalt: Data?- private var lastActivityCacheRetentionCutoff: Date?- private var lastTokenCacheRetentionCutoff: Date?- private var isActivityCacheComplete = true+ private var lastActivityDataCacheRetentionCutoff: Date?+ private var lastTokenUsageCacheRetentionCutoff: Date?+ private var isActivityDataCacheComplete = trueinit(container: CKContainer? = nil,database: (any SyncDatabase)? = nil,directoryURL: URL = HistoryStorage.syncDirectoryURL(),- activityEventsDirectoryURL: URL? = nil,isEnabled: @escaping @Sendable () -> Bool = { SyncSettings.isEnabled() }) {self.container = containersuppliedDatabase = databaseself.directoryURL = directoryURL- self.activityEventsDirectoryURL = activityEventsDirectoryURL ?? HistoryStorage.eventsDirectoryURL(in: directoryURL.deletingLastPathComponent())self.isEnabled = isEnabled}@@ -63,55 +58,51 @@ actor SyncService {func pruneLocalCaches(now: Date = Date()) {let cutoff = HistoryStorage.retentionCutoffDate(today: now)- if lastActivityCacheRetentionCutoff != cutoff {+ if lastActivityDataCacheRetentionCutoff != cutoff {do {- try activityStore.prune(now: now)- lastActivityCacheRetentionCutoff = cutoff+ try activityDataStore.prune(now: now)+ lastActivityDataCacheRetentionCutoff = cutoff} catch {AppLog.sync.error("活动同步缓存过期清理失败")}}- if lastTokenCacheRetentionCutoff != cutoff {+ if lastTokenUsageCacheRetentionCutoff != cutoff {do {- try TokenSync.pruneLocalCache(in: directoryURL.appendingPathComponent("Tokens"), now: now)- lastTokenCacheRetentionCutoff = cutoff+ try TokenUsageSync.pruneLocalCache(in: directoryURL.appendingPathComponent("tokenUsage"), now: now)+ lastTokenUsageCacheRetentionCutoff = cutoff} catch {AppLog.sync.error("Token 同步缓存过期清理失败")}}}- func snapshotFromCacheIfEnabled(localTokenTurns: [TokenTurn] = []) async -> SyncSnapshot {+ func snapshotFromCacheIfEnabled(localTokenUsageAggregates: [TokenUsageAggregate] = []) async -> SyncSnapshot {guard isEnabled() else {return .disabled}- let state = activityStore.loadState()- return await snapshot(from: state, localTokenTurns: localTokenTurns)+ let state = activityDataStore.loadState()+ return await snapshot(from: state, localTokenUsageAggregates: localTokenUsageAggregates)}- func tokenRecoveryBaselineIfEnabled() async -> TokenHistoryBaseline? {- guard isEnabled() else { return nil }- return await tokenSync.recoveryBaseline(accountScopedDeviceID: activityStore.loadState().deviceID)- }-- func hasPendingActivityUpdates(local: [ActivityAggregate]) -> Bool {+ func hasPendingActivityDataUpdates(local: [ActivityDataAggregate]) -> Bool {guard isEnabled() else { return false }- guard let state = try? activityStore.load().state else { return !local.isEmpty }+ guard let cache = try? activityDataStore.load(), let deviceID = cache.state.deviceID else { return !local.isEmpty }return local.contains { aggregate in- (try? hash(for: aggregate.syncedAggregate)) != state.hashByDate[aggregate.date]+ let id = ActivityDataSyncRecord.recordName(deviceID: deviceID, date: aggregate.date)+ return cache.records[id]?.activityData != aggregate.syncedAggregate}}- func hasPendingTokenUpdates(local: [TokenTurn]) async -> Bool {+ func hasPendingTokenUsageUpdates(local: [TokenUsageAggregate]) async -> Bool {guard isEnabled() else { return false }- return await tokenSync.hasPendingUpdates(local: local, accountScopedDeviceID: activityStore.loadState().deviceID)+ return await tokenUsageSync.hasPendingUpdates(local: local, accountScopedDeviceID: activityDataStore.loadState().deviceID)}func synchronizeIfEnabled(- localAggregates: [ActivityAggregate],- localTokenTurns: [TokenTurn] = [],- recoveredTokenIDs: Set<String> = [],+ localActivityDataAggregates: [ActivityDataAggregate],+ localTokenUsageAggregates: [TokenUsageAggregate] = [],+ isTokenUsageSourceComplete: Bool = true,trigger: LogTrigger) async -> SyncSnapshot {guard isEnabled(), !Task.isCancelled else {@@ -133,19 +124,19 @@ actor SyncService {)}- var state: SyncState- do { state = try activityStore.load().state } catch {+ var state: ActivityDataSyncState+ do { state = try activityDataStore.load().state } catch {failureMessage = SyncFailureReason.classify(error).message- return SyncSnapshot(records: [], currentDeviceID: nil, isActivityComplete: false)+ return SyncSnapshot(activityData: [], currentDeviceID: nil, isActivityDataComplete: false)}// 显式开启同步允许恢复缺失的 zone, 同一轮最多重新执行一次- cachedAccountSalt = nilfor attempt in 0 ..< 2 {do {try SyncCancellation.check(isEnabled: isEnabled)failureMessage = try await synchronizeOnce(- localAggregates: localAggregates, localTokenTurns: localTokenTurns, recoveredTokenIDs: recoveredTokenIDs,+ localActivityDataAggregates: localActivityDataAggregates, localTokenUsageAggregates: localTokenUsageAggregates,+ isTokenUsageSourceComplete: isTokenUsageSourceComplete,state: &state, stage: &stage, trigger: trigger)didSucceed = failureMessage == nil@@ -172,19 +163,19 @@ actor SyncService {logSyncFailed(trigger: trigger, stage: stage, elapsed: duration.elapsed, reason: reason, detail: failure.localizedDescription)failureMessage = reason.message}- try? activityStore.saveState(state)+ try? activityDataStore.saveState(state)break}}guard !Task.isCancelled, isEnabled() else { return .disabled }- let latestState = activityStore.loadState()- return await snapshot(from: latestState, localTokenTurns: localTokenTurns)+ let latestState = activityDataStore.loadState()+ return await snapshot(from: latestState, localTokenUsageAggregates: isTokenUsageSourceComplete ? localTokenUsageAggregates : [])}private func synchronizeOnce(- localAggregates: [ActivityAggregate], localTokenTurns: [TokenTurn], recoveredTokenIDs: Set<String>,- state: inout SyncState, stage: inout SyncStage, trigger: LogTrigger+ localActivityDataAggregates: [ActivityDataAggregate], localTokenUsageAggregates: [TokenUsageAggregate], isTokenUsageSourceComplete: Bool,+ state: inout ActivityDataSyncState, stage: inout SyncStage, trigger: LogTrigger) async throws -> String? {let duration = LogDuration()try await ensureSyncZoneExists(state: &state)@@ -192,21 +183,21 @@ actor SyncService {let deviceID = try await resolveCurrentDeviceID()try await resetStateIfDeviceChanged(deviceID, state: &state)- var localByDate: [String: SyncedActivity] = [:]+ var localByDate: [String: SyncedActivityData] = [:]var confirmedDates = Set<String>()- var activityFailure: Error?+ var activityDataFailure: Error?do {stage = .upload- localByDate = try Self.syncedAggregatesByDate(localAggregates)- confirmedDates = try await synchronizeActivity(- localByDate: localByDate, deviceID: deviceID, state: &state, stage: &stage+ localByDate = try Self.syncedAggregatesByDate(localActivityDataAggregates)+ confirmedDates = try await synchronizeActivityData(+ localByDate: localByDate, state: &state, stage: &stage)} catch {- isActivityCacheComplete = false+ isActivityDataCacheComplete = falseif SyncRecovery.stopsOtherSync(error) {throw error}- activityFailure = error+ activityDataFailure = errorlogSyncFailed(trigger: trigger,stage: stage,@@ -215,19 +206,19 @@ actor SyncService {detail: error.localizedDescription)}- stage = .tokens- try await tokenSync.synchronize(- local: localTokenTurns, recoveredIDs: recoveredTokenIDs, accountScopedDeviceID: deviceID, salt: accountSalt(), zoneID: syncZoneID+ stage = .tokenUsage+ try await tokenUsageSync.synchronize(+ local: localTokenUsageAggregates, accountScopedDeviceID: deviceID, zoneID: syncZoneID, canUpload: isTokenUsageSourceComplete)- if let activityFailure {+ if let activityDataFailure {try SyncCancellation.check(isEnabled: isEnabled)- try activityStore.saveState(state)- return (activityFailure as? ActivitySyncError)?.errorDescription ?? SyncFailureReason.classify(activityFailure).message+ try activityDataStore.saveState(state)+ return (activityDataFailure as? ActivityDataSyncError)?.errorDescription ?? SyncFailureReason.classify(activityDataFailure).message}stage = .prunelet deletedCount = try await pruneCurrentDeviceRecordsIfNeeded(deviceID: deviceID, state: &state)- try activityStore.saveState(state)- // confirmed 是本地与远端已对齐的日期数, 含哈希未变而无需上传的那些+ try activityDataStore.saveState(state)+ // confirmed 是本地与远端已对齐的日期数, 含缓存内容相同而无需上传的日期// 它小于 local 就说明这一轮还有日期没落到云上let elapsed = duration.elapsedlogSyncCompleted(@@ -240,9 +231,9 @@ actor SyncService {return nil}- private func synchronizeActivity(- localByDate: [String: SyncedActivity], deviceID _: String,- state: inout SyncState, stage: inout SyncStage+ private func synchronizeActivityData(+ localByDate: [String: SyncedActivityData],+ state: inout ActivityDataSyncState, stage: inout SyncStage) async throws -> Set<String> {stage = .fetchtry await refreshCacheFromRemote()@@ -253,7 +244,7 @@ actor SyncService {)stage = .fetchtry await refreshCacheFromRemote()- isActivityCacheComplete = true+ isActivityDataCacheComplete = truereturn dates}@@ -301,7 +292,7 @@ actor SyncService {private func resetStateIfDeviceChanged(_ deviceID: String,- state: inout SyncState+ state: inout ActivityDataSyncState) async throws {guard state.deviceID != deviceID else {return@@ -309,71 +300,49 @@ actor SyncService {try await resetSyncState(state: &state)state.deviceID = deviceID- try activityStore.saveState(state)+ try activityDataStore.saveState(state)}- private func resetSyncState(state: inout SyncState) async throws {+ private func resetSyncState(state: inout ActivityDataSyncState) async throws {try SyncCancellation.check(isEnabled: isEnabled)// 先持久化未绑定状态, 中途失败或退出后仍会重新清理, 不把旧缓存当作已恢复- state = SyncState()- try activityStore.reset(state: state)- try await tokenSync.resetCache()+ state = ActivityDataSyncState()+ try activityDataStore.reset(state: state)+ try await tokenUsageSync.resetCache()try SyncCancellation.check(isEnabled: isEnabled)}private static func syncedAggregatesByDate(- _ aggregates: [ActivityAggregate]- ) throws -> [String: SyncedActivity] {- try aggregates.reduce(into: [String: SyncedActivity]()) { result, aggregate in- _ = try aggregate.syncedAggregate.requiredGenerationID()+ _ aggregates: [ActivityDataAggregate]+ ) throws -> [String: SyncedActivityData] {+ try aggregates.reduce(into: [String: SyncedActivityData]()) { result, aggregate in+ guard HistoryStorage.isValidDateKey(aggregate.date) else { throw ActivityDataSyncError.invalidRecordIdentity }result[aggregate.date] = aggregate.syncedAggregate}}- private func snapshot(from state: SyncState, localTokenTurns: [TokenTurn]) async -> SyncSnapshot {- var result = SyncSnapshot(records: [], currentDeviceID: state.deviceID)+ private func snapshot(from state: ActivityDataSyncState, localTokenUsageAggregates: [TokenUsageAggregate]) async -> SyncSnapshot {+ var result = SyncSnapshot(activityData: [], currentDeviceID: state.deviceID)do {- let cache = try activityStore.load()- result = SyncSnapshot(records: Self.filteredRetained(records: cache.records), currentDeviceID: cache.state.deviceID)+ let cache = try activityDataStore.load()+ result = SyncSnapshot(activityData: Self.filteredRetained(records: cache.sortedRecords), currentDeviceID: cache.state.deviceID)} catch {- result.isActivityComplete = false+ result.isActivityDataComplete = false}- result.tokenUsageByDate = try? await tokenSync.snapshot(local: localTokenTurns, accountScopedDeviceID: state.deviceID)- result.isActivityComplete = result.isActivityComplete && isActivityCacheComplete+ result.tokenUsageByDate = try? await tokenUsageSync.snapshot(local: localTokenUsageAggregates, accountScopedDeviceID: state.deviceID)+ result.isActivityDataComplete = result.isActivityDataComplete && isActivityDataCacheCompletereturn result}}private extension SyncService {- struct PendingUpload {- let date: String- let aggregate: SyncedActivity- let hash: String- }-- struct PendingRecord {- let upload: PendingUpload- let recordID: CKRecord.ID- }-- struct RecordToSave {- let upload: PendingUpload- let record: CKRecord- }-struct UploadBatchResult {- var confirmed: [ConfirmedHash] = []+ var confirmed: [ActivityDataSyncRecord] = []+ var didUpload = falsevar failures = SyncFailures()}- struct ConfirmedHash {- let date: String- let hash: String- let didUpload: Bool- }-enum Metrics {- static let syncVersion = ActivityRecordCodec.versionstatic let saltByteCount = 32static let recordFetchLimit = 200static let queryFetchLimit = 200@@ -381,8 +350,8 @@ private extension SyncService {static let uploadTimeBudget: TimeInterval = 20}- typealias RecordTypes = ActivityRecordCodec.RecordTypes- typealias FieldKeys = ActivityRecordCodec.FieldKeys+ typealias RecordTypes = ActivityDataRecordCodec.RecordTypes+ typealias FieldKeys = ActivityDataRecordCodec.FieldKeysstatic let accountSaltRecordName = "accountSalt"@@ -394,34 +363,29 @@ private extension SyncService {CKRecord.ID(recordName: Self.accountSaltRecordName, zoneID: syncZoneID)}- private var activityStore: ActivitySyncStore {- ActivitySyncStore(directoryURL: directoryURL)+ private var activityDataStore: ActivityDataSyncStore {+ ActivityDataSyncStore(directoryURL: directoryURL)}// MARK: - 上传func uploadChangedAggregates(- localByDate: [String: SyncedActivity],- state: inout SyncState+ localByDate: [String: SyncedActivityData],+ state: inout ActivityDataSyncState) async throws -> Set<String> {guard let deviceID = state.deviceID else {return []}- // 每个日期的 hash 是一次 JSON 编码加 SHA256, 本轮只算一次- // 既用来判定"和上次一样不必上传", 也直接填进待上传项- let localHashByDate = try localByDate.mapValues { try hash(for: $0) }-+ let cache = try activityDataStore.load()var confirmedDates = Set<String>()- for (date, hash) in localHashByDate where state.hashByDate[date] == hash {- confirmedDates.insert(date)+ for (date, aggregate) in localByDate {+ let id = ActivityDataSyncRecord.recordName(deviceID: deviceID, date: date)+ if cache.records[id]?.activityData == aggregate {+ confirmedDates.insert(date)+ }}-- let pendingUploads = makePendingUploads(- localByDate: localByDate,- localHashByDate: localHashByDate,- state: state- )+ let pendingUploads = localByDate.values.filter { !confirmedDates.contains($0.date) }.sorted { $0.date < $1.date }guard !pendingUploads.isEmpty else {return confirmedDates}@@ -438,7 +402,13 @@ private extension SyncService {let batchEnd = min(batchStart + Metrics.uploadBatchSize, pendingUploads.count)let batch = Array(pendingUploads[batchStart ..< batchEnd])let result = try await processUploadBatch(batch, deviceID: deviceID)- try applyConfirmedHashes(result.confirmed, to: &state)+ if !result.confirmed.isEmpty {+ let lastUploadAt = result.didUpload ? Date() : nil+ try activityDataStore.saveConfirmedRecords(result.confirmed, lastUploadAt: lastUploadAt)+ if let lastUploadAt {+ state.lastUploadAt = lastUploadAt+ }+ }confirmedDates.formUnion(result.confirmed.map(\.date))try result.failures.checkStopping()if let error = result.failures.first {@@ -477,123 +447,31 @@ private extension SyncService {}}- func applyConfirmedHashes(- _ confirmedBatch: [ConfirmedHash],- to state: inout SyncState- ) throws {- guard !confirmedBatch.isEmpty else {- return- }-- for confirmation in confirmedBatch {- state.hashByDate[confirmation.date] = confirmation.hash- }-- if confirmedBatch.contains(where: \.didUpload) {- state.lastUploadAt = Date()- }- try activityStore.saveState(state)- }-- /// hash 由调用方一次算好传进来, 这里只做筛选- func makePendingUploads(- localByDate: [String: SyncedActivity],- localHashByDate: [String: String],- state: SyncState- ) -> [PendingUpload] {- localByDate.compactMap { date, local in- guard let hash = localHashByDate[date],- state.hashByDate[date] != hash else {- return nil- }-- return PendingUpload(- date: date,- aggregate: local,- hash: hash- )- }- .sorted { $0.date < $1.date }- }-- /// 单条待上传记录落到哪个 CKRecord 上, 或者本轮不需要上传- private enum UploadTarget {- case skip- case save(CKRecord)- }-- /// 只有可验证的同源日志前缀才能覆盖, 算法改变后计数减少也允许提交- private func resolveUploadTarget(- _ pendingRecord: PendingRecord,- existingRecords: [CKRecord.ID: Result<CKRecord, any Error>]- ) throws -> UploadTarget {- guard let candidateCheckpoint = pendingRecord.upload.aggregate.sourceCheckpoint else {- throw StorageCompatibilityError.incompleteSource- }- let candidate = pendingRecord.upload.aggregate- let journal = HistoryStorage.eventLogURL(- for: candidate.date,- in: activityEventsDirectoryURL- )- let header = try AppServerEventJournal.header(at: journal)- guard header.date == candidate.date, header.generationID == candidate.generationID,- try candidateCheckpoint.matchesSource(ActivitySourceCheckpoint.read(at: journal, byteCount: candidateCheckpoint.byteCount)) else {- throw StorageCompatibilityError.sourceConflict- }- let existing = try Self.fetchedRecord(from: existingRecords[pendingRecord.recordID])- if let existing {- guard let remote = try ActivityRecordCodec.remoteDailyRecord(from: existing),- remote.daily.generationID == candidate.generationID,- let checkpoint = remote.daily.sourceCheckpoint else {- throw ActivitySyncError.invalidRecordIdentity- }- if remote.daily.aggregationVersion > candidate.aggregationVersion || checkpoint.byteCount > candidateCheckpoint.byteCount {- return .skip- }- guard try checkpoint.matchesSource(ActivitySourceCheckpoint.read(at: journal, byteCount: checkpoint.byteCount)) else {- throw StorageCompatibilityError.sourceConflict- }- return .save(existing)- }- return .save(CKRecord(recordType: RecordTypes.activity, recordID: pendingRecord.recordID))- }-func processUploadBatch(- _ pendingUploads: [PendingUpload],+ _ aggregates: [SyncedActivityData],deviceID: String) async throws -> UploadBatchResult {- let pendingRecords = try pendingUploads.map { upload in- try PendingRecord(- upload: upload,- recordID: recordID(deviceID: deviceID, date: upload.date, generation: upload.aggregate.requiredGenerationID())- )- }- let recordIDs = pendingRecords.map(\.recordID)+ let recordIDs = aggregates.map { recordID(deviceID: deviceID, date: $0.date) }try SyncCancellation.check(isEnabled: isEnabled)let existingRecords = try await database.records(for: recordIDs, desiredKeys: nil)var outcome = UploadBatchResult()- var recordsToSave = [RecordToSave]()+ var recordsToSave: [CKRecord] = []- for pendingRecord in pendingRecords {+ for (aggregate, id) in zip(aggregates, recordIDs) {do {- switch try resolveUploadTarget(- pendingRecord,- existingRecords: existingRecords- ) {- case .skip:- outcome.confirmed.append(- ConfirmedHash(- date: pendingRecord.upload.date,- hash: pendingRecord.upload.hash,- didUpload: false- )- )- case let .save(record):- ActivityRecordCodec.apply(pendingRecord.upload.aggregate, deviceID: deviceID, to: record)- recordsToSave.append(- RecordToSave(upload: pendingRecord.upload, record: record)- )+ let existing = try Self.fetchedRecord(from: existingRecords[id])+ if let existing {+ guard let remote = try ActivityDataRecordCodec.remoteDailyRecord(from: existing) else {+ throw ActivityDataSyncError.invalidRecordIdentity+ }+ if remote.activityData == aggregate {+ outcome.confirmed.append(remote)+ continue+ }}+ let record = existing ?? CKRecord(recordType: RecordTypes.activityData, recordID: id)+ try ActivityDataRecordCodec.apply(aggregate, deviceID: deviceID, to: record)+ recordsToSave.append(record)} catch {outcome.failures.record(error)if outcome.failures.stopping != nil {@@ -602,37 +480,28 @@ private extension SyncService {}}- guard !recordsToSave.isEmpty else {- return outcome- }-- let records = recordsToSave.map(\.record)-+ guard !recordsToSave.isEmpty else { return outcome }try SyncCancellation.check(isEnabled: isEnabled)let result = try await database.modifyRecords(- saving: records,+ saving: recordsToSave,deleting: [],savePolicy: .ifServerRecordUnchanged,atomically: false)-- for pendingRecord in recordsToSave {- switch result.saveResults[pendingRecord.record.recordID] {- case .success:- outcome.confirmed.append(- ConfirmedHash(- date: pendingRecord.upload.date,- hash: pendingRecord.upload.hash,- didUpload: true- )- )- case let .failure(error):+ for record in recordsToSave {+ do {+ guard let saved = result.saveResults[record.recordID] else {+ throw SyncRecovery.Failure.missingRecordResult+ }+ guard let confirmed = try ActivityDataRecordCodec.remoteDailyRecord(from: saved.get()) else {+ throw ActivityDataSyncError.invalidRecordIdentity+ }+ outcome.confirmed.append(confirmed)+ outcome.didUpload = true+ } catch {outcome.failures.record(error)- case nil:- outcome.failures.record(SyncRecovery.Failure.missingRecordResult)}}-return outcome}@@ -640,7 +509,7 @@ private extension SyncService {func refreshCacheFromRemote() async throws {do {- let cache = try activityStore.load()+ let cache = try activityDataStore.load()guard let token = try cache.cursor.map(SyncRecovery.decodeCursor) else {try await rebuildCacheFromRemote()return@@ -663,9 +532,9 @@ private extension SyncService {func applyZoneChangesToCache(since initialToken: CKServerChangeToken?,- cachedRecords: [ActivitySyncRecord]+ cachedRecords: [String: ActivityDataSyncRecord]) async throws {- var cacheByID = Self.recordsByID(records: cachedRecords)+ var cacheByID = cachedRecordsvar token: CKServerChangeToken? = initialTokenvar moreComing = true@@ -687,13 +556,11 @@ private extension SyncService {moreComing = result.moreComing}- try activityStore.saveFetchedRecords(- Self.filteredRetained(records: Array(cacheByID.values)), cursor: token- )+ try activityDataStore.saveFetchedRecords(cacheByID, cursor: token)}func rebuildCacheFromRemote() async throws {- try await applyZoneChangesToCache(since: nil, cachedRecords: [])+ try await applyZoneChangesToCache(since: nil, cachedRecords: [:])}func fetchCurrentDeviceRecordIDsToPrune(@@ -701,7 +568,7 @@ private extension SyncService {cutoffKey: String) async throws -> [CKRecord.ID] {let query = CKQuery(- recordType: RecordTypes.activity,+ recordType: RecordTypes.activityData,predicate: NSPredicate(format: "%K == %@",FieldKeys.deviceID,@@ -715,8 +582,8 @@ private extension SyncService {)return try currentDeviceMatches.compactMap { recordID, result inlet record = try result.get()- guard let decoded = try ActivityRecordCodec.remoteDailyRecord(from: record) else {- throw ActivitySyncError.invalidRecordIdentity+ guard let decoded = try ActivityDataRecordCodec.remoteDailyRecord(from: record) else {+ throw ActivityDataSyncError.invalidRecordIdentity}return decoded.date < cutoffKey ? recordID : nil}@@ -752,11 +619,11 @@ private extension SyncService {func mergeChangedRecords(_ modificationResults: Dictionary<CKRecord.ID, Result<CKRecord, Error>>.Values,- into cacheByID: inout [String: ActivitySyncRecord]+ into cacheByID: inout [String: ActivityDataSyncRecord]) throws {for modificationResult in modificationResults {let modification = try modificationResult.get()- guard let record = try ActivityRecordCodec.remoteDailyRecord(from: modification) else {+ guard let record = try ActivityDataRecordCodec.remoteDailyRecord(from: modification) else {continue}@@ -766,9 +633,9 @@ private extension SyncService {func removeDeletedRecords(_ deletions: [(id: CKRecord.ID, type: String)],- from cacheByID: inout [String: ActivitySyncRecord]+ from cacheByID: inout [String: ActivityDataSyncRecord]) {- for deletion in deletions where deletion.type == RecordTypes.activity {+ for deletion in deletions where deletion.type == RecordTypes.activityData {guard let cacheID = cacheID(fromRecordName: deletion.id.recordName) else {continue}@@ -779,7 +646,7 @@ private extension SyncService {@discardableResultfunc pruneCurrentDeviceRecordsIfNeeded(deviceID: String,- state: inout SyncState+ state: inout ActivityDataSyncState) async throws -> Int {let today = HistoryStorage.dateKey(for: Date())guard state.lastPrunedDate != today else {@@ -787,7 +654,6 @@ private extension SyncService {}let cutoffKey = HistoryStorage.dateKey(for: HistoryStorage.retentionCutoffDate())- let expiredDates = Set(state.hashByDate.keys.filter { $0 < cutoffKey })let recordIDs = try await Set(fetchCurrentDeviceRecordIDsToPrune(deviceID: deviceID,@@ -801,9 +667,6 @@ private extension SyncService {)}- for date in expiredDates {- state.hashByDate.removeValue(forKey: date)- }state.lastPrunedDate = todayreturn recordIDs.count}@@ -820,20 +683,13 @@ private extension SyncService {return Self.hexString(Data(mac))}+ /// 每轮读取最新 salt, 识别其他设备重建同名 zone 后的设备身份变化func accountSalt() async throws -> Data {- if let cachedAccountSalt {- return cachedAccountSalt- }-let recordID = accountSaltRecordID- let salt: Data = if let fetched = try await fetchAccountSalt(recordID) {- fetched- } else {- try await createAccountSalt(recordID)+ if let salt = try await fetchAccountSalt(recordID) {+ return salt}-- cachedAccountSalt = salt- return salt+ return try await createAccountSalt(recordID)}func fetchAccountSalt(_ recordID: CKRecord.ID) async throws -> Data? {@@ -842,7 +698,10 @@ private extension SyncService {guard let record = try Self.fetchedRecord(from: result[recordID]) else {return nil}- try StorageVersion.require(ActivityRecordCodec.optionalIntValue(record[FieldKeys.version]) ?? -1, current: ActivityRecordCodec.metadataVersion, name: "CloudMetadata")+ try StorageVersion.require(+ ActivityDataRecordCodec.optionalIntValue(record[FieldKeys.schemaVersion]) ?? -1,+ current: ActivityDataRecordCodec.metadataSchemaVersion, name: "CloudMetadata"+ )guard let salt = record[FieldKeys.salt] as? Data,salt.count == Metrics.saltByteCount else {throw SyncError.missingAccountSalt@@ -854,7 +713,7 @@ private extension SyncService {let salt = try Self.randomSalt()let record = CKRecord(recordType: RecordTypes.metadata, recordID: recordID)record[FieldKeys.salt] = salt as CKRecordValue- record[FieldKeys.version] = ActivityRecordCodec.metadataVersion as CKRecordValue+ record[FieldKeys.schemaVersion] = ActivityDataRecordCodec.metadataSchemaVersion as CKRecordValuedo {try SyncCancellation.check(isEnabled: isEnabled)@@ -883,7 +742,7 @@ private extension SyncService {// MARK: - 记录与 zone 维护- func ensureSyncZoneExists(state: inout SyncState) async throws {+ func ensureSyncZoneExists(state: inout ActivityDataSyncState) async throws {guard !isSyncZoneConfirmed else {return}@@ -911,7 +770,6 @@ private extension SyncService {func invalidateAccountScopedCaches() {isSyncZoneConfirmed = false- cachedAccountSalt = nil}func syncZoneExists() async throws -> Bool {@@ -924,9 +782,9 @@ private extension SyncService {}}- func recordID(deviceID: String, date: String, generation: String) -> CKRecord.ID {+ func recordID(deviceID: String, date: String) -> CKRecord.ID {CKRecord.ID(- recordName: ActivitySyncRecord.recordName(deviceID: deviceID, date: date, generation: generation),+ recordName: ActivityDataSyncRecord.recordName(deviceID: deviceID, date: date),zoneID: syncZoneID)}@@ -935,11 +793,6 @@ private extension SyncService {recordName.isEmpty ? nil : recordName}- func hash(for aggregate: SyncedActivity) throws -> String {- let digest = try SHA256.hash(data: aggregate.jsonLineData())- return Self.hexString(Data(digest))- }-nonisolated static func postSyncNotification(_ name: Notification.Name,didSucceed: Bool? = nil,@@ -964,9 +817,9 @@ private extension SyncService {}extension SyncService {- /// 设置页读取最近上传时间, 与内部 loadState 使用同一 version 校验口径+ /// 设置页读取最近上传时间, 与内部 loadState 使用同一 schemaVersion 校验口径nonisolated static func loadLastUploadAt() -> Date? {- ActivitySyncStore(directoryURL: HistoryStorage.syncDirectoryURL()).loadState().lastUploadAt+ ActivityDataSyncStore(directoryURL: HistoryStorage.syncDirectoryURL()).loadState().lastUploadAt}}@@ -987,21 +840,12 @@ private extension SyncService {}static func filteredRetained(- records: [ActivitySyncRecord]- ) -> [ActivitySyncRecord] {+ records: [ActivityDataSyncRecord]+ ) -> [ActivityDataSyncRecord] {let cutoffKey = HistoryStorage.dateKey(for: HistoryStorage.retentionCutoffDate())return records.filter { $0.date >= cutoffKey }}- static func recordsByID(- records: [ActivitySyncRecord]- ) -> [String: ActivitySyncRecord] {- filteredRetained(records: records)- .reduce(into: [String: ActivitySyncRecord]()) { result, record in- result[record.id] = record- }- }-static func randomSalt() throws -> Data {var bytes = [UInt8](repeating: 0, count: Metrics.saltByteCount)let status = SecRandomCopyBytes(kSecRandomDefault, bytes.count, &bytes)@@ -1041,15 +885,15 @@ private extension SyncService {}}-/// records 包含所有设备的日聚合, 不含 threadIDs 和 turnIds-/// currentDeviceId 用于展示时替换本机云端副本, 避免重复计数+/// activityData 包含所有设备的日聚合, 不含 threadIDs 和 turnIds+/// currentDeviceID 用于展示时替换本机云端副本, 避免重复计数nonisolated struct SyncSnapshot: Equatable {- let records: [ActivitySyncRecord]+ let activityData: [ActivityDataSyncRecord]let currentDeviceID: String?var tokenUsageByDate: [String: TokenUsage]?- var isActivityComplete = true+ var isActivityDataComplete = true- static let disabled = SyncSnapshot(records: [], currentDeviceID: nil)+ static let disabled = SyncSnapshot(activityData: [], currentDeviceID: nil)}private nonisolated enum SyncError: Error {diff --git a/CodexBar/Services/Sync/TokenSync.swift b/CodexBar/Services/Sync/TokenSync.swiftdeleted file mode 100644index 0d2d20c..0000000--- a/CodexBar/Services/Sync/TokenSync.swift+++ /dev/null@@ -1,318 +0,0 @@-import CloudKit-import Foundation--/// 每个线程轮次一个云端累计记录, 多设备通过条件写入收敛到同一份用量-actor TokenSync {- private let database: any SyncDatabase- private let directoryURL: URL- private let isEnabled: @Sendable () -> Bool- private var fileCache: TokenFileCache<TokenSyncCache>- static let recordType = "Tokens"- private static let currentVersion = 1-- init(database: any SyncDatabase, directoryURL: URL, isEnabled: @escaping @Sendable () -> Bool = { SyncSettings.isEnabled() }) {- self.database = database- self.directoryURL = directoryURL- self.isEnabled = isEnabled- fileCache = TokenFileCache(url: directoryURL.appendingPathComponent("cache.json"))- }-- func recoveryBaseline(accountScopedDeviceID: String?) -> TokenHistoryBaseline? {- guard let accountScopedDeviceID, let cache = try? load(), cache.accountScopedDeviceID == accountScopedDeviceID else { return nil }- return TokenHistoryBaseline(salt: cache.salt, turns: cache.turns)- }-- func snapshot(local: [TokenTurn], accountScopedDeviceID: String?) throws -> [String: TokenUsage] {- guard let cache = try load(), cache.accountScopedDeviceID == accountScopedDeviceID else {- return TokenTurn.dailyUsage(local)- }- let merged = try TokenTurn.merged(TokenTurn.pseudonymized(local, salt: cache.salt) + Array(cache.turns.values))- return TokenTurn.dailyUsage(Array(merged.values))- }-- func hasPendingUpdates(local: [TokenTurn], accountScopedDeviceID: String?) -> Bool {- let records = TokenTurn.syncable(local)- guard let cache = try? load(), cache.accountScopedDeviceID == accountScopedDeviceID else { return !records.isEmpty }- return records.contains { turn in- let candidate = turn.pseudonymized(salt: cache.salt)- guard let remote = cache.turns[candidate.id] else { return true }- return (try? remote.merging(candidate)) != remote- }- }-- func resetCache() throws {- try SyncCancellation.check(isEnabled: isEnabled)- guard let lock = try JSONFileStorage.acquireLock(in: directoryURL, name: "sync.lock", nonblocking: true) else {- throw POSIXError(.EWOULDBLOCK)- }- defer { JSONFileStorage.releaseLock(lock) }- try JSONFileStorage.withLock(in: directoryURL) {- do { _ = try Self.loadCache(&fileCache) } catch is DecodingError {- // 已知格式损坏可清除, 未知版本继续向上抛出- }- if FileManager.default.fileExists(atPath: fileCache.url.path) {- try FileManager.default.removeItem(at: fileCache.url)- }- fileCache = TokenFileCache(url: fileCache.url)- }- }-- func synchronize(local: [TokenTurn], recoveredIDs: Set<String> = [], accountScopedDeviceID: String, salt: Data, zoneID: CKRecordZone.ID) async throws {- try SyncCancellation.check(isEnabled: isEnabled)- // 整轮同步共用非阻塞锁, 防止另一个进程用较早的快照覆盖游标, 界面读取不等待网络- guard let lock = try JSONFileStorage.acquireLock(in: directoryURL, name: "sync.lock", nonblocking: true) else { return }- defer { JSONFileStorage.releaseLock(lock) }- var cache: TokenSyncCache- do {- cache = try load() ?? TokenSyncCache(accountScopedDeviceID: accountScopedDeviceID, salt: salt)- } catch is DecodingError {- cache = TokenSyncCache(accountScopedDeviceID: accountScopedDeviceID, salt: salt)- }- if cache.accountScopedDeviceID != accountScopedDeviceID || cache.salt != salt {- cache = TokenSyncCache(accountScopedDeviceID: accountScopedDeviceID, salt: salt)- }- do {- try await fetch(into: &cache, zoneID: zoneID)- } catch where SyncRecovery.isInvalidCursor(error) {- var rebuilt = TokenSyncCache(accountScopedDeviceID: accountScopedDeviceID, salt: salt)- try await fetch(into: &rebuilt, zoneID: zoneID)- cache = rebuilt- }- // 先落盘拉取结果, 上传失败时也能展示已经读取的其他设备贡献- try save(cache)- try await upload(local: local, recoveredIDs: recoveredIDs, cache: &cache, zoneID: zoneID)- try await prune(cache: &cache, zoneID: zoneID)- try save(cache)- }-- private func fetch(into cache: inout TokenSyncCache, zoneID: CKRecordZone.ID) async throws {- var token = try cache.cursor.map(SyncRecovery.decodeCursor)- var turns = token == nil ? [:] : cache.turns- var more = true- while more {- try Task.checkCancellation()- try SyncCancellation.check(isEnabled: isEnabled)- let result = try await database.fetchChanges(inZoneWith: zoneID, since: token, resultsLimit: 200)- for modification in result.records.values {- if let turn = try Self.turn(from: modification.get()) {- turns[turn.id] = turn- }- }- for deletion in result.deletions where deletion.type == Self.recordType {- turns.removeValue(forKey: deletion.id.recordName)- }- token = result.token- more = result.moreComing- }- cache.turns = turns- cache.cursor = try token.map { try NSKeyedArchiver.archivedData(withRootObject: $0, requiringSecureCoding: true) }- }-- private func upload(local: [TokenTurn], recoveredIDs: Set<String>, cache: inout TokenSyncCache, zoneID: CKRecordZone.ID) async throws {- // 恢复结果先与本轮新鲜云端基线建立替换关系, 再执行普通条件写入- let baseline = TokenHistoryBaseline(salt: cache.salt, turns: cache.turns)- let candidates = local.compactMap { turn -> TokenTurn? in- guard recoveredIDs.contains(turn.id) else { return turn }- guard baseline.acceptsRecovery(turn) else { return nil }- return baseline.replacement(for: turn, recovering: true) ?? turn- }- let pending = try TokenTurn.pseudonymized(TokenTurn.syncable(candidates), salt: cache.salt)- .filter { candidate in- guard let remote = cache.turns[candidate.id] else { return true }- return try remote.merging(candidate) != remote- }- .sorted { $0.updatedAt > $1.updatedAt }- let deadline = Date().addingTimeInterval(20)- var failures = SyncFailures()- for offset in stride(from: 0, to: pending.count, by: 25) {- try Task.checkCancellation()- guard Date() < deadline else { break }- let batch = Array(pending[offset ..< min(offset + 25, pending.count)])- let ids = batch.map { Self.recordID($0.id, zoneID: zoneID) }- try SyncCancellation.check(isEnabled: isEnabled)- let existing = try await database.records(for: ids, desiredKeys: nil)- var saving: [CKRecord] = []- for candidate in batch {- let id = Self.recordID(candidate.id, zoneID: zoneID)- let record: CKRecord- let remote: TokenTurn?- do {- let fetched = try fetchedRecord(existing[id])- remote = try fetched.flatMap(Self.turn)- record = fetched ?? CKRecord(recordType: Self.recordType, recordID: id)- } catch {- failures.record(error)- if failures.stopping != nil {- break- }- continue- }- let merged = try remote?.merging(candidate) ?? candidate- if remote == merged {- cache.turns[merged.id] = merged- continue- }- Self.apply(merged, to: record)- saving.append(record)- }- try save(cache)- try failures.checkStopping()- if saving.isEmpty {- continue- }- try SyncCancellation.check(isEnabled: isEnabled)- let result = try await database.modifyRecords(saving: saving, deleting: [], savePolicy: .ifServerRecordUnchanged, atomically: false)- for record in saving {- switch result.saveResults[record.recordID] {- case let .success(saved):- if let turn = try Self.turn(from: saved) {- cache.turns[turn.id] = turn- }- case let .failure(error): failures.record(error)- case nil: failures.record(TokenSyncError.missingRecordResult)- }- }- try save(cache)- // 冲突不会使用覆盖写入, 下一轮重新获取服务端累计值后合并- try failures.checkStopping()- }- try failures.check()- }-- private func fetchedRecord(_ result: Result<CKRecord, any Error>?) throws -> CKRecord? {- switch result {- case let .success(record): record- case let .failure(error as CKError) where error.code == .unknownItem: nil- case let .failure(error): throw error- case nil: throw TokenSyncError.missingRecordResult- }- }-- private func prune(cache: inout TokenSyncCache, zoneID: CKRecordZone.ID) async throws {- let cutoff = HistoryStorage.retentionCutoffDate()- let requiredRoots = Set(cache.turns.values.filter { $0.updatedAt >= cutoff }.map(\.rootID))- let expired = cache.turns.values.filter {- $0.updatedAt < cutoff && !requiredRoots.contains($0.id)- }.prefix(200)- let ids = expired.map { Self.recordID($0.id, zoneID: zoneID) }- guard !ids.isEmpty else { return }- try SyncCancellation.check(isEnabled: isEnabled)- let result = try await database.modifyRecords(saving: [], deleting: ids, savePolicy: .ifServerRecordUnchanged, atomically: false)- for id in ids {- guard let deletion = result.deleteResults[id] else { throw TokenSyncError.missingRecordResult }- do { try deletion.get() } catch let error as CKError where error.code == .unknownItem {}- cache.turns.removeValue(forKey: id.recordName)- }- }-- private func load() throws -> TokenSyncCache? {- try JSONFileStorage.withLock(in: directoryURL) { try Self.loadCache(&fileCache) }- }-- private nonisolated static func loadCache(_ fileCache: inout TokenFileCache<TokenSyncCache>) throws -> TokenSyncCache? {- try fileCache.load { data in- let header = try JSONLines.decoder.decode(TokenSyncCacheHeader.self, from: data)- guard header.version == TokenSyncCache.currentVersion else { throw SyncRecovery.Failure.unsupportedCacheVersion }- }- }-- private func save(_ cache: TokenSyncCache) throws {- try JSONFileStorage.withLock(in: directoryURL) {- var cache = cache- cache.prune()- try fileCache.save(cache)- }- }-- nonisolated static func pruneLocalCache(in directoryURL: URL, now: Date) throws {- try JSONFileStorage.withLock(in: directoryURL) {- var fileCache = TokenFileCache<TokenSyncCache>(url: directoryURL.appendingPathComponent("cache.json"))- if var cache = try loadCache(&fileCache), cache.prune(now: now) {- try fileCache.save(cache)- }- }- }-- static func recordID(_ id: String, zoneID: CKRecordZone.ID) -> CKRecord.ID {- CKRecord.ID(recordName: id, zoneID: zoneID)- }-- static func apply(_ turn: TokenTurn, to record: CKRecord) {- record["version"] = currentVersion as CKRecordValue- record["aggregationVersion"] = turn.aggregationVersion as CKRecordValue- record["rootID"] = turn.rootID as CKRecordValue- record["startedAt"] = turn.startedAt as CKRecordValue?- record["updatedAt"] = turn.updatedAt as CKRecordValue- record["rebuiltAt"] = turn.rebuiltAt as CKRecordValue?- record["generationID"] = turn.generationID as CKRecordValue- record["ancestorIDs"] = turn.ancestorIDs.sorted() as CKRecordValue- record["checkpoint"] = try? JSONLines.stableEncoder.encode(turn.checkpoint) as CKRecordValue- record["hasConflict"] = (turn.hasConflict ? 1 : 0) as CKRecordValue- record["usage"] = turn.usage.flatMap { try? JSONLines.stableEncoder.encode($0) } as CKRecordValue?- }-- static func turn(from record: CKRecord) throws -> TokenTurn? {- guard record.recordType == recordType else { return nil }- try StorageVersion.require((record["version"] as? NSNumber)?.intValue ?? -1, current: currentVersion, name: "CloudTokens")- try AggregationVersion.require((record["aggregationVersion"] as? NSNumber)?.intValue ?? -1, current: AggregationVersion.tokens, name: "Tokens")- guard- let rootID = record["rootID"] as? String,- let updatedAt = record["updatedAt"] as? Date,- let generationID = record["generationID"] as? String,- let ancestors = record["ancestorIDs"] as? [String],- let checkpointData = record["checkpoint"] as? Data,- let checkpoint = try? JSONDecoder().decode([String: TokenObservationCheckpoint].self, from: checkpointData),- checkpoint.values.allSatisfy({ $0.sequence > 0 && $0.usage.isValid }),- let hasConflict = record["hasConflict"] as? NSNumber else { throw StorageCompatibilityError.incompleteSource }- let id = record.recordID.recordName- guard isHash(id), isHash(rootID) else { throw StorageCompatibilityError.incompleteSource }- var usage: TokenUsage?- if let value = record["usage"] {- guard let data = value as? Data, let decoded = try? JSONDecoder().decode(TokenUsage.self, from: data),- decoded.isValid else { throw StorageCompatibilityError.incompleteSource }- usage = decoded- }- guard usage == nil || !checkpoint.isEmpty else { throw StorageCompatibilityError.incompleteSource }- if let rebuiltAt = record["rebuiltAt"], !(rebuiltAt is Date) {- throw StorageCompatibilityError.incompleteSource- }- return TokenTurn(- id: id, rootID: rootID, startedAt: record["startedAt"] as? Date, updatedAt: updatedAt,- usage: usage, rebuiltAt: record["rebuiltAt"] as? Date,- generationID: generationID, ancestorIDs: Set(ancestors),- checkpoint: checkpoint, hasConflict: hasConflict.boolValue,- aggregationVersion: (record["aggregationVersion"] as? NSNumber)?.intValue ?? -1- )- }-- private static func isHash(_ text: String) -> Bool {- text.count == 64 && text.utf8.allSatisfy { (48 ... 57).contains($0) || (97 ... 102).contains($0) }- }-}--private nonisolated struct TokenSyncCacheHeader: Decodable { let version: Int }--private nonisolated struct TokenSyncCache: Codable, Equatable {- static let currentVersion = 1- var version = currentVersion- let accountScopedDeviceID: String- let salt: Data- var turns: [String: TokenTurn] = [:]- var cursor: Data?-- @discardableResult- mutating func prune(now: Date = Date()) -> Bool {- let cutoff = HistoryStorage.retentionCutoffDate(today: now)- let roots = Set(turns.values.filter { $0.updatedAt >= cutoff }.map(\.rootID))- let retained = turns.filter { $0.value.updatedAt >= cutoff || roots.contains($0.key) }- guard retained.count != turns.count else { return false }- turns = retained- // 本地先过期, 云端删除仍可能失败. 下次完整拉取让过期记录重新进入删除流程- cursor = nil- return true- }-}--private nonisolated enum TokenSyncError: Error {- case missingRecordResult-}diff --git a/CodexBar/Services/Sync/TokenUsageSync.swift b/CodexBar/Services/Sync/TokenUsageSync.swiftnew file mode 100644index 0000000..1124ee9--- /dev/null+++ b/CodexBar/Services/Sync/TokenUsageSync.swift@@ -0,0 +1,283 @@+import CloudKit+import Foundation++/// 按设备和日期覆盖每日数量, 本机数据与其他设备的汇总分别保留+actor TokenUsageSync {+ private let database: any SyncDatabase+ private let directoryURL: URL+ private let isEnabled: @Sendable () -> Bool+ private var fileCache: TokenUsageFileCache<TokenUsageSyncCache>+ static let recordType = "TokenUsage"+ private static let schemaVersion = 1++ init(database: any SyncDatabase, directoryURL: URL, isEnabled: @escaping @Sendable () -> Bool = { SyncSettings.isEnabled() }) {+ self.database = database+ self.directoryURL = directoryURL+ self.isEnabled = isEnabled+ fileCache = TokenUsageFileCache(url: directoryURL.appendingPathComponent("cache.json"))+ }++ func snapshot(local: [TokenUsageAggregate], accountScopedDeviceID: String?) throws -> [String: TokenUsage] {+ guard let deviceID = accountScopedDeviceID, let cache = try load(), cache.state.deviceID == deviceID else {+ return TokenUsageAggregate.tokenUsageByDate(local)+ }+ var records = cache.records+ for record in try TokenUsageSyncRecord.dailyRecords(local: local, deviceID: deviceID) {+ records[record.id] = record+ }+ return TokenUsageSyncRecord.tokenUsageByDate(Array(records.values))+ }++ func hasPendingUpdates(local: [TokenUsageAggregate], accountScopedDeviceID: String?) -> Bool {+ guard let deviceID = accountScopedDeviceID else { return !local.isEmpty }+ guard let records = try? TokenUsageSyncRecord.dailyRecords(local: local, deviceID: deviceID) else { return true }+ guard let cache = try? load(), cache.state.deviceID == deviceID else { return !records.isEmpty }+ return records.contains { cache.records[$0.id]?.hasSameData(as: $0) != true }+ }++ func resetCache() throws {+ try SyncCancellation.check(isEnabled: isEnabled)+ guard let lock = try JSONFileStorage.acquireLock(in: directoryURL, name: "sync.lock", nonblocking: true) else {+ throw POSIXError(.EWOULDBLOCK)+ }+ defer { JSONFileStorage.releaseLock(lock) }+ try JSONFileStorage.withLock(in: directoryURL) {+ do { _ = try Self.loadCache(&fileCache) } catch is DecodingError {+ // 已知格式损坏可清除, 未知版本继续向上抛出+ }+ if FileManager.default.fileExists(atPath: fileCache.url.path) {+ try FileManager.default.removeItem(at: fileCache.url)+ }+ fileCache = TokenUsageFileCache(url: fileCache.url)+ }+ }++ func synchronize(local: [TokenUsageAggregate], accountScopedDeviceID: String, zoneID: CKRecordZone.ID, canUpload: Bool = true) async throws {+ try SyncCancellation.check(isEnabled: isEnabled)+ // 整轮同步共用非阻塞锁, 防止另一个进程用较早的快照覆盖游标, 界面读取不等待网络+ guard let lock = try JSONFileStorage.acquireLock(in: directoryURL, name: "sync.lock", nonblocking: true) else { return }+ defer { JSONFileStorage.releaseLock(lock) }+ var cache: TokenUsageSyncCache+ do {+ cache = try load() ?? TokenUsageSyncCache(state: TokenUsageSyncState(deviceID: accountScopedDeviceID))+ } catch is DecodingError {+ cache = TokenUsageSyncCache(state: TokenUsageSyncState(deviceID: accountScopedDeviceID))+ }+ if cache.state.deviceID != accountScopedDeviceID {+ cache = TokenUsageSyncCache(state: TokenUsageSyncState(deviceID: accountScopedDeviceID))+ }+ do {+ try await fetch(into: &cache, zoneID: zoneID)+ } catch where SyncRecovery.isInvalidCursor(error) {+ var rebuilt = TokenUsageSyncCache(state: TokenUsageSyncState(deviceID: accountScopedDeviceID))+ try await fetch(into: &rebuilt, zoneID: zoneID)+ cache = rebuilt+ }+ // 先落盘拉取结果, 上传失败时也能展示已经读取的其他设备贡献+ try save(cache)+ // 原始事件不完整时仍可拉取其他设备数量, 但不能用局部结果替换整天的数量+ if canUpload {+ try await upload(local: local, cache: &cache, zoneID: zoneID)+ }+ try await prune(cache: &cache, zoneID: zoneID)+ try save(cache)+ }++ private func fetch(into cache: inout TokenUsageSyncCache, zoneID: CKRecordZone.ID) async throws {+ var token = try cache.cursor.map(SyncRecovery.decodeCursor)+ var records = token == nil ? [:] : cache.records+ var more = true+ while more {+ try Task.checkCancellation()+ try SyncCancellation.check(isEnabled: isEnabled)+ let result = try await database.fetchChanges(inZoneWith: zoneID, since: token, resultsLimit: 200)+ for modification in result.records.values {+ if let record = try Self.dailyRecord(from: modification.get()) {+ records[record.id] = record+ }+ }+ for deletion in result.deletions where deletion.type == Self.recordType {+ records.removeValue(forKey: deletion.id.recordName)+ }+ token = result.token+ more = result.moreComing+ }+ cache.records = records+ cache.cursor = try token.map { try NSKeyedArchiver.archivedData(withRootObject: $0, requiringSecureCoding: true) }+ }++ private func upload(local: [TokenUsageAggregate], cache: inout TokenUsageSyncCache, zoneID: CKRecordZone.ID) async throws {+ let pending = try TokenUsageSyncRecord.dailyRecords(local: local, deviceID: cache.state.deviceID)+ .filter { cache.records[$0.id]?.hasSameData(as: $0) != true }+ .sorted { $0.date > $1.date }+ let deadline = Date().addingTimeInterval(20)+ var failures = SyncFailures()+ for offset in stride(from: 0, to: pending.count, by: 25) {+ try Task.checkCancellation()+ guard Date() < deadline else { break }+ let batch = Array(pending[offset ..< min(offset + 25, pending.count)])+ let ids = batch.map { Self.recordID($0.id, zoneID: zoneID) }+ try SyncCancellation.check(isEnabled: isEnabled)+ let existing = try await database.records(for: ids, desiredKeys: nil)+ var saving: [CKRecord] = []+ for candidate in batch {+ let id = Self.recordID(candidate.id, zoneID: zoneID)+ do {+ let fetched = try fetchedRecord(existing[id])+ if let remote = try fetched.flatMap(Self.dailyRecord), remote.hasSameData(as: candidate) {+ cache.records[candidate.id] = remote+ continue+ }+ let record = fetched ?? CKRecord(recordType: Self.recordType, recordID: id)+ try Self.apply(candidate, to: record)+ saving.append(record)+ } catch {+ failures.record(error)+ if failures.stopping != nil {+ break+ }+ }+ }+ try save(cache)+ try failures.checkStopping()+ guard !saving.isEmpty else { continue }+ try SyncCancellation.check(isEnabled: isEnabled)+ let result = try await database.modifyRecords(saving: saving, deleting: [], savePolicy: .ifServerRecordUnchanged, atomically: false)+ for record in saving {+ switch result.saveResults[record.recordID] {+ case let .success(saved):+ if let tokenUsageRecord = try Self.dailyRecord(from: saved) {+ cache.records[tokenUsageRecord.id] = tokenUsageRecord+ }+ case let .failure(error): failures.record(error)+ case nil: failures.record(TokenUsageSyncError.missingRecordResult)+ }+ }+ try save(cache)+ // 条件写入冲突后下轮重新读取, 每次都提交本机当前的完整日汇总+ try failures.checkStopping()+ }+ try failures.check()+ }++ private func fetchedRecord(_ result: Result<CKRecord, any Error>?) throws -> CKRecord? {+ switch result {+ case let .success(record): record+ case let .failure(error as CKError) where error.code == .unknownItem: nil+ case let .failure(error): throw error+ case nil: throw TokenUsageSyncError.missingRecordResult+ }+ }++ private func prune(cache: inout TokenUsageSyncCache, zoneID: CKRecordZone.ID) async throws {+ let cutoff = HistoryStorage.dateKey(for: HistoryStorage.retentionCutoffDate())+ let expired = cache.records.values.filter {+ $0.deviceID == cache.state.deviceID && $0.date < cutoff+ }.prefix(200)+ let ids = expired.map { Self.recordID($0.id, zoneID: zoneID) }+ guard !ids.isEmpty else { return }+ try SyncCancellation.check(isEnabled: isEnabled)+ let result = try await database.modifyRecords(saving: [], deleting: ids, savePolicy: .ifServerRecordUnchanged, atomically: false)+ for id in ids {+ guard let deletion = result.deleteResults[id] else { throw TokenUsageSyncError.missingRecordResult }+ do { try deletion.get() } catch let error as CKError where error.code == .unknownItem {}+ cache.records.removeValue(forKey: id.recordName)+ }+ }++ private func load() throws -> TokenUsageSyncCache? {+ try JSONFileStorage.withLock(in: directoryURL) { try Self.loadCache(&fileCache) }+ }++ private nonisolated static func loadCache(_ fileCache: inout TokenUsageFileCache<TokenUsageSyncCache>) throws -> TokenUsageSyncCache? {+ let cache = try fileCache.load()+ guard cache?.state.containerIdentifier == SyncCloudKit.containerIdentifier else { return nil }+ return cache+ }++ private func save(_ cache: TokenUsageSyncCache) throws {+ try JSONFileStorage.withLock(in: directoryURL) {+ var cache = cache+ cache.prune()+ try fileCache.save(cache)+ }+ }++ nonisolated static func pruneLocalCache(in directoryURL: URL, now: Date) throws {+ try JSONFileStorage.withLock(in: directoryURL) {+ var fileCache = TokenUsageFileCache<TokenUsageSyncCache>(url: directoryURL.appendingPathComponent("cache.json"))+ if var cache = try loadCache(&fileCache), cache.prune(now: now) {+ try fileCache.save(cache)+ }+ }+ }++ static func recordID(_ id: String, zoneID: CKRecordZone.ID) -> CKRecord.ID {+ CKRecord.ID(recordName: id, zoneID: zoneID)+ }++ static func apply(_ tokenUsageRecord: TokenUsageSyncRecord, to record: CKRecord) throws {+ record["schemaVersion"] = schemaVersion as CKRecordValue+ record["aggregationVersion"] = tokenUsageRecord.aggregationVersion as CKRecordValue+ record["deviceID"] = tokenUsageRecord.deviceID as CKRecordValue+ record["date"] = tokenUsageRecord.date as CKRecordValue+ record["updatedAt"] = Date() as CKRecordValue+ let tokenUsage = tokenUsageRecord.tokenUsage+ let tokenUsageCounts: [String: Int64?] = [+ "inputTokens": tokenUsage?.inputTokens, "cachedInputTokens": tokenUsage?.cachedInputTokens,+ "cacheWriteInputTokens": tokenUsage?.cacheWriteInputTokens, "outputTokens": tokenUsage?.outputTokens,+ "reasoningOutputTokens": tokenUsage?.reasoningOutputTokens, "totalTokens": tokenUsage?.totalTokens+ ]+ record["tokenUsage"] = try JSONLines.stableEncoder.encode(tokenUsageCounts) as CKRecordValue+ }++ static func dailyRecord(from record: CKRecord) throws -> TokenUsageSyncRecord? {+ guard record.recordType == recordType else { return nil }+ try StorageVersion.require((record["schemaVersion"] as? NSNumber)?.intValue ?? -1, current: schemaVersion, name: "CloudTokenUsage")+ let aggregationVersion = (record["aggregationVersion"] as? NSNumber)?.intValue ?? -1+ try AggregationVersion.require(aggregationVersion, current: AggregationVersion.tokenUsage, name: "tokenUsage")+ guard let deviceID = record["deviceID"] as? String, !deviceID.isEmpty,+ let date = record["date"] as? String, HistoryStorage.isValidDateKey(date),+ let data = record["tokenUsage"] as? Data else { throw StorageCompatibilityError.incompleteSource }+ let tokenUsageCounts = try JSONDecoder().decode([String: Int64?].self, from: data)+ guard Set(tokenUsageCounts.keys) == [+ "inputTokens", "cachedInputTokens", "cacheWriteInputTokens", "outputTokens", "reasoningOutputTokens", "totalTokens"+ ], tokenUsageCounts.values.compactMap(\.self).allSatisfy({ $0 >= 0 }) else { throw StorageCompatibilityError.incompleteSource }+ var tokenUsage: TokenUsage?+ if tokenUsageCounts.values.allSatisfy({ $0 != nil }) {+ let decoded = try JSONDecoder().decode(TokenUsage.self, from: data)+ guard decoded.isValid else { throw StorageCompatibilityError.incompleteSource }+ tokenUsage = decoded+ }+ let tokenUsageRecord = TokenUsageSyncRecord(+ aggregationVersion: aggregationVersion, deviceID: deviceID, date: date, tokenUsage: tokenUsage,+ updatedAt: record["updatedAt"] as? Date ?? record.modificationDate+ )+ guard tokenUsageRecord.id == record.recordID.recordName else { throw StorageCompatibilityError.incompleteSource }+ return tokenUsageRecord+ }+}++typealias TokenUsageSyncCache = SyncCache<TokenUsageSyncRecord, TokenUsageSyncState>++nonisolated struct TokenUsageSyncState: Codable, Equatable {+ var containerIdentifier = SyncCloudKit.containerIdentifier+ let deviceID: String+}++nonisolated extension SyncCache where Record == TokenUsageSyncRecord, State == TokenUsageSyncState {+ @discardableResult+ mutating func prune(now: Date = Date()) -> Bool {+ let cutoff = HistoryStorage.dateKey(for: HistoryStorage.retentionCutoffDate(today: now))+ let retained = records.filter { $0.value.date >= cutoff }+ guard retained.count != records.count else { return false }+ records = retained+ // 本地先过期, 云端删除仍可能失败. 下次完整拉取让过期记录重新进入删除流程+ cursor = nil+ return true+ }+}++private nonisolated enum TokenUsageSyncError: Error {+ case missingRecordResult+}diff --git a/CodexBar/Views/Log/LogPreview.swift b/CodexBar/Views/Log/LogPreview.swiftindex 9f3858d..6a0b45a 100644--- a/CodexBar/Views/Log/LogPreview.swift+++ b/CodexBar/Views/Log/LogPreview.swift@@ -1,17 +1,21 @@import AppKit+import Observationimport SwiftUI// MARK: - JSON 预览与高亮/// 日志预览文本, JSON 时会带基础语法高亮-struct LogCodePreview {+private nonisolated struct LogCodePreview {let attributedText: NSAttributedStringlet language: String?}/// 将 JSON 格式化并做轻量 token 高亮, 非 JSON 保持纯文本-enum LogCodePreviewFormatter {- private static let font = NSFont.monospacedSystemFont(ofSize: 12, weight: .regular)+private nonisolated enum LogCodePreviewFormatter {+ private static var font: NSFont {+ NSFont.monospacedSystemFont(ofSize: 12, weight: .regular)+ }+private static var baseAttributes: [NSAttributedString.Key: Any] {[.font: font,@@ -30,9 +34,10 @@ enum LogCodePreviewFormatter {pattern: #""(?:\\.|[^"\\])*"|-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?\b|\b(?:true|false|null)\b|[{}\[\]:,]"#)- static func preview(for text: String) -> LogCodePreview {+ static func preview(for text: String) throws -> LogCodePreview {+ try Task.checkCancellation()if let formattedJSON = formattedJSON(text) {- return LogCodePreview(+ return try LogCodePreview(attributedText: highlightedJSON(formattedJSON),language: "JSON")@@ -64,7 +69,8 @@ enum LogCodePreviewFormatter {)}- private static func highlightedJSON(_ text: String) -> NSAttributedString {+ private static func highlightedJSON(_ text: String) throws -> NSAttributedString {+ try Task.checkCancellation()let attributedText = NSMutableAttributedString(string: text,attributes: baseAttributes@@ -76,7 +82,11 @@ enum LogCodePreviewFormatter {let nsText = text as NSStringlet fullRange = NSRange(location: 0, length: nsText.length)- tokenRegex.enumerateMatches(in: text, range: fullRange) { match, _, _ in+ tokenRegex.enumerateMatches(in: text, range: fullRange) { match, _, stop in+ guard !Task.isCancelled else {+ stop.pointee = true+ return+ }guard let range = match?.range else {return}@@ -89,6 +99,7 @@ enum LogCodePreviewFormatter {)}+ try Task.checkCancellation()return attributedText}@@ -130,67 +141,210 @@ enum LogCodePreviewFormatter {}}-/// AppKit 文本视图承载完整日志, 支持横向滚动-struct LogCodePreviewView: NSViewRepresentable {- let attributedText: NSAttributedString+// MARK: - 后台预处理++/// 排版期间仅由后台 actor 使用, 完成后整体转交主 actor+nonisolated struct LogPreviewDocument {+ let storage: NSTextStorage+ let container: NSTextContainer+ let contentSize: NSSize+ let language: String?+ let characterCount: Int+}++nonisolated struct LogInlinePreviewInput: Equatable, Sendable {+ let request: String?+ let detail: String?+ let needsSummary: Bool+}++nonisolated struct LogInlinePreview: Sendable {+ struct Payload: Sendable {+ let text: String+ let hasText: Bool+ }++ let input: LogInlinePreviewInput+ let request: Payload?+ let detail: Payload?+ let summary: String?+}++actor LogPreviewPreparer {+ static let shared = LogPreviewPreparer()++ func document(for text: String) throws -> sending LogPreviewDocument {+ try Task.checkCancellation()+ let preview = try LogCodePreviewFormatter.preview(for: text)+ try Task.checkCancellation()++ let storage = NSTextStorage(attributedString: preview.attributedText)+ let manager = NSLayoutManager()+ // 排版图不连接视图, 禁止 AppKit 自行调度后台排版+ manager.backgroundLayoutEnabled = false+ let container = NSTextContainer(containerSize: NSSize(+ width: CGFloat.greatestFiniteMagnitude,+ height: CGFloat.greatestFiniteMagnitude+ ))+ container.widthTracksTextView = false+ manager.addTextContainer(container)+ storage.addLayoutManager(manager)+ try Task.checkCancellation()+ manager.ensureLayout(for: container)+ let usedRect = manager.usedRect(for: container)+ try Task.checkCancellation()+ return LogPreviewDocument(+ storage: storage,+ container: container,+ contentSize: NSSize(width: ceil(usedRect.maxX), height: ceil(usedRect.maxY)),+ language: preview.language,+ characterCount: text.count+ )+ }++ func inlinePreview(for input: LogInlinePreviewInput) throws -> LogInlinePreview {+ try Task.checkCancellation()+ let request = input.request.map(inlinePayload)+ try Task.checkCancellation()+ let detail = input.detail.map(inlinePayload)+ let summary = input.needsSummary ? input.detail.map {+ AppServerLogEntry.singleLinePreview($0, limit: AppServerLogEntry.summaryPreviewLength)+ } : nil+ try Task.checkCancellation()+ return LogInlinePreview(input: input, request: request, detail: detail, summary: summary)+ }++ private func inlinePayload(_ text: String) -> LogInlinePreview.Payload {+ LogInlinePreview.Payload(+ text: AppServerLogEntry.singleLinePreview(text, limit: AppServerLogEntry.expandedInlinePreviewLength),+ hasText: !text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty+ )+ }+}++// MARK: - 预览生命周期++@Observable+final class LogPreviewPresentation {+ struct Request: Identifiable {+ let id = UUID()+ let entryID: UUID+ let title: String+ let text: String+ }++ struct Item: Identifiable {+ let request: Request+ let document: LogPreviewDocument+ var id: UUID {+ request.id+ }+ }++ private(set) var request: Request?+ var item: Item?++ func show(entryID: UUID, title: String, text: String) {+ request = Request(entryID: entryID, title: title, text: text)+ item = nil+ }++ func prepare(_ request: Request) async {+ guard self.request?.id == request.id else { return }+ do {+ let document = try await LogPreviewPreparer.shared.document(for: request.text)+ try Task.checkCancellation()+ guard self.request?.id == request.id else { return }+ item = Item(request: request, document: document)+ } catch is CancellationError {+ // 关闭窗口或选择其他日志后, 丢弃尚未展示的结果+ } catch {+ assertionFailure("Unexpected log preview error: \(error)")+ }+ }++ func dismiss(_ item: Item) {+ guard request?.id == item.id else { return }+ cancel()+ }- func makeNSView(context _: Context) -> NSScrollView {- let scrollView = NSScrollView()- scrollView.hasVerticalScroller = true- scrollView.hasHorizontalScroller = true- scrollView.autohidesScrollers = false- scrollView.drawsBackground = true- scrollView.backgroundColor = .textBackgroundColor+ func cancel(for entryID: UUID) {+ guard request?.entryID == entryID else { return }+ cancel()+ }++ func cancel() {+ request = nil+ item = nil+ }+}++// MARK: - 原生文本视图++/// 保留后台生成的文本存储和排版缓存, 窗口缩放只调整可滚动区域+final class LogPreviewScrollView: NSScrollView {+ private var previewDocument: LogPreviewDocument?+ private static let textInset = NSSize(width: 12, height: 12)++ init(document: LogPreviewDocument) {+ super.init(frame: .zero)+ hasVerticalScroller = true+ hasHorizontalScroller = true+ autohidesScrollers = false+ drawsBackground = true+ backgroundColor = .textBackgroundColor+ display(document)+ }- let textView = NSTextView()+ @available(*, unavailable)+ required init?(coder _: NSCoder) {+ fatalError("init(coder:) is unavailable")+ }++ func display(_ document: LogPreviewDocument) {+ guard previewDocument?.storage !== document.storage else { return }+ previewDocument = document+ let size = fittedSize(for: document)+ let textView = NSTextView(frame: NSRect(origin: .zero, size: size), textContainer: document.container)textView.isEditable = falsetextView.isSelectable = truetextView.isRichText = truetextView.drawsBackground = truetextView.backgroundColor = .textBackgroundColor- textView.textContainerInset = NSSize(width: 12, height: 12)- textView.minSize = NSSize(width: 0, height: scrollView.contentSize.height)- textView.maxSize = NSSize(- width: CGFloat.greatestFiniteMagnitude,- height: CGFloat.greatestFiniteMagnitude- )+ textView.textContainerInset = Self.textInset+ textView.minSize = .zero+ textView.maxSize = NSSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)textView.isVerticallyResizable = truetextView.isHorizontallyResizable = truetextView.autoresizingMask = []- textView.textContainer?.containerSize = NSSize(- width: CGFloat.greatestFiniteMagnitude,- height: CGFloat.greatestFiniteMagnitude- )- textView.textContainer?.widthTracksTextView = false-- scrollView.documentView = textView- return scrollView+ documentView = textView}- func updateNSView(_ scrollView: NSScrollView, context _: Context) {- guard let textView = scrollView.documentView as? NSTextView else {- return+ override func layout() {+ super.layout()+ guard let previewDocument, let documentView else { return }+ let size = fittedSize(for: previewDocument)+ if documentView.frame.size != size {+ documentView.setFrameSize(size)}+ }- textView.textStorage?.setAttributedString(attributedText)- updateDocumentSize(for: textView, in: scrollView)+ private func fittedSize(for document: LogPreviewDocument) -> NSSize {+ NSSize(+ width: max(document.contentSize.width + Self.textInset.width * 2, contentSize.width),+ height: max(document.contentSize.height + Self.textInset.height * 2, contentSize.height)+ )}+}- private func updateDocumentSize(for textView: NSTextView, in scrollView: NSScrollView) {- guard let layoutManager = textView.layoutManager,- let textContainer = textView.textContainer else {- return- }+struct LogCodePreviewView: NSViewRepresentable {+ let document: LogPreviewDocument- layoutManager.ensureLayout(for: textContainer)- let usedRect = layoutManager.usedRect(for: textContainer)- let inset = textView.textContainerInset- let minimumSize = scrollView.contentSize- let fittedSize = NSSize(- width: max(ceil(usedRect.maxX + inset.width * 2), minimumSize.width),- height: max(ceil(usedRect.maxY + inset.height * 2), minimumSize.height)- )+ func makeNSView(context _: Context) -> LogPreviewScrollView {+ LogPreviewScrollView(document: document)+ }- textView.setFrameSize(fittedSize)+ func updateNSView(_ scrollView: LogPreviewScrollView, context _: Context) {+ scrollView.display(document)}}diff --git a/CodexBar/Views/Log/LogView.swift b/CodexBar/Views/Log/LogView.swiftindex 69313eb..d4ca137 100644--- a/CodexBar/Views/Log/LogView.swift+++ b/CodexBar/Views/Log/LogView.swift@@ -4,6 +4,7 @@ import SwiftUI/// app-server 交互日志窗口根视图struct LogView: View {@ObservedObject var store: AppServerLogViewModel+ @State private var preview = LogPreviewPresentation()var body: some View {// 每轮渲染只取一次发布快照复用@@ -28,6 +29,16 @@ struct LogView: View {}}.frame(minWidth: 640, minHeight: 480)+ .task(id: preview.request?.id) {+ if let request = preview.request {+ await preview.prepare(request)+ }+ }+ .sheet(item: $preview.item) { item in+ FullLogTextView(item: item)+ .onDisappear { preview.dismiss(item) }+ }+ .onDisappear { preview.cancel() }}private var header: some View {@@ -44,7 +55,9 @@ struct LogView: View {.numericTransition(value: store.totalCount, comparison: Double(store.totalCount)).padding(.horizontal, 7).padding(.vertical, 2)- .background(Capsule().fill(.quaternary))+ .background {+ Capsule().fill(.quaternary)+ }Spacer()@@ -76,7 +89,7 @@ struct LogView: View {ScrollView {LazyVStack(alignment: .leading, spacing: 0) {ForEach(entries) { entry in- LogRow(entry: entry)+ LogRow(entry: entry, preview: preview)Divider()}if store.hasMore {@@ -97,7 +110,8 @@ struct LogView: View {private struct LogRow: View {let entry: AppServerLogEntry@State private var isExpanded = false- @State private var fullTextItem: FullLogTextItem?+ let preview: LogPreviewPresentation+ @State private var inlinePreview: LogInlinePreview?var body: some View {VStack(alignment: .leading, spacing: 0) {@@ -117,6 +131,7 @@ private struct LogRow: View {caption: "log.payload.request",time: entry.requestedAt,text: request,+ inline: currentInlinePreview?.request,color: .primary)}@@ -126,6 +141,7 @@ private struct LogRow: View {caption: detailCaption,time: entry.respondedAt,text: detail,+ inline: currentInlinePreview?.detail,color: entry.status == .failure ? .red : .primary)} else if entry.status == .pending {@@ -141,9 +157,28 @@ private struct LogRow: View {}.padding(.horizontal, 16).padding(.vertical, 8)- .sheet(item: $fullTextItem) { item in- FullLogTextView(item: item)+ .task(id: inlineInput) {+ let input = inlineInput+ guard inlinePreview?.input != input else { return }+ do {+ let result = try await LogPreviewPreparer.shared.inlinePreview(for: input)+ try Task.checkCancellation()+ inlinePreview = result+ } catch is CancellationError {+ // 行离开列表或正文更新后, 由下一次任务生成摘要+ } catch {+ assertionFailure("Unexpected inline preview error: \(error)")+ }}+ .onDisappear { preview.cancel(for: entry.id) }+ }++ private var inlineInput: LogInlinePreviewInput {+ LogInlinePreviewInput(request: entry.request, detail: entry.detail, needsSummary: entry.method == nil)+ }++ private var currentInlinePreview: LogInlinePreview? {+ inlinePreview?.input == inlineInput ? inlinePreview : nil}private var summary: some View {@@ -163,19 +198,19 @@ private struct LogRow: View {.foregroundStyle(entry.status.tint).padding(.horizontal, 6).padding(.vertical, 1)- .background(+ .background {RoundedRectangle(cornerRadius: 4, style: .continuous).fill(entry.status.tint.opacity(0.14))- )+ }if let method = entry.method {Text(method).font(.caption.weight(.medium).monospaced()).foregroundStyle(.primary)- } else if let detail = entry.detail {+ } else if entry.detail != nil {// 无方法名的记录 (信息/进程级错误) 直接预览正文// 避免标签后留空- Text(AppServerLogEntry.singleLinePreview(detail, limit: AppServerLogEntry.summaryPreviewLength))+ Text(verbatim: currentInlinePreview?.summary ?? "").font(.caption).foregroundStyle(.secondary).lineLimit(1)@@ -191,14 +226,10 @@ private struct LogRow: View {caption: LocalizedStringResource,time: Date?,text: String,+ inline: LogInlinePreview.Payload?,color: Color) -> some View {let caption = String(localized: caption)- let hasText = !text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty- let displayText = AppServerLogEntry.singleLinePreview(- text,- limit: AppServerLogEntry.expandedInlinePreviewLength- )return VStack(alignment: .leading, spacing: 2) {HStack(spacing: 6) {@@ -212,9 +243,9 @@ private struct LogRow: View {.foregroundStyle(.tertiary)}- if hasText {+ if inline?.hasText == true {Button {- fullTextItem = FullLogTextItem(title: caption, text: text)+ preview.show(entryID: entry.id, title: caption, text: text)} label: {Label("common.action.preview", systemImage: "doc.text.magnifyingglass")}@@ -230,7 +261,7 @@ private struct LogRow: View {.controlSize(.small).font(.caption)- Text(verbatim: displayText)+ Text(verbatim: inline?.text ?? "").font(.caption.monospaced()).foregroundStyle(color).lineLimit(1)@@ -262,50 +293,37 @@ private struct LogRow: View {// MARK: - 全文查看-/// 预览弹窗数据源-private struct FullLogTextItem: Identifiable {- let id = UUID()- let title: String- let text: String-}-/// 完整请求/响应预览弹窗private struct FullLogTextView: View {- let item: FullLogTextItem+ let item: LogPreviewPresentation.Item@Environment(\.dismiss) private var dismiss- private let preview: LogCodePreview-- init(item: FullLogTextItem) {- self.item = item- preview = LogCodePreviewFormatter.preview(for: item.text)- }var body: some View {VStack(spacing: 0) {HStack(spacing: 10) {- Text(item.title)+ Text(item.request.title).font(.headline)- Text(verbatim: "\(item.text.count)")+ Text(verbatim: "\(item.document.characterCount)").font(.caption.monospacedDigit()).foregroundStyle(.secondary)- if let language = preview.language {+ if let language = item.document.language {Text(language).font(.caption2.weight(.semibold)).foregroundStyle(.secondary).padding(.horizontal, 6).padding(.vertical, 2)- .background(+ .background {RoundedRectangle(cornerRadius: 4, style: .continuous).fill(.quaternary)- )+ }}Spacer()Button {- PasteboardWriter.copy(item.text)+ PasteboardWriter.copy(item.request.text)} label: {Label("common.action.copy", systemImage: "doc.on.doc")}@@ -323,7 +341,8 @@ private struct FullLogTextView: View {Divider()- LogCodePreviewView(attributedText: preview.attributedText)+ LogCodePreviewView(document: item.document)+ .id(item.id).frame(minWidth: 860, minHeight: 600)}}diff --git a/CodexBar/Views/Menu/ActivityCard.swift b/CodexBar/Views/Menu/ActivityCard.swiftindex c9841c9..3d86d8d 100644--- a/CodexBar/Views/Menu/ActivityCard.swift+++ b/CodexBar/Views/Menu/ActivityCard.swift@@ -61,10 +61,10 @@ struct ActivityCard: View {.frame(minHeight: content.detail == nil ? nil : Metrics.height).id(snapshot.hasTaskCenterContent).transition(.opacity)- if let usage = content.tokenUsage {+ if let tokenUsage = content.tokenUsage {VStack(spacing: 8) {LiquidGlassDivider()- tokenUsageMetrics(usage)+ tokenUsageMetrics(tokenUsage)}.frame(height: Metrics.usageHeight - Metrics.height, alignment: .top).transition(.modifier(@@ -203,28 +203,28 @@ struct ActivityCard: View {}}- private func tokenUsageMetrics(_ usage: TokenUsage) -> some View {+ private func tokenUsageMetrics(_ tokenUsage: TokenUsage) -> some View {HStack(alignment: .top, spacing: 0) {- tokenMetric("usage.tokens.total", tokens: usage.totalTokens)- tokenMetric("usage.tokens.input", tokens: usage.inputTokens)- tokenMetric("usage.tokens.output", tokens: usage.outputTokens)- tokenMetric("usage.tokens.cached-input", tokens: usage.cachedInputTokens)- tokenMetric("usage.tokens.cache-write-input", tokens: usage.cacheWriteInputTokens)+ tokenMetric("usage.token-usage.total", tokens: tokenUsage.totalTokens)+ tokenMetric("usage.token-usage.input", tokens: tokenUsage.inputTokens)+ tokenMetric("usage.token-usage.output", tokens: tokenUsage.outputTokens)+ tokenMetric("usage.token-usage.cached-input", tokens: tokenUsage.cachedInputTokens)+ tokenMetric("usage.token-usage.cache-write-input", tokens: tokenUsage.cacheWriteInputTokens)VStack(spacing: 3) {- Text("usage.tokens.cache-hit-rate")+ Text("usage.token-usage.cache-hit-rate").font(.caption2).foregroundStyle(Color.codexSecondaryLabel).minimumScaleFactor(0.65)- Text(usage.cacheHitRate.map { $0.formatted(.percent.precision(.fractionLength(0 ... 1))) } ?? "—")+ Text(tokenUsage.cacheHitRate.map { $0.formatted(.percent.precision(.fractionLength(0 ... 1))) } ?? "—").font(.caption2.monospacedDigit().weight(.semibold)).foregroundStyle(Color.codexLabel)- .numericTransition(value: usage.cacheHitRate, comparison: usage.cacheHitRate ?? 0, enabled: allowsAnimations)+ .numericTransition(value: tokenUsage.cacheHitRate, comparison: tokenUsage.cacheHitRate ?? 0, enabled: allowsAnimations)}.frame(minWidth: 0, maxWidth: .infinity)- tokenMetric("usage.tokens.reasoning-output", tokens: usage.reasoningOutputTokens)+ tokenMetric("usage.token-usage.reasoning-output", tokens: tokenUsage.reasoningOutputTokens)}.lineLimit(1)- .animation(allowsAnimations ? .codexStatus : nil, value: usage)+ .animation(allowsAnimations ? .codexStatus : nil, value: tokenUsage)}private func tokenMetric(_ title: LocalizedStringKey, tokens: Int64) -> some View {@@ -350,7 +350,7 @@ struct ActivityCard: View {// MARK: - 动画/// 用展开进度控制显隐, 让动画中途反向时保持连贯-private struct ActivityTokenReveal: AnimatableModifier {+private struct ActivityTokenReveal: ViewModifier, Animatable {var progress: CGFloatvar animatableData: CGFloat {diff --git a/CodexBar/Views/Menu/ActivityCenterView.swift b/CodexBar/Views/Menu/ActivityCenterView.swiftindex 72e0efd..158e9b3 100644--- a/CodexBar/Views/Menu/ActivityCenterView.swift+++ b/CodexBar/Views/Menu/ActivityCenterView.swift@@ -337,7 +337,7 @@ struct ActivityCenterView: View {.numericTransition(value: detail, enabled: allowsAnimations)if let tokenUsage {Spacer(minLength: 0)- TokenUsageText(usage: tokenUsage)+ TokenUsageText(tokenUsage: tokenUsage)}}}diff --git a/CodexBar/Views/Menu/CodexStatusMenuSections.swift b/CodexBar/Views/Menu/CodexStatusMenuSections.swiftindex 9e8ca8e..9899b17 100644--- a/CodexBar/Views/Menu/CodexStatusMenuSections.swift+++ b/CodexBar/Views/Menu/CodexStatusMenuSections.swift@@ -119,16 +119,22 @@ struct StatusAccountCard: View {.liquidGlassSurface(cornerRadius: MenuMetrics.panelCornerRadius)}- private var statusDisplay: (text: LocalizedStringResource?, color: Color) {+ /// 使用命名类型避免编译器遗漏 switch 元组分支中的本地化引用+ private struct StatusDisplay {+ let text: LocalizedStringResource?+ let color: Color+ }++ private var statusDisplay: StatusDisplay {switch loadState {case .notLoggedIn:- ("codex-status.account.not-signed-in", .orange)+ StatusDisplay(text: LocalizedStringResource("codex-status.account.not-signed-in"), color: .orange)case let .unsupportedVersion(minimum):- (LocalizedStringResource("codex-version.requirement", defaultValue: "\(minimum)"), .red)+ StatusDisplay(text: LocalizedStringResource("codex-version.requirement", defaultValue: "\(minimum)"), color: .red)case .initializationFailed:- ("初始化失败", .red)+ StatusDisplay(text: LocalizedStringResource("codex-status.account.initialization-failed"), color: .red)case .loading, .loaded:- (nil, .accentColor)+ StatusDisplay(text: nil, color: .accentColor)}}}diff --git a/CodexBar/Views/Menu/CodexStatusMenuView.swift b/CodexBar/Views/Menu/CodexStatusMenuView.swiftindex 503dbdb..ac40cc1 100644--- a/CodexBar/Views/Menu/CodexStatusMenuView.swift+++ b/CodexBar/Views/Menu/CodexStatusMenuView.swift@@ -242,7 +242,7 @@ private extension CodexStatusMenuView {case .quota:viewModel.snapshot?.limits.isEmpty == falsecase .usage:- viewModel.snapshot != nil || !historyViewModel.snapshot.dailyMetrics.isEmpty+ viewModel.snapshot != nil || !historyViewModel.snapshot.activityData.isEmpty|| !historyViewModel.snapshot.tokenUsageByDate.isEmptycase .account, .activity, .status:false@@ -282,7 +282,7 @@ private extension CodexStatusMenuView {}}-private struct ActivitySectionReveal: AnimatableModifier {+private struct ActivitySectionReveal: ViewModifier, Animatable {var progress: CGFloatvar animatableData: CGFloat {diff --git a/CodexBar/Views/Menu/HeatmapDetailView.swift b/CodexBar/Views/Menu/HeatmapDetailView.swiftindex 42fc974..8655738 100644--- a/CodexBar/Views/Menu/HeatmapDetailView.swift+++ b/CodexBar/Views/Menu/HeatmapDetailView.swift@@ -52,7 +52,7 @@ struct HeatmapDetailView: View {HStack(alignment: .top, spacing: Metrics.columnSpacing) {VStack(alignment: .leading, spacing: Metrics.sectionSpacing) {- columnTitle("usage.heatmap.activity-metrics")+ columnTitle("usage.heatmap.activity-data-metrics")Grid(alignment: .leading, horizontalSpacing: Metrics.metricRowSpacing, verticalSpacing: Metrics.metricSpacing) {mostUsedModelMetricRowForEach(activityMetricRows) { row in@@ -67,8 +67,8 @@ struct HeatmapDetailView: View {.frame(width: 1)VStack(alignment: .leading, spacing: Metrics.sectionSpacing) {- columnTitle("usage.heatmap.token-metrics")- dailyTokenMetrics+ columnTitle("usage.heatmap.token-usage-metrics")+ dailyTokenUsageMetrics}.frame(minWidth: 0, maxWidth: .infinity)}@@ -83,18 +83,18 @@ struct HeatmapDetailView: View {.frame(height: Metrics.metricRowHeight)}- private var dailyTokenMetrics: some View {- let usage = context.day.tokenUsage- let cacheHitRate = usage?.cacheHitRate ?? 0+ private var dailyTokenUsageMetrics: some View {+ let tokenUsage = context.day.tokenUsage+ let cacheHitRate = tokenUsage?.cacheHitRate ?? 0return Grid(alignment: .leading, horizontalSpacing: Metrics.metricRowSpacing, verticalSpacing: Metrics.metricSpacing) {- dailyTokenRow("usage.tokens.total", tokens: usage?.totalTokens, tint: .blue)- dailyTokenRow("usage.tokens.input", tokens: usage?.inputTokens, tint: .indigo)- dailyTokenRow("usage.tokens.output", tokens: usage?.outputTokens, tint: .orange)- dailyTokenRow("usage.tokens.cached-input", tokens: usage?.cachedInputTokens, tint: .green)- dailyTokenRow("usage.tokens.cache-write-input", tokens: usage?.cacheWriteInputTokens, tint: .purple)+ dailyTokenUsageRow("usage.token-usage.total", tokens: tokenUsage?.totalTokens, tint: .blue)+ dailyTokenUsageRow("usage.token-usage.input", tokens: tokenUsage?.inputTokens, tint: .indigo)+ dailyTokenUsageRow("usage.token-usage.output", tokens: tokenUsage?.outputTokens, tint: .orange)+ dailyTokenUsageRow("usage.token-usage.cached-input", tokens: tokenUsage?.cachedInputTokens, tint: .green)+ dailyTokenUsageRow("usage.token-usage.cache-write-input", tokens: tokenUsage?.cacheWriteInputTokens, tint: .purple)metricRowLayout {metricDot(tint: .teal)- Text("usage.tokens.cache-hit-rate")+ Text("usage.token-usage.cache-hit-rate").foregroundStyle(.secondary).fixedSize(horizontal: true, vertical: false).frame(height: Metrics.metricRowHeight)@@ -103,11 +103,11 @@ struct HeatmapDetailView: View {comparison: cacheHitRate)}- dailyTokenRow("usage.tokens.reasoning-output", tokens: usage?.reasoningOutputTokens, tint: .cyan)+ dailyTokenUsageRow("usage.token-usage.reasoning-output", tokens: tokenUsage?.reasoningOutputTokens, tint: .cyan)}}- private func dailyTokenRow(_ title: LocalizedStringKey, tokens: Int64?, tint: Color) -> some View {+ private func dailyTokenUsageRow(_ title: LocalizedStringKey, tokens: Int64?, tint: Color) -> some View {let count = tokens ?? 0return metricRowLayout {metricDot(tint: tint)@@ -134,7 +134,7 @@ struct HeatmapDetailView: View {.fixedSize(horizontal: true, vertical: false).frame(height: Metrics.metricRowHeight)- fittingModelValue(context.day.history.mostUsedModel ?? "--")+ fittingModelValue(context.day.activityData.mostUsedModel ?? "--")}}@@ -163,33 +163,33 @@ struct HeatmapDetailView: View {private var activityMetricRows: [ActivityMetricRow] {[ActivityMetricRow(- label: String(localized: "history.metric.sessions"),- value: context.day.history.threadCount,+ label: String(localized: "history.activity-data.threads"),+ value: context.day.activityData.threadCount,tint: .green),ActivityMetricRow(- label: String(localized: "history.metric.turns"),- value: context.day.history.turnCount,+ label: String(localized: "history.activity-data.turns"),+ value: context.day.activityData.turnCount,tint: .teal),ActivityMetricRow(- label: String(localized: "history.metric.subagents"),- value: context.day.history.subagentCount,+ label: String(localized: "history.activity-data.subagents"),+ value: context.day.activityData.subagentCount,tint: .indigo),ActivityMetricRow(- label: String(localized: "history.metric.tool-calls"),- value: context.day.history.toolCallCount,+ label: String(localized: "history.activity-data.tool-calls"),+ value: context.day.activityData.toolCallCount,tint: .orange),ActivityMetricRow(- label: String(localized: "history.metric.approvals"),- value: context.day.history.approvalRequestedCount,+ label: String(localized: "history.activity-data.approvals"),+ value: context.day.activityData.approvalRequestedCount,tint: .red),ActivityMetricRow(- label: String(localized: "history.metric.compactions"),- value: context.day.history.contextCompactionCount,+ label: String(localized: "history.activity-data.compactions"),+ value: context.day.activityData.contextCompactionCount,tint: .purple)]diff --git a/CodexBar/Views/Menu/TokenCountText.swift b/CodexBar/Views/Menu/TokenCountText.swiftindex 4e031f4..0563f52 100644--- a/CodexBar/Views/Menu/TokenCountText.swift+++ b/CodexBar/Views/Menu/TokenCountText.swift@@ -37,11 +37,11 @@ struct TokenCountText: View {}struct TokenUsageText: View {- let usage: TokenUsage+ let tokenUsage: TokenUsagevar body: some View {HStack(spacing: 3) {- TokenCountText(tokens: Int(usage.totalTokens), font: .caption2.monospacedDigit())+ TokenCountText(tokens: Int(tokenUsage.totalTokens), font: .caption2.monospacedDigit())Text(verbatim: "tokens").font(.caption2)}diff --git a/CodexBar/Views/Menu/UsageHeatmap.swift b/CodexBar/Views/Menu/UsageHeatmap.swiftindex cd3f38b..f221f33 100644--- a/CodexBar/Views/Menu/UsageHeatmap.swift+++ b/CodexBar/Views/Menu/UsageHeatmap.swift@@ -17,9 +17,13 @@ nonisolated enum UsageHeatmapDetailSide: Equatable {/// hover 单元格在热力图中的列/行位置, 用于吸附和详情定位nonisolated struct UsageHeatmapSelection: Equatable {- let day: UsageHeatmapDay+ let dateID: Stringlet column: Intlet row: Int++ func day(in days: [UsageHeatmapDay?]) -> UsageHeatmapDay? {+ days.lazy.compactMap(\.self).first { $0.id == dateID }+ }}// MARK: - 热力图与 hover 吸附@@ -94,7 +98,7 @@ struct UsageHeatmap: View {UsageHeatmapSquare(day: day,percent: Double(day.tokensForHeatmap) / Double(peakTokens),- isHovered: snapSelection?.day.id == day.id+ isHovered: snapSelection?.dateID == day.id).opacity(showsSquares ? 1 : 0).scaleEffect(showsSquares ? 1 : Metrics.entranceScale)@@ -237,7 +241,7 @@ struct UsageHeatmap: View {return nil}- return UsageHeatmapSelection(day: day, column: column, row: row)+ return UsageHeatmapSelection(dateID: day.id, column: column, row: row)}private func activate(_ target: UsageHeatmapSelection) {@@ -267,7 +271,7 @@ struct UsageHeatmap: View {}private func matches(_ lhs: UsageHeatmapSelection?, _ rhs: UsageHeatmapSelection) -> Bool {- lhs?.day.id == rhs.day.id && lhs?.column == rhs.column && lhs?.row == rhs.row+ lhs?.dateID == rhs.dateID && lhs?.column == rhs.column && lhs?.row == rhs.row}}diff --git a/CodexBar/Views/Menu/UsageSummaryView.swift b/CodexBar/Views/Menu/UsageSummaryView.swiftindex d5c8e70..72e3658 100644--- a/CodexBar/Views/Menu/UsageSummaryView.swift+++ b/CodexBar/Views/Menu/UsageSummaryView.swift@@ -45,7 +45,7 @@ struct UsageSummaryView: View {clearHover()}.onChange(of: days) { _, newDays in- refreshHoveredDay(from: newDays)+ clearMissingHoverSelection(in: newDays)}.onChange(of: hoverContext) { _, context inonHoverContextChange(context)@@ -81,7 +81,7 @@ struct UsageSummaryView: View {value: Self.dayText(usage?.summary.longestStreakDays))textMetric(- label: "usage.summary.longest-chat",+ label: "usage.summary.longest-turn",value: Self.durationText(seconds: usage?.summary.longestRunningTurnSec))}@@ -150,39 +150,20 @@ struct UsageSummaryView: View {private var hoverContext: UsageHeatmapHoverContext? {// 详情面板由控制器显示, SwiftUI 这里只负责传递屏幕坐标和数据- hoverSelection.map {- UsageHeatmapHoverContext(- day: $0.day,- isStale: isStale,- alignmentScreenFrame: heatmapScreenFrame,- preferredSide: UsageHeatmap.Metrics.preferredDetailSide(for: $0.column),- peakTokens: peakTokens- )- }+ guard let hoverSelection, let day = hoverSelection.day(in: days) else { return nil }+ return UsageHeatmapHoverContext(+ day: day,+ isStale: isStale,+ alignmentScreenFrame: heatmapScreenFrame,+ preferredSide: UsageHeatmap.Metrics.preferredDetailSide(for: hoverSelection.column),+ peakTokens: peakTokens+ )}- private func refreshHoveredDay(from days: [UsageHeatmapDay?]) {- guard let hoverSelection else {- return- }-- guard let updatedDay = days.compactMap(\.self).first(where: { $0.id == hoverSelection.day.id }) else {- withAnimation(Metrics.statusAnimation) {- self.hoverSelection = nil- }- return- }-- guard updatedDay != hoverSelection.day else {- return- }-+ private func clearMissingHoverSelection(in days: [UsageHeatmapDay?]) {+ guard let hoverSelection, hoverSelection.day(in: days) == nil else { return }withAnimation(Metrics.statusAnimation) {- self.hoverSelection = UsageHeatmapSelection(- day: updatedDay,- column: hoverSelection.column,- row: hoverSelection.row- )+ self.hoverSelection = nil}}diff --git a/CodexBar/Views/Settings/AppSettingsView.swift b/CodexBar/Views/Settings/AppSettingsView.swiftindex 28c0fc5..c364fa8 100644--- a/CodexBar/Views/Settings/AppSettingsView.swift+++ b/CodexBar/Views/Settings/AppSettingsView.swift@@ -150,13 +150,27 @@ struct AppSettingsView: View {} message: { result inText(verbatim: result.message)}- .alert(item: $helperFeatureConfirmation) { feature in- feature.alert(helperStatus: keepAliveController.helperStatus) {+ .alert(+ helperFeatureConfirmation?.title ?? Text(verbatim: ""),+ isPresented: Binding(+ get: { helperFeatureConfirmation != nil },+ set: {+ if !$0 {+ helperFeatureConfirmation = nil+ }+ }+ ),+ presenting: helperFeatureConfirmation+ ) { feature in+ Button("common.action.enable", role: .destructive) {switch feature {case .autoReset: autoResetSettings.setEnabled(true)case .keepAlive: keepAliveController.setEnabled(true)}}+ Button("common.action.cancel", role: .cancel) {}+ } message: { feature in+ Text(verbatim: feature.message(helperStatus: keepAliveController.helperStatus))}}}@@ -763,9 +777,7 @@ private extension AppSettingsView {func refreshRebuildableDates() {Task {let dates = await Task.detached(priority: .utility) {- let eventDates = HistoryStorage.rebuildableEventDateKeys()- let tokenDates = await (try? TokenHistoryStore().rebuildableDateKeys()) ?? []- return Set(eventDates + tokenDates).sorted()+ HistoryStorage.rebuildableEventDateKeys()}.valuerebuildableDates = dates}@@ -821,12 +833,12 @@ private extension AppSettingsView {localized: "history.rebuild.summary.completed",defaultValue: "\(summary.rebuiltDateCount, specifier: "%lld")\(summary.eventCount, specifier: "%lld")")- message += String(localized: "history.rebuild.summary.token-record-count", defaultValue: "\(summary.tokenTurnCount, specifier: "%lld")")- if summary.didFailTokenRebuild {- message += String(localized: "history.rebuild.summary.token-history-incomplete")+ message += String(localized: "history.rebuild.summary.token-usage-record-count", defaultValue: "\(summary.tokenUsageEventCount, specifier: "%lld")")+ if summary.didFailTokenUsageRebuild {+ message += String(localized: "history.rebuild.summary.token-usage-history-incomplete")}- let retryDates = summary.failedDateKeys.filter { !summary.failedRequestDateKeys.contains($0) }+ let retryDates = summary.failedDateKeysif !retryDates.isEmpty {let listed = retryDates.prefix(rebuildFailedDateListLimit)var dates = listed.joined(separator: ", ")@@ -837,9 +849,6 @@ private extension AppSettingsView {message += String(localized: "history.rebuild.summary.retry-later")}- if !summary.failedRequestDateKeys.isEmpty {- message += String(localized: "history.rebuild.summary.request-failed")- }if summary.isSyncPending {message += String(localized: "history.rebuild.summary.cloud-replacement-pending")}diff --git a/CodexBar/Views/Settings/CodexVersionSection.swift b/CodexBar/Views/Settings/CodexVersionSection.swiftindex e04c638..d5f95c2 100644--- a/CodexBar/Views/Settings/CodexVersionSection.swift+++ b/CodexBar/Views/Settings/CodexVersionSection.swift@@ -97,7 +97,7 @@ struct CodexVersionSection: View {.padding(.vertical, 2).liquidGlassCapsule(tint: .green)- Text(info.version ?? String(localized: "codex.version.unknown"))+ Text(info.version).monospacedDigit().foregroundStyle(Color.codexSecondaryLabel).numericTransition(value: info.version, enabled: animationState.allowsAnimations)diff --git a/CodexBar/Views/Settings/HelperFeatureConfirmation.swift b/CodexBar/Views/Settings/HelperFeatureConfirmation.swiftindex 8687458..4a160ce 100644--- a/CodexBar/Views/Settings/HelperFeatureConfirmation.swift+++ b/CodexBar/Views/Settings/HelperFeatureConfirmation.swift@@ -1,26 +1,10 @@import SwiftUI-enum HelperFeatureConfirmation: Identifiable {+enum HelperFeatureConfirmation {case autoResetcase keepAlive- var id: Self {- self- }-- func alert(- helperStatus: KeepAliveController.HelperStatus,- onConfirm: @escaping () -> Void- ) -> Alert {- Alert(- title: title,- message: Text(verbatim: message(helperStatus: helperStatus)),- primaryButton: .destructive(Text("common.action.enable"), action: onConfirm),- secondaryButton: .cancel(Text("common.action.cancel"))- )- }-- private var title: Text {+ var title: Text {switch self {case .autoReset:Text("auto-reset.confirmation.title")@@ -38,7 +22,7 @@ enum HelperFeatureConfirmation: Identifiable {}}- private func message(helperStatus: KeepAliveController.HelperStatus) -> String {+ func message(helperStatus: KeepAliveController.HelperStatus) -> String {switch helperStatus {case .notRegistered, .notFound:"\(String(localized: "helper.status.install-and-authorize"))\n\(details)"diff --git a/CodexBarTests/Activity/ActivityArchitectureTests.swift b/CodexBarTests/Activity/ActivityArchitectureTests.swiftindex f5eece2..728a4a4 100644--- a/CodexBarTests/Activity/ActivityArchitectureTests.swift+++ b/CodexBarTests/Activity/ActivityArchitectureTests.swift@@ -17,19 +17,19 @@ struct ActivityArchitectureTests {monitor.consume(snapshot ? .snapshotEvents([event]) : .live([event]))let old = ActivityTaskKey(thread: "thread-a", turn: "turn-a")let current = ActivityTaskKey(thread: "thread-a", turn: "turn-b")- let id = try #require(monitor.tasks[current]?.displayID)- #expect(monitor.tasks[old] == nil)- #expect(monitor.pendingTerminalTasks[old] != nil)+ let id = try #require(monitor.graph.tasks[current]?.displayID)+ #expect(monitor.graph.tasks[old] == nil)+ #expect(monitor.graph.pendingTerminalTasks[old] != nil)monitor.consume(.snapshotEvents([event, TestFixtures.event(at: now)]))- #expect(monitor.tasks[current]?.displayID == id)- #expect(monitor.tasks.count == 1)+ #expect(monitor.graph.tasks[current]?.displayID == id)+ #expect(monitor.graph.tasks.count == 1)monitor.consume(.live([TestFixtures.event(.turnCompleted, at: now.addingTimeInterval(1))]))- #expect(monitor.pendingTerminalTasks.isEmpty)- #expect(monitor.completions.count == 1)- #expect(monitor.terminations.isEmpty)+ #expect(monitor.graph.pendingTerminalTasks.isEmpty)+ #expect(monitor.graph.completions.count == 1)+ #expect(monitor.graph.terminations.isEmpty)monitor.consume(.live([TestFixtures.event(.turnCompleted, at: now.addingTimeInterval(2), turn: "turn-b")]))monitor.consume(.snapshotEvents([event]))- #expect(monitor.tasks.isEmpty)+ #expect(monitor.graph.tasks.isEmpty)}@Test func creationNoticeUpdatesRootWithoutInventingExecution() throws {@@ -41,22 +41,22 @@ struct ActivityArchitectureTests {defer { monitor.stop() }let now = Date()monitor.consume(.snapshotEvents([TestFixtures.event(at: now)]))- var event = ActivityRecord(- timestamp: now.addingTimeInterval(1), name: ActivityEventKind.subagentStarted.rawValue, origin: .main,+ var event = ActivityDataRecord(+ timestamp: now.addingTimeInterval(1), name: ActivityDataEventKind.subagentStarted.rawValue, origin: .main,cwd: "/child-project", toolName: nil, model: "child-model", effort: "low",- threadID: "thread-a", turnID: nil, agentID: "child"+ threadID: "thread-a", turnID: nil, agentThreadID: "child")- event.context = ActivityContext(+ event.context = ActivityDataContext(method: "item/completed", threadID: "thread-a", turnID: "turn-a",rootThreadID: "thread-a", rootTurnID: "turn-a", agentThreadID: "child", itemKind: "started")monitor.consume(.live([event]))- let task = try #require(monitor.tasks[.init(thread: "thread-a", turn: "turn-a")])+ let task = try #require(monitor.graph.tasks[.init(thread: "thread-a", turn: "turn-a")])#expect(task.modelName == "gpt-5")#expect(task.projectName == "example")#expect(task.effort == "mixed")- #expect(task.executions.keys.allSatisfy { $0.agentID == nil })- #expect(task.subagentsByID["child"]?.isRunning == true)+ #expect(task.executions.keys.allSatisfy { $0.agentThreadID == nil })+ #expect(task.subagentsByThreadID["child"]?.isRunning == true)}@Test func authoritativeChildrenRecoverCountWithoutObservedStarts() throws {@@ -69,30 +69,30 @@ struct ActivityArchitectureTests {let now = Date()monitor.consume(.snapshotEvents([TestFixtures.event(at: now)]))let key = ActivityTaskKey(thread: "thread-a", turn: "turn-a")- #expect(monitor.tasks[key]?.snapshot.activeSubagentCount == nil)+ #expect(monitor.graph.tasks[key]?.snapshot.activeSubagentCount == nil)let root = state("thread-a", at: now)apply([root], to: monitor)- #expect(monitor.tasks[key]?.snapshot.activeSubagentCount == 0)- monitor.tasks[key]?.recordSubagentActivity(agentID: "finished", isStarting: false, at: now)- #expect(monitor.tasks[key]?.snapshot.activeSubagentCount == nil)+ #expect(monitor.graph.tasks[key]?.snapshot.activeSubagentCount == 0)+ monitor.graph.tasks[key]?.recordSubagentActivity(agentThreadID: "finished", isStarting: false, at: now)+ #expect(monitor.graph.tasks[key]?.snapshot.activeSubagentCount == nil)let finished = state("finished", at: now, parent: "thread-a", terminal: .completed(at: now, duration: 1))let active = state("active", at: now, parent: "thread-a")apply([root, finished, active], to: monitor)- #expect(monitor.tasks[key]?.snapshot.activeSubagentCount == 1)+ #expect(monitor.graph.tasks[key]?.snapshot.activeSubagentCount == 1)var unavailable = activeunavailable.readStatus = .unavailableapply([root, finished, unavailable], to: monitor)- #expect(monitor.tasks[key]?.snapshot.activeSubagentCount == nil)+ #expect(monitor.graph.tasks[key]?.snapshot.activeSubagentCount == nil)apply([root, finished, active], to: monitor)- #expect(monitor.tasks[key]?.snapshot.activeSubagentCount == 1)+ #expect(monitor.graph.tasks[key]?.snapshot.activeSubagentCount == 1)apply([root, finished], to: monitor)- #expect(monitor.tasks[key]?.snapshot.activeSubagentCount == nil)+ #expect(monitor.graph.tasks[key]?.snapshot.activeSubagentCount == nil)let ended = state("active", at: now.addingTimeInterval(1), parent: "thread-a", terminal: .completed(at: now.addingTimeInterval(1), duration: 1))apply([root, finished, ended], to: monitor)- #expect(monitor.tasks[key]?.snapshot.activeSubagentCount == 0)+ #expect(monitor.graph.tasks[key]?.snapshot.activeSubagentCount == 0)}- @Test func unresolvedAncestryKeepsCountUnknownUntilOwnershipRecovers() async throws {+ @Test func unresolvedAncestryKeepsCountUnknownUntilOwnershipRecovers() throws {let directory = try TestDirectory()defer { try? directory.remove() }let preferences = try TestPreferences()@@ -104,33 +104,31 @@ struct ActivityArchitectureTests {let root = ActivityTurnReference(threadID: "thread-a", turnID: "turn-a")let child = ActivityTurnReference(threadID: "child", turnID: "turn-a")let grandchild = ActivityTurnReference(threadID: "grandchild", turnID: "turn-a")- let cache = SessionLifecycleCache()var states = [root: state("thread-a", at: now), child: state("child", at: now, parent: "thread-a"), grandchild: state("grandchild", at: now, parent: "child")]for reference in [child, grandchild] {states[reference]?.rootThreadID = nilstates[reference]?.rootTurnID = nil}let verified = Dictionary(uniqueKeysWithValues: states.keys.map { ($0, now) })- await cache.replace(states, verifiedTurns: verified)- let incomplete = await cache.lifecycleStates(for: [root], now: now)+ let incomplete = TurnLifecycleSnapshot(states: states, verifiedTurns: verified).lifecycleStates(for: [root], now: now)#expect(incomplete.count == 3)apply(incomplete, to: monitor)let key = ActivityTaskKey(thread: "thread-a", turn: "turn-a")- #expect(monitor.tasks[key]?.snapshot.activeSubagentCount == nil)+ #expect(monitor.graph.tasks[key]?.snapshot.activeSubagentCount == nil)for reference in [child, grandchild] {states[reference]?.rootThreadID = "thread-a"states[reference]?.rootTurnID = "turn-a"}- await cache.replace(states, verifiedTurns: verified)- await apply(cache.lifecycleStates(for: [root], now: now), to: monitor)- #expect(monitor.tasks[key]?.snapshot.activeSubagentCount == 2)+ let snapshot = TurnLifecycleSnapshot(states: states, verifiedTurns: verified)+ apply(snapshot.lifecycleStates(for: [root], now: now), to: monitor)+ #expect(monitor.graph.tasks[key]?.snapshot.activeSubagentCount == 2)let unidentified = TestFixtures.event(.toolStarted, at: now, origin: .auxiliary)- monitor.tasks[key]?.recordExecutionEvent(unidentified)- await apply(cache.lifecycleStates(for: [root], now: now), to: monitor)- #expect(monitor.tasks[key]?.snapshot.activeSubagentCount == nil)+ monitor.graph.tasks[key]?.recordExecutionEvent(unidentified)+ apply(snapshot.lifecycleStates(for: [root], now: now), to: monitor)+ #expect(monitor.graph.tasks[key]?.snapshot.activeSubagentCount == nil)}- @Test func cacheReconciliationIncludesEveryRetainedTaskWithoutDelay() async throws {+ @Test func snapshotReconciliationIncludesEveryRetainedTaskWithoutDelay() throws {let directory = try TestDirectory()defer { try? directory.remove() }let preferences = try TestPreferences()@@ -139,9 +137,9 @@ struct ActivityArchitectureTests {defer { monitor.stop() }let now = Date()var transitions: [ActivityTransition] = []- var terminalStates: [ActivityTurnReference: SessionLifecycleState] = [:]+ var terminalStates: [ActivityTurnReference: TurnLifecycleState] = [:]for index in 0 ..< 20 {- let event = TestFixtures.event(at: now, thread: "session-\(index)")+ let event = TestFixtures.event(at: now, thread: "thread-\(index)")let key = try #require(ActivityTaskKey(event: event))let task = ActivityTask(displayID: UUID(), key: key, event: event, state: .running, startedAt: now, progressGeneration: 1)monitor.resolveTerminal(.completed(at: now, duration: 1), task: task, key: key, abortFallback: now, into: &transitions)@@ -152,23 +150,25 @@ struct ActivityArchitectureTests {let pendingEvent = TestFixtures.event(at: now, thread: "pending-\(index)")let pendingKey = try #require(ActivityTaskKey(event: pendingEvent))let pending = ActivityTask(displayID: UUID(), key: pendingKey, event: pendingEvent, state: .running, startedAt: now, progressGeneration: 1)- monitor.pendingTerminalTasks[pendingKey] = PendingTerminalTask(task: pending, supersededAt: now)+ monitor.graph.pendingTerminalTasks[pendingKey] = PendingTerminalTask(task: pending, supersededAt: now)}- let cache = SessionLifecycleCache()- await cache.replace(terminalStates, verifiedTurns: [:])- #expect(monitor.lifecycleReferences().count == 40)- #expect(await !monitor.applyTerminalTokenUsage(cache.lifecycleStates(for: monitor.lifecycleReferences())))+ let snapshot = TurnLifecycleSnapshot(states: terminalStates, verifiedTurns: [:])+ let observedResult1 = monitor.graph.lifecycleReferences().count == 40+ #expect(observedResult1)+ monitor.graph.applyTerminalTokenUsage(snapshot.lifecycleStates(for: monitor.graph.lifecycleReferences()))+ #expect(monitor.graph.completions.allSatisfy { $0.tokenUsage == nil })for reference in terminalStates.keys {terminalStates[reference]?.tokenUsage = TokenUsage(inputTokens: 100, cachedInputTokens: 0, cacheWriteInputTokens: 0,outputTokens: 0, reasoningOutputTokens: 0, totalTokens: 100)}- await cache.replace(terminalStates, verifiedTurns: [:])- #expect(monitor.lifecycleReferences().count == 40)- #expect(await monitor.applyTerminalTokenUsage(cache.lifecycleStates(for: monitor.lifecycleReferences())))- #expect(monitor.completions.count == 20)- #expect(monitor.completions.allSatisfy { $0.tokenUsage?.totalTokens == 100 })+ let updated = TurnLifecycleSnapshot(states: terminalStates, verifiedTurns: [:])+ let observedResult3 = monitor.graph.lifecycleReferences().count == 40+ #expect(observedResult3)+ monitor.graph.applyTerminalTokenUsage(updated.lifecycleStates(for: monitor.graph.lifecycleReferences()))+ #expect(monitor.graph.completions.count == 20)+ #expect(monitor.graph.completions.allSatisfy { $0.tokenUsage?.totalTokens == 100 })}@Test(arguments: [false, true], [nil, "read", "search"] as [String?])@@ -193,7 +193,7 @@ struct ActivityArchitectureTests {#expect(event.commandActionTypes == action.map { [$0] })#expect(event.context?.itemType == "commandExecution")#expect(reducer.states.values.first?.pendingApprovals.values.first?.commandActionTypes == action.map { [$0] })- let stored = try #require(AppServerEventRecord.decode(from: AppServerEventRecord(activity: event).jsonLineData()).activity)+ let stored = try #require(AppServerEventRecord.decode(from: AppServerEventRecord(activityData: event).jsonLineData()).activityData)#expect(stored.commandActionTypes == event.commandActionTypes)}@@ -218,7 +218,7 @@ struct ActivityArchitectureTests {monitor.consume(.live(reducer.consume(started, now: now)))}let key = ActivityTaskKey(thread: "main", turn: "turn")- var task = try #require(monitor.tasks[key])+ var task = try #require(monitor.graph.tasks[key])let displayID = task.displayID#expect(task.startedAt == nil)#expect(task.preciseDuration(until: now) == nil)@@ -226,15 +226,15 @@ struct ActivityArchitectureTests {let corrected = ActivityTurn(id: "turn", status: .running, startedAt: startedAt)_ = reducer.reconcile(thread: thread, turns: [corrected], now: now)let state = try #require(reducer.states.values.first)- #expect(ActivityMonitor.mergeLifecycleBackfill(from: state, into: &task))+ ActivityTaskGraph.mergeLifecycleBackfill(from: state, into: &task)#expect(task.startedAt == startedAt)#expect(task.preciseDuration(until: now) == 60)#expect(task.displayID == displayID)- monitor.tasks[key] = task+ monitor.graph.tasks[key] = taskmonitor.consume(.snapshotEvents(reducer.reconcile(thread: thread, turns: [ActivityTurn(id: "turn", status: .running)], now: now, bootstrap: true)))- #expect(monitor.tasks[key]?.startedAt == startedAt)+ #expect(monitor.graph.tasks[key]?.startedAt == startedAt)}@Test(arguments: ["completed", "failed", "interrupted"], [@@ -266,8 +266,8 @@ struct ActivityArchitectureTests {// 后续快照缺少时间字段时保留已经确认的终态时间和耗时_ = reducer.reconcile(thread: thread, turns: [ActivityTurn(id: "turn", status: ActivityTurnStatus(rawValue: status) ?? .unknown)], now: now)let lifecycle = try #require(reducer.states.values.first)- var task = try #require(monitor.tasks[key])- _ = ActivityMonitor.mergeLifecycleBackfill(from: lifecycle, into: &task)+ var task = try #require(monitor.graph.tasks[key])+ ActivityTaskGraph.mergeLifecycleBackfill(from: lifecycle, into: &task)var transitions: [ActivityTransition] = []try monitor.resolveTerminal(#require(lifecycle.terminal), task: task, key: key, abortFallback: now, into: &transitions)} else {@@ -280,10 +280,10 @@ struct ActivityArchitectureTests {monitor.consume(.live(reducer.consume(completed, now: now)))}if status == "completed" {- #expect(monitor.completions.first?.duration == 1.5)+ #expect(monitor.graph.completions.first?.duration == 1.5)} else {- #expect(monitor.terminations.first?.duration == 1.5)- #expect(monitor.terminations.first?.isFailure == (status == "failed"))+ #expect(monitor.graph.terminations.first?.duration == 1.5)+ #expect(monitor.graph.terminations.first?.isFailure == (status == "failed"))}}@@ -291,15 +291,15 @@ struct ActivityArchitectureTests {try TestFixtures.decode(ActivityThread.self, #"{"id":"main","source":"cli","status":{"type":"active"}}"#)}- private func apply(_ states: [SessionLifecycleState], to monitor: ActivityMonitor) {+ private func apply(_ states: [TurnLifecycleState], to monitor: ActivityMonitor) {for state in states {- _ = monitor.applySubagentLifecycle(state, terminalOnly: false)+ monitor.applySubagentLifecycle(state)}- monitor.reconcileSubagentCounts(states)+ monitor.graph.reconcileSubagentCounts(states)}- private func state(_ id: String, at date: Date, parent: String? = nil, terminal: SessionTerminalState? = nil) -> SessionLifecycleState {- SessionLifecycleState(+ private func state(_ id: String, at date: Date, parent: String? = nil, terminal: TurnTerminalState? = nil) -> TurnLifecycleState {+ TurnLifecycleState(requestedThreadID: id, turnID: "turn-a", startedAt: date, effort: nil,lastProgressAt: date, terminal: terminal, rootTurnID: "turn-a", rootThreadID: "thread-a", parentThreadID: parent)@@ -308,7 +308,7 @@ struct ActivityArchitectureTests {private func makeMonitor(_ directory: TestDirectory, _ preferences: TestPreferences) -> ActivityMonitor {let monitor = ActivityMonitor(protectionSettings: ProtectionSettings(defaults: preferences.defaults),- protectionStore: ProtectionStore(directoryURL: directory.url), activityDirectoryURL: directory.url+ protectionStore: ProtectionStore(directoryURL: directory.url), activityDataDirectoryURL: directory.url)monitor.isActivitySourceHealthy = truereturn monitordiff --git a/CodexBarTests/Activity/ActivityBackfillTests.swift b/CodexBarTests/Activity/ActivityBackfillTests.swiftnew file mode 100644index 0000000..7d5b4e7--- /dev/null+++ b/CodexBarTests/Activity/ActivityBackfillTests.swift@@ -0,0 +1,584 @@+import Foundation+import Synchronization+import Testing++struct ActivityBackfillTests {+ nonisolated enum BackfillOutcome: CaseIterable { case complete, stop, invalidResponse, liveCreation }++ @Test(arguments: [false, true])+ func shrinksSameCursorAndStopsAtOversizedItem(singleItemTooLarge: Bool) async throws {+ let attempts = Mutex<[Int]>([])+ let server = try SharedServerFixture(connections: singleItemTooLarge ? 5 : 4) { peer in+ try Self.initialize(peer)+ let request = try peer.readMessage()+ let params = try #require(request["params"] as? [String: Any])+ #expect(request["method"] as? String == "thread/items/list")+ #expect(params["threadId"] as? String == "parent")+ #expect(params["turnId"] as? String == "root")+ #expect(params["sortDirection"] as? String == "desc")+ #expect(params["cursor"] as? String == "same-cursor")+ let limit = try #require(params["limit"] as? Int)+ attempts.withLock { $0.append(limit) }+ if limit > 2 || singleItemTooLarge {+ try peer.sendHeader(length: 4097)+ } else {+ try peer.reply(to: request, result: ["data": [["turnId": "root", "item": ["id": "tool", "type": "commandExecution"]]], "nextCursor": "next-page"])+ }+ }+ defer { server.close() }+ let backfill = AppServerActivityBackfill(socketURL: server.url, logStorage: nil, maximumMessageSize: 4096)+ do {+ let page = try await backfill.readPage(reference: .init(threadID: "parent", turnID: "root"), cursor: "same-cursor", limit: 20, timeout: 3)+ #expect(!singleItemTooLarge)+ #expect(page.nextCursor == "next-page")+ #expect(page.limit == 2)+ #expect(page.items.first?.item.id == "tool")+ } catch let error as CodexStatusError {+ #expect(singleItemTooLarge)+ guard case .messageTooLarge = error else { Issue.record("Wrong error: \(error)")+ return+ }+ }+ #expect(attempts.withLock { $0 } == (singleItemTooLarge ? [20, 10, 5, 2, 1] : [20, 10, 5, 2]))+ try server.finish()+ }++ @Test func shrinkingRetriesShareOneDeadline() async throws {+ let release = DispatchSemaphore(value: 0)+ defer { release.signal() }+ let requests = Mutex(0)+ let server = try SharedServerFixture(connections: 2) { peer in+ try Self.initialize(peer)+ _ = try peer.readMessage()+ let attempt = requests.withLock { $0 += 1+ return $0+ }+ if attempt == 1 {+ Thread.sleep(forTimeInterval: 0.15)+ try peer.sendHeader(length: 4097)+ } else {+ _ = release.wait(timeout: .now() + 2)+ }+ }+ defer { server.close() }+ let backfill = AppServerActivityBackfill(socketURL: server.url, logStorage: nil, maximumMessageSize: 4096)+ let started = Date()+ do {+ _ = try await backfill.readPage(reference: .init(threadID: "parent", turnID: "root"), cursor: nil, limit: 20, timeout: 0.3)+ Issue.record("Expected shared deadline")+ } catch let error as CodexStatusError {+ guard case .serverTimeout = error else {+ Issue.record("Wrong error: \(error)")+ return+ }+ }+ #expect(Date().timeIntervalSince(started) < 1)+ #expect(requests.withLock { $0 } == 2)+ release.signal()+ try server.finish()+ }++ @Test(arguments: BackfillOutcome.allCases)+ func slowItemResponseDoesNotBlockLiveCollectionOrDrain(outcome: BackfillOutcome) async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let itemsStarted = DispatchSemaphore(value: 0)+ let releaseItems = DispatchSemaphore(value: 0)+ defer { itemsStarted.signal()+ releaseItems.signal()+ }+ let timestamp = Date().addingTimeInterval(-60).timeIntervalSince1970+ let server = try SharedServerFixture(connections: outcome == .stop ? 2 : 3, concurrentConnections: true) { peer in+ try Self.serveLiveDuringBackfill(peer, itemsStarted: itemsStarted, releaseItems: releaseItems, timestamp: timestamp, outcome: outcome)+ }+ defer { server.close() }+ let store = TokenUsageHistoryStore(directoryURL: directory.url)+ let logs = AppServerLogStore(directoryURL: directory.url.appendingPathComponent("Logs"))+ let probe = ActivityReaderProbe()+ var ready = false+ var updates = 0+ var unavailable = false+ let reader = AppServerActivityReader(+ probe: probe, socketURL: server.url, logStorage: logs, tokenUsageHistory: store, activityDataRecorder: ActivityDataRecorder(directoryURL: directory.url)+ ) {+ updates += 1+ if $0.phase == .bootstrapEnd {+ ready = true+ }+ if $0.phase == .sourceUnavailable {+ unavailable = true+ }+ }+ await reader.start()+ let key = ActivityTaskKey(thread: "parent", turn: "root-turn")+ for _ in 0 ..< 300 {+ if ready, probe.graph.tasks[key]?.tokenUsage?.totalTokens == 50 {+ break+ }+ try await Task.sleep(for: .milliseconds(10))+ }+ #expect(ready)+ #expect(probe.graph.tasks[key]?.tokenUsage?.totalTokens == 50)+ #expect(await reader.drainNow().status == .completed)+ #expect(!unavailable)+ #expect(probe.graph.tasks[key]?.state == .running)+ let progress = probe.graph.tasks[key]?.progressGeneration+ let references = [ActivityTurnReference(threadID: "child", turnID: "child-turn")]+ let before = await reader.lifecycleStatesForTesting(for: references).first+ #expect(before?.effort == "high")+ if outcome == .stop {+ await reader.stop()+ let stoppedUpdates = updates+ releaseItems.signal()+ try await Task.sleep(for: .milliseconds(200))+ #expect(updates == stoppedUpdates)+ } else {+ let beforeBackfillUpdates = updates+ releaseItems.signal()+ if outcome == .invalidResponse {+ try await Self.waitForBackfillFailure(logs)+ } else {+ for _ in 0 ..< 200 {+ if updates > beforeBackfillUpdates {+ break+ }+ try await Task.sleep(for: .milliseconds(10))+ }+ #expect(updates > beforeBackfillUpdates)+ }+ let after = await reader.lifecycleStatesForTesting(for: references).first+ #expect(after?.effort == (outcome == .complete ? "mixed" : "high"))+ #expect(after?.verifiedAt == before?.verifiedAt)+ #expect(probe.graph.tasks[key]?.progressGeneration == progress)+ #expect(probe.graph.tasks[key]?.state == .running)+ #expect(probe.graph.tasks[key]?.tokenUsage?.totalTokens == 50)+ #expect(await reader.drainNow().status == .completed)+ #expect(!unavailable)+ try await Self.waitForItemReads(logs, count: outcome == .invalidResponse ? 1 : 2)+ await reader.stop()+ }+ #expect(try await store.refresh().first?.tokenUsage.totalTokens == 50)+ try server.finish()+ }++ private static func waitForItemReads(_ logs: AppServerLogStore, count: Int) async throws {+ for _ in 0 ..< 200 {+ let successes = try await logs.page().entries.filter { $0.method == "thread/items/list" && $0.status == .success }.count+ if successes >= count {+ return+ }+ try await Task.sleep(for: .milliseconds(10))+ }+ Issue.record("Item pages did not finish")+ }++ private static func waitForBackfillFailure(_ logs: AppServerLogStore) async throws {+ for _ in 0 ..< 200 {+ if try await logs.page().entries.contains(where: { $0.method == "activity/backfill" && $0.status == .failure }) {+ break+ }+ try await Task.sleep(for: .milliseconds(10))+ }+ #expect(try await logs.page().entries.contains { $0.method == "activity/backfill" && $0.status == .failure })+ }++ private nonisolated static func serveLiveDuringBackfill(+ _ peer: SharedServerFixture.Peer, itemsStarted: DispatchSemaphore, releaseItems: DispatchSemaphore, timestamp: Double, outcome: BackfillOutcome+ ) throws {+ let name = try Self.initialize(peer)+ if name == "codex_bar_activity_backfill" {+ let request = try peer.readMessage()+ let params = try #require(request["params"] as? [String: Any])+ #expect(request["method"] as? String == "thread/items/list")+ if params["threadId"] as? String == "child" {+ try peer.reply(to: request, result: ["data": []])+ return+ }+ var body = try JSONSerialization.data(withJSONObject: ["id": #require(request["id"]), "result": ["data": [[+ "turnId": "root-turn", "startedAtMs": timestamp * 1000, "completedAtMs": (timestamp + 1) * 1000,+ "item": [+ "id": "spawn",+ "type": "subAgentActivity",+ "kind": "started",+ "agentThreadId": "child",+ "reasoningEffort": "low",+ "ignoredOutput": String(repeating: "a", count: 200000)+ ]+ ]]]])+ if outcome == .invalidResponse {+ body = try JSONSerialization.data(withJSONObject: ["id": #require(request["id"]), "result": ["data": "invalid", "padding": String(repeating: "a", count: 200000)]])+ }+ try peer.sendHeader(length: UInt64(body.count))+ try peer.sendBytes(body.prefix(100))+ itemsStarted.signal()+ #expect(releaseItems.wait(timeout: .now() + 5) == .success)+ if outcome == .stop {+ try? peer.sendBytes(body.dropFirst(100))+ } else {+ try peer.sendBytes(body.dropFirst(100))+ }+ return+ }+ var sent = false+ while let request = try? peer.readMessage() {+ let params = request["params"] as? [String: Any] ?? [:]+ let id = params["threadId"] as? String ?? ""+ switch request["method"] as? String {+ case "thread/loaded/list":+ try peer.reply(to: request, result: ["data": ["parent", "child"]])+ case "thread/read", "thread/resume":+ var thread: [String: Any] = ["id": id, "source": "cli", "status": ["type": "active"], "reasoningEffort": "high"]+ if id == "child" {+ thread["parentThreadId"] = "parent"+ }+ try peer.reply(to: request, result: ["thread": thread])+ case "thread/turns/list":+ #expect(params["itemsView"] as? String == "notLoaded")+ try peer.reply(to: request, result: ["data": [[+ "id": id == "parent" ? "root-turn" : "child-turn", "rootTurnId": "root-turn", "status": "inProgress", "startedAt": timestamp+ ]]])+ if id == "parent", !sent {+ sent = true+ #expect(itemsStarted.wait(timeout: .now() + 3) == .success)+ try Self.sendLiveUpdates(peer, timestamp: timestamp)+ if outcome == .liveCreation {+ try peer.send(["method": "item/completed", "params": ["threadId": "parent", "turnId": "root-turn", "item": [+ "id": "live-spawn", "type": "subAgentActivity", "kind": "started", "agentThreadId": "child", "reasoningEffort": "high"+ ]]])+ }+ }+ default: Issue.record("Unexpected request")+ }+ }+ }++ @discardableResult+ private nonisolated static func initialize(_ peer: SharedServerFixture.Peer) throws -> String? {+ let request = try peer.readMessage()+ #expect(request["method"] as? String == "initialize")+ let client = (request["params"] as? [String: Any])?["clientInfo"] as? [String: Any]+ try peer.reply(to: request, result: ["userAgent": "codex/0.162.0"])+ #expect(try peer.readMessage()["method"] as? String == "initialized")+ return client?["name"] as? String+ }++ private nonisolated static func sendLiveUpdates(_ peer: SharedServerFixture.Peer, timestamp: Double) throws {+ for index in 0 ..< 30 {+ try peer.send(["method": "item/started", "params": [+ "threadId": "parent", "turnId": "root-turn", "item": ["id": "tool-\(index)", "type": "commandExecution"]+ ]])+ }+ try peer.send(["id": "approval", "method": "item/commandExecution/requestApproval", "params": [+ "threadId": "parent", "turnId": "root-turn", "itemId": "approval-tool", "startedAtMs": (timestamp + 10) * 1000+ ]])+ try peer.send(["method": "serverRequest/resolved", "params": ["threadId": "parent", "requestId": "approval"]])+ try peer.send(["method": "thread/status/changed", "params": ["threadId": "parent", "status": ["type": "active", "activeFlags": []]]])+ for total in [100, 120, 150] {+ let usage = ["inputTokens": total, "cachedInputTokens": 0, "outputTokens": 0, "reasoningOutputTokens": 0, "totalTokens": total]+ try peer.send(["method": "thread/tokenUsage/updated", "params": [+ "threadId": "parent", "turnId": "root-turn", "tokenUsage": ["total": usage, "last": usage]+ ]])+ }+ }+}++extension ActivityBackfillTests {+ @Test func newSubagentDoesNotRestartCurrentHistoryScan() async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let firstPage = DispatchSemaphore(value: 0)+ let releasePage = DispatchSemaphore(value: 0)+ defer { firstPage.signal()+ releasePage.signal()+ }+ let cursors = Mutex<[String?]>([])+ let timestamp = Date().addingTimeInterval(-60).timeIntervalSince1970+ let server = try SharedServerFixture(connections: 4, concurrentConnections: true) { peer in+ if try Self.initialize(peer) == "codex_bar_activity_backfill" {+ let request = try peer.readMessage()+ let params = try #require(request["params"] as? [String: Any])+ if params["threadId"] as? String == "child" {+ try peer.reply(to: request, result: ["data": []])+ return+ }+ let cursor = params["cursor"] as? String+ let index = cursors.withLock { $0.append(cursor)+ return $0.count+ }+ if index == 1 {+ firstPage.signal()+ #expect(releasePage.wait(timeout: .now() + 3) == .success)+ try peer.reply(to: request, result: ["data": [], "nextCursor": "older"])+ } else {+ let items = ["child", "new-child"].map { child in+ ["turnId": "root", "item": [+ "id": "spawn-" + child,+ "type": "subAgentActivity",+ "kind": "started",+ "agentThreadId": child,+ "reasoningEffort": "low"+ ]] as [String: Any]+ }+ try peer.reply(to: request, result: ["data": items])+ }+ } else {+ try Self.serveNewSubagent(peer, timestamp: timestamp, firstPage: firstPage)+ }+ }+ defer { server.close() }+ let reader = AppServerActivityReader(+ socketURL: server.url, logStorage: nil, tokenUsageHistory: TokenUsageHistoryStore(directoryURL: directory.url),+ activityDataRecorder: ActivityDataRecorder(directoryURL: directory.url), onBatch: { _ in }+ )+ await reader.start()+ let references = [ActivityTurnReference(threadID: "new-child", turnID: "new-child-turn"), .init(threadID: "child", turnID: "child-turn")]+ for _ in 0 ..< 200 {+ if await reader.lifecycleStatesForTesting(for: references).contains(where: { $0.requestedThreadID == "new-child" && $0.startedAt != nil }) {+ break+ }+ try await Task.sleep(for: .milliseconds(10))+ }+ #expect(await reader.lifecycleStatesForTesting(for: references).contains { $0.requestedThreadID == "new-child" && $0.startedAt != nil })+ releasePage.signal()+ for _ in 0 ..< 200 {+ if await reader.lifecycleStatesForTesting(for: references).allSatisfy({ $0.effort == "low" }) {+ break+ }+ try await Task.sleep(for: .milliseconds(10))+ }+ #expect(await reader.lifecycleStatesForTesting(for: references).allSatisfy { $0.effort == "low" })+ #expect(cursors.withLock { $0 } == [nil, "older"])+ #expect(await !reader.historyTargetsForTesting().contains(.init(threadID: "new-child", turnID: "new-child-turn")))+ await reader.stop()+ try server.finish()+ }++ private nonisolated static func serveNewSubagent(_ peer: SharedServerFixture.Peer, timestamp: Double, firstPage: DispatchSemaphore) throws {+ var sent = false+ while let request = try? peer.readMessage() {+ let id = (request["params"] as? [String: Any])?["threadId"] as? String ?? ""+ switch request["method"] as? String {+ case "thread/loaded/list": try peer.reply(to: request, result: ["data": ["parent", "child"]])+ case "thread/read", "thread/resume":+ var thread: [String: Any] = ["id": id, "source": "cli", "status": ["type": "active"]]+ if id != "parent" {+ thread["parentThreadId"] = "parent"+ }+ try peer.reply(to: request, result: ["thread": thread])+ case "thread/turns/list":+ try peer.reply(to: request, result: ["data": [[+ "id": id == "parent" ? "root" : id + "-turn", "rootTurnId": "root", "status": "inProgress", "startedAt": timestamp+ ]]])+ if id == "parent", !sent {+ sent = true+ #expect(firstPage.wait(timeout: .now() + 3) == .success)+ try peer.send(["method": "thread/started", "params": ["thread": [+ "id": "new-child", "parentThreadId": "parent", "source": "cli", "status": ["type": "active"]+ ]]])+ try peer.send(["method": "turn/started", "params": ["threadId": "new-child", "turn": [+ "id": "new-child-turn", "rootTurnId": "root", "status": "inProgress", "startedAt": Date().timeIntervalSince1970+ ]]])+ }+ default: Issue.record("Unexpected request")+ }+ }+ }+}++extension ActivityBackfillTests {+ @Test(arguments: [false, true])+ func historyAndLiveActivityShareIdentityWithoutReplayingState(historyFirst: Bool) async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let now = Date()+ let reference = ActivityTurnReference(threadID: "main", turnID: "root")+ var reducer = AppServerActivityReducer()+ _ = reducer.reconcile(+ thread: .init(id: "main", status: .init(type: .active), origin: .main),+ turns: [.init(id: "root", status: .running, rootTurnId: "root", startedAt: now.addingTimeInterval(-60))],+ now: now+ )+ let started = try TestFixtures.decode(ActivityInput.self, """+ {"method":"item/started","params":{"threadId":"main","turnId":"root","item":{"id":"tool","type":"commandExecution","commandActions":[+ {"type":"search"},{"type":"read"},{"type":"read"}]}}}+ """)+ let live = reducer.consume(started, now: now)+ let before = try #require(reducer.states[reference])+ let entry = ActivityItemEntry(+ turnId: "root",+ item: ActivityItem(+ id: "tool",+ type: .commandExecution,+ status: "completed",+ commandActions: [.init(type: "search"), .init(type: "read"), .init(type: "read")]+ ),+ startedAtMs: now.addingTimeInterval(-30).timeIntervalSince1970 * 1000,+ completedAtMs: now.addingTimeInterval(-20).timeIntervalSince1970 * 1000+ )+ let history = reducer.historicalEvents([entry], reference: reference, now: now.addingTimeInterval(10))+ #expect(history.first?.id == live.first?.id)+ #expect(reducer.states[reference]?.lastProgressAt == before.lastProgressAt)+ #expect(reducer.states[reference]?.isWaitingApproval == before.isWaitingApproval)+ #expect(reducer.states[reference]?.terminal == nil)+ let recorder = ActivityDataRecorder(directoryURL: directory.url)+ try await recorder.record(events: historyFirst ? history : live)+ try await recorder.record(events: historyFirst ? live : history)+ try await ActivityDataRecorder(directoryURL: directory.url).record(events: history)+ let database = try HistoryDatabase(directoryURL: directory.url)+ let aggregate = try database.aggregateActivityData(date: HistoryStorage.dateKey(for: now))+ #expect(aggregate.toolStartedCount == 3)+ #expect(aggregate.toolCompletedCount == 3)+ #expect(aggregate.eventCount == 2)+ #expect(aggregate.approvalRequestedCount == 0)+ }++ @Test func consecutiveReaderRestartsDeduplicateActivityAndContinueTokenBaseline() async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let start = Date().addingTimeInterval(-3600)+ let initial = TokenUsageHistoryStore(directoryURL: directory.url)+ _ = try await initial.record(TestFixtures.tokenSample(100, at: start, start: start))+ _ = try await initial.record(TestFixtures.tokenSample(150, at: start, start: start))+ for (count, total) in [(2, 200), (3, 300)] {+ let server = try SharedServerFixture(connections: 2, concurrentConnections: true) { peer in+ let client = try Self.initialize(peer)+ if client == "codex_bar_activity_backfill" {+ let request = try peer.readMessage()+ #expect(request["method"] as? String == "thread/items/list")+ let entries = (1 ... count).map { index in+ [+ "turnId": "root",+ "startedAtMs": start.timeIntervalSince1970 * 1000,+ "item": ["id": "tool-\(index)", "type": "commandExecution", "status": "inProgress"]+ ] as [String: Any]+ }+ try peer.reply(to: request, result: ["data": entries])+ return+ }+ var emitted = false+ while let request = try? peer.readMessage() {+ switch request["method"] as? String {+ case "thread/loaded/list": try peer.reply(to: request, result: ["data": ["main"]])+ case "thread/read", "thread/resume":+ try peer.reply(to: request, result: ["thread": ["id": "main", "source": "cli", "status": ["type": "active"]]])+ case "thread/turns/list":+ #expect((request["params"] as? [String: Any])?["itemsView"] as? String == "notLoaded")+ try peer.reply(to: request, result: ["data": [["id": "root", "rootTurnId": "root", "status": "inProgress", "startedAt": start.timeIntervalSince1970]]])+ if !emitted {+ emitted = true+ let usage = ["inputTokens": total, "cachedInputTokens": 0, "outputTokens": 0, "reasoningOutputTokens": 0, "totalTokens": total]+ try peer.send(["method": "thread/tokenUsage/updated", "params": ["threadId": "main", "turnId": "root", "tokenUsage": ["total": usage, "last": usage]]])+ try peer.send(["method": "item/started", "params": ["threadId": "main", "turnId": "root", "item": ["id": "tool-\(count)", "type": "commandExecution"]]])+ }+ default: Issue.record("Unexpected request")+ }+ }+ }+ defer { server.close() }+ let probe = ActivityReaderProbe()+ let store = TokenUsageHistoryStore(directoryURL: directory.url)+ let reader = AppServerActivityReader(+ probe: probe,+ socketURL: server.url,+ logStorage: nil,+ tokenUsageHistory: store,+ activityDataRecorder: ActivityDataRecorder(directoryURL: directory.url),+ onBatch: { _ in }+ )+ await reader.start()+ for _ in 0 ..< 300 {+ let tools = try directory.historyRecords().filter { $0.activityData?.eventKind == .toolStarted }.count+ if tools == count, probe.graph.tasks[.init(thread: "main", turn: "root")]?.tokenUsage?.totalTokens == Int64(total - 100) {+ break+ }+ try await Task.sleep(for: .milliseconds(10))+ }+ #expect(probe.graph.tasks[.init(thread: "main", turn: "root")]?.tokenUsage?.totalTokens == Int64(total - 100))+ #expect(await reader.stop())+ let database = try HistoryDatabase(directoryURL: directory.url)+ let aggregate = try database.aggregateActivityData(date: HistoryStorage.dateKey(for: start))+ #expect(aggregate.toolStartedCount == count)+ #expect(aggregate.turnStartedCount == 1)+ #expect(try await store.refresh().first?.tokenUsage.totalTokens == Int64(total - 100))+ try server.finish()+ }+ }+}++extension ActivityBackfillTests {+ @Test func taskStartedWhileReaderIsConnectedDoesNotQueryHistory() async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let startTask = Mutex(false)+ let server = try SharedServerFixture { peer in+ try Self.initialize(peer)+ var startedAt: Double?+ while let request = try? peer.readMessage() {+ switch request["method"] as? String {+ case "thread/loaded/list":+ if startTask.withLock({ $0 }), startedAt == nil {+ let timestamp = Date().timeIntervalSince1970+ startedAt = timestamp+ try peer.send(["method": "thread/started", "params": ["thread": ["id": "main", "source": "cli", "status": ["type": "active"]]]])+ try peer.send(["method": "turn/started", "params": ["threadId": "main", "turn": [+ "id": "root", "rootTurnId": "root", "status": "inProgress", "startedAt": timestamp+ ]]])+ }+ try peer.reply(to: request, result: ["data": startedAt == nil ? [] : ["main"]])+ case "thread/read", "thread/resume":+ try peer.reply(to: request, result: ["thread": ["id": "main", "source": "cli", "status": ["type": "active"]]])+ case "thread/turns/list":+ #expect((request["params"] as? [String: Any])?["itemsView"] as? String == "notLoaded")+ try peer.reply(to: request, result: ["data": [[+ "id": "root", "rootTurnId": "root", "status": "inProgress", "startedAt": #require(startedAt)+ ]]])+ default: Issue.record("Unexpected request")+ }+ }+ }+ defer { server.close() }+ var ready = false+ let probe = ActivityReaderProbe()+ let reader = AppServerActivityReader(+ probe: probe,+ socketURL: server.url,+ logStorage: nil,+ tokenUsageHistory: TokenUsageHistoryStore(directoryURL: directory.url),+ activityDataRecorder: ActivityDataRecorder(directoryURL: directory.url)+ ) {+ if $0.phase == .bootstrapEnd {+ ready = true+ }+ }+ await reader.start()+ for _ in 0 ..< 200 where !ready {+ try await Task.sleep(for: .milliseconds(5))+ }+ #expect(ready)+ startTask.withLock { $0 = true }+ #expect(await reader.drainNow().status == .completed)+ #expect(probe.graph.tasks[.init(thread: "main", turn: "root")]?.state == .running)+ #expect(await reader.historyTargetsForTesting().isEmpty)+ #expect(await reader.stop())+ #expect(try directory.historyRecords().allSatisfy { $0.context?.method != "thread/items/list" })+ try server.finish()+ }++ @Test(arguments: [false, true])+ func invalidHistoryCursorOrTurnFailsWithoutAcceptingPage(wrongTurn: Bool) async throws {+ let server = try SharedServerFixture { peer in+ try Self.initialize(peer)+ let request = try peer.readMessage()+ try peer.reply(to: request, result: [+ "data": [["turnId": wrongTurn ? "other" : "root", "item": ["id": "item", "type": "commandExecution"]]],+ "nextCursor": wrongTurn ? "next" : "cursor"+ ])+ }+ defer { server.close() }+ let backfill = AppServerActivityBackfill(socketURL: server.url, logStorage: nil)+ await #expect(throws: CodexStatusError.self) {+ try await backfill.readPage(reference: .init(threadID: "main", turnID: "root"), cursor: "cursor", limit: 20, timeout: 3)+ }+ try server.finish()+ }+}diff --git a/CodexBarTests/Activity/ActivityIsolationTests.swift b/CodexBarTests/Activity/ActivityIsolationTests.swiftindex 221ed22..93bb58f 100644--- a/CodexBarTests/Activity/ActivityIsolationTests.swift+++ b/CodexBarTests/Activity/ActivityIsolationTests.swift@@ -3,6 +3,61 @@ import Synchronizationimport Testingstruct ActivityIsolationTests {+ @Test func cumulativeTokenNotificationsReachLogsStorageAndLiveTasks() async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let now = Date().addingTimeInterval(-60)+ let server = try SharedServerFixture { peer in+ try peer.reply(to: peer.readMessage(), result: ["userAgent": "codex/0.162.1"])+ _ = try peer.readMessage()+ var emitted = false+ while let request = try? peer.readMessage() {+ switch request["method"] as? String {+ case "thread/loaded/list": try peer.reply(to: request, result: ["data": ["healthy"]])+ case "thread/read", "thread/resume":+ try peer.reply(to: request, result: ["thread": Self.thread("healthy", now: now)])+ case "thread/turns/list":+ try peer.reply(to: request, result: ["data": [["id": "turn", "rootTurnId": "turn", "status": "inProgress", "startedAt": now.timeIntervalSince1970]]])+ if !emitted {+ emitted = true+ for total in [100, 100, 120, 120, 150] {+ let tokenUsage = ["inputTokens": total, "cachedInputTokens": 0, "outputTokens": 0, "reasoningOutputTokens": 0, "totalTokens": total]+ try peer.send(["method": "thread/tokenUsage/updated", "params": [+ "threadId": "healthy", "turnId": "turn", "tokenUsage": ["total": tokenUsage, "last": tokenUsage]+ ]])+ }+ try peer.send(Self.tool(thread: "healthy", id: "received"))+ }+ default: Issue.record("Unexpected request")+ }+ }+ }+ defer { server.close() }+ let store = TokenUsageHistoryStore(directoryURL: directory.url)+ let logs = AppServerLogStore(directoryURL: directory.url.appendingPathComponent("Logs"))+ var received = false+ var liveUsage: Int64?+ let reader = AppServerActivityReader(+ socketURL: server.url, logStorage: logs, tokenUsageHistory: store,+ activityDataRecorder: ActivityDataRecorder(directoryURL: directory.url)+ ) { update in+ if update.phase == .live, !update.graph.tasks.isEmpty {+ received = true+ }+ liveUsage = update.graph.tasks[.init(thread: "healthy", turn: "turn")]?.tokenUsage?.totalTokens+ }+ await reader.start()+ for _ in 0 ..< 200 where !received || liveUsage != 50 {+ try await Task.sleep(for: .milliseconds(10))+ }+ #expect(received)+ #expect(await reader.stop())+ #expect(liveUsage == 50)+ #expect(try await store.refresh().first?.tokenUsage.totalTokens == 50)+ #expect(try await logs.page(limit: 100).entries.filter { $0.method == "thread/tokenUsage/updated" }.count == 5)+ try server.finish()+ }+@Test(arguments: [false, true])func brokenThreadDoesNotStopHealthyThreadAndCanRecover(brokenQuery: Bool) async throws {let directory = try TestDirectory()@@ -45,17 +100,17 @@ struct ActivityIsolationTests {}}defer { server.close() }- let lifecycle = SessionLifecycleCache()+ let lifecycle = ActivityReaderProbe()var observed = falsevar disconnected = falselet reader = AppServerActivityReader(- lifecycleCache: lifecycle, socketURL: server.url,- tokenHistory: TokenHistoryStore(directoryURL: directory.url), recorder: ActivityRecorder(directoryURL: directory.url)+ probe: lifecycle, socketURL: server.url,+ tokenUsageHistory: TokenUsageHistoryStore(directoryURL: directory.url), activityDataRecorder: ActivityDataRecorder(directoryURL: directory.url)) { batch in- if case .sourceUnavailable = batch {+ if case .sourceUnavailable = batch.phase {disconnected = true}- if case let .live(events) = batch, events.contains(where: { $0.threadID == "healthy" }) {+ if batch.phase == .live, batch.graph.tasks.keys.contains(where: { $0.threadID == "healthy" }) {observed = true}}@@ -64,13 +119,13 @@ struct ActivityIsolationTests {try await Task.sleep(for: .milliseconds(10))}#expect(observed && !disconnected)- let references = ["a-broken", "healthy"].map { ActivityTurnReference(threadID: $0, turnID: "turn") }- let states = await lifecycle.lifecycleStates(for: references)- #expect(states.first { $0.requestedThreadID == "healthy" }?.readStatus == .complete)- #expect(states.first { $0.requestedThreadID == "a-broken" }?.readStatus == .unavailable)+ let keys = ["a-broken", "healthy"].map { ActivityTaskKey(thread: $0, turn: "turn") }+ #expect(lifecycle.graph.tasks[keys[1]]?.lifecycleCoverageCheckedAt != nil)+ #expect(lifecycle.graph.tasks[keys[0]]?.lifecycleCoverageCheckedAt == nil)repaired.withLock { $0 = true }- #expect(await reader.drainNow() == .completed)- #expect(await lifecycle.lifecycleStates(for: references).allSatisfy { $0.readStatus == .complete })+ #expect(await reader.drainNow().status == .completed)+ #expect(keys.allSatisfy { lifecycle.graph.tasks[$0]?.lifecycleCoverageCheckedAt != nil })+ #expect(lifecycle.graph.unavailableTurns.isDisjoint(with: keys))#expect(!disconnected)await reader.stop()release.signal()@@ -81,7 +136,7 @@ struct ActivityIsolationTests {func fullStorageBufferKeepsLiveReadingAndRetainsAcceptedEvents(byteLimit: Bool) async throws {let directory = try TestDirectory()defer { try? directory.remove() }- let blocker = try directory.write("blocked", to: "State")+ let blocker = try directory.write("blocked", to: HistoryDatabase.filename)let now = Date().addingTimeInterval(-60)let server = try SharedServerFixture { peer inlet initialize = try peer.readMessage()@@ -109,15 +164,15 @@ struct ActivityIsolationTests {var failures = 0var observed = 0let reader = AppServerActivityReader(- lifecycleCache: SessionLifecycleCache(), socketURL: server.url,- tokenHistory: TokenHistoryStore(directoryURL: directory.url), recorder: ActivityRecorder(directoryURL: directory.url),+ probe: ActivityReaderProbe(), socketURL: server.url,+ tokenUsageHistory: TokenUsageHistoryStore(directoryURL: directory.url), activityDataRecorder: ActivityDataRecorder(directoryURL: directory.url),maximumPendingEvents: byteLimit ? 4096 : 2, maximumPendingBytes: byteLimit ? 1 : 8 * 1024 * 1024) { batch in- if case .sourceUnavailable = batch {+ if case .sourceUnavailable = batch.phase {failures += 1}- if case let .live(events) = batch {- observed += events.count+ if batch.phase == .live {+ observed += 1}}await reader.start()@@ -126,18 +181,10 @@ struct ActivityIsolationTests {}#expect(failures == 0)#expect(observed == 3)- #expect(await reader.drainNow() == .completed)+ #expect(await reader.drainNow().status == .completed)try FileManager.default.removeItem(at: blocker)await reader.stop()- var recorded = 0- let files = try FileManager.default.contentsOfDirectory(at: directory.url.appendingPathComponent("Events"), includingPropertiesForKeys: nil)- for file in files {- try AppServerEventJournal.read(at: file) {- if $0.activity != nil {- recorded += 1- }- }- }+ let recorded = try directory.historyRecords().filter { $0.activityData != nil }.count#expect(recorded == (byteLimit ? 1 : 2))try server.finish()}@@ -155,27 +202,23 @@ extension ActivityIsolationTests {@Test func busyHistoryLockDoesNotDelayLiveTasksOrDrain() async throws {let directory = try TestDirectory()defer { try? directory.remove() }- let lockURL = HistoryStorage.lockURL(in: directory.url)- var descriptor = try JSONFileStorage.acquireLock(in: lockURL.deletingLastPathComponent(), name: lockURL.lastPathComponent, nonblocking: true)- defer {- if let descriptor {- JSONFileStorage.releaseLock(descriptor)- }- }+ let database = try HistoryDatabase(directoryURL: directory.url)+ try database.execute("BEGIN IMMEDIATE")+ defer { try? database.execute("ROLLBACK") }let phase = Mutex(0)let server = try Self.tokenServer(now: Date().addingTimeInterval(-60), phase: { phase.withLock { $0 } })defer { server.close() }- let store = TokenHistoryStore(directoryURL: directory.url)+ let store = TokenUsageHistoryStore(directoryURL: directory.url)var observed = 0var disconnected = falselet reader = AppServerActivityReader(- lifecycleCache: SessionLifecycleCache(), socketURL: server.url, logStorage: nil,- tokenHistory: store, recorder: ActivityRecorder(directoryURL: directory.url)+ probe: ActivityReaderProbe(), socketURL: server.url, logStorage: nil,+ tokenUsageHistory: store, activityDataRecorder: ActivityDataRecorder(directoryURL: directory.url)) { batch in- if case let .live(events) = batch {- observed += events.count+ if batch.phase == .live {+ observed += 1}- if case .sourceUnavailable = batch {+ if case .sourceUnavailable = batch.phase {disconnected = true}}@@ -186,23 +229,22 @@ extension ActivityIsolationTests {#expect(observed == 1)phase.withLock { $0 = 1 }let started = ContinuousClock.now- #expect(await reader.drainNow() == .completed)+ #expect(await reader.drainNow().status == .completed)#expect(ContinuousClock.now - started < .seconds(1))#expect(observed == 2 && !disconnected)- try JSONFileStorage.releaseLock(#require(descriptor))- descriptor = nil+ try database.execute("COMMIT")#expect(await reader.stop())let observations = try Self.observations(in: directory.url)- #expect(observations.map(\.current.totalTokens) == [120, 150])- #expect(try await store.refresh().first?.usage?.totalTokens == 50)- #expect(try await store.refresh().first?.usage?.totalTokens == 50)+ #expect(observations.map { $0.delta?.totalTokens } == [0, 20, 30])+ #expect(try await store.refresh().first?.tokenUsage.totalTokens == 50)+ #expect(try await store.refresh().first?.tokenUsage.totalTokens == 50)try server.finish()}@Test func stopDoesNotWaitIndefinitelyForBusyStorageOrPublishLateResults() async throws {let directory = try TestDirectory()defer { try? directory.remove() }- let store = TokenHistoryStore(directoryURL: directory.url)+ let store = TokenUsageHistoryStore(directoryURL: directory.url)let entered = Mutex(false)let release = DispatchSemaphore(value: 0)defer { release.signal() }@@ -216,12 +258,12 @@ extension ActivityIsolationTests {var observed = 0var batches = 0let reader = AppServerActivityReader(- lifecycleCache: SessionLifecycleCache(), socketURL: server.url, logStorage: nil,- tokenHistory: store, recorder: ActivityRecorder(directoryURL: directory.url)+ probe: ActivityReaderProbe(), socketURL: server.url, logStorage: nil,+ tokenUsageHistory: store, activityDataRecorder: ActivityDataRecorder(directoryURL: directory.url)) { batch inbatches += 1- if case let .live(events) = batch {- observed += events.count+ if batch.phase == .live {+ observed += 1}}await reader.start()@@ -238,8 +280,8 @@ extension ActivityIsolationTests {// 再次停止等待旧工作循环释放资源, 不能发布已停止连接的状态_ = await reader.stop()#expect(batches == countAtStop)- #expect(try await store.acquireRecordingLease())- await store.releaseRecordingLease()+ #expect(try store.acquireRecordingLease())+ store.releaseRecordingLease()try server.finish()}@@ -249,30 +291,30 @@ extension ActivityIsolationTests {defer { try? directory.remove() }let now = Date().addingTimeInterval(-60)let phase = Mutex(0)- let store = TokenHistoryStore(directoryURL: directory.url)+ let store = TokenUsageHistoryStore(directoryURL: directory.url)let oldDate = HistoryStorage.dateKey(for: now.addingTimeInterval(-86400))- func corruptHistory() throws -> URL {- _ = try directory.write("broken cache", to: "Aggregates/tokens.json")- return try directory.writeJournal(Data("broken event\n".utf8), to: "Events/\(oldDate).jsonl")+ func corruptHistory() throws {+ let database = try HistoryDatabase(directoryURL: directory.url)+ try database.execute("INSERT OR IGNORE INTO days(date) VALUES (?)", [.text(oldDate)])+ try database.execute("INSERT INTO events(date, kind, payload) VALUES (?, 'tokenUsage', ?)", [.text(oldDate), .data(Data("broken".utf8))])}- var brokenJournal: URL?if !corruptWhileRunning {- brokenJournal = try corruptHistory()+ try corruptHistory()}let server = try Self.tokenServer(now: now, phase: { phase.withLock { $0 } })defer { server.close() }var observed = 0var disconnected = false- let lifecycle = SessionLifecycleCache()+ let lifecycle = ActivityReaderProbe()let reader = AppServerActivityReader(- lifecycleCache: lifecycle, socketURL: server.url, logStorage: nil,- tokenHistory: store, recorder: ActivityRecorder(directoryURL: directory.url)+ probe: lifecycle, socketURL: server.url, logStorage: nil,+ tokenUsageHistory: store, activityDataRecorder: ActivityDataRecorder(directoryURL: directory.url)) { batch in- if case .sourceUnavailable = batch {+ if case .sourceUnavailable = batch.phase {disconnected = true}- if case let .live(events) = batch {- observed += events.count+ if batch.phase == .live {+ observed += 1}}await reader.start()@@ -281,25 +323,26 @@ extension ActivityIsolationTests {}#expect(observed == 1)if corruptWhileRunning {- brokenJournal = try corruptHistory()+ try corruptHistory()}phase.withLock { $0 = 1 }- #expect(await reader.drainNow() == .completed)+ #expect(await reader.drainNow().status == .completed)#expect(observed == 2)// 给周期缓存刷新一次触发机会, 失败不能使实时连接失效try await Task.sleep(for: .milliseconds(1200))- #expect(await reader.drainNow() == .completed)+ #expect(await reader.drainNow().status == .completed)#expect(!disconnected)- let states = await lifecycle.lifecycleStates(for: [.init(threadID: "healthy", turnID: "turn")])- #expect(states.first?.readStatus == .complete)- await #expect(throws: TokenCacheError.self) { try await store.refresh() }- #expect(try String(contentsOf: directory.url.appendingPathComponent("Aggregates/tokens.json"), encoding: .utf8) == "broken cache")+ let key = ActivityTaskKey(thread: "healthy", turn: "turn")+ #expect(lifecycle.graph.tasks[key]?.lifecycleCoverageCheckedAt != nil)+ #expect(!lifecycle.graph.unavailableTurns.contains(key))+ #expect(try await store.refresh().first?.tokenUsage.totalTokens == 50)+ #expect(try await store.loadSnapshot().coverage == .partial)let observations = try Self.observations(in: directory.url)- #expect(observations.map(\.sequence) == [1, 2])- #expect(observations.map(\.current.totalTokens) == [120, 150])- try FileManager.default.removeItem(at: #require(brokenJournal))+ #expect(Set(observations.map(\.id)).count == 3)+ #expect(observations.map { $0.delta?.totalTokens } == [0, 20, 30])+ try HistoryDatabase(directoryURL: directory.url).execute("DELETE FROM days WHERE date = ?", [.text(oldDate)])let recovered = try await store.refresh()- #expect(recovered.first?.usage?.totalTokens == 50)+ #expect(recovered.first?.tokenUsage.totalTokens == 50)#expect(try await store.refresh() == recovered)#expect(await reader.stop())try server.finish()@@ -318,8 +361,9 @@ extension ActivityIsolationTests {let nextPhase = phase()if nextPhase > emittedPhase {emittedPhase = nextPhase- for total in nextPhase == 1 ? [150] : [200, 250] {- try peer.send(Self.usage(total))+ try peer.send(Self.tokenUsage(nextPhase == 1 ? 50 : 80))+ if nextPhase == 2 {+ try peer.send(Self.tokenUsage(130))}try peer.send(Self.tool(thread: "healthy", id: "phase-\(nextPhase)"))}@@ -330,8 +374,8 @@ extension ActivityIsolationTests {try peer.reply(to: request, result: ["data": [["id": "turn", "rootTurnId": "turn", "status": "inProgress", "startedAt": now.timeIntervalSince1970]]])if !emitted {emitted = true- try peer.send(Self.usage(100))- try peer.send(Self.usage(120))+ try peer.send(Self.tokenUsage(0))+ try peer.send(Self.tokenUsage(20))try peer.send(Self.tool(thread: "healthy", id: "first"))}default: Issue.record("Unexpected request")@@ -340,18 +384,17 @@ extension ActivityIsolationTests {}}- private nonisolated static func observations(in directory: URL) throws -> [TokenObservation] {- let url = HistoryStorage.eventLogURL(for: HistoryStorage.dateKey(for: Date()), in: directory.appendingPathComponent("Events"))- var observations: [TokenObservation] = []- try AppServerEventJournal.read(at: url) {- if let observation = $0.observation {+ private nonisolated static func observations(in directory: URL) throws -> [TokenUsageObservation] {+ var observations: [TokenUsageObservation] = []+ try HistoryDatabase(directoryURL: directory).entries(date: HistoryStorage.dateKey(for: Date())) {+ if let observation = $0.record.tokenUsage {observations.append(observation)}}return observations}- private nonisolated static func usage(_ total: Int) -> [String: Any] {+ private nonisolated static func tokenUsage(_ total: Int) -> [String: Any] {let counts = ["inputTokens": total, "cachedInputTokens": 0, "outputTokens": 0, "reasoningOutputTokens": 0, "totalTokens": total]return ["method": "thread/tokenUsage/updated", "params": ["threadId": "healthy", "turnId": "turn", "tokenUsage": ["total": counts, "last": counts]@@ -359,7 +402,7 @@ extension ActivityIsolationTests {}}-private extension TokenHistoryStore {+private extension TokenUsageHistoryStore {func holdForReaderTest(entered: @Sendable () -> Void, release: DispatchSemaphore) {entered()_ = release.wait(timeout: .now() + 10)@@ -367,25 +410,25 @@ private extension TokenHistoryStore {}extension ActivityIsolationTests {- @Test func fullObservationQueueResumesWithNewBaselineAfterStorageRecovery() async throws {+ @Test func fullObservationQueueContinuesBaselineAfterStorageRecovery() async throws {let directory = try TestDirectory()defer { try? directory.remove() }- let blocker = try directory.write("blocked", to: "State")+ let blocker = try directory.write("blocked", to: HistoryDatabase.filename)let phase = Mutex(0)let server = try Self.tokenServer(now: Date().addingTimeInterval(-60), phase: { phase.withLock { $0 } })defer { server.close() }- let store = TokenHistoryStore(directoryURL: directory.url)+ let store = TokenUsageHistoryStore(directoryURL: directory.url)var observed = 0var disconnected = falselet reader = AppServerActivityReader(- lifecycleCache: SessionLifecycleCache(), socketURL: server.url, logStorage: nil,- tokenHistory: store, recorder: ActivityRecorder(directoryURL: directory.url), maximumPendingEvents: 1+ probe: ActivityReaderProbe(), socketURL: server.url, logStorage: nil,+ tokenUsageHistory: store, activityDataRecorder: ActivityDataRecorder(directoryURL: directory.url), maximumPendingEvents: 1) { batch in- if case .sourceUnavailable = batch {+ if case .sourceUnavailable = batch.phase {disconnected = true}- if case let .live(events) = batch {- observed += events.count+ if batch.phase == .live {+ observed += 1}}await reader.start()@@ -394,23 +437,23 @@ extension ActivityIsolationTests {}#expect(observed == 1)phase.withLock { $0 = 1 }- #expect(await reader.drainNow() == .completed)+ #expect(await reader.drainNow().status == .completed)#expect(observed == 2)try FileManager.default.removeItem(at: blocker)- for _ in 0 ..< 200 where !FileManager.default.fileExists(atPath: HistoryStorage.maintenanceURL(in: directory.url).path) {+ for _ in 0 ..< 200 where !FileManager.default.fileExists(atPath: directory.url.appendingPathComponent(HistoryDatabase.filename).path) {try await Task.sleep(for: .milliseconds(10))}- #expect(await reader.drainNow() == .completed)+ #expect(await reader.drainNow().status == .completed)phase.withLock { $0 = 2 }- #expect(await reader.drainNow() == .completed)+ #expect(await reader.drainNow().status == .completed)#expect(observed == 3)#expect(!disconnected)#expect(await reader.stop())let observations = try Self.observations(in: directory.url)- #expect(observations.map(\.sequence) == [1, 1])- #expect(Set(observations.map(\.streamID)).count == 2)+ #expect(observations.map { $0.delta?.totalTokens } == [0, 80])+ #expect(Set(observations.map(\.id)).count == 2)let restored = try await store.refresh()- #expect(restored.first?.usage?.totalTokens == 70)+ #expect(restored.first?.tokenUsage.totalTokens == 80)#expect(try await store.refresh() == restored)try server.finish()}diff --git a/CodexBarTests/Activity/ActivityLivePresentationTests.swift b/CodexBarTests/Activity/ActivityLivePresentationTests.swiftindex 2a3da91..cb42be2 100644--- a/CodexBarTests/Activity/ActivityLivePresentationTests.swift+++ b/CodexBarTests/Activity/ActivityLivePresentationTests.swift@@ -60,7 +60,7 @@ struct ActivityLivePresentationTests {@Test(arguments: [("commandExecution", "command")])func outputAfterReconnectRestoresToolPhaseWithoutInventingInventory(type: String, key: String) throws {var reducer = try prepared()- reducer.reconnect(now: now)+ reducer.reconnect()let thread = ActivityThread(id: "thread-a", status: ActivityThreadStatus(type: .active, activeFlags: []))_ = reducer.reconcile(thread: thread, turns: [ActivityTurn(id: "turn-a", status: .running)], now: now)let output = try message("item/\(type)/outputDelta", #""itemId":"tool","delta":"output""#)@@ -107,9 +107,9 @@ struct ActivityLivePresentationTests {}@Test(arguments: [- (["read"], "command-read"), (["listFiles"], "command-listFiles"), (["search"], "command-search"),- (["search", "read", "read"], "command-read-search"), (["search", "listFiles"], "command-listFiles-search"),- (["listFiles", "read"], "command-read-listFiles"), (["search", "listFiles", "read"], "command-read-listFiles-search"),+ (["read"], "command-read"), (["listFiles"], "command-listfiles"), (["search"], "command-search"),+ (["search", "read", "read"], "command-read-search"), (["search", "listFiles"], "command-listfiles-search"),+ (["listFiles", "read"], "command-read-listfiles"), (["search", "listFiles", "read"], "command-read-listfiles-search"),(["read", "unknown"], "command"), (["future"], "command"), ([], "command")])func commandActionsUseServerClassification(actions: [String], key: String) {@@ -124,7 +124,7 @@ struct ActivityLivePresentationTests {(["search", "read"], "actions-read-search"),(["read", "search"], "actions-read-search"),(["search", "unknown", "read", "read"], "actions-read-search"),- (["search", "listFiles", "read", "listFiles"], "actions-read-listFiles-search"),+ (["search", "listFiles", "read", "listFiles"], "actions-read-listfiles-search"),([], nil), (["unknown", "future"], nil)] as [([String], String?)])func commandCompletionLocalizesAndOrdersKnownActions(actions: [String], detailKey: String?) {@@ -287,7 +287,7 @@ struct ActivityLivePresentationTests {@Test func reconnectDropsUnverifiablePhasesAndWaitingDuration() throws {var reducer = try prepared()_ = try reducer.consume(message("thread/status/changed", #""status":{"type":"active","activeFlags":["waitingOnUserInput"]}"#, turn: false), now: now)- reducer.reconnect(now: now.addingTimeInterval(10))+ reducer.reconnect()#expect(reducer.states.values.first?.presentation == nil)let thread = ActivityThread(id: "thread-a", status: ActivityThreadStatus(type: .active, activeFlags: [.waitingOnUserInput]))_ = reducer.reconcile(thread: thread, turns: [ActivityTurn(id: "turn-a", status: .running)], now: now.addingTimeInterval(12))@@ -329,7 +329,7 @@ struct ActivityLivePresentationTests {#expect(try summary(reducer).planCompleted == 1)#expect(try summary(reducer).planTotal == 1)#expect(try summary(reducer).waiting == nil)- #expect(reducer.tokenTurns.isEmpty)+ #expect(reducer.tokenUsageTurns.isEmpty)}@Test(arguments: ["completed", "failed", "declined", "unknown"])@@ -351,7 +351,7 @@ struct ActivityLivePresentationTests {var reducer = try prepared()_ = try reducer.consume(message("thread/status/changed", #""status":{"type":"active","activeFlags":["waitingOnUserInput"]}"#, turn: false), now: now)let state = try #require(reducer.states.values.first)- task.mergeExecutionLifecycle(state, owner: ActivityExecutionKey(agentID: nil, turnID: "turn-a"))+ task.mergeExecutionLifecycle(state, owner: ActivityExecutionKey(agentThreadID: nil, turnID: "turn-a"))let snapshot = ActivitySnapshot(waitingTasks: [], runningTasks: [task.snapshot], recentCompletions: [], recentTerminations: [])#expect(task.state == .running)#expect(snapshot.waitingCount == 0)@@ -400,8 +400,8 @@ struct ActivityLivePresentationTests {try root.consume(message("item/agentMessage/delta", #""itemId":"answer""#), at: now.addingTimeInterval(4))var child = ActivityLivePresentation()try child.consume(message("thread/status/changed", #""status":{"type":"active","activeFlags":["waitingOnUserInput"]}"#, turn: false), at: now)- task.executions[ActivityExecutionKey(agentID: nil, turnID: "turn-a")] = ActivityExecution(presentation: root)- let childOwner = ActivityExecutionKey(agentID: "child", turnID: "child-turn")+ task.executions[ActivityExecutionKey(agentThreadID: nil, turnID: "turn-a")] = ActivityExecution(presentation: root)+ let childOwner = ActivityExecutionKey(agentThreadID: "child", turnID: "child-turn")task.executions[childOwner] = ActivityExecution(presentation: child)#expect(task.snapshot.presentation?.waiting?.since == now)#expect(task.snapshot.presentation?.current.key == "waiting-input")@@ -428,7 +428,7 @@ struct ActivityLivePresentationTests {startedAt: now,progressGeneration: 0)- _ = ActivityMonitor.mergeLifecycleBackfill(from: state, into: &task)+ ActivityTaskGraph.mergeLifecycleBackfill(from: state, into: &task)#expect(task.terminalFailed)}diff --git a/CodexBarTests/Activity/ActivityReaderBoundsTests.swift b/CodexBarTests/Activity/ActivityReaderBoundsTests.swiftindex 06ae4bf..e848a4a 100644--- a/CodexBarTests/Activity/ActivityReaderBoundsTests.swift+++ b/CodexBarTests/Activity/ActivityReaderBoundsTests.swift@@ -21,14 +21,14 @@ struct ActivityReaderBoundsTests {var failed = falsevar ready = falselet reader = AppServerActivityReader(- lifecycleCache: SessionLifecycleCache(), socketURL: server.url, logStorage: nil,- tokenHistory: TokenHistoryStore(directoryURL: directory.url),- recorder: ActivityRecorder(directoryURL: directory.url), maximumLoadedPages: 3+ probe: ActivityReaderProbe(), socketURL: server.url, logStorage: nil,+ tokenUsageHistory: TokenUsageHistoryStore(directoryURL: directory.url),+ activityDataRecorder: ActivityDataRecorder(directoryURL: directory.url), maximumLoadedPages: 3) { batch in- if case .sourceUnavailable = batch {+ if case .sourceUnavailable = batch.phase {failed = true}- if case .bootstrapEnd = batch {+ if case .bootstrapEnd = batch.phase {ready = true}}@@ -68,11 +68,11 @@ struct ActivityReaderBoundsTests {defer { server.close() }var ready = falselet reader = AppServerActivityReader(- lifecycleCache: SessionLifecycleCache(), socketURL: server.url, logStorage: nil,- tokenHistory: TokenHistoryStore(directoryURL: directory.url),- recorder: ActivityRecorder(directoryURL: directory.url), reconciliationTimeout: 0.5+ probe: ActivityReaderProbe(), socketURL: server.url, logStorage: nil,+ tokenUsageHistory: TokenUsageHistoryStore(directoryURL: directory.url),+ activityDataRecorder: ActivityDataRecorder(directoryURL: directory.url), reconciliationTimeout: 0.5) { batch in- if case .bootstrapEnd = batch {+ if case .bootstrapEnd = batch.phase {ready = true}}@@ -82,7 +82,7 @@ struct ActivityReaderBoundsTests {}#expect(ready)let start = Date()- #expect(await reader.drainNow() == .sourceUnavailable)+ #expect(await reader.drainNow().status == .sourceUnavailable)#expect(Date().timeIntervalSince(start) < 2)await reader.stop()release.signal()@@ -93,7 +93,7 @@ struct ActivityReaderBoundsTests {let directory = try TestDirectory()defer { try? directory.remove() }let now = Date().addingTimeInterval(-60)- let server = try SharedServerFixture { peer in+ let server = try SharedServerFixture(connections: 3, concurrentConnections: true) { peer intry Self.initialize(peer)while let request = try? peer.readMessage() {let params = request["params"] as? [String: Any] ?? [:]@@ -116,34 +116,37 @@ struct ActivityReaderBoundsTests {"id": "child-turn", "rootTurnId": "root-turn", "status": "inProgress", "startedAt": now.timeIntervalSince1970 + 1]]])} else if params["cursor"] == nil {- #expect(params["itemsView"] as? String == "full")+ #expect(params["itemsView"] as? String == "notLoaded")try peer.reply(to: request, result: ["data": [], "nextCursor": "older"])} else {#expect(params["cursor"] as? String == "older")- #expect(params["itemsView"] as? String == "full")- let item: [String: Any] = [- "id": "spawn", "type": "subAgentActivity", "kind": "started",- "agentThreadId": "child", "model": "child-model", "reasoningEffort": "low"- ]+ #expect(params["itemsView"] as? String == "notLoaded")let turn: [String: Any] = ["id": "root-turn", "rootTurnId": "root-turn", "status": "completed",- "startedAt": now.timeIntervalSince1970, "completedAt": now.timeIntervalSince1970 + 2, "items": [item]+ "startedAt": now.timeIntervalSince1970, "completedAt": now.timeIntervalSince1970 + 2,+ "items": []]try peer.reply(to: request, result: ["data": [turn]])}+ case "thread/items/list":+ let data: [[String: Any]] = id == "parent" ? [["turnId": "root-turn", "item": [+ "id": "spawn", "type": "subAgentActivity", "kind": "started",+ "agentThreadId": "child", "model": "child-model", "reasoningEffort": "low"+ ]]] : []+ try peer.reply(to: request, result: ["data": data])default:Issue.record("Unexpected request")}}}defer { server.close() }- let lifecycle = SessionLifecycleCache()+ let lifecycle = ActivityReaderProbe()var ready = falselet reader = AppServerActivityReader(- lifecycleCache: lifecycle, socketURL: server.url, logStorage: nil,- tokenHistory: TokenHistoryStore(directoryURL: directory.url), recorder: ActivityRecorder(directoryURL: directory.url)+ probe: lifecycle, socketURL: server.url, logStorage: nil,+ tokenUsageHistory: TokenUsageHistoryStore(directoryURL: directory.url), activityDataRecorder: ActivityDataRecorder(directoryURL: directory.url)) { batch in- if case .bootstrapEnd = batch {+ if case .bootstrapEnd = batch.phase {ready = true}}@@ -152,8 +155,15 @@ struct ActivityReaderBoundsTests {try await Task.sleep(for: .milliseconds(5))}#expect(ready)- #expect(await reader.drainNow() == .completed)- let states = await lifecycle.lifecycleStates(for: [.init(threadID: "child", turnID: "child-turn")])+ #expect(await reader.drainNow().status == .completed)+ for _ in 0 ..< 200 {+ let current = await reader.lifecycleStatesForTesting(for: [.init(threadID: "child", turnID: "child-turn")])+ if current.first?.effort == "low" {+ break+ }+ try await Task.sleep(for: .milliseconds(10))+ }+ let states = await reader.lifecycleStatesForTesting(for: [.init(threadID: "child", turnID: "child-turn")])let child = try #require(states.first)#expect(child.rootThreadID == "parent")#expect(child.rootTurnID == "root-turn")@@ -208,13 +218,13 @@ extension ActivityReaderBoundsTests {}}defer { server.close() }- let lifecycle = SessionLifecycleCache()+ let lifecycle = ActivityReaderProbe()var ready = falselet reader = AppServerActivityReader(- lifecycleCache: lifecycle, socketURL: server.url, logStorage: nil,- tokenHistory: TokenHistoryStore(directoryURL: directory.url), recorder: ActivityRecorder(directoryURL: directory.url)+ probe: lifecycle, socketURL: server.url, logStorage: nil,+ tokenUsageHistory: TokenUsageHistoryStore(directoryURL: directory.url), activityDataRecorder: ActivityDataRecorder(directoryURL: directory.url)) { batch in- if case .bootstrapEnd = batch {+ if case .bootstrapEnd = batch.phase {ready = true}}@@ -224,9 +234,10 @@ extension ActivityReaderBoundsTests {}#expect(ready)let result = await reader.drainNow()- let states = await lifecycle.lifecycleStates(for: [.init(threadID: "healthy", turnID: "healthy-turn")])- #expect(states.first?.readStatus == .complete)- #expect(result == (running ? .sourceUnavailable : .completed))+ let key = ActivityTaskKey(thread: "healthy", turn: "healthy-turn")+ #expect(lifecycle.graph.tasks[key]?.lifecycleCoverageCheckedAt != nil)+ #expect(!lifecycle.graph.unavailableTurns.contains(key))+ #expect(result.status == (running ? .partial : .completed))await reader.stop()try server.finish()}diff --git a/CodexBarTests/Activity/ActivityRecoveryTests.swift b/CodexBarTests/Activity/ActivityRecoveryTests.swiftindex 2a25474..817152c 100644--- a/CodexBarTests/Activity/ActivityRecoveryTests.swift+++ b/CodexBarTests/Activity/ActivityRecoveryTests.swift@@ -6,18 +6,20 @@ struct ActivityRecoveryTests {let directory = try TestDirectory()defer { try? directory.remove() }let now = Date()- let store = TokenHistoryStore(directoryURL: directory.url)- let id = TokenTurn.identifier(thread: "t", turn: "u")- let record = TestFixtures.tokenTurn(id: id, rootID: id, startedAt: now, updatedAt: now, usage: .zero)- try await directory.seedTokenSnapshots([record], now: now)- let cache = try directory.write("broken", to: "Aggregates/tokens.json")+ let store = TokenUsageHistoryStore(directoryURL: directory.url)+ try await store.appendObservation(TestFixtures.tokenUsageObservation(at: now, tokenUsage: .zero), now: now)+ _ = try await store.refresh(now: now)+ let database = try HistoryDatabase(directoryURL: directory.url)+ try database.execute("UPDATE aggregates SET tokenUsage = ?", [.data(Data("broken".utf8))])var refreshFailed = falsevar rebuildFailed = falsedo { _ = try await store.refresh(now: now) } catch { refreshFailed = true }- try Data("broken".utf8).write(to: cache)- do { _ = try await store.rebuild(for: [CodexDateFormat.dayString(from: now)], now: now) } catch { rebuildFailed = true }+ try database.execute("UPDATE aggregates SET tokenUsage = ?", [.data(Data("broken".utf8))])+ do { _ = try await store.rebuild(for: [CodexDateFormat.dayString(from: now)]) } catch { rebuildFailed = true }#expect(!refreshFailed && !rebuildFailed)- try FileManager.default.removeItem(at: cache)+ try database.execute("""+ UPDATE aggregates SET tokenUsage = NULL+ """)let recovered = try await store.refresh(now: now)#expect(recovered.count == 1)}@@ -52,14 +54,14 @@ struct ActivityRecoveryTests {var failed = falsevar live = 0let reader = AppServerActivityReader(- lifecycleCache: SessionLifecycleCache(),+ probe: ActivityReaderProbe(),socketURL: server.url,- tokenHistory: TokenHistoryStore(directoryURL: directory.url),- recorder: ActivityRecorder(directoryURL: directory.url)+ tokenUsageHistory: TokenUsageHistoryStore(directoryURL: directory.url),+ activityDataRecorder: ActivityDataRecorder(directoryURL: directory.url)) { batch in- switch batch {+ switch batch.phase {case .sourceUnavailable: failed = true- case let .live(events): live += events.count+ case .live: live += batch.graph.tasks.countdefault: break}}@@ -73,17 +75,7 @@ struct ActivityRecoveryTests {await reader.stop()release.signal()try server.finish()- var recorded = 0- let events = directory.url.appendingPathComponent("Events")- if FileManager.default.fileExists(atPath: events.path) {- for file in try FileManager.default.contentsOfDirectory(at: events, includingPropertiesForKeys: nil) {- try AppServerEventJournal.read(at: file) {- if $0.activity != nil {- recorded += 1- }- }- }- }+ let recorded = try directory.historyRecords().filter { $0.activityData != nil }.count#expect(recorded == 1)#expect(live == 1)#expect(!failed)diff --git a/CodexBarTests/Activity/ActivitySchedulingTests.swift b/CodexBarTests/Activity/ActivitySchedulingTests.swiftindex 76575a0..e83c990 100644--- a/CodexBarTests/Activity/ActivitySchedulingTests.swift+++ b/CodexBarTests/Activity/ActivitySchedulingTests.swift@@ -27,17 +27,17 @@ struct ActivitySchedulingTests {_ = release.wait(timeout: .now() + 5)}defer { server.close() }- let lifecycle = SessionLifecycleCache()+ let lifecycle = ActivityReaderProbe()var ready = falselet reader = AppServerActivityReader(- lifecycleCache: lifecycle, socketURL: server.url,- tokenHistory: TokenHistoryStore(directoryURL: directory.url),- recorder: ActivityRecorder(directoryURL: directory.url)+ probe: lifecycle, socketURL: server.url,+ tokenUsageHistory: TokenUsageHistoryStore(directoryURL: directory.url),+ activityDataRecorder: ActivityDataRecorder(directoryURL: directory.url)) { batch in- if case .bootstrapEnd = batch {+ if case .bootstrapEnd = batch.phase {ready = true}- if case let .live(events) = batch, events.contains(where: { $0.eventKind == .toolStarted }) {+ if batch.phase == .live, batch.graph.tasks.values.contains(where: { $0.lastProgressAt > Date(timeIntervalSince1970: timestamp) }) {observed.signal()}}@@ -46,9 +46,8 @@ struct ActivitySchedulingTests {try await Task.sleep(for: .milliseconds(5))}#expect(ready)- #expect(await reader.drainNow() == .sourceUnavailable)- let reference = ActivityTurnReference(threadID: "thread", turnID: "turn")- #expect(await lifecycle.lifecycleStates(for: [reference]).first?.isWaitingApproval == true)+ #expect(await reader.drainNow().status == .partial)+ #expect(lifecycle.graph.tasks[.init(thread: "thread", turn: "turn")]?.state == .waitingApproval)await reader.stop()release.signal()try server.finish()@@ -62,21 +61,21 @@ struct ActivitySchedulingTests {let timestamp = Date().addingTimeInterval(-60).timeIntervalSince1970let server = try SharedServerFixture { peer intry Self.bootstrap(peer, timestamp: timestamp)- for total in [100, 150] {- let usage = ["inputTokens": total, "cachedInputTokens": 0, "outputTokens": 0, "reasoningOutputTokens": 0, "totalTokens": total]- try peer.send(["method": "thread/tokenUsage/updated", "params": ["threadId": "thread", "turnId": "turn", "tokenUsage": ["total": usage, "last": usage]]])+ for total in [100, 120, 150] {+ let tokenUsage = ["inputTokens": total, "cachedInputTokens": 0, "outputTokens": 0, "reasoningOutputTokens": 0, "totalTokens": total]+ try peer.send(["method": "thread/tokenUsage/updated", "params": ["threadId": "thread", "turnId": "turn", "tokenUsage": ["total": tokenUsage, "last": tokenUsage]]])}try peer.send(["method": "thread/status/changed", "params": ["threadId": "thread", "status": 123]])_ = release.wait(timeout: .now() + 5)}defer { server.close() }var failed = false- let store = TokenHistoryStore(directoryURL: directory.url)+ let store = TokenUsageHistoryStore(directoryURL: directory.url)let reader = AppServerActivityReader(- lifecycleCache: SessionLifecycleCache(), socketURL: server.url,- tokenHistory: store, recorder: ActivityRecorder(directoryURL: directory.url)+ probe: ActivityReaderProbe(), socketURL: server.url,+ tokenUsageHistory: store, activityDataRecorder: ActivityDataRecorder(directoryURL: directory.url)) {- if case .sourceUnavailable = $0 {+ if case .sourceUnavailable = $0.phase {failed = true}}@@ -86,7 +85,7 @@ struct ActivitySchedulingTests {}#expect(!failed)await reader.stop()- #expect(try await store.refresh().first?.usage?.totalTokens == 50)+ #expect(try await store.refresh().first?.tokenUsage.totalTokens == 50)release.signal()try server.finish()}@@ -107,9 +106,9 @@ struct ActivitySchedulingTests {}defer { server.close() }let reader = AppServerActivityReader(- lifecycleCache: SessionLifecycleCache(), socketURL: server.url,- tokenHistory: TokenHistoryStore(directoryURL: directory.url),- recorder: ActivityRecorder(directoryURL: directory.url), onBatch: { _ in }+ probe: ActivityReaderProbe(), socketURL: server.url,+ tokenUsageHistory: TokenUsageHistoryStore(directoryURL: directory.url),+ activityDataRecorder: ActivityDataRecorder(directoryURL: directory.url), onBatch: { _ in })await reader.start()try await Task.sleep(for: .milliseconds(4400))@@ -131,23 +130,23 @@ struct ActivitySchedulingTests {}}defer { server.close() }- let lifecycle = SessionLifecycleCache()+ let lifecycle = ActivityReaderProbe()var ready = falsevar disconnected = falsevar tools = 0let reader = AppServerActivityReader(- lifecycleCache: lifecycle, socketURL: server.url,- tokenHistory: TokenHistoryStore(directoryURL: directory.url),- recorder: ActivityRecorder(directoryURL: directory.url)+ probe: lifecycle, socketURL: server.url,+ tokenUsageHistory: TokenUsageHistoryStore(directoryURL: directory.url),+ activityDataRecorder: ActivityDataRecorder(directoryURL: directory.url)) { batch in- if case .bootstrapEnd = batch {+ if case .bootstrapEnd = batch.phase {ready = true}- if case .sourceUnavailable = batch {+ if case .sourceUnavailable = batch.phase {disconnected = true}- if case let .live(events) = batch {- tools += events.filter { $0.eventKind == .toolStarted }.count+ if batch.phase == .live {+ tools += batch.graph.tasks.values.filter { $0.snapshot.presentation?.current.key == "command" }.count}}await reader.start()@@ -159,9 +158,10 @@ struct ActivitySchedulingTests {#expect(tools == 1)#expect(discoveries.withLock { $0 } == 2)if ready, !disconnected {- #expect(await reader.drainNow() == .completed)- let reference = ActivityTurnReference(threadID: "a-missing", turnID: "turn")- #expect(await lifecycle.lifecycleStates(for: [reference]).first?.readStatus == .complete)+ #expect(await reader.drainNow().status == .completed)+ let key = ActivityTaskKey(thread: "a-missing", turn: "turn")+ #expect(lifecycle.graph.tasks[key]?.lifecycleCoverageCheckedAt != nil)+ #expect(!lifecycle.graph.unavailableTurns.contains(key))#expect(!disconnected)}await reader.stop()@@ -172,7 +172,7 @@ struct ActivitySchedulingTests {@Test func persistedEventIsPublishedOnceAfterMaintenanceWriteRecovers() async throws {let directory = try TestDirectory()defer { try? directory.remove() }- let blocker = try directory.write("block maintenance directory", to: "State")+ let blocker = try directory.write("block maintenance directory", to: HistoryDatabase.filename)let release = DispatchSemaphore(value: 0)defer { release.signal() }let server = try SharedServerFixture { peer in@@ -184,15 +184,15 @@ struct ActivitySchedulingTests {var failures = 0var published = 0let reader = AppServerActivityReader(- lifecycleCache: SessionLifecycleCache(), socketURL: server.url,- tokenHistory: TokenHistoryStore(directoryURL: directory.url),- recorder: ActivityRecorder(directoryURL: directory.url)+ probe: ActivityReaderProbe(), socketURL: server.url,+ tokenUsageHistory: TokenUsageHistoryStore(directoryURL: directory.url),+ activityDataRecorder: ActivityDataRecorder(directoryURL: directory.url)) { batch in- if case .sourceUnavailable = batch {+ if case .sourceUnavailable = batch.phase {failures += 1}- if case let .live(events) = batch {- published += events.count+ if batch.phase == .live {+ published += 1}}await reader.start()@@ -202,24 +202,16 @@ struct ActivitySchedulingTests {#expect(published == 1)#expect(failures == 0)try FileManager.default.removeItem(at: blocker)- for _ in 0 ..< 150 where try HistoryStorage.loadMaintenanceState(in: directory.url).days.isEmpty {+ for _ in 0 ..< 150 where try HistoryDatabase(directoryURL: directory.url).dates().isEmpty {try await Task.sleep(for: .milliseconds(10))}- #expect(try !HistoryStorage.loadMaintenanceState(in: directory.url).days.isEmpty)+ #expect(try !HistoryDatabase(directoryURL: directory.url).dates().isEmpty)#expect(failures == 0)#expect(published == 1)await reader.stop()release.signal()try server.finish()- var recorded = 0- let files = try FileManager.default.contentsOfDirectory(at: HistoryStorage.eventsDirectoryURL(in: directory.url), includingPropertiesForKeys: nil)- for file in files {- try AppServerEventJournal.read(at: file) {- if $0.activity != nil {- recorded += 1- }- }- }+ let recorded = try directory.historyRecords().filter { $0.activityData != nil }.count#expect(recorded == 1)}@@ -245,7 +237,7 @@ struct ActivitySchedulingTests {try peer.send(["id": #require(resume["id"]), "error": ["code": -32600, "message": "no rollout found for thread id a-missing"]])- // 单线程失败后继续初始化其他线程, 不重新 initialize+ // 单会话失败后继续初始化其他会话, 不重新 initializefor method in ["thread/read", "thread/resume"] {let request = try peer.readMessage()#expect(request["method"] as? String == method)@@ -256,14 +248,14 @@ struct ActivitySchedulingTests {try peer.send(["method": "item/started", "params": ["threadId": "thread", "turnId": "turn", "item": ["id": "tool", "type": "commandExecution"]]])- // 普通发现不立即重试不可用线程, 健康线程仍可接收推送+ // 普通发现不立即重试不可用会话, 健康会话仍可接收推送for _ in 0 ..< 2 {let request = try peer.readMessage()#expect(request["method"] as? String == "thread/loaded/list")try peer.reply(to: request, result: ["data": ["a-missing", "thread"]])onDiscovery()}- // 强制核对重新尝试失败线程, 服务端恢复后无需重连即可加入+ // 强制核对重新尝试失败会话, 服务端恢复后无需重连即可加入let discovery = try peer.readMessage()#expect(discovery["method"] as? String == "thread/loaded/list")try peer.reply(to: discovery, result: ["data": ["a-missing", "thread"]])@@ -279,7 +271,7 @@ struct ActivitySchedulingTests {}let request = try peer.readMessage()#expect(request["method"] as? String == "thread/turns/list")- #expect((request["params"] as? [String: Any])?["itemsView"] as? String == (id == "a-missing" ? "full" : "notLoaded"))+ #expect((request["params"] as? [String: Any])?["itemsView"] as? String == "notLoaded")try peer.reply(to: request, result: ["data": [["id": "turn", "rootTurnId": "turn", "status": "inProgress", "startedAt": timestamp]]])}_ = release.wait(timeout: .now() + 5)@@ -301,13 +293,160 @@ struct ActivitySchedulingTests {}let turns = try peer.readMessage()#expect(turns["method"] as? String == "thread/turns/list")- #expect((turns["params"] as? [String: Any])?["itemsView"] as? String == "full")+ #expect((turns["params"] as? [String: Any])?["itemsView"] as? String == "notLoaded")try peer.reply(to: turns, result: ["data": [["id": "turn", "rootTurnId": "turn", "status": idle ? "completed" : "inProgress", "startedAt": timestamp]]])}}extension ActivitySchedulingTests {- @Test func turnPaginationResumesAndDoesNotVerifyOmittedTurn() async throws {+ @Test func snapshotCannotDismissNewMCPInputRequest() async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let release = DispatchSemaphore(value: 0)+ defer { release.signal() }+ let timestamp = Date().addingTimeInterval(-60).timeIntervalSince1970+ let server = try SharedServerFixture { peer in+ try Self.bootstrap(peer, timestamp: timestamp)+ let loaded = try peer.readMessage()+ try peer.reply(to: loaded, result: ["data": ["thread"]])+ let read = try peer.readMessage()+ try peer.send(["id": "input", "method": "mcpServer/elicitation/request", "params": [+ "threadId": "thread", "turnId": "turn", "mode": "form"+ ]])+ try peer.reply(to: read, result: ["thread": Self.thread(timestamp, status: "idle")])+ let turns = try peer.readMessage()+ try peer.reply(to: turns, result: ["data": [["id": "turn", "rootTurnId": "turn", "status": "inProgress", "startedAt": timestamp]]])+ _ = release.wait(timeout: .now() + 5)+ }+ defer { server.close() }+ var ready = false+ let reader = AppServerActivityReader(+ socketURL: server.url, logStorage: nil, tokenUsageHistory: TokenUsageHistoryStore(directoryURL: directory.url),+ activityDataRecorder: ActivityDataRecorder(directoryURL: directory.url)+ ) {+ if $0.phase == .bootstrapEnd {+ ready = true+ }+ }+ await reader.start()+ for _ in 0 ..< 200 where !ready {+ try await Task.sleep(for: .milliseconds(5))+ }+ #expect(await reader.drainNow().status == .partial)+ let state = await reader.lifecycleStatesForTesting(for: [.init(threadID: "thread", turnID: "turn")]).first+ #expect(state?.presentation?.requests["input"] != nil)+ await reader.stop()+ release.signal()+ try server.finish()+ }++ @Test func staleTurnResponseCannotResurrectCompletedTurnOrReplaceNewTurn() async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let release = DispatchSemaphore(value: 0)+ defer { release.signal() }+ let timestamp = Date().addingTimeInterval(-60).timeIntervalSince1970+ let server = try SharedServerFixture { peer in+ try Self.bootstrap(peer, timestamp: timestamp)+ let loaded = try peer.readMessage()+ try peer.reply(to: loaded, result: ["data": ["thread"]])+ let read = try peer.readMessage()+ try peer.reply(to: read, result: ["thread": Self.thread(timestamp)])+ let turns = try peer.readMessage()+ try peer.send(["method": "turn/completed", "params": ["threadId": "thread", "turn": [+ "id": "turn", "rootTurnId": "turn", "status": "completed", "completedAt": timestamp + 20+ ]]])+ try peer.send(["method": "turn/started", "params": ["threadId": "thread", "turn": [+ "id": "new-turn", "rootTurnId": "new-turn", "status": "inProgress", "startedAt": timestamp + 30+ ]]])+ try peer.reply(to: turns, result: ["data": [[+ "id": "turn", "rootTurnId": "turn", "status": "inProgress", "startedAt": timestamp+ ]]])+ _ = release.wait(timeout: .now() + 5)+ }+ defer { server.close() }+ let probe = ActivityReaderProbe()+ var ready = false+ let reader = AppServerActivityReader(+ probe: probe, socketURL: server.url, tokenUsageHistory: TokenUsageHistoryStore(directoryURL: directory.url),+ activityDataRecorder: ActivityDataRecorder(directoryURL: directory.url)+ ) {+ if $0.phase == .bootstrapEnd {+ ready = true+ }+ }+ await reader.start()+ for _ in 0 ..< 200 where !ready {+ try await Task.sleep(for: .milliseconds(5))+ }+ #expect(ready)+ #expect(await reader.drainNow().status == .partial)+ #expect(probe.graph.tasks[.init(thread: "thread", turn: "turn")] == nil)+ #expect(probe.graph.tasks[.init(thread: "thread", turn: "new-turn")]?.state == .running)+ #expect(probe.graph.completions.count == 1)+ let old = await reader.lifecycleStatesForTesting(for: [.init(threadID: "thread", turnID: "turn")]).first+ #expect(old?.terminal != nil)+ #expect(old?.turnStatus == .completed)+ await reader.stop()+ release.signal()+ try server.finish()+ }++ @Test func tokenAndToolUpdatesDoNotPreventSuccessfulDrain() async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let timestamp = Date().addingTimeInterval(-60).timeIntervalSince1970+ let server = try SharedServerFixture { peer in+ try Self.bootstrap(peer, timestamp: timestamp)+ var total = 100+ while let request = try? peer.readMessage() {+ let usage = ["inputTokens": total, "cachedInputTokens": 0, "outputTokens": 0, "reasoningOutputTokens": 0, "totalTokens": total]+ try peer.send(["method": "thread/tokenUsage/updated", "params": [+ "threadId": "thread", "turnId": "turn", "tokenUsage": ["total": usage, "last": usage]+ ]])+ try peer.send(["method": "item/started", "params": [+ "threadId": "thread", "turnId": "turn", "item": ["id": "tool-\(total)", "type": "commandExecution"]+ ]])+ total += 10+ switch request["method"] as? String {+ case "thread/loaded/list": try peer.reply(to: request, result: ["data": ["thread"]])+ case "thread/read": try peer.reply(to: request, result: ["thread": Self.thread(timestamp)])+ case "thread/turns/list": try peer.reply(to: request, result: ["data": [[+ "id": "turn", "rootTurnId": "turn", "status": "inProgress", "startedAt": timestamp+ ]]])+ default: Issue.record("Unexpected request")+ }+ }+ }+ defer { server.close() }+ let probe = ActivityReaderProbe()+ var ready = false+ let reader = AppServerActivityReader(+ probe: probe, socketURL: server.url, tokenUsageHistory: TokenUsageHistoryStore(directoryURL: directory.url),+ activityDataRecorder: ActivityDataRecorder(directoryURL: directory.url)+ ) {+ if $0.phase == .bootstrapEnd {+ ready = true+ }+ }+ await reader.start()+ for _ in 0 ..< 200 where !ready {+ try await Task.sleep(for: .milliseconds(5))+ }+ for _ in 0 ..< 3 {+ #expect(await reader.drainNow().status == .completed)+ }+ for _ in 0 ..< 100 where probe.graph.tasks[.init(thread: "thread", turn: "turn")]?.tokenUsage?.totalTokens != 80 {+ try await Task.sleep(for: .milliseconds(5))+ }+ #expect(probe.graph.tasks[.init(thread: "thread", turn: "turn")]?.tokenUsage?.totalTokens == 80)+ #expect(probe.graph.unavailableTurns.isEmpty)+ await reader.stop()+ try server.finish()+ }++ @Test(arguments: [false, true])+ func turnPaginationResumesAndDoesNotVerifyOmittedTurn(withUpdates: Bool) async throws {let directory = try TestDirectory()defer { try? directory.remove() }let release = DispatchSemaphore(value: 0)@@ -328,6 +467,15 @@ extension ActivitySchedulingTests {for page in 1 ... 8 {let request = try peer.readMessage()#expect((request["params"] as? [String: Any])?["cursor"] as? String == "page-\(page)")+ if withUpdates {+ let usage = ["inputTokens": page * 10, "cachedInputTokens": 0, "outputTokens": 0, "reasoningOutputTokens": 0, "totalTokens": page * 10]+ try peer.send(["method": "thread/tokenUsage/updated", "params": [+ "threadId": "thread", "turnId": "turn", "tokenUsage": ["total": usage, "last": usage]+ ]])+ try peer.send(["method": "item/started", "params": [+ "threadId": "thread", "turnId": "turn", "item": ["id": "live-\(page)", "type": "commandExecution"]+ ]])+ }try peer.reply(to: request, result: ["data": [["id": "history-\(page)", "status": "completed"]], "nextCursor": "page-\(page + 1)"])}} else {@@ -339,16 +487,16 @@ extension ActivitySchedulingTests {_ = release.wait(timeout: .now() + 5)}defer { server.close() }- let cache = SessionLifecycleCache()+ let cache = ActivityReaderProbe()var ready = falselet reader = AppServerActivityReader(- lifecycleCache: cache,+ probe: cache,socketURL: server.url,logStorage: nil,- tokenHistory: TokenHistoryStore(directoryURL: directory.url),- recorder: ActivityRecorder(directoryURL: directory.url)+ tokenUsageHistory: TokenUsageHistoryStore(directoryURL: directory.url),+ activityDataRecorder: ActivityDataRecorder(directoryURL: directory.url)) {- if case .bootstrapEnd = $0 {+ if case .bootstrapEnd = $0.phase {ready = true}}@@ -357,13 +505,13 @@ extension ActivitySchedulingTests {try await Task.sleep(for: .milliseconds(5))}try #require(ready)- let reference = ActivityTurnReference(threadID: "thread", turnID: "turn")- #expect(await reader.drainNow() == .sourceUnavailable)- #expect(await cache.lifecycleStates(for: [reference]).first?.readStatus == .unavailable)- #expect(await reader.drainNow() == .completed)- let state = try #require(await cache.lifecycleStates(for: [reference]).first)- #expect(state.readStatus == .complete)- #expect(state.terminal != nil)+ let key = ActivityTaskKey(thread: "thread", turn: "turn")+ #expect(await reader.drainNow().status == .partial)+ #expect(cache.graph.unavailableTurns.contains(key))+ #expect(await reader.drainNow().status == .completed)+ #expect(cache.graph.tasks[key] == nil)+ #expect(cache.graph.completions.count == 1)+ #expect(cache.graph.recentlyEndedTaskAt[key] != nil)#expect(await reader.stop())release.signal()try server.finish()@@ -373,7 +521,7 @@ extension ActivitySchedulingTests {func stopWaitsForPendingWritesAndReportsPersistenceFailure(failFirstStop: Bool) async throws {let directory = try TestDirectory()defer { try? directory.remove() }- let blocker = try directory.write("block maintenance directory", to: "State")+ let blocker = try directory.write("block maintenance directory", to: HistoryDatabase.filename)let release = DispatchSemaphore(value: 0)defer { release.signal() }let server = try SharedServerFixture { peer in@@ -384,13 +532,13 @@ extension ActivitySchedulingTests {defer { server.close() }var published = falselet reader = AppServerActivityReader(- lifecycleCache: SessionLifecycleCache(),+ probe: ActivityReaderProbe(),socketURL: server.url,logStorage: nil,- tokenHistory: TokenHistoryStore(directoryURL: directory.url),- recorder: ActivityRecorder(directoryURL: directory.url)+ tokenUsageHistory: TokenUsageHistoryStore(directoryURL: directory.url),+ activityDataRecorder: ActivityDataRecorder(directoryURL: directory.url)) {- if case let .live(events) = $0, !events.isEmpty {+ if $0.phase == .live, !$0.graph.tasks.isEmpty {published = true}}@@ -404,18 +552,9 @@ extension ActivitySchedulingTests {}try FileManager.default.removeItem(at: blocker)#expect(await reader.stop())- let dates = HistoryStorage.eventLogDateKeys(in: HistoryStorage.eventsDirectoryURL(in: directory.url))- var count = 0- for date in dates {- let url = HistoryStorage.eventLogURL(for: date, in: HistoryStorage.eventsDirectoryURL(in: directory.url))- try AppServerEventJournal.read(at: url) {- if $0.activity != nil {- count += 1- }- }- }+ let count = try directory.historyRecords().filter { $0.activityData != nil }.count#expect(count == 1)- #expect(try !HistoryStorage.loadMaintenanceState(in: directory.url).pending.isEmpty)+ #expect(try !HistoryDatabase(directoryURL: directory.url).pendingActivityDataDates().isEmpty)release.signal()try server.finish()}diff --git a/CodexBarTests/Activity/ActivityTaskTests.swift b/CodexBarTests/Activity/ActivityTaskTests.swiftindex 5fe1c80..43515c3 100644--- a/CodexBarTests/Activity/ActivityTaskTests.swift+++ b/CodexBarTests/Activity/ActivityTaskTests.swift@@ -2,33 +2,33 @@ import Foundationimport Testingstruct ActivityTaskTests {- @Test(arguments: [ActivityEventKind.turnCompleted, .turnAborted])- func terminalSnapshotPreservesTaskIdentityAcrossTurns(terminal: ActivityEventKind) throws {+ @Test(arguments: [ActivityDataEventKind.turnCompleted, .turnAborted])+ func terminalSnapshotPreservesTaskIdentityAcrossTurns(terminal: ActivityDataEventKind) throws {let directory = try TestDirectory()defer { try? directory.remove() }let preferences = try TestPreferences()defer { preferences.remove() }let monitor = ActivityMonitor(protectionSettings: ProtectionSettings(defaults: preferences.defaults),- protectionStore: ProtectionStore(directoryURL: directory.url), activityDirectoryURL: directory.url+ protectionStore: ProtectionStore(directoryURL: directory.url), activityDataDirectoryURL: directory.url)defer { monitor.stop() }monitor.isActivitySourceHealthy = truelet now = Date()monitor.consume(.live([TestFixtures.event(at: now)]))- let firstID = try #require(monitor.tasks.values.first?.snapshot.id)+ let firstID = try #require(monitor.graph.tasks.values.first?.snapshot.id)monitor.consume(.live([TestFixtures.event(terminal, at: now.addingTimeInterval(1))]))if terminal == .turnCompleted {- let completion = try #require(monitor.completions.first)+ let completion = try #require(monitor.graph.completions.first)#expect(completion.taskID == firstID)#expect(completion.id != firstID)} else {- let termination = try #require(monitor.terminations.first)+ let termination = try #require(monitor.graph.terminations.first)#expect(termination.taskID == firstID)#expect(termination.id != firstID)}monitor.consume(.live([TestFixtures.event(at: now.addingTimeInterval(2), turn: "turn-b")]))- let nextID = try #require(monitor.tasks.values.first?.snapshot.id)+ let nextID = try #require(monitor.graph.tasks.values.first?.snapshot.id)#expect(nextID != firstID)}@@ -40,7 +40,7 @@ struct ActivityTaskTests {defer { preferences.remove() }let monitor = ActivityMonitor(protectionSettings: ProtectionSettings(defaults: preferences.defaults),- protectionStore: ProtectionStore(directoryURL: directory.url), activityDirectoryURL: directory.url+ protectionStore: ProtectionStore(directoryURL: directory.url), activityDataDirectoryURL: directory.url)defer { monitor.stop() }monitor.isActivitySourceHealthy = true@@ -55,22 +55,22 @@ struct ActivityTaskTests {case "empty-turn": ""default: "turn-a"}- let events = ActivityEventKind.allCases.map {+ let events = ActivityDataEventKind.allCases.map {TestFixtures.event($0, at: now.addingTimeInterval(1), thread: thread, turn: turn)}monitor.consume(snapshot ? .snapshotEvents(events) : .live(events))- #expect(monitor.tasks.isEmpty)- #expect(monitor.completions.isEmpty)- #expect(monitor.terminations.isEmpty)+ #expect(monitor.graph.tasks.isEmpty)+ #expect(monitor.graph.completions.isEmpty)+ #expect(monitor.graph.terminations.isEmpty)monitor.consume(.live([TestFixtures.event(at: now)]))- let original = try #require(monitor.tasks.values.first?.snapshot)+ let original = try #require(monitor.graph.tasks.values.first?.snapshot)monitor.consume(snapshot ? .snapshotEvents(events) : .live(events))- #expect(monitor.tasks.count == 1)- #expect(monitor.tasks.values.first?.snapshot == original)- #expect(monitor.completions.isEmpty)- #expect(monitor.terminations.isEmpty)- #expect(monitor.pendingTerminalTasks.isEmpty)- #expect(monitor.terminalTokenUsageRequests.isEmpty)+ #expect(monitor.graph.tasks.count == 1)+ #expect(monitor.graph.tasks.values.first?.snapshot == original)+ #expect(monitor.graph.completions.isEmpty)+ #expect(monitor.graph.terminations.isEmpty)+ #expect(monitor.graph.pendingTerminalTasks.isEmpty)+ #expect(monitor.graph.terminalTokenUsageRequests.isEmpty)}@Test func lateTerminalOnlyCompletesItsOwnTurnOnce() throws {@@ -80,7 +80,7 @@ struct ActivityTaskTests {defer { preferences.remove() }let monitor = ActivityMonitor(protectionSettings: ProtectionSettings(defaults: preferences.defaults),- protectionStore: ProtectionStore(directoryURL: directory.url), activityDirectoryURL: directory.url+ protectionStore: ProtectionStore(directoryURL: directory.url), activityDataDirectoryURL: directory.url)defer { monitor.stop() }monitor.isActivitySourceHealthy = true@@ -89,10 +89,10 @@ struct ActivityTaskTests {monitor.consume(.live([TestFixtures.event(at: now.addingTimeInterval(1), turn: "turn-b")]))let terminal = TestFixtures.event(.turnCompleted, at: now.addingTimeInterval(2))monitor.consume(.live([terminal, terminal]))- #expect(Set(monitor.tasks.keys) == [.init(thread: "thread-a", turn: "turn-b")])- #expect(monitor.pendingTerminalTasks.isEmpty)- #expect(monitor.completions.count == 1)- #expect(monitor.terminations.isEmpty)+ #expect(Set(monitor.graph.tasks.keys) == [.init(thread: "thread-a", turn: "turn-b")])+ #expect(monitor.graph.pendingTerminalTasks.isEmpty)+ #expect(monitor.graph.completions.count == 1)+ #expect(monitor.graph.terminations.isEmpty)}@Test func dailySnapshotDoesNotEnterConnectionRecovery() throws {@@ -102,13 +102,13 @@ struct ActivityTaskTests {defer { preferences.remove() }let monitor = ActivityMonitor(protectionSettings: ProtectionSettings(defaults: preferences.defaults),- protectionStore: ProtectionStore(directoryURL: directory.url), activityDirectoryURL: directory.url+ protectionStore: ProtectionStore(directoryURL: directory.url), activityDataDirectoryURL: directory.url)defer { monitor.stop() }monitor.isActivitySourceHealthy = truelet presentation = monitor.sourcePresentationmonitor.consume(.snapshotEvents([TestFixtures.event(at: Date())]))- #expect(monitor.tasks.count == 1)+ #expect(monitor.graph.tasks.count == 1)#expect(monitor.sourcePresentation == presentation)#expect(!monitor.isBootstrapping)}@@ -120,21 +120,21 @@ struct ActivityTaskTests {defer { preferences.remove() }let monitor = ActivityMonitor(protectionSettings: ProtectionSettings(defaults: preferences.defaults),- protectionStore: ProtectionStore(directoryURL: directory.url), activityDirectoryURL: directory.url+ protectionStore: ProtectionStore(directoryURL: directory.url), activityDataDirectoryURL: directory.url)defer { monitor.stop() }monitor.isActivitySourceHealthy = truelet now = Date()- for kind in [ActivityEventKind.turnStarted, .toolStarted, .approvalRequested, .turnCompleted, .turnAborted] {+ for kind in [ActivityDataEventKind.turnStarted, .toolStarted, .approvalRequested, .turnCompleted, .turnAborted] {monitor.consume(.live([TestFixtures.event(kind, at: now, origin: .autoReview)]))}- #expect(monitor.tasks.isEmpty)- #expect(monitor.pendingTerminalTasks.isEmpty)- #expect(monitor.completions.isEmpty)- #expect(monitor.terminations.isEmpty)- #expect(monitor.recentlyEndedTaskAt.isEmpty)+ #expect(monitor.graph.tasks.isEmpty)+ #expect(monitor.graph.pendingTerminalTasks.isEmpty)+ #expect(monitor.graph.completions.isEmpty)+ #expect(monitor.graph.terminations.isEmpty)+ #expect(monitor.graph.recentlyEndedTaskAt.isEmpty)monitor.consume(.live([TestFixtures.event(at: now)]))- #expect(monitor.tasks.count == 1)+ #expect(monitor.graph.tasks.count == 1)}@Test func subagentLinkRetentionDoesNotDependOnIdentityOrRefreshTime() throws {@@ -144,24 +144,24 @@ struct ActivityTaskTests {defer { preferences.remove() }let monitor = ActivityMonitor(protectionSettings: ProtectionSettings(defaults: preferences.defaults),- protectionStore: ProtectionStore(directoryURL: directory.url), activityDirectoryURL: directory.url+ protectionStore: ProtectionStore(directoryURL: directory.url), activityDataDirectoryURL: directory.url)defer { monitor.stop() }let root = ActivityTaskKey(thread: "root", turn: "root-turn")let child = ActivityTurnReference(threadID: "child", turnID: "child-turn")let expired = Date().addingTimeInterval(-ActivityRetention.window - 1)- monitor.subagentTurnLinks[child] = (root, expired)- var state = SessionLifecycleState(+ monitor.graph.subagentTurnLinks[child] = (root, expired)+ var state = TurnLifecycleState(requestedThreadID: "child", turnID: "child-turn", startedAt: nil,effort: nil, lastProgressAt: nil, terminal: nil)state.rootThreadID = "root"state.rootTurnID = "root-turn"state.parentThreadID = "root"- _ = monitor.applySubagentLifecycle(state, terminalOnly: false)- #expect(monitor.subagentTurnLinks[child]?.retainedAt == expired)- _ = monitor.subagentLifecycleReferences()- #expect(monitor.subagentTurnLinks[child] == nil)+ monitor.graph.applySubagentLifecycle(state)+ #expect(monitor.graph.subagentTurnLinks[child]?.retainedAt == expired)+ _ = monitor.graph.subagentLifecycleReferences()+ #expect(monitor.graph.subagentTurnLinks[child] == nil)}@Test func incompleteSubagentRootCannotBeGuessedFromQueuedEventsOrPreviousLink() throws {@@ -171,23 +171,27 @@ struct ActivityTaskTests {defer { preferences.remove() }let monitor = ActivityMonitor(protectionSettings: ProtectionSettings(defaults: preferences.defaults),- protectionStore: ProtectionStore(directoryURL: directory.url), activityDirectoryURL: directory.url+ protectionStore: ProtectionStore(directoryURL: directory.url), activityDataDirectoryURL: directory.url)defer { monitor.stop() }- let event = TestFixtures.event(.toolStarted, agent: "child", origin: .auxiliary)- #expect(monitor.deferUnassociatedSubagentEvent(event, source: .live))- var state = SessionLifecycleState(- requestedThreadID: "child", turnID: "turn-a", startedAt: TestFixtures.now,+ let now = Date()+ let event = TestFixtures.event(.toolStarted, at: now, agentThreadID: "child", origin: .auxiliary)+ let observedResult1 = monitor.graph.deferUnassociatedSubagentEvent(event, source: .live)+ #expect(observedResult1)+ var state = TurnLifecycleState(+ requestedThreadID: "child", turnID: "turn-a", startedAt: now,effort: nil, lastProgressAt: nil, terminal: nil,rootTurnID: "turn-a", parentThreadID: "thread-a")let reference = ActivityTurnReference(threadID: "child", turnID: "turn-a")- #expect(!monitor.applySubagentLifecycle(state, terminalOnly: false))- #expect(monitor.subagentTurnLinks[reference] == nil)- monitor.subagentTurnLinks[reference] = (.init(thread: "thread-a", turn: "turn-a"), TestFixtures.now)- #expect(!monitor.applySubagentLifecycle(state, terminalOnly: false))+ monitor.applySubagentLifecycle(state)+ #expect(monitor.graph.subagentTurnLinks[reference] == nil)+ monitor.graph.subagentTurnLinks[reference] = (.init(thread: "thread-a", turn: "turn-a"), now)+ monitor.applySubagentLifecycle(state)+ #expect(monitor.graph.subagentTurnLinks[reference]?.root == .init(thread: "thread-a", turn: "turn-a"))state.rootThreadID = "different-thread"- #expect(!monitor.applySubagentLifecycle(state, terminalOnly: false))+ monitor.applySubagentLifecycle(state)+ #expect(monitor.graph.subagentTurnLinks[reference]?.root == .init(thread: "thread-a", turn: "turn-a"))}@Test(arguments: [-172800.0, 0.5, 2.0])@@ -196,11 +200,11 @@ struct ActivityTaskTests {task.startedAt = nillet start = task.lastActivityAt.addingTimeInterval(offset)let state = healthyLifecycle(startedAt: start)- _ = ActivityMonitor.mergeLifecycleBackfill(from: state, into: &task)+ ActivityTaskGraph.mergeLifecycleBackfill(from: state, into: &task)#expect(task.startedAt == (offset <= 1 ? start : nil))if task.startedAt != nil {let earlier = healthyLifecycle(startedAt: start.addingTimeInterval(-60))- _ = ActivityMonitor.mergeLifecycleBackfill(from: earlier, into: &task)+ ActivityTaskGraph.mergeLifecycleBackfill(from: earlier, into: &task)#expect(task.startedAt == start)}}@@ -208,22 +212,22 @@ struct ActivityTaskTests {@Test func onlyPendingApprovalExposesNotificationAction() {var task = makeTask(TestFixtures.event(.toolStarted))#expect(task.snapshot.approvalActionText == nil)- let event = ActivityRecord(- timestamp: TestFixtures.now.addingTimeInterval(1), name: ActivityEventKind.toolStarted.rawValue,+ let event = ActivityDataRecord(+ timestamp: TestFixtures.now.addingTimeInterval(1), name: ActivityDataEventKind.toolStarted.rawValue,origin: .main, cwd: nil, toolName: nil, model: nil, effort: nil,- threadID: "thread-a", turnID: "turn-a", agentID: nil+ threadID: "thread-a", turnID: "turn-a", agentThreadID: nil)var nameless = event- nameless.context = ActivityContext(method: "item/started", threadID: "thread-a", itemType: "fileChange")+ nameless.context = ActivityDataContext(method: "item/started", threadID: "thread-a", itemType: "fileChange")task.mergeMetadata(from: nameless)#expect(task.snapshot.approvalActionText == nil)- var approval = ActivityRecord(- timestamp: TestFixtures.now.addingTimeInterval(2), name: ActivityEventKind.approvalRequested.rawValue,+ var approval = ActivityDataRecord(+ timestamp: TestFixtures.now.addingTimeInterval(2), name: ActivityDataEventKind.approvalRequested.rawValue,origin: .main, cwd: nil, toolName: nil, model: nil, effort: nil,- threadID: "thread-a", turnID: "turn-a", agentID: nil+ threadID: "thread-a", turnID: "turn-a", agentThreadID: nil)- approval.context = ActivityContext(method: "item/commandExecution/requestApproval", threadID: "thread-a", itemType: "commandExecution")+ approval.context = ActivityDataContext(method: "item/commandExecution/requestApproval", threadID: "thread-a", itemType: "commandExecution")task.mergeMetadata(from: approval)let enteredWaiting = task.recordApprovalRequest(from: approval)#expect(enteredWaiting)@@ -233,7 +237,7 @@ struct ActivityTaskTests {var lifecycle = healthyLifecycle(startedAt: TestFixtures.now)lifecycle.isWaitingApproval = falselifecycle.approvalChangedAt = TestFixtures.now.addingTimeInterval(4)- task.mergeExecutionLifecycle(lifecycle, owner: ActivityExecutionKey(agentID: nil, turnID: "turn-a"))+ task.mergeExecutionLifecycle(lifecycle, owner: ActivityExecutionKey(agentThreadID: nil, turnID: "turn-a"))#expect(task.snapshot.approvalActionText == nil)}@@ -241,14 +245,14 @@ struct ActivityTaskTests {func approvalActionsDoNotFallBackToAnotherCommandsActions(_ actions: [String]?) {var task = makeTask()var earlier = TestFixtures.event(.toolStarted)- earlier.context = ActivityContext(method: "item/started", threadID: "thread-a", itemType: "commandExecution")+ earlier.context = ActivityDataContext(method: "item/started", threadID: "thread-a", itemType: "commandExecution")task.mergeMetadata(from: earlier)- var approval = ActivityRecord(- timestamp: TestFixtures.now.addingTimeInterval(1), name: ActivityEventKind.approvalRequested.rawValue,+ var approval = ActivityDataRecord(+ timestamp: TestFixtures.now.addingTimeInterval(1), name: ActivityDataEventKind.approvalRequested.rawValue,origin: .main, cwd: nil, toolName: nil, commandActionTypes: actions, model: nil, effort: nil,- threadID: "thread-a", turnID: "turn-a", agentID: nil+ threadID: "thread-a", turnID: "turn-a", agentThreadID: nil)- approval.context = ActivityContext(method: "item/commandExecution/requestApproval", threadID: "thread-a", itemType: "commandExecution")+ approval.context = ActivityDataContext(method: "item/commandExecution/requestApproval", threadID: "thread-a", itemType: "commandExecution")let enteredWaiting = task.recordApprovalRequest(from: approval)#expect(enteredWaiting)#expect(task.displayedApproval?.commandActionTypes == actions)@@ -265,17 +269,17 @@ struct ActivityTaskTests {defer { preferences.remove() }let monitor = ActivityMonitor(protectionSettings: ProtectionSettings(defaults: preferences.defaults),- protectionStore: ProtectionStore(directoryURL: directory.url), activityDirectoryURL: directory.url+ protectionStore: ProtectionStore(directoryURL: directory.url), activityDataDirectoryURL: directory.url)defer { monitor.stop() }monitor.isActivitySourceHealthy = truemonitor.consume(.live([TestFixtures.event(at: Date())]))monitor.consume(.live([TestFixtures.event(.turnCompleted, at: Date(), origin: .unknown)]))- #expect(monitor.tasks.count == 1)- #expect(monitor.completions.isEmpty)+ #expect(monitor.graph.tasks.count == 1)+ #expect(monitor.graph.completions.isEmpty)}- @Test func taskIdentityRequiresSessionAndTurnAndSeparatesTheirBoundaries() {+ @Test func taskIdentityRequiresThreadAndTurnAndSeparatesTheirBoundaries() {#expect(ActivityTaskKey(event: TestFixtures.event())?.threadID == "thread-a")#expect(ActivityTaskKey(event: TestFixtures.event(turn: nil)) == nil)#expect(ActivityTaskKey(event: TestFixtures.event(thread: nil)) == nil)@@ -296,24 +300,24 @@ struct ActivityTaskTests {@Test func mixedEffortIsStickyAndEmptyMetadataIsIgnored() {var task = makeTask()- let sameEffortChanged = task.mergeEffort(" high ")- #expect(!sameEffortChanged)- let emptyEffortChanged = task.mergeEffort(" \n")- #expect(!emptyEffortChanged)- let differentEffortChanged = task.mergeEffort("low")- #expect(differentEffortChanged)+ task.mergeEffort(" high ")+ #expect(task.effort == "high")+ task.mergeEffort(" \n")+ #expect(task.effort == "high")+ task.mergeEffort("low")+ #expect(task.effort == "mixed")+ task.mergeEffort("high")#expect(task.effort == "mixed")- let mixedEffortChanged = task.mergeEffort("high")- #expect(!mixedEffortChanged)}@Test func executionProgressDoesNotMoveEventOrderingBarrier() {var task = makeTask()task.recordProgress(at: TestFixtures.now.addingTimeInterval(30))task.recordProgress(at: TestFixtures.now.addingTimeInterval(10))+ task.recordProgress(at: TestFixtures.now.addingTimeInterval(30))#expect(task.acceptsExecutionEvent(TestFixtures.event(.toolCompleted, at: TestFixtures.now.addingTimeInterval(10))))#expect(task.lastProgressAt == TestFixtures.now.addingTimeInterval(30))- #expect(task.progressGeneration == 3)+ #expect(task.progressGeneration == 2)}@Test func protectionDeadlineUsesLatestProgressAndCurrentThreshold() {@@ -328,8 +332,8 @@ struct ActivityTaskTests {#expect(task.protectionDeadline(at: checked.addingTimeInterval(5), inactivityDuration: 3600) == nil)}- @Test(arguments: [SessionReadStatus.unavailable, .notFound])- func losingLifecycleCoverageRevokesProtectionEligibility(status: SessionReadStatus) {+ @Test(arguments: [TurnReadStatus.unavailable, .notFound])+ func losingLifecycleCoverageRevokesProtectionEligibility(status: TurnReadStatus) {var task = makeTask()let now = TestFixtures.now.addingTimeInterval(3600)task.recordLifecycleRead(healthyLifecycle(), at: now)@@ -349,7 +353,7 @@ struct ActivityTaskTests {state.lastProgressAt = nowstate.isWaitingApproval = falsestate.approvalChangedAt = now- let owner = ActivityExecutionKey(agentID: nil, turnID: "turn-a")+ let owner = ActivityExecutionKey(agentThreadID: nil, turnID: "turn-a")task.recordLifecycleRead(state, at: now)task.mergeExecutionLifecycle(state, owner: owner)#expect(task.state == .waitingApproval)@@ -362,13 +366,68 @@ struct ActivityTaskTests {#expect(task.state == .running)}- private func healthyLifecycle(startedAt: Date? = nil) -> SessionLifecycleState {- SessionLifecycleState(+ private func healthyLifecycle(startedAt: Date? = nil) -> TurnLifecycleState {+ TurnLifecycleState(requestedThreadID: "thread-a", turnID: "turn-a", startedAt: startedAt, effort: nil,lastProgressAt: nil, terminal: nil, readStatus: .complete)}+ @Test(arguments: [-10.0, 0.0, 30.0])+ func delayedChildToolEventOnlyInvalidatesProtectionForLaterProgress(offset: TimeInterval) throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let preferences = try TestPreferences()+ defer { preferences.remove() }+ let monitor = try makeProtectionMonitor(in: directory, preferences: preferences)+ defer { monitor.stop() }+ let now = Date()+ let progress = now.addingTimeInterval(-7200)+ let root = ActivityTurnReference(threadID: "thread-a", turnID: "turn-a")+ let child = ActivityTurnReference(threadID: "child", turnID: "child-turn")+ let rootState = TurnLifecycleState(+ requestedThreadID: root.threadID, turnID: root.turnID, startedAt: progress.addingTimeInterval(-100),+ lastProgressAt: progress, terminal: nil, rootTurnID: root.turnID, rootThreadID: root.threadID+ )+ var childState = TurnLifecycleState(+ requestedThreadID: child.threadID, turnID: child.turnID, startedAt: progress.addingTimeInterval(-90),+ lastProgressAt: progress.addingTimeInterval(-60), terminal: nil,+ rootTurnID: root.turnID, rootThreadID: root.threadID, parentThreadID: root.threadID+ )+ var graph = ActivityTaskGraph()+ _ = graph.update(+ events: [TestFixtures.event(at: progress.addingTimeInterval(-100))], source: .bootstrap,+ lifecycle: TurnLifecycleSnapshot(states: [root: rootState, child: childState], verifiedTurns: [root: now, child: now])+ )+ graph.backfilledTasks.removeAll()+ monitor.graph = graph+ let task = try #require(graph.tasks.values.first)+ let candidate = ProtectionCandidate(+ key: task.key, taskID: task.displayID, projectName: task.projectName,+ lastProgressAt: task.lastProgressAt, progressGeneration: task.progressGeneration, inactivityDuration: .oneHour+ )+ #expect(monitor.isProtectionCandidateRelevant(candidate, now: now))+ let attemptID = UUID()+ monitor.protectionAttempts[task.key] = ProtectionAttempt(+ id: attemptID, candidate: candidate, markedAt: now, timeoutTask: Task {}+ )+ let eventDate = progress.addingTimeInterval(offset)+ childState.lastProgressAt = eventDate+ let transitions = graph.update(+ events: [TestFixtures.event(.toolCompleted, at: eventDate, turn: child.turnID, agentThreadID: child.threadID, origin: .auxiliary)], source: .live,+ lifecycle: TurnLifecycleSnapshot(states: [root: rootState, child: childState], verifiedTurns: [root: now, child: now])+ )+ #expect(transitions.isEmpty)+ #expect(graph.tasks[task.key]?.lastProgressAt == max(progress, eventDate))+ #expect(graph.tasks[task.key]?.progressGeneration == task.progressGeneration + (offset > 0 ? 1 : 0))+ #expect(graph.tasks[task.key]?.executions[.init(agentThreadID: child.threadID, turnID: child.turnID)]?.lastEventAt == eventDate)+ monitor.acceptFixtureGraph(graph, phase: .live, transitions: transitions)+ #expect(monitor.isProtectionCandidateRelevant(candidate, now: now) == (offset <= 0))+ monitor.finishProtectionAttempt(task.key, attemptID: attemptID, taskID: task.displayID, notificationWasSubmitted: false)+ #expect((monitor.suppressedTasks[task.key] != nil) == (offset <= 0))+ #expect(monitor.protectionAttempts.isEmpty)+ }+@Test(arguments: ["unchanged", "growth", "unavailable", "stale"])func suppressionRevalidatesCoverageAfterNotification(scenario: String) throws {let directory = try TestDirectory()@@ -380,7 +439,7 @@ struct ActivityTaskTests {let now = Date()var task = makeTask(TestFixtures.event(at: now.addingTimeInterval(-7200)))task.recordLifecycleRead(healthyLifecycle(), at: now)- monitor.tasks[task.key] = task+ monitor.graph.tasks[task.key] = tasklet candidate = ProtectionCandidate(key: task.key, taskID: task.displayID, projectName: task.projectName,lastProgressAt: task.lastProgressAt, progressGeneration: task.progressGeneration, inactivityDuration: .oneHour@@ -396,13 +455,14 @@ struct ActivityTaskTests {case "stale": task.lifecycleCoverageCheckedAt = now.addingTimeInterval(-5)default: break}- monitor.tasks[task.key] = task+ monitor.graph.tasks[task.key] = tasklet attemptID = UUID()monitor.protectionAttempts[task.key] = ProtectionAttempt(id: attemptID, candidate: candidate, markedAt: now, timeoutTask: Task {})monitor.finishProtectionAttempt(task.key, attemptID: attemptID, taskID: task.displayID, notificationWasSubmitted: false)- #expect(monitor.tasks[task.key]?.state == (scenario == "unchanged" ? .suppressed : .running))+ #expect(monitor.graph.tasks[task.key]?.state == .running)+ #expect((monitor.suppressedTasks[task.key] != nil) == (scenario == "unchanged"))#expect(monitor.protectionAttempts.isEmpty)}@@ -426,28 +486,31 @@ struct ActivityTaskTests {let key = task.keylet identifier = key.protectionIdentifiertask.recordLifecycleRead(healthyLifecycle(), at: hiddenAt)- monitor.tasks[key] = task+ monitor.graph.tasks[key] = taskmonitor.protectionSettings.setInactivityDuration(previousDuration)monitor.reconcileProtection(now: hiddenAt, sendsNotification: false)- #expect(monitor.tasks[key]?.state == .suppressed)+ #expect(monitor.graph.tasks[key]?.state == .running)+ #expect(monitor.suppressedTasks[key] != nil)#expect(monitor.protectionRecords[identifier] != nil)let checkedAt = expiredObservation ? changedAt.addingTimeInterval(-5) : changedAt- monitor.tasks[key]?.recordLifecycleRead(healthyLifecycle(), at: checkedAt)+ monitor.graph.tasks[key]?.recordLifecycleRead(healthyLifecycle(), at: checkedAt)monitor.protectionSettings.setInactivityDuration(newDuration)monitor.handleProtectionTimingChange()- #expect(monitor.tasks[key]?.state == .running)+ #expect(monitor.graph.tasks[key]?.state == .running)#expect(monitor.protectionRecords[identifier] == nil)- #expect(monitor.tasks[key]?.lastProgressAt == task.lastProgressAt)- #expect(monitor.tasks[key]?.hasFreshLifecycle(at: changedAt) == !expiredObservation)+ #expect(monitor.suppressedTasks[key] == nil)+ #expect(monitor.graph.tasks[key]?.lastProgressAt == task.lastProgressAt)+ #expect(monitor.graph.tasks[key]?.hasFreshLifecycle(at: changedAt) == !expiredObservation)monitor.reconcileProtection(now: newDeadline, sendsNotification: false)- #expect(monitor.tasks[key]?.state == .running)- monitor.tasks[key]?.recordLifecycleRead(healthyLifecycle(), at: newDeadline.addingTimeInterval(-1))+ #expect(monitor.graph.tasks[key]?.state == .running)+ monitor.graph.tasks[key]?.recordLifecycleRead(healthyLifecycle(), at: newDeadline.addingTimeInterval(-1))monitor.reconcileProtection(now: newDeadline.addingTimeInterval(-1), sendsNotification: false)- #expect(monitor.tasks[key]?.state == .running)+ #expect(monitor.graph.tasks[key]?.state == .running)monitor.reconcileProtection(now: newDeadline, sendsNotification: false)- #expect(monitor.tasks[key]?.state == .suppressed)+ #expect(monitor.graph.tasks[key]?.state == .running)+ #expect(monitor.suppressedTasks[key] != nil)#expect(monitor.protectionRecords[identifier] != nil)monitor.cancelInactivityCheck()await monitor.protectionPersistenceTask?.value@@ -462,7 +525,7 @@ struct ActivityTaskTests {monitor.isStarted = truemonitor.isProtectionEnabled = truemonitor.isActivitySourceHealthy = true- monitor.activityReader = AppServerActivityReader(lifecycleCache: SessionLifecycleCache(), socketURL: directory.url.appendingPathComponent("test.sock")) { _ in }+ monitor.activityReader = AppServerActivityReader(probe: ActivityReaderProbe(), socketURL: directory.url.appendingPathComponent("test.sock")) { _ in }return monitor}@@ -475,22 +538,22 @@ struct ActivityTaskTests {#expect(task.snapshot.activeSubagentCount == nil)task.hasCompleteSubagentCoverage = true#expect(task.snapshot.activeSubagentCount == 0)- task.recordSubagentActivity(agentID: "agent", isStarting: true, at: TestFixtures.now)- task.recordSubagentActivity(agentID: "agent", isStarting: true, at: TestFixtures.now)+ task.recordSubagentActivity(agentThreadID: "agent", isStarting: true, at: TestFixtures.now)+ task.recordSubagentActivity(agentThreadID: "agent", isStarting: true, at: TestFixtures.now)#expect(task.snapshot.activeSubagentCount == 1)- task.recordSubagentActivity(agentID: "agent", isStarting: false, at: TestFixtures.now.addingTimeInterval(2))- task.recordSubagentActivity(agentID: "agent", isStarting: true, at: TestFixtures.now.addingTimeInterval(1))+ task.recordSubagentActivity(agentThreadID: "agent", isStarting: false, at: TestFixtures.now.addingTimeInterval(2))+ task.recordSubagentActivity(agentThreadID: "agent", isStarting: true, at: TestFixtures.now.addingTimeInterval(1))#expect(task.snapshot.activeSubagentCount == 0)}@Test func missingSubagentIdentityAndUnmatchedStopMakeCountUnavailable() {var missing = makeTask()missing.hasCompleteSubagentCoverage = true- missing.recordSubagentActivity(agentID: nil, isStarting: true, at: TestFixtures.now)+ missing.recordSubagentActivity(agentThreadID: nil, isStarting: true, at: TestFixtures.now)#expect(missing.snapshot.activeSubagentCount == nil)var unmatched = makeTask()unmatched.hasCompleteSubagentCoverage = true- unmatched.recordSubagentActivity(agentID: "unknown", isStarting: false, at: TestFixtures.now)+ unmatched.recordSubagentActivity(agentThreadID: "unknown", isStarting: false, at: TestFixtures.now)#expect(unmatched.snapshot.activeSubagentCount == nil)}@@ -507,14 +570,14 @@ struct ActivityTaskTests {@Test func mainProgressCannotDismissSubagentApproval() {var task = makeTask()- let approval = TestFixtures.event(.approvalRequested, agent: "agent", origin: .auxiliary)+ let approval = TestFixtures.event(.approvalRequested, agentThreadID: "agent", origin: .auxiliary)let enteredWaiting = task.recordApprovalRequest(from: approval)#expect(enteredWaiting)task.resumeExecution(from: TestFixtures.event(.toolCompleted, at: TestFixtures.now.addingTimeInterval(1)))#expect(task.state == .waitingApproval)- task.resumeExecution(from: TestFixtures.event(.toolCompleted, at: TestFixtures.now.addingTimeInterval(2), agent: "agent", origin: .auxiliary))+ task.resumeExecution(from: TestFixtures.event(.toolCompleted, at: TestFixtures.now.addingTimeInterval(2), agentThreadID: "agent", origin: .auxiliary))#expect(task.state == .waitingApproval)- task.finishExecution(ActivityExecutionKey(agentID: "agent", turnID: "turn-a"), at: TestFixtures.now.addingTimeInterval(3))+ task.finishExecution(ActivityExecutionKey(agentThreadID: "agent", turnID: "turn-a"), at: TestFixtures.now.addingTimeInterval(3))#expect(task.state == .running)}@@ -522,13 +585,13 @@ struct ActivityTaskTests {var task = makeTask()let mainEnteredWaiting = task.recordApprovalRequest(from: TestFixtures.event(.approvalRequested))#expect(mainEnteredWaiting)- let agentRequest = TestFixtures.event(.approvalRequested, at: TestFixtures.now.addingTimeInterval(1), agent: "agent", origin: .auxiliary)+ let agentRequest = TestFixtures.event(.approvalRequested, at: TestFixtures.now.addingTimeInterval(1), agentThreadID: "agent", origin: .auxiliary)let agentEnteredWaiting = task.recordApprovalRequest(from: agentRequest)#expect(!agentEnteredWaiting)- task.finishExecution(ActivityExecutionKey(agentID: nil, turnID: "turn-a"), at: TestFixtures.now.addingTimeInterval(2))+ task.finishExecution(ActivityExecutionKey(agentThreadID: nil, turnID: "turn-a"), at: TestFixtures.now.addingTimeInterval(2))#expect(task.state == .waitingApproval)#expect(task.displayedApproval?.requestedAt == TestFixtures.now.addingTimeInterval(1))- task.finishExecution(ActivityExecutionKey(agentID: "agent", turnID: "turn-a"), at: TestFixtures.now.addingTimeInterval(3))+ task.finishExecution(ActivityExecutionKey(agentThreadID: "agent", turnID: "turn-a"), at: TestFixtures.now.addingTimeInterval(3))#expect(task.state == .running)}@@ -539,7 +602,7 @@ struct ActivityTaskTests {let lateRequestChanged = task.recordApprovalRequest(from: TestFixtures.event(.approvalRequested))#expect(lateRequestChanged)#expect(task.state == .waitingApproval)- task.finishExecution(ActivityExecutionKey(agentID: nil, turnID: "turn-a"), at: progress)+ task.finishExecution(ActivityExecutionKey(agentThreadID: nil, turnID: "turn-a"), at: progress)#expect(!task.acceptsExecutionEvent(TestFixtures.event(.toolStarted, at: progress.addingTimeInterval(1))))}@@ -552,7 +615,7 @@ struct ActivityTaskTests {#expect(task.state == .waitingApproval)}- private func makeTask(_ event: ActivityRecord = TestFixtures.event()) -> ActivityTask {+ private func makeTask(_ event: ActivityDataRecord = TestFixtures.event()) -> ActivityTask {ActivityTask(displayID: UUID(), key: ActivityTaskKey(event: event)!, event: event,state: .running, startedAt: event.timestamp, progressGeneration: 1diff --git a/CodexBarTests/Activity/ActivityTokenUsageTests.swift b/CodexBarTests/Activity/ActivityTokenUsageTests.swiftindex 840a0d4..d631c78 100644--- a/CodexBarTests/Activity/ActivityTokenUsageTests.swift+++ b/CodexBarTests/Activity/ActivityTokenUsageTests.swift@@ -1,13 +1,14 @@+import Combineimport Foundationimport Testingstruct ActivityTokenUsageTests {@Test func cacheHitRateUsesAllInputAndIsUnavailableWithoutInput() {- let usage = TokenUsage(+ let tokenUsage = TokenUsage(inputTokens: 1000, cachedInputTokens: 800, cacheWriteInputTokens: 100,outputTokens: 200, reasoningOutputTokens: 50, totalTokens: 1200)- #expect(usage.cacheHitRate == 0.8)+ #expect(tokenUsage.cacheHitRate == 0.8)let outputOnly = TokenUsage(inputTokens: 0, cachedInputTokens: 0, cacheWriteInputTokens: 0,outputTokens: 200, reasoningOutputTokens: 50, totalTokens: 200@@ -25,14 +26,14 @@ struct ActivityTokenUsageTests {var rootState = state(root, input: 100)rootState.rootThreadID = explicitRootThread ? root.threadID : nillet states = [rootState, state(first, input: 200), state(second, input: 300), state(third, input: 400)]- let usage = try #require(request.usage(from: states))- #expect(usage.inputTokens == 1000)- #expect(usage.outputTokens == 40)- #expect(usage.totalTokens == 1040)- #expect(usage.cachedInputTokens == 80)+ let tokenUsage = try #require(request.tokenUsage(from: states))+ #expect(tokenUsage.inputTokens == 1000)+ #expect(tokenUsage.outputTokens == 40)+ #expect(tokenUsage.totalTokens == 1040)+ #expect(tokenUsage.cachedInputTokens == 80)}- @Test(arguments: ["missing", "unavailable", "other-root", "other-session", "running-child", "missing-usage"])+ @Test(arguments: ["missing", "unavailable", "other-root", "other-root-thread", "running-child", "missing-tokenUsage"])func incompleteOrUnrelatedChildPreventsPartialTotal(scenario: String) {let root = reference("thread-a", "turn-a")let child = reference("child-a", "child-turn")@@ -41,21 +42,21 @@ struct ActivityTokenUsageTests {switch scenario {case "unavailable": childState.readStatus = .unavailablecase "other-root": childState.rootTurnID = "other-root"- case "other-session": childState.rootThreadID = "other-session"- case "missing-usage": childState.tokenUsage = nil+ case "other-root-thread": childState.rootThreadID = "other-root-thread"+ case "missing-tokenUsage": childState.tokenUsage = nilcase "running-child": childState = state(child, terminal: nil)default: break}let states = scenario == "missing" ? [state(root)] : [state(root), childState]- #expect(request.usage(from: states) == nil)+ #expect(request.tokenUsage(from: states) == nil)}@Test func knownAgentWithoutResolvedTurnPreventsPartialTotal() {let root = reference("thread-a", "turn-a")- let request = TaskTokenRequest(- root: root, expectedAgentIDs: ["unknown-child"], references: [root]+ let request = TaskTokenUsageRequest(+ root: root, expectedAgentThreadIDs: ["unknown-child"], references: [root])- #expect(request.usage(from: [state(root)]) == nil)+ #expect(request.tokenUsage(from: [state(root)]) == nil)}@Test func inProgressUsageIncludesAvailableRunningAndCompletedChildren() throws {@@ -65,14 +66,14 @@ struct ActivityTokenUsageTests {let unknown = reference("child-b", "child-turn-c")let request = request(root: root, children: [running, completed, unknown, running])let states = [state(root, terminal: nil), state(running, input: 200, terminal: nil), state(completed, input: 300)]- let usage = try #require(request.usage(from: states, requiresFinalUsage: false))- #expect(usage.totalTokens == 630)- #expect(usage.cachedInputTokens == 60)- #expect(request.usage(from: states) == nil)- #expect(request.usage(from: [], requiresFinalUsage: false) == nil)+ let tokenUsage = try #require(request.tokenUsage(from: states, requiresFinalTokenUsage: false))+ #expect(tokenUsage.totalTokens == 630)+ #expect(tokenUsage.cachedInputTokens == 60)+ #expect(request.tokenUsage(from: states) == nil)+ #expect(request.tokenUsage(from: [], requiresFinalTokenUsage: false) == nil)}- @Test(arguments: ["other-root", "other-session", "other-thread", "unavailable", "missing-usage"])+ @Test(arguments: ["other-root", "other-root-thread", "other-thread", "unavailable", "missing-tokenUsage"])func inProgressUsageDoesNotIncludeUnverifiedChildren(scenario: String) {let root = reference("thread-a", "turn-a")let child = reference("child-a", "child-turn")@@ -80,13 +81,13 @@ struct ActivityTokenUsageTests {var childState = state(child, terminal: nil)switch scenario {case "other-root": childState.rootTurnID = "other-root"- case "other-session": childState.rootThreadID = "other-session"+ case "other-root-thread": childState.rootThreadID = "other-root-thread"case "other-thread": childState = state(reference("other-thread", child.turnID), terminal: nil)case "unavailable": childState.readStatus = .unavailabledefault: childState.tokenUsage = nil}let states = [state(root, terminal: nil), childState]- #expect(request.usage(from: states, requiresFinalUsage: false)?.totalTokens == 110)+ #expect(request.tokenUsage(from: states, requiresFinalTokenUsage: false)?.totalTokens == 110)}@Test func activeSnapshotsUpdateBeforeCompletionAndKeepCompletedChildReferences() throws {@@ -97,38 +98,47 @@ struct ActivityTokenUsageTests {let monitor = try makeMonitor(directory: directory, preferences: preferences)let event = TestFixtures.event()let key = try #require(ActivityTaskKey(event: event))- monitor.tasks[key] = ActivityTask(+ monitor.graph.tasks[key] = ActivityTask(displayID: UUID(), key: key, event: event, state: .running,startedAt: TestFixtures.now, progressGeneration: 1)let root = reference("thread-a", "turn-a")let child = reference("child-a", "child-turn")- monitor.subagentTurnLinks[child] = (key, TestFixtures.now)- #expect(Set(monitor.activeTokenUsageReferences()) == [root, child])- #expect(monitor.tasks[key]?.snapshot.tokenUsage == nil)+ monitor.graph.subagentTurnLinks[child] = (key, TestFixtures.now)+ #expect(Set(monitor.graph.activeTokenUsageReferences()) == [root, child])+ #expect(monitor.graph.tasks[key]?.snapshot.tokenUsage == nil)let initial = [state(root, terminal: nil)]- #expect(monitor.applyActiveTokenUsage(initial))- let running = try #require(monitor.tasks[key]?.snapshot)+ monitor.graph.applyActiveTokenUsage(initial)+ let running = try #require(monitor.graph.tasks[key]?.snapshot)#expect(running.tokenUsage?.totalTokens == 110)- #expect(!monitor.applyActiveTokenUsage(initial))+ monitor.graph.applyActiveTokenUsage(initial)+ #expect(monitor.graph.tasks[key]?.snapshot == running)- monitor.tasks[key]?.state = .waitingApproval+ monitor.graph.tasks[key]?.state = .waitingApprovallet updated = [state(root, input: 200, terminal: nil), state(child, input: 300)]- #expect(monitor.applyActiveTokenUsage(updated))- let waiting = try #require(monitor.tasks[key]?.snapshot)+ monitor.graph.applyActiveTokenUsage(updated)+ let waiting = try #require(monitor.graph.tasks[key]?.snapshot)#expect(waiting.tokenUsage?.totalTokens == 520)- #expect(monitor.tasks[key]?.state == .waitingApproval)- #expect(monitor.pendingTerminalPresentationEvents.isEmpty)- #expect(monitor.completions.isEmpty)+ #expect(monitor.graph.tasks[key]?.state == .waitingApproval)+ var presentations: [ActivityPresentationUpdate] = []+ let presentation = monitor.presentationPublisher.sink { presentations.append($0) }+ defer { presentation.cancel()+ monitor.stop()+ }+ monitor.refreshSnapshot(now: TestFixtures.now)+ #expect(presentations.count == 1)+ let hasNoTerminalEvents = presentations.allSatisfy(\.terminalEvents.isEmpty)+ #expect(hasNoTerminalEvents)+ #expect(monitor.graph.completions.isEmpty)- let task = try #require(monitor.tasks[key])+ let task = try #require(monitor.graph.tasks[key])var transitions: [ActivityTransition] = []- monitor.resolveTerminal(+ monitor.graph.resolveTerminal(.completed(at: TestFixtures.now, duration: 1), task: task, key: key,abortFallback: TestFixtures.now, into: &transitions)- #expect(monitor.applyTerminalTokenUsage(updated))- #expect(monitor.completions.first?.tokenUsage?.totalTokens == 520)+ monitor.graph.applyTerminalTokenUsage(updated)+ #expect(monitor.graph.completions.first?.tokenUsage?.totalTokens == 520)}@Test func activeUsageDoesNotCarryAcrossTurnsOrKeepUnavailableData() throws {@@ -139,19 +149,19 @@ struct ActivityTokenUsageTests {let monitor = try makeMonitor(directory: directory, preferences: preferences)let event = TestFixtures.event(turn: "turn-b")let key = try #require(ActivityTaskKey(event: event))- monitor.tasks[key] = ActivityTask(+ monitor.graph.tasks[key] = ActivityTask(displayID: UUID(), key: key, event: event, state: .running,startedAt: TestFixtures.now, progressGeneration: 1)- #expect(!monitor.applyActiveTokenUsage([state(reference("thread-a", "turn-a"))]))- #expect(monitor.tasks[key]?.tokenUsage == nil)+ monitor.graph.applyActiveTokenUsage([state(reference("thread-a", "turn-a"))])+ #expect(monitor.graph.tasks[key]?.tokenUsage == nil)var current = state(reference("thread-a", "turn-b"), terminal: nil)current.rootTurnID = "turn-b"- #expect(monitor.applyActiveTokenUsage([current]))- #expect(monitor.tasks[key]?.tokenUsage?.totalTokens == 110)+ monitor.graph.applyActiveTokenUsage([current])+ #expect(monitor.graph.tasks[key]?.tokenUsage?.totalTokens == 110)current.readStatus = .unavailable- #expect(monitor.applyActiveTokenUsage([current]))- #expect(monitor.tasks[key]?.tokenUsage == nil)+ monitor.graph.applyActiveTokenUsage([current])+ #expect(monitor.graph.tasks[key]?.tokenUsage == nil)}@Test func overflowIsRejectedAndConfirmedZeroRemainsAvailable() {@@ -182,41 +192,55 @@ struct ActivityTokenUsageTests {startedAt: TestFixtures.now, progressGeneration: 1)var transitions: [ActivityTransition] = []- monitor.resolveTerminal(+ monitor.graph.resolveTerminal(.completed(at: TestFixtures.now, duration: 1), task: task, key: key,abortFallback: TestFixtures.now, into: &transitions)- let completedID = try #require(monitor.completions.first?.id)+ let completedID = try #require(monitor.graph.completions.first?.id)let otherEvent = TestFixtures.event(.turnAborted, turn: "turn-b")let otherKey = try #require(ActivityTaskKey(event: otherEvent))- monitor.tasks[otherKey] = ActivityTask(+ monitor.graph.tasks[otherKey] = ActivityTask(displayID: UUID(), key: otherKey, event: otherEvent, state: .running,startedAt: TestFixtures.now, progressGeneration: 1)- monitor.finishTask(from: otherEvent, source: .bootstrap, into: &transitions)- #expect(monitor.completions.first?.tokenUsage == nil)- #expect(monitor.terminations.first?.tokenUsage == nil)+ monitor.graph.finishTask(from: otherEvent, source: .bootstrap, into: &transitions)+ #expect(monitor.graph.completions.first?.tokenUsage == nil)+ #expect(monitor.graph.terminations.first?.tokenUsage == nil)var stopped = state(reference("thread-a", "turn-b"), input: 200, terminal: nil)stopped.rootTurnID = "turn-b"let states = [state(reference("thread-a", "turn-a")), stopped]- #expect(monitor.applyTerminalTokenUsage(states))- #expect(monitor.completions.first?.tokenUsage?.totalTokens == 110)- #expect(monitor.terminations.first?.tokenUsage?.totalTokens == 210)- #expect(monitor.completions.first?.id == completedID)- #expect(monitor.pendingTerminalPresentationEvents.isEmpty)- #expect(!monitor.applyTerminalTokenUsage(states))+ var presentations: [ActivityPresentationUpdate] = []+ let presentation = monitor.presentationPublisher.sink { presentations.append($0) }+ defer { presentation.cancel()+ monitor.stop()+ }+ monitor.graph.applyTerminalTokenUsage(states)+ #expect(monitor.graph.completions.first?.tokenUsage?.totalTokens == 110)+ #expect(monitor.graph.terminations.first?.tokenUsage?.totalTokens == 210)+ #expect(monitor.graph.completions.first?.id == completedID)+ monitor.refreshSnapshot(now: TestFixtures.now)+ #expect(presentations.count == 1)+ let hasNoTerminalEvents = presentations.allSatisfy(\.terminalEvents.isEmpty)+ #expect(hasNoTerminalEvents)+ let completed = monitor.graph.completions+ let terminated = monitor.graph.terminations+ monitor.graph.applyTerminalTokenUsage(states)+ monitor.refreshSnapshot(now: TestFixtures.now)+ #expect(presentations.count == 1)+ #expect(monitor.graph.completions == completed)+ #expect(monitor.graph.terminations == terminated)stopped.readStatus = .unavailable- #expect(!monitor.applyTerminalTokenUsage([stopped]))- #expect(monitor.terminations.first?.tokenUsage?.totalTokens == 210)- let terminalReferences = monitor.terminalTokenUsageReferences()+ monitor.graph.applyTerminalTokenUsage([stopped])+ #expect(monitor.graph.terminations.first?.tokenUsage?.totalTokens == 210)+ let terminalReferences = monitor.graph.terminalTokenUsageReferences()#expect(terminalReferences.count == 2)- let laterReferences = monitor.terminalTokenUsageReferences()+ let laterReferences = monitor.graph.terminalTokenUsageReferences()#expect(Set(laterReferences) == Set(terminalReferences))- #expect(monitor.terminalTokenUsageRequests.count == 2)- #expect(monitor.completions.first?.tokenUsage?.totalTokens == 110)+ #expect(monitor.graph.terminalTokenUsageRequests.count == 2)+ #expect(monitor.graph.completions.first?.tokenUsage?.totalTokens == 110)monitor.clearCollectedActivityState()- #expect(monitor.completions.isEmpty)+ #expect(monitor.graph.completions.isEmpty)}@Test func laterTurnPendingAgentMustNotEraseCompletedTurnUsage() throws {@@ -232,31 +256,32 @@ struct ActivityTokenUsageTests {startedAt: TestFixtures.now, progressGeneration: 1)var transitions: [ActivityTransition] = []- monitor.resolveTerminal(.completed(at: TestFixtures.now, duration: 1), task: task, key: firstKey, abortFallback: TestFixtures.now, into: &transitions)+ monitor.graph.resolveTerminal(.completed(at: TestFixtures.now, duration: 1), task: task, key: firstKey, abortFallback: TestFixtures.now, into: &transitions)let firstState = state(reference("thread-a", "turn-a"))- _ = monitor.applyTerminalTokenUsage([firstState])- #expect(monitor.completions.first?.tokenUsage?.totalTokens == 110)+ monitor.graph.applyTerminalTokenUsage([firstState])+ #expect(monitor.graph.completions.first?.tokenUsage?.totalTokens == 110)let second = TestFixtures.event(at: TestFixtures.now.addingTimeInterval(2), turn: "turn-b")let secondKey = try #require(ActivityTaskKey(event: second))- monitor.tasks[secondKey] = ActivityTask(+ monitor.graph.tasks[secondKey] = ActivityTask(displayID: UUID(), key: secondKey, event: second, state: .running,startedAt: second.timestamp, progressGeneration: 2)- let child = TestFixtures.event(.subagentStarted, at: TestFixtures.now.addingTimeInterval(3), turn: "child-turn-b", agent: "child-b")- #expect(monitor.deferUnassociatedSubagentEvent(child, source: .live))- _ = monitor.applyTerminalTokenUsage([firstState])- #expect(monitor.completions.first?.tokenUsage?.totalTokens == 110)+ let child = TestFixtures.event(.subagentStarted, at: TestFixtures.now.addingTimeInterval(3), turn: "child-turn-b", agentThreadID: "child-b")+ let observedResult11 = monitor.graph.deferUnassociatedSubagentEvent(child, source: .live)+ #expect(observedResult11)+ monitor.graph.applyTerminalTokenUsage([firstState])+ #expect(monitor.graph.completions.first?.tokenUsage?.totalTokens == 110)let lateChild = reference("child-a", "child-turn-a")- monitor.subagentTurnLinks[lateChild] = (firstKey, TestFixtures.now)- #expect(!monitor.applyTerminalTokenUsage([firstState]))- #expect(monitor.completions.first?.tokenUsage?.totalTokens == 110)- #expect(monitor.applyTerminalTokenUsage([firstState, state(lateChild, input: 200)]))- #expect(monitor.completions.first?.tokenUsage?.totalTokens == 320)+ monitor.graph.subagentTurnLinks[lateChild] = (firstKey, TestFixtures.now)+ monitor.graph.applyTerminalTokenUsage([firstState])+ #expect(monitor.graph.completions.first?.tokenUsage?.totalTokens == 110)+ monitor.graph.applyTerminalTokenUsage([firstState, state(lateChild, input: 200)])+ #expect(monitor.graph.completions.first?.tokenUsage?.totalTokens == 320)var unavailable = firstStateunavailable.readStatus = .unavailable- #expect(!monitor.applyTerminalTokenUsage([unavailable]))- _ = monitor.terminalTokenUsageReferences()- #expect(monitor.completions.first?.tokenUsage?.totalTokens == 320)+ monitor.graph.applyTerminalTokenUsage([unavailable])+ _ = monitor.graph.terminalTokenUsageReferences()+ #expect(monitor.graph.completions.first?.tokenUsage?.totalTokens == 320)}@Test(arguments: [false, true])@@ -269,7 +294,7 @@ struct ActivityTokenUsageTests {monitor.isActivitySourceHealthy = truemonitor.isProtectionRecoveryInProgress = falselet now = Date()- monitor.sessionTransitionNotBefore = now.addingTimeInterval(-1)+ monitor.turnTransitionNotBefore = now.addingTimeInterval(-1)monitor.terminalPresentationNotBefore = now.addingTimeInterval(-1)#expect(monitor.canPublishActivityTransitions)for publishesEvents in [false, true] {@@ -280,22 +305,23 @@ struct ActivityTokenUsageTests {startedAt: now, progressGeneration: 1)var transitions: [ActivityTransition] = []- monitor.resolveTerminal(+ monitor.graph.resolveTerminal(aborted ? .aborted(at: now, duration: nil) : .completed(at: now, duration: 1),task: task, key: key, abortFallback: now, publishesEvents: publishesEvents, into: &transitions)#expect(transitions.isEmpty == (aborted || !publishesEvents))- #expect(monitor.pendingTerminalPresentationEvents.isEmpty == !publishesEvents)- #expect(monitor.recentEndedDate(for: key, now: now) != nil)- let presentationCount = monitor.pendingTerminalPresentationEvents.count- monitor.resolveTerminal(+ #expect(monitor.graph.terminalEvents.isEmpty == !publishesEvents)+ let observedResult15 = monitor.graph.recentEndedDate(for: key, now: now) != nil+ #expect(observedResult15)+ let presentationCount = monitor.graph.terminalEvents.count+ monitor.graph.resolveTerminal(aborted ? .aborted(at: now, duration: nil) : .completed(at: now, duration: 1),task: task, key: key, abortFallback: now, publishesEvents: true, into: &transitions)- #expect(monitor.pendingTerminalPresentationEvents.count == presentationCount)+ #expect(monitor.graph.terminalEvents.count == presentationCount)}- #expect(aborted ? monitor.terminations.count == 2 : monitor.completions.count == 2)- #expect(monitor.terminalTokenUsageRequests.count == 2)+ #expect(aborted ? monitor.graph.terminations.count == 2 : monitor.graph.completions.count == 2)+ #expect(monitor.graph.terminalTokenUsageRequests.count == 2)}@Test(arguments: [false, true])@@ -308,7 +334,7 @@ struct ActivityTokenUsageTests {monitor.isActivitySourceHealthy = truemonitor.isProtectionRecoveryInProgress = falselet now = Date()- monitor.sessionTransitionNotBefore = now.addingTimeInterval(-1)+ monitor.turnTransitionNotBefore = now.addingTimeInterval(-1)monitor.terminalPresentationNotBefore = now.addingTimeInterval(-1)let event = TestFixtures.event(.turnAborted, at: now)let key = try #require(ActivityTaskKey(event: event))@@ -318,21 +344,21 @@ struct ActivityTokenUsageTests {)var transitions: [ActivityTransition] = []if interrupted {- monitor.tasks[key] = task- monitor.finishTask(from: event, source: .live, into: &transitions)- monitor.finishTask(from: event, source: .live, into: &transitions)- #expect(monitor.tasks.isEmpty)- #expect(monitor.terminations.count == 1)+ monitor.graph.tasks[key] = task+ monitor.graph.finishTask(from: event, source: .live, into: &transitions)+ monitor.graph.finishTask(from: event, source: .live, into: &transitions)+ #expect(monitor.graph.tasks.isEmpty)+ #expect(monitor.graph.terminations.count == 1)} else {- monitor.resolveTerminal(+ monitor.graph.resolveTerminal(.completed(at: now, duration: 1), task: task, key: key,abortFallback: now, into: &transitions)- #expect(monitor.completions.count == 1)+ #expect(monitor.graph.completions.count == 1)}#expect(transitions.count == (interrupted ? 0 : 1))- #expect(monitor.pendingTerminalPresentationEvents.count == 1)- #expect(monitor.terminalTokenUsageRequests.count == 1)+ #expect(monitor.graph.terminalEvents.count == 1)+ #expect(monitor.graph.terminalTokenUsageRequests.count == 1)}@Test(arguments: [ActivityTaskState.running, .waitingApproval])@@ -346,12 +372,12 @@ struct ActivityTokenUsageTests {let old = now.addingTimeInterval(-ActivityRetention.window - 1)let event = TestFixtures.event(at: old)let key = try #require(ActivityTaskKey(event: event))- monitor.tasks[key] = ActivityTask(+ monitor.graph.tasks[key] = ActivityTask(displayID: UUID(), key: key, event: event, state: state,startedAt: old, progressGeneration: 1)monitor.refreshSnapshot(now: now)- #expect(monitor.tasks.count == 1)+ #expect(monitor.graph.tasks.count == 1)#expect(monitor.snapshot.activeCount == 1)}@@ -370,37 +396,37 @@ struct ActivityTokenUsageTests {startedAt: now, progressGeneration: 1)var transitions: [ActivityTransition] = []- monitor.resolveTerminal(.completed(at: now, duration: 1), task: task, key: key, abortFallback: now, into: &transitions)+ monitor.graph.resolveTerminal(.completed(at: now, duration: 1), task: task, key: key, abortFallback: now, into: &transitions)let root = reference("thread-a", "turn-a")- #expect(monitor.terminalTokenUsageReferences().contains(root))- #expect(monitor.terminalTokenUsageReferences().contains(root))- #expect(monitor.applyTerminalTokenUsage([state(root, input: 500)]))- #expect(monitor.completions.first?.tokenUsage?.totalTokens == 510)+ #expect(monitor.graph.terminalTokenUsageReferences().contains(root))+ #expect(monitor.graph.terminalTokenUsageReferences().contains(root))+ monitor.graph.applyTerminalTokenUsage([state(root, input: 500)])+ #expect(monitor.graph.completions.first?.tokenUsage?.totalTokens == 510)if clearExplicitly {monitor.clearCollectedActivityState()} else {monitor.refreshSnapshot(now: now.addingTimeInterval(601))}- #expect(monitor.completions.isEmpty)- #expect(monitor.terminalTokenUsageRequests.isEmpty)- #expect(monitor.terminalTokenUsageReferences().isEmpty)+ #expect(monitor.graph.completions.isEmpty)+ #expect(monitor.graph.terminalTokenUsageRequests.isEmpty)+ #expect(monitor.graph.terminalTokenUsageReferences().isEmpty)}private func reference(_ thread: String, _ turn: String) -> ActivityTurnReference {ActivityTurnReference(threadID: thread, turnID: turn)}- private func request(root: ActivityTurnReference, children: [ActivityTurnReference]) -> TaskTokenRequest {- TaskTokenRequest(- root: root, expectedAgentIDs: Set(children.map(\.threadID)), references: Set([root] + children)+ private func request(root: ActivityTurnReference, children: [ActivityTurnReference]) -> TaskTokenUsageRequest {+ TaskTokenUsageRequest(+ root: root, expectedAgentThreadIDs: Set(children.map(\.threadID)), references: Set([root] + children))}private func state(_ reference: ActivityTurnReference, input: Int64 = 100,- terminal: SessionTerminalState? = .completed(at: TestFixtures.now, duration: 1)- ) -> SessionLifecycleState {- SessionLifecycleState(+ terminal: TurnTerminalState? = .completed(at: TestFixtures.now, duration: 1)+ ) -> TurnLifecycleState {+ TurnLifecycleState(requestedThreadID: reference.threadID, turnID: reference.turnID, startedAt: TestFixtures.now,effort: nil, lastProgressAt: TestFixtures.now, terminal: terminal,rootTurnID: "turn-a", rootThreadID: "thread-a",@@ -442,7 +468,7 @@ extension ActivityTokenUsageTests {progressGeneration: 1)var transitions: [ActivityTransition] = []- monitor.resolveTerminal(+ monitor.graph.resolveTerminal(.completed(at: nil, duration: nil),task: task,key: key,@@ -450,15 +476,15 @@ extension ActivityTokenUsageTests {publishesEvents: false,into: &transitions)- monitor.resolveTerminal(+ monitor.graph.resolveTerminal(.completed(at: now.addingTimeInterval(-10), duration: 10),task: task,key: key,abortFallback: now,into: &transitions)- #expect(monitor.completions.count == 1)- #expect(monitor.completions.first?.duration == nil)+ #expect(monitor.graph.completions.count == 1)+ #expect(monitor.graph.completions.first?.duration == nil)#expect(transitions.isEmpty)}}diff --git a/CodexBarTests/Activity/ActivityUpdateTests.swift b/CodexBarTests/Activity/ActivityUpdateTests.swiftnew file mode 100644index 0000000..d21f053--- /dev/null+++ b/CodexBarTests/Activity/ActivityUpdateTests.swift@@ -0,0 +1,317 @@+import Combine+import Foundation+import Synchronization+import Testing++struct ActivityUpdateTests {+ @Test(arguments: ["resolved", "pending", "replaced", "unknown", "ended"], [false, true])+ func approvalPublicationUsesFinalState(scenario: String, deferred: Bool) throws {+ let now = Date(timeIntervalSince1970: floor(Date().timeIntervalSince1970))+ var reducer = try approvalReducer(at: now)+ var graph = ActivityTaskGraph()+ _ = graph.update(+ events: [TestFixtures.event(at: now.addingTimeInterval(-60))],+ source: .bootstrap,+ lifecycle: lifecycle(reducer, at: now, verifiesChild: !deferred)+ )+ let request = try approvalRequest(id: "first", at: now.addingTimeInterval(-2))+ let events = reducer.consume(request, now: now.addingTimeInterval(-2))+ if deferred {+ let transitions = graph.update(events: events, source: .live, lifecycle: lifecycle(reducer, at: now, verifiesChild: false))+ #expect(transitions.isEmpty)+ #expect(graph.pendingSubagentEvents.count == 1)+ }+ let child = ActivityTurnReference(threadID: "child", turnID: "child-turn")+ if scenario == "resolved" || scenario == "replaced" {+ let resolved = try TestFixtures.decode(ActivityInput.self, #"{"method":"serverRequest/resolved","params":{"threadId":"child","requestId":"first"}}"#)+ #expect(reducer.consume(resolved, now: now.addingTimeInterval(-1)).isEmpty)+ }+ if scenario == "replaced" {+ _ = try reducer.consume(approvalRequest(id: "second", at: now.addingTimeInterval(-1)), now: now.addingTimeInterval(-1))+ } else if scenario == "unknown" {+ reducer.states[child]?.isWaitingApproval = nil+ reducer.states[child]?.approvalChangedAt = nil+ reducer.states[child]?.pendingApprovals.removeAll()+ } else if scenario == "ended" {+ let ended = try TestFixtures.decode(ActivityInput.self, """+ {"method":"turn/completed","params":{"threadId":"child","turn":{"id":"child-turn","rootTurnId":"turn-a","status":"completed","completedAt":\(now.timeIntervalSince1970)}}}+ """)+ _ = reducer.consume(ended, now: now)+ }+ let snapshot = lifecycle(reducer, at: now)+ let transitions = graph.update(events: deferred ? [] : events, source: deferred ? .bootstrap : .live, lifecycle: snapshot)+ let task = try #require(graph.tasks[.init(thread: "thread-a", turn: "turn-a")])+ let expectsWaiting = ["pending", "replaced", "unknown"].contains(scenario)+ #expect(graph.pendingSubagentEvents.isEmpty)+ #expect((task.state == .waitingApproval) == expectsWaiting)+ #expect(transitions.count == (expectsWaiting ? 1 : 0))+ if expectsWaiting {+ guard case let .waitingApproval(published) = try #require(transitions.first) else {+ Issue.record("Expected waiting transition")+ return+ }+ #expect(published == task.snapshot)+ #expect(published.stateChangedAt == now.addingTimeInterval(scenario == "replaced" ? -1 : -2))+ }+ let repeated = graph.update(events: [], source: .bootstrap, lifecycle: snapshot)+ #expect(repeated.isEmpty)+ #expect(graph.tasks[task.key]?.snapshot == task.snapshot)+ #expect(graph.tasks[task.key]?.progressGeneration == task.progressGeneration)+ }++ @Test(arguments: ["complete", "unknown", "unavailable"])+ func otherExecutionProgressCannotClearWaitingApproval(coverage: String) throws {+ let now = Date(timeIntervalSince1970: floor(Date().timeIntervalSince1970))+ var reducer = try approvalReducer(at: now)+ var graph = ActivityTaskGraph()+ _ = graph.update(events: [TestFixtures.event(at: now.addingTimeInterval(-60))], source: .bootstrap, lifecycle: lifecycle(reducer, at: now))+ let events = try reducer.consume(approvalRequest(id: "first", at: now.addingTimeInterval(-2)), now: now.addingTimeInterval(-2))+ let transitions = graph.update(events: events, source: .live, lifecycle: lifecycle(reducer, at: now))+ #expect(transitions.count == 1)+ let key = ActivityTaskKey(thread: "thread-a", turn: "turn-a")+ let before = try #require(graph.tasks[key]?.snapshot)+ let child = ActivityTurnReference(threadID: "child", turnID: "child-turn")+ if coverage == "unknown" {+ reducer.states[child]?.isWaitingApproval = nil+ reducer.states[child]?.approvalChangedAt = nil+ reducer.states[child]?.pendingApprovals.removeAll()+ }+ let progress = [+ TestFixtures.event(.toolCompleted, at: now),+ TestFixtures.event(.toolCompleted, at: now, turn: "child-turn", agentThreadID: "child", origin: .auxiliary)+ ]+ let updated = graph.update(events: progress, source: .live, lifecycle: lifecycle(reducer, at: now, verifiesChild: coverage != "unavailable"))+ #expect(updated.isEmpty)+ #expect(graph.tasks[key]?.state == .waitingApproval)+ #expect(graph.tasks[key]?.snapshot.stateChangedAt == before.stateChangedAt)+ #expect(graph.tasks[key]?.snapshot.approvalActionText == before.approvalActionText)+ }++ @Test func resolvingOneChildKeepsAnotherChildWaiting() throws {+ let now = Date(timeIntervalSince1970: floor(Date().timeIntervalSince1970))+ var reducer = try approvalReducer(at: now)+ let sibling = try TestFixtures.decode(ActivityThread.self, """+ {"id":"sibling","source":{"subAgent":{"thread_spawn":{"parent_thread_id":"thread-a"}}},"status":{"type":"active"}}+ """)+ var turn = ActivityTurn(id: "sibling-turn", status: .running, startedAt: now.addingTimeInterval(-10))+ turn.rootTurnId = "turn-a"+ _ = reducer.reconcile(thread: sibling, turns: [turn], now: now)+ var graph = ActivityTaskGraph()+ _ = graph.update(events: [TestFixtures.event(at: now.addingTimeInterval(-60))], source: .bootstrap, lifecycle: lifecycle(reducer, at: now))+ let first = try reducer.consume(approvalRequest(id: "first", at: now.addingTimeInterval(-2)), now: now.addingTimeInterval(-2))+ let second = try reducer.consume(approvalRequest(id: "second", at: now.addingTimeInterval(-1), thread: "sibling", turn: "sibling-turn"), now: now.addingTimeInterval(-1))+ #expect(graph.update(events: first + second, source: .live, lifecycle: lifecycle(reducer, at: now)).count == 1)+ let resolved = try TestFixtures.decode(ActivityInput.self, #"{"method":"serverRequest/resolved","params":{"threadId":"child","requestId":"first"}}"#)+ _ = reducer.consume(resolved, now: now)+ #expect(graph.update(events: [], source: .bootstrap, lifecycle: lifecycle(reducer, at: now)).isEmpty)+ let task = try #require(graph.tasks[.init(thread: "thread-a", turn: "turn-a")])+ #expect(task.state == .waitingApproval)+ #expect(task.displayedApproval?.requestedAt == now.addingTimeInterval(-1))+ #expect(task.executions[.init(agentThreadID: "child", turnID: "child-turn")]?.approval == nil)+ #expect(task.executions[.init(agentThreadID: "sibling", turnID: "sibling-turn")]?.approval != nil)+ }++ @Test func historicalApprovalReplayDoesNotNotify() throws {+ let now = Date(timeIntervalSince1970: floor(Date().timeIntervalSince1970))+ var reducer = try approvalReducer(at: now)+ var graph = ActivityTaskGraph()+ _ = graph.update(events: [TestFixtures.event(at: now.addingTimeInterval(-60))], source: .bootstrap, lifecycle: lifecycle(reducer, at: now, verifiesChild: false))+ let events = try reducer.consume(approvalRequest(id: "first", at: now.addingTimeInterval(-2)), now: now.addingTimeInterval(-2))+ #expect(graph.update(events: events, source: .bootstrap, lifecycle: lifecycle(reducer, at: now, verifiesChild: false)).isEmpty)+ #expect(graph.update(events: [], source: .bootstrap, lifecycle: lifecycle(reducer, at: now)).isEmpty)+ #expect(graph.tasks.values.first?.state == .waitingApproval)+ }++ @Test func monitorRejectsOlderRevisionsAndDifferentReaders() throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let preferences = try TestPreferences()+ defer { preferences.remove() }+ let monitor = ActivityMonitor(+ protectionSettings: ProtectionSettings(defaults: preferences.defaults),+ protectionStore: ProtectionStore(directoryURL: directory.url),+ activityDataDirectoryURL: directory.url+ )+ monitor.isActivitySourceHealthy = true+ let readerID = UUID()+ let now = Date()+ var first = ActivityTaskGraph()+ _ = first.consume([TestFixtures.event(at: now)], source: .live)+ var second = first+ _ = second.consume([TestFixtures.event(at: now.addingTimeInterval(1), turn: "next")], source: .live)+ monitor.consume(update(second, readerID: readerID, revision: 2))+ let expected = monitor.snapshot+ monitor.consume(update(first, readerID: readerID, revision: 1))+ monitor.consume(update(first, readerID: UUID(), revision: 3))+ monitor.consume(update(first, readerID: readerID, revision: 2))+ #expect(monitor.snapshot == expected)+ #expect(monitor.tasks.keys.map(\.turnID) == ["next"])+ }++ @Test func repeatedPublicationCannotRenewLifecycleVerification() throws {+ let now = Date()+ let reference = ActivityTurnReference(threadID: "thread-a", turnID: "turn-a")+ let state = TurnLifecycleState(+ requestedThreadID: reference.threadID,+ turnID: reference.turnID,+ startedAt: now,+ effort: nil,+ lastProgressAt: now,+ terminal: nil+ )+ let snapshot = TurnLifecycleSnapshot(states: [reference: state], verifiedTurns: [reference: now])+ var graph = ActivityTaskGraph()+ _ = graph.consume([TestFixtures.event(at: now)], source: .bootstrap)+ for _ in 0 ..< 3 {+ _ = graph.reconcile(snapshot.lifecycleStates(for: [reference], now: now.addingTimeInterval(4)))+ }+ let task = try #require(graph.tasks.values.first)+ #expect(task.lifecycleCoverageCheckedAt == now)+ #expect(!task.hasFreshLifecycle(at: now.addingTimeInterval(6)))+ }++ @Test func recoveryAppliesExplicitTerminalSilentlyAndKeepsProtectionPaused() throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let preferences = try TestPreferences()+ defer { preferences.remove() }+ let monitor = ActivityMonitor(+ protectionSettings: ProtectionSettings(defaults: preferences.defaults),+ protectionStore: ProtectionStore(directoryURL: directory.url),+ activityDataDirectoryURL: directory.url+ )+ monitor.isActivitySourceHealthy = true+ let readerID = UUID()+ let now = Date()+ var graph = ActivityTaskGraph()+ _ = graph.consume([TestFixtures.event(at: now)], source: .live)+ monitor.consume(update(graph, readerID: readerID, revision: 1))+ monitor.beginProtectionRecovery()+ var transitions = 0+ var terminalEvents = 0+ let notification = monitor.transitionPublisher.sink { _ in transitions += 1 }+ let presentation = monitor.presentationPublisher.sink { terminalEvents += $0.terminalEvents.count }+ defer { notification.cancel()+ presentation.cancel()+ }+ let changes = graph.consume([TestFixtures.event(.turnCompleted, at: now)], source: .live)+ var partial = update(graph, readerID: readerID, revision: 2, transitions: changes)+ partial.isComplete = false+ monitor.consume(partial)+ #expect(monitor.snapshot.activeCount == 0)+ #expect(monitor.snapshot.recentCompletions.count == 1)+ #expect(monitor.isProtectionRecoveryInProgress)+ #expect(transitions == 0 && terminalEvents == 0)+ }++ @Test func eachDrainWaitsForAReadStartedAfterItsOwnRequest() async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let release = DispatchSemaphore(value: 0)+ defer { release.signal() }+ let entered = Mutex(false)+ let now = Date()+ let server = try SharedServerFixture { peer in+ let initialize = try peer.readMessage()+ try peer.reply(to: initialize, result: ["userAgent": "codex/0.162.0"])+ _ = try peer.readMessage()+ var round = 0+ while let request = try? peer.readMessage() {+ switch request["method"] as? String {+ case "thread/loaded/list":+ round += 1+ if round == 2 {+ entered.withLock { $0 = true }+ _ = release.wait(timeout: .now() + 5)+ }+ try peer.reply(to: request, result: ["data": ["thread-a"]])+ case "thread/read", "thread/resume":+ try peer.reply(to: request, result: ["thread": ["id": "thread-a", "source": "cli", "status": ["type": "active"]]])+ case "thread/turns/list":+ let turns = (1 ... round).map { index -> [String: Any] in+ ["id": "turn-\(index)", "rootTurnId": "turn-\(index)", "status": index == round ? "inProgress" : "completed", "startedAt": now.timeIntervalSince1970]+ }+ try peer.reply(to: request, result: ["data": turns])+ default: Issue.record("Unexpected request")+ }+ }+ }+ defer { server.close() }+ var ready = false+ var published: [ActivityUpdate] = []+ let reader = AppServerActivityReader(+ socketURL: server.url,+ logStorage: nil,+ tokenUsageHistory: TokenUsageHistoryStore(directoryURL: directory.url),+ activityDataRecorder: ActivityDataRecorder(directoryURL: directory.url)+ ) { update in+ published.append(update)+ if case .bootstrapEnd = update.phase {+ ready = true+ }+ }+ await reader.start()+ for _ in 0 ..< 200 where !ready {+ try await Task.sleep(for: .milliseconds(5))+ }+ try #require(ready)+ let first = Task { await reader.drainNow() }+ for _ in 0 ..< 200 where !entered.withLock({ $0 }) {+ try await Task.sleep(for: .milliseconds(5))+ }+ try #require(entered.withLock { $0 })+ let second = Task { await reader.drainNow() }+ try await Task.sleep(for: .milliseconds(20))+ release.signal()+ let firstResult = await first.value+ let secondResult = await second.value+ #expect(firstResult.status == .completed)+ #expect(secondResult.status == .completed)+ if case let .completed(a) = firstResult, case let .completed(b) = secondResult {+ #expect(a.readerID == b.readerID && a.revision < b.revision)+ #expect(a.graph.tasks.keys.map(\.turnID) == ["turn-2"])+ #expect(b.graph.tasks.keys.map(\.turnID) == ["turn-3"])+ #expect(published.contains { $0.readerID == b.readerID && $0.revision == b.revision })+ }+ #expect(await reader.stop())+ #expect(await reader.drainNow().status == .cancelled)+ try server.finish()+ }++ private func approvalReducer(at now: Date) throws -> AppServerActivityReducer {+ var reducer = AppServerActivityReducer()+ let root = try TestFixtures.decode(ActivityThread.self, #"{"id":"thread-a","source":"cli","status":{"type":"active"}}"#)+ let child = try TestFixtures.decode(ActivityThread.self, """+ {"id":"child","source":{"subAgent":{"thread_spawn":{"parent_thread_id":"thread-a"}}},"status":{"type":"active"}}+ """)+ for (thread, id, offset) in [(root, "turn-a", -60.0), (child, "child-turn", -10.0)] {+ var turn = ActivityTurn(id: id, status: .running, startedAt: now.addingTimeInterval(offset))+ turn.rootTurnId = "turn-a"+ _ = reducer.reconcile(thread: thread, turns: [turn], now: now)+ }+ return reducer+ }++ private func approvalRequest(id: String, at date: Date, thread: String = "child", turn: String = "child-turn") throws -> ActivityInput {+ try TestFixtures.decode(ActivityInput.self, """+ {"id":"\(id)","method":"item/commandExecution/requestApproval","params":{"threadId":"\(thread)","turnId":"\(turn)","itemId":"\(id)","startedAtMs":\(date.timeIntervalSince1970 * 1000)}}+ """)+ }++ private func lifecycle(_ reducer: AppServerActivityReducer, at now: Date, verifiesChild: Bool = true) -> TurnLifecycleSnapshot {+ let verified = reducer.states.keys.filter { verifiesChild || $0.threadID == "thread-a" }.map { ($0, now) }+ return TurnLifecycleSnapshot(states: reducer.states, verifiedTurns: Dictionary(uniqueKeysWithValues: verified))+ }++ private func update(_ graph: ActivityTaskGraph, readerID: UUID, revision: UInt64, transitions: [ActivityTransition] = []) -> ActivityUpdate {+ ActivityUpdate(+ readerID: readerID,+ revision: revision,+ phase: .lifecycleChanged,+ graph: graph,+ transitions: transitions,+ terminalEvents: graph.terminalEvents,+ backfilledTasks: graph.backfilledTasks+ )+ }+}diff --git a/CodexBarTests/Activity/AppServerActivityTests.swift b/CodexBarTests/Activity/AppServerActivityTests.swiftindex 3c21633..e548eef 100644--- a/CodexBarTests/Activity/AppServerActivityTests.swift+++ b/CodexBarTests/Activity/AppServerActivityTests.swift@@ -56,9 +56,8 @@ struct AppServerActivityTests {_ = try reducer.consume(message("item/commandExecution/requestApproval", params: """{"threadId":"main","turnId":"turn","itemId":"command"}""", id: 1), now: now)- _ = try reducer.consume(tokenMessage(total: 100), now: now)- _ = try reducer.consume(tokenMessage(total: 110), now: now)- let tokens = reducer.tokenTurns+ reducer.applyTokenUsage(TokenUsageThreadSnapshot(threadID: "main", position: 1, turns: ["turn": TestFixtures.tokenUsage(10)]))+ let tokens = reducer.tokenUsageTurnslet later = now.addingTimeInterval(30)let events = try reducer.consume(message(method, params: """{"threadId":"main","turnId":"turn","itemId":"item","delta":"content"}@@ -68,7 +67,7 @@ struct AppServerActivityTests {#expect(state.isWaitingApproval == true)#expect(state.approvalChangedAt == now)#expect(events.isEmpty)- #expect(reducer.tokenTurns == tokens)+ #expect(reducer.tokenUsageTurns == tokens)#expect(state.tokenUsage?.totalTokens == 10)}@@ -87,7 +86,7 @@ struct AppServerActivityTests {#expect(AppServerActivityProtocol.kind(for: method).category == .ignored)#expect(state.lastProgressAt == now)#expect(events.isEmpty)- #expect(reducer.tokenTurns.isEmpty)+ #expect(reducer.tokenUsageTurns.isEmpty)}@Test(arguments: ["agentMessage", "reasoning", "userMessage", "plan", "subAgentActivity"])@@ -124,20 +123,20 @@ struct AppServerActivityTests {#expect(ended.first?.name == "toolCompleted")#expect(ended.first?.id == "main:turn:item:toolCompleted")#expect(try reducer.consume(message("item/completed", params: params), now: now).isEmpty)- var accumulator = ActivityAccumulator(rebuilding: CodexDateFormat.dayString(from: now), generationID: nil, eventCountAvailability: .all)+ var accumulator = ActivityDataAccumulator(rebuilding: CodexDateFormat.dayString(from: now))for event in started + ended {accumulator.record(event)}#expect(accumulator.finalized(identifierStorage: .retained).metrics.toolCallCount == 1)let event = try #require(started.first)- let data = try AppServerEventRecord(activity: event, recordedAt: now).jsonLineData()+ let data = try AppServerEventRecord(activityData: event, recordedAt: now).jsonLineData()let object = try #require(JSONSerialization.jsonObject(with: data) as? [String: Any])- let activity = try #require(object["activityPayload"] as? [String: Any])+ let activity = try #require(object["activityData"] as? [String: Any])#expect(activity["toolName"] as? String == expectedTool)#expect(activity["tool"] == nil)#expect(activity["commandActionTypes"] == nil)#expect((object["context"] as? [String: Any])?["itemType"] as? String == type)- let restored = try #require(AppServerEventRecord.decode(from: data).activity)+ let restored = try #require(AppServerEventRecord.decode(from: data).activityData)#expect(restored.toolName == expectedTool)#expect(restored.context?.itemType == type)}@@ -154,16 +153,16 @@ struct AppServerActivityTests {]}}"""), now: now)let event = try #require(events.first)- let data = try AppServerEventRecord(activity: event, recordedAt: now).jsonLineData()+ let data = try AppServerEventRecord(activityData: event, recordedAt: now).jsonLineData()let storedText = try #require(String(data: data, encoding: .utf8))#expect(!storedText.contains("private"))let object = try #require(JSONSerialization.jsonObject(with: data) as? [String: Any])- let payload = try #require(object["activityPayload"] as? [String: Any])+ let payload = try #require(object["activityData"] as? [String: Any])#expect(payload["commandActionTypes"] as? [String] == ["search", "unknown", "read", "search", "listFiles", "read"])#expect(payload["commandActions"] == nil)#expect(payload["toolName"] == nil)#expect(payload["tool"] == nil)- let restored = try #require(AppServerEventRecord.decode(from: data).activity)+ let restored = try #require(AppServerEventRecord.decode(from: data).activityData)let approval = ActivityApproval(requestedAt: now, toolName: restored.toolName, sequence: 0,itemType: restored.context?.itemType, commandActionTypes: restored.commandActionTypes@@ -171,7 +170,7 @@ struct AppServerActivityTests {#expect(restored.toolName == nil)#expect(restored.commandActionTypes == ["search", "unknown", "read", "search", "listFiles", "read"])#expect(approval.commandActionTypes == restored.commandActionTypes)- let expectedDisplay = String(localized: "activity.live.actions-read-listFiles-search")+ let expectedDisplay = String(localized: "activity.live.actions-read-listfiles-search")#expect(approval.actionText == expectedDisplay)#expect(restored.context?.itemType == "commandExecution")#expect(restored.eventKind == (method == "item/started" ? .toolStarted : .toolCompleted))@@ -201,7 +200,7 @@ struct AppServerActivityTests {let event = try #require(events.first)#expect(event.toolName == name)#expect(event.commandActionTypes == nil)- let restored = try #require(AppServerEventRecord.decode(from: AppServerEventRecord(activity: event).jsonLineData()).activity)+ let restored = try #require(AppServerEventRecord.decode(from: AppServerEventRecord(activityData: event).jsonLineData()).activityData)let approval = ActivityApproval(requestedAt: now, toolName: restored.toolName, sequence: 0,itemType: restored.context?.itemType, commandActionTypes: restored.commandActionTypes@@ -223,7 +222,7 @@ struct AppServerActivityTests {{"threadId":"main","turnId":"turn","itemId":"command","commandActions":[{"type":"read"},{"type":"search"}]}""", id: 1), now: now.addingTimeInterval(1))let approval = try #require(approvals.first)- let restored = try #require(AppServerEventRecord.decode(from: AppServerEventRecord(activity: approval).jsonLineData()).activity)+ let restored = try #require(AppServerEventRecord.decode(from: AppServerEventRecord(activityData: approval).jsonLineData()).activityData)let enteredWaiting = task.recordApprovalRequest(from: restored)#expect(enteredWaiting)let others = try reducer.consume(message("item/started", params: """@@ -233,7 +232,7 @@ struct AppServerActivityTests {try task.resumeExecution(from: #require(others.first))#expect(task.state == .waitingApproval)let lifecycle = try #require(reducer.states[ActivityTurnReference(threadID: "main", turnID: "turn")])- task.mergeExecutionLifecycle(lifecycle, owner: ActivityExecutionKey(agentID: nil, turnID: "turn"))+ task.mergeExecutionLifecycle(lifecycle, owner: ActivityExecutionKey(agentThreadID: nil, turnID: "turn"))#expect(task.displayedApproval?.toolName == nil)#expect(task.displayedApproval?.commandActionTypes == ["read", "search"])#expect(task.snapshot.approvalActionText == String(localized: "activity.live.actions-read-search"))@@ -305,43 +304,19 @@ struct AppServerActivityTests {#expect(agent.first?.toolName == nil)}- private func tokenMessage(total: Int) throws -> ActivityInput {+ private func usageMessage(_ total: Int, last: Int = 1, thread: String = "main", turn: String = "turn") throws -> ActivityInput {try message("thread/tokenUsage/updated", params: """- {"threadId":"main","turnId":"turn","tokenUsage":{+ {"threadId":"\(thread)","turnId":"\(turn)","tokenUsage":{"total":{"inputTokens":\(total),"cachedInputTokens":0,"outputTokens":0,"reasoningOutputTokens":0,"totalTokens":\(total)},- "last":{"inputTokens":10,"cachedInputTokens":0,"outputTokens":0,"reasoningOutputTokens":0,"totalTokens":10}}}+ "last":{"inputTokens":\(last),"cachedInputTokens":0,"outputTokens":0,"reasoningOutputTokens":0,"totalTokens":\(last)}}}""")}- @Test func tokenTotalsUseDeltasAndReconnectDiscardsOfflineGap() throws {- var reducer = try prepared()- _ = try reducer.consume(tokenMessage(total: 1000), now: now)- #expect(reducer.tokenTurns.isEmpty)- _ = try reducer.consume(tokenMessage(total: 1100), now: now)- #expect(reducer.tokenTurns.values.first?.usage?.totalTokens == 100)- _ = try reducer.consume(tokenMessage(total: 1100), now: now)- #expect(reducer.tokenTurns.values.first?.usage?.totalTokens == 100)- reducer.reconnect()- _ = try reducer.consume(tokenMessage(total: 5000), now: now)- _ = try reducer.consume(tokenMessage(total: 5020), now: now)- #expect(reducer.tokenTurns.values.first?.usage?.totalTokens == 120)- }-- @Test func newThreadIncludesFirstResponseAndCounterResetDoesNotSubtractUsage() throws {- var reducer = try prepared()- reducer.markNewThread("main")- _ = try reducer.consume(tokenMessage(total: 100), now: now)- #expect(reducer.tokenTurns.values.first?.usage?.totalTokens == 100)- _ = try reducer.consume(tokenMessage(total: 20), now: now)- _ = try reducer.consume(tokenMessage(total: 30), now: now)- #expect(reducer.tokenTurns.values.first?.usage?.totalTokens == 110)- }-@Test func eventRecorderDeduplicatesAcrossInstancesAndRestarts() async throws {let directory = try TestDirectory()defer { try? directory.remove() }- let first = ActivityRecorder(directoryURL: directory.url)- let second = ActivityRecorder(directoryURL: directory.url)+ let first = ActivityDataRecorder(directoryURL: directory.url)+ let second = ActivityDataRecorder(directoryURL: directory.url)var reducer = try prepared()let events = try reducer.consume(message("item/started", params: """{"threadId":"main","turnId":"turn","item":{"id":"command","type":"commandExecution"}}@@ -350,10 +325,10 @@ struct AppServerActivityTests {try await first.record(event: event)try await second.record(event: event)try await first.record(event: event)- let url = directory.url.appendingPathComponent("Events/\(CodexDateFormat.dayString(from: now)).jsonl")- var stored: [ActivityRecord] = []- try AppServerEventJournal.read(at: url) {- if let event = $0.activity {+ var stored: [ActivityDataRecord] = []+ try HistoryDatabase(directoryURL: directory.url).entries { entry in+ let record = entry.record+ if let event = record.activityData {stored.append(event)}}@@ -363,8 +338,8 @@ struct AppServerActivityTests {@Test func incrementalTokenStorageRetainsAllCountersAcrossRestart() async throws {let directory = try TestDirectory()defer { try? directory.remove() }- let store = TokenHistoryStore(directoryURL: directory.url)- let usage = TokenUsage(+ let store = TokenUsageHistoryStore(directoryURL: directory.url)+ let tokenUsage = TokenUsage(inputTokens: 100,cachedInputTokens: 50,cacheWriteInputTokens: 10,@@ -372,34 +347,33 @@ struct AppServerActivityTests {reasoningOutputTokens: 5,totalTokens: 120)- let observation = TokenObservation(- turn: TokenTurn(id: "one", rootID: "one", startedAt: now, updatedAt: now),- rootStartedAt: now, streamID: "stream", sequence: 1, previous: .zero, current: usage+ let observation = TestFixtures.tokenUsageObservation(+ id: "stream", at: now, tokenUsage: tokenUsage)_ = try await store.recordObservations([observation, observation], now: now)- let restarted = TokenHistoryStore(directoryURL: directory.url)+ let restarted = TokenUsageHistoryStore(directoryURL: directory.url)let records = try await restarted.refresh(now: now)#expect(records.count == 1)- #expect(records.first?.usage == usage)+ #expect(records.first?.tokenUsage == tokenUsage)}- @Test func tokenWriterLeaseTransfersWithoutTwoWriters() async throws {+ @Test func tokenWriterLeaseTransfersWithoutTwoWriters() throws {let directory = try TestDirectory()defer { try? directory.remove() }- let first = TokenHistoryStore(directoryURL: directory.url)- let second = TokenHistoryStore(directoryURL: directory.url)- #expect(try await first.acquireRecordingLease())- #expect(try await !second.acquireRecordingLease())- await first.releaseRecordingLease()- #expect(try await second.acquireRecordingLease())- await second.releaseRecordingLease()+ let first = TokenUsageHistoryStore(directoryURL: directory.url)+ let second = TokenUsageHistoryStore(directoryURL: directory.url)+ #expect(try first.acquireRecordingLease())+ #expect(try !second.acquireRecordingLease())+ first.releaseRecordingLease()+ #expect(try second.acquireRecordingLease())+ second.releaseRecordingLease()}@Test func rebuildUsesObservedRecordsAndContinuesCollecting() async throws {let directory = try TestDirectory()defer { try? directory.remove() }- let store = TokenHistoryStore(directoryURL: directory.url)- let usage = TokenUsage(+ let store = TokenUsageHistoryStore(directoryURL: directory.url)+ let tokenUsage = TokenUsage(inputTokens: 100,cachedInputTokens: 30,cacheWriteInputTokens: 10,@@ -407,24 +381,17 @@ struct AppServerActivityTests {reasoningOutputTokens: 5,totalTokens: 120)- let record = TokenTurn(id: "one", rootID: "one", startedAt: now, updatedAt: now)- let first = TokenObservation(turn: record, rootStartedAt: now, streamID: "stream", sequence: 1, previous: .zero, current: usage)+ let first = TestFixtures.tokenUsageObservation(id: "stream", at: now, tokenUsage: tokenUsage)_ = try await store.recordObservations([first], now: now)- let rebuilt = try await store.rebuild(for: [CodexDateFormat.dayString(from: now)], now: now.addingTimeInterval(1))- #expect(rebuilt.turnCount == 1)- let second = try TokenObservation(- turn: record,- rootStartedAt: now,- streamID: "stream",- sequence: 2,- previous: usage,- current: #require(usage.adding(usage))+ let rebuilt = try await store.rebuild(for: [CodexDateFormat.dayString(from: now)])+ #expect(rebuilt.eventCount == 1)+ let second = TestFixtures.tokenUsageObservation(+ id: "second", at: now,+ tokenUsage: tokenUsage)_ = try await store.recordObservations([second], now: now.addingTimeInterval(2))let records = try await store.refresh(now: now.addingTimeInterval(2))- #expect(records.first?.usage == usage.adding(usage))- #expect(records.first?.rebuiltAt == now.addingTimeInterval(1))- #expect(records.first?.generationID != "initial")+ #expect(records.first?.tokenUsage == tokenUsage.adding(tokenUsage))}@Test func multipleApprovalRequestsResolveIndependently() throws {@@ -432,7 +399,7 @@ struct AppServerActivityTests {let event = TestFixtures.event(at: now, thread: "main", turn: "turn")var task = ActivityTask(displayID: UUID(), key: .init(thread: "main", turn: "turn"), event: event, state: .running, startedAt: now, progressGeneration: 0)let reference = ActivityTurnReference(threadID: "main", turnID: "turn")- let owner = ActivityExecutionKey(agentID: nil, turnID: "turn")+ let owner = ActivityExecutionKey(agentThreadID: nil, turnID: "turn")for id in [1, 2] {let action = id == 1 ? "read" : "search"_ = try reducer.consume(message("item/commandExecution/requestApproval", params: """@@ -507,21 +474,17 @@ struct AppServerActivityTests {childTurn.rootTurnId = "turn"_ = reducer.reconcile(thread: child, turns: [childTurn], now: now)#expect(reducer.missingRootTurns()["middle"] == ["turn"])- reducer.markNewThread("child")- _ = try reducer.consume(message("thread/tokenUsage/updated", params: """- {"threadId":"child","turnId":"child-turn","tokenUsage":{- "total":{"inputTokens":15,"cachedInputTokens":0,"outputTokens":0,"reasoningOutputTokens":0,"totalTokens":15},- "last":{"inputTokens":15,"cachedInputTokens":0,"outputTokens":0,"reasoningOutputTokens":0,"totalTokens":15}}}- """), now: now)- #expect(reducer.takeTokenObservations().isEmpty)+ _ = try reducer.consume(usageMessage(15, thread: "child", turn: "child-turn"), now: now)+ let pending = try #require(reducer.takeTokenUsageSamples().first)+ #expect(pending.current.total.totalTokens == 15)+ reducer.applyTokenUsage(TokenUsageThreadSnapshot(threadID: "child", position: 1, turns: ["child-turn": TestFixtures.tokenUsage(15)]))_ = try reducer.reconcile(thread: thread(), turns: [turn()], now: now)let state = reducer.states[.init(threadID: "child", turnID: childTurn.id)]#expect(state?.rootThreadID == "main")- let observation = try #require(reducer.takeTokenObservations().first)- #expect(observation.turn.rootID == TokenTurn.identifier(thread: "main", turn: "turn"))- #expect(observation.current.totalTokens == 15)+ #expect(reducer.takeTokenUsageSamples().isEmpty)+ #expect(state?.tokenUsage?.totalTokens == 15)_ = try reducer.reconcile(thread: thread(), turns: [turn()], now: now)- #expect(reducer.takeTokenObservations().isEmpty)+ #expect(reducer.takeTokenUsageSamples().isEmpty)}@Test func creationMetadataUsesChildModelWithoutInventingChildTurn() throws {@@ -535,7 +498,7 @@ struct AppServerActivityTests {#expect(event.effort == "low")#expect(event.turnID == nil)#expect(event.context?.turnID == "turn")- var accumulator = ActivityAccumulator(rebuilding: "2026-10-09", generationID: nil, eventCountAvailability: .all)+ var accumulator = ActivityDataAccumulator(rebuilding: "2026-10-09")accumulator.record(event)#expect(accumulator.finalized(identifierStorage: .retained).modelCounts == ["child-model": 1])let parentEvent = try #require(reducer.reconcile(thread: thread(), turns: [turn()], now: now, bootstrap: true).first)@@ -567,7 +530,7 @@ struct AppServerActivityTests {var childTurn = turn("child-turn")childTurn.rootTurnId = "turn"_ = reducer.reconcile(thread: child, turns: [childTurn], now: now)- #expect(reducer.missingSubagentContexts(in: "main").isEmpty)+ #expect(reducer.missingSubagentParents().isEmpty)#expect(reducer.states[.init(threadID: "child", turnID: "child-turn")]?.effort == "low")}@@ -578,12 +541,12 @@ struct AppServerActivityTests {var childTurn = turn("child-turn")childTurn.rootTurnId = "turn"_ = reducer.reconcile(thread: child, turns: [childTurn], now: now)- #expect(reducer.missingSubagentContexts(in: "main") == ["child"])+ #expect(reducer.missingSubagentParents() == ["main"])var parentTurn = turn()parentTurn.items = [ActivityItem(id: "spawn", type: .subAgentActivity, agentThreadId: "child", kind: "started", model: "child-model", reasoningEffort: "low")]let events = try reducer.reconcile(thread: thread(), turns: [parentTurn], now: now)#expect(events.isEmpty)- #expect(reducer.missingSubagentContexts(in: "main").isEmpty)+ #expect(reducer.missingSubagentParents().isEmpty)#expect(reducer.states[.init(threadID: "child", turnID: "child-turn")]?.effort == "low")}@@ -605,24 +568,6 @@ struct AppServerActivityTests {#expect(command.first?.model == nil)#expect(command.first?.effort == nil)}-- @Test func missingFirstTurnUsageDoesNotMoveItsTokensIntoNextTurn() throws {- var reducer = try prepared()- reducer.markNewThread("main")- _ = try reducer.consume(message("turn/completed", params: """- {"threadId":"main","turn":{"id":"turn","status":"completed"}}- """), now: now)- _ = try reducer.consume(message("turn/started", params: """- {"threadId":"main","turn":{"id":"next","status":"inProgress"}}- """), now: now)- let raw = """- {"threadId":"main","turnId":"next","tokenUsage":{- "total":{"inputTokens":500,"cachedInputTokens":0,"outputTokens":0,"reasoningOutputTokens":0,"totalTokens":500},- "last":{"inputTokens":100,"cachedInputTokens":0,"outputTokens":0,"reasoningOutputTokens":0,"totalTokens":100}}}- """- _ = try reducer.consume(message("thread/tokenUsage/updated", params: raw), now: now)- #expect(reducer.tokenTurns.isEmpty)- }}extension AppServerActivityTests {@@ -654,54 +599,6 @@ extension AppServerActivityTests {}extension AppServerActivityTests {- @Test func refreshedHistoryReplaysOnlyUncommittedObservationsWithoutLosingLiveUsage() throws {- var reducer = try prepared()- reducer.markNewThread("main")- _ = try reducer.consume(tokenMessage(total: 100), now: now)- _ = reducer.takeTokenObservations()- let snapshot = Array(reducer.tokenTurns.values)- _ = try reducer.consume(tokenMessage(total: 120), now: now.addingTimeInterval(1))- _ = try reducer.consume(tokenMessage(total: 150), now: now.addingTimeInterval(2))- let pending = reducer.takeTokenObservations()- try reducer.acceptTokenTurns(snapshot, replaying: pending)- #expect(reducer.states.values.first?.tokenUsage?.totalTokens == 150)- let committed = Array(reducer.tokenTurns.values)- try reducer.acceptTokenTurns(committed, replaying: pending)- #expect(reducer.states.values.first?.tokenUsage?.totalTokens == 150)- _ = try reducer.consume(tokenMessage(total: 160), now: now.addingTimeInterval(3))- #expect(reducer.states.values.first?.tokenUsage?.totalTokens == 160)- }-- @Test func collectorContinuesFromCommittedCorrectionWithoutResettingServerCounter() async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let store = TokenHistoryStore(directoryURL: directory.url)- var reducer = try prepared()- reducer.markNewThread("main")- _ = try reducer.consume(tokenMessage(total: 100), now: now)- let committed = try await store.recordObservations(reducer.takeTokenObservations(), now: now)- try reducer.acceptTokenTurns(committed)- var correction = try #require(committed.first { $0.usage != nil })- correction.usage = TokenUsage(- inputTokens: 60,- cachedInputTokens: 0,- cacheWriteInputTokens: 0,- outputTokens: 0,- reasoningOutputTokens: 0,- totalTokens: 60- )- correction.startNewGeneration(at: now)- try await directory.seedTokenSnapshots([correction], now: now)- // 修正发生在采集器提交下一笔之前, 内存仍可能暂时持有旧累计值- _ = try reducer.consume(tokenMessage(total: 110), now: now.addingTimeInterval(1))- let result = try await store.recordObservations(reducer.takeTokenObservations(), now: now)- try reducer.acceptTokenTurns(result)- #expect(reducer.tokenTurns.values.first?.usage?.totalTokens == 70)- #expect(reducer.states.values.first?.tokenUsage?.totalTokens == 70)- _ = try reducer.consume(tokenMessage(total: 120), now: now.addingTimeInterval(2))- #expect(reducer.tokenTurns.values.first?.usage?.totalTokens == 80)- }-@Test func repeatedHistoricalTerminalKeepsObservationTimeAndNotificationPolicy() throws {var reducer = try prepared()let idle = ActivityThread(id: "main", status: ActivityThreadStatus(type: .idle))@@ -721,9 +618,9 @@ extension AppServerActivityTests {let directory = try TestDirectory()defer { try? directory.remove() }var reducer = try prepared()- let recorder = ActivityRecorder(directoryURL: directory.url)+ let activityDataRecorder = ActivityDataRecorder(directoryURL: directory.url)let date = Date()- var events: [ActivityRecord] = []+ var events: [ActivityDataRecord] = []for (index, callback) in [1, 2, 1].enumerated() {let started = date.timeIntervalSince1970 * 1000 + (hasApprovalID ? 0 : Double(callback))let approval = hasApprovalID ? ",\"approvalId\":\"callback-\(callback)\"" : ""@@ -735,12 +632,12 @@ extension AppServerActivityTests {#expect(events.count == 2)#expect(Set(events.compactMap(\.id)).count == 2)for event in events + events {- try await recorder.record(event: event)+ try await activityDataRecorder.record(event: event)}- let url = HistoryStorage.eventLogURL(for: HistoryStorage.dateKey(for: date), in: HistoryStorage.eventsDirectoryURL(in: directory.url))var count = 0- try AppServerEventJournal.read(at: url) {- if $0.activity?.eventKind == .approvalRequested {+ try HistoryDatabase(directoryURL: directory.url).entries { entry in+ let record = entry.record+ if record.activityData?.eventKind == .approvalRequested {count += 1}}@@ -759,43 +656,17 @@ extension AppServerActivityTests {#expect(reducer.states.count == 1)}- @Test func historyRetentionIsRecheckedOnNextDayAndAfterReplacingHistory() throws {+ @Test func transientTurnUsageIsReleasedWithExpiredLifecycle() throws {var reducer = try prepared()- let cutoff = HistoryStorage.retentionCutoffDate(today: now)- let retained = TokenTurn(id: "edge", rootID: "edge", updatedAt: cutoff)- try reducer.acceptTokenTurns([retained])- reducer.maintain(loadedThreads: ["main"], now: now)- #expect(reducer.tokenTurns.count == 1)- let expired = TokenTurn(id: "expired", rootID: "expired", updatedAt: cutoff.addingTimeInterval(-1))- try reducer.acceptTokenTurns([retained, expired])- reducer.maintain(loadedThreads: ["main"], now: now.addingTimeInterval(1))- #expect(Set(reducer.tokenTurns.keys) == ["edge"])- reducer.maintain(loadedThreads: ["main"], now: now.addingTimeInterval(86400))- #expect(reducer.tokenTurns.isEmpty)+ reducer.applyTokenUsage(TokenUsageThreadSnapshot(threadID: "main", position: 1, turns: ["turn": TestFixtures.tokenUsage(100)]))+ let completed = ActivityTurn(id: "turn", status: .completed, completedAt: now)+ _ = try reducer.reconcile(thread: thread(), turns: [completed], now: now)+ reducer.maintain(loadedThreads: [], now: now.addingTimeInterval(2 * 86400))+ #expect(reducer.tokenUsageTurns.isEmpty)}}extension AppServerActivityTests {- @Test func retiringThreadDoesNotReuseTokenSequenceOnReobservation() throws {- var reducer = try prepared()- _ = try reducer.consume(tokenMessage(total: 100), now: now)- _ = try reducer.consume(tokenMessage(total: 110), now: now)- let first = try #require(reducer.takeTokenObservations().first)- _ = try reducer.consume(message("turn/completed", params: """- {"threadId":"main","turn":{"id":"turn","status":"completed","completedAt":\(now.timeIntervalSince1970)}}- """), now: now)- let later = now.addingTimeInterval(2 * 86400)- reducer.maintain(loadedThreads: [], now: later)- #expect(reducer.threads.isEmpty)- _ = try reducer.reconcile(thread: thread(), turns: [turn()], now: later)- _ = try reducer.consume(tokenMessage(total: 500), now: later)- _ = try reducer.consume(tokenMessage(total: 520), now: later)- let second = try #require(reducer.takeTokenObservations().first)- #expect(second.sequence == 1)- #expect(second.streamID != first.streamID)- #expect(reducer.tokenTurns.values.first?.usage?.totalTokens == 30)- }-@Test func malformedApprovalCannotInventPersistentIdentity() throws {#expect(throws: DecodingError.self) {try TestFixtures.decode(ActivityInput.self, """@@ -805,29 +676,4 @@ extension AppServerActivityTests {}}-extension AppServerActivityTests {- @Test func historyRecordingGapStartsNewTokenStreamWithoutCountingMissingUsage() throws {- var reducer = try prepared()- _ = try reducer.consume(tokenMessage(total: 100), now: now)- _ = try reducer.consume(tokenMessage(total: 120), now: now)- let first = try #require(reducer.takeTokenObservations().first)- let persisted = try first.applying(to: nil)- reducer.setTokenRecordingEnabled(false)- _ = try reducer.consume(tokenMessage(total: 500), now: now)- #expect(reducer.takeTokenObservations().isEmpty)- let events = try reducer.consume(message("item/started", params: """- {"threadId":"main","turnId":"turn","item":{"id":"tool","type":"commandExecution"}}- """), now: now)- #expect(!events.isEmpty)- reducer.setTokenRecordingEnabled(true)- _ = try reducer.consume(tokenMessage(total: 600), now: now)- #expect(reducer.takeTokenObservations().isEmpty)- _ = try reducer.consume(tokenMessage(total: 650), now: now)- let resumed = try #require(reducer.takeTokenObservations().first)- #expect(resumed.streamID != first.streamID)- #expect(resumed.sequence == 1)- let combined = try resumed.applying(to: persisted)- #expect(combined.usage?.totalTokens == 70)- #expect(try resumed.applying(to: combined) == combined)- }-}+extension AppServerActivityTests {}diff --git a/CodexBarTests/Activity/ProtectionRecoveryTests.swift b/CodexBarTests/Activity/ProtectionRecoveryTests.swiftindex 3895f85..1b1f058 100644--- a/CodexBarTests/Activity/ProtectionRecoveryTests.swift+++ b/CodexBarTests/Activity/ProtectionRecoveryTests.swift@@ -47,13 +47,13 @@ struct ProtectionRecoveryTests {let monitor = ActivityMonitor(protectionSettings: ProtectionSettings(defaults: preferences.defaults),protectionStore: ProtectionStore(directoryURL: directory.url),- activityDirectoryURL: directory.url+ activityDataDirectoryURL: directory.url)// 注入未启动的 reader, 避免状态恢复测试连接真实 Codexmonitor.activityReader = AppServerActivityReader(- lifecycleCache: SessionLifecycleCache(), socketURL: directory.url.appendingPathComponent("absent.sock"),- tokenHistory: TokenHistoryStore(directoryURL: directory.url),- recorder: ActivityRecorder(directoryURL: directory.url)+ probe: ActivityReaderProbe(), socketURL: directory.url.appendingPathComponent("absent.sock"),+ tokenUsageHistory: TokenUsageHistoryStore(directoryURL: directory.url),+ activityDataRecorder: ActivityDataRecorder(directoryURL: directory.url)) { _ in }monitor.isBootstrapping = truereturn monitordiff --git a/CodexBarTests/Activity/ProtectionStoreTests.swift b/CodexBarTests/Activity/ProtectionStoreTests.swiftindex aa4db78..eb974b4 100644--- a/CodexBarTests/Activity/ProtectionStoreTests.swift+++ b/CodexBarTests/Activity/ProtectionStoreTests.swift@@ -39,7 +39,7 @@ struct ProtectionStoreTests {#expect(try await Set(first.load(now: TestFixtures.now).keys) == ["first", "second"])}- @Test(arguments: ["not-json", #"{"version":999,"records":[]}"#])+ @Test(arguments: ["not-json", #"{"schemaVersion":999,"records":[]}"#])func invalidStateIsPreservedAndRejectsWrites(_ content: String) async throws {let directory = try TestDirectory()defer { try? directory.remove() }@@ -57,10 +57,13 @@ struct ProtectionStoreTests {let older = makeRecord("task", markedAfter: 0)let newer = makeRecord("task", markedAfter: 5)let records = try #require(String(data: JSONEncoder().encode([newer, older]), encoding: .utf8))- let url = try directory.write("{\"version\":1,\"records\":\(records)}", to: "state.json")+ let url = try directory.write("{\"schemaVersion\":1,\"records\":\(records)}", to: "state.json")let store = ProtectionStore(directoryURL: directory.url)#expect(try await store.load(now: TestFixtures.now)["task"] == newer)try await store.apply(now: TestFixtures.now)+ let stored = try #require(JSONSerialization.jsonObject(with: Data(contentsOf: url)) as? [String: Any])+ #expect(Set(stored.keys) == ["schemaVersion", "records"])+ #expect(stored["schemaVersion"] as? Int == 1)let attributes = try FileManager.default.attributesOfItem(atPath: url.path)#expect((attributes[.posixPermissions] as? NSNumber)?.intValue == 0o600)}diff --git a/CodexBarTests/Activity/SessionLifecycleCacheTests.swift b/CodexBarTests/Activity/TurnLifecycleSnapshotTests.swiftsimilarity index 62%rename from CodexBarTests/Activity/SessionLifecycleCacheTests.swiftrename to CodexBarTests/Activity/TurnLifecycleSnapshotTests.swiftindex b4b0c67..0a923ba 100644--- a/CodexBarTests/Activity/SessionLifecycleCacheTests.swift+++ b/CodexBarTests/Activity/TurnLifecycleSnapshotTests.swift@@ -1,14 +1,13 @@import Foundationimport Testing-struct SessionLifecycleCacheTests {- @Test func unavailableChildInvalidatesAncestorsButNotOtherThreads() async {- let reader = SessionLifecycleCache()+struct TurnLifecycleSnapshotTests {+ @Test func unavailableChildInvalidatesAncestorsButNotOtherThreads() {let now = Date()- var states: [ActivityTurnReference: SessionLifecycleState] = [:]+ var states: [ActivityTurnReference: TurnLifecycleState] = [:]for id in ["root", "child", "grandchild", "other"] {let reference = ActivityTurnReference(threadID: id, turnID: "turn")- var state = SessionLifecycleState(+ var state = TurnLifecycleState(requestedThreadID: id, turnID: "turn", startedAt: now, effort: nil, lastProgressAt: now, terminal: nil)state.parentThreadID = id == "grandchild" ? "child" : id == "child" ? "root" : nil@@ -19,39 +18,37 @@ struct SessionLifecycleCacheTests {let verified = Dictionary(uniqueKeysWithValues: ["root", "child", "other"].map {(ActivityTurnReference(threadID: $0, turnID: "turn"), now)})- await reader.replace(states, verifiedTurns: verified)- let result = await reader.lifecycleStates(for: Array(states.keys), now: now)+ let snapshot = TurnLifecycleSnapshot(states: states, verifiedTurns: verified)+ let result = snapshot.lifecycleStates(for: Array(states.keys), now: now)#expect(result.first { $0.requestedThreadID == "other" }?.readStatus == .complete)#expect(result.filter { $0.requestedThreadID != "other" }.allSatisfy { $0.readStatus == .unavailable })}- @Test func disconnectAndCoverageExpiryInvalidateCachedLifecycle() async {- let reader = SessionLifecycleCache()+ @Test func missingVerificationAndCoverageExpiryInvalidateLifecycle() {let now = TestFixtures.nowlet reference = ActivityTurnReference(threadID: "thread", turnID: "turn")- let state = SessionLifecycleState(+ let state = TurnLifecycleState(requestedThreadID: "thread", turnID: "turn", startedAt: now, effort: "high", lastProgressAt: now, terminal: nil)- await reader.replace([reference: state], verifiedTurns: [reference: now])- #expect(await reader.lifecycleStates(for: [reference], now: now).first?.readStatus == .complete)- #expect(await reader.lifecycleStates(for: [reference], now: now.addingTimeInterval(16)).first?.readStatus == .unavailable)- await reader.invalidate()- #expect(await reader.lifecycleStates(for: [reference], now: now).first?.readStatus == .unavailable)- await reader.replace([:], verifiedTurns: [reference: now])- #expect(await reader.lifecycleStates(for: [reference], now: now).first?.readStatus == .notFound)- await reader.replace([:], verifiedTurns: [:])- #expect(await reader.lifecycleStates(for: [reference], now: now).first?.readStatus == .unavailable)+ let verified = TurnLifecycleSnapshot(states: [reference: state], verifiedTurns: [reference: now])+ #expect(verified.lifecycleStates(for: [reference], now: now).first?.readStatus == .complete)+ #expect(verified.lifecycleStates(for: [reference], now: now.addingTimeInterval(16)).first?.readStatus == .unavailable)+ let disconnected = TurnLifecycleSnapshot(states: [reference: state], verifiedTurns: [:])+ #expect(disconnected.lifecycleStates(for: [reference], now: now).first?.readStatus == .unavailable)+ let missing = TurnLifecycleSnapshot(states: [:], verifiedTurns: [reference: now])+ #expect(missing.lifecycleStates(for: [reference], now: now).first?.readStatus == .notFound)+ let unavailable = TurnLifecycleSnapshot(states: [:], verifiedTurns: [:])+ #expect(unavailable.lifecycleStates(for: [reference], now: now).first?.readStatus == .unavailable)}}-extension SessionLifecycleCacheTests {- @Test func lifecycleVerificationDoesNotRefreshOtherThreads() async {- let reader = SessionLifecycleCache()+extension TurnLifecycleSnapshotTests {+ @Test func lifecycleVerificationDoesNotRefreshOtherThreads() {let now = TestFixtures.nowfunc reference(_ id: String) -> ActivityTurnReference {.init(threadID: id, turnID: "turn")}- func state(_ id: String) -> SessionLifecycleState {+ func state(_ id: String) -> TurnLifecycleState {.init(requestedThreadID: id,turnID: "turn",@@ -61,20 +58,22 @@ extension SessionLifecycleCacheTests {terminal: nil)}- await reader.replace([reference("a"): state("a"), reference("b"): state("b")], verifiedTurns: [reference("a"): now, reference("b"): now.addingTimeInterval(-16)])- #expect(await reader.lifecycleStates(for: [reference("a")], now: now).first?.readStatus == .complete)- #expect(await reader.lifecycleStates(for: [reference("b")], now: now).first?.readStatus == .unavailable)+ let snapshot = TurnLifecycleSnapshot(+ states: [reference("a"): state("a"), reference("b"): state("b")],+ verifiedTurns: [reference("a"): now, reference("b"): now.addingTimeInterval(-16)]+ )+ #expect(snapshot.lifecycleStates(for: [reference("a")], now: now).first?.readStatus == .complete)+ #expect(snapshot.lifecycleStates(for: [reference("b")], now: now).first?.readStatus == .unavailable)}}-extension SessionLifecycleCacheTests {+extension TurnLifecycleSnapshotTests {@Test(arguments: [false, true])- func finishedChildCannotExpireRunningRoot(previousTurn: Bool) async {- let cache = SessionLifecycleCache()+ func finishedChildCannotExpireRunningRoot(previousTurn: Bool) {let now = TestFixtures.nowlet root = ActivityTurnReference(threadID: "root", turnID: "current")let child = ActivityTurnReference(threadID: "child", turnID: "child")- let rootState = SessionLifecycleState(+ let rootState = TurnLifecycleState(requestedThreadID: "root",turnID: "current",startedAt: now,@@ -82,7 +81,7 @@ extension SessionLifecycleCacheTests {lastProgressAt: now,terminal: nil)- let childState = SessionLifecycleState(+ let childState = TurnLifecycleState(requestedThreadID: "child",turnID: "child",startedAt: now,@@ -93,21 +92,20 @@ extension SessionLifecycleCacheTests {rootThreadID: "root",parentThreadID: "root")- await cache.replace([root: rootState, child: childState], verifiedTurns: [root: now])- let states = await cache.lifecycleStates(for: [root], now: now)+ let snapshot = TurnLifecycleSnapshot(states: [root: rootState, child: childState], verifiedTurns: [root: now])+ let states = snapshot.lifecycleStates(for: [root], now: now)#expect(states.first { $0.requestedThreadID == "root" }?.readStatus == .complete)#expect(states.allSatisfy { $0.readStatus == .complete })}- @Test func unavailablePreviousTurnDoesNotInvalidateCurrentTurn() async {- let cache = SessionLifecycleCache()+ @Test func unavailablePreviousTurnDoesNotInvalidateCurrentTurn() {let now = TestFixtures.nowfunc reference(_ thread: String, _ turn: String) -> ActivityTurnReference {.init(threadID: thread, turnID: turn)}- var states: [ActivityTurnReference: SessionLifecycleState] = [:]+ var states: [ActivityTurnReference: TurnLifecycleState] = [:]for turn in ["old", "current"] {- states[reference("root", turn)] = SessionLifecycleState(+ states[reference("root", turn)] = TurnLifecycleState(requestedThreadID: "root",turnID: turn,startedAt: now,@@ -116,7 +114,7 @@ extension SessionLifecycleCacheTests {terminal: nil)}- states[reference("child", "child")] = SessionLifecycleState(+ states[reference("child", "child")] = TurnLifecycleState(requestedThreadID: "child",turnID: "child",startedAt: now,@@ -127,23 +125,22 @@ extension SessionLifecycleCacheTests {rootThreadID: "root",parentThreadID: "root")- await cache.replace(states, verifiedTurns: [reference("root", "old"): now, reference("root", "current"): now])- let result = await cache.lifecycleStates(for: [reference("root", "old"), reference("root", "current")], now: now)+ let snapshot = TurnLifecycleSnapshot(states: states, verifiedTurns: [reference("root", "old"): now, reference("root", "current"): now])+ let result = snapshot.lifecycleStates(for: [reference("root", "old"), reference("root", "current")], now: now)#expect(result.first { $0.turnID == "old" }?.readStatus == .unavailable)#expect(result.first { $0.turnID == "current" }?.readStatus == .complete)- await cache.replace(states, verifiedTurns: Dictionary(uniqueKeysWithValues: states.keys.map { ($0, now) }))- #expect(await cache.lifecycleStates(for: Array(states.keys), now: now).allSatisfy { $0.readStatus == .complete })+ let verified = TurnLifecycleSnapshot(states: states, verifiedTurns: Dictionary(uniqueKeysWithValues: states.keys.map { ($0, now) }))+ #expect(verified.lifecycleStates(for: Array(states.keys), now: now).allSatisfy { $0.readStatus == .complete })}}-extension SessionLifecycleCacheTests {- @Test func verifyingNewTurnDoesNotRefreshOmittedTurnInSameThread() async {- let cache = SessionLifecycleCache()+extension TurnLifecycleSnapshotTests {+ @Test func verifyingNewTurnDoesNotRefreshOmittedTurnInSameThread() {let now = TestFixtures.nowlet old = ActivityTurnReference(threadID: "thread", turnID: "old")let current = ActivityTurnReference(threadID: "thread", turnID: "current")let states = Dictionary(uniqueKeysWithValues: [old, current].map {- ($0, SessionLifecycleState(+ ($0, TurnLifecycleState(requestedThreadID: $0.threadID,turnID: $0.turnID,startedAt: now,@@ -152,20 +149,20 @@ extension SessionLifecycleCacheTests {terminal: nil))})- await cache.replace(states, verifiedTurns: [current: now])- let result = await cache.lifecycleStates(for: [old, current], now: now)+ let snapshot = TurnLifecycleSnapshot(states: states, verifiedTurns: [current: now])+ let result = snapshot.lifecycleStates(for: [old, current], now: now)#expect(result.first { $0.turnID == "old" }?.readStatus == .unavailable)#expect(result.first { $0.turnID == "current" }?.readStatus == .complete)}}-extension SessionLifecycleCacheTests {+extension TurnLifecycleSnapshotTests {@Test func unresolvedChildrenRespectKnownRootIdentityAcrossNestedThreads() {let now = TestFixtures.nowlet a = ActivityTurnReference(threadID: "parent", turnID: "a")let b = ActivityTurnReference(threadID: "parent", turnID: "b")- func child(_ id: String, parent: String, root: String?) -> SessionLifecycleState {- SessionLifecycleState(+ func child(_ id: String, parent: String, root: String?) -> TurnLifecycleState {+ TurnLifecycleState(requestedThreadID: id, turnID: id, startedAt: now, effort: nil,lastProgressAt: now, terminal: nil, rootTurnID: root, parentThreadID: parent)@@ -177,7 +174,7 @@ extension SessionLifecycleCacheTests {child("unknown", parent: "parent", root: nil)]func ids(_ roots: Set<ActivityTurnReference>) -> Set<String> {- Set(SessionLifecycleState.subagentStates(for: roots, in: states).map(\.requestedThreadID))+ Set(TurnLifecycleState.subagentStates(for: roots, in: states).map(\.requestedThreadID))}#expect(ids([a]) == ["child-a", "grandchild-a", "unknown"])#expect(ids([b]) == ["child-b", "unknown"])diff --git a/CodexBarTests/Activity/TurnTokenCumulativeTests.swift b/CodexBarTests/Activity/TurnTokenCumulativeTests.swiftnew file mode 100644index 0000000..d378e3e--- /dev/null+++ b/CodexBarTests/Activity/TurnTokenCumulativeTests.swift@@ -0,0 +1,227 @@+import Foundation+import Testing++extension TestFixtures {+ static func tokenUsage(_ total: Int64) -> TokenUsage {+ TokenUsage(inputTokens: total, cachedInputTokens: 0, cacheWriteInputTokens: 0, outputTokens: 0, reasoningOutputTokens: 0, totalTokens: total)+ }++ static func tokenSample(_ total: Int64, thread: String = "main", turn: String = "root", at: Date = now, start: Date? = nil) -> TokenUsageSample {+ TokenUsageSample(observedAt: at, threadID: thread, turnID: turn, turnStartedAt: start, current: TokenUsageUpdate(total: tokenUsage(total), last: tokenUsage(1)))+ }+}++struct TurnTokenCumulativeTests {+ private let now = TestFixtures.now++ @Test func firstObservationIsPersistedAndRepeatedNotificationsCountEachFieldOnce() async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let store = TokenUsageHistoryStore(directoryURL: directory.url)+ let first = try await store.record(TestFixtures.tokenSample(1000))+ #expect(first.turns["root"] == nil)+ let total = TokenUsage(inputTokens: 1180, cachedInputTokens: 80, cacheWriteInputTokens: 10, outputTokens: 20, reasoningOutputTokens: 5, totalTokens: 1200)+ let sample = TokenUsageSample(+ observedAt: now,+ threadID: "main",+ turnID: "root",+ turnStartedAt: now,+ current: TokenUsageUpdate(total: total, last: TestFixtures.tokenUsage(200), modelContextWindow: 10000)+ )+ let recorded = try await store.record(sample)+ #expect(recorded.turns["root"] == TokenUsage(+ inputTokens: 180,+ cachedInputTokens: 80,+ cacheWriteInputTokens: 10,+ outputTokens: 20,+ reasoningOutputTokens: 5,+ totalTokens: 200+ ))+ #expect(try await store.record(sample).turns == recorded.turns)+ var repeated = sample+ repeated.id = UUID().uuidString+ #expect(try await store.record(repeated).position == recorded.position)+ let observations = try directory.historyRecords().compactMap(\.tokenUsage)+ #expect(observations.count == 2)+ #expect(observations.first?.previous == observations.first?.current.total)+ #expect(observations.last?.current.modelContextWindow == 10000)+ #expect(try await store.refresh(now: now).first?.tokenUsage.totalTokens == 200)+ }++ @Test func successiveRestartsIncludeOfflineDifferencesWithoutCountingFirstTotal() async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ for (totals, expected) in [([1000, 1500], 500), ([2200, 2500], 1500), ([3100, 3300], 2300)] {+ let store = TokenUsageHistoryStore(directoryURL: directory.url)+ for total in totals {+ _ = try await store.record(TestFixtures.tokenSample(Int64(total), start: now))+ }+ #expect(try await store.snapshot(threadID: "main").turns["root"]?.totalTokens == Int64(expected))+ #expect(try await store.refresh(now: now).first?.tokenUsage.totalTokens == Int64(expected))+ }+ let restarted = TokenUsageHistoryStore(directoryURL: directory.url)+ #expect(try await restarted.snapshot(threadID: "main").turns["root"]?.totalTokens == 2300)+ _ = try await restarted.rebuild(for: [HistoryStorage.dateKey(for: now)])+ #expect(try await restarted.persistedAggregates(now: now).first?.tokenUsage.totalTokens == 2300)+ }++ @Test func newTurnRetainsThreadBaselineAndLateTerminalUsageKeepsItsTurn() async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let store = TokenUsageHistoryStore(directoryURL: directory.url)+ _ = try await store.record(TestFixtures.tokenSample(1000))+ _ = try await store.record(TestFixtures.tokenSample(1200))+ _ = try await store.record(TestFixtures.tokenSample(1250, turn: "next"))+ let result = try await store.record(TestFixtures.tokenSample(1270, turn: "root"))+ #expect(result.turns["root"]?.totalTokens == 220)+ #expect(result.turns["next"]?.totalTokens == 50)+ #expect(try await store.refresh(now: now).first?.tokenUsage.totalTokens == 270)+ }++ @Test func childThreadsHaveIndependentBaselinesAndRestoreIntoResolvedRoot() async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let store = TokenUsageHistoryStore(directoryURL: directory.url)+ var reducer = AppServerActivityReducer()+ for (thread, turn, baseline, current) in [("main", "root", 100, 130), ("child", "child-turn", 500, 580), ("grandchild", "grand-turn", 700, 720)] {+ _ = try await store.record(TestFixtures.tokenSample(Int64(baseline), thread: thread, turn: turn))+ try await reducer.applyTokenUsage(store.record(TestFixtures.tokenSample(Int64(current), thread: thread, turn: turn)))+ _ = reducer.reconcile(+ thread: ActivityThread(id: thread, status: .init(type: .active), origin: .main, parentThreadID: thread == "main" ? nil : "main"),+ turns: [ActivityTurn(id: turn, status: .running, rootTurnId: "root", startedAt: now)],+ now: now+ )+ }+ let root = ActivityTurnReference(threadID: "main", turnID: "root")+ let request = TaskTokenUsageRequest(+ root: root, expectedAgentThreadIDs: ["child", "grandchild"],+ references: [root, .init(threadID: "child", turnID: "child-turn"), .init(threadID: "grandchild", turnID: "grand-turn")]+ )+ #expect(request.tokenUsage(from: Array(reducer.states.values), requiresFinalTokenUsage: false)?.totalTokens == 130)+ #expect(try await store.refresh(now: now).first?.tokenUsage.totalTokens == 130)+ }++ @Test func crossMidnightUsesKnownTurnStartAndUnknownStartUsesObservationDate() async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let store = TokenUsageHistoryStore(directoryURL: directory.url)+ let yesterday = Calendar.current.startOfDay(for: now).addingTimeInterval(-30)+ _ = try await store.record(TestFixtures.tokenSample(100, at: yesterday, start: yesterday))+ _ = try await store.record(TestFixtures.tokenSample(150, at: now, start: yesterday))+ _ = try await store.record(TestFixtures.tokenSample(180, turn: "unknown", at: now))+ let daily = try await TokenUsageAggregate.tokenUsageByDate(store.refresh(now: now), now: now)+ #expect(daily[HistoryStorage.dateKey(for: yesterday)]?.totalTokens == 50)+ #expect(daily[HistoryStorage.dateKey(for: now)]?.totalTokens == 30)+ }++ @Test func failedTransactionAndRetriedOldSampleNeverAdvanceOrRollbackBaseline() async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let store = TokenUsageHistoryStore(directoryURL: directory.url)+ let first = TestFixtures.tokenSample(100)+ _ = try await store.record(first)+ let database = try HistoryDatabase(directoryURL: directory.url)+ try database.execute("CREATE TRIGGER reject_sample BEFORE INSERT ON events BEGIN SELECT RAISE(ABORT, 'injected failure'); END")+ let sample = TestFixtures.tokenSample(150)+ await #expect(throws: (any Error).self) { try await store.record(sample) }+ #expect(try await store.snapshot(threadID: "main").turns["root"] == nil)+ try database.execute("DROP TRIGGER reject_sample")+ #expect(try await store.record(sample).turns["root"]?.totalTokens == 50)+ _ = try await store.record(TestFixtures.tokenSample(200))+ #expect(try await store.record(sample).turns["root"]?.totalTokens == 100)+ #expect(try await store.record(first).turns["root"]?.totalTokens == 100)+ #expect(try await store.record(TestFixtures.tokenSample(230)).turns["root"]?.totalTokens == 130)+ }++ @Test func contextWindowSentinelOnlyRebaselinesAndDoesNotPolluteFollowingIncrements() async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let store = TokenUsageHistoryStore(directoryURL: directory.url)+ _ = try await store.record(TestFixtures.tokenSample(100))+ _ = try await store.record(TestFixtures.tokenSample(120))+ for (input, total) in [(0, 1000), (20, 1020), (30, 1030)] {+ let usage = TokenUsage(+ inputTokens: Int64(input),+ cachedInputTokens: 0,+ cacheWriteInputTokens: 0,+ outputTokens: 0,+ reasoningOutputTokens: 0,+ totalTokens: Int64(total)+ )+ let sample = TokenUsageSample(+ observedAt: now, threadID: "main", turnID: "root", turnStartedAt: now, current: TokenUsageUpdate(total: usage, last: .zero)+ )+ _ = try await store.record(sample)+ }+ #expect(try await store.snapshot(threadID: "main").turns["root"]?.totalTokens == 50)+ #expect(try await store.refresh(now: now).first?.tokenUsage.totalTokens == 50)+ }++ @Test func explicitZeroObservationIsDifferentFromMissingChildUsage() async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let store = TokenUsageHistoryStore(directoryURL: directory.url)+ #expect(try await store.snapshot(threadID: "child").turns["child-turn"] == nil)+ #expect(try await store.record(TestFixtures.tokenSample(0, thread: "child", turn: "child-turn")).turns["child-turn"] == .zero)+ }++ @Test func initialBaselineStaysHiddenAfterRestoreAndFirstIncrementIsDisplayed() async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let store = TokenUsageHistoryStore(directoryURL: directory.url)+ let baseline = try await store.record(TestFixtures.tokenSample(10000))+ #expect(baseline.position > 0)+ #expect(baseline.turns["root"] == nil)++ let restarted = TokenUsageHistoryStore(directoryURL: directory.url)+ #expect(try await restarted.snapshot(threadID: "main").turns["root"] == nil)+ #expect(try await restarted.record(TestFixtures.tokenSample(10000)).turns["root"] == nil)+ let updated = try await restarted.record(TestFixtures.tokenSample(12000))+ #expect(updated.turns["root"]?.totalTokens == 2000)+ #expect(updated.position > baseline.position)+ #expect(try await store.snapshot(threadID: "main").turns == updated.turns)+ #expect(try await store.refresh(now: now).first?.tokenUsage.totalTokens == 2000)+ }++ @Test func knownThreadBaselineKeepsNewTurnZeroAndFirstIncrement() async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let store = TokenUsageHistoryStore(directoryURL: directory.url)+ _ = try await store.record(TestFixtures.tokenSample(10000))+ let nextTurn = try await store.record(TestFixtures.tokenSample(10000, turn: "next"))+ #expect(nextTurn.turns["root"] == nil)+ #expect(nextTurn.turns["next"] == .zero)+ #expect(try await store.record(TestFixtures.tokenSample(12000, turn: "next")).turns["next"]?.totalTokens == 2000)+ }++ @Test func recordingPauseAndReconnectPreserveCommittedBaseline() async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let store = TokenUsageHistoryStore(directoryURL: directory.url)+ var reducer = AppServerActivityReducer()+ _ = try await store.record(TestFixtures.tokenSample(20))+ reducer.threads["main"] = ActivityThread(id: "main", origin: .main)+ reducer.setTokenUsageRecordingEnabled(false)+ let input = try TestFixtures.decode(ActivityInput.self, """+ {"method":"thread/tokenUsage/updated","params":{"threadId":"main","turnId":"root","tokenUsage":{+ "total":{"inputTokens":500,"cachedInputTokens":0,"outputTokens":0,"reasoningOutputTokens":0,"totalTokens":500},+ "last":{"inputTokens":0,"cachedInputTokens":0,"outputTokens":0,"reasoningOutputTokens":0,"totalTokens":0}}}}+ """)+ _ = reducer.consume(input, now: now)+ #expect(reducer.takeTokenUsageSamples().isEmpty)+ reducer.reconnect()+ reducer.setTokenUsageRecordingEnabled(true)+ _ = reducer.consume(input, now: now)+ for sample in reducer.takeTokenUsageSamples() {+ try await reducer.applyTokenUsage(store.record(sample))+ }+ #expect(reducer.tokenUsageTurns[.init(threadID: "main", turnID: "root")]?.totalTokens == 480)+ }++ @Test func olderRestorationCannotOverwriteNewlyCommittedUsage() {+ var reducer = AppServerActivityReducer()+ reducer.applyTokenUsage(TokenUsageThreadSnapshot(threadID: "main", position: 2, turns: ["root": TestFixtures.tokenUsage(30)]))+ reducer.applyTokenUsage(TokenUsageThreadSnapshot(threadID: "main", position: 1, turns: ["root": TestFixtures.tokenUsage(10)]))+ #expect(reducer.tokenUsageTurns[.init(threadID: "main", turnID: "root")]?.totalTokens == 30)+ }+}diff --git a/CodexBarTests/AppServer/AppServerLogTests.swift b/CodexBarTests/AppServer/AppServerLogTests.swiftindex 2c94dee..0fb5a34 100644--- a/CodexBarTests/AppServer/AppServerLogTests.swift+++ b/CodexBarTests/AppServer/AppServerLogTests.swift@@ -1,9 +1,101 @@import Combineimport Foundation+import osimport SQLite3import Testingstruct AppServerLogTests {+ @Test(arguments: [false, true])+ func stoppingDuringFirstReadDiscardsResultsAndAllowsReopening(reopenImmediately: Bool) async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let writer = AppServerLogStore(directoryURL: directory.url)+ writer.recordFailure(message: "saved log")+ _ = try await writer.page()++ let reads = OSAllocatedUnfairLock(initialState: 0)+ let release = DispatchSemaphore(value: 0)+ defer { release.signal() }+ let storage = AppServerLogStore(directoryURL: directory.url, now: {+ let firstRead = reads.withLock { count in+ count += 1+ return count == 1+ }+ if firstRead {+ // 仅暂停存储队列, 让主 actor 能在读取完成前关闭窗口+ #expect(release.wait(timeout: .now() + 5) == .success)+ }+ return Date()+ })+ let model = AppServerLogViewModel(storage: storage)+ defer { model.stop() }+ let firstRead = Task { await model.refresh() }+ defer { firstRead.cancel() }+ for _ in 0 ..< 200 where reads.withLock({ $0 }) == 0 {+ try await Task.sleep(for: .milliseconds(5))+ }+ try #require(reads.withLock { $0 } > 0)+ #expect(model.isLoading)++ model.stop()+ #expect(!model.isLoading)+ #expect(model.entries.isEmpty)+ if reopenImmediately {+ model.start()+ }+ release.signal()+ await firstRead.value+ if !reopenImmediately {+ #expect(!model.isLoading)+ #expect(model.entries.isEmpty)+ #expect(model.totalCount == 0)+ model.start()+ }++ for _ in 0 ..< 200 where model.entries.isEmpty || model.isLoading {+ try await Task.sleep(for: .milliseconds(5))+ }+ #expect(!model.isLoading)+ #expect(model.totalCount == 1)+ #expect(model.entries.first?.detail == "saved log")+ #expect(model.errorMessage == nil)+ }++ @Test func startingObservationDuringFirstReadCompletesLoading() async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let storage = AppServerLogStore(directoryURL: directory.url)+ storage.recordFailure(message: "startup log")+ let model = AppServerLogViewModel(storage: storage)+ defer { model.stop() }++ // 确定性复现视图先读取, 窗口随后开始观察的调用顺序+ var didStart = false+ let subscription = model.$isLoading.sink { isLoading in+ guard isLoading, !didStart else { return }+ didStart = true+ model.start()+ }+ defer { subscription.cancel() }++ await model.refresh()+ #expect(didStart)+ #expect(!model.isLoading)+ #expect(model.totalCount == 1)+ #expect(model.entries.first?.detail == "startup log")+ #expect(model.errorMessage == nil)+ #expect(try await storage.page().total == 1)++ await model.refresh()+ #expect(!model.isLoading)+ #expect(model.entries.count == 1)++ model.stop()+ await model.refresh()+ #expect(!model.isLoading)+ #expect(model.entries.first?.detail == "startup log")+ }+@Test func unsupportedDatabaseVersionRejectsWritesAndClear() async throws {let directory = try TestDirectory()defer { try? directory.remove() }diff --git a/CodexBarTests/AppServer/AppServerSessionTests.swift b/CodexBarTests/AppServer/AppServerSessionTests.swiftindex 9a31249..0dfe465 100644--- a/CodexBarTests/AppServer/AppServerSessionTests.swift+++ b/CodexBarTests/AppServer/AppServerSessionTests.swift@@ -4,6 +4,69 @@ import Foundationimport Testingstruct AppServerSessionTests {+ @Test func acceptsResponseLargerThanSixteenMiB() throws {+ let server = try SharedServerFixture { peer in+ let request = try peer.readMessage()+ try peer.reply(to: request, result: ["value": 7, "output": String(repeating: "a", count: 17 * 1024 * 1024)])+ }+ defer { server.close() }+ let session = try AppServerSession(socketURL: server.url, logStorage: nil)+ defer { session.close() }+ let result: Value = try session.request("probe")+ #expect(result.value == 7)+ try server.finish()+ }++ @Test(arguments: [false, true])+ func oversizedMessageClosesSocketBeforeReadingBody(fragmented: Bool) throws {+ let limit = fragmented ? 1024 : 64 * 1024 * 1024+ let server = try SharedServerFixture { peer in+ _ = try peer.readMessage()+ if fragmented {+ try peer.sendRaw(Data(repeating: 32, count: 512), final: false)+ try peer.sendHeader(length: 513, opcode: 0)+ } else {+ try peer.sendHeader(length: UInt64(limit + 1))+ }+ }+ defer { server.close() }+ let session = try AppServerSession(socketURL: server.url, logStorage: nil, maximumMessageSize: limit)+ do {+ let _: Value = try session.request("probe")+ Issue.record("Expected oversized message")+ } catch let error as CodexStatusError {+ guard case let .messageTooLarge(bytes, maximum) = error else {+ Issue.record("Wrong error: \(error)")+ return+ }+ #expect(bytes == UInt64(limit + 1))+ #expect(maximum == limit)+ #expect(error.isTransportFailure)+ }+ #expect(!session.isOpen)+ try server.finish()+ }++ @Test func invalidControlFrameIsNotClassifiedAsOversizedHistory() throws {+ let server = try SharedServerFixture { peer in+ _ = try peer.readMessage()+ try peer.sendHeader(length: 64 * 1024 * 1024 + 1, opcode: 9)+ }+ defer { server.close() }+ let session = try AppServerSession(socketURL: server.url, logStorage: nil)+ do {+ let _: Value = try session.request("probe")+ Issue.record("Expected invalid control frame")+ } catch let error as CodexStatusError {+ guard case .invalidServerResponse = error else {+ Issue.record("Wrong error: \(error)")+ return+ }+ }+ #expect(!session.isOpen)+ try server.finish()+ }+@Test(arguments: ["account", "activity"], [nil, "0.162.0", "codex/", "codex/invalid", "codex/0.159.0"] as [String?])func connectionsShareHandshakeValidation(_ connection: String, userAgent: String?) throws {let server = try SharedServerFixture { peer in@@ -94,11 +157,11 @@ struct AppServerSessionTests {var disconnected = falsevar bootstrapped = falselet reader = AppServerActivityReader(- lifecycleCache: SessionLifecycleCache(), socketURL: server.url,- tokenHistory: TokenHistoryStore(directoryURL: directory.url),- recorder: ActivityRecorder(directoryURL: directory.url)+ probe: ActivityReaderProbe(), socketURL: server.url,+ tokenUsageHistory: TokenUsageHistoryStore(directoryURL: directory.url),+ activityDataRecorder: ActivityDataRecorder(directoryURL: directory.url)) { batch in- switch batch {+ switch batch.phase {case .sourceUnavailable:disconnected = truecase .bootstrapEnd:@@ -118,7 +181,7 @@ struct AppServerSessionTests {if supported {#expect(bootstrapped)for _ in 0 ..< 3 {- #expect(await reader.drainNow() == .completed)+ #expect(await reader.drainNow().status == .completed)}#expect(!disconnected)} else {diff --git a/CodexBarTests/AppServer/AppServerStartupTests.swift b/CodexBarTests/AppServer/AppServerStartupTests.swiftindex dc315ae..99a33fe 100644--- a/CodexBarTests/AppServer/AppServerStartupTests.swift+++ b/CodexBarTests/AppServer/AppServerStartupTests.swift@@ -399,7 +399,7 @@ struct AppServerStartupTests {}_ = try directory.write("allow", to: "allow")if manual {- #expect(try await service.reconnect(minimumVersion: "0.162.0").version == "0.162.0")+ #expect(try await service.reconnect().version == "0.162.0")} else {clock.withLock { $0 = $0.advanced(by: .seconds(60)) }let recovered = await service.fetchOutcome()@@ -412,7 +412,8 @@ struct AppServerStartupTests {try server.finish()}- @Test func existingOldDaemonIsRejectedWithoutStartingAnother() async throws {+ @Test(arguments: [false, true])+ func existingOldDaemonIsRejectedWithoutStartingAnother(manual: Bool) async throws {let server = try SharedServerFixture { peer inlet initialize = try peer.readMessage()try peer.reply(to: initialize, result: ["userAgent": "codex/0.161.0"])@@ -420,11 +421,39 @@ struct AppServerStartupTests {defer { server.close() }let startup = AppServerStartup(environment: [:], installations: .init(globalPath: nil, bundledPath: nil), probeConnection: {})let service = CodexStatusService(socketURL: server.url, logStorage: nil, startup: startup)- let result = await service.fetchOutcome()- guard case let .unsupportedVersion(minimum) = result.outcome else { Issue.record("Expected actual server version check")- return+ if manual {+ do {+ _ = try await service.reconnect()+ Issue.record("Expected actual server version check")+ } catch let error as CodexStatusError {+ guard case let .unsupportedVersion(minimum) = error else { throw error }+ #expect(minimum == "0.162.0")+ }+ } else {+ let result = await service.fetchOutcome()+ guard case let .unsupportedVersion(minimum) = result.outcome else { Issue.record("Expected actual server version check")+ return+ }+ #expect(minimum == "0.162.0")+ }+ #expect(await service.currentConnectionInfo() == nil)+ try server.finish()+ }++ @Test func manualReconnectRejectsInvalidHandshakeVersion() async throws {+ let server = try SharedServerFixture { peer in+ let initialize = try peer.readMessage()+ try peer.reply(to: initialize, result: ["userAgent": "codex/invalid"])+ }+ defer { server.close() }+ let service = CodexStatusService(socketURL: server.url, logStorage: nil)+ do {+ _ = try await service.reconnect()+ Issue.record("Expected invalid version rejection")+ } catch let error as CodexStatusError {+ guard case .serverConnectionClosed = error else { throw error }}- #expect(minimum == "0.162.0")+ #expect(await service.currentConnectionInfo() == nil)try server.finish()}diff --git a/CodexBarTests/CodexStatus/AccountNotificationTests.swift b/CodexBarTests/CodexStatus/AccountNotificationTests.swiftindex ecb6a28..8c8aa36 100644--- a/CodexBarTests/CodexStatus/AccountNotificationTests.swift+++ b/CodexBarTests/CodexStatus/AccountNotificationTests.swift@@ -198,23 +198,22 @@ struct AccountNotificationTests {}defer { server.close() }var changes: [AccountChange] = []- var events: [ActivityRecord] = []+ var updates: [ActivityUpdate] = []let reader = AppServerActivityReader(- lifecycleCache: SessionLifecycleCache(), socketURL: server.url, logStorage: nil,- tokenHistory: TokenHistoryStore(directoryURL: directory.url),- recorder: ActivityRecorder(directoryURL: directory.url),+ probe: ActivityReaderProbe(), socketURL: server.url, logStorage: nil,+ tokenUsageHistory: TokenUsageHistoryStore(directoryURL: directory.url),+ activityDataRecorder: ActivityDataRecorder(directoryURL: directory.url),onAccountChange: { changes.append($0) },- onBatch: { batch in- switch batch {- case let .live(values), let .snapshotEvents(values): events += values- default: break- }- }+ onBatch: { updates.append($0) })await reader.start()try await eventually { changes.contains(.account) }await reader.stop()- #expect(events.isEmpty)+ #expect(!updates.isEmpty)+ #expect(updates.allSatisfy {+ $0.graph.tasks.isEmpty && $0.graph.completions.isEmpty && $0.graph.terminations.isEmpty+ && $0.transitions.isEmpty && $0.terminalEvents.isEmpty+ })#expect(!FileManager.default.fileExists(atPath: directory.url.appendingPathComponent("Events").path))release.signal()try server.finish()@@ -234,6 +233,242 @@ struct AccountNotificationTests {}extension AccountNotificationTests {+ @Test(arguments: ["business", "unsupported", "invalidPayload"], [false, true])+ func supplementalFailurePreservesCacheAndUnsupportedMethodPolicy(failure: String, hasCache: Bool) async throws {+ let server = try SharedServerFixture { peer in+ let initialize = try peer.readMessage()+ try peer.reply(to: initialize, result: ["userAgent": "codex/0.162.0"])+ _ = try peer.readMessage()+ let rounds = hasCache ? 2 : 1+ for round in 0 ..< rounds {+ let account = try peer.readMessage()+ #expect(account["method"] as? String == "account/read")+ try peer.reply(to: account, result: ["account": ["type": "apiKey"]])+ let fails = round == rounds - 1+ for method in ["account/rateLimits/read", "account/usage/read"] {+ let attempts = fails && failure == "business" && method == "account/rateLimits/read" ? 2 : 1+ for _ in 0 ..< attempts {+ let request = try peer.readMessage()+ #expect(request["method"] as? String == method)+ if !fails {+ let result: [String: Any] = method == "account/rateLimits/read"+ ? ["rateLimits": [:], "ordinaryUsageAllowed": true]+ : ["summary": ["lifetimeTokens": 123], "dailyUsageBuckets": []]+ try peer.reply(to: request, result: result)+ } else if failure == "invalidPayload" {+ try peer.reply(to: request, result: [:])+ } else {+ try peer.send(["id": #require(request["id"]), "error": [+ "code": failure == "unsupported" ? -32601 : -32000,+ "message": failure == "unsupported" ? "unsupported" : "upstream network request failed"+ ]])+ }+ }+ }+ }+ }+ defer { server.close() }+ let service = CodexStatusService(socketURL: server.url, logStorage: nil)+ if hasCache {+ guard case .data = await service.fetchOutcome().outcome else {+ Issue.record("Initial read did not populate the cache")+ return+ }+ }+ let result = await service.fetchOutcome()+ guard case let .data(snapshot) = result.outcome else {+ Issue.record("Supplemental failure discarded the account snapshot")+ return+ }+ let usesCache = hasCache && failure != "unsupported"+ let step: CodexFetchTrace.StepResult = failure == "unsupported" ? .skipped : (usesCache ? .cached : .failed)+ #expect(result.trace.rateLimits == step && result.trace.usage == step)+ #expect(result.trace.failureStage == nil)+ #expect(snapshot.isRateLimitsStale == usesCache && snapshot.isUsageStale == usesCache)+ #expect(snapshot.ordinaryUsageAllowed == (usesCache ? true : nil))+ #expect(snapshot.usage?.summary.lifetimeTokens == (usesCache ? 123 : nil))+ try server.finish()+ }++ @Test(arguments: [false, true])+ func quotaReadsShareOneAuthenticationRefreshBudget(repeatsOnSameMethod: Bool) async throws {+ let server = try SharedServerFixture { peer in+ let initialize = try peer.readMessage()+ try peer.reply(to: initialize, result: ["userAgent": "codex/0.162.0"])+ _ = try peer.readMessage()+ let account = try peer.readMessage()+ try peer.reply(to: account, result: ["account": ["type": "apiKey"]])+ let limits = try peer.readMessage()+ #expect(limits["method"] as? String == "account/rateLimits/read")+ try peer.send(["id": #require(limits["id"]), "error": ["code": -32000, "message": "codex account authentication required"]])+ let refresh = try peer.readMessage()+ #expect(refresh["method"] as? String == "account/read")+ #expect((refresh["params"] as? [String: Any])?["refreshToken"] as? Bool == true)+ try peer.reply(to: refresh, result: ["account": ["type": "apiKey"]])+ let retried = try peer.readMessage()+ #expect(retried["method"] as? String == "account/rateLimits/read")+ let secondFailure: [String: Any]+ if repeatsOnSameMethod {+ secondFailure = retried+ } else {+ try peer.reply(to: retried, result: ["rateLimits": [:]])+ secondFailure = try peer.readMessage()+ #expect(secondFailure["method"] as? String == "account/usage/read")+ }+ try peer.send(["id": #require(secondFailure["id"]), "error": ["code": -32000, "message": "codex account authentication required"]])+ }+ defer { server.close() }+ let result = await CodexStatusService(socketURL: server.url, logStorage: nil).fetchOutcome()+ guard case .authenticationRequired = result.outcome else {+ Issue.record("Repeated authentication failure was not propagated")+ return+ }+ #expect(result.trace.failureStage == .snapshot)+ #expect(result.trace.rateLimits == (repeatsOnSameMethod ? nil : .ok))+ #expect(result.trace.usage == nil)+ try server.finish()+ }++ @Test(arguments: ["serverError", "invalidPayload", "signedOut"])+ func failedAuthenticationRefreshCannotFallBackToSupplementalCache(failure: String) async throws {+ let server = try SharedServerFixture { peer in+ let initialize = try peer.readMessage()+ try peer.reply(to: initialize, result: ["userAgent": "codex/0.162.0"])+ _ = try peer.readMessage()+ for round in 0 ..< 2 {+ let account = try peer.readMessage()+ #expect(account["method"] as? String == "account/read")+ try peer.reply(to: account, result: ["account": ["type": "apiKey"]])+ let limits = try peer.readMessage()+ #expect(limits["method"] as? String == "account/rateLimits/read")+ if round == 0 {+ try peer.reply(to: limits, result: ["rateLimits": [:], "ordinaryUsageAllowed": true])+ let usage = try peer.readMessage()+ #expect(usage["method"] as? String == "account/usage/read")+ try peer.reply(to: usage, result: ["summary": ["lifetimeTokens": 123]])+ } else {+ try peer.send(["id": #require(limits["id"]), "error": ["code": -32000, "message": "codex account authentication required"]])+ for _ in 0 ..< (failure == "serverError" ? 2 : 1) {+ let refresh = try peer.readMessage()+ #expect(refresh["method"] as? String == "account/read")+ #expect((refresh["params"] as? [String: Any])?["refreshToken"] as? Bool == true)+ switch failure {+ case "signedOut": try peer.reply(to: refresh, result: ["account": NSNull()])+ case "invalidPayload": try peer.reply(to: refresh, result: [:])+ default:+ try peer.send(["id": #require(refresh["id"]), "error": ["code": -32000, "message": "upstream network request failed"]])+ }+ }+ }+ }+ }+ defer { server.close() }+ let service = CodexStatusService(socketURL: server.url, logStorage: nil)+ guard case .data = await service.fetchOutcome().outcome else {+ Issue.record("Initial read did not populate the cache")+ return+ }+ let result = await service.fetchOutcome()+ #expect(result.trace.rateLimits == nil && result.trace.usage == nil)+ #expect(result.trace.failureStage == .snapshot)+ switch result.outcome {+ case .notLoggedIn: #expect(failure == "signedOut")+ case .initializationFailed: #expect(failure != "signedOut")+ default: Issue.record("Authentication refresh failure was hidden by cached data")+ }+ try server.finish()+ }++ @Test(arguments: ["authentication", "invalidPayload", "unsupportedVersion"], [false, true])+ func connectionFailurePreservesFetchStageAndOperationError(failure: String, readsConfiguration: Bool) async throws {+ let server = try SharedServerFixture { peer in+ let initialize = try peer.readMessage()+ try peer.reply(to: initialize, result: ["userAgent": failure == "unsupportedVersion" ? "codex/0.161.0" : "codex/0.162.0"])+ guard failure != "unsupportedVersion" else { return }+ #expect(try peer.readMessage()["method"] as? String == "initialized")+ let account = try peer.readMessage()+ #expect(account["method"] as? String == "account/read")+ if failure == "authentication" {+ try peer.send(["id": #require(account["id"]), "error": ["code": -32000, "message": "codex account authentication required"]])+ } else {+ try peer.reply(to: account, result: [:])+ }+ }+ defer { server.close() }+ let service = CodexStatusService(socketURL: server.url, logStorage: nil)+ if readsConfiguration {+ do {+ _ = try await service.areTUINotificationsEnabled()+ Issue.record("Configuration read accepted an uninitialized connection")+ } catch let error as CodexStatusError {+ switch error {+ case .authenticationRequired: #expect(failure == "authentication")+ case .unsupportedVersion: #expect(failure == "unsupportedVersion")+ case .serverConnectionClosed: #expect(failure == "invalidPayload")+ default: Issue.record("Unexpected connection error")+ }+ }+ } else {+ let result = await service.fetchOutcome()+ switch result.outcome {+ case .authenticationRequired: #expect(failure == "authentication")+ case .unsupportedVersion: #expect(failure == "unsupportedVersion")+ case .initializationFailed: #expect(failure == "invalidPayload")+ default: Issue.record("Unexpected fetch outcome")+ }+ #expect(result.trace.failureStage == (failure == "authentication" ? .account : .connect))+ #expect(result.trace.connection == nil)+ }+ #expect(await service.currentConnectionInfo() == nil)+ try server.finish()+ }++ @Test func autoResetAuthenticationRetryKeepsCreditIdentityAndFreshAccountCheck() async throws {+ let server = try SharedServerFixture { peer in+ let initialize = try peer.readMessage()+ try peer.reply(to: initialize, result: ["userAgent": "codex/0.162.0"])+ #expect(try peer.readMessage()["method"] as? String == "initialized")+ for refreshToken in [false, false] {+ let account = try peer.readMessage()+ #expect(account["method"] as? String == "account/read")+ #expect((account["params"] as? [String: Any])?["refreshToken"] as? Bool == refreshToken)+ try peer.reply(to: account, result: ["account": ["type": "apiKey"]])+ }+ let first = try peer.readMessage()+ #expect(first["method"] as? String == "account/rateLimitResetCredit/consume")+ try peer.send(["id": #require(first["id"]), "error": ["code": -32000, "message": "codex account authentication required"]])+ for refreshToken in [true, false] {+ let account = try peer.readMessage()+ #expect(account["method"] as? String == "account/read")+ #expect((account["params"] as? [String: Any])?["refreshToken"] as? Bool == refreshToken)+ try peer.reply(to: account, result: ["account": ["type": "apiKey"]])+ }+ let retried = try peer.readMessage()+ #expect(retried["method"] as? String == "account/rateLimitResetCredit/consume")+ for request in [first, retried] {+ let params = try #require(request["params"] as? [String: String])+ #expect(params == ["creditId": "credit", "idempotencyKey": "same-key"])+ }+ try peer.reply(to: retried, result: ["outcome": "reset"])+ let account = try peer.readMessage()+ #expect(account["method"] as? String == "account/read")+ try peer.reply(to: account, result: ["account": ["type": "apiKey"]])+ let limits = try peer.readMessage()+ #expect(limits["method"] as? String == "account/rateLimits/read")+ try peer.reply(to: limits, result: ["rateLimits": [:], "rateLimitResetCredits": ["availableCount": 0, "credits": []]])+ }+ defer { server.close() }+ let service = CodexStatusService(socketURL: server.url, logStorage: nil)+ let result = try await service.consumeResetCredit(+ id: "credit", idempotencyKey: "same-key", expectedAccountIdentity: "apiKey\u{0}",+ expirationDate: Date().addingTimeInterval(600),+ budget: AppServerRequestBudget(deadline: Date().addingTimeInterval(5))+ )+ #expect(result.outcome == .reset)+ #expect(result.refreshedRead?.availableCount == 0)+ try server.finish()+ }+@Test func missingAccountFieldIsNotSignedOut() throws {#expect(throws: DecodingError.self) { try TestFixtures.decode(AccountReadResponse.self, "{}") }#expect(try TestFixtures.decode(AccountReadResponse.self, "{\"account\":null}").account == nil)@@ -267,8 +502,10 @@ extension AccountNotificationTests {}defer { server.close() }let service = CodexStatusService(socketURL: server.url, logStorage: nil)- let outcome = await service.fetchOutcome().outcome- switch outcome {+ let fetched = await service.fetchOutcome()+ #expect(fetched.trace.failureStage == .snapshot)+ #expect(fetched.trace.connection == .new)+ switch fetched.outcome {case .notLoggedIn: #expect(result == "signedOut")case .authenticationRequired: #expect(result == "authentication")case .initializationFailed: #expect(["serverError", "invalidPayload", "connectionClosed"].contains(result))diff --git a/CodexBarTests/History/AggregationPolicyTests.swift b/CodexBarTests/History/AggregationPolicyTests.swiftindex 1d56312..1ae86ca 100644--- a/CodexBarTests/History/AggregationPolicyTests.swift+++ b/CodexBarTests/History/AggregationPolicyTests.swift@@ -3,114 +3,21 @@ import Foundationimport Testingstruct AggregationPolicyTests {- @Test func refreshPreservesExistingResultsUntilExplicitRebuild() async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }+ @Test func tokenUsageIdentityAndCloudEncodingPreserveAlgorithmVersions() throws {let now = Date()- let date = HistoryStorage.dateKey(for: now)- let service = history(in: directory)- let recorder = ActivityRecorder(directoryURL: directory.url)- try await recorder.record(event: event(at: now))- _ = await service.loadSnapshotWithMaintenance(synchronize: false, trigger: .manual)- let url = HistoryStorage.dailyURL(in: directory.url)- var old = try #require(JSONLines.decode(ActivityAggregate.self, from: Data(contentsOf: url)).first)- old.aggregationVersion = 1- let oldBoundary = try #require(old.sourceCheckpoint?.byteCount)- old.sourceCheckpoint?.aggregationRanges = [.init(version: 1, end: oldBoundary)]- old.turnStartedCount = 50- try old.jsonLineData().write(to: url)- let unchanged = await service.loadSnapshotWithMaintenance(synchronize: false, trigger: .auto)- #expect(unchanged.counts == nil)- #expect(try JSONLines.decode(ActivityAggregate.self, from: Data(contentsOf: url)).first?.turnStartedCount == 50)-- try await recorder.record(event: event(at: now.addingTimeInterval(1), turn: "second"))- _ = await service.loadSnapshotWithMaintenance(synchronize: false, trigger: .auto)- let appended = try #require(JSONLines.decode(ActivityAggregate.self, from: Data(contentsOf: url)).first)- #expect(appended.turnStartedCount == 51)- #expect(appended.metrics.turnCount == 2)- #expect(appended.sourceCheckpoint?.aggregationRanges.first?.version == 1)- #expect(appended.sourceCheckpoint?.aggregationRanges.last?.version == AggregationVersion.activity)-- _ = try await service.rebuildData(for: [date], synchronize: false)- let rebuilt = try #require(JSONLines.decode(ActivityAggregate.self, from: Data(contentsOf: url)).first)- #expect(rebuilt.turnStartedCount == 2)- #expect(rebuilt.aggregationVersion == AggregationVersion.activity)- #expect(try rebuilt.sourceCheckpoint?.aggregationRanges == [.init(version: AggregationVersion.activity, end: #require(rebuilt.sourceCheckpoint?.byteCount))])- }-- @Test func damagedActivityFileRecoversOnlyMissingDates() async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let now = Date()- let yesterday = try #require(Calendar.current.date(byAdding: .day, value: -1, to: now))- let service = history(in: directory)- let recorder = ActivityRecorder(directoryURL: directory.url)- try await recorder.record(event: event(at: now))- try await recorder.record(event: event(at: yesterday))- _ = await service.loadSnapshotWithMaintenance(synchronize: false, trigger: .manual)- let url = HistoryStorage.dailyURL(in: directory.url)- var preserved = try #require(JSONLines.decode(ActivityAggregate.self, from: Data(contentsOf: url)).first { $0.date == HistoryStorage.dateKey(for: yesterday) })- preserved.turnStartedCount = 40- try (preserved.jsonLineData() + Data("broken\n".utf8)).write(to: url)- _ = await service.loadSnapshotWithMaintenance(synchronize: false, trigger: .auto)- let results = try JSONLines.decode(ActivityAggregate.self, from: Data(contentsOf: url))- #expect(results.first { $0.date == preserved.date }?.turnStartedCount == 40)- #expect(results.first { $0.date == HistoryStorage.dateKey(for: now) }?.turnStartedCount == 1)- }-- @Test func expiredInvalidFileDoesNotBlockHealthyDate() async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let now = Date()- let expired = HistoryStorage.dateKey(for: HistoryStorage.retentionCutoffDate(today: now).addingTimeInterval(-86400))- let bad = try directory.write("broken header\n", to: "Events/\(expired).jsonl")- var state = HistoryMaintenanceState(dirty: [expired])- state.markPending(expired)- try HistoryStorage.saveMaintenanceState(state, in: directory.url)- let recorder = ActivityRecorder(directoryURL: directory.url)- try await recorder.record(event: event(at: now))- let service = history(in: directory)- let result = await service.loadSnapshotWithMaintenance(synchronize: false, trigger: .manual)- #expect(result.counts?.written == 1)- #expect(result.snapshot.dailyMetrics.first?.turnCount == 1)- #expect(try String(contentsOf: bad, encoding: .utf8) == "broken header\n")- #expect(try HistoryStorage.loadMaintenanceState(in: directory.url).dirty.contains(expired))- }-- @Test func pendingTodayKeepsYesterdaysMetricsVisible() async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let now = Date()- let yesterday = try #require(Calendar.current.date(byAdding: .day, value: -1, to: now))- let recorder = ActivityRecorder(directoryURL: directory.url)- let service = history(in: directory)- try await recorder.record(event: event(at: yesterday))- _ = await service.loadSnapshotWithMaintenance(synchronize: false, trigger: .manual)- try await recorder.record(event: event(at: now))- let snapshot = await service.loadSnapshot()- let days = UsageHeatmapDay.grid(usage: nil, history: snapshot, columnCount: 2, today: now).compactMap(\.self)- #expect(days.first { $0.startDate == HistoryStorage.dateKey(for: yesterday) }?.history.turnCount == 1)- #expect(days.first { $0.startDate == HistoryStorage.dateKey(for: now) }?.history.turnCount == nil)- }-- @Test func tokenIdentityAndCloudEncodingPreserveAlgorithmVersions() throws {- let now = Date()- let id = TokenTurn.identifier(thread: "thread", turn: "turn")- var turn = TestFixtures.tokenTurn(id: id, rootID: id, startedAt: now, updatedAt: now, usage: .zero)+ var turn = TestFixtures.tokenUsageAggregate(at: now)turn.aggregationVersion = 1- let exported = turn.pseudonymized(salt: Data("salt".utf8))- #expect(exported.aggregationVersion == 1)- let record = CKRecord(recordType: TokenSync.recordType, recordID: CKRecord.ID(recordName: exported.id))- TokenSync.apply(exported, to: record)- #expect(try TokenSync.turn(from: record)?.aggregationVersion == 1)- let baseline = TokenHistoryBaseline(salt: Data("salt".utf8), turns: [exported.id: exported])- #expect(baseline.replacement(for: turn) == nil)- let observation = TokenObservation(turn: turn, rootStartedAt: now, streamID: "stream", sequence: 1, previous: .zero, current: .zero)+ let daily = try #require(TokenUsageSyncRecord.dailyRecords(local: [turn], deviceID: "device", now: now).first)+ let record = CKRecord(recordType: TokenUsageSync.recordType, recordID: CKRecord.ID(recordName: daily.id))+ try TokenUsageSync.apply(daily, to: record)+ #expect(try TokenUsageSync.dailyRecord(from: record)?.aggregationVersion == AggregationVersion.tokenUsage)+ let observation = TestFixtures.tokenUsageObservation(at: now, tokenUsage: .zero)let encoded = try JSONLines.stableEncoder.encode(observation)let object = try #require(JSONSerialization.jsonObject(with: encoded) as? [String: Any])- let identity = try #require(object["turn"] as? [String: Any])- #expect(identity["aggregationVersion"] == nil)- #expect(identity["usage"] == nil)+ #expect(object["turn"] == nil)+ #expect(object["rootID"] == nil)+ #expect(object["aggregationVersion"] == nil)+ #expect(object["observedAt"] != nil)}@Test func algorithmCompatibilityAcceptsPastAndRejectsFuture() throws {@@ -119,62 +26,14 @@ struct AggregationPolicyTests {#expect(throws: StorageCompatibilityError.self) { try AggregationVersion.require(0, current: 2, name: "test") }}- private func event(at date: Date, turn: String = "first") -> ActivityRecord {- ActivityRecord(- timestamp: date, name: ActivityEventKind.turnStarted.rawValue, origin: .main,- cwd: nil, toolName: nil, model: nil, effort: nil,- threadID: "thread", turnID: turn, agentID: nil, id: "event-\(date.timeIntervalSince1970)-\(turn)"- )- }-- private func history(in directory: TestDirectory) -> HistoryService {- HistoryService(directoryURL: directory.url, syncService: SyncService(directoryURL: HistoryStorage.syncDirectoryURL(in: directory.url), isEnabled: { false }))- }-}--extension AggregationPolicyTests {- @Test func recoveredTokenResultsReplaceCoveredCloudBaseline() async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let now = Date()- let id = TokenTurn.identifier(thread: "thread", turn: "turn")- let observed = TokenUsage(inputTokens: 10, cachedInputTokens: 0, cacheWriteInputTokens: 0, outputTokens: 0, reasoningOutputTokens: 0, totalTokens: 10)- let observation = TokenObservation(- turn: TokenTurn(id: id, rootID: id, startedAt: now, updatedAt: now),- rootStartedAt: now, streamID: "stream", sequence: 1, previous: .zero, current: observed- )- let store = TokenHistoryStore(directoryURL: directory.url)- let recorded = try await store.recordObservations([observation], now: now)- var previous = try #require(recorded.first)- previous.aggregationVersion = 1- previous.usage = TokenUsage(inputTokens: 12, cachedInputTokens: 0, cacheWriteInputTokens: 0, outputTokens: 0, reasoningOutputTokens: 0, totalTokens: 12)- let salt = Data("account".utf8)- let remote = previous.pseudonymized(salt: salt)- let baseline = TokenHistoryBaseline(salt: salt, turns: [remote.id: remote])- try FileManager.default.removeItem(at: directory.url.appendingPathComponent("Aggregates/tokens.json"))- // 离线恢复后重新启动, 仍须保留与云端旧结果建立替换关系的能力- _ = try await store.refresh(now: now)- let reopened = TokenHistoryStore(directoryURL: directory.url)- let records = try await reopened.refresh(now: now, baseline: baseline)- let recovered = try #require(records.first)- #expect(recovered.usage == observed)- #expect(!recovered.hasConflict)- #expect(recovered.aggregationVersion == AggregationVersion.tokens)- #expect(recovered.ancestorIDs.contains(previous.generationID))- let merged = try remote.merging(recovered.pseudonymized(salt: salt))- #expect(merged.usage == observed)- #expect(merged.aggregationVersion == AggregationVersion.tokens)- #expect(try await reopened.refresh(now: now, baseline: baseline).first?.generationID == recovered.generationID)- }-@Test func partialSourcesKeepKnownValuesVisible() {let now = Date()let date = HistoryStorage.dateKey(for: now)- var aggregate = ActivityAggregate(date: date)+ var aggregate = ActivityDataAggregate(date: date)aggregate.turnCount = 3- var snapshot = HistorySnapshot(dailyMetrics: [aggregate.metrics])- snapshot.isActivityComplete = false+ var snapshot = HistorySnapshot(activityData: [aggregate.metrics])+ snapshot.isActivityDataComplete = falselet day = UsageHeatmapDay.grid(usage: nil, history: snapshot, columnCount: 1, today: now).compactMap(\.self).first { $0.startDate == date }- #expect(day?.history.turnCount == 3)+ #expect(day?.activityData.turnCount == 3)}}diff --git a/CodexBarTests/History/AppServerEventStorageTests.swift b/CodexBarTests/History/AppServerEventStorageTests.swiftindex 12f3e1a..159f73b 100644--- a/CodexBarTests/History/AppServerEventStorageTests.swift+++ b/CodexBarTests/History/AppServerEventStorageTests.swift@@ -1,148 +1,39 @@-import Darwinimport Foundationimport Testingstruct AppServerEventStorageTests {private let now = TestFixtures.now- private func token(id: String = "root", root: String = "root", at date: Date? = nil, input: Int64 = 100, sequence: Int64 = 1) -> TokenTurn {- var value = TestFixtures.tokenTurn(id: id, rootID: root, startedAt: date ?? now, updatedAt: date ?? now, usage: TokenUsage(- inputTokens: input, cachedInputTokens: 20, cacheWriteInputTokens: 5,- outputTokens: 10, reasoningOutputTokens: 2, totalTokens: input + 10- ))- value.checkpoint["test-stream"]?.sequence = sequence- return value- }-- private func events(in directory: URL, date: Date? = nil) throws -> [AppServerEventRecord] {- var records: [AppServerEventRecord] = []- let url = HistoryStorage.eventLogURL(for: HistoryStorage.dateKey(for: date ?? now), in: HistoryStorage.eventsDirectoryURL(in: directory))- try AppServerEventJournal.read(at: url) { records.append($0) }- return records- }-- @Test func mixedJournalPreservesEveryActivityMetricAndSkipsTokensWithoutCorruption() throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let path = "Events/\(HistoryStorage.dateKey(for: now)).jsonl"- let activity = ActivityEventKind.allCases.map { TestFixtures.event($0) }- var before = ActivityAccumulator(rebuilding: HistoryStorage.dateKey(for: now), generationID: nil, eventCountAvailability: .all)- var after = before- var data = Data()- for event in activity {- before.record(event)- try data.append(AppServerEventRecord(activity: event, recordedAt: now).jsonLineData())- try data.append(AppServerEventRecord(token: token(), recordedAt: now).jsonLineData())- }- let url = try directory.writeJournal(data, to: path)- var corrupt = 0- try AppServerEventJournal.read(at: url, onInvalidLine: { corrupt += 1 }, consume: { entry in- if let event = entry.activity {- after.record(event)- }- })- let expected = before.finalized(identifierStorage: .retained)- let actual = after.finalized(identifierStorage: .retained)- #expect(corrupt == 0)- #expect(actual == expected)- }-- @Test func boundedReadExcludesLaterRecordsAndResumesAtBoundary() throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let first = TestFixtures.event(.turnStarted)- let second = TestFixtures.event(.turnCompleted)- let firstData = try AppServerEventRecord(activity: first, recordedAt: now).jsonLineData()- let secondData = try AppServerEventRecord(activity: second, recordedAt: now).jsonLineData()- let url = try directory.writeJournal(firstData + secondData, to: "Events/\(HistoryStorage.dateKey(for: now)).jsonl")- let boundary = HistoryStorage.fileSize(at: url) - UInt64(secondData.count)- var records: [ActivityRecord] = []- try AppServerEventJournal.read(at: url, upTo: boundary) { entry in- if let event = entry.activity {- records.append(event)- }- }- #expect(records == [first])- try AppServerEventJournal.read(at: url, from: boundary) { entry in- if let event = entry.activity {- records.append(event)- }- }- #expect(records == [first, second])- }-- @Test func cancelledJournalScanStopsBetweenRecordsAndCanBeReadAgain() async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let line = try AppServerEventRecord(activity: TestFixtures.event(), recordedAt: now).jsonLineData()- let url = try directory.writeJournal(line + line, to: "Events/\(HistoryStorage.dateKey(for: now)).jsonl")- let scan = Task.detached {- var count = 0- do {- try AppServerEventJournal.read(at: url) { _ in- count += 1- withUnsafeCurrentTask { $0?.cancel() }- }- return false- } catch is CancellationError {- return count == 1- }- }- #expect(try await scan.value)- var count = 0- try AppServerEventJournal.read(at: url) { _ in count += 1 }- #expect(count == 2)- }-- @Test func oversizedAndPartialLinesDoNotHideFollowingValidRecords() throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let event = TestFixtures.event()- var data = Data(repeating: 120, count: 1024 * 1024 + 100)- data.append(Data("\n \t\r\n".utf8))- try data.append(AppServerEventRecord(activity: event, recordedAt: now).jsonLineData())- data.append(Data("{broken".utf8))- let url = try directory.writeJournal(data, to: "Events/\(HistoryStorage.dateKey(for: now)).jsonl")- var corrupt = 0- var records: [ActivityRecord] = []- try AppServerEventJournal.read(at: url, upTo: HistoryStorage.fileSize(at: url), onInvalidLine: { corrupt += 1 }, consume: { entry in- if let event = entry.activity {- records.append(event)- }- })- #expect(corrupt == 2)- #expect(records == [event])- }-@Test func nativeMetadataRoundTripsWithoutChangingBusinessIdentity() throws {- var event = TestFixtures.event(.toolStarted, thread: "root", turn: "child-turn", agent: "child", origin: .auxiliary)- event.context = ActivityContext(+ var event = TestFixtures.event(.toolStarted, thread: "root", turn: "child-turn", agentThreadID: "child", origin: .auxiliary)+ event.context = ActivityDataContext(method: "item/started", threadID: "child", turnID: "child-turn", parentThreadID: "parent",rootThreadID: "root", rootTurnID: "root-turn", itemID: "item", itemType: "commandExecution", itemStatus: "inProgress")- let record = AppServerEventRecord(activity: event, recordedAt: now)+ let record = AppServerEventRecord(activityData: event, recordedAt: now)let decoded = try AppServerEventRecord.decode(from: record.jsonLineData())- #expect(decoded.activity == event)+ #expect(decoded.activityData == event)#expect(decoded.context?.threadID == "child")- #expect(decoded.activity?.threadID == "root")+ #expect(decoded.activityData?.threadID == "root")let text = try #require(String(bytes: record.jsonLineData(), encoding: .utf8))#expect(text.contains("toolStarted"))#expect(!text.contains("PreToolUse"))#expect(!text.contains("commandLine"))}- @Test func activityStorageSeparatesProtocolContextFromBusinessFields() throws {- var event = TestFixtures.event(.approvalRequested, agent: "child")- event.context = ActivityContext(+ @Test func activityDataStorageSeparatesProtocolContextFromBusinessFields() throws {+ var event = TestFixtures.event(.approvalRequested, agentThreadID: "child")+ event.context = ActivityDataContext(method: "item/commandExecution/requestApproval", threadID: "child", turnID: "child-turn",parentThreadID: "parent", rootThreadID: "root", rootTurnID: "root-turn", itemID: "item",itemType: "commandExecution", itemStatus: "inProgress", agentThreadID: "child",itemKind: "started", requestID: "request", turnStatus: .running,turnStartedAt: now, turnCompletedAt: now.addingTimeInterval(1), duration: 1)- let data = try AppServerEventRecord(activity: event, recordedAt: now).jsonLineData()+ let data = try AppServerEventRecord(activityData: event, recordedAt: now).jsonLineData()let record = try #require(JSONSerialization.jsonObject(with: data) as? [String: Any])- #expect(Set(record.keys) == ["version", "kind", "recordedAt", "context", "activityPayload"])+ #expect(Set(record.keys) == ["schemaVersion", "recordedAt", "context", "activityData"])+ #expect(record["kind"] == nil)let context = try #require(record["context"] as? [String: Any])#expect(Set(context.keys) == ["method", "threadID", "turnID", "parentThreadID", "rootThreadID",@@ -152,101 +43,43 @@ struct AppServerEventStorageTests {#expect(context["itemKind"] as? String == "started")#expect(context["duration"] as? Double == 1)#expect(context["turnStartedAt"] as? Double == now.timeIntervalSince1970 * 1000)- let payload = try #require(record["activityPayload"] as? [String: Any])+ let payload = try #require(record["activityData"] as? [String: Any])#expect(Set(payload.keys) == ["timestamp", "name", "origin", "cwd", "toolName", "model", "effort",- "threadID", "turnID", "agentID"- ])- #expect(try AppServerEventRecord.decode(from: data).activity == event)- }-- @Test func tokenStorageAndCheckpointsUsePropertyNamesAndPreserveAllCounters() async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let store = TokenHistoryStore(directoryURL: directory.url)- var turn = token()- turn.rebuiltAt = now- try await directory.seedTokenSnapshots([turn], now: now)- let data = try AppServerEventRecord(token: turn, recordedAt: now).jsonLineData()- let record = try #require(JSONSerialization.jsonObject(with: data) as? [String: Any])- #expect(Set(record.keys) == ["version", "kind", "recordedAt", "token"])- let snapshot = try #require(record["token"] as? [String: Any])- #expect(Set(snapshot.keys) == [- "aggregationVersion",- "id",- "rootID",- "startedAt",- "updatedAt",- "rebuiltAt",- "usage",- "generationID",- "ancestorIDs",- "checkpoint",- "hasConflict"+ "threadID", "turnID", "agentThreadID"])- let counts = try #require(snapshot["usage"] as? [String: Int64])- #expect(counts == ["inputTokens": 100, "cachedInputTokens": 20, "cacheWriteInputTokens": 5, "outputTokens": 10, "reasoningOutputTokens": 2, "totalTokens": 110])- #expect(try AppServerEventRecord.decode(from: data).token == turn)- _ = try await store.refresh(now: now)- let cacheData = try Data(contentsOf: directory.url.appendingPathComponent("Aggregates/tokens.json"))- let cache = try #require(JSONSerialization.jsonObject(with: cacheData) as? [String: Any])- #expect(Set(cache.keys) == ["version", "turns", "files", "recoveredTurnIDs"])- let checkpoints = try #require(cache["files"] as? [String: [String: Any]])- let checkpoint = try #require(checkpoints[HistoryStorage.dateKey(for: now)])- #expect(Set(checkpoint.keys) == ["size", "identifier", "modificationTime"])- #expect(try await TokenHistoryStore(directoryURL: directory.url).refresh(now: now) == [turn])+ #expect(try AppServerEventRecord.decode(from: data).activityData == event)}@Test func unsupportedVersionAndMixedPayloadAreRejected() throws {- let valid = try #require(String(bytes: AppServerEventRecord(token: token(), recordedAt: now).jsonLineData(), encoding: .utf8))- let future = valid.replacingOccurrences(of: "\"version\":1", with: "\"version\":99")- let mixed = valid.replacingOccurrences(of: "\"version\":1", with: "\"version\":1,\"activityPayload\":{}")+ let observation = TestFixtures.tokenUsageObservation(at: now, tokenUsage: .zero)+ let valid = try #require(String(bytes: AppServerEventRecord(tokenUsage: observation, recordedAt: now).jsonLineData(), encoding: .utf8))+ let future = valid.replacingOccurrences(of: "\"schemaVersion\":1", with: "\"schemaVersion\":99")+ let mixed = valid.replacingOccurrences(of: "\"schemaVersion\":1", with: "\"schemaVersion\":1,\"activityData\":{}")for text in [future, mixed] {#expect(throws: (any Error).self) { try AppServerEventRecord.decode(from: Data(text.utf8)) }}}- @Test func sharedJournalSurvivesRestartCacheDeletionAndDuplicateSnapshots() async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let first = TokenHistoryStore(directoryURL: directory.url)- try await directory.seedTokenSnapshots([token()], now: now)- try await directory.seedTokenSnapshots([token()], now: now)- #expect(try events(in: directory.url).count == 2)- #expect(try await first.refresh(now: now) == [token()])- let update = token(at: now.addingTimeInterval(1), input: 200, sequence: 2)- try await directory.seedTokenSnapshots([update], now: now)- #expect(try await first.refresh(now: now).first?.usage == update.usage)- try FileManager.default.removeItem(at: directory.url.appendingPathComponent("Aggregates/tokens.json"))- let restarted = TokenHistoryStore(directoryURL: directory.url)- let restored = try await restarted.refresh(now: now)- #expect(restored.count == 1)- #expect(restored.first?.usage == update.usage)- #expect(!FileManager.default.fileExists(atPath: directory.url.appendingPathComponent("Tokens").path))- }-- @Test func submillisecondTimestampsRecoverExactlyFromJournal() async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let store = TokenHistoryStore(directoryURL: directory.url)- let timestamp = now.addingTimeInterval(0.1234567)- try await directory.seedTokenSnapshots([token(at: timestamp)], now: timestamp)- _ = try await store.rebuild(for: [HistoryStorage.dateKey(for: timestamp)], now: timestamp.addingTimeInterval(0.0000456))- let original = try await store.refresh(now: timestamp)- try FileManager.default.removeItem(at: directory.url.appendingPathComponent("Aggregates/tokens.json"))- let recovered = try await TokenHistoryStore(directoryURL: directory.url).refresh(now: timestamp)- #expect(recovered == original)+ @Test func tokenUsageStorageUsesTokenUsageWithoutDuplicatingKind() throws {+ let observation = TestFixtures.tokenUsageObservation(at: now, tokenUsage: .zero)+ let record = AppServerEventRecord(tokenUsage: observation, recordedAt: now)+ let data = try record.jsonLineData()+ let object = try #require(JSONSerialization.jsonObject(with: data) as? [String: Any])+ #expect(Set(object.keys) == ["schemaVersion", "recordedAt", "tokenUsage"])+ #expect(record.kind.rawValue == "tokenUsage")+ #expect(try AppServerEventRecord.decode(from: data).tokenUsage == observation)}@Test func timestampsUseIntegerMillisecondsAcrossJournalAndSyncCache() throws {let timestamp = now.addingTimeInterval(0.1234567)let expected = Int64(now.timeIntervalSince1970 * 1000) + 123var event = TestFixtures.event(.turnStarted)- event.context = ActivityContext(+ event.context = ActivityDataContext(method: "turn/started", threadID: "thread", turnStartedAt: timestamp,turnCompletedAt: timestamp, duration: 0.4567)- let data = try AppServerEventRecord(activity: event, recordedAt: timestamp).jsonLineData()+ let data = try AppServerEventRecord(activityData: event, recordedAt: timestamp).jsonLineData()let text = try #require(String(data: data, encoding: .utf8))#expect(text.contains("\"recordedAt\":\(expected),"))#expect(text.contains("\"turnStartedAt\":\(expected)}"))@@ -256,114 +89,23 @@ struct AppServerEventStorageTests {let directory = try TestDirectory()defer { try? directory.remove() }- let url = directory.url.appendingPathComponent("cache.jsonl")- let record = try ActivitySyncRecord(- deviceID: "device", daily: ActivityAggregate(date: "2026-09-15", generationID: "source").syncedAggregate,- updatedAt: timestamp, recordName: "device_2026-09-15_source"+ let url = directory.url.appendingPathComponent("cache.json")+ let record = try ActivityDataSyncRecord(+ deviceID: "device", activityData: ActivityDataAggregate(date: "2026-09-15").syncedAggregate,+ updatedAt: timestamp, recordName: "activityData_device_2026-09-15")try JSONFileStorage.save(record, to: url)let stored = try Data(contentsOf: url)let object = try #require(JSONSerialization.jsonObject(with: stored) as? [String: Any])#expect(object["updatedAt"] as? Int64 == expected)- #expect((object["daily"] as? [String: Any])?["date"] as? String == "2026-09-15")- let records = JSONLines.decode(ActivitySyncRecord.self, from: stored)- #expect(records.first?.updatedAt == decoded.recordedAt)- #expect(try JSONLines.stableEncoder.encode(#require(records.first)) == stored)-- var cache = TokenFileCache<[TokenTurn]>(url: directory.url.appendingPathComponent("tokens.json"))- try cache.save([token(at: timestamp)])- var reopened = TokenFileCache<[TokenTurn]>(url: cache.url)- #expect(try reopened.load()?.first?.updatedAt == decoded.recordedAt)- }-- @Test func sameMillisecondSameSizeRewriteIsReadAgain() async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let store = TokenHistoryStore(directoryURL: directory.url)- try await directory.seedTokenSnapshots([token()], now: now)- let url = HistoryStorage.eventLogURL(for: HistoryStorage.dateKey(for: now), in: HistoryStorage.eventsDirectoryURL(in: directory.url))- let seconds = Int(now.timeIntervalSince1970)- let firstTimes = [timespec(tv_sec: seconds, tv_nsec: 1000100), timespec(tv_sec: seconds, tv_nsec: 1000100)]- #expect(utimensat(AT_FDCWD, url.path, firstTimes, 0) == 0)- _ = try await store.refresh(now: now)- let before = try #require(HistoryStorage.fileStat(at: url))- let replacement = try AppServerEventRecord(token: token(input: 200, sequence: 2), recordedAt: now).jsonLineData()- #expect(UInt64(replacement.count) < before.size)- let headerSize = try UInt64(#require(Data(contentsOf: url).firstIndex(of: 10)) + 1)- #expect(UInt64(replacement.count) + headerSize == before.size)- let handle = try FileHandle(forWritingTo: url)- try handle.seek(toOffset: headerSize)- try handle.write(contentsOf: replacement)- try handle.close()- let secondTimes = [timespec(tv_sec: seconds, tv_nsec: 1000900), timespec(tv_sec: seconds, tv_nsec: 1000900)]- #expect(utimensat(AT_FDCWD, url.path, secondTimes, 0) == 0)- let after = try #require(HistoryStorage.fileStat(at: url))- #expect(before.modificationTime == after.modificationTime)- #expect(before.modifiedAtNanoseconds != after.modifiedAtNanoseconds)- #expect(try await store.refresh(now: now).first?.usage?.inputTokens == 200)- #expect(try await TokenHistoryStore(directoryURL: directory.url).refresh(now: now).first?.usage?.inputTokens == 200)- let cache = try #require(JSONSerialization.jsonObject(with: Data(contentsOf: directory.url.appendingPathComponent("Aggregates/tokens.json"))) as? [String: Any])- let checkpoints = try #require(cache["files"] as? [String: [String: Any]])- #expect(checkpoints[HistoryStorage.dateKey(for: now)]?["modificationTime"] as? Int64 == after.modificationTime)- }-- @Test func rebuildMarkerAlsoSurvivesDeletingAggregateCache() async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let store = TokenHistoryStore(directoryURL: directory.url)- let value = token()- let observation = try TokenObservation(- turn: TokenTurn(id: value.id, rootID: value.rootID, startedAt: now, updatedAt: now),- rootStartedAt: now,- streamID: "stream",- sequence: 1,- previous: .zero,- current: #require(value.usage)- )- _ = try await store.recordObservations([observation], now: now)- let rebuiltAt = now.addingTimeInterval(1)- _ = try await store.rebuild(for: [HistoryStorage.dateKey(for: now)], now: rebuiltAt)- try FileManager.default.removeItem(at: directory.url.appendingPathComponent("Aggregates/tokens.json"))- #expect(try await store.refresh(now: rebuiltAt).first?.rebuiltAt == rebuiltAt)- }-- @Test func childAfterMidnightKeepsRootDateWhenAllCachesAreDeleted() async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let start = try #require(CodexDateFormat.dayDate(from: "2026-09-15")).addingTimeInterval(86390)- let next = start.addingTimeInterval(60)- let store = TokenHistoryStore(directoryURL: directory.url)- try await directory.seedTokenSnapshots([token(at: start)], now: start)- try await directory.seedTokenSnapshots([token(id: "child", at: next, input: 200)], now: next)- try FileManager.default.removeItem(at: directory.url.appendingPathComponent("Aggregates/tokens.json"))- let restored = try await store.refresh(now: next)- #expect(TokenTurn.dailyUsage(restored, now: next)["2026-09-15"]?.totalTokens == 320)- #expect(TokenTurn.dailyUsage(restored, now: next).count == 1)- }-- @Test func partialTailAndInterleavedActivityDoNotLoseTokensOrDefeatDeduplication() async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let recorder = ActivityRecorder(directoryURL: directory.url)- let store = TokenHistoryStore(directoryURL: directory.url)- let event = ActivityRecord(- timestamp: now, name: ActivityEventKind.toolStarted.rawValue, origin: .main,- cwd: nil, toolName: "Bash", model: "model", effort: nil,- threadID: "thread", turnID: "turn", agentID: nil, id: "tool"- )- try await recorder.record(event: event)- try await directory.seedTokenSnapshots([token()], now: now)- let url = HistoryStorage.eventLogURL(for: HistoryStorage.dateKey(for: now), in: HistoryStorage.eventsDirectoryURL(in: directory.url))- let handle = try FileHandle(forWritingTo: url)- try handle.seekToEnd()- try handle.write(contentsOf: Data("{\"partial\":".utf8))- try handle.close()- let updated = token(at: now.addingTimeInterval(1), input: 300, sequence: 2)- try await directory.seedTokenSnapshots([updated], now: now)- try await recorder.record(event: event)- let entries = try events(in: directory.url)- #expect(entries.compactMap(\.activity).count == 1)- #expect(entries.compactMap(\.token).count == 2)- #expect(try await store.refresh(now: now).first?.usage == updated.usage)+ #expect(object["date"] as? String == "2026-09-15")+ let restored = try JSONLines.decoder.decode(ActivityDataSyncRecord.self, from: stored)+ #expect(restored.updatedAt == decoded.recordedAt)+ #expect(try JSONLines.stableEncoder.encode(restored) == stored)++ var cache = TokenUsageFileCache<[TokenUsageObservation]>(url: directory.url.appendingPathComponent("tokenUsage.json"))+ try cache.save([TestFixtures.tokenUsageObservation(at: timestamp, tokenUsage: .zero)])+ var reopened = TokenUsageFileCache<[TokenUsageObservation]>(url: cache.url)+ #expect(try reopened.load()?.first?.observedAt == decoded.recordedAt)}}diff --git a/CodexBarTests/History/HistoryAggregationTests.swift b/CodexBarTests/History/HistoryAggregationTests.swiftindex d0bbd83..a519d16 100644--- a/CodexBarTests/History/HistoryAggregationTests.swift+++ b/CodexBarTests/History/HistoryAggregationTests.swift@@ -3,8 +3,47 @@ import Foundationimport Testingstruct HistoryAggregationTests {+ @Test(arguments: [(nil as [String]?, 1), ([], 1), (["unknown"], 1), (["read"], 1), (["search", "read", "read"], 3)])+ func toolCountExpandsCommandActions(actions: [String]?, expected: Int) {+ var accumulator = makeAccumulator()+ accumulator.record(commandEvent(.toolStarted, actions: actions))+ let started = accumulator.finalized(identifierStorage: .retained)+ #expect(started.eventCount == 1)+ #expect(started.toolStartedCount == expected)+ #expect(started.metrics.toolCallCount == expected)++ accumulator.record(commandEvent(.toolCompleted, actions: actions))+ let completed = accumulator.finalized(identifierStorage: .retained)+ #expect(completed.eventCount == 2)+ #expect(completed.toolStartedCount == expected)+ #expect(completed.toolCompletedCount == expected)+ #expect(completed.metrics.toolCallCount == expected)+ #expect(completed.syncedAggregate.metrics.toolCallCount == expected)+ }++ @Test func commandActionCountsMatchBetweenIncrementalAndFullAggregation() {+ let firstEvents = [+ commandEvent(.toolStarted, actions: ["search", "read", "read"]),+ commandEvent(.toolCompleted, actions: ["search", "read", "read"]),+ TestFixtures.event(.toolStarted)+ ]+ let laterEvents = [TestFixtures.event(.toolCompleted)]+ var full = makeAccumulator()+ (firstEvents + laterEvents).forEach { full.record($0) }+ var initial = makeAccumulator()+ firstEvents.forEach { initial.record($0) }+ var incremental = ActivityDataAccumulator(appending: initial.finalized(identifierStorage: .retained))+ laterEvents.forEach { incremental.record($0) }+ let aggregate = full.finalized(identifierStorage: .retained)+ #expect(aggregate.metrics.toolCallCount == 4)+ #expect(aggregate.eventCount == 4)+ #expect(incremental.finalized(identifierStorage: .retained) == aggregate)+ #expect(full.finalized(identifierStorage: .compacted).metrics.toolCallCount == 4)+ }+@Test func aggregateJSONLinesPreserveSlashesAndStableBytes() throws {var first = TestFixtures.aggregate()+ first.position = 42first.projectCounts = ["/projects/a": 1, "/projects/z": 2]first.modelCounts = ["model/name": 3]var second = first@@ -21,8 +60,10 @@ struct HistoryAggregationTests {#expect(data.last == JSONLines.newlineByte)#expect(data.filter { $0 == JSONLines.newlineByte }.count == 1)}- #expect(try JSONLines.decoder.decode(ActivityAggregate.self, from: local) == first)- #expect(try JSONLines.decoder.decode(SyncedActivity.self, from: synced) == first.syncedAggregate)+ #expect(try JSONLines.decoder.decode(ActivityDataAggregate.self, from: local) == first)+ #expect(try JSONLines.decoder.decode(SyncedActivityData.self, from: synced) == first.syncedAggregate)+ let syncedFields = try #require(JSONSerialization.jsonObject(with: synced) as? [String: Any])+ #expect(syncedFields["position"] == nil)}@Test func syncStateIsIsolatedByApplicationRoot() throws {@@ -30,14 +71,14 @@ struct HistoryAggregationTests {defer { try? directory.remove() }let debugRoot = directory.url.appendingPathComponent("CodexBar Data Debug", isDirectory: true)let releaseRoot = directory.url.appendingPathComponent("CodexBar Data", isDirectory: true)- let debugStore = ActivitySyncStore(directoryURL: HistoryStorage.syncDirectoryURL(in: debugRoot))- let releaseStore = ActivitySyncStore(directoryURL: HistoryStorage.syncDirectoryURL(in: releaseRoot))- try releaseStore.saveState(SyncState(hashByDate: ["2026-09-15": "release"]))- #expect(debugStore.loadState().hashByDate.isEmpty)- try debugStore.saveState(SyncState(hashByDate: ["2026-10-05": "debug"]))- #expect(releaseStore.loadState().hashByDate["2026-10-05"] == nil)- let restored = ActivitySyncStore(directoryURL: HistoryStorage.syncDirectoryURL(in: debugRoot))- #expect(restored.loadState().hashByDate["2026-10-05"] == "debug")+ let debugStore = ActivityDataSyncStore(directoryURL: HistoryStorage.syncDirectoryURL(in: debugRoot))+ let releaseStore = ActivityDataSyncStore(directoryURL: HistoryStorage.syncDirectoryURL(in: releaseRoot))+ try releaseStore.saveState(ActivityDataSyncState(deviceID: "release"))+ #expect(debugStore.loadState().deviceID == nil)+ try debugStore.saveState(ActivityDataSyncState(deviceID: "debug"))+ #expect(releaseStore.loadState().deviceID == "release")+ let restored = ActivityDataSyncStore(directoryURL: HistoryStorage.syncDirectoryURL(in: debugRoot))+ #expect(restored.loadState().deviceID == "debug")#expect(SyncCloudKit.zoneName == "CodexBarAppZone")let root = AppStorage.directoryURL()#if DEBUG@@ -52,8 +93,8 @@ struct HistoryAggregationTests {@Test func identifierFieldsUseModelPropertyNames() throws {let aggregate = try TestFixtures.decode(- ActivityAggregate.self,- #"{"version":1,"aggregationVersion":1,"date":"2026-09-15","threadIDs":["thread-a"],"turnIDs":["turn-a"]}"#+ ActivityDataAggregate.self,+ #"{"schemaVersion":1,"aggregationVersion":1,"position":0,"date":"2026-09-15","threadIDs":["thread-a"],"turnIDs":["turn-a"]}"#)#expect(aggregate.threadIDs == ["thread-a"])#expect(aggregate.turnIDs == ["turn-a"])@@ -64,8 +105,8 @@ struct HistoryAggregationTests {#expect(fields["threadIds"] == nil)#expect(fields["turnIds"] == nil)- let record = try TestFixtures.decode(ActivitySyncRecord.self, """- {"deviceID":"device-a","recordName":"device-a_2026-09-15_source","daily":{"version":1,"aggregationVersion":1,"date":"2026-09-15","generationID":"source"}}+ let record = try TestFixtures.decode(ActivityDataSyncRecord.self, """+ {"deviceID":"device-a","recordName":"activityData_device-a_2026-09-15","schemaVersion":1,"aggregationVersion":1,"date":"2026-09-15","activityData":{}}""")#expect(record.deviceID == "device-a")let recordData = try JSONEncoder().encode(record)@@ -75,30 +116,30 @@ struct HistoryAggregationTests {}@Test func missingEventCountsRemainUnavailableThroughBothStorageFormats() throws {- let aggregate = try TestFixtures.decode(ActivityAggregate.self, #"{"version":1,"aggregationVersion":1,"date":"2026-09-15","turnCompletedCount":0}"#)+ let aggregate = try TestFixtures.decode(ActivityDataAggregate.self, #"{"schemaVersion":1,"aggregationVersion":1,"position":0,"date":"2026-09-15","turnCompletedCount":0}"#)#expect(aggregate.eventCount == nil)#expect(aggregate.turnAbortedCount == nil)#expect(aggregate.turnCompletedCount == 0)- let local = try JSONDecoder().decode(ActivityAggregate.self, from: aggregate.jsonLineData())- let synced = try JSONDecoder().decode(SyncedActivity.self, from: aggregate.syncedAggregate.jsonLineData())+ let local = try JSONDecoder().decode(ActivityDataAggregate.self, from: aggregate.jsonLineData())+ let synced = try JSONDecoder().decode(SyncedActivityData.self, from: aggregate.syncedAggregate.jsonLineData())#expect(local == aggregate)#expect(synced.turnAbortedCount == nil)#expect(synced.turnCompletedCount == 0)}@Test func completionEventsCannotStandInForUniqueTurnCounts() throws {- var aggregate = try TestFixtures.decode(ActivityAggregate.self, #"{"version":1,"aggregationVersion":1,"date":"2026-09-15","turnCompletedCount":9}"#)+ var aggregate = try TestFixtures.decode(ActivityDataAggregate.self, #"{"schemaVersion":1,"aggregationVersion":1,"position":0,"date":"2026-09-15","turnCompletedCount":9}"#)aggregate.normalizeIdentifierStorage(retainsIdentifiers: false)#expect(aggregate.turnCount == nil)#expect(aggregate.metrics.turnCount == nil)- let synced = try JSONDecoder().decode(SyncedActivity.self, from: aggregate.syncedAggregate.jsonLineData())+ let synced = try JSONDecoder().decode(SyncedActivityData.self, from: aggregate.syncedAggregate.jsonLineData())#expect(synced.turnCount == nil)#expect(synced.metrics.turnCount == nil)}@Test func eventPairsAndIdentifiersAreCountedWithoutDoubleCounting() {var accumulator = makeAccumulator()- for name in [ActivityEventKind.turnStarted, .toolStarted, .toolCompleted, .compactionStarted, .compactionCompleted, .subagentStarted, .subagentEnded] {+ for name in [ActivityDataEventKind.turnStarted, .toolStarted, .toolCompleted, .compactionStarted, .compactionCompleted, .subagentStarted, .subagentEnded] {accumulator.record(TestFixtures.event(name))}accumulator.record(TestFixtures.event(.turnCompleted, thread: "terminal-only", turn: "terminal-only"))@@ -125,9 +166,7 @@ struct HistoryAggregationTests {(firstEvents + laterEvents).forEach { full.record($0) }var initial = makeAccumulator()firstEvents.forEach { initial.record($0) }- var incremental = ActivityAccumulator(- appending: initial.finalized(identifierStorage: .retained), generationID: "source"- )+ var incremental = ActivityDataAccumulator(appending: initial.finalized(identifierStorage: .retained))laterEvents.forEach { incremental.record($0) }#expect(incremental.finalized(identifierStorage: .retained) == full.finalized(identifierStorage: .retained))}@@ -166,8 +205,8 @@ struct HistoryAggregationTests {}@Test func compactingLegacyIdentifiersPreservesUniqueCounts() throws {- var aggregate = try TestFixtures.decode(ActivityAggregate.self, """- {"version":1,"aggregationVersion":1,"date":"2026-09-15","threadCount":0,"threadIDs":["a","a","b"],"turnIDs":["x","x"],"turnCompletedCount":9}+ var aggregate = try TestFixtures.decode(ActivityDataAggregate.self, """+ {"schemaVersion":1,"aggregationVersion":1,"position":0,"date":"2026-09-15","threadCount":0,"threadIDs":["a","a","b"],"turnIDs":["x","x"],"turnCompletedCount":9}""")aggregate.normalizeIdentifierStorage(retainsIdentifiers: false)#expect(aggregate.threadCount == 2)@@ -181,6 +220,7 @@ struct HistoryAggregationTests {let data = try accumulator.finalized(identifierStorage: .retained).syncedAggregate.jsonLineData()let object = try #require(JSONSerialization.jsonObject(with: data) as? [String: Any])#expect(object["threadIDs"] == nil)+ #expect(object["sourceCheckpoint"] == nil)#expect(object["turnIDs"] == nil)#expect(object["threadCount"] as? Int == 1)let text = try #require(String(data: data, encoding: .utf8))@@ -196,109 +236,109 @@ struct HistoryAggregationTests {"compactionCompletedCount": 10, "subagentStartedCount": 11, "subagentEndedCount": 12]var fields: [String: Any] = counts- fields["version"] = 1+ fields["schemaVersion"] = 1fields["aggregationVersion"] = 1- fields["sourceCheckpoint"] = ["byteCount": 1, "digest": String(repeating: "a", count: 64)]fields["date"] = "2026-09-15"- fields["generationID"] = "source"let aggregate = try JSONDecoder().decode(- SyncedActivity.self, from: JSONSerialization.data(withJSONObject: fields)+ SyncedActivityData.self, from: JSONSerialization.data(withJSONObject: fields))- let record = CKRecord(recordType: "Activity", recordID: CKRecord.ID(recordName: "device-a_2026-09-15_source"))- ActivityRecordCodec.apply(aggregate, deviceID: "device-a", to: record)- #expect(Set(record.allKeys()) == Set(counts.keys).union([- "version", "aggregationVersion", "sourceCheckpoint", "deviceID", "date", "generationID", "projectCounts", "modelCounts", "updatedAt"- ]))+ let record = CKRecord(recordType: "ActivityData", recordID: CKRecord.ID(recordName: "activityData_device-a_2026-09-15"))+ try ActivityDataRecordCodec.apply(aggregate, deviceID: "device-a", to: record)+ #expect(Set(record.allKeys()) == [+ "schemaVersion", "aggregationVersion", "deviceID", "date", "activityData", "updatedAt"+ ])+ let payload = try #require(record["activityData"] as? Data)+ let encodedCounts = try #require(JSONSerialization.jsonObject(with: payload) as? [String: Any])+ #expect(Set(encodedCounts.keys) == Set(counts.keys).union(["eventCount", "projectCounts", "modelCounts"]))for (name, count) in counts {- #expect((record[name] as? NSNumber)?.intValue == count)+ #expect(encodedCounts[name] as? Int == count)}- let restored = try #require(try ActivityRecordCodec.remoteDailyRecord(from: record))+ #expect(encodedCounts["eventCount"] is NSNull)+ let restored = try #require(try ActivityDataRecordCodec.remoteDailyRecord(from: record))#expect(restored.deviceID == "device-a")- #expect(restored.daily == aggregate)+ #expect(restored.activityData == aggregate)- record["turnAbortedCount"] = nil- record["toolStartedCount"] = 0 as CKRecordValue- let missing = try #require(try ActivityRecordCodec.remoteDailyRecord(from: record))- #expect(missing.daily.turnAbortedCount == nil)- #expect(missing.daily.toolStartedCount == 0)+ var changed = aggregate+ changed.turnAbortedCount = nil+ changed.toolStartedCount = 0+ try ActivityDataRecordCodec.apply(changed, deviceID: "device-a", to: record)+ let changedData = try #require(record["activityData"] as? Data)+ let changedCounts = try #require(JSONSerialization.jsonObject(with: changedData) as? [String: Any])+ #expect(changedCounts["turnAbortedCount"] is NSNull)+ #expect(changedCounts["toolStartedCount"] as? Int == 0)+ let missing = try #require(try ActivityDataRecordCodec.remoteDailyRecord(from: record))+ #expect(missing.activityData.turnAbortedCount == nil)+ #expect(missing.activityData.toolStartedCount == 0)}- @Test(arguments: [nil, "", " "]) func cloudActivityRejectsMissingGeneration(generation: String?) {- let record = CKRecord(recordType: "Activity", recordID: CKRecord.ID(recordName: "device_2026-09-15_source"))- record["version"] = 1 as CKRecordValue+ @Test(arguments: [nil, ""]) func cloudActivityDataRejectsMissingDevice(deviceID: String?) {+ let record = CKRecord(recordType: "ActivityData", recordID: CKRecord.ID(recordName: "activityData_device_2026-09-15"))+ record["schemaVersion"] = 1 as CKRecordValuerecord["aggregationVersion"] = 1 as CKRecordValue- record["sourceCheckpoint"] = try? JSONEncoder().encode(ActivitySourceCheckpoint(byteCount: 1, digest: String(repeating: "a", count: 64))) as CKRecordValue- record["projectCounts"] = Data("{}".utf8) as CKRecordValue- record["modelCounts"] = Data("{}".utf8) as CKRecordValue- record["deviceID"] = "device" as CKRecordValue+ record["activityData"] = Data(#"{"projectCounts":{},"modelCounts":{}}"#.utf8) as CKRecordValue+ record["deviceID"] = deviceID as CKRecordValue?record["date"] = "2026-09-15" as CKRecordValue- record["generationID"] = generation as CKRecordValue?- #expect(throws: ActivitySyncError.self) { try ActivityRecordCodec.remoteDailyRecord(from: record) }+ #expect(throws: ActivityDataSyncError.self) { try ActivityDataRecordCodec.remoteDailyRecord(from: record) }}- @Test func cloudAndCachedActivityRejectMismatchedIdentity() throws {- let daily = SyncedActivity(- sourceCheckpoint: ActivitySourceCheckpoint(byteCount: 1, digest: String(repeating: "a", count: 64)),+ @Test func cloudAndCachedActivityDataRejectMismatchedIdentity() throws {+ let daily = SyncedActivityData(date: "2026-09-15",- generationID: "source",projectCounts: [:],modelCounts: [:])- let record = CKRecord(recordType: "Activity", recordID: CKRecord.ID(recordName: "device_2026-09-15"))- ActivityRecordCodec.apply(daily, deviceID: "device", to: record)- #expect(throws: ActivitySyncError.self) { try ActivityRecordCodec.remoteDailyRecord(from: record) }+ let record = CKRecord(recordType: "ActivityData", recordID: CKRecord.ID(recordName: "activityData_other_2026-09-15"))+ try ActivityDataRecordCodec.apply(daily, deviceID: "device", to: record)+ #expect(throws: ActivityDataSyncError.self) { try ActivityDataRecordCodec.remoteDailyRecord(from: record) }#expect(throws: DecodingError.self) {try TestFixtures.decode(- ActivitySyncRecord.self,- #"{"deviceID":"device","recordName":"device_2026-09-15","daily":{"version":1,"aggregationVersion":1,"date":"2026-09-15","generationID":"source"}}"#+ ActivityDataSyncRecord.self,+ #"{"deviceID":"device","recordName":"activityData_other_2026-09-15","schemaVersion":1,"aggregationVersion":1,"date":"2026-09-15","activityData":{}}"#)}}- private func makeAccumulator() -> ActivityAccumulator {- ActivityAccumulator(rebuilding: "2026-09-15", generationID: "source", eventCountAvailability: .all)+ private func makeAccumulator() -> ActivityDataAccumulator {+ ActivityDataAccumulator(rebuilding: "2026-09-15")+ }++ private func commandEvent(_ kind: ActivityDataEventKind, actions: [String]?) -> ActivityDataRecord {+ ActivityDataRecord(+ timestamp: TestFixtures.now, name: kind.rawValue, origin: .main, cwd: "/projects/example",+ toolName: nil, commandActionTypes: actions, model: "gpt-5", effort: "high",+ threadID: "thread-a", turnID: "turn-a", agentThreadID: nil+ )}}struct HistoryMergeTests {- @Test func sameGenerationUsesOneMostCompleteContribution() throws {+ @Test func localDailyCountsReplaceCachedCountsEvenWhenLower() throws {let local = TestFixtures.aggregate(events: 2, turns: 1)let remote = try record(TestFixtures.aggregate(events: 4, turns: 2))- #expect(snapshot(local, [remote]).dailyMetrics.first?.turnCount == 2)- #expect(snapshot(TestFixtures.aggregate(events: 6, turns: 3), [remote]).dailyMetrics.first?.turnCount == 3)- }-- @Test func freshIndependentGenerationAddsToPreviousContribution() throws {- let local = TestFixtures.aggregate(generation: "new", turns: 2)- #expect(try snapshot(local, [record(TestFixtures.aggregate(turns: 3))]).dailyMetrics.first?.turnCount == 5)- }-- @Test func independentSourceRemainsIndependentWithoutFreshnessFlag() throws {- let local = TestFixtures.aggregate(generation: "unknown", turns: 2)- #expect(try snapshot(local, [record(TestFixtures.aggregate(turns: 3))]).dailyMetrics.first?.turnCount == 5)+ #expect(snapshot(local, [remote]).activityData.first?.turnCount == 1)+ #expect(snapshot(TestFixtures.aggregate(events: 6, turns: 3), [remote]).activityData.first?.turnCount == 3)}- @Test func sameGenerationDeduplicatesAndOtherDevicesStillAdd() throws {+ @Test func localAndCachedDeviceCountOnceWhileOtherDevicesAdd() throws {let local = TestFixtures.aggregate()let remote = try record(TestFixtures.aggregate())let otherDevice = try record(TestFixtures.aggregate(turns: 4), device: "other")- #expect(snapshot(local, [remote, otherDevice]).dailyMetrics.first?.turnCount == 5)+ #expect(snapshot(local, [remote, otherDevice]).activityData.first?.turnCount == 5)}@Test func remoteOnlyDaysRemainVisible() throws {- let result = try HistorySnapshot(localAggregates: [], syncedRecords: [record(TestFixtures.aggregate(turns: 4))], currentDeviceID: "device")- #expect(result.dailyMetrics.first?.turnCount == 4)+ let result = try HistorySnapshot(localActivityDataAggregates: [], syncedActivityDataRecords: [record(TestFixtures.aggregate(turns: 4))], currentDeviceID: "device")+ #expect(result.activityData.first?.turnCount == 4)}- private func record(_ aggregate: ActivityAggregate, device: String = "device") throws -> ActivitySyncRecord {- let generation = try aggregate.syncedAggregate.requiredGenerationID()- return try ActivitySyncRecord(- deviceID: device, daily: aggregate.syncedAggregate,- recordName: ActivitySyncRecord.recordName(deviceID: device, date: aggregate.date, generation: generation)+ private func record(_ aggregate: ActivityDataAggregate, device: String = "device") throws -> ActivityDataSyncRecord {+ try ActivityDataSyncRecord(+ deviceID: device, activityData: aggregate.syncedAggregate,+ recordName: ActivityDataSyncRecord.recordName(deviceID: device, date: aggregate.date))}- private func snapshot(_ local: ActivityAggregate, _ remote: [ActivitySyncRecord]) -> HistorySnapshot {- HistorySnapshot(localAggregates: [local], syncedRecords: remote, currentDeviceID: "device")+ private func snapshot(_ local: ActivityDataAggregate, _ remote: [ActivityDataSyncRecord]) -> HistorySnapshot {+ HistorySnapshot(localActivityDataAggregates: [local], syncedActivityDataRecords: remote, currentDeviceID: "device")}}diff --git a/CodexBarTests/History/HistoryDatabaseTests.swift b/CodexBarTests/History/HistoryDatabaseTests.swiftnew file mode 100644index 0000000..5798996--- /dev/null+++ b/CodexBarTests/History/HistoryDatabaseTests.swift@@ -0,0 +1,543 @@+import Foundation+import SQLite3+import Testing++struct HistoryDatabaseTests {+ @Test func storedCommandActionsPreserveDuplicatesAndDoNotCountCompletionTwice() throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let database = try HistoryDatabase(directoryURL: directory.url)+ let now = Date()+ let date = HistoryStorage.dateKey(for: now)+ let actions = ["search", "read", "read"]+ let started = Self.record("command:started", at: now, commandActionTypes: actions)+ try database.append(started)+ try database.append(started)+ let first = try database.aggregateActivityData(date: date, now: now)+ #expect(first.eventCount == 1)+ #expect(first.metrics.toolCallCount == 3)++ let completed = Self.record("command:completed", at: now, kind: .toolCompleted, commandActionTypes: actions)+ try database.append(completed)+ let incremental = try database.aggregateActivityData(date: date, now: now)+ #expect(incremental.eventCount == 2)+ #expect(incremental.metrics.toolCallCount == 3)+ let rebuilt = try database.aggregateActivityData(date: date, rebuilding: true, now: now)+ #expect(rebuilt == incremental)+ #expect(rebuilt.syncedAggregate.metrics.toolCallCount == 3)+ }++ @Test func rebuildableDatesIncludeActivityAndTokenOnlyDaysWithinRetention() throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let database = try HistoryDatabase(directoryURL: directory.url)+ let now = Date()+ let yesterday = try #require(Calendar.current.date(byAdding: .day, value: -1, to: now))+ let cutoff = HistoryStorage.retentionCutoffDate(today: now)+ let expired = cutoff.addingTimeInterval(-1)+ for date in [expired, yesterday, now] {+ try database.append(event(UUID().uuidString, at: date))+ }+ for date in [expired, cutoff, now] {+ let observation = TestFixtures.tokenUsageObservation(at: date, tokenUsage: .zero)+ try database.append(AppServerEventRecord(tokenUsage: observation, recordedAt: now))+ }+ #expect(HistoryStorage.rebuildableEventDateKeys(in: directory.url) == [cutoff, yesterday, now].map(HistoryStorage.dateKey(for:)))+ }++ @Test func committedEventsSurviveRestartAndDeduplicate() throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let now = Date()+ let record = event("one", at: now)+ #expect(try HistoryDatabase(directoryURL: directory.url).append(record))+ #expect(try !HistoryDatabase(directoryURL: directory.url).append(record))+ let database = try HistoryDatabase(directoryURL: directory.url)+ let date = HistoryStorage.dateKey(for: now)+ #expect(try database.pendingActivityDataDates() == [date])+ let first = try database.aggregateActivityData(date: date)+ #expect(first.eventCount == 1)+ #expect(try database.pendingActivityDataDates().isEmpty)+ #expect(try database.aggregateActivityData(date: date) == first)+ #expect(try database.exportJSONLines(date: date) == record.jsonLineData())+ }++ @Test func aggregateAndCursorRollBackTogetherWithoutLosingSource() throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let database = try HistoryDatabase(directoryURL: directory.url)+ let now = Date()+ let date = HistoryStorage.dateKey(for: now)+ try database.append(event("one", at: now))+ let before = try database.aggregateActivityData(date: date)+ try database.append(event("two", at: now))+ try database.execute("CREATE TRIGGER reject_aggregate BEFORE UPDATE ON aggregates BEGIN SELECT RAISE(ABORT, 'injected failure'); END")+ #expect(throws: (any Error).self) { try database.aggregateActivityData(date: date) }+ #expect(try database.activityDataAggregates() == [before])+ #expect(try database.pendingActivityDataDates() == [date])+ var count = 0+ try database.entries { _ in count += 1 }+ #expect(count == 2)+ try database.execute("DROP TRIGGER reject_aggregate")+ #expect(try database.aggregateActivityData(date: date).eventCount == 2)+ #expect(try database.pendingActivityDataDates().isEmpty)+ }++ @Test func transactionFailureAndCancellationReleaseWriter() throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let database = try HistoryDatabase(directoryURL: directory.url)+ #expect(throws: (any Error).self) {+ try database.transaction {+ try database.execute("INSERT INTO days(date) VALUES ('test')")+ throw CancellationError()+ }+ }+ #expect(try database.dates().isEmpty)+ try database.transaction { try database.execute("INSERT INTO days(date) VALUES ('test')") }+ #expect(try database.dates() == ["test"])+ }++ @Test func separateConnectionsSerializeWritersAndKeepAllEvents() async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let url = directory.url+ let now = Date()+ _ = try HistoryDatabase(directoryURL: url)+ try await withThrowingTaskGroup(of: Void.self) { group in+ for writer in 0 ..< 3 {+ group.addTask {+ let database = try HistoryDatabase(directoryURL: url)+ for index in 0 ..< 12 {+ try database.append(Self.record("\(writer)-\(index)", at: now))+ }+ }+ }+ try await group.waitForAll()+ }+ let aggregate = try HistoryDatabase(directoryURL: url).aggregateActivityData(date: HistoryStorage.dateKey(for: now))+ #expect(aggregate.eventCount == 36)+ }++ @Test func compactedIdentityStillDeduplicatesLateEvents() throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let database = try HistoryDatabase(directoryURL: directory.url)+ let now = Date()+ let old = now.addingTimeInterval(-7 * 86400)+ let date = HistoryStorage.dateKey(for: old)+ try database.append(event("one", at: old))+ let first = try database.aggregateActivityData(date: date, now: now)+ #expect(first.threadIDs == nil)+ #expect(first.threadCount == 1)+ try database.append(event("two", at: old))+ let updated = try database.aggregateActivityData(date: date, now: now)+ let rebuilt = try database.aggregateActivityData(date: date, rebuilding: true, now: now)+ #expect(updated == rebuilt)+ #expect(updated.eventCount == 2)+ #expect(updated.threadCount == 1)+ #expect(updated.turnCount == 1)+ }++ @Test func retentionRemovesSourceAndAggregateInOneTransaction() throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let database = try HistoryDatabase(directoryURL: directory.url)+ let now = Date()+ let cutoff = HistoryStorage.retentionCutoffDate(today: now)+ let expired = cutoff.addingTimeInterval(-86400)+ for date in [expired, cutoff, now] {+ try database.append(event(UUID().uuidString, at: date))+ _ = try database.aggregateActivityData(date: HistoryStorage.dateKey(for: date), now: now)+ }+ #expect(try database.prune(now: now) == 1)+ #expect(try database.dates().count == 2)+ #expect(try database.activityDataAggregates().count == 2)+ #expect(throws: HistoryDataRebuildError.self) {+ try database.aggregateActivityData(date: HistoryStorage.dateKey(for: expired), rebuilding: true, now: now)+ }+ var count = 0+ try database.entries { _ in count += 1 }+ #expect(count == 2)+ }++ @Test func unsupportedDatabaseIsRejectedWithoutResettingData() throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let database = try HistoryDatabase(directoryURL: directory.url)+ try database.append(event("one", at: Date()))+ try database.execute("PRAGMA user_version = 999")+ #expect(throws: StorageCompatibilityError.unsupportedFormat("HistoryDatabase", 999)) {+ try HistoryDatabase(directoryURL: directory.url)+ }+ #expect(try database.dates().count == 1)+ }++ @Test func brokenDateDoesNotPreventHealthyDateCommit() throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let database = try HistoryDatabase(directoryURL: directory.url)+ let now = Date()+ let yesterday = now.addingTimeInterval(-86400)+ try database.append(event("bad", at: yesterday))+ try database.append(event("good", at: now))+ try database.execute("UPDATE events SET payload = ? WHERE identity = ?", [.data(Data("broken".utf8)), .text("activityData:bad")])+ #expect(throws: (any Error).self) { try database.aggregateActivityData(date: HistoryStorage.dateKey(for: yesterday)) }+ #expect(try database.aggregateActivityData(date: HistoryStorage.dateKey(for: now)).eventCount == 1)+ #expect(try database.pendingActivityDataDates() == [HistoryStorage.dateKey(for: yesterday)])+ }++ @Test func recordingLeaseHasOneWriterAndReleasesOnStop() throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let first = TokenUsageHistoryStore(directoryURL: directory.url)+ let second = TokenUsageHistoryStore(directoryURL: directory.url)+ #expect(try first.acquireRecordingLease())+ #expect(try !second.acquireRecordingLease())+ first.releaseRecordingLease()+ #expect(try second.acquireRecordingLease())+ second.releaseRecordingLease()+ }++ @Test func actualTaskCancellationRollsBackAndReleasesDatabaseLock() async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let url = directory.url+ let writer = Task.detached {+ let database = try HistoryDatabase(directoryURL: url)+ try database.transaction {+ try database.execute("INSERT INTO days(date) VALUES ('cancelled')")+ withUnsafeCurrentTask { $0?.cancel() }+ }+ }+ await #expect(throws: CancellationError.self) { try await writer.value }+ let reopened = try HistoryDatabase(directoryURL: url)+ #expect(try reopened.dates().isEmpty)+ try reopened.transaction { try reopened.execute("INSERT INTO days(date) VALUES ('cancelled')") }+ #expect(try reopened.dates() == ["cancelled"])+ }++ @Test func damagedAggregateIsPendingWhileOtherDatesRemainReadable() throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let database = try HistoryDatabase(directoryURL: directory.url)+ let now = Date()+ let today = HistoryStorage.dateKey(for: now)+ let yesterday = HistoryStorage.dateKey(for: now.addingTimeInterval(-86400))+ try database.append(event("one", at: now))+ try database.append(event("two", at: now.addingTimeInterval(-86400)))+ _ = try database.aggregateActivityData(date: today)+ let good = try database.aggregateActivityData(date: yesterday)+ try database.execute(+ "UPDATE aggregates SET activityData = ? WHERE date = ?",+ [.data(Data("broken".utf8)), .text(today)]+ )+ #expect(try database.activityDataAggregates() == [good])+ #expect(try database.pendingActivityDataDates() == [today])+ #expect(try database.aggregateActivityData(date: today).eventCount == 1)+ #expect(try database.pendingActivityDataDates().isEmpty)+ }++ private func tokenUsage(_ count: Int64) -> TokenUsage {+ TokenUsage(inputTokens: count, cachedInputTokens: 0, cacheWriteInputTokens: 0, outputTokens: 0, reasoningOutputTokens: 0, totalTokens: count)+ }++ private func event(_ id: String, at date: Date) -> AppServerEventRecord {+ Self.record(id, at: date)+ }++ private nonisolated static func record(+ _ id: String, at date: Date, kind: ActivityDataEventKind = .toolStarted, commandActionTypes: [String]? = nil+ ) -> AppServerEventRecord {+ AppServerEventRecord(activityData: ActivityDataRecord(+ timestamp: date, name: kind.rawValue, origin: .main, cwd: "/test",+ toolName: "read", commandActionTypes: commandActionTypes, model: nil, effort: nil, threadID: "thread", turnID: "turn", agentThreadID: nil, id: id+ ), recordedAt: date)+ }+}++extension HistoryDatabaseTests {+ @Test func aggregateColumnsStoreProgressWithEachResultAndLeaveMissingUsageNull() throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let database = try HistoryDatabase(directoryURL: directory.url)+ let now = Date()+ try database.append(event("one", at: now))+ let aggregate = try database.aggregateActivityData(date: HistoryStorage.dateKey(for: now))+ var connection: OpaquePointer?+ #expect(sqlite3_open_v2(directory.url.appendingPathComponent(HistoryDatabase.filename).path, &connection, SQLITE_OPEN_READONLY, nil) == SQLITE_OK)+ defer { sqlite3_close(connection) }+ var statement: OpaquePointer?+ let sql = """+ SELECT (SELECT group_concat(name, ',') FROM pragma_table_info('aggregates')), activityData, tokenUsage,+ (SELECT group_concat(name, ',') FROM pragma_table_info('days'))+ FROM aggregates+ """+ #expect(sqlite3_prepare_v2(connection, sql, -1, &statement, nil) == SQLITE_OK)+ defer { sqlite3_finalize(statement) }+ #expect(sqlite3_step(statement) == SQLITE_ROW)+ let columns = try #require(sqlite3_column_text(statement, 0))+ #expect(String(cString: columns) == "date,activityData,tokenUsage")+ let dayColumns = try #require(sqlite3_column_text(statement, 3))+ #expect(String(cString: dayColumns) == "date")+ #expect(sqlite3_column_type(statement, 1) == SQLITE_BLOB)+ #expect(sqlite3_column_type(statement, 2) == SQLITE_NULL)+ let bytes = try #require(sqlite3_column_blob(statement, 1))+ let data = Data(bytes: bytes, count: Int(sqlite3_column_bytes(statement, 1)))+ let fields = try #require(JSONSerialization.jsonObject(with: data) as? [String: Any])+ #expect(fields["position"] as? Int == 1)+ #expect(fields["aggregationVersion"] as? Int == AggregationVersion.activityData)+ #expect(try JSONLines.decoder.decode(ActivityDataAggregate.self, from: data) == aggregate)+ #expect(try database.pendingTokenUsageDates().isEmpty)+ #expect(try database.tokenUsageAggregates().isEmpty)+ }++ @Test(arguments: ["activityData", "tokenUsage"], ["outdatedAlgorithm", "missingPosition", "negativePosition"])+ func invalidAggregateMetadataRebuildsOnlyItsOwnColumn(kind: String, damage: String) throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let database = try HistoryDatabase(directoryURL: directory.url)+ let now = Date()+ let date = HistoryStorage.dateKey(for: now)+ try database.append(event("one", at: now))+ try database.append(AppServerEventRecord(tokenUsage: TestFixtures.tokenUsageObservation(at: now, tokenUsage: tokenUsage(20)), recordedAt: now))+ let activityData = try database.aggregateActivityData(date: date)+ let usage = try database.aggregateTokenUsage(date: date)+ let mutation = switch damage {+ case "outdatedAlgorithm": "json_set(CAST(\(kind) AS TEXT), '$.aggregationVersion', 0)"+ case "missingPosition": "json_remove(CAST(\(kind) AS TEXT), '$.position')"+ default: "json_set(CAST(\(kind) AS TEXT), '$.position', -1)"+ }+ try database.execute("UPDATE aggregates SET \(kind) = CAST(\(mutation) AS BLOB)")+ #expect(try database.pendingActivityDataDates() == (kind == "activityData" ? [date] : []))+ #expect(try database.pendingTokenUsageDates() == (kind == "tokenUsage" ? [date] : []))+ if kind == "activityData" {+ #expect(try database.aggregateActivityData(date: date) == activityData)+ } else {+ #expect(try database.aggregateTokenUsage(date: date) == usage)+ }+ #expect(try database.activityDataAggregates() == [activityData])+ #expect(try database.tokenUsageAggregates() == [usage])+ #expect(try database.pendingActivityDataDates().isEmpty)+ #expect(try database.pendingTokenUsageDates().isEmpty)+ }++ @Test func damagedAggregateColumnDoesNotReplaceTheOtherSummary() throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let database = try HistoryDatabase(directoryURL: directory.url)+ let now = Date()+ let date = HistoryStorage.dateKey(for: now)+ try database.append(event("one", at: now))+ try database.append(AppServerEventRecord(tokenUsage: TestFixtures.tokenUsageObservation(at: now, tokenUsage: tokenUsage(20)), recordedAt: now))+ _ = try database.aggregateActivityData(date: date)+ _ = try database.aggregateTokenUsage(date: date)+ try database.execute("UPDATE aggregates SET activityData = CAST('broken' AS BLOB)")+ try database.append(AppServerEventRecord(tokenUsage: TestFixtures.tokenUsageObservation(at: now, tokenUsage: tokenUsage(30)), recordedAt: now))+ #expect(try database.aggregateTokenUsage(date: date).tokenUsage == tokenUsage(50))+ #expect(try database.pendingActivityDataDates() == [date])+ #expect(try database.aggregateActivityData(date: date).eventCount == 1)+ #expect(try database.tokenUsageAggregates().first?.tokenUsage == tokenUsage(50))++ try database.execute("UPDATE aggregates SET tokenUsage = CAST('broken' AS BLOB)")+ try database.append(event("two", at: now))+ #expect(try database.aggregateActivityData(date: date).eventCount == 2)+ #expect(try database.pendingTokenUsageDates() == [date])+ #expect(try database.aggregateTokenUsage(date: date).tokenUsage == tokenUsage(50))+ #expect(try database.activityDataAggregates().first?.eventCount == 2)+ #expect(try database.pendingTokenUsageDates().isEmpty)+ }++ @Test func mismatchedEventKindCannotAdvanceTheAggregateCursor() throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let database = try HistoryDatabase(directoryURL: directory.url)+ let now = Date()+ let date = HistoryStorage.dateKey(for: now)+ try database.append(event("one", at: now))+ try database.execute("UPDATE events SET kind = 'tokenUsage'")+ #expect(throws: StorageCompatibilityError.incompleteSource) { try database.aggregateTokenUsage(date: date) }+ #expect(try database.tokenUsageAggregates().isEmpty)+ #expect(try database.pendingTokenUsageDates() == [date])+ }++ @Test func activityDataAndTokenUsageTotalsShareOneDayWithoutAdvancingEachOthersCursor() throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let database = try HistoryDatabase(directoryURL: directory.url)+ let now = Date()+ let date = HistoryStorage.dateKey(for: now)+ try database.append(event("first", at: now))+ try database.append(AppServerEventRecord(tokenUsage: TestFixtures.tokenUsageObservation(at: now, tokenUsage: tokenUsage(20)), recordedAt: now))+ let tokens = try database.aggregateTokenUsage(date: date)+ let activityData = try database.aggregateActivityData(date: date)+ #expect(activityData.position == 1)+ #expect(tokens.position == 2)+ #expect(try database.tokenUsageAggregates() == [tokens])+ #expect(try database.activityDataAggregates() == [activityData])+ #expect(try database.pendingActivityDataDates().isEmpty)+ #expect(try database.pendingTokenUsageDates().isEmpty)++ try database.append(AppServerEventRecord(tokenUsage: TestFixtures.tokenUsageObservation(at: now, tokenUsage: tokenUsage(30)), recordedAt: now))+ #expect(try database.pendingActivityDataDates().isEmpty)+ #expect(try database.pendingTokenUsageDates() == [date])+ _ = try database.aggregateActivityData(date: date)+ #expect(try database.pendingTokenUsageDates() == [date])+ #expect(try database.aggregateTokenUsage(date: date).tokenUsage == tokenUsage(50))+ #expect(try HistoryDatabase(directoryURL: directory.url).tokenUsageAggregates().first?.position == 3)+ #expect(try database.activityDataAggregates() == [activityData])++ try database.append(event("second", at: now))+ #expect(try database.pendingTokenUsageDates().isEmpty)+ #expect(try database.pendingActivityDataDates() == [date])+ _ = try database.aggregateTokenUsage(date: date)+ #expect(try database.pendingActivityDataDates() == [date])+ #expect(try database.aggregateActivityData(date: date).eventCount == 2)+ #expect(try HistoryDatabase(directoryURL: directory.url).activityDataAggregates().first?.position == 4)+ #expect(try database.tokenUsageAggregates().first?.tokenUsage == tokenUsage(50))+ }++ @Test func failedTokenUsageCommitDoesNotBlockActivityDataInTheSameRow() async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let database = try HistoryDatabase(directoryURL: directory.url)+ let now = Date()+ let date = HistoryStorage.dateKey(for: now)+ let store = TokenUsageHistoryStore(directoryURL: directory.url)+ _ = try await store.recordObservations([TestFixtures.tokenUsageObservation(at: now, tokenUsage: tokenUsage(20))], now: now)+ try await store.appendObservation(TestFixtures.tokenUsageObservation(at: now, tokenUsage: tokenUsage(30)), now: now)+ try database.append(event("activityData", at: now))+ try database.execute("CREATE TRIGGER reject_tokenUsage BEFORE UPDATE OF tokenUsage ON aggregates BEGIN SELECT RAISE(ABORT, 'injected failure'); END")+ #expect(try await store.loadSnapshot(now: now).coverage == .partial)+ #expect(try database.aggregateActivityData(date: date).eventCount == 1)+ #expect(try database.tokenUsageAggregates().first?.tokenUsage == tokenUsage(20))+ #expect(try database.pendingActivityDataDates().isEmpty)+ #expect(try database.pendingTokenUsageDates() == [date])+ try database.execute("DROP TRIGGER reject_tokenUsage")+ #expect(try await store.refresh(now: now).first?.tokenUsage == tokenUsage(50))+ #expect(try database.activityDataAggregates().first?.eventCount == 1)+ }++ @Test func dailyTokenUsageDeduplicateRetriesAndRebuildAfterCacheDamage() async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let now = Date()+ let store = TokenUsageHistoryStore(directoryURL: directory.url)+ let first = TestFixtures.tokenUsageObservation(at: now, tokenUsage: tokenUsage(20))+ let second = TestFixtures.tokenUsageObservation(at: now, tokenUsage: tokenUsage(30), previous: tokenUsage(20))+ let initial = try await store.recordObservations([first, first, second], now: now)+ #expect(initial.first?.tokenUsage == tokenUsage(50))+ #expect(initial.first?.eventCount == 2)+ #expect(try await TokenUsageHistoryStore(directoryURL: directory.url).refresh(now: now) == initial)+ let database = try HistoryDatabase(directoryURL: directory.url)+ try database.execute("UPDATE aggregates SET tokenUsage = ?", [.data(Data("broken".utf8))])+ #expect(try await store.refresh(now: now) == initial)+ let rebuilt = try await store.rebuild(for: [HistoryStorage.dateKey(for: now)])+ #expect(rebuilt.eventCount == 2)+ #expect(try await store.persistedAggregates(now: now) == initial)+ }++ @Test func observedDateSurvivesDelayedWriteAcrossMidnight() async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let now = Date()+ let midnight = Calendar.current.startOfDay(for: now)+ let before = midnight.addingTimeInterval(-1)+ let store = TokenUsageHistoryStore(directoryURL: directory.url)+ let first = TestFixtures.tokenUsageObservation(at: before, tokenUsage: tokenUsage(20))+ let second = TestFixtures.tokenUsageObservation(at: now, tokenUsage: tokenUsage(30), previous: tokenUsage(20))+ let result = try await store.recordObservations([first, second], now: now)+ #expect(TokenUsageAggregate.tokenUsageByDate(result, now: now) == [HistoryStorage.dateKey(for: before): tokenUsage(20), HistoryStorage.dateKey(for: now): tokenUsage(30)])+ let database = try HistoryDatabase(directoryURL: directory.url)+ try database.execute("""+ UPDATE aggregates SET tokenUsage = NULL+ """)+ #expect(try await store.refresh(now: now) == result)+ }++ @Test func unchangedSnapshotReadsSavedDailyTotalsWithoutReplayingEventsOrWriting() async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let now = Date()+ let store = TokenUsageHistoryStore(directoryURL: directory.url)+ let saved = try await store.recordObservations([TestFixtures.tokenUsageObservation(at: now, tokenUsage: tokenUsage(20))], now: now)+ let database = try HistoryDatabase(directoryURL: directory.url)+ try database.execute("UPDATE events SET payload = ? WHERE kind = 'tokenUsage'", [.data(Data("broken".utf8))])+ try database.execute("CREATE TRIGGER reject_tokenUsage BEFORE UPDATE OF tokenUsage ON aggregates BEGIN SELECT RAISE(ABORT, 'unexpected rewrite'); END")+ let snapshot = try await store.loadSnapshot(now: now)+ #expect(snapshot.coverage == .complete)+ #expect(snapshot.aggregates == saved)+ }++ @Test func failedTokenUsageDayPreservesTotalAndHealthyDayStillAdvances() async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let now = Date()+ let yesterday = now.addingTimeInterval(-86400)+ let store = TokenUsageHistoryStore(directoryURL: directory.url)+ _ = try await store.recordObservations([TestFixtures.tokenUsageObservation(at: yesterday, tokenUsage: tokenUsage(10))], now: now)+ let bad = TestFixtures.tokenUsageObservation(at: yesterday, tokenUsage: tokenUsage(30))+ try await store.appendObservation(bad, now: now)+ try await store.appendObservation(TestFixtures.tokenUsageObservation(at: now, tokenUsage: tokenUsage(20)), now: now)+ let database = try HistoryDatabase(directoryURL: directory.url)+ try database.execute("UPDATE events SET payload = ? WHERE identity = ?", [.data(Data("broken".utf8)), .text("tokenUsage:" + bad.id)])+ for _ in 0 ..< 2 {+ let snapshot = try await store.loadSnapshot(now: now)+ #expect(snapshot.coverage == .partial)+ #expect(TokenUsageAggregate.tokenUsageByDate(snapshot.aggregates, now: now) == [+ HistoryStorage.dateKey(for: yesterday): tokenUsage(10), HistoryStorage.dateKey(for: now): tokenUsage(20)+ ])+ }+ let rebuilt = try await store.rebuild(for: [HistoryStorage.dateKey(for: yesterday)])+ #expect(rebuilt.eventCount == 0)+ #expect(rebuilt.failedDateKeys == [HistoryStorage.dateKey(for: yesterday)])+ }++ @Test func failedDailyCommitKeepsCursorAndRetryCountsIncrementOnce() async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let now = Date()+ let store = TokenUsageHistoryStore(directoryURL: directory.url)+ let before = try await store.recordObservations([TestFixtures.tokenUsageObservation(at: now, tokenUsage: tokenUsage(10))], now: now)+ try await store.appendObservation(TestFixtures.tokenUsageObservation(at: now, tokenUsage: tokenUsage(20)), now: now)+ let database = try HistoryDatabase(directoryURL: directory.url)+ try database.execute("CREATE TRIGGER reject_tokenUsage BEFORE UPDATE OF tokenUsage ON aggregates BEGIN SELECT RAISE(ABORT, 'injected failure'); END")+ let failed = try await store.loadSnapshot(now: now)+ #expect(failed.coverage == .partial)+ #expect(failed.aggregates == before)+ try database.execute("DROP TRIGGER reject_tokenUsage")+ let recovered = try await store.loadSnapshot(now: now)+ #expect(recovered.coverage == .complete)+ #expect(recovered.aggregates.first?.tokenUsage == tokenUsage(30))+ #expect(try await store.refresh(now: now) == recovered.aggregates)+ }++ @Test func missingObservationCannotReplaceSavedDailyTotalDuringRebuild() async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let now = Date()+ let store = TokenUsageHistoryStore(directoryURL: directory.url)+ let second = TestFixtures.tokenUsageObservation(at: now, tokenUsage: tokenUsage(20))+ let before = try await store.recordObservations([TestFixtures.tokenUsageObservation(at: now, tokenUsage: tokenUsage(10)), second], now: now)+ try HistoryDatabase(directoryURL: directory.url).execute("DELETE FROM events WHERE identity = ?", [.text("tokenUsage:" + second.id)])+ let rebuilt = try await store.rebuild(for: [HistoryStorage.dateKey(for: now)])+ #expect(rebuilt.failedDateKeys == [HistoryStorage.dateKey(for: now)])+ #expect(try await store.persistedAggregates(now: now) == before)+ }++ @Test func tokenUsageRetentionRemovesDailyAggregateWithItsObservedEvents() async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let now = Date()+ let expired = HistoryStorage.retentionCutoffDate(today: now).addingTimeInterval(-86400)+ let store = TokenUsageHistoryStore(directoryURL: directory.url)+ _ = try await store.recordObservations([+ TestFixtures.tokenUsageObservation(at: expired, tokenUsage: tokenUsage(20)),+ TestFixtures.tokenUsageObservation(at: now, tokenUsage: tokenUsage(30))+ ], now: now)+ let database = try HistoryDatabase(directoryURL: directory.url)+ #expect(try database.prune(now: now) == 1)+ #expect(try database.tokenUsageAggregates().count == 1)+ #expect(try database.tokenUsageAggregates().first?.tokenUsage == tokenUsage(30))+ }+}diff --git a/CodexBarTests/History/HistoryMaintenanceTests.swift b/CodexBarTests/History/HistoryMaintenanceTests.swiftdeleted file mode 100644index 0c5fee1..0000000--- a/CodexBarTests/History/HistoryMaintenanceTests.swift+++ /dev/null@@ -1,256 +0,0 @@-import Darwin-import Foundation-import Synchronization-import Testing--struct HistoryMaintenanceTests {- @Test func normalizationWaitsForWriterAndUsesLatestAggregate() async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let sync = SyncService(directoryURL: HistoryStorage.syncDirectoryURL(in: directory.url), isEnabled: { false })- let service = HistoryService(directoryURL: directory.url, syncService: sync)- let date = HistoryStorage.dateKey(for: HistoryStorage.identifierRetentionCutoffDate().addingTimeInterval(-86400))- var aggregate = ActivityAggregate(date: date)- aggregate.eventCount = 1- aggregate.threadIDs = ["session"]- aggregate.turnIDs = ["turn"]- let dailyURL = HistoryStorage.dailyURL(in: directory.url)- try FileManager.default.createDirectory(at: dailyURL.deletingLastPathComponent(), withIntermediateDirectories: true)- try aggregate.jsonLineData().write(to: dailyURL)- try FileManager.default.createDirectory(at: HistoryStorage.lockURL(in: directory.url).deletingLastPathComponent(), withIntermediateDirectories: true)- let descriptor = open(HistoryStorage.lockURL(in: directory.url).path, O_CREAT | O_RDWR, 0o600)- try #require(descriptor >= 0)- defer { flock(descriptor, LOCK_UN)- close(descriptor)- }- try #require(flock(descriptor, LOCK_EX) == 0)- let started = Mutex(false)- let finished = Mutex(false)- let normalization = Task.detached {- started.withLock { $0 = true }- try await service.normalizeDailyAggregatesIfNeeded()- finished.withLock { $0 = true }- }- for _ in 0 ..< 100 where !started.withLock({ $0 }) {- try await Task.sleep(for: .milliseconds(5))- }- try #require(started.withLock { $0 })- try await Task.sleep(for: .milliseconds(100))- #expect(!finished.withLock { $0 })- aggregate.eventCount = 7- try aggregate.jsonLineData().write(to: dailyURL, options: .atomic)- #expect(flock(descriptor, LOCK_UN) == 0)- try await normalization.value- let normalized = try #require(JSONLines.decode(ActivityAggregate.self, from: Data(contentsOf: dailyURL)).first)- #expect(normalized.eventCount == 7)- #expect(normalized.threadIDs == nil)- #expect(normalized.turnIDs == nil)- }-- @Test func explicitRebuildRecalculatesFromEventsWithoutChangingFormatOrSource() async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let now = Date()- let date = HistoryStorage.dateKey(for: now)- let records = ["request-a", "request-b"].map {- AppServerEventRecord(activity: TestFixtures.event(.approvalRequested, at: now, turn: $0), recordedAt: now)- }- let recordsData = try records.reduce(into: Data()) { result, record in try result.append(record.jsonLineData()) }- let journalURL = try directory.writeJournal(recordsData, to: "Events/\(date).jsonl")- let data = try Data(contentsOf: journalURL)- let sync = SyncService(directoryURL: HistoryStorage.syncDirectoryURL(in: directory.url), isEnabled: { false })- let service = HistoryService(directoryURL: directory.url, syncService: sync)- _ = await service.loadSnapshotWithMaintenance(synchronize: false, trigger: .manual)- var aggregate = try #require(JSONLines.decode(ActivityAggregate.self, from: Data(contentsOf: HistoryStorage.dailyURL(in: directory.url))).first)- aggregate.approvalRequestedCount = 1- try aggregate.jsonLineData().write(to: HistoryStorage.dailyURL(in: directory.url))- var state = try HistoryStorage.loadMaintenanceState(in: directory.url)- state.markDirty(date)- try HistoryStorage.saveMaintenanceState(state, in: directory.url)- _ = await service.loadSnapshotWithMaintenance(synchronize: false, trigger: .manual)- let rebuilt = try #require(JSONLines.decode(ActivityAggregate.self, from: Data(contentsOf: HistoryStorage.dailyURL(in: directory.url))).first)- #expect(rebuilt.approvalRequestedCount == 2)- #expect(rebuilt.eventCount == 2)- #expect(rebuilt.generationID == aggregate.generationID)- #expect(try HistoryStorage.loadMaintenanceState(in: directory.url).version == HistoryMaintenanceState.currentVersion)- #expect(try Data(contentsOf: directory.url.appendingPathComponent("Events/\(date).jsonl")) == data)- }-- @Test func historyMaintenanceUsesOnlyInjectedRoot() async throws {- let first = try TestDirectory()- let second = try TestDirectory()- defer { try? first.remove()- try? second.remove()- }- let recorder = ActivityRecorder(directoryURL: first.url)- let event = ActivityRecord(- timestamp: Date(), name: ActivityEventKind.turnStarted.rawValue, origin: .main,- cwd: nil, toolName: nil, model: nil, effort: nil, threadID: "session", turnID: "turn", agentID: nil, id: "isolated"- )- try await recorder.record(event: event)- let sync = SyncService(directoryURL: HistoryStorage.syncDirectoryURL(in: first.url), isEnabled: { false })- let service = HistoryService(directoryURL: first.url, syncService: sync)- let result = await service.loadSnapshotWithMaintenance(synchronize: false, trigger: .manual)- #expect(result.counts?.events == 1)- #expect(FileManager.default.fileExists(atPath: HistoryStorage.dailyURL(in: first.url).path))- #expect(!FileManager.default.fileExists(atPath: HistoryStorage.dailyURL(in: second.url).path))- #expect(try !HistoryStorage.loadMaintenanceState(in: first.url).days.isEmpty)- #expect(try HistoryStorage.loadMaintenanceState(in: second.url).days.isEmpty)- }-}--extension HistoryMaintenanceTests {- @Test func boundaryVerificationDetectsSameMillisecondRewriteAndSurvivesRestart() async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let now = Date()- let date = HistoryStorage.dateKey(for: now)- var originalEvent = TestFixtures.event(.turnStarted, at: now)- originalEvent.context = nil- let original = try AppServerEventRecord(activity: originalEvent, recordedAt: now).jsonLineData()- let replacement = try AppServerEventRecord(activity: TestFixtures.event(.toolStarted, at: now), recordedAt: now).jsonLineData()- #expect(original.count == replacement.count)- let url = try directory.writeJournal(original, to: "Events/\(date).jsonl")- let seconds = Int(now.timeIntervalSince1970)- let firstTimes = [timespec(tv_sec: seconds, tv_nsec: 1000100), timespec(tv_sec: seconds, tv_nsec: 1000100)]- #expect(utimensat(AT_FDCWD, url.path, firstTimes, 0) == 0)- let sync = SyncService(directoryURL: HistoryStorage.syncDirectoryURL(in: directory.url), isEnabled: { false })- let service = HistoryService(directoryURL: directory.url, syncService: sync)- _ = await service.loadSnapshotWithMaintenance(synchronize: false, trigger: .manual)- let stateBefore = try HistoryStorage.loadMaintenanceState(in: directory.url)- let maintenanceBefore = try #require(HistoryStorage.fileStat(at: HistoryStorage.maintenanceURL(in: directory.url)))- _ = await service.loadSnapshotWithMaintenance(synchronize: false, trigger: .manual)- #expect(HistoryStorage.fileStat(at: HistoryStorage.maintenanceURL(in: directory.url)) == maintenanceBefore)- let before = try #require(HistoryStorage.fileStat(at: url))- let handle = try FileHandle(forWritingTo: url)- try handle.seek(toOffset: UInt64(#require(Data(contentsOf: url).firstIndex(of: 10)) + 1))- try handle.write(contentsOf: replacement)- try handle.close()- let secondTimes = [timespec(tv_sec: seconds, tv_nsec: 1000900), timespec(tv_sec: seconds, tv_nsec: 1000900)]- #expect(utimensat(AT_FDCWD, url.path, secondTimes, 0) == 0)- let after = try #require(HistoryStorage.fileStat(at: url))- #expect(before.modificationTime == after.modificationTime)- #expect(before.modifiedAtNanoseconds != after.modifiedAtNanoseconds)- _ = await service.loadSnapshotWithMaintenance(synchronize: false, trigger: .manual)- let rebuilt = try #require(JSONLines.decode(ActivityAggregate.self, from: Data(contentsOf: HistoryStorage.dailyURL(in: directory.url))).first)- #expect(rebuilt.toolStartedCount == 1)- #expect(rebuilt.turnStartedCount == 0)- #expect(rebuilt.generationID == stateBefore.days[date]?.generationID)- let restarted = HistoryService(directoryURL: directory.url, syncService: sync)- let maintenanceAfter = try #require(HistoryStorage.fileStat(at: HistoryStorage.maintenanceURL(in: directory.url)))- _ = await restarted.loadSnapshotWithMaintenance(synchronize: false, trigger: .manual)- #expect(HistoryStorage.fileStat(at: HistoryStorage.maintenanceURL(in: directory.url)) == maintenanceAfter)- }-- @Test func historyLockIsExclusiveAndReleasedWhenWorkThrows() throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- enum TestError: Error { case expected }- #expect(throws: TestError.self) {- try HistoryStorage.withExclusiveLock(in: directory.url) {- let descriptor = open(HistoryStorage.lockURL(in: directory.url).path, O_RDWR)- #expect(descriptor >= 0)- guard descriptor >= 0 else { throw TestError.expected }- defer { close(descriptor) }- let result = flock(descriptor, LOCK_EX | LOCK_NB)- let lockError = errno- #expect(result == -1)- #expect(lockError == EWOULDBLOCK)- throw TestError.expected- }- }- let descriptor = open(HistoryStorage.lockURL(in: directory.url).path, O_RDWR)- #expect(descriptor >= 0)- guard descriptor >= 0 else { return }- defer { close(descriptor) }- #expect(flock(descriptor, LOCK_EX | LOCK_NB) == 0)- #expect(flock(descriptor, LOCK_UN) == 0)- }-- @Test func lockFileOpenFailurePreservesPOSIXError() throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- for url in [HistoryStorage.lockURL(in: directory.url), directory.url.appendingPathComponent("store.lock")] {- try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)- }- #expect(throws: POSIXError(.EISDIR)) {- try HistoryStorage.withExclusiveLock(in: directory.url) {- Issue.record("Lock acquisition should fail before running the operation")- }- }- #expect(throws: POSIXError(.EISDIR)) {- try JSONFileStorage.withLock(in: directory.url) {- Issue.record("Lock acquisition should fail before running the operation")- }- }- }-- @Test func maintenanceNormalizesQueuesAndRejectsUnknownVersion() throws {- let state = try TestFixtures.decode(HistoryMaintenanceState.self, """- {"version":1,"pending":["2026-09-15","invalid","2026-09-14","2026-09-15"],"dirty":["2026-02-30","2026-09-14"]}- """)- #expect(state.pending == ["2026-09-14", "2026-09-15"])- #expect(state.dirty == ["2026-09-14"])- #expect(throws: StorageCompatibilityError.self) {- try TestFixtures.decode(HistoryMaintenanceState.self, "{\"version\":2}")- }- }-- @Test func sourceIdentitySurvivesMissingMaintenanceState() async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let recorder = ActivityRecorder(directoryURL: directory.url)- let event = ActivityRecord(- timestamp: Date(),- name: ActivityEventKind.turnStarted.rawValue,- origin: .main,- cwd: nil,- toolName: nil,- model: nil,- effort: nil,- threadID: "thread",- turnID: "turn",- agentID: nil,- id: "source-recovery"- )- try await recorder.record(event: event)- let service = HistoryService(directoryURL: directory.url, syncService: SyncService(isEnabled: { false }))- _ = await service.loadSnapshotWithMaintenance(synchronize: false, trigger: .manual)- let original = try HistoryStorage.loadMaintenanceState(in: directory.url)- try FileManager.default.removeItem(at: HistoryStorage.maintenanceURL(in: directory.url))- _ = await service.loadSnapshotWithMaintenance(synchronize: false, trigger: .manual)- let recovered = try HistoryStorage.loadMaintenanceState(in: directory.url)- #expect(recovered.days.mapValues(\.generationID) == original.days.mapValues(\.generationID))- }-- @Test func maintenanceStoragePreservesReadBoundaryAndFileIdentity() throws {- let date = "2026-09-15"- let state = HistoryMaintenanceState(pending: [date], dirty: [date], days: [date: HistoryDayMaintenanceState(- offset: 50, size: 60, corrupt: 2, generationID: "source",- fileIdentifier: 7- )])- let data = try JSONLines.stableEncoder.encode(state)- let root = try #require(JSONSerialization.jsonObject(with: data) as? [String: Any])- #expect(Set(root.keys) == ["version", "pending", "dirty", "days"])- let days = try #require(root["days"] as? [String: [String: Any]])- let day = try #require(days[date])- #expect(Set(day.keys) == [- "offset", "size", "corrupt", "generationID",- "fileIdentifier"- ])- #expect(try JSONDecoder().decode(HistoryMaintenanceState.self, from: data) == state)- }-- @Test func repeatedPendingAndDirtyUpdatesAreIdempotent() {- var state = HistoryMaintenanceState()- let result0 = state.markPending("2026-09-15")- #expect(result0)- let result1 = !state.markPending("2026-09-15")- #expect(result1)- let result2 = state.markDirty("2026-09-15")- #expect(result2)- let result3 = !state.markDirty("2026-09-15")- #expect(result3)- #expect(state.days["2026-09-15"]?.generationID == nil)- }-}diff --git a/CodexBarTests/History/JSONLinesAndActivityTests.swift b/CodexBarTests/History/JSONLinesAndActivityDataTests.swiftsimilarity index 60%rename from CodexBarTests/History/JSONLinesAndActivityTests.swiftrename to CodexBarTests/History/JSONLinesAndActivityDataTests.swiftindex db0f553..1aa8e00 100644--- a/CodexBarTests/History/JSONLinesAndActivityTests.swift+++ b/CodexBarTests/History/JSONLinesAndActivityDataTests.swift@@ -1,7 +1,7 @@import Foundationimport Testing-struct JSONLinesAndActivityTests {+struct JSONLinesAndActivityDataTests {@Test func millisecondTimestampsRemainStableAcrossRepeatedEncoding() throws {let base = Int64(TestFixtures.now.timeIntervalSince1970 * 1000)for offset in 0 ..< 1000 {@@ -12,25 +12,18 @@ struct JSONLinesAndActivityTests {}}- @Test func corruptUTF8DoesNotDiscardNeighboringJSONLines() {- let data = Data("1\n \t\r\n".utf8) + Data([0xFF, 0x0A]) + Data("{broken}\n2\n3".utf8)- let result = JSONLines.decodeWithFailures(Int.self, from: data)- #expect(result.values == [1, 2, 3])- #expect(result.failedLineCount == 2)- }-@Test(arguments: ["PermissionRequest", "permission_request", "permission-request", "approval.requested"])func eventAliasesAreNotAccepted(_ name: String) {- #expect(ActivityEventKind(eventName: name) == nil)- #expect(ActivityEventKind(eventName: "approvalRequested") == .approvalRequested)+ #expect(ActivityDataEventKind(eventName: name) == nil)+ #expect(ActivityDataEventKind(eventName: "approvalRequested") == .approvalRequested)}@Test func unknownEventNamesAreNotGuessed() {- #expect(ActivityEventKind(eventName: "future_event") == nil)+ #expect(ActivityDataEventKind(eventName: "future_event") == nil)}@Test func eventDecodingToleratesMissingMetadataAndUnknownOrigins() throws {- let event = try TestFixtures.decode(ActivityRecord.self, """+ let event = try TestFixtures.decode(ActivityDataRecord.self, """{"timestamp":0,"name":"turnStarted","origin":"future","threadID":" ","toolName":123}""")#expect(event.eventKind == .turnStarted)@@ -42,24 +35,28 @@ struct JSONLinesAndActivityTests {@Test(arguments: [#"{"name":"turnCompleted"}"#, #"{"timestamp":"invalid","name":"turnCompleted"}"#, #"{"timestamp":0,"name":" "}"#])func eventDecodingRequiresTimestampAndEvent(_ json: String) {#expect(throws: (any Error).self) {- try TestFixtures.decode(ActivityRecord.self, json)+ try TestFixtures.decode(ActivityDataRecord.self, json)}}@Test func eventSerializationRoundTripsEscapedMetadata() throws {- let event = ActivityRecord(+ let event = ActivityDataRecord(timestamp: TestFixtures.now, name: "toolStarted", origin: .main,cwd: "/projects/a\"b", toolName: "line\nbreak", model: nil,- effort: nil, threadID: "session", turnID: "turn", agentID: "agent"+ effort: nil, threadID: "thread", turnID: "turn", agentThreadID: "agent-thread")let data = try JSONLines.stableEncoder.encode(event)+ let payload = try #require(JSONSerialization.jsonObject(with: data) as? [String: Any])+ #expect(payload["threadID"] as? String == "thread")+ #expect(payload["turnID"] as? String == "turn")+ #expect(payload["agentThreadID"] as? String == "agent-thread")#expect(!data.contains(JSONLines.newlineByte))- #expect(try JSONLines.decoder.decode(ActivityRecord.self, from: data) == event)+ #expect(try JSONLines.decoder.decode(ActivityDataRecord.self, from: data) == event)}@Test func modelNameDoesNotClassifyTaskOrigin() throws {- let unknown = try TestFixtures.decode(ActivityRecord.self, #"{"timestamp":0,"name":"turnCompleted","model":"codex-auto-review"}"#)- let explicit = try TestFixtures.decode(ActivityRecord.self, #"{"timestamp":0,"name":"turnCompleted","model":"codex-auto-review","origin":"main"}"#)+ let unknown = try TestFixtures.decode(ActivityDataRecord.self, #"{"timestamp":0,"name":"turnCompleted","model":"codex-auto-review"}"#)+ let explicit = try TestFixtures.decode(ActivityDataRecord.self, #"{"timestamp":0,"name":"turnCompleted","model":"codex-auto-review","origin":"main"}"#)#expect(unknown.origin == .unknown)#expect(explicit.origin == .main)}diff --git a/CodexBarTests/History/StorageEvolutionTests.swift b/CodexBarTests/History/StorageEvolutionTests.swiftindex 7e6291d..3b720df 100644--- a/CodexBarTests/History/StorageEvolutionTests.swift+++ b/CodexBarTests/History/StorageEvolutionTests.swift@@ -2,63 +2,13 @@ import Foundationimport Testingstruct StorageEvolutionTests {- @Test(arguments: ["version", "aggregationVersion"])- func unsupportedAggregateIsNotRewrittenOrShownAsZero(_ field: String) async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- var aggregate = TestFixtures.aggregate()- aggregate = ActivityAggregate(date: HistoryStorage.dateKey(for: Date()), generationID: aggregate.generationID)- var object = try #require(JSONSerialization.jsonObject(with: aggregate.jsonLineData()) as? [String: Any])- object[field] = 999- let original = try JSONSerialization.data(withJSONObject: object)- let url = try directory.write(original, to: "Aggregates/activity.jsonl")- let history = HistoryService(directoryURL: directory.url, syncService: SyncService(isEnabled: { false }))- let result = await history.loadSnapshotWithMaintenance(synchronize: false, trigger: .manual)- #expect(!result.snapshot.isActivityComplete)- #expect(try Data(contentsOf: url) == original)- let days = UsageHeatmapDay.grid(usage: nil, history: result.snapshot, columnCount: 1, today: Date()).compactMap(\.self)- #expect(days.allSatisfy { $0.history.threadCount == nil && $0.history.toolCallCount == nil })- }-- @Test func unsupportedJournalCannotBeAppendedOrPruned() async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let now = Date()- let old = HistoryStorage.retentionCutoffDate(today: now).addingTimeInterval(-86400)- let date = HistoryStorage.dateKey(for: old)- let data = Data("{\"version\":999,\"date\":\"\(date)\",\"generationID\":\"source\"}\n".utf8)- let url = try directory.write(data, to: "Events/\(date).jsonl")- var journal = AppServerEventJournal()- #expect(throws: StorageCompatibilityError.self) {- try journal.append(AppServerEventRecord(activity: TestFixtures.event(at: old)), in: directory.url)- }- let history = HistoryService(directoryURL: directory.url, syncService: SyncService(isEnabled: { false }))- _ = await history.loadSnapshotWithMaintenance(synchronize: false, trigger: .manual, now: now)- #expect(try Data(contentsOf: url) == data)- }-- @Test func checkpointDetectsMiddleRewriteOutsideOldTailWindow() throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let url = try directory.write(Data(repeating: 97, count: 16384), to: "prefix")- let checkpoint = try ActivitySourceCheckpoint.read(at: url, byteCount: 16384)- let handle = try FileHandle(forWritingTo: url)- try handle.seek(toOffset: 10)- try handle.write(contentsOf: Data([98]))- try handle.close()- #expect(try ActivitySourceCheckpoint.read(at: url, byteCount: 16384) != checkpoint)- #expect(throws: StorageCompatibilityError.incompleteSource) {- try ActivitySourceCheckpoint.read(at: url, byteCount: 16385)- }- }-- @Test func tokenAlgorithmMismatchCannotBeMerged() throws {- let first = TestFixtures.tokenTurn(id: "turn", rootID: "turn", startedAt: Date(), updatedAt: Date(), usage: .zero)+ @Test func unsupportedTokenUsageAlgorithmCannotEnterLocalSnapshots() throws {+ let first = TestFixtures.tokenUsageAggregate(at: Date())var unsupported = firstunsupported.aggregationVersion = 999- #expect(throws: StorageCompatibilityError.self) { try first.merging(unsupported) }+ #expect(throws: StorageCompatibilityError.self) { try TokenUsageSyncRecord.dailyRecords(local: [first, unsupported], deviceID: "device") }#expect(throws: StorageCompatibilityError.self) {- try JSONLines.decoder.decode(TokenTurn.self, from: JSONLines.stableEncoder.encode(unsupported))+ try JSONLines.decoder.decode(TokenUsageAggregate.self, from: JSONLines.stableEncoder.encode(unsupported))}}diff --git a/CodexBarTests/History/StorageRetentionTests.swift b/CodexBarTests/History/StorageRetentionTests.swiftindex 8313942..bb806af 100644--- a/CodexBarTests/History/StorageRetentionTests.swift+++ b/CodexBarTests/History/StorageRetentionTests.swift@@ -3,50 +3,6 @@ import SQLite3import Testingstruct StorageRetentionTests {- @Test func dailyCleanupSkipsRepeatedRefreshButStillAggregatesNewEvents() async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let now = Date()- let tomorrow = try #require(Calendar.current.date(byAdding: .day, value: 1, to: now))- let expiredKey = HistoryStorage.dateKey(for: HistoryStorage.retentionCutoffDate(today: now).addingTimeInterval(-86400))- let sync = SyncService(directoryURL: HistoryStorage.syncDirectoryURL(in: directory.url), isEnabled: { false })- let service = HistoryService(directoryURL: directory.url, syncService: sync)- _ = await service.loadSnapshotWithMaintenance(synchronize: false, trigger: .manual, now: now)- let expiredURL = try directory.writeJournal(Data(), to: "Events/\(expiredKey).jsonl")- let recorder = ActivityRecorder(directoryURL: directory.url)- try await recorder.record(event: ActivityRecord(- timestamp: now, name: ActivityEventKind.turnStarted.rawValue, origin: .main,- cwd: nil, toolName: nil, model: nil, effort: nil, threadID: "session", turnID: "turn", agentID: nil, id: "new-event"- ))- _ = await service.loadSnapshotWithMaintenance(synchronize: false, trigger: .manual, now: now)- #expect(FileManager.default.fileExists(atPath: expiredURL.path))- let data = try Data(contentsOf: HistoryStorage.dailyURL(in: directory.url))- #expect(JSONLines.decode(ActivityAggregate.self, from: data).first { $0.date == HistoryStorage.dateKey(for: now) }?.turnStartedCount == 1)- _ = await service.loadSnapshotWithMaintenance(synchronize: false, trigger: .manual, now: tomorrow)- #expect(!FileManager.default.fileExists(atPath: expiredURL.path))- _ = try directory.writeJournal(Data(), to: "Events/\(expiredKey).jsonl")- let restarted = HistoryService(directoryURL: directory.url, syncService: sync)- _ = await restarted.loadSnapshotWithMaintenance(synchronize: false, trigger: .manual, now: tomorrow)- #expect(!FileManager.default.fileExists(atPath: expiredURL.path))- }-- @Test func failedEventCleanupRetriesDuringSameDay() async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let now = Date()- let date = HistoryStorage.dateKey(for: HistoryStorage.retentionCutoffDate(today: now).addingTimeInterval(-86400))- let expiredURL = try directory.writeJournal(Data(), to: "Events/\(date).jsonl")- let lockURL = HistoryStorage.lockURL(in: directory.url)- try FileManager.default.createDirectory(at: lockURL, withIntermediateDirectories: true)- let sync = SyncService(directoryURL: HistoryStorage.syncDirectoryURL(in: directory.url), isEnabled: { false })- let service = HistoryService(directoryURL: directory.url, syncService: sync)- _ = await service.loadSnapshotWithMaintenance(synchronize: false, trigger: .manual, now: now)- #expect(FileManager.default.fileExists(atPath: expiredURL.path))- try FileManager.default.removeItem(at: lockURL)- _ = await service.loadSnapshotWithMaintenance(synchronize: false, trigger: .manual, now: now)- #expect(!FileManager.default.fileExists(atPath: expiredURL.path))- }-@Test func cacheCleanupRunsDailyAndRetriesOnlyFailedStore() async throws {let directory = try TestDirectory()defer { try? directory.remove() }@@ -54,54 +10,35 @@ struct StorageRetentionTests {let tomorrow = try #require(Calendar.current.date(byAdding: .day, value: 1, to: now))let expired = HistoryStorage.retentionCutoffDate(today: now).addingTimeInterval(-86400)let key = HistoryStorage.dateKey(for: expired)- let activity = try JSONLines.stableEncoder.encode(ActivitySyncCache(state: SyncState(hashByDate: [key: "old"])))- let token = TokenTurn(id: "old", rootID: "old", updatedAt: expired)- let tokens = try JSONLines.stableEncoder.encode(SeededTokenCache(turns: [token.id: token]))- let activityURL = try directory.write(activity, to: "Activity/cache.json")- let tokenURL = try directory.write(Data(#"{"version":999}"#.utf8), to: "Tokens/cache.json")+ let activityDataRecord = try ActivityDataSyncRecord(+ deviceID: "device", activityData: ActivityDataAggregate(date: key).syncedAggregate,+ recordName: ActivityDataSyncRecord.recordName(deviceID: "device", date: key)+ )+ let activityDataCache = try JSONLines.stableEncoder.encode(ActivityDataSyncCache(+ state: ActivityDataSyncState(deviceID: "device"), records: [activityDataRecord.id: activityDataRecord]+ ))+ let tokenUsageRecord = TokenUsageSyncRecord(deviceID: "device", date: key, tokenUsage: .zero)+ let tokenUsageCacheData = try JSONLines.stableEncoder.encode(TokenUsageSyncCache(+ state: TokenUsageSyncState(deviceID: "device"), records: [tokenUsageRecord.id: tokenUsageRecord]+ ))+ let activityDataURL = try directory.write(activityDataCache, to: "activityData/cache.json")+ let tokenUsageURL = try directory.write(Data(#"{"schemaVersion":999}"#.utf8), to: "tokenUsage/cache.json")let sync = SyncService(directoryURL: directory.url, isEnabled: { false })await sync.pruneLocalCaches(now: now)- #expect(try JSONLines.decoder.decode(ActivitySyncCache.self, from: Data(contentsOf: activityURL)).state.hashByDate.isEmpty)- try activity.write(to: activityURL)- try tokens.write(to: tokenURL)+ #expect(try JSONLines.decoder.decode(ActivityDataSyncCache.self, from: Data(contentsOf: activityDataURL)).records.isEmpty)+ try activityDataCache.write(to: activityDataURL)+ try tokenUsageCacheData.write(to: tokenUsageURL)await sync.pruneLocalCaches(now: now)- #expect(try Data(contentsOf: activityURL) == activity)- #expect(try JSONLines.decoder.decode(SeededTokenCache.self, from: Data(contentsOf: tokenURL)).turns.isEmpty)- try tokens.write(to: tokenURL)+ #expect(try Data(contentsOf: activityDataURL) == activityDataCache)+ #expect(try JSONLines.decoder.decode(TokenUsageSyncCache.self, from: Data(contentsOf: tokenUsageURL)).records.isEmpty)+ try tokenUsageCacheData.write(to: tokenUsageURL)await sync.pruneLocalCaches(now: now)- #expect(try Data(contentsOf: tokenURL) == tokens)- _ = try ActivitySyncStore(directoryURL: directory.url).load()- #expect(try Data(contentsOf: activityURL) == activity)+ #expect(try Data(contentsOf: tokenUsageURL) == tokenUsageCacheData)+ _ = try ActivityDataSyncStore(directoryURL: directory.url).load()+ #expect(try Data(contentsOf: activityDataURL) == activityDataCache)await sync.pruneLocalCaches(now: tomorrow)- #expect(try JSONLines.decoder.decode(ActivitySyncCache.self, from: Data(contentsOf: activityURL)).state.hashByDate.isEmpty)- #expect(try JSONLines.decoder.decode(SeededTokenCache.self, from: Data(contentsOf: tokenURL)).turns.isEmpty)- }-- @Test func eventFilesAndOrphanedStateExpireAtCalendarBoundary() async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let cutoff = HistoryStorage.retentionCutoffDate()- let dates = [cutoff.addingTimeInterval(-86400), cutoff, Date()]- let keys = dates.map { HistoryStorage.dateKey(for: $0) }- for (date, key) in zip(dates, keys) {- let record = AppServerEventRecord(activity: TestFixtures.event(.turnStarted, at: date), recordedAt: date)- _ = try directory.writeJournal(record.jsonLineData(), to: "Events/\(key).jsonl")- }- let orphan = HistoryStorage.dateKey(for: cutoff.addingTimeInterval(-2 * 86400))- var state = HistoryMaintenanceState()- state.markPending(orphan)- state.markDirty(orphan)- try HistoryStorage.saveMaintenanceState(state, in: directory.url)- let sync = SyncService(directoryURL: HistoryStorage.syncDirectoryURL(in: directory.url), isEnabled: { false })- let service = HistoryService(directoryURL: directory.url, syncService: sync)- _ = await service.loadSnapshotWithMaintenance(synchronize: false, trigger: .manual)- #expect(HistoryStorage.eventLogDateKeys(in: HistoryStorage.eventsDirectoryURL(in: directory.url)) == Array(keys.dropFirst()))- let retained = try HistoryStorage.loadMaintenanceState(in: directory.url)- #expect(retained.days[orphan] == nil)- #expect(!retained.pending.contains(orphan))- #expect(!retained.dirty.contains(orphan))- let aggregates = try Data(contentsOf: HistoryStorage.dailyURL(in: directory.url))- #expect(JSONLines.decode(ActivityAggregate.self, from: aggregates).allSatisfy { $0.date >= keys[1] })+ #expect(try JSONLines.decoder.decode(ActivityDataSyncCache.self, from: Data(contentsOf: activityDataURL)).records.isEmpty)+ #expect(try JSONLines.decoder.decode(TokenUsageSyncCache.self, from: Data(contentsOf: tokenUsageURL)).records.isEmpty)}@Test(arguments: [false, true])@@ -132,7 +69,7 @@ struct StorageRetentionTests {#expect(try await reader.page().total == 1)}- @Test func disabledSyncStillPrunesBothLocalCachesAndKeepsRequiredTokenRoot() async throws {+ @Test func disabledSyncStillPrunesBothLocalCaches() async throws {let directory = try TestDirectory()defer { try? directory.remove() }let cutoff = HistoryStorage.retentionCutoffDate()@@ -140,45 +77,43 @@ struct StorageRetentionTests {let expiredKey = HistoryStorage.dateKey(for: expired)let retainedKey = HistoryStorage.dateKey(for: cutoff)let records = try [expiredKey, retainedKey].map { date in- let daily = try TestFixtures.decode(SyncedActivity.self, """- {"version":1,"aggregationVersion":1,"date":"\(date)","generationID":"source","eventCount":1}+ let daily = try TestFixtures.decode(SyncedActivityData.self, """+ {"schemaVersion":1,"aggregationVersion":1,"date":"\(date)","eventCount":1}""")- return try ActivitySyncRecord(- deviceID: "device", daily: daily,- recordName: ActivitySyncRecord.recordName(deviceID: "device", date: date, generation: "source")+ return try ActivityDataSyncRecord(+ deviceID: "device", activityData: daily,+ recordName: ActivityDataSyncRecord.recordName(deviceID: "device", date: date))}- let cache = ActivitySyncCache(state: SyncState(hashByDate: [expiredKey: "old", retainedKey: "keep"]), records: records)- _ = try directory.write(JSONLines.stableEncoder.encode(cache), to: "Sync/Activity/cache.json")- let turns = [- TokenTurn(id: "expired", rootID: "expired", updatedAt: expired),- TokenTurn(id: "root", rootID: "root", updatedAt: expired),- TokenTurn(id: "child", rootID: "root", updatedAt: cutoff)+ let cache = ActivityDataSyncCache(+ state: ActivityDataSyncState(deviceID: "device"),+ records: Dictionary(uniqueKeysWithValues: records.map { ($0.id, $0) })+ )+ _ = try directory.write(JSONLines.stableEncoder.encode(cache), to: "Sync/activityData/cache.json")+ let days = [+ TokenUsageSyncRecord(deviceID: "device", date: expiredKey, tokenUsage: .zero),+ TokenUsageSyncRecord(deviceID: "device", date: retainedKey, tokenUsage: .zero),+ TokenUsageSyncRecord(deviceID: "other", date: retainedKey, tokenUsage: .zero)]- let tokenCache = SeededTokenCache(turns: Dictionary(uniqueKeysWithValues: turns.map { ($0.id, $0) }))- _ = try directory.write(JSONLines.stableEncoder.encode(tokenCache), to: "Sync/Tokens/cache.json")+ let tokenUsageCache = TokenUsageSyncCache(+ state: TokenUsageSyncState(deviceID: "device"), records: Dictionary(uniqueKeysWithValues: days.map { ($0.id, $0) }),+ cursor: Data("cursor".utf8)+ )+ _ = try directory.write(JSONLines.stableEncoder.encode(tokenUsageCache), to: "Sync/tokenUsage/cache.json")let sync = SyncService(directoryURL: HistoryStorage.syncDirectoryURL(in: directory.url), isEnabled: { false })let service = HistoryService(directoryURL: directory.url, syncService: sync)_ = await service.loadSnapshotWithMaintenance(synchronize: false, trigger: .manual)- let activityData = try Data(contentsOf: directory.url.appendingPathComponent("Sync/Activity/cache.json"))- let activity = try JSONLines.decoder.decode(ActivitySyncCache.self, from: activityData)- #expect(activity.records.map(\.date) == [retainedKey])- #expect(activity.state.hashByDate == [retainedKey: "keep"])- let tokenData = try Data(contentsOf: directory.url.appendingPathComponent("Sync/Tokens/cache.json"))- let tokens = try JSONLines.decoder.decode(SeededTokenCache.self, from: tokenData)- #expect(Set(tokens.turns.keys) == ["root", "child"])- #expect(tokens.cursor == nil)+ let activityData = try Data(contentsOf: directory.url.appendingPathComponent("Sync/activityData/cache.json"))+ let activityDataCache = try JSONLines.decoder.decode(ActivityDataSyncCache.self, from: activityData)+ #expect(activityDataCache.sortedRecords.map(\.date) == [retainedKey])+ #expect(activityDataCache.state.deviceID == "device")+ let tokenUsageData = try Data(contentsOf: directory.url.appendingPathComponent("Sync/tokenUsage/cache.json"))+ let tokenUsageCacheData = try JSONLines.decoder.decode(TokenUsageSyncCache.self, from: tokenUsageData)+ #expect(Set(tokenUsageCacheData.records.keys) == ["tokenUsage_device_\(retainedKey)", "tokenUsage_other_\(retainedKey)"])+ #expect(tokenUsageCacheData.cursor == nil)}}-private nonisolated struct SeededTokenCache: Codable {- var version = 1- var accountScopedDeviceID = "device"- var salt = Data(repeating: 1, count: 32)- var turns: [String: TokenTurn]- var cursor: Data? = Data("cursor".utf8)-}-private final nonisolated class RetentionClock: @unchecked Sendable {private let lock = NSLock()private var value = Date()@@ -192,66 +127,4 @@ private final nonisolated class RetentionClock: @unchecked Sendable {}}-extension StorageRetentionTests {- @Test func eventDirectoryReadFailureDoesNotCompleteDailyCleanup() async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let eventsURL = try directory.write("not a directory", to: "Events")- #expect(throws: (any Error).self) { try HistoryStorage.readEventLogDateKeys(in: eventsURL) }- let now = Date()- let sync = SyncService(directoryURL: HistoryStorage.syncDirectoryURL(in: directory.url), isEnabled: { false })- let service = HistoryService(directoryURL: directory.url, syncService: sync)- _ = await service.loadSnapshotWithMaintenance(synchronize: false, trigger: .manual, now: now)- try FileManager.default.removeItem(at: eventsURL)- #expect(try HistoryStorage.readEventLogDateKeys(in: eventsURL).isEmpty)- let expired = HistoryStorage.dateKey(for: HistoryStorage.retentionCutoffDate(today: now).addingTimeInterval(-86400))- let expiredURL = try directory.writeJournal(Data(), to: "Events/\(expired).jsonl")- _ = await service.loadSnapshotWithMaintenance(synchronize: false, trigger: .manual, now: now)- #expect(!FileManager.default.fileExists(atPath: expiredURL.path))- }-- @Test func newDailyAggregateIncludesKnownZeroAborts() async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let now = Date()- let recorder = ActivityRecorder(directoryURL: directory.url)- let event = ActivityRecord(- timestamp: now,- name: ActivityEventKind.turnStarted.rawValue,- origin: .main,- cwd: nil,- toolName: nil,- model: nil,- effort: nil,- threadID: "session",- turnID: "turn",- agentID: nil,- id: "start"- )- try await recorder.record(event: event)- let sync = SyncService(directoryURL: HistoryStorage.syncDirectoryURL(in: directory.url), isEnabled: { false })- let service = HistoryService(directoryURL: directory.url, syncService: sync)- _ = await service.loadSnapshotWithMaintenance(synchronize: false, trigger: .manual)- let first = try #require(JSONLines.decode(ActivityAggregate.self, from: Data(contentsOf: HistoryStorage.dailyURL(in: directory.url))).first)- #expect(first.turnStartedCount == 1)- #expect(first.turnAbortedCount == 0)- let aborted = ActivityRecord(- timestamp: now,- name: ActivityEventKind.turnAborted.rawValue,- origin: .main,- cwd: nil,- toolName: nil,- model: nil,- effort: nil,- threadID: "session",- turnID: "turn",- agentID: nil,- id: "abort"- )- try await recorder.record(event: aborted)- _ = await service.loadSnapshotWithMaintenance(synchronize: false, trigger: .manual)- let second = try #require(JSONLines.decode(ActivityAggregate.self, from: Data(contentsOf: HistoryStorage.dailyURL(in: directory.url))).first)- #expect(second.turnAbortedCount == 1)- #expect(try HistoryStorage.loadMaintenanceState(in: directory.url).version == HistoryMaintenanceState.currentVersion)- }-}+extension StorageRetentionTests {}diff --git a/CodexBarTests/History/TokenHistoryTests.swift b/CodexBarTests/History/TokenHistoryTests.swiftdeleted file mode 100644index f07c62f..0000000--- a/CodexBarTests/History/TokenHistoryTests.swift+++ /dev/null@@ -1,704 +0,0 @@-import CloudKit-import CryptoKit-import Foundation-import Synchronization-import Testing--struct TokenHistoryTests {- @Test func usageWithoutObservationBoundaryIsRejectedLocallyAndInCloud() throws {- var value = turn("main", "a", input: 100)- value.checkpoint.removeAll()- let data = try JSONEncoder().encode(value)- #expect(throws: DecodingError.self) { try JSONDecoder().decode(TokenTurn.self, from: data) }- let record = CKRecord(recordType: TokenSync.recordType, recordID: .init(recordName: value.id))- TokenSync.apply(value, to: record)- #expect(throws: StorageCompatibilityError.self) { try TokenSync.turn(from: record) }- value.usage = nil- #expect(try JSONDecoder().decode(TokenTurn.self, from: JSONEncoder().encode(value)) == value)- }-- @Test func sameObservationBoundaryWithDifferentTotalsIsConflict() throws {- let first = turn("main", "a", input: 100)- var second = first- second.usage = usage(input: 200)- #expect(try first.merging(second).hasConflict)- #expect(try first.merging(second).usage == nil)- #expect(try second.merging(first).usage == nil)- }-- @Test func unchangedTokenStorageDoesNotReplaceFile() throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let url = directory.url.appendingPathComponent("cache.json")- let values = [turn("main", "a", input: 100)]- try JSONFileStorage.save(values, to: url)- let before = try #require(HistoryStorage.fileStat(at: url))- try JSONFileStorage.save(values, to: url)- let after = try #require(HistoryStorage.fileStat(at: url))- #expect(after.identifier == before.identifier)- #expect(after.modifiedAtNanoseconds == before.modifiedAtNanoseconds)- #expect(try JSONFileStorage.load([TokenTurn].self, from: url) == values)- }-- @Test func tokenIdentitiesMatchExistingEncodingExactly() throws {- let bytes = Data((0 ... 255).map(UInt8.init))- #expect(TokenTurn.hexString(bytes) == bytes.map { String(format: "%02x", $0) }.joined())- #expect(TokenTurn.hexString(Data()).isEmpty)- let salt = Data((0 ... 255).map(UInt8.init))- for thread in ["", "thread", "中文/路径", "a\u{0000}b", String(repeating: "x", count: 1024)] {- let data = try JSONEncoder().encode(["token-turn-v1", thread, "turn"])- let expected = SHA256.hash(data: data).map { String(format: "%02x", $0) }.joined()- #expect(TokenTurn.identifier(thread: thread, turn: "turn") == expected)- for root in [expected, "other-root"] {- let value = TokenTurn(id: expected, rootID: root, updatedAt: TestFixtures.now)- let exported = value.pseudonymized(salt: salt)- func legacyHash(_ text: String) -> String {- HMAC<SHA256>.authenticationCode(for: Data(text.utf8), using: SymmetricKey(data: salt))- .map { String(format: "%02x", $0) }.joined()- }- #expect(exported.id == legacyHash(expected))- #expect(exported.rootID == legacyHash(root))- #expect(exported.updatedAt == value.updatedAt)- }- }- }-- @Test func batchPseudonymsPreserveUpdatedValuesAndAccountIsolation() {- let root = turn("main", "a", input: 100)- let child = turn("child", "b", root: root.id, input: 200)- var updated = child- updated.usage = usage(input: 50)- updated.rebuiltAt = TestFixtures.now.addingTimeInterval(60)- let records = [root, child, updated]- for salt in [Data("account-a".utf8), Data("account-b".utf8), Data()] {- #expect(TokenTurn.pseudonymized(records, salt: salt) == records.map { $0.pseudonymized(salt: salt) })- }- }-- @Test func cachedTokenFileChecksBytesAcrossExternalWritesAndDeletion() throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let url = directory.url.appendingPathComponent("cache.json")- var first = TokenFileCache<[String: Int]>(url: url)- var second = TokenFileCache<[String: Int]>(url: url)- try first.save(["cursor": 1])- #expect(try first.load() == ["cursor": 1])- #expect(try second.load() == ["cursor": 1])- let initial = try #require(HistoryStorage.fileStat(at: url))- try first.save(["cursor": 1])- #expect(HistoryStorage.fileStat(at: url)?.identifier == initial.identifier)- let modifiedAt = try FileManager.default.attributesOfItem(atPath: url.path)[.modificationDate]- try Data("{\"cursor\":2}".utf8).write(to: url)- if let modifiedAt {- try FileManager.default.setAttributes([.modificationDate: modifiedAt], ofItemAtPath: url.path)- }- #expect(HistoryStorage.fileStat(at: url)?.size == initial.size)- #expect(try first.load() == ["cursor": 2])- #expect(try second.load() == ["cursor": 2])- try second.save(["cursor": 3])- #expect(try first.load() == ["cursor": 3])- // 内存命中不能跳过另一进程修改后的保存检查点- try second.save(["cursor": 4])- try first.save(["cursor": 3])- #expect(try second.load() == ["cursor": 3])- try Data("invalid".utf8).write(to: url)- #expect(throws: (any Error).self) { try first.load() }- try FileManager.default.removeItem(at: url)- #expect(try first.load() == nil)- try first.save(["cursor": 3])- #expect(try second.load() == ["cursor": 3])- }-- @Test func cloudRebuildMarkerOverridesStaleTotalsAndCanClearUsage() throws {- let stale = turn("main", "a", input: 500)- var rebuilt = turn("main", "a", input: 100)- rebuilt.checkpoint = stale.checkpoint- rebuilt.startNewGeneration(at: TestFixtures.now)- #expect(try stale.merging(rebuilt) == rebuilt)- #expect(try rebuilt.merging(stale) == rebuilt)- let exported = rebuilt.pseudonymized(salt: Data("account".utf8))- let record = CKRecord(recordType: TokenSync.recordType, recordID: .init(recordName: exported.id))- TokenSync.apply(exported, to: record)- #expect(try TokenSync.turn(from: record) == exported)- var cleared = exported- cleared.usage = nil- cleared.startNewGeneration(at: TestFixtures.now.addingTimeInterval(1))- TokenSync.apply(cleared, to: record)- #expect(try TokenSync.turn(from: record) == cleared)- #expect(try exported.merging(cleared).usage == nil)- #expect(TokenTurn.syncable([cleared]) == [cleared])- }-- @Test func cloudRecoveryMatchesAccountSaltAndRootIdentity() {- let local = turn("child", "b", root: TokenTurn.identifier(thread: "main", turn: "a"), input: 100)- var remote = local.pseudonymized(salt: Data("account-a".utf8))- remote.startNewGeneration(at: TestFixtures.now)- #expect(TokenHistoryBaseline(salt: Data("account-b".utf8), turns: [remote.id: remote]).replacement(for: local) == nil)- let baseline = TokenHistoryBaseline(salt: Data("account-a".utf8), turns: [remote.id: remote])- #expect(baseline.replacement(for: local)?.rootID == local.rootID)- var unknownRoot = local- unknownRoot.rootID = local.id- #expect(baseline.replacement(for: unknownRoot) == nil)- }-- @Test func sharedSyncLockExcludesConcurrentWritersWithoutBlockingCacheReads() throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let first = try #require(try JSONFileStorage.acquireLock(in: directory.url, name: "sync.lock", nonblocking: true))- let competing = try JSONFileStorage.acquireLock(in: directory.url, name: "sync.lock", nonblocking: true)- #expect(competing == nil)- if let competing {- JSONFileStorage.releaseLock(competing)- }- try JSONFileStorage.withLock(in: directory.url) {}- JSONFileStorage.releaseLock(first)- let next = try #require(try JSONFileStorage.acquireLock(in: directory.url, name: "sync.lock", nonblocking: true))- JSONFileStorage.releaseLock(next)- }-- @Test func cumulativeSnapshotsAndMultipleDevicesCountEachThreadTurnOnce() throws {- let root = turn("main", "a", input: 100)- var updated = turn("main", "a", input: 200)- updated.checkpoint["test-stream"]?.sequence = 2- updated.updatedAt = TestFixtures.now.addingTimeInterval(10)- let child = turn("child", "b", root: root.id, input: 300)- let result = TokenTurn.dailyUsage([root, updated, root, child, child], now: TestFixtures.now.addingTimeInterval(60))- #expect(result[day]?.totalTokens == 520)- #expect(try TokenTurn.merged([updated, root])[root.id] == TokenTurn.merged([root, updated])[root.id])- }-- @Test func allChildrenIncludingLaterTurnsBelongToRootStartDate() throws {- let start = try #require(CodexDateFormat.dayDate(from: "2026-09-15")?.addingTimeInterval(86390))- var root = turn("main", "a", input: 100)- root.startedAt = start- root.updatedAt = start.addingTimeInterval(30)- var child = turn("child", "b", root: root.id, input: 200)- child.startedAt = start.addingTimeInterval(20)- child.updatedAt = start.addingTimeInterval(80)- var followup = turn("child", "c", root: root.id, input: 300)- followup.startedAt = start.addingTimeInterval(40)- followup.updatedAt = start.addingTimeInterval(90)- let days = TokenTurn.dailyUsage([root, child, followup], now: start.addingTimeInterval(100))- #expect(days.count == 1)- #expect(days["2026-09-15"]?.totalTokens == 630)- #expect(TokenTurn.dailyUsage([child], now: start.addingTimeInterval(100)).isEmpty)- }-- @Test func cacheRateUsesDailyTotalsAndMissingUsageDoesNotBecomeZero() {- var a = turn("main", "a", input: 100)- var b = turn("other", "a", input: 300)- a.usage = usage(input: 100, cached: 80)- b.usage = usage(input: 300, cached: 30)- let merged = TokenTurn.dailyUsage([a, b], now: TestFixtures.now)- #expect(merged[day]?.cacheHitRate == 0.275)- a.usage = nil- #expect(TokenTurn.dailyUsage([a], now: TestFixtures.now).isEmpty)- a.usage = TokenUsage(inputTokens: 0, cachedInputTokens: 0, cacheWriteInputTokens: 0, outputTokens: 0, reasoningOutputTokens: 0, totalTokens: 0)- #expect(TokenTurn.dailyUsage([a], now: TestFixtures.now)[day]?.totalTokens == 0)- }-- @Test func cloudCodecUsesAccountScopedHashesAndRetainsAllCounters() throws {- let source = turn("private-thread", "private-turn", input: 100)- let exported = source.pseudonymized(salt: Data("account-a".utf8))- #expect(exported.id == exported.rootID)- #expect(exported.id != source.id)- #expect(exported.id != source.pseudonymized(salt: Data("account-b".utf8)).id)- let zone = CKRecordZone.ID(zoneName: "test", ownerName: CKCurrentUserDefaultName)- let id = TokenSync.recordID(exported.id, zoneID: zone)- #expect(id.recordName == exported.id)- let record = CKRecord(recordType: TokenSync.recordType, recordID: id)- TokenSync.apply(exported, to: record)- #expect(try TokenSync.turn(from: record) == exported)- #expect(Set(record.allKeys()) == [- "updatedAt",- "rootID",- "version",- "aggregationVersion",- "startedAt",- "usage",- "generationID",- "ancestorIDs",- "checkpoint",- "hasConflict"- ])- let cloudUsage = try #require(record["usage"] as? Data)- let cloudCounts = try #require(JSONSerialization.jsonObject(with: cloudUsage) as? [String: Int64])- #expect(Set(cloudCounts.keys) == [- "inputTokens", "cachedInputTokens", "cacheWriteInputTokens",- "outputTokens", "reasoningOutputTokens", "totalTokens"- ])- let text = try #require(String(data: JSONEncoder().encode(exported), encoding: .utf8))- #expect(!text.contains("private-thread"))- #expect(!text.contains("private-turn"))- record["version"] = 99 as CKRecordValue- #expect(record.recordID == TokenSync.recordID(exported.id, zoneID: zone))- #expect(throws: StorageCompatibilityError.self) { try TokenSync.turn(from: record) }- }-- @Test func childUsageWaitsForRootStartAndNeedsNoRootUsageRecord() {- var root = turn("main", "a", input: 100)- root.usage = nil- let child = turn("child", "b", root: root.id, input: 200)- #expect(TokenTurn.dailyUsage([child], now: TestFixtures.now).isEmpty)- #expect(TokenTurn.dailyUsage([root, child], now: TestFixtures.now)[day]?.totalTokens == 210)- #expect(Set(TokenTurn.syncable([root, child]).map(\.id)) == [root.id, child.id])- }-- @Test func dailyOverflowIsUnavailableAndExpiredTurnsAreExcluded() {- var root = turn("main", "a", input: 100)- root.usage = TokenUsage(- inputTokens: .max, cachedInputTokens: 0, cacheWriteInputTokens: 0,- outputTokens: 0, reasoningOutputTokens: 0, totalTokens: .max- )- let child = turn("child", "b", root: root.id, input: 200)- #expect(TokenTurn.dailyUsage([root, child], now: TestFixtures.now).isEmpty)- root.startedAt = TestFixtures.now.addingTimeInterval(-220 * 86400)- #expect(TokenTurn.dailyUsage([root], now: TestFixtures.now).isEmpty)- }-- @Test func heatmapSeparatesAccountIntensityFromLocalDailyMetrics() throws {- var history = HistorySnapshot.empty- history.tokenUsageByDate = [day: usage(input: 100)]- let grid = UsageHeatmapDay.grid(usage: nil, history: history, columnCount: 2, today: TestFixtures.now)- let today = try #require(grid.compactMap(\.self).first { $0.startDate == day })- #expect(today.tokenState == .unavailable)- #expect(today.tokenUsage?.totalTokens == 110)- #expect(today.history.turnCount == 0)- }-- private var day: String {- CodexDateFormat.dayString(from: TestFixtures.now)- }-- private func usage(input: Int64, cached: Int64 = 20) -> TokenUsage {- TokenUsage(inputTokens: input, cachedInputTokens: cached, cacheWriteInputTokens: 5, outputTokens: 10, reasoningOutputTokens: 2, totalTokens: input + 10)- }-- private func turn(_ thread: String, _ turn: String, root: String? = nil, input: Int64) -> TokenTurn {- let id = TokenTurn.identifier(thread: thread, turn: turn)- return TestFixtures.tokenTurn(id: id, rootID: root ?? id, startedAt: TestFixtures.now, updatedAt: TestFixtures.now, usage: usage(input: input))- }-}--extension TokenHistoryTests {- @Test func unknownCacheVersionIsNotOverwritten() async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let data = Data(#"{"version":999,"turns":{},"files":{}}"#.utf8)- let url = try directory.write(data, to: "Aggregates/tokens.json")- let store = TokenHistoryStore(directoryURL: directory.url)- await #expect(throws: TokenCacheError.self) { try await store.refresh() }- #expect(try Data(contentsOf: url) == data)- }-- @Test func cacheReadFailureIsNotReplacedWithEmptyData() async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let url = directory.url.appendingPathComponent("Aggregates/tokens.json")- try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)- let store = TokenHistoryStore(directoryURL: directory.url)- await #expect(throws: (any Error).self) { try await store.refresh() }- #expect(try (url.resourceValues(forKeys: [.isDirectoryKey])).isDirectory == true)- }-- @Test func incompleteJournalDoesNotOverwriteDamagedCache() async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let cache = try directory.write("broken cache", to: "Aggregates/tokens.json")- _ = try directory.writeJournal(Data("broken event\n".utf8), to: "Events/\(HistoryStorage.dateKey(for: Date())).jsonl")- let store = TokenHistoryStore(directoryURL: directory.url)- await #expect(throws: TokenCacheError.self) { try await store.refresh() }- #expect(try String(contentsOf: cache, encoding: .utf8) == "broken cache")- }-}--extension TokenHistoryTests {- private func observation(sequence: Int64, previous: Int64, current: Int64, stream: String = "stream") -> TokenObservation {- let id = TokenTurn.identifier(thread: "observed", turn: "turn")- func counts(_ count: Int64) -> TokenUsage {- TokenUsage(- inputTokens: count,- cachedInputTokens: 0,- cacheWriteInputTokens: 0,- outputTokens: 0,- reasoningOutputTokens: 0,- totalTokens: count- )- }- return TokenObservation(- turn: TokenTurn(id: id, rootID: id, startedAt: TestFixtures.now, updatedAt: TestFixtures.now),- rootStartedAt: TestFixtures.now,- streamID: stream,- sequence: sequence,- previous: counts(previous),- current: counts(current)- )- }-- @Test func correctionAndRetryPreserveOnlyUncoveredUsageAcrossRestart() async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let store = TokenHistoryStore(directoryURL: directory.url)- let first = observation(sequence: 1, previous: 0, current: 100)- var corrected = try #require(try await store.recordObservations([first], now: TestFixtures.now).first)- corrected.usage = observation(sequence: 1, previous: 0, current: 60).current- corrected.startNewGeneration(at: TestFixtures.now.addingTimeInterval(-60))- let salt = Data("test".utf8)- let remote = corrected.pseudonymized(salt: salt)- _ = try await store.refresh(now: TestFixtures.now, baseline: TokenHistoryBaseline(salt: salt, turns: [remote.id: remote]))- let second = observation(sequence: 2, previous: 100, current: 110)- let result = try await store.recordObservations([first, second, second], now: TestFixtures.now)- #expect(result.first?.usage?.totalTokens == 70)- #expect(result.first?.generationID == corrected.generationID)- let restarted = TokenHistoryStore(directoryURL: directory.url)- #expect(try await restarted.recordObservations([second], now: TestFixtures.now).first?.usage?.totalTokens == 70)- try FileManager.default.removeItem(at: directory.url.appendingPathComponent("Aggregates/tokens.json"))- let recovered = try #require(try await restarted.refresh(now: TestFixtures.now).first)- #expect(recovered.usage?.totalTokens == 110)- #expect(recovered.ancestorIDs.contains(corrected.generationID))- }-- @Test func remoteCoveredObservationIsNotAddedTwice() async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let store = TokenHistoryStore(directoryURL: directory.url)- let first = observation(sequence: 1, previous: 0, current: 100)- let second = observation(sequence: 2, previous: 100, current: 110)- let local = try #require(try await store.recordObservations([first], now: TestFixtures.now).first)- var corrected = try second.applying(to: local)- corrected.startNewGeneration(at: TestFixtures.now)- corrected.usage = observation(sequence: 1, previous: 0, current: 60).current- let salt = Data("test".utf8)- let remote = corrected.pseudonymized(salt: salt)- _ = try await store.refresh(now: TestFixtures.now, baseline: TokenHistoryBaseline(salt: salt, turns: [remote.id: remote]))- #expect(try await store.recordObservations([second], now: TestFixtures.now).first?.usage?.totalTokens == 60)- }-- @Test func rebuildRecomputesFromCountersAndRejectsIncompleteCoverage() async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let store = TokenHistoryStore(directoryURL: directory.url)- let first = observation(sequence: 1, previous: 1000, current: 1100)- var bad = try #require(try await store.recordObservations([first], now: TestFixtures.now).first)- bad.startNewGeneration(at: TestFixtures.now)- bad.usage = observation(sequence: 1, previous: 0, current: 900).current- try await directory.seedTokenSnapshots([bad], now: TestFixtures.now)- _ = try await store.rebuild(for: [day], now: TestFixtures.now)- let restored = try #require(try await store.currentTurns(now: TestFixtures.now).first)- #expect(restored.usage?.totalTokens == 100)- #expect(restored.generationID != bad.generationID)- let second = observation(sequence: 2, previous: 1100, current: 1120)- var remote = try second.applying(to: restored)- remote.startNewGeneration(at: TestFixtures.now)- try await directory.seedTokenSnapshots([remote], now: TestFixtures.now)- let failed = try await store.rebuild(for: [day], now: TestFixtures.now)- #expect(failed.failedDateKeys == [day])- #expect(failed.turnCount == 0)- #expect(try await store.currentTurns(now: TestFixtures.now).first == remote)- }-- @Test func concurrentCorrectionsConvergeToExplicitConflictWithoutClockOrdering() throws {- let original = try observation(sequence: 1, previous: 0, current: 100).applying(to: nil)- var a = original- var b = original- a.startNewGeneration(at: TestFixtures.now.addingTimeInterval(9999))- b.startNewGeneration(at: TestFixtures.now.addingTimeInterval(-9999))- a.usage = observation(sequence: 1, previous: 0, current: 60).current- b.usage = observation(sequence: 1, previous: 0, current: 80).current- let merged = try a.merging(b)- #expect(merged.hasConflict)- #expect(merged.usage == nil)- #expect(try merged.generationID == b.merging(a).generationID)- #expect(try merged.merging(a).generationID == merged.generationID)- #expect(try merged.merging(b).generationID == merged.generationID)- #expect(TokenTurn.dailyUsage([merged], now: TestFixtures.now).isEmpty)- }-- @Test func missingObservationCannotAdvanceCheckpoint() async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let store = TokenHistoryStore(directoryURL: directory.url)- let missingFirst = observation(sequence: 2, previous: 100, current: 110)- #expect(try await store.recordObservations([missingFirst], now: TestFixtures.now).isEmpty)- #expect(try await store.currentTurns(now: TestFixtures.now).isEmpty)- }-}--extension TokenHistoryTests {- @Test func staleSnapshotRebasesOnlyItsUncoveredCheckpointTail() throws {- let original = try observation(sequence: 1, previous: 0, current: 100).applying(to: nil)- var correction = original- correction.startNewGeneration(at: TestFixtures.now)- correction.usage = observation(sequence: 1, previous: 0, current: 60).current- let stale = try observation(sequence: 2, previous: 100, current: 110).applying(to: original)- let merged = try correction.merging(stale)- #expect(merged.usage?.totalTokens == 70)- #expect(merged.generationID == correction.generationID)- #expect(try stale.merging(correction) == merged)- #expect(try merged.merging(stale) == merged)- #expect(try merged.merging(correction) == merged)- }-}--extension TokenHistoryTests {- private func rebuildObservation(- _ name: String, root: String? = nil, start: Date = TestFixtures.now,- sequence: Int64 = 1, previous: Int64 = 0, current: Int64 = 10- ) -> TokenObservation {- let id = TokenTurn.identifier(thread: name, turn: "rebuild")- let rootID = TokenTurn.identifier(thread: root ?? name, turn: "rebuild")- let counts = observation(sequence: sequence, previous: previous, current: current)- return TokenObservation(- turn: TokenTurn(id: id, rootID: rootID, startedAt: start, updatedAt: TestFixtures.now),- rootStartedAt: start, streamID: name, sequence: sequence, previous: counts.previous, current: counts.current- )- }-- private func writeJournal(_ observations: [TokenObservation], in directory: TestDirectory, at date: Date = TestFixtures.now) throws {- let data = try observations.reduce(into: Data()) { result, observation in- try result.append(AppServerEventRecord(observation: observation, recordedAt: date).jsonLineData())- }- _ = try directory.writeJournal(data, to: "Events/\(HistoryStorage.dateKey(for: date)).jsonl")- }-- @Test func unselectedIncompleteRootDoesNotBlockRebuildOrSubsequentCacheLoad() async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let selected = rebuildObservation("selected")- let unselected = rebuildObservation("other", start: TestFixtures.now.addingTimeInterval(-86400), sequence: 2, previous: 10, current: 20)- try writeJournal([selected, unselected], in: directory)- let store = TokenHistoryStore(directoryURL: directory.url)- let result = try await store.rebuild(for: [day], now: TestFixtures.now)- #expect(result.failedDateKeys.isEmpty)- #expect(result.turnCount == 1)- let expected = try #require(result.turns?.first { $0.id == selected.turn.id })- #expect(expected.usage?.totalTokens == 10)- let restarted = TokenHistoryStore(directoryURL: directory.url)- let loaded = try await restarted.refresh(now: TestFixtures.now)- #expect(loaded == [expected])- let cache = try #require(JSONSerialization.jsonObject(with: Data(contentsOf: directory.url.appendingPathComponent("Aggregates/tokens.json"))) as? [String: Any])- #expect((cache["files"] as? [String: Any])?.isEmpty == true)- }-- @Test func failedRootPreservesOldResultWhileSameDayHealthyRootIsRebuilt() async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let store = TokenHistoryStore(directoryURL: directory.url)- let previous = rebuildObservation("bad")- let old = try #require(try await store.recordObservations([previous], now: TestFixtures.now).first)- let good = rebuildObservation("good")- let broken = rebuildObservation("bad", sequence: 3, previous: 20, current: 30)- try writeJournal([good, broken], in: directory)- let result = try await store.rebuild(for: [day], now: TestFixtures.now)- #expect(result.failedDateKeys == [day])- #expect(result.turnCount == 1)- #expect(result.turns?.first { $0.id == old.id } == old)- let rebuilt = try #require(result.turns?.first { $0.id == good.turn.id })- #expect(rebuilt.generationID != "initial")- let reopened = TokenHistoryStore(directoryURL: directory.url)- let loaded = try await reopened.refresh(now: TestFixtures.now)- #expect(loaded.first { $0.id == old.id } == old)- #expect(loaded.first { $0.id == rebuilt.id } == rebuilt)- }-- @Test func rebuildIncludesChildObservationsWrittenOnAnotherDay() async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let root = rebuildObservation("root")- let child = rebuildObservation("child", root: "root", current: 20)- try writeJournal([root], in: directory)- try writeJournal([child], in: directory, at: TestFixtures.now.addingTimeInterval(86400))- let store = TokenHistoryStore(directoryURL: directory.url)- let result = try await store.rebuild(for: [day], now: TestFixtures.now.addingTimeInterval(86400))- #expect(result.failedDateKeys.isEmpty)- #expect(result.turnCount == 2)- #expect(TokenTurn.dailyUsage(result.turns ?? [], now: TestFixtures.now.addingTimeInterval(86400))[day]?.totalTokens == 30)- }-- @Test func missingCachedChildPreventsReplacingItsWholeRoot() async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let store = TokenHistoryStore(directoryURL: directory.url)- let root = rebuildObservation("root")- let child = rebuildObservation("child", root: "root")- let before = try await store.recordObservations([root, child], now: TestFixtures.now)- try writeJournal([root], in: directory)- let result = try await store.rebuild(for: [day], now: TestFixtures.now)- #expect(result.failedDateKeys == [day])- #expect(result.turnCount == 0)- #expect(Set(result.turns?.map(\.generationID) ?? []) == Set(before.map(\.generationID)))- #expect(result.turns?.first { $0.id == child.turn.id } == before.first { $0.id == child.turn.id })- }-- @Test func failedRootRollsBackAcrossFilesAndRetainsCursorsUntilRepair() async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let first = rebuildObservation("root")- let second = rebuildObservation("root", sequence: 2, previous: 10, current: 20)- let third = rebuildObservation("root", sequence: 3, previous: 20, current: 30)- let fourth = rebuildObservation("root", sequence: 4, previous: 30, current: 40)- let store = TokenHistoryStore(directoryURL: directory.url)- let before = try #require(try await store.recordObservations([first], now: TestFixtures.now).first)- let cacheURL = directory.url.appendingPathComponent("Aggregates/tokens.json")- let previousCache = try #require(JSONSerialization.jsonObject(with: Data(contentsOf: cacheURL)) as? [String: Any])- let previousFiles = try #require(previousCache["files"] as? NSDictionary)- try writeJournal([first, second], in: directory)- let nextDay = TestFixtures.now.addingTimeInterval(86400)- try writeJournal([fourth], in: directory, at: nextDay)- let partial = try await store.refresh(now: nextDay)- #expect(partial == [before])- let cache = try #require(JSONSerialization.jsonObject(with: Data(contentsOf: cacheURL)) as? [String: Any])- #expect(cache["files"] as? NSDictionary == previousFiles)- try writeJournal([third, fourth], in: directory, at: nextDay)- let restarted = TokenHistoryStore(directoryURL: directory.url)- let repaired = try await restarted.refresh(now: nextDay)- #expect(repaired.first?.usage?.totalTokens == 40)- #expect(try await restarted.refresh(now: nextDay) == repaired)- }-- @Test func unattributedCorruptionAndConflictingOwnershipDoNotOverwriteLedger() async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let store = TokenHistoryStore(directoryURL: directory.url)- let first = rebuildObservation("root")- _ = try await store.recordObservations([first], now: TestFixtures.now)- let cacheURL = directory.url.appendingPathComponent("Aggregates/tokens.json")- let before = try Data(contentsOf: cacheURL)- let eventURL = HistoryStorage.eventLogURL(for: day, in: HistoryStorage.eventsDirectoryURL(in: directory.url))- let broken = try FileHandle(forWritingTo: eventURL)- try broken.seekToEnd()- try broken.write(contentsOf: Data("broken record\n".utf8))- try broken.close()- await #expect(throws: TokenCacheError.self) { try await store.rebuild(for: [day], now: TestFixtures.now) }- #expect(try Data(contentsOf: cacheURL) == before)- let ambiguous = rebuildObservation("root", root: "different")- try writeJournal([first, ambiguous], in: directory)- await #expect(throws: TokenCacheError.self) { try await store.rebuild(for: [day], now: TestFixtures.now) }- #expect(try Data(contentsOf: cacheURL) == before)- }-}--extension TokenHistoryTests {- @Test(arguments: [false, true])- func rootIdentityCannotChangeAcrossMergeGenerations(rebuilt: Bool) throws {- let original = turn("root", "turn", input: 15)- var incoming = original- incoming.rootID = "different-root"- if rebuilt {- incoming.startNewGeneration(at: TestFixtures.now)- }- #expect(throws: TokenCacheError.self) { try original.merging(incoming) }- #expect(throws: TokenCacheError.self) { try incoming.merging(original) }- #expect(TokenTurn.dailyUsage([original, incoming], now: TestFixtures.now).isEmpty)- #expect(TokenTurn.dailyUsage([incoming, original], now: TestFixtures.now).isEmpty)- var otherChild = original- otherChild.rootID = "another-root"- #expect(throws: TokenCacheError.self) { try incoming.merging(otherChild) }- let observation = TokenObservation(- turn: incoming, rootStartedAt: TestFixtures.now, streamID: "test-stream", sequence: 1,- previous: .zero, current: usage(input: 15)- )- #expect(throws: TokenCacheError.self) { try observation.applying(to: original) }- }-}--extension TokenHistoryTests {- @Test(arguments: [true, false], [true, false])- func cacheRecoveryRecomputesMixedSnapshots(snapshotsFirst: Bool, damagedCache: Bool) async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let store = TokenHistoryStore(directoryURL: directory.url)- let first = observation(sequence: 1, previous: 0, current: 10)- _ = try await store.recordObservations([first], now: TestFixtures.now)- let cacheURL = directory.url.appendingPathComponent("Aggregates/tokens.json")- var cache = try #require(JSONSerialization.jsonObject(with: Data(contentsOf: cacheURL)) as? [String: Any])- var turns = try #require(cache["turns"] as? [String: Any])- var previous = try #require(turns[first.turn.id] as? [String: Any])- previous["aggregationVersion"] = 1- previous["usage"] = try JSONSerialization.jsonObject(with: JSONEncoder().encode(observation(sequence: 1, previous: 0, current: 12).current))- var checkpoints = try #require(previous["checkpoint"] as? [String: Any])- var point = try #require(checkpoints["stream"] as? [String: Any])- point["aggregationRanges"] = [["version": 1, "end": 1]]- checkpoints["stream"] = point- previous["checkpoint"] = checkpoints- turns[first.turn.id] = previous- cache["turns"] = turns- try JSONSerialization.data(withJSONObject: cache).write(to: cacheURL)-- let second = observation(sequence: 2, previous: 10, current: 15)- let mixed = try #require(try await store.recordObservations([second], now: TestFixtures.now).first)- #expect(mixed.usage?.totalTokens == 17)- #expect(mixed.aggregationVersion == AggregationVersion.tokens)- #expect(mixed.checkpoint["stream"]?.aggregationRanges.first?.version == 1)- #expect(mixed.checkpoint["stream"]?.aggregationRanges.last?.version == AggregationVersion.tokens)- let salt = Data("account".utf8)- var remote = mixed.pseudonymized(salt: salt)- remote.updatedAt = TestFixtures.now.addingTimeInterval(1)- _ = try await store.refresh(now: TestFixtures.now, baseline: TokenHistoryBaseline(salt: salt, turns: [remote.id: remote]))- let path = "Events/\(day).jsonl"- var entries: [AppServerEventRecord] = []- try AppServerEventJournal.read(at: directory.url.appendingPathComponent(path)) { entries.append($0) }- #expect(entries.contains { $0.token?.usage?.totalTokens == 17 })- if snapshotsFirst {- entries = entries.filter { $0.token != nil } + entries.filter { $0.token == nil }- try directory.writeJournal(entries.reduce(into: Data()) { try $0.append($1.jsonLineData()) }, to: path)- }- if damagedCache {- try Data("broken cache".utf8).write(to: cacheURL)- } else {- try FileManager.default.removeItem(at: cacheURL)- }- let recovered = try #require(try await store.refresh(now: TestFixtures.now).first)- #expect(recovered.usage?.totalTokens == 15)- #expect(!recovered.hasConflict)- #expect(recovered.ancestorIDs.contains(mixed.generationID))- #expect(recovered.checkpoint["stream"]?.aggregationRanges == [.init(version: AggregationVersion.tokens, end: 2)])- #expect(await store.pendingRecoveryIDs() == [recovered.id])- let reopened = TokenHistoryStore(directoryURL: directory.url)- #expect(try await reopened.refresh(now: TestFixtures.now) == [recovered])- #expect(try await reopened.refresh(now: TestFixtures.now) == [recovered])- let third = observation(sequence: 3, previous: 15, current: 20)- let continued = try #require(try await reopened.recordObservations([second, third, third], now: TestFixtures.now).first)- #expect(continued.usage?.totalTokens == 20)- #expect(continued.generationID == recovered.generationID)- }-- @Test(arguments: [true, false])- func unreadableEventsDirectoryPreservesCache(hasValidCache: Bool) async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let store = TokenHistoryStore(directoryURL: directory.url)- _ = try await store.recordObservations([observation(sequence: 1, previous: 0, current: 10)], now: TestFixtures.now)- let cacheURL = directory.url.appendingPathComponent("Aggregates/tokens.json")- if !hasValidCache {- try Data("broken cache".utf8).write(to: cacheURL)- }- let before = try Data(contentsOf: cacheURL)- let events = directory.url.appendingPathComponent("Events")- try FileManager.default.setAttributes([.posixPermissions: 0o000], ofItemAtPath: events.path)- defer { try? FileManager.default.setAttributes([.posixPermissions: 0o700], ofItemAtPath: events.path) }- #expect(throws: (any Error).self) { try HistoryStorage.readEventLogDateKeys(in: events) }- await #expect(throws: (any Error).self) { try await store.refresh(now: TestFixtures.now) }- #expect(try Data(contentsOf: cacheURL) == before)- try FileManager.default.setAttributes([.posixPermissions: 0o700], ofItemAtPath: events.path)- #expect(try await store.refresh(now: TestFixtures.now).first?.usage?.totalTokens == 10)- }-- @Test func cacheRecoveryPreservesWholeRootWhenChildObservationsAreMissing() async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let root = rebuildObservation("root")- let child = rebuildObservation("child", root: "root")- let store = TokenHistoryStore(directoryURL: directory.url)- let before = try await store.recordObservations([root, child], now: TestFixtures.now)- var data = try AppServerEventRecord(observation: root, recordedAt: TestFixtures.now).jsonLineData()- for turn in before {- try data.append(AppServerEventRecord(token: turn, recordedAt: TestFixtures.now).jsonLineData())- }- try directory.writeJournal(data, to: "Events/\(day).jsonl")- try FileManager.default.removeItem(at: directory.url.appendingPathComponent("Aggregates/tokens.json"))- let restored = try await store.refresh(now: TestFixtures.now)- #expect(try TokenTurn.merged(restored) == TokenTurn.merged(before))- #expect(await store.pendingRecoveryIDs().isEmpty)- }-}diff --git a/CodexBarTests/History/TokenUsageHistoryTests.swift b/CodexBarTests/History/TokenUsageHistoryTests.swiftnew file mode 100644index 0000000..86f0776--- /dev/null+++ b/CodexBarTests/History/TokenUsageHistoryTests.swift@@ -0,0 +1,90 @@+import Foundation+import Testing++struct TokenUsageHistoryTests {+ private let day = HistoryStorage.dateKey(for: TestFixtures.now)+ @Test func cachedTokenUsageFileChecksBytesAcrossExternalWritesAndDeletion() throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let url = directory.url.appendingPathComponent("cache.json")+ var first = TokenUsageFileCache<[String: Int]>(url: url)+ var second = TokenUsageFileCache<[String: Int]>(url: url)+ try first.save(["cursor": 1])+ #expect(try first.load() == ["cursor": 1])+ #expect(try second.load() == ["cursor": 1])+ let initial = try url.resourceValues(forKeys: [.fileSizeKey, .contentModificationDateKey])+ try first.save(["cursor": 1])+ #expect(try (url.resourceValues(forKeys: [.fileSizeKey])).fileSize == initial.fileSize)+ let modifiedAt = try FileManager.default.attributesOfItem(atPath: url.path)[.modificationDate]+ try Data("{\"cursor\":2}".utf8).write(to: url)+ if let modifiedAt {+ try FileManager.default.setAttributes([.modificationDate: modifiedAt], ofItemAtPath: url.path)+ }+ #expect(try url.resourceValues(forKeys: [.fileResourceIdentifierKey, .fileSizeKey, .contentModificationDateKey]).fileSize == initial.fileSize)+ #expect(try first.load() == ["cursor": 2])+ #expect(try second.load() == ["cursor": 2])+ try second.save(["cursor": 3])+ #expect(try first.load() == ["cursor": 3])+ // 内存命中不能跳过另一进程修改后的保存检查点+ try second.save(["cursor": 4])+ try first.save(["cursor": 3])+ #expect(try second.load() == ["cursor": 3])+ try Data("invalid".utf8).write(to: url)+ #expect(throws: (any Error).self) { try first.load() }+ try FileManager.default.removeItem(at: url)+ #expect(try first.load() == nil)+ try first.save(["cursor": 3])+ #expect(try second.load() == ["cursor": 3])+ }++ @Test func sharedSyncLockExcludesConcurrentWritersWithoutBlockingCacheReads() throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let first = try #require(try JSONFileStorage.acquireLock(in: directory.url, name: "sync.lock", nonblocking: true))+ let competing = try JSONFileStorage.acquireLock(in: directory.url, name: "sync.lock", nonblocking: true)+ #expect(competing == nil)+ if let competing {+ JSONFileStorage.releaseLock(competing)+ }+ try JSONFileStorage.withLock(in: directory.url) {}+ JSONFileStorage.releaseLock(first)+ let next = try #require(try JSONFileStorage.acquireLock(in: directory.url, name: "sync.lock", nonblocking: true))+ JSONFileStorage.releaseLock(next)+ }++ @Test func heatmapSeparatesAccountIntensityFromLocalDailyMetrics() throws {+ var history = HistorySnapshot.empty+ history.tokenUsageByDate = [day: tokenUsage(input: 100)]+ let grid = UsageHeatmapDay.grid(usage: nil, history: history, columnCount: 2, today: TestFixtures.now)+ let today = try #require(grid.compactMap(\.self).first { $0.startDate == day })+ #expect(today.tokenState == .unavailable)+ #expect(today.tokenUsage?.totalTokens == 110)+ #expect(today.activityData.turnCount == 0)+ }++ private func tokenUsage(input: Int64, cached: Int64 = 20) -> TokenUsage {+ TokenUsage(inputTokens: input, cachedInputTokens: cached, cacheWriteInputTokens: 5, outputTokens: 10, reasoningOutputTokens: 2, totalTokens: input + 10)+ }++ @Test func dailyAggregatesPreserveCountersAndRetention() throws {+ let now = TestFixtures.now+ var current = TestFixtures.tokenUsageAggregate(at: now, tokenUsage: tokenUsage(input: 100))+ current.position = 42+ let old = TestFixtures.tokenUsageAggregate(at: now.addingTimeInterval(-220 * 86400), tokenUsage: tokenUsage(input: 300))+ let future = TestFixtures.tokenUsageAggregate(at: now.addingTimeInterval(86400), tokenUsage: tokenUsage(input: 500))+ #expect(TokenUsageAggregate.tokenUsageByDate([old, current, future], now: now) == [day: current.tokenUsage])+ #expect(try JSONLines.decoder.decode(TokenUsageAggregate.self, from: JSONLines.stableEncoder.encode(current)) == current)+ let record = try #require(TokenUsageSyncRecord.dailyRecords(local: [current], deviceID: "device", now: now).first)+ let fields = try #require(JSONSerialization.jsonObject(with: JSONLines.stableEncoder.encode(record)) as? [String: Any])+ #expect(fields["position"] == nil)+ }++ @Test func invalidCountersCannotReplaceCommittedAggregate() throws {+ var value = TokenUsageAggregate(date: day)+ let invalid = TokenUsage(inputTokens: 100, cachedInputTokens: 0, cacheWriteInputTokens: 0, outputTokens: 0, reasoningOutputTokens: 0, totalTokens: 50)+ let observation = TokenUsageObservation(observedAt: TestFixtures.now, previous: .zero, current: TokenUsageUpdate(total: invalid, last: invalid))+ #expect(throws: StorageCompatibilityError.self) { try value.record(observation) }+ #expect(value.tokenUsage == .zero)+ #expect(value.eventCount == 0)+ }+}diff --git a/CodexBarTests/Presentation/ActivityPresentationTests.swift b/CodexBarTests/Presentation/ActivityPresentationTests.swiftindex 5ce2ea8..9554027 100644--- a/CodexBarTests/Presentation/ActivityPresentationTests.swift+++ b/CodexBarTests/Presentation/ActivityPresentationTests.swift@@ -11,7 +11,7 @@ struct ActivityPresentationTests {defer { preferences.remove() }let monitor = ActivityMonitor(protectionSettings: ProtectionSettings(defaults: preferences.defaults),- protectionStore: ProtectionStore(directoryURL: directory.url), activityDirectoryURL: directory.url+ protectionStore: ProtectionStore(directoryURL: directory.url), activityDataDirectoryURL: directory.url)defer { monitor.stop() }let now = Date()@@ -30,7 +30,7 @@ struct ActivityPresentationTests {task.recordProgress(at: progress.timestamp)}monitor.isActivitySourceHealthy = true- monitor.sessionTransitionNotBefore = startedAt+ monitor.turnTransitionNotBefore = startedAtmonitor.terminalPresentationNotBefore = startedAtmonitor.isProtectionRecoveryInProgress = scenario == "recovering"var notices = 0@@ -46,17 +46,17 @@ struct ActivityPresentationTests {let presentation = monitor.presentationPublisher.sink { presentations.append($0) }defer { presentation.cancel() }if scenario == "pending" || scenario == "superseded" {- monitor.pendingTerminalTasks[key] = PendingTerminalTask(task: task, supersededAt: now)+ monitor.graph.pendingTerminalTasks[key] = PendingTerminalTask(task: task, supersededAt: now)} else if scenario != "missing" {- monitor.tasks[key] = task+ monitor.graph.tasks[key] = task}let next = TestFixtures.event(at: now, turn: "next")if scenario == "superseded" {let nextKey = try #require(ActivityTaskKey(event: next))- monitor.tasks[nextKey] = ActivityTask(displayID: UUID(), key: nextKey, event: next, state: .running, startedAt: now, progressGeneration: 0)+ monitor.graph.tasks[nextKey] = ActivityTask(displayID: UUID(), key: nextKey, event: next, state: .running, startedAt: now, progressGeneration: 0)}var end = TestFixtures.event(.turnCompleted, at: endedAt, thread: start.threadID, turn: start.turnID)- end.context = ActivityContext(+ end.context = ActivityDataContext(method: "turn/completed", threadID: "thread-a", turnID: "turn-a", turnStatus: .completed,turnStartedAt: startedAt, turnCompletedAt: scenario == "missing-time" ? nil : endedAt, duration: scenario == "missing-time" ? nil : 2)@@ -65,24 +65,24 @@ struct ActivityPresentationTests {} else {monitor.consume(.live([end, end]))}- #expect(monitor.tasks[key] == nil)- #expect(monitor.pendingTerminalTasks[key] == nil)+ #expect(monitor.graph.tasks[key] == nil)+ #expect(monitor.graph.pendingTerminalTasks[key] == nil)#expect(monitor.completions.count == 1)#expect(monitor.completions.first?.duration == (scenario == "missing-time" ? nil : 2))- #expect(monitor.terminalTokenUsageRequests.count == 1)+ #expect(monitor.graph.terminalTokenUsageRequests.count == 1)let audible = ["active", "missing", "pending", "superseded", "rounded", "missing-time"].contains(scenario)#expect(notices == (audible ? 1 : 0))+ #expect(presentations.flatMap(\.terminalEvents).count == (audible ? 1 : 0))if scenario == "superseded" {- #expect(try monitor.tasks[#require(ActivityTaskKey(event: next))] != nil)- }- if ["bootstrap", "recovering", "old"].contains(scenario) {- let hasNoTerminalEvents = presentations.allSatisfy(\.terminalEvents.isEmpty)- #expect(hasNoTerminalEvents)+ #expect(try monitor.graph.tasks[#require(ActivityTaskKey(event: next))] != nil)}var repeated: [ActivityTransition] = []monitor.resolveTerminal(.completed(at: endedAt, duration: 2), task: task, key: key, abortFallback: endedAt, into: &repeated)+ monitor.consume(.lifecycleChanged)+ monitor.refreshSnapshot(now: now)#expect(repeated.isEmpty)#expect(monitor.completions.count == 1)+ #expect(presentations.flatMap(\.terminalEvents).count == (audible ? 1 : 0))}@Test(arguments: [@@ -123,7 +123,7 @@ struct ActivityPresentationTests {let monitor = ActivityMonitor(protectionSettings: ProtectionSettings(defaults: preferences.defaults),protectionStore: ProtectionStore(directoryURL: directory.url),- activityDirectoryURL: directory.url+ activityDataDirectoryURL: directory.url)monitor.protectionLoadState = .availablemonitor.isStarted = truediff --git a/CodexBarTests/Presentation/DateAndPresentationTests.swift b/CodexBarTests/Presentation/DateAndPresentationTests.swiftindex a09a6ea..77da161 100644--- a/CodexBarTests/Presentation/DateAndPresentationTests.swift+++ b/CodexBarTests/Presentation/DateAndPresentationTests.swift@@ -60,6 +60,21 @@ struct DateAndPresentationTests {#expect(unavailable.allSatisfy { $0.tokenState == .unavailable })}+ @Test func heatmapSelectionReadsLatestDayDataAndKeepsItsPosition() {+ let date = "2026-09-15"+ let selection = UsageHeatmapSelection(dateID: date, column: 8, row: 2)+ let initial = UsageHeatmapDay(startDate: date, tokenState: .pending, activityData: .empty(startDate: date))+ let updated = UsageHeatmapDay(+ startDate: date, tokenState: .available(120), activityData: .unavailable(startDate: date), tokenUsage: .zero+ )+ let unrelated = UsageHeatmapDay(startDate: "2026-09-14", tokenState: .available(5), activityData: .empty(startDate: "2026-09-14"))+ #expect(selection.day(in: [initial, unrelated]) == initial)+ #expect(selection.day(in: [nil, unrelated, updated]) == updated)+ #expect(selection.column == 8 && selection.row == 2)+ #expect(selection.day(in: [nil, unrelated]) == nil)+ #expect(selection.day(in: []) == nil)+ }+@Test func statusItemTerminalExpiresAtTenSecondsWhileCardKeepsHistory() {let completion = ActivityCompletion(id: UUID(), projectName: nil, modelName: nil, effort: nil, completedAt: TestFixtures.now, duration: 30)let snapshot = ActivitySnapshot(waitingTasks: [], runningTasks: [], recentCompletions: [completion], recentTerminations: [])diff --git a/CodexBarTests/Presentation/LogPreviewTests.swift b/CodexBarTests/Presentation/LogPreviewTests.swiftnew file mode 100644index 0000000..3268b9e--- /dev/null+++ b/CodexBarTests/Presentation/LogPreviewTests.swift@@ -0,0 +1,184 @@+import AppKit+import Testing++struct LogPreviewTests {+ @Test func jsonFormattingAndHighlightingPreserveUnicodeAndOriginalText() async throws {+ let raw = #"{"z":null,"🙂":"é 汉字","a":true,"n":-1.25,"url":"https://example.com"}"#+ let presentation = LogPreviewPresentation()+ presentation.show(entryID: UUID(), title: "Response", text: raw)+ let request = try #require(presentation.request)+ await presentation.prepare(request)+ let item = try #require(presentation.item)+ let document = item.document+ let formatted = document.storage.string as NSString++ #expect(item.request.text == raw)+ #expect(document.characterCount == raw.count)+ #expect(document.language == "JSON")+ #expect(formatted.range(of: "\"a\"").location < formatted.range(of: "\"z\"").location)+ #expect(formatted.contains("\n"))+ #expect(formatted.contains("https://example.com"))+ for (token, color) in [+ ("\"🙂\"", NSColor.systemBlue),+ ("\"é 汉字\"", .systemGreen),+ ("true", .systemOrange),+ ("null", .secondaryLabelColor),+ ("-1.25", .systemPurple)+ ] {+ let range = formatted.range(of: token)+ #expect(range.location != NSNotFound)+ #expect(document.storage.attribute(.foregroundColor, at: range.location, effectiveRange: nil) as? NSColor == color)+ }+ }++ @Test(arguments: ["", "🙂 汉字 é\nsecond line", "{invalid JSON}", "{\"partial\":\n[Truncated, original: 200000 bytes]"])+ func plainInvalidAndTruncatedContentRemainUnchanged(raw: String) async throws {+ let document = try await LogPreviewPreparer.shared.document(for: raw)+ #expect(document.language == nil)+ #expect(document.storage.string == raw)+ #expect(document.characterCount == raw.count)+ }++ @Test(arguments: ["true", "null", "42", "\"🙂\""])+ func jsonFragmentsRemainSupported(raw: String) async throws {+ let document = try await LogPreviewPreparer.shared.document(for: raw)+ #expect(document.language == "JSON")+ #expect(document.storage.string == raw)+ }++ @Test func inlinePreviewUpdatesWhenResponseArrivesWithoutChangingEntryIdentity() async throws {+ let pending = LogInlinePreviewInput(request: #"{"b":2,"a":1}"#, detail: nil, needsSummary: true)+ let first = try await LogPreviewPreparer.shared.inlinePreview(for: pending)+ #expect(first.request?.text == #"{"a":1,"b":2}"#)+ #expect(first.request?.hasText == true)+ #expect(first.detail == nil)+ let response = LogInlinePreviewInput(request: pending.request, detail: "received\nsecond line", needsSummary: true)+ let updated = try await LogPreviewPreparer.shared.inlinePreview(for: response)+ #expect(updated.input != first.input)+ #expect(updated.request?.text == first.request?.text)+ #expect(updated.detail?.text == "received...")+ #expect(updated.summary == "received...")+ let blank = LogInlinePreviewInput(request: " \n\t ", detail: nil, needsSummary: false)+ #expect(try await LogPreviewPreparer.shared.inlinePreview(for: blank).request?.hasText == false)+ }++ @Test func inlinePreviewBoundsTextAtCharacterBoundaries() async throws {+ let input = LogInlinePreviewInput(request: nil, detail: String(repeating: "🙂", count: 300), needsSummary: true)+ let preview = try await LogPreviewPreparer.shared.inlinePreview(for: input)+ #expect(preview.summary == String(repeating: "🙂", count: 160) + "...")+ #expect(preview.detail?.text == String(repeating: "🙂", count: 260) + "...")+ }++ @Test func cancelledPreparationDoesNotPublishAPreview() async throws {+ let presentation = LogPreviewPresentation()+ presentation.show(entryID: UUID(), title: "Cancelled", text: "{}")+ let request = try #require(presentation.request)+ let task = Task { await presentation.prepare(request) }+ task.cancel()+ await task.value+ #expect(presentation.item == nil)+ }++ @Test func closingAndReplacingRequestsRejectLateResults() async throws {+ let presentation = LogPreviewPresentation()+ let entryID = UUID()+ presentation.show(entryID: entryID, title: "Old", text: Self.denseJSON)+ let old = try #require(presentation.request)+ let oldTask = Task { await presentation.prepare(old) }+ await Task.yield()+ presentation.show(entryID: UUID(), title: "New", text: "new response")+ let current = try #require(presentation.request)+ presentation.cancel(for: entryID)+ #expect(presentation.request?.id == current.id)+ await presentation.prepare(current)+ await oldTask.value+ #expect(presentation.item?.id == current.id)+ #expect(presentation.item?.document.storage.string == "new response")++ presentation.show(entryID: entryID, title: "Closing", text: Self.denseJSON)+ let closing = try #require(presentation.request)+ let closingTask = Task { await presentation.prepare(closing) }+ await Task.yield()+ presentation.cancel(for: entryID)+ await closingTask.value+ #expect(presentation.request == nil)+ #expect(presentation.item == nil)+ }++ @Test func previousSheetDismissalDoesNotCancelNewRequest() async throws {+ let presentation = LogPreviewPresentation()+ presentation.show(entryID: UUID(), title: "First", text: "first")+ try await presentation.prepare(#require(presentation.request))+ let previous = try #require(presentation.item)+ presentation.show(entryID: UUID(), title: "Next", text: "next")+ let current = try #require(presentation.request)+ presentation.dismiss(previous)+ #expect(presentation.request?.id == current.id)+ await presentation.prepare(current)+ let item = try #require(presentation.item)+ #expect(item.document.storage.string == "next")+ presentation.dismiss(item)+ #expect(presentation.request == nil)+ #expect(presentation.item == nil)+ }++ @Test func repeatedDisplayAndResizeKeepSelectionScrollAndLayout() async throws {+ let document = try await LogPreviewPreparer.shared.document(for: Self.denseJSON)+ let scrollView = LogPreviewScrollView(document: document)+ scrollView.setFrameSize(NSSize(width: 860, height: 600))+ scrollView.layoutSubtreeIfNeeded()+ let textView = try #require(scrollView.documentView as? NSTextView)+ let manager = try #require(textView.layoutManager)+ #expect(textView.textStorage === document.storage)+ #expect(textView.textContainer === document.container)+ #expect(manager.firstUnlaidCharacterIndex() == document.storage.length)+ let selection = NSRange(location: document.storage.length - 10, length: 10)+ textView.setSelectedRange(selection)+ textView.scrollRangeToVisible(selection)+ let visibleOrigin = scrollView.contentView.bounds.origin+ #expect(visibleOrigin.y > 0)+ for _ in 0 ..< 3 {+ scrollView.display(document)+ }+ #expect(scrollView.documentView === textView)+ #expect(textView.selectedRange() == selection)+ #expect(scrollView.contentView.bounds.origin == visibleOrigin)++ scrollView.setFrameSize(NSSize(width: 1100, height: 800))+ scrollView.layoutSubtreeIfNeeded()+ #expect(textView.frame.width >= scrollView.contentSize.width)+ #expect(textView.frame.height >= document.contentSize.height)+ #expect(textView.selectedRange() == selection)+ #expect(manager.firstUnlaidCharacterIndex() == document.storage.length)+ textView.scrollRangeToVisible(selection)+ let selectedRect = manager.boundingRect(forGlyphRange: manager.glyphRange(forCharacterRange: selection, actualCharacterRange: nil), in: document.container)+ #expect(scrollView.contentView.bounds.maxY >= selectedRect.maxY)+ }++ @Test func longLinesScrollHorizontallyAndShortTextFillsViewport() async throws {+ let document = try await LogPreviewPreparer.shared.document(for: String(repeating: "a", count: 131000))+ let scrollView = LogPreviewScrollView(document: document)+ scrollView.setFrameSize(NSSize(width: 860, height: 600))+ scrollView.layoutSubtreeIfNeeded()+ let textView = try #require(scrollView.documentView as? NSTextView)+ textView.scrollRangeToVisible(NSRange(location: document.storage.length - 1, length: 1))+ #expect(scrollView.contentView.bounds.origin.x > 0)+ #expect(scrollView.contentView.bounds.maxX >= document.contentSize.width)++ let short = try await LogPreviewPreparer.shared.document(for: "🙂 汉字 é")+ scrollView.display(short)+ scrollView.layoutSubtreeIfNeeded()+ let replacement = try #require(scrollView.documentView as? NSTextView)+ #expect(replacement !== textView)+ #expect(replacement.string == "🙂 汉字 é")+ #expect(replacement.frame.width >= scrollView.contentSize.width)+ #expect(replacement.frame.height >= scrollView.contentSize.height)+ replacement.setSelectedRange(NSRange(location: 0, length: short.storage.length))+ #expect(replacement.selectedRange().length == short.storage.length)+ #expect(scrollView.contentView.bounds.origin.x == 0)+ }++ private static var denseJSON: String {+ "[" + Array(repeating: "0", count: 65500).joined(separator: ",") + "]"+ }+}diff --git a/CodexBarTests/Support/ActivityUpdateFixtures.swift b/CodexBarTests/Support/ActivityUpdateFixtures.swiftnew file mode 100644index 0000000..cb904a5--- /dev/null+++ b/CodexBarTests/Support/ActivityUpdateFixtures.swift@@ -0,0 +1,129 @@+import Combine+import Foundation++/// 协议测试通过正式发布接口观察值快照, 不共享 reader 内部状态+@MainActor final class ActivityReaderProbe {+ var graph = ActivityTaskGraph()+}++extension AppServerActivityReader {+ @MainActor init(+ probe: ActivityReaderProbe,+ socketURL: URL = AppServerActivityReader.defaultSocketURL,+ logStorage: AppServerLogStore? = nil,+ tokenUsageHistory: TokenUsageHistoryStore = TokenUsageHistoryStore(),+ activityDataRecorder: ActivityDataRecorder = ActivityDataRecorder(),+ maximumPendingEvents: Int = 4096,+ maximumPendingBytes: Int = 8 * 1024 * 1024,+ maximumLoadedPages: Int = 100,+ reconciliationTimeout: TimeInterval = 30,+ onAccountChange: @escaping @MainActor @Sendable (AccountChange) -> Void = { _ in },+ onBatch: @escaping @MainActor @Sendable (ActivityUpdate) -> Void+ ) {+ self.init(+ socketURL: socketURL,+ logStorage: logStorage,+ tokenUsageHistory: tokenUsageHistory,+ activityDataRecorder: activityDataRecorder,+ maximumPendingEvents: maximumPendingEvents,+ maximumPendingBytes: maximumPendingBytes,+ maximumLoadedPages: maximumLoadedPages,+ reconciliationTimeout: reconciliationTimeout,+ onAccountChange: onAccountChange+ ) { update in+ probe.graph = update.graph+ onBatch(update)+ }+ }+}++enum ActivityFixtureBatch {+ case bootstrapStart+ case snapshotEvents([ActivityDataRecord])+ case bootstrapEnd+ case live([ActivityDataRecord])+ case sourceUnavailable+ case lifecycleChanged++ var phase: ActivityUpdate.Phase {+ switch self {+ case .bootstrapStart: .bootstrapStart+ case .snapshotEvents: .snapshotEvents+ case .bootstrapEnd: .bootstrapEnd+ case .live: .live+ case .sourceUnavailable: .sourceUnavailable+ case .lifecycleChanged: .lifecycleChanged+ }+ }+}++extension ActivityMonitor {+ private static var fixtureRevision: UInt64 = 0+ private static let fixtureReaderID = UUID()++ /// 展示和保护测试从任务图产生真实 update, 每次经过 monitor 的正式消费接口+ func acceptFixtureGraph(_ graph: ActivityTaskGraph, phase: ActivityUpdate.Phase = .lifecycleChanged, transitions: [ActivityTransition] = []) {+ Self.fixtureRevision &+= 1+ consume(ActivityUpdate(+ readerID: Self.fixtureReaderID,+ revision: Self.fixtureRevision,+ phase: phase,+ graph: graph,+ transitions: transitions,+ terminalEvents: graph.terminalEvents,+ backfilledTasks: graph.backfilledTasks+ ))+ self.graph.terminalEvents.removeAll()+ self.graph.backfilledTasks.removeAll()+ }++ func consume(_ batch: ActivityFixtureBatch) {+ var next = graph+ let transitions: [ActivityTransition]+ switch batch {+ case .bootstrapStart:+ next = ActivityTaskGraph()+ transitions = []+ case let .snapshotEvents(events): transitions = next.consume(events, source: .bootstrap)+ case let .live(events): transitions = next.consume(events, source: .live)+ default: transitions = []+ }+ acceptFixtureGraph(next, phase: batch.phase, transitions: transitions)+ }++ func apply(_ event: ActivityDataRecord, source: ActivityEventSource, into transitions: inout [ActivityTransition]) -> ActivityTaskKey? {+ var next = graph+ var changes: [ActivityTransition] = []+ let waiting = next.apply(event, source: source, into: &changes)+ acceptFixtureGraph(next)+ transitions += changes.filter { isCurrentTransition($0) }+ return waiting+ }++ func resolveTerminal(+ _ terminal: TurnTerminalState,+ task: ActivityTask,+ key: ActivityTaskKey,+ abortFallback: Date,+ publishesEvents: Bool = true,+ into transitions: inout [ActivityTransition]+ ) {+ var next = graph+ var changes: [ActivityTransition] = []+ next.resolveTerminal(terminal, task: task, key: key, abortFallback: abortFallback, publishesEvents: publishesEvents, into: &changes)+ acceptFixtureGraph(next)+ transitions += changes.filter { isCurrentTransition($0) }+ }++ func applySubagentLifecycle(_ state: TurnLifecycleState) {+ var next = graph+ next.applySubagentLifecycle(state)+ acceptFixtureGraph(next)+ }++ func publishWaitingApprovalTransitions(_ keys: [ActivityTaskKey]) {+ for transition in graph.waitingApprovalTransitions(keys) where isCurrentTransition(transition) {+ transitionSubject.send(transition)+ }+ }+}diff --git a/CodexBarTests/Support/SharedServerFixture.swift b/CodexBarTests/Support/SharedServerFixture.swiftindex f644fcb..65e41a4 100644--- a/CodexBarTests/Support/SharedServerFixture.swift+++ b/CodexBarTests/Support/SharedServerFixture.swift@@ -11,7 +11,7 @@ final nonisolated class SharedServerFixture: @unchecked Sendable {private let lock = NSLock()private var failure: (any Error)?- init(connections: Int = 1, handler: @escaping @Sendable (Peer) throws -> Void) throws {+ init(connections: Int = 1, concurrentConnections: Bool = false, handler: @escaping @Sendable (Peer) throws -> Void) throws {descriptor = socket(AF_UNIX, SOCK_STREAM, 0)guard descriptor >= 0 else { throw FixtureError.io }var address = sockaddr_un()@@ -24,27 +24,46 @@ final nonisolated class SharedServerFixture: @unchecked Sendable {Darwin.bind(descriptor, $0, socklen_t(MemoryLayout<sockaddr_un>.size))}}- guard bound == 0, listen(descriptor, 1) == 0 else {+ guard bound == 0, listen(descriptor, Int32(max(1, connections))) == 0 else {Darwin.close(descriptor)throw FixtureError.io}DispatchQueue.global().async { [self] in- defer { completed.signal() }+ let clients = DispatchGroup()+ defer {+ clients.wait()+ completed.signal()+ }do {for _ in 0 ..< connections {var poller = pollfd(fd: descriptor, events: Int16(POLLIN), revents: 0)guard poll(&poller, 1, 5000) > 0 else { throw FixtureError.io }let client = accept(descriptor, nil, nil)guard client >= 0 else { throw FixtureError.io }- defer { Darwin.close(client) }var timeout = timeval(tv_sec: 5, tv_usec: 0)_ = setsockopt(client, SOL_SOCKET, SO_RCVTIMEO, &timeout, socklen_t(MemoryLayout<timeval>.size))_ = setsockopt(client, SOL_SOCKET, SO_SNDTIMEO, &timeout, socklen_t(MemoryLayout<timeval>.size))var enabled: Int32 = 1_ = setsockopt(client, SOL_SOCKET, SO_NOSIGPIPE, &enabled, socklen_t(MemoryLayout<Int32>.size))- let peer = Peer(descriptor: client)- try peer.handshake()- try handler(peer)+ let serve: @Sendable () -> Void = { [self] in+ defer { Darwin.close(client) }+ do {+ let peer = Peer(descriptor: client)+ try peer.handshake()+ try handler(peer)+ } catch {+ lock.withLock { failure = error }+ }+ }+ if concurrentConnections {+ clients.enter()+ DispatchQueue.global().async {+ serve()+ clients.leave()+ }+ } else {+ serve()+ }}} catch {lock.withLock { failure = error }@@ -104,17 +123,28 @@ final nonisolated class SharedServerFixture: @unchecked Sendable {try sendRaw(JSONSerialization.data(withJSONObject: object))}- func sendRaw(_ payload: Data, opcode: UInt8 = 1) throws {- var frame = Data([0x80 | opcode])- if payload.count < 126 {- frame.append(UInt8(payload.count))+ func sendRaw(_ payload: Data, opcode: UInt8 = 1, final: Bool = true) throws {+ try sendHeader(length: UInt64(payload.count), opcode: opcode, final: final)+ try sendBytes(payload)+ }++ func sendHeader(length: UInt64, opcode: UInt8 = 1, final: Bool = true) throws {+ var frame = Data([(final ? 0x80 : 0) | opcode])+ if length < 126 {+ frame.append(UInt8(length))+ } else if length <= UInt16.max {+ frame.append(contentsOf: [126, UInt8(length >> 8), UInt8(length & 255)])} else {- frame.append(contentsOf: [126, UInt8(payload.count >> 8), UInt8(payload.count & 255)])+ frame.append(127)+ frame.append(contentsOf: (0 ..< 8).reversed().map { UInt8((length >> ($0 * 8)) & 255) })}- frame.append(payload)try write(frame)}+ func sendBytes(_ data: Data) throws {+ try write(data)+ }+private func read(_ count: Int) throws -> Data {var data = Data()while data.count < count {diff --git a/CodexBarTests/Support/TestSupport.swift b/CodexBarTests/Support/TestSupport.swiftindex e302824..7cf4c0a 100644--- a/CodexBarTests/Support/TestSupport.swift+++ b/CodexBarTests/Support/TestSupport.swift@@ -9,43 +9,40 @@ nonisolated enum TestFixtures {}static func event(- _ name: ActivityEventKind = .turnStarted,+ _ name: ActivityDataEventKind = .turnStarted,at timestamp: Date = now,thread: String? = "thread-a",turn: String? = "turn-a",- agent: String? = nil,- origin: ActivityOrigin = .main- ) -> ActivityRecord {- var event = ActivityRecord(+ agentThreadID: String? = nil,+ origin: ActivityDataOrigin = .main+ ) -> ActivityDataRecord {+ var event = ActivityDataRecord(timestamp: timestamp, name: name.rawValue, origin: origin,cwd: "/projects/example", toolName: "exec_command", model: "gpt-5",effort: "high",- threadID: thread, turnID: turn, agentID: agent+ threadID: thread, turnID: turn, agentThreadID: agentThreadID)if name == .turnStarted, let thread {- event.context = ActivityContext(- method: "turn/started", threadID: agent ?? thread, turnID: turn, turnStartedAt: timestamp+ event.context = ActivityDataContext(+ method: "turn/started", threadID: agentThreadID ?? thread, turnID: turn, turnStartedAt: timestamp)}return event}- static func tokenTurn(- id: String, rootID: String, startedAt: Date? = nil, updatedAt: Date, usage: TokenUsage? = nil- ) -> TokenTurn {- TokenTurn(- id: id, rootID: rootID, startedAt: startedAt, updatedAt: updatedAt, usage: usage,- checkpoint: usage.map { ["test-stream": TokenObservationCheckpoint(sequence: 1, usage: $0)] } ?? [:]- )+ static func tokenUsageAggregate(at date: Date = now, tokenUsage: TokenUsage = .zero, eventCount: Int = 1) -> TokenUsageAggregate {+ TokenUsageAggregate(date: HistoryStorage.dateKey(for: date), tokenUsage: tokenUsage, eventCount: eventCount)+ }++ static func tokenUsageObservation(id: String = UUID().uuidString, at date: Date = now, tokenUsage: TokenUsage, previous: TokenUsage = .zero) -> TokenUsageObservation {+ TokenUsageObservation(id: id, observedAt: date, previous: previous, current: TokenUsageUpdate(total: previous.adding(tokenUsage)!, last: tokenUsage))}static func aggregate(- generation: String? = "generation-a",events: Int = 2,turns: Int = 1- ) -> ActivityAggregate {- var aggregate = ActivityAggregate(date: "2026-09-15", generationID: generation)- aggregate.sourceCheckpoint = ActivitySourceCheckpoint(byteCount: UInt64(events + 1), digest: String(repeating: "a", count: 64))+ ) -> ActivityDataAggregate {+ var aggregate = ActivityDataAggregate(date: "2026-09-15")aggregate.eventCount = eventsaggregate.turnCount = turnsaggregate.threadCount = 1@@ -74,14 +71,31 @@ nonisolated struct TestDirectory {try write(Data(text.utf8), to: relativePath)}- func seedTokenSnapshots(_ turns: [TokenTurn], now: Date) async throws {- try HistoryStorage.withExclusiveLock(in: url) {- var journal = AppServerEventJournal()- for turn in turns {- try journal.append(AppServerEventRecord(token: turn, recordedAt: now), in: url)- }+ func seedTokenUsageAggregates(_ aggregates: [TokenUsageAggregate], now _: Date) async throws {+ let database = try HistoryDatabase(directoryURL: url)+ for var aggregate in aggregates {+ aggregate.position = 0+ try database.entries(date: aggregate.date, kind: .tokenUsage) { aggregate.position = $0.position }+ try database.execute("INSERT OR IGNORE INTO days(date) VALUES (?)", [.text(aggregate.date)])+ try database.execute("""+ INSERT INTO aggregates(date, tokenUsage) VALUES (?, ?)+ ON CONFLICT(date) DO UPDATE SET tokenUsage = excluded.tokenUsage+ """, [.text(aggregate.date), .data(JSONLines.stableEncoder.encode(aggregate))])+ }+ }++ func historyRecords() throws -> [AppServerEventRecord] {+ var records: [AppServerEventRecord] = []+ try HistoryDatabase(directoryURL: url).entries { records.append($0.record) }+ return records+ }++ func replaceHistory(_ data: Data, date: String) throws {+ let database = try HistoryDatabase(directoryURL: url)+ try database.execute("DELETE FROM events WHERE date = ?", [.text(date)])+ for line in data.split(separator: JSONLines.newlineByte) {+ try database.append(AppServerEventRecord.decode(from: Data(line)))}- _ = try await TokenHistoryStore(directoryURL: url).refresh(now: now)}func remove() throws {@@ -118,31 +132,36 @@ struct TestPreferences {}nonisolated extension TestDirectory {- @discardableResult- func writeJournal(_ records: Data, to relativePath: String, generation: String? = nil) throws -> URL {- let url = url.appendingPathComponent(relativePath)- let date = url.deletingPathExtension().lastPathComponent- let source = generation ?? (try? AppServerEventJournal.header(at: url).generationID) ?? UUID().uuidString.lowercased()- let header = AppServerEventJournal.Header(version: 1, date: date, generationID: source)- return try write(JSONLines.stableEncoder.encode(header) + Data([10]) + records, to: relativePath)- }-- func activityAggregate(date: String, generation: String = "source", events: Int = 1) throws -> ActivityAggregate {- let path = "Events/\(date).jsonl"- let url = url.appendingPathComponent(path)- if !FileManager.default.fileExists(atPath: url.path) {- try writeJournal(Data(), to: path, generation: generation)- }- var aggregate = ActivityAggregate(date: date, generationID: generation)+ func activityDataAggregate(date: String, events: Int = 1) throws -> ActivityDataAggregate {+ var aggregate = ActivityDataAggregate(date: date)aggregate.eventCount = events- aggregate.sourceCheckpoint = try ActivitySourceCheckpoint.read(at: url, byteCount: HistoryStorage.fileSize(at: url))return aggregate}}+nonisolated extension HistoryDatabase {+ func exportJSONLines(date: String) throws -> Data {+ var result = Data()+ try entries(date: date) { try result.append($0.record.jsonLineData()) }+ return result+ }+}++nonisolated extension ActivityDataAggregate {+ func jsonLineData() throws -> Data {+ try JSONLines.stableEncoder.encode(self) + Data([JSONLines.newlineByte])+ }+}++nonisolated extension SyncedActivityData {+ func jsonLineData() throws -> Data {+ try JSONLines.stableEncoder.encode(self) + Data([JSONLines.newlineByte])+ }+}+/// 测试夹具按采集器的原始记录与缓存聚合两个步骤写入-extension TokenHistoryStore {- func recordObservations(_ observations: [TokenObservation], now: Date = Date()) throws -> [TokenTurn] {+extension TokenUsageHistoryStore {+ func recordObservations(_ observations: [TokenUsageObservation], now: Date = Date()) throws -> [TokenUsageAggregate] {_ = try refresh(now: now)for observation in observations {try appendObservation(observation, now: now)diff --git a/CodexBarTests/Sync/SyncRecoveryTests.swift b/CodexBarTests/Sync/SyncRecoveryTests.swiftindex 1347963..66f33f9 100644--- a/CodexBarTests/Sync/SyncRecoveryTests.swift+++ b/CodexBarTests/Sync/SyncRecoveryTests.swift@@ -1,184 +1,155 @@import CloudKit-import CryptoKitimport Foundationimport Testingstruct SyncRecoveryTests {- @Test func conflictingTokenRootCannotOverwriteRemoteOrProduceCompleteTotals() async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let database = SyncDatabaseFixture()- let local = Self.turn(0)- var remote = local.pseudonymized(salt: Self.salt)- remote.rootID = TokenTurn.identifier(thread: "other-root", turn: "other-turn")- let record = CKRecord(recordType: TokenSync.recordType, recordID: TokenSync.recordID(remote.id, zoneID: Self.zone))- TokenSync.apply(remote, to: record)- await database.storeRecord(record)- let service = TokenSync(database: database, directoryURL: directory.url, isEnabled: { true })- await #expect(throws: TokenCacheError.self) {- try await service.synchronize(local: [local], accountScopedDeviceID: "device", salt: Self.salt, zoneID: Self.zone)- }- #expect(await database.savedTypes.isEmpty)- let records = try await database.records(for: [record.recordID], desiredKeys: nil)- let saved = try #require(try records[record.recordID]?.get())- #expect(try TokenSync.turn(from: saved)?.rootID == remote.rootID)- await #expect(throws: TokenCacheError.self) { try await service.snapshot(local: [local], accountScopedDeviceID: "device") }- }-- @Test func expiredTokenCacheStaysBoundedWhenCloudDeletionFailsAndRetries() async throws {+ @Test func expiredTokenUsageCacheStaysBoundedWhenCloudDeletionFailsAndRetries() async throws {let directory = try TestDirectory()defer { try? directory.remove() }let database = SyncDatabaseFixture()let expiredAt = HistoryStorage.retentionCutoffDate().addingTimeInterval(-86400)var expiredIDs: [CKRecord.ID] = []for index in 1 ... 2 {- var turn = Self.turn(index).pseudonymized(salt: Self.salt)- turn.updatedAt = expiredAt- let record = CKRecord(recordType: TokenSync.recordType, recordID: TokenSync.recordID(turn.id, zoneID: Self.zone))- TokenSync.apply(turn, to: record)+ let daily = TokenUsageSyncRecord(deviceID: "device", date: HistoryStorage.dateKey(for: expiredAt.addingTimeInterval(Double(-index) * 86400)), tokenUsage: .zero)+ let record = CKRecord(recordType: TokenUsageSync.recordType, recordID: TokenUsageSync.recordID(daily.id, zoneID: Self.zone))+ try TokenUsageSync.apply(daily, to: record)await database.storeRecord(record)expiredIDs.append(record.recordID)}await database.failNextDeletion()- let service = TokenSync(database: database, directoryURL: directory.url, isEnabled: { true })+ let service = TokenUsageSync(database: database, directoryURL: directory.url, isEnabled: { true })await #expect(throws: (any Error).self) {- try await service.synchronize(local: [Self.turn(0)], accountScopedDeviceID: "device", salt: Self.salt, zoneID: Self.zone)+ try await service.synchronize(local: [Self.turn(0)], accountScopedDeviceID: "device", zoneID: Self.zone)}- #expect(try Self.cachedTurnCount(in: directory.url) == 1)- let reopened = TokenSync(database: database, directoryURL: directory.url, isEnabled: { true })- try await reopened.synchronize(local: [Self.turn(0)], accountScopedDeviceID: "device", salt: Self.salt, zoneID: Self.zone)- #expect(try Self.cachedTurnCount(in: directory.url) == 1)+ #expect(try Self.cachedDailyCount(in: directory.url) == 1)+ let reopened = TokenUsageSync(database: database, directoryURL: directory.url, isEnabled: { true })+ try await reopened.synchronize(local: [Self.turn(0)], accountScopedDeviceID: "device", zoneID: Self.zone)+ #expect(try Self.cachedDailyCount(in: directory.url) == 1)let remaining = try await database.records(for: expiredIDs, desiredKeys: nil)for id in expiredIDs {#expect(throws: (any Error).self) { try remaining[id]?.get() }}}- @Test func previousEncodingHashConvergesWithoutRepeatedUploads() async throws {+ @Test(arguments: [false, true])+ func remoteActivityDataChangesAreRepairedWithoutRepeatedUploads(deleted: Bool) async throws {let directory = try TestDirectory()defer { try? directory.remove() }let database = SyncDatabaseFixture()let date = HistoryStorage.dateKey(for: Date())- let local = try directory.activityAggregate(date: date)- let service = SyncService(- database: database,- directoryURL: directory.url,- activityEventsDirectoryURL: directory.url.appendingPathComponent("Events"),- isEnabled: { true }- )- _ = await service.synchronizeIfEnabled(localAggregates: [local], trigger: .manual)- let store = ActivitySyncStore(directoryURL: directory.url)- var state = store.loadState()- let currentHash = try #require(state.hashByDate[date])- let previousBytes = Data(("""- {"date":"\(date)","generationID":"source","eventCount":1,"threadCount":null,"turnCount":null,"projectCounts":{},"modelCounts":{}}- """ + "\n").utf8)- let previousHash = TokenTurn.hexString(SHA256.hash(data: previousBytes))- #expect(previousHash != currentHash)- state.hashByDate[date] = previousHash- try store.saveState(state)+ let local = try directory.activityDataAggregate(date: date)+ let service = SyncService(database: database, directoryURL: directory.url, isEnabled: { true })+ _ = await service.synchronizeIfEnabled(localActivityDataAggregates: [local], trigger: .manual)+ #expect(await service.hasPendingActivityDataUpdates(local: [local]) == false)+ let remote = try #require(await database.activityDataRecords().first)+ if deleted {+ await database.removeRecord(remote.recordID)+ } else {+ var changed = local+ changed.eventCount = 9+ try ActivityDataRecordCodec.apply(changed.syncedAggregate, deviceID: #require(remote["deviceID"] as? String), to: remote)+ await database.storeRecord(remote)+ }- let restarted = SyncService(- database: database,- directoryURL: directory.url,- activityEventsDirectoryURL: directory.url.appendingPathComponent("Events"),- isEnabled: { true }- )- _ = await restarted.synchronizeIfEnabled(localAggregates: [local], trigger: .manual)- #expect(store.loadState().hashByDate[date] == currentHash)- #expect(await database.activityRecords().count == 1)- #expect(await database.activityRecords().first?["eventCount"] as? Int == 1)- #expect(await database.deletedIDs.isEmpty)+ let restarted = SyncService(database: database, directoryURL: directory.url, isEnabled: { true })+ _ = await restarted.synchronizeIfEnabled(localActivityDataAggregates: [local], trigger: .manual)+ #expect(await database.savedTypes.filter { $0 == "ActivityData" }.count == 2)+ #expect(try await Self.activityDataEventCount(database.activityDataRecords().first) == 1)+ let cache = try ActivityDataSyncStore(directoryURL: directory.url).load()+ #expect(cache.sortedRecords.map(\.activityData) == [local.syncedAggregate])+ #expect(await restarted.hasPendingActivityDataUpdates(local: [local]) == false)let writes = await database.savedTypes.countlet reads = await database.requestedIDs.filter { $0.recordName != "accountSalt" }.count- _ = await restarted.synchronizeIfEnabled(localAggregates: [local], trigger: .manual)+ _ = await restarted.synchronizeIfEnabled(localActivityDataAggregates: [local], trigger: .manual)#expect(await database.savedTypes.count == writes)#expect(await database.requestedIDs.filter { $0.recordName != "accountSalt" }.count == reads)+ #expect(try ActivityDataSyncStore(directoryURL: directory.url).load().state.lastUploadAt == cache.state.lastUploadAt)}private static let zone = CKRecordZone.ID(zoneName: SyncCloudKit.zoneName, ownerName: CKCurrentUserDefaultName)private static let salt = Data(repeating: 1, count: 32)- @Test func damagedActivityCursorRebuildsWithoutBlockingTokens() async throws {+ @Test func damagedActivityDataCursorRebuildsWithoutBlockingTokenUsage() async throws {let directory = try TestDirectory()defer { try? directory.remove() }let database = SyncDatabaseFixture()let service = SyncService(database: database,directoryURL: directory.url,- activityEventsDirectoryURL: directory.url.appendingPathComponent("Events"),isEnabled: { true })- _ = await service.synchronizeIfEnabled(localAggregates: [], trigger: .manual)- let url = directory.url.appendingPathComponent("Activity/cache.json")+ _ = await service.synchronizeIfEnabled(localActivityDataAggregates: [], trigger: .manual)+ let url = directory.url.appendingPathComponent("activityData/cache.json")var json = try #require(JSONSerialization.jsonObject(with: Data(contentsOf: url)) as? [String: Any])json["cursor"] = Data("broken cursor".utf8).base64EncodedString()try JSONSerialization.data(withJSONObject: json).write(to: url)let queriesBefore = await database.queryCountlet changesBefore = await database.changesSinceNil.count- _ = await service.synchronizeIfEnabled(localAggregates: [], localTokenTurns: [Self.turn(0)], trigger: .manual)+ _ = await service.synchronizeIfEnabled(localActivityDataAggregates: [], localTokenUsageAggregates: [Self.turn(0)], trigger: .manual)#expect(await database.queryCount == queriesBefore)#expect(await database.changesSinceNil.count > changesBefore)#expect(await database.changesSinceNil[changesBefore])- #expect(await database.savedTypes.contains(TokenSync.recordType))- #expect(try ActivitySyncStore(directoryURL: directory.url).load().cursor == nil)+ #expect(await database.savedTypes.contains(TokenUsageSync.recordType))+ #expect(try ActivityDataSyncStore(directoryURL: directory.url).load().cursor == nil)}@Test(arguments: [false, true])- func activityRebuildCommitsOnlyAfterAllPagesSucceed(failSecondPage: Bool) async throws {+ func activityDataRebuildCommitsOnlyAfterAllPagesSucceed(failSecondPage: Bool) async throws {let directory = try TestDirectory()defer { try? directory.remove() }let database = SyncDatabaseFixture()let service = SyncService(database: database,directoryURL: directory.url,- activityEventsDirectoryURL: directory.url.appendingPathComponent("Events"),isEnabled: { true })- _ = await service.synchronizeIfEnabled(localAggregates: [], trigger: .manual)- let store = ActivitySyncStore(directoryURL: directory.url)+ _ = await service.synchronizeIfEnabled(localActivityDataAggregates: [], trigger: .manual)+ let store = ActivityDataSyncStore(directoryURL: directory.url)let date = HistoryStorage.dateKey(for: Date())- let daily = try directory.activityAggregate(date: date).syncedAggregate- let generation = try #require(daily.generationID)- let stale = try ActivitySyncRecord(deviceID: "stale", daily: daily, recordName: "stale_\(date)_\(generation)")- try store.saveFetchedRecords([stale], cursor: nil)- let url = directory.url.appendingPathComponent("Activity/cache.json")+ let daily = try directory.activityDataAggregate(date: date).syncedAggregate+ let stale = try ActivityDataSyncRecord(deviceID: "stale", activityData: daily, recordName: "activityData_stale_\(date)")+ try store.saveFetchedRecords([stale.id: stale], cursor: nil)+ let url = directory.url.appendingPathComponent("activityData/cache.json")let original = try Data(contentsOf: url)- let remote = CKRecord(recordType: "Activity", recordID: CKRecord.ID(recordName: "remote_\(date)_\(generation)", zoneID: Self.zone))- ActivityRecordCodec.apply(daily, deviceID: "remote", to: remote)+ let remote = CKRecord(recordType: "ActivityData", recordID: CKRecord.ID(recordName: "activityData_remote_\(date)", zoneID: Self.zone))+ try ActivityDataRecordCodec.apply(daily, deviceID: "remote", to: remote)remote["updatedAt"] = Date(timeIntervalSince1970: Date().timeIntervalSince1970.rounded(.down)) as CKRecordValue- let deleted = CKRecord(recordType: "Activity", recordID: CKRecord.ID(recordName: "deleted_\(date)_\(generation)", zoneID: Self.zone))- ActivityRecordCodec.apply(daily, deviceID: "deleted", to: deleted)+ let deleted = CKRecord(recordType: "ActivityData", recordID: CKRecord.ID(recordName: "activityData_deleted_\(date)", zoneID: Self.zone))+ try ActivityDataRecordCodec.apply(daily, deviceID: "deleted", to: deleted)+ let expiredDate = HistoryStorage.dateKey(for: HistoryStorage.retentionCutoffDate().addingTimeInterval(-86400))+ let expiredDaily = try directory.activityDataAggregate(date: expiredDate).syncedAggregate+ let expired = CKRecord(recordType: "ActivityData", recordID: CKRecord.ID(recordName: "activityData_remote_\(expiredDate)", zoneID: Self.zone))+ try ActivityDataRecordCodec.apply(expiredDaily, deviceID: "remote", to: expired)let metadata = CKRecord(recordType: "Metadata", recordID: CKRecord.ID(recordName: "ignored", zoneID: Self.zone))let first = SyncChanges(- records: [remote.recordID: .success(remote), deleted.recordID: .success(deleted), metadata.recordID: .success(metadata)],+ records: [remote.recordID: .success(remote), deleted.recordID: .success(deleted), expired.recordID: .success(expired), metadata.recordID: .success(metadata)],deletions: [], token: nil, moreComing: true)- let last = SyncChanges(records: [:], deletions: [(deleted.recordID, "Activity")], token: nil, moreComing: false)+ let last = SyncChanges(records: [:], deletions: [(deleted.recordID, "ActivityData")], token: nil, moreComing: false)// 同步在上传前后各拉取一次, 两次都要独立得到完整结果await database.setChangesPages(failSecondPage? [.success(first), .failure(CKError(.networkFailure))]: [.success(first), .success(last), .success(first), .success(last)])let queriesBefore = await database.queryCount- let result = await service.synchronizeIfEnabled(localAggregates: [], trigger: .manual)+ let result = await service.synchronizeIfEnabled(localActivityDataAggregates: [], trigger: .manual)#expect(await database.queryCount == queriesBefore)#expect(await database.remainingChangesPages == 0)if failSecondPage {#expect(try Data(contentsOf: url) == original)- #expect(result.records == [stale])+ #expect(result.activityData == [stale])} else {- let expected = try #require(try ActivityRecordCodec.remoteDailyRecord(from: remote))- #expect(result.records == [expected])- #expect(try store.load().records == [expected])+ let expected = try #require(try ActivityDataRecordCodec.remoteDailyRecord(from: remote))+ #expect(result.activityData == [expected])+ #expect(try store.load().sortedRecords == [expected])}}- @Test(arguments: [false, true]) func damagedTokenCacheOrCursorRebuilds(cursorOnly: Bool) async throws {+ @Test(arguments: [false, true]) func damagedTokenUsageCacheOrCursorRebuilds(cursorOnly: Bool) async throws {let directory = try TestDirectory()defer { try? directory.remove() }let database = SyncDatabaseFixture()- let service = TokenSync(database: database, directoryURL: directory.url, isEnabled: { true })- try await service.synchronize(local: [Self.turn(0)], accountScopedDeviceID: "device", salt: Self.salt, zoneID: Self.zone)+ let service = TokenUsageSync(database: database, directoryURL: directory.url, isEnabled: { true })+ try await service.synchronize(local: [Self.turn(0)], accountScopedDeviceID: "device", zoneID: Self.zone)let url = directory.url.appendingPathComponent("cache.json")if cursorOnly {var json = try #require(JSONSerialization.jsonObject(with: Data(contentsOf: url)) as? [String: Any])@@ -187,110 +158,141 @@ struct SyncRecoveryTests {} else {try Data("broken cache".utf8).write(to: url)}- let reopened = TokenSync(database: database, directoryURL: directory.url, isEnabled: { true })- try await reopened.synchronize(local: [], accountScopedDeviceID: "device", salt: Self.salt, zoneID: Self.zone)- #expect(try Self.cachedTurnCount(in: directory.url) == 1)+ let reopened = TokenUsageSync(database: database, directoryURL: directory.url, isEnabled: { true })+ try await reopened.synchronize(local: [], accountScopedDeviceID: "device", zoneID: Self.zone)+ #expect(try Self.cachedDailyCount(in: directory.url) == 1)}- @Test func failedTokenRebuildPreservesOriginalBytes() async throws {+ @Test func failedTokenUsageRebuildPreservesOriginalBytes() async throws {let directory = try TestDirectory()defer { try? directory.remove() }let original = Data("broken cache".utf8)let url = try directory.write(original, to: "cache.json")let database = SyncDatabaseFixture(changesFail: true)- let service = TokenSync(database: database, directoryURL: directory.url, isEnabled: { true })+ let service = TokenUsageSync(database: database, directoryURL: directory.url, isEnabled: { true })await #expect(throws: (any Error).self) {- try await service.synchronize(local: [], accountScopedDeviceID: "device", salt: Self.salt, zoneID: Self.zone)+ try await service.synchronize(local: [], accountScopedDeviceID: "device", zoneID: Self.zone)}#expect(try Data(contentsOf: url) == original)}- @Test(arguments: [false, true]) func activityFailureOnlyStopsTokensForSharedErrors(shared: Bool) async throws {+ @Test(arguments: [false, true]) func activityDataFailureOnlyStopsTokenUsageForSharedErrors(shared: Bool) async throws {let directory = try TestDirectory()defer { try? directory.remove() }let database = SyncDatabaseFixture(nextChangesError: shared ? .networkFailure : .invalidArguments)let service = SyncService(database: database,directoryURL: directory.url,- activityEventsDirectoryURL: directory.url.appendingPathComponent("Events"),isEnabled: { true })- _ = await service.synchronizeIfEnabled(localAggregates: [], localTokenTurns: [Self.turn(0)], trigger: .manual)- #expect(await database.savedTypes.contains(TokenSync.recordType) == !shared)+ _ = await service.synchronizeIfEnabled(localActivityDataAggregates: [], localTokenUsageAggregates: [Self.turn(0)], trigger: .manual)+ #expect(await database.savedTypes.contains(TokenUsageSync.recordType) == !shared)}- @Test(arguments: [false, true]) func partialTokenBatchContinuesAndRetainsSuccesses(readFailure: Bool) async throws {+ @Test(arguments: [false, true]) func partialTokenUsageBatchContinuesAndRetainsSuccesses(readFailure: Bool) async throws {let directory = try TestDirectory()defer { try? directory.remove() }- let turns = (0 ..< 30).map(Self.turn)- let badID = turns[0].pseudonymized(salt: Self.salt).id+ let aggregates = (0 ..< 30).map(Self.turn)+ let badID = "tokenUsage_device_" + aggregates[0].datelet database = SyncDatabaseFixture(failingName: badID, readFailure: readFailure)- let service = TokenSync(database: database, directoryURL: directory.url, isEnabled: { true })+ let service = TokenUsageSync(database: database, directoryURL: directory.url, isEnabled: { true })await #expect(throws: (any Error).self) {- try await service.synchronize(local: turns, accountScopedDeviceID: "device", salt: Self.salt, zoneID: Self.zone)+ try await service.synchronize(local: aggregates, accountScopedDeviceID: "device", zoneID: Self.zone)}- #expect(await database.savedTypes.filter { $0 == TokenSync.recordType }.count == 29)- #expect(try Self.cachedTurnCount(in: directory.url) == 29)+ #expect(await database.savedTypes.filter { $0 == TokenUsageSync.recordType }.count == 29)+ #expect(try Self.cachedDailyCount(in: directory.url) == 29)await database.repair()- try await service.synchronize(local: turns, accountScopedDeviceID: "device", salt: Self.salt, zoneID: Self.zone)- #expect(await database.savedTypes.filter { $0 == TokenSync.recordType }.count == 30)+ try await service.synchronize(local: aggregates, accountScopedDeviceID: "device", zoneID: Self.zone)+ #expect(await database.savedTypes.filter { $0 == TokenUsageSync.recordType }.count == 30)}- @Test func partialActivityBatchConfirmsSuccessesAndContinuesLaterDates() async throws {+ @Test(arguments: [CKError.Code.constraintViolation, .serverRecordChanged])+ func partialActivityDataBatchConfirmsSuccessesAndContinuesLaterDates(code: CKError.Code) async throws {let directory = try TestDirectory()defer { try? directory.remove() }let dates = (0 ..< 30).map { HistoryStorage.dateKey(for: Date().addingTimeInterval(Double(-$0) * 86400)) }- let aggregates = try dates.map { try directory.activityAggregate(date: $0) }- let database = SyncDatabaseFixture(failingDate: dates[0])+ let aggregates = try dates.map { try directory.activityDataAggregate(date: $0) }+ let database = SyncDatabaseFixture(failingDate: dates[0], uploadFailure: code)let service = SyncService(database: database,directoryURL: directory.url,- activityEventsDirectoryURL: directory.url.appendingPathComponent("Events"),isEnabled: { true })- _ = await service.synchronizeIfEnabled(localAggregates: aggregates, localTokenTurns: [Self.turn(0)], trigger: .manual)- #expect(ActivitySyncStore(directoryURL: directory.url).loadState().hashByDate.count == 29)- #expect(await database.savedTypes.filter { $0 == "Activity" }.count == 29)- #expect(await database.savedTypes.contains(TokenSync.recordType))+ _ = await service.synchronizeIfEnabled(localActivityDataAggregates: aggregates, localTokenUsageAggregates: [Self.turn(0)], trigger: .manual)+ #expect(try ActivityDataSyncStore(directoryURL: directory.url).load().records.count == 29)+ #expect(await database.savedTypes.filter { $0 == "ActivityData" }.count == 29)+ #expect(await database.savedTypes.contains(TokenUsageSync.recordType))+ #expect(await service.hasPendingActivityDataUpdates(local: aggregates))+ await database.repair()+ let restarted = SyncService(database: database, directoryURL: directory.url, isEnabled: { true })+ _ = await restarted.synchronizeIfEnabled(localActivityDataAggregates: aggregates, trigger: .manual)+ #expect(await database.savedTypes.filter { $0 == "ActivityData" }.count == 30)+ #expect(try ActivityDataSyncStore(directoryURL: directory.url).load().records.count == 30)+ #expect(await restarted.hasPendingActivityDataUpdates(local: aggregates) == false)+ }++ @Test func confirmedActivityDataSurvivesPostUploadFetchFailureAndRestart() async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let database = SyncDatabaseFixture()+ await database.setChangesPages([+ .success(SyncChanges(records: [:], deletions: [], token: nil, moreComing: false)),+ .failure(CKError(.networkFailure))+ ])+ let local = try directory.activityDataAggregate(date: HistoryStorage.dateKey(for: Date()))+ let service = SyncService(database: database, directoryURL: directory.url, isEnabled: { true })+ let snapshot = await service.synchronizeIfEnabled(localActivityDataAggregates: [local], trigger: .manual)+ #expect(!snapshot.isActivityDataComplete)+ let cache = try ActivityDataSyncStore(directoryURL: directory.url).load()+ let confirmed = try #require(cache.sortedRecords.first)+ #expect(confirmed.activityData == local.syncedAggregate)+ #expect(confirmed.updatedAt != nil)+ #expect(cache.state.lastUploadAt != nil)++ let restarted = SyncService(database: database, directoryURL: directory.url, isEnabled: { true })+ let restored = await restarted.synchronizeIfEnabled(localActivityDataAggregates: [local], trigger: .manual)+ #expect(restored.isActivityDataComplete)+ #expect(await database.savedTypes.filter { $0 == "ActivityData" }.count == 1)+ #expect(try ActivityDataSyncStore(directoryURL: directory.url).load().state.lastUploadAt == cache.state.lastUploadAt)}@Test func failedRecordDuringFetchDoesNotCommitPartialCache() async throws {let directory = try TestDirectory()defer { try? directory.remove() }let database = SyncDatabaseFixture()- let service = TokenSync(database: database, directoryURL: directory.url, isEnabled: { true })- try await service.synchronize(local: [Self.turn(0)], accountScopedDeviceID: "device", salt: Self.salt, zoneID: Self.zone)+ let service = TokenUsageSync(database: database, directoryURL: directory.url, isEnabled: { true })+ try await service.synchronize(local: [Self.turn(0)], accountScopedDeviceID: "device", zoneID: Self.zone)let url = directory.url.appendingPathComponent("cache.json")let original = try Data(contentsOf: url)await database.failChangesRecord()await #expect(throws: (any Error).self) {- try await service.synchronize(local: [Self.turn(1)], accountScopedDeviceID: "device", salt: Self.salt, zoneID: Self.zone)+ try await service.synchronize(local: [Self.turn(1)], accountScopedDeviceID: "device", zoneID: Self.zone)}#expect(try Data(contentsOf: url) == original)}- @Test func unsupportedTokenVersionIsNotTreatedAsCorruption() async throws {+ @Test func unsupportedTokenUsageVersionIsNotTreatedAsCorruption() async throws {let directory = try TestDirectory()defer { try? directory.remove() }let database = SyncDatabaseFixture()- let service = TokenSync(database: database, directoryURL: directory.url, isEnabled: { true })- try await service.synchronize(local: [], accountScopedDeviceID: "device", salt: Self.salt, zoneID: Self.zone)+ let service = TokenUsageSync(database: database, directoryURL: directory.url, isEnabled: { true })+ try await service.synchronize(local: [], accountScopedDeviceID: "device", zoneID: Self.zone)let url = directory.url.appendingPathComponent("cache.json")- let original = Data(#"{"version":999,"futureField":"unsupported"}"#.utf8)+ let original = Data(#"{"schemaVersion":999,"futureField":"unsupported"}"#.utf8)try original.write(to: url)await #expect(throws: (any Error).self) {- try await service.synchronize(local: [], accountScopedDeviceID: "device", salt: Self.salt, zoneID: Self.zone)+ try await service.synchronize(local: [], accountScopedDeviceID: "device", zoneID: Self.zone)}#expect(try Data(contentsOf: url) == original)}- @Test func activityCacheCommitsRecordsAndCursorTogether() throws {+ @Test func activityDataCacheCommitsRecordsAndCursorTogether() throws {let directory = try TestDirectory()defer { try? directory.remove() }- let store = ActivitySyncStore(directoryURL: directory.url)- let state = SyncState(deviceID: "device", hashByDate: ["2026-10-05": "hash"])+ let store = ActivityDataSyncStore(directoryURL: directory.url)+ let state = ActivityDataSyncState(deviceID: "device")try store.saveState(state)- let url = directory.url.appendingPathComponent("Activity/cache.json")+ let url = directory.url.appendingPathComponent("activityData/cache.json")var json = try #require(JSONSerialization.jsonObject(with: Data(contentsOf: url)) as? [String: Any])json["cursor"] = Data("previous cursor".utf8).base64EncodedString()try JSONSerialization.data(withJSONObject: json).write(to: url)@@ -298,66 +300,81 @@ struct SyncRecoveryTests {updated.lastPrunedDate = "2026-10-05"try store.saveState(updated)#expect(try store.load().cursor == Data("previous cursor".utf8))- let daily = SyncedActivity(date: "2026-10-05", generationID: "source", eventCount: 2, projectCounts: [:], modelCounts: [:])- let record = try ActivitySyncRecord(deviceID: "device", daily: daily, recordName: "device_2026-10-05_source")- try store.saveFetchedRecords([record], cursor: nil)- let reopened = try ActivitySyncStore(directoryURL: directory.url).load()+ let daily = SyncedActivityData(date: "2026-10-05", eventCount: 2, projectCounts: [:], modelCounts: [:])+ let record = try ActivityDataSyncRecord(deviceID: "device", activityData: daily, recordName: "activityData_device_2026-10-05")+ let uploadedAt = JSONLines.storageDate(Date())+ try store.saveConfirmedRecords([record], lastUploadAt: uploadedAt)+ let confirmed = try store.load()+ #expect(confirmed.cursor == Data("previous cursor".utf8))+ #expect(confirmed.sortedRecords == [record])+ #expect(confirmed.state.lastUploadAt == uploadedAt)+ updated.lastUploadAt = uploadedAt+ try store.saveFetchedRecords([record.id: record], cursor: nil)+ let reopened = try ActivityDataSyncStore(directoryURL: directory.url).load()#expect(reopened.state == updated)- #expect(reopened.records == [record])+ #expect(reopened.sortedRecords == [record])#expect(reopened.cursor == nil)let files = try FileManager.default.contentsOfDirectory(atPath: url.deletingLastPathComponent().path)#expect(Set(files) == ["cache.json", "store.lock"])- try store.reset(state: SyncState())- #expect(try store.load() == ActivitySyncCache())+ try store.reset(state: ActivityDataSyncState())+ #expect(try store.load() == ActivityDataSyncCache(state: ActivityDataSyncState()))}- @Test(arguments: ["missingFields", "missingGeneration", "mismatchedIdentity"])- func damagedActivityRecordsInvalidateCursorAndUploadState(_ damage: String) throws {+ @Test(arguments: ["missingFields", "missingDate", "mismatchedIdentity"])+ func damagedActivityDataRecordsInvalidateCursorAndUploadState(_ damage: String) throws {let directory = try TestDirectory()defer { try? directory.remove() }- let store = ActivitySyncStore(directoryURL: directory.url)- try store.saveState(SyncState(deviceID: "device", hashByDate: ["2026-10-05": "hash"]))- let url = directory.url.appendingPathComponent("Activity/cache.json")+ let store = ActivityDataSyncStore(directoryURL: directory.url)+ try store.saveState(ActivityDataSyncState(deviceID: "device"))+ let url = directory.url.appendingPathComponent("activityData/cache.json")var json = try #require(JSONSerialization.jsonObject(with: Data(contentsOf: url)) as? [String: Any])json["cursor"] = Data("stale cursor".utf8).base64EncodedString()- let daily: [String: Any] = damage == "missingGeneration"- ? ["date": "2026-10-05"] : ["date": "2026-10-05", "generationID": "source"]- json["records"] = damage == "missingFields" ? [["invalid": true]] : [[- "deviceID": "device", "recordName": "device_2026-10-05_other", "daily": daily- ]]+ var record: [String: Any] = [+ "schemaVersion": 1, "aggregationVersion": 1, "date": "2026-10-05",+ "deviceID": "device", "recordName": "activityData_device_2026-10-05", "activityData": [:]+ ]+ switch damage {+ case "missingFields": record = ["invalid": true]+ case "missingDate": record.removeValue(forKey: "date")+ default: record["recordName"] = "activityData_device_2026-10-05_other"+ }+ json["records"] = [record]try JSONSerialization.data(withJSONObject: json).write(to: url)- #expect(try store.load() == ActivitySyncCache())- try store.saveState(SyncState(deviceID: "new"))- #expect(try store.load().records.isEmpty)+ #expect(try store.load() == ActivityDataSyncCache(state: ActivityDataSyncState()))+ try store.saveState(ActivityDataSyncState(deviceID: "new"))+ #expect(try store.load().sortedRecords.isEmpty)#expect(try store.load().cursor == nil)#expect(try store.load().state.deviceID == "new")}- @Test func unsupportedActivityCacheIsNotOverwritten() throws {+ @Test func unsupportedActivityDataCacheIsNotOverwritten() throws {let directory = try TestDirectory()defer { try? directory.remove() }- let original = Data(#"{"version":999,"futureField":"unsupported"}"#.utf8)- let url = try directory.write(original, to: "Activity/cache.json")- let store = ActivitySyncStore(directoryURL: directory.url)- #expect(throws: SyncRecovery.Failure.self) { try store.saveState(SyncState()) }- #expect(throws: SyncRecovery.Failure.self) { try store.reset(state: SyncState()) }+ let original = Data(#"{"schemaVersion":999,"futureField":"unsupported"}"#.utf8)+ let url = try directory.write(original, to: "activityData/cache.json")+ let store = ActivityDataSyncStore(directoryURL: directory.url)+ #expect(throws: SyncRecovery.Failure.self) { try store.saveState(ActivityDataSyncState()) }+ #expect(throws: SyncRecovery.Failure.self) { try store.reset(state: ActivityDataSyncState()) }#expect(try Data(contentsOf: url) == original)}- @Test func failedActivitySavePreservesCacheAndCursor() throws {+ @Test func failedActivityDataSavePreservesCacheAndCursor() throws {let directory = try TestDirectory()defer { try? directory.remove() }- let store = ActivitySyncStore(directoryURL: directory.url)- try store.saveState(SyncState(deviceID: "device"))- let url = directory.url.appendingPathComponent("Activity/cache.json")+ let store = ActivityDataSyncStore(directoryURL: directory.url)+ try store.saveState(ActivityDataSyncState(deviceID: "device"))+ let url = directory.url.appendingPathComponent("activityData/cache.json")var json = try #require(JSONSerialization.jsonObject(with: Data(contentsOf: url)) as? [String: Any])json["cursor"] = Data("previous cursor".utf8).base64EncodedString()let original = try JSONSerialization.data(withJSONObject: json)try original.write(to: url)- let daily = SyncedActivity(date: "2026-10-05", generationID: "source", eventCount: 2, projectCounts: [:], modelCounts: [:])- let record = try ActivitySyncRecord(deviceID: "device", daily: daily, updatedAt: Date(timeIntervalSince1970: .infinity), recordName: "device_2026-10-05_source")+ let daily = SyncedActivityData(date: "2026-10-05", eventCount: 2, projectCounts: [:], modelCounts: [:])+ let record = try ActivityDataSyncRecord(+ deviceID: "device", activityData: daily, updatedAt: Date(timeIntervalSince1970: .infinity),+ recordName: "activityData_device_2026-10-05"+ )#expect(throws: EncodingError.self) {- try store.saveFetchedRecords([record], cursor: nil)+ try store.saveFetchedRecords([record.id: record], cursor: nil)}#expect(try Data(contentsOf: url) == original)}@@ -365,112 +382,117 @@ struct SyncRecoveryTests {@Test func sameSourceRecalculationCanReduceCountsWithoutDeletingRecord() async throws {let fixture = try await HistoryRebuildFixture()defer { try? fixture.directory.remove() }- let before = try #require(await fixture.database.activityRecords().first)+ let before = try #require(await fixture.database.activityDataRecords().first)let result = try await fixture.history.rebuildData(for: [fixture.date], synchronize: true)- let after = try #require(await fixture.database.activityRecords().first)+ let after = try #require(await fixture.database.activityDataRecords().first)#expect(before.recordID == after.recordID)- #expect(before["eventCount"] as? Int == 9)- #expect(after["eventCount"] as? Int == 1)+ #expect(try Self.activityDataEventCount(before) == 9)+ #expect(try Self.activityDataEventCount(after) == 1)#expect(!result.summary.isSyncPending)#expect(await fixture.database.deletedIDs.isEmpty)}- @Test func missingLocalGenerationFailsBeforeReplacingRemoteRecords() async throws {+ @Test func dailyActivityDataUploadDoesNotRequireLocalSourceMetadata() async throws {let directory = try TestDirectory()defer { try? directory.remove() }let database = SyncDatabaseFixture()let service = SyncService(database: database,directoryURL: directory.url,- activityEventsDirectoryURL: directory.url.appendingPathComponent("Events"),isEnabled: { true })- var local = try directory.activityAggregate(date: HistoryStorage.dateKey(for: Date()))+ var local = ActivityDataAggregate(date: HistoryStorage.dateKey(for: Date()))local.eventCount = 5- _ = await service.synchronizeIfEnabled(localAggregates: [local], trigger: .manual)- let before = await database.activityRecords().map(\.recordID)- local.generationID = nil- let result = await service.synchronizeIfEnabled(localAggregates: [local], trigger: .manual)+ _ = await service.synchronizeIfEnabled(localActivityDataAggregates: [local], trigger: .manual)+ let before = await database.activityDataRecords().map(\.recordID)+ local.eventCount = 3+ let result = await service.synchronizeIfEnabled(localActivityDataAggregates: [local], trigger: .manual)#expect(await database.deletedIDs.isEmpty)- #expect(await database.activityRecords().map(\.recordID) == before)- #expect(result.records.count == before.count)+ #expect(await database.activityDataRecords().map(\.recordID) == before)+ #expect(result.activityData.count == before.count)+ #expect(try await Self.activityDataEventCount(database.activityDataRecords().first) == 3)}- @Test func invalidGenerationDuringCursorBaselineStopsActivityUpload() async throws {+ @Test func invalidRecordDuringCursorBaselineStopsActivityDataUpload() async throws {let directory = try TestDirectory()defer { try? directory.remove() }let database = SyncDatabaseFixture()- let invalid = CKRecord(recordType: "Activity", recordID: CKRecord.ID(recordName: "device_invalid"))+ let invalid = CKRecord(recordType: "ActivityData", recordID: CKRecord.ID(recordName: "device_invalid"))invalid["deviceID"] = "device" as CKRecordValueinvalid["date"] = HistoryStorage.dateKey(for: Date()) as CKRecordValueawait database.injectChangesRecord(invalid)let service = SyncService(database: database,directoryURL: directory.url,- activityEventsDirectoryURL: directory.url.appendingPathComponent("Events"),isEnabled: { true })- var local = try directory.activityAggregate(date: HistoryStorage.dateKey(for: Date()))+ var local = try directory.activityDataAggregate(date: HistoryStorage.dateKey(for: Date()))local.eventCount = 1- _ = await service.synchronizeIfEnabled(localAggregates: [local], trigger: .manual)- #expect(await database.activityRecords().isEmpty)- #expect(try ActivitySyncStore(directoryURL: directory.url).load().cursor == nil)- #expect(ActivitySyncStore(directoryURL: directory.url).loadState().hashByDate.isEmpty)+ _ = await service.synchronizeIfEnabled(localActivityDataAggregates: [local], trigger: .manual)+ #expect(await database.activityDataRecords().isEmpty)+ #expect(try ActivityDataSyncStore(directoryURL: directory.url).load().cursor == nil)+ #expect(try ActivityDataSyncStore(directoryURL: directory.url).load().records.isEmpty)}- @Test func independentJournalSourcesAreNotDeletedByRebuild() async throws {+ @Test func recreatedJournalReplacesSameDeviceDateInsteadOfAddingAnotherBatch() async throws {let fixture = try await HistoryRebuildFixture()defer { try? fixture.directory.remove() }- let previous = try #require(await fixture.database.activityRecords().first)- let old = CKRecord(recordType: "Activity", recordID: CKRecord.ID(recordName: "older-source", zoneID: Self.zone))- var independent = fixture.local.syncedAggregate- independent.generationID = "independent"- try ActivityRecordCodec.apply(independent, deviceID: #require(previous["deviceID"] as? String), to: old)- await fixture.database.storeRecord(old)+ let previous = try #require(await fixture.database.activityDataRecords().first)+ try fixture.directory.replaceHistory(+ AppServerEventRecord(activityData: TestFixtures.event(at: Date())).jsonLineData(),+ date: fixture.date+ )_ = try await fixture.history.rebuildData(for: [fixture.date], synchronize: true)- #expect(await fixture.database.activityRecords().count == 2)+ let updated = try #require(await fixture.database.activityDataRecords().first)+ #expect(updated.recordID == previous.recordID)+ #expect(try Self.activityDataEventCount(updated) == 1)+ #expect(await fixture.database.activityDataRecords().count == 1)#expect(await fixture.database.deletedIDs.isEmpty)}- @Test func unknownCloudVersionPreservesCacheAndPreventsActivityUpload() async throws {+ @Test func unknownCloudVersionPreservesCacheAndPreventsActivityDataUpload() async throws {let fixture = try await HistoryRebuildFixture()defer { try? fixture.directory.remove() }- let cacheURL = HistoryStorage.syncDirectoryURL(in: fixture.directory.url).appendingPathComponent("Activity/cache.json")+ let cacheURL = HistoryStorage.syncDirectoryURL(in: fixture.directory.url).appendingPathComponent("activityData/cache.json")let before = try Data(contentsOf: cacheURL)- let unsupported = CKRecord(recordType: "Activity", recordID: CKRecord.ID(recordName: "future", zoneID: Self.zone))- unsupported["version"] = 999 as CKRecordValue+ let unsupported = CKRecord(recordType: "ActivityData", recordID: CKRecord.ID(recordName: "future", zoneID: Self.zone))+ unsupported["schemaVersion"] = 999 as CKRecordValueawait fixture.database.injectChangesRecord(unsupported)let result = await fixture.history.loadSnapshot(synchronize: true)- #expect(!result.isActivityComplete)+ #expect(!result.isActivityDataComplete)#expect(try Data(contentsOf: cacheURL) == before)- #expect(await fixture.database.activityRecords().first?["eventCount"] as? Int == 9)+ #expect(try await Self.activityDataEventCount(fixture.database.activityDataRecords().first) == 9)#expect(await fixture.database.deletedIDs.isEmpty)}@Test func unknownLocalSyncCacheFailsBeforeCloudMutation() async throws {let directory = try TestDirectory()defer { try? directory.remove() }- let original = Data("{\"version\":999}".utf8)- let url = try directory.write(original, to: "Activity/cache.json")+ let original = Data("{\"schemaVersion\":999}".utf8)+ let url = try directory.write(original, to: "activityData/cache.json")let database = SyncDatabaseFixture()let service = SyncService(database: database, directoryURL: directory.url, isEnabled: { true })- let result = await service.synchronizeIfEnabled(localAggregates: [], localTokenTurns: [Self.turn(0)], trigger: .manual)- #expect(!result.isActivityComplete)+ let result = await service.synchronizeIfEnabled(localActivityDataAggregates: [], localTokenUsageAggregates: [Self.turn(0)], trigger: .manual)+ #expect(!result.isActivityDataComplete)#expect(await database.savedTypes.isEmpty)#expect(await database.deletedIDs.isEmpty)#expect(try Data(contentsOf: url) == original)}- private static func cachedTurnCount(in directory: URL) throws -> Int {+ private static func activityDataEventCount(_ record: CKRecord?) throws -> Int? {+ guard let record else { return nil }+ return try ActivityDataRecordCodec.remoteDailyRecord(from: record)?.activityData.eventCount+ }++ private static func cachedDailyCount(in directory: URL) throws -> Int {let data = try Data(contentsOf: directory.appendingPathComponent("cache.json"))let json = try #require(JSONSerialization.jsonObject(with: data) as? [String: Any])- return try #require(json["turns"] as? [String: Any]).count+ return try #require(json["records"] as? [[String: Any]]).count}- private static func turn(_ index: Int) -> TokenTurn {- let id = TokenTurn.identifier(thread: "test", turn: String(index))- let now = Date().addingTimeInterval(Double(-index))- return TestFixtures.tokenTurn(id: id, rootID: id, startedAt: now, updatedAt: now, usage: .zero)+ private static func turn(_ index: Int) -> TokenUsageAggregate {+ let now = Date().addingTimeInterval(Double(-index) * 86400)+ return TestFixtures.tokenUsageAggregate(at: now, tokenUsage: .zero)}}@@ -484,6 +506,7 @@ private actor SyncDatabaseFixture: SyncDatabase {private var failingName: String?private var failingDate: String?private let readFailure: Bool+ private let uploadFailure: CKError.Codeprivate(set) var queryCount = 0private(set) var changesSinceNil: [Bool] = []private(set) var savedTypes: [String] = []@@ -491,7 +514,7 @@ private actor SyncDatabaseFixture: SyncDatabase {private(set) var deletedIDs: [CKRecord.ID] = []private var changesRecord: CKRecord?private var deletionFails = false- private var activitySaveCallback: (@Sendable () -> Void)?+ private var activityDataSaveCallback: (@Sendable () -> Void)?var remainingChangesPages: Int {changesPages.count@@ -505,16 +528,20 @@ private actor SyncDatabaseFixture: SyncDatabase {deletionFails = true}- func failActivityUpload(on date: String) {+ func failActivityDataUpload(on date: String) {failingDate = date}- func afterActivitySave(_ callback: @escaping @Sendable () -> Void) {- activitySaveCallback = callback+ func afterActivityDataSave(_ callback: @escaping @Sendable () -> Void) {+ activityDataSaveCallback = callback+ }++ func activityDataRecords() -> [CKRecord] {+ stored.values.filter { $0.recordType == "ActivityData" }.map(Self.copy)}- func activityRecords() -> [CKRecord] {- stored.values.filter { $0.recordType == "Activity" }.map(Self.copy)+ func removeRecord(_ id: CKRecord.ID) {+ stored[id] = nil}func storeRecord(_ record: CKRecord) {@@ -525,12 +552,13 @@ private actor SyncDatabaseFixture: SyncDatabase {changesRecord = Self.copy(record)}- init(nextChangesError: CKError.Code? = nil, changesFail: Bool = false, failingName: String? = nil, failingDate: String? = nil, readFailure: Bool = false) {+ init(nextChangesError: CKError.Code? = nil, changesFail: Bool = false, failingName: String? = nil, failingDate: String? = nil, readFailure: Bool = false, uploadFailure: CKError.Code = .constraintViolation) {self.nextChangesError = nextChangesErrorself.changesFail = changesFailself.failingName = failingNameself.failingDate = failingDateself.readFailure = readFailure+ self.uploadFailure = uploadFailure}private nonisolated static func copy(_ record: CKRecord) -> CKRecord {@@ -544,7 +572,7 @@ private actor SyncDatabaseFixture: SyncDatabase {func repair() {failingName = nilfailingDate = nil- activitySaveCallback = nil+ activityDataSaveCallback = nil}func records(for ids: [CKRecord.ID], desiredKeys _: [CKRecord.FieldKey]?) async throws -> Records {@@ -565,14 +593,14 @@ private actor SyncDatabaseFixture: SyncDatabase {) async throws -> Modification {var saved: Records = [:]for record in records {- if record.recordID.recordName == failingName || (failingDate != nil && record["date"] as? String == failingDate) {- saved[record.recordID] = .failure(CKError(.constraintViolation))+ if record.recordID.recordName == failingName || (record.recordType == "ActivityData" && failingDate != nil && record["date"] as? String == failingDate) {+ saved[record.recordID] = .failure(CKError(uploadFailure))} else {stored[record.recordID] = record.copy() as? CKRecordsaved[record.recordID] = .success(record)savedTypes.append(record.recordType)- if record.recordType == "Activity" {- activitySaveCallback?()+ if record.recordType == "ActivityData" {+ activityDataSaveCallback?()}}}@@ -633,15 +661,15 @@ extension SyncRecoveryTests {defer { try? fixture.directory.remove() }let outcome = try await fixture.history.rebuildData(for: [fixture.date], synchronize: false)#expect(outcome.summary.isSyncPending)- await fixture.database.failActivityUpload(on: fixture.date)+ await fixture.database.failActivityDataUpload(on: fixture.date)_ = await fixture.history.loadSnapshot(synchronize: true)- #expect(await fixture.database.activityRecords().first?["eventCount"] as? Int == 9)+ #expect(try await Self.activityDataEventCount(fixture.database.activityDataRecords().first) == 9)#expect(await fixture.database.deletedIDs.isEmpty)await fixture.database.repair()let restarted = HistoryService(directoryURL: fixture.directory.url, syncService: fixture.sync)_ = await restarted.loadSnapshotWithMaintenance(synchronize: true, trigger: .auto)- #expect(await fixture.database.activityRecords().first?["eventCount"] as? Int == 1)- #expect(await fixture.database.activityRecords().count == 1)+ #expect(try await Self.activityDataEventCount(fixture.database.activityDataRecords().first) == 1)+ #expect(await fixture.database.activityDataRecords().count == 1)}@Test func repeatedRebuildKeepsSourceAndNeedsNoSeparateAcknowledgement() async throws {@@ -649,52 +677,52 @@ extension SyncRecoveryTests {defer { try? fixture.directory.remove() }_ = try await fixture.history.rebuildData(for: [fixture.date], synchronize: false)_ = try await fixture.history.rebuildData(for: [fixture.date], synchronize: true)- let state = try HistoryStorage.loadMaintenanceState(in: fixture.directory.url)- #expect(state.days[fixture.date]?.generationID == fixture.local.generationID)- #expect(state.dirty.isEmpty)- #expect(await fixture.database.activityRecords().count == 1)+ let state = try HistoryDatabase(directoryURL: fixture.directory.url)+ #expect(try state.activityDataAggregates().first == fixture.local)+ #expect(try state.pendingActivityDataDates().isEmpty)+ #expect(await fixture.database.activityDataRecords().count == 1)#expect(await fixture.database.deletedIDs.isEmpty)}@Test func unavailableSourceCannotOverwriteRemoteAndRecoversAfterRepair() async throws {let fixture = try await HistoryRebuildFixture()defer { try? fixture.directory.remove() }- let original = try Data(contentsOf: fixture.eventURL)- try Data("broken journal\n".utf8).write(to: fixture.eventURL)+ let local = try HistoryDatabase(directoryURL: fixture.directory.url)+ let original = try local.exportJSONLines(date: fixture.date)+ try local.execute("UPDATE events SET payload = ?", [.data(Data("broken".utf8))])+ try local.execute("UPDATE aggregates SET activityData = CAST(json_set(CAST(activityData AS TEXT), '$.aggregationVersion', 0) AS BLOB)")await #expect(throws: (any Error).self) {try await fixture.history.rebuildData(for: [fixture.date], synchronize: true)}let restarted = HistoryService(directoryURL: fixture.directory.url, syncService: fixture.sync)_ = await restarted.loadSnapshotWithMaintenance(synchronize: true, trigger: .auto)- #expect(await fixture.database.activityRecords().first?["eventCount"] as? Int == 9)+ #expect(try await Self.activityDataEventCount(fixture.database.activityDataRecords().first) == 9)#expect(await fixture.database.deletedIDs.isEmpty)- try original.write(to: fixture.eventURL)+ try fixture.directory.replaceHistory(original, date: fixture.date)_ = await restarted.loadSnapshotWithMaintenance(synchronize: true, trigger: .auto)- #expect(await fixture.database.activityRecords().first?["eventCount"] as? Int == 1)+ #expect(try await Self.activityDataEventCount(fixture.database.activityDataRecords().first) == 1)}- @Test func rewrittenPrefixCannotOverwriteRemoteEvenWithMoreEvents() async throws {+ @Test func rewrittenLocalJournalReplacesCloudCounts() async throws {let fixture = try await HistoryRebuildFixture()defer { try? fixture.directory.remove() }let event = TestFixtures.event(.toolStarted, at: Date(), turn: "different")- try fixture.directory.writeJournal(AppServerEventRecord(activity: event).jsonLineData(), to: "Events/\(fixture.date).jsonl")+ try fixture.directory.replaceHistory(AppServerEventRecord(activityData: event).jsonLineData(), date: fixture.date)_ = try await fixture.history.rebuildData(for: [fixture.date], synchronize: true)- #expect(await fixture.database.activityRecords().first?["eventCount"] as? Int == 9)+ #expect(try await Self.activityDataEventCount(fixture.database.activityDataRecords().first) == 1)#expect(await fixture.database.deletedIDs.isEmpty)}@Test func requestSaveFailureDoesNotModifyAggregateOrCloud() async throws {let fixture = try await HistoryRebuildFixture()defer { try? fixture.directory.remove() }- let aggregateURL = HistoryStorage.dailyURL(in: fixture.directory.url)- let original = try Data(contentsOf: aggregateURL)- let stateDirectory = HistoryStorage.maintenanceURL(in: fixture.directory.url).deletingLastPathComponent()- try FileManager.default.setAttributes([.posixPermissions: 0o500], ofItemAtPath: stateDirectory.path)- defer { try? FileManager.default.setAttributes([.posixPermissions: 0o700], ofItemAtPath: stateDirectory.path) }+ let local = try HistoryDatabase(directoryURL: fixture.directory.url)+ let original = try local.activityDataAggregates()+ try local.execute("CREATE TRIGGER reject_aggregate BEFORE UPDATE ON aggregates BEGIN SELECT RAISE(ABORT, 'injected failure'); END")await #expect(throws: (any Error).self) {try await fixture.history.rebuildData(for: [fixture.date], synchronize: true)}- #expect(try Data(contentsOf: aggregateURL) == original)+ #expect(try local.activityDataAggregates() == original)#expect(await fixture.database.deletedIDs.isEmpty)}@@ -704,18 +732,18 @@ extension SyncRecoveryTests {let disabled = SyncService(database: fixture.database, directoryURL: HistoryStorage.syncDirectoryURL(in: fixture.directory.url), isEnabled: { false })let history = HistoryService(directoryURL: fixture.directory.url, syncService: disabled)_ = try await history.rebuildData(for: [fixture.date], synchronize: true)- #expect(await fixture.database.activityRecords().first?["eventCount"] as? Int == 9)+ #expect(try await Self.activityDataEventCount(fixture.database.activityDataRecords().first) == 9)_ = await fixture.history.loadSnapshot(synchronize: true)- #expect(await fixture.database.activityRecords().first?["eventCount"] as? Int == 1)+ #expect(try await Self.activityDataEventCount(fixture.database.activityDataRecords().first) == 1)}- @Test func appendedEventsKeepGenerationAndExtendCheckpoint() async throws {+ @Test func appendedEventsUpdateSameDeviceDailyCounts() async throws {let fixture = try await HistoryRebuildFixture()defer { try? fixture.directory.remove() }- let recorder = ActivityRecorder(directoryURL: fixture.directory.url)- let event = ActivityRecord(+ let activityDataRecorder = ActivityDataRecorder(directoryURL: fixture.directory.url)+ let event = ActivityDataRecord(timestamp: Date(),- name: ActivityEventKind.toolStarted.rawValue,+ name: ActivityDataEventKind.toolStarted.rawValue,origin: .main,cwd: nil,toolName: "exec_command",@@ -723,45 +751,33 @@ extension SyncRecoveryTests {effort: nil,threadID: "thread-a",turnID: "turn-a",- agentID: nil,+ agentThreadID: nil,id: "appended-tool")- try await recorder.record(event: event)+ try await activityDataRecorder.record(event: event)_ = await fixture.history.loadSnapshotWithMaintenance(synchronize: true, trigger: .auto)- let remote = try #require(await fixture.database.activityRecords().first)- #expect(remote["eventCount"] as? Int == 2)- #expect(remote["generationID"] as? String == fixture.local.generationID)- let updated = try #require(try ActivityRecordCodec.remoteDailyRecord(from: remote))- #expect(try #require(updated.daily.sourceCheckpoint).byteCount > #require(fixture.local.sourceCheckpoint).byteCount)+ let remote = try #require(await fixture.database.activityDataRecords().first)+ #expect(try Self.activityDataEventCount(remote) == 2)+ #expect(remote["sourceCheckpoint"] == nil)+ #expect(await fixture.database.activityDataRecords().count == 1)}- @Test func partialTokenFailureIsReportedWithoutClaimingWholeDaySuccess() async throws {+ @Test func partialTokenUsageFailureIsReportedWithoutClaimingWholeDaySuccess() async throws {let fixture = try await HistoryRebuildFixture()defer { try? fixture.directory.remove() }let now = Date()- let goodID = TokenTurn.identifier(thread: "good", turn: "summary")- let badID = TokenTurn.identifier(thread: "bad", turn: "summary")- let good = TokenObservation(- turn: TokenTurn(id: goodID, rootID: goodID, startedAt: now, updatedAt: now), rootStartedAt: now,- streamID: "good", sequence: 1, previous: .zero, current: .zero- )- let bad = TokenObservation(- turn: TokenTurn(id: badID, rootID: badID, startedAt: now, updatedAt: now), rootStartedAt: now,- streamID: "bad", sequence: 2, previous: .zero, current: .zero- )- let handle = try FileHandle(forWritingTo: fixture.eventURL)- try handle.seekToEnd()- try handle.write(contentsOf: AppServerEventRecord(observation: good, recordedAt: now).jsonLineData())- try handle.write(contentsOf: AppServerEventRecord(observation: bad, recordedAt: now).jsonLineData())- try handle.close()+ let good = TestFixtures.tokenUsageObservation(id: "good", at: now, tokenUsage: .zero)+ let bad = TokenUsageObservation(id: "bad", observedAt: now, previous: Self.counts(10), current: TokenUsageUpdate(total: .zero, last: .zero))+ let local = try HistoryDatabase(directoryURL: fixture.directory.url)+ try local.append(AppServerEventRecord(tokenUsage: good, recordedAt: now))+ try local.append(AppServerEventRecord(tokenUsage: bad, recordedAt: now))let result = try await fixture.history.rebuildData(for: [fixture.date], synchronize: false)#expect(result.summary.rebuiltDateCount == 0)#expect(result.summary.eventCount == 1)- #expect(result.summary.tokenTurnCount == 1)- #expect(result.summary.failedTokenDateKeys == [fixture.date])- #expect(result.summary.didFailTokenRebuild)+ #expect(result.summary.tokenUsageEventCount == 0)+ #expect(result.summary.failedTokenUsageDateKeys == [fixture.date])+ #expect(result.summary.didFailTokenUsageRebuild)#expect(result.summary.failedDateKeys.isEmpty)- #expect(result.summary.failedRequestDateKeys.isEmpty)}}@@ -771,8 +787,7 @@ private struct HistoryRebuildFixture {let sync: SyncServicelet history: HistoryServicelet date: String- let eventURL: URL- let local: ActivityAggregate+ let local: ActivityDataAggregateinit() async throws {directory = try TestDirectory()@@ -781,13 +796,13 @@ private struct HistoryRebuildFixture {history = HistoryService(directoryURL: directory.url, syncService: sync)let now = Date()date = HistoryStorage.dateKey(for: now)- eventURL = try directory.writeJournal(AppServerEventRecord(activity: TestFixtures.event(at: now), recordedAt: now).jsonLineData(), to: "Events/\(date).jsonl")+ try directory.replaceHistory(AppServerEventRecord(activityData: TestFixtures.event(at: now), recordedAt: now).jsonLineData(), date: date)_ = await history.loadSnapshotWithMaintenance(synchronize: false, trigger: .manual)- local = try #require(JSONLines.decode(ActivityAggregate.self, from: Data(contentsOf: HistoryStorage.dailyURL(in: directory.url))).first)+ local = try #require(HistoryDatabase(directoryURL: directory.url).activityDataAggregates().first)var remote = localremote.eventCount = 9- _ = await sync.synchronizeIfEnabled(localAggregates: [remote], trigger: .manual)- #expect(await database.activityRecords().count == 1)+ _ = await sync.synchronizeIfEnabled(localActivityDataAggregates: [remote], trigger: .manual)+ #expect(await database.activityDataRecords().count == 1)}}@@ -797,168 +812,332 @@ extension SyncRecoveryTests {defer { try? fixture.directory.remove() }var previous = fixture.localprevious.aggregationVersion = 1- let boundary = try #require(previous.sourceCheckpoint?.byteCount)- previous.sourceCheckpoint?.aggregationRanges = [.init(version: 1, end: boundary)]- try previous.jsonLineData().write(to: HistoryStorage.dailyURL(in: fixture.directory.url))- let remote = try #require(await fixture.database.activityRecords().first)- try ActivityRecordCodec.apply(previous.syncedAggregate, deviceID: #require(remote["deviceID"] as? String), to: remote)+ try HistoryDatabase(directoryURL: fixture.directory.url).execute(+ "UPDATE aggregates SET activityData = ? WHERE date = ?",+ [.data(JSONLines.stableEncoder.encode(previous)), .text(fixture.date)]+ )+ let remote = try #require(await fixture.database.activityDataRecords().first)+ try ActivityDataRecordCodec.apply(previous.syncedAggregate, deviceID: #require(remote["deviceID"] as? String), to: remote)_ = try await fixture.database.modifyRecords(saving: [remote], deleting: [], savePolicy: .ifServerRecordUnchanged, atomically: false)- _ = await fixture.sync.synchronizeIfEnabled(localAggregates: [previous], trigger: .manual)+ _ = await fixture.sync.synchronizeIfEnabled(localActivityDataAggregates: [previous], trigger: .manual)let outcome = try await fixture.history.rebuildData(for: [fixture.date], synchronize: true)- let updated = try #require(await fixture.database.activityRecords().first)- #expect(updated["eventCount"] as? Int == previous.eventCount)- #expect(updated["aggregationVersion"] as? Int == AggregationVersion.activity)+ let updated = try #require(await fixture.database.activityDataRecords().first)+ #expect(try Self.activityDataEventCount(updated) == previous.eventCount)+ #expect(updated["aggregationVersion"] as? Int == AggregationVersion.activityData)#expect(!outcome.summary.isSyncPending)- #expect(await fixture.database.activityRecords().count == 1)+ #expect(await fixture.database.activityDataRecords().count == 1)}}extension SyncRecoveryTests {- @Test(arguments: [true, false])- func recoveredTokensUseFreshCloudBaselineBeforeUploading(coversRemote: Bool) async throws {- let directory = try TestDirectory()- defer { try? directory.remove() }- let now = Date()- let id = TokenTurn.identifier(thread: "thread", turn: "turn")- let usage = TokenUsage(inputTokens: 10, cachedInputTokens: 0, cacheWriteInputTokens: 0, outputTokens: 0, reasoningOutputTokens: 0, totalTokens: 10)- let observation = TokenObservation(- turn: TokenTurn(id: id, rootID: id, startedAt: now, updatedAt: now),- rootStartedAt: now, streamID: "stream", sequence: 1, previous: .zero, current: usage- )- let store = TokenHistoryStore(directoryURL: directory.url)- var old = try #require(try await store.recordObservations([observation], now: now).first)- old.usage = TokenUsage(inputTokens: 12, cachedInputTokens: 0, cacheWriteInputTokens: 0, outputTokens: 0, reasoningOutputTokens: 0, totalTokens: 12)- old.aggregationVersion = 1- if !coversRemote {- old.checkpoint = ["missing-stream": TokenObservationCheckpoint(sequence: 1, usage: usage)]- }- let salt = Data("account".utf8)- let remote = old.pseudonymized(salt: salt)- let record = CKRecord(recordType: TokenSync.recordType, recordID: CKRecord.ID(recordName: remote.id, zoneID: Self.zone))- TokenSync.apply(remote, to: record)- let database = SyncDatabaseFixture()- await database.storeRecord(record)- try FileManager.default.removeItem(at: directory.url.appendingPathComponent("Aggregates/tokens.json"))- let recovered = try await store.refresh(now: now)- let sync = TokenSync(database: database, directoryURL: directory.url.appendingPathComponent("Sync/Tokens"), isEnabled: { true })- try await sync.synchronize(local: recovered, recoveredIDs: store.pendingRecoveryIDs(), accountScopedDeviceID: "device", salt: salt, zoneID: Self.zone)- let saved = try #require(try await database.records(for: [record.recordID], desiredKeys: nil)[record.recordID]?.get())- let updated = try #require(try TokenSync.turn(from: saved))- if !coversRemote {- #expect(updated == remote)- #expect(await store.pendingRecoveryIDs().contains(id))- return- }- #expect(updated.usage == usage)- #expect(!updated.hasConflict)- #expect(updated.aggregationVersion == AggregationVersion.tokens)- #expect(updated.ancestorIDs.contains(old.generationID))- let more = TokenUsage(inputTokens: 15, cachedInputTokens: 0, cacheWriteInputTokens: 0, outputTokens: 0, reasoningOutputTokens: 0, totalTokens: 15)- let next = TokenObservation(- turn: observation.turn.emptyTurn, rootStartedAt: now, streamID: "stream", sequence: 2,- previous: usage, current: more- )- _ = try await store.recordObservations([next], now: now)- let cloudBaseline = TokenHistoryBaseline(salt: salt, turns: [updated.id: updated])- let reconciled = try #require(try await store.refresh(now: now, baseline: cloudBaseline).first)- #expect(reconciled.usage == more)- #expect(!reconciled.hasConflict)- #expect(reconciled.ancestorIDs.contains(updated.generationID))- #expect(await store.pendingRecoveryIDs().isEmpty)- #expect(try await store.refresh(now: now, baseline: cloudBaseline).first?.generationID == reconciled.generationID)- }-@Test func remoteDateDoesNotHideMissingLocalContribution() async throws {let directory = try TestDirectory()defer { try? directory.remove() }let now = Date()let date = HistoryStorage.dateKey(for: now)- var remote = ActivityAggregate(date: date, generationID: "other-source")+ var remote = ActivityDataAggregate(date: date)remote.turnCount = 8- let record = try ActivitySyncRecord(- deviceID: "other", daily: remote.syncedAggregate,- recordName: ActivitySyncRecord.recordName(deviceID: "other", date: date, generation: "other-source")+ let record = try ActivityDataSyncRecord(+ deviceID: "other", activityData: remote.syncedAggregate,+ recordName: ActivityDataSyncRecord.recordName(deviceID: "other", date: date))let root = HistoryStorage.syncDirectoryURL(in: directory.url)- try ActivitySyncStore(directoryURL: root).saveCachedRecords([record], state: SyncState(deviceID: "local"))- var journal = AppServerEventJournal()- try journal.append(AppServerEventRecord(activity: TestFixtures.event(at: now)), in: directory.url)+ let store = ActivityDataSyncStore(directoryURL: root)+ try store.saveState(ActivityDataSyncState(deviceID: "local"))+ try store.saveFetchedRecords([record.id: record], cursor: nil)+ try HistoryDatabase(directoryURL: directory.url).append(AppServerEventRecord(activityData: TestFixtures.event(at: now)))let sync = SyncService(database: SyncDatabaseFixture(), directoryURL: root, isEnabled: { true })let history = HistoryService(directoryURL: directory.url, syncService: sync)let snapshot = await history.loadSnapshot()let day = UsageHeatmapDay.grid(usage: nil, history: snapshot, columnCount: 1, today: now).compactMap(\.self).first { $0.startDate == date }- #expect(snapshot.unavailableActivityDates.contains(date))- #expect(day?.history.turnCount == nil)+ #expect(snapshot.unavailableActivityDataDates.contains(date))+ #expect(day?.activityData.turnCount == nil)}}extension SyncRecoveryTests {- @Test(arguments: [true, false])- func recoveringOneTaskDoesNotReplaceUnrecoverableCloudTask(hasLocalSnapshot: Bool) async throws {+ @Test func activityDataAndTokenUsageKeepSeparateDailyRecordsAndMergeOtherDevicesOnlyForDisplay() async throws {let directory = try TestDirectory()defer { try? directory.remove() }+ let database = SyncDatabaseFixture()+ let sync = SyncService(database: database, directoryURL: directory.url, isEnabled: { true })+ let date = HistoryStorage.dateKey(for: Date())+ var local = ActivityDataAggregate(date: date)+ local.turnCount = 2+ var turn = Self.turn(0)+ turn.tokenUsage = Self.counts(100)+ let initial = await sync.synchronizeIfEnabled(localActivityDataAggregates: [local], localTokenUsageAggregates: [turn], trigger: .manual)+ let deviceID = try #require(initial.currentDeviceID)+ let activityDataID = CKRecord.ID(recordName: ActivityDataSyncRecord.recordName(deviceID: deviceID, date: date), zoneID: Self.zone)+ let tokenUsageID = TokenUsageSync.recordID("tokenUsage_\(deviceID)_\(date)", zoneID: Self.zone)+ #expect(activityDataID != tokenUsageID)+ let own = try await database.records(for: [activityDataID, tokenUsageID], desiredKeys: nil)+ #expect(try own[activityDataID]?.get().recordType == "ActivityData")+ #expect(try own[tokenUsageID]?.get().recordType == "TokenUsage")++ var other = ActivityDataAggregate(date: date)+ other.turnCount = 3+ let otherActivityData = CKRecord(+ recordType: "ActivityData",+ recordID: CKRecord.ID(recordName: ActivityDataSyncRecord.recordName(deviceID: "other", date: date), zoneID: Self.zone)+ )+ try ActivityDataRecordCodec.apply(other.syncedAggregate, deviceID: "other", to: otherActivityData)+ await database.storeRecord(otherActivityData)+ try await database.storeRecord(Self.tokenUsageRecord(TokenUsageSyncRecord(deviceID: "other", date: date, tokenUsage: Self.counts(900))))++ let combined = await sync.synchronizeIfEnabled(localActivityDataAggregates: [local], localTokenUsageAggregates: [turn], trigger: .manual)+ let snapshot = HistorySnapshot(localActivityDataAggregates: [local], syncedActivityDataRecords: combined.activityData, currentDeviceID: combined.currentDeviceID)+ #expect(snapshot.activityData.first?.turnCount == 5)+ #expect(combined.tokenUsageByDate?[date] == Self.counts(1000))+ #expect(await database.savedTypes.filter { $0 == "ActivityData" }.count == 1)+ #expect(await database.savedTypes.filter { $0 == "TokenUsage" }.count == 1)++ local.turnCount = 1+ turn.tokenUsage = Self.counts(50)+ let reduced = await sync.synchronizeIfEnabled(localActivityDataAggregates: [local], localTokenUsageAggregates: [turn], trigger: .manual)+ let updated = HistorySnapshot(localActivityDataAggregates: [local], syncedActivityDataRecords: reduced.activityData, currentDeviceID: reduced.currentDeviceID)+ #expect(updated.activityData.first?.turnCount == 4)+ #expect(reduced.tokenUsageByDate?[date] == Self.counts(950))+ #expect(await database.activityDataRecords().count == 2)+ let saved = try #require(try await database.records(for: [activityDataID], desiredKeys: nil)[activityDataID]?.get())+ #expect(try ActivityDataRecordCodec.remoteDailyRecord(from: saved)?.activityData.turnCount == 1)+ }++ private static func counts(_ input: Int64) -> TokenUsage {+ TokenUsage(inputTokens: input, cachedInputTokens: 0, cacheWriteInputTokens: 0, outputTokens: 0, reasoningOutputTokens: 0, totalTokens: input)+ }++ private static func tokenUsageRecord(_ daily: TokenUsageSyncRecord) throws -> CKRecord {+ let record = CKRecord(recordType: TokenUsageSync.recordType, recordID: TokenUsageSync.recordID(daily.id, zoneID: zone))+ try TokenUsageSync.apply(daily, to: record)+ return record+ }++ @Test(arguments: [nil, TokenUsage.zero, TokenUsage(+ inputTokens: 100, cachedInputTokens: 20, cacheWriteInputTokens: 5,+ outputTokens: 10, reasoningOutputTokens: 2, totalTokens: 110+ )])+ func cloudDailyTokenUsageAlwaysEncodeAllQuantityKeys(tokenUsage: TokenUsage?) throws {+ let daily = TokenUsageSyncRecord(deviceID: "device", date: "2026-10-12", tokenUsage: tokenUsage)+ let record = try Self.tokenUsageRecord(daily)+ #expect(record.recordID.recordName == "tokenUsage_device_2026-10-12")+ #expect(Set(record.allKeys()) == ["schemaVersion", "aggregationVersion", "deviceID", "date", "tokenUsage", "updatedAt"])+ let data = try #require(record["tokenUsage"] as? Data)+ let counts = try #require(JSONSerialization.jsonObject(with: data) as? [String: Any])+ #expect(Set(counts.keys) == ["inputTokens", "cachedInputTokens", "cacheWriteInputTokens", "outputTokens", "reasoningOutputTokens", "totalTokens"])+ if let tokenUsage {+ #expect(try JSONDecoder().decode(TokenUsage.self, from: data) == tokenUsage)+ } else {+ #expect(counts.values.allSatisfy { $0 is NSNull })+ }+ let restored = try #require(try TokenUsageSync.dailyRecord(from: record))+ #expect(restored.hasSameData(as: daily))+ #expect(restored.updatedAt == record["updatedAt"] as? Date)+ record["deviceID"] = "other" as CKRecordValue+ #expect(throws: StorageCompatibilityError.self) { try TokenUsageSync.dailyRecord(from: record) }+ }++ @Test func cloudDailyTokenUsageRejectIncompleteKeysAndInvalidQuantities() throws {+ let daily = TokenUsageSyncRecord(deviceID: "device", date: "2026-10-12", tokenUsage: .zero)+ let record = try Self.tokenUsageRecord(daily)+ let data = try #require(record["tokenUsage"] as? Data)+ var counts = try #require(JSONSerialization.jsonObject(with: data) as? [String: Any])+ counts.removeValue(forKey: "outputTokens")+ record["tokenUsage"] = try JSONSerialization.data(withJSONObject: counts) as CKRecordValue+ #expect(throws: StorageCompatibilityError.self) { try TokenUsageSync.dailyRecord(from: record) }+ counts["outputTokens"] = -1+ record["tokenUsage"] = try JSONSerialization.data(withJSONObject: counts) as CKRecordValue+ #expect(throws: StorageCompatibilityError.self) { try TokenUsageSync.dailyRecord(from: record) }+ counts["outputTokens"] = NSNull()+ record["tokenUsage"] = try JSONSerialization.data(withJSONObject: counts) as CKRecordValue+ #expect(try TokenUsageSync.dailyRecord(from: record)?.tokenUsage == nil)+ }++ @Test func dailyTokenUsageReplaceOneDeviceDateIncludingDecreasesAndZero() async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let database = SyncDatabaseFixture()+ let service = TokenUsageSync(database: database, directoryURL: directory.url, isEnabled: { true })+ var daily = Self.turn(0)+ daily.tokenUsage = Self.counts(300)+ let recordID = TokenUsageSync.recordID("tokenUsage_device_\(daily.date)", zoneID: Self.zone)+ try await service.synchronize(local: [daily], accountScopedDeviceID: "device", zoneID: Self.zone)+ #expect(await database.savedTypes == ["TokenUsage"])+ #expect(try await service.snapshot(local: [daily], accountScopedDeviceID: "device")[daily.date] == Self.counts(300))+ #expect(await service.hasPendingUpdates(local: [daily], accountScopedDeviceID: "device") == false)+ try await service.synchronize(local: [daily], accountScopedDeviceID: "device", zoneID: Self.zone)+ #expect(await database.savedTypes == ["TokenUsage"])+ daily.tokenUsage = Self.counts(250)+ #expect(await service.hasPendingUpdates(local: [daily], accountScopedDeviceID: "device"))+ try await service.synchronize(local: [daily], accountScopedDeviceID: "device", zoneID: Self.zone)+ let saved = try #require(try await database.records(for: [recordID], desiredKeys: nil)[recordID]?.get())+ #expect(try TokenUsageSync.dailyRecord(from: saved)?.tokenUsage == Self.counts(250))+ #expect(try Self.cachedDailyCount(in: directory.url) == 1)+ daily.tokenUsage = .zero+ try await service.synchronize(local: [daily], accountScopedDeviceID: "device", zoneID: Self.zone)+ #expect(try await service.snapshot(local: [daily], accountScopedDeviceID: "device")[daily.date] == .zero)+ }++ @Test func otherDeviceDailyUsageIsNeverReuploadedAsLocalContribution() async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let database = SyncDatabaseFixture()+ var local = Self.turn(0)+ local.tokenUsage = Self.counts(100)+ let date = local.date+ let other = TokenUsageSyncRecord(deviceID: "other", date: date, tokenUsage: Self.counts(900))+ let otherRecord = try Self.tokenUsageRecord(other)+ await database.storeRecord(otherRecord)+ let service = TokenUsageSync(database: database, directoryURL: directory.url, isEnabled: { true })+ try await service.synchronize(local: [local], accountScopedDeviceID: "device", zoneID: Self.zone)+ #expect(try await service.snapshot(local: [local], accountScopedDeviceID: "device")[date] == Self.counts(1000))+ let localID = TokenUsageSync.recordID("tokenUsage_device_\(date)", zoneID: Self.zone)+ let saved = try #require(try await database.records(for: [localID], desiredKeys: nil)[localID]?.get())+ #expect(try TokenUsageSync.dailyRecord(from: saved)?.tokenUsage == Self.counts(100))+ let restarted = TokenUsageSync(database: database, directoryURL: directory.url, isEnabled: { true })+ try await restarted.synchronize(local: [local], accountScopedDeviceID: "device", zoneID: Self.zone)+ #expect(await database.savedTypes == ["TokenUsage"])+ #expect(try await restarted.snapshot(local: [local], accountScopedDeviceID: "new-account")[date] == Self.counts(100))+ #expect(try await restarted.snapshot(local: [], accountScopedDeviceID: "device")[date] == Self.counts(1000))+ }++ @Test func deviceDailyContributionsAreAddedWithoutComparingTurnIdentities() async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let database = SyncDatabaseFixture()+ var local = Self.turn(0)+ local.tokenUsage = Self.counts(100)+ let other = try #require(TokenUsageSyncRecord.dailyRecords(local: [local], deviceID: "other").first)+ try await database.storeRecord(Self.tokenUsageRecord(other))+ let sync = TokenUsageSync(database: database, directoryURL: directory.url, isEnabled: { true })+ try await sync.synchronize(local: [local], accountScopedDeviceID: "device", zoneID: Self.zone)+ #expect(try await sync.snapshot(local: [local], accountScopedDeviceID: "device")[other.date] == Self.counts(200))+ }++ @Test func rebuiltLocalObservationsReplaceCloudDailyTotalsWithoutCloudTurnState() async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let now = Date()+ let observation = TestFixtures.tokenUsageObservation(at: now, tokenUsage: Self.counts(100))+ let store = TokenUsageHistoryStore(directoryURL: directory.url)+ var old = try #require(try await store.recordObservations([observation], now: now).first)+ old.tokenUsage = Self.counts(120)+ try await directory.seedTokenUsageAggregates([old], now: now)+ let database = SyncDatabaseFixture()+ let sync = TokenUsageSync(database: database, directoryURL: directory.url.appendingPathComponent("Sync/tokenUsage"), isEnabled: { true })+ try await sync.synchronize(local: [old], accountScopedDeviceID: "device", zoneID: Self.zone)+ let date = HistoryStorage.dateKey(for: now)+ _ = try await store.rebuild(for: [date])+ let aggregates = try await store.persistedAggregates(now: now)+ try await sync.synchronize(local: aggregates, accountScopedDeviceID: "device", zoneID: Self.zone)+ let recordID = TokenUsageSync.recordID("tokenUsage_device_\(date)", zoneID: Self.zone)+ let saved = try #require(try await database.records(for: [recordID], desiredKeys: nil)[recordID]?.get())+ #expect(try TokenUsageSync.dailyRecord(from: saved)?.tokenUsage == Self.counts(100))+ #expect(await sync.hasPendingUpdates(local: aggregates, accountScopedDeviceID: "device") == false)+ try HistoryDatabase(directoryURL: directory.url).execute("""+ UPDATE aggregates SET tokenUsage = NULL+ """)+ let recovered = try await TokenUsageHistoryStore(directoryURL: directory.url).refresh(now: now)+ try await sync.synchronize(local: recovered, accountScopedDeviceID: "device", zoneID: Self.zone)+ #expect(await database.savedTypes == ["TokenUsage", "TokenUsage"])+ }++ @Test func incompleteLocalHistoryPreservesCloudDailyUsageUntilSourceIsRepaired() async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let database = SyncDatabaseFixture()+ let sync = SyncService(database: database, directoryURL: HistoryStorage.syncDirectoryURL(in: directory.url), isEnabled: { true })+ let history = HistoryService(directoryURL: directory.url, syncService: sync)let now = Date()- func counts(_ value: Int64) -> TokenUsage {- TokenUsage(inputTokens: value, cachedInputTokens: 0, cacheWriteInputTokens: 0, outputTokens: 0, reasoningOutputTokens: 0, totalTokens: value)+ let date = HistoryStorage.dateKey(for: now)+ func entry(_ name: String, count: Int64) throws -> Data {+ let observation = TestFixtures.tokenUsageObservation(id: name, at: now, tokenUsage: Self.counts(count))+ return try AppServerEventRecord(tokenUsage: observation, recordedAt: now).jsonLineData()}- func observation(_ name: String, sequence: Int64 = 1, previous: Int64 = 0, current: Int64) -> TokenObservation {- let id = TokenTurn.identifier(thread: name, turn: "turn")- return TokenObservation(- turn: TokenTurn(id: id, rootID: id, startedAt: now, updatedAt: now), rootStartedAt: now,- streamID: name, sequence: sequence, previous: counts(previous), current: counts(current)- )+ try directory.replaceHistory(entry("first", count: 100) + entry("second", count: 200), date: date)+ let initial = await history.loadSnapshot(synchronize: true)+ #expect(initial.tokenUsageByDate[date] == Self.counts(300))+ let deviceID = try #require(ActivityDataSyncStore(directoryURL: HistoryStorage.syncDirectoryURL(in: directory.url)).loadState().deviceID)+ let recordID = TokenUsageSync.recordID("tokenUsage_\(deviceID)_\(date)", zoneID: Self.zone)+ try HistoryDatabase(directoryURL: directory.url).execute("""+ UPDATE aggregates SET tokenUsage = NULL+ """)+ try directory.replaceHistory(entry("first", count: 80) + entry("second", count: 200), date: date)+ try HistoryDatabase(directoryURL: directory.url).execute("UPDATE events SET payload = ? WHERE identity = 'tokenUsage:second'", [.data(Data("broken".utf8))])++ for _ in 0 ..< 2 {+ let partial = await history.loadSnapshot(synchronize: true)+ #expect(partial.tokenUsageByDate[date] == Self.counts(300))+ let saved = try #require(try await database.records(for: [recordID], desiredKeys: nil)[recordID]?.get())+ #expect(try TokenUsageSync.dailyRecord(from: saved)?.tokenUsage == Self.counts(300))}- let a = observation("a", current: 100)- let b = observation("b", current: 200)- var oldA = try a.applying(to: nil)- oldA.usage = counts(120)- let oldB = try b.applying(to: nil)- let remoteA = oldA.pseudonymized(salt: Self.salt)- let remoteB = oldB.pseudonymized(salt: Self.salt)+ #expect(await database.savedTypes.filter { $0 == "TokenUsage" }.count == 1)++ try HistoryDatabase(directoryURL: directory.url).execute("""+ UPDATE aggregates SET tokenUsage = NULL+ """)+ try directory.replaceHistory(entry("first", count: 80) + entry("second", count: 200), date: date)+ let repaired = await history.loadSnapshot(synchronize: true)+ #expect(repaired.tokenUsageByDate[date] == Self.counts(280))+ let saved = try #require(try await database.records(for: [recordID], desiredKeys: nil)[recordID]?.get())+ #expect(try TokenUsageSync.dailyRecord(from: saved)?.tokenUsage == Self.counts(280))+ #expect(await database.savedTypes.filter { $0 == "TokenUsage" }.count == 2)+ }+}++extension SyncRecoveryTests {+ @Test func bothSyncCachesPersistTheSameCloudRecordEnvelope() async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }let database = SyncDatabaseFixture()- let aID = TokenSync.recordID(remoteA.id, zoneID: Self.zone)- let bID = TokenSync.recordID(remoteB.id, zoneID: Self.zone)- for turn in [remoteA, remoteB] {- let record = CKRecord(recordType: TokenSync.recordType, recordID: TokenSync.recordID(turn.id, zoneID: Self.zone))- TokenSync.apply(turn, to: record)- await database.storeRecord(record)- }- let brokenB = observation("b", sequence: 3, previous: 250, current: 300)- var data = try AppServerEventRecord(observation: a, recordedAt: now).jsonLineData()- + AppServerEventRecord(observation: brokenB, recordedAt: now).jsonLineData()- if hasLocalSnapshot {- for turn in [oldA, oldB] {- try data.append(AppServerEventRecord(token: turn, recordedAt: now).jsonLineData())- }+ let date = HistoryStorage.dateKey(for: Date())+ let local = try directory.activityDataAggregate(date: date)+ let service = SyncService(database: database, directoryURL: directory.url, isEnabled: { true })+ _ = await service.synchronizeIfEnabled(localActivityDataAggregates: [local], localTokenUsageAggregates: [Self.turn(0)], trigger: .manual)++ var deviceIDs: [String] = []+ for kind in ["activityData", "tokenUsage"] {+ let data = try Data(contentsOf: directory.url.appendingPathComponent("\(kind)/cache.json"))+ let cache = try #require(JSONSerialization.jsonObject(with: data) as? [String: Any])+ #expect(Set(cache.keys) == ["schemaVersion", "state", "cursor", "records"])+ let state = try #require(cache["state"] as? [String: Any])+ #expect(state["containerIdentifier"] as? String == SyncCloudKit.containerIdentifier)+ try deviceIDs.append(#require(state["deviceID"] as? String))+ let records = try #require(cache["records"] as? [[String: Any]])+ #expect(records.count == 1)+ let record = try #require(records.first)+ #expect(Set(record.keys) == ["schemaVersion", "aggregationVersion", "deviceID", "date", "recordName", "updatedAt", kind])+ #expect(record["date"] as? String == date)+ let recordName = try #require(record["recordName"] as? String)+ let id = CKRecord.ID(recordName: recordName, zoneID: Self.zone)+ let remote = try #require(try await database.records(for: [id], desiredKeys: nil)[id]?.get())+ let remoteUpdatedAt = try #require(remote["updatedAt"] as? Date)+ let cachedUpdatedAt = try #require(record["updatedAt"] as? Double)+ #expect(abs(cachedUpdatedAt / 1000 - remoteUpdatedAt.timeIntervalSince1970) < 0.001)+ let remotePayload = try #require(remote[kind] as? Data)+ let remoteCounts = try #require(JSONSerialization.jsonObject(with: remotePayload) as? NSDictionary)+ let cachedCounts = try #require(record[kind] as? NSDictionary)+ #expect(cachedCounts == remoteCounts)}- try directory.writeJournal(data, to: "Events/\(HistoryStorage.dateKey(for: now)).jsonl")- try directory.write("broken cache", to: "Aggregates/tokens.json")- let store = TokenHistoryStore(directoryURL: directory.url)- let recovered = try await store.refresh(now: now)- #expect(recovered.first { $0.id == a.turn.id }?.usage == counts(100))- #expect(recovered.first { $0.id == b.turn.id } == (hasLocalSnapshot ? oldB : nil))- #expect(await store.pendingRecoveryIDs() == [a.turn.id])- let sync = TokenSync(database: database, directoryURL: directory.url.appendingPathComponent("Sync/Tokens"), isEnabled: { true })- try await sync.synchronize(local: recovered, recoveredIDs: store.pendingRecoveryIDs(), accountScopedDeviceID: "device", salt: Self.salt, zoneID: Self.zone)- let saved = try await database.records(for: [aID, bID], desiredKeys: nil)- let aRecord = try #require(try saved[aID]?.get())- let bRecord = try #require(try saved[bID]?.get())- let savedA = try #require(try TokenSync.turn(from: aRecord))- let savedB = try #require(try TokenSync.turn(from: bRecord))- #expect(savedA.usage == counts(100))- #expect(!savedA.hasConflict)- #expect(savedA.aggregationVersion == AggregationVersion.tokens)- #expect(savedA.ancestorIDs.contains(oldA.generationID))- #expect(savedB == remoteB)- #expect(await database.savedTypes == [TokenSync.recordType])- #expect(await database.deletedIDs.isEmpty)- let baseline = try #require(await sync.recoveryBaseline(accountScopedDeviceID: "device"))- let reopened = TokenHistoryStore(directoryURL: directory.url)- let reconciled = try await reopened.refresh(now: now, baseline: baseline)- try await sync.synchronize(local: reconciled, recoveredIDs: reopened.pendingRecoveryIDs(), accountScopedDeviceID: "device", salt: Self.salt, zoneID: Self.zone)- #expect(await database.savedTypes == [TokenSync.recordType])- #expect(await database.deletedIDs.isEmpty)- let total = try await sync.snapshot(local: reconciled, accountScopedDeviceID: "device")- #expect(total[HistoryStorage.dateKey(for: now)] == counts(300))+ #expect(deviceIDs[0] == deviceIDs[1])+ }++ @Test func tokenUsageCloudTimestampSurvivesCacheRestartWithoutReupload() async throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ let database = SyncDatabaseFixture()+ let local = Self.turn(0)+ let service = TokenUsageSync(database: database, directoryURL: directory.url, isEnabled: { true })+ try await service.synchronize(local: [local], accountScopedDeviceID: "device", zoneID: Self.zone)+ let url = directory.url.appendingPathComponent("cache.json")+ let first = try JSONLines.decoder.decode(TokenUsageSyncCache.self, from: Data(contentsOf: url))+ let before = try #require(first.sortedRecords.first)+ #expect(before.updatedAt != nil)+ let restarted = TokenUsageSync(database: database, directoryURL: directory.url, isEnabled: { true })+ #expect(await restarted.hasPendingUpdates(local: [local], accountScopedDeviceID: "device") == false)+ try await restarted.synchronize(local: [local], accountScopedDeviceID: "device", zoneID: Self.zone)+ let after = try JSONLines.decoder.decode(TokenUsageSyncCache.self, from: Data(contentsOf: url))+ #expect(after.records[before.id] == before)+ #expect(await database.savedTypes == ["TokenUsage"])}}diff --git a/CodexBarTests/Sync/ZoneRecoveryTests.swift b/CodexBarTests/Sync/ZoneRecoveryTests.swiftindex 3edd20e..0eba1bb 100644--- a/CodexBarTests/Sync/ZoneRecoveryTests.swift+++ b/CodexBarTests/Sync/ZoneRecoveryTests.swift@@ -12,31 +12,34 @@ struct ZoneRecoveryTests {defer { try? directory.remove() }let database = ZoneDatabase()await database.removeZone(code: code, wrapped: wrapped)- _ = try directory.write("invalid activity cache", to: "Activity/cache.json")- _ = try directory.write("invalid token cache", to: "Tokens/cache.json")+ _ = try directory.write("invalid activityData cache", to: "activityData/cache.json")+ _ = try directory.write("invalid token cache", to: "tokenUsage/cache.json")let service = service(database, directory)- let snapshot = try await service.synchronizeIfEnabled(localAggregates: [aggregate(0, directory: directory)], localTokenTurns: [turn(0)], trigger: .manual)+ let snapshot = try await service.synchronizeIfEnabled(+ localActivityDataAggregates: [aggregate(0, directory: directory)],+ localTokenUsageAggregates: [turn(0)], trigger: .manual+ )#expect(snapshot.currentDeviceID != nil)#expect(await database.creations == 1)- #expect(await database.recordCount("Activity") == 1)- #expect(await database.recordCount("Tokens") == 1)- #expect(ActivitySyncStore(directoryURL: directory.url).loadState().hashByDate.count == 1)+ #expect(await database.recordCount("ActivityData") == 1)+ #expect(await database.recordCount("TokenUsage") == 1)+ #expect(try ActivityDataSyncStore(directoryURL: directory.url).load().records.count == 1)}- @Test(arguments: [ZoneDatabase.Point.metadata, .query, .changes, .activityUpload, .tokenUpload], [false, true])+ @Test(arguments: [ZoneDatabase.Point.metadata, .query, .changes, .activityDataUpload, .tokenUsageUpload], [false, true])private func deletionDuringSyncResetsBothPipelinesAndRetries(point: ZoneDatabase.Point, wrapped: Bool) async throws {let directory = try TestDirectory()defer { try? directory.remove() }let database = ZoneDatabase()let service = service(database, directory)_ = try await service.synchronizeIfEnabled(- localAggregates: [aggregate(0, directory: directory), aggregate(1, directory: directory)],- localTokenTurns: [turn(0), turn(1)],+ localActivityDataAggregates: [aggregate(0, directory: directory), aggregate(1, directory: directory)],+ localTokenUsageAggregates: [turn(0), turn(1)],trigger: .manual)- let previousID = ActivitySyncStore(directoryURL: directory.url).loadState().deviceID+ let previousID = ActivityDataSyncStore(directoryURL: directory.url).loadState().deviceIDif point == .query {- let store = ActivitySyncStore(directoryURL: directory.url)+ let store = ActivityDataSyncStore(directoryURL: directory.url)var state = store.loadState()state.lastPrunedDate = niltry store.saveState(state)@@ -44,38 +47,41 @@ struct ZoneRecoveryTests {// 保留 salt 可以验证恢复不依赖设备 ID 碰巧变化await database.failOnce(at: point, wrapped: wrapped, preservingSalt: true)let snapshot = try await service.synchronizeIfEnabled(- localAggregates: [aggregate(0, events: 3, directory: directory)],- localTokenTurns: [turn(0, tokens: 3)],+ localActivityDataAggregates: [aggregate(0, events: 3, directory: directory)],+ localTokenUsageAggregates: [turn(0, tokens: 3)],trigger: .manual)#expect(snapshot.currentDeviceID == previousID)#expect(await database.creations == 1)- #expect(await database.recordCount("Activity") == 1)- #expect(await database.recordCount("Tokens") == 1)- #expect(snapshot.records.count == 1)- #expect(try cachedTokenCount(directory) == 1)- #expect(ActivitySyncStore(directoryURL: directory.url).loadState().hashByDate.count == 1)+ #expect(await database.recordCount("ActivityData") == 1)+ #expect(await database.recordCount("TokenUsage") == 1)+ #expect(snapshot.activityData.count == 1)+ #expect(try cachedTokenUsageCount(directory) == 1)+ #expect(try ActivityDataSyncStore(directoryURL: directory.url).load().records.count == 1)}- @Test func peerRecreatingSameZoneInvalidatesCachedIdentityAndUploadHashes() async throws {+ @Test func peerRecreatingSameZoneInvalidatesCachedIdentityAndRecords() async throws {let directory = try TestDirectory()defer { try? directory.remove() }let database = ZoneDatabase()let service = service(database, directory)let first = try await service.synchronizeIfEnabled(- localAggregates: [aggregate(0, directory: directory), aggregate(1, directory: directory)],- localTokenTurns: [turn(0), turn(1)],+ localActivityDataAggregates: [aggregate(0, directory: directory), aggregate(1, directory: directory)],+ localTokenUsageAggregates: [turn(0), turn(1)],trigger: .manual)await database.recreateByPeer()- let second = try await service.synchronizeIfEnabled(localAggregates: [aggregate(0, directory: directory)], localTokenTurns: [turn(0)], trigger: .manual)+ let second = try await service.synchronizeIfEnabled(+ localActivityDataAggregates: [aggregate(0, directory: directory)],+ localTokenUsageAggregates: [turn(0)], trigger: .manual+ )#expect(first.currentDeviceID != second.currentDeviceID)#expect(second.currentDeviceID != nil)#expect(await database.creations == 0)- #expect(await database.recordCount("Activity") == 1)- #expect(await database.recordCount("Tokens") == 1)- #expect(second.records.count == 1)- #expect(try cachedTokenCount(directory) == 1)+ #expect(await database.recordCount("ActivityData") == 1)+ #expect(await database.recordCount("TokenUsage") == 1)+ #expect(second.activityData.count == 1)+ #expect(try cachedTokenUsageCount(directory) == 1)}@Test(arguments: [nil, "iCloud.app.zabrian.codexbar"] as [String?])@@ -85,11 +91,11 @@ struct ZoneRecoveryTests {let database = ZoneDatabase()let first = service(database, directory)_ = try await first.synchronizeIfEnabled(- localAggregates: [aggregate(0, directory: directory), aggregate(1, directory: directory)],- localTokenTurns: [turn(0), turn(1)],+ localActivityDataAggregates: [aggregate(0, directory: directory), aggregate(1, directory: directory)],+ localTokenUsageAggregates: [turn(0), turn(1)],trigger: .manual)- let store = ActivitySyncStore(directoryURL: directory.url)+ let store = ActivityDataSyncStore(directoryURL: directory.url)var state = store.loadState()state.containerIdentifier = containerIdentifiertry store.saveState(state)@@ -99,15 +105,15 @@ struct ZoneRecoveryTests {let next = service(nextDatabase, directory)let snapshot = await next.snapshotFromCacheIfEnabled()#expect(snapshot.currentDeviceID == nil)- #expect(snapshot.records.isEmpty)- #expect(await next.tokenRecoveryBaselineIfEnabled() == nil)- #expect(store.loadState().hashByDate.isEmpty)- let synced = try await next.synchronizeIfEnabled(localAggregates: [aggregate(0, directory: directory)], localTokenTurns: [turn(0)], trigger: .manual)+ #expect(snapshot.activityData.isEmpty)+ #expect(snapshot.tokenUsageByDate?.isEmpty == true)+ #expect(try store.load().records.isEmpty)+ let synced = try await next.synchronizeIfEnabled(localActivityDataAggregates: [aggregate(0, directory: directory)], localTokenUsageAggregates: [turn(0)], trigger: .manual)#expect(synced.currentDeviceID != nil)- #expect(synced.records.count == 1)- #expect(try cachedTokenCount(directory) == 1)- #expect(await nextDatabase.recordCount("Activity") == 1)- #expect(await nextDatabase.recordCount("Tokens") == 1)+ #expect(synced.activityData.count == 1)+ #expect(try cachedTokenUsageCount(directory) == 1)+ #expect(await nextDatabase.recordCount("ActivityData") == 1)+ #expect(await nextDatabase.recordCount("TokenUsage") == 1)#expect(store.loadState().containerIdentifier == SyncCloudKit.containerIdentifier)}@@ -117,17 +123,23 @@ struct ZoneRecoveryTests {let database = ZoneDatabase()await database.failOnce(at: .query, repeats: true)let service = service(database, directory)- let failed = try await service.synchronizeIfEnabled(localAggregates: [aggregate(0, directory: directory)], localTokenTurns: [turn(0)], trigger: .manual)+ let failed = try await service.synchronizeIfEnabled(+ localActivityDataAggregates: [aggregate(0, directory: directory)],+ localTokenUsageAggregates: [turn(0)], trigger: .manual+ )#expect(await database.queryCount == 2)#expect(await database.creations == 1)#expect(failed.currentDeviceID == nil)- #expect(ActivitySyncStore(directoryURL: directory.url).loadState().hashByDate.isEmpty)+ #expect(try ActivityDataSyncStore(directoryURL: directory.url).load().records.isEmpty)await database.repair()- let recovered = try await service.synchronizeIfEnabled(localAggregates: [aggregate(0, directory: directory)], localTokenTurns: [turn(0)], trigger: .manual)+ let recovered = try await service.synchronizeIfEnabled(+ localActivityDataAggregates: [aggregate(0, directory: directory)],+ localTokenUsageAggregates: [turn(0)], trigger: .manual+ )#expect(recovered.currentDeviceID != nil)#expect(await database.creations == 2)- #expect(await database.recordCount("Activity") == 1)- #expect(await database.recordCount("Tokens") == 1)+ #expect(await database.recordCount("ActivityData") == 1)+ #expect(await database.recordCount("TokenUsage") == 1)}@Test(arguments: [CKError.Code.networkFailure, .notAuthenticated, .permissionFailure, .quotaExceeded])@@ -136,14 +148,14 @@ struct ZoneRecoveryTests {defer { try? directory.remove() }let database = ZoneDatabase()let service = service(database, directory)- _ = try await service.synchronizeIfEnabled(localAggregates: [aggregate(0, directory: directory)], localTokenTurns: [turn(0)], trigger: .manual)- let before = ActivitySyncStore(directoryURL: directory.url).loadState()- let tokens = try Data(contentsOf: directory.url.appendingPathComponent("Tokens/cache.json"))+ _ = try await service.synchronizeIfEnabled(localActivityDataAggregates: [aggregate(0, directory: directory)], localTokenUsageAggregates: [turn(0)], trigger: .manual)+ let before = ActivityDataSyncStore(directoryURL: directory.url).loadState()+ let tokens = try Data(contentsOf: directory.url.appendingPathComponent("tokenUsage/cache.json"))await database.rejectChanges(code)- _ = try await service.synchronizeIfEnabled(localAggregates: [aggregate(0, directory: directory)], localTokenTurns: [turn(0)], trigger: .manual)+ _ = try await service.synchronizeIfEnabled(localActivityDataAggregates: [aggregate(0, directory: directory)], localTokenUsageAggregates: [turn(0)], trigger: .manual)#expect(await database.creations == 0)- #expect(ActivitySyncStore(directoryURL: directory.url).loadState() == before)- #expect(try Data(contentsOf: directory.url.appendingPathComponent("Tokens/cache.json")) == tokens)+ #expect(ActivityDataSyncStore(directoryURL: directory.url).loadState() == before)+ #expect(try Data(contentsOf: directory.url.appendingPathComponent("tokenUsage/cache.json")) == tokens)}@Test func switchingSyncOffDuringFailurePreventsZoneCreation() async throws {@@ -155,10 +167,9 @@ struct ZoneRecoveryTests {let service = SyncService(database: database,directoryURL: directory.url,- activityEventsDirectoryURL: directory.url.appendingPathComponent("Events"),isEnabled: { enabled.withLock { $0 } })- _ = try await service.synchronizeIfEnabled(localAggregates: [aggregate(0, directory: directory)], localTokenTurns: [turn(0)], trigger: .manual)+ _ = try await service.synchronizeIfEnabled(localActivityDataAggregates: [aggregate(0, directory: directory)], localTokenUsageAggregates: [turn(0)], trigger: .manual)#expect(await database.creations == 0)#expect(await database.recordCount("Metadata") == 0)}@@ -168,23 +179,26 @@ struct ZoneRecoveryTests {defer { try? directory.remove() }let database = ZoneDatabase()await database.removeZone()- let store = ActivitySyncStore(directoryURL: directory.url)- try store.saveState(SyncState(deviceID: "stale", hashByDate: [aggregate(0, directory: directory).date: "stale"]))- let blocked = directory.url.appendingPathComponent("Tokens/store.lock")+ let store = ActivityDataSyncStore(directoryURL: directory.url)+ try store.saveState(ActivityDataSyncState(deviceID: "stale"))+ let stale = try aggregate(0, directory: directory).syncedAggregate+ let record = try ActivityDataSyncRecord(deviceID: "stale", activityData: stale, recordName: ActivityDataSyncRecord.recordName(deviceID: "stale", date: stale.date))+ try store.saveFetchedRecords([record.id: record], cursor: nil)+ let blocked = directory.url.appendingPathComponent("tokenUsage/store.lock")try FileManager.default.createDirectory(at: blocked, withIntermediateDirectories: true)- _ = try await service(database, directory).synchronizeIfEnabled(localAggregates: [aggregate(0, directory: directory)], trigger: .manual)+ _ = try await service(database, directory).synchronizeIfEnabled(localActivityDataAggregates: [aggregate(0, directory: directory)], trigger: .manual)#expect(store.loadState().deviceID == nil)- #expect(store.loadState().hashByDate.isEmpty)+ #expect(try store.load().records.isEmpty)#expect(await database.creations == 0)try FileManager.default.removeItem(at: blocked)let snapshot = try await service(database, directory).synchronizeIfEnabled(- localAggregates: [aggregate(0, directory: directory)],- localTokenTurns: [turn(0)],+ localActivityDataAggregates: [aggregate(0, directory: directory)],+ localTokenUsageAggregates: [turn(0)],trigger: .manual)#expect(snapshot.currentDeviceID != nil)#expect(await database.creations == 1)- #expect(await database.recordCount("Activity") == 1)+ #expect(await database.recordCount("ActivityData") == 1)}@Test func zoneClassificationKeepsRecordAndZoneErrorsSeparate() {@@ -202,32 +216,31 @@ struct ZoneRecoveryTests {}private func service(_ database: ZoneDatabase, _ directory: TestDirectory) -> SyncService {- SyncService(database: database, directoryURL: directory.url, activityEventsDirectoryURL: directory.url.appendingPathComponent("Events"), isEnabled: { true })+ SyncService(database: database, directoryURL: directory.url, isEnabled: { true })}- private func aggregate(_ index: Int, events: Int = 1, directory: TestDirectory) throws -> ActivityAggregate {- try directory.activityAggregate(date: HistoryStorage.dateKey(for: Date().addingTimeInterval(Double(-index) * 86400)), events: events)+ private func aggregate(_ index: Int, events: Int = 1, directory: TestDirectory) throws -> ActivityDataAggregate {+ try directory.activityDataAggregate(date: HistoryStorage.dateKey(for: Date().addingTimeInterval(Double(-index) * 86400)), events: events)}- private func turn(_ index: Int, tokens: Int = 1) -> TokenTurn {- let id = TokenTurn.identifier(thread: "test", turn: String(index))+ private func turn(_ index: Int, tokens: Int = 1) -> TokenUsageAggregate {let now = Date().addingTimeInterval(Double(-index) * 86400)- let usage = TokenUsage(+ let tokenUsage = TokenUsage(inputTokens: Int64(tokens), cachedInputTokens: 0, cacheWriteInputTokens: 0,outputTokens: 0, reasoningOutputTokens: 0, totalTokens: Int64(tokens))- return TestFixtures.tokenTurn(id: id, rootID: id, startedAt: now, updatedAt: now, usage: usage)+ return TestFixtures.tokenUsageAggregate(at: now, tokenUsage: tokenUsage)}- private func cachedTokenCount(_ directory: TestDirectory) throws -> Int {- let data = try Data(contentsOf: directory.url.appendingPathComponent("Tokens/cache.json"))+ private func cachedTokenUsageCount(_ directory: TestDirectory) throws -> Int {+ let data = try Data(contentsOf: directory.url.appendingPathComponent("tokenUsage/cache.json"))let json = try #require(JSONSerialization.jsonObject(with: data) as? [String: Any])- return try #require(json["turns"] as? [String: Any]).count+ return try #require(json["records"] as? [[String: Any]]).count}}private actor ZoneDatabase: SyncDatabase {- enum Point: Sendable { case metadata, query, changes, activityUpload, tokenUpload }+ enum Point: Sendable { case metadata, query, changes, activityDataUpload, tokenUsageUpload }private var exists = trueprivate var stored: [CKRecord.ID: CKRecord] = [:]private var missingCode = CKError.Code.userDeletedZone@@ -329,7 +342,7 @@ private actor ZoneDatabase: SyncDatabase {atomically _: Bool) async throws -> Modification {if let record = records.first {- let point: Point? = record.recordType == "Activity" ? .activityUpload : record.recordType == "Tokens" ? .tokenUpload : nil+ let point: Point? = record.recordType == "ActivityData" ? .activityDataUpload : record.recordType == "TokenUsage" ? .tokenUsageUpload : niltry check(point, zone: record.recordID.zoneID, record: record.recordID)}for record in records {diff --git a/Config/CloudKit.ckdb b/Config/CloudKit.ckdbindex b0e133a..a0f6924 100644--- a/Config/CloudKit.ckdb+++ b/Config/CloudKit.ckdb@@ -1,6 +1,6 @@DEFINE SCHEMA- RECORD TYPE Activity (+ RECORD TYPE ActivityData ("___createTime" TIMESTAMP,"___createdBy" REFERENCE,"___etag" STRING,@@ -8,27 +8,11 @@ DEFINE SCHEMA"___modifiedBy" REFERENCE,"___recordID" REFERENCE QUERYABLE,aggregationVersion INT64,- approvalRequestedCount INT64,- compactionCompletedCount INT64,- compactionStartedCount INT64,+ activityData BYTES,date STRING SORTABLE,deviceID STRING QUERYABLE SORTABLE,- eventCount INT64,- generationID STRING,- modelCounts BYTES,- projectCounts BYTES,- sourceCheckpoint BYTES,- subagentEndedCount INT64,- subagentStartedCount INT64,- threadCount INT64,- toolCompletedCount INT64,- toolStartedCount INT64,- turnAbortedCount INT64,- turnCompletedCount INT64,- turnCount INT64,- turnStartedCount INT64,+ schemaVersion INT64,updatedAt TIMESTAMP,- version INT64,GRANT WRITE TO "_creator",GRANT CREATE TO "_icloud",GRANT READ TO "_world"@@ -42,13 +26,13 @@ DEFINE SCHEMA"___modifiedBy" REFERENCE,"___recordID" REFERENCE QUERYABLE,salt BYTES QUERYABLE SORTABLE,- version INT64 QUERYABLE SORTABLE,+ schemaVersion INT64 QUERYABLE SORTABLE,GRANT WRITE TO "_creator",GRANT CREATE TO "_icloud",GRANT READ TO "_world");- RECORD TYPE Tokens (+ RECORD TYPE TokenUsage ("___createTime" TIMESTAMP,"___createdBy" REFERENCE,"___etag" STRING,@@ -56,29 +40,12 @@ DEFINE SCHEMA"___modifiedBy" REFERENCE,"___recordID" REFERENCE QUERYABLE,aggregationVersion INT64,- ancestorIDs LIST<STRING>,- checkpoint BYTES,- generationID STRING,- hasConflict INT64,- rebuiltAt TIMESTAMP,- rootID STRING,- startedAt TIMESTAMP,+ tokenUsage BYTES,+ date STRING SORTABLE,+ deviceID STRING QUERYABLE SORTABLE,+ schemaVersion INT64,updatedAt TIMESTAMP,- usage BYTES,- version INT64,GRANT WRITE TO "_creator",GRANT CREATE TO "_icloud",GRANT READ TO "_world");-- RECORD TYPE Users (- "___createTime" TIMESTAMP,- "___createdBy" REFERENCE,- "___etag" STRING,- "___modTime" TIMESTAMP,- "___modifiedBy" REFERENCE,- "___recordID" REFERENCE,- roles LIST<INT64>,- GRANT WRITE TO "_creator",- GRANT READ TO "_world"- );diff --git a/Images/icon.png b/Images/icon.pngindex 5eba19a..80bb6af 100644Binary files a/Images/icon.png and b/Images/icon.png differdiff --git a/Protocol.md b/Protocol.mdindex 2cd6fca..314fd5b 100644--- a/Protocol.md+++ b/Protocol.md@@ -15,10 +15,11 @@| `account/rateLimitResetCredit/consume` | 消费额度重置凭证 || `config/read` | 读取 Codex TUI 通知配置 || `config/batchWrite` | 修改 Codex TUI 通知配置 |-| `thread/loaded/list` | 获取已加载线程列表 |-| `thread/read` | 读取线程信息和状态 |-| `thread/resume` | 订阅已加载线程的消息 |-| `thread/turns/list` | 获取线程的轮次列表、状态、根轮次标识和子 Agent 创建信息 |+| `thread/loaded/list` | 获取已加载会话列表 |+| `thread/read` | 读取会话信息和状态 |+| `thread/resume` | 订阅已加载会话的消息 |+| `thread/turns/list` | 获取会话的轮次列表、状态和根轮次标识 |+| `thread/items/list` | 分页读取目标轮次的执行项,补齐历史活动和子 Agent 创建信息 |### 通知@@ -35,13 +36,13 @@| `account/updated` | 接收账户变化通知 || `account/rateLimits/updated` | 接收额度变化通知 |-### 线程、轮次和 item 通知+### 会话、轮次和 item 通知| 接口 | 作用 || --- | --- |-| `thread/started` | 接收线程启动通知 |-| `thread/status/changed` | 接收线程状态变化 |-| `thread/tokenUsage/updated` | 接收线程 Token 用量更新 |+| `thread/started` | 接收会话启动通知 |+| `thread/status/changed` | 接收会话状态变化 |+| `thread/tokenUsage/updated` | 接收会话 Token 用量更新 || `turn/started` | 接收轮次开始通知 || `turn/completed` | 接收轮次结束通知 || `item/started` | 接收执行项开始通知 |@@ -86,7 +87,7 @@| 接口 | 字段 | 作用 || --- | --- | --- |-| `thread/turns/list` | 请求参数 `itemsView` | 使用 `notLoaded` 读取轮次信息,使用 `full` 读取包含子 Agent 创建信息的执行项 |+| `thread/turns/list` | 请求参数 `itemsView` | 使用 `notLoaded` 读取轮次信息,不加载执行项正文 || `thread/turns/list`、`turn/started`、`turn/completed` | `Turn.rootTurnId` | 获取当前轮次所属工作链的根轮次 ID |-| `thread/turns/list`、`item/started`、`item/completed` | `subAgentActivity.model` | 获取 `kind` 为 `started` 时子 Agent 创建所用的实际模型 |-| `thread/turns/list`、`item/started`、`item/completed` | `subAgentActivity.reasoningEffort` | 获取 `kind` 为 `started` 时子 Agent 创建所用的实际推理强度 |+| `thread/items/list`、`item/started`、`item/completed` | `subAgentActivity.model` | 获取 `kind` 为 `started` 时子 Agent 创建所用的实际模型 |+| `thread/items/list`、`item/started`、`item/completed` | `subAgentActivity.reasoningEffort` | 获取 `kind` 为 `started` 时子 Agent 创建所用的实际推理强度 |diff --git a/README.md b/README.mdindex 654c2eb..62f2722 100644--- a/README.md+++ b/README.md@@ -4,7 +4,7 @@# CodexBar-**在 macOS 菜单栏一眼看清 Codex**+**在 macOS 菜单栏看清 Codex**简体中文 | [English](README.en.md)@@ -54,7 +54,7 @@ CodexBar 是 macOS 的菜单栏 App,用于集中展示 Codex 账户、额度- 任务运行时自动防止 Mac 睡眠,结束后恢复正常睡眠- 可在等待审批时继续保持唤醒,或同时保持屏幕常亮-- 支持防睡眠时限、低电量保护和异常会话保护+- 支持防睡眠时限、低电量保护和异常任务保护## 安装