# System architecture

## The shape of the system

Taaweel 2.0 is a Fastify app (`src/`) with its own SQLite database, its own port (3901) and a
server-rendered dashboard. It is self-contained: its corpora live in its own `data/corpora/`, opened **read-only**.

```mermaid
flowchart TD
  subgraph User["You"]
    U1["Refine chat"] --> U2["Lock question"] --> U3["Run"] --> U4["Report + chat + regenerate"]
  end
  subgraph Server["Taaweel (Fastify :3901)"]
    API["REST API\n/api/*"] --> ENG
    DASH["Dashboard\n/dashboard/*"] --> ENG
    DOCS["Docs viewer\n/docs"]
    ENG["Research engine\n13-stage runner"]
    ENG --> LED[("Evidence ledger\nburhan.db")]
    ENG --> ART[("Artifacts\nreport-N.md")]
    CHAT["Chat service\nrefine · discuss"] --> ENG
  end
  subgraph Read["Read-only corpora (data/corpora)"]
    Q[("Quran text\n6,236 verses · 77k tokens · roots")]
    L[("Lane lexicon\n+ classical dictionaries")]
    T[("Tafsīr ×5\nṬabarī · Kathīr · Quṭubī · Baghawī")]
    H[("Hadith\n34k reports, graded")]
  end
  LLM["LLM provider\ndeepseek / GLM / openai"]
  ENG -->|"SELECT only"| Q & L & T & H
  ENG -->|"judgment calls, JSON-validated"| LLM
  U1 & U4 --> API
```

## Layered design

| Layer | Files | Responsibility |
| --- | --- | --- |
| Config | `src/config.ts` | ports, paths, provider, per-stage models, `provider:model` specs |
| Data | `src/db.ts`, `src/repos/*` | Taaweel's own tables (runs, ledger, claims, chats…) + read-only corpus repos |
| LLM | `src/llm/*` | provider-agnostic client, JSON extraction + zod validation + retry, cost table, mock fixtures |
| Method | `src/pipeline/*`, `src/services/*` | the 13 stages, cartography, anchors, trial, science, examination, uṣūl |
| Interface | `src/routes/*` | REST API, dashboard, report viewer, meta report, docs |
| Seeds | `src/seed.ts` | attribute-lexeme families (cosmic, psyche, biology…) + starter hadith |

## Key design decisions

1. **Everything through the ledger.** Stages never talk to each other directly; each reads the
   ledger the previous ones wrote. That makes runs **resumable** (rebuild-from any stage) and every
   claim **auditable** (the `[E-n]` chain).
2. **Code decides what code can decide.** Concordance, clustering, pairing stats, scope
   partitioning, blind scoring, fit arithmetic, the L7 gate, terminology guards — all pure code.
   The LLM judges, drafts, and objects — inside zod-validated contracts with retry.
3. **The engine is provider-agnostic.** DeepSeek, GLM (Z.AI), OpenAI, OpenRouter or a deterministic
   mock — one env var. Cheap models work because the pipeline decomposes reasoning into small,
   grounded, code-checked steps.
4. **Corpora are pristine.** Corpus connections are opened `readonly` with `query_only = ON`; all
   writes go to `taaweel/data/`.
5. **The report is assembled, not authored.** Deterministic sections (corpus map, ledger, trials)
   come straight from the database; LLM prose fills only the discussion and answer, and its
   citations are validated against the ledger before printing.
