From a6e31dec9de5bd2006352e5c77f932af181d5bd8 Mon Sep 17 00:00:00 2001 From: Agent Workbench Dev Date: Fri, 6 Nov 2026 12:22:37 +0800 Subject: [PATCH] feat(context): audit tool visibility and isolate diagnostic history --- docs/QA-TOOL-PREFIX-C.md | 23 +++++ findings.md | 7 ++ package-lock.json | 4 +- package.json | 2 +- progress.md | 9 ++ scripts/verify-request-observation-ui.mjs | 80 +++++++++++++++++ src/client/RequestContextPanel.tsx | 16 ++++ src/client/TaskPanel.css | 8 ++ src/client/types.ts | 8 +- src/core/context/prefix-audit.mjs | 26 +++++- src/core/context/request-observation.mjs | 19 ++++ src/core/context/request-plan.mjs | 38 +++++--- src/core/context/tool-surface.mjs | 40 +++++++++ src/server/diagnostics.mjs | 2 +- src/server/runtime.mjs | 56 +++++++++--- task_plan.md | 12 +++ tests/task-control.test.mjs | 6 +- tests/tool-prefix-observation.test.mjs | 100 ++++++++++++++++++++++ 18 files changed, 420 insertions(+), 36 deletions(-) create mode 100644 docs/QA-TOOL-PREFIX-C.md create mode 100644 scripts/verify-request-observation-ui.mjs create mode 100644 src/core/context/request-observation.mjs create mode 100644 src/core/context/tool-surface.mjs create mode 100644 tests/tool-prefix-observation.test.mjs diff --git a/docs/QA-TOOL-PREFIX-C.md b/docs/QA-TOOL-PREFIX-C.md new file mode 100644 index 00000000..4c863026 --- /dev/null +++ b/docs/QA-TOOL-PREFIX-C.md @@ -0,0 +1,23 @@ +# C:工具可见性与请求前缀证据 + +本阶段从 b14e26e6 的干净工作树接续。保持默认完整历史透传、不主动附加输出额度和持久操作权限;没有为诊断恢复旧 16K/2048 限制。正式起步 1.25.7 / PID71628,用户已改选「高性能」池 revision8,原选择与五条有凭据连接保留。 + +实现范围: + +- 每次实际装配记录已登记工具、所属能力组、装配/可见状态和缺席原因;保存 schema SHA、定义字节数与基于内容的 generation。generation 是内容摘要,不是假定递增计数。注册或发送不等于获得执行权限;实际执行仍重新核对资源修订和权限。 +- prefixAudit v2 保留原有按序 UTF-8 共同前缀口径,补齐型号/工具/原生系统/逐消息块的 SHA 与字节数、首次变化位置和原因、各协议最终工具的 SHA/字节。该规范化顺序不是供应商内部缓存算法,不能换算缓存费用。 +- 请求观察记录用户消息实例、原 provider 历史摘要及主任务/诊断/worker 路径。新用户消息绑定真实消息 UUID;历史无法归类的 provider-user 保留 unclassified,不冒认为用户原话。 +- 修复新诊断问答混入主任务模型历史的问题。诊断复制当时主历史,在所属 Run 独立保存后续问答与工具配对;UI 原消息和原失败保持。后续主请求与先前主请求比较,诊断有自己的前驱链。旧诊断/旧发送字节不迁移或重写。 +- 现有“请求上下文”显示归属、用户实例、变化段和可展开的本轮工具表。估算 tokens、输出策略、HTTP 字节、共同前缀字节与上游原缓存用量分开,缺失缓存始终为未知。 + +验证与首失败: + +- 首 14 项 13 通过,旧诊断测试仍从主历史找旁路结果而失败。按新独立历史契约调整,并增加主历史完全保持断言。 +- 扩大相关原轮 41 项 40 通过:MCP/Skills 源改变与停用后的执行拒绝、原工具配对、任务权限及恢复通过;新动态 tab 夹具的非法身份导致失败。数次修正仍保留失败,最终直接捕获持久化异常确认是夹具 tab ID 不符合 UUID 契约,非上游网络故障。原网络分类只作为本地夹具错误现象保留,不归因于真实供应商。 +- 修正后专项 4/4,包括三协议真实适配 schema、动态 tab 代次、两次“继续”的不同消息实例、配置保存后旧工件不变、诊断与主线隔离/重启、旧 v1 工件核对。 +- 网页和实际 Electron 各 4 组、1440/390 两种尺寸通过,每端 4 次本地模拟模型请求、界面操作期间 0 次模型请求。首网页 fixture 没有真实请求尝试导致无比较前驱、次轮窄屏未重新打开面板的失败均保留;改为本地 HTTP 模型和真实窄屏操作验证。 +- 只读运行 verify-working-memory-u.mjs,620 份 / 100126636 字节全部核对;8 份原 T 请求、2 份 U 重建和21份历史 fixture计划仍可验证。没有重放历史模型、工具、安装或变更其原预算。 + +最终1.25.8构建通过,精确前缀/观察回归8/8、版本/保留原进程升级3/3通过;最终Electron4组通过,CSS只将辅助说明独立成行。证据目录 `.qa/tool-prefix-c/`。源码实现及相关验证完成,不可变发行/隔离安装/正式保留状态升级继续执行;不能把源码验证当作已部署。正式更新仅检查本次升级原状态,源码导出不访问项目数据。 + +本阶段尚未发出真实供应商请求。真实供应商长期稳定、自主 Node 交付、普遍重复读取改善、缓存收益、真实异常预留排除边界及 Purge AB/BA 收益均不据此通过。后续另冻新批次,保留所有历史网络、截断、只读不写和未交付事实。 diff --git a/findings.md b/findings.md index 99f8b550..f67905ae 100644 --- a/findings.md +++ b/findings.md @@ -1,5 +1,12 @@ # 汇总发现 +## 当前 C 与新真实验证 + +- 起步 b14e26e6 工作树干净;正式 1.25.7 / PID71628 / instance37d6b536-c0dc-4cbf-9c51-095bbe779062 空闲。默认 unrestricted/full/passthrough,当前选择用户新配的「高性能」池(revision8),五条真实连接公开 hasKey=true;legacy 占位 hasKey=false 不能代表其它连接没有凭据。 +- 已有 prefixAudit v1 保存实际适配请求的工具名/整体 SHA/字节、共同前缀字节与原始 cache 用量。缺 per-tool 缺席原因与 generation、语义变化段/消息实例/路径隔离;这些诊断应写入模型输入外的不可变工件,不改变原 wire。 +- 旧 finite-budget 样本只保留原契约;新样本遵从用户完整透传/默认不限要求。不将共同前缀字节当上游缓存命中,也不据短样本宣称供应商稳定。 + + 1.25.5正式交付已完成:来源5acb6571,实际重启PID71628,unrestricted/full已持久保存。当前85项目/104会话/17804事件;原工件字节保持,外部引航演示续接/新会话及40事件另列,严格final首失败和独立只读复核均保留。临时Key随重启消失,公开hasKey=false,未读取或转存。最终714输入重建22/22、三归档、大历史跨版3组、新网页10/Electron11及桌面32项通过;976轮958/18与旧0c130UI保留真实来源。见docs/QA-OPEN-PERMISSIONS-1.25.5.md和发行身份。真实供应商、自主交付、缓存、重复读取、真实预留边界和Purge收益仍未验证。 1.25.5权限接续:build-7与45/45权限/透传/浏览器/文件结果通过。末遍沿调用链补齐变更集4MB、Git索引/检出/进程、Python结果、宿主JSON和REA原文的旧额度。build-8通过;扩大回归仍在运行,已观察研究工具、大文件变更集和32轮压力三项失败,待终态定位。网页初轮把权限演示的旧有限fixture请求混入透传断言而失败,原件保留;改为modernDefaults后网页7组通过。Electron原7组已过,新增full浏览器流程首失败正在定位,不称全过。正式仍1.25.4,真实推理0,未操作Key。新正式驱动按before/upgrade/configure/restart/final写一次性收据,仅两项配置行允许明确变化,其余原行/全导出/工件SHA对账;不沿用旧“不重启”驱动。 diff --git a/package-lock.json b/package-lock.json index 57bdbedb..0092a300 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "local-agent-workbench", - "version": "1.25.7", + "version": "1.25.8", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "local-agent-workbench", - "version": "1.25.7", + "version": "1.25.8", "dependencies": { "@babel/parser": "7.29.11", "@peculiar/x509": "3.1.0", diff --git a/package.json b/package.json index 8af7e610..48df1049 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "local-agent-workbench", - "version": "1.25.7", + "version": "1.25.8", "private": true, "type": "module", "description": "A local coding workbench with explicit tool execution, reviewable changes and recoverable sessions.", diff --git a/progress.md b/progress.md index db88acda..2ee35e21 100644 --- a/progress.md +++ b/progress.md @@ -1,5 +1,14 @@ # 统一执行进度 +## C / 新真实验证接续 + +- C实现已完成:工具库存/资源代次、v2语义块、用户UUID/路径、独立诊断历史及现有请求面板。首14项13过(旧诊断历史断言)、扩大41项40过(新tab夹具身份);修正后专项4/4。夹具中几次补字段仍未定位UUID根因的失败全部保留,不把其网络外观当真实供应商异常。 +- 网页/Electron各4组通过,首网页未产生请求前驱与窄屏未重新打开面板的验收器失败保持。旧U620份100126636字节只读通过,8原请求/2重建/21历史fixture仍可核对。版本同步1.25.8,最后构建与精确回归进行中;正式仍1.25.7,未发真实请求。 + +- 已读 AGENTS、天枢与1.25计划,核对 b14e26e6 干净基线、正式1.25.7及用户新模型池配置。未发上游请求、未触Key、未续跑原项目。 +- 接续使用 planning-with-files;核对现有 prefix-audit/request-plan/runtime 入口。不存在的脚本名和 PowerShell rg 路径通配读取失败仅为发现阶段命令错误,未改变任何项目或历史证据。首规划补丁因 progress 标题误用被拒,无文件变化,已按实际标题修正。 +- 当前实现 C 工具与前缀证据,随后相关验证/界面观察/阶段本地提交;新真实验证尚未冻结或执行。 + 本接续45/45专项与build-8已完成;网页新默认7组通过,原错误/自动写入/原字节回滚和权限运行中切换实测成立。扩大回归及Electron full浏览器增验尚未全过,原首失败保留,定位后再冻结发行。新增正式分阶段驱动,正式1.25.4未改动/未重启,不读取或重存Key。一次只读命令cwd漏“向”被拒、空规划补丁标题不匹配被拒,均无产品变化。 当前推进1.25.5默认透传和三权限:操作权限控件已接入对话框,可运行中保存;新安装ask、自动批准auto、完全访问full分别持久化。已补自动Git基线、单项原字节回滚、开放提示与旧预算文案修正;构建已通过,权限/透传专项尚在执行。已观察真实宿主三权限重启保存及等待中切full一次执行通过,一项新Git基线交付测试失败待定位。正式仍1.25.4,未安装、未重启、未碰真实Key或续跑原项目。 diff --git a/scripts/verify-request-observation-ui.mjs b/scripts/verify-request-observation-ui.mjs new file mode 100644 index 00000000..d04fa3d8 --- /dev/null +++ b/scripts/verify-request-observation-ui.mjs @@ -0,0 +1,80 @@ +import fs from 'node:fs'; +import path from 'node:path'; +import net from 'node:net'; +import assert from 'node:assert/strict'; +import { randomUUID } from 'node:crypto'; +import { chromium, _electron } from 'playwright'; +import { fixture, launchWorkbench, until, call, completion, compatibleFixture } from '../tests/helpers.mjs'; +import { Provider } from '../src/server/provider.mjs'; +import { Runtime } from '../src/server/runtime.mjs'; +import { HostController } from '../src/core/desktop/host-controller.mjs'; +import { desktopExecutableIdentity } from '../src/core/desktop/app-installation.mjs'; +import { projectEnvironment } from '../src/core/process-environment.mjs'; + +assert.ok(process.argv.slice(2).every(value => value === '--desktop')); +const desktop = process.argv.includes('--desktop'), appRoot = path.resolve(process.env.AGENT_WORKBENCH_QA_APP_ROOT || '.'); +const directory = path.resolve('.qa/tool-prefix-c', (desktop ? 'electron-' : 'web-') + randomUUID()); +fs.mkdirSync(directory, { recursive: true }); console.log('EVIDENCE ' + directory); +const disposers = [], t = { after: fn => disposers.push(fn) }, f = fixture(t, { modernDefaults: true }), provider = new Provider(); +let round = 0; +const model = await compatibleFixture(t, () => ({ ...completion(round++ ? [] : [call('owned-ui-list', 'list_files', { path: '' })], '已核对原事实。'), usage: { prompt_tokens: 1000, completion_tokens: 12 } })); +const hub = provider.modelHub(f.store), connection = hub.upsert({ name: '本机界面夹具', model: 'owned-ui', baseUrl: model.baseUrl, requiresKey: false }); +hub.select({ type: 'connection', id: connection.id }); +const runtime = new Runtime(f.store, provider); +const runs = []; +for (const content of ['读取项目并回答,不修改文件,不运行工程检查。', '前面的错误是怎么回事?', '继续']) { + runtime.start(f.session.id, content); runs.push(f.session.runId); await runtime.active.get(f.session.id).promise; +} +f.store.close(); +const marks = [], screenshots = [], errors = [], launches = []; +let host, controller, browser, app, page, window, result; +const panel = () => page.getByLabel('请求上下文', { exact: true }); +const contains = async text => until(async () => (await panel().innerText()).includes(text)); +const mark = value => { marks.push(value); console.log('PASS ' + value); }; +async function shot(name) { const file = path.join(directory, name + '.png'); await page.screenshot({ path: file, fullPage: true }); screenshots.push(file); } +async function resize(width) { + if (window) { await window.evaluate((win, size) => win.setContentSize(size.width, size.height), { width, height: 1000 }); await until(() => page.evaluate(w => innerWidth === w, width)); } + else await page.setViewportSize({ width, height: 1000 }); + assert.ok(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth + 1), 'Unexpected horizontal overflow.'); +} +async function openRecord() { await page.getByRole('button', { name: '查看任务记录', exact: true }).click(); await panel().waitFor({ state: 'visible' }); } +try { + if (desktop) { + const socket = net.createServer(); await new Promise(resolve => socket.listen(0, '127.0.0.1', resolve)); const port = socket.address().port; await new Promise(resolve => socket.close(resolve)); assert.notEqual(port, 4318); + controller = new HostController({ appRoot, dataRoot: f.data, port, nodePath: process.execPath, timeoutMs: 60000 }); await controller.start(); + const executablePath = desktopExecutableIdentity(appRoot).path; + app = await _electron.launch({ executablePath, args: [...(executablePath.toLowerCase().endsWith('agentworkbench.exe') ? [] : [path.join(appRoot, 'dist-desktop/main.cjs')]), + '--app-root', appRoot, '--data-root', f.data, '--node', process.execPath, '--port', String(port)], env: projectEnvironment({ AGENT_WORKBENCH_QA: '1', AGENT_WORKBENCH_NODE: process.execPath }) }); + page = await app.firstWindow(); window = await app.browserWindow(page); + } else { + host = await launchWorkbench({ after: fn => disposers.push(fn) }, f.data, {}, { appRoot }); assert.notEqual(new URL(host.url).port, '4318'); + browser = await chromium.launch({ channel: 'msedge', headless: true }); page = await browser.newPage(); await page.goto(host.url); + } + page.on('pageerror', error => errors.push(error.message)); + page.on('request', request => { if (/\/sessions\/[^/]+\/messages$/.test(request.url())) launches.push(request.url()); }); + await page.waitForLoadState('networkidle'); await resize(1440); await openRecord(); + await contains('主任务'); await contains('用户消息实例'); await contains('首次变化位置'); await contains('上游未报告'); + await panel().locator('summary').click(); + await until(async () => await panel().getByLabel('本轮工具可见性').isVisible()); + const toolText = await panel().getByLabel('本轮工具可见性').innerText(); + assert.ok(toolText.includes('list_files') && toolText.includes('本轮可见') && toolText.includes('缺少当前任务资源') && toolText.includes('schema')); + await shot('main-tools-1440'); mark('Main request explains tool visibility, missing resources, generation, changed message and unknown cache.'); + await page.getByLabel('选择运行记录', { exact: true }).selectOption(runs[1]); await contains('独立诊断'); + await shot('diagnostic-1440'); mark('Historical diagnostic is identified separately from the main path.'); + await page.getByLabel('选择运行记录', { exact: true }).selectOption(runs[0]); + await page.getByLabel('选择模型请求', { exact: true }).selectOption({ index: 0 }); + await contains('首次请求或新路线'); + await panel().getByRole('button', { name: '查看发送内容', exact: true }).click(); + const artifact = page.getByRole('dialog', { name: '执行结果工件', exact: true }); await artifact.waitFor({ state: 'visible' }); + const content = await artifact.locator('pre').innerText(); assert.doesNotMatch(content, /requestObservation|schemaSha256|generation/); + await page.keyboard.press('Escape'); mark('Historical request selector opens original wire without injecting observations or executing tools.'); + await page.keyboard.press('Escape'); await page.reload(); await page.waitForLoadState('networkidle'); await openRecord(); + await contains('主任务'); await contains('用户消息实例'); await resize(390); await openRecord(); + await panel().locator('summary').click(); await panel().locator('summary').scrollIntoViewIfNeeded(); + assert.ok(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth + 1)); + await shot('main-tools-390'); mark('Reload retains records; 390px expanded tool details fit the existing task panel.'); + assert.deepEqual(errors, []); assert.equal(launches.length, 0); + result = { success: true, desktop, appRoot, marks, screenshots, errors, fixtureModelRequests: model.requests.length, uiModelRequests: 0, realModelRequests: 0 }; +} catch (error) { if (page) await shot('failure').catch(() => {}); result = { success: false, desktop, error: error.stack, marks, screenshots, errors }; } +finally { await app?.close(); await browser?.close(); await controller?.stop(); for (const dispose of disposers.reverse()) await dispose(); fs.writeFileSync(path.join(directory, 'result.json'), JSON.stringify(result, null, 2)); } +console.log(JSON.stringify({ directory, ...result })); if (!result.success) process.exitCode = 1; diff --git a/src/client/RequestContextPanel.tsx b/src/client/RequestContextPanel.tsx index 147794b6..368db360 100644 --- a/src/client/RequestContextPanel.tsx +++ b/src/client/RequestContextPanel.tsx @@ -6,6 +6,10 @@ import type { ContextHistory, ContextHistoryGroup, ContextPlan, Session } from ' const number = (value: number | null | undefined) => (value ?? 0).toLocaleString('zh-CN'); const states: Record = { prepared: '已准备', requesting: '正在请求', completed: '已收到响应', failed: '请求失败', interrupted: '请求已中断' }; +const toolReasons: Record = { visible: '本轮可见', not_assembled: '未装配', missing_current_resource: '缺少当前任务资源', disabled: '已停用', + disabled_by_policy: '当前策略未启用', disabled_by_execution_mode: '当前执行方式未启用', disabled_by_worker_contract: '子任务范围外', disabled_by_task_contract: '当前任务约定不执行', not_assembled_for_worker: '子任务未装配' }; +const changeLabel = (path: string) => path === 'tools' ? '工具定义' : path === 'model' ? '请求型号' : path === 'system' ? '系统规则' + : path.startsWith('messages[') || path.startsWith('input[') ? '消息段 ' + (Number(path.match(/\[(\d+)\]/)?.[1]) + 1) : path; const toolNames: Record = { read_file: '读取文件', read_artifact: '读取保存工件', inspect_project: '识别工程', inspect_session: '核对运行记录', index_project: '建立符号索引', query_code: '定位代码', propose_patch: '精确补丁提案', propose_file: '文件提案', run_command: '终端命令', run_checks: '工程检查', plan_checks: '检查计划', prepare_change_set: '封存变更集', plan_delivery: 'Git交付预览', delivery_report: '交付报告', list_files: '查看文件', search_files: '搜索文件' }; @@ -28,6 +32,8 @@ export function RequestContextPanel({ session, runId, openArtifact, onError }: { : historical.filter(plan => plan.runId === runId && plan.sessionId === session.id); const plan = plans.find(plan => plan.id === selected) ?? plans.at(-1); if (!plan) return null; + const observation = plan.requestObservation, surface = observation?.toolSurface; + const userMessage = observation?.userMessageInstances.filter(message => message.origin === 'user').at(-1); const total = plan.estimatedInputTokens + (plan.budget.outputReservation ?? 0) + plan.budget.safetyTokens; return

