Repository navigation
fix(agent-runtime): stamp the session id on self-hosted endpoints - #1640
Conversation
pi-ai supplies a conversation id on its own only where a vendor published one: the Responses adapters send it on every request, and the Completions and Anthropic Messages adapters send it only while `compat.sendSessionAffinityHeaders` is set, which defaults to OpenRouter. The Completions body key is narrower still, gated on `api.openai.com`. A provider row the user pointed at their own gateway or relay therefore sent every session of one credential with nothing to tell them apart, so that gateway could neither pin a conversation to one backend nor isolate its prompt cache. Stamp that identity for a row whose base URL host is not a published vendor host, on the Completions and Anthropic Messages wire APIs: the built model gains the compat that makes the adapter write `x-session-affinity` and `x-client-request-id` beside `session_id`, and a Completions payload carrying no `prompt_cache_key` gains the session id as one. The key is appended after every other payload hook and rides a copy, so an extension that rewrites the payload cannot drop it and a caller's hook keeps its own object. That default is spread under the row's own `compat`, so a catalog entry that already answers the question still wins. A published vendor host, the OpenCode path's own `x-opencode-session`, and the Responses adapters' existing behavior keep their current request bytes. Refs #1634
|
独立复核了一遍(head 复核结论1. 核心模块( 2. 接线完整。 3. 文档同步到位(§6.3 新增 + 相邻段落修正 + E2E 计划 + D658 + zh-CN 镜像),测试含 mutation 验证的"先红后绿"声明。 一个覆盖范围问题,建议作者裁量v2-share 在 #1634 补充了一条 responses 线路(非 chat_completions)的自建网关场景:nginx 默认
因此:本 PR 的 compat 路线( 小注(非阻塞)
|
Custom Responses endpoints already carry an underscored session header, but common reverse proxies drop it. Add the stable x-session-affinity alias at the shared request-options seam while preserving the adapter's existing headers and cache key. Keep the behavior limited to non-published endpoints and cover session turns plus compaction summaries, subagents, and plugin one-shots.
|
感谢你把自建端点的会话标识缺口补进主请求、子代理、压缩摘要和插件一次性调用;我复核后确认原始问题确实存在,Chat Completions 与 Anthropic 路径的修复方向也成立。 同时,关联 issue 后续补充的 Responses + nginx 场景也已纳入本 PR:自定义 已在合并最新 main 的候选上跑完 agent-runtime 全量测试(98 文件、1389 项)、TypeScript 检查,以及中英文文档校验(85 对、574 页),均通过。新头提交的 GitHub 检查尚未显示;等检查完成后我会继续处理合入。再次感谢你的贡献。 |
|
感谢你提交这项修复并配合补充覆盖。复核确认自建端点确实缺少稳定的会话亲和标识;我补上了 Responses + nginx 丢弃下划线请求头时使用的 更新到最新 main 的分支无冲突。最终验证通过:agent-runtime 98 个文件 / 1389 项测试、类型检查、文档校验,以及 PR CI(JS 全量、Rust、Windows transcript E2E、Trusted Extensions Electron E2E);PR base 检查也通过。已合入 main,merge commit |
问题
chat_completions端点的出网请求不带任何会话级标识:请求头只有Authorization与User-Agent,请求体没有prompt_cache_key。自建网关或中转因此无法把一个会话的多轮请求固定到同一后端实例,也没有按会话隔离 prompt cache 的依据(issue #1634)。根因在 pi-ai 适配器的默认值:
openai-completions只在compat.sendSessionAffinityHeaders为真时才写会话头,而该值默认为isOpenRouter;请求体的prompt_cache_key则硬编码在api.openai.com上。anthropic-messages的自定义端点存在同一处缺口。改动
对 base URL 主机不属于任何已发布厂商主机(
NAMED_ENDPOINT_PRESETS)的 provider 行,在openai-completions与anthropic-messages上由 PI-Desktop 自己补上会话身份:x-session-affinity、x-client-request-id、session_id(sessionAffinityFormat: "openai",与 Responses 线路的现有行为一致)prompt_cache_key(仅openai-completions;Anthropic 侧没有对应的请求体字段)头给网关做会话亲和,体给上游做缓存隔离——两者解决的不是同一个问题,所以两条都补。
覆盖会话回合、子智能体回合、上下文压缩摘要,以及拿到会话 id 的插件一次性调用。已发布厂商主机与 OpenCode 线路(自带
x-opencode-session)的出网字节不变;行自身的compat仍然优先。packages/agent-runtime/src/session-affinity.ts(新增)packages/agent-runtime/src/provider-binding.tscompatruntime.ts/subagent-model-binding.ts/one-shot-complete.ts/compaction-request.tsonPayload之后packages/shared/src/provider-presets.tsaddressesPublishedVendorHost(),按主机判定端点归属规格
docs/spec/03-runtime/02-agent-runtime.md— 新增 §6.3,并修正 §6.2 相邻段落里已不成立的表述docs/spec/03-runtime/11-provider-model-system.mddocs/spec/06-delivery/04-e2e-test-plan.md— 新增E2E-PROVIDER-custom-endpoint-session-identity,并修正 E2E-084 中「其他线协议保持不变」这句docs/spec/08-meta/decisions-log.md— D658验证
已跑:
pnpm --filter @pi-desktop/agent-runtime test— 1372 通过 / 1 跳过(98 文件)pnpm --filter @pi-desktop/shared test— 1266 通过(98 文件)两个包
tsc --noEmit干净;check-architecture.mjs、check-agent-policy-sync.mjs通过node scripts/e2e-smoke.mjs— 23 / 23 通过,2 跳过(跳过项需要PI_DESKTOP_TEST_API_KEY)node scripts/e2e-agent-live.mjs— 真实DesktopAgentRuntime跑完整回合,端点指向本地捕获网关:PASS。网关侧收到的唯一一个请求,四个位置同值:另用 Reqable 独立抓包复核:两个不同会话的上述四处字段随会话变化、同一会话多轮之间保持不变。
新增测试均以 mutation 方式验证过「先红后绿」(例如把判定函数改成恒 false、把
enabled门去掉、把 64 字符截断改成 63、把会话 id 的trim去掉,各自都有对应用例变红)。未跑:
说明
https://api.deepseek.com/v1这类带路径的写法仍被视为官方端点。session-affinity.ts一个地方。Closes #1634