Skip to content

Declare reports.type required, with ck_reports_type, on sim's model, with the API v1 pin on D1 · 8 - #1209

Merged
anth-volk merged 6 commits into
mainfrom
claude/d1-9-required-report-type
Dec 4, 2026
Merged

anth-volk merged 6 commits into
mainfrom
claude/d1-9-required-report-type

Conversation

@anth-volk

@anth-volk anth-volk commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1208

Same-Repository Draft PR Check

  • This PR is a draft.
  • This PR is from a branch in PolicyEngine/policyengine-sim-api, not a personal fork.

Changelog

  • This PR adds a changelog fragment, changelog.d/<name>.<type>.md, whose type is breaking, added, changed, fixed or removed.

Summary

Sim's Report model declares reports.type as required, with API v1's check ck_reports_type, which API v1's revision 24b571d7d079 adds (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.type NOT NULL, and adds ck_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_tables compares sim's models with API v1's tables constraint by constraint (rule sim.persistence.models-match-public-api). So from #4400's merge on, it fails against API v1's master, and the weekly drift run would fail too.

  • The model: Report.type is Mapped[str] (NOT NULL), and Report.__table_args__ declares CheckConstraint(..., name="ck_reports_type") with API v1's text. PostgreSQL gives the same pg_get_constraintdef for both. The Report docstring and the library README describe the type and its check.

  • The pin: public-api-commit moves from 41fc0151 to 57202c04, #4400's merge commit, on API v1's master (774cc342). The branch was built and tested against #4400's head 675e20f4. Between 41fc0151 and that head, API v1 adds only revision 24b571d7d079, 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_rows gives its budget window the type budget_window.
    • economy_rows.insert_report_of always sets a type.
    • The Stage 12 library's seeders ReportRunSeeder.seed_report_run and BudgetWindowSeeder.seed_window write economy and budget_window.
  • The re-run's refusal stays: rerun_definitions._report_kind still reads a report with no type, a type the service doesn't write, or a type that disagrees with its household or window size as unsupported. 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:

    • D1 · 7a's test_a_report_whose_type_disagrees_with_its_columns_is_unsupported;
    • test_a_report_of_another_type_is_read_as_unsupported, which stored custom_total.

    Both now call _report_kind directly with the same cases, in the new unmarked module tests/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 nullable or 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 to main releases and deploys the service and publishes policyengine-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.sh at #4400's head 675e20f4 (revision 24b571d7d079 applied).

Commit Test Result
e182b8ce (pin moved, tests adjusted, model unchanged) test_public_api_schema.py::test_the_models_match_the_public_api_tables fails on its assertion with exactly two differences: reports.type: the model allows null, the Public API's refuses it, and the missing ck_reports_type
e182b8ce test_report_kinds.py::test_a_report_whose_type_disagrees_with_its_columns_is_unsupported (5 cases) passes (behaviour already present; moved from a database test)
a0cc003b (model change) both of the above pass
a0cc003b Stage 12: test_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) fail: NotNullViolation on reports.type (round 1's bug)
777ad6a2 (Stage 12 seeders give a type) the two Stage 12 tests pass
d823404b test_economy_reruns.py::test_a_report_of_another_type_is_read_as_unsupported, before its removal fails: CheckViolation on ck_reports_type (round 2's bug); replaced by test_report_kinds.py::test_a_report_of_another_type_is_read_as_unsupported, which passes
a0fe7b48 test_report_kinds.py::test_a_report_with_no_type_is_read_as_unsupported passes

The 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's master. That job's earlier steps passed: every public_api_schema test and the Stage 12 library's tests, against API v1's tables at that head. 774cc342 pins the merge commit, which is on master.

Review

Three rounds of adversarial review (files in the session's notes folder):

  • Round 1 (on 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 in 777ad6a2, which also makes the check's comment say "exactly when" and adds to the README when a type can still disagree.
  • Round 2 (on 777ad6a2): one high-priority bug. test_a_report_of_another_type_is_read_as_unsupported stored the type custom_total, which the check refuses. Fixed in d823404b, as described above; the README's description of the revision is also made exact.
  • Round 3 (on d823404b): no bugs. Its two notes, a README sentence and a test of a report with no type on its own, are done in a0fe7b48.

No bugs are left. These code-quality notes are left as they are:

  • The three type values are written as literals in the check and the test helpers. tables can't import the constants from economy_run_records or household_report_records, which import tables, and the model comparison catches any difference.
  • tables is at 797 of its 800-line limit.
  • insert_report_of defaults its type to economy rather than working it out from its arguments.

What the merge waits for

It builds on main, not on another task's branch.

🤖 Generated with Claude Code

anth-volk and others added 6 commits December 4, 2026 20:59
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
anth-volk marked this pull request as ready for review December 4, 2026 18:41
@anth-volk
anth-volk merged commit 7a77a81 into main Dec 4, 2026
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sim's model of reports lacks the required type and ck_reports_type, which API v1 adds

1 participant