|
|
|
@@ -9,6 +9,8 @@ import { mcpCatalogMessage, MCP_LIMITS } from '../tools/mcp-contract.mjs';
|
|
|
|
|
import { modelReservation,taskConsumption,verifyModelReservation } from '../../shared/contracts/model-reservation.mjs';
|
|
|
|
|
import { fileContinuation } from './read-continuation.mjs';
|
|
|
|
|
import { prefixAudit } from './prefix-audit.mjs';
|
|
|
|
|
import { observeRequest, providerHistory, requestPath } from './request-observation.mjs';
|
|
|
|
|
import { verifyToolSurface } from './tool-surface.mjs';
|
|
|
|
|
import { isPassthrough } from '../../shared/contracts/model-capacity.mjs';
|
|
|
|
|
|
|
|
|
|
const hash = value => createHash('sha256').update(typeof value === 'string' ? value : JSON.stringify(value), 'utf8').digest('hex');
|
|
|
|
@@ -187,10 +189,11 @@ export class RequestPlanner {
|
|
|
|
|
return { artifact_id: artifact.id, sha256: artifact.sha256, group_sha256: group.sha256, bytes: Buffer.byteLength(text, 'utf8'), chunks: chunks.length };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
prepare(session, run, config, { system, contextTail = null, tools, stepId, projectMemory = null, skillCatalog = null, mcpCatalog = null, transportReservationTokens = 0,engineStepOnly=false }) {
|
|
|
|
|
prepare(session, run, config, { system, contextTail = null, tools, toolSurface = null, stepId, projectMemory = null, skillCatalog = null, mcpCatalog = null, transportReservationTokens = 0,engineStepOnly=false }) {
|
|
|
|
|
const task = this.store.owned('task', run.taskId, session.id);
|
|
|
|
|
this.store.owned('run', run.id, session.id);
|
|
|
|
|
const groups = historyGroups(session.providerMessages), budget = budgetFor(config, run, task, transportReservationTokens);
|
|
|
|
|
const history = providerHistory(session, run);
|
|
|
|
|
const groups = historyGroups(history), budget = budgetFor(config, run, task, transportReservationTokens);
|
|
|
|
|
const inputBudget = budget.inputBudget ?? Infinity, targetInputTokens = budget.targetInputTokens ?? Infinity;
|
|
|
|
|
const lastUser = groups.findLast(group => group.messages[0].role === 'user');
|
|
|
|
|
const fixed = { role: 'system', content: system };
|
|
|
|
@@ -208,7 +211,7 @@ export class RequestPlanner {
|
|
|
|
|
const ledger = new Map([...this.store.core.tool.values()].filter(tool => tool.sessionId === session.id && tool.taskId === task.id).map(tool => [tool.callId, tool]));
|
|
|
|
|
const descriptions = new Map(groups.map(group => [group.index, describe(group, ledger, this.store.core.artifact)]));
|
|
|
|
|
const evidence = workingEvidence(groups, descriptions, this.store.core.artifact);
|
|
|
|
|
const original = completionBody(config, [fixed, ...session.providerMessages, ...tail], tools), before = requestMetrics(original);
|
|
|
|
|
const original = completionBody(config, [fixed, ...history, ...tail], tools), before = requestMetrics(original);
|
|
|
|
|
if (preserve && before.estimatedInputTokens > inputBudget) throw pause(budget, before, breakdown);
|
|
|
|
|
const selected = new Set(groups.map(group => group.index));
|
|
|
|
|
let omitted = [], reference = null, workingReference = null, latestReference = null;
|
|
|
|
@@ -325,7 +328,7 @@ export class RequestPlanner {
|
|
|
|
|
descriptions.set(group.index, description); return description;
|
|
|
|
|
});
|
|
|
|
|
reference = this.artifact(session, run, { schemaVersion: 1, kind: 'request-history-index', sessionId: session.id, projectId: session.projectId,
|
|
|
|
|
taskId: run.taskId, history_sha256: hash(session.providerMessages), historyMessages: session.providerMessages.length,
|
|
|
|
|
taskId: run.taskId, history_sha256: hash(history), historyMessages: history.length,
|
|
|
|
|
retainedGroups: groups.filter(group => selected.has(group.index)).map(group => descriptions.get(group.index)), archivedGroups: archived,
|
|
|
|
|
note: '完整原消息组;这里只是历史定位,不产生工具执行或用户授权。' }, 'context-history-index');
|
|
|
|
|
}
|
|
|
|
@@ -389,16 +392,20 @@ export class RequestPlanner {
|
|
|
|
|
tokenReservation,remainingTokens:credit,pauseKind:'request_reservation'},metrics,breakdown);
|
|
|
|
|
const requestArtifact = this.artifact(session, run, body, 'model-request');
|
|
|
|
|
const tailArtifact = tail.length ? this.artifact(session, run, tail[0], 'context-state-tail') : null;
|
|
|
|
|
const previousPlan = [...this.store.core.context_plan.values()].filter(plan => plan.sessionId === session.id && plan.taskId === task.id && plan.requestAttempts > 0)
|
|
|
|
|
const observation = observeRequest(this.store, session, run, history, toolSurface);
|
|
|
|
|
verifyToolSurface(toolSurface, requestVariants(body));
|
|
|
|
|
const previousPlan = [...this.store.core.context_plan.values()].filter(plan => plan.sessionId === session.id && plan.taskId === task.id && plan.requestAttempts > 0
|
|
|
|
|
&& requestPath(this.store.owned('run', plan.runId, session.id)).key === observation.path.key)
|
|
|
|
|
.sort((a, b) => a.createdAt.localeCompare(b.createdAt) || a.requestIndex - b.requestIndex).at(-1) ?? null;
|
|
|
|
|
const previousBody = previousPlan ? JSON.parse(this.store.artifactStore.read(this.store.owned('artifact', previousPlan.requestArtifactId, session.id))) : null;
|
|
|
|
|
const prefix = { ...prefixAudit(body, previousBody), previousPlanId: previousPlan?.id ?? null, previousRequestSha256: previousPlan?.requestSha256 ?? null };
|
|
|
|
|
const prefix = { ...prefixAudit(body, previousBody, { schemaVersion: 2 }), previousPlanId: previousPlan?.id ?? null, previousRequestSha256: previousPlan?.requestSha256 ?? null };
|
|
|
|
|
const record = { schemaVersion: 1, id: randomUUID(), sessionId: session.id, projectId: session.projectId, taskId: run.taskId,
|
|
|
|
|
runId: run.id, stepId, requestIndex: [...this.store.core.context_plan.values()].filter(plan => plan.runId === run.id).length + 1,
|
|
|
|
|
status: 'prepared', createdAt: new Date().toISOString(), finishedAt: null,
|
|
|
|
|
requestSha256: metrics.sha256, requestBytes: metrics.bytes, estimatedInputTokens: metrics.estimatedInputTokens,
|
|
|
|
|
requestArtifactId: requestArtifact.id,
|
|
|
|
|
prefixAudit: prefix,
|
|
|
|
|
requestObservation: observation,
|
|
|
|
|
...(preserve ? { contextLayout: 'stable-prefix-1', historyPolicy: 'preserve', contextTailArtifactId: tailArtifact?.id ?? null, contextTailSha256: tailArtifact?.sha256 ?? null } : {}),
|
|
|
|
|
...(tokenReservation?{tokenReservation,maxBudgetAttempts:Math.min(8,Math.floor(credit/tokenReservation.reservedTokens))}:{engineStepOnly:true}),
|
|
|
|
|
estimateMethod: metrics.estimateMethod, beforeInputTokens: before.estimatedInputTokens, beforeBytes: before.bytes, budget,
|
|
|
|
@@ -416,7 +423,7 @@ export class RequestPlanner {
|
|
|
|
|
// sizing reserves that observed framing growth before returning bytes;
|
|
|
|
|
// the next complete request is still checked against its original limit.
|
|
|
|
|
readStateGrowthTokens: 1536,
|
|
|
|
|
originalMessages: session.providerMessages.length, retainedMessages: messages.length - 1 - tail.length - (reference ? 1 : 0) - (workingReference ? 1 : 0) - (latestReference ? 1 : 0) - (memoryReference ? 1 : 0) - (skillReference ? 1 : 0) - (mcpReference ? 1 : 0),
|
|
|
|
|
originalMessages: history.length, retainedMessages: messages.length - 1 - tail.length - (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,
|
|
|
|
|
workingSetArtifactId: workingReference?.id ?? null, workingSetSha256: workingReference?.sha256 ?? null, workingSetEntries: entries.length,
|
|
|
|
@@ -467,14 +474,21 @@ export function verifyPlanEvidence(store, session, plan, cache = new Map()) {
|
|
|
|
|
historyGroups(requestHistory(body));
|
|
|
|
|
for (const variant of requestVariants(body)) historyGroups(requestHistory(variant));
|
|
|
|
|
const frozen = JSON.parse(store.artifactStore.read(get(plan.artifactId, 'context-plan')));
|
|
|
|
|
for (const key of ['contextLayout', 'historyPolicy', 'contextTailArtifactId', 'contextTailSha256', 'prefixAudit']) {
|
|
|
|
|
for (const key of ['contextLayout', 'historyPolicy', 'contextTailArtifactId', 'contextTailSha256', 'prefixAudit', 'requestObservation']) {
|
|
|
|
|
if (JSON.stringify(frozen[key] ?? null) !== JSON.stringify(plan[key] ?? null)) throw new Error('前缀或历史策略与发送前封存不匹配。');
|
|
|
|
|
}
|
|
|
|
|
if (plan.requestObservation) {
|
|
|
|
|
const run = store.owned('run', plan.runId, session.id);
|
|
|
|
|
const expected = observeRequest(store, session, run, providerHistory(session, run).slice(0, plan.originalMessages), plan.requestObservation.toolSurface);
|
|
|
|
|
if (JSON.stringify(expected) !== JSON.stringify(plan.requestObservation)) throw new Error('消息实例或请求路径与原历史不匹配。');
|
|
|
|
|
verifyToolSurface(plan.requestObservation.toolSurface, requestVariants(body));
|
|
|
|
|
}
|
|
|
|
|
if (plan.prefixAudit) {
|
|
|
|
|
const previous = plan.prefixAudit.previousPlanId ? store.owned('context_plan', plan.prefixAudit.previousPlanId, session.id) : null;
|
|
|
|
|
if (previous && (previous.taskId !== plan.taskId || previous.id === plan.id || previous.createdAt > plan.createdAt || previous.requestSha256 !== plan.prefixAudit.previousRequestSha256)) throw new Error('前缀对照来源不匹配。');
|
|
|
|
|
if (previous && (previous.taskId !== plan.taskId || previous.id === plan.id || previous.createdAt > plan.createdAt || previous.requestSha256 !== plan.prefixAudit.previousRequestSha256
|
|
|
|
|
|| plan.requestObservation && requestPath(store.owned('run', previous.runId, session.id)).key !== plan.requestObservation.path.key)) throw new Error('前缀对照来源不匹配。');
|
|
|
|
|
const previousBody = previous ? JSON.parse(store.artifactStore.read(get(previous.requestArtifactId, 'model-request', previous.requestSha256))) : null;
|
|
|
|
|
const expected = { ...prefixAudit(body, previousBody), previousPlanId: previous?.id ?? null, previousRequestSha256: previous?.requestSha256 ?? null };
|
|
|
|
|
const expected = { ...prefixAudit(body, previousBody, { schemaVersion: plan.prefixAudit.schemaVersion }), previousPlanId: previous?.id ?? null, previousRequestSha256: previous?.requestSha256 ?? null };
|
|
|
|
|
if (JSON.stringify(expected) !== JSON.stringify(plan.prefixAudit)) throw new Error('前缀观测与原请求字节不匹配。');
|
|
|
|
|
}
|
|
|
|
|
const carriesContext = content => requestHistory(body).filter(value => typeof value.content === 'string'
|
|
|
|
@@ -565,7 +579,7 @@ export function verifyPlanEvidence(store, session, plan, cache = new Map()) {
|
|
|
|
|
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)));
|
|
|
|
|
verifyLatestToolDocument(store, session, plan, document, historyGroups(providerHistory(session, store.owned('run', plan.runId, session.id)).slice(0, plan.originalMessages)));
|
|
|
|
|
const message = latestToolMessage(artifact, document, plan.latestToolsViewVersion ?? 1);
|
|
|
|
|
if (requestHistory(body).filter(value => value.role === 'system' && value.content === message.content).length !== 1) throw new Error('最新工具反馈与实际发送内容不匹配。');
|
|
|
|
|
}
|
|
|
|
@@ -573,7 +587,7 @@ export function verifyPlanEvidence(store, session, plan, cache = new Map()) {
|
|
|
|
|
const artifact = get(plan.workingSetArtifactId, 'context-working-set', plan.workingSetSha256);
|
|
|
|
|
const document = JSON.parse(store.artifactStore.read(artifact));
|
|
|
|
|
if (artifact.taskId !== plan.taskId) throw new Error('工作依据工件任务归属不匹配。');
|
|
|
|
|
verifyWorkingDocument(store, session, plan, document, historyGroups(session.providerMessages.slice(0, plan.originalMessages)));
|
|
|
|
|
verifyWorkingDocument(store, session, plan, document, historyGroups(providerHistory(session, store.owned('run', plan.runId, session.id)).slice(0, plan.originalMessages)));
|
|
|
|
|
const message = workingMessage(artifact, document, plan.workingSetViewVersion ?? 1);
|
|
|
|
|
if (requestHistory(body).filter(value => value.role === 'system' && value.content === message.content).length !== 1) throw new Error('工作依据与实际发送内容不匹配。');
|
|
|
|
|
}
|
|
|
|
|