Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions .github/workflows/task-settlement-regression.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
name: Isolated task settlement regression

on:
workflow_dispatch:
pull_request:
paths:
- 'apps/api-go/model/task_quota_settlement.go'
- 'apps/api-go/service/task_billing.go'
- 'apps/api-go/service/task_quota_settlement_test.go'
- '.github/workflows/task-settlement-regression.yml'

permissions:
contents: read

jobs:
isolated-regression:
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0
with:
persist-credentials: false
- uses: actions/setup-go@7a3fe6cf4cb3a834922a1244abfce67bcef6a0c5
with:
go-version-file: apps/api-go/go.mod
cache-dependency-path: apps/api-go/go.sum
- name: Record source and compile without executing application code
working-directory: apps/api-go
shell: bash
run: |
set -euo pipefail
evidence="$RUNNER_TEMP/task-settlement-evidence"
mkdir -p "$evidence"
git rev-parse HEAD > "$evidence/revision.txt"
go version > "$evidence/go-version.txt"
sha256sum model/task_quota_settlement.go service/task_billing.go service/task_quota_settlement_test.go > "$evidence/source-sha256.txt"
test -z "$(gofmt -l model/task_quota_settlement.go service/task_billing.go service/task_quota_settlement_test.go)"
go test -p 2 -race -c -o "$RUNNER_TEMP/task-settlement.test" ./service 2>&1 | tee "$evidence/compile.log"
go test -p 2 -c -o "$RUNNER_TEMP/payment-units.test" ./pkg/paymentpricing 2>&1 | tee "$evidence/payment-compile.log"
- name: Run tests with a new network namespace and clean environment
shell: bash
run: |
set -uo pipefail
evidence="$RUNNER_TEMP/task-settlement-evidence"
status=0
for suite in task-settlement payment-units; do
pattern='.'
if [[ "$suite" == task-settlement ]]; then pattern='^TestTaskQuotaSettlement'; fi
sudo unshare --net -- bash -c '
set -euo pipefail
ip link set lo up
work=$(mktemp -d)
trap '\''rm -rf "$work"'\'' EXIT
cd "$work"
env -i PATH=/usr/bin:/bin HOME="$work" TMPDIR="$work" \
"$1" -test.run "$2" -test.count=1 -test.v -test.timeout=120s
' isolated-test "$RUNNER_TEMP/$suite.test" "$pattern" 2>&1 | tee "$evidence/$suite.log"
code=${PIPESTATUS[0]}
printf '%s\n' "$code" > "$evidence/$suite.exit-code.txt"
if [[ "$code" != 0 ]]; then status=1; fi
done
(cd "$evidence" && sha256sum *.log > logs-sha256.txt)
exit "$status"
- name: Upload source-matched test evidence
if: always()
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02
with:
name: task-settlement-evidence
path: ${{ runner.temp }}/task-settlement-evidence
retention-days: 7
if-no-files-found: error
109 changes: 109 additions & 0 deletions apps/api-go/model/task_quota_settlement.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package model

import (
"errors"

"github.com/LIghtJUNction/api.lmm.best/common"
"gorm.io/gorm"
)

var ErrTaskQuotaSettlementConflict = errors.New("task quota settlement conflicts with persisted state")

