IWZF5VWU3D57MRTTJOW3T76BLZJN76QJURLCBMJFXIMS2W2VEU2AC (defun madrigal--obj-has-key-p (obj key)(and (listp obj)(let* ((base (if (keywordp key)(substring (symbol-name key) 1)(and (symbolp key) (symbol-name key))))(plain (and base (intern base)))(kword (and base (intern (format ":%s" base)))))(or (and (symbolp key) (assq key obj))(and plain (assq plain obj))(and kword (assq kword obj))(and base (assoc base obj))(and (keywordp key) (plist-member obj key))))))
(step (or (nth idx results)(car (last results)))))(or (madrigal--alist-get* field step)(madrigal--alist-get* (intern (format ":%s" field)) step)value))
(step-count (length results)))(unless (and (>= idx 0) (< idx step-count))(error "madrigal: invalid reference %s (step out of range)" value))(let* ((step (nth idx results))(resolved (or (madrigal--alist-get* field step)(madrigal--alist-get* (intern (format ":%s" field)) step))))(if resolvedresolved(error "madrigal: invalid reference %s (field missing)" value))))
(unless (integerp value)(error "madrigal: %s must be integer" field))value)
(let ((parsed(cond((integerp value) value)((and (floatp value)(= value (truncate value)))(truncate value))((and (stringp value)(string-match-p "^-?[0-9]+$" value))(string-to-number value))(t nil))))(unless (integerp parsed)(error "madrigal: %s must be integer" field))parsed))
(let* ((needle0 (or (madrigal--alist-get* 'needle raw-op)(madrigal--alist-get* :needle raw-op)(madrigal--alist-get* 'text raw-op)
(let* ((needle0 (or (madrigal--alist-get* 'needle raw-op)(madrigal--alist-get* :needle raw-op)(madrigal--alist-get* 'text raw-op)
(set-region0 (or (madrigal--alist-get* 'set_region raw-op)(madrigal--alist-get* :set_region raw-op)nil))(wrap0 (or (madrigal--alist-get* 'wrap raw-op)(madrigal--alist-get* :wrap raw-op)t))(needle (format "%s" (madrigal--resolve-ref needle0 results)))(count (madrigal--require-integer(madrigal--resolve-ref count0 results) "count"))(set-region-p (not (memq set-region0 '(nil :false false))))(wrap-p (not (memq wrap0 '(nil :false false)))))(when (string-empty-p needle)(error "madrigal: needle must be non-empty"))(when (<= count 0)(error "madrigal: count must be > 0"))(with-current-buffer replica(let ((found nil)(wrapped nil))(dotimes (_ count)(setq found (search-forward needle nil t))(unless found(when wrap-p(goto-char (point-min))(setq wrapped t)(setq found (search-forward needle nil t)))(unless found(error "madrigal: search_forward needle not found"))))(when set-region-p(madrigal--set-region replica (- (point) (length needle)) (point)))(setq row (append row(list (cons 'point (point))(cons 'match_start (- (point) (length needle)))(cons 'match_end (point))(cons 'wrapped wrapped))))))))
(set-region0 (or (madrigal--alist-get* 'set_region raw-op)(madrigal--alist-get* :set_region raw-op)nil))(required0 (or (madrigal--alist-get* 'required raw-op)(madrigal--alist-get* :required raw-op)nil))(wrap-present (or (madrigal--obj-has-key-p raw-op 'wrap)(madrigal--obj-has-key-p raw-op :wrap)))(wrap0 (or (madrigal--alist-get* 'wrap raw-op)(madrigal--alist-get* :wrap raw-op)))(needle (format "%s" (madrigal--resolve-ref needle0 results)))(count (madrigal--require-integer(madrigal--resolve-ref count0 results) "count"))(set-region-p (not (memq set-region0 '(nil :false false))))(required-p (not (memq required0 '(nil :false false))))(wrap-p (if wrap-present(not (memq wrap0 '(nil :false false)))t)))(when (string-empty-p needle)(error "madrigal: needle must be non-empty"))(when (<= count 0)(error "madrigal: count must be > 0"))(with-current-buffer replica(let ((found nil)(wrapped nil)(missing nil))(cl-loop repeat count do(progn(setq found (search-forward needle nil t))(unless found(when wrap-p(goto-char (point-min))(setq wrapped t)(setq found (search-forward needle nil t))))(unless found(setq missing t)(cl-return))))(when (and missing required-p)(error "madrigal: search_forward needle not found"))(when (and found set-region-p)(madrigal--set-region replica (- (point) (length needle)) (point)))(setq row (append row(list (cons 'found (and found t))(cons 'point (point))(cons 'match_start (and found (- (point) (length needle))))(cons 'match_end (and found (point)))(cons 'wrapped wrapped))))))))
(let* ((needle0 (or (madrigal--alist-get* 'needle raw-op)(madrigal--alist-get* :needle raw-op)(madrigal--alist-get* 'text raw-op)
(let* ((needle0 (or (madrigal--alist-get* 'needle raw-op)(madrigal--alist-get* :needle raw-op)(madrigal--alist-get* 'text raw-op)
(set-region0 (or (madrigal--alist-get* 'set_region raw-op)(madrigal--alist-get* :set_region raw-op)nil))(wrap0 (or (madrigal--alist-get* 'wrap raw-op)(madrigal--alist-get* :wrap raw-op)t))(needle (format "%s" (madrigal--resolve-ref needle0 results)))(count (madrigal--require-integer(madrigal--resolve-ref count0 results) "count"))(set-region-p (not (memq set-region0 '(nil :false false))))(wrap-p (not (memq wrap0 '(nil :false false)))))(when (string-empty-p needle)(error "madrigal: needle must be non-empty"))(when (<= count 0)(error "madrigal: count must be > 0"))(with-current-buffer replica(let ((found nil)(wrapped nil))(dotimes (_ count)(setq found (search-backward needle nil t))(unless found(when wrap-p(goto-char (point-max))(setq wrapped t)(setq found (search-backward needle nil t)))(unless found(error "madrigal: search_backward needle not found"))))(when set-region-p(madrigal--set-region replica found (+ found (length needle))))(goto-char found)(setq row (append row(list (cons 'point (point))(cons 'match_start found)(cons 'match_end (+ found (length needle)))(cons 'wrapped wrapped))))))))
(set-region0 (or (madrigal--alist-get* 'set_region raw-op)(madrigal--alist-get* :set_region raw-op)nil))(required0 (or (madrigal--alist-get* 'required raw-op)(madrigal--alist-get* :required raw-op)nil))(wrap-present (or (madrigal--obj-has-key-p raw-op 'wrap)(madrigal--obj-has-key-p raw-op :wrap)))(wrap0 (or (madrigal--alist-get* 'wrap raw-op)(madrigal--alist-get* :wrap raw-op)))(needle (format "%s" (madrigal--resolve-ref needle0 results)))(count (madrigal--require-integer(madrigal--resolve-ref count0 results) "count"))(set-region-p (not (memq set-region0 '(nil :false false))))(required-p (not (memq required0 '(nil :false false))))(wrap-p (if wrap-present(not (memq wrap0 '(nil :false false)))t)))(when (string-empty-p needle)(error "madrigal: needle must be non-empty"))(when (<= count 0)(error "madrigal: count must be > 0"))(with-current-buffer replica(let ((found nil)(wrapped nil)(missing nil))(cl-loop repeat count do(progn(setq found (search-backward needle nil t))(unless found(when wrap-p(goto-char (point-max))(setq wrapped t)(setq found (search-backward needle nil t))))(unless found(setq missing t)(cl-return))))(when (and missing required-p)(error "madrigal: search_backward needle not found"))(when (and found set-region-p)(madrigal--set-region replica found (+ found (length needle))))(when found(goto-char found))(setq row (append row(list (cons 'found (and found t))(cons 'point (point))(cons 'match_start found)(cons 'match_end (and found (+ found (length needle))))(cons 'wrapped wrapped))))))))
(unless madrigal-allow-save-buffer-op(error "madrigal: save_buffer disabled; user should save explicitly"))(let ((server-buffer (madrigal--server-buffer-from-network-name network-name)))(unless (buffer-live-p server-buffer)(error "madrigal: no server buffer for %s" network-name))(unless (madrigal--flush-replica-to-serverreplica agent-session server-buffer madrigal-save-sync-timeout)(error "madrigal: timed out waiting for CRDT sync before save"))(with-current-buffer server-buffer(when (and buffer-file-name (buffer-modified-p))(save-buffer))(setq row (append row(list (cons 'saved (and buffer-file-name t))(cons 'path buffer-file-name)))))))
(if (not madrigal-allow-save-buffer-op)(setq row (append row(list (cons 'saved nil)(cons 'skipped t)(cons 'reason "user-save-required"))))(let ((server-buffer (madrigal--server-buffer-from-network-name network-name)))(unless (buffer-live-p server-buffer)(error "madrigal: no server buffer for %s" network-name))(unless (madrigal--flush-replica-to-serverreplica agent-session server-buffer madrigal-save-sync-timeout)(error "madrigal: timed out waiting for CRDT sync before save"))(with-current-buffer server-buffer(when (and buffer-file-name (buffer-modified-p))(save-buffer))(setq row (append row(list (cons 'saved (and buffer-file-name t))(cons 'path buffer-file-name))))))))
"Operations: open_file, list_buffers, switch_buffer, get_point, goto_char, ""search_forward, search_backward, set_region, clear_region, read_region, "
"Operations: open_file, list_buffers, switch_buffer, get_point, goto_char, ""search_forward, search_backward, set_region, clear_region, read_region, "
"search_forward/search_backward support needle plus text/query aliases and optional wrap boolean (default true). "
"search_forward/search_backward support needle plus text/query aliases, optional wrap boolean (default true), and optional required boolean (default false). "
(lambda (final)(madrigal--log-round request-id round-no final)(if (plist-get final :tool-results)(madrigal--run-model-loop provider prompt (1- rounds-left) done request-id (1+ round-no))(funcall done (list :status 'ok :result final))))
(lambda (final)(madrigal--log-round request-id round-no final)(if (and (plist-get final :tool-results)(string-empty-p (or (plist-get final :text) "")))(madrigal--run-model-loop provider prompt (1- rounds-left) done request-id (1+ round-no))(funcall done (list :status 'ok :result final))))
(lambda (final)(madrigal--log-round request-id round-no final)(if (plist-get final :tool-results)(madrigal--run-model-loop provider prompt (1- rounds-left) done request-id (1+ round-no))(funcall done (list :status 'ok :result final :fallback 'non-streaming-tool-use))))
(lambda (final)(madrigal--log-round request-id round-no final)(if (and (plist-get final :tool-results)(string-empty-p (or (plist-get final :text) "")))(madrigal--run-model-loop provider prompt (1- rounds-left) done request-id (1+ round-no))(funcall done (list :status 'ok :result final :fallback 'non-streaming-tool-use))))