codesign Keychain 弹窗挂起构建
现象与根因
通过 SSH、CI 执行器或后台编程 Agent 运行 codesign 与 xcodebuild archive 时,进程常无限挂起等待系统弹窗,或直接报错 errSecInternalComponent 或 errSecInteractionNotAllowed(User interaction is not allowed)。macOS 会根据进程分区列表和图形会话锁状态限制访问 login.keychain-db 中的私钥,任何非交互式签名调用都会触发后台进程无法点击的系统授权框。
诊断与修复命令
检查当前可用的代码签名证书以及 Keychain 锁定配置:
# 列出当前 Keychain 搜索路径下有效的代码签名证书
security find-identity -v -p codesigning
# 查看 Keychain 搜索列表与自动锁定策略
security list-keychains -d user
security show-keychain-info ~/Library/Keychains/login.keychain-db若现有脚本暂时仍需调用系统 /usr/bin/codesign,需在当前会话解锁 Keychain 并设置分区白名单:
# 解锁登录 Keychain
security unlock-keychain -p "<KEYCHAIN_PASSWORD>" ~/Library/Keychains/login.keychain-db
# 允许 codesign 无弹窗访问私钥
security set-key-partition-list -S apple-tool:,apple:,codesign: -s -k "<KEYCHAIN_PASSWORD>" ~/Library/Keychains/login.keychain-db改为使用 XCLoom 在交互式 TTY 中一次导入 Development 证书,后续后台与编程 Agent 运行不再访问系统 Keychain(仅在专用发布机上导入 Distribution / Release 证书时再加 --include-distribution):
# 查看 Keychain 中可导入的签名身份
xcloom sign list-keychain --json
# 仅需在交互终端执行一次(默认仅导入 Development 证书)
xcloom sign import-keychain --team-id <TEAM_ID>
# 仅限专用发布机(附加 Apple Distribution 证书):
# xcloom sign import-keychain --team-id <TEAM_ID> --include-distributionXCLoom 如何防止复发
xcloom sign import-keychain 强制要求交互式 TTY(阻止无头 Agent 自动导出 Keychain 凭据)。默认仅导入 Development 身份,Distribution 与 Release 导入必须显式传入 --include-distribution(否则会报 XCLOOM-SIGN-DISTRIBUTION-OPT-IN-REQUIRED)。XCLoom 在排除 Time Machine 备份的 0700 团队目录内保存加密的 kc_<fp>.p12、0600 权限的 .p12.password 与公钥 kc_<fp>.cer,并在释放内存时清零敏感缓冲区。这套布局防的是磁盘残留明文 .key.pem、Time Machine 备份外泄、其他 macOS 账号跨用户读取以及无头 Agent 绕过 TTY 导出 Keychain;它不防同一 macOS 用户下的其他未沙箱化进程(同目录密码文件用于无人值守签名)。
后续 xcloom build、xcloom archive 与 xcloom preview 均在进程内直接完成 CMS 签名,完全不触发 macOS Keychain 弹窗。针对包含 27 个 bundle 的 85 MB 应用,XCLoom 自签 CMS 签名耗时 0.43s(9 次中位数),产物通过 /usr/bin/codesign --verify --deep --strict 严格校验;若证书尚未导入,工具会直接返回 XCLOOM-SIGN-KEYCHAIN-UNIMPORTED 并给出精确导入命令。
安全边界说明:由于无人值守本地签名需要在当前 macOS 用户下同时读取上述 0600 权限的 .p12 与 .p12.password,与你运行在同一 macOS 用户下的非沙箱编程 Agent 同样能访问当前用户拥有的文件。因此若你不信任 Agent 读取本地文件,建议编程 Agent 主机仅保留 Development 证书,并将 Apple Distribution 签名隔离到独立的系统用户或发布主机。
常见问题
XCLoom 生成标准的 Mach-O CodeDirectory 与 CMS 签名块,产物在本机通过 /usr/bin/codesign --verify --deep --strict 严格校验(针对含 27 个 bundle 的 85 MB 应用,自签 CMS 9 次中位数耗时 0.43s)。带 RFC 3161 TSA 时间戳的完整 Apple Distribution 签名、物理 iPhone 安装及 App Store Connect 接收情况尚未真机实测;随时可继续使用标准 codesign 或 xcodebuild 执行发布签名。
不需要。本机在交互式 TTY 中通过 xcloom sign import-keychain 一次导入(默认 Development,独立发布用户或发布主机上的 Distribution/Release 签名需加上 --include-distribution,或在 CI 上直接传入 --p12 / --cert + --key)后,无需再向 security unlock-keychain 传递登录 Keychain 密码。
该错误表示系统 Keychain 中存在匹配当前 DEVELOPMENT_TEAM 的有效证书,但尚未导入 XCLoom 凭据库。仅需 Development 签名时在交互终端执行 xcloom sign import-keychain --team-id <TEAM_ID>,用于 Distribution 与 Release 签名时加上 --include-distribution(未加该参数会返回 XCLOOM-SIGN-DISTRIBUTION-OPT-IN-REQUIRED)。
相关链接

# 通过 Homebrew 安装(或 curl -fsSL https://xcloom.dev/install.sh | sh,默认装入 ~/.local/bin)
brew install xcloom/tap/xcloom
# 只读环境与模拟器检查(不改动任何工程配置)
xcloom doctor
xcloom sim status
# 接入编程 Agent 的 MCP 与 Skill(无需 Node;默认当前仓库,加 --global 为用户全局安装)
xcloom mcp install --claude --codex --cursor
xcloom skill install --claude --codex --cursor也可以直接把下面一句话发给 Claude Code、Codex 或 Cursor,让 Agent 自动完成 CLI 安装、MCP 与 Skill 注册、环境校验和首次本地构建,并按需开启 git 合并驱动或运行当前仓库的构建对比测速:
Read https://xcloom.dev/agent-setup.md and set up XCLoom for this repository: install the CLI, register the MCP server and agent skill, verify the environment with xcloom doctor, and run a first local build with xcloom build --local --json. Optionally install the git merge drivers and run xcloom bench --repo . --json (isolated cold xcodebuild comparison; can take several minutes on large repositories).需要 macOS 及本地已安装的 Xcode SDK 与编译器。无需迁移工程格式,直接支持现有的 .xcodeproj、.xcworkspace、CocoaPods 与 XcodeGen 工程;独立 Package.swift 应用与暂不支持的构建规则会自动回退至 xcodebuild。