Skip to content

Commit 9866c2c

Browse files
fix: isolate trust resolution and recall depth
1 parent 1a475ea commit 9866c2c

4 files changed

Lines changed: 59 additions & 6 deletions

File tree

engraphis/core/engine.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ def _resolve_and_store(self, content: str, *, text: str, vec: Optional[np.ndarra
518518
record for record in claim_history
519519
if record.valid_from is not None and record.valid_from <= valid_from
520520
and (record.valid_to is None or valid_from < record.valid_to)
521-
and (trusted_write or not provenance_is_trusted(record.provenance))
521+
and provenance_is_trusted(record.provenance) == trusted_write
522522
]
523523
if predecessors:
524524
predecessor = max(
@@ -914,7 +914,7 @@ def _resolve_against_neighbors(self, text: str, vec: np.ndarray, *, workspace_id
914914
if (nrec and nrec.workspace_id == workspace_id and nrec.repo_id == repo_id
915915
and nrec.scope == scope and nrec.mtype == mtype
916916
and (scope != Scope.SESSION or nrec.session_id == session_id)
917-
and (trusted_write or not provenance_is_trusted(nrec.provenance))
917+
and provenance_is_trusted(nrec.provenance) == trusted_write
918918
and (memory_matches_filter(nrec, flt)
919919
or (current_fallback and nrec.expired_at is None
920920
and nrec.valid_to is None))):
@@ -945,7 +945,7 @@ def _resolve_against_neighbors(self, text: str, vec: np.ndarray, *, workspace_id
945945
authoritative = [
946946
record for record in claim_history
947947
if memory_matches_filter(record, flt, at=valid_at)
948-
and (trusted_write or not provenance_is_trusted(record.provenance))
948+
and provenance_is_trusted(record.provenance) == trusted_write
949949
]
950950
if not authoritative and valid_at is not None:
951951
# A backfill before the first recorded version has no visible
@@ -957,7 +957,7 @@ def _resolve_against_neighbors(self, text: str, vec: np.ndarray, *, workspace_id
957957
if record.expired_at is None
958958
and record.valid_from is not None
959959
and record.valid_from > valid_at
960-
and (trusted_write or not provenance_is_trusted(record.provenance))
960+
and provenance_is_trusted(record.provenance) == trusted_write
961961
]
962962
if later:
963963
authoritative = [min(

engraphis/core/recall.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ def recall(self, query: str, flt: Optional[SearchFilter] = None, *, k: int = 8,
145145
# vector indexes do not carry provenance. Over-fetch so external records cannot
146146
# crowd trusted evidence out of a grounded/adaptive context candidate set.
147147
prompt_only = bool(prompt_only or not include_untrusted)
148-
arm_candidate_k = candidate_k if not prompt_only else min(
149-
250, max(candidate_k, candidate_k * 4)
148+
arm_candidate_k = candidate_k if not prompt_only else (
149+
candidate_k + min(250, candidate_k * 3)
150150
)
151151
if config.vector:
152152
qvec = self.embedder.embed([query])[0]

tests/test_poisoning.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,31 @@ def test_untrusted_write_cannot_resolve_or_link_to_trusted_memory():
338338
assert trusted["id"] in ordinary_ids
339339
assert external["id"] not in ordinary_ids
340340
assert {trusted["id"], external["id"]} <= inspection_ids
341+
342+
343+
def test_trusted_write_creates_an_approved_record_for_an_untrusted_duplicate():
344+
eng, wid, rid = _engine()
345+
external = eng.remember_with_resolution(
346+
"Production releases deploy to the blue environment.",
347+
workspace_id=wid,
348+
repo_id=rid,
349+
metadata={"provenance": {"source": "web", "trusted": False}},
350+
)
351+
352+
approved = eng.remember_with_resolution(
353+
"Production releases deploy to the blue environment.",
354+
workspace_id=wid,
355+
repo_id=rid,
356+
metadata={"provenance": {"source": "human", "trusted": True}},
357+
)
358+
359+
assert approved["op"] == "add"
360+
assert approved["id"] != external["id"]
361+
assert eng.store.get_memory(approved["id"]).provenance["trusted"] is True
362+
ordinary_ids = {
363+
chunk["id"] for chunk in eng.recall(
364+
"Where do production releases deploy?", workspace_id=wid, repo_id=rid, k=10,
365+
).chunks
366+
}
367+
assert approved["id"] in ordinary_ids
368+
assert external["id"] not in ordinary_ids

tests/test_recall.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,31 @@ def test_lexical_recall_is_filtered_before_candidate_limit():
314314
assert [c["id"] for c in res.chunks] == [wanted]
315315

316316

317+
def test_prompt_overfetch_never_reduces_the_requested_candidate_depth():
318+
store = Store(":memory:")
319+
emb = DeterministicEmbedder(256)
320+
index = NumpyVectorIndex(store)
321+
requested: list[int] = []
322+
original_search = index.search
323+
324+
def recording_search(query, k, filter=None):
325+
requested.append(k)
326+
return original_search(query, k, filter=filter)
327+
328+
index.search = recording_search
329+
eng = RecallEngine(store, emb, index, IdentityReranker())
330+
wid = store.get_or_create_workspace("w")
331+
_add(store, emb, wid, None, "A sufficiently deep candidate set remains available.")
332+
333+
result = eng.recall(
334+
"candidate depth", SearchFilter(workspace_id=wid), k=1, candidate_k=500,
335+
)
336+
337+
assert result.candidate_k_requested == 500
338+
assert result.candidate_k_used == 500
339+
assert requested[0] == 750
340+
341+
317342
def test_graph_arm_does_not_match_entity_names_inside_other_words():
318343
from engraphis.core.interfaces import Edge, Node
319344

0 commit comments

Comments
 (0)