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:
1 parent
42dd70fe20
commit
5e012019ba
6 files changed
+137
-40
No files matched your search
+18
-14
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user