Skip to content

fix(platform): give each security scan a spend limit - #978

Merged
Brooooooklyn merged 2 commits into
mainfrom
fix/security-cached-input-cap
Nov 30, 2026
Merged

Brooooooklyn merged 2 commits into
mainfrom
fix/security-cached-input-cap

Conversation

@hyfdev

@hyfdev hyfdev commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

A full security scan can stop as incomplete (token_cap) long before it costs the $0.504 Void reserves by default, because cached input counts in full toward maxInputTokens.

This raises that limit's default and adds a per-scan spend limit, using the model's cached price if it has one.

Void refuses calls it estimates the scan can't afford, so some scans stop earlier.

Installations that already scan keep their saved maxInputTokens (1,200,000 unless changed) until it's raised with void platform security limits --scan-input-tokens 10000000. --scan-usd <usd|auto> sets or clears maxScanUsd.

One more 1.6 MB call on the default model, with mocked replies:

The scan so far maxInputTokens Before Now
#966's first scan: 1,279,208 input tokens, mostly cached, $0.0376 default (1,200,000 before, 10,000,000 now) refused by the token limit allowed: $0.0376 + $0.2117 ≤ $0.504
The same scan 1,200,000, saved refused by the token limit refused by the token limit
1,000,000 input tokens, none cached, $0.3014 default (1,200,000 before, 10,000,000 now), or 1,200,000, saved allowed refused by the spend limit: $0.3014 + $0.2117 > $0.504
1,400,000 input tokens, none cached, $0.4214 2,000,000, saved allowed refused by the spend limit: $0.4214 + $0.2117 > $0.504

Void now also refuses a call when:

spent + estimates of calls still running + this call's estimate > limit
(the existing limits still apply)

spent      = the scan's cost so far, including earlier attempts. Cached input counts at
             DeepSeek's or GLM's cached price, and at full price on Terra, Sol and Opus.

limit      = maxScanUsd, or if it's null:
             min(maxInputTokens, 1,200,000) × input price + maxOutputTokens × output price
             default model and settings: 1.2M × $0.30/M + 120,000 × $1.20/M = $0.504

estimate   = (bytes ÷ 2.5) × input price + this call's output token limit × output price
             1.6 MB, as if none is cached: 640,000 × $0.30/M + 16,384 × $1.20/M = $0.2117

worst case = what each monthly cap holds for a call: the same formula with one token per
             byte, as before ($0.4997 for 1.6 MB)

If a possibly billed call's usage isn't recorded, spent keeps its estimate, and the monthly
caps still keep its worst case.

On Opus, the estimate and the worst case use twice the input price.
Before Now
maxInputTokens, which counts cached input in full 1,200,000 by default, at most 2,000,000 10,000,000 by default, at most 20,000,000
maxScanUsd, set through the operator API doesn't exist null by default, or $0.01 to $12
Reserved for each scan in the monthly caps (now also its spend limit), with the default model and settings $0.504 $0.504
The same, with a saved maxInputTokens of 2,000,000 $0.744 $0.504
Shown when scans are turned on, with no default or plan caps "Monthly scan budgets are off (no money limit)." "There is no default monthly scan limit. Limits set for individual projects still apply."

A refusal ends the scan as incomplete (spend_cap) and keeps its confirmed findings. The call ledger keeps a spend, token or call-count stop when a later transient failure (such as a 503) comes in, so a later failure no longer turns the stop into a retried scan with no findings:

