fix: preserve advancing pages across resumed task runs

This commit is contained in:
Agent Workbench Dev
2026-10-30 04:15:08 +08:00
parent 700244d2a8
commit 4d06618f62
4 changed files with 95 additions and 12 deletions
+16 -8
View File
@@ -30,7 +30,7 @@ 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 - (plan.workingRetentionTokens ?? 0) - assistantTokens - calls * 8 - 512;
const tokens = plan.budget.inputBudget - (plan.nextReadFixedInputTokens ?? plan.mandatoryInputTokens) - (plan.workingRetentionTokens ?? 0) - assistantTokens - calls * 8 - 512;
return Math.max(1024, Math.floor(tokens * 3 / calls));
}
@@ -60,24 +60,32 @@ export function filePage(file, args, { maximumMessageBytes = 16000, callId = ''
return make(end);
}
export function recordArtifactRead(task, artifact, result, invocationId) {
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 };
progress.sessionInspection = null;
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]);
// 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]);
progress.repeated = covered ? progress.repeated + 1 : 0;
if (!covered) {
const ranges = [...read.ranges, [start, end]].sort((a, b) => a[0] - b[0]), merged = [];
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); }
read.ranges = merged.slice(-64);
}
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.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,
+6 -2
View File
@@ -167,10 +167,10 @@ export class RequestPlanner {
let omitted = [], reference = null, workingReference = null, latestReference = null;
const latestDocument = latestToolDocument(this.store, session, run, groups);
const entries = [];
const metricsFor = (indices, includeNotice = true, currentEntries = entries) => {
const metricsFor = (indices, includeNotice = true, currentEntries = entries, includeLatest = 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, evidence)] : []),
...(latestDocument && !indices.has(latestDocument.group_index) ? [latestToolMessage({ id: PLACEHOLDER_ID, sha256: PLACEHOLDER_SHA }, latestDocument)] : []),
...(includeLatest && latestDocument && !indices.has(latestDocument.group_index) ? [latestToolMessage({ id: PLACEHOLDER_ID, sha256: PLACEHOLDER_SHA }, latestDocument)] : []),
...(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));
@@ -256,6 +256,9 @@ export class RequestPlanner {
requestArtifactId: requestArtifact.id,
estimateMethod: metrics.estimateMethod, beforeInputTokens: before.estimatedInputTokens, beforeBytes: before.bytes, budget,
mandatoryInputTokens: metricsFor(new Set(lastUser ? [lastUser.index] : []), true, []).estimatedInputTokens,
// The next result replaces the current latest-result feedback. Do not
// reserve both views when sizing that result's actual protocol page.
nextReadFixedInputTokens: metricsFor(new Set(lastUser ? [lastUser.index] : []), true, [], false).estimatedInputTokens,
workingRetentionTokens: Math.min(2048, Math.max(0, metricsFor(new Set(lastUser ? [lastUser.index] : []), true,
entries.filter(entry => entry.observation.kind === 'file' && !entry.truncated && entry.total_content_characters <= 2000)).estimatedInputTokens
- metricsFor(new Set(lastUser ? [lastUser.index] : []), true, []).estimatedInputTokens)),
@@ -302,6 +305,7 @@ export function verifyPlanEvidence(store, session, plan, cache = new Map()) {
|| (frozen.workingSetEntries ?? 0) !== (plan.workingSetEntries ?? 0)) throw new Error('工作依据与发送前的封存记录不匹配。');
if ((frozen.workingSetViewVersion ?? 1) !== (plan.workingSetViewVersion ?? 1)
|| (frozen.workingRetentionTokens ?? 0) !== (plan.workingRetentionTokens ?? 0)) throw new Error('工作依据视图或保留预算与封存记录不匹配。');
if ((frozen.nextReadFixedInputTokens ?? null) !== (plan.nextReadFixedInputTokens ?? null)) throw new Error('下一工具页的固定预算与封存记录不匹配。');
if ((frozen.latestToolsArtifactId ?? null) !== (plan.latestToolsArtifactId ?? null)
|| (frozen.latestToolsSha256 ?? null) !== (plan.latestToolsSha256 ?? null)
|| (frozen.latestToolsEntries ?? 0) !== (plan.latestToolsEntries ?? 0)) throw new Error('最新工具反馈与发送前的封存记录不匹配。');
+1 -1
View File
@@ -291,7 +291,7 @@ export class Runtime {
else if (tool === 'read_artifact') {
const artifact = this.store.owned('artifact', args.artifact_id, session.id), text = this.store.artifactStore.read(artifact);
result = artifactPage(artifact, text, args, { callId: call.id, maximumMessageBytes: run.readMessageBytes ?? 16000 });
recordArtifactRead(this.store.core.task.get(run.taskId), artifact, result, event.invocationId);
recordArtifactRead(this.store.core.task.get(run.taskId), artifact, result, event.invocationId, run.id);
}
else if (tool === 'prepare_change_set') {
const set = await this.changeSetManager().prepare(session, args.change_ids, { source: 'agent' });
+72 -1
View File
@@ -1,7 +1,7 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { randomUUID, createHash } from 'node:crypto';
import { artifactPage, recordArtifactRead, clearReadStreak } from '../src/core/context/read-budget.mjs';
import { artifactPage, recordArtifactRead, clearReadStreak, readMessageBudget } from '../src/core/context/read-budget.mjs';
import { Runtime } from '../src/server/runtime.mjs';
import { Provider } from '../src/server/provider.mjs';
import { Store } from '../src/server/store.mjs';
@@ -10,6 +10,38 @@ import { fixture, compatibleFixture, call, completion, until, assertToolPairs }
const sha = text => createHash('sha256').update(text, 'utf8').digest('hex');
const bytes = (page, id) => Buffer.byteLength(JSON.stringify({ role: 'tool', tool_call_id: id, content: JSON.stringify(page) }), 'utf8');
test('continuation reads advance through historical pages without a false pause while repeated current-Run pages still pause', () => {
const task = { activeRunId: randomUUID() }, text = 'Persisted failure log\n'.repeat(300),
artifact = { id: randomUUID(), kind: 'check-log', sha256: sha(text) };
recordArtifactRead(task, artifact, artifactPage(artifact, text, { limit: text.length }, { maximumMessageBytes: 30000 }), 'previous-run');
const original = structuredClone(task.readProgress.reads);
task.activeRunId = randomUUID(); clearReadStreak(task);
for (const offset of [0, 669, 1662]) {
const page = artifactPage(artifact, text, { offset, limit: offset === 0 ? 669 : offset === 669 ? 993 : 2500 }, { maximumMessageBytes: 30000 });
recordArtifactRead(task, artifact, page, 'forward-' + offset);
assert.equal(task.readProgress.repeated, 0); assert.equal(task.readProgress.pause, false);
}
assert.deepEqual(task.readProgress.reads, original, 'Historical coverage must remain intact.');
assert.equal(task.readProgress.readWindow.runId, task.activeRunId);
for (const limit of [500, 400, 300]) recordArtifactRead(task, artifact, artifactPage(artifact, text, { offset: 669, limit }), 'repeat-' + limit);
assert.equal(task.readProgress.repeated, 3); assert.equal(task.readProgress.pause, true);
assert.deepEqual(task.readProgress.reads, original);
});
test('a new tool page replaces old latest-feedback space and still obeys its escaped protocol budget', () => {
const message = { role: 'assistant', content: 'Continue reading the actual log.', tool_calls: [call('next', 'read_artifact', { artifact_id: randomUUID() })] };
const base = { budget: { inputBudget: 12000 }, nextReadFixedInputTokens: 6000, workingRetentionTokens: 1000 };
const a = readMessageBudget({ ...base, mandatoryInputTokens: 11500 }, message),
b = readMessageBudget({ ...base, mandatoryInputTokens: 8000 }, message);
assert.equal(a, b, 'Previous feedback is replaced, not reserved twice.');
const text = 'Actual test failure "detail"\n'.repeat(1500), artifact = { id: randomUUID(), kind: 'check-log', sha256: sha(text) };
const page = artifactPage(artifact, text, { limit: 12000 }, { maximumMessageBytes: a, callId: 'next' });
assert.ok(page.next_offset > 2000); assert.equal(page.content, text.slice(0, page.next_offset));
assert.ok(bytes(page, 'next') <= a);
assert.equal(readMessageBudget({ budget: base.budget, mandatoryInputTokens: base.nextReadFixedInputTokens, workingRetentionTokens: 1000 }, message), a,
'Legacy plans keep their original mandatory-budget interpretation.');
});
test('budgeted artifact pages count escaped protocol bytes and reconstruct original multilingual JSON without losing characters', () => {
const text = JSON.stringify({ source: '原文🙂\r\n"\\'.repeat(4000) }), artifact = { id: randomUUID(), sha256: sha(text), kind: 'tool-result' };
let offset = 0, reconstructed = '', pages = 0;
@@ -90,3 +122,42 @@ test('a real repeated artifact loop pauses the same Task after recorded results,
assert.equal(saved.status, 'completed'); assert.equal(saved.taskId, taskId); assert.equal(requests, 5);
assert.equal([...reopened.core.tool.values()].length, 4); assert.equal(reopened.core.task.get(taskId).readProgress.pause, false);
});
test('actual HTTP continuation after reload sends distinct historically covered pages without false no_progress or replay', async t => {
const f = fixture(t), original = '实际失败日志 original test failure\n'.repeat(100);
const artifact = f.store.artifactStore.write(f.session, null, original, { kind: 'check-log' });
f.store.core.artifact.set(artifact.id, artifact); f.store.save(f.session);
let requests = 0; const observed = [];
const model = await compatibleFixture(t, (route, body) => {
if (!route.endsWith('/chat/completions')) return { body: {} };
requests++;
if (requests >= 3) {
const actual = body.messages.filter(value => value.role === 'tool').map(value => JSON.parse(value.content))
.filter(value => value.artifact_id === artifact.id).at(-1);
assert.ok(actual, 'The previous actual page must reach the resumed model.');
assert.equal(actual.content, original.slice(actual.offset, actual.next_offset)); observed.push(actual);
}
const next = requests === 1 ? { offset: 0, limit: 16000 } : requests === 2 ? { offset: 0, limit: 669 }
: requests === 3 ? { offset: 669, limit: 993 } : requests === 4 ? { offset: 1662, limit: 800 } : null;
return { body: next ? completion([call('historical-page-' + requests, 'read_artifact', { artifact_id: artifact.id, ...next })])
: completion([], '三段原失败依据均已核对。') };
});
f.store.saveConfig({ mode: 'compatible', baseUrl: model.baseUrl, model: 'continuation-pages-fixture', maxSteps: 1 });
const first = new Runtime(f.store, new Provider()); first.start(f.session.id, '读取实际保存的失败依据,然后按原任务继续核对。');
await until(() => !first.isActive(f.session.id), 20000);
assert.equal(f.session.status, 'paused'); assert.equal(requests, 1);
assert.equal(f.store.core.tool.size, 1); const savedResult = f.store.savedToolResult(f.session.id, 'historical-page-1');
assert.equal(savedResult.content, original, 'The historical Run actually returned the full source.');
const taskId = f.session.taskId, oldRunId = f.session.runId, coverage = structuredClone(f.store.core.task.get(taskId).readProgress.reads);
f.store.close(); const reopened = new Store(f.data); f.onCleanup(() => reopened.close());
reopened.saveConfig({ maxSteps: 8 }); const session = reopened.session(f.session.id);
const resumed = new Runtime(reopened, new Provider()); resumed.start(session.id, '', true);
await until(() => !resumed.isActive(session.id), 20000);
assert.equal(session.status, 'completed', JSON.stringify({ failure: session.failure, status: session.status, requests }));
assert.equal(session.taskId, taskId); assert.equal(reopened.core.run.get(session.runId).resumeOf, oldRunId);
assert.equal(requests, 5); assert.equal(reopened.core.tool.size, 4);
assert.deepEqual(observed.map(value => value.offset), [0, 669, 1662]);
const progress = reopened.core.task.get(taskId).readProgress;
assert.equal(progress.pause, false); assert.equal(progress.repeated, 0); assert.deepEqual(progress.reads, coverage);
assert.equal(progress.readWindow.runId, session.runId); assertToolPairs(session.providerMessages);
});