feat(models): retain native cache blocks and bind cross-run provider state
This commit is contained in:
@@ -3,8 +3,8 @@
|
||||
2026-11-07,基线 `bdfeb29c85b3231bfd7bc24b8b5f49d40a3beebf`,干净工作树。承接 I14/I12 的冻结契约/当次 fetch 数据区分,完成 I09 原生块与工具缓存注解往返、I11 可配置会话/Task亲和与供应商状态绑定;I27/I28 CI 与 M3 单独续接。
|
||||
|
||||
1. [complete] 固定当前官方缓存/状态来源、复用 I15 原件并核SHA;8份来源清单见 tests/fixtures/native-cache-state/SOURCE.json 与 docs/NATIVE-CACHE-STATE-SOURCES.md。保留原丢注解、跨 Run 选线和状态身份缺项观察。Mnemo新结果只读登记,不作为本地验收。
|
||||
2. [in_progress] 保留 Messages 系统/用户/工具块及工具元数据;新增原生状态身份绑定和会话/Task/Run亲和,原用户设置、原快照、完整历史和未知原件不变。默认不偷偷添加缓存参数。
|
||||
3. [pending] 自有 HTTP JSON/SSE、混池、跨 Run、手选换模、修订/停用、停止/恢复/重启与所有工具结果不重放的确定性核对;上游原错误保留。
|
||||
2. [complete] 保留 Messages 系统/用户/工具块及工具元数据;新增原生状态身份绑定和会话/Task/Run亲和,修复纯Chat混池仅换model造成旧状态误用,optional亲和存储失败不改变原响应。原用户设置、完整历史和未知原件保留,默认不添加缓存参数。source-5最终16/16,源码类型检查通过。
|
||||
3. [in_progress] 自有 HTTP JSON/SSE、混池、跨 Run、手选换模、修订/停用、停止/恢复/重启与所有工具结果不重放的确定性核对;上游原错误保留。核心专项已验证,接续独立候选相关回归。
|
||||
4. [pending] 复用 Orven 模型中心/原生参数/上下文,完成真实保存恢复与宽窄网页/实际Electron验收、独立构建及逐输入/编译核对。
|
||||
5. [pending] 每阶段回顾、限定本地提交、最终证据清单与普通GitAtlas增量推送;旧封存根只读,原失败保留。
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
# 汇总发现
|
||||
|
||||
I09/I11 source-4为14/15:收窄断言后确认真实纯Chat混池缺陷,不是整正文误命中。completionBody在所有成员均无native/streaming时只构造首成员body,actualRequest只换model,旧reasoning_content进入后备连接。现有状态时必须逐成员投影,连同型号相同但凭据不同的池验证。原source-3/4失败保留;原生Messages/Responses SSE、停止续同Task与可选亲和存储故障均在source-4通过。未知亲和记录保留不重写,optional存储失败不改变已完成上游响应的结果。
|
||||
|
||||
I09/I11接续决策:新池可默认会话保持,但旧池必须保留原Run范围;亲和只来自完成的真实响应且诊断路径独立。状态身份要核对实际请求路径/认证模板/版本头/型号/连接与凭据修订,改变原生普通生成参数不应误认凭据变化。状态无法适用时保留原件和原因、以常规文本/原调用构造新历史,不丢用户数据或自动重放工具。沿既有Orven池编辑与上下文/请求账本补真实控制和反馈,不另立页面。
|
||||
|
||||
I09/I11具体缺口:messagesBody重建文本和工具时丢非结构元数据,messagesHistory反向只取text并改变tool_result顺序;Messages/Responses原生助手状态只有endpoint/model绑定,缺实际请求版本/头/凭据范围;ModelHub sticky仅runId且重启不持久。需要按来源区分声明原生输入与不透明输出状态,当前状态不能跨供应商或凭据误用,原件不能因此丢弃。
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
# 统一执行进度
|
||||
|
||||
I09/I11阶段2核心完成:source-5最终16/16、typecheck-2退出0。纯Chat状态逐成员投影已通过相同型号/不同凭据、实际停用/改头边界;原生块TTL/工具元数据、JSON/SSE签名/加密reasoning/phase、跨Run/Store与Provider重启、手选、同Task停止恢复和optional保存故障保持。原source-1/2/3/4失败原件不动。核心限定本地提交后独立构建与相关回归/两端UI继续;未发行或触正式服务。
|
||||
|
||||
I09/I11 source-4完整新15项为14过1失败:纯Chat池只换型号、误用首成员推理状态已定位并修复,新增相同型号/不同凭据的独立投影边界,下一source-5不覆盖原轮。原生JSON/SSE及phase/cache块、停止恢复不重放、unknown亲和和可选保存故障通过。额外只读调查误猜两个.d.mts路径以及rg参数放在--后均保留为工具使用错误,无产品或正式数据改变。
|
||||
|
||||
I09/I11来源提交前git diff --check发现官方原文6处原有尾空白,来源字节/SHA保持不修剪;限定排除这两份官方正文后核对其它新增文件,后续另核Git blob原字节。不是产品检查失败,不改原来源来制造通过。
|
||||
|
||||
I09/I11阶段1来源复核完成:8份官方来源固定,SOURCE.json SHA ced3703c4afb174c5b939ffb3dd8572f22ff8e5f3fb8489f627f45880315b799;OpenAI完整Docs原件复用、Claude/DeepSeek六份逐SHA一致。接续再次打开当前两篇页面,原source-1/2/3失败保留,未重跑旧轮次;首次mkdir既有目录拒绝后改为新文件名写入。下一完成核心与用户连接保持范围及确定性边界,限定来源提交先行。
|
||||
|
||||
@@ -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 };
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { isPassthrough, messagesOutputSetting } from './model-capacity.mjs';
|
||||
import { providerStateCompatibility } from './model-provider-state.mjs';
|
||||
const object = value => value && typeof value === 'object' && !Array.isArray(value);
|
||||
const fail = message => { throw new Error('Messages 请求未发送:' + message); };
|
||||
|
||||
@@ -6,9 +7,12 @@ function contentBlocks(content) {
|
||||
if (typeof content === 'string') return content ? [{ type: 'text', text: content }] : [];
|
||||
if (!Array.isArray(content)) fail('消息内容必须为文本或已接入的内容块。');
|
||||
return content.map(part => {
|
||||
if (['text', 'input_text', 'output_text'].includes(part.type) && typeof part.text === 'string') return { type: 'text', text: part.text };
|
||||
if (['text', 'input_text', 'output_text'].includes(part.type) && typeof part.text === 'string') return { ...structuredClone(part), type: 'text' };
|
||||
const url = part.type === 'image_url' ? part.image_url?.url : part.type === 'input_image' ? part.image_url : null;
|
||||
if (typeof url === 'string' && /^data:image\/png;base64,[A-Za-z0-9+/=]+$/.test(url)) return { type: 'image', source: { type: 'base64', media_type: 'image/png', data: url.slice(22) } };
|
||||
if (typeof url === 'string' && /^data:image\/png;base64,[A-Za-z0-9+/=]+$/.test(url)) {
|
||||
const { image_url, ...metadata } = structuredClone(part);
|
||||
return { ...metadata, type: 'image', source: { type: 'base64', media_type: 'image/png', data: url.slice(22) } };
|
||||
}
|
||||
fail('未接入的输入内容块 ' + String(part?.type ?? '未知') + '。');
|
||||
});
|
||||
}
|
||||
@@ -23,14 +27,23 @@ export function messagesBody(config, messages, tools, native) {
|
||||
else wire.push({ role, content });
|
||||
};
|
||||
for (const message of messages) {
|
||||
if (message.providerInput?.protocol === 'anthropic-messages') {
|
||||
const original = message.providerInput;
|
||||
if (message.role === 'system') system.push(...structuredClone(original.content));
|
||||
else if (message.role === 'tool') append('user', [structuredClone(original.toolResult)]);
|
||||
else append(message.role, structuredClone(original.content));
|
||||
continue;
|
||||
}
|
||||
if (message.role === 'system') { system.push(...contentBlocks(message.content)); continue; }
|
||||
if (message.role === 'tool') {
|
||||
if (typeof message.tool_call_id !== 'string' || !message.tool_call_id || typeof message.content !== 'string') fail('工具结果缺少原调用身份或文本。');
|
||||
append('user', [{ type: 'tool_result', tool_use_id: message.tool_call_id, content: message.content }]);
|
||||
append('user', [{ type: 'tool_result', tool_use_id: message.tool_call_id, content: message.content,
|
||||
...(message.cache_control !== undefined ? { cache_control: structuredClone(message.cache_control) } : {}),
|
||||
...(message.is_error !== undefined ? { is_error: message.is_error } : {}) }]);
|
||||
} else if (message.role === 'user') append('user', contentBlocks(message.content));
|
||||
else if (message.role === 'assistant') {
|
||||
const state = message.providerState;
|
||||
if (state?.protocol === 'anthropic-messages' && state.endpoint === config.baseUrl && state.model === config.model) {
|
||||
if (state?.protocol === 'anthropic-messages' && providerStateCompatibility(config, state).applicable) {
|
||||
if (!Array.isArray(state.content)) fail('原生助手历史格式无效。');
|
||||
append('assistant', structuredClone(state.content));
|
||||
continue;
|
||||
@@ -46,8 +59,10 @@ export function messagesBody(config, messages, tools, native) {
|
||||
}
|
||||
if (system.some(block => block.type !== 'text')) fail('系统消息只支持文本。');
|
||||
const definitions = (tools ?? []).map(tool => {
|
||||
if (typeof tool.name === 'string' && object(tool.input_schema)) return structuredClone(tool);
|
||||
if (tool.type !== 'function' || !tool.function?.name || !object(tool.function.parameters)) fail('工具定义格式无效。');
|
||||
return { name: tool.function.name, ...(tool.function.description ? { description: tool.function.description } : {}), input_schema: tool.function.parameters };
|
||||
const { type, function: definition, ...metadata } = structuredClone(tool), { parameters, ...fields } = definition;
|
||||
return { ...fields, ...metadata, name: definition.name, input_schema: parameters };
|
||||
});
|
||||
return { model: config.model, ...(system.length ? { system } : {}), messages: wire,
|
||||
...(definitions.length ? { tools: definitions, tool_choice: { type: 'auto' } } : {}),
|
||||
@@ -61,14 +76,25 @@ export function messagesBody(config, messages, tools, native) {
|
||||
export function messagesHistory(body) {
|
||||
const history = [];
|
||||
if (typeof body.system === 'string') history.push({ role: 'system', content: body.system });
|
||||
else for (const part of body.system ?? []) history.push({ role: 'system', content: part.text });
|
||||
else if (body.system?.length) history.push({ role: 'system', content: body.system.filter(part => part.type === 'text').map(part => part.text).join(''),
|
||||
providerInput: { protocol: 'anthropic-messages', content: structuredClone(body.system) } });
|
||||
for (const message of body.messages) {
|
||||
if (!Array.isArray(message.content)) { history.push(message); continue; }
|
||||
const blocks = message.content, text = blocks.filter(block => block.type === 'text').map(block => block.text).join('');
|
||||
const calls = blocks.filter(block => block.type === 'tool_use').map(block => ({ id: block.id, type: 'function', function: { name: block.name, arguments: JSON.stringify(block.input) } }));
|
||||
const results = blocks.filter(block => block.type === 'tool_result');
|
||||
for (const result of results) history.push({ role: 'tool', tool_call_id: result.tool_use_id, content: result.content });
|
||||
if (message.role === 'assistant' || text || blocks.some(block => block.type === 'image')) history.push({ role: message.role, content: text, ...(calls.length ? { tool_calls: calls } : {}) });
|
||||
if (!Array.isArray(message.content)) { history.push(structuredClone(message)); continue; }
|
||||
let pending = [];
|
||||
const flush = () => {
|
||||
if (!pending.length) return;
|
||||
const text = pending.filter(block => block.type === 'text').map(block => block.text).join('');
|
||||
const calls = pending.filter(block => block.type === 'tool_use').map(block => ({ id: block.id, type: 'function', function: { name: block.name, arguments: JSON.stringify(block.input) } }));
|
||||
history.push({ role: message.role, content: text, ...(calls.length ? { tool_calls: calls } : {}),
|
||||
providerInput: { protocol: 'anthropic-messages', content: structuredClone(pending) } });
|
||||
pending = [];
|
||||
};
|
||||
for (const block of message.content) {
|
||||
if (block.type !== 'tool_result') { pending.push(block); continue; }
|
||||
flush(); history.push({ role: 'tool', tool_call_id: block.tool_use_id, content: typeof block.content === 'string' ? block.content : JSON.stringify(block.content),
|
||||
providerInput: { protocol: 'anthropic-messages', toolResult: structuredClone(block) } });
|
||||
}
|
||||
flush();
|
||||
}
|
||||
return history;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
import { createHash } from 'node:crypto';
|
||||
import { modelProtocol } from './model-capabilities.mjs';
|
||||
import { modelRequestUrl } from './model-endpoint.mjs';
|
||||
import { requestHeaders } from './model-request-contract.mjs';
|
||||
|
||||
const sha = value => createHash('sha256').update(JSON.stringify(value)).digest('hex');
|
||||
export function providerStateBinding(config) {
|
||||
const protocol = modelProtocol(config.protocol), connection = { ...config, protocol };
|
||||
const identity = { protocol, endpoint: config.baseUrl ?? null, requestUrl: config.baseUrl ? modelRequestUrl(connection) : null,
|
||||
model: config.model ?? null, connectionId: config.modelConnectionId ?? null, credentialRevision: config.modelCredentialRevision ?? null,
|
||||
headers: [...requestHeaders(connection, '{{credential}}')] };
|
||||
return { schemaVersion: 1, ...identity, fingerprint: sha(identity), representation: 'provider-state-scope-credential-template-v1' };
|
||||
}
|
||||
export const bindProviderState = (config, payload) => ({ ...payload, schemaVersion: 2, binding: providerStateBinding(config) });
|
||||
|
||||
export function providerStateCompatibility(config, state) {
|
||||
if (!state) return { applicable: false, reason: 'no-state' };
|
||||
const binding = providerStateBinding(config);
|
||||
if (state.schemaVersion !== undefined && ![1, 2].includes(state.schemaVersion)) return { applicable: false, reason: 'unknown-state-schema', binding };
|
||||
if (state.schemaVersion === 2) return state.binding?.fingerprint === binding.fingerprint
|
||||
? { applicable: true, reason: 'same-provider-scope', binding }
|
||||
: { applicable: false, reason: 'provider-scope-changed', binding, previousFingerprint: state.binding?.fingerprint ?? null };
|
||||
if (state.protocol !== binding.protocol || state.endpoint !== binding.endpoint || state.model !== binding.model)
|
||||
return { applicable: false, reason: 'legacy-provider-changed', binding };
|
||||
if (state.protocol === 'chat-completions') return state.connectionId === binding.connectionId && state.credentialRevision === binding.credentialRevision
|
||||
? { applicable: true, reason: 'legacy-chat-identity-match', binding }
|
||||
: { applicable: false, reason: 'legacy-provider-changed', binding };
|
||||
// Original unbound direct-protocol histories remain readable. A named current
|
||||
// connection cannot infer authentication/version scope from an old bare state.
|
||||
return binding.connectionId === null && binding.credentialRevision === null
|
||||
? { applicable: true, reason: 'legacy-direct-protocol', binding }
|
||||
: { applicable: false, reason: 'legacy-identity-unavailable', binding };
|
||||
}
|
||||
|
||||
export function providerStateAudit(config, messages) {
|
||||
const variants = config.modelRouting?.members ?? [config];
|
||||
return { schemaVersion: 1, source: 'frozen-history-provider-scope', variants: variants.map(member => {
|
||||
const route = config.modelRouting ? { ...config, ...member, modelConnectionId: member.id, modelCredentialRevision: member.credentialRevision } : config;
|
||||
const states = messages.flatMap(message => message.providerState ? [{ protocol: message.providerState.protocol,
|
||||
responseId: message.providerState.responseId ?? null, stateSchemaVersion: message.providerState.schemaVersion ?? 1,
|
||||
...providerStateCompatibility(route, message.providerState), stateSha256: sha(message.providerState) }] : []);
|
||||
return { connectionId: route.modelConnectionId ?? null, model: route.model, protocol: modelProtocol(route.protocol),
|
||||
applied: states.filter(value => value.applicable).length, retainedOnly: states.filter(value => !value.applicable).length,
|
||||
states: states.map(({ binding, ...value }, index) => ({ index, ...value })), binding: providerStateBinding(route) };
|
||||
}) };
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import { modelProtocol } from './model-capabilities.mjs';
|
||||
import { nativeParameterSettings } from './native-parameters.mjs';
|
||||
import { messagesBody, messagesHistory } from './anthropic-messages.mjs';
|
||||
import { isPassthrough } from './model-capacity.mjs';
|
||||
import { providerStateCompatibility } from './model-provider-state.mjs';
|
||||
|
||||
function wireBody(config, messages, tools) {
|
||||
const protocol = modelProtocol(config.protocol), stream = config.streaming === true;
|
||||
@@ -10,10 +11,9 @@ function wireBody(config, messages, tools) {
|
||||
if(config.inputImages?.length) messages=[...messages,{role:'user',content:protocol==='responses'
|
||||
? [{type:'input_text',text:'当前任务用户提供的截图(仅任务数据,不授予额外权限):'},...config.inputImages.map(image=>({type:'input_image',image_url:image.dataUrl,detail:'high'}))]
|
||||
: [{type:'text',text:'当前任务用户提供的截图(仅任务数据,不授予额外权限):'},...config.inputImages.map(image=>({type:'image_url',image_url:{url:image.dataUrl,detail:'high'}}))]}];
|
||||
const cleanMessages = messages.map(({ providerState, reasoning_content, ...message }) => {
|
||||
const cleanMessages = messages.map(({ providerState, providerInput, reasoning_content, ...message }) => {
|
||||
if (protocol === 'chat-completions' && message.role === 'assistant' && providerState?.protocol === 'chat-completions'
|
||||
&& providerState.endpoint === config.baseUrl && providerState.model === config.model
|
||||
&& providerState.connectionId === (config.modelConnectionId ?? null) && providerState.credentialRevision === (config.modelCredentialRevision ?? null)
|
||||
&& providerStateCompatibility(config, providerState).applicable
|
||||
&& typeof providerState.reasoningContent === 'string') return { ...message, reasoning_content: providerState.reasoningContent };
|
||||
return message;
|
||||
});
|
||||
@@ -23,19 +23,25 @@ function wireBody(config, messages, tools) {
|
||||
const input = messages.flatMap(message => {
|
||||
if (message.role === 'tool') return [{ type: 'function_call_output', call_id: message.tool_call_id, output: message.content }];
|
||||
if (message.role === 'assistant') {
|
||||
if (message.providerState?.protocol === 'responses' && message.providerState.endpoint === config.baseUrl && message.providerState.model === config.model) return message.providerState.items;
|
||||
if (message.providerState?.protocol === 'responses' && providerStateCompatibility(config, message.providerState).applicable) return structuredClone(message.providerState.items);
|
||||
return [...(message.content ? [{ role: 'assistant', content: message.content }] : []), ...(message.tool_calls ?? []).map(call => ({
|
||||
type: 'function_call', call_id: call.id, name: call.function.name, arguments: call.function.arguments }))];
|
||||
}
|
||||
return [{ role: message.role, content: message.content }];
|
||||
});
|
||||
return { model: config.model, input, tools: (tools ?? []).map(tool => ({ type: 'function', ...tool.function, strict: false })),
|
||||
return { model: config.model, input, tools: (tools ?? []).map(tool => tool.function
|
||||
? { ...structuredClone(tool.function), ...Object.fromEntries(Object.entries(structuredClone(tool)).filter(([key]) => key !== 'function')), strict: tool.strict ?? tool.function.strict ?? false }
|
||||
: structuredClone(tool)),
|
||||
tool_choice: 'auto', ...(!isPassthrough(config) ? { max_output_tokens: config.maxOutputTokens } : {}), stream, store: false, include: ['reasoning.encrypted_content'], ...native.nativeParameters };
|
||||
}
|
||||
|
||||
export function completionBody(config, messages, tools) {
|
||||
const members = config.modelRouting?.members;
|
||||
if (members?.length && (config.capacityMode === 'model' || members.some(member => member.protocol && member.protocol !== 'chat-completions' || member.streaming === true || member.nativeParameters || member.outputTokenField))) {
|
||||
// Even plain Chat members need separate projections when an opaque history
|
||||
// exists. Replacing only body.model would reuse the first member's reasoning
|
||||
// on another connection, credential or model.
|
||||
if (members?.length && (config.capacityMode === 'model' || messages.some(message => message.providerState)
|
||||
|| members.some(member => member.protocol && member.protocol !== 'chat-completions' || member.streaming === true || member.nativeParameters || member.outputTokenField))) {
|
||||
return { schema: 1, kind: 'model-route-plan', messages, variants: members.map(member => ({ connectionId: member.id,
|
||||
protocol: modelProtocol(member.protocol), body: wireBody({ ...config, ...member, nativeParameters: member.nativeParameters ?? {}, outputTokenField: member.outputTokenField,
|
||||
modelConnectionId: member.id, modelCredentialRevision: member.credentialRevision,
|
||||
|
||||
@@ -0,0 +1,248 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import http from 'node:http';
|
||||
import { completionBody, requestHistory, actualRequest } from '../src/shared/contracts/model-request.mjs';
|
||||
import { messagesBody, messagesHistory } from '../src/shared/contracts/anthropic-messages.mjs';
|
||||
import { normalizeProtocolResponse } from '../src/core/models/protocols.mjs';
|
||||
import { providerStateAudit, providerStateCompatibility } from '../src/shared/contracts/model-provider-state.mjs';
|
||||
import { fixture, completion, call } from './helpers.mjs';
|
||||
import { Provider } from '../src/server/provider.mjs';
|
||||
import { Store } from '../src/server/store.mjs';
|
||||
import { Runtime } from '../src/server/runtime.mjs';
|
||||
import { verifyPlanEvidence } from '../src/core/context/request-plan.mjs';
|
||||
|
||||
const open = { mode: 'compatible', executionMode: 'unrestricted', approvalMode: 'full', capacityMode: 'passthrough', historyPolicy: 'preserve', maxSteps: 0, maxDurationMs: 0, maxTokens: 0, maxTaskTokens: 0 };
|
||||
const messagesConfig = { ...open, protocol: 'anthropic-messages', model: 'claude-opus-5-5', baseUrl: 'https://api.anthropic.com', modelConnectionId: 'a', modelCredentialRevision: 2 };
|
||||
const cache = { type: 'ephemeral', ttl: '1h', vendor_unknown: { keep: true } };
|
||||
const tool = { type: 'function', cache_control: cache, defer_loading: true, function: { name: 'owned', description: '原工具描述', strict: true, parameters: { type: 'object', properties: {} } } };
|
||||
const response = protocol => protocol === 'anthropic-messages'
|
||||
? { id: 'owned-message', type: 'message', role: 'assistant', model: 'claude-opus-5-5', content: [{ type: 'thinking', thinking: 'private', signature: 'owned-signature' }, { type: 'text', text: '完成', cache_control: cache }], stop_reason: 'end_turn', usage: { input_tokens: 1, output_tokens: 1 } }
|
||||
: { id: 'owned-response', status: 'completed', output: [{ type: 'reasoning', id: 'owned-reasoning', encrypted_content: 'owned-encrypted' }, { type: 'message', role: 'assistant', content: [{ type: 'output_text', text: '完成' }] }], usage: { input_tokens: 1, output_tokens: 1, total_tokens: 2 } };
|
||||
|
||||
test('Messages preserves system/user text cache annotations and complete native tool metadata without mutating the canonical inputs', () => {
|
||||
const messages = [{ role: 'system', content: [{ type: 'text', text: '固定规则', cache_control: cache, citations: [] }] }, { role: 'user', content: [{ type: 'input_text', text: '问题', cache_control: cache, source: { kind: 'owned' } }] }];
|
||||
const original = structuredClone({ messages, tool });
|
||||
const body = completionBody(messagesConfig, messages, [tool]);
|
||||
assert.deepEqual(body.system[0], messages[0].content[0]); assert.deepEqual(body.messages[0].content[0], { ...messages[1].content[0], type: 'text' });
|
||||
assert.deepEqual(body.tools[0], { name: 'owned', description: '原工具描述', strict: true, defer_loading: true, cache_control: cache, input_schema: tool.function.parameters });
|
||||
assert.deepEqual({ messages, tool }, original); assert.equal(body.cache_control, undefined);
|
||||
});
|
||||
|
||||
test('Messages request history round-trip preserves native block order, tool_result metadata and independent TTLs', () => {
|
||||
const body = { ...completionBody(messagesConfig, [], []), system: [{ type: 'text', text: '规则', cache_control: cache }], tools: [{ name: 'owned', input_schema: { type: 'object' }, cache_control: cache }],
|
||||
messages: [{ role: 'assistant', content: [{ type: 'thinking', thinking: 'opaque', signature: 'sig' }, { type: 'tool_use', id: 'call-own', name: 'owned', input: {}, cache_control: { type: 'ephemeral', ttl: '5m' } }] },
|
||||
{ role: 'user', content: [{ type: 'tool_result', tool_use_id: 'call-own', content: [{ type: 'text', text: '原结果', cache_control: cache }], is_error: true, cache_control: cache }, { type: 'text', text: '补充', cache_control: { type: 'ephemeral', ttl: '5m' } }] }] };
|
||||
const history = messagesHistory(body), original = structuredClone(history);
|
||||
const restored = messagesBody(messagesConfig, history, body.tools, { nativeParameters: {} });
|
||||
assert.deepEqual(restored.system, body.system); assert.deepEqual(restored.messages, body.messages); assert.deepEqual(restored.tools, body.tools); assert.deepEqual(history, original);
|
||||
const chat = completionBody({ ...messagesConfig, protocol: 'chat-completions' }, history, []);
|
||||
assert.ok(chat.messages.every(value => value.providerInput === undefined && value.providerState === undefined)); assert.ok(!JSON.stringify(chat).includes('opaque'));
|
||||
});
|
||||
|
||||
test('Responses explicit cache breakpoints, tool metadata and top-level controls keep independent upstream fields', () => {
|
||||
const config = { ...open, model: 'gpt-6.1-sol', protocol: 'responses', baseUrl: 'https://api.openai.com/v1', nativeParameters: { prompt_cache_options: { mode: 'explicit', ttl: '30m' }, prompt_cache_key: 'owned-session', store: false } };
|
||||
const messages = [{ role: 'developer', content: [{ type: 'input_text', text: '固定规则', prompt_cache_breakpoint: { mode: 'explicit', ttl: '12h' } }] }];
|
||||
const body = completionBody(config, messages, [tool]); assert.deepEqual(body.input[0], messages[0]); assert.deepEqual(body.prompt_cache_options, config.nativeParameters.prompt_cache_options);
|
||||
assert.equal(body.tools[0].strict, true); assert.deepEqual(body.tools[0].cache_control, cache); assert.equal(body.tools[0].defer_loading, true); assert.equal(body.max_output_tokens, undefined);
|
||||
});
|
||||
|
||||
test('plain Chat pool members with the same model project opaque history by their own credential and request scope', () => {
|
||||
const config = { ...open, protocol: 'chat-completions', model: 'same-model', baseUrl: 'https://owned.invalid/v1', modelConnectionId: 'a', modelCredentialRevision: 2 };
|
||||
const members = [{ id: 'a', model: config.model, protocol: config.protocol, baseUrl: config.baseUrl, credentialRevision: 2 },
|
||||
{ id: 'b', model: config.model, protocol: config.protocol, baseUrl: config.baseUrl, credentialRevision: 3 }];
|
||||
const message = normalizeProtocolResponse(config, { choices: [{ message: { role: 'assistant', content: '原答复', reasoning_content: 'opaque-owned-first' }, finish_reason: 'stop' }] }).choices[0].message;
|
||||
const original = structuredClone(message), body = completionBody({ ...config, modelRouting: { members } }, [message], []);
|
||||
assert.equal(actualRequest(body, members[0]).messages[0].reasoning_content, 'opaque-owned-first');
|
||||
assert.equal(actualRequest(body, members[1]).messages[0].reasoning_content, undefined);
|
||||
assert.equal(actualRequest(body, members[1]).messages[0].content, '原答复'); assert.deepEqual(message, original);
|
||||
});
|
||||
|
||||
for (const protocol of ['anthropic-messages', 'responses', 'chat-completions']) test(protocol + ' opaque output state binds actual URL, version/headers, model, connection and credential without discarding historical data', () => {
|
||||
const config = { ...messagesConfig, protocol, requestContract: { schemaVersion: 1, defaultsVersion: '2026-11-07.1', anthropicVersion: '2023-06-01', anthropicBetas: [], headers: { 'OpenAI-Project': 'owned-original' }, auth: 'default', completionPath: null, modelsPath: null, versionSource: 'explicit' } };
|
||||
const raw = protocol === 'chat-completions' ? { ...completion([], '完成'), choices: [{ message: { role: 'assistant', content: '完成', reasoning_content: 'owned-private' }, finish_reason: 'stop' }] } : response(protocol);
|
||||
const message = normalizeProtocolResponse(config, raw).choices[0].message, original = structuredClone(message);
|
||||
assert.equal(message.providerState.schemaVersion, 2); assert.equal(providerStateCompatibility(config, message.providerState).applicable, true);
|
||||
const same = completionBody(config, [message], []); assert.ok(JSON.stringify(same).includes(protocol === 'anthropic-messages' ? 'owned-signature' : protocol === 'responses' ? 'owned-encrypted' : 'owned-private'));
|
||||
for (const patch of [{ model: 'different' }, { modelConnectionId: 'b' }, { modelCredentialRevision: 3 }, { baseUrl: 'https://other.invalid/v1' },
|
||||
{ requestContract: { ...config.requestContract, completionPath: '/different/messages' } }, { requestContract: { ...config.requestContract, headers: { 'OpenAI-Project': 'different' } } },
|
||||
...(protocol === 'anthropic-messages' ? [{ requestContract: { ...config.requestContract, anthropicVersion: '2026-10-21' } }] : [])]) {
|
||||
const changed = { ...config, ...patch }; assert.equal(providerStateCompatibility(changed, message.providerState).applicable, false);
|
||||
const body = completionBody(changed, [message], []); assert.ok(!/owned-(signature|encrypted|private)/.test(JSON.stringify(body)));
|
||||
assert.equal(providerStateAudit(changed, [message]).variants[0].retainedOnly, 1);
|
||||
}
|
||||
assert.deepEqual(message, original); assert.deepEqual(raw, protocol === 'chat-completions' ? raw : response(protocol));
|
||||
});
|
||||
|
||||
const untilIdle = async (runtime, session) => { const deadline = Date.now() + 25000; while (runtime.isActive(session.id)) { if (Date.now() > deadline) throw Error('Owned runtime did not finish'); await new Promise(resolve => setTimeout(resolve, 25)); } };
|
||||
async function ownedServer(t, fn) {
|
||||
const requests = [], server = http.createServer(async (req, res) => { const chunks=[]; for await (const chunk of req) chunks.push(chunk); const body=JSON.parse(Buffer.concat(chunks)); requests.push({ path:req.url,headers:req.headers,body }); await fn(body,res,requests.length); });
|
||||
await new Promise(resolve => server.listen(0,'127.0.0.1',resolve)); t.after(()=>new Promise(resolve=>{server.closeAllConnections();server.close(resolve);}));
|
||||
return { requests, baseUrl:'http://127.0.0.1:'+server.address().port+'/v1' };
|
||||
}
|
||||
|
||||
test('session affinity survives actual independent Runs and Store/Provider restart while raw reasoning and completed tool IDs remain exact', async t => {
|
||||
const f=fixture(t); f.store.saveConfig(open);
|
||||
let firstCall=true;
|
||||
const a=await ownedServer(t,(body,res)=>{const output=completion(firstCall?[call('owned-original-call','list_files',{path:'',depth:1})]:[],'完成'); firstCall=false; output.choices[0].message.reasoning_content='owned-reasoning-'+body.messages.length; res.writeHead(200,{'Content-Type':'application/json'});res.end(JSON.stringify(output));});
|
||||
const b=await ownedServer(t,(body,res)=>{res.writeHead(200,{'Content-Type':'application/json'});res.end(JSON.stringify(completion([],'备用完成')));});
|
||||
const provider=new Provider(),hub=provider.modelHub(f.store),memberA=hub.upsert({name:'Owned A',model:'owned-a',baseUrl:a.baseUrl,requiresKey:false}),memberB=hub.upsert({name:'Owned B',model:'owned-b',baseUrl:b.baseUrl,requiresKey:false});
|
||||
const pool=hub.pool({name:'Owned session affinity',members:[{connectionId:memberA.id,weight:1},{connectionId:memberB.id,weight:1}],sticky:true}); assert.equal(pool.affinityScope,'session'); hub.select({type:'pool',id:pool.id});
|
||||
const runtime=new Runtime(f.store,provider); t.after(()=>runtime.stopAll()); runtime.start(f.session.id,'读取工程'); await untilIdle(runtime,f.session); assert.equal(f.session.status,'completed',JSON.stringify(f.session.failure));
|
||||
assert.equal(a.requests.length,2); assert.equal(b.requests.length,0); const toolCount=f.store.core.tool.size,history=structuredClone(f.session.providerMessages),firstRun=f.session.runId;
|
||||
runtime.start(f.session.id,'继续检查原结果'); await untilIdle(runtime,f.session); assert.equal(a.requests.length,3); assert.equal(b.requests.length,0); assert.notEqual(f.session.runId,firstRun); assert.equal(f.store.core.tool.size,toolCount);
|
||||
assert.deepEqual(f.session.providerMessages.slice(0,history.length),history);
|
||||
for(const plan of f.store.core.context_plan.values()) assert.doesNotThrow(()=>verifyPlanEvidence(f.store,f.session,plan));
|
||||
const affinity=structuredClone(f.session.modelAffinity),storedHistory=structuredClone(f.session.providerMessages);f.store.close();const reopened=new Store(f.data);f.onCleanup(()=>reopened.close()); const session=reopened.session(f.session.id); assert.deepEqual(session.providerMessages,storedHistory);assert.deepEqual(session.modelAffinity,affinity);
|
||||
const restoredProvider=new Provider(),restoredHub=restoredProvider.modelHub(reopened),restoredRuntime=new Runtime(reopened,restoredProvider);t.after(()=>restoredRuntime.stopAll());
|
||||
restoredHub.cursors.set(pool.id,1);restoredRuntime.start(session.id,'继续核对原记录');await untilIdle(restoredRuntime,session);assert.equal(a.requests.length,4);assert.equal(b.requests.length,0);assert.equal(reopened.core.tool.size,toolCount);
|
||||
const latest=restoredHub.view().attempts.find(value=>value.runId===session.runId);assert.equal(latest.routingDecision.affinity.source,'saved-completed-response');assert.equal(latest.routingDecision.affinity.continued,true);
|
||||
for(const plan of reopened.core.context_plan.values()) assert.doesNotThrow(()=>verifyPlanEvidence(reopened,session,plan));
|
||||
});
|
||||
|
||||
test('task affinity ends with its Task; explicit manual selection wins and old pools keep their original Run scope', async t => {
|
||||
const f=fixture(t);f.store.saveConfig(open);const model=await ownedServer(t,(body,res)=>{res.writeHead(200,{'Content-Type':'application/json'});res.end(JSON.stringify(completion([],'完成')));});
|
||||
const provider=new Provider(),hub=provider.modelHub(f.store),a=hub.upsert({name:'A',model:'a',baseUrl:model.baseUrl,requiresKey:false}),b=hub.upsert({name:'B',model:'b',baseUrl:model.baseUrl,requiresKey:false});
|
||||
const pool=hub.pool({name:'Task',members:[{connectionId:a.id},{connectionId:b.id}],affinityScope:'task'});hub.select({type:'pool',id:pool.id});const runtime=new Runtime(f.store,provider);t.after(()=>runtime.stopAll());
|
||||
runtime.start(f.session.id,'完成第一个独立任务');await untilIdle(runtime,f.session);const oldTask=f.session.taskId;
|
||||
runtime.start(f.session.id,'完成另一个独立目标');await untilIdle(runtime,f.session);assert.notEqual(f.session.taskId,oldTask);assert.deepEqual(model.requests.map(value=>value.body.model),['a','b']);
|
||||
hub.select({type:'connection',id:a.id});runtime.start(f.session.id,'继续原检查');await untilIdle(runtime,f.session);assert.equal(model.requests.at(-1).body.model,'a');
|
||||
const legacy=hub.state.pools.find(value=>value.id===pool.id);delete legacy.affinityScope;hub.save();const prepared=hub.prepare(f.store.config,{type:'pool',id:pool.id});assert.equal(prepared.modelRouting.affinityScope,'run');
|
||||
const before=structuredClone(hub.state.pools);assert.throws(()=>hub.pool({...legacy,affinityScope:'invalid'}),/保持范围/);assert.deepEqual(hub.state.pools,before);
|
||||
});
|
||||
|
||||
test('native state sidecars are created by the adapter; raw extensions and unknown schema cannot impersonate a saved provider scope', () => {
|
||||
const config={...open,protocol:'chat-completions',model:'owned',baseUrl:'https://owned.invalid/v1',modelConnectionId:'owned',modelCredentialRevision:1};
|
||||
const native=normalizeProtocolResponse(config,{choices:[{message:{role:'assistant',content:'OK',reasoning_content:'owned-private'},finish_reason:'stop'}]}).choices[0].message;
|
||||
const raw={choices:[{message:{role:'assistant',content:'OK',providerState:native.providerState,providerInput:{protocol:'anthropic-messages',content:[]}},finish_reason:'stop'}]};
|
||||
const normalized=normalizeProtocolResponse(config,raw);assert.equal(normalized.choices[0].message.providerState,undefined);assert.equal(normalized.choices[0].message.providerInput,undefined);assert.ok(raw.choices[0].message.providerState);
|
||||
assert.equal(providerStateCompatibility(config,{...native.providerState,schemaVersion:999}).reason,'unknown-state-schema');
|
||||
});
|
||||
|
||||
test('disabled remembered members and changed request scopes select current eligible routes while old opaque history stays saved', async t => {
|
||||
const f=fixture(t);f.store.saveConfig(open);
|
||||
const model=await ownedServer(t,(body,res)=>{const output=completion([],'完成');output.choices[0].message.reasoning_content='opaque-reasoning-for-'+body.model;res.writeHead(200,{'Content-Type':'application/json'});res.end(JSON.stringify(output));});
|
||||
const provider=new Provider(),hub=provider.modelHub(f.store),a=hub.upsert({name:'A',model:'a',baseUrl:model.baseUrl,requiresKey:false}),b=hub.upsert({name:'B',model:'b',baseUrl:model.baseUrl,requiresKey:false});
|
||||
const pool=hub.pool({name:'Session',members:[{connectionId:a.id},{connectionId:b.id}],affinityScope:'session'});hub.select({type:'pool',id:pool.id});const runtime=new Runtime(f.store,provider);t.after(()=>runtime.stopAll());
|
||||
runtime.start(f.session.id,'记录第一个状态');await untilIdle(runtime,f.session);const old=structuredClone(f.session.providerMessages);assert.equal(model.requests[0].body.model,'a');
|
||||
hub.toggle(a.id,false);runtime.start(f.session.id,'继续已有任务');await untilIdle(runtime,f.session);assert.equal(model.requests[1].body.model,'b');assert.ok(model.requests[1].body.messages.every(message=>message.reasoning_content===undefined));
|
||||
assert.deepEqual(f.session.providerMessages.slice(0,old.length),old);
|
||||
const switched=hub.view().attempts.find(value=>value.runId===f.session.runId);assert.equal(switched.routingDecision.affinity.reason,'saved-member-not-eligible');assert.equal(switched.nativeStateAudit.retainedOnly,1);
|
||||
const previous=hub.connection(b.id);hub.upsert({...previous,requestContract:{...previous.requestContract,headers:{'OpenAI-Project':'new-owned'}}});
|
||||
runtime.start(f.session.id,'继续在新项目头下核对');await untilIdle(runtime,f.session);const last=model.requests.at(-1);assert.equal(last.headers['openai-project'],'new-owned');assert.ok(last.body.messages.every(message=>message.reasoning_content===undefined));
|
||||
const changed=hub.view().attempts.find(value=>value.runId===f.session.runId);assert.equal(changed.routingDecision.affinity.reason,'saved-provider-scope-changed');assert.equal(changed.nativeStateAudit.retainedOnly,2);
|
||||
for(const plan of f.store.core.context_plan.values())assert.doesNotThrow(()=>verifyPlanEvidence(f.store,f.session,plan));
|
||||
});
|
||||
|
||||
test('failed and unknown upstream requests keep a single original attempt and do not overwrite completed session affinity or dispatch tools', async t => {
|
||||
const f=fixture(t);f.store.saveConfig(open);let fail=false;
|
||||
const model=await ownedServer(t,(body,res)=>{if(fail){res.writeHead(400,{'Content-Type':'application/json','x-request-id':'owned-original-400'});res.end(JSON.stringify({error:{type:'invalid_request_error',message:'owned-original-rejection'}}));}else{res.writeHead(200,{'Content-Type':'application/json'});res.end(JSON.stringify(completion([],'完成')));}});
|
||||
const provider=new Provider(),hub=provider.modelHub(f.store),a=hub.upsert({name:'Owned',model:'owned',baseUrl:model.baseUrl,requiresKey:false}),pool=hub.pool({name:'Session',members:[{connectionId:a.id}],affinityScope:'session'});hub.select({type:'pool',id:pool.id});const runtime=new Runtime(f.store,provider);t.after(()=>runtime.stopAll());
|
||||
runtime.start(f.session.id,'取得原完成结果');await untilIdle(runtime,f.session);const saved=structuredClone(f.session.modelAffinity),history=structuredClone(f.session.providerMessages),tools=f.store.core.tool.size;
|
||||
fail=true;runtime.start(f.session.id,'继续观察上游错误');await untilIdle(runtime,f.session);assert.equal(model.requests.length,2);assert.deepEqual(f.session.modelAffinity,saved);assert.equal(f.store.core.tool.size,tools);assert.deepEqual(f.session.providerMessages.slice(0,history.length),history);
|
||||
const attempt=hub.view().attempts.find(value=>value.runId===f.session.runId);assert.equal(attempt.status,'failed');assert.equal(attempt.httpStatus,400);assert.match(JSON.stringify(attempt.diagnostic),/owned-original-rejection/);
|
||||
assert.equal(attempt.routingDecision.affinity.continued,true);
|
||||
});
|
||||
|
||||
test('unknown saved affinity is retained and optional affinity persistence cannot change a completed upstream response into an error', async t => {
|
||||
const f = fixture(t); f.store.saveConfig(open);
|
||||
const model = await ownedServer(t, (body, res) => { res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify(completion([], '完成'))); });
|
||||
const provider = new Provider(), hub = provider.modelHub(f.store);
|
||||
const member = hub.upsert({ name: 'Owned', model: 'owned', baseUrl: model.baseUrl, requiresKey: false });
|
||||
const pool = hub.pool({ name: 'Session', members: [{ connectionId: member.id }], affinityScope: 'session' }); hub.select({ type: 'pool', id: pool.id });
|
||||
const runtime = new Runtime(f.store, provider); t.after(() => runtime.stopAll());
|
||||
const unknown = { schemaVersion: 999, bindings: { original: true }, extension: ['keep-original'] };
|
||||
f.session.modelAffinity = structuredClone(unknown); f.store.save(f.session);
|
||||
runtime.start(f.session.id, '完成独立请求'); await untilIdle(runtime, f.session);
|
||||
assert.equal(f.session.status, 'completed'); assert.deepEqual(f.session.modelAffinity, unknown); assert.equal(model.requests.length, 1);
|
||||
let attempt = hub.view().attempts.find(value => value.runId === f.session.runId);
|
||||
assert.equal(attempt.routingDecision.affinity.reason, 'unknown-affinity-state'); assert.equal(attempt.affinityPersistence.status, 'retained-unknown');
|
||||
delete f.session.modelAffinity; f.store.save(f.session);
|
||||
const save = f.store.save.bind(f.store);
|
||||
f.store.save = (session, event) => { if (event?.type === 'model.affinity_recorded') throw new Error('owned-optional-affinity-write-failed'); return save(session, event); };
|
||||
runtime.start(f.session.id, '继续实际请求'); await untilIdle(runtime, f.session); f.store.save = save;
|
||||
attempt = hub.view().attempts.find(value => value.runId === f.session.runId);
|
||||
assert.equal(f.session.status, 'completed'); assert.equal(attempt.status, 'completed'); assert.equal(attempt.affinityPersistence.status, 'unknown');
|
||||
assert.match(attempt.affinityPersistence.reason, /owned-optional-affinity-write-failed/); assert.equal(model.requests.length, 2);
|
||||
});
|
||||
|
||||
test('task affinity survives a stopped response and explicit same-Task continuation without dispatching the interrupted request again', async t => {
|
||||
const f = fixture(t); f.store.saveConfig(open); let block = false;
|
||||
const a = await ownedServer(t, (body, res) => {
|
||||
if (block) { res.writeHead(200, { 'Content-Type': 'application/json' }); res.write('{'); return; }
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify(completion([], '完成')));
|
||||
});
|
||||
const b = await ownedServer(t, (body, res) => { res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify(completion([], '备用完成'))); });
|
||||
const provider = new Provider(), hub = provider.modelHub(f.store);
|
||||
const memberA = hub.upsert({ name: 'A', model: 'a', baseUrl: a.baseUrl, requiresKey: false });
|
||||
const memberB = hub.upsert({ name: 'B', model: 'b', baseUrl: b.baseUrl, requiresKey: false });
|
||||
const pool = hub.pool({ name: 'Task', members: [{ connectionId: memberA.id }, { connectionId: memberB.id }], affinityScope: 'task' });
|
||||
hub.select({ type: 'pool', id: pool.id }); const runtime = new Runtime(f.store, provider); t.after(() => runtime.stopAll());
|
||||
runtime.start(f.session.id, '完成任务'); await untilIdle(runtime, f.session);
|
||||
const taskId = f.session.taskId, original = structuredClone(f.session.modelAffinity), tools = f.store.core.tool.size;
|
||||
block = true; runtime.start(f.session.id, '', true);
|
||||
const deadline = Date.now() + 20000; while (a.requests.length < 2) { assert.ok(Date.now() < deadline); await new Promise(resolve => setTimeout(resolve, 25)); }
|
||||
await runtime.stop(f.session.id); await untilIdle(runtime, f.session);
|
||||
assert.equal(f.session.taskId, taskId); assert.deepEqual(f.session.modelAffinity, original); assert.equal(b.requests.length, 0);
|
||||
const interrupted = hub.view().attempts.find(value => value.runId === f.session.runId); assert.equal(interrupted.status, 'interrupted');
|
||||
block = false; runtime.resume(f.session.id); await untilIdle(runtime, f.session);
|
||||
assert.equal(f.session.taskId, taskId); assert.equal(a.requests.length, 3); assert.equal(b.requests.length, 0); assert.equal(f.store.core.tool.size, tools);
|
||||
const resumed = hub.view().attempts.find(value => value.runId === f.session.runId);
|
||||
assert.equal(resumed.routingDecision.affinity.source, 'saved-completed-response');
|
||||
});
|
||||
|
||||
for (const protocol of ['anthropic-messages', 'responses']) test(protocol + ' real owned SSE retains native cache blocks and opaque output across turns, then projects ordinary history after a header change', async t => {
|
||||
const f = fixture(t); f.store.saveConfig(open);
|
||||
const model = await ownedServer(t, (body, res, index) => {
|
||||
const tools = index === 1;
|
||||
const value = protocol === 'anthropic-messages'
|
||||
? { id: 'owned-native-' + index, type: 'message', role: 'assistant', model: body.model,
|
||||
content: [{ type: 'thinking', thinking: 'opaque-native', signature: 'owned-native-signature-' + index },
|
||||
{ type: 'text', text: '完成', cache_control: cache },
|
||||
...(tools ? [{ type: 'tool_use', id: 'owned-native-tool', name: 'list_files', input: { path: '', depth: 1 }, cache_control: { type: 'ephemeral', ttl: '5m' } }] : [])],
|
||||
stop_reason: tools ? 'tool_use' : 'end_turn', usage: { input_tokens: 10, output_tokens: 3, cache_read_input_tokens: 2, cache_creation_input_tokens: 1 } }
|
||||
: { id: 'owned-native-' + index, model: body.model, status: 'completed',
|
||||
output: [{ type: 'reasoning', id: 'owned-reasoning-' + index, encrypted_content: 'owned-encrypted-' + index, summary: [] },
|
||||
{ type: 'message', id: 'owned-message-' + index, role: 'assistant', phase: 'final_answer', content: [{ type: 'output_text', text: '完成', annotations: [] }] },
|
||||
...(tools ? [{ type: 'function_call', id: 'owned-call-item', call_id: 'owned-native-tool', name: 'list_files', arguments: JSON.stringify({ path: '', depth: 1 }), status: 'completed' }] : [])],
|
||||
usage: { input_tokens: 10, output_tokens: 3, input_tokens_details: { cached_tokens: 2 } } };
|
||||
const event = value => 'data: ' + JSON.stringify(value) + '\n\n';
|
||||
res.writeHead(200, { 'Content-Type': 'text/event-stream' });
|
||||
if (protocol === 'anthropic-messages') {
|
||||
res.write(event({ type: 'message_start', message: { ...value, content: [], stop_reason: null } }));
|
||||
value.content.forEach((block, index) => { res.write(event({ type: 'content_block_start', index, content_block: block })); res.write(event({ type: 'content_block_stop', index })); });
|
||||
res.write(event({ type: 'message_delta', delta: { stop_reason: value.stop_reason }, usage: value.usage })); res.end(event({ type: 'message_stop' }));
|
||||
} else {
|
||||
res.write(event({ type: 'response.created', sequence_number: 0, response: { ...value, status: 'in_progress', output: [] } }));
|
||||
res.end(event({ type: 'response.completed', sequence_number: 1, response: value }));
|
||||
}
|
||||
});
|
||||
const provider = new Provider(), hub = provider.modelHub(f.store);
|
||||
const member = hub.upsert({ name: 'Owned native', model: protocol === 'anthropic-messages' ? 'claude-opus-5-5' : 'gpt-6.1-sol',
|
||||
protocol, streaming: true, baseUrl: model.baseUrl, requiresKey: false });
|
||||
const pool = hub.pool({ name: 'Native session', members: [{ connectionId: member.id }], affinityScope: 'session' }); hub.select({ type: 'pool', id: pool.id });
|
||||
const runtime = new Runtime(f.store, provider); t.after(() => runtime.stopAll());
|
||||
runtime.start(f.session.id, '读取工程然后结束'); await untilIdle(runtime, f.session);
|
||||
assert.equal(f.session.status, 'completed', JSON.stringify(f.session.failure)); assert.equal(model.requests.length, 2);
|
||||
const second = model.requests[1].body;
|
||||
if (protocol === 'anthropic-messages') {
|
||||
const blocks = second.messages.filter(message => message.role === 'assistant').flatMap(message => message.content);
|
||||
assert.ok(blocks.some(block => block.signature === 'owned-native-signature-1'));
|
||||
assert.deepEqual(blocks.find(block => block.type === 'text' && block.text === '完成').cache_control, cache);
|
||||
assert.equal(blocks.find(block => block.type === 'tool_use').cache_control.ttl, '5m');
|
||||
} else {
|
||||
assert.ok(second.input.some(item => item.encrypted_content === 'owned-encrypted-1'));
|
||||
assert.equal(second.input.find(item => item.type === 'message' && item.role === 'assistant').phase, 'final_answer');
|
||||
assert.ok(second.input.some(item => item.type === 'function_call_output' && item.call_id === 'owned-native-tool'));
|
||||
assert.equal(second.max_output_tokens, undefined);
|
||||
}
|
||||
const old = structuredClone(f.session.providerMessages), tools = f.store.core.tool.size;
|
||||
hub.upsert({ ...hub.connection(member.id), requestContract: { ...hub.connection(member.id).requestContract, headers: { 'X-Owned-Scope': 'changed' } } });
|
||||
runtime.start(f.session.id, '追加说明然后结束'); await untilIdle(runtime, f.session);
|
||||
assert.equal(model.requests.length, 3); assert.equal(f.store.core.tool.size, tools); assert.deepEqual(f.session.providerMessages.slice(0, old.length), old);
|
||||
const changed = model.requests.at(-1).body;
|
||||
if (protocol === 'anthropic-messages') assert.ok(changed.messages.flatMap(message => message.content).every(block => !['thinking', 'redacted_thinking'].includes(block.type)));
|
||||
else assert.ok(changed.input.every(item => item.type !== 'reasoning'));
|
||||
const attempt = hub.view().attempts.find(value => value.runId === f.session.runId); assert.equal(attempt.nativeStateAudit.retainedOnly, 2);
|
||||
for (const plan of f.store.core.context_plan.values()) assert.doesNotThrow(() => verifyPlanEvidence(f.store, f.session, plan));
|
||||
});
|
||||
Reference in New Issue
Block a user