Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,32 @@ fn filter_by_key_scope(
.with_code("model_not_found"))
}

/// 转发成功的「记住这个会话的承载账号」出口 —— **三条成功路径共用这一个**。
///
/// 抽出来的理由不是省几行:粘性的写入曾经在自定义提供商那条路径上整条漏掉
/// (issue #2 / 上游 #443 —— v3.0.8 的 `attempt_custom` 成功分支里没有
/// `sticky_bind`,自定义家转发的会话因此永远不粘,而请求日志里只看得到
/// `stickyHit:false`、没有任何报错)。三条路径各写一遍 `sticky_bind`,就还会
/// 有第四次的遗漏;写成唯一出口之后,「新增一条转发路径」与「粘性写没写」
/// 不再需要靠人记着对上。
///
/// 判据(空会话键 / TTL 档位 / 清除代次)全部在 `StickySessions::bind` 里,
/// 本函数只把「此刻的配置」换算成 TTL —— 与选路侧 `routing::sticky_ttl_ms`
/// 同一来源,免得写与读各用一个档位。
fn remember_session(
service: &UpstreamService,
conv_key: &str,
model: &str,
account_id: &str,
source: &str,
generation: u64,
) {
let ttl = config::routing_settings().sticky.ttl_ms().unwrap_or(0);
service
.route
.sticky_bind(conv_key, model, account_id, source, logging::now_ms(), ttl, generation);
}

