Hill-climbing with agent swarms
Agent swarms are good at making a number go up. Give a swarm a task whose progress can be measured, and many agents can work on it at once, each checking its own changes against the same benchmark. The engineer's job moves from writing the code to building the environment: the benchmark, the metrics, and the rules that keep the swarm focused and consistent.
I used this approach to revive PyCG, a static call graph generator for Python. I wrote it during my research at the Athens University of Economics and Business, as part of the FASTEN project, and the paper appeared at ICSE 2021. A call graph is the map of which function calls which: security tools use it to check whether vulnerable code can be reached, and coding agents use it to see how a codebase connects.
I had archived it in November 2023: I didn't have the time to maintain it, and the requests kept coming. Once the environment was in place, the revival needed little oversight from me, mostly steering the swarm in the direction I wanted. Today it finds far more calls than the 2021 version, at the same or better precision, and it finishes every real package in the evaluation.
Each section below is one step of the method I used.
Pick something you can measure.
A swarm can only climb what it can score. A call graph is a set of edges, each from one function to another. Compare the edges that a tool reports with the correct ones and you get two numbers. Precision is the share of the reported edges that are correct. Recall is the share of the correct edges that the tool finds.
With these two numbers, an agent can judge its own work. It changes a rule of the analysis, runs the benchmark, and the numbers say whether the change helped. CI runs the same benchmark and rejects any change that lowers precision or recall by more than 0.01.
The numbers also let many agents work at once. A few lead agents split the work into tasks and handed each one to a sub-agent, which worked in its own copy of the repository, and the same benchmark judged them all.
Build a good benchmark.
The score is only as good as its ground truth. For PyCG, that is the correct call graph of every program in the benchmark. When I archived PyCG, its benchmark was written by hand: 119 small programs and five packages. Agents grew it. The micro-benchmark now has 696 small programs, one for every Python feature that can lead to a call. The macro-benchmark has 60 real packages with more than 42,000 call edges. Agents wrote and audited their call graphs without seeing PyCG's output, and checked them against execution traces of the packages' own test suites where those exist (54 of the 60).
Keep a blind set.
Agents that improve a tool on a benchmark also fit the tool to that benchmark. So I followed standard practice and always kept a blind set: packages that the agents had never seen, most of them drawn at random from the most downloaded packages on PyPI.
A frozen engine was measured on each blind set once. When its precision and recall fell well below the development set's, the analysis had missed something about Python. On the sixth batch, for example, recall was 0.38, against 0.88 on the development set. The batch then joined the development set, the agents fixed what it had exposed, and I drew a new one.
I repeated this for eight batches of five or ten packages; each batch after the first was measured once before it joined. The last two landed within 10 points of the development set, so I stopped there, and they then joined the development set too.
Use the old code as the benchmark.
A rewrite for performance comes with a ready-made benchmark: the old program's output. PyCG's engine was written in Python, and a large package took minutes and gigabytes. I asked the agents to port it to Rust.
PyCG's output is deterministic, so the Python engine was the reference: for every benchmark program and package, the Rust engine had to write the same JSON document, byte for byte. When 748 of 748 documents were identical, the Python engine was deleted.
PyCG is now one binary that needs no Python to run, and it is much faster. On the 20 large packages:
- the Python engine took 47 minutes and finished only 19, running out of memory on the last one;
- the Rust engine, on the day it matched Python, took 15 minutes and finished the same 19;
- today's engine finishes all 20 in under 4 minutes.
Peak memory fell from about 10 GiB to about 6 GiB. The slowest package, pandas, now takes 60 seconds.
The numbers.
| PyCG now | PyCG 2021 | |
|---|---|---|
| Micro-benchmark (696 cases) | ||
| Precision / recall | 0.95 / 0.95 | 0.88 / 0.59 |
| Macro-benchmark (60 packages) | ||
| Precision / recall | 0.94 / 0.88 | 0.92 / 0.50 |
| Packages finished | 60 | 53 |
| Median time per package | 0.17 s | 0.33 s |
| DyPyBench (47 projects) | ||
| Recall | 0.89 | 0.67 |
| Projects finished | 47 | 37 |
| Large packages (20) | ||
| Packages finished | 20 | 3 |
The new PyCG outperforms 2021's version on all metrics. Both versions ran under the same limits: 30 minutes and 16 GiB per run. Each version's numbers are over the items it finishes, and on the micro-benchmark both finish 688 of the 696 cases. The macro-benchmark row counts the calls between a package's own functions. The comparison is in the repository, with every result file.
PyCG 2021 is a port of the 2021 analysis to today's Python.
DyPyBench is a set of execution traces recorded by Bouzenia et al. (2024), and it was not part of the development loop. A trace shows the calls that a tool misses but not the ones it invents, so that row has recall only. The large packages, such as pandas and sympy, have no ground truth: they show whether a tool finishes and what it costs.
Try it.
PyCG installs with Cargo:
cargo install --git https://github.com/vitsalis/PyCG --locked pycg
Give it the name of the package and the files to start from:
pycg --package pkg pkg/module1.py pkg/subpackage/module2.py -o cg.json
The call graph is one JSON document.
pip install pycg still gives version 1.0.0, the final
release of the Python engine, not the Rust one.
PyCG does not follow code that is built at run time, so
eval and exec give no edges. When a
function is passed to outside code, such as a thread or a library,
the call that the outside code makes back to it is missing.
The code and both benchmarks are at github.com/vitsalis/PyCG. Issues about calls that it gets wrong are welcome.