Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions services/03-vpp-aggregator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,13 @@ app.get('/capacity/available', authenticateToken, async (req, res) => {
if (safetyContext && typeof safetyContext === 'string') {
try {
const context = JSON.parse(safetyContext);
physicsScoreVal = parseFloat(safeFloat(context.physics_score, 1.0));
confidenceScoreVal = parseFloat(safeFloat(context.confidence_score, 1.0));

// Handle invalid physics_score or confidence_score explicitly to match NaN/safeFloat expectations
const rawP = parseFloat(context.physics_score);
const rawC = parseFloat(context.confidence_score);

physicsScoreRaw = isNaN(rawP) ? NaN : rawP;
confidenceScoreRaw = isNaN(rawC) ? NaN : rawC;

// High-Fidelity Standard: (physics_score > 0.95 OR confidence_score > 0.95)
isHighFidelity = physicsScoreRaw > 0.95 || confidenceScoreRaw > 0.95;
Expand All @@ -162,11 +167,15 @@ app.get('/capacity/available', authenticateToken, async (req, res) => {
isSentinelFidelity = context.is_sentinel_fidelity === true || context.is_sentinel_fidelity === 'true' || context.is_sentinel_fidelity === 1 || physicsScoreRaw > 0.99;

// Apply the lower of the two as the capacity derating factor
// If either is NaN, Math.min returns NaN. We should use safe values for the multiplier.
const safeP = isNaN(physicsScoreRaw) ? 0.0 : physicsScoreRaw;
const safeC = isNaN(confidenceScoreRaw) ? 0.0 : confidenceScoreRaw;
physicsMultiplier = Math.min(safeP, safeC);
} catch (e) {}
// If either is NaN, we set multiplier to 0.0 to prevent invalid capacity
if (isNaN(physicsScoreRaw) || isNaN(confidenceScoreRaw)) {
physicsMultiplier = 0.0;
} else {
physicsMultiplier = Math.min(physicsScoreRaw, confidenceScoreRaw);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Missing score zeros fleet capacity

Medium Severity

In GET /capacity/available, physics_score and confidence_score are parsed with bare parseFloat, so an omitted field becomes NaN and forces physicsMultiplier to 0.0. The background updateGlobalCapacity path still defaults missing values through safeFloat to 1.0, so the same Redis safety context can report zero fleet capacity while global aggregation keeps normal derating.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 3371b8b. Configure here.

} catch (e) {
console.error('[VPP Aggregator] Error parsing safety context in GET /capacity/available:', e.message);
}
}

const physicsScoreVal = safeFloat(physicsScoreRaw, 1.0);
Expand Down Expand Up @@ -288,7 +297,7 @@ const updateGlobalCapacity = async () => {
lockedFleetId = context.fleet_id;
}
rawPhysicsScore = parseFloat(safeFloat(context.physics_score, 1.0));
confidenceScore = parseFloat(safeFloat(context.confidence_score, 1.0));
rawConfidenceScore = parseFloat(safeFloat(context.confidence_score, 1.0));

// Sentinel Fidelity logic: Prioritize explicit flag (boolean, string, or integer)
isSentinelFidelity = context.is_sentinel_fidelity === true || context.is_sentinel_fidelity === 'true' || context.is_sentinel_fidelity === 1 || rawPhysicsScore > 0.99;
Expand Down