| 1 | using CascadeIDE.Models; |
| 2 | |
| 3 | namespace CascadeIDE.Features.Agent.Environment; |
| 4 | |
| 5 | /// <summary>Оркестрация verify: policy, sandbox profile, worktree (ADR 0148 W6/E).</summary> |
| 6 | public sealed class AgentOrchestrator : IAgentOrchestrator |
| 7 | { |
| 8 | private readonly IAgentEnvironmentService _environment; |
| 9 | private readonly AgentEnvironmentSettings _settings; |
| 10 | private readonly Func<string?> _getSolutionPath; |
| 11 | private readonly Func<AgentVerifyPolicy> _getDefaultPolicy; |
| 12 | |
| 13 | public AgentOrchestrator( |
| 14 | IAgentEnvironmentService environment, |
| 15 | AgentEnvironmentSettings settings, |
| 16 | Func<string?> getSolutionPath, |
| 17 | Func<AgentVerifyPolicy>? getDefaultPolicy = null) |
| 18 | { |
| 19 | _environment = environment; |
| 20 | _settings = settings; |
| 21 | _getSolutionPath = getSolutionPath; |
| 22 | _getDefaultPolicy = getDefaultPolicy ?? (() => AgentVerifyPolicy.Standard); |
| 23 | } |
| 24 | |
| 25 | public AgentVerifyStartResult TryVerifyAfterStep(bool writesOccurred, bool longRun = false) |
| 26 | { |
| 27 | if (!writesOccurred) |
| 28 | return new(false, null, null, "No writes; verify skipped."); |
| 29 | |
| 30 | var solution = _getSolutionPath(); |
| 31 | if (string.IsNullOrWhiteSpace(solution)) |
| 32 | return new(false, null, null, "Solution not open."); |
| 33 | |
| 34 | if (longRun) |
| 35 | return StartLongRunVerify(solution); |
| 36 | |
| 37 | return _environment.StartVerify(solution, _getDefaultPolicy()); |
| 38 | } |
| 39 | |
| 40 | private AgentVerifyStartResult StartLongRunVerify(string solution) |
| 41 | { |
| 42 | var profile = ResolveLongRunSandboxProfile(); |
| 43 | var useWorktree = profile == AgentSandboxProfile.AgentWorktree; |
| 44 | |
| 45 | return _environment.StartVerifyBatch(new AgentVerifyBatchRequest( |
| 46 | _getDefaultPolicy(), |
| 47 | profile, |
| 48 | solution, |
| 49 | UseWorktree: useWorktree)); |
| 50 | } |
| 51 | |
| 52 | private AgentSandboxProfile ResolveLongRunSandboxProfile() |
| 53 | { |
| 54 | if (AgentSandboxProfileParser.TryParse(_settings.LongRunSandboxProfile, out var profile)) |
| 55 | return profile; |
| 56 | |
| 57 | return AgentSandboxProfile.AgentWorktree; |
| 58 | } |
| 59 | } |
| 60 | |