about summary refs log tree commit diff
path: root/scripts
diff options
context:
space:
mode:
authorPjotr <wrk@bosa.lan>2026-11-12 13:56:50 +0000
committerPjotr <wrk@bosa.lan>2026-11-12 13:56:50 +0000
commit051faf04d59bf45fde128df9d3c4b88c24f289f3 (patch)
treeb131223eb148c23f3d89b9c4bfa558c22a749644 /scripts
parent3fab7adf0ec147dceb150d861fdbd5f5b3693384 (diff)
downloadai-memory-051faf04d59bf45fde128df9d3c4b88c24f289f3.tar.gz
index: a commit inherits the topics of the reasoning attached to it
topic:pve answered with nine reasoning entries and no commits, which is
backwards for anyone following the story: the pve thread is carried by the
commits (cd3e65b, 64e63bd, 3a42b5f, 8e11ec0) as much as by the notes.

Commit documents now take the sessions of the reasoning attached to them
(one GROUP_CONCAT query over reasoning, joined in memory) and gain the same
S<session> and O<topic> terms, so the same query returns 13 documents
instead of 9.

Also documents the honest limit of session-derived topics in doc/SKILL.md:
a session named gemma-wrapper-diagnostics about the same work does not
answer topic:pve, while a plain-text pve search still finds it. topic: is
the curated answer, free text is the recall answer -- name a session after
the concept you want it found by.
Diffstat (limited to 'scripts')
-rw-r--r--scripts/index.scm61
1 files changed, 48 insertions, 13 deletions
diff --git a/scripts/index.scm b/scripts/index.scm
index ac83850..87e32f8 100644
--- a/scripts/index.scm
+++ b/scripts/index.scm
@@ -113,6 +113,9 @@
 (define (repo-name store)
   (basename (dirname (canonicalize-path store))))
 
+(define (alist-set table key value)
+  (cons (cons key value) table))
+
 (define (non-empty-string? value)
   (and (string? value) (not (string-null? value))))
 
@@ -228,12 +231,20 @@
                    (field 'text (string-append title " " (or body "")))))))
     (else (die "unexpected reasoning row: ~s" row))))
 
-(define (commit-record repo row)
+(define (commit-record repo row sessions)
   (match row
     ((hash subject body author_ts)
      (let* ((key (format #f "commit:~a" hash))
             (date (yyyymmdd author_ts))
-            (subject (or subject "")))
+            (subject (or subject ""))
+            ;; A commit inherits the topics of the reasoning attached to it:
+            ;; otherwise the pve story answers with its reasoning and not with
+            ;; the commits that carry it, which is exactly backwards for a
+            ;; reader following it.
+            (topics (append-map (lambda (session)
+                                  (map (lambda (word) (term "O" word))
+                                       (string-split session #\-)))
+                                sessions)))
        (list key
              (list (field 'type "commit")
                    (field 'key key)
@@ -241,17 +252,35 @@
                    (field 'date date)
                    (field 'commit hash)
                    (field 'title subject)
-                   (field 'terms (list (term "T" "commit")
-                                       (term "R" repo)
-                                       (term "D" (number->string date))
-                                       (term "C" (substring hash 0 7))))
+                   (field 'session (if (null? sessions) "" (car sessions)))
+                   (field 'terms (append (list (term "T" "commit")
+                                               (term "R" repo)
+                                               (term "D" (number->string date))
+                                               (term "C" (substring hash 0 7)))
+                                         (map (lambda (session) (term "S" session))
+                                              sessions)
+                                         topics))
                    (field 'body (or body ""))
                    (field 'text (string-append subject " " (or body "")))))))
     (else (die "unexpected commit row: ~s" row))))
 
-(define (records repo reasoning commits)
-  (append (map (lambda (row) (reasoning-record repo row)) reasoning)
-          (map (lambda (row) (commit-record repo row)) commits)))
+(define (sessions-by-commit attached)
+  (fold (lambda (row table)
+          (match row
+            ((commit_hash sessions)
+             (alist-set table commit_hash
+                        (if sessions (string-split sessions #\,) (list))))
+            (else table)))
+        '()
+        attached))
+
+(define (records repo reasoning commits attached)
+  (let ((sessions (sessions-by-commit attached)))
+    (append (map (lambda (row) (reasoning-record repo row)) reasoning)
+            (map (lambda (row)
+                   (commit-record repo row
+                                  (or (assoc-ref sessions (first row)) (list))))
+                 commits))))
 
 ;;; ---------------------------------------------------------------------
 ;;; Writing
@@ -310,9 +339,15 @@
                                LEFT JOIN commits c ON c.hash = r.commit_hash
                                ORDER BY r.id"))
           (commits (query db "SELECT hash, subject, COALESCE(body, ''), author_ts
-                              FROM commits ORDER BY author_ts")))
+                              FROM commits ORDER BY author_ts"))
+          ;; Which sessions' reasoning is attached to which commit, so a commit
+          ;; document can carry those sessions' topics.
+          (attached (query db "SELECT commit_hash, GROUP_CONCAT(DISTINCT session_id)
+                               FROM reasoning
+                               WHERE commit_hash IS NOT NULL AND session_id <> ''
+                               GROUP BY commit_hash")))
       (sqlite-close db)
-      (values reasoning commits))))
+      (values reasoning commits attached))))
 
 ;;; ---------------------------------------------------------------------
 ;;; Commands
@@ -325,9 +360,9 @@
     (unless index (die "update needs --index DIR"))
     (unless (file-exists? store) (die "no store at ~a" store))
     (call-with-values (lambda () (store-rows store))
-      (lambda (reasoning commits)
+      (lambda (reasoning commits attached)
         (let* ((repo (repo-name store))
-               (records (records repo reasoning commits))
+               (records (records repo reasoning commits attached))
                (db (open-writable index rebuild))
                (stemmer (new-Stem "english"))
                (written 0))