diff --git a/CONTRIBUTING.org b/CONTRIBUTING.org new file mode 100644 index 0000000..f276af1 --- /dev/null +++ b/CONTRIBUTING.org @@ -0,0 +1,44 @@ +#+TITLE: Contributing to OpenCortex +#+AUTHOR: OpenCortex Contributors +#+STARTUP: content +#+FILETAGS: :docs:contributing: + +* Philosophy +OpenCortex is built on a "Zero-Bloat" mandate. The core kernel is mathematically pure, pushing all peripheral logic, API integrations, and routing to hot-reloadable "Skills". + +* Literate Granularity +We strictly adhere to Literate Programming using Org-mode. +- *Never* edit `.lisp` files in `src/` directly. +- Modify the corresponding `.org` files in the `literate/` or `skills/` directories. +- Run `org-babel-tangle` to generate the source code. +- Every architectural decision, constraint, and implementation detail must be documented alongside the code in the `.org` file. + +* Skill Creation Standard +Skills are the building blocks of OpenCortex. They reside in the `skills/` directory. + +A skill must define: +1. *Trigger*: A lambda determining if the skill should activate based on the context. +2. *Probabilistic Gate*: Optional. Generates a prompt for the LLM. +3. *Deterministic Gate*: A hardcoded Lisp function that guarantees safety or executes side-effects (the "Bouncer" pattern). + +Example Registration: +#+begin_src lisp +(defskill :skill-example + :priority 100 + :trigger (lambda (ctx) ...) + :probabilistic nil + :deterministic (lambda (action ctx) ...)) +#+end_src + +* The Unified Envelope (Communication Protocol) +All inter-process communication occurs via the Unified Envelope. Do not use legacy specific types like `:CHAT`. +- Always use semantic types: `:REQUEST`, `:EVENT`, `:RESPONSE`, `:STATUS`, `:LOG`. +- Include routing metadata in the `:META` block (e.g., `(:SOURCE :TUI)`). +- Ensure generated `:REQUEST` messages include a mandatory `:TARGET` field. + +* Pull Request Process +1. Ensure your working tree is clean. +2. Write tests for your skill in `tests/`. +3. Tangle all files. +4. Run the test suite: `sbcl --eval "(asdf:test-system :opencortex)"`. +5. Submit a PR outlining the architectural intent and the specific Literate changes. \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 5216c5d..eae7fd9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,71 +1,32 @@ -# OPENCORTEX v1.0 Production Environment -FROM debian:bookworm-slim +FROM debian:bullseye-slim -# Prevent interactive prompts during build ENV DEBIAN_FRONTEND=noninteractive -# 1. Install System Dependencies -# - sbcl: The Lisp Runtime -# - curl/git/unzip: Standard tools for Quicklisp and binaries -# - default-jre: Required by signal-cli -# - python3/pip: Required for Playwright bridge -# - socat: Required for stateful CLI interaction RUN apt-get update && apt-get install -y \ sbcl \ + emacs-nox \ curl \ git \ - unzip \ - default-jre \ - libsqlite3-0 \ - python3 \ - python3-pip \ - python3-venv \ - emacs-nox \ socat \ + netcat-openbsd \ + libssl-dev \ + libncurses5-dev \ + libffi-dev \ + zlib1g-dev \ + libsqlite3-dev \ && rm -rf /var/lib/apt/lists/* -# 2. Setup Playwright (High-Fidelity Browsing) -RUN python3 -m venv /opt/venv -ENV PATH="/opt/venv/bin:$PATH" -RUN pip install playwright \ - && playwright install --with-deps chromium - -# 3. Install signal-cli (v0.14.0) -ENV SIGNAL_CLI_VERSION=0.14.0 -RUN curl -L https://github.com/AsamK/signal-cli/releases/download/v${SIGNAL_CLI_VERSION}/signal-cli-${SIGNAL_CLI_VERSION}-Linux.tar.gz | tar xz -C /opt \ - && ln -s /opt/signal-cli-${SIGNAL_CLI_VERSION}/bin/signal-cli /usr/local/bin/signal-cli - -# 4. Install Quicklisp & Pin Distribution -# Pinned to 2026-04-01 for bit-rot resistance. -WORKDIR /root +# Install Quicklisp RUN curl -O https://beta.quicklisp.org/quicklisp.lisp \ - && sbcl --non-interactive \ - --load quicklisp.lisp \ - --eval '(quicklisp-quickstart:install)' \ - --eval '(ql-dist:install-dist "http://beta.quicklisp.org/dist/quicklisp/2026-04-01/distinfo.txt" :prompt nil :replace t)' + && sbcl --non-interactive --load quicklisp.lisp --eval "(quicklisp-quickstart:install)" --eval "(ql-util:without-prompting (ql:add-to-init-file))" \ + && rm quicklisp.lisp -# 5. Configure SBCL to load Quicklisp on startup -RUN echo '(let ((quicklisp-init (merge-pathnames "quicklisp/setup.lisp" (user-homedir-pathname)))) (when (probe-file quicklisp-init) (load quicklisp-init)))' > /root/.sbclrc - -# 6. Setup Application Directory WORKDIR /app -COPY . /app/projects/opencortex +COPY . . -# 7. Pre-cache Lisp Dependencies -RUN sbcl --non-interactive \ - --eval '(push #p"/app/projects/opencortex/" asdf:*central-registry*)' \ - --eval '(ql:quickload :opencortex)' +# Initialize system in non-interactive mode +RUN mkdir -p /root/memex && ./opencortex.sh setup --non-interactive -# 8. Environment & Volumes -# The host's memex root should be mounted to /memex -ENV MEMEX_DIR=/memex -VOLUME ["/memex"] +EXPOSE 9105 -# Default Ports -EXPOSE 9105 8080 - -# Entrypoint -CMD ["sbcl", "--non-interactive", \ - "--eval", "(push #p\"/app/projects/opencortex/\" asdf:*central-registry*)", \ - "--eval", "(ql:quickload :opencortex)", \ - "--eval", "(opencortex:main)"] +CMD ["./opencortex.sh", "boot"] diff --git a/USER_MANUAL.md b/USER_MANUAL.md deleted file mode 100644 index b0f7097..0000000 --- a/USER_MANUAL.md +++ /dev/null @@ -1,55 +0,0 @@ -# OpenCortex v0.1.0 User Manual - -OpenCortex is a neurosymbolic AI agent designed for autonomous Memex maintenance. It combines the probabilistic power of Large Language Models with the deterministic safety of Common Lisp and the structured clarity of Org-mode. - -## 1. Quick Start - -Install and boot OpenCortex with a single command: - -```bash -curl -fsSL https://raw.githubusercontent.com/gharbeia/opencortex/main/opencortex.sh | bash -``` - -Once installed, simply run `opencortex` to start the interactive CLI. - -## 2. The Core MVP Skills - -The v0.1.0 release includes the following essential skills: - -### Safety & Integrity -* **System Policy:** Enforces core invariants (Sovereignty, Transparency). -* **The Bouncer:** Inspects all proposed actions and blocks high-risk operations. -* **Protocol Validator:** Ensures communication integrity. - -### Cognitive Kernel -* **LLM Gateway:** Routes requests to your preferred provider (Gemini, Anthropic, etc.). -* **Peripheral Vision:** Manages context and retrieves relevant notes via Sparse Trees. -* **Memory Steward:** Maintains the live graph of your Org-mode Memex. -* **Credentials Vault:** Securely stores your API keys. - -### Interaction & Actuation -* **CLI Gateway:** The primary interface for chatting with your agent. -* **Shell Actuator:** Allows the agent to perform safe system side-effects. - -### Autonomous Services -* **The Scribe:** Automatically distills your daily chronological logs into structured notes. -* **The Gardener:** Proactively repairs broken links and flags orphaned nodes. - -## 3. Basic Usage - -### Chatting -Type natural language messages into the CLI. The agent will perceive your intent, consult its Memory, and propose actions. - -### Memex Maintenance -OpenCortex monitors your `daily/` directory. Use the Scribe to distill your thoughts: -`User: Distill my notes from yesterday.` - -### Safety Approvals -When the Bouncer intercepts a high-impact action, it will create a "Flight Plan" in your Memex. You must mark it as `APPROVED` before the agent proceeds. - -## 4. Configuration - -All configuration is stored in the `.env` file in your installation directory. You can update your API keys, change your Assistant's name, or modify the mandatory skill list there. - ---- -*OpenCortex: The Conductor of your Life Stack.* diff --git a/USER_MANUAL.org b/USER_MANUAL.org new file mode 100644 index 0000000..d1790c1 --- /dev/null +++ b/USER_MANUAL.org @@ -0,0 +1,56 @@ +#+TITLE: OpenCortex User Manual +#+AUTHOR: OpenCortex Contributors +#+STARTUP: content +#+FILETAGS: :docs:manual: + +* Introduction +Welcome to OpenCortex v0.1.0 (The Autonomous Foundation). OpenCortex is a neurosymbolic AI agent and a Lisp Machine operating system designed to autonomously maintain your Memex (knowledge base) and interact with you via multiple, equal-citizen interfaces. + +* Installation +OpenCortex is bootstrapped via a single shell script. + +#+begin_src bash +git clone ssh://git@10.10.10.201:2222/amr/opencortex.git +cd opencortex +./opencortex.sh setup +#+end_src + +This process will install SBCL, Quicklisp, and prompt you to create a `.env` file for your API keys. + +* Configuration +The system is configured via a `.env` file in the project root. Essential variables include: + +- `OPENROUTER_API_KEY`: Your LLM provider key. +- `PROVIDER_CASCADE`: The fallback order for LLM providers (e.g., `openrouter,ollama,anthropic`). +- `MEMEX_DIR`: The absolute path to your knowledge base (defaults to `~/memex`). + +* Interacting with OpenCortex +Because of the Unified Envelope Architecture, the kernel treats all clients as interchangeable. You must first boot the background daemon: + +#+begin_src bash +./opencortex.sh --boot & +#+end_src + +** Terminal User Interface (TUI) +For a rich, split-pane terminal experience: +#+begin_src bash +./opencortex.sh tui +#+end_src + +** Command Line Interface (CLI) +For raw, pipe-friendly interaction: +#+begin_src bash +./opencortex.sh cli +#+end_src + +** Emacs Integration +OpenCortex functions as your "foveal vision" inside Emacs. +1. Ensure `org-agent.el` is loaded. +2. Run `M-x opencortex-connect`. +3. Interact via the `*opencortex-chat*` buffer. + +* The Memex Structure +OpenCortex assumes a local folder structure representing your "Memex". +- Core memories and identities are mapped to Org-mode files. +- The `Scribe` background worker distills chronological logs into structured Zettelkasten notes. +- The `Gardener` continuously repairs broken links and flags orphaned nodes. \ No newline at end of file diff --git a/check_syntax.py b/check_syntax.py deleted file mode 100644 index 32d14b5..0000000 --- a/check_syntax.py +++ /dev/null @@ -1,25 +0,0 @@ -import re, glob - -def check_file(fp): - with open(fp, 'r') as f: - content = f.read() - blocks = re.findall(r'#\+begin_src lisp\s+(.*?)\s+#\+end_src', content, re.DOTALL) - code = ' '.join(blocks) - - # Very simple check for unbalanced backquotes/commas - # (Doesn't handle strings/comments perfectly but helps) - backquotes = code.count('`') - commas = code.count(',') - - # Count character literals - bq_chars = code.count('#\\`') - comma_chars = code.count('#\\,') - - real_commas = commas - comma_chars - real_backquotes = backquotes - bq_chars - - if real_commas > 0 and real_backquotes == 0: - print(f"WARN: {fp} has {real_commas} commas but 0 backquotes.") - -for fp in glob.glob('skills/*.org'): - check_file(fp) diff --git a/definitive_fix.py b/definitive_fix.py deleted file mode 100644 index b025953..0000000 --- a/definitive_fix.py +++ /dev/null @@ -1,57 +0,0 @@ -import os, glob, re - -def fix_package(): - path = 'src/package.lisp' - with open(path, 'r') as f: content = f.read() - if '*VAULT-MEMORY*' not in content: - content = content.replace('#:read-framed-message', '#:read-framed-message\n #:*VAULT-MEMORY*\n #:COSINE-SIMILARITY\n #:VAULT-MASK-STRING') - with open(path, 'w') as f: f.write(content) - -def fix_bouncer(): - path = 'skills/org-skill-bouncer.org' - with open(path, 'r') as f: content = f.read() - content = content.replace('*vault-memory*', 'opencortex::*vault-memory*') - with open(path, 'w') as f: f.write(content) - -def fix_actuator(): - path = 'skills/org-skill-shell-actuator.org' - with open(path, 'r') as f: content = f.read() - content = content.replace("#`", "#\\`").replace("#,", "#\\,") - # Ensure backquotes are NOT escaped by previous failed sed attempts - content = content.replace("\\`(", "`(").replace("\\,cmd", ",cmd").replace("\\,stdout", ",stdout") - with open(path, 'w') as f: f.write(content) - -def fix_llama(): - path = 'skills/org-skill-llama-backend.org' - with open(path, 'r') as f: content = f.read() - content = content.replace("#`", "#\\`").replace("#,", "#\\,") - content = content.replace("\\`((", "`((").replace("\\,full-prompt", ",full-prompt") - with open(path, 'w') as f: f.write(content) - -def fix_memory(): - path = 'skills/org-skill-homoiconic-memory.org' - with open(path, 'r') as f: content = f.read() - # Replace FiveAM package with a commented version - content = content.replace("(:use :cl :fiveam :opencortex))", "#| (:use :cl :fiveam :opencortex)) |#") - with open(path, 'w') as f: f.write(content) - -def fix_stubs(): - path = 'literate/skills.org' - with open(path, 'r') as f: content = f.read() - stubs = """ -(in-package :opencortex) -(defvar *VAULT-MEMORY* (make-hash-table :test 'equal)) -(defun VAULT-MASK-STRING (s) (if (> (length s) 8) (format nil "~a...~a" (subseq s 0 4) (subseq s (- (length s) 4))) "[MASKED]")) -(defun COSINE-SIMILARITY (v1 v2) (declare (ignore v1 v2)) 1.0) -""" - if 'defvar *VAULT-MEMORY*' not in content: - content = content.replace('(in-package :opencortex)', stubs) - with open(path, 'w') as f: f.write(content) - -fix_package() -fix_bouncer() -fix_actuator() -fix_llama() -fix_memory() -fix_stubs() -print("Definitive fix applied.") diff --git a/final_test.py b/final_test.py deleted file mode 100644 index 3535647..0000000 --- a/final_test.py +++ /dev/null @@ -1,46 +0,0 @@ -import pty, os, time, socket - -# 1. Wait for daemon to be ready -print("Waiting for port 9105...") -for i in range(30): - try: - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.connect(("localhost", 9105)) - s.close() - print("Daemon is up!") - break - except: - time.sleep(1) -else: - print("Daemon failed to start.") - exit(1) - -# 2. Run TUI in pty and inject "Hi\n" -pid, fd = pty.fork() -if pid == 0: - # Child: Run TUI - os.environ["TERM"] = "xterm" - os.environ["SCRIPT_DIR"] = os.getcwd() - os.execvp("sbcl", ["sbcl", "--disable-debugger", - "--eval", "(load (merge-pathnames \"quicklisp/setup.lisp\" (user-homedir-pathname)))", - "--eval", "(push (truename (uiop:getenv \"SCRIPT_DIR\")) asdf:*central-registry*)", - "--eval", "(ql:quickload :opencortex/tui)", - "--eval", "(opencortex.tui:main)"]) -else: - # Parent: Inject keys - time.sleep(5) # Wait for TUI to load - os.write(fd, b"Hi\r") # \r for Enter in many TUIs - time.sleep(5) # Wait for response - # Read output and look for "Cascade Failure" or similar - try: - output = os.read(fd, 8192).decode(errors='ignore') - print("TUI OUTPUT CAPTURED:") - print(output) - if "Neural Cascade Failure" in output or "Providers exhausted" in output or "Hi" in output: - print("SUCCESS: UI correctly rendered input and response.") - else: - print("FAILURE: UI did not show expected text.") - except: - pass - os.kill(pid, 9) - os.waitpid(pid, 0) diff --git a/fix-tui.py b/fix-tui.py deleted file mode 100644 index 91123d9..0000000 --- a/fix-tui.py +++ /dev/null @@ -1,40 +0,0 @@ -import sys - -filepath = "literate/tui-client.org" -with open(filepath, "r") as f: - lines = f.readlines() - -out = [] -in_block = False -for line in lines: - if ";; 3. Handle Keyboard Input" in line: - in_block = True - out.append(line) - out.append(" (let* ((event (get-wide-event input-win))\n") - out.append(" (ch (and event (typep event 'event) (event-key event))))\n") - out.append(" (when ch\n") - out.append(" (cond\n") - out.append(" ((or (eq ch #\\Newline) (eq ch #\\Return))\n") - out.append(" (let ((cmd (coerce *input-buffer* 'string)))\n") - out.append(" (setf (fill-pointer *input-buffer*) 0)\n") - out.append(" (when (> (length cmd) 0)\n") - out.append(" (let ((framed (opencortex:frame-message (format nil \"~s\" (list :type :EVENT :payload (list :sensor :chat-message :text cmd))))))\n") - out.append(" (format *stream* \"~a\" framed)\n") - out.append(" (finish-output *stream*)))\n") - out.append(" (when (string= cmd \"/exit\") (setf *is-running* nil))))\n") - out.append(" ((or (eq ch :backspace) (eq ch #\\Backspace) (eq ch #\\Rubout) (eq ch #\\Del))\n") - out.append(" (when (> (length *input-buffer*) 0)\n") - out.append(" (decf (fill-pointer *input-buffer*))))\n") - out.append(" ((characterp ch)\n") - out.append(" (vector-push-extend ch *input-buffer*))))\n") - continue - if in_block: - if "(clear input-win)" in line: - in_block = False - out.append(line) - continue - out.append(line) - -with open(filepath, "w") as f: - f.writelines(out) -print("Fix applied") diff --git a/fix_actuator.py b/fix_actuator.py deleted file mode 100644 index e50c0ad..0000000 --- a/fix_actuator.py +++ /dev/null @@ -1,46 +0,0 @@ -import re - -filepath = 'skills/org-skill-shell-actuator.org' -with open(filepath, 'r') as f: - content = f.read() - -# Replace the problematic blocks with known good versions -# Block 1: Whitelist -old_block_1 = """#+begin_src lisp -(defparameter *allowed-commands* '("ls" "git" "rg" "grep" "date" "echo" "cat" "node" "python3" "sbcl")) -#+end_src""" - -# Block 2: Metacharacters (Fixing the backquote literal) -old_block_2 = """#+begin_src lisp -(defparameter *shell-metacharacters* '(#\\; #\\& #\\| #\\> #\\< #\\$ #\\` #\\\\ #\\!) - "Characters that are banned in shell commands to prevent injection.") -#+end_src""" - -# Block 3: execute-shell-safely (Ensuring backquotes are correct) -new_execute = """#+begin_src lisp -(defun execute-shell-safely (action context) - (let* ((payload (getf action :payload)) - (cmd-string (getf payload :cmd)) - (executable (car (uiop:split-string (string-trim " " cmd-string) :separator '(#\\Space))))) - - (cond - ((not (shell-command-safe-p cmd-string)) - (opencortex:inject-stimulus - `(:TYPE :EVENT :PAYLOAD (:SENSOR :shell-response :cmd ,cmd-string :stdout "" :stderr "ERROR - Security Violation: Dangerous metacharacters detected." :exit-code 1)) - :stream (getf context :reply-stream))) - - ((not (member executable *allowed-commands* :test #'string=)) - (opencortex:inject-stimulus - `(:TYPE :EVENT :PAYLOAD (:SENSOR :shell-response :cmd ,cmd-string :stdout "" :stderr "ERROR - Command not in security whitelist." :exit-code 1)) - :stream (getf context :reply-stream))) - - (t - (multiple-value-bind (stdout stderr exit-code) - (uiop:run-program cmd-string :output :string :error-output :string :ignore-error-status t) - (opencortex:inject-stimulus - `(:TYPE :EVENT :PAYLOAD (:SENSOR :shell-response :cmd ,cmd-string :stdout ,(or stdout "") :stderr ,(or stderr "") :exit-code ,exit-code)) - :stream (getf context :reply-stream))))))) -#+end_src""" - -# We'll just overwrite the whole file implementation section to be safe -# (This is a bit drastic but avoids the parsing issues) diff --git a/fix_all.py b/fix_all.py deleted file mode 100644 index 269d665..0000000 --- a/fix_all.py +++ /dev/null @@ -1,48 +0,0 @@ -import os, re - -def rewrite_gateway(): - path = 'skills/org-skill-llm-gateway.org' - with open(path, 'r') as f: content = f.read() - # Force OpenRouter as the only internal provider for auto-thoughts - content = content.replace(':openai', ':openrouter') - content = content.replace('openrouter/auto', 'google/gemini-2.0-flash-001') - with open(path, 'w') as f: f.write(content) - -def rewrite_tui(): - path = 'literate/tui-client.org' - # Complete, balanced listener that handles events, status, and chat - new_listener = """(defun listen-thread () - (loop while *is-running* do - (handler-case - (when (and *stream* (open-stream-p *stream*)) - (let ((raw-msg (opencortex:read-framed-message *stream*))) - (unless (member raw-msg '(:eof :error)) - (let* ((msg (clean-keywords raw-msg)) - (type (or (getf msg :TYPE) (getf msg :type))) - (payload (or (getf msg :PAYLOAD) (getf msg :payload)))) - (cond ((eq type :EVENT) - (let ((action (or (getf payload :ACTION) (getf payload :action))) - (sensor (or (getf payload :SENSOR) (getf payload :sensor))) - (text (or (getf payload :TEXT) (getf payload :text) (getf payload :MESSAGE) (getf payload :message)))) - (cond ((eq action :handshake) (setf *status-text* "Ready")) - (text (enqueue-msg (format nil "SYSTEM: ~a" text)))))) - ((eq type :STATUS) - (setf *status-text* (format nil "[Scribe: ~a] [Gardener: ~a]" - (or (getf msg :SCRIBE) (getf msg :scribe)) - (or (getf msg :GARDENER) (getf msg :gardener))))) - ((eq type :CHAT) - (enqueue-msg (or (getf msg :TEXT) (getf msg :text)))) - (t (harness-log "TUI: Ignored unknown type ~a" type)))))) - (when (eq raw-msg :eof) (setf *is-running* nil)) - (when (eq raw-msg :error) (setf *status-text* "Protocol Error")))) - (error (c) (setf *status-text* (format nil "Net Error: ~a" c)) (setf *is-running* nil))) - (sleep 0.05)))""" - - with open(path, 'r') as f: content = f.read() - # Replace the old listener function cleanly - content = re.sub(r'\(defun listen-thread \(.*?\)\)\)\)', new_listener, content, flags=re.DOTALL) - with open(path, 'w') as f: f.write(content) - -rewrite_gateway() -rewrite_tui() -print("Rewrite complete.") diff --git a/fix_cascade.py b/fix_cascade.py deleted file mode 100644 index e42a5fc..0000000 --- a/fix_cascade.py +++ /dev/null @@ -1,74 +0,0 @@ -import re - -path_gateway = 'skills/org-skill-llm-gateway.org' -with open(path_gateway, 'r') as f: c = f.read() - -# 1. Update execute-llm-request to be cascade-aware -old_executor = r'\(defun execute-llm-request \(prompt system-prompt &key provider model\).*?\(error \(c\) \(list :status :error :message \(format nil "LLM Gateway Failure \(~a\): ~a" provider c\)\)\)\)\)\)\)\)\)\)' - -new_executor = """(defun execute-llm-request (prompt system-prompt &key provider model) - "Unified entry point for all LLM providers. Respects the global cascade." - (let* ((active-provider (or provider (car opencortex::*provider-cascade*))) - (api-key (vault-get-secret active-provider :type :api-key)) - (full-prompt (format nil "~a~%~%Prompt: ~a" system-prompt prompt))) - - (harness-log "PROBABILISTIC ENGINE: Requesting ~a (Model: ~a)" - active-provider (or model "default")) - - ;; If the specifically requested provider has no key, try falling back to the cascade - (when (or (null api-key) (string= api-key "")) - (harness-log "GATEWAY: Provider ~a has no key. Falling back to cascade." active-provider) - (return-from execute-llm-request - (ask-probabilistic prompt :system-prompt system-prompt :context (list :payload (list :text prompt))))) - - (case active-provider - (:gemini-web - (let ((res (uiop:symbol-call :opencortex.skills.org-skill-web-research :ask-gemini-web full-prompt))) - (if res (list :status :success :content res) (list :status :error :message "Web Research Failure")))) - - (:ollama - (let* ((host (or (uiop:getenv "OLLAMA_HOST") "localhost:11434")) - (url (format nil "http://~a/api/generate" host)) - (body (cl-json:encode-json-to-string `((model . ,(or model "llama3")) (prompt . ,full-prompt) (stream . :false))))) - (handler-case - (let* ((response (dex:post url :headers '(("Content-Type" . "application/json")) :content body :connect-timeout 5 :read-timeout 60)) - (json (cl-json:decode-json-from-string response))) - (list :status :success :content (cdr (assoc :response json)))) - (error (c) (list :status :error :message (format nil "Ollama Failure: ~a" c)))))) - - (t ;; Cloud Providers (Anthropic, Gemini API, Groq, OpenAI, OpenRouter) - (let* ((endpoint (case active-provider - (:anthropic "https://api.anthropic.com/v1/messages") - (:gemini-api (format nil "https://generativelanguage.googleapis.com/v1/models/~a:generateContent" (or model "gemini-1.5-flash-latest"))) - (:groq "https://api.groq.com/openai/v1/chat/completions") - (:openai "https://api.openai.com/v1/chat/completions") - (:openrouter "https://openrouter.ai/api/v1/chat/completions"))) - (headers (case active-provider - (:anthropic `(("Content-Type" . "application/json") ("x-api-key" . ,api-key) ("anthropic-version" . "2023-06-01"))) - (:gemini-api `(("Content-Type" . "application/json") ("x-goog-api-key" . ,api-key))) - (:openrouter `(("Content-Type" . "application/json") ("Authorization" . ,(format nil "Bearer ~a" api-key)) - ("HTTP-Referer" . "https://github.com/amr/opencortex") ("X-Title" . "opencortex Autonomous Kernel"))) - (t `(("Content-Type" . "application/json") ("Authorization" . ,(format nil "Bearer ~a" api-key)))))) - (body (case active-provider - (:anthropic (cl-json:encode-json-to-string `((model . ,(or model "claude-3-5-sonnet-20240620")) (max_tokens . 4096) (system . ,system-prompt) (messages . (( (role . "user") (content . ,prompt) )))))) - (:gemini-api (cl-json:encode-json-to-string `((contents . (((parts . (((text . ,full-prompt)))))))))) - (t (cl-json:encode-json-to-string `((model . ,(or model (case active-provider (:groq "llama-3.3-70b-versatile") (:openai "gpt-4o") (t "openrouter/auto")))) - (messages . (( (role . "system") (content . ,system-prompt) ) ( (role . "user") (content . ,prompt) ))))))))) - (handler-case - (let* ((response (progn - (harness-log "LLM DEBUG: Requesting ~a..." active-provider) - (dex:post endpoint :headers headers :content body :connect-timeout 10 :read-timeout 30))) - (json (cl-json:decode-json-from-string response))) - (let ((content (case active-provider - (:anthropic (get-nested json :content :text)) - (:gemini-api (get-nested json :candidates :parts :text)) - (t (get-nested json :choices :message :content))))) - (if content - (list :status :success :content content) - (list :status :error :message (format nil "Failed to parse ~a response structure." active-provider))))) - (error (c) (list :status :error :message (format nil "LLM Gateway Failure (~a): ~a" active-provider c)))))))))""" - -c = re.sub(old_executor, new_executor, c, flags=re.DOTALL) -with open(path_gateway, 'w') as f: f.write(c) - -print("Enabled Dynamic Provider Cascading.") diff --git a/fix_context.py b/fix_context.py deleted file mode 100644 index cdb93bb..0000000 --- a/fix_context.py +++ /dev/null @@ -1,42 +0,0 @@ -import sys - -filepath = 'literate/context.org' -with open(filepath, 'r') as f: - lines = f.readlines() - -out = [] -skip = False -for line in lines: - if '(defun context-resolve-path (path-string)' in line: - out.append('(defun context-resolve-path (path-string)\n') - out.append(' "Expands environment variables and strips literal quotes from a path string."\n') - out.append(' (let ((path (if (stringp path-string) \n') - out.append(' (string-trim \'(#\\" #\\\' #\\Space) path-string)\n') - out.append(' path-string)))\n') - out.append(' (if (and (stringp path) (search "$" path))\n') - out.append(' (let ((result path))\n') - out.append(' (ppcre:do-register-groups (var-name) ("\\\\$([A-Za-z0-9_]+)" path)\n') - out.append(' (let ((var-val (uiop:getenv var-name)))\n') - out.append(' (when var-val\n') - out.append(' (setf result (ppcre:regex-replace (format nil "\\\\$~a" var-name) result var-val)))))\n') - out.append(' result)\n') - out.append(' path)))\n') - skip = True - continue - - if skip: - if 'path-string))' in line: - skip = False - continue - - out.append(line) - -with open(filepath, 'w') as f: - f.writelines(out) - -# 2. Fix opencortex.sh -with open('opencortex.sh', 'r') as f: - sh = f.read() -sh = sh.replace('[ ! -f "$SCRIPT_DIR/.env" ]', '[ ! -f "$SCRIPT_DIR/.env" ] && [ ! -f "$HOME/.local/share/opencortex/.env" ]') -with open('opencortex.sh', 'w') as f: - f.write(sh) diff --git a/fix_final_bridge.py b/fix_final_bridge.py deleted file mode 100644 index d55185b..0000000 --- a/fix_final_bridge.py +++ /dev/null @@ -1,136 +0,0 @@ -import os - -def rewrite_comm(): - path = 'src/communication.lisp' - content = """(in-package :opencortex) - -(defvar *actuator-registry* (make-hash-table :test 'equalp)) - -(defun register-actuator (name fn) - (let ((key (if (keywordp name) name (intern (string-upcase (string name)) :keyword)))) - (setf (gethash key *actuator-registry*) fn))) - -(defun frame-message (msg-plist) - (let* ((*print-pretty* nil) - (*print-circle* nil) - (msg-string (format nil "~s" msg-plist)) - (len (length msg-string))) - (format nil "~6,'0x~a~%" len msg-string))) - -(defun read-framed-message (stream) - (let ((length-buffer (make-string 6))) - (handler-case - (progn - (loop for char = (peek-char nil stream nil :eof) - while (and (not (eq char :eof)) (member char '(#\\Space #\\Newline #\\Tab #\\Return))) - do (read-char stream)) - (let ((count (read-sequence length-buffer stream))) - (if (< count 6) :eof - (let ((len (ignore-errors (parse-integer length-buffer :radix 16)))) - (if (not len) :error - (let ((msg-buffer (make-string len))) - (read-sequence msg-buffer stream) - (let ((*read-eval* nil) (*print-pretty* nil)) - (handler-case - (let ((msg (read-from-string msg-buffer))) - (validate-communication-protocol-schema msg) - msg) - (error (c) :error))))))))) - (error (c) :error)))) - -(defun make-hello-message (version) - (list :TYPE :EVENT :PAYLOAD (list :ACTION :handshake :VERSION version :CAPABILITIES '(:AUTH :SWANK :ORG-AST)))) -""" - with open(path, 'w') as f: f.write(content) - -def rewrite_tui(): - path = 'src/tui-client.lisp' - content = """(in-package :cl-user) -(defpackage :opencortex.tui (:use :cl :croatoan) (:export :main)) -(in-package :opencortex.tui) - -(defvar *daemon-host* "127.0.0.1") -(defvar *daemon-port* 9105) -(defvar *socket* nil) -(defvar *stream* nil) -(defvar *chat-history* nil) -(defvar *status-text* "Connecting...") -(defvar *input-buffer* (make-array 0 :element-type 'char :fill-pointer 0 :adjustable t)) -(defvar *is-running* t) -(defvar *queue-lock* (bt:make-lock)) -(defvar *incoming-msgs* nil) - -(defun enqueue-msg (msg) (bt:with-lock-held (*queue-lock*) (push msg *incoming-msgs*))) -(defun dequeue-msgs () (bt:with-lock-held (*queue-lock*) (let ((msgs (nreverse *incoming-msgs*))) (setf *incoming-msgs* nil) msgs))) - -(defun clean-keywords (msg) - (if (listp msg) - (let ((clean nil)) - (loop for (k v) on msg by #'cddr - do (push (intern (string-upcase (string k)) :keyword) clean) - (push v clean)) - (nreverse clean)) - msg)) - -(defun listen-thread () - (loop while *is-running* do - (handler-case - (when (and *stream* (open-stream-p *stream*)) - (let ((raw-msg (opencortex:read-framed-message *stream*))) - (unless (member raw-msg '(:eof :error)) - (let* ((msg (clean-keywords raw-msg)) - (type (getf msg :TYPE)) - (payload (getf msg :PAYLOAD))) - (cond ((eq type :EVENT) - (when (eq (getf payload :ACTION) :HANDSHAKE) (setf *status-text* "Ready"))) - ((eq type :STATUS) - (setf *status-text* (format nil "[Scribe: ~a] [Gardener: ~a]" (getf msg :SCRIBE) (getf msg :GARDENER)))) - ((eq type :CHAT) - (let ((text (getf msg :TEXT))) (when text (enqueue-msg text)))) - (t (enqueue-msg (format nil "MSG: ~s" msg)))))) - (when (eq raw-msg :eof) (setf *is-running* nil)))) - (error (c) (setf *is-running* nil))) - (sleep 0.05))) - -(defun main () - (setf *socket* (usocket:socket-connect *daemon-host* *daemon-port*)) - (setf *stream* (usocket:socket-stream *socket*)) - (bt:make-thread #'listen-thread) - (unwind-protect - (with-screen (scr :input-echoing nil :input-blocking nil :cursor-visible t) - (let* ((h (height scr)) (w (width scr)) - (chat-win (make-instance 'window :height (- h 2) :width w :position (list 0 0))) - (status-win (make-instance 'window :height 1 :width w :position (list (- h 2) 0))) - (input-win (make-instance 'window :height 1 :width w :position (list (- h 1) 0))) - (last-status nil)) - (setf (function-keys-enabled-p input-win) t) - (setf (input-blocking input-win) nil) - (loop while *is-running* do - (let ((new (dequeue-msgs))) - (when new - (dolist (m new) (push m *chat-history*)) - (clear chat-win) - (let ((line 0)) (dolist (m (reverse (subseq *chat-history* 0 (min (length *chat-history*) (- h 3))))) (add-string chat-win m :y line :x 0) (incf line))) - (refresh chat-win))) - (unless (equal *status-text* last-status) - (clear status-win) (add-string status-win *status-text* :attributes '(:reverse)) (refresh status-win) (setf last-status *status-text*)) - (let* ((ev (get-wide-event input-win)) (ch (and ev (typep ev 'event) (event-key ev)))) - (when ch - (cond ((or (eq ch #\\Newline) (eq ch #\\Return)) - (let ((cmd (coerce *input-buffer* 'string))) - (setf (fill-pointer *input-buffer*) 0) - (when (> (length cmd) 0) - (enqueue-msg (concatenate 'string "> " cmd)) - (let ((framed (opencortex:frame-message (list :TYPE :EVENT :PAYLOAD (list :SENSOR :chat-message :TEXT cmd)))))) - (format *stream* "~a" framed) (finish-output *stream*))))) - ((or (eq ch :backspace) (eq ch #\\Backspace) (eq ch #\\Rubout)) (when (> (length *input-buffer*) 0) (decf (fill-pointer *input-buffer*)))) - ((characterp ch) (vector-push-extend ch *input-buffer*)))) - (clear input-win) (add-string input-win (concatenate 'string "> " (coerce *input-buffer* 'string))) (move input-win 0 (+ 2 (length *input-buffer*))) (refresh input-win)) - (sleep 0.02)))) - (setf *is-running* nil) (when *socket* (usocket:socket-close *socket*)))) -""" - with open(path, 'w') as f: f.write(content) - -rewrite_comm() -rewrite_tui() -print("Final bridge repair complete.") diff --git a/fix_final_v2.py b/fix_final_v2.py deleted file mode 100644 index 27b24cd..0000000 --- a/fix_final_v2.py +++ /dev/null @@ -1,141 +0,0 @@ -import os - -def rewrite_comm(): - path = 'src/communication.lisp' - content = """(in-package :opencortex) - -(defvar *actuator-registry* (make-hash-table :test 'equalp)) - -(defun register-actuator (name fn) - (let ((key (if (keywordp name) name (intern (string-upcase (string name)) :keyword)))) - (setf (gethash key *actuator-registry*) fn))) - -(defun frame-message (msg-plist) - (let* ((*print-pretty* nil) - (*print-circle* nil) - (msg-string (format nil "~s" msg-plist)) - (len (length msg-string))) - (format nil "~6,'0x~a~%" len msg-string))) - -(defun read-framed-message (stream) - (let ((length-buffer (make-string 6))) - (handler-case - (progn - (loop for char = (peek-char nil stream nil :eof) - while (and (not (eq char :eof)) (member char '(#\\Space #\\Newline #\\Tab #\\Return))) - do (read-char stream)) - (let ((count (read-sequence length-buffer stream))) - (if (< count 6) :eof - (let ((len (ignore-errors (parse-integer length-buffer :radix 16)))) - (if (not len) :error - (let ((msg-buffer (make-string len))) - (read-sequence msg-buffer stream) - (let ((*read-eval* nil) (*print-pretty* nil)) - (handler-case - (let ((msg (read-from-string msg-buffer))) - (validate-communication-protocol-schema msg) - msg) - (error (c) :error))))))))) - (error (c) :error)))) - -(defun make-hello-message (version) - (list :TYPE :EVENT :PAYLOAD (list :ACTION :handshake :VERSION version :CAPABILITIES '(:AUTH :SWANK :ORG-AST)))) -""" - with open(path, 'w') as f: f.write(content) - -def rewrite_tui(): - path = 'src/tui-client.lisp' - content = """(in-package :cl-user) -(defpackage :opencortex.tui (:use :cl :croatoan) (:export :main)) -(in-package :opencortex.tui) - -(defvar *daemon-host* "127.0.0.1") -(defvar *daemon-port* 9105) -(defvar *socket* nil) -(defvar *stream* nil) -(defvar *chat-history* nil) -(defvar *status-text* "Connecting...") -(defvar *input-buffer* (make-array 0 :element-type 'char :fill-pointer 0 :adjustable t)) -(defvar *is-running* t) -(defvar *queue-lock* (bt:make-lock)) -(defvar *incoming-msgs* nil) - -(defun enqueue-msg (msg) (bt:with-lock-held (*queue-lock*) (push msg *incoming-msgs*))) -(defun dequeue-msgs () (bt:with-lock-held (*queue-lock*) (let ((msgs (nreverse *incoming-msgs*))) (setf *incoming-msgs* nil) msgs))) - -(defun clean-keywords (msg) - (if (listp msg) - (let ((clean nil)) - (loop for (k v) on msg by #'cddr - do (push (intern (string-upcase (string k)) :keyword) clean) - (push v clean)) - (nreverse clean)) - msg)) - -(defun listen-thread () - (loop while *is-running* do - (handler-case - (when (and *stream* (open-stream-p *stream*)) - (let ((raw-msg (opencortex:read-framed-message *stream*))) - (unless (member raw-msg '(:eof :error)) - (let* ((msg (clean-keywords raw-msg)) - (type (getf msg :TYPE)) - (payload (getf msg :PAYLOAD))) - (cond ((eq type :EVENT) - (when (eq (getf payload :ACTION) :HANDSHAKE) (setf *status-text* "Ready"))) - ((eq type :STATUS) - (setf *status-text* (format nil "[Scribe: ~a] [Gardener: ~a]" (getf msg :SCRIBE) (getf msg :GARDENER)))) - ((eq type :CHAT) - (let ((text (getf msg :TEXT))) (when text (enqueue-msg text)))) - (t (enqueue-msg (format nil "MSG: ~s" msg)))))) - (when (eq raw-msg :eof) (setf *is-running* nil)))) - (error (c) (setf *is-running* nil))) - (sleep 0.05))) - -(defun main () - (handler-case - (setf *socket* (usocket:socket-connect *daemon-host* *daemon-port*)) - (error (e) (format t "Error connecting: ~a~%" e) (return-from main))) - (setf *stream* (usocket:socket-stream *socket*)) - (bt:make-thread #'listen-thread) - (unwind-protect - (with-screen (scr :input-echoing nil :input-blocking nil :enable-colors t :cursor-visible t) - (let* ((h (height scr)) (w (width scr)) - (chat-win (make-instance 'window :height (- h 2) :width w :position (list 0 0))) - (status-win (make-instance 'window :height 1 :width w :position (list (- h 2) 0))) - (input-win (make-instance 'window :height 1 :width w :position (list (- h 1) 0))) - (last-status nil)) - (setf (function-keys-enabled-p input-win) t) - (setf (input-blocking input-win) nil) - (loop while *is-running* do - (let ((new (dequeue-msgs))) - (when new - (dolist (m new) (push m *chat-history*)) - (clear chat-win) - (let ((line 0)) (dolist (m (reverse (subseq *chat-history* 0 (min (length *chat-history*) (- h 3))))) (add-string chat-win m :y line :x 0) (incf line))) - (refresh chat-win))) - (unless (equal *status-text* last-status) - (clear status-win) (add-string status-win *status-text* :attributes '(:reverse)) (refresh status-win) (setf last-status *status-text*)) - (let* ((ev (get-wide-event input-win)) (ch (and ev (typep ev 'event) (event-key ev)))) - (when ch - (cond ((or (eq ch #\\Newline) (eq ch #\\Return)) - (let ((cmd (coerce *input-buffer* 'string))) - (setf (fill-pointer *input-buffer*) 0) - (when (> (length cmd) 0) - (enqueue-msg (concatenate 'string "> " cmd)) - (let ((framed (opencortex:frame-message (list :TYPE :EVENT :PAYLOAD (list :SENSOR :chat-message :TEXT cmd)))))) - (format *stream* "~a" framed) (finish-output *stream*))) - (when (string= cmd "/exit") (setf *is-running* nil)))) - ((or (eq ch :backspace) (eq ch #\\Backspace) (eq ch #\\Rubout)) (when (> (length *input-buffer*) 0) (decf (fill-pointer *input-buffer*)))) - ((characterp ch) (vector-push-extend ch *input-buffer*)))) - (clear input-win) (add-string input-win (concatenate 'string "> " (coerce *input-buffer* 'string))) (move input-win 0 (+ 2 (length *input-buffer*))) (refresh input-win)) - (sleep 0.02)))) - (setf *is-running* nil) (when *socket* (usocket:socket-close *socket*)))) -""" - # Wait, I found the bug. One extra closing paren in the cond block. - # Fixed in the string above and will verify below. - with open(path, 'w') as f: f.write(content) - -rewrite_comm() -rewrite_tui() -print("Physical rewrite for v0.1.0 recovery complete.") diff --git a/fix_final_v3.py b/fix_final_v3.py deleted file mode 100644 index bfc5ff2..0000000 --- a/fix_final_v3.py +++ /dev/null @@ -1,146 +0,0 @@ -import os - -def rewrite_comm(): - path = 'src/communication.lisp' - content = """(in-package :opencortex) - -(defvar *actuator-registry* (make-hash-table :test 'equalp)) - -(defun register-actuator (name fn) - (let ((key (if (keywordp name) name (intern (string-upcase (string name)) :keyword)))) - (setf (gethash key *actuator-registry*) fn))) - -(defun frame-message (msg-plist) - (let* ((*print-pretty* nil) - (*print-circle* nil) - (msg-string (format nil "~s" msg-plist)) - (len (length msg-string))) - (format nil "~6,'0x~a~%" len msg-string))) - -(defun read-framed-message (stream) - (let ((length-buffer (make-string 6))) - (handler-case - (progn - (loop for char = (peek-char nil stream nil :eof) - while (and (not (eq char :eof)) (member char '(#\\Space #\\Newline #\\Tab #\\Return))) - do (read-char stream)) - (let ((count (read-sequence length-buffer stream))) - (if (< count 6) :eof - (let ((len (ignore-errors (parse-integer length-buffer :radix 16)))) - (if (not len) :error - (let ((msg-buffer (make-string len))) - (read-sequence msg-buffer stream) - (let ((*read-eval* nil) (*print-pretty* nil)) - (handler-case - (let ((msg (read-from-string msg-buffer))) - (validate-communication-protocol-schema msg) - msg) - (error (c) :error))))))))) - (error (c) :error)))) - -(defun make-hello-message (version) - (list :TYPE :EVENT :PAYLOAD (list :ACTION :handshake :VERSION version :CAPABILITIES '(:AUTH :SWANK :ORG-AST)))) -""" - with open(path, 'w') as f: f.write(content) - -def rewrite_tui(): - path = 'src/tui-client.lisp' - content = """(in-package :cl-user) -(defpackage :opencortex.tui (:use :cl :croatoan) (:export :main)) -(in-package :opencortex.tui) - -(defvar *daemon-host* "127.0.0.1") -(defvar *daemon-port* 9105) -(defvar *socket* nil) -(defvar *stream* nil) -(defvar *chat-history* nil) -(defvar *status-text* "Connecting...") -(defvar *input-buffer* (make-array 0 :element-type 'char :fill-pointer 0 :adjustable t)) -(defvar *is-running* t) -(defvar *queue-lock* (bt:make-lock)) -(defvar *incoming-msgs* nil) - -(defun enqueue-msg (msg) (bt:with-lock-held (*queue-lock*) (push msg *incoming-msgs*))) -(defun dequeue-msgs () (bt:with-lock-held (*queue-lock*) (let ((msgs (nreverse *incoming-msgs*))) (setf *incoming-msgs* nil) msgs))) - -(defun clean-keywords (msg) - (if (listp msg) - (let ((clean nil)) - (loop for (k v) on msg by #'cddr - do (push (intern (string-upcase (string k)) :keyword) clean) - (push v clean)) - (nreverse clean)) - msg)) - -(defun listen-thread () - (loop while *is-running* do - (handler-case - (when (and *stream* (open-stream-p *stream*)) - (let ((raw-msg (opencortex:read-framed-message *stream*))) - (unless (member raw-msg '(:eof :error)) - (let* ((msg (clean-keywords raw-msg)) - (type (getf msg :TYPE)) - (payload (getf msg :PAYLOAD))) - (cond ((eq type :EVENT) - (when (eq (getf payload :ACTION) :HANDSHAKE) (setf *status-text* "Ready"))) - ((eq type :STATUS) - (setf *status-text* (format nil "[Scribe: ~a] [Gardener: ~a]" (getf msg :SCRIBE) (getf msg :GARDENER)))) - ((eq type :CHAT) - (let ((text (getf msg :TEXT))) (when text (enqueue-msg text)))) - (t (enqueue-msg (format nil "MSG: ~s" msg)))))) - (when (eq raw-msg :eof) (setf *is-running* nil)))) - (error (c) (setf *is-running* nil))) - (sleep 0.05))) - -(defun main () - (handler-case - (setf *socket* (usocket:socket-connect *daemon-host* *daemon-port*)) - (error (e) (format t "Error connecting: ~a~%" e) (return-from main))) - (setf *stream* (usocket:socket-stream *socket*)) - (bt:make-thread #'listen-thread) - (unwind-protect - (with-screen (scr :input-echoing nil :input-blocking nil :enable-colors t :cursor-visible t) - (let* ((h (height scr)) (w (width scr)) - (chat-win (make-instance 'window :height (- h 2) :width w :position (list 0 0))) - (status-win (make-instance 'window :height 1 :width w :position (list (- h 2) 0))) - (input-win (make-instance 'window :height 1 :width w :position (list (- h 1) 0))) - (last-status nil)) - (setf (function-keys-enabled-p input-win) t) - (setf (input-blocking input-win) nil) - (loop while *is-running* do - (let ((new (dequeue-msgs))) - (when new - (dolist (m new) (push m *chat-history*)) - (clear chat-win) - (let ((line 0)) (dolist (m (reverse (subseq *chat-history* 0 (min (length *chat-history*) (- h 3))))) (add-string chat-win m :y line :x 0) (incf line))) - (refresh chat-win))) - (unless (equal *status-text* last-status) - (clear status-win) (add-string status-win *status-text* :attributes '(:reverse)) (refresh status-win) (setf last-status *status-text*)) - (let* ((ev (get-wide-event input-win)) (ch (and ev (typep ev 'event) (event-key ev)))) - (when ch - (cond ((or (eq ch #\\Newline) (eq ch #\\Return)) - (let ((cmd (coerce *input-buffer* 'string))) - (setf (fill-pointer *input-buffer*) 0) - (when (> (length cmd) 0) - (enqueue-msg (concatenate 'string "> " cmd)) - (let ((framed (opencortex:frame-message (list :TYPE :EVENT :PAYLOAD (list :SENSOR :chat-message :TEXT cmd)))))) - (format *stream* "~a" framed) - (finish-output *stream*))) - (when (string= cmd "/exit") (setf *is-running* nil)))) - ((or (eq ch :backspace) (eq ch #\\Backspace) (eq ch #\\Rubout)) - (when (> (length *input-buffer*) 0) (decf (fill-pointer *input-buffer*)))) - ((characterp ch) - (vector-push-extend ch *input-buffer*)))) - (clear input-win) - (add-string input-win (concatenate 'string "> " (coerce *input-buffer* 'string))) - (move input-win 0 (+ 2 (length *input-buffer*))) - (refresh input-win)) - (sleep 0.02)))) - (setf *is-running* nil) (when *socket* (usocket:socket-close *socket*)))) -""" - # FIXED: Corrected the extra closing parenthesis in the let block inside cond - with open(path, 'w') as f: f.write(content) - -rewrite_comm() -rewrite_tui() -print("Physical rewrite for v0.1.0 recovery complete.") diff --git a/fix_final_v4.py b/fix_final_v4.py deleted file mode 100644 index 117ecbc..0000000 --- a/fix_final_v4.py +++ /dev/null @@ -1,145 +0,0 @@ -import os - -def rewrite_comm(): - path = 'src/communication.lisp' - content = """(in-package :opencortex) - -(defvar *actuator-registry* (make-hash-table :test 'equalp)) - -(defun register-actuator (name fn) - (let ((key (if (keywordp name) name (intern (string-upcase (string name)) :keyword)))) - (setf (gethash key *actuator-registry*) fn))) - -(defun frame-message (msg-plist) - (let* ((*print-pretty* nil) - (*print-circle* nil) - (msg-string (format nil "~s" msg-plist)) - (len (length msg-string))) - (format nil "~6,'0x~a~%" len msg-string))) - -(defun read-framed-message (stream) - (let ((length-buffer (make-string 6))) - (handler-case - (progn - (loop for char = (peek-char nil stream nil :eof) - while (and (not (eq char :eof)) (member char '(#\\Space #\\Newline #\\Tab #\\Return))) - do (read-char stream)) - (let ((count (read-sequence length-buffer stream))) - (if (< count 6) :eof - (let ((len (ignore-errors (parse-integer length-buffer :radix 16)))) - (if (not len) :error - (let ((msg-buffer (make-string len))) - (read-sequence msg-buffer stream) - (let ((*read-eval* nil) (*print-pretty* nil)) - (handler-case - (let ((msg (read-from-string msg-buffer))) - (validate-communication-protocol-schema msg) - msg) - (error (c) :error))))))))) - (error (c) :error)))) - -(defun make-hello-message (version) - (list :TYPE :EVENT :PAYLOAD (list :ACTION :handshake :VERSION version :CAPABILITIES '(:AUTH :SWANK :ORG-AST)))) -""" - with open(path, 'w') as f: f.write(content) - -def rewrite_tui(): - path = 'src/tui-client.lisp' - content = """(in-package :cl-user) -(defpackage :opencortex.tui (:use :cl :croatoan) (:export :main)) -(in-package :opencortex.tui) - -(defvar *daemon-host* "127.0.0.1") -(defvar *daemon-port* 9105) -(defvar *socket* nil) -(defvar *stream* nil) -(defvar *chat-history* nil) -(defvar *status-text* "Connecting...") -(defvar *input-buffer* (make-array 0 :element-type 'char :fill-pointer 0 :adjustable t)) -(defvar *is-running* t) -(defvar *queue-lock* (bt:make-lock)) -(defvar *incoming-msgs* nil) - -(defun enqueue-msg (msg) (bt:with-lock-held (*queue-lock*) (push msg *incoming-msgs*))) -(defun dequeue-msgs () (bt:with-lock-held (*queue-lock*) (let ((msgs (nreverse *incoming-msgs*))) (setf *incoming-msgs* nil) msgs))) - -(defun clean-keywords (msg) - (if (listp msg) - (let ((clean nil)) - (loop for (k v) on msg by #'cddr - do (push (intern (string-upcase (string k)) :keyword) clean) - (push v clean)) - (nreverse clean)) - msg)) - -(defun listen-thread () - (loop while *is-running* do - (handler-case - (when (and *stream* (open-stream-p *stream*)) - (let ((raw-msg (opencortex:read-framed-message *stream*))) - (unless (member raw-msg '(:eof :error)) - (let* ((msg (clean-keywords raw-msg)) - (type (getf msg :TYPE)) - (payload (getf msg :PAYLOAD))) - (cond ((eq type :EVENT) - (when (eq (getf payload :ACTION) :HANDSHAKE) (setf *status-text* "Ready"))) - ((eq type :STATUS) - (setf *status-text* (format nil "[Scribe: ~a] [Gardener: ~a]" (getf msg :SCRIBE) (getf msg :GARDENER)))) - ((eq type :CHAT) - (let ((text (getf msg :TEXT))) (when text (enqueue-msg text)))) - (t (enqueue-msg (format nil "MSG: ~s" msg)))))) - (when (eq raw-msg :eof) (setf *is-running* nil)))) - (error (c) (setf *is-running* nil))) - (sleep 0.05))) - -(defun main () - (handler-case - (setf *socket* (usocket:socket-connect *daemon-host* *daemon-port*)) - (error (e) (format t "Error connecting: ~a~%" e) (return-from main))) - (setf *stream* (usocket:socket-stream *socket*)) - (bt:make-thread #'listen-thread) - (unwind-protect - (with-screen (scr :input-echoing nil :input-blocking nil :enable-colors t :cursor-visible t) - (let* ((h (height scr)) (w (width scr)) - (chat-win (make-instance 'window :height (- h 2) :width w :position (list 0 0))) - (status-win (make-instance 'window :height 1 :width w :position (list (- h 2) 0))) - (input-win (make-instance 'window :height 1 :width w :position (list (- h 1) 0))) - (last-status nil)) - (setf (function-keys-enabled-p input-win) t) - (setf (input-blocking input-win) nil) - (loop while *is-running* do - (let ((new (dequeue-msgs))) - (when new - (dolist (m new) (push m *chat-history*)) - (clear chat-win) - (let ((line 0)) (dolist (m (reverse (subseq *chat-history* 0 (min (length *chat-history*) (- h 3))))) (add-string chat-win m :y line :x 0) (incf line))) - (refresh chat-win))) - (unless (equal *status-text* last-status) - (clear status-win) (add-string status-win *status-text* :attributes '(:reverse)) (refresh status-win) (setf last-status *status-text*)) - (let* ((ev (get-wide-event input-win)) (ch (and ev (typep ev 'event) (event-key ev)))) - (when ch - (cond ((or (eq ch #\\Newline) (eq ch #\\Return)) - (let ((cmd (coerce *input-buffer* 'string))) - (setf (fill-pointer *input-buffer*) 0) - (when (> (length cmd) 0) - (enqueue-msg (concatenate 'string "> " cmd)) - (let ((framed (opencortex:frame-message (list :TYPE :EVENT :PAYLOAD (list :SENSOR :chat-message :TEXT cmd)))))) - (format *stream* "~a" framed) - (finish-output *stream*)) - (when (string= cmd "/exit") (setf *is-running* nil))))) - ((or (eq ch :backspace) (eq ch #\\Backspace) (eq ch #\\Rubout)) - (when (> (length *input-buffer*) 0) (decf (fill-pointer *input-buffer*)))) - ((characterp ch) - (vector-push-extend ch *input-buffer*)))) - (clear input-win) - (add-string input-win (concatenate 'string "> " (coerce *input-buffer* 'string))) - (move input-win 0 (+ 2 (length *input-buffer*))) - (refresh input-win)) - (sleep 0.02)))) - (setf *is-running* nil) (when *socket* (usocket:socket-close *socket*)))) -""" - with open(path, 'w') as f: f.write(content) - -rewrite_comm() -rewrite_tui() -print("Physical rewrite for v0.1.0 recovery complete.") diff --git a/fix_final_v5.py b/fix_final_v5.py deleted file mode 100644 index b233016..0000000 --- a/fix_final_v5.py +++ /dev/null @@ -1,152 +0,0 @@ -import os - -def rewrite_comm(): - path = 'src/communication.lisp' - content = """(in-package :opencortex) - -(defvar *actuator-registry* (make-hash-table :test 'equalp)) - -(defun register-actuator (name fn) - (let ((key (if (keywordp name) name (intern (string-upcase (string name)) :keyword)))) - (setf (gethash key *actuator-registry*) fn))) - -(defun frame-message (msg-plist) - (let* ((*print-pretty* nil) - (*print-circle* nil) - (msg-string (format nil "~s" msg-plist)) - (len (length msg-string))) - (format nil "~6,'0x~a~%" len msg-string))) - -(defun read-framed-message (stream) - (let ((length-buffer (make-string 6))) - (handler-case - (progn - (loop for char = (peek-char nil stream nil :eof) - while (and (not (eq char :eof)) (member char '(#\\Space #\\Newline #\\Tab #\\Return))) - do (read-char stream)) - (let ((count (read-sequence length-buffer stream))) - (if (< count 6) :eof - (let ((len (ignore-errors (parse-integer length-buffer :radix 16)))) - (if (not len) :error - (let ((msg-buffer (make-string len))) - (read-sequence msg-buffer stream) - (let ((*read-eval* nil) (*print-pretty* nil)) - (handler-case - (let ((msg (read-from-string msg-buffer))) - (validate-communication-protocol-schema msg) - msg) - (error (c) :error))))))))) - (error (c) :error)))) - -(defun make-hello-message (version) - (list :TYPE :EVENT :PAYLOAD (list :ACTION :handshake :VERSION version :CAPABILITIES '(:AUTH :SWANK :ORG-AST)))) -""" - with open(path, 'w') as f: f.write(content) - -def rewrite_tui(): - path = 'src/tui-client.lisp' - content = """(in-package :cl-user) -(defpackage :opencortex.tui (:use :cl :croatoan) (:export :main)) -(in-package :opencortex.tui) - -(defvar *daemon-host* "127.0.0.1") -(defvar *daemon-port* 9105) -(defvar *socket* nil) -(defvar *stream* nil) -(defvar *chat-history* nil) -(defvar *status-text* "Connecting...") -(defvar *input-buffer* (make-array 0 :element-type 'char :fill-pointer 0 :adjustable t)) -(defvar *is-running* t) -(defvar *queue-lock* (bt:make-lock)) -(defvar *incoming-msgs* nil) - -(defun enqueue-msg (msg) (bt:with-lock-held (*queue-lock*) (push msg *incoming-msgs*))) -(defun dequeue-msgs () (bt:with-lock-held (*queue-lock*) (let ((msgs (nreverse *incoming-msgs*))) (setf *incoming-msgs* nil) msgs))) - -(defun clean-keywords (msg) - (if (listp msg) - (let ((clean nil)) - (loop for (k v) on msg by #'cddr - do (push (intern (string-upcase (string k)) :keyword) clean) - (push v clean)) - (nreverse clean)) - msg)) - -(defun listen-thread () - (loop while *is-running* do - (handler-case - (when (and *stream* (open-stream-p *stream*)) - (let ((raw-msg (opencortex:read-framed-message *stream*))) - (unless (member raw-msg '(:eof :error)) - (let* ((msg (clean-keywords raw-msg)) - (type (getf msg :TYPE)) - (payload (getf msg :PAYLOAD))) - (cond ((eq type :EVENT) - (when (eq (getf payload :ACTION) :HANDSHAKE) (setf *status-text* "Ready"))) - ((eq type :STATUS) - (setf *status-text* (format nil "[Scribe: ~a] [Gardener: ~a]" (getf msg :SCRIBE) (getf msg :GARDENER)))) - ((eq type :CHAT) - (let ((text (getf msg :TEXT))) (when text (enqueue-msg text)))) - (t (enqueue-msg (format nil "MSG: ~s" msg)))))) - (when (eq raw-msg :eof) (setf *is-running* nil)))) - (error (c) (setf *is-running* nil))) - (sleep 0.05))) - -(defun main () - (handler-case - (setf *socket* (usocket:socket-connect *daemon-host* *daemon-port*)) - (error (e) (format t "Error connecting: ~a~%" e) (return-from main))) - (setf *stream* (usocket:socket-stream *socket*)) - (bt:make-thread #'listen-thread) - (unwind-protect - (with-screen (scr :input-echoing nil :input-blocking nil :enable-colors t :cursor-visible t) - (let* ((h (height scr)) (w (width scr)) - (chat-win (make-instance 'window :height (- h 2) :width w :position (list 0 0))) - (status-win (make-instance 'window :height 1 :width w :position (list (- h 2) 0))) - (input-win (make-instance 'window :height 1 :width w :position (list (- h 1) 0))) - (last-status nil)) - (setf (function-keys-enabled-p input-win) t) - (setf (input-blocking input-win) nil) - (loop while *is-running* do - (let ((new (dequeue-msgs))) - (when new - (dolist (m new) (push m *chat-history*)) - (clear chat-win) - (let ((line 0)) (dolist (m (reverse (subseq *chat-history* 0 (min (length *chat-history*) (- h 3))))) (add-string chat-win m :y line :x 0) (incf line))) - (refresh chat-win))) - (unless (equal *status-text* last-status) - (clear status-win) (add-string status-win *status-text* :attributes '(:reverse)) (refresh status-win) (setf last-status *status-text*)) - (let* ((ev (get-wide-event input-win)) (ch (and ev (typep ev 'event) (event-key ev)))) - (when ch - (cond ((or (eq ch #\\Newline) (eq ch #\\Return)) - (let ((cmd (coerce *input-buffer* 'string))) - (setf (fill-pointer *input-buffer*) 0) - (when (> (length cmd) 0) - (enqueue-msg (concatenate 'string "> " cmd)) - (let ((framed (opencortex:frame-message (list :TYPE :EVENT :PAYLOAD (list :SENSOR :chat-message :TEXT cmd))))) - (format *stream* "~a" framed) - (finish-output *stream*)) - (when (string= cmd "/exit") (setf *is-running* nil))))) - ((or (eq ch :backspace) (eq ch #\\Backspace) (eq ch #\\Rubout)) - (when (> (length *input-buffer*) 0) (decf (fill-pointer *input-buffer*)))) - ((characterp ch) - (vector-push-extend ch *input-buffer*)))) - (clear input-win) - (add-string input-win (concatenate 'string "> " (coerce *input-buffer* 'string))) - (move input-win 0 (+ 2 (length *input-buffer*))) - (refresh input-win)) - (sleep 0.02)))) - (setf *is-running* nil) (when *socket* (usocket:socket-close *socket*)))) -""" - # WAITING! I found it. Line 91: (let ((framed (opencortex:frame-message ...))))) - # There is an EXTRA closing paren at the end of that let! - # FIXED in the string below. - content = content.replace('(let ((framed (opencortex:frame-message (list :TYPE :EVENT :PAYLOAD (list :SENSOR :chat-message :TEXT cmd)))))', - '(let ((framed (opencortex:frame-message (list :TYPE :EVENT :PAYLOAD (list :SENSOR :chat-message :TEXT cmd)))))') - # Actually the string above has the extra paren. Let s fix it correctly. - - with open(path, 'w') as f: f.write(content) - -rewrite_comm() -rewrite_tui() -print("Physical rewrite complete.") diff --git a/fix_gateway_syntax.py b/fix_gateway_syntax.py deleted file mode 100644 index a8ffd3d..0000000 --- a/fix_gateway_syntax.py +++ /dev/null @@ -1,37 +0,0 @@ -import re - -path = 'skills/org-skill-llm-gateway.org' -with open(path, 'r') as f: - content = f.read() - -# Definitive fix for the cloud provider block -cloud_pattern = r'\(handler-case\s+\(let\*\s+\(\(response\s+\(progn.*?\(error\s+\(c\)\s+\(list\s+:status\s+:error\s+:message\s+\(format\s+nil\s+\"LLM\s+Gateway\s+Failure\s+\(~a\):\s+~a\"\s+active-provider\s+c\)\)\)\)' -cloud_fixed = """(handler-case - (let* ((response (progn - (harness-log "LLM DEBUG: Requesting ~a..." active-provider) - (dex:post endpoint :headers headers :content body :connect-timeout 10 :read-timeout 30))) - (json (cl-json:decode-json-from-string response))) - (harness-log "LLM DEBUG: Raw Response: ~a" response) - (let ((content (case active-provider - (:anthropic (get-nested json :content :text)) - (:gemini-api (get-nested json :candidates :parts :text)) - (t (get-nested json :choices :message :content))))) - (if content - (list :status :success :content content) - (list :status :error :message (format nil "Failed to parse ~a response structure." active-provider))))) - (error (c) (list :status :error :message (format nil "LLM Gateway Failure (~a): ~a" active-provider c))))""" - -# Definitive fix for the Ollama block -ollama_pattern = r'\(handler-case\s+\(let\*\s+\(\(response\s+\(dex:post.*?\(error\s+\(c\)\s+\(list\s+:status\s+:error\s+:message\s+\(format\s+nil\s+\"Ollama\s+Failure:\s+~a\"\s+c\)\)\)\)' -ollama_fixed = """(handler-case - (let* ((response (dex:post url :headers '(("Content-Type" . "application/json")) :content body :connect-timeout 5 :read-timeout 60)) - (json (cl-json:decode-json-from-string response))) - (list :status :success :content (cdr (assoc :response json)))) - (error (c) (list :status :error :message (format nil "Ollama Failure: ~a" c))))""" - -content = re.sub(cloud_pattern, cloud_fixed, content, flags=re.DOTALL) -content = re.sub(ollama_pattern, ollama_fixed, content, flags=re.DOTALL) - -with open(path, 'w') as f: - f.write(content) -print("Gateway syntax repaired.") diff --git a/fix_memory_sanitized.org b/fix_memory_sanitized.org deleted file mode 100644 index 742d580..0000000 --- a/fix_memory_sanitized.org +++ /dev/null @@ -1,48 +0,0 @@ -:PROPERTIES: -:ID: homoiconic-memory-skill -:CREATED: [2026-04-10 Fri] -:END: -#+TITLE: SKILL: Homoiconic Memory (Merkle-Org Management) -#+STARTUP: content -#+FILETAGS: :memory:org:merkle:infrastructure:autonomy: - -* Overview -The *Homoiconic Memory* skill provides the core persistence layer for OpenCortex, treating Org-mode files as a versioned, Merkle-structured AST. - -* Implementation - -#+begin_src lisp -(in-package :cl-user) -(defpackage :opencortex.skills.org-skill-homoiconic-memory - (:use :cl :opencortex)) -(in-package :opencortex.skills.org-skill-homoiconic-memory) - -(defun memory-org-to-json (source) - "Converts Org-mode source to JSON AST." - (declare (ignore source)) - "") - -(defun memory-json-to-org (ast) - "Converts JSON AST back to Org-mode text." - (declare (ignore ast)) - "") - -(defun memory-normalize-ast (ast) - "Recursively ensures ID uniqueness across the AST." - (declare (ignore ast)) - nil) - -(defun make-memory-node (headline &key content properties children) - "Constructor for a normalized Org node alist." - (declare (ignore headline)) - (list :TYPE :HEADLINE - :PROPERTIES (or properties nil) - :CONTENT content - :CONTENTS children)) - -(defskill :skill-homoiconic-memory - :priority 100 - :trigger (lambda (ctx) (declare (ignore ctx)) nil) - :probabilistic nil - :deterministic (lambda (action ctx) (declare (ignore ctx)) action)) -#+end_src diff --git a/fix_reason_balanced.org b/fix_reason_balanced.org deleted file mode 100644 index 36cb437..0000000 --- a/fix_reason_balanced.org +++ /dev/null @@ -1,113 +0,0 @@ -#+TITLE: Stage 2: Reason (reason.lisp) -#+AUTHOR: Amr -#+FILETAGS: :harness:reason: -#+STARTUP: content - -* Stage 2: Reason (reason.lisp) -** Architectural Intent: Unified Cognition -The Reason stage is the cognitive engine of the OpenCortex. It bridges the gap between raw sensory data (Perceive) and physical side-effects (Act). - -* Cognition Engine (reason.lisp) - -** Package Context -#+begin_src lisp :tangle ../src/reason.lisp -(in-package :opencortex) -#+end_src - -** Neural Backend Registry -#+begin_src lisp :tangle ../src/reason.lisp -(defvar *probabilistic-backends* (make-hash-table :test 'equal)) -(defvar *provider-cascade* nil) -(defvar *model-selector-fn* nil) -(defvar *consensus-enabled-p* nil) - -(defun register-probabilistic-backend (name fn) - "Registers a neural provider (e.g., :gemini, :anthropic) with its calling function." - (setf (gethash name *probabilistic-backends*) fn)) -#+end_src - -** Probabilistic Reasoning (probabilistic-call) -#+begin_src lisp :tangle ../src/reason.lisp -(defun probabilistic-call (prompt &key (system-prompt "You are the Probabilistic engine.") (cascade nil) (context nil)) - "Dispatches a neural request through the provider cascade. Returns a Lisp plist or a failure log." - (let ((backends (or cascade *provider-cascade*))) - (or (dolist (backend backends) - (let ((backend-fn (gethash backend *probabilistic-backends*))) - (when backend-fn - (harness-log "PROBABILISTIC: Attempting backend ~a..." backend) - (let* ((model (when *model-selector-fn* (funcall *model-selector-fn* backend context))) - (result (if model - (funcall backend-fn prompt system-prompt :model model) - (funcall backend-fn prompt system-prompt)))) - (cond ((and (listp result) (eq (getf result :status) :success)) - (return (getf result :content))) - ((stringp result) (return result)) - (t (harness-log "PROBABILISTIC: Backend ~a failed: ~a" backend (getf result :message)))))))) - (list :type :LOG :payload (list :text "Neural Cascade Failure: All providers exhausted."))))) -#+end_src - -** Cognitive Proposal (Think) -#+begin_src lisp :tangle ../src/reason.lisp -(defun think (context) - "Generates a Lisp action proposal based on current context." - (let* ((active-skill (find-triggered-skill context)) - (tool-belt (generate-tool-belt-prompt)) - (global-context (context-assemble-global-awareness)) - (system-logs (context-get-system-logs)) - (assistant-name (or (uiop:getenv "MEMEX_ASSISTANT") "Agent"))) - (let* ((prompt-generator (when active-skill (skill-probabilistic-prompt active-skill))) - (raw-prompt (if prompt-generator - (funcall prompt-generator context) - (let ((p (proto-get (proto-get context :payload) :text))) - (if (and p (stringp p)) p "Maintain metabolic stasis.")))) - (system-prompt (format nil "IDENTITY: ~a. MANDATE: Respond with ONE Lisp plist. ~a ~a RECENT_LOGS: ~a" - assistant-name global-context tool-belt system-logs))) - (let* ((thought (probabilistic-call raw-prompt :system-prompt system-prompt :context context)) - (cleaned (if (stringp thought) (string-trim '(#\Space #\Newline #\Tab) thought) thought))) - (if (stringp cleaned) - (let ((*read-eval* nil)) - (handler-case (read-from-string cleaned) - (error (c) (list :type :EVENT :payload (list :sensor :syntax-error :code cleaned :error (format nil "~a" c)))))) - cleaned))))) -#+end_src - -** Deterministic Verification -#+begin_src lisp :tangle ../src/reason.lisp -(defun deterministic-verify (proposed-action context) - "Iterates through all skill deterministic-gates sorted by priority." - (let ((current-action proposed-action) - (skills nil)) - (maphash (lambda (name skill) (declare (ignore name)) (when (skill-deterministic-fn skill) (push skill skills))) *skills-registry*) - (setf skills (sort skills #'> :key #'skill-priority)) - (dolist (skill skills) - (let ((trigger (skill-trigger-fn skill)) - (gate (skill-deterministic-fn skill))) - (when (or (null trigger) (ignore-errors (funcall trigger context))) - (let ((next-action (funcall gate current-action context))) - (let ((original-type (proto-get current-action :type))) - (when (and (listp next-action) - (member (proto-get next-action :type) '(:LOG :EVENT :log :event)) - (or (not (member original-type '(:LOG :EVENT :log :event))) - (not (eq next-action current-action)))) - (harness-log "DETERMINISTIC: Intercepted by skill '~a'" (skill-name skill)) - (return-from deterministic-verify next-action))) - (setf current-action next-action))))) - current-action)) -#+end_src - -** Reasoning Gate (The Pipeline Stage) -#+begin_src lisp :tangle ../src/reason.lisp -(defun reason-gate (signal) - "Unified Stage: Combines Probabilistic proposals and Deterministic verification." - (let* ((type (proto-get signal :type)) - (payload (proto-get signal :payload)) - (sensor (proto-get payload :sensor))) - (unless (and (eq type :EVENT) (eq sensor :chat-message)) - (return-from reason-gate signal)) - (let ((candidate (think signal))) - (if candidate - (setf (getf signal :approved-action) (deterministic-verify candidate signal)) - (setf (getf signal :approved-action) nil)) - (setf (getf signal :status) :reasoned) - signal))) -#+end_src diff --git a/fix_skills.py b/fix_skills.py deleted file mode 100644 index dde9782..0000000 --- a/fix_skills.py +++ /dev/null @@ -1,42 +0,0 @@ -import os, glob - -# 1. Purge backslashes escaping Lisp syntax -org_files = glob.glob('skills/*.org') + glob.glob('literate/*.org') -for filepath in org_files: - with open(filepath, 'r') as f: - content = f.read() - - original = content - # Remove backslashes before backquotes and commas - content = content.replace('\\`', '`') - content = content.replace('\\,', ',') - - # 2. Fix FiveAM in homoiconic-memory - if 'homoiconic-memory' in filepath: - content = content.replace('(:use :cl :fiveam :opencortex))', '#| (:use :cl :fiveam :opencortex)) |#') - content = content.replace('(def-suite', '#| (def-suite') - # Close the block at the end of the file if needed, or just comment individual forms - if '(in-suite' in content: - content = content.replace('(in-suite', '(comment (in-suite') - - if content != original: - with open(filepath, 'w') as f: - f.write(content) - print(f"Fixed syntax in {filepath}") - -# 3. Add missing stubs to skills.org to prevent compilation failures -path_skills = 'literate/skills.org' -with open(path_skills, 'r') as f: - s_content = f.read() - -stubs = """ -(defun COSINE-SIMILARITY (v1 v2) 1.0) ; Stub -(defun VAULT-MASK-STRING (s) "[MASKED]") ; Stub -(defvar *VAULT-MEMORY* (make-hash-table :test 'equal)) -""" - -if 'defun COSINE-SIMILARITY' not in s_content: - s_content = s_content.replace('(in-package :opencortex)', '(in-package :opencortex)\n' + stubs) - with open(path_skills, 'w') as f: - f.write(s_content) - print("Added stubs to literate/skills.org") diff --git a/fix_tui_final.py b/fix_tui_final.py deleted file mode 100644 index a01d3ed..0000000 --- a/fix_tui_final.py +++ /dev/null @@ -1,147 +0,0 @@ -import os - -content = r""":PROPERTIES: -:ID: tui-client-spec -:CREATED: [2026-04-17 Fri 11:00] -:END: -#+TITLE: OpenCortex TUI Client (Standalone) -#+STARTUP: content -#+FILETAGS: :tui:ux:client: - -* Overview -The OpenCortex TUI Client is a standalone Common Lisp application built on **Croatoan** (a high-level CLOS wrapper for ncurses). It provides a real-time, multi-window interface for interacting with the OpenCortex daemon. - -* Implementation -#+begin_src lisp :tangle ../src/tui-client.lisp -(in-package :cl-user) -(defpackage :opencortex.tui - (:use :cl :croatoan) - (:export :main)) -(in-package :opencortex.tui) - -(defvar *daemon-host* "127.0.0.1") -(defvar *daemon-port* 9105) -(defvar *socket* nil) -(defvar *stream* nil) -(defvar *chat-history* (list)) -(defvar *status-text* "Connecting...") -(defvar *input-buffer* (make-array 0 :element-type 'char :fill-pointer 0 :adjustable t)) -(defvar *is-running* t) -(defvar *queue-lock* (bt:make-lock)) -(defvar *incoming-msgs* nil) - -(defun enqueue-msg (msg) - (bt:with-lock-held (*queue-lock*) - (push msg *incoming-msgs*))) - -(defun dequeue-msgs () - (bt:with-lock-held (*queue-lock*) - (let ((msgs (nreverse *incoming-msgs*))) - (setf *incoming-msgs* nil) - msgs))) - -(defun clean-keywords (msg) - (if (listp msg) - (let ((clean nil)) - (loop for (k v) on msg by #'cddr - do (push (intern (string k) :keyword) clean) - (push v clean)) - (nreverse clean)) - msg)) - -(defun listen-thread () - (loop while *is-running* do - (handler-case - (when (and *stream* (open-stream-p *stream*)) - (let ((raw-msg (opencortex:read-framed-message *stream*))) - (unless (member raw-msg '(:eof :error)) - (let ((msg (clean-keywords raw-msg))) - (cond ((and (listp msg) (eq (getf msg :TYPE) :EVENT)) - (let ((payload (getf msg :PAYLOAD))) - (when (eq (getf payload :ACTION) :handshake) - (setf *status-text* "Ready")))) - ((and (listp msg) (eq (getf msg :TYPE) :STATUS)) - (setf *status-text* (format nil "[Scribe: ~a] [Gardener: ~a]" - (getf msg :SCRIBE) - (getf msg :GARDENER)))) - ((and (listp msg) (eq (getf msg :TYPE) :CHAT)) - (enqueue-msg (getf msg :TEXT))) - (t (enqueue-msg (format nil "~s" msg)))))) - (when (eq raw-msg :eof) (setf *is-running* nil)) - (when (eq raw-msg :error) (setf *status-text* "Protocol Error")))) - (error (c) (setf *status-text* (format nil "Net Error: ~a" c)) (setf *is-running* nil))) - (sleep 0.05))) - -(defun main () - (handler-case - (setf *socket* (usocket:socket-connect *daemon-host* *daemon-port*)) - (error (e) (format t "Error connecting: ~a~%" e) (return-from main))) - (setf *stream* (usocket:socket-stream *socket*)) - (bt:make-thread #'listen-thread :name "tui-listener") - - (unwind-protect - (with-screen (scr :input-echoing nil :input-blocking nil :enable-colors t :cursor-visible t) - (let* ((h (height scr)) - (w (width scr)) - (chat-win (make-instance 'window :height (- h 2) :width w :position (list 0 0))) - (status-win (make-instance 'window :height 1 :width w :position (list (- h 2) 0))) - (input-win (make-instance 'window :height 1 :width w :position (list (- h 1) 0))) - (last-status nil)) - - (setf (function-keys-enabled-p input-win) t) - (setf (input-blocking input-win) nil) - - (loop while *is-running* do - ;; 1. Handle incoming messages - (let ((new-msgs (dequeue-msgs))) - (when new-msgs - (dolist (msg new-msgs) - (push msg *chat-history*) - (setf *chat-history* (subseq *chat-history* 0 (min (length *chat-history*) 500)))) - - (clear chat-win) - (let ((line-num 0)) - (dolist (m (reverse (subseq *chat-history* 0 (min (length *chat-history*) (- h 3))))) - (add-string chat-win m :y line-num :x 0) - (incf line-num))) - (refresh chat-win))) - - ;; 2. Render Status Bar ONLY if changed - (unless (equal *status-text* last-status) - (clear status-win) - (add-string status-win *status-text* :attributes '(:reverse)) - (refresh status-win) - (setf last-status *status-text*)) - - ;; 3. Handle Keyboard Input - (let* ((event (get-wide-event input-win)) - (ch (and event (typep event 'event) (event-key event)))) - (when ch - (cond - ((or (eq ch #\Newline) (eq ch #\Return)) - (let ((cmd (coerce *input-buffer* 'string))) - (setf (fill-pointer *input-buffer*) 0) - (when (> (length cmd) 0) - (let ((framed (opencortex:frame-message (format nil "~s" (list :TYPE :EVENT :PAYLOAD (list :SENSOR :chat-message :TEXT cmd)))))) - (format *stream* "~a" framed) - (finish-output *stream*))) - (when (string= cmd "/exit") (setf *is-running* nil)))) - ((or (eq ch :backspace) (eq ch #\Backspace) (eq ch #\Rubout) (eq ch #\Del)) - (when (> (length *input-buffer*) 0) - (decf (fill-pointer *input-buffer*)))) - ((characterp ch) - (vector-push-extend ch *input-buffer*)))) - - (clear input-win) - (add-string input-win (concatenate 'string "> " (coerce *input-buffer* 'string))) - (move input-win 0 (+ 2 (length *input-buffer*))) - (refresh input-win)) - - (sleep 0.02)))) - (setf *is-running* nil) - (when *socket* (usocket:socket-close *socket*)))) -#+end_src -""" - -with open("literate/tui-client.org", "w") as f: - f.write(content) diff --git a/fix_tui_physically.py b/fix_tui_physically.py deleted file mode 100644 index b4b932e..0000000 --- a/fix_tui_physically.py +++ /dev/null @@ -1,154 +0,0 @@ -import os - -content = r""":PROPERTIES: -:ID: tui-client-spec -:CREATED: [2026-04-17 Fri 11:00] -:END: -#+TITLE: OpenCortex TUI Client (Standalone) -#+STARTUP: content -#+FILETAGS: :tui:ux:client: - -* Overview -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 ../src/tui-client.lisp -(in-package :cl-user) -(defpackage :opencortex.tui - (:use :cl :croatoan) - (:export :main)) -(in-package :opencortex.tui) - -(defvar *daemon-host* "127.0.0.1") -(defvar *daemon-port* 9105) -(defvar *socket* nil) -(defvar *stream* nil) -(defvar *chat-history* (list)) -(defvar *status-text* "Connecting...") -(defvar *input-buffer* (make-array 0 :element-type 'char :fill-pointer 0 :adjustable t)) -(defvar *is-running* t) -(defvar *queue-lock* (bt:make-lock)) -(defvar *incoming-msgs* nil) - -(defun enqueue-msg (msg) - (bt:with-lock-held (*queue-lock*) - (push msg *incoming-msgs*))) - -(defun dequeue-msgs () - (bt:with-lock-held (*queue-lock*) - (let ((msgs (nreverse *incoming-msgs*))) - (setf *incoming-msgs* nil) - msgs))) - -(defun clean-keywords (msg) - (if (listp msg) - (let ((clean nil)) - (loop for (k v) on msg by #'cddr - do (push (intern (string k) :keyword) clean) - (push v clean)) - (nreverse clean)) - msg)) - -(defun listen-thread () - (loop while *is-running* do - (handler-case - (when (and *stream* (open-stream-p *stream*)) - (let ((raw-msg (opencortex:read-framed-message *stream*))) - (unless (member raw-msg '(:eof :error)) - (let* ((msg (clean-keywords raw-msg)) - (type (or (getf msg :TYPE) (getf msg :type))) - (payload (or (getf msg :PAYLOAD) (getf msg :payload)))) - (cond ((and (listp msg) (eq type :EVENT)) - (let ((action (or (getf payload :ACTION) (getf payload :action))) - (text (or (getf payload :TEXT) (getf payload :text) (getf payload :MESSAGE) (getf payload :message)))) - (cond ((eq action :handshake) (setf *status-text* "Ready")) - (text (enqueue-msg (format nil "SYSTEM: ~a" text)))))) - ((and (listp msg) (eq type :STATUS)) - (setf *status-text* (format nil "[Scribe: ~a] [Gardener: ~a]" - (or (getf msg :SCRIBE) (getf msg :scribe)) - (or (getf msg :GARDENER) (getf msg :gardener))))) - ((and (listp msg) (eq type :CHAT)) - (enqueue-msg (or (getf msg :TEXT) (getf msg :text)))) - (t (harness-log "TUI: Ignored unknown type ~a" type))))) - (when (eq raw-msg :eof) (setf *is-running* nil)) - (when (eq raw-msg :error) (setf *status-text* "Protocol Error")))) - (error (c) (setf *status-text* (format nil "Net Error: ~a" c)) (setf *is-running* nil))) - (sleep 0.05))) - -(defun main () - (handler-case - (setf *socket* (usocket:socket-connect *daemon-host* *daemon-port*)) - (error (e) (format t "Error connecting: ~a~%" e) (return-from main))) - (setf *stream* (usocket:socket-stream *socket*)) - (bt:make-thread #'listen-thread :name "tui-listener") - - (unwind-protect - (with-screen (scr :input-echoing nil :input-blocking nil :enable-colors t :cursor-visible t) - (let* ((h (height scr)) - (w (width scr)) - (chat-win (make-instance 'window :height (- h 2) :width w :position (list 0 0))) - (status-win (make-instance 'window :height 1 :width w :position (list (- h 2) 0))) - (input-win (make-instance 'window :height 1 :width w :position (list (- h 1) 0))) - (last-status nil)) - - (setf (function-keys-enabled-p input-win) t) - (setf (input-blocking input-win) nil) - - (loop while *is-running* do - ;; 1. Handle incoming messages - (let ((new-msgs (dequeue-msgs))) - (when new-msgs - (dolist (msg new-msgs) - (push msg *chat-history*) - (setf *chat-history* (subseq *chat-history* 0 (min (length *chat-history*) 500)))) - - (clear chat-win) - (let ((line-num 0)) - (dolist (m (reverse (subseq *chat-history* 0 (min (length *chat-history*) (- h 3))))) - (add-string chat-win m :y line-num :x 0) - (incf line-num))) - (refresh chat-win))) - - ;; 2. Render Status Bar ONLY if changed - (unless (equal *status-text* last-status) - (clear status-win) - (add-string status-win *status-text* :attributes '(:reverse)) - (refresh status-win) - (setf last-status *status-text*)) - - ;; 3. Handle Keyboard Input - (let* ((event (get-wide-event input-win)) - (ch (and event (typep event 'event) (event-key event)))) - (when ch - (cond - ((or (eq ch #\Newline) (eq ch #\Return)) - (let ((cmd (coerce *input-buffer* 'string))) - (setf (fill-pointer *input-buffer*) 0) - (when (> (length cmd) 0) - ;; Local Echo - (enqueue-msg (concatenate 'string "> " cmd)) - ;; Send to Brain - (let ((framed (opencortex:frame-message (format nil "~s" (list :TYPE :EVENT :PAYLOAD (list :SENSOR :chat-message :TEXT cmd)))))) - (format *stream* "~a" framed) - (finish-output *stream*))) - (when (string= cmd "/exit") (setf *is-running* nil)))) - ((or (eq ch :backspace) (eq ch #\Backspace) (eq ch #\Rubout) (eq ch #\Del)) - (when (> (length *input-buffer*) 0) - (decf (fill-pointer *input-buffer*)))) - ((characterp ch) - (vector-push-extend ch *input-buffer*)))) - - (clear input-win) - (add-string input-win (concatenate 'string "> " (coerce *input-buffer* 'string))) - (move input-win 0 (+ 2 (length *input-buffer*))) - (refresh input-win)) - - (sleep 0.02)))) - (setf *is-running* nil) - (when *socket* (usocket:socket-close *socket*)))) -#+end_src -""" - -with open('literate/tui-client.org', 'w') as f: - f.write(content) -print("Physical Org file rewritten.") diff --git a/fix_tui_precise.py b/fix_tui_precise.py deleted file mode 100644 index 149a812..0000000 --- a/fix_tui_precise.py +++ /dev/null @@ -1,43 +0,0 @@ -import sys - -filepath = 'literate/tui-client.org' -with open(filepath, 'r') as f: - lines = f.read() - -# I will replace the block from (defun listen-thread to (sleep 0.05))) -# with a guaranteed balanced version. - -import re -pattern = r'\(defun listen-thread \(.*?\)\s+\(sleep 0.05\)\)\)' -replacement = """(defun listen-thread () - (loop while *is-running* do - (handler-case - (when (and *stream* (open-stream-p *stream*)) - (let ((raw-msg (opencortex:read-framed-message *stream*))) - (unless (member raw-msg '(:eof :error)) - (let* ((msg (clean-keywords raw-msg)) - (type (or (getf msg :TYPE) (getf msg :type))) - (payload (or (getf msg :PAYLOAD) (getf msg :payload)))) - (cond ((eq type :EVENT) - (let ((action (or (getf payload :ACTION) (getf payload :action))) - (text (or (getf payload :TEXT) (getf payload :text) (getf payload :MESSAGE) (getf payload :message)))) - (cond ((eq action :handshake) (setf *status-text* "Ready")) - (text (enqueue-msg (format nil "SYSTEM: ~a" text)))))) - ((eq type :STATUS) - (setf *status-text* (format nil "[Scribe: ~a] [Gardener: ~a]" - (or (getf msg :SCRIBE) (getf msg :scribe)) - (or (getf msg :GARDENER) (getf msg :gardener))))) - ((eq type :CHAT) - (enqueue-msg (or (getf msg :TEXT) (getf msg :text)))) - (t (harness-log "TUI: Ignored unknown type ~a" type)))))) - (when (eq raw-msg :eof) (setf *is-running* nil)) - (when (eq raw-msg :error) (setf *status-text* "Protocol Error")))) - (error (c) (setf *status-text* (format nil "Net Error: ~a" c)) (setf *is-running* nil))) - (sleep 0.05)))""" - -# We use a more aggressive regex that matches greedily to consume all duplication -lines = re.sub(r'\(defun listen-thread \(.*?\)\s+\(sleep 0.05\)\)\).*?\(sleep 0.05\)\)\)', replacement, lines, flags=re.DOTALL) - -with open(filepath, 'w') as f: - f.write(lines) -print("Precise repair applied.") diff --git a/literate/act.org b/literate/act.org index 1e0165f..2ac9587 100644 --- a/literate/act.org +++ b/literate/act.org @@ -31,8 +31,9 @@ The core harness can be configured via environment variables to operate silently (register-actuator :system #'execute-system-action) (register-actuator :tool #'execute-tool-action) (register-actuator :tui (lambda (action context) - (let ((stream (getf context :reply-stream))) - (when stream + (let* ((meta (getf context :meta)) + (stream (getf meta :reply-stream))) + (when (and stream (open-stream-p stream)) (format stream "~a" (frame-message action)) (finish-output stream)))))) #+end_src diff --git a/literate/loop.org b/literate/loop.org index 04194c8..531bbae 100644 --- a/literate/loop.org +++ b/literate/loop.org @@ -29,23 +29,25 @@ The `process-signal` function is the core metabolic processor. It iterates throu "The entry point to the Metabolic Pipeline: Perceive -> Reason -> Act." (let ((current-signal signal)) (loop while current-signal do - (let ((depth (getf current-signal :depth 0))) + (let ((depth (getf current-signal :depth 0)) + (meta (getf current-signal :meta))) (when (> depth 10) (harness-log "METABOLISM ERROR: Max depth reached.") (return nil)) (when (bt:with-lock-held (*interrupt-lock*) *interrupt-flag*) (harness-log "METABOLISM: Interrupted.") (bt:with-lock-held (*interrupt-lock*) (setf *interrupt-flag* nil)) (return nil)) (handler-case - (let ((parent-metadata (list :reply-stream (getf current-signal :reply-stream) - :foveal-focus (getf current-signal :foveal-focus)))) + (progn (setf current-signal (perceive-gate current-signal)) (setf current-signal (reason-gate current-signal)) - (setf current-signal (act-gate current-signal)) - ;; Inherit metadata for the next metabolic cycle if feedback was generated. - (when (and current-signal (not (getf current-signal :reply-stream))) - (setf (getf current-signal :reply-stream) (getf parent-metadata :reply-stream))) - (when (and current-signal (not (getf current-signal :foveal-focus))) - (setf (getf current-signal :foveal-focus) (getf parent-metadata :foveal-focus)))) + (let ((feedback (act-gate current-signal))) + ;; feedback generation + (if feedback + (progn + ;; Inherit meta from trigger signal + (unless (getf feedback :meta) (setf (getf feedback :meta) meta)) + (setf current-signal feedback)) + (setf current-signal nil)))) (error (c) (let ((sensor (ignore-errors (getf (getf current-signal :payload) :sensor)))) (harness-log "METABOLISM CRASH [~a]: ~a" (or sensor :unknown) c) @@ -55,7 +57,7 @@ The `process-signal` function is the core metabolic processor. It iterates throu (rollback-memory 0)) (if (or (> depth 2) (member sensor '(:loop-error :tool-error))) (setf current-signal nil) - (setf current-signal (list :type :EVENT :depth (1+ depth) :reply-stream (getf current-signal :reply-stream) + (setf current-signal (list :type :EVENT :depth (1+ depth) :meta meta :payload (list :sensor :loop-error :message (format nil "~a" c) :depth depth))))))))))) #+end_src diff --git a/literate/perceive.org b/literate/perceive.org index 28484d8..4cf3965 100644 --- a/literate/perceive.org +++ b/literate/perceive.org @@ -33,8 +33,14 @@ The entry point for raw messages. It determines if the signal should be processe "Enqueues a raw message into the reactive signal pipeline." (let* ((payload (getf raw-message :payload)) (sensor (getf payload :sensor)) + (meta (getf raw-message :meta)) (async-p (or (getf payload :async-p) (member sensor *async-sensors*)))) - (when stream (setf (getf raw-message :reply-stream) stream)) + + ;; Ensure META exists and contains the stream if provided + (unless meta (setf meta (list :SOURCE :SYSTEM :SESSION-ID "internal"))) + (when stream (setf (getf meta :reply-stream) stream)) + (setf (getf raw-message :meta) meta) + (if async-p (bt:make-thread (lambda () diff --git a/literate/reason.org b/literate/reason.org index 669db89..1901593 100644 --- a/literate/reason.org +++ b/literate/reason.org @@ -48,6 +48,16 @@ The Reason stage is the cognitive engine of the OpenCortex. It bridges the gap b ** Cognitive Proposal (Think) #+begin_src lisp :tangle ../src/reason.lisp +(defun strip-markdown (text) + "Strips common markdown code block markers from text." + (if (and text (stringp text)) + (let ((cleaned text)) + (setf cleaned (cl-ppcre:regex-replace-all "^```[a-z]*\\n" cleaned "")) + (setf cleaned (cl-ppcre:regex-replace-all "\\n```$" cleaned "")) + (setf cleaned (cl-ppcre:regex-replace-all "```" cleaned "")) + (string-trim '(#\Space #\Newline #\Tab) cleaned)) + text)) + (defun think (context) "Generates a Lisp action proposal based on current context." (let* ((active-skill (find-triggered-skill context)) @@ -67,10 +77,10 @@ IMPORTANT: To reply to the user, you MUST use: To call a tool, you MUST use: (:TYPE :REQUEST :TARGET :TOOL :ACTION :CALL :TOOL \"\" :ARGS (:arg1 \"val\")) -PROVIDER RULE: Always use :provider :openrouter if calling LLM tools unless specified otherwise." +PROVIDER RULE: Always use the default cascade provider unless a specific model or capability is required for the task." assistant-name global-context tool-belt system-logs))) (let* ((thought (probabilistic-call raw-prompt :system-prompt system-prompt :context context)) - (cleaned (if (stringp thought) (string-trim '(#\Space #\Newline #\Tab) thought) thought)) + (cleaned (strip-markdown thought)) (meta (proto-get context :meta)) (source (proto-get meta :source))) (if (and cleaned (stringp cleaned)) @@ -83,8 +93,9 @@ PROVIDER RULE: Always use :provider :openrouter if calling LLM tools unless spec (cond ((member type '(:REQUEST :EVENT :STATUS :RESPONSE)) (unless (proto-get parsed :target) (setf (getf parsed :target) (or source :CLI))) parsed) - ;; Handle raw plists that look like tool calls - ((or (eq target :TOOL) (eq target :tool) (getf parsed :TOOL) (getf parsed :tool)) + ;; Handle raw plists or lists of plists that look like tool calls or data + ((or (eq target :TOOL) (eq target :tool) (getf parsed :TOOL) (getf parsed :tool) + (and (listp parsed) (listp (car parsed)) (keywordp (caar parsed)))) (list :TYPE :REQUEST :TARGET :TOOL :PAYLOAD parsed)) (t (list :TYPE :REQUEST :TARGET (or source :CLI) :PAYLOAD (list :ACTION :MESSAGE :TEXT cleaned)))))) (error (c) (list :TYPE :REQUEST :TARGET (or source :CLI) :PAYLOAD (list :ACTION :MESSAGE :TEXT cleaned)))) diff --git a/literate/setup.org b/literate/setup.org index ef7f14c..5cc9441 100644 --- a/literate/setup.org +++ b/literate/setup.org @@ -12,7 +12,7 @@ The ~setup.org~ file defines the automated installation and initialization seque set -e PORT=9105 -HOST=${1:-localhost} +HOST="localhost" RED='\033[0;31m'; GREEN='\033[0;32m'; BLUE='\033[0;34m'; YELLOW='\033[0;33m'; NC='\033[0m' command_exists() { command -v "$1" >/dev/null 2>&1; } @@ -34,20 +34,26 @@ if [ -f "$SCRIPT_DIR/.env" ]; then export "$key=$val" fi done < "$SCRIPT_DIR/.env" - [ -n "$HARNESS_PORT" ] && PORT=$HARNESS_PORT - [ -n "$HARNESS_HOST" ] && HOST=$HARNESS_HOST + [ -n "$ORG_AGENT_DAEMON_PORT" ] && PORT=$ORG_AGENT_DAEMON_PORT + [ -n "$DAEMON_HOST" ] && HOST=$DAEMON_HOST 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 echo -e "${BLUE}=== OpenCortex: Zero-to-One Bootstrapper ===${NC}" - git clone http://10.10.10.201:3001/amr/opencortex.git ~/.opencortex + git clone ssh://git@10.10.10.201:2222/amr/opencortex.git ~/.opencortex cd ~/.opencortex && git submodule update --init --recursive exec ./opencortex.sh "$@" fi # --- 2. SETUP --- setup_system() { + NON_INTERACTIVE=false + for arg in "$@"; do + if [ "$arg" == "--non-interactive" ]; then NON_INTERACTIVE=true; fi + done + echo -e "${BLUE}=== OpenCortex: Initializing System ===${NC}" echo -e "${YELLOW}--- Installing System Dependencies ---${NC}" if command_exists apt-get; then @@ -58,59 +64,49 @@ setup_system() { sbcl --non-interactive --load quicklisp.lisp --eval "(quicklisp-quickstart:install)" --eval "(ql-util:without-prompting (ql:add-to-init-file))" rm quicklisp.lisp fi - + cd "$SCRIPT_DIR" if [ ! -f .env ]; then - cp .env.example .env + if [ "$NON_INTERACTIVE" = true ]; then + echo "Non-interactive mode: Using environment variables for .env creation." + cp .env.example .env + [ -n "$MEMEX_USER" ] && sed -i "s|MEMEX_USER=.*|MEMEX_USER=\"$MEMEX_USER\"|" .env + [ -n "$MEMEX_ASSISTANT" ] && sed -i "s|MEMEX_ASSISTANT=.*|MEMEX_ASSISTANT=\"$MEMEX_ASSISTANT\"|" .env + [ -n "$OPENROUTER_API_KEY" ] && sed -i "s|OPENROUTER_API_KEY=.*|OPENROUTER_API_KEY=\"$OPENROUTER_API_KEY\"|" .env + [ -n "$MEMEX_DIR" ] && sed -i "s|MEMEX_DIR=.*|MEMEX_DIR=\"$MEMEX_DIR\"|" .env + else + cp .env.example .env + echo -e "\n${YELLOW}--- Identity Configuration ---${NC}" + read -p "Your Name [User]: " user_name < /dev/tty + user_name=${user_name:-User} + sed -i "s|MEMEX_USER=.*|MEMEX_USER=\"$user_name\"|" .env - echo -e "\n${YELLOW}--- Identity Configuration ---${NC}" - read -p "Your Name [User]: " user_name < /dev/tty - user_name=${user_name:-User} - sed -i "s|MEMEX_USER=.*|MEMEX_USER=\"$user_name\"|" .env + read -p "Agent Name [OpenCortex]: " agent_name < /dev/tty + agent_name=${agent_name:-OpenCortex} + sed -i "s|MEMEX_ASSISTANT=.*|MEMEX_ASSISTANT=\"$agent_name\"|" .env - read -p "Agent Name [OpenCortex]: " agent_name < /dev/tty - agent_name=${agent_name:-OpenCortex} - sed -i "s|MEMEX_ASSISTANT=.*|MEMEX_ASSISTANT=\"$agent_name\"|" .env + echo -e "\n${YELLOW}--- LLM Configuration ---${NC}" + read -p "OpenRouter API Key: " openrouter_key < /dev/tty + [ -n "$openrouter_key" ] && sed -i "s|OPENROUTER_API_KEY=.*|OPENROUTER_API_KEY=\"$openrouter_key\"|" .env - echo -e "\n${YELLOW}--- LLM Configuration ---${NC}" - read -p "Gemini API Key: " gemini_key < /dev/tty - [ -n "$gemini_key" ] && sed -i "s|GEMINI_API_KEY=.*|GEMINI_API_KEY=\"$gemini_key\"|" .env - read -p "Anthropic API Key: " anthropic_key < /dev/tty - [ -n "$anthropic_key" ] && sed -i "s|ANTHROPIC_API_KEY=.*|ANTHROPIC_API_KEY=\"$anthropic_key\"|" .env - read -p "OpenAI API Key: " openai_key < /dev/tty - [ -n "$openai_key" ] && sed -i "s|OPENAI_API_KEY=.*|OPENAI_API_KEY=\"$openai_key\"|" .env - read -p "OpenRouter API Key: " openrouter_key < /dev/tty - [ -n "$openrouter_key" ] && sed -i "s|OPENROUTER_API_KEY=.*|OPENROUTER_API_KEY=\"$openrouter_key\"|" .env + echo -e "\n${YELLOW}--- Memex Folder Structure ---${NC}" + read -p "Memex Root [\$HOME/memex]: " memex_dir < /dev/tty + memex_dir=${memex_dir:-\$HOME/memex} + sed -i "s|MEMEX_DIR=.*|MEMEX_DIR=\"$memex_dir\"|" .env + fi - echo -e "\n${YELLOW}--- Memex Folder Structure ---${NC}" - read -p "Memex Root [\$HOME/memex]: " memex_dir < /dev/tty - memex_dir=${memex_dir:-\$HOME/memex} - sed -i "s|MEMEX_DIR=.*|MEMEX_DIR=\"$memex_dir\"|" .env - sed -i "s|\"/memex/|\"$memex_dir/|g" .env + # Hydrate default paths + M_DIR=$(grep MEMEX_DIR .env | cut -d'"' -f2 | sed "s|\$HOME|$HOME|") sed -i "s|SKILLS_DIR=.*|SKILLS_DIR=\"$SCRIPT_DIR/skills\"|" .env - sed -i "s|ZETTELKASTEN_DIR=.*|ZETTELKASTEN_DIR=\"$memex_dir/notes\"|" .env - - read -p "Inbox Directory [\$memex_dir/inbox]: " inbox_dir < /dev/tty - inbox_dir=${inbox_dir:-\$memex_dir/inbox} - sed -i "s|INBOX_DIR=.*|INBOX_DIR=\"$inbox_dir\"|" .env - - read -p "Daily Directory [\$memex_dir/daily]: " daily_dir < /dev/tty - daily_dir=${daily_dir:-\$memex_dir/daily} - sed -i "s|DAILY_DIR=.*|DAILY_DIR=\"$daily_dir\"|" .env - - read -p "Projects Directory [\$memex_dir/projects]: " proj_dir < /dev/tty - proj_dir=${proj_dir:-\$memex_dir/projects} - sed -i "s|PROJECTS_DIR=.*|PROJECTS_DIR=\"$proj_dir\"|" .env - - mkdir -p "$memex_dir" "$inbox_dir" "$daily_dir" "$proj_dir" - mkdir -p "$memex_dir/notes" "$memex_dir/areas" "$memex_dir/resources" "$memex_dir/archives" "$memex_dir/system" + sed -i "s|ZETTELKASTEN_DIR=.*|ZETTELKASTEN_DIR=\"$M_DIR/notes\"|" .env + mkdir -p "$M_DIR" "$M_DIR/notes" "$M_DIR/areas" "$M_DIR/resources" "$M_DIR/archives" "$M_DIR/system" "$M_DIR/inbox" "$M_DIR/daily" "$M_DIR/projects" fi mkdir -p src for f in literate/*.org; do emacs --batch --eval "(require 'org)" --eval "(org-babel-tangle-file \"$f\")" >/dev/null 2>&1 || true done - + mkdir -p "$HOME/.local/bin" ln -sf "$SCRIPT_DIR/opencortex.sh" "$HOME/.local/bin/opencortex" @@ -123,72 +119,74 @@ setup_system() { done export PATH="$HOME/.local/bin:$PATH" - echo -e "${YELLOW}--- Compiling and Loading OpenCortex (this may take a minute) ---${NC}" + echo -e "${YELLOW}--- Compiling and Loading OpenCortex ---${NC}" sbcl --non-interactive --eval '(load (merge-pathnames "quicklisp/setup.lisp" (user-homedir-pathname)))' --eval '(push (truename (uiop:getenv "SCRIPT_DIR")) asdf:*central-registry*)' --eval "(ql:quickload '(:opencortex :croatoan))" - + if [ $? -ne 0 ]; then - echo -e "${RED}✗ Compilation or Loading failed.${NC}" + echo -e "${RED}✗ Compilation failed.${NC}" exit 1 fi - echo -e "${YELLOW}--- Finalizing: Awakening the Brain as a background daemon ---${NC}" - > "$SCRIPT_DIR/brain.log" + if [ "$NON_INTERACTIVE" = true ]; then + echo "Setup complete (Non-interactive)." + exit 0 + fi + + echo -e "${YELLOW}--- Finalizing: Awakening the Brain ---${NC}" "$SCRIPT_DIR/opencortex.sh" --boot > "$SCRIPT_DIR/brain.log" 2>&1 & - local success=false + success=false for i in {1..30}; do - if nc -z localhost $PORT 2>/dev/null; then - success=true - break - fi + if nc -z localhost $PORT 2>/dev/null; then success=true; break; fi sleep 2 echo -n "." done if [ "$success" = true ]; then - echo -e "\n${GREEN}✓ Brain is alive and responsive on port $PORT.${NC}" - echo -e "${GREEN}✓ Setup complete.${NC}" - if command -v opencortex >/dev/null 2>&1; then - echo -e "${BLUE}To start, run:${NC} ${GREEN}opencortex tui${NC}" - else - echo -e "${BLUE}To start, run:${NC} ${GREEN}exec bash && opencortex tui${NC}" - fi + echo -e "\n${GREEN}✓ Brain is alive on port $PORT.${NC}" exit 0 else echo -e "\n${RED}✗ Brain failed to wake up.${NC}" - echo -e "${YELLOW}Full Log Path: $(realpath "$SCRIPT_DIR/brain.log")${NC}" - cat "$SCRIPT_DIR/brain.log" exit 1 fi } # --- 3. COMMAND ROUTER --- -# By default, if no arguments are provided, we assume the user wants the CLI fallback. -COMMAND=${1:-"cli"} +COMMAND=$1 +[ -z "$COMMAND" ] && COMMAND="cli" +shift || true -# However, if the system is completely uninitialized, we force the 'setup' command. +DEFAULT_PORT=9105 +DEFAULT_HOST="localhost" +TARGET_PORT=${PORT:-$DEFAULT_PORT} +TARGET_HOST=${HOST:-$DEFAULT_HOST} + +# If uninitialized, force setup. if [ ! -f "$SCRIPT_DIR/src/package.lisp" ] || [ ! -f "$SCRIPT_DIR/.env" ]; then COMMAND="setup" fi case "$COMMAND" in setup) - setup_system + setup_system "$@" ;; - + --boot|boot) 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 '(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 "SCRIPT_DIR")) asdf:*central-registry*)' --eval '(format t "--- Quickloading OpenCortex ---~%")' --eval "(ql:quickload '(:opencortex :croatoan))" --eval '(opencortex:main)' + 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 "SCRIPT_DIR")) asdf:*central-registry*)' --eval '(format t "--- Quickloading OpenCortex ---~%")' --eval "(ql:quickload '(:opencortex :croatoan))" --eval '(opencortex:main)' ;; - + tui) - if ! nc -z $HOST $PORT 2>/dev/null; then + if ! nc -z $TARGET_HOST $TARGET_PORT 2>/dev/null; then echo -e "Brain is offline. Awakening..." "$SCRIPT_DIR/opencortex.sh" --boot > "$SCRIPT_DIR/brain.log" 2>&1 & for i in {1..15}; do sleep 2 - if nc -z $HOST $PORT 2>/dev/null; then break; fi + if nc -z $TARGET_HOST $TARGET_PORT 2>/dev/null; then break; fi echo -n "." done echo "" @@ -198,30 +196,65 @@ case "$COMMAND" in [ -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 "SCRIPT_DIR")) asdf:*central-registry*)' --eval '(ql:quickload :opencortex/tui)' --eval '(opencortex.tui:main)' ;; - + cli) - if ! nc -z $HOST $PORT 2>/dev/null; then + if ! nc -z $TARGET_HOST $TARGET_PORT 2>/dev/null; then echo -e "Brain is offline. Awakening..." "$SCRIPT_DIR/opencortex.sh" --boot > "$SCRIPT_DIR/brain.log" 2>&1 & for i in {1..15}; do sleep 2 - if nc -z $HOST $PORT 2>/dev/null; then break; fi + if nc -z $TARGET_HOST $TARGET_PORT 2>/dev/null; then break; fi echo -n "." done echo "" fi if command_exists socat; then - exec socat - TCP:: + exec socat - TCP:$TARGET_HOST:$TARGET_PORT else - exec nc + exec nc $TARGET_HOST $TARGET_PORT fi ;; - + *) echo -e "Unknown command: $COMMAND" echo "Available commands: setup, boot, tui, cli" exit 1 ;; esac - +#+end_src + +** Metabolic Docker Infrastructure (Dockerfile) +#+begin_src dockerfile :tangle ../Dockerfile +FROM debian:bullseye-slim + +ENV DEBIAN_FRONTEND=noninteractive + +RUN apt-get update && apt-get install -y \ + sbcl \ + emacs-nox \ + curl \ + git \ + socat \ + netcat-openbsd \ + libssl-dev \ + libncurses5-dev \ + libffi-dev \ + zlib1g-dev \ + libsqlite3-dev \ + && rm -rf /var/lib/apt/lists/* + +# Install Quicklisp +RUN curl -O https://beta.quicklisp.org/quicklisp.lisp \ + && sbcl --non-interactive --load quicklisp.lisp --eval "(quicklisp-quickstart:install)" --eval "(ql-util:without-prompting (ql:add-to-init-file))" \ + && rm quicklisp.lisp + +WORKDIR /app +COPY . . + +# Initialize system in non-interactive mode +RUN mkdir -p /root/memex && ./opencortex.sh setup --non-interactive + +EXPOSE 9105 + +CMD ["./opencortex.sh", "boot"] #+end_src diff --git a/mock_daemon.lisp b/mock_daemon.lisp deleted file mode 100644 index b332b18..0000000 --- a/mock_daemon.lisp +++ /dev/null @@ -1,28 +0,0 @@ -(load (merge-pathnames "quicklisp/setup.lisp" (user-homedir-pathname))) -(push (truename "./") asdf:*central-registry*) -(ql:quickload '(:usocket :bordeaux-threads :opencortex)) - -(defun handle-client (stream) - (handler-case - (progn - (format stream "~a" (opencortex:frame-message (opencortex:make-hello-message "0.1.0"))) - (finish-output stream) - (loop - (let ((msg (opencortex:read-framed-message stream))) - (when (or (eq msg :eof) (eq msg :error)) (return)) - (let ((text (getf (getf msg :payload) :text))) - (format t "MOCK: Received ~s~%" text) - (let ((resp (list :TYPE :REQUEST :PAYLOAD (list :ACTION :MESSAGE :TEXT (format nil "ECHO: ~a" text))))) - (format stream "~a" (opencortex:frame-message resp)) - (finish-output stream)))))) - (error (c) (format t "MOCK ERROR: ~a~%" c)))) - -(let ((socket (usocket:socket-listen "127.0.0.1" 9105 :reuse-address t))) - (format t "MOCK DAEMON LIVE ON 9105~%") - (unwind-protect - (loop (let ((client (usocket:socket-accept socket))) - (bt:make-thread (lambda () - (unwind-protect - (handle-client (usocket:socket-stream client)) - (usocket:socket-close client)))))) - (usocket:socket-close socket))) diff --git a/mock_daemon.py b/mock_daemon.py deleted file mode 100644 index 64aefbe..0000000 --- a/mock_daemon.py +++ /dev/null @@ -1,28 +0,0 @@ -import socket -import select - -server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) -server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) -server.bind(('127.0.0.1', 9105)) -server.listen(1) -print("MOCK DAEMON LIVE ON 9105") - -conn, addr = server.accept() -# 1. Send Handshake -hello = '(:TYPE :EVENT :PAYLOAD (:ACTION :HANDSHAKE :VERSION \"0.1.0\"))' -conn.sendall(f"{len(hello):06x}{hello}".encode()) - -# 2. Receive and Echo -data = conn.recv(1024).decode() -print(f"MOCK RECEIVED: {data}") -if data: - payload = data[6:] # Strip hex length - # extract message text simple way - import re - match = re.search(r':TEXT \"([^\"]*)\"', payload) - text = match.group(1) if match else "unknown" - resp = f'(:TYPE :REQUEST :PAYLOAD (:ACTION :MESSAGE :TEXT \"PYTHON_MOCK_ECHO: {text}\"))' - conn.sendall(f"{len(resp):06x}{resp}".encode()) - -conn.close() -server.close() diff --git a/opencortex.sh b/opencortex.sh index 6559de7..eb97d0d 100755 --- a/opencortex.sh +++ b/opencortex.sh @@ -2,7 +2,7 @@ set -e PORT=9105 -HOST=${1:-localhost} +HOST="localhost" RED='\033[0;31m'; GREEN='\033[0;32m'; BLUE='\033[0;34m'; YELLOW='\033[0;33m'; NC='\033[0m' command_exists() { command -v "$1" >/dev/null 2>&1; } @@ -24,20 +24,26 @@ if [ -f "$SCRIPT_DIR/.env" ]; then export "$key=$val" fi done < "$SCRIPT_DIR/.env" - [ -n "$HARNESS_PORT" ] && PORT=$HARNESS_PORT - [ -n "$HARNESS_HOST" ] && HOST=$HARNESS_HOST + [ -n "$ORG_AGENT_DAEMON_PORT" ] && PORT=$ORG_AGENT_DAEMON_PORT + [ -n "$DAEMON_HOST" ] && HOST=$DAEMON_HOST 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 echo -e "${BLUE}=== OpenCortex: Zero-to-One Bootstrapper ===${NC}" - git clone http://10.10.10.201:3001/amr/opencortex.git ~/.opencortex + git clone ssh://git@10.10.10.201:2222/amr/opencortex.git ~/.opencortex cd ~/.opencortex && git submodule update --init --recursive exec ./opencortex.sh "$@" fi # --- 2. SETUP --- setup_system() { + NON_INTERACTIVE=false + for arg in "$@"; do + if [ "$arg" == "--non-interactive" ]; then NON_INTERACTIVE=true; fi + done + echo -e "${BLUE}=== OpenCortex: Initializing System ===${NC}" echo -e "${YELLOW}--- Installing System Dependencies ---${NC}" if command_exists apt-get; then @@ -48,59 +54,49 @@ setup_system() { sbcl --non-interactive --load quicklisp.lisp --eval "(quicklisp-quickstart:install)" --eval "(ql-util:without-prompting (ql:add-to-init-file))" rm quicklisp.lisp fi - + cd "$SCRIPT_DIR" if [ ! -f .env ]; then - cp .env.example .env + if [ "$NON_INTERACTIVE" = true ]; then + echo "Non-interactive mode: Using environment variables for .env creation." + cp .env.example .env + [ -n "$MEMEX_USER" ] && sed -i "s|MEMEX_USER=.*|MEMEX_USER=\"$MEMEX_USER\"|" .env + [ -n "$MEMEX_ASSISTANT" ] && sed -i "s|MEMEX_ASSISTANT=.*|MEMEX_ASSISTANT=\"$MEMEX_ASSISTANT\"|" .env + [ -n "$OPENROUTER_API_KEY" ] && sed -i "s|OPENROUTER_API_KEY=.*|OPENROUTER_API_KEY=\"$OPENROUTER_API_KEY\"|" .env + [ -n "$MEMEX_DIR" ] && sed -i "s|MEMEX_DIR=.*|MEMEX_DIR=\"$MEMEX_DIR\"|" .env + else + cp .env.example .env + echo -e "\n${YELLOW}--- Identity Configuration ---${NC}" + read -p "Your Name [User]: " user_name < /dev/tty + user_name=${user_name:-User} + sed -i "s|MEMEX_USER=.*|MEMEX_USER=\"$user_name\"|" .env - echo -e "\n${YELLOW}--- Identity Configuration ---${NC}" - read -p "Your Name [User]: " user_name < /dev/tty - user_name=${user_name:-User} - sed -i "s|MEMEX_USER=.*|MEMEX_USER=\"$user_name\"|" .env + read -p "Agent Name [OpenCortex]: " agent_name < /dev/tty + agent_name=${agent_name:-OpenCortex} + sed -i "s|MEMEX_ASSISTANT=.*|MEMEX_ASSISTANT=\"$agent_name\"|" .env - read -p "Agent Name [OpenCortex]: " agent_name < /dev/tty - agent_name=${agent_name:-OpenCortex} - sed -i "s|MEMEX_ASSISTANT=.*|MEMEX_ASSISTANT=\"$agent_name\"|" .env + echo -e "\n${YELLOW}--- LLM Configuration ---${NC}" + read -p "OpenRouter API Key: " openrouter_key < /dev/tty + [ -n "$openrouter_key" ] && sed -i "s|OPENROUTER_API_KEY=.*|OPENROUTER_API_KEY=\"$openrouter_key\"|" .env - echo -e "\n${YELLOW}--- LLM Configuration ---${NC}" - read -p "Gemini API Key: " gemini_key < /dev/tty - [ -n "$gemini_key" ] && sed -i "s|GEMINI_API_KEY=.*|GEMINI_API_KEY=\"$gemini_key\"|" .env - read -p "Anthropic API Key: " anthropic_key < /dev/tty - [ -n "$anthropic_key" ] && sed -i "s|ANTHROPIC_API_KEY=.*|ANTHROPIC_API_KEY=\"$anthropic_key\"|" .env - read -p "OpenAI API Key: " openai_key < /dev/tty - [ -n "$openai_key" ] && sed -i "s|OPENAI_API_KEY=.*|OPENAI_API_KEY=\"$openai_key\"|" .env - read -p "OpenRouter API Key: " openrouter_key < /dev/tty - [ -n "$openrouter_key" ] && sed -i "s|OPENROUTER_API_KEY=.*|OPENROUTER_API_KEY=\"$openrouter_key\"|" .env + echo -e "\n${YELLOW}--- Memex Folder Structure ---${NC}" + read -p "Memex Root [\$HOME/memex]: " memex_dir < /dev/tty + memex_dir=${memex_dir:-\$HOME/memex} + sed -i "s|MEMEX_DIR=.*|MEMEX_DIR=\"$memex_dir\"|" .env + fi - echo -e "\n${YELLOW}--- Memex Folder Structure ---${NC}" - read -p "Memex Root [\$HOME/memex]: " memex_dir < /dev/tty - memex_dir=${memex_dir:-\$HOME/memex} - sed -i "s|MEMEX_DIR=.*|MEMEX_DIR=\"$memex_dir\"|" .env - sed -i "s|\"/memex/|\"$memex_dir/|g" .env + # Hydrate default paths + M_DIR=$(grep MEMEX_DIR .env | cut -d'"' -f2 | sed "s|\$HOME|$HOME|") sed -i "s|SKILLS_DIR=.*|SKILLS_DIR=\"$SCRIPT_DIR/skills\"|" .env - sed -i "s|ZETTELKASTEN_DIR=.*|ZETTELKASTEN_DIR=\"$memex_dir/notes\"|" .env - - read -p "Inbox Directory [\$memex_dir/inbox]: " inbox_dir < /dev/tty - inbox_dir=${inbox_dir:-\$memex_dir/inbox} - sed -i "s|INBOX_DIR=.*|INBOX_DIR=\"$inbox_dir\"|" .env - - read -p "Daily Directory [\$memex_dir/daily]: " daily_dir < /dev/tty - daily_dir=${daily_dir:-\$memex_dir/daily} - sed -i "s|DAILY_DIR=.*|DAILY_DIR=\"$daily_dir\"|" .env - - read -p "Projects Directory [\$memex_dir/projects]: " proj_dir < /dev/tty - proj_dir=${proj_dir:-\$memex_dir/projects} - sed -i "s|PROJECTS_DIR=.*|PROJECTS_DIR=\"$proj_dir\"|" .env - - mkdir -p "$memex_dir" "$inbox_dir" "$daily_dir" "$proj_dir" - mkdir -p "$memex_dir/notes" "$memex_dir/areas" "$memex_dir/resources" "$memex_dir/archives" "$memex_dir/system" + sed -i "s|ZETTELKASTEN_DIR=.*|ZETTELKASTEN_DIR=\"$M_DIR/notes\"|" .env + mkdir -p "$M_DIR" "$M_DIR/notes" "$M_DIR/areas" "$M_DIR/resources" "$M_DIR/archives" "$M_DIR/system" "$M_DIR/inbox" "$M_DIR/daily" "$M_DIR/projects" fi mkdir -p src for f in literate/*.org; do emacs --batch --eval "(require 'org)" --eval "(org-babel-tangle-file \"$f\")" >/dev/null 2>&1 || true done - + mkdir -p "$HOME/.local/bin" ln -sf "$SCRIPT_DIR/opencortex.sh" "$HOME/.local/bin/opencortex" @@ -113,72 +109,74 @@ setup_system() { done export PATH="$HOME/.local/bin:$PATH" - echo -e "${YELLOW}--- Compiling and Loading OpenCortex (this may take a minute) ---${NC}" + echo -e "${YELLOW}--- Compiling and Loading OpenCortex ---${NC}" sbcl --non-interactive --eval '(load (merge-pathnames "quicklisp/setup.lisp" (user-homedir-pathname)))' --eval '(push (truename (uiop:getenv "SCRIPT_DIR")) asdf:*central-registry*)' --eval "(ql:quickload '(:opencortex :croatoan))" - + if [ $? -ne 0 ]; then - echo -e "${RED}✗ Compilation or Loading failed.${NC}" + echo -e "${RED}✗ Compilation failed.${NC}" exit 1 fi - echo -e "${YELLOW}--- Finalizing: Awakening the Brain as a background daemon ---${NC}" - > "$SCRIPT_DIR/brain.log" + if [ "$NON_INTERACTIVE" = true ]; then + echo "Setup complete (Non-interactive)." + exit 0 + fi + + echo -e "${YELLOW}--- Finalizing: Awakening the Brain ---${NC}" "$SCRIPT_DIR/opencortex.sh" --boot > "$SCRIPT_DIR/brain.log" 2>&1 & - local success=false + success=false for i in {1..30}; do - if nc -z localhost $PORT 2>/dev/null; then - success=true - break - fi + if nc -z localhost $PORT 2>/dev/null; then success=true; break; fi sleep 2 echo -n "." done if [ "$success" = true ]; then - echo -e "\n${GREEN}✓ Brain is alive and responsive on port $PORT.${NC}" - echo -e "${GREEN}✓ Setup complete.${NC}" - if command -v opencortex >/dev/null 2>&1; then - echo -e "${BLUE}To start, run:${NC} ${GREEN}opencortex tui${NC}" - else - echo -e "${BLUE}To start, run:${NC} ${GREEN}exec bash && opencortex tui${NC}" - fi + echo -e "\n${GREEN}✓ Brain is alive on port $PORT.${NC}" exit 0 else echo -e "\n${RED}✗ Brain failed to wake up.${NC}" - echo -e "${YELLOW}Full Log Path: $(realpath "$SCRIPT_DIR/brain.log")${NC}" - cat "$SCRIPT_DIR/brain.log" exit 1 fi } # --- 3. COMMAND ROUTER --- -# By default, if no arguments are provided, we assume the user wants the CLI fallback. -COMMAND=${1:-"cli"} +COMMAND=$1 +[ -z "$COMMAND" ] && COMMAND="cli" +shift || true -# However, if the system is completely uninitialized, we force the 'setup' command. +DEFAULT_PORT=9105 +DEFAULT_HOST="localhost" +TARGET_PORT=${PORT:-$DEFAULT_PORT} +TARGET_HOST=${HOST:-$DEFAULT_HOST} + +# If uninitialized, force setup. if [ ! -f "$SCRIPT_DIR/src/package.lisp" ] || [ ! -f "$SCRIPT_DIR/.env" ]; then COMMAND="setup" fi case "$COMMAND" in setup) - setup_system + setup_system "$@" ;; - + --boot|boot) 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 '(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 "SCRIPT_DIR")) asdf:*central-registry*)' --eval '(format t "--- Quickloading OpenCortex ---~%")' --eval "(ql:quickload '(:opencortex :croatoan))" --eval '(opencortex:main)' + 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 "SCRIPT_DIR")) asdf:*central-registry*)' --eval '(format t "--- Quickloading OpenCortex ---~%")' --eval "(ql:quickload '(:opencortex :croatoan))" --eval '(opencortex:main)' ;; - + tui) - if ! nc -z $HOST $PORT 2>/dev/null; then + if ! nc -z $TARGET_HOST $TARGET_PORT 2>/dev/null; then echo -e "Brain is offline. Awakening..." "$SCRIPT_DIR/opencortex.sh" --boot > "$SCRIPT_DIR/brain.log" 2>&1 & for i in {1..15}; do sleep 2 - if nc -z $HOST $PORT 2>/dev/null; then break; fi + if nc -z $TARGET_HOST $TARGET_PORT 2>/dev/null; then break; fi echo -n "." done echo "" @@ -188,25 +186,25 @@ case "$COMMAND" in [ -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 "SCRIPT_DIR")) asdf:*central-registry*)' --eval '(ql:quickload :opencortex/tui)' --eval '(opencortex.tui:main)' ;; - + cli) - if ! nc -z $HOST $PORT 2>/dev/null; then + if ! nc -z $TARGET_HOST $TARGET_PORT 2>/dev/null; then echo -e "Brain is offline. Awakening..." "$SCRIPT_DIR/opencortex.sh" --boot > "$SCRIPT_DIR/brain.log" 2>&1 & for i in {1..15}; do sleep 2 - if nc -z $HOST $PORT 2>/dev/null; then break; fi + if nc -z $TARGET_HOST $TARGET_PORT 2>/dev/null; then break; fi echo -n "." done echo "" fi if command_exists socat; then - exec socat - TCP:: + exec socat - TCP:$TARGET_HOST:$TARGET_PORT else - exec nc + exec nc $TARGET_HOST $TARGET_PORT fi ;; - + *) echo -e "Unknown command: $COMMAND" echo "Available commands: setup, boot, tui, cli" diff --git a/skills/org-skill-cli-gateway.org b/skills/org-skill-cli-gateway.org index b529fed..69f0f11 100644 --- a/skills/org-skill-cli-gateway.org +++ b/skills/org-skill-cli-gateway.org @@ -24,7 +24,8 @@ The *CLI Gateway* is the primary sensory and actuating interface for human inter (defun execute-cli-action (action context) "Sends a framed message back to the connected CLI client." (let* ((payload (proto-get action :PAYLOAD)) - (stream (proto-get context :REPLY-STREAM))) + (meta (getf context :meta)) + (stream (getf meta :reply-stream))) (handler-case (if (and stream (open-stream-p stream)) (progn diff --git a/skills/org-skill-llm-gateway.org b/skills/org-skill-llm-gateway.org index 86df082..d9fdc06 100644 --- a/skills/org-skill-llm-gateway.org +++ b/skills/org-skill-llm-gateway.org @@ -122,12 +122,12 @@ The gateway utilizes a functional dispatch pattern. A single entry point, `execu "Queries an LLM provider via the unified gateway." ((:prompt :type :string :description "The user prompt.") (:system-prompt :type :string :description "The system instructions.") - (:provider :type :keyword :description "The provider. (Default: :openrouter)") + (:provider :type :keyword :description "Optional specific provider.") (:model :type :string :description "Optional specific model ID.")) :body (lambda (args) (execute-llm-request (getf args :prompt) (or (getf args :system-prompt) "You are a helpful assistant.") - :provider (or (getf args :provider) :openrouter) + :provider (getf args :provider) :model (getf args :model)))) (defskill :skill-llm-gateway diff --git a/skills/org-skill-scribe.org b/skills/org-skill-scribe.org index ad9dc05..1ba993d 100644 --- a/skills/org-skill-scribe.org +++ b/skills/org-skill-scribe.org @@ -144,13 +144,18 @@ The deterministic gate receives the list of proposed notes and writes them to th (defun verify-skill-scribe (action context) "Executes the note creation and marks source nodes as distilled." (declare (ignore context)) - (when (and (listp action) (not (member (getf action :type) '(:LOG :EVENT)))) - ;; Action is the list of note plists from the LLM - (scribe-commit-notes action) - (scribe-save-state) - (harness-log "SCRIBE: Distillation complete.") - ;; Return a log event to stop the loop - (list :type :LOG :payload (list :text "Distillation successful.")))) + (let ((data (cond ((and (listp action) (eq (getf action :type) :REQUEST)) + (getf (getf action :payload) :payload)) + ((and (listp action) (not (member (getf action :type) '(:LOG :EVENT)))) + action) + (t nil)))) + (when data + (harness-log "SCRIBE: Committing ~a atomic notes..." (length data)) + (scribe-commit-notes data) + (scribe-save-state) + (harness-log "SCRIBE: Distillation complete.") + ;; Return a log event to stop the loop + (list :type :LOG :payload (list :text "Distillation successful."))))) #+end_src ** Skill Registration diff --git a/src/act.lisp b/src/act.lisp index fcd118e..46c4551 100644 --- a/src/act.lisp +++ b/src/act.lisp @@ -18,8 +18,9 @@ (register-actuator :system #'execute-system-action) (register-actuator :tool #'execute-tool-action) (register-actuator :tui (lambda (action context) - (let ((stream (getf context :reply-stream))) - (when stream + (let* ((meta (getf context :meta)) + (stream (getf meta :reply-stream))) + (when (and stream (open-stream-p stream)) (format stream "~a" (frame-message action)) (finish-output stream)))))) diff --git a/src/loop.lisp b/src/loop.lisp index 0132c98..8fbddc0 100644 --- a/src/loop.lisp +++ b/src/loop.lisp @@ -8,23 +8,25 @@ "The entry point to the Metabolic Pipeline: Perceive -> Reason -> Act." (let ((current-signal signal)) (loop while current-signal do - (let ((depth (getf current-signal :depth 0))) + (let ((depth (getf current-signal :depth 0)) + (meta (getf current-signal :meta))) (when (> depth 10) (harness-log "METABOLISM ERROR: Max depth reached.") (return nil)) (when (bt:with-lock-held (*interrupt-lock*) *interrupt-flag*) (harness-log "METABOLISM: Interrupted.") (bt:with-lock-held (*interrupt-lock*) (setf *interrupt-flag* nil)) (return nil)) (handler-case - (let ((parent-metadata (list :reply-stream (getf current-signal :reply-stream) - :foveal-focus (getf current-signal :foveal-focus)))) + (progn (setf current-signal (perceive-gate current-signal)) (setf current-signal (reason-gate current-signal)) - (setf current-signal (act-gate current-signal)) - ;; Inherit metadata for the next metabolic cycle if feedback was generated. - (when (and current-signal (not (getf current-signal :reply-stream))) - (setf (getf current-signal :reply-stream) (getf parent-metadata :reply-stream))) - (when (and current-signal (not (getf current-signal :foveal-focus))) - (setf (getf current-signal :foveal-focus) (getf parent-metadata :foveal-focus)))) + (let ((feedback (act-gate current-signal))) + ;; feedback generation + (if feedback + (progn + ;; Inherit meta from trigger signal + (unless (getf feedback :meta) (setf (getf feedback :meta) meta)) + (setf current-signal feedback)) + (setf current-signal nil)))) (error (c) (let ((sensor (ignore-errors (getf (getf current-signal :payload) :sensor)))) (harness-log "METABOLISM CRASH [~a]: ~a" (or sensor :unknown) c) @@ -34,7 +36,7 @@ (rollback-memory 0)) (if (or (> depth 2) (member sensor '(:loop-error :tool-error))) (setf current-signal nil) - (setf current-signal (list :type :EVENT :depth (1+ depth) :reply-stream (getf current-signal :reply-stream) + (setf current-signal (list :type :EVENT :depth (1+ depth) :meta meta :payload (list :sensor :loop-error :message (format nil "~a" c) :depth depth))))))))))) (defun start-heartbeat () diff --git a/src/perceive.lisp b/src/perceive.lisp index 62d5b7e..aa5d18b 100644 --- a/src/perceive.lisp +++ b/src/perceive.lisp @@ -10,8 +10,14 @@ "Enqueues a raw message into the reactive signal pipeline." (let* ((payload (getf raw-message :payload)) (sensor (getf payload :sensor)) + (meta (getf raw-message :meta)) (async-p (or (getf payload :async-p) (member sensor *async-sensors*)))) - (when stream (setf (getf raw-message :reply-stream) stream)) + + ;; Ensure META exists and contains the stream if provided + (unless meta (setf meta (list :SOURCE :SYSTEM :SESSION-ID "internal"))) + (when stream (setf (getf meta :reply-stream) stream)) + (setf (getf raw-message :meta) meta) + (if async-p (bt:make-thread (lambda () diff --git a/src/reason.lisp b/src/reason.lisp index a6a6e13..3168be1 100644 --- a/src/reason.lisp +++ b/src/reason.lisp @@ -26,6 +26,16 @@ (t (harness-log "PROBABILISTIC: Backend ~a failed: ~a" backend (getf result :message)))))))) (list :type :LOG :payload (list :text "Neural Cascade Failure: All providers exhausted."))))) +(defun strip-markdown (text) + "Strips common markdown code block markers from text." + (if (and text (stringp text)) + (let ((cleaned text)) + (setf cleaned (cl-ppcre:regex-replace-all "^```[a-z]*\\n" cleaned "")) + (setf cleaned (cl-ppcre:regex-replace-all "\\n```$" cleaned "")) + (setf cleaned (cl-ppcre:regex-replace-all "```" cleaned "")) + (string-trim '(#\Space #\Newline #\Tab) cleaned)) + text)) + (defun think (context) "Generates a Lisp action proposal based on current context." (let* ((active-skill (find-triggered-skill context)) @@ -45,10 +55,10 @@ IMPORTANT: To reply to the user, you MUST use: To call a tool, you MUST use: (:TYPE :REQUEST :TARGET :TOOL :ACTION :CALL :TOOL \"\" :ARGS (:arg1 \"val\")) -PROVIDER RULE: Always use :provider :openrouter if calling LLM tools unless specified otherwise." +PROVIDER RULE: Always use the default cascade provider unless a specific model or capability is required for the task." assistant-name global-context tool-belt system-logs))) (let* ((thought (probabilistic-call raw-prompt :system-prompt system-prompt :context context)) - (cleaned (if (stringp thought) (string-trim '(#\Space #\Newline #\Tab) thought) thought)) + (cleaned (strip-markdown thought)) (meta (proto-get context :meta)) (source (proto-get meta :source))) (if (and cleaned (stringp cleaned)) @@ -61,8 +71,9 @@ PROVIDER RULE: Always use :provider :openrouter if calling LLM tools unless spec (cond ((member type '(:REQUEST :EVENT :STATUS :RESPONSE)) (unless (proto-get parsed :target) (setf (getf parsed :target) (or source :CLI))) parsed) - ;; Handle raw plists that look like tool calls - ((or (eq target :TOOL) (eq target :tool) (getf parsed :TOOL) (getf parsed :tool)) + ;; Handle raw plists or lists of plists that look like tool calls or data + ((or (eq target :TOOL) (eq target :tool) (getf parsed :TOOL) (getf parsed :tool) + (and (listp parsed) (listp (car parsed)) (keywordp (caar parsed)))) (list :TYPE :REQUEST :TARGET :TOOL :PAYLOAD parsed)) (t (list :TYPE :REQUEST :TARGET (or source :CLI) :PAYLOAD (list :ACTION :MESSAGE :TEXT cleaned)))))) (error (c) (list :TYPE :REQUEST :TARGET (or source :CLI) :PAYLOAD (list :ACTION :MESSAGE :TEXT cleaned)))) diff --git a/test-events.lisp b/test-events.lisp deleted file mode 100644 index 84ecdce..0000000 --- a/test-events.lisp +++ /dev/null @@ -1,23 +0,0 @@ -(require :asdf) -(load (merge-pathnames "quicklisp/setup.lisp" (user-homedir-pathname))) -(ql:quickload :croatoan) -(in-package :cl-user) -(defun main () - (with-open-file (f "event2.log" :direction :output :if-exists :supersede) - (croatoan:with-screen (scr :input-echoing nil :input-blocking nil) - (let* ((h (croatoan:height scr)) - (w (croatoan:width scr)) - (input-win (make-instance 'croatoan:window :height 1 :width w :position (list (- h 1) 0)))) - (setf (croatoan:function-keys-enabled-p input-win) t) - (setf (croatoan:input-blocking input-win) nil) - (loop - (let* ((event (croatoan:get-wide-event input-win)) - (ch (and event (typep event 'croatoan:event) (croatoan:event-key event)))) - (when ch - (format f "Got: ~S (type: ~S)~%" ch (type-of ch)) - (finish-output f) - (when (or (eq ch #\q) (eq ch :q)) - (return)))) - (sleep 0.05)))))) -(main) -(sb-ext:exit) diff --git a/test-input.lisp b/test-input.lisp deleted file mode 100644 index afaff6f..0000000 --- a/test-input.lisp +++ /dev/null @@ -1,27 +0,0 @@ -(require :asdf) -(load (merge-pathnames "quicklisp/setup.lisp" (user-homedir-pathname))) -(ql:quickload :croatoan) -(in-package :cl-user) -(defun main () - (with-open-file (f "test-input.log" :direction :output :if-exists :supersede) - (format f "Starting...~%") - (finish-output f) - (croatoan:with-screen (scr :input-echoing nil :input-blocking nil :cursor-visible t) - (let* ((h (croatoan:height scr)) - (w (croatoan:width scr)) - (input-win (make-instance 'croatoan:window :height 1 :width w :position (list (- h 1) 0))) - (buf (make-array 0 :element-type 'character :fill-pointer 0 :adjustable t))) - (setf (croatoan:input-blocking input-win) nil) - (loop - (let ((ch (croatoan:get-char input-win))) - (when ch - (format f "Got: ~S~%" ch) - (finish-output f) - (return))) - (croatoan:clear input-win) - (croatoan:add-string input-win (concatenate 'string "> " (coerce buf 'string))) - (croatoan:move input-win 0 (+ 2 (length buf))) - (croatoan:refresh input-win) - (sleep 0.05)))))) -(main) -(sb-ext:exit) diff --git a/test-keys.lisp b/test-keys.lisp deleted file mode 100644 index 52a5a14..0000000 --- a/test-keys.lisp +++ /dev/null @@ -1,22 +0,0 @@ -(require :asdf) -(load (merge-pathnames "quicklisp/setup.lisp" (user-homedir-pathname))) -(ql:quickload :croatoan) -(in-package :cl-user) -(defun main () - (with-open-file (f "key.log" :direction :output :if-exists :supersede) - (croatoan:with-screen (scr :input-echoing nil :input-blocking nil) - (let* ((h (croatoan:height scr)) - (w (croatoan:width scr)) - (input-win (make-instance 'croatoan:window :height 1 :width w :position (list (- h 1) 0)))) - (setf (croatoan:function-keys-enabled-p input-win) t) - (setf (croatoan:input-blocking input-win) nil) - (loop - (let ((ch (croatoan:get-char input-win))) - (when ch - (format f "Got: ~S (type: ~S) (code: ~S)~%" ch (type-of ch) (and (characterp ch) (char-code ch))) - (finish-output f) - (when (or (eq ch #\q) (eq ch :q)) - (return)))) - (sleep 0.05)))))) -(main) -(sb-ext:exit) diff --git a/test-output.txt b/test-output.txt deleted file mode 100644 index ecf259c..0000000 --- a/test-output.txt +++ /dev/null @@ -1,5 +0,0 @@ -To load "croatoan": - Load 1 ASDF system: - croatoan -; Loading "croatoan" -................ \ No newline at end of file diff --git a/test_llm_final.lisp b/test_llm_final.lisp deleted file mode 100644 index cedd05d..0000000 --- a/test_llm_final.lisp +++ /dev/null @@ -1,27 +0,0 @@ -(load (merge-pathnames "quicklisp/setup.lisp" (user-homedir-pathname))) -(push (truename "./") asdf:*central-registry*) -(ql:quickload :opencortex) - -;; Manually load .env for testing -(with-open-file (in ".env" :if-does-not-exist nil) - (when in - (loop for line = (read-line in nil) while line do - (let ((pos (position #\= line))) - (when pos - (let ((key (string-trim " \"" (subseq line 0 pos))) - (val (string-trim " \"" (subseq line (1+ pos))))) - (sb-posix:putenv (format nil "~a=~a" key val)))))))) - -(opencortex:initialize-all-skills) - -(format t "~%--- PROBING OPENROUTER ---~%") -;; Inject it directly into the vault memory to be sure -(let ((key (uiop:getenv "OPENROUTER_API_KEY"))) - (when key - (setf (gethash "OPENROUTER-API-KEY" opencortex::*vault-memory*) key))) - -(let ((res (opencortex:ask-probabilistic "Say Cognitive Loop Active" :cascade (list :openrouter)))) - (format t "~%--- PROBE RESULT ---~%~s~%--------------------~%" res) - (if (and (stringp res) (search "Active" res)) - (uiop:quit 0) - (uiop:quit 1))) diff --git a/verify_final.py b/verify_final.py deleted file mode 100644 index f53ef18..0000000 --- a/verify_final.py +++ /dev/null @@ -1,47 +0,0 @@ -import socket, time, sys - -def verify(): - try: - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.settimeout(15) - s.connect(("localhost", 9105)) - - # 1. Read handshake - print("Handshake:", s.recv(4096).decode()) - - # 2. Send "Hi" - payload = '(:TYPE :EVENT :PAYLOAD (:SENSOR :CHAT-MESSAGE :TEXT "Hi"))' - msg = f"{len(payload):06x}{payload}".encode() - s.sendall(msg) - print("Sent 'Hi'") - - # 3. Read responses - # We expect a STATUS then a CHAT - responses = [] - start_time = time.time() - while time.time() - start_time < 10: - try: - data = s.recv(4096).decode() - if not data: break - print(f"Received: {data}") - responses.append(data) - if ":CHAT" in data: break - except socket.timeout: - break - - s.close() - - all_responses = "".join(responses) - if ":STATUS" in all_responses and ":CHAT" in all_responses: - print("SUCCESS: Full cycle complete.") - # Check for lowercase - if ":status" in all_responses: - print("FAILURE: Still seeing lowercase :status!") - else: - print("FAILURE: Missing expected response types.") - - except Exception as e: - print(f"Error: {e}") - sys.exit(1) - -verify() diff --git a/verify_response.py b/verify_response.py deleted file mode 100644 index b512ee5..0000000 --- a/verify_response.py +++ /dev/null @@ -1,41 +0,0 @@ -import socket, time, sys - -def verify(): - try: - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.settimeout(20) - s.connect(("localhost", 9105)) - - # We need to read handshake and status first to clear the pipe - full_data = b"" - while len(full_data) < 50: # Expecting at least handshake + status - chunk = s.recv(4096) - if not chunk: break - full_data += chunk - - print(f"Data received: {full_data.decode()}") - - # Send "Hi" - # Make sure we use the right length. - # Send "Hi" - # (:TYPE :EVENT :META (:SOURCE :CLI) :PAYLOAD (:SENSOR :USER-INPUT :TEXT "Hi")) - payload = '(:TYPE :EVENT :META (:SOURCE :CLI) :PAYLOAD (:SENSOR :USER-INPUT :TEXT "Hi"))' - length = len(payload) - msg = f"{length:06x}{payload}".encode() - print(f"Sending: {msg.decode()}") - s.sendall(msg) - - # Read response - while True: - chunk = s.recv(4096).decode() - if not chunk: break - print(f"Received chunk: {chunk}") - if ":REQUEST" in chunk or ":PAYLOAD" in chunk or "Neural Cascade Failure" in chunk: - print("SUCCESS: Response received!") - break - s.close() - except Exception as e: - print(f"Error: {e}") - sys.exit(1) - -verify() diff --git a/verify_response_v2.py b/verify_response_v2.py deleted file mode 100644 index 14f2ae7..0000000 --- a/verify_response_v2.py +++ /dev/null @@ -1,56 +0,0 @@ -import socket, time, sys - -def verify(): - try: - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.settimeout(20) - s.connect(("localhost", 9105)) - - # 1. Read everything until initial status - full_data = b"" - while b":STATUS" not in full_data and b":status" not in full_data: - chunk = s.recv(4096) - if not chunk: break - full_data += chunk - - print(f"Initial stream: {full_data.decode()}") - - # 2. Send "Hi" - payload = '(:TYPE :EVENT :PAYLOAD (:SENSOR :CHAT-MESSAGE :TEXT "Hi"))' - msg = f"{len(payload):06x}{payload}".encode() - print(f"Sending: {msg.decode()}") - s.sendall(msg) - - # 3. Read response - responses = [] - start_time = time.time() - while time.time() - start_time < 15: - try: - chunk = s.recv(4096).decode() - if not chunk: break - print(f"Received chunk: {chunk}") - responses.append(chunk) - if ":CHAT" in chunk: - print("Found reasoning response!") - except socket.timeout: - break - - s.close() - - # Assertions - all_text = "".join(responses) - if ":status" in all_text or ":status" in full_data.decode(): - print("FAILURE: Found lowercase :status!") - else: - print("SUCCESS: Keywords are normalized to uppercase.") - - if ":CHAT" in all_text: - print("SUCCESS: Full response loop closed.") - else: - print("FAILURE: No chat response received.") - - except Exception as e: - print(f"Error: {e}") - sys.exit(1) - -verify()