Skip to content

Commit 1c637a3

Browse files
authored
remove-schema-trust-classifier (#579)
1 parent c928039 commit 1c637a3

2 files changed

Lines changed: 3 additions & 101 deletions

File tree

src/jacobian/schema_registry.py

Lines changed: 3 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -92,58 +92,6 @@ def _validated_schema(canonical_schema: bytes) -> Draft202012Validator:
9292
return Draft202012Validator(normalized, format_checker=FormatChecker())
9393

9494

95-
@lru_cache(maxsize=1024)
96-
def _trusted_validated_schema(canonical_schema: bytes) -> Draft202012Validator:
97-
"""Compile a schema without re-validating against the meta-schema.
98-
99-
Pydantic's model_json_schema() produces valid Draft 2020-12 schemas
100-
by construction. Skipping the expensive meta-schema walk for those
101-
schemas eliminates the dominant cost of runtime construction (~6.7s
102-
across 527 unique schemas at ~12.7ms each). External references are
103-
still rejected; only the meta-schema check is skipped.
104-
"""
105-
106-
normalized = loads_strict_json(canonical_schema)
107-
_reject_external_references(normalized)
108-
return Draft202012Validator(normalized, format_checker=FormatChecker())
109-
110-
111-
def _uses_operator_owned_pydantic_schema(model: type[BaseModel]) -> bool:
112-
"""Return whether Pydantic owns the complete model schema generation path."""
113-
114-
model_schema_method = getattr(model.model_json_schema, "__func__", None)
115-
base_schema_method = getattr(BaseModel.model_json_schema, "__func__", None)
116-
model_schema_hook = getattr(model.__get_pydantic_json_schema__, "__func__", None)
117-
base_schema_hook = getattr(BaseModel.__get_pydantic_json_schema__, "__func__", None)
118-
return (
119-
model.__module__.startswith("jacobian.")
120-
and model_schema_method is base_schema_method
121-
and model_schema_hook is base_schema_hook
122-
and model.model_config.get("json_schema_extra") is None
123-
and _core_schema_uses_only_pydantic_defaults(model.__pydantic_core_schema__)
124-
)
125-
126-
127-
def _core_schema_uses_only_pydantic_defaults(value: Any) -> bool:
128-
if isinstance(value, list | tuple):
129-
return all(_core_schema_uses_only_pydantic_defaults(item) for item in value)
130-
if not isinstance(value, dict):
131-
return True
132-
if value.get("pydantic_js_extra") is not None or value.get(
133-
"pydantic_js_annotation_functions"
134-
):
135-
return False
136-
base_schema_hook = getattr(BaseModel.__get_pydantic_json_schema__, "__func__", None)
137-
for hook in value.get("pydantic_js_functions", ()):
138-
function = getattr(hook, "__func__", hook)
139-
module = getattr(function, "__module__", "")
140-
if function is not base_schema_hook and not module.startswith("pydantic."):
141-
return False
142-
return all(
143-
_core_schema_uses_only_pydantic_defaults(item) for item in value.values()
144-
)
145-
146-
14795
class SchemaRegistry:
14896
"""Store and apply closed local JSON Schemas used by artifact contracts."""
14997

@@ -164,23 +112,6 @@ def register(
164112
) -> str:
165113
"""Register a schema after rejecting unsupported external references."""
166114

167-
return self._register(
168-
name=name,
169-
version=version,
170-
schema=schema,
171-
skip_meta_validation=False,
172-
)
173-
174-
def _register(
175-
self,
176-
*,
177-
name: str,
178-
version: str,
179-
schema: dict[str, Any],
180-
skip_meta_validation: bool,
181-
) -> str:
182-
"""Register one schema under an internally established trust policy."""
183-
184115
self._reconcile_pending()
185116
canonical_schema = canonicalize_json(schema)
186117
registration = (name, version, canonical_schema)
@@ -207,10 +138,7 @@ def _register(
207138
version=version,
208139
definition=schema,
209140
)
210-
if skip_meta_validation:
211-
_trusted_validated_schema(canonical_schema)
212-
else:
213-
_validated_schema(canonical_schema)
141+
_validated_schema(canonical_schema)
214142
schema_uri = self.store.register_descriptor(
215143
kind="schema",
216144
name=name,
@@ -242,12 +170,10 @@ def register_model(
242170
repeats this registration after every restart before accepting writes.
243171
"""
244172

245-
schema = model_schema(model)
246-
schema_uri = self._register(
173+
schema_uri = self.register(
247174
name=name,
248175
version=version,
249-
schema=schema,
250-
skip_meta_validation=_uses_operator_owned_pydantic_schema(model),
176+
schema=model_schema(model),
251177
)
252178
self._bind_model_contract(schema_uri, model)
253179
if producer_only:

tests/component/schemas/test_schema_registry.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import pytest
77
from pydantic import BaseModel, ConfigDict, Field, WithJsonSchema, model_validator
88

9-
import jacobian.schema_registry as schema_registry
10-
from jacobian.contracts.results import ResultEnvelope
119
from jacobian.schema_registry import (
1210
SchemaRegistry,
1311
SchemaRegistryError,
@@ -121,28 +119,6 @@ def test_customized_model_schema_is_validated_before_persistence(
121119
store.get_descriptor(uri, expected_kind="schema")
122120

123121

124-
def test_operator_owned_default_model_skips_redundant_meta_validation(
125-
tmp_path: Path,
126-
monkeypatch: pytest.MonkeyPatch,
127-
) -> None:
128-
def unexpected_meta_validation(_canonical_schema: bytes) -> None:
129-
pytest.fail("default operator-owned Pydantic schema was revalidated")
130-
131-
monkeypatch.setattr(
132-
schema_registry,
133-
"_validated_schema",
134-
unexpected_meta_validation,
135-
)
136-
137-
uri = SchemaRegistry(ArtifactRepository(tmp_path)).register_model(
138-
name="operator-owned-result-envelope",
139-
version="1",
140-
model=ResultEnvelope,
141-
)
142-
143-
assert uri.startswith("artifact://sha256/")
144-
145-
146122
def test_schema_validator_cache_is_bound_to_canonical_schema(
147123
tmp_path: Path,
148124
) -> None:

0 commit comments

Comments
 (0)