feat(models): retain native cache blocks and bind cross-run provider state

This commit is contained in:
Agent Workbench Dev
2026-11-07 20:00:33 +08:00
parent 71f1805561
commit 9e1feeebb3
11 changed files with 435 additions and 33 deletions
+10 -1
View File
@@ -10,6 +10,7 @@ import { modelReservation,taskConsumption,verifyModelReservation } from '../../s
import { fileContinuation } from './read-continuation.mjs';
import { prefixAudit } from './prefix-audit.mjs';
import { requestContractAudit } from '../models/request-contract-audit.mjs';
import { providerStateAudit } from '../../shared/contracts/model-provider-state.mjs';
import { observeRequest, providerHistory, requestPath } from './request-observation.mjs';
import { verifyToolSurface } from './tool-surface.mjs';
import { isPassthrough } from '../../shared/contracts/model-capacity.mjs';
@@ -425,6 +426,7 @@ export class RequestPlanner {
requestArtifactId: requestArtifact.id,
prefixAudit: prefix,
...(contractAudit ? { requestContractAudit: contractAudit } : {}),
...(config.mode === 'compatible' ? { nativeStateAudit: providerStateAudit(config, messages) } : {}),
requestObservation: observation,
...(progressReminder ? { progressReminder } : {}),
...(preserve ? { contextLayout: 'stable-prefix-1', historyPolicy: 'preserve', contextTailArtifactId: tailArtifact?.id ?? null, contextTailSha256: tailArtifact?.sha256 ?? null } : {}),
@@ -499,10 +501,17 @@ 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', 'requestContractAudit', 'requestObservation', 'progressReminder']) {
for (const key of ['contextLayout', 'historyPolicy', 'contextTailArtifactId', 'contextTailSha256', 'prefixAudit', 'requestContractAudit', 'requestObservation', 'progressReminder', 'nativeStateAudit']) {
if (JSON.stringify(frozen[key] ?? null) !== JSON.stringify(plan[key] ?? null)) throw new Error('前缀或历史策略与发送前封存不匹配。');
}
verifyReminderMessage(store, session, plan, body);
if (plan.nativeStateAudit) {
const run = store.owned('run', plan.runId, session.id);
const original = providerHistory(session, run).slice(0, plan.originalMessages);
const retained = historyGroups(original).filter(group => plan.retainedGroups.includes(group.index)).flatMap(group => group.messages);
const expected = providerStateAudit({ ...run.model, modelRouting: run.model.routing }, retained);
if (JSON.stringify(expected) !== JSON.stringify(plan.nativeStateAudit)) 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);