feat(extensions): add OperationValidator.precheck pre-body-parse hook#1548
Open
cdbartholomew wants to merge 1 commit into
Open
feat(extensions): add OperationValidator.precheck pre-body-parse hook#1548cdbartholomew wants to merge 1 commit into
cdbartholomew wants to merge 1 commit into
Conversation
Add an optional ``precheck`` method to ``OperationValidatorExtension`` that extensions can override to gate a request *before* its body is read off the wire. Wire it as a FastAPI ``Depends`` ahead of the body parameter on the billable POST routes (retain, recall, reflect, file retain, mental-model create, mental-model refresh) so a rejecting precheck short-circuits the request without ever materialising the JSON payload in memory. The post-body-parse ``validate_retain`` / ``validate_recall`` / ``validate_reflect`` hooks are unchanged and remain the source of truth for precise per-call cost and quota arithmetic. ``precheck`` is intentionally a cheap, side-effect-free check — its sole purpose is to let an extension short-circuit work that would otherwise allocate the request body unnecessarily (e.g. a quota-exhausted caller submitting many large bodies). Why before body parse: FastAPI resolves dependencies before deserialising the route's body parameter. A validator that runs only after parse — i.e. inside the route handler's body — sees the already-materialised request, which is the wrong layer for "this caller should not be allowed to spend resources on this request at all" decisions. Wiring as ``Depends`` puts the gate at the right layer with a one-line change per route. Verified: - FastAPI 0.125.0 resolves ``Depends`` raising ``HTTPException`` before Pydantic deserialises the body, regardless of declaration order. A reproducer using a ``model_validator(mode='before')`` recorder confirms zero body-parse calls on the rejection path. - The new ``PrecheckContext`` carries only operation name + bank_id + request_context (already-resolved tenant). No body access — by design. - Default ``precheck`` returns ``ValidationResult.accept()``; existing validators are unaffected. Tests: +7 unit tests covering the default no-op, the FastAPI Depends wiring, accept/reject paths, status-code/reason propagation, and explicit "body never parsed on rejection" assertions for retain / recall / reflect plus a "GET routes are unaffected" guard. All passing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
precheck()method toOperationValidatorthat lets an extension gate a request before its body is read.Dependsahead of the body parameter on the billable POST routes (retain, recall, reflect, files retain, mental-model create + refresh).Test plan
tests/test_extensions.pycovering: default no-op accept, rejection short-circuits before body parse (asserted via Pydanticmodel_validator(mode='before')recorder — body never deserialized on reject), rejection returns the validator's status code + reason, GET routes are unaffected, accept lets request through to body parse, body-parse-skipped behaviour holds for retain/recall/reflect.uv run pytest tests/test_extensions.py→ 15 passed). The 21 errors visible in the same run are pre-existing environment issues (missing LLM API key, alembic state) onmainitself — verified independent of this PR.