# tridibbanik17/stock_agent architecture

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

- Interactive diagram: https://gitdiagram.com/tridibbanik17/stock_agent
- Repository: https://github.com/tridibbanik17/stock_agent

## Overview

#### Purpose and Entry Points
Stock Agent is a privacy-first Chrome Manifest V3 watchlist grader. The extension keeps holdings and Gemini keys locally, requests ticker-only snapshots from a FastAPI backend, and optionally subscribes users to scheduled email reports. Primary entry points are `extension/manifest.json`, `extension/popup/popup.js`, `backend/app/main.py`, and the scheduled dispatcher `backend/worker/cron_dispatch.py`.

#### Extension Client and Privacy Boundary
The browser extension is the user-facing runtime: popup UI manages watchlists, local portfolio values, themes, subscription settings, and refreshes. Local storage is the hard privacy boundary for share counts, buy prices, and optional Gemini key; backend calls send only tickers and email scheduling data. `extension/background.js` supports MV3 background behavior, while `extension/lib/api.js` and `extension/lib/storage.js` separate network and persistence concerns.
Paths: `extension/popup/popup.js`, `extension/lib/api.js`, `extension/lib/storage.js`.

#### Snapshot API and Request Controls
FastAPI exposes subscription, quote snapshot, unsubscribe, and protected internal dispatch routes. Routing is centralized separately from startup/configuration, with Pydantic schemas enforcing request/response contracts. Public endpoints are bounded by body validation and per-IP rate limiting; the internal dispatch route authenticates with a shared dispatch secret.
Paths: `backend/app/main.py`, `backend/app/api/routes.py`, `backend/app/api/abuse.py`, `backend/app/models/schemas.py`.

#### Market Data and Deterministic Grading
The snapshot pipeline obtains live market/fundamental data and news, then grades each ticker with explainable rules rather than AI. The grading service applies sector-aware D/E, PEG, ROE, 200-SMA, RSI, and news-risk logic to produce STRONG BUY, HOLD, or AVOID outcomes. Market-provider failures/fallback behavior belongs at the market-data boundary rather than in extension UI logic.
Paths: `backend/app/services/market_data.py`, `backend/app/services/grading.py`, `backend/app/services/news.py`.

#### Subscription Persistence and Email Delivery
Subscription requests upsert email, tickers, schedule, timezone, and delivery state in Supabase PostgreSQL. Due reports are assembled from fresh quote/grade data, sent through Resend, and recorded to prevent duplicate or excessive sends. Schema plus incremental migrations define operational state including unsubscribe tokens, last-send timestamps, delivery logs, grade-change email support, and daily caps.
Paths: `backend/app/services/supabase_client.py`, `backend/app/services/email_report.py`, `backend/database_schema.sql`, `backend/migrations/004_add_delivery_logs.sql`.

#### Scheduled Dispatch Control Plane
AWS EventBridge and Lambda invoke the hosted protected dispatch endpoint every five minutes; actual database access and email sending remain in the backend. A local worker provides equivalent cron-trigger behavior for development or fallback. The deployment target is Heroku using the backend Docker image/Procfile runtime.
Paths: `backend/app/services/dispatch.py`, `backend/worker/cron_dispatch.py`, `backend/Dockerfile`, `Procfile`.

#### External Dependencies and Delivery
Material external services are Supabase/PostgreSQL for subscriptions, yfinance for market data, Resend for email, Google Gemini directly from the browser for optional explanations, and AWS EventBridge/Lambda for triggering. GitHub Actions runs tests and automates extension version bumps; the disabled cron workflow remains a reference/fallback.
Paths: `.github/workflows/test.yml`, `.github/workflows/bump-extension-version.yml`, `.github/workflows/cron-dispatch.yml`.

