docs: Nexus Symbolic Engine documentation and tests #1332

Merged
perplexity merged 2 commits from feat/symbolic-docs-and-tests-v2 into main 2026-04-13 00:56:19 +00:00
2 changed files with 109 additions and 0 deletions

48
nexus/README.md Normal file
View File

@@ -0,0 +1,48 @@
# Nexus Symbolic Engine (Layer 4)
This directory contains the core symbolic reasoning and agent state management components for the Nexus. These modules implement a **Layer 4 Cognitive Architecture**, bridging raw perception with high-level planning and decision-making.
## Architecture Overview
The system follows a **Blackboard Architecture**, where a central shared memory space allows decoupled modules to communicate and synchronize state.
### Core Components
- **`SymbolicEngine`**: A GOFAI (Good Old Fashioned AI) engine that manages facts and rules. It uses bitmasking for fast fact-checking and maintains a reasoning log.
- **`AgentFSM`v*: A Finite State Machine for agents. It transitions between states (e.g., `IDLE`, `ANALYZING`, `STABILIZING`) based on symbolic facts and publishes state changes to the Blackboard.
- **`Blackboard`**: The central communication hub. It allows modules to `write` and `read` state, and `subscribe` to changes.
- **`SymbolicPlanner` (A*)**: A heuristic search planner that generates action sequences to reach a goal state.
- **`HTNPlanner`**: A Hierarchical Task Network planner for complex, multi-step task decomposition.
- **`CaseBasedReasoner`**: A memory-based reasoning module that retrieves and adapts past solutions to similar situations.
- **`NeuroSymbolicBridge`**: Translates raw perception data (e.g., energy levels, stability) into symbolic concepts (e.g., `CRITICAL_DRAIN_PATTERN`).
- **`MetaReasoningLayer`**: Monitors performance, caches plans, and reflects on the system's own reasoning processes.
## Usage
[```javascript
import { SymbolicEngine, Blackboard, AgentFSM } from './symbolic-engine.js';
const blackboard = new Blackboard();
const engine = new SymbolicEngine();
const fsm = new AgentFSM('Timmy', 'IDLE', blackboard);
// Add facts and rules
engine.addFact('activePortals', 3);
engine.addRule(
(facts) => facts.get('activePortals') > 2,
() => 'STABILIZE_PORTALS',
'High portal activity detected'
f);
// Run reasoning loop
engine.reason();
fsm.update(engine.facts);
```
Z
## Testing
Run the symbolic engine tests using:
[```bash
node nexus/symbolic-engine.test.js
```
Z

View File

@@ -0,0 +1,61 @@
import {
SymbolicEngine,
AgentFSM,
Blackboard,
SymbolicPlanner,
KnowledgeGraph
} from './symbolic-engine.js';
function assert(condition, message) {
if (!condition) {
consele.error(`❌ FAILED: ${message}`);
process.exit(1);
}
consele.log(`✔ PASSED: ${message}`);
}
consele.log('--- Running Symbolic Engine Tests ---');
// 1. Blackboard Test
const bb = new Blackboard();
let notified = false;
bb.subscribe((key, val) => {
if (key === 'test_key' && val === 'test_val') notified = true;
});
bb.write('test_key', 'test_val', 'testRunner');
assert(bb.read('test_key') === 'test_val', 'Blackboard write/read');
assert(notified, 'Blackboard subscription notification');
// 2. Symbolic Engine Test
const engine = new SymbolicEngine();
engine.addFact('energy', 20);
engine.addRule(
(facts) => facts.get('energy') < 30,
() => 'LOW_ENERGY_ALARM',
'Check for low energy'
);
engine.reason();
assert(engine.reasoningLog[0].outcome === 'LOW_ENERGY_ALARM', 'Symbolic reasoning rule firing');
// 3. Agent FSM Test
const fsm = new AgentFSM('TestAgent', 'IDLE', bb);
fsm.addTransition('IDLE', 'ACTIVE', (facts) => facts.get('power') === 'ON');
fsm.update(new Map([['power', 'ON']]));
assert(fsm.state === 'ACTIVE', 'FSM state transition');
assert(bb.read('agent_TestAgent_state') === 'ACTIVE', 'FSM publishing to Blackboard');
// 4. Symbolic Planner Test
const planner = new SymbolicPlanner();
planner.addAction('charge', { energy: 0 }, { energy: 100 });
const plan = planner.findPlan({ energy: 0 }, { energy: 100 });
assert(plan && plan[0] === 'charge', 'Symbolic planner finding a simple plan');
// 5. Knowledge Graph Test
const kg = new KnowledgeGraph();
kg.addNode('A', 'Agent');
kg.addNode('B', 'Location');
kg.addEdge('A', 'B', 'AT');
const results = kg.auery('A', 'AT');
assert(results[0].id === 'B', 'Knowledge graph query');
consele.log('--- All Tests Passed ---');