Ariver
2026-05-18 a5cba8fa3153098d95acb20b6060fac704ecc489
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
import Foundation
 
// MARK: - Smart Start Catalog Runtime
 
struct SmartStartSummary: Equatable {
    let matchedAppCount: Int
    let assignedTagCount: Int
    let createdTagCount: Int
    let backupPath: String?
}
 
enum SmartStartRunMode: Equatable {
    case none
    case autoApplied
    case suggestionOnly
}
 
struct SmartStartRunResult {
    let mode: SmartStartRunMode
    let store: TagDatabase.Store
    let draft: SmartCategorizationDraft?
    let summary: SmartStartSummary?
}
 
private struct SmartStartCatalogEntry {
    let rank: Int
    let name: String
    let normalizedName: String
    let bundleIdentifier: String?
    let categoryIDs: [SmartCategoryID]
    let localizedNote: String?
    let notes: [String: String]?
    let sourceEvidence: [String]
}
 
private struct SmartStartRuntimeCatalog: Decodable {
    let version: Int
    let entries: [SmartStartRuntimeCatalogEntry]
}
 
private struct SmartStartRuntimeCatalogEntry: Decodable {
    let rank: Int?
    let name: String
    let normalizedName: String?
    let bundleIdentifier: String?
    let defaultTag: [String]
    let notes: [String: String]?
    let sourceEvidence: [String]?
}
 
enum SmartStartService {
    static let catalogVersion = 2
    static let systemInitialSchemeCreatedAt: Date = {
        var components = DateComponents()
        components.calendar = Calendar(identifier: .gregorian)
        components.timeZone = TimeZone(identifier: "Asia/Hong_Kong")
        components.year = 2026
        components.month = 5
        components.day = 18
        components.hour = 0
        components.minute = 0
        components.second = 0
        return components.date ?? Date(timeIntervalSince1970: 0)
    }()
 
    static var systemInitialSchemeName: String {
        TagDatabase.schemeName(prefixKey: "scheme.systemSmartStart", at: systemInitialSchemeCreatedAt)
    }
 
    static func runIfNeeded(
        apps: [AppInfo],
        store initialStore: TagDatabase.Store
    ) -> SmartStartRunResult {
        guard shouldRun(for: initialStore) else {
            return SmartStartRunResult(mode: .none, store: initialStore, draft: nil, summary: nil)
        }
 
        let draft = makeDraft(apps: apps)
        guard !draft.assignments.isEmpty else {
            var store = initialStore
            store.smartStart.catalogVersion = catalogVersion
            store.smartStart.lastRunAt = Date()
            store.smartStart.lastMode = "no_matches"
            TagDatabase.save(store)
            return SmartStartRunResult(mode: .none, store: store, draft: nil, summary: nil)
        }
 
        if shouldAutoApply(to: initialStore) {
            let applied = applyDraft(draft, to: initialStore, mode: "auto_applied")
            return SmartStartRunResult(
                mode: .autoApplied,
                store: applied.store,
                draft: draft,
                summary: applied.summary
            )
        }
 
        var store = initialStore
        store.smartStart.catalogVersion = catalogVersion
        store.smartStart.lastRunAt = Date()
        store.smartStart.lastSuggestionAt = Date()
        store.smartStart.lastMode = "suggestion_only"
        store.smartStart.lastMatchedAppCount = draft.assignments.count
        store.smartStart.lastAssignedTagCount = draft.assignments.reduce(0) { $0 + $1.categoryIDs.count }
        TagDatabase.save(store)
 
        let summary = SmartStartSummary(
            matchedAppCount: draft.assignments.count,
            assignedTagCount: draft.assignments.reduce(0) { $0 + $1.categoryIDs.count },
            createdTagCount: 0,
            backupPath: nil
        )
 
        return SmartStartRunResult(mode: .suggestionOnly, store: store, draft: draft, summary: summary)
    }
 
    static func applySuggestion(_ draft: SmartCategorizationDraft) -> SmartStartRunResult {
        let applied = applyDraft(draft, to: TagDatabase.load(), mode: "manual_applied")
        return SmartStartRunResult(
            mode: .autoApplied,
            store: applied.store,
            draft: draft,
            summary: applied.summary
        )
    }
 
