-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistory.py
More file actions
152 lines (131 loc) · 6.19 KB
/
Copy pathhistory.py
File metadata and controls
152 lines (131 loc) · 6.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
"""Replay a LangGraph app's git history through `graphlock check`.
uv run python scripts/history.py https://github.com/langchain-ai/react-agent --workdir /tmp/gl-history
For every commit that touches Python files (oldest first), it reads langgraph.json, extracts the
shape of each graph in the app's own environment, and runs `check` between consecutive shapes of
the same graph. The result is every change in the app's history that `check` would have flagged,
written to <workdir>/<repo>.json for review. The environment is the app's current dependencies, so
commits old enough not to import with them are counted and skipped.
"""
from __future__ import annotations
import argparse
import json
import subprocess
import sys
from pathlib import Path
from typing import Any
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "src"))
from graphlock import check # noqa: E402
def run(*args: str, cwd: Path | None = None) -> str:
return subprocess.run(args, cwd=cwd, capture_output=True, text=True, check=True).stdout
def prepare(url: str, work: Path) -> tuple[Path, Path]:
name = url.rstrip("/").rsplit("/", 1)[-1]
clone = work / name
if not clone.exists():
run("git", "clone", "-q", url, str(clone))
run("git", "checkout", "-q", "-f", run("git", "rev-parse", "origin/HEAD", cwd=clone).strip(), cwd=clone)
venv = work / f"{name}-venv"
if not venv.exists():
run("uv", "venv", "-q", "-p", "3.12", str(venv))
run("uv", "pip", "install", "-q", "-p", str(venv), "-e", str(clone), "-e", str(ROOT))
return clone, venv / "bin" / "python"
def commits(clone: Path, limit: int | None) -> list[tuple[str, str, str]]:
# --first-parent: the mainline, one commit after another as they were deployed. Without it,
# commits of merged branches interleave, and "consecutive" commits are not parent and child.
log = run(
"git",
"log",
"--first-parent",
"--reverse",
"--format=%H%x09%ad%x09%s",
"--date=short",
"HEAD",
"--",
"*.py",
"langgraph.json",
cwd=clone,
)
rows = [tuple(line.split("\t", 2)) for line in log.splitlines() if line]
return rows[-limit:] if limit else rows # type: ignore[return-value]
def shapes_at(clone: Path, python: Path, sha: str) -> dict[str, Any]:
run("git", "checkout", "-q", "-f", sha, cwd=clone)
if not (clone / "langgraph.json").exists():
return {}
proc = subprocess.run(
[str(python), str(ROOT / "scripts" / "_extract_shapes.py"), str(clone)],
capture_output=True,
text=True,
check=False,
timeout=300,
)
if proc.returncode != 0:
return {
"*": {"error": proc.stderr.strip().splitlines()[-1][:200] if proc.stderr.strip() else "failed"}
}
return json.loads(proc.stdout.strip().splitlines()[-1])
def diff(old: dict[str, Any], new: dict[str, Any]) -> list[str]:
"""What changed between two shapes, in words, for reviewing changes `check` stays quiet about."""
out = []
pairs = (("node", old["nodes"], new["nodes"]), ("field", old["state"]["fields"], new["state"]["fields"]))
for kind, a, b in pairs:
out += [f"{kind} added: {n}" for n in sorted(set(b) - set(a))]
out += [f"{kind} removed: {n}" for n in sorted(set(a) - set(b))]
for name in sorted(set(old["nodes"]) & set(new["nodes"])):
o, n = old["nodes"][name], new["nodes"][name]
if o.get("code") != n.get("code"):
out.append(f"code changed: {name}" + (" (calls interrupt)" if n.get("interrupts") else ""))
if o.get("triggers") != n.get("triggers"):
out.append(f"triggers changed: {name}")
if o.get("subgraph") != n.get("subgraph"):
out.append(f"subgraph changed: {name}")
for name in sorted(set(old["state"]["fields"]) & set(new["state"]["fields"])):
if old["state"]["fields"][name] != new["state"]["fields"][name]:
out.append(f"field changed: {name}")
if old["types"] != new["types"]:
out.append("stored classes changed")
if not out and old["channels"] != new["channels"]:
out.append("channels changed")
return out or ["other: breakpoints"]
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("url")
parser.add_argument("--workdir", type=Path, required=True)
parser.add_argument("--limit", type=int, help="only the most recent N commits")
args = parser.parse_args()
args.workdir.mkdir(parents=True, exist_ok=True)
clone, python = prepare(args.url, args.workdir)
history = commits(clone, args.limit)
last: dict[str, tuple[str, Any]] = {}
report: dict[str, Any] = {"repo": args.url, "commits": len(history), "importable": 0, "changes": []}
for sha, date, subject in history:
shapes = shapes_at(clone, python, sha)
ok = {name: shape for name, shape in shapes.items() if "error" not in shape}
report["importable"] += bool(ok) and len(ok) == len(shapes)
for name, shape in ok.items():
previous = last.get(name)
last[name] = (sha, shape)
if previous is None or previous[1] == shape:
continue
findings = check(previous[1], shape)
report["changes"].append(
{
"graph": name,
"from": previous[0][:10],
"to": sha[:10],
"date": date,
"subject": subject,
"diff": diff(previous[1], shape),
"findings": [f.to_json() for f in findings],
}
)
print(f"{sha[:10]} {date} {len(ok)}/{len(shapes)} graphs {subject[:70]}", file=sys.stderr)
run("git", "checkout", "-q", "-f", history[-1][0] if history else "HEAD", cwd=clone)
out = args.workdir / f"{clone.name}.json"
out.write_text(json.dumps(report, indent=2))
blocking = [c for c in report["changes"] if any(f["blocking"] for f in c["findings"])]
print(
f"{clone.name}: {report['commits']} commits, {report['importable']} importable, "
f"{len(report['changes'])} shape changes, {len(blocking)} with blocking findings -> {out}"
)
if __name__ == "__main__":
main()