about summary refs log tree commit diff
path: root/guix.scm
blob: 0a0885bd4c52a284dbaf8803fc4ce9a613269ab6 (plain)
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
;;; 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=<prefix>', 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-<version>/ 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