CTGC5XHGRX4TDP5JKKWOALY6LEA5OCQCR3WRF4N6KHTLWGA5SLGAC (defun madrigal--log-append-src (request-id obj &optional tag)(let* ((buf (madrigal--ensure-log-buffer))(marker (gethash request-id madrigal--log-request-markers)))(with-current-buffer buf(save-excursion(goto-char (if (and marker (marker-buffer marker)) marker (point-max)))(when tag(insert (format "#+name: %s\n" tag)))(madrigal--log-insert-src-block obj)(puthash request-id (copy-marker (point) t) madrigal--log-request-markers)))(madrigal--log-refresh-visibility buf)))(defun madrigal--as-list-safe (v)(cond((vectorp v) (append v nil))((listp v) v)(t nil)))
(when tool-uses(madrigal--log-append-obj request-id 3 "Tool Uses" tool-uses))(when tool-results(madrigal--log-append-obj request-id 3 "Tool Results" tool-results)))))
(when (or tool-uses tool-results)(let* ((uses (madrigal--as-list-safe tool-uses))(results (madrigal--as-list-safe tool-results))(count (max (length uses) (length results))))(dotimes (idx count)(let ((n (1+ idx))(use (nth idx uses))(result (nth idx results)))(when use(madrigal--log-append-srcrequest-iduse(format "round-%d-tool-use-%d" round n)))(when result(madrigal--log-append-srcrequest-idresult(format "round-%d-tool-result-%d" round n))))))))))
Returns number of characters moved."(let ((before (point)))(pcase unit("char"(let ((target (+ before (if forward-p count (- count)))))(goto-char (min (point-max) (max (point-min) target)))))("line"(forward-line (if forward-p count (- count))))("word"(condition-case err(funcall (if forward-p #'forward-word #'backward-word) count)((beginning-of-buffer end-of-buffer)(goto-char (if forward-p (point-max) (point-min))))(error(signal (car err) (cdr err)))))("symbol"(condition-case err(forward-symbol (if forward-p count (- count)))((beginning-of-buffer end-of-buffer)(goto-char (if forward-p (point-max) (point-min))))(error(signal (car err) (cdr err)))))("sexp"(condition-case err(funcall (if forward-p #'forward-sexp #'backward-sexp) count)((beginning-of-buffer end-of-buffer)(goto-char (if forward-p (point-max) (point-min))))(error(signal (car err) (cdr err)))))("buffer"(goto-char (if forward-p (point-max) (point-min)))))(abs (- (point) before))))
(unless (member direction '("forward" "backward"))(error "madrigal: move direction must be forward/backward"))(unless (member unit '("char" "line" "word" "symbol" "sexp" "buffer"))(error "madrigal: move unit must be char/line/word/symbol/sexp/buffer"))(when (< count 0)(error "madrigal: count must be >= 0"))(with-current-buffer replica(let ((before (point)))(pcase unit("char" (funcall (if forward-p #'forward-char #'backward-char) count))("line" (forward-line (if forward-p count (- count))))("word" (funcall (if forward-p #'forward-word #'backward-word) count))("symbol" (forward-symbol (if forward-p count (- count))))("sexp" (funcall (if forward-p #'forward-sexp #'backward-sexp) count))("buffer" (goto-char (if forward-p (point-max) (point-min)))))(setq row (append row(list (cons 'moved_chars (abs (- (point) before))))))))))
(unless (member direction '("forward" "backward"))(error "madrigal: move direction must be forward/backward"))(unless (member unit '("char" "line" "word" "symbol" "sexp" "buffer"))(error "madrigal: move unit must be char/line/word/symbol/sexp/buffer"))(when (< count 0)(error "madrigal: count must be >= 0"))(with-current-buffer replica(setq row (append row(list (cons 'moved_chars(madrigal--move-clamped forward-p unit count))))))))