# clr4takeoff/fintune architecture

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

- Interactive diagram: https://gitdiagram.com/clr4takeoff/fintune
- Repository: https://github.com/clr4takeoff/fintune

## Overview

1. **Repository type and runtime boundary**
   - This is a **single-page React application** built with **TypeScript + Vite** and deployed as a static front end.
   - There is **no custom backend server** in the repo; persistence is handled either through **Firebase Firestore** or a **browser localStorage fallback** when Firebase is not configured.

2. **Primary product flow**
   - The app is an **experiment platform** for a Prolific study on AI reliance and confidence calibration.
   - The user journey is route-driven and follows the research protocol: **register → login → screening → practice → experiment → result → result report → post survey → completion**.
   - These steps are implemented as separate page components under `src/pages/`, which makes the route sequence a central architectural boundary.

3. **Application shell and navigation**
   - `src/main.tsx` boots the React app, and `src/App.tsx` likely owns the route composition and top-level navigation logic.
   - `src/layouts/ExperimentLayout.tsx` provides the shared frame for the trial experience, separating experiment UI chrome from the page-specific content.
   - The route structure is important because several pages are not independent screens but checkpoints in a controlled experimental flow.

4. **State management layer**
   - The app uses **Zustand** stores in `src/stores/` for client-side experiment state.
   - `participantStore` appears to hold participant identity, condition assignment, validity, and screening metadata.
   - `experimentStore` likely holds trial progression, current responses, timing, and report-related state.
   - This creates a clear split between **participant lifecycle state** and **in-experiment session state**.

5. **Participant lifecycle and validation**
   - `src/services/participantService.ts` and `src/services/participantDB.ts` manage participant creation, lookup, persistence, and likely fallback behavior.
   - The participant record includes Prolific-facing identity, condition assignment, literacy score, and completion timestamps.
   - The `screening` page acts as a gate before practice/experiment, so participant validity is part of the architectural control flow, not just data storage.

6. **Trial generation and experimental content**
   - `src/services/trialGenerationService.ts` is the content-generation boundary for the experiment.
   - The app consumes a static dataset from `public/data/sampling_data.json`, which contains the company finance records used to build trials.
   - This service is responsible for producing the trial sequence, likely splitting **practice trials** and **main experiment trials**, and ensuring condition-dependent presentation.

7. **Condition-specific behavior**
   - The experiment has three conditions:
     - **A**: AI recommendation only
     - **B**: AI recommendation + confidence display
     - **C**: AI recommendation + confidence + interactive What-If slider
   - This condition switch is a major architectural axis because it changes what the trial UI renders and what events are recorded.
   - In practice, the trial pages need to branch on condition to decide whether confidence visualization and slider interaction are available.

8. **AI prediction and What-If simulation**
   - The README indicates the AI logic is an **inline logistic regression model on the frontend**.
   - That model is used for real-time prediction updates in the What-If flow, so the app does not depend on a server round-trip for AI feedback.
   - This is a key boundary: the “AI” is a client-side simulation engine, not an external inference service.

9. **Persistence model**
   - When Firebase is available, data goes to **Firestore** using the schema described in the README:
     - `participants/{participant_id}`
     - `participants/{participant_id}/trials/{trial_id}`
     - `participants/{participant_id}/trials/{trial_id}/slider_logs/{log_id}`
     - `participants/{participant_id}/post_survey/{question_id}`
   - When Firebase is unavailable, the same logical data is kept in **localStorage**, which means the app is designed to run in a fully offline/developer mode as well.
   - `firebase.json`, `firestore.rules`, and `firestore.indexes.json` show that Firebase hosting and Firestore access control are part of the deployment architecture.

10. **Experiment telemetry and event granularity**
   - Trial response data is recorded at multiple levels: initial judgment, final judgment, confidence, switch status, response time, correctness, and reliance classification.
   - Condition C adds fine-grained **slider logs** with before/after values and timestamps, which are stored separately from the trial summary.
   - This separation suggests a reporting pipeline where trial outcomes and interaction traces are distinct analytical datasets.

11. **Reporting and feedback outputs**
   - `ResultPage` and `ResultReportPage` split immediate per-trial feedback from the aggregated experiment summary.
   - This indicates two different presentation layers: one for **trial-level outcome feedback** and another for **study-level calibration/reporting**.
   - The `post-survey` page collects qualitative and Likert-scale data after the main task, and `complete` marks the terminal state of the study flow.

