Skip to content
Closed
Show file tree
Hide file tree
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
32 changes: 32 additions & 0 deletions fixtures/decision-reader/legacy.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,38 @@
"accepts": true,
"kind": "legacy"
},
{
"name": "legacy-choice-total-lower-edge",
"answer": {
"type": "choice",
"choice": "b",
"probabilities": {
"a": 0.060817,
"b": 0.532085,
"c": 0.407097
},
"confidence": 0.125,
"calibration_version": "unpublished"
},
"accepts": true,
"kind": "legacy"
},
{
"name": "legacy-choice-total-lower-edge-unordered-keys",
"answer": {
"type": "choice",
"choice": "b",
"probabilities": {
"b": 0.611317,
"1": 0.032434,
"a": 0.356248
},
"confidence": 0.255,
"calibration_version": "unpublished"
},
"accepts": true,
"kind": "legacy"
},
{
"name": "legacy-score",
"answer": {
Expand Down
5 changes: 4 additions & 1 deletion python/src/system_zero/decisions.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ def _distribution(answer: Mapping[str, Any], canonical: bool) -> Dict[str, Union
if not isinstance(key, str) or not _probability(value):
_invalid("probabilities must be finite numbers in [0, 1]")
values[key] = _units(value) if canonical else value
total = sum(values.values())
# Explicit sequential addition in UTF-8 key order matches TypeScript; sum() is compensated on Python 3.12+.
total = 0
for key in sorted(values, key=lambda key: key.encode("utf-8")):
total += values[key]

if (canonical and (abs(total - _MILLION) > 1 or (len(values) == 1 and total != _MILLION))) or (not canonical and abs(total - 1) > 0.000001):
_invalid("probabilities must sum to one")
Expand Down
8 changes: 4 additions & 4 deletions typescript/src/decisions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ function units(value: unknown): number {
function distribution(answer: Record<string, unknown>, canonical: boolean): Map<string, number> {
if (!record(answer.probabilities) || Object.keys(answer.probabilities).length === 0) invalid('missing probabilities');
const values = new Map<string, number>();
let total = 0;
for (const [key, value] of Object.entries(answer.probabilities)) {
if (!probability(value)) invalid('probabilities must be finite numbers in [0, 1]');
const amount = canonical ? units(value) : value;
values.set(key, amount);
total += amount;
values.set(key, canonical ? units(value) : value);
}
// Add in UTF-8 key order, the reader's tie order, so a legacy float total never depends on property enumeration order.
let total = 0;
for (const key of [...values.keys()].sort((a, b) => Buffer.compare(Buffer.from(a), Buffer.from(b)))) total += values.get(key)!;

if (canonical ? Math.abs(total - million) > 1 || (values.size === 1 && total !== million) : Math.abs(total - 1) > 0.000001) invalid('probabilities must sum to one');
return values;
Expand Down