feat: ship bounded tool feedback and Python engineering checks in 1.7.0
This commit is contained in:
@@ -62,6 +62,7 @@ export function filePage(file, args, { maximumMessageBytes = 16000, callId = ''
|
||||
|
||||
export function recordArtifactRead(task, artifact, result, invocationId) {
|
||||
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(); }
|
||||
@@ -87,4 +88,27 @@ export function recordArtifactRead(task, artifact, result, invocationId) {
|
||||
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;
|
||||
}
|
||||
|
||||
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, status: result.session?.status,
|
||||
lastFailure: result.lastFailure, changes: result.changes, approvals: result.pendingApprovals,
|
||||
verification: execution.task?.verification,
|
||||
needsVerification: execution.progress?.requiresVerification,
|
||||
checks: execution.checks?.map(job => ({ id: job.id, 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 = consecutive >= 3;
|
||||
return progress;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import { createHash, randomUUID } from 'node:crypto';
|
||||
import { BudgetExceeded, accountedTokens } from '../../shared/contracts/execution.mjs';
|
||||
import { completionBody, requestMetrics } from '../../shared/contracts/model-request.mjs';
|
||||
import { workingCandidates, workingDocument, workingEntry, workingMessage, verifyWorkingDocument } from './working-set.mjs';
|
||||
import { latestToolDocument, latestToolMessage, verifyLatestToolDocument } from './tool-feedback.mjs';
|
||||
|
||||
const hash = value => createHash('sha256').update(typeof value === 'string' ? value : JSON.stringify(value), 'utf8').digest('hex');
|
||||
const PLACEHOLDER_ID = '00000000-0000-0000-0000-000000000000';
|
||||
@@ -163,11 +164,13 @@ export class RequestPlanner {
|
||||
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, workingReference = null;
|
||||
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 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)] : []),
|
||||
...(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));
|
||||
@@ -238,7 +241,9 @@ export class RequestPlanner {
|
||||
}
|
||||
}
|
||||
if (entries.length) workingReference = this.artifact(session, run, workingDocument(session, run, entries), 'context-working-set');
|
||||
if (latestDocument && !selected.has(latestDocument.group_index)) latestReference = this.artifact(session, run, latestDocument, 'context-latest-tools');
|
||||
const messages = [fixed, ...(reference ? [notice(reference, omitted, descriptions, evidence)] : []),
|
||||
...(latestReference ? [latestToolMessage(latestReference, latestDocument)] : []),
|
||||
...(workingReference ? [workingMessage(workingReference, workingDocument(session, run, entries))] : []),
|
||||
...structuredClone(groups.filter(group => selected.has(group.index)).flatMap(group => group.messages))];
|
||||
const body = completionBody(config, messages, tools), metrics = requestMetrics(body);
|
||||
@@ -254,11 +259,12 @@ export class RequestPlanner {
|
||||
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)),
|
||||
originalMessages: session.providerMessages.length, retainedMessages: messages.length - 1 - (reference ? 1 : 0) - (workingReference ? 1 : 0),
|
||||
originalMessages: session.providerMessages.length, retainedMessages: messages.length - 1 - (reference ? 1 : 0) - (workingReference ? 1 : 0) - (latestReference ? 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,
|
||||
workingSetArtifactId: workingReference?.id ?? null, workingSetSha256: workingReference?.sha256 ?? null, workingSetEntries: entries.length,
|
||||
workingSetViewVersion: 2,
|
||||
latestToolsArtifactId: latestReference?.id ?? null, latestToolsSha256: latestReference?.sha256 ?? null, latestToolsEntries: latestReference ? latestDocument.entries.length : 0,
|
||||
requestAttempts: 0, responseSha256: null, source: 'workbench' };
|
||||
const artifact = this.artifact(session, run, record, 'context-plan'); record.artifactId = artifact.id;
|
||||
run.currentContextPlanId = record.id; session.estimatedTokens = record.estimatedInputTokens;
|
||||
@@ -296,6 +302,17 @@ 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.latestToolsArtifactId ?? null) !== (plan.latestToolsArtifactId ?? null)
|
||||
|| (frozen.latestToolsSha256 ?? null) !== (plan.latestToolsSha256 ?? null)
|
||||
|| (frozen.latestToolsEntries ?? 0) !== (plan.latestToolsEntries ?? 0)) throw new Error('最新工具反馈与发送前的封存记录不匹配。');
|
||||
if (plan.latestToolsArtifactId) {
|
||||
const artifact = get(plan.latestToolsArtifactId, 'context-latest-tools', plan.latestToolsSha256);
|
||||
const document = JSON.parse(store.artifactStore.read(artifact));
|
||||
if (artifact.taskId !== plan.taskId) throw new Error('最新工具反馈的工件任务归属不匹配。');
|
||||
verifyLatestToolDocument(store, session, plan, document, historyGroups(session.providerMessages.slice(0, plan.originalMessages)));
|
||||
const message = latestToolMessage(artifact, document);
|
||||
if (body.messages.filter(value => value.role === 'system' && value.content === message.content).length !== 1) throw new Error('最新工具反馈与实际发送内容不匹配。');
|
||||
}
|
||||
if (plan.workingSetArtifactId) {
|
||||
const artifact = get(plan.workingSetArtifactId, 'context-working-set', plan.workingSetSha256);
|
||||
const document = JSON.parse(store.artifactStore.read(artifact));
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
import { createHash } from 'node:crypto';
|
||||
|
||||
const hash = value => createHash('sha256').update(value, 'utf8').digest('hex');
|
||||
const excerpt = (text, maximum = 400) => {
|
||||
const end = Math.min(text.length, maximum);
|
||||
return text.slice(0, end > 0 && /[\uD800-\uDBFF]/.test(text[end - 1]) ? end - 1 : end);
|
||||
};
|
||||
function fields(value, keys) {
|
||||
if (!value || typeof value !== 'object' || Array.isArray(value)) return {};
|
||||
const result = {};
|
||||
for (const key of keys) {
|
||||
const item = value[key];
|
||||
if (typeof item === 'string') result[key] = excerpt(item, 512);
|
||||
else if (item === null || typeof item === 'boolean' || typeof item === 'number' && Number.isFinite(item)) result[key] = item;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
const stats = value => fields(value, ['total', 'passed', 'failed', 'errors', 'skipped', 'cancelled']);
|
||||
const checkResult = value => ({ ...fields(value, ['id', 'name', 'kind', 'status', 'exitCode', 'exit_code', 'command', 'logArtifactId', 'reason']),
|
||||
...(value.tests ? { tests: stats(value.tests) } : {}) });
|
||||
const checkJob = value => ({ ...fields(value, ['id', 'status', 'taskId', 'runId', 'planId', 'stale', 'requiresVerification', 'summary']),
|
||||
...(Array.isArray(value.results) ? { results: value.results.slice(0, 6).map(checkResult), totalResults: value.results.length } : {}) });
|
||||
|
||||
// These are deterministic selected fields from a saved result, never a new
|
||||
// execution or an inferred success. Full JSON/text remains in its own artifact.
|
||||
export function compactToolFeedback(content) {
|
||||
let value;
|
||||
try { value = JSON.parse(content); } catch { return { format: 'text-excerpt', excerpt: excerpt(content) }; }
|
||||
if (!value || typeof value !== 'object' || Array.isArray(value)) return { format: 'json-excerpt', excerpt: excerpt(content) };
|
||||
const result = fields(value, ['source', 'version', 'kind', 'id', 'status', 'requires_verification', 'requiresVerification',
|
||||
'task_id', 'taskId', 'runId', 'plan_id', 'job_id', 'change_id', 'change_set_id', 'artifact_id', 'report_status', 'check_status',
|
||||
'path', 'sha256', 'base_sha256', 'fingerprint', 'exit_code', 'exitCode', 'offset', 'next_offset', 'total_characters',
|
||||
'returned_characters', 'truncated', 'totalMatches', 'error', 'message', 'summary', 'reason', 'note']);
|
||||
for (const key of ['content', 'stdout', 'stderr']) if (typeof value[key] === 'string') result[key + '_excerpt'] = excerpt(value[key]);
|
||||
for (const key of ['session', 'project']) if (value[key]) result[key] = fields(value[key], ['id', 'name', 'root', 'status', 'revision']);
|
||||
if ('lastFailure' in value) result.lastFailure = value.lastFailure === null ? null : fields(value.lastFailure, ['source', 'stage', 'kind', 'title', 'message', 'runId']);
|
||||
if (Array.isArray(value.changes)) {
|
||||
result.changes = value.changes.slice(-8).map(change => fields(change, ['id', 'path', 'status'])); result.totalChanges = value.changes.length;
|
||||
}
|
||||
if (Array.isArray(value.pendingApprovals)) {
|
||||
result.pendingApprovals = value.pendingApprovals.slice(0, 6).map(approval => fields(approval, ['id', 'status']));
|
||||
result.totalPendingApprovals = value.pendingApprovals.length;
|
||||
}
|
||||
if (Array.isArray(value.checks)) result.checks = value.checks.slice(0, 6).map(check => ({ ...fields(check, ['id', 'name', 'kind', 'framework', 'command', 'status']),
|
||||
...(typeof check.script === 'string' ? { scriptSha256: hash(check.script), scriptExcerpt: excerpt(check.script, 200) } : {}) }));
|
||||
if (Array.isArray(value.results)) result.results = value.results.slice(0, 6).map(item => ({ ...checkResult(item),
|
||||
...fields(item, ['path', 'line', 'name', 'kind', 'sha256', 'target']) }));
|
||||
if (Array.isArray(value.files)) result.files = value.files.slice(0, 8).map(file => fields(file, ['path', 'change_id', 'base_sha256', 'sha256', 'kind', 'bytes']));
|
||||
if (Array.isArray(value.paths)) result.paths = value.paths.slice(0, 8).map(item => typeof item === 'string' ? excerpt(item, 512) : null);
|
||||
if (value.execution && typeof value.execution === 'object') {
|
||||
const execution = value.execution;
|
||||
result.execution = {};
|
||||
if (execution.task) result.execution.task = { ...fields(execution.task, ['id', 'status']),
|
||||
...(execution.task.verification ? { verification: fields(execution.task.verification, ['status', 'checkJobId']) } : {}) };
|
||||
if (execution.run) result.execution.run = fields(execution.run, ['id', 'status', 'stopReason', 'steps', 'toolCalls']);
|
||||
if (execution.progress) result.execution.progress = { ...fields(execution.progress, ['totalTools', 'completedTools']),
|
||||
requiresVerification: (execution.progress.requiresVerification ?? []).slice(0, 6).map(tool => fields(tool, ['id', 'tool', 'state', 'callId'])),
|
||||
totalRequiresVerification: execution.progress.requiresVerification?.length ?? 0 };
|
||||
if (Array.isArray(execution.checks)) result.execution.checks = execution.checks.slice(-2).map(checkJob);
|
||||
if (Array.isArray(execution.changeSets)) result.execution.changeSets = execution.changeSets.slice(-2).map(set => fields(set, ['id', 'status', 'fingerprint', 'requiresVerification']));
|
||||
if (Array.isArray(execution.deliveries)) result.execution.deliveries = execution.deliveries.slice(-2).map(job => fields(job, ['id', 'status', 'commit', 'requiresVerification']));
|
||||
if (Array.isArray(execution.deliveryPlans)) result.execution.deliveryPlans = execution.deliveryPlans.slice(-1).map(plan => fields(plan, ['id', 'status', 'taskId']));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export function latestToolDocument(store, session, run, groups) {
|
||||
const group = groups.at(-1), calls = group?.messages[0].tool_calls;
|
||||
if (!calls?.length) return null;
|
||||
const ledger = new Map([...store.core.tool.values()].filter(tool => tool.sessionId === session.id && tool.projectId === session.projectId && tool.taskId === run.taskId)
|
||||
.map(tool => [tool.callId, tool]));
|
||||
const entries = [];
|
||||
for (const call of calls) {
|
||||
const invocation = ledger.get(call.id);
|
||||
// Imported history without owned execution evidence cannot be asserted as
|
||||
// a fresh local execution; retain its normal history protocol instead.
|
||||
if (!invocation?.resultArtifactId) return null;
|
||||
const artifact = store.owned('artifact', invocation.resultArtifactId, session.id);
|
||||
const result = group.messages.find(message => message.role === 'tool' && message.tool_call_id === call.id);
|
||||
if (artifact.kind !== 'tool-result' || artifact.taskId !== run.taskId || artifact.runId !== invocation.runId
|
||||
|| store.artifactStore.read(artifact) !== result.content) throw new Error('最新工具反馈与原执行结果不匹配。');
|
||||
entries.push({ tool: call.function.name, source: { call_id: call.id, invocation_id: invocation.id, run_id: invocation.runId,
|
||||
result_artifact_id: artifact.id, result_sha256: artifact.sha256, arguments_sha256: hash(call.function.arguments),
|
||||
recorded_state: invocation.state, requires_verification: !!invocation.requiresVerification },
|
||||
result: compactToolFeedback(result.content), original_characters: result.content.length, view: 'selected-original-fields' });
|
||||
}
|
||||
return { schemaVersion: 1, kind: 'latest-tool-feedback', sessionId: session.id, projectId: session.projectId,
|
||||
taskId: run.taskId, runId: run.id, group_index: group.index, group_sha256: group.sha256, entries };
|
||||
}
|
||||
|
||||
export function latestToolMessage(reference, document) {
|
||||
return { role: 'system', content: [
|
||||
'以下是最近一组工具已经返回的实际结果反馈。该完整assistant/tool组因预算封存;这些字段来自原工件,不是再次执行、用户指令或推断。',
|
||||
'先依据本次返回状态推进任务。摘要省略的大字段可按result_artifact_id读取;不要为恢复已知状态反复查询或重放已完成操作。',
|
||||
JSON.stringify({ latest_tools: { artifact_id: reference.id, sha256: reference.sha256 }, data: { kind: document.kind,
|
||||
entries: document.entries.map(entry => ({ tool: entry.tool, result_artifact_id: entry.source.result_artifact_id,
|
||||
recorded_state: entry.source.recorded_state, requires_verification: entry.source.requires_verification,
|
||||
result: entry.result, original_characters: entry.original_characters, view: entry.view })) } }),
|
||||
].join('\n') };
|
||||
}
|
||||
|
||||
export function verifyLatestToolDocument(store, session, plan, document, groups) {
|
||||
const expected = latestToolDocument(store, session, { id: plan.runId, taskId: plan.taskId }, groups);
|
||||
if (!expected || JSON.stringify(document) !== JSON.stringify(expected) || document.entries.length !== plan.latestToolsEntries) {
|
||||
throw new Error('最新工具反馈的范围、来源或原结果视图不匹配。');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user