mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-21 04:32:05 +00:00
普通用户建应用集成收尾:
- 问数据和个人应用的图放行绑定只读查询等合规工具的调用工具节点,只读和遮罩在这条路径上生效;被治理成写入的工具在只读运行里直接拒;知识库范围在工具层固定(图级默认值、参数名纠错、Agent 都改不了) - 个人应用的刷新和个人蓝图的试运行只有拥有者能读:运行、事件、证据、工件、文件、运行与审批列表、对比、可验证报告、质询、假设分析、提取工作流一并收紧;个人蓝图的试运行不论谁发起都按个人应用约束 - 共享蓝图只给拥有者、对应应用的管理者、管理员,看不到的一律 404;审核副本归审核人;管理员看别人的个人应用只有元数据、可以归档 - 记录表跨版本汇总(只读挂载旧版本、按当前列 UNION ALL、各版本各自固定高水位,缺的版本照实写进印章);停用前发起的运行可以继续写旧版本;更正沿用原提交人、更正人记审计 - 约定收口:待办种类 review、事件 file.extracted、表单错误 file、模板定义带示例与预置;前端类型一次补齐、去掉临时的局部类型;审核待办卡不再给打不开的链接;读附件事件有中文说法;检查脚本补断言
This commit is contained in:
@@ -271,6 +271,28 @@ async def test_on_demand_views(client):
|
||||
await session.commit()
|
||||
|
||||
|
||||
async def test_the_detail_says_whether_the_draft_differs_from_the_published_version(client):
|
||||
"""AppDetail.draft_changed(N6):按发布时的规整方式比草稿和当前发布版——刚发布完一致;改了页面的字、改了岗位绑定都算
|
||||
改过;再发布又一致。只读草稿规整的那一份不算改过(钉住项的级别是发布时按库里补的);没发布过的不给。"""
|
||||
wf, hash_ = await workflow(KPI)
|
||||
spec = kpi_spec(wf, hash_)
|
||||
app_id = await published_app(client, spec, name="按需看板")
|
||||
detail = (await client.get(f"/api/apps/{app_id}")).json()
|
||||
assert detail["draft_changed"] is False, "刚发布完:草稿就是发布版"
|
||||
|
||||
renamed = {**spec, "pages": [{**spec["pages"][0], "title": "周报首页"}]}
|
||||
detail = (await client.patch(f"/api/apps/{app_id}", json={"draft": renamed})).json()
|
||||
assert detail["draft_changed"] is True
|
||||
assert (await client.post(f"/api/apps/{app_id}/publish", json={})).json()["ok"]
|
||||
assert (await client.get(f"/api/apps/{app_id}")).json()["draft_changed"] is False
|
||||
|
||||
bound = {"slots": {"finance": [{"kind": "user", "name": "钱七"}]}}
|
||||
assert (await client.patch(f"/api/apps/{app_id}", json={"draft_bindings": bound})).json()["draft_changed"] is True
|
||||
|
||||
fresh = (await client.post("/api/apps", json={"name": "没发布过", "spec": spec})).json()
|
||||
assert "draft_changed" not in (await client.get(f"/api/apps/{fresh['id']}")).json(), "没发布过的不比"
|
||||
|
||||
|
||||
async def test_auto_views_launch_when_the_page_opens(client):
|
||||
wf, hash_ = await workflow(KPI)
|
||||
app_id = await published_app(client, kpi_spec(wf, hash_, auto=True), name="自动看板")
|
||||
|
||||
@@ -20,12 +20,13 @@ import pytest
|
||||
from app.blueprints.generation import BlueprintApplier, BlueprintChecker, TemplateApplier, TemplateChecker
|
||||
from app.blueprints.instantiate import generated_groups, instantiate
|
||||
from app.blueprints.templates import _common as c
|
||||
from app.blueprints.templates import get_template
|
||||
from app.db.base import SessionLocal
|
||||
import app.db.models # noqa: F401 - 建表前要先登记全部模型(conftest 按 Base.metadata 建)
|
||||
|
||||
|
||||
def _board():
|
||||
extras = c.EXTRAS["periodic_board"]
|
||||
extras = get_template("periodic_board")
|
||||
inst = instantiate(extras.example())
|
||||
group = generated_groups(inst)["fetch_shop"]
|
||||
return extras, inst, group
|
||||
@@ -143,7 +144,7 @@ async def test_apply_ops_and_save_writes_events_and_marks_stale_joints():
|
||||
from app.blueprints.ops import BpRefused
|
||||
from app.db.models_blueprints import Blueprint, BlueprintEvent
|
||||
|
||||
extras = c.EXTRAS["periodic_board"]
|
||||
extras = get_template("periodic_board")
|
||||
spec = extras.example()
|
||||
inst = instantiate(spec, filled=extras.example_fill())
|
||||
bp_id = await _blueprint(spec, template_id="periodic_board", template_version=1, joints=inst.joints)
|
||||
|
||||
@@ -204,7 +204,8 @@ async def test_who_can_start_what(login):
|
||||
listed = {b["id"] for b in (await builder.get("/api/blueprints")).json()["items"]}
|
||||
assert r.json()["id"] in listed and mine["id"] not in listed
|
||||
# 使用者看不到共享蓝图
|
||||
assert (await user.get(f"/api/blueprints/{r.json()['id']}")).status_code == 403
|
||||
# 使用者碰共享蓝图回 404,和看不到的一样,试不出它在不在(D5)
|
||||
assert (await user.get(f"/api/blueprints/{r.json()['id']}")).status_code == 404
|
||||
|
||||
|
||||
async def test_patch_applies_card_ops_as_the_user(client):
|
||||
|
||||
@@ -10,7 +10,7 @@ import uuid
|
||||
import pytest
|
||||
|
||||
from app.blueprints.instantiate import instantiate
|
||||
from app.blueprints.templates import _common as c
|
||||
from app.blueprints.templates import get_template
|
||||
from app.data.engine import engines
|
||||
from app.db.base import SessionLocal
|
||||
from app.db.models import DataSource, Run
|
||||
@@ -48,7 +48,7 @@ async def shop(tmp_path):
|
||||
async def test_the_board_template_runs_and_issues_by_metrics(shop):
|
||||
from app.blueprints.templates._common import safe_id
|
||||
|
||||
extras = c.EXTRAS["periodic_board"]
|
||||
extras = get_template("periodic_board")
|
||||
spec = extras.example()
|
||||
for item in spec["data"]:
|
||||
item["table"]["value"]["source"] = shop
|
||||
|
||||
@@ -14,14 +14,14 @@ import copy
|
||||
import pytest
|
||||
|
||||
from app.blueprints.instantiate import filled_from, generated_groups, instantiate, registry_problems, skeleton_changes
|
||||
from app.blueprints.templates import GENERATED_JOINT_KINDS, TEMPLATE_IDS, get_template
|
||||
from app.blueprints.templates import GENERATED_JOINT_KINDS, TEMPLATE_IDS, check_registry, get_template
|
||||
from app.blueprints.templates import _common as c
|
||||
from app.engine.governance import publish_issues
|
||||
from app.engine.schema import GraphSpec, validate_graph
|
||||
|
||||
|
||||
def _graph_for(template_id: str):
|
||||
if c.EXTRAS[template_id].imports_graph:
|
||||
if get_template(template_id).imports_graph:
|
||||
from app.blueprints.templates.freeform import example_graph
|
||||
|
||||
return example_graph()
|
||||
@@ -30,16 +30,31 @@ def _graph_for(template_id: str):
|
||||
|
||||
def test_registry_is_complete():
|
||||
assert registry_problems() == []
|
||||
assert check_registry() == [], "启动核对(check_registry)走的就是 registry_problems 这一份"
|
||||
assert all(get_template(t) is not None for t in TEMPLATE_IDS)
|
||||
for t in TEMPLATE_IDS:
|
||||
template = get_template(t)
|
||||
assert template.applies_when and template.cards and template.skeleton
|
||||
assert all(note.startswith(("必填", "可选")) for note in template.cards.values()), t
|
||||
# 示例蓝图、预置、每组关节的说明、模板自己认的问题都登记在 TemplateDef 上,没有旁路登记
|
||||
assert callable(template.example) and callable(template.example_fill), t
|
||||
assert template.imports_graph == (t == "freeform")
|
||||
assert not hasattr(c, "EXTRAS")
|
||||
|
||||
|
||||
def test_a_template_without_an_example_is_reported(monkeypatch):
|
||||
"""模板定义、实例化、测试三样缺一样启动即报错:少了示例蓝图的模板,check_registry 照实报出来。"""
|
||||
import dataclasses
|
||||
|
||||
from app.blueprints.templates import _REGISTRY
|
||||
|
||||
monkeypatch.setitem(_REGISTRY, "approval", dataclasses.replace(get_template("approval"), example=None))
|
||||
assert check_registry() == ["模板「approval」没有示例蓝图"]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("template_id", TEMPLATE_IDS)
|
||||
def test_every_template_instantiates_validates_and_publishes(template_id):
|
||||
extras = c.EXTRAS[template_id]
|
||||
extras = get_template(template_id)
|
||||
example = extras.example()
|
||||
graph = _graph_for(template_id)
|
||||
|
||||
@@ -71,7 +86,7 @@ def test_every_template_instantiates_validates_and_publishes(template_id):
|
||||
|
||||
|
||||
def test_board_details():
|
||||
extras = c.EXTRAS["periodic_board"]
|
||||
extras = get_template("periodic_board")
|
||||
inst = instantiate(extras.example(), filled=extras.example_fill(), schedule_id="sch_1", blueprint_id="bp_1")
|
||||
nodes = {n["id"]: n for n in inst.graph["nodes"]}
|
||||
assert nodes["fetch_shop"]["data"]["config"]["tool"] == "db_query__shop"
|
||||
@@ -95,7 +110,7 @@ def test_board_details():
|
||||
|
||||
|
||||
def test_board_with_reused_caliber_and_two_sources():
|
||||
spec = c.EXTRAS["periodic_board"].example()
|
||||
spec = get_template("periodic_board").example()
|
||||
spec["data"].append({"id": "visits", "label": {"value": "访客数", "source": "user", "status": "confirmed", "why": None},
|
||||
"unit": {"value": "人", "source": "user", "status": "confirmed", "why": None},
|
||||
"table": {"value": {"source": "crm", "table": "visits"}, "source": "user",
|
||||
@@ -125,7 +140,7 @@ def test_board_with_reused_caliber_and_two_sources():
|
||||
|
||||
|
||||
def test_approval_details():
|
||||
inst = instantiate(c.EXTRAS["approval"].example())
|
||||
inst = instantiate(get_template("approval").example())
|
||||
nodes = {n["id"]: n for n in inst.graph["nodes"]}
|
||||
finance = nodes["review_finance"]["data"]["config"]
|
||||
assert finance["assignee"] == {"kind": "slot", "slot": "finance"} and finance["policy"] == "all"
|
||||
@@ -143,7 +158,7 @@ def test_approval_details():
|
||||
assert inst.app_spec["entries"][0]["map"] == {"amount": "form.amount", "reason": "form.reason"}
|
||||
assert inst.schedule is None
|
||||
# 确定性关节按卡片换:改了金额线,分支跟着变;骨架比对只许关节位置不同
|
||||
other = c.EXTRAS["approval"].example()
|
||||
other = get_template("approval").example()
|
||||
other["people"][1]["threshold"]["value"]["value"] = 8000
|
||||
changed = instantiate(other)
|
||||
assert {n["id"]: n for n in changed.graph["nodes"]}["gate_finance"]["data"]["config"]["cases"][0]["condition"] \
|
||||
@@ -151,7 +166,7 @@ def test_approval_details():
|
||||
|
||||
|
||||
def test_approval_writeback_confirms_with_last_step(monkeypatch):
|
||||
spec = c.EXTRAS["approval"].example()
|
||||
spec = get_template("approval").example()
|
||||
item = c.template_item
|
||||
spec["writeback"] = {"capability": item("erp_reimburse"), "mapping": item({"amount": "form.amount", "kind": "'报销'"}),
|
||||
"confirm": item(True)}
|
||||
@@ -170,7 +185,7 @@ def test_approval_writeback_confirms_with_last_step(monkeypatch):
|
||||
def test_records_routes_submit_and_summary():
|
||||
from app.records import RECORDS_APPEND
|
||||
|
||||
extras = c.EXTRAS["records"]
|
||||
extras = get_template("records")
|
||||
inst = instantiate(extras.example(), records={"table": "loss", "source": "records_loss"}, blueprint_id="bp_9")
|
||||
nodes = {n["id"]: n for n in inst.graph["nodes"]}
|
||||
append = nodes["append"]["data"]["config"]
|
||||
@@ -190,18 +205,18 @@ def test_records_routes_submit_and_summary():
|
||||
|
||||
|
||||
def test_exception_and_reconcile_details():
|
||||
inst = instantiate(c.EXTRAS["exception_handling"].example())
|
||||
inst = instantiate(get_template("exception_handling").example())
|
||||
nodes = {n["id"]: n for n in inst.graph["nodes"]}
|
||||
assert nodes["handle"]["data"]["config"]["mode"] == "input"
|
||||
assert nodes["handle"]["data"]["config"]["assignee"] == {"kind": "slot", "slot": "owner"}
|
||||
assert nodes["review"]["data"]["config"]["assignee"] == {"kind": "slot", "slot": "区域经理"}
|
||||
assert nodes["gate"]["data"]["config"]["cases"][0]["condition"] == "nodes.parse.row_count > 0"
|
||||
# 只看清单(个人应用能用的那一部分):没有人工节点
|
||||
spec = c.EXTRAS["exception_handling"].example()
|
||||
spec = get_template("exception_handling").example()
|
||||
spec["people"] = []
|
||||
assert not [n for n in instantiate(spec).graph["nodes"] if n["type"] == "human"]
|
||||
|
||||
inst = instantiate(c.EXTRAS["reconcile_writeback"].example())
|
||||
inst = instantiate(get_template("reconcile_writeback").example())
|
||||
write = {n["id"]: n for n in inst.graph["nodes"]}["writeback"]["data"]["config"]
|
||||
assert write["args"] == {"items": "{{ nodes.parse.rows | compact }}", "total": "{{ nodes.caliber.metrics[1].value }}"}
|
||||
assert write["assignee"] == {"kind": "slot", "slot": "check"}
|
||||
@@ -210,7 +225,7 @@ def test_exception_and_reconcile_details():
|
||||
|
||||
|
||||
def test_skeleton_changes_catch_anything_but_joints():
|
||||
extras = c.EXTRAS["periodic_board"]
|
||||
extras = get_template("periodic_board")
|
||||
inst = instantiate(extras.example(), filled=extras.example_fill())
|
||||
joints = inst.joint_defs
|
||||
graph = copy.deepcopy(inst.graph)
|
||||
@@ -228,7 +243,7 @@ def test_skeleton_changes_catch_anything_but_joints():
|
||||
|
||||
|
||||
def test_filled_values_go_stale_when_the_cards_change():
|
||||
extras = c.EXTRAS["periodic_board"]
|
||||
extras = get_template("periodic_board")
|
||||
spec = extras.example()
|
||||
inst = instantiate(spec, filled=extras.example_fill())
|
||||
joints = inst.joints
|
||||
@@ -247,6 +262,6 @@ def test_trial_inputs_follow_the_cadence():
|
||||
|
||||
from app.blueprints.instantiate import trial_inputs
|
||||
|
||||
inputs = trial_inputs(c.EXTRAS["periodic_board"].example(), at=datetime(2026, 10, 15, 3, tzinfo=timezone.utc))
|
||||
inputs = trial_inputs(get_template("periodic_board").example(), at=datetime(2026, 10, 15, 3, tzinfo=timezone.utc))
|
||||
assert inputs == {"period": "2026-W41", "period_start": "2026-10-05", "period_end": "2026-10-11"}
|
||||
assert trial_inputs(c.EXTRAS["approval"].example()) == {}
|
||||
assert trial_inputs(get_template("approval").example()) == {}
|
||||
|
||||
@@ -78,6 +78,7 @@ async def test_trial_runs_the_unpublished_graph_and_previews_with_a_trial_stamp(
|
||||
# 自由形导入的图原样跑(哈希和导入的那一版一样),名字记蓝图名
|
||||
assert graph_hash(run.graph) == bp["graph_hash"] and run.workflow_name == bp["name"]
|
||||
assert run.input["period_start"] == s.PERIOD["period_start"]
|
||||
assert r.json()["inputs"] == run.input, "回包里是这次实际用的输入"
|
||||
|
||||
r = await client.get(f"/api/blueprints/{bp['id']}/preview")
|
||||
assert r.status_code == 200, r.text
|
||||
@@ -171,6 +172,8 @@ async def test_process_trial_assigns_every_step_to_the_trial_runner_and_never_es
|
||||
assert form["trial"] is True and form["handler"] is True and form["task"]["id"] == task.id
|
||||
timeline = find(vm, "instance_timeline")[0]["data"]
|
||||
assert timeline["state"] == "ok" and timeline["status_text"] == "等待审批"
|
||||
# 「换一期再试」的表单随预览给(blueprints/trial.retry_form):流程类是入口的示例表单
|
||||
assert set(vm["trial"]["form"]["properties"]) >= {"amount"}, vm["trial"]["form"]
|
||||
# 首页的「我发起的」列的就是这次试运行
|
||||
home = (await client.get(f"/api/blueprints/{bp['id']}/preview", params={"page": "home"})).json()
|
||||
assert [i["run_id"] for i in find(home, "instance_list")[0]["data"]["items"]] == [run_id]
|
||||
@@ -253,7 +256,8 @@ async def login():
|
||||
|
||||
|
||||
async def test_a_users_trial_follows_the_chat_rules(login, tmp_path):
|
||||
"""使用者试运行自己的个人蓝图:和问数据同一份约束——结果遮罩、只读、记忆是他自己的;图里有使用者不能用的节点不试。"""
|
||||
"""使用者试运行自己的个人蓝图:和问数据同一份约束——结果遮罩、只读、记忆是他自己的;取数的调用工具节点(查数据源)
|
||||
能试(D1),绑了使用者不能用的工具(写入能力)的不试。"""
|
||||
from app.tools.datasource import MASK_RESULTS_OPTION, READ_ONLY_OPTION
|
||||
|
||||
await login.turn_on_local()
|
||||
@@ -282,7 +286,22 @@ async def test_a_users_trial_follows_the_chat_rules(login, tmp_path):
|
||||
source = await s.register_source(s.make_source(tmp_path))
|
||||
bp = await personal(s.board_graph(source))
|
||||
r = await user.post(f"/api/blueprints/{bp['id']}/trial", json={})
|
||||
assert r.status_code == 200, r.text
|
||||
run = await s.wait(r.json()["run_id"])
|
||||
assert run.status == "succeeded" and run.options[READ_ONLY_OPTION] is True, run.error
|
||||
|
||||
# 同一张图里取数节点换成写记录表:使用者不能用的工具,不试
|
||||
from app.db.models_blueprints import Blueprint
|
||||
|
||||
writes = s.board_graph(source)
|
||||
writes["nodes"][1]["data"]["config"] = {"tool": "records_append", "args": {"table": "x", "values": {}}}
|
||||
async with SessionLocal() as session:
|
||||
row = await session.get(Blueprint, bp["id"])
|
||||
row.graph, row.graph_hash = writes, graph_hash(writes)
|
||||
await session.commit()
|
||||
r = await user.post(f"/api/blueprints/{bp['id']}/trial", json={})
|
||||
assert r.status_code == 403 and r.json()["code"] == "trial_policy" and "个人应用" in r.json()["detail"], r.text
|
||||
assert "records_append" in r.json()["detail"]
|
||||
|
||||
|
||||
async def test_a_template_blueprint_trials_with_the_latest_period_and_keeps_its_filled_joints(client, tmp_path):
|
||||
@@ -327,6 +346,8 @@ async def test_a_template_blueprint_trials_with_the_latest_period_and_keeps_its_
|
||||
vm = (await client.get(f"/api/blueprints/{bp['id']}/preview")).json()
|
||||
stamp = find(vm, "stamp")[0]["data"]["stamp"]
|
||||
assert stamp["from"] == TRIAL_FROM and stamp["trial"] is True
|
||||
# 「换一期再试」的表单随预览给:看板类没有入口,是按条件查看的条件表单(图的输入,统计期在里面),界面照它画
|
||||
assert {"period_start", "period_end"} <= set(vm["trial"]["form"]["properties"]), vm["trial"]["form"]
|
||||
# 周期看板有趋势:试运行只有一个点,照实说没有趋势
|
||||
trends = [n["data"] for n in walk(vm["tree"]) if (n.get("data") or {}).get("note") == TRIAL_SERIES]
|
||||
assert trends and len(trends[0]["series"][0]["points"]) == 1
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
"""普通用户建应用的后端约定(docs/design/normal-user-apps.md 第五部分):前端类型(typesApp.ts、typesBlueprint.ts)对着的
|
||||
那些常量和回包形状,在这里钉住,改了就要两边一起改。
|
||||
|
||||
- 待办种类有 review(个人应用提交审核),事件类型有 file.extracted,表单错误 code 有 file;
|
||||
- 记录列表(RecordsPage)每个键都在:next_cursor 没有下一页为 null,state 是 ok / empty / preview;
|
||||
- 审核待办的 payload 不论蓝图在不在,键都一样;
|
||||
- 关节状态带模板声明的说法、类型、分组。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import uuid
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
import app.db.models # noqa: F401 - 建表前要先登记全部模型(conftest 按 Base.metadata 建)
|
||||
import app.db.models_apps # noqa: F401
|
||||
import app.db.models_blueprints # noqa: F401
|
||||
import app.db.models_process # noqa: F401
|
||||
from app.core.config import settings
|
||||
from app.db.base import SessionLocal
|
||||
|
||||
RECORDS_PAGE_KEYS = {"table", "version", "columns", "items", "mine_only", "next_cursor", "state"}
|
||||
REVIEW_PAYLOAD_KEYS = {"kind", "blueprint_id", "app_id", "name", "purpose", "template_id", "submitted_by", "note",
|
||||
"copies"}
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def data_dir(monkeypatch, tmp_path):
|
||||
data = tmp_path / "data"
|
||||
(data / "uploads").mkdir(parents=True)
|
||||
monkeypatch.setattr(settings, "data_dir", data)
|
||||
yield data
|
||||
|
||||
|
||||
def test_constants_the_frontend_types_mirror():
|
||||
from app.apps.spec import FORM_ERROR_CODES
|
||||
from app.core.events import EventType
|
||||
from app.db.models_process import TASK_KINDS
|
||||
from app.files.store import FILE_ERROR_CODE
|
||||
from app.process.tasks import REVIEW_KIND
|
||||
|
||||
assert REVIEW_KIND in TASK_KINDS
|
||||
assert EventType.FILE_EXTRACTED == "file.extracted"
|
||||
assert FILE_ERROR_CODE == "file" and FILE_ERROR_CODE in FORM_ERROR_CODES
|
||||
|
||||
|
||||
async def test_records_page_always_has_every_key():
|
||||
from app.api.deps import Principal
|
||||
from app.apps.resolve import _records_list
|
||||
from app.db.models_apps import App
|
||||
from app.records.listing import page
|
||||
from app.records.tables import ensure_table
|
||||
|
||||
async with SessionLocal() as session:
|
||||
app = App(name=f"报损-{uuid.uuid4().hex[:4]}", created_by="建立人")
|
||||
session.add(app)
|
||||
await session.commit()
|
||||
viewer = Principal(username="amy", role="admin", verified=True, mode="local")
|
||||
async with SessionLocal() as session:
|
||||
empty = await page(session, app, principal=viewer, role="manage")
|
||||
assert set(empty) == RECORDS_PAGE_KEYS and empty["state"] == "empty" and empty["next_cursor"] is None
|
||||
async with SessionLocal() as session:
|
||||
await ensure_table(session, app, [{"name": "门店", "type": "string"}])
|
||||
await session.commit()
|
||||
async with SessionLocal() as session:
|
||||
ok = await page(session, app, principal=viewer, role="manage")
|
||||
assert set(ok) == RECORDS_PAGE_KEYS and ok["state"] == "ok" and ok["items"] == [] and ok["next_cursor"] is None
|
||||
preview = await _records_list(None, SimpleNamespace(draft=True), {"who": "mine"})
|
||||
assert set(preview) == RECORDS_PAGE_KEYS and preview["state"] == "preview", "草稿预览不说成「还没有记录表」"
|
||||
|
||||
|
||||
async def test_review_payload_keeps_its_keys_when_the_blueprint_is_gone():
|
||||
from app.blueprints.personal import review_payload
|
||||
|
||||
async with SessionLocal() as session:
|
||||
got = await review_payload(session, SimpleNamespace(correlation_key="blueprint:gone", app_id="a1"))
|
||||
assert set(got) == REVIEW_PAYLOAD_KEYS
|
||||
assert got["kind"] == "review" and got["blueprint_id"] == "gone" and got["copies"] == [] and got["name"] is None
|
||||
|
||||
|
||||
def test_joint_states_carry_the_templates_label_kind_and_group():
|
||||
from app.blueprints.instantiate import instantiate
|
||||
from app.blueprints.store import blueprint_out
|
||||
from app.blueprints.templates import get_template
|
||||
|
||||
inst = instantiate(get_template("periodic_board").example())
|
||||
defs = {j.id: j for j in inst.joint_defs}
|
||||
assert defs and all(state["kind"] == defs[jid].kind and state["group"] == defs[jid].group
|
||||
and state["label"] == (defs[jid].label or jid) for jid, state in inst.joints.items())
|
||||
bp = SimpleNamespace(id="b1", name="周报", owner=None, scope="shared", status="drafting", spec={},
|
||||
template_id="periodic_board", template_version=1, source={}, conversation_id=None,
|
||||
workflow_id=None, app_id=None, last_trial_run_id=None, graph_hash=None, joints=inst.joints,
|
||||
copied_from=None, created_at=None, updated_at=None)
|
||||
out = blueprint_out(bp, issues=False)["joints"]
|
||||
assert all(set(state) <= {"status", "by", "note", "label", "kind", "group"} for state in out.values())
|
||||
assert all("value" not in state and "basis" not in state for state in out.values()), "填的值不给前端"
|
||||
@@ -227,8 +227,9 @@ async def test_personal_blueprints_need_the_switch(client, monkeypatch):
|
||||
|
||||
def _board_spec(shop: str, crm: str) -> dict:
|
||||
from app.blueprints.templates import _common as c
|
||||
from app.blueprints.templates import get_template
|
||||
|
||||
spec = c.EXTRAS["periodic_board"].example()
|
||||
spec = get_template("periodic_board").example()
|
||||
for item in spec["data"]:
|
||||
item["table"]["value"] = {"source": shop, "table": "orders"}
|
||||
spec["data"] = spec["data"][:2]
|
||||
@@ -381,7 +382,7 @@ async def test_records_fill_uses_the_records_table(client, monkeypatch):
|
||||
这一个表版本上(「键.v版本」)。"""
|
||||
import re
|
||||
|
||||
from app.blueprints.templates import _common as c
|
||||
from app.blueprints.templates import get_template
|
||||
from app.db.models_blueprints import RecordsTable
|
||||
|
||||
def route(messages, n):
|
||||
@@ -396,7 +397,7 @@ async def test_records_fill_uses_the_records_table(client, monkeypatch):
|
||||
{"op": "fill_joint", "joint": "metric_loss_count", "value": f"cell(nodes.{node}, 0, 'loss_count')"},
|
||||
{"op": "done"}]
|
||||
|
||||
bp_id = await _blueprint(spec=c.EXTRAS["records"].example(), template_id="records", template_version=1)
|
||||
bp_id = await _blueprint(spec=get_template("records").example(), template_id="records", template_version=1)
|
||||
try:
|
||||
events = await _events(client, monkeypatch, Scripted(route), "/api/copilot/fill-joints-stream",
|
||||
{"blueprint_id": bp_id})
|
||||
|
||||
@@ -178,15 +178,23 @@ async def test_entry_binds_the_file_once_and_file_text_reads_it(client):
|
||||
assert text.startswith(TEXT[:12]) and f"共 {len(TEXT)} 字" in text and "只读入了前 12 字" in text
|
||||
async with SessionLocal() as session:
|
||||
events = list((await session.execute(select(RunEvent).where(RunEvent.run_id == run_id))).scalars())
|
||||
extracted = [e.data for e in events if e.type == "file.extracted" or e.data.get("code") == "file.extracted"]
|
||||
extracted = [e.data for e in events if e.type == "file.extracted"]
|
||||
assert len(extracted) == 1
|
||||
assert extracted[0]["truncated"] is True and extracted[0]["chars"] == 12
|
||||
assert extracted[0]["total_chars"] == len(TEXT) and extracted[0]["artifact"]
|
||||
# 可验证报告的事件名表认得它(运行时间线的说法在前端 run/decode.ts,check-decode 有一节)
|
||||
from pathlib import Path
|
||||
|
||||
from app import bundle as bundle_pkg
|
||||
|
||||
verifier = (Path(bundle_pkg.__file__).parent / "verifier.js").read_text(encoding="utf-8")
|
||||
assert "'file.extracted': '读入附件'" in verifier
|
||||
|
||||
again = await client.post(f"/api/apps/{app_id}/entries/report", json={"values": {"doc": ref}},
|
||||
headers=actor("amy"))
|
||||
assert again.status_code == 422
|
||||
assert again.json()["errors"][0]["path"] == "doc" and "已经提交过" in again.json()["errors"][0]["message"]
|
||||
assert again.json()["errors"][0]["code"] == "file"
|
||||
|
||||
bobs = (await upload(client, app_id, "别人的.txt", "二店报损".encode(), who="bob")).json()
|
||||
stolen = await client.post(f"/api/apps/{app_id}/entries/report", json={"values": {"doc": bobs}},
|
||||
@@ -355,6 +363,7 @@ async def test_import_period_uses_the_recipe_and_leaves_decisions_to_people(clie
|
||||
|
||||
def test_form_file_widget_rules():
|
||||
from app.apps.forms import input_schema, validate_form, validate_form_schema
|
||||
from app.apps.spec import FORM_ERROR_CODES
|
||||
|
||||
form = {"type": "object", "required": ["doc"], "properties": {"doc": FILE_FIELD}}
|
||||
assert validate_form_schema(form) == []
|
||||
@@ -367,9 +376,10 @@ def test_form_file_widget_rules():
|
||||
ref = {"file_id": "f", "name": "a.txt", "size": 3, "sha256": "0" * 64, "type": "text/plain"}
|
||||
assert validate_form(form, {"doc": ref}) == []
|
||||
assert validate_form(form, {})[0]["code"] == "required"
|
||||
assert validate_form(form, {"doc": "a.txt"})[0]["message"] == "「附件」需要先上传文件"
|
||||
assert validate_form(form, {"doc": "a.txt"})[0] == {"path": "doc", "code": "file", "message": "「附件」需要先上传文件"}
|
||||
wrong = validate_form(form, {"doc": {**ref, "name": "a.pdf", "type": "application/pdf"}})
|
||||
assert wrong and "只收这些类型" in wrong[0]["message"]
|
||||
assert wrong and wrong[0]["code"] == "file" and "只收这些类型" in wrong[0]["message"]
|
||||
assert "file" in FORM_ERROR_CODES, "文件控件的错误有自己的 code(前端 FormError.code 同一份)"
|
||||
schema = input_schema([{"name": "doc", "type": "file", "required": True, "accept": [".pdf"], "max_mb": 5}])
|
||||
assert schema["properties"]["doc"] == {"type": "object", "x-widget": "file", "title": "doc",
|
||||
"x-file": {"accept": [".pdf"], "max_mb": 5}}
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
"""前端约定(frontend/src/typesApp.ts、typesBlueprint.ts)里的取值和服务端的常量逐个对上。
|
||||
|
||||
前端的联合类型是照服务端的常量抄的:服务端加了一种(待办种类 review、运行来源 trial / personal_app、查看对象 caliber、
|
||||
表单错误 file……),前端没跟上,界面就把它当成别的、或者写不出名字;反过来前端多写了一种,界面就在等一个永远不会来的值。
|
||||
这里直接读两份 .ts 文件,把联合类型里的字面量和服务端的常量逐个比(能比顺序的连顺序一起比),改了一边没改另一边就过不去。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
import typing
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
TYPES_APP = ROOT / "frontend" / "src" / "typesApp.ts"
|
||||
TYPES_BLUEPRINT = ROOT / "frontend" / "src" / "typesBlueprint.ts"
|
||||
TYPES = ROOT / "frontend" / "src" / "types.ts"
|
||||
|
||||
_COMMENT = re.compile(r"/\*.*?\*/|//[^\n]*", re.S)
|
||||
|
||||
|
||||
def _source(path: Path) -> str:
|
||||
"""去掉注释的源码:注释里举例写的字面量不算进联合类型。"""
|
||||
return _COMMENT.sub("", path.read_text(encoding="utf-8"))
|
||||
|
||||
|
||||
def _union(path: Path, name: str) -> list[str]:
|
||||
"""`export type Name = | 'a' | 'b'` 里的字面量,按写的顺序。"""
|
||||
text = _source(path)
|
||||
found = re.search(rf"export type {name} =(.*?)(?=\nexport |\n\n|\Z)", text, re.S)
|
||||
assert found is not None, f"{path.name} 里没有 export type {name}"
|
||||
return re.findall(r"'([^']+)'", found.group(1))
|
||||
|
||||
|
||||
def _field(path: Path, interface: str, field: str) -> list[str]:
|
||||
"""`export interface X { field: 'a' | 'b' }` 里这一项的字面量,按写的顺序。"""
|
||||
text = _source(path)
|
||||
body = re.search(rf"export interface {interface}\b[^{{]*\{{(.*?)\n\}}", text, re.S)
|
||||
assert body is not None, f"{path.name} 里没有 export interface {interface}"
|
||||
line = re.search(rf"^\s*{re.escape(field)}\??: ([^\n]+)$", body.group(1), re.M)
|
||||
assert line is not None, f"{interface} 里没有 {field}"
|
||||
return re.findall(r"'([^']+)'", line.group(1))
|
||||
|
||||
|
||||
def test_task_kinds_and_statuses():
|
||||
from app.db.models_process import TASK_KINDS, TASK_STATUSES
|
||||
|
||||
assert _union(TYPES_APP, "TaskKind") == list(TASK_KINDS), "待办种类(含 review)"
|
||||
assert _union(TYPES_APP, "TaskStatus") == list(TASK_STATUSES)
|
||||
|
||||
|
||||
def test_origin_kinds_in_the_same_order():
|
||||
from app.engine.origin import ORIGIN_KINDS
|
||||
|
||||
# 运行列表按来源筛选的下拉照这个顺序(lib/terms.ORIGIN_LABEL 的类型是 Record<OriginKind, string>,少一个编译不过)
|
||||
assert _union(TYPES_APP, "OriginKind") == list(ORIGIN_KINDS)
|
||||
|
||||
|
||||
def test_look_object_kinds():
|
||||
from app.look.base import LOOK_KINDS
|
||||
|
||||
assert _field(TYPES_APP, "LookObject", "kind") == list(LOOK_KINDS), "查看对象(含口径卡 caliber)"
|
||||
|
||||
|
||||
def test_form_contract():
|
||||
from app.apps.spec import FILE_FIELD_TYPE, FORM_ERROR_CODES, FORM_FORMATS, FORM_TYPES, FORM_WIDGETS
|
||||
|
||||
assert _field(TYPES_APP, "FormError", "code") == list(FORM_ERROR_CODES), "表单错误(含文件控件的 file)"
|
||||
assert _field(TYPES_APP, "FormField", "type") == [*FORM_TYPES, FILE_FIELD_TYPE], "字段类型:object 只给文件控件"
|
||||
assert _union(TYPES_APP, "FormWidget") == list(FORM_WIDGETS)
|
||||
assert _field(TYPES_APP, "FormField", "format") == list(FORM_FORMATS)
|
||||
file_ref = re.search(r"export interface FileRef \{([^}]*)\}", _source(TYPES_BLUEPRINT))
|
||||
assert file_ref is not None
|
||||
from app.apps.spec import FILE_REF_KEYS
|
||||
|
||||
assert re.findall(r"(\w+):", file_ref.group(1)) == list(FILE_REF_KEYS), "文件引用的五个键"
|
||||
|
||||
|
||||
def test_app_view_source_kinds():
|
||||
from app.apps.spec import ViewSource
|
||||
|
||||
kinds = list(typing.get_args(ViewSource.model_fields["kind"].annotation))
|
||||
assert _field(TYPES_APP, "AppViewSource", "kind") == kinds, "视图来源(个人应用的 exploratory)"
|
||||
# 解析出来的来源另有试运行预览的 trial(apps/resolve.Source.out)
|
||||
assert _field(TYPES_APP, "ViewSourceState", "kind") == [*kinds, "trial"]
|
||||
|
||||
|
||||
def test_blueprint_enums():
|
||||
from app.blueprints.publish_plan import LEVELS, PROBLEM_WHO, STEP_KINDS
|
||||
from app.blueprints.spec import BP_OPS, ITEM_SOURCES, ITEM_STATUSES
|
||||
from app.blueprints.templates import JOINT_KINDS
|
||||
from app.db.models_blueprints import BLUEPRINT_SCOPES, BLUEPRINT_SOURCES, BLUEPRINT_STATUSES, PLAN_STATUSES
|
||||
|
||||
assert _union(TYPES_BLUEPRINT, "ItemSource") == list(ITEM_SOURCES)
|
||||
assert _union(TYPES_BLUEPRINT, "ItemStatus") == list(ITEM_STATUSES)
|
||||
assert _union(TYPES_BLUEPRINT, "BpOpName") == list(BP_OPS)
|
||||
assert _union(TYPES_BLUEPRINT, "BlueprintScope") == list(BLUEPRINT_SCOPES)
|
||||
assert _union(TYPES_BLUEPRINT, "BlueprintStatus") == list(BLUEPRINT_STATUSES)
|
||||
assert _union(TYPES_BLUEPRINT, "BlueprintSourceKind") == list(BLUEPRINT_SOURCES)
|
||||
assert _union(TYPES_BLUEPRINT, "JointKind") == list(JOINT_KINDS)
|
||||
assert _union(TYPES_BLUEPRINT, "PlanStepKind") == list(STEP_KINDS), "发布单的步骤(顺序即执行顺序)"
|
||||
assert _field(TYPES_BLUEPRINT, "PublishPlanRow", "status") == list(PLAN_STATUSES)
|
||||
assert _field(TYPES_BLUEPRINT, "PublishPlanRow", "level") == list(LEVELS)
|
||||
assert _union(TYPES_BLUEPRINT, "ProblemWho") == list(PROBLEM_WHO)
|
||||
|
||||
|
||||
def test_trial_refusal_codes():
|
||||
from app.blueprints import trial
|
||||
|
||||
assert _union(TYPES_BLUEPRINT, "TrialRefusalCode") == [trial.NOT_READY, trial.NO_GRAPH, trial.POLICY, trial.FORM,
|
||||
trial.NO_RUN]
|
||||
|
||||
|
||||
def test_record_rows_carry_every_system_column():
|
||||
from app.records import SYSTEM_COLUMNS
|
||||
|
||||
row = re.search(r"export interface RecordRow \{(.*?)\n\}", _source(TYPES_BLUEPRINT), re.S)
|
||||
assert row is not None
|
||||
fields = re.findall(r"^\s*(\w+)\??:", row.group(1), re.M)
|
||||
assert set(SYSTEM_COLUMNS) <= set(fields), f"RecordRow 少了系统列:{set(SYSTEM_COLUMNS) - set(fields)}"
|
||||
assert "_version" in fields, "这一行在第几版表里(records/listing._shown)"
|
||||
|
||||
|
||||
def test_conversation_kinds_include_what_the_server_creates():
|
||||
# 前端建的只有 chat、canvas(api/conversations.ConversationIn);服务端另建应用助手的 app、蓝图对话的 blueprint
|
||||
assert _field(TYPES, "Conversation", "kind") == ["chat", "canvas", "app", "blueprint"]
|
||||
@@ -17,6 +17,7 @@ from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import copy
|
||||
import json
|
||||
import uuid
|
||||
from datetime import timedelta
|
||||
|
||||
@@ -28,7 +29,7 @@ from app.api.deps import Principal
|
||||
from app.api.settings import APPS_GROUP
|
||||
from app.core.artifact_store import graph_hash
|
||||
from app.db.base import SessionLocal, utcnow
|
||||
from app.db.models import Run, Setting, Workflow, WorkflowVersion
|
||||
from app.db.models import Run, RunEvent, Setting, Workflow, WorkflowVersion
|
||||
from app.db.models_apps import App
|
||||
from app.db.models_blueprints import Blueprint, BlueprintEvent
|
||||
from app.db.models_process import Task, TaskAssignment
|
||||
@@ -165,13 +166,17 @@ async def test_the_switch_gates_users_but_not_builders():
|
||||
|
||||
# 搭建者不受开关管(§3.12)
|
||||
assert (await c["bob"].get("/api/apps")).json()["can_create"] == {"shared": True, "personal": True}
|
||||
await saved(c["bob"], await blueprint("bob"))
|
||||
bob_app = await saved(c["bob"], await blueprint("bob"))
|
||||
|
||||
await switch(True)
|
||||
assert (await c["uma"].get("/api/apps")).json()["can_create"] == {"shared": False, "personal": True}
|
||||
app_id = await saved(c["uma"], uma_bp)
|
||||
assert (await c["uma"].get(f"/api/apps/{app_id}")).json()["personal"]["allowed"] is True
|
||||
|
||||
await switch(False)
|
||||
# 关了开关:个人应用照样看得到,详情照同一条规则说现在不能刷新、提交审核(界面照它置灰,不自己再推一遍)
|
||||
assert (await c["uma"].get(f"/api/apps/{app_id}")).json()["personal"]["allowed"] is False
|
||||
assert (await c["bob"].get(f"/api/apps/{bob_app}")).json()["personal"]["allowed"] is True, "搭建者不受开关管"
|
||||
assert (await c["uma"].post(f"/api/apps/{app_id}/refresh")).json()["code"] == "personal.off"
|
||||
r = await c["uma"].post(f"/api/apps/{app_id}/submit-review", json={"to": [{"kind": "user", "name": "bob"}]})
|
||||
assert r.status_code == 403 and r.json()["code"] == "personal.off"
|
||||
@@ -398,19 +403,31 @@ async def test_only_the_owner_sees_a_personal_app():
|
||||
|
||||
|
||||
async def test_an_admin_sees_that_it_exists_and_can_archive_it_but_not_its_content():
|
||||
"""管理员看别人的个人应用(D7):列表和详情里只有名称、拥有者、状态、创建和更新时间,能归档;页面、草稿、候选、
|
||||
版本、刷新的运行、蓝图一概看不到。"""
|
||||
root, c = await people()
|
||||
await switch(True)
|
||||
app_id = await saved(c["uma"], await blueprint("uma"))
|
||||
await refreshed(c["uma"], app_id)
|
||||
for path in (f"/api/apps/{app_id}", f"/api/apps/{app_id}/pages/home", f"/api/apps/{app_id}/pages/home?draft=1",
|
||||
f"/api/apps/{app_id}/candidates", f"/api/apps/{app_id}/versions"):
|
||||
bp = await blueprint("uma")
|
||||
app_id = await saved(c["uma"], bp)
|
||||
run = await refreshed(c["uma"], app_id)
|
||||
for path in (f"/api/apps/{app_id}/pages/home", f"/api/apps/{app_id}/pages/home?draft=1",
|
||||
f"/api/apps/{app_id}/candidates", f"/api/apps/{app_id}/versions", f"/api/blueprints/{bp}"):
|
||||
assert (await root.get(path)).status_code == 404, path
|
||||
for path in (f"/api/runs/{run.id}", f"/api/runs/{run.id}/evidence"):
|
||||
assert (await root.get(path)).status_code == 403, path
|
||||
listed = next(a for a in (await root.get("/api/apps")).json()["items"] if a["id"] == app_id)
|
||||
assert (listed["owner"], listed["scope"], listed["my_role"], listed["description"], listed["name"]) == (
|
||||
"uma", "personal", None, "", "我的周报")
|
||||
assert listed["created_at"] and listed["updated_at"] and listed["blueprint_id"] is None
|
||||
detail = await root.get(f"/api/apps/{app_id}")
|
||||
assert detail.status_code == 200, detail.text
|
||||
assert {k: detail.json()[k] for k in listed} == listed, "详情和列表里那一条一样,只有元数据"
|
||||
assert not {"draft", "draft_bindings", "personal"} & set(detail.json()) and detail.json()["published"] is None
|
||||
r = await root.post(f"/api/apps/{app_id}/archive")
|
||||
assert r.status_code == 200 and r.json()["status"] == "archived", r.text
|
||||
assert "draft" not in r.json() and "personal" not in r.json()
|
||||
# 别的搭建者连元数据也没有:当这个应用不存在
|
||||
assert (await c["bob"].get(f"/api/apps/{app_id}")).status_code == 404
|
||||
r = await c["uma"].post(f"/api/apps/{app_id}/refresh")
|
||||
assert r.status_code == 409 and "已归档" in r.json()["detail"]
|
||||
|
||||
@@ -471,6 +488,11 @@ async def test_review_gives_the_builder_a_shared_copy_and_leaves_the_original(mo
|
||||
assert (dup["copied_from"], dup["scope"], dup["owner"], dup["status"]) == (bp, "shared", "bob", "drafting")
|
||||
assert dup["graph_hash"] == graph_hash(KPI) and dup["spec"]["purpose"]["value"] == "每周看销售额"
|
||||
assert (await c["bob"].post(f"/api/blueprints/{bp}/copy")).json()["id"] == dup["id"], "再取是同一份"
|
||||
# 副本归处理审核的人(D5):别的搭建者看不到;审核的人也读不到拥有者那次刷新(D2,只给拥有者)
|
||||
assert (await c["bob"].get(f"/api/blueprints/{dup['id']}")).status_code == 200
|
||||
assert (await c["bea"].get(f"/api/blueprints/{dup['id']}")).status_code == 404
|
||||
assert dup["id"] not in [b["id"] for b in (await c["bea"].get("/api/blueprints")).json()["items"]]
|
||||
assert (await c["bob"].get(f"/api/runs/{run.id}")).status_code == 403
|
||||
async with SessionLocal() as session:
|
||||
original = await session.get(Blueprint, bp)
|
||||
assert original.status == "in_review" and original.scope == "personal" and original.graph == KPI
|
||||
@@ -812,11 +834,11 @@ async def test_a_personal_blueprint_never_creates_a_records_table():
|
||||
个人蓝图建记录表、登记全局数据源。"""
|
||||
from app.blueprints.ops import blueprint_issues
|
||||
from app.blueprints.store import instantiate_context
|
||||
from app.blueprints.templates._common import EXTRAS
|
||||
from app.blueprints.templates import get_template
|
||||
from app.db.models import DataSource
|
||||
from app.db.models_blueprints import RecordsTable
|
||||
|
||||
spec = EXTRAS["records"].example()
|
||||
spec = get_template("records").example()
|
||||
assert any(i["code"] == "bp.template_mismatch" and i["path"] == "/template"
|
||||
for i in blueprint_issues(spec, for_="personal"))
|
||||
async with SessionLocal() as session:
|
||||
@@ -830,3 +852,284 @@ async def test_a_personal_blueprint_never_creates_a_records_table():
|
||||
assert "records" not in ctx
|
||||
assert not (await session.execute(select(RecordsTable).where(RecordsTable.table_key == row.id))).first()
|
||||
assert int(await session.scalar(select(func.count(DataSource.id))) or 0) == sources
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 只有拥有者读得到的运行(D2)
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
async def test_personal_runs_are_read_by_their_owner_only():
|
||||
"""个人应用的刷新、个人蓝图的试运行只有拥有者读得到:管理员、搭建者也不行——运行记录、事件、运行时的图、证据、工件、
|
||||
对比、可验证报告、文件下载、运行列表、审批列表、实时流一个口径(auth/access.private_owner / hidden_runs)。
|
||||
共享蓝图的试运行、别的运行照旧给搭建者读。"""
|
||||
from app.api.deps import run_events_readable
|
||||
from app.auth import artifact_readable, run_readable
|
||||
from app.db.models import Artifact
|
||||
from app.db.models_blueprints import StoredFile
|
||||
from tests.test_user_chat_access import _cookie, _refused, _ws
|
||||
|
||||
root, c = await people()
|
||||
await switch(True)
|
||||
bp = await blueprint("uma", trialed=False)
|
||||
r = await c["uma"].post(f"/api/blueprints/{bp}/trial", json={})
|
||||
assert r.status_code == 200, r.text
|
||||
trial = await wait(r.json()["run_id"])
|
||||
assert trial.status == "succeeded" and trial.origin_kind == "trial", trial.error
|
||||
app_id = await saved(c["uma"], bp)
|
||||
refresh = await refreshed(c["uma"], app_id)
|
||||
async with SessionLocal() as session:
|
||||
shared_bp = Blueprint(name="共享周报", owner="bob", scope="shared", status="drafting", spec={},
|
||||
source={"kind": "template"})
|
||||
session.add(shared_bp)
|
||||
await session.flush()
|
||||
shared_trial = Run(workflow_name="共享周报", graph=KPI, status="succeeded", origin_kind="trial",
|
||||
origin={"blueprint_id": shared_bp.id}, started_by="bob")
|
||||
old = Run(workflow_name="老运行", graph=KPI, status="succeeded", origin_kind=None, started_by="bob")
|
||||
session.add_all([shared_trial, old])
|
||||
await session.commit()
|
||||
assert await session.scalar(select(Artifact.id).where(Artifact.run_id == refresh.id).limit(1)), "刷新落了工件"
|
||||
# 工件按内容寻址,同样内容的工件可能也挂在别的(读得到的)运行上:另落一件只挂在这次刷新上的
|
||||
from app.core.artifact_store import put_json
|
||||
|
||||
artifact = await put_json({"note": uuid.uuid4().hex}, kind="node_output", run_id=refresh.id, node_id="card")
|
||||
up = await c["uma"].post("/api/files", files={"file": ("说明.txt", "本周说明".encode(), "text/plain")},
|
||||
data={"context": json.dumps({"blueprint_id": bp})})
|
||||
assert up.status_code == 201, up.text
|
||||
file_id = up.json()["file_id"]
|
||||
async with SessionLocal() as session:
|
||||
(await session.get(StoredFile, file_id)).bound_run_id = trial.id
|
||||
await session.commit()
|
||||
|
||||
def principal(name: str, role: str) -> Principal:
|
||||
return Principal(username=name, role=role, verified=True, mode="local")
|
||||
|
||||
for who, role in (("root", "admin"), ("bob", "builder")):
|
||||
client = root if who == "root" else c[who]
|
||||
for run in (trial, refresh):
|
||||
for path in ("", "/events", "/graph", "/evidence", "/compare/base", "/bundle.html", "/artifacts"):
|
||||
r = await client.get(f"/api/runs/{run.id}{path}")
|
||||
assert r.status_code == 403, (who, run.origin_kind, path, r.status_code)
|
||||
assert not await run_readable(principal(who, role), run.id)
|
||||
assert not await run_events_readable(principal(who, role), run)
|
||||
assert (await client.get(f"/api/artifacts/{artifact}")).status_code == 403, who
|
||||
assert not await artifact_readable(principal(who, role), artifact)
|
||||
assert (await client.get(f"/api/files/{file_id}")).status_code == 403, who
|
||||
listed = {r["id"] for r in (await client.get("/api/runs?limit=200")).json()}
|
||||
assert not {trial.id, refresh.id} & listed, who
|
||||
assert {shared_trial.id, old.id} <= listed, "共享蓝图的试运行、来源没记的老运行照旧列出"
|
||||
assert (await client.get(f"/api/runs/{shared_trial.id}")).status_code == 200
|
||||
assert _refused(await _ws(f"/api/runs/{refresh.id}/stream", _cookie(client))), "实时流握手前就拒"
|
||||
|
||||
for run in (trial, refresh):
|
||||
assert await run_readable(principal("uma", "user"), run.id)
|
||||
assert (await c["uma"].get(f"/api/runs/{run.id}/evidence")).status_code == 200
|
||||
assert (await c["uma"].get(f"/api/files/{file_id}")).status_code == 200
|
||||
assert await artifact_readable(principal("uma", "user"), artifact)
|
||||
# 拥有者是搭建者时:他自己的个人应用刷新、个人蓝图试运行照样读得到、列得出,别的搭建者读不到;试运行和刷新一样
|
||||
# 遮罩、只读(试运行是刷新的预演)
|
||||
from app.tools.datasource import MASK_RESULTS_OPTION, READ_ONLY_OPTION
|
||||
|
||||
bob_bp = await blueprint("bob", trialed=False)
|
||||
r = await c["bob"].post(f"/api/blueprints/{bob_bp}/trial", json={})
|
||||
assert r.status_code == 200, r.text
|
||||
bob_trial = await wait(r.json()["run_id"])
|
||||
assert bob_trial.options[READ_ONLY_OPTION] is True and bob_trial.options[MASK_RESULTS_OPTION] is True
|
||||
assert bob_trial.memory_scope == "chat:bob"
|
||||
bob_run = await refreshed(c["bob"], await saved(c["bob"], bob_bp))
|
||||
for run_id in (bob_trial.id, bob_run.id):
|
||||
assert (await c["bob"].get(f"/api/runs/{run_id}")).status_code == 200
|
||||
assert run_id in {r["id"] for r in (await c["bob"].get("/api/runs?limit=200")).json()}
|
||||
assert (await c["bea"].get(f"/api/runs/{run_id}")).status_code == 403
|
||||
|
||||
|
||||
async def test_the_other_doors_to_a_private_run_are_shut_too(monkeypatch):
|
||||
"""D2 的其余入口:别人的个人应用刷新、个人蓝图试运行,管理员、搭建者从这些地方同样碰不到——「提取成工作流」、运行复核、
|
||||
从问数据的回答开始建蓝图(当它不存在);质询、假设分析(403);审批列表、探索问题聚类里不出现。拥有者自己的照旧能用、
|
||||
列得出;别的运行照旧。"""
|
||||
from app.api.deps import PRIVATE_RUN
|
||||
from app.db.models import Approval
|
||||
from app.memory import embeddings
|
||||
|
||||
root, c = await people()
|
||||
await switch(True)
|
||||
bp = await blueprint("uma", trialed=False)
|
||||
r = await c["uma"].post(f"/api/blueprints/{bp}/trial", json={})
|
||||
assert r.status_code == 200, r.text
|
||||
trial = await wait(r.json()["run_id"])
|
||||
refresh = await refreshed(c["uma"], await saved(c["uma"], bp))
|
||||
bob_bp = await blueprint("bob", trialed=False)
|
||||
r = await c["bob"].post(f"/api/blueprints/{bob_bp}/trial", json={})
|
||||
assert r.status_code == 200, r.text
|
||||
bob_trial = await wait(r.json()["run_id"])
|
||||
question = {"question": "上周各门店的销售额"}
|
||||
async with SessionLocal() as session:
|
||||
# 聚类只列两条以上的簇:别的探索运行放两条
|
||||
shared, also = (Run(workflow_name="共享的运行", graph=KPI, status="succeeded", run_class="exploratory",
|
||||
origin_kind="chat", started_by="bob", input=question) for _ in range(2))
|
||||
session.add_all([shared, also])
|
||||
await session.flush()
|
||||
for run_id in (trial.id, refresh.id, bob_trial.id):
|
||||
(await session.get(Run, run_id)).input = question
|
||||
approvals = {run_id: Approval(run_id=run_id, node_id="card", title="看一眼") for run_id in
|
||||
(refresh.id, bob_trial.id, shared.id)}
|
||||
session.add_all(approvals.values())
|
||||
await session.commit()
|
||||
approval_ids = {run_id: a.id for run_id, a in approvals.items()}
|
||||
|
||||
async def _same(texts):
|
||||
return [[1.0, 0.0] for _ in texts]
|
||||
|
||||
# 聚类只看谁进了候选:每条输入的向量一样,候选就都在一簇里
|
||||
monkeypatch.setattr(embeddings, "embed_texts", _same)
|
||||
|
||||
async def clustered(client) -> set[str]:
|
||||
body = (await client.get("/api/governance/exploratory-clusters", params={"limit": 500})).json()
|
||||
return {rid for cluster in body["clusters"] for rid in cluster["run_ids"]}
|
||||
|
||||
async def approvals_of(client) -> set[str]:
|
||||
return {a["id"] for a in (await client.get("/api/approvals", params={"status": "all", "limit": 500})).json()}
|
||||
|
||||
for who, client in (("root", root), ("bob", c["bob"])):
|
||||
for run in (trial, refresh):
|
||||
r = await client.post("/api/copilot/from-run", json={"run_id": run.id})
|
||||
assert r.status_code == 404, (who, run.origin_kind, r.status_code)
|
||||
r = await client.post("/api/copilot/review", json={"run_id": run.id})
|
||||
assert r.status_code == 403 and r.json()["detail"] == PRIVATE_RUN, (who, r.status_code)
|
||||
r = await client.post("/api/blueprints", json={"source": {"kind": "run", "id": run.id}, "scope": "shared"})
|
||||
assert r.status_code == 404, (who, r.status_code)
|
||||
assert (await client.get(f"/api/runs/{run.id}/inquiries")).status_code == 403, who
|
||||
assert (await client.post(f"/api/runs/{run.id}/inquiries", json={"question": "x"})).status_code == 403
|
||||
assert (await client.post(f"/api/runs/{run.id}/evidence/whatif", json={})).status_code == 403, who
|
||||
assert approval_ids[refresh.id] not in await approvals_of(client), who
|
||||
assert approval_ids[shared.id] in await approvals_of(client), "别的运行上的审批照旧列出"
|
||||
seen = await clustered(client)
|
||||
assert not {trial.id, refresh.id} & seen, who
|
||||
assert shared.id in seen, "别的探索运行照旧进聚类"
|
||||
|
||||
# 拥有者是搭建者时:自己的个人蓝图试运行照样能用、列得出;别的搭建者不行
|
||||
assert approval_ids[bob_trial.id] in await approvals_of(c["bob"])
|
||||
assert approval_ids[bob_trial.id] not in await approvals_of(c["bea"])
|
||||
assert bob_trial.id in await clustered(c["bob"]) and bob_trial.id not in await clustered(c["bea"])
|
||||
assert (await c["bob"].get(f"/api/runs/{bob_trial.id}/inquiries")).status_code == 200
|
||||
assert (await c["bea"].get(f"/api/runs/{bob_trial.id}/inquiries")).status_code == 403
|
||||
assert (await c["bob"].post("/api/copilot/review", json={"run_id": bob_trial.id})).status_code == 200
|
||||
r = await c["bob"].post("/api/blueprints", json={"source": {"kind": "run", "id": bob_trial.id}, "scope": "shared"})
|
||||
assert r.status_code == 201, r.text
|
||||
assert (await c["bea"].post("/api/blueprints", json={"source": {"kind": "run", "id": bob_trial.id},
|
||||
"scope": "shared"})).status_code == 404
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 按模板做个人应用(D1:取数的调用工具节点过同一份白名单)
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _example(template_id: str, **cards) -> tuple[dict, dict, dict | None]:
|
||||
"""模板登记的示例蓝图(卡片可以改几张)、生成关节的填法、自由形导入的示例图。"""
|
||||
from app.blueprints.templates import get_template
|
||||
from app.blueprints.templates.freeform import example_graph
|
||||
|
||||
template = get_template(template_id)
|
||||
spec = {**template.example(), **cards}
|
||||
return spec, template.example_fill(), example_graph() if template.imports_graph else None
|
||||
|
||||
|
||||
@pytest.mark.parametrize("template_id, cards", [
|
||||
("periodic_board", {}),
|
||||
# 异常处置只看清单的那一部分:不写「谁来做」,没有处置和复核
|
||||
("exception_handling", {"people": []}),
|
||||
("freeform", {}),
|
||||
])
|
||||
def test_the_template_examples_that_can_be_personal_pass_the_personal_rules(template_id, cards):
|
||||
"""模板说能做成个人应用的那一部分,示例实例化出来的图过个人应用的约束(取数节点是调用工具节点查数据源)。"""
|
||||
from app.blueprints.instantiate import instantiate
|
||||
from app.blueprints.personal import policy_problem
|
||||
from app.blueprints.templates import get_template
|
||||
|
||||
assert get_template(template_id).personal
|
||||
spec, fill, graph = _example(template_id, **cards)
|
||||
inst = instantiate(spec, filled=fill, graph=graph)
|
||||
assert not [p for p in inst.problems if p["level"] == "error"], inst.problems
|
||||
nodes = (graph or inst.graph)["nodes"]
|
||||
assert any(n["type"] == "tool" and n["data"]["config"]["tool"].startswith("db_query__") for n in nodes)
|
||||
assert policy_problem(graph or inst.graph) is None
|
||||
|
||||
|
||||
def test_the_parts_that_cannot_be_personal_are_still_refused():
|
||||
"""异常处置写了「谁来做」就有人工处置;审批、对账回写、填报登记不能做成个人应用(personal 为 None)。"""
|
||||
from app.blueprints.instantiate import instantiate
|
||||
from app.blueprints.personal import policy_problem
|
||||
from app.blueprints.templates import get_template
|
||||
|
||||
spec, fill, _ = _example("exception_handling")
|
||||
problem = policy_problem(instantiate(spec, filled=fill).graph)
|
||||
assert problem and "人工" in problem
|
||||
for template_id in ("approval", "reconcile_writeback", "records"):
|
||||
assert get_template(template_id).personal is None, template_id
|
||||
|
||||
|
||||
async def test_a_personal_board_from_the_template_trials_saves_and_refreshes(tmp_path):
|
||||
"""使用者按周期看板模板(示例卡片,库换成现造的)做个人应用:试运行、存成我的应用、刷新都跑通。取数的调用工具节点
|
||||
在刷新里照样遮罩、只读:遮罩列只有「已遮罩」,页面上的数是这次刷新算的。"""
|
||||
import sqlite3
|
||||
from datetime import date, timedelta as days
|
||||
|
||||
from app.blueprints.instantiate import instantiate
|
||||
from app.data import introspect
|
||||
from app.db.models import DataSource
|
||||
from app.tools.datasource import MASK_RESULTS_OPTION, READ_ONLY_OPTION
|
||||
|
||||
_, c = await people()
|
||||
await switch(True)
|
||||
name = f"shop_{uuid.uuid4().hex[:6]}"
|
||||
path = tmp_path / f"{name}.db"
|
||||
today = date.today()
|
||||
with sqlite3.connect(path) as db:
|
||||
db.execute("CREATE TABLE orders (id INTEGER PRIMARY KEY, amount REAL, refunded INTEGER, paid_at TEXT, "
|
||||
"phone TEXT)")
|
||||
db.executemany("INSERT INTO orders (amount, refunded, paid_at, phone) VALUES (?, ?, ?, ?)",
|
||||
[(100.0, i % 5 == 0, (today - days(days=i)).isoformat(), "13900000001") for i in range(40)])
|
||||
async with SessionLocal() as session:
|
||||
row = DataSource(name=name, kind="sqlite", database=str(path), readonly=False, enabled=True,
|
||||
options={"mask_columns": "phone"})
|
||||
session.add(row)
|
||||
await session.flush()
|
||||
row.schema_cache = await introspect.introspect(row)
|
||||
await session.commit()
|
||||
|
||||
r = await c["uma"].post("/api/blueprints", json={"source": {"kind": "template", "id": "periodic_board"},
|
||||
"scope": "personal", "name": "我的门店周报"})
|
||||
assert r.status_code == 201, r.text
|
||||
bp = r.json()["id"]
|
||||
spec, fill, _ = _example("periodic_board")
|
||||
spec = json.loads(json.dumps(spec, ensure_ascii=False).replace('"source": "shop"', f'"source": "{name}"'))
|
||||
fill = json.loads(json.dumps(fill, ensure_ascii=False).replace("fetch_shop", f"fetch_{name}"))
|
||||
# 取数时顺带查了遮罩列:刷新里它只会是「已遮罩」
|
||||
fill[f"sql_fetch_{name}"] = fill[f"sql_fetch_{name}"].replace(" FROM orders", ", MAX(phone) AS phone FROM orders")
|
||||
filled = instantiate(spec, filled=fill, name="我的门店周报", blueprint_id=bp)
|
||||
assert not [p for p in filled.problems if p["level"] == "error"], filled.problems
|
||||
async with SessionLocal() as session:
|
||||
row = await session.get(Blueprint, bp)
|
||||
row.spec, row.joints = spec, filled.joints
|
||||
await session.commit()
|
||||
|
||||
r = await c["uma"].post(f"/api/blueprints/{bp}/trial", json={})
|
||||
assert r.status_code == 200, r.text
|
||||
trial = await wait(r.json()["run_id"])
|
||||
assert trial.status == "succeeded", trial.error
|
||||
assert trial.options[READ_ONLY_OPTION] is True and trial.options[MASK_RESULTS_OPTION] is True
|
||||
app_id = await saved(c["uma"], bp)
|
||||
run = await refreshed(c["uma"], app_id)
|
||||
assert run.options[READ_ONLY_OPTION] is True and run.options[MASK_RESULTS_OPTION] is True
|
||||
async with SessionLocal() as session:
|
||||
events = list((await session.execute(select(RunEvent).where(RunEvent.run_id == run.id))).scalars())
|
||||
dumped = json.dumps([e.data for e in events], ensure_ascii=False)
|
||||
assert "13900000001" not in dumped and "已遮罩" in dumped
|
||||
page = (await c["uma"].get(f"/api/apps/{app_id}/pages/home")).json()
|
||||
metrics = [n for n in walk(page["tree"]) if n["type"] == "metric"]
|
||||
assert metrics and all(m["data"]["state"] == "ok" for m in metrics), metrics
|
||||
assert {m["data"]["evidence"]["run_id"] for m in metrics} == {run.id}
|
||||
async with SessionLocal() as session:
|
||||
await session.execute(delete(DataSource).where(DataSource.name == name))
|
||||
await session.commit()
|
||||
|
||||
@@ -313,7 +313,8 @@ async def test_governed_plans_need_an_admin(login, tmp_path):
|
||||
# 使用者看不到发布单
|
||||
await auth_env.make_user(f"lee{u}", "user")
|
||||
user = await auth_env.login(f"lee{u}")
|
||||
assert (await user.get(f"/api/publish-plans/{plan['id']}")).status_code == 403
|
||||
# 使用者碰共享蓝图的发布单回 404,和看不到的一样(D5)
|
||||
assert (await user.get(f"/api/publish-plans/{plan['id']}")).status_code == 404
|
||||
|
||||
|
||||
async def test_runs_launched_by_a_plan_are_formal_and_from_the_published_version(client, tmp_path):
|
||||
@@ -432,11 +433,11 @@ async def test_problems_are_split_into_three_kinds(client):
|
||||
|
||||
async def _approval_blueprint(client) -> str:
|
||||
"""审批流程模板的蓝图:只有一步主管审批(处理人写了具体的人,岗位按卡片绑上),没有写回。"""
|
||||
from app.blueprints.templates._common import EXTRAS
|
||||
from app.blueprints.templates import get_template
|
||||
|
||||
bp = (await client.post("/api/blueprints", json={"source": {"kind": "template", "id": "approval"}, "scope": "shared",
|
||||
"name": f"报销-{uuid.uuid4().hex[:4]}"})).json()
|
||||
spec = EXTRAS["approval"].example()
|
||||
spec = get_template("approval").example()
|
||||
spec["people"] = spec["people"][:1]
|
||||
spec["access"] = {"use": [{"kind": "user", "name": "zhang"}], "observe": [], "manage": []}
|
||||
await s.edit_spec(bp["id"], **spec)
|
||||
@@ -489,8 +490,14 @@ async def test_cards_changed_after_the_trial_are_what_gets_published(client):
|
||||
|
||||
async def test_a_plan_cannot_take_over_an_app_the_builder_cannot_manage(login, tmp_path):
|
||||
"""蓝图已经发布成应用:只有对那个应用有管理权限的人能出单、执行(发布单会改它的草稿、发布它、整份替换授权)。
|
||||
只能查看的搭建者出单时预检报 plan.app_forbidden(要找管理员),执行 403;应用的授权没被改。"""
|
||||
await auth_env.turn_on_local()
|
||||
|
||||
- 别的搭建者连这张共享蓝图都看不到(D5):读、改、出单、看单子一律 404,列表里没有;给了那个应用的管理授权之后才
|
||||
看得到;
|
||||
- 蓝图的拥有者对应用没有管理权限(应用是管理员替他发布的、只授给他使用)时,把自己写进「谁能看」的管理想借发布单
|
||||
拿到管理权:出单时预检报 plan.app_forbidden(要找管理员),执行 403;应用的授权没被改。"""
|
||||
import json
|
||||
|
||||
root = await auth_env.turn_on_local()
|
||||
u = uuid.uuid4().hex[:6]
|
||||
await auth_env.make_user(f"zhao{u}", "builder")
|
||||
await auth_env.make_user(f"qian{u}", "builder")
|
||||
@@ -502,26 +509,51 @@ async def test_a_plan_cannot_take_over_an_app_the_builder_cannot_manage(login, t
|
||||
"scope": "shared"})).json()
|
||||
await s.edit_spec(bp["id"], purpose=s.item("每周看上周销售额"),
|
||||
access={"use": [{"kind": "user", "name": f"zhao{u}"}], "observe": [], "manage": []})
|
||||
plan = (await owner.post(f"/api/blueprints/{bp['id']}/publish-plan", json={"level": "published",
|
||||
"first_run": False})).json()
|
||||
# 管理员能读写、出单(D5):应用由管理员发布,创建人是管理员
|
||||
plan = (await root.post(f"/api/blueprints/{bp['id']}/publish-plan", json={"level": "published",
|
||||
"first_run": False})).json()
|
||||
assert not [p for p in plan["problems"] if p["code"] == "plan.app_forbidden"]
|
||||
done = (await owner.post(f"/api/publish-plans/{plan['id']}/run")).json()
|
||||
done = (await root.post(f"/api/publish-plans/{plan['id']}/run")).json()
|
||||
assert done["status"] == "done", done["steps"]
|
||||
app_id = (await s.blueprint(bp["id"])).app_id
|
||||
|
||||
# 另一个搭建者把自己写进「谁能看」的管理,想借发布单拿到管理权
|
||||
await s.edit_spec(bp["id"], access={"use": [], "observe": [], "manage": [{"kind": "user", "name": f"qian{u}"}]})
|
||||
plan = (await other.post(f"/api/blueprints/{bp['id']}/publish-plan", json={"level": "published",
|
||||
# 别的搭建者:当这张蓝图不存在
|
||||
assert (await other.get(f"/api/blueprints/{bp['id']}")).status_code == 404
|
||||
assert (await other.patch(f"/api/blueprints/{bp['id']}", json={"name": "改个名"})).status_code == 404
|
||||
assert (await other.post(f"/api/blueprints/{bp['id']}/publish-plan", json={"level": "published"})).status_code \
|
||||
== 404
|
||||
assert (await other.get(f"/api/publish-plans/{plan['id']}")).status_code == 404
|
||||
assert (await other.post(f"/api/publish-plans/{plan['id']}/run")).status_code == 404
|
||||
assert bp["id"] not in [b["id"] for b in (await other.get("/api/blueprints")).json()["items"]]
|
||||
assert bp["id"] in [b["id"] for b in (await owner.get("/api/blueprints")).json()["items"]]
|
||||
up = await other.post("/api/files", files={"file": ("说明.txt", "x".encode(), "text/plain")},
|
||||
data={"context": json.dumps({"blueprint_id": bp["id"]})})
|
||||
assert up.status_code == 404 and up.json()["code"] == "files.context_invalid", "往别人的蓝图上传附件同样不收"
|
||||
ghost = await other.post("/api/files", files={"file": ("说明.txt", "x".encode(), "text/plain")},
|
||||
data={"context": json.dumps({"blueprint_id": uuid.uuid4().hex})})
|
||||
assert (ghost.status_code, ghost.json()) == (up.status_code, up.json()), "看不到的和不存在的一个说法,试不出它在不在"
|
||||
assert bp["id"] in [b["id"] for b in (await root.get("/api/blueprints")).json()["items"]]
|
||||
|
||||
# 拥有者把自己写进「谁能看」的管理,想借发布单拿到管理权
|
||||
await s.edit_spec(bp["id"], access={"use": [], "observe": [], "manage": [{"kind": "user", "name": f"zhao{u}"}]})
|
||||
plan = (await owner.post(f"/api/blueprints/{bp['id']}/publish-plan", json={"level": "published",
|
||||
"first_run": False})).json()
|
||||
forbidden = [p for p in plan["problems"] if p["code"] == "plan.app_forbidden"]
|
||||
assert plan["ok"] is False and forbidden and forbidden[0]["who"] == "admin" and forbidden[0]["plain"]["title"]
|
||||
r = await other.post(f"/api/publish-plans/{plan['id']}/run")
|
||||
r = await owner.post(f"/api/publish-plans/{plan['id']}/run")
|
||||
assert r.status_code == 403 and r.json()["code"] == "plan.app_forbidden", r.text
|
||||
async with SessionLocal() as session:
|
||||
grants = {(g.principal, g.role) for g in (await session.execute(
|
||||
select(AppGrant).where(AppGrant.app_id == app_id))).scalars()}
|
||||
assert grants == {(f"zhao{u}", "use")}, "授权没被改"
|
||||
|
||||
# 应用的管理者看得到它出自的蓝图(D5)
|
||||
async with SessionLocal() as session:
|
||||
session.add(AppGrant(app_id=app_id, principal_kind="user", principal=f"qian{u}", role="manage"))
|
||||
await session.commit()
|
||||
assert (await other.get(f"/api/blueprints/{bp['id']}")).status_code == 200
|
||||
assert bp["id"] in [b["id"] for b in (await other.get("/api/blueprints")).json()["items"]]
|
||||
|
||||
|
||||
async def test_plans_left_running_by_a_dead_process_are_stopped_at_startup(client, tmp_path):
|
||||
"""执行到一半进程没了:单子停在「正在执行」,谁也接不着。启动时改成停下,停在没做完的那一步、写明执行中断,就能接着做。"""
|
||||
|
||||
@@ -670,3 +670,251 @@ async def test_a_correction_only_reaches_rows_the_submitter_can_see(monkeypatch)
|
||||
assert (await fix("amy", amy_row.row_id, "a2")).status == 200
|
||||
assert (await fix("cat", bob_row.row_id, "c1")).status == 200
|
||||
assert "不在这张记录表里" in (await fix("dan", amy_row.row_id, "d1")).error
|
||||
|
||||
|
||||
def correct_graph(table_ref: str) -> dict:
|
||||
"""写记录表的那一步带上要更正的行(supersedes)。"""
|
||||
return chain(node("start", "input", fields=[{"name": "store"}, {"name": "qty"}, {"name": "fix"}]),
|
||||
node("w", "tool", tool=RECORDS_APPEND, approval="never",
|
||||
args={"table": table_ref, "values": {"门店": "{{ input.store }}", "数量": "{{ input.qty }}"},
|
||||
"supersedes": "{{ input.fix }}"}),
|
||||
node("out", "output", fields=[{"name": "id", "value": "{{ nodes.w.receipt._id }}"}]))
|
||||
|
||||
|
||||
async def test_a_correction_keeps_the_submitter_and_records_who_corrected(engine_up):
|
||||
"""别人代为更正(D6):更正行的提交人沿用原行的——原提交人按「只看自己的」照样看得见改过的那一行;动手更正的人记进
|
||||
动作记录的回执和审计(records.correct),不记进行里。重放同一个动作不再写行、也不再记审计。"""
|
||||
from sqlalchemy import func
|
||||
|
||||
from app.capabilities.platform import execute
|
||||
from app.capabilities.registry import get_capability
|
||||
from app.core.artifact_store import load
|
||||
from app.db.models import AuditEvent
|
||||
|
||||
app = await make_app(grants={"amy": "use", "cat": "observe"})
|
||||
table = await make_table(app)
|
||||
source = await source_of(table)
|
||||
original = add(table, {"门店": "一店", "数量": 3}, by="amy")
|
||||
workflow_id = await writer_of(app, correct_graph(source.name))
|
||||
await _sync()
|
||||
|
||||
run = await run_manager.start(graph=correct_graph(source.name),
|
||||
input_payload={"store": "一店", "qty": "9", "fix": original.row_id},
|
||||
run_class="formal", started_by="cat", workflow_id=workflow_id)
|
||||
done = await wait(run.id, ("succeeded", "failed", "interrupted"))
|
||||
assert done.status == "succeeded", done.error
|
||||
async with SessionLocal() as session:
|
||||
[rec] = list((await session.execute(select(ActionRecord).where(ActionRecord.run_id == run.id))).scalars())
|
||||
audits = list((await session.execute(select(AuditEvent).where(AuditEvent.action == "records.correct",
|
||||
AuditEvent.subject_id == table.id))).scalars())
|
||||
body = load(rec.receipt["artifact"])["body"]
|
||||
assert (body["_submitted_by"], body["corrected_by"]) == ("amy", "cat"), "回执里记着谁交的、谁改的"
|
||||
assert [(a.actor, a.data["submitted_by"], a.data["supersedes"], a.data["row_id"], a.data["run_id"])
|
||||
for a in audits] == [("cat", "amy", original.row_id, body["_id"], run.id)]
|
||||
|
||||
mine, _ = store.rows(path_of(table), columns_of(table), scope="mine", viewer="amy")
|
||||
assert [(r["_id"], r["_submitted_by"], r["values"]["数量"]) for r in mine] == [(body["_id"], "amy", 9)], \
|
||||
"原提交人看得见改过的那一行"
|
||||
assert store.rows(path_of(table), columns_of(table), scope="mine", viewer="cat")[0] == [], "更正的人不算提交人"
|
||||
history, _ = store.rows(path_of(table), columns_of(table), scope="all", viewer=None, history=True)
|
||||
assert {r["_id"]: r["_run_id"] for r in history}[body["_id"]] == run.id, "这一行出自哪次运行照样查得到"
|
||||
|
||||
async with SessionLocal() as session:
|
||||
cap = await get_capability(session, RECORDS_APPEND)
|
||||
again = await execute(None, cap, {"table": source.name, "values": {"门店": "一店", "数量": 9},
|
||||
"supersedes": original.row_id},
|
||||
environment="prod", idem_key=rec.idem_key, on_behalf={"sub": "cat"}, run_id=run.id)
|
||||
assert again.status == 200 and again.body["duplicate"] is True and again.body["_submitted_by"] == "amy"
|
||||
async with SessionLocal() as session:
|
||||
assert int(await session.scalar(select(func.count(AuditEvent.id)).where(
|
||||
AuditEvent.action == "records.correct", AuditEvent.subject_id == table.id)) or 0) == 1
|
||||
|
||||
# 自己交的新一行:提交人就是自己,回执里没有 corrected_by
|
||||
fresh = store.append(path_of(table), columns_of(table), {"门店": "二店", "数量": 1}, version=table.version,
|
||||
run_id=None, actor="amy", action_key=uuid.uuid4().hex)
|
||||
assert fresh.submitted_by == "amy" and "corrected_by" not in fresh.receipt()
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 跨版本汇总(停用的版本只读挂在连接上,UNION ALL 接上);停用之前发起的运行照常写完
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
#: 不兼容的改动:数量从整数放宽成数(都是数,旧记录照样并进来)、原因从文字改成是或否(意思变了,旧记录这一列为空)、
|
||||
#: 加必填的店长(旧记录没有,为空)
|
||||
CHANGED = [COLUMNS[0], {**COLUMNS[1], "type": "number"}, {**COLUMNS[2], "type": "boolean"},
|
||||
{"name": "店长", "label": "店长", "type": "string", "required": True}]
|
||||
SUMMARY = 'SELECT count(*) AS n, sum("数量") AS s FROM records'
|
||||
|
||||
|
||||
async def _publish_changed(app: App, columns=CHANGED) -> tuple[RecordsTable, RecordsTable]:
|
||||
"""草稿出第 2 版(draft_table),发布单切换(publish_table):第 1 版这时才停用。交回(第 1 版,第 2 版)。"""
|
||||
from app.records.tables import draft_table, publish_table
|
||||
|
||||
async with SessionLocal() as session:
|
||||
owner = await session.get(App, app.id)
|
||||
drafted = await draft_table(session, owner, columns)
|
||||
await publish_table(session, owner, f"{drafted.table_key}.v{drafted.version}", columns)
|
||||
await session.commit()
|
||||
key = drafted.table_key
|
||||
async with SessionLocal() as session:
|
||||
v1, v2 = list((await session.execute(select(RecordsTable).where(RecordsTable.table_key == key)
|
||||
.order_by(RecordsTable.version))).scalars())
|
||||
return v1, v2
|
||||
|
||||
|
||||
async def test_the_summary_reads_every_version_after_an_incompatible_change(engine_up):
|
||||
"""表单不兼容地改过并发布之后,看板查新一版的数据源,旧版本的记录照样在汇总里:列照新一版的来,旧版本没有的、类型改得
|
||||
对不上的为空,整数放宽成数的照样并进来;各版本各按自己的高水位固定,同一次运行审批前后查到的数一样;挂上去的旧版本和
|
||||
main 一样不能直接写库名查;用户 SQL 写 ATTACH 照旧被拒。"""
|
||||
from app.apps.resolve import _version_text
|
||||
from app.records.views import cache_token
|
||||
|
||||
app = await make_app()
|
||||
v1 = await make_table(app)
|
||||
first = add(v1, {"门店": "一店", "数量": 2, "原因": "破损"})
|
||||
add(v1, {"门店": "一店", "数量": 5, "原因": "破损"}, supersedes=first.row_id)
|
||||
add(v1, {"门店": "二店", "数量": 1}, by="bob")
|
||||
v1, v2 = await _publish_changed(app)
|
||||
assert v1.retired_at is not None and v2.retired_at is None and v2.version == 2
|
||||
add(v2, {"门店": "三店", "数量": 7.5, "原因": True, "店长": "钱七"})
|
||||
source = await source_of(v2)
|
||||
|
||||
async with SessionLocal() as session:
|
||||
now = await resolve(session, source)
|
||||
got = await run_query(now, 'SELECT _schema_version AS v, "门店", "数量", "原因", "店长" FROM records '
|
||||
'ORDER BY v, "门店"')
|
||||
assert got.rows == [[1, "一店", 5, None, None], [1, "二店", 1, None, None], [2, "三店", 7.5, 1, "钱七"]], \
|
||||
"旧版本的记录并进来(更正过的只留最新的一行),对不上的列为空"
|
||||
assert (await run_query(now, SUMMARY)).rows == [[3, 13.5]]
|
||||
for sql in ("SELECT count(*) FROM records_v1.records", 'SELECT count(*) FROM "records_v1"."records"',
|
||||
"SELECT count(*) FROM [RECORDS_V1].records", "SELECT count(*) FROM main.records"):
|
||||
with pytest.raises(SqlRejected):
|
||||
await run_query(now, sql)
|
||||
for sql in (f"ATTACH DATABASE '{path_of(v1)}' AS sneak", "DETACH DATABASE records_v1"):
|
||||
with pytest.raises(SqlRejected):
|
||||
await run_query(now, sql)
|
||||
# 同一个数据源不经解析查(数据剖析这类入口):连接上挂过的旧版本摘掉了,库名写了也查不到
|
||||
with pytest.raises(Exception) as gone:
|
||||
await run_query(source, "SELECT count(*) FROM records_v1.records")
|
||||
assert "records_v1" in str(gone.value)
|
||||
assert [r[1] for r in (await run_query(source, "PRAGMA database_list")).rows] == ["main", "temp"]
|
||||
assert (await run_query(now, "PRAGMA database_list")).rows[-1][1] == "records_v1", "再按汇总查又挂上"
|
||||
# 行级过滤对每一版都生效
|
||||
async with SessionLocal() as session:
|
||||
bobs = await resolve(session, source, {"seq": 1, "rows": "mine", "viewer": "bob",
|
||||
"older": [{"version": 1, "seq": 3}]})
|
||||
assert (await run_query(bobs, SUMMARY)).rows == [[1, 1]]
|
||||
|
||||
# 高水位:旧版本同样按发起时固定(停用之前发起的运行还能往旧版本写完)
|
||||
tool = f"db_query__{source.name}"
|
||||
graph = chain(node("start", "input"), node("t1", "tool", tool=tool, args={"sql": SUMMARY}),
|
||||
node("gate", "human", mode="approve", title="看一眼"),
|
||||
node("t2", "tool", tool=tool, args={"sql": SUMMARY}),
|
||||
node("out", "output", fields=[{"name": "t1", "value": "{{ nodes.t1 }}"},
|
||||
{"name": "t2", "value": "{{ nodes.t2 }}"}]))
|
||||
run = await run_manager.start(graph=graph, input_payload={}, run_class="formal")
|
||||
assert (await wait(run.id)).status == "interrupted"
|
||||
add(v1, {"门店": "四店", "数量": 4})
|
||||
add(v2, {"门店": "五店", "数量": 6, "店长": "孙八"})
|
||||
await run_manager.resume(run.id, {"approved": True})
|
||||
done = await wait(run.id, ("succeeded", "failed"))
|
||||
assert done.status == "succeeded", done.error
|
||||
assert json.loads(done.output["t1"])["rows"] == [[3, 13.5]]
|
||||
assert json.loads(done.output["t2"])["rows"] == [[3, 13.5]], "审批期间两个版本新来的都没混进这次运行"
|
||||
entry = done.data_versions[source.id]
|
||||
assert entry["seq"] == 1 and entry["older"] == [{"version": 1, "seq": 3}]
|
||||
assert _version_text(entry) == f"{source.name} 截至第 1 条(连同第 1 版截至第 3 条)"
|
||||
assert cache_token(entry) != cache_token({**entry, "older": [{"version": 1, "seq": 4}]}), \
|
||||
"旧版本多了一行,按需视图的缓存就不再算数"
|
||||
again = await run_manager.start(graph=chain(node("start", "input"),
|
||||
node("t", "tool", tool=tool, args={"sql": SUMMARY}),
|
||||
node("out", "output", fields=[{"name": "t", "value": "{{ nodes.t }}"}])),
|
||||
input_payload={}, run_class="formal")
|
||||
assert json.loads((await wait(again.id, ("succeeded", "failed"))).output["t"])["rows"] == [[5, 23.5]]
|
||||
|
||||
|
||||
async def test_a_lost_old_version_is_named_not_silently_dropped(engine_up):
|
||||
"""旧版本的数据文件不在了(管理员手工删了):汇总照常查得出来、只是少了那一版,但不是悄悄少——发起时记进 data_versions 的
|
||||
older(missing),印章写明缺了哪一版、数不全;查询快照里同样记着;不在运行里的查询也一样。"""
|
||||
from app.apps.resolve import _records_gap, _version_text
|
||||
from app.core.artifact_store import load
|
||||
from app.db.models import Artifact
|
||||
from app.records.views import pin_entry
|
||||
|
||||
app = await make_app()
|
||||
v1 = await make_table(app)
|
||||
add(v1, {"门店": "一店", "数量": 2})
|
||||
v1, v2 = await _publish_changed(app)
|
||||
add(v2, {"门店": "三店", "数量": 7.5, "店长": "钱七"})
|
||||
source = await source_of(v2)
|
||||
path_of(v1).unlink()
|
||||
|
||||
async with SessionLocal() as session:
|
||||
entry = await pin_entry(session, source)
|
||||
now = await resolve(session, source)
|
||||
assert entry["seq"] == 1 and entry["older"] == [{"version": 1, "seq": None, "missing": True}]
|
||||
assert now.older == ({"version": 1, "seq": None, "missing": True},) and not now.attach
|
||||
assert (await run_query(now, SUMMARY)).rows == [[1, 7.5]], "查得出来,只是没有第 1 版"
|
||||
assert _version_text({**entry, "name": source.name}) == f"{source.name} 截至第 1 条"
|
||||
assert _records_gap({**entry, "name": source.name}) == \
|
||||
f"记录表「{source.name}」第 1 版的数据文件不在了,汇总里没有这一版的记录,数不全"
|
||||
|
||||
tool = f"db_query__{source.name}"
|
||||
run = await run_manager.start(graph=chain(node("start", "input"), node("t", "tool", tool=tool, args={"sql": SUMMARY}),
|
||||
node("out", "output", fields=[{"name": "t", "value": "{{ nodes.t }}"}])),
|
||||
input_payload={}, run_class="formal")
|
||||
done = await wait(run.id, ("succeeded", "failed"))
|
||||
assert done.status == "succeeded", done.error
|
||||
assert json.loads(done.output["t"])["rows"] == [[1, 7.5]]
|
||||
pinned = done.data_versions[source.id]
|
||||
assert pinned["older"] == [{"version": 1, "seq": None, "missing": True}] and _records_gap(pinned)
|
||||
async with SessionLocal() as session:
|
||||
snaps = list((await session.execute(select(Artifact).where(Artifact.run_id == run.id,
|
||||
Artifact.kind == "query_snapshot"))).scalars())
|
||||
assert snaps and load(snaps[0].id)["records_view"]["older"] == [{"version": 1, "seq": None, "missing": True}]
|
||||
|
||||
|
||||
def test_union_keeps_numbers_widened_and_drops_meanings_that_changed():
|
||||
from app.records.store import Column, union_compatible
|
||||
|
||||
assert union_compatible(Column("数量", type="number"), Column("数量", type="integer"))
|
||||
assert union_compatible(Column("门店"), Column("门店"))
|
||||
assert not union_compatible(Column("原因", type="boolean"), Column("原因"))
|
||||
assert not union_compatible(Column("数量"), Column("数量", type="number")), "数改成文字:旧记录这一列为空"
|
||||
|
||||
|
||||
async def test_runs_launched_before_a_version_is_retired_still_write_to_it(engine_up):
|
||||
"""停用之前发起、审批停在半路的实例,批完照常写进它发起时的那一版(同「进行中的实例照常走完」);停用之后发起的
|
||||
运行不能往停用的版本写,一行都不落。"""
|
||||
app = await make_app()
|
||||
v1 = await make_table(app)
|
||||
source = await source_of(v1)
|
||||
graph = chain(node("start", "input", fields=[{"name": "store"}, {"name": "qty"}]),
|
||||
node("gate", "human", mode="approve", title="批一下"),
|
||||
node("w", "tool", tool=RECORDS_APPEND, approval="never",
|
||||
args={"table": source.name, "values": {"门店": "{{ input.store }}", "数量": "{{ input.qty }}"}}),
|
||||
node("out", "output", fields=[{"name": "id", "value": "{{ nodes.w.receipt._id }}"}]))
|
||||
workflow_id = await writer_of(app, graph)
|
||||
await _sync()
|
||||
before = await run_manager.start(graph=graph, input_payload={"store": "一店", "qty": "3"}, run_class="formal",
|
||||
started_by="amy", workflow_id=workflow_id)
|
||||
assert (await wait(before.id)).status == "interrupted"
|
||||
v1, v2 = await _publish_changed(app)
|
||||
after = await run_manager.start(graph=graph, input_payload={"store": "二店", "qty": "4"}, run_class="formal",
|
||||
started_by="amy", workflow_id=workflow_id)
|
||||
assert (await wait(after.id)).status == "interrupted"
|
||||
for run in (before, after):
|
||||
await run_manager.resume(run.id, {"approved": True})
|
||||
|
||||
done = await wait(before.id, ("succeeded", "failed"))
|
||||
assert done.status == "succeeded", done.error
|
||||
[row], _ = store.rows(path_of(v1), columns_of(v1), scope="all", viewer=None)
|
||||
assert row["_run_id"] == before.id and row["_schema_version"] == 1 and row["values"]["门店"] == "一店"
|
||||
assert done.output["id"] == row["_id"]
|
||||
|
||||
late = await wait(after.id, ("succeeded", "failed", "interrupted"))
|
||||
assert late.status != "succeeded"
|
||||
async with SessionLocal() as session:
|
||||
[rec] = list((await session.execute(select(ActionRecord).where(ActionRecord.run_id == after.id))).scalars())
|
||||
assert rec.state == "failed" and "停用" in (rec.error or ""), (rec.state, rec.error)
|
||||
assert store.high_water(path_of(v1)) == 1 and store.high_water(path_of(v2)) == 0, "停用之后发起的一行都没写"
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
"""开登录后使用者的问数据(D-c):能发起什么样的运行、查询结果在工具边界遮、运行的 WebSocket 按运行判、数据源名单。
|
||||
|
||||
- 图:只许输入、模型、Agent、报告、成果、知识检索、数据整形、条件分支、口径卡、合并查询;Agent 只许数据源工具和只读的
|
||||
内置工具(app/auth/chat_policy.py)。调用工具、沙箱代码、自定义工具、MCP、网页、HTTP、文件一律 403;
|
||||
- 图:只许输入、模型、Agent、报告、成果、知识检索、数据整形、条件分支、口径卡、合并查询、调用工具;Agent 和调用工具
|
||||
节点只许数据源工具和只读的内置工具(app/auth/chat_policy.py)。沙箱代码、自定义工具、MCP、网页、HTTP、文件、平台的
|
||||
写入能力一律 403;
|
||||
- 调用工具节点查数据源:和 Agent 同一条路,结果在工具边界遮、可写源上的写语句被守卫拒掉;能力治理里改成写入的工具
|
||||
在只读运行里不执行;
|
||||
- 使用者发起的问数据运行一定带 MASK_RESULTS_OPTION:真的执行一次(演示模型调数据源查询),事件、快照、实时流里都没有
|
||||
遮罩列的原值;
|
||||
- WebSocket:没登录的握手前关掉;使用者只能连自己能读、查询结果在工具边界遮过的运行(握手时判,1008 拒);
|
||||
@@ -54,6 +57,10 @@ def chat_graph(*tools: str, middle: dict | None = None) -> dict:
|
||||
"memory_recall"),
|
||||
chain(node("start", "input"), node("kb", "retrieve", query="x"), node("t", "transform", expression="1"),
|
||||
node("out", "output")),
|
||||
# 调用工具节点:绑定的工具过同一份白名单就行(D1)
|
||||
chat_graph(middle=node("ask", "tool", tool="db_query__crm", args={"sql": "SELECT city FROM customers"})),
|
||||
chat_graph(middle=node("ask", "tool", tool="calculator", args={"expression": "1 + 1"})),
|
||||
chat_graph(middle=node("ask", "tool", tool="kb_search", args={"query": "退款规则"})),
|
||||
{"nodes": "坏的"}, # 不成形的图交给 launch_run 照常报校验问题
|
||||
])
|
||||
def test_graphs_a_user_may_run_in_chat(graph):
|
||||
@@ -62,7 +69,11 @@ def test_graphs_a_user_may_run_in_chat(graph):
|
||||
|
||||
@pytest.mark.parametrize("graph, said", [
|
||||
(chat_graph(middle=node("ask", "code", code="print(1)")), "沙箱代码"),
|
||||
(chat_graph(middle=node("ask", "tool", tool="calculator")), "调用工具"),
|
||||
(chat_graph(middle=node("ask", "tool", tool="http_request")), "http_request"),
|
||||
(chat_graph(middle=node("ask", "tool", tool="records_append")), "records_append"),
|
||||
(chat_graph(middle=node("ask", "tool", tool="memory_write")), "memory_write"),
|
||||
(chat_graph(middle=node("ask", "tool", tool="mcp:fs/read_file")), "mcp:fs/read_file"),
|
||||
(chat_graph(middle=node("ask", "tool", tool="{{ vars.tool }}")), "模板"),
|
||||
(chat_graph(middle=node("ask", "supervisor", agents=[])), "多 Agent 协作"),
|
||||
(chat_graph(middle=node("ask", "subgraph", workflow_id="w")), "子工作流"),
|
||||
(chat_graph(middle=node("ask", "human", mode="approve")), "人工审批"),
|
||||
@@ -85,7 +96,11 @@ def test_graphs_a_user_may_not_run_in_chat(graph, said):
|
||||
chat_graph(middle=node("ask", "agent", prompt="x", tools=["memory_recall"], memory_scope="builders")),
|
||||
chat_graph(middle=node("kb", "retrieve", query="x", collection="hr")),
|
||||
{**chat_graph("kb_search"), "defaults": {"collection": "hr"}},
|
||||
# 调用工具节点的参数直接交给工具:在参数里挑知识库同样不许
|
||||
chat_graph(middle=node("kb", "tool", tool="kb_search", args={"query": "x", "collection": "hr"})),
|
||||
{**chat_graph("memory_recall"), "defaults": {"memory_scope": " "}},
|
||||
# 节点上没写参数、运行时取图级 defaults 里的 args(和运行时同一个取值规则,engine/launch.node_setting)
|
||||
{**chat_graph(middle=node("kb", "tool", tool="kb_search")), "defaults": {"args": {"query": "x", "collection": "hr"}}},
|
||||
])
|
||||
def test_users_may_not_pick_memory_or_knowledge_scope(graph):
|
||||
"""记忆、知识库的内容不经遮罩:节点上、图级 defaults 里都不许指定范围,用管理员设好的默认值。"""
|
||||
@@ -268,6 +283,181 @@ async def test_a_users_chat_run_cannot_write_a_writable_source(world, monkeypatc
|
||||
assert sqlite3.connect(path).execute("SELECT COUNT(*) FROM ledger").fetchone()[0] == 0, "搭建者照旧能写"
|
||||
|
||||
|
||||
async def _writable_masked_source(tmp_path) -> tuple[str, object]:
|
||||
"""可写的客户库(手机号是遮罩列):调用工具节点查它、想删它。"""
|
||||
path = tmp_path / f"crm_rw_{uuid.uuid4().hex[:6]}.db"
|
||||
db = sqlite3.connect(path)
|
||||
db.executescript("CREATE TABLE customers (id INTEGER PRIMARY KEY, name TEXT, phone TEXT, city TEXT);"
|
||||
f"INSERT INTO customers VALUES (1, '张三', '{PHONE}', '杭州'), (2, '李四', '13900000002', '宁波');")
|
||||
db.commit()
|
||||
db.close()
|
||||
name = f"crm_rw_{uuid.uuid4().hex[:6]}"
|
||||
async with SessionLocal() as session:
|
||||
row = DataSource(name=name, kind="sqlite", database=str(path), readonly=False, enabled=True,
|
||||
description="客户(可写)", options={"mask_columns": "phone"})
|
||||
session.add(row)
|
||||
await session.flush()
|
||||
row.schema_cache = await introspect.introspect(row)
|
||||
await session.commit()
|
||||
return name, path
|
||||
|
||||
|
||||
def _tool_chat(tool: str, sql: str) -> dict:
|
||||
"""问数据当场搭的图:取数用调用工具节点(D1),不经 Agent、不调模型。approval=never 是发起人自己写的:只读不交给
|
||||
审批,写成 never 也照样拒写。"""
|
||||
return chain(node("start", "input", fields=[{"name": "question"}]),
|
||||
node("q", "tool", tool=tool, args={"sql": sql}, fail_fast=False, approval="never"),
|
||||
node("out", "output", fields=[{"name": "结果", "value": "{{ nodes.q }}"}]))
|
||||
|
||||
|
||||
async def test_a_users_tool_node_is_masked_and_read_only(world, tmp_path):
|
||||
"""调用工具节点这条路和 Agent 一样(D1):使用者问数据的运行里,结果在工具边界就遮(事件、查询快照、运行产出里都没有
|
||||
原值),可写源上的写语句被守卫拒掉、一行没少(节点上写了免审批也一样);搭建者发起的同一张写图照旧能写。"""
|
||||
from app.core.artifact_store import load
|
||||
from app.tools.datasource import FORCED_READONLY, READ_ONLY_OPTION
|
||||
|
||||
await _allow_chat(world)
|
||||
name, path = await _writable_masked_source(tmp_path)
|
||||
tool = f"db_query__{name}"
|
||||
|
||||
r = await world["uma"].post("/api/runs", json={"origin": "chat", "input": {"question": "客户"},
|
||||
"graph": _tool_chat(tool, "SELECT name, phone, city FROM customers")})
|
||||
assert r.status_code == 201, r.text
|
||||
run = await _finish(r.json()["id"])
|
||||
assert run.status == "succeeded", run.error
|
||||
assert run.options[MASK_RESULTS_OPTION] is True and run.options[READ_ONLY_OPTION] is True
|
||||
async with SessionLocal() as s:
|
||||
events = list((await s.execute(select(RunEvent).where(RunEvent.run_id == run.id))).scalars())
|
||||
snaps = list((await s.execute(select(Artifact).where(Artifact.run_id == run.id,
|
||||
Artifact.kind == "query_snapshot"))).scalars())
|
||||
dumped = json.dumps([e.data for e in events], ensure_ascii=False) + json.dumps(run.output, ensure_ascii=False)
|
||||
assert "tool.end" in {e.type for e in events}
|
||||
assert PHONE not in dumped and MASKED in dumped and "张三" in dumped, "遮罩列换掉,别的列照旧"
|
||||
assert snaps and all(PHONE not in json.dumps(load(snap.id), ensure_ascii=False) for snap in snaps)
|
||||
|
||||
delete_all = _tool_chat(tool, "DELETE FROM customers")
|
||||
r = await world["uma"].post("/api/runs", json={"origin": "chat", "input": {"question": "清空"}, "graph": delete_all})
|
||||
assert r.status_code == 201, r.text
|
||||
run = await _finish(r.json()["id"])
|
||||
async with SessionLocal() as s:
|
||||
events = list((await s.execute(select(RunEvent).where(RunEvent.run_id == run.id))).scalars())
|
||||
assert FORCED_READONLY[:12] in json.dumps([e.data for e in events], ensure_ascii=False), "写语句被守卫拒掉"
|
||||
assert sqlite3.connect(path).execute("SELECT COUNT(*) FROM customers").fetchone()[0] == 2
|
||||
|
||||
r = await world["bob"].post("/api/runs", json={"origin": "chat", "input": {"question": "清空"}, "graph": delete_all})
|
||||
assert r.status_code == 201, r.text
|
||||
await _finish(r.json()["id"])
|
||||
assert sqlite3.connect(path).execute("SELECT COUNT(*) FROM customers").fetchone()[0] == 0, "搭建者照旧能写"
|
||||
|
||||
|
||||
async def test_a_tool_governed_as_write_is_not_run_in_a_read_only_run(world, tmp_path):
|
||||
"""能力治理把数据源查询改成「写入」:使用者问数据里的调用工具节点不进动作流程(探索运行在那里会演练、交回假回执),
|
||||
直接拒、说清是只读运行;表里的行一条没动。"""
|
||||
from app.db.models import ActionRecord, Capability
|
||||
|
||||
await _allow_chat(world)
|
||||
name, path = await _writable_masked_source(tmp_path)
|
||||
tool = f"db_query__{name}"
|
||||
async with SessionLocal() as s:
|
||||
s.add(Capability(name=tool, system=name, title="客户库", effect_system="dynamic", effect="write"))
|
||||
await s.commit()
|
||||
try:
|
||||
r = await world["uma"].post("/api/runs", json={"origin": "chat", "input": {"question": "清空"},
|
||||
"graph": _tool_chat(tool, "DELETE FROM customers")})
|
||||
assert r.status_code == 201, r.text
|
||||
run = await _finish(r.json()["id"])
|
||||
assert run.status == "failed" and "只能读数据" in (run.error or "") and tool in run.error, run.error
|
||||
async with SessionLocal() as s:
|
||||
assert not list((await s.execute(select(ActionRecord).where(ActionRecord.run_id == run.id))).scalars())
|
||||
assert sqlite3.connect(path).execute("SELECT COUNT(*) FROM customers").fetchone()[0] == 2
|
||||
finally:
|
||||
async with SessionLocal() as s:
|
||||
await s.execute(delete(Capability).where(Capability.name == tool))
|
||||
await s.commit()
|
||||
|
||||
|
||||
async def test_kb_search_keeps_to_the_runs_knowledge_base_when_the_scope_is_fixed(monkeypatch):
|
||||
"""工具层的那一道(D1):范围固定的运行里 kb_search 不认 collection 参数——直接写的、参数名写错被纠成 collection 的都拒,
|
||||
不悄悄改查默认库;没写的照常查运行定下的那一个。范围不固定(搭建者的运行)照旧按参数查。"""
|
||||
from app.memory import kb
|
||||
from app.tools.registry import ToolArgsError, ToolContext, Versions, call_tool
|
||||
|
||||
searched: list[str] = []
|
||||
|
||||
async def _search(session, *, collection, query, limit, **_):
|
||||
searched.append(collection)
|
||||
return []
|
||||
|
||||
monkeypatch.setattr(kb, "search", _search)
|
||||
fixed = ToolContext(collection="default", fixed_scope=True, data_versions=Versions.CURRENT)
|
||||
for args in ({"query": "x", "collection": "hr"}, {"query": "x", "limit": 5, "coll": "hr"}):
|
||||
with pytest.raises(ToolArgsError) as refused:
|
||||
await call_tool("kb_search", args, fixed)
|
||||
assert "只能检索管理员设好的知识库" in str(refused.value) and "hr" in str(refused.value)
|
||||
assert searched == []
|
||||
await call_tool("kb_search", {"query": "x"}, fixed)
|
||||
await call_tool("kb_search", {"query": "x", "collection": "default"}, fixed)
|
||||
await call_tool("kb_search", {"query": "x", "collection": "hr"},
|
||||
ToolContext(collection="default", data_versions=Versions.CURRENT))
|
||||
assert searched == ["default", "default", "hr"]
|
||||
|
||||
|
||||
def _kb_search_spy(monkeypatch) -> list[str]:
|
||||
from app.memory import kb
|
||||
|
||||
searched: list[str] = []
|
||||
|
||||
async def _search(session, *, collection, query, limit, **_):
|
||||
searched.append(collection)
|
||||
return [{"content": f"{collection} 库里的一段", "score": 1.0}]
|
||||
|
||||
monkeypatch.setattr(kb, "search", _search)
|
||||
return searched
|
||||
|
||||
|
||||
async def test_a_users_run_cannot_reach_another_knowledge_base_through_the_tool_layer(world, monkeypatch):
|
||||
"""白名单认不出的两种写法,运行时在工具层拒(D1):调用工具节点的参数名写错(coll 被纠成 collection)、Agent 在参数里
|
||||
挑知识库。使用者的问数据只查得到运行定下的知识库;搭建者发起的同一张图照旧按参数查。"""
|
||||
from app.providers import mock_model
|
||||
|
||||
await _allow_chat(world)
|
||||
searched = _kb_search_spy(monkeypatch)
|
||||
typo = chain(node("start", "input", fields=[{"name": "question"}]),
|
||||
node("kb", "tool", tool="kb_search", args={"query": "x", "limit": 5, "coll": "hr"}),
|
||||
node("out", "output", fields=[{"name": "结果", "value": "{{ nodes.kb }}"}]))
|
||||
assert chat_graph_problem(typo) is None, "白名单只看字面写的参数名,这一道在工具层"
|
||||
r = await world["uma"].post("/api/runs", json={"origin": "chat", "graph": typo, "input": {"question": "x"}})
|
||||
assert r.status_code == 201, r.text
|
||||
run = await _finish(r.json()["id"])
|
||||
assert run.status == "failed" and "只能检索管理员设好的知识库" in (run.error or ""), run.error
|
||||
assert "hr" not in searched
|
||||
|
||||
replies: list[str] = []
|
||||
|
||||
def _decide(self, messages):
|
||||
if seen := [m for m in messages if isinstance(m, ToolMessage)]:
|
||||
replies.append(str(seen[-1].content))
|
||||
return AIMessage(content="好了。")
|
||||
return AIMessage(content="", tool_calls=[{"name": "kb_search", "args": {"query": "薪酬", "collection": "hr"},
|
||||
"id": "call_1"}])
|
||||
|
||||
monkeypatch.setattr(mock_model.MockChatModel, "_decide", _decide)
|
||||
r = await world["uma"].post("/api/runs", json={"origin": "chat", "graph": chat_graph("kb_search"),
|
||||
"input": {"question": "薪酬"}})
|
||||
assert r.status_code == 201, r.text
|
||||
run = await _finish(r.json()["id"])
|
||||
assert run.status == "succeeded", run.error
|
||||
assert replies and "只能检索管理员设好的知识库" in replies[-1], replies
|
||||
assert "hr" not in searched
|
||||
|
||||
replies.clear()
|
||||
r = await world["bob"].post("/api/runs", json={"origin": "chat", "graph": chat_graph("kb_search"),
|
||||
"input": {"question": "薪酬"}})
|
||||
assert r.status_code == 201, r.text
|
||||
assert (await _finish(r.json()["id"])).status == "succeeded"
|
||||
assert searched == ["hr"] and "hr 库里的一段" in replies[-1], "搭建者的运行照旧按参数查"
|
||||
|
||||
|
||||
async def test_a_users_chat_run_keeps_to_its_own_memory_scope(world):
|
||||
"""请求体里带记忆范围、知识库的拒;不带的,记忆是这位使用者自己的范围,知识库是设置里的默认值。"""
|
||||
await _allow_chat(world)
|
||||
@@ -287,10 +477,12 @@ async def test_a_users_chat_run_keeps_to_its_own_memory_scope(world):
|
||||
|
||||
|
||||
class _Scripted:
|
||||
"""第一轮按节点手册的标准写法搭(tool 节点查库),收到自查请求时改成 Agent 查库。记下 system 和每一轮的请求。"""
|
||||
"""第一轮按节点手册的标准写法搭(tool 节点调 fetch_tool,缺省是查库),收到自查请求时改成 Agent 查库。记下 system 和
|
||||
每一轮的请求。"""
|
||||
|
||||
def __init__(self, source: str) -> None:
|
||||
def __init__(self, source: str, fetch_tool: str | None = None) -> None:
|
||||
self.source = source
|
||||
self.fetch_tool = fetch_tool or f"db_query__{source}"
|
||||
self.systems: list[str] = []
|
||||
self.humans: list[str] = []
|
||||
|
||||
@@ -308,7 +500,7 @@ class _Scripted:
|
||||
{"op": "add_node", "node": {"id": "start", "type": "input", "label": "输入",
|
||||
"config": {"fields": [{"name": "question"}]}}},
|
||||
{"op": "add_node", "node": {"id": "fetch", "type": "tool", "label": "查库",
|
||||
"config": {"tool": f"db_query__{self.source}",
|
||||
"config": {"tool": self.fetch_tool,
|
||||
"args": {"sql": "SELECT city FROM customers"}}}},
|
||||
{"op": "add_node", "node": {"id": "out", "type": "output", "label": "成果",
|
||||
"config": {"fields": [{"name": "回答", "value": "{{ last_message }}"}]}}},
|
||||
@@ -344,13 +536,21 @@ async def _generate(client, monkeypatch, model: _Scripted) -> list[dict]:
|
||||
|
||||
|
||||
async def test_generation_for_a_user_keeps_to_the_chat_policy(world, monkeypatch):
|
||||
"""使用者问数据时,提示词先说清只能 Agent 查库;照标准写法搭出的 tool 节点图按 chat_policy 交回去改,改好才交付、
|
||||
自动运行。搭建者照旧按节点手册来,tool 节点不算问题。"""
|
||||
"""使用者问数据时,提示词先说清只能用数据源查询和只读的内置工具;tool 节点查库照标准写法搭,一次就过、自动运行(D1)。
|
||||
tool 节点绑了不许的工具(http_request)按 chat_policy 交回去改,改好才交付。搭建者照旧按节点手册来。"""
|
||||
await _allow_chat(world)
|
||||
model = _Scripted(world["source"])
|
||||
events = await _generate(world["uma"], monkeypatch, model)
|
||||
assert "这位用户是「使用者」" in model.systems[0] and "- http_request" not in model.systems[0]
|
||||
assert len(model.humans) == 2 and "调用工具" in model.humans[1], "tool 节点按 chat_policy 交回去改"
|
||||
assert "tool 节点和 agent 都只能用" in model.systems[0]
|
||||
assert len(model.humans) == 1, "tool 节点查库不算问题"
|
||||
final = next(e for e in events if e.get("op") == "final")
|
||||
assert final["autorun"] is True and "tool" in {n["type"] for n in final["graph"]["nodes"]}
|
||||
assert chat_graph_problem(final["graph"]) is None
|
||||
|
||||
model = _Scripted(world["source"], fetch_tool="http_request")
|
||||
events = await _generate(world["uma"], monkeypatch, model)
|
||||
assert len(model.humans) == 2 and "http_request" in model.humans[1], "绑了不许的工具按 chat_policy 交回去改"
|
||||
check = [e for e in events if e.get("op") == "check"][-1]
|
||||
final = next(e for e in events if e.get("op") == "final")
|
||||
assert check["status"] == "passed" and final["autorun"] is True
|
||||
@@ -367,9 +567,9 @@ async def test_generation_for_a_user_keeps_to_the_chat_policy(world, monkeypatch
|
||||
async def test_generation_for_a_user_reports_what_it_could_not_fix(world, monkeypatch):
|
||||
"""改不好的照实交付:自查失败、final 里列出 chat.not_allowed,不自动运行。"""
|
||||
await _allow_chat(world)
|
||||
model = _Scripted(world["source"])
|
||||
model._lines = lambda repair: _Scripted(world["source"])._lines(False) if not repair else [
|
||||
json.dumps({"op": "done", "explanation": "没改"}, ensure_ascii=False)]
|
||||
model = _Scripted(world["source"], fetch_tool="http_request")
|
||||
model._lines = lambda repair: _Scripted(world["source"], fetch_tool="http_request")._lines(False) if not repair \
|
||||
else [json.dumps({"op": "done", "explanation": "没改"}, ensure_ascii=False)]
|
||||
events = await _generate(world["uma"], monkeypatch, model)
|
||||
check = [e for e in events if e.get("op") == "check"][-1]
|
||||
final = next(e for e in events if e.get("op") == "final")
|
||||
@@ -448,9 +648,12 @@ async def test_source_names_for_the_chat_page(world):
|
||||
assert (await uma.get("/api/datasources")).status_code == 403
|
||||
await _allow_chat(world)
|
||||
body = (await uma.get("/api/datasources/names")).json()
|
||||
assert [i["name"] for i in body["items"]] == [world["source"]], "只列启用着的"
|
||||
assert set(body["items"][0]) == {"id", "name", "description", "kind"}, "连接信息不在名单里"
|
||||
assert body["items"][0]["description"] == "客户" and body["items"][0]["kind"] == "sqlite"
|
||||
# 同一个测试进程里别的测试留下的数据源也会在名单里:只核对这里建的两个(一个启用、一个停用)
|
||||
names = [i["name"] for i in body["items"]]
|
||||
assert world["source"] in names and not [n for n in names if n.startswith("off_")], "只列启用着的"
|
||||
item = next(i for i in body["items"] if i["name"] == world["source"])
|
||||
assert set(item) == {"id", "name", "description", "kind"}, "连接信息不在名单里"
|
||||
assert item["description"] == "客户" and item["kind"] == "sqlite"
|
||||
assert (await uma.get("/api/datasources")).status_code == 403, "完整的数据源列表照旧只给搭建者"
|
||||
assert (await bob.get("/api/datasources/names")).status_code == 200
|
||||
|
||||
|
||||
Reference in New Issue
Block a user