156 lines
12 KiB
JavaScript
156 lines
12 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;
|
|
|
|
// 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.nextReadFixedInputTokens ?? plan.mandatoryInputTokens) - (plan.workingRetentionTokens ?? 0) - assistantTokens - calls * 8 - 512;
|
|
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, 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]);
|
|
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.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.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 || consecutive >= 3 || progress.repeated >= 3;
|
|
return progress;
|
|
}
|