Anchor Engine - System Specification

June 24, 2026 · View on GitHub

Version: 5.3.0 | Status: Production Ready | Updated: June 13, 2026

Quick Reference

AspectValue
Port3160 (configurable)
DatabasePGlite (PostgreSQL-compatible)
Source of Truth~/.anchor/mirrored_brain/ filesystem
IndexDisposable, rebuildable on startup
SearchSTAR Algorithm (70/30 Planets/Moons)
Dockerdocker-compose up -d (2 CPU, 2GB RAM)
Version Sourceuser_settings.json.template$HOME/.anchor/user_settings.json

Recent Changes (v5.3.0 — June 2026)

Streaming Architecture

  • Streaming Search (/v1/memory/search/stream) - SSE-based progressive results
  • Streaming Ingest (/v1/ingest/streaming) - Large file processing in chunks with progress tracking

Centralized Validation

  • Zod Schemas - engine/src/config/index.ts (645 lines) shared across all API routes
  • PostgreSQL Array Conversion - toPgArray() helper for proper DB format

Performance Monitoring

  • Performance Monitor Service - Memory, CPU, engine status tracking (engine/src/utils/performance-monitor.ts)
  • UI Stats Dashboard - Real-time system metrics display
  • DB Clearing & Distill Output - Clean state management

Runtime Data Consolidation

  • All runtime data routes to ~/.anchor/ via engine/src/config/paths.ts
  • user_settings.json.template generates user_settings.json at $HOME/.anchor/ on pnpm install + pnpm start

