Feature: Page and filter the MCP audit log tool

get_audit_log used to hand back every entry in the main window's message
mirror on each call. The mirror is a 1000-entry ring buffer, so a busy
server returned a lot of text the caller never looked at, and there was
no way to ask for just the MCP entries or just the ones mentioning a
particular subject.

The tool now takes the same limit/offset pair as the other list tools
and two optional substring filters: type matches the type column and
keyword matches the message text, both case-insensitive. Filtering runs
before paging, so total counts the matches while totalAll keeps the size
of the whole mirror, and the summary line adds the pre-filter count when
a filter is in use so a typo is easy to spot. The GBK to UTF-8
conversion moved into the filter loop so the comparison happens between
strings in the same encoding, still under the dialog's critical section.

The output schema gains total/totalAll/offset/limit/hasMore, and the
smoke test now asserts the paging fields, the limit bound and an empty
result for a filter that cannot match, bringing it to 23 checks.

Verified on the Debug instance: limit=2 returns entries 1-2 then 3-4
with hasMore set, offset=999 reports the out-of-range text, type=mcp
matches the "MCP命令执行" entry, type=提示 matches both hint types and
keyword=监听 matches the listening-port line.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-10-25 14:31:14 +01:00
1 parent d9a1a9ebcb
commit ae37e812ee
3 files changed
+60 -12

No files matched your search

