feat(arch): implement 'Code as Thought' architecture and formalize PSF Consensus Loop

This commit is contained in:
2026-03-31 13:03:26 -04:00
parent 5a9129132e
commit 1712b1e4a9
114 changed files with 3652 additions and 2581 deletions

View File

@@ -0,0 +1,36 @@
#+TITLE: PRD: Skill - Scribe Agent
#+STATUS: FROZEN
#+AUTHOR: Agent
#+CREATED: [2026-03-31 Tue 13:45]
* 1. Purpose
The Scribe Agent is the automated distillation and auditing engine for the PSF. Its primary goal is to prevent "context rot" by transforming daily captures into atomic notes and ensuring project compliance with PSF standards.
* 2. User Needs
** 2.1 Knowledge Distillation
As a user (Amr), I need my ephemeral daily thoughts transformed into a structured knowledge base.
- The Scribe MUST extract core concepts from `$MEMEX_DAILY`.
- It MUST generate normalized atomic notes in `$MEMEX_NOTES`.
- It MUST preserve provenance via `Source:` backlinks.
** 2.2 Incremental Processing
I need the system to be efficient and avoid redundant work.
- The Scribe MUST use Git state tracking (commit hashes) to identify only new content.
- It MUST maintain state in a Lisp-native format (`scribe-state.lisp`) for full system introspection.
** 2.3 PSF Mandate Audit
I need my foundry projects to maintain high integrity.
- The Scribe MUST audit active projects for `PRD.org`, `PROTOCOL.org`, and Literate Programming blocks.
- It MUST flag "Mandate Violations" in `institutional-memory.org`.
** 2.4 Autonomous Execution
I need the Scribe to run reliably without manual intervention.
- The skill must be compatible with cron-based triggers.
- It must handle path normalization via environment variables.
* 3. Success Criteria
- [ ] **Distillation Accuracy:** Scribe identifies a concept in a daily log and creates a correctly formatted atomic note.
- [ ] **Provenance Check:** Atomic notes contain a valid `Source:` link back to the daily file.
- [ ] **Audit Trigger:** Scribe correctly identifies a project missing a `PROTOCOL.org` and records the violation.
- [ ] **State Persistence:** `distillation-state.json` is updated after every successful run.

View File

