From 2e8e79a1939ba738573ed04617d823f336ae6372 Mon Sep 17 00:00:00 2001 From: Amr Gharbeia Date: Mon, 27 Apr 2026 18:54:18 -0400 Subject: [PATCH] fix(v0.2.0): finalize structural integrity and clean boot - Fixed memory.org source blocks to ensure persistence functions are tangled. - Improved extract-tangle-target to handle complex Elisp expressions. - Corrected opencortex.sh initialization paths to prevent setup loops. - Reordered variable definitions in policy and standards skills to eliminate forward-reference warnings. --- harness/act.org | 17 +- harness/communication.org | 7 +- harness/context.org | 21 +- harness/loop.org | 13 +- harness/manifest.org | 7 +- harness/memory.org | 220 +++++---------------- harness/package.org | 19 +- harness/perceive.org | 11 +- harness/reason.org | 19 +- harness/skills.org | 55 +++--- harness/tui-client.org | 3 +- opencortex.sh | 8 +- skills/org-skill-bouncer.org | 17 +- skills/org-skill-cli-gateway.org | 3 +- skills/org-skill-credentials-vault.org | 17 +- skills/org-skill-emacs-edit.org | 21 +- skills/org-skill-engineering-standards.org | 141 +++++++------ skills/org-skill-gardener.org | 13 +- skills/org-skill-homoiconic-memory.org | 3 +- skills/org-skill-lisp-utils.org | 3 +- skills/org-skill-literate-programming.org | 11 +- skills/org-skill-llama-backend.org | 7 +- skills/org-skill-llm-gateway.org | 3 +- skills/org-skill-peripheral-vision.org | 9 +- skills/org-skill-policy.org | 147 ++++++++------ skills/org-skill-protocol-validator.org | 5 +- skills/org-skill-scribe.org | 15 +- skills/org-skill-self-edit.org | 19 +- skills/org-skill-self-fix.org | 9 +- skills/org-skill-shell-actuator.org | 3 +- skills/org-skill-tool-permissions.org | 3 +- 31 files changed, 390 insertions(+), 459 deletions(-) diff --git a/harness/act.org b/harness/act.org index 5f33be5..b58909f 100644 --- a/harness/act.org +++ b/harness/act.org @@ -1,3 +1,4 @@ +#+PROPERTY: header-args:lisp :tangle (expand-file-name "act.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) #+TITLE: Stage 3: Act (act.lisp) #+AUTHOR: Amr #+FILETAGS: :harness:act: @@ -35,7 +36,7 @@ Example feedback chain: * Package Context -#+begin_src lisp :tangle (expand-file-name "act.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (in-package :opencortex) #+end_src @@ -43,7 +44,7 @@ Example feedback chain: ** Actuator Registry Variables -#+begin_src lisp :tangle (expand-file-name "act.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defvar *default-actuator* :cli "The actuator used when no explicit target is specified. Override with DEFAULT_ACTUATOR environment variable.") @@ -55,7 +56,7 @@ Example feedback chain: ** initialize-actuators: System Bootstrap -#+begin_src lisp :tangle (expand-file-name "act.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defun initialize-actuators () "Load actuator configuration from environment and register core actuators. @@ -102,7 +103,7 @@ Example feedback chain: ** dispatch-action: The Router -#+begin_src lisp :tangle (expand-file-name "act.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defun dispatch-action (action context) "Route an approved action to its registered actuator. @@ -149,7 +150,7 @@ Example feedback chain: ** execute-system-action: Internal Commands -#+begin_src lisp :tangle (expand-file-name "act.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defun execute-system-action (action context) "Execute internal harness commands. @@ -198,7 +199,7 @@ Example feedback chain: ** execute-tool-action: Cognitive Tool Execution -#+begin_src lisp :tangle (expand-file-name "act.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defun execute-tool-action (action context) "Execute a registered cognitive tool. @@ -267,7 +268,7 @@ Example feedback chain: ** format-tool-result: Human-Readable Output -#+begin_src lisp :tangle (expand-file-name "act.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defun format-tool-result (tool-name result) "Format a tool result for human-readable display. @@ -295,7 +296,7 @@ Example feedback chain: ** act-gate: Final Pipeline Stage -#+begin_src lisp :tangle (expand-file-name "act.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defun act-gate (signal) "Final stage of the metabolic pipeline: Actuation. diff --git a/harness/communication.org b/harness/communication.org index 7e1560a..21d0d0b 100644 --- a/harness/communication.org +++ b/harness/communication.org @@ -1,3 +1,4 @@ +#+PROPERTY: header-args:lisp :tangle (expand-file-name "communication.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) #+TITLE: Communication Protocol (communication.lisp) #+AUTHOR: Amr #+FILETAGS: :harness:protocol: @@ -10,7 +11,7 @@ The ~communication.lisp~ module defines the low-level transport and framing logi * Implementation (communication.lisp) -#+begin_src lisp :tangle (expand-file-name "communication.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (in-package :opencortex) (defun proto-get (plist key) @@ -21,7 +22,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 (expand-file-name "communication.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (in-package :opencortex) (defvar *actuator-registry* (make-hash-table :test 'equalp) @@ -128,7 +129,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 (expand-file-name "communication.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defun sanitize-protocol-message (msg) "Recursively strips non-serializable objects from a protocol plist." (if (and msg (listp msg)) diff --git a/harness/context.org b/harness/context.org index 5f80a09..54fc293 100644 --- a/harness/context.org +++ b/harness/context.org @@ -1,3 +1,4 @@ +#+PROPERTY: header-args:lisp :tangle (expand-file-name "context.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) #+TITLE: Peripheral Vision (context.lisp) #+AUTHOR: Amr #+FILETAGS: :harness:context: @@ -37,14 +38,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 (expand-file-name "context.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src 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 (expand-file-name "context.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src 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 +63,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 (expand-file-name "context.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defun context-get-active-projects () "Returns headlines tagged as 'project' that are not yet marked DONE." (remove-if (lambda (obj) (equal (getf (org-object-attributes obj) :TODO-STATE) "DONE")) @@ -72,7 +73,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 (expand-file-name "context.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defun context-get-recent-completed-tasks () "Retrieves recently finished tasks from the store." (context-query-store :todo-state "DONE" :type :HEADLINE)) @@ -81,7 +82,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 (expand-file-name "context.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defun context-list-all-skills () "Provides a sorted overview of currently loaded system capabilities." (let ((results nil)) @@ -95,7 +96,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 (expand-file-name "context.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src 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 +109,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 (expand-file-name "context.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defun context-get-system-logs (&optional limit) "Retrieves the most recent lines from the harness's internal log." (let ((log-limit (or limit (ignore-errors (parse-integer (uiop:getenv "CONTEXT_LOG_LIMIT"))) 20))) @@ -128,7 +129,7 @@ It implements the following deterministic logic: The semantic threshold is externalized to `CONTEXT_SEMANTIC_THRESHOLD`. -#+begin_src lisp :tangle (expand-file-name "context.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src 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 +178,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 (expand-file-name "context.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src 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 +197,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 (expand-file-name "context.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src 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) diff --git a/harness/loop.org b/harness/loop.org index 5d22b7c..a7e64d9 100644 --- a/harness/loop.org +++ b/harness/loop.org @@ -1,3 +1,4 @@ +#+PROPERTY: header-args:lisp :tangle (expand-file-name "loop.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) #+TITLE: The Metabolic Loop (loop.lisp) #+AUTHOR: Amr #+FILETAGS: :harness:loop: @@ -67,7 +68,7 @@ The loop operates in a multi-threaded environment: * Package and Thread-Safe Variables -#+begin_src lisp :tangle (expand-file-name "loop.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (in-package :opencortex) (defvar *interrupt-flag* nil @@ -90,7 +91,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 (expand-file-name "loop.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defun process-signal (signal) "The entry point to the Metabolic Pipeline: Perceive -> Reason -> Act. @@ -184,7 +185,7 @@ The heartbeat thread ensures the agent remains alive even without external input ** Heartbeat Configuration Variables -#+begin_src lisp :tangle (expand-file-name "loop.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defvar *auto-save-interval* 300 "Interval in seconds between automatic memory saves. Defaults to 300 seconds (5 minutes). Set via MEMORY_AUTO_SAVE_INTERVAL env var.") @@ -195,7 +196,7 @@ The heartbeat thread ensures the agent remains alive even without external input ** start-heartbeat: The Pulsing Heart -#+begin_src lisp :tangle (expand-file-name "loop.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defun start-heartbeat () "Starts the background heartbeat thread. @@ -241,7 +242,7 @@ The heartbeat thread ensures the agent remains alive even without external input ** Shutdown Configuration -#+begin_src lisp :tangle (expand-file-name "loop.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defvar *shutdown-save-enabled* t "When T, save memory to disk on graceful shutdown. Disable for testing or when memory persistence is handled externally.") @@ -258,7 +259,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 (expand-file-name "loop.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defun main () "Entry point for OpenCortex. Initializes the system and enters idle loop. diff --git a/harness/manifest.org b/harness/manifest.org index 8f0b3d4..f77305e 100644 --- a/harness/manifest.org +++ b/harness/manifest.org @@ -1,3 +1,4 @@ +#+PROPERTY: header-args:lisp :tangle (expand-file-name "opencortex.asd" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) #+TITLE: Manifest (opencortex.asd) #+AUTHOR: Amr #+FILETAGS: :harness:system: @@ -79,7 +80,7 @@ The testing system (~:opencortex/tests~) is separate from the production system ** Main Harness System -#+begin_src lisp :tangle (expand-file-name "opencortex.asd" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defsystem :opencortex :name "opencortex" :author "Amr" @@ -119,7 +120,7 @@ The testing system (~:opencortex/tests~) is separate from the production system ** Test System -#+begin_src lisp :tangle (expand-file-name "opencortex.asd" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defsystem :opencortex/tests :depends-on (:opencortex ; The harness we're testing :fiveam) ; Testing framework @@ -154,7 +155,7 @@ The testing system (~:opencortex/tests~) is separate from the production system ** TUI Client System -#+begin_src lisp :tangle (expand-file-name "opencortex.asd" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defsystem :opencortex/tui :depends-on (:opencortex ; The daemon we're connecting to :croatoan ; Terminal UI library diff --git a/harness/memory.org b/harness/memory.org index e0a4a28..40c40d9 100644 --- a/harness/memory.org +++ b/harness/memory.org @@ -148,63 +148,8 @@ Restores the state of the Memex from one of the previous snapshots. (harness-log "MEMORY ERROR - Snapshot ~a not found." index)))) #+end_src -* Test Suite - -These tests verify the Memory system. Run with: -~(fiveam:run! 'memory-suite)~ - -#+begin_src lisp :tangle (expand-file-name "memory-tests.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/tests")) -(defpackage :opencortex-memory-tests - (:use :cl :fiveam :opencortex) - (:export #:memory-suite)) - -(in-package :opencortex-memory-tests) - -(def-suite memory-suite - :description "Tests for the Merkle-Tree Memory") - -(in-suite memory-suite) - -(test merkle-hash-consistency - "Verify identical ASTs produce identical Merkle hashes." - (let* ((ast1 '(:type :HEADLINE :properties (:ID "test-1" :TITLE "Node 1") :contents nil))) - (clrhash *memory*) - (let ((id1 (ingest-ast ast1))) - (let ((hash1 (org-object-hash (lookup-object id1)))) - (clrhash *memory*) - (let ((id2 (ingest-ast ast1))) - (let ((hash2 (org-object-hash (lookup-object id2)))) - (is (equal hash1 hash2)))))))) - -(test history-store-immutability - "Verify that *history-store* retains old versions." - (clrhash *memory*) - (clrhash *history-store*) - (let* ((ast-v1 '(:type :HEADLINE :properties (:ID "test-node" :TITLE "Version 1") :contents nil)) - (id-v1 (ingest-ast ast-v1)) - (obj-v1 (lookup-object id-v1)) - (hash-v1 (org-object-hash obj-v1))) - (let* ((ast-v2 '(:type :HEADLINE :properties (:ID "test-node" :TITLE "Version 2") :contents nil)) - (id-v2 (ingest-ast ast-v2)) - (hash-v2 (org-object-hash (lookup-object id-v2)))) - (is (equal (org-object-hash (lookup-object "test-node")) hash-v2)) - (is (not (null (gethash hash-v1 *history-store*))) - (is (not (null (gethash hash-v2 *history-store*)))))) - -(test cow-snapshot-and-rollback - "Verify that lightweight snapshots restore previous pointer states." - (clrhash *memory*) - (setf *object-store-snapshots* nil) - (let* ((ast-v1 '(:type :HEADLINE :properties (:ID "cow-node" :TITLE "State A") :contents nil)) - (id-v1 (ingest-ast ast-v1)) - (hash-v1 (org-object-hash (lookup-object id-v1)))) - (snapshot-memory) - (let* ((ast-v2 '(:type :HEADLINE :properties (:ID "cow-node" :TITLE "State B") :contents nil)) - (id-v2 (ingest-ast ast-v2)) - (hash-v2 (org-object-hash (lookup-object id-v2)))) - ** 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. +Essential for surviving crashes. Saves the in-memory hash tables to disk and loads them back on restart. #+begin_src lisp :tangle (expand-file-name "memory.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) (defvar *memory-snapshot-path* nil @@ -259,8 +204,6 @@ Reconstitutes alists into hash tables." ** Semantic Search (get-embedding, semantic-search) Support for vector embeddings via Ollama and semantic search with cosine similarity. -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 (expand-file-name "memory.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) (defvar *embedding-cache* (make-hash-table :test 'equal) "Cache for embeddings to avoid redundant API calls.") @@ -307,7 +250,10 @@ Returns up to LIMIT objects with similarity >= MIN-SIMILARITY, sorted by similar *memory*) (setf results (sort results #'> :key (lambda (r) (getf r :similarity)))) (subseq results 0 (min limit (length results))))) +#+end_src +** Cognitive Tool: Semantic Search +#+begin_src lisp :tangle (expand-file-name "memory.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) (def-cognitive-tool :semantic-search "Searches memory for objects semantically similar to a query." ((:query :type :string :description "The search query.") @@ -317,29 +263,27 @@ 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)))) +#+end_src ** Cognitive Tool: Generate Embeddings -Provided for the Probabilistic Engine to invoke embedding generation on demand. - #+begin_src lisp :tangle (expand-file-name "memory.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) (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)))))) + (if (not (and texts (listp texts))) + (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 @@ -359,6 +303,7 @@ Basic functions for retrieving objects by ID or type. (let ((results nil)) (maphash (lambda (id obj) (declare (ignore id)) (when (eq (org-object-type obj) type) (push obj results))) *memory*) results)) + (defun list-objects-with-attribute (attr-name value) "Returns a list of all objects where ATTR-NAME matches VALUE." (let ((results nil)) @@ -387,8 +332,7 @@ Utility functions for AST traversal and path resolution. (let ((pos (position #\/ path :from-end t))) (if pos (subseq path (1+ pos)) path))) #+end_src -* 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. +* Test Suite #+begin_src lisp :tangle (expand-file-name "memory-tests.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/tests")) (defpackage :opencortex-memory-tests @@ -398,93 +342,12 @@ Following the Engineering Standards, the Memory must be empirically verified thr (in-package :opencortex-memory-tests) (def-suite memory-suite - :description "Tests for the Merkle-Tree Memory.") + :description "Tests for the Merkle-Tree Memory") (in-suite memory-suite) (test merkle-hash-consistency - (let* ((ast1 '(:type :HEADLINE :properties (:ID "test-1" :TITLE "Node 1") :contents nil)) - (ast2 '(:type :HEADLINE :properties (:ID "test-1" :TITLE "Node 1") :contents nil))) - (clrhash *memory*) - (let ((id1 (ingest-ast ast1))) - (let ((hash1 (org-object-hash (lookup-object id1)))) - (clrhash *memory*) - (let ((id2 (ingest-ast ast2))) - (let ((hash2 (org-object-hash (lookup-object id2)))) - (is (equal hash1 hash2)))))))) - -(test merkle-hash-cascading - (let* ((ast-leaf '(:type :HEADLINE :properties (:ID "leaf" :TITLE "Leaf") :contents nil)) - (ast-root-full '(:type :HEADLINE :properties (:ID "root" :TITLE "Root") - :contents ((:type :HEADLINE :properties (:ID "leaf" :TITLE "Leaf") :contents nil)))) - (id-root (progn (clrhash *memory*) (ingest-ast ast-root-full))) - (initial-root-hash (org-object-hash (lookup-object id-root)))) - - ;; Now ingest a modified version (title change) - (let* ((ast-root-modified '(:type :HEADLINE :properties (:ID "root" :TITLE "Root") - :contents ((:type :HEADLINE :properties (:ID "leaf" :TITLE "Leaf Modified") :contents nil)))) - (id-root-mod (progn (clrhash *memory*) (ingest-ast ast-root-modified))) - (modified-root-hash (org-object-hash (lookup-object id-root-mod)))) - (is (not (equal initial-root-hash modified-root-hash)))))) - -(test history-store-immutability - "Verify that *history-store* retains old versions even after *memory* updates." - (clrhash *memory*) - (clrhash *history-store*) - (let* ((ast-v1 '(:type :HEADLINE :properties (:ID "test-node" :TITLE "Version 1") :contents nil)) - (id-v1 (ingest-ast ast-v1)) - (obj-v1 (lookup-object id-v1)) - (hash-v1 (org-object-hash obj-v1))) - - (let* ((ast-v2 '(:type :HEADLINE :properties (:ID "test-node" :TITLE "Version 2") :contents nil)) - (id-v2 (ingest-ast ast-v2)) - (obj-v2 (lookup-object id-v2)) - (hash-v2 (org-object-hash obj-v2))) - - ;; The active pointer should be v2 - (is (equal (org-object-hash (lookup-object "test-node")) hash-v2)) - - ;; Both v1 and v2 should exist in the immutable history store - (is (not (null (gethash hash-v1 *history-store*)))) - (is (not (null (gethash hash-v2 *history-store*)))) - - ;; Modifying v2 should not affect v1 in the history store - (is (equal (org-object-content (gethash hash-v1 *history-store*)) "Version 1 -")) - (is (equal (org-object-content (gethash hash-v2 *history-store*)) "Version 2 -"))))) - -(test cow-snapshot-and-rollback - "Verify that lightweight snapshots can accurately restore previous pointer states." - (clrhash *memory*) - (clrhash *history-store*) - (setf *object-store-snapshots* nil) - - (let* ((ast-v1 '(:type :HEADLINE :properties (:ID "cow-node" :TITLE "State A") :contents nil)) - (id-v1 (ingest-ast ast-v1)) - (hash-v1 (org-object-hash (lookup-object id-v1)))) - - ;; Take a snapshot at State A - (snapshot-memory) - - (let* ((ast-v2 '(:type :HEADLINE :properties (:ID "cow-node" :TITLE "State B") :contents nil)) - (id-v2 (ingest-ast ast-v2)) - (hash-v2 (org-object-hash (lookup-object id-v2)))) - - ;; Verify we are currently in State B - (is (equal (org-object-hash (lookup-object "cow-node")) hash-v2)) - - ;; Rollback to State A (index 0 because we only took 1 snapshot) - (rollback-memory 0) - - ;; Verify we are back in State A - (is (equal (org-object-hash (lookup-object "cow-node")) hash-v1)) - -;; Verify State B is still safely in the history store (no data loss) - (is (not (null (gethash hash-v2 *history-store*))))))) - -(test merkle-hash-consistency - "Verify that identical ASTs produce identical Merkle hashes." + "Verify identical ASTs produce identical Merkle hashes." (let* ((ast1 '(:type :HEADLINE :properties (:ID "test-1" :TITLE "Node 1") :contents nil))) (clrhash *memory*) (let ((id1 (ingest-ast ast1))) @@ -494,16 +357,33 @@ Following the Engineering Standards, the Memory must be empirically verified thr (let ((hash2 (org-object-hash (lookup-object id2)))) (is (equal hash1 hash2)))))))) -(test merkle-hash-cascading - "Verify that child changes propagate to parent hashes." - (let* ((ast-root '(:type :HEADLINE :properties (:ID "root" :TITLE "Root") - :contents ((:type :HEADLINE :properties (:ID "leaf" :TITLE "Leaf") :contents nil)))) - (id-root (progn (clrhash *memory*) (ingest-ast ast-root))) - (root-hash (org-object-hash (lookup-object id-root)))) - ;; Now ingest a modified child - parent hash should change - (let* ((ast-mod '(:type :HEADLINE :properties (:ID "root" :TITLE "Root") - :contents ((:type :HEADLINE :properties (:ID "leaf" :TITLE "Changed") :contents nil)))) - (id-mod (progn (clrhash *memory*) (ingest-ast ast-mod))) - (mod-hash (org-object-hash (lookup-object id-mod)))) - (is (not (equal root-hash mod-hash)))))) +(test history-store-immutability + "Verify that *history-store* retains old versions." + (clrhash *memory*) + (clrhash *history-store*) + (let* ((ast-v1 '(:type :HEADLINE :properties (:ID "test-node" :TITLE "Version 1") :contents nil)) + (id-v1 (ingest-ast ast-v1)) + (obj-v1 (lookup-object id-v1)) + (hash-v1 (org-object-hash obj-v1))) + (let* ((ast-v2 '(:type :HEADLINE :properties (:ID "test-node" :TITLE "Version 2") :contents nil)) + (id-v2 (ingest-ast ast-v2)) + (hash-v2 (org-object-hash (lookup-object id-v2)))) + (is (equal (org-object-hash (lookup-object "test-node")) hash-v2)) + (is (not (null (gethash hash-v1 *history-store*)))) + (is (not (null (gethash hash-v2 *history-store*))))))) + +(test cow-snapshot-and-rollback + "Verify that lightweight snapshots restore previous pointer states." + (clrhash *memory*) + (setf *object-store-snapshots* nil) + (let* ((ast-v1 '(:type :HEADLINE :properties (:ID "cow-node" :TITLE "State A") :contents nil)) + (id-v1 (ingest-ast ast-v1)) + (hash-v1 (org-object-hash (lookup-object id-v1)))) + (snapshot-memory) + (let* ((ast-v2 '(:type :HEADLINE :properties (:ID "cow-node" :TITLE "State B") :contents nil)) + (id-v2 (ingest-ast ast-v2)) + (hash-v2 (org-object-hash (lookup-object id-v2)))) + (is (equal (org-object-hash (lookup-object "cow-node")) hash-v2)) + (rollback-memory 0) + (is (equal (org-object-hash (lookup-object "cow-node")) hash-v1))))) #+end_src diff --git a/harness/package.org b/harness/package.org index 3b075f2..e1404d0 100644 --- a/harness/package.org +++ b/harness/package.org @@ -1,3 +1,4 @@ +#+PROPERTY: header-args:lisp :tangle (expand-file-name "package.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) #+TITLE: System Interface (package.lisp) #+AUTHOR: Amr #+FILETAGS: :harness:interface: @@ -9,7 +10,7 @@ The ~package.lisp~ file defines the public API of the ~opencortex~ harness. It s ** Public API Export -#+begin_src lisp :tangle (expand-file-name "package.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defpackage :opencortex (:use :cl) (:export @@ -154,13 +155,13 @@ The ~package.lisp~ file defines the public API of the ~opencortex~ harness. It s * Package Implementation -#+begin_src lisp :tangle (expand-file-name "package.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (in-package :opencortex) #+end_src ** Robust Plist Accessor -#+begin_src lisp :tangle (expand-file-name "package.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defun proto-get (plist key) "Robustly retrieves a value from a plist, checking both uppercase and lowercase keyword versions." (let* ((s (string key)) @@ -173,7 +174,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 (expand-file-name "package.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defvar *system-logs* nil) (defvar *logs-lock* (bordeaux-threads:make-lock "harness-logs-lock")) (defvar *max-log-history* 100) @@ -181,14 +182,14 @@ The harness maintains a thread-safe circular log buffer to provide context for d ** Skills Registry -#+begin_src lisp :tangle (expand-file-name "package.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defvar *skills-registry* (make-hash-table :test 'equal) "Global registry of all loaded skills.") #+end_src ** Skill Telemetry State -#+begin_src lisp :tangle (expand-file-name "package.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defvar *skill-telemetry* (make-hash-table :test 'equal)) (defvar *telemetry-lock* (bordeaux-threads:make-lock "harness-telemetry-lock")) #+end_src @@ -197,7 +198,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 (expand-file-name "package.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src 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 @@ -213,7 +214,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 (expand-file-name "package.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defvar *cognitive-tools* (make-hash-table :test 'equal)) (defstruct cognitive-tool @@ -237,7 +238,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 (expand-file-name "package.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defun harness-log (msg &rest args) "Centralized logging for the harness." (let ((formatted-msg (apply #'format nil msg args))) diff --git a/harness/perceive.org b/harness/perceive.org index e446df9..f2dc6da 100644 --- a/harness/perceive.org +++ b/harness/perceive.org @@ -1,3 +1,4 @@ +#+PROPERTY: header-args:lisp :tangle (expand-file-name "perceive.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) #+TITLE: Stage 1: Perceive (perceive.lisp) #+AUTHOR: Amr #+FILETAGS: :harness:perceive: @@ -53,7 +54,7 @@ Other sensors (heartbeats, interrupts) are processed synchronously to maintain o * Package Context -#+begin_src lisp :tangle (expand-file-name "perceive.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (in-package :opencortex) #+end_src @@ -61,7 +62,7 @@ Other sensors (heartbeats, interrupts) are processed synchronously to maintain o ** Async Sensor Registry -#+begin_src lisp :tangle (expand-file-name "perceive.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defvar *async-sensors* '(:chat-message :delegation :user-command) "Sensors that are processed in dedicated threads. @@ -74,7 +75,7 @@ Other sensors (heartbeats, interrupts) are processed synchronously to maintain o ** Foveal Focus State -#+begin_src lisp :tangle (expand-file-name "perceive.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defvar *foveal-focus-id* nil "The Org ID of the node the user is currently interacting with. @@ -89,7 +90,7 @@ Other sensors (heartbeats, interrupts) are processed synchronously to maintain o ** inject-stimulus: Entry Point -#+begin_src lisp :tangle (expand-file-name "perceive.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defun inject-stimulus (raw-message &key stream (depth 0)) "Inject a raw message into the signal processing pipeline. @@ -146,7 +147,7 @@ Other sensors (heartbeats, interrupts) are processed synchronously to maintain o ** perceive-gate: Signal Normalization -#+begin_src lisp :tangle (expand-file-name "perceive.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defun perceive-gate (signal) "Stage 1 of the metabolic pipeline: Normalize sensory input. diff --git a/harness/reason.org b/harness/reason.org index a5a0a4c..ed36139 100644 --- a/harness/reason.org +++ b/harness/reason.org @@ -1,3 +1,4 @@ +#+PROPERTY: header-args:lisp :tangle (expand-file-name "reason.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) #+TITLE: Stage 2: Reason (reason.lisp) #+AUTHOR: Amr #+FILETAGS: :harness:reason: @@ -33,7 +34,7 @@ This means the reasoning pipeline can generate, modify, and execute its own comm * Package Context -#+begin_src lisp :tangle (expand-file-name "reason.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (in-package :opencortex) #+end_src @@ -43,7 +44,7 @@ The probabilistic engine is responsible for all neural/LLM operations. It mainta ** Backend Registry Variables -#+begin_src lisp :tangle (expand-file-name "reason.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defvar *probabilistic-backends* (make-hash-table :test 'equal) "Registry mapping provider keywords (:openrouter, :ollama) to their calling functions.") @@ -60,7 +61,7 @@ The probabilistic engine is responsible for all neural/LLM operations. It mainta ** register-probabilistic-backend: Backend Registration -#+begin_src lisp :tangle (expand-file-name "reason.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defun register-probabilistic-backend (name fn) "Register a neural provider backend. @@ -79,7 +80,7 @@ The probabilistic engine is responsible for all neural/LLM operations. It mainta ** probabilistic-call: Cascade Dispatch -#+begin_src lisp :tangle (expand-file-name "reason.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defun probabilistic-call (prompt &key (system-prompt "You are the Probabilistic engine.") (cascade nil) @@ -129,7 +130,7 @@ The `think` function is the heart of the probabilistic engine. It constructs a p ** strip-markdown: Clean LLM Output -#+begin_src lisp :tangle (expand-file-name "reason.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defun strip-markdown (text) "Strip markdown formatting from LLM output. @@ -152,7 +153,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 (expand-file-name "reason.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defun normalize-plist-keywords (plist) "Normalize all keys in a plist to keywords. @@ -176,7 +177,7 @@ The `think` function is the heart of the probabilistic engine. It constructs a p ** think: Generate Action Proposal -#+begin_src lisp :tangle (expand-file-name "reason.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defun think (context) "Generate a Lisp action proposal based on current context. @@ -328,7 +329,7 @@ The deterministic engine runs all registered skills' verification functions. Thi ** deterministic-verify: Skill Chain Verification -#+begin_src lisp :tangle (expand-file-name "reason.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defun deterministic-verify (proposed-action context) "Run all skill deterministic gates on a proposed action. @@ -399,7 +400,7 @@ The deterministic engine runs all registered skills' verification functions. Thi ** reason-gate: The Stage Function -#+begin_src lisp :tangle (expand-file-name "reason.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defun reason-gate (signal) "Stage 2 of the metabolic pipeline: Reason. diff --git a/harness/skills.org b/harness/skills.org index ac24f67..77adf3a 100644 --- a/harness/skills.org +++ b/harness/skills.org @@ -1,3 +1,4 @@ +#+PROPERTY: header-args:lisp :tangle (expand-file-name "skills.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) #+TITLE: The Skill Engine (skills.lisp) #+AUTHOR: Amr #+FILETAGS: :harness:skills: @@ -61,7 +62,7 @@ flowchart LR ** Global Skill Registry -#+begin_src lisp :tangle (expand-file-name "skills.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (in-package :opencortex) (defun COSINE-SIMILARITY (v1 v2) @@ -137,7 +138,7 @@ flowchart LR #+end_src ** Skill File Analysis (parse-skill-metadata) -#+begin_src lisp :tangle (expand-file-name "skills.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defun parse-skill-metadata (filepath) "Extracts ID and DEPENDS_ON tags from org file." (let ((dependencies nil) @@ -163,7 +164,7 @@ flowchart LR #+end_src ** Dependency Resolution (topological-sort-skills) -#+begin_src lisp :tangle (expand-file-name "skills.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src 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")) @@ -207,7 +208,7 @@ flowchart LR #+end_src ** Jailed Loading (load-skill-from-org) -#+begin_src lisp :tangle (expand-file-name "skills.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src 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 @@ -227,15 +228,26 @@ reader check during early boot before the validator skill is loaded." (values nil (or (getf result :reason) "Lisp Validator rejected code."))))) (defun extract-tangle-target (line) - "Extracts the value of the :tangle header from an org src block line." + "Extracts the value of the :tangle header from an org src block line. +Handles both simple strings and parenthesized elisp expressions." (let ((pos (search ":tangle" line))) (when pos - (let* ((rest (subseq line (+ pos 7))) - (trimmed (string-trim '(#\Space #\Tab) rest)) - (end (position #\Space trimmed))) - (if end - (subseq trimmed 0 end) - trimmed))))) + (let ((rest (string-trim '(#\Space #\Tab) (subseq line (+ pos 7))))) + (if (char= (char rest 0) #\() + ;; It's an elisp expression, find the matching closing paren + (let ((balance 0) + (end nil)) + (dotimes (i (length rest)) + (let ((ch (char rest i))) + (cond ((char= ch #\() (incf balance)) + ((char= ch #\)) (decf balance))) + (when (and (> i 0) (= balance 0)) + (setf end (1+ i)) + (return-from extract-tangle-target (subseq rest 0 end))))) + rest) + ;; It's a simple string, stop at next space + (let ((end (position #\Space rest))) + (if end (subseq rest 0 end) rest))))))) (defun load-skill-from-org (filepath) "Parses and evaluates Lisp blocks with :tangle directives from an Org file. @@ -259,9 +271,8 @@ Only loads blocks that specify a .lisp tangle target, ignoring tests and example ((uiop:string-prefix-p "#+begin_src lisp" clean-line) (setf in-lisp-block t) (let ((tangle-target (extract-tangle-target clean-line))) - (if (and tangle-target - (not (search "tests/" tangle-target)) - (not (search ":tangle no" clean-line))) + (if (or (and tangle-target (not (search "/tests" tangle-target)) (not (search ":tangle no" clean-line))) + (and (not tangle-target) (not (search ":tangle no" clean-line)))) (setf collect-this-block t) (setf collect-this-block nil)))) @@ -359,7 +370,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 (expand-file-name "skills.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (defun generate-tool-belt-prompt () "Aggregates all registered cognitive tools into a descriptive prompt." (let ((output (format nil "AVAILABLE TOOLS: @@ -386,7 +397,7 @@ EXAMPLES: ** The Default Tool Belt *** The Eval Tool (Internal Inspection) -#+begin_src lisp :tangle (expand-file-name "skills.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src 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) @@ -404,7 +415,7 @@ EXAMPLES: #+end_src *** The Grep Tool (File Discovery) -#+begin_src lisp :tangle (expand-file-name "skills.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src 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)")) @@ -416,7 +427,7 @@ EXAMPLES: #+end_src *** The Shell Tool (Machine Actuation) -#+begin_src lisp :tangle (expand-file-name "skills.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src 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) @@ -431,7 +442,7 @@ EXAMPLES: #+end_src *** The Reload-Skill Tool (Hot Reload) -#+begin_src lisp :tangle (expand-file-name "skills.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src 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) @@ -467,7 +478,7 @@ EXAMPLES: #+end_src *** The File Read Tool (V 0.2.0 File I/O) -#+begin_src lisp :tangle (expand-file-name "skills.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src 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) @@ -486,7 +497,7 @@ EXAMPLES: #+end_src *** The File Write Tool (V 0.2.0 File I/O) -#+begin_src lisp :tangle (expand-file-name "skills.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src 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") @@ -519,7 +530,7 @@ EXAMPLES: #+end_src *** The String Replace Tool (V 0.2.0 File I/O) -#+begin_src lisp :tangle (expand-file-name "skills.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src 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") diff --git a/harness/tui-client.org b/harness/tui-client.org index 4ad0b5e..cd63a02 100644 --- a/harness/tui-client.org +++ b/harness/tui-client.org @@ -1,3 +1,4 @@ +#+PROPERTY: header-args:lisp :tangle (expand-file-name "tui-client.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) :PROPERTIES: :ID: tui-client-spec :CREATED: [2026-04-17 Fri 11:00] @@ -10,7 +11,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 (expand-file-name "tui-client.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/harness")) +#+begin_src lisp (in-package :cl-user) (defpackage :opencortex.tui (:use :cl :croatoan) diff --git a/opencortex.sh b/opencortex.sh index 85da4fb..78b8d77 100755 --- a/opencortex.sh +++ b/opencortex.sh @@ -30,7 +30,7 @@ fi # --- 1. BOOTSTRAP --- # If the script is run standalone, it clones the full repo and restarts itself. -if [ ! -d "$SCRIPT_DIR/.git" ] && [ ! -d "$HOME/.opencortex" ] && [[ ! "$(pwd)" =~ "opencortex" ]]; then +if [ ! -d "$SCRIPT_DIR/.git" ] && [ ! -f "$SCRIPT_DIR/harness/package.org" ] && [ ! -d "$HOME/.opencortex" ] && [[ ! "$(pwd)" =~ "opencortex" ]]; then echo -e "${BLUE}=== OpenCortex: Zero-to-One Bootstrapper ===${NC}" git clone ssh://git@10.10.10.201:2222/amr/opencortex.git ~/.opencortex cd ~/.opencortex && git submodule update --init --recursive @@ -144,7 +144,7 @@ setup_system() { fi echo -e "${YELLOW}--- Finalizing: Awakening the Brain ---${NC}" - "$I_DIR/opencortex.sh" --boot > "$I_DIR/brain.log" 2>&1 & + export I_DIR="$I_DIR"; "$I_DIR/opencortex.sh" --boot > "$I_DIR/brain.log" 2>&1 & success=false for i in {1..30}; do @@ -188,7 +188,7 @@ case "$COMMAND" in if [ -f "$SCRIPT_DIR/.env" ]; then export OPENROUTER_API_KEY=$(grep OPENROUTER_API_KEY "$SCRIPT_DIR/.env" | cut -d'"' -f2) fi - exec sbcl --non-interactive --eval '(load (merge-pathnames "quicklisp/setup.lisp" (user-homedir-pathname)))' --eval '(setf *debugger-hook* (lambda (c h) (declare (ignore h)) (format *error-output* "FATAL LISP ERROR: ~a~%" c) (uiop:print-backtrace :stream *error-output*) (uiop:quit 1)))' --eval '(push (truename (uiop:getenv "I_DIR")) asdf:*central-registry*)' --eval '(format t "--- Quickloading OpenCortex ---~%")' --eval "(ql:quickload '(:opencortex :croatoan))" --eval '(opencortex:main)' + exec sbcl --non-interactive --eval '(load (merge-pathnames "quicklisp/setup.lisp" (user-homedir-pathname)))' --eval '(setf *debugger-hook* (lambda (c h) (declare (ignore h)) (format *error-output* "FATAL LISP ERROR: ~a~%" c) (uiop:print-backtrace :stream *error-output*) (uiop:quit 1)))' --eval '(let ((path (or (uiop:getenv "I_DIR") (uiop:getenv "SCRIPT_DIR")))) (when path (push (truename path) asdf:*central-registry*)))' --eval '(format t "--- Quickloading OpenCortex ---~%")' --eval "(ql:quickload '(:opencortex :croatoan))" --eval '(opencortex:main)' ;; tui) @@ -205,7 +205,7 @@ case "$COMMAND" in echo -e "Launching Croatoan TUI..." export SKILLS_DIR="${SCRIPT_DIR}/skills" [ -z "$MEMEX_DIR" ] && export MEMEX_DIR="$HOME/memex" - exec sbcl --eval '(load (merge-pathnames "quicklisp/setup.lisp" (user-homedir-pathname)))' --eval '(push (truename (uiop:getenv "I_DIR")) asdf:*central-registry*)' --eval '(ql:quickload :opencortex/tui)' --eval '(opencortex.tui:main)' + exec sbcl --eval '(load (merge-pathnames "quicklisp/setup.lisp" (user-homedir-pathname)))' --eval '(let ((path (or (uiop:getenv "I_DIR") (uiop:getenv "SCRIPT_DIR")))) (when path (push (truename path) asdf:*central-registry*)))' --eval '(ql:quickload :opencortex/tui)' --eval '(opencortex.tui:main)' ;; cli) diff --git a/skills/org-skill-bouncer.org b/skills/org-skill-bouncer.org index d3c12c2..4e4f69f 100644 --- a/skills/org-skill-bouncer.org +++ b/skills/org-skill-bouncer.org @@ -1,3 +1,4 @@ +#+PROPERTY: header-args:lisp :tangle (expand-file-name "org-skill-bouncer.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) :PROPERTIES: :ID: bouncer-agent-skill :CREATED: [2026-04-11 Sat 15:20] @@ -38,7 +39,7 @@ When something is blocked, the logs clearly show which layer blocked it and why. * Package Context -#+begin_src lisp :tangle (expand-file-name "org-skill-bouncer.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (in-package :opencortex) #+end_src @@ -58,7 +59,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 (expand-file-name "org-skill-bouncer.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defun bouncer-scan-secrets (text) "Scans TEXT for known secrets from the vault. @@ -91,7 +92,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 (expand-file-name "org-skill-bouncer.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src 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 +130,7 @@ Detects when shell commands try to send data to untrusted network destinations. ** bouncer-check: Main Security Gate -#+begin_src lisp :tangle (expand-file-name "org-skill-bouncer.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defun bouncer-check (action context) "The 5-Vector security gate for high-risk actions. @@ -213,7 +214,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 (expand-file-name "org-skill-bouncer.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defun bouncer-process-approvals () "Scans the object store for APPROVED flight plans and re-injects them. @@ -269,7 +270,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 (expand-file-name "org-skill-bouncer.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defun bouncer-create-flight-plan (blocked-action) "Creates an Org node representing a pending flight plan for manual approval. @@ -306,7 +307,7 @@ When the Bouncer intercepts a high-risk action, it creates a flight plan node fo ** Main Gate Function -#+begin_src lisp :tangle (expand-file-name "org-skill-bouncer.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defun bouncer-deterministic-gate (action context) "Main deterministic gate for the Bouncer skill. @@ -345,7 +346,7 @@ When the Bouncer intercepts a high-risk action, it creates a flight plan node fo ** Skill Registration -#+begin_src lisp :tangle (expand-file-name "org-skill-bouncer.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defskill :skill-bouncer :priority 150 :trigger (lambda (ctx) (declare (ignore ctx)) t) diff --git a/skills/org-skill-cli-gateway.org b/skills/org-skill-cli-gateway.org index 2ecb18a..9f82723 100644 --- a/skills/org-skill-cli-gateway.org +++ b/skills/org-skill-cli-gateway.org @@ -1,3 +1,4 @@ +#+PROPERTY: header-args:lisp :tangle (expand-file-name "org-skill-cli-gateway.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) :PROPERTIES: :ID: cli-gateway-skill :CREATED: [2026-04-13 Mon 17:00] @@ -11,7 +12,7 @@ The *CLI Gateway* is the primary sensory and actuating interface for human inter * Implementation -#+begin_src lisp :tangle (expand-file-name "org-skill-cli-gateway.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defvar *cli-port* 9105) (defvar *cli-server-socket* nil) diff --git a/skills/org-skill-credentials-vault.org b/skills/org-skill-credentials-vault.org index 25f7761..d8081f8 100644 --- a/skills/org-skill-credentials-vault.org +++ b/skills/org-skill-credentials-vault.org @@ -1,3 +1,4 @@ +#+PROPERTY: header-args:lisp :tangle (expand-file-name "org-skill-credentials-vault.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) :PROPERTIES: :ID: credentials-vault-skill :CREATED: [2026-04-09 Thu] @@ -33,7 +34,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 (expand-file-name "org-skill-credentials-vault.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defun vault-get-secret (provider &key type) "Retrieves a secret (api-key or session) for a provider.") @@ -61,13 +62,13 @@ Tests in `tests/vault-tests.lisp` will verify: * Phase D: Build (Implementation) ** Package Context -#+begin_src lisp :tangle (expand-file-name "org-skill-credentials-vault.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src 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 (expand-file-name "org-skill-credentials-vault.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defvar opencortex::*vault-memory* (make-hash-table :test 'equal) "In-memory cache of sensitive credentials.") #+end_src @@ -75,7 +76,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 (expand-file-name "org-skill-credentials-vault.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defun vault-mask-string (str) "Returns a masked version of a sensitive string." (if (and str (> (length str) 8)) @@ -86,7 +87,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 (expand-file-name "org-skill-credentials-vault.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src 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 +113,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 (expand-file-name "org-skill-credentials-vault.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src 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 +126,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 (expand-file-name "org-skill-credentials-vault.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defun vault-onboard-gemini-web () "Instructions for the Autonomous Cookie Handshake." (harness-log "--- GEMINI WEB ONBOARDING ---") @@ -137,7 +138,7 @@ Retained from the legacy Google skill, this provides the instructions for the au #+end_src ** Registration -#+begin_src lisp :tangle (expand-file-name "org-skill-credentials-vault.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (progn (defskill :skill-credentials-vault :priority 200 ; High priority, foundational diff --git a/skills/org-skill-emacs-edit.org b/skills/org-skill-emacs-edit.org index 9dd3223..76178ad 100644 --- a/skills/org-skill-emacs-edit.org +++ b/skills/org-skill-emacs-edit.org @@ -1,3 +1,4 @@ +#+PROPERTY: header-args:lisp :tangle (expand-file-name "org-skill-emacs-edit.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) :PROPERTIES: :ID: emacs-edit-skill :CREATED: [2026-04-23 Thu] @@ -58,14 +59,14 @@ Single entry point `emacs-edit-modify` takes a file path, operation, and paramet * Phase D: Build (Implementation) ** Package Context -#+begin_src lisp :tangle (expand-file-name "org-skill-emacs-edit.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (in-package :opencortex) #+end_src ** ID Generation Generate unique IDs for headlines. -#+begin_src lisp :tangle (expand-file-name "org-skill-emacs-edit.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defun emacs-edit-generate-id () "Generates a unique ID for org-mode headlines. Format: 8-char hex + timestamp for uniqueness." @@ -84,7 +85,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 (expand-file-name "org-skill-emacs-edit.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src 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 +155,7 @@ Preserves structure including #+begin_src blocks." ** Read Operation Parse org file to AST. -#+begin_src lisp :tangle (expand-file-name "org-skill-emacs-edit.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defvar *org-parser-cache* (make-hash-table :test 'equal) "Cache for parsed org files.") @@ -180,7 +181,7 @@ Returns the parsed AST. Uses cache for performance." ** Write Operation Write AST back to file preserving structure. -#+begin_src lisp :tangle (expand-file-name "org-skill-emacs-edit.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defun emacs-edit-write-file (file-path ast) "Writes AST back to FILE-PATH, preserving org structure. Clears cache after write." @@ -195,7 +196,7 @@ Clears cache after write." ** Add Headline Operation Add a new headline to an existing AST. -#+begin_src lisp :tangle (expand-file-name "org-skill-emacs-edit.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defun emacs-edit-add-headline (ast title &key todo properties) "Adds a new headline to AST. Returns modified AST." @@ -224,7 +225,7 @@ Returns modified AST." ** Set Property Operation Set a property on an existing headline (by ID or TITLE). -#+begin_src lisp :tangle (expand-file-name "org-skill-emacs-edit.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defun emacs-edit-find-headline-by-id (ast target-id) "Recursively finds headline with matching :ID: property." (when (eq (getf ast :type) :headline) @@ -268,7 +269,7 @@ Returns modified AST." ** Set TODO State Operation Change TODO state (TODO → DONE → etc). -#+begin_src lisp :tangle (expand-file-name "org-skill-emacs-edit.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src 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." @@ -279,7 +280,7 @@ NEW-STATE should be 'TODO', 'DONE', 'IN-PROGRESS', etc." ** Unified Entry Point Main operation dispatcher. -#+begin_src lisp :tangle (expand-file-name "org-skill-emacs-edit.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defun emacs-edit-modify (file-path operation &key params) "Main entry point for org-mode file manipulation. OPERATIONS: @@ -322,7 +323,7 @@ OPERATIONS: ** Cognitive Tools Exposes operations to the Probabilistic Engine. -#+begin_src lisp :tangle (expand-file-name "org-skill-emacs-edit.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src 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." diff --git a/skills/org-skill-engineering-standards.org b/skills/org-skill-engineering-standards.org index 62b42f6..d34ea93 100644 --- a/skills/org-skill-engineering-standards.org +++ b/skills/org-skill-engineering-standards.org @@ -1,3 +1,4 @@ +#+PROPERTY: header-args:lisp :tangle (expand-file-name "org-skill-engineering-standards.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) :PROPERTIES: :ID: 37f2b59f-4537-4cca-ac7f-5c24b9e2e773 :CREATED: [2026-03-30 Mon 21:16] @@ -105,16 +106,16 @@ You are forbidden from considering a task complete without updating ~gtd.org~. R The engineering standards skill is a HARD BLOCK gate. Violations are rejected, not warned. -** Pre-Task Enforcement (Blocking) +** Global Configuration #+begin_src lisp :tangle (expand-file-name "org-skill-engineering-standards.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) (in-package :opencortex) -(defvar *engineering-std-*project-root* nil +(defvar *engineering-std-project-root* nil "Path to the project root for enforcement checks.") (defun engineering-std-set-project-root (path) - (setf *engineering-std-*project-root* (uiop:ensure-directory-pathname path))) + (setf *engineering-std-project-root* (uiop:ensure-directory-pathname path))) (defstruct engineering-violation (phase nil) @@ -133,12 +134,14 @@ The engineering standards skill is a HARD BLOCK gate. Violations are rejected, n (:post-task (:tests-pass "All tests must pass") (:no-artifacts "No orphaned .bak, .log, .tmp files")))) + +(defvar *engineering-std-initialized* nil) #+end_src ** Git Clean Check (Blocking) #+begin_src lisp :tangle (expand-file-name "org-skill-engineering-standards.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) -(defun verify-git-clean-p (&optional (dir *engineering-std-*project-root*)) +(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 (let ((status (uiop:run-program (list "git" "-C" (namestring dir) "status" "--porcelain") @@ -146,7 +149,7 @@ The engineering standards skill is a HARD BLOCK gate. Violations are rejected, n :ignore-error-status t))) (string= "" (string-trim '(#\Space #\Newline #\Tab) status))))) -(defun check-git-clean (&optional (dir *engineering-std-*project-root*)) +(defun check-git-clean (&optional (dir *engineering-std-project-root*)) "Returns violation if git is dirty, nil if clean." (unless (verify-git-clean-p dir) (make-engineering-violation @@ -156,11 +159,69 @@ The engineering standards skill is a HARD BLOCK gate. Violations are rejected, n :severity :blocker))) #+end_src +** Blocking Gate (Hard Enforcement) -** Test Suite +#+begin_src lisp :tangle (expand-file-name "org-skill-engineering-standards.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +(defun engineering-standards-gate (action context) + "The deterministic HARD BLOCK gate for Engineering Standards. -These tests verify the enforcement logic. Run with: -~(fiveam:run! 'engineering-standards-suite)~ + BLOCKING checks (return :LOG on violation): + - Git tree must be clean before file modifications + + WARNING checks (log only): + - Skill catalog should be queried first + + Returns modified action, or :LOG/:EVENT on violation." + (let* ((payload (getf action :payload)) + (tool (getf payload :tool)) + (file (getf payload :file)) + (code (getf payload :code)) + (modifies-files-p (or file code tool))) + + ;; BLOCKING: Git clean required for file modifications + (when modifies-files-p + (let ((git-check (check-git-clean *engineering-std-project-root*))) + (when git-check + (harness-log "~a" (engineering-violation-message git-check)) + (return-from engineering-standards-gate + (list :type :log + :payload (list :text (engineering-violation-message git-check))))))) + + action)) +#+end_src + +** Skill Registration + +The skill runs at highest priority (1000) to block violations before any other skill. + +#+begin_src lisp :tangle (expand-file-name "org-skill-engineering-standards.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +(defskill :skill-engineering-standards + :priority 1000 + :trigger (lambda (ctx) + (declare (ignore ctx)) + t) + :probabilistic nil + :deterministic #'engineering-standards-gate) +#+end_src + +** Initialize Project Root + +#+begin_src lisp :tangle (expand-file-name "org-skill-engineering-standards.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +(defun engineering-std-init () + "Initialize the enforcement system with project root." + (unless *engineering-std-initialized* + (let ((env-root (or (uiop:getenv "OPENCORTEX_ROOT") + (uiop:getenv "MEMEX_DIR") + "/home/user/memex/projects/opencortex"))) + (engineering-std-set-project-root env-root) + (setf *engineering-std-initialized* t) + (harness-log "ENGINEERING STANDARDS: Initialized with root ~a" *engineering-std-project-root*)))) + +;; Auto-initialize on load +(engineering-std-init) +#+end_src + +* Test Suite #+begin_src lisp :tangle (expand-file-name "engineering-standards-tests.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/tests")) (defpackage :opencortex-engineering-standards-tests @@ -227,70 +288,6 @@ These tests verify the enforcement logic. Run with: (is (eq :request (getf result :type)))))) #+end_src -** Blocking Gate (Hard Enforcement) - -#+begin_src lisp :tangle (expand-file-name "org-skill-engineering-standards.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) -(defun engineering-standards-gate (action context) - "The deterministic HARD BLOCK gate for Engineering Standards. - - BLOCKING checks (return :LOG on violation): - - Git tree must be clean before file modifications - - WARNING checks (log only): - - Skill catalog should be queried first - - Returns modified action, or :LOG/:EVENT on violation." - (let* ((payload (getf action :payload)) - (tool (getf payload :tool)) - (file (getf payload :file)) - (code (getf payload :code)) - (modifies-files-p (or file code tool))) - - ;; BLOCKING: Git clean required for file modifications - (when modifies-files-p - (let ((git-check (check-git-clean *engineering-std-*project-root*))) - (when git-check - (harness-log "~a" (engineering-violation-message git-check)) - (return-from engineering-standards-gate - (list :type :log - :payload (list :text (engineering-violation-message git-check))))))) - - action)) -#+end_src - -** Skill Registration - -The skill runs at highest priority (1000) to block violations before any other skill. - -#+begin_src lisp :tangle (expand-file-name "org-skill-engineering-standards.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) -(defskill :skill-engineering-standards - :priority 1000 - :trigger (lambda (ctx) - (declare (ignore ctx)) - t) - :probabilistic nil - :deterministic #'engineering-standards-gate) -#+end_src - -** Initialize Project Root - -#+begin_src lisp :tangle (expand-file-name "org-skill-engineering-standards.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) -(defvar *engineering-std-initialized* nil) - -(defun engineering-std-init () - "Initialize the enforcement system with project root." - (unless *engineering-std-initialized* - (let ((env-root (or (uiop:getenv "OPENCORTEX_ROOT") - (uiop:getenv "MEMEX_DIR") - "/home/user/memex/projects/opencortex"))) - (engineering-std-set-project-root env-root) - (setf *engineering-std-initialized* t) - (harness-log "ENGINEERING STANDARDS: Initialized with root ~a" *engineering-std-*project-root*)))) - -;; Auto-initialize on load -(engineering-std-init) -#+end_src - * See Also - [[file:org-skill-literate-programming.org][Literate Programming Skill]] - Structural validation and tangle rules - [[file:org-skill-policy.org][Policy Skill]] - Constitutional constraints diff --git a/skills/org-skill-gardener.org b/skills/org-skill-gardener.org index bf53fdb..7859065 100644 --- a/skills/org-skill-gardener.org +++ b/skills/org-skill-gardener.org @@ -1,3 +1,4 @@ +#+PROPERTY: header-args:lisp :tangle (expand-file-name "org-skill-gardener.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) :PROPERTIES: :ID: gardener-skill :CREATED: [2026-04-13 Mon 18:50] @@ -37,14 +38,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 (expand-file-name "org-skill-gardener.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src 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 (expand-file-name "org-skill-gardener.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defvar *gardener-last-audit* 0 "The universal-time of the last full Memex audit.") #+end_src @@ -52,7 +53,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 (expand-file-name "org-skill-gardener.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defun gardener-find-broken-links () "Returns a list of broken ID links found in the Memex." (let ((broken nil)) @@ -69,7 +70,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 (expand-file-name "org-skill-gardener.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src 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 +96,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 (expand-file-name "org-skill-gardener.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defun gardener-deterministic-gate (action context) "Main gate for the Gardener skill. Audits graph integrity." (declare (ignore action context)) @@ -118,7 +119,7 @@ The Gardener's deterministic gate performs the actual analysis and logs the resu #+end_src ** Skill Registration -#+begin_src lisp :tangle (expand-file-name "org-skill-gardener.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defskill :skill-gardener :priority 40 :trigger (lambda (ctx) diff --git a/skills/org-skill-homoiconic-memory.org b/skills/org-skill-homoiconic-memory.org index 3e15b56..6ce5f1e 100644 --- a/skills/org-skill-homoiconic-memory.org +++ b/skills/org-skill-homoiconic-memory.org @@ -1,3 +1,4 @@ +#+PROPERTY: header-args:lisp :tangle (expand-file-name "org-skill-homoiconic-memory.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) :PROPERTIES: :ID: homoiconic-memory-skill :CREATED: [2026-04-10 Fri] @@ -11,7 +12,7 @@ The *Homoiconic Memory* skill provides the core persistence layer for OpenCortex * Implementation -#+begin_src lisp :tangle (expand-file-name "org-skill-homoiconic-memory.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defun memory-org-to-json (source) "Converts Org-mode source to JSON AST." diff --git a/skills/org-skill-lisp-utils.org b/skills/org-skill-lisp-utils.org index 47aa8f7..ba01e39 100644 --- a/skills/org-skill-lisp-utils.org +++ b/skills/org-skill-lisp-utils.org @@ -1,3 +1,4 @@ +#+PROPERTY: header-args:lisp :tangle (expand-file-name "org-skill-lisp-utils.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) :PROPERTIES: :ID: lisp-utils-skill :CREATED: [2026-04-23 Thu] @@ -16,7 +17,7 @@ The *Lisp Utils* skill provides general-purpose Lisp utilities for the entire sy * Phase D: Build (Implementation) -#+begin_src lisp :tangle (expand-file-name "org-skill-lisp-utils.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (in-package :opencortex) (defun count-char (char string) diff --git a/skills/org-skill-literate-programming.org b/skills/org-skill-literate-programming.org index 8ce64b9..5f31935 100644 --- a/skills/org-skill-literate-programming.org +++ b/skills/org-skill-literate-programming.org @@ -1,3 +1,4 @@ +#+PROPERTY: header-args:lisp :tangle (expand-file-name "org-skill-literate-programming.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) :PROPERTIES: :ID: literate-programming-skill-2026 :CREATED: [2026-04-25 Sat] @@ -57,7 +58,7 @@ Code without surrounding prose is a bug report waiting to happen. ** Block Balance Checker -#+begin_src lisp :tangle (expand-file-name "org-skill-literate-programming.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (in-package :opencortex) (defun literate-check-block-balance (code-string) @@ -94,7 +95,7 @@ Code without surrounding prose is a bug report waiting to happen. ** File-Level Balance Audit -#+begin_src lisp :tangle (expand-file-name "org-skill-literate-programming.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defun literate-audit-org-file (filepath) "Audits all tangled lisp blocks in an Org file for structural balance. @@ -145,7 +146,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 (expand-file-name "org-skill-literate-programming.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src 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 +185,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 (expand-file-name "org-skill-literate-programming.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defskill :skill-literate-programming :priority 1100 :trigger (lambda (ctx) @@ -218,7 +219,7 @@ The LP skill runs at priority 1100 (just below engineering-standards at 1000). ** Initialize Project Root -#+begin_src lisp :tangle (expand-file-name "org-skill-literate-programming.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defvar *lp-initialized* nil) (defun lp-init () diff --git a/skills/org-skill-llama-backend.org b/skills/org-skill-llama-backend.org index b50c825..9fd779f 100644 --- a/skills/org-skill-llama-backend.org +++ b/skills/org-skill-llama-backend.org @@ -1,3 +1,4 @@ +#+PROPERTY: header-args:lisp :tangle (expand-file-name "org-skill-llama-backend.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) :PROPERTIES: :ID: llama-backend-skill :CREATED: [2026-04-17 Fri 20:00] @@ -21,12 +22,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 (expand-file-name "org-skill-llama-backend.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (in-package :opencortex) #+end_src ** The Inference Engine (llama-inference) -#+begin_src lisp :tangle (expand-file-name "org-skill-llama-backend.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src 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 +52,7 @@ This skill acts as a proxy between the OpenCortex kernel and the Lisp-agnostic ` #+end_src ** Registration -#+begin_src lisp :tangle (expand-file-name "org-skill-llama-backend.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (progn (register-probabilistic-backend :llama #'llama-inference) (harness-log "LLAMA: Local backend registered and active.")) diff --git a/skills/org-skill-llm-gateway.org b/skills/org-skill-llm-gateway.org index ee08b87..38a6348 100644 --- a/skills/org-skill-llm-gateway.org +++ b/skills/org-skill-llm-gateway.org @@ -1,3 +1,4 @@ +#+PROPERTY: header-args:lisp :tangle (expand-file-name "org-skill-llm-gateway.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) :PROPERTIES: :ID: llm-gateway-skill :CREATED: [2026-04-09 Thu] @@ -19,7 +20,7 @@ The gateway utilizes a functional dispatch pattern. A single entry point, `execu * Phase D: Build (Implementation) ** Implementation -#+begin_src lisp :tangle (expand-file-name "org-skill-llm-gateway.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defun get-nested (alist &rest keys) "Recursively extracts nested values from an alist, handling both objects and arrays." diff --git a/skills/org-skill-peripheral-vision.org b/skills/org-skill-peripheral-vision.org index 2226617..9f75bbb 100644 --- a/skills/org-skill-peripheral-vision.org +++ b/skills/org-skill-peripheral-vision.org @@ -1,3 +1,4 @@ +#+PROPERTY: header-args:lisp :tangle (expand-file-name "org-skill-peripheral-vision.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) :PROPERTIES: :ID: org-skill-peripheral-vision :CREATED: [2026-04-12 Sun 14:15] @@ -39,10 +40,10 @@ Move context pruning and rendering logic out of `context.lisp` to allow for more * Package Context -#+begin_src lisp :tangle (expand-file-name "org-skill-peripheral-vision.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (in-package :opencortex) #+end_src -\n#+begin_src lisp :tangle (expand-file-name "org-skill-peripheral-vision.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +\n#+begin_src lisp (defun context-render-to-org (obj &key depth foveal-id semantic-threshold foveal-vector) "Recursively renders an org-object with foveal-peripheral pruning.") @@ -53,7 +54,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 (expand-file-name "org-skill-peripheral-vision.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src 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." @@ -117,7 +118,7 @@ Move context pruning and rendering logic out of `context.lisp` to allow for more #+end_src * Registration -#+begin_src lisp :tangle (expand-file-name "org-skill-peripheral-vision.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defskill :skill-peripheral-vision :priority 90 :dependencies ("org-skill-embedding") diff --git a/skills/org-skill-policy.org b/skills/org-skill-policy.org index 46d6a70..097e621 100644 --- a/skills/org-skill-policy.org +++ b/skills/org-skill-policy.org @@ -1,3 +1,4 @@ +#+PROPERTY: header-args:lisp :tangle (expand-file-name "org-skill-policy.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) :PROPERTIES: :ID: 47425a43-2be0-423c-8509-22592cfe9c9e :CREATED: [2026-04-07 Tue 12:57] @@ -44,23 +45,11 @@ 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 (expand-file-name "org-skill-policy.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (in-package :opencortex) #+end_src -* The Override Hierarchy - -When two invariants conflict, resolution follows a strict priority order. This prevents the agent from freezing on ethical edge cases. - -| Priority | Invariant | Philosophy | -|----------|-----------|------------| -| 500 | Transparency | If you can't explain it, you can't do it | -| 400 | Autonomy | Independence from proprietary control is the primary goal | -| 300 | Zero-Bloat | Complexity must be earned, not imported | -| 250 | Modularity | Complexity belongs at the edges, not the core | -| 200 | Mentorship | Teaching increases capability; doing removes it | -| 100 | Sustainability | Offline capability today enables 100-year survival | - +* Global Policy Configuration #+begin_src lisp :tangle (expand-file-name "org-skill-policy.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) (defvar *policy-invariant-priorities* '((:transparency . 500) @@ -76,6 +65,75 @@ When two invariants conflict, the higher priority wins. Example: Modularity (250) takes precedence over Mentorship (200), meaning a change that would fatten the harness is blocked even if it would be educational.") + +(defvar *proprietary-domain-watchlist* + '("googleapis.com" "api.openai.com" "anthropic.com" "api.groq.com" "openrouter.ai") + "Domains representing centralized, proprietary control. + + Actions targeting these are logged as autonomy debt, not hard-blocked. + This is because tactical gateway usage (Telegram, Signal, OpenRouter) + is permitted under the strategic mandate for autonomy. + + Strategic goal: Replace all proprietary APIs with local alternatives. + Tactical reality: Use what's available while building toward that goal.") + +(defvar *policy-max-skill-size-chars* 50000 + "Maximum recommended size for a skill file tangled from an Org note. + + This is a soft limit—the check warns but does not block. + A large, well-documented skill is acceptable; a small, poorly-documented + one that adds unnecessary complexity is not.") + +(defvar *modularity-protected-paths* + '("harness/" "opencortex.asd") + "Paths that constitute the unbreakable core of the system. + + Any action targeting these paths must include a :modularity-justification + explaining why the change cannot be implemented as a skill. + + The Thin Harness principle: What belongs in the harness? + - Core signal processing (Perceive-Reason-Act loop) + - Memory and persistence primitives + - Protocol definition and validation + - Skills register and dispatch + + What belongs in skills? + - Policy and security + - LLM integration + - Domain-specific functionality + - New actuators") + +(defvar *mentorship-required-actions* + '(:create-skill :eval :modify-file :write-file :replace + :rename-file :delete-file :shell :create-note) + "Actions that trigger the Mentorship invariant. + + These are high-impact actions that should come with explanations + not just for the user, but for future debugging and maintenance.") + +(defvar *cloud-only-backends* '(:openrouter :openai :anthropic :groq :gemini-api) + "Backends requiring internet connection and external infrastructure. + + These are acceptable as fallbacks when local inference is unavailable, + but should be logged as sustainability debt for tracking purposes.") +#+end_src + + +* The Override Hierarchy + +When two invariants conflict, resolution follows a strict priority order. This prevents the agent from freezing on ethical edge cases. + +| Priority | Invariant | Philosophy | +|----------|-----------|------------| +| 500 | Transparency | If you can't explain it, you can't do it | +| 400 | Autonomy | Independence from proprietary control is the primary goal | +| 300 | Zero-Bloat | Complexity must be earned, not imported | +| 250 | Modularity | Complexity belongs at the edges, not the core | +| 200 | Mentorship | Teaching increases capability; doing removes it | +| 100 | Sustainability | Offline capability today enables 100-year survival | + +#+begin_src lisp + #+end_src * The Core Invariants @@ -91,7 +149,7 @@ At the gate: - Every user-facing action must carry an `:explanation` - Log messages must include the triggering invariant -#+begin_src lisp :tangle (expand-file-name "org-skill-policy.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defun policy-check-transparency (action context) (defun policy-check-transparency (action context) "Ensures the action is inspectable and user-facing actions carry an explanation. @@ -138,17 +196,8 @@ 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 (expand-file-name "org-skill-policy.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) -(defvar *proprietary-domain-watchlist* - '("googleapis.com" "api.openai.com" "anthropic.com" "api.groq.com" "openrouter.ai") - "Domains representing centralized, proprietary control. +#+begin_src lisp - Actions targeting these are logged as autonomy debt, not hard-blocked. - This is because tactical gateway usage (Telegram, Signal, OpenRouter) - is permitted under the strategic mandate for autonomy. - - Strategic goal: Replace all proprietary APIs with local alternatives. - Tactical reality: Use what's available while building toward that goal.") (defun policy-scan-proprietary-references (action) "Scans ACTION text fields for proprietary domain references. @@ -205,13 +254,8 @@ 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 (expand-file-name "org-skill-policy.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) -(defvar *policy-max-skill-size-chars* 50000 - "Maximum recommended size for a skill file tangled from an Org note. +#+begin_src lisp - This is a soft limit—the check warns but does not block. - A large, well-documented skill is acceptable; a small, poorly-documented - one that adds unnecessary complexity is not.") (defun policy-check-bloat (action context) "Warns if a :create-skill action exceeds the bloat threshold. @@ -256,25 +300,8 @@ 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 (expand-file-name "org-skill-policy.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) -(defvar *modularity-protected-paths* - '("harness/" "opencortex.asd") - "Paths that constitute the unbreakable core of the system. +#+begin_src lisp - Any action targeting these paths must include a :modularity-justification - explaining why the change cannot be implemented as a skill. - - The Thin Harness principle: What belongs in the harness? - - Core signal processing (Perceive-Reason-Act loop) - - Memory and persistence primitives - - Protocol definition and validation - - Skills register and dispatch - - What belongs in skills? - - Policy and security - - LLM integration - - Domain-specific functionality - - New actuators") (defun policy-check-modularity (action context) "Blocks modifications to the system's protected core unless justified. @@ -323,14 +350,8 @@ 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 (expand-file-name "org-skill-policy.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) -(defvar *mentorship-required-actions* - '(:create-skill :eval :modify-file :write-file :replace - :rename-file :delete-file :shell :create-note) - "Actions that trigger the Mentorship invariant. +#+begin_src lisp - These are high-impact actions that should come with explanations - not just for the user, but for future debugging and maintenance.") (defun policy-check-mentorship (action context) "Blocks high-impact actions that lack a mentorship note. @@ -380,12 +401,8 @@ The Memex should be functional even when: This means preferring local, energy-efficient architectures over cloud-dependent ones. -#+begin_src lisp :tangle (expand-file-name "org-skill-policy.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) -(defvar *cloud-only-backends* '(:openrouter :openai :anthropic :groq :gemini-api) - "Backends requiring internet connection and external infrastructure. +#+begin_src lisp - These are acceptable as fallbacks when local inference is unavailable, - but should be logged as sustainability debt for tracking purposes.") (defun policy-check-sustainability (action context) "Logs sustainability debt when action relies on cloud-only infrastructure. @@ -417,7 +434,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 (expand-file-name "org-skill-policy.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defun policy-explain (invariant-key message &optional original-action) "Formats a policy decision into an auditable explanation plist. @@ -446,7 +463,7 @@ When the policy gate blocks or modifies an action, it must tell the user *why*. ** Running Invariant Checks -#+begin_src lisp :tangle (expand-file-name "org-skill-policy.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defun policy-run-invariant-checks (action context) "Runs all invariant checks in priority order. @@ -509,7 +526,7 @@ When the policy gate blocks or modifies an action, it must tell the user *why*. ** Main Policy Gate -#+begin_src lisp :tangle (expand-file-name "org-skill-policy.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defun policy-deterministic-gate (action context) "The main policy gate entry point. @@ -541,7 +558,7 @@ When the policy gate blocks or modifies an action, it must tell the user *why*. * Skill Registration -#+begin_src lisp :tangle (expand-file-name "org-skill-policy.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defskill :skill-policy :priority 500 :trigger (lambda (ctx) (declare (ignore ctx)) t) diff --git a/skills/org-skill-protocol-validator.org b/skills/org-skill-protocol-validator.org index 9cd9e7a..f873279 100644 --- a/skills/org-skill-protocol-validator.org +++ b/skills/org-skill-protocol-validator.org @@ -1,3 +1,4 @@ +#+PROPERTY: header-args:lisp :tangle (expand-file-name "org-skill-protocol-validator.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) :PROPERTIES: :ID: org-skill-communication-protocol-validator :CREATED: [2026-04-12 Sun 14:35] @@ -45,7 +46,7 @@ Decouple protocol parsing (framing/unframing) from semantic validation. * Phase D: Build (Implementation) ** Schema Enforcement -#+begin_src lisp :tangle (expand-file-name "org-skill-protocol-validator.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (in-package :opencortex) (defun validate-communication-protocol-schema (msg) @@ -84,7 +85,7 @@ Decouple protocol parsing (framing/unframing) from semantic validation. #+end_src * Registration -#+begin_src lisp :tangle (expand-file-name "org-skill-protocol-validator.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defskill :skill-communication-protocol-validator :priority 95 :trigger (lambda (ctx) (member (getf (getf ctx :payload) :sensor) '(:protocol-received))) diff --git a/skills/org-skill-scribe.org b/skills/org-skill-scribe.org index d5baa94..951b864 100644 --- a/skills/org-skill-scribe.org +++ b/skills/org-skill-scribe.org @@ -1,3 +1,4 @@ +#+PROPERTY: header-args:lisp :tangle (expand-file-name "org-skill-scribe.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) :PROPERTIES: :ID: scribe-skill :CREATED: [2026-04-13 Mon 18:40] @@ -41,14 +42,14 @@ The Scribe reacts to the `:heartbeat` sensor. It maintains a state file (`scribe * Phase D: Build (Implementation) ** Package Context -#+begin_src lisp :tangle (expand-file-name "org-skill-scribe.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (in-package :opencortex) #+end_src ** State: Checkpoint Management We track the last processed universal time to avoid redundant distillation. -#+begin_src lisp :tangle (expand-file-name "org-skill-scribe.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defvar *scribe-last-checkpoint* 0 "The universal-time of the last successful distillation run.") @@ -70,7 +71,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 (expand-file-name "org-skill-scribe.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defun scribe-get-distillable-nodes () "Returns a list of org-objects from the daily/ folder that require distillation." (let ((results nil)) @@ -91,7 +92,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 (expand-file-name "org-skill-scribe.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defun probabilistic-skill-scribe (context) "Generates the extraction prompt for the Scribe." (let* ((payload (getf context :payload)) @@ -122,7 +123,7 @@ TEXT: ** Deterministic: Note Committal The deterministic gate receives the list of proposed notes and writes them to the filesystem. -#+begin_src lisp :tangle (expand-file-name "org-skill-scribe.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src 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 +160,7 @@ The deterministic gate receives the list of proposed notes and writes them to th #+end_src ** Skill Registration -#+begin_src lisp :tangle (expand-file-name "org-skill-scribe.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defskill :skill-scribe :priority 50 :trigger (lambda (ctx) @@ -174,6 +175,6 @@ The deterministic gate receives the list of proposed notes and writes them to th #+end_src ** Initialization -#+begin_src lisp :tangle (expand-file-name "org-skill-scribe.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (scribe-load-state) #+end_src diff --git a/skills/org-skill-self-edit.org b/skills/org-skill-self-edit.org index 84cd3fa..18de3ae 100644 --- a/skills/org-skill-self-edit.org +++ b/skills/org-skill-self-edit.org @@ -1,3 +1,4 @@ +#+PROPERTY: header-args:lisp :tangle (expand-file-name "org-skill-self-edit.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) :PROPERTIES: :ID: self-edit-001 :END: @@ -14,14 +15,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 (expand-file-name "org-skill-self-edit.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (in-package :opencortex) #+end_src ** Deterministic Paren Repair Fast paren balancing for syntax errors. -#+begin_src lisp :tangle (expand-file-name "org-skill-self-edit.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src 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 +42,7 @@ Fast paren balancing for syntax errors. ** Parse Target Location Extract file and line info from error context. -#+begin_src lisp :tangle (expand-file-name "org-skill-self-edit.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defun self-edit-parse-location (context) "Extracts file and line from error context payload." (let* ((payload (getf context :payload)) @@ -58,7 +59,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 (expand-file-name "org-skill-self-edit.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src 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 +91,7 @@ Returns list with :status and :message keys." #+end_src ** Cognitive Tool: Edit File -#+begin_src lisp :tangle (expand-file-name "org-skill-self-edit.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src 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 +107,7 @@ Returns list with :status and :message keys." ** Skill Definition Hooks into syntax-error events for self-repair. -#+begin_src lisp :tangle (expand-file-name "org-skill-self-edit.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defskill :skill-self-edit :priority 95 :trigger (lambda (ctx) @@ -146,7 +147,7 @@ Provide a fixed version of the code as a lisp form.") #+end_src ** Tool: Quick Paren Fix -#+begin_src lisp :tangle (expand-file-name "org-skill-self-edit.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (def-cognitive-tool :balance-parens "Balances parentheses in a code string." ((:code :type :string :description "The code to balance")) @@ -164,7 +165,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 (expand-file-name "org-skill-self-edit.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defvar *self-edit-skills-backup* nil "Backup of skill registry before hot-reload.") @@ -217,7 +218,7 @@ Swap compiled skill files without breaking active sockets. ** Cognitive Tool: Reload Skill -#+begin_src lisp :tangle (expand-file-name "org-skill-self-edit.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src 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)") diff --git a/skills/org-skill-self-fix.org b/skills/org-skill-self-fix.org index fd0af70..6dfda1a 100644 --- a/skills/org-skill-self-fix.org +++ b/skills/org-skill-self-fix.org @@ -1,3 +1,4 @@ +#+PROPERTY: header-args:lisp :tangle (expand-file-name "org-skill-self-fix.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) :PROPERTIES: :ID: 65891ce2-a465-49e6-a0c1-be13d3288d55 :CREATED: [2026-03-30 Mon 21:16] @@ -15,11 +16,11 @@ This skill enables self-editing by applying surgical fixes to files (including s * Phase D: Build (Implementation) ** Repair Logic -#+begin_src lisp :tangle (expand-file-name "org-skill-self-fix.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (in-package :opencortex) #+end_src -#+begin_src lisp :tangle (expand-file-name "org-skill-self-fix.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defun self-fix-apply (action context) "Applies a surgical code fix and reloads the modified skill." (declare (ignore context)) @@ -66,7 +67,7 @@ This skill enables self-editing by applying surgical fixes to files (including s #+end_src ** Cognitive Tool -#+begin_src lisp :tangle (expand-file-name "org-skill-self-fix.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src 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 +80,7 @@ This skill enables self-editing by applying surgical fixes to files (including s #+end_src ** Skill Definition -#+begin_src lisp :tangle (expand-file-name "org-skill-self-fix.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defskill :skill-self-fix :priority 95 :trigger (lambda (context) (eq (getf (getf context :payload) :sensor) :repair-request)) diff --git a/skills/org-skill-shell-actuator.org b/skills/org-skill-shell-actuator.org index a6db181..bbc6553 100644 --- a/skills/org-skill-shell-actuator.org +++ b/skills/org-skill-shell-actuator.org @@ -1,3 +1,4 @@ +#+PROPERTY: header-args:lisp :tangle (expand-file-name "org-skill-shell-actuator.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) :PROPERTIES: :ID: shell-actuator-skill :CREATED: [2026-04-12 Sun] @@ -11,7 +12,7 @@ The *Shell Actuator* provides a controlled interface for the OpenCortex to execu * Implementation -#+begin_src lisp :tangle (expand-file-name "org-skill-shell-actuator.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (defparameter *allowed-commands* '("ls" "git" "rg" "grep" "date" "echo" "cat" "node" "python3" "sbcl")) diff --git a/skills/org-skill-tool-permissions.org b/skills/org-skill-tool-permissions.org index 13f9dfe..fd8d9f6 100644 --- a/skills/org-skill-tool-permissions.org +++ b/skills/org-skill-tool-permissions.org @@ -1,3 +1,4 @@ +#+PROPERTY: header-args:lisp :tangle (expand-file-name "org-skill-tool-permissions.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) :PROPERTIES: :ID: tool-permissions-skill-001 :CREATED: [2026-04-23 Thu] @@ -27,7 +28,7 @@ Also provides vector embeddings via Ollama or llama.cpp. * Implementation Tool permissions and embedding generation via multiple providers. -#+begin_src lisp :tangle (expand-file-name "org-skill-tool-permissions.lisp" (concat (or (getenv "INSTALL_DIR") ".") "/skills")) +#+begin_src lisp (in-package :opencortex) (defvar *tool-permissions* (make-hash-table :test 'equal)