feat: add tree forking, custom step tracking, and session sharing

Implement three foundational schema features from the design doc:

- Tree forking with lineage tracking (migration 022): parent_tree_id,
  root_tree_id, fork_depth columns with self-referential FKs and
  composite analytics index
- Custom step enhancement: CustomStepSchema with source tracking
  (ad-hoc, step-library, forked-tree) for backward-compatible JSONB
- Session sharing (migration 023): session_shares and session_share_views
  tables with account-scoped visibility, cryptographic tokens, view
  tracking, and allow_public_shares account policy

Includes 21 new integration tests (9 forking, 12 sharing), SaaS
consultant-recommended denormalizations, rate limiting on public share
access, and test fixture fix for invite code requirement.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Michael Chihlas
2026-02-07 19:10:47 -05:00
parent c8e7aaad1a
commit ffb14cd014
16 changed files with 1345 additions and 8 deletions

View File

@@ -0,0 +1,73 @@
"""add tree forking support with lineage tracking
Revision ID: 022
Revises: 021
Create Date: 2026-02-07
Adds fork relationship and lineage tracking to trees table:
- parent_tree_id: Points to immediate parent tree (NULL for root trees)
- fork_reason: Optional engineer note explaining fork purpose
- parent_updated_at: Snapshot of parent's updated_at at fork time
- root_tree_id: Points to original tree at root of fork chain
- fork_depth: How many forks deep (1 = direct fork, 2 = fork of fork, etc.)
Fork on parent delete behavior: SET NULL (orphaned forks survive)
Root tree delete behavior: SET NULL (lineage preserved via fork_depth)
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects.postgresql import UUID
# revision identifiers, used by Alembic.
revision: str = '022'
down_revision: Union[str, None] = '021'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# Add fork tracking columns
op.add_column('trees', sa.Column('parent_tree_id', UUID(as_uuid=True), nullable=True))
op.add_column('trees', sa.Column('fork_reason', sa.String(255), nullable=True))
op.add_column('trees', sa.Column('parent_updated_at', sa.DateTime(timezone=True), nullable=True))
# Add fork lineage columns
op.add_column('trees', sa.Column('root_tree_id', UUID(as_uuid=True), nullable=True))
op.add_column('trees', sa.Column('fork_depth', sa.Integer, nullable=False, server_default='0'))
# Add foreign key constraints
op.create_foreign_key(
'fk_trees_parent_tree_id',
'trees', 'trees',
['parent_tree_id'], ['id'],
ondelete='SET NULL'
)
op.create_foreign_key(
'fk_trees_root_tree_id',
'trees', 'trees',
['root_tree_id'], ['id'],
ondelete='SET NULL'
)
# Add indexes for fork queries
op.create_index('ix_trees_parent_tree_id', 'trees', ['parent_tree_id'])
op.create_index('ix_trees_root_tree_id', 'trees', ['root_tree_id'])
# Composite index for fork analytics (descendants + depth sorting)
op.create_index('ix_trees_fork_analytics', 'trees', ['root_tree_id', 'fork_depth'])
def downgrade() -> None:
op.drop_index('ix_trees_fork_analytics', table_name='trees')
op.drop_index('ix_trees_root_tree_id', table_name='trees')
op.drop_index('ix_trees_parent_tree_id', table_name='trees')
op.drop_constraint('fk_trees_root_tree_id', 'trees', type_='foreignkey')
op.drop_constraint('fk_trees_parent_tree_id', 'trees', type_='foreignkey')
op.drop_column('trees', 'fork_depth')
op.drop_column('trees', 'root_tree_id')
op.drop_column('trees', 'parent_updated_at')
op.drop_column('trees', 'fork_reason')
op.drop_column('trees', 'parent_tree_id')

View File

@@ -0,0 +1,89 @@
"""add session sharing tables
Revision ID: 023
Revises: 022
Create Date: 2026-02-07
Adds session sharing infrastructure:
- session_shares: Share tokens with visibility control (public/account-only)
- session_share_views: Detailed view tracking with viewer identification
- accounts.allow_public_shares: Account-level policy for public share creation
Includes SaaS consultant-recommended denormalizations:
- account_id on session_shares (faster access control, survives user transfers)
- session_id on session_share_views (faster analytics queries)
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects.postgresql import UUID
# revision identifiers, used by Alembic.
revision: str = '023'
down_revision: Union[str, None] = '022'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# Create session_shares table
op.create_table(
'session_shares',
sa.Column('id', UUID(as_uuid=True), primary_key=True, server_default=sa.text('gen_random_uuid()')),
sa.Column('session_id', UUID(as_uuid=True), nullable=False),
sa.Column('account_id', UUID(as_uuid=True), nullable=False),
sa.Column('share_token', sa.String(64), nullable=False, unique=True),
sa.Column('share_name', sa.String(100), nullable=True),
sa.Column('visibility', sa.String(20), nullable=False, server_default='public'),
sa.Column('created_by', UUID(as_uuid=True), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False, server_default=sa.text('now()')),
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=False, server_default=sa.text('now()')),
sa.Column('expires_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('view_count', sa.Integer, nullable=False, server_default='0'),
sa.Column('last_viewed_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('is_active', sa.Boolean, nullable=False, server_default='true'),
sa.ForeignKeyConstraint(['session_id'], ['sessions.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['account_id'], ['accounts.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['created_by'], ['users.id'], ondelete='CASCADE'),
sa.CheckConstraint("visibility IN ('public', 'account')", name='ck_session_shares_visibility')
)
# Create indexes for session_shares
op.create_index('ix_session_shares_session_id', 'session_shares', ['session_id'])
op.create_index('ix_session_shares_account_id', 'session_shares', ['account_id'])
op.create_index('ix_session_shares_share_token', 'session_shares', ['share_token'])
op.create_index('ix_session_shares_created_by', 'session_shares', ['created_by'])
op.create_index('ix_session_shares_expires_at', 'session_shares', ['expires_at'])
op.create_index('ix_session_shares_is_active', 'session_shares', ['is_active'])
# Create session_share_views table
op.create_table(
'session_share_views',
sa.Column('id', UUID(as_uuid=True), primary_key=True, server_default=sa.text('gen_random_uuid()')),
sa.Column('share_id', UUID(as_uuid=True), nullable=False),
sa.Column('session_id', UUID(as_uuid=True), nullable=False),
sa.Column('viewer_id', UUID(as_uuid=True), nullable=True),
sa.Column('viewed_at', sa.DateTime(timezone=True), nullable=False, server_default=sa.text('now()')),
sa.Column('viewer_ip', sa.String(45), nullable=True),
sa.Column('viewer_user_agent', sa.String(500), nullable=True),
sa.ForeignKeyConstraint(['share_id'], ['session_shares.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['session_id'], ['sessions.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['viewer_id'], ['users.id'], ondelete='SET NULL')
)
# Create indexes for session_share_views
op.create_index('ix_session_share_views_share_id', 'session_share_views', ['share_id'])
op.create_index('ix_session_share_views_session_id', 'session_share_views', ['session_id'])
op.create_index('ix_session_share_views_viewer_id', 'session_share_views', ['viewer_id'])
op.create_index('ix_session_share_views_viewed_at', 'session_share_views', ['viewed_at'])
# Add account policy for public shares
op.add_column('accounts', sa.Column('allow_public_shares', sa.Boolean, nullable=False, server_default='true'))
def downgrade() -> None:
op.drop_column('accounts', 'allow_public_shares')
op.drop_table('session_share_views')
op.drop_table('session_shares')