Skip to content

Commit d29d949

Browse files
lucbrinkmanclaude
andauthored
Fix probability slider bugs by using direct node ID lookups (#4)
Fixed two related bugs: 1. Setting slider to 0% displayed correctly on arrows but calculated as 50% downstream 2. Upgrading intermediate nodes to question nodes didn't affect downstream calculations Root cause: Array-based slider indexing was using questionNodeIndices.indexOf() which found the wrong position when node indices didn't align with slider indices. Changes: - Replace sliderProbs array with Map<string, number> keyed by node ID - Remove buggy questionNodeIndices.indexOf() lookup - Use direct map lookup: nodeIdToProb.get(sourceNode.id) - Fix falsy value bug: change || to ?? so 0 is treated as valid value - Keep sliderIndex field for sidebar visual sorting only Both bugs now resolved and tested. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude <[email protected]>
1 parent cb45ff5 commit d29d949

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

lib/probability.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ export function calculateProbabilities(
9999
const edges = initialEdges.map(e => ({ ...e }));
100100

101101
// Extract slider probabilities from question nodes (0-100 to 0.0-1.0)
102-
// Build array indexed by sliderIndex
103-
const sliderProbs: number[] = [];
102+
// Build map from node ID to probability value
103+
const nodeIdToProb = new Map<string, number>();
104104
graphData.nodes.forEach(node => {
105-
if (node.type === NodeType.QUESTION && node.sliderIndex !== null) {
106-
sliderProbs[node.sliderIndex] = (node.probability || 50) / 100.0;
105+
if (node.type === NodeType.QUESTION) {
106+
nodeIdToProb.set(node.id, (node.probability ?? 50) / 100.0);
107107
}
108108
});
109109

@@ -145,15 +145,14 @@ export function calculateProbabilities(
145145

146146
// Calculate conditional probability based on edge type
147147
if (edge.yn !== EdgeType.ALWAYS) {
148-
// Find the slider index for the source node
149-
const sliderIndex = questionNodeIndices.indexOf(edge.source);
148+
// Get the source node and look up its probability by ID
149+
const sourceNode = nodes[edge.source];
150+
const sliderProb = nodeIdToProb.get(sourceNode.id);
150151

151-
if (sliderIndex === -1) {
152-
throw new Error(`No slider value for node ${edge.source} from edge ${edgeIndex}`);
152+
if (sliderProb === undefined) {
153+
throw new Error(`No slider value for node ${sourceNode.id} (index ${edge.source}) from edge ${edgeIndex}`);
153154
}
154155

155-
const sliderProb = sliderProbs[sliderIndex];
156-
157156
// YES edge = slider probability, NO edge = 1 - slider probability
158157
conditionalProb = edge.yn === EdgeType.YES ? sliderProb : 1.0 - sliderProb;
159158
}

0 commit comments

Comments
 (0)