Spaces:
Running
Running
File size: 9,020 Bytes
a9e27c0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | """Regressions for issue #1730 at the shared Claude conversion boundary."""
import re
import unicodedata
from copy import deepcopy
import pytest
from free_claude_code.core.anthropic.conversion import build_base_request_body
from free_claude_code.core.anthropic.models import MessagesRequest
from free_claude_code.core.openai_responses.provider_input import (
build_responses_provider_request,
)
from free_claude_code.core.reasoning import ReasoningPolicy
from free_claude_code.core.tool_schema_patterns import translate_tool_schema_patterns
ARTIFACT_PATTERN = r'^(?!__.*__$)[^\p{Cc}\p{Cf}\p{Zl}\p{Zp}"\\./[\]]{1,200}$'
@pytest.mark.parametrize("wire_format", ["chat", "responses"])
@pytest.mark.parametrize(
"pattern", [r"^[^\p{Cc}\p{Cf}\p{Zl}\p{Zp}]{1,200}$", ARTIFACT_PATTERN]
)
def test_claude_converters_preserve_artifact_rules_in_portable_pattern(
wire_format, pattern
):
schema = {
"type": "object",
"properties": {"name": {"type": "string", "pattern": pattern}},
"required": ["name"],
}
request = MessagesRequest.model_validate(
{
"model": "test-model",
"messages": [{"role": "user", "content": "Call Artifact with demo"}],
"tools": [
{"name": "Artifact", "description": "Save", "input_schema": schema}
],
}
)
original = deepcopy(request.model_dump())
if wire_format == "chat":
body = build_base_request_body(request)
parameters = body["tools"][0]["function"]["parameters"]
else:
body = build_responses_provider_request(
request, reasoning=ReasoningPolicy.provider_default()
)
parameters = body["tools"][0]["parameters"]
translated = parameters["properties"]["name"]["pattern"]
assert r"\p{" not in translated
regex = re.compile(translated)
assert regex.fullmatch("demo")
assert not regex.fullmatch("bad\u2028name")
assert not regex.fullmatch("bad\U000e0001name")
assert not regex.fullmatch("a" * 201)
assert not regex.fullmatch("")
assert parameters["required"] == ["name"]
assert request.model_dump() == original
def translated_pattern(pattern):
return translate_tool_schema_patterns({"pattern": pattern})["pattern"]
@pytest.mark.parametrize("wire_format", ["chat", "responses"])
@pytest.mark.parametrize(
"pattern",
[r"^[^\p{Cc}]{4294967296}$", "(" * 500 + r"[^\p{Cc}]" + ")" * 500],
ids=["repeat-overflow", "group-recursion"],
)
def test_compiler_limits_preserve_original_schema_in_both_converters(
wire_format, pattern
):
schema = {"type": "string", "pattern": pattern}
request = MessagesRequest.model_validate(
{
"model": "test-model",
"messages": [{"role": "user", "content": "Test schema passthrough"}],
"tools": [{"name": "Artifact", "input_schema": schema}],
}
)
original = deepcopy(request.model_dump())
if wire_format == "chat":
parameters = build_base_request_body(request)["tools"][0]["function"][
"parameters"
]
else:
body = build_responses_provider_request(
request, reasoning=ReasoningPolicy.provider_default()
)
parameters = body["tools"][0]["parameters"]
assert parameters == schema
assert request.model_dump() == original
@pytest.mark.parametrize("category", ["Cc", "Cf", "Zl", "Zp"])
def test_category_expansion_preserves_every_unicode_scalar(category):
regex = re.compile(translated_pattern(r"[^\p{" + category + "}]+"))
for point in range(0x110000):
if 0xD800 <= point <= 0xDFFF:
continue
char = chr(point)
assert bool(regex.fullmatch(char)) == (
unicodedata.category(char) != category
), f"U+{point:04X}, category {category}, Unicode {unicodedata.unidata_version}"
@pytest.mark.parametrize(
"name,accepted",
[
("demo", True),
("é名字😀", True),
("__name", True),
("name__", True),
("-name-", True),
("a", True),
("a" * 200, True),
("", False),
("a" * 201, False),
("__name__", False),
*[("x" + char + "y", False) for char in r'"\/.[]'],
*[
("x" + char + "y", False)
for char in [
"\x00",
"\x1f",
"\x7f",
"\x9f",
"\u200d",
"\u2028",
"\u2029",
"\U000110bd",
"\U000e0001",
"\U000e0020",
"\U000e007f",
]
],
],
)
def test_artifact_restrictions_survive_translation(name, accepted):
assert bool(re.fullmatch(translated_pattern(ARTIFACT_PATTERN), name)) == accepted
@pytest.mark.parametrize(
"pattern",
[
r"^[a-z]+$",
r"[^\\p{Cc}]",
r"\\p{Cc}",
r"[\p{Cc}]",
r"\p{Cc}",
r"[^\P{Cc}]",
r"[^\p{L}]",
r"[^\p{cc}]",
r"[^\p{Cc}\p{L}]",
r"[^\p{Cc}]\P{Cf}",
r"[^\p{Cc}]\p{Cf}",
r"[^\p{Cc}\p{General_Category=Format}]",
r"[^\p{Cc}\p{Cf]",
r"[^\p{Cc}",
r"[^\p{Cc}]" + "\\",
r"([^\p{Cc}]",
r"[^\p{Cc}-z]",
r"[^a-\p{Cc}]",
r"[^\p{Cc}--a]",
r"[^\p{Cc}&&a]",
r"[^\p{Cc}~~a]",
r"[^\p{Cc}||a]",
r"[^\p{Cc}[a]]",
r"\[^\p{Cc}]",
],
)
def test_unsupported_or_literal_patterns_remain_unchanged(pattern):
schema = {"type": "string", "pattern": pattern}
assert translate_tool_schema_patterns(schema) is schema
@pytest.mark.parametrize(
"pattern,excluded,accepted",
[
(r"[^\\\p{Cc}]", "\\\x00", "p{}C"),
(r"[^\p{Cc}\-]", "-\x00", "az"),
(r"[^\]\p{Cc}]", "]\x00", "["),
(r"[^\p{Cc}[\]]", "[]\x00", "az"),
(r"[^\p{Zl}][^\p{Zp}]", "\u2028x", "ab"),
],
)
def test_class_escaping_is_preserved(pattern, excluded, accepted):
translated = translated_pattern(pattern)
assert translated != pattern
regex = re.compile(translated)
if pattern == r"[^\p{Zl}][^\p{Zp}]":
assert regex.fullmatch(accepted)
assert not regex.fullmatch(excluded)
else:
assert all(not regex.fullmatch(char) for char in excluded)
assert all(regex.fullmatch(char) for char in accepted)
@pytest.mark.parametrize(
"container",
[
"$defs",
"definitions",
"properties",
"patternProperties",
"dependentSchemas",
"dependencies",
],
)
def test_schema_maps_visit_values_and_preserve_names(container):
child = {"type": "string", "pattern": r"[^\p{Cc}]"}
schema = {container: {"pattern": child, r"\p{Cc}": True, "required": ["name"]}}
original = deepcopy(schema)
converted = translate_tool_schema_patterns(schema)
assert converted[container]["pattern"]["pattern"] != child["pattern"]
assert converted[container][r"\p{Cc}"] is True
assert converted[container]["required"] == ["name"]
assert schema == original
@pytest.mark.parametrize(
"keyword",
[
"additionalProperties",
"additionalItems",
"unevaluatedProperties",
"unevaluatedItems",
"items",
"contains",
"propertyNames",
"if",
"then",
"else",
"not",
"contentSchema",
],
)
def test_single_schema_keywords_are_translated(keyword):
schema = {keyword: {"pattern": r"[^\p{Cf}]"}}
converted = translate_tool_schema_patterns(schema)
assert converted[keyword]["pattern"] != schema[keyword]["pattern"]
@pytest.mark.parametrize("keyword", ["allOf", "anyOf", "oneOf", "prefixItems", "items"])
def test_schema_arrays_preserve_boolean_and_unchanged_children(keyword):
child = {"type": "string", "pattern": r"[a-z]"}
schema = {keyword: [child, False, {"pattern": r"[^\p{Cf}]"}]}
converted = translate_tool_schema_patterns(schema)
assert converted[keyword][0] is child
assert converted[keyword][1] is False
assert converted[keyword][2]["pattern"] != r"[^\p{Cf}]"
def test_instance_data_is_untouched_and_translation_is_idempotent():
literal = {"pattern": r"[^\p{Cc}]"}
schema = {
"type": "object",
"properties": {"name": literal},
"default": literal,
"const": literal,
"enum": [literal],
"examples": [literal],
"x-custom": literal,
"description": r"\p{Cc}",
}
before = deepcopy(schema)
converted = translate_tool_schema_patterns(schema)
for key in ("default", "const", "enum", "examples", "x-custom", "description"):
assert converted[key] is schema[key]
assert schema == before
assert translate_tool_schema_patterns(converted) is converted
@pytest.mark.parametrize(
"schema", [True, False, {}, {"pattern": 123}, {"type": "string"}]
)
def test_non_patterns_remain_unchanged(schema):
assert translate_tool_schema_patterns(schema) is schema
|