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
+21 -6
View File
@@ -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;