工件库:内容寻址存储、节点输出与工具快照落盘、运行清单哈希

事件流里的输出是给画布看的截断预览,出具溯源需要完整证据。
新增 data/artifacts/ 内容寻址工件库(sha256 即地址,取回时复验哈希,
篡改即报错);节点完整输出与工具调用快照(query+结果集)自动落盘,
事件只携带工件 id。运行终态对全部事件计算清单哈希存入 Run,
事后对事件流的增删改会与它对不上。

graph_hash 剔除 viewport 后计算——挪画布不改变图的身份。
Run/Workflow/WorkflowVersion/Approval 预置分级与治理列,
init_db 附带 SQLite 轻量列迁移。
This commit is contained in:
ThearchyHelios
2026-09-14 10:10:20 +08:00
parent dd57c12016
commit 2df591d05f
8 changed files with 247 additions and 1 deletions
+22
View File
@@ -71,11 +71,33 @@ def _wrap(node: GraphNode, run_ctx: RunContext) -> Callable[[GraphState], Awaita
try:
updates = await runner(state, ctx)
elapsed = int((time.perf_counter() - started) * 1000)
# 完整输出落工件库。事件里只放截断预览是给画布看的;
# 出具溯源、周对比、轨迹提模板需要的是这份完整证据。
artifact_id: str | None = None
payload = (updates.get("nodes") or {}).get(node.id)
if payload is not None:
from app.core.artifact_store import put_json
try:
artifact_id = await put_json(
payload,
kind="node_output",
run_id=run_ctx.run_id,
node_id=node.id,
meta={"type": str(node.type), "attempt": attempt + 1},
)
if isinstance(payload, dict):
payload["__artifact__"] = artifact_id
except Exception: # noqa: BLE001 - 工件写失败不该毁掉运行本身
artifact_id = None
ctx.emit(
EventType.NODE_FINISHED,
duration_ms=elapsed,
attempt=attempt + 1,
preview=_preview(updates, node.id),
artifact=artifact_id,
)
trail = {
"node_id": node.id,
+12
View File
@@ -326,12 +326,24 @@ async def run_agent(state: GraphState, ctx: NodeContext) -> dict[str, Any]:
content = f"工具执行失败:{type(e).__name__}: {e}"
ok = False
elapsed = int((time.perf_counter() - started) * 1000)
snapshot_id: str | None = None
if ok:
from app.core.artifact_store import put_json
try:
snapshot_id = await put_json(
{"tool": name, "args": args, "result": content},
kind="tool_snapshot", run_id=ctx.run.run_id, node_id=ctx.node.id,
)
except Exception: # noqa: BLE001
snapshot_id = None
ctx.emit(
EventType.TOOL_END if ok else EventType.TOOL_ERROR,
tool=name,
call_id=call_id,
duration_ms=elapsed,
preview=content[:2000],
artifact=snapshot_id,
)
transcript.append(
{"tool": name, "args": args, "ok": ok, "duration_ms": elapsed, "result": content[:4000]}
+12 -1
View File
@@ -67,8 +67,19 @@ async def run_tool(state: GraphState, ctx: NodeContext) -> dict[str, Any]:
result = {"error": f"{type(e).__name__}: {e}"}
elapsed = int((time.perf_counter() - started) * 1000)
# 取数快照:query(args)和结果集一起进工件库,正式出具时数字回指的就是它
from app.core.artifact_store import put_json
try:
snapshot_id = await put_json(
{"tool": name, "args": args, "result": result},
kind="tool_snapshot", run_id=ctx.run.run_id, node_id=ctx.node.id,
)
except Exception: # noqa: BLE001
snapshot_id = None
ctx.emit(EventType.TOOL_END, tool=name, duration_ms=elapsed,
preview=json.dumps(result, ensure_ascii=False, default=str)[:2000])
preview=json.dumps(result, ensure_ascii=False, default=str)[:2000],
artifact=snapshot_id)
updates: dict[str, Any] = {"nodes": {ctx.node.id: result}}
var_name = ctx.cfg("assign_to", "")
+25
View File
@@ -100,6 +100,13 @@ class RunManager:
)
await session.commit()
async def note(
self, run_id: str, event_type: EventType | str, *, node_id: str | None = None,
**data: Any,
) -> None:
"""给 API 层用的事件入口(比如 formal 启动时记录口径升版处置)。"""
await self._emit(run_id, event_type, node_id=node_id, data=data)
# ---------------- 启动 ----------------
async def start(
@@ -111,6 +118,10 @@ class RunManager:
workflow_name: str = "",
memory_scope: str = "default",
collection: str = "default",
run_class: str = "exploratory",
version: int | None = None,
version_hash: str | None = None,
started_by: str | None = None,
) -> Run:
spec = GraphSpec.model_validate(graph)
report = validate_graph(spec)
@@ -126,6 +137,10 @@ class RunManager:
status="queued",
graph=graph,
input=input_payload,
run_class=run_class,
version=version,
version_hash=version_hash,
started_by=started_by,
)
run.thread_id = run.id # 一个 run 一条 checkpoint 线程
session.add(run)
@@ -343,6 +358,16 @@ class RunManager:
run.usage = usage
if status != "interrupted":
run.finished_at = datetime.now(timezone.utc)
# 终态时对全部已落库事件计算清单哈希:事后任何对事件流的
# 增删改都会与这个值对不上。中断态不算——事件还会继续追加。
from app.core.artifact_store import manifest_hash as _manifest
rows = await session.execute(
select(RunEvent.seq, RunEvent.type, RunEvent.node_id, RunEvent.data)
.where(RunEvent.run_id == run_id)
.order_by(RunEvent.seq)
)
run.manifest_hash = _manifest([tuple(r) for r in rows])
await session.commit()
if status == "succeeded":