| 1 | #nullable enable |
| 2 | |
| 3 | using CascadeIDE.Features.HybridIndex.Application; |
| 4 | |
| 5 | namespace CascadeIDE.Services.Intercom; |
| 6 | |
| 7 | /// <summary> |
| 8 | /// Фоновая синхронизация symbol sidecar с HCI reindex (ADR 0135). |
| 9 | /// Fallback, если reindex прошёл без <see cref="HybridCodebaseIndex.Core.Indexing.ICodebaseIndexReindexObserver"/> |
| 10 | /// (внешний MCP). In-proc CascadeIDE передаёт <see cref="IntercomSymbolLineHciReindexObserver"/>. |
| 11 | /// </summary> |
| 12 | public static class IntercomSymbolLineIndexCoordinator |
| 13 | { |
| 14 | private static int _rebuildGeneration; |
| 15 | |
| 16 | public static void ScheduleRebuildAfterHybridIndex( |
| 17 | string workspaceRoot, |
| 18 | string? solutionPath, |
| 19 | string indexDirectoryRelative) |
| 20 | { |
| 21 | if (string.IsNullOrWhiteSpace(workspaceRoot)) |
| 22 | return; |
| 23 | |
| 24 | var gen = Interlocked.Increment(ref _rebuildGeneration); |
| 25 | var root = workspaceRoot.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); |
| 26 | var sln = string.IsNullOrWhiteSpace(solutionPath) ? null : solutionPath.Trim(); |
| 27 | var indexDir = HybridIndexIndexDirectoryRelative.ResolveOrDefault(indexDirectoryRelative); |
| 28 | var cache = new IntercomAttachResolveCacheContext(root, sln, null, indexDir); |
| 29 | |
| 30 | _ = Task.Run(() => rebuildAsync(cache, gen, CancellationToken.None)); |
| 31 | } |
| 32 | |
| 33 | private static async Task rebuildAsync( |
| 34 | IntercomAttachResolveCacheContext cache, |
| 35 | int generation, |
| 36 | CancellationToken cancellationToken) |
| 37 | { |
| 38 | if (string.IsNullOrWhiteSpace(cache.WorkspaceRoot)) |
| 39 | return; |
| 40 | |
| 41 | var files = IntercomSymbolLineIndexScanner.EnumerateCsFiles( |
| 42 | cache.WorkspaceRoot, |
| 43 | cache.IndexDirectoryRelative).ToList(); |
| 44 | |
| 45 | foreach (var absolute in files) |
| 46 | { |
| 47 | cancellationToken.ThrowIfCancellationRequested(); |
| 48 | if (generation != Volatile.Read(ref _rebuildGeneration)) |
| 49 | return; |
| 50 | |
| 51 | var rel = Path.GetRelativePath(cache.WorkspaceRoot!, absolute).Replace('\\', '/'); |
| 52 | var perFile = cache with { RelativePath = rel }; |
| 53 | try |
| 54 | { |
| 55 | IntercomSymbolLineIndexBuilder.IndexFile(perFile, absolute, rel); |
| 56 | } |
| 57 | catch |
| 58 | { |
| 59 | // best-effort per file |
| 60 | } |
| 61 | |
| 62 | await Task.Yield(); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |