Feature: Filter search_file results by size, mtime and slim fields

Add four optional arguments to search_file that narrow the match list
after the client has returned it: min_size and max_size in bytes,
modified_after in either YYYY-MM-DD or YYYY-MM-DD HH:MM:SS form, and
name_only to keep just path and name per entry. The client still runs
the same name-matched recursive search, so these filters cut the
response size and the reasoning work on our side, not the scan time on
the host -- the summary sentence says so and totalAll keeps the
pre-filter count so a too-narrow condition is obvious. Invalid values
(negative sizes, an inverted range, a malformed timestamp) are rejected
with -32602 before any packet goes out.

The timestamp validator only accepted the ten-character form because
its shape loop never allowed the two colons, which rejected every
nineteen-character value; the loop now whitelists them.

Smoke tests cover the unchanged totalAll under a size filter, the slim
name_only payload and a rejected timestamp, for 33 checks when the
search path is supplied. Design notes are in section 11.4.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-10-25 14:41:52 +01:00
1 parent 929065e7be
commit 7c5bf3e73d
3 files changed
+127 -1

No files matched your search

+98 -1
View File
@@ -1830,7 +1830,7 @@ std::string BuildToolsListResult(const Json::Value& id) {
{
Json::Value tool(Json::objectValue);
tool["name"] = "search_file";
tool["description"] = u8"在指定主机(仅 Windows)的某个目录下按文件名搜索文件(不区分大小写;pattern 不含通配符时按子串匹配,含 * / ? 时按通配符匹配)。客户端始终递归子目录(深度 32、最多 10000 条,跳过隐藏+系统目录与符号链接),结果边搜边收集、按 limit/offset 分页;finished=false 表示到达 timeout_ms 时限、返回的是部分结果(服务端已通知客户端停止搜索)。单次最多收集 5000 条 / 4 MB,超出时 truncated=true。注意:客户端递归用无界 wsprintf 拼 MAX_PATH 缓冲,整盘/深目录搜索会栈溢出(Debug 客户端弹 Run-Time Check Failure #2 并挂住搜索线程,Release 静默破坏栈)——请只搜具体的中浅层目录,不要从盘符根开始扫。";
tool["description"] = u8"在指定主机(仅 Windows)的某个目录下按文件名搜索文件(不区分大小写;pattern 不含通配符时按子串匹配,含 * / ? 时按通配符匹配)。客户端始终递归子目录(深度 32、最多 10000 条,跳过隐藏+系统目录与符号链接),结果边搜边收集、按 limit/offset 分页;finished=false 表示到达 timeout_ms 时限、返回的是部分结果(服务端已通知客户端停止搜索)。单次最多收集 5000 条 / 4 MB,超出时 truncated=true。可选 min_size / max_size(字节)与 modified_after(YYYY-MM-DD 或 YYYY-MM-DD HH:MM:SS)在服务端做结果侧过滤,name_only=true 只返回 path 与 name;这些过滤不减少客户端搜索耗时,total 为命中数、totalAll 为过滤前条数。注意:客户端递归用无界 wsprintf 拼 MAX_PATH 缓冲,整盘/深目录搜索会栈溢出(Debug 客户端弹 Run-Time Check Failure #2 并挂住搜索线程,Release 静默破坏栈)——请只搜具体的中浅层目录,不要从盘符根开始扫。";
tool["inputSchema"] = BuildSearchFileInputSchema();
tool["outputSchema"] = BuildSearchFileOutputSchema();
tools.append(tool);
@@ -5595,6 +5595,27 @@ Json::Value BuildSearchFileInputSchema() {
toProp["description"] = u8"搜索时限(毫秒),默认 20000,范围 1000-120000。到时限会通知客户端停止搜索,返回已收到的部分结果(finished=false)。";
props["timeout_ms"] = toProp;
// ===== C4:结果侧过滤 =====
Json::Value minProp(Json::objectValue);
minProp["type"] = "integer";
minProp["description"] = u8"只保留大小不小于该字节数的条目。目录项也会参与比较(客户端上报的目录 size 恒为 0),只想看文件时用 min_size=1。";
props["min_size"] = minProp;
Json::Value maxProp(Json::objectValue);
maxProp["type"] = "integer";
maxProp["description"] = u8"只保留不大于该字节数的条目。与 min_size 同时给出时区间取交集。";
props["max_size"] = maxProp;
Json::Value maProp(Json::objectValue);
maProp["type"] = "string";
maProp["description"] = u8"只保留修改时间晚于该时刻的条目,格式 YYYY-MM-DD 或 YYYY-MM-DD HH:MM:SS(主机本地时间)。只给日期时按「该日 00:00:00 之后」理解。";
props["modified_after"] = maProp;
Json::Value nameOnlyProp(Json::objectValue);
nameOnlyProp["type"] = "boolean";
nameOnlyProp["description"] = u8"true 时每条只返回 path 与 name,省略 dir/size/modified,用于结果集很大时省流量。默认 false。";
props["name_only"] = nameOnlyProp;
AppendPagingProps(props);
Json::Value schema(Json::objectValue);
@@ -5671,6 +5692,51 @@ std::string BuildSearchFile(const Json::Value& id, const Json::Value& args, CMy2
timeoutMs = v;
}
// ===== C4:结果侧过滤参数(全部在服务端做,客户端仍只做「按名字匹配 + 递归」)=====
// 读可选的字节数参数:0=未给出;1=已给出;-1=非法(负数/非数字)。
auto readSizeArg = [](const Json::Value& a, const char* key, uint64_t& out) -> int {
if (!a.isMember(key)) return 0;
const Json::Value& val = a[key];
if (val.isNumeric() && val.asDouble() < 0) return -1;
if (!ReadUInt64(val, out)) return -1;
return 1;
};
uint64_t minSize = 0, maxSize = 0;
int hasMinSize = readSizeArg(args, "min_size", minSize);
int hasMaxSize = readSizeArg(args, "max_size", maxSize);
if (hasMinSize < 0)
return BuildError(id, -32602, "min_size must be a non-negative integer (bytes)");
if (hasMaxSize < 0)
return BuildError(id, -32602, "max_size must be a non-negative integer (bytes)");
if (hasMinSize == 1 && hasMaxSize == 1 && minSize > maxSize)
return BuildError(id, -32602, "min_size must not be greater than max_size");
// 客户端 modified 是固定宽度的 "YYYY-MM-DD HH:MM:SS",所以边界值可直接按字典序比较。
std::string modifiedAfter;
if (args.isMember("modified_after")) {
if (!args["modified_after"].isString())
return BuildError(id, -32602, "modified_after must be a string");
modifiedAfter = Trim(args["modified_after"].asString());
if (!modifiedAfter.empty()) {
bool shaped = (modifiedAfter.size() == 10 || modifiedAfter.size() == 19);
for (size_t i = 0; shaped && i < modifiedAfter.size(); ++i) {
const char c = modifiedAfter[i];
if (i == 4 || i == 7) shaped = (c == '-');
else if (i == 10) shaped = (c == ' ');
else if (i == 13 || i == 16) shaped = (c == ':'); // 19 位形式才有
else shaped = (c >= '0' && c <= '9');
}
if (!shaped)
return BuildError(id, -32602,
"modified_after must look like YYYY-MM-DD or YYYY-MM-DD HH:MM:SS");
}
}
bool nameOnly = args.isMember("name_only") && args["name_only"].isBool() &&
args["name_only"].asBool();
const bool hasResultFilter = hasMinSize == 1 || hasMaxSize == 1 ||
!modifiedAfter.empty() || nameOnly;
std::string ansiPath, ansiPattern;
if (!Utf8ToCpExact(path, 936, ansiPath))
return BuildError(id, -32602,
@@ -5748,9 +5814,37 @@ std::string BuildSearchFile(const Json::Value& id, const Json::Value& args, CMy2
all = filtered;
}
// C4:结果侧过滤。注意它发生在收集之后——客户端的 5000 条 / 4 MB 收集上限
// 仍在过滤之前生效,命中太多时该 truncated 还是会 truncated。
const int collected = (int)all.size();
if (hasResultFilter) {
Json::Value filtered(Json::arrayValue);
for (const Json::Value& rec : all) {
if (hasMinSize == 1 || hasMaxSize == 1) {
uint64_t sz = rec["size"].asUInt64();
if (hasMinSize == 1 && sz < minSize) continue;
if (hasMaxSize == 1 && sz > maxSize) continue;
}
if (!modifiedAfter.empty()) {
std::string m = JsonStrField(rec, "modified");
if (m.empty() || m <= modifiedAfter) continue; // 字典序即时间序(格式固定)
}
if (nameOnly) {
Json::Value slim(Json::objectValue);
slim["path"] = rec["path"];
slim["name"] = rec["name"];
filtered.append(slim);
} else {
filtered.append(rec);
}
}
all = filtered;
}
PageWindow page = ResolvePageWindow(args);
Json::Value pageItems;
ApplyPageWindow(all, page, pageItems);
if (hasResultFilter) page.totalAll = collected; // total 是命中数,totalAll 是过滤前条数
Json::Value result(Json::objectValue);
Json::Value structuredContent(Json::objectValue);
@@ -5761,6 +5855,9 @@ std::string BuildSearchFile(const Json::Value& id, const Json::Value& args, CMy2
result["structuredContent"] = structuredContent;
std::string text = PageSummary(page, u8"个匹配项");
if (hasResultFilter)
text += std::string(u8"过滤前共 ") + std::to_string(collected) +
std::string(u8" 条(大小/修改时间/精简字段过滤在服务端进行,不减少客户端搜索耗时)。");
if (!finished)
text += u8" 搜索在时限内未完成,以上为已收到的部分结果(可加大 timeout_ms 重试)。";
if (truncated)