Skip to content

fix(tools): report missing working directory instead of fork/exec shell ENOENT (#457) - #458

Merged
CjiW merged 3 commits into
masterfrom
agent/peer/fix-shell-cwd/fix-shell-lost-working-dir-457
Oct 24, 2026
Merged

CjiW merged 3 commits into
masterfrom
agent/peer/fix-shell-cwd/fix-shell-lost-working-dir-457

Conversation

@CjiW

@CjiW CjiW commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

问题

Closes #457.

会话 cwd 被删除/移走(开发机迁移、git worktree 清理、临时目录回收)后,所有 Shell 调用都报:

sandbox exec: fork/exec /usr/bin/zsh: no such file or directory

把原因指向 shell 二进制,真实原因是 cmd.Dir 已不存在;agent 无法通过 Shell 自恢复,只能先 Cd 到存在目录。

根因

os/exec 在子进程里做 chdir,失败时返回 *os.PathError{Op:"fork/exec", Path:<shell>, Err:ENOENT} —— Path 是 shell 路径,cwd 完全不出现。本地 none sandbox 在 internal/cmdbuilder/cmdbuilder.go 直接 cmd.Dir = dir,没有存在性校验;远程 runner 反而有 resolveWorkDir 兜底,两端行为不一致。

修复

  • internal/cmdbuilder/cmdbuilder.go:赋值 cmd.Dir 前 os.Stat 校验;缺失/非目录 ⇒ working directory %q is not accessible: ...; use the Cd tool to change to an existing directory
  • tools/none_sandbox.go setupPipes:cmd.Start() 失败且 cmd.Dir != "" 时把 cwd + 可执行文件写进错误,保留 stat 检测不到的竞态/权限类 chdir 原因
  • 本地不做静默 fallback(home/祖先目录)——在错误目录里跑 git/rm 比明确失败危险;Cd 是设计好的恢复路径
  • docs/agent/gotchas-tools.md 记录该 gotcha

验证

新增 tools/none_sandbox_missing_dir_test.go(修复前红灯,报 fork/exec /bin/sh: ...):

  • Exec 缺失目录 ⇒ 明确 cwd 错误 + Cd 提示、不含 fork/exec
  • noneSandboxExecAsync(后台/流式路径)同上
  • Shell 工具端到端错误文案
  • 已存在目录仍正常执行(防回归)
ok  xbot/tools              22.877s
ok  xbot/internal/cmdbuilder
ok  xbot/internal/runnerclient
go build ./...              通过

…ll ENOENT

Issue #457: when a session cwd is deleted (machine migration, worktree
cleanup, tmp reclamation), every Shell call failed with

  sandbox exec: fork/exec /usr/bin/zsh: no such file or directory

os/exec performs chdir in the child, so the failure is reported against the
shell path and hides the real cause; the agent cannot recover via Shell.

- cmdbuilder.Build validates Dir before assigning cmd.Dir and returns an
  actionable error naming the directory + the Cd recovery path.
- setupPipes keeps the cwd (and executable) in Start errors so races and
  permission failures that stat cannot detect are still attributable.
- Regression tests at Exec / async / Shell-tool level; existing-dir path
  stays green.

No silent fallback to home/ancestor dirs: running git/rm in the wrong
directory is worse than an explicit failure.
@vercel

vercel Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
xbot-preview Ready Ready Preview Oct 24, 2026 7:51am UTC

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-24T07:51:57.890344Z 564a91e New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3c5b5211d7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread docs/agent/gotchas-tools.md Resolved
@CjiW

CjiW commented Oct 24, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: 2523d36aff

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@CjiW
CjiW merged commit 0f82bdf into master Oct 24, 2026
9 checks passed
@CjiW
CjiW deleted the agent/peer/fix-shell-cwd/fix-shell-lost-working-dir-457 branch October 24, 2026 16:34

This branch was successfully deployed

1 active deployment
Preview — 564a91e6 Deployed Oct 24, 2026 by vercel[bot]
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.

[Bug] Shell 工具在工作目录丢失时误报 fork/exec /usr/bin/zsh 且无法自恢复

1 participant