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