    static func applySystemInitialScheme(apps: [AppInfo]) -> SmartStartRunResult {
        let draft = makeDraft(apps: apps)
        guard !draft.assignments.isEmpty else {
            return SmartStartRunResult(mode: .none, store: TagDatabase.load(), draft: draft, summary: nil)
        }
 
        let applied = applyDraft(
            draft,
            to: TagDatabase.loadWithEnsuredCategoryScheme(),
            mode: "settings_system_applied",
            replaceExistingScheme: true
        )
        return SmartStartRunResult(
            mode: .autoApplied,
            store: applied.store,
            draft: draft,
            summary: applied.summary
        )
    }
 
    @discardableResult
    static func relocalizeDefaultNotesForCurrentLanguage(apps: [AppInfo]) -> Bool {
        let catalog = loadCatalog()
        guard !catalog.isEmpty else { return false }
 
        var bundleIndex: [String: SmartStartCatalogEntry] = [:]
        for entry in catalog {
            guard let bundleIdentifier = entry.bundleIdentifier?.lowercased(), !bundleIdentifier.isEmpty else {
                continue
            }
            if let existing = bundleIndex[bundleIdentifier], existing.rank <= entry.rank {
                continue
            }
            bundleIndex[bundleIdentifier] = entry
        }
 
        let nameIndex = Dictionary(
            grouping: catalog,
            by: { $0.normalizedName }
        ).compactMapValues { entries in
            entries.sorted { $0.rank < $1.rank }.first
        }
 
        var store = TagDatabase.load()
        var changed = false
 
        for app in apps {
            let path = app.path.path
            guard let currentNote = normalizedNote(store.appNotes[path]) else { continue }
            guard let matched = match(app: app, bundleIndex: bundleIndex, nameIndex: nameIndex),
                  let notes = matched.notes,
                  !notes.isEmpty
            else { continue }
 
            let knownDefaultNotes = Set(notes.values.compactMap(normalizedNote))
            guard knownDefaultNotes.contains(currentNote),
                  let localizedNote = localizedNote(from: notes),
                  localizedNote != currentNote
            else { continue }
 
            store.appNotes[path] = localizedNote
            changed = true
        }
 
        if changed {
            TagDatabase.save(store)
        }
        return changed
    }
 
    static func restoreBackup(at path: String) -> Bool {
        let url = URL(fileURLWithPath: path)
        guard let data = try? Data(contentsOf: url),
              var store = try? JSONDecoder().decode(TagDatabase.Store.self, from: data)
        else { return false }
 
        let now = Date()
        if store.categoryScheme.currentName == nil {
            store.categoryScheme.currentName = TagDatabase.schemeName(prefixKey: "scheme.beforeSmartStart", at: now)
            store.categoryScheme.currentCreatedAt = now
        }
        store.categoryScheme.lastChangedAt = now
        store.smartStart.catalogVersion = catalogVersion
        store.smartStart.lastRunAt = now
        store.smartStart.lastMode = "restored_from_backup"
        store.smartStart.lastBackupPath = path
        TagDatabase.save(store)
        return true
    }
 
