feat(models): retain native cache blocks and bind cross-run provider state
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
import { providerStateBinding } from '../../shared/contracts/model-provider-state.mjs';
|
||||
|
||||
export const affinityScope = routing => routing.sticky === false ? 'none' : routing.affinityScope ?? 'run';
|
||||
const memberScope = member => providerStateBinding({ ...member, modelConnectionId: member.id, modelCredentialRevision: member.credentialRevision });
|
||||
|
||||
export function savedAffinity(store, routing, plan) {
|
||||
const scope = affinityScope(routing);
|
||||
if (!['task', 'session'].includes(scope) || !plan?.sessionId || !plan?.runId) return { scope, record: null };
|
||||
const session = store.session(plan.sessionId), run = store.core.run.get(plan.runId);
|
||||
if (run?.purpose === 'diagnostic' || !run || run.sessionId !== session.id) return { scope: 'run', record: null, reason: 'independent-run-path' };
|
||||
const scopeId = scope === 'task' ? plan.taskId : session.id;
|
||||
const original = session.modelAffinity;
|
||||
if (original != null && (original.schemaVersion !== 1 || !Array.isArray(original.bindings)
|
||||
|| original.bindings.some(value => !value || typeof value !== 'object' || Array.isArray(value))))
|
||||
return { scope, scopeId, session, record: null, reason: 'unknown-affinity-state', retainedUnknown: true };
|
||||
const record = original?.bindings.find(value => value.scope === scope && value.scopeId === scopeId
|
||||
&& value.routingType === routing.type && value.routingId === routing.id) ?? null;
|
||||
return { scope, scopeId, session, record };
|
||||
}
|
||||
|
||||
export function affinityPreference(store, routing, plan, members) {
|
||||
const saved = savedAffinity(store, routing, plan), record = saved.record;
|
||||
if (!record) return { ...saved, preferredId: null, reason: saved.reason ?? (saved.scope === 'none' ? 'disabled' : 'no-saved-affinity') };
|
||||
const member = members.find(value => value.id === record.connectionId);
|
||||
return member && memberScope(member).fingerprint === record.providerScopeFingerprint
|
||||
? { ...saved, preferredId: member.id, reason: 'saved-provider-scope-match' }
|
||||
: { ...saved, preferredId: null, reason: member ? 'saved-provider-scope-changed' : 'saved-member-not-eligible' };
|
||||
}
|
||||
|
||||
export function rememberAffinity(store, routing, member, plan) {
|
||||
const saved = savedAffinity(store, routing, plan);
|
||||
if (!saved.session || !saved.scopeId) return;
|
||||
if (saved.retainedUnknown) return { status: 'retained-unknown', reason: saved.reason };
|
||||
const record = { scope: saved.scope, scopeId: saved.scopeId, routingType: routing.type, routingId: routing.id,
|
||||
connectionId: member.id, model: member.model, protocol: member.protocol ?? 'chat-completions',
|
||||
providerScopeFingerprint: memberScope(member).fingerprint, sourceRunId: plan.runId,
|
||||
lastRequestId: plan.id, updatedAt: new Date().toISOString(), source: 'completed-model-response' };
|
||||
const original = saved.session.modelAffinity ?? { schemaVersion: 1, bindings: [] };
|
||||
saved.session.modelAffinity = { ...original, schemaVersion: 1, bindings: [...original.bindings.filter(value => !(value.scope === record.scope && value.scopeId === record.scopeId
|
||||
&& value.routingType === record.routingType && value.routingId === record.routingId)), record] };
|
||||
store.save(saved.session, { type: 'model.affinity_recorded', detail: { scope: record.scope, sourceRunId: plan.runId, connectionId: member.id,
|
||||
previous: saved.record, current: record } });
|
||||
return { status: 'recorded', scope: record.scope, sourceRunId: plan.runId };
|
||||
}
|
||||
@@ -19,6 +19,7 @@ import { capacitySettings, routedCapacity } from '../../shared/contracts/model-c
|
||||
import { ModelRequestLedger } from './request-ledger.mjs';
|
||||
import { modelOverrideSettings, resolveModelSpecification } from '../../shared/contracts/official-model-registry.mjs';
|
||||
import { defaultMessagesAuth, effectiveRequestContract, requestContractSettings, requestCredentialIdentity } from '../../shared/contracts/model-request-contract.mjs';
|
||||
import { affinityScope, affinityPreference, rememberAffinity } from './model-affinity.mjs';
|
||||
|
||||
const legacyId = 'legacy-default';
|
||||
const stamp = () => new Date().toISOString();
|
||||
@@ -325,8 +326,10 @@ export class ModelHub {
|
||||
seen.add(value.connectionId); return { connectionId: value.connectionId, weight: bounded(value.weight, 1, 1, 10) };
|
||||
});
|
||||
if (!['round-robin', 'weighted'].includes(input.strategy || 'round-robin')) throw error('请选择轮询或权重策略。');
|
||||
const scope = input.affinityScope ?? (previous ? previous.affinityScope ?? 'run' : 'session');
|
||||
if (!['none', 'run', 'task', 'session'].includes(scope)) throw error('请选择关闭、当前运行、当前任务或当前会话的连接保持范围。');
|
||||
const value = { id: previous?.id ?? randomUUID(), name: label(input.name, ceiling(this.store, 100)), members, strategy: input.strategy || 'round-robin', sticky: input.sticky !== false,
|
||||
maxAttempts: bounded(input.maxAttempts, 3, 1, 8), queueTimeoutMs: bounded(input.queueTimeoutMs, 15000, 100, 60000), revision: (previous?.revision ?? 0) + 1 };
|
||||
affinityScope: scope, maxAttempts: bounded(input.maxAttempts, 3, 1, 8), queueTimeoutMs: bounded(input.queueTimeoutMs, 15000, 100, 60000), revision: (previous?.revision ?? 0) + 1 };
|
||||
this.state.pools = [...this.state.pools.filter(item => item.id !== value.id), value]; this.save(); return value;
|
||||
}
|
||||
select(input, config = null) {
|
||||
@@ -363,7 +366,7 @@ export class ModelHub {
|
||||
modelConnectionId: members[0].id, modelCredentialRevision: members[0].credentialRevision,
|
||||
...routedCapacity(config, members),
|
||||
modelRouting: { type: selection.type, id: selection.id, revision: pool?.revision ?? members[0].revision,
|
||||
strategy: pool?.strategy ?? 'round-robin', sticky: pool?.sticky ?? true, maxAttempts: pool?.maxAttempts ?? 1, queueTimeoutMs: pool?.queueTimeoutMs ?? 15000, members } };
|
||||
strategy: pool?.strategy ?? 'round-robin', sticky: pool?.sticky ?? true, affinityScope: pool?.affinityScope ?? 'run', maxAttempts: pool?.maxAttempts ?? 1, queueTimeoutMs: pool?.queueTimeoutMs ?? 15000, members } };
|
||||
}
|
||||
available(member) {
|
||||
const current = this.connections().find(value => value.id === member.id);
|
||||
@@ -372,7 +375,7 @@ export class ModelHub {
|
||||
const health = this.health.get(this.identity(member));
|
||||
return unrestricted(this.store) || (!member.requiresKey || !!this.credential(member)?.key) && !health?.blocked && !(health?.cooldownUntil > Date.now());
|
||||
}
|
||||
async acquire(routing, runId, excluded, signal, deadline, requirements = [], { probe = false } = {}) {
|
||||
async acquire(routing, runId, excluded, signal, deadline, requirements = [], { probe = false, plan = null } = {}) {
|
||||
if (!unrestricted(this.store) && this.waiting >= 100) throw error('模型请求队列已满,请稍后继续任务。');
|
||||
this.waiting++;
|
||||
try {
|
||||
@@ -386,17 +389,21 @@ export class ModelHub {
|
||||
: '模型池暂时没有可用成员;已完成操作保留,请核对凭据、停用状态和冷却时间。'), { routingDecision: decision });
|
||||
const ready = healthy.filter(value => unrestricted(this.store) || (value.concurrency === 0 || (this.leases.get(value.id) ?? 0) < value.concurrency));
|
||||
if (ready.length) {
|
||||
const remembered = routing.sticky && this.sticky.get(runId);
|
||||
const affinity = affinityPreference(this.store, routing, plan, ready);
|
||||
const remembered = affinityScope(routing) !== 'none' && (this.sticky.get(runId) ?? affinity.preferredId);
|
||||
let selected = ready.find(value => value.id === remembered);
|
||||
if (!selected) {
|
||||
const weighted = ready.flatMap(value => Array(routing.strategy === 'weighted' ? value.weight : 1).fill(value));
|
||||
const cursor = this.cursors.get(routing.id) ?? 0; selected = weighted[cursor % weighted.length]; this.cursors.set(routing.id, cursor + 1);
|
||||
}
|
||||
this.leases.set(selected.id, (this.leases.get(selected.id) ?? 0) + 1);
|
||||
this.sticky.set(runId, selected.id); if (this.sticky.size > 1000) this.sticky.delete(this.sticky.keys().next().value);
|
||||
if (affinityScope(routing) !== 'none') this.sticky.set(runId, selected.id);
|
||||
let released = false;
|
||||
decision.selectedConnectionId = selected.id;
|
||||
decision.selectedBy = remembered === selected.id ? 'sticky' : routing.strategy;
|
||||
decision.affinity = { scope: affinity.scope, reason: affinity.reason, previousConnectionId: affinity.record?.connectionId ?? null,
|
||||
sourceRunId: affinity.record?.sourceRunId ?? null, selectedConnectionId: selected.id,
|
||||
continued: remembered === selected.id, source: affinity.preferredId === selected.id ? 'saved-completed-response' : remembered === selected.id ? 'current-run' : 'routing-strategy' };
|
||||
decision.candidates.find(value => value.connectionId === selected.id).disposition = 'selected';
|
||||
return { member: selected, decision, release: () => { if (!released) { released = true; this.leases.set(selected.id, Math.max(0, (this.leases.get(selected.id) ?? 0) - 1)); } } };
|
||||
}
|
||||
@@ -420,7 +427,7 @@ export class ModelHub {
|
||||
signal?.throwIfAborted();
|
||||
beforeDispatch?.({previousFailure:lastError?.diagnostic});
|
||||
let lease;
|
||||
try { lease = await this.acquire(routing, runId, excluded, signal, deadline, requirements, { probe: config.capabilityProbe === true }); }
|
||||
try { lease = await this.acquire(routing, runId, excluded, signal, deadline, requirements, { probe: config.capabilityProbe === true, plan }); }
|
||||
catch (cause) {
|
||||
if (!lastError && cause.routingDecision) {
|
||||
this.addAttempt({ id: randomUUID(), requestId, taskId: plan?.taskId ?? null, runId, sessionId: plan?.sessionId ?? null, projectId: plan?.projectId ?? null,
|
||||
@@ -439,6 +446,8 @@ export class ModelHub {
|
||||
requestBytes: Buffer.byteLength(bytes, 'utf8'), requirements, routingDecision: lease.decision,
|
||||
rejections: lease.decision.candidates.filter(value => value.disposition === 'excluded').flatMap(value => value.reasons.map(reason => ({ connectionId: value.connectionId, connectionName: value.connectionName, ...reason }))),
|
||||
requestSha256: createHash('sha256').update(bytes).digest('hex'), usage: null, usageStatus: 'unknown' };
|
||||
if (plan?.nativeStateAudit) attempt.nativeStateAudit = { source: 'frozen-plan-provider-scope-for-selected-request', requestPlanId: plan.id,
|
||||
...structuredClone(plan.nativeStateAudit.variants.find(value => value.connectionId === member.id) ?? {}) };
|
||||
try {
|
||||
if (plan?.sessionId) {
|
||||
const session = this.store.session(plan.sessionId), run = this.store.owned('run', plan.runId, session.id);
|
||||
@@ -490,6 +499,12 @@ export class ModelHub {
|
||||
attempt.status = 'completed'; attempt.usage = result.usage ?? attempt.usage; attempt.usageStatus = attempt.usage ? 'reported' : 'unknown';
|
||||
if (result.compatibility?.length) attempt.compatibility = result.compatibility;
|
||||
this.health.set(this.identity(member), { state: 'responded', lastSuccessAt: stamp(), cooldownUntil: 0, failures: 0 });
|
||||
if (!config.capabilityProbe) {
|
||||
// Continuity is optional metadata. A persistence failure cannot turn a
|
||||
// completed upstream response into a failed/replayed model request.
|
||||
try { const saved = rememberAffinity(this.store, routing, member, plan); if (saved) attempt.affinityPersistence = saved; }
|
||||
catch (cause) { attempt.affinityPersistence = { status: 'unknown', reason: this.provider.redact(cause.message) }; }
|
||||
}
|
||||
return result;
|
||||
} catch (cause) {
|
||||
const diagnostic = cause.diagnostic;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { unrestricted, ceiling } from '../../shared/contracts/execution-policy.mjs';
|
||||
import { ModelRequestError } from '../../server/diagnostics.mjs';
|
||||
import { withTokenAccounting } from '../../shared/contracts/model-usage.mjs';
|
||||
import { bindProviderState } from '../../shared/contracts/model-provider-state.mjs';
|
||||
export const protocolFailure = (message, kind = 'protocol', extra = null) => new ModelRequestError({ kind, source: 'model_connection', origin: 'local', stage: 'model_request',
|
||||
title: '模型响应未通过完整性核对', message, retryable: false, occurredAt: new Date().toISOString(), ...(extra ? extra : {}) });
|
||||
|
||||
@@ -94,18 +95,19 @@ export function normalizeProtocolResponse(config, value) {
|
||||
if ((value.stop_reason === 'tool_use') !== (calls.length > 0)) throw protocolFailure('Messages 停止原因与工具内容不一致。');
|
||||
if (!calls.length && !texts.join('').trim()) throw protocolFailure('Messages 返回为空:没有可用文本或工具调用。原响应及用量已保留,请核对供应商响应。');
|
||||
return { choices: [{ message: { role: 'assistant', content: texts.join(''), ...(calls.length ? { tool_calls: calls } : {}),
|
||||
providerState: { protocol: 'anthropic-messages', endpoint: config.baseUrl, model: config.model, responseId: value.id, content: structuredClone(value.content) } },
|
||||
providerState: bindProviderState(config, { protocol: 'anthropic-messages', endpoint: config.baseUrl, model: config.model, responseId: value.id, content: structuredClone(value.content) }) },
|
||||
finish_reason: calls.length ? 'tool_calls' : 'stop' }], usage: reportedUsage(config, value) };
|
||||
}
|
||||
if (config.protocol !== 'responses') {
|
||||
const received = value?.choices?.[0]?.message;
|
||||
if (received?.reasoning_content === undefined || received.reasoning_content === null) return { ...value, usage: reportedUsage(config, value) };
|
||||
const normalized = structuredClone(value), target = normalized?.choices?.[0]?.message;
|
||||
if (target) { delete target.providerState; delete target.providerInput; }
|
||||
if (received?.reasoning_content === undefined || received.reasoning_content === null) return { ...normalized, usage: reportedUsage(config, value) };
|
||||
if (typeof received.reasoning_content !== 'string') throw protocolFailure('Chat reasoning_content必须为字符串,原响应未用于执行工具。');
|
||||
const normalized = structuredClone(value), target = normalized.choices[0].message;
|
||||
normalized.usage = reportedUsage(config, value);
|
||||
delete target.reasoning_content;
|
||||
target.providerState = { protocol: 'chat-completions', endpoint: config.baseUrl, model: config.model,
|
||||
connectionId: config.modelConnectionId ?? null, credentialRevision: config.modelCredentialRevision ?? null, reasoningContent: received.reasoning_content };
|
||||
target.providerState = bindProviderState(config, { protocol: 'chat-completions', endpoint: config.baseUrl, model: config.model,
|
||||
connectionId: config.modelConnectionId ?? null, credentialRevision: config.modelCredentialRevision ?? null, reasoningContent: received.reasoning_content });
|
||||
return normalized;
|
||||
}
|
||||
if (value.status === 'incomplete' && value.incomplete_details?.reason === 'max_output_tokens') throw outputTruncatedFailure();
|
||||
@@ -127,5 +129,5 @@ export function normalizeProtocolResponse(config, value) {
|
||||
}
|
||||
const usage = reportedUsage(config, value);
|
||||
return { choices: [{ message: { role: 'assistant', content: texts.join(''), ...(calls.length ? { tool_calls: calls } : {}),
|
||||
providerState: { protocol: 'responses', endpoint: config.baseUrl, model: config.model, responseId: value.id ?? null, items: value.output } }, finish_reason: calls.length ? 'tool_calls' : 'stop' }], usage };
|
||||
providerState: bindProviderState(config, { protocol: 'responses', endpoint: config.baseUrl, model: config.model, responseId: value.id ?? null, items: structuredClone(value.output) }) }, finish_reason: calls.length ? 'tool_calls' : 'stop' }], usage };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user