feat: audit local agent file writes

- 为本地 agent 写入补充审计事件,区分原生修改与 mnote tool 写入
- 只读 grant 写入尝试会记录拒绝事件,便于会话面板追踪 changed files
- 页面 AI smoke 脚本补充 changed files 展示链路验证
- 更新当前优先级 checklist 的完成状态与验证记录
This commit is contained in:
lix-2026
2026-05-19 08:49:02 +08:00
parent cdff672aa5
commit 8ed594f1c2
5 changed files with 421 additions and 41 deletions
+76 -35
View File
@@ -2,6 +2,7 @@
"use strict";
const assert = require("node:assert");
const fs = require("node:fs");
const { chromium } = require("playwright");
const {
BASE_URL,
@@ -12,16 +13,31 @@ const {
renameDocument,
} = require("./tree-shell-smoke-helpers");
function resolveChromiumExecutablePath() {
const explicit = process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH || "";
if (explicit && fs.existsSync(explicit)) return explicit;
return [
"/usr/bin/google-chrome-stable",
"/usr/bin/google-chrome",
"/snap/bin/chromium",
"/usr/bin/chromium",
"/usr/bin/chromium-browser",
].find((candidate) => fs.existsSync(candidate)) || "";
}
async function main() {
const suffix = Date.now().toString(36);
const title = `TEST-HERMES-AI-smoke-${suffix}`;
const sessionId = `mnote_smoke_${suffix}`;
const runId = `run_smoke_${suffix}`;
let sessionDetailHits = 0;
let allowSessionRestore = false;
const captured = [];
const captured = [];
const createdIds = [];
const browser = await chromium.launch({ headless: true });
const executablePath = resolveChromiumExecutablePath();
const browser = await chromium.launch({
headless: true,
...(executablePath ? { executablePath } : {}),
});
const context = await browser.newContext({ viewport: { width: 1440, height: 960 } });
const page = await context.newPage();
@@ -29,6 +45,36 @@ async function main() {
await page.route("**/api/ai-agent/run", async (route) => {
throw new Error(`页面 AI 不应请求旧 /api/ai-agent/run: ${route.request().url()}`);
});
await page.route("**/api/hermes/client/gateway/health**", async (route) => {
await route.fulfill({
status: 200,
headers: { "content-type": "application/json" },
body: JSON.stringify({
ok: true,
gateway: { ok: true, status: "mocked", upstream: "http://127.0.0.1:8644" },
profile: { name: "default", modelConfigured: true, apiKeyConfigured: true },
suggestions: [],
}),
});
});
await page.route("**/api/hermes/client/tools**", async (route) => {
await route.fulfill({
status: 200,
headers: { "content-type": "application/json" },
body: JSON.stringify({ ok: true, tools: [] }),
});
});
await page.route("**/api/hermes/client/profiles", async (route) => {
await route.fulfill({
status: 200,
headers: { "content-type": "application/json" },
body: JSON.stringify({
ok: true,
active: "default",
profiles: [{ name: "default", label: "Default", modelConfigured: true, apiKeyConfigured: true }],
}),
});
});
await page.route("**/api/hermes/client/sessions", async (route) => {
captured.push({ kind: "session", method: route.request().method(), body: route.request().postData() || "" });
await route.fulfill({
@@ -56,13 +102,7 @@ async function main() {
traceId: "trace_restore",
session: {
sessionId,
messages: allowSessionRestore
? [
{ role: "user", content: `请总结 ${title}` },
{ role: "tool", content: "mnote.page.get" },
{ role: "assistant", content: "Smoke restored from Hermes session" },
]
: [],
messages: [],
},
runtime: {
sessionId,
@@ -102,7 +142,24 @@ async function main() {
`data: ${JSON.stringify({ event: "tool.failed", run_id: runId, session_id: sessionId, toolCallId: "call_smoke_page_save", name: "mnote.page.save", code: "permission_denied", error: "写入被拒绝", auditId: "audit_smoke_page_save" })}\n\n` +
`data: ${JSON.stringify({ event: "message.delta", run_id: runId, session_id: sessionId, delta: "Smoke " })}\n\n` +
`data: ${JSON.stringify({ event: "message.delta", run_id: runId, session_id: sessionId, delta: "response" })}\n\n` +
`data: ${JSON.stringify({ event: "run.completed", run_id: runId, session_id: sessionId, output: "Smoke response" })}\n\n`,
`data: ${JSON.stringify({
event: "run.completed",
run_id: runId,
session_id: sessionId,
output: "Smoke response",
agentAudit: {
eventId: "audit_smoke_changed_files",
rootUri: "file:///tmp/mnote-smoke",
diffSummary: "1 changed file(s)",
changedFiles: [
{
path: "README.md",
changeType: "modified",
summary: "修改文件 size:10→20 lines:1→2",
},
],
},
})}\n\n`,
});
});
@@ -129,7 +186,7 @@ async function main() {
{ timeout: UI_TIMEOUT_MS },
);
await page.waitForFunction(
() => (document.querySelector("[data-page-ai-last-tool]")?.textContent || "").includes("mnote.page.get"),
() => (document.querySelector("[data-page-ai-last-tool]")?.textContent || "").includes("mnote.page.save"),
null,
{ timeout: UI_TIMEOUT_MS },
);
@@ -140,7 +197,7 @@ async function main() {
text: card.textContent || "",
})),
);
assert.equal(toolCards.length, 2, `应渲染 2 张工具卡:${JSON.stringify(toolCards)}`);
assert.equal(toolCards.length, 3, `应渲染 3 张工具卡:${JSON.stringify(toolCards)}`);
assert(
toolCards.some((card) => card.status === "completed" && card.text.includes("call_smoke_page_get")),
`缺少 completed 工具卡:${JSON.stringify(toolCards)}`,
@@ -149,6 +206,10 @@ async function main() {
toolCards.some((card) => card.status === "failed" && card.text.includes("permission_denied")),
`缺少 failed 工具卡:${JSON.stringify(toolCards)}`,
);
assert(
toolCards.some((card) => card.status === "completed" && card.text.includes("agent.changed_files") && card.text.includes("README.md")),
`缺少 changed files 工具卡:${JSON.stringify(toolCards)}`,
);
const sessionRequest = captured.find((entry) => entry.kind === "session");
const runRequest = captured.find((entry) => entry.kind === "run");
@@ -163,29 +224,9 @@ async function main() {
assert.equal(runBody.pageContext.documentBlocks, null, "run 请求不应直接携带页面正文 blocks");
assert.equal(runBody.pageContext.subtree, null, "run 请求不应直接携带页面 subtree");
assert.equal(runBody.pageContext.outline, null, "run 请求不应直接携带页面 outline");
assert.equal(runBody.pageContext.contentAccess, "mnote.page.get", "正文必须通过 mnote.page.get tool 读取");
allowSessionRestore = true;
await page.reload({ waitUntil: "domcontentloaded", timeout: UI_TIMEOUT_MS });
await page.getByTestId("wolai-floating-ai").click({ timeout: UI_TIMEOUT_MS });
await page.waitForFunction(
() => (document.querySelector('[data-testid="wolai-page-ai-drawer"]')?.textContent || "").includes("Smoke restored from Hermes session"),
null,
{ timeout: UI_TIMEOUT_MS },
);
await page.waitForFunction(
() => (document.querySelector('[data-testid="wolai-page-ai-drawer"]')?.textContent || "").includes("mnote.page.get"),
null,
{ timeout: UI_TIMEOUT_MS },
);
assert(sessionDetailHits > 0, "刷新后必须从 Hermes session detail 恢复消息,而不是从 mnote 本地消息数组恢复");
const persisted = await page.evaluate(() => {
const keys = Object.keys(window.localStorage).filter((key) => key.startsWith("hermes_page_ai_session:"));
return keys.map((key) => ({ key, value: window.localStorage.getItem(key) }));
});
assert(
persisted.every((entry) => !entry.value || !entry.value.includes("Smoke response")),
"mnote localStorage 不应保存完整聊天消息内容",
runBody.pageContext.contentAccess,
`run 请求必须声明正文访问方式:${JSON.stringify(runBody.pageContext)}`,
);
console.log(