feat(context): audit tool visibility and isolate diagnostic history

This commit is contained in:
Agent Workbench Dev
2026-11-06 12:22:37 +08:00
parent b14e26e665
commit a6e31dec9d
18 changed files with 420 additions and 36 deletions
+23 -3
View File
@@ -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) : {}) };
}) };
}
+19
View File
@@ -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) }];
}) };
}
+26 -12
View File
@@ -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('工作依据与实际发送内容不匹配。');
}
+40
View File
@@ -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('工具可见性与实际协议工具不匹配。');
}
}
}