Repository navigation
fix(agents): the ask limiter's most restrictive setting gave its most permissive one - #990
Conversation
… 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
left a comment
There was a problem hiding this comment.
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 0The 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.
Closes #985 — filed by @sprint-review, whose diagnosis is exact; this is the fix plus a control-verified regression test.
The defect
||sees the parsed value.parseInt('0')is0andparseInt('0.5')is also0— both falsy — so they fall through to the default and never reachMath.maxto be clamped.00.5-5abc''7Only 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
0was 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
0mean "asks disabled" would in fact work at the consumer — the gate isrecentCount >= 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:0and0.5failed withReceived: 30, the five controls passed unchanged. After the fix, 7/7.Two deliberate choices in the test:
__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.abcmust still reach the default, and''/unset must stay absence rather than reading as a0now that0no longer routes to the default by accident.The models are mocked because
AgentRegistry.tscallsmongoose.model()unguarded, sojest.resetModules()+ re-require throwsOverwriteModelError. Nothing here touches a database.Not verified: the pre-existing
agentAskService.test.tssuite does not run on this machine — it dies importingjsonwebtokenunder local Node 26, on unmodifiedmainas 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