fix(reason): complete reconstruction of reason.org to resolve catastrophic syntax failures

This commit is contained in:
2026-04-28 19:15:31 -04:00
parent 9f71f7c391
commit 9506b23ea6

View File

@@ -1,4 +1,3 @@
#+PROPERTY: header-args:lisp :tangle reason.lisp
#+TITLE: Stage 2: Reason (reason.lisp) #+TITLE: Stage 2: Reason (reason.lisp)
#+AUTHOR: Agent #+AUTHOR: Agent
#+FILETAGS: :harness:reason: #+FILETAGS: :harness:reason:
@@ -17,33 +16,27 @@ The Reason stage implements the core Innovation of OpenCortex: the separation of
** Probabilistic Engine Configuration ** Probabilistic Engine Configuration
#+begin_src lisp #+begin_src lisp
(defvar *probabilistic-backends* (make-hash-table :test 'equal) (defvar *probabilistic-backends* (make-hash-table :test 'equal))
"Registry mapping provider keywords (:openrouter, :ollama) to their calling functions.
(defvar *provider-cascade* nil (defvar *provider-cascade* nil)
"Ordered list of provider keywords to try. First available provider wins.
(defvar *model-selector-fn* nil (defvar *model-selector-fn* nil)
"Optional function that selects a specific model for each provider.
(defvar *consensus-enabled-p* nil (defvar *consensus-enabled-p* nil)
"When T, run multiple providers and compare results for critical decisions.
#+end_src #+end_src
** Backend Registration (register-probabilistic-backend) ** Backend Registration (register-probabilistic-backend)
#+begin_src lisp #+begin_src lisp
(defun register-probabilistic-backend (name fn) (defun register-probabilistic-backend (name fn)
"Register a neural provider backend."
(setf (gethash name *probabilistic-backends*) fn)) (setf (gethash name *probabilistic-backends*) fn))
#+end_src #+end_src
** Cascade Dispatch (probabilistic-call) ** Cascade Dispatch (probabilistic-call)
#+begin_src lisp #+begin_src lisp
(defun probabilistic-call (prompt &key (defun probabilistic-call (prompt &key
(system-prompt "You are the Probabilistic engine. (system-prompt "You are the Probabilistic engine.")
(cascade nil) (cascade nil)
(context nil)) (context nil))
"Dispatch a neural request through the provider cascade."
(let ((backends (or cascade *provider-cascade*))) (let ((backends (or cascade *provider-cascade*)))
(or (dolist (backend backends) (or (dolist (backend backends)
(let ((backend-fn (gethash backend *probabilistic-backends*))) (let ((backend-fn (gethash backend *probabilistic-backends*)))
@@ -62,23 +55,21 @@ The Reason stage implements the core Innovation of OpenCortex: the separation of
(harness-log "PROBABILISTIC: Backend ~a failed: ~a" (harness-log "PROBABILISTIC: Backend ~a failed: ~a"
backend (getf result :message)))))))) backend (getf result :message))))))))
(list :type :LOG (list :type :LOG
:payload (list :text "Neural Cascade Failure: All providers exhausted.)))) :payload (list :text "Neural Cascade Failure: All providers exhausted.")))))
#+end_src #+end_src
** Cognitive Proposal Generation (Think) ** Cognitive Proposal Generation (Think)
#+begin_src lisp #+begin_src lisp
(defun strip-markdown (text) (defun strip-markdown (text)
"Strip markdown formatting from LLM output."
(if (and text (stringp text)) (if (and text (stringp text))
(let ((cleaned text)) (let ((cleaned text))
(setf cleaned (cl-ppcre:regex-replace-all "^```[a-z]*\\n" cleaned (setf cleaned (cl-ppcre:regex-replace-all "^```[a-z]*\\n" cleaned ""))
(setf cleaned (cl-ppcre:regex-replace-all "\\n```$" cleaned (setf cleaned (cl-ppcre:regex-replace-all "\\n```$" cleaned ""))
(setf cleaned (cl-ppcre:regex-replace-all "```" cleaned (setf cleaned (cl-ppcre:regex-replace-all "```" cleaned ""))
(string-trim '(#\Space #\Newline #\Tab) cleaned)) (string-trim '(#\Space #\Newline #\Tab) cleaned))
text)) text))
(defun normalize-plist-keywords (plist) (defun normalize-plist-keywords (plist)
"Normalize all keys in a plist to keywords."
(when (listp plist) (when (listp plist)
(loop for (k v) on plist by #'cddr (loop for (k v) on plist by #'cddr
collect (if (and (symbolp k) (not (keywordp k))) collect (if (and (symbolp k) (not (keywordp k)))
@@ -87,39 +78,37 @@ The Reason stage implements the core Innovation of OpenCortex: the separation of
collect v))) collect v)))
(defun think (context) (defun think (context)
"Generate a Lisp action proposal based on current context."
(let* ((active-skill (find-triggered-skill context)) (let* ((active-skill (find-triggered-skill context))
(tool-belt (generate-tool-belt-prompt)) (tool-belt (generate-tool-belt-prompt))
(global-context (context-assemble-global-awareness)) (global-context (context-assemble-global-awareness))
(system-logs (context-get-system-logs)) (system-logs (context-get-system-logs))
(assistant-name (or (uiop:getenv "MEMEX_ASSISTANT "Agent) (assistant-name (or (uiop:getenv "MEMEX_ASSISTANT") "Agent"))
(rejection-trace (proto-get (proto-get context :payload) :rejection-trace)) (rejection-trace (proto-get (proto-get context :payload) :rejection-trace))
(prompt-generator (when active-skill (skill-probabilistic-prompt active-skill))) (prompt-generator (when active-skill (skill-probabilistic-prompt active-skill)))
(raw-prompt (if prompt-generator (raw-prompt (if prompt-generator
(funcall prompt-generator context) (funcall prompt-generator context)
(let ((p (proto-get (proto-get context :payload) :text))) (let ((p (proto-get (proto-get context :payload) :text)))
(if (and p (stringp p)) p "Maintain metabolic stasis.))) (if (and p (stringp p)) p "Maintain metabolic stasis."))))
(reflection-feedback (if rejection-trace (reflection-feedback (if rejection-trace
(format nil "~%~%PREVIOUS PROPOSAL REJECTED: ~a" rejection-trace) (format nil "~%~%PREVIOUS PROPOSAL REJECTED: ~a" rejection-trace)
""))
(system-prompt (format nil "IDENTITY: ~a~a~%~%TOOLS:~%~a~%~%CONTEXT:~%~a~%~%LOGS:~%~a" (system-prompt (format nil "IDENTITY: ~a~a~%~%TOOLS:~%~a~%~%CONTEXT:~%~a~%~%LOGS:~%~a"
assistant-name reflection-feedback tool-belt global-context system-logs))) assistant-name reflection-feedback tool-belt global-context system-logs)))
(let* ((thought (probabilistic-call raw-prompt :system-prompt system-prompt :context context)) (let* ((thought (probabilistic-call raw-prompt :system-prompt system-prompt :context context))
(cleaned (strip-markdown thought))) (cleaned (strip-markdown thought)))
(if (and cleaned (stringp cleaned) (> (length cleaned) 0) (char= (char cleaned 0) #\()) (if (and cleaned (stringp cleaned) (> (length cleaned) 0) (char= (char cleaned 0) #\((char= (char cleaned 0) #\()))
(handler-case (handler-case
(let ((parsed (read-from-string cleaned))) (let ((parsed (read-from-string cleaned)))
(if (listp parsed) (if (listp parsed)
(normalize-plist-keywords parsed) (normalize-plist-keywords parsed)
(list :TYPE :REQUEST :PAYLOAD (list :ACTION :MESSAGE :TEXT cleaned)))) (list :TYPE :REQUEST :PAYLOAD (list :ACTION :MESSAGE :TEXT cleaned))))
(error () (list :TYPE :REQUEST :PAYLOAD (list :ACTION :MESSAGE :TEXT cleaned)))) (error () (list :TYPE :REQUEST :PAYLOAD (list :ACTION :MESSAGE :TEXT cleaned))))
(list :TYPE :REQUEST :PAYLOAD (list :ACTION :MESSAGE :TEXT (or cleaned "No response)))))) (list :TYPE :REQUEST :PAYLOAD (list :ACTION :MESSAGE :TEXT (or cleaned "No response")))))))
#+end_src #+end_src
** Deterministic Engine (Verification) ** Deterministic Engine (Verification)
#+begin_src lisp #+begin_src lisp
(defun deterministic-verify (proposed-action context) (defun deterministic-verify (proposed-action context)
"Run all skill deterministic gates on a proposed action."
(let ((current-action proposed-action) (let ((current-action proposed-action)
(skills nil)) (skills nil))
(maphash (lambda (name skill) (maphash (lambda (name skill)
@@ -144,7 +133,6 @@ The Reason stage implements the core Innovation of OpenCortex: the separation of
** Reason Gate (Stage 2) ** Reason Gate (Stage 2)
#+begin_src lisp #+begin_src lisp
(defun reason-gate (signal) (defun reason-gate (signal)
"Stage 2 of the metabolic pipeline: Reason."
(let* ((type (proto-get signal :type)) (let* ((type (proto-get signal :type))
(payload (proto-get signal :payload)) (payload (proto-get signal :payload))
(sensor (proto-get payload :sensor))) (sensor (proto-get payload :sensor)))
@@ -176,28 +164,30 @@ The Reason stage implements the core Innovation of OpenCortex: the separation of
#+end_src #+end_src
* Test Suite * Test Suite
#+begin_src lisp :tangle reason.lisp #+begin_src lisp :tangle tests/pipeline-reason-tests.lisp
(eval-when (:compile-toplevel :load-toplevel :execute)
(ql:quickload :fiveam :silent t))
(defpackage :opencortex-pipeline-reason-tests (defpackage :opencortex-pipeline-reason-tests
(:use :cl :fiveam :opencortex) (:use :cl :fiveam :opencortex)
(:export #:pipeline-reason-suite)) (:export #:pipeline-reason-suite))
(in-package :opencortex-pipeline-reason-tests) (in-package :opencortex-pipeline-reason-tests)
(def-suite pipeline-reason-suite :description "Test suite for Reason pipeline (def-suite pipeline-reason-suite :description "Test suite for Reason pipeline")
(in-suite pipeline-reason-suite) (in-suite pipeline-reason-suite)
(test test-decide-gate-safety (test test-decide-gate-safety
"Decide gate should block unsafe LLM proposals."
(clrhash opencortex::*skills-registry*) (clrhash opencortex::*skills-registry*)
(opencortex::defskill :mock-safety (opencortex::defskill :mock-safety
:priority 50 :priority 50
:trigger (lambda (ctx) t) :trigger (lambda (ctx) (declare (ignore ctx)) t)
:deterministic (lambda (action ctx) :deterministic (lambda (action ctx)
(declare (ignore ctx)) (declare (ignore ctx))
(if (search "rm -rf" (format nil "~s" action)) (if (search "rm -rf" (format nil "~s" action))
(list :type :LOG :payload (list :text "Rejected) (list :type :LOG :payload (list :text "Rejected"))
action))) action)))
(let* ((candidate '(:type :REQUEST :payload (:action :shell :cmd "rm -rf /)) (let* ((candidate '(:type :REQUEST :payload (:action :shell :cmd "rm -rf /")))
(signal '(:type :EVENT :payload (:sensor :user-input))) (signal '(:type :EVENT :payload (:sensor :user-input)))
(result (deterministic-verify candidate signal))) (result (deterministic-verify candidate signal)))
(is (eq :LOG (getf result :type))))) (is (eq :LOG (getf result :type)))))