Security Hardening (April 2026)

  • API key validation: 32-128 chars with mixed case/digits (Standard 028)
  • Path traversal prevention (Standard 029)
  • Auth bypass prevention - removed /v1/test/* endpoints (Standard 027)
  • Rate limiting for MCP server (60 req/min)
  • Write operations opt-in with bucket validation


Architecture Overview

System Diagram

flowchart TB
    subgraph UI["UI Layer<br/>React/Vite"]
        A[http://localhost:3160]
    end

    subgraph API["HTTP API Layer<br/>Express.js Port 3160"]
        B[Routes /v1/*]
        C[Middleware]
        D[Zod Validation<br/>config/index.ts]
    end

    subgraph SERVICES["Core Services"]
        E[Ingestion +<br/>Streaming Ingest]
        F[Search STAR +<br/>Streaming Search]
        G[Watchdog]
        H[Mirror]
        I[Performance Monitor]
    end

    subgraph STORAGE["Storage"]
        K[(PGlite<br/>Disposable)]
        L[~/.anchor/mirrored_brain/<br/>Source]
        M[~/.anchor/inbox/<br/>Files]
    end

    A --> B --> C --> D
    C --> E
    C --> F
    C --> G --> M
    C --> H --> L & M
    I --> K & L
    E & F --> K & L

    style K fill:#ffebee
    style L fill:#e8f5e9
    style M fill:#e3f2fd

Key Components

  1. UI Layer: React/Vite frontend at http://localhost:3160
  2. HTTP API: Express.js REST API on port 3160 with Zod validation middleware
  3. Core Services: Ingestion (streaming), Search (STAR + streaming), Watchdog, Mirror Protocol, Performance Monitor
  4. Storage: PGlite database (disposable index) + ~/.anchor/mirrored_brain/ (source of truth)

Data Flow

User Query → API Route → Zod Validation → Search Service → PGlite Query → Context Inflation → Return 618k chars

Web Dashboard

Location: engine/public/index.html

Tech Stack: React 18.3.1 (CDN), ReactDOM 18.3.1 (CDN), Babel 7.26.0 (CDN), TailwindCSS 3.4 (CDN), Lucide Icons, js-yaml 4.1.0

RouteDescriptionAPI Endpoints
/Homepage with statusHealth check, status
/searchMain search interfaceGET /v1/search
/settingsConfiguration pagePOST /v1/settings, GET /v1/settings
/quarantineQuarantined filesGET /v1/quarantine, POST /v1/quarantine/unquarantine
/testTest suite runnerPOST /v1/test/run

Engine Core Modules

ModulePurposeLocation
PGlite DBIn-memory PostgreSQL-compatible storeengine/src/core/db.ts
Search (STAR)Physics-inspired graph retrievalengine/src/services/search/
DistillationSummarization & extraction (Radial v2)engine/src/services/distillation/
Git ClonerRepository cloning & AST parsingengine/src/services/ingest/
Mirror ProtocolFilesystem source-of-truth syncengine/src/services/mirror/

Quick Reference

TaskCommand
Start enginepnpm start
Run all testspnpm test
Run integration testspnpm test:vitest run integration
Build productionpnpm build
Open dashboardhttp://localhost:3160

Process Separation Architecture

Problem Statement

The current architecture runs everything on the Node.js single thread. Heavy operations (synonym generation, mirror protocol, tag cleanup, WASM loading, search queries) block the event loop, making the HTTP server unresponsive. This is a fundamental architecture problem — not a configuration issue.

Current Architecture (BROKEN)

┌─────────────────────────────────────────────────────┐
│                    Main Thread (Event Loop)           │
│                                                     │
│  HTTP Server ──────────────────────────────────┐    │
│  Health Check ────────────────────────────────┐│    │
│  Search Endpoint ─────────────────────────────┐││    │
│  Distill Endpoint ────────────────────────────┐│││    │
│  Watchdog ────────────────────────────────────┐││││    │
│  Synonym Generator ───────────────────────────┐│││││    │
│  Mirror Protocol ─────────────────────────────┐││││││    │
│  Tag Cleanup ─────────────────────────────────┐│││││││    │
│  WASM Loading ────────────────────────────────┐│││││││    │
│  Database Init ───────────────────────────────┘││││││    │
│                                                 ││││││    │
│  BLOCKED when ANY of the above runs synchronously│││││    │
└─────────────────────────────────────────────────┘│││    │
                                                    ││    │
  Problem: All heavy work competes for the same event loop tick.
  Result: HTTP requests hang until heavy work completes.

Target Architecture (ISOLATED)

┌──────────────────────────────────────────────────────────────────┐
│                      MAIN THREAD (HTTP Server)                   │
│                                                                  │
│  ┌──────────┐  ┌──────────┐  ┌──────────┐  ┌──────────┐       │
│  │ Health   │  │ Search   │  │ Distill  │  │ System   │       │
│  │ Endpoint │→ │ Endpoint │→ │ Endpoint │→ │ Endpoint │       │
│  └────┬─────┘  └────┬─────┘  └────┬─────┘  └────┬─────┘       │
│       │              │              │              │             │
│       ▼              ▼              ▼              ▼             │
│  ┌──────────────────────────────────────────────────────────┐    │
│  │              Process Manager (Router)                     │    │
│  │  Routes requests to appropriate worker pools              │    │
│  └──────────────────────────────────────────────────────────┘    │
│                                                                  │
│  Always responsive — never blocks on heavy work                  │
└──────────────────────────────────────────────────────────────────┘

          ┌───────────────────┼───────────────────┐
          │                   │                   │
          ▼                   ▼                   ▼
┌─────────────────┐  ┌─────────────────┐  ┌─────────────────┐
│  WORKER POOL A  │  │  WORKER POOL B  │  │  WORKER POOL C  │
│  (Lightweight)  │  │  (Heavy Search) │  │  (Batch Jobs)   │
│                 │  │                 │  │                 │
│  - Health check │  │  - STAR search  │  │  - Synonym gen  │
│  - Stats        │  │  - Exact search │  │  - Mirror prot  │
│  - System info  │  │  - Deep search  │  │  - Tag cleanup  │
│  - MCP tools    │  │  - Physics walk │  │  - WASM init    │
│  - Watchdog     │  │  - Context infl │  │                 │
│                 │  │                 │  │  Runs on demand  │
│  Non-blocking   │  │  Time-bounded   │  │  with timeout   │
└─────────────────┘  └─────────────────┘  └─────────────────┘

Component Breakdown

Main Thread (Always Available)

ResponsibilityDetails
HTTP serverExpress app listening on port 3160
Route dispatchParse request → route to appropriate worker pool
Health checkLightweight DB ping (SELECT 1) — <10ms
System infoProcess stats, uptime — <1ms
MCP stdioMessage passing with Claude/Cursor clients
Request routingSend to correct worker pool, await result

Worker Pool A — Lightweight (Inline on Main Thread)

JobMethodTimeout
Health checkInline (no worker)<10ms
StatsInline<1ms
System infoInline<1ms
MCP toolsInline (stdio)N/A

Worker Pool B — Heavy Search (Time-bounded)

JobMethodTimeout
STAR searchWorker thread30s
Exact searchWorker thread10s
Deep searchWorker thread60s
Physics walkerWorker thread30s
Context inflationWorker thread30s

Worker thread pool (3-5 threads, share memory with main) Timeout enforcement per query Shared LRU result cache (keyed by query hash, ~100 entries) |---|---|---| | Synonym generation | Child process | 5 min | | Mirror protocol | Child process | 2 min | | Tag cleanup | Child process | 30s | | WASM loading | Child process | 30s | | Distillation | Child process | 10 min | | Watchdog (file watcher) | Child process | N/A (long-running) |

Runs on demand with timeout + auto-restart on failure

Implementation Status

Phase 1: Route Dispatcher ✅ COMPLETE

  • File: engine/src/routing/dispatcher.ts
  • Class: RequestDispatcher — central coordinator between HTTP endpoints and worker pools
  • Features: Type-safe routing via RequestType enum, pool health monitoring, graceful shutdown
  • Configurable: Search timeout (30s), batch timeout (600s), max threads (3)

Phase 2: Worker Pools ✅ COMPLETE

PoolFileDescription
Pool Aengine/src/workers/pool-a/index.tsInline handlers for health, stats, system info — always available on main thread
Pool Bengine/src/workers/pool-b/index.tsWorker thread pool for STAR search, exact/deep search operations
Pool Cengine/src/workers/pool-c/index.tsChild process pool for synonym generation, mirror protocol, batch jobs

Phase 3: Integration Testing ✅ IN PROGRESS

  • Dispatcher integration with existing API routes in progress
  • Health monitoring and pool status reporting functional
  • Timeout enforcement active on all worker pools
  • Graceful degradation when pools unavailable (inline fallback)

Key Design Decisions

  1. Worker threads for search — share memory with main thread, fast IPC, good for CPU-bound but short-lived work
  2. Child processes for batch — complete isolation, can't block main thread, slower IPC but acceptable for infrequent jobs
  3. Timeout enforcement — every worker has a max runtime; excess → kill + partial result
  4. Pool sizing — bounded by available CPU cores; no unbounded spawning
  5. Health monitoring — each pool reports heartbeat; unhealthy pool → degrade gracefully

Verification Criteria

  • Health check responds <50ms regardless of synonym generation
  • Search queries return within timeout, never block HTTP server (testing)
  • Batch jobs run to completion without affecting HTTP responsiveness (testing)
  • Pool exhaustion degrades gracefully (queue + retry, not 503)
  • No --skip-* flags needed in production
  • All pools recoverable (auto-restart on crash)

API Base URL: http://localhost:3160/v1


Streaming Architecture

Streaming Search (/v1/memory/search/stream)

Purpose: Memory-efficient search with progressive results via Server-Sent Events (SSE)

Benefits:

  • 60% lower peak memory during large searches
  • Results arrive progressively (20 per batch by default)
  • GC hints between batches for mobile optimization
  • Configurable batch size via batch_size parameter

Flow:

Request → Query Parsing → Batch 1 (SSE emit) → Batch 2 (SSE emit) → ... → Completion Event

Streaming Ingest (/v1/ingest/streaming)

Purpose: Process large files in configurable chunks to prevent OOM errors

Benefits:

  • Handles files of any size without memory issues
  • Progress tracking with callbacks for monitoring ingestion progress
  • Configurable chunk size (default: 1MB) and batch processing parameters
  • Fallback to regular ingestion for smaller files (<1MB threshold)

Data Model: Compound → Molecule → Atom

Visual Representation

flowchart LR
    subgraph FILESYSTEM["Filesystem Source of Truth"]
        A["ChatSessions.yaml<br/>91.88MB"]
        B["~/.anchor/mirrored_brain/<br/>Plain Text Files"]
    end

    subgraph DATABASE["PGlite Index<br/>Pointers Only"]
        C["Compound<br/>File Reference"]
        D["Molecule<br/>Byte Offsets<br/>start: 1024<br/>end: 2048"]
        E["Atom<br/>Tags Only<br/>No Content"]
    end

    A -->|Mirror Protocol| B
    B -->|Atomize| C
    C -->|Contains| D
    D -->|Tagged With| E

    style FILESYSTEM fill:#e1f5ff
    style DATABASE fill:#fff4e1

Key Insight: Database is disposable. Content lives in ~/.anchor/mirrored_brain/. Database stores byte-offset pointers only.

Component Definitions

  • Compound: File/document reference (path, hash, metadata) - BEING REMOVED via migration
  • Molecule: Semantic chunk with byte offsets (start, end) and content
  • Atom: Tag/concept extracted from molecules (NOT content — content lives in ~/.anchor/mirrored_brain/)

Database Schema Reference

Overview

The Anchor Engine uses PGlite (PostgreSQL-compatible WASM database) as its disposable index. The schema follows a three-tier hierarchy:

  1. Atoms - Individual concepts/keywords with provenance
  2. Molecules - Semantic text chunks with byte offsets
  3. Compounds - File-level aggregation (deprecated, being removed)

Entity Relationship Diagram

erDiagram
    %% Atoms table - individual concepts
    atoms {
        TEXT id PK "UUID v4 identifier"
        TEXT source_path "File path where found"
        TEXT provenance "Source: internal/external/github"
        TEXT simhash "SimHash for dedup"
        TEXT embedding "Vector embedding (JSON)"
        TEXT content "Extracted text content"
        JSONB tags "Array of tag strings"
        JSONB entities "Named entity results"
        JSONB payload "Additional structured data"
    }

    %% Molecules table - semantic chunks
    molecules {
        TEXT id PK "UUID v4 identifier"
        TEXT content "Semantic chunk text"
        TEXT source_path "Direct file path reference"
        INTEGER start_byte "Byte offset start"
        INTEGER end_byte "Byte offset end"
        TEXT molecular_signature "64-bit Hamming SimHash"
        JSONB tags "Array of tag strings"
        JSONB entities "Named entity results"
    }

    %% Compounds table - DEPRECATED (being removed)
    compounds {
        TEXT id PK "UUID v4 identifier"
        TEXT path "File path reference"
        TEXT provenance "Source origin metadata"
        TEXT molecular_signature "Compound-level signature"
        TEXT[] atoms "Array of atom IDs (FK)"
        TEXT[] molecules "Array of molecule IDs (FK)"
    }

    %% Tags table - atom-tag relationships
    tags {
        TEXT atom_id FK "Reference to atoms.id"
        TEXT tag "Tag name/concept"
        TEXT bucket "Bucket for grouping"
    }

    %% Edges table - graph relationships
    edges {
        TEXT source_id FK "Reference to atoms.id"
        TEXT target_id FK "Reference to atoms.id"
        TEXT relation "Relationship type"
        REAL weight "Edge weight for ranking"
    }

    %% Sources table - source tracking
    sources {
        TEXT path PK "File path as unique key"
        TEXT hash "Content hash for dedup"
        INTEGER total_atoms "Count of atoms in this source"
        REAL last_ingest "Last ingestion timestamp"
    }

    %% Atom positions - lazy molecule inflation
    atom_positions {
        TEXT compound_id FK "Reference to compounds.id"
        TEXT atom_label "Atom/keyword label"
        INTEGER byte_offset "Position in source text"
    }

    %% Relationships
    atoms ||--o{ tags : "has_tags"
    atoms ||--o{ edges : "is_source"
    molecules ||--o{ atoms : "contains"
    compounds ||--o{ molecules : "contains"
    compounds ||--o{ atom_positions : "tracks"
    
    note "COMPOUNDS TABLE IS DEPRECATED Being removed in migration Phase 2. All data migrated to atoms/molecules." compounds;

Table Reference

atoms - Individual Concepts/Keywords

Stores individual extracted concepts with their provenance and metadata. Does not store full content (pointer-only architecture).

ColumnTypeDescription
idTEXT (UUID)Primary key, unique identifier
source_pathTEXTFile path where this atom was extracted from
timestampREALIngestion timestamp (Unix epoch seconds)
simhashTEXTSimHash for deduplication (fingerprint)
embeddingTEXTVector embedding as JSON array or null
vector_idBIGINTAuto-increment ID for vector database
provenanceTEXTSource origin: 'internal', 'external', 'github'
compound_idTEXTFK reference (deprecated, legacy compatibility)
sequenceINTEGERSequence number within source document
typeTEXTAtom type classification
hashTEXTContent hash for deduplication
molecular_signatureTEXT64-bit Hamming SimHash of parent molecule
start_byteINTEGERByte offset start in source file
end_byteINTEGERByte offset end in source file
numeric_valueREALNumeric value if present (for numbers)
numeric_unitTEXTUnit for numeric values (e.g., 'kg', 'm/s²')
contentTEXTExtracted text content of this atom
tagsJSONBArray of tag strings
entitiesJSONBNamed entity extraction results
payloadJSONBAdditional structured data (Crystal Atom)

Indexes:

  • idx_atoms_source_path - Fast lookup by file path
  • idx_atoms_provenance - Filter by source origin
  • idx_atoms_simhash - Deduplication queries
  • idx_atoms_timestamp - Recent atoms (DESC)
  • idx_atoms_compound_id - Legacy compound lookups
  • idx_atoms_payload_gin - GIN index for payload JSONB

molecules - Semantic Text Chunks

Stores semantic chunks of text with byte offsets for content extraction. Each molecule represents a meaningful segment (sentence, paragraph, or concept block).

ColumnTypeDescription
idTEXT (UUID)Primary key, unique identifier
contentTEXTSemantic chunk text content
compound_idTEXTFK reference to compounds (deprecated)
sequenceINTEGERSequence number within source document
start_byteINTEGERByte offset start in source file
end_byteINTEGERByte offset end in source file
typeTEXTType classification ('number', 'percentage', etc.)
numeric_valueREALParsed numeric value if applicable
numeric_unitTEXTUnit for numeric values
molecular_signatureTEXT64-bit Hamming SimHash for molecule
embeddingTEXTVector embedding as JSON array
timestampREALIngestion timestamp (Unix epoch)
tagsJSONBArray of tag strings
entitiesJSONBNamed entity extraction results
source_pathTEXTDirect file path reference
provenanceTEXTSource origin metadata

Indexes:

  • idx_molecules_source_path - Fast lookup by file path
  • idx_molecules_provenance - Filter by source origin
  • idx_molecules_compound_id - Legacy compound lookups
  • idx_molecules_timestamp - Recent molecules (DESC)
  • idx_molecules_signature - SimHash-based queries

compounds - File References (DEPRECATED)

Status: Being removed in migration Phase 2. This table served as an index/aggregation layer but is redundant given that atoms and molecules already store all necessary metadata.

ColumnTypeDescription
idTEXT (UUID)Primary key, referenced by atoms/molecules
pathTEXTFile path pointer
timestampREALIngestion timestamp
provenanceTEXTSource/provenance metadata
molecular_signatureTEXTCompound-level signature
atomsTEXT[]Array of atom IDs (foreign keys)
moleculesTEXT[]Array of molecule IDs (foreign keys)

Migration Note: All data from this table is being migrated to the atoms and molecules tables. After migration, this table will be dropped.


tags - Tag-Atom Relationships

The "nervous system" that connects atoms to conceptual buckets. Enables fast tag-based search and filtering.

ColumnTypeDescription
atom_idTEXTForeign key to atoms.id
tagTEXTTag name/concept (e.g., 'quantum', 'machine-learning')
bucketTEXTBucket for grouping tags (e.g., 'physics', 'ml')

Primary Key: Composite (atom_id, tag, bucket)

Indexes:

  • idx_tags_tag - Fast tag lookup
  • idx_tags_bucket - Bucket-based filtering
  • idx_tags_atom_id - Atom-to-tags resolution

edges - Graph Relationships

Stores relationships between atoms for the knowledge graph. Used by the STAR search algorithm's "Moons" component for semantic discovery.

ColumnTypeDescription
source_idTEXTForeign key to atoms.id (source atom)
target_idTEXTForeign key to atoms.id (target atom)
relationTEXTRelationship type (e.g., 'related_to', 'causes')
weightREALEdge weight for ranking/relevance

Primary Key: Composite (source_id, target_id, relation)


sources - Source Registry

Tracks ingestion sources and provides quick access to recently ingested files.

ColumnTypeDescription
pathTEXTFile path (primary key)
hashTEXTContent hash for deduplication
total_atomsINTEGERCount of atoms in this source
last_ingestREALLast ingestion timestamp

atom_positions - Atom Position Tracking

Tracks where specific atoms/keywords appear in documents. Used by the radial distiller for context inflation.

ColumnTypeDescription
compound_idTEXTForeign key to compounds.id
atom_labelTEXTAtom/keyword label (e.g., 'quantum')
byte_offsetINTEGERPosition in source text

Indexes:

  • idx_atom_positions_label - Fast keyword lookup

summary_nodes - Dreamer Abstractions

High-level summary nodes created by the "Dreamer" abstraction layer. These represent compressed knowledge representations.

ColumnTypeDescription
idTEXT (UUID)Primary key
typeTEXTNode type classification
span_startREALStart position in context window
span_endREALEnd position in context window
embeddingTEXTVector embedding for semantic search

github_repos - GitHub Repository Tracking

Tracks ingested GitHub repositories for incremental sync and status monitoring.

ColumnTypeDescription
idTEXT (UUID)Primary key
ownerTEXTGitHub username/organization
repoTEXTRepository name
branchTEXTGit branch (default: 'main')
bucketTEXTStorage bucket reference
github_urlTEXTFull GitHub URL
last_synced_atTIMESTAMPLast sync timestamp
last_sync_statusTEXTSync status ('success' | 'error')
last_errorTEXTError message if failed
total_filesINTEGERTotal files indexed
total_atomsINTEGERTotal atoms extracted
total_size_bytesINTEGERTotal size in bytes

distills - Distillation Output Tracking (Standard 031)

Stores metadata pointers to distillation output files on disk. Does not store the actual content.

ColumnTypeDescription
idTEXT (UUID)Primary key
timestampTEXTISO timestamp of distillation
filenameTEXTBase filename
file_pathTEXTFull path to distill file
line_countINTEGERTotal lines in output
lines_uniqueINTEGERUnique lines (deduplicated)
compression_ratioREALCompression efficiency metric
source_sessionsTEXT[]Array of session IDs
source_filesTEXT[]Array of file paths processed
parametersJSONBProcessing parameters used

engrams - Lexical Sidecar

Simple key-value store for quick lookups. Used for storing computed values or cached results.

ColumnTypeDescription
keyTEXTLookup key (primary key)
valueTEXTAssociated value

synonyms - Query Expansion Terms

Stores synonym mappings for search query expansion. Helps improve recall by expanding search terms.

ColumnTypeDescription
termTEXTBase search term (primary key)
synonymsTEXTComma-separated synonym list
created_atTIMESTAMPCreation timestamp

Schema Migration Status

TableStatusNotes
atoms✅ ActivePrimary concept storage
molecules✅ ActiveSemantic chunk storage
compounds⚠️ DeprecatedBeing removed (migration in progress)
tags✅ ActiveTag-atom relationships
edges✅ ActiveGraph edges for STAR search
sources✅ ActiveSource tracking
atom_positions✅ ActivePosition indexing
summary_nodes✅ ActiveDreamer abstractions
github_repos✅ ActiveGitHub ingestion
distills✅ ActiveDistillation metadata (Standard 031)
engrams✅ ActiveKey-value store
synonyms✅ ActiveQuery expansion

Migration Notes

Phase 1 (Complete): Schema analysis and data mapping completed. All unique fields from compounds have been identified and mapped to atoms and molecules.

Phase 2 (In Progress): Running migration script to:

  1. Copy provenance and molecular_signature from compounds to molecules/atoms
  2. Drop the compounds table

Phase 3 (Pending): Update ingestion pipeline to skip compound creation during normal operations.

Migration to pointer-only storage is tracked via the schema migration process described above. The compounds table has been removed and all data now flows through atoms/molecules tables with direct source_path references.


STAR Search Algorithm

Search Flow

flowchart TB
    A[User Query<br/>Coda C-001 Rob Dory] --> B{Budget Check<br/>max_chars > 65k?}

    B -->|No| C[Standard Search<br/>70/30 Budget<br/>1-hop<br/>Temporal Decay]
    B -->|Yes| D[Max-Recall Search<br/>Zero Decay<br/>3-hop<br/>200 nodes/hop]

    C --> E[Query Parsing<br/>NLP + Key Terms]
    D --> E

    E --> F[Parallel Searches<br/>5 Sub-queries<br/>4-word chunks]

    F --> G[Merge & Deduplicate<br/>60 Atoms]

    G --> H{Max-Recall?}
    H -->|Yes| I[Context Inflation<br/>n-1, n+1 from Disk<br/>8,550 chars/atom]
    H -->|No| J[Return Results<br/>16k-32k chars]

    I --> K[Serialize Context<br/>512k-618k chars]
    J --> K

    K --> L[Return to User]

    style D fill:#ffeb3b
    style I fill:#ffeb3b
    style K fill:#c8e6c9

Unified Field Equation

Gravity(atom, anchor) = α × (C × e^(-λΔt) × (1 - d/64))

Where:
  α (Alpha)     = Damping factor (0.85 standard, 1.0 max-recall)
  C             = Co-occurrence (shared tags via SQL JOIN)
  e^(-λΔt)      = Temporal decay (λ=0.00001 standard, 0.0 max-recall)
  d             = SimHash Hamming distance (0-64 bits)
  (1 - d/64)    = SimHash gravity (1.0 = identical, 0.0 = orthogonal)

Parameter Comparison

ParameterStandardMax-RecallImpact
α (Damping)0.851.0Zero signal loss on multi-hop
λ (Decay)0.000010.0Age irrelevant in max-recall
Max Hops133× deeper graph traversal
Max/Hop502004× more nodes per hop
Temperature0.20.84× more serendipitous

Search Strategy

70% Planets: Direct FTS matches
30% Moons: Graph-discovered associations via tag-walker

Deduplication Pipeline

5-Layer Dedup Strategy

flowchart TB
    A[Raw Search Results<br/>44 Items] --> B[Sort by Score<br/>Descending]
    B --> C[For Each Candidate]
    C --> D{Has Content && >20 chars?}
    D -->|No| E[Keep Automatically]
    D -->|Yes| F[1. Geometric Dedup<br/>50% Overlap]
    F --> G{Duplicate?}
    G -->|Yes| H[Skip]
    G -->|No| I[2. MD5 Fingerprint]
    I --> J{Duplicate?}
    J -->|Yes| H
    J -->|No| K[3. Containment]
    K --> L{Duplicate?}
    L -->|Yes| H
    L -->|No| M[4. Fuzzy Prefix]
    M --> N{Duplicate?}
    N -->|Yes| H
    N -->|No| O[5. SimHash < 5]
    O --> P{Duplicate?}
    P -->|Yes| H
    P -->|No| Q[Keep Candidate]
    Q --> R{More?}
    R -->|Yes| C
    R -->|No| S[Final: 33 Items]

    style O fill:#ffeb3b
    style S fill:#c8e6c9

Dedup Layer Details

LayerCatchesExample
1. GeometricSame-file overlapping windowsMolecule A: bytes 100-200, B: bytes 150-250 → 50% overlap
2. Content FingerprintCross-file exact duplicatesSame paragraph in multiple files
3. ContainmentOne result is subset of anotherFull document vs. excerpt
4. Fuzzy PrefixNear-exact with whitespace/timestamp diffsSame content, different formatting
5. SimHash DistanceCross-file near-duplicates ⭐Paraphrased versions, modified quotes

Performance

  • Before v5.2.0: 25-35% dedup rate
  • After v5.2.0: 40-50% dedup rate

Max-Recall Auto-Trigger

Trigger Flow

flowchart LR
    A[User Sets Volume<br/>Slider to Max] --> B{max_chars > 65,536?}
    B -->|Yes| C[Auto-Trigger Max-Recall]
    B -->|No| D[Standard Mode]
    C --> E[Log: SEARCH_AUTO_MAX_RECALL]
    E --> F[Split Query: 4-word Chunks, 5 Max]
    F --> G[Parallel Search: Full Budget Each]
    G --> H[Merge: 60 Atoms]
    H --> I[Context Inflation: n-1, n+1]
    I --> J[Return: 618k Chars, 98% Budget]

    style C fill:#ffeb3b
    style I fill:#ffeb3b
    style J fill:#c8e6c9

Trigger Conditions

  1. Manual: strategy: 'max-recall' in request body
  2. Automatic: max_chars > 65,536 (estimated_tokens > 16,000)

Phoenix Protocol Backup/Restore

Backup & Restore Flow

flowchart TB
    subgraph BACKUP["Backup Process"]
        A[User: 💾 Eject Memory]
        B[Export DB: atoms, sources, engrams]
        C[Aggregate by Source]
        D[Write JSON Backup]
    end
    subgraph RESTORE["Phoenix Restore"]
        E[User: Select Backup]
        F[Wipe Database]
        G[Restore Tables]
        H[Rebuild Filesystem]
        I[Verify Integrity]
    end
    A --> B --> C --> D
    E --> F --> G --> H --> I
    style BACKUP fill:#e3f2fd
    style RESTORE fill:#fff3e0
    style H fill:#ffeb3b

Key Feature: Phoenix Protocol rebuilds both database AND filesystem structure from backup.


Performance Benchmarks

Search Performance

StrategyLatencyContextUse Case
Standard~300ms16k-32k charsDaily queries
Max-Recall~50s512k-618k charsResearch, audits

Context Retrieval

  • Standard: 32k chars average
  • Max-Recall: 618k chars (exceeds 524k whitepaper claim by 18%)

Deduplication

  • Before v5.2.0: 25-35% dedup rate
  • After v5.2.0: 40-50% dedup rate (+15%)

Memory Management

flowchart LR
    A[Startup: ~500MB] --> B{Large File?}
    B -->|Yes 90MB+| C[Peak: ~1.6GB]
    B -->|No| D[Steady: ~500MB]
    C --> E[Idle: 5min]
    D --> E
    E --> F[GC: ~650MB<br/>60% Reduction]
    style C fill:#ffeb3b
    style F fill:#c8e6c9
  • Peak: ~1.6GB (during 90MB file ingestion)
  • Idle: ~650MB (after 5min timeout + GC)
  • Reduction: 60% memory savings after idle cleanup

Key Metrics

MetricResultTargetStatus
90MB Ingestion~178s<200s
Memory Peak~1.6GB<2GB
Search Latency (p95)~150ms<200s
SimHash Speed~2ms/atom<5ms

File Locations

ComponentPathPurpose
UIengine/public/index.htmlReact frontend
Engineengine/dist/Compiled TypeScript
Database~/.anchor/context_data/PGlite files (disposable)
Mirror~/.anchor/mirrored_brain/Source of truth (gitignored)
Inbox~/.anchor/inbox/, ~/.anchor/external-inbox/Ingestion sources
Backups~/.anchor/backups/Phoenix Protocol backups
Logs~/.anchor/logs/Engine logs
Standardsspecs/current-standards/Architecture specs

Project History (July 2025 - June 2026)

PhaseDateMilestone
InceptionJuly 2025Project started, initial architecture
FoundationAug-Sep 2025CozoDB integration, core ingestion
StabilizationOct-Nov 2025PGlite migration, reliability fixes
AccelerationDec 2025Rust WASM packages (@rbalchii/*-wasm), zero native compilation
Browser ParadigmJan 2026Tag-Walker replaces vector search
Standards ConsolidationFeb 2026Unified 29 standards (001-029)
Security HardeningApr 2026Path traversal, SQL injection, auth bypass, API key strength
Streaming & ObservabilityMay 2026v5.2.0: Streaming search/ingest, Zod validation, performance monitoring
Documentation Drift RepairJun 2026v5.3.0: Renumbered standards 001-038, removed deprecated packages/, cleaned root dead code

File Structure

anchor-engine-node/
├── README.md              # Quick start & overview
├── CHANGELOG.md           # Version history
├── docs/
│   ├── whitepaper.md      # The Sovereign Context Protocol (95% compliance)
│   └── INDEX.md           # Documentation navigation hub
├── specs/
│   ├── spec.md            # This file
│   ├── tasks.md           # Current sprint tasks
│   ├── plan.md            # Roadmap
│   └── current-standards/ # Active architecture standards (001-038)
├── engine/                # Core engine source
│   ├── src/
│   │   ├── config/        # Zod validation schemas
│   │   ├── services/      # Core services
│   │   ├── routes/v1/     # API endpoints
│   ├── public/            # React frontend (CDN-based)
│   ├── context/           # Context engine runtime data
│   └── tests/             # Test suites
└── user_settings.json.template  # Version source (generates ~/.anchor/user_settings.json)

Test Framework Architecture

Test Suite Structure

The test suite is organized across two locations:

Primary test suite (engine/tests/):

engine/tests/
├── unit/              # Unit tests for individual components (*.test.ts, *.vitest.ts)
├── integration/       # Integration tests for cross-component workflows
├── live-fire/         # End-to-end smoke tests
├── benchmarks/        # Performance benchmark tests
├── helpers/           # Shared test utilities and mocks
└── setup.ts           # Vitest global setup

Legacy/runner test suite (tests/):

tests/
├── benchmarks/        # Benchmark runner scripts
├── e2e/              # End-to-end test workflows
├── fixtures/         # Test data and fixtures
├── framework/        # Test framework utilities
├── legacy/           # Deprecated Jest-based tests (migrating to vitest)
└── run-tests-with-logger.js  # Unified test runner

Test Framework Decision Matrix

Use CaseFrameworkRationale
WASM/ASM integration pointsVitestESM/WASM support required
PGlite database operationsVitestNative async/await support
Critical path verification (<5 min)Native (P0 smoke)Fast execution, simple setup
Legacy tests (migration zone)Jest → VitestGradual migration in progress

Test Pipeline Phases

Phase 1: P0 Smoke Tests - Critical path verification, must complete in <5 minutes. If failed, abort entire pipeline.

Phase 2: Vitest Engine Tests - Comprehensive coverage of all engine components including WASM integration and PGlite operations.

Phase 3: Integration Tests - Cross-component workflows (ingestion → search → distillation).

Phase 4: Legacy Jest Tests - Deprecated tests marked for migration to vitest. Results logged separately.

Test Result Logging

All test results are saved to .anchor/logs/ for human review:

.anchor/logs/search-tests/
├── P0-semantic-search-complex-2026-05-18T12-00-00.json
├── P1-tag-search-multi-filter-2026-05-18T12-00-30.json
└── ...

.anchor/logs/distillation-tests/
├── unseeded-2026-05-18T12-01-00.json
└── seeded-context-2026-05-18T12-01-30.json

Search Algorithm Testing Methodology

Test Order: Hardest → Easiest

Tests are ordered from most challenging to simplest queries. This approach stress-tests the system first and reveals edge cases early.

PriorityCategoryExample QueryPurpose
P0Semantic/Complex"authentication and authorization in Node.js best practices"Multi-concept, requires understanding relationships
P1Tag-based Advanced#test #api #node with filtersTests tag intersection logic
P2Byte Offset Search"function findAnchors" with offset trackingVerifies content boundary handling
P3FTS Basic"workspace" or "atom"Standard full-text search
P4Empty/All Results"" (empty query)Returns all indexed content

Distillation Testing

Tests cover both unseeded and seeded distillation scenarios:

  • Unseeded: No prior context, tests basic compression
  • Seeded: With context window, tests knowledge retention

API Endpoints

GET  /health                     # System status
POST /v1/ingest                  # Ingest content
POST /v1/ingest/streaming        # Stream large file ingestion
POST /v1/memory/search           # Search memory
POST /v1/memory/search/stream    # Streaming search with SSE results
POST /v1/memory/explore          # BFS graph traversal (illuminate)
GET  /v1/buckets                 # List buckets
GET  /v1/tags                    # List tags

Active Standards (Unified: 001-038)

#NameStatus
001Memory-Safe Ingestion10MB file limit, 10,000 molecule limit
002Reproducible BenchmarkingStandardized test framework
003MCP Tool InterfaceModel Context Protocol integration
004Streaming SearchSSE-based result streaming
005Adaptive Concurrency ControlMemory-aware search pacing
006Mobile Search OptimizationLow-memory device support
007PGlite Memory OptimizationWASM buffer tuning
008Radial DistillationKnowledge compression
009Illuminate BFS TraversalGraph exploration
010Radial Distillation v2Decision Records output
011Security HardeningAPI key validation
012Data IntegritySource tracking
013WASM FallbackRust WASM fallbacks for performance-critical operations
014Circuit Breaker PatternResilience pattern for external calls
015Operational VisibilitySystem status endpoints
016Search Algorithm TestingHardest→easiest methodology
017Configuration ManagementPath/setting management
018MCP Integration TestingTool validation
019Dependency ValidationPackage verification
020AST Parser WASMTree-sitter WASM integration
021Configuration ValidationZod schema validation
022Code AnalysisESLint integration
023Test Environment ConsistencyReproducible test environments
024Ephemeral DatabaseDisposable PGlite index
025Pointer-Only StorageByte-offset indexing
026Documentation HygieneStandard updates
027Auth Bypass PreventionTest endpoint removal
028API Key Strength32-128 chars, mixed case
029Path Traversal PreventionInput validation
030Zero-Copy DeduplicationSHA-256 before UTF-8
031Distillation Output StorageMetadata pointers in DB
032Pain Point LoggingOperational logging
033Self-Contamination PreventionAvoid ingesting own output
034Unified Test PipelineTest orchestration
035Path Usage ValidationRuntime path verification
036Tag-Based DistillationTag-driven knowledge compression
037Search Algorithms ComprehensiveFull search methodology
038API Error Handling StandardConsistent error responses

All active standards live in specs/current-standards/.


API Reference

All routes served from http://localhost:3160/v1/. Route files live in engine/src/routes/v1/.

Route FileEndpointsPurpose
admin.ts/v1/admin/*Administrative operations
atoms.ts/v1/atomsAtom (entity/keyword) queries
backup.ts/v1/backup/*Database backup and restore
distills.ts/v1/distills/list, /v1/distills/:id, /v1/distills/:id/streamDistillation output access
encryption.ts/v1/encrypt/*Content encryption/decryption
git.ts/v1/git/cloneGitHub repository cloning
ingest.ts/v1/ingest, /v1/ingest/streamingFile ingestion (standard + streaming)
memory.ts/v1/memory/search, /v1/memory/search/stream, /v1/memory/distillCore memory operations
molecules.ts/v1/moleculesMolecule queries
research.ts/v1/research/*Web research operations
search.ts/v1/searchSearch with distill: prefix support
settings.ts/v1/settingsConfiguration management
stats.ts/v1/statsSystem metrics (uptime, memory, DB counts)
system.ts/v1/system/*, /v1/health, /v1/watchdog/*System health, watchdog control
tags.ts/v1/tagsTag queries and management

Search supports special prefixes:

  • distill: — list all distills
  • distill:<bucket> — query distills by bucket name

Health check: GET http://localhost:3160/health


Documentation


Repository: https://github.com/RSBalchII/anchor-engine-node License: AGPL-3.0 Production Status: ✅ Ready (February 20, 2026) | Security Hardening Complete | v5.3.0 Documentation Drift Repair