>catfathom-a-program-database-for-elixir.md

Fathom: A Program Database for Elixir

Compiling an Elixir project under a tracer and writing what the compiler knows to SQLite, so an agent can ask structural questions in SQL

date

Fathom is a program database for Elixir codebases. It compiles a project under a compiler tracer and writes what the compiler resolved into a SQLite file: every definition and every call, module dependencies and their kind, Ecto schemas, Phoenix routes, behaviours and protocols, and the places where dispatch is dynamic enough that the call graph stops being complete.

It exists for the kind of question that has no grep. The following asks which public functions can, transitively, delete everything:

WITH RECURSIVE up(mfa) AS (
  SELECT caller FROM calls WHERE callee = 'MyApp.Repo.delete_all/1'
  UNION
  SELECT c.caller FROM calls c JOIN up ON c.callee = up.mfa
)
SELECT f.mfa, f.file, f.line
FROM functions f JOIN up USING (mfa)
WHERE f.kind = 'def';

A language server answers such a question one hop at a time, because that is how a person reads code: jump to the definition, look around, jump again. An agent is under no such constraint. It will write the recursive query without complaint, and it can compose questions that no editor would think to put in a menu.1

Where the facts come from

Three sources are needed, each covering what the others cannot. A compiler tracer records references after macro expansion, which is the only way to see what use, import and framework macros actually generated. While each module is still open, Module.definitions_in/1 yields every definition, private ones included, and these cannot be recovered from the BEAM file afterwards. Once compilation is over, reflection fills in docs, specs and behaviours, along with whatever Ecto and Phoenix will say about themselves.

Two design decisions matter more than the schema. The first is that macro-generated definitions are flagged, since otherwise they swamp any question about the code a person actually wrote; run over Credo, Fathom found that 3,192 of its 4,783 function definitions came from a macro. The second is that gaps in the call graph are recorded rather than hidden. apply/3, a module held in a variable, a protocol dispatch: none of these produces an edge, so Fathom walks the expanded AST and records them in a dynamic_sites table. An agent asking who calls a function can then also ask whether the module dispatches dynamically, and so know how far to trust the answer.

Against grep

A benchmark in the repository puts nine real navigation questions to Credo, with both tools working on the same checkout. Grep answered one correctly, three approximately and four wrongly, and could not attempt the ninth. Asked which public functions reach Credo.Code.Block.do_block_for!/1, grep finds the nine direct call sites; the real answer is sixty-eight. Fathom’s references were also checked against mix xref callers, which reads the same compiler output by a different path, and they were a strict superset on every module tried.

Grep still wins on a freshly cloned repository that will be asked two questions, because it needs no index. The build is a full forced compile, taking around two and a half seconds for Credo’s 266 files and producing 79,057 facts.2

mix fathom.install   # writes the schema and example queries into AGENTS.md
mix fathom.build     # compiles under the tracer, writes .fathom/program.db
mix fathom.query "SELECT ..."

Of the three tasks, it is the install that makes an agent actually use the database. It writes the schema and a set of worked queries into AGENTS.md, between markers, so that running it again updates the file in place.

Footnotes

  1. The idea follows José Valim’s argument that the information a language server already holds is better exposed as a database than as a protocol. ↩

  2. An opt-in incremental mode lets the compiler decide what is stale and replaces only those modules’ rows. ↩