| 1 | #nullable enable |
| 2 | using CascadeIDE.Contracts; |
| 3 | using CascadeIDE.Models; |
| 4 | |
| 5 | namespace CascadeIDE.Features.HybridIndex.Application; |
| 6 | |
| 7 | /// <summary> |
| 8 | /// Application-level policy for wiring Hybrid Codebase Index orchestration to current settings/runtime. |
| 9 | /// Keeps watcher enablement and debounce decisions out of UI code. |
| 10 | /// </summary> |
| 11 | [ComputingUnit] |
| 12 | public static class HybridIndexOrchestrationPolicy |
| 13 | { |
| 14 | public static int ResolveDebounceMs(HybridIndexSettings settings) |
| 15 | { |
| 16 | var v = settings.DebounceMs; |
| 17 | if (v < 0) v = 0; |
| 18 | if (v > 60_000) v = 60_000; |
| 19 | return v; |
| 20 | } |
| 21 | |
| 22 | public static bool ShouldEnableWatcher(HybridIndexSettings settings, bool chatMcpOnly) => |
| 23 | settings.Enabled |
| 24 | && settings.WatchFiles |
| 25 | && !(chatMcpOnly && settings.PauseWhenMcpStdioHost); |
| 26 | |
| 27 | public static void ApplyForCurrentScope( |
| 28 | HybridIndexOrchestrator orchestrator, |
| 29 | HybridIndexSettings settings, |
| 30 | bool chatMcpOnly, |
| 31 | string workspaceRoot, |
| 32 | string? solutionPath, |
| 33 | bool pokeWhenAutoReindex) |
| 34 | { |
| 35 | var (hciWs, hciSln) = HybridIndexScopeResolver.ApplyScopeMode(settings.ScopeMode, workspaceRoot, solutionPath); |
| 36 | if (string.IsNullOrWhiteSpace(hciWs)) |
| 37 | return; |
| 38 | |
| 39 | var enableWatcher = ShouldEnableWatcher(settings, chatMcpOnly); |
| 40 | orchestrator.SetEnabled(hciWs, hciSln, enabled: enableWatcher, debounceMs: ResolveDebounceMs(settings)); |
| 41 | if (pokeWhenAutoReindex && settings.AutoReindexOnSolutionOpen) |
| 42 | orchestrator.Poke(hciWs, hciSln); |
| 43 | } |
| 44 | |
| 45 | /// <summary> |
| 46 | /// Manual reindex request from UI. Re-applies enablement (watcher may be off) и всегда один полный проход in-proc |
| 47 | /// со снимком в DataBus; фоновый watcher при необходимости остаётся активен для последующих debounce-прогонов. |
| 48 | /// </summary> |
| 49 | public static Task TriggerReindexNowAsync( |
| 50 | HybridIndexOrchestrator orchestrator, |
| 51 | HybridIndexSettings settings, |
| 52 | bool chatMcpOnly, |
| 53 | string workspaceRoot, |
| 54 | string? solutionPath, |
| 55 | CancellationToken cancellationToken) |
| 56 | { |
| 57 | var (hciWs, hciSln) = HybridIndexScopeResolver.ApplyScopeMode(settings.ScopeMode, workspaceRoot, solutionPath); |
| 58 | if (string.IsNullOrWhiteSpace(hciWs)) |
| 59 | return Task.CompletedTask; |
| 60 | |
| 61 | var enableWatcher = ShouldEnableWatcher(settings, chatMcpOnly); |
| 62 | orchestrator.SetEnabled(hciWs, hciSln, enabled: enableWatcher, debounceMs: ResolveDebounceMs(settings)); |
| 63 | |
| 64 | return orchestrator.RunFullReindexAndPublishStatusAsync(hciWs, hciSln, cancellationToken); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | |