fix: retain verifiable task observations within context budgets

This commit is contained in:
Agent Workbench Dev
2026-10-30 01:28:23 +08:00
parent 04a74babc6
commit f12a12e12e
43 changed files with 933 additions and 46 deletions
+26
View File
@@ -34,6 +34,32 @@ export function readMessageBudget(plan, message) {
return Math.max(1024, Math.floor(tokens * 3 / calls));
}
export function filePage(file, args, { maximumMessageBytes = 16000, callId = '' } = {}) {
const text = file.content, requestedOffset = args.offset ?? 0;
let offset = Math.min(requestedOffset, text.length);
if (offset > 0 && /[\uDC00-\uDFFF]/.test(text[offset]) && /[\uD800-\uDBFF]/.test(text[offset - 1])) offset--;
let maximumEnd = boundary(text, Math.min(text.length, offset + (args.limit ?? text.length)));
if (maximumEnd === offset && offset < text.length) maximumEnd = boundary(text, Math.min(offset + 2, text.length));
const newlineCount = value => value.split('\n').length - 1;
const startLine = file.start_line + newlineCount(text.slice(0, offset));
const make = end => ({ ...file, content: text.slice(offset, end),
range_start_line: file.start_line, range_end_line: file.end_line,
start_line: startLine, end_line: Math.min(file.end_line, file.start_line + newlineCount(text.slice(0, end))),
offset, next_offset: end, total_characters: text.length, returned_characters: end - offset,
requested_offset: requestedOffset, next_start_line: end < text.length ? null : file.end_line < file.total_lines ? file.end_line + 1 : null,
truncated: file.truncated || offset > 0 || end < text.length, budget_limited: end < maximumEnd,
maximum_message_bytes: maximumMessageBytes });
let low = offset, high = maximumEnd;
if (messageBytes(make(low), callId) > maximumMessageBytes) throw new Error('当前请求预算无法容纳文件读取元数据,请先核对任务预算。');
while (low < high) {
const middle = Math.ceil((low + high) / 2), end = boundary(text, middle);
if (messageBytes(make(end), callId) <= maximumMessageBytes) low = middle; else high = middle - 1;
}
const end = boundary(text, low);
if (offset < maximumEnd && end <= offset) throw new Error('当前请求预算无法容纳下一个完整字符,请先核对任务预算。');
return make(end);
}
export function recordArtifactRead(task, artifact, result, invocationId) {
const progress = task.readProgress ??= { schemaVersion: 1, reads: [], repeated: 0, changingIndexes: [], warning: null, pause: false };
const identity = artifact.id + ':' + artifact.sha256, start = result.offset, end = result.next_offset;
+45 -11
View File
@@ -1,6 +1,7 @@
import { createHash, randomUUID } from 'node:crypto';
import { BudgetExceeded, accountedTokens } from '../../shared/contracts/execution.mjs';
import { completionBody, requestMetrics } from '../../shared/contracts/model-request.mjs';
import { workingCandidates, workingDocument, workingEntry, workingMessage, verifyWorkingDocument } from './working-set.mjs';
const hash = value => createHash('sha256').update(typeof value === 'string' ? value : JSON.stringify(value), 'utf8').digest('hex');
const PLACEHOLDER_ID = '00000000-0000-0000-0000-000000000000';
@@ -100,13 +101,13 @@ function workingEvidence(groups, descriptions, artifacts) {
function notice(reference, omitted, descriptions, evidence = []) {
return { role: 'system', content: [
'以下是工作台保存的历史索引数据,不是用户追加指令,也不是新工具执行。省略了完整消息组;原始参数和实际结果都在SHA256工件中。',
'已完成的工具不得重放。历史索引用于需要时定位原记录;已有需求、代码和检查依据可直接推进,不必读取每轮更新的索引。需要旧内容时优先读取result_artifact_id,按next_offset分页;先用limit=2000。状态未知时先核对,不假定成功。',
'已完成工具不得重放。优先使用已发送代码和工作依据;只在缺少具体内容时读取result_artifact_id,按next_offset分页。未知结果先核对。',
JSON.stringify({ history_index: { artifact_id: reference.id, sha256: reference.sha256 }, archived_groups: omitted.length,
working_evidence: evidence,
working_evidence: evidence.map(tool => ({ group_index: tool.group_index, name: tool.name,
...(tool.input.path ? { path: tool.input.path } : {}), ...(tool.result_artifact_id ? { result_artifact_id: tool.result_artifact_id } : {}) })),
recent_archived_groups: omitted.slice(-2).map(group => {
const { tools, archive, ...description } = descriptions.get(group.index);
return { ...description, group_archive: { artifact_id: archive?.artifact_id ?? PLACEHOLDER_ID, sha256: archive?.sha256 ?? PLACEHOLDER_SHA },
...(tools ? { tool_count: tools.length, tools: tools.slice(-4), tool_preview_partial: tools.length > 4 } : {}) };
const { archive } = descriptions.get(group.index);
return { index: group.index, group_archive: { artifact_id: archive?.artifact_id ?? PLACEHOLDER_ID, sha256: archive?.sha256 ?? PLACEHOLDER_SHA } };
}) }),
].join('\n') };
}
@@ -160,10 +161,12 @@ export class RequestPlanner {
const evidence = workingEvidence(groups, descriptions, this.store.core.artifact);
const original = completionBody(config, [fixed, ...session.providerMessages], tools), before = requestMetrics(original);
const selected = new Set(groups.map(group => group.index));
let omitted = [], reference = null;
const metricsFor = (indices, includeNotice = true) => {
let omitted = [], reference = null, workingReference = null;
const entries = [];
const metricsFor = (indices, includeNotice = true, currentEntries = entries) => {
const missing = groups.filter(group => !indices.has(group.index));
const messages = [fixed, ...(missing.length && includeNotice ? [notice({ id: PLACEHOLDER_ID, sha256: PLACEHOLDER_SHA }, missing, descriptions, evidence)] : []),
...(currentEntries.length ? [workingMessage({ id: PLACEHOLDER_ID, sha256: PLACEHOLDER_SHA }, workingDocument(session, run, currentEntries))] : []),
...groups.filter(group => indices.has(group.index)).flatMap(group => group.messages)];
return requestMetrics(completionBody(config, messages, tools));
};
@@ -179,11 +182,28 @@ export class RequestPlanner {
}
const target = Math.max(budget.targetInputTokens, metricsFor(selected).estimatedInputTokens);
const priority = [...new Set(evidence.map(value => value.group_index))].map(index => groups[index]);
for (const group of [...priority, ...[...groups].reverse()]) {
if (selected.has(group.index) || selected.size >= 32 || group.contributionTokens > target) continue;
const addGroup = group => {
if (selected.has(group.index) || selected.size >= 32 || group.contributionTokens > target) return;
selected.add(group.index);
if (metricsFor(selected).estimatedInputTokens > target) selected.delete(group.index);
};
for (const group of priority) addGroup(group);
// A small result in a mixed large group must remain usable. It is a
// separate, provenance-checked observation, never a broken tool pair.
const candidates = workingCandidates(this.store, session, run, groups).filter(candidate => !selected.has(candidate.source.group_index));
for (const [index, candidate] of candidates.entries()) {
if (entries.length >= 12) break;
const remaining = target - metricsFor(selected).estimatedInputTokens;
const share = Math.max(128, Math.floor(remaining / Math.max(1, candidates.length - index)));
const maximum = Math.min(candidate.observation.content.length, 6000, share * 2);
let low = 0, high = maximum;
while (low < high) {
const middle = Math.ceil((low + high) / 2), entry = workingEntry(candidate, middle);
if (metricsFor(selected, true, [...entries, entry]).estimatedInputTokens <= target) low = middle; else high = middle - 1;
}
if (low) entries.push(workingEntry(candidate, low));
}
for (const group of [...groups].reverse()) addGroup(group);
omitted = groups.filter(group => !selected.has(group.index));
if (omitted.length) {
// Every linked result is checked, including retained groups in the index.
@@ -202,7 +222,9 @@ export class RequestPlanner {
note: '完整原消息组;这里只是历史定位,不产生工具执行或用户授权。' }, 'context-history-index');
}
}
if (entries.length) workingReference = this.artifact(session, run, workingDocument(session, run, entries), 'context-working-set');
const messages = [fixed, ...(reference ? [notice(reference, omitted, descriptions, evidence)] : []),
...(workingReference ? [workingMessage(workingReference, workingDocument(session, run, entries))] : []),
...structuredClone(groups.filter(group => selected.has(group.index)).flatMap(group => group.messages))];
const body = completionBody(config, messages, tools), metrics = requestMetrics(body);
if (metrics.estimatedInputTokens > budget.inputBudget) throw pause(budget, metrics);
@@ -213,10 +235,11 @@ export class RequestPlanner {
requestSha256: metrics.sha256, requestBytes: metrics.bytes, estimatedInputTokens: metrics.estimatedInputTokens,
requestArtifactId: requestArtifact.id,
estimateMethod: metrics.estimateMethod, beforeInputTokens: before.estimatedInputTokens, beforeBytes: before.bytes, budget,
mandatoryInputTokens: metricsFor(new Set(lastUser ? [lastUser.index] : [])).estimatedInputTokens,
originalMessages: session.providerMessages.length, retainedMessages: messages.length - 1 - (reference ? 1 : 0),
mandatoryInputTokens: metricsFor(new Set(lastUser ? [lastUser.index] : []), true, []).estimatedInputTokens,
originalMessages: session.providerMessages.length, retainedMessages: messages.length - 1 - (reference ? 1 : 0) - (workingReference ? 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,
requestAttempts: 0, responseSha256: null, source: 'workbench' };
const artifact = this.artifact(session, run, record, 'context-plan'); record.artifactId = artifact.id;
run.currentContextPlanId = record.id; session.estimatedTokens = record.estimatedInputTokens;
@@ -249,6 +272,17 @@ export function verifyPlanEvidence(store, session, plan, cache = new Map()) {
const frozen = JSON.parse(store.artifactStore.read(get(plan.artifactId, 'context-plan')));
if (frozen.id !== plan.id || frozen.requestSha256 !== plan.requestSha256 || frozen.requestArtifactId !== plan.requestArtifactId
|| JSON.stringify(frozen.budget) !== JSON.stringify(plan.budget)) throw new Error('请求计划与发送前的封存记录不匹配。');
if ((frozen.workingSetArtifactId ?? null) !== (plan.workingSetArtifactId ?? null)
|| (frozen.workingSetSha256 ?? null) !== (plan.workingSetSha256 ?? null)
|| (frozen.workingSetEntries ?? 0) !== (plan.workingSetEntries ?? 0)) throw new Error('工作依据与发送前的封存记录不匹配。');
if (plan.workingSetArtifactId) {
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)));
const message = workingMessage(artifact, document);
if (body.messages.filter(value => value.role === 'system' && value.content === message.content).length !== 1) throw new Error('工作依据与实际发送内容不匹配。');
}
if (!plan.historyIndexArtifactId) return;
const index = JSON.parse(store.artifactStore.read(get(plan.historyIndexArtifactId, 'context-history-index', plan.historyIndexSha256)));
if (index.sessionId !== session.id || index.projectId !== session.projectId || index.taskId !== plan.taskId
+113
View File
@@ -0,0 +1,113 @@
import { createHash } from 'node:crypto';
const hash = value => createHash('sha256').update(value, 'utf8').digest('hex');
const parse = text => { try { return JSON.parse(text); } catch { return null; } };
const boundary = (text, end) => end > 0 && end < text.length && /[\uD800-\uDBFF]/.test(text[end - 1]) ? end - 1 : end;
// An observation is a deterministic view of an actual result. It is never a
// replacement assistant/tool message and never a model-generated assertion.
function observation(tool, value) {
if (!value || typeof value !== 'object' || value.requires_verification || ['unknown', 'interrupted', 'denied'].includes(value.status)) return null;
if (tool === 'read_file' && typeof value.content === 'string' && typeof value.path === 'string' && /^[a-f0-9]{64}$/i.test(value.sha256 ?? '')) {
return { kind: 'file', path: value.path, sha256: value.sha256, start_line: value.start_line, end_line: value.end_line,
...(value.offset !== undefined ? { offset: value.offset, next_offset: value.next_offset,
range_start_line: value.range_start_line, range_end_line: value.range_end_line } : {}), content: value.content };
}
if (tool === 'read_artifact' && typeof value.content === 'string' && typeof value.kind === 'string'
&& !value.kind.startsWith('context-') && !['model-request', 'tool-result'].includes(value.kind)) {
return { kind: 'artifact-page', artifact_id: value.artifact_id, sha256: value.sha256, artifact_kind: value.kind,
offset: value.offset, next_offset: value.next_offset, total_characters: value.total_characters, content: value.content };
}
if (tool === 'run_checks' && Array.isArray(value.results)) {
const checks = value.results.map(check => Object.fromEntries(['name', 'kind', 'command', 'status', 'exitCode', 'tests', 'testCount',
'logArtifactId'].filter(key => check[key] !== undefined).map(key => [key, check[key]])));
const logs = [...value.results].sort((a, b) => Number(a.status === 'passed') - Number(b.status === 'passed'))
.map(check => '[' + check.name + ']\n' + String(check.logExcerpt ?? check.stdoutExcerpt ?? '') + (check.stderrExcerpt ? '\n' + check.stderrExcerpt : '')).join('\n');
return { kind: 'check-results', job_id: value.job_id, plan_id: value.plan_id, status: value.check_status ?? value.status,
fingerprint: value.fingerprint, checks, content: String(value.summary ?? '') + '\n' + logs };
}
if (tool === 'run_command' && typeof value.stdout === 'string') {
return { kind: 'command-result', status: value.status, exit_code: value.exit_code,
content: value.stdout + (value.stderr ? '\n' + value.stderr : '') };
}
return null;
}
export function workingCandidates(store, session, run, groups) {
const ledger = new Map([...store.core.tool.values()].filter(tool => tool.sessionId === session.id && tool.projectId === session.projectId
&& tool.taskId === run.taskId).map(tool => [tool.callId, tool]));
const candidates = [], revisions = new Map(), seen = new Set(), ranges = new Map();
for (const group of [...groups].reverse()) for (const call of [...(group.messages[0].tool_calls ?? [])].reverse()) {
const invocation = ledger.get(call.id);
if (!invocation?.resultArtifactId || invocation.requiresVerification) continue;
const result = group.messages.find(message => message.role === 'tool' && message.tool_call_id === call.id);
const artifact = store.owned('artifact', invocation.resultArtifactId, session.id);
if (artifact.taskId !== run.taskId || artifact.kind !== 'tool-result' || artifact.runId !== invocation.runId) throw new Error('工作依据的任务或运行归属不匹配。');
if (store.artifactStore.read(artifact) !== result.content) throw new Error('工作依据与原工具结果不一致。');
const observed = observation(call.function.name, parse(result.content));
if (!observed) continue;
if (observed.kind === 'check-results') {
const job = store.core.check_job.get(observed.job_id);
if (job?.stale || job?.status === 'stale' || job?.requiresVerification) continue;
}
let key = observed.kind;
if (observed.kind === 'file') {
const changed = session.changes.findLast(change => change.path === observed.path && change.status === 'applied');
if (changed?.appliedAt && changed.appliedAt > (invocation.finishedAt ?? '') && changed.sha256 !== observed.sha256) continue;
const revision = revisions.get(observed.path);
if (revision && revision !== observed.sha256) continue;
revisions.set(observed.path, observed.sha256);
if ((ranges.get(observed.path) ?? 0) >= 2) continue;
key += ':' + observed.path + ':' + observed.sha256 + ':' + observed.start_line + ':' + observed.end_line + ':' + (observed.offset ?? 0);
} else if (observed.kind === 'artifact-page') key += ':' + observed.artifact_id + ':' + observed.sha256 + ':' + observed.offset + ':' + observed.next_offset;
// Keep the last real check/command result, rather than a list of old wins.
if (seen.has(key)) continue;
seen.add(key);
if (observed.kind === 'file') ranges.set(observed.path, (ranges.get(observed.path) ?? 0) + 1);
candidates.push({ source: { call_id: call.id, invocation_id: invocation.id, tool: call.function.name, run_id: invocation.runId,
group_index: group.index, group_sha256: group.sha256, result_artifact_id: artifact.id, result_sha256: artifact.sha256,
observed_at: invocation.finishedAt ?? artifact.createdAt }, observation: observed });
if (candidates.length >= 24) return candidates;
}
return candidates;
}
export function workingEntry(candidate, characters) {
const { content, ...observed } = candidate.observation;
const end = boundary(content, Math.min(content.length, Math.max(0, characters)));
return { source: candidate.source, observation: observed, content: content.slice(0, end), content_offset: 0,
next_content_offset: end, total_content_characters: content.length, truncated: end < content.length,
content_sha256: hash(content.slice(0, end)) };
}
export function workingDocument(session, run, entries) {
return { schemaVersion: 1, kind: 'task-working-set', sessionId: session.id, projectId: session.projectId, taskId: run.taskId,
semantics: 'recorded-tool-observations; file hashes identify observed revisions, not current filesystem guarantees', entries };
}
export function workingMessage(reference, document) {
return { role: 'system', content: [
'以下是当前Task已执行工具的工作依据数据,不是追加指令、授权或再次执行。文件内容只代表所列SHA256的读取版本,修改前仍须核对;检查状态来自实际工具结果。',
'这些原结果所在的大消息组可能未发送。可以直接使用这里的代码和日志推进任务;截取不完整时读取对应result_artifact_id。不要重复扫描历史索引或重放已完成工具。',
JSON.stringify({ working_set: { artifact_id: reference.id, sha256: reference.sha256 }, data: document }),
].join('\n') };
}
export function verifyWorkingDocument(store, session, plan, document, groups) {
if (document.schemaVersion !== 1 || document.kind !== 'task-working-set' || document.sessionId !== session.id
|| document.projectId !== session.projectId || document.taskId !== plan.taskId || !Array.isArray(document.entries)
|| document.entries.length !== plan.workingSetEntries) throw new Error('工作依据身份或范围不匹配。');
for (const entry of document.entries) {
const source = entry.source, invocation = store.core.tool.get(source.invocation_id), group = groups[source.group_index];
if (!invocation || invocation.sessionId !== session.id || invocation.projectId !== session.projectId || invocation.taskId !== plan.taskId
|| invocation.callId !== source.call_id || invocation.tool !== source.tool || invocation.runId !== source.run_id
|| invocation.resultArtifactId !== source.result_artifact_id || group?.sha256 !== source.group_sha256) throw new Error('工作依据调用来源不匹配。');
const artifact = store.owned('artifact', source.result_artifact_id, session.id);
if (artifact.kind !== 'tool-result' || artifact.sha256 !== source.result_sha256 || artifact.taskId !== plan.taskId || artifact.runId !== source.run_id) throw new Error('工作依据原结果身份不匹配。');
const text = store.artifactStore.read(artifact), message = group.messages.find(value => value.role === 'tool' && value.tool_call_id === source.call_id);
if (text !== message?.content) throw new Error('工作依据原消息内容不匹配。');
const observed = observation(source.tool, parse(text));
if (!observed || !Number.isInteger(entry.next_content_offset) || entry.next_content_offset < 0 || entry.next_content_offset > observed.content.length
|| JSON.stringify(workingEntry({ source, observation: observed }, entry.next_content_offset)) !== JSON.stringify(entry)) throw new Error('工作依据摘录与实际工具结果不匹配。');
}
}
+3 -1
View File
@@ -68,7 +68,9 @@ export class DeliveryReporter {
requestArtifactId: plan.requestArtifactId, planArtifactId: plan.artifactId, estimatedInputTokens: plan.estimatedInputTokens,
beforeInputTokens: plan.beforeInputTokens, beforeBytes: plan.beforeBytes, estimateMethod: plan.estimateMethod, budget: plan.budget,
archivedGroups: plan.archivedGroups, retainedMessages: plan.retainedMessages, actualUsage: plan.actualUsage, requestAttempts: plan.requestAttempts,
historyIndexArtifactId: plan.historyIndexArtifactId, historyIndexSha256: plan.historyIndexSha256, evidenceVerified, evidenceError };
historyIndexArtifactId: plan.historyIndexArtifactId, historyIndexSha256: plan.historyIndexSha256,
workingSetArtifactId: plan.workingSetArtifactId ?? null, workingSetSha256: plan.workingSetSha256 ?? null,
workingSetEntries: plan.workingSetEntries ?? 0, evidenceVerified, evidenceError };
});
requiresVerification.push(...contextPlans.filter(plan => !plan.evidenceVerified).map(plan => ({ id: plan.id, tool: 'model_context', status: 'evidence_unverified' })));
const deliveryReview = pendingDeliveryReview(this.store, session);