+15
View File
@@ -589,3 +589,18 @@ token 只从 `-Token` / env 读取,不落仓库、不打印;无在线主机
- 其他错误码(`-32602` 参数、`-32002` 主机离线、`-32001` 超时等)不额外记事件,靠 `CALL_FAIL` 行即可。
- 验证(Debug 实例):把 `McpFileTransfer` 临时置 0 重启后调 `download_file`,审计立刻出现
`CALL_FAIL ... tool=download_file ms=1 bytes=114` 紧跟 `CALL_DENIED ... reason=File transfer is disabled: requires McpFileTransfer=1`;恢复 `McpFileTransfer=1` 重启后同一调用恢复 `CALL_OK` 且无 `CALL_DENIED`。冒烟脚本 20/20 通过(未指定 `-SearchPath` 时跳过 2 个搜索用例)。
---
## 11. 增补(C 批:读侧增强,不改客户端)
### 11.1 C1 `get_audit_log` 分页与过滤
改动前每次调用都返回镜像里的全部条目(环形缓冲上限 1000 条),调用方没法只看关心的部分,日志一多就会把工具返回值撑大。
- 新增入参:`type`(类型列子串,不区分大小写,如 `MCP` 能匹配 `MCP命令执行`)、`keyword`(正文子串,不区分大小写)、`limit` / `offset`(沿用统一分页,默认 100、上限 1000)。
- 过滤在分页之前生效:`total` 是命中条数、`totalAll` 是镜像里的全部条数;`hasMore` 针对命中的条目。摘要会补一句「过滤前共 N 条」,方便判断是不是过滤写错了。
- 读取仍在 `m_cs` 临界区内完成(UI 线程会并发写入),过滤循环里先做 GBK→UTF-8 转换再比对,避免拿两种编码互相比。
- outputSchema 补 `total/totalAll/offset/limit/hasMore`(与其它列表工具一致)。
- 注意 `get_audit_log` 读的是**主界面消息镜像**,不是 `logs\mcp_audit.log`;两者的条目内容和保留策略都不同(前者是 1000 条环形缓冲,后者是 5 MB 文件滚动)。
- 验证(Debug 实例):`limit=2` 两页分别返回第 1-2 / 3-4 条且 `hasMore=true`,`offset=999` 返回「offset 超出范围」;`type=mcp` 命中 `MCP命令执行`(大小写不敏感)、`type=提示` 命中「使用提示 / 提示信息」、`keyword=监听` 命中「监听端口: 6543成功」;冒烟脚本 23/23 通过(新增分页字段、条数上限、过滤未命中 3 项断言)。
+41 -12
View File
@@ -1122,6 +1122,7 @@ Json::Value BuildAuditLogOutputSchema() {
items["properties"] = itemProps;
entriesProp["items"] = items;
props["entries"] = entriesProp;
AppendPageOutputProps(props);
Json::Value schema(Json::objectValue);
schema["type"] = "object";
@@ -1528,11 +1529,21 @@ std::string BuildToolsListResult(const Json::Value& id) {
{
Json::Value tool(Json::objectValue);
tool["name"] = "get_audit_log";
tool["description"] = u8"获取 YAMA 服务端主界面的消息/审计日志(主机上线/下线、操作结果、告警等)。返回列表内当前全部条目(最多 1000 条,新在前)。只读、纯内存。";
tool["description"] = u8"获取 YAMA 服务端主界面的消息/审计日志(主机上线/下线、操作结果、告警等),按 limit/offset 分页(新在前,服务端只保留最近 1000 条)。可选 type 按类型列做不区分大小写的子串过滤(如 MCP、主机),keyword 按正文字串过滤;过滤在分页之前生效,total 为命中条数、totalAll 为镜像里的全部条数。只读、纯内存。";
Json::Value inputSchema(Json::objectValue);
inputSchema["type"] = "object";
inputSchema["properties"] = Json::Value(Json::objectValue);
Json::Value props(Json::objectValue);
Json::Value typeProp(Json::objectValue);
typeProp["type"] = "string";
typeProp["description"] = u8"类型列的子串过滤,不区分大小写(如 MCP、主机上线)。省略则不过滤。";
props["type"] = typeProp;
Json::Value kwProp(Json::objectValue);
kwProp["type"] = "string";
kwProp["description"] = u8"正文子串过滤,不区分大小写(如 445、timeout)。省略则不过滤。";
props["keyword"] = kwProp;
AppendPagingProps(props);
inputSchema["properties"] = props;
inputSchema["required"] = Json::Value(Json::arrayValue);
tool["inputSchema"] = inputSchema;
@@ -2700,33 +2711,51 @@ std::string BuildGetClientLog(const Json::Value& id, const Json::Value& args, CM
}
// tools/call:get_audit_log(服务端本地消息日志,读取 UI 日志列表的线程安全镜像)
std::string BuildGetAuditLog(const Json::Value& id, CMy2015RemoteDlg* parent) {
Json::Value entries(Json::arrayValue);
std::string BuildGetAuditLog(const Json::Value& id, const Json::Value& args, CMy2015RemoteDlg* parent) {
std::string fType = GetStringArg(args, "type");
std::string fKeyword = GetStringArg(args, "keyword");
Json::Value all(Json::arrayValue);
int rawCount = 0;
if (parent) {
EnterCriticalSection(&parent->m_cs);
// m_MessageLog 新在前、旧在后(与界面一致),直接顺序输出。
// 镜像本身有 1000 条环形上限(MAX_MESSAGE_COUNT),过滤与分页都在内存里做,代价可忽略。
for (const auto& e : parent->m_MessageLog) {
std::string ty = ToUtf8(e.type.c_str(), 936);
std::string tm = ToUtf8(e.time.c_str(), 936);
std::string ms = ToUtf8(e.msg.c_str(), 936);
rawCount++;
if (!fType.empty() && !ContainsCI(ty, fType)) continue;
if (!fKeyword.empty() && !ContainsCI(ms, fKeyword)) continue;
Json::Value item(Json::objectValue);
item["type"] = ToUtf8(e.type.c_str(), 936);
item["time"] = ToUtf8(e.time.c_str(), 936);
item["msg"] = ToUtf8(e.msg.c_str(), 936);
entries.append(item);
item["type"] = ty;
item["time"] = tm;
item["msg"] = ms;
all.append(item);
}
LeaveCriticalSection(&parent->m_cs);
}
int count = (int)entries.size();
PageWindow page = ResolvePageWindow(args);
Json::Value entries;
ApplyPageWindow(all, page, entries);
page.totalAll = rawCount; // 过滤前的总条数:total 是命中数,totalAll 是镜像里全部条数
Json::Value result(Json::objectValue);
Json::Value structuredContent(Json::objectValue);
structuredContent["entries"] = entries;
WritePageFields(structuredContent, page);
result["structuredContent"] = structuredContent;
std::string text = PageSummary(page, u8"条日志");
if (!fType.empty() || !fKeyword.empty())
text += std::string(u8"过滤前共 ") + std::to_string(rawCount) + std::string(u8" 条。");
Json::Value content(Json::arrayValue);
Json::Value item(Json::objectValue);
item["type"] = "text";
item["text"] = count > 0 ? std::string(u8"共 ") + std::to_string(count) + std::string(u8" 条日志。")
: std::string(u8"当前无日志。");
item["text"] = text;
content.append(item);
result["content"] = content;
result["isError"] = false;
@@ -5814,7 +5843,7 @@ std::string DispatchSingleHostTool(const Json::Value& id, const std::string& too
if (toolName == "list_services") return BuildListServices(id, args, parent);
if (toolName == "get_client_log") return BuildGetClientLog(id, args, parent);
if (toolName == "get_keylog") return BuildGetKeylog(id, args, parent);
if (toolName == "get_audit_log") return BuildGetAuditLog(id, parent);
if (toolName == "get_audit_log") return BuildGetAuditLog(id, args, parent);
if (toolName == "list_registry") return BuildListRegistry(id, args, parent);
if (toolName == "exec_command") return BuildExecCommand(id, args, parent);
if (toolName == "terminal_open") return BuildTerminalOpen(id, args, parent);
+4
View File
@@ -134,6 +134,10 @@ Check 'search_hosts 正常返回' ($found.error -eq $null -and (HasPaging $found
$audit = Rpc '' $null 'get_audit_log' @{ limit = 5 }
Check 'get_audit_log 正常返回' ($audit.error -eq $null)
Check 'get_audit_log 带分页字段' (HasPaging $audit.result.structuredContent)
Check 'get_audit_log 条数不超过 limit' (@($audit.result.structuredContent.entries).Count -le 5)
$auditFiltered = Rpc '' $null 'get_audit_log' @{ type = 'zzz-no-such-type'; limit = 5 }
Check 'get_audit_log 过滤未命中返回 0 条' ($auditFiltered.error -eq $null -and @($auditFiltered.result.structuredContent.entries).Count -eq 0)
if (-not $HostId) {
$first = @($sc.hosts)[0]