perf(reading): share bounded file and log budgets across independent reads

This commit is contained in:
Agent Workbench Dev
2026-11-04 19:53:13 +08:00
parent d2703ad340
commit 3647296a8d
7 changed files with 81 additions and 12 deletions
+17 -5
View File
@@ -5,6 +5,8 @@ export const BROWSER_READ_HINT = '当前Run已经返回此页面结果区间,
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.
@@ -36,8 +38,8 @@ export function readMessageBudget(plan, message) {
// 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 = plan.readBudgetPolicyVersion === 2 && message.tool_calls?.length
&& message.tool_calls.every(call => call.function.name === 'read_file');
const pureReads = message.tool_calls?.length && (plan.readBudgetPolicyVersion === 3 && pooledReads(message.tool_calls)
|| 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.
@@ -50,9 +52,19 @@ export function readMessageBudget(plan, message) {
export function readBatchBudget(plan, message) {
const maximum = readMessageBudget(plan, message);
if (plan.readBudgetPolicyVersion !== 2 || !message.tool_calls?.length
|| !message.tool_calls.every(call => call.function.name === 'read_file')) return null;
return { remainingBytes: maximum * message.tool_calls.length, remainingCalls: message.tool_calls.length };
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) {
+1 -1
View File
@@ -357,7 +357,7 @@ export class RequestPlanner {
workingRetentionTokens: Math.max(0, metricsFor(new Set(lastUser ? [lastUser.index] : []), true, retainedSources).estimatedInputTokens
- metricsFor(new Set(lastUser ? [lastUser.index] : []), true, []).estimatedInputTokens),
workingRetentionPolicyVersion: 1,
readBudgetPolicyVersion: 2,
readBudgetPolicyVersion: 3,
originalMessages: session.providerMessages.length, retainedMessages: messages.length - 1 - (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,