Files
passepartout/literate/context.org
Amr Gharbeia dd3873cd5e
Some checks failed
Deploy-Agent-V15-Stdin / JOB-V15-STDIN (push) Failing after 2s
DOCS: Systematic overhaul of Literate source (Granularity & Technical Reasoning)
2026-04-21 11:49:58 -04:00

4.2 KiB

Peripheral Vision (context.lisp)

Peripheral Vision (context.lisp)

Architectural Intent: Contextual Awareness

The Context stage (often referred to as "Peripheral Vision") is responsible for assembling the situational awareness that the Probabilistic Engine needs to make informed decisions.

In most agent frameworks, context is provided as a massive, unstructured text dump of recent chat history. OpenCortex takes a more sophisticated approach:

  1. Foveal Focus: The data immediately relevant to the current task (e.g., the specific Org headline being edited).
  2. Peripheral Awareness: Low-resolution metadata about the rest of the Memex (e.g., list of active projects, recent system logs, current time/location).
  3. Semantic Retrieval: Utilizing vector embeddings to pull in semantically related nodes from the long-term memory.

By balancing these three layers, we provide the agent with a "Wide Angle" view of the user's life without overflowing the LLM's context window.

Pipeline Initialization

(in-package :opencortex)

Awareness Assembly

Project Awareness (context-get-active-projects)

Identifies current active work by querying the Org Memory for nodes with the :PROJECT: tag or NEXT status.

(defun context-get-active-projects ()
  "Retrieves a list of project headlines currently marked as NEXT or in progress."
  (let ((all-projects (list-objects-with-attribute :CATEGORY "Project")))
    (loop for p in all-projects
          collect (list :id (org-object-id p) 
                        :title (getf (org-object-attributes p) :TITLE)))))

Historical Awareness (context-get-recent-completed-tasks)

Provides short-term memory of what was recently achieved, allowing the agent to maintain continuity.

(defun context-get-recent-completed-tasks (&optional (limit 5))
  "Retrieves the last N tasks marked as DONE from the memory history."
  (let ((all-completed (list-objects-with-attribute :TODO "DONE")))
    (subseq (sort all-completed #'> :key #'org-object-version)
            0 (min limit (length all-completed)))))

Skill Awareness (context-list-all-skills)

Allows the agent to understand its own capabilities by listing the human-readable descriptions of all loaded Literate Skills.

(defun context-list-all-skills ()
  "Returns a list of registered skills and their documentation."
  (let ((results nil))
    (maphash (lambda (id skill)
               (push (list :id id :name (skill-name skill)) results))
             *skills-registry*)
    results))

System Awareness (context-get-system-logs)

Crucial for self-debugging. Provides the agent with the internal logs so it can explain why a previous action failed or was blocked by a Bouncer.

(defun context-get-system-logs ()
  "Retrieves the in-memory circular log buffer."
  (bt:with-lock-held (*logs-lock*)
    (format nil "~{~a~%~}" (reverse *system-logs*))))

Global Context Generation

Awareness Assembly (context-assemble-global-awareness)

This function acts as the "Contextual Conductor." It synthesizes the various awareness layers into a single, high-signal string suitable for the LLM system prompt.

(defun context-assemble-global-awareness ()
  "Assembles the full context block for a neural request."
  (let ((projects (context-get-active-projects))
        (time (multiple-value-bind (s m h d mo y) (get-decoded-time) (format nil "~a-~a-~a ~a:~a:~a" y mo d h m s))))
    (format nil "CURRENT_TIME: ~a. ACTIVE_PROJECTS: ~s. FOVEAL_FOCUS: ~a" 
            time 
            projects 
            (or *foveal-focus-id* "None"))))

Semantic Context Query (context-query-store)

A hook for future vector-based retrieval. In the MVP, it performs a simple keyword search over the Memory graph.

(defun context-query-store (query &key (limit 5))
  "Placeholder for semantic/vector search over the Memex."
  (declare (ignore query limit))
  nil)