From eb33106771e14240210e557fa568e84aa9b33528 Mon Sep 17 00:00:00 2001 From: Amr Gharbeia Date: Sun, 19 Apr 2026 11:47:44 -0400 Subject: [PATCH] refactor(setup): Implement strict command router to eliminate fallthrough bugs --- literate/setup.org | 104 +++++++++++++++++++++++++------------------- opencortex.sh | 105 +++++++++++++++++++++++++-------------------- 2 files changed, 118 insertions(+), 91 deletions(-) diff --git a/literate/setup.org b/literate/setup.org index 44bf7b8..65f45a5 100644 --- a/literate/setup.org +++ b/literate/setup.org @@ -162,52 +162,66 @@ setup_system() { fi } -# --- 3. AUTO-SETUP --- -if [ ! -f "$SCRIPT_DIR/src/package.lisp" ] || [ ! -f "$SCRIPT_DIR/.env" ]; then - setup_system +# --- 3. COMMAND ROUTER --- +# By default, if no arguments are provided, we assume the user wants the CLI fallback. +COMMAND="cli" + +# However, if the system is completely uninitialized, we force the 'setup' command. +if [ ! -f "/src/package.lisp" ] || [ ! -f "/.env" ]; then + COMMAND="setup" fi -# --- 4. BOOT --- -if [[ "$1" == "--boot" ]]; then - 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)' -fi +case "" in + setup) + setup_system + ;; + + --boot|boot) + export SKILLS_DIR="/skills" + [ -z "" ] && export MEMEX_DIR="/home/user/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)' + ;; + + tui) + if ! nc -z 2>/dev/null; then + echo -e "Brain is offline. Awakening..." + "/opencortex.sh" --boot > "/brain.log" 2>&1 & + for i in {1..15}; do + sleep 2 + if nc -z 2>/dev/null; then break; fi + echo -n "." + done + echo "" + fi + echo -e "Launching Croatoan TUI..." + export SKILLS_DIR="/skills" + [ -z "" ] && export MEMEX_DIR="/home/user/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 2>/dev/null; then + echo -e "Brain is offline. Awakening..." + "/opencortex.sh" --boot > "/brain.log" 2>&1 & + for i in {1..15}; do + sleep 2 + if nc -z 2>/dev/null; then break; fi + echo -n "." + done + echo "" + fi + if command_exists socat; then + exec socat - TCP:: + else + exec nc + fi + ;; + + *) + echo -e "Unknown command: " + echo "Available commands: setup, boot, tui, cli" + exit 1 + ;; +esac -# --- 5. INTERACT --- -if [[ "$1" == "tui" ]]; then - if ! nc -z $HOST $PORT 2>/dev/null; then - echo -e "${YELLOW}Brain is offline. Awakening...${NC}" - "$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 - echo -n "." - done - echo "" - fi - echo -e "${BLUE}Launching Croatoan TUI...${NC}" - export SKILLS_DIR="${SCRIPT_DIR}/skills" - [ -z "$MEMEX_DIR" ] && export MEMEX_DIR="$HOME/memex" - exec sbcl --eval '(load (merge-pathnames "quicklisp/setup.lisp" (user-homedir-pathname)))' --eval '(push (truename (uiop:getenv "SCRIPT_DIR")) asdf:*central-registry*)' --eval '(ql:quickload :opencortex/tui)' --eval '(opencortex.tui:main)' -fi - -# --- 6. CLI FALLBACK --- -if [ -z "$1" ]; then - if ! nc -z $HOST $PORT 2>/dev/null; then - echo -e "${YELLOW}Brain is offline. Awakening...${NC}" - "$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 - echo -n "." - done - echo "" - fi - if command_exists socat; then - exec socat - TCP:$HOST:$PORT - else - exec nc $HOST $PORT - fi -fi #+end_src diff --git a/opencortex.sh b/opencortex.sh index 06e91c1..8a5b37f 100755 --- a/opencortex.sh +++ b/opencortex.sh @@ -152,51 +152,64 @@ setup_system() { fi } -# --- 3. AUTO-SETUP --- -if [ ! -f "$SCRIPT_DIR/src/package.lisp" ] || [ ! -f "$SCRIPT_DIR/.env" ]; then - setup_system +# --- 3. COMMAND ROUTER --- +# By default, if no arguments are provided, we assume the user wants the CLI fallback. +COMMAND="cli" + +# However, if the system is completely uninitialized, we force the 'setup' command. +if [ ! -f "/src/package.lisp" ] || [ ! -f "/.env" ]; then + COMMAND="setup" fi -# --- 4. BOOT --- -if [[ "$1" == "--boot" ]]; then - 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)' -fi - -# --- 5. INTERACT --- -if [[ "$1" == "tui" ]]; then - if ! nc -z $HOST $PORT 2>/dev/null; then - echo -e "${YELLOW}Brain is offline. Awakening...${NC}" - "$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 - echo -n "." - done - echo "" - fi - echo -e "${BLUE}Launching Croatoan TUI...${NC}" - export SKILLS_DIR="${SCRIPT_DIR}/skills" - [ -z "$MEMEX_DIR" ] && export MEMEX_DIR="$HOME/memex" - exec sbcl --eval '(load (merge-pathnames "quicklisp/setup.lisp" (user-homedir-pathname)))' --eval '(push (truename (uiop:getenv "SCRIPT_DIR")) asdf:*central-registry*)' --eval '(ql:quickload :opencortex/tui)' --eval '(opencortex.tui:main)' -fi - -# --- 6. CLI FALLBACK --- -if [ -z "$1" ]; then - if ! nc -z $HOST $PORT 2>/dev/null; then - echo -e "${YELLOW}Brain is offline. Awakening...${NC}" - "$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 - echo -n "." - done - echo "" - fi - if command_exists socat; then - exec socat - TCP:$HOST:$PORT - else - exec nc $HOST $PORT - fi -fi +case "" in + setup) + setup_system + ;; + + --boot|boot) + export SKILLS_DIR="/skills" + [ -z "" ] && export MEMEX_DIR="/home/user/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)' + ;; + + tui) + if ! nc -z 2>/dev/null; then + echo -e "Brain is offline. Awakening..." + "/opencortex.sh" --boot > "/brain.log" 2>&1 & + for i in {1..15}; do + sleep 2 + if nc -z 2>/dev/null; then break; fi + echo -n "." + done + echo "" + fi + echo -e "Launching Croatoan TUI..." + export SKILLS_DIR="/skills" + [ -z "" ] && export MEMEX_DIR="/home/user/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 2>/dev/null; then + echo -e "Brain is offline. Awakening..." + "/opencortex.sh" --boot > "/brain.log" 2>&1 & + for i in {1..15}; do + sleep 2 + if nc -z 2>/dev/null; then break; fi + echo -n "." + done + echo "" + fi + if command_exists socat; then + exec socat - TCP:: + else + exec nc + fi + ;; + + *) + echo -e "Unknown command: " + echo "Available commands: setup, boot, tui, cli" + exit 1 + ;; +esac