Files
AX-Copilot-Codex/src/AxCopilot/Services/Agent/FileWatchTool.cs
lacvet 33c1db4dae
Some checks failed
Release Gate / gate (push) Has been cancelled
에이전트 선택적 탐색 구조 개선과 경고 정리 반영
- claude-code 선택적 탐색 흐름을 참고해 Cowork/Code 시스템 프롬프트에서 folder_map 상시 선행 지시를 완화하고 glob/grep 기반 좁은 탐색을 우선하도록 조정함

- FolderMapTool 기본 depth를 2로, include_files 기본값을 false로 낮추고 MultiReadTool 최대 파일 수를 8개로 줄여 초기 과탐색 폭을 보수적으로 조정함

- AgentLoopExplorationPolicy partial을 추가해 탐색 범위 분류, broad-scan corrective hint, exploration_breadth 성능 로그를 연결함

- AgentLoopService에 탐색 범위 가이드 주입과 실행 중 탐색 폭 추적을 추가하고, 좁은 질문에서 반복적인 folder_map/대량 multi_read를 교정하도록 정리함

- DocxToHtmlConverter nullable 경고를 수정해 Release 빌드 경고 0 / 오류 0 기준을 다시 충족함

- README와 docs/DEVELOPMENT.md에 2026-04-09 10:36 (KST) 기준 개발 이력을 반영함
2026-04-09 14:27:59 +09:00

183 lines
7.3 KiB
C#

