feat(billing-probe): optionally sync account rate from upstream declared rate

Successful upstream billing probes already persist the upstream-declared
rate as a display-only snapshot. Add a per-account opt-in that writes
that declared rate back to the account's rate_multiplier, so the account
cost basis follows upstream repricing automatically instead of drifting
until an operator notices.

- new per-account flag upstream_billing_rate_sync_enabled stored next to
  the probe flag in account extra: enabling sync force-enables the
  probe, disabling the probe cascades sync off, and eligibility follows
  IsUpstreamBillingProbeIdentity (tightened from any non-empty platform
  to an explicit whitelist of the five supported API-key platforms so
  future platforms do not silently inherit probe/sync semantics)
- only a successful probe whose declared rate survives validation
  (finite, within bounds, not rounded to zero at the rate_multiplier
  decimal(10,4) scale) writes back; failed/unsupported/invalid probes
  leave rate_multiplier unchanged
- the writeback rides the existing snapshot CAS transaction:
  UpdateUpstreamBillingProbeSnapshot takes an optional rateMultiplier
  and applies it atomically with the snapshot under the same
  identity/snapshot compare-and-swap, so a probe result observed on a
  stale account cannot clobber a concurrent admin edit
- admin edit goes through UpdateWithAccountBillingSettings, which
  applies the form without overwriting a rate that a probe synchronized
  after the edit form was loaded (nil rateMultiplier = not edited);
  once sync is enabled the edit form shows the rate as managed
- bulk update rejects a manual rate_multiplier change when any target
  account has rate sync enabled (whole batch fails with a dedicated
  error so partial writes cannot bypass the sync ownership)
- frontend: sync toggle with hints in the edit modal (probe/sync
  enable/disable coupling enforced in the form), synced-rate tooltip on
  the rate cell, bulk edit modal warns and blocks rate edits that hit
  sync-enabled accounts; en/zh copy updated
- tests: service unit tests for sync gating/validation/cascade, sqlmock
  repo tests for the extended CAS, real-PostgreSQL integration tests
  (rate written only for successful+enabled accounts, manual rate
  protected after sync disabled, admin edit preserved across concurrent
  probe sync), handler/API contract updates, frontend specs for modal
  coupling, bulk rejection and rate cell
This commit is contained in:
Brisbanehuang authored and shaw committed 2026-08-01 22:11:09 +08:00
1 parent 56f3d3c9b0
commit b0f5007f04
34 files changed
+1382 -146

No files matched your search

@@ -1441,8 +1441,43 @@
</div>
<div>
<label class="input-label">{{ t('admin.accounts.billingRateMultiplier') }}</label>
<input v-model.number="form.rate_multiplier" type="number" min="0" step="0.001" class="input" />
<p class="input-hint">{{ t('admin.accounts.billingRateMultiplierHint') }}</p>
<input
v-model.number="form.rate_multiplier"
type="number"
min="0"
step="0.001"
class="input disabled:cursor-not-allowed disabled:opacity-60"
data-testid="account-rate-multiplier"
:disabled="upstreamBillingRateSyncEnabled"
/>
<p class="input-hint">
{{
t(
upstreamBillingRateSyncEnabled
? 'admin.accounts.upstreamBilling.syncRateManagedHint'
: 'admin.accounts.billingRateMultiplierHint'
)
}}
</p>
<div
v-if="account?.type === 'apikey'"
class="mt-3 flex items-center justify-between gap-3"
>
<div class="min-w-0">
<p class="text-xs font-medium text-gray-700 dark:text-gray-200">
{{ t('admin.accounts.upstreamBilling.syncRate') }}
</p>
<p class="mt-1 text-xs text-gray-500 dark:text-gray-400">
{{ t('admin.accounts.upstreamBilling.syncRateHint') }}
</p>
</div>
<Toggle
:model-value="upstreamBillingRateSyncEnabled"
data-testid="upstream-billing-rate-sync"
:aria-label="t('admin.accounts.upstreamBilling.syncRate')"
@update:model-value="handleUpstreamBillingRateSyncChange"
/>
</div>
</div>
</div>
<div class="border-t border-gray-200 pt-4 dark:border-dark-600">
@@ -1660,9 +1695,10 @@
</p>
</div>
<Toggle
v-model="upstreamBillingAutoProbeEnabled"
:model-value="upstreamBillingAutoProbeEnabled"
data-testid="upstream-billing-auto-probe"
:aria-label="t('admin.accounts.upstreamBilling.autoProbe')"
@update:model-value="handleUpstreamBillingAutoProbeChange"
/>
</div>
@@ -2822,6 +2858,7 @@ const autoPause7dThreshold = ref<number | null>(null)
const autoPause5hDisabled = ref(false)
const autoPause7dDisabled = ref(false)
const upstreamBillingAutoProbeEnabled = ref(false)
const upstreamBillingRateSyncEnabled = ref(false)
const mixedScheduling = ref(false) // For antigravity accounts: enable mixed scheduling
const allowOverages = ref(false) // For antigravity accounts: enable AI Credits overages
const antigravityProjectId = ref('')
@@ -3191,6 +3228,20 @@ const form = reactive({
expires_at: null as number | null
})
const handleUpstreamBillingRateSyncChange = (enabled: boolean) => {
upstreamBillingRateSyncEnabled.value = enabled
if (enabled) {
upstreamBillingAutoProbeEnabled.value = true
}
}
const handleUpstreamBillingAutoProbeChange = (enabled: boolean) => {
upstreamBillingAutoProbeEnabled.value = enabled
if (!enabled) {
upstreamBillingRateSyncEnabled.value = false
}
}
const statusOptions = computed(() => {
const options = [
{ value: 'active', label: t('common.active') },
@@ -3306,6 +3357,8 @@ const syncFormFromAccount = (newAccount: Account | null) => {
autoPause5hDisabled.value = extra?.auto_pause_5h_disabled === true
autoPause7dDisabled.value = extra?.auto_pause_7d_disabled === true
upstreamBillingAutoProbeEnabled.value = extra?.upstream_billing_probe_enabled === true
upstreamBillingRateSyncEnabled.value =
upstreamBillingAutoProbeEnabled.value && extra?.upstream_billing_rate_sync_enabled === true
// Load OpenAI passthrough toggle (OpenAI OAuth/SetupToken/API Key)
openaiPassthroughEnabled.value = false
@@ -4086,6 +4139,13 @@ const handleSubmit = async () => {
updatePayload.load_factor = 0
}
updatePayload.auto_pause_on_expired = autoPauseOnExpired.value
if (props.account.type === 'apikey') {
updatePayload.upstream_billing_probe_enabled = upstreamBillingAutoProbeEnabled.value
updatePayload.upstream_billing_rate_sync_enabled = upstreamBillingRateSyncEnabled.value
if (upstreamBillingRateSyncEnabled.value) {
delete updatePayload.rate_multiplier
}
}
// For apikey type, handle credentials update
if (props.account.type === 'apikey') {
@@ -4651,7 +4711,8 @@ const handleSubmit = async () => {
// 上游倍率自动探测对全部 API-key 平台开放(sub2api 上游即可应答),
// Bedrock 凭证无静态 Key 不参与。
if (props.account.type === 'apikey') {
newExtra.upstream_billing_probe_enabled = upstreamBillingAutoProbeEnabled.value
delete newExtra.upstream_billing_probe_enabled
delete newExtra.upstream_billing_rate_sync_enabled
}
// Total quota
if (editQuotaLimit.value != null && editQuotaLimit.value > 0) {