Fix: Paginate get_activity_history results
The tool was the only list-type tool without pagination. The client keeps ACTIVITY_HISTORY_MAX=500 records and Dump() prepends the in-progress window, so a single call returned up to 501 lines of "[time] [title] duration" -- enough to blow up the caller's context, especially since titles carry personal browsing history. Records come back newest-first (m_records uses push_front and the current window is prepended), so the standard limit/offset window applies directly: offset=0 is the most recent page, no need to read total first and count backwards. That ordering is now stated in the tool description so the model can rely on it. - inputSchema: BuildGetHostDetailInputSchema -> BuildPagedHostListInputSchema (adds limit, default 100 / max 1000, and offset) - outputSchema: append AppendPageOutputProps plus truncated - implementation: rename the full split to `all`, slice via ResolvePageWindow and ApplyPageWindow, write back with WritePageFields - activityHistory is rebuilt from the window instead of returning the whole Dump() text -- otherwise the raw copy would bypass pagination entirely - 64 KB byte budget (same as get_client_log), trimmed from the end so the newest records survive; hasMore is computed as offset+kept < total rather than hardcoded true, since a single oversized tail record must not claim there is another page Batch calls need no extra work: the tool is already in IsBatchReadOnlyTool, and BuildBatchCall forwards limit/offset to each host, so ids + limit means "the latest N records of every host". Compiles clean; not yet exercised against a running server (the exe was locked by the live instance). Verification cases are listed in docs/Mcp_Test_Report_2026-10-29.md section 8.
This commit is contained in:
1 parent
56a937fabb
commit
dcb5ae25c6
3 files changed
+154
-10
No files matched your search
@@ -1183,7 +1183,7 @@ Json::Value BuildWindowListOutputSchema() {
|
||||
return schema;
|
||||
}
|
||||
|
||||
// get_activity_history 的 outputSchema(records 数组 + 原始文本)
|
||||
// get_activity_history 的 outputSchema(records 数组 + 原始文本 + 翻页字段)
|
||||
Json::Value BuildActivityHistoryOutputSchema() {
|
||||
Json::Value props(Json::objectValue);
|
||||
|
||||
@@ -1192,12 +1192,21 @@ Json::Value BuildActivityHistoryOutputSchema() {
|
||||
Json::Value items(Json::objectValue);
|
||||
items["type"] = "string";
|
||||
recordsProp["items"] = items;
|
||||
recordsProp["description"] = u8"本次窗口内的记录,每条一行「[时间] [标题] 时长」。新在前。";
|
||||
props["records"] = recordsProp;
|
||||
|
||||
Json::Value rawProp(Json::objectValue);
|
||||
rawProp["type"] = "string";
|
||||
rawProp["description"] = u8"与 records 同源的纯文本(records 按行拼接),窗口外的记录不包含。";
|
||||
props["activityHistory"] = rawProp;
|
||||
|
||||
Json::Value truncProp(Json::objectValue);
|
||||
truncProp["type"] = "boolean";
|
||||
truncProp["description"] = u8"因 64 KB 字节预算而比 limit 少返回了条目(此时 hasMore 也为 true)。";
|
||||
props["truncated"] = truncProp;
|
||||
|
||||
AppendPageOutputProps(props); // total / totalAll / offset / limit / hasMore
|
||||
|
||||
Json::Value schema(Json::objectValue);
|
||||
schema["type"] = "object";
|
||||
schema["properties"] = props;
|
||||
@@ -1993,9 +2002,9 @@ std::string BuildToolsListResult(const Json::Value& id) {
|
||||
{
|
||||
Json::Value tool(Json::objectValue);
|
||||
tool["name"] = "get_activity_history";
|
||||
tool["description"] = u8"获取指定在线主机的历史活动记录(前台窗口驻留时长,每行一条「[时间] [标题] 时长」)。主连接 RPC、一次性返回。";
|
||||
tool["description"] = u8"获取指定在线主机的历史活动记录(前台窗口驻留时长,每行一条「[时间] [标题] 时长」)。主连接 RPC。结果按 limit/offset 分页(默认 100 条、上限 1000),新在前:offset=0 就是最近 limit 条,想看更早的再往后翻。total 为本机现有记录总数,hasMore 表示还有更早的记录;客户端只保留最近 500 条,所以 total 最多约 501(含进行中的当前窗口)。";
|
||||
|
||||
tool["inputSchema"] = BuildGetHostDetailInputSchema(); // 复用 { id } 必填 schema
|
||||
tool["inputSchema"] = BuildPagedHostListInputSchema(); // { id | ids } + limit/offset
|
||||
tool["outputSchema"] = BuildActivityHistoryOutputSchema();
|
||||
|
||||
tools.append(tool);
|
||||
@@ -2966,21 +2975,72 @@ std::string BuildGetActivityHistory(const Json::Value& id, const Json::Value& ar
|
||||
text.assign((const char*)data.data() + 1, data.size() - 1);
|
||||
|
||||
// 拆行:每行一条记录,过滤空行(含 Dump 末尾的换行)。
|
||||
Json::Value records(Json::arrayValue);
|
||||
// 顺序是「新在前」:客户端 m_records 用 push_front 维护,Dump() 又把进行中的
|
||||
// 当前窗口放在最前面。所以 offset=0 天然就是“最近 limit 条”,不需要先取
|
||||
// total 再倒着算 offset——这正是日志类数据最常用的取法。
|
||||
Json::Value all(Json::arrayValue);
|
||||
size_t start = 0;
|
||||
while (start <= text.size()) {
|
||||
size_t nl = text.find('\n', start);
|
||||
std::string line = text.substr(start, (nl == std::string::npos ? text.size() : nl) - start);
|
||||
if (!line.empty()) records.append(line);
|
||||
if (!line.empty()) all.append(line);
|
||||
if (nl == std::string::npos) break;
|
||||
start = nl + 1;
|
||||
}
|
||||
int count = (int)records.size();
|
||||
|
||||
// 分页:实测一次 501 条会把上下文撑爆(P2-1)。本工具无服务端过滤,
|
||||
// total == totalAll,ApplyPageWindow 的默认口径即可。
|
||||
PageWindow page = ResolvePageWindow(args);
|
||||
Json::Value records(Json::arrayValue);
|
||||
ApplyPageWindow(all, page, records);
|
||||
|
||||
// 字节预算:limit 上限 1000,窗口内若标题很长仍可能过大。从窗口末尾裁
|
||||
// (保留最新的那些),裁掉的部分由 truncated 与 hasMore 一起表达。
|
||||
const size_t kMaxBytes = 64 * 1024;
|
||||
bool byteTrimmed = false;
|
||||
{
|
||||
size_t bytes = 0;
|
||||
int kept = 0;
|
||||
for (const Json::Value& r : records) {
|
||||
const std::string s = r.asString();
|
||||
if (bytes + s.size() + 1 > kMaxBytes) { byteTrimmed = true; break; }
|
||||
bytes += s.size() + 1;
|
||||
++kept;
|
||||
}
|
||||
if (byteTrimmed) {
|
||||
// 至少留一条,保证调用方按 offset+returned 前进时不会原地打转
|
||||
// (单条超过 64 KB 在窗口标题上几乎不可能,这里只是兜底)。
|
||||
if (kept == 0) {
|
||||
kept = 1;
|
||||
Json::Value one(Json::arrayValue);
|
||||
one.append(records[0]);
|
||||
records = one;
|
||||
} else {
|
||||
Json::Value trimmed(Json::arrayValue);
|
||||
for (int i = 0; i < kept; ++i) trimmed.append(records[i]);
|
||||
records = trimmed;
|
||||
}
|
||||
page.returned = kept;
|
||||
// 还有没有更早的记录,按「已返回到的绝对位置」算,而不是一律置 true:
|
||||
// 末尾那条自己超预算时,offset+kept 已经等于 total,不该再报 hasMore。
|
||||
page.hasMore = (page.offset + kept < page.total);
|
||||
}
|
||||
}
|
||||
|
||||
// activityHistory 是同一份数据的纯文本形态:必须按窗口重建,
|
||||
// 否则整包原文会绕过分页,等于没分页。
|
||||
std::string shown;
|
||||
for (const Json::Value& r : records) {
|
||||
shown += r.asString();
|
||||
shown += '\n';
|
||||
}
|
||||
|
||||
Json::Value result(Json::objectValue);
|
||||
Json::Value structuredContent(Json::objectValue);
|
||||
structuredContent["records"] = records;
|
||||
structuredContent["activityHistory"] = text;
|
||||
structuredContent["activityHistory"] = shown;
|
||||
structuredContent["truncated"] = byteTrimmed;
|
||||
WritePageFields(structuredContent, page);
|
||||
result["structuredContent"] = structuredContent;
|
||||
|
||||
// 正文外的 JSON 块只放 records:activityHistory 是同一份数据的原文,
|
||||
@@ -2992,7 +3052,11 @@ std::string BuildGetActivityHistory(const Json::Value& id, const Json::Value& ar
|
||||
Json::Value content(Json::arrayValue);
|
||||
Json::Value item(Json::objectValue);
|
||||
item["type"] = "text";
|
||||
item["text"] = std::string(u8"共 ") + std::to_string(count) + std::string(u8" 条历史活动记录。");
|
||||
item["text"] = std::string(u8"共 ") + std::to_string(page.total) + std::string(u8" 条历史活动记录(新在前),本次返回第 ") +
|
||||
std::to_string(page.offset) + std::string(u8" 条起的 ") + std::to_string(page.returned) + std::string(u8" 条") +
|
||||
(page.hasMore ? std::string(u8";hasMore=true 表示还有更早的记录,可用 offset=") +
|
||||
std::to_string(page.offset + page.returned) + std::string(u8" 继续取。")
|
||||
: std::string(u8"。"));
|
||||
content.append(item);
|
||||
result["content"] = content;
|
||||
result["isError"] = false;
|
||||
|
||||
Reference in new issue
Block a user