@@ -216,13 +216,8 @@ extension CostUsageStore {
216216 baseline: PersistedFileBaseline ,
217217 calendar: Calendar )
218218 {
219- let snapshots = ( usage. codexTokenSnapshots ?? [ ] ) . enumerated ( ) . map {
220- Self . tokenSnapshot ( path: path, eventIndex: $0. offset, snapshot: $0. element, calendar: calendar)
221- }
222- let rows = ( usage. codexRows ?? [ ] ) . enumerated ( ) . compactMap { index, row -> CostUsageStoreUsageRow ? in
223- guard let payload = try ? JSONEncoder ( ) . encode ( row) else { return nil }
224- return CostUsageStoreUsageRow ( path: path, rowIndex: index, payload: payload)
225- }
219+ let tokenSnapshots = usage. codexTokenSnapshots ?? [ ]
220+ let usageRows = usage. codexRows ?? [ ]
226221 let details = StoredFileDetails (
227222 lastTotals: usage. lastTotals,
228223 projectPath: usage. projectPath,
@@ -269,19 +264,34 @@ extension CostUsageStore {
269264 let appendSafe = baseline. canReuseRows
270265 && baseline. file? . scanState. fileIdentity == file. scanState. fileIdentity
271266 && oldParsedBytes < newParsedBytes
272- if baseline. canReuseRows, oldParsedBytes == newParsedBytes, baseline. snapshotCount == snapshots . count {
267+ if baseline. canReuseRows, oldParsedBytes == newParsedBytes, baseline. snapshotCount == tokenSnapshots . count {
273268 // Stable cursor: the persisted prefix is already authoritative.
274- } else if appendSafe, baseline. snapshotCount <= snapshots. count {
275- _ = self . appendTokenSnapshots ( Array ( snapshots. dropFirst ( baseline. snapshotCount) ) )
269+ } else if appendSafe, baseline. snapshotCount <= tokenSnapshots. count {
270+ _ = self . appendTokenSnapshots ( Self . storeTokenSnapshots (
271+ path: path,
272+ snapshots: tokenSnapshots,
273+ startingAt: baseline. snapshotCount,
274+ calendar: calendar) )
276275 } else {
277- _ = self . replaceTokenSnapshots ( path: path, snapshots: snapshots)
276+ _ = self . replaceTokenSnapshots (
277+ path: path,
278+ snapshots: Self . storeTokenSnapshots (
279+ path: path,
280+ snapshots: tokenSnapshots,
281+ startingAt: 0 ,
282+ calendar: calendar) )
278283 }
279- if baseline. canReuseRows, oldParsedBytes == newParsedBytes, baseline. rowCount == rows . count {
284+ if baseline. canReuseRows, oldParsedBytes == newParsedBytes, baseline. rowCount == usageRows . count {
280285 // Stable cursor: metadata/aggregate updates do not rewrite historical rows.
281- } else if appendSafe, baseline. rowCount <= rows. count {
282- _ = self . appendUsageRows ( Array ( rows. dropFirst ( baseline. rowCount) ) )
286+ } else if appendSafe, baseline. rowCount <= usageRows. count {
287+ _ = self . appendUsageRows ( Self . storeUsageRows (
288+ path: path,
289+ rows: usageRows,
290+ startingAt: baseline. rowCount) )
283291 } else {
284- _ = self . replaceUsageRows ( path: path, rows: rows)
292+ _ = self . replaceUsageRows (
293+ path: path,
294+ rows: Self . storeUsageRows ( path: path, rows: usageRows, startingAt: 0 ) )
285295 }
286296 _ = self . replaceFileDayAggregates ( path: path, aggregates: Self . fileAggregates ( usage) )
287297 _ = self . upsertForkLineage ( CostUsageStoreForkLineage (
@@ -295,7 +305,7 @@ extension CostUsageStore {
295305 self . persistBuffers ( path: path, usage: usage)
296306 _ = self . upsertAccumulator ( CostUsageStoreAccumulator (
297307 path: path,
298- eventCount: snapshots . count,
308+ eventCount: tokenSnapshots . count,
299309 nextUsageRowIndex: CostUsageScanner . nextCodexUsageRowIndex ( usage. codexRows) ,
300310 countedTotals: Self . totals ( usage. lastCountedTotals) ,
301311 rawTotalsBaseline: Self . totals ( usage. lastRawTotalsBaseline) ,
@@ -579,6 +589,7 @@ extension CostUsageStore {
579589 snapshot: CostUsageCodexTokenSnapshot ,
580590 calendar: Calendar ) -> CostUsageStoreTokenSnapshot
581591 {
592+ tokenSnapshotConversionForTesting ? ( path, eventIndex)
582593 let date = CostUsageScanner . dateFromTimestamp ( snapshot. timestamp)
583594 return CostUsageStoreTokenSnapshot (
584595 path: path,
@@ -591,6 +602,28 @@ extension CostUsageStore {
591602 endOffset: snapshot. endOffset)
592603 }
593604
605+ private static func storeTokenSnapshots(
606+ path: String ,
607+ snapshots: [ CostUsageCodexTokenSnapshot ] ,
608+ startingAt startIndex: Int ,
609+ calendar: Calendar ) -> [ CostUsageStoreTokenSnapshot ]
610+ {
611+ snapshots. indices. dropFirst ( startIndex) . map { index in
612+ Self . tokenSnapshot ( path: path, eventIndex: index, snapshot: snapshots [ index] , calendar: calendar)
613+ }
614+ }
615+
616+ private static func storeUsageRows(
617+ path: String ,
618+ rows: [ CostUsageScanner . CodexUsageRow ] ,
619+ startingAt startIndex: Int ) -> [ CostUsageStoreUsageRow ]
620+ {
621+ rows. indices. dropFirst ( startIndex) . compactMap { index in
622+ guard let payload = try ? JSONEncoder ( ) . encode ( rows [ index] ) else { return nil }
623+ return CostUsageStoreUsageRow ( path: path, rowIndex: index, payload: payload)
624+ }
625+ }
626+
594627 private static func tokenSnapshot( from value: CostUsageStoreTokenSnapshot ) -> CostUsageCodexTokenSnapshot {
595628 CostUsageCodexTokenSnapshot (
596629 timestamp: value. timestamp,
0 commit comments