hebbrix 2.12.0__tar.gz → 2.12.1__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.
- {hebbrix-2.12.0 → hebbrix-2.12.1}/CHANGELOG.md +5 -0
- {hebbrix-2.12.0/hebbrix.egg-info → hebbrix-2.12.1}/PKG-INFO +1 -1
- {hebbrix-2.12.0 → hebbrix-2.12.1}/hebbrix/__init__.py +1 -1
- {hebbrix-2.12.0 → hebbrix-2.12.1}/hebbrix/answers.py +6 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/hebbrix/client.py +1 -1
- {hebbrix-2.12.0 → hebbrix-2.12.1}/hebbrix/resources.py +33 -8
- {hebbrix-2.12.0 → hebbrix-2.12.1}/hebbrix/sync_client.py +1 -1
- {hebbrix-2.12.0 → hebbrix-2.12.1/hebbrix.egg-info}/PKG-INFO +1 -1
- {hebbrix-2.12.0 → hebbrix-2.12.1}/hebbrix.egg-info/SOURCES.txt +1 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/pyproject.toml +1 -1
- hebbrix-2.12.1/tests/test_closest_match_envelope.py +521 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/LICENSE +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/MANIFEST.in +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/README.md +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/contracts/openapi-routes.json +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/docs/native-experience-operations.md +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/examples/native_reflection_worker.py +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/hebbrix/_advice.py +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/hebbrix/chat.py +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/hebbrix/exceptions.py +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/hebbrix/execution.py +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/hebbrix/experience.py +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/hebbrix/models.py +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/hebbrix/openai_reflection.py +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/hebbrix/reflection_worker.py +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/hebbrix/workflow.py +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/hebbrix.egg-info/dependency_links.txt +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/hebbrix.egg-info/entry_points.txt +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/hebbrix.egg-info/requires.txt +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/hebbrix.egg-info/top_level.txt +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/setup.cfg +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/tests/test_advanced_resources.py +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/tests/test_advisor_integrity.py +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/tests/test_answer_context.py +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/tests/test_answer_decision.py +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/tests/test_answer_values.py +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/tests/test_chat_answer_options.py +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/tests/test_compact_search_default.py +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/tests/test_customer_developer_experience.py +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/tests/test_evidence_loop_resource.py +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/tests/test_evidence_safety_boundary.py +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/tests/test_execution_permission.py +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/tests/test_experience_request.py +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/tests/test_experience_workflow.py +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/tests/test_followup_helpers.py +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/tests/test_memories_resource.py +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/tests/test_openai_reflection.py +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/tests/test_openapi_parity.py +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/tests/test_outcome_advice_resource.py +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/tests/test_procedural_resource.py +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/tests/test_reflection_worker_cli.py +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/tests/test_release_version.py +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/tests/test_sync_client.py +0 -0
- {hebbrix-2.12.0 → hebbrix-2.12.1}/tests/test_workflow_route_contract.py +0 -0
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 2.12.1 — 2026-12-05 (closest matches beside no_match)
|
|
4
|
+
|
|
5
|
+
- Fix: `search_with_proof()` no longer discards a response that matched nothing but returns the closest memories about the same subject (server 1.4.53 and later: `no_match: true`, `answer_status: "no_relevant_memory"`, `confidence_reason: "closest_match_not_an_answer"`, each row `low_confidence: true` with `low_confidence_reason: "closest_match_not_an_answer"`). 2.12.0 rejected it (`rows_not_bound_to_evidence_ids`), dropped the rows, and `should_answer` returned `malformed_response` / `abstain`. The rows are now kept with their labels, and `should_answer` returns reason `no_relevant_memory`, action `say_unknown`, those rows in `decision.context` and never in `memories`. `reason()` applies the same rule to its `sources`.
|
|
6
|
+
- The envelope rule, precisely: a row with `low_confidence: true` is a candidate and may be absent from `evidence_ids`; every other row (including one that does not state `low_confidence`) is evidence and must still be bound to `evidence_ids`. `no_match: true` may carry rows only when every row is a candidate and `evidence_ids` is empty; `no_match: true` beside an evidence row, or a non-empty `evidence_ids`, still fails closed as before. Because `search()` returns `search_with_proof()`'s rows, it now returns those closest matches too (it returned none before), each flagged `low_confidence: true`: filter on `low_confidence` to keep only answers, as the API documents. Nothing else in the validator or in `should_answer` changed.
|
|
7
|
+
|
|
3
8
|
## 2.12.0 — 2026-11-18 (values and value conflicts)
|
|
4
9
|
|
|
5
10
|
- Types: search rows (`MemoryRow`) gain `values` (server 1.4.45) and `value_conflicts` (server 1.4.46), and now also type `applies_to` and `entity_match`. `values` lists the codes a memory gives as values: a field's value (`customs_form='EXP-7'`) or a code a rule requires ("need customs form EXP-7"); a value never scopes a memory away from the task. `value_conflicts` lists `{"value", "query_value"}` objects (new type `ValueConflict`), set when the query or task names a different value of the same kind than one the memory requires. Both are optional: rows from older servers omit them, and unknown keys are kept as before.
|
|
@@ -177,6 +177,12 @@ def should_answer(
|
|
|
177
177
|
``use_as_context``: give ``decision.context`` to your model to shape what it
|
|
178
178
|
suggests, never as an established fact or a permission to act.
|
|
179
179
|
|
|
180
|
+
A response that matched nothing (``no_match``) may carry the closest
|
|
181
|
+
memories about the same subject, each ``low_confidence: true``
|
|
182
|
+
(``confidence_reason`` ``closest_match_not_an_answer``). It returns reason
|
|
183
|
+
``no_relevant_memory`` and action ``say_unknown``, those rows in
|
|
184
|
+
``decision.context``: what is closest, never an answer.
|
|
185
|
+
|
|
180
186
|
An ``unverified`` search response whose rows include an
|
|
181
187
|
``evidence_role: applicable_rule`` row returns reason ``applicable_rule``
|
|
182
188
|
and action ``use_as_context``, with those rules first in
|
|
@@ -94,10 +94,28 @@ _SEARCH_SAFETY_FIELDS = frozenset(
|
|
|
94
94
|
)
|
|
95
95
|
|
|
96
96
|
|
|
97
|
+
def _is_candidate_row(row: Any) -> bool:
|
|
98
|
+
"""A row the server flags ``low_confidence: true``: a retrieval candidate
|
|
99
|
+
returned as context (a closest match beside ``no_match``, or a candidate on
|
|
100
|
+
an answerable page), never evidence. Only the boolean ``true`` says so; a
|
|
101
|
+
row that does not say it is a candidate is treated as evidence."""
|
|
102
|
+
|
|
103
|
+
return isinstance(row, dict) and row.get("low_confidence") is True
|
|
104
|
+
|
|
105
|
+
|
|
97
106
|
def _canonical_search_envelope(
|
|
98
107
|
response: Dict[str, Any], *, rows_key: str = "results"
|
|
99
108
|
) -> Dict[str, Any]:
|
|
100
|
-
"""Validate API-owned evidence metadata and fail closed on contract drift.
|
|
109
|
+
"""Validate API-owned evidence metadata and fail closed on contract drift.
|
|
110
|
+
|
|
111
|
+
Every evidence row (``low_confidence`` not ``true``) must be bound to
|
|
112
|
+
``evidence_ids``. A candidate row (``low_confidence: true``) may be absent
|
|
113
|
+
from them: servers list candidates on answerable pages but not the closest
|
|
114
|
+
matches they return beside ``no_match``. ``no_match: true`` may carry rows
|
|
115
|
+
only when every row is a candidate and ``evidence_ids`` is empty; those
|
|
116
|
+
rows are kept as context. Any evidence beside ``no_match`` is a
|
|
117
|
+
contradiction and fails closed.
|
|
118
|
+
"""
|
|
101
119
|
|
|
102
120
|
data = dict(response) if isinstance(response, dict) else {}
|
|
103
121
|
raw_rows = data.get(rows_key)
|
|
@@ -141,26 +159,33 @@ def _canonical_search_envelope(
|
|
|
141
159
|
elif not rows and data.get("no_match") is False:
|
|
142
160
|
reason = "no_evidence_rows"
|
|
143
161
|
else:
|
|
144
|
-
|
|
162
|
+
# Candidate rows need no binding; every other row is evidence.
|
|
163
|
+
evidence_row_ids = {
|
|
145
164
|
str(row.get("memory_id") or row.get("id"))
|
|
146
165
|
for row in rows
|
|
147
|
-
if isinstance(row, dict)
|
|
166
|
+
if isinstance(row, dict)
|
|
167
|
+
and (row.get("memory_id") or row.get("id"))
|
|
168
|
+
and not _is_candidate_row(row)
|
|
148
169
|
}
|
|
149
170
|
evidence_ids = {str(value) for value in data.get("evidence_ids", []) if value}
|
|
150
|
-
if not
|
|
171
|
+
if not evidence_row_ids.issubset(evidence_ids):
|
|
151
172
|
reason = "rows_not_bound_to_evidence_ids"
|
|
152
|
-
elif data.get("no_match") and (
|
|
173
|
+
elif data.get("no_match") and (evidence_row_ids or evidence_ids):
|
|
153
174
|
reason = "no_match_contains_evidence"
|
|
154
175
|
|
|
155
176
|
# Degradation and abstention are confidence signals, not proof that the
|
|
156
177
|
# API returned no evidence. Erasing valid, evidence-bound rows here made
|
|
157
178
|
# the SDK disagree with the REST response and converted a safe fallback
|
|
158
179
|
# into a false negative. Fail closed only when the envelope is malformed
|
|
159
|
-
# or the server explicitly reports no match.
|
|
180
|
+
# or the server explicitly reports no match. An explicit no match keeps
|
|
181
|
+
# its candidate rows (the closest memories about the same subject, every
|
|
182
|
+
# one ``low_confidence: true``) as context: nothing answers, and these are
|
|
183
|
+
# what is closest. A malformed envelope keeps nothing.
|
|
160
184
|
if reason or data.get("no_match") is True:
|
|
161
|
-
|
|
185
|
+
context_rows = [] if reason else [row for row in rows if _is_candidate_row(row)]
|
|
186
|
+
data[rows_key] = context_rows
|
|
162
187
|
if rows_key == "results":
|
|
163
|
-
data["total"] =
|
|
188
|
+
data["total"] = len(context_rows)
|
|
164
189
|
data["no_match"] = True
|
|
165
190
|
data["abstain_recommended"] = True
|
|
166
191
|
data["query_confidence"] = 0.0
|
|
@@ -1144,7 +1144,7 @@ class SyncMemoryClient:
|
|
|
1144
1144
|
self.source = source or os.getenv("HEBBRIX_SOURCE")
|
|
1145
1145
|
headers = {
|
|
1146
1146
|
"Content-Type": "application/json",
|
|
1147
|
-
"User-Agent": "hebbrix-python/2.12.
|
|
1147
|
+
"User-Agent": "hebbrix-python/2.12.1",
|
|
1148
1148
|
}
|
|
1149
1149
|
if api_key:
|
|
1150
1150
|
headers["Authorization"] = f"Bearer {api_key}"
|
|
@@ -32,6 +32,7 @@ tests/test_answer_context.py
|
|
|
32
32
|
tests/test_answer_decision.py
|
|
33
33
|
tests/test_answer_values.py
|
|
34
34
|
tests/test_chat_answer_options.py
|
|
35
|
+
tests/test_closest_match_envelope.py
|
|
35
36
|
tests/test_compact_search_default.py
|
|
36
37
|
tests/test_customer_developer_experience.py
|
|
37
38
|
tests/test_evidence_loop_resource.py
|
|
@@ -0,0 +1,521 @@
|
|
|
1
|
+
"""A response that matched nothing may carry the closest memories as context.
|
|
2
|
+
|
|
3
|
+
``POST /v1/search`` (server 1.4.53 and later): when no stored memory states
|
|
4
|
+
what the question asks but memories about the same person or thing exist, up
|
|
5
|
+
to 3 of them are returned, each ``low_confidence: true`` with
|
|
6
|
+
``low_confidence_reason: "closest_match_not_an_answer"``, beside
|
|
7
|
+
``no_match: true``, ``abstain_recommended: true``, ``answer_status:
|
|
8
|
+
"no_relevant_memory"`` and an empty ``evidence_ids``. They are context, never
|
|
9
|
+
an answer. SDK 2.12.0 rejected that page as malformed (HX-11).
|
|
10
|
+
|
|
11
|
+
The rule: an evidence row (``low_confidence`` not ``true``) must be bound to
|
|
12
|
+
``evidence_ids``; a candidate row (``low_confidence: true``) need not be.
|
|
13
|
+
``no_match: true`` may carry rows only when every row is a candidate and
|
|
14
|
+
``evidence_ids`` is empty.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
import copy
|
|
18
|
+
import random
|
|
19
|
+
|
|
20
|
+
import pytest
|
|
21
|
+
from hebbrix import should_answer, usable_memories
|
|
22
|
+
from hebbrix.answers import ANSWER_STATUSES
|
|
23
|
+
from hebbrix.resources import SearchResource, _canonical_search_envelope
|
|
24
|
+
from hebbrix.sync_client import SyncSearchResource
|
|
25
|
+
|
|
26
|
+
CLOSEST = "closest_match_not_an_answer"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def closest_row(memory_id="726b5a68-dfa0-4e7b-a54b-7b49feee4ddc", **fields):
|
|
30
|
+
# A row as the local server returns it (synthetic data).
|
|
31
|
+
return {
|
|
32
|
+
"memory_id": memory_id,
|
|
33
|
+
"id": memory_id,
|
|
34
|
+
"content": "My workshop labels are mint green.",
|
|
35
|
+
"score": 0.9985,
|
|
36
|
+
"rank": 1,
|
|
37
|
+
"low_confidence": True,
|
|
38
|
+
"low_confidence_reason": CLOSEST,
|
|
39
|
+
"evidence_role": "related",
|
|
40
|
+
"revision_status": "current",
|
|
41
|
+
"is_current": True,
|
|
42
|
+
"stated_at": "2026-12-05T04:53:53.170608Z",
|
|
43
|
+
**fields,
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def closest_match_response(rows=None, **fields):
|
|
48
|
+
# The page from the HX-11 report: nothing answers, the closest is returned.
|
|
49
|
+
rows = [closest_row()] if rows is None else rows
|
|
50
|
+
return {
|
|
51
|
+
"query": "What font do my workshop labels use?",
|
|
52
|
+
"results": rows,
|
|
53
|
+
"total": len(rows),
|
|
54
|
+
"search_type": "hybrid",
|
|
55
|
+
"answer_status": "no_relevant_memory",
|
|
56
|
+
"answer_finality": "none",
|
|
57
|
+
"no_match": True,
|
|
58
|
+
"abstain_recommended": True,
|
|
59
|
+
"confidence_reason": CLOSEST,
|
|
60
|
+
"query_confidence": 0.0,
|
|
61
|
+
"grounding": {
|
|
62
|
+
"status": "closest_match",
|
|
63
|
+
"reason": CLOSEST,
|
|
64
|
+
"answerability": "unanswerable",
|
|
65
|
+
"mode": "soft",
|
|
66
|
+
},
|
|
67
|
+
"indexing": {"status": "ready", "answer_risk": "none"},
|
|
68
|
+
"evidence_ids": [],
|
|
69
|
+
"evidence_claims": [],
|
|
70
|
+
"safety_contract_version": "search-safety-v1",
|
|
71
|
+
**fields,
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def evidence_row(memory_id, **fields):
|
|
76
|
+
return closest_row(
|
|
77
|
+
memory_id,
|
|
78
|
+
content=f"memory {memory_id}",
|
|
79
|
+
low_confidence=False,
|
|
80
|
+
low_confidence_reason=None,
|
|
81
|
+
evidence_role="answer",
|
|
82
|
+
**fields,
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def answerable_response(rows, evidence_ids, **fields):
|
|
87
|
+
return closest_match_response(
|
|
88
|
+
rows,
|
|
89
|
+
answer_status="answerable",
|
|
90
|
+
answer_finality="final",
|
|
91
|
+
no_match=False,
|
|
92
|
+
abstain_recommended=False,
|
|
93
|
+
confidence_reason="calibrated_match",
|
|
94
|
+
query_confidence=0.99,
|
|
95
|
+
grounding={"status": "supported", "answerability": "answerable"},
|
|
96
|
+
evidence_ids=evidence_ids,
|
|
97
|
+
**fields,
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def ids(rows):
|
|
102
|
+
return [row["memory_id"] for row in rows]
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
class RecordingAsyncClient:
|
|
106
|
+
def __init__(self, response):
|
|
107
|
+
self.response = response
|
|
108
|
+
|
|
109
|
+
async def post(self, path, **kwargs):
|
|
110
|
+
return copy.deepcopy(self.response)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
class RecordingSyncClient:
|
|
114
|
+
def __init__(self, response):
|
|
115
|
+
self.response = response
|
|
116
|
+
|
|
117
|
+
def post(self, path, **kwargs):
|
|
118
|
+
return copy.deepcopy(self.response)
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
# ------------------------------------------------------------ the HX-11 page
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def assert_closest_match_kept(envelope, response):
|
|
125
|
+
assert "sdk_safety_reason" not in envelope
|
|
126
|
+
assert envelope["results"] == response["results"] # labels intact
|
|
127
|
+
assert envelope["total"] == 1
|
|
128
|
+
assert envelope["no_match"] is True
|
|
129
|
+
assert envelope["abstain_recommended"] is True
|
|
130
|
+
assert envelope["evidence_ids"] == []
|
|
131
|
+
assert envelope["query_confidence"] == 0.0
|
|
132
|
+
assert envelope["confidence_reason"] == CLOSEST
|
|
133
|
+
|
|
134
|
+
decision = should_answer(envelope)
|
|
135
|
+
assert not decision
|
|
136
|
+
assert decision.reason == "no_relevant_memory"
|
|
137
|
+
assert decision.action == "say_unknown"
|
|
138
|
+
assert decision.memories == []
|
|
139
|
+
assert decision.context == response["results"]
|
|
140
|
+
assert decision.context[0]["low_confidence"] is True
|
|
141
|
+
assert decision.context[0]["low_confidence_reason"] == CLOSEST
|
|
142
|
+
assert usable_memories(envelope) == []
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def test_the_closest_match_page_is_kept_and_means_say_unknown():
|
|
146
|
+
response = closest_match_response()
|
|
147
|
+
envelope = _canonical_search_envelope(response)
|
|
148
|
+
assert_closest_match_kept(envelope, response)
|
|
149
|
+
assert response == closest_match_response() # caller's dict not mutated
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
@pytest.mark.asyncio
|
|
153
|
+
async def test_search_with_proof_keeps_the_closest_match_rows():
|
|
154
|
+
response = closest_match_response()
|
|
155
|
+
envelope = await SearchResource(RecordingAsyncClient(response)).search_with_proof(
|
|
156
|
+
"What font do my workshop labels use?"
|
|
157
|
+
)
|
|
158
|
+
assert_closest_match_kept(envelope, response)
|
|
159
|
+
rows = await SearchResource(RecordingAsyncClient(response)).search(
|
|
160
|
+
"What font do my workshop labels use?"
|
|
161
|
+
)
|
|
162
|
+
assert rows == response["results"]
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def test_sync_search_with_proof_keeps_the_closest_match_rows():
|
|
166
|
+
response = closest_match_response()
|
|
167
|
+
resource = SyncSearchResource(RecordingSyncClient(response))
|
|
168
|
+
envelope = resource.search_with_proof("What font do my workshop labels use?")
|
|
169
|
+
assert_closest_match_kept(envelope, response)
|
|
170
|
+
assert resource.search("What font do my workshop labels use?") == response["results"]
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def test_a_closest_match_row_that_does_not_state_its_revision_is_still_context():
|
|
174
|
+
row = closest_row()
|
|
175
|
+
del row["revision_status"]
|
|
176
|
+
response = closest_match_response([row])
|
|
177
|
+
decision = should_answer(_canonical_search_envelope(response))
|
|
178
|
+
assert decision.action == "say_unknown"
|
|
179
|
+
assert decision.context == [row]
|
|
180
|
+
assert decision.memories == []
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def test_up_to_three_closest_matches_are_all_context():
|
|
184
|
+
rows = [closest_row(f"near-{index}") for index in range(3)]
|
|
185
|
+
envelope = _canonical_search_envelope(closest_match_response(rows))
|
|
186
|
+
assert ids(envelope["results"]) == ["near-0", "near-1", "near-2"]
|
|
187
|
+
assert envelope["total"] == 3
|
|
188
|
+
decision = should_answer(envelope)
|
|
189
|
+
assert decision.action == "say_unknown"
|
|
190
|
+
assert ids(decision.context) == ["near-0", "near-1", "near-2"]
|
|
191
|
+
assert decision.memories == []
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def test_history_never_answers_from_a_closest_match():
|
|
195
|
+
rows = [
|
|
196
|
+
closest_row("old", revision_status="superseded", is_current=False),
|
|
197
|
+
closest_row("near"),
|
|
198
|
+
]
|
|
199
|
+
envelope = _canonical_search_envelope(closest_match_response(rows))
|
|
200
|
+
for options in ({"history": True}, {"history": True, "require_ready": True}):
|
|
201
|
+
decision = should_answer(envelope, **options)
|
|
202
|
+
assert not decision
|
|
203
|
+
assert decision.memories == []
|
|
204
|
+
assert ids(decision.context) == ["old", "near"]
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def test_reason_sources_beside_no_match_follow_the_same_rule():
|
|
208
|
+
source = closest_row()
|
|
209
|
+
response = closest_match_response(answer="It might be mint green.", citations=["x"])
|
|
210
|
+
del response["results"]
|
|
211
|
+
response["sources"] = [source]
|
|
212
|
+
envelope = _canonical_search_envelope(response, rows_key="sources")
|
|
213
|
+
assert "sdk_safety_reason" not in envelope
|
|
214
|
+
assert envelope["sources"] == [source]
|
|
215
|
+
assert envelope["answer"] is None and envelope["citations"] == []
|
|
216
|
+
decision = should_answer(envelope)
|
|
217
|
+
assert decision.action == "say_unknown"
|
|
218
|
+
assert decision.context == [source]
|
|
219
|
+
assert decision.memories == []
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
# ------------------------------------------------------- still fails closed
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def assert_failed_closed(envelope, reason):
|
|
226
|
+
assert envelope["sdk_safety_reason"] == reason
|
|
227
|
+
assert envelope["results"] == []
|
|
228
|
+
assert envelope["total"] == 0
|
|
229
|
+
assert envelope["no_match"] is True
|
|
230
|
+
assert envelope["evidence_ids"] == []
|
|
231
|
+
decision = should_answer(envelope)
|
|
232
|
+
assert not decision
|
|
233
|
+
assert decision.reason == "malformed_response"
|
|
234
|
+
assert decision.action == "abstain"
|
|
235
|
+
assert decision.memories == [] and decision.context == []
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def test_no_match_beside_an_unbound_evidence_row_fails_closed():
|
|
239
|
+
rows = [evidence_row("fact")]
|
|
240
|
+
envelope = _canonical_search_envelope(closest_match_response(rows))
|
|
241
|
+
assert_failed_closed(envelope, "rows_not_bound_to_evidence_ids")
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def test_no_match_beside_a_bound_evidence_row_fails_closed():
|
|
245
|
+
rows = [evidence_row("fact")]
|
|
246
|
+
response = closest_match_response(rows, evidence_ids=["fact"])
|
|
247
|
+
assert_failed_closed(
|
|
248
|
+
_canonical_search_envelope(response), "no_match_contains_evidence"
|
|
249
|
+
)
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
def test_no_match_beside_one_evidence_row_among_candidates_fails_closed():
|
|
253
|
+
rows = [closest_row("near"), evidence_row("fact")]
|
|
254
|
+
for evidence_ids, reason in (
|
|
255
|
+
([], "rows_not_bound_to_evidence_ids"),
|
|
256
|
+
(["fact"], "no_match_contains_evidence"),
|
|
257
|
+
):
|
|
258
|
+
response = closest_match_response(rows, evidence_ids=evidence_ids)
|
|
259
|
+
assert_failed_closed(_canonical_search_envelope(response), reason)
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
def test_no_match_with_candidates_listed_as_evidence_fails_closed():
|
|
263
|
+
response = closest_match_response(evidence_ids=[closest_row()["memory_id"]])
|
|
264
|
+
assert_failed_closed(
|
|
265
|
+
_canonical_search_envelope(response), "no_match_contains_evidence"
|
|
266
|
+
)
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
@pytest.mark.parametrize("flag", ["missing", None, "true", 1, "yes"])
|
|
270
|
+
def test_a_row_that_does_not_say_it_is_a_candidate_is_evidence(flag):
|
|
271
|
+
row = closest_row()
|
|
272
|
+
if flag == "missing":
|
|
273
|
+
del row["low_confidence"]
|
|
274
|
+
else:
|
|
275
|
+
row["low_confidence"] = flag
|
|
276
|
+
envelope = _canonical_search_envelope(closest_match_response([row]))
|
|
277
|
+
assert_failed_closed(envelope, "rows_not_bound_to_evidence_ids")
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
def test_an_answerable_page_with_an_unbound_evidence_row_fails_closed():
|
|
281
|
+
rows = [evidence_row("bound"), evidence_row("unbound")]
|
|
282
|
+
envelope = _canonical_search_envelope(answerable_response(rows, ["bound"]))
|
|
283
|
+
assert_failed_closed(envelope, "rows_not_bound_to_evidence_ids")
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
# --------------------------------------- candidates beside answerable rows
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
def test_an_unlisted_candidate_on_an_answerable_page_is_context():
|
|
290
|
+
rows = [
|
|
291
|
+
evidence_row("fact"),
|
|
292
|
+
closest_row("candidate", low_confidence_reason="claim_grounding_unverified"),
|
|
293
|
+
]
|
|
294
|
+
response = answerable_response(rows, ["fact"])
|
|
295
|
+
envelope = _canonical_search_envelope(response)
|
|
296
|
+
assert "sdk_safety_reason" not in envelope
|
|
297
|
+
assert envelope["results"] == rows
|
|
298
|
+
assert envelope["no_match"] is False
|
|
299
|
+
decision = should_answer(envelope)
|
|
300
|
+
assert decision
|
|
301
|
+
assert decision.action == "answer"
|
|
302
|
+
assert ids(decision.memories) == ["fact"]
|
|
303
|
+
assert ids(decision.context) == ["candidate"]
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
def test_a_listed_candidate_on_an_answerable_page_is_still_context():
|
|
307
|
+
rows = [evidence_row("fact"), closest_row("candidate")]
|
|
308
|
+
envelope = _canonical_search_envelope(
|
|
309
|
+
answerable_response(rows, ["fact", "candidate"])
|
|
310
|
+
)
|
|
311
|
+
decision = should_answer(envelope)
|
|
312
|
+
assert ids(decision.memories) == ["fact"]
|
|
313
|
+
assert ids(decision.context) == ["candidate"]
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
def test_an_answerable_page_of_unlisted_candidates_only_never_answers():
|
|
317
|
+
rows = [closest_row("candidate")]
|
|
318
|
+
envelope = _canonical_search_envelope(answerable_response(rows, []))
|
|
319
|
+
assert envelope["results"] == rows
|
|
320
|
+
decision = should_answer(envelope)
|
|
321
|
+
assert not decision
|
|
322
|
+
assert decision.reason == "no_usable_memories"
|
|
323
|
+
assert decision.memories == []
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
def test_an_empty_no_match_page_is_unchanged():
|
|
327
|
+
envelope = _canonical_search_envelope(
|
|
328
|
+
closest_match_response([], confidence_reason="no_relevant_memory")
|
|
329
|
+
)
|
|
330
|
+
assert "sdk_safety_reason" not in envelope
|
|
331
|
+
assert envelope["results"] == [] and envelope["total"] == 0
|
|
332
|
+
decision = should_answer(envelope)
|
|
333
|
+
assert decision.action == "say_unknown"
|
|
334
|
+
assert decision.context == []
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
# ------------------------------------------------------- property: seeded
|
|
338
|
+
|
|
339
|
+
SEED = 20261205
|
|
340
|
+
ENVELOPES = 500
|
|
341
|
+
_ABSENT = object()
|
|
342
|
+
_PAST = ("superseded", "historical")
|
|
343
|
+
_OPTIONS = (
|
|
344
|
+
{},
|
|
345
|
+
{"history": True},
|
|
346
|
+
{"require_ready": True},
|
|
347
|
+
{"history": True, "require_ready": True},
|
|
348
|
+
)
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
def _put(target, key, value):
|
|
352
|
+
if value is not _ABSENT:
|
|
353
|
+
target[key] = value
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
def _random_row(rng, index):
|
|
357
|
+
memory_id = f"m{index}"
|
|
358
|
+
row = {"memory_id": memory_id, "id": memory_id, "content": f"memory {index}"}
|
|
359
|
+
flag = rng.choice([True, True, False, False, _ABSENT, None, "true"])
|
|
360
|
+
_put(row, "low_confidence", flag)
|
|
361
|
+
if flag is True:
|
|
362
|
+
row["low_confidence_reason"] = rng.choice(
|
|
363
|
+
[CLOSEST, "claim_grounding_unverified", "historical_record", "superseded_version"]
|
|
364
|
+
)
|
|
365
|
+
revision = rng.choice(["current", "current", "superseded", "historical", "conflicting", _ABSENT])
|
|
366
|
+
_put(row, "revision_status", revision)
|
|
367
|
+
_put(row, "is_current", rng.choice([revision == "current", True, False, _ABSENT]))
|
|
368
|
+
_put(row, "evidence_role", rng.choice(["answer", "related", "applicable_rule", _ABSENT]))
|
|
369
|
+
return row
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
def _random_envelope(rng):
|
|
373
|
+
"""A response of any shape, biased toward the pages this rule is about."""
|
|
374
|
+
|
|
375
|
+
rows = [_random_row(rng, index) for index in range(rng.randint(0, 4))]
|
|
376
|
+
row_ids = ids(rows)
|
|
377
|
+
shape = rng.choice(["closest", "closest", "answerable", "answerable", "superseded", "free", "free"])
|
|
378
|
+
if shape == "closest":
|
|
379
|
+
for row in rows:
|
|
380
|
+
row["low_confidence"] = True
|
|
381
|
+
row["low_confidence_reason"] = CLOSEST
|
|
382
|
+
if rows and rng.random() < 0.3: # one perturbation away from the page
|
|
383
|
+
victim = rng.choice(rows)
|
|
384
|
+
victim["low_confidence"] = rng.choice([False, None, "true", _ABSENT])
|
|
385
|
+
if victim["low_confidence"] is _ABSENT:
|
|
386
|
+
del victim["low_confidence"]
|
|
387
|
+
evidence_ids = [] if rng.random() < 0.8 else rng.sample(row_ids, k=min(1, len(row_ids)))
|
|
388
|
+
fields = {
|
|
389
|
+
"answer_status": "no_relevant_memory",
|
|
390
|
+
"no_match": True,
|
|
391
|
+
"abstain_recommended": True,
|
|
392
|
+
"confidence_reason": CLOSEST,
|
|
393
|
+
"grounding": {"status": "closest_match", "answerability": "unanswerable"},
|
|
394
|
+
}
|
|
395
|
+
elif shape in ("answerable", "superseded"):
|
|
396
|
+
# Well formed: every evidence row bound, candidates listed or not.
|
|
397
|
+
evidence_ids = [
|
|
398
|
+
row["memory_id"]
|
|
399
|
+
for row in rows
|
|
400
|
+
if row.get("low_confidence") is not True or rng.random() < 0.5
|
|
401
|
+
]
|
|
402
|
+
if rng.random() < 0.1:
|
|
403
|
+
evidence_ids.append("elsewhere")
|
|
404
|
+
fields = {
|
|
405
|
+
"answer_status": shape,
|
|
406
|
+
"no_match": False,
|
|
407
|
+
"abstain_recommended": shape == "superseded",
|
|
408
|
+
"confidence_reason": "calibrated_match" if shape == "answerable" else None,
|
|
409
|
+
"grounding": {"status": "supported", "answerability": "answerable"},
|
|
410
|
+
}
|
|
411
|
+
else:
|
|
412
|
+
binding = rng.choice(["all", "evidence", "none", "some", "extra"])
|
|
413
|
+
if binding == "all":
|
|
414
|
+
evidence_ids = list(row_ids)
|
|
415
|
+
elif binding == "evidence":
|
|
416
|
+
evidence_ids = [row["memory_id"] for row in rows if row.get("low_confidence") is not True]
|
|
417
|
+
elif binding == "none":
|
|
418
|
+
evidence_ids = []
|
|
419
|
+
elif binding == "some":
|
|
420
|
+
evidence_ids = [value for value in row_ids if rng.random() < 0.5]
|
|
421
|
+
else:
|
|
422
|
+
evidence_ids = list(row_ids) + ["elsewhere"]
|
|
423
|
+
fields = {
|
|
424
|
+
"answer_status": rng.choice([*ANSWER_STATUSES, "future_status"]),
|
|
425
|
+
"no_match": rng.choice([False, False, True, "true"]),
|
|
426
|
+
"abstain_recommended": rng.choice([False, True]),
|
|
427
|
+
"confidence_reason": rng.choice(
|
|
428
|
+
[None, CLOSEST, "authorization_not_verifiable", "soft_claim_candidate", "calibrated_match"]
|
|
429
|
+
),
|
|
430
|
+
"grounding": rng.choice(
|
|
431
|
+
[
|
|
432
|
+
{"status": "supported", "answerability": "answerable"},
|
|
433
|
+
{"status": "soft_candidate", "answerability": "retrieval_only"},
|
|
434
|
+
{"status": "supported", "answerability": "context_only"},
|
|
435
|
+
]
|
|
436
|
+
),
|
|
437
|
+
}
|
|
438
|
+
envelope = {
|
|
439
|
+
"results": rows,
|
|
440
|
+
"total": len(rows),
|
|
441
|
+
"query_confidence": rng.choice([0.0, 0.49, 0.99]),
|
|
442
|
+
"evidence_ids": evidence_ids,
|
|
443
|
+
"safety_contract_version": rng.choice(["search-safety-v1"] * 19 + ["future"]),
|
|
444
|
+
**fields,
|
|
445
|
+
}
|
|
446
|
+
_put(envelope, "decision_abstained", rng.choice([False, False, True, _ABSENT, _ABSENT]))
|
|
447
|
+
_put(
|
|
448
|
+
envelope,
|
|
449
|
+
"indexing",
|
|
450
|
+
rng.choice(
|
|
451
|
+
[
|
|
452
|
+
{"status": "ready", "answer_risk": "none"},
|
|
453
|
+
{"status": "ready", "answer_risk": "none"},
|
|
454
|
+
{"status": "incomplete", "answer_risk": "may_change"},
|
|
455
|
+
_ABSENT,
|
|
456
|
+
]
|
|
457
|
+
),
|
|
458
|
+
)
|
|
459
|
+
if rng.random() < 0.15: # Reason's shape: cited sources, not search rows
|
|
460
|
+
envelope["sources"] = envelope.pop("results")
|
|
461
|
+
envelope.pop("total")
|
|
462
|
+
envelope["answer"] = rng.choice([None, "An answer."])
|
|
463
|
+
return envelope
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+
def _check_decision(decision, response, options):
|
|
467
|
+
if response.get("no_match") is True:
|
|
468
|
+
assert decision.memories == [], "a row answered beside no_match"
|
|
469
|
+
for row in decision.memories:
|
|
470
|
+
if row.get("low_confidence") is True:
|
|
471
|
+
assert options.get("history") and row.get("revision_status") in _PAST, (
|
|
472
|
+
"a candidate row answered outside a history question"
|
|
473
|
+
)
|
|
474
|
+
|
|
475
|
+
|
|
476
|
+
def test_property_candidates_never_answer_and_no_match_never_answers():
|
|
477
|
+
rng = random.Random(SEED)
|
|
478
|
+
seen = {"closest_kept": 0, "failed_closed": 0, "answered": 0, "history_candidate": 0}
|
|
479
|
+
for _ in range(ENVELOPES):
|
|
480
|
+
raw = _random_envelope(rng)
|
|
481
|
+
rows_key = "sources" if "sources" in raw else "results"
|
|
482
|
+
pristine = copy.deepcopy(raw)
|
|
483
|
+
envelope = _canonical_search_envelope(raw, rows_key=rows_key)
|
|
484
|
+
assert raw == pristine # the caller's response is never mutated
|
|
485
|
+
kept = envelope[rows_key]
|
|
486
|
+
safety = envelope.get("sdk_safety_reason")
|
|
487
|
+
|
|
488
|
+
# The validator's own contract.
|
|
489
|
+
if safety and safety != "degraded_evidence_preserved":
|
|
490
|
+
assert kept == [] and envelope["no_match"] is True
|
|
491
|
+
assert envelope["evidence_ids"] == []
|
|
492
|
+
seen["failed_closed"] += 1
|
|
493
|
+
else:
|
|
494
|
+
bound = set(envelope["evidence_ids"])
|
|
495
|
+
for row in kept:
|
|
496
|
+
if row.get("low_confidence") is not True:
|
|
497
|
+
assert row["memory_id"] in bound
|
|
498
|
+
if envelope["no_match"] is True:
|
|
499
|
+
assert all(row.get("low_confidence") is True for row in kept)
|
|
500
|
+
assert envelope["evidence_ids"] == []
|
|
501
|
+
seen["closest_kept"] += bool(kept)
|
|
502
|
+
if raw.get("no_match") is True:
|
|
503
|
+
assert envelope["no_match"] is True
|
|
504
|
+
|
|
505
|
+
# The decision, on what the SDK returns and on the raw response alike.
|
|
506
|
+
for options in _OPTIONS:
|
|
507
|
+
decision = should_answer(envelope, **options)
|
|
508
|
+
_check_decision(decision, envelope, options)
|
|
509
|
+
_check_decision(decision, raw, options)
|
|
510
|
+
assert all(row in kept for row in decision.memories)
|
|
511
|
+
if decision.memories:
|
|
512
|
+
seen["answered"] += 1
|
|
513
|
+
if any(row.get("low_confidence") is True for row in decision.memories):
|
|
514
|
+
seen["history_candidate"] += 1
|
|
515
|
+
_check_decision(should_answer(raw, **options), raw, options)
|
|
516
|
+
|
|
517
|
+
# The run exercised every path the invariant is about.
|
|
518
|
+
assert seen["closest_kept"] >= 40, seen
|
|
519
|
+
assert seen["failed_closed"] >= 40, seen
|
|
520
|
+
assert seen["answered"] >= 40, seen
|
|
521
|
+
assert seen["history_candidate"] >= 5, seen
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|