hyf

Context-aware query service for Radroots
git clone https://radroots.dev/git/hyf.git
Log | Files | Refs | README | LICENSE

commit 49410dd3d553a808259a17392a6351a9066ea1c4
parent 71ea28a4020d6c17bba5e617e9bda0bd304896c7
Author: triesap <tyson@radroots.org>
Date:   Wed,  8 Apr 2026 20:45:08 +0000

core: narrow hyf v1 context contract

Diffstat:
Msrc/hyf_core/request_context.mojo | 73+++++--------------------------------------------------------------------
Mtests/test_hyf.mojo | 31+++++++++++++++++++++++++++++++
2 files changed, 36 insertions(+), 68 deletions(-)

diff --git a/src/hyf_core/request_context.mojo b/src/hyf_core/request_context.mojo @@ -1,7 +1,7 @@ from std.collections import List, Optional from mojson import Value -from mojson.deserialize import get_bool, get_int, get_string +from mojson.deserialize import get_bool, get_string def _has_key(value: Value, key: String) -> Bool: @@ -81,13 +81,8 @@ def request_context_feature_names() -> List[String]: var features = List[String]() features.append("consumer") features.append("execution_mode_preference") - features.append("deadline_ms") features.append("scope") - features.append("time_range") - features.append("evidence_limit") - features.append("consistency") features.append("return_provenance") - features.append("explain_plan") return features^ @@ -114,48 +109,20 @@ def _parse_scope(json: Value) raises -> RequestScope: var allowed_keys = List[String]() allowed_keys.append("listing_ids") - allowed_keys.append("farm_ids") - allowed_keys.append("account_ids") - allowed_keys.append("platform_ids") - allowed_keys.append("object_filters") _require_allowed_keys(json, allowed_keys, "request context scope") - var object_filters: Optional[Value] = None - if _has_key(json, "object_filters"): - var raw_filters = json["object_filters"].clone() - if not raw_filters.is_null(): - object_filters = raw_filters^ - var listing_ids_json = Value(None) if _has_key(json, "listing_ids"): listing_ids_json = json["listing_ids"].clone() - var farm_ids_json = Value(None) - if _has_key(json, "farm_ids"): - farm_ids_json = json["farm_ids"].clone() - - var account_ids_json = Value(None) - if _has_key(json, "account_ids"): - account_ids_json = json["account_ids"].clone() - - var platform_ids_json = Value(None) - if _has_key(json, "platform_ids"): - platform_ids_json = json["platform_ids"].clone() - return RequestScope( listing_ids=_parse_string_list( listing_ids_json, "request context scope listing_ids" ), - farm_ids=_parse_string_list( - farm_ids_json, "request context scope farm_ids" - ), - account_ids=_parse_string_list( - account_ids_json, "request context scope account_ids" - ), - platform_ids=_parse_string_list( - platform_ids_json, "request context scope platform_ids" - ), - object_filters=object_filters^, + farm_ids=List[String](), + account_ids=List[String](), + platform_ids=List[String](), + object_filters=None, ) @@ -203,42 +170,12 @@ def parse_request_context(json: Value) raises -> RequestContext: "request context execution_mode_preference must be 'deterministic' or 'assisted'" ) - if _has_key(json, "deadline_ms"): - context.deadline_ms = get_int(json, "deadline_ms") - if context.deadline_ms <= 0: - raise Error("request context deadline_ms must be greater than zero") - if _has_key(json, "scope"): var scope_json = json["scope"].clone() if not scope_json.is_null(): context.scope = _parse_scope(scope_json) - if _has_key(json, "time_range"): - var time_range_json = json["time_range"].clone() - if not time_range_json.is_null(): - context.time_range = _parse_time_range(time_range_json) - - if _has_key(json, "evidence_limit"): - context.evidence_limit = get_int(json, "evidence_limit") - if context.evidence_limit <= 0: - raise Error( - "request context evidence_limit must be greater than zero" - ) - - if _has_key(json, "consistency"): - context.consistency = get_string(json, "consistency") - if ( - context.consistency != "default" - and context.consistency != "strong" - ): - raise Error( - "request context consistency must be 'default' or 'strong'" - ) - if _has_key(json, "return_provenance"): context.return_provenance = get_bool(json, "return_provenance") - if _has_key(json, "explain_plan"): - context.explain_plan = get_bool(json, "explain_plan") - return context^ diff --git a/tests/test_hyf.mojo b/tests/test_hyf.mojo @@ -55,6 +55,20 @@ def test_decode_request_rejects_unexpected_field() raises: ) +def test_decode_request_rejects_unsupported_context_field() raises: + with assert_raises(): + _ = decode_request( + '{"version":1,"request_id":"req-ctx-1","capability":"query_rewrite","context":{"deadline_ms":2500},"input":{"query":"eggs"}}' + ) + + +def test_decode_request_rejects_unsupported_scope_field() raises: + with assert_raises(): + _ = decode_request( + '{"version":1,"request_id":"req-scope-1","capability":"semantic_rank","context":{"scope":{"farm_ids":["farm-1"]}},"input":{"query":"eggs","candidates":[{"id":"lst_1","title":"Eggs","farm":"One Farm","delivery":"pickup","distance_km":1.0,"freshness_minutes":5}]}}' + ) + + def test_encode_success_and_error_shapes() raises: var output = loads("{}") output.set("kind", Value("ok")) @@ -348,6 +362,23 @@ def test_semantic_rank_returns_ranked_ids_and_reasons() raises: ) +def test_semantic_rank_scope_listing_ids_remains_effective() raises: + var result = _dispatch( + '{"version":1,"request_id":"rank-scope-1","capability":"semantic_rank","context":{"scope":{"listing_ids":["lst_8k1p"]}},"input":{"query":"eggs","candidates":[{"id":"lst_7ak2","title":"Pasture eggs","farm":"La Huerta del Sur","delivery":"pickup","distance_km":3.2,"freshness_minutes":2},{"id":"lst_8k1p","title":"Free range eggs","farm":"Santa Elena","delivery":"delivery","distance_km":8.7,"freshness_minutes":18}]}}' + ) + + assert_equal(Int(result["version"].int_value()), 1) + assert_equal(result["ok"].bool_value(), True) + assert_equal( + result["output"]["ranked_ids"][0].string_value(), + "lst_8k1p", + ) + assert_equal( + result["output"]["scored_candidates"][0]["scope_match"].bool_value(), + True, + ) + + def test_semantic_rank_rejects_unknown_top_level_field() raises: var result = _dispatch( '{"version":1,"request_id":"rank-bad-top-1","capability":"semantic_rank","input":{"query":"eggs near me","candidates":[{"id":"lst_7ak2","title":"Pasture eggs","farm":"La Huerta del Sur","delivery":"pickup","distance_km":3.2,"freshness_minutes":2}],"tone":"brief"}}'