File size: 10,115 Bytes
3b70664 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | (defpackage #:nekod.cli
(:use #:cl #:nekod #:nekod.docker #:nekod.moe #:nekod.llm2asm)
(:export #:neko-main))
(in-package #:nekod.cli)
;; neko -- Nekomata CLI
;; Thin wrapper over the Nekomata core. All commands end up as
;; function calls into the live Lisp image.
(defun neko-main ()
"neko CLI entry point."
(let ((args (uiop:command-line-arguments)))
(cond
((null args) (print-usage))
((string= (first args) "run") (cmd-run (rest args)))
((string= (first args) "ps") (cmd-ps))
((string= (first args) "inspect") (cmd-inspect (rest args)))
((string= (first args) "intent") (cmd-intent (rest args)))
((string= (first args) "compile") (cmd-compile (rest args)))
((string= (first args) "policy") (cmd-policy (rest args)))
((string= (first args) "evolve") (cmd-evolve (rest args)))
((string= (first args) "snap") (cmd-snap (rest args)))
((string= (first args) "repl") (nekod:nekod-repl))
((string= (first args) "version") (cmd-version))
(t (format t "Unknown command: ~a~%" (first args))
(print-usage)
(uiop:quit 1)))))
;; ---- Help -------------------------------------------------------------------
(defun print-usage ()
(format t "~&neko -- Nekomata container engine~%")
(format t "~%Usage: neko <command> [args]~%")
(format t "~%Container commands:~%")
(format t " run <spec-file> Run containers from s-expression spec file~%")
(format t " ps List running containers~%")
(format t " inspect <name> Inspect a running container~%")
(format t "~%Intent commands:~%")
(format t " intent <string> Compile NL intent string to ASM and execute~%")
(format t " compile <string> Compile NL intent to ASM, show bytes only~%")
(format t "~%Policy commands:~%")
(format t " policy list List active policies~%")
(format t " policy check <p1> <p2> Check if two policies conflict~%")
(format t "~%System commands:~%")
(format t " evolve [N] Run N evolutionary generations (default: 1)~%")
(format t " snap save [label] Save a state snapshot~%")
(format t " snap list List saved snapshots~%")
(format t " snap restore <id> Restore a state snapshot~%")
(format t " repl Enter the infrastructure REPL~%")
(format t " version Show Nekomata version~%"))
;; ---- Container commands -----------------------------------------------------
(defun cmd-run (args)
"Run containers from a spec file."
(when (null args)
(format t "Usage: neko run <spec-file>~%")
(uiop:quit 1))
(let ((spec-file (first args)))
(unless (probe-file spec-file)
(format t "Error: file not found: ~a~%" spec-file)
(uiop:quit 1))
(format t "Loading spec: ~a~%" spec-file)
(load spec-file)
(let ((registered (nekod:list-registered)))
(format t "Registered containers: ~{~a~^, ~}~%" registered)
(dolist (name registered)
(let ((spec (nekod:lookup-container name)))
(when spec
(handler-case
(let ((id (nekod.docker:create-container spec)))
(nekod.docker:start-container id)
(format t " Started ~a (~a)~%" name id))
(error (e)
(format t " Failed ~a: ~a~%" name e)))))))))
(defun cmd-ps ()
"List running containers."
(handler-case
(let ((containers (nekod.docker:list-containers)))
(if (null containers)
(format t "No containers running.~%")
(progn
(format t "~%~16a ~30a ~10a~%"
"CONTAINER ID" "IMAGE" "STATUS")
(format t "~a~%" (make-string 60 :initial-element #\-))
(dolist (c containers)
(format t "~16a ~30a ~10a~%"
(or (cdr (assoc "Id" c :test #'string=)) "?")
(or (cdr (assoc "Image" c :test #'string=)) "?")
(or (cdr (assoc "State" c :test #'string=)) "?"))))))
(error (e)
(format t "Error listing containers: ~a~%" e))))
(defun cmd-inspect (args)
"Inspect a running container."
(when (null args)
(format t "Usage: neko inspect <name-or-id>~%")
(uiop:quit 1))
(let ((name (first args)))
(handler-case
(let ((info (nekod.docker:inspect-container name)))
(format t "~a~%" info))
(error (e)
(format t "Error inspecting ~a: ~a~%" name e)))))
;; ---- Intent commands --------------------------------------------------------
(defun cmd-intent (args)
"Compile and execute a natural language intent."
(when (null args)
(format t "Usage: neko intent <string>~%")
(format t "Example: neko intent deploy the api server~%")
(uiop:quit 1))
(let ((intent-string (format nil "~{~a~^ ~}" args)))
(format t "~%Intent: ~s~%" intent-string)
(multiple-value-bind (bytes ast reason)
(nekod.llm2asm:safe-compile-intent intent-string)
(if bytes
(progn
(format t "AST type: ~a~%" (nekod.llm2asm:infra-ast-type ast))
(format t "AST target: ~a~%" (nekod.llm2asm:infra-ast-target ast))
(format t "Byte count: ~a~%" (length bytes))
(format t "Bytes: ~{#x~2,'0x~^ ~}~%" (coerce bytes 'list))
(format t "~%JIT-linking...~%")
(nekod.llm2asm:jit-link bytes (nekod.llm2asm:infra-ast-target ast))
(format t "Executed.~%"))
(format t "Compile failed: ~a~%" reason)))))
(defun cmd-compile (args)
"Compile a natural language intent to ASM bytes (no execution)."
(when (null args)
(format t "Usage: neko compile <string>~%")
(uiop:quit 1))
(let ((intent-string (format nil "~{~a~^ ~}" args)))
(format t "~%Intent: ~s~%" intent-string)
(multiple-value-bind (bytes ast reason)
(nekod.llm2asm:safe-compile-intent intent-string)
(if bytes
(progn
(format t "AST type: ~a~%" (nekod.llm2asm:infra-ast-type ast))
(format t "Byte count: ~a~%" (length bytes))
(format t "~%Hex dump:~%")
(loop for i from 0 below (length bytes)
do (format t "~2,'0x " (aref bytes i))
(when (= (mod (1+ i) 16) 0) (format t "~%")))
(format t "~%"))
(format t "Compile failed: ~a~%" reason)))))
;; ---- Policy commands --------------------------------------------------------
(defun cmd-policy (args)
"Policy management commands."
(let ((sub (first args))
(rest-args (rest args)))
(cond
((or (null sub) (string= sub "list"))
(format t "Active policies: (policy engine not yet wired to CLI)~%"))
((string= sub "check")
(when (< (length rest-args) 2)
(format t "Usage: neko policy check <policy1> <policy2>~%")
(uiop:quit 1))
(format t "Policy conflict check: ~a vs ~a~%"
(first rest-args) (second rest-args))
(format t "(Policy conflict detection: load the regex-math module)~%"))
(t
(format t "Unknown policy sub-command: ~a~%" sub)
(uiop:quit 1)))))
;; ---- Evolutionary commands --------------------------------------------------
(defun cmd-evolve (args)
"Run evolutionary optimization."
(let ((n (if args
(parse-integer (first args) :junk-allowed t)
1)))
(format t "Running ~a evolutionary generation~p...~%" n n)
(when (fboundp 'nekod.selfmod:evolve)
(let ((pop (symbol-value 'nekod.selfmod:*population*))
(telemetry (list :uptime 0.9 :cpu 0.4 :memory 0.3)))
(if pop
(let ((new-pop (nekod.selfmod:evolve pop telemetry)))
(setf (symbol-value 'nekod.selfmod:*population*) new-pop)
(format t "Generation ~a complete. Population: ~a~%"
(symbol-value 'nekod.selfmod:*generation*)
(length new-pop)))
(format t "Population is empty. Load container specs first.~%"))))))
;; ---- Snapshot commands ------------------------------------------------------
(defun cmd-snap (args)
"State snapshot management."
(let ((sub (first args))
(rest-args (rest args)))
(cond
((or (null sub) (string= sub "list"))
(if (fboundp 'nekod.selfmod:list-snapshots)
(let ((snaps (nekod.selfmod:list-snapshots)))
(if snaps
(dolist (s snaps)
(format t " ~a: gen ~a, ~a containers~%"
(getf s :id)
(getf s :generation)
(getf s :containers)))
(format t "No snapshots.~%")))
(format t "(selfmod not loaded)~%")))
((string= sub "save")
(let ((label (or (first rest-args) "manual")))
(if (fboundp 'nekod.selfmod:capture-state)
(let ((id (nekod.selfmod:capture-state (list :label label))))
(format t "Saved snapshot ~a (~s)~%" id label))
(format t "(selfmod not loaded)~%"))))
((string= sub "restore")
(when (null rest-args)
(format t "Usage: neko snap restore <id>~%")
(uiop:quit 1))
(let ((id (parse-integer (first rest-args) :junk-allowed t)))
(if (fboundp 'nekod.selfmod:restore-state)
(nekod.selfmod:restore-state id)
(format t "(selfmod not loaded)~%"))))
(t
(format t "Unknown snap sub-command: ~a~%" sub)
(uiop:quit 1)))))
;; ---- Version ----------------------------------------------------------------
(defun cmd-version ()
(format t "Nekomata ~a~%" nekod:*nekomata-version*)
(format t "Common Lisp: ~a ~a~%"
(lisp-implementation-type)
(lisp-implementation-version)))
|