请求上下文

{states[plan.status] ?? plan.status}
@@ -40,11 +46,21 @@ export function RequestContextPanel({ session, runId, openArtifact, onError }: {
历史记录
保留 {plan.retainedMessages} 条 · 保存 {plan.archivedGroups} 组原记录
上游缓存命中
{plan.actualUsage?.prompt_tokens_details?.cached_tokens === undefined ? '上游未报告' : number(plan.actualUsage.prompt_tokens_details.cached_tokens) + ' tokens'}
{plan.prefixAudit && <>
缓存前缀核对
{plan.prefixAudit.variants.map((variant, index) =>
{variant.model} · {variant.previousPromptBytes === null ? '首次请求或新路线' : '共同前缀 ' + number(variant.commonPrefixBytes) + ' B'}{variant.reason === 'tool_surface_changed' ? ' · 可用工具已变化' : ''}
)}共同前缀用于核对稳定性,不等于上游命中。
} + {observation && <>
请求归属
{({ main: '主任务', diagnostic: '独立诊断', worker: '子任务' } as Record)[observation.path.kind] ?? observation.path.kind}
} + {userMessage && <>
用户消息实例
{userMessage.id.slice(0, 12)}同样的文字分别记录,不按文本合并。
} + {plan.prefixAudit?.variants.some(variant => variant.firstChange && variant.previousPromptBytes !== null) && <>
首次变化位置
{plan.prefixAudit.variants.filter(variant => variant.previousPromptBytes !== null).map((variant, index) =>
{variant.model} · {variant.firstChange ? changeLabel(variant.firstChange.path) : '前缀内容相同'}
)}
} {!!plan.workingSetEntries && <>
当前任务依据
{plan.workingSetEntries} 项代码与执行结果
} {!!plan.projectMemoryEntries && <>
项目记忆
{plan.projectMemoryEntries} 条来源已核对的记录
} {!!plan.skillCatalogEntries && <>
技能目录
{plan.skillCatalogEntries} 项本次目录记录
}
发送内容
{number(plan.requestBytes)} B{plan.requestSha256.slice(0, 16)}
+ {surface &&
本轮工具 · 可见 {surface.entries.filter(entry => entry.visible).length} / 已登记 {surface.entries.length} +

这是本次实际发送的工具范围。资源和权限在执行时重新核对;登记不代表本轮可用。

+
    {surface.entries.map(entry =>
  • +
    {entry.name}{toolReasons[entry.reason] ?? entry.reason}
    + {number(entry.definitionBytes)} B · schema {entry.schemaSha256.slice(0, 12)} · 代次 {entry.generation.slice(0, 8)} +
  • )}
+
}

估算包含系统提示、工具定义和历史。{plan.budget.capacityMode === 'passthrough' ? '本次完整传递历史,不按本地上下文额度裁剪或拒发。' : plan.historyPolicy === 'preserve' ? '本次完整传递历史,容量不足时暂停。' : '任务要求完整保留;整理过的原记录可从本机工件检索。'}实际用量与缓存命中以模型服务为准。

{plan.error &&

{plan.error}

}
diff --git a/src/client/TaskPanel.css b/src/client/TaskPanel.css index 0fc72977..eafd1946 100644 --- a/src/client/TaskPanel.css +++ b/src/client/TaskPanel.css @@ -77,6 +77,14 @@ .context-meter { height: 5px; margin: 15px 0; border-radius: 9px; overflow: hidden; background: #e8eeeb; } .context-meter i { display: block; height: 100%; background: #56877b; border-radius: inherit; } .context-sha { display: block; font-size: 10px; color: var(--muted); margin-top: 4px; overflow-wrap: anywhere; } +.context-tool-surface { margin: 12px 0; border-block: 1px solid var(--border); padding: 10px 0; } +.context-tool-surface summary { cursor: pointer; font-size: 12px; font-weight: 600; } +.context-tool-surface ul { list-style: none; margin: 8px 0 0; padding: 0; max-height: 340px; overflow: auto; } +.context-tool-surface li { padding: 8px 0; border-bottom: 1px solid var(--border); } +.context-tool-surface li > div { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 4px 12px; font-size: 12px; } +.context-tool-surface code { overflow-wrap: anywhere; } +.context-tool-surface li small { display: block; margin-top: 4px; color: var(--muted); font-size: 11px; overflow-wrap: anywhere; } +.request-context .record-fields dd small { display: block; margin-top: 4px; } .context-actions { display: flex; flex-wrap: wrap; gap: 4px 12px; } .context-actions .text-button { padding-left: 0; } .context-history-list { flex: 1; min-height: 0; overflow: auto; padding: 1px; } diff --git a/src/client/types.ts b/src/client/types.ts index 6928904f..52cb17a6 100644 --- a/src/client/types.ts +++ b/src/client/types.ts @@ -185,7 +185,13 @@ export interface ContextPlan { skillCatalogArtifactId?: string | null; skillCatalogSha256?: string | null; skillCatalogEntries?: number; actualUsage: { prompt_tokens?: number; completion_tokens?: number; total_tokens?: number; prompt_tokens_details?: { cached_tokens?: number; cache_creation_tokens?: number } } | null; requestAttempts: number; error?: string; historyPolicy?: 'preserve' | 'compact'; contextLayout?: string; - prefixAudit?: { variants: { connectionId: string | null; model: string; commonPrefixBytes: number; previousPromptBytes: number | null; reason: string; toolNames: string[]; toolsSha256: string }[] }; + prefixAudit?: { schemaVersion: number; previousPlanId: string | null; variants: { connectionId: string | null; model: string; commonPrefixBytes: number; previousPromptBytes: number | null; reason: string; toolNames: string[]; toolsSha256: string; + firstChange?: { path: string; kind: string; sha256: string | null; previousSha256: string | null } | null; + blocks?: { path: string; role: string | null; sha256: string; bytes: number }[]; + }[] }; + requestObservation?: { schemaVersion: number; path: { kind: string; key: string }; providerHistorySha256: string; + userMessageInstances: { id: string; providerIndex: number; origin: string; sha256: string }[]; + toolSurface: { generation: string; execution: string; entries: { name: string; group: string; registered: boolean; assembled: boolean; visible: boolean; reason: string; schemaSha256: string; definitionSha256: string; definitionBytes: number; generation: string }[] } | null }; budget: { executionMode?: 'unrestricted' | 'restricted'; capacityMode?: 'passthrough'; contextLimit: number | null; outputReservation: number | null; safetyTokens: number; inputBudget: number | null; targetInputTokens: number | null; runRemainingTokens: number | null; taskRemainingTokens: number | null }; } diff --git a/src/core/context/prefix-audit.mjs b/src/core/context/prefix-audit.mjs index 3fb5a0ae..86395257 100644 --- a/src/core/context/prefix-audit.mjs +++ b/src/core/context/prefix-audit.mjs @@ -4,11 +4,30 @@ const hash = value => createHash('sha256').update(value).digest('hex'); const variants = body => body.kind === 'model-route-plan' ? body.variants : [{ connectionId: null, protocol: body.input ? 'responses' : Array.isArray(body.system) ? 'anthropic-messages' : 'chat-completions', body }]; const prompt = body => Buffer.from(JSON.stringify({ model: body.model, tools: body.tools ?? [], system: body.system ?? null, messages: body.messages ?? body.input ?? [] })); +const block = (path, value, role = null) => ({ path, role, sha256: hash(JSON.stringify(value)), bytes: Buffer.byteLength(JSON.stringify(value)) }); +const blocks = body => [block('model', body.model), block('tools', body.tools ?? []), block('system', body.system ?? null), + ...(body.messages ?? body.input ?? []).map((message, index) => block((body.input ? 'input' : 'messages') + '[' + index + ']', message, message.role ?? message.type ?? null))]; + +function segmentAudit(body, previous) { + const current = blocks(body), prior = previous ? blocks(previous) : []; + const first = current.findIndex((entry, index) => entry.sha256 !== prior[index]?.sha256); + const index = first >= 0 ? first : prior.length > current.length ? current.length : -1; + return { blocks: current, firstChange: index < 0 ? null : { path: (current[index] ?? prior[index]).path, + kind: !prior[index] ? 'added' : !current[index] ? 'removed' : 'changed', + reason: !previous ? 'first_route_request' : index === 1 ? 'tool_definitions_changed' : index === 0 ? 'model_changed' + : index === 2 ? 'native_system_changed' : 'message_sequence_changed', + previousSha256: prior[index]?.sha256 ?? null, sha256: current[index]?.sha256 ?? null }, + tools: (body.tools ?? []).map(tool => ({ name: tool.function?.name ?? tool.name, + schemaSha256: hash(JSON.stringify(tool.function?.parameters ?? tool.input_schema ?? tool.parameters ?? {})), + definitionSha256: hash(JSON.stringify(tool)), bytes: Buffer.byteLength(JSON.stringify(tool)) })) }; +} + // This measures ordered prompt fields, not a supplier cache hit or saved // tokens. Metadata stays outside the request and cannot invalidate its prefix. -export function prefixAudit(body, previous = null) { +export function prefixAudit(body, previous = null, { schemaVersion = 1 } = {}) { + if (![1, 2].includes(schemaVersion)) throw new Error('未知前缀观测版本。'); const old = previous ? variants(previous) : []; - return { schemaVersion: 1, method: 'ordered-wire-prompt-fields-utf8', upstreamCache: 'unknown', variants: variants(body).map(current => { + return { schemaVersion, method: 'ordered-wire-prompt-fields-utf8', upstreamCache: 'unknown', variants: variants(body).map(current => { const tools = JSON.stringify(current.body.tools ?? []), bytes = prompt(current.body); const prior = old.find(value => value.connectionId === current.connectionId && value.protocol === current.protocol && value.body.model === current.body.model); const previousBytes = prior ? prompt(prior.body) : null; @@ -18,6 +37,7 @@ export function prefixAudit(body, previous = null) { return { connectionId: current.connectionId, protocol: current.protocol, model: current.body.model, toolsSha256: hash(tools), toolBytes: Buffer.byteLength(tools), toolNames: (current.body.tools ?? []).map(tool => tool.function?.name ?? tool.name), promptBytes: bytes.length, previousPromptBytes: previousBytes?.length ?? null, commonPrefixBytes: common, - reason: !prior ? 'new_route_or_first_request' : toolsChanged ? 'tool_surface_changed' : bytes.equals(previousBytes) ? 'same_prompt' : 'prompt_append_or_change' }; + reason: !prior ? 'new_route_or_first_request' : toolsChanged ? 'tool_surface_changed' : bytes.equals(previousBytes) ? 'same_prompt' : 'prompt_append_or_change', + ...(schemaVersion >= 2 ? segmentAudit(current.body, prior?.body) : {}) }; }) }; } diff --git a/src/core/context/request-observation.mjs b/src/core/context/request-observation.mjs new file mode 100644 index 00000000..7009ef18 --- /dev/null +++ b/src/core/context/request-observation.mjs @@ -0,0 +1,19 @@ +import { createHash } from 'node:crypto'; + +const hash = value => createHash('sha256').update(JSON.stringify(value)).digest('hex'); +export const requestPath = run => run.purpose === 'diagnostic' ? { kind: 'diagnostic', key: 'diagnostic:' + run.id } + : run.workerId ? { kind: 'worker', key: 'worker:' + run.workerId } : { kind: 'main', key: 'main' }; +export const providerHistory = (session, run) => run.diagnosticMessages ?? session.providerMessages; + +export function observeRequest(store, session, run, history, toolSurface = null) { + const path = requestPath(run); + const inputs = [...store.core.run.values()].filter(value => value.sessionId === session.id && value.inputMessage + && requestPath(value).key === path.key); + return { schemaVersion: 1, path, providerHistorySha256: hash(history), toolSurface, + userMessageInstances: history.flatMap((message, index) => { + if (message.role !== 'user') return []; + const input = inputs.find(value => value.inputMessage.providerIndex === index)?.inputMessage; + return [{ id: input?.id ?? session.id + ':' + path.key + ':provider:' + index, providerIndex: index, + origin: input?.origin ?? (input ? 'user' : 'unclassified_provider_user'), sha256: hash(message) }]; + }) }; +} diff --git a/src/core/context/request-plan.mjs b/src/core/context/request-plan.mjs index bce25551..a87bb7ae 100644 --- a/src/core/context/request-plan.mjs +++ b/src/core/context/request-plan.mjs @@ -9,6 +9,8 @@ import { mcpCatalogMessage, MCP_LIMITS } from '../tools/mcp-contract.mjs'; import { modelReservation,taskConsumption,verifyModelReservation } from '../../shared/contracts/model-reservation.mjs'; import { fileContinuation } from './read-continuation.mjs'; import { prefixAudit } from './prefix-audit.mjs'; +import { observeRequest, providerHistory, requestPath } from './request-observation.mjs'; +import { verifyToolSurface } from './tool-surface.mjs'; import { isPassthrough } from '../../shared/contracts/model-capacity.mjs'; const hash = value => createHash('sha256').update(typeof value === 'string' ? value : JSON.stringify(value), 'utf8').digest('hex'); @@ -187,10 +189,11 @@ export class RequestPlanner { return { artifact_id: artifact.id, sha256: artifact.sha256, group_sha256: group.sha256, bytes: Buffer.byteLength(text, 'utf8'), chunks: chunks.length }; } - prepare(session, run, config, { system, contextTail = null, tools, stepId, projectMemory = null, skillCatalog = null, mcpCatalog = null, transportReservationTokens = 0,engineStepOnly=false }) { + prepare(session, run, config, { system, contextTail = null, tools, toolSurface = null, stepId, projectMemory = null, skillCatalog = null, mcpCatalog = null, transportReservationTokens = 0,engineStepOnly=false }) { const task = this.store.owned('task', run.taskId, session.id); this.store.owned('run', run.id, session.id); - const groups = historyGroups(session.providerMessages), budget = budgetFor(config, run, task, transportReservationTokens); + const history = providerHistory(session, run); + const groups = historyGroups(history), budget = budgetFor(config, run, task, transportReservationTokens); const inputBudget = budget.inputBudget ?? Infinity, targetInputTokens = budget.targetInputTokens ?? Infinity; const lastUser = groups.findLast(group => group.messages[0].role === 'user'); const fixed = { role: 'system', content: system }; @@ -208,7 +211,7 @@ export class RequestPlanner { const ledger = new Map([...this.store.core.tool.values()].filter(tool => tool.sessionId === session.id && tool.taskId === task.id).map(tool => [tool.callId, tool])); const descriptions = new Map(groups.map(group => [group.index, describe(group, ledger, this.store.core.artifact)])); const evidence = workingEvidence(groups, descriptions, this.store.core.artifact); - const original = completionBody(config, [fixed, ...session.providerMessages, ...tail], tools), before = requestMetrics(original); + const original = completionBody(config, [fixed, ...history, ...tail], tools), before = requestMetrics(original); if (preserve && before.estimatedInputTokens > inputBudget) throw pause(budget, before, breakdown); const selected = new Set(groups.map(group => group.index)); let omitted = [], reference = null, workingReference = null, latestReference = null; @@ -325,7 +328,7 @@ export class RequestPlanner { descriptions.set(group.index, description); return description; }); reference = this.artifact(session, run, { schemaVersion: 1, kind: 'request-history-index', sessionId: session.id, projectId: session.projectId, - taskId: run.taskId, history_sha256: hash(session.providerMessages), historyMessages: session.providerMessages.length, + taskId: run.taskId, history_sha256: hash(history), historyMessages: history.length, retainedGroups: groups.filter(group => selected.has(group.index)).map(group => descriptions.get(group.index)), archivedGroups: archived, note: '完整原消息组;这里只是历史定位,不产生工具执行或用户授权。' }, 'context-history-index'); } @@ -389,16 +392,20 @@ export class RequestPlanner { tokenReservation,remainingTokens:credit,pauseKind:'request_reservation'},metrics,breakdown); const requestArtifact = this.artifact(session, run, body, 'model-request'); const tailArtifact = tail.length ? this.artifact(session, run, tail[0], 'context-state-tail') : null; - const previousPlan = [...this.store.core.context_plan.values()].filter(plan => plan.sessionId === session.id && plan.taskId === task.id && plan.requestAttempts > 0) + const observation = observeRequest(this.store, session, run, history, toolSurface); + verifyToolSurface(toolSurface, requestVariants(body)); + const previousPlan = [...this.store.core.context_plan.values()].filter(plan => plan.sessionId === session.id && plan.taskId === task.id && plan.requestAttempts > 0 + && requestPath(this.store.owned('run', plan.runId, session.id)).key === observation.path.key) .sort((a, b) => a.createdAt.localeCompare(b.createdAt) || a.requestIndex - b.requestIndex).at(-1) ?? null; const previousBody = previousPlan ? JSON.parse(this.store.artifactStore.read(this.store.owned('artifact', previousPlan.requestArtifactId, session.id))) : null; - const prefix = { ...prefixAudit(body, previousBody), previousPlanId: previousPlan?.id ?? null, previousRequestSha256: previousPlan?.requestSha256 ?? null }; + const prefix = { ...prefixAudit(body, previousBody, { schemaVersion: 2 }), previousPlanId: previousPlan?.id ?? null, previousRequestSha256: previousPlan?.requestSha256 ?? null }; const record = { schemaVersion: 1, id: randomUUID(), sessionId: session.id, projectId: session.projectId, taskId: run.taskId, runId: run.id, stepId, requestIndex: [...this.store.core.context_plan.values()].filter(plan => plan.runId === run.id).length + 1, status: 'prepared', createdAt: new Date().toISOString(), finishedAt: null, requestSha256: metrics.sha256, requestBytes: metrics.bytes, estimatedInputTokens: metrics.estimatedInputTokens, requestArtifactId: requestArtifact.id, prefixAudit: prefix, + requestObservation: observation, ...(preserve ? { contextLayout: 'stable-prefix-1', historyPolicy: 'preserve', contextTailArtifactId: tailArtifact?.id ?? null, contextTailSha256: tailArtifact?.sha256 ?? null } : {}), ...(tokenReservation?{tokenReservation,maxBudgetAttempts:Math.min(8,Math.floor(credit/tokenReservation.reservedTokens))}:{engineStepOnly:true}), estimateMethod: metrics.estimateMethod, beforeInputTokens: before.estimatedInputTokens, beforeBytes: before.bytes, budget, @@ -416,7 +423,7 @@ export class RequestPlanner { // sizing reserves that observed framing growth before returning bytes; // the next complete request is still checked against its original limit. readStateGrowthTokens: 1536, - originalMessages: session.providerMessages.length, retainedMessages: messages.length - 1 - tail.length - (reference ? 1 : 0) - (workingReference ? 1 : 0) - (latestReference ? 1 : 0) - (memoryReference ? 1 : 0) - (skillReference ? 1 : 0) - (mcpReference ? 1 : 0), + originalMessages: history.length, retainedMessages: messages.length - 1 - tail.length - (reference ? 1 : 0) - (workingReference ? 1 : 0) - (latestReference ? 1 : 0) - (memoryReference ? 1 : 0) - (skillReference ? 1 : 0) - (mcpReference ? 1 : 0), archivedGroups: omitted.length, retainedGroups: groups.filter(group => selected.has(group.index)).map(group => group.index), historyIndexArtifactId: reference?.id ?? null, historyIndexSha256: reference?.sha256 ?? null, actualUsage: null, workingSetArtifactId: workingReference?.id ?? null, workingSetSha256: workingReference?.sha256 ?? null, workingSetEntries: entries.length, @@ -467,14 +474,21 @@ export function verifyPlanEvidence(store, session, plan, cache = new Map()) { historyGroups(requestHistory(body)); for (const variant of requestVariants(body)) historyGroups(requestHistory(variant)); const frozen = JSON.parse(store.artifactStore.read(get(plan.artifactId, 'context-plan'))); - for (const key of ['contextLayout', 'historyPolicy', 'contextTailArtifactId', 'contextTailSha256', 'prefixAudit']) { + for (const key of ['contextLayout', 'historyPolicy', 'contextTailArtifactId', 'contextTailSha256', 'prefixAudit', 'requestObservation']) { if (JSON.stringify(frozen[key] ?? null) !== JSON.stringify(plan[key] ?? null)) throw new Error('前缀或历史策略与发送前封存不匹配。'); } + if (plan.requestObservation) { + const run = store.owned('run', plan.runId, session.id); + const expected = observeRequest(store, session, run, providerHistory(session, run).slice(0, plan.originalMessages), plan.requestObservation.toolSurface); + if (JSON.stringify(expected) !== JSON.stringify(plan.requestObservation)) throw new Error('消息实例或请求路径与原历史不匹配。'); + verifyToolSurface(plan.requestObservation.toolSurface, requestVariants(body)); + } if (plan.prefixAudit) { const previous = plan.prefixAudit.previousPlanId ? store.owned('context_plan', plan.prefixAudit.previousPlanId, session.id) : null; - if (previous && (previous.taskId !== plan.taskId || previous.id === plan.id || previous.createdAt > plan.createdAt || previous.requestSha256 !== plan.prefixAudit.previousRequestSha256)) throw new Error('前缀对照来源不匹配。'); + if (previous && (previous.taskId !== plan.taskId || previous.id === plan.id || previous.createdAt > plan.createdAt || previous.requestSha256 !== plan.prefixAudit.previousRequestSha256 + || plan.requestObservation && requestPath(store.owned('run', previous.runId, session.id)).key !== plan.requestObservation.path.key)) throw new Error('前缀对照来源不匹配。'); const previousBody = previous ? JSON.parse(store.artifactStore.read(get(previous.requestArtifactId, 'model-request', previous.requestSha256))) : null; - const expected = { ...prefixAudit(body, previousBody), previousPlanId: previous?.id ?? null, previousRequestSha256: previous?.requestSha256 ?? null }; + const expected = { ...prefixAudit(body, previousBody, { schemaVersion: plan.prefixAudit.schemaVersion }), previousPlanId: previous?.id ?? null, previousRequestSha256: previous?.requestSha256 ?? null }; if (JSON.stringify(expected) !== JSON.stringify(plan.prefixAudit)) throw new Error('前缀观测与原请求字节不匹配。'); } const carriesContext = content => requestHistory(body).filter(value => typeof value.content === 'string' @@ -565,7 +579,7 @@ export function verifyPlanEvidence(store, session, plan, cache = new Map()) { const artifact = get(plan.latestToolsArtifactId, 'context-latest-tools', plan.latestToolsSha256); const document = JSON.parse(store.artifactStore.read(artifact)); if (artifact.taskId !== plan.taskId) throw new Error('最新工具反馈的工件任务归属不匹配。'); - verifyLatestToolDocument(store, session, plan, document, historyGroups(session.providerMessages.slice(0, plan.originalMessages))); + verifyLatestToolDocument(store, session, plan, document, historyGroups(providerHistory(session, store.owned('run', plan.runId, session.id)).slice(0, plan.originalMessages))); const message = latestToolMessage(artifact, document, plan.latestToolsViewVersion ?? 1); if (requestHistory(body).filter(value => value.role === 'system' && value.content === message.content).length !== 1) throw new Error('最新工具反馈与实际发送内容不匹配。'); } @@ -573,7 +587,7 @@ export function verifyPlanEvidence(store, session, plan, cache = new Map()) { const artifact = get(plan.workingSetArtifactId, 'context-working-set', plan.workingSetSha256); const document = JSON.parse(store.artifactStore.read(artifact)); if (artifact.taskId !== plan.taskId) throw new Error('工作依据工件任务归属不匹配。'); - verifyWorkingDocument(store, session, plan, document, historyGroups(session.providerMessages.slice(0, plan.originalMessages))); + verifyWorkingDocument(store, session, plan, document, historyGroups(providerHistory(session, store.owned('run', plan.runId, session.id)).slice(0, plan.originalMessages))); const message = workingMessage(artifact, document, plan.workingSetViewVersion ?? 1); if (requestHistory(body).filter(value => value.role === 'system' && value.content === message.content).length !== 1) throw new Error('工作依据与实际发送内容不匹配。'); } diff --git a/src/core/context/tool-surface.mjs b/src/core/context/tool-surface.mjs new file mode 100644 index 00000000..1f51beee --- /dev/null +++ b/src/core/context/tool-surface.mjs @@ -0,0 +1,40 @@ +import { createHash } from 'node:crypto'; + +const hash = value => createHash('sha256').update(JSON.stringify(value)).digest('hex'); +const bytes = value => Buffer.byteLength(JSON.stringify(value)); +export const toolName = tool => tool.function?.name ?? tool.name; + +// Observations only. Resource managers still recheck current authority at +// execution; registration and model visibility do not grant that authority. +export function observeToolSurface(groups, tools, policy = {}) { + const sent = new Map(tools.map(tool => [toolName(tool), tool])); + const entries = groups.flatMap(group => group.definitions.map(definition => { + const name = toolName(definition), actual = sent.get(name); + const reason = actual ? 'visible' : !group.assembled ? group.reason ?? 'not_assembled' + : policy.reason?.(name) ?? 'disabled_by_policy'; + const identity = actual ?? definition; + return { name, group: group.name, registered: true, assembled: !!group.assembled, visible: !!actual, + reason, schemaSha256: hash(identity.function?.parameters ?? identity.input_schema ?? {}), + definitionSha256: hash(identity), definitionBytes: bytes(identity), + generation: hash({ definition: identity, reason, resources: group.resources ?? [], policy: policy.identity ?? null }) }; + })); + const observation = { schemaVersion: 1, method: 'assembled-tool-definitions-and-resource-revisions', + execution: 'rechecked_at_execution', policy: policy.identity ?? null, + resources: groups.map(group => ({ group: group.name, identities: group.resources ?? [] })), entries }; + return { ...observation, generation: hash(observation) }; +} + +export function verifyToolSurface(surface, bodyVariants) { + if (!surface) return; + const { generation, ...observation } = surface; + if (surface.schemaVersion !== 1 || hash(observation) !== generation) throw new Error('工具可见性代次不匹配。'); + const expected = surface.entries.filter(entry => entry.visible); + if (new Set(surface.entries.map(entry => entry.name)).size !== surface.entries.length) throw new Error('工具可见性有重复名称。'); + for (const body of bodyVariants) { + const actual = body.tools ?? []; + if (actual.length !== expected.length || actual.some((tool, index) => toolName(tool) !== expected[index].name + || hash(tool.function?.parameters ?? tool.input_schema ?? tool.parameters ?? {}) !== expected[index].schemaSha256)) { + throw new Error('工具可见性与实际协议工具不匹配。'); + } + } +} diff --git a/src/server/diagnostics.mjs b/src/server/diagnostics.mjs index 84cfaafd..f0f4e5da 100644 --- a/src/server/diagnostics.mjs +++ b/src/server/diagnostics.mjs @@ -1,6 +1,6 @@ import { randomUUID, createHash } from 'node:crypto'; -export const APP_VERSION = '1.25.7'; +export const APP_VERSION = '1.25.8'; export function executionEnvironment() { const windows = process.platform === 'win32'; diff --git a/src/server/runtime.mjs b/src/server/runtime.mjs index b916badf..88d2d493 100644 --- a/src/server/runtime.mjs +++ b/src/server/runtime.mjs @@ -30,6 +30,8 @@ import { projectEnvironment } from '../core/process-environment.mjs'; import { GitDelivery } from '../core/git-delivery.mjs'; import { ProjectIndex } from '../core/context/project-index.mjs'; import { RequestPlanner, verifyPlanEvidence } from '../core/context/request-plan.mjs'; +import { observeToolSurface } from '../core/context/tool-surface.mjs'; +import { providerHistory } from '../core/context/request-observation.mjs'; import { ENGINE_SEED_RESERVATION } from '../shared/contracts/engine-context.mjs'; import { InputAttachments } from '../core/context/input-attachments.mjs'; import { BudgetResume } from '../core/context/budget-resume.mjs'; @@ -291,6 +293,7 @@ export class Runtime { if (attachments.length && config.mode==='demo') throw Object.assign(new Error('演示执行器不会读取图片,请先选择支持视觉的模型连接。'),{status:409}); if (!unrestricted(config) && attachments.length && !config.modelRouting) throw Object.assign(new Error('请在模型中心添加并声明支持视觉的连接,再发送图片。'),{status:409}); const record = this.store.beginRun(session, content, resume, config, { instructionSource, inputAttachments:attachments,resumePlan }); + if (record.purpose === 'diagnostic') record.diagnosticMessages = structuredClone(session.providerMessages); const run = { controller, id: record.id, taskId: record.taskId, project, record, config, ...(worker ? { worker } : {}) }; this.active.set(id, run); session.status = 'running'; session.recoveryNotice = null; session.executionMode = config.mode; @@ -299,7 +302,9 @@ export class Runtime { if (!resume || content) { const userMessage=this.store.message(session, 'user', String(content)); if(attachments.length)userMessage.attachmentIds=attachments.map(row=>row.boundAttachmentId); - session.providerMessages.push({ role: 'user', content: String(content) }); + const history = providerHistory(session, record); + record.inputMessage = { id: userMessage.id, providerIndex: history.length, origin: instructionSource ? 'system_action' : 'user' }; + history.push({ role: 'user', content: String(content) }); } if (resume) this.store.message(session, 'system', '从已保存的执行记录继续,已完成的工具结果保留。'); session.failure = null; @@ -412,15 +417,38 @@ export class Runtime { }) : '')); const skillCatalog = run.worker ? null : this.skillRegistry().catalog(session.projectId); const mcpCatalog = run.worker ? null : this.mcpRegistry().catalog(session.projectId); - const availableTools = [...toolDefinitions, ...(unrestricted(config) ? automaticToolDefinitions : []), ...taskPlanToolDefinitions, ...fileResultToolDefinitions, ...(unrestricted(config) || [...this.store.core.browsing_tab.values()].some(tab=>tab.sessionId===session.id&&tab.taskId===run.taskId&&tab.status==='open')?browsingToolDefinitions:[]), ...(skillCatalog?.entries.some(value => value.status === 'current') ? skillToolDefinitions : []), - ...(mcpCatalog?.entries.length ? mcpToolDefinitions : []), - ...([...this.store.core.research_target.values()].some(target => target.sessionId === session.id && target.taskId === run.taskId) ? researchToolDefinitions : []), - ...([...this.store.core.engine_job.values()].some(job => job.sessionId === session.id && job.taskId === run.taskId) ? engineToolDefinitions : []), - ...(this.browserManager().builds(run.project).length || [...this.store.core.browser_plan.values()].some(plan => plan.sessionId === session.id && plan.taskId === run.taskId) ? browserToolDefinitions : [])]; + const owned = kind => [...this.store.core[kind].values()].filter(value => value.sessionId === session.id && value.taskId === run.taskId); + const tabs = owned('browsing_tab').filter(tab => tab.status === 'open'), targets = owned('research_target'), engines = owned('engine_job'); + const browserResources = [...this.browserManager().builds(run.project).map(value => ({ kind: 'build', path: value })), + ...owned('browser_plan').map(value => ({ kind: 'plan', id: value.id }))]; + const registrations = registry => registry.state().registrations.filter(value => value.projectId === session.projectId) + .map(({ id, enabled, revision, configurationRevision }) => ({ id, enabled, revision, configurationRevision })); + const skills = run.worker ? [] : registrations(this.skillRegistry()), mcps = run.worker ? [] : registrations(this.mcpRegistry()); + const groups = [ + { name: 'core', definitions: toolDefinitions, assembled: true }, + { name: 'automatic', definitions: automaticToolDefinitions, assembled: unrestricted(config), reason: 'disabled_by_execution_mode' }, + { name: 'task_plan', definitions: taskPlanToolDefinitions, assembled: true }, + { name: 'file_results', definitions: fileResultToolDefinitions, assembled: true }, + { name: 'browsing', definitions: browsingToolDefinitions, assembled: unrestricted(config) || !!tabs.length, reason: 'missing_current_resource', resources: tabs.map(({ id, revision }) => ({ id, revision })) }, + { name: 'skills', definitions: skillToolDefinitions, assembled: !!skillCatalog?.entries.some(value => value.status === 'current'), + reason: run.worker ? 'not_assembled_for_worker' : skills.length && !skills.some(value => value.enabled) ? 'disabled' : 'missing_current_resource', + resources: [{ registrations: skills, entries: skillCatalog?.entries ?? [] }] }, + { name: 'mcp', definitions: mcpToolDefinitions, assembled: !!mcpCatalog?.entries.length, + reason: run.worker ? 'not_assembled_for_worker' : mcps.length && !mcps.some(value => value.enabled) ? 'disabled' : 'missing_current_resource', + resources: [{ registrations: mcps, entries: mcpCatalog?.entries ?? [] }] }, + { name: 'research', definitions: researchToolDefinitions, assembled: !!targets.length, reason: 'missing_current_resource', resources: targets.map(({ id }) => ({ id })) }, + { name: 'engine', definitions: engineToolDefinitions, assembled: !!engines.length, reason: 'missing_current_resource', resources: engines.map(({ id }) => ({ id })) }, + { name: 'browser_checks', definitions: browserToolDefinitions, assembled: !!browserResources.length, reason: 'missing_current_resource', resources: browserResources }, + ]; + const availableTools = groups.filter(group => group.assembled).flatMap(group => group.definitions); const tools = openToolDefinitions(availableTools.filter(tool => unrestricted(config) || (!run.worker || WORKER_TOOLS.has(tool.function.name)) && taskToolAllowed(task, run.record, tool.function.name)), config); + const toolSurface = observeToolSurface(groups, tools, { identity: { executionMode: config.executionMode ?? 'restricted', approvalMode: config.approvalMode ?? 'ask', + purpose: run.record.purpose ?? 'execution', workerId: run.worker?.id ?? null }, + reason: name => run.worker && !WORKER_TOOLS.has(name) ? 'disabled_by_worker_contract' + : !taskToolAllowed(task, run.record, name) ? 'disabled_by_task_contract' : 'disabled_by_execution_mode' }); const projectMemory = await this.memoryManager().context(session, { redact: value => this.provider.redact(value) }); signal.throwIfAborted(); - const planned = this.requestPlanner().prepare(session, run.record, config, { system: stablePrefix ? this.provider.redact(fixedContext) : context, contextTail: stablePrefix ? context : null, tools, stepId: stepRecord.id, projectMemory, skillCatalog, mcpCatalog, + const planned = this.requestPlanner().prepare(session, run.record, config, { system: stablePrefix ? this.provider.redact(fixedContext) : context, contextTail: stablePrefix ? context : null, tools, toolSurface, stepId: stepRecord.id, projectMemory, skillCatalog, mcpCatalog, transportReservationTokens:run.worker?.engine?ENGINE_SEED_RESERVATION:0,engineStepOnly:!!run.worker?.engine }); if (planned.record.archivedGroups && !projectionLogged) { projectionLogged = true; session.compactions++; @@ -484,7 +512,7 @@ export class Runtime { response = externalEngine ? await this.engineRunner().complete(session, run, planned.messages, progressHandler, planned.record, tools) : await this.provider.complete(requestConfig, planned.messages, run.project, signal, progressHandler, planned.record, tools, - { ...(config.mode === 'demo' ? { demoHistory: session.providerMessages } : {}), maxBudgetAttempts:planned.record.maxBudgetAttempts,beforeDispatch:({previousFailure}={})=>checkModelDispatchBudget(this.store,session,run.record,requestConfig,planned.body,0,previousFailure)}); + { ...(config.mode === 'demo' ? { demoHistory: providerHistory(session, run.record) } : {}), maxBudgetAttempts:planned.record.maxBudgetAttempts,beforeDispatch:({previousFailure}={})=>checkModelDispatchBudget(this.store,session,run.record,requestConfig,planned.body,0,previousFailure)}); if (response.compatibility?.length && requestEvent) { requestEvent.compatibility = response.compatibility; this.store.save(session); } this.requestPlanner().finish(session, planned.record, { response, attempts: planned.record.requestAttempts }); if (run.worker && !externalEngine && !responseAccounted) { @@ -518,7 +546,7 @@ export class Runtime { // The truncated assistant turn was never appended, so the conversation // stays valid. Feed the instruction to the model as a standalone turn // in the provider history, and mirror it to the visible chat log. - session.providerMessages.push({ role: 'user', content: note }); + providerHistory(session, run.record).push({ role: 'user', content: note }); this.store.event(session, { kind: 'context', runId: run.id, label: '模型输出被上限截断,已请求拆分后继续', phase: 'completed', contextPlanId: planned.record.id, output: '第 ' + run.record.outputTruncationRecoveries + '/' + OUTPUT_TRUNCATION_RECOVERY_LIMIT + ' 次恢复:上一响应 finish_reason=length,未执行任何工具,原响应与用量已保存。' }); this.store.message(session, 'system', note); @@ -545,9 +573,9 @@ export class Runtime { : this.store.owned('artifact', args.artifact_id, session.id).bytes; return Math.max(0, Math.min(size - (args.offset ?? 0), args.limit ?? (call.function.name === 'read_artifact' ? 2000 : Infinity))); }); - const previousIds = new Set(session.providerMessages.flatMap(item => (item.tool_calls ?? []).map(call => call.id))); + const previousIds = new Set(providerHistory(session, run.record).flatMap(item => (item.tool_calls ?? []).map(call => call.id))); if (new Set(calls.map(call => call.id)).size !== calls.length || calls.some(call => previousIds.has(call.id))) throw new Error('模型返回了重复的工具调用 ID,请重新发送任务。'); - session.providerMessages.push(message); + providerHistory(session, run.record).push(message); if (!responseAccounted) this.store.recordResponse(session, run.record, response.usage, planned.record.estimatedInputTokens + Math.ceil(Buffer.byteLength(JSON.stringify(message), 'utf8') / 3) + 8); for (const call of calls) { const event = { id: randomUUID(), createdAt: now(), startedAt: now(), kind: 'tool', phase: 'queued', @@ -621,7 +649,7 @@ export class Runtime { const invocation = [...this.store.core.tool.values()].findLast(row => row.runId === run.id && row.callId === call.id); if (invocation?.resultArtifactId) (stepRecord.progressToolIds ??= []).push(invocation.id); spendReadBudget(run.readBatchBudget, call, result); - session.providerMessages.push({ role: 'tool', tool_call_id: call.id, content: JSON.stringify(result) }); + providerHistory(session, run.record).push({ role: 'tool', tool_call_id: call.id, content: JSON.stringify(result) }); if (call.function.name === 'prepare_change_set' && result.status === 'awaiting_review') preparedSets.add(result.change_set_id); if (call.function.name === 'call_registered_mcp' && result.requires_verification) mcpReview = result.mcp_job_id; if (call.function.name === 'plan_delivery' && result.status === 'awaiting_review') { @@ -674,8 +702,8 @@ export class Runtime { this.store.save(session); } catch (error) { this.store.interruptPending(session, run.record); - const repaired = repairToolPairs(session.providerMessages, callId => this.store.savedToolResult(session.id, callId)); - if (repaired.changed) session.providerMessages = repaired.messages; + const history = providerHistory(session, run.record), repaired = repairToolPairs(history, callId => this.store.savedToolResult(session.id, callId)); + if (repaired.changed) history.splice(0, history.length, ...repaired.messages); const budget = error instanceof BudgetExceeded ? error : signal.reason instanceof BudgetExceeded ? signal.reason : null; if (signal.aborted || budget) { run.stopReason = budget?.reason ?? 'cancelled'; diff --git a/task_plan.md b/task_plan.md index 3dacdd4e..42158fde 100644 --- a/task_plan.md +++ b/task_plan.md @@ -1,5 +1,17 @@ # Agent Workbench:核心代理能力研究与产品计划 +## 当前接续:C 工具可见性、前缀证据与新真实验证(进行中) + +基线 b14e26e6,feat/task-agreement-1.25,起步工作树干净。正式 1.25.7 / PID71628;用户当前选择「高性能」池 revision8,保留此新配置,不沿用旧 DeepSeek 单线默认。用户授权开始 C → 新供应商预检 → 新 Node → 有条件 React/Python/Purge;不续跑历史样本或用户项目、不委派、不推送、不操作真实 Key。 + +- [complete] C:版本化记录实际工具 schema/字节/可见或缺席原因/generation、请求稳定块/首次变化段/用户实例/主侧路径;诊断独立历史,默认透传不变,旧证据按原版本核对。 +- [in_progress] C:MCP/Skills与任务相关原轮40/41、新专项修正后4/4、网页/Electron各4组和620份历史U只读核验通过。最终构建/发行/隔离安装/正式升级,回顾并本地提交。 +- [pending] 另冻新真实兼容批次:当前有凭据连接的目录/诊断/单请求身份预检;保留每次上游原错误,不重试旧批次,不把短答通过当工程能力。 +- [pending] 使用通过连接另冻新独立 Node 工程,默认透传和用户持久权限,模型独立修改/原检查/本地 Git;按原始事实报告结果与失败。 +- [pending] 仅 Node 闭环后推进 React/Python 与原版 Purge AB/BA;前置未满足时明确未执行、收益 unknown。 + +历史网络异常、未交付、重复读取普遍改善/缓存收益/真实预留排除边界/Purge 未验证继续保留;下文为历史阶段。 + ## 当前:默认透传、三种操作权限与可回滚自动执行(已本地交付) 用户最新要求覆盖旧的默认限制与独立审查停顿。额度与操作权限分离:新安装完整历史透传、零额度表示不限,权限首次为“请求批准”,改选后持久保存且运行中可切换。文件和本地Git不再因独立差异审查停下;保存原字节与Git基线并提供回滚。完全访问以宿主用户权限执行命令、程序和浏览器操作,不冒称已有原生鼠标键盘工具。 diff --git a/tests/task-control.test.mjs b/tests/task-control.test.mjs index f46febff..8bf29fd4 100644 --- a/tests/task-control.test.mjs +++ b/tests/task-control.test.mjs @@ -87,15 +87,17 @@ test('diagnostic answers retain the unfinished task, limits and check result, an const task = f.store.core.task.get(f.session.taskId), taskId = task.id; task.verification = { status: 'failed', checkJobId: null }; f.session.status = 'paused'; f.store.save(f.session); const instructions = structuredClone(task.instructions); + const mainHistory = structuredClone(f.session.providerMessages); f.runtime.start(f.session.id, '前面的错误是怎么回事?'); await f.wait(); assert.equal(f.session.taskId, taskId); assert.equal(task.status, 'paused'); assert.equal(f.session.status, 'paused'); assert.deepEqual(task.instructions, instructions); assert.equal(task.verification.status, 'failed'); const run = f.store.core.run.get(f.session.runId); assert.equal(run.purpose, 'diagnostic'); assert.equal(run.status, 'completed'); assert.equal(run.stopReason, 'diagnostic_answered'); assert.equal(f.session.changes.length, 0); assert.ok(!fs.existsSync(path.join(f.projectRoot, 'unwanted.txt'))); - const denied = JSON.parse(f.session.providerMessages.find(message => message.tool_call_id === 'bad-diagnostic-write').content); + assert.deepEqual(f.session.providerMessages, mainHistory, 'Diagnostic exchanges stay outside the main model history.'); + const denied = JSON.parse(run.diagnosticMessages.find(message => message.tool_call_id === 'bad-diagnostic-write').content); assert.equal(denied.status, 'error'); assert.match(denied.message, /只读/); - const inspected = JSON.parse(f.session.providerMessages.find(message => message.tool_call_id === 'inspect-error').content); + const inspected = JSON.parse(run.diagnosticMessages.find(message => message.tool_call_id === 'inspect-error').content); assert.equal(inspected.diagnosticContext.evidenceVerified, true); assert.equal(inspected.diagnosticContext.taskId, taskId); f.runtime.start(f.session.id, '继续完成'); await f.wait(); assert.equal(f.session.taskId, taskId); assert.equal(f.session.status, 'paused'); diff --git a/tests/tool-prefix-observation.test.mjs b/tests/tool-prefix-observation.test.mjs new file mode 100644 index 00000000..b5bc97c9 --- /dev/null +++ b/tests/tool-prefix-observation.test.mjs @@ -0,0 +1,100 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { randomUUID } from 'node:crypto'; +import { fixture, compatibleFixture, completion, call } from './helpers.mjs'; +import { Provider, toolDefinitions } from '../src/server/provider.mjs'; +import { Runtime } from '../src/server/runtime.mjs'; +import { Store } from '../src/server/store.mjs'; +import { RequestPlanner, verifyPlanEvidence } from '../src/core/context/request-plan.mjs'; +import { completionBody, requestVariants } from '../src/shared/contracts/model-request.mjs'; +import { prefixAudit } from '../src/core/context/prefix-audit.mjs'; +import { observeToolSurface, verifyToolSurface } from '../src/core/context/tool-surface.mjs'; + +test('protocol-adapted tool definitions, semantic changes and raw prompt bytes are independent of diagnostic metadata', () => { + const tools = toolDefinitions.slice(0, 2), groups = [{ name: 'core', definitions: tools, assembled: true }]; + const surface = observeToolSurface(groups, tools); + for (const protocol of ['chat-completions', 'responses', 'anthropic-messages']) { + const config = { protocol, model: 'owned', maxOutputTokens: 32768, capacityMode: 'passthrough' }; + const first = completionBody(config, [{ role: 'system', content: '稳定规则🙂' }, { role: 'user', content: '原目标' }], tools); + const second = completionBody(config, [{ role: 'system', content: '稳定规则🙂' }, { role: 'user', content: '原目标' }, { role: 'assistant', content: '原结果' }, { role: 'user', content: '继续' }], tools); + const original = JSON.stringify(second), audit = prefixAudit(second, first, { schemaVersion: 2 }).variants[0]; + assert.doesNotThrow(() => verifyToolSurface(surface, requestVariants(second))); + assert.equal(audit.tools.length, tools.length); assert.ok(audit.tools.every(value => value.bytes > 0 && /^[a-f0-9]{64}$/.test(value.schemaSha256))); + assert.equal(audit.firstChange.kind, 'added'); assert.match(audit.firstChange.path, /^(?:messages|input)\[/); + assert.ok(audit.commonPrefixBytes > 40); assert.equal(audit.blocks[0].path, 'model'); assert.equal(audit.blocks[1].path, 'tools'); + const changed = prefixAudit({ ...second, tools: [] }, first, { schemaVersion: 2 }).variants[0]; + assert.equal(changed.firstChange.path, 'tools'); assert.equal(changed.reason, 'tool_surface_changed'); + assert.equal(JSON.stringify(second), original); assert.doesNotMatch(original, /generation|schemaSha256|requestObservation/); + } +}); + +test('actual runtime observes new browser resources, missing resources and distinct repeated user instances; old configuration stays frozen', async t => { + const f = fixture(t); let firstRound = true; + const server = await compatibleFixture(t, () => { + if (firstRound) { + firstRound = false; + const tabId = randomUUID(); + f.store.core.browsing_tab.set(tabId, { id: tabId, projectId: f.project.id, sessionId: f.session.id, taskId: f.session.taskId, + runId: f.session.runId, createdAt: new Date().toISOString(), status: 'open', revision: 1, + windowId: 'owned-window', profileId: 'owned-profile', origins: [], url: 'http://127.0.0.1/', grantRevision: 'owned-grant' }); + return completion([call('one-list', 'list_files', { path: '' })]); + } + return completion([], '所属读取已完成。'); + }); + const provider = new Provider(); provider.setKey('owned-test-key'); + f.store.saveConfig({ mode: 'compatible', baseUrl: server.baseUrl, model: 'owned', contextLimit: 64000 }); + const runtime = new Runtime(f.store, provider); f.onCleanup(() => runtime.stopAll()); + const run = async content => { runtime.start(f.session.id, content); await runtime.active.get(f.session.id).promise; }; + await run('只读取一次目录然后回答,不修改文件,不执行工程检查。'); + const plans = [...f.store.core.context_plan.values()]; assert.equal(plans.length, 2, JSON.stringify(f.session.lastFailure)); + const [one, two] = plans.map(plan => plan.requestObservation.toolSurface); + assert.equal(one.entries.find(tool => tool.name === 'observe_browser_tab').reason, 'missing_current_resource'); + assert.equal(two.entries.find(tool => tool.name === 'observe_browser_tab').reason, 'visible'); + assert.notEqual(one.generation, two.generation); + assert.equal(plans[1].prefixAudit.variants[0].firstChange.path, 'tools'); + assert.equal(one.entries.find(tool => tool.name === 'read_registered_skill').reason, 'missing_current_resource'); + const oldArtifact = f.store.artifactStore.read(f.store.core.artifact.get(plans[0].artifactId)); + f.store.saveConfig({ model: 'owned-new-setting' }); + await run('继续'); await run('继续'); + const all = [...f.store.core.context_plan.values()]; + const instances = all.at(-1).requestObservation.userMessageInstances.filter(value => value.origin === 'user'); + assert.equal(instances.length, 3); assert.notEqual(instances[1].id, instances[2].id); assert.equal(instances[1].sha256, instances[2].sha256); + assert.equal(all.at(-1).prefixAudit.previousPlanId, all.at(-2).id); + assert.equal(f.store.artifactStore.read(f.store.core.artifact.get(plans[0].artifactId)), oldArtifact); + for (const plan of all) assert.doesNotThrow(() => verifyPlanEvidence(f.store, f.session, plan)); + assert.equal(server.requests.length, 4); +}); + +test('diagnostic request history and prefix predecessor remain separate from the main path across restart', async t => { + const f = fixture(t, { modernDefaults: true }); + const server = await compatibleFixture(t, () => completion([], '原事实答复。')); + const provider = new Provider(), hub = provider.modelHub(f.store); + const connection = hub.upsert({ name: 'Owned', model: 'owned', baseUrl: server.baseUrl, requiresKey: false }); hub.select({ type: 'connection', id: connection.id }); + const runtime = new Runtime(f.store, provider); f.onCleanup(() => runtime.stopAll()); + const run = async content => { runtime.start(f.session.id, content); await runtime.active.get(f.session.id).promise; }; + await run('读取原项目说明并回答;不修改文件,不执行检查。'); + const main = structuredClone(f.session.providerMessages), first = [...f.store.core.context_plan.values()].at(-1); + await run('前面的错误是怎么回事?'); + const diagnostic = [...f.store.core.context_plan.values()].at(-1); + assert.equal(diagnostic.requestObservation.path.kind, 'diagnostic'); assert.equal(diagnostic.prefixAudit.previousPlanId, null); + assert.deepEqual(f.session.providerMessages, main); + await run('继续'); + const last = [...f.store.core.context_plan.values()].at(-1); + assert.equal(last.prefixAudit.previousPlanId, first.id); assert.equal(last.requestObservation.path.kind, 'main'); + assert.doesNotMatch(JSON.stringify(server.requests.at(-1).input), /前面的错误是怎么回事/); + for (const plan of f.store.core.context_plan.values()) assert.doesNotThrow(() => verifyPlanEvidence(f.store, f.session, plan)); + f.store.close(); const reopened = new Store(f.data); f.onCleanup(() => reopened.close()); + for (const plan of reopened.core.context_plan.values()) assert.doesNotThrow(() => verifyPlanEvidence(reopened, reopened.session(f.session.id), plan)); +}); + +test('legacy v1 evidence still verifies without adding new observations or changing old artifacts', t => { + const f = fixture(t), run = f.store.beginRun(f.session, 'Original finite-budget sample.', false, f.store.config); + f.session.providerMessages.push({ role: 'user', content: 'Original finite-budget sample.' }); + const planner = new RequestPlanner(f.store), { body, record } = planner.prepare(f.session, run, f.store.config, { system: 'Original rules', tools: [] }); + record.prefixAudit = { ...prefixAudit(body), previousPlanId: null, previousRequestSha256: null }; delete record.requestObservation; + record.artifactId = planner.artifact(f.session, run, record, 'context-plan').id; + const original = f.store.artifactStore.read(f.store.core.artifact.get(record.artifactId)); + assert.doesNotThrow(() => verifyPlanEvidence(f.store, f.session, record)); + assert.equal(record.prefixAudit.schemaVersion, 1); assert.equal(record.requestObservation, undefined); + assert.equal(f.store.artifactStore.read(f.store.core.artifact.get(record.artifactId)), original); +});