sync update from LAN repo (source master @ ca48c4ec)

This commit is contained in:
gozeros committed 2026-11-05 10:53:54 +08:00
1 parent 8bc8b0e082
commit ea9dcff9c0
873 files changed
+107866 -14011

No files matched your search

@@ -29,6 +29,7 @@ Inspired by LangGraph Auth system: https://github.com/langchain-ai/langgraph/blo
from __future__ import annotations
import asyncio
import functools
import inspect
import logging
@@ -49,6 +50,15 @@ P = ParamSpec("P")
T = TypeVar("T")
async def _project_for_thread(project_store: Any, thread_id: str) -> dict | None:
"""Best-effort project binding lookup; a store error must not fail the read."""
try:
return await project_store.project_for_thread(thread_id)
except Exception:
logger.debug("project_for_thread failed for %s", thread_id, exc_info=True)
return None
async def _thread_checkpoint_exists(request: Request, thread_id: str) -> bool:
"""Return True if a LangGraph checkpoint exists for ``thread_id``.
@@ -269,8 +279,8 @@ def require_permission(
HTTPException 401: If authentication required but user is anonymous
HTTPException 403: If user lacks permission
HTTPException 404: If owner_check=True but user doesn't own the thread
ValueError: If owner_check=True but no ``thread_id`` is supplied,
either directly or on the Pydantic request body
ValueError: If owner_check=True but no ``thread_id`` is supplied,
either directly or on the Pydantic request body
"""
def decorator(func: Callable[P, T]) -> Callable[P, T]:
@@ -312,40 +322,45 @@ def require_permission(
# ``ThreadMetaStore.check_access``: it returns True for
# missing rows (untracked legacy thread) and for rows whose
# ``user_id`` is NULL (shared / pre-auth data), so this is
# strict-deny rather than strict-allow — only an *existing*
# row with a *different* user_id triggers 404.
if owner_check:
thread_id = kwargs.get("thread_id")
# Most thread-scoped routes expose ``thread_id`` in their URL.
# A small number of mutation endpoints (for example complete
# document rewrite) intentionally keep the target inside a
# Pydantic request body so their public URL stays stable. The
# FastAPI wrapper passes that body as ``body=...``; accept its
# Python field name here while preserving the exact same
# ownership verification below.
if thread_id is None:
body = kwargs.get("body")
thread_id = getattr(body, "thread_id", None)
if thread_id is None:
raise ValueError(
"require_permission with owner_check=True requires "
"a 'thread_id' parameter or body.thread_id"
)
# strict-deny rather than strict-allow — only an *existing*
# row with a *different* user_id triggers 404.
if owner_check:
thread_id = kwargs.get("thread_id")
# Most thread-scoped routes expose ``thread_id`` in their URL.
# A small number of mutation endpoints (for example complete
# document rewrite) intentionally keep the target inside a
# Pydantic request body so their public URL stays stable. The
# FastAPI wrapper passes that body as ``body=...``; accept its
# Python field name here while preserving the exact same
# ownership verification below.
if thread_id is None:
body = kwargs.get("body")
thread_id = getattr(body, "thread_id", None)
if thread_id is None:
raise ValueError(
"require_permission with owner_check=True requires "
"a 'thread_id' parameter or body.thread_id"
)
from app.gateway.deps import get_thread_store
thread_store = get_thread_store(request)
user_id = str(auth.user.id)
project_store = getattr(request.app.state, "analysis_project_store", None)
if project_store is not None:
project = await project_store.project_for_thread(thread_id)
if project and project["user_id"] != user_id:
raise HTTPException(status_code=404, detail="Project thread not accessible")
allowed = await thread_store.check_access(
user_id = str(auth.user.id)
project_store = getattr(request.app.state, "analysis_project_store", None)
allowed_coro = thread_store.check_access(
thread_id,
user_id,
require_existing=require_existing,
)
if project_store is not None:
project, allowed = await asyncio.gather(
_project_for_thread(project_store, thread_id),
allowed_coro,
)
if project and project["user_id"] != user_id:
raise HTTPException(status_code=404, detail="Project thread not accessible")
else:
allowed = await allowed_coro
if not allowed:
# Tell apart three denial causes so the client gets an
# accurate message and so DB-switch orphans can be adopted