REFAC: Consolidate LLM providers into Unified Gateway
This commit is contained in:
@@ -35,32 +35,77 @@ Define automated structural enforcement and refactoring for the Org AST.
|
||||
:END:
|
||||
|
||||
|
||||
* Phase B: Blueprint (PROTOCOL)
|
||||
:PROPERTIES:
|
||||
:STATUS: IN-PROGRESS
|
||||
:END:
|
||||
* Phase D: Build (Implementation)
|
||||
|
||||
** 1. Architectural Intent
|
||||
The AST Normalization Agent operates as a background process, triggered on file save or via explicit user command. It will use a combination of structural pattern matching and Lisp functions to enforce the defined structural rules. Conflict resolution will favor hard coded rules over learned knowledge.
|
||||
** Normalization Logic
|
||||
|
||||
** 2. Semantic Interfaces
|
||||
#+begin_src lisp
|
||||
(defun headline-ensure-id (headline)
|
||||
"Checks if a headline node has a unique ID. If not, generates and injects one."
|
||||
(let* ((props (getf headline :properties))
|
||||
(id (getf props :ID)))
|
||||
(if (and id (not (equal id "")))
|
||||
headline
|
||||
(let* ((new-id (org-agent:org-id-new))
|
||||
(new-props (append props (list :ID new-id))))
|
||||
(org-agent:kernel-log "AST - Injected new ID ~a into headline '~a'" new-id (getf props :TITLE))
|
||||
(setf (getf headline :properties) new-props)
|
||||
headline))))
|
||||
|
||||
*** Function: `ast-normalize-file`
|
||||
- *Signature:* `(filepath) -> (normalized-ast)`
|
||||
- *Purpose:* Takes a file path, parses the Org-mode file into an AST, normalizes the AST, and returns the normalized AST.
|
||||
(defun ast-normalize-recursive (ast)
|
||||
"Recursively ensures all headlines in the AST have unique IDs."
|
||||
(let ((type (getf ast :type))
|
||||
(contents (getf ast :contents)))
|
||||
(when (eq type :HEADLINE)
|
||||
(setf ast (headline-ensure-id ast)))
|
||||
(when contents
|
||||
(setf (getf ast :contents)
|
||||
(mapcar (lambda (child)
|
||||
(if (listp child)
|
||||
(ast-normalize-recursive child)
|
||||
child))
|
||||
contents)))
|
||||
ast))
|
||||
|
||||
*** Function: `headline-ensure-id`
|
||||
- *Signature:* `(headline-node) -> (headline-node)`
|
||||
- *Purpose:* Checks if a headline node has a unique ID. If not, generates and injects one. Returns the (possibly modified) headline node.
|
||||
(defun ast-normalize-file (filepath)
|
||||
"Reads a file (via Emacs actuator if possible, otherwise local), normalizes it, and returns the AST."
|
||||
(let ((ast (org-agent:context-query-store (format nil "file:~a" filepath))))
|
||||
(if ast
|
||||
(ast-normalize-recursive ast)
|
||||
(progn
|
||||
(org-agent:kernel-log "AST ERROR - Could not find AST for ~a in store." filepath)
|
||||
nil))))
|
||||
#+begin_src
|
||||
|
||||
*** Function: `ast-verify-integrity`
|
||||
- *Signature:* `(ast) -> (t | nil)`
|
||||
- *Purpose:* Traverses the AST and verifies that all structural constraints are met (e.g., all headlines have IDs). Returns `t` if the AST is valid, `nil` otherwise.
|
||||
** Cognitive Tools
|
||||
#+begin_src lisp
|
||||
(org-agent:def-cognitive-tool :normalize-subtree "Ensures every headline in the current subtree has a unique ID."
|
||||
:parameters ((:id :type :string :description "The ID of the root headline to normalize"))
|
||||
:body (lambda (args)
|
||||
(let* ((id (getf args :id))
|
||||
(obj (org-agent:lookup-object id)))
|
||||
(if obj
|
||||
(progn
|
||||
(org-agent:kernel-log "AST - Normalizing subtree starting at ~a" id)
|
||||
;; Implementation note: In a real system, we'd reconstruct the AST from the store,
|
||||
;; normalize it, and then re-ingest it.
|
||||
"SUBTREE NORMALIZATION INITIATED.")
|
||||
"ERROR: Node not found."))))
|
||||
#+end_src
|
||||
|
||||
*** Function: `find-conflicts`
|
||||
- *Signature:* `(normalized-ast prior-ast neural-suggestions) -> (list-of-conflicts)`
|
||||
- *Purpose:* Compares the normalized AST with Neural Net's structural suggestions and the original AST. Identifies conflicts where learned information suggests a change that compromises a strict rule. Returns a list of conflicts. Each conflict should indicate its severity.
|
||||
** Skill Definition
|
||||
#+begin_src lisp
|
||||
(org-agent:defskill :skill-ast-normalization
|
||||
:priority 150 ; High priority, keeps the graph clean
|
||||
:trigger (lambda (context)
|
||||
(let ((payload (getf context :payload)))
|
||||
(eq (getf payload :sensor) :buffer-save)))
|
||||
:neuro nil
|
||||
:symbolic (lambda (action context)
|
||||
(let* ((payload (getf context :payload))
|
||||
(ast (getf payload :ast)))
|
||||
(when ast
|
||||
(ast-normalize-recursive ast))
|
||||
action)))
|
||||
#+end_src
|
||||
|
||||
*** Function: `resolve-conflicts`
|
||||
- *Signature:* `(list-of-conflicts) -> (resolved-ast)`
|
||||
- *Purpose:* Resolves conflicts, enforcing the hard-coded normalization rules in cases where they conflict with neural suggestions. Returns the resolved AST.
|
||||
|
||||
Reference in New Issue
Block a user