Opus scan with mocked replies: one confirmed finding, $0.008 spent, $0.01 reserved (Terra and Sol use the same check but weren't tested) Before Now
The next call is estimated at more than the $0.002 left allowed, succeeded with 2 findings refused, incomplete (spend_cap) with 1 finding
The same, and another call's 503 is recorded later allowed, succeeded with 2 findings refused, incomplete (spend_cap) with 1 finding
The token limit refuses the call instead, and another call's 503 is recorded later refused, failed with 0 findings, retried refused, incomplete (token_cap) with 1 finding

Previews call the scan amount an estimate (estimatedUsd; worstCaseUsd is kept for older CLIs), because a call can cost more than estimated: one mocked call, estimated at $0.2117, took a scan from $0.29 to $0.5222, past $0.504, before and now.

Concurrent calls can go further over.

Part of #966.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

🚀 Deploying Preview to Cloudflare 🚀

Preview URL: https://fix-security-cached-input-cap-void-cloud.voidzero-docs.workers.dev (commit 787ffd9)

This URL reflects your latest Preview deployment

Preview Deployments by commit

Status Deployment URL Commit Updated (UTC) See this deployment's details
  • Build: Success ✅
  • Deployment: Success ✅

View logs ↗
https://7404a46f-void-cloud.voidzero-docs.workers.dev 787ffd9 2026-11-30T10:24:54.236Z Visit the dashboard ↗
  • Build: Success ✅
  • Deployment: Success ✅

View logs ↗
https://e7eeec6f-void-cloud.voidzero-docs.workers.dev 1abf073 2026-11-30T03:33:42.669Z Visit the dashboard ↗
  • Build: Success ✅
  • Deployment: Success ✅

View logs ↗
https://c9e0e08d-void-cloud.voidzero-docs.workers.dev 889b8bb 2026-11-29T04:32:46.496Z Visit the dashboard ↗

@hyfdev
hyfdev marked this pull request as ready for review November 29, 2026 04:36
Copilot AI balanced review requested due to automatic review settings November 29, 2026 04:36

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@hyfdev
hyfdev requested a review from Brooooooklyn November 29, 2026 05:34
@cpojer

cpojer commented Nov 29, 2026

Copy link
Copy Markdown
Member

imho we should keep maxInputTokens as a token count and give scans a separate spend limit that accounts for the cache discount. Also can we add a check for whether it can afford the next call?

@hyfdev
hyfdev force-pushed the fix/security-cached-input-cap branch from 889b8bb to 1abf073 Compare November 30, 2026 03:32
@hyfdev hyfdev changed the title fix(platform): count cached input at its cached price in security scan limits fix(platform): give each security scan a spend limit Nov 30, 2026
@hyfdev

hyfdev commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author

I've made both changes.

maxInputTokens is still a token count, and each scan has its own spend limit, which uses the cached price for models that have one.

Before each call, Void checks that its estimated cost fits in what's left of that limit.

@cpojer

cpojer commented Nov 30, 2026

Copy link
Copy Markdown
Member

Slop Review

  • [P2] The new limit breaks @void/platform/security-policy consumers. SECURITY_LIMIT_KEYS now includes maxScanUsd, but SECURITY_LIMIT_RANGES has no entry for it. Existing code that iterates the keys and reads each range’s .max throws. Existing SecurityPolicyLimits literals also fail to type-check because the new field is required. Keep the exported keys and ranges aligned and make the new field optional for callers. [Policy diff](codex://review?pr=https%3A%2F%2Fgithub.com%2Fvoidzero-dev%2Fvoid%2Fpull%2F978&path=packages%2Fplatform%2Fsrc%2Fsecurity-policy.ts&line=54&side=right)
  • [P2] The CLI still promises a hard maximum. With mocked provider usage, the same three-call sequence stopped at $0.23652 on main and reached $0.52032 on the PR after quoting a $0.504 maximum. Keep the pre-call check and call the amount an estimate in the API and CLI. [Cost estimate](codex://review?pr=https%3A%2F%2Fgithub.com%2Fvoidzero-dev%2Fvoid%2Fpull%2F978&path=platform%2Fpackages%2Fapi%2Fsrc%2Fsecurity%2Fmodel-proxy.ts&line=1072&side=right) · [CLI wording](
    `${first}${typeof preview.worstCaseUsd === 'number' ? ` It can cost up to ${money(preview.worstCaseUsd)}.` : ''}\n${dim('Monthly caps apply; raise the cap first if a rescan is refused.')}\n`,
    )
  • [P2] A later 503 can discard confirmed findings after a spend refusal. I reproduced a scan retaining its finding when it stopped at the spend limit. Adding a later 503 caused the scan to requeue and save zero findings. Preserve the spend stop when deciding the attempt’s final result so it can save the findings it already has. [Result handling](codex://review?pr=https%3A%2F%2Fgithub.com%2Fvoidzero-dev%2Fvoid%2Fpull%2F978&path=platform%2Fpackages%2Fapi%2Fsrc%2Fsecurity%2Fingest.ts&line=645&side=right)

hyfdev and others added 2 commits November 30, 2026 18:18
- Keep `SECURITY_LIMIT_KEYS` paired with `SECURITY_LIMIT_RANGES` and make
  `SecurityPolicyLimits.maxScanUsd` optional; add `SECURITY_SCAN_USD_RANGE`.
- Send `estimatedUsd` (keep `worstCaseUsd` for older CLIs); the CLI shows
  "Estimated cost: up to $X".
- The call ledger keeps a stop on the scan's own limits over later transient
  failures, so a later 503 no longer turns a spend stop into a retried
  scan with no findings.
- Refuse a call the spend limit cannot fit before it takes a session call
  or rate token.
- Add `--scan-input-tokens` and `--scan-usd <usd|auto>` to
  `void platform security limits`.
- Show the spend limit as $X / automatic in the policy preview and
  "spend limit reached" in local checks; trim the guide.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Brooooooklyn
Brooooooklyn force-pushed the fix/security-cached-input-cap branch from 1abf073 to 787ffd9 Compare November 30, 2026 10:23
@Brooooooklyn

Copy link
Copy Markdown
Member

Addressed the three points in 787ffd9:

  1. Policy exports. SECURITY_LIMIT_KEYS is back to the five keys of SECURITY_LIMIT_RANGES, and SecurityPolicyLimits.maxScanUsd is optional, so five-field literals type-check again. The spend limit has its own SECURITY_SCAN_USD_RANGE. A unit test checks that keys and ranges match.
  2. Estimate, not a maximum. The pre-call check is unchanged. Previews now send estimatedUsd (worstCaseUsd stays, with the same value, for older CLIs), and the CLI prints "Estimated cost: up to $X". The guide says a scan can go a little over its limit.
  3. Later 503 after a spend stop. The call ledger now keeps a stop on the scan's own limits (spend, tokens, call counts) when a later transient failure (upstream, rate_wait, not_allowed, refused) comes in. A newer budget or credits stop still replaces it. last-failure keeps returning the stop, so the runner reports incomplete and ingest saves spend_cap with the confirmed findings, with no requeue. A proxy test (spend refusal, then a 503, then last-failure) fails before this change and passes after it. An ingest test covers spend then 503, and the finding is kept.

Also in this commit:

  • A call the spend limit can't fit is refused before it takes a session call or a @cf rate token.
  • void platform security limits --scan-input-tokens <n> --scan-usd <usd|auto>, so installations with a saved 1.2M limit can raise it without a raw API call.
  • The policy preview shows the spend limit as $X or automatic.

Rebased on main.

@Brooooooklyn
Brooooooklyn merged commit b5bbda3 into main Nov 30, 2026
22 checks passed
@Brooooooklyn
Brooooooklyn deleted the fix/security-cached-input-cap branch November 30, 2026 10:29
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.

4 participants