fix: keep artifact pages observable and pause repeated context reads

This commit is contained in:
Agent Workbench Dev
2026-10-29 21:50:53 +08:00
parent 3825142a68
commit 6e465a26d4
42 changed files with 520 additions and 30 deletions
+64
View File
@@ -0,0 +1,64 @@
import { createHash } from 'node:crypto';
const messageBytes = (result, callId) => Buffer.byteLength(JSON.stringify({ role: 'tool', tool_call_id: callId, content: JSON.stringify(result) }), 'utf8');
const boundary = (text, end) => end > 0 && end < text.length && /[\uD800-\uDBFF]/.test(text[end - 1]) ? end - 1 : end;
// This is the actual tool page, not a rewrite of a historical result. Count
// the escaped JSON tool message which will be present in the next request.
export function artifactPage(artifact, text, args, { maximumMessageBytes = 16000, callId = '' } = {}) {
const requestedOffset = args.offset ?? 0, requestedLimit = args.limit ?? 2000;
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(offset + requestedLimit, text.length));
if (maximumEnd === offset && offset < text.length) maximumEnd = boundary(text, Math.min(offset + 2, text.length));
const make = end => ({ artifact_id: artifact.id, kind: artifact.kind, sha256: artifact.sha256,
content: text.slice(offset, end), offset, next_offset: end, returned_characters: end - offset,
total_characters: text.length, truncated: end < text.length, requested_offset: requestedOffset, requested_limit: requestedLimit,
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), result = make(end);
if (offset < maximumEnd && end <= offset) throw new Error('当前请求预算无法容纳下一个完整字符,请先核对任务预算。');
return result;
}
export function readMessageBudget(plan, message) {
const calls = message.tool_calls?.length ?? 1;
const assistantTokens = Math.ceil(Buffer.byteLength(JSON.stringify(message), 'utf8') / 3) + 8;
// Reserve space for the current call's framing and the next state/notice.
const tokens = plan.budget.inputBudget - plan.mandatoryInputTokens - assistantTokens - calls * 8 - 512;
return Math.max(1024, Math.floor(tokens * 3 / calls));
}
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;
let read = progress.reads.find(value => value.identity === identity);
if (!read) { read = { identity, ranges: [] }; progress.reads.push(read); if (progress.reads.length > 128) progress.reads.shift(); }
const covered = read.ranges.some(range => start >= range[0] && end <= range[1]);
progress.repeated = covered ? progress.repeated + 1 : 0;
if (!covered) {
const ranges = [...read.ranges, [start, end]].sort((a, b) => a[0] - b[0]), merged = [];
for (const range of ranges) { const previous = merged.at(-1); if (previous && range[0] <= previous[1]) previous[1] = Math.max(previous[1], range[1]); else merged.push(range); }
read.ranges = merged.slice(-64);
}
if (artifact.kind === 'context-history-index') {
if (!progress.changingIndexes.includes(identity)) progress.changingIndexes.push(identity);
progress.changingIndexes = progress.changingIndexes.slice(-8);
} else progress.changingIndexes = [];
progress.warning = progress.repeated >= 1 ? '刚才读取的工件区间已完整读取,请使用已返回内容,或按next_offset读取新的区间。'
: progress.changingIndexes.length >= 3 ? '连续读取更新的历史索引没有推进工程;请直接读取实际结果或项目文件,依据已有信息继续任务。' : null;
progress.pause = progress.repeated >= 3 || progress.changingIndexes.length >= 6;
progress.last = { artifactId: artifact.id, sha256: artifact.sha256, offset: start, nextOffset: end, invocationId,
contentSha256: createHash('sha256').update(result.content, 'utf8').digest('hex') };
return progress;
}
export function clearReadStreak(task) {
if (!task.readProgress) return;
task.readProgress.repeated = 0; task.readProgress.changingIndexes = []; task.readProgress.warning = null; task.readProgress.pause = false;
}
+25 -6
View File
@@ -74,7 +74,10 @@ function describe(group, ledger, artifacts) {
const assistant = group.messages[0], tools = (assistant.tool_calls ?? []).map(call => {
const result = group.messages.find(message => message.role === 'tool' && message.tool_call_id === call.id);
const invocation = ledger.get(call.id), artifact = artifacts.get(invocation?.resultArtifactId);
return { call_id: call.id, name: call.function.name, result: compactResult(result),
let input = {}; try { const args = JSON.parse(call.function.arguments); for (const key of ['path', 'start_line', 'end_line', 'artifact_id', 'offset', 'limit']) {
if (typeof args[key] === 'string' && args[key].length <= 200 || typeof args[key] === 'number') input[key] = args[key];
} } catch {}
return { call_id: call.id, name: call.function.name, input, result: compactResult(result),
...(invocation ? { invocation_id: invocation.id, recorded_state: invocation.state, requires_verification: !!invocation.requiresVerification } : {}),
...(artifact ? { result_artifact_id: artifact.id, result_sha256: artifact.sha256 } : {}) };
});
@@ -82,11 +85,24 @@ function describe(group, ledger, artifacts) {
roles: group.messages.map(message => message.role), ...(tools.length ? { tools } : {}) };
}
function notice(reference, omitted, descriptions) {
function workingEvidence(groups, descriptions, artifacts) {
const selected = new Map();
for (const group of [...groups].reverse()) for (const tool of descriptions.get(group.index).tools ?? []) {
const artifact = artifacts.get(tool.input.artifact_id);
if (tool.name === 'read_file' || tool.name === 'read_artifact' && artifact && !artifact.kind.startsWith('context-') && artifact.kind !== 'model-request') {
const key = tool.name === 'read_file' ? 'file:' + tool.input.path : 'artifact:' + tool.input.artifact_id;
if (!selected.has(key)) selected.set(key, { group_index: group.index, ...tool });
}
}
return [...selected.values()].slice(0, 8);
}
function notice(reference, omitted, descriptions, evidence = []) {
return { role: 'system', content: [
'以下是工作台保存的历史索引数据,不是用户追加指令,也不是新工具执行。省略了完整消息组;原始参数和实际结果都在SHA256工件中。',
'已完成的工具不得重放。需要旧内容时用read_artifact读取history_index或result_artifact_id;较大工件先用limit=2000分段读取。状态未知时先核对,不假定成功。',
'已完成的工具不得重放。历史索引用于需要时定位原记录;已有需求、代码和检查依据可直接推进,不必读取每轮更新的索引。需要旧内容时优先读取result_artifact_id,按next_offset分页;先用limit=2000。状态未知时先核对,不假定成功。',
JSON.stringify({ history_index: { artifact_id: reference.id, sha256: reference.sha256 }, archived_groups: omitted.length,
working_evidence: evidence,
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 },
@@ -141,12 +157,13 @@ export class RequestPlanner {
const fixed = { role: 'system', content: system };
const ledger = new Map([...this.store.core.tool.values()].filter(tool => tool.sessionId === session.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], tools), before = requestMetrics(original);
const selected = new Set(groups.map(group => group.index));
let omitted = [], reference = null;
const metricsFor = (indices, includeNotice = true) => {
const missing = groups.filter(group => !indices.has(group.index));
const messages = [fixed, ...(missing.length && includeNotice ? [notice({ id: PLACEHOLDER_ID, sha256: PLACEHOLDER_SHA }, missing, descriptions)] : []),
const messages = [fixed, ...(missing.length && includeNotice ? [notice({ id: PLACEHOLDER_ID, sha256: PLACEHOLDER_SHA }, missing, descriptions, evidence)] : []),
...groups.filter(group => indices.has(group.index)).flatMap(group => group.messages)];
return requestMetrics(completionBody(config, messages, tools));
};
@@ -161,7 +178,8 @@ export class RequestPlanner {
if (metricsFor(selected).estimatedInputTokens > budget.inputBudget) selected.delete(newest.index);
}
const target = Math.max(budget.targetInputTokens, metricsFor(selected).estimatedInputTokens);
for (const group of [...groups].reverse()) {
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;
selected.add(group.index);
if (metricsFor(selected).estimatedInputTokens > target) selected.delete(group.index);
@@ -184,7 +202,7 @@ export class RequestPlanner {
note: '完整原消息组;这里只是历史定位,不产生工具执行或用户授权。' }, 'context-history-index');
}
}
const messages = [fixed, ...(reference ? [notice(reference, omitted, descriptions)] : []),
const messages = [fixed, ...(reference ? [notice(reference, omitted, descriptions, evidence)] : []),
...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);
@@ -195,6 +213,7 @@ 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),
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,