Elffuss BraidHub

kikocis/utopia public

A tombstone push no longer needs content, and a bad payload is not a sync failure (#229)

Closes #225

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
Changeseta5cb6ee00a00e105b2f9f6de6ab599548ed15c5063f40ee64acee74c53ce1da7
AuthorWaylandYang <145302500+WaylandYang@users.noreply.github.com> · 2026-09-03 05:53 +08:00
CommitterGitHub <noreply@github.com> · 2026-09-03 05:53 +08:00
Parents6085661b1298

Browse the files at this changeset

1 file changed

modified crates/utopia-server/src/api/sources_routes.rs +41 −10
@@ -285,6 +285,10 @@
285285 #[derive(Deserialize)]
286286 pub struct IngestBody {
287287 pub filename: String,
288+ /// 墓碑推送(`deleted: true`)可以不带 content——指南一直这么写,而字段
289+ /// 从前是必填,缺了就在反序列化那一步被拒。其余情况仍然必填:空串在
290+ /// 下面的校验里挡
291+ #[serde(default)]
288292 pub content: String,
289293 #[serde(default)]
290294 pub doc_time: Option<DateTime<Utc>>,
@@ -341,14 +345,33 @@
341345 Ok(Json(json!({ "action": action_str(action) })))
342346 }
343347
348+/// 推送失败的两种性质。**调用方发错了**和**我们这边没接住**得分开:前者记进
349+/// run 供集成调试,但不算来源同步失败——来源没坏,是那一次请求不合格;
350+/// 后者才该把来源标成 failed 并进告警中心。从前两者都走 `finish_sync(error)`,
351+/// 一次格式错误就让铃铛说"来源同步失败,没有新内容进来"
352+enum PushError {
353+ /// 4xx:负载不合格(JSON 解析、缺字段)
354+ Rejected(String),
355+ /// 摄入本身失败
356+ Failed(String),
357+}
358+
359+impl PushError {
360+ fn message(&self) -> &str {
361+ match self {
362+ PushError::Rejected(m) | PushError::Failed(m) => m,
363+ }
364+ }
365+}
366+
344367 /// 认证之后的推送处理:解析 + 校验 + 摄入/墓碑。错误一律返回文字(记进 run)。
345368 async fn handle_push(
346369 state: &AppState,
347370 source: &utopia_core::models::Source,
348371 bytes: &[u8],
349-) -> Result<crate::ingest_sources::IngestAction, String> {
350- let body: IngestBody =
351- serde_json::from_slice(bytes).map_err(|e| format!("Invalid JSON payload: {e}"))?;
372+) -> Result<crate::ingest_sources::IngestAction, PushError> {
373+ let body: IngestBody = serde_json::from_slice(bytes)
374+ .map_err(|e| PushError::Rejected(format!("Invalid JSON payload: {e}")))?;
352375 let identity = body
353376 .external_id
354377 .as_deref()
@@ -357,7 +380,9 @@
357380 .unwrap_or_else(|| body.filename.trim())
358381 .to_string();
359382 if identity.is_empty() {
360- return Err("external_id or filename is required".into());
383+ return Err(PushError::Rejected(
384+ "external_id or filename is required".into(),
385+ ));
361386 }
362387 let key = format!("api:{identity}");
363388
@@ -365,12 +390,14 @@
365390 if body.deleted {
366391 utopia_store::documents::mark_missing_keys(&state.pool, source.id, &[key])
367392 .await
368- .map_err(|e| e.to_string())?;
393+ .map_err(|e| PushError::Failed(e.to_string()))?;
369394 return Ok(crate::ingest_sources::IngestAction::Tombstoned);
370395 }
371396
372397 if body.filename.trim().is_empty() || body.content.trim().is_empty() {
373- return Err("filename and content are required".into());
398+ return Err(PushError::Rejected(
399+ "filename and content are required".into(),
400+ ));
374401 }
375402 let action = crate::ingest_sources::ingest_item(
376403 state,
@@ -383,11 +410,11 @@
383410 body.doc_time,
384411 )
385412 .await
386- .map_err(|e| e.to_string())?;
413+ .map_err(|e| PushError::Failed(e.to_string()))?;
387414 // 失而复得:曾被墓碑标记的身份再次正常推送,摘掉 missing 标记
388415 utopia_store::documents::clear_missing_keys(&state.pool, source.id, &[key])
389416 .await
390- .map_err(|e| e.to_string())?;
417+ .map_err(|e| PushError::Failed(e.to_string()))?;
391418 Ok(action)
392419 }
393420
@@ -434,10 +461,14 @@
434461 state.emit_source(source.kb_id);
435462 Ok(Json(json!({ "action": action_str(action) })))
436463 }
437- Err(msg) => {
464+ Err(err) => {
465+ let msg = err.message().to_string();
438466 utopia_store::sources::finish_run(&state.pool, run, source.id, Some(&msg), 0, 0)
439467 .await?;
440- utopia_store::sources::finish_sync(&state.pool, source.id, Some(&msg), 0).await?;
468+ // 只有我们这边没接住才算来源失败;调用方发错了留在 run 历史里就够
469+ if let PushError::Failed(_) = err {
470+ utopia_store::sources::finish_sync(&state.pool, source.id, Some(&msg), 0).await?;
471+ }
441472 state.emit_source(source.kb_id);
442473 Err(utopia_core::AppError::Validation(msg).into())
443474 }