fix: Improve the coder agent's usage of tools
This commit is contained in:
@@ -62,22 +62,9 @@ instructions: |
|
|||||||
3. Implement each, calling `todo__done` immediately after
|
3. Implement each, calling `todo__done` immediately after
|
||||||
|
|
||||||
## Writing Code
|
## Writing Code
|
||||||
|
1. **Use fs_patch for surgical edits** - `fs_patch --path "src/main.rs" --contents "<diff>"` applies targeted changes without rewriting the whole file
|
||||||
**CRITICAL**: Write code using `write_file` tool, NEVER paste code in chat.
|
2. **use fs_write for full file writes** - `fs_write --path "src/main.rs" --contents "<contents"` writes the full file contents to a file at the specified path
|
||||||
|
|
||||||
Correct:
|
|
||||||
```
|
|
||||||
write_file --path "src/user.rs" --content "pub struct User { ... }"
|
|
||||||
```
|
|
||||||
|
|
||||||
Wrong:
|
|
||||||
```
|
|
||||||
Here's the implementation:
|
|
||||||
\`\`\`rust
|
|
||||||
pub struct User { ... }
|
|
||||||
\`\`\`
|
|
||||||
```
|
|
||||||
|
|
||||||
## File Reading Strategy (IMPORTANT - minimize token usage)
|
## File Reading Strategy (IMPORTANT - minimize token usage)
|
||||||
|
|
||||||
1. **Use grep to find relevant code** - `fs_grep --pattern "fn handle_request" --include "*.rs"` finds where things are
|
1. **Use grep to find relevant code** - `fs_grep --pattern "fn handle_request" --include "*.rs"` finds where things are
|
||||||
|
|||||||
@@ -14,99 +14,6 @@ _project_dir() {
|
|||||||
(cd "${dir}" 2>/dev/null && pwd) || echo "${dir}"
|
(cd "${dir}" 2>/dev/null && pwd) || echo "${dir}"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Normalize a path to be relative to project root.
|
|
||||||
# Strips the project_dir prefix if the LLM passes an absolute path.
|
|
||||||
# Usage: local rel_path; rel_path=$(_normalize_path "/abs/or/rel/path")
|
|
||||||
_normalize_path() {
|
|
||||||
local input_path="$1"
|
|
||||||
local project_dir
|
|
||||||
project_dir=$(_project_dir)
|
|
||||||
|
|
||||||
if [[ "${input_path}" == /* ]]; then
|
|
||||||
input_path="${input_path#"${project_dir}"/}"
|
|
||||||
fi
|
|
||||||
|
|
||||||
input_path="${input_path#./}"
|
|
||||||
echo "${input_path}"
|
|
||||||
}
|
|
||||||
|
|
||||||
# @cmd Read a file's contents before modifying
|
|
||||||
# @option --path! Path to the file (relative to project root)
|
|
||||||
read_file() {
|
|
||||||
local file_path
|
|
||||||
# shellcheck disable=SC2154
|
|
||||||
file_path=$(_normalize_path "${argc_path}")
|
|
||||||
local project_dir
|
|
||||||
project_dir=$(_project_dir)
|
|
||||||
local full_path="${project_dir}/${file_path}"
|
|
||||||
|
|
||||||
if [[ ! -f "${full_path}" ]]; then
|
|
||||||
warn "File not found: ${file_path}" >> "$LLM_OUTPUT"
|
|
||||||
return 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
{
|
|
||||||
info "Reading: ${file_path}"
|
|
||||||
echo ""
|
|
||||||
cat "${full_path}"
|
|
||||||
} >> "$LLM_OUTPUT"
|
|
||||||
}
|
|
||||||
|
|
||||||
# @cmd Write complete file contents
|
|
||||||
# @option --path! Path for the file (relative to project root)
|
|
||||||
# @option --content! Complete file contents to write
|
|
||||||
write_file() {
|
|
||||||
local file_path
|
|
||||||
file_path=$(_normalize_path "${argc_path}")
|
|
||||||
# shellcheck disable=SC2154
|
|
||||||
local content="${argc_content}"
|
|
||||||
local project_dir
|
|
||||||
project_dir=$(_project_dir)
|
|
||||||
local full_path="${project_dir}/${file_path}"
|
|
||||||
|
|
||||||
mkdir -p "$(dirname "${full_path}")"
|
|
||||||
printf '%s' "${content}" > "${full_path}"
|
|
||||||
|
|
||||||
green "Wrote: ${file_path}" >> "$LLM_OUTPUT"
|
|
||||||
}
|
|
||||||
|
|
||||||
# @cmd Find files similar to a given path (for pattern matching)
|
|
||||||
# @option --path! Path to find similar files for
|
|
||||||
find_similar_files() {
|
|
||||||
local file_path
|
|
||||||
file_path=$(_normalize_path "${argc_path}")
|
|
||||||
local project_dir
|
|
||||||
project_dir=$(_project_dir)
|
|
||||||
|
|
||||||
local ext="${file_path##*.}"
|
|
||||||
local dir
|
|
||||||
dir=$(dirname "${file_path}")
|
|
||||||
|
|
||||||
info "Similar files to: ${file_path}" >> "$LLM_OUTPUT"
|
|
||||||
echo "" >> "$LLM_OUTPUT"
|
|
||||||
|
|
||||||
local results
|
|
||||||
results=$(find "${project_dir}/${dir}" -maxdepth 1 -type f -name "*.${ext}" \
|
|
||||||
! -name "$(basename "${file_path}")" \
|
|
||||||
! -name "*test*" \
|
|
||||||
! -name "*spec*" \
|
|
||||||
2>/dev/null | sed "s|^${project_dir}/||" | head -3)
|
|
||||||
|
|
||||||
if [[ -z "${results}" ]]; then
|
|
||||||
results=$(find "${project_dir}/src" -type f -name "*.${ext}" \
|
|
||||||
! -name "*test*" \
|
|
||||||
! -name "*spec*" \
|
|
||||||
-not -path '*/target/*' \
|
|
||||||
2>/dev/null | sed "s|^${project_dir}/||" | head -3)
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ -n "${results}" ]]; then
|
|
||||||
echo "${results}" >> "$LLM_OUTPUT"
|
|
||||||
else
|
|
||||||
warn "No similar files found" >> "$LLM_OUTPUT"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# @cmd Verify the project builds successfully
|
# @cmd Verify the project builds successfully
|
||||||
verify_build() {
|
verify_build() {
|
||||||
local project_dir
|
local project_dir
|
||||||
@@ -189,28 +96,3 @@ get_project_structure() {
|
|||||||
} >> "$LLM_OUTPUT"
|
} >> "$LLM_OUTPUT"
|
||||||
}
|
}
|
||||||
|
|
||||||
# @cmd Search for content in the codebase
|
|
||||||
# @option --pattern! Pattern to search for
|
|
||||||
search_code() {
|
|
||||||
# shellcheck disable=SC2154
|
|
||||||
local pattern="${argc_pattern}"
|
|
||||||
local project_dir
|
|
||||||
project_dir=$(_project_dir)
|
|
||||||
|
|
||||||
info "Searching: ${pattern}" >> "$LLM_OUTPUT"
|
|
||||||
echo "" >> "$LLM_OUTPUT"
|
|
||||||
|
|
||||||
local results
|
|
||||||
results=$(grep -rn "${pattern}" "${project_dir}" 2>/dev/null | \
|
|
||||||
grep -v '/target/' | \
|
|
||||||
grep -v '/node_modules/' | \
|
|
||||||
grep -v '/.git/' | \
|
|
||||||
sed "s|^${project_dir}/||" | \
|
|
||||||
head -20) || true
|
|
||||||
|
|
||||||
if [[ -n "${results}" ]]; then
|
|
||||||
echo "${results}" >> "$LLM_OUTPUT"
|
|
||||||
else
|
|
||||||
warn "No matches" >> "$LLM_OUTPUT"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user