# eriktmueller/daydreamer architecture

> Architecture diagram of the GitHub repository eriktmueller/daydreamer, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/eriktmueller/daydreamer
- Repository: https://github.com/eriktmueller/daydreamer

## Overview

1. **Repository purpose**
   This repo implements DAYDREAMER, a Common Lisp goal-based agent that turns goals and input events into a narrated stream of inference, planning, emotion, and action. It is not a single monolith; it is split into a reusable reasoning kernel and the DAYDREAMER agent built on top of it.

2. **Two main layers: GATE and DAYDREAMER**
   The `gate_*.cl` files are the generic reasoning/representation engine. The `dd_*.cl` files are the DAYDREAMER-specific agent logic that uses GATE for types, objects, unification, proof, and context management. This separation is the key architectural boundary in the repo.

3. **Core runtime model: contexts and sprouting**
   The system operates over nested “contexts” (`CX`) rather than a flat global state. A new plan or subgoal typically “sprouts” a new context from the current one, carrying forward relevant facts while adding goal-specific structure. The trace in the README shows this as `CX.4 -> CX.5 -> CX.6`, which is central to how planning is isolated and reversible.

4. **Knowledge representation and inference**
   GATE provides the symbolic backbone: types, objects, facts, rules, instantiation, and unification. The files `gate_ty.cl`, `gate_obs.cl`, `gate_unify.cl`, `gate_instan.cl`, `gate_prove.cl`, and `gate_read_pr.cl` indicate a rule/proof engine that can parse and execute logical forms, match them against current context, and assert/retract derived beliefs or goals.

5. **DAYDREAMER agent behavior**
   The DAYDREAMER layer adds the agent’s “mind”: control loop, episodic memory, emotion handling, goal management, daydreaming modes, reversal/rationalization behavior, and domain-specific rules. Files like `dd_cntrl.cl`, `dd_epis.cl`, `dd_night.cl`, `dd_reversal.cl`, `dd_mutation.cl`, `dd_rule1.cl`, and `dd_rule2.cl` suggest distinct behavioral subsystems rather than one planner.

6. **Goal lifecycle**
   The main flow is: infer unmet needs or triggers, activate a top-level concern, select a plan, decompose into subgoals, perform actions, then mark the goal succeeded or failed. The README’s trace shows explicit statuses like `HALTED`, `RUNABLE`, `WAITING`, `SUCCEEDED-GOAL`, and the control loop re-evaluates concerns based on motivation and emotional state.

7. **Emotion as control signal**
   Emotions are not decorative output; they are part of the decision mechanism. Goals can create motivating emotions, and successful completion can generate positive emotions and remove prior motivation. This means the architecture couples belief/planning state to affect state, with emotion influencing which concern becomes the active focus.

8. **Planning and action execution**
   Plans are rule-driven. A high-level goal can be rewritten into a lower-level subgoal, and then into an action goal with preconditions. The README examples show this chain clearly: `ENTERTAINMENT -> M-MOVIE`, or `HACK -> FTP -> RTRUE`. The architecture therefore has a hierarchical planner plus an action layer that only performs “real action” when in performance mode.

9. **Episodic memory and learning**
   After goal completion, the agent stores episodes, links them to the rules that produced them, and indexes them by objects, emotions, and goals. `dd_epis.cl` is likely the memory store/retrieval subsystem, allowing future planning to reuse prior episodes or support analogical reasoning and serendipitous recall.

10. **Language generation and narrated traces**
   `dd_gen.cl` is the English generator. It converts asserted facts, goal changes, and completed actions into human-readable narration, which is why the runtime output is a detailed prose trace rather than a silent state machine. This is a separate subsystem from reasoning: the agent reasons symbolically, then verbalizes the results.

11. **Initialization and knowledge base**
   `dd_kb.cl` and the sample input files (`inputlovers*.txt`, `inputemployment1.txt`, `inputrecovery3.txt`, etc.) define the initial world model, personal goals, and scenario-specific facts. The README’s examples show that loading `dd` or `dd_get` starts from a prepared initial reality context with built-in needs and themes.

12. **Build/load and test structure**
   The repo supports both interpreted loading and compiled operation. `gate_compile.cl` and `dd_compile.cl` are the compilation entry points, while `gate_get.cl` and `dd_get.cl` are the loadable runtime entry points. `gate_test.cl` is the test suite, and files like `hello_world.cl` demonstrate how to extend the system by defining new types, needs, rules, and generation templates.

13. **External interaction boundary**
   The only “interface” is a Lisp REPL plus textual inputs/outputs. Inputs are either scripted scenario files or parser-entered concepts; outputs are the generated English trace and the console state log. There is no evidence of a web/UI layer, database, or network service in this repo.

