feat(provider): split memory search scope_filter → scope_all + scope_any (bifrost 0.8.0/wire v0.6)
Repin bifrost 0.7.0→0.8.0 and reimplement the memory store's search scope filter to the v0.6 split (#11): scope_all (AND/intersection) + scope_any (OR/union over a list of conjunctive scopes), at parity with the v0.6 reference _matches_scope / _validate_scope. No-compat: scope_filter removed. scope_any is the union-visibility primitive that resolves the #295/#297 silent-zero AND foot-gun — a subset-scoped chunk now recalls via an OR member. End-to-end cold recall now gated only on Worldtree emitting scope_any on its recall path (#297, upstream). - store: search(scope_all, scope_any); _scope_subset + _matches_scope + _validate_scope - contract v1.2: search FN sig, INV-005 recomposed, PRE-003 both fields, scope_any_union test - tests: scope_any union, scope_all∧scope_any compose, both-empty match-all; parity vs real 0.8.0 dispatch (433 green) - #17 contract: sync stale scope_filter/_scope_matches-AND refs to scope_all/scope_any - runbook + persistent-memory updated; provider bounced onto 0.8.0 (fresh empty db) v0.17.6
This commit is contained in:
@@ -176,7 +176,7 @@ async def test_basic_search_ranks_by_cosine_with_recalled_view():
|
||||
idempotency_key="k1",
|
||||
ctx=_ctx(),
|
||||
)
|
||||
results = await store.search(_vec(1.0, 0.0), top_k=2, scope_filter=scope)
|
||||
results = await store.search(_vec(1.0, 0.0), top_k=2, scope_all=scope)
|
||||
assert [r["chunk_id"] for r in results] == ["c1", "c3"] # nearest to [1,0] by cosine
|
||||
top = results[0]
|
||||
assert top["chunk"] == c1 # verbatim chunk, no revision attached
|
||||
@@ -196,7 +196,7 @@ async def test_scope_isolation_excludes_other_scope_even_if_closer():
|
||||
idempotency_key="k1",
|
||||
ctx=_ctx(),
|
||||
)
|
||||
results = await store.search(_vec(1.0, 0.0), top_k=2, scope_filter={"end_user": "u1"})
|
||||
results = await store.search(_vec(1.0, 0.0), top_k=2, scope_all={"end_user": "u1"})
|
||||
assert [r["chunk_id"] for r in results] == ["u1-far"] # u2-near excluded despite ranking first
|
||||
|
||||
|
||||
@@ -219,19 +219,28 @@ async def test_search_wrong_vector_dim_rejected():
|
||||
await store.search([1.0, 0.0], top_k=5)
|
||||
|
||||
|
||||
async def test_search_non_dict_scope_filter_rejected():
|
||||
# search STEP 1: scope_filter must be a flat {axis: value} dict
|
||||
async def test_search_non_dict_scope_all_rejected():
|
||||
# search STEP 1: scope_all must be a flat {axis: value} dict
|
||||
store = open_memory_store(":memory:", embedding_dim=EMBEDDING_DIM)
|
||||
with pytest.raises(InvalidArguments):
|
||||
await store.search(_vec(1.0), top_k=5, scope_filter="u1")
|
||||
await store.search(_vec(1.0), top_k=5, scope_all="u1")
|
||||
|
||||
|
||||
async def test_search_non_list_scope_any_rejected():
|
||||
# search STEP 1: scope_any must be a LIST of {axis: value} dicts (#11)
|
||||
store = open_memory_store(":memory:", embedding_dim=EMBEDDING_DIM)
|
||||
with pytest.raises(InvalidArguments):
|
||||
await store.search(_vec(1.0), top_k=5, scope_any={"end_user": "u1"})
|
||||
|
||||
|
||||
async def test_search_out_of_lattice_scope_axis_rejected():
|
||||
# v0.5 scope lattice = {end_user, group, tenant, agent_self}; an axis outside
|
||||
# it is InvalidFilter (-> memory.invalid_filter 400), matching bifrost's reference.
|
||||
# v0.6 scope lattice = {end_user, group, tenant, agent_self}; an axis outside
|
||||
# it is InvalidFilter (-> memory.invalid_filter 400) in EITHER field, matching the reference.
|
||||
store = open_memory_store(":memory:", embedding_dim=EMBEDDING_DIM)
|
||||
with pytest.raises(InvalidFilter):
|
||||
await store.search(_vec(1.0), top_k=5, scope_filter={"bogus_axis": "x"})
|
||||
await store.search(_vec(1.0), top_k=5, scope_all={"bogus_axis": "x"})
|
||||
with pytest.raises(InvalidFilter):
|
||||
await store.search(_vec(1.0), top_k=5, scope_any=[{"bogus_axis": "x"}])
|
||||
|
||||
|
||||
async def test_search_agent_self_axis_accepted():
|
||||
@@ -243,7 +252,7 @@ async def test_search_agent_self_axis_accepted():
|
||||
ctx=_ctx(),
|
||||
)
|
||||
results = await store.search(
|
||||
_vec(1.0), top_k=5, scope_filter={"agent_self": "ratatoskr:smoke"}
|
||||
_vec(1.0), top_k=5, scope_all={"agent_self": "ratatoskr:smoke"}
|
||||
)
|
||||
assert [r["chunk_id"] for r in results] == ["a1"]
|
||||
|
||||
@@ -252,7 +261,67 @@ async def test_search_top_k_zero_returns_empty():
|
||||
# POST-001: at most top_k — zero means zero
|
||||
store = open_memory_store(":memory:", embedding_dim=EMBEDDING_DIM)
|
||||
await store.upsert_many([_chunk("c1")], idempotency_key="k1", ctx=_ctx())
|
||||
assert await store.search(_vec(1.0), top_k=0, scope_filter={"end_user": "u1"}) == []
|
||||
assert await store.search(_vec(1.0), top_k=0, scope_all={"end_user": "u1"}) == []
|
||||
|
||||
|
||||
async def test_search_no_scope_matches_all():
|
||||
# v0.6: both fields empty -> no scope constraint (match all, within top_k).
|
||||
store = open_memory_store(":memory:", embedding_dim=EMBEDDING_DIM)
|
||||
await store.upsert_many(
|
||||
[
|
||||
_chunk("u1", scope={"end_user": "u1"}),
|
||||
_chunk("u2", scope={"end_user": "u2"}),
|
||||
],
|
||||
idempotency_key="k1",
|
||||
ctx=_ctx(),
|
||||
)
|
||||
results = await store.search(_vec(1.0), top_k=10)
|
||||
assert {r["chunk_id"] for r in results} == {"u1", "u2"}
|
||||
|
||||
|
||||
async def test_search_scope_any_unions_across_scopes():
|
||||
# v0.6 (#11): scope_any is OR/union over a LIST of conjunctive scopes. A {end_user:u1}
|
||||
# chunk AND an {agent_self:a} chunk are BOTH recalled in ONE call — the capability
|
||||
# that resolves the #295/#297 silent-zero AND foot-gun (subset-scoped chunks now recall).
|
||||
store = open_memory_store(":memory:", embedding_dim=EMBEDDING_DIM)
|
||||
await store.upsert_many(
|
||||
[
|
||||
_chunk("subj", embedding=_vec(1.0, 0.0), scope={"end_user": "u1"}),
|
||||
_chunk("self", embedding=_vec(0.9, 0.1), scope={"agent_self": "ratatoskr:sindra"}),
|
||||
_chunk("other", embedding=_vec(0.8, 0.2), scope={"end_user": "u9"}),
|
||||
],
|
||||
idempotency_key="k1",
|
||||
ctx=_ctx(),
|
||||
)
|
||||
results = await store.search(
|
||||
_vec(1.0, 0.0),
|
||||
top_k=10,
|
||||
scope_any=[{"end_user": "u1"}, {"agent_self": "ratatoskr:sindra"}],
|
||||
)
|
||||
assert {r["chunk_id"] for r in results} == {"subj", "self"} # union; u9 excluded
|
||||
|
||||
|
||||
async def test_search_scope_all_and_scope_any_compose_by_and():
|
||||
# v0.6: a record passes iff (record ⊇ scope_all) AND (matches ≥1 scope_any element).
|
||||
store = open_memory_store(":memory:", embedding_dim=EMBEDDING_DIM)
|
||||
await store.upsert_many(
|
||||
[
|
||||
# tenant t1 AND (end_user u1 OR u2) — only these pass
|
||||
_chunk("t1u1", embedding=_vec(1.0, 0.0), scope={"tenant": "t1", "end_user": "u1"}),
|
||||
_chunk("t1u2", embedding=_vec(0.9, 0.1), scope={"tenant": "t1", "end_user": "u2"}),
|
||||
_chunk("t1u9", embedding=_vec(0.8, 0.2), scope={"tenant": "t1", "end_user": "u9"}),
|
||||
_chunk("t2u1", embedding=_vec(0.7, 0.3), scope={"tenant": "t2", "end_user": "u1"}),
|
||||
],
|
||||
idempotency_key="k1",
|
||||
ctx=_ctx(),
|
||||
)
|
||||
results = await store.search(
|
||||
_vec(1.0, 0.0),
|
||||
top_k=10,
|
||||
scope_all={"tenant": "t1"},
|
||||
scope_any=[{"end_user": "u1"}, {"end_user": "u2"}],
|
||||
)
|
||||
assert {r["chunk_id"] for r in results} == {"t1u1", "t1u2"} # t1u9 fails any; t2u1 fails all
|
||||
|
||||
|
||||
async def test_scope_isolation_fills_top_k_from_in_scope_past_higher_out_of_scope():
|
||||
@@ -269,7 +338,7 @@ async def test_scope_isolation_fills_top_k_from_in_scope_past_higher_out_of_scop
|
||||
idempotency_key="k1",
|
||||
ctx=_ctx(),
|
||||
)
|
||||
results = await store.search(_vec(1.0, 0.0), top_k=2, scope_filter={"end_user": "u1"})
|
||||
results = await store.search(_vec(1.0, 0.0), top_k=2, scope_all={"end_user": "u1"})
|
||||
# exactly top_k in-scope (the 2 nearest u1 chunks); the higher-ranked u2 chunk is excluded
|
||||
assert [r["chunk_id"] for r in results] == ["u1-near", "u1-mid"]
|
||||
|
||||
@@ -298,7 +367,7 @@ async def test_delete_hit_removes_chunk_and_vec_row():
|
||||
assert _row_count(store, "memory_chunks") == 1
|
||||
assert _row_count(store, "memory_vec") == 1 # c1's vec row gone too (no orphan)
|
||||
# delete_hit: search no longer surfaces it (vec/chunk coupling held)
|
||||
hits = await store.search(_vec(1.0), top_k=5, scope_filter={"end_user": "u1"})
|
||||
hits = await store.search(_vec(1.0), top_k=5, scope_all={"end_user": "u1"})
|
||||
assert all(r["chunk_id"] != "c1" for r in hits)
|
||||
|
||||
|
||||
@@ -388,7 +457,7 @@ async def test_parity_search_ranked_ids_vs_reference_through_dispatch():
|
||||
await dispatch_memory_call(up, wctx, mine)
|
||||
search_env = {
|
||||
"operation": "search",
|
||||
"args": {"vector": [1.0, 0.0], "top_k": 2, "scope_filter": {"end_user": "u1"}},
|
||||
"args": {"vector": [1.0, 0.0], "top_k": 2, "scope_all": {"end_user": "u1"}},
|
||||
}
|
||||
rstatus, rbody = await dispatch_memory_call(search_env, rctx, ref)
|
||||
mstatus, mbody = await dispatch_memory_call(search_env, rctx, mine)
|
||||
@@ -415,7 +484,9 @@ async def test_parity_expected_revisions_vs_reference_through_dispatch():
|
||||
"args": {"records": [_ref_record("a", [1.0, 0.0]), _ref_record("b", [0.0, 1.0])]},
|
||||
"idempotency_key": "seed",
|
||||
}
|
||||
assert await dispatch_memory_call(seed, wctx, ref) == await dispatch_memory_call(seed, wctx, mine)
|
||||
ref_seed = await dispatch_memory_call(seed, wctx, ref)
|
||||
mine_seed = await dispatch_memory_call(seed, wctx, mine)
|
||||
assert ref_seed == mine_seed
|
||||
|
||||
# partial map: only "a" is locked (revision 1); "b" is omitted from expected_revisions
|
||||
partial = {
|
||||
|
||||
Reference in New Issue
Block a user