Skip to content

Commit 9e1cd50

Browse files
authored
refactor(checkers): let declarations own provider runtimes (#1299)
* refactor(checkers): let declarations own provider runtimes Compose declaration-owned clean-process runtimes with the latest batched checker-identity path. Existing checker families retain the legacy registry; new declarations may carry one unassigned provider runtime, and the composition root batches identity material across the full declaration set before authorization. * fix(checkers): defer declaration runtime measurement Store passive provider-runtime factories on exact replay declarations and realize each runtime once, at installation. Importing domain declarations no longer identifies or hashes checker source, while the existing installer and authorization boundary keep the same resolved runtime contract. * refactor(checkers): keep direct runtime migration compatibility Retain the existing direct provider_runtime constructor as a temporary compatibility seam while making provider_runtime_factory lazy and cached. Reject dual ownership and preserve pre-authorization invariants for both forms. * fix(checkers): type the lazy runtime accessor Declare the compatibility accessor's dynamic return as Any so mypy accepts the dataclass field interception while the public provider_runtime field retains its precise declared type. * test: include check-all in the primary help contract * fix declaration-owned checker runtime compatibility * fix(checkers): satisfy static validation * fix(checkers): handle declaration-owned runtime installation * fix(checkers): require explicit optional provider omission * fix(checkers): omit unavailable optional FLINT replays
1 parent 66dbe47 commit 9e1cd50

9 files changed

Lines changed: 431 additions & 99 deletions

File tree

src/jacobian/checker_operations.py

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
from __future__ import annotations
44

55
from collections.abc import Callable
6-
from dataclasses import dataclass
6+
from dataclasses import dataclass, field
7+
from typing import Any
78

89
from jacobian.contracts.capabilities import CapabilityProviderRuntime
910
from jacobian.contracts.checkers import EvidenceKind
1011
from jacobian.contracts.results import ContractModel
1112

13+
ProviderRuntimeFactory = Callable[[], CapabilityProviderRuntime]
14+
1215
# Producer operation verb segments stripped when deriving a verifier capability
1316
# ID. Each producer capability ID contains exactly one of these segments; the
1417
# derived verifier ID removes it and appends ``.verify``.
@@ -107,11 +110,21 @@ class ExactReplayCheckerDeclaration:
107110
verification_title: str | None = None
108111
verification_description: str | None = None
109112
verification_tags: tuple[str, ...] = ()
110-
provider_runtime_factory: Callable[..., CapabilityProviderRuntime] | None = None
113+
provider_runtime: CapabilityProviderRuntime | None = field(
114+
default=None,
115+
repr=False,
116+
compare=False,
117+
)
118+
provider_runtime_factory: ProviderRuntimeFactory | None = field(
119+
default=None,
120+
repr=False,
121+
compare=False,
122+
)
123+
optional: bool = False
111124
supports_input: Callable[[object], bool] | None = None
112125

113126
def __post_init__(self) -> None:
114-
for field, value in {
127+
for field_name, value in {
115128
"capability_id": self.capability_id,
116129
"function": self.function,
117130
"format_id": self.format_id,
@@ -121,11 +134,18 @@ def __post_init__(self) -> None:
121134
}.items():
122135
if not value.strip():
123136
raise ValueError(
124-
f"exact replay checker declaration {field} must not be empty"
137+
f"exact replay checker declaration {field_name} must not be empty"
125138
)
126-
if self.provider_runtime_factory is None:
139+
runtime = object.__getattribute__(self, "provider_runtime")
140+
factory = object.__getattribute__(self, "provider_runtime_factory")
141+
if runtime is not None and factory is not None:
142+
raise ValueError(
143+
"declaration must provide either provider_runtime or "
144+
"provider_runtime_factory, not both"
145+
)
146+
if runtime is not None and runtime.checker_ids:
127147
raise ValueError(
128-
"exact replay checker declaration requires a provider runtime factory"
148+
"declaration-owned provider runtime must not pre-authorize checker IDs"
129149
)
130150
derived_id = derive_verification_capability_id(self.capability_id)
131151
explicit_text = (
@@ -173,6 +193,28 @@ def __post_init__(self) -> None:
173193
derive_verification_tags(self.capability_id),
174194
)
175195

196+
def __getattribute__(self, name: str) -> Any:
197+
if name != "provider_runtime":
198+
return object.__getattribute__(self, name)
199+
runtime = object.__getattribute__(self, "provider_runtime")
200+
if runtime is not None:
201+
return runtime
202+
factory = object.__getattribute__(self, "provider_runtime_factory")
203+
if factory is None:
204+
return None
205+
realized = factory()
206+
if not isinstance(realized, CapabilityProviderRuntime):
207+
raise TypeError(
208+
"declaration-owned provider runtime factory must return "
209+
"CapabilityProviderRuntime"
210+
)
211+
if realized.checker_ids:
212+
raise ValueError(
213+
"declaration-owned provider runtime must not pre-authorize checker IDs"
214+
)
215+
object.__setattr__(self, "provider_runtime", realized)
216+
return realized
217+
176218

177219
@dataclass(frozen=True, slots=True)
178220
class CheckerOperation:
@@ -199,9 +241,9 @@ def __post_init__(self) -> None:
199241
"format_version": self.format_version,
200242
"reason": self.reason,
201243
}
202-
for field, value in required_text.items():
244+
for field_name, value in required_text.items():
203245
if not value.strip():
204-
raise ValueError(f"checker operation {field} must not be empty")
246+
raise ValueError(f"checker operation {field_name} must not be empty")
205247
if not self.claim_schema_uris:
206248
raise ValueError("checker operation must declare a claim schema")
207249
if not self.semantics_uris:

src/jacobian/domains/matrix_lattice/checkers.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,48 +66,55 @@ def _hnf_runtime(*, checker_ids: tuple[str, ...] = ()) -> CapabilityProviderRunt
6666
"check_matrix_determinant",
6767
"matrix.determinant.flint-replay",
6868
provider_runtime_factory=_flint_exact_replay_runtime,
69+
optional=True,
6970
),
7071
ExactReplayCheckerDeclaration(
7172
"matrix.rank.compute",
7273
MatrixRankRequest,
7374
"check_matrix_rank",
7475
"matrix.rank.flint-replay",
7576
provider_runtime_factory=_flint_exact_replay_runtime,
77+
optional=True,
7678
),
7779
ExactReplayCheckerDeclaration(
7880
"matrix.multiply.compute",
7981
RationalMatrixProductRequest,
8082
"check_matrix_product",
8183
"matrix.product.flint-replay",
8284
provider_runtime_factory=_flint_exact_replay_runtime,
85+
optional=True,
8386
),
8487
ExactReplayCheckerDeclaration(
8588
"matrix.normal_form.rref.compute",
8689
RationalMatrixRequest,
8790
"check_matrix_rref",
8891
"matrix.rref.flint-replay",
8992
provider_runtime_factory=_flint_exact_replay_runtime,
93+
optional=True,
9094
),
9195
ExactReplayCheckerDeclaration(
9296
"matrix.nullspace.compute",
9397
RationalMatrixRequest,
9498
"check_matrix_nullspace",
9599
"matrix.nullspace.flint-replay",
96100
provider_runtime_factory=_flint_exact_replay_runtime,
101+
optional=True,
97102
),
98103
ExactReplayCheckerDeclaration(
99104
"matrix.characteristic_polynomial.compute",
100105
SquareRationalMatrixRequest,
101106
"check_matrix_characteristic_polynomial",
102107
"matrix.characteristic-polynomial.flint-replay",
103108
provider_runtime_factory=_flint_exact_replay_runtime,
109+
optional=True,
104110
),
105111
ExactReplayCheckerDeclaration(
106112
"matrix.normal_form.smith.compute",
107113
IntegerMatrixRequest,
108114
"check_matrix_smith_normal_form",
109115
"matrix.smith-normal-form.flint-replay",
110116
provider_runtime_factory=_flint_exact_replay_runtime,
117+
optional=True,
111118
),
112119
)
113120

