Elffuss BraidHub

kikocis/utopia public

Being turned away by a quota is worth telling someone about (#176)

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Changesetf54c9bcf46f3afb141d9bb6b00fef792160e4e2e819a1fab752636846859a376
AuthorWaylandYang <145302500+WaylandYang@users.noreply.github.com> · 2026-09-02 16:35 +08:00
CommitterGitHub <noreply@github.com> · 2026-09-02 16:35 +08:00
Parentse403707743da

Browse the files at this changeset

4 files changed

modified crates/utopia-server/src/alerting.rs +78 −8
@@ -19,8 +19,8 @@
1919 /// 就是同一件事发三遍——重试本来就是为了不惊动人,报出去等于把它的意义抵消掉。
2020 /// 一个任务至多一条告警,不需要任何去重状态。
2121 ///
22-/// 判据是错误链里有没有 [`utopia_llm::Unreachable`],**不匹配错误文本**——
23-/// 调用链上任何一层加一句 context 都会改文本。
22+/// 判据是错误的**类型**,不匹配错误文本——调用链上任何一层加一句 context
23+/// 都会改文本。分类本身抽在 [`alert_for`]。
2424 pub async fn observe_job_failure(
2525 state: &AppState,
2626 job: &utopia_store::jobs::Job,
@@ -29,16 +29,16 @@
2929 if job.attempts < job.max_attempts {
3030 return;
3131 }
32- if !utopia_llm::is_unreachable(err) {
32+ let Some((kind, severity)) = alert_for(err) else {
3333 return;
34- }
35- // 系统级:哪个库的任务撞上的不重要,端点是整个部署共用的
34+ };
35+ // 系统级:哪个库的任务撞上的不重要,端点与配额是整个部署共用的
3636 if let Err(e) = alerts::raise(
3737 &state.pool,
3838 alerts::NewAlert {
3939 kb_id: None,
40- severity: "error",
41- kind: alerts::kind::LLM_UNREACHABLE,
40+ severity,
41+ kind,
4242 min_role: Role::Admin,
4343 subject_type: Some("system"),
4444 subject_id: None,
@@ -47,12 +47,31 @@
4747 )
4848 .await
4949 {
50- tracing::warn!(error = %e, "上报 llm.unreachable 失败");
50+ tracing::warn!(error = %e, %kind, "上报告警失败");
5151 return;
5252 }
5353 state.emit_alert();
5454 }
5555
56+/// 一次任务失败该报哪一类告警,报不报。
57+///
58+/// **抽成纯函数是为了能测**:`observe_job_failure` 要 `AppState` 和一个真数据库,
59+/// 而这里要守的是分类本身——限流不能被当成端点不可达,反之亦然。两者该做的事
60+/// 完全不同:一个去查网络或地址,一个去降并发或升配额。
61+///
62+/// **限流排在前面。** 两者今天互斥,但顺序反过来的话,将来谁给 `Unreachable`
63+/// 加一层包装就会把限流吞进去,而症状是这条告警再也不出现——没有任何测试会红。
64+fn alert_for(err: &anyhow::Error) -> Option<(&'static str, &'static str)> {
65+ if utopia_llm::rate_limited(err).is_some() {
66+ // `warning` 不是 `error`:配额会自己恢复,端点挂了不会
67+ return Some((alerts::kind::LLM_RATE_LIMITED, "warning"));
68+ }
69+ if utopia_llm::is_unreachable(err) {
70+ return Some((alerts::kind::LLM_UNREACHABLE, "error"));
71+ }
72+ None
73+}
74+
5675 /// 每天清一次过期告警。
5776 pub fn spawn_retention_sweep(state: AppState) {
5877 tokio::spawn(async move {
@@ -112,3 +131,54 @@
112131 }
113132 state.emit_alert();
114133 }
134+
135+#[cfg(test)]
136+mod tests {
137+ use super::alert_for;
138+ use utopia_llm::RateLimited;
139+ use utopia_store::alerts::kind;
140+
141+ /// 限流与端点不可达是两类事,报的告警必须分开。
142+ ///
143+ /// 混成一条的后果不是"标签不好看":`llm.unreachable` 说的是端点连不上,
144+ /// 管理员会去查网络和地址,而真正该做的是降并发或者升配额——**告警把人
145+ /// 指向了错误的地方**,比不报还费时间。
146+ #[test]
147+ fn a_rate_limit_is_not_an_unreachable_endpoint() {
148+ let err = anyhow::Error::new(RateLimited {
149+ status: 429,
150+ retry_after: None,
151+ detail: "TPM limit reached".into(),
152+ });
153+ assert_eq!(alert_for(&err), Some((kind::LLM_RATE_LIMITED, "warning")));
154+ }
155+
156+ /// **判定要穿透 context 层。** 抽取那条路上至少加了一句
157+ /// 「限流退避 5 次仍未通过」,靠文本匹配的判定当天就废。
158+ #[test]
159+ fn it_survives_context_layers() {
160+ let err = anyhow::Error::new(RateLimited {
161+ status: 429,
162+ retry_after: None,
163+ detail: "slow down".into(),
164+ })
165+ .context("限流退避 5 次仍未通过")
166+ .context("extract_document 失败");
167+ assert_eq!(alert_for(&err), Some((kind::LLM_RATE_LIMITED, "warning")));
168+ }
169+
170+ /// 反面:普通失败不该报告警,否则每一次解析错都弹一条,人会学会忽略它。
171+ #[test]
172+ fn an_ordinary_failure_raises_nothing() {
173+ let err = anyhow::anyhow!("结果解析失败").context("抽取失败");
174+ assert_eq!(alert_for(&err), None);
175+ }
176+
177+ /// 反面:干净的 4xx 不是限流也不是不可达。密钥错了要换密钥,
178+ /// 而那既不该降并发也不该查网络。
179+ #[test]
180+ fn an_auth_failure_raises_nothing() {
181+ let err = anyhow::anyhow!("LLM request failed (401 Unauthorized): bad key");
182+ assert_eq!(alert_for(&err), None);
183+ }
184+}
modified crates/utopia-store/src/alerts.rs +13 −1
@@ -25,8 +25,20 @@
2525 /// 库级:某个来源同步失败。`min_role = editor`
2626 pub const SOURCE_SYNC_FAILED: &str = "source.sync_failed";
2727 /// 系统级:模型端点没给出可用的回答——连不上,或者连上了但回来的不是这个 API。
28- /// 端点干净地回 4xx 不算:那说明它就是模型 API,只是密钥或配额不对
28+ /// 端点干净地回 4xx 不算:那说明它就是模型 API,只是密钥或配额不对。
29+ /// 配额那一种见 [`LLM_RATE_LIMITED`]
2930 pub const LLM_UNREACHABLE: &str = "llm.unreachable";
31+ /// 系统级:端点在限流,退避重试用尽后仍然过不去。`min_role = admin`
32+ ///
33+ /// **与 [`LLM_UNREACHABLE`] 分开,因为该做的事不同**:端点不可达要去查
34+ /// 网络或地址,配额打满要去降并发或升档,找的人和动作都不一样。
35+ ///
36+ /// severity 是 `warning` 不是 `error`:配额会自己恢复,端点挂了不会。
37+ ///
38+ /// 它补的是「退避重试」留下的那一半。重试之后不再丢数据,但一篇文档
39+ /// 真的被配额挡在外面时,没有这条告警就没有任何人知道——
40+ /// 实测一次跑测里 4 篇失败,一半是不可达(有告警),一半是限流(静默)。
41+ pub const LLM_RATE_LIMITED: &str = "llm.rate_limited";
3042 /// 库级:数据源挂上了,它的库表结构却没摄进来。`min_role = admin`
3143 ///
3244 /// **这一条描述的不是那次失败,是它留下的状态**:源挂着,而问数看不见它有
modified web/src/i18n/en.ts +4 −0
@@ -158,6 +158,10 @@
158158 title: "The model endpoint gave no usable answer",
159159 hint: "Extraction and embedding are stopped. Check the endpoint URL in system settings.",
160160 },
161+ "llm.rate_limited": {
162+ title: "The model endpoint is rate limiting us",
163+ hint: "Documents were retried and still turned away, so some are missing facts. Lower model concurrency in system settings, or raise the quota on the account.",
164+ },
161165 } as Record<string, { title: string; hint: string } | undefined>,
162166 // 没见过的 kind 也要能显示:新告警源上线时前端可能还没更新
163167 unknownKind: (kind: string) => kind,
modified web/src/i18n/zh.ts +4 −0
@@ -140,6 +140,10 @@
140140 title: "模型端点没有给出可用的回答",
141141 hint: "抽取与向量化已停摆。去系统设置里检查端点地址。",
142142 },
143+ "llm.rate_limited": {
144+ title: "模型端点在限流",
145+ hint: "退避重试之后仍然被挡回来,有文档因此缺了事实。去系统设置里降低模型并发,或者把账号配额升一档。",
146+ },
143147 } as Record<string, { title: string; hint: string } | undefined>,
144148 unknownKind: (kind: string) => kind,
145149 },