跳到正文
Skillpedia
通用编程7.8k · #13030

working-directory-resolution

它能做什么

这项技能展示智能体如何避免因在错误的文件夹中运行命令而导致的文件错误。适合文件实际存在,但智能体却报告找不到的人。

适用场景

  • ✓当文件读取因找不到文件或没有此文件的错误而失败,而该文件其实存在的时候。
  • ✓当沙箱找不到任务中提到的文件时。
  • ✓当 shell 命令报告一个本应存在的文件缺失的时候。

技能链接

https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/working-directory-resolution/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/working-directory-resolution/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“智能体说我的 config.json 不存在,但我能在工作区里看到它。请修复文件夹问题并读取该文件。”

每周 5 个新技能,1 分钟读完。

工作原理

智能体会在每条 shell 命令前加上 cd 到目标文件夹,使每条命令都在正确的位置运行。这样就消除了工作文件夹与文件所在位置之间的不匹配。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Working Directory Resolution Pattern

Many file operation failures occur because the tool's current working directory doesn't match where the target files are located. This skill provides a reliable pattern for resolving such issues.

Problem Symptoms

File operations fail with errors like:

  • "File not found" or "No such file or directory"
  • "Permission denied" (when path is actually relative)
  • Tools returning empty results for files that should exist

These failures often occur even when files are present in the workspace—the working directory context is simply wrong.

Solution

Always explicitly set the working directory before file operations by prepending cd to your shell commands.

Implementation Pattern

Basic Syntax

cd /path/to/target/directory && your-command-here

With run_shell

run_shell(command="cd /workspace/project && cat config.json")

Multiple Operations in Same Directory

cd /workspace/project && ls -la && cat README.md && python script.py

Conditional Directory Change

cd /workspace/project 2>/dev/null && cat file.txt || echo "Directory not found"

When to Apply

Use this pattern when:

  • read_file fails to locate an existing file
  • execute_code_sandbox can't find referenced files
  • Shell commands report missing files that should exist
  • File paths work in some contexts but not others
  • You're unsure of the tool's current working directory

Best Practices

  1. Use absolute paths when possible

    cd /workspace/project/src && python main.py
    
  2. Combine related operations to avoid repeated cd calls

    cd /workspace/project && ./build.sh && ./test.sh
    
  3. Verify directory exists before operations

    [ -d /workspace/project ] && cd /workspace/project && ls
    
  4. For scripts, set working directory at the start

    #!/bin/bash
    cd "$(dirname "$0")" || exit 1
    # Rest of script runs from script's directory
    

Common Pitfalls

Issue Wrong Approach Correct Approach
Relative paths cat config.json cd /workspace && cat config.json
Assumed cwd python script.py cd /workspace && python script.py
Multiple dirs cd dir1; cd dir2; cmd cd dir1/dir2 && cmd

Fallback Strategy

If cd also fails:

  1. Use pwd to check current directory
  2. Use find or ls -R to locate files
  3. Verify the path structure with ls -la /path/to/check
pwd && find /workspace -name "target_file.txt" 2>/dev/null

This pattern is especially valuable when tools like execute_code_sandbox or read_file fail but run_shell with explicit directory context succeeds.

常见问题

为什么这能修复找不到文件的错误?

这些错误大多是因为命令在与文件不同的文件夹中运行而造成的。

我可以在同一个文件夹中运行多条命令吗?

可以,技能展示了在一次 cd 之后连接多条命令,使它们都在该文件夹中运行。

如果文件夹不存在怎么办?

技能展示了一种条件写法,在找不到该文件夹时打印一条消息。

详情

作者的原始描述

Resolve file access failures by changing to the correct working directory before command execution

repo: HKUDS/AgentPilot

热度

GitHub 星标
7.8k
排名
#13030
许可证
MIT
更新时间
2026/11/1

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能