72 lines
2.7 KiB
Org Mode
72 lines
2.7 KiB
Org Mode
:PROPERTIES:
|
|
:ID: 1e5a2c30-d3a9-4674-8db7-b08e7e1f44d1
|
|
:CREATED: [2026-04-11 Sat 14:40]
|
|
:END:
|
|
#+TITLE: SKILL: Lisp Repair Syntax Gate
|
|
#+STARTUP: content
|
|
#+FILETAGS: :system:repair:syntax:lisp:psf:
|
|
|
|
* Overview
|
|
The *Lisp Repair Syntax Gate* intercepts System 1 (LLM) proposals that fail the formal Lisp `read-from-string` parser due to syntax errors (e.g., hallucinated trailing text, unbalanced parentheses). It acts as a pre-eval System 2 safety buffer.
|
|
|
|
* Implementation
|
|
|
|
** Core Repair Logic
|
|
#+begin_src lisp :tangle ../src/lisp-repair.lisp
|
|
(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."
|
|
(let* ((open-parens (count-char #\( code))
|
|
(close-parens (count-char #\) code))
|
|
(diff (- open-parens close-parens)))
|
|
(if (> diff 0)
|
|
;; Append missing closing parens
|
|
(concatenate 'string code (make-string diff :initial-element #\)))
|
|
code)))
|
|
|
|
(defun neural-repair (code error-message)
|
|
"Uses System 1 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))))
|
|
|
|
(defun repair-lisp-syntax (code error-message)
|
|
"The entry point called by the neuro-gate when read-from-string fails."
|
|
(kernel-log "SYNTAX GATE: Intercepted broken Lisp. Attempting deterministic repair...")
|
|
(let ((fast-fix (deterministic-repair code)))
|
|
(handler-case
|
|
(read-from-string fast-fix)
|
|
(error ()
|
|
(kernel-log "SYNTAX GATE: Deterministic repair failed. Escalating to neural repair...")
|
|
(let ((deep-fix (neural-repair code error-message)))
|
|
(handler-case
|
|
(read-from-string deep-fix)
|
|
(error ()
|
|
(kernel-log "SYNTAX GATE: Neural repair failed.")
|
|
nil)))))))
|
|
#+end_src
|
|
|
|
** Skill Definition
|
|
Since this skill is a passive System 2 interceptor (called dynamically by `neuro.lisp`), it doesn't need to trigger on its own via the Event Bus.
|
|
|
|
#+begin_src lisp :tangle ../src/lisp-repair.lisp
|
|
(defskill :skill-lisp-repair
|
|
:priority 90
|
|
:trigger (lambda (ctx) (declare (ignore ctx)) nil) ;; Passive interceptor
|
|
:neuro nil
|
|
:symbolic nil)
|
|
#+end_src
|