Skip to content

feat: Antigravity 会话粘性选号(防会话内跨账号漂移) - #410

Open
neverforget-1 wants to merge 1 commit into
su-kaka:masterfrom
neverforget-1:feat/sticky-credential-selection
Open

neverforget-1 wants to merge 1 commit into
su-kaka:masterfrom
neverforget-1:feat/sticky-credential-selection

Conversation

@neverforget-1

Copy link
Copy Markdown

背景 / Background

多账号场景下,get_valid_credential 每次请求都随机选号。同一会话的多轮对话可能在轮换后落到不同凭证上,导致:

  • 会话上下文与账号状态不一致(部分账号侧的会话性数据丢失)
  • 同一会话跨账号漂移,增加触发上游风控的概率

方案 / Solution

为 Antigravity 模式增加会话粘性选号:

  • 从请求的 sessionId(或首条用户消息的稳定哈希)派生会话指纹
  • StickySessionStore 记录 会话指纹 → 凭证 的绑定(带缓存命中)
  • 命中时优先复用上次凭证;凭证出错换号重试成功后,将绑定刷新到新凭证,避免下一轮又切回故障账号
  • 无稳定指纹信号的请求退化为原有随机选号,行为不变

改动范围 / Scope

  • src/api/antigravity.py:流式/非流式请求接入粘性选号
  • src/credential_manager.py:get_valid_credential 支持 sticky_file 参数 + StickySessionStore
  • src/storage/{sqlite,mongodb,psql}_manager.py:三个存储后端的绑定读写
  • tests/test_sticky_credential.py:新增测试

测试 / Testing

  • pytest tests/test_sticky_credential.py:2 passed
  • 绑定命中、换号后绑定刷新、无指纹退化路径均有覆盖

English TL;DR: Add session-sticky credential selection for Antigravity mode. Session fingerprint (sessionId or first-user-message hash) is bound to the last-used credential; retries after rotation refresh the binding. Requests without a stable fingerprint fall back to random selection. Includes tests for all three storage backends.

同一会话固定使用同一上游凭证:提高上游上下文缓存命中,
并消除同一场对话 sessionId 出现在多个账号上的风控信号。

- credential_manager: get_valid_credential 支持 sticky_file 优先选号;
  新增 StickySessionStore(进程内会话→凭证绑定,TTL 30 分钟,容量上限 2048)
- sqlite/psql/mongodb: get_next_available_credential 支持 preferred_filename
  优先排序(仅影响排序,不绕过 disabled/冷却/preview 过滤)
- antigravity: stream/non_stream 按会话指纹粘性选号,换号后重绑定;
  发往上游的请求字节完全不变
- 存储层 SQL 全部改为固定字符串 + 白名单值替换,DDL 逐列字面量内联
  (配合静态扫描加固,行为不变)
- 新增 tests/test_sticky_credential.py(上游 .gitignore 忽略 tests/,此处强制纳入)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant