Skip to content

Commit f7c6928

Browse files
authored
.NET: Add file search timeout (#9536)
* Add file search timeout * Address PR comments
1 parent fa77467 commit f7c6928

12 files changed

Lines changed: 468 additions & 102 deletions

File tree

‎dotnet/src/Microsoft.Agents.AI/Harness/FileStore/AgentFileStore.cs‎

Lines changed: 63 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ namespace Microsoft.Agents.AI;
2828
[Experimental(DiagnosticIds.Experiments.AgentsAIExperiments)]
2929
public abstract class AgentFileStore
3030
{
31+
private static readonly TimeSpan s_regexMatchTimeout = TimeSpan.FromSeconds(5);
32+
33+
internal static TimeSpan SearchTimeout { get; } = TimeSpan.FromSeconds(10);
34+
3135
/// <summary>
3236
/// Writes content to a file, creating or overwriting it.
3337
/// </summary>
@@ -100,7 +104,7 @@ public abstract class AgentFileStore
100104
/// Implementers overriding this method must report <see cref="FileSearchMatch.LineNumber"/> as a
101105
/// 1-based coordinate into <see cref="SplitLines"/> of the same content <see cref="ReadAsync"/>
102106
/// returns, and should report <see cref="FileSearchMatch.Line"/> verbatim, terminator included.
103-
/// <see cref="ScanContent"/> produces both correctly and is the recommended way to build results.
107+
/// <see cref="ScanContent(string, string, Regex, CancellationToken)"/> produces both correctly and is the recommended way to build results.
104108
/// </para>
105109
/// <para>
106110
/// Numbering against anything else — a different split rule, or content this store does not serve
@@ -111,39 +115,49 @@ public abstract class AgentFileStore
111115
/// </remarks>
112116
public virtual async Task<IReadOnlyList<FileSearchResult>> SearchAsync(string directory, string regexPattern, string? globPattern = null, bool recursive = false, CancellationToken cancellationToken = default)
113117
{
114-
// Compile with a match timeout to guard against catastrophic backtracking (ReDoS).
115-
var regex = new Regex(regexPattern, RegexOptions.IgnoreCase, TimeSpan.FromSeconds(5));
116-
IReadOnlyList<string> names = await this.FindMatchingFilesAsync(directory, regexPattern, globPattern, recursive, cancellationToken).ConfigureAwait(false);
117-
Matcher? matcher = globPattern is not null ? StorePaths.CreateGlobMatcher(globPattern) : null;
118-
var results = new List<FileSearchResult>();
118+
var regex = CreateSearchRegex(regexPattern);
119+
using var timeout = new MultiStepOperationTimeout(SearchTimeout, cancellationToken);
119120

120-
foreach (string name in names)
121+
try
121122
{
122-
cancellationToken.ThrowIfCancellationRequested();
123+
IReadOnlyList<string> names = await timeout.RunStepAsync(
124+
token => this.FindMatchingFilesAsync(directory, regexPattern, globPattern, recursive, token)).ConfigureAwait(false);
125+
Matcher? matcher = globPattern is not null ? StorePaths.CreateGlobMatcher(globPattern) : null;
126+
var results = new List<FileSearchResult>();
123127

124-
// Re-apply the caller's scope: FindMatchingFilesAsync is explicitly allowed to
125-
// over-return, and must not be able to widen what the caller asked for.
126-
if (!StorePaths.MatchesGlob(name, matcher) ||
127-
(!recursive && name.IndexOf("/", StringComparison.Ordinal) >= 0))
128+
foreach (string name in names)
128129
{
129-
continue;
130-
}
130+
timeout.ThrowIfCancellationRequested();
131131

132-
string path = string.IsNullOrEmpty(directory) ? name : $"{directory.TrimEnd('/')}/{name}";
133-
string? content = await this.ReadAsync(path, cancellationToken).ConfigureAwait(false);
134-
if (content is null)
135-
{
136-
continue; // Deleted between enumeration and read.
137-
}
132+
// Re-apply the caller's scope: FindMatchingFilesAsync is explicitly allowed to
133+
// over-return, and must not be able to widen what the caller asked for.
134+
if (!StorePaths.MatchesGlob(name, matcher) ||
135+
(!recursive && name.IndexOf("/", StringComparison.Ordinal) >= 0))
136+
{
137+
continue;
138+
}
138139

139-
FileSearchResult? result = ScanContent(name, content, regex);
140-
if (result is not null)
141-
{
142-
results.Add(result);
140+
string path = string.IsNullOrEmpty(directory) ? name : $"{directory.TrimEnd('/')}/{name}";
141+
string? content = await timeout.RunStepAsync(token => this.ReadAsync(path, token)).ConfigureAwait(false);
142+
if (content is null)
143+
{
144+
continue; // Deleted between enumeration and read.
145+
}
146+
147+
FileSearchResult? result = timeout.RunStep(token => ScanContent(name, content, regex, token));
148+
if (result is not null)
149+
{
150+
results.Add(result);
151+
}
143152
}
144-
}
145153

146-
return results;
154+
timeout.ThrowIfCancellationRequested();
155+
return results;
156+
}
157+
catch (TimeoutException) when (timeout.Token.IsCancellationRequested)
158+
{
159+
throw CreateSearchTimeoutException(regexPattern);
160+
}
147161
}
148162

149163
/// <summary>
@@ -248,8 +262,8 @@ protected virtual async Task<IReadOnlyList<string>> FindMatchingFilesAsync(strin
248262
public static IReadOnlyList<string> SplitLines(string content) => FileEditor.SplitLinesKeepEnds(Throw.IfNull(content));
249263

250264
/// <summary>
251-
/// Finds every line of <paramref name="content"/> matching <paramref name="regex"/>, numbered by
252-
/// <see cref="SplitLines"/>.
265+
/// Finds every line of <paramref name="content"/> matching <paramref name="regex"/>, allowing
266+
/// cancellation between lines and after each regex match.
253267
/// </summary>
254268
/// <remarks>
255269
/// This is the numbering primitive <see cref="SearchAsync"/> uses, published so a store that
@@ -260,22 +274,36 @@ protected virtual async Task<IReadOnlyList<string>> FindMatchingFilesAsync(strin
260274
/// <param name="fileName">The name recorded on the result, relative to the searched directory.</param>
261275
/// <param name="content">The file's full text.</param>
262276
/// <param name="regex">A compiled pattern, normally from the same source string passed to <see cref="SearchAsync"/>.</param>
277+
/// <param name="cancellationToken">A token to cancel content scanning.</param>
263278
/// <returns>The match metadata, or <see langword="null"/> when no line matches.</returns>
264-
public static FileSearchResult? ScanContent(string fileName, string content, Regex regex)
279+
/// <exception cref="OperationCanceledException">The cancellation token was cancelled.</exception>
280+
public static FileSearchResult? ScanContent(string fileName, string content, Regex regex, CancellationToken cancellationToken = default)
265281
{
266282
_ = Throw.IfNull(fileName);
267283
_ = Throw.IfNull(content);
268284
_ = Throw.IfNull(regex);
269285

286+
return ScanContent(fileName, content, (line, length) => regex.Match(line, 0, length), cancellationToken);
287+
}
288+
289+
private static FileSearchResult? ScanContent(string fileName, string content, Func<string, int, Match> matchLine, CancellationToken cancellationToken)
290+
{
291+
_ = Throw.IfNull(fileName);
292+
_ = Throw.IfNull(content);
293+
_ = Throw.IfNull(matchLine);
294+
270295
IReadOnlyList<string> lines = SplitLines(content);
271296
var matchingLines = new List<FileSearchMatch>();
272297
string? firstSnippet = null;
273298
int lineStartOffset = 0;
274299

275300
for (int i = 0; i < lines.Count; i++)
276301
{
302+
cancellationToken.ThrowIfCancellationRequested();
303+
277304
// Match over the line's text only, without copying it out of the line.
278-
Match match = regex.Match(lines[i], 0, FileEditor.LineContentLength(lines[i]));
305+
Match match = matchLine(lines[i], FileEditor.LineContentLength(lines[i]));
306+
cancellationToken.ThrowIfCancellationRequested();
279307
if (match.Success)
280308
{
281309
matchingLines.Add(new FileSearchMatch { LineNumber = i + 1, Line = lines[i] });
@@ -299,6 +327,11 @@ protected virtual async Task<IReadOnlyList<string>> FindMatchingFilesAsync(strin
299327
: new FileSearchResult { FileName = fileName, Snippet = firstSnippet!, MatchingLines = matchingLines };
300328
}
301329

330+
internal static Regex CreateSearchRegex(string pattern) => new(pattern, RegexOptions.IgnoreCase, s_regexMatchTimeout);
331+
332+
internal static RegexMatchTimeoutException CreateSearchTimeoutException(string pattern) =>
333+
new(string.Empty, pattern, SearchTimeout);
334+
302335
/// <summary>
303336
/// Ensures a directory exists, creating it if necessary.
304337
/// </summary>

‎dotnet/src/Microsoft.Agents.AI/Harness/FileStore/FileSystemAgentFileStore.cs‎

Lines changed: 56 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -169,61 +169,81 @@ public override async Task<IReadOnlyList<FileSearchResult>> SearchAsync(
169169
bool recursive = false,
170170
CancellationToken cancellationToken = default)
171171
{
172-
string fullDir = this.ResolveSafeDirectoryPath(directory);
172+
Regex regex = CreateSearchRegex(regexPattern);
173+
using var timeout = new MultiStepOperationTimeout(SearchTimeout, cancellationToken);
173174

174-
if (!Directory.Exists(fullDir))
175+
try
175176
{
176-
return [];
177-
}
177+
string fullDir = this.ResolveSafeDirectoryPath(directory);
178+
timeout.ThrowIfCancellationRequested();
178179

179-
// Compile the regex with a timeout to guard against catastrophic backtracking (ReDoS).
180-
var regex = new Regex(regexPattern, RegexOptions.IgnoreCase, TimeSpan.FromSeconds(5));
181-
Matcher? matcher = globPattern is not null ? StorePaths.CreateGlobMatcher(globPattern) : null;
182-
var results = new List<FileSearchResult>();
180+
if (!Directory.Exists(fullDir))
181+
{
182+
timeout.ThrowIfCancellationRequested();
183+
return [];
184+
}
183185

184-
foreach (string filePath in EnumerateFiles(fullDir, recursive))
185-
{
186-
// The file path relative to the search directory, using forward slashes.
187-
string relativeName = GetRelativeStorePath(fullDir, filePath);
186+
Matcher? matcher = globPattern is not null ? StorePaths.CreateGlobMatcher(globPattern) : null;
187+
var results = new List<FileSearchResult>();
188188

189-
// Apply the optional glob filter on the relative path.
190-
if (!StorePaths.MatchesGlob(relativeName, matcher))
189+
foreach (string filePath in EnumerateFiles(fullDir, recursive, timeout.Token))
191190
{
192-
continue;
193-
}
191+
timeout.ThrowIfCancellationRequested();
192+
193+
// The file path relative to the search directory, using forward slashes.
194+
string relativeName = GetRelativeStorePath(fullDir, filePath);
195+
196+
// Apply the optional glob filter on the relative path.
197+
if (!StorePaths.MatchesGlob(relativeName, matcher))
198+
{
199+
continue;
200+
}
194201

195-
// Read file content.
202+
// Read file content.
196203
#if NET8_0_OR_GREATER
197-
string fileContent = await File.ReadAllTextAsync(filePath, Encoding.UTF8, cancellationToken).ConfigureAwait(false);
204+
string fileContent = await timeout.RunStepAsync(
205+
token => File.ReadAllTextAsync(filePath, Encoding.UTF8, token)).ConfigureAwait(false);
198206
#else
199-
string fileContent;
200-
using (var reader = new StreamReader(filePath, Encoding.UTF8))
201-
{
202-
fileContent = await reader.ReadToEndAsync().ConfigureAwait(false);
203-
}
207+
string fileContent = await timeout.RunStepAsync(async _ =>
208+
{
209+
using var reader = new StreamReader(filePath, Encoding.UTF8);
210+
return await reader.ReadToEndAsync().ConfigureAwait(false);
211+
}).ConfigureAwait(false);
204212
#endif
205213

206-
// Number the lines through the base class's published primitive, so this store
207-
// and the line editor cannot drift apart.
208-
FileSearchResult? result = ScanContent(relativeName, fileContent, regex);
209-
if (result is not null)
210-
{
211-
results.Add(result);
214+
// Number the lines through the base class's published primitive, so this store
215+
// and the line editor cannot drift apart.
216+
FileSearchResult? result = timeout.RunStep(token => ScanContent(relativeName, fileContent, regex, token));
217+
if (result is not null)
218+
{
219+
results.Add(result);
220+
}
212221
}
213-
}
214222

215-
return results;
223+
timeout.ThrowIfCancellationRequested();
224+
return results;
225+
}
226+
catch (TimeoutException) when (timeout.Token.IsCancellationRequested)
227+
{
228+
throw CreateSearchTimeoutException(regexPattern);
229+
}
216230
}
217231

218232
/// <summary>
219233
/// Enumerates the files directly under <paramref name="directory"/> (or all descendant files when
220234
/// <paramref name="recursive"/> is <see langword="true"/>), skipping symlinks/reparse points for both
221235
/// files and directories to prevent reading outside the root.
222236
/// </summary>
223-
private static IEnumerable<string> EnumerateFiles(string directory, bool recursive)
237+
private static IEnumerable<string> EnumerateFiles(
238+
string directory,
239+
bool recursive,
240+
CancellationToken cancellationToken)
224241
{
242+
cancellationToken.ThrowIfCancellationRequested();
225243
foreach (string filePath in Directory.EnumerateFiles(directory))
226244
{
245+
cancellationToken.ThrowIfCancellationRequested();
246+
227247
// Skip files that are symlinks/reparse points.
228248
if ((File.GetAttributes(filePath) & FileAttributes.ReparsePoint) != 0)
229249
{
@@ -233,20 +253,23 @@ private static IEnumerable<string> EnumerateFiles(string directory, bool recursi
233253
yield return filePath;
234254
}
235255

256+
cancellationToken.ThrowIfCancellationRequested();
236257
if (!recursive)
237258
{
238259
yield break;
239260
}
240261

241262
foreach (string subDir in Directory.EnumerateDirectories(directory))
242263
{
264+
cancellationToken.ThrowIfCancellationRequested();
265+
243266
// Skip symlinked/reparse-point directories so recursion cannot escape the root.
244267
if ((File.GetAttributes(subDir) & FileAttributes.ReparsePoint) != 0)
245268
{
246269
continue;
247270
}
248271

249-
foreach (string filePath in EnumerateFiles(subDir, recursive: true))
272+
foreach (string filePath in EnumerateFiles(subDir, recursive: true, cancellationToken))
250273
{
251274
yield return filePath;
252275
}

‎dotnet/src/Microsoft.Agents.AI/Harness/FileStore/InMemoryAgentFileStore.cs‎

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -106,51 +106,61 @@ public override Task<bool> FileExistsAsync(string path, CancellationToken cancel
106106
/// <inheritdoc />
107107
public override Task<IReadOnlyList<FileSearchResult>> SearchAsync(string directory, string regexPattern, string? globPattern = null, bool recursive = false, CancellationToken cancellationToken = default)
108108
{
109-
// Normalize the directory prefix for path matching.
110-
string prefix = StorePaths.NormalizeRelativePath(directory, isDirectory: true);
111-
if (prefix.Length > 0 && !prefix.EndsWith("/", StringComparison.Ordinal))
112-
{
113-
prefix += "/";
114-
}
115-
116-
// Compile the regex with a timeout to guard against catastrophic backtracking (ReDoS).
117-
var regex = new Regex(regexPattern, RegexOptions.IgnoreCase, TimeSpan.FromSeconds(5));
118-
Matcher? matcher = globPattern is not null ? StorePaths.CreateGlobMatcher(globPattern) : null;
119-
var results = new List<FileSearchResult>();
109+
Regex regex = CreateSearchRegex(regexPattern);
110+
using var timeout = new MultiStepOperationTimeout(SearchTimeout, cancellationToken);
120111

121-
foreach (var kvp in this._files)
112+
try
122113
{
123-
// Only consider files within the target directory (by path prefix).
124-
if (!kvp.Key.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
114+
// Normalize the directory prefix for path matching.
115+
string prefix = StorePaths.NormalizeRelativePath(directory, isDirectory: true);
116+
if (prefix.Length > 0 && !prefix.EndsWith("/", StringComparison.Ordinal))
125117
{
126-
continue;
118+
prefix += "/";
127119
}
128120

129-
// The file path relative to the search directory.
130-
string relativeName = kvp.Key.Substring(prefix.Length);
121+
Matcher? matcher = globPattern is not null ? StorePaths.CreateGlobMatcher(globPattern) : null;
122+
var results = new List<FileSearchResult>();
131123

132-
// When not recursive, exclude files in subdirectories (direct children only).
133-
if (!recursive && relativeName.IndexOf("/", StringComparison.Ordinal) >= 0)
124+
foreach (var kvp in this._files)
134125
{
135-
continue;
136-
}
126+
timeout.ThrowIfCancellationRequested();
137127

138-
// Apply the optional glob filter on the relative path.
139-
if (!StorePaths.MatchesGlob(relativeName, matcher))
140-
{
141-
continue;
142-
}
128+
// Only consider files within the target directory (by path prefix).
129+
if (!kvp.Key.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
130+
{
131+
continue;
132+
}
143133

144-
// Number the lines through the base class's published primitive, so this store
145-
// and the line editor cannot drift apart.
146-
FileSearchResult? result = ScanContent(relativeName, kvp.Value, regex);
147-
if (result is not null)
148-
{
149-
results.Add(result);
134+
// The file path relative to the search directory.
135+
string relativeName = kvp.Key.Substring(prefix.Length);
136+
137+
// When not recursive, exclude files in subdirectories (direct children only).
138+
if (!recursive && relativeName.IndexOf("/", StringComparison.Ordinal) >= 0)
139+
{
140+
continue;
141+
}
142+
143+
// Apply the optional glob filter on the relative path.
144+
if (!StorePaths.MatchesGlob(relativeName, matcher))
145+
{
146+
continue;
147+
}
148+
149+
// Number the lines through the base class's published primitive, so this store
150+
// and the line editor cannot drift apart.
151+
FileSearchResult? result = timeout.RunStep(token => ScanContent(relativeName, kvp.Value, regex, token));
152+
if (result is not null)
153+
{
154+
results.Add(result);
155+
}
150156
}
151-
}
152157

153-
return Task.FromResult<IReadOnlyList<FileSearchResult>>(results);
158+
return Task.FromResult<IReadOnlyList<FileSearchResult>>(results);
159+
}
160+
catch (TimeoutException) when (timeout.Token.IsCancellationRequested)
161+
{
162+
throw CreateSearchTimeoutException(regexPattern);
163+
}
154164
}
155165

156166
/// <inheritdoc />

0 commit comments

Comments
 (0)