fix: 智能体可见性对齐——列表可见即可打开问答,消除 404

get_visible 此前比列表过滤更严格(仅 内置/本人/已发布),导致私密广场
智能体(转私密强制 published=false)和「我的」里已收藏但被取消发布的
智能体能在列表看到、点开问答却 404(前端连带报 X.map is not a function)。

- AgentRepository._visible_filter / MemoryAgentStore._is_fetchable:
  单条读取放宽为所有列表口径的并集(+is_private 豁免、+本人收藏豁免)
- 详情接口与 _enforce_agent_access 运行门禁:管理员回退 get_any,
  对齐 ?admin=true 全量列表
- 普通未发布草稿对陌生人仍不可见,隐私不放开
- 本地新增 tests/test_agent_visibility.py 覆盖 SQL/Memory 双实现
  (tests/ 目录在本仓库被 gitignore,不随提交)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
吉鑫升andClaude Fable 5 committed 2026-06-11 21:25:36 +08:00
1 parent 0b98d18483
commit e7b51a30a3
5 files changed
+45 -9

No files matched your search

@@ -723,6 +723,10 @@ async def get_agent(agent_id: str, request: Request) -> AgentResponse:
await _maybe_sync_legacy_agents(store)
user_id = _current_user_id(request)
record = await store.get_visible(agent_id, user_id)
if record is None and _is_admin_user(request):
# The admin console lists every agent (?admin=true), so an admin
# must be able to open any of them — mirror that here.
record = await store.get_any(agent_id)
if record is None:
raise HTTPException(status_code=404, detail=f"Agent '{agent_id}' not found")
# Detail view legitimately reads SOUL.md + config.yaml — hop to a
@@ -477,6 +477,9 @@ async def _enforce_agent_access(body: Any, request: Request) -> None:
store = get_agent_store(request)
await _maybe_sync_legacy_agent_records(store)
user_id = get_effective_user_id()
# Admins see every agent in the admin console listing (?admin=true), so
# they may also start a run against any of them.
is_admin = getattr(getattr(request.state, "user", None), "system_role", None) == "admin"
for agent_id in agent_ids:
try:
normalized = validate_agent_id(agent_id)
@@ -484,8 +487,11 @@ async def _enforce_agent_access(body: Any, request: Request) -> None:
raise HTTPException(status_code=422, detail=str(exc)) from exc
if normalized is None:
continue
if await store.get_visible(normalized, user_id) is None:
raise HTTPException(status_code=404, detail=f"Agent {normalized} not found")
if await store.get_visible(normalized, user_id) is not None:
continue
if is_admin and await store.get_any(normalized) is not None:
continue
raise HTTPException(status_code=404, detail=f"Agent {normalized} not found")
# ---------------------------------------------------------------------------