Repository navigation
fix(gitwork): strip GIT_AUTHOR_*/GIT_COMMITTER_* from subprocess env - #1466
Robert0622 wants to merge 2 commits into
Conversation
When LoopForge runs inside a container that has GIT_AUTHOR_NAME / GIT_COMMITTER_NAME / GIT_AUTHOR_EMAIL / GIT_COMMITTER_EMAIL set (e.g. from a CI or agent harness), those four variables are inherited through gitwork.Env() into every git subprocess the platform spawns. They take precedence over the per-workspace local config written by applyWorkspaceGitIdentity(), so all commits are attributed to the container user instead of the requesting user. Fix: extend the Env() filter list to strip those four vars, so the local 'git config user.name/user.email' values set by applyWorkspaceGitIdentity() are always the authoritative identity. Regression test TestEnvStripsInheritedCommitIdentity added. Existing test TestPushConversationWorkspaceCommitsAndPushesWorkBranch now passes without the global git config workaround. Co-Authored-By: Claude Code <noreply@anthropic.com>
The control plane strips GIT_AUTHOR_*/GIT_COMMITTER_* from its own git subprocesses, but local and process sandboxes still passed them from the host environment to the Agent CLI. Agent commits in LOOP nodes and conversations then carried the operator's identity instead of the requester's, and even an explicit `git config user.name` by the agent was ignored. Drop the four identity variables in controlledBaseEnvironment so the requester identity installed as the Git system default applies, and the agent can still override it with git config. Co-Authored-By: Claude Code <noreply@anthropic.com>
01:00 轮 CR + 部署实测
|
| 场景 | main be6918d7 |
0cf36fdc |
f5f6d74d |
|---|---|---|---|
| 定时任务派发的 LOOP,Agent 只改文件,平台提交「LoopForge: save scheduled LOOP work」,再推到工作分支和集成分支 | container-bot | e2e-local | e2e-local |
手动派发的 LOOP,节点里 Agent 自己 git commit && git push |
container-bot | container-bot | e2e-local |
三套实例的运行都是 succeeded(每次 35~70 秒)。修复会话(thr_cd02f9adaca2)在自己的私有库上另外验证了对话场景(CodeBuddy 和 Cursor grok-4.7 high)、Agent 自己设置 user.name、空库初始化,以及不带身份变量启动的情况,结论一致。
说明:一开始两套实例同一分钟派发了编号相同的需求,生成了同名的工作分支,导致 0cf36fd 实例第一次定时运行推送失败。这是测试安排撞车,和本 PR 无关,换编号重跑后已通过(表里的 4102d16d)。
CI:f5f6d74d 上除 core 还在跑外,其余 13 项都是 pass。
结论:LGTM,可以合入。token 的身份就是 PR 作者,没法点 Approve,需要一位非作者评审人点 Approve。
部署实测 + 补充修复(head
|
| # | 场景(影响路径) | main be6918d7 |
PR 0cf36fdc |
修复 f5f6d74d |
|---|---|---|---|---|
| S1 | LOOP 节点里 Agent 自己 git commit(CodeBuddy,native 沙箱)。用归档恢复后的工作区 git log 和平台 /workspace/log 核对 |
ci-bot | ci-bot ❌ | e2e-local ✅ |
| S2 | 空仓库首次派发,平台执行 Initialize repository 提交并推到 GitHub |
ci-bot ❌ | LoopForge ✅ | LoopForge ✅ |
| S3a | 定时任务(带 integration 分支):Agent 提交 → 推工作分支 → 在 GitHub 上合入 agentflow/integration。查看 GitHub 上的提交 |
ci-bot | ci-bot ❌ | e2e-local ✅ |
| S3b | 先往 main 推一个上游提交,再跑定时任务,平台把 main 对齐合并进 integration(LoopForge: merge latest main),合并提交推到 GitHub |
ci-bot ❌ | LoopForge ✅ | LoopForge ✅ |
| S4 | 对话(native 沙箱)里 Agent 执行 git init && git commit,然后看 log 作者和环境里有没有 GIT_AUTHOR_NAME:CodeBuddy deepseek-v4.1-flash |
ci-bot / 有 | ci-bot / 有 ❌ | e2e-local / 无 ✅ |
| S4' | 同 S4,换 Cursor grok-4.7 high | ci-bot / 有 | ci-bot / 有 ❌ | e2e-local / 无 ✅ |
| S6 | 对话里 Agent 先 git config user.name bob 再提交 |
– | ci-bot ❌ | bob ✅ |
| S5 | 回归:f5f6d74d 不带身份环境变量启动,跑 S1、S2、S4 |
– | – | S1 e2e-local、S2 LoopForge、S4 e2e-local ✅ |
补充说明
- S2、S3b 是平台自己做的提交,用的是
withFallbackIdentity/EnsureIdentity设置的 LoopForge 兜底身份。修复后,带不带身份环境变量,结果都和正常部署一致。 - clone、fetch、push 和 GitHub 合并在 3 套环境里都正常,令牌认证路径没有受影响。
单测 / CI
go vet ./...:main 和f5f6d74d都通过。- 拿 gitwork、runtime、engine、runner、httpapi 这 5 个包,在同一台机器上把 main 和
f5f6d74d对比着跑:f5f6d74d没有比 main 多出来的失败用例。- main 比它多一个失败:
TestPushConversationWorkspaceCommitsAndPushesWorkBranch,正是这个 PR 修的问题。 - 两边都失败的 16 个用例是本机环境问题,比如没有 Docker / Chromium、没有容器网络、bwrap 用的是替身。其中
TestPrepareRepositoryAppliesResponsibleUserGitIdentityOnCloneAndResume失败,是因为测试里的mustGit直接用了本机进程的GIT_AUTHOR_*,不经过产品代码,CI 里没有这个问题。
- CI:
f5f6d74d16/16 通过;和 mainbe6918d7没有冲突(MERGEABLE)。
非阻塞说明(不是本 PR 引入的,这次没改)
- S3b 那一轮的运行最终显示「合入 integration 失败:GitHub returned HTTP 422」,main 上也一样。
- 原因:第二个需求和第一个改的是同一处,Agent 没有产生新提交,工作分支和 integration 完全一样,GitHub 拒绝创建空 PR。
- 现在的提示把它说成「检查令牌和权限」,有误导性,可以另开 PR 处理成「无改动,跳过」。
- 执行机(runner)模式下,
internal/runner/exec.go故意把执行机的整套环境透传给 Agent CLI,用来让 CLI 读取本机凭据,所以执行机自己的GIT_AUTHOR_*仍然会生效。- 执行机是用户自己的机器,这是现有设计,这次没动。
|
Code Review & Deploy Test: PASS ✅ Reviewed changes: strips Deploy test: Service started on main (v0.4.3). Git credential handling confirmed working via API; existing git operations unaffected. Minor notes (non-blocking):
(GitHub does not allow the PR author to approve their own PR; leaving this code review comment instead.) loopforge-pr-review:0cf36fdc2a23473814bc6ef121dc0e32b9b2dc5d |
crispinhu27
left a comment
There was a problem hiding this comment.
初审 f5f6d74d:APPROVE
审查对象是当前 head f5f6d74dc3339dbf7fb11e68eaba1a3fb08c2ad7(base main be6918d7,也就是 merge-base),共 2 个提交:0cf36fdc 的 gitwork.Env() 和 f5f6d74d 的 runtime.controlledBaseEnvironment 都过滤掉了继承来的 GIT_AUTHOR_NAME/EMAIL、GIT_COMMITTER_NAME/EMAIL。
审查要点
- 两处过滤都只作用于从父进程继承的环境。
Env()之后追加的变量、-c user.*参数,以及 CommandSpec / profile 显式传入的GIT_AUTHOR_*仍然生效,我用临时探针验证过:CommandSpec 传GIT_AUTHOR_NAME=explicit,命令里拿到的就是explicit。仓库里没有调用方会主动通过继承环境传身份,gitwork.Env的调用方只有git.go:92和repository_cache.go:193。
验证(go1.26.11,隔离工作区,HOME、GOPATH 等都放在工作区内,测试数据是合成的)
go build ./...:head 和 base 都通过;go vet(gitwork、runtime、engine):head 通过。TestPushConversationWorkspaceCommitsAndPushesWorkBranch,导出GIT_AUTHOR_NAME/GIT_COMMITTER_NAME=roberttwang等变量后运行:base 失败(作者变成roberttwang,期望alice),head 通过。四个变量全部env -u掉后,head 和 base 都通过。所以这个问题只在宿主导出了提交身份时出现,和 PR 描述一致。- 把新增的
TestBindGitWorkspaceIgnoresInheritedCommitIdentity拷到 base 上,在注入身份的环境里运行:base 失败(first=leaked <leaked@example.invalid> | leaked …),head 通过;不注入时 head 也通过。 go test ./internal/gitwork/...:有无注入、head 和 base 都通过。./internal/repo:通过。./internal/runtime(9 项)、./internal/httpapi(17 项)、./internal/runner、./internal/engine的失败集合在 head 和 base 上相同,原因是缺 Docker、bwrap、Chromium、relay 等环境,不是本 PR 引入的。engine 包里两项在 head 上因整包超时被截断,单独重跑后和 base 的失败原因相同(bwrap不存在)。- CI:
f5f6d74d上 16/16 全部 success。
非阻塞说明(不是本 PR 引入的)
gitwork/shared.go的MergeWorkIntoSharedBranch没有兜底身份。在没有本地user.*的探针仓库里,base 靠继承来的环境变量碰巧能合并;head 和 clean env 下的 base 一样,都报Committer identity unknown。两个生产调用方(conversation_push.go:100、runner/work_branch.go:103)合并前都会先写本地身份,所以没能复现生产故障。以后可以考虑在这里也用withFallbackIdentity。- Docker 沙箱(
dockerSandbox.execOnce)和 runner 的executeHostCommand没有经过这两处过滤,属于本 PR 范围之外。
以上只代表本次审查和验证范围内没有发现阻塞问题,不代表代码没有缺陷。
02:00 轮确认:head
|

问题
当 LoopForge 运行在设置了
GIT_AUTHOR_NAME/GIT_COMMITTER_NAME/GIT_AUTHOR_EMAIL/GIT_COMMITTER_EMAIL的容器或 CI 环境中时,这四个环境变量会通过gitwork.Env()传入平台调度的所有 git 子进程。Git 优先级规则:环境变量 >
git config --local。因此applyWorkspaceGitIdentity()通过git config --local user.name <alice>写入的用户身份被容器全局变量覆盖,所有提交都错误地归属于容器用户(如roberttwang),而非发起请求的用户(如alice)。复现
测试
TestPushConversationWorkspaceCommitsAndPushesWorkBranch失败,错误:(期望作者为
alice)根因
gitwork/auth.go的Env()函数已过滤GIT_ASKPASS、GIT_CONFIG_*等变量,但遗漏了GIT_AUTHOR_NAME、GIT_AUTHOR_EMAIL、GIT_COMMITTER_NAME、GIT_COMMITTER_EMAIL。修复
在
Env()的过滤列表中新增四个变量,使每次 git 操作的作者身份完全由applyWorkspaceGitIdentity()写入的本地 git config 决定。影响范围
gitwork.Env()是所有 git 子命令(clone、commit、push、fetch、config 等)共用的环境构建函数,修复一处即全面覆盖。无架构变更,无新依赖,不影响 SSH 路径(SSH 分支不经过Env()写入 author 变量的路径)。测试
新增
TestEnvStripsInheritedCommitIdentity(internal/gitwork/auth_test.go):模拟容器环境设置四个变量,断言Env("")的输出中不含任何GIT_AUTHOR_*/GIT_COMMITTER_*。修复后:
TestEnvStripsInheritedCommitIdentity✅ PASSTestPushConversationWorkspaceCommitsAndPushesWorkBranch✅ PASS(之前 FAIL)go test ./internal/gitwork/...✅ 全绿go test ./internal/engine/...✅ 全绿(除已知 bwrap/Chromium 环境限制)go vet ./internal/gitwork/... ./internal/engine/...✅ 0 issues补充修复(f5f6d74d)
gitwork.Env()只用于平台自己执行的 git 命令。Agent CLI 在本地 / native 沙箱里运行时,环境来自runtime.controlledBaseEnvironment(os.Environ()),同样会继承GIT_AUTHOR_*/GIT_COMMITTER_*。这会覆盖平台通过GIT_CONFIG_SYSTEM设置的请求人身份,Agent 自己执行git config user.name也会被覆盖。云端 LOOP 节点和对话里的代码提交,基本都是 Agent 自己做的。f5f6d74d在controlledBaseEnvironment里同样屏蔽了这 4 个变量,并新增TestBindGitWorkspaceIgnoresInheritedCommitIdentity,去掉修复后这个测试会失败。部署实测结果见 PR 评论。🤖 Generated with Claude Code