fix(billing): 封住发布前计费竞态
ci / verify (pull_request) Failing after 8s

阻止上游提交状态不明的任务被租约接管后重复执行,并为人工复核保留可操作的结算记录。

将生产提交绑定到当前估价签名,统一复用预处理快照,并补强规则形状、定点溢出与历史规则兼容校验。

已通过 PostgreSQL 16 集成测试、Go 全量测试与静态检查、前端测试与构建、OpenAPI、依赖审计、镜像、迁移、流水线和 SemVer 门禁。
This commit is contained in:
2026-07-21 10:23:58 +08:00
parent 257ee09e58
commit 8beb8501fa
18 changed files with 762 additions and 75 deletions
@@ -95,6 +95,20 @@ ALTER TABLE model_pricing_rules
UPDATE model_pricing_rules SET is_free = false WHERE is_free IS NULL;
UPDATE model_pricing_rules
SET calculator_type = CASE
WHEN resource_type IN ('text_input', 'text_cached_input', 'text_output', 'text_total') THEN 'token_usage'
WHEN resource_type = 'video' THEN 'duration_weight'
ELSE calculator_type
END,
unit = CASE
WHEN resource_type = 'video' AND unit = 'video' THEN '5s'
ELSE unit
END,
updated_at = now()
WHERE (resource_type IN ('text_input', 'text_cached_input', 'text_output', 'text_total') AND calculator_type = 'unit_weight')
OR (resource_type = 'video' AND (calculator_type = 'unit_weight' OR unit = 'video'));
ALTER TABLE model_pricing_rules
ADD CONSTRAINT model_pricing_rules_explicit_free_v2_check CHECK (
base_price > 0 OR (base_price = 0 AND is_free)
@@ -152,6 +166,30 @@ WHERE task.status = 'succeeded'
AND transaction.transaction_type = 'task_billing'
);
INSERT INTO settlement_outbox (
task_id, event_type, action, amount, currency, pricing_snapshot, payload,
status, next_attempt_at, manual_review_reason
)
SELECT task.id,
'task.billing.review',
'settle',
COALESCE(task.final_charge_amount, 0),
COALESCE(NULLIF(task.billing_summary->>'currency', ''), 'resource'),
COALESCE(task.pricing_snapshot, '{}'::jsonb),
jsonb_build_object(
'taskId', task.id,
'classification', 'historical_success_without_charge'
),
'manual_review',
now(),
'historical_success_without_charge'
FROM gateway_tasks task
WHERE task.billing_status = 'manual_review'
AND task.status = 'succeeded'
AND task.run_mode = 'production'
AND task.gateway_user_id IS NOT NULL
ON CONFLICT (task_id, event_type) DO NOTHING;
INSERT INTO settlement_outbox (
task_id, event_type, action, amount, currency, payload, status, next_attempt_at
)