Files
Agent-Workbench/src/core/context/read-budget.mjs
T

248 lines
18 KiB
JavaScript

import { createHash } from 'node:crypto';
const messageBytes = (result, callId) => Buffer.byteLength(JSON.stringify({ role: 'tool', tool_call_id: callId, content: JSON.stringify(result) }), 'utf8');
export const BROWSER_READ_HINT = '当前Run已经返回此页面结果区间,请使用已返回的实际字段或按next_offset读取新段;页面失败后准备修正的新计划。';
export const readMetadataReserve = metadata => Object.keys(metadata).length
? Buffer.byteLength(JSON.stringify(JSON.stringify(metadata)), 'utf8') + 8 : 0;
const boundary = (text, end) => end > 0 && end < text.length && /[\uD800-\uDBFF]/.test(text[end - 1]) ? end - 1 : end;
export const isPagedRead = call => ['read_file', 'read_artifact'].includes(call.function.name);
const pooledReads = calls => calls?.length && calls.every(isPagedRead) && (calls.length > 1 || calls[0].function.name === 'read_file');
// 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 ?? (maximumMessageBytes === Infinity ? text.length : 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) {
if (plan.budget.inputBudget === null) return Infinity;
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.
// The next request also gains bounded read coverage and route observations.
// Reserve their growth before returning a page, rather than dropping that
// new page or enlarging the request budget afterwards.
const pureReads = message.tool_calls?.length && (plan.readBudgetPolicyVersion === 3 && (pooledReads(message.tool_calls)
|| message.tool_calls.every(call => call.function.name === 'read_browser_result'))
|| plan.readBudgetPolicyVersion === 2 && message.tool_calls.every(call => call.function.name === 'read_file'));
// A pure read batch cannot create a plan, command output or change set. Its
// bounded state/coverage growth uses 512 tokens plus 256 for each additional
// read. Mixed batches and historical plans retain the original 2560 reserve.
// This is page sizing, never dispatch authorization: the complete next wire
// request and observed-usage reservation are still checked independently.
// Browser fields do not grow source-page coverage. Keep the larger source
// reservation for file/artifact reads without shrinking a small browser page.
const browserOnly = message.tool_calls?.length && message.tool_calls.every(call => call.function.name === 'read_browser_result');
const growth = Math.max(browserOnly ? 0 : plan.readStateGrowthTokens ?? 0, pureReads ? 512 + Math.min(2048, Math.max(0, calls - 1) * 256) : 2560);
const tokens = plan.budget.inputBudget - (plan.nextReadFixedInputTokens ?? plan.mandatoryInputTokens) - (plan.workingRetentionTokens ?? 0) - assistantTokens - calls * 8 - growth;
return Math.max(1024, Math.floor(tokens * 3 / calls));
}
export function readBatchBudget(plan, message) {
const maximum = readMessageBudget(plan, message);
if (!message.tool_calls?.length || !(plan.readBudgetPolicyVersion === 3 && pooledReads(message.tool_calls)
|| plan.readBudgetPolicyVersion === 2 && message.tool_calls.every(call => call.function.name === 'read_file'))) return null;
return { policyVersion: plan.readBudgetPolicyVersion, remainingBytes: maximum * message.tool_calls.length, remainingCalls: message.tool_calls.length };
}
export function orderReadCalls(batch, calls, estimateBytes) {
if (batch?.policyVersion !== 3) return calls;
// Independent reads finish with their original IDs. Small sources release
// unused space to larger files/logs even if the model listed the latter first.
return calls.map((call, index) => {
let bytes = Infinity; try { const value = estimateBytes(call); if (Number.isFinite(value) && value >= 0) bytes = value; } catch {}
return { call, index, bytes };
}).sort((a, b) => a.bytes - b.bytes || a.index - b.index).map(value => value.call);
}
export function spendReadBudget(batch, call, result) {
if (!batch) return;
batch.remainingBytes = Math.max(0, batch.remainingBytes - messageBytes(result, call.id));
batch.remainingCalls = Math.max(0, batch.remainingCalls - 1);
}
export function filePage(file, args, { maximumMessageBytes = 16000, callId = '', maximumPageCharacters = Infinity } = {}) {
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 = Math.min(maximumEnd, boundary(text, offset + maximumPageCharacters));
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 beginReadTurn(task, runId, stepId) {
const progress = task.readProgress ??= { schemaVersion: 1, reads: [], repeated: 0, changingIndexes: [], warning: null, pause: false };
progress.readTurn = { runId, stepId, newCharacters: 0, repeatedReads: 0, settled: false };
}
export function finishReadTurn(task, runId, stepId) {
const progress = task.readProgress, turn = progress?.readTurn;
if (!turn || turn.runId !== runId || turn.stepId !== stepId || turn.settled) return;
turn.settled = true;
const independentPause = progress.checkPause === true || progress.noopPause === true
|| (progress.sessionInspection?.consecutive ?? 0) >= 3 || progress.changingIndexes.length >= 6;
progress.pause ||= independentPause;
// A trailing sequence of repeated tools is not a wholly repeated turn when
// this same response returned new source/evidence bytes. Keep independent
// unchanged-check/action/state and changing-index guards authoritative.
if (turn.newCharacters > 0 && !independentPause) {
progress.pause = false;
progress.repeated = 0;
}
}
export function recordArtifactRead(task, artifact, result, invocationId, runId = task.activeRunId ?? null) {
const progress = task.readProgress ??= { schemaVersion: 1, reads: [], repeated: 0, changingIndexes: [], warning: null, pause: false };
const identity = artifact.id + ':' + (artifact.section ? artifact.section + ':' : '') + artifact.sha256, start = result.offset, end = result.next_offset;
let read = progress.reads.find(value => value.identity === identity);
if (!read) { read = { identity, kind: artifact.kind, ranges: [] }; progress.reads.push(read); if (progress.reads.length > 128) progress.reads.shift(); }
// Historical coverage is an audit of returned pages, not proof that a new
// Run received them in its bounded request. Re-reading different old pages
// in a continuation must not accumulate as three semantic repeats.
if (!progress.readWindow || progress.readWindow.runId !== runId) progress.readWindow = { runId, reads: [] };
let window = progress.readWindow.reads.find(value => value.identity === identity);
if (!window) { window = { identity, ranges: [] }; progress.readWindow.reads.push(window); if (progress.readWindow.reads.length > 128) progress.readWindow.reads.shift(); }
const covered = window.ranges.some(range => start >= range[0] && end <= range[1]);
const turn = progress.readTurn;
if (turn?.runId === runId && !turn.settled) {
if (covered) turn.repeatedReads++;
else if (artifact.kind !== 'context-history-index') turn.newCharacters += Math.max(0, end - start
- window.ranges.reduce((sum, [left, right]) => sum + Math.max(0, Math.min(end, right) - Math.max(start, left)), 0));
}
progress.repeated = covered ? progress.repeated + 1 : 0;
const merge = ranges => {
ranges.sort((a, b) => a[0] - b[0]); const 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); }
return merged.slice(-64);
};
if (!covered) window.ranges = merge([...window.ranges, [start, end]]);
if (!read.ranges.some(range => start >= range[0] && end <= range[1])) read.ranges = merge([...read.ranges, [start, end]]);
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.checkPause === true || progress.noopPause === true || 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;
task.readProgress.sessionInspection = null;
task.readProgress.observations = [];
task.readProgress.checkPause = false;
task.readProgress.noopPause = false;
task.readProgress.resultRepeats = 0;
}
export function recordFileRead(task, result, invocationId, runId) {
return recordArtifactRead(task, { id: 'file:' + result.path + ':' + result.range_start_line + ':' + result.range_end_line,
sha256: result.sha256, kind: 'project-file' }, result, invocationId, runId);
}
export function recordUnchangedCheck(task, job, runId) {
const progress = task.readProgress ??= { schemaVersion: 1, reads: [], repeated: 0, changingIndexes: [], warning: null, pause: false };
const observations = progress.observations ??= [];
let value = observations.find(value => value.runId === runId && value.key === 'check:' + job.inputIdentity);
if (!value) { value = { runId, key: 'check:' + job.inputIdentity, count: 0 }; observations.push(value); if (observations.length > 64) observations.shift(); }
value.count++;
progress.warning = '所选检查的已记录输入未变化,已返回原检查日志,没有再次执行。请依据失败修复源码;确需重跑可由用户在工程检查中确认。';
progress.checkPause ||= value.count >= 2;
progress.pause ||= progress.checkPause;
return progress;
}
export function recordBrowserRead(task, result, invocationId, runId = task.activeRunId ?? null) {
const progress = recordArtifactRead(task, { id: result.source_artifact_id, sha256: result.view_sha256, kind: 'browser-result-view', section: result.section }, result, invocationId, runId);
progress.last.section = result.section; progress.last.sourceSha256 = result.source_sha256;
result.repeated_read_count = progress.repeated;
if (progress.warning) result.reading_hint = BROWSER_READ_HINT;
return progress;
}
export function recordSessionInspection(task, result) {
const progress = task.readProgress ??= { schemaVersion: 1, reads: [], repeated: 0, changingIndexes: [], warning: null, pause: false };
const execution = result.execution ?? {};
// Revision, request usage and tool counters change merely by inspecting.
// They must not make identical engineering state appear to be progress.
const state = { sessionId: result.session?.id,
lastFailure: result.lastFailure, changes: result.changes, approvals: result.pendingApprovals,
verification: execution.task?.verification && { status: execution.task.verification.status, sourceFingerprint: execution.task.verification.sourceFingerprint },
taskPlanId: execution.task?.currentTaskPlanId,
taskProofs: execution.taskProofs?.map(proof => ({ id: proof.id, planId: proof.planId, stepKey: proof.stepKey, status: proof.status })),
browserJobs: execution.browserJobs?.map(job => ({ id: job.id, planId: job.planId, status: job.status, resultSha256: job.resultSha256, assertions: job.assertions })),
needsVerification: execution.progress?.requiresVerification,
checks: execution.checks?.map(job => ({ inputIdentity: job.inputIdentity, fingerprint: job.fingerprint, status: job.status, stale: job.stale,
results: job.results.map(check => ({ name: check.name, status: check.status, exitCode: check.exitCode, tests: check.tests })) })),
changeSets: execution.changeSets?.map(set => ({ id: set.id, status: set.status, requiresVerification: set.requiresVerification })),
deliveries: execution.deliveries?.map(job => ({ id: job.id, status: job.status, commit: job.commit })) };
const signature = createHash('sha256').update(JSON.stringify(state)).digest('hex');
const previous = progress.sessionInspection;
const consecutive = previous?.signature === signature ? previous.consecutive + 1 : 1;
progress.sessionInspection = { signature, consecutive };
progress.warning = consecutive >= 2 ? '连续核对的工程状态没有变化,请使用已经返回的状态与源码推进当前任务。' : null;
progress.pause = progress.checkPause === true || progress.noopPause === true || consecutive >= 3 || progress.repeated >= 3;
return progress;
}
export function recordUnchangedAction(task,tool,result,runId){
const progress=task.readProgress??={schemaVersion:1,reads:[],repeated:0,changingIndexes:[],warning:null,pause:false};
const observations=progress.observations??=[];
const key=JSON.stringify(['unchanged-action',tool,result.proof_id??result.path]);
let entry=observations.find(value=>value.runId===runId&&value.key===key);
if(!entry){entry={runId,key,count:0};observations.push(entry);if(observations.length>64)observations.shift();}
entry.count++;
progress.warning='本次操作没有新增文件修改或步骤证明;请使用原结果推进任务,重复核验或相同内容提案不会恢复进展计数。';
progress.noopPause ||= entry.count>=3;progress.pause ||= progress.noopPause;
return entry.count;
}
export function readCoverageContext(task,runId){
const progress=task.readProgress,window=progress?.readWindow;
if(!window||window.runId!==runId||!window.reads.length)return null;
const snapshot={schemaVersion:1,run_id:runId,semantics:'ranges_returned_by_tools_in_this_run_not_a_guarantee_all_content_is_in_this_request',
covered:window.reads.slice(-6).map(value=>({identity:value.identity,ranges:value.ranges.slice(-4)})),
total_sources:window.reads.length,truncated:window.reads.length>6||window.reads.some(value=>value.ranges.length>4),
note:'相同SHA的已返回区间可以使用当前工作依据,缺失内容仍可必要重读;新分页、新SHA或新的实际检查分别核对,不重放旧工具。'};
while(snapshot.covered.length&&Buffer.byteLength(JSON.stringify(snapshot),'utf8')>2000){snapshot.covered.shift();snapshot.truncated=true;}
return snapshot;
}