;;; ai-memory -- guix.scm ;;; ;;; Development environment and package definitions for ai-memory, plus its ;;; dependencies that are not in Guix: the Odin compiler and the odin-sqlite3 ;;; bindings. ;;; ;;; Development shell with `odin', `clang' and the SQLite bindings on PATH: ;;; ;;; guix shell -D -f guix.scm -- odin version ;;; ;;; ai-memory links against SQLite, so building it needs a library collection ;;; (for "sqlite3:sqlite3"), two #config defines and a search path for ;;; libsqlite3. Inside the shell, use the flags the package build uses: ;;; ;;; odin build . -out:bin/ai-memory -vet -strict-style \ ;;; -collection:sqlite3=$GUIX_ENVIRONMENT/share/odin-sqlite3 \ ;;; -define:SQLITE3_SYSTEM_LIB=true -define:SQLITE3_DYNAMIC_LIB=true \ ;;; "-extra-linker-flags:-L$GUIX_ENVIRONMENT/lib -Wl,-rpath,$GUIX_ENVIRONMENT/lib" ;;; ;;; Build and install the project: ;;; ;;; guix build -f guix.scm ;;; ;;; Build only a dependency (useful while hacking on a package): ;;; ;;; guix build -f guix.scm -e '(@ (guix) odin)' ;;; guix build -f guix.scm -e '(@ (guix) odin-sqlite3)' ;;; ;;; Note: guix does not substitute Odin, so the first build compiles clang and ;;; LLVM if no substitutes are available for them. (define-module (guix) #:use-module ((guix licenses) #:prefix license:) #:use-module (guix build-system copy) #:use-module (guix build-system gnu) #:use-module (guix gexp) #:use-module (guix git-download) #:use-module (guix packages) #:use-module (guix utils) #:use-module (gnu packages bash) #:use-module (gnu packages code) #:use-module (gnu packages guile) #:use-module (gnu packages guile-xyz) #:use-module (gnu packages llvm) #:use-module (gnu packages sqlite) #:use-module (gnu packages version-control)) (define %source-dir (dirname (current-filename))) ;; Machine state in the working tree that must not leak into the build. (define (source-file? file stat) (not (member file (list (string-append %source-dir "/.git") (string-append %source-dir "/bin") (string-append %source-dir "/ai-memory.sqlite") (string-append %source-dir "/ai-memory.sqlite-wal") (string-append %source-dir "/ai-memory.sqlite-shm"))))) ;; Odin is not packaged in Guix, so build the compiler here. Upstream's ;; build_odin.sh supports LLVM 17 through 22 and uses clang as both the ;; bootstrap C++ compiler and (at run time) the linker driver; keep these ;; inputs in sync with what src/build_odin.sh accepts. (define-public odin (let ((commit "9a6991685ddf663956d9836602eb421e141fc443") (revision "0")) (package (name "odin") (version (git-version "dev-2026-11a" revision commit)) (source (origin (method git-fetch) (uri (git-reference (url "https://github.com/odin-lang/Odin") (commit commit))) (file-name (git-file-name name version)) (sha256 (base32 "0qd1mbad3j4409ylhhrndr5i5nc6im3s3q4dvqqlwnv68qg66wq4")))) (build-system gnu-build-system) (inputs (list bash-minimal ;for the wrappers clang-toolchain-22 ;C++ bootstrap compiler and linker driver llvm-22)) ;code generation (arguments (list #:modules `((guix build gnu-build-system) (guix build utils) (ice-9 popen) (ice-9 rdelim) (srfi srfi-13)) #:phases #~(modify-phases %standard-phases (delete 'configure) (replace 'build ;; This reproduces the "release" branch of upstream's ;; build_odin.sh for Linux. The script itself is not used ;; because it derives the version string from the build date, ;; which is not reproducible, and because it probes for brew, ;; llvm-config-$version and the like. (lambda* (#:key inputs #:allow-other-keys) (define llvm-config (search-input-file inputs "/bin/llvm-config")) (define (llvm-flags . args) ;; --libfiles prints one file per line, the rest is on a ;; single line; flatten it all into a list of arguments. (let* ((port (apply open-pipe* OPEN_READ llvm-config args)) (lines (let loop ((line (read-line port)) (acc '())) (if (eof-object? line) (reverse acc) (loop (read-line port) (cons line acc))))) (status (close-pipe port))) (unless (zero? status) (error "llvm-config failed" args status)) (string-tokenize (string-join lines " ")))) (apply invoke "clang++" (append (list "src/main.cpp" "src/libtommath.cpp" "-o" "odin" "-std=c++14" "-O3" "-pthread" "-Wno-switch" "-Wno-macro-redefined" "-Wno-unused-value" (string-append "-DODIN_VERSION_RAW=\"" #$version "\"")) (llvm-flags "--cxxflags" "--ldflags") (list "-lstdc++" "-ldl" "-lm") (llvm-flags "--libs" "core" "native" "passes" "arm" "aarch64" "x86" "webassembly" "riscv" "--system-libs" "--libfiles"))))) (replace 'check ;; There is no make-based test suite; use upstream's own smoke ;; test: compile and run examples/demo. (lambda* (#:key tests? #:allow-other-keys) (when tests? (invoke "./odin" "version") (invoke "./odin" "run" "examples/demo" "-vet" "-strict-style" "--" "Hellope" "World")))) (replace 'install (lambda* (#:key inputs outputs #:allow-other-keys) (define llvm-config (search-input-file inputs "/bin/llvm-config")) (define libdir (string-trim-both (let* ((port (open-pipe* OPEN_READ llvm-config "--libdir")) (line (read-line port)) (status (close-pipe port))) (unless (zero? status) (error "llvm-config failed" "--libdir" status)) line))) (define root (string-append #$output "/lib/odin")) (install-file "odin" (string-append #$output "/bin")) ;; The compiler looks up `base' and `core' through ODIN_ROOT ;; and finds `vendor' below it. (for-each (lambda (dir) (copy-recursively dir (string-append root "/" dir))) '("base" "core" "vendor")) (wrap-program (string-append #$output "/bin/odin") `("ODIN_ROOT" ":" prefix (,root)) ;; odin(1) shells out to clang to link programs. `("PATH" ":" prefix (,#$(file-append (this-package-input "clang-toolchain") "/bin"))) ;; libLLVM is linked dynamically. `("LD_LIBRARY_PATH" ":" prefix (,libdir)))))))) (home-page "https://odin-lang.org") (synopsis "Compiler for the Odin programming language") (description "Odin is a general-purpose programming language with distinct typing, built for high performance, modern systems, and data-oriented programming. This package provides @command{odin}, the compiler and its standard library collections (@code{core} and @code{vendor}).") (license license:zlib)))) ;; Odin bindings for SQLite3. Upstream also vendors a prebuilt libsqlite3.a; ;; it is deliberately not installed. The bindings are instead used with ;; SQLITE3_SYSTEM_LIB, so programs link the packaged SQLite3 below. (define-public odin-sqlite3 (let ((commit "0666d2d4af9efe32a7a7c26184536e8c3be79b3b") (revision "0")) (package (name "odin-sqlite3") (version (git-version "0" revision commit)) (source (origin (method git-fetch) (uri (git-reference (url "https://github.com/saenai255/odin-sqlite3") (commit commit))) (file-name (git-file-name name version)) (sha256 (base32 "0nnyppg08x28p097lb1xlkxwl2w0m3b9amv142jxpr8mg22wz636")))) (build-system copy-build-system) (arguments (list ;; Consumed as `-collection:sqlite3=', so the package keeps its ;; own directory: Odin sources import "sqlite3:sqlite3" and ;; "sqlite3:addons". #:install-plan #~'(("sqlite.odin" "share/odin-sqlite3/sqlite3/") ("addons" "share/odin-sqlite3/sqlite3/addons")))) (home-page "https://github.com/saenai255/odin-sqlite3") (synopsis "Odin bindings to the SQLite3 C API") (description "Odin bindings for the SQLite3 C API, covering most of the C interface, plus an addon package with higher-level query, execute and prepare helpers built on Odin reflection. Build with @code{-define:SQLITE3_SYSTEM_LIB=true} to link the system SQLite3 instead of a bundled copy.") (license license:expat)))) (define-public ai-memory (package (name "ai-memory") (version "0.1.0") ;; While developing, build the working tree. Once this package is ;; released, switch to a git-fetch or url-fetch origin. ;; The working tree, minus the local build output (bin/) and the local ;; store, which are machine state rather than source. (source (local-file %source-dir #:recursive? #t #:select? source-file?)) (build-system gnu-build-system) ;; libsqlite3.so is linked at run time; the topic index (scripts/index.scm) ;; is a Guile program, because libxapian is C++-only and guile-xapian ;; already has the C++ shim that a foreign call would otherwise need; ;; universal-ctags writes the C/C++ declarations that no diff line can. (inputs (list sqlite guile-3.0 guile-xapian guile-sqlite3 universal-ctags)) ;; git drives the whole derived half of the store, so it is a runtime ;; dependency too -- but it belongs to the user's environment, not to this ;; profile, which is why it is only a native input for the check phase. (native-inputs (list odin odin-sqlite3 git)) (arguments (list #:phases #~(let* ((sqlite #$(this-package-input "sqlite")) (bindings #$(file-append (this-package-native-input "odin-sqlite3") "/share/odin-sqlite3")) ;; odin-sqlite3 asks for "system:libsqlite3.so"; odin links that ;; as -l:libsqlite3.so, so it needs both a search path at link ;; time and an rpath at run time. (flags (list "-vet" "-strict-style" (string-append "-collection:sqlite3=" bindings) ;; ctags is compiled in rather than exported by ;; wrap-program: a wrapper's export would overwrite ;; $AI_MEMORY_CTAGS, so a user could not point at ;; their own ctags build. (string-append "-define:CTAGS_PATH=" #$(this-package-input "universal-ctags") "/bin/ctags") "-define:SQLITE3_SYSTEM_LIB=true" "-define:SQLITE3_DYNAMIC_LIB=true" (string-append "-extra-linker-flags:-L" sqlite "/lib -Wl,-rpath," sqlite "/lib")))) (modify-phases %standard-phases (delete 'configure) (replace 'build (lambda _ (mkdir-p "bin") (apply invoke "odin" "build" "." "-out:bin/ai-memory" flags))) (replace 'check (lambda* (#:key tests? #:allow-other-keys) (when tests? (mkdir-p "bin") (apply invoke "odin" "build" "." "-out:bin/ai-memory-check" flags) ;; Links and runs; fail loudly if libsqlite3 is missing. (invoke "./bin/ai-memory-check") ;; The topic index end to end: a store with one entry, the ;; Guile indexer over it, and a prefixed query -- which is ;; what proves the script's dependencies are complete. (mkdir-p "tmp") (setenv "AI_MEMORY_DB" (string-append (getcwd) "/tmp/store.sqlite")) (setenv "AI_MEMORY_INDEX_SCRIPT" (string-append (getcwd) "/scripts/index.scm")) (setenv "GUILE" (string-append #$(this-package-input "guile") "/bin/guile")) ;; guile-xapian needs guile-lib (snippets) and the sqlite ;; binding; both come from the package inputs, so the check ;; fails here rather than in a user's first `update-index'. (setenv "GUILE_LOAD_PATH" (string-append #$(this-package-input "guile-xapian") "/share/guile/site/3.0:" #$(this-package-input "guile-sqlite3") "/share/guile/site/3.0")) (invoke "./bin/ai-memory" "init") ;; A session id is what gives the entry its topic: terms ;; ("topic-index" -> topic:topic and topic:index), so the ;; query below is a real concept query, not a text match. ;; The gist is given separately from the body on purpose: ;; it is what titles a reasoning document and it is indexed ;; as text, so a claim written only in the gist must still ;; answer a search. (Before that was true, an entry with no ;; commit was indexed with an empty title and its gist was ;; not searchable at all.) (invoke "./bin/ai-memory" "record" "--kind" "conclusion" "--session" "topic-index" "--gist" "gisttext names the document" "--body" "the index answers topic: queries") (invoke "./bin/ai-memory" "--index" "tmp/index" "update-index") ;; The reply is JSON on stdout: pipe it into grep rather ;; than capturing it (with-output-to-string captures the ;; Scheme port, not the child's stdout). (invoke "sh" "-c" (string-append "./bin/ai-memory --index tmp/index" " search topic:index" " | grep -q '\"count\":1'")) (invoke "sh" "-c" (string-append "./bin/ai-memory --index tmp/index" " search gisttext" " | grep -q '\"count\":1'")) (invoke "sh" "-c" (string-append "./bin/ai-memory --index tmp/index" " search topic:index" " | grep -q" " '\"title\":\"gisttext names the document\"'")) ;; C and C++ declarations, end to end: they come from ctags ;; over the file blobs, diffed between a commit and its ;; parent, so only a real (throwaway) repository can prove ;; it. The same file gains `dec' in the second commit: ;; `dec' must be an add, and `inc' -- whose body changed but ;; whose declaration did not -- must not be. (mkdir-p "tmp/cpp") (invoke "git" "init" "-q" "tmp/cpp") (call-with-output-file "tmp/cpp/kern.cpp" (lambda (out) (display "int inc(int a) {\n return a + 1;\n}\n" out))) (invoke "git" "-C" "tmp/cpp" "add" "kern.cpp") (invoke "git" "-C" "tmp/cpp" "-c" "user.name=check" "-c" "user.email=check@example" "commit" "-qm" "add inc") (call-with-output-file "tmp/cpp/kern.cpp" (lambda (out) (display (string-append "int inc(int a) {\n int one = 1;\n return a + one;\n}\n" "\nint dec(int a) {\n return a - 1;\n}\n") out))) (invoke "git" "-C" "tmp/cpp" "-c" "user.name=check" "-c" "user.email=check@example" "commit" "-qam" "add dec") (invoke "./bin/ai-memory" "-C" "tmp/cpp" "--db" "tmp/cpp/store.sqlite" "init") (invoke "./bin/ai-memory" "-C" "tmp/cpp" "--db" "tmp/cpp/store.sqlite" "import") (invoke "sh" "-c" (string-append "./bin/ai-memory -C tmp/cpp --db" " tmp/cpp/store.sqlite why dec" " | grep -q 'fn kern.cpp.dec, add'")) (invoke "sh" "-c" (string-append "./bin/ai-memory -C tmp/cpp --db" " tmp/cpp/store.sqlite why inc" " | grep -q 'fn kern.cpp.inc, add'")) (invoke "sh" "-c" (string-append "./bin/ai-memory -C tmp/cpp --db" " tmp/cpp/store.sqlite why inc" " | grep -q 'fn kern.cpp.inc, context'")) ;; Python and Ruby are line-scanned, which is also the only ;; thing that can attribute a file that was just *added*: git ;; has no before-side for it, so its hunks carry no context. (call-with-output-file "tmp/cpp/script.py" (lambda (out) (display "import os\n\ndef helper(x):\n return x\n" out))) (call-with-output-file "tmp/cpp/script.rb" (lambda (out) (display "module Util\n def self.twice(x)\n x * 2\n end\nend\n" out))) (invoke "git" "-C" "tmp/cpp" "add" "script.py" "script.rb") (invoke "git" "-C" "tmp/cpp" "-c" "user.name=check" "-c" "user.email=check@example" "commit" "-qm" "add scripts") (invoke "./bin/ai-memory" "-C" "tmp/cpp" "--db" "tmp/cpp/store.sqlite" "sync") (invoke "sh" "-c" (string-append "./bin/ai-memory -C tmp/cpp --db" " tmp/cpp/store.sqlite why helper" " | grep -q 'fn script.helper, add'")) (invoke "sh" "-c" (string-append "./bin/ai-memory -C tmp/cpp --db" " tmp/cpp/store.sqlite why twice" " | grep -q 'fn script.twice, add'")) ;; The substring lookup: `why script' reaches both files. (invoke "sh" "-c" (string-append "./bin/ai-memory -C tmp/cpp --db" " tmp/cpp/store.sqlite why script" " | grep -q 'fn script.helper, add'"))))) (replace 'install (lambda _ (install-file "bin/ai-memory" (string-append #$output "/bin")) ;; The indexer travels with the binary: index.odin looks for ;; ../share/ai-memory/index.scm beside it. (install-file "scripts/index.scm" (string-append #$output "/share/ai-memory")) ;; LICENSE lands in share/doc/ai-memory-/ from the ;; standard install-license-files phase. (install-file "README.md" (string-append #$output "/share/doc/ai-memory")) (install-file "doc/SKILL.md" (string-append #$output "/share/doc/ai-memory")) (wrap-program (string-append #$output "/bin/ai-memory") `("GUILE" = (,(string-append #$(this-package-input "guile") "/bin/guile"))) `("GUILE_LOAD_PATH" = (,(string-append #$(this-package-input "guile-xapian") "/share/guile/site/3.0:" #$(this-package-input "guile-sqlite3") "/share/guile/site/3.0")))))))))) (home-page "https://github.com/ai-memory/ai-memory") (synopsis "Memory for AI assistants") (description "ai-memory is a store for facts that an AI assistant should remember across sessions. It keeps them in a SQLite database.") ;; BSD 3-Clause, see LICENSE. (license license:bsd-3))) ai-memory