Fix(mcp): list the whole directory before paging list_files

list_files stopped parsing at 500 entries, so a larger directory reported
total=500 with hasMore=false and offsets past 500 returned nothing: the model
was told a false limit and could not reach the tail. Parse the full
TOKEN_FILE_LIST payload (100000 cap only to bound a pathological directory),
page over it, and report truncated=true plus a summary note when the cap hits.

Only an empty path now means list drives. Windows "." "/" "\" are normalized
to empty in BuildListFiles, while Linux/macOS "/" is a real root directory and
is listed; OnDriveList no longer keeps its own copy of the magic-path rule.

The file-manager channel carries GBK both ways (Windows FindFirstFileA;
common/FileManager.h runs gbkToUtf8 on incoming paths, utf8ToGbk on outgoing
names). The old CP_UTF8 choice for LNX/MAC decoded GBK names wrongly and sent
UTF-8 paths to a client that treats them as GBK. Paths now go out as GBK, or
as raw UTF-8 when GBK cannot represent them (the client's iconv then passes
the bytes through). Names decode GBK-first, UTF-8 on failure: UTF-8-first
misreads ~18% of common hanzi whose GBK bytes are also valid UTF-8.

Go's server gets the same contract: 100000 cap, truncated in
structuredContent, and the cap check after a real record so an exact-cap
listing with trailing padding is not called truncated. tools_minimal.json is
regenerated; the Go truncation test and the design doc are updated.

Debug server builds; check_platform_table, gen_go_tools --check and
go test ./... pass. The Release instance was not rebuilt or restarted.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-13 12:13:20 +01:00
1 parent 42dd70fe20
commit 5e012019ba
6 files changed
+137 -40

No files matched your search

+18 -14
View File
@@ -31,10 +31,11 @@ import (
// ctx answered, which is also how late packets from a previous sub-conn are
// told apart from the current one.
const (
// fileListMaxEntries matches the published contract: tools.json advertises
// "最多 500 条" and the Win32 server parses 500 entries, so a larger
// directory is cut here too (the summary tells the model it was cut).
fileListMaxEntries = 500
// fileListMaxEntries is the parse cap shared with the Win32 server: the
// directory is parsed in full and paged afterwards, so total/hasMore stay
// true; only a pathological directory is cut here, and structuredContent
// then carries truncated=true and the summary says so.
fileListMaxEntries = 100000
// fileRecordFixedLen is attr(1) + size(8) + FILETIME(8).
fileRecordFixedLen = 17
@@ -522,15 +523,17 @@ func parseFileList(p []byte) ([]fileEntry, bool) {
out := make([]fileEntry, 0, 64)
off := 0
for off < len(p) {
if len(out) >= fileListMaxEntries {
return out, true
}
attr := p[off]
off++
name, n, ok := cString(p[off:])
if !ok || n == 0 {
break
}
// Cap check after a real record is seen: trailing zero padding (an empty
// name) must not report truncation when the listing is exactly at the cap.
if len(out) >= fileListMaxEntries {
return out, true
}
off += n + 1
if off+fileRecordFixedLen-1 > len(p) {
break
@@ -609,12 +612,13 @@ func listFilesResult(args listFilesArgs, drives []driveEntry, files []fileEntry,
begin := min(offset, total)
end := min(begin+limit, total)
structured = map[string]any{
"files": files[begin:end],
"total": total,
"totalAll": total,
"limit": limit,
"offset": offset,
"hasMore": end < total,
"files": files[begin:end],
"total": total,
"totalAll": total,
"limit": limit,
"offset": offset,
"hasMore": end < total,
"truncated": truncated,
}
text = pageSummary(total, limit, offset, "个条目")
header = []string{"name", "isDir", "size", "mtime"}
@@ -627,7 +631,7 @@ func listFilesResult(args listFilesArgs, drives []driveEntry, files []fileEntry,
}
}
if truncated {
text += fmt.Sprintf("目录条目超过 %d 条,只解析了前 %d 条。", fileListMaxEntries, fileListMaxEntries)
text += fmt.Sprintf("目录条目超过 %d 条,total 只是上限内的数量,请缩小目录范围。", fileListMaxEntries)
}
text = appendRows(text, header, rows)
return newToolResult(text, structured)
+19 -1
View File
@@ -723,7 +723,10 @@ func TestListFilesTruncationNote(t *testing.T) {
}
sc := c.res.StructuredContent.(map[string]any)
checkInt(t, sc, "total", fileListMaxEntries)
if !strings.Contains(c.res.Content[0].Text, "只解析了前") {
if v, ok := sc["truncated"].(bool); !ok || !v {
t.Fatalf("structuredContent truncated = %#v, want true", sc["truncated"])
}
if !strings.Contains(c.res.Content[0].Text, "total 只是上限内的数量") {
t.Fatalf("summary does not mention the cap: %q", c.res.Content[0].Text)
}
// The note must stay on its own line, before the table.
@@ -743,3 +746,18 @@ func TestParseFileListEntryCap(t *testing.T) {
t.Fatalf("parsed %d entries, truncated=%v", len(files), truncated)
}
}
func TestParseFileListAtCapIsNotTruncated(t *testing.T) {
// Exactly cap entries followed by the client's zero padding: nothing was
// dropped, so the summary must not claim truncation.
var buf []byte
for i := 0; i < fileListMaxEntries; i++ {
buf = append(buf, 0, 'a', 0)
buf = append(buf, make([]byte, 16)...)
}
buf = append(buf, 0, 0, 0)
files, truncated := parseFileList(buf)
if len(files) != fileListMaxEntries || truncated {
t.Fatalf("parsed %d entries, truncated=%v", len(files), truncated)
}
}
+6 -2
View File
@@ -434,7 +434,7 @@
},
{
"name": "list_files",
"description": "列举指定在线主机的目录。省略或空 path 返回驱动器列表(drives);给定 path 返回该目录一层内的文件/子目录(files,最多 500 条,含 name/isDir/size/mtime)。只读。",
"description": "列举指定在线主机的目录。省略或空 path 返回驱动器列表(drives);给定 path 返回该目录一层内的文件/子目录(files,含 name/isDir/size/mtime),整目录解析后按 limit/offset 分页(默认 100 条、上限 1000 条,total/hasMore 是真实的目录条目数;目录超过 10 万条时按上限截断并返回 truncated=true)。Windows 上 \".\"/\"/\"/\"\\\\\" 视同省略 path;Linux/macOS 上 \"/\" 是根目录。只读。",
"inputSchema": {
"anyOf": [
{
@@ -469,7 +469,7 @@
"type": "integer"
},
"path": {
"description": "要列举的目录路径(如 C:\\Windows);省略或传空则返回驱动器列表",
"description": "要列举的目录路径(如 C:\\Windows 或 /etc)。省略或传空返回驱动器列表;Windows 上 \".\"、\"/\"、\"\\\\\" 视同省略;Linux/macOS 上 \"/\" 是根目录,按真实目录列举。",
"type": "string"
}
},
@@ -539,6 +539,10 @@
},
"totalAll": {
"type": "integer"
},
"truncated": {
"description": "目录条目超过服务端解析上限(100000 条)时为 true;仅在返回 files 时可能出现,此时 total 不是真实总数。",
"type": "boolean"
}
},
"type": "object"