fix: 对齐规格解析跨端超时预算 (#257)

This commit is contained in:
chengma
2026-08-18 08:56:56 +08:00
parent 29cf66ce96
commit 6345b00106
7 changed files with 97 additions and 4 deletions
+13 -1
View File
@@ -26,6 +26,7 @@ const (
MaxPurchaseSpecResolutionBodyBytes = 64 << 10
PurchaseSpecRulesVersion = "purchase_rules_v1"
purchaseSpecSchemaVersion = 1
purchaseSpecResolutionAITimeout = 30 * time.Second
)
var (
@@ -143,7 +144,7 @@ func resolvePurchaseSpecWithSnapshotLoader(ctx context.Context, db *sql.DB, secr
// 可以安全接管;最终 UPDATE 仍只允许 pending 完成一次。
decision, terminal := matchPurchaseSpecByRule(req.TargetSize, req.Candidates)
if !terminal {
decision = matchPurchaseSpecWithAI(ctx, db, secrets, policy, req, loadSnapshot)
decision = matchPurchaseSpecWithRuntimeAI(ctx, db, secrets, policy, req, loadSnapshot)
}
if purchaseSpecDiagnosticSource(db, req.PddGoodsID) == "shopee_backfill" {
decision.Reason = truncateRunes(decision.Reason+";诊断来源:shopee_backfill", 500)
@@ -151,6 +152,17 @@ func resolvePurchaseSpecWithSnapshotLoader(ctx context.Context, db *sql.DB, secr
return completePurchaseSpecResolution(db, idempotencyKey, requestHash, resolution, req.Candidates, decision)
}
// matchPurchaseSpecWithRuntimeAI 给采购运行时 AI 单独设置 30 秒上限。
// Client 会等待最多 60 秒,因此 Admin 有足够时间保存安全结论并返回;如果请求更早取消,
// 子上下文也会立即取消,不会让模型调用脱离原 HTTP 请求继续运行。
func matchPurchaseSpecWithRuntimeAI(ctx context.Context, db *sql.DB, secrets AISecretStore,
policy AIEndpointPolicy, req PurchaseSpecResolutionRequest,
loadSnapshot aiSnapshotLoader) purchaseSpecDecision {
aiContext, cancel := context.WithTimeout(ctx, purchaseSpecResolutionAITimeout)
defer cancel()
return matchPurchaseSpecWithAI(aiContext, db, secrets, policy, req, loadSnapshot)
}
func parsePurchaseSpecResolutionRequest(taskID, idempotencyKey string, rawBody []byte) (PurchaseSpecResolutionRequest, error) {
var req PurchaseSpecResolutionRequest
if len(rawBody) == 0 || len(rawBody) > MaxPurchaseSpecResolutionBodyBytes || !utf8.Valid(rawBody) {