fix(reason): Mandate strict sexp output and implement cognitive extraction safety
Some checks failed
Deploy-Agent-V15-Stdin / JOB-V15-STDIN (push) Failing after 3s
Some checks failed
Deploy-Agent-V15-Stdin / JOB-V15-STDIN (push) Failing after 3s
This commit is contained in:
136
fix_final_bridge.py
Normal file
136
fix_final_bridge.py
Normal file
@@ -0,0 +1,136 @@
|
|||||||
|
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.")
|
||||||
141
fix_final_v2.py
Normal file
141
fix_final_v2.py
Normal file
@@ -0,0 +1,141 @@
|
|||||||
|
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.")
|
||||||
146
fix_final_v3.py
Normal file
146
fix_final_v3.py
Normal file
@@ -0,0 +1,146 @@
|
|||||||
|
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.")
|
||||||
145
fix_final_v4.py
Normal file
145
fix_final_v4.py
Normal file
@@ -0,0 +1,145 @@
|
|||||||
|
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.")
|
||||||
152
fix_final_v5.py
Normal file
152
fix_final_v5.py
Normal file
@@ -0,0 +1,152 @@
|
|||||||
|
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.")
|
||||||
@@ -64,15 +64,17 @@ The Reason stage is the cognitive engine of the OpenCortex. It bridges the gap b
|
|||||||
assistant-name global-context tool-belt system-logs)))
|
assistant-name global-context tool-belt system-logs)))
|
||||||
(let* ((thought (probabilistic-call raw-prompt :system-prompt system-prompt :context context))
|
(let* ((thought (probabilistic-call raw-prompt :system-prompt system-prompt :context context))
|
||||||
(cleaned (if (stringp thought) (string-trim '(#\Space #\Newline #\Tab) thought) thought)))
|
(cleaned (if (stringp thought) (string-trim '(#\Space #\Newline #\Tab) thought) thought)))
|
||||||
(if (stringp cleaned)
|
(if (and cleaned (stringp cleaned))
|
||||||
(let ((*read-eval* nil))
|
(let ((*read-eval* nil))
|
||||||
(handler-case
|
(if (and (> (length cleaned) 0) (char= (char cleaned 0) #\())
|
||||||
(let ((parsed (read-from-string cleaned)))
|
(handler-case
|
||||||
(if (and (listp parsed) (getf parsed :response))
|
(let ((parsed (read-from-string cleaned)))
|
||||||
(list :TYPE :CHAT :TEXT (getf parsed :response))
|
(if (and (listp parsed) (member (proto-get parsed :TYPE) '(:CHAT :REQUEST :EVENT :STATUS :RESPONSE)))
|
||||||
parsed))
|
parsed
|
||||||
(error (c) (list :type :EVENT :payload (list :sensor :syntax-error :code cleaned :error (format nil "~a" c))))))
|
(list :TYPE :CHAT :TEXT cleaned)))
|
||||||
cleaned)))))
|
(error (c) (list :TYPE :CHAT :TEXT cleaned)))
|
||||||
|
(list :TYPE :CHAT :TEXT cleaned)))
|
||||||
|
thought)))))
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** Deterministic Verification
|
** Deterministic Verification
|
||||||
|
|||||||
145
opencortex.sh
145
opencortex.sh
@@ -17,13 +17,21 @@ done
|
|||||||
export SCRIPT_DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
|
export SCRIPT_DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
|
||||||
|
|
||||||
# Load environment variables if they exist
|
# Load environment variables if they exist
|
||||||
if [ -f "$SCRIPT_DIR/.env" ]; then
|
# Priority 1: $HOME/.local/share/opencortex/.env
|
||||||
|
# Priority 2: $SCRIPT_DIR/.env
|
||||||
|
if [ -f "$HOME/.local/share/opencortex/.env" ]; then
|
||||||
|
ENV_PATH="$HOME/.local/share/opencortex/.env"
|
||||||
|
elif [ -f "$SCRIPT_DIR/.env" ]; then
|
||||||
|
ENV_PATH="$SCRIPT_DIR/.env"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -n "$ENV_PATH" ]; then
|
||||||
while IFS="=" read -r key value || [ -n "$key" ]; do
|
while IFS="=" read -r key value || [ -n "$key" ]; do
|
||||||
if [[ $key =~ ^[a-zA-Z_][a-zA-Z0-9_]*$ ]]; then
|
if [[ $key =~ ^[a-zA-Z_][a-zA-Z0-9_]*$ ]]; then
|
||||||
val=$(echo "$value" | sed "s/^\"//;s/\"$//")
|
val=$(echo "$value" | sed "s/^\"//;s/\"$//;s/^'//;s/'$//")
|
||||||
export "$key=$val"
|
export "$key=$val"
|
||||||
fi
|
fi
|
||||||
done < "$SCRIPT_DIR/.env"
|
done < "$ENV_PATH"
|
||||||
[ -n "$HARNESS_PORT" ] && PORT=$HARNESS_PORT
|
[ -n "$HARNESS_PORT" ] && PORT=$HARNESS_PORT
|
||||||
[ -n "$HARNESS_HOST" ] && HOST=$HARNESS_HOST
|
[ -n "$HARNESS_HOST" ] && HOST=$HARNESS_HOST
|
||||||
fi
|
fi
|
||||||
@@ -39,9 +47,10 @@ fi
|
|||||||
# --- 2. SETUP ---
|
# --- 2. SETUP ---
|
||||||
setup_system() {
|
setup_system() {
|
||||||
echo -e "${BLUE}=== OpenCortex: Initializing System ===${NC}"
|
echo -e "${BLUE}=== OpenCortex: Initializing System ===${NC}"
|
||||||
|
|
||||||
echo -e "${YELLOW}--- Installing System Dependencies ---${NC}"
|
echo -e "${YELLOW}--- Installing System Dependencies ---${NC}"
|
||||||
if command_exists apt-get; then
|
if command_exists apt-get; then
|
||||||
sudo apt-get update && sudo apt-get install -y sbcl emacs-nox rlwrap netcat-openbsd curl git socat libssl-dev libncurses5-dev libffi-dev zlib1g-dev libsqlite3-dev
|
sudo apt-get update && sudo apt-get install -y sbcl emacs-nox rlwrap netcat-openbsd curl git socat libssl-dev libncurses-dev libffi-dev zlib1g-dev libsqlite3-dev
|
||||||
fi
|
fi
|
||||||
if [ ! -d "$HOME/quicklisp" ]; then
|
if [ ! -d "$HOME/quicklisp" ]; then
|
||||||
curl -O https://beta.quicklisp.org/quicklisp.lisp
|
curl -O https://beta.quicklisp.org/quicklisp.lisp
|
||||||
@@ -50,7 +59,7 @@ setup_system() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
cd "$SCRIPT_DIR"
|
cd "$SCRIPT_DIR"
|
||||||
if [ ! -f .env ]; then
|
if [ ! -f .env ] && [ ! -f "$HOME/.local/share/opencortex/.env" ]; then
|
||||||
cp .env.example .env
|
cp .env.example .env
|
||||||
|
|
||||||
echo -e "\n${YELLOW}--- Identity Configuration ---${NC}"
|
echo -e "\n${YELLOW}--- Identity Configuration ---${NC}"
|
||||||
@@ -73,57 +82,20 @@ setup_system() {
|
|||||||
[ -n "$openrouter_key" ] && sed -i "s|OPENROUTER_API_KEY=.*|OPENROUTER_API_KEY=\"$openrouter_key\"|" .env
|
[ -n "$openrouter_key" ] && sed -i "s|OPENROUTER_API_KEY=.*|OPENROUTER_API_KEY=\"$openrouter_key\"|" .env
|
||||||
|
|
||||||
echo -e "\n${YELLOW}--- Memex Folder Structure ---${NC}"
|
echo -e "\n${YELLOW}--- Memex Folder Structure ---${NC}"
|
||||||
read -p "Memex Root [\$HOME/memex]: " memex_dir < /dev/tty
|
read -p "Memex Root [$HOME/memex]: " memex_root < /dev/tty
|
||||||
memex_dir=${memex_dir:-\$HOME/memex}
|
memex_root=${memex_root:-$HOME/memex}
|
||||||
sed -i "s|MEMEX_DIR=.*|MEMEX_DIR=\"$memex_dir\"|" .env
|
sed -i "s|MEMEX_ROOT=.*|MEMEX_ROOT=\"$memex_root\"|" .env
|
||||||
sed -i "s|\"/memex/|\"$memex_dir/|g" .env
|
|
||||||
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"
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
mkdir -p src
|
echo -e "\n${YELLOW}--- Warming Neural Cache ---${NC}"
|
||||||
for f in literate/*.org; do
|
rm -rf "$HOME/.cache/common-lisp"
|
||||||
emacs --batch --eval "(require 'org)" --eval "(org-babel-tangle-file \"$f\")" >/dev/null 2>&1 || true
|
sbcl --non-interactive --eval "(load (merge-pathnames \"quicklisp/setup.lisp\" (user-homedir-pathname)))" \
|
||||||
done
|
--eval "(push (truename \"$SCRIPT_DIR\") asdf:*central-registry*)" \
|
||||||
|
--eval "(ql:quickload '(:opencortex :opencortex/tui :croatoan))"
|
||||||
|
|
||||||
mkdir -p "$HOME/.local/bin"
|
echo -e "\n${YELLOW}--- Finalizing: Awakening the Brain as a background daemon ---${NC}"
|
||||||
ln -sf "$SCRIPT_DIR/opencortex.sh" "$HOME/.local/bin/opencortex"
|
|
||||||
|
|
||||||
for shell_config in "$HOME/.bashrc" "$HOME/.profile"; do
|
|
||||||
if [ -f "$shell_config" ]; then
|
|
||||||
if ! grep -q ".local/bin" "$shell_config"; then
|
|
||||||
echo 'export PATH="$HOME/.local/bin:$PATH"' >> "$shell_config"
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
export PATH="$HOME/.local/bin:$PATH"
|
|
||||||
|
|
||||||
echo -e "${YELLOW}--- Compiling and Loading OpenCortex (this may take a minute) ---${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}"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo -e "${YELLOW}--- Finalizing: Awakening the Brain as a background daemon ---${NC}"
|
|
||||||
> "$SCRIPT_DIR/brain.log"
|
> "$SCRIPT_DIR/brain.log"
|
||||||
"$SCRIPT_DIR/opencortex.sh" --boot > "$SCRIPT_DIR/brain.log" 2>&1 &
|
bash "$SCRIPT_DIR/opencortex.sh" --boot > "$SCRIPT_DIR/brain.log" 2>&1 &
|
||||||
|
|
||||||
local success=false
|
local success=false
|
||||||
for i in {1..30}; do
|
for i in {1..30}; do
|
||||||
@@ -131,18 +103,14 @@ setup_system() {
|
|||||||
success=true
|
success=true
|
||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
sleep 2
|
|
||||||
echo -n "."
|
echo -n "."
|
||||||
|
sleep 2
|
||||||
done
|
done
|
||||||
|
|
||||||
if [ "$success" = true ]; then
|
if [ "$success" = true ]; then
|
||||||
echo -e "\n${GREEN}✓ Brain is alive and responsive on port $PORT.${NC}"
|
echo -e "\n${GREEN}✓ Brain is alive and responsive on port $PORT.${NC}"
|
||||||
echo -e "${GREEN}✓ Setup complete.${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}"
|
||||||
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
|
|
||||||
exit 0
|
exit 0
|
||||||
else
|
else
|
||||||
echo -e "\n${RED}✗ Brain failed to wake up.${NC}"
|
echo -e "\n${RED}✗ Brain failed to wake up.${NC}"
|
||||||
@@ -153,11 +121,9 @@ setup_system() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# --- 3. COMMAND ROUTER ---
|
# --- 3. COMMAND ROUTER ---
|
||||||
# By default, if no arguments are provided, we assume the user wants the CLI fallback.
|
|
||||||
COMMAND=${1:-"cli"}
|
COMMAND=${1:-"cli"}
|
||||||
|
|
||||||
# However, if the system is completely uninitialized, we force the 'setup' command.
|
if [ ! -f "$SCRIPT_DIR/src/package.lisp" ] || ([ ! -f "$SCRIPT_DIR/.env" ] && [ ! -f "$HOME/.local/share/opencortex/.env" ]); then
|
||||||
if [ ! -f "$SCRIPT_DIR/src/package.lisp" ] || [ ! -f "$SCRIPT_DIR/.env" ] && [ ! -f "$HOME/.local/share/opencortex/.env" ]; then
|
|
||||||
COMMAND="setup"
|
COMMAND="setup"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -167,52 +133,57 @@ case "$COMMAND" in
|
|||||||
;;
|
;;
|
||||||
|
|
||||||
--boot|boot)
|
--boot|boot)
|
||||||
rm -rf "/home/user/.cache/common-lisp"
|
# Prevent double-booting
|
||||||
|
if nc -z localhost $PORT 2>/dev/null; then
|
||||||
|
echo -e "${GREEN}Brain is already active on port $PORT.${NC}"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo -e "${YELLOW}--- Awakening OpenCortex Conducter ---${NC}"
|
||||||
export SKILLS_DIR="${SCRIPT_DIR}/skills"
|
export SKILLS_DIR="${SCRIPT_DIR}/skills"
|
||||||
[ -z "$MEMEX_DIR" ] && export MEMEX_DIR="$HOME/memex"
|
[ -z "$MEMEX_DIR" ] && export MEMEX_DIR="$HOME/memex"
|
||||||
# 1. Warm the cache in the foreground (Blocking)
|
|
||||||
echo -e "touch "/boot.lock"
|
|
||||||
echo -e "--- Pre-compiling Neural Dependencies ---""
|
|
||||||
sbcl --non-interactive --eval '(load (merge-pathnames "quicklisp/setup.lisp" (user-homedir-pathname)))' \
|
|
||||||
--eval "(push (truename \"\") asdf:*central-registry*)" \
|
|
||||||
--eval "(ql:quickload '(:opencortex :opencortex/tui :croatoan))"
|
|
||||||
|
|
||||||
# 2. Now launch the actual daemon
|
# We don't purge cache here to avoid race conditions with TUI launch
|
||||||
rm -f "/boot.lock"
|
exec sbcl --eval "(load (merge-pathnames \"quicklisp/setup.lisp\" (user-homedir-pathname)))" \
|
||||||
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)'
|
--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 \"$SCRIPT_DIR\") asdf:*central-registry*)" \
|
||||||
|
--eval "(ql:quickload '(:opencortex :croatoan))" \
|
||||||
|
--eval "(opencortex:main)"
|
||||||
;;
|
;;
|
||||||
|
|
||||||
tui)
|
tui)
|
||||||
if ! nc -z $HOST $PORT 2>/dev/null; then
|
if ! nc -z $HOST $PORT 2>/dev/null; then
|
||||||
echo -e "Brain is offline. Awakening..."
|
if [ -f "$SCRIPT_DIR/boot.lock" ]; then
|
||||||
"$SCRIPT_DIR/opencortex.sh" --boot > "$SCRIPT_DIR/brain.log" 2>&1 &
|
echo -e "${YELLOW}Brain is currently waking up. Waiting for initialization...${NC}"
|
||||||
|
else
|
||||||
|
echo -e "Brain is offline. Awakening..."
|
||||||
|
touch "$SCRIPT_DIR/boot.lock"
|
||||||
|
bash "$SCRIPT_DIR/opencortex.sh" --boot > "$SCRIPT_DIR/brain.log" 2>&1 &
|
||||||
|
fi
|
||||||
|
|
||||||
for i in {1..30}; do
|
for i in {1..30}; do
|
||||||
sleep 2
|
sleep 2
|
||||||
if nc -z $HOST $PORT 2>/dev/null; then break; fi
|
if nc -z $HOST $PORT 2>/dev/null; then break; fi
|
||||||
echo -n "."
|
echo -n "."
|
||||||
done
|
done
|
||||||
echo ""
|
echo ""
|
||||||
|
rm -f "$SCRIPT_DIR/boot.lock"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo -e "Launching Croatoan TUI..."
|
echo -e "Launching Croatoan TUI..."
|
||||||
export SKILLS_DIR="${SCRIPT_DIR}/skills"
|
export SKILLS_DIR="${SCRIPT_DIR}/skills"
|
||||||
[ -z "$MEMEX_DIR" ] && export MEMEX_DIR="$HOME/memex"
|
[ -z "$MEMEX_DIR" ] && export MEMEX_DIR="$HOME/memex"
|
||||||
# 1. Warm the cache in the foreground (Blocking)
|
exec sbcl --disable-debugger --eval "(load (merge-pathnames \"quicklisp/setup.lisp\" (user-homedir-pathname)))" \
|
||||||
echo -e "touch "/boot.lock"
|
--eval "(push (truename \"$SCRIPT_DIR\") asdf:*central-registry*)" \
|
||||||
echo -e "--- Pre-compiling Neural Dependencies ---""
|
--eval "(ql:quickload :opencortex/tui)" \
|
||||||
sbcl --non-interactive --eval '(load (merge-pathnames "quicklisp/setup.lisp" (user-homedir-pathname)))' \
|
--eval "(opencortex.tui:main)"
|
||||||
--eval "(push (truename \"\") asdf:*central-registry*)" \
|
|
||||||
--eval "(ql:quickload '(:opencortex :opencortex/tui :croatoan))"
|
|
||||||
|
|
||||||
# 2. Now launch the actual daemon
|
|
||||||
rm -f "/boot.lock"
|
|
||||||
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)
|
cli)
|
||||||
if ! nc -z $HOST $PORT 2>/dev/null; then
|
if ! nc -z $HOST $PORT 2>/dev/null; then
|
||||||
echo -e "Brain is offline. Awakening..."
|
echo -e "Brain is offline. Awakening..."
|
||||||
"$SCRIPT_DIR/opencortex.sh" --boot > "$SCRIPT_DIR/brain.log" 2>&1 &
|
bash "$SCRIPT_DIR/opencortex.sh" --boot > "$SCRIPT_DIR/brain.log" 2>&1 &
|
||||||
for i in {1..30}; do
|
for i in {1..15}; do
|
||||||
sleep 2
|
sleep 2
|
||||||
if nc -z $HOST $PORT 2>/dev/null; then break; fi
|
if nc -z $HOST $PORT 2>/dev/null; then break; fi
|
||||||
echo -n "."
|
echo -n "."
|
||||||
@@ -220,9 +191,9 @@ case "$COMMAND" in
|
|||||||
echo ""
|
echo ""
|
||||||
fi
|
fi
|
||||||
if command_exists socat; then
|
if command_exists socat; then
|
||||||
exec socat - TCP::
|
exec socat - TCP:$HOST:$PORT
|
||||||
else
|
else
|
||||||
exec nc
|
exec nc $HOST $PORT
|
||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
|
|
||||||
|
|||||||
@@ -42,15 +42,17 @@
|
|||||||
assistant-name global-context tool-belt system-logs)))
|
assistant-name global-context tool-belt system-logs)))
|
||||||
(let* ((thought (probabilistic-call raw-prompt :system-prompt system-prompt :context context))
|
(let* ((thought (probabilistic-call raw-prompt :system-prompt system-prompt :context context))
|
||||||
(cleaned (if (stringp thought) (string-trim '(#\Space #\Newline #\Tab) thought) thought)))
|
(cleaned (if (stringp thought) (string-trim '(#\Space #\Newline #\Tab) thought) thought)))
|
||||||
(if (stringp cleaned)
|
(if (and cleaned (stringp cleaned))
|
||||||
(let ((*read-eval* nil))
|
(let ((*read-eval* nil))
|
||||||
(handler-case
|
(if (and (> (length cleaned) 0) (char= (char cleaned 0) #\())
|
||||||
(let ((parsed (read-from-string cleaned)))
|
(handler-case
|
||||||
(if (and (listp parsed) (getf parsed :response))
|
(let ((parsed (read-from-string cleaned)))
|
||||||
(list :TYPE :CHAT :TEXT (getf parsed :response))
|
(if (and (listp parsed) (member (proto-get parsed :TYPE) '(:CHAT :REQUEST :EVENT :STATUS :RESPONSE)))
|
||||||
parsed))
|
parsed
|
||||||
(error (c) (list :type :EVENT :payload (list :sensor :syntax-error :code cleaned :error (format nil "~a" c))))))
|
(list :TYPE :CHAT :TEXT cleaned)))
|
||||||
cleaned)))))
|
(error (c) (list :TYPE :CHAT :TEXT cleaned)))
|
||||||
|
(list :TYPE :CHAT :TEXT cleaned)))
|
||||||
|
thought)))))
|
||||||
|
|
||||||
(defun deterministic-verify (proposed-action context)
|
(defun deterministic-verify (proposed-action context)
|
||||||
"Iterates through all skill deterministic-gates sorted by priority."
|
"Iterates through all skill deterministic-gates sorted by priority."
|
||||||
|
|||||||
Reference in New Issue
Block a user