# youcef3939/trace architecture

> Architecture diagram of the GitHub repository youcef3939/trace, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/youcef3939/trace
- Repository: https://github.com/youcef3939/trace

## Overview

#### Purpose and Runtime
TRACE is a Next.js/React media-information-literacy platform. Users submit text, URLs, or images for an explainable, evidence-grounded investigation rather than a bare truth verdict. The App Router supplies both rendered pages and Node-backed route handlers. Primary shell and landing entry points are `app/layout.tsx`, `app/page.tsx`, and `app/globals.css`.

#### User-Facing Investigation Experience
The investigation workflow is exposed at `app/investigate/page.tsx`, with its main interactive presentation in `components/InvestigateExperience.tsx` and `components/InvestigationLive.tsx`. The browser submits user material to `app/api/investigate/route.ts`; the returned investigation chain is rendered as a guided, inspectable experience rather than a single score.

#### Investigation Pipeline
`lib/pipeline/runInvestigation.ts` is the orchestration boundary for the core verification flow. It composes:
- claim identification: `lib/pipeline/extractClaims.ts`
- source-origin analysis: `lib/pipeline/traceSource.ts`
- supporting/contradicting web evidence: `lib/pipeline/retrieveEvidence.ts`
- final evidence-based judgment: `lib/pipeline/assess.ts`

This separation makes individual stages independently replaceable while preserving a single API-level workflow.

#### AI, Retrieval, and Grounding Boundaries
LLM-facing work is concentrated in pipeline stages, while web retrieval is abstracted through `lib/tavily.ts` and `lib/verifySearch.ts`. `lib/manipulationSignals.ts` adds heuristic indicators such as potentially misleading framing. The intended control flow is: submitted content → extracted claims → source/evidence retrieval → assessment with explanation. Retrieval results are the factual boundary that should constrain generated assessments.

#### Image/OCR Input Path
Image submission is handled independently by `app/api/ocr/route.ts`, with `eng.traineddata` available as OCR language data. OCR-derived text can then enter the same investigation pipeline as typed or pasted content, keeping multimodal ingestion separate from claim assessment.

#### Learning and Lab Modes
Educational content is delivered through `app/learn/page.tsx`, `components/LearnContent.tsx`, and the explanatory site sections. A separate interactive practice mode lives at `app/lab/page.tsx`; `components/lab/LabGame.tsx` drives the challenge UI using local claim content in `lib/lab/claims.ts`. Lab evaluation is exposed through `app/api/lab/evaluate/route.ts`, separating pedagogy/game state from real-content investigations.

#### Persistence and Data Model
Supabase is the persistence integration boundary, initialized through `lib/supabase.ts` with relational schema definitions in `supabase/schema.sql`. Investigation/source/evidence relationships are modeled for durable history and traceability. Shared trace-oriented types are centralized in `types/trace.ts`, providing a contract between pipeline/API output and UI consumers.

#### Supporting Site Surface
Informational routes—`app/how-it-works/page.tsx`, `app/resources/page.tsx`, and `app/about/page.tsx`—frame the product and learning model. Shared navigation and framing are provided by `components/Nav.tsx`, `components/Footer.tsx`, and reusable presentation components, while API routes remain the operational boundary for OCR, investigations, and lab evaluation.