14. **How to draw the architecture**
   A useful diagram would show: REPL/load scripts on the left, GATE reasoning core in the middle, DAYDREAMER agent logic on top of it, and a loop connecting inference → goal activation → planning → action/performance → episodic memory → generation. The main boundary is between the generic symbolic engine (`gate_*`) and the agent-specific behavioral layer (`dd_*`).

Main entry points: [`gate_main.cl`](https://github.com/eriktmueller/daydreamer/blob/master/gate_main.cl) (Kernel core), [`dd_cntrl.cl`](https://github.com/eriktmueller/daydreamer/blob/master/dd_cntrl.cl) (Control loop), [`dd.cl`](https://github.com/eriktmueller/daydreamer/blob/master/dd.cl) (Agent core), [`gate_prove.cl`](https://github.com/eriktmueller/daydreamer/blob/master/gate_prove.cl) (Prove)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_interface["Interface"]
  node_dd_get["DD load<br/>runtime entry<br/>[dd_get.cl]"]
  node_dd_compile["DD compile<br/>build entry<br/>[dd_compile.cl]"]
  node_gate_get["GATE load<br/>runtime entry<br/>[gate_get.cl]"]
  node_gate_compile["GATE compile<br/>build entry<br/>[gate_compile.cl]"]
end

subgraph group_gate["GATE Core"]
  node_gate_main{{"Kernel core<br/>reasoning hub<br/>[gate_main.cl]"}}
  node_gate_cx[("Contexts<br/>context model<br/>[gate_cx.cl]")]
  node_gate_ty["Types<br/>type system<br/>[gate_ty.cl]"]
  node_gate_obs["Objects<br/>facts<br/>[gate_obs.cl]"]
  node_gate_unify{{"Unify<br/>matcher<br/>[gate_unify.cl]"}}
  node_gate_instan["Instantiate<br/>substitution<br/>[gate_instan.cl]"]
  node_gate_prove{{"Prove<br/>inference<br/>[gate_prove.cl]"}}
  node_gate_read_pr["Parser<br/>reader<br/>[gate_read_pr.cl]"]
end

subgraph group_daydreamer["DAYDREAMER"]
  node_dd{{"Agent core<br/>agent layer<br/>[dd.cl]"}}
  node_dd_kb[("Knowledge base<br/>initial world<br/>[dd_kb.cl]")]
  node_dd_cntrl{{"Control loop<br/>agent control<br/>[dd_cntrl.cl]"}}
  node_dd_epis[("Episodic memory<br/>memory store<br/>[dd_epis.cl]")]
  node_dd_gen["Generator<br/>narration<br/>[dd_gen.cl]"]
  node_dd_rule1["Rules 1<br/>behavior rules<br/>[dd_rule1.cl]"]
  node_dd_rule2["Rules 2<br/>behavior rules<br/>[dd_rule2.cl]"]
  node_dd_night["Night mode<br/>state mode<br/>[dd_night.cl]"]
  node_dd_reversal["Reversal<br/>metacontrol<br/>[dd_reversal.cl]"]
  node_dd_mutation["Mutation<br/>adaptation<br/>[dd_mutation.cl]"]
end

node_dd_get -->|"loads"| node_dd
node_dd_compile -->|"builds"| node_dd
node_gate_get -->|"loads"| node_gate_main
node_gate_compile -->|"builds"| node_gate_main
node_dd -->|"uses"| node_gate_main
node_gate_main -->|"manages"| node_gate_cx
node_gate_main -->|"defines"| node_gate_ty
node_gate_main -->|"stores"| node_gate_obs
node_gate_main -->|"matches"| node_gate_unify
node_gate_main -->|"instantiates"| node_gate_instan
node_gate_main -->|"proves"| node_gate_prove
node_gate_main -->|"parses"| node_gate_read_pr
node_dd -->|"starts from"| node_dd_kb
node_dd -->|"runs"| node_dd_cntrl
node_dd -->|"narrates"| node_dd_gen
node_dd -->|"records"| node_dd_epis
node_dd_cntrl -->|"sprouts"| node_gate_cx
node_dd_cntrl -->|"queries"| node_gate_prove
node_dd_cntrl -->|"applies"| node_dd_rule1
node_dd_cntrl -->|"applies"| node_dd_rule2
node_dd_cntrl -->|"switches"| node_dd_night
node_dd_cntrl -->|"revises"| node_dd_reversal
node_dd_cntrl -->|"adapts"| node_dd_mutation
node_dd_rule1 -->|"matches"| node_gate_unify
node_dd_rule2 -->|"derives"| node_gate_prove
node_dd_epis -->|"reports"| node_dd_gen

click node_dd_get "https://github.com/eriktmueller/daydreamer/blob/master/dd_get.cl"
click node_dd_compile "https://github.com/eriktmueller/daydreamer/blob/master/dd_compile.cl"
click node_gate_get "https://github.com/eriktmueller/daydreamer/blob/master/gate_get.cl"
click node_gate_compile "https://github.com/eriktmueller/daydreamer/blob/master/gate_compile.cl"
click node_gate_main "https://github.com/eriktmueller/daydreamer/blob/master/gate_main.cl"
click node_gate_cx "https://github.com/eriktmueller/daydreamer/blob/master/gate_cx.cl"
click node_gate_ty "https://github.com/eriktmueller/daydreamer/blob/master/gate_ty.cl"
click node_gate_obs "https://github.com/eriktmueller/daydreamer/blob/master/gate_obs.cl"
click node_gate_unify "https://github.com/eriktmueller/daydreamer/blob/master/gate_unify.cl"
click node_gate_instan "https://github.com/eriktmueller/daydreamer/blob/master/gate_instan.cl"
click node_gate_prove "https://github.com/eriktmueller/daydreamer/blob/master/gate_prove.cl"
click node_gate_read_pr "https://github.com/eriktmueller/daydreamer/blob/master/gate_read_pr.cl"
click node_dd "https://github.com/eriktmueller/daydreamer/blob/master/dd.cl"
click node_dd_kb "https://github.com/eriktmueller/daydreamer/blob/master/dd_kb.cl"
click node_dd_cntrl "https://github.com/eriktmueller/daydreamer/blob/master/dd_cntrl.cl"
click node_dd_epis "https://github.com/eriktmueller/daydreamer/blob/master/dd_epis.cl"
click node_dd_gen "https://github.com/eriktmueller/daydreamer/blob/master/dd_gen.cl"
click node_dd_rule1 "https://github.com/eriktmueller/daydreamer/blob/master/dd_rule1.cl"
click node_dd_rule2 "https://github.com/eriktmueller/daydreamer/blob/master/dd_rule2.cl"
click node_dd_night "https://github.com/eriktmueller/daydreamer/blob/master/dd_night.cl"
click node_dd_reversal "https://github.com/eriktmueller/daydreamer/blob/master/dd_reversal.cl"
click node_dd_mutation "https://github.com/eriktmueller/daydreamer/blob/master/dd_mutation.cl"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_dd_get,node_dd_compile,node_gate_get,node_gate_compile toneBlue
class node_gate_main,node_gate_cx,node_gate_ty,node_gate_obs,node_gate_unify,node_gate_instan,node_gate_prove,node_gate_read_pr toneAmber
class node_dd,node_dd_kb,node_dd_cntrl,node_dd_epis,node_dd_gen,node_dd_rule1,node_dd_rule2,node_dd_night,node_dd_reversal,node_dd_mutation toneMint
```

## Components

### Interface

Load/compile entry points and REPL-facing scripts.

- [DD load](https://github.com/eriktmueller/daydreamer/blob/master/dd_get.cl): runtime entry · Loadable entry point for the DAYDREAMER agent. · `dd_get.cl`
- [DD compile](https://github.com/eriktmueller/daydreamer/blob/master/dd_compile.cl): build entry · Compilation entry point for the DAYDREAMER layer. · `dd_compile.cl`
- [GATE load](https://github.com/eriktmueller/daydreamer/blob/master/gate_get.cl): runtime entry · Loadable entry point for the generic GATE kernel. · `gate_get.cl`
- [GATE compile](https://github.com/eriktmueller/daydreamer/blob/master/gate_compile.cl): build entry · Compilation entry point for the GATE kernel. · `gate_compile.cl`

### GATE Core

Generic symbolic reasoning kernel for contexts, types, matching, proof, and parsing.

- [Kernel core](https://github.com/eriktmueller/daydreamer/blob/master/gate_main.cl): reasoning hub · Top-level orchestration for the symbolic reasoning engine. · `gate_main.cl`
- [Contexts](https://github.com/eriktmueller/daydreamer/blob/master/gate_cx.cl): context model · Nested context structure used for sprouting, scoping, and reversible reasoning. · `gate_cx.cl`
- [Types](https://github.com/eriktmueller/daydreamer/blob/master/gate_ty.cl): type system · Symbolic type hierarchy and representation backbone. · `gate_ty.cl`
- [Objects](https://github.com/eriktmueller/daydreamer/blob/master/gate_obs.cl): facts · Object and fact representation shared by rules and inference. · `gate_obs.cl`
- [Unify](https://github.com/eriktmueller/daydreamer/blob/master/gate_unify.cl): matcher · Unification and symbolic matching for logical forms. · `gate_unify.cl`
- [Instantiate](https://github.com/eriktmueller/daydreamer/blob/master/gate_instan.cl): substitution · Builds concrete instances from abstract logical structures. · `gate_instan.cl`
- [Prove](https://github.com/eriktmueller/daydreamer/blob/master/gate_prove.cl): inference · Rule execution and proof search over the current context. · `gate_prove.cl`
- [Parser](https://github.com/eriktmueller/daydreamer/blob/master/gate_read_pr.cl): reader · Reads and interprets logical forms for the proof engine. · `gate_read_pr.cl`

### DAYDREAMER

Agent-specific control, knowledge, emotion, memory, and narration built on GATE.

- [Agent core](https://github.com/eriktmueller/daydreamer/blob/master/dd.cl): agent layer · DAYDREAMER-specific behavior built on the generic GATE kernel. · `dd.cl`
- [Knowledge base](https://github.com/eriktmueller/daydreamer/blob/master/dd_kb.cl): initial world · Built-in scenario facts, goals, and starting state. · `dd_kb.cl`
- [Control loop](https://github.com/eriktmueller/daydreamer/blob/master/dd_cntrl.cl): agent control · Chooses the active concern, evaluates status, and drives the goal lifecycle. · `dd_cntrl.cl`
- [Episodic memory](https://github.com/eriktmueller/daydreamer/blob/master/dd_epis.cl): memory store · Stores and retrieves episodes, outcomes, and learned associations. · `dd_epis.cl`
- [Generator](https://github.com/eriktmueller/daydreamer/blob/master/dd_gen.cl): narration · Turns internal state changes into English narration. · `dd_gen.cl`
- [Rules 1](https://github.com/eriktmueller/daydreamer/blob/master/dd_rule1.cl): behavior rules · Primary agent rules for goal rewrites and behavior selection. · `dd_rule1.cl`
- [Rules 2](https://github.com/eriktmueller/daydreamer/blob/master/dd_rule2.cl): behavior rules · Additional rule set for planning, emotion, and action selection. · `dd_rule2.cl`
- [Night mode](https://github.com/eriktmueller/daydreamer/blob/master/dd_night.cl): state mode · Non-daytime or idle-mode behavior and transitions. · `dd_night.cl`
- [Reversal](https://github.com/eriktmueller/daydreamer/blob/master/dd_reversal.cl): metacontrol · Rationalization and reversal behavior when outcomes change or need reinterpretation. · `dd_reversal.cl`
- [Mutation](https://github.com/eriktmueller/daydreamer/blob/master/dd_mutation.cl): adaptation · Transforms goals or plans when conditions require adjustment. · `dd_mutation.cl`

## Connections

- DD load → Agent core: loads · Runtime entry loads the DAYDREAMER layer.
- DD compile → Agent core: builds · Compilation entry builds the DAYDREAMER layer.
- GATE load → Kernel core: loads · Runtime entry loads the GATE kernel.
- GATE compile → Kernel core: builds · Compilation entry builds the GATE kernel.
- Agent core → Kernel core: uses · DAYDREAMER delegates symbolic work to the generic kernel.
- Kernel core → Contexts: manages · The kernel organizes reasoning around nested contexts.
- Kernel core → Types: defines · Kernel core depends on the type system.
- Kernel core → Objects: stores · Kernel core works over objects and facts.
- Kernel core → Unify: matches · Unification supports inference and rule firing.
- Kernel core → Instantiate: instantiates · Abstract forms are turned into concrete instances.
- Kernel core → Prove: proves · Proof search drives symbolic inference.
- Kernel core → Parser: parses · Logical input is read into the reasoning engine.
- Agent core → Knowledge base: starts from · The agent boots from its built-in knowledge base.
- Agent core → Control loop: runs · The control loop is the agent's main runtime behavior.
- Agent core → Generator: narrates · The agent verbalizes internal state changes and outcomes.
- Agent core → Episodic memory: records · Episodes are stored for later reuse and recall.
- Control loop → Contexts: sprouts · Planning and subgoaling occur by creating new contexts.
- Control loop → Prove: queries · Control asks the proof engine what follows next.
- Control loop → Rules 1: applies · Primary behavioral rules shape goal activation and rewriting.
- Control loop → Rules 2: applies · Secondary rule set supports planning and action selection.
- Control loop → Night mode: switches · Control can move into a night/idle behavioral mode.
- Control loop → Reversal: revises · Control can rationalize or reverse prior conclusions.
- Control loop → Mutation: adapts · Control can mutate goals and plans to fit current conditions.
- Rules 1 → Unify: matches · Rules rely on unification to bind goal patterns.
- Rules 2 → Prove: derives · Rules feed the inference engine with goal rewrites.
- Episodic memory → Generator: reports · Memory can be surfaced through narration.
