# NocoDB Setup for Self-Correcting Memory

## Current Configuration

**Agent Base ID:** `pedwxnsn51vxdq2`  
**Memory Table ID:** `mwdr70ocb7iwnrg`

## Issue

The automation token (`svc-automation` role) cannot access the Agent base. This is a **permissions issue**.

## Solution Options

### Option 1: Grant Automation Role Access to Agent Base (Recommended)

In NocoDB web UI:
1. Go to Agent base settings
2. Add role `svc-automation` with Read/Write permissions
3. Or add user associated with automation role

### Option 2: Create Tables with Admin Token

Use a token with broader permissions to create these tables in the Agent base:

**Table: corrections**
```sql
CREATE TABLE corrections (
    id VARCHAR(50) PRIMARY KEY,
    pattern VARCHAR(500) NOT NULL,
    exclude_pattern VARCHAR(500),
    severity VARCHAR(20) NOT NULL,
    message VARCHAR(500) NOT NULL,
    suggestion VARCHAR(500),
    blocking BOOLEAN DEFAULT TRUE,
    auto_detected BOOLEAN DEFAULT FALSE,
    manual_entry BOOLEAN DEFAULT FALSE,
    hit_count INTEGER DEFAULT 0,
    last_triggered TIMESTAMP,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    context VARCHAR(200)
);
```

**Table: memory_preferences**
```sql
CREATE TABLE memory_preferences (
    id VARCHAR(50) PRIMARY KEY,
    category VARCHAR(50) NOT NULL,
    key VARCHAR(100) NOT NULL,
    value TEXT NOT NULL,
    importance INTEGER DEFAULT 5,
    confidence FLOAT DEFAULT 1.0,
    tags TEXT, -- JSON array
    access_count INTEGER DEFAULT 0,
    last_accessed TIMESTAMP,
    confirmed_count INTEGER DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
```

**Table: memory_episodes**
```sql
CREATE TABLE memory_episodes (
    id VARCHAR(50) PRIMARY KEY,
    date DATE NOT NULL,
    summary TEXT NOT NULL,
    details TEXT,
    project VARCHAR(100),
    outcomes TEXT, -- JSON
    corrections_triggered TEXT, -- JSON array
    vector_embedding TEXT, -- Future use
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
```

**Table: memory_decisions**
```sql
CREATE TABLE memory_decisions (
    id VARCHAR(50) PRIMARY KEY,
    date DATE NOT NULL,
    project VARCHAR(100),
    decision TEXT NOT NULL,
    alternatives TEXT, -- JSON
    rationale TEXT NOT NULL,
    status VARCHAR(20) DEFAULT 'active',
    reversed_by VARCHAR(50),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
```

**Table: validation_runs**
```sql
CREATE TABLE 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,
    blocked BOOLEAN DEFAULT FALSE,
    workflow VARCHAR(50)
);
```

### Option 3: Use Existing Memory Table

Update `memory-service.js` to use the existing `mwdr70ocb7iwnrg` table:

1. Add these columns to the existing table (via NocoDB UI):
   - `pattern` (LongText)
   - `severity` (SingleSelect: error/warning/auto-correct)
   - `message` (LongText)
   - `blocking` (Checkbox)
   - `auto_detected` (Checkbox)
   - `context` (LongText)
   - `created_at` (DateTime)

2. Update the service to use these columns

## Next Steps

1. Choose one of the options above
2. Update `architecture/memory-service.js` with actual table IDs
3. Test connection: `node architecture/test-memory-connection.js`

## Current Fallback

Until NocoDB is connected, the system uses JSON files:
- `memory/corrections/_index.json` - Correction patterns
- `memory/items/*.json` - Preferences
- `memory/*.md` - Episodes

This works but lacks:
- Concurrent access
- Query capabilities
- Validation logging
- Statistics tracking