Main entry points: [`lib/pipeline/runInvestigation.ts`](https://github.com/youcef3939/trace/blob/main/lib/pipeline/runInvestigation.ts) (Investigation orchestrator), [`components/InvestigateExperience.tsx`](https://github.com/youcef3939/trace/blob/main/components/InvestigateExperience.tsx) (Investigation experience), [`app/api/investigate/route.ts`](https://github.com/youcef3939/trace/blob/main/app/api/investigate/route.ts) (Investigation API), [`lib/pipeline/retrieveEvidence.ts`](https://github.com/youcef3939/trace/blob/main/lib/pipeline/retrieveEvidence.ts) (Evidence retrieval)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_web["Next.js App"]
  node_shell["App shell<br/>Next.js layout<br/>[layout.tsx]"]
  node_landing["Landing page<br/>Next.js page<br/>[page.tsx]"]
  node_investigate_page["Investigate route<br/>Next.js page<br/>[page.tsx]"]
  node_investigate_api{{"Investigation API<br/>route handler<br/>[route.ts]"}}
  node_ocr_api{{"OCR API<br/>route handler<br/>[route.ts]"}}
  node_learn_page["Learning content<br/>Next.js page<br/>[page.tsx]"]
  node_lab_page["Lab route<br/>Next.js page<br/>[page.tsx]"]
  node_lab_api{{"Lab evaluation API<br/>route handler<br/>[route.ts]"}}
end

subgraph group_ui["User Experiences"]
  node_investigate_ui["Investigation experience<br/>React UI"]
  node_live_results["Live investigation view<br/>React UI"]
  node_learn_ui["Learning experience<br/>React UI<br/>[LearnContent.tsx]"]
  node_lab_game["Lab game<br/>React UI<br/>[LabGame.tsx]"]
end

subgraph group_pipeline["Investigation Pipeline"]
  node_orchestrator["Investigation orchestrator<br/>pipeline service"]
  node_claims["Claim extraction<br/>pipeline stage<br/>[extractClaims.ts]"]
  node_source_trace["Source tracing<br/>pipeline stage<br/>[traceSource.ts]"]
  node_evidence["Evidence retrieval<br/>pipeline stage"]
  node_assessment["Evidence assessment<br/>pipeline stage<br/>[assess.ts]"]
end

subgraph group_data["Data &amp; Integrations"]
  node_ocr_language["OCR language data<br/>OCR model data<br/>[eng.traineddata]"]
  node_search["Web retrieval adapters<br/>retrieval integration<br/>[tavily.ts]"]
  node_verification_search["Verification search<br/>retrieval helper<br/>[verifySearch.ts]"]
  node_signals["Manipulation signals<br/>heuristics"]
  node_trace_types["Trace contract<br/>shared types<br/>[trace.ts]"]
  node_supabase[("Supabase persistence<br/>database integration<br/>[supabase.ts]")]
  node_schema["Trace schema<br/>SQL schema<br/>[schema.sql]"]
  node_lab_claims["Lab claims<br/>local content<br/>[claims.ts]"]
end

node_shell -->|"frames"| node_landing
node_shell -->|"frames"| node_investigate_page
node_investigate_page -->|"renders"| node_investigate_ui
node_investigate_ui -->|"submits image"| node_ocr_api
node_ocr_language -->|"OCR data"| node_ocr_api
node_ocr_api -->|"OCR text"| node_investigate_api
node_investigate_ui -->|"submits text or URL"| node_investigate_api
node_investigate_api -->|"runs"| node_orchestrator
node_orchestrator -->|"extracts"| node_claims
node_claims -->|"claims"| node_source_trace
node_source_trace -->|"origin context"| node_evidence
node_evidence -->|"grounding evidence"| node_assessment
node_search -->|"web results"| node_evidence
node_verification_search -->|"verification results"| node_evidence
node_signals -->|"heuristics"| node_assessment
node_assessment -->|"explained judgment"| node_orchestrator
node_orchestrator -->|"persists trace"| node_supabase
node_schema -->|"defines relations"| node_supabase
node_orchestrator -->|"returns contract"| node_trace_types
node_trace_types -->|"typed result"| node_live_results
node_investigate_api -->|"investigation chain"| node_live_results
node_investigate_ui -->|"renders"| node_live_results
node_learn_page -->|"renders"| node_learn_ui
node_lab_page -->|"renders"| node_lab_game
node_lab_claims -->|"challenge content"| node_lab_game
node_lab_game -->|"submits evaluation"| node_lab_api

click node_shell "https://github.com/youcef3939/trace/blob/main/app/layout.tsx"
click node_landing "https://github.com/youcef3939/trace/blob/main/app/page.tsx"
click node_investigate_page "https://github.com/youcef3939/trace/blob/main/app/investigate/page.tsx"
click node_investigate_ui "https://github.com/youcef3939/trace/blob/main/components/InvestigateExperience.tsx"
click node_live_results "https://github.com/youcef3939/trace/blob/main/components/InvestigationLive.tsx"
click node_investigate_api "https://github.com/youcef3939/trace/blob/main/app/api/investigate/route.ts"
click node_ocr_api "https://github.com/youcef3939/trace/blob/main/app/api/ocr/route.ts"
click node_ocr_language "https://github.com/youcef3939/trace/blob/main/eng.traineddata"
click node_orchestrator "https://github.com/youcef3939/trace/blob/main/lib/pipeline/runInvestigation.ts"
click node_claims "https://github.com/youcef3939/trace/blob/main/lib/pipeline/extractClaims.ts"
click node_source_trace "https://github.com/youcef3939/trace/blob/main/lib/pipeline/traceSource.ts"
click node_evidence "https://github.com/youcef3939/trace/blob/main/lib/pipeline/retrieveEvidence.ts"
click node_assessment "https://github.com/youcef3939/trace/blob/main/lib/pipeline/assess.ts"
click node_search "https://github.com/youcef3939/trace/blob/main/lib/tavily.ts"
click node_verification_search "https://github.com/youcef3939/trace/blob/main/lib/verifySearch.ts"
click node_signals "https://github.com/youcef3939/trace/blob/main/lib/manipulationSignals.ts"
click node_trace_types "https://github.com/youcef3939/trace/blob/main/types/trace.ts"
click node_supabase "https://github.com/youcef3939/trace/blob/main/lib/supabase.ts"
click node_schema "https://github.com/youcef3939/trace/blob/main/supabase/schema.sql"
click node_learn_page "https://github.com/youcef3939/trace/blob/main/app/learn/page.tsx"
click node_learn_ui "https://github.com/youcef3939/trace/blob/main/components/LearnContent.tsx"
click node_lab_page "https://github.com/youcef3939/trace/blob/main/app/lab/page.tsx"
click node_lab_game "https://github.com/youcef3939/trace/blob/main/components/lab/LabGame.tsx"
click node_lab_claims "https://github.com/youcef3939/trace/blob/main/lib/lab/claims.ts"
click node_lab_api "https://github.com/youcef3939/trace/blob/main/app/api/lab/evaluate/route.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_shell,node_landing,node_investigate_page,node_investigate_api,node_ocr_api,node_learn_page,node_lab_page,node_lab_api toneBlue
class node_investigate_ui,node_live_results,node_learn_ui,node_lab_game toneAmber
class node_orchestrator,node_claims,node_source_trace,node_evidence,node_assessment toneMint
class node_ocr_language,node_search,node_verification_search,node_signals,node_trace_types,node_supabase,node_schema,node_lab_claims toneRose
```

## Components

### Next.js App

Rendered routes and Node-backed API handlers.

- [App shell](https://github.com/youcef3939/trace/blob/main/app/layout.tsx): Next.js layout · `app/layout.tsx`
- [Landing page](https://github.com/youcef3939/trace/blob/main/app/page.tsx): Next.js page · `app/page.tsx`
- [Investigate route](https://github.com/youcef3939/trace/blob/main/app/investigate/page.tsx): Next.js page · `app/investigate/page.tsx`
- [Investigation API](https://github.com/youcef3939/trace/blob/main/app/api/investigate/route.ts): route handler · Operational boundary for submitted investigation material. · `app/api/investigate/route.ts`
- [OCR API](https://github.com/youcef3939/trace/blob/main/app/api/ocr/route.ts): route handler · Separates image transcription from claim assessment. · `app/api/ocr/route.ts`
- [Learning content](https://github.com/youcef3939/trace/blob/main/app/learn/page.tsx): Next.js page · `app/learn/page.tsx`
- [Lab route](https://github.com/youcef3939/trace/blob/main/app/lab/page.tsx): Next.js page · `app/lab/page.tsx`
- [Lab evaluation API](https://github.com/youcef3939/trace/blob/main/app/api/lab/evaluate/route.ts): route handler · `app/api/lab/evaluate/route.ts`

### User Experiences

Interactive investigation, learning, and practice interfaces.

- [Investigation experience](https://github.com/youcef3939/trace/blob/main/components/InvestigateExperience.tsx): React UI · Guided, inspectable rendering of the investigation chain. · `components/InvestigateExperience.tsx`
- [Live investigation view](https://github.com/youcef3939/trace/blob/main/components/InvestigationLive.tsx): React UI · `components/InvestigationLive.tsx`
- [Learning experience](https://github.com/youcef3939/trace/blob/main/components/LearnContent.tsx): React UI · `components/LearnContent.tsx`
- [Lab game](https://github.com/youcef3939/trace/blob/main/components/lab/LabGame.tsx): React UI · Practice challenges using local claims rather than real investigations. · `components/lab/LabGame.tsx`

### Investigation Pipeline

Replaceable stages for evidence-grounded assessment.

- [Investigation orchestrator](https://github.com/youcef3939/trace/blob/main/lib/pipeline/runInvestigation.ts): pipeline service · Composes the verification workflow into one API-level flow. · `lib/pipeline/runInvestigation.ts`
- [Claim extraction](https://github.com/youcef3939/trace/blob/main/lib/pipeline/extractClaims.ts): pipeline stage · `lib/pipeline/extractClaims.ts`
- [Source tracing](https://github.com/youcef3939/trace/blob/main/lib/pipeline/traceSource.ts): pipeline stage · `lib/pipeline/traceSource.ts`
- [Evidence retrieval](https://github.com/youcef3939/trace/blob/main/lib/pipeline/retrieveEvidence.ts): pipeline stage · `lib/pipeline/retrieveEvidence.ts`
- [Evidence assessment](https://github.com/youcef3939/trace/blob/main/lib/pipeline/assess.ts): pipeline stage · Produces an explained judgment constrained by retrieved evidence. · `lib/pipeline/assess.ts`

### Data & Integrations

Retrieval, persistence, and shared contracts.

- [OCR language data](https://github.com/youcef3939/trace/blob/main/eng.traineddata): OCR model data · `eng.traineddata`
- [Web retrieval adapters](https://github.com/youcef3939/trace/blob/main/lib/tavily.ts): retrieval integration · `lib/tavily.ts`
- [Verification search](https://github.com/youcef3939/trace/blob/main/lib/verifySearch.ts): retrieval helper · `lib/verifySearch.ts`
- [Manipulation signals](https://github.com/youcef3939/trace/blob/main/lib/manipulationSignals.ts): heuristics · Adds indicators such as misleading framing. · `lib/manipulationSignals.ts`
- [Trace contract](https://github.com/youcef3939/trace/blob/main/types/trace.ts): shared types · Contract between pipeline/API output and UI consumers. · `types/trace.ts`
- [Supabase persistence](https://github.com/youcef3939/trace/blob/main/lib/supabase.ts): database integration · Stores durable investigation, source, and evidence history. · `lib/supabase.ts`
- [Trace schema](https://github.com/youcef3939/trace/blob/main/supabase/schema.sql): SQL schema · Models investigation, source, and evidence relationships. · `supabase/schema.sql`
- [Lab claims](https://github.com/youcef3939/trace/blob/main/lib/lab/claims.ts): local content · `lib/lab/claims.ts`

## Connections

- App shell → Landing page: frames
- App shell → Investigate route: frames
- Investigate route → Investigation experience: renders
- Investigation experience → OCR API: submits image
- OCR language data → OCR API: OCR data
- OCR API → Investigation API: OCR text
- Investigation experience → Investigation API: submits text or URL
- Investigation API → Investigation orchestrator: runs
- Investigation orchestrator → Claim extraction: extracts
- Claim extraction → Source tracing: claims
- Source tracing → Evidence retrieval: origin context
- Evidence retrieval → Evidence assessment: grounding evidence
- Web retrieval adapters → Evidence retrieval: web results
- Verification search → Evidence retrieval: verification results
- Manipulation signals → Evidence assessment: heuristics
- Evidence assessment → Investigation orchestrator: explained judgment
- Investigation orchestrator → Supabase persistence: persists trace
- Trace schema → Supabase persistence: defines relations
- Investigation orchestrator → Trace contract: returns contract
- Trace contract → Live investigation view: typed result
- Investigation API → Live investigation view: investigation chain
- Investigation experience → Live investigation view: renders
- Learning content → Learning experience: renders
- Lab route → Lab game: renders
- Lab claims → Lab game: challenge content
- Lab game → Lab evaluation API: submits evaluation
