Skip to content

fix(agents): the ask limiter's most restrictive setting gave its most permissive one - #990

Merged
lilyshen0722 merged 1 commit into
mainfrom
fix/ask-rate-limit-zero-floor
Aug 18, 2026
Merged

lilyshen0722 merged 1 commit into
mainfrom
fix/ask-rate-limit-zero-floor

Conversation

@lilyshen0722

Copy link
Copy Markdown
Contributor

Closes #985 — filed by @sprint-review, whose diagnosis is exact; this is the fix plus a control-verified regression test.

The defect

Math.max(1, Number.parseInt(process.env.AGENT_ASK_RATE_LIMIT_PER_HOUR || '', 10) || 30)

|| sees the parsed value. parseInt('0') is 0 and parseInt('0.5') is also 0 — both falsy — so they fall through to the default and never reach Math.max to be clamped.

env before after
0 30 1
0.5 30 1
-5 1 1
abc 30 30
unset / '' 30 30
7 7 7

Only two rows move, and they are the two that inverted the operator's intent: every other out-of-range value already clamped to the floor, so 0 was uniquely the input that turned the most restrictive setting into the most permissive one.

The fix

Split the NaN test out from the clamp, so the two questions stop sharing one operator — unparsable means "use the default", parsed-low means "clamp to the floor". || cannot express both.

What this deliberately does not do

The floor stays 1, not 0. Making 0 mean "asks disabled" would in fact work at the consumer — the gate is recentCount >= ASK_RATE_LIMIT_PER_HOUR, so a limit of 0 refuses every ask rather than dividing by zero or reading as unlimited — but that is a new capability, not this defect. Left to whoever owns the ask surface.

Proof

backend/__tests__/unit/services/agentAskService.rateLimitEnv.test.js — 7 cases, run red first: 0 and 0.5 failed with Received: 30, the five controls passed unchanged. After the fix, 7/7.

Two deliberate choices in the test:

  • It pins the delivered constant (__testing__.ASK_RATE_LIMIT_PER_HOUR, re-required per env value) rather than an extracted resolver. A test against a helper stays green if a later edit stops routing the env var through it.
  • It carries controls in both directions — abc must still reach the default, and ''/unset must stay absence rather than reading as a 0 now that 0 no longer routes to the default by accident.

The models are mocked because AgentRegistry.ts calls mongoose.model() unguarded, so jest.resetModules() + re-require throws OverwriteModelError. Nothing here touches a database.

Not verified: the pre-existing agentAskService.test.ts suite does not run on this machine — it dies importing jsonwebtoken under local Node 26, on unmodified main as well as on this branch (0 tests collected either way). CI pins Node 20 and is the real signal for that file.

🤖 Generated with Claude Code

… permissive one

AGENT_ASK_RATE_LIMIT_PER_HOUR=0 resolved to 30. The resolver was
Math.max(1, parseInt(env) || 30), and `||` sees the PARSED value:
parseInt('0') is 0 and parseInt('0.5') is also 0, both falsy, so they
took the default instead of reaching Math.max to be clamped. Every other
out-of-range value already clamped correctly — -5 gave 1 — so 0 was the
one input that inverted the operator's intent rather than merely
rounding it.

Split the NaN test out from the clamp so the two questions stop sharing
one operator: unparsable means "use the default", parsed-low means
"clamp to the floor". `||` cannot express both.

The floor stays 1. Making 0 mean "asks disabled" would in fact work at
the consumer (recentCount >= 0 refuses every ask), but that is a new
capability rather than this defect, and it belongs to whoever owns the
ask surface.

Tests pin the delivered constant under a re-required module, not an
extracted helper — a test against a helper stays green if a later edit
stops routing the env var through it. Verified red first: 0 and 0.5
failed with 30, the five control cases passed unchanged.

Closes #985

Co-Authored-By: Claude Opus 5 <[email protected]>

@lilyshen0722 lilyshen0722 left a comment

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.

Verified independently rather than reading the table — and it reproduces exactly.

Red/green, run here. I took the PR's test file, ran it against origin/main's unfixed agentAskService.ts (identical to my branch's copy, checked), then against this PR's:

unfixed  → 2 failed, 5 passed
             ✕ clamps 0 to the floor …            Received: 30
             ✕ clamps a sub-1 fraction to the floor …  Received: 30
             ✓ abc → 30   ✓ unset → 30   ✓ '' → 30   ✓ -5 → 1   ✓ 7 → 7
fixed    → 7 passed

Exactly the two rows the PR claims move, both failing with the default rather than some other wrong value, and all five controls unchanged across the mutation — so the test discriminates the fix and nothing else. Pinning __testing__.ASK_RATE_LIMIT_PER_HOUR rather than an extracted resolver is the right call and it works: __testing__ already exported that constant on main (:283), so the test binds to the delivered value with no new seam.

The ''/unset control pair is the one I'd have asked for and it is already there. Now that 0 no longer routes to the default by accident, an empty string reading as 0 would be a live regression (a k8s env block spells "unset" that way) — and it is pinned in both directions.

No blocking findings. Land it.

One thing worth doing separately, not on this PR. The same shape appears four more times in backend/, and they do not all mean the same thing — I checked each consumer rather than assuming a shared mechanism means a shared bug:

site zero resolves to verdict
agentEventService.ts:531 AGENT_SESSION_MAX_SIZE_KB 400 (floor is 64) same defect — =10 clamps to 64, =0 jumps to 400, so the most aggressive setting produces a laxer threshold than the floor
schedulerService.ts:767 SUMMARIZER_FANOUT_BATCH_PAUSE_MS 500 same defect, and worse — see below
schedulerService.ts:766 SUMMARIZER_FANOUT_BATCH_SIZE 10 not a defect — the loop is i += BATCH_SIZE, so a delivered 0 is an infinite loop. Here || 10 is load-bearing protection
gateway/index.ts:80 GATEWAY_PORT 5001 marginal — 0 is Node's "ephemeral port", plausible in tests, harmless in practice

BATCH_PAUSE_MS is the sharp one, because the code already knows 0 is meaningful:

const BATCH_PAUSE_MS = parseInt(process.env.SUMMARIZER_FANOUT_BATCH_PAUSE_MS || '500', 10) || 500;
…
if (i + BATCH_SIZE < installations.length && BATCH_PAUSE_MS > 0) {   // ← anticipates 0

The consumer explicitly branches on "no pause", and the parser guarantees that branch can never be taken from configuration: every path yields 500 or a positive int. So it is simultaneously a setting that silently does not work and a permanently-true condition. That is a strictly better bug report than "same pattern over here", which is why it is worth the grep — and why BATCH_SIZE, three lines above it and syntactically identical, must not be changed.

Filing those two so they are not lost; this PR should not grow to hold them.

@lilyshen0722
lilyshen0722 merged commit 61dacdf into main Aug 18, 2026
12 checks passed
@lilyshen0722
lilyshen0722 deleted the fix/ask-rate-limit-zero-floor branch August 18, 2026 11:13
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.

AGENT_ASK_RATE_LIMIT_PER_HOUR=0 resolves to 30, bypassing its own Math.max(1) floor

1 participant