Main entry points: [`backend/app/api/routes.py`](https://github.com/tridibbanik17/stock_agent/blob/main/backend/app/api/routes.py) (API Routes), [`backend/app/services/email_report.py`](https://github.com/tridibbanik17/stock_agent/blob/main/backend/app/services/email_report.py) (Email Report Service), [`backend/app/services/market_data.py`](https://github.com/tridibbanik17/stock_agent/blob/main/backend/app/services/market_data.py) (Market Data Service), [`backend/app/services/dispatch.py`](https://github.com/tridibbanik17/stock_agent/blob/main/backend/app/services/dispatch.py) (Dispatch Service)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_extension["Chrome Extension"]
  node_popup["Popup UI<br/>extension UI<br/>[popup.js]"]
  node_storage[("Local Storage<br/>privacy boundary<br/>[storage.js]")]
  node_extension_api["Backend API Client<br/>extension network<br/>[api.js]"]
  node_background["MV3 Background<br/>extension runtime<br/>[background.js]"]
end

subgraph group_backend["FastAPI Backend"]
  node_main["FastAPI App<br/>application entry<br/>[main.py]"]
  node_routes["API Routes<br/>HTTP boundary<br/>[routes.py]"]
  node_abuse["Request Controls<br/>rate limiting<br/>[abuse.py]"]
  node_schemas["Pydantic Schemas<br/>API contracts<br/>[schemas.py]"]
  node_market_data["Market Data Service<br/>provider boundary<br/>[market_data.py]"]
  node_news["News Service<br/>news retrieval<br/>[news.py]"]
  node_grading["Deterministic Grading<br/>grading rules<br/>[grading.py]"]
end

subgraph group_delivery["Report Delivery"]
  node_supabase_client["Supabase Client<br/>persistence adapter<br/>[supabase_client.py]"]
  node_dispatch["Dispatch Service<br/>schedule processor<br/>[dispatch.py]"]
  node_email_report["Email Report Service<br/>report delivery<br/>[email_report.py]"]
  node_cron_worker["Local Cron Worker<br/>fallback scheduler<br/>[cron_dispatch.py]"]
end

subgraph group_external["External Services"]
  node_gemini{{"Google Gemini<br/>optional browser AI"}}
  node_yfinance{{"yfinance<br/>market provider"}}
  node_postgres[("Supabase PostgreSQL<br/>subscription store")]
  node_resend{{"Resend<br/>email provider"}}
  node_scheduler["AWS EventBridge + Lambda<br/>hosted scheduler"]
end

node_popup -->|"reads and writes"| node_storage
node_popup -->|"requests snapshots"| node_extension_api
node_popup -.->|"optional explanations"| node_gemini
node_background -->|"uses"| node_storage
node_extension_api -->|"tickers and subscription data"| node_routes
node_main -->|"mounts"| node_routes
node_routes -->|"public request controls"| node_abuse
node_routes -->|"validates contracts"| node_schemas
node_routes -->|"snapshot tickers"| node_market_data
node_market_data -->|"fetches market data"| node_yfinance
node_routes -->|"retrieves news"| node_news
node_market_data -->|"fundamentals and prices"| node_grading
node_news -->|"news risk"| node_grading
node_routes -->|"upserts subscriptions"| node_supabase_client
node_supabase_client -->|"persists state"| node_postgres
node_scheduler -->|"protected dispatch call"| node_routes
node_cron_worker -->|"triggers"| node_dispatch
node_routes -->|"authenticated internal dispatch"| node_dispatch
node_dispatch -->|"loads due subscriptions"| node_supabase_client
node_dispatch -->|"creates due reports"| node_email_report
node_email_report -->|"fresh quote data"| node_market_data
node_email_report -->|"fresh grades"| node_grading
node_email_report -->|"sends email"| node_resend
node_email_report -->|"records delivery"| node_supabase_client

click node_popup "https://github.com/tridibbanik17/stock_agent/blob/main/extension/popup/popup.js"
click node_storage "https://github.com/tridibbanik17/stock_agent/blob/main/extension/lib/storage.js"
click node_extension_api "https://github.com/tridibbanik17/stock_agent/blob/main/extension/lib/api.js"
click node_background "https://github.com/tridibbanik17/stock_agent/blob/main/extension/background.js"
click node_main "https://github.com/tridibbanik17/stock_agent/blob/main/backend/app/main.py"
click node_routes "https://github.com/tridibbanik17/stock_agent/blob/main/backend/app/api/routes.py"
click node_abuse "https://github.com/tridibbanik17/stock_agent/blob/main/backend/app/api/abuse.py"
click node_schemas "https://github.com/tridibbanik17/stock_agent/blob/main/backend/app/models/schemas.py"
click node_market_data "https://github.com/tridibbanik17/stock_agent/blob/main/backend/app/services/market_data.py"
click node_news "https://github.com/tridibbanik17/stock_agent/blob/main/backend/app/services/news.py"
click node_grading "https://github.com/tridibbanik17/stock_agent/blob/main/backend/app/services/grading.py"
click node_supabase_client "https://github.com/tridibbanik17/stock_agent/blob/main/backend/app/services/supabase_client.py"
click node_postgres "https://github.com/tridibbanik17/stock_agent/blob/main/backend/database_schema.sql"
click node_dispatch "https://github.com/tridibbanik17/stock_agent/blob/main/backend/app/services/dispatch.py"
click node_email_report "https://github.com/tridibbanik17/stock_agent/blob/main/backend/app/services/email_report.py"
click node_cron_worker "https://github.com/tridibbanik17/stock_agent/blob/main/backend/worker/cron_dispatch.py"

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_popup,node_storage,node_extension_api,node_background toneBlue
class node_main,node_routes,node_abuse,node_schemas,node_market_data,node_news,node_grading toneAmber
class node_supabase_client,node_dispatch,node_email_report,node_cron_worker toneMint
class node_gemini,node_yfinance,node_postgres,node_resend,node_scheduler toneRose
```

## Components

### Chrome Extension

Privacy-first browser runtime.

- [Popup UI](https://github.com/tridibbanik17/stock_agent/blob/main/extension/popup/popup.js): extension UI · Manages watchlists, settings, and refreshes. · `extension/popup/popup.js`
- [Local Storage](https://github.com/tridibbanik17/stock_agent/blob/main/extension/lib/storage.js): privacy boundary · Retains holdings, prices, and optional Gemini key locally. · `extension/lib/storage.js`
- [Backend API Client](https://github.com/tridibbanik17/stock_agent/blob/main/extension/lib/api.js): extension network · Sends ticker-only snapshots and subscription data. · `extension/lib/api.js`
- [MV3 Background](https://github.com/tridibbanik17/stock_agent/blob/main/extension/background.js): extension runtime · `extension/background.js`

### FastAPI Backend

Hosted snapshot and subscription service.

- [FastAPI App](https://github.com/tridibbanik17/stock_agent/blob/main/backend/app/main.py): application entry · Initializes hosted API configuration and startup. · `backend/app/main.py`
- [API Routes](https://github.com/tridibbanik17/stock_agent/blob/main/backend/app/api/routes.py): HTTP boundary · Exposes snapshots, subscriptions, unsubscribe, and internal dispatch. · `backend/app/api/routes.py`
- [Request Controls](https://github.com/tridibbanik17/stock_agent/blob/main/backend/app/api/abuse.py): rate limiting · Applies public request validation and per-IP limits. · `backend/app/api/abuse.py`
- [Pydantic Schemas](https://github.com/tridibbanik17/stock_agent/blob/main/backend/app/models/schemas.py): API contracts · `backend/app/models/schemas.py`
- [Market Data Service](https://github.com/tridibbanik17/stock_agent/blob/main/backend/app/services/market_data.py): provider boundary · Fetches live market and fundamental data with fallbacks. · `backend/app/services/market_data.py`
- [News Service](https://github.com/tridibbanik17/stock_agent/blob/main/backend/app/services/news.py): news retrieval · `backend/app/services/news.py`
- [Deterministic Grading](https://github.com/tridibbanik17/stock_agent/blob/main/backend/app/services/grading.py): grading rules · Produces explainable STRONG BUY, HOLD, or AVOID grades. · `backend/app/services/grading.py`

### Report Delivery

Scheduled dispatch and email reporting.

- [Supabase Client](https://github.com/tridibbanik17/stock_agent/blob/main/backend/app/services/supabase_client.py): persistence adapter · Upserts subscriptions and tracks delivery state. · `backend/app/services/supabase_client.py`
- [Dispatch Service](https://github.com/tridibbanik17/stock_agent/blob/main/backend/app/services/dispatch.py): schedule processor · Finds due reports and coordinates report delivery. · `backend/app/services/dispatch.py`
- [Email Report Service](https://github.com/tridibbanik17/stock_agent/blob/main/backend/app/services/email_report.py): report delivery · Builds fresh graded reports and records sends. · `backend/app/services/email_report.py`
- [Local Cron Worker](https://github.com/tridibbanik17/stock_agent/blob/main/backend/worker/cron_dispatch.py): fallback scheduler · Provides equivalent local or fallback dispatch triggering. · `backend/worker/cron_dispatch.py`

### External Services

- **Google Gemini**: optional browser AI · Receives the optional local user key directly from the browser.
- **yfinance**: market provider
- [Supabase PostgreSQL](https://github.com/tridibbanik17/stock_agent/blob/main/backend/database_schema.sql): subscription store · Stores schedules, tokens, send state, logs, caps, and grade changes. · `backend/database_schema.sql`
- **Resend**: email provider
- **AWS EventBridge + Lambda**: hosted scheduler · Invokes protected dispatch every five minutes.

## Connections

- Popup UI → Local Storage: reads and writes
- Popup UI → Backend API Client: requests snapshots
- Popup UI → Google Gemini: optional explanations
- MV3 Background → Local Storage: uses
- Backend API Client → API Routes: tickers and subscription data
- FastAPI App → API Routes: mounts
- API Routes → Request Controls: public request controls
- API Routes → Pydantic Schemas: validates contracts
- API Routes → Market Data Service: snapshot tickers
- Market Data Service → yfinance: fetches market data
- API Routes → News Service: retrieves news
- Market Data Service → Deterministic Grading: fundamentals and prices
- News Service → Deterministic Grading: news risk
- API Routes → Supabase Client: upserts subscriptions
- Supabase Client → Supabase PostgreSQL: persists state
- AWS EventBridge + Lambda → API Routes: protected dispatch call
- Local Cron Worker → Dispatch Service: triggers
- API Routes → Dispatch Service: authenticated internal dispatch
- Dispatch Service → Supabase Client: loads due subscriptions
- Dispatch Service → Email Report Service: creates due reports
- Email Report Service → Market Data Service: fresh quote data
- Email Report Service → Deterministic Grading: fresh grades
- Email Report Service → Resend: sends email
- Email Report Service → Supabase Client: records delivery
