fix: hoist grantedMs/grantedMin to avoid ReferenceError in ncc bundles#545
fix: hoist grantedMs/grantedMin to avoid ReferenceError in ncc bundles#545
Conversation
…in ncc bundles
The `grantedMs` and `grantedMin` variables were declared with `const`
inside the `if (decision.extend && decision.minutes > 0)` block but
referenced in the `return` statement outside that block. While the
conditional spread `...(decision.extend ? { granted_ms: grantedMs } : {})`
logically only evaluates when `decision.extend` is true, bundlers like
ncc can hoist or restructure code in ways that cause a ReferenceError
at runtime because the block-scoped `const` is not visible outside the
`if` block.
Fix: declare both variables with `let` (initialized to 0) before the
`if` block so they are in scope for the return statement.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
PR Overview: Fix Block-Scoping Issue in Negotiated Timeout ObserverSummaryThis PR fixes a critical JavaScript scoping bug that caused Files Changed
Technical ChangesRoot CauseIn the negotiated timeout observer function within if (decision.extend && decision.minutes > 0) {
const requestedMs = Math.min(decision.minutes, maxPerReqMin) * 60000;
const grantedMs = Math.min(requestedMs, remainingBudgetMs, negotiatedTimeoutState.maxPerRequestMs);
const grantedMin = Math.round(grantedMs / 60000 * 10) / 10;
// ...
}
return {
// ... references grantedMs and grantedMin here
};When bundled with Fix AppliedThe fix hoists the variable declarations outside the let grantedMs = 0;
let grantedMin = 0;
if (decision.extend && decision.minutes > 0) {
const requestedMs = Math.min(decision.minutes, maxPerReqMin) * 60000;
grantedMs = Math.min(requestedMs, remainingBudgetMs, negotiatedTimeoutState.maxPerRequestMs);
grantedMin = Math.round(grantedMs / 60000 * 10) / 10;
// ...
}
return {
// ... now can safely reference grantedMs and grantedMin
};This ensures the variables are in scope for the Architecture & ImpactComponent Affected
Flow Diagramgraph TD
A[Negotiated Timeout Trigger] --> B{LLM Decision}
B -->|extend=true| C[Calculate grantedMs/grantedMin]
B -->|extend=false| D[Skip calculation]
C --> E[Update state]
D --> E
E --> F[Return result object]
F --> G{grantedMs/grantedMin accessible?}
G -->|Before fix| H[ReferenceError in ncc bundles]
G -->|After fix| I[✓ Variables in scope]
Impact Scope
Test CoverageThe PR adds comprehensive regression tests:
Review Notes
Metadata
Powered by Visor from Probelabs Last updated: 2026-03-20T20:27:31.829Z | Triggered by: pr_opened | Commit: fb40ab7 💡 TIP: You can chat with Visor using |
\n\n
✅ Architecture Check PassedNo architecture issues found – changes LGTM. ✅ Performance Check PassedNo performance issues found – changes LGTM. ✅ Quality Check PassedNo quality issues found – changes LGTM. Powered by Visor from Probelabs Last updated: 2026-03-20T20:22:31.333Z | Triggered by: pr_opened | Commit: fb40ab7 💡 TIP: You can chat with Visor using |
Summary
grantedMsandgrantedMinwere declared withconstinside theif (decision.extend && decision.minutes > 0)block in the negotiated timeout observer, but referenced in thereturnstatement outside that blockReferenceErrorat runtime because block-scopedconstvariables are not visible outside theifblocklet(initialized to 0) before theifblock so they remain in scope for thereturnstatementTest plan
grantedMs/grantedMinare accessible in the return statement for both extend and decline pathsletdeclarations are used instead ofconst🤖 Generated with Claude Code