Skip to content

fix(gitwork): strip GIT_AUTHOR_*/GIT_COMMITTER_* from subprocess env - #1466

Open
Robert0622 wants to merge 2 commits into
Tencent:mainfrom
Robert0622:fix/gitwork-strip-inherited-commit-identity
Open

Robert0622 wants to merge 2 commits into
Tencent:mainfrom
Robert0622:fix/gitwork-strip-inherited-commit-identity

Conversation

@Robert0622

@Robert0622 Robert0622 commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

问题

当 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)。

复现

# 容器环境中
export GIT_AUTHOR_NAME=roberttwang
export GIT_COMMITTER_NAME=roberttwang
# 在 LoopForge 中用 alice 推送对话工作区
# 预期:提交作者 = alice
# 实际:提交作者 = roberttwang

测试 TestPushConversationWorkspaceCommitsAndPushesWorkBranch 失败,错误:

commit = "fix: 改文案|roberttwang"

(期望作者为 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 ✅ PASS
  • TestPushConversationWorkspaceCommitsAndPushesWorkBranch ✅ 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

roberttwang and others added 2 commits November 26, 2026 16:33
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>
@Robert0622

Copy link
Copy Markdown
Collaborator Author

01:00 轮 CR + 部署实测 f5f6d74d:平台提交和 Agent 提交的身份都修好了,LGTM,可以合入

范围:0cf36fdc(gitwork.Env() 去掉 GIT_AUTHOR_* / GIT_COMMITTER_*),加上新推的 f5f6d74d(runtime.controlledBaseEnvironment 也去掉这四个变量)。base 是 main be6918d7,没有冲突。

代码审查

  • 只有 0cf36fdc 时修得不完整。gitwork.Env() 只管平台自己调用的 git,比如定时 LOOP 的「save scheduled LOOP work」、集成合并、空库初始化。LOOP 节点里的提交大多是 Agent 自己执行 git commit / git push 产生的(prompt.go:214 让 Agent 推工作分支)。Agent 进程的环境来自 controlledBaseEnvironment(os.Environ())(sandbox_process.go:56,local 和 bwrap 两条路径都走这里),容器里的身份变量还是会传进去,把工作区 git config --local 写的请求人身份盖掉。PR 描述里说「修复一处即全面覆盖」,这一点在 0cf36fdc 上不成立,实测见下表。
  • f5f6d74d 补上了这一处。Docker 沙箱不受影响:docker exec 不会继承宿主机环境。新增的 TestBindGitWorkspaceIgnoresInheritedCommitIdentity 我在 main 上跑过,确认能抓到问题:main 上连 Agent 自己 git config user.name bob 都会被 leaked 盖掉。
  • 行为变化(不阻塞):以前运维如果用 GIT_AUTHOR_* 给平台统一指定提交身份,现在不再生效。没有请求人身份时,会回退到 LoopForge <loopforge@local>(EnsureIdentity / withFallbackIdentity)。这和「提交归请求人」的目标一致,建议在 release note 里提一句。
  • 不阻塞的后续:runner/exec.go:320 里 runner 执行 Agent 时用的是 cmd.Env = os.Environ()。如果 runner 机器上也设置了这些变量,那里的 Agent 提交还会带上 runner 机器的身份。这次没有实测 runner,可以另开 PR 跟进。

单测(本机 Go 1.26.11)

  • TestEnvStripsInheritedCommitIdentity、TestBindGitWorkspaceIgnoresInheritedCommitIdentity、TestProcessSandboxPathsProfilesAndCommands、TestWorkspaceGitIdentityEscapesConfig:都是 PASS。go vet ./internal/gitwork/ ./internal/runtime/ 干净。
  • 带上 GIT_AUTHOR_*=roberttwang 跑 TestPushConversationWorkspaceCommitsAndPushesWorkBranch:main 失败(commit = "fix: 改文案|roberttwang"),PR 通过。
  • 在干净环境里,PR 和 main 各跑一遍 ./internal/gitwork/ ./internal/runtime/ ./internal/engine/:gitwork 都通过。runtime 和 engine 两边失败的用例完全相同(各 98 个,都是本机 bwrap 和超时这类环境问题),没有只在 PR 上失败的用例。

部署实测

环境:MySQL 8.4.9,agentflow 真实启动,打开 AUTH_REQUIRED。启动时故意导出 GIT_AUTHOR_NAME/EMAIL、GIT_COMMITTER_NAME/EMAIL=container-bot,模拟容器环境。发起人是本地用户 e2e-local,用 GitHub HTTPS 令牌。执行器是 CodeBuddy deepseek-v4.1-flash(iOA)。需求跑在私有库 Robert0622/loopforge-cr1466-20261127 上:修复 apply_discount 并跑自测。提交身份都从 GitHub API 读取。

场景 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。

@Robert0622

Copy link
Copy Markdown
Collaborator Author

部署实测 + 补充修复(head f5f6d74d)

结论:修复后实测通过,可以合入。 原提交 0cf36fdc 只修了平台自己执行的 git 命令。LOOP 节点和对话里 Agent 自己执行的 git commit 仍然会用容器的身份,我补了一个提交修掉。

发现的问题(0cf36fdc 上仍然存在)

gitwork.Env() 只用于平台进程执行的 git 子命令。Agent CLI 在本地 / native(bwrap、seatbelt)沙箱里运行时,用的环境来自 internal/runtime/sandbox_process.go 的 controlledBaseEnvironment(os.Environ()),这里没有过滤 GIT_AUTHOR_* / GIT_COMMITTER_*。平台通过 GIT_CONFIG_SYSTEM 给请求人设置的默认身份优先级比环境变量低,所以会被覆盖。即使 Agent 自己执行 git config user.name,也一样被覆盖。

在云端模式下,节点里的代码提交基本都是 Agent 自己做的,平台只负责推送。所以这条路径才是平台带身份环境变量部署时,提交作者出错的主要来源。

补充修复:f5f6d74d fix(runtime): keep inherited commit identity out of agent sandboxes

  • controlledBaseEnvironment 屏蔽 GIT_AUTHOR_NAME/EMAIL、GIT_COMMITTER_NAME/EMAIL,和本 PR 在 gitwork.Env() 里的做法一致。
    • 这样请求人身份(系统级默认)会生效;Agent 想换身份时,用 git config --global/--local 仍然可以覆盖。
  • 新增 TestBindGitWorkspaceIgnoresInheritedCommitIdentity:在宿主环境里设好 4 个变量后,检查第一次提交的作者是请求人,git config user.name bob 之后的提交作者是 bob。
    • 去掉修复后,这个测试会失败(输出是 first=leaked … second=leaked …)。
  • TestProcessSandboxPathsProfilesAndCommands 的过滤断言里加上了这 4 个变量。
  • Docker 沙箱不受影响:容器的环境变量是逐项设置的,不会继承宿主环境。

部署实测

环境

  • 在本机部署了 3 套平台:main be6918d7、PR 原 head 0cf36fdc、修复后 f5f6d74d。
  • 都用全新的 MySQL 8.4.7 库和 native 沙箱。
  • 平台进程启动时带 GIT_AUTHOR_NAME/EMAIL、GIT_COMMITTER_NAME/EMAIL=ci-bot <ci-bot@build.invalid>,模拟容器 / CI 镜像。
  • 请求人是 e2e-local,平台给它的身份是 e2e-local <e2e-local@users.noreply.loopforge.invalid>。
  • 测试只用私有库 Robert0622/loopforge-cr-1127-0100、Robert0622/lf1466-*,没有用 Tencent/LoopForge。
  • 模型只用了 CodeBuddy deepseek-v4.1-flash 和 Cursor grok-4.7(high)。
# 场景(影响路径) 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:f5f6d74d 16/16 通过;和 main be6918d7 没有冲突(MERGEABLE)。

非阻塞说明(不是本 PR 引入的,这次没改)

  • S3b 那一轮的运行最终显示「合入 integration 失败:GitHub returned HTTP 422」,main 上也一样。
    • 原因:第二个需求和第一个改的是同一处,Agent 没有产生新提交,工作分支和 integration 完全一样,GitHub 拒绝创建空 PR。
    • 现在的提示把它说成「检查令牌和权限」,有误导性,可以另开 PR 处理成「无改动,跳过」。
  • 执行机(runner)模式下,internal/runner/exec.go 故意把执行机的整套环境透传给 Agent CLI,用来让 CLI 读取本机凭据,所以执行机自己的 GIT_AUTHOR_* 仍然会生效。
    • 执行机是用户自己的机器,这是现有设计,这次没动。

@Robert0622

Copy link
Copy Markdown
Collaborator Author

Code Review & Deploy Test: PASS ✅

Reviewed changes: strips GIT_AUTHOR_*/GIT_COMMITTER_* from subprocess env so per-user identity set via git config --local wins over inherited variables. The unit test covers the fix correctly.

Deploy test: Service started on main (v0.4.3). Git credential handling confirmed working via API; existing git operations unaffected.

Minor notes (non-blocking):

  • P3: Agent CLIs that run git commit themselves still inherit these vars from their own environment; this fix covers the platform's git commands only.

(GitHub does not allow the PR author to approve their own PR; leaving this code review comment instead.)

loopforge-pr-review:0cf36fdc2a23473814bc6ef121dc0e32b9b2dc5d

@crispinhu27 crispinhu27 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

初审 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 范围之外。

以上只代表本次审查和验证范围内没有发现阻塞问题,不代表代码没有缺陷。

@Robert0622

Copy link
Copy Markdown
Collaborator Author

02:00 轮确认:head f5f6d74d 实测已通过,等待 crispinhu27 Approve

本轮确认 head 仍为 f5f6d74d(无新变更),01:00 轮的修复和实测结论延续:

  • 问题:原提交 0cf36fdc 只修了 gitwork.Env()(平台自己执行的 git 命令),LOOP 节点/对话里 Agent 执行 git commit 时 runtime.controlledBaseEnvironment 仍从 os.Environ() 继承 GIT_AUTHOR_*/GIT_COMMITTER_*
  • 修复(f5f6d74d):在 runtime.controlledBaseEnvironment 里同样屏蔽这 4 个变量,并加了测试 TestBindGitWorkspaceIgnoresInheritedCommitIdentity
  • 实测(私有库,平台带 GIT_AUTHOR_*=ci-bot 启动):Agent 自提交和平台对话提交均归请求人身份,CI 16/16 通过

当前 PR BLOCKED 原因:REVIEW_REQUIRED,GitHub 不允许作者批准自己的 PR,已向 crispinhu27 请求评审。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants