Code map¶
The code map is a structural graph of a source repository (files, classes, functions, methods and the relations between them) stored in the same brain as the memory graph. It is extracted on the developer's machine by a deterministic parser, pushed to Spongram over HTTP, and queried by agents through five MCP tools or by people through a 3D explorer page. This page covers what is indexed, how a repository is ingested, how concurrent ingests are serialised, and the limits that apply.
What is indexed¶
Extraction is done by the spongram_codemap package, which wraps the open-source graphifyy extractor
(tree-sitter based) and normalises its output. No language model is involved, and file contents are never
sent to the server: the payload holds only node and edge lists (names, kinds, relative file paths, source
locations).
| Language | File suffixes |
|---|---|
| Python | .py |
| JavaScript | .js, .jsx, .mjs, .cjs |
| TypeScript | .ts, .tsx |
| Rust | .rs |
| Go | .go |
Each node carries these fields:
| Field | Meaning |
|---|---|
id |
Identifier assigned by the extractor, unique within the repository |
label |
Human-readable name (file name, class name, function name) |
kind |
file, class, function or method |
source_file |
Path relative to the repository root |
source_location |
Position in the file, when the extractor reports one |
Each edge carries source, target, a relation (for example contains, method, calls,
imports; uses when the extractor gives none) and a confidence:
| Confidence | Meaning |
|---|---|
EXTRACTED |
Read directly from the syntax tree (the default) |
INFERRED |
Inferred by the extractor |
AST_INFERRED |
A qualified Python call such as module.func(), resolved by a Spongram post-pass over the file's imports; emitted only when the target resolves to exactly one symbol of the repository |
Three things are removed before the payload is sent, so that the map describes your code and nothing else:
- nodes the extractor creates for docstrings and comments;
- external symbols (imported from the standard library or from dependencies) that no scanned file defines;
- any edge whose source or target is not a node of the payload.
How a repository is ingested¶
Extraction always runs on the client. The package installs a spongram-codemap command with two
subcommands that take the same arguments:
spongram-codemap build /path/to/repo --base-url https://spongram.example.com \
--key spt_brain_xxx --repo my-repo --state /path/to/repo/.git/spongram-codemap.json
spongram-codemap update /path/to/repo --base-url https://spongram.example.com \
--key spt_brain_xxx --repo my-repo --state /path/to/repo/.git/spongram-codemap.json
Both extract the whole repository and send the complete node and edge lists. update also reads the
state file (a manifest of file hashes written by the previous run) and reports the files deleted since
then. The server replaces the repository's subgraph on every ingest, so a full build and an update
result in the same graph.
In normal use nobody types these commands. The Spongram plugin for Claude Code drives them through hooks (see Claude Code):
- At session start, the
setup-codemap.shhook runs when the working directory is inside a git repository. It copies the extractor and a launcher script to~/.spongram/codemap/, appends a marked block to the repository'spost-commitgit hook (existing hook content is kept, andcore.hooksPathis honoured), and, if the repository has no state file yet, starts a firstbuildin the background. - After each commit, the git hook starts an
updatein the background. A commit never waits for it and never fails because of it. - At session start, the recall hook also reads
GET /v1/codemap/data/minimapand injects a compact summary of the map (size, top-level directories, most connected nodes) into the session context.
Details that matter when something does not work:
- The launcher creates a private virtual environment under
~/.spongram/codemap/venvon first use and installs a pinned release of the extractor into it. It needs a Python 3.10 or newer interpreter on thePATH; without one the code map is skipped and a message is written to standard error. Output of background runs goes to~/.spongram/codemap/last.log. - The repository name sent as
repois the directory name of the main repository. - A linked git worktree never publishes the map. Because an ingest replaces the whole subgraph of a repository, publishing from worktrees would make the map follow whichever branch committed last.
- A per-repository lock directory (
spongram-codemap.lockin the git directory) debounces overlapping runs on one machine. It is a convenience; correctness comes from the server-side guard described below. - To opt out, set
SPONGRAM_CODEMAP_DISABLEin the environment of Claude Code or create the file~/.spongram/codemap/disabled.SPONGRAM_CODEMAP_SKIP_BUILDinstalls the git hook without the initial build. The plugin shipshooks/uninstall-codemap-hook.shto remove the git hook and the state file from a repository. - When the server answers
409or503, the client waits for theRetry-Afterdelay (15 seconds when the header is absent, 120 seconds at most) and tries again, up to 6 attempts and 10 minutes in total.
Ingest API¶
POST /v1/codemap/ingest, authenticated with the brain key (Authorization: Bearer spt_brain_xxx).
The route exists on a server and in the desktop edition.
| Body field | Required | Meaning |
|---|---|---|
repo |
yes | Non-empty repository name; the namespace of this subgraph inside the brain |
nodes |
yes | Array of node objects (see above) |
edges |
yes | Array of edge objects |
pruned_files |
no | Array of deleted file paths; accepted for the client contract, the graph does not depend on it |
group_id |
no | If present, must equal the group of the authenticated brain |
curl -X POST https://spongram.example.com/v1/codemap/ingest \
-H "Authorization: Bearer spt_brain_xxx" \
-H "Content-Type: application/json" \
-d '{"repo": "my-repo",
"nodes": [{"id": "cli", "label": "cli.py", "kind": "file", "source_file": "pkg/cli.py"},
{"id": "cli_main", "label": "main", "kind": "function", "source_file": "pkg/cli.py"}],
"edges": [{"source": "cli", "target": "cli_main", "relation": "contains", "confidence": "EXTRACTED"}]}'
| Status | Meaning |
|---|---|
401 |
Missing or invalid brain key |
402 |
Desktop edition only: the licence is locked, writes are suspended (reads still work) |
403 |
The body carries a group_id different from the authenticated brain's |
409 |
Another ingest of the same repository is running; the response has Retry-After: 60 |
422 |
The body is not a JSON object, or repo, nodes or edges is missing or of the wrong type |
With the Cloud control plane enabled, the request is first admitted by the control plane (it is
authorised as the memory.import action and may carry an Idempotency-Key header) before the graph is
written.
How the graph is stored¶
Code nodes use a single :Code label and relations a single :CODE_EDGE type, so the same queries run
on Neo4j (server) and on the embedded graph of the desktop edition. They live in the brain's
group_id next to the memory graph but are a separate layer: the Cortex explorer and its node and edge
counts exclude them.
| Node property | Value |
|---|---|
gid_uuid |
<group_id>::<repo>::<id>. The single key nodes are merged on |
uuid |
<repo>::<id> |
group_id, repo |
Brain and repository the node belongs to |
label, kind, source_file, source_location |
As sent by the client |
match_key |
Normalised label (case-folded, punctuation collapsed, file extension dropped) used for lookups |
updated_at |
Timestamp of the ingest that last wrote the node |
Edges carry relation, confidence, group_id, repo and updated_at.
An ingest is a sequence of independent statements, not one transaction. It is made safe by its order, write first and delete last:
- Upsert every node of the payload, stamping
updated_atwith the time of this ingest. - Upsert every edge the same way.
- Delete the edges, then the nodes, of this repository whose stamp is older than this ingest.
- Recompute the links between memory entities and code nodes for this repository.
If an ingest is interrupted, either nothing has been deleted yet or everything had already been written; the worst outcome is a few stale nodes that the next ingest removes. Two safeguards follow from the same reasoning: a payload with no node changes nothing (it is treated as a failed extraction, not as an order to delete the repository), and a payload with no edge leaves existing edges in place.
On Neo4j, an index on :Code(gid_uuid) is created at the first ingest. It is not created on the desktop
edition's embedded graph.
Links to memory entities¶
After each ingest, every memory entity of the brain whose normalised name equals a code label or a file
name of the repository is linked to that code node by a :REFERENCES relation. search_nodes then
returns, for each entity that has such links, a linked_code_nodes list of at most five items
(label, kind, source_file). The enrichment is best-effort and never fails a search.
The ingest guard¶
Every write to a repository's subgraph goes through ingest_guard, which takes two locks keyed on
(group_id, repo):
- an in-process lock, which absorbs contention inside one process at no cost;
- a PostgreSQL session advisory lock, which is the only one that holds across processes.
The guard exists because merging nodes is not safe under concurrency without a uniqueness constraint that
both graph back ends could enforce: two interleaved ingests of the same repository would duplicate its
graph. The in-process lock alone would require that a single process ever serves the API. The advisory
lock removes that requirement, which is what lets the service be rolled with order: start-first, where
the old and the new container overlap for a while, instead of being stopped during every deployment.
The wait for either lock is bounded by SPONGRAM_CODEMAP_LOCK_WAIT_SEC (10 seconds by default). Past
that, the server answers 409 with Retry-After: 60 rather than queueing the request. Nothing is lost:
an ingest is idempotent, the client retries, and the next commit sends the full map again. Ingests of
different repositories, or of different brains, run concurrently.
Note
The desktop edition uses SQLite, which has no advisory locks, and runs as a single process. There the guard is the in-process lock only.
The admin purge route described below takes the same guard, so it never crosses a running ingest.
Querying the map from an agent¶
Five MCP tools read the map. Spongram serves them itself (they are never forwarded to Graphiti), they are
always listed, and each one except code_map_stats accepts an optional repo argument to scope the
answer to one repository when the brain holds several. Arguments are documented in the
tools reference.
| Tool | Answers |
|---|---|
code_map_query |
A subgraph around the symbols matching a question, rendered as text |
code_map_neighbors |
Direct neighbours of a symbol or file: callers, callees, imports, methods |
code_map_god_nodes |
The most connected nodes of the map |
code_map_shortest_path |
The shortest path between two symbols |
code_map_stats |
Node, file, symbol and edge counts |
The map is intended for structural questions (what a module contains, what calls a function, which files are central). For "where is this symbol defined", a text search in the repository is cheaper.
The code map explorer¶
GET /v1/codemap serves a 3D view of the map. It is the Cortex page retargeted at the
code map: same rendering engine, same authentication (brain key in the URL fragment in the server
edition, no key in the desktop edition), same ?lang=fr|en parameter. The differences are that nodes are
coloured by kind, the project list shows ingested repositories, there is no live stream (the map is a
snapshot of the last ingest) and no "forget" action.
The page reads these routes, all authenticated with the brain key:
| Route | Returns |
|---|---|
GET /v1/codemap/data/stats |
Brain identity and node, file, symbol and edge counts |
GET /v1/codemap/data/minimap |
{"text": "..."}, the compact summary injected at session start; empty text when no map exists |
GET /v1/codemap/data/full?limit=&project=&label= |
Nodes and links for the 3D view; project is a repository, label a kind; limit defaults to 2000 |
GET /v1/codemap/data/search?q=&limit=&project= |
Nodes whose normalised label contains a term of q; limit defaults to 25 |
GET /v1/codemap/data/projects |
One entry per ingested repository with its node count |
GET /v1/codemap/data/labels?project= |
Node counts per kind |
GET /v1/codemap/data/node/{uuid} |
One node and up to 100 neighbours; 404 outside the brain |
Operators get the same page and data routes for any brain under
/admin/api/spongram/instances/{id}/codemap, authenticated with the admin token instead of the brain
key (see Cortex and Wiki for how the mirror works).
Removing a repository¶
On a server, an operator can delete one repository's map without touching the rest of the brain:
curl -X DELETE https://spongram.example.com/admin/api/spongram/instances/42/codemap/my-repo \
-H "Authorization: Bearer <admin-token>"
The answer is {"repo": "my-repo", "deleted": <node count>}; deleted is 0 when the repository had
no map, and the status is 404 when the instance does not exist. Use it for a repository indexed under
a wrong name. To stop a repository from being indexed again, remove its git hook first.
Limits and settings¶
| Limit | Value | Setting |
|---|---|---|
Wait for the ingest guard before 409 |
10 s | SPONGRAM_CODEMAP_LOCK_WAIT_SEC |
Retry-After on a refused ingest |
60 s | fixed |
| Timeout of each graph statement of an ingest | 300 s | SPONGRAM_NEO4J_INGEST_TIMEOUT_SEC |
Client retries on 409 and 503 |
6 attempts, 10 min in total | fixed in the client |
| Client request timeout | 120 s | fixed in the client |
code_map_query depth |
1 to 6, default 2 | tool argument depth |
code_map_query text size |
about 2000 tokens by default | tool argument token_budget |
code_map_neighbors result size |
200 rows | fixed |
linked_code_nodes per entity in search_nodes |
5 | fixed |
Keep SPONGRAM_CODEMAP_LOCK_WAIT_SEC well below the database command timeout
(SPONGRAM_PG_COMMAND_TIMEOUT_SEC, 30 seconds by default): the wait is meant to end in an explicit 409, not in a database
timeout that would surface as a 500.