hyf

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

commit d7586074d9301c7ba716916bf4dd2525c0044e2e
parent 40f9e11eccf54fefa73f6f8c9f8431f30a05fb4c
Author: triesap <tyson@radroots.org>
Date:   Wed,  8 Apr 2026 21:25:20 +0000

stdio: bound hyf internal error contract

Diffstat:
Msrc/hyf_core/metadata.mojo | 12+++++++++++-
Msrc/hyf_stdio/errors.mojo | 10++++++++--
Msrc/hyf_stdio/server.mojo | 46+++++++++++++++++++++++++++++++++++++++++++++-
Mtests/test_hyf.mojo | 65++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Mtests/test_stdio_contract.mojo | 56+++++++++++++++++++++++++++++++++++++++++++++++++++++++-
5 files changed, 183 insertions(+), 6 deletions(-)

diff --git a/src/hyf_core/metadata.mojo b/src/hyf_core/metadata.mojo @@ -1,3 +1,4 @@ +from std.os import getenv from std.pathlib import Path, _dir_of_current_file @@ -28,6 +29,15 @@ def _package_surface_manifest_path() raises -> Path: return _dir_of_current_file() / ".." / ".." / "pixi.toml" +def _package_surface_manifest_text() raises -> String: + if ( + getenv("HYF_TEST_FAULT_CURRENT_PACKAGE_SURFACE", "") + == "invalid_unquoted_version" + ): + return '[workspace]\nname = "hyf"\nversion = 0.1.0\n' + return _package_surface_manifest_path().read_text() + + def _parse_quoted_assignment_value(value: String) raises -> String: var trimmed_value = value.strip() if ( @@ -47,7 +57,7 @@ def current_package_surface() raises -> HyfPackageSurface: var package_name = String("") var package_version = String("") - for raw_line in _package_surface_manifest_path().read_text().splitlines(): + for raw_line in _package_surface_manifest_text().splitlines(): var line = String(raw_line).strip() if line == "" or line.startswith("#"): continue diff --git a/src/hyf_stdio/errors.mojo b/src/hyf_stdio/errors.mojo @@ -13,6 +13,10 @@ struct WireError(Copyable, Movable): return value^ +def internal_error_message() -> String: + return "internal hyf daemon error; inspect local diagnostics" + + def invalid_request_error(message: String) -> WireError: return WireError(code="invalid_request", message=message) @@ -38,5 +42,7 @@ def capability_unavailable_error(capability: String) -> WireError: ) -def internal_error(message: String) -> WireError: - return WireError(code="internal_error", message=message) +def internal_error() -> WireError: + return WireError( + code="internal_error", message=internal_error_message() + ) diff --git a/src/hyf_stdio/server.mojo b/src/hyf_stdio/server.mojo @@ -1,5 +1,7 @@ from std.collections import Optional from std.io.io import _fdopen +from std.os import getenv +from std.pathlib import Path from std.sys import stdin from mojson import Value @@ -75,6 +77,42 @@ def _write_success(response: WireSuccessResponse) raises: print(encode_success(response)) +def _diagnostic_value(value: String) -> String: + return value.replace("\n", "\\n").replace("\r", "\\r") + + +def _internal_diagnostic_path() -> Path: + return Path(getenv("TMPDIR", "/tmp")) / "hyf-internal-error.log" + + +def _diagnostic_trace_id(trace_id: Optional[String]) -> String: + if trace_id: + return _diagnostic_value(String(trace_id.value())) + return "" + + +def _emit_internal_diagnostic( + request_id: String, + trace_id: Optional[String], + capability: String, + detail: String, +): + try: + _internal_diagnostic_path().write_text( + "hyf_internal_error request_id=\"" + + _diagnostic_value(request_id) + + "\" trace_id=\"" + + _diagnostic_trace_id(trace_id) + + "\" capability=\"" + + _diagnostic_value(capability) + + "\" detail=\"" + + _diagnostic_value(detail) + + "\"\n" + ) + except: + pass + + def _wire_error_from_core_failure( request_id: String, trace_id: Optional[String], @@ -171,12 +209,18 @@ def handle_request(request: WireRequest) raises -> String: return encode_error(_unavailable_response(request)) return encode_error(_unsupported_response(request)) except e: + _emit_internal_diagnostic( + request_id, + trace_id, + String(request.capability), + String(e), + ) return encode_error( WireErrorResponse( version=hyf_protocol_version(), request_id=request_id, trace_id=trace_id, - error=internal_error(String(e)), + error=internal_error(), ) ) diff --git a/tests/test_hyf.mojo b/tests/test_hyf.mojo @@ -1,4 +1,10 @@ -from std.testing import assert_equal, assert_true, assert_raises, TestSuite +from std.os import getenv, setenv, unsetenv +from std.testing import ( + TestSuite, + assert_equal, + assert_raises, + assert_true, +) from mojson import Value, loads @@ -14,6 +20,15 @@ from hyf_stdio.errors import WireError from hyf_stdio.server import handle_request_line +comptime _EXPECTED_INTERNAL_ERROR_MESSAGE = ( + "internal hyf daemon error; inspect local diagnostics" +) + +comptime _PACKAGE_SURFACE_FAULT_ENV = ( + "HYF_TEST_FAULT_CURRENT_PACKAGE_SURFACE" +) + + def _dispatch(line: String) raises -> Value: return loads(handle_request_line(line)) @@ -620,5 +635,53 @@ def test_invalid_request_preserves_request_and_trace_correlation() raises: ) +def test_internal_error_is_bounded_on_wire() raises: + var original_fault = getenv(_PACKAGE_SURFACE_FAULT_ENV, "") + _ = setenv( + _PACKAGE_SURFACE_FAULT_ENV, "invalid_unquoted_version" + ) + try: + var result = _dispatch( + '{"version":1,"request_id":"status-internal-1","trace_id":"trace-status-internal-1","capability":"sys.status","input":{}}' + ) + + assert_equal(Int(result["version"].int_value()), 1) + assert_equal( + result["request_id"].string_value(), "status-internal-1" + ) + assert_equal( + result["trace_id"].string_value(), "trace-status-internal-1" + ) + assert_equal(result["ok"].bool_value(), False) + assert_equal( + result["error"]["code"].string_value(), "internal_error" + ) + assert_equal( + result["error"]["message"].string_value(), + _EXPECTED_INTERNAL_ERROR_MESSAGE, + ) + assert_true( + result["error"]["message"].string_value().find("quoted string") + < 0 + ) + assert_true( + result["error"]["message"].string_value().find( + "unable to derive hyf package surface" + ) + < 0 + ) + except e: + if original_fault == "": + _ = unsetenv(_PACKAGE_SURFACE_FAULT_ENV) + else: + _ = setenv(_PACKAGE_SURFACE_FAULT_ENV, original_fault) + raise e^ + + if original_fault == "": + _ = unsetenv(_PACKAGE_SURFACE_FAULT_ENV) + else: + _ = setenv(_PACKAGE_SURFACE_FAULT_ENV, original_fault) + + def main() raises: TestSuite.discover_tests[__functions_in_module()]().run() diff --git a/tests/test_stdio_contract.mojo b/tests/test_stdio_contract.mojo @@ -1,9 +1,18 @@ +from std.os import getenv, setenv, unsetenv from std.subprocess import run from std.testing import assert_equal, assert_true, TestSuite from mojson import Value, loads +comptime _EXPECTED_INTERNAL_ERROR_MESSAGE = ( + "internal hyf daemon error; inspect local diagnostics" +) +comptime _PACKAGE_SURFACE_FAULT_ENV = ( + "HYF_TEST_FAULT_CURRENT_PACKAGE_SURFACE" +) + + def _run_hyf(request_json: String) raises -> Value: var command = ( "printf '%s\\n' '" @@ -15,7 +24,6 @@ def _run_hyf(request_json: String) raises -> Value: raise Error("hyf process returned no stdout payload") return loads(output) - def _has_key(value: Value, key: String) -> Bool: for candidate in value.object_keys(): if candidate == key: @@ -113,5 +121,51 @@ def test_strict_semantic_rank_failure() raises: ) +def test_internal_error_is_bounded_on_wire() raises: + var original_fault = getenv(_PACKAGE_SURFACE_FAULT_ENV, "") + _ = setenv( + _PACKAGE_SURFACE_FAULT_ENV, "invalid_unquoted_version" + ) + try: + var response = _run_hyf( + '{"version":1,"request_id":"status-internal-proc-1","trace_id":"trace-status-internal-proc-1","capability":"sys.status","input":{}}' + ) + + assert_equal(Int(response["version"].int_value()), 1) + assert_equal( + response["request_id"].string_value(), + "status-internal-proc-1", + ) + assert_equal( + response["trace_id"].string_value(), + "trace-status-internal-proc-1", + ) + assert_true(not response["ok"].bool_value()) + assert_equal( + response["error"]["code"].string_value(), "internal_error" + ) + assert_equal( + response["error"]["message"].string_value(), + _EXPECTED_INTERNAL_ERROR_MESSAGE, + ) + assert_true( + response["error"]["message"].string_value().find( + "quoted string" + ) + < 0 + ) + except e: + if original_fault == "": + _ = unsetenv(_PACKAGE_SURFACE_FAULT_ENV) + else: + _ = setenv(_PACKAGE_SURFACE_FAULT_ENV, original_fault) + raise e^ + + if original_fault == "": + _ = unsetenv(_PACKAGE_SURFACE_FAULT_ENV) + else: + _ = setenv(_PACKAGE_SURFACE_FAULT_ENV, original_fault) + + def main() raises: TestSuite.discover_tests[__functions_in_module()]().run()