/// 账号循环:每一轮从候选池里按全局优先级选一个账号,用它所属家的适配器发一次。
///
/// 按 `is_stateful` 分流(架构文档 §4.2.1):
Expand Down Expand Up @@ -762,7 +788,15 @@ async fn attempt_queue(
// 决策快照挂进 telemetry(最后一次为准:请求日志的「路由回放」显示
// 的是最终承载那次选路的完整排序 —— 与 attempts 明细的换号链互补,
// 那边回答「试了谁」,这边回答「当时为什么这么排」)。
if let Some(decision) = target.decision.clone() {
if let Some(mut decision) = target.decision.clone() {
// 会话键的**来源标签**(哪条提取链生效):路由回放要能回答「这次
// 请求的会话键是哪个字段给的」。只记标签,不记原始键值。
if let Some(object) = decision.as_object_mut() {
object.insert(
"stickySource".to_string(),
Value::String(sticky_source.to_string()),
);
}
ctx.telemetry.set_route_decision(decision);
}
// 连接计数改绑到这一轮选中的账号:失败重试换账号时计数跟着走,
Expand All @@ -781,9 +815,8 @@ async fn attempt_queue(
match attempt_custom(service, ctx, target, slot, connections, degraded).await {
Ok(outcome) => {
if let Some(account_id) = custom_account_id.as_deref() {
service.route.sticky_bind(
&conv_key, &model, account_id, sticky_source, logging::now_ms(),
config::routing_settings().sticky.ttl_ms().unwrap_or(0), sticky_generation,
remember_session(
service, &conv_key, &model, account_id, sticky_source, sticky_generation,
);
service.route.backoff_clear(account_id);
service.route.sink_clear(account_id);
Expand Down Expand Up @@ -929,9 +962,8 @@ async fn attempt_queue(
{
Ok(outcome) => {
if let Some(account_id) = stateful_account_id.as_deref() {
service.route.sticky_bind(
&conv_key, &model, account_id, sticky_source, logging::now_ms(),
config::routing_settings().sticky.ttl_ms().unwrap_or(0), sticky_generation,
remember_session(
service, &conv_key, &model, account_id, sticky_source, sticky_generation,
);
service.route.backoff_clear(account_id);
service.route.sink_clear(account_id);
Expand Down Expand Up @@ -1395,15 +1427,12 @@ async fn attempt_queue(
);
}
}
if let (Some(_ttl), true, Some(account_id)) = (
route_settings.sticky.ttl_ms(),
!conv_key.is_empty(),
target.account_id.as_deref(),
) {
service.route.sticky_bind(
&conv_key, &model, account_id, sticky_source,
logging::now_ms(), route_settings.sticky.ttl_ms().unwrap_or(0),
sticky_generation,
if let Some(account_id) = target.account_id.as_deref() {
// 判据(空键 / TTL 档位 / 清除代次)在 `StickySessions::bind`
// 里,这里不再重复一遍 —— 三处各写一份闸门正是此前自定义
// 家成功分支整条漏掉绑定的成因(issue #2)。
remember_session(
service, &conv_key, &model, account_id, sticky_source, sticky_generation,
);
}
// Antigravity 的会话键随信封带出(签名缓存兜底档的写入键,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,33 @@ fn single(name: &str) -> Vec<String> {
vec![name.to_string()]
}

/// 一份临时库 + 指定 provider 下的若干账号(`uids` 按给定顺序落库)。
///
/// 与 [`temp_service`] 的差别只有两处:provider 可指定、账号可多于一条。
/// 后者是选路用例必须的 —— 只有一条账号时「粘住」与「没粘住」的结论相同,
/// 断言不出任何东西。
fn temp_service_for(provider: &str, uids: &[&str]) -> (TempDbDir, UpstreamService) {
use crate::server::core::account_store::AccountStore;
use crate::server::core::auth::AuthService;
use crate::server::db::Db;

let seq = TEMP_SEQ.fetch_add(1, Ordering::SeqCst);
let dir = std::env::temp_dir().join(format!("pl-sticky-{}-{seq}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
let store = AccountStore::with_db(Some(Db::open(&dir.join("agent2api.db")).expect("临时库应当能建起来")));
for uid in uids {
store
.add_account(
&json!({"auth": {"accessToken": "at"}, "account": {"uid": uid}}),
None,
Some(provider),
)
.expect("临时账号应当能建起来");
}
let service = UpstreamService::new(store.clone(), AuthService::for_store(store));
(TempDbDir(dir), service)
}

#[test]
fn stateful_quota_bookkeeping_keeps_the_adapter_reset_at() {
let (_dir, service, id) = temp_service();
Expand Down Expand Up @@ -429,3 +456,66 @@ fn a_deterministic_404_skips_the_in_place_resend() {
"404 的豁免不许顺手把 400 也豁免了"
);
}

// ── 会话粘性的写入 → 查找闭环(issue #2)──────────────────────────────
//
// 这一组用例钉的是「失败了也不会报错」的那半边:写入侧(成功后
// `sticky_bind`)与查找侧(选路时 `snapshot_for_model`)必须用**同一把键**、
// **同一个 TTL 口径**。任何一侧漏了 model、时间戳或账号 id 的换算,症状都只是
// `route.stickyHit` 恒为 false(请求照样成功、账号照样轮换),不会抛错。

/// 同一会话键的第二条请求必须命中第一条绑定的账号。
///
/// 账号挂在 `custom-` 前缀的提供商下:那类账号不受「插件是否已安装」的可见性
/// 闸门约束(见 `account_store::account_visible`),于是这条用例在任何测试环境
/// 里都能走到真实的 `select_target_account` 选路,而不是被候选池为空提前短路。
#[tokio::test]
async fn a_second_request_on_the_same_session_sticks_to_the_first_account() {
use crate::server::core::routing::CooldownKeys;
use crate::server::core::upstream::rotate::select_target_account;
use crate::server::core::upstream::route_state::RouteState;
use axum::http::HeaderMap;

let provider = "custom-sticky-test";
let (_dir, service) = temp_service_for(provider, &["sticky-1", "sticky-2"]);
let body = json!({
"model": "sticky-model",
"messages": [{ "role": "user", "content": "hello" }],
});
let key = RouteState::conversation_key(&HeaderMap::new(), &body);
assert!(!key.is_empty(), "带 messages 的请求必须有会话键,否则粘性永远不生效");
let keys = CooldownKeys::new("sticky-model");

// 第一条:没有绑定,正常选路(转发成功后由 attempt_queue 调 sticky_bind)
let first = select_target_account(&service, &[provider], &keys, &[], None, &key)
.await
.expect("第一条请求应当选到账号");
assert_eq!(
first.decision.as_ref().map(|decision| decision["stickyHit"].clone()),
Some(json!(false)),
"首次请求不该命中粘性"
);
let bound = first.account_id.clone().expect("应当选到有 id 的账号");

// 与转发成功出口逐字同源的写入
let ttl = config::routing_settings().sticky.ttl_ms().unwrap_or(0);
assert!(ttl > 0, "测试环境的粘性档位必须开着(默认 auto)");
service.route.sticky_bind(
&key,
"sticky-model",
&bound,
"content-hash",
logging::now_ms(),
ttl,
service.route.sticky_generation(),
);

// 第二条:同一会话、同一模型 → 命中,并且仍然用同一个账号
let second = select_target_account(&service, &[provider], &keys, &[], None, &key)
.await
.expect("第二条请求应当选到账号");
let decision = second.decision.clone().expect("选路要带决策快照");
assert_eq!(decision["stickyHit"], json!(true), "同一会话的第二条请求应当命中粘性");
assert_eq!(decision["picked"], json!(bound), "命中后应当置顶原账号");
assert_eq!(second.account_id.as_deref(), Some(bound.as_str()));
}
Loading
Loading