Repository navigation
Declare reports.type required, with ck_reports_type, on sim's model, with the API v1 pin on D1 · 8 - #1209
Merged
Conversation
Moves public-api-commit to 675e20f4, the head of PolicyEngine/policyengine-api#4400 (D1 · 8), whose v2 revision 24b571d7d079 makes reports.type NOT NULL and adds ck_reports_type. The comparison of sim's table models with API v1's tables now fails on reports, which the next commit fixes. The pin moves to #4400's merge commit before this branch merges. The tests' rows follow the check: insert_service_rows gives its budget window the type budget_window, and insert_report_of always sets a type. The re-run's refusal of a report whose type disagrees with its window size or household is now tested by calling _report_kind directly (test_report_kinds.py), since API v1's database refuses those rows. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s model Sim's Report model now matches API v1's reports table at D1 · 8's revision 24b571d7d079: type is NOT NULL, and ck_reports_type allows only economy, household and budget_window, with household exactly when the report names a household and budget_window exactly when it has a window size. Sim never creates tables, and its writers already set every report's type, so no runtime path changes. The library's README says so. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…e can disagree Review round 1 found that the Stage 12 library's test helpers ReportRunSeeder.seed_report_run and BudgetWindowSeeder.seed_window insert reports with no type, which API v1's table refuses at the new pin (NOT NULL), so the Public API schema job's Stage 12 step would fail. They now write economy and budget_window. The comment on ck_reports_type now says household and budget_window hold exactly when the report names a household and has a window size, and the library README says a disagreeing or missing type exists only where API v1's revision 24b571d7d079 has not run yet. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review round 2 found that test_economy_reruns' test_a_report_of_another_type_is_read_as_unsupported sets a report's type to custom_total, which API v1's ck_reports_type refuses at the new pin (CheckViolation), so the Public API schema job would fail. As with D1 · 7a's test of disagreeing types, the refusal in _report_kind stays and is now tested by calling it directly, in test_report_kinds. The library README now says exactly what revision 24b571d7d079's data step does: it sets each report's type from its household and window size where they disagree or the type is missing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…README From review round 3's notes: test_report_kinds gains a report with no type and no household or window, which nothing else refused on its own, and the persistence library README lists a type the service doesn't write among the unsupported ones and says the revision sets every report whose type differs from the one its household and window size give. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PolicyEngine/policyengine-api#4400 merged as 57202c04, on API v1's master. Between its head 675e20f4, which this branch was tested against, and the merge commit, API v1 changes no v2 revision, privilege code or migration configuration, so the tables its chain builds are the same. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
anth-volk
marked this pull request as ready for review
December 4, 2026 18:41
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.
Fixes #1208
Same-Repository Draft PR Check
PolicyEngine/policyengine-sim-api, not a personal fork.Changelog
changelog.d/<name>.<type>.md, whose type is breaking, added, changed, fixed or removed.Summary
Sim's
Reportmodel declaresreports.typeas required, with API v1's checkck_reports_type, which API v1's revision24b571d7d079adds (PolicyEngine/policyengine-api#4400, D1 · 8). Sim's tests now write only report rows that check accepts.Why: #4400 sets each report's type from its household and window size where they differ, makes
reports.typeNOT NULL, and addsck_reports_type:type IN ('economy', 'household', 'budget_window') AND (type = 'household') = (household_id IS NOT NULL) AND (type = 'budget_window') = (budget_window_size IS NOT NULL).test_the_models_match_the_public_api_tablescompares sim's models with API v1's tables constraint by constraint (rulesim.persistence.models-match-public-api). So from #4400's merge on, it fails against API v1'smaster, and the weekly drift run would fail too.The model:
Report.typeisMapped[str](NOT NULL), andReport.__table_args__declaresCheckConstraint(..., name="ck_reports_type")with API v1's text. PostgreSQL gives the samepg_get_constraintdeffor both. TheReportdocstring and the library README describe the type and its check.The pin:
public-api-commitmoves from41fc0151to57202c04, #4400's merge commit, on API v1'smaster(774cc342). The branch was built and tested against #4400's head675e20f4. Between41fc0151and that head, API v1 adds only revision24b571d7d079, and its privilege manifest is unchanged. Between the head and the merge commit, API v1 changes no v2 revision, privilege code or migration configuration.Tests that wrote rows the check refuses: sim's database tests run against API v1's tables built at the pin, so each now writes a type the check accepts.
service_rows.insert_service_rowsgives its budget window the typebudget_window.economy_rows.insert_report_ofalways sets a type.ReportRunSeeder.seed_report_runandBudgetWindowSeeder.seed_windowwriteeconomyandbudget_window.The re-run's refusal stays:
rerun_definitions._report_kindstill reads a report with no type, a type the service doesn't write, or a type that disagrees with its household or window size asunsupported. Production rows like that may exist until API v1's revision runs there. Two database tests stored such rows to test that refusal, and the check now refuses those rows:test_a_report_whose_type_disagrees_with_its_columns_is_unsupported;test_a_report_of_another_type_is_read_as_unsupported, which storedcustom_total.Both now call
_report_kinddirectly with the same cases, in the new unmarked moduletests/test_report_kinds.py. It also gains a case with no type and no other columns. The project owner chose this approach for D1 · 7a's test on 2026-12-04, and the second test follows the same decision.Changelog fragment:
changelog.d/d1-9-required-report-type.changed.md.No behaviour change: sim never creates tables, and SQLAlchemy doesn't enforce
nullableor check constraints itself. Since D1 · 7a (Store budget_window in reports.type for a budget window, and re-run a report by its stored type #1193), the service's writers (economy_report_row,keyed_household_records._report_row) set a type that agrees with the check. A merge tomainreleases and deploys the service and publishespolicyengine-api-simulation-client, as every sim merge does.Testing
Test first, with the existing comparison as the failing test. All tests below were selected by node ID and run against a disposable PostgreSQL 17 database, built with
.github/scripts/public-api-schema-reference.shat #4400's head675e20f4(revision24b571d7d079applied).e182b8ce(pin moved, tests adjusted, model unchanged)test_public_api_schema.py::test_the_models_match_the_public_api_tablesreports.type: the model allows null, the Public API's refuses it, and the missingck_reports_typee182b8cetest_report_kinds.py::test_a_report_whose_type_disagrees_with_its_columns_is_unsupported(5 cases)a0cc003b(model change)a0cc003btest_report_run_store.py::test_a_report_run_is_claimed_once_and_resumed_by_the_same_call,test_budget_window_store.py::test_the_starter_is_given_each_year_and_its_runs(as the Modal worker role)NotNullViolationonreports.type(round 1's bug)777ad6a2(Stage 12 seeders give a type)d823404btest_economy_reruns.py::test_a_report_of_another_type_is_read_as_unsupported, before its removalCheckViolationonck_reports_type(round 2's bug); replaced bytest_report_kinds.py::test_a_report_of_another_type_is_read_as_unsupported, which passesa0fe7b48test_report_kinds.py::test_a_report_with_no_type_is_read_as_unsupportedThe tests that call
insert_service_rows(test_service_writes.py,test_modal_worker_writes.py) were not run locally; CI runs them.Sim's checks pass: the pinned Ruff format and lint, the rules registry render, the quality guards (file length included), the lock checks, and Pyright for
libs/policyengine-simulation-persistence.CI: with the pin on #4400's head (
a0fe7b48), every check passed except the "Public API schema" job's last step, "Require the pinned commit on the Public API's default branch", because that head wasn't on API v1'smaster. That job's earlier steps passed: everypublic_api_schematest and the Stage 12 library's tests, against API v1's tables at that head.774cc342pins the merge commit, which is onmaster.Review
Three rounds of adversarial review (files in the session's notes folder):
a0cc003b): one high-priority bug. The Stage 12 library's seeders inserted reports with no type, so that job's Stage 12 step would fail at the new pin. Fixed in777ad6a2, which also makes the check's comment say "exactly when" and adds to the README when a type can still disagree.777ad6a2): one high-priority bug.test_a_report_of_another_type_is_read_as_unsupportedstored the typecustom_total, which the check refuses. Fixed ind823404b, as described above; the README's description of the revision is also made exact.d823404b): no bugs. Its two notes, a README sentence and a test of a report with no type on its own, are done ina0fe7b48.No bugs are left. These code-quality notes are left as they are:
tablescan't import the constants fromeconomy_run_recordsorhousehold_report_records, which importtables, and the model comparison catches any difference.tablesis at 797 of its 800-line limit.insert_report_ofdefaults its type toeconomyrather than working it out from its arguments.What the merge waits for
done.2af8bca4, which is enough. This PR's model change needs none of its deployment, since sim emits no DDL.57202c04, which is enough. The pin names that merge commit, and the comparison builds API v1's tables at that commit itself. The refusal in_report_kindstays, so this PR doesn't need #4400's revision to have run in production.It builds on
main, not on another task's branch.🤖 Generated with Claude Code