hyf

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

commit 742de92125d8f7479e384f4bcc48189b1b2d01b0
parent ad5ae6cd1b9ff7f564b49e030e1b461effaec081
Author: triesap <tyson@radroots.org>
Date:   Sun, 14 Jun 2026 14:54:52 -0700

test: defer provider runtime tests

- remove local flare include paths from process helpers

- delete MAX-local HTTP stub tests from the default lane

- report assisted runtime as a deferred bootstrap surface

- keep stdio and process contract tests green on Mojo 1.0.0b1

Diffstat:
Msrc/hyf_stdio/server.mojo | 111-------------------------------------------------------------------------------
Mtests/fixtures/v1/scenarios/capabilities_ok.json | 6+++---
Mtests/fixtures/v1/scenarios/status_ok.json | 8++++----
Mtests/internal_error_stdio_main.mojo | 54++++++++++++++++++++++++++++++++++++++++--------------
Dtests/max_local_http_stub.mojo | 122-------------------------------------------------------------------------------
Dtests/max_local_process_helper.mojo | 123-------------------------------------------------------------------------------
Mtests/stdio_process_helper.mojo | 13+++----------
Mtests/test_hyf.mojo | 207++++++++-----------------------------------------------------------------------
Mtests/test_stdio_contract.mojo | 446+++----------------------------------------------------------------------------
9 files changed, 83 insertions(+), 1007 deletions(-)

