fix(chaos): finalized absolute tangle paths via concat and INSTALL_DIR

This commit is contained in:
2026-04-28 18:22:49 -04:00
parent a2d6c5ae38
commit 357efbdb59
35 changed files with 641 additions and 641 deletions

View File

@@ -1,4 +1,4 @@
#+PROPERTY: header-args:lisp :tangle (expand-file-name "harness/act.lisp" (expand-file-name "harness/"))
#+PROPERTY: header-args:lisp :tangle (concat (identity (getenv "INSTALL_DIR")) "/harness/act.lisp")" )
#+TITLE: Stage 3: Act (act.lisp)
#+AUTHOR: Amr
#+FILETAGS: :harness:act:
@@ -47,11 +47,11 @@ Example feedback chain:
#+begin_src lisp
(defvar *default-actuator* :cli
"The actuator used when no explicit target is specified.
Override with DEFAULT_ACTUATOR environment variable.")
Override with DEFAULT_ACTUATOR environment variable.
(defvar *silent-actuators* '(:cli :system-message :emacs)
"List of actuators that don't generate tool-output feedback.
These typically have their own feedback mechanisms (CLI prints directly, etc.)")
These typically have their own feedback mechanisms (CLI prints directly, etc.)
#+end_src
** initialize-actuators: System Bootstrap
@@ -70,20 +70,20 @@ Example feedback chain:
3. :tui - Terminal UI output via reply stream"
;; Load environment configuration
(let ((def (getenv "DEFAULT_ACTUATOR"))
(silent (getenv "SILENT_ACTUATORS")))
(let ((def (getenv "DEFAULT_ACTUATOR)
(silent (getenv "SILENT_ACTUATORS))
;; Set default actuator
(when def
(setf *default-actuator*
(intern (string-upcase def) "KEYWORD")))
(intern (string-upcase def) "KEYWORD))
;; Parse silent actuators list
(when silent
(setf *silent-actuators*
(mapcar (lambda (s)
(intern (string-upcase (string-trim '(#\Space) s))
"KEYWORD"))
"KEYWORD)
(str:split "," silent)))))
;; Register core harness actuators
@@ -179,7 +179,7 @@ Example feedback chain:
(:create-skill
(let* ((filename (getf payload :filename))
(content (getf payload :content))
(skills-dir (merge-pathnames "skills/"
(skills-dir (merge-pathnames ""
(asdf:system-source-directory :opencortex)))
(full-path (merge-pathnames filename skills-dir)))
(with-open-file (out full-path
@@ -273,7 +273,7 @@ Example feedback chain:
"Format a tool result for human-readable display.
Tools return either:
- A plist: (:status :success :content \"...\") or (:status :error :message \"...\")
- A plist: (:status :success :content \"...\ or (:status :error :message \"...\
- A raw value (string, number, etc.)
This function normalizes both formats into a consistent string presentation."
@@ -336,7 +336,7 @@ Example feedback chain:
;; Action was blocked by verification
(progn
(harness-log "ACT BLOCKED: Action failed last-mile deterministic check.")
(harness-log "ACT BLOCKED: Action failed last-mile deterministic check.
(setf (getf signal :approved-action) nil)
(setf approved nil)
(setf feedback verified))
@@ -393,7 +393,7 @@ Example feedback chain:
These tests verify the Act pipeline. Run with:
~(fiveam:run! 'pipeline-act-suite)~
#+begin_src lisp :tangle (expand-file-name "harness/pipeline-act-tests.lisp" (concat (concat (or (getenv "INSTALL_DIR") ".") "/harness") "/tests"))
#+begin_src lisp :tangle pipeline-act-tests.lisp" (concat (concat (or (getenv "INSTALL_DIR ". "/harness "/tests)
(defpackage :opencortex-pipeline-act-tests
(:use :cl :fiveam :opencortex)
(:export #:pipeline-act-suite))
@@ -401,14 +401,14 @@ These tests verify the Act pipeline. Run with:
(in-package :opencortex-pipeline-act-tests)
(def-suite pipeline-act-suite
:description "Test suite for Act pipeline")
:description "Test suite for Act pipeline
(in-suite pipeline-act-suite)
(test test-act-gate-symbolic-guard-bypass
"Verify that act-gate proceeds normally when no skill intercepts."
(clrhash opencortex::*skills-registry*)
(let* ((signal (list :type :EVENT :status nil :depth 0 :approved-action '(:target :cli :payload (:text "Hello"))))
(let* ((signal (list :type :EVENT :status nil :depth 0 :approved-action '(:target :cli :payload (:text "Hello)))
(result (opencortex:act-gate signal)))
(is (eq :acted (getf signal :status)))
(is (null result))))
@@ -421,8 +421,8 @@ These tests verify the Act pipeline. Run with:
:trigger (lambda (ctx) (declare (ignore ctx)) t)
:deterministic (lambda (action ctx)
(declare (ignore action ctx))
(list :type :LOG :payload (list :text "BLOCKED BY SYMBOLIC GUARD"))))
(let* ((signal (list :type :EVENT :status nil :depth 0 :approved-action '(:target :shell :payload (:cmd "ls"))))
(list :type :LOG :payload (list :text "BLOCKED BY SYMBOLIC GUARD)))
(let* ((signal (list :type :EVENT :status nil :depth 0 :approved-action '(:target :shell :payload (:cmd "ls)))
(result (opencortex:act-gate signal)))
(is (eq :acted (getf signal :status)))
(is (not (null result)))

View File

@@ -1,4 +1,4 @@
#+PROPERTY: header-args:lisp :tangle communication.lisp
#+PROPERTY: header-args:lisp :tangle (concat (identity (getenv "INSTALL_DIR")) "/harness/communication.lisp")
#+TITLE: Communication Protocol (communication.lisp)
#+AUTHOR: Amr
#+FILETAGS: :harness:protocol:
@@ -79,7 +79,7 @@ The ~communication.lisp~ module defines the low-level transport and framing logi
** Structural Validation (communication-validator.lisp)
The validator ensures that incoming messages adhere to the strict property list schema of the communication protocol.
#+begin_src lisp :tangle communication-validator.lisp
#+begin_src lisp :tangle (concat (identity (getenv "INSTALL_DIR")) "/harness/communication-validator.lisp")
(in-package :opencortex)
(defun validate-communication-protocol-schema (msg)

View File

@@ -1,4 +1,4 @@
#+PROPERTY: header-args:lisp :tangle (expand-file-name "harness/context.lisp" (expand-file-name "harness/"))
#+PROPERTY: header-args:lisp :tangle (concat (identity (getenv "INSTALL_DIR")) "/harness/context.lisp")" )
#+TITLE: Peripheral Vision (context.lisp)
#+AUTHOR: Amr
#+FILETAGS: :harness:context:
@@ -66,7 +66,7 @@ Identifies headlines tagged with ~project~ that have not yet reached a terminal
#+begin_src lisp
(defun context-get-active-projects ()
"Returns headlines tagged as 'project' that are not yet marked DONE."
(remove-if (lambda (obj) (equal (getf (org-object-attributes obj) :TODO-STATE) "DONE"))
(remove-if (lambda (obj) (equal (getf (org-object-attributes obj) :TODO-STATE) "DONE)
(context-query-store :tag "project" :type :HEADLINE)))
#+end_src
@@ -100,7 +100,7 @@ Reads the raw literate Org source of a specific skill. This is a foundational ca
(defun context-get-skill-source (skill-name)
"Reads the raw literate source of a specific skill for inspection."
(let* ((filename (format nil "~a.org" skill-name))
(skills-dir-str (or (getenv "SKILLS_DIR") (namestring (merge-pathnames "notes/" (user-homedir-pathname)))))
(skills-dir-str (or (getenv "SKILLS_DIR (namestring (merge-pathnames "notes/" (user-homedir-pathname)))))
(skills-dir (uiop:ensure-directory-pathname (context-resolve-path skills-dir-str)))
(full-path (merge-pathnames filename skills-dir)))
(if (uiop:file-exists-p full-path) (uiop:read-file-string full-path) nil)))
@@ -112,7 +112,7 @@ Retrieves the most recent entries from the harness's internal circular log buffe
#+begin_src lisp
(defun context-get-system-logs (&optional limit)
"Retrieves the most recent lines from the harness's internal log."
(let ((log-limit (or limit (ignore-errors (parse-integer (getenv "CONTEXT_LOG_LIMIT"))) 20)))
(let ((log-limit (or limit (ignore-errors (parse-integer (getenv "CONTEXT_LOG_LIMIT)) 20)))
(bt:with-lock-held (*logs-lock*)
(let ((count (min log-limit (length *system-logs*))))
(subseq *system-logs* 0 count)))))
@@ -134,12 +134,12 @@ The semantic threshold is externalized to `CONTEXT_SEMANTIC_THRESHOLD`.
"Recursively renders an org-object and its children to an Org string using a Foveal-Peripheral Hybrid model."
(let* ((id (org-object-id obj))
(is-foveal (equal id foveal-id))
(title (or (getf (org-object-attributes obj) :TITLE) "Untitled"))
(title (or (getf (org-object-attributes obj) :TITLE) "Untitled)
(content (org-object-content obj))
(children (org-object-children obj))
(stars (make-string depth :initial-element #\*))
(obj-vector (org-object-vector obj))
(threshold (or semantic-threshold (ignore-errors (read-from-string (getenv "CONTEXT_SEMANTIC_THRESHOLD"))) 0.75))
(threshold (or semantic-threshold (ignore-errors (read-from-string (getenv "CONTEXT_SEMANTIC_THRESHOLD)) 0.75))
(similarity (if (and foveal-vector obj-vector (not is-foveal))
(cosine-similarity foveal-vector obj-vector)
0.0))
@@ -148,13 +148,13 @@ The semantic threshold is externalized to `CONTEXT_SEMANTIC_THRESHOLD`.
;; We always render the foveal node and its immediate children.
;; We render deeper nodes ONLY if they are semantically relevant.
(should-render (or (<= depth 2) is-foveal is-semantically-relevant))
(output ""))
(output
(when should-render
(setf output (format nil "~a ~a~%:PROPERTIES:~%:ID: ~a~%" stars title id))
(when is-semantically-relevant
(setf output (concatenate 'string output (format nil ":SEMANTIC_SCORE: ~,2f~%" similarity))))
(setf output (concatenate 'string output (format nil ":END:~%")))
(setf output (concatenate 'string output (format nil ":END:~%))
;; Only include full body content if this is the Foveal focus or highly relevant
(when (and content (or is-foveal is-semantically-relevant))
@@ -204,12 +204,12 @@ The primary entry point for context generation. This function identifies active
(ignore-errors (getf (getf signal :payload) :target-id))))
(projects (context-get-active-projects))
(output "GLOBAL MEMEX AWARENESS (Peripheral Vision):
"))
)
(if projects
(dolist (project projects)
(setf output (concatenate 'string output
(context-render-to-org project :foveal-id foveal-id))))
(setf output (concatenate 'string output "No active projects found.~%")))
(setf output (concatenate 'string output "No active projects found.~%))
output))
#+end_src
@@ -217,32 +217,32 @@ The primary entry point for context generation. This function identifies active
Following the Engineering Standards, the peripheral vision extraction and rendering logic must be empirically verified.
** Test Suite Context
#+begin_src lisp :tangle (expand-file-name "harness/peripheral-vision-tests.lisp" (concat (concat (or (getenv "INSTALL_DIR") ".") "/harness") "/tests"))
#+begin_src lisp :tangle peripheral-vision-tests.lisp" (concat (concat (or (getenv "INSTALL_DIR ". "/harness "/tests)
(defpackage :opencortex-peripheral-vision-tests
(:use :cl :fiveam :opencortex)
(:export #:vision-suite))
(in-package :opencortex-peripheral-vision-tests)
(def-suite vision-suite
:description "Verification of Foveal-Peripheral context model.")
:description "Verification of Foveal-Peripheral context model.
(in-suite vision-suite)
#+end_src
** Foveal Rendering Test
Verify that the foveal target is rendered with content, while siblings are skeletal.
#+begin_src lisp :tangle (expand-file-name "harness/peripheral-vision-tests.lisp" (concat (concat (or (getenv "INSTALL_DIR") ".") "/harness") "/tests"))
#+begin_src lisp :tangle peripheral-vision-tests.lisp" (concat (concat (or (getenv "INSTALL_DIR ". "/harness "/tests)
(test test-foveal-rendering
"Verify that the foveal target is rendered with content, while siblings are skeletal."
(clrhash opencortex::*memory*)
(let* ((ast '(:type :HEADLINE :properties (:ID "proj-root" :TITLE "Project" :TAGS "project")
:contents ((:type :HEADLINE :properties (:ID "node-foveal" :TITLE "Foveal Node")
(let* ((ast '(:type :HEADLINE :properties (:ID "proj-root" :TITLE "Project" :TAGS "project
:contents ((:type :HEADLINE :properties (:ID "node-foveal" :TITLE "Foveal Node
:raw-content "FOVEAL CONTENT" :contents nil)
(:type :HEADLINE :properties (:ID "node-peripheral" :TITLE "Peripheral Node")
(:type :HEADLINE :properties (:ID "node-peripheral" :TITLE "Peripheral Node
:raw-content "PERIPHERAL CONTENT" :contents nil)))))
(ingest-ast ast)
;; Test both foveal focus in signal top-level and in payload (legacy)
(let ((output (context-assemble-global-awareness (list :foveal-focus "node-foveal"))))
(let ((output (context-assemble-global-awareness (list :foveal-focus "node-foveal)))
(is (search "FOVEAL CONTENT" output))
(is (search "* Peripheral Node" output))
(is (not (search "PERIPHERAL CONTENT" output))))))
@@ -251,12 +251,12 @@ Verify that the foveal target is rendered with content, while siblings are skele
** Awareness Budget Test
Verify that context-assemble-global-awareness handles multiple projects correctly.
#+begin_src lisp :tangle (expand-file-name "harness/peripheral-vision-tests.lisp" (concat (concat (or (getenv "INSTALL_DIR") ".") "/harness") "/tests"))
#+begin_src lisp :tangle peripheral-vision-tests.lisp" (concat (concat (or (getenv "INSTALL_DIR ". "/harness "/tests)
(test test-awareness-budget
"Verify that context-assemble-global-awareness handles multiple projects."
(clrhash opencortex::*memory*)
(ingest-ast '(:type :HEADLINE :properties (:ID "p1" :TITLE "Project 1" :TAGS "project") :contents nil))
(ingest-ast '(:type :HEADLINE :properties (:ID "p2" :TITLE "Project 2" :TAGS "project") :contents nil))
(ingest-ast '(:type :HEADLINE :properties (:ID "p1" :TITLE "Project 1" :TAGS "project :contents nil))
(ingest-ast '(:type :HEADLINE :properties (:ID "p2" :TITLE "Project 2" :TAGS "project :contents nil))
(let ((output (context-assemble-global-awareness)))
(is (search "Project 1" output))
(is (search "Project 2" output))))

View File

@@ -23,65 +23,65 @@ Common Lisp's `getenv` is strictly typed in SBCL. The Doctor must ensure that mi
* Phase B: Protocol (Success Criteria)
** Package Context
#+begin_src lisp :tangle (expand-file-name "harness/doctor-tests.lisp" (concat (concat (or (getenv "INSTALL_DIR") ".") "/harness") "/tests"))
#+begin_src lisp :tangle doctor-tests.lisp" (concat (concat (or (getenv "INSTALL_DIR ". "/harness "/tests)
(defpackage :opencortex-doctor-tests
(:use :cl :fiveam :opencortex)
(:export #:doctor-suite))
#+end_src
#+begin_src lisp :tangle (expand-file-name "harness/doctor-tests.lisp" (concat (concat (or (getenv "INSTALL_DIR") ".") "/harness") "/tests"))
#+begin_src lisp :tangle doctor-tests.lisp" (concat (concat (or (getenv "INSTALL_DIR ". "/harness "/tests)
(in-package :opencortex-doctor-tests)
#+end_src
#+begin_src lisp :tangle (expand-file-name "harness/doctor-tests.lisp" (concat (concat (or (getenv "INSTALL_DIR") ".") "/harness") "/tests"))
(def-suite doctor-suite :description "Verification of the System Doctor diagnostic logic")
#+begin_src lisp :tangle doctor-tests.lisp" (concat (concat (or (getenv "INSTALL_DIR ". "/harness "/tests)
(def-suite doctor-suite :description "Verification of the System Doctor diagnostic logic
#+end_src
#+begin_src lisp :tangle (expand-file-name "harness/doctor-tests.lisp" (concat (concat (or (getenv "INSTALL_DIR") ".") "/harness") "/tests"))
#+begin_src lisp :tangle doctor-tests.lisp" (concat (concat (or (getenv "INSTALL_DIR ". "/harness "/tests)
(in-suite doctor-suite)
#+end_src
** Dependency Tests
#+begin_src lisp :tangle (expand-file-name "harness/doctor-tests.lisp" (concat (concat (or (getenv "INSTALL_DIR") ".") "/harness") "/tests"))
#+begin_src lisp :tangle doctor-tests.lisp" (concat (concat (or (getenv "INSTALL_DIR ". "/harness "/tests)
(test test-dependency-check-fail
"Verify that missing binaries are correctly identified as failures."
(let ((opencortex::*doctor-required-binaries* '("non-existent-binary-123")))
(let ((opencortex::*doctor-required-binaries* '("non-existent-binary-123))
(is (null (opencortex:doctor-check-dependencies)))))
#+end_src
** Environment Tests
#+begin_src lisp :tangle (expand-file-name "harness/doctor-tests.lisp" (concat (concat (or (getenv "INSTALL_DIR") ".") "/harness") "/tests"))
#+begin_src lisp :tangle doctor-tests.lisp" (concat (concat (or (getenv "INSTALL_DIR ". "/harness "/tests)
(test test-env-validation-fail
"Verify that an invalid MEMEX_DIR triggers a critical failure."
(let ((old-m (getenv "MEMEX_DIR"))
(old-s (getenv "SKILLS_DIR")))
(let ((old-m (getenv "MEMEX_DIR)
(old-s (getenv "SKILLS_DIR))
(unwind-protect
(progn
(setf (getenv "MEMEX_DIR") "/non/existent/path/999")
(setf (getenv "MEMEX_DIR "/non/existent/path/999
(is (null (opencortex:doctor-check-env))))
(setf (getenv "MEMEX_DIR") (or old-m ""))
(setf (getenv "SKILLS_DIR") (or old-s "")))))
(setf (getenv "MEMEX_DIR (or old-m
(setf (getenv "SKILLS_DIR (or old-s )))
#+end_src
* Phase C: Implementation (Build)
** Package Context
#+begin_src lisp :tangle (expand-file-name "harness/doctor.lisp" (expand-file-name "harness/"))
#+begin_src lisp :tangle (concat (identity (getenv "INSTALL_DIR")) "/harness/doctor.lisp")" )
(in-package :opencortex)
#+end_src
** Global Configuration
#+begin_src lisp :tangle (expand-file-name "harness/doctor.lisp" (expand-file-name "harness/"))
(defvar *doctor-required-binaries* '("sbcl" "emacs" "git" "socat" "nc")
"List of external binaries required for full system operation.")
#+begin_src lisp :tangle (concat (identity (getenv "INSTALL_DIR")) "/harness/doctor.lisp")" )
(defvar *doctor-required-binaries* '("sbcl" "emacs" "git" "socat" "nc
"List of external binaries required for full system operation.
#+end_src
** Dependency Verification
#+begin_src lisp :tangle (expand-file-name "harness/doctor.lisp" (expand-file-name "harness/"))
#+begin_src lisp :tangle (concat (identity (getenv "INSTALL_DIR")) "/harness/doctor.lisp")" )
(defun doctor-check-dependencies ()
"Verifies that required external binaries are available in the PATH via a shell probe."
(let ((all-ok t))
(harness-log "DOCTOR: Checking system dependencies...")
(harness-log "DOCTOR: Checking system dependencies...
(dolist (dep *doctor-required-binaries*)
(let ((path (ignore-errors
(uiop:run-program (list "which" dep)
@@ -95,15 +95,15 @@ Common Lisp's `getenv` is strictly typed in SBCL. The Doctor must ensure that mi
#+end_src
** Environment & XDG Validation
#+begin_src lisp :tangle (expand-file-name "harness/doctor.lisp" (expand-file-name "harness/"))
#+begin_src lisp :tangle (concat (identity (getenv "INSTALL_DIR")) "/harness/doctor.lisp")" )
(defun doctor-check-env ()
"Validates XDG directories and environment configuration against the POSIX standard."
(harness-log "DOCTOR: Checking XDG environment...")
(harness-log "DOCTOR: Checking XDG environment...
(let ((all-ok t)
(config-dir (getenv "OC_CONFIG_DIR"))
(data-dir (getenv "OC_DATA_DIR"))
(state-dir (getenv "OC_STATE_DIR"))
(memex-dir (getenv "MEMEX_DIR")))
(config-dir (getenv "OC_CONFIG_DIR)
(data-dir (getenv "OC_DATA_DIR)
(state-dir (getenv "OC_STATE_DIR)
(memex-dir (getenv "MEMEX_DIR))
(flet ((check-dir (name path critical)
(if (and path (> (length path) 0))
@@ -124,42 +124,42 @@ Common Lisp's `getenv` is strictly typed in SBCL. The Doctor must ensure that mi
#+end_src
** LLM Connectivity
#+begin_src lisp :tangle (expand-file-name "harness/doctor.lisp" (expand-file-name "harness/"))
#+begin_src lisp :tangle (concat (identity (getenv "INSTALL_DIR")) "/harness/doctor.lisp")" )
(defun doctor-check-llm ()
"Tests connectivity to primary LLM providers. Non-critical fallback allowed."
(harness-log "DOCTOR: Checking LLM connectivity...")
(let ((openrouter-key (getenv "OPENROUTER_API_KEY")))
(harness-log "DOCTOR: Checking LLM connectivity...
(let ((openrouter-key (getenv "OPENROUTER_API_KEY))
(if (and openrouter-key (> (length openrouter-key) 0))
(progn
(harness-log " [OK] OpenRouter API Key detected.")
(harness-log " [OK] OpenRouter API Key detected.
t)
(progn
(harness-log " [WARN] No OpenRouter API Key. Falling back to local inference only.")
(harness-log " [WARN] No OpenRouter API Key. Falling back to local inference only.
t))))
#+end_src
** Orchestration
#+begin_src lisp :tangle (expand-file-name "harness/doctor.lisp" (expand-file-name "harness/"))
#+begin_src lisp :tangle (concat (identity (getenv "INSTALL_DIR")) "/harness/doctor.lisp")" )
(defun doctor-run-all ()
"Executes the full diagnostic suite and returns T if system is healthy."
(harness-log "==================================================")
(harness-log " OPENCORTEX DOCTOR: Commencing Health Check")
(harness-log "==================================================")
(harness-log "==================================================
(harness-log " OPENCORTEX DOCTOR: Commencing Health Check
(harness-log "==================================================
(let ((dep-ok (doctor-check-dependencies))
(env-ok (doctor-check-env))
(llm-ok (doctor-check-llm)))
(harness-log "==================================================")
(harness-log "==================================================
(if (and dep-ok env-ok)
(progn
(harness-log " ✓ SYSTEM HEALTHY: Ready for ignition.")
(harness-log " ✓ SYSTEM HEALTHY: Ready for ignition.
t)
(progn
(harness-log " ✗ SYSTEM UNHEALTHY: Fix the errors above.")
(harness-log " SYSTEM UNHEALTHY: Fix the errors above.
nil))))
#+end_src
** CLI Entry Point
#+begin_src lisp :tangle (expand-file-name "harness/doctor.lisp" (expand-file-name "harness/"))
#+begin_src lisp :tangle (concat (identity (getenv "INSTALL_DIR")) "/harness/doctor.lisp")" )
(defun doctor-main ()
"Entry point for the 'doctor' CLI command."
(if (doctor-run-all)

View File

@@ -1,4 +1,4 @@
#+PROPERTY: header-args:lisp :tangle (expand-file-name "harness/loop.lisp" (expand-file-name "harness/"))
#+PROPERTY: header-args:lisp :tangle (concat (identity (getenv "INSTALL_DIR")) "/harness/loop.lisp")" )
#+TITLE: The Metabolic Loop (loop.lisp)
#+AUTHOR: Amr
#+FILETAGS: :harness:loop:
@@ -73,14 +73,14 @@ The loop operates in a multi-threaded environment:
(defvar *interrupt-flag* nil
"Atomic flag set by signal handlers to trigger graceful shutdown.
Using a dedicated variable avoids race conditions in interrupt handling.")
Using a dedicated variable avoids race conditions in interrupt handling.
(defvar *interrupt-lock* (bt:make-lock "harness-interrupt-lock")
(defvar *interrupt-lock* (bt:make-lock "harness-interrupt-lock
"Mutex protecting *interrupt-flag* access.
Locking is required because SBCL's interrupt handlers run in uncertain contexts.")
Locking is required because SBCL's interrupt handlers run in uncertain contexts.
(defvar *heartbeat-thread* nil
"Handle to the heartbeat thread, allowing explicit termination on shutdown.")
"Handle to the heartbeat thread, allowing explicit termination on shutdown.
#+end_src
* The Metabolic Pipeline
@@ -111,12 +111,12 @@ The depth counter prevents infinite recursion—a signal that generates another
(let ((depth (getf current-signal :depth 0))
(meta (getf current-signal :meta)))
(when (> depth 10)
(harness-log "METABOLISM ERROR: Max recursion depth reached.")
(harness-log "METABOLISM ERROR: Max recursion depth reached.
(return nil))
;; Check for graceful shutdown interrupt
(when (bt:with-lock-held (*interrupt-lock*) *interrupt-flag*)
(harness-log "METABOLISM: Interrupted by shutdown signal.")
(harness-log "METABOLISM: Interrupted by shutdown signal.
(bt:with-lock-held (*interrupt-lock*) (setf *interrupt-flag* nil))
(return nil))
@@ -149,7 +149,7 @@ The depth counter prevents infinite recursion—a signal that generates another
;; Only rollback memory on critical errors, not transient tool failures
;; This prevents losing recent context due to a single bad API call
(unless (member sensor '(:loop-error :tool-error :syntax-error))
(harness-log "CRITICAL ERROR: Initiating Micro-Rollback.")
(harness-log "CRITICAL ERROR: Initiating Micro-Rollback.
(rollback-memory 0))
;; At deep recursion or known error types, terminate gracefully
@@ -188,10 +188,10 @@ The heartbeat thread ensures the agent remains alive even without external input
#+begin_src lisp
(defvar *auto-save-interval* 300
"Interval in seconds between automatic memory saves.
Defaults to 300 seconds (5 minutes). Set via MEMORY_AUTO_SAVE_INTERVAL env var.")
Defaults to 300 seconds (5 minutes). Set via MEMORY_AUTO_SAVE_INTERVAL env var.
(defvar *heartbeat-save-counter* 0
"Tracks heartbeats since last save, used to calculate auto-save timing.")
"Tracks heartbeats since last save, used to calculate auto-save timing.
#+end_src
** start-heartbeat: The Pulsing Heart
@@ -210,8 +210,8 @@ The heartbeat thread ensures the agent remains alive even without external input
- HEARTBEAT_INTERVAL: Seconds between heartbeats (default: 60)
- MEMORY_AUTO_SAVE_INTERVAL: Seconds between auto-saves (default: 300)"
(let ((interval (or (ignore-errors (parse-integer (getenv "HEARTBEAT_INTERVAL"))) 60))
(auto-save (or (ignore-errors (parse-integer (getenv "MEMORY_AUTO_SAVE_INTERVAL"))) *auto-save-interval*)))
(let ((interval (or (ignore-errors (parse-integer (getenv "HEARTBEAT_INTERVAL)) 60))
(auto-save (or (ignore-errors (parse-integer (getenv "MEMORY_AUTO_SAVE_INTERVAL)) *auto-save-interval*)))
(setf *auto-save-interval* auto-save)
(setf *heartbeat-save-counter* 0)
@@ -235,7 +235,7 @@ The heartbeat thread ensures the agent remains alive even without external input
:payload (list :sensor :heartbeat
:unix-time (get-universal-time)))))
:name "opencortex-heartbeat")))))
:name "opencortex-heartbeat))))
#+end_src
* Main Entry Point
@@ -245,7 +245,7 @@ The heartbeat thread ensures the agent remains alive even without external input
#+begin_src lisp
(defvar *shutdown-save-enabled* t
"When T, save memory to disk on graceful shutdown.
Disable for testing or when memory persistence is handled externally.")
Disable for testing or when memory persistence is handled externally.
#+end_src
** main: System Bootstrap and Idle Loop
@@ -275,7 +275,7 @@ The main function orchestrates system startup:
The idle loop checks for interrupts and saves memory before exit."
;; Step 1: Load environment variables from standard location
(let* ((home (getenv "HOME"))
(let* ((home (getenv "HOME)
(env-file (uiop:merge-pathnames*
".local/share/opencortex/.env"
(uiop:ensure-directory-pathname home))))
@@ -298,19 +298,19 @@ The main function orchestrates system startup:
(sb-sys:enable-interrupt sb-unix:sigint
(lambda (sig code scp)
(declare (ignore sig code scp))
(harness-log "SHUTDOWN: SIGINT received. Saving memory...")
(harness-log "SHUTDOWN: SIGINT received. Saving memory...
(when *shutdown-save-enabled*
(save-memory-to-disk))
(uiop:quit 0)))
;; Step 7: Idle loop - sleep in chunks, checking for interrupts
(let ((sleep-interval (or (ignore-errors
(parse-integer (getenv "DAEMON_SLEEP_INTERVAL")))
(parse-integer (getenv "DAEMON_SLEEP_INTERVAL))
3600)))
(loop
;; Check for interrupt before each sleep cycle
(when (bt:with-lock-held (*interrupt-lock*) *interrupt-flag*)
(harness-log "SHUTDOWN: Interrupt flag set. Saving memory...")
(harness-log "SHUTDOWN: Interrupt flag set. Saving memory...
(when *shutdown-save-enabled*
(save-memory-to-disk))
(return))
@@ -324,7 +324,7 @@ The main function orchestrates system startup:
These tests verify the metabolic loop and error recovery. Run with:
~(fiveam:run! 'immune-suite)~
#+begin_src lisp :tangle (expand-file-name "harness/immune-system-tests.lisp" (concat (concat (or (getenv "INSTALL_DIR") ".") "/harness") "/tests"))
#+begin_src lisp :tangle immune-system-tests.lisp" (concat (concat (or (getenv "INSTALL_DIR ". "/harness "/tests)
(defpackage :opencortex-immune-system-tests
(:use :cl :fiveam :opencortex)
(:export #:immune-suite))
@@ -332,7 +332,7 @@ These tests verify the metabolic loop and error recovery. Run with:
(in-package :opencortex-immune-system-tests)
(def-suite immune-suite
:description "Verification of the Immune System (Core Error Hooks)")
:description "Verification of the Immune System (Core Error Hooks)
(in-suite immune-suite)
@@ -342,9 +342,9 @@ These tests verify the metabolic loop and error recovery. Run with:
(opencortex:defskill :evil-skill
:priority 100
:trigger (lambda (ctx) (eq (getf (getf ctx :payload) :sensor) :user-input))
:probabilistic (lambda (ctx) (error "CRITICAL BRAIN FAILURE"))
:probabilistic (lambda (ctx) (error "CRITICAL BRAIN FAILURE)
:deterministic nil)
(opencortex:harness-log "CLEAN LOG")
(opencortex:harness-log "CLEAN LOG
(opencortex:process-signal '(:type :EVENT :payload (:sensor :user-input)))
(let ((logs (opencortex:context-get-system-logs 20)))
(is (not (null (find-if (lambda (line) (search "CRITICAL BRAIN FAILURE" line)) logs))))))

View File

@@ -9,7 +9,7 @@ The *System Manifest* defines the structural components of the OpenCortex. It se
* Implementation
** Main System
#+begin_src lisp :tangle (expand-file-name "opencortex.asd")
#+begin_src lisp :tangle (concat (identity (getenv "INSTALL_DIR")) "/opencortex.asd")
(defsystem :opencortex
:name "opencortex"
:author "Amr Gharbeia"
@@ -18,73 +18,73 @@ The *System Manifest* defines the structural components of the OpenCortex. It se
:description "The Probabilistic-Deterministic Lisp Machine"
:depends-on (:usocket :bordeaux-threads :dexador :uiop :cl-dotenv :cl-ppcre :hunchentoot :ironclad :str :cl-json :uuid)
:serial t
:components ((:file "harness/package")
(:file "harness/skills")
(:file "harness/communication")
(:file "harness/communication-validator")
(:file "harness/memory")
(:file "harness/context")
(:file "harness/perceive")
(:file "harness/reason")
(:file "harness/act")
(:file "harness/loop")))
:components ((:file "package
(:file "skills
(:file "communication
(:file "communication-validator
(:file "memory
(:file "context
(:file "perceive
(:file "reason
(:file "act
(:file "loop))
#+end_src
** Test System
#+begin_src lisp :tangle (expand-file-name "opencortex.asd")
#+begin_src lisp :tangle (concat (identity (getenv "INSTALL_DIR")) "/opencortex.asd")
(defsystem :opencortex/tests
:depends-on (:opencortex :fiveam)
:components ((:file "tests/pipeline-act-tests")
(:file "tests/boot-sequence-tests")
(:file "tests/immune-system-tests")
(:file "tests/memory-tests")
(:file "tests/pipeline-perceive-tests")
(:file "tests/pipeline-reason-tests")
(:file "tests/peripheral-vision-tests")
(:file "tests/emacs-edit-tests")
(:file "tests/engineering-standards-tests")
(:file "tests/lisp-utils-tests")
(:file "tests/literate-programming-tests")
(:file "tests/self-edit-tests")
(:file "tests/tool-permissions-tests")
(:file "tests/diagnostics-tests")
(:file "tests/config-manager-tests")
(:file "tests/gateway-manager-tests")
(:file "tests/tui-tests")
(:file "tests/llm-gateway-tests")))
:components ((:file "pipeline-act-tests
(:file "boot-sequence-tests
(:file "immune-system-tests
(:file "memory-tests
(:file "pipeline-perceive-tests
(:file "pipeline-reason-tests
(:file "peripheral-vision-tests
(:file "emacs-edit-tests
(:file "engineering-standards-tests
(:file "lisp-utils-tests
(:file "literate-programming-tests
(:file "self-edit-tests
(:file "tool-permissions-tests
(:file "diagnostics-tests
(:file "config-manager-tests
(:file "gateway-manager-tests
(:file "tui-tests
(:file "llm-gateway-tests))
#+end_src
** TUI System
#+begin_src lisp :tangle (expand-file-name "opencortex.asd")
#+begin_src lisp :tangle (concat (identity (getenv "INSTALL_DIR")) "/opencortex.asd")
(defsystem :opencortex/tui
:depends-on (:opencortex :croatoan :usocket :bordeaux-threads)
:components ((:file "harness/tui-client")))
:components ((:file "tui-client))
#+end_src
** Test Orchestrator
#+begin_src lisp :tangle (expand-file-name "run-all-tests.lisp")
#+begin_src lisp :tangle (concat (identity (getenv "INSTALL_DIR")) "/harness/run-all-tests.lisp")
(load (merge-pathnames "quicklisp/setup.lisp" (user-homedir-pathname)))
(let ((oc-dir (or (getenv "OC_DATA_DIR")
(namestring (truename "./")))))
(let ((oc-dir (or (getenv "OC_DATA_DIR
(namestring (truename "./))))
(push (uiop:ensure-directory-pathname oc-dir) asdf:*central-registry*))
(ql:quickload '(:opencortex :opencortex/tui :opencortex/tests) :silent t)
(format t "~%=== Initializing Skills BEFORE loading tests ===~%")
(format t "~%=== Initializing Skills BEFORE loading tests ===~%
(opencortex:initialize-all-skills)
(format t "~%=== Running ALL Test Suites ===~%")
(format t "~%=== Running ALL Test Suites ===~%
(dolist (suite-spec '(("OPENCORTEX-BOOT-TESTS" "BOOT-SUITE")
("OPENCORTEX-COMMUNICATION-TESTS" "COMMUNICATION-PROTOCOL-SUITE")
("OPENCORTEX-PIPELINE-ACT-TESTS" "PIPELINE-ACT-SUITE")
("OPENCORTEX-MEMORY-TESTS" "MEMORY-SUITE")
("OPENCORTEX-ENGINEERING-STANDARDS-TESTS" "ENGINEERING-STANDARDS-SUITE")
("OPENCORTEX-DIAGNOSTICS-TESTS" "DIAGNOSTICS-SUITE")
("OPENCORTEX-GATEWAY-MANAGER-TESTS" "GATEWAY-SUITE")
("OPENCORTEX-TUI-TESTS" "TUI-SUITE")
("OPENCORTEX-LLM-GATEWAY-TESTS" "LLM-GATEWAY-SUITE")))
(dolist (suite-spec '(("OPENCORTEX-BOOT-TESTS" "BOOT-SUITE
("OPENCORTEX-COMMUNICATION-TESTS" "COMMUNICATION-PROTOCOL-SUITE
("OPENCORTEX-PIPELINE-ACT-TESTS" "PIPELINE-ACT-SUITE
("OPENCORTEX-MEMORY-TESTS" "MEMORY-SUITE
("OPENCORTEX-ENGINEERING-STANDARDS-TESTS" "ENGINEERING-STANDARDS-SUITE
("OPENCORTEX-DIAGNOSTICS-TESTS" "DIAGNOSTICS-SUITE
("OPENCORTEX-GATEWAY-MANAGER-TESTS" "GATEWAY-SUITE
("OPENCORTEX-TUI-TESTS" "TUI-SUITE
("OPENCORTEX-LLM-GATEWAY-TESTS" "LLM-GATEWAY-SUITE))
(let ((pkg (find-package (first suite-spec))))
(when pkg
(let ((suite-sym (find-symbol (second suite-spec) pkg)))
@@ -92,6 +92,6 @@ The *System Manifest* defines the structural components of the OpenCortex. It se
(format t "~&--- Suite: ~A ---~%" (first suite-spec))
(fiveam:run! suite-sym))))))
(format t "~%=== ALL TESTS COMPLETE ===~%")
(format t "~%=== ALL TESTS COMPLETE ===~%
#+end_src

View File

@@ -1,4 +1,4 @@
#+PROPERTY: header-args:lisp :tangle memory.lisp
#+PROPERTY: header-args:lisp :tangle (concat (identity (getenv "INSTALL_DIR")) "/harness/memory.lisp")
#+TITLE: The System Memory (memory.lisp)
#+AUTHOR: Amr
#+FILETAGS: :harness:memory:
@@ -32,14 +32,14 @@ flowchart TD
#+end_src
** Package Context
#+begin_src lisp :tangle memory.lisp" )
#+begin_src lisp :tangle (concat (identity (getenv "INSTALL_DIR")) "/harness/memory.lisp")" )
(in-package :opencortex)
#+end_src
** The Object Repository
The `*memory*` is the global hash table that holds every Org element by its unique ID. This is the "live RAM" of the agent's memory.
#+begin_src lisp :tangle memory.lisp" )
#+begin_src lisp :tangle (concat (identity (getenv "INSTALL_DIR")) "/harness/memory.lisp")" )
(defvar *memory* (make-hash-table :test 'equal))
(defvar *history-store* (make-hash-table :test 'equal)
@@ -49,7 +49,7 @@ The `*memory*` is the global hash table that holds every Org element by its uniq
** The Data Structure (org-object)
Every element in the Memex (headlines, paragraphs, etc.) is represented by an `org-object` structure. It contains both semantic metadata (attributes, content) and structural metadata (parent/child pointers, Merkle hashes).
#+begin_src lisp :tangle memory.lisp" )
#+begin_src lisp :tangle (concat (identity (getenv "INSTALL_DIR")) "/harness/memory.lisp")" )
(defstruct org-object
id type attributes content vector parent-id children version last-sync hash)
@@ -61,7 +61,7 @@ Every element in the Memex (headlines, paragraphs, etc.) is represented by an `o
** Merkle Tree Integrity (compute-merkle-hash)
The `compute-merkle-hash` function ensures the cryptographic integrity of the knowledge graph. A node's hash depends on its own properties and the hashes of all its children. This creates a recursive fingerprint where any change to a single note propagates up to the root hash.
#+begin_src lisp :tangle memory.lisp" )
#+begin_src lisp :tangle (concat (identity (getenv "INSTALL_DIR")) "/harness/memory.lisp")" )
(defun compute-merkle-hash (id type attributes content child-hashes)
"Computes a SHA-256 Merkle hash for a node based on its core properties and children's hashes."
(let* ((alist (loop for (k v) on attributes by #'cddr collect (cons k v)))
@@ -78,7 +78,7 @@ The `compute-merkle-hash` function ensures the cryptographic integrity of the kn
** Ingesting the AST (ingest-ast)
The `ingest-ast` function is the primary bridge between the external world (Emacs/JSON) and the internal Lisp machine. It recursively parses an Org-mode Abstract Syntax Tree (AST) into `org-object` structures and registers them in the store.
#+begin_src lisp :tangle memory.lisp" )
#+begin_src lisp :tangle (concat (identity (getenv "INSTALL_DIR")) "/harness/memory.lisp")" )
(defun ingest-ast (ast &optional parent-id)
"Parses an Org AST into the recursive Lisp Memory with Merkle hashing."
(let* ((type (getf ast :type))
@@ -117,7 +117,7 @@ The `ingest-ast` function is the primary bridge between the external world (Emac
** Memory Snapshots (snapshot-memory)
Because objects are stored immutably in the `*history-store*`, a snapshot is a lightweight shallow copy of the active `*memory*` pointers. The system maintains a rolling buffer of 20 snapshots, allowing for near-instant, zero-cost rollback.
#+begin_src lisp :tangle memory.lisp" )
#+begin_src lisp :tangle (concat (identity (getenv "INSTALL_DIR")) "/harness/memory.lisp")" )
(defvar *object-store-snapshots* nil)
(defun copy-hash-table (hash-table)
@@ -144,7 +144,7 @@ Because objects are stored immutably in the `*history-store*`, a snapshot is a l
** Memory Rollback (rollback-memory)
Restores the state of the Memex from one of the previous snapshots.
#+begin_src lisp :tangle memory.lisp" )
#+begin_src lisp :tangle (concat (identity (getenv "INSTALL_DIR")) "/harness/memory.lisp")" )
(defun rollback-memory (&optional (index 0))
"Restores the Memory to a previously captured snapshot using immutable history pointers."
(let ((snapshot (nth index *object-store-snapshots*)))
@@ -157,7 +157,7 @@ Restores the state of the Memex from one of the previous snapshots.
** Disk Persistence (save-memory / load-memory)
Essential for surviving crashes. Saves the in-memory hash tables to disk and loads them back on restart.
#+begin_src lisp :tangle memory.lisp" )
#+begin_src lisp :tangle (concat (identity (getenv "INSTALL_DIR")) "/harness/memory.lisp")" )
(defvar *memory-snapshot-path* nil
"Path to the memory snapshot file. Set from MEMORY_SNAPSHOT_PATH env or default.
@@ -210,7 +210,7 @@ Reconstitutes alists into hash tables."
** Semantic Search (get-embedding, semantic-search)
Support for vector embeddings via Ollama and semantic search with cosine similarity.
#+begin_src lisp :tangle memory.lisp" )
#+begin_src lisp :tangle (concat (identity (getenv "INSTALL_DIR")) "/harness/memory.lisp")" )
(defvar *embedding-cache* (make-hash-table :test 'equal)
"Cache for embeddings to avoid redundant API calls.
@@ -259,7 +259,7 @@ Returns up to LIMIT objects with similarity >= MIN-SIMILARITY, sorted by similar
#+end_src
** Cognitive Tool: Semantic Search
#+begin_src lisp :tangle memory.lisp" )
#+begin_src lisp :tangle (concat (identity (getenv "INSTALL_DIR")) "/harness/memory.lisp")" )
(def-cognitive-tool :semantic-search
"Searches memory for objects semantically similar to a query."
((:query :type :string :description "The search query.
@@ -272,7 +272,7 @@ Returns up to LIMIT objects with similarity >= MIN-SIMILARITY, sorted by similar
#+end_src
** Cognitive Tool: Generate Embeddings
#+begin_src lisp :tangle memory.lisp" )
#+begin_src lisp :tangle (concat (identity (getenv "INSTALL_DIR")) "/harness/memory.lisp")" )
(def-cognitive-tool :generate-embeddings
"Generates vector embeddings for given text via the configured embedding backend (Ollama)."
((:texts :type :list :description "List of text strings to embed.)
@@ -295,7 +295,7 @@ Returns up to LIMIT objects with similarity >= MIN-SIMILARITY, sorted by similar
** Lookup Utilities
Basic functions for retrieving objects by ID or type.
#+begin_src lisp :tangle memory.lisp" )
#+begin_src lisp :tangle (concat (identity (getenv "INSTALL_DIR")) "/harness/memory.lisp")" )
(defun org-id-new ()
"Generates a new UUID string for Org-mode identification."
(string-downcase (format nil "~a" (uuid:make-v4-uuid))))
@@ -325,7 +325,7 @@ Basic functions for retrieving objects by ID or type.
** Structural Helpers
Utility functions for AST traversal and path resolution.
#+begin_src lisp :tangle memory.lisp" )
#+begin_src lisp :tangle (concat (identity (getenv "INSTALL_DIR")) "/harness/memory.lisp")" )
(defun find-headline-missing-id (ast)
"Traverses an AST to find headlines that lack an :ID: property."
(when (listp ast)

View File

@@ -1,4 +1,4 @@
#+PROPERTY: header-args:lisp :tangle package.lisp
#+PROPERTY: header-args:lisp :tangle (concat (identity (getenv "INSTALL_DIR")) "/harness/package.lisp")
#+TITLE: System Interface (package.lisp)
#+AUTHOR: Amr
#+FILETAGS: :harness:interface:

View File

@@ -1,4 +1,4 @@
#+PROPERTY: header-args:lisp :tangle (expand-file-name "harness/perceive.lisp" (expand-file-name "harness/"))
#+PROPERTY: header-args:lisp :tangle (concat (identity (getenv "INSTALL_DIR")) "/harness/perceive.lisp")" )
#+TITLE: Stage 1: Perceive (perceive.lisp)
#+AUTHOR: Amr
#+FILETAGS: :harness:perceive:
@@ -70,7 +70,7 @@ Other sensors (heartbeats, interrupts) are processed synchronously to maintain o
so they run in separate threads to avoid blocking the main pipeline.
Other sensors (:heartbeat, :interrupt, :buffer-update) are processed
synchronously to maintain temporal ordering.")
synchronously to maintain temporal ordering.
#+end_src
** Foveal Focus State
@@ -83,7 +83,7 @@ Other sensors (heartbeats, interrupts) are processed synchronously to maintain o
responses. When editing a specific note, the agent knows which
note you're referring to without needing explicit ID references.
Updated on :point-update events from Emacs.")
Updated on :point-update events from Emacs.
#+end_src
* Stimulus Injection
@@ -113,7 +113,7 @@ Other sensors (heartbeats, interrupts) are processed synchronously to maintain o
;; Ensure metadata exists
(unless meta
(setf meta (list :SOURCE :SYSTEM :SESSION-ID "internal")))
(setf meta (list :SOURCE :SYSTEM :SESSION-ID "internal))
;; Attach reply stream if provided
(when stream
@@ -131,7 +131,7 @@ Other sensors (heartbeats, interrupts) are processed synchronously to maintain o
(invoke-restart 'skip-event))))
(process-signal raw-message))
(skip-event () nil)))
:name "opencortex-async-task")
:name "opencortex-async-task
;; Sync: process in main thread with recovery
(restart-case
@@ -140,7 +140,7 @@ Other sensors (heartbeats, interrupts) are processed synchronously to maintain o
(invoke-restart 'skip-event))))
(process-signal raw-message))
(skip-event ()
(harness-log "SYSTEM RECOVERY: Stimulus dropped."))))))
(harness-log "SYSTEM RECOVERY: Stimulus dropped.)))))
#+end_src
* The Perceive Gate
@@ -170,7 +170,7 @@ Other sensors (heartbeats, interrupts) are processed synchronously to maintain o
;; Log the incoming signal for debugging
(harness-log "GATE [Perceive]: ~a (~a) [Source: ~s]"
type (or sensor "no-sensor") (getf meta :source))
type (or sensor "no-sensor (getf meta :source))
;; Handle EVENT type sensors
(cond ((eq type :EVENT)
@@ -227,7 +227,7 @@ Other sensors (heartbeats, interrupts) are processed synchronously to maintain o
These tests verify the Perceive pipeline. Run with:
~(fiveam:run! 'pipeline-perceive-suite)~
#+begin_src lisp :tangle (expand-file-name "harness/pipeline-perceive-tests.lisp" (concat (concat (or (getenv "INSTALL_DIR") ".") "/harness") "/tests"))
#+begin_src lisp :tangle pipeline-perceive-tests.lisp" (concat (concat (or (getenv "INSTALL_DIR ". "/harness "/tests)
(defpackage :opencortex-pipeline-perceive-tests
(:use :cl :fiveam :opencortex)
(:export #:pipeline-perceive-suite))
@@ -235,14 +235,14 @@ These tests verify the Perceive pipeline. Run with:
(in-package :opencortex-pipeline-perceive-tests)
(def-suite pipeline-perceive-suite
:description "Test suite for Perceive pipeline")
:description "Test suite for Perceive pipeline
(in-suite pipeline-perceive-suite)
(test test-perceive-gate
"Perceive gate should update the object store and normalize signal."
(clrhash opencortex::*memory*)
(let* ((signal (list :type :EVENT :payload (list :sensor :buffer-update :ast (list :type :HEADLINE :properties (list :ID "test-node" :TITLE "Test") :contents nil))))
(let* ((signal (list :type :EVENT :payload (list :sensor :buffer-update :ast (list :type :HEADLINE :properties (list :ID "test-node" :TITLE "Test :contents nil))))
(result (perceive-gate signal)))
(is (eq :perceived (getf result :status)))
(is (not (null (gethash "test-node" opencortex::*memory*))))))

View File

@@ -1,4 +1,4 @@
#+PROPERTY: header-args:lisp :tangle (expand-file-name "harness/reason.lisp" (expand-file-name "harness/"))
#+PROPERTY: header-args:lisp :tangle (concat (identity (getenv "INSTALL_DIR")) "/harness/reason.lisp")" )
#+TITLE: Stage 2: Reason (reason.lisp)
#+AUTHOR: Amr
#+FILETAGS: :harness:reason:
@@ -23,7 +23,7 @@ The LLM proposes; the skills verify. This is the "Bouncer Pattern" - the determi
The Reason stage communicates exclusively through property lists (plists). This design choice reflects the homoiconic nature of Lisp - plists are native data structures that can be read, written, and manipulated by the same code that processes them.
A plist message like:
: (TYPE :REQUEST TARGET :CLI PAYLOAD (ACTION :MESSAGE TEXT "Hello"))
: (TYPE :REQUEST TARGET :CLI PAYLOAD (ACTION :MESSAGE TEXT "Hello)
Is simultaneously:
- Human-readable text
@@ -46,17 +46,17 @@ The probabilistic engine is responsible for all neural/LLM operations. It mainta
#+begin_src lisp
(defvar *probabilistic-backends* (make-hash-table :test 'equal)
"Registry mapping provider keywords (:openrouter, :ollama) to their calling functions.")
"Registry mapping provider keywords (:openrouter, :ollama) to their calling functions.
(defvar *provider-cascade* nil
"Ordered list of provider keywords to try. First available provider wins.")
"Ordered list of provider keywords to try. First available provider wins.
(defvar *model-selector-fn* nil
"Optional function that selects a specific model for each provider.
Signature: (funcall fn provider context) => model-name-string")
Signature: (funcall fn provider context) => model-name-string
(defvar *consensus-enabled-p* nil
"When T, run multiple providers and compare results for critical decisions.")
"When T, run multiple providers and compare results for critical decisions.
#+end_src
** register-probabilistic-backend: Backend Registration
@@ -68,8 +68,8 @@ The probabilistic engine is responsible for all neural/LLM operations. It mainta
NAME is a keyword like :openrouter or :ollama.
FN is a function with signature: (funcall fn prompt system-prompt &key model)
returning either:
- (list :status :success :content \"response text\")
- (list :status :error :message \"error description\")
- (list :status :success :content \"response text\
- (list :status :error :message \"error description\
- a simple string on success
Example registration:
@@ -82,7 +82,7 @@ The probabilistic engine is responsible for all neural/LLM operations. It mainta
#+begin_src lisp
(defun probabilistic-call (prompt &key
(system-prompt "You are the Probabilistic engine.")
(system-prompt "You are the Probabilistic engine.
(cascade nil)
(context nil))
"Dispatch a neural request through the provider cascade.
@@ -121,7 +121,7 @@ The probabilistic engine is responsible for all neural/LLM operations. It mainta
;; All providers failed
(list :type :LOG
:payload (list :text "Neural Cascade Failure: All providers exhausted.")))))
:payload (list :text "Neural Cascade Failure: All providers exhausted.))))
#+end_src
* Cognitive Proposal Generation (Think)
@@ -144,9 +144,9 @@ The `think` function is the heart of the probabilistic engine. It constructs a p
(if (and text (stringp text))
(let ((cleaned text))
(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 "```" 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 "```" cleaned
(string-trim '(#\Space #\Newline #\Tab) cleaned))
text))
#+end_src
@@ -164,8 +164,8 @@ The `think` function is the heart of the probabilistic engine. It constructs a p
making the plist compatible with standard Lisp property accessors.
Example transformation:
(TYPE REQUEST PAYLOAD (ACTION MESSAGE TEXT \"Hi\"))
=> (:TYPE :REQUEST :PAYLOAD (:ACTION :MESSAGE :TEXT \"Hi\"))"
(TYPE REQUEST PAYLOAD (ACTION MESSAGE TEXT \"Hi\)
=> (:TYPE :REQUEST :PAYLOAD (:ACTION :MESSAGE :TEXT \"Hi\)"
(when (listp plist)
(loop for (k . rest) on plist by #'cddr
@@ -193,14 +193,14 @@ The `think` function is the heart of the probabilistic engine. It constructs a p
This constraint makes parsing deterministic and prevents rambling.
Returns a plist with structure:
(:TYPE :REQUEST :TARGET :CLI :PAYLOAD (:ACTION :MESSAGE :TEXT \"...\"))"
(:TYPE :REQUEST :TARGET :CLI :PAYLOAD (:ACTION :MESSAGE :TEXT \"...\)"
;; Gather context components
(let* ((active-skill (find-triggered-skill context))
(tool-belt (generate-tool-belt-prompt))
(global-context (context-assemble-global-awareness))
(system-logs (context-get-system-logs))
(assistant-name (or (getenv "MEMEX_ASSISTANT") "Agent"))
(assistant-name (or (getenv "MEMEX_ASSISTANT "Agent)
(rejection-trace (proto-get (proto-get context :payload) :rejection-trace)))
;; Generate prompt from skill or raw text
@@ -212,12 +212,12 @@ The `think` function is the heart of the probabilistic engine. It constructs a p
(let ((p (proto-get (proto-get context :payload) :text)))
(if (and p (stringp p))
p
"Maintain metabolic stasis."))))
"Maintain metabolic stasis.)))
;; Inject Reflection Loop feedback if a previous proposal was rejected
(reflection-feedback (if rejection-trace
(format nil "~%~%PREVIOUS PROPOSAL REJECTED:~%Your previous proposal was rejected by the deterministic safety gates.~%Rejection Trace: ~a~%You MUST fix the syntax or logic error described above and try again." rejection-trace)
""))
(system-prompt (format nil
"IDENTITY: ~a~a
@@ -228,10 +228,10 @@ Your task is to generate exactly ONE valid Lisp plist response.
MANDATE: Respond with ONE Lisp plist. Never output prose.
IMPORTANT: To reply to the user, you MUST use:
(:TYPE :REQUEST :PAYLOAD (:ACTION :MESSAGE :TEXT \"<Response Text>\"))
(:TYPE :REQUEST :PAYLOAD (:ACTION :MESSAGE :TEXT \"<Response Text>\)
To call a tool, you MUST use:
(:TYPE :REQUEST :TARGET :TOOL :ACTION :CALL :TOOL \"<name>\" :ARGS (:arg1 \"val\"))
(:TYPE :REQUEST :TARGET :TOOL :ACTION :CALL :TOOL \"<name>\" :ARGS (:arg1 \"val\)
MANDATORY VALIDATION RULE: Before declaring any Lisp code edit complete,
you MUST call the `:validate-lisp` tool with the proposed code. If the tool
@@ -432,7 +432,7 @@ The deterministic engine runs all registered skills' verification functions. Thi
(last-rejection nil))
(loop
(when (<= retries 0)
(harness-log "REASON: Reflection loop exhausted. Final rejection.")
(harness-log "REASON: Reflection loop exhausted. Final rejection.
(setf (getf signal :approved-action) last-rejection)
(setf (getf signal :status) :reasoned)
(return signal))
@@ -472,7 +472,7 @@ The deterministic engine runs all registered skills' verification functions. Thi
These tests verify the Reason (cognitive) pipeline. Run with:
~(fiveam:run! 'pipeline-reason-suite)~
#+begin_src lisp :tangle (expand-file-name "harness/pipeline-reason-tests.lisp" (concat (concat (or (getenv "INSTALL_DIR") ".") "/harness") "/tests"))
#+begin_src lisp :tangle pipeline-reason-tests.lisp" (concat (concat (or (getenv "INSTALL_DIR ". "/harness "/tests)
(defpackage :opencortex-pipeline-reason-tests
(:use :cl :fiveam :opencortex)
(:export #:pipeline-reason-suite))
@@ -480,7 +480,7 @@ These tests verify the Reason (cognitive) pipeline. Run with:
(in-package :opencortex-pipeline-reason-tests)
(def-suite pipeline-reason-suite
:description "Test suite for Reason pipeline")
:description "Test suite for Reason pipeline
(in-suite pipeline-reason-suite)
@@ -491,10 +491,10 @@ These tests verify the Reason (cognitive) pipeline. Run with:
(opencortex::defskill :mock-safety
:priority 50
:trigger (lambda (ctx) t)
:probabilistic (lambda (ctx) "Mock probabilistic")
:probabilistic (lambda (ctx) "Mock probabilistic
:deterministic (lambda (action ctx)
(list :type :LOG :payload (list :text "Action rejected by skill heuristics"))))
(let* ((candidate (list :type :REQUEST :payload (list :action :eval :code "(shell-command \"rm -rf /\")")))
(list :type :LOG :payload (list :text "Action rejected by skill heuristics)))
(let* ((candidate (list :type :REQUEST :payload (list :action :eval :code "(shell-command \"rm -rf /\))
(signal (list :type :EVENT :candidate candidate))
(result (deterministic-verify candidate signal)))
(is (eq :LOG (getf result :type)))

View File

@@ -23,7 +23,7 @@ To maintain sovereignty, the harness must remain a "dumb" bus. It should not kno
** The Installer Script (opencortex.sh)
The shell script is the primary entry point. It handles the initial git clone, dependency installation, and literate tangle.
#+begin_src bash :tangle (expand-file-name "harness/../opencortex.sh")
#+begin_src bash :tangle ../opencortex.sh
#!/bin/bash
# (The content here is a duplicate of the main opencortex.sh for literate consistency)
# [Note: Implementation is already verified in the top-level script]

View File

@@ -1,4 +1,4 @@
#+PROPERTY: header-args:lisp :tangle skills.lisp
#+PROPERTY: header-args:lisp :tangle (concat (identity (getenv "INSTALL_DIR")) "/harness/skills.lisp")
#+TITLE: The Skill Engine (skills.lisp)
#+AUTHOR: Amr
#+FILETAGS: :harness:skills:

View File

@@ -1,4 +1,4 @@
#+PROPERTY: header-args:lisp :tangle (concat (getenv "INSTALL_DIR") "/tui-client.lisp")
#+PROPERTY: header-args:lisp :tangle (concat (getenv "INSTALL_DIR "/tui-client.lisp
:PROPERTIES:
:ID: tui-client-spec
:CREATED: [2026-04-17 Fri 11:00]
@@ -22,26 +22,26 @@ A simple MVP console is insufficient for a Lisp Machine. To reach v0.2.0, the TU
* Phase B: Protocol (Success Criteria)
** Test Suite Context
#+begin_src lisp :tangle ((expand-file-name "tui-tests.lisp"))
#+begin_src lisp :tangle (tui-tests.lisp)
(defpackage :opencortex-tui-tests
(:use :cl :fiveam :opencortex)
(:export #:tui-suite))
#+end_src
#+begin_src lisp :tangle ((expand-file-name "tui-tests.lisp"))
#+begin_src lisp :tangle (tui-tests.lisp)
(in-package :opencortex-tui-tests)
#+end_src
#+begin_src lisp :tangle ((expand-file-name "tui-tests.lisp"))
(def-suite tui-suite :description "Verification of the TUI parsing and styling logic")
#+begin_src lisp :tangle (tui-tests.lisp)
(def-suite tui-suite :description "Verification of the TUI parsing and styling logic
#+end_src
#+begin_src lisp :tangle ((expand-file-name "tui-tests.lisp"))
#+begin_src lisp :tangle (tui-tests.lisp)
(in-suite tui-suite)
#+end_src
** Command Parsing Tests
#+begin_src lisp :tangle ((expand-file-name "tui-tests.lisp"))
#+begin_src lisp :tangle (tui-tests.lisp)
(test test-tui-connection-drop
"Tier 2 Chaos: Verify that handle-return degrades gracefully when the daemon connection is lost."
(let ((opencortex.tui::*incoming-msgs* nil)
@@ -57,81 +57,81 @@ A simple MVP console is insufficient for a Lisp Machine. To reach v0.2.0, the TU
* Phase C: Implementation (Build)
** Package Context
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR") "/tui-client.lisp")
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR "/tui-client.lisp
(in-package :cl-user)
#+end_src
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR") "/tui-client.lisp")
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR "/tui-client.lisp
(defpackage :opencortex.tui
(:use :cl :croatoan)
(:export :main))
#+end_src
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR") "/tui-client.lisp")
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR "/tui-client.lisp
(in-package :opencortex.tui)
#+end_src
** Global State
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR") "/tui-client.lisp")
(defvar *daemon-host* "127.0.0.1")
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR "/tui-client.lisp
(defvar *daemon-host* "127.0.0.1
#+end_src
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR") "/tui-client.lisp")
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR "/tui-client.lisp
(defvar *daemon-port* 9105)
#+end_src
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR") "/tui-client.lisp")
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR "/tui-client.lisp
(defvar *socket* nil)
#+end_src
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR") "/tui-client.lisp")
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR "/tui-client.lisp
(defvar *stream* nil)
#+end_src
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR") "/tui-client.lisp")
(defvar *chat-history* (list) "Full chronological log of messages.")
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR "/tui-client.lisp
(defvar *chat-history* (list) "Full chronological log of messages.
#+end_src
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR") "/tui-client.lisp")
(defvar *scroll-index* 0 "Offset for history rendering.")
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR "/tui-client.lisp
(defvar *scroll-index* 0 "Offset for history rendering.
#+end_src
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR") "/tui-client.lisp")
(defvar *status-text* "Connecting...")
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR "/tui-client.lisp
(defvar *status-text* "Connecting...
#+end_src
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR") "/tui-client.lisp")
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR "/tui-client.lisp
(defvar *input-buffer* (make-array 0 :element-type 'char :fill-pointer 0 :adjustable t))
#+end_src
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR") "/tui-client.lisp")
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR "/tui-client.lisp
(defvar *command-history* (make-array 0 :element-type 't :fill-pointer 0 :adjustable t))
#+end_src
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR") "/tui-client.lisp")
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR "/tui-client.lisp
(defvar *history-index* -1)
#+end_src
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR") "/tui-client.lisp")
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR "/tui-client.lisp
(defvar *is-running* t)
#+end_src
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR") "/tui-client.lisp")
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR "/tui-client.lisp
(defvar *queue-lock* (bt:make-lock))
#+end_src
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR") "/tui-client.lisp")
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR "/tui-client.lisp
(defvar *incoming-msgs* nil)
#+end_src
** Utilities
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR") "/tui-client.lisp")
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR "/tui-client.lisp
(defun enqueue-msg (msg)
"Thread-safe addition to incoming message queue."
(bt:with-lock-held (*queue-lock*)
(setf *incoming-msgs* (append *incoming-msgs* (list msg)))))
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR") "/tui-client.lisp")
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR "/tui-client.lisp
(defun dequeue-msgs ()
"Thread-safe retrieval of incoming messages."
(bt:with-lock-held (*queue-lock*)
@@ -142,7 +142,7 @@ A simple MVP console is insufficient for a Lisp Machine. To reach v0.2.0, the TU
#+end_src
** Styling Engine
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR") "/tui-client.lisp")
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR "/tui-client.lisp
(defun get-line-style (text)
"Determines croatoan attributes based on content patterns."
(cond
@@ -154,7 +154,7 @@ A simple MVP console is insufficient for a Lisp Machine. To reach v0.2.0, the TU
#+end_src
** Rendering Orchestrator
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR") "/tui-client.lisp")
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR "/tui-client.lisp
(defun render-chat (win)
"Renders the chat history with scrolling and styling."
(clear win)
@@ -172,14 +172,14 @@ A simple MVP console is insufficient for a Lisp Machine. To reach v0.2.0, the TU
#+end_src
** Input Handling
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR") "/tui-client.lisp")
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR "/tui-client.lisp
(defun handle-backspace ()
"Deletes last character from input buffer."
(when (> (fill-pointer *input-buffer*) 0)
(decf (fill-pointer *input-buffer*))))
#+end_src
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR") "/tui-client.lisp")
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR "/tui-client.lisp
(defun handle-return (stream)
"Process input buffer as message or command."
(let ((cmd (coerce *input-buffer* 'string)))
@@ -193,14 +193,14 @@ A simple MVP console is insufficient for a Lisp Machine. To reach v0.2.0, the TU
:PAYLOAD (list :SENSOR :user-input :TEXT cmd))))
(finish-output stream))
(error (c)
(enqueue-msg "ERROR: Connection to daemon lost.")
(enqueue-msg "ERROR: Connection to daemon lost.
(setf *is-running* nil))))
(when (string= cmd "/exit") (setf *is-running* nil))
(when (string= cmd "/clear") (setf *chat-history* nil))))
(when (string= cmd "/exit (setf *is-running* nil))
(when (string= cmd "/clear (setf *chat-history* nil))))
#+end_src
** Main Entry Point
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR") "/tui-client.lisp")
#+begin_src lisp :tangle (concat (getenv "INSTALL_DIR "/tui-client.lisp
(defun main ()
"Initializes ncurses and starts the TUI event loop."
(handler-case