test_stdio_contract.mojo (86434B)
1 import std.os 2 from std.os.path import exists 3 from std.pathlib import Path 4 from std.testing import assert_equal, assert_true, TestSuite 5 from std.tempfile import TemporaryDirectory 6 7 from json import Value 8 from fixture_assertions import ( 9 assert_matches_scenario_response, 10 load_scenario_request_json, 11 status_request_with_invalid_version_json, 12 ) 13 from max_local_process_helper import ( 14 reserve_loopback_port, 15 spawn_max_local_stub, 16 ) 17 from stdio_process_helper import ( 18 HYF_PATHS_PROFILE_ENV, 19 HYF_PATHS_REPO_LOCAL_ROOT_ENV, 20 ScopedEnvVar, 21 run_hyf_stdio, 22 run_stdio_entrypoint, 23 ) 24 25 26 comptime _EXPECTED_INTERNAL_ERROR_MESSAGE = ( 27 "internal hyf daemon error; inspect local diagnostics" 28 ) 29 comptime _HYF_DIAGNOSTICS_DIR_ENV = "HYF_DIAGNOSTICS_DIR" 30 31 32 def _has_key(value: Value, key: String) -> Bool: 33 for candidate in value.object_keys(): 34 if candidate == key: 35 return True 36 return False 37 38 39 def _array_contains_string(value: Value, expected: String) raises -> Bool: 40 for item in value.array_items(): 41 if item.is_string() and item.string_value() == expected: 42 return True 43 return False 44 45 46 def _assert_provider_runtime_fallback_meta( 47 response: Value, expected_reason: String 48 ) raises: 49 assert_equal( 50 response["meta"]["fallback_kind"].string_value(), 51 "provider_runtime", 52 ) 53 assert_equal( 54 response["meta"]["fallback_reason"].string_value(), 55 expected_reason, 56 ) 57 assert_true( 58 _business_fallback_reason_family(expected_reason) != "undeclared" 59 ) 60 assert_true(expected_reason != "non_2xx") 61 62 63 def _assert_no_top_level_fallback_meta(response: Value) raises: 64 assert_true(not _has_key(response["meta"], "fallback_kind")) 65 assert_true(not _has_key(response["meta"], "fallback_reason")) 66 67 68 def _business_fallback_reason_family(reason: String) -> String: 69 if reason == "timeout": 70 return "provider_io" 71 if reason == "connection_failed": 72 return "provider_io" 73 if reason == "invalid_url": 74 return "provider_io" 75 if reason == "provider_non_2xx": 76 return "provider_io" 77 if reason == "provider_error_payload": 78 return "provider_io" 79 if reason == "provider_invalid_json": 80 return "provider_io" 81 if reason == "provider_schema_invalid": 82 return "provider_io" 83 if reason == "provider_empty_choices": 84 return "provider_io" 85 if reason == "provider_missing_content": 86 return "provider_io" 87 if reason == "provider_error": 88 return "provider_io" 89 if reason == "invalid_config": 90 return "runtime_config" 91 if reason == "disabled_by_runtime_config": 92 return "runtime_config" 93 if reason == "provider_unconfigured": 94 return "runtime_config" 95 if reason == "unsupported_capability": 96 return "capability_routing" 97 return "undeclared" 98 99 100 def _assert_declared_business_fallback_reason( 101 reason: String, expected_family: String 102 ) raises: 103 assert_equal(_business_fallback_reason_family(reason), expected_family) 104 assert_true(reason != "non_2xx") 105 106 107 def _max_local_runtime_config_toml_with_urls( 108 base_url: String, health_url: String, request_timeout_ms: Int 109 ) -> String: 110 return ( 111 '[service]\ntransport = "stdio"\n\n' 112 '[runtime]\ndefault_execution_mode = "deterministic"\nallow_assisted = true\n\n' 113 '[assisted]\nprovider = "max_local"\n\n' 114 '[assisted.max_local]\nenabled = true\n' 115 'base_url = "' 116 + base_url 117 + '"\n' 118 + 'health_url = "' 119 + health_url 120 + '"\n' 121 + 'model = "max-local-query-rewrite"\n' 122 + 'request_timeout_ms = ' 123 + String(request_timeout_ms) 124 + "\n" 125 ) 126 127 128 def _unavailable_max_local_runtime_config_toml() raises -> String: 129 var provider_port = reserve_loopback_port() 130 return _max_local_runtime_config_toml_with_urls( 131 "http://127.0.0.1:" + String(provider_port) + "/v1", 132 "http://127.0.0.1:" + String(provider_port) + "/health", 133 250, 134 ) 135 136 137 def _query_rewrite_assisted_request_json(request_id: String) -> String: 138 return _query_rewrite_assisted_request_json_with_deadline( 139 request_id, 2500 140 ) 141 142 143 def _query_rewrite_assisted_request_json_with_deadline( 144 request_id: String, deadline_ms: Int 145 ) -> String: 146 return ( 147 '{"version":1,"request_id":"' 148 + request_id 149 + '","trace_id":"' 150 + request_id 151 + '","capability":"query_rewrite","context":{"execution_mode_preference":"assisted","return_provenance":true,"deadline_ms":' 152 + String(deadline_ms) 153 + '},"input":{"query":"apples near me with weekend pickup"}}' 154 ) 155 156 157 def _query_rewrite_assisted_request_json_without_provenance( 158 request_id: String 159 ) -> String: 160 return ( 161 '{"version":1,"request_id":"' 162 + request_id 163 + '","trace_id":"' 164 + request_id 165 + '","capability":"query_rewrite","context":{"execution_mode_preference":"assisted","return_provenance":false,"deadline_ms":2500},"input":{"query":"apples near me with weekend pickup"}}' 166 ) 167 168 169 def _semantic_rank_assisted_request_json(request_id: String) -> String: 170 return ( 171 '{"version":1,"request_id":"' 172 + request_id 173 + '","trace_id":"' 174 + request_id 175 + '","capability":"semantic_rank","context":{"execution_mode_preference":"assisted","return_provenance":true},"input":{"query":"apples near me with weekend pickup","candidates":[{"id":"listing_local_1","title":"Organic apples","farm":"Local Orchard","delivery":"pickup","distance_km":4.1,"freshness_minutes":3},{"id":"listing_regional_1","title":"Honeycrisp apples","farm":"Regional Orchard","delivery":"delivery","distance_km":28.0,"freshness_minutes":25}]}}' 176 ) 177 178 179 def _assert_query_rewrite_provider_fallback_with_requests( 180 mode: String, expected_reason: String, request_timeout_ms: Int, requests: Int 181 ) raises: 182 _assert_query_rewrite_provider_fallback_with_deadline( 183 mode, expected_reason, request_timeout_ms, 2500, requests 184 ) 185 186 187 def _assert_query_rewrite_provider_fallback_with_deadline( 188 mode: String, 189 expected_reason: String, 190 request_timeout_ms: Int, 191 deadline_ms: Int, 192 requests: Int, 193 ) raises: 194 with TemporaryDirectory() as temp_dir: 195 var provider_port = reserve_loopback_port() 196 var provider_stub = spawn_max_local_stub(provider_port, mode, requests) 197 var startup_config_path = Path(temp_dir) / "explicit-hyf-config.toml" 198 startup_config_path.write_text( 199 _max_local_runtime_config_toml_with_urls( 200 "http://127.0.0.1:" + String(provider_port) + "/v1", 201 "http://127.0.0.1:" + String(provider_port) + "/health", 202 request_timeout_ms, 203 ) 204 ) 205 with ScopedEnvVar(HYF_PATHS_PROFILE_ENV, "repo_local"): 206 with ScopedEnvVar(HYF_PATHS_REPO_LOCAL_ROOT_ENV, temp_dir): 207 var response = run_stdio_entrypoint( 208 "src/main.mojo", 209 _query_rewrite_assisted_request_json_with_deadline( 210 "rewrite-assisted-" + mode, deadline_ms 211 ), 212 "--config", 213 startup_config_path.__fspath__(), 214 ) 215 216 assert_true(response["ok"].bool_value()) 217 assert_equal( 218 response["meta"]["execution_mode"].string_value(), 219 "deterministic", 220 ) 221 assert_equal( 222 response["meta"]["backend"].string_value(), 223 "heuristic", 224 ) 225 assert_true(not _has_key(response["meta"], "provider")) 226 _assert_provider_runtime_fallback_meta( 227 response, expected_reason 228 ) 229 assert_equal( 230 response["meta"]["provenance"]["fallback"][ 231 "fallback_kind" 232 ].string_value(), 233 "provider_runtime", 234 ) 235 assert_equal( 236 response["meta"]["provenance"]["fallback"]["reason"] 237 .string_value(), 238 expected_reason, 239 ) 240 assert_equal( 241 response["output"]["rewritten_text"].string_value(), 242 "apples", 243 ) 244 245 provider_stub.wait() 246 247 248 def _assert_query_rewrite_provider_fallback( 249 mode: String, expected_reason: String, request_timeout_ms: Int 250 ) raises: 251 _assert_query_rewrite_provider_fallback_with_requests( 252 mode, expected_reason, request_timeout_ms, 2 253 ) 254 255 256 def _assert_query_rewrite_runtime_config_fallback( 257 config_text: String, expected_reason: String, request_id: String 258 ) raises: 259 with TemporaryDirectory() as temp_dir: 260 var startup_config_path = Path(temp_dir) / "explicit-hyf-config.toml" 261 startup_config_path.write_text(config_text) 262 with ScopedEnvVar(HYF_PATHS_PROFILE_ENV, "repo_local"): 263 with ScopedEnvVar(HYF_PATHS_REPO_LOCAL_ROOT_ENV, temp_dir): 264 var response = run_stdio_entrypoint( 265 "src/main.mojo", 266 _query_rewrite_assisted_request_json(request_id), 267 "--config", 268 startup_config_path.__fspath__(), 269 ) 270 271 assert_true(response["ok"].bool_value()) 272 assert_equal( 273 response["meta"]["execution_mode"].string_value(), 274 "deterministic", 275 ) 276 assert_equal( 277 response["meta"]["backend"].string_value(), 278 "heuristic", 279 ) 280 assert_true(not _has_key(response["meta"], "provider")) 281 _assert_provider_runtime_fallback_meta( 282 response, expected_reason 283 ) 284 assert_equal( 285 response["meta"]["provenance"]["fallback"][ 286 "fallback_kind" 287 ].string_value(), 288 "provider_runtime", 289 ) 290 assert_equal( 291 response["meta"]["provenance"]["fallback"]["reason"] 292 .string_value(), 293 expected_reason, 294 ) 295 assert_equal( 296 response["output"]["rewritten_text"].string_value(), 297 "apples", 298 ) 299 300 301 def _assert_query_rewrite_runtime_config_fallback_without_provenance( 302 config_text: String, expected_reason: String, request_id: String 303 ) raises: 304 with TemporaryDirectory() as temp_dir: 305 var startup_config_path = Path(temp_dir) / "explicit-hyf-config.toml" 306 startup_config_path.write_text(config_text) 307 with ScopedEnvVar(HYF_PATHS_PROFILE_ENV, "repo_local"): 308 with ScopedEnvVar(HYF_PATHS_REPO_LOCAL_ROOT_ENV, temp_dir): 309 var response = run_stdio_entrypoint( 310 "src/main.mojo", 311 _query_rewrite_assisted_request_json_without_provenance( 312 request_id 313 ), 314 "--config", 315 startup_config_path.__fspath__(), 316 ) 317 318 assert_true(response["ok"].bool_value()) 319 assert_equal( 320 response["meta"]["execution_mode"].string_value(), 321 "deterministic", 322 ) 323 assert_equal( 324 response["meta"]["backend"].string_value(), 325 "heuristic", 326 ) 327 assert_true(not _has_key(response["meta"], "provider")) 328 assert_true(not _has_key(response["meta"], "provenance")) 329 _assert_provider_runtime_fallback_meta( 330 response, expected_reason 331 ) 332 assert_equal( 333 response["output"]["rewritten_text"].string_value(), 334 "apples", 335 ) 336 337 338 def _assert_invalid_runtime_config_load_error( 339 config_text: String, expected_error_fragment: String 340 ) raises: 341 with TemporaryDirectory() as temp_dir: 342 var startup_config_path = Path(temp_dir) / "invalid-hyf-config.toml" 343 startup_config_path.write_text(config_text) 344 with ScopedEnvVar(HYF_PATHS_PROFILE_ENV, "repo_local"): 345 with ScopedEnvVar(HYF_PATHS_REPO_LOCAL_ROOT_ENV, temp_dir): 346 var response = run_stdio_entrypoint( 347 "src/main.mojo", 348 load_scenario_request_json("scenarios/status_ok.json"), 349 "--config", 350 startup_config_path.__fspath__(), 351 ) 352 353 assert_true(response["ok"].bool_value()) 354 assert_equal( 355 response["output"]["runtime"]["config"]["loaded"] 356 .bool_value(), 357 False, 358 ) 359 assert_equal( 360 response["output"]["runtime"]["config"]["load_state"] 361 .string_value(), 362 "invalid", 363 ) 364 assert_true( 365 response["output"]["runtime"]["config"]["load_error"] 366 .string_value() 367 .find(expected_error_fragment) 368 >= 0 369 ) 370 371 372 def _assert_valid_runtime_config_load(config_text: String) raises: 373 with TemporaryDirectory() as temp_dir: 374 var startup_config_path = Path(temp_dir) / "explicit-hyf-config.toml" 375 startup_config_path.write_text(config_text) 376 with ScopedEnvVar(HYF_PATHS_PROFILE_ENV, "repo_local"): 377 with ScopedEnvVar(HYF_PATHS_REPO_LOCAL_ROOT_ENV, temp_dir): 378 var response = run_stdio_entrypoint( 379 "src/main.mojo", 380 load_scenario_request_json("scenarios/status_ok.json"), 381 "--config", 382 startup_config_path.__fspath__(), 383 ) 384 385 assert_true(response["ok"].bool_value()) 386 assert_equal( 387 response["output"]["runtime"]["config"]["loaded"] 388 .bool_value(), 389 True, 390 ) 391 assert_equal( 392 response["output"]["runtime"]["config"]["load_state"] 393 .string_value(), 394 "loaded", 395 ) 396 397 398 def test_business_fallback_reason_taxonomy_declares_provider_io_family() raises: 399 _assert_declared_business_fallback_reason("timeout", "provider_io") 400 _assert_declared_business_fallback_reason( 401 "connection_failed", "provider_io" 402 ) 403 _assert_declared_business_fallback_reason("invalid_url", "provider_io") 404 _assert_declared_business_fallback_reason( 405 "provider_non_2xx", "provider_io" 406 ) 407 _assert_declared_business_fallback_reason( 408 "provider_error_payload", "provider_io" 409 ) 410 _assert_declared_business_fallback_reason( 411 "provider_invalid_json", "provider_io" 412 ) 413 _assert_declared_business_fallback_reason( 414 "provider_schema_invalid", "provider_io" 415 ) 416 _assert_declared_business_fallback_reason( 417 "provider_empty_choices", "provider_io" 418 ) 419 _assert_declared_business_fallback_reason( 420 "provider_missing_content", "provider_io" 421 ) 422 _assert_declared_business_fallback_reason( 423 "provider_error", "provider_io" 424 ) 425 426 427 def test_business_fallback_reason_taxonomy_declares_runtime_family() raises: 428 _assert_declared_business_fallback_reason( 429 "invalid_config", "runtime_config" 430 ) 431 _assert_declared_business_fallback_reason( 432 "disabled_by_runtime_config", "runtime_config" 433 ) 434 _assert_declared_business_fallback_reason( 435 "provider_unconfigured", "runtime_config" 436 ) 437 438 439 def test_business_fallback_reason_taxonomy_declares_capability_family() raises: 440 _assert_declared_business_fallback_reason( 441 "unsupported_capability", "capability_routing" 442 ) 443 444 445 def test_business_fallback_reason_taxonomy_excludes_control_health_reason() raises: 446 assert_equal( 447 _business_fallback_reason_family( 448 "non_2xx" 449 ), 450 "undeclared", 451 ) 452 453 454 def test_business_fallback_reason_taxonomy_excludes_transport_boundary_reason() raises: 455 assert_equal( 456 _business_fallback_reason_family("unknown_transport"), "undeclared" 457 ) 458 459 460 def test_status_success() raises: 461 var response = run_hyf_stdio( 462 load_scenario_request_json("scenarios/status_ok.json") 463 ) 464 assert_matches_scenario_response(response, "scenarios/status_ok.json") 465 466 467 def test_status_reports_repo_local_runtime_truth() raises: 468 with TemporaryDirectory() as temp_dir: 469 var startup_config_path = Path(temp_dir) / "explicit-hyf-config.toml" 470 with ScopedEnvVar(HYF_PATHS_PROFILE_ENV, "repo_local"): 471 with ScopedEnvVar(HYF_PATHS_REPO_LOCAL_ROOT_ENV, temp_dir): 472 var response = run_stdio_entrypoint( 473 "src/main.mojo", 474 load_scenario_request_json("scenarios/status_ok.json"), 475 "--config", 476 startup_config_path.__fspath__(), 477 ) 478 479 assert_equal( 480 response["output"]["runtime"]["id"].string_value(), 481 "hyf_runtime", 482 ) 483 assert_equal( 484 response["output"]["runtime"]["namespace"].string_value(), 485 "services/hyf", 486 ) 487 assert_equal( 488 response["output"]["runtime"][ 489 "paths_profile" 490 ].string_value(), 491 "repo_local", 492 ) 493 assert_equal( 494 response["output"]["runtime"][ 495 "repo_local_base_root" 496 ].string_value(), 497 temp_dir, 498 ) 499 assert_equal( 500 response["output"]["runtime"]["paths"][ 501 "config_path" 502 ].string_value(), 503 temp_dir + "/config/services/hyf/config.toml", 504 ) 505 assert_equal( 506 response["output"]["runtime"]["config"][ 507 "artifact_path" 508 ].string_value(), 509 startup_config_path.__fspath__(), 510 ) 511 assert_equal( 512 response["output"]["runtime"]["config"][ 513 "artifact_path_source" 514 ].string_value(), 515 "startup_flag", 516 ) 517 assert_equal( 518 response["output"]["runtime"]["config"][ 519 "artifact_present" 520 ].bool_value(), 521 False, 522 ) 523 assert_equal( 524 response["output"]["runtime"]["config"][ 525 "load_state" 526 ].string_value(), 527 "not_found", 528 ) 529 assert_equal( 530 response["output"]["runtime"]["config"][ 531 "compiled_defaults_active" 532 ].bool_value(), 533 True, 534 ) 535 assert_equal( 536 response["output"]["runtime"]["config"]["effective"][ 537 "default_execution_mode" 538 ].string_value(), 539 "deterministic", 540 ) 541 assert_equal( 542 response["output"]["runtime"]["config"]["effective"][ 543 "allow_assisted" 544 ].bool_value(), 545 False, 546 ) 547 assert_equal( 548 response["output"]["assisted_runtime"]["state"] 549 .string_value(), 550 "disabled_by_runtime_config", 551 ) 552 assert_equal( 553 response["output"]["backend_reachability"][ 554 "assisted_backend" 555 ].string_value(), 556 "disabled_by_runtime_config", 557 ) 558 assert_equal( 559 response["output"]["runtime"]["paths"][ 560 "diagnostics_dir" 561 ].string_value(), 562 temp_dir + "/logs/services/hyf/diagnostics", 563 ) 564 assert_equal( 565 response["output"]["runtime"]["diagnostics"][ 566 "canonical_dir" 567 ].string_value(), 568 temp_dir + "/logs/services/hyf/diagnostics", 569 ) 570 assert_equal( 571 response["output"]["runtime"]["diagnostics"][ 572 "effective_dir" 573 ].string_value(), 574 temp_dir + "/logs/services/hyf/diagnostics", 575 ) 576 assert_equal( 577 response["output"]["runtime"]["diagnostics"][ 578 "debug_override_active" 579 ].bool_value(), 580 False, 581 ) 582 assert_equal( 583 response["output"]["runtime"]["secret_storage"][ 584 "default_backend" 585 ].string_value(), 586 "encrypted_file", 587 ) 588 assert_equal( 589 response["output"]["runtime"]["secret_storage"][ 590 "status" 591 ].string_value(), 592 "reserved", 593 ) 594 assert_equal( 595 response["output"]["runtime"]["secret_storage"][ 596 "identity_path" 597 ].string_value(), 598 temp_dir + "/secrets/services/hyf/identity.secret.json", 599 ) 600 assert_equal( 601 response["output"]["runtime"]["secret_storage"][ 602 "identity_material_configured" 603 ].bool_value(), 604 False, 605 ) 606 assert_equal( 607 response["output"]["runtime"]["secret_storage"][ 608 "backend_implemented" 609 ].bool_value(), 610 False, 611 ) 612 assert_equal( 613 response["output"]["runtime"]["secret_storage"][ 614 "identity_material_loaded" 615 ].bool_value(), 616 False, 617 ) 618 assert_equal( 619 response["output"]["runtime"]["secret_storage"][ 620 "identity_material_created_by_startup" 621 ].bool_value(), 622 False, 623 ) 624 assert_equal( 625 response["output"]["runtime"]["secret_storage"][ 626 "secret_values_reported" 627 ].bool_value(), 628 False, 629 ) 630 assert_equal( 631 response["output"]["runtime"]["protected_local_data"][ 632 "status" 633 ].string_value(), 634 "reserved", 635 ) 636 assert_equal( 637 response["output"]["runtime"]["protected_local_data"][ 638 "default_dir" 639 ].string_value(), 640 temp_dir + "/data/services/hyf/protected", 641 ) 642 assert_equal( 643 response["output"]["runtime"]["protected_local_data"][ 644 "configured" 645 ].bool_value(), 646 False, 647 ) 648 assert_equal( 649 response["output"]["runtime"]["protected_local_data"][ 650 "support_implemented" 651 ].bool_value(), 652 False, 653 ) 654 assert_equal( 655 response["output"]["runtime"]["protected_local_data"][ 656 "store_open" 657 ].bool_value(), 658 False, 659 ) 660 assert_true( 661 not exists( 662 Path(temp_dir) 663 / "secrets" 664 / "services" 665 / "hyf" 666 / "identity.secret.json" 667 ) 668 ) 669 assert_true( 670 not exists( 671 Path(temp_dir) 672 / "data" 673 / "services" 674 / "hyf" 675 / "protected" 676 ) 677 ) 678 679 680 def test_status_loads_valid_runtime_config_truthfully() raises: 681 with TemporaryDirectory() as temp_dir: 682 var startup_config_path = Path(temp_dir) / "explicit-hyf-config.toml" 683 startup_config_path.write_text( 684 _unavailable_max_local_runtime_config_toml() 685 ) 686 with ScopedEnvVar(HYF_PATHS_PROFILE_ENV, "repo_local"): 687 with ScopedEnvVar(HYF_PATHS_REPO_LOCAL_ROOT_ENV, temp_dir): 688 var response = run_stdio_entrypoint( 689 "src/main.mojo", 690 load_scenario_request_json("scenarios/status_ok.json"), 691 "--config", 692 startup_config_path.__fspath__(), 693 ) 694 695 assert_true(response["ok"].bool_value()) 696 assert_equal( 697 response["output"]["enabled_execution_modes"][ 698 "assisted" 699 ].bool_value(), 700 True, 701 ) 702 assert_equal( 703 response["output"]["execution_mode_request_behavior"][ 704 "assisted" 705 ].string_value(), 706 "provider_unavailable", 707 ) 708 assert_equal( 709 response["output"]["assisted_runtime"]["state"] 710 .string_value(), 711 "unavailable", 712 ) 713 assert_equal( 714 response["output"]["assisted_runtime"]["reason"] 715 .string_value(), 716 "connection_failed", 717 ) 718 assert_equal( 719 response["output"]["assisted_runtime"]["id"] 720 .string_value(), 721 "hyf_provider_runtime", 722 ) 723 assert_equal( 724 response["output"]["assisted_runtime"]["kind"] 725 .string_value(), 726 "provider_runtime", 727 ) 728 assert_equal( 729 response["output"]["assisted_runtime"]["transport"] 730 .string_value(), 731 "http", 732 ) 733 assert_equal( 734 response["output"]["assisted_runtime"]["backend_kind"] 735 .string_value(), 736 "max_local", 737 ) 738 assert_equal( 739 response["output"]["assisted_runtime"]["provider"] 740 .string_value(), 741 "max_local", 742 ) 743 assert_equal( 744 response["output"]["assisted_runtime"]["route"] 745 .string_value(), 746 "provider_runtime.query_rewrite.max_local", 747 ) 748 assert_equal( 749 response["output"]["assisted_runtime"]["model"] 750 .string_value(), 751 "max-local-query-rewrite", 752 ) 753 assert_equal( 754 response["output"]["assisted_runtime"]["reachable"] 755 .bool_value(), 756 False, 757 ) 758 assert_equal( 759 response["output"]["backend_reachability"][ 760 "assisted_backend" 761 ].string_value(), 762 "unavailable", 763 ) 764 assert_equal( 765 response["output"]["runtime"]["config"][ 766 "artifact_present" 767 ].bool_value(), 768 True, 769 ) 770 assert_equal( 771 response["output"]["runtime"]["config"][ 772 "loaded" 773 ].bool_value(), 774 True, 775 ) 776 assert_equal( 777 response["output"]["runtime"]["config"][ 778 "load_state" 779 ].string_value(), 780 "loaded", 781 ) 782 assert_equal( 783 response["output"]["runtime"]["config"][ 784 "compiled_defaults_active" 785 ].bool_value(), 786 False, 787 ) 788 assert_equal( 789 response["output"]["runtime"]["config"]["effective"][ 790 "service_transport" 791 ].string_value(), 792 "stdio", 793 ) 794 assert_equal( 795 response["output"]["runtime"]["config"]["effective"][ 796 "default_execution_mode" 797 ].string_value(), 798 "deterministic", 799 ) 800 assert_equal( 801 response["output"]["runtime"]["config"]["effective"][ 802 "allow_assisted" 803 ].bool_value(), 804 True, 805 ) 806 assert_equal( 807 response["output"]["runtime"]["config"]["effective"][ 808 "assisted_runtime_enabled" 809 ].bool_value(), 810 True, 811 ) 812 assert_equal( 813 response["output"]["runtime"]["config"]["effective"][ 814 "assisted_runtime_configured" 815 ].bool_value(), 816 True, 817 ) 818 assert_equal( 819 response["output"]["runtime"]["config"]["effective"][ 820 "assisted_provider" 821 ].string_value(), 822 "max_local", 823 ) 824 assert_equal( 825 response["output"]["runtime"]["config"]["effective"][ 826 "max_local_enabled" 827 ].bool_value(), 828 True, 829 ) 830 assert_equal( 831 response["output"]["runtime"]["config"]["effective"][ 832 "max_local_model" 833 ].string_value(), 834 "max-local-query-rewrite", 835 ) 836 assert_equal( 837 response["output"]["runtime"]["config"]["effective"][ 838 "max_local_route" 839 ].string_value(), 840 "provider_runtime.query_rewrite.max_local", 841 ) 842 assert_equal( 843 Int( 844 response["output"]["runtime"]["config"]["effective"][ 845 "max_local_request_timeout_ms" 846 ].int_value() 847 ), 848 15000, 849 ) 850 assert_true( 851 not _has_key( 852 response["output"]["runtime"]["config"], 853 "load_error", 854 ) 855 ) 856 857 858 def test_status_reports_invalid_runtime_config_without_crashing() raises: 859 with TemporaryDirectory() as temp_dir: 860 var startup_config_path = Path(temp_dir) / "invalid-hyf-config.toml" 861 startup_config_path.write_text( 862 '[runtime]\ndefault_execution_mode = "assisted"\n' 863 ) 864 with ScopedEnvVar(HYF_PATHS_PROFILE_ENV, "repo_local"): 865 with ScopedEnvVar(HYF_PATHS_REPO_LOCAL_ROOT_ENV, temp_dir): 866 var response = run_stdio_entrypoint( 867 "src/main.mojo", 868 load_scenario_request_json("scenarios/status_ok.json"), 869 "--config", 870 startup_config_path.__fspath__(), 871 ) 872 873 assert_true(response["ok"].bool_value()) 874 assert_equal( 875 response["output"]["enabled_execution_modes"][ 876 "assisted" 877 ].bool_value(), 878 False, 879 ) 880 assert_equal( 881 response["output"]["execution_mode_request_behavior"][ 882 "assisted" 883 ].string_value(), 884 "invalid_config", 885 ) 886 assert_equal( 887 response["output"]["assisted_runtime"]["state"] 888 .string_value(), 889 "invalid_config", 890 ) 891 assert_equal( 892 response["output"]["assisted_runtime"]["reason"] 893 .string_value(), 894 "invalid_config", 895 ) 896 assert_equal( 897 response["output"]["runtime"]["config"][ 898 "artifact_present" 899 ].bool_value(), 900 True, 901 ) 902 assert_equal( 903 response["output"]["runtime"]["config"][ 904 "loaded" 905 ].bool_value(), 906 False, 907 ) 908 assert_equal( 909 response["output"]["runtime"]["config"][ 910 "load_state" 911 ].string_value(), 912 "invalid", 913 ) 914 assert_equal( 915 response["output"]["runtime"]["config"][ 916 "compiled_defaults_active" 917 ].bool_value(), 918 True, 919 ) 920 assert_true( 921 response["output"]["runtime"]["config"]["load_error"] 922 .string_value() 923 .find("default_execution_mode") 924 >= 0 925 ) 926 assert_equal( 927 response["output"]["runtime"]["config"]["effective"][ 928 "allow_assisted" 929 ].bool_value(), 930 False, 931 ) 932 933 934 def test_status_reports_unconfigured_assisted_runtime_truthfully() raises: 935 with TemporaryDirectory() as temp_dir: 936 var startup_config_path = Path(temp_dir) / "explicit-hyf-config.toml" 937 startup_config_path.write_text( 938 '[service]\ntransport = "stdio"\n\n' 939 + '[runtime]\ndefault_execution_mode = "deterministic"\nallow_assisted = true\n\n' 940 + '[assisted]\nprovider = "max_local"\n' 941 ) 942 with ScopedEnvVar(HYF_PATHS_PROFILE_ENV, "repo_local"): 943 with ScopedEnvVar(HYF_PATHS_REPO_LOCAL_ROOT_ENV, temp_dir): 944 var response = run_stdio_entrypoint( 945 "src/main.mojo", 946 load_scenario_request_json("scenarios/status_ok.json"), 947 "--config", 948 startup_config_path.__fspath__(), 949 ) 950 951 assert_true(response["ok"].bool_value()) 952 assert_equal( 953 response["output"]["execution_mode_request_behavior"][ 954 "assisted" 955 ].string_value(), 956 "provider_unconfigured", 957 ) 958 assert_equal( 959 response["output"]["assisted_runtime"]["state"] 960 .string_value(), 961 "unconfigured", 962 ) 963 assert_equal( 964 response["output"]["assisted_runtime"]["reason"] 965 .string_value(), 966 "not_checked", 967 ) 968 assert_equal( 969 response["output"]["assisted_runtime"]["transport"] 970 .string_value(), 971 "deferred", 972 ) 973 assert_equal( 974 response["output"]["assisted_runtime"]["configured"] 975 .bool_value(), 976 False, 977 ) 978 979 980 def test_status_reports_non_2xx_max_local_health_truthfully() raises: 981 with TemporaryDirectory() as temp_dir: 982 var provider_port = reserve_loopback_port() 983 var provider_stub = spawn_max_local_stub( 984 provider_port, "health_non_2xx", 1 985 ) 986 var startup_config_path = Path(temp_dir) / "explicit-hyf-config.toml" 987 startup_config_path.write_text( 988 _max_local_runtime_config_toml_with_urls( 989 "http://127.0.0.1:" + String(provider_port) + "/v1", 990 "http://127.0.0.1:" + String(provider_port) + "/health", 991 15000, 992 ) 993 ) 994 with ScopedEnvVar(HYF_PATHS_PROFILE_ENV, "repo_local"): 995 with ScopedEnvVar(HYF_PATHS_REPO_LOCAL_ROOT_ENV, temp_dir): 996 var response = run_stdio_entrypoint( 997 "src/main.mojo", 998 load_scenario_request_json("scenarios/status_ok.json"), 999 "--config", 1000 startup_config_path.__fspath__(), 1001 ) 1002 1003 assert_true(response["ok"].bool_value()) 1004 assert_equal( 1005 response["output"]["assisted_runtime"]["state"] 1006 .string_value(), 1007 "unavailable", 1008 ) 1009 assert_equal( 1010 response["output"]["assisted_runtime"]["reason"] 1011 .string_value(), 1012 "non_2xx", 1013 ) 1014 assert_equal( 1015 response["output"]["assisted_runtime"]["reachable"] 1016 .bool_value(), 1017 False, 1018 ) 1019 1020 provider_stub.wait() 1021 1022 1023 def test_status_reports_ready_max_local_provider_truthfully() raises: 1024 with TemporaryDirectory() as temp_dir: 1025 var provider_port = reserve_loopback_port() 1026 var provider_stub = spawn_max_local_stub( 1027 provider_port, "query_rewrite_ok", 1 1028 ) 1029 var startup_config_path = Path(temp_dir) / "explicit-hyf-config.toml" 1030 startup_config_path.write_text( 1031 _max_local_runtime_config_toml_with_urls( 1032 "http://127.0.0.1:" + String(provider_port) + "/v1", 1033 "http://127.0.0.1:" + String(provider_port) + "/health", 1034 15000, 1035 ) 1036 ) 1037 with ScopedEnvVar(HYF_PATHS_PROFILE_ENV, "repo_local"): 1038 with ScopedEnvVar(HYF_PATHS_REPO_LOCAL_ROOT_ENV, temp_dir): 1039 var response = run_stdio_entrypoint( 1040 "src/main.mojo", 1041 load_scenario_request_json("scenarios/status_ok.json"), 1042 "--config", 1043 startup_config_path.__fspath__(), 1044 ) 1045 1046 assert_true(response["ok"].bool_value()) 1047 assert_equal( 1048 response["output"]["execution_mode_request_behavior"][ 1049 "assisted" 1050 ].string_value(), 1051 "execute", 1052 ) 1053 assert_equal( 1054 response["output"]["backend_reachability"][ 1055 "assisted_backend" 1056 ].string_value(), 1057 "ready", 1058 ) 1059 assert_equal( 1060 response["output"]["assisted_runtime"]["state"] 1061 .string_value(), 1062 "ready", 1063 ) 1064 assert_equal( 1065 response["output"]["assisted_runtime"]["reason"] 1066 .string_value(), 1067 "ready", 1068 ) 1069 assert_equal( 1070 response["output"]["assisted_runtime"]["transport"] 1071 .string_value(), 1072 "http", 1073 ) 1074 assert_equal( 1075 response["output"]["assisted_runtime"]["backend_kind"] 1076 .string_value(), 1077 "max_local", 1078 ) 1079 assert_equal( 1080 response["output"]["assisted_runtime"]["provider"] 1081 .string_value(), 1082 "max_local", 1083 ) 1084 assert_equal( 1085 response["output"]["assisted_runtime"]["route"] 1086 .string_value(), 1087 "provider_runtime.query_rewrite.max_local", 1088 ) 1089 assert_equal( 1090 response["output"]["assisted_runtime"]["model"] 1091 .string_value(), 1092 "max-local-query-rewrite", 1093 ) 1094 assert_equal( 1095 response["output"]["assisted_runtime"]["reachable"] 1096 .bool_value(), 1097 True, 1098 ) 1099 1100 provider_stub.wait() 1101 1102 1103 def test_status_bounds_max_local_health_probe_timeout() raises: 1104 with TemporaryDirectory() as temp_dir: 1105 var provider_port = reserve_loopback_port() 1106 var provider_stub = spawn_max_local_stub( 1107 provider_port, "health_timeout", 1 1108 ) 1109 var startup_config_path = Path(temp_dir) / "explicit-hyf-config.toml" 1110 startup_config_path.write_text( 1111 _max_local_runtime_config_toml_with_urls( 1112 "http://127.0.0.1:" + String(provider_port) + "/v1", 1113 "http://127.0.0.1:" + String(provider_port) + "/health", 1114 15000, 1115 ) 1116 ) 1117 with ScopedEnvVar(HYF_PATHS_PROFILE_ENV, "repo_local"): 1118 with ScopedEnvVar(HYF_PATHS_REPO_LOCAL_ROOT_ENV, temp_dir): 1119 var response = run_stdio_entrypoint( 1120 "src/main.mojo", 1121 load_scenario_request_json("scenarios/status_ok.json"), 1122 "--config", 1123 startup_config_path.__fspath__(), 1124 ) 1125 1126 assert_true(response["ok"].bool_value()) 1127 assert_equal( 1128 response["output"]["assisted_runtime"]["state"] 1129 .string_value(), 1130 "unavailable", 1131 ) 1132 assert_equal( 1133 response["output"]["assisted_runtime"]["reason"] 1134 .string_value(), 1135 "timeout", 1136 ) 1137 assert_equal( 1138 response["output"]["assisted_runtime"]["reachable"] 1139 .bool_value(), 1140 False, 1141 ) 1142 1143 provider_stub.wait() 1144 1145 1146 def test_status_rejects_invalid_max_local_runtime_config() raises: 1147 var prefix = ( 1148 '[service]\ntransport = "stdio"\n\n' 1149 '[runtime]\ndefault_execution_mode = "deterministic"\nallow_assisted = true\n\n' 1150 ) 1151 var disabled_prefix = ( 1152 '[service]\ntransport = "stdio"\n\n' 1153 '[runtime]\ndefault_execution_mode = "deterministic"\nallow_assisted = false\n\n' 1154 ) 1155 var provider = '[assisted]\nprovider = "max_local"\n\n' 1156 var max_local_header = '[assisted.max_local]\nenabled = true\n' 1157 _assert_invalid_runtime_config_load_error( 1158 prefix + '[assisted]\nprovider = "unsupported"\n', 1159 "assisted.provider", 1160 ) 1161 _assert_invalid_runtime_config_load_error( 1162 prefix + '[assisted]\nprovider = " max_local"\n', 1163 "assisted.provider", 1164 ) 1165 _assert_invalid_runtime_config_load_error( 1166 disabled_prefix 1167 + provider 1168 + max_local_header 1169 + 'base_url = "http://127.0.0.1:8000/v1"\n' 1170 + 'health_url = "http://127.0.0.1:8000/health"\n' 1171 + 'model = "max-local-query-rewrite"\n' 1172 + 'request_timeout_ms = 15000\n', 1173 "runtime.allow_assisted", 1174 ) 1175 _assert_invalid_runtime_config_load_error( 1176 prefix 1177 + provider 1178 + max_local_header 1179 + 'health_url = "http://127.0.0.1:8000/health"\n' 1180 + 'model = "max-local-query-rewrite"\n' 1181 + 'request_timeout_ms = 15000\n', 1182 "assisted.max_local.base_url", 1183 ) 1184 _assert_invalid_runtime_config_load_error( 1185 prefix 1186 + provider 1187 + max_local_header 1188 + 'base_url = " http://127.0.0.1:8000/v1"\n' 1189 + 'health_url = "http://127.0.0.1:8000/health"\n' 1190 + 'model = "max-local-query-rewrite"\n' 1191 + 'request_timeout_ms = 15000\n', 1192 "assisted.max_local.base_url", 1193 ) 1194 _assert_invalid_runtime_config_load_error( 1195 prefix 1196 + provider 1197 + max_local_header 1198 + 'base_url = "http://127.0.0.1:8000/v1"\n' 1199 + 'health_url = "http://127.0.0.1:8000/health "\n' 1200 + 'model = "max-local-query-rewrite"\n' 1201 + 'request_timeout_ms = 15000\n', 1202 "assisted.max_local.health_url", 1203 ) 1204 _assert_invalid_runtime_config_load_error( 1205 prefix 1206 + provider 1207 + max_local_header 1208 + 'base_url = "file:///tmp/max"\n' 1209 + 'health_url = "http://127.0.0.1:8000/health"\n' 1210 + 'model = "max-local-query-rewrite"\n' 1211 + 'request_timeout_ms = 15000\n', 1212 "assisted.max_local.base_url", 1213 ) 1214 _assert_invalid_runtime_config_load_error( 1215 prefix 1216 + provider 1217 + max_local_header 1218 + 'base_url = "http://127.0.0.1:8000/v1"\n' 1219 + 'health_url = "http://127.0.0.1:8000/health"\n' 1220 + 'model = " max-local-query-rewrite"\n' 1221 + 'request_timeout_ms = 15000\n', 1222 "assisted.max_local.model", 1223 ) 1224 _assert_invalid_runtime_config_load_error( 1225 prefix 1226 + provider 1227 + max_local_header 1228 + 'base_url = "http://127.0.0.1:8000/v1"\n' 1229 + 'health_url = "http://127.0.0.1:8000/health"\n' 1230 + 'model = ""\n' 1231 + 'request_timeout_ms = 15000\n', 1232 "assisted.max_local.model", 1233 ) 1234 _assert_invalid_runtime_config_load_error( 1235 prefix 1236 + provider 1237 + max_local_header 1238 + 'base_url = "http://127.0.0.1:8000/v1"\n' 1239 + 'health_url = "http://127.0.0.1:8000/health"\n' 1240 + 'model = "max-local-query-rewrite"\n' 1241 + 'route = "provider_runtime.query_rewrite.max_local"\n' 1242 + 'request_timeout_ms = 15000\n', 1243 "assisted.max_local.route", 1244 ) 1245 _assert_invalid_runtime_config_load_error( 1246 prefix 1247 + provider 1248 + '[assisted.max_local] # provider route is derived\n' 1249 + 'enabled = true\n' 1250 + 'base_url = "http://127.0.0.1:8000/v1"\n' 1251 + 'health_url = "http://127.0.0.1:8000/health"\n' 1252 + 'model = "max-local-query-rewrite"\n' 1253 + 'route = "provider_runtime.query_rewrite.max_local"\n' 1254 + 'request_timeout_ms = 15000\n', 1255 "assisted.max_local.route", 1256 ) 1257 _assert_invalid_runtime_config_load_error( 1258 prefix 1259 + provider 1260 + max_local_header 1261 + 'base_url = "http://127.0.0.1:8000/v1"\n' 1262 + 'health_url = "http://127.0.0.1:8000/health"\n' 1263 + 'model = "max-local-query-rewrite"\n' 1264 + '"route" = "provider_runtime.query_rewrite.max_local"\n' 1265 + 'request_timeout_ms = 15000\n', 1266 "assisted.max_local.route", 1267 ) 1268 _assert_invalid_runtime_config_load_error( 1269 prefix 1270 + provider 1271 + max_local_header 1272 + 'base_url = "http://127.0.0.1:8000/v1"\n' 1273 + 'health_url = "http://127.0.0.1:8000/health"\n' 1274 + 'model = "max-local-query-rewrite"\n' 1275 + "'route' = \"provider_runtime.query_rewrite.max_local\"\n" 1276 + 'request_timeout_ms = 15000\n', 1277 "assisted.max_local.route", 1278 ) 1279 _assert_invalid_runtime_config_load_error( 1280 prefix 1281 + provider 1282 + 'assisted.max_local.route = "provider_runtime.query_rewrite.max_local"\n' 1283 + max_local_header 1284 + 'base_url = "http://127.0.0.1:8000/v1"\n' 1285 + 'health_url = "http://127.0.0.1:8000/health"\n' 1286 + 'model = "max-local-query-rewrite"\n' 1287 + 'request_timeout_ms = 15000\n', 1288 "assisted.max_local.route", 1289 ) 1290 _assert_invalid_runtime_config_load_error( 1291 prefix 1292 + provider 1293 + '"assisted"."max_local"."route" = "provider_runtime.query_rewrite.max_local"\n' 1294 + max_local_header 1295 + 'base_url = "http://127.0.0.1:8000/v1"\n' 1296 + 'health_url = "http://127.0.0.1:8000/health"\n' 1297 + 'model = "max-local-query-rewrite"\n' 1298 + 'request_timeout_ms = 15000\n', 1299 "assisted.max_local.route", 1300 ) 1301 _assert_invalid_runtime_config_load_error( 1302 prefix 1303 + provider 1304 + "'assisted'.'max_local'.'route' = \"provider_runtime.query_rewrite.max_local\"\n" 1305 + max_local_header 1306 + 'base_url = "http://127.0.0.1:8000/v1"\n' 1307 + 'health_url = "http://127.0.0.1:8000/health"\n' 1308 + 'model = "max-local-query-rewrite"\n' 1309 + 'request_timeout_ms = 15000\n', 1310 "assisted.max_local.route", 1311 ) 1312 _assert_invalid_runtime_config_load_error( 1313 prefix 1314 + provider 1315 + '[assisted."max_local"]\n' 1316 + 'enabled = true\n' 1317 + 'base_url = "http://127.0.0.1:8000/v1"\n' 1318 + 'health_url = "http://127.0.0.1:8000/health"\n' 1319 + 'model = "max-local-query-rewrite"\n' 1320 + 'route = "provider_runtime.query_rewrite.max_local"\n' 1321 + 'request_timeout_ms = 15000\n', 1322 "assisted.max_local.route", 1323 ) 1324 _assert_invalid_runtime_config_load_error( 1325 prefix 1326 + provider 1327 + '["assisted"."max_local"]\n' 1328 + 'enabled = true\n' 1329 + 'base_url = "http://127.0.0.1:8000/v1"\n' 1330 + 'health_url = "http://127.0.0.1:8000/health"\n' 1331 + 'model = "max-local-query-rewrite"\n' 1332 + 'route = "provider_runtime.query_rewrite.max_local"\n' 1333 + 'request_timeout_ms = 15000\n', 1334 "assisted.max_local.route", 1335 ) 1336 _assert_invalid_runtime_config_load_error( 1337 prefix 1338 + provider 1339 + 'assisted.max_local = { enabled = true, ' 1340 + 'base_url = "http://127.0.0.1:8000/v1", ' 1341 + 'health_url = "http://127.0.0.1:8000/health", ' 1342 + 'model = "max-local-query-rewrite", ' 1343 + 'route = "provider_runtime.query_rewrite.max_local", ' 1344 + 'request_timeout_ms = 15000 }\n', 1345 "assisted.max_local.route", 1346 ) 1347 _assert_invalid_runtime_config_load_error( 1348 prefix 1349 + provider 1350 + 'assisted.max_local = { enabled = true, ' 1351 + 'base_url = "http://127.0.0.1:8000/v1", ' 1352 + 'health_url = "http://127.0.0.1:8000/health", ' 1353 + 'model = "max-local-query-rewrite, route = quoted literal", ' 1354 + 'route = "provider_runtime.query_rewrite.max_local", ' 1355 + 'request_timeout_ms = 15000 }\n', 1356 "assisted.max_local.route", 1357 ) 1358 _assert_invalid_runtime_config_load_error( 1359 prefix 1360 + provider 1361 + 'assisted.max_local = { enabled = true, ' 1362 + 'base_url = "http://127.0.0.1:8000/v1", ' 1363 + 'health_url = "http://127.0.0.1:8000/health", ' 1364 + 'model = "max-local-query-rewrite", ' 1365 + "'route' = \"provider_runtime.query_rewrite.max_local\", " 1366 + 'request_timeout_ms = 15000 }\n', 1367 "assisted.max_local.route", 1368 ) 1369 _assert_invalid_runtime_config_load_error( 1370 prefix 1371 + provider 1372 + max_local_header 1373 + 'base_url = "http://127.0.0.1:8000/v1"\n' 1374 + 'health_url = "http://127.0.0.1:8000/health"\n' 1375 + 'model = "max-local-query-rewrite"\n' 1376 + 'request_timeout_ms = 0\n', 1377 "assisted.max_local.request_timeout_ms", 1378 ) 1379 1380 1381 def test_status_allows_non_route_toml_mentions() raises: 1382 var config = ( 1383 '[service]\ntransport = "stdio"\n\n' 1384 + '[runtime]\ndefault_execution_mode = "deterministic"\nallow_assisted = true\n\n' 1385 + '[assisted]\nprovider = "max_local"\n\n' 1386 + '# assisted.max_local.route is intentionally derived by HYF\n' 1387 + '[assisted.max_local]\n' 1388 + '# route = "provider_runtime.query_rewrite.max_local"\n' 1389 + 'enabled = true\n' 1390 + 'base_url = "http://127.0.0.1:8000/v1?route=provider_runtime.query_rewrite.max_local"\n' 1391 + 'health_url = "http://127.0.0.1:8000/health"\n' 1392 + 'model = "max-local-query-rewrite-route-token"\n' 1393 + 'request_timeout_ms = 15000\n' 1394 ) 1395 _assert_valid_runtime_config_load(config) 1396 1397 1398 def test_status_allows_inline_table_quoted_route_mentions() raises: 1399 var config = ( 1400 '[service]\ntransport = "stdio"\n\n' 1401 + '[runtime]\ndefault_execution_mode = "deterministic"\nallow_assisted = true\n\n' 1402 + '[assisted]\nprovider = "max_local"\n\n' 1403 + 'assisted.max_local = { enabled = true, ' 1404 + 'base_url = "http://127.0.0.1:8000/v1", ' 1405 + 'health_url = "http://127.0.0.1:8000/health", ' 1406 + 'model = "max-local-query-rewrite, route = quoted literal", ' 1407 + 'request_timeout_ms = 15000 }\n' 1408 ) 1409 _assert_valid_runtime_config_load(config) 1410 1411 1412 def test_capabilities_reports_configured_provider_runtime_truthfully() raises: 1413 with TemporaryDirectory() as temp_dir: 1414 var startup_config_path = Path(temp_dir) / "explicit-hyf-config.toml" 1415 startup_config_path.write_text( 1416 _unavailable_max_local_runtime_config_toml() 1417 ) 1418 with ScopedEnvVar(HYF_PATHS_PROFILE_ENV, "repo_local"): 1419 with ScopedEnvVar(HYF_PATHS_REPO_LOCAL_ROOT_ENV, temp_dir): 1420 var response = run_stdio_entrypoint( 1421 "src/main.mojo", 1422 load_scenario_request_json("scenarios/capabilities_ok.json"), 1423 "--config", 1424 startup_config_path.__fspath__(), 1425 ) 1426 1427 assert_true(response["ok"].bool_value()) 1428 assert_equal( 1429 response["output"]["business_capabilities"][0][ 1430 "assisted_execution" 1431 ].string_value(), 1432 "unavailable", 1433 ) 1434 assert_equal( 1435 response["output"]["business_capabilities"][0][ 1436 "assisted_backend_available" 1437 ].bool_value(), 1438 False, 1439 ) 1440 assert_equal( 1441 response["output"]["assisted_runtime_capabilities"][0][ 1442 "id" 1443 ].string_value(), 1444 "hyf_provider_runtime", 1445 ) 1446 assert_equal( 1447 response["output"]["assisted_runtime_capabilities"][0][ 1448 "kind" 1449 ].string_value(), 1450 "provider_runtime", 1451 ) 1452 assert_equal( 1453 response["output"]["assisted_runtime_capabilities"][0][ 1454 "state" 1455 ].string_value(), 1456 "unavailable", 1457 ) 1458 assert_equal( 1459 response["output"]["assisted_runtime_capabilities"][0][ 1460 "reason" 1461 ].string_value(), 1462 "connection_failed", 1463 ) 1464 assert_equal( 1465 response["output"]["assisted_runtime_capabilities"][0][ 1466 "backend_kind" 1467 ].string_value(), 1468 "max_local", 1469 ) 1470 1471 1472 def test_capabilities_reports_ready_max_local_provider_truthfully() raises: 1473 with TemporaryDirectory() as temp_dir: 1474 var provider_port = reserve_loopback_port() 1475 var provider_stub = spawn_max_local_stub( 1476 provider_port, "query_rewrite_ok", 1 1477 ) 1478 var startup_config_path = Path(temp_dir) / "explicit-hyf-config.toml" 1479 startup_config_path.write_text( 1480 _max_local_runtime_config_toml_with_urls( 1481 "http://127.0.0.1:" + String(provider_port) + "/v1", 1482 "http://127.0.0.1:" + String(provider_port) + "/health", 1483 15000, 1484 ) 1485 ) 1486 with ScopedEnvVar(HYF_PATHS_PROFILE_ENV, "repo_local"): 1487 with ScopedEnvVar(HYF_PATHS_REPO_LOCAL_ROOT_ENV, temp_dir): 1488 var response = run_stdio_entrypoint( 1489 "src/main.mojo", 1490 load_scenario_request_json("scenarios/capabilities_ok.json"), 1491 "--config", 1492 startup_config_path.__fspath__(), 1493 ) 1494 1495 assert_true(response["ok"].bool_value()) 1496 assert_equal( 1497 response["output"]["business_capabilities"][0][ 1498 "assisted_execution" 1499 ].string_value(), 1500 "ready", 1501 ) 1502 assert_equal( 1503 response["output"]["business_capabilities"][0][ 1504 "assisted_backend_available" 1505 ].bool_value(), 1506 True, 1507 ) 1508 assert_equal( 1509 response["output"]["business_capabilities"][2][ 1510 "assisted_execution" 1511 ].string_value(), 1512 "unsupported_capability", 1513 ) 1514 assert_equal( 1515 response["output"]["assisted_runtime_capabilities"][0][ 1516 "state" 1517 ].string_value(), 1518 "ready", 1519 ) 1520 assert_equal( 1521 response["output"]["assisted_runtime_capabilities"][0][ 1522 "reason" 1523 ].string_value(), 1524 "ready", 1525 ) 1526 assert_equal( 1527 response["output"]["assisted_runtime_capabilities"][0][ 1528 "backend_kind" 1529 ].string_value(), 1530 "max_local", 1531 ) 1532 1533 provider_stub.wait() 1534 1535 1536 def test_capabilities_bounds_max_local_health_probe_timeout() raises: 1537 with TemporaryDirectory() as temp_dir: 1538 var provider_port = reserve_loopback_port() 1539 var provider_stub = spawn_max_local_stub( 1540 provider_port, "health_timeout", 1 1541 ) 1542 var startup_config_path = Path(temp_dir) / "explicit-hyf-config.toml" 1543 startup_config_path.write_text( 1544 _max_local_runtime_config_toml_with_urls( 1545 "http://127.0.0.1:" + String(provider_port) + "/v1", 1546 "http://127.0.0.1:" + String(provider_port) + "/health", 1547 15000, 1548 ) 1549 ) 1550 with ScopedEnvVar(HYF_PATHS_PROFILE_ENV, "repo_local"): 1551 with ScopedEnvVar(HYF_PATHS_REPO_LOCAL_ROOT_ENV, temp_dir): 1552 var response = run_stdio_entrypoint( 1553 "src/main.mojo", 1554 load_scenario_request_json("scenarios/capabilities_ok.json"), 1555 "--config", 1556 startup_config_path.__fspath__(), 1557 ) 1558 1559 assert_true(response["ok"].bool_value()) 1560 assert_equal( 1561 response["output"]["business_capabilities"][0][ 1562 "assisted_execution" 1563 ].string_value(), 1564 "unavailable", 1565 ) 1566 assert_equal( 1567 response["output"]["business_capabilities"][0][ 1568 "assisted_backend_available" 1569 ].bool_value(), 1570 False, 1571 ) 1572 assert_equal( 1573 response["output"]["assisted_runtime_capabilities"][0][ 1574 "state" 1575 ].string_value(), 1576 "unavailable", 1577 ) 1578 assert_equal( 1579 response["output"]["assisted_runtime_capabilities"][0][ 1580 "reason" 1581 ].string_value(), 1582 "timeout", 1583 ) 1584 1585 provider_stub.wait() 1586 1587 1588 def test_query_rewrite_falls_back_deterministically_when_provider_is_unavailable() raises: 1589 with TemporaryDirectory() as temp_dir: 1590 var startup_config_path = Path(temp_dir) / "explicit-hyf-config.toml" 1591 startup_config_path.write_text( 1592 _unavailable_max_local_runtime_config_toml() 1593 ) 1594 with ScopedEnvVar(HYF_PATHS_PROFILE_ENV, "repo_local"): 1595 with ScopedEnvVar(HYF_PATHS_REPO_LOCAL_ROOT_ENV, temp_dir): 1596 var response = run_stdio_entrypoint( 1597 "src/main.mojo", 1598 _query_rewrite_assisted_request_json( 1599 "rewrite-assisted-fallback-1" 1600 ), 1601 "--config", 1602 startup_config_path.__fspath__(), 1603 ) 1604 1605 assert_true(response["ok"].bool_value()) 1606 assert_equal( 1607 response["meta"]["execution_mode"].string_value(), 1608 "deterministic", 1609 ) 1610 assert_equal( 1611 response["meta"]["backend"].string_value(), 1612 "heuristic", 1613 ) 1614 assert_true(not _has_key(response["meta"], "provider")) 1615 assert_equal( 1616 response["meta"]["provenance"]["fallback"][ 1617 "fallback_kind" 1618 ].string_value(), 1619 "provider_runtime", 1620 ) 1621 assert_equal( 1622 response["meta"]["provenance"]["fallback"]["reason"] 1623 .string_value(), 1624 "connection_failed", 1625 ) 1626 assert_equal( 1627 response["output"]["rewritten_text"].string_value(), 1628 "apples", 1629 ) 1630 1631 1632 def test_query_rewrite_falls_back_on_unconfigured_provider_runtime() raises: 1633 _assert_query_rewrite_runtime_config_fallback( 1634 '[service]\ntransport = "stdio"\n\n' 1635 + '[runtime]\ndefault_execution_mode = "deterministic"\nallow_assisted = true\n\n' 1636 + '[assisted]\nprovider = "max_local"\n', 1637 "provider_unconfigured", 1638 "rewrite-assisted-unconfigured-runtime-1", 1639 ) 1640 1641 1642 def test_query_rewrite_falls_back_on_invalid_provider_runtime_config() raises: 1643 _assert_query_rewrite_runtime_config_fallback( 1644 '[runtime]\ndefault_execution_mode = "assisted"\n', 1645 "invalid_config", 1646 "rewrite-assisted-invalid-runtime-1", 1647 ) 1648 1649 1650 def test_query_rewrite_fallback_metadata_is_visible_without_provenance() raises: 1651 _assert_query_rewrite_runtime_config_fallback_without_provenance( 1652 '[service]\ntransport = "stdio"\n\n' 1653 + '[runtime]\ndefault_execution_mode = "deterministic"\nallow_assisted = true\n\n' 1654 + '[assisted]\nprovider = "max_local"\n', 1655 "provider_unconfigured", 1656 "rewrite-assisted-no-provenance-1", 1657 ) 1658 1659 1660 def test_assisted_semantic_rank_falls_back_as_unsupported_provider_capability() raises: 1661 with TemporaryDirectory() as temp_dir: 1662 var startup_config_path = Path(temp_dir) / "explicit-hyf-config.toml" 1663 startup_config_path.write_text( 1664 _unavailable_max_local_runtime_config_toml() 1665 ) 1666 with ScopedEnvVar(HYF_PATHS_PROFILE_ENV, "repo_local"): 1667 with ScopedEnvVar(HYF_PATHS_REPO_LOCAL_ROOT_ENV, temp_dir): 1668 var response = run_stdio_entrypoint( 1669 "src/main.mojo", 1670 _semantic_rank_assisted_request_json( 1671 "rank-assisted-unsupported-1" 1672 ), 1673 "--config", 1674 startup_config_path.__fspath__(), 1675 ) 1676 1677 assert_true(response["ok"].bool_value()) 1678 assert_equal( 1679 response["meta"]["execution_mode"].string_value(), 1680 "deterministic", 1681 ) 1682 assert_equal( 1683 response["meta"]["backend"].string_value(), 1684 "heuristic", 1685 ) 1686 assert_true(not _has_key(response["meta"], "provider")) 1687 _assert_provider_runtime_fallback_meta( 1688 response, "unsupported_capability" 1689 ) 1690 assert_equal( 1691 response["meta"]["provenance"]["fallback"][ 1692 "fallback_kind" 1693 ].string_value(), 1694 "provider_runtime", 1695 ) 1696 assert_equal( 1697 response["meta"]["provenance"]["fallback"]["reason"] 1698 .string_value(), 1699 "unsupported_capability", 1700 ) 1701 assert_equal( 1702 response["output"]["ranked_ids"][0].string_value(), 1703 "listing_local_1", 1704 ) 1705 1706 1707 def test_query_rewrite_uses_max_local_provider_when_ready() raises: 1708 with TemporaryDirectory() as temp_dir: 1709 var provider_port = reserve_loopback_port() 1710 var provider_stub = spawn_max_local_stub( 1711 provider_port, "query_rewrite_ok", 2 1712 ) 1713 var startup_config_path = Path(temp_dir) / "explicit-hyf-config.toml" 1714 startup_config_path.write_text( 1715 _max_local_runtime_config_toml_with_urls( 1716 "http://127.0.0.1:" + String(provider_port) + "/v1", 1717 "http://127.0.0.1:" + String(provider_port) + "/health", 1718 15000, 1719 ) 1720 ) 1721 with ScopedEnvVar(HYF_PATHS_PROFILE_ENV, "repo_local"): 1722 with ScopedEnvVar(HYF_PATHS_REPO_LOCAL_ROOT_ENV, temp_dir): 1723 var response = run_stdio_entrypoint( 1724 "src/main.mojo", 1725 '{"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"}}', 1726 "--config", 1727 startup_config_path.__fspath__(), 1728 ) 1729 1730 assert_true(response["ok"].bool_value()) 1731 assert_equal( 1732 response["meta"]["execution_mode"].string_value(), 1733 "assisted", 1734 ) 1735 assert_equal( 1736 response["meta"]["backend"].string_value(), 1737 "provider_runtime", 1738 ) 1739 assert_equal( 1740 response["meta"]["provider"].string_value(), 1741 "max_local", 1742 ) 1743 assert_equal( 1744 response["meta"]["route"].string_value(), 1745 "provider_runtime.query_rewrite.max_local", 1746 ) 1747 assert_equal( 1748 response["meta"]["model"].string_value(), 1749 "max-local-query-rewrite", 1750 ) 1751 assert_true( 1752 Int(response["meta"]["latency_ms"].int_value()) >= 0 1753 ) 1754 assert_equal( 1755 Int(response["meta"]["schema_version"].int_value()), 1 1756 ) 1757 assert_equal( 1758 response["meta"]["prompt_version"].string_value(), 1759 "max_local_query_rewrite_v1", 1760 ) 1761 assert_equal( 1762 response["meta"]["provenance"]["kind"].string_value(), 1763 "assisted", 1764 ) 1765 assert_true( 1766 response["meta"]["provenance"]["fallback"].is_null() 1767 ) 1768 _assert_no_top_level_fallback_meta(response) 1769 assert_equal( 1770 response["output"]["rewritten_text"].string_value(), 1771 "apples pickup weekend", 1772 ) 1773 assert_equal( 1774 response["output"]["query_terms"][0].string_value(), 1775 "apples", 1776 ) 1777 assert_equal( 1778 response["output"]["query_terms"][1].string_value(), 1779 "pickup", 1780 ) 1781 assert_equal( 1782 response["output"]["query_terms"][2].string_value(), 1783 "weekend", 1784 ) 1785 1786 provider_stub.wait() 1787 1788 1789 def test_query_rewrite_falls_back_on_provider_non_2xx() raises: 1790 _assert_query_rewrite_provider_fallback( 1791 "query_rewrite_non_2xx", "provider_non_2xx", 15000 1792 ) 1793 1794 1795 def test_query_rewrite_falls_back_on_provider_timeout() raises: 1796 _assert_query_rewrite_provider_fallback( 1797 "query_rewrite_timeout", "timeout", 100 1798 ) 1799 1800 1801 def test_query_rewrite_falls_back_on_unknown_transport_as_provider_error() raises: 1802 _assert_query_rewrite_provider_fallback( 1803 "query_rewrite_malformed_http", "provider_error", 15000 1804 ) 1805 1806 1807 def test_query_rewrite_falls_back_when_provider_readiness_probe_times_out() raises: 1808 _assert_query_rewrite_provider_fallback_with_requests( 1809 "health_timeout", "timeout", 100, 1 1810 ) 1811 1812 1813 def test_query_rewrite_falls_back_on_health_non_2xx_with_business_reason() raises: 1814 _assert_query_rewrite_provider_fallback_with_requests( 1815 "health_non_2xx", "provider_non_2xx", 15000, 1 1816 ) 1817 1818 1819 def test_query_rewrite_completion_uses_remaining_deadline_after_readiness() raises: 1820 _assert_query_rewrite_provider_fallback_with_deadline( 1821 "query_rewrite_remaining_deadline_timeout", 1822 "timeout", 1823 1000, 1824 500, 1825 2, 1826 ) 1827 1828 1829 def test_query_rewrite_falls_back_on_provider_invalid_json() raises: 1830 _assert_query_rewrite_provider_fallback( 1831 "query_rewrite_invalid_json", "provider_invalid_json", 15000 1832 ) 1833 1834 1835 def test_query_rewrite_falls_back_on_provider_schema_invalid_json() raises: 1836 _assert_query_rewrite_provider_fallback( 1837 "query_rewrite_schema_invalid", "provider_schema_invalid", 15000 1838 ) 1839 1840 1841 def test_query_rewrite_falls_back_on_provider_top_level_string() raises: 1842 _assert_query_rewrite_provider_fallback( 1843 "query_rewrite_top_level_string", "provider_schema_invalid", 15000 1844 ) 1845 1846 1847 def test_query_rewrite_falls_back_on_provider_top_level_array() raises: 1848 _assert_query_rewrite_provider_fallback( 1849 "query_rewrite_top_level_array", "provider_schema_invalid", 15000 1850 ) 1851 1852 1853 def test_query_rewrite_falls_back_on_provider_top_level_null() raises: 1854 _assert_query_rewrite_provider_fallback( 1855 "query_rewrite_top_level_null", "provider_schema_invalid", 15000 1856 ) 1857 1858 1859 def test_query_rewrite_falls_back_on_provider_empty_choices() raises: 1860 _assert_query_rewrite_provider_fallback( 1861 "query_rewrite_empty_choices", "provider_empty_choices", 15000 1862 ) 1863 1864 1865 def test_query_rewrite_falls_back_on_provider_missing_content() raises: 1866 _assert_query_rewrite_provider_fallback( 1867 "query_rewrite_missing_content", "provider_missing_content", 15000 1868 ) 1869 1870 1871 def test_query_rewrite_falls_back_on_provider_error_payload() raises: 1872 _assert_query_rewrite_provider_fallback( 1873 "query_rewrite_error_payload", "provider_error_payload", 15000 1874 ) 1875 1876 1877 def test_status_reports_configured_but_deferred_custody_truthfully() raises: 1878 with TemporaryDirectory() as temp_dir: 1879 var identity_dir = Path(temp_dir) / "secrets" / "services" / "hyf" 1880 _ = std.os.makedirs(identity_dir.__fspath__(), exist_ok=True) 1881 (identity_dir / "identity.secret.json").write_text( 1882 "{\"configured\":\"test-only-placeholder\"}" 1883 ) 1884 1885 var protected_dir = ( 1886 Path(temp_dir) / "data" / "services" / "hyf" / "protected" 1887 ) 1888 _ = std.os.makedirs(protected_dir.__fspath__(), exist_ok=True) 1889 1890 with ScopedEnvVar(HYF_PATHS_PROFILE_ENV, "repo_local"): 1891 with ScopedEnvVar(HYF_PATHS_REPO_LOCAL_ROOT_ENV, temp_dir): 1892 var response = run_stdio_entrypoint( 1893 "src/main.mojo", 1894 load_scenario_request_json("scenarios/status_ok.json"), 1895 ) 1896 1897 assert_equal( 1898 response["output"]["runtime"]["secret_storage"][ 1899 "status" 1900 ].string_value(), 1901 "reserved", 1902 ) 1903 assert_equal( 1904 response["output"]["runtime"]["secret_storage"][ 1905 "backend_implemented" 1906 ].bool_value(), 1907 False, 1908 ) 1909 assert_equal( 1910 response["output"]["runtime"]["secret_storage"][ 1911 "identity_material_configured" 1912 ].bool_value(), 1913 True, 1914 ) 1915 assert_equal( 1916 response["output"]["runtime"]["secret_storage"][ 1917 "identity_material_loaded" 1918 ].bool_value(), 1919 False, 1920 ) 1921 assert_equal( 1922 response["output"]["runtime"]["secret_storage"][ 1923 "identity_material_created_by_startup" 1924 ].bool_value(), 1925 False, 1926 ) 1927 1928 assert_equal( 1929 response["output"]["runtime"]["protected_local_data"][ 1930 "status" 1931 ].string_value(), 1932 "reserved", 1933 ) 1934 assert_equal( 1935 response["output"]["runtime"]["protected_local_data"][ 1936 "configured" 1937 ].bool_value(), 1938 True, 1939 ) 1940 assert_equal( 1941 response["output"]["runtime"]["protected_local_data"][ 1942 "support_implemented" 1943 ].bool_value(), 1944 False, 1945 ) 1946 assert_equal( 1947 response["output"]["runtime"]["protected_local_data"][ 1948 "store_open" 1949 ].bool_value(), 1950 False, 1951 ) 1952 1953 1954 def test_status_clears_repo_local_root_outside_repo_local_profile() raises: 1955 with TemporaryDirectory() as temp_dir: 1956 with ScopedEnvVar(HYF_PATHS_PROFILE_ENV, "interactive_user"): 1957 with ScopedEnvVar(HYF_PATHS_REPO_LOCAL_ROOT_ENV, temp_dir): 1958 var response = run_stdio_entrypoint( 1959 "src/main.mojo", 1960 load_scenario_request_json("scenarios/status_ok.json"), 1961 ) 1962 1963 assert_equal( 1964 response["output"]["runtime"][ 1965 "paths_profile" 1966 ].string_value(), 1967 "interactive_user", 1968 ) 1969 assert_equal( 1970 response["output"]["runtime"][ 1971 "repo_local_base_root" 1972 ].string_value(), 1973 "", 1974 ) 1975 assert_true( 1976 response["output"]["runtime"]["paths"][ 1977 "config_path" 1978 ].string_value().find(temp_dir) 1979 < 0 1980 ) 1981 1982 1983 def test_status_reports_effective_diagnostics_override_truthfully() raises: 1984 with TemporaryDirectory() as temp_dir: 1985 var diagnostics_override_dir = ( 1986 Path(temp_dir) / "debug-diagnostics-override" 1987 ) 1988 with ScopedEnvVar(HYF_PATHS_PROFILE_ENV, "repo_local"): 1989 with ScopedEnvVar(HYF_PATHS_REPO_LOCAL_ROOT_ENV, temp_dir): 1990 with ScopedEnvVar( 1991 _HYF_DIAGNOSTICS_DIR_ENV, 1992 diagnostics_override_dir.__fspath__(), 1993 ): 1994 var response = run_stdio_entrypoint( 1995 "src/main.mojo", 1996 load_scenario_request_json("scenarios/status_ok.json"), 1997 ) 1998 1999 assert_equal( 2000 response["output"]["runtime"]["paths"][ 2001 "diagnostics_dir" 2002 ].string_value(), 2003 temp_dir + "/logs/services/hyf/diagnostics", 2004 ) 2005 assert_equal( 2006 response["output"]["runtime"]["diagnostics"][ 2007 "canonical_dir" 2008 ].string_value(), 2009 temp_dir + "/logs/services/hyf/diagnostics", 2010 ) 2011 assert_equal( 2012 response["output"]["runtime"]["diagnostics"][ 2013 "effective_dir" 2014 ].string_value(), 2015 diagnostics_override_dir.__fspath__(), 2016 ) 2017 assert_equal( 2018 response["output"]["runtime"]["diagnostics"][ 2019 "debug_override_active" 2020 ].bool_value(), 2021 True, 2022 ) 2023 2024 2025 def test_capabilities_success() raises: 2026 var response = run_hyf_stdio( 2027 load_scenario_request_json("scenarios/capabilities_ok.json") 2028 ) 2029 assert_matches_scenario_response(response, "scenarios/capabilities_ok.json") 2030 2031 2032 def test_invalid_envelope_preserves_correlation() raises: 2033 var response = run_hyf_stdio(status_request_with_invalid_version_json()) 2034 2035 assert_equal(Int(response["version"].int_value()), 1) 2036 assert_equal(response["request_id"].string_value(), "status-fixture-1") 2037 assert_equal(response["trace_id"].string_value(), "trace-status-fixture-1") 2038 assert_true(not response["ok"].bool_value()) 2039 assert_equal(response["error"]["code"].string_value(), "invalid_request") 2040 2041 2042 def test_assisted_request_fails_explicitly() raises: 2043 var response = run_hyf_stdio( 2044 load_scenario_request_json( 2045 "scenarios/assisted_backend_unavailable.json" 2046 ) 2047 ) 2048 assert_matches_scenario_response( 2049 response, "scenarios/assisted_backend_unavailable.json" 2050 ) 2051 2052 2053 def test_deferred_capability_returns_disabled_error() raises: 2054 var response = run_hyf_stdio( 2055 load_scenario_request_json( 2056 "scenarios/deferred_capability_disabled.json" 2057 ) 2058 ) 2059 assert_matches_scenario_response( 2060 response, "scenarios/deferred_capability_disabled.json" 2061 ) 2062 2063 2064 def test_query_rewrite_success() raises: 2065 var response = run_hyf_stdio( 2066 load_scenario_request_json( 2067 "scenarios/query_rewrite_local_pickup_weekend.json" 2068 ) 2069 ) 2070 assert_matches_scenario_response( 2071 response, "scenarios/query_rewrite_local_pickup_weekend.json" 2072 ) 2073 _assert_no_top_level_fallback_meta(response) 2074 2075 2076 def test_query_rewrite_does_not_create_protected_local_artifacts() raises: 2077 with TemporaryDirectory() as temp_dir: 2078 with ScopedEnvVar(HYF_PATHS_PROFILE_ENV, "repo_local"): 2079 with ScopedEnvVar(HYF_PATHS_REPO_LOCAL_ROOT_ENV, temp_dir): 2080 var response = run_stdio_entrypoint( 2081 "src/main.mojo", 2082 load_scenario_request_json( 2083 "scenarios/query_rewrite_local_pickup_weekend.json" 2084 ), 2085 ) 2086 2087 assert_true(response["ok"].bool_value()) 2088 assert_true( 2089 not exists( 2090 Path(temp_dir) 2091 / "data" 2092 / "services" 2093 / "hyf" 2094 / "protected" 2095 ) 2096 ) 2097 assert_true( 2098 not exists( 2099 Path(temp_dir) 2100 / "cache" 2101 / "services" 2102 / "hyf" 2103 ) 2104 ) 2105 2106 2107 def test_semantic_rank_exports_heuristic_score_without_latency() raises: 2108 var response = run_hyf_stdio( 2109 load_scenario_request_json( 2110 "scenarios/semantic_rank_local_pickup_weekend.json" 2111 ) 2112 ) 2113 assert_matches_scenario_response( 2114 response, "scenarios/semantic_rank_local_pickup_weekend.json" 2115 ) 2116 2117 2118 def test_explain_result_success() raises: 2119 var response = run_hyf_stdio( 2120 load_scenario_request_json( 2121 "scenarios/explain_result_local_pickup_weekend.json" 2122 ) 2123 ) 2124 assert_matches_scenario_response( 2125 response, "scenarios/explain_result_local_pickup_weekend.json" 2126 ) 2127 2128 2129 def test_strict_query_rewrite_failure() raises: 2130 var response = run_hyf_stdio( 2131 load_scenario_request_json( 2132 "scenarios/query_rewrite_unexpected_field.json" 2133 ) 2134 ) 2135 assert_matches_scenario_response( 2136 response, "scenarios/query_rewrite_unexpected_field.json" 2137 ) 2138 2139 2140 def test_strict_semantic_rank_failure() raises: 2141 var response = run_hyf_stdio( 2142 '{"version":1,"request_id":"rank-bad-proc-1","capability":"semantic_rank","input":{"query":"eggs' 2143 ' near me","candidates":[{"id":"lst_7ak2","title":"Pasture' 2144 ' eggs","farm":"La Huerta del' 2145 ' Sur","delivery":"pickup","distance_km":3.2,"freshness_minutes":2,"rating":5}]}}' 2146 ) 2147 2148 assert_true(not response["ok"].bool_value()) 2149 assert_equal(response["error"]["code"].string_value(), "invalid_request") 2150 assert_true( 2151 response["error"]["message"].string_value().find("unexpected field") 2152 >= 0 2153 ) 2154 2155 2156 def test_duplicate_candidate_ids_fail_explicitly() raises: 2157 var response = run_hyf_stdio( 2158 '{"version":1,"request_id":"rank-dup-proc-1","capability":"semantic_rank","input":{"query":"eggs' 2159 ' near me","candidates":[{"id":"lst_dup","title":"Pasture' 2160 ' eggs","farm":"La Huerta del' 2161 ' Sur","delivery":"pickup","distance_km":3.2,"freshness_minutes":2},{"id":"lst_dup","title":"Free' 2162 ' range eggs","farm":"Santa' 2163 ' Elena","delivery":"delivery","distance_km":8.7,"freshness_minutes":18}]}}' 2164 ) 2165 2166 assert_true(not response["ok"].bool_value()) 2167 assert_equal(response["error"]["code"].string_value(), "invalid_request") 2168 assert_true( 2169 response["error"]["message"] 2170 .string_value() 2171 .find("duplicate candidate id") 2172 >= 0 2173 ) 2174 2175 2176 def test_missing_input_fails_explicitly() raises: 2177 var response = run_hyf_stdio( 2178 '{"version":1,"request_id":"missing-input-proc-1","capability":"query_rewrite"}' 2179 ) 2180 2181 assert_true(not response["ok"].bool_value()) 2182 assert_equal(response["error"]["code"].string_value(), "invalid_request") 2183 assert_true( 2184 response["error"]["message"] 2185 .string_value() 2186 .find("field 'input' is required") 2187 >= 0 2188 ) 2189 2190 2191 def test_internal_error_is_bounded_on_wire() raises: 2192 with TemporaryDirectory() as temp_dir: 2193 with ScopedEnvVar(HYF_PATHS_PROFILE_ENV, "repo_local"): 2194 with ScopedEnvVar(HYF_PATHS_REPO_LOCAL_ROOT_ENV, temp_dir): 2195 var response = run_stdio_entrypoint( 2196 "tests/internal_error_stdio_main.mojo", 2197 '{"version":1,"request_id":"status-internal-proc-1","trace_id":"trace-status-internal-proc-1","capability":"sys.status","input":{}}', 2198 ) 2199 2200 assert_equal(Int(response["version"].int_value()), 1) 2201 assert_equal( 2202 response["request_id"].string_value(), 2203 "status-internal-proc-1", 2204 ) 2205 assert_equal( 2206 response["trace_id"].string_value(), 2207 "trace-status-internal-proc-1", 2208 ) 2209 assert_true(not response["ok"].bool_value()) 2210 assert_equal( 2211 response["error"]["code"].string_value(), "internal_error" 2212 ) 2213 assert_equal( 2214 response["error"]["message"].string_value(), 2215 _EXPECTED_INTERNAL_ERROR_MESSAGE, 2216 ) 2217 assert_true( 2218 response["error"]["message"] 2219 .string_value() 2220 .find("simulated test-only") 2221 < 0 2222 ) 2223 2224 2225 def test_internal_error_records_detail_in_canonical_runtime_diagnostics_dir() raises: 2226 with TemporaryDirectory() as temp_dir: 2227 var diagnostics_dir = ( 2228 Path(temp_dir) / "logs" / "services" / "hyf" / "diagnostics" 2229 ) 2230 with ScopedEnvVar(HYF_PATHS_PROFILE_ENV, "repo_local"): 2231 with ScopedEnvVar(HYF_PATHS_REPO_LOCAL_ROOT_ENV, temp_dir): 2232 var response = run_stdio_entrypoint( 2233 "tests/internal_error_stdio_main.mojo", 2234 '{"version":1,"request_id":"status-internal-proc-diag-1","trace_id":"trace-status-internal-proc-diag-1","capability":"sys.status","input":{}}', 2235 ) 2236 2237 assert_true(not response["ok"].bool_value()) 2238 assert_equal( 2239 response["error"]["code"].string_value(), 2240 "internal_error", 2241 ) 2242 assert_true(exists(diagnostics_dir)) 2243 2244 var entries = std.os.listdir(diagnostics_dir) 2245 assert_equal(len(entries), 1) 2246 assert_true(entries[0].startswith("hyf-internal-error-pid-")) 2247 2248 var content = (diagnostics_dir / entries[0]).read_text() 2249 assert_true( 2250 content.find('request_id="status-internal-proc-diag-1"') 2251 >= 0 2252 ) 2253 assert_true( 2254 content.find('trace_id="trace-status-internal-proc-diag-1"') 2255 >= 0 2256 ) 2257 assert_true( 2258 content.find( 2259 'detail="simulated test-only status builder failure"' 2260 ) 2261 >= 0 2262 ) 2263 assert_true( 2264 (diagnostics_dir / entries[0]) 2265 .__fspath__() 2266 .startswith(temp_dir + "/logs/services/hyf/diagnostics/") 2267 ) 2268 2269 2270 def main() raises: 2271 TestSuite.discover_tests[__functions_in_module()]().run()