rchekalov/silo
GitHub: rchekalov/silo
Silo 是一个基于 Apple Containerization 的隔离式开发工具运行器,通过在短暂微型虚拟机中执行包管理器和 AI 编程智能体,防止供应链攻击和 AI 幻觉命令导致的主机敏感数据泄露。
Stars: 4 | Forks: 0
# Silo
**基于 Apple Containers 的隔离式开发工具运行器。**
/rootfs.ext4` 中(需要项目根目录中有 `silo.toml`)。对于您想在所有项目中使用的包,请使用 `--global`:
```
# 项目本地(默认) — 只有此项目获取这些 deps
silo build node npm install
# 全局 — 在所有项目中可用
silo build --global python pip install poetry
# 项目本地构建在全局之上
# (全局包含 poetry,项目添加特定于项目的 deps)
silo build python poetry install
```
### 其他用途
```
# 安装系统包或浏览器二进制文件
silo build node npx playwright install --with-deps
# 重置自定义环境
silo build node --remove
silo build --global python --remove
```
旧的 `--` 分隔符(`silo build node -- npm install`)仍然受支持。
稍后从存储的脚本重新构建:
```
silo rebuild node # Re-run stored setup
silo rebuild --all # Rebuild all customized tools
silo rebuild python --setup "pip install -U pip" # Override the script
```
## Shim 管理
在运行时为已安装的工具添加或移除 shim,无需重新安装:
```
# 添加 shim(创建指向 python 工具的 ~/.silo/bin/ipython)
silo shim python add ipython
# 自定义映射 — 宿主机命令运行不同的容器命令
silo shim python add ipython:python -m IPython
# 一次添加多个 shim
silo shim python add ipython black mypy
# 移除 shim
silo shim python remove ipython
# 列出工具的所有 shim
silo shim python list
```
如果某个 shim 名称已被其他工具占用,Silo 会检测到冲突。
## IDE 集成(实验性)
Silo 可以在沙箱内运行语言服务器(LSP),这样您的编辑器就能获得自动补全、转到定义和错误检查功能——同时代码分析保持隔离。LSP 服务器在与其工具相同的沙箱环境中运行,因此它能看到完全相同的包和依赖项。
支持的语言服务器:
| 工具 | 语言服务器 | 安装 |
|---|---|---|
| `python` | Pyright | `pip install pyright` (自动) |
| `node` | TypeScript Language Server | `npm install -g typescript-language-server` (自动) |
| `rust` | rust-analyzer | `rustup component add rust-analyzer` (自动) |
| `go` | gopls | `go install golang.org/x/tools/gopls@latest` (自动) |
启动语言服务器(首次使用时会自动安装 LSP 服务器):
```
silo lsp python # starts pyright, communicates via stdio
silo lsp node # starts typescript-language-server
silo lsp rust # starts rust-analyzer
silo lsp go # starts gopls
```
生成 IDE 配置,将您的编辑器指向 silo 的 LSP:
```
silo ide vscode # creates .vscode/settings.json
silo ide zed # creates .zed/settings.json
silo ide neovim # generates LSP client config
```
Silo 透明地在主机和容器之间重写文件路径,因此您的编辑器可以使用真实的文件路径,而语言服务器在沙箱内运行。
## 缓存管理和磁盘使用
Silo 缓存解压后的 rootfs ext4 文件,以便后续的 `silo run` 调用在约 600 毫秒内而不是约 25 秒内启动。在 APFS 上,缓存是稀疏的(一个“2 GB”的文件在磁盘上占用约 200 MB),但随着时间的推移它仍然会累积。磁盘相关命令如下:
### 查看磁盘使用情况
```
silo cache report # total ~/.silo footprint by bucket
silo cache list # per-entry view (raw, zstd, or both) with last-used times
```
示例:
```
~/.silo disk usage (on-disk / apparent):
rootfs cache 37.0 MiB / 36.2 MiB (0 raw, 1 zstd, 0 both)
per-tool caches 78.5 MiB
containers (live) 0.0 MiB
OCI image store 280.0 MiB
orphan blobs: 45.0 MiB (run `silo cache gc --images` to free)
builds 202.4 MiB
--
total 597.9 MiB
```
### 垃圾回收(LRU + 基于时间的策略)
```
silo cache gc # evict oldest rootfs entries until under the size cap
silo cache gc --dry-run # preview
silo cache gc --images # also GC unreferenced OCI layer blobs
silo cache gc --tool-caches # also evict old files from pip/npm/cargo caches
silo cache gc --max-size 2048 # override the policy for one run
silo cache gc --max-age 30 # only evict entries untouched for >30 days
```
GC 会在每个进程的 `silo run`时自动运行一次——您只需正常使用 silo 即可被动回收磁盘。冷条目会一直存在,直到缓存超过策略上限(默认 8 GiB)或达到时间截止点(默认 60 天)。
### 压缩冷条目 (zstd)
```
silo cache compress # compress entries last used >14 days ago (~4× smaller)
silo cache compress --all # compress every entry regardless of age
silo cache compress --dry-run # preview
```
压缩后的条目在下次缓存命中时会被解压回原始状态(对于一个 500 MB 的镜像,大约需要 1-3 秒的一次性成本),然后正常克隆。我的 debian-slim 镜像在使用默认 zstd 的情况下,从 155 MB 减小到了 37 MB。
### 配置策略
在 `silo.toml` 或 `~/.silo/silo.toml` 中放置一个 `[cache]` 块:
```
[cache.rootfs]
maxSizeMB = 8192 # LRU eviction above this cap
maxAgeDays = 60 # entries untouched beyond this are evicted
[cache.tools]
maxSizeMB = 4096 # per-mount cap for pip/npm/cargo
maxAgeDays = 30 # file-level eviction by atime inside each mount
[cache.tools.perMount]
"rust/cargo" = 8192 # override for specific mounts
```
### 完全清理
```
# 移除特定的 buckets
silo cache clean --safe # only orphans (not referenced by installed tools)
silo cache clean --rootfs # nuke rootfs cache
silo cache clean --containers # nuke stale container state
silo cache clean # wipe rootfs cache + container state
# 项目范围的回收(向上查找 silo.toml)
silo clean # rootfs cache + per-tool caches + stale VMs for this project
silo clean --rootfs-only
silo clean --caches-only
silo clean --force # also remove artifacts shared with other tools
# 卸载工具也会回收其 rootfs cache + OCI 镜像(如果不共享)
silo uninstall python # use --keep-image to opt out
# 完全重置(下次安装重新引导)
silo reset
```
## 工作原理
```
User runs: python script.py
│
▼
Shim (~/.silo/bin/python)
│ calls: silo run --shim python python script.py
▼
silo run
│ 1. Loads ~/.silo/config.toml (tool definition)
│ 2. Finds silo.toml (walks up from cwd)
│ 3. Merges config (project overrides global)
▼
Apple Container (ephemeral micro-VM)
┌─────────────────────────────────────┐
│ /workspace ← project dir (rw) │
│ /root/.cache/pip ← cache (rw) │
│ │
│ $GITHUB_TOKEN ← if silo.toml │
│ allows it │
│ │
│ No access to: ~/.ssh, ~/.aws, │
│ ~/Library, keychain, other projects│
└─────────────────────────────────────┘
│
▼
VM destroyed after exit (ephemeral mode)
```
## 命令参考
| 命令 | 描述 |
|---|---|
| `silo install ` | 从 registry 或自定义镜像安装工具 |
| `silo uninstall ` | 移除已安装的工具及其 shim |
| `silo run [args...]` | 在短暂的沙箱中运行命令 |
| `silo shell ` | 沙箱内的交互式 Shell |
| `silo list [--available]` | 显示已安装或所有可用的工具 |
| `silo init` | 自动检测项目并生成 `silo.toml`(实验性) |
| `silo build ` | 自定义工具的虚拟机并持久化更改 |
| `silo rebuild []` | 重新运行存储的设置脚本 |
| `silo shim ` | 为工具添加、移除或列出 shim |
| `silo lsp ` | 启动沙箱化的语言服务器 |
| `silo ide ` | 为 LSP 生成 IDE 配置 |
| `silo status` | 显示正在运行的虚拟机的状态 |
| `silo cache report` | 按类别汇总磁盘使用情况 |
| `silo cache list` | rootfs 缓存的逐条目视图(原始 / zstd / 两者) |
| `silo cache gc` | LRU + 基于时间的驱逐(也支持 `--images`、`--tool-caches`) |
| `silo cache compress` | 使用 zstd 压缩冷 rootfs 条目(约缩小 4 倍) |
| `silo cache clean` | 强制清理 rootfs 缓存 / 容器 / 孤立文件 |
| `silo clean` | 项目范围的回收(遵循 `silo.toml`) |
| `silo pull` | 使环境与 `silo.toml` 保持一致 |
| `silo reset` | 移除所有 silo 数据 |
## 首次运行引导
首次 `silo install` 会引导安装容器运行时(一次性,约 5 分钟):
1. **Linux 内核** — 从 [Kata Containers](https://github.com/kata-containers/kata-containers) 发布版下载(约 280MB)
2. **Swift 工具链** — 安装 [swiftly](https://www.swift.org/swiftly/) 和 Swift 6.3 快照以进行交叉编译
3. **静态 Linux SDK** — 交叉编译 vminitd for Linux 所需(约 290MB)
4. **vminitd** — 从 [containerization](https://github.com/apple/containerization) 源代码交叉编译
5. **initfs** — 包含 vminitd 的 ext4 文件系统镜像
所有构建产物都缓存在 `~/.silo/` 中。后续的工具安装只需拉取 OCI 镜像(几秒钟,而不是几分钟)。
使用 `--timing` 查看时间花费在哪里:
```
$ silo run --timing python --version
[silo] config loaded: 5ms
[silo] runtime ready: 6ms
[silo] ephemeral completed: 1200ms
[silo] total: 1211ms
```
## 已知限制
- **仅限 macOS 26+ 和 Apple Silicon** — 需要 Apple Containerization 框架
- **首次安装需要约 5 分钟** — 下载内核、Swift 工具链,交叉编译 vminitd(一次性)
- **无热启动虚拟机持久性** — 每个 `silo run` 都是一个全新的进程;计划推出后台守护进程
- **LSP/IDE 集成是实验性的** — 可能需要手动配置编辑器
## 架构
Silo 构建在 [Apple Containerization](https://github.com/apple/containerization) Swift 框架之上。每个容器都是一个拥有自己 Linux 内核的轻量级虚拟机——而不是像 Docker 那样的进程命名空间。这提供了更强的隔离性:
- 独立的内核(macOS 主机上的 Linux 客户机)
- 除了显式挂载之外没有共享文件系统
- 没有共享进程命名空间
- 亚秒级启动时间
- 接近零的空闲开销
## 许可证
Apache License 2.0 — 详见 [LICENSE](LICENSE)。
标签:AI编程助手, Apple Containers, Apple Silicon, EVTX分析, JSONLines, Linux虚拟机, NIDS, npm安全, pip安全, RAT防护, 临时环境, 代码执行安全, 企业安全, 凭证保护, 包管理器安全, 安全渗透, 容器化, 开发安全, 开发环境隔离, 微虚拟机, 日志审计, 沙盒, 环境变量隔离, 网络资产管理, 防横向移动, 防止恶意代码, 隔离执行, 零信任