macore-client-dev 0.1.100__tar.gz → 0.1.102__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: macore-client-dev
3
- Version: 0.1.100
3
+ Version: 0.1.102
4
4
  Summary: Client for the Model Access Core (MAC) server
5
5
  Requires-Python: >=3.11
6
6
  Requires-Dist: mcp-proxy-adapter<9,>=8.11.21
@@ -1,3 +1,3 @@
1
1
  """The single release version shared by the client and server packages."""
2
2
 
3
- __version__ = "0.1.100"
3
+ __version__ = "0.1.102"
@@ -20,7 +20,7 @@ from dataclasses import asdict
20
20
  import uuid
21
21
  from macore_client.decision import (
22
22
  DecisionResult, DecisionAuditReceipt, ThresholdResult, finite,
23
- selected_probability, validate_decision_request, DecisionRemoteError, answer_from_dict,
23
+ selected_probability, validate_decision_request, DecisionRemoteError, _answer_from_dict,
24
24
  )
25
25
 
26
26
 
@@ -159,7 +159,8 @@ class MacClient:
159
159
  if decision.provider_id != provider_id or decision.requested_model_id != model_id or list(decision.answers) != list(questions):
160
160
  raise ValueError("model_decide response correlation differs")
161
161
  for question_id, answer in decision.answers.items():
162
- answer_from_dict(asdict(answer), questions[question_id])
162
+ _answer_from_dict(asdict(answer), questions[question_id], provider_id=decision.provider_id,
163
+ requested_model_id=decision.requested_model_id, model_id=decision.model_id)
163
164
  return decision
164
165
 
