集成收尾:前端约定写入后端实际返回的形状(应用详情只对 manage 给草稿、补算计划与结果、按名字的钩子行、能力字段审阅),去掉多种字段名都认的兼容解析;钩子按(应用, 名字)、应用助手对话按(应用, 建立人)加唯一索引,迁移前去重,插入改在保存点里、撞上就改用已有的那一行;口径卡固定的版本也须已发布且未退役,记级别之前有正式运行凭据的老版本启动时补记为已发布;证据面板和应用视图按列名遮端点快照(同一个函数),不再整张表遮掉;待办计数带上最新一条的时间,新待办通知不再漏;非管理员的搭建者不出模型接入的改动按钮

This commit is contained in:
ThearchyHelios
2026-10-14 22:08:33 +08:00
parent 1dd4f93579
commit bff3062a97
32 changed files with 419 additions and 246 deletions
+13 -9
View File
@@ -302,18 +302,22 @@ async def assistant_conversation(app_id: str, session: AsyncSession = Depends(ge
lookup = (select(Conversation).where(Conversation.kind == "app", Conversation.app_id == app.id,
Conversation.archived.is_(False), mine)
.order_by(Conversation.last_active_at.desc()).limit(1))
# 取或建在进程里按 (应用, 人) 串行:编辑器开发模式下 effect 执行两次、同一个人开两个标签页,都会同时来取。库里没有
# 唯一约束,建完再查一遍:别处(另一个进程)抢先建了的,删掉刚建的这段(还没有轮次),用那一段——历史才不会被拆开
# 取或建在进程里按 (应用, 人) 串行:编辑器开发模式下 effect 执行两次、同一个人开两个标签页,都会同时来取。库里有
# (app_id, created_by) 的部分唯一索引(ux_conversations_app_owner):别处(另一个进程)抢先建了的,这次插入在保存点里
# 被拒,改用那一段——历史才不会被拆开。没署名(关登录又没填)的不受索引约束,靠这把进程内的锁
async with _assistant_lock(app.id, who):
row = await session.scalar(lookup)
if row is None:
row = Conversation(kind="app", app_id=app.id, created_by=who, title=f"「{app.name}」的助手")
session.add(row)
await session.flush()
other = await session.scalar(lookup.where(Conversation.id != row.id))
if other is not None:
await session.delete(row)
row = other
fresh = Conversation(kind="app", app_id=app.id, created_by=who, title=f"「{app.name}」的助手")
try:
async with session.begin_nested():
session.add(fresh)
await session.flush()
row = fresh
except IntegrityError:
row = await session.scalar(lookup)
if row is None:
raise
await session.commit()
await session.refresh(row)
turns = list((await session.execute(select(ConversationTurn).where(
+7
View File
@@ -2139,6 +2139,13 @@ async def _mask_sets(snap: dict[str, Any] | None, source: Any, masks: _Masks, *,
artifact 是 snap 的工件 id:给了的,分析结果按快照记在进程里(_MEMO,键见 _Masks.memo_key),之后的请求不再分析。"""
if snap is not None and snap.get("source") == MERGE_SOURCE:
return await _merge_mask_sets(snap, masks, depth=depth)
if snap is not None and snap.get("capability"):
# 端点快照:列是能力登记的表格映射排出来的,没有 SQL 可认、也不该按数据源认(端点不是数据源,按数据源找不到会把
# 整张表都当成「数据源不在了」遮掉)。按列名遮:落快照时记下的加能力现在的遮罩字段,确定就是这几列
from app.capabilities.registry import snapshot_masked_columns
base = await snapshot_masked_columns(snap)
return _MaskSets(set(base), set(base), base)
name = source or (snap or {}).get("source")
current, found = await masks.of(name)
base = set(current) | (_recorded_masks(snap) if snap is not None else set())
+2 -2
View File
@@ -61,8 +61,8 @@ async def list_tasks(
@router.get("/count")
async def count_tasks(mine: int = 1, x_actor: str | None = Header(default=None)) -> dict[str, int]:
"""导航徽标:派给我的、待处理的待办数,按来源分开(apps:应用里的;other:画布、定时任务的审批)。"""
async def count_tasks(mine: int = 1, x_actor: str | None = Header(default=None)) -> dict[str, Any]:
"""导航徽标:派给我的、待处理的待办数,按来源分开(apps:应用里的;other:画布、定时任务的审批);latest 是最新一条的开出时间。"""
return await task_service.count(principal_of(x_actor))
+1 -1
View File
@@ -164,7 +164,7 @@ async def _lint_subgraph_pins(session: AsyncSession, spec: dict[str, Any], pins:
except ValueError:
continue
for problem in await subgraph_pin_problems(session, graph):
out.error(problem.code, f"「{state.label}」里的子工作流「{problem.title}」固定的{problem.reason}:这一版的正式运行"
out.error(problem.code, f"「{state.label}」里的{problem.who}固定的{problem.reason}:这一版的正式运行"
"发起时会被拒绝。请在那个工作流里改好并发布新版本,再把这里的引用换成新版本", f"pins[{i}]",
node_id=problem.node_id)
+5 -21
View File
@@ -869,36 +869,20 @@ def _query_artifact(source: Source, node_id: str, exec_no: Any = None) -> str |
async def _snapshot(source: Source, artifact: str) -> tuple[dict[str, Any] | None, set[str]]:
"""查询快照和要遮的列(小写):查询当时记下的加数据源现在设的,按 SQL 认出改了名字的也遮(同证据面板)。端点快照
按列名遮(见下)。"""
from app.api.evidence import _fetch, _recorded_masks
from app.api.evidence import _fetch
snap, ok = _fetch(artifact)
if ok is not True or not isinstance(snap, dict) or not isinstance(snap.get("rows"), list):
return None, set()
if snap.get("capability"):
# 端点快照:列是能力登记的表格映射排出来的,没有 SQL 改名这回事,按列名遮就是准的——落快照时记下的,加上能力
# 现在的遮罩字段。查询快照那一套(按 SQL 认改了名字的遮罩列)在这里认不出出处,会把整张表都遮掉
return snap, _recorded_masks(snap) | await _capability_masks(str(snap["capability"]))
# 端点快照:按列名遮(落快照时记下的,加上能力现在的遮罩字段),证据面板用的是同一个函数
from app.capabilities.registry import snapshot_masked_columns
return snap, await snapshot_masked_columns(snap)
sets = await source.masks.sets(artifact, snap.get("source"), snap)
return snap, set(sets.hidden)
async def _capability_masks(name: str) -> set[str]:
"""端点快照(capabilities/endpoints.py 落的 endpoint_snapshot)按能力现在的遮罩字段要遮的列(小写)。
快照里的 mask_columns 是落快照那一刻的遮罩(调用方另外并进来);管理员事后给能力加了遮罩字段,来源运行没换,
视图也得跟着遮(P4,同数据源事后加遮罩列)。遮罩字段是 JSON 指针,按能力登记的表格映射认它落在哪几列
(masked_columns_of,落快照时记 mask_columns 用的同一个函数)。能力删掉了的只能按快照记下的遮。"""
from app.capabilities.endpoints import masked_columns_of
from app.capabilities.registry import get_capability, masked_paths
from app.db.base import SessionLocal
async with SessionLocal() as session:
cap = await get_capability(session, name)
if cap is None:
return set()
return {c.lower() for c in masked_columns_of(cap.tabular or {}, masked_paths(cap))}
async def _cell(ctx: _Page, source: Source, cid: str, ref: Any, conv: Any) -> dict[str, Any]:
from app.api.evidence import MASKED
from app.engine.evidence import MISSING, RenderError, cell_eid, render_cell
+69 -22
View File
@@ -80,10 +80,13 @@ async def version_row(session: AsyncSession, workflow_id: str, version: int) ->
# 子工作流固定的版本(D-q)
# --------------------------------------------------------------------------
#: 子工作流节点固定的版本不能用的机器码。发起正式运行(engine/launch.py)、发布工作流与发布前检查(engine/lint_process.py
#: 的 lint_subgraph_pins)、应用检查(apps/lint.py)都按 subgraph_pin_problems 判,机器码相同
#: 固定的上游版本不能用的机器码:子工作流节点(workflow_version)、口径卡转抄别处的口径(caliber_from)。发起正式运行
#: (engine/launch.py)、发布工作流与发布前检查(engine/lint_process.py 的 lint_subgraph_pins)、应用检查(apps/lint.py)
#: 都按 subgraph_pin_problems 判,机器码相同
SUBGRAPH_UNPUBLISHED = "subgraph_version_unpublished"
SUBGRAPH_RETIRED = "subgraph_version_retired"
CALIBER_UNPUBLISHED = "caliber_version_unpublished"
CALIBER_RETIRED = "caliber_version_retired"
@dataclass(frozen=True)
@@ -99,40 +102,84 @@ class SubgraphPinProblem:
reason: str
#: 怎么改
advice: str
#: 句子的主语:「子工作流「…」」或「口径卡「…」」
who: str = ""
#: 节点上哪个配置项固定了版本(问题定位到它)
field: str = "workflow_version"
def pinned_upstream(node: Any) -> tuple[str, str, int] | None:
"""节点固定的上游版本:(种类 subgraph / caliber, 工作流 id, 版本);没固定就是 None。子工作流写在 workflow_id /
workflow_version,口径卡转抄别处的口径写在 caliber_from 里。"""
from app.engine.schema import NodeType
cfg = node.config
if node.type == NodeType.SUBGRAPH:
wid, pinned = cfg.get("workflow_id"), cfg.get("workflow_version")
kind = "subgraph"
elif node.type == NodeType.METRICS and isinstance(cfg.get("caliber_from"), dict):
wid, pinned = cfg["caliber_from"].get("workflow_id"), cfg["caliber_from"].get("workflow_version")
kind = "caliber"
else:
return None
if not wid or not str(pinned or "").isdigit():
return None
return kind, str(wid), int(pinned)
async def subgraph_pin_problems(session: AsyncSession, spec: Any) -> list[SubgraphPinProblem]:
"""图里每个固定了版本的子工作流节点,固定的那一版必须「已发布且未退役」(is_published);不是的逐个交回。
子工作流固定版本就是方法卡的机制:外层发布时认可的是「用它的 vN」。vN 只是一张保存出来的草稿快照、或者已经退役,
正式运行照样执行它,就等于绕过了发布门禁、绕过了退役。发起时拦(launch.py),发布时也按同一条拦——不然钉在草稿上的
工作流照样发得出去,之后每一次正式运行(定时任务、应用入口、按需视图、补算)都在发起时才被拒。
没固定版本的子工作流跟随最新,那是受管级门禁管的事(governed.subgraph_unpinned),这里不管。"""
from app.engine.schema import NodeType
"""图里每个固定了上游版本的节点(子工作流、转抄别处口径的口径卡),固定的那一版必须「已发布且未退役」
(is_published);不是的逐个交回。
固定版本就是方法卡的机制:外层发布时认可的是「用它的 vN」。vN 只是一张保存出来的草稿快照、或者已经退役,正式运行
照样执行它、照样按它的口径算,就等于绕过了发布门禁、绕过了退役。发起时拦(launch.py),发布时也按同一条拦——不然钉在
草稿上的工作流照样发得出去,之后每一次正式运行(定时任务、应用入口、按需视图、补算)都在发起时才被拒。
没固定版本的跟随最新,那是受管级门禁管的事(governed.subgraph_unpinned),这里不管。"""
out: list[SubgraphPinProblem] = []
for node in spec.nodes:
if node.type != NodeType.SUBGRAPH:
pin = pinned_upstream(node)
if pin is None:
continue
wid, pinned = node.config.get("workflow_id"), node.config.get("workflow_version")
if not wid or not str(pinned or "").isdigit():
continue
version = int(pinned)
workflow = await session.get(Workflow, str(wid))
row = await version_row(session, str(wid), version) if workflow is not None else None
kind, wid, version = pin
caliber = kind == "caliber"
who = f"口径卡「{node.title}」" if caliber else f"子工作流「{node.title}」"
field = "caliber_from" if caliber else "workflow_version"
unpublished, retired = (CALIBER_UNPUBLISHED, CALIBER_RETIRED) if caliber else (SUBGRAPH_UNPUBLISHED, SUBGRAPH_RETIRED)
workflow = await session.get(Workflow, wid)
row = await version_row(session, wid, version) if workflow is not None else None
if workflow is None or row is None:
out.append(SubgraphPinProblem(node.id, node.title, SUBGRAPH_UNPUBLISHED, str(wid), version,
f"工作流版本 v{version} 不存在", "请在节点中重新选择版本"))
out.append(SubgraphPinProblem(node.id, node.title, unpublished, wid, version,
f"工作流版本 v{version} 不存在", "请在节点中重新选择版本", who, field))
elif row.retired_at is not None:
out.append(SubgraphPinProblem(node.id, node.title, SUBGRAPH_RETIRED, str(wid), version,
f"「{workflow.name}」v{version} 已退役", "请把节点改为固定一个在用的发布版本"))
out.append(SubgraphPinProblem(node.id, node.title, retired, wid, version,
f"「{workflow.name}」v{version} 已退役", "请把节点改为固定一个在用的发布版本",
who, field))
elif not is_published(row, current=workflow.published_version):
out.append(SubgraphPinProblem(node.id, node.title, SUBGRAPH_UNPUBLISHED, str(wid), version,
out.append(SubgraphPinProblem(node.id, node.title, unpublished, wid, version,
f"「{workflow.name}」v{version} 没有发布过",
"请先发布那个工作流,或把节点改为固定一个已发布的版本"))
"请先发布那个工作流,或把节点改为固定一个已发布的版本", who, field))
return out
async def backfill_legacy_levels(session: AsyncSession) -> int:
"""给记级别之前发布过的老版本补上 level(启动时跑一次,幂等)。
level 是后来才加的列:那之前发布过的版本都是空的,按 is_published 只认当前发布版——而子工作流、口径卡钉住的往往是
更早的某一版,新规则会把它们误判成「没发布过」。补的依据只有一条,有据可查:这一版发起过正式运行。老代码里正式运行
只能从当时的发布版发起,所以它一定发布过。级别记 published:当时是不是受管级查不出来,往低处记(应用的级别取最低,
只会少给信任,不会多给)。没有正式运行凭据的照旧为空,不猜。"""
from sqlalchemy import exists, update
from app.db.models import Run
evidence = exists().where(Run.workflow_id == WorkflowVersion.workflow_id, Run.version == WorkflowVersion.version,
Run.run_class == "formal")
result = await session.execute(update(WorkflowVersion)
.where(WorkflowVersion.level.is_(None), WorkflowVersion.retired_at.is_(None), evidence)
.values(level="published").execution_options(synchronize_session=False))
return result.rowcount or 0
# --------------------------------------------------------------------------
# 账本(L4):一个工作流版本被谁固定、生效了几次、正式运行了几次。退役确认、版本详情、删除约束、账本接口共用
# --------------------------------------------------------------------------
+18
View File
@@ -305,6 +305,24 @@ async def endpoint_request(session: AsyncSession, capability: Any, args: dict[st
run_id=run_id, node_id=node_id)
async def snapshot_masked_columns(snap: dict[str, Any]) -> set[str]:
"""一份端点快照要遮的列(小写):落快照时记下的(mask_columns),加上能力现在的遮罩字段按表格映射落到的列。
端点快照的列是能力登记的表格映射排出来的,没有 SQL 改名这回事,按列名遮就是准的。管理员事后给能力加了遮罩字段,
来源运行没换,证据面板和视图也得跟着遮(P4,同数据源事后加遮罩列)。能力删掉了的只能按快照记下的遮。
证据面板(api/evidence._mask_sets)和应用视图(apps/resolve._snapshot)共用这一份。"""
from app.capabilities.endpoints import masked_columns_of
from app.db.base import SessionLocal
recorded = snap.get("mask_columns")
out = {c.lower() for c in recorded if isinstance(c, str)} if isinstance(recorded, list) else set()
async with SessionLocal() as session:
cap = await get_capability(session, str(snap.get("capability") or ""))
if cap is not None:
out |= {c.lower() for c in masked_columns_of(cap.tabular or {}, masked_paths(cap))}
return out
def masked_paths(capability: Any) -> list[str]:
"""能力的遮罩字段(JSON 指针)。"""
return list(getattr(capability, "masked", None) or [])
+23
View File
@@ -252,6 +252,21 @@ _INDEX_MIGRATIONS: list[tuple[str, str, str | tuple[str, ...]]] = [
]
#: 迁移补建的唯一索引:(索引名, 表, 列, 部分索引的条件, 建之前的去重语句)。只在索引还不存在时先去重再建——存量库里可能
#: 已经有并发留下的重复行,直接建会失败。去重的规矩写在各自的语句里:多出的钩子行删掉(只留最早的那一行,它的密钥照旧
#: 能用),多出的助手对话归档(不删,历史还在)
_UNIQUE_INDEX_MIGRATIONS: list[tuple[str, str, tuple[str, ...], str | None, str]] = [
("ux_app_hooks_app_name", "app_hooks", ("app_id", "name"), None,
"DELETE FROM app_hooks WHERE id NOT IN (SELECT id FROM (SELECT id, ROW_NUMBER() OVER "
"(PARTITION BY app_id, name ORDER BY created_at, id) AS rn FROM app_hooks) WHERE rn = 1)"),
("ux_conversations_app_owner", "conversations", ("app_id", "created_by"), "kind = 'app' AND archived = 0",
"UPDATE conversations SET archived = 1 WHERE kind = 'app' AND archived = 0 AND created_by IS NOT NULL "
"AND id NOT IN (SELECT id FROM (SELECT id, ROW_NUMBER() OVER (PARTITION BY app_id, created_by "
"ORDER BY last_active_at DESC, id) AS rn FROM conversations WHERE kind = 'app' AND archived = 0 "
"AND created_by IS NOT NULL) WHERE rn = 1)"),
]
def _migrate(conn) -> None: # pragma: no cover - 同步回调
from sqlalchemy import text
@@ -264,6 +279,14 @@ def _migrate(conn) -> None: # pragma: no cover - 同步回调
cols = {row[1] for row in conn.execute(text(f"PRAGMA table_info({table})"))}
if all(c in cols for c in wanted):
conn.execute(text(f"CREATE INDEX IF NOT EXISTS {name} ON {table} ({', '.join(wanted)})"))
for name, table, columns, where, dedupe in _UNIQUE_INDEX_MIGRATIONS:
cols = {row[1] for row in conn.execute(text(f"PRAGMA table_info({table})"))}
exists = conn.execute(text("SELECT 1 FROM sqlite_master WHERE type = 'index' AND name = :n"), {"n": name}).first()
if exists or not all(c in cols for c in columns):
continue
conn.execute(text(dedupe))
conn.execute(text(f"CREATE UNIQUE INDEX {name} ON {table} ({', '.join(columns)})"
+ (f" WHERE {where}" if where else "")))
async def init_db() -> None:
+4
View File
@@ -803,6 +803,10 @@ class Conversation(Base, TimestampMixin):
"""
__tablename__ = "conversations"
# 一个人在一个应用里只有一段(未归档的)助手对话(D-e)。存量库由 db/base.py 的 _UNIQUE_INDEX_MIGRATIONS 补建。
# created_by 为空(关登录又没填署名)的不受约束:SQLite 的唯一索引里 NULL 互不相等,那种情况靠接口里的进程内锁
__table_args__ = (Index("ux_conversations_app_owner", "app_id", "created_by", unique=True,
sqlite_where=text("kind = 'app' AND archived = 0")),)
id: Mapped[str] = mapped_column(String(32), primary_key=True, default=new_id)
title: Mapped[str] = mapped_column(String(200), default="")
+3 -1
View File
@@ -9,7 +9,7 @@ from __future__ import annotations
from datetime import datetime
from typing import Any
from sqlalchemy import ForeignKey, Integer, String, Text, UniqueConstraint
from sqlalchemy import ForeignKey, Index, Integer, String, Text, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column
from app.db.base import Base, TimestampMixin, UTCDateTime, new_id, utcnow
@@ -86,6 +86,8 @@ class AppHook(Base, TimestampMixin):
"""入站钩子。start:按入口发起实例;signal:按 correlation 解决一条「等外部系统」的待办。"""
__tablename__ = "app_hooks"
# 一个名字一行(D-g):并发「生成密钥」不会多出第二把能用的密钥。存量库由 db/base.py 的 _UNIQUE_INDEX_MIGRATIONS 补建
__table_args__ = (Index("ux_app_hooks_app_name", "app_id", "name", unique=True),)
id: Mapped[str] = mapped_column(String(32), primary_key=True, default=new_id)
app_id: Mapped[str] = mapped_column(ForeignKey("apps.id", ondelete="CASCADE"), index=True)
+1 -1
View File
@@ -421,7 +421,7 @@ async def _refuse_unpublished_subgraphs(session: AsyncSession, spec: GraphSpec)
"""正式运行里,每个固定了版本的子工作流节点,固定的那一版必须「已发布且未退役」(D-q)。规则在
apps/versions.subgraph_pin_problems,发布门禁按同一条拦;这里是发起这一刻的那一道(发布之后那一版才退役的)。"""
for problem in await subgraph_pin_problems(session, spec):
raise LaunchRefused(409, f"子工作流「{problem.title}」固定的{problem.reason},不能发起正式运行。"
raise LaunchRefused(409, f"{problem.who}固定的{problem.reason},不能发起正式运行。"
f"{problem.advice},再发布这个工作流", problem.code)
+6 -4
View File
@@ -264,16 +264,18 @@ def lint_process(spec: GraphSpec, flag: Callable[..., None], *, level: str, stri
def lint_subgraph_pins(spec: GraphSpec, flag: Callable[..., None], *, level: str, strict: bool) -> None:
"""子工作流固定的版本不是「已发布且未退役」的(D-q):各级都报错(always),机器码和发起时拒绝的一样。
load_subgraphs 没读好、没放进来时不查(门禁本身不查库)。"""
from app.apps.versions import pinned_upstream
graphs = _SUBGRAPHS.get()
nodes = spec.node_map()
for problem in getattr(graphs, "pin_problems", None) or []:
node = nodes.get(problem.node_id)
# 读好的结果对的是读的那张图;拿去查改过的图(自动修复的预览)时,节点改了固定的版本就不再算它
if node is None or str(node.config.get("workflow_id") or "") != problem.workflow_id \
or str(node.config.get("workflow_version") or "") != str(problem.version):
if node is None or pinned_upstream(node) is None \
or pinned_upstream(node)[1:] != (problem.workflow_id, problem.version):
continue
flag(f"子工作流「{problem.title}」固定的{problem.reason}:正式运行发起时会被拒绝。{problem.advice}",
code=problem.code, node_id=problem.node_id, field="workflow_version", always=True)
flag(f"{problem.who}固定的{problem.reason}:正式运行发起时会被拒绝。{problem.advice}",
code=problem.code, node_id=problem.node_id, field=problem.field, always=True)
def _register() -> None:
+9
View File
@@ -42,6 +42,15 @@ async def lifespan(_: FastAPI) -> AsyncIterator[None]:
settings.ensure_dirs()
await init_db()
await seed_defaults()
# 记级别之前发布过的老版本:有正式运行凭据的补记为已发布,免得子工作流、口径卡钉住它们的正式运行被误判成「没发布过」
from app.apps.versions import backfill_legacy_levels
from app.db.base import SessionLocal as _Sessions
async with _Sessions() as _s:
if levelled := await backfill_legacy_levels(_s):
logger.info("给 %d 个老的发布版本补记了级别(有正式运行凭据)", levelled)
await _s.commit()
await _platform_startup()
# 把存的 embedding 配置读进来。不读的话重启后会悄悄退回本地哈希向量,
+12 -9
View File
@@ -340,8 +340,8 @@ async def rotate_hook(session: AsyncSession, app_id: str, name: str, *, kind: st
"""生成或轮换这个名字的密钥,返回 (行, 密钥明文, 是不是新生成的)。密钥只在这一次交回,库里存密文。
kind / entry 只是照声明记在行上给人看;收请求时以当前发布版的声明为准(published_declaration)。调用方提交。
新插入一行之后再查一遍:别人抢先插了(并发生成,hook_lock 管不到的另一个进程),删掉自己这行、改去轮换最早那一行,
交回的仍是这一次的密钥——和两个人先后点「轮换」一样,后点的那把生效。"""
(app_id, name) 有唯一索引(ux_app_hooks_app_name):别人抢先插了这个名字(并发生成,hook_lock 管不到的另一个进程),
这次插入在保存点里被库拒掉,改去轮换已有的那一行,交回的仍是这一次的密钥——和两个人先后点「轮换」一样,后点的那把生效。"""
from app.core.crypto import encrypt
if kind not in KINDS:
@@ -350,13 +350,16 @@ async def rotate_hook(session: AsyncSession, app_id: str, name: str, *, kind: st
row = await hook_row(session, app_id, name)
created = row is None
if row is None:
row = AppHook(app_id=app_id, name=name[:100], kind=kind, enabled=True, secret="")
session.add(row)
await session.flush()
first = await hook_row(session, app_id, name)
if first is not None and first.id != row.id:
await session.delete(row)
row, created = first, False
fresh = AppHook(app_id=app_id, name=name[:100], kind=kind, enabled=True, secret="")
try:
async with session.begin_nested():
session.add(fresh)
await session.flush()
row = fresh
except IntegrityError:
row, created = await hook_row(session, app_id, name), False
if row is None:
raise
row.kind, row.entry_id = kind, (entry if kind == "start" else None)
row.secret = encrypt(secret) or ""
await session.flush()
+13 -6
View File
@@ -825,15 +825,22 @@ async def list_tasks(principal: Any, *, assigned: str = "me", statuses: list[str
return await _describe(session, tasks, principal)
async def count(principal: Any) -> dict[str, int]:
"""导航徽标:派给我的、待处理的待办数。apps 是应用里的,other 是不属于应用的(画布、定时任务的审批)。"""
async def count(principal: Any) -> dict[str, Any]:
"""导航徽标:派给我的、待处理的待办数。apps 是应用里的,other 是不属于应用的(画布、定时任务的审批)。
latest 是其中最新一条的开出时间:前端的新待办通知看它变没变,而不只看数——同一拍里一条处理掉、一条新来,数不变,
新的那条照样要通知。"""
async with SessionLocal() as session:
stmt = select(Task.app_id.is_not(None), func.count()).where(Task.status == "pending")
stmt = select(Task.app_id.is_not(None), func.count(), func.max(Task.created_at)).where(Task.status == "pending")
if (clause := _mine(principal)) is not None:
stmt = stmt.where(clause)
rows = dict((await session.execute(stmt.group_by(Task.app_id.is_not(None)))).all())
apps, other = int(rows.get(True, 0)), int(rows.get(False, 0))
return {"mine": apps + other, "apps": apps, "other": other}
rows = {has_app: (n, latest) for has_app, n, latest in
(await session.execute(stmt.group_by(Task.app_id.is_not(None)))).all()}
apps, other = int(rows.get(True, (0, None))[0]), int(rows.get(False, (0, None))[0])
stamps = [latest for _, latest in rows.values() if latest is not None]
latest = max(stamps) if stamps else None
return {"mine": apps + other, "apps": apps, "other": other,
"latest": latest.isoformat() if hasattr(latest, "isoformat") else latest}
async def detail(task_id: str, principal: Any) -> dict[str, Any] | None:
+49
View File
@@ -334,3 +334,52 @@ async def test_a_workflow_in_use_cannot_be_deleted(client):
assert res.status_code == 409 and "v1 有 1 条生效记录" in res.json()["detail"]
plain = await workflow({1: "published"}, current=1)
assert (await client.delete(f"/api/workflows/{plain}")).status_code == 204
async def test_caliber_pins_follow_the_same_rule_as_subgraphs():
"""D-q 延伸到口径卡:转抄别处口径的口径卡固定的那一版,同样必须已发布且未退役;机器码各用一套。"""
from types import SimpleNamespace
from app.apps.versions import CALIBER_UNPUBLISHED, subgraph_pin_problems
from app.db.base import SessionLocal
from app.db.models import Workflow, WorkflowVersion
from app.engine.schema import GraphSpec
async with SessionLocal() as session:
wf = Workflow(name="周销售口径", graph={}, published_version=2)
session.add(wf)
await session.flush()
session.add_all([WorkflowVersion(workflow_id=wf.id, version=1, graph={}),
WorkflowVersion(workflow_id=wf.id, version=2, graph={}, level="published")])
await session.commit()
graph = GraphSpec.model_validate({"nodes": [
{"id": "in", "type": "input", "data": {"label": "入口", "config": {}}},
{"id": "kpi", "type": "metrics", "data": {"label": "指标", "config": {
"caliber_from": {"workflow_id": wf.id, "workflow_version": 1, "node_id": "card"}}}},
], "edges": [{"source": "in", "target": "kpi"}]})
problems = await subgraph_pin_problems(session, graph)
assert [(p.code, p.field) for p in problems] == [(CALIBER_UNPUBLISHED, "caliber_from")]
assert problems[0].who == "口径卡「指标」"
graph.nodes[1].config["caliber_from"]["workflow_version"] = 2
assert await subgraph_pin_problems(session, graph) == []
async def test_legacy_versions_with_formal_runs_are_backfilled_as_published():
"""记级别之前发布过的老版本:有正式运行凭据的补记为 published(幂等),没有凭据的不猜。"""
from app.apps.versions import backfill_legacy_levels
from app.db.base import SessionLocal
from app.db.models import Run, Workflow, WorkflowVersion
async with SessionLocal() as session:
wf = Workflow(name="老方法卡", graph={}, published_version=3)
session.add(wf)
await session.flush()
session.add_all([WorkflowVersion(workflow_id=wf.id, version=v, graph={}) for v in (1, 2, 3)])
session.add(Run(workflow_id=wf.id, run_class="formal", version=1, graph={}, input={}))
await session.commit()
assert await backfill_legacy_levels(session) >= 1
await session.commit()
levels = {v.version: v.level for v in (await session.execute(
WorkflowVersion.__table__.select().where(WorkflowVersion.workflow_id == wf.id))).all()}
assert levels == {1: "published", 2: None, 3: None}
assert await backfill_legacy_levels(session) == 0
+3 -1
View File
@@ -67,7 +67,9 @@ async def publish(graph, *, name="口径库", versions=1, status="published", la
session.add(wf)
await session.flush()
for v in range(1, versions + 1):
session.add(WorkflowVersion(workflow_id=wf.id, version=v, graph=later if later and v > 1 else graph))
# 每一版都发布过(level 记着):口径卡固定的版本须「已发布且未退役」(D-q)
session.add(WorkflowVersion(workflow_id=wf.id, version=v, graph=later if later and v > 1 else graph,
level="published"))
await session.commit()
return wf.id
+11
View File
@@ -266,3 +266,14 @@ def test_run_compare_reads_endpoint_requests():
assert _compare_request(q("erp", "/orders", {"d": "1"}), q("erp", "/orders", {"d": "1"})).structure == "same"
assert _compare_request(q("erp", "/orders", {"d": "1"}), q("erp", "/orders", {"d": "2"})).structure == "params_only"
assert _compare_request(q("erp", "/orders", {}), q("erp", "/refunds", {})).structure == "changed"
async def test_evidence_panel_masks_an_endpoint_snapshot_by_column():
"""证据面板认端点快照:按列名遮(落快照时记下的加能力现在的遮罩字段),不按数据源找、不把整张表当成「数据源不在了」
遮掉。能力登记里查不到的只按快照记下的遮。"""
from app.api.evidence import _mask_sets, _Masks
snap = {"capability": "erp_orders", "source": "erp", "columns": ["单号", "电话", "金额"],
"rows": [["A1", "13800000000", 12]], "mask_columns": ["电话"]}
sets = await _mask_sets(snap, "erp", _Masks())
assert sets.hidden == {"电话"} and sets.sure == {"电话"} and not sets.gone
+9 -13
View File
@@ -252,8 +252,8 @@ async def test_hook_management_functions():
async def test_one_name_one_row_even_under_races(client, monkeypatch):
"""库里还没有 (app_id, name) 的唯一约束:并发生成只留一行;插入后发现别人抢先插了,删掉自己、改去轮换那一行;万一
多出一行,收请求只认最早那一行,停用按名字改全部行。"""
"""(app_id, name) 有唯一索引:并发生成只留一行;插入时发现别人抢先插了(库拒掉),改去轮换那一行;库里不可能
多出第二行,停用按名字改。"""
from app.db.base import utcnow
from app.db.models import AppHook
@@ -269,7 +269,7 @@ async def test_one_name_one_row_even_under_races(client, monkeypatch):
rows = list((await session.execute(select(AppHook).where(AppHook.app_id == app_id))).scalars())
assert len(rows) == 1
# 另一个进程在「读不到行」和「插入」之间抢先插好了:这一次插入后再查,删掉自己、轮换最早那一行
# 另一个进程在「读不到行」和「插入」之间抢先插好了:这一次插入被唯一索引拒掉,改去轮换已有的那一行
real = hooks.hook_row
calls = {"n": 0}
@@ -286,21 +286,17 @@ async def test_one_name_one_row_even_under_races(client, monkeypatch):
async with SessionLocal() as session:
assert len(list((await session.execute(select(AppHook).where(AppHook.app_id == app_id))).scalars())) == 1
# 真的多出一行(多进程各自插入):它的密钥收不到请求;停用两行一起停
from app.core.crypto import encrypt
# 库不许多出第二行:多进程各自插入时,后插的那一次被唯一索引拒掉
from sqlalchemy.exc import IntegrityError
async with SessionLocal() as session:
stray = AppHook(app_id=app_id, name="paid", kind="signal", enabled=True, secret=encrypt("stray-secret") or "",
created_at=utcnow())
session.add(stray)
await session.commit()
stray_id = stray.id
headers, body = signed("stray-secret", {"correlation": "x"}, key="k-stray")
assert (await client.post(f"/api/hooks/{stray_id}", content=body, headers=headers)).status_code == 404
session.add(AppHook(app_id=app_id, name="paid", kind="signal", enabled=True, secret="x", created_at=utcnow()))
with pytest.raises(IntegrityError):
await session.commit()
assert (await client.patch(f"/api/apps/{app_id}/hooks/paid", json={"enabled": False})).status_code == 200
async with SessionLocal() as session:
states = [r.enabled for r in (await session.execute(select(AppHook).where(AppHook.app_id == app_id))).scalars()]
assert states == [False, False]
assert states == [False]
async def test_a_hook_works_by_what_the_published_version_declares(client, monkeypatch):
+1 -1
View File
@@ -443,7 +443,7 @@ async def test_count_and_list_api(client):
await _wait(run.id, ("interrupted",))
task = await _task(run.id)
count = (await client.get("/api/tasks/count?mine=1")).json()
assert set(count) == {"mine", "apps", "other"} and count["other"] >= 1
assert set(count) == {"mine", "apps", "other", "latest"} and count["other"] >= 1 and count["latest"]
listed = (await client.get(f"/api/tasks?assigned=me&run_id={run.id}")).json()["items"]
assert [t["id"] for t in listed] == [task.id]
item = listed[0]
+3 -2
View File
@@ -140,8 +140,9 @@ async def upstream_with_two_versions() -> str:
lib = Workflow(name=f"口径库-{_tag()}", graph=library(), status="published", published_version=2, version=2)
session.add(lib)
await session.flush()
session.add(WorkflowVersion(workflow_id=lib.id, version=1, graph=library()))
session.add(WorkflowVersion(workflow_id=lib.id, version=2, graph=library(version="v4")))
# 两版都发布过(level 记着):口径卡固定的版本须「已发布且未退役」(D-q)
session.add(WorkflowVersion(workflow_id=lib.id, version=1, graph=library(), level="published"))
session.add(WorkflowVersion(workflow_id=lib.id, version=2, graph=library(version="v4"), level="published"))
await session.commit()
return lib.id