Files
provenance/backend/migrations/versions/a1b2c3d4e5f6_change_proposals.py
T
justin abaa8efdd5 Fix #214: ChangeProposal (propose-then-confirm)
Implements non-negotiable #1: the AI assistant never writes autonomously. Every
assistant/contributor "write" emits a ChangeProposal — a structured diff a human
approves, edits, or rejects. Design: docs/design/change-proposal.md.

Structural guarantee: a proposal's operations reach the DB ONLY via
change_proposal_service.apply(), which requires the actor be an editor and
dispatches each op through the normal editing services (person/name/event/
relationship/source/citation create/update/delete) — so every change passes the
privacy engine and is audited as the approving human. propose() only inserts a
pending row; it performs no domain mutation. Model providers stay read-only, so
no model response can mutate tree data.

- ChangeProposal model + migration (status pending|applied|rejected, origin
  assistant|contributor, JSONB operations, reviewer + apply_error).
- Service: propose / list / get / apply (with optional edited ops) / reject /
  delete; a dispatcher mapping ops → editing services. v1 applies ops in order,
  not cross-op transactional (single-op is atomic; documented).
- API /trees/{id}/proposals + a frontend review page (approve/reject; editor-
  gated) and sidebar entry.

Tests: proposal doesn't apply until approved; reject doesn't apply; non-editor
member can see but not apply; multi-op; approve-with-edits; apply-error keeps it
pending. Full suite 87 passed; single alembic head.

Closes #214

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Justin Paul <justin@jpaul.me>
2026-06-09 15:44:40 -04:00

63 lines
2.6 KiB
Python

"""change_proposals (AI propose-then-confirm)
Revision ID: a1b2c3d4e5f6
Revises: d4a9c1e7b2f3
Create Date: 2026-06-09
"""
from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql
revision: str = "a1b2c3d4e5f6"
down_revision: str | None = "d4a9c1e7b2f3"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None
def upgrade() -> None:
op.create_table(
"change_proposals",
sa.Column("id", sa.Uuid(), nullable=False),
sa.Column("tree_id", sa.Uuid(), nullable=False),
sa.Column(
"status",
sa.Enum("pending", "applied", "rejected", name="change_proposal_status"),
server_default="pending",
nullable=False,
),
sa.Column(
"origin",
sa.Enum("assistant", "contributor", name="change_proposal_origin"),
server_default="assistant",
nullable=False,
),
sa.Column("created_by_user_id", sa.Uuid(), nullable=True),
sa.Column("summary", sa.String(length=512), nullable=False),
sa.Column("rationale", sa.Text(), nullable=True),
sa.Column("operations", postgresql.JSONB(astext_type=sa.Text()), nullable=False),
sa.Column("reviewed_by_user_id", sa.Uuid(), nullable=True),
sa.Column("reviewed_at", sa.DateTime(timezone=True), nullable=True),
sa.Column("review_note", sa.String(length=512), nullable=True),
sa.Column("apply_error", sa.Text(), nullable=True),
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
sa.ForeignKeyConstraint(["tree_id"], ["trees.id"], ondelete="CASCADE"),
sa.ForeignKeyConstraint(["created_by_user_id"], ["users.id"], ondelete="SET NULL"),
sa.ForeignKeyConstraint(["reviewed_by_user_id"], ["users.id"], ondelete="SET NULL"),
sa.PrimaryKeyConstraint("id"),
)
op.create_index("ix_change_proposals_tree_id", "change_proposals", ["tree_id"])
op.create_index("ix_change_proposals_status", "change_proposals", ["status"])
def downgrade() -> None:
op.drop_index("ix_change_proposals_status", table_name="change_proposals")
op.drop_index("ix_change_proposals_tree_id", table_name="change_proposals")
op.drop_table("change_proposals")
sa.Enum(name="change_proposal_status").drop(op.get_bind())
sa.Enum(name="change_proposal_origin").drop(op.get_bind())