Forge
csharpdeeb25a2
1namespace CascadeIDE.Features.Agent.Harness;
2
3/// <summary>Debounce auto-verify after .cs writes (ADR 0166 P0.4).</summary>
4public sealed class AgentVerifyCoalescer : IDisposable
5{
6 private readonly object _gate = new();
7 private readonly Action _fire;
8 private readonly int _windowMs;
9 private CancellationTokenSource? _pending;
10 private bool _disposed;
11
12 public AgentVerifyCoalescer(int coalesceWindowMs, Action fire)
13 {
14 _windowMs = Math.Max(200, coalesceWindowMs);
15 _fire = fire;
16 }
17
18 public void Schedule()
19 {
20 lock (_gate)
21 {
22 if (_disposed)
23 return;
24
25 _pending?.Cancel();
26 _pending?.Dispose();
27 _pending = new CancellationTokenSource();
28 var token = _pending.Token;
29 _ = RunDelayedAsync(token);
30 }
31 }
32
33 private async Task RunDelayedAsync(CancellationToken token)
34 {
35 try
36 {
37 await Task.Delay(_windowMs, token).ConfigureAwait(false);
38 if (!token.IsCancellationRequested)
39 _fire();
40 }
41 catch (OperationCanceledException)
42 {
43 // superseded
44 }
45 }
46
47 public void Dispose()
48 {
49 lock (_gate)
50 {
51 if (_disposed)
52 return;
53 _disposed = true;
54 _pending?.Cancel();
55 _pending?.Dispose();
56 _pending = null;
57 }
58 }
59}
60
View only · write via MCP/CIDE