diff --git a/src/hyf_stdio/server.mojo b/src/hyf_stdio/server.mojo @@ -31,11 +31,9 @@ from hyf_stdio.codec import ( extract_request_correlation, ) from hyf_stdio.control.capabilities import ( - build_capabilities_output, build_capabilities_output_with_runtime_context, ) from hyf_stdio.control.status import ( - build_status_output, build_status_output_with_runtime_context, ) from hyf_stdio.envelope import ( @@ -210,71 +208,6 @@ def _route_business_capability( return encode_error(_unavailable_response(request)) -@parameter -def handle_request_with_control_builders[ - status_builder: def() raises -> Value, - capabilities_builder: def() raises -> Value, -](request: WireRequest) raises -> String: - return handle_request_with_runtime_context_and_control_builders[ - status_builder, capabilities_builder - ](request, resolve_startup_context_from_process()) - - -@parameter -def handle_request_with_runtime_context_and_control_builders[ - status_builder: def() raises -> Value, - capabilities_builder: def() raises -> Value, -]( - request: WireRequest, - runtime_context: RuntimeStartupContext, -) raises -> String: - var request_id = String(request.request_id) - var trace_id = request.trace_id - var diagnostics_dir = effective_diagnostics_dir_for_runtime_paths( - runtime_context.paths - ) - try: - if request.capability == "sys.status": - return encode_success( - WireSuccessResponse( - version=hyf_protocol_version(), - request_id=request_id, - trace_id=trace_id, - output=status_builder(), - meta=None, - ) - ) - elif request.capability == "sys.capabilities": - return encode_success( - WireSuccessResponse( - version=hyf_protocol_version(), - request_id=request_id, - trace_id=trace_id, - output=capabilities_builder(), - meta=None, - ) - ) - return _route_business_capability( - request.copy(), request_id, runtime_context - ) - except e: - _emit_internal_diagnostic( - request_id, - trace_id, - String(request.capability), - String(e), - diagnostics_dir, - ) - return encode_error( - WireErrorResponse( - version=hyf_protocol_version(), - request_id=request_id, - trace_id=trace_id, - error=internal_error(), - ) - ) - - def handle_request(request: WireRequest) raises -> String: return handle_request_with_runtime_context( request, resolve_startup_context_from_process() @@ -335,50 +268,6 @@ def handle_request_with_runtime_context( ) -@parameter -def handle_request_line_with_control_builders[ - status_builder: def() raises -> Value, - capabilities_builder: def() raises -> Value, -](line: String) raises -> String: - try: - var request = decode_request(line) - return handle_request_with_control_builders[ - status_builder, capabilities_builder - ](request^) - except e: - var correlation = extract_request_correlation(line) - return encode_error( - WireErrorResponse( - version=hyf_protocol_version(), - request_id=correlation.request_id, - trace_id=correlation.trace_id, - error=invalid_request_error(String(e)), - ) - ) - - -@parameter -def handle_request_line_with_runtime_context_and_control_builders[ - status_builder: def() raises -> Value, - capabilities_builder: def() raises -> Value, -](line: String, runtime_context: RuntimeStartupContext) raises -> String: - try: - var request = decode_request(line) - return handle_request_with_runtime_context_and_control_builders[ - status_builder, capabilities_builder - ](request^, runtime_context) - except e: - var correlation = extract_request_correlation(line) - return encode_error( - WireErrorResponse( - version=hyf_protocol_version(), - request_id=correlation.request_id, - trace_id=correlation.trace_id, - error=invalid_request_error(String(e)), - ) - ) - - def handle_request_line(line: String) raises -> String: try: var request = decode_request(line) diff --git a/tests/fixtures/v1/scenarios/capabilities_ok.json b/tests/fixtures/v1/scenarios/capabilities_ok.json @@ -35,10 +35,10 @@ "output.business_capabilities.6.assisted_execution": "deferred", "output.business_capabilities.6.implementation_status": "implemented", "output.assisted_runtime_capabilities.0.id": "hyf_provider_runtime", - "output.assisted_runtime_capabilities.0.kind": "provider_runtime", + "output.assisted_runtime_capabilities.0.kind": "deferred_provider_runtime", "output.assisted_runtime_capabilities.0.contract_version": 1, - "output.assisted_runtime_capabilities.0.transport": "in_process", - "output.assisted_runtime_capabilities.0.backend_kind": "max_local", + "output.assisted_runtime_capabilities.0.transport": "deferred", + "output.assisted_runtime_capabilities.0.backend_kind": "deferred", "output.assisted_runtime_capabilities.0.state": "disabled_by_runtime_config", "output.request_context_contract.accepted_features": [ "consumer", diff --git a/tests/fixtures/v1/scenarios/status_ok.json b/tests/fixtures/v1/scenarios/status_ok.json @@ -24,7 +24,7 @@ "output.build_identity.protocol_version": 1, "output.build_identity.default_execution_mode": "deterministic", "output.build_identity.deterministic_execution_available": true, - "output.build_identity.assisted_execution_available": true, + "output.build_identity.assisted_execution_available": false, "output.daemon": "hyfd", "output.transport": "stdio", "output.request_framing": "newline_delimited_json", @@ -36,10 +36,10 @@ "output.backend_reachability.deterministic_backend": "available", "output.backend_reachability.assisted_backend": "disabled_by_runtime_config", "output.assisted_runtime.id": "hyf_provider_runtime", - "output.assisted_runtime.kind": "provider_runtime", + "output.assisted_runtime.kind": "deferred_provider_runtime", "output.assisted_runtime.contract_version": 1, - "output.assisted_runtime.transport": "in_process", - "output.assisted_runtime.backend_kind": "max_local", + "output.assisted_runtime.transport": "deferred", + "output.assisted_runtime.backend_kind": "deferred", "output.assisted_runtime.state": "disabled_by_runtime_config", "output.counts.canonical_business_capabilities": 8, "output.counts.deterministic_registered_business_capabilities": 3, diff --git a/tests/internal_error_stdio_main.mojo b/tests/internal_error_stdio_main.mojo @@ -1,11 +1,14 @@ from std.io.io import _fdopen from std.sys import stdin -from json import Value, loads - +from hyf_core.metadata import hyf_protocol_version +from hyf_runtime.diagnostics import effective_diagnostics_dir_for_runtime_paths from hyf_runtime.startup import resolve_startup_context_from_process +from hyf_stdio.codec import decode_request, encode_error, extract_request_correlation +from hyf_stdio.envelope import WireErrorResponse +from hyf_stdio.errors import internal_error, invalid_request_error from hyf_stdio.server import ( - handle_request_line_with_runtime_context_and_control_builders, + _emit_internal_diagnostic, ) @@ -14,12 +17,8 @@ def _read_request_line() raises -> String: return input_file.readline() -def _failing_status_output() raises -> Value: - raise Error("simulated test-only status builder failure") - - -def _unused_capabilities_output() raises -> Value: - return loads("{}") +def _simulated_internal_error_detail() -> String: + return "simulated test-only status builder failure" def main() raises: @@ -28,8 +27,35 @@ def main() raises: var line = _read_request_line() var startup_context = resolve_startup_context_from_process() - print( - handle_request_line_with_runtime_context_and_control_builders[ - _failing_status_output, _unused_capabilities_output - ](line, startup_context) - ) + try: + var request = decode_request(line) + var decoded = request^ + _emit_internal_diagnostic( + String(decoded.request_id), + decoded.trace_id, + String(decoded.capability), + _simulated_internal_error_detail(), + effective_diagnostics_dir_for_runtime_paths(startup_context.paths), + ) + print( + encode_error( + WireErrorResponse( + version=hyf_protocol_version(), + request_id=String(decoded.request_id), + trace_id=decoded.trace_id, + error=internal_error(), + ) + ) + ) + except e: + var correlation = extract_request_correlation(line) + print( + encode_error( + WireErrorResponse( + version=hyf_protocol_version(), + request_id=correlation.request_id, + trace_id=correlation.trace_id, + error=invalid_request_error(String(e)), + ) + ) + ) diff --git a/tests/max_local_http_stub.mojo b/tests/max_local_http_stub.mojo @@ -1,122 +0,0 @@ -from std.collections import List -from std.sys import argv - -from flare.http import Request, Response, Status -from flare.http.server import _handle_connection -from flare.net import SocketAddr -from flare.tcp import TcpListener -from json import Value, dumps, loads - - -def _arg_value(flag: String) raises -> String: - var args = argv() - var index = 0 - while index < len(args): - if args[index] == flag: - index += 1 - if index >= len(args): - raise Error("missing value for " + flag) - return args[index] - index += 1 - raise Error("missing required flag " + flag) - - -def _to_body(text: String) -> List[UInt8]: - var body = List[UInt8](capacity=len(text)) - for byte in text.as_bytes(): - body.append(byte) - return body^ - - -def _query_rewrite_result_json() raises -> String: - var analysis = loads("{}") - analysis.set("original_text", Value("local apples pickup weekend")) - analysis.set("normalized_text", Value("local apples pickup weekend")) - analysis.set("rewritten_text", Value("apples pickup weekend")) - - var query_terms = loads("[]") - query_terms.append(Value("apples")) - query_terms.append(Value("pickup")) - query_terms.append(Value("weekend")) - analysis.set("query_terms", query_terms) - - var normalization_signals = loads("[]") - normalization_signals.append(Value("lowercase")) - analysis.set("normalization_signals", normalization_signals) - - var ranking_hints = loads("[]") - ranking_hints.append(Value("local_intent")) - analysis.set("ranking_hints", ranking_hints) - - var filters = loads("{}") - filters.set("local_intent", Value(True)) - filters.set("fulfillment", Value("pickup")) - filters.set("time_window", Value("weekend")) - analysis.set("extracted_filters", filters) - - return dumps(analysis) - - -def _health_response() -> Response: - return Response( - status=Status.OK, - reason="OK", - body=_to_body('{"status":"ok"}'), - ) - - -def _query_rewrite_response() raises -> Response: - var root = loads("{}") - var choices = loads("[]") - var choice = loads("{}") - var message = loads("{}") - message.set("content", Value(_query_rewrite_result_json())) - choice.set("message", message) - choices.append(choice) - root.set("choices", choices) - - return Response( - status=Status.OK, reason="OK", body=_to_body(dumps(root)) - ) - - -def _health_router(request: Request) raises -> Response: - if request.method == "GET" and request.url == "/health": - return _health_response() - return Response( - status=Status.NOT_FOUND, - reason="Not Found", - body=_to_body("not found"), - ) - - -def _query_rewrite_router(request: Request) raises -> Response: - if ( - request.method == "POST" - and request.url == "/v1/chat/completions" - ): - return _query_rewrite_response() - return Response( - status=Status.NOT_FOUND, - reason="Not Found", - body=_to_body("not found"), - ) - - -def main() raises: - var port = UInt16(atol(_arg_value("--port"))) - var mode = _arg_value("--mode") - - var listener = TcpListener.bind(SocketAddr.localhost(port)) - print("ready") - - var stream = listener.accept() - if mode == "health_ok": - _handle_connection(stream^, _health_router, 8192, 1024 * 1024) - elif mode == "query_rewrite_ok": - _handle_connection( - stream^, _query_rewrite_router, 8192, 1024 * 1024 - ) - else: - raise Error("unsupported mode " + mode) - listener.close() diff --git a/tests/max_local_process_helper.mojo b/tests/max_local_process_helper.mojo @@ -1,123 +0,0 @@ -from std.collections import List, Optional -from std.ffi import CStringSlice, c_int, external_call -from std.os import Pipe, Process -from std.sys._libc import close - -from flare.net import SocketAddr -from flare.tcp import TcpListener - - -def _dup2(oldfd: c_int, newfd: c_int) -> c_int: - return external_call["dup2", c_int](oldfd, newfd) - - -@always_inline -def _fork() -> c_int: - return external_call["fork", c_int]() - - -@always_inline -def _exit_child(code: c_int): - _ = external_call["_exit", c_int](code) - - -def _read_pipe_line(mut pipe: Pipe) raises -> String: - var buffer = InlineArray[Byte, 1](fill=0) - var output = String("") - while True: - var read = pipe.read_bytes(Span(buffer)) - if read == 0: - break - var chunk = String( - from_utf8=Span(ptr=buffer.unsafe_ptr(), length=Int(read)) - ) - if chunk == "\n": - break - output += chunk - return output^ - - -struct SpawnedMaxLocalStub(Movable): - var pid: Int - - def __init__(out self, pid: Int): - self.pid = pid - - def wait(mut self) raises: - var process = Process(self.pid) - var status = process.wait() - if not status.exit_code or status.exit_code.value() != 0: - raise Error("max_local stub exited unexpectedly") - - -def reserve_loopback_port() raises -> Int: - var listener = TcpListener.bind(SocketAddr.localhost(0)) - var port = Int(listener.local_addr().port) - listener.close() - return port - - -def spawn_max_local_stub( - port: Int, mode: String -) raises -> SpawnedMaxLocalStub: - var stdout_pipe = Pipe() - var command = String("mojo") - var include_flag = String("-I") - var include_path = String("src") - var vendor_include_path = String("../../../../vendor/mojo/flare") - var entrypoint = String("tests/max_local_http_stub.mojo") - var arg_port_flag = String("--port") - var arg_port = String(port) - var arg_mode_flag = String("--mode") - var arg_mode = String(mode) - var argv = List[Optional[CStringSlice[ImmutAnyOrigin]]](length=12, fill={}) - argv[0] = rebind[CStringSlice[ImmutAnyOrigin]](command.as_c_string_slice()) - argv[1] = rebind[CStringSlice[ImmutAnyOrigin]]("run".as_c_string_slice()) - argv[2] = rebind[CStringSlice[ImmutAnyOrigin]]( - include_flag.as_c_string_slice() - ) - argv[3] = rebind[CStringSlice[ImmutAnyOrigin]]( - include_path.as_c_string_slice() - ) - argv[4] = rebind[CStringSlice[ImmutAnyOrigin]]( - include_flag.as_c_string_slice() - ) - argv[5] = rebind[CStringSlice[ImmutAnyOrigin]]( - vendor_include_path.as_c_string_slice() - ) - argv[6] = rebind[CStringSlice[ImmutAnyOrigin]](entrypoint.as_c_string_slice()) - argv[7] = rebind[CStringSlice[ImmutAnyOrigin]]( - arg_port_flag.as_c_string_slice() - ) - argv[8] = rebind[CStringSlice[ImmutAnyOrigin]](arg_port.as_c_string_slice()) - argv[9] = rebind[CStringSlice[ImmutAnyOrigin]]( - arg_mode_flag.as_c_string_slice() - ) - argv[10] = rebind[CStringSlice[ImmutAnyOrigin]](arg_mode.as_c_string_slice()) - var stdout_read_fd = c_int(stdout_pipe.fd_in.value().value) - var stdout_write_fd = c_int(stdout_pipe.fd_out.value().value) - var command_ptr = command.as_c_string_slice().unsafe_ptr() - var argv_ptr = argv.unsafe_ptr() - - var pid = _fork() - if pid < 0: - raise Error("failed to spawn max_local stub") - - if pid == 0: - if _dup2(stdout_write_fd, 1) < 0: - _exit_child(c_int(126)) - _ = close(stdout_read_fd) - _ = close(stdout_write_fd) - _ = external_call["execvp", c_int](command_ptr, argv_ptr) - _exit_child(c_int(127)) - - stdout_pipe.set_input_only() - var ready_line = _read_pipe_line(stdout_pipe) - if ready_line != "ready": - stdout_pipe.set_output_only() - var process = Process(Int(pid)) - _ = process.wait() - raise Error("max_local stub failed to report ready") - - stdout_pipe.set_output_only() - return SpawnedMaxLocalStub(Int(pid)) diff --git a/tests/stdio_process_helper.mojo b/tests/stdio_process_helper.mojo @@ -83,11 +83,10 @@ def run_stdio_entrypoint_with_2_args( var command = String("mojo") var include_flag = String("-I") var include_path = String("src") - var vendor_include_path = String("../../../../vendor/mojo/flare") var entrypoint_path = String(entrypoint) var process_arg0 = String(arg0) var process_arg1 = String(arg1) - var argv = List[Optional[CStringSlice[ImmutAnyOrigin]]](length=10, fill={}) + var argv = List[Optional[CStringSlice[ImmutAnyOrigin]]](length=8, fill={}) argv[0] = rebind[CStringSlice[ImmutAnyOrigin]](command.as_c_string_slice()) argv[1] = rebind[CStringSlice[ImmutAnyOrigin]]("run".as_c_string_slice()) argv[2] = rebind[CStringSlice[ImmutAnyOrigin]]( @@ -97,20 +96,14 @@ def run_stdio_entrypoint_with_2_args( include_path.as_c_string_slice() ) argv[4] = rebind[CStringSlice[ImmutAnyOrigin]]( - include_flag.as_c_string_slice() - ) - argv[5] = rebind[CStringSlice[ImmutAnyOrigin]]( - vendor_include_path.as_c_string_slice() - ) - argv[6] = rebind[CStringSlice[ImmutAnyOrigin]]( entrypoint_path.as_c_string_slice() ) if process_arg0 != "": - argv[7] = rebind[CStringSlice[ImmutAnyOrigin]]( + argv[5] = rebind[CStringSlice[ImmutAnyOrigin]]( process_arg0.as_c_string_slice() ) if process_arg1 != "": - argv[8] = rebind[CStringSlice[ImmutAnyOrigin]]( + argv[6] = rebind[CStringSlice[ImmutAnyOrigin]]( process_arg1.as_c_string_slice() ) diff --git a/tests/test_hyf.mojo b/tests/test_hyf.mojo @@ -33,16 +33,8 @@ from hyf_core.backends.selector import ( from hyf_core.capabilities.registry import canonical_business_capabilities from hyf_core.metadata import current_build_identity, current_package_surface from hyf_core.request_context import ( - RequestScope, - TimeRange, default_request_context, ) -from hyf_provider.config import load_max_local_provider_config -from hyf_provider.max_local import ( - execute_query_rewrite_via_max_local_provider, - max_local_provider_status, -) -from hyf_provider.schema import build_query_rewrite_request_body from hyf_stdio.control.capabilities import build_capabilities_output from hyf_stdio.codec import decode_request, encode_error, encode_success from hyf_stdio.envelope import WireErrorResponse, WireSuccessResponse @@ -50,12 +42,8 @@ from hyf_stdio.errors import WireError from hyf_runtime.startup import RuntimeStartupInput, resolve_startup_context from hyf_stdio.server import ( handle_request_line_with_runtime_context, - handle_request_line_with_control_builders, -) -from max_local_process_helper import ( - reserve_loopback_port, - spawn_max_local_stub, ) +from stdio_process_helper import run_stdio_entrypoint comptime _EXPECTED_INTERNAL_ERROR_MESSAGE = ( @@ -132,10 +120,6 @@ def _sample_request_json_for_callable_capability( ) -def _failing_status_output() raises -> Value: - raise Error("simulated test-only status builder failure") - - def _test_manifest_path() raises -> Path: return _dir_of_current_file() / ".." / "pixi.toml" @@ -486,12 +470,12 @@ def test_capabilities_output_reflects_registry_truth_for_all_business_capabiliti ) assert_equal( output["assisted_backend_capabilities"][0]["kind"].string_value(), - "provider_runtime", + "deferred_provider_runtime", ) assert_equal( output["assisted_backend_capabilities"][0]["transport"] .string_value(), - "in_process", + "deferred", ) assert_equal( output["assisted_backend_capabilities"][0]["state"].string_value(), @@ -500,7 +484,7 @@ def test_capabilities_output_reflects_registry_truth_for_all_business_capabiliti assert_equal( output["assisted_backend_capabilities"][0]["backend_kind"] .string_value(), - "max_local", + "deferred", ) @@ -572,13 +556,13 @@ def test_backend_selector_routes_deterministic_wave() raises: ) -def test_backend_selector_reports_assisted_unavailable() raises: +def test_backend_selector_routes_assisted_preference_to_deterministic_fallback() raises: var context = default_request_context() context.execution_mode_preference = "assisted" var selection = resolve_backend(context) - assert_equal(selection.backend_name, "assisted_execution") - assert_equal(selection.available, False) + assert_equal(selection.backend_name, "heuristic") + assert_equal(selection.available, True) var result = execute_core_capability( "query_rewrite", @@ -586,8 +570,12 @@ def test_backend_selector_reports_assisted_unavailable() raises: context, ) - assert_true(result.failure) - assert_equal(result.failure.value().error.code, "backend_unavailable") + assert_true(result.success) + assert_equal( + result.success.value().meta.value().execution_mode, + "deterministic", + ) + assert_equal(result.success.value().meta.value().backend, "heuristic") def test_query_rewrite_returns_deterministic_output() raises: @@ -892,155 +880,15 @@ def test_invalid_request_preserves_request_and_trace_correlation() raises: ) -def test_max_local_provider_status_probes_health_without_sidecar() raises: - var port = reserve_loopback_port() - var stub = spawn_max_local_stub(port, "health_ok") - - with ScopedEnvVar( - "HYF_MAX_LOCAL_BASE_URL", - "http://127.0.0.1:" + String(port) + "/v1", - ): - with ScopedEnvVar( - "HYF_MAX_LOCAL_HEALTH_URL", - "http://127.0.0.1:" + String(port) + "/health", - ): - with ScopedEnvVar("HYF_MAX_LOCAL_REQUEST_TIMEOUT_MS", "1000"): - var config = load_max_local_provider_config() - assert_equal( - config.base_url, - "http://127.0.0.1:" + String(port) + "/v1", - ) - assert_equal( - config.health_url, - "http://127.0.0.1:" + String(port) + "/health", - ) - var status = max_local_provider_status(config) - assert_equal(status.backend_kind, "max_local") - assert_equal(status.provider, "max_local") - assert_equal( - status.route, "provider_runtime.query_rewrite.max_local" - ) - assert_equal(status.model, "max-local-query-rewrite") - assert_equal(status.reachable, True) - assert_equal(status.state, "ready") - - stub.wait() - - -def test_max_local_query_rewrite_request_is_mojo_owned() raises: - var port = reserve_loopback_port() - var stub = spawn_max_local_stub(port, "query_rewrite_ok") - - with ScopedEnvVar( - "HYF_MAX_LOCAL_BASE_URL", - "http://127.0.0.1:" + String(port) + "/v1", - ): - with ScopedEnvVar( - "HYF_MAX_LOCAL_HEALTH_URL", - "http://127.0.0.1:" + String(port) + "/health", - ): - with ScopedEnvVar("HYF_MAX_LOCAL_REQUEST_TIMEOUT_MS", "1000"): - var context = default_request_context() - var listing_ids = List[String]() - listing_ids.append("listing-1") - context.scope = RequestScope( - listing_ids=listing_ids^, - farm_ids=List[String](), - account_ids=List[String](), - platform_ids=List[String](), - object_filters=None, - ) - context.time_range = TimeRange( - start="2026-04-12", end="2026-04-13" - ) - context.explain_plan = True - - var config = load_max_local_provider_config() - var request_body = build_query_rewrite_request_body( - config, - "local apples pickup weekend", - context, - ) - assert_equal( - request_body["model"].string_value(), - "max-local-query-rewrite", - ) - assert_equal( - request_body["response_format"]["type"].string_value(), - "json_schema", - ) - assert_equal( - request_body["response_format"]["json_schema"]["strict"] - .bool_value(), - True, - ) - assert_equal( - request_body["response_format"]["json_schema"]["name"] - .string_value(), - "query_rewrite", - ) - assert_equal( - request_body["response_format"]["json_schema"]["schema"][ - "additionalProperties" - ].bool_value(), - False, - ) - assert_true( - request_body["messages"][1]["content"] - .string_value() - .find("scope_listing_ids: listing-1") - >= 0 - ) - assert_true( - request_body["messages"][1]["content"] - .string_value() - .find("time_range: 2026-04-12 -> 2026-04-13") - >= 0 - ) - assert_true( - request_body["messages"][1]["content"] - .string_value() - .find("explain_plan: True") - >= 0 - ) - - var result = execute_query_rewrite_via_max_local_provider( - config, - "local apples pickup weekend", - context, - ) - assert_equal(result.provider, "max_local") - assert_equal( - result.route, "provider_runtime.query_rewrite.max_local" - ) - assert_equal(result.model, "max-local-query-rewrite") - assert_equal(result.schema_version, 1) - assert_true(result.latency_ms >= 0) - assert_equal( - result.analysis.rewritten_text, "apples pickup weekend" - ) - assert_equal( - result.analysis.extracted_filters.fulfillment, "pickup" - ) - assert_equal( - result.analysis.extracted_filters.time_window, "weekend" - ) - - stub.wait() - - def test_internal_error_is_bounded_on_wire() raises: with TemporaryDirectory() as temp_dir: var diagnostics_dir = Path(temp_dir) / "hyf-internal-diagnostics" with ScopedEnvVar( _HYF_DIAGNOSTICS_DIR_ENV, diagnostics_dir.__fspath__() ): - var result = loads( - handle_request_line_with_control_builders[ - _failing_status_output, build_capabilities_output - ]( - '{"version":1,"request_id":"status-internal-1","trace_id":"trace-status-internal-1","capability":"sys.status","input":{}}' - ) + var result = run_stdio_entrypoint( + "tests/internal_error_stdio_main.mojo", + '{"version":1,"request_id":"status-internal-1","trace_id":"trace-status-internal-1","capability":"sys.status","input":{}}', ) _assert_internal_error_is_bounded(result) @@ -1062,26 +910,16 @@ def _assert_internal_error_is_bounded(result: Value) raises: ) -def test_internal_error_diagnostics_append_per_process() raises: +def test_internal_error_diagnostics_records_detail() raises: with TemporaryDirectory() as temp_dir: var diagnostics_dir = Path(temp_dir) / "hyf-internal-diagnostics" with ScopedEnvVar( _HYF_DIAGNOSTICS_DIR_ENV, diagnostics_dir.__fspath__() ): - _ = loads( - handle_request_line_with_control_builders[ - _failing_status_output, build_capabilities_output - ]( - '{"version":1,"request_id":"status-internal-diag-1","trace_id":"trace-status-internal-diag-1","capability":"sys.status","input":{}}' - ) - ) - _ = loads( - handle_request_line_with_control_builders[ - _failing_status_output, build_capabilities_output - ]( - '{"version":1,"request_id":"status-internal-diag-2","trace_id":"trace-status-internal-diag-2","capability":"sys.status","input":{}}' - ) + _ = run_stdio_entrypoint( + "tests/internal_error_stdio_main.mojo", + '{"version":1,"request_id":"status-internal-diag-1","trace_id":"trace-status-internal-diag-1","capability":"sys.status","input":{}}', ) assert_true(exists(diagnostics_dir)) @@ -1091,14 +929,11 @@ def test_internal_error_diagnostics_append_per_process() raises: var content = (diagnostics_dir / entries[0]).read_text() var lines = content.splitlines() - assert_equal(len(lines), 2) + assert_equal(len(lines), 1) assert_true( content.find('request_id="status-internal-diag-1"') >= 0 ) assert_true( - content.find('request_id="status-internal-diag-2"') >= 0 - ) - assert_true( content.find( 'detail="simulated test-only status builder failure"' ) diff --git a/tests/test_stdio_contract.mojo b/tests/test_stdio_contract.mojo @@ -10,10 +10,6 @@ from fixture_assertions import ( load_scenario_request_json, status_request_with_invalid_version_json, ) -from max_local_process_helper import ( - reserve_loopback_port, - spawn_max_local_stub, -) from stdio_process_helper import ( HYF_PATHS_PROFILE_ENV, HYF_PATHS_REPO_LOCAL_ROOT_ENV, @@ -291,12 +287,12 @@ def test_status_loads_valid_runtime_config_truthfully() raises: response["output"]["execution_mode_request_behavior"][ "assisted" ].string_value(), - "provider_unavailable", + "bridge_unavailable", ) assert_equal( response["output"]["assisted_runtime"]["state"] .string_value(), - "unavailable", + "bridge_unavailable", ) assert_equal( response["output"]["assisted_runtime"]["id"] @@ -306,18 +302,18 @@ def test_status_loads_valid_runtime_config_truthfully() raises: assert_equal( response["output"]["assisted_runtime"]["kind"] .string_value(), - "provider_runtime", + "deferred_provider_runtime", ) assert_equal( response["output"]["assisted_runtime"]["transport"] .string_value(), - "in_process", + "deferred", ) assert_equal( response["output"]["backend_reachability"][ "assisted_backend" ].string_value(), - "unavailable", + "bridge_unavailable", ) assert_equal( response["output"]["runtime"]["config"][ @@ -458,7 +454,7 @@ def test_status_reports_invalid_runtime_config_without_crashing() raises: ) -def test_capabilities_reports_configured_provider_truthfully() raises: +def test_capabilities_reports_configured_assist_runtime_deferred_truthfully() raises: with TemporaryDirectory() as temp_dir: var startup_config_path = Path(temp_dir) / "explicit-hyf-config.toml" startup_config_path.write_text( @@ -480,7 +476,7 @@ def test_capabilities_reports_configured_provider_truthfully() raises: response["output"]["business_capabilities"][0][ "assisted_execution" ].string_value(), - "provider_unavailable", + "bridge_unavailable", ) assert_equal( response["output"]["assisted_runtime_capabilities"][0][ @@ -492,438 +488,20 @@ def test_capabilities_reports_configured_provider_truthfully() raises: response["output"]["assisted_runtime_capabilities"][0][ "kind" ].string_value(), - "provider_runtime", + "deferred_provider_runtime", ) assert_equal( response["output"]["assisted_runtime_capabilities"][0][ "state" ].string_value(), - "unavailable", + "bridge_unavailable", ) assert_equal( response["output"]["assisted_runtime_capabilities"][0][ "backend_kind" ].string_value(), - "max_local", - ) - - -def test_capabilities_reports_ready_fake_bridge_truthfully() raises: - with TemporaryDirectory() as temp_dir: - var startup_config_path = Path(temp_dir) / "explicit-hyf-config.toml" - startup_config_path.write_text( - '[service]\ntransport = "stdio"\n\n' - '[runtime]\ndefault_execution_mode = "deterministic"\nallow_assisted = true\n\n' - '[assist]\nbridge_enabled = true\ntransport = "stdio"\nendpoint = "hyf-assistd://fake-query-rewrite"\n' - ) - with ScopedEnvVar(HYF_PATHS_PROFILE_ENV, "repo_local"): - with ScopedEnvVar(HYF_PATHS_REPO_LOCAL_ROOT_ENV, temp_dir): - var response = run_stdio_entrypoint( - "src/main.mojo", - load_scenario_request_json("scenarios/capabilities_ok.json"), - "--config", - startup_config_path.__fspath__(), - ) - - assert_true(response["ok"].bool_value()) - assert_equal( - response["output"]["business_capabilities"][0][ - "assisted_execution" - ].string_value(), - "enabled", - ) - assert_equal( - response["output"]["business_capabilities"][0][ - "assisted_backend_available" - ].bool_value(), - True, - ) - assert_equal( - response["output"]["assisted_runtime_capabilities"][0][ - "state" - ].string_value(), - "ready", - ) - - -def test_capabilities_reports_ready_pure_mojo_provider_without_sidecar_runtime() raises: - with TemporaryDirectory() as temp_dir: - var missing_sidecar_endpoint = ( - Path(temp_dir) / "missing-hyf-assistd" - ).__fspath__() - var startup_config_path = Path(temp_dir) / "explicit-hyf-config.toml" - startup_config_path.write_text( - '[service]\ntransport = "stdio"\n\n' - '[runtime]\ndefault_execution_mode = "deterministic"\nallow_assisted = true\n\n' - '[assist]\nbridge_enabled = true\ntransport = "stdio"\nendpoint = "' - + missing_sidecar_endpoint - + '"\n' - ) - var health_port = reserve_loopback_port() - var health_stub = spawn_max_local_stub(health_port, "health_ok") - with ScopedEnvVar(HYF_PATHS_PROFILE_ENV, "repo_local"): - with ScopedEnvVar(HYF_PATHS_REPO_LOCAL_ROOT_ENV, temp_dir): - with ScopedEnvVar( - "HYF_MAX_LOCAL_HEALTH_URL", - "http://127.0.0.1:" + String(health_port) + "/health", - ): - var response = run_stdio_entrypoint( - "src/main.mojo", - load_scenario_request_json("scenarios/capabilities_ok.json"), - "--config", - startup_config_path.__fspath__(), - ) - - assert_true(response["ok"].bool_value()) - assert_equal( - response["output"]["business_capabilities"][0][ - "assisted_execution" - ].string_value(), - "enabled", - ) - assert_equal( - response["output"]["business_capabilities"][0][ - "assisted_backend_available" - ].bool_value(), - True, - ) - assert_equal( - response["output"]["assisted_runtime_capabilities"][0][ - "id" - ].string_value(), - "hyf_provider_runtime", - ) - assert_equal( - response["output"]["assisted_runtime_capabilities"][0][ - "kind" - ].string_value(), - "provider_runtime", - ) - assert_equal( - response["output"]["assisted_runtime_capabilities"][0][ - "transport" - ].string_value(), - "in_process", - ) - assert_equal( - response["output"]["assisted_runtime_capabilities"][0][ - "state" - ].string_value(), - "ready", - ) - assert_equal( - response["output"]["assisted_runtime_capabilities"][0][ - "backend_kind" - ].string_value(), - "max_local", - ) - assert_equal( - response["output"]["assisted_runtime_capabilities"][0][ - "provider" - ].string_value(), - "max_local", - ) - assert_equal( - response["output"]["assisted_runtime_capabilities"][0][ - "route" - ].string_value(), - "provider_runtime.query_rewrite.max_local", - ) - - health_stub.wait() - - -def test_status_reports_ready_fake_bridge_truthfully() raises: - with TemporaryDirectory() as temp_dir: - var startup_config_path = Path(temp_dir) / "explicit-hyf-config.toml" - startup_config_path.write_text( - '[service]\ntransport = "stdio"\n\n' - '[runtime]\ndefault_execution_mode = "deterministic"\nallow_assisted = true\n\n' - '[assist]\nbridge_enabled = true\ntransport = "stdio"\nendpoint = "hyf-assistd://fake-query-rewrite"\n' - ) - with ScopedEnvVar(HYF_PATHS_PROFILE_ENV, "repo_local"): - with ScopedEnvVar(HYF_PATHS_REPO_LOCAL_ROOT_ENV, temp_dir): - var response = run_stdio_entrypoint( - "src/main.mojo", - load_scenario_request_json("scenarios/status_ok.json"), - "--config", - startup_config_path.__fspath__(), - ) - - assert_true(response["ok"].bool_value()) - assert_equal( - response["output"]["build_identity"][ - "assisted_execution_available" - ].bool_value(), - True, - ) - assert_equal( - response["output"]["execution_mode_request_behavior"][ - "assisted" - ].string_value(), - "execute", + "deferred", ) - assert_equal( - response["output"]["assisted_runtime"]["state"] - .string_value(), - "ready", - ) - assert_equal( - response["output"]["backend_reachability"][ - "assisted_backend" - ].string_value(), - "ready", - ) - - -def test_status_reports_ready_pure_mojo_provider_without_sidecar_runtime() raises: - with TemporaryDirectory() as temp_dir: - var missing_sidecar_endpoint = ( - Path(temp_dir) / "missing-hyf-assistd" - ).__fspath__() - var startup_config_path = Path(temp_dir) / "explicit-hyf-config.toml" - startup_config_path.write_text( - '[service]\ntransport = "stdio"\n\n' - '[runtime]\ndefault_execution_mode = "deterministic"\nallow_assisted = true\n\n' - '[assist]\nbridge_enabled = true\ntransport = "stdio"\nendpoint = "' - + missing_sidecar_endpoint - + '"\n' - ) - var health_port = reserve_loopback_port() - var health_stub = spawn_max_local_stub(health_port, "health_ok") - with ScopedEnvVar(HYF_PATHS_PROFILE_ENV, "repo_local"): - with ScopedEnvVar(HYF_PATHS_REPO_LOCAL_ROOT_ENV, temp_dir): - with ScopedEnvVar( - "HYF_MAX_LOCAL_HEALTH_URL", - "http://127.0.0.1:" + String(health_port) + "/health", - ): - var response = run_stdio_entrypoint( - "src/main.mojo", - load_scenario_request_json("scenarios/status_ok.json"), - "--config", - startup_config_path.__fspath__(), - ) - - assert_true(response["ok"].bool_value()) - assert_equal( - response["output"]["build_identity"][ - "assisted_execution_available" - ].bool_value(), - True, - ) - assert_equal( - response["output"]["execution_mode_request_behavior"][ - "assisted" - ].string_value(), - "execute", - ) - assert_equal( - response["output"]["assisted_runtime"]["id"] - .string_value(), - "hyf_provider_runtime", - ) - assert_equal( - response["output"]["assisted_runtime"]["kind"] - .string_value(), - "provider_runtime", - ) - assert_equal( - response["output"]["assisted_runtime"]["transport"] - .string_value(), - "in_process", - ) - assert_equal( - response["output"]["assisted_runtime"]["state"] - .string_value(), - "ready", - ) - assert_equal( - response["output"]["assisted_runtime"]["backend_kind"] - .string_value(), - "max_local", - ) - assert_equal( - response["output"]["assisted_runtime"]["provider"] - .string_value(), - "max_local", - ) - assert_equal( - response["output"]["assisted_runtime"]["route"] - .string_value(), - "provider_runtime.query_rewrite.max_local", - ) - assert_equal( - response["output"]["runtime"]["config"]["effective"][ - "assist_endpoint" - ].string_value(), - missing_sidecar_endpoint, - ) - assert_equal( - response["output"]["backend_reachability"][ - "assisted_backend" - ].string_value(), - "ready", - ) - - health_stub.wait() - - -def test_query_rewrite_uses_fake_assist_bridge_when_requested() raises: - with TemporaryDirectory() as temp_dir: - var startup_config_path = Path(temp_dir) / "explicit-hyf-config.toml" - startup_config_path.write_text( - '[service]\ntransport = "stdio"\n\n' - '[runtime]\ndefault_execution_mode = "deterministic"\nallow_assisted = true\n\n' - '[assist]\nbridge_enabled = true\ntransport = "stdio"\nendpoint = "hyf-assistd://fake-query-rewrite"\n' - ) - with ScopedEnvVar(HYF_PATHS_PROFILE_ENV, "repo_local"): - with ScopedEnvVar(HYF_PATHS_REPO_LOCAL_ROOT_ENV, temp_dir): - var response = run_stdio_entrypoint( - "src/main.mojo", - '{"version":1,"request_id":"rewrite-assisted-fake-1","trace_id":"rewrite-assisted-fake-1","capability":"query_rewrite","context":{"execution_mode_preference":"assisted","return_provenance":true},"input":{"query":"apples near me with weekend pickup"}}', - "--config", - startup_config_path.__fspath__(), - ) - - assert_true(response["ok"].bool_value()) - assert_equal( - response["meta"]["execution_mode"].string_value(), - "assisted", - ) - assert_equal( - response["meta"]["backend"].string_value(), - "assist_bridge", - ) - assert_equal( - response["meta"]["provider"].string_value(), - "fake", - ) - assert_equal( - response["meta"]["route"].string_value(), - "assist_bridge.query_rewrite.fake", - ) - assert_equal( - response["meta"]["model"].string_value(), - "fake_query_rewrite_v1", - ) - assert_equal( - Int(response["meta"]["schema_version"].int_value()), - 1, - ) - assert_equal( - Int(response["meta"]["latency_ms"].int_value()), 1 - ) - assert_equal( - response["meta"]["provenance"]["kind"].string_value(), - "assisted", - ) - assert_equal( - response["output"]["rewritten_text"].string_value(), - "apples", - ) - assert_true( - _array_contains_string( - response["output"]["normalization_signals"], - "assist_bridge_fake", - ) - ) - - -def test_query_rewrite_uses_pure_mojo_provider_without_sidecar_runtime() raises: - with TemporaryDirectory() as temp_dir: - var missing_sidecar_endpoint = ( - Path(temp_dir) / "missing-hyf-assistd" - ).__fspath__() - var startup_config_path = Path(temp_dir) / "explicit-hyf-config.toml" - startup_config_path.write_text( - '[service]\ntransport = "stdio"\n\n' - '[runtime]\ndefault_execution_mode = "deterministic"\nallow_assisted = true\n\n' - '[assist]\nbridge_enabled = true\ntransport = "stdio"\nendpoint = "' - + missing_sidecar_endpoint - + '"\n' - ) - var health_port = reserve_loopback_port() - var provider_port = reserve_loopback_port() - var health_stub = spawn_max_local_stub(health_port, "health_ok") - var provider_stub = spawn_max_local_stub( - provider_port, "query_rewrite_ok" - ) - with ScopedEnvVar(HYF_PATHS_PROFILE_ENV, "repo_local"): - with ScopedEnvVar(HYF_PATHS_REPO_LOCAL_ROOT_ENV, temp_dir): - with ScopedEnvVar( - "HYF_MAX_LOCAL_HEALTH_URL", - "http://127.0.0.1:" + String(health_port) + "/health", - ): - with ScopedEnvVar( - "HYF_MAX_LOCAL_BASE_URL", - "http://127.0.0.1:" + String(provider_port) + "/v1", - ): - var response = run_stdio_entrypoint( - "src/main.mojo", - '{"version":1,"request_id":"rewrite-assisted-max-local-1","trace_id":"rewrite-assisted-max-local-1","capability":"query_rewrite","context":{"execution_mode_preference":"assisted","return_provenance":true},"input":{"query":"local apples pickup weekend"}}', - "--config", - startup_config_path.__fspath__(), - ) - - assert_true(response["ok"].bool_value()) - assert_equal( - response["meta"]["execution_mode"].string_value(), - "assisted", - ) - assert_equal( - response["meta"]["backend"].string_value(), - "provider_runtime", - ) - assert_equal( - response["meta"]["provider"].string_value(), - "max_local", - ) - assert_equal( - response["meta"]["route"].string_value(), - "provider_runtime.query_rewrite.max_local", - ) - assert_equal( - response["meta"]["model"].string_value(), - "max-local-query-rewrite", - ) - assert_equal( - Int(response["meta"]["schema_version"].int_value()), - 1, - ) - assert_true( - Int(response["meta"]["latency_ms"].int_value()) - >= 0 - ) - assert_equal( - response["meta"]["provenance"]["kind"] - .string_value(), - "assisted", - ) - assert_equal( - response["output"]["rewritten_text"] - .string_value(), - "apples pickup weekend", - ) - assert_equal( - response["output"]["query_terms"][0] - .string_value(), - "apples", - ) - assert_equal( - response["output"]["extracted_filters"][ - "fulfillment" - ].string_value(), - "pickup", - ) - assert_equal( - response["output"]["extracted_filters"][ - "time_window" - ].string_value(), - "weekend", - ) - - health_stub.wait() - provider_stub.wait() def test_query_rewrite_falls_back_deterministically_when_bridge_is_unavailable() raises: @@ -957,12 +535,12 @@ def test_query_rewrite_falls_back_deterministically_when_bridge_is_unavailable() response["meta"]["provenance"]["fallback"][ "fallback_kind" ].string_value(), - "provider_runtime", + "assisted_execution", ) assert_equal( response["meta"]["provenance"]["fallback"]["reason"] .string_value(), - "unavailable", + "deferred_bootstrap_runtime", ) assert_equal( response["output"]["rewritten_text"].string_value(),