Skip to content

Latest commit

 

History

28 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Four Sided Triangle

there is nothing new under the sun

Four-Sided Triangle Logo

Overview

Four-Sided Triangle is a retrieval-augmented generation system built around a specific claim: a passage that matches a query's tokens is not necessarily an answer to the question the query was asked from. The theoretical basis for this distinction is developed in publications/federated-retrieval-augmentation, which models retrieval as an act of individuation against a receiver's own accumulated context, rather than a lookup scored against the query alone. The repository implements that model at three levels: an eight-stage processing pipeline in Python for domain-expert knowledge extraction, a Rust core for the computationally heavy parts of that pipeline, and a standalone TypeScript implementation (sdk-ts/) of the paper's core primitives that can be embedded in other projects without the Python backend.

Why content and meaning are treated as separate problems

Standard RAG pipelines rank passages by similarity to the query and return the top-scoring result. This works when the query's meaning and its literal tokens coincide, and fails otherwise: a correct, well-sourced passage can still be irrelevant to the specific problem the querying agent already has in front of them, because relevance is computed against the query string, not against what the querier already knows. The paper referenced above formalizes this as a distinction between a claim's content (its literal tokens) and its meaning (its individuating cut against a specific receiver's own graph of prior context), proves the two are not generally equal, and derives from that a set of concrete requirements: retrieval should be evaluated per receiver rather than globally; a claim should not be reported as reliable on the strength of a single source, or even two, without a third independent corroborating source; and a search should terminate by exhausting the available sources rather than by crossing a similarity threshold. The eight-stage pipeline and the sdk-ts library are two different implementations of this same set of requirements, aimed at different deployment shapes.

Repository layout

four-sided-triangle/
├── app/                    Python pipeline (FastAPI, 8-stage orchestrator)
├── src/                    Rust core (Bayesian inference, fuzzy evidence,
│                           metacognitive optimization, Turbulance DSL)
├── frontend/               Next.js client for the Python pipeline's API
├── sdk-ts/                 Standalone TypeScript library implementing the
│                           paper's primitives directly (no Python backend
│                           required) — receiver graphs, coherence-triangle
│                           grounding, closure, water-filling attention
├── four-corners/           Hosted product built on sdk-ts: an account/
│   ├── server/             pairing backend (Node/TS + SQLite) and a
│   └── worker/             downloadable Rust worker that pairs to an
│                           account via a one-time token and runs
│                           individuation compute locally
├── publications/           The paper. Read this first for the theory.
├── backend/                Distributed-computing helpers (Ray/Dask)
├── docs/                   Architecture decision records
└── scripts/                Utility scripts

The Python pipeline

The pipeline processes a query through eight stages, coordinated by a metacognitive orchestrator that tracks intermediate state in a working memory store and evaluates output quality at each stage:

Stage Purpose
0. Query Processor Structures a natural-language query: intent, entities, constraints
1. Semantic ATDB Reformulates the query and detects LLM throttling patterns that would otherwise degrade output quality
2. Domain Knowledge Extraction Queries domain-adapted models and fuses their output, with consensus detection across sources
3. Reasoning Optimization Applies mathematical/logical reasoning to the problem space
4. Solution Generation Produces candidate solutions from the reasoning output
5. Response Scoring Scores candidates against quality and preference models
6. Ensemble Diversification Selects a diverse, high-quality subset of candidates
7. Threshold Verification Checks candidates for logical consistency against known facts before release

Supporting components:

  • Working memory — per-session state isolation across the pipeline, so concurrent queries do not share intermediate results.
  • Process monitor — evaluates each stage's output against completeness, consistency, and confidence criteria, and triggers a refinement pass when a stage's output falls below threshold.
  • Glycolytic query investment cycle (app/core/glycolytic_cycle.py) — allocates computational budget across query sub-tasks based on expected information yield, tracked and adjusted over time.
  • Semantic ATDB (app/core/stages/stage1_semantic_atdb/) — detects when a model's output shows signs of throttling (truncation, generic hedging, reduced depth) and selects a reformulation strategy in response.
  • Solver dispatcher — routes a sub-problem to either an LLM or a traditional mathematical solver depending on the problem's structure.

Stage-level model choices (which base models, at what temperature, for which stage) are documented per-stage under app/core/stages/, not reproduced here, since they are configuration rather than architecture.

Turbulance

app/turbulance (Python) and src/turbulance (Rust) implement a small domain-specific language for expressing a complete research protocol — hypothesis, data sources, and analysis steps — as a single script, compiled into an execution plan for the pipeline above. This is an alternative entry point to the pipeline, not a separate system; see app/turbulance/parser.py and src/turbulance/compiler.rs for the grammar and compilation target.