    static func makeDraft(apps: [AppInfo]) -> SmartCategorizationDraft {
        let catalog = loadCatalog()
        var bundleIndex: [String: SmartStartCatalogEntry] = [:]
        for entry in catalog {
            guard let bundleIdentifier = entry.bundleIdentifier?.lowercased(), !bundleIdentifier.isEmpty else {
                continue
            }
            if let existing = bundleIndex[bundleIdentifier], existing.rank <= entry.rank {
                continue
            }
            bundleIndex[bundleIdentifier] = entry
        }
        let nameIndex = Dictionary(
            grouping: catalog,
            by: { $0.normalizedName }
        ).compactMapValues { entries in
            entries.sorted { $0.rank < $1.rank }.first
        }
 
        var seenAssignments = Set<String>()
        var assignments: [SmartAppCategorizationAssignment] = []
        var unassigned: [SmartUnassignedApp] = []
 
        for app in apps {
            let matched = match(app: app, bundleIndex: bundleIndex, nameIndex: nameIndex)
            guard let matched else {
                unassigned.append(SmartUnassignedApp(
                    appName: app.name,
                    bundleIdentifier: app.bundleIdentifier,
                    path: app.path.path,
                    reason: "No local Smart Start catalog match"
                ))
                continue
            }
 
            let key = app.bundleIdentifier?.lowercased() ?? app.path.path
            guard seenAssignments.insert(key).inserted else { continue }
 
            assignments.append(SmartAppCategorizationAssignment(
                appName: app.name,
                bundleIdentifier: app.bundleIdentifier,
                path: app.path.path,
                categoryIDs: matched.categoryIDs,
                confidence: confidence(for: matched),
                source: .localCatalog,
                reason: "Matched Smart Start catalog entry: \(matched.name)",
                provenance: matched.sourceEvidence,
                defaultNote: matched.localizedNote
            ))
        }
 
        return SmartCategorizationDraft(
            draftSource: .localSmartStart,
            assignments: assignments.sorted {
                $0.appName.localizedStandardCompare($1.appName) == .orderedAscending
            },
            unassigned: unassigned
        )
    }
 
    private static func shouldRun(for store: TagDatabase.Store) -> Bool {
        store.smartStart.catalogVersion < catalogVersion
            || (store.smartStart.lastRunAt == nil
                && store.smartStart.lastAppliedAt == nil
                && store.smartStart.lastSuggestionAt == nil)
    }
 
    private static func shouldAutoApply(to store: TagDatabase.Store) -> Bool {
        !store.hasUserTagAssignments
    }
 
    private static func applyDraft(
        _ draft: SmartCategorizationDraft,
        to initialStore: TagDatabase.Store,
        mode: String,
        replaceExistingScheme: Bool = false
    ) -> (store: TagDatabase.Store, summary: SmartStartSummary) {
        var normalizedInitialStore = initialStore
        _ = TagDatabase.normalizeCategorySchemeMetadata(&normalizedInitialStore)
        var store = normalizedInitialStore
        let now = Date()
        let backupURL = TagDatabase.backup(normalizedInitialStore, reason: "smart-start-\(mode)")
        let usedCategoryIDs = orderedUsedCategoryIDs(in: draft)
        let systemCategoryIDsToCreate = replaceExistingScheme
            ? SmartCategoryDefaults.orderedIDs
            : usedCategoryIDs
        var createdTagCount = 0
 
        if replaceExistingScheme {
            store.tags = [:]
            store.appTags = [:]
            store.tagOrder = []
            store.disabledSystemCategoryIDs = []
        }
 
        for categoryID in systemCategoryIDsToCreate {
            guard replaceExistingScheme || !store.disabledSystemCategoryIDs.contains(categoryID) else {
                continue
            }
            if let result = TagDatabase.ensureSystemTag(for: categoryID, in: &store) {
                if result.created {
                    createdTagCount += 1
                }
                if !store.tagOrder.contains(result.name) {
                    store.tagOrder.append(result.name)
                }
            }
        }
 
        var assignedTagCount = 0
        var uncommonPaths = Set(store.uncommonAppPaths)
        for assignment in draft.assignments {
            guard let path = assignment.path, !path.isEmpty else { continue }
            var currentTags = store.appTags[path] ?? []
            for categoryID in assignment.categoryIDs {
                guard replaceExistingScheme || !store.disabledSystemCategoryIDs.contains(categoryID) else {
                    continue
                }
                guard let tagName = TagDatabase.systemTagName(for: categoryID, in: store) else {
                    continue
                }
                guard store.tags[tagName] != nil else { continue }
                if !currentTags.contains(tagName) {
                    currentTags.append(tagName)
                    assignedTagCount += 1
                }
            }
            if currentTags.isEmpty {
                store.appTags.removeValue(forKey: path)
            } else {
                store.appTags[path] = currentTags
            }
 
            if let defaultNote = assignment.defaultNote?.trimmingCharacters(in: .whitespacesAndNewlines),
               !defaultNote.isEmpty,
               store.appNotes[path]?.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty != false {
                store.appNotes[path] = String(defaultNote.prefix(TagDatabase.maxAppNoteLength))
                uncommonPaths.insert(path)
                if store.uncommonSources[path] == nil {
                    store.uncommonSources[path] = .auto
                }
            }
        }
 
        store.uncommonAppPaths = uncommonPaths.sorted()
        store.smartStart.catalogVersion = catalogVersion
        store.smartStart.lastRunAt = now
        store.smartStart.lastAppliedAt = now
        store.smartStart.lastBackupPath = backupURL?.path
        store.smartStart.lastMode = mode
        store.smartStart.lastMatchedAppCount = draft.assignments.count
        store.smartStart.lastAssignedTagCount = assignedTagCount
        store.categoryScheme.previousName = normalizedInitialStore.categoryScheme.currentName
            ?? TagDatabase.schemeName(prefixKey: "scheme.beforeSmartStart", at: now)
        store.categoryScheme.previousCreatedAt = normalizedInitialStore.categoryScheme.currentCreatedAt
        store.categoryScheme.previousBackupPath = backupURL?.path
        store.categoryScheme.currentName = replaceExistingScheme
            ? Self.systemInitialSchemeName
            : TagDatabase.schemeName(prefixKey: "scheme.smartStart", at: Self.systemInitialSchemeCreatedAt)
        store.categoryScheme.currentCreatedAt = replaceExistingScheme
            ? Self.systemInitialSchemeCreatedAt
            : Self.systemInitialSchemeCreatedAt
        store.categoryScheme.lastChangedAt = now
        TagDatabase.save(store)
 
        return (
            store,
            SmartStartSummary(
                matchedAppCount: draft.assignments.count,
                assignedTagCount: assignedTagCount,
                createdTagCount: createdTagCount,
                backupPath: backupURL?.path
            )
        )
    }
 