@@ -0,0 +1,48 @@
#+TITLE: PROTOCOL: Skill - Scribe Agent
#+STATUS: SIGNED
#+AUTHOR: Architect-Agent
#+CREATED: [2026-03-31 Tue 13:50]
#+SIGNED: [2026-03-31 Tue 13:55] Agent (Architect)
* 1. Architectural Intent
This protocol defines the shared Lisp interfaces for the Scribe skill. It ensures that the distillation of ephemeral thoughts and the auditing of foundry integrity are performed deterministically and with clear provenance.
Following the **Literate Mandate**, the Scribe skill's implementation must be tangled from its Org-mode source.
* 2. Semantic Interfaces
** 2.1 State Perception
#+begin_src lisp
(defun scribe-scan-for-knowledge-gaps ()
"Uses 'git diff' against the last processed commit hash to identify new content in $MEMEX_DAILY.
Returns a list of daily files with new content."
)
#+end_src
** 2.2 Concept Distillation
#+begin_src lisp
(defun scribe-distill-concept (daily-path concept-meta)
"Transforms a raw capture into an atomic note.
Input: (:title \"Concept Title\" :content \"Body text\" :source \"daily/2026-03-31.org\")
Output: Path to the new note in $MEMEX_NOTES."
)
#+end_src
** 2.3 Mandate Auditing
#+begin_src lisp
(defun scribe-audit-foundry-mandate (project-name)
"Checks a project for compliance with PSF Level 3 standards.
Checks: PRD.org exists, PROTOCOL.org exists, src/ contains tangled blocks.
Returns a list of violations or NIL if compliant."
)
#+end_src
** 2.4 Integrity Reporting
#+begin_src lisp
(defun scribe-record-violation (project-name violation-type)
"Appends a Mandate Violation entry to notes/institutional-memory.org."
)
#+end_src
* 3. Integration with Cron and Git
The Scribe runs as an asynchronous process. It MUST update `scribe-state.lisp` (containing a Lisp alist) and perform a Git commit after each successful batch.

View File

@@ -0,0 +1,52 @@
;;;; scribe-engine.lisp --- Knowledge distillation and mandate auditing logic.
;;;; This file is TANGLED from org-skill-scribe.org. DO NOT EDIT MANUALLY.
(defpackage :org-skill-scribe
(:use :cl :uiop :cl-ppcre :local-time)
(:export #:scribe-scan-for-knowledge-gaps
#:scribe-distill-concept
#:scribe-audit-foundry-mandate
#:scribe-update-state))
(in-package :org-skill-scribe)
(defun scribe-scan-for-knowledge-gaps ()
"Uses 'git diff' to identify new daily captures using Lisp-native state."
(let* ((state-file (or (uiop:getenv "SCRIBE_STATE") "scribe-state.lisp"))
(state (if (uiop:file-exists-p state-file)
(with-open-file (in state-file) (read in))
'((:last-commit . "HEAD~1"))))
(last-hash (cdr (assoc :last-commit state))))
(uiop:run-program (list "git" "diff" "--name-only" last-hash "HEAD" "--" (or (uiop:getenv "MEMEX_DAILY") "daily/"))
:output :lines)))
(defun scribe-update-state (new-hash)
"Serializes the new state alist to disk."
(let ((state-file (or (uiop:getenv "SCRIBE_STATE") "scribe-state.lisp")))
(with-open-file (out state-file :direction :output :if-exists :supersede)
(print `((:last-commit . ,new-hash)
(:last-run . ,(local-time:format-timestring nil (local-time:now))))
out))))
(defun scribe-distill-concept (daily-path concept-meta)
"Creates an atomic note with snake_case filename and Source: backlink."
(let* ((title (getf concept-meta :title))
(content (getf concept-meta :content))
(source (getf concept-meta :source))
(filename (format nil "~a.org" (cl-ppcre:regex-replace-all " " (string-downcase title) "_")))
(target-path (format nil "~a/~a" (or (uiop:getenv "MEMEX_NOTES") "notes") filename)))
(with-open-file (out target-path :direction :output :if-exists :supersede)
(format out "#+TITLE: ~a~%#+ID: ~a~%~%Source: [[file:~a]]~%~%~a"
title (uiop:read-file-string "/proc/sys/kernel/random/uuid") source content))
target-path))
(defun scribe-audit-foundry-mandate (project-name)
"Audits a project for PRD, PROTOCOL, and Literate src/ structure."
(let ((project-dir (format nil "~a/~a/" (or (uiop:getenv "PROJECTS_DIR") "projects") project-name))
(violations '()))
(unless (uiop:file-exists-p (format nil "~aPRD.org" project-dir))
(push :missing-prd violations))
(unless (uiop:file-exists-p (format nil "~aPROTOCOL.org" project-dir))
(push :missing-protocol violations))
violations))

View File

@@ -0,0 +1,42 @@
import os
import re
def simulate_distill(title, content, source):
filename = title.lower().replace(" ", "_") + ".org"
target_path = os.path.join("notes", filename)
# Mocking the note creation
note_content = f"#+TITLE: {title}\n#+ID: mock-id\n\nSource: [[file:{source}]]\n\n{content}"
# In simulation, we just verify the content structure
if f"Source: [[file:{source}]]" in note_content and title in note_content:
return target_path
return None
def simulate_audit(project_path):
violations = []
if not os.path.exists(os.path.join(project_path, "PRD.org")):
violations.append("MISSING_PRD")
if not os.path.exists(os.path.join(project_path, "PROTOCOL.org")):
violations.append("MISSING_PROTOCOL")
return violations
if __name__ == "__main__":
# Test 1: Distillation
path = simulate_distill("Lisp Sovereignty", "Control the code.", "daily/2026-03-31.org")
print(f"--- Test: Distillation ---")
print(f"Target Path: {path}")
print(f"Status: {'PASS' if path == 'notes/lisp_sovereignty.org' else 'FAIL'}")
# Test 2: Audit (Current Project)
print(f"\n--- Test: Audit (org-skill-scribe) ---")
violations = simulate_audit("projects/org-skill-scribe")
print(f"Violations: {violations}")
print(f"Status: {'PASS' if not violations else 'FAIL'}")
# Test 3: Audit (A broken project if exists)
# We'll just mock a non-existent one
print(f"\n--- Test: Audit (Non-existent) ---")
violations = simulate_audit("projects/missing-project")
print(f"Violations: {violations}")
print(f"Status: {'PASS' if 'MISSING_PRD' in violations else 'FAIL'}")

View File

@@ -0,0 +1,49 @@
;;; TDD Suite: org-skill-scribe (Distillation & Audit)
;;; Status: RED
;;; Author: Tech-Analyst-Agent
;;; Created: [2026-03-31 Tue 14:00]
(defpackage :org-skill-scribe-tests
(:use :cl :fiveam :org-skill-scribe))
(in-package :org-skill-scribe-tests)
(def-suite scribe-pipeline-suite
:description "Tests for the Scribe distillation and audit pipeline.")
(in-suite scribe-pipeline-suite)
;;; --- 2.1 State Perception Tests ---
(test scan-for-knowledge-gaps
"Ensure the scribe correctly identifies new daily files via Git."
;; Requires mock Git environment
(skip "Mock Git environment required."))
;;; --- 2.2 Concept Distillation Tests ---
(test distill-concept-with-backlink
"Ensure a concept is transformed into an atomic note with provenance."
(let ((daily-path "/tmp/scribe-daily.org")
(concept '(:title "Lisp Machine Mandate"
:content "The system must be fully introspectable."
:source "daily/2026-03-31.org")))
(let ((note-path (scribe-distill-concept daily-path concept)))
(is (cl-ppcre:scan "lisp_machine_mandate.org" note-path))
(is (cl-ppcre:scan "Source: \\[\\[file:daily/2026-03-31.org\\]\\]"
(uiop:read-file-string note-path))))))
;;; --- 2.3 Mandate Auditing Tests ---
(test audit-compliant-project
"Ensure a project with PRD and PROTOCOL passes the audit."
(let ((project-name "compliant-project"))
;; Logic to scaffold a mock compliant project
(is (null (scribe-audit-foundry-mandate project-name)))))
(test audit-missing-protocol
"Ensure a project missing a PROTOCOL.org is flagged."
(let ((project-name "broken-project"))
;; Logic to scaffold a mock project missing protocol
(let ((violations (scribe-audit-foundry-mandate project-name)))
(is (member :missing-protocol violations)))))