// ApplyTaskQuotaSettlement commits the funding, token, usage counters and task
// marker together. expectedQuota prevents a stale result from replacing a newer
// settlement. A retry for an already committed target has no financial effect.
// This function does not choose a price, change rounding, or authorize a refund.
func ApplyTaskQuotaSettlement(id int64, expectedQuota, actualQuota int) (int, bool, error) {
if DB == nil || id <= 0 || expectedQuota < 0 || expectedQuota > common.MaxQuota ||
actualQuota <= 0 || actualQuota > common.MaxQuota {
return 0, false, gorm.ErrInvalidData
}
var previousQuota, userID int
var tokenKey string
var applied bool
err := DB.Transaction(func(tx *gorm.DB) error {
var task Task
if err := lockForUpdate(tx).Where("id = ?", id).First(&task).Error; err != nil {
return err
}
previousQuota = task.Quota
if task.UserId <= 0 || task.Quota < 0 || task.Quota > common.MaxQuota ||
task.RefundStatus != "" || task.Status == TaskStatusFailure {
return ErrTaskQuotaSettlementConflict
}
if task.Quota == actualQuota {
return nil
}
if task.Quota != expectedQuota {
return ErrTaskQuotaSettlementConflict
}
delta := actualQuota - task.Quota
userID = task.UserId
// Keep this compare-and-swap in the same transaction as every balance.
// It is also required on databases where SELECT FOR UPDATE is omitted.
result := tx.Model(&Task{}).
Where("id = ? AND quota = ? AND (refund_status = '' OR refund_status IS NULL)", id, expectedQuota).
Updates(map[string]interface{}{"quota": actualQuota, "updated_at": common.GetTimestamp()})
if result.Error != nil {
return result.Error
}
if result.RowsAffected != 1 {
Comment on lines +48 to +50

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛡️ Codex Security Review · Automatically triggered

P1 Badge Security: Match the subscription billing lock order

When a subscription-backed async task with a positive final adjustment completes while the same API-token user starts another subscription-billed request, this transaction locks subscription→token→user, while pre-consume locks user→subscription→token. PostgreSQL/MySQL can deadlock; if settlement is aborted, RecalculateTaskQuota only logs the error after SUCCESS was stored, and terminal tasks are never polled again. A user can repeatedly overlap requests to leave final deltas uncharged. Lock the user before the subscription and durably retry failed settlements.

Useful? React with 👍 / 👎.

return ErrTaskQuotaSettlementConflict
}

if task.PrivateData.BillingSource == "subscription" && task.PrivateData.SubscriptionId > 0 {
if err := postConsumeUserSubscriptionDeltaTx(tx, task.PrivateData.SubscriptionId, int64(delta)); err != nil {
return err
}
} else if err := ApplyWalletQuotaDelta(tx, userID, -delta); err != nil {
return err
}

if task.PrivateData.TokenId > 0 {
var token Token
err := lockForUpdate(tx).Where("id = ?", task.PrivateData.TokenId).First(&token).Error
if err == nil {
if token.UserId != userID {
return ErrTaskQuotaSettlementConflict
}
tokenKey = token.Key
if err := tx.Model(&Token{}).Where("id = ? AND user_id = ?", token.Id, userID).
Updates(map[string]interface{}{
"remain_quota": gorm.Expr("remain_quota - ?", delta),
"used_quota": gorm.Expr("used_quota + ?", delta),
"accessed_time": common.GetTimestamp(),
}).Error; err != nil {
return err
}
} else if !errors.Is(err, gorm.ErrRecordNotFound) {
return err
}
// A deleted token must not prevent settlement of its completed task.
}
if err := tx.Model(&User{}).Where("id = ?", userID).
UpdateColumn("used_quota", boundedQuotaCounterExpr("used_quota", delta)).Error; err != nil {
return err
}
if task.ChannelId > 0 {
if err := tx.Model(&Channel{}).Where("id = ?", task.ChannelId).
UpdateColumn("used_quota", boundedQuotaCounterExpr("used_quota", delta)).Error; err != nil {
return err
}
}
applied = true
return nil
})
if err != nil || !applied {
return previousQuota, false, err
}
// Cache failures cannot turn a committed settlement into a second debit.
if err := invalidateUserCache(userID); err != nil {
common.SysLog("invalidate settled task user cache: " + err.Error())
}
if tokenKey != "" {
if err := invalidateTokenCacheForMutation(tokenKey); err != nil {
common.SysLog("invalidate settled task token cache: " + err.Error())
}
}
return previousQuota, true, nil
}
43 changes: 12 additions & 31 deletions apps/api-go/service/task_billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,43 +228,24 @@ func RefundTaskQuota(ctx context.Context, task *model.Task, reason string) bool
// reason 用于日志记录(例如 "token重算" 或 "adaptor调整")。
// clamps 可选:若计算 actualQuota 时发生额度饱和,将其记入日志 admin_info(仅管理员可见)。
func RecalculateTaskQuota(ctx context.Context, task *model.Task, actualQuota int, reason string, clamps ...*common.QuotaClamp) {
if actualQuota <= 0 {
if task == nil || actualQuota <= 0 {
return
}
preConsumedQuota := task.Quota
quotaDelta := actualQuota - preConsumedQuota

if quotaDelta == 0 {
logger.LogInfo(ctx, fmt.Sprintf("任务 %s 预扣费准确(%s,%s)",
task.TaskID, logger.LogQuota(actualQuota), reason))
return
}

logger.LogInfo(ctx, fmt.Sprintf("任务 %s 差额结算:delta=%s(实际:%s,预扣:%s,%s)",
task.TaskID,
logger.LogQuota(quotaDelta),
logger.LogQuota(actualQuota),
logger.LogQuota(preConsumedQuota),
reason,
))

// 调整资金来源
if err := taskAdjustFunding(task, quotaDelta); err != nil {
logger.LogError(ctx, fmt.Sprintf("差额结算资金调整失败 task %s: %s", task.TaskID, err.Error()))
preConsumedQuota, applied, err := model.ApplyTaskQuotaSettlement(task.ID, task.Quota, actualQuota)
if err != nil {
logger.LogError(ctx, fmt.Sprintf("任务差额结算未提交 task %s: %s", task.TaskID, err.Error()))
return
}

// 调整令牌额度
taskAdjustTokenQuota(ctx, task, quotaDelta)

// Refresh stale caller snapshots only after the database has committed.
task.Quota = actualQuota
if err := task.UpdateQuota(); err != nil {
logger.LogError(ctx, fmt.Sprintf("差额结算回写 quota 失败 task %s: %s", task.TaskID, err.Error()))
if !applied {
return
}

// 提交阶段已经累计过一次请求;结算阶段只调整最终用量。
model.UpdateUserUsedQuota(task.UserId, quotaDelta)
model.UpdateChannelUsedQuota(task.ChannelId, quotaDelta)
quotaDelta := actualQuota - preConsumedQuota
logger.LogInfo(ctx, fmt.Sprintf("任务 %s 差额结算:delta=%s(实际:%s,预扣:%s,%s)",
task.TaskID, logger.LogQuota(quotaDelta), logger.LogQuota(actualQuota),
logger.LogQuota(preConsumedQuota), reason))
// LOG_DB can be separate. Logs are not the balance idempotency marker.

var logType int
var logQuota int
Expand Down
Loading
Loading