自建AI Agent开发工厂:硬件、网络与自动化部署实践
Self-hosted software development factory
Agent落地常遇安全与运维难题,这篇提供了从硬件选型到网络隔离、密钥管理的完整可复用架构,值得做本地Agent的同学参考。
Recently, I have described my vibe-coding setup, and I mentioned there how my MacBook is getting warmer and no longer holding the battery, now that I run a bunch of VMs on it. In that article, I said that it would be cool to buy a machine that will be at home, to which I can VPN from my laptop and monitor my army or LLM agents who do the work for me 24/7. Well, I’m glad to say that I have achieved that.
最近,我描述了我的 vibe-coding 环境,并提到过由于我在 MacBook 上运行了一堆虚拟机,它变得越来越热且电池不再耐用。在那篇文章中,我说如果能买一台放在家里的机器就好了,这样我可以从笔记本电脑通过 VPN 连接上去,监控我的“军队”或全天候为我工作的 LLM 代理。嗯,我很高兴地说,我已经实现了这一目标。
In this article I would like to describe what have I made, and how it works. With the advent of AI summarization tools, I believe that not many humans will read this, so I guess my main reason for writing this to try to understand myself what have I done and how it works, but also inspire others who still like to read human generated content, to perhaps experiment.
在这篇文章中,我想描述一下我做了什么,以及它是如何工作的。随着 AI 摘要工具的出现,我相信不会有太多人类阅读这篇文章,所以我猜我写这篇文章的主要原因是试图理清自己做了什么以及它是如何工作的,同时也希望激励那些仍然喜欢阅读人类生成内容的其他人去尝试实验。
AI is great for doing risky experiments, but at the same time I do see the downsides in the fact that people no longer consume human written content, and every blog post is scraped a thousand times, shredded into tokens, and fed back to users through “AI Overview”. You can check out my other blog post about my AI bipolar disorder: I think I have AIpolar disorder.
AI 非常适合进行高风险的实验,但同时我也看到了弊端:人们不再消费人类撰写的内容,每篇博客文章都被抓取上千次,被粉碎成 token,然后通过“AI 概览”反馈给用户。你可以查看我关于我的 AI 双相情感障碍的另一篇博文:我觉得我有 AIpolar disorder(AI 双相障碍)。
Lastly, this article will be split into sections that build from the ground up, starting with hardware, moving to networking, virtualization, software, and harness. Feel free to skip/skim any section that is not relevant or interesting for you. Enjoy your reading.
最后,这篇文章将分为几个部分,从基础开始构建,依次介绍硬件、网络、虚拟化、软件和 harness(框架/工具集)。请随意跳过或略读任何与你无关或不感兴趣的章节。祝你阅读愉快。
Goals and motivation#
目标与动机#
As I said earlier, my main goal was to get work off my laptop into an always-on machine at home to which I can VPN from anywhere. This machine would run virtual machines (a VM per project) which will run agentic harnesses like Claude code (with an option to expand to more harnesses). Everything needs to be as isolated as possible, and easy to recreate in case of a VM killed by misbehaving. This means zero setup by hand, and as much automation as possible.
正如我之前所说,我的主要目标是将工作从笔记本电脑转移到一台在家常开的机器上,我可以从任何地方通过 VPN 连接它。这台机器将运行虚拟机(每个项目一个 VM),这些虚拟机将运行类似 Claude code 的 agentic harness(智能体框架)(也可以选择扩展到更多框架)。一切需要尽可能隔离,并且在 VM 因行为不端而被终止时易于重建。这意味着零手动设置,以及尽可能多的自动化。
On top of that, every service I use needs to be self-hosted. No dependency on public services, no subscriptions. Aside for custom harness and control, there should be no custom app, and the setup should reuse existing services as much as possible.
此外,我使用的每项服务都需要自托管。不依赖公共服务,没有订阅费用。除了自定义 harness 和控制之外,不应有自定义应用程序,并且设置应尽可能重用现有服务。
There are many various control panels for agents developed by various people. I explicitly decided against using these because they are most likely vibe-coded or VC-funded. I had the time, and my own vibe-coding abilities, to create something that works for me, rather than building my workflow around a tool by some YouTube influencer.
有许多由不同人开发的用于控制代理的控制面板。我明确决定不使用它们,因为它们很可能是 vibe-coded(氛围编码)或由风险投资资助的。我有时间,也有自己的 vibe-coding 能力,去创建适合我的东西,而不是围绕某个 YouTube 网红打造的工具来构建我的工作流。
Hardware and virtualization#
硬件与虚拟化#
At the core of everything lays a refurbished Dell Optiplex Micro 7010 with 13th Gen Intel Core i5-13500T, and 32 GB of RAM. I tried to find i7, but couldn’t find any in the Micro chassis. And 32GB already costed me a hefty amount, so 64GB will have to wait for some time in the future. I wanted a Micro chassis because it’s small, quiet, and low on power consumption. It runs Proxmox as base OS. Since I already owned a Lenovo ThinkCentre Tiny, which also runs Proxmox, I opted for Proxmox datacenter manager to manage both hosts with pulse to monitor both.
一切的核心是一台翻新过的 Dell Optiplex Micro 7010,配备第 13 代 Intel Core i5-13500T 处理器和 32 GB 内存。我试图寻找 i7 版本,但在 Micro 机箱中找不到。而且 32GB 已经花了我一大笔钱,所以 64GB 只能等到未来再考虑了。我想要 Micro 机箱是因为它体积小、安静且功耗低。它以 Proxmox 作为基础操作系统。由于我已经拥有一台同样运行 Proxmox 的 Lenovo ThinkCentre Tiny,因此我选择使用 Proxmox datacenter manager 来管理这两台主机,并使用 pulse 来监控它们。
Networking#
网络#
I had a long debate with myself whether I should put everything in one VLAN or separate services from dev machines, and ultimately decided on the latter. Every supporting service (git, CI/CD, and others; more on them in the next section) run in their own VLAN which is separated from the VLAN where dev machines run. There are tiny holes that allow dev machines to speak to the needed services, but ultimately, a rogue AI agent can’t scan the entire network and find management ports for my CI/CD services or something else. The worst a compromised VM/Agent can do is list all other VMs in its own VLAN, and maybe access the dev web version of whatever the dev machine is running. This complicated things a bit, especially around DNS and a bunch of holes in VLAN ACLs, but it is a cleaner separation.
我曾长时间纠结于是否应该将所有内容放在同一个 VLAN 中,还是将服务与开发机器分开,最终决定采用后者。每个支持服务(git、CI/CD 以及其他服务;下文将详细介绍)都运行在独立的 VLAN 中,该 VLAN 与运行开发机器的 VLAN 隔离开来。存在一些微小的漏洞,允许开发机器访问所需的服务,但归根结底,恶意的 AI 代理无法扫描整个网络并找到我的 CI/CD 服务或其他服务的管理端口。最坏的情况下,被攻陷的 VM/Agent 只能列出其所在 VLAN 中的所有其他 VM,并可能访问开发机器正在运行的任何内容的开发版 Web 界面。这在一定程度上增加了复杂性,特别是在 DNS 和 VLAN ACL 中的一系列漏洞方面,但这种分离更加清晰。
For your convenience, going further, I will call the VLAN that hosts services srv and the VLAN that runs the dev machines dev.
为了方便起见,在接下来的内容中,我将把托管服务的 VLAN 称为 srv,将运行开发机器的 VLAN 称为 dev。
Services#
服务#
DNS and DHCP#
DNS 和 DHCP#
First, I bought a domain. This allows me to get SSL certificates, and rids me of the necessity to remember a bunch of IPs.
首先,我购买了一个域名。这让我能够获取 SSL 证书,并且免去了记住一堆 IP 地址的麻烦。
Then, inside srv VLAN I run Technitium which is a light DNS and DHCP server. Technitium is responsible for resolving internal domains, as well as creating a “Magic DNS” for each new dev machine. I wanted every new dev machine with a host name of host to get a domain under *.dev.[domain].net so it becomes [host].dev.[domain].net. Technitium is set up as the DHCP server for the dev VLAN. When a dev machine boots, it advertises its host name to Technitium, and it creates a DNS record for it.
然后,在 srv VLAN 内部,我运行 Technitium,这是一个轻量级的 DNS 和 DHCP 服务器。Technitium 负责解析内部域名,并为每台新的开发机器创建“Magic DNS”。我希望每台主机名为 host 的新开发机器都能获得 *.dev.[domain].net 下的域名,从而变成 [host].dev.[domain].net。Technitium 被设置为 dev VLAN 的 DHCP 服务器。当开发机器启动时,它会向 Technitium 广播其主机名,Technitium 会为其创建一条 DNS 记录。
However, there is one problem with this approach. DHCP requests are broadcasts, and broadcasts can’t cross VLAN boundary. This is usually solved by a built-in DHCP relay on the Router, by mine does not have it, so I had to run a small LXC in dev to act as a relay. dnsmasq acts as the DHCP relay between dev and srv.
然而,这种方法存在一个问题。DHCP 请求是广播包,而广播包无法跨越 VLAN 边界。这通常由路由器内置的 DHCP 中继来解决,但我的路由器没有这个功能,所以我不得不在 dev 中运行一个小型 LXC 作为中继。dnsmasq 充当 dev 和 srv 之间的 DHCP 中继。
Git and CI/CD#
Git 和 CI/CD#
I self-host Forgejo as my git repository and UI. I also self-host the Forgejo runner that runs the jobs. The runner actually sits in a completely different VLAN, which is completely isolated from the rest of the VLANs (except for a tiny tiny hole to reach out to Forgejo instance to register itself). The reason I did it this way is that runner runs a bunch of code that is downloaded from the internet and package repositories. I didn’t want the risk of the runner getting compromised to risk the rest of my network.
我自建了 Forgejo 作为我的 Git 仓库和 UI。我还自建了运行任务的 Forgejo runner。该 runner 实际上位于一个完全不同的 VLAN 中,该 VLAN 与其他所有 VLAN 完全隔离(仅留有一个极小的通道用于连接至 Forgejo 实例以完成注册)。我采用这种方式的原因是,runner 会执行从互联网和软件包仓库下载的大量代码。我不希望 runner 被攻破的风险波及到我的其余网络。
In Forgejo I use Forgejo actions (which are like GitHub Actions) in order to check my code. I used to run deploys via Forgejo actions as well, but this meant that Forgejo secrets had private SSH keys to production servers. main branch is always protected, and deploys happen only from main (after I review and approve), but an agent running in a dev VM could still create a workflow that dumps all the secrets (including private SSH keys), and then forward it to whoever instructed it to do so. For this reason, I had to move deploys to Semaphore UI. Semaphore holds the private keys, and it only deploys using a custom deploy script, from a webhook from Forgejo run that finished in main. Since agents can’t push to main, and everything that goes into main is reviewed and approved by me, agents do not have access to secrets in Semaphore. They can still slip a poisoned code that gets built into an artifact and deployed, but oh well… The world is far from perfect.
在 Forgejo 中,我使用 Forgejo actions(类似于 GitHub Actions)来检查我的代码。过去我也通过 Forgejo actions 进行部署,但这意味着 Forgejo 的 secrets 中包含了生产服务器的私有 SSH 密钥。main 分支始终受到保护,且部署仅在 main 分支上进行(在我审查并批准之后),但在开发 VM 中运行的代理仍可能创建一个工作流,将所有 secrets(包括私有 SSH 密钥)转储出来,然后转发给指示它这样做的人。出于这个原因,我不得不将部署迁移到 Semaphore UI。Semaphore 持有这些私钥,并且仅通过自定义部署脚本进行部署,该脚本由 Forgejo 在 main 分支上完成运行的 webhook 触发。由于代理无法推送到 main 分支,且所有进入 main 分支的内容都经过我的审查和批准,因此代理无法访问 Semaphore 中的 secrets。它们仍然可以植入恶意代码,将其构建为制品并进行部署,但唉……世界远非完美。
Secrets and environment variables#
Secrets 和环境变量#
Last, but not least, are API keys and anything else that the agent should not have access to. I opted to running agent-vault by Infisical to act as a proxy that hold the API keys and other secrets, so agents never have access to them. During the initial set up (more on that later), I create and populate a vault for each dev VM with the relevant secrets, and set up the CA certificates on the machine, so everything will trust the proxy. Standalone agent-vault is no longer the recommended way to use it, and Infisical are pushing you towards using their platform. I think my next step will be to ditch agent-vault in favor of a wrapper around mtimproxy.
最后但同样重要的是 API 密钥以及任何代理不应访问的其他内容。我选择运行由 Infisical 提供的 agent-vault 作为代理,以持有 API 密钥和其他 secrets,从而确保代理永远无法直接访问它们。在初始设置阶段(稍后详述),我为每个开发 VM 创建并填充包含相关 secrets 的 vault,并在机器上配置 CA 证书,以便所有组件都能信任该代理。独立的 agent-vault 已不再是推荐的使用方式,Infisical 正引导用户使用其平台。我认为我下一步的做法将是弃用 agent-vault,转而使用围绕 mtimproxy 构建的包装器。
It’s important to note that agent-vault is its own LXC container, in a srv VLAN. This means that agents can’t bypass it. They can decide to ignore it, but the requests would simply fail as the dev VM never holds any API keys or secrets.
需要注意的是,agent-vault 是一个独立的 LXC 容器,位于 srv VLAN 中。这意味着代理无法绕过它。它们可以选择忽略它,但由于开发 VM 本身不持有任何 API 密钥或 secrets,请求将会直接失败。
Control center and VM provisioning#
控制中心和 VM 供应#
As I said earlier, VMs are some-what disposable. If they get corrupted or hijacked, I should be able to just destroy them and re-create with as little hassle as possible. This means their creation needs to be configured in code.
正如我之前所说,虚拟机是某种程度上的可销毁资源。如果它们被损坏或劫持,我应该能够直接销毁它们并尽可能少地麻烦地重新创建。这意味着它们的创建需要在代码中进行配置。
更进一步:量化金融体系
看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力