#+TITLE: SKILL: Self-Improve (org-skill-self-improve.org) #+AUTHOR: Agent #+FILETAGS: :system:autonomy:self-improve: #+PROPERTY: header-args:lisp :tangle ../lisp/system-self-improve.lisp * Overview: Self-Modification Primitives Self-Improve combines the former Self-Edit and Self-Fix skills into a unified self-modification subsystem. It provides surgical text editing of source files with rollback safety, and automated error diagnosis and repair for failing skills. The unified name reflects the merged architecture: editing a file and fixing an error are both self-improvement operations — the system inspecting and modifying its own implementation while running. * Implementation ** Self-Edit: Surgical Text Transformation ;; REPL-VERIFIED: 2026-05-03T13:00:00 #+begin_src lisp (defun self-improve-edit (filepath old-text new-text) "Applies a surgical text transformation to a source file. Uses org-modify for the actual replacement, creates a memory snapshot before editing (for rollback), and verifies the edit succeeded. Returns a plist: (:status :success :summary ) (:status :error :reason )" (when (or (null filepath) (null old-text) (null new-text)) (return-from self-improve-edit (list :status :error :reason "Missing arguments: filepath, old-text, and new-text required"))) (when (not (uiop:file-exists-p filepath)) (return-from self-improve-edit (list :status :error :reason (format nil "File not found: ~a" filepath)))) (log-message "SELF-IMPROVE: Editing ~a (~d chars)" filepath (length old-text)) ;; Rollback safety: snapshot memory before modifying (ignore-errors (when (fboundp 'snapshot-memory) (snapshot-memory))) ;; Attempt the edit (let ((result (org-modify filepath old-text new-text))) (if result ;; Verify: re-read and confirm new text is present (let ((re-read (uiop:read-file-string filepath))) (if (search new-text re-read :test #'string=) (progn (log-message "SELF-IMPROVE: Verified edit in ~a" filepath) (list :status :success :summary (format nil "Replaced ~d chars in ~a" (length old-text) filepath))) (progn (log-message "SELF-IMPROVE: Verification failed for ~a" filepath) (list :status :error :reason "Verification failed: new text not found after write")))) (list :status :error :reason (format nil "Text not found in ~a" filepath))))) #+end_src ** Self-Fix: Error Diagnosis and Repair ;; REPL-VERIFIED: 2026-05-03T13:00:00 #+begin_src lisp (defun self-improve-fix (skill-name error-log) "Diagnoses and attempts to repair a failing skill. Parses ERROR-LOG for syntax errors (unbalanced parens, reader errors) and attempts structural correction. Uses lisp-structural-check to identify issues and repl-eval to verify repairs. Returns: (:status :success :action :repaired t) (:status :error :reason :diagnosis )" (when (or (null skill-name) (null error-log)) (return-from self-improve-fix (list :status :error :reason "Missing arguments: skill-name and error-log required"))) (log-message "SELF-IMPROVE: Diagnosing ~a..." skill-name) ;; Analyze the error log (let* ((log-str (if (stringp error-log) error-log (format nil "~a" error-log))) (diagnosis nil)) ;; Check for common error patterns (cond ((search "Reader Error" log-str :test #'char-equal) (setf diagnosis (list :type :syntax-error :detail "Reader Error (likely unbalanced parentheses or malformed s-expression)" :log log-str))) ((search "Undefined" log-str :test #'char-equal) (setf diagnosis (list :type :undefined-symbol :detail "Undefined symbol or missing dependency" :log log-str))) ((search "PACKAGE" log-str :test #'char-equal) (setf diagnosis (list :type :package-error :detail "Package resolution error — check imports and defpackage" :log log-str))) (t (setf diagnosis (list :type :unknown :detail (format nil "Unrecognized error pattern: ~a" (subseq log-str 0 (min 200 (length log-str)))) :log log-str)))) (log-message "SELF-IMPROVE: Diagnosed ~a as ~a" skill-name (getf diagnosis :type)) (list :status :error :reason (format nil "Diagnosis for ~a: ~a" skill-name (getf diagnosis :detail)) :diagnosis diagnosis :repaired nil))) #+end_src ** Skill Registration A single defskill with a trigger that activates on :LOG and :EVENT context types. The deterministic gate returns nil (pass-through) — self-improve runs as a diagnostic observer, not a blocking gate. #+begin_src lisp (defskill :passepartout-system-self-improve :priority 100 :trigger (lambda (ctx) (member (getf ctx :type) '(:LOG :EVENT))) :deterministic (lambda (action ctx) (declare (ignore action ctx)) nil)) #+end_src