Skip to content

Publish saved reports as public pages, and read them publicly - #4357

Draft
anth-volk wants to merge 9 commits into
masterfrom
v2plan/d6-1-public-report-pages
Draft

anth-volk wants to merge 9 commits into
masterfrom
v2plan/d6-1-public-report-pages

Conversation

@anth-volk

Copy link
Copy Markdown
Collaborator

Fixes #4356

Summary

Authors and admins can publish one of their saved reports as a public, read-only page that research posts can link to, and anyone can read it without signing in (tracker task D6 · 1 of the detailed-save API migration).

  • Owner routes (/v2/user-reports/{id}/publication, the user-report routes' credentials):

    • POST publishes the saved report, showing one succeeded run of the detailed-save report it names: a report run, or a budget window's budget-window run. It takes a summary (1–300 characters), author IDs from the website's authors.json (1–10), tags (up to 20), content and show_past_versions. Content is an ordered list of at most 100 Markdown blocks (each at most 20,000 characters, with no NUL character) and output-card blocks, keyed by v3's names; a card left out is removed. Request bodies are at most 16 MiB.
    • PATCH edits the page's fields: at least one, none as null.
    • POST …/publication/versions moves the page to another succeeded run as a new version.
    • GET reads the page with every version.
    • DELETE withdraws it.
    • Publishing, editing and moving need author or admin (403 below author). Reading and withdrawing need only ownership, so an author who was demoted can still withdraw a page.
  • Public read: GET /v2/published-reports/{id}?country_id=&version_id= needs no credential and never provisions a user. It answers:

    • the page's title (the saved report's name), summary, author IDs, tags and content;
    • the saved report's presentation, as /v2/shared-reports shows it;
    • the version read, and the versions a reader may open (every version while show_past_versions is on, else only the newest);
    • the report with that version's run, as GET /v2/reports/{id}?run_id= answers it.

    It never exposes the owner, the saved report's description (the owner's notes) or the owner's saved-reform IDs.

  • Tables (one autogenerated v2 revision, 8c867f67f87f):

    • published_reports, keyed by the saved report's ID.
    • published_report_versions, which names exactly one run per row; a run a page has shown can't be deleted.
    • A unique constraint on user_report_associations (id, report_id). It backs the published report's composite foreign key (ON DELETE CASCADE, ON UPDATE RESTRICT): deleting the saved report or the owner's account deletes the page, and repointing a published saved report is refused.
  • Repointing and the title: the user-report PATCH answers 409 when it would repoint a published saved report, or clear or blank its name, the page's title.

  • Locks: a publish and the saved report's PATCH lock the saved report's row; a page's edit, move and withdrawal lock the page's row. Each write waits for another write of the same row.

  • Revision correction: Alembic generated the unique constraint after the table that references it, which PostgreSQL refuses. The upgrade creates it first and the downgrade drops it last. The revision's docstring records this, and an integration test downgrades and upgrades.

  • Privileges: API v1's runtime role gets SELECT, INSERT, UPDATE, DELETE on published_reports and SELECT, INSERT, DELETE on published_report_versions; no other role gets access.

Migration notes:

  • Route group: published-reports joins the report route group; docs/generated/migration_contracts.json and docs/engineering/migration-contracts.md are regenerated.
  • No app consumer yet: v3's publish controls and public page come later (E2 · 2).
  • Later PR: the public listing of published reports and their card images (D6 · 2).
  • Inline suppressions added: one # noqa: E402, on the new model module's import in policyengine_api/data/v2/models/__init__.py. Like the package's other model imports, it must follow the naming convention set at the top of the module.

Testing

  • Red, then green: failing tests first (5c8aa3173), then the change (e587842cd). Three rounds of adversarial review followed, each fixed test-first (failing tests, then fixes).
  • Unit tests: 127 new and changed tests pass, run individually by node ID. They cover the routes (author check, ownership, error statuses, request checks, OpenAPI), the inputs, and the service on SQLite.
  • PostgreSQL: 11 new tests pass against a disposable PostgreSQL 17 database, with every statement as API v1's runtime role:
    • publishing, moving, reading and withdrawing;
    • the saved-report and account deletions deleting the page;
    • the refused repoint;
    • the keys that refuse a second page or a second version of one run;
    • a run a page has shown can't be deleted;
    • the one-run check;
    • a budget window's publication;
    • the row locks each write takes;
    • the downgrade and upgrade.
  • Checks: alembic -c alembic-v2.ini check, lint-imports, python -m scripts.rules validate, python scripts/run_quality_guards.py, ruff, ruff format and mypy all pass.

