feat(l1): l1_session_service record_step + update_notes
record_step appends to walked_path JSONB and advances current_node_id on flow/proposal walks; refuses adhoc sessions. update_notes replaces walk_notes (used by adhoc walks for debounced autosave); 256KB size cap to prevent unbounded JSONB growth. Both reject non-active sessions. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
start_* functions live in T12; step/notes are T13; resolve/escalate are T14.
|
||||
"""
|
||||
import json
|
||||
from datetime import datetime, timezone
|
||||
from typing import Optional
|
||||
from uuid import UUID
|
||||
|
||||
@@ -91,3 +93,59 @@ async def start_adhoc_session(
|
||||
db.add(session)
|
||||
await db.flush()
|
||||
return session
|
||||
|
||||
|
||||
async def record_step(
|
||||
db: AsyncSession,
|
||||
*,
|
||||
session_id: UUID,
|
||||
node_id: str,
|
||||
question: str,
|
||||
answer: str,
|
||||
note: Optional[str] = None,
|
||||
) -> L1WalkSession:
|
||||
"""Record an answered step in a tree walk. Appends to walked_path JSONB and
|
||||
advances current_node_id. Raises ValueError on adhoc sessions or inactive
|
||||
sessions. Updates last_step_at."""
|
||||
session = await db.get(L1WalkSession, session_id)
|
||||
if not session:
|
||||
raise ValueError(f"L1WalkSession {session_id} not found")
|
||||
if session.session_kind == "adhoc":
|
||||
raise ValueError("Cannot record step on adhoc session — use update_notes")
|
||||
if session.status != "active":
|
||||
raise ValueError(f"Session {session_id} is not active (status={session.status})")
|
||||
entry = {
|
||||
"node_id": node_id,
|
||||
"question": question,
|
||||
"answer": answer,
|
||||
"l1_note": note,
|
||||
}
|
||||
# JSONB requires assigning a new list — in-place mutation isn't tracked
|
||||
session.walked_path = [*session.walked_path, entry]
|
||||
session.current_node_id = node_id
|
||||
session.last_step_at = datetime.now(timezone.utc)
|
||||
await db.flush()
|
||||
return session
|
||||
|
||||
|
||||
async def update_notes(
|
||||
db: AsyncSession,
|
||||
*,
|
||||
session_id: UUID,
|
||||
notes: list[dict],
|
||||
) -> L1WalkSession:
|
||||
"""Replace walk_notes on an active session. Used by adhoc walks for
|
||||
debounced autosave. Raises ValueError if missing or inactive. Caps notes
|
||||
payload at 256KB to prevent unbounded growth."""
|
||||
session = await db.get(L1WalkSession, session_id)
|
||||
if not session:
|
||||
raise ValueError(f"L1WalkSession {session_id} not found")
|
||||
if session.status != "active":
|
||||
raise ValueError(f"Session {session_id} is not active (status={session.status})")
|
||||
encoded_size = len(json.dumps(notes).encode("utf-8"))
|
||||
if encoded_size > 256 * 1024:
|
||||
raise ValueError("walk_notes exceeds 256KB cap — consider escalating")
|
||||
session.walk_notes = notes
|
||||
session.last_step_at = datetime.now(timezone.utc)
|
||||
await db.flush()
|
||||
return session
|
||||
|
||||
Reference in New Issue
Block a user