Files
passepartout/skills/org-skill-lisp-repair.org

3.2 KiB

SKILL: Lisp Repair Syntax Gate

Overview

The Lisp Repair Syntax Gate asynchronously intercepts `:syntax-error` events emitted by the harness when Probabilistic Engine (LLM) proposals fail to parse. It performs deterministic or neural repairs and re-injects the corrected action into the pipeline.

Implementation

Core Repair Logic

(in-package :org-agent)

(defun count-char (char string)
  (let ((count 0))
    (loop for c across string
          when (char= c char)
            do (incf count))
    count))

(defun deterministic-repair (code)
  "Attempts instant fixes on broken Lisp code (e.g. balancing parens)."
  (let* ((open-parens (count-char #\( code))
         (close-parens (count-char #\) code))
         (diff (- open-parens close-parens)))
    (if (> diff 0)
        (concatenate 'string code (make-string diff :initial-element #\)))
        code)))

(defun neural-repair (code error-message)
  "Uses Probabilistic Engine to deeply repair the syntax structure."
  (let ((prompt (format nil "The following Lisp code failed to parse. 
ERROR: ~a
CODE: ~a
MANDATE: Output EXACTLY ONE valid Common Lisp list. Do not explain. Do not use markdown blocks."
                        error-message code))
        (system-prompt "You are a Lisp Syntax Repair Actuator. Return only valid, balanced Lisp code."))
    (let ((repaired (ask-neuro prompt :system-prompt system-prompt)))
      (string-trim '(#\Space #\Newline #\Tab) repaired))))

Skill Definition

Reacts to syntax error events and transforms them into repaired requests.

(defskill :skill-lisp-repair
  :priority 90
  :trigger (lambda (ctx) (eq (getf (getf ctx :payload) :sensor) :syntax-error))
  :neuro nil ;; Handled deterministically in symbolic or manually via ask-neuro
  :symbolic (lambda (action context)
              (declare (ignore action))
              (let* ((payload (getf context :payload))
                     (code (getf payload :code))
                     (error-msg (getf payload :error)))
                (harness-log "SYNTAX GATE: Reacting to broken Lisp stimulus...")
                (let ((fast-fix (deterministic-repair code)))
                  (handler-case
                      (let ((repaired (read-from-string fast-fix)))
                        (harness-log "SYNTAX GATE: Deterministic repair SUCCESS.")
                        repaired)
                    (error ()
                      (harness-log "SYNTAX GATE: Deterministic repair failed. Escalating...")
                      (let ((deep-fix (neural-repair code error-msg)))
                        (handler-case
                            (let ((repaired (read-from-string deep-fix)))
                              (harness-log "SYNTAX GATE: Neural repair SUCCESS.")
                              repaired)
                          (error ()
                            (harness-log "SYNTAX GATE: Neural repair failed.")
                            (list :type :LOG :payload (list :text "Lisp Repair Failed.")))))))))))