Skip to content

feat(credit-floor): skip paid models when account credit is below threshold (#188) - #534

Merged
caigee-cmd merged 1 commit into
caigee-cmd:mainfrom
nxcjs:fix/credit-floor-188
Nov 20, 2026
Merged

caigee-cmd merged 1 commit into
caigee-cmd:mainfrom
nxcjs:fix/credit-floor-188

Conversation

@nxcjs

@nxcjs nxcjs commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Fixes #188 — workbuddy 账号调度保底

Implements the configurable credit floor the maintainer described in #188: when an account's remaining credit falls below a threshold, it is excluded from paid model scheduling but stays eligible for free models, so the account is never drained to zero (free models stop working too once credit hits 0).

What changed

  • CLI2API_CREDIT_FLOOR — global threshold, default 0 (disabled, existing behaviour unchanged).
  • Per-account credit_floor override (console account card + management API), falling back to the global value.
  • Fail-closed parsing: negative or non-numeric values are rejected at startup instead of silently disabling the gate.
  • Executor scheduling: below-floor accounts are skipped for paid models; free-model requests still work.
  • DB migration + store/console/control plumbing, bilingual unreleased changelog fragment.

Why this shape

The maintainer's reply on #188 asked for "a configurable threshold". This keeps the default off, so no existing deployment changes behaviour unless the env var or account field is set.

Validation

  • go vet ./... — pass
  • go test ./... -count=1 — pass (29 packages, 0 failures)
  • go build ./cmd/server — pass
  • go test ./internal/app -run 'TestImportConstraints|TestDutyBoundaries' — pass
  • go test -race ./internal/executor ./internal/store ./internal/config — pass
  • git diff --check and scripts/release-notes.py validate — pass
  • Verified against upstream main HEAD 81665ac; no secrets in diff.

@caigee-cmd

Copy link
Copy Markdown
Owner

方向对,默认关闭也合适,先不合并。有两处合进去会改到现有账号的调度:

  1. 阈值直接和 Quota.Remaining 比较,没有看 Unit。WorkBuddy 的剩余量是积分,但 Devin / Kimi / Zhipu 报的是百分比,Trae 是 entitlement pack,Codex 是窗口。CLI2API_CREDIT_FLOOR=100 时,百分比账号只要没用满就会被整批移出付费调度。比较前收窄到 Unit == "credits"(或者按单位换算),其他单位保持现在的行为。
  2. changelog 写了账号卡片可以设置 credit_floor,前端没有入口。另外 CreditFloor *float64 带了 json:",omitempty",显式设 0(关闭这个账号的保底)在响应里会被省略,读回来变成「继承全局」。

这两处补上之后我再看。

nxcjs added a commit to nxcjs/cli2api that referenced this pull request Nov 19, 2026
Address maintainer review on PR caigee-cmd#534:
- gate only Unit=="credits" with CLI2API_CREDIT_FLOOR
- add independent CLI2API_PERCENT_FLOOR for percent quota providers
- expose per-account credit_floor/percent_floor overrides in console/API
- keep explicit zero distinguishable from inherit, persist both overrides
- cover cross-unit isolation and free-model exemption with tests

Rebased onto main including account-import changes.
@nxcjs
nxcjs force-pushed the fix/credit-floor-188 branch from c91cd20 to ac81234 Compare November 19, 2026 06:49
@nxcjs

nxcjs commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

把这些改动合并到 PR:

  1. 回复第 1 点:CLI2API_CREDIT_FLOOR 现在只对 Quota.Unit == "credits" 生效,百分比 / entitlement pack / rate-limit window 不再被积分阈值误伤。另有负控测试 TestCreditFloorDoesNotBlockPercentAccount。
  2. 按你给的「或者按单位换算」路线,新增独立的 CLI2API_PERCENT_FLOOR(默认 0 关闭)与按账号 percent_floor,用于 Devin / Kimi / Zhipu 的剩余百分比;两种单位分别判断,不做跨单位数值换算。<= 阈值拦截计费模型,免费模型豁免,nil quota 不拦截,Exceeded 仍优先。
  3. 回复第 2 点:console 账号卡片新增「积分保底 / 百分比保底」两个覆盖开关和输入;credit_floor、percent_floor 去掉 omitempty,显式 0 可以在 API 响应中回读;PATCH 用 JSON 字段是否存在区分「显式关闭」(0) 与「继承全局」(null)。
  4. 数据库新增 029_account_percent_floor.sql 迁移;两个阈值都做启动/存储负值校验。

验证:go test ./... 全绿;前端 npm run build 成功、npm run lint 0 error;pr head 已 rebase 到当前 main(a7fbf013),ahead_by=1 behind_by=0。

@nxcjs
nxcjs force-pushed the fix/credit-floor-188 branch 5 times, most recently from 26d3cea to 8ea2502 Compare November 19, 2026 12:05
@nxcjs

nxcjs commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

CI 失败已修好,刚强推更新了 PR 分支。

原因是我之前的提交没有把 internal/webui/static 与改动后的前端源码同步:frontend job 的 Check internal/webui/static is up to date 失败,而且旧 bundle 里也没有保底入口相关代码;同时分支基于旧 main,合入了会删除 Kiro 导入逻辑的旧版 AccountsPage.tsx。

这次重建为基于当前 main(563c9ca)的单提交:

  • 重新生成 internal/webui/static,bundle 中已包含 credit_floor / percent_floor 和 console 入口
  • 恢复当前 main 的 Kiro 导入与签到行为,只叠加保底功能需要的 onSaveSettings 字段
  • 保留积分/百分比按单位隔离、免费模型豁免、显式 0 与继承区分、迁移和测试

验证:最新 CI run 44520683842 六个 job 全部 success。
https://github.com/caigee-cmd/cli2api/actions/runs/44520683842

@nxcjs
nxcjs force-pushed the fix/credit-floor-188 branch 6 times, most recently from f52d6a6 to 7b605bf Compare November 19, 2026 17:40
Rebuild static assets from current main + credit-floor feature. Keep unchanged CSS/images intact and replace only the five stale JS chunks.
@nxcjs
nxcjs force-pushed the fix/credit-floor-188 branch from 7b605bf to bb9e679 Compare November 19, 2026 17:45
@caigee-cmd
caigee-cmd merged commit 91edc07 into caigee-cmd:main Nov 20, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

workbuddy账号调度保底

2 participants