Skip to content

Add z.ai peak/off-peak quota burn rate row - #2721

Merged
steipete merged 5 commits into
steipete:mainfrom
cruzanstx:zai-peak-quota-rate
Aug 8, 2026
Merged

Add z.ai peak/off-peak quota burn rate row#2721
steipete merged 5 commits into
steipete:mainfrom
cruzanstx:zai-peak-quota-rate

Conversation

@cruzanstx

@cruzanstx cruzanstx commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Summary

z.ai's credit-based (CREDIT_LIMIT) coding plans charge 1x during peak hours (Monday–Friday, 14:00–18:00 UTC+8, i.e. 06:00–10:00 UTC) and 0.5x off-peak; weekends are off-peak all day — see docs.z.ai/devpack/overview and the usage-revision notice. No z.ai endpoint exposes the phase — it is purely a function of the clock — so CodexBar shows how much quota is left but not that the same prompt costs 2x as much for the next four hours.

Now that z.ai is a JavaScript-only provider (#2758), this is a single-commit, 2-file, +75-line diff: a quotaRateRow() in zai.js derived from the injected ctx.date.now() clock, and golden coverage.

Quota rate    Peak        off-peak in 2h 5m
Quota rate    Off-peak    peak in 2d 6h
  • Gated to credit plans (per maintainer review): legacy TOKENS_LIMIT plans charge model-dependent flat rates (GLM-4.7 is 1x all day), where a phase display would claim a rate that isn't being charged — verified live below.
  • Weekend-aware (per Codex Review): Saturday/Sunday are always off-peak; a Friday-evening snapshot counts down to Monday 06:00 UTC.
  • Uses main's ctx.date.now() injected clock — deterministic under golden tests, no new API surface. (The earlier revisions' ctx.date.nowMillis() plumbing is superseded by the cutover's own clock and is gone from this diff.)
  • Countdown text mirrors UsageFormatter.resetCountdownDescription so the row reads like native reset text.

Prior art: a similar client-side marker has been running in cclimits (Python CLI) since early 2026 — improved here per current z.ai docs, with the weekend fix ported back there.

Behavior proof

Fixed-clock golden renders (all passing on Linux/QuickJS — the production engine for this provider on Linux):

  • zai CREDIT_LIMIT fixture matches the cut-over golden — pins the full details including ("Quota rate", "Off-peak", "peak in 2h 21m") at epoch 1_786_073_946 (Friday 03:39 UTC).
  • New zai quota rate row tracks the credit-plan peak schedule — four fixed-clock epochs: weekday peak (Peak · off-peak in 2h 30m), weekday off-peak, Friday 23:00 skipping the weekend (peak in 2d 7h), Saturday inside the window staying off-peak (peak in 1d 23h).
  • zai fixture matches the cut-over golden (legacy TOKENS_LIMIT) — unchanged: no row, demonstrating the gate.

Live gate verification: Linux CodexBarCLI from this branch (QuickJS zai.js path) against a real legacy TOKENS_LIMIT account renders no Quota rate row, as intended. Earlier PR revisions include the pre-gating transcript where the row appeared on the same account.

Commands run

  • swift test --filter ProviderPluginDetailsParityTests12/12 passed on Linux/QuickJS (swift:6.2 container), including both zai goldens and the new schedule test.
  • swift build --product CodexBarCLI + live usage --provider zai — gate verification above.
  • swiftformat / swiftlint and macOS CI — theirs to confirm; happy to fix anything flagged.

🤖 Generated with Claude Code

@cruzanstx
cruzanstx marked this pull request as ready for review August 6, 2026 22:01

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

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 Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2c058b7d87

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

calendar.timeZone = TimeZone(secondsFromGMT: 0) ?? calendar.timeZone

let hour = calendar.component(.hour, from: reference)
let isPeak = hour >= Self.peakStartHourUTC && hour < Self.peakEndHourUTC

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Keep weekends in the off-peak window

When reference falls on a Saturday or Sunday between 06:00 and 10:00 UTC, this hour-only check reports 3x (peak) and points the next transition to the same-day 10:00 boundary. The current z.ai coding-plan docs state that peak hours are Monday to Friday, 14:00–18:00 UTC+8, and that weekend usage is off-peak all day (see https://docs.z.ai/devpack/overview and https://docs.z.ai/devpack/notice/usage-revision), so weekend snapshots would display the wrong burn rate and countdown until the next refresh.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch — confirmed against both cited docs and fixed in 5a93ef9. Peak is now gated on Mon–Fri, and since the coefficients differ by plan generation (3x/1x quota plans per the usage-revision notice, 1x/0.5x credits plans per the overview), the row now shows phase + transition countdown instead of a hardcoded multiplier. Added Saturday-inside-window and Friday-evening-skips-to-Monday test cases (7/7 passing on Linux).

@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P2 Normal priority bug or improvement with limited blast radius. merge-risk: 🚨 compatibility 🚨 Merging this PR could break existing users, config, migrations, defaults, or upgrades. labels Aug 6, 2026
@clawsweeper

clawsweeper Bot commented Aug 6, 2026

Copy link
Copy Markdown

Codex review: needs maintainer review before merge. Reviewed August 8, 2026, 5:51 PM ET / 21:51 UTC.

ClawSweeper review

What this changes

Adds a credit-plan-only z.ai quota-rate row that shows peak or off-peak status and time to the next rate transition, plus a deterministic plugin clock helper and focused coverage.

Merge readiness

⚠️ Ready for maintainer review - 2 items remain

Keep open as a normal merge candidate: the owner approved the feature and public clock contract, the credit-only schedule logic is bounded and covered, and no patch defect was found.

Priority: P2
Reviewed head: 4cb8280f0314de6398996e3502f19a86d9930bb3

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused, owner-approved provider enhancement with concrete schedule coverage and recorded runtime validation.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (terminal): The PR records fixed-clock production-engine renders and a live CodexBarCLI verification of the legacy-plan gate.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (terminal): The PR records fixed-clock production-engine renders and a live CodexBarCLI verification of the legacy-plan gate.
Evidence reviewed 5 items Maintainer approval: The repository owner explicitly approved the injected millisecond clock as a public plugin-runtime API and stated that the feature was landable after the rebase, credit-plan gate, and parity coverage.
Current z.ai baseline: Current main already parses CREDIT_LIMIT entries alongside legacy token limits, establishing the quota type used by the new credit-only gate.
Focused PR implementation: The proposed diff adds the UTC weekday/weekend transition calculation, only appends the row when a CREDIT_LIMIT is present, and adds fixed-clock parity and runtime coverage.
Findings None None.
Security None None.

How this fits together

CodexBar’s bundled z.ai provider fetches quota limits and converts them into generic usage details consumed by the menu bar and CLI. The new row derives the documented pricing phase from the refresh clock only when the quota response contains a credit limit.

flowchart LR
    A[z.ai quota response] --> B[Quota limit parsing]
    B --> C{Credit plan present?}
    C -->|Yes| D[Injected refresh clock]
    D --> E[Peak schedule calculation]
    E --> F[Quota details row]
    C -->|No| G[Existing quota details]
    F --> H[Menu bar and CLI]
    G --> H
Loading

Before merge

  • Resolve merge risk (P1) - ctx.date.nowMillis() is now a documented installed-plugin API, so its epoch-millisecond and per-refresh injected-clock semantics must remain backward compatible after merge.
  • Complete next step (P2) - No repair lane is needed because no actionable patch defect remains; this is ready for ordinary maintainer merge validation.
Agent review details

Security

None.

Review metrics

Metric Value Why it matters
Production versus test delta production +45, tests +43, docs +4 The new provider display logic has near-parity focused test coverage and narrowly scoped API documentation.

Merge-risk options

Maintainer options:

  1. Preserve the approved clock contract (recommended)
    Merge the feature while treating ctx.date.nowMillis() as a backward-compatible epoch-millisecond snapshot clock for installed plugins.

Technical review

Best possible solution:

Merge the additive credit-plan row while preserving the documented clock helper as a stable plugin contract.

Do we have a high-confidence way to reproduce the issue?

Not applicable as a bug reproduction: fixed-clock parity cases cover peak, off-peak, weekend, and legacy-plan behavior, and the PR records a production QuickJS CLI gate check.

Is this the best way to solve the issue?

Yes. A credit-limit gate plus a deterministic UTC schedule is the narrowest maintainable approach and avoids claiming a rate for legacy token plans.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against b772c66b741b.

Labels

Label justifications:

  • P2: This is a bounded provider usage-display improvement with limited blast radius.
  • merge-risk: 🚨 compatibility: The documented plugin clock helper becomes a compatibility commitment for installed plugins.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (terminal): The PR records fixed-clock production-engine renders and a live CodexBarCLI verification of the legacy-plan gate.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR records fixed-clock production-engine renders and a live CodexBarCLI verification of the legacy-plan gate.

Evidence

What I checked:

Likely related people:

  • steipete: Authored the merged CREDIT_LIMIT parser and JavaScript-provider cutover, approved this feature and its public clock contract, and merged current main into the PR branch. (role: feature owner and recent area contributor; confidence: high; commits: 013680770b04, 8fc67d7f04ad, 4cb8280f0314; files: Sources/CodexBarCore/Resources/Plugins/zai.js, Sources/CodexBarCore/Resources/Plugins/provider-plugin-prelude.js)

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

History

Review history (15 earlier review cycles; latest 8 shown)
  • reviewed 2026-08-07T12:16:37.644Z sha 3aa5610 :: found issues before merge. :: [P1] Get approval for the new public plugin clock API | [P2] Accept Unix epoch zero as an injected reference time
  • reviewed 2026-08-07T12:24:27.222Z sha 4099b4f :: found issues before merge. :: [P1] Obtain approval for the public plugin clock API
  • reviewed 2026-08-07T13:06:00.201Z sha 4099b4f :: found issues before merge. :: [P1] Obtain approval for the public plugin clock API
  • reviewed 2026-08-08T06:59:06.110Z sha 6e7497d :: found issues before merge. :: [P1] Rebase the quota row onto the JavaScript-only z.ai provider
  • reviewed 2026-08-08T07:11:55.790Z sha 6c62805 :: needs maintainer review before merge. :: none
  • reviewed 2026-08-08T11:25:41.516Z sha 6c62805 :: needs maintainer review before merge. :: none
  • reviewed 2026-08-08T21:23:25.375Z sha 6504584 :: needs changes before merge. :: [P2] Document the public millisecond clock helper
  • reviewed 2026-08-08T21:30:37.541Z sha 75fcc43 :: needs maintainer review before merge. :: none

@clawsweeper clawsweeper Bot added proof: sufficient Contributor real behavior proof is sufficient. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Aug 6, 2026
cruzanstx pushed a commit to cruzanstx/cclimits that referenced this pull request Aug 7, 2026
Peak hours are Mon-Fri only (docs.z.ai/devpack/overview and
/devpack/notice/usage-revision) - weekend runs inside 06:00-10:00 UTC
previously showed a false 3x peak marker. The 2x off-peak/promo-1x
scheme was superseded by z.ai's usage revision: quota plans burn
3x peak / 1x off-peak; off-peak boundary now skips to Monday 06:00.
Surfaced by Codex review on the CodexBar port of this logic
(steipete/CodexBar#2721).

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@clawsweeper clawsweeper Bot added rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. and removed rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. labels Aug 7, 2026
@cruzanstx

Copy link
Copy Markdown
Contributor Author

ClawSweeper's two findings split cleanly, so handling them separately:

Epoch-zero defect (P2): fixed in 4099b4f. The value > 0 guard now accepts any finite non-negative value, and a new ProviderPluginRuntimeTests case pins both the epoch-zero and fixed-instant paths.

Public plugin clock API (P1): this one is your call, since ctx.date.nowMillis() becomes a durable installed-plugin contract. Options, in order of my preference:

  • (a) Bless it as documented public API — current state of the branch (documented in both plugin references).
  • (b) Keep the helper, drop it from docs/plugins.md — stays a bundled-plugin-only convention, no installed-plugin commitment.
  • (c) Different shape — e.g. hand the reference time to fetchUsage as an argument instead of a ctx.date helper.

Context for why it exists at all: the z.ai quota-rate row is wall-clock-derived, and ProviderPluginDetailsParityTests needs Swift and JS to share a deterministic clock. Happy to implement whichever you prefer.

🤖 Generated with Claude Code

@steipete

steipete commented Aug 8, 2026

Copy link
Copy Markdown
Owner

Thanks — the schedule itself is well-researched (matches z.ai's documented Mon–Fri 14:00–18:00 UTC+8 peak windows and multipliers), but three changes are needed before this can land:

  1. Rebase on current main: fix(zai): parse CREDIT_LIMIT quota entries; restore 5-hour primary reset #2751 landed today and adds CREDIT_LIMIT credit-plan parsing; your branch conflicts with it in the Swift parser, zai.js, parity tests, and plugin runtime. After rebasing, credit-plan users are exactly the audience for this row.
  2. Gate the phase display to plans where it's true: the quota API exposes no peak flag, so the row derives phase purely from the clock. That's accurate for credit (CREDIT_LIMIT) plans, but legacy TOKENS_LIMIT plans on GLM-4.7 are 1× all day — showing "Peak" there claims a rate that isn't being charged. Either gate the row to credit plans or reword so it describes the schedule, not an active rate.
  3. Add credit-plan parity tests (Swift/JS golden fixtures — see the pattern from fix(zai): parse CREDIT_LIMIT quota entries; restore 5-hour primary reset #2751's tests).

Separate note: the new ctx.date.nowMillis() public plugin-API surface is a maintainer decision we'll rule on independently — keep it isolated in the diff so it can be discussed without blocking the row.

@steipete

steipete commented Aug 8, 2026

Copy link
Copy Markdown
Owner

Maintainer ruling on the API question: ctx.date.nowMillis() is approved as a public plugin-runtime API — an injected clock keeps time-dependent rows deterministic under golden tests (vs. plugins reaching for Date.now() anyway). Keep it an isolated commit in the rebase, with a golden-test demonstration of a fixed-clock render. The three review asks from the earlier comment still stand; with those plus the rebase this is landable.

@cruzanstx
cruzanstx force-pushed the zai-peak-quota-rate branch from 4099b4f to 6e7497d Compare August 8, 2026 06:54
@cruzanstx

Copy link
Copy Markdown
Contributor Author

All three asks plus the ruling conditions are in — rebased branch force-pushed as 6e7497d (8 commits):

  1. Rebased on current main through fix(zai): parse CREDIT_LIMIT quota entries; restore 5-hour primary reset #2751 and the QuickJS engine port. One find during the rebase: the QuickJS engine landed without setting host.nowMillis, so Linux plugins silently fell back to Date.now() — fixed in its own commit (5d0cb3d).
  2. Gated to credit plans in Swift and zai.js (6e7497d), with the GLM-4.7 rationale in the doc comment. Verified live: my legacy TOKENS_LIMIT account now correctly renders no row (transcript in the updated PR body).
  3. Credit-plan parity golden: the fix(zai): parse CREDIT_LIMIT quota entries; restore 5-hour primary reset #2751 CREDIT_LIMIT fixture now pins the fixed-clock render on both sides — ("Quota rate", "Off-peak", "peak in 2h 21m") at its Friday 03:39 UTC epoch, with now injected into the Swift call for determinism.

Per the ruling, the ctx.date.nowMillis() work stays isolated (e96aa4e docs, 1ecb07f epoch-zero + runtime regression test, 5d0cb3d QuickJS host) and the fixed-clock golden demonstration is the CREDIT_LIMIT fixture above. Linux verification post-rebase: CodexBarCore builds, 7/7 ZaiQuotaRateLinuxTests, live CLI gate check. macOS suite is CI's to confirm — happy to fix anything it flags.

🤖 Generated with Claude Code

@cruzanstx
cruzanstx force-pushed the zai-peak-quota-rate branch from 6e7497d to 6c62805 Compare August 8, 2026 07:07
@cruzanstx

Copy link
Copy Markdown
Contributor Author

Re-ported onto the JavaScript-only z.ai provider from #2758 — force-pushed as a single commit 6c62805: 2 files, +75 lines (was 8 commits / 10 files before the cutover).

  • zai.js gains quotaRateRow() using main's injected ctx.date.now() — the parallel nowMillis plumbing from earlier revisions is gone; the cutover's own clock supersedes it, so there's no new API surface left to rule on.
  • Golden coverage per the ruling: the CREDIT_LIMIT golden pins ("Quota rate", "Off-peak", "peak in 2h 21m") at its fixed epoch, plus a new 4-epoch schedule test (weekday peak, weekday off-peak, Friday-night weekend skip, Saturday-inside-window). Legacy TOKENS_LIMIT golden unchanged — no row, demonstrating the credit gate.
  • Verified on the production Linux path: swift test --filter ProviderPluginDetailsParityTests12/12 on QuickJS, and a live CodexBarCLI run against a real legacy account correctly renders no row.

🤖 Generated with Claude Code

@clawsweeper clawsweeper Bot added rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. and removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. labels Aug 8, 2026
@clawsweeper clawsweeper Bot added status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. merge-risk: 🚨 compatibility 🚨 Merging this PR could break existing users, config, migrations, defaults, or upgrades. labels Aug 8, 2026
pwnqgljxs and others added 3 commits August 8, 2026 14:11
Credit-based plans charge 1x during peak (Mon-Fri 14:00-18:00 UTC+8;
weekends off-peak all day) and 0.5x off-peak, but no z.ai endpoint
exposes the phase, so derive it from ctx.date.now() and render
"Quota rate: Off-peak - peak in 2h 21m" in the quota details. Gated
to CREDIT_LIMIT plans: legacy TOKENS_LIMIT plans charge model-
dependent flat rates where a phase display would claim a rate that
isn't being charged. Golden-covered at four fixed-clock epochs
(weekday peak, weekday off-peak, Friday-night weekend skip,
Saturday-inside-window).

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@steipete
steipete force-pushed the zai-peak-quota-rate branch from 6c62805 to 6504584 Compare August 8, 2026 21:19
@clawsweeper clawsweeper Bot added rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. merge-risk: 🚨 compatibility 🚨 Merging this PR could break existing users, config, migrations, defaults, or upgrades. and removed rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Aug 8, 2026
🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@clawsweeper clawsweeper Bot added rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Aug 8, 2026
@steipete
steipete merged commit d427c2d into steipete:main Aug 8, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-risk: 🚨 compatibility 🚨 Merging this PR could break existing users, config, migrations, defaults, or upgrades. P2 Normal priority bug or improvement with limited blast radius. proof: sufficient Contributor real behavior proof is sufficient. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants