Files that outlived the chats that held them, and a page that led with its footnotes
The second audit pass. Four things, and the first two were reported. The Prompts page put a screen of variables and a screen of preview above the editor, so the tabs began two screens down and switching one had to drag the whole page to be any use -- and on a short tab it could not drag far enough, leaving the panel stranded above a screenful of nothing. Editor first, reference after, bar sticky. Custom themes were three fixed slots: fifty-seven empty colour boxes on a fresh instance and no way to make a fourth theme. One block per theme plus a blank one, colours behind a disclosure. Both measured rather than argued about -- rendered through TestClient and driven under headless Chromium, where the tab bar moved 385->642px before and does not move now, and the themes page went from 5495px to 2820px. Asking where generated images go found the other two. Deleting a chat cascades to the attachment rows and leaves every file on disk; the helper written for exactly that was called from one place, and it was not the delete button, a schedule's chat, a helper's chat or deleting an account. Underneath it, `claim` bound message_id and never chat_id, so anything picked before a chat existed kept an empty chat_id forever -- which six readers filter on, so those files were also unnamed in the prompt, unopenable in the canvas, and invisible to the one caller the cleanup had. And folders nest now. The route has handled parent_id since folders existed, with a cycle guard and a depth cap the move path never applied; the sidebar has always drawn a tree. Nothing could ask for one. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from fastapi import APIRouter, Depends, Form, HTTPException, Request, Response, status
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session as DBSession
|
||||
|
||||
from lembas.api.deps import Db, RequiredUser, require_permission
|
||||
@@ -36,6 +37,62 @@ def _depth_of(db: DBSession, folder: Folder | None) -> int:
|
||||
return depth
|
||||
|
||||
|
||||
def _descendants(db: DBSession, folder: Folder) -> set[str]:
|
||||
"""Every folder under this one, and this one. Bounded by MAX_DEPTH."""
|
||||
found = {folder.id}
|
||||
frontier = [folder.id]
|
||||
for _ in range(MAX_DEPTH + 1):
|
||||
if not frontier:
|
||||
break
|
||||
children = list(
|
||||
db.scalars(select(Folder).where(Folder.parent_id.in_(frontier)))
|
||||
)
|
||||
frontier = [c.id for c in children if c.id not in found]
|
||||
found.update(frontier)
|
||||
return found
|
||||
|
||||
|
||||
def _subtree_height(db: DBSession, folder: Folder) -> int:
|
||||
"""How many levels this folder's own subtree occupies, itself included.
|
||||
|
||||
A move has to consider it: the constraint is on the *deepest leaf* after the
|
||||
move, not on the folder being dragged.
|
||||
"""
|
||||
height = 1
|
||||
frontier = [folder.id]
|
||||
for _ in range(MAX_DEPTH + 1):
|
||||
children = list(
|
||||
db.scalars(select(Folder.id).where(Folder.parent_id.in_(frontier)))
|
||||
)
|
||||
if not children:
|
||||
break
|
||||
height += 1
|
||||
frontier = children
|
||||
return height
|
||||
|
||||
|
||||
def candidate_parents(db: DBSession, user_id: str, folder: Folder) -> list[Folder]:
|
||||
"""Folders this one could be moved into.
|
||||
|
||||
Everything the person owns, minus the folder itself and its own subtree --
|
||||
which is the cycle guard in `update_folder` stated as a list rather than as
|
||||
a refusal. A picker that offers a move the route will reject is a control
|
||||
that looks like it works.
|
||||
|
||||
Depth is checked at the route rather than filtered here: it depends on how
|
||||
tall *this* folder's subtree is, and a select that silently omitted a folder
|
||||
for that reason would be unexplainable from the screen.
|
||||
"""
|
||||
blocked = _descendants(db, folder)
|
||||
return [
|
||||
candidate
|
||||
for candidate in db.scalars(
|
||||
select(Folder).where(Folder.user_id == user_id).order_by(Folder.name)
|
||||
)
|
||||
if candidate.id not in blocked
|
||||
]
|
||||
|
||||
|
||||
def _refresh_sidebar() -> Response:
|
||||
"""Tell the browser to reload so the tree re-renders.
|
||||
|
||||
@@ -141,6 +198,17 @@ async def update_folder(
|
||||
"A folder cannot be moved inside itself.",
|
||||
)
|
||||
cursor = db.get(Folder, cursor.parent_id) if cursor.parent_id else None
|
||||
# And the depth cap, which `create_folder` has always applied and this
|
||||
# path never did -- moving a three-deep subtree under a six-deep folder
|
||||
# builds a tree nine deep, which is what MAX_DEPTH exists to keep out of
|
||||
# the recursive sidebar template. It went unnoticed because nothing in
|
||||
# the interface could submit `parent_id` at all until now.
|
||||
subtree = _subtree_height(db, folder)
|
||||
if new_parent is not None and _depth_of(db, new_parent) + subtree > MAX_DEPTH:
|
||||
raise HTTPException(
|
||||
status.HTTP_400_BAD_REQUEST,
|
||||
f"Folders cannot be nested more than {MAX_DEPTH} deep.",
|
||||
)
|
||||
folder.parent_id = new_parent.id if new_parent else None
|
||||
|
||||
if "collapsed" in form:
|
||||
|
||||
Reference in New Issue
Block a user