普通用户建应用集成收尾:

- 问数据和个人应用的图放行绑定只读查询等合规工具的调用工具节点,只读和遮罩在这条路径上生效;被治理成写入的工具在只读运行里直接拒;知识库范围在工具层固定(图级默认值、参数名纠错、Agent 都改不了)
- 个人应用的刷新和个人蓝图的试运行只有拥有者能读:运行、事件、证据、工件、文件、运行与审批列表、对比、可验证报告、质询、假设分析、提取工作流一并收紧;个人蓝图的试运行不论谁发起都按个人应用约束
- 共享蓝图只给拥有者、对应应用的管理者、管理员,看不到的一律 404;审核副本归审核人;管理员看别人的个人应用只有元数据、可以归档
- 记录表跨版本汇总(只读挂载旧版本、按当前列 UNION ALL、各版本各自固定高水位,缺的版本照实写进印章);停用前发起的运行可以继续写旧版本;更正沿用原提交人、更正人记审计
- 约定收口:待办种类 review、事件 file.extracted、表单错误 file、模板定义带示例与预置;前端类型一次补齐、去掉临时的局部类型;审核待办卡不再给打不开的链接;读附件事件有中文说法;检查脚本补断言
This commit is contained in:
ThearchyHelios
2026-10-15 13:55:19 +08:00
parent a702a5c14b
commit 7739340607
95 changed files with 3107 additions and 611 deletions
+81 -28
View File
@@ -1,14 +1,17 @@
"""运行、工件的读权限(§3.12「运行、证据、工件(只读)」那一行)。
关登录、builder 及以上:都能读。开登录后 user 角色只能读:
- 自己发起的运行;
- 自己参与的实例:派给过自己(或自己所在的组)的待办,或者自己回复过的待办所在的运行。派给「任何人」的不算:那是没有
应用绑定的运行(画布、定时任务),user 角色不该因此读到;
- 自己的个人应用(docs/design/normal-user-apps.md N7)的刷新:只有拥有者,别人的个人应用即使有人自报同名也不算;
- 有授权(use / observe / manage,含 everyone 和自己所在的组)的应用里:
- 视图运行(来源是应用视图、按新口径补算);
- observe、manage 授权另能读全部实例;
- 应用发布版本里作为视图固定的工作流版本的正式运行(视图读的是定时任务那次运行,它不带应用 id)。
关登录:都能读。开登录后:
- 只给拥有者的运行(PRIVATE_ORIGINS,docs/design/normal-user-apps.md N7、§3.12「看别人的个人应用内容」一行全是 ✗):
个人应用的刷新、个人蓝图的试运行只有拥有者能读——管理员、搭建者也不例外,运行、证据、工件、文件下载、运行列表、
事件和实时流一个口径(private_owner / hidden_runs);
- builder 及以上:别的运行都能读;
- user 角色只能读:
- 自己发起的运行;
- 自己参与的实例:派给过自己(或自己所在的组)的待办,或者自己回复过的待办所在的运行。派给「任何人」的不算:那是
没有应用绑定的运行(画布、定时任务),user 角色不该因此读到;
- 有授权(use / observe / manage,含 everyone 和自己所在的组)的应用里:视图运行(来源是应用视图、按新口径补算);
observe、manage 授权另能读全部实例;应用发布版本里作为视图固定的工作流版本的正式运行(视图读的是定时任务那次
运行,它不带应用 id)。
user 角色读到的查询快照一律按派生遮罩处理、tool_snapshot 的原文不给(api/artifacts.py),这一层只回答「能不能读」。
"""
@@ -21,10 +24,56 @@ from sqlalchemy.ext.asyncio import AsyncSession
#: 视图运行的来源(engine/origin.py)
_VIEW_ORIGINS = ("app_view", "app_backfill")
#: 只给拥有者读的运行来源:个人应用的刷新(personal_app);试运行(trial)里出自个人蓝图的那些
PRIVATE_ORIGINS = ("personal_app", "trial")
def _unrestricted(principal: Any) -> bool:
return principal.mode == "off" or principal.at_least("builder")
async def private_owner(session: AsyncSession, run: Any) -> tuple[bool, str | None]:
"""这次运行是不是只给拥有者读的,是的话交回拥有者:(是不是, 拥有者)。
- 个人应用的刷新:拥有者按应用上记的认(刷新只能由拥有者发起,这里不靠发起人)。应用不在了、不是个人应用的,拥有者
为 None——谁都读不了,不往宽处猜;
- 试运行:出自个人蓝图的(run.origin 里记的蓝图 id,发起时由服务端写)只给蓝图的拥有者;共享蓝图的试运行照旧按
搭建者读。"""
from app.db.models_apps import App
from app.db.models_blueprints import Blueprint
kind = getattr(run, "origin_kind", None)
if kind == "personal_app":
owner = await session.scalar(select(App.owner).where(App.id == run.app_id, App.scope == "personal")) \
if run.app_id else None
return True, owner
if kind == "trial":
blueprint_id = (run.origin or {}).get("blueprint_id") if isinstance(run.origin, dict) else None
row = (await session.execute(select(Blueprint.scope, Blueprint.owner).where(
Blueprint.id == str(blueprint_id or "")))).first()
if row is not None and row.scope == "personal":
return True, row.owner
return False, None
def hidden_runs(principal: Any) -> Any:
"""SQL 条件:这个人读不了的只给拥有者的运行(别人的个人应用刷新、别人的个人蓝图试运行)。运行列表、审批列表、
探索问题聚类按 not_(hidden_runs(...)) 筛,和 run_readable 同一个口径。关登录时什么都不藏(没有可信的身份)。
各项都按非空比较(coalesce):来源、应用、蓝图 id 为空的老运行不能因为 NULL 被一起筛掉。"""
from sqlalchemy import false, func
from app.db.models import Run
from app.db.models_apps import App
from app.db.models_blueprints import Blueprint
if principal.mode == "off":
return false()
me = principal.username or ""
mine = select(App.id).where(App.scope == "personal", App.owner == me)
others = select(Blueprint.id).where(Blueprint.scope == "personal",
func.coalesce(Blueprint.owner, "") != me)
kind = func.coalesce(Run.origin_kind, "")
return or_(
and_(kind == "personal_app", or_(Run.app_id.is_(None), Run.app_id.not_in(mine))),
and_(kind == "trial", func.coalesce(func.json_extract(Run.origin, "$.blueprint_id"), "").in_(others)),
)
async def app_roles(session: AsyncSession, principal: Any) -> dict[str, set[str]]:
@@ -73,10 +122,17 @@ async def _viewed_by_granted_app(session: AsyncSession, run: Any, apps: set[str]
async def run_readable(principal: Any, run: Any) -> bool:
"""能不能读这次运行(证据、事件、工件)。run 可以是 Run,也可以是运行 id;不存在的运行读不了。"""
if _unrestricted(principal):
"""能不能读这次运行(证据、事件、工件)。run 可以是 Run,也可以是运行 id。
关登录:能。只给拥有者的运行(private_owner):只有拥有者能读,管理员、搭建者也不例外。builder 及以上:别的都能读,
不存在的运行也答「能」(接口照旧回 404,不改成 403)。user:见模块开头。"""
if principal.mode == "off":
return True
if not principal.username:
builder = principal.at_least("builder")
if builder and run is not None and not isinstance(run, str) \
and (getattr(run, "origin_kind", None) or "") not in PRIVATE_ORIGINS:
return True
if not builder and not principal.username:
return False
from app.db.base import SessionLocal
from app.db.models import Run
@@ -85,11 +141,14 @@ async def run_readable(principal: Any, run: Any) -> bool:
if isinstance(run, str):
run = await session.get(Run, run)
if run is None:
return False
return builder
private, owner = await private_owner(session, run)
if private:
return bool(owner) and bool(principal.username) and owner == principal.username
if builder:
return True
if run.started_by == principal.username:
return True
if run.origin_kind == "personal_app" and run.app_id:
return await _owns_personal_app(session, principal, run.app_id)
if await _participated(session, principal, run.id):
return True
apps = await app_roles(session, principal)
@@ -99,18 +158,10 @@ async def run_readable(principal: Any, run: Any) -> bool:
return await _viewed_by_granted_app(session, run, set(apps))
async def _owns_personal_app(session: AsyncSession, principal: Any, app_id: str) -> bool:
"""个人应用的刷新只有这个应用的拥有者能读:别人参与不了(不派待办)、也没有授权可言(个人应用不能分享)。按应用上记的
拥有者认,不按发起人——刷新只能由拥有者发起,这里不靠它。"""
from app.db.models import App
owner = await session.scalar(select(App.owner).where(App.id == app_id, App.scope == "personal"))
return bool(owner) and owner == principal.username
async def artifact_readable(principal: Any, artifact_id: str) -> bool:
"""能不能读这件工件:引用它的某次运行能读就能读。工件按内容寻址,同一件可能被好几次运行引用。"""
if _unrestricted(principal):
"""能不能读这件工件:引用它的某次运行能读就能读。工件按内容寻址,同一件可能被好几次运行引用。不挂在任何运行上的
工件:builder 及以上照旧能读,user 读不了。只被别人的个人应用刷新、个人蓝图试运行引用的,builder 及以上也读不了。"""
if principal.mode == "off":
return True
from app.db.base import SessionLocal
from app.db.models import Artifact
@@ -118,6 +169,8 @@ async def artifact_readable(principal: Any, artifact_id: str) -> bool:
async with SessionLocal() as session:
run_ids = [r for r in (await session.execute(
select(Artifact.run_id).where(Artifact.id == artifact_id))).scalars() if r]
if not run_ids:
return principal.at_least("builder")
for run_id in run_ids:
if await run_readable(principal, run_id):
return True
+23 -10
View File
@@ -4,16 +4,23 @@
start_run 先查来源和运行类别)。图本身再按这里收窄,理由是同一条:对 user 角色来说遮罩就是边界(A7 第 6 条),它能
碰的只有「读数据、整理、撰写」这一类,不能执行代码、不能调外部系统、不能写任何东西:
- 节点只许:输入、模型、Agent、报告、输出、知识库检索、数据整形、条件分支、口径卡、合并查询。调用工具(会写外部系统)、
代码、协作、子工作流、循环、人工审批、记忆写入、校验、双盲复算、反方审稿一律不许——后几种不是危险,而是问数据用不到,
多放一种就多一处要单独核对遮罩的地方;
- Agent 的工具只许数据源工具(db_query__ / db_schema__,查询结果按 MASK_RESULTS_OPTION 在工具边界遮)和只读的内置工具
(计算、当前时间、JSON 取值、知识库检索、记忆读取)。自定义工具、MCP、文件、网页、HTTP、代码执行一律不许;模板拼出来的
工具名({{ vars.tool }})发起时不知道是谁,也不许。数据源查询一律只读:可写源上的写语句由守卫拒掉,不交给审批(api/runs.py
给运行打上 READ_ONLY_OPTION)——Agent 的 approval 是发起人自己写在图里的,写成 never 就不问人了;
- 节点只许:输入、模型、Agent、报告、输出、知识库检索、数据整形、条件分支、口径卡、合并查询、调用工具。代码、协作、
子工作流、循环、人工审批、记忆写入、校验、双盲复算、反方审稿一律不许——后几种不是危险,而是问数据用不到,多放一种
就多一处要单独核对遮罩的地方;
- 工具(Agent 绑的、调用工具节点调的同一份白名单,engine/launch.tool_bindings 一并列出)只许数据源工具(db_query__ /
db_schema__,查询结果按 MASK_RESULTS_OPTION 在工具边界遮)和只读的内置工具(计算、当前时间、JSON 取值、知识库检索、
记忆读取)。自定义工具、MCP、文件、网页、HTTP、代码执行、平台的写入能力(records_append 这类)一律不许;模板拼出来的
工具名({{ vars.tool }})发起时不知道是谁,也不许。调用工具节点放行的理由:它和 Agent 调同一个查询工具等价——同一个
ToolContext 带着遮罩、只读两个开关(engine/nodes/tools.py 的 _tool_ctx),参数由人写死反而比模型挑的更好核对;
周期看板、异常处置这些模板的取数节点就是它(个人应用和问数据同一份白名单,blueprints/personal.policy_problem);
- 数据源查询一律只读:可写源上的写语句由守卫拒掉,不交给审批(api/runs.py、个人应用刷新、个人蓝图试运行给运行打上
READ_ONLY_OPTION)——Agent、调用工具节点的 approval 是发起人自己写在图里的,写成 never 就不问人了。能力治理里改成
「写入」的工具在只读运行里同样不执行(调用工具节点在走动作流程之前拒,Agent 建工具时拒);
- 记忆范围、知识库不由发起人挑:记忆和知识库里的内容不经遮罩,搭建者的 Agent 存进去的查询原文,换个范围就读到了。
记忆固定为这位使用者自己的范围(chat_memory_scope;问数据里写不了记忆,它读到的只会是空的),知识库固定为设置里的
运行默认值。节点上、图级 defaults 里写了 memory_scope / collection 的图不许发起,请求体里带的同样拒(api/runs.py)。
运行默认值。节点上、图级 defaults 里、调用工具节点的参数里写了 memory_scope / collection 的图不许发起,请求体里带的
同样拒(api/runs.py)。这只是发起前的一道:根子在运行时——遮罩、只读的运行里范围由运行定,节点配置改不了,kb_search
的 collection 参数(参数名纠错改出来的、模型挑的)一律拒(tools/registry.ToolContext.fixed_scope)。
没在这里列出的节点类型、工具,以后加了也默认不许(白名单),要放开得先想清楚遮罩怎么守。
"""
@@ -23,7 +30,7 @@ from typing import Any
#: 问数据运行里 user 角色能用的节点类型(engine/schema.py 的 NodeType 取值)
CHAT_NODE_TYPES = frozenset({"input", "llm", "agent", "report", "output", "retrieve", "transform", "branch",
"metrics", "merge"})
"metrics", "merge", "tool"})
#: Agent 能绑的只读内置工具(tools/builtin/)
CHAT_BUILTIN_TOOLS = frozenset({"calculator", "current_time", "json_query", "kb_search", "memory_recall"})
#: 运行时改记忆范围、知识库的配置键(engine/nodes 的 ctx.cfg("memory_scope") / ctx.cfg("collection"),图级 defaults 同样生效)
@@ -54,7 +61,7 @@ def chat_graph_problem(graph: Any, *, where: str = "问数据") -> str | None:
校验的图本来就发起不了。
where 是说法里的「在哪」:个人应用(blueprints/personal.py)用的是同一份白名单,只把「问数据」换成「个人应用」。"""
from app.engine.launch import tool_bindings
from app.engine.launch import node_setting, tool_bindings
from app.engine.schema import GraphSpec, type_label
if not isinstance(graph, dict):
@@ -72,6 +79,12 @@ def chat_graph_problem(graph: Any, *, where: str = "问数据") -> str | None:
"如需这类处理,请联系搭建者做成应用")
if any(_set(node.config.get(key)) for key in SCOPE_KEYS):
return f"{scope_refused}(「{node.title}」)"
# 调用工具节点的参数直接交给工具:kb_search 的 collection 这类参数写了,同样是自己挑了范围。取值和运行时同一个规则
# (node_setting:节点上没写就取图级 defaults 里的 args)。参数名写错、运行时被纠成 collection 的,这里认不出——
# 运行时那一道由工具自己拒(tools/registry.ToolContext.fixed_scope,kb_search 只能检索运行定下的知识库)
args = node_setting(spec, node, "args") if str(node.type) == "tool" else None
if isinstance(args, dict) and any(_set(args.get(key)) for key in SCOPE_KEYS):
return f"{scope_refused}(「{node.title}」)"
for who, name in tool_bindings(spec):
if "{{" in name:
return f"没有权限:{who}的工具名由模板拼成,发起前无法确定是哪个工具,使用者在{where}中不能这样绑定工具"