Garage (garagerag.app) 是我为自己的生活想要的搜索引擎:我写的、收集的以及收到的所有内容,都在我的电脑上建立索引,并一次向 AI 助手提供一段摘录。它采用 MIT 许可证,源码位于 lwm-luminx/garage-rag,并以签名的 Mac 应用、macOS、Linux 与 Windows 上的 garage 命令行,以及正在开发中的 Windows 应用形式运行。
本文介绍它的内部工作原理:磁盘上的文件夹与 Claude 给出的答案之间发生了什么,以及隐私界限到底画在哪里。

它的结构
sources ──▶ walker ──▶ [materialize] ──▶ extract ──▶ quality gate
│
attribution ◀─────────────────┤
│ ▼
└──────▶ documents ── chunks ◀── facts
│
┌───────────┴───────────┐
▼ ▼
emb_<model_1> emb_<model_2>
└───────────┬───────────┘
▼
hybrid search (RRF)
│
MCP server
所有数据都存储在带有 pgvector 的 PostgreSQL 中。Mac 应用自带可迁移的 PostgreSQL 18、pgvector 与 Apache AGE、用于本地嵌入和生成的 llama.cpp、用于图像嵌入的 Core ML,以及用于 OCR 的 Tesseract,因此无需额外安装任何东西。整个管道使用 Rust 编写。最初用 Python 实现,随后逐阶段移植。
遍历个人语料库是难点
个人电脑是一个充满敌意的索引环境。上面的多数内容并非由人撰写,很多根本不存在,且有些属于他人。
遍历过程中不打开文件。 遍历器对每个候选项统计一次,并在下降过程中剪枝排除的子树(版本控制、构建产出、依赖缓存如 .cargo/registry 和 go/pkg/mod、诊断包)。打开文件才会产生开销:解析,以及对云端文件的下载。
云端占位符受配额限制。 iCloud Drive 与 Dropbox 会留下“无数据”文件,报告真实大小但字节在云端,读取时会触发下载。Garage 将这些文件置于一个材料化预算之下,限制每次运行的文件数和字节数。在 macOS 上,内核同样强制执行预算:摄取线程关闭 IOPOL_TYPE_VFS_MATERIALIZE_DATALESS_FILES I/O 策略,因此管道其他位置对占位符的意外读取会返回 EDEADLK 而不是悄悄下载大量数据。只有材料化步骤会重新打开策略,以完成一次受预算限制的读取。达到上限并非错误。摄取是幂等的,重复的受限运行会逐步覆盖整个语料库。
机器生成的文本通过内容过滤。 我机器上的一次 macOS sysdiagnose 包产生约 200,000 条块,占整个索引的 76%。路径规则可以捕获大部分此类内容。质量门槛作为后备,检查结构化信号:重复的行形、时间戳前缀、十六进制和 base64 密度、字母与其他字符的比例。任何单一信号都会产生误报(参考文献重复、密码学论文充斥十六进制),因此拒绝文档需要多信号 corroboration 或一个决定性信号。我们在 21 份真实散文文档上验证了零误报,并通过每文档块上限确保单个文件不会主导索引。
重新摄取成本低廉。 每个文档保留两个哈希,且并非冗余:一个基于原始字节,用于在文件未改变且未打开(因此未材料化)时跳过;另一个基于提取的文本,用于在提取器改进时重新生成块,即使文件本身未变。当文档改变时,文本和位置仍匹配的块保留其行 ID,从而保留在每个模型表中的向量。Messages 线程新增一条消息时,仅重新嵌入一个块。
删除被视为危险操作。 一个未挂载的卷看起来就像一次大规模删除。只有当遍历完整且记录了遍历看到的每个 URI 时,运行才算完成;如果一次运行将删除超过 25% 的来源内容,则会在没有 --force 的情况下被拒绝。
提取支持 Markdown、PDF(当页面文本稀少时会升级为表格感知解析器)、Word、PowerPoint 与 Excel,图像通过 Tesseract,Apple Mail 与 .eml 消息,代码和配置文件。Messages 的 chat.db 被作为数据库读取而非遍历:每个会话成为一个文档,每条消息对应一个块,并记录是我发送还是接收的。
这是谁写的?
大多数个人 RAG 将每个文件视为相同。Garage 为每个文件记录来源,标记为 authored(我的原创)、reference(我收集的)或 received(别人发给我的),搜索时可以基于此过滤。询问“我写过关于共识的内容是什么?”与“我读过关于共识的内容是什么?”是不同的问题。
信号按顺序尝试,每一次决策都会记录其依据,以便错误答案可以追溯到触发它的规则:
- Git 历史 是权威的,并且按文件生效。如果我对某文件有提交记录,则标记为
authored;否则为reference。这是唯一能区分我自己的仓库与他人克隆的信号,并且能够正确处理分叉:在一个分叉中我修改了三份文件,这三份属于我,其余则属于上游。拥有仓库并不等同于拥有文件的作者身份。 - 嵌入元数据: PDF
/Author, Office 核心属性, Markdown 前置元数据. 此处会过滤工具名称,因为库会把自己的名称写入这些字段。如果不过滤,python-pptx’s default template attributes every slide deck to the library’s author. - 路径约定, 一个数据表而非代码。
- 源的默认值。
Mail 跳过元数据步骤: 发送者决定。来自我的消息标记为 authored,其他所有则为 received.
Running git log --follow once per file across about 18,000 tracked documents would mean 18,000 git invocations. Instead Garage makes one --name-only pass per repository and builds a path-to-authors map in memory: 60 repositories and 277,796 tracked paths in 29 seconds. History is read in-process through libgit2, so it works on a Mac without the developer tools installed.
超越模型寿命的存储
Chunks are model-agnostic. Each embedding model gets its own table, emb_<slug>, keyed on the chunk id with ON DELETE CASCADE. A vector(1024) and a vector(2560) cannot share a column, and per-model nullable columns would make indexing and backfills painful. With separate tables, adding a model is a backfill rather than a re-ingest, and two models can be compared side by side over the same chunks.
pgvector’s HNSW indexes have hard ceilings (2,000 dimensions for vector, 4,000 for halfvec), so the storage type follows the model’s width:
| 模型宽度 | 存储 | 索引 |
|---|---|---|
| ≤ 2,000 | vector(d) |
模型距离上的 HNSW |
| 2,001‑4,000 | halfvec(d) |
模型距离上的 HNSW |
| > 4,000,Matryoshka 训练的 | halfvec(4000), truncated and renormalized |
模型距离上的 HNSW |
| > 4,000,非 Matryoshka | vector(d) |
HNSW on binary_quantize(...), re-ranked on the exact distance |
截断仅在模型专门为此训练时才可靠,因此模型必须声明这一点,截断后的向量会重新归一化,因为单位向量的前缀本身不是单位向量。
Pictures get the same treatment. An image with little or no OCR text becomes one image chunk whose vector comes from the file, embedded by a CLIP-style model on Core ML. Search queries go through that model’s text tower, so a phrase can find a photo.
混合搜索
Search is Reciprocal Rank Fusion over two engines: vector nearest neighbours from pgvector, and Postgres full-text search. It takes 200 candidates from each and fuses them with k = 60. RRF only needs each side’s ranking, which matters because cosine distance and ts_rank_cd are on scales that cannot be compared.
One detail made a large difference in practice. The keyword side ORs its terms instead of ANDing them. A strict query for “secure enclave firmware validation” requires every word in one chunk and usually returns nothing. Since RRF decides the final order anyway, the keyword side should favour recall, and the vector side keeps the results on topic.
Facts, distilled locally
An optional pass distills documents into atomic facts using a local model, with a Rust port of the local part of Google’s LangExtract (langextract-rs). Every fact is grounded to the exact span of the document it came from, and a fact the extractor cannot locate in the text is dropped rather than stored. Each fact also gets a chunk of its own, so the ordinary backfill embeds it under every model and search finds facts alongside passages with no special path.
The same claim stated in five documents, or quoted in every message of a mail thread, is extracted five times. A clustering pass groups restatements into one distilled fact, with a guard that never groups facts disagreeing on numbers, dates or negation. Embeddings put “42 employees” next to “45 employees”, and “on Tuesday” next to “not on Tuesday”, so similarity alone is not enough. Where the database has Apache AGE, documents, authors and facts are projected into a graph the app can browse.
Serving it over MCP
The results reach an assistant over the Model Context Protocol, on stdio for a client that launches the server or local HTTP with DNS-rebinding protection. The read tools are rag_search, rag_get_document, rag_list_sources, rag_list_authors and rag_stats. Three more, rag_ask, rag_agent and rag_generate, generate text, and they do it entirely on a local model.
rag_ask runs the same retrieval, numbers the excerpts and asks the local model to answer citing [n]. rag_agent lets a small local model drive the read tools itself. Small models follow a one-line signature per tool better than a JSON schema, so that is what they get. By default the agent’s first step is a search it has already made: without it, a model such as Gemma 2 2B reads “when did I last talk to BMW?” as a question about its training data and answers that it has no access to personal information.
Garage works with Claude Desktop, Claude Code, Cursor and other MCP clients, and garage mcp-install writes the client configuration for you.
Where the privacy line actually is
The product claim is that Garage never uploads your files or its index. That claim holds, but what leaves your computer depends on which AI you connect, and it is worth being exact about it.
- A cloud assistant over MCP, which is the usual setup. The assistant’s app queries Garage on your computer and receives only the excerpts its searches return and the documents it opens, never the whole index. Garage uploads nothing, but the assistant does send those excerpts to its provider with your conversation.
- 完全本地化。 使用内置的 llama.cpp 引擎,或 Ollama 或 LM Studio 在同一台电脑上运行,并且助手在本地运行其模型,任何数据都不会离开机器。
- 在你的网络上运行自己的模型服务器。 文档和代码只会发送到该服务器,仅该服务器会处理。消息和邮件永不离开。
最后一条规则是在代码层面实现的,而不是政策层面,分层各自独立测试:
- 单一瓶颈点。 流程的任何部分都不会自行发起出站连接。每一次模型服务器请求都在同一处构造,并且只有在出站检查批准其目的地后才发送。
- 不使用云 AI SDK。 没有任何 crate 依赖它,锁文件中也没有。曾经有一个可选的云 OCR 备选方案;它已被移除。
- 目的地白名单。 回环地址,或恰好是你配置的模型服务器的来源。没有任何设置会添加其他主机。除
localhost之外的主机名永远不会被视为回环,即使它今天解析到回环,因为 DNS 响应可能会改变。客户端会忽略代理变量,也永不遵循重定向,所以http_proxy或Location头都无法将内容发送到别处。 - 通信保持在机器内部。 任何被归类为通信的内容(邮件、Messages)在目的地不是回环时都会被拒绝,即使是已批准的目的地。消息永不发送到箱外模型获取事实,若检索到答案则中止而不发送,其块也不会嵌入远程模型。
删除上述任意一层都会导致测试套件失败。
从其他地方访问它
以上所有功能都可以在一台没有任何云服务的电脑上运行。Garage Door 随 Garage 2.0 一起提供,是从外部进入的方式:网页上的助手,或手机上的 Garage Remote。你的电脑会打开一个出站的加密连接到门并保持打开状态,因此门本身不监听,路由器也无需更改。助手会为账户添加一个 MCP 地址,每台已连接的电脑都会响应,各自使用自己的名称和规则(默认只读搜索)。
门本身不存储、记录或缓存任何 MCP 内容;它只记录是哪扇门、使用何种方式、状态以及时间。对于网页上的 Claude 和 ChatGPT,门会在内存中处理每个请求,因此该路径并非端到端加密。Garage Remote 则不同:配对后,它通过端到端加密的连接访问你的电脑,门只转发它无法解读的密文。该加密流是 MCP 密封传输 的主题,文中将其重新表述为一个提议的 MCP 扩展。
Garage 本身是开源的。Garage Door 的代码是闭源的;其协议已公开。
试一试
Mac 应用位于 garagerag.app,配有 用户指南,以及本帖引用的完整 架构 与 隐私 文档。Linux 或 Windows 上,请参阅 README,从头开始配置 Postgres、Ollama 和 garage 命令。