forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileIndexerService.swift
More file actions
685 lines (610 loc) · 22.2 KB
/
Copy pathFileIndexerService.swift
File metadata and controls
685 lines (610 loc) · 22.2 KB
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
import Foundation
@preconcurrency import GRDB
// MARK: - FileIndexerService
actor FileIndexerService {
static let shared = FileIndexerService()
private var _dbQueue: DatabasePool?
/// Shared Rewind pool epoch. `nil` denotes an explicitly injected test pool.
private var _dbGeneration: Int?
private var isScanning = false
private var activeScanOperations = 0
private var scanCompletionWaiters: [CheckedContinuation<Void, Never>] = []
private let scanPolicy: FileIndexScanPolicy
/// Batch insert size
private let batchSize: Int
private init() {
scanPolicy = .standard
batchSize = 500
}
init(databasePool: DatabasePool, scanPolicy: FileIndexScanPolicy = .standard, batchSize: Int = 500) {
_dbQueue = databasePool
self.scanPolicy = scanPolicy
self.batchSize = batchSize
}
// MARK: - Database Access
private func ensureDB() async throws -> DatabasePool {
if let db = _dbQueue {
guard let generation = _dbGeneration else { return db }
if await RewindDatabase.shared.poolGeneration() == generation {
return db
}
}
try await RewindDatabase.shared.initialize()
let (queue, generation) = await RewindDatabase.shared.getDatabaseQueueWithGeneration()
guard let db = queue else {
throw FileIndexerError.databaseNotInitialized
}
_dbQueue = db
_dbGeneration = generation
return db
}
func invalidateCache() async {
if activeScanOperations > 0 {
await withCheckedContinuation { continuation in
scanCompletionWaiters.append(continuation)
}
}
_dbQueue = nil
_dbGeneration = nil
}
private func finishScanning() {
isScanning = false
}
private func finishScanOperation() {
activeScanOperations = max(0, activeScanOperations - 1)
guard activeScanOperations == 0 else { return }
let waiters = scanCompletionWaiters
scanCompletionWaiters.removeAll()
for waiter in waiters {
waiter.resume()
}
}
/// Returns the total number of indexed files in the database
func getIndexedFileCount() async -> Int {
guard let db = try? await ensureDB() else { return 0 }
do {
return try await db.read { database in
try Int.fetchOne(database, sql: "SELECT COUNT(*) FROM indexed_files") ?? 0
}
} catch {
log("FileIndexer: Failed to get indexed file count: \(error)")
return 0
}
}
// MARK: - Onboarding Pipeline
/// Main entry point: scan files → post notification → chat AI does the analysis
func runOnboardingPipeline() async {
guard !UserDefaults.standard.bool(forKey: "hasCompletedFileIndexing") else {
log("FileIndexer: Already completed, skipping")
return
}
guard !isScanning else {
log("FileIndexer: Scan already in progress, skipping")
return
}
isScanning = true
defer { finishScanning() }
log("FileIndexer: Starting onboarding pipeline")
let home = FileManager.default.homeDirectoryForCurrentUser
let foldersToScan = scanPolicy.standardScanRoots(homeURL: home)
// 1. Scan files
let totalFiles = await scanFolders(foldersToScan)
guard totalFiles > 0 else {
log("FileIndexer: No files found, skipping")
await MainActor.run {
UserDefaults.standard.set(true, forKey: "hasCompletedFileIndexing")
}
return
}
log("FileIndexer: Scanned \(totalFiles) files")
// 2. Mark complete and set the pending chat flag.
await MainActor.run {
UserDefaults.standard.set(true, forKey: "hasCompletedFileIndexing")
// NOTE: nothing reads `pendingFileIndexingChat` or `.fileIndexingComplete` today.
if UserDefaults.standard.integer(forKey: "pendingFileIndexingChat") == 0 {
UserDefaults.standard.set(totalFiles, forKey: "pendingFileIndexingChat")
}
}
// 3. Post the completion notification.
await MainActor.run {
NotificationCenter.default.post(
name: .fileIndexingComplete,
object: nil,
userInfo: ["totalFiles": totalFiles]
)
}
log("FileIndexer: Pipeline complete, posted fileIndexingComplete notification")
}
// MARK: - Background Re-scan
/// Incremental background re-scan of the standard folders.
/// Updates metadata for existing files and adds new ones.
///
/// - Parameter fullDiskAccessGranted: Automatic callers probe and pass the real
/// answer; without it the TCC-protected roots (Documents/Desktop/Downloads) are
/// skipped so a background scan never raises a per-folder consent sheet. Pass
/// `true` for explicit user-initiated rescans, which may scan everything.
func backgroundRescan(fullDiskAccessGranted: Bool = true) async {
guard !isScanning else {
log("FileIndexer: Scan already in progress, skipping background rescan")
return
}
isScanning = true
defer { finishScanning() }
log("FileIndexer: Starting background rescan")
let home = FileManager.default.homeDirectoryForCurrentUser
let scanPlan = scanPolicy.automaticScanPlan(
homeURL: home,
fullDiskAccessGranted: fullDiskAccessGranted)
let count = await scanFolders(
scanPlan.roots,
incremental: true,
retentionProtectedPrefixes: scanPlan.retainedPrefixes)
log("FileIndexer: Background rescan complete, \(count) files indexed")
}
// MARK: - File Scanning
/// Scan folders and store file metadata in indexed_files table
/// Returns total number of files indexed
@discardableResult
func scanFolders(
_ folders: [URL],
incremental: Bool = false,
retentionProtectedPrefixes: Set<String> = [],
shouldContinue: @escaping @Sendable () -> Bool = { !Task.isCancelled }
) async -> Int {
activeScanOperations += 1
defer { finishScanOperation() }
guard shouldContinue() else { return 0 }
let db: DatabasePool
do {
db = try await ensureDB()
} catch {
log("FileIndexer: DB init failed: \(error.localizedDescription)")
return 0
}
// For incremental scans, load existing index for O(1) lookup
let existingIndex: [String: Date?] = incremental ? loadExistingIndex(from: db) : [:]
var scannedPaths = Set<String>()
// ~-relative prefixes of subtrees that were NOT scanned, for either of two
// reasons: enumeration failed (permission revoked, transient I/O), or the
// caller deliberately omitted a TCC-protected root it has no access to and
// passed it in as `retentionProtectedPrefixes`. Neither is deletion, so both
// must be excluded from the retention diff — otherwise one unreadable folder,
// or one root left out for want of Full Disk Access, purges its whole index
// subtree.
var failedDirectories = retentionProtectedPrefixes
if incremental {
log("FileIndexer: Loaded \(existingIndex.count) existing paths for incremental scan")
}
let fm = FileManager.default
let home = fm.homeDirectoryForCurrentUser.path
var totalFiles = 0
var batch: [IndexedFileRecord] = []
let resourceKeys: [URLResourceKey] = [
.fileSizeKey, .creationDateKey, .contentModificationDateKey,
.isRegularFileKey, .isDirectoryKey,
]
for folder in folders {
guard shouldContinue() else { return 0 }
guard fm.fileExists(atPath: folder.path) else { continue }
let folderName = folder.lastPathComponent
log("FileIndexer: Scanning ~/\(folderName)")
scanDirectory(
url: folder,
folderName: folderName,
homePath: home,
depth: 0,
resourceKeys: resourceKeys,
fm: fm,
batch: &batch,
totalFiles: &totalFiles,
db: db,
existingIndex: existingIndex,
scannedPaths: &scannedPaths,
failedDirectories: &failedDirectories,
shouldContinue: shouldContinue
)
guard shouldContinue() else { return 0 }
}
// Flush remaining batch
if !batch.isEmpty, shouldContinue() {
insertBatch(batch, into: db)
}
// For incremental scans, remove files that no longer exist on disk
if incremental && !existingIndex.isEmpty, shouldContinue() {
deleteRemovedFiles(
scannedPaths: scannedPaths,
existingPaths: Set(existingIndex.keys),
protectedPrefixes: failedDirectories,
db: db
)
}
return totalFiles
}
private func scanDirectory(
url: URL,
folderName: String,
homePath: String,
depth: Int,
resourceKeys: [URLResourceKey],
fm: FileManager,
batch: inout [IndexedFileRecord],
totalFiles: inout Int,
db: DatabasePool,
existingIndex: [String: Date?],
scannedPaths: inout Set<String>,
failedDirectories: inout Set<String>,
shouldContinue: @escaping @Sendable () -> Bool
) {
guard shouldContinue() else { return }
guard scanPolicy.shouldScanDirectory(atDepth: depth) else { return }
let contents: [URL]
do {
contents = try fm.contentsOfDirectory(
at: url,
includingPropertiesForKeys: resourceKeys,
options: [.skipsHiddenFiles]
)
} catch {
// Enumeration failure is a read error, not deletion. Record this
// directory so its previously-indexed files are NOT purged by the
// retention diff (see deleteRemovedFiles / failedDirectories).
failedDirectories.insert(scanPolicy.relativePath(for: url, homePath: homePath))
log("FileIndexer: Cannot read \(url.lastPathComponent): \(error.localizedDescription)")
return
}
for item in contents {
guard shouldContinue() else { return }
// Check directory
let resourceValues = try? item.resourceValues(forKeys: Set(resourceKeys))
if resourceValues == nil {
// A per-item stat/permission failure is a READ error, not a deletion.
// We cannot tell whether this entry is a directory; defaulting
// `isDirectory` to false would treat a real subdirectory as a file,
// drop it (makeFileRecord returns nil), never descend into it, and never
// add its children to `scannedPaths` — so the retention diff would purge
// every previously-indexed file under it. Protect its subtree like an
// enumeration failure instead of silently dropping it.
failedDirectories.insert(scanPolicy.relativePath(for: item, homePath: homePath))
log("FileIndexer: Cannot stat \(item.lastPathComponent); protecting its subtree from retention purge")
continue
}
let isDirectory = resourceValues?.isDirectory ?? false
if isDirectory {
switch scanPolicy.planDirectoryEntry(item) {
case .skipSubtree:
continue
case .indexPackage:
guard
let record = scanPolicy.makePackageRecord(
for: item,
folderName: folderName,
homePath: homePath,
depth: depth,
createdAt: resourceValues?.creationDate,
modifiedAt: resourceValues?.contentModificationDate
)
else {
continue
}
scannedPaths.insert(record.path)
// Skip unchanged files (incremental scan)
if let existingModified = existingIndex[record.path],
let newModified = resourceValues?.contentModificationDate,
let existing = existingModified,
abs(existing.timeIntervalSince(newModified)) < 1.0
{
continue
}
batch.append(record)
totalFiles += 1
if batch.count >= batchSize {
insertBatch(batch, into: db)
batch.removeAll(keepingCapacity: true)
}
continue
case .descend:
scanDirectory(
url: item,
folderName: folderName,
homePath: homePath,
depth: depth + 1,
resourceKeys: resourceKeys,
fm: fm,
batch: &batch,
totalFiles: &totalFiles,
db: db,
existingIndex: existingIndex,
scannedPaths: &scannedPaths,
failedDirectories: &failedDirectories,
shouldContinue: shouldContinue
)
continue
}
}
// Regular file
guard
let record = scanPolicy.makeFileRecord(
for: item,
folderName: folderName,
homePath: homePath,
depth: depth,
isRegularFile: resourceValues?.isRegularFile == true,
sizeBytes: Int64(resourceValues?.fileSize ?? 0),
createdAt: resourceValues?.creationDate,
modifiedAt: resourceValues?.contentModificationDate
)
else {
continue
}
scannedPaths.insert(record.path)
// Skip unchanged files (incremental scan)
if let existingModified = existingIndex[record.path],
let newModified = resourceValues?.contentModificationDate,
let existing = existingModified,
abs(existing.timeIntervalSince(newModified)) < 1.0
{
continue
}
batch.append(record)
totalFiles += 1
if batch.count >= batchSize {
insertBatch(batch, into: db)
batch.removeAll(keepingCapacity: true)
}
}
}
private func insertBatch(_ records: [IndexedFileRecord], into db: DatabasePool) {
do {
try db.write { database in
for record in records {
// Upsert: insert new files, update metadata for existing ones
try database.execute(
sql: """
INSERT INTO indexed_files (path, filename, fileExtension, fileType, sizeBytes, folder, depth, createdAt, modifiedAt, indexedAt)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(path) DO UPDATE SET
sizeBytes = excluded.sizeBytes,
modifiedAt = excluded.modifiedAt,
indexedAt = excluded.indexedAt
""",
arguments: [
record.path, record.filename, record.fileExtension,
record.fileType, record.sizeBytes, record.folder,
record.depth, record.createdAt, record.modifiedAt, record.indexedAt,
]
)
}
}
} catch {
log("FileIndexer: Batch insert error: \(error.localizedDescription)")
}
}
// MARK: - Incremental Scan Helpers
/// Load all existing indexed file paths and their modifiedAt dates for O(1) lookup
private func loadExistingIndex(from db: DatabasePool) -> [String: Date?] {
do {
return try db.read { database in
var index: [String: Date?] = [:]
let rows = try Row.fetchAll(database, sql: "SELECT path, modifiedAt FROM indexed_files")
for row in rows {
guard let path: String = row["path"] else { continue }
let modifiedAt: Date? = row["modifiedAt"]
index[path] = modifiedAt
}
return index
}
} catch {
log("FileIndexer: Failed to load existing index: \(error.localizedDescription)")
return [:]
}
}
/// Delete files from the index that no longer exist on disk
/// Paths that are genuinely gone from disk (present in the index, not seen this
/// scan, and NOT under a directory whose enumeration failed). Pure + static so
/// the retention diff can be tested without a database or filesystem.
static func pathsToDelete(
scannedPaths: Set<String>,
existingPaths: Set<String>,
protectedPrefixes: Set<String>
) -> Set<String> {
existingPaths.subtracting(scannedPaths).filter { path in
!protectedPrefixes.contains { prefix in
path == prefix || path.hasPrefix(prefix + "/")
}
}
}
private func deleteRemovedFiles(
scannedPaths: Set<String>,
existingPaths: Set<String>,
protectedPrefixes: Set<String>,
db: DatabasePool
) {
let removed = Self.pathsToDelete(
scannedPaths: scannedPaths,
existingPaths: existingPaths,
protectedPrefixes: protectedPrefixes
)
guard !removed.isEmpty else { return }
log("FileIndexer: Removing \(removed.count) deleted files from index")
let removedArray = Array(removed)
var offset = 0
while offset < removedArray.count {
let end = min(offset + 500, removedArray.count)
let chunk = Array(removedArray[offset..<end])
do {
try db.write { database in
let placeholders = chunk.map { _ in "?" }.joined(separator: ", ")
try database.execute(
sql: "DELETE FROM indexed_files WHERE path IN (\(placeholders))",
arguments: StatementArguments(chunk)
)
}
} catch {
log("FileIndexer: Batch delete error: \(error.localizedDescription)")
}
offset = end
}
}
// MARK: - Summary Generation
/// Generate a compact text summary of the indexed files for AI analysis
func generateFileSummary() async -> String {
guard let db = try? await ensureDB() else { return "" }
var sections: [String] = []
do {
// 1. Counts by file type
let typeCounts: [(type: String, count: Int, totalSize: Int64)] = try await db.read { database in
try Row.fetchAll(
database,
sql: """
SELECT fileType, COUNT(*) as cnt, SUM(sizeBytes) as totalSize
FROM indexed_files
GROUP BY fileType
ORDER BY cnt DESC
"""
).compactMap { row in
guard let type: String = row["fileType"],
let count: Int = row["cnt"]
else { return nil }
let totalSize: Int64 = row["totalSize"] ?? 0
return (type, count, totalSize)
}
}
if !typeCounts.isEmpty {
var lines = ["## Files by Type"]
for item in typeCounts {
lines.append("- \(item.type): \(item.count) files (\(formatSize(item.totalSize)))")
}
sections.append(lines.joined(separator: "\n"))
}
// 2. Counts by folder
let folderCounts: [(folder: String, count: Int)] = try await db.read { database in
try Row.fetchAll(
database,
sql: """
SELECT folder, COUNT(*) as cnt
FROM indexed_files
GROUP BY folder
ORDER BY cnt DESC
"""
).compactMap { row in
guard let folder: String = row["folder"],
let count: Int = row["cnt"]
else { return nil }
return (folder, count)
}
}
if !folderCounts.isEmpty {
var lines = ["## Files by Folder"]
for item in folderCounts {
lines.append("- ~/\(item.folder): \(item.count) files")
}
sections.append(lines.joined(separator: "\n"))
}
// 3. Top extensions (limit 25)
let topExts: [(ext: String, count: Int)] = try await db.read { database in
try Row.fetchAll(
database,
sql: """
SELECT fileExtension, COUNT(*) as cnt
FROM indexed_files
WHERE fileExtension IS NOT NULL
GROUP BY fileExtension
ORDER BY cnt DESC
LIMIT 25
"""
).compactMap { row in
guard let ext: String = row["fileExtension"],
let count: Int = row["cnt"]
else { return nil }
return (ext, count)
}
}
if !topExts.isEmpty {
var lines = ["## Top File Extensions"]
for item in topExts {
lines.append("- .\(item.ext): \(item.count)")
}
sections.append(lines.joined(separator: "\n"))
}
// 4. Project indicators (package.json, Cargo.toml, etc.)
let projectFiles = [
"package.json", "Cargo.toml", "requirements.txt", "Pipfile",
"Gemfile", "go.mod", "build.gradle", "pom.xml",
"Makefile", "CMakeLists.txt", "Package.swift",
"pyproject.toml", "setup.py", "composer.json",
"Podfile", "Dockerfile", ".xcodeproj", ".xcworkspace",
]
let placeholders = projectFiles.map { _ in "?" }.joined(separator: ", ")
let projectHits: [(name: String, path: String)] = try await db.read { database in
try Row.fetchAll(
database,
sql: """
SELECT filename, path FROM indexed_files
WHERE filename IN (\(placeholders))
ORDER BY filename
LIMIT 50
""", arguments: StatementArguments(projectFiles)
).compactMap { row in
guard let name: String = row["filename"],
let path: String = row["path"]
else { return nil }
return (name, path)
}
}
if !projectHits.isEmpty {
var lines = ["## Project Indicators"]
for item in projectHits {
lines.append("- \(item.name) at \(item.path)")
}
sections.append(lines.joined(separator: "\n"))
}
// 5. Recently modified files (last 30 days, limit 50)
let thirtyDaysAgo = Calendar.current.date(byAdding: .day, value: -30, to: Date()) ?? Date()
let recentFiles: [(name: String, path: String, modified: Date)] = try await db.read { database in
try Row.fetchAll(
database,
sql: """
SELECT filename, path, modifiedAt FROM indexed_files
WHERE modifiedAt >= ?
ORDER BY modifiedAt DESC
LIMIT 50
""", arguments: [thirtyDaysAgo]
).compactMap { row in
guard let name: String = row["filename"],
let path: String = row["path"],
let modified: Date = row["modifiedAt"]
else { return nil }
return (name, path, modified)
}
}
if !recentFiles.isEmpty {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
var lines = ["## Recently Modified Files (last 30 days)"]
for item in recentFiles {
lines.append("- \(item.name) (\(formatter.string(from: item.modified))) at \(item.path)")
}
sections.append(lines.joined(separator: "\n"))
}
} catch {
log("FileIndexer: Summary generation error: \(error.localizedDescription)")
}
return sections.joined(separator: "\n\n")
}
// MARK: - Helpers
private func formatSize(_ bytes: Int64) -> String {
let formatter = ByteCountFormatter()
formatter.countStyle = .file
return formatter.string(fromByteCount: bytes)
}
}
// MARK: - Errors
enum FileIndexerError: LocalizedError {
case databaseNotInitialized
var errorDescription: String? {
switch self {
case .databaseNotInitialized:
return "File indexer database is not initialized"
}
}
}