@@ -28,6 +28,10 @@ namespace Microsoft.Agents.AI;
2828[ Experimental ( DiagnosticIds . Experiments . AgentsAIExperiments ) ]
2929public 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>
0 commit comments