feat(persistence): PR6 T6.2 — ThreadMetaRepository.get filters by workspace_id

`get()` accepts `workspace_id: str | None | _AutoSentinel = AUTO` and
moves the workspace check into the SQL WHERE so cross-workspace lookups
short-circuit without loading the row. `MemoryThreadMetaStore._get_owned_record`
gets the same filter for parity.

The user_id check stays as a post-load comparison (preserves the existing
shared-row semantics where row.user_id IS NULL means "everyone in this
workspace"). Cross-workspace always returns None, never the row.

3 new tests cover the three states: in-workspace get returns the row,
out-of-workspace get returns None even when user_id matches, explicit
workspace_id=None bypasses (migration / CLI). Existing 22 thread_meta
tests stay green.
This commit is contained in:
1445043649
2026-05-13 17:19:00 +08:00
parent 361e653d37
commit 296a4f1950
3 changed files with 72 additions and 7 deletions
@@ -102,3 +102,53 @@ class TestCreateWorkspace:
record = await repo.create("t1", workspace_id=None)
assert record["workspace_id"] is None
await _cleanup()
class TestGetWorkspace:
@pytest.mark.anyio
async def test_get_filters_by_workspace(self, tmp_path):
"""Cross-workspace get returns None even when user_id matches."""
repo = await _make_repo(tmp_path, workspaces=("ws-alpha", "ws-beta"))
token = _use_workspace("ws-alpha")
try:
await repo.create("t1", user_id="alice")
finally:
reset_current_workspace(token)
token = _use_workspace("ws-beta")
try:
assert await repo.get("t1", user_id="alice") is None
finally:
reset_current_workspace(token)
await _cleanup()
@pytest.mark.anyio
async def test_get_returns_row_in_same_workspace(self, tmp_path):
repo = await _make_repo(tmp_path, workspaces=("ws-alpha",))
token = _use_workspace("ws-alpha")
try:
await repo.create("t1", user_id="alice")
record = await repo.get("t1", user_id="alice")
finally:
reset_current_workspace(token)
await _cleanup()
assert record is not None
assert record["thread_id"] == "t1"
assert record["workspace_id"] == "ws-alpha"
@pytest.mark.anyio
async def test_get_workspace_none_bypasses_filter(self, tmp_path):
"""Explicit workspace_id=None lets migration scripts see any row."""
repo = await _make_repo(tmp_path, workspaces=("ws-alpha",))
token = _use_workspace("ws-alpha")
try:
await repo.create("t1", user_id="alice")
finally:
reset_current_workspace(token)
token = _use_workspace("ws-beta")
try:
assert await repo.get("t1", user_id=None, workspace_id=None) is not None
finally:
reset_current_workspace(token)
await _cleanup()