feat: Add household support (CRUD, associations, impact analysis)#80
feat: Add household support (CRUD, associations, impact analysis)#80anth-volk merged 19 commits intoapp-v2-migrationfrom
Conversation
- Replace synchronous inline calculation with async trigger pattern - Add _trigger_household_impact() mirroring _trigger_economy_comparison() - Add _run_local_household_impact() for local execution (blocking) - Add _run_simulation_in_session() for running individual simulations - Update POST endpoint to trigger and return immediately - Add test script for manual end-to-end testing Note: Local execution blocks the request (same as economic impact). True async requires Modal functions (household_impact_uk/us). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
…lue serialization - Remove Enum/date serialization from seed_common.py since policyengine.py now pre-serializes default_value for JSON compatibility - Change default_value type from `str | None` to `Any` in Variable model since it stores JSON values (bool, int, float, str) Depends on policyengine.py feat/add-variable-default-value branch 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Add tests for Variable with int, float, bool, and string default values - Add test for null default_value handling - Add test for Household model creation 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
session.get(Report, report_id) expects a UUID, but report_id was passed as a string. This caused 'str' object has no attribute 'hex' errors. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
Converting to draft, as this implementation is actually flawed and duplicative. |
… reform bug is fixed This commit removes the workaround code that was added to bypass policyengine.py's Simulation class. The workaround was needed because policyengine.py's US simulation applied reforms via p.update() after Microsimulation construction, which didn't work due to the US package's shared singleton TaxBenefitSystem. That bug has now been fixed in policyengine.py (issue #232), so we can use policyengine.py's Simulation class directly again. Changes: - Revert household.py to use policyengine.core.Simulation instead of manually building Microsimulation with reform dicts - Revert modal_app.py to use PESimulation instead of custom helper functions (_pe_policy_to_reform_dict, _merge_reform_dicts, _run_us_economy_simulation, _run_uk_economy_simulation) - Remove now-obsolete test files for the workaround functions 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Update: Workaround code removedThe latest commit removes the workaround code ( This is possible because the root cause bug has been fixed in policyengine.py - see:
This PR now depends on policyengine.py PR #231 being merged first. The bug was that policyengine-us uses a shared singleton This commit removes ~1,800 lines of workaround code from this PR. |
Add tests to verify that US policy reforms are applied correctly to household calculations. These tests cover: - Integration tests via API endpoints (TestUSPolicyReform, TestUKPolicyReform) - Unit tests for the calculation functions directly (test_household_calculation.py) The tests verify: 1. Baseline calculations work correctly 2. Reforms change household net income as expected 3. Running a reform doesn't pollute subsequent baseline calculations (regression test for the singleton pollution bug) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
The PyPI release (3.1.15) has a bug where US reforms silently fail due to the shared singleton TaxBenefitSystem (policyengine.py#232). The fix exists on the app-v2-migration branch but hasn't been released. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Hatchling rejects git URL dependencies unless explicitly opted in. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…ions Test scripts in scripts/ were ad-hoc debugging aids, not part of the test suite. Nevada seed is no longer needed. Archived Supabase migrations are superseded by Alembic. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Fixes #74
Fixes #75
Fixes #76
Fixes #78
Fixes #79
Important
The
policyengine-app-v2repo will also need changes to consume the modifications from this PR. Frontend work should be coordinated once this API is merged.Important
Production database migration required. This PR switches from Supabase SQL migrations to Alembic. After merging, run
alembic stamp a17ac554f4aaon the production database to mark existing schema as migrated. See.claude/skills/database-migrations.mdfor details.Summary
This PR adds comprehensive household support to the API v2 alpha and migrates to Alembic for database migrations.
1. Stored Household CRUD (#74)
Householdmodel withhousehold_dataJSONB storagePOST/GET/DELETE /householdsendpoints2. User-Household Associations (#75)
UserHouseholdAssociationmodel linking users to householdsPOST/GET/PUT/DELETE /user-household-associationsendpoints3. Household Impact Analysis (#76)
Simulationmodel withSimulationTypeenum,household_id,household_resultReportmodel withreport_typefieldPOST/GET /analysis/household-impactendpoints4. Alembic Migration System (#78)
SQLModel.metadata.create_all()with Alembic migrations.claude/skills/database-migrations.md5. Fix Policy Reforms Not Applied in Simulations (#79)
policyengine.core.Simulationclass createsMicrosimulationwithout the reform, then tries to apply policy after construction (which doesn't work due to caching)_pe_policy_to_reform_dict()and_merge_reform_dicts()to convert PEPolicy objects to reform dict format_run_us_economy_simulation()and_run_uk_economy_simulation()helpers that pass reforms at Microsimulation construction time_calculate_household_usand_calculate_household_uk)Database Migrations
Now managed by Alembic in
alembic/versions/:d6e30d3b834d_initial_schema.py- Creates all 21 tablesa17ac554f4aa_add_parameter_values_indexes.py- Adds performance indexesFiles Changed
New Files
src/policyengine_api/models/household.pysrc/policyengine_api/models/user_household_association.pysrc/policyengine_api/api/households.pysrc/policyengine_api/api/user_household_associations.pyalembic/- Alembic configuration and migrations.claude/skills/database-migrations.md- Migration guidelinesscripts/seed_nevada.py- Nevada dataset seeding scriptscripts/test_economy_simulation.py- Test script to verify policy reform bugtest_fixtures/fixtures_households.pytest_fixtures/fixtures_user_household_associations.pytest_fixtures/fixtures_analysis.pytests/test_households.pytests/test_user_household_associations.pytests/test_analysis_household_impact.pyModified Files
src/policyengine_api/models/simulation.py- Added SimulationType, household_id, household_resultsrc/policyengine_api/models/report.py- Added report_typesrc/policyengine_api/models/__init__.py- Exports new modelssrc/policyengine_api/api/__init__.py- Registers new routerssrc/policyengine_api/api/analysis.py- Added household-impact endpointssrc/policyengine_api/api/household.py- Fixed policy reform application in household calculationssrc/policyengine_api/modal_app.py- Fixed policy reform application in economy-wide simulationssrc/policyengine_api/config/settings.py- Fixed database URL portscripts/init.py- Now uses Alembic instead of create_all()CLAUDE.md- Added Alembic documentation referenceTest Plan
alembic stampon production after merge🤖 Generated with Claude Code
Co-Authored-By: Claude Opus 4.5 noreply@anthropic.com