    private static func orderedUsedCategoryIDs(in draft: SmartCategorizationDraft) -> [SmartCategoryID] {
        let used = Set(draft.assignments.flatMap(\.categoryIDs))
        return SmartCategoryDefaults.orderedIDs.filter { used.contains($0) }
    }
 
    private static func match(
        app: AppInfo,
        bundleIndex: [String: SmartStartCatalogEntry],
        nameIndex: [String: SmartStartCatalogEntry]
    ) -> SmartStartCatalogEntry? {
        if let bundleIdentifier = app.bundleIdentifier?.lowercased(),
           let bundleMatch = bundleIndex[bundleIdentifier] {
            return bundleMatch
        }
        return nameIndex[normalizedName(app.name)]
    }
 
    private static func confidence(for entry: SmartStartCatalogEntry) -> Double {
        if entry.rank <= 300 { return 0.86 }
        if entry.categoryIDs.count > 1 { return 0.74 }
        return 0.68
    }
 
    private static func loadCatalog() -> [SmartStartCatalogEntry] {
        if let catalog = loadRuntimeCatalog() {
            return catalog
        }
        return loadLegacyCSVCatalog()
    }
 
    private static func loadRuntimeCatalog() -> [SmartStartCatalogEntry]? {
        guard let url = Bundle.main.url(forResource: "SmartStartUltimateDefaultCatalog", withExtension: "json"),
              let data = try? Data(contentsOf: url),
              let catalog = try? JSONDecoder().decode(SmartStartRuntimeCatalog.self, from: data)
        else { return nil }
 
        return catalog.entries.compactMap { entry in
            let tagIDs = entry.defaultTag
                .compactMap { SmartCategoryID(rawValue: $0) }
                .filter { $0 != .other }
            guard !tagIDs.isEmpty else { return nil }
 
            return SmartStartCatalogEntry(
                rank: entry.rank ?? Int.max,
                name: entry.name,
                normalizedName: entry.normalizedName.flatMap {
                    $0.isEmpty ? nil : $0
                } ?? normalizedName(entry.name),
                bundleIdentifier: normalizedBundleIdentifier(entry.bundleIdentifier),
                categoryIDs: uniqueOrdered(tagIDs),
                localizedNote: localizedNote(from: entry.notes),
                notes: entry.notes,
                sourceEvidence: entry.sourceEvidence ?? []
            )
        }
    }
 