165
166
  async def model_decision_record(self, *, decision_audit_id: str, question_id: str,
@@ -187,7 +188,8 @@ class MacClient:
187
188
  if not finite(threshold):
188
189
  raise ValueError("threshold must be a finite number in [0,1], not boolean")
189
190
  answer = decision.answers[question_id]
190
- answer_from_dict(asdict(answer))
191
+ _answer_from_dict(asdict(answer), provider_id=decision.provider_id,
192
+ requested_model_id=decision.requested_model_id, model_id=decision.model_id)
191
193
  chosen, probability = selected_probability(answer, target)
192
194
  outcome = "accepted" if probability >= threshold else "below_threshold"
193
195
  receipt = await self.model_decision_record(decision_audit_id=decision.audit_record_id,
@@ -3,6 +3,8 @@ from __future__ import annotations
3
3
 
4
4
  import math
5
5
  from dataclasses import asdict, dataclass
6
+ from decimal import Decimal
7
+ from fractions import Fraction
6
8
  from typing import Any, Mapping, TypeAlias
7
9
 
8
10
 
@@ -156,6 +158,43 @@ DecisionAnswer: TypeAlias = NoulAnswer | ChoiceAnswer | ScoreAnswer
156
158
 
157
159
  def answer_from_dict(raw: Any, question: Mapping[str, Any] | None = None) -> DecisionAnswer:
158
160
  """Validate a whole native answer; never repair probabilities or round a score."""
161
+ return _answer_from_dict(raw, question)
162
+
163
+
164
+ def _score_representation_matches(score: int | float, probabilities: Mapping[str, Any], count: int) -> bool:
165
+ """Check the closed decimal envelope without constructing replacement provider data."""
166
+ if not 2 <= count <= 10:
167
+ return False
168
+ # Fractions keep decimal JSON values exact through all bounded-fill operations,
169
+ # independently of the process-wide Decimal context or binary float rounding.
170
+ half_quantum = Fraction(Decimal("0.005"))
171
+ values = [Fraction(Decimal(str(probabilities[str(i)]))) for i in range(count)]
172
+ lower = [max(0, value - half_quantum) for value in values]
173
+ upper = [min(1, value + half_quantum) for value in values]
174
+ remaining = 1 - sum(lower)
175
+ if remaining < 0 or sum(upper) < 1:
176
+ return False
177
+
178
+ def extreme(order: range) -> Fraction:
179
+ mass = remaining
180
+ expectation = sum(i * value for i, value in enumerate(lower))
181
+ for i in order:
182
+ added = min(mass, upper[i] - lower[i])
183
+ expectation += i * added
184
+ mass -= added
185
+ return expectation
186
+
187
+ minimum = extreme(range(count))
188
+ maximum = extreme(range(count - 1, -1, -1))
189
+ native_score = Fraction(Decimal(str(score)))
190
+ return max(minimum, max(0, native_score - half_quantum)) <= min(
191
+ maximum, min(count - 1, native_score + half_quantum))
192
+
193
+
194
+ def _answer_from_dict(raw: Any, question: Mapping[str, Any] | None = None, *,
195
+ provider_id: str | None = None, requested_model_id: str | None = None,
196
+ model_id: str | None = None) -> DecisionAnswer:
197
+ """Internal decoder; only established outer provenance can select the JEV policy."""
159
198
  if not isinstance(raw, dict):
160
199
  raise ValueError("decision answer must be an object")
161
200
  kind = raw.get("type")
@@ -176,7 +215,12 @@ def answer_from_dict(raw: Any, question: Mapping[str, Any] | None = None) -> Dec
176
215
  probs = raw.get("probabilities")
177
216
  if not isinstance(probs, dict) or not probs or any(not isinstance(k, str) or not finite(v) for k, v in probs.items()):
178
217
  raise ValueError("invalid probability distribution")
179
- if abs(sum(probs.values()) - 1) > (1e-3 if kind == "choice" else 1e-6):
218
+ scoped_score = (kind == "score" and provider_id == "typesafe"
219
+ and requested_model_id == model_id == "jev-1.14.0")
220
+ probability_sum = (sum(Fraction(Decimal(str(value))) for value in probs.values())
221
+ if scoped_score else sum(probs.values()))
222
+ sum_tolerance = Fraction(Decimal("0.000001")) if scoped_score else (1e-3 if kind == "choice" else 1e-6)
223
+ if abs(probability_sum - 1) > sum_tolerance:
180
224
  raise ValueError("probabilities are not normalized")
181
225
  if kind == "choice":
182
226
  if question is not None and set(probs) != set(question["criteria"]):
@@ -189,7 +233,9 @@ def answer_from_dict(raw: Any, question: Mapping[str, Any] | None = None) -> Dec
189
233
  if set(probs) != {str(i) for i in range(count)} or not finite(raw.get("score"), 0, count - 1):
190
234
  raise ValueError("invalid score levels or expectation")
191
235
  expected = sum(i * probs[str(i)] for i in range(count))
192
- if abs(raw["score"] - expected) > 1e-3:
236
+ consistent = (_score_representation_matches(raw["score"], probs, count) if scoped_score
237
+ else abs(raw["score"] - expected) <= 1e-3)
238
+ if not consistent:
193
239
  raise ValueError("score differs from native expected level")
194
240
  legend = {str(i): v for i, v in enumerate(question["criteria"])} if question is not None else raw.get("legend")
195
241
  if not isinstance(legend, dict) or set(legend) != set(probs) or ("legend" in raw and raw["legend"] != legend):
@@ -232,7 +278,9 @@ class DecisionResult:
232
278
  raise ValueError("invalid cost amount")
233
279
  if not isinstance(data["answers"], dict) or not data["answers"]:
234
280
  raise ValueError("invalid answers")
235
- data["answers"] = {k: answer_from_dict(v) for k, v in data["answers"].items()}
281
+ data["answers"] = {k: _answer_from_dict(v, provider_id=data["provider_id"],
282
+ requested_model_id=data["requested_model_id"], model_id=data["model_id"])
283
+ for k, v in data["answers"].items()}
236
284
  return cls(**data)
237
285
 
238
286