diff options
| author | dev <dev@example.com> | 2026-10-04 13:28:35 +0000 |
|---|---|---|
| committer | dev <dev@example.com> | 2026-10-04 13:28:35 +0000 |
| commit | b0457c7dadc7712ba0cb09db8087b5965311dd24 (patch) | |
| tree | cd065e7bdd58370d8d6422b309b3688bc319aabd /tools/fs_patch_new.sh | |
| parent | 3263c4666ee0b2b392e3382e4c2ac53db647da89 (diff) | |
| download | llm-functions-docker-b0457c7dadc7712ba0cb09db8087b5965311dd24.tar.gz | |
fs_write_new/fs_patch_new: no user interaction, always exit 0 with errors in output
Diffstat (limited to 'tools/fs_patch_new.sh')
| -rwxr-xr-x | tools/fs_patch_new.sh | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/tools/fs_patch_new.sh b/tools/fs_patch_new.sh new file mode 100755 index 0000000..060c99a --- /dev/null +++ b/tools/fs_patch_new.sh @@ -0,0 +1,72 @@ +#!/usr/bin/env bash +set -uo pipefail + +# @describe Apply a patch to a file at the specified path. +# This can be used to edit the file, without having to rewrite the whole file. +# This tool NEVER requests user interaction and NEVER exits with a non-zero code. +# Any error is reported in the tool output so the model can react within the same cycle. +# @option --path! The path of the file to apply to +# @option --contents! The patch to apply to the file +# +# Here is an example of a patch block that can be applied to modify the file to request the user's name: +# --- a/hello.py +# +++ b/hello.py +# \@@ ... @@ +# def hello(): +# - print("Hello World") +# + name = input("What is your name? ") +# + print(f"Hello {name}") + +# @env LLM_OUTPUT=/dev/stdout The output path + +ROOT_DIR="${LLM_ROOT_DIR:-$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)}" +OUT="${LLM_OUTPUT:-/dev/stdout}" + +# Report an error in the tool output and finish successfully (exit 0). +err() { + echo "ERROR: $*" >> "$OUT" + exit 0 +} + +main() { + path="${argc_path:-}" + patch="${argc_contents:-}" + + [[ -z "$path" ]] && err "missing --path" + [[ -z "$patch" ]] && err "missing --contents" + [[ ! -e "$path" ]] && err "file not found: $path" + [[ ! -f "$path" ]] && err "not a regular file: $path" + [[ ! -r "$path" ]] && err "file not readable: $path" + + new_contents="" + if ! new_contents="$(awk -f "$ROOT_DIR/utils/patch.awk" "$path" <(printf "%s" "$patch") 2>/tmp/fs_patch_new.err)"; then + msg="$(cat /tmp/fs_patch_new.err 2>/dev/null || true)" + err "patch apply failed for $path: ${msg:-unknown error}" + fi + + # Show the diff (if any) so the model sees what is applied. + # NOTE: no guard_operation.sh here -> no user interaction. + printf "%s" "$new_contents" | git diff --no-index "$path" - >> "$OUT" 2>/dev/null || true + + if ! printf "%s" "$new_contents" > "$path" 2>/tmp/fs_patch_new_write.err; then + msg="$(cat /tmp/fs_patch_new_write.err 2>/dev/null || true)" + err "failed to write patched file $path: ${msg:-unknown error}" + fi + + echo "The patch applied to: $path" >> "$OUT" + exit 0 +} + +# Parse arguments with argc and run the tool in a subshell: +# - argc calls `main`, which always ends with `exit 0`. +# - If argc itself fails (missing/invalid arguments) its generated code prints the +# error to stderr and calls `exit 1`; running it in a subshell keeps that from +# terminating this script, and the message is captured into the tool output. +# - The script itself always finishes with exit 0. +argc_err="$(mktemp)" +( eval "$(argc --argc-eval "$0" "$@" 2>/dev/null)" ) 2>"$argc_err" +if [[ -s "$argc_err" ]]; then + { echo "ERROR: argument parsing failed:"; cat "$argc_err"; } >> "$OUT" +fi +rm -f "$argc_err" +exit 0 |