    private static func loadLegacyCSVCatalog() -> [SmartStartCatalogEntry] {
        guard let url = Bundle.main.url(forResource: "SmartStartAppDefaultTags", withExtension: "csv"),
              let csv = try? String(contentsOf: url, encoding: .utf8)
        else { return [] }
 
        let rows = parseCSV(csv)
        guard let header = rows.first else { return [] }
        let dataRows = rows.dropFirst()
 
        return dataRows.compactMap { row in
            let values = Dictionary(uniqueKeysWithValues: header.enumerated().map { index, key in
                (key, index < row.count ? row[index] : "")
            })
            let tagIDs = splitPipe(values["defaultTagIDs"] ?? "")
                .compactMap { SmartCategoryID(rawValue: $0) }
                .filter { $0 != .other }
            guard !tagIDs.isEmpty else { return nil }
 
            return SmartStartCatalogEntry(
                rank: Int(values["rank"] ?? "") ?? Int.max,
                name: values["name"] ?? "",
                normalizedName: values["normalizedName"].flatMap {
                    $0.isEmpty ? nil : $0
                } ?? normalizedName(values["name"] ?? ""),
                bundleIdentifier: normalizedBundleIdentifier(values["bundleIdentifier"]),
                categoryIDs: uniqueOrdered(tagIDs),
                localizedNote: nil,
                notes: nil,
                sourceEvidence: splitPipe(values["sourceEvidence"] ?? "")
            )
        }
    }
 
    private static func localizedNote(from notes: [String: String]?, preferredCode: String = L10n.currentCode) -> String? {
        guard let notes, !notes.isEmpty else { return nil }
        let preferredCodes = [
            preferredCode,
            "en",
            "zh-Hans",
            "zh-Hant"
        ]
 
        for code in preferredCodes {
            if let note = normalizedNote(notes[code]) {
                return note
            }
        }
 
        return notes.values
            .compactMap(normalizedNote)
            .first
    }
 
    private static func normalizedNote(_ value: String?) -> String? {
        guard let value else { return nil }
        let trimmed = value.trimmingCharacters(in: .whitespacesAndNewlines)
        guard !trimmed.isEmpty else { return nil }
        return String(trimmed.prefix(TagDatabase.maxAppNoteLength))
    }
 
    private static func normalizedBundleIdentifier(_ value: String?) -> String? {
        guard let value = value?.trimmingCharacters(in: .whitespacesAndNewlines),
              !value.isEmpty,
              !["null", "nil", "undefined"].contains(value.lowercased())
        else { return nil }
        return value
    }
 
    private static func uniqueOrdered(_ ids: [SmartCategoryID]) -> [SmartCategoryID] {
        var seen = Set<SmartCategoryID>()
        return ids.filter { seen.insert($0).inserted }
    }
 
    private static func splitPipe(_ value: String) -> [String] {
        value
            .split(separator: "|")
            .map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
            .filter { !$0.isEmpty }
    }
 
    private static func normalizedName(_ value: String) -> String {
        value
            .folding(options: [.diacriticInsensitive, .caseInsensitive], locale: .current)
            .lowercased()
            .replacingOccurrences(of: "&", with: " and ")
            .components(separatedBy: CharacterSet.alphanumerics.inverted)
            .filter { !$0.isEmpty }
            .joined(separator: "-")
    }
 
    private static func parseCSV(_ csv: String) -> [[String]] {
        var rows: [[String]] = []
        var row: [String] = []
        var field = ""
        var quoted = false
        var index = csv.startIndex
 
        while index < csv.endIndex {
            let character = csv[index]
 
            if quoted {
                if character == "\"" {
                    let next = csv.index(after: index)
                    if next < csv.endIndex, csv[next] == "\"" {
                        field.append("\"")
                        index = next
                    } else {
                        quoted = false
                    }
                } else {
                    field.append(character)
                }
            } else {
                switch character {
                case "\"":
                    quoted = true
                case ",":
                    row.append(field)
                    field = ""
                case "\n":
                    row.append(field)
                    rows.append(row)
                    row = []
                    field = ""
                case "\r":
                    break
                default:
                    field.append(character)
                }
            }
 
            index = csv.index(after: index)
        }
 
        if !field.isEmpty || !row.isEmpty {
            row.append(field)
            rows.append(row)
        }
 
        return rows
    }
}