Skip to content

Define the fields App v3 reads for a 2027 bill report - #241

Merged
DTrim99 merged 2 commits into
mainfrom
app-v3-bill-contract
Dec 3, 2026
Merged

DTrim99 merged 2 commits into
mainfrom
app-v3-bill-contract

Conversation

@DTrim99

@DTrim99 DTrim99 commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

From the 2027 sessions on, bill analyses are scored and published through App v3 (Teamverse p301, milestones p451, p453 and p454). This PR fixes the data contract between the bill pipeline and App v3, so the pipeline, App v3 and the CRM build against the same fields.

What changes

  • docs/APP_V3_BILL_CONTRACT.md sets out:

    • the lifecycle (imported, triaged, encoded and scored, approved), with the writer and table at each stage;
    • each field App v3 reads, who writes it, and how App v3 uses it;
    • the comparisons row format (tier, kind, estimate, ratio, band);
    • how /encode-bill will save an App v3 report through the Public API.
  • scripts/sql/010_add_app_v3_bill_fields.sql adds columns, all with ADD COLUMN IF NOT EXISTS:

    • research: session_name, session_year, sponsors, introduced_date, bill_text_url, app_report_id, app_report_url, review_pr_url, approved_at;
    • validation_metadata: comparisons and calibration_check.

    It runs after 009 from the import PR.

  • publish-bill.yml now records the approval when a bill-review PR merges: approved_at (the merge time) and review_pr_url. 010 was applied to production on 3 December, so the fallback that published the status alone is gone (after Pavel's review); a missing column is now a real error.

  • publish-bill.yml security fix. This pull_request_target job holds the Supabase and Modal secrets. It pasted the PR's branch name into a shell script and into Python source, so a branch name containing $(…) could run code with those secrets. The branch name, reform id, PR URL and merge time now reach the scripts only through env:, and the reform id must match ^[a-z0-9][a-z0-9-]*$.

Why copies on research

App v3's bill feed reads research with select=*, so new columns there reach it without an App v3 change. It reads processed_bills and validation_metadata with fixed column lists, and naming a column that doesn't exist yet fails the whole read. So /encode-bill copies sponsors, introduced date and bill text link onto research, and App v3 reads comparisons in a separate query that can fail on its own (policyengine-app-v2#1658).

Not in this PR

  • Applying 009 and 010 to the Supabase project.
  • /encode-bill writing the new fields. That comes with the switch to scoring through the Public API (p451). Anthony added the machine credential as repo secrets on 3 December; the API accepts it after API v1's next production deploy.
  • App v3 reading them (p453).

Related: the import PR (009, sponsors and sessions), the readiness lookup PR (calibration_check), and tracker #239 (session-aware ids).

🤖 Generated with Claude Code

- docs/APP_V3_BILL_CONTRACT.md: the lifecycle from import to approval,
  each field App v3 reads with its writer, the comparison row format,
  and the saved App v3 report.
- scripts/sql/010_add_app_v3_bill_fields.sql: research gains the session,
  sponsors, introduced date, bill text link, App v3 report id and link,
  review PR and approval time; validation_metadata gains comparisons and
  calibration_check.
- publish-bill.yml records approved_at and the review PR when a
  bill-review PR merges, falling back to the status alone until 010 runs.
  The PR's branch name and URL now reach the shell and Python only through
  environment variables, and the reform id must be a plain research id:
  this pull_request_target job holds the Supabase and Modal secrets.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@PavelMakarchuk PavelMakarchuk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review at 1f2c0c1

This PR defines the data contract between the bill pipeline and App v3 for 2027 bills. It adds docs/APP_V3_BILL_CONTRACT.md and migration 010, which adds nine columns to research and two to validation_metadata. It changes publish-bill.yml to record approved_at and review_pr_url when a bill-review PR merges. It also closes a shell and Python injection path: the branch name now reaches the scripts only through env:, and the reform id must match ^[a-z0-9][a-z0-9-]*$.

Verdict: REQUEST_CHANGES

The code is correct. The two items below are statements in the contract document that do not match how App v3 reads the tracker today. Because this document is the contract, a later change in App v3 or in the pipeline can follow them and break.

SHOULD ADDRESS

  1. docs/APP_V3_BILL_CONTRACT.md:31: validation_metadata has the same fixed-column hazard as processed_bills, but the document warns only about processed_bills.
    App v3 (fetchTrackerBills in app/src/api/billFeed.ts, also at policyengine-app-v2#1646 head dba4aaa) reads validation_metadata with a fixed list: id,fiscal_note_source,…,verified_at,validated_against. That list does not include comparisons or calibration_check. Case: an App v3 change adds comparisons to that list before 010 is applied. PostgREST then rejects the read, and the whole bill feed fails, the same way the paragraph describes for processed_bills.
    Fix: in the paragraph at line 31, name validation_metadata too. Say that App v3 adds comparisons and calibration_check only after 010 is applied, or reads them in a separate query that can fail alone (as App v3 already does for reform_impacts.baseline_params).

  2. docs/APP_V3_BILL_CONTRACT.md:14: "App v3's feed shows published bill rows only" is not what App v3 does.
    isBillRecord in billFeed.ts keeps a row when its type is bill (or empty) and its status is not in_review. Case: a bill row with status in_progress or planned (both values that 001_create_tables.sql lists for research.status) shows in App v3, although it was never approved. The lifecycle stays safe only because /encode-bill always writes in_review.
    Fix: change the sentence to "App v3's feed hides in_review rows, so merging the review PR is what lists a bill". Or, if "published only" is the intended rule, record it as a required App v3 change and filter on status = published there.

SUGGESTION

  1. docs/APP_V3_BILL_CONTRACT.md:57: define ratio and the band edges. The example implies ratio = pe_estimate / estimate (−697,798,094 / −680,000,000 = 1.03). Write that formula. Also say which band takes an exact edge, for example |ratio − 1| = 0.10 goes to 10_to_25.
  2. docs/APP_V3_BILL_CONTRACT.md:29: give the shape of calibration_check. Section 3 fixes the shape of comparisons, but calibration_check has no shape. A link to the output format of scripts/readiness_lookup.py (tracker #242) is enough.
  3. .github/workflows/publish-bill.yml:72: plan to remove the fallback after 010 is applied. After that, a PGRST204 error means something is wrong, but the job still publishes and only prints a line. Add a TODO that names 010, so the fallback does not stay in place for good.

What I checked

  • Contract against App v3 (policyengine-app-v2#1646 at dba4aaa). intakeFields reads sponsors, introduced_date, session_name, session_year, bill_text_url, app_report_url and approved_at from research. All seven are in 010, with types that suit the reader: session_year is INTEGER (App v3 checks Number.isInteger), and the dates arrive as ISO strings. App v3 reads research with select=*, as the document says. It does not read app_report_id or review_pr_url yet, which is fine.
  • Contract against the importer (tracker #243 at 8a6568a). 009 adds sponsors, introduced_date, bill_text_url, session_start_date, session_end_date and session_classification to processed_bills. These match the table in section 2. The sponsor objects that #243 writes (name, primary, party, district, and more) contain the keys App v3 reads. session_name is already on processed_bills.
  • Other series PRs. #239, #242 and #244 do not write any 010 column yet. #239 gives ids such as ky-hb1-2027, which pass the new reform id check.
  • Workflow probe. I extracted both changed steps from the YAML and ran them locally. The extract step accepted bill/ga-sb168 and bill/ky-hb1-2027. It rejected bill/$(touch pwned), bill/x";id;", bill/GA-SB1, bill/ga_sb1, bill/ and feature/x, and no command ran. Against a fake Supabase client, the publish step wrote all three fields, fell back to the status alone on a PGRST204 error, exited 1 when no row matched, and raised again on other errors.
  • Reform id check against history. All 35 merged bill-review PR branches pass ^[a-z0-9][a-z0-9-]*$.
  • Lifecycle claims. The triage score below 20 is not_modelable (scripts/auto_triage.py), and /encode-bill writes in_review. Both match the document.
  • Tests. vitest: 82 passed in 8 files. pytest on test_compute_impacts_datasets.py and test_publish_analysis.py: 37 passed. test_auto_triage.py does not collect here, because hypothesis is not installed (not related to this PR). eslint: 0 errors and 9 warnings, the same as on main. The PR changes no app code, so I did not run a separate build for it.
  • The branch contains main (cdaec13) and merges cleanly. No CI checks run on this branch.

Pavel's review of #241:
- validation_metadata is read with a fixed column list too, so the doc
  now says new columns there go in a separate query that can fail alone
  (as app-v2 #1658 does for comparisons) or wait for the migration.
- "Published rows only" is what app-v2 #1658 does; until it's live the
  feed hides in_review rows, and the doc says what that requires.
- research.session_name is the OpenStates identifier, so App v3 reads
  the session from session_year (app-v2 #1646).
- ratio and the band edges are defined (an exact edge goes to the wider
  band, as band_of does), and calibration_check points to
  readiness_lookup's result.
- 009-012 are applied in production (3 December), so publish-bill.yml no
  longer falls back to the status alone on a missing column.
- The credential section names the secrets Anthony added.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@DTrim99

DTrim99 commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks, Pavel. Fixed in ea9c1e6.

Should address

  1. validation_metadata fixed list. The paragraph under the field table now names both processed_bills and validation_metadata. It says App v3 reads a new column on either table in a separate query that can fail on its own, or adds it to the fixed list only after the migration is applied. policyengine-app-v2#1658 already reads comparisons that way: a separate validation_metadata select whose failure leaves the feed intact.
  2. "Published rows only". It's now what App v3 does: #1658 changes isBillRecord to keep a bill row only when its status is published (or empty, for older rows). The doc cites #1658 and says what holds until it's live: the feed hides in_review, so nothing should write a bill row with another unapproved status before then.

Suggestions

  1. ratio and the band edges. ratio = pe_estimate / estimate, rounded to four places, with your example. An exact edge goes to the wider band (|ratio − 1| = 0.10 is 10_to_25), which is what band_of in compare_estimates.py (Score bills through the Public API App v3 uses (p451 groundwork) #246) does.
  2. calibration_check shape. It points to lookup() in scripts/readiness_lookup.py (Add a readiness lookup: calibration and scorecard evidence for a reform in a state #242) and its tests.
  3. The 010 fallback. 009–012 were applied to production on 3 December (I checked that approved_at, review_pr_url, comparisons and calibration_check exist). So I removed the fallback instead of adding a TODO: publish-bill.yml now writes the status and both approval fields in one update, and any error fails the step.

Also changed

  • Session field. The contract now says that research.session_name holds the OpenStates identifier (2027_28), not a display name, and that App v3 reads the session from session_year. That came out of your #1646 review.
  • Credential. The credential section names the secrets Anthony added today.
  • Migration numbers. The doc says a new migration takes 013 or later, since 009–012 are applied under their current names. That also answers the 009 clash with Add row level security to processed_bills #229 from your Read 2027 bills with sponsors, text links and session dates #243 review.

#247, which is stacked on this branch, now includes these changes.

@PavelMakarchuk PavelMakarchuk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review at ea9c1e6

This PR defines the data contract between the bill pipeline and App v3 for 2027 bills. It adds docs/APP_V3_BILL_CONTRACT.md and migration 010, and it makes publish-bill.yml record approved_at and review_pr_url when a bill-review PR merges. It also closes the shell and Python injection path through the branch name. Since 1f2c0c1, one commit (ea9c1e6) changes the contract text to match the App v3 readers in policyengine-app-v2#1646 and #1658, and removes the 010 fallback from the publish step.

Verdict: APPROVE

Both SHOULD ADDRESS items from the last review are fixed. I found no new CRITICAL or SHOULD ADDRESS items. The new items below are small text fixes.

Earlier items

  1. SHOULD ADDRESS 1, validation_metadata fixed list: fixed. Line 31 now names processed_bills and validation_metadata. It says to read a new column in a separate query, or to add it to the fixed list only after the migration. policyengine-app-v2#1658 (head 59a897c) does this: it reads id,comparisons in its own trackerSelect with .catch(() => null), and the fixed validation_metadata list is unchanged.
  2. SHOULD ADDRESS 2, "published rows only": fixed. #1658 changes isBillRecord to keep a bill row only when its status is published or empty. Line 14 cites #1658 and states the rule that applies until it is live (the feed hides in_review). Both statements match the code. research.status is NOT NULL, so the empty case applies only to unusual rows.
  3. SUGGESTION 1, ratio and band edges: fixed, with one small error. See new suggestion 1 and 2.
  4. SUGGESTION 2, shape of calibration_check: fixed. Line 61 points to lookup() in scripts/readiness_lookup.py (tracker #242). calibration_check() in #246 returns that result.
  5. SUGGESTION 3, the 010 fallback: fixed. The step now writes the status and both approval fields in one update. Any error fails the step. See new suggestion 3 for a stale comment.

SUGGESTION

  1. docs/APP_V3_BILL_CONTRACT.md:57: the example does not match "rounded to four places". −697,798,094 / −680,000,000 rounded to four places is 1.0262, not 1.03. compare_estimates.py (#246) stores round(ratio, 4), so it writes 1.0262 for this example. Fix: change the text to "= 1.0262", and change "ratio": 1.03 in the JSON example at line 48 to 1.0262.
  2. docs/APP_V3_BILL_CONTRACT.md:58: an exact edge below 1 goes to the narrower band. band_of computes abs(ratio - 1) < 0.10 on the unrounded float. For ratio 1.1 the gap is 0.10000000000000009, so it goes to 10_to_25, as the text says. For ratio 0.9 (PE −612,000,000, estimate −680,000,000) the gap is 0.09999999999999998, so it goes to under_10. Fix: in #246, round the gap before the compare (for example round(abs(ratio - 1), 4)), or remove the "exact edge" sentence from the contract.
  3. .github/workflows/publish-bill.yml:41: the comment above the step still describes the removed fallback. It says "Before scripts/sql/010 is applied those columns don't exist, so it falls back to the status alone." The step no longer falls back, and the new comment at line 66 says the opposite. Fix: remove the last sentence of the comment at lines 41–43.
  4. docs/APP_V3_BILL_CONTRACT.md:24: App v3 does not read session_start_date, session_end_date or session_classification. The fixed processed_bills list in #1658 is bill_id,state,bill_number,status,legiscan_url,last_action_date. The session view in billSessions.ts uses only session_year. Fix: mark this row's App v3 use as planned. The same applies to calibration_check at line 29, which no App v3 PR reads yet.

What I checked

  • Contract against App v3. I read the full diffs of policyengine-app-v2#1646 (head 98a993f) and #1658 (head 59a897c, stacked on #1646), and billFeed.ts at 59a897c. intakeFields reads sponsors, introduced_date, session_name, session_year, bill_text_url, app_report_url and approved_at from research. The overview names the session from session_year, as line 23 now says. comparisonsFrom reads source, tier, kind, estimate, window, pe_estimate, url and note, all in the section 3 shape. App v3 computes its own ratio and does not read ratio, band or unit.
  • Helpers that the contract cites. band_of and the ratio rounding in scripts/compare_estimates.py (tracker #246 at db0e51d). lookup() in scripts/readiness_lookup.py (tracker #242 at 1c1b333). I ran the two edge cases above in Python.
  • Workflow probe. I extracted the new publish step from the YAML and ran it against a fake Supabase client. A matching row: exit 0, one update with all three fields. No matching row: exit 1. A missing-column error (PGRST204): exit 1. Another error: exit 1. The extract step did not change since 1f2c0c1.
  • Migration claim. The author states that 009 to 012 are applied to production. I cannot check the production database, so I did not verify this. The publish step now depends on it.
  • Tests. vitest: 82 passed in 8 files. pytest on test_compute_impacts_datasets.py and test_publish_analysis.py: 37 passed. eslint: 0 errors and 9 warnings, the same as before. The workflow YAML parses. The PR changes no app code, so I did not run a build.
  • The branch contains main (cdaec13) and merges cleanly. No CI checks run on this branch. The head was ea9c1e6 at the start and at the end of this review.

@DTrim99
DTrim99 merged commit fe43e9c into main Dec 3, 2026
DTrim99 added a commit that referenced this pull request Dec 3, 2026
… band

Pavel's review of #241: abs(0.9 - 1) is 0.09999999999999998 in floating
point, so a ratio of exactly 0.9 fell in under_10 while 1.1 went to
10_to_25. The gap is now rounded to four places, matching the stored ratio
and the contract's rule.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
DTrim99 added a commit that referenced this pull request Dec 4, 2026
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.

2 participants