12. **Models and shared types**
   - `src/models/Participant.ts` contains the core domain types used across services, stores, and pages.
   - The repo likely centralizes participant/trial schema here so Firestore documents, UI state, and generated trial objects stay aligned.
   - This model layer is small but important because it defines the contract between experiment content generation, UI state, and persistence.

13. **Operational/tooling boundary**
   - The project is configured for a modern frontend toolchain: **Vite**, **React 19**, **TypeScript**, **ESLint**, and npm-based builds.
   - `makefile`, `package-lock.json`, and Firebase config files indicate the repo is intended to be reproducibly built and deployed, but the architecture remains client-centric.

14. **How to draw the architecture**
   - The cleanest diagram is: **browser SPA** at the center, with:
     - routed **pages** on the UI side,
     - **Zustand stores** as shared in-memory state,
     - **services** for participant persistence, trial generation, and AI simulation,
     - **static dataset** input from `public/data/sampling_data.json`,
     - and **Firestore/localStorage** as the persistence layer.
   - The most important cross-cutting control variable is the **experiment condition**, because it determines the UI, the interactions collected, and the data written.

Main entry points: [`src/App.tsx`](https://github.com/clr4takeoff/fintune/blob/main/src/App.tsx) (App), [`src/stores/participantStore.ts`](https://github.com/clr4takeoff/fintune/blob/main/src/stores/participantStore.ts) (Participant store), [`src/pages/ExperimentPage.tsx`](https://github.com/clr4takeoff/fintune/blob/main/src/pages/ExperimentPage.tsx) (Experiment), [`src/services/trialGenerationService.ts`](https://github.com/clr4takeoff/fintune/blob/main/src/services/trialGenerationService.ts) (Trial generation)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ui["React app"]
  node_main["main.tsx<br/>boot<br/>[main.tsx]"]
  node_app["App<br/>router<br/>[App.tsx]"]
  node_layout["Experiment layout<br/>shell"]
  node_register["Register<br/>page<br/>[RegisterPage.tsx]"]
  node_login["Login<br/>page<br/>[LoginPage.tsx]"]
  node_condition["Condition select<br/>page"]
  node_screening["Screening<br/>page<br/>[ScreeningPage.tsx]"]
  node_practice["Practice<br/>page<br/>[PracticePage.tsx]"]
  node_experiment["Experiment<br/>page<br/>[ExperimentPage.tsx]"]
  node_result["Result<br/>page<br/>[ResultPage.tsx]"]
  node_report["Report<br/>page"]
  node_survey["Post survey<br/>page<br/>[PostSurveyPage.tsx]"]
  node_complete["Complete<br/>page<br/>[CompletePage.tsx]"]
end

subgraph group_state["Client state"]
  node_participant[("Participant store<br/>zustand store")]
  node_experiment_store[("Experiment store<br/>zustand store<br/>[experimentStore.ts]")]
end

subgraph group_services["Services"]
  node_trial_gen{{"Trial generation<br/>experiment logic"}}
  node_participant_service{{"Participant service<br/>persistence logic"}}
  node_participant_db[("Participant DB<br/>storage adapter<br/>[participantDB.ts]")]
end

subgraph group_data["Data"]
  node_participant_model["Participant model<br/>domain types<br/>[Participant.ts]"]
  node_sampling["Sampling data<br/>static dataset<br/>[sampling_data.json]"]
end

subgraph group_infra["Deployment"]
  node_firebase{{"Firebase<br/>firestore config<br/>[firebase.ts]"}}
end

node_main -->|"mounts"| node_app
node_app -->|"routes"| node_register
node_app -->|"routes"| node_login
node_app -->|"routes"| node_condition
node_app -->|"routes"| node_screening
node_app -->|"routes"| node_practice
node_app -->|"routes"| node_experiment
node_app -->|"routes"| node_result
node_app -->|"routes"| node_report
node_app -->|"routes"| node_survey
node_app -->|"routes"| node_complete
node_experiment -->|"uses"| node_layout
node_register -->|"updates"| node_participant
node_login -->|"loads"| node_participant
node_condition -->|"assigns"| node_participant
node_screening -->|"validates"| node_participant
node_practice -->|"advances"| node_experiment_store
node_experiment -->|"records"| node_experiment_store
node_result -->|"reads"| node_experiment_store
node_report -->|"reads"| node_experiment_store
node_survey -->|"saves"| node_participant_service
node_participant_service -->|"uses"| node_participant_db
node_participant_db -->|"prefers"| node_firebase
node_participant_db -->|"serializes"| node_participant
node_trial_gen -->|"consumes"| node_sampling
node_practice -->|"builds"| node_trial_gen
node_experiment -->|"builds"| node_trial_gen
node_trial_gen -->|"depends on"| node_participant

click node_main "https://github.com/clr4takeoff/fintune/blob/main/src/main.tsx"
click node_app "https://github.com/clr4takeoff/fintune/blob/main/src/App.tsx"
click node_layout "https://github.com/clr4takeoff/fintune/blob/main/src/layouts/ExperimentLayout.tsx"
click node_register "https://github.com/clr4takeoff/fintune/blob/main/src/pages/RegisterPage.tsx"
click node_login "https://github.com/clr4takeoff/fintune/blob/main/src/pages/LoginPage.tsx"
click node_condition "https://github.com/clr4takeoff/fintune/blob/main/src/pages/ConditionSelectPage.tsx"
click node_screening "https://github.com/clr4takeoff/fintune/blob/main/src/pages/ScreeningPage.tsx"
click node_practice "https://github.com/clr4takeoff/fintune/blob/main/src/pages/PracticePage.tsx"
click node_experiment "https://github.com/clr4takeoff/fintune/blob/main/src/pages/ExperimentPage.tsx"
click node_result "https://github.com/clr4takeoff/fintune/blob/main/src/pages/ResultPage.tsx"
click node_report "https://github.com/clr4takeoff/fintune/blob/main/src/pages/ResultReportPage.tsx"
click node_survey "https://github.com/clr4takeoff/fintune/blob/main/src/pages/PostSurveyPage.tsx"
click node_complete "https://github.com/clr4takeoff/fintune/blob/main/src/pages/CompletePage.tsx"
click node_participant "https://github.com/clr4takeoff/fintune/blob/main/src/stores/participantStore.ts"
click node_experiment_store "https://github.com/clr4takeoff/fintune/blob/main/src/stores/experimentStore.ts"
click node_participant_model "https://github.com/clr4takeoff/fintune/blob/main/src/models/Participant.ts"
click node_trial_gen "https://github.com/clr4takeoff/fintune/blob/main/src/services/trialGenerationService.ts"
click node_participant_service "https://github.com/clr4takeoff/fintune/blob/main/src/services/participantService.ts"
click node_participant_db "https://github.com/clr4takeoff/fintune/blob/main/src/services/participantDB.ts"
click node_firebase "https://github.com/clr4takeoff/fintune/blob/main/src/services/firebase.ts"
click node_sampling "https://github.com/clr4takeoff/fintune/blob/main/public/data/sampling_data.json"

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_main,node_app,node_layout,node_register,node_login,node_condition,node_screening,node_practice,node_experiment,node_result,node_report,node_survey,node_complete toneBlue
class node_participant,node_experiment_store toneAmber
class node_trial_gen,node_participant_service,node_participant_db toneMint
class node_participant_model,node_sampling toneRose
class node_firebase toneIndigo
```

## Components

### React app

Browser SPA, routed pages, and shared layout.

- [main.tsx](https://github.com/clr4takeoff/fintune/blob/main/src/main.tsx): boot · React entrypoint that mounts the app. · `src/main.tsx`
- [App](https://github.com/clr4takeoff/fintune/blob/main/src/App.tsx): router · Top-level route composition and navigation flow. · `src/App.tsx`
- [Experiment layout](https://github.com/clr4takeoff/fintune/blob/main/src/layouts/ExperimentLayout.tsx): shell · Shared experiment frame around trial screens. · `src/layouts/ExperimentLayout.tsx`
- [Register](https://github.com/clr4takeoff/fintune/blob/main/src/pages/RegisterPage.tsx): page · Participant registration step. · `src/pages/RegisterPage.tsx`
- [Login](https://github.com/clr4takeoff/fintune/blob/main/src/pages/LoginPage.tsx): page · Participant login / lookup step. · `src/pages/LoginPage.tsx`
- [Condition select](https://github.com/clr4takeoff/fintune/blob/main/src/pages/ConditionSelectPage.tsx): page · Assigns or confirms experiment condition. · `src/pages/ConditionSelectPage.tsx`
- [Screening](https://github.com/clr4takeoff/fintune/blob/main/src/pages/ScreeningPage.tsx): page · Validity gate before the task flow. · `src/pages/ScreeningPage.tsx`
- [Practice](https://github.com/clr4takeoff/fintune/blob/main/src/pages/PracticePage.tsx): page · Practice trials before the main experiment. · `src/pages/PracticePage.tsx`
- [Experiment](https://github.com/clr4takeoff/fintune/blob/main/src/pages/ExperimentPage.tsx): page · Main trial task and response capture. · `src/pages/ExperimentPage.tsx`
- [Result](https://github.com/clr4takeoff/fintune/blob/main/src/pages/ResultPage.tsx): page · Immediate per-trial feedback screen. · `src/pages/ResultPage.tsx`
- [Report](https://github.com/clr4takeoff/fintune/blob/main/src/pages/ResultReportPage.tsx): page · Aggregated calibration and study summary. · `src/pages/ResultReportPage.tsx`
- [Post survey](https://github.com/clr4takeoff/fintune/blob/main/src/pages/PostSurveyPage.tsx): page · Likert and free-response questionnaire. · `src/pages/PostSurveyPage.tsx`
- [Complete](https://github.com/clr4takeoff/fintune/blob/main/src/pages/CompletePage.tsx): page · Terminal completion screen. · `src/pages/CompletePage.tsx`

### Client state

In-memory experiment and participant state.

- [Participant store](https://github.com/clr4takeoff/fintune/blob/main/src/stores/participantStore.ts): zustand store · Participant identity, condition, validity, and screening metadata. · `src/stores/participantStore.ts`
- [Experiment store](https://github.com/clr4takeoff/fintune/blob/main/src/stores/experimentStore.ts): zustand store · Trial progression, responses, timing, and report state. · `src/stores/experimentStore.ts`

### Services

Experiment logic, persistence orchestration, and AI simulation.

- [Trial generation](https://github.com/clr4takeoff/fintune/blob/main/src/services/trialGenerationService.ts): experiment logic · Builds practice and main trials from study data and condition. · `src/services/trialGenerationService.ts`
- [Participant service](https://github.com/clr4takeoff/fintune/blob/main/src/services/participantService.ts): persistence logic · Creates, loads, validates, and saves participant records. · `src/services/participantService.ts`
- [Participant DB](https://github.com/clr4takeoff/fintune/blob/main/src/services/participantDB.ts): storage adapter · Persistence boundary with Firestore or localStorage fallback. · `src/services/participantDB.ts`

### Data

Static input and domain models.

- [Participant model](https://github.com/clr4takeoff/fintune/blob/main/src/models/Participant.ts): domain types · Shared participant and trial contracts. · `src/models/Participant.ts`
- [Sampling data](https://github.com/clr4takeoff/fintune/blob/main/public/data/sampling_data.json): static dataset · Company finance records used to generate trials. · `public/data/sampling_data.json`

### Deployment

Firebase and build/runtime configuration.

- [Firebase](https://github.com/clr4takeoff/fintune/blob/main/src/services/firebase.ts): firestore config · Firestore initialization and environment integration. · `src/services/firebase.ts`

## Connections

- main.tsx → App: mounts · Bootstraps the routed React application.
- App → Register: routes · Defines the registration step.
- App → Login: routes · Defines the login step.
- App → Condition select: routes · Defines condition selection.
- App → Screening: routes · Defines the screening gate.
- App → Practice: routes · Defines practice flow.
- App → Experiment: routes · Defines main trial flow.
- App → Result: routes · Defines trial feedback.
- App → Report: routes · Defines summary reporting.
- App → Post survey: routes · Defines post-study survey.
- App → Complete: routes · Defines terminal completion state.
- Experiment → Experiment layout: uses · Renders within the shared experiment shell.
- Register → Participant store: updates · Creates participant state.
- Login → Participant store: loads · Restores participant state.
- Condition select → Participant store: assigns · Sets experimental condition.
- Screening → Participant store: validates · Sets eligibility and screening metadata.
- Practice → Experiment store: advances · Seeds practice progression and responses.
- Experiment → Experiment store: records · Stores trial state and telemetry.
- Result → Experiment store: reads · Consumes trial outcome state.
- Report → Experiment store: reads · Aggregates experiment outcomes.
- Post survey → Participant service: saves · Persists post-survey and completion data.
- Participant service → Participant DB: uses · Delegates storage operations.
- Participant DB → Firebase: prefers · Uses Firestore when configured.
- Participant DB → Participant store: serializes · Maps domain records to stored participant data.
- Trial generation → Sampling data: consumes · Builds trials from the static dataset.
- Practice → Trial generation: builds · Generates practice trials.
- Experiment → Trial generation: builds · Generates main experiment trials.
- Trial generation → Participant store: depends on · Uses participant condition and validity.