using System.IO;
using System.Text;
using System.Text.Json;
namespace AxCopilot.Services.Agent;
/// <summary>
/// 지정 경로의 파일 변경을 감지하고 변경 내역을 반환하는 도구.
/// FileSystemInfo의 타임스탬프 기반으로 최근 변경 파일을 조회합니다.
/// </summary>
public class FileWatchTool : IAgentTool
{
public string Name => "file_watch";
public string Description =>
"Detect recent file changes in a folder. Returns a list of created, modified, and deleted files " +
"since a given time. Useful for monitoring data folders, detecting log updates, " +
"or tracking file system changes.";
public ToolParameterSchema Parameters => new()
{
Properties = new()
{
["path"] = new()
{
Type = "string",
Description = "Folder path to watch. Relative to work folder."
},
["pattern"] = new()
{
Type = "string",
Description = "File pattern filter (e.g. '*.csv', '*.log', '*.xlsx'). Default: '*' (all files)"
},
["since"] = new()
{
Type = "string",
Description = "Time threshold: ISO 8601 datetime (e.g. '2026-03-30T09:00:00') " +
"or relative duration ('1h', '6h', '24h', '7d', '30d'). Default: '24h'"
},
["recursive"] = new()
{
Type = "boolean",
Description = "Search subdirectories recursively. Default: true"
},
["include_size"] = new()
{
Type = "boolean",
Description = "Include file sizes in output. Default: true"
},
["top_n"] = new()
{
Type = "integer",
Description = "Limit results to most recent N files. Default: 50"
},
},
Required = ["path"]
};
public Task<ToolResult> ExecuteAsync(JsonElement args, AgentContext context, CancellationToken ct)
{
var path = args.GetProperty("path").SafeGetString() ?? "";
var pattern = args.SafeTryGetProperty("pattern", out var patEl) ? patEl.SafeGetString() ?? "*" : "*";
var sinceStr = args.SafeTryGetProperty("since", out var sinceEl) ? sinceEl.SafeGetString() ?? "24h" : "24h";
var recursive = !args.SafeTryGetProperty("recursive", out var recEl) || recEl.GetBoolean(); // default true
var includeSize = !args.SafeTryGetProperty("include_size", out var sizeEl) || sizeEl.GetBoolean();
var topN = args.SafeTryGetProperty("top_n", out var topEl) && topEl.TryGetInt32(out var n) ? n : 50;
var fullPath = FileReadTool.ResolvePath(path, context.WorkFolder);
if (!context.IsPathAllowed(fullPath))
return Task.FromResult(ToolResult.Fail($"경로 접근 차단: {fullPath}"));
if (!Directory.Exists(fullPath))
return Task.FromResult(ToolResult.Fail($"폴더 없음: {fullPath}"));
try
{
var since = ParseSince(sinceStr);
var searchOpt = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
var files = Directory.GetFiles(fullPath, pattern, searchOpt)
.Select(f => new FileInfo(f))
.Where(fi => fi.LastWriteTime >= since || fi.CreationTime >= since)
.OrderByDescending(fi => fi.LastWriteTime)
.Take(topN)
.ToList();
if (files.Count == 0)
return Task.FromResult(ToolResult.Ok(
$"📂 {sinceStr} 이내 변경된 파일이 없습니다. (경로: {path}, 패턴: {pattern})"));
var sb = new StringBuilder();
sb.AppendLine($"📂 파일 변경 감지: {files.Count}개 파일 ({sinceStr} 이내)");
sb.AppendLine($" 경로: {path} | 패턴: {pattern}");
sb.AppendLine();
// 생성/수정 분류
var created = files.Where(f => f.CreationTime >= since && f.CreationTime == f.LastWriteTime).ToList();
var modified = files.Where(f => f.LastWriteTime >= since && f.CreationTime < since).ToList();
var recentlyCreated = files.Where(f => f.CreationTime >= since && f.CreationTime != f.LastWriteTime).ToList();
if (created.Count > 0)
{
sb.AppendLine($"🆕 신규 생성 ({created.Count}개):");
foreach (var f in created)
AppendFileInfo(sb, f, fullPath, includeSize);
sb.AppendLine();
}
if (modified.Count > 0)
{
sb.AppendLine($"✏️ 수정됨 ({modified.Count}개):");
foreach (var f in modified)
AppendFileInfo(sb, f, fullPath, includeSize);
sb.AppendLine();
}
if (recentlyCreated.Count > 0)
{
sb.AppendLine($"📝 생성 후 수정됨 ({recentlyCreated.Count}개):");
foreach (var f in recentlyCreated)
AppendFileInfo(sb, f, fullPath, includeSize);
sb.AppendLine();
}
// 요약 통계
var totalSize = files.Sum(f => f.Length);
sb.AppendLine($"── 요약: 총 {files.Count}개 파일, {FormatSize(totalSize)}");
// 파일 유형별 분포
var byExt = files.GroupBy(f => f.Extension.ToLowerInvariant())
.OrderByDescending(g => g.Count())
.Take(10);
sb.Append(" 유형: ");
sb.AppendLine(string.Join(", ", byExt.Select(g => $"{g.Key}({g.Count()})")));
return Task.FromResult(ToolResult.Ok(sb.ToString()));
}
catch (Exception ex)
{
return Task.FromResult(ToolResult.Fail($"파일 감시 실패: {ex.Message}"));
}
}
private static DateTime ParseSince(string since)
{
if (DateTime.TryParse(since, out var dt))
return dt;
// 상대 시간: "1h", "24h", "7d", "30d"
var match = System.Text.RegularExpressions.Regex.Match(since, @"^(\d+)(h|d|m)$");
if (match.Success)
{
var amount = int.Parse(match.Groups[1].Value);
var unit = match.Groups[2].Value;
return unit switch
{
"h" => DateTime.Now.AddHours(-amount),
"d" => DateTime.Now.AddDays(-amount),
"m" => DateTime.Now.AddMinutes(-amount),
_ => DateTime.Now.AddHours(-24)
};
}
return DateTime.Now.AddHours(-24); // default: 24시간
}
private static void AppendFileInfo(StringBuilder sb, FileInfo f, string basePath, bool includeSize)
{
var relPath = Path.GetRelativePath(basePath, f.FullName);
var timeStr = f.LastWriteTime.ToString("MM-dd HH:mm");
if (includeSize)
sb.AppendLine($" {relPath} ({FormatSize(f.Length)}, {timeStr})");
else
sb.AppendLine($" {relPath} ({timeStr})");
}
private static string FormatSize(long bytes)
{
if (bytes < 1024) return $"{bytes}B";
if (bytes < 1024 * 1024) return $"{bytes / 1024.0:F1}KB";
if (bytes < 1024 * 1024 * 1024) return $"{bytes / (1024.0 * 1024):F1}MB";
return $"{bytes / (1024.0 * 1024 * 1024):F2}GB";
}
}