"""Tests for the shared tool-schema module (scheduler/tools/schemas.py). This module is the single canonical source both worker.py's OpenAI-compatible loop and backends/copilot.py's Copilot SDK loop build their tool lists from (Session 2 dedup, docs/refactor-handoffs/02-dedup.md Task 2). These tests pin the filtering contract directly, independent of either agent loop. """ from afk_developer.scheduler.tools.schemas import ( TOOL_SCHEMAS, build_tool_schemas, to_openai_tool_definitions, tool_parameters, ) from afk_developer.scheduler.verification import VERIFICATION_TOOL_SCHEMA def _names(schemas) -> set[str]: return {schema[0] for schema in schemas} def test_tool_schema_names_are_unique() -> None: names = [schema[0] for schema in TOOL_SCHEMAS] assert len(names) == len(set(names)) def test_read_only_task_excludes_mutating_tools_but_keeps_verification() -> None: names = _names(build_tool_schemas(read_only=True, has_evidence_roots=False)) assert "write_file" not in names assert "edit_file" not in names assert "run_command" not in names assert "normalize_line_endings" not in names assert "run_verification" in names assert "read_file" in names def test_mutating_task_excludes_run_verification_but_keeps_write_tools() -> None: names = _names(build_tool_schemas(read_only=False, has_evidence_roots=False)) assert "run_verification" not in names assert "write_file" in names assert "edit_file" in names assert "run_command" in names assert "normalize_line_endings" in names def test_missing_evidence_roots_excludes_all_evidence_tools_regardless_of_read_only() -> None: for read_only in (True, False): names = _names(build_tool_schemas(read_only=read_only, has_evidence_roots=False)) assert "read_evidence_file" not in names assert "read_evidence_metadata" not in names assert "read_evidence_section" not in names assert "search_evidence_text" not in names def test_configured_evidence_roots_keep_all_evidence_tools() -> None: names = _names(build_tool_schemas(read_only=True, has_evidence_roots=True)) assert { "read_evidence_file", "read_evidence_metadata", "read_evidence_section", "search_evidence_text", } <= names def test_task_complete_and_task_blocked_are_never_filtered() -> None: for read_only in (True, False): for has_evidence_roots in (True, False): names = _names( build_tool_schemas(read_only=read_only, has_evidence_roots=has_evidence_roots) ) assert "task_complete" in names assert "task_blocked" in names def test_run_verification_parameters_use_the_full_schema_with_additional_properties() -> None: parameters = tool_parameters("run_verification", {}, []) assert parameters is VERIFICATION_TOOL_SCHEMA assert parameters["additionalProperties"] is False def test_strict_mode_schema_excludes_lint_baseline_compare_but_keeps_file_hashing() -> None: schemas = build_tool_schemas( read_only=True, has_evidence_roots=False, allow_baseline_compare=False, ) _, _, properties, required = next( schema for schema in schemas if schema[0] == "run_verification" ) parameters = tool_parameters("run_verification", properties, required) assert "baseline_compare" not in parameters["properties"]["kind"]["enum"] assert "file_sha256" in parameters["properties"]["kind"]["enum"] assert "baseline_compare" not in parameters["properties"]["kind"]["description"] assert "baseline_compare" in VERIFICATION_TOOL_SCHEMA["properties"]["kind"]["enum"] def test_openai_tool_definitions_use_the_expected_wire_shape() -> None: schemas = build_tool_schemas(read_only=False, has_evidence_roots=False) definitions = to_openai_tool_definitions(schemas) assert all(d["type"] == "function" for d in definitions) write_file_def = next(d for d in definitions if d["function"]["name"] == "write_file") assert write_file_def["function"]["parameters"]["type"] == "object" assert "path" in write_file_def["function"]["parameters"]["properties"] assert write_file_def["function"]["parameters"]["required"] == ["path", "content"]