Pinned Search and Reads
Source Graph answers each request at exactly one Git commit. It first turns the requested revision into a commit inside the repository's retention window, then searches the index or reads Git at that commit, and returns the commit with the result so the caller can keep using it. This page explains how revisions resolve, how versions are kept and indexed, what bounds a search or read, and why the approach is faster than running rg over a checkout.
sequenceDiagram
participant A as Agent
participant S as Source Graph
participant Z as Zoekt index
participant G as Git mirror
A->>S: search(repo, pattern) — no revision
S->>S: resolve default branch to commit C
S->>Z: generated query at C
Z-->>S: bounded hits
S-->>A: hits + Commit C + Partial / Truncated / Coverage
A->>S: read(repo, revision C, path, lines)
S->>G: blob at C
S-->>A: numbered lines + Commit C + next rangeHow a revision resolves
Every operation that reads code — search, read, read_many, list and diff — accepts a revision and turns it into one commit with the same rules. resolve applies the same rules and also accepts any other branch or tag, so it is the way in for a revision the other operations refuse:
| You pass | Source Graph uses | Accepted by |
|---|---|---|
| Nothing | The repository's discovered default branch when the policy follows it; otherwise the single configured main or master branch, at its last synchronized head | Every operation |
A monitored branch, by name or as refs/heads/<name> | That branch's last synchronized head; the remote is not asked, so the answer can lag behind the remote's latest commit | Every operation |
| A full 40-character lowercase commit SHA | That commit, if it is one of the repository's observed or retained versions; otherwise REVISION_NOT_ELIGIBLE | Every operation |
Any other branch or tag, by name or as refs/heads/<name> or refs/tags/<name> | The commit the ref currently points to on the remote | resolve only; the others return BRANCH_NOT_MONITORED or INVALID_REVISION |
A short SHA, HEAD, a revision expression or a name with characters the service does not accept | Rejected (SHORT_SHA_UNSUPPORTED, INVALID_REVISION) | — |
For a branch or tag outside the monitored set, resolve first consults the repository's ref catalog — the in-memory list of branches and tags that refs pages through — but trusts it only when it is fresh and complete. A stale catalog, a truncated one or a names-only one never proves that a name is absent; in those cases resolve asks the remote for exactly that name. The lookup downloads only the matching ref's commit and tag objects, without file trees, into a scratch directory, reads the object type and commit details from what was actually downloaded and deletes the directory. A branch that moves between listing and download therefore resolves to the downloaded commit, a tag that ends at a tree or blob returns REF_NOT_COMMIT, and an object that cannot be verified returns a retryable REMOTE_UNAVAILABLE rather than a guess. A bare name shared by a branch and a tag returns REF_AMBIGUOUS, and an unknown name REF_NOT_FOUND. The answer reports source (observed, catalog or remote), the full ref, and whether the commit is indexed. Resolving never downloads code, adds a version or builds an index.
REVISION_NOT_ELIGIBLE means the commit is not among the versions Source Graph currently keeps for that repository. A commit can be missing because retention or capacity released it, but also because it was never a synchronized branch head — commits pushed between two syncs are not recorded — because it comes from an unmonitored branch or a tag and was never prepared, or because the SHA belongs to a different repository; check the repository and full SHA together before assuming it expired. To use such a commit, prepare its full SHA. Source Graph never silently replaces a requested baseline with another commit, so a caller that pins the returned commit gets the same snapshot on every later call for as long as that version is kept.
Which versions exist
A repository's versions come from two places. Background synchronization fetches depth-one snapshots of one to eight monitored branch heads (optionally including the remote's default branch) — never tags, and without their parent history — on the repository's sync interval, and records each head; commits pushed between two syncs are not recorded. Preparing a full SHA adds any other commit: Source Graph fetches that snapshot directly from the code source without deepening the history, typically after resolve has turned a branch or tag into that SHA. Many versions of one repository can therefore coexist.
Every version is either protected or ordinary. Current branch heads, pinned snapshots and the commits of queued or running jobs are protected and never released. Pins are set only in the repository's settings; preparing a SHA does not pin it, so a prepared snapshot is ordinary like any other. Ordinary snapshots leave in two ways:
- Retention keeps them by days since first observation (1–365) or by a count of the most recently observed ones (1–4096). It runs after each successful sync and again as soon as a preparation publishes its index.
- Capacity releases the oldest unprotected snapshots, by first observation, instead of refusing new work: when a path group reaches 4096 snapshots, and when the repository's storage budget is reached at a safe point before or after a fetch or index build, in batches of at most 64.
A released version answers REVISION_NOT_ELIGIBLE until it is prepared again. Only when protected versions alone fill the 4096 slots or the budget does work fail, with SNAPSHOT_LIMIT or REPOSITORY_STORAGE_LIMIT.
Indexing is separate from eligibility. After a successful sync, the service queues an index job for each branch head that is not yet indexed, and stops retrying a head after three failures. Older retained snapshots stay readable from Git but are searchable only after someone with permission asks for them with prepare; searching an unindexed commit returns INDEX_NOT_READY, and resolve reports indexed: false with a hint about who can prepare it. Each index is stored as a separate generation. Generations that no retained snapshot references are collected in background rounds that work repository by repository, and a separate background pass finishes the Git collection of released snapshots, so their space is recovered even if the job that released them was interrupted.
What bounds a search
Callers never send engine syntax. A search is a set of structured, rg-like fields — a line-oriented regular expression or literal, case folding, globs and directory paths — that the service compiles into an engine query at the resolved commit. The Query Syntax reference lists every field.
Results are a bounded first page. The first attempt returns up to 100 files and 1,000 matches with two lines of context; if the response would exceed its 2 MiB budget, the service retries with smaller pages and finally a file-only listing, all within a five-second deadline. The response metadata tells the caller what it got:
CommitandRepositoryidentify the snapshot.Partialmeans the engine stopped early, for example at its 10,000-match cap.Truncatedmeans more files or matches existed than were displayed.Coveragedescribes what was indexed — text only, no symbol index, the registered paths and groups — so a zero-hit result is not proof that something is absent from the repository.
What bounds a read
A read returns an inclusive line range of one file at one commit, at most 500 lines and 256 KiB per call; end_line is the absolute number of the last line, not a line count. When no range is given over MCP it starts at line 1 and covers up to 500 lines. The response says which lines were returned and where to continue, and flags truncation, binary content, unsupported encodings and Git LFS pointers instead of guessing.
In a full-repository registration, reads go straight to Git and do not wait for indexing. In a monorepo registration, the path group must already be prepared at that commit (CONTENT_NOT_READY otherwise). read_many reads one to ten known ranges from one repository at one full commit SHA in a single MCP call, sharing a budget of 1,000 lines, 256 KiB and 30 seconds; each item reports its own content and error.
Why it is faster than a checkout
rg and git grep scan files on every call; the trigram index does that work once per commit. The project's README reports this comparison against the public demo repository (context4ai/context, 2,359 indexed files) and a local checkout of the same commit:
| Query | Source Graph (server) | rg (local) | git grep (local) | grep -r (local) |
|---|---|---|---|---|
| Literal, 13 matching files | 0.7–0.9 ms | 61 ms | — | — |
| Literal, ~100+ matching files | 3.6–5.2 ms | 61 ms | 39 ms | 3.6 s |
| Regex, no matches | 0.4 ms | 62 ms | — | — |
Server time is the search duration the engine reports. The local tools ran on an Apple M4 Pro with a warm page cache. Add the network round trip — a few milliseconds within one region — for end-to-end latency. The gap widens as repositories grow, and the service also removes the need to check out every repository at the right revision.
Speed depends on locality: each instance keeps its Git mirrors and indexes on its own disk, so deploy instances in the same region as the Agents that call them. How Context's own measurements compare answering with and without Source Graph is covered in Knowledge Retrieval.
固定版本的搜索与阅读
Source Graph 对每个请求都只在一个确定的 Git 提交上作答。它先把请求的版本转换为仓库保留窗口内的一个提交,然后在该提交上搜索索引或读取 Git,并把这个提交随结果一起返回,方便调用方继续使用。本页说明版本如何解析、版本如何保留和建索引、搜索和读取受哪些限制,以及为什么这种方式比在检出目录上运行 rg 更快。
sequenceDiagram
participant A as Agent
participant S as Source Graph
participant Z as Zoekt 索引
participant G as Git 镜像
A->>S: search(repo, pattern),不指定版本
S->>S: 将默认分支解析为提交 C
S->>Z: 在 C 上执行生成的查询
Z-->>S: 有上限的命中结果
S-->>A: 命中结果 + Commit C + Partial / Truncated / Coverage
A->>S: read(repo, revision C, path, lines)
S->>G: C 上的 blob
S-->>A: 带行号的内容 + Commit C + 下一段范围版本如何解析
所有读取代码的操作——search、read、read_many、list 和 diff——都接受一个版本参数,并按相同规则把它转换为一个提交。resolve 使用同样的规则,还接受任意其他分支或标签,因此其他操作拒绝的版本可以先通过它解析:
| 你传入 | Source Graph 使用 | 适用操作 |
|---|---|---|
| 不传 | 若保留策略跟随默认分支,则使用发现到的仓库默认分支;否则使用唯一配置的 main 或 master 分支,取其最近一次同步的最新提交 | 所有操作 |
一个监控分支,可写分支名或 refs/heads/<name> | 该分支最近一次同步的最新提交;不会询问远端,因此结果可能落后于远端的最新提交 | 所有操作 |
| 一个 40 位小写的完整提交 SHA | 该提交,前提是它是该仓库已观察到或已保留的版本之一;否则返回 REVISION_NOT_ELIGIBLE | 所有操作 |
其他任意分支或标签,可写名称、refs/heads/<name> 或 refs/tags/<name> | 该引用当前在远端指向的提交 | 仅 resolve;其他操作返回 BRANCH_NOT_MONITORED 或 INVALID_REVISION |
短 SHA、HEAD、版本表达式,或包含服务不接受字符的名称 | 拒绝(SHORT_SHA_UNSUPPORTED、INVALID_REVISION) | — |
对于监控范围之外的分支或标签,resolve 会先查阅仓库的引用目录——即 refs 分页浏览的那份内存中的分支和标签列表——但只有在目录新鲜且完整时才信任它。过期的、被截断的或只有名称的目录都不能证明某个名称不存在;这些情况下,resolve 会向远端精确查询该名称。查询只把匹配引用的提交对象和标签对象(不含文件树)下载到一个临时目录,根据实际下载的内容读取对象类型和提交详情,然后删除该目录。因此,在列出与下载之间移动过的分支会解析到实际下载的提交;最终指向树或 blob 的标签返回 REF_NOT_COMMIT;无法验证的对象返回可重试的 REMOTE_UNAVAILABLE,而不是给出猜测。分支和标签同名的裸名称返回 REF_AMBIGUOUS,未知名称返回 REF_NOT_FOUND。结果会报告 source(observed、catalog 或 remote)、完整的 ref,以及该提交是否已建索引(indexed)。解析从不下载代码、增加版本或建索引。
REVISION_NOT_ELIGIBLE 表示该提交不在 Source Graph 当前为这个仓库保留的版本中。提交缺失可能是因为保留策略或容量限制释放了它,也可能是因为它从未作为分支最新提交被同步过——两次同步之间推送的提交不会被记录——或者它来自未被监控的分支或标签且从未执行过 prepare,又或者这个 SHA 属于另一个仓库;在认定它已过期之前,先把仓库和完整 SHA 一起核对。要使用这样的提交,就对它的完整 SHA 执行 prepare。Source Graph 从不悄悄把请求的基线替换成另一个提交,因此调用方只要固定使用返回的提交,在该版本被保留期间,之后每次调用拿到的都是同一个快照。
有哪些版本
仓库的版本有两个来源。后台同步按仓库的同步间隔,拉取一到八个监控分支(可选包含远端的默认分支)最新提交的深度为一的快照——从不拉取标签,也不拉取其父提交历史——并记录每个最新提交;两次同步之间推送的提交不会被记录。对完整 SHA 执行 prepare 可以加入任何其他提交:Source Graph 直接从代码源拉取该快照,不会加深历史,通常是在 resolve 把某个分支或标签解析为该 SHA 之后进行。因此,同一仓库的多个版本可以并存。
每个版本要么受保护,要么是普通版本。当前的分支最新提交、被固定的快照,以及排队中或运行中任务所用的提交都受保护,永远不会被释放。固定只能在仓库设置中配置;对某个 SHA 执行 prepare 并不会固定它,因此准备好的快照与其他快照一样是普通快照。普通快照通过两种方式移除:
- 保留策略按首次观察到之后的天数(1–365)或最近观察到的快照数量(1–4096)保留快照。它在每次同步成功后运行,并在某次准备发布其索引后立即再运行一次。
- 容量限制会按首次观察时间释放最旧的未受保护快照,而不是拒绝新的工作:当某个路径组达到 4096 个快照时,以及在拉取或建索引前后的安全时机达到仓库的存储预算时触发,每批最多释放 64 个。
已释放的版本会返回 REVISION_NOT_ELIGIBLE,直到再次执行 prepare。只有当受保护的版本本身就占满 4096 个名额或存储预算时,工作才会失败,返回 SNAPSHOT_LIMIT 或 REPOSITORY_STORAGE_LIMIT。
建索引与版本是否符合条件相互独立。同步成功后,服务会为每个尚未建索引的分支最新提交排入一个索引任务;同一个最新提交失败三次后不再重试。较早的保留快照仍可从 Git 读取,但只有在有权限的人通过 prepare 请求后才能被搜索;搜索未建索引的提交会返回 INDEX_NOT_READY,resolve 则报告 indexed: false,并提示谁可以准备它。每份索引都作为独立的一代(generation)存储。不被任何保留快照引用的代会在后台按轮次逐个仓库回收;另有一个后台流程负责完成已释放快照的 Git 回收,因此即使释放它们的任务中断,其空间也能回收。
搜索受哪些限制
调用方从不发送引擎语法。一次搜索是一组结构化、类似 rg 的字段——按行匹配的正则表达式或字面量、大小写折叠、glob 和目录路径——由服务在解析出的提交上编译为引擎查询。搜索语法 参考文档列出了所有字段。
结果是有上限的第一页。第一次尝试最多返回 100 个文件和 1,000 个匹配,并附带两行上下文;如果响应会超出 2 MiB 的预算,服务会改用更小的页重试,最后退化为只列文件,整个过程都在五秒期限内完成。响应元数据会告诉调用方拿到了什么:
Commit和Repository标识快照。Partial表示引擎提前停止,例如达到其 10,000 个匹配的上限。Truncated表示实际存在的文件或匹配多于显示的数量。Coverage描述索引了哪些内容——仅文本、没有符号索引、已登记的路径和路径组——因此零命中并不能证明仓库中不存在某样东西。
读取受哪些限制
read 返回某个提交上一个文件的闭区间行范围,每次调用最多 500 行、256 KiB;end_line 是最后一行的绝对行号,而不是行数。通过 MCP 调用且未指定范围时,从第 1 行开始,最多覆盖 500 行。响应会说明返回了哪些行以及从哪里继续,并明确标出截断、二进制内容、不支持的编码和 Git LFS 指针,而不是靠猜测处理。
在全量仓库登记下,读取直接访问 Git,无需等待建索引。在 Monorepo 登记下,该路径组必须已在该提交上准备好(否则返回 CONTENT_NOT_READY)。read_many 在一次 MCP 调用中读取同一仓库、同一完整提交 SHA 上一到十个已知范围,共享 1,000 行、256 KiB 和 30 秒的预算;每一项分别报告自己的内容和错误。
为什么比检出目录更快
rg 和 git grep 每次调用都要扫描文件;三元组索引则对每个提交只做一次这项工作。项目 README 给出了在公开演示仓库(context4ai/context,2,359 个已索引文件)与同一提交的本地检出目录上的对比:
| 查询 | Source Graph(服务端) | rg(本地) | git grep(本地) | grep -r(本地) |
|---|---|---|---|---|
| 字面量,13 个匹配文件 | 0.7–0.9 ms | 61 ms | — | — |
| 字面量,约 100+ 个匹配文件 | 3.6–5.2 ms | 61 ms | 39 ms | 3.6 s |
| 正则,无匹配 | 0.4 ms | 62 ms | — | — |
服务端时间是引擎报告的搜索耗时。本地工具运行在 Apple M4 Pro 上,页缓存已预热。端到端延迟还需加上网络往返——同一地域内为几毫秒。仓库越大,差距越明显;而且该服务还省去了把每个仓库按正确版本检出的需要。
速度取决于本地性:每个实例都把自己的 Git 镜像和索引保存在自己的磁盘上,因此应把实例部署在与调用它的 Agent 相同的地域。Context 自己关于使用与不使用 Source Graph 作答的对比测量,见 知识检索。