llm-tools.el

Opinionated tools for use by a (local) LLM
Log | Files | Refs | LICENSE

commit 008cb42c3cd44c2220944966dfd9095edf12b98e
parent b052ec7ded67ba5f187b00291aa58cc38856c826
Author: Vineet Kumar <git@vineetk.net>
Date:   Mon, 25 May 2026 13:25:29 -0400

start working on hashline diff applying

Diffstat:
Mllm-tools-hl.el | 59++++++++++++++++++++++++++++++++++++++++-------------------
1 file changed, 40 insertions(+), 19 deletions(-)

diff --git a/llm-tools-hl.el b/llm-tools-hl.el @@ -99,12 +99,6 @@ Each element is a line number and two-character hash, like `1vx'." (let ((lines (llm-tools--hl-file-lines file))) (llm-tools--hl-hash (elt lines (1- n))))) -;; before applying patch, first read the file to see if there were any changes -;; if the llm's replacement text's hashlines do not match current hashlines, reject the request -;; maybe if it just shifted a little bit (since the anchor is line number and hash of the line's content), there can be some kind of autohealing? -;; rather, the llm would not format it like the reads. it's just going to provide text according to what the anchors it gives say -;; so I just need to verify if the anchors didn't change - ;;;; Diff Parsing (defun llm-tools--split-patch-sections (patch) "Return a list of (FILENAME . REST) cons cells from PATCH. @@ -149,7 +143,7 @@ the operations in that file's sections. Each anchor is a string like (string-lines (cdr patch)))))))) sections)))) -(defun llm-tools--hl-verify (patch) +(defun llm-tools--hl-verify-anchors (patch) "Verify that all anchors in PATCH match the current file contents. Returns a list of booleans, one per file, indicating whether every anchor in that file's patch sections still matches the corresponding @@ -163,6 +157,42 @@ line in the on-disk file." (cons file (cl-subsetp anchors hl :test #'equal)))) patch-anchors))) +(defun llm-tools--hl-verify (patch) + "Verify if the files in PATCH need to be re-read. +Returns a string message that states which files need to be re-read. +Empty string means no files need to be re-read." + (string-join + (delete-dups + (remq nil + (mapcar (lambda (elem) + (unless (cdr elem) + (format "%s had incorrect anchors. Use the 'read_file' tool to get correct anchors." + (car elem)))) + (llm-tools--hl-verify-anchors patch)))) + "\n")) + +(defun llm-tools--hl-parse-section (section) + "Parses operation and optional payload from SECTION." + ) + +(defun llm-tools--hl-apply (patch) + "TODO rewrite docstring. Applies a patch." + ;; when performing each operation, I need to make sure that the line + ;; numbers of subsequent operations in same file also get updated. + (let ((sections (split-patch-sections patch))) + (llm-tools--hl-parse sections))) + +(defun llm-tools--hl-edit (patch) + "TODO write better docstring. First verifies if any files in PATCH needs +to be re-read. Then, applies the changes in PATCH. If any files need to +be re-read, the edit operation is cancelled for all files." + (let ((msg (llm-tools--hl-verify patch))) + (if (string= msg "") + (progn + (llm-tools--hl-apply patch) + (format "Finished applying patch.")) + (format "%s" msg)))) + (let* ((patch "\ # Replace one line (the payload must re-emit the original indentation) @@ mod.ts @@ -190,22 +220,13 @@ line in the on-disk file." ~export const done = true; # Delete a line -@@ test.el +@@ mod.ts - 5ff..5ff # Blank a line (replace with LF) @@ mod.ts -= 5bb..5bb += 5ff..5ff ")) - (string-join - (delete-dups - (remq nil - (mapcar (lambda (elem) - (unless (cdr elem) - (format "%s had incorrect anchors. Use the 'read_file' tool to get correct anchors." - (car elem)))) - (llm-tools--hl-verify patch)))) - "\n")) + (llm-tools--hl-apply patch)) (provide 'llm-tools-hl) -