fix(reason): Definitive clean-room rewrite of cognition engine (verified syntax)
Some checks failed
Deploy-Agent-V15-Stdin / JOB-V15-STDIN (push) Failing after 2s

This commit is contained in:
2026-04-19 17:26:09 -04:00
parent 3374d27e75
commit aa39bbbaa8
3 changed files with 147 additions and 53 deletions

View File

@@ -5,16 +5,17 @@
* Stage 2: Reason (reason.lisp)
** Architectural Intent: Unified Cognition
The Reason stage is the cognitive engine of the OpenCortex. It unifies two distinct reasoning modes:
1. **Probabilistic Reasoning:** Consulting neural models (LLMs) to generate action proposals based on current context.
2. **Deterministic Reasoning:** Running those proposals through a series of deterministic safety gates (Policy, Invariants, and Skill-specific validation) to ensure alignment and security.
The Reason stage is the cognitive engine of the OpenCortex. It bridges the gap between raw sensory data (Perceive) and physical side-effects (Act).
** Package and Registry
We initialize the probabilistic backends and the provider cascade which determines the order in which models are consulted.
* Cognition Engine (reason.lisp)
** Package Context
#+begin_src lisp :tangle ../src/reason.lisp
(in-package :opencortex)
#+end_src
** Neural Backend Registry
#+begin_src lisp :tangle ../src/reason.lisp
(defvar *probabilistic-backends* (make-hash-table :test 'equal))
(defvar *provider-cascade* nil)
(defvar *model-selector-fn* nil)
@@ -25,9 +26,7 @@ We initialize the probabilistic backends and the provider cascade which determin
(setf (gethash name *probabilistic-backends*) fn))
#+end_src
** Neural Dispatch (Probabilistic)
The `probabilistic-call` function manages the cascade of neural providers. If the primary provider fails, it automatically falls back to the next one in the list.
** Probabilistic Reasoning (probabilistic-call)
#+begin_src lisp :tangle ../src/reason.lisp
(defun probabilistic-call (prompt &key (system-prompt "You are the Probabilistic engine.") (cascade nil) (context nil))
"Dispatches a neural request through the provider cascade. Returns a Lisp plist or a failure log."
@@ -40,19 +39,14 @@ The `probabilistic-call` function manages the cascade of neural providers. If th
(result (if model
(funcall backend-fn prompt system-prompt :model model)
(funcall backend-fn prompt system-prompt))))
;; If the result is valid, return it.
;; If it is an error plist from the gateway, continue the cascade but log it.
(cond ((and (listp result) (eq (getf result :status) :success))
(return (getf result :content)))
((stringp result) (return result))
(t (harness-log "PROBABILISTIC: Backend ~a failed: ~a" backend (getf result :message))))))))
;; Final fallback if all backends in the cascade fail.
(list :type :LOG :payload (list :text "Neural Cascade Failure: All providers exhausted.")))))
#+end_src
** Cognitive Proposal (Think)
The `think` function represents the "intuitive" side of the agent. It identifies the active skill, assembles the global context, and asks the probabilistic engine for a structured action proposal.
#+begin_src lisp :tangle ../src/reason.lisp
(defun think (context)
"Generates a Lisp action proposal based on current context."
@@ -78,28 +72,21 @@ The `think` function represents the "intuitive" side of the agent. It identifies
#+end_src
** Deterministic Verification
Once a proposal is generated, it MUST pass through the deterministic gates. Every skill can register a gate that inspects and potentially modifies or blocks the proposed action.
#+begin_src lisp :tangle ../src/reason.lisp
(defun deterministic-verify (proposed-action context)
"Iterates through all skill deterministic-gates sorted by priority."
(let ((current-action proposed-action)
(skills nil))
;; 1. Collect and sort active gates
(maphash (lambda (name skill) (declare (ignore name)) (when (skill-deterministic-fn skill) (push skill skills))) *skills-registry*)
(setf skills (sort skills #'> :key #'skill-priority))
;; 2. Execute gates sequentially if their trigger allows
(dolist (skill skills)
(let ((trigger (skill-trigger-fn skill))
(gate (skill-deterministic-fn skill)))
(when (or (null trigger) (ignore-errors (funcall trigger context)))
(let ((next-action (funcall gate current-action context)))
;; Interception occurs if the gate returns a signal (LOG/EVENT) AND the original was a REQUEST.
;; If the original was already a LOG/EVENT, we only intercept if the gate returns a NEW signal object.
(let ((original-type (getf current-action :type)))
(let ((original-type (proto-get current-action :type)))
(when (and (listp next-action)
(member (getf next-action :type) '(:LOG :EVENT :log :event))
(member (proto-get next-action :type) '(:LOG :EVENT :log :event))
(or (not (member original-type '(:LOG :EVENT :log :event)))
(not (eq next-action current-action))))
(harness-log "DETERMINISTIC: Intercepted by skill '~a'" (skill-name skill))
@@ -108,19 +95,19 @@ Once a proposal is generated, it MUST pass through the deterministic gates. Ever
current-action))
#+end_src
** The Reason Gate
The entry point for the Reason stage. It orchestrates the transition from probabilistic "thought" to deterministic "verification."
** Reasoning Gate (The Pipeline Stage)
#+begin_src lisp :tangle ../src/reason.lisp
(defun reason-gate (signal)
"Unified Stage: Combines Probabilistic proposals and Deterministic verification."
;; Only process events that haven't been reasoned yet.
(unless (eq (getf signal :type) :EVENT) (return-from reason-gate signal))
(let ((candidate (think signal)))
(if candidate
(setf (getf signal :approved-action) (deterministic-verify candidate signal))
(setf (getf signal :approved-action) nil))
(setf (getf signal :status) :reasoned)
signal))
(let* ((type (proto-get signal :type))
(payload (proto-get signal :payload))
(sensor (proto-get payload :sensor)))
(unless (and (eq type :EVENT) (eq sensor :chat-message))
(return-from reason-gate signal))
(let ((candidate (think signal)))
(if candidate
(setf (getf signal :approved-action) (deterministic-verify candidate signal))
(setf (getf signal :approved-action) nil))
(setf (getf signal :status) :reasoned)
signal)))
#+end_src