-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileWatcher.cs
More file actions
194 lines (163 loc) · 5.33 KB
/
FileWatcher.cs
File metadata and controls
194 lines (163 loc) · 5.33 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
namespace RalphController;
/// <summary>
/// Watches project files for changes (hot-reload support)
/// </summary>
public class FileWatcher : IDisposable
{
private readonly RalphConfig _config;
private readonly List<FileSystemWatcher> _watchers = new();
private readonly Dictionary<string, DateTime> _lastChangeTime = new();
private readonly TimeSpan _debounceDelay = TimeSpan.FromMilliseconds(500);
private bool _disposed;
/// <summary>Fired when prompt.md changes</summary>
public event Action? OnPromptChanged;
/// <summary>Fired when implementation_plan.md changes</summary>
public event Action? OnPlanChanged;
/// <summary>Fired when agents.md changes</summary>
public event Action? OnAgentsChanged;
/// <summary>Fired when any file in specs/ changes</summary>
public event Action<string>? OnSpecChanged;
/// <summary>Whether prompt.md has changed since last acknowledged</summary>
public bool PromptChanged { get; private set; }
/// <summary>Whether implementation_plan.md has changed since last acknowledged</summary>
public bool PlanChanged { get; private set; }
public FileWatcher(RalphConfig config)
{
_config = config;
}
/// <summary>
/// Start watching files
/// </summary>
public void Start()
{
// Watch prompt.md
if (File.Exists(_config.PromptFilePath))
{
WatchFile(_config.PromptFilePath, () =>
{
PromptChanged = true;
OnPromptChanged?.Invoke();
});
}
// Watch implementation_plan.md
if (File.Exists(_config.PlanFilePath))
{
WatchFile(_config.PlanFilePath, () =>
{
PlanChanged = true;
OnPlanChanged?.Invoke();
});
}
// Watch agents.md
if (File.Exists(_config.AgentsFilePath))
{
WatchFile(_config.AgentsFilePath, () =>
{
OnAgentsChanged?.Invoke();
});
}
// Watch specs directory
if (Directory.Exists(_config.SpecsDirectoryPath))
{
WatchDirectory(_config.SpecsDirectoryPath, "*.md", path =>
{
OnSpecChanged?.Invoke(path);
});
}
}
/// <summary>
/// Stop watching files
/// </summary>
public void Stop()
{
foreach (var watcher in _watchers)
{
watcher.EnableRaisingEvents = false;
}
}
/// <summary>
/// Acknowledge prompt change (reset flag)
/// </summary>
public void AcknowledgePromptChange()
{
PromptChanged = false;
}
/// <summary>
/// Acknowledge plan change (reset flag)
/// </summary>
public void AcknowledgePlanChange()
{
PlanChanged = false;
}
/// <summary>
/// Read current content of implementation_plan.md
/// </summary>
public async Task<string?> ReadPlanAsync()
{
if (!File.Exists(_config.PlanFilePath))
return null;
return await File.ReadAllTextAsync(_config.PlanFilePath);
}
/// <summary>
/// Read last N lines of implementation_plan.md
/// </summary>
public async Task<string[]> ReadPlanLinesAsync(int lastNLines = 10)
{
if (!File.Exists(_config.PlanFilePath))
return Array.Empty<string>();
var lines = await File.ReadAllLinesAsync(_config.PlanFilePath);
return lines.TakeLast(lastNLines).ToArray();
}
private void WatchFile(string filePath, Action onChange)
{
var directory = Path.GetDirectoryName(filePath)!;
var fileName = Path.GetFileName(filePath);
var watcher = new FileSystemWatcher(directory, fileName)
{
NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Size
};
watcher.Changed += (_, e) => HandleChange(e.FullPath, onChange);
watcher.EnableRaisingEvents = true;
_watchers.Add(watcher);
}
private void WatchDirectory(string directoryPath, string filter, Action<string> onChange)
{
var watcher = new FileSystemWatcher(directoryPath, filter)
{
NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.Size,
IncludeSubdirectories = true
};
watcher.Changed += (_, e) => HandleChange(e.FullPath, () => onChange(e.FullPath));
watcher.Created += (_, e) => HandleChange(e.FullPath, () => onChange(e.FullPath));
watcher.EnableRaisingEvents = true;
_watchers.Add(watcher);
}
private void HandleChange(string path, Action onChange)
{
// Debounce - ignore rapid consecutive changes
lock (_lastChangeTime)
{
if (_lastChangeTime.TryGetValue(path, out var lastTime))
{
if (DateTime.UtcNow - lastTime < _debounceDelay)
{
return;
}
}
_lastChangeTime[path] = DateTime.UtcNow;
}
// Fire change handler on thread pool to avoid blocking
Task.Run(onChange);
}
public void Dispose()
{
if (_disposed) return;
_disposed = true;
foreach (var watcher in _watchers)
{
watcher.Dispose();
}
_watchers.Clear();
GC.SuppressFinalize(this);
}
}