fix(l1): resolve PR #193 backend review findings (1,4,5,6,7,8,9,10)

Server-assigns a uuid4 id to every AI-generated node (Finding 1 showstopper:
nodes had no id but the advance protocol keys on node_id, so ai_build walks
never advanced past question 1). Replaces the hidden {"node_type":"meta"}
walked_path convention with real category/problem_text/pending_node columns on
l1_walk_sessions (migration 61dda4f615c6) — fixes junk proposals + off-by-one
depth cap (Findings 8,9), and pending_node replays the served node on re-mount
(no duplicate paid LLM call). Intake honors explicit flow_id and adhoc=True
(Findings 4,5); flow_proposals.l1_session_id FK -> CASCADE (Finding 6 time
bomb); L1 category GET is owner+admin like PATCH and require_account_owner_or_admin
delegates to User.can_manage_account (Finding 7); escalate falls back to default
recipients + filters deleted_at + warns when empty (Finding 10). Cleanups: dead
ticket_ref removed, IntakeResponse per-outcome validator, unused acknowledged
dropped, escalations partial index, restored a deleted audit assertion.

Full Phase 2A backend set: 110 passed / 0 failed.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-09 15:55:45 -04:00
parent 42a4536c63
commit ac89e7b2fa
17 changed files with 592 additions and 80 deletions

View File

@@ -0,0 +1,92 @@
"""l1 ai_build columns (category/problem_text/pending_node) + l1_session FK cascade
Two changes that ship together for the Phase 2A L1 AI tree builder:
1. Add real ``category`` / ``problem_text`` / ``pending_node`` columns to
``l1_walk_sessions``. These replace the former hidden
``{"node_type": "meta"}`` walked_path entry that smuggled the intake category:
that convention leaked into every consumer that forgot to skip it (junk
proposals, off-by-one depth cap, blank escalation rows). ``pending_node``
persists the served-but-unanswered node so a refresh / StrictMode double-mount
replays it instead of firing a fresh paid LLM call.
2. Flip ``flow_proposals.l1_session_id`` FK from SET NULL to CASCADE. Under the
exactly-one-source CHECK an L1-sourced proposal has ``source_session_id`` NULL,
so a SET NULL on l1_session deletion would NULL both columns and the
non-deferrable CHECK would abort the DELETE — making the session undeletable.
Also adds a partial index for the engineer escalations list.
Revision ID: 61dda4f615c6
Revises: 1fd88a68b145
Create Date: 2026-06-09
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic.
revision: str = '61dda4f615c6'
down_revision: Union[str, None] = '1fd88a68b145'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# 1. New ai_build context columns on l1_walk_sessions.
op.add_column(
"l1_walk_sessions",
sa.Column("category", sa.String(length=100), nullable=True),
)
op.add_column(
"l1_walk_sessions",
sa.Column("problem_text", sa.Text(), nullable=True),
)
op.add_column(
"l1_walk_sessions",
sa.Column("pending_node", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
)
# Partial index for GET /l1/escalations (engineer handoff queue).
op.create_index(
"ix_l1_walk_sessions_escalated",
"l1_walk_sessions",
["account_id", sa.text("last_step_at DESC")],
postgresql_where=sa.text("status = 'escalated'"),
)
# 2. flow_proposals.l1_session_id: SET NULL -> CASCADE.
op.drop_constraint(
"fk_flow_proposals_l1_session_id", "flow_proposals", type_="foreignkey"
)
op.create_foreign_key(
"fk_flow_proposals_l1_session_id",
"flow_proposals",
"l1_walk_sessions",
["l1_session_id"],
["id"],
ondelete="CASCADE",
)
def downgrade() -> None:
op.drop_constraint(
"fk_flow_proposals_l1_session_id", "flow_proposals", type_="foreignkey"
)
op.create_foreign_key(
"fk_flow_proposals_l1_session_id",
"flow_proposals",
"l1_walk_sessions",
["l1_session_id"],
["id"],
ondelete="SET NULL",
)
op.drop_index("ix_l1_walk_sessions_escalated", table_name="l1_walk_sessions")
op.drop_column("l1_walk_sessions", "pending_node")
op.drop_column("l1_walk_sessions", "problem_text")
op.drop_column("l1_walk_sessions", "category")