diff options
Diffstat (limited to 'tools')
| -rwxr-xr-x | tools/fs_patch.sh | 72 | ||||
| -rwxr-xr-x | tools/fs_patch_new.sh | 72 | ||||
| -rwxr-xr-x | tools/fs_write.sh | 46 | ||||
| -rwxr-xr-x | tools/fs_write_new.sh | 64 |
4 files changed, 62 insertions, 192 deletions
diff --git a/tools/fs_patch.sh b/tools/fs_patch.sh index a87cef5..060c99a 100755 --- a/tools/fs_patch.sh +++ b/tools/fs_patch.sh @@ -2,12 +2,13 @@ set -uo pipefail # @describe Apply a patch to a file at the specified path. -# NOTE: This tool NEVER exits with non-zero (even on missing args). It reports errors in output. -# @option --path The path of the file to apply to -# @option --contents The patch to apply to the file -# @env LLM_OUTPUT=/dev/stdout The output 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 # -# Example: +# 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 # \@@ ... @@ @@ -16,9 +17,12 @@ set -uo pipefail # + 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 @@ -28,47 +32,41 @@ 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}" + [[ -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.err)"; then - msg="$(cat /tmp/fs_patch.err 2>/dev/null || true)" - err "patch apply failed for ${path}: ${msg:-unknown error}" + 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 - # Diff in Tool-Output schreiben (damit das LLM es sieht) + # 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 - # Guard kann “abbrechen” -> als ERROR ausgeben, aber exit 0 - if ! "$ROOT_DIR/utils/guard_operation.sh" "Apply changes?" >>"$OUT" 2>&1; then - err "operation cancelled by user" - fi - - if ! printf "%s" "$new_contents" > "$path" 2>/tmp/fs_patch_write.err; then - msg="$(cat /tmp/fs_patch_write.err 2>/dev/null || true)" - err "failed to write patched file ${path}: ${msg:-unknown error}" + 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" + echo "The patch applied to: $path" >> "$OUT" exit 0 } -# IMPORTANT: Do not let argc failures abort the script. -# - remove "set -e" -# - and swallow argc's non-zero return -eval "$(argc --argc-eval "$0" "$@" 2>/tmp/argc_fs_patch.err || true)" - -# If argc itself failed (e.g. parse error), report but still exit 0 -if [[ -s /tmp/argc_fs_patch.err ]]; then - echo "ERROR: argc parse failed: $(cat /tmp/argc_fs_patch.err)" >> "$OUT" - rm -f /tmp/argc_fs_patch.err - 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 /tmp/argc_fs_patch.err - -main - +rm -f "$argc_err" +exit 0 diff --git a/tools/fs_patch_new.sh b/tools/fs_patch_new.sh deleted file mode 100755 index 060c99a..0000000 --- a/tools/fs_patch_new.sh +++ /dev/null @@ -1,72 +0,0 @@ -#!/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 diff --git a/tools/fs_write.sh b/tools/fs_write.sh index 746c24b..3ff38d4 100755 --- a/tools/fs_write.sh +++ b/tools/fs_write.sh @@ -2,6 +2,8 @@ set -uo pipefail # @describe Write the full file contents to a file at the specified path. +# 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 write to # @option --contents! The full contents to write to the file # @env LLM_OUTPUT=/dev/stdout The output path @@ -9,6 +11,7 @@ set -uo pipefail 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 @@ -19,31 +22,26 @@ main() { contents="${argc_contents:-}" [[ -z "$path" ]] && err "missing --path" - # contents darf leer sein – das ist erlaubt (File leeren) - dir="$(dirname "$path")" + # empty contents are allowed (used to truncate a file) if [[ -e "$path" && ! -f "$path" ]]; then err "target exists but is not a regular file: $path" fi + # Show the diff (if any) so the model sees what is written. + # NOTE: no guard_operation.sh / guard_path.sh here -> no user interaction. if [[ -f "$path" ]]; then - # Diff in Output schreiben (damit LLM es sieht) printf "%s" "$contents" | git diff --no-index "$path" - >> "$OUT" 2>/dev/null || true - if ! "$ROOT_DIR/utils/guard_operation.sh" "Apply changes?" >>"$OUT" 2>&1; then - err "operation cancelled by user" - fi - else - if ! "$ROOT_DIR/utils/guard_path.sh" "$path" "Write '$path'?" >>"$OUT" 2>&1; then - err "operation cancelled by user" - fi - if ! mkdir -p "$dir" 2>/tmp/fs_write_mkdir.err; then - msg="$(cat /tmp/fs_write_mkdir.err 2>/dev/null || true)" - err "failed to create parent dir $dir: ${msg:-unknown error}" - fi fi - if ! printf "%s" "$contents" > "$path" 2>/tmp/fs_write.err; then - msg="$(cat /tmp/fs_write.err 2>/dev/null || true)" + dir="$(dirname "$path")" + if ! mkdir -p "$dir" 2>/tmp/fs_write_new_mkdir.err; then + msg="$(cat /tmp/fs_write_new_mkdir.err 2>/dev/null || true)" + err "failed to create parent dir $dir: ${msg:-unknown error}" + fi + + if ! printf "%s" "$contents" > "$path" 2>/tmp/fs_write_new.err; then + msg="$(cat /tmp/fs_write_new.err 2>/dev/null || true)" err "failed to write file $path: ${msg:-unknown error}" fi @@ -51,6 +49,16 @@ main() { exit 0 } -eval "$(argc --argc-eval "$0" "$@")" -main - +# 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
\ No newline at end of file diff --git a/tools/fs_write_new.sh b/tools/fs_write_new.sh deleted file mode 100755 index 3ff38d4..0000000 --- a/tools/fs_write_new.sh +++ /dev/null @@ -1,64 +0,0 @@ -#!/usr/bin/env bash -set -uo pipefail - -# @describe Write the full file contents to a file at the specified path. -# 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 write to -# @option --contents! The full contents to write to the file -# @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:-}" - contents="${argc_contents:-}" - - [[ -z "$path" ]] && err "missing --path" - # empty contents are allowed (used to truncate a file) - - if [[ -e "$path" && ! -f "$path" ]]; then - err "target exists but is not a regular file: $path" - fi - - # Show the diff (if any) so the model sees what is written. - # NOTE: no guard_operation.sh / guard_path.sh here -> no user interaction. - if [[ -f "$path" ]]; then - printf "%s" "$contents" | git diff --no-index "$path" - >> "$OUT" 2>/dev/null || true - fi - - dir="$(dirname "$path")" - if ! mkdir -p "$dir" 2>/tmp/fs_write_new_mkdir.err; then - msg="$(cat /tmp/fs_write_new_mkdir.err 2>/dev/null || true)" - err "failed to create parent dir $dir: ${msg:-unknown error}" - fi - - if ! printf "%s" "$contents" > "$path" 2>/tmp/fs_write_new.err; then - msg="$(cat /tmp/fs_write_new.err 2>/dev/null || true)" - err "failed to write file $path: ${msg:-unknown error}" - fi - - echo "The contents written 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
\ No newline at end of file |
