Implement self-correcting memory architecture with critic engine

Adds:
- Critic Engine (critic.js): Validates output against correction store
- Auto-detection: Identifies uncorrected patterns (quantitative without proof, vague quantification, etc.)
- Auto-storage: New patterns immediately stored as corrections
- NocoDB Schema: Tables for corrections, preferences, episodes, decisions, validation
- Memory Service (memory-service.js): NocoDB integration layer
- Response Generator (response-generator.js): End-to-end pipeline with critic
- Correction Store: JSON-based with README documentation

Behavior:
- ALL output validated before delivery
- Quantitative claims without evidence → auto-corrected
- Corrections immediately block future occurrences
- System learns from its own mistakes

Test: node architecture/test-critic.js
This commit is contained in:
JC Beasley
2026-07-04 16:23:05 -07:00
parent 076e9eb605
commit cf3f1e1c53
9 changed files with 1159 additions and 0 deletions
+329
View File
@@ -0,0 +1,329 @@
#!/usr/bin/env node
/**
* Self-Correcting Memory Architecture - Critic Engine
*
* Flow:
* 1. Generate response → 2. Critic checks against correction store →
* 3. If violations → Block & regenerate → 4. If new pattern → Auto-store
*
* Principles:
* - ALL output validated against correction store before delivery
* - Quantitative claims without proof → auto-correction
* - Corrections immediately block future occurrences
* - System learns from its own mistakes
*/
const fs = require('fs');
const path = require('path');
class CriticEngine {
constructor(options = {}) {
this.correctionStorePath = options.correctionStorePath || '/home/jcbeasley/.openclaw/workspace/memory/corrections';
this.autoLearnEnabled = options.autoLearn !== false;
this.strictMode = options.strictMode || false; // Block on warnings too
// Correction patterns (loaded from store + hardcoded rules)
this.corrections = [];
this.patternRules = this.getPatternRules();
}
/**
* Pattern Rules - Auto-detect uncorrected issues
*/
getPatternRules() {
return [
{
id: 'quantitative_without_proof',
name: 'Quantitative Claim Without Evidence',
pattern: /\b\d+\s*(%|percent|times|x|percent|fold)\b/i,
exclude: /\b(according to|based on|from|measured|tested|verified|source:|citation|data:)\b/i,
severity: 'auto-correct',
message: 'Quantitative claim requires evidence',
autoStore: true
},
{
id: 'unverified_performance',
name: 'Performance Claim Without Benchmark',
pattern: /\b(faster|slower|better|worse|improved|optimized)\s+(than|by)\b/i,
exclude: /\b(measured|benchmarked|tested|profiled|verified)\b/i,
severity: 'auto-correct',
message: 'Performance claims require benchmarks',
autoStore: true
},
{
id: 'absolute_without_qualification',
name: 'Absolute Statement Without Qualification',
pattern: /\b(always|never|all|none|every|impossible)\b/i,
exclude: /\b(in this case|for this|under these|given the|based on)\b/i,
severity: 'warning',
message: 'Absolute statements need qualification',
autoStore: false // Manual review first
},
{
id: 'vague_quantification',
name: 'Vague Quantification',
pattern: /\b(many|few|several|some|most|lots|a lot)\b/i,
exclude: /\b(specifically|exactly|precisely)\b/i,
severity: 'auto-correct',
message: 'Use specific numbers or omit',
autoStore: true
},
{
id: 'unverified_completion',
name: '"Done" Without Verification',
pattern: /\b(done|complete|finished|working)\b/i,
exclude: /\b(verified|tested|confirmed|validated|checked)\b/i,
severity: 'error',
message: 'Completion claims require verification evidence',
autoStore: true
}
];
}
/**
* Load corrections from store
*/
async loadCorrections() {
// Load from NocoDB (to be implemented) or local JSON
const correctionsFile = path.join(this.correctionStorePath, '_index.json');
try {
if (fs.existsSync(correctionsFile)) {
const data = JSON.parse(fs.readFileSync(correctionsFile, 'utf8'));
this.corrections = data.corrections || [];
}
} catch (err) {
console.warn('Could not load corrections:', err.message);
}
return this;
}
/**
* Main Critic Check
* Returns: { passed: bool, violations: [], newPatterns: [], corrected: string }
*/
async critique(response, context = {}) {
await this.loadCorrections();
const violations = [];
const newPatterns = [];
// 1. Check against known corrections (blocking)
const knownViolations = this.checkKnownCorrections(response);
violations.push(...knownViolations);
// 2. Auto-detect new patterns (if enabled)
if (this.autoLearnEnabled && violations.length === 0) {
const detectedPatterns = this.detectNewPatterns(response, context);
for (const pattern of detectedPatterns) {
if (pattern.severity === 'auto-correct') {
// Auto-store as new correction
await this.storeCorrection(pattern);
violations.push({
...pattern,
message: `${pattern.message} [AUTO-STORED AS CORRECTION]`,
autoStored: true
});
} else if (pattern.severity === 'warning') {
newPatterns.push(pattern);
}
}
}
// 3. Generate corrected version if violations found
let corrected = response;
if (violations.length > 0) {
corrected = await this.generateCorrection(response, violations);
}
return {
passed: violations.length === 0,
blocked: violations.length > 0 && violations.some(v => v.blocking),
violations,
newPatterns,
corrected,
original: response
};
}
/**
* Check against known corrections (from store)
*/
checkKnownCorrections(response) {
const violations = [];
for (const correction of this.corrections) {
const pattern = new RegExp(correction.pattern, 'i');
if (pattern.test(response)) {
// Check if exclusion applies
if (correction.excludePattern) {
const exclude = new RegExp(correction.excludePattern, 'i');
if (exclude.test(response)) {
continue; // Has exclusion, skip
}
}
violations.push({
id: correction.id,
type: 'known-correction',
severity: correction.severity || 'error',
blocking: correction.blocking !== false,
message: correction.message,
suggestion: correction.suggestion,
originalPattern: correction.pattern
});
}
}
return violations;
}
/**
* Detect new patterns that should become corrections
*/
detectNewPatterns(response, context) {
const detected = [];
for (const rule of this.patternRules) {
if (rule.pattern.test(response)) {
// Check if exclusion applies
if (rule.exclude && rule.exclude.test(response)) {
continue;
}
detected.push({
id: rule.id,
type: 'auto-detected',
severity: rule.severity,
message: rule.message,
pattern: rule.pattern.source,
autoStore: rule.autoStore,
context: context.task || 'unknown'
});
}
}
return detected;
}
/**
* Store new correction (immediate write)
*/
async storeCorrection(pattern) {
const correction = {
id: `correction_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
pattern: pattern.pattern,
severity: pattern.severity,
message: pattern.message,
suggestion: `Add evidence or qualification`,
blocking: true,
autoDetected: true,
createdAt: new Date().toISOString(),
context: pattern.context
};
// Add to memory
this.corrections.push(correction);
// Persist to disk (immediate)
await this.persistCorrections();
// Log for audit
console.log(`[CRITIC] Auto-stored correction: ${correction.id} - ${pattern.message}`);
return correction;
}
/**
* Persist corrections to disk (will move to NocoDB)
*/
async persistCorrections() {
try {
if (!fs.existsSync(this.correctionStorePath)) {
fs.mkdirSync(this.correctionStorePath, { recursive: true });
}
const indexFile = path.join(this.correctionStorePath, '_index.json');
fs.writeFileSync(indexFile, JSON.stringify({
version: 1,
updatedAt: new Date().toISOString(),
corrections: this.corrections
}, null, 2));
// Also write individual correction files for inspection
for (const correction of this.corrections.slice(-10)) { // Last 10
const filePath = path.join(this.correctionStorePath, `${correction.id}.json`);
fs.writeFileSync(filePath, JSON.stringify(correction, null, 2));
}
} catch (err) {
console.error('[CRITIC] Failed to persist corrections:', err);
}
}
/**
* Generate corrected version of response
*/
async generateCorrection(original, violations) {
// Simple correction: add warning header
const warnings = violations.map(v => `- ${v.message}`).join('\n');
return `⚠️ CORRECTIONS REQUIRED:\n${warnings}\n\n---\n\n${original}`;
// Future: Use LLM to actually fix the content
// const fixPrompt = buildFixPrompt(original, violations);
// return await llm.generate(fixPrompt);
}
/**
* Manual correction entry (for user feedback)
*/
async addManualCorrection(originalText, correctionText, reason) {
const correction = {
id: `manual_${Date.now()}`,
pattern: this.escapeRegex(originalText.substring(0, 100)),
severity: 'error',
message: reason,
suggestion: correctionText,
blocking: true,
manualEntry: true,
createdAt: new Date().toISOString()
};
this.corrections.push(correction);
await this.persistCorrections();
return correction;
}
/**
* Escape regex special chars
*/
escapeRegex(str) {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
}
// Export
module.exports = CriticEngine;
// CLI usage
if (require.main === module) {
const responseFile = process.argv[2];
if (!responseFile) {
console.error('Usage: node critic.js <response-file>');
process.exit(1);
}
const response = fs.readFileSync(responseFile, 'utf8');
const critic = new CriticEngine();
critic.critique(response)
.then(result => {
console.log(JSON.stringify(result, null, 2));
})
.catch(err => {
console.error('Critic error:', err);
process.exit(1);
});
}
+278
View File
@@ -0,0 +1,278 @@
#!/usr/bin/env node
/**
* Memory Service - NocoDB + Critic Integration
*
* Provides:
* - Structured memory storage (preferences, episodes, decisions)
* - Correction store (self-correcting patterns)
* - Validation logging
*/
const axios = require('axios');
class MemoryService {
constructor(config = {}) {
this.nocodbUrl = config.nocodbUrl || 'http://192.168.25.5:8080';
this.nocodbToken = config.nocodbToken || process.env.NOCODB_TOKEN;
this.projectId = config.projectId || 'default';
// Table mappings (NocoDB table IDs)
this.tables = {
corrections: config.correctionsTable || 'corrections',
preferences: config.preferencesTable || 'memory_preferences',
episodes: config.episodesTable || 'memory_episodes',
decisions: config.decisionsTable || 'memory_decisions',
validation: config.validationTable || 'validation_runs'
};
}
/**
* Get HTTP client with auth
*/
getClient() {
return axios.create({
baseURL: `${this.nocodbUrl}/api/v2/tables`,
headers: {
'xc-token': this.nocodbToken,
'Content-Type': 'application/json'
}
});
}
// ============================================
// CORRECTIONS API
// ============================================
/**
* Load all active corrections
*/
async loadCorrections() {
const client = this.getClient();
const response = await client.get(`/${this.tables.corrections}/records`, {
params: {
where: `(blocking,eq,true)`,
limit: 1000
}
});
return response.data.list;
}
/**
* Store new correction
*/
async storeCorrection(correction) {
const client = this.getClient();
const payload = {
id: correction.id,
pattern: correction.pattern,
exclude_pattern: correction.excludePattern || null,
severity: correction.severity,
message: correction.message,
suggestion: correction.suggestion,
blocking: correction.blocking !== false,
auto_detected: correction.autoDetected || true,
manual_entry: correction.manualEntry || false,
hit_count: 0,
context: correction.context || 'unknown',
created_at: new Date().toISOString(),
updated_at: new Date().toISOString()
};
const response = await client.post(
`/${this.tables.corrections}/records`,
payload
);
return response.data;
}
/**
* Increment hit count for correction
*/
async incrementCorrectionHit(correctionId) {
const client = this.getClient();
// Get current hit count
const current = await client.get(
`/${this.tables.corrections}/records/${correctionId}`
);
const newCount = (current.data.hit_count || 0) + 1;
await client.patch(
`/${this.tables.corrections}/records/${correctionId}`,
{
hit_count: newCount,
last_triggered: new Date().toISOString(),
updated_at: new Date().toISOString()
}
);
return newCount;
}
// ============================================
// PREFERENCES API
// ============================================
/**
* Get preferences by category
*/
async getPreferences(category, minImportance = 5) {
const client = this.getClient();
const response = await client.get(`/${this.tables.preferences}/records`, {
params: {
where: `(category,eq,${category})~and(importance,gte,${minImportance})`,
sort: '-importance,-access_count'
}
});
return response.data.list;
}
/**
* Store preference
*/
async storePreference(pref) {
const client = this.getClient();
const payload = {
id: pref.id,
category: pref.category,
key: pref.key,
value: pref.value,
importance: pref.importance || 5,
confidence: pref.confidence || 1.0,
tags: JSON.stringify(pref.tags || []),
confirmed_count: pref.confirmedCount || 0,
created_at: new Date().toISOString(),
updated_at: new Date().toISOString()
};
const response = await client.post(
`/${this.tables.preferences}/records`,
payload
);
return response.data;
}
// ============================================
// EPISODES API
// ============================================
/**
* Store episodic memory
*/
async storeEpisode(episode) {
const client = this.getClient();
const payload = {
id: episode.id,
date: episode.date || new Date().toISOString().split('T')[0],
summary: episode.summary,
details: episode.details,
project: episode.project,
outcomes: JSON.stringify(episode.outcomes || []),
corrections_triggered: JSON.stringify(episode.correctionsTriggered || []),
created_at: new Date().toISOString()
};
const response = await client.post(
`/${this.tables.episodes}/records`,
payload
);
return response.data;
}
// ============================================
// DECISIONS API
// ============================================
/**
* Store decision
*/
async storeDecision(decision) {
const client = this.getClient();
const payload = {
id: decision.id,
date: decision.date || new Date().toISOString().split('T')[0],
project: decision.project,
decision: decision.decision,
alternatives: JSON.stringify(decision.alternatives || []),
rationale: decision.rationale,
status: decision.status || 'active',
created_at: new Date().toISOString()
};
const response = await client.post(
`/${this.tables.decisions}/records`,
payload
);
return response.data;
}
// ============================================
// VALIDATION LOGGING
// ============================================
/**
* Log validation run
*/
async logValidation(run) {
const client = this.getClient();
const payload = {
id: run.id,
session_id: run.sessionId,
timestamp: new Date().toISOString(),
input_length: run.inputLength,
output_length: run.outputLength,
violations_found: run.violationsFound || 0,
new_patterns_detected: run.newPatternsDetected || 0,
corrections_auto_stored: run.correctionsAutoStored || 0,
processing_time_ms: run.processingTimeMs,
blocked: run.blocked || false,
workflow: run.workflow
};
const response = await client.post(
`/${this.tables.validation}/records`,
payload
);
return response.data;
}
/**
* Get validation statistics
*/
async getValidationStats(days = 7) {
const client = this.getClient();
const response = await client.get(`/${this.tables.validation}/records`, {
params: {
where: `(timestamp,gte,${days} days ago})`,
fields: 'blocked,violations_found,corrections_auto_stored'
}
});
const runs = response.data.list;
return {
totalRuns: runs.length,
blockedCount: runs.filter(r => r.blocked).length,
totalViolations: runs.reduce((sum, r) => sum + (r.violations_found || 0), 0),
totalAutoCorrections: runs.reduce((sum, r) => sum + (r.corrections_auto_stored || 0), 0),
blockRate: runs.length > 0 ? (runs.filter(r => r.blocked).length / runs.length * 100).toFixed(1) : 0
};
}
}
// Export
module.exports = MemoryService;
+140
View File
@@ -0,0 +1,140 @@
-- NocoDB Schema for Self-Correcting Memory Architecture
-- Tables: corrections, memory_preferences, memory_episodes, memory_decisions
-- ============================================
-- TABLE: corrections
-- Stores known mistakes and auto-detected patterns
-- ============================================
CREATE TABLE IF NOT EXISTS corrections (
id VARCHAR(50) PRIMARY KEY,
pattern VARCHAR(500) NOT NULL, -- Regex pattern to match
exclude_pattern VARCHAR(500), -- Pattern that excludes match
severity VARCHAR(20) NOT NULL, -- error, warning, auto-correct
message VARCHAR(500) NOT NULL, -- What to tell user
suggestion VARCHAR(500), -- How to fix
blocking BOOLEAN DEFAULT TRUE, -- Block output if matched?
auto_detected BOOLEAN DEFAULT FALSE, -- Was this auto-detected?
manual_entry BOOLEAN DEFAULT FALSE, -- Was this manually added?
hit_count INTEGER DEFAULT 0, -- How many times triggered
last_triggered TIMESTAMP, -- Last time this fired
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
context VARCHAR(200) -- Task/workflow context
);
-- Indexes for fast lookup
CREATE INDEX IF NOT EXISTS idx_corrections_severity ON corrections(severity);
CREATE INDEX IF NOT EXISTS idx_corrections_blocking ON corrections(blocking);
CREATE INDEX IF NOT EXISTS idx_corrections_auto ON corrections(auto_detected);
-- ============================================
-- TABLE: memory_preferences
-- Persistent user preferences (Layer 2)
-- ============================================
CREATE TABLE IF NOT EXISTS memory_preferences (
id VARCHAR(50) PRIMARY KEY,
category VARCHAR(50) NOT NULL, -- identity, pref, goal, knowledge
key VARCHAR(100) NOT NULL,
value TEXT NOT NULL,
importance INTEGER DEFAULT 5, -- 1-10 scale
confidence FLOAT DEFAULT 1.0, -- 0.0-1.0
tags JSON, -- Array of tags
access_count INTEGER DEFAULT 0, -- How often retrieved
last_accessed TIMESTAMP,
confirmed_count INTEGER DEFAULT 0, -- Times user confirmed
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_prefs_category ON memory_preferences(category);
CREATE INDEX IF NOT EXISTS idx_prefs_importance ON memory_preferences(importance);
-- ============================================
-- TABLE: memory_episodes
-- Episodic memory - what happened when
-- ============================================
CREATE TABLE IF NOT EXISTS memory_episodes (
id VARCHAR(50) PRIMARY KEY,
date DATE NOT NULL,
summary TEXT NOT NULL, -- Brief summary
details TEXT, -- Full details
project VARCHAR(100), -- Which project
outcomes JSON, -- What resulted
corrections_triggered JSON, -- Array of correction IDs
vector_embedding JSON, -- For semantic search (future)
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_episodes_date ON memory_episodes(date);
CREATE INDEX IF NOT EXISTS idx_episodes_project ON memory_episodes(project);
-- ============================================
-- TABLE: memory_decisions
-- Architectural/technical decisions
-- ============================================
CREATE TABLE IF NOT EXISTS memory_decisions (
id VARCHAR(50) PRIMARY KEY,
date DATE NOT NULL,
project VARCHAR(100),
decision TEXT NOT NULL, -- What was decided
alternatives JSON, -- What was considered
rationale TEXT NOT NULL, -- Why this choice
status VARCHAR(20) DEFAULT 'active', -- active, reversed, deprecated
reversed_by VARCHAR(50), -- If reversed, link to new decision
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_decisions_project ON memory_decisions(project);
CREATE INDEX IF NOT EXISTS idx_decisions_status ON memory_decisions(status);
-- ============================================
-- TABLE: validation_runs
-- Track critic engine performance
-- ============================================
CREATE TABLE IF NOT EXISTS validation_runs (
id VARCHAR(50) PRIMARY KEY,
session_id VARCHAR(50),
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
input_length INTEGER,
output_length INTEGER,
violations_found INTEGER DEFAULT 0,
new_patterns_detected INTEGER DEFAULT 0,
corrections_auto_stored INTEGER DEFAULT 0,
processing_time_ms INTEGER, -- How long validation took
blocked BOOLEAN DEFAULT FALSE, -- Was output blocked?
workflow VARCHAR(50) -- Which workflow was active
);
CREATE INDEX IF NOT EXISTS idx_validation_session ON validation_runs(session_id);
CREATE INDEX IF NOT EXISTS idx_validation_timestamp ON validation_runs(timestamp);
-- ============================================
-- SEED DATA: Initial correction patterns
-- ============================================
-- Critical patterns (always blocking)
INSERT INTO corrections (id, pattern, severity, message, suggestion, blocking, auto_detected) VALUES
('uncorrected_quantitative', '\\d+\\s*(%|percent|x\\s|times|fold)', 'auto-correct', 'Quantitative claim requires evidence citation', 'Add source or measurement method', TRUE, TRUE),
('unverified_done', '\\b(done|complete|finished|shipped)\\b(?!(?:.*\\b(verified|tested|validated|checked)\\b))', 'error', '"Done" claims require verification evidence', 'Add verification steps completed', TRUE, FALSE),
('unverified_performance', '\\b\\d+\\s*(%|percent|times|x\\s)\\s*(?:faster|slower|better|improved)', 'auto-correct', 'Performance claim requires benchmark data', 'Add benchmark methodology and results', TRUE, TRUE)
ON CONFLICT (id) DO NOTHING;
-- ============================================
-- VIEWS: Useful queries
-- ============================================
-- High-impact corrections (blocking + high hit count)
CREATE OR REPLACE VIEW high_impact_corrections AS
SELECT id, pattern, message, hit_count, last_triggered, created_at
FROM corrections
WHERE blocking = TRUE AND hit_count > 5
ORDER BY hit_count DESC;
-- Recent auto-detected patterns needing review
CREATE OR REPLACE VIEW auto_patterns_for_review AS
SELECT id, pattern, message, created_at, hit_count
FROM corrections
WHERE auto_detected = TRUE AND manual_entry = FALSE
AND created_at > CURRENT_TIMESTAMP - INTERVAL '7 days'
ORDER BY hit_count DESC;
+206
View File
@@ -0,0 +1,206 @@
#!/usr/bin/env node
/**
* Response Generator with Self-Correcting Memory
*
* Architecture:
* 1. Load context (rules + prefs + memory)
* 2. Generate draft
* 3. Critic validates
* 4. If violations → regenerate with corrections
* 5. If new patterns → auto-store as corrections
* 6. Return validated response
*/
const ContextPipeline = require('./pipeline');
const CriticEngine = require('./critic');
const MemoryService = require('./memory-service');
class ResponseGenerator {
constructor(config = {}) {
this.pipeline = new ContextPipeline();
this.critic = new CriticEngine({
strictMode: config.strictMode || false,
autoLearn: config.autoLearn !== false
});
this.memory = config.memoryService || null;
this.workflow = config.workflow || 'default';
this.maxRetries = config.maxRetries || 3;
}
/**
* Generate validated response
*/
async generate(taskInput, options = {}) {
const startTime = Date.now();
// Step 1: Load context
await this.pipeline
.loadRules()
.then(() => this.pipeline.loadPreferences())
.then(() => this.pipeline.loadRelevantMemory(taskInput));
const contextPacket = this.pipeline.buildPacket(taskInput);
// Step 2-4: Generate with validation loop
let attempts = 0;
let lastViolations = [];
let response = null;
let criticResult = null;
while (attempts < this.maxRetries) {
attempts++;
// Generate draft (in real implementation, this calls LLM)
response = await this.callLLM(contextPacket, lastViolations);
// Validate
criticResult = await this.critic.critique(response, {
task: taskInput,
workflow: this.workflow,
attempt: attempts
});
if (criticResult.passed) {
break; // Success!
}
// Blocked - need to regenerate
lastViolations = criticResult.violations;
console.log(`[GENERATOR] Attempt ${attempts} blocked: ${criticResult.violations.map(v => v.message).join(', ')}`);
}
const processingTime = Date.now() - startTime;
// Step 5: Log validation run
if (this.memory) {
await this.memory.logValidation({
id: `run_${Date.now()}`,
sessionId: options.sessionId || 'unknown',
inputLength: taskInput.length,
outputLength: response.length,
violationsFound: criticResult.violations.length,
newPatternsDetected: criticResult.newPatterns.length,
correctionsAutoStored: criticResult.violations.filter(v => v.autoStored).length,
processingTimeMs: processingTime,
blocked: attempts > 1,
workflow: this.workflow
});
}
return {
response: criticResult.corrected,
original: criticResult.original,
attempts,
passed: criticResult.passed,
violations: criticResult.violations,
newPatterns: criticResult.newPatterns,
processingTimeMs: processingTime,
context: {
rulesCount: contextPacket.metadata.rules_count,
prefsCount: contextPacket.metadata.prefs_count,
memoryCount: contextPacket.metadata.memory_count
}
};
}
/**
* Call LLM with context (placeholder - integrate with actual LLM)
*/
async callLLM(contextPacket, previousViolations = []) {
// In production, this calls OpenClaw/LLM
// For now, return a mock response
let prompt = this.buildPrompt(contextPacket, previousViolations);
// Simulate LLM call
// const response = await llm.generate(prompt);
// For demo: return context to show it works
return `## Task
${contextPacket.task}
## System Rules
${contextPacket.system.substring(0, 500)}...
## Response
This is 50% faster than before.`; // Intentional violation for testing
}
/**
* Build prompt with corrections injected
*/
buildPrompt(contextPacket, previousViolations) {
const parts = [];
// System rules (highest priority)
parts.push('# SYSTEM RULES\n' + contextPacket.system);
// Corrections from previous attempts
if (previousViolations.length > 0) {
parts.push('\n# CORRECTIONS REQUIRED\n');
for (const violation of previousViolations) {
parts.push(`- ${violation.message}`);
if (violation.suggestion) {
parts.push(` Fix: ${violation.suggestion}`);
}
}
}
// Preferences
if (contextPacket.preferences) {
parts.push('\n# USER PREFERENCES\n' + contextPacket.preferences);
}
// Memory
if (contextPacket.memory) {
parts.push('\n# RELEVANT MEMORY\n' + contextPacket.memory);
}
// Task
parts.push('\n# TASK\n' + contextPacket.task);
parts.push('\nRespond following all system rules and corrections above.');
return parts.join('\n');
}
/**
* Manual correction entry (user feedback)
*/
async addUserCorrection(originalText, correctionText, reason) {
const correction = await this.critic.addManualCorrection(
originalText,
correctionText,
reason
);
if (this.memory) {
await this.memory.storeCorrection(correction);
}
return correction;
}
}
// Export
module.exports = ResponseGenerator;
// CLI usage
if (require.main === module) {
const generator = new ResponseGenerator({
workflow: process.argv[2] || 'coding',
strictMode: true,
autoLearn: true
});
const task = process.argv[3] || 'Write a function to process data';
generator.generate(task)
.then(result => {
console.log('=== GENERATION RESULT ===');
console.log(JSON.stringify(result, null, 2));
})
.catch(err => {
console.error('Generation failed:', err);
process.exit(1);
});
}
+106
View File
@@ -0,0 +1,106 @@
#!/usr/bin/env node
/**
* Test Self-Correcting Memory System
*
* Demonstrates:
* - Auto-detection of uncorrected patterns
* - Immediate storage to correction store
* - Blocking of future occurrences
*/
const CriticEngine = require('./critic');
// Test cases
const testCases = [
{
name: 'Quantitative claim without evidence',
input: 'This code is 50% faster than the old implementation.',
shouldBlock: true,
shouldAutoStore: true
},
{
name: 'Unverified "done"',
input: 'The task is done. I finished the deployment.',
shouldBlock: true,
shouldAutoStore: true
},
{
name: 'Vague quantification',
input: 'Many users reported issues with the new feature.',
shouldBlock: true,
shouldAutoStore: true
},
{
name: 'Performance claim without benchmark',
input: 'The new algorithm is 3x faster than the previous one.',
shouldBlock: true,
shouldAutoStore: true
},
{
name: 'Good: Quantitative WITH evidence',
input: 'According to our benchmarks (see test-results.json), the code is 50% faster.',
shouldBlock: false,
shouldAutoStore: false
},
{
name: 'Good: Verified completion',
input: 'The task is complete. Verified by running all tests (100% pass).',
shouldBlock: false,
shouldAutoStore: false
}
];
async function runTests() {
const critic = new CriticEngine({
autoLearn: true,
strictMode: false
});
console.log('=== SELF-CORRECTING MEMORY TEST ===\n');
for (const test of testCases) {
console.log(`Test: ${test.name}`);
console.log(`Input: "${test.input.substring(0, 60)}..."`);
const result = await critic.critique(test.input);
console.log(`Result: ${result.passed ? '✅ PASSED' : '❌ BLOCKED'}`);
console.log(`Violations: ${result.violations.length}`);
console.log(`Auto-stored: ${result.violations.some(v => v.autoStored) ? 'YES' : 'NO'}`);
if (result.violations.length > 0) {
for (const v of result.violations) {
console.log(` - ${v.message}${v.autoStored ? ' [AUTO-STORED]' : ''}`);
}
}
// Verify expectations
const expectedBlocked = test.shouldBlock;
const actualBlocked = !result.passed;
const expectedStored = test.shouldAutoStore;
const actualStored = result.violations.some(v => v.autoStored);
if (expectedBlocked !== actualBlocked) {
console.log(`⚠️ UNEXPECTED: Expected blocked=${expectedBlocked}, got ${actualBlocked}`);
}
if (expectedStored !== actualStored) {
console.log(`⚠️ UNEXPECTED: Expected stored=${expectedStored}, got ${actualStored}`);
}
console.log('---\n');
}
// Summary
console.log('\n=== CORRECTION STORE CONTENTS ===');
console.log(`Total corrections: ${critic.corrections.length}`);
console.log('Auto-detected patterns:');
const autoDetected = critic.corrections.filter(c => c.autoDetected);
for (const c of autoDetected) {
console.log(` - ${c.id}: ${c.message}`);
}
console.log('\n=== TEST COMPLETE ===');
}
runTests().catch(console.error);
+50
View File
@@ -0,0 +1,50 @@
# Self-Correcting Memory - Correction Store
## What This Is
This directory contains **corrections** - patterns that identify mistakes and enforce quality.
## How It Works
1. **Critic Engine** validates ALL output before delivery
2. **If violation found** → Output blocked, user notified
3. **If new pattern detected** → Auto-stored as correction
4. **Future occurrences** → Immediately blocked
## Correction Format
```json
{
"id": "correction_<timestamp>_<random>",
"pattern": "regex pattern to match",
"excludePattern": "regex that exempts match",
"severity": "error|warning|auto-correct",
"message": "What to tell user",
"suggestion": "How to fix",
"blocking": true,
"autoDetected": true,
"hitCount": 0,
"createdAt": "2026-07-04T..."
}
```
## Active Patterns
| ID | Pattern | Severity | Action |
|----|---------|----------|--------|
| quantitative_without_proof | `\d+\s*(%\|percent\|times)` | auto-correct | Block + require evidence |
| vague_quantification | `\b(many\|few\|several\|some)\b` | auto-correct | Block + require specificity |
| unverified_done | `\b(done\|complete\|finished)\b` | error | Block + require verification |
## Files
- `_index.json` - All corrections (master list)
- `correction_*.json` - Individual correction files
- `README.md` - This file
## Learning
The system learns from its own mistakes:
- Every uncorrected pattern → Becomes a correction
- No manual intervention required
- Immediate enforcement
+28
View File
@@ -0,0 +1,28 @@
{
"version": 1,
"updatedAt": "2026-07-04T23:22:57.184Z",
"corrections": [
{
"id": "correction_1783207377183_yc4ucl8pr",
"pattern": "\\b(faster|slower|better|worse|improved|optimized)\\s+(than|by)\\b",
"severity": "auto-correct",
"message": "Performance claims require benchmarks",
"suggestion": "Add evidence or qualification",
"blocking": true,
"autoDetected": true,
"createdAt": "2026-07-04T23:22:57.183Z",
"context": "unknown"
},
{
"id": "correction_1783207377184_lzuf1l34i",
"pattern": "\\b(many|few|several|some|most|lots|a lot)\\b",
"severity": "auto-correct",
"message": "Use specific numbers or omit",
"suggestion": "Add evidence or qualification",
"blocking": true,
"autoDetected": true,
"createdAt": "2026-07-04T23:22:57.184Z",
"context": "unknown"
}
]
}
@@ -0,0 +1,11 @@
{
"id": "correction_1783207377183_yc4ucl8pr",
"pattern": "\\b(faster|slower|better|worse|improved|optimized)\\s+(than|by)\\b",
"severity": "auto-correct",
"message": "Performance claims require benchmarks",
"suggestion": "Add evidence or qualification",
"blocking": true,
"autoDetected": true,
"createdAt": "2026-07-04T23:22:57.183Z",
"context": "unknown"
}
@@ -0,0 +1,11 @@
{
"id": "correction_1783207377184_lzuf1l34i",
"pattern": "\\b(many|few|several|some|most|lots|a lot)\\b",
"severity": "auto-correct",
"message": "Use specific numbers or omit",
"suggestion": "Add evidence or qualification",
"blocking": true,
"autoDetected": true,
"createdAt": "2026-07-04T23:22:57.184Z",
"context": "unknown"
}