askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
test_ai_strict_tools.py · 6.0 KB · 131 lines
| 1 | """Tools whose schema can be closed are sent ``strict``; the rest are not. |
| 2 | |
| 3 | Strict tool use needs every object in the input schema to list its |
| 4 | properties, set additionalProperties: false and carry a required list. |
| 5 | Several MCP tools take a free-form object (a record, a change set, a |
| 6 | filter, a payload); closing those would refuse the fields the tool exists |
| 7 | to carry, so they stay non-strict, by name, below. |
| 8 | """ |
| 9 | |
| 10 | import json |
| 11 | |
| 12 | import object_ai |
| 13 | import object_mcp |
| 14 | |
| 15 | NON_STRICT = {"execute_object", "list_records", "create_record", "update_record", |
| 16 | "update_schema"} |
| 17 | |
| 18 | |
| 19 | def _all_tools(): |
| 20 | names = [tool["name"] for tool in object_mcp.TOOLS] |
| 21 | return object_ai.mcp_tools_as_provider_tools(names, object_mcp.TOOLS, service="anthropic") |
| 22 | |
| 23 | |
| 24 | def _walk_objects(schema): |
| 25 | if schema.get("type") == "object": |
| 26 | yield schema |
| 27 | for prop in schema.get("properties", {}).values(): |
| 28 | yield from _walk_objects(prop) |
| 29 | if schema.get("type") == "array": |
| 30 | yield from _walk_objects(schema["items"]) |
| 31 | |
| 32 | |
| 33 | def test_exactly_the_free_form_tools_stay_non_strict(): |
| 34 | tools = object_ai.with_strict_tools(_all_tools(), limit=None) |
| 35 | assert {t["name"] for t in tools if not t.get("strict")} == NON_STRICT |
| 36 | assert {t["name"] for t in tools if t.get("strict")} >= { |
| 37 | "global_search", "get_record", "create_object", "update_object_source", |
| 38 | "read_page", "list_collections", "delete_record"} |
| 39 | |
| 40 | |
| 41 | def test_every_strict_schema_is_closed_at_every_level(): |
| 42 | for tool in object_ai.with_strict_tools(_all_tools(), limit=None): |
| 43 | if not tool.get("strict"): |
| 44 | continue |
| 45 | assert tool["strict"] is True |
| 46 | for obj in _walk_objects(tool["input_schema"]): |
| 47 | assert obj["additionalProperties"] is False, tool["name"] |
| 48 | assert isinstance(obj["required"], list), tool["name"] |
| 49 | assert isinstance(obj["properties"], dict), tool["name"] |
| 50 | assert '"default":' not in json.dumps(tool["input_schema"]), tool["name"] |
| 51 | |
| 52 | |
| 53 | def test_closing_keeps_required_fields_and_moves_defaults_into_descriptions(): |
| 54 | [tool] = object_ai.with_strict_tools( |
| 55 | object_ai.mcp_tools_as_provider_tools(["global_search"], object_mcp.TOOLS, |
| 56 | service="anthropic")) |
| 57 | schema = tool["input_schema"] |
| 58 | assert schema["required"] == ["query"] |
| 59 | assert schema["properties"]["limit"]["description"].endswith("Default: 10.") |
| 60 | assert schema["properties"]["collections"]["items"] == {"type": "string"} |
| 61 | |
| 62 | |
| 63 | def test_the_catalog_itself_is_never_mutated(): |
| 64 | before = json.dumps(object_mcp.TOOLS, sort_keys=True) |
| 65 | object_ai.with_strict_tools(_all_tools(), limit=None) |
| 66 | assert json.dumps(object_mcp.TOOLS, sort_keys=True) == before |
| 67 | |
| 68 | |
| 69 | def test_strict_input_schema_refuses_what_it_cannot_close(): |
| 70 | assert object_ai.strict_input_schema({"type": "object"}) is None |
| 71 | assert object_ai.strict_input_schema( |
| 72 | {"type": "object", "properties": {"x": {"type": "object"}}}) is None |
| 73 | assert object_ai.strict_input_schema( |
| 74 | {"type": "object", "properties": {}, "additionalProperties": True}) is None |
| 75 | assert object_ai.strict_input_schema( |
| 76 | {"type": "object", "properties": {"x": {"type": "string", "pattern": "^a"}}}) is None |
| 77 | assert object_ai.strict_input_schema( |
| 78 | {"type": "object", "properties": {}, "required": ["ghost"]}) is None |
| 79 | assert object_ai.strict_input_schema( |
| 80 | {"type": "object", "properties": {"x": {"anyOf": []}}}) is None |
| 81 | assert object_ai.strict_input_schema({"type": "object", "properties": {}}) == { |
| 82 | "type": "object", "properties": {}, "required": [], "additionalProperties": False} |
| 83 | |
| 84 | |
| 85 | def _sent_tools(model, service="anthropic"): |
| 86 | bodies = [] |
| 87 | |
| 88 | def send_http(url, headers, body): |
| 89 | bodies.append(json.loads(body)) |
| 90 | if service == "anthropic": |
| 91 | return 200, json.dumps({"content": [{"type": "text", "text": "ok"}], |
| 92 | "stop_reason": "end_turn", "usage": {}}).encode() |
| 93 | return 200, json.dumps({"choices": [{"message": {"content": "ok"}}]}).encode() |
| 94 | |
| 95 | tools = object_ai.mcp_tools_as_provider_tools( |
| 96 | ["global_search", "create_record"], object_mcp.TOOLS, service=service) |
| 97 | object_ai.run_chat(send_http=send_http, dispatch_tool=lambda n, a: {}, service=service, |
| 98 | model=model, key="sk-test", message="hi", tools=tools) |
| 99 | return bodies[0]["tools"] |
| 100 | |
| 101 | |
| 102 | def test_run_chat_sends_strict_only_where_valid_and_only_to_current_claude(): |
| 103 | sent = {t["name"]: t for t in _sent_tools("claude-haiku-5-5")} |
| 104 | assert sent["global_search"]["strict"] is True |
| 105 | assert "strict" not in sent["create_record"] |
| 106 | # The last tool still carries the cache breakpoint alongside. |
| 107 | assert sent["create_record"]["cache_control"] == {"type": "ephemeral"} |
| 108 | |
| 109 | assert all("strict" not in t for t in _sent_tools("claude-haiku-4-5")) |
| 110 | openai = _sent_tools("gpt-5-mini", service="openai") |
| 111 | assert all("strict" not in t and "strict" not in t["function"] for t in openai) |
| 112 | |
| 113 | |
| 114 | def test_no_more_strict_tools_than_the_provider_accepts(): |
| 115 | """A live call with every tool marked strict was refused: 'Too many |
| 116 | strict tools (25). The maximum ... is 20.' The default keeps to the cap |
| 117 | and marks the tools that change something first.""" |
| 118 | tools = object_ai.with_strict_tools(_all_tools()) |
| 119 | strict = [t["name"] for t in tools if t.get("strict")] |
| 120 | assert len(strict) == object_ai.MAX_STRICT_TOOLS == 20 |
| 121 | assert [t["name"] for t in tools] == [t["name"] for t in _all_tools()] |
| 122 | closable = [t["name"] for t in object_ai.with_strict_tools(_all_tools(), limit=None) if t.get("strict")] |
| 123 | writers = [n for n in closable if n.startswith(("create_", "update_", "delete_", "rollback_", "setup_", "start_", "stop_"))] |
| 124 | assert set(writers) <= set(strict) |
| 125 | assert len(closable) > 20 |
| 126 | |
| 127 | |
| 128 | def test_a_small_subset_is_all_strict_under_the_cap(): |
| 129 | names = ["global_search", "list_objects", "get_object_source", "create_object", "update_object_source"] |
| 130 | subset = [t for t in _all_tools() if t["name"] in names] |
| 131 | assert all(t.get("strict") for t in object_ai.with_strict_tools(subset)) |