Files
passepartout/harness/package.org
Amr Gharbeia 87a0459497
Some checks failed
Deploy-Agent-V15-Stdin / JOB-V15-STDIN (push) Failing after 2s
feat(v0.2.0): comprehensive foundation hardening and test verification
- Finalized Reflection Loop: Injected deterministic rejection traces back into LLM prompts.
- Hardened Actuators: Added path-traversal guards and enforced Merkle snapshots on AST edits.
- Refactored Lisp Utils: Merged validator/repair into a unified utility skill with whitelist Ast-walking.
- Fixed Build: Resolved all 30+ syntax, scoping, and package visibility errors.
- Verified: Full pass (100%) on all 5 core test suites.
2026-04-27 17:48:01 -04:00

9.3 KiB

System Interface (package.lisp)

System Interface (package.lisp)

The package.lisp file defines the public API of the opencortex harness. It serves as the primary membrane between the deterministic core modules and the dynamic world of skills and actuators.

Public API Export

(defpackage :opencortex
  (:use :cl)
  (:export
   ;; --- communication protocol ---
   #:frame-message
   #:read-framed-message
   #:PROTO-GET
   #:LIST-OBJECTS-WITH-ATTRIBUTE
   #:COSINE-SIMILARITY
   #:VAULT-MASK-STRING
   #:*VAULT-MEMORY*
   #:parse-message
   #:make-hello-message
   #:validate-communication-protocol-schema

   ;; --- Daemon Lifecycle ---
   #:start-daemon
   #:stop-daemon
   #:harness-log
   #:main

   ;; --- Memory (CLOSOS) ---
   #:ingest-ast
   #:lookup-object
   #:list-objects-by-type
   #:org-id-new
   #:*memory*
   #:*history-store*
   #:org-object
   #:make-org-object
   #:org-object-id
   #:org-object-type
   #:org-object-attributes
   #:org-object-parent-id
   #:org-object-children
   #:org-object-version
   #:org-object-last-sync
   #:org-object-vector
   #:org-object-content
   #:org-object-hash
   #:snapshot-memory
   #:rollback-memory

   ;; --- Context API (Peripheral Vision) ---
   #:context-query-store
   #:context-get-active-projects
   #:context-get-recent-completed-tasks
   #:context-list-all-skills
   #:context-get-skill-source
   #:context-get-system-logs
   #:context-resolve-path
   #:context-get-skill-telemetry
   #:harness-track-telemetry
   #:context-assemble-global-awareness

   ;; --- Reactive Signal Pipeline ---
   #:process-signal
   #:perceive-gate
   #:probabilistic-gate
   #:consensus-gate
   #:act-gate
   #:reason-gate
   #:perceive-gate
   #:dispatch-gate
   #:inject-stimulus
   #:initialize-actuators
   #:dispatch-action
   #:register-actuator

   ;; --- Skill Engine ---
   #:load-skill-from-org
   #:initialize-all-skills
   #:load-skill-with-timeout
   #:topological-sort-skills
   #:validate-lisp-syntax
   #:defskill
   #:*skills-registry*
   #:skill
   #:skill-name
   #:skill-priority
   #:skill-dependencies
   #:skill-trigger-fn
   #:skill-probabilistic-prompt
   #:skill-deterministic-fn

   ;; --- Tool Registry ---
   #:def-cognitive-tool
   #:*cognitive-tools*

   ;; --- Emacs Edit Skill ---
   #:emacs-edit-read-file
   #:emacs-edit-write-file
   #:emacs-edit-add-headline
   #:emacs-edit-set-property
   #:emacs-edit-set-todo
   #:emacs-edit-find-headline-by-id
   #:emacs-edit-find-headline-by-title
   #:emacs-edit-generate-id
   #:emacs-edit-id-format

   ;; --- Lisp Utils Skill ---
   #:lisp-utils-validate
   #:lisp-utils-check-structural
   #:lisp-utils-check-syntactic
   #:lisp-utils-check-semantic
   #:lisp-utils-register

   ;; --- Tool Permissions Skill ---
   #:get-tool-permission
   #:set-tool-permission
   #:check-tool-permission-gate
   #:cognitive-tool
   #:cognitive-tool-name
   #:cognitive-tool-description
   #:cognitive-tool-parameters
   #:cognitive-tool-guard
   #:cognitive-tool-body

   ;; --- Emacs Client Registry ---
   #:*emacs-clients*
   #:*clients-lock*
   #:register-emacs-client
   #:unregister-emacs-client

   ;; --- Probabilistic Engine ---
   #:ask-probabilistic
   #:register-probabilistic-backend
   #:distill-prompt
   #:*provider-cascade*

   ;; --- Security Vault ---
   #:vault-get-secret
   #:vault-set-secret

   ;; --- Deterministic Logic ---
   #:list-objects-with-attribute
   #:deterministic-verify

   ;; --- AST Helpers ---
   #:find-headline-missing-id))

Package Implementation

(in-package :opencortex)

Robust Plist Accessor

(defun proto-get (plist key)
  "Robustly retrieves a value from a plist, checking both uppercase and lowercase keyword versions."
  (let* ((s (string key))
         (up (intern (string-upcase s) :keyword))
         (dn (intern (string-downcase s) :keyword)))
    (or (getf plist up) (getf plist dn))))

Harness Logging State

The harness maintains a thread-safe circular log buffer to provide context for debugging and neural reasoning.

(defvar *system-logs* nil)
(defvar *logs-lock* (bordeaux-threads:make-lock "harness-logs-lock"))
(defvar *max-log-history* 100)

Skills Registry

(defvar *skills-registry* (make-hash-table :test 'equal)
  "Global registry of all loaded skills.")

Skill Telemetry State

(defvar *skill-telemetry* (make-hash-table :test 'equal))
(defvar *telemetry-lock* (bordeaux-threads:make-lock "harness-telemetry-lock"))

Telemetry Implementation

The system tracks the performance and reliability of individual skills.

(defun harness-track-telemetry (skill-name duration status)
  "Updates performance metrics for a specific skill. Status should be :success or :rejected."
  (when skill-name
    (bordeaux-threads:with-lock-held (*telemetry-lock*)
      (let ((entry (or (gethash skill-name *skill-telemetry*) (list :executions 0 :total-time 0 :failures 0))))
        (incf (getf entry :executions))
        (incf (getf entry :total-time) duration)
        (when (eq status :rejected) (incf (getf entry :failures)))
        (setf (gethash skill-name *skill-telemetry*) entry)))))

Cognitive Tool Registry

The Tool Registry allows the agent to interact with the physical world. Every tool must define a guard (for security) and a body (for execution).

(defvar *cognitive-tools* (make-hash-table :test 'equal))

(defstruct cognitive-tool
  name
  description
  parameters
  guard
  body)

(defmacro def-cognitive-tool (name description parameters &key guard body)
  "Registers a new cognitive tool into the global registry. Parameters must be a list of property lists."
  `(setf (gethash (string-downcase (string ',name)) *cognitive-tools*)
         (make-cognitive-tool :name (string-downcase (string ',name))
                              :description ,description
                              :parameters ',parameters
                              :guard ,guard
                              :body ,body)))

Harness Logging Implementation

Centralized logging function. It simultaneously writes to standard output and the in-memory circular buffer.

(defun harness-log (msg &rest args)
  "Centralized logging for the harness."
  (let ((formatted-msg (apply #'format nil msg args)))
    (bordeaux-threads:with-lock-held (*logs-lock*)
      (push formatted-msg *system-logs*)
      (when (> (length *system-logs*) *max-log-history*)
        (setq *system-logs* (subseq *system-logs* 0 *max-log-history*))))
    (format t "~a~%" formatted-msg)
    (finish-output)))

Global Test Runner

(load "~/quicklisp/setup.lisp")

(push #p"./" asdf:*central-registry*)

(ql:quickload '(:usocket :bordeaux-threads :cl-postgres :split-sequence
                :dexador :jonathan :cl-dotenv :hunchentoot
                :trivial-garbage :s-sql :str :uuid :cl-json :uiop :fiveam))

(asdf:load-system :opencortex)
(asdf:load-system :opencortex/tests)

(format t "~%=== Running ALL Test Suites ===~%")

;; Engineering Standards tests
(when (find-package :OPENCORTEX-ENGINEERING-STANDARDS-TESTS)
  (fiveam:run! 'OPENCORTEX-ENGINEERING-STANDARDS-TESTS::ENGINEERING-STANDARDS-SUITE))

;; Literate Programming tests
(when (find-package :OPENCORTEX-LITERATE-PROGRAMMING-TESTS)
  (fiveam:run! 'OPENCORTEX-LITERATE-PROGRAMMING-TESTS::LITERATE-PROGRAMMING-SUITE))

;; Communication tests
(when (find-package :OPENCORTEX-TESTS)
  (fiveam:run! 'OPENCORTEX-TESTS::COMMUNICATION-PROTOCOL-SUITE))

;; Pipeline tests
(when (find-package :OPENCORTEX-PIPELINE-TESTS)
  (fiveam:run! 'OPENCORTEX-PIPELINE-TESTS::PIPELINE-SUITE))

;; Boot sequence tests
(when (find-package :OPENCORTEX-BOOT-TESTS)
  (fiveam:run! 'OPENCORTEX-BOOT-TESTS::BOOT-SUITE))

;; Memory tests
(when (find-package :OPENCORTEX-MEMORY-TESTS)
  (fiveam:run! 'OPENCORTEX-MEMORY-TESTS::MEMORY-SUITE))

;; Immune system tests
(when (find-package :OPENCORTEX-IMMUNE-SYSTEM-TESTS)
  (fiveam:run! 'OPENCORTEX-IMMUNE-SYSTEM-TESTS::IMMUNE-SUITE))

;; Emacs edit tests
(when (find-package :OPENCORTEX-EMACS-EDIT-TESTS)
  (fiveam:run! 'OPENCORTEX-EMACS-EDIT-TESTS::EMACS-EDIT-SUITE))

;; Lisp utils tests
(when (find-package :OPENCORTEX-LISP-UTILS-TESTS)
  (fiveam:run! 'OPENCORTEX-LISP-UTILS-TESTS::LISP-UTILS-SUITE))

(format t "~%=== ALL TESTS COMPLETE ===~%")