From 3da7688bdf695fe7b88ca7e6ae5d0daf25f394cd Mon Sep 17 00:00:00 2001 From: WuYongJie <549727955@qq.com> Date: Tue, 17 Nov 2026 13:30:49 +0800 Subject: [PATCH] fix(client): settle original failures with gateway error metadata --- .../apps/photocraft/src/jiansun_images.rs | 29 +++++++++++++++++-- .../apps/photocraft/src/jiansun_pi.rs | 18 +++++++++--- 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/apps/detail-photocraft/apps/photocraft/src/jiansun_images.rs b/apps/detail-photocraft/apps/photocraft/src/jiansun_images.rs index c972d186..c864f52b 100644 --- a/apps/detail-photocraft/apps/photocraft/src/jiansun_images.rs +++ b/apps/detail-photocraft/apps/photocraft/src/jiansun_images.rs @@ -253,8 +253,7 @@ fn located_original(code: u16, response: &Value, task: &Value) -> R R bool { + let Some(fields) = response.as_object() else { return false }; + ["request_id", "status", "result_available", "result_path", "result_expires_at"].iter().all(|key| fields.contains_key(*key)) + && match response.get("error") { + None => fields.len() == 5, + Some(error) => fields.len() == 6 && error.as_object().is_some_and(|values| values.len() == 2) + && error["code"].as_str().is_some_and(|code| !code.is_empty()) && error["http_status"].is_u64(), + } +} + fn recovery_binding(task: &Value) -> Value { json!({"id":task["id"],"accountScope":task["accountScope"],"localProjectId":task["localProjectId"],"origin":task["origin"], "keyFingerprint":task["keyFingerprint"],"slotBinding":task["slotBinding"],"modelId":task["modelId"],"modelKey":task["modelKey"], @@ -282,7 +291,10 @@ fn lookup_observation(code: u16, value: &Value) -> Value { let path = request.as_ref().map(|request| format!("/api/external/requests/{request}/result")).filter(|path| value["result_path"].as_str() == Some(path.as_str())); let status = value["status"].as_str().filter(|status| ["succeeded", "failed", "processing", "unresolved", "unknown"].contains(status)); - json!({"httpStatus":code,"requestId":request,"status":status,"resultAvailable":value["result_available"].as_bool(),"resultPath":path}) + let error = value["error"]["code"].as_str().filter(|code| code.len() <= 80 && code.bytes().all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')); + let error_status = value["error"]["http_status"].as_u64().filter(|status| (400..600).contains(status)); + json!({"httpStatus":code,"requestId":request,"status":status,"resultAvailable":value["result_available"].as_bool(),"resultPath":path, + "errorCode":error,"errorHTTPStatus":error_status}) } fn locate_original( @@ -1066,6 +1078,17 @@ mod tests { json!({"request_id":FAILURE_REQUEST,"status":"failed","result_available":false, "result_path":format!("/api/external/requests/{FAILURE_REQUEST}/result"),"result_expires_at":null}) } + #[test] + fn failed_lookup_accepts_the_gateway_error_code_and_status_without_posting() { + let mut body = failure_lookup(); + body["error"] = json!({"code":"service_error","http_status":502}); + let mut task = unknown_image(); + let result = original_response(&mut task, |path, _| { + assert_eq!(path, "/api/external/requests/by-idempotency-key"); + Ok((200, body.clone())) + }, |_| Ok(())); + assert!(matches!(result, Ok(OriginalResponse::TerminalFailure(_)))); + } fn unknown_image() -> Value { json!({"id":Uuid::new_v4().to_string(),"phase":"unknown","requestId":null,"keyFingerprint":"same-personal-key-digest", "accountScope":"same-account-scope","localProjectId":"same-local-project","origin":"same-origin","modelId":"image-model", diff --git a/apps/detail-photocraft/apps/photocraft/src/jiansun_pi.rs b/apps/detail-photocraft/apps/photocraft/src/jiansun_pi.rs index 03a6b0fc..d06fd767 100644 --- a/apps/detail-photocraft/apps/photocraft/src/jiansun_pi.rs +++ b/apps/detail-photocraft/apps/photocraft/src/jiansun_pi.rs @@ -118,9 +118,7 @@ fn lookup_binding(task: &Value) -> R { /// Called only behind the original-scope/original-Key GET transport below. /// Neither HTTP 410 nor a provider error body is evidence of a terminal status. fn original_lookup(task: &Value, code: u16, response: &Value) -> R { - let object = response.as_object().ok_or("原SDK请求定位结果无效,保留未知状态")?; - let fields = ["request_id", "status", "result_available", "result_path", "result_expires_at"]; - if code != 200 || object.len() != fields.len() || object.keys().any(|key| !fields.contains(&key.as_str())) || response["result_available"] != false { + if code != 200 || !super::images::original_lookup_fields(response) || response["result_available"] != false { return Err("原SDK请求尚无明确的不可取回证明,保留未知状态;不会重发".into()); } let request = uuid(task, "requestId")?; @@ -139,9 +137,11 @@ fn original_lookup(task: &Value, code: u16, response: &Value) -> R { if !valid_expiry { return Err("原SDK请求保留期限格式尚未确认,保留未知状态;不会重发".into()); } + let evidence = json!({"request_id":response["request_id"],"status":status,"result_available":false, + "result_path":response["result_path"],"result_expires_at":expiry}); Ok(json!({"verified":true,"method":"GET","path":"/api/external/requests/by-idempotency-key","httpStatus":200, "idempotencyKey":uuid(task,"id")?,"requestId":request,"status":status,"resultAvailable":false, - "resultPath":response["result_path"],"resultExpiresAt":expiry,"lookupSha256":hash(&response.to_string()), + "resultPath":response["result_path"],"resultExpiresAt":expiry,"lookupSha256":hash(&evidence.to_string()), "bodyHash":task["bodyHash"],"bindingHash":lookup_binding(task)?,"keyFingerprintMatches":true})) } @@ -902,6 +902,16 @@ mod tests { assert_eq!(task["phase"], "unknown"); } #[test] + fn original_chat_failure_accepts_the_gateway_error_metadata() { + let mut task = lookup_task(); + let mut response = lookup_body("failed"); + response["error"] = json!({"code":"rate_limit_exceeded","http_status":429}); + let proof = original_lookup(&task, 200, &response).unwrap(); + settle_lookup(&mut task, proof, |_| Ok(())).unwrap(); + assert_eq!(task["phase"], "failed"); + assert_eq!(checked_lookup(&task).unwrap()["status"], "failed"); + } + #[test] fn unavailable_finish_refuses_missing_forged_or_failed_proofs_and_non_boolean_acknowledgement() { let mut unavailable = lookup_task(); let proof = original_lookup(&unavailable, 200, &lookup_body("succeeded")).unwrap();