src/jacobian/domains/number_theory/checkers.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def _integer_lcm_runtime(
6262
"integer.prime-factorization.flint-replay",
6363
entrypoint_module=_EXACT_DOMAIN_ENTRYPOINT,
6464
provider_runtime_factory=_flint_exact_replay_runtime,
65+
optional=True,
6566
replay_method="Python-FLINT prime-factorization replay",
6667
reason=(
6768
"operator-authorized Python-FLINT checker independent of the "
@@ -88,6 +89,7 @@ def _integer_lcm_runtime(
8889
"integer.powerful.flint-replay",
8990
entrypoint_module=_EXACT_DOMAIN_ENTRYPOINT,
9091
provider_runtime_factory=_flint_exact_replay_runtime,
92+
optional=True,
9193
replay_method="Python-FLINT powerful-number replay",
9294
reason=(
9395
"operator-authorized Python-FLINT checker independent of the "
@@ -115,6 +117,7 @@ def _integer_lcm_runtime(
115117
"modular.polynomial-residue-image.flint-replay",
116118
entrypoint_module=_EXACT_DOMAIN_ENTRYPOINT,
117119
provider_runtime_factory=_flint_exact_replay_runtime,
120+
optional=True,
118121
replay_method="Python-FLINT exhaustive modular-polynomial replay",
119122
reason=(
120123
"operator-authorized Python-FLINT checker independently reconstructs "

src/jacobian/domains/polynomial/checkers.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ def _materialized_syzygy_supports(payload: object) -> bool:
227227
"check_polynomial_gcd",
228228
"polynomial.gcd.flint-replay",
229229
provider_runtime_factory=_flint_exact_replay_runtime,
230+
optional=True,
230231
supports_input=_univariate_polynomial("left", "right"),
231232
),
232233
ExactReplayCheckerDeclaration(
@@ -235,6 +236,7 @@ def _materialized_syzygy_supports(payload: object) -> bool:
235236
"check_polynomial_resultant",
236237
"polynomial.resultant.flint-replay",
237238
provider_runtime_factory=_flint_exact_replay_runtime,
239+
optional=True,
238240
supports_input=_univariate_polynomial("left", "right"),
239241
),
240242
ExactReplayCheckerDeclaration(
@@ -243,6 +245,7 @@ def _materialized_syzygy_supports(payload: object) -> bool:
243245
"check_polynomial_discriminant",
244246
"polynomial.discriminant.flint-replay",
245247
provider_runtime_factory=_flint_exact_replay_runtime,
248+
optional=True,
246249
supports_input=_univariate_polynomial("polynomial"),
247250
),
248251
ExactReplayCheckerDeclaration(
@@ -251,6 +254,7 @@ def _materialized_syzygy_supports(payload: object) -> bool:
251254
"check_polynomial_square_free",
252255
"polynomial.square-free.flint-replay",
253256
provider_runtime_factory=_flint_exact_replay_runtime,
257+
optional=True,
254258
supports_input=_univariate_polynomial("polynomial"),
255259
),
256260
ExactReplayCheckerDeclaration(
@@ -259,6 +263,7 @@ def _materialized_syzygy_supports(payload: object) -> bool:
259263
"check_polynomial_factorization",
260264
"polynomial.factorization.flint-replay",
261265
provider_runtime_factory=_flint_exact_replay_runtime,
266+
optional=True,
262267
supports_input=_univariate_polynomial("polynomial"),
263268
),
264269
)

src/jacobian/exact_domain_checkers.py

Lines changed: 32 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from __future__ import annotations
44

55
import logging
6-
from collections.abc import Callable, Mapping
6+
from collections.abc import Mapping
77
from dataclasses import dataclass
88
from typing import Any, Literal
99

@@ -59,7 +59,6 @@
5959
from jacobian.verification.service import VerificationService
6060

6161
_LOGGER = logging.getLogger(__name__)
62-
_OPTIONAL_EXACT_REPLAY_PROVIDERS = frozenset({"jacobian.exact-domain-checkers"})
6362

6463

6564
@dataclass(frozen=True, slots=True)
@@ -86,60 +85,45 @@ class _InstalledDeclaration:
8685
@dataclass(frozen=True, slots=True)
8786
class _DeclaredRuntimeGroup:
8887
probe: CapabilityProviderRuntime
89-
factory: Callable[..., CapabilityProviderRuntime]
9088
members: tuple[tuple[InstalledDomainBundle, ExactReplayCheckerDeclaration], ...]
91-
factories: tuple[Callable[..., CapabilityProviderRuntime], ...]
92-
93-
94-
def _declaration_factory(
95-
declaration: ExactReplayCheckerDeclaration,
96-
) -> Callable[..., CapabilityProviderRuntime]:
97-
factory = declaration.provider_runtime_factory
98-
if factory is None:
99-
raise ValueError(
100-
"exact replay checker declaration requires a provider runtime factory"
101-
)
102-
return factory
10389

10490

10591
def _declared_runtime_groups(
10692
pairs: tuple[tuple[InstalledDomainBundle, ExactReplayCheckerDeclaration], ...],
10793
) -> tuple[_DeclaredRuntimeGroup, ...]:
108-
probes: dict[
109-
Callable[..., CapabilityProviderRuntime], CapabilityProviderRuntime
110-
] = {}
11194
grouped: dict[
11295
str,
11396
tuple[
11497
CapabilityProviderRuntime,
115-
list[Callable[..., CapabilityProviderRuntime]],
11698
list[tuple[InstalledDomainBundle, ExactReplayCheckerDeclaration]],
11799
],
118100
] = {}
119101
for installed, declaration in pairs:
120-
factory = _declaration_factory(declaration)
121-
probe = probes.setdefault(factory, factory())
102+
factory = object.__getattribute__(declaration, "provider_runtime_factory")
103+
probe = factory() if factory is not None else declaration.provider_runtime
104+
if probe is None:
105+
continue
106+
if not isinstance(probe, CapabilityProviderRuntime):
107+
raise TypeError(
108+
"provider runtime factory must return CapabilityProviderRuntime"
109+
)
122110
current = grouped.get(probe.provider)
123111
if current is None:
124-
grouped[probe.provider] = (probe, [factory], [(installed, declaration)])
112+
grouped[probe.provider] = (probe, [(installed, declaration)])
125113
continue
126-
existing_probe, factories, members = current
127-
if existing_probe != probe:
114+
existing_probe, members = current
115+
if existing_probe.model_dump(mode="json") != probe.model_dump(mode="json"):
128116
raise ValueError(
129117
"exact replay grouped distinct probes under one provider "
130118
f"identity: {probe.provider}"
131119
)
132-
if factory not in factories:
133-
factories.append(factory)
134120
members.append((installed, declaration))
135121
return tuple(
136122
_DeclaredRuntimeGroup(
137123
probe=probe,
138-
factory=factories[0],
139124
members=tuple(members),
140-
factories=tuple(factories),
141125
)
142-
for probe, factories, members in grouped.values()
126+
for probe, members in grouped.values()
143127
)
144128

145129

@@ -150,15 +134,13 @@ def _authorize_replay_operation(
150134
authorize: bool,
151135
provider_runtime: CapabilityProviderRuntime,
152136
source_available: bool,
137+
optional: bool,
153138
capability_id: str,
154139
diagnostics: list[CapabilityDiagnostic],
155140
) -> str | None:
156141
if provider_runtime.availability is CapabilityProviderAvailability.AVAILABLE:
157142
return installer.install(operation, authorize=authorize).checker_id
158-
can_omit = (
159-
provider_runtime.provider in _OPTIONAL_EXACT_REPLAY_PROVIDERS
160-
and source_available
161-
)
143+
can_omit = optional and source_available
162144
if not can_omit:
163145
return installer.install(operation, authorize=authorize).checker_id
164146
diagnostic = CapabilityDiagnostic(
@@ -191,18 +173,11 @@ def _authorized_provider_runtimes(
191173
for _installed, declaration in group.members
192174
if (checker_id := checker_ids[declaration.capability_id]) is not None
193175
)
194-
runtime = group.factory(checker_ids=authorized)
195-
for factory in group.factories:
196-
if factory is group.factory:
197-
continue
198-
other = factory(checker_ids=authorized)
199-
if other != runtime:
200-
raise ValueError(
201-
"exact replay grouped distinct runtimes under one provider "
202-
f"identity: {group.probe.provider}"
203-
)
176+
runtime = group.probe.model_copy(update={"checker_ids": authorized})
204177
existing = provider_runtimes.get(runtime.provider)
205-
if existing is not None and existing != runtime:
178+
if existing is not None and existing.model_dump(
179+
mode="json"
180+
) != runtime.model_dump(mode="json"):
206181
raise ValueError(
207182
"exact replay grouped distinct runtimes under one provider "
208183
f"identity: {runtime.provider}"
@@ -225,15 +200,26 @@ def install_exact_domain_checkers(
225200
"""Install independent exact replay against dynamically registered schemas."""
226201

227202
installer = CheckerInstaller(checkers)
228-
groups = _declared_runtime_groups(_available_declaration_bundles(bundles))
203+
available_declarations = _available_declaration_bundles(bundles)
229204
checker_ids: dict[str, str | None] = {}
230205
declaration_providers: dict[str, str] = {}
231206
diagnostics: list[CapabilityDiagnostic] = []
207+
checker_ids.update(
208+
(declaration.capability_id, None)
209+
for _installed, declaration in available_declarations
210+
)
211+
if not authorize and not installer.bind_existing:
212+
return ExactDomainCheckerInstallation(
213+
checker_ids=checker_ids,
214+
provider_runtimes={},
215+
declaration_providers={},
216+
)
232217
source_available = (
233218
exact_domain_checker_source_provider_runtime().availability
234219
is CapabilityProviderAvailability.AVAILABLE
235220
)
236221
with batch_checker_manifest_measurement():
222+
groups = _declared_runtime_groups(available_declarations)
237223
for group in groups:
238224
for installed, declaration in group.members:
239225
declaration_providers[declaration.capability_id] = group.probe.provider
@@ -264,6 +250,7 @@ def install_exact_domain_checkers(
264250
authorize=authorize,
265251
provider_runtime=group.probe,
266252
source_available=source_available,
253+
optional=declaration.optional,
267254
capability_id=declaration.capability_id,
268255
diagnostics=diagnostics,
269256
)

0 commit comments

Comments
 (0)