feat(gateway): 支持幂等 capture 重试#476
Conversation
|
Thank you for submitting this PR and participating in Tencent Rhino-bird Open-source Training Program! |
YOMXXX
left a comment
There was a problem hiding this comment.
Approved.
I verified this PR locally on the branch:
pnpm vitest run src/gateway/capture-idempotency.test.ts— passed: 1 file, 7 testspnpm test— passed: 5 files, 74 testspnpm build— passedgit diff --check HEAD~1..HEAD— passed
The idempotency implementation is scoped by session key, fingerprints the request while excluding only idempotency_key, coalesces in-flight duplicate requests, rejects same-key/different-payload reuse with a conflict, and drops failed entries so retries can proceed. The bounded TTL store and validation rules look appropriate for this gateway-level retry-safety feature.
关联 Issue
Related to #235
背景与定位
#235 已确定:
本 PR 不再添加新的平台适配器,而是补充这些适配器共同依赖的 Gateway 运行时语义:
POST /capture的幂等重试保护。现有适配器讨论已经明确将
tdai_capture标记为“非幂等、有写副作用”。当客户端请求实际执行成功、但响应因超时或网络中断而丢失时,客户端无法安全重试。问题根因
Gateway 在请求没有携带
messages时,会根据user_content和assistant_content临时构造消息。这些消息没有稳定时间戳,L0 recorder 会在每次请求中重新生成
Date.now()。因此同一轮 capture 被重试时,新时间戳可能越过 checkpoint cursor,被错误识别为新的消息并再次写入。实现方案
CaptureRequest增加可选idempotency_keysession_keyidempotency_replayed: true409兼容性
未传入
idempotency_key时,继续走原有 capture 路径,行为完全不变。本 PR 不包含:
user_id租户隔离改造因此它是 #316、#372 和 #470 可以共同使用的独立 Gateway 协议增量,而不是另一套竞争性的 Adapter SDK。
文档
README 和 README_CN 增加了 retry-safe capture 的请求示例、冲突语义、容量和有效期边界。
验证
1个文件、7项测试通过5个文件、74项测试通过git diff --check:通过定向测试覆盖:
验证截图