The Rust core

src/ contains the computationally intensive parts of the pipeline — Bayesian evidence networks, fuzzy inference, and metacognitive optimization — exposed to the Python orchestrator through pyo3 FFI bindings (app/core/rust_integration.py). This is a single pyo3 extension-module crate, not a standalone binary; it is built with cargo build --release and loaded by the Python process. Specific performance figures depend on hardware, workload, and problem size and are not reproduced here without a benchmark to cite; if you need numbers for your own workload, profile it directly.

sdk-ts: the paper's primitives as a standalone library

sdk-ts/ implements the same theoretical model as a dependency-free TypeScript library, usable in any project without running the Python backend. Each module corresponds to one section of the paper:

Module Implements
graph.ts Weighted contact graphs, minimum-cut separation cost via max-flow
receiver.ts A per-user/per-project receiver graph with append-only history
catalysis.ts Coherence: a claim is marked grounded only once three independent, mutually-supporting sources back it
closure.ts Search termination by exhausting available sources, rather than by a similarity threshold
federation.ts, sources/ Combining multiple sources (local files, APIs) into one queryable pool
waterfill.ts Optimal allocation of a fixed attention/compute budget across competing sources
table.ts, routeAudit.ts Multi-hop reasoning and cross-source verification (require a supplied LLM client)

The public entry point, createIndividuator(...).ask(query), returns a result tagged with one of grounded, single-sourced, two-sourced, contested, or declined — the library does not return a bare matching passage as if it were an answer; a query result is qualified by how well it is actually supported. See sdk-ts/README.md for usage and sdk-ts/tests/ for a suite of unit tests, several of which reproduce specific numerical claims from the paper's own worked examples as assertions (exact floor values, coherence-robustness at a threshold of three sources, water-filling allocations checked against grid search).

four-corners: an account-backed deployment of sdk-ts

four-corners/ packages sdk-ts as a product with user accounts, rather than requiring each consumer to embed the library directly:

  • server/ (Node/TypeScript, SQLite) — accounts, per-user source configuration, and a device-pairing flow: the web UI issues a short-lived, one-time code, and a device redeems it for a long-lived credential.
  • worker/ (Rust) — a downloadable binary that redeems a pairing code and then runs the individuation computation locally, on the user's own machine, rather than centrally. worker/src/ is a direct Rust port of sdk-ts/src/, module for module; worker/tests/ reuses the same fixture values as sdk-ts/tests/ as a cross-implementation check.

The rationale for running compute on the paired device rather than server-side is that the receiver graph is per-user state (Theorem 8.9 in the paper: the graph accretes monotonically and must not be silently reset or shared), and the coherence and attention-allocation computations involved are exact numerical work better suited to hardware the user controls than to a metered shared service.

Getting started

Python pipeline

Prerequisites: Python 3.10+, Rust 1.70+ and Cargo, Docker and Docker Compose (for containerized deployment), a CUDA-capable GPU if you intend to run the larger stage models locally.

python -m venv venv
source venv/bin/activate          # Windows: venv\Scripts\activate
cargo build --release             # builds the Rust core (src/)
pip install -r requirements.txt
python -m app.main

Or via Docker:

docker-compose up -d
# API at http://localhost:8000

Configuration is defined in app/config/ (config.py, settings.py, validators.py); see app/config/README.md.

sdk-ts

cd sdk-ts
npm install
npm run build
npm test

four-corners

cd four-corners/server
npm install
JWT_SECRET=<secret> npm run dev   # server/src/main.ts, listens on :4000 by default

cd ../worker
cargo build --release
./target/release/four-corners-worker pair --code <CODE> --server http://localhost:4000

API reference (Python pipeline)

Routers are defined in app/api/endpoints.py and mounted in app/api/app.py:

  • query_router (/query, also reachable under /api/query) — POST /process runs a query through the full pipeline.
  • metrics_router (/metrics, also under /api/metrics) — metrics calculation endpoints.
  • modeler_router (/modeler, also under /api/modeler) — the standalone modeler component.
  • turbulance_router (/turbulance) — parse, compile, execute, validate, statistics for Turbulance scripts.
  • GET /health — service health check.

Full request/response schemas are served at /docs and /redoc on the running application; that is the authoritative reference, not this file.

Architecture decision records

Significant architectural decisions are recorded under docs/adr/: module naming conventions, error-handling strategy, dependency injection, and pipeline stage design. See docs/adr/index.md.

Contributing

See docs/CONTRIBUTING.md.

License

MIT. See LICENSE.

Releases

Packages

Used by

Contributors

Languages