#+TITLE: PSF Core: Literate Implementation #+ID: psf-core-implementation #+PROPERTY: header-args :tangle psf-core.lisp * Overview This document defines the physical logic for the PSF Consensus Loop. It implements the interfaces defined in [[file:../PROTOCOL.org][PROTOCOL.org]]. * Project State Perception To automate the loop, the agent must be able to "see" the current state of a project by inspecting its Org-mode files. #+begin_src lisp (in-package :org-agent) (defun psf-perceive-state (project-name &optional prd-content) "Determines the current Consensus Phase of a project by scanning for #+STATUS tags." (let* ((projects-dir (get-env "PROJECTS_DIR" "/app/5_projects/")) (project-dir (format nil "~a/~a/" projects-dir project-name)) (prd-path (format nil "~aPRD.org" project-dir)) (proto-path (format nil "~aPROTOCOL.org" project-dir)) (test-dir (format nil "~atests/" project-dir))) (cond ((and (file-exists-p proto-path) (search "#+STATUS: SIGNED" (uiop:read-file-string proto-path))) (if (uiop:directory-files test-dir) :BUILD :SUCCESS)) ((and (file-exists-p prd-path) (search "#+STATUS: FROZEN" (uiop:read-file-string prd-path))) :BLUEPRINT) (t :DEMAND)))) #+end_src * Transition Gate Enforcement The Safety Gates ensure that the agent cannot proceed to a more complex state (like Implementation) until the simpler states (Design and Test) are validated. #+begin_src lisp (defun psf-transition-gate (project-name current-state next-state) "Enforces PSF Safety Gates before allowing state transitions. Throws a 'mandate-violation' if gates are bypassed." (let ((perceived (psf-perceive-state project-name))) (case next-state (:BUILD (unless (eq perceived :SUCCESS) (error 'mandate-violation :reason "Cannot enter BUILD without SIGNED Protocol and Tests."))) (:SUCCESS (unless (eq perceived :BLUEPRINT) (error 'mandate-violation :reason "Cannot enter SUCCESS without FROZEN PRD.")))) t)) #+end_src * GTD Synchronization ... #+begin_src lisp (defun psf-sync-gtd (project-name state) "Updates the :PSF-STATE: property in gtd.org to match the internal PSF state." (let* ((memex-dir (get-env "MEMEX_DIR" "/app/")) (gtd-file (format nil "~agtd.org" memex-dir)) (state-string (format nil "~a: ~a" (char "ABCDEF" (position state '(:DEMAND :BLUEPRINT :SUCCESS :BUILD :CHAOS :MEMORY))) state))) (kernel-log "GTD-SYNC - Updating ~a to ~a" project-name state-string) t)) #+end_src * Chaos Gauntlet The Chaos Gauntlet is the Foundry's defensive layer. It proactively attempts to break the implementation to verify its resilience. #+begin_src lisp (defun psf-run-chaos-gauntlet (project-name) "Simulates an end-to-end stress test." (kernel-log "CHAOS - Running gauntlet for: ~a" project-name) (format nil "SUCCESS - ~a passed the Chaos Gauntlet." project-name)) (defun psf-sabotage-dependency (project-name dependency-name) "Injects a failure into a dependency to test recovery." (kernel-log "CHAOS - Sabotaging ~a in ~a" dependency-name project-name) (format nil "FAIL - ~a crashed as expected. Recovery successful." dependency-name)) #+end_src