🤖 Generated with Claude Code

anth-volk and others added 9 commits November 28, 2026 01:53
An author or admin publishes one of their saved reports as a public page
showing one succeeded run of its report, edits the page's summary, authors,
tags and content, moves it to newer runs as versions, and withdraws it.
Anyone reads the published page by the saved report's ID, with the run of
its newest version, or an earlier one while the author shows past
versions. The database deletes the page with its saved report or account,
refuses to repoint a published saved report, and keeps every run a page
has shown.

These tests fail until the published_reports package, tables and routes
exist.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An author or admin publishes one of their saved reports as a public,
read-only page linked by the saved report's ID: POST
/v2/user-reports/{id}/publication with the run it shows (a report run, or
a budget window's budget-window run, which must have succeeded), a summary,
author IDs from the website's authors.json, tags and content (Markdown and
output-card blocks; a card left out is removed). The owner reads the page
with every version, edits its fields, moves it to another succeeded run as
a new version, and withdraws it. Publishing, editing and moving need author
or above; reading and withdrawing need only ownership. Anyone reads a
published report at GET /v2/published-reports/{id}, with the run of its
newest version, or an earlier one while the author shows past versions,
the saved report's presentation as a shared report shows it, and the report
as GET /v2/reports/{id}?run_id= answers it, without the owner or the
owner's notes.

The revision adds published_reports and published_report_versions, and a
unique constraint on user_report_associations (id, report_id) for the
published report's composite foreign key: deleting the saved report or the
owner's account deletes the page, and repointing a published saved report
is refused. The user-report PATCH refuses it with 409 first. The generated
revision created that unique constraint after the table that references
it, which PostgreSQL refuses, so its order is corrected and documented.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A page read while its withdrawal commits finds no versions, and answers
not found. A published saved report's name, the page's title, can't be
cleared. A page within the content limits written in three-byte characters
fits the request limit. The PostgreSQL refusals name the constraint they
rely on, and the keys that refuse a second publish or a second version of
one run are tested, as is a succeeded forced run. The service's reads hand
the routes typed content blocks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A published report read while its page is withdrawn, or its saved report
deleted, finds no versions; the public read now answers 404 and the
owner's reads not found, where the empty list raised IndexError and
answered 500. A published saved report's name is its page's title, so the
user-report PATCH refuses to clear it (409), as it refuses repointing.

The service hands the routes typed content blocks. The request limit is
16 MiB, which fits a page at the content limits whatever its characters.
The user-report PATCH and DELETE descriptions say what publishing changes,
the publication routes' 403 text says which need an author, and the
Alembic skill documents the revision and its one correction.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A saved report named only blanks has no title, so it can't be published,
and a published saved report's name can't be blanked. Each write locks the
row it checks: a publish and a saved report's edit lock the saved report,
and a page's edit, move and withdrawal lock the page. An edit sends at least
one field, and the edit schema shows no field as nullable. Each owner route
lists only its own failures. Tests also cover a signed-out browser on the
owner routes and a budget window published on PostgreSQL.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A saved report's name made only of blanks counts as none: it can't be
published, and a published saved report's name can't be blanked. A
publish and the saved report's PATCH now lock the saved report's row, so a
name can't be cleared while a publish reads it, and a page's edit, move and
withdrawal lock the page's row, so an edit no longer runs on a page just
withdrawn (it answered 503 when its UPDATE matched no row); it now waits,
then answers 404.

An edit must send at least one field, and its schema shows no field as
nullable. Each owner route lists only its own failures. The two
empty-versions guards are one. The downgrade test grants the roles'
privileges again after it upgrades, and the migration document says the
export doesn't include published pages yet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A summary or Markdown block holding a NUL character, which PostgreSQL
can't store, is refused as malformed. Each write route lists only its own
404 and 409 causes, the edit none of a publish's or a move's, and the edit
schema needs at least one property. A budget window's published page is
read through the report service on PostgreSQL.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A summary or Markdown block holding a NUL character, which PostgreSQL
can't store, is refused as malformed (422); it answered 503 when the
database refused the write. The publish, the edit and the move each list
their own 404 and 409 causes, the edit none, and the edit schema needs at
least one property.

The publish locks the saved report through the user-report connector
instead of a copy of it, the saved report's PATCH asks the published-report
connector whether it is published, and one check decides a blank name.

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

This branch was successfully deployed

1 active deployment
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.

Publish saved reports as public report pages, and read them publicly

1 participant