refactor: Flatten directory structure library->harness, library/gen->skills

This commit is contained in:
2026-04-27 08:41:26 -04:00
parent 43dbe3cf2d
commit 664ba8243d
68 changed files with 637 additions and 666 deletions

View File

@@ -1,6 +1,11 @@
# opencortex: Environment Configuration Template
# Copy this to .env and fill in your values
# =============================================================================
# INSTALLATION
# =============================================================================
INSTALL_DIR="$HOME/opencortex"
# =============================================================================
# IDENTITY
# =============================================================================

View File

@@ -35,7 +35,7 @@ Example feedback chain:
* Package Context
#+begin_src lisp :tangle ../library/act.lisp
#+begin_src lisp :tangle ./act.lisp
(in-package :opencortex)
#+end_src
@@ -43,7 +43,7 @@ Example feedback chain:
** Actuator Registry Variables
#+begin_src lisp :tangle ../library/act.lisp
#+begin_src lisp :tangle ./act.lisp
(defvar *default-actuator* :cli
"The actuator used when no explicit target is specified.
Override with DEFAULT_ACTUATOR environment variable.")
@@ -55,7 +55,7 @@ Example feedback chain:
** initialize-actuators: System Bootstrap
#+begin_src lisp :tangle ../library/act.lisp
#+begin_src lisp :tangle ./act.lisp
(defun initialize-actuators ()
"Load actuator configuration from environment and register core actuators.
@@ -102,7 +102,7 @@ Example feedback chain:
** dispatch-action: The Router
#+begin_src lisp :tangle ../library/act.lisp
#+begin_src lisp :tangle ./act.lisp
(defun dispatch-action (action context)
"Route an approved action to its registered actuator.
@@ -149,7 +149,7 @@ Example feedback chain:
** execute-system-action: Internal Commands
#+begin_src lisp :tangle ../library/act.lisp
#+begin_src lisp :tangle ./act.lisp
(defun execute-system-action (action context)
"Execute internal harness commands.
@@ -198,7 +198,7 @@ Example feedback chain:
** execute-tool-action: Cognitive Tool Execution
#+begin_src lisp :tangle ../library/act.lisp
#+begin_src lisp :tangle ./act.lisp
(defun execute-tool-action (action context)
"Execute a registered cognitive tool.
@@ -267,7 +267,7 @@ Example feedback chain:
** format-tool-result: Human-Readable Output
#+begin_src lisp :tangle ../library/act.lisp
#+begin_src lisp :tangle ./act.lisp
(defun format-tool-result (tool-name result)
"Format a tool result for human-readable display.
@@ -295,7 +295,7 @@ Example feedback chain:
** act-gate: Final Pipeline Stage
#+begin_src lisp :tangle ../library/act.lisp
#+begin_src lisp :tangle ./act.lisp
(defun act-gate (signal)
"Final stage of the metabolic pipeline: Actuation.
@@ -392,7 +392,7 @@ Example feedback chain:
These tests verify the Act pipeline. Run with:
~(fiveam:run! 'pipeline-act-suite)~
#+begin_src lisp :tangle ../tests/pipeline-act-tests.lisp
#+begin_src lisp :tangle ./tests/pipeline-act-tests.lisp
(defpackage :opencortex-pipeline-act-tests
(:use :cl :fiveam :opencortex)
(:export #:pipeline-act-suite))

View File

@@ -10,7 +10,7 @@ The ~communication.lisp~ module defines the low-level transport and framing logi
* Implementation (communication.lisp)
#+begin_src lisp :tangle ../library/communication.lisp
#+begin_src lisp :tangle ./communication.lisp
(in-package :opencortex)
(defun proto-get (plist key)
@@ -21,7 +21,7 @@ The ~communication.lisp~ module defines the low-level transport and framing logi
(or (getf plist up) (getf plist dn))))
#+end_src
#+begin_src lisp :tangle ../library/communication.lisp
#+begin_src lisp :tangle ./communication.lisp
(in-package :opencortex)
(defvar *actuator-registry* (make-hash-table :test 'equalp)
@@ -78,7 +78,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 ../library/communication-validator.lisp
#+begin_src lisp :tangle ./communication-validator.lisp
(in-package :opencortex)
(defun validate-communication-protocol-schema (msg)
@@ -128,7 +128,7 @@ The validator ensures that incoming messages adhere to the strict property list
** Message Framing (communication.lisp)
Frames a message with a hex length prefix and ensures all data is serializable.
#+begin_src lisp :tangle ../library/communication.lisp
#+begin_src lisp :tangle ./communication.lisp
(defun sanitize-protocol-message (msg)
"Recursively strips non-serializable objects from a protocol plist."
(if (and msg (listp msg))
@@ -153,7 +153,7 @@ Frames a message with a hex length prefix and ensures all data is serializable.
These tests verify the communication protocol functions. Run with:
~(fiveam:run! 'communication-protocol-suite)~
#+begin_src lisp :tangle ../library/communication-tests.lisp
#+begin_src lisp :tangle ./communication-tests.lisp
(defpackage :opencortex-communication-tests
(:use :cl :fiveam :opencortex)
(:export #:communication-protocol-suite))

View File

@@ -37,14 +37,14 @@ The ~context.lisp~ module provides the deterministic functional layer for queryi
** Package Context
We begin by ensuring we are executing within the correct isolated package namespace.
#+begin_src lisp :tangle ../library/context.lisp
#+begin_src lisp :tangle ./context.lisp
(in-package :opencortex)
#+end_src
** Querying the Store (context-query-store)
A generalized filter for the Memory. This function allows skills to perform high-level semantic sweeps of the Memex based on tags, TODO states, or Org element types. It returns a list of ~org-object~ structures.
#+begin_src lisp :tangle ../library/context.lisp
#+begin_src lisp :tangle ./context.lisp
(defun context-query-store (&key tag todo-state type)
"Filters the Memory based on tags, todo states, or types."
(let ((results nil))
@@ -62,7 +62,7 @@ A generalized filter for the Memory. This function allows skills to perform high
** Active Projects (context-get-active-projects)
Identifies headlines tagged with ~project~ that have not yet reached a terminal ~DONE~ state. This provides the primary high-level structure for the agent's global awareness.
#+begin_src lisp :tangle ../library/context.lisp
#+begin_src lisp :tangle ./context.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"))
@@ -72,7 +72,7 @@ Identifies headlines tagged with ~project~ that have not yet reached a terminal
** Completed Tasks (context-get-recent-completed-tasks)
Retrieves a list of tasks that have reached the terminal ~DONE~ state. This is useful for providing the agent with historical context or for generating summaries of recent work.
#+begin_src lisp :tangle ../library/context.lisp
#+begin_src lisp :tangle ./context.lisp
(defun context-get-recent-completed-tasks ()
"Retrieves recently finished tasks from the store."
(context-query-store :todo-state "DONE" :type :HEADLINE))
@@ -81,7 +81,7 @@ Retrieves a list of tasks that have reached the terminal ~DONE~ state. This is u
** Capability Discovery (context-list-all-skills)
Provides a sorted list of all currently loaded skills. In a "Self-Writing" environment, the agent must be able to discover and understand its own capabilities. This function provides the metadata necessary for the agent to decide which skill to trigger or how to resolve dependencies.
#+begin_src lisp :tangle ../library/context.lisp
#+begin_src lisp :tangle ./context.lisp
(defun context-list-all-skills ()
"Provides a sorted overview of currently loaded system capabilities."
(let ((results nil))
@@ -95,7 +95,7 @@ Provides a sorted list of all currently loaded skills. In a "Self-Writing" envir
** Skill Inspection (context-get-skill-source)
Reads the raw literate Org source of a specific skill. This is a foundational capability for an agent expected to eventually "self-write" or perform its own maintenance. By reading the literate source, the agent can understand the *intent* behind a skill's logic before proposing a modification. We use the `SKILLS_DIR` environment variable to locate the source files.
#+begin_src lisp :tangle ../library/context.lisp
#+begin_src lisp :tangle ./context.lisp
(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))
@@ -108,7 +108,7 @@ Reads the raw literate Org source of a specific skill. This is a foundational ca
** Harness Logs (context-get-system-logs)
Retrieves the most recent entries from the harness's internal circular log buffer. This allows the Probabilistic Engine to see recent errors or successful dispatches, enabling it to course-correct or explain failures to the user. The log limit is externalized to `CONTEXT_LOG_LIMIT`.
#+begin_src lisp :tangle ../library/context.lisp
#+begin_src lisp :tangle ./context.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 (uiop:getenv "CONTEXT_LOG_LIMIT"))) 20)))
@@ -128,7 +128,7 @@ It implements the following deterministic logic:
The semantic threshold is externalized to `CONTEXT_SEMANTIC_THRESHOLD`.
#+begin_src lisp :tangle ../library/context.lisp
#+begin_src lisp :tangle ./context.lisp
(defun context-render-to-org (obj &key (depth 1) (foveal-id nil) semantic-threshold (foveal-vector nil))
"Recursively renders an org-object and its children to an Org string using a Foveal-Peripheral Hybrid model."
(let* ((id (org-object-id obj))
@@ -177,7 +177,7 @@ The semantic threshold is externalized to `CONTEXT_SEMANTIC_THRESHOLD`.
** Path Resolution (context-resolve-path)
A utility function that expands environment variables (like ~$HOME~ or ~$MEMEX_ROOT~) within path strings. This ensures that the agent can interact with files across different machine configurations without hardcoding absolute paths. This version is more robust, supporting multiple environment variables throughout the string.
#+begin_src lisp :tangle ../library/context.lisp
#+begin_src lisp :tangle ./context.lisp
(defun context-resolve-path (path-string)
"Expands environment variables and strips literal quotes from a path string."
(let ((path (if (stringp path-string)
@@ -196,7 +196,7 @@ A utility function that expands environment variables (like ~$HOME~ or ~$MEMEX_R
** Global Awareness (context-assemble-global-awareness)
The primary entry point for context generation. This function identifies active projects and the current user focus (captured during the Perceive stage), then invokes the recursive renderer to assemble the pruned Org-mode skeletal outline sent to the LLM.
#+begin_src lisp :tangle ../library/context.lisp
#+begin_src lisp :tangle ./context.lisp
(defun context-assemble-global-awareness (&optional signal)
"Produces a high-level skeletal outline of the current Memory for the LLM."
(let* ((foveal-id (or (getf signal :foveal-focus)
@@ -216,7 +216,7 @@ 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 ../tests/peripheral-vision-tests.lisp
#+begin_src lisp :tangle ./tests/peripheral-vision-tests.lisp
(defpackage :opencortex-peripheral-vision-tests
(:use :cl :fiveam :opencortex)
(:export #:vision-suite))
@@ -230,7 +230,7 @@ Following the Engineering Standards, the peripheral vision extraction and render
** Foveal Rendering Test
Verify that the foveal target is rendered with content, while siblings are skeletal.
#+begin_src lisp :tangle ../tests/peripheral-vision-tests.lisp
#+begin_src lisp :tangle ./tests/peripheral-vision-tests.lisp
(test test-foveal-rendering
"Verify that the foveal target is rendered with content, while siblings are skeletal."
(clrhash opencortex::*memory*)
@@ -250,7 +250,7 @@ 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 ../tests/peripheral-vision-tests.lisp
#+begin_src lisp :tangle ./tests/peripheral-vision-tests.lisp
(test test-awareness-budget
"Verify that context-assemble-global-awareness handles multiple projects."
(clrhash opencortex::*memory*)

View File

@@ -35,7 +35,7 @@ The loop operates in a multi-threaded environment:
* Package and Thread-Safe Variables
#+begin_src lisp :tangle ../library/loop.lisp
#+begin_src lisp :tangle ./loop.lisp
(in-package :opencortex)
(defvar *interrupt-flag* nil
@@ -58,7 +58,7 @@ This function implements the Perceive-Reason-Act pipeline. It processes a signal
The depth counter prevents infinite recursion—a signal that generates another signal that generates another, etc. By limiting to depth 10, we ensure the system eventually converges or gracefully terminates.
#+begin_src lisp :tangle ../library/loop.lisp
#+begin_src lisp :tangle ./loop.lisp
(defun process-signal (signal)
"The entry point to the Metabolic Pipeline: Perceive -> Reason -> Act.
@@ -152,7 +152,7 @@ The heartbeat thread ensures the agent remains alive even without external input
** Heartbeat Configuration Variables
#+begin_src lisp :tangle ../library/loop.lisp
#+begin_src lisp :tangle ./loop.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.")
@@ -163,7 +163,7 @@ The heartbeat thread ensures the agent remains alive even without external input
** start-heartbeat: The Pulsing Heart
#+begin_src lisp :tangle ../library/loop.lisp
#+begin_src lisp :tangle ./loop.lisp
(defun start-heartbeat ()
"Starts the background heartbeat thread.
@@ -209,7 +209,7 @@ The heartbeat thread ensures the agent remains alive even without external input
** Shutdown Configuration
#+begin_src lisp :tangle ../library/loop.lisp
#+begin_src lisp :tangle ./loop.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.")
@@ -226,7 +226,7 @@ The main function orchestrates system startup:
5. Register SIGINT handler for graceful Ctrl+C shutdown
6. Enter idle loop (sleeping in 1-hour increments)
#+begin_src lisp :tangle ../library/loop.lisp
#+begin_src lisp :tangle ./loop.lisp
(defun main ()
"Entry point for OpenCortex. Initializes the system and enters idle loop.
@@ -291,7 +291,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 ../tests/immune-system-tests.lisp
#+begin_src lisp :tangle ./tests/immune-system-tests.lisp
(defpackage :opencortex-immune-system-tests
(:use :cl :fiveam :opencortex)
(:export #:immune-suite))

View File

@@ -83,62 +83,6 @@
(harness-log "MEMORY - Memory rolled back to snapshot ~a" index))
(harness-log "MEMORY ERROR - Snapshot ~a not found." index))))
(defvar *embedding-cache* (make-hash-table :test 'equal)
"Cache for embeddings to avoid redundant API calls.")
(defun get-embedding (text)
"Generates a vector embedding for the given text via Ollama. Returns nil on failure."
(when (or (null text) (string= text ""))
(return-from get-embedding nil))
(let ((cached (gethash text *embedding-cache*)))
(when cached (return-from get-embedding cached)))
(let ((result (funcall (get-cognitive-tool-body :get-ollama-embedding) (list :text text))))
(when (eq (getf result :status) :success)
(let ((vec (getf result :vector)))
(setf (gethash text *embedding-cache*) vec)
vec))))
(defun cosine-similarity (vec-a vec-b)
"Computes cosine similarity between two vectors. Both should be sequences of numbers."
(when (or (null vec-a) (null vec-b) (zerop (length vec-a)) (zerop (length vec-b)))
(return-from cosine-similarity 0.0))
(let ((dot-product (loop for a across vec-a
for b across vec-b
sum (* a b)))
(norm-a (sqrt (loop for a across vec-a sum (* a a))))
(norm-b (sqrt (loop for b across vec-b sum (* b b)))))
(if (or (zerop norm-a) (zerop norm-b))
0.0
(/ dot-product (* norm-a norm-b)))))
(defun semantic-search (query &key (limit 10) (min-similarity 0.5))
"Searches memory for objects semantically similar to the query.
Returns up to LIMIT objects with similarity >= MIN-SIMILARITY, sorted by similarity descending."
(let* ((query-vec (get-embedding query))
(results nil))
(unless query-vec
(harness-log "EMBEDDING: Failed to generate embedding for query: ~a" query)
(return-from semantic-search nil))
(maphash (lambda (id obj)
(let ((obj-vec (org-object-vector obj)))
(when obj-vec
(let ((sim (cosine-similarity query-vec obj-vec)))
(when (>= sim min-similarity)
(push (list :id id :object obj :similarity sim) results))))))
*memory*)
(setf results (sort results #'> :key (lambda (r) (getf r :similarity))))
(subseq results 0 (min limit (length results)))))
(def-cognitive-tool :semantic-search
"Searches memory for objects semantically similar to a query."
((:query :type :string :description "The search query.")
(:limit :type :integer :description "Maximum results to return." :default 10)
(:min-similarity :type :number :description "Minimum similarity threshold (0-1)." :default 0.5))
:body (lambda (args)
(semantic-search (getf args :query)
:limit (or (getf args :limit) 10)
:min-similarity (or (getf args :min-similarity) 0.5))))
(defun org-id-new ()
"Generates a new UUID string for Org-mode identification."
(string-downcase (format nil "~a" (uuid:make-v4-uuid))))

View File

@@ -31,14 +31,14 @@ flowchart TD
#+end_src
** Package Context
#+begin_src lisp :tangle ../library/memory.lisp
#+begin_src lisp :tangle ./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 ../library/memory.lisp
#+begin_src lisp :tangle ./memory.lisp
(defvar *memory* (make-hash-table :test 'equal))
(defvar *history-store* (make-hash-table :test 'equal)
@@ -48,7 +48,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 ../library/memory.lisp
#+begin_src lisp :tangle ./memory.lisp
(defstruct org-object
id type attributes content vector parent-id children version last-sync hash)
@@ -60,7 +60,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 ../library/memory.lisp
#+begin_src lisp :tangle ./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)))
@@ -77,7 +77,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 ../library/memory.lisp
#+begin_src lisp :tangle ./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))
@@ -116,7 +116,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 ../library/memory.lisp
#+begin_src lisp :tangle ./memory.lisp
(defvar *object-store-snapshots* nil)
(defun copy-hash-table (hash-table)
@@ -138,7 +138,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 ../library/memory.lisp
#+begin_src lisp :tangle ./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*)))
@@ -153,7 +153,7 @@ Restores the state of the Memex from one of the previous snapshots.
These tests verify the Memory system. Run with:
~(fiveam:run! 'memory-suite)~
#+begin_src lisp :tangle ../tests/memory-tests.lisp
#+begin_src lisp :tangle ./tests/memory-tests.lisp
(defpackage :opencortex-memory-tests
(:use :cl :fiveam :opencortex)
(:export #:memory-suite))
@@ -206,7 +206,7 @@ These tests verify the Memory system. Run with:
** Disk Persistence (save-memory / load-memory)
Essential for surviving crashes. Saves the in-memory hash tables to disk and loads them back on restart. The path is controlled by the `MEMORY_SNAPSHOT_PATH` environment variable.
#+begin_src lisp :tangle ../library/memory.lisp
#+begin_src lisp :tangle ./memory.lisp
(defvar *memory-snapshot-path* nil
"Path to the memory snapshot file. Set from MEMORY_SNAPSHOT_PATH env or default.")
@@ -261,7 +261,7 @@ Support for vector embeddings via Ollama and semantic search with cosine similar
The vector slot on org-objects enables semantic recall - searching memory by meaning rather than just keywords. Embeddings are generated on ingest when the :EMBED property is set to "t", and cached locally to avoid redundant API calls.
#+begin_src lisp :tangle ../library/memory.lisp
#+begin_src lisp :tangle ./memory.lisp
(defvar *embedding-cache* (make-hash-table :test 'equal)
"Cache for embeddings to avoid redundant API calls.")
@@ -317,12 +317,35 @@ Returns up to LIMIT objects with similarity >= MIN-SIMILARITY, sorted by similar
(semantic-search (getf args :query)
:limit (or (getf args :limit) 10)
:min-similarity (or (getf args :min-similarity) 0.5))))
** Cognitive Tool: Generate Embeddings
Provided for the Probabilistic Engine to invoke embedding generation on demand.
#+begin_src lisp :tangle ./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."))
:body (lambda (args)
(let ((texts (getf args :texts)))
(unless (and texts (listp texts))
(return-from generate-embeddings
(list :status :error :message ":texts must be a list of strings.")))
(let ((results nil) (errors nil))
(dolist (text texts)
(let ((vec (get-embedding text)))
(if vec
(push (list :text text :vector vec) results)
(push text errors))))
(list :status (if errors :partial :success)
:embeddings (nreverse results)
:failed (when errors (nreverse errors))
:count (length results))))))
#+end_src
** Lookup Utilities
Basic functions for retrieving objects by ID or type.
#+begin_src lisp :tangle ../library/memory.lisp
#+begin_src lisp :tangle ./memory.lisp
(defun org-id-new ()
"Generates a new UUID string for Org-mode identification."
(string-downcase (format nil "~a" (uuid:make-v4-uuid))))
@@ -351,7 +374,7 @@ Basic functions for retrieving objects by ID or type.
** Structural Helpers
Utility functions for AST traversal and path resolution.
#+begin_src lisp :tangle ../library/memory.lisp
#+begin_src lisp :tangle ./memory.lisp
(defun find-headline-missing-id (ast)
"Traverses an AST to find headlines that lack an :ID: property."
(when (listp ast)
@@ -367,7 +390,7 @@ Utility functions for AST traversal and path resolution.
* Phase E: Chaos (Verification)
Following the Engineering Standards, the Memory must be empirically verified through automated testing. The following test suite ensures the mathematical integrity of the Merkle hashes and the behavioral correctness of the immutable versioning and rollback systems.
#+begin_src lisp :tangle ../tests/memory-tests.lisp
#+begin_src lisp :tangle ./tests/memory-tests.lisp
(defpackage :opencortex-memory-tests
(:use :cl :fiveam :opencortex)
(:export #:memory-suite))

View File

@@ -9,7 +9,7 @@ The ~package.lisp~ file defines the public API of the ~opencortex~ harness. It s
** Public API Export
#+begin_src lisp :tangle ../library/package.lisp
#+begin_src lisp :tangle ./package.lisp
(defpackage :opencortex
(:use :cl)
(:export
@@ -131,13 +131,13 @@ The ~package.lisp~ file defines the public API of the ~opencortex~ harness. It s
* Package Implementation
#+begin_src lisp :tangle ../library/package.lisp
#+begin_src lisp :tangle ./package.lisp
(in-package :opencortex)
#+end_src
** Robust Plist Accessor
#+begin_src lisp :tangle ../library/package.lisp
#+begin_src lisp :tangle ./package.lisp
(defun proto-get (plist key)
"Robustly retrieves a value from a plist, checking both uppercase and lowercase keyword versions."
(let* ((s (string key))
@@ -150,7 +150,7 @@ The ~package.lisp~ file defines the public API of the ~opencortex~ harness. It s
The harness maintains a thread-safe circular log buffer to provide context for debugging and neural reasoning.
#+begin_src lisp :tangle ../library/package.lisp
#+begin_src lisp :tangle ./package.lisp
(defvar *system-logs* nil)
(defvar *logs-lock* (bordeaux-threads:make-lock "harness-logs-lock"))
(defvar *max-log-history* 100)
@@ -158,14 +158,14 @@ The harness maintains a thread-safe circular log buffer to provide context for d
** Skills Registry
#+begin_src lisp :tangle ../library/package.lisp
#+begin_src lisp :tangle ./package.lisp
(defvar *skills-registry* (make-hash-table :test 'equal)
"Global registry of all loaded skills.")
#+end_src
** Skill Telemetry State
#+begin_src lisp :tangle ../library/package.lisp
#+begin_src lisp :tangle ./package.lisp
(defvar *skill-telemetry* (make-hash-table :test 'equal))
(defvar *telemetry-lock* (bordeaux-threads:make-lock "harness-telemetry-lock"))
#+end_src
@@ -174,7 +174,7 @@ The harness maintains a thread-safe circular log buffer to provide context for d
The system tracks the performance and reliability of individual skills.
#+begin_src lisp :tangle ../library/package.lisp
#+begin_src lisp :tangle ./package.lisp
(defun harness-track-telemetry (skill-name duration status)
"Updates performance metrics for a specific skill. Status should be :success or :rejected."
(when skill-name
@@ -190,7 +190,7 @@ The system tracks the performance and reliability of individual skills.
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).
#+begin_src lisp :tangle ../library/package.lisp
#+begin_src lisp :tangle ./package.lisp
(defvar *cognitive-tools* (make-hash-table :test 'equal))
(defstruct cognitive-tool
@@ -214,7 +214,7 @@ The Tool Registry allows the agent to interact with the physical world. Every to
Centralized logging function. It simultaneously writes to standard output and the in-memory circular buffer.
#+begin_src lisp :tangle ../library/package.lisp
#+begin_src lisp :tangle ./package.lisp
(defun harness-log (msg &rest args)
"Centralized logging for the harness."
(let ((formatted-msg (apply #'format nil msg args)))

View File

@@ -53,7 +53,7 @@ Other sensors (heartbeats, interrupts) are processed synchronously to maintain o
* Package Context
#+begin_src lisp :tangle ../library/perceive.lisp
#+begin_src lisp :tangle ./perceive.lisp
(in-package :opencortex)
#+end_src
@@ -61,7 +61,7 @@ Other sensors (heartbeats, interrupts) are processed synchronously to maintain o
** Async Sensor Registry
#+begin_src lisp :tangle ../library/perceive.lisp
#+begin_src lisp :tangle ./perceive.lisp
(defvar *async-sensors* '(:chat-message :delegation :user-command)
"Sensors that are processed in dedicated threads.
@@ -74,7 +74,7 @@ Other sensors (heartbeats, interrupts) are processed synchronously to maintain o
** Foveal Focus State
#+begin_src lisp :tangle ../library/perceive.lisp
#+begin_src lisp :tangle ./perceive.lisp
(defvar *foveal-focus-id* nil
"The Org ID of the node the user is currently interacting with.
@@ -89,7 +89,7 @@ Other sensors (heartbeats, interrupts) are processed synchronously to maintain o
** inject-stimulus: Entry Point
#+begin_src lisp :tangle ../library/perceive.lisp
#+begin_src lisp :tangle ./perceive.lisp
(defun inject-stimulus (raw-message &key stream (depth 0))
"Inject a raw message into the signal processing pipeline.
@@ -146,7 +146,7 @@ Other sensors (heartbeats, interrupts) are processed synchronously to maintain o
** perceive-gate: Signal Normalization
#+begin_src lisp :tangle ../library/perceive.lisp
#+begin_src lisp :tangle ./perceive.lisp
(defun perceive-gate (signal)
"Stage 1 of the metabolic pipeline: Normalize sensory input.
@@ -226,7 +226,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 ../tests/pipeline-perceive-tests.lisp
#+begin_src lisp :tangle ./tests/pipeline-perceive-tests.lisp
(defpackage :opencortex-pipeline-perceive-tests
(:use :cl :fiveam :opencortex)
(:export #:pipeline-perceive-suite))

View File

@@ -33,7 +33,7 @@ This means the reasoning pipeline can generate, modify, and execute its own comm
* Package Context
#+begin_src lisp :tangle ../library/reason.lisp
#+begin_src lisp :tangle ./reason.lisp
(in-package :opencortex)
#+end_src
@@ -43,7 +43,7 @@ The probabilistic engine is responsible for all neural/LLM operations. It mainta
** Backend Registry Variables
#+begin_src lisp :tangle ../library/reason.lisp
#+begin_src lisp :tangle ./reason.lisp
(defvar *probabilistic-backends* (make-hash-table :test 'equal)
"Registry mapping provider keywords (:openrouter, :ollama) to their calling functions.")
@@ -60,7 +60,7 @@ The probabilistic engine is responsible for all neural/LLM operations. It mainta
** register-probabilistic-backend: Backend Registration
#+begin_src lisp :tangle ../library/reason.lisp
#+begin_src lisp :tangle ./reason.lisp
(defun register-probabilistic-backend (name fn)
"Register a neural provider backend.
@@ -79,7 +79,7 @@ The probabilistic engine is responsible for all neural/LLM operations. It mainta
** probabilistic-call: Cascade Dispatch
#+begin_src lisp :tangle ../library/reason.lisp
#+begin_src lisp :tangle ./reason.lisp
(defun probabilistic-call (prompt &key
(system-prompt "You are the Probabilistic engine.")
(cascade nil)
@@ -129,7 +129,7 @@ The `think` function is the heart of the probabilistic engine. It constructs a p
** strip-markdown: Clean LLM Output
#+begin_src lisp :tangle ../library/reason.lisp
#+begin_src lisp :tangle ./reason.lisp
(defun strip-markdown (text)
"Strip markdown formatting from LLM output.
@@ -152,7 +152,7 @@ The `think` function is the heart of the probabilistic engine. It constructs a p
** normalize-plist-keywords: Fix LLM Keyword Output
#+begin_src lisp :tangle ../library/reason.lisp
#+begin_src lisp :tangle ./reason.lisp
(defun normalize-plist-keywords (plist)
"Normalize all keys in a plist to keywords.
@@ -176,7 +176,7 @@ The `think` function is the heart of the probabilistic engine. It constructs a p
** think: Generate Action Proposal
#+begin_src lisp :tangle ../library/reason.lisp
#+begin_src lisp :tangle ./reason.lisp
(defun think (context)
"Generate a Lisp action proposal based on current context.
@@ -320,7 +320,7 @@ The deterministic engine runs all registered skills' verification functions. Thi
** deterministic-verify: Skill Chain Verification
#+begin_src lisp :tangle ../library/reason.lisp
#+begin_src lisp :tangle ./reason.lisp
(defun deterministic-verify (proposed-action context)
"Run all skill deterministic gates on a proposed action.
@@ -391,7 +391,7 @@ The deterministic engine runs all registered skills' verification functions. Thi
** reason-gate: The Stage Function
#+begin_src lisp :tangle ../library/reason.lisp
#+begin_src lisp :tangle ./reason.lisp
(defun reason-gate (signal)
"Stage 2 of the metabolic pipeline: Reason.
@@ -448,7 +448,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 ../tests/pipeline-reason-tests.lisp
#+begin_src lisp :tangle ./tests/pipeline-reason-tests.lisp
(defpackage :opencortex-pipeline-reason-tests
(:use :cl :fiveam :opencortex)
(:export #:pipeline-reason-suite))

View File

@@ -1,25 +1,27 @@
(in-package :opencortex)
(defun COSINE-SIMILARITY (v1 v2)
"Computes the cosine similarity between two vectors.
Both arguments should be sequences of numbers. Returns a value between -1.0 and 1.0."
(let ((len1 (length v1)) (len2 (length v2)))
"Computes the cosine similarity between two vectors."
(let* ((len1 (length v1))
(len2 (length v2)))
(if (or (zerop len1) (zerop len2))
0.0
(let ((dot-product 0.0d0)
(let* ((dot-product 0.0d0)
(norm1 0.0d0)
(norm2 0.0d0))
(let ((len (min len1 len2)))
(dotimes (i len)
(let ((x (coerce (elt v1 i) 'double-float)))
(let ((y (coerce (elt v2 i) 'double-float)))
(dotimes (i (min len1 len2))
(let* ((x (coerce (elt v1 i) 'double-float))
(y (coerce (elt v2 i) 'double-float)))
(incf dot-product (* x y))
(incf norm1 (* x x))
(incf norm2 (* y y))))))
(incf norm2 (* y y))))
(if (or (zerop norm1) (zerop norm2))
0.0
(/ dot-product (sqrt (* norm1 norm2))))))))
(defun VAULT-MASK-STRING (s) "[MASKED]") ; Stub
;; TODO: Stub for vault - implement later
(defun VAULT-MASK-STRING (s) "[MASKED]")
(defvar *VAULT-MEMORY* (make-hash-table :test 'equal))
@@ -70,20 +72,24 @@ Both arguments should be sequences of numbers. Returns a value between -1.0 and
(nreverse resolved))))
(defun parse-skill-metadata (filepath)
"Extracts ID and DEPENDS_ON tags using robust regex scanning."
"Extracts ID and DEPENDS_ON tags from org file."
(let ((dependencies nil)
(id nil)
(content (uiop:read-file-string filepath)))
;; Extract ID
(multiple-value-bind (match regs)
(ppcre:scan-to-strings "(?im:^:ID:\\s*([^\\s\\r\\n]+))" content)
(when match (setf id (aref regs 0))))
;; Extract all DEPENDS_ON lines
(ppcre:do-register-groups (deps-string)
("(?im:^#\\+DEPENDS_ON:\\s*(.*))" content)
(let ((deps (ppcre:split "\\s+" (string-trim " " deps-string))))
(setf dependencies (append dependencies (mapcar (lambda (s) (string-trim "[] " s)) deps)))))
(values id (remove-if (lambda (s) (= 0 (length s))) dependencies))))
;; Simple ID extraction using string search
(let ((id-start (search ":ID:" content)))
(when id-start
(let ((id-end (position #\Newline content :start id-start)))
(when id-end
(setf id (subseq content (+ id-start 4) id-end)))))
;; Simple DEPENDS_ON extraction
(let ((pos 0))
(loop while (setf pos (search "#+DEPENDS_ON:" content :start2 pos))
do (let ((end (position #\Newline content :start pos)))
(when end
(push (subseq content (+ pos 13) end) dependencies)
(setf pos end))))
(values id (reverse dependencies))))
(defun topological-sort-skills (skills-dir)
"Returns a list of skill filepaths sorted by dependency (dependencies first)."
@@ -179,7 +185,7 @@ Only loads blocks that specify a .lisp tangle target, ignoring tests and example
((and in-lisp-block collect-this-block)
(unless (or (uiop:string-prefix-p ":PROPERTIES:" (string-upcase clean-line))
(uiop:string-prefix-p ":END:" (string-upcase clean-line)))
(setf lisp-code (concatenate 'string lisp-code line (string #\Newline))))))))
(setf lisp-code (concatenate 'string lisp-code line (string #\Newline)))))))
(if (= (length lisp-code) 0)
(progn (setf (skill-entry-status entry) :ready) t)
@@ -199,7 +205,7 @@ Only loads blocks that specify a .lisp tangle target, ignoring tests and example
(harness-log "LOADER ERROR in skill '~a': ~a" skill-base-name msg)
(setf (skill-entry-status entry) :failed)
(setf (skill-entry-error-log entry) msg)
nil)))))
nil)))
(defun load-skill-with-timeout (filepath timeout-seconds)
"Loads a skill Org file with a hard execution timeout."

View File

@@ -10,29 +10,31 @@ A static, hardcoded architecture is inherently fragile. The ~opencortex~ Skill E
** Global Skill Registry
#+begin_src lisp :tangle ../library/skills.lisp
#+begin_src lisp :tangle ./skills.lisp
(in-package :opencortex)
(defun COSINE-SIMILARITY (v1 v2)
"Computes the cosine similarity between two vectors.
Both arguments should be sequences of numbers. Returns a value between -1.0 and 1.0."
(let ((len1 (length v1)) (len2 (length v2)))
"Computes the cosine similarity between two vectors."
(let* ((len1 (length v1))
(len2 (length v2)))
(if (or (zerop len1) (zerop len2))
0.0
(let ((dot-product 0.0d0)
(let* ((dot-product 0.0d0)
(norm1 0.0d0)
(norm2 0.0d0))
(let ((len (min len1 len2)))
(dotimes (i len)
(let ((x (coerce (elt v1 i) 'double-float)))
(let ((y (coerce (elt v2 i) 'double-float)))
(dotimes (i (min len1 len2))
(let* ((x (coerce (elt v1 i) 'double-float))
(y (coerce (elt v2 i) 'double-float)))
(incf dot-product (* x y))
(incf norm1 (* x x))
(incf norm2 (* y y))))))
(incf norm2 (* y y))))
(if (or (zerop norm1) (zerop norm2))
0.0
(/ dot-product (sqrt (* norm1 norm2))))))))
(defun VAULT-MASK-STRING (s) "[MASKED]") ; Stub
;; TODO: Stub for vault - implement later
(defun VAULT-MASK-STRING (s) "[MASKED]")
(defvar *VAULT-MEMORY* (make-hash-table :test 'equal))
@@ -84,26 +86,30 @@ Both arguments should be sequences of numbers. Returns a value between -1.0 and
#+end_src
** Skill File Analysis (parse-skill-metadata)
#+begin_src lisp :tangle ../library/skills.lisp
#+begin_src lisp :tangle ./skills.lisp
(defun parse-skill-metadata (filepath)
"Extracts ID and DEPENDS_ON tags using robust regex scanning."
"Extracts ID and DEPENDS_ON tags from org file."
(let ((dependencies nil)
(id nil)
(content (uiop:read-file-string filepath)))
;; Extract ID
(multiple-value-bind (match regs)
(ppcre:scan-to-strings "(?im:^:ID:\\s*([^\\s\\r\\n]+))" content)
(when match (setf id (aref regs 0))))
;; Extract all DEPENDS_ON lines
(ppcre:do-register-groups (deps-string)
("(?im:^#\\+DEPENDS_ON:\\s*(.*))" content)
(let ((deps (ppcre:split "\\s+" (string-trim " " deps-string))))
(setf dependencies (append dependencies (mapcar (lambda (s) (string-trim "[] " s)) deps)))))
(values id (remove-if (lambda (s) (= 0 (length s))) dependencies))))
;; Simple ID extraction using string search
(let ((id-start (search ":ID:" content)))
(when id-start
(let ((id-end (position #\Newline content :start id-start)))
(when id-end
(setf id (subseq content (+ id-start 4) id-end)))))
;; Simple DEPENDS_ON extraction
(let ((pos 0))
(loop while (setf pos (search "#+DEPENDS_ON:" content :start2 pos))
do (let ((end (position #\Newline content :start pos)))
(when end
(push (subseq content (+ pos 13) end) dependencies)
(setf pos end))))
(values id (reverse dependencies))))
#+end_src
** Dependency Resolution (topological-sort-skills)
#+begin_src lisp :tangle ../library/skills.lisp
#+begin_src lisp :tangle ./skills.lisp
(defun topological-sort-skills (skills-dir)
"Returns a list of skill filepaths sorted by dependency (dependencies first)."
(let ((files (uiop:directory-files skills-dir "org-skill-*.org"))
@@ -147,7 +153,7 @@ Both arguments should be sequences of numbers. Returns a value between -1.0 and
#+end_src
** Jailed Loading (load-skill-from-org)
#+begin_src lisp :tangle ../library/skills.lisp
#+begin_src lisp :tangle ./skills.lisp
(defun validate-lisp-syntax (code-string)
"Checks if a string contains valid, readable Common Lisp forms.
Delegates to the Lisp Validator skill when available; falls back to a basic
@@ -201,7 +207,7 @@ Only loads blocks that specify a .lisp tangle target, ignoring tests and example
((and in-lisp-block collect-this-block)
(unless (or (uiop:string-prefix-p ":PROPERTIES:" (string-upcase clean-line))
(uiop:string-prefix-p ":END:" (string-upcase clean-line)))
(setf lisp-code (concatenate 'string lisp-code line (string #\Newline))))))))
(setf lisp-code (concatenate 'string lisp-code line (string #\Newline)))))))
(if (= (length lisp-code) 0)
(progn (setf (skill-entry-status entry) :ready) t)
@@ -221,7 +227,7 @@ Only loads blocks that specify a .lisp tangle target, ignoring tests and example
(harness-log "LOADER ERROR in skill '~a': ~a" skill-base-name msg)
(setf (skill-entry-status entry) :failed)
(setf (skill-entry-error-log entry) msg)
nil)))))
nil)))
(defun load-skill-with-timeout (filepath timeout-seconds)
"Loads a skill Org file with a hard execution timeout."
@@ -246,7 +252,7 @@ Only loads blocks that specify a .lisp tangle target, ignoring tests and example
#+end_src
** Initializing All Skills (initialize-all-skills)
#+begin_src lisp :tangle ../library/skills.lisp
#+begin_src lisp :tangle ./skills.lisp
(defun initialize-all-skills ()
"Scans the directory defined by SKILLS_DIR and hot-loads skills using topological order."
(let* ((env-path (uiop:getenv "SKILLS_DIR"))
@@ -292,7 +298,7 @@ Only loads blocks that specify a .lisp tangle target, ignoring tests and example
#+end_src
** Toolbelt Prompt Generation (generate-tool-belt-prompt)
#+begin_src lisp :tangle ../library/skills.lisp
#+begin_src lisp :tangle ./skills.lisp
(defun generate-tool-belt-prompt ()
"Aggregates all registered cognitive tools into a descriptive prompt."
(let ((output (format nil "AVAILABLE TOOLS:
@@ -317,7 +323,7 @@ EXAMPLES:
** The Default Tool Belt
*** The Eval Tool (Internal Inspection)
#+begin_src lisp :tangle ../library/skills.lisp
#+begin_src lisp :tangle ./skills.lisp
(def-cognitive-tool :eval "Evaluates raw Common Lisp code in the harness image. Use this for complex calculations or internal state inspection."
((:code :type :string :description "The Lisp code to evaluate"))
:guard (lambda (args context)
@@ -335,7 +341,7 @@ EXAMPLES:
#+end_src
*** The Grep Tool (File Discovery)
#+begin_src lisp :tangle ../library/skills.lisp
#+begin_src lisp :tangle ./skills.lisp
(def-cognitive-tool :grep-search "Searches for a pattern in the project files."
((:pattern :type :string :description "The regex pattern to search for")
(:dir :type :string :description "Directory to search in (default is project root)"))
@@ -347,7 +353,7 @@ EXAMPLES:
#+end_src
*** The Shell Tool (Machine Actuation)
#+begin_src lisp :tangle ../library/skills.lisp
#+begin_src lisp :tangle ./skills.lisp
(def-cognitive-tool :shell "Executes a shell command on the local machine. Use this for file operations, system checks, or running tests."
((:cmd :type :string :description "The full bash command to execute"))
:guard (lambda (args context)
@@ -362,7 +368,7 @@ EXAMPLES:
#+end_src
*** The Reload-Skill Tool (Hot Reload)
#+begin_src lisp :tangle ../library/skills.lisp
#+begin_src lisp :tangle ./skills.lisp
(def-cognitive-tool :reload-skill "Reloads a skill from its Org-mode source file, recompiling into the live image without restarting the daemon."
((:skill :type :string :description "The skill name (e.g., \"org-skill-policy\") or full path to the .org file"))
:guard (lambda (args context)
@@ -398,7 +404,7 @@ EXAMPLES:
#+end_src
*** The File Read Tool (V 0.2.0 File I/O)
#+begin_src lisp :tangle ../library/skills.lisp
#+begin_src lisp :tangle ./skills.lisp
(def-cognitive-tool :read-file "Reads the contents of a file as a string."
((:file :type :string :description "The path to the file to read"))
:guard (lambda (args context)
@@ -417,7 +423,7 @@ EXAMPLES:
#+end_src
*** The File Write Tool (V 0.2.0 File I/O)
#+begin_src lisp :tangle ../library/skills.lisp
#+begin_src lisp :tangle ./skills.lisp
(def-cognitive-tool :write-file "Writes content to a file, creating it if it doesn't exist."
((:file :type :string :description "The path to the file to write")
(:content :type :string :description "The content to write")
@@ -449,7 +455,7 @@ EXAMPLES:
#+end_src
*** The String Replace Tool (V 0.2.0 File I/O)
#+begin_src lisp :tangle ../library/skills.lisp
#+begin_src lisp :tangle ./skills.lisp
(def-cognitive-tool :replace-string "Replaces occurrences of old-string with new-string in a file."
((:file :type :string :description "The path to the file")
(:old :type :string :description "The substring to find and replace")
@@ -484,7 +490,7 @@ EXAMPLES:
These tests verify the Skill Engine and loader. Run with:
~(fiveam:run! 'boot-suite)~
#+begin_src lisp :tangle ../tests/boot-sequence-tests.lisp
#+begin_src lisp :tangle ./tests/boot-sequence-tests.lisp
(defpackage :opencortex-boot-tests
(:use :cl :fiveam :opencortex)
(:export #:boot-suite))

View File

@@ -10,7 +10,7 @@
The OpenCortex TUI Client is a standalone Common Lisp application built on **Croatoan**. It provides a real-time, multi-window interface for interacting with the OpenCortex daemon.
* Implementation
#+begin_src lisp :tangle ../library/tui-client.lisp
#+begin_src lisp :tangle ./tui-client.lisp
(in-package :cl-user)
(defpackage :opencortex.tui
(:use :cl :croatoan)

View File

@@ -1,231 +0,0 @@
(in-package :opencortex)
(defun lisp-validator-check-structural (code-string)
"Checks for balanced parens, brackets, and terminated strings.
Returns (VALUES t nil) if clean, or (VALUES nil reason-string line col)."
(let ((stack nil)
(in-string nil)
(escaped nil)
(line 1)
(col 0)
(last-open-line 1)
(last-open-col 0))
(dotimes (i (length code-string)
(if (null stack)
(values t nil nil nil)
(values nil (format nil "Unbalanced '~a' opened at line ~a, col ~a"
(caar stack) last-open-line last-open-col)
last-open-line last-open-col)))
(let ((ch (char code-string i)))
(cond (escaped (setf escaped nil))
((char= ch #\\) (setf escaped t))
(in-string
(when (char= ch #\") (setf in-string nil)))
((char= ch #\;)
;; Skip to end of line
(loop while (and (< i (1- (length code-string)))
(not (char= (char code-string (1+ i)) #\Newline)))
do (incf i))
(incf line) (setf col 0))
((char= ch #\")
(setf in-string t))
((member ch '(#\( #\[))
(push (list (string ch) line col) stack)
(setf last-open-line line last-open-col col))
((char= ch #\))
(cond ((null stack)
(return-from lisp-validator-check-structural
(values nil (format nil "Unexpected ')' at line ~a, col ~a" line col) line col)))
((string= (caar stack) "[")
(return-from lisp-validator-check-structural
(values nil (format nil "Mismatched ']' expected at line ~a, col ~a" line col) line col)))
(t (pop stack))))
((char= ch #\])
(cond ((null stack)
(return-from lisp-validator-check-structural
(values nil (format nil "Unexpected ']' at line ~a, col ~a" line col) line col)))
((string= (caar stack) "(")
(return-from lisp-validator-check-structural
(values nil (format nil "Mismatched ')' expected at line ~a, col ~a" line col) line col)))
(t (pop stack))))
((char= ch #\Newline)
(incf line) (setf col 0)))
(unless (char= ch #\Newline) (incf col))))))
(defun lisp-validator-check-syntactic (code-string)
"Checks if the code can be read by SBCL with *read-eval* nil.
Returns (VALUES t nil) if clean, or (VALUES nil error-message line col)."
(handler-case
(let ((*read-eval* nil))
(with-input-from-string (stream (format nil "(progn ~a)" code-string))
(loop for form = (read stream nil :eof) until (eq form :eof)))
(values t nil nil nil))
(error (c)
(let ((msg (format nil "~a" c)))
(values nil msg nil nil)))))
(defparameter *lisp-validator-whitelist*
'(;; Math & Logic
+ - * / = < > <= >= 1+ 1- min max mod abs floor ceiling round
and or not null eq eql equal string= string-equal char= char-equal
;; List Manipulation
list cons car cdr cadr cddr cdar caar caddr cdddr append mapcar remove-if remove-if-not
length reverse sort nth nthcdr push pop last butlast subseq
;; Plists, Alists, and Hash Tables
getf gethash assoc acons pairlis rassoc
;; Control Flow
let let* if cond when unless case typecase prog1 progn
;; Strings
format concatenate string-downcase string-upcase search subseq replace
;; Type predicates
stringp numberp integerp listp symbolp keywordp null
;; Kernel safe symbols
opencortex::harness-log
opencortex::snapshot-memory opencortex::rollback-memory
opencortex::lookup-object opencortex::list-objects-by-type
opencortex::ingest-ast opencortex::find-headline-missing-id
opencortex::context-query-store opencortex::context-get-active-projects
opencortex::context-get-recent-completed-tasks opencortex::context-list-all-skills
opencortex::context-get-system-logs opencortex::context-assemble-global-awareness
opencortex::org-object-id opencortex::org-object-type opencortex::org-object-attributes
opencortex::org-object-content opencortex::org-object-parent-id
opencortex::org-object-children opencortex::org-object-version
opencortex::org-object-last-sync opencortex::org-object-hash
opencortex::org-object-vector
;; Essential macros and special operators
declare ignore quote function lambda defun defvar defparameter defmacro
;; Safe I/O
with-open-file write-string read-line
;; Package introspection
find-package make-package in-package do-external-symbols find-symbol
;; Safe system interaction
uiop:run-program uiop:getenv uiop:merge-pathnames* uiop:file-exists-p
uiop:directory-exists-p uiop:read-file-string uiop:split-string
;; Time
get-universal-time get-internal-real-time sleep
;; Equality
equalp = equal eq eql))
"Static whitelist of symbols permitted in the Lisp Validator sandbox."
(defvar *lisp-validator-registry* nil
"List of dynamically registered safe symbols.")
(defun lisp-validator-register (symbols)
"Adds symbols to the global validator registry."
(setf *lisp-validator-registry*
(append *lisp-validator-registry*
(if (listp symbols) symbols (list symbols))))
(harness-log "LISP VALIDATOR: Registered ~a new safe symbols."
(length (if (listp symbols) symbols (list symbols)))))
(defun lisp-validator-is-safe (symbol)
"Checks if a symbol is in the static whitelist or the dynamic registry."
(or (member symbol *lisp-validator-whitelist* :test #'string-equal)
(member symbol *lisp-validator-registry* :test #'string-equal)))
(defun lisp-validator-ast-walk (form)
"Recursively walks the Lisp AST. Returns T if safe, NIL if unsafe."
(cond
;; Self-evaluating objects are safe.
((or (stringp form) (numberp form) (keywordp form) (characterp form)) t)
;; Symbols used as variables (in non-function position)
((symbolp form) (lisp-validator-is-safe form))
;; Lists represent function calls or special forms.
((listp form)
(let ((head (car form)))
(cond
((eq head 'quote) t)
((not (symbolp head)) nil)
((lisp-validator-is-safe head)
(every #'lisp-validator-ast-walk (cdr form)))
(t
(harness-log "LISP VALIDATOR: Blocked call to non-whitelisted function ~a" head)
nil))))
(t nil)))
(defun lisp-validator-check-semantic (code-string)
"Checks if all symbols in CODE-STRING are whitelisted.
Returns (VALUES t nil) if clean, or (VALUES nil reason-string nil nil)."
(handler-case
(let ((*read-eval* nil))
(with-input-from-string (stream (format nil "(progn ~a)" code-string))
(loop for form = (read stream nil :eof)
until (eq form :eof)
do (unless (lisp-validator-ast-walk form)
(return-from lisp-validator-check-semantic
(values nil "Code contains non-whitelisted symbols." nil nil)))))
(values t nil nil nil))
(error (c)
(values nil (format nil "Semantic check failed: ~a" c) nil nil))))
(defun lisp-validator-validate (code-string &key strict)
"Validates Lisp code through structural, syntactic, and optional semantic checks.
Returns a plist:
(:status :success :checks (:structural t :syntactic t :semantic t))
or
(:status :error :failed <check-key> :reason <string> :line <n> :col <n>)
When STRICT is non-nil, the semantic whitelist check is enforced.
When STRICT is nil, semantic check is skipped for general validation."
(let ((structural-ok nil) (syntactic-ok nil) (semantic-ok nil)
(reason nil) (line nil) (col nil))
;; Phase 1: Structural
(multiple-value-setq (structural-ok reason line col)
(lisp-validator-check-structural code-string))
(unless structural-ok
(return-from lisp-validator-validate
(list :status :error :failed :structural :reason reason :line line :col col)))
;; Phase 2: Syntactic
(multiple-value-setq (syntactic-ok reason line col)
(lisp-validator-check-syntactic code-string))
(unless syntactic-ok
(return-from lisp-validator-validate
(list :status :error :failed :syntactic :reason reason :line line :col col)))
;; Phase 3: Semantic (only when strict)
(when strict
(multiple-value-setq (semantic-ok reason line col)
(lisp-validator-check-semantic code-string))
(unless semantic-ok
(return-from lisp-validator-validate
(list :status :error :failed :semantic :reason reason :line line :col col))))
;; All clear
(list :status :success
:checks (list :structural t :syntactic t :semantic (or (not strict) semantic-ok)))))
(def-cognitive-tool :validate-lisp
"Deterministically validates Lisp code for structural, syntactic, and semantic correctness.
Use this BEFORE declaring any Lisp code edit complete."
((:code :type :string :description "The Lisp code string to validate.")
(:strict :type :boolean :description "If non-nil, enforces the semantic whitelist."))
:body (lambda (args)
(let ((code (getf args :code))
(strict (getf args :strict)))
(if (and code (stringp code))
(lisp-validator-validate code :strict strict)
(list :status :error :reason "Missing :code argument.")))))
(defskill :skill-lisp-validator
:priority 900
:trigger (lambda (ctx)
;; Trigger on any eval or shell action, or when validation is explicitly requested
(let ((candidate (getf ctx :approved-action)))
(when candidate
(let ((payload (getf candidate :payload)))
(member (getf payload :action) '(:eval :shell))))))
:probabilistic nil
:deterministic (lambda (action context)
(declare (ignore context))
(let ((payload (getf action :payload)))
(if (eq (getf payload :action) :eval)
(let* ((code (getf payload :code))
(result (lisp-validator-validate code :strict t)))
(if (eq (getf result :status) :error)
(progn
(harness-log "LISP VALIDATOR: Blocked unsafe :eval action. ~a"
(getf result :reason))
(list :type :LOG
:payload (list :level :error
:text (format nil "LISP VALIDATOR: Blocked unsafe eval. ~a"
(getf result :reason)))))
action))
action))))

View File

@@ -1,84 +1,38 @@
(defsystem :opencortex
:name "opencortex"
:author "Amr"
:version "0.1.0"
:version "0.2.0"
:license "AGPLv3"
:description "The Probabilistic-Deterministic Lisp Machine Harness"
:description "The Probabilistic-Deterministic Lisp Machine"
:depends-on (:usocket ; TCP socket networking
:bordeaux-threads ; Threading (heartbeat, async sensors)
:dexador ; HTTP client (LLM APIs)
:uiop ; Portable I/O, file operations
:cl-dotenv ; Environment variable loading
:cl-ppcre ; Regular expressions (parsing)
:hunchentoot ; HTTP server (optional web interface)
:ironclad ; Cryptography (Merkle hashing)
:str ; String utilities
:cl-json ; JSON parsing/serialization
:uuid) ; UUID generation for org-mode IDs
:depends-on (:usocket :bordeaux-threads :dexador :uiop :cl-dotenv :cl-ppcre :hunchentoot :ironclad :str :cl-json :uuid)
:serial t ; Load files in order listed below
: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 "library/package") ; Package definitions, core vars
(:file "library/skills") ; Skill engine, cognitive tools
(:file "library/communication") ; Protocol, framing
(:file "library/communication-validator") ; Schema validation
(:file "library/memory") ; Org-object store, snapshots
(:file "library/context") ; Context assembly, query
(:file "library/perceive") ; Stage 1: Sensory normalization
(:file "library/reason") ; Stage 2: Neural + deterministic
(:file "library/act") ; Stage 3: Actuation
(:file "library/loop")) ; Main entry, heartbeat
(:file "skills/org-skill-policy")
(:file "skills/org-skill-bouncer")
(:file "skills/org-skill-scribe")
(:file "skills/org-skill-gardener")
(:file "skills/org-skill-lisp-utils")
(:file "skills/org-skill-literate-programming")
(:file "skills/org-skill-engineering-standards")
(:file "skills/org-skill-self-edit")
(:file "skills/org-skill-emacs-edit")
(:file "skills/org-skill-tool-permissions")
(:file "skills/org-skill-self-fix")
(:file "skills/org-skill-lisp-validator")
(:file "skills/org-skill-peripheral-vision"))
:serial t
:build-operation "program-op"
:build-pathname "opencortex-server"
:entry-point "opencortex:main")
(defsystem :opencortex/tests
:depends-on (:opencortex ; The harness we're testing
:fiveam) ; Testing framework
:components ((:file "library/gen/org-skill-engineering-standards")
(:file "library/gen/org-skill-literate-programming")
(:file "library/gen/org-skill-self-edit")
(:file "library/gen/org-skill-emacs-edit")
(:file "library/gen/org-skill-lisp-utils")
(:file "tests/engineering-standards-tests")
(:file "tests/literate-programming-tests")
(:file "tests/pipeline-perceive-tests")
(:file "tests/pipeline-reason-tests")
(:file "tests/pipeline-act-tests")
(:file "tests/act-tests")
(:file "tests/boot-sequence-tests")
(:file "tests/memory-tests")
(:file "tests/immune-system-tests")
(:file "tests/emacs-edit-tests")
(:file "tests/lisp-utils-tests")
(:file "tests/lisp-validator-tests")
(:file "tests/self-edit-tests")
(:file "tests/tool-permissions-tests")
(:file "tests/peripheral-vision-tests"))
:perform (test-op (o s)
(uiop:symbol-call :fiveam :run!
(uiop:find-symbol* :communication-protocol-suite :opencortex-tests))
(uiop:symbol-call :fiveam :run!
(uiop:find-symbol* :pipeline-suite :opencortex-pipeline-tests))
(uiop:symbol-call :fiveam :run!
(uiop:find-symbol* :boot-suite :opencortex-boot-tests))
(uiop:symbol-call :fiveam :run!
(uiop:find-symbol* :memory-suite :opencortex-memory-tests))
(uiop:symbol-call :fiveam :run!
(uiop:find-symbol* :immune-suite :opencortex-immune-system-tests))
(uiop:symbol-call :fiveam :run!
(uiop:find-symbol* :emacs-edit-suite :opencortex-emacs-edit-tests))
(uiop:symbol-call :fiveam :run!
(uiop:find-symbol* :lisp-utils-suite :opencortex-lisp-utils-tests))))
(defsystem :opencortex/tui
:depends-on (:opencortex ; The daemon we're connecting to
:croatoan ; Terminal UI library
:usocket ; Socket communication
:bordeaux-threads) ; Background listening thread
:components ((:file "library/tui-client")))

View File

@@ -38,7 +38,7 @@ When something is blocked, the logs clearly show which layer blocked it and why.
* Package Context
#+begin_src lisp :tangle ../library/gen/org-skill-bouncer.lisp
#+begin_src lisp :tangle ./org-skill-bouncer.lisp
(in-package :opencortex)
#+end_src
@@ -58,7 +58,7 @@ The Bouncer implements the 5-Vector security model:
The vault stores sensitive credentials. This check scans action text for vault secrets to prevent accidental exposure.
#+begin_src lisp :tangle ../library/gen/org-skill-bouncer.lisp
#+begin_src lisp :tangle ./org-skill-bouncer.lisp
(defun bouncer-scan-secrets (text)
"Scans TEXT for known secrets from the vault.
@@ -91,7 +91,7 @@ The vault stores sensitive credentials. This check scans action text for vault s
Detects when shell commands try to send data to untrusted network destinations.
#+begin_src lisp :tangle ../library/gen/org-skill-bouncer.lisp
#+begin_src lisp :tangle ./org-skill-bouncer.lisp
(defvar *bouncer-network-whitelist*
'("api.telegram.org" "matrix.org" "googleapis.com" "openai.com" "anthropic.com")
"Domains that the Bouncer considers safe for outbound connections.
@@ -129,7 +129,7 @@ Detects when shell commands try to send data to untrusted network destinations.
** bouncer-check: Main Security Gate
#+begin_src lisp :tangle ../library/gen/org-skill-bouncer.lisp
#+begin_src lisp :tangle ./org-skill-bouncer.lisp
(defun bouncer-check (action context)
"The 5-Vector security gate for high-risk actions.
@@ -213,7 +213,7 @@ Detects when shell commands try to send data to untrusted network destinations.
When a flight plan is approved in Emacs, the Bouncer detects it and re-injects the action.
#+begin_src lisp :tangle ../library/gen/org-skill-bouncer.lisp
#+begin_src lisp :tangle ./org-skill-bouncer.lisp
(defun bouncer-process-approvals ()
"Scans the object store for APPROVED flight plans and re-injects them.
@@ -269,7 +269,7 @@ When a flight plan is approved in Emacs, the Bouncer detects it and re-injects t
When the Bouncer intercepts a high-risk action, it creates a flight plan node for manual approval.
#+begin_src lisp :tangle ../library/gen/org-skill-bouncer.lisp
#+begin_src lisp :tangle ./org-skill-bouncer.lisp
(defun bouncer-create-flight-plan (blocked-action)
"Creates an Org node representing a pending flight plan for manual approval.
@@ -306,7 +306,7 @@ When the Bouncer intercepts a high-risk action, it creates a flight plan node fo
** Main Gate Function
#+begin_src lisp :tangle ../library/gen/org-skill-bouncer.lisp
#+begin_src lisp :tangle ./org-skill-bouncer.lisp
(defun bouncer-deterministic-gate (action context)
"Main deterministic gate for the Bouncer skill.
@@ -345,7 +345,7 @@ When the Bouncer intercepts a high-risk action, it creates a flight plan node fo
** Skill Registration
#+begin_src lisp :tangle ../library/gen/org-skill-bouncer.lisp
#+begin_src lisp :tangle ./org-skill-bouncer.lisp
(defskill :skill-bouncer
:priority 150
:trigger (lambda (ctx) (declare (ignore ctx)) t)

View File

@@ -11,7 +11,7 @@ The *CLI Gateway* is the primary sensory and actuating interface for human inter
* Implementation
#+begin_src lisp :tangle ../library/gen/org-skill-cli-gateway.lisp
#+begin_src lisp :tangle ./org-skill-cli-gateway.lisp
(defvar *cli-port* 9105)
(defvar *cli-server-socket* nil)

View File

@@ -33,7 +33,7 @@ Securely manage all authentication tokens required for the opencortex to operate
The vault provides a secure lookup table in RAM, backed by the persistent Memory. Access is restricted to internal kernel requests and explicitly authorized deterministic gates.
** 2. Semantic Interfaces
#+begin_src lisp :tangle ../library/gen/org-skill-credentials-vault.lisp
#+begin_src lisp :tangle ./org-skill-credentials-vault.lisp
(defun vault-get-secret (provider &key type)
"Retrieves a secret (api-key or session) for a provider.")
@@ -61,13 +61,13 @@ Tests in `tests/vault-tests.lisp` will verify:
* Phase D: Build (Implementation)
** Package Context
#+begin_src lisp :tangle ../library/gen/org-skill-credentials-vault.lisp
#+begin_src lisp :tangle ./org-skill-credentials-vault.lisp
#+end_src
** Vault State
We maintain an in-memory hash table for secrets, which is hydrated from and persisted to the Memory.
#+begin_src lisp :tangle ../library/gen/org-skill-credentials-vault.lisp
#+begin_src lisp :tangle ./org-skill-credentials-vault.lisp
(defvar opencortex::*vault-memory* (make-hash-table :test 'equal)
"In-memory cache of sensitive credentials.")
#+end_src
@@ -75,7 +75,7 @@ We maintain an in-memory hash table for secrets, which is hydrated from and pers
** Helper: Secret Masking
The `vault-mask-string` function ensures that diagnostic output never contains the full plaintext of a sensitive token.
#+begin_src lisp :tangle ../library/gen/org-skill-credentials-vault.lisp
#+begin_src lisp :tangle ./org-skill-credentials-vault.lisp
(defun vault-mask-string (str)
"Returns a masked version of a sensitive string."
(if (and str (> (length str) 8))
@@ -86,7 +86,7 @@ The `vault-mask-string` function ensures that diagnostic output never contains t
** Retrieval (vault-get-secret)
This function is the secure getter for all system secrets. It prioritizes the Vault (Memory) and falls back to environment variables for legacy compatibility.
#+begin_src lisp :tangle ../library/gen/org-skill-credentials-vault.lisp
#+begin_src lisp :tangle ./org-skill-credentials-vault.lisp
(defun vault-get-secret (provider &key (type :api-key))
"Retrieves a credential. Type can be :api-key or :session."
(let* ((key (format nil "~a-~a" provider type))
@@ -112,7 +112,7 @@ This function is the secure getter for all system secrets. It prioritizes the Va
** Persistence (vault-set-secret)
When a secret is updated, we immediately snapshot the Memory to ensure the credential change is versioned and durable.
#+begin_src lisp :tangle ../library/gen/org-skill-credentials-vault.lisp
#+begin_src lisp :tangle ./org-skill-credentials-vault.lisp
(defun vault-set-secret (provider secret &key (type :api-key))
"Securely stores a secret and triggers a Merkle snapshot."
(let ((key (format nil "~a-~a" provider type)))
@@ -125,7 +125,7 @@ When a secret is updated, we immediately snapshot the Memory to ensure the crede
** Onboarding Logic
Retained from the legacy Google skill, this provides the instructions for the autonomous cookie handshake.
#+begin_src lisp :tangle ../library/gen/org-skill-credentials-vault.lisp
#+begin_src lisp :tangle ./org-skill-credentials-vault.lisp
(defun vault-onboard-gemini-web ()
"Instructions for the Autonomous Cookie Handshake."
(harness-log "--- GEMINI WEB ONBOARDING ---")
@@ -137,7 +137,7 @@ Retained from the legacy Google skill, this provides the instructions for the au
#+end_src
** Registration
#+begin_src lisp :tangle ../library/gen/org-skill-credentials-vault.lisp
#+begin_src lisp :tangle ./org-skill-credentials-vault.lisp
(progn
(defskill :skill-credentials-vault
:priority 200 ; High priority, foundational
@@ -153,7 +153,7 @@ Retained from the legacy Google skill, this provides the instructions for the au
Note: Tests disabled in jail load.
** 1. Unit Tests (FiveAM)
#+begin_src lisp :tangle ../library/gen/org-skill-credentials-vault.lisp
#+begin_src lisp :tangle ./org-skill-credentials-vault.lisp
#|
(defpackage :opencortex-vault-tests
(:use :cl :fiveam :opencortex))

View File

@@ -58,14 +58,14 @@ Single entry point `emacs-edit-modify` takes a file path, operation, and paramet
* Phase D: Build (Implementation)
** Package Context
#+begin_src lisp :tangle ../library/gen/org-skill-emacs-edit.lisp
#+begin_src lisp :tangle ./org-skill-emacs-edit.lisp
(in-package :opencortex)
#+end_src
** ID Generation
Generate unique IDs for headlines.
#+begin_src lisp :tangle ../library/gen/org-skill-emacs-edit.lisp
#+begin_src lisp :tangle ./org-skill-emacs-edit.lisp
(defun emacs-edit-generate-id ()
"Generates a unique ID for org-mode headlines.
Format: 8-char hex + timestamp for uniqueness."
@@ -84,7 +84,7 @@ Format: 8-char hex + timestamp for uniqueness."
** Org Printer (AST → Org Format)
Converts AST back to org format, preserving structure.
#+begin_src lisp :tangle ../library/gen/org-skill-emacs-edit.lisp
#+begin_src lisp :tangle ./org-skill-emacs-edit.lisp
(defun emacs-edit-print-headline (ast &key indent-level)
"Converts a HEADLINE AST node to org text.
INDENT-LEVEL is number of leading asterisks."
@@ -154,7 +154,7 @@ Preserves structure including #+begin_src blocks."
** Read Operation
Parse org file to AST.
#+begin_src lisp :tangle ../library/gen/org-skill-emacs-edit.lisp
#+begin_src lisp :tangle ./org-skill-emacs-edit.lisp
(defvar *org-parser-cache* (make-hash-table :test 'equal)
"Cache for parsed org files.")
@@ -180,7 +180,7 @@ Returns the parsed AST. Uses cache for performance."
** Write Operation
Write AST back to file preserving structure.
#+begin_src lisp :tangle ../library/gen/org-skill-emacs-edit.lisp
#+begin_src lisp :tangle ./org-skill-emacs-edit.lisp
(defun emacs-edit-write-file (file-path ast)
"Writes AST back to FILE-PATH, preserving org structure.
Clears cache after write."
@@ -194,7 +194,7 @@ Clears cache after write."
** Add Headline Operation
Add a new headline to an existing AST.
#+begin_src lisp :tangle ../library/gen/org-skill-emacs-edit.lisp
#+begin_src lisp :tangle ./org-skill-emacs-edit.lisp
(defun emacs-edit-add-headline (ast title &key todo properties)
"Adds a new headline to AST.
Returns modified AST."
@@ -223,7 +223,7 @@ Returns modified AST."
** Set Property Operation
Set a property on an existing headline (by ID or TITLE).
#+begin_src lisp :tangle ../library/gen/org-skill-emacs-edit.lisp
#+begin_src lisp :tangle ./org-skill-emacs-edit.lisp
(defun emacs-edit-find-headline-by-id (ast target-id)
"Recursively finds headline with matching :ID: property."
(when (eq (getf ast :type) :headline)
@@ -267,7 +267,7 @@ Returns modified AST."
** Set TODO State Operation
Change TODO state (TODO → DONE → etc).
#+begin_src lisp :tangle ../library/gen/org-skill-emacs-edit.lisp
#+begin_src lisp :tangle ./org-skill-emacs-edit.lisp
(defun emacs-edit-set-todo (ast target new-state)
"Sets TODO state on headline matching TARGET.
NEW-STATE should be 'TODO', 'DONE', 'IN-PROGRESS', etc."
@@ -278,7 +278,7 @@ NEW-STATE should be 'TODO', 'DONE', 'IN-PROGRESS', etc."
** Unified Entry Point
Main operation dispatcher.
#+begin_src lisp :tangle ../library/gen/org-skill-emacs-edit.lisp
#+begin_src lisp :tangle ./org-skill-emacs-edit.lisp
(defun emacs-edit-modify (file-path operation &key params)
"Main entry point for org-mode file manipulation.
OPERATIONS:
@@ -321,7 +321,7 @@ OPERATIONS:
** Cognitive Tools
Exposes operations to the Probabilistic Engine.
#+begin_src lisp :tangle ../library/gen/org-skill-emacs-edit.lisp
#+begin_src lisp :tangle ./org-skill-emacs-edit.lisp
(def-cognitive-tool :org-read
"Reads an org-mode file and parses it to structured AST.
Use this BEFORE modifying org files to understand their structure."
@@ -388,7 +388,7 @@ Use this AFTER modifications to save changes."
#+end_src
* Phase E: Chaos (Verification)
#+begin_src lisp :tangle ../tests/emacs-edit-tests.lisp
#+begin_src lisp :tangle ./tests/emacs-edit-tests.lisp
(defpackage :opencortex-emacs-edit-tests
(:use :cl :fiveam :opencortex)
(:export #:emacs-edit-suite))

View File

@@ -107,7 +107,7 @@ The engineering standards skill is a HARD BLOCK gate. Violations are rejected, n
** Pre-Task Enforcement (Blocking)
#+begin_src lisp :tangle ../library/gen/org-skill-engineering-standards.lisp
#+begin_src lisp :tangle ./org-skill-engineering-standards.lisp
(in-package :opencortex)
(defvar *engineering-std-*project-root* nil
@@ -137,7 +137,7 @@ The engineering standards skill is a HARD BLOCK gate. Violations are rejected, n
** Git Clean Check (Blocking)
#+begin_src lisp :tangle ../library/gen/org-skill-engineering-standards.lisp
#+begin_src lisp :tangle ./org-skill-engineering-standards.lisp
(defun verify-git-clean-p (&optional (dir *engineering-std-*project-root*))
"Returns T if the git repository at DIR has no uncommitted changes."
(when dir
@@ -162,7 +162,7 @@ The engineering standards skill is a HARD BLOCK gate. Violations are rejected, n
These tests verify the enforcement logic. Run with:
~(fiveam:run! 'engineering-standards-suite)~
#+begin_src lisp :tangle ../tests/engineering-standards-tests.lisp
#+begin_src lisp :tangle ./tests/engineering-standards-tests.lisp
(defpackage :opencortex-engineering-standards-tests
(:use :cl :fiveam :opencortex)
(:export #:engineering-standards-suite))
@@ -229,7 +229,7 @@ These tests verify the enforcement logic. Run with:
** Blocking Gate (Hard Enforcement)
#+begin_src lisp :tangle ../library/gen/org-skill-engineering-standards.lisp
#+begin_src lisp :tangle ./org-skill-engineering-standards.lisp
(defun engineering-standards-gate (action context)
"The deterministic HARD BLOCK gate for Engineering Standards.
@@ -262,7 +262,7 @@ These tests verify the enforcement logic. Run with:
The skill runs at highest priority (1000) to block violations before any other skill.
#+begin_src lisp :tangle ../library/gen/org-skill-engineering-standards.lisp
#+begin_src lisp :tangle ./org-skill-engineering-standards.lisp
(defskill :skill-engineering-standards
:priority 1000
:trigger (lambda (ctx)
@@ -274,7 +274,7 @@ The skill runs at highest priority (1000) to block violations before any other s
** Initialize Project Root
#+begin_src lisp :tangle ../library/gen/org-skill-engineering-standards.lisp
#+begin_src lisp :tangle ./org-skill-engineering-standards.lisp
(defvar *engineering-std-initialized* nil)
(defun engineering-std-init ()

View File

@@ -37,14 +37,14 @@ The Gardener runs on a low-priority heartbeat. It performs a "Deep Audit" of the
* Phase D: Build (Implementation)
** Package Context
#+begin_src lisp :tangle ../library/gen/org-skill-gardener.lisp
#+begin_src lisp :tangle ./org-skill-gardener.lisp
(in-package :opencortex)
#+end_src
** State: Maintenance Cycle
We track the last audit time to ensure the Gardener doesn't over-consume resources.
#+begin_src lisp :tangle ../library/gen/org-skill-gardener.lisp
#+begin_src lisp :tangle ./org-skill-gardener.lisp
(defvar *gardener-last-audit* 0
"The universal-time of the last full Memex audit.")
#+end_src
@@ -52,7 +52,7 @@ We track the last audit time to ensure the Gardener doesn't over-consume resourc
** Audit: Broken Links
Scans the content of all objects for `id:` links and verifies the targets exist.
#+begin_src lisp :tangle ../library/gen/org-skill-gardener.lisp
#+begin_src lisp :tangle ./org-skill-gardener.lisp
(defun gardener-find-broken-links ()
"Returns a list of broken ID links found in the Memex."
(let ((broken nil))
@@ -69,7 +69,7 @@ Scans the content of all objects for `id:` links and verifies the targets exist.
** Audit: Orphaned Nodes
Identifies nodes that are not linked to and do not link to anything else.
#+begin_src lisp :tangle ../library/gen/org-skill-gardener.lisp
#+begin_src lisp :tangle ./org-skill-gardener.lisp
(defun gardener-find-orphans ()
"Returns a list of IDs for headlines that are structurally isolated."
(let ((inbound (make-hash-table :test 'equal))
@@ -95,7 +95,7 @@ Identifies nodes that are not linked to and do not link to anything else.
** Skill Logic: The Audit Pass
The Gardener's deterministic gate performs the actual analysis and logs the results. In future versions, it will generate probabilistic repair proposals.
#+begin_src lisp :tangle ../library/gen/org-skill-gardener.lisp
#+begin_src lisp :tangle ./org-skill-gardener.lisp
(defun gardener-deterministic-gate (action context)
"Main gate for the Gardener skill. Audits graph integrity."
(declare (ignore action context))
@@ -118,7 +118,7 @@ The Gardener's deterministic gate performs the actual analysis and logs the resu
#+end_src
** Skill Registration
#+begin_src lisp :tangle ../library/gen/org-skill-gardener.lisp
#+begin_src lisp :tangle ./org-skill-gardener.lisp
(defskill :skill-gardener
:priority 40
:trigger (lambda (ctx)

View File

@@ -11,7 +11,7 @@ The *Homoiconic Memory* skill provides the core persistence layer for OpenCortex
* Implementation
#+begin_src lisp :tangle ../library/gen/org-skill-homoiconic-memory.lisp
#+begin_src lisp :tangle ./org-skill-homoiconic-memory.lisp
(defun memory-org-to-json (source)
"Converts Org-mode source to JSON AST."

View File

@@ -61,14 +61,14 @@ Separate repair functions that can be called independently.
* Phase D: Build (Implementation)
** Package Context
#+begin_src lisp :tangle ../library/gen/org-skill-lisp-utils.lisp
#+begin_src lisp :tangle ./org-skill-lisp-utils.lisp
(in-package :opencortex)
#+end_src
** Character & String Utilities
General-purpose utilities for string manipulation.
#+begin_src lisp :tangle ../library/gen/org-skill-lisp-utils.lisp
#+begin_src lisp :tangle ./org-skill-lisp-utils.lisp
(defun count-char (char string)
"Counts occurrences of CHAR in STRING.
Returns an integer count."
@@ -83,7 +83,7 @@ Returns an integer count."
Attempts instant fixes on broken Lisp code (e.g., balancing parens).
This is the fast path - used for simple syntax errors.
#+begin_src lisp :tangle ../library/gen/org-skill-lisp-utils.lisp
#+begin_src lisp :tangle ./org-skill-lisp-utils.lisp
(defun deterministic-repair (code)
"Attempts instant fixes on broken Lisp code (e.g., balancing parens).
Returns the fixed code string."
@@ -99,7 +99,7 @@ Returns the fixed code string."
Uses the LLM to deeply repair syntax structure when deterministic fails.
This is the slow path - used for complex errors.
#+begin_src lisp :tangle ../library/gen/org-skill-lisp-utils.lisp
#+begin_src lisp :tangle ./org-skill-lisp-utils.lisp
(defun neural-repair (code error-message)
"Uses the Probabilistic Engine to deeply repair the syntax structure.
Returns the fixed code string."
@@ -117,7 +117,7 @@ MANDATE: Output EXACTLY ONE valid Common Lisp list. Do not explain. Do not use m
Scans the raw string character-by-character, tracking open/close pairs.
This is O(n) and does not invoke the Lisp reader.
#+begin_src lisp :tangle ../library/gen/org-skill-lisp-utils.lisp
#+begin_src lisp :tangle ./org-skill-lisp-utils.lisp
(defun lisp-utils-check-structural (code-string)
"Checks for balanced parens, brackets, and terminated strings.
Returns (VALUES t nil) if clean, or (VALUES nil reason-string line col)."
@@ -173,7 +173,7 @@ Returns (VALUES t nil) if clean, or (VALUES nil reason-string line col)."
** Check 2: Syntactic Validation (Reader Check)
Wraps the code and attempts to read with *read-eval* disabled.
#+begin_src lisp :tangle ../library/gen/org-skill-lisp-utils.lisp
#+begin_src lisp :tangle ./org-skill-lisp-utils.lisp
(defun lisp-utils-check-syntactic (code-string)
"Checks if the code can be read by SBCL with *read-eval* nil.
Returns (VALUES t nil) if clean, or (VALUES nil error-message nil nil)."
@@ -190,7 +190,7 @@ Returns (VALUES t nil) if clean, or (VALUES nil error-message nil nil)."
** Check 3: Semantic Validation (Whitelist AST Walk)
Recursively walks the parsed AST and verifies whitelisted symbols.
#+begin_src lisp :tangle ../library/gen/org-skill-lisp-utils.lisp
#+begin_src lisp :tangle ./org-skill-lisp-utils.lisp
(defparameter *lisp-utils-whitelist*
'(;; Math & Logic
+ - * / = < > <= >= 1+ 1- min max mod abs floor ceiling round
@@ -272,7 +272,7 @@ Returns (VALUES t nil) if clean, or (VALUES nil reason-string nil nil)."
** Unified Entry Point
Orchestrates the three validation checks.
#+begin_src lisp :tangle ../library/gen/org-skill-lisp-utils.lisp
#+begin_src lisp :tangle ./org-skill-lisp-utils.lisp
(defun lisp-utils-validate (code-string &key strict)
"Validates Lisp code through structural, syntactic, and optional semantic checks.
Returns a plist:
@@ -310,7 +310,7 @@ When STRICT is non-nil, the semantic whitelist check is enforced."
** Cognitive Tools
Exposes utilities to the Probabilistic Engine.
#+begin_src lisp :tangle ../library/gen/org-skill-lisp-utils.lisp
#+begin_src lisp :tangle ./org-skill-lisp-utils.lisp
(def-cognitive-tool :validate-lisp
"Deterministically validates Lisp code for structural, syntactic, and semantic correctness.
Use this BEFORE declaring any Lisp code edit complete."
@@ -348,7 +348,7 @@ Use this BEFORE declaring any Lisp code edit complete."
** Skill Definition: Lisp Repair
Intercepts :syntax-error events and repairs the code.
#+begin_src lisp :tangle ../library/gen/org-skill-lisp-utils.lisp
#+begin_src lisp :tangle ./org-skill-lisp-utils.lisp
(defskill :skill-lisp-repair
:priority 90
:trigger (lambda (ctx) (eq (getf (getf ctx :payload) :sensor) :syntax-error))
@@ -379,7 +379,7 @@ Intercepts :syntax-error events and repairs the code.
** Skill Definition: Lisp Validator
Validates all Lisp code before execution.
#+begin_src lisp :tangle ../library/gen/org-skill-lisp-utils.lisp
#+begin_src lisp :tangle ./org-skill-lisp-utils.lisp
(defskill :skill-lisp-validator
:priority 900
:trigger (lambda (ctx)
@@ -407,7 +407,7 @@ Validates all Lisp code before execution.
#+end_src
* Phase E: Chaos (Verification)
#+begin_src lisp :tangle ../tests/lisp-utils-tests.lisp
#+begin_src lisp :tangle ./tests/lisp-utils-tests.lisp
(defpackage :opencortex-lisp-utils-tests
(:use :cl :fiveam :opencortex)
(:export #:lisp-utils-suite))
@@ -511,7 +511,7 @@ Validates all Lisp code before execution.
These tests verify the Lisp Validator gate. Run with:
~(fiveam:run! 'lisp-validator-suite)~
#+begin_src lisp :tangle ../tests/lisp-validator-tests.lisp
#+begin_src lisp :tangle ./tests/lisp-validator-tests.lisp
(defpackage :opencortex-lisp-validator-tests
(:use :cl :fiveam :opencortex)
(:export #:lisp-validator-suite))

View File

@@ -0,0 +1,125 @@
(defparameter *lisp-validator-whitelist*
'(;; Math & Logic
+ - * / = < > <= >= 1+ 1- min max
and or not null eq eql equal string= string-equal
;; List Manipulation
list cons car cdr cadr cddr cdar caar append mapcar remove-if remove-if-not
length reverse sort nth nthcdr push pop
;; Plists and Hash Tables
getf gethash
;; Control Flow
let let* if cond when unless case typecase
;; Strings
format concatenate string-downcase string-upcase search
;; Kernel specifics
opencortex::harness-log
opencortex::snapshot-memory
opencortex::rollback-memory
opencortex::lookup-object
opencortex::list-objects-by-type
opencortex::ingest-ast
opencortex::find-headline-missing-id
opencortex::context-query-store
opencortex::context-get-active-projects
opencortex::context-get-recent-completed-tasks
opencortex::context-list-all-skills
opencortex::context-get-system-logs
opencortex::context-assemble-global-awareness
opencortex::org-object-id
opencortex::org-object-type
opencortex::org-object-attributes
opencortex::org-object-content
opencortex::org-object-parent-id
opencortex::org-object-children
opencortex::org-object-version
opencortex::org-object-last-sync
opencortex::org-object-hash
;; Essential macros
declare ignore
;; Let's also add simple data types
t nil quote function))
(defvar *lisp-validator-registry* nil
"List of dynamically registered safe symbols.")
(defun lisp-validator-register (symbols)
"Adds symbols to the global validator registry."
(setf *lisp-validator-registry* (append *lisp-validator-registry* (if (listp symbols) symbols (list symbols))))
(harness-log "LISP VALIDATOR: Registered ~a new safe symbols." (length (if (listp symbols) symbols (list symbols)))))
(defun lisp-validator-is-safe (symbol)
"Checks if a symbol is in the static whitelist or the dynamic registry."
(or (member symbol *lisp-validator-whitelist* :test #'string-equal)
(member symbol *lisp-validator-registry* :test #'string-equal)))
(defun lisp-validator-ast-walk (form)
"Recursively walks the Lisp AST. Returns T if safe, NIL if unsafe."
(cond
;; Self-evaluating objects (strings, numbers, keywords) are safe.
((or (stringp form) (numberp form) (keywordp form) (characterp form))
t)
;; Symbols used as variables (in non-function position)
((symbolp form)
(lisp-validator-is-safe form))
;; Lists represent function calls or special forms.
((listp form)
(let ((head (car form)))
(cond
((eq head 'quote) t)
((not (symbolp head)) nil)
((lisp-validator-is-safe head)
(every #'lisp-validator-ast-walk (cdr form)))
(t
(harness-log "LISP VALIDATOR: Blocked call to non-whitelisted function ~a" head)
nil))))
(t nil)))
(opencortex:def-cognitive-tool :lisp-validator-status "Returns validator-related telemetry, including blocked actions and harness status."
nil
:body (lambda (args)
(declare (ignore args))
(format nil "LISP VALIDATOR STATUS:
- Static Whitelist: ~a symbols
- Dynamic Registry: ~a symbols
- Total Blocked Actions: ~a"
(length *lisp-validator-whitelist*)
(length *lisp-validator-registry*)
"Not implemented")))
(opencortex:defskill :skill-lisp-validator
:priority 900 ; High priority, before most skills
:trigger (lambda (ctx)
;; Check if any proposed action is an :eval or :shell call
(let ((candidate (getf ctx :candidate)))
(when candidate
(let ((payload (getf candidate :payload)))
(member (getf payload :action) '(:eval :shell))))))
:probabilistic nil ; Purely deterministic/safety skill
:deterministic (lambda (action context)
(harness-log "DETERMINISTIC ENGINE [Lisp-Validator]: Intercepted critical action for structural validation.")
action))
(defpackage :opencortex-lisp-validator-tests
(:use :cl :fiveam :opencortex)
(:export #:lisp-validator-suite))
(in-package :opencortex-lisp-validator-tests)
(def-suite lisp-validator-suite :description "Tests for the Lisp Validator.")
(in-suite lisp-validator-suite)
(test test-basic-math-safe
(is (opencortex:lisp-validator-validate "(+ 1 2)")))
(test test-blocked-eval
(is (not (opencortex:lisp-validator-validate "(eval '(+ 1 2))"))))
(test test-blocked-shell
(is (not (opencortex:lisp-validator-validate "(uiop:run-program \"ls\")"))))
(test test-nested-unsafe
(is (not (opencortex:lisp-validator-validate "(let ((x 1)) (delete-file \"test.txt\"))"))))
(test test-safe-kernel-api
(is (opencortex:lisp-validator-validate "(opencortex::lookup-object \"node-1\")")))

View File

@@ -0,0 +1,182 @@
:PROPERTIES:
:ID: 98576df2-c496-4e4a-9acb-0bca514a0305
:CREATED: [2026-03-31 Tue 18:28]
:EDITED: [2026-04-09 Thu]
:END:
#+TITLE: SKILL: Lisp Validator
#+STARTUP: content
#+FILETAGS: :security:lisp:ast:autonomy:
* Overview
The *Lisp Validator* is the primary structural gate for the Probabilistic-Deterministic Lisp Machine. It provides a recursive AST validator that subjects all Lisp proposals from the Probabilistic Engine to a strict "Deny-by-Default" sandbox.
* Phase A: Demand (PRD)
:PROPERTIES:
:STATUS: FROZEN
:END:
** 1. Purpose
Define a high-integrity, recursive security sandbox for Lisp execution.
** 2. User Needs
- *Recursive Validation:* Every nested function call and variable access MUST be checked.
- *Deny-by-Default:* Only explicitly whitelisted functions and variables are permitted.
- *Eval Protection:* Block all forms of `eval`, `load`, or dynamic execution.
- *Deterministic Preemption:* This skill acts as a mandatory global Deterministic Engine check.
** 3. Success Criteria
*** DONE Implement recursive AST walker in Lisp
*** DONE Establish strict function whitelist (surgical Org operations)
*** DONE Detect and block nested 'eval' attempts
*** DONE Verify that malformed or malicious sexps are rejected
* Implementation
** Package
#+begin_src lisp :tangle ./org-skill-lisp-validator.lisp :tangle ./org-skill-lisp-validator.lisp
#+end_src
** Whitelist Definition
#+begin_src lisp :tangle ./org-skill-lisp-validator.lisp :tangle ./org-skill-lisp-validator.lisp
(defparameter *lisp-validator-whitelist*
'(;; Math & Logic
+ - * / = < > <= >= 1+ 1- min max
and or not null eq eql equal string= string-equal
;; List Manipulation
list cons car cdr cadr cddr cdar caar append mapcar remove-if remove-if-not
length reverse sort nth nthcdr push pop
;; Plists and Hash Tables
getf gethash
;; Control Flow
let let* if cond when unless case typecase
;; Strings
format concatenate string-downcase string-upcase search
;; Kernel specifics
opencortex::harness-log
opencortex::snapshot-memory
opencortex::rollback-memory
opencortex::lookup-object
opencortex::list-objects-by-type
opencortex::ingest-ast
opencortex::find-headline-missing-id
opencortex::context-query-store
opencortex::context-get-active-projects
opencortex::context-get-recent-completed-tasks
opencortex::context-list-all-skills
opencortex::context-get-system-logs
opencortex::context-assemble-global-awareness
opencortex::org-object-id
opencortex::org-object-type
opencortex::org-object-attributes
opencortex::org-object-content
opencortex::org-object-parent-id
opencortex::org-object-children
opencortex::org-object-version
opencortex::org-object-last-sync
opencortex::org-object-hash
;; Essential macros
declare ignore
;; Let's also add simple data types
t nil quote function))
#+end_src
** Dynamic Symbol Registration
We allow other skills to register safe symbols for the validator.
#+begin_src lisp :tangle ./org-skill-lisp-validator.lisp
(defvar *lisp-validator-registry* nil
"List of dynamically registered safe symbols.")
(defun lisp-validator-register (symbols)
"Adds symbols to the global validator registry."
(setf *lisp-validator-registry* (append *lisp-validator-registry* (if (listp symbols) symbols (list symbols))))
(harness-log "LISP VALIDATOR: Registered ~a new safe symbols." (length (if (listp symbols) symbols (list symbols)))))
(defun lisp-validator-is-safe (symbol)
"Checks if a symbol is in the static whitelist or the dynamic registry."
(or (member symbol *lisp-validator-whitelist* :test #'string-equal)
(member symbol *lisp-validator-registry* :test #'string-equal)))
#+end_src
** Recursive AST Walker
#+begin_src lisp :tangle ./org-skill-lisp-validator.lisp
(defun lisp-validator-ast-walk (form)
"Recursively walks the Lisp AST. Returns T if safe, NIL if unsafe."
(cond
;; Self-evaluating objects (strings, numbers, keywords) are safe.
((or (stringp form) (numberp form) (keywordp form) (characterp form))
t)
;; Symbols used as variables (in non-function position)
((symbolp form)
(lisp-validator-is-safe form))
;; Lists represent function calls or special forms.
((listp form)
(let ((head (car form)))
(cond
((eq head 'quote) t)
((not (symbolp head)) nil)
((lisp-validator-is-safe head)
(every #'lisp-validator-ast-walk (cdr form)))
(t
(harness-log "LISP VALIDATOR: Blocked call to non-whitelisted function ~a" head)
nil))))
(t nil)))
#+end_src
** Cognitive Tools
#+begin_src lisp :tangle ./org-skill-lisp-validator.lisp
(opencortex:def-cognitive-tool :lisp-validator-status "Returns validator-related telemetry, including blocked actions and harness status."
nil
:body (lambda (args)
(declare (ignore args))
(format nil "LISP VALIDATOR STATUS:
- Static Whitelist: ~a symbols
- Dynamic Registry: ~a symbols
- Total Blocked Actions: ~a"
(length *lisp-validator-whitelist*)
(length *lisp-validator-registry*)
"Not implemented")))
#+end_src
** Skill Definition
#+begin_src lisp :tangle ./org-skill-lisp-validator.lisp
(opencortex:defskill :skill-lisp-validator
:priority 900 ; High priority, before most skills
:trigger (lambda (ctx)
;; Check if any proposed action is an :eval or :shell call
(let ((candidate (getf ctx :candidate)))
(when candidate
(let ((payload (getf candidate :payload)))
(member (getf payload :action) '(:eval :shell))))))
:probabilistic nil ; Purely deterministic/safety skill
:deterministic (lambda (action context)
(harness-log "DETERMINISTIC ENGINE [Lisp-Validator]: Intercepted critical action for structural validation.")
action))
#+end_src
* Phase E: Chaos (Verification)
#+begin_src lisp :tangle ./org-skill-lisp-validator.lisp
(defpackage :opencortex-lisp-validator-tests
(:use :cl :fiveam :opencortex)
(:export #:lisp-validator-suite))
(in-package :opencortex-lisp-validator-tests)
(def-suite lisp-validator-suite :description "Tests for the Lisp Validator.")
(in-suite lisp-validator-suite)
(test test-basic-math-safe
(is (opencortex:lisp-validator-validate "(+ 1 2)")))
(test test-blocked-eval
(is (not (opencortex:lisp-validator-validate "(eval '(+ 1 2))"))))
(test test-blocked-shell
(is (not (opencortex:lisp-validator-validate "(uiop:run-program \"ls\")"))))
(test test-nested-unsafe
(is (not (opencortex:lisp-validator-validate "(let ((x 1)) (delete-file \"test.txt\"))"))))
(test test-safe-kernel-api
(is (opencortex:lisp-validator-validate "(opencortex::lookup-object \"node-1\")")))
#+end_src

View File

@@ -57,7 +57,7 @@ Code without surrounding prose is a bug report waiting to happen.
** Block Balance Checker
#+begin_src lisp :tangle ../library/gen/org-skill-literate-programming.lisp
#+begin_src lisp :tangle ./org-skill-literate-programming.lisp
(in-package :opencortex)
(defun literate-check-block-balance (code-string)
@@ -94,7 +94,7 @@ Code without surrounding prose is a bug report waiting to happen.
** File-Level Balance Audit
#+begin_src lisp :tangle ../library/gen/org-skill-literate-programming.lisp
#+begin_src lisp :tangle ./org-skill-literate-programming.lisp
(defun literate-audit-org-file (filepath)
"Audits all tangled lisp blocks in an Org file for structural balance.
@@ -145,7 +145,7 @@ Code without surrounding prose is a bug report waiting to happen.
Verifies that tangled `.lisp` files are in sync with their Org source. Violation: edited .lisp directly instead of through Org.
#+begin_src lisp :tangle ../library/gen/org-skill-literate-programming.lisp
#+begin_src lisp :tangle ./org-skill-literate-programming.lisp
(defvar *tangle-targets*
'(("skills/org-skill-engineering-standards.org" . "library/gen/org-skill-engineering-standards.lisp")
("skills/org-skill-literate-programming.org" . "library/gen/org-skill-literate-programming.lisp")
@@ -184,7 +184,7 @@ This detects direct .lisp edits (which violate the LP workflow)."
The LP skill runs at priority 1100 (just below engineering-standards at 1000).
#+begin_src lisp :tangle ../library/gen/org-skill-literate-programming.lisp
#+begin_src lisp :tangle ./org-skill-literate-programming.lisp
(defskill :skill-literate-programming
:priority 1100
:trigger (lambda (ctx)
@@ -218,7 +218,7 @@ The LP skill runs at priority 1100 (just below engineering-standards at 1000).
** Initialize Project Root
#+begin_src lisp :tangle ../library/gen/org-skill-literate-programming.lisp
#+begin_src lisp :tangle ./org-skill-literate-programming.lisp
(defvar *lp-initialized* nil)
(defun lp-init ()
@@ -240,7 +240,7 @@ The LP skill runs at priority 1100 (just below engineering-standards at 1000).
These tests verify the LP enforcement logic. Run with:
~(fiveam:run! 'literate-programming-suite)~
#+begin_src lisp :tangle ../tests/literate-programming-tests.lisp
#+begin_src lisp :tangle ./tests/literate-programming-tests.lisp
(defpackage :opencortex-literate-programming-tests
(:use :cl :fiveam :opencortex)
(:export #:literate-programming-suite))

View File

@@ -21,12 +21,12 @@ This skill acts as a proxy between the OpenCortex kernel and the Lisp-agnostic `
* Phase D: Build (Implementation)
** Package Context
#+begin_src lisp :tangle ../library/gen/org-skill-llama-backend.lisp
#+begin_src lisp :tangle ./org-skill-llama-backend.lisp
(in-package :opencortex)
#+end_src
** The Inference Engine (llama-inference)
#+begin_src lisp :tangle ../library/gen/org-skill-llama-backend.lisp
#+begin_src lisp :tangle ./org-skill-llama-backend.lisp
(defun llama-inference (prompt system-prompt &key (model "local-model"))
"Sends a completion request to the local llama.cpp server."
(let ((endpoint (uiop:getenv "LLAMACPP_ENDPOINT")))
@@ -51,7 +51,7 @@ This skill acts as a proxy between the OpenCortex kernel and the Lisp-agnostic `
#+end_src
** Registration
#+begin_src lisp :tangle ../library/gen/org-skill-llama-backend.lisp
#+begin_src lisp :tangle ./org-skill-llama-backend.lisp
(progn
(register-probabilistic-backend :llama #'llama-inference)
(harness-log "LLAMA: Local backend registered and active."))

View File

@@ -19,7 +19,7 @@ The gateway utilizes a functional dispatch pattern. A single entry point, `execu
* Phase D: Build (Implementation)
** Implementation
#+begin_src lisp :tangle ../library/gen/org-skill-llm-gateway.lisp
#+begin_src lisp :tangle ./org-skill-llm-gateway.lisp
(defun get-nested (alist &rest keys)
"Recursively extracts nested values from an alist, handling both objects and arrays."

View File

@@ -37,7 +37,7 @@ Move context pruning and rendering logic out of `context.lisp` to allow for more
** 2. Semantic Interfaces
#+begin_src lisp :tangle ../library/gen/org-skill-peripheral-vision.lisp
#+begin_src lisp :tangle ./org-skill-peripheral-vision.lisp
(defun context-render-to-org (obj &key depth foveal-id semantic-threshold foveal-vector)
"Recursively renders an org-object with foveal-peripheral pruning.")
@@ -48,7 +48,7 @@ Move context pruning and rendering logic out of `context.lisp` to allow for more
* Phase D: Build (Implementation)
** Foveal-Peripheral Pruning
#+begin_src lisp :tangle ../library/gen/org-skill-peripheral-vision.lisp
#+begin_src lisp :tangle ./org-skill-peripheral-vision.lisp
(defun context-render-to-org (obj &key (depth 1) (foveal-id nil) (semantic-threshold 0.75) (foveal-vector nil))
"Recursively renders an org-object and its children to an Org string using a Foveal-Peripheral Hybrid model."
@@ -112,7 +112,7 @@ Move context pruning and rendering logic out of `context.lisp` to allow for more
#+end_src
* Registration
#+begin_src lisp :tangle ../library/gen/org-skill-peripheral-vision.lisp
#+begin_src lisp :tangle ./org-skill-peripheral-vision.lisp
(defskill :skill-peripheral-vision
:priority 90
:dependencies ("org-skill-embedding")

View File

@@ -44,7 +44,7 @@ Therefore, Policy encodes not just rules, but *values*:
Every skill executes within its own jailed package namespace, inheriting core harness symbols while maintaining isolation from other skills.
#+begin_src lisp :tangle ../library/gen/org-skill-policy.lisp
#+begin_src lisp :tangle ./org-skill-policy.lisp
(in-package :opencortex)
#+end_src
@@ -61,7 +61,7 @@ When two invariants conflict, resolution follows a strict priority order. This p
| 200 | Mentorship | Teaching increases capability; doing removes it |
| 100 | Sustainability | Offline capability today enables 100-year survival |
#+begin_src lisp :tangle ../library/gen/org-skill-policy.lisp
#+begin_src lisp :tangle ./org-skill-policy.lisp
(defvar *policy-invariant-priorities*
'((:transparency . 500)
(:autonomy . 400)
@@ -91,7 +91,7 @@ At the gate:
- Every user-facing action must carry an `:explanation`
- Log messages must include the triggering invariant
#+begin_src lisp :tangle ../library/gen/org-skill-policy.lisp
#+begin_src lisp :tangle ./org-skill-policy.lisp
(defun policy-check-transparency (action context)
"Ensures the action is inspectable and user-facing actions carry an explanation.
@@ -137,7 +137,7 @@ At the gate:
Every action should increase the user's independence from centralized, proprietary platforms. When the system uses a proprietary API, it's logged as "autonomy debt"—acceptable tactically, but flagged for eventual replacement.
#+begin_src lisp :tangle ../library/gen/org-skill-policy.lisp
#+begin_src lisp :tangle ./org-skill-policy.lisp
(defvar *proprietary-domain-watchlist*
'("googleapis.com" "api.openai.com" "anthropic.com" "api.groq.com" "openrouter.ai")
"Domains representing centralized, proprietary control.
@@ -204,7 +204,7 @@ Every action should increase the user's independence from centralized, proprieta
The system harness must remain minimalist. "Just-in-case" code is a security vulnerability. Complexity must be earned through demonstrated need, not anticipation of future use.
#+begin_src lisp :tangle ../library/gen/org-skill-policy.lisp
#+begin_src lisp :tangle ./org-skill-policy.lisp
(defvar *policy-max-skill-size-chars* 50000
"Maximum recommended size for a skill file tangled from an Org note.
@@ -255,7 +255,7 @@ This is the most important invariant for system stability. If the harness grows
- Harder to debug when things go wrong
- Harder to maintain across versions
#+begin_src lisp :tangle ../library/gen/org-skill-policy.lisp
#+begin_src lisp :tangle ./org-skill-policy.lisp
(defvar *modularity-protected-paths*
'("harness/" "opencortex.asd")
"Paths that constitute the unbreakable core of the system.
@@ -322,7 +322,7 @@ This is the most important invariant for system stability. If the harness grows
The agent's goal is not to "do it for the user," but to "empower the user." Every autonomous action must be explained at a level that increases the user's technical understanding.
#+begin_src lisp :tangle ../library/gen/org-skill-policy.lisp
#+begin_src lisp :tangle ./org-skill-policy.lisp
(defvar *mentorship-required-actions*
'(:create-skill :eval :modify-file :write-file :replace
:rename-file :delete-file :shell :create-note)
@@ -379,7 +379,7 @@ The Memex should be functional even when:
This means preferring local, energy-efficient architectures over cloud-dependent ones.
#+begin_src lisp :tangle ../library/gen/org-skill-policy.lisp
#+begin_src lisp :tangle ./org-skill-policy.lisp
(defvar *cloud-only-backends* '(:openrouter :openai :anthropic :groq :gemini-api)
"Backends requiring internet connection and external infrastructure.
@@ -416,7 +416,7 @@ This means preferring local, energy-efficient architectures over cloud-dependent
When the policy gate blocks or modifies an action, it must tell the user *why*. This creates an auditable log of every policy decision.
#+begin_src lisp :tangle ../library/gen/org-skill-policy.lisp
#+begin_src lisp :tangle ./org-skill-policy.lisp
(defun policy-explain (invariant-key message &optional original-action)
"Formats a policy decision into an auditable explanation plist.
@@ -445,7 +445,7 @@ When the policy gate blocks or modifies an action, it must tell the user *why*.
** Running Invariant Checks
#+begin_src lisp :tangle ../library/gen/org-skill-policy.lisp
#+begin_src lisp :tangle ./org-skill-policy.lisp
(defun policy-run-invariant-checks (action context)
"Runs all invariant checks in priority order.
@@ -492,7 +492,7 @@ When the policy gate blocks or modifies an action, it must tell the user *why*.
** Finding Engineering Standards
#+begin_src lisp :tangle ../library/gen/org-skill-policy.lisp
#+begin_src lisp :tangle ./org-skill-policy.lisp
(defun policy-find-engineering-standards-gate ()
"Searches for the Engineering Standards gate across known jailed package names.
@@ -515,7 +515,7 @@ When the policy gate blocks or modifies an action, it must tell the user *why*.
** Main Policy Gate
#+begin_src lisp :tangle ../library/gen/org-skill-policy.lisp
#+begin_src lisp :tangle ./org-skill-policy.lisp
(defun policy-deterministic-gate (action context)
"The main policy gate entry point.
@@ -547,7 +547,7 @@ When the policy gate blocks or modifies an action, it must tell the user *why*.
* Skill Registration
#+begin_src lisp :tangle ../library/gen/org-skill-policy.lisp
#+begin_src lisp :tangle ./org-skill-policy.lisp
(defskill :skill-policy
:priority 500
:trigger (lambda (ctx) (declare (ignore ctx)) t)

View File

@@ -45,7 +45,7 @@ Decouple protocol parsing (framing/unframing) from semantic validation.
* Phase D: Build (Implementation)
** Schema Enforcement
#+begin_src lisp :tangle ../library/gen/org-skill-protocol-validator.lisp
#+begin_src lisp :tangle ./org-skill-protocol-validator.lisp
(in-package :opencortex)
(defun validate-communication-protocol-schema (msg)
@@ -84,7 +84,7 @@ Decouple protocol parsing (framing/unframing) from semantic validation.
#+end_src
* Registration
#+begin_src lisp :tangle ../library/gen/org-skill-protocol-validator.lisp
#+begin_src lisp :tangle ./org-skill-protocol-validator.lisp
(defskill :skill-communication-protocol-validator
:priority 95
:trigger (lambda (ctx) (member (getf (getf ctx :payload) :sensor) '(:protocol-received)))

View File

@@ -41,14 +41,14 @@ The Scribe reacts to the `:heartbeat` sensor. It maintains a state file (`scribe
* Phase D: Build (Implementation)
** Package Context
#+begin_src lisp :tangle ../library/gen/org-skill-scribe.lisp
#+begin_src lisp :tangle ./org-skill-scribe.lisp
(in-package :opencortex)
#+end_src
** State: Checkpoint Management
We track the last processed universal time to avoid redundant distillation.
#+begin_src lisp :tangle ../library/gen/org-skill-scribe.lisp
#+begin_src lisp :tangle ./org-skill-scribe.lisp
(defvar *scribe-last-checkpoint* 0
"The universal-time of the last successful distillation run.")
@@ -70,7 +70,7 @@ We track the last processed universal time to avoid redundant distillation.
** Filtering: Privacy & Relevance
The Scribe only cares about non-personal, non-distilled headlines.
#+begin_src lisp :tangle ../library/gen/org-skill-scribe.lisp
#+begin_src lisp :tangle ./org-skill-scribe.lisp
(defun scribe-get-distillable-nodes ()
"Returns a list of org-objects from the daily/ folder that require distillation."
(let ((results nil))
@@ -91,7 +91,7 @@ The Scribe only cares about non-personal, non-distilled headlines.
** Probabilistic: Extraction Prompt
The LLM is tasked with identifying atomic concepts within the raw text.
#+begin_src lisp :tangle ../library/gen/org-skill-scribe.lisp
#+begin_src lisp :tangle ./org-skill-scribe.lisp
(defun probabilistic-skill-scribe (context)
"Generates the extraction prompt for the Scribe."
(let* ((payload (getf context :payload))
@@ -122,7 +122,7 @@ TEXT:
** Deterministic: Note Committal
The deterministic gate receives the list of proposed notes and writes them to the filesystem.
#+begin_src lisp :tangle ../library/gen/org-skill-scribe.lisp
#+begin_src lisp :tangle ./org-skill-scribe.lisp
(defun scribe-commit-notes (proposals)
"Writes proposed atomic notes to the notes/ directory. Appends if the note exists."
(let ((notes-dir (uiop:merge-pathnames* "notes/" (asdf:system-source-directory :opencortex))))
@@ -159,7 +159,7 @@ The deterministic gate receives the list of proposed notes and writes them to th
#+end_src
** Skill Registration
#+begin_src lisp :tangle ../library/gen/org-skill-scribe.lisp
#+begin_src lisp :tangle ./org-skill-scribe.lisp
(defskill :skill-scribe
:priority 50
:trigger (lambda (ctx)
@@ -174,6 +174,6 @@ The deterministic gate receives the list of proposed notes and writes them to th
#+end_src
** Initialization
#+begin_src lisp :tangle ../library/gen/org-skill-scribe.lisp
#+begin_src lisp :tangle ./org-skill-scribe.lisp
(scribe-load-state)
#+end_src

View File

@@ -14,14 +14,14 @@ The *Self-Edit Agent* enables the agent to modify its own code and files with sa
* Phase D: Build (Implementation)
** Package Context
#+begin_src lisp :tangle ../library/gen/org-skill-self-edit.lisp
#+begin_src lisp :tangle ./org-skill-self-edit.lisp
(in-package :opencortex)
#+end_src
** Deterministic Paren Repair
Fast paren balancing for syntax errors.
#+begin_src lisp :tangle ../library/gen/org-skill-self-edit.lisp
#+begin_src lisp :tangle ./org-skill-self-edit.lisp
(defun self-edit-count-char (char string)
"Counts occurrences of CHAR in STRING."
(loop for c across string count (char= c char)))
@@ -41,7 +41,7 @@ Fast paren balancing for syntax errors.
** Parse Target Location
Extract file and line info from error context.
#+begin_src lisp :tangle ../library/gen/org-skill-self-edit.lisp
#+begin_src lisp :tangle ./org-skill-self-edit.lisp
(defun self-edit-parse-location (context)
"Extracts file and line from error context payload."
(let* ((payload (getf context :payload))
@@ -58,7 +58,7 @@ Extract file and line info from error context.
** Apply Surgical Edit
Apply a find/replace to a file with rollback on failure.
#+begin_src lisp :tangle ../library/gen/org-skill-self-edit.lisp
#+begin_src lisp :tangle ./org-skill-self-edit.lisp
(defun self-edit-apply (target-file old-code new-code)
"Applies surgical edit to TARGET-FILE: replace OLD-CODE with NEW-CODE.
Returns list with :status and :message keys."
@@ -90,7 +90,7 @@ Returns list with :status and :message keys."
#+end_src
** Cognitive Tool: Edit File
#+begin_src lisp :tangle ../library/gen/org-skill-self-edit.lisp
#+begin_src lisp :tangle ./org-skill-self-edit.lisp
(def-cognitive-tool :self-edit
"Applies a surgical code modification to a file with automatic rollback on failure."
((:file :type :string :description "Path to the target file")
@@ -106,7 +106,7 @@ Returns list with :status and :message keys."
** Skill Definition
Hooks into syntax-error events for self-repair.
#+begin_src lisp :tangle ../library/gen/org-skill-self-edit.lisp
#+begin_src lisp :tangle ./org-skill-self-edit.lisp
(defskill :skill-self-edit
:priority 95
:trigger (lambda (ctx)
@@ -146,7 +146,7 @@ Provide a fixed version of the code as a lisp form.")
#+end_src
** Tool: Quick Paren Fix
#+begin_src lisp :tangle ../library/gen/org-skill-self-edit.lisp
#+begin_src lisp :tangle ./org-skill-self-edit.lisp
(def-cognitive-tool :balance-parens
"Balances parentheses in a code string."
((:code :type :string :description "The code to balance"))
@@ -164,7 +164,7 @@ Provide a fixed version of the code as a lisp form.")
** Skill Hot-Reload
Swap compiled skill files without breaking active sockets.
#+begin_src lisp :tangle ../library/gen/org-skill-self-edit.lisp
#+begin_src lisp :tangle ./org-skill-self-edit.lisp
(defvar *self-edit-skills-backup* nil
"Backup of skill registry before hot-reload.")
@@ -217,7 +217,7 @@ Swap compiled skill files without breaking active sockets.
** Cognitive Tool: Reload Skill
#+begin_src lisp :tangle ../library/gen/org-skill-self-edit.lisp
#+begin_src lisp :tangle ./org-skill-self-edit.lisp
(def-cognitive-tool :reload-skill
"Hot-reloads a skill from its compiled source file without restarting the system."
((:skill-name :type :string :description "Name of the skill to reload (e.g. :skill-engineering-standards)")
@@ -231,7 +231,7 @@ Swap compiled skill files without breaking active sockets.
* Phase E: Verification
#+begin_src lisp :tangle ../tests/self-edit-tests.lisp
#+begin_src lisp :tangle ./tests/self-edit-tests.lisp
(defpackage :opencortex-self-edit-tests
(:use :cl :fiveam :opencortex)
(:export #:self-edit-suite))

View File

@@ -15,11 +15,11 @@ This skill enables self-editing by applying surgical fixes to files (including s
* Phase D: Build (Implementation)
** Repair Logic
#+begin_src lisp :tangle ../library/gen/org-skill-self-fix.lisp
#+begin_src lisp :tangle ./org-skill-self-fix.lisp
(in-package :opencortex)
#+end_src
#+begin_src lisp :tangle ../library/gen/org-skill-self-fix.lisp
#+begin_src lisp :tangle ./org-skill-self-fix.lisp
(defun self-fix-apply (action context)
"Applies a surgical code fix and reloads the modified skill."
(declare (ignore context))
@@ -66,7 +66,7 @@ This skill enables self-editing by applying surgical fixes to files (including s
#+end_src
** Cognitive Tool
#+begin_src lisp :tangle ../library/gen/org-skill-self-fix.lisp
#+begin_src lisp :tangle ./org-skill-self-fix.lisp
(def-cognitive-tool :repair-file
"Applies a surgical code modification to a file and reloads the skill if applicable."
((:file :type :string :description "Path to the target file")
@@ -79,7 +79,7 @@ This skill enables self-editing by applying surgical fixes to files (including s
#+end_src
** Skill Definition
#+begin_src lisp :tangle ../library/gen/org-skill-self-fix.lisp
#+begin_src lisp :tangle ./org-skill-self-fix.lisp
(defskill :skill-self-fix
:priority 95
:trigger (lambda (context) (eq (getf (getf context :payload) :sensor) :repair-request))

View File

@@ -11,7 +11,7 @@ The *Shell Actuator* provides a controlled interface for the OpenCortex to execu
* Implementation
#+begin_src lisp :tangle ../library/gen/org-skill-shell-actuator.lisp
#+begin_src lisp :tangle ./org-skill-shell-actuator.lisp
(defparameter *allowed-commands* '("ls" "git" "rg" "grep" "date" "echo" "cat" "node" "python3" "sbcl"))

View File

@@ -27,7 +27,7 @@ Also provides vector embeddings via Ollama or llama.cpp.
* Implementation
Tool permissions and embedding generation via multiple providers.
#+begin_src lisp :tangle ../library/gen/org-skill-tool-permissions.lisp
#+begin_src lisp :tangle ./org-skill-tool-permissions.lisp
(in-package :opencortex)
(defvar *tool-permissions* (make-hash-table :test 'equal)
@@ -122,7 +122,7 @@ Tool permissions and embedding generation via multiple providers.
These tests verify tool permissions. Run with:
~(fiveam:run! 'tool-permissions-suite)~
#+begin_src lisp :tangle ../tests/tool-permissions-tests.lisp
#+begin_src lisp :tangle ./tests/tool-permissions-tests.lisp
(defpackage :opencortex-tool-permissions-tests
(:use :cl :fiveam :opencortex)
(:export #:tool-permissions-suite))

View File

@@ -1,43 +0,0 @@
(defpackage :opencortex-act-tests
(:use :cl :fiveam :opencortex))
(in-package :opencortex-act-tests)
(def-suite act-suite
:description "Verification of the Act Gate and Symbolic Guard.")
(in-suite act-suite)
(test test-act-gate-symbolic-guard-bypass
"Verify that opencortex: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"))))
(result (opencortex:act-gate signal)))
(is (eq :acted (getf signal :status)))
(is (null result))))
(test test-act-gate-symbolic-guard-interception
"Verify that opencortex:act-gate intercepts actions when a skill returns a LOG/EVENT."
(clrhash opencortex::*skills-registry*)
;; Register a mock skill that acts like a symbolic guard
(opencortex::defskill :mock-bouncer
:priority 200
:trigger (lambda (ctx) t)
:deterministic (lambda (action ctx)
(declare (ignore action ctx))
'(:type :LOG :payload (: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)))
(is (eq :LOG (getf result :type)))
(is (search "BLOCKED BY SYMBOLIC GUARD" (getf (getf result :payload) :text)))
;; The approved action in signal should be NIL'd out
(is (null (getf signal :approved-action)))))
(test test-act-gate-symbolic-guard-pass-through
"Verify that opencortex:act-gate allows actions when skills permit them."
(clrhash opencortex::*skills-registry*)
(let* ((signal (list :type :EVENT :status nil :depth 0 :approved-action '(:target :cli :payload (:text "Allowed"))))
(result (opencortex:act-gate signal)))
(is (eq :acted (getf signal :status)))
(is (equal '(:target :cli :payload (:text "Allowed")) (getf signal :approved-action)))))

View File

@@ -44,4 +44,4 @@
(progn
(opencortex::load-skill-from-org tmp-skill)
(is (not (null (gethash "org-skill-jail-test" opencortex::*skills-registry*)))))
(uiop:delete-file-if-exists tmp-skill)))))
(uiop:delete-file-if-exists tmp-skill))))

View File

@@ -24,10 +24,10 @@
:priority 200
:trigger (lambda (ctx) t)
:deterministic (lambda (action ctx)
(list :type :LOG :payload '(:text "BLOCKED BY SYMBOLIC GUARD"))))
(list :type :LOG :payload (: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)))
(is (eq :LOG (getf result :type)))
(is (search "BLOCKED BY SYMBOLIC GUARD" (getf (getf result :payload) :text)))))
(is (search "BLOCKED BY SYMBOLIC GUARD" (getf (getf result :payload) :text))))))