Files

136 lines
6.8 KiB
Python

"""Building on earlier results: cross-graph imports, store.query, lineage across graphs,
re-entrant loops across runs."""
import json
import yaml
from pipewright import Graph, ops
from pipewright.check import check
from conftest import FAKE
def ev(summary, node):
return summary["funnel"]["nodes"][node]["events"]
def test_two_graph_example_is_incremental(example):
d = example("hardening")
b1 = ops.run_graph(d / "graph_build.py", limit=3) # add, classify, reverse_words
assert b1["funnel"]["nodes"]["bucket"]["out"] == {"too_easy": 1, "learnable": 1, "too_hard": 1}
h1 = ops.run_graph(d / "graph_harden.py")
assert h1["status"] == "completed", h1["errors"]
assert h1["funnel"]["nodes"]["build:bucket"]["out"] == {"too_easy": 1}
assert h1["funnel"]["nodes"]["build:bucket"]["imported_from"]["graph"] == "build"
assert h1["funnel"]["nodes"]["bucket"]["out"] == {"learnable": 1} # add_many: 1 of 2 rollouts pass
# upstream grows: only the new too_easy task is hardened and probed
ops.run_graph(d / "graph_build.py")
h2 = ops.run_graph(d / "graph_harden.py")
assert ev(h2, "harden") == {"ok": 2, "cached": 1}
assert ev(h2, "probe") == {"ok": 4, "cached": 2}
assert h2["funnel"]["nodes"]["bucket"]["out"] == {"learnable": 1, "too_hard": 1}
# lineage crosses the graph boundary: the variant's root is build's synthesized task
var = ops.query(d / "graph_harden.py", node="variant", where="payload.name == 'second_biggest'")["items"][0]
roots = ops.lineage(d / "graph_harden.py", var["id"], roots=True)["roots"]
assert [(r["key"], r["origin"]["graph"], r["producer"]) for r in roots] == [("biggest", "build", "synth")]
desc = ops.lineage(d / "graph_build.py", "biggest", descendants=True)["descendants"]
assert {x["key"] for x in desc if x["origin"]["graph"] == "harden"} >= {"second_biggest", "biggest"}
# report shows the cross-graph edge; preview/funnel work with imports
html = open(ops.report(d / "graph_harden.py")["path"]).read()
assert "from graph build:bucket" in html and "edge ext" in html
pv = ops.preview(d / "graph_harden.py", upto="variant", n=1)
assert pv["status"] == "completed" and len(pv["items"]) == 1
def test_check_reports_bad_imports(example):
d = example("hardening")
spec = yaml.safe_load(json.dumps({"name": "bad", "imports": {"build": {"graph": "graph_build.py"}},
"nodes": {"x": {"type": "filter.expr", "inputs": {"in": "build:bucket.nope"},
"params": {"expr": "true"}},
"y": {"type": "filter.expr", "inputs": {"in": "build:nonode"},
"params": {"expr": "true"}},
"z": {"type": "filter.expr", "inputs": {"in": "other:a.b"},
"params": {"expr": "true"}}}}))
(d / "bad.yaml").write_text(yaml.safe_dump(spec))
res = ops.check_graph(d / "bad.yaml")
msgs = [x["message"] for x in res["diagnostics"] if x["code"] == "PW020"]
assert any("no output port 'nope'" in m for m in msgs)
assert any("has no node 'nonode'" in m for m in msgs)
assert any("unknown import alias 'other'" in m for m in msgs)
assert not res["ok"]
def test_imported_port_kinds_are_checked(example):
d = example("hardening")
g = Graph("kinds")
up = g.import_graph("build", "graph_build.py")
g.add("only_issues", "llm.label", input=up.node("bucket").port("too_easy"), model="generator",
prompt="x", labels={"a": {"type": "string"}})
spec = g.to_spec()
spec._base_dir = d
from pipewright.project import Project
diags = check(spec, Project.discover(d))
assert not [x for x in diags if x.level == "error"], [x.format() for x in diags]
from pipewright.imports import expand_imports
ex = expand_imports(spec, Project.discover(d))
assert ex.nodes["build:bucket"].params["ports"] == {"too_easy": "task_result"}
def test_store_query_node_and_other_store(make_project, tmp_path):
make_project(FAKE)
g = Graph("scores")
src = g.add("src", "source.inline", kind="t", records=[{"id": f"r{i}", "s": i / 10} for i in range(10)])
g.add("lab", "label.expr", input=src, labels={"score": "payload.s"})
g.save(tmp_path / "scores.yaml")
ops.run_graph(tmp_path / "scores.yaml")
q = Graph("seeds")
sel = q.add("sel", "store.query", graph="scores", node="lab", where="labels.score >= 0.75", kind="t")
q.add("tag", "label.expr", input=sel, labels={"seeded": "True"})
q.save(tmp_path / "seeds.yaml")
r = ops.run_graph(tmp_path / "seeds.yaml")
assert r["funnel"]["nodes"]["sel"]["out"] == {"out": 2} # 0.8, 0.9
# another project reading this store: items (and lineage) are copied over
other = tmp_path / "other"
other.mkdir()
(other / "pipewright.yaml").write_text(yaml.safe_dump({"store": ".pw2", **FAKE}))
q2 = Graph("remote-seeds")
q2.add("sel", "store.query", store=str(tmp_path / "pipewright.yaml"), graph="scores", node="lab",
labels={"score": 0.9})
q2.save(other / "g.yaml")
r2 = ops.run_graph(other / "g.yaml")
assert r2["funnel"]["nodes"]["sel"]["out"] == {"out": 1}
item = ops.items(other / "g.yaml", "sel")["items"][0]
lin = ops.lineage(other / "g.yaml", item["id"])["lineage"]
assert len(lin) == 2 # copied with its parent
res = ops.query(tmp_path / "scores.yaml", node="lab", where="labels.score < 0.2")
assert res["total"] == 2 and res["as_node"]["type"] == "store.query"
def test_reentrant_loop_across_runs(make_project, tmp_path):
"""Failures of run N become seeds of run N+1 (self import), without a live cycle."""
make_project(FAKE)
g = Graph("loop")
prev = g.import_graph("prev", "self") # run=previous by default for self
seeds = g.add("seeds", "source.inline", kind="n", records=[{"id": "a", "v": 1}, {"id": "b", "v": 8}])
step = g.add("step", "map.expr", input=[seeds, prev.node("audit").port("retry")],
fields={"v": "payload.v * 2"})
g.add("audit", "route.expr", input=step, routes={"done": "payload.v >= 10"}, default="retry")
g.save(tmp_path / "loop.yaml")
assert ops.check_graph(tmp_path / "loop.yaml")["ok"]
outs = []
for _ in range(4):
r = ops.run_graph(tmp_path / "loop.yaml")
assert r["status"] == "completed", r["errors"]
outs.append(r["funnel"]["nodes"]["audit"]["out"])
# a: 1->2 (retry) ->4 ->8 ->16 done; b: 8->16 done
assert outs[0] == {"done": 1, "retry": 1}
assert outs[1] == {"done": 1, "retry": 2} # a's 2 is retried as 4 (also re-emitted from seeds: memoized)
vals = sorted(i["payload"]["v"] for i in ops.items(tmp_path / "loop.yaml", "audit", port="done", n=10)["items"])
assert 16 in vals