Files
passepartout/skills/org-skill-memory-archivist.org

6.4 KiB

SKILL: Memory Archivist (Universal Literate Note)

Overview

The Memory Archivist provides long-term, decentralized, and immutable history for the agent's knowledge graph. It leverages IPFS to achieve "Sovereignty Above All," ensuring that memory state is preserved across space and time.

Implementation

Package Context

(in-package :org-agent)

Serialization (archivist-serialize-store)

Converts the live `*object-store*` into a JSON-compatible list of alists.

(defun archivist-serialize-store ()
  "Serializes the entire object-store for archival."
  (let ((objects nil))
    (maphash (lambda (id obj)
               (declare (ignore id))
               (push `((:id . ,(org-object-id obj))
                       (:type . ,(format nil "~s" (org-object-type obj)))
                       (:attributes . ,(loop for (k v) on (org-object-attributes obj) by #'cddr 
                                             collect (cons (format nil "~a" k) (format nil "~a" v))))
                       (:content . ,(org-object-content obj))
                       (:parent-id . ,(org-object-parent-id obj))
                       (:children . ,(org-object-children obj))
                       (:version . ,(org-object-version obj))
                       (:last-sync . ,(org-object-last-sync obj))
                       (:hash . ,(org-object-hash obj)))
                     objects))
             *object-store*)
    objects))

IPFS Integration (archivist-push-to-ipfs)

Pushes the serialized knowledge graph to the local IPFS daemon.

(defun archivist-push-to-ipfs ()
  "Serializes the store and pushes it to IPFS, returning the CID."
  (let* ((data (archivist-serialize-store))
         (json-payload (cl-json:encode-json-to-string data))
         (ipfs-url "http://127.0.0.1:5001/api/v0/add"))
    (handler-case
        (let* ((response (dex:post ipfs-url 
                                   :content `(("file" . ,json-payload))
                                   :headers '(("Content-Type" . "multipart/form-data"))))
               (result (cl-json:decode-json-from-string response))
               (cid (cdr (assoc :hash result))))
          (kernel-log "ARCHIVIST - Memory checkpointed to IPFS. CID: ~a" cid)
          cid)
      (error (c)
        (kernel-log "ARCHIVIST ERROR - IPFS push failed: ~a" c)
        nil))))

Restoration (archivist-pull-from-ipfs)

Fetches a knowledge graph image from IPFS and hydrates the `*object-store*`.

(defun archivist-pull-from-ipfs (cid)
  "Fetches data from IPFS by CID and restores the object-store."
  (let ((ipfs-url (format nil "http://127.0.0.1:5001/api/v0/cat?arg=~a" cid)))
    (handler-case
        (let* ((response (dex:post ipfs-url))
               (data (cl-json:decode-json-from-string response)))
          (clrhash *object-store*)
          (dolist (item data)
            (let* ((id (cdr (assoc :id item)))
                   (obj (make-org-object 
                         :id id
                         :type (read-from-string (cdr (assoc :type item)))
                         :attributes (loop for attr in (cdr (assoc :attributes item))
                                           append (list (intern (string-upcase (car attr)) :keyword) (cdr attr)))
                         :content (cdr (assoc :content item))
                         :parent-id (cdr (assoc :parent-id item))
                         :children (cdr (assoc :children item))
                         :version (cdr (assoc :version item))
                         :last-sync (cdr (assoc :last-sync item))
                         :hash (cdr (assoc :hash item)))))
              (setf (gethash id *object-store*) obj)))
          (kernel-log "ARCHIVIST - Knowledge graph restored from IPFS CID: ~a" cid)
          t)
      (error (c)
        (kernel-log "ARCHIVIST ERROR - Restoration failed: ~a" c)
        nil))))

Cognitive Tools

Expose archival capabilities to System 1.

(def-cognitive-tool :ipfs-checkpoint "Creates an immutable snapshot of the current knowledge graph on IPFS."
  :parameters nil
  :body (lambda (args)
          (declare (ignore args))
          (let ((cid (archivist-push-to-ipfs)))
            (if cid
                (format nil "Checkpoint success. CID: ~a" cid)
                "Checkpoint failed. Ensure IPFS daemon is running."))))

(def-cognitive-tool :ipfs-restore "Restores the entire knowledge graph from a specific IPFS CID."
  :parameters ((:cid :type :string :description "The IPFS CID to restore from"))
  :body (lambda (args)
          (let ((cid (getf args :cid)))
            (if (archivist-pull-from-ipfs cid)
                (format nil "Restoration successful from ~a" cid)
                "Restoration failed."))))

Skill Definition

(defskill :skill-memory-archivist
  :priority 80
  :trigger (lambda (ctx) (eq (getf (getf ctx :payload) :command) :checkpoint-ipfs))
  :neuro (lambda (ctx) "Propose an IPFS checkpoint if the user wants decentralized persistence.")
  :symbolic (lambda (action ctx) 
              (let ((cid (archivist-push-to-ipfs)))
                (if cid
                    `(:target :system :payload (:action :message :text ,(format nil "IPFS Checkpoint: ~a" cid)))
                    `(:target :system :payload (:action :message :text "IPFS Checkpoint failed."))))))

Phase E: Chaos (Verification)

The Memory Archivist must be verified for serialization integrity.

(defpackage :org-agent-archivist-tests
  (:use :cl :fiveam :org-agent))
(in-package :org-agent-archivist-tests)

(def-suite archivist-suite :description "Tests for IPFS Archival.")
(in-suite archivist-suite)

(test test-serialization-integrity
  "Verify that the object-store can be serialized and partially reconstructed."
  (clrhash org-agent::*object-store*)
  (ingest-ast '(:type :HEADLINE :properties (:ID "test-id" :TITLE "Test Node") :raw-content "Body Text" :contents nil))
  (let* ((data (org-agent::archivist-serialize-store))
         (first-item (first data)))
    (is (= 1 (length data)))
    (is (equal "test-id" (cdr (assoc :id first-item))))
    (is (equal "Body Text" (cdr (assoc :content first-item))))))