05be7f9ad0
`ThreadMetaStore.check_access(thread_id, user_id, workspace_id, *, require_existing)` is now a three-positional method. Cross-workspace is denied unconditionally — even when the row exists and `user_id` matches — so the decorator layer can convert the False into a **404** and never leak the existence of a thread across tenants. Inside the workspace, the existing legacy semantics still hold (NULL `row.user_id` stays "shared in workspace", `require_existing` still gates the missing-row path against ghost-row re-targeting). `@require_permission(owner_check=True)` in `app/gateway/authz.py` now reads the active workspace from `get_effective_workspace_id()` (set by PR4 AuthMiddleware; falls back to "default" in no-auth dev) and passes it through. The existing 404-not-403 mapping is unchanged. Existing positional callers in `test_thread_meta_repo.py` and the permissive mock in `test_threads_router.py` were updated for the new arity. 58 thread_meta / router / memory tests stay green; 90 auth / uploads / suggestions tests stay green.
213 lines
7.7 KiB
Python
213 lines
7.7 KiB
Python
"""In-memory ThreadMetaStore backed by LangGraph BaseStore.
|
|
|
|
Used when database.backend=memory. Delegates to the LangGraph Store's
|
|
``("threads",)`` namespace — the same namespace used by the Gateway
|
|
router for thread records.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from langgraph.store.base import BaseStore
|
|
|
|
from deerflow.persistence.thread_meta.base import ThreadMetaStore
|
|
from deerflow.runtime.user_context import AUTO, _AutoSentinel, resolve_user_id
|
|
from deerflow.runtime.workspace_context import AUTO as WORKSPACE_AUTO
|
|
from deerflow.runtime.workspace_context import (
|
|
_AutoSentinel as _WorkspaceAutoSentinel,
|
|
)
|
|
from deerflow.runtime.workspace_context import (
|
|
resolve_workspace_id,
|
|
)
|
|
from deerflow.utils.time import coerce_iso, now_iso
|
|
|
|
THREADS_NS: tuple[str, ...] = ("threads",)
|
|
|
|
|
|
class MemoryThreadMetaStore(ThreadMetaStore):
|
|
def __init__(self, store: BaseStore) -> None:
|
|
self._store = store
|
|
|
|
async def _get_owned_record(
|
|
self,
|
|
thread_id: str,
|
|
user_id: str | None | _AutoSentinel,
|
|
workspace_id: str | None | _WorkspaceAutoSentinel,
|
|
method_name: str,
|
|
) -> dict | None:
|
|
"""Fetch a record and verify workspace + ownership. Returns a mutable copy, or None."""
|
|
resolved_user = resolve_user_id(user_id, method_name=method_name)
|
|
resolved_workspace = resolve_workspace_id(workspace_id, method_name=method_name)
|
|
item = await self._store.aget(THREADS_NS, thread_id)
|
|
if item is None:
|
|
return None
|
|
record = dict(item.value)
|
|
if resolved_workspace is not None and record.get("workspace_id") != resolved_workspace:
|
|
return None
|
|
if resolved_user is not None and record.get("user_id") != resolved_user:
|
|
return None
|
|
return record
|
|
|
|
async def create(
|
|
self,
|
|
thread_id: str,
|
|
*,
|
|
assistant_id: str | None = None,
|
|
user_id: str | None | _AutoSentinel = AUTO,
|
|
workspace_id: str | None | _WorkspaceAutoSentinel = WORKSPACE_AUTO,
|
|
display_name: str | None = None,
|
|
metadata: dict | None = None,
|
|
) -> dict:
|
|
resolved_user_id = resolve_user_id(user_id, method_name="MemoryThreadMetaStore.create")
|
|
resolved_workspace_id = resolve_workspace_id(workspace_id, method_name="MemoryThreadMetaStore.create")
|
|
now = now_iso()
|
|
record: dict[str, Any] = {
|
|
"thread_id": thread_id,
|
|
"assistant_id": assistant_id,
|
|
"user_id": resolved_user_id,
|
|
"workspace_id": resolved_workspace_id,
|
|
"display_name": display_name,
|
|
"status": "idle",
|
|
"metadata": metadata or {},
|
|
"values": {},
|
|
"created_at": now,
|
|
"updated_at": now,
|
|
}
|
|
await self._store.aput(THREADS_NS, thread_id, record)
|
|
return record
|
|
|
|
async def get(
|
|
self,
|
|
thread_id: str,
|
|
*,
|
|
user_id: str | None | _AutoSentinel = AUTO,
|
|
workspace_id: str | None | _WorkspaceAutoSentinel = WORKSPACE_AUTO,
|
|
) -> dict | None:
|
|
return await self._get_owned_record(thread_id, user_id, workspace_id, "MemoryThreadMetaStore.get")
|
|
|
|
async def search(
|
|
self,
|
|
*,
|
|
metadata: dict | None = None,
|
|
status: str | None = None,
|
|
limit: int = 100,
|
|
offset: int = 0,
|
|
user_id: str | None | _AutoSentinel = AUTO,
|
|
workspace_id: str | None | _WorkspaceAutoSentinel = WORKSPACE_AUTO,
|
|
) -> list[dict]:
|
|
resolved_user_id = resolve_user_id(user_id, method_name="MemoryThreadMetaStore.search")
|
|
resolved_workspace_id = resolve_workspace_id(workspace_id, method_name="MemoryThreadMetaStore.search")
|
|
filter_dict: dict[str, Any] = {}
|
|
if metadata:
|
|
filter_dict.update(metadata)
|
|
if status:
|
|
filter_dict["status"] = status
|
|
if resolved_workspace_id is not None:
|
|
filter_dict["workspace_id"] = resolved_workspace_id
|
|
if resolved_user_id is not None:
|
|
filter_dict["user_id"] = resolved_user_id
|
|
|
|
items = await self._store.asearch(
|
|
THREADS_NS,
|
|
filter=filter_dict or None,
|
|
limit=limit,
|
|
offset=offset,
|
|
)
|
|
return [self._item_to_dict(item) for item in items]
|
|
|
|
async def check_access(
|
|
self,
|
|
thread_id: str,
|
|
user_id: str,
|
|
workspace_id: str,
|
|
*,
|
|
require_existing: bool = False,
|
|
) -> bool:
|
|
item = await self._store.aget(THREADS_NS, thread_id)
|
|
if item is None:
|
|
return not require_existing
|
|
record_workspace_id = item.value.get("workspace_id")
|
|
if record_workspace_id is not None and record_workspace_id != workspace_id:
|
|
return False
|
|
record_user_id = item.value.get("user_id")
|
|
if record_user_id is None:
|
|
return True
|
|
return record_user_id == user_id
|
|
|
|
async def update_display_name(
|
|
self,
|
|
thread_id: str,
|
|
display_name: str,
|
|
*,
|
|
user_id: str | None | _AutoSentinel = AUTO,
|
|
workspace_id: str | None | _WorkspaceAutoSentinel = WORKSPACE_AUTO,
|
|
) -> None:
|
|
record = await self._get_owned_record(thread_id, user_id, workspace_id, "MemoryThreadMetaStore.update_display_name")
|
|
if record is None:
|
|
return
|
|
record["display_name"] = display_name
|
|
record["updated_at"] = now_iso()
|
|
await self._store.aput(THREADS_NS, thread_id, record)
|
|
|
|
async def update_status(
|
|
self,
|
|
thread_id: str,
|
|
status: str,
|
|
*,
|
|
user_id: str | None | _AutoSentinel = AUTO,
|
|
workspace_id: str | None | _WorkspaceAutoSentinel = WORKSPACE_AUTO,
|
|
) -> None:
|
|
record = await self._get_owned_record(thread_id, user_id, workspace_id, "MemoryThreadMetaStore.update_status")
|
|
if record is None:
|
|
return
|
|
record["status"] = status
|
|
record["updated_at"] = now_iso()
|
|
await self._store.aput(THREADS_NS, thread_id, record)
|
|
|
|
async def update_metadata(
|
|
self,
|
|
thread_id: str,
|
|
metadata: dict,
|
|
*,
|
|
user_id: str | None | _AutoSentinel = AUTO,
|
|
workspace_id: str | None | _WorkspaceAutoSentinel = WORKSPACE_AUTO,
|
|
) -> None:
|
|
record = await self._get_owned_record(thread_id, user_id, workspace_id, "MemoryThreadMetaStore.update_metadata")
|
|
if record is None:
|
|
return
|
|
merged = dict(record.get("metadata") or {})
|
|
merged.update(metadata)
|
|
record["metadata"] = merged
|
|
record["updated_at"] = now_iso()
|
|
await self._store.aput(THREADS_NS, thread_id, record)
|
|
|
|
async def delete(
|
|
self,
|
|
thread_id: str,
|
|
*,
|
|
user_id: str | None | _AutoSentinel = AUTO,
|
|
workspace_id: str | None | _WorkspaceAutoSentinel = WORKSPACE_AUTO,
|
|
) -> None:
|
|
record = await self._get_owned_record(thread_id, user_id, workspace_id, "MemoryThreadMetaStore.delete")
|
|
if record is None:
|
|
return
|
|
await self._store.adelete(THREADS_NS, thread_id)
|
|
|
|
@staticmethod
|
|
def _item_to_dict(item) -> dict[str, Any]:
|
|
"""Convert a Store SearchItem to the dict format expected by callers."""
|
|
val = item.value
|
|
return {
|
|
"thread_id": item.key,
|
|
"assistant_id": val.get("assistant_id"),
|
|
"user_id": val.get("user_id"),
|
|
"display_name": val.get("display_name"),
|
|
"status": val.get("status", "idle"),
|
|
"metadata": val.get("metadata", {}),
|
|
# ``coerce_iso`` heals legacy unix-second values written by
|
|
# earlier Gateway versions that called ``str(time.time())``.
|
|
"created_at": coerce_iso(val.get("created_at", "")),
|
|
"updated_at": coerce_iso(val.get("updated_at", "")),
|
|
}
|