Files
AgentLab/backend/app/records/listing.py
T
ThearchyHelios eddeded2a4 全栈安全加固、性能提速与架构演进:连接池与沙箱止血、双路混合检索、多版本归档及前端分包治理
- 第一批(安全加固与生命周期止血):隔离测试宿主机密钥与 .env 泄漏;修复 SQLite 取消查询挂死、:memory: 连接池报错与 EngineCache 世代竞态;补齐运行与评测删除时的 Checkpointer 线程、Artifact 磁盘文件与无主附件回收;加固三套沙箱路径越界、进程组清理与 Seatbelt 数据目录隔离,封堵 SSRF IPv4-mapped IPv6 元数据绕过与响应体 OOM;修复推理模型多轮 reasoning_content、结构化输出 Token 漏计与缓存折扣计费
- 第二批(数据库/集群/问数提速与首屏分包):补齐核心表索引与 SQLite WAL 调优,列表接口按需 defer 大列,收敛启动扫描并打通跨进程 slot 槽位唤醒;优化跨源全局选表、哈希桶数字绑定、流式 CSV 导入及多方言分析算子;剥离首屏静态引入的 @xyflow/react 与全局样式(首屏主包降至 461.59 kB),补充流式 rAF 攒批、Store 精细订阅与表单无障碍绑定
- 第三批(架构演进与工程自动化闭环):记录库超挂接阈值历史版本自动增量归档至单库多表(解除 SQLite 10 库 ATTACH 硬上限);重构知识库为词面 BM25 与全集合归一化向量矩阵真·双路独立召回并支持游标分批重建;升级多方言感知 SQL 模板词法分析(E'...' / $$...$$ / q'[...]' / # / 嵌套注释)与 Decimal / bool / NaN 参数校验,消除 ledger 层对 api 路由层的反向依赖;拆分 3101 行 CaliberDrafts 页面为 7 个领域子模块,消除检查脚本本机路径硬编码并新增 GitHub Actions CI 门禁与三批回归测试套件
2026-11-13 14:51:36 +08:00

109 lines
5.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""记录的逐行查看(组件 records_list、GET /api/apps/{id}/records,docs/design/normal-user-apps.md §3.7、§4.7)。
每次打开按看的人实时过滤,不走封存(同实例列表):use 只看自己提交的,observe、manage 看全部;关登录时看全部(没填署名、
又要只看自己的,分不出谁是谁,按全部给,同实例列表)。一张记录表的各版本合在一起列:新版本在前、每个版本里新提交的
在前;被更正过的行默认不列,history 打开时列出并标 superseded。
遮罩:别人提交的行里,数据源设了遮罩的列(「填什么」里标了敏感的,加上管理员在数据页手工加的)换成「已遮罩」——和看板证据
同一套遮罩列、同一个说法(P4、P5);自己提交的行照原样给(是自己填的)。附件列遮掉之后连文件的引用也不给。
返回的形状同 frontend/src/typesBlueprint.ts 的 RecordsPage,每个键都在:{table, version, columns, items, mine_only,
next_cursor, state}。next_cursor 是下一页的游标(「版本:序号」),没有下一页为 null;state 是 ok(有这张表,items 可能
为空)、empty(这个应用还没有记录表),草稿预览(apps/resolve._records_list)是 preview(不列真实的记录)。
"""
from __future__ import annotations
import asyncio
from typing import Any
from sqlalchemy.ext.asyncio import AsyncSession
from app.records import store
from app.records.access import scope_of_role
from app.records.tables import columns_of, path_of, tables_of_app, versions_of
#: 一页最多几行
PAGE_MAX = 200
async def _masked(session: AsyncSession, row: Any) -> set[str]:
"""这一版登记成的数据源设了遮罩的列(小写)。"""
from app.data.engine import masked_columns
from app.db.models import DataSource
source = await session.get(DataSource, row.source_id) if row.source_id else None
return {c.lower() for c in masked_columns(source.options if source is not None else None)}
def _shown(item: dict[str, Any], version: int, masked: set[str], viewer: str | None) -> dict[str, Any]:
"""一行交给看的人的样子:别人提交的行,遮罩列有值的换成「已遮罩」(P4)。"""
from app.engine.evidence_reader import MASKED
out = {**item, "_version": version}
if not masked or (viewer and item.get("_submitted_by") == viewer):
return out
values = item.get("values") or {}
out["values"] = {k: MASKED if k.lower() in masked and v is not None else v for k, v in values.items()}
return out
def _cursor(raw: str | None) -> tuple[int, int] | None:
if not raw:
return None
version, _, seq = str(raw).partition(":")
return (int(version), int(seq)) if version.isdigit() and seq.isdigit() else None
async def page(session: AsyncSession, app: Any, *, principal: Any, role: str | None, table: str | None = None,
who: str | None = None, cursor: str | None = None, history: bool = False,
limit: int = 50) -> dict[str, Any]:
tables = await tables_of_app(session, app.id)
current = next((t for t in tables if table in (None, "", t.table_key, t.id)), None)
if current is None and table:
# 也认数据源名(rec_…):模板里手上拿着的可能是它
from app.records.tables import find_table
found = await find_table(session, table)
current = next((t for t in tables if found is not None and t.table_key == found.table_key), None)
scope = scope_of_role(principal, role)
if who == "mine" and scope == "all":
scope = "mine"
viewer = principal.username
if scope == "mine" and not viewer and principal.mode == "off":
scope = "all"
if current is None:
return {"table": table, "version": 0, "columns": [], "items": [], "mine_only": scope == "mine",
"state": "empty", "next_cursor": None}
limit = max(1, min(int(limit), PAGE_MAX))
versions = await versions_of(session, current.table_key)
at = _cursor(cursor)
items: list[dict[str, Any]] = []
more = False
for row in versions:
if at is not None and row.version > at[0]:
continue
before = at[1] if at is not None and row.version == at[0] else None
cols = columns_of(row)
try:
got, more_here = await asyncio.to_thread(
store.rows, path_of(row), cols, scope=scope, viewer=viewer, history=history, before=before,
limit=limit - len(items))
except store.RecordsRefused:
continue
masked = await _masked(session, row)
items += [_shown(r, row.version, masked, viewer) for r in got]
if len(items) >= limit:
more = more_here or row is not versions[-1]
break
columns: list[dict[str, Any]] = []
seen: set[str] = set()
for row in versions:
for c in columns_of(row):
if c.name.lower() not in seen:
seen.add(c.name.lower())
columns.append({"name": c.name, "label": c.label or c.name, "type": c.type})
last = items[-1] if items else None
return {"table": current.table_key, "version": versions[0].version if versions else current.version,
"columns": columns, "items": items, "mine_only": scope == "mine",
"next_cursor": f"{last['_version']}:{last['_seq']}" if more and last else None, "state": "ok"}