Repository navigation
Conversation
A Pydantic schema sees one payload, so an OCL invariant that navigates associations was left as a TODO and nothing enforced it in the generated backend. The generated database.py now checks the invariants it can translate before each commit, on the records the request loaded or changed and their direct neighbours, and refuses a violating change with HTTP 422 naming the rule. Translated subset (besser/generators/backend/ocl_invariants.py, over the B-OCL parse tree): navigation from self, isEmpty/notEmpty/size/includes/ excludes/isUnique, comparisons of numbers/strings/booleans, and/or/xor/ implies/not, literals. Anything else keeps its TODO, and so does a rule that would fail while an end whose records need self first is still empty (it would make the record impossible to create). The schema comment now says the rule is checked at commit, and the Spec-Driven Agent no longer seeds an "enforce this rule" task for it on the scaffolds that carry the check (FastAPI backend, web app).
- A minimum on an end whose records each need a record of the rule's class keeps its TODO whatever that end's upper bound: with "one or more" it made the first record impossible to create. - size(), isEmpty() and the other collection operations on a string keep their TODO; size() of a string was checked as a collection of one. - isUnique by an end with several records per element keeps its TODO. - An ordering comparison that meets an empty value leaves the rule undefined for that record instead of a TypeError being caught and logged; other errors are no longer swallowed. - The check covers the records a change writes (and those a changed or deleted link pointed to), not every record the request loaded, and loads links in batches: SQL statements per write no longer grow with the data (808 -> 35 for a label linked to 400 requests). - The "checked at commit" note is written only by BackendGenerator, which generates the database.py that checks it.
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.
Why
The Pydantic schemas only see one request payload. So an OCL invariant that navigates associations was skipped, and the generated backend shipped it as a
TODOcomment that nothing enforced. Found on the CaseLens model: both of these were TODOs, and the generated API accepted every violation.What
besser/generators/backend/ocl_invariants.py(new) translates these invariants into Python expressions, working from the B-OCL parse tree. It only takes rules the Pydantic path skips, so no rule is checked twice. Every expression is screened against an AST whitelist before it's emitted.selfthrough attributes and ends;isEmpty,notEmpty,size,includes,excludeson collections;isUniqueby an attribute or a single-valued end; comparisons of numbers, strings and booleans;and,or,xor,implies,not; literals.forAll,select,collect,sum, the length of a string, enums, dates).self.items->notEmpty()when every item needs one order, or one or more. Checked at commit, it would make the first record impossible to create.<,>, ...) that meets an empty value leaves the rule undefined, so it isn't applied to that record.=and<>treat an empty value as null.database.py, only when a model has such rules: abefore_commitlistener flushes and re-reads the changed records, so link rows written by Coreinsert()/delete()count. It evaluates each rule on the records the change writes, the records a changed or deleted link pointed to, and their direct neighbours. It loads their links in batches, so the number of SQL statements per write doesn't grow with the data. It refuses the commit with HTTP 422 naming the rule (get_dbrolls back). The check lives indatabase.py, not a new file, so the Dockerfile and deploy file lists don't change.pydantic_classes.py: for these rules, the TODO becomesOCL constraint 'X' is enforced when a change is committed (database.py). OnlyBackendGeneratorasks for this note; Pydantic generated on its own keeps the TODO._unenforced_rule_tasksno longer seeds an "Enforce the modeled rule" task for a rule thatdatabase.pychecks. That applies only togenerate_fastapi_backendandgenerate_web_app; the plaingenerate_rest_apihas nodatabase.py, so it still gets the task.generators/backend.rst, and a pointer to it frompydantic.rst.Known limit
Both are documented in
generators/backend.rst.Tests
tests/generators/backend/test_ocl_invariant_checks.pygenerates the CaseLens rules into a backend and drives the real API. A result for a label outside its request gets 422. So does a second result for the same label, and so does removing a label that a result still uses (the other-side change). A refused change leaves nothing behind. Before this change, all four violations returned 200, and 4 results were stored instead of 2.isEmpty() = falseandimplies), plusforAllandcollect()->sum(), stay TODOs.tests/spec_driven_agent/test_ocl_rule_seed.py: a commit-checked rule is not seeded on the FastAPI backend or the web app, and is seeded on the plain REST API. The existing "still enforced" cases now run against the plain REST API, because on the other two scaffolds the backend enforces those rules itself.size()of a string;isUniqueby a many-valued end;tests/generators tests/spec_driven_agent tests/utilities: 15026 passed. 4 failed, and the same 4 fail on unchangeddevelopmenton this machine (nojavac, and noruffon the test venv's PATH).ruff check besser/passes,lint-importskeeps all 4 contracts, anddocs/check_docs.py --offlinepasses.