# ymoola/llm4cp-mod-ref architecture

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

- Interactive diagram: https://gitdiagram.com/ymoola/llm4cp-mod-ref
- Repository: https://github.com/ymoola/llm4cp-mod-ref

## Overview

1. This repository has two major subsystems:
   - `src/cpmod_web`: a product-oriented web app for managing projects, change requests, runs, and model packages.
   - `src/mod-ref-benchmark`: an offline benchmark / agent pipeline for model-refinement experiments on structured problem folders.

2. The web app is split into a Python backend and a Next.js frontend. The backend is the system of record for projects, model packages, change requests, and run records; the frontend is the interactive UI that calls the backend APIs and also talks directly to Supabase for authentication.

3. Backend entrypoint and API boundary:
   - `backend/main.py` wires the FastAPI application.
   - `backend/routers/*` exposes domain routes for projects, runs, dashboard, settings, model packages, and change requests.
   - `backend/middleware/auth.py` sits at the request boundary to enforce auth behavior.
   This creates a clear separation between HTTP transport and the underlying domain/services.

4. Backend domain logic is organized by service layer:
   - `model_package_service.py`, `resource_service.py`, `storage_service.py`, `credential_service.py`, `dependency_policy.py`, `diff_service.py`, and `llm_service.py` encapsulate business logic.
   - `workflow/*` contains the orchestration logic for automated change-request processing, with graph/state/schema/prompt definitions.
   - `services/execution/*` abstracts execution environments through a common base, with local and E2B implementations selected via a factory.
   This means “what to do” is separated from “how/where it runs.”

5. Persistence is Supabase-backed. The backend `db/supabase_client.py` and `db/queries.py` indicate a query layer over Supabase rather than a local database. The `supabase/migrations/*.sql` files define the schema evolution for core entities such as projects, change requests, run metadata, credentials, model selections, and validation artifacts.

6. The frontend is a Next.js app (`frontend/app/*`) using the app router structure. It has auth pages, dashboard, project views, model-package pages, run pages, and settings. UI components like `run-launcher`, `run-viewer`, `diff-view`, `cr-form`, and `model-upload` map closely to backend concepts, so the frontend is mostly a presentation and interaction layer rather than a separate domain engine.

7. Frontend infrastructure details matter architecturally:
   - `frontend/lib/api.ts` centralizes backend API calls.
   - `frontend/lib/supabase.ts` handles Supabase client/auth integration.
   - `frontend/middleware.ts` and `components/auth-gate.tsx` enforce authenticated routing on the client side.
   This means the browser is not just rendering data; it is also participating in auth/session management.

8. The backend’s workflow subsystem is the automation core for the web product. It likely coordinates:
   - parsing model/reference artifacts,
   - generating or revising changes,
   - executing candidate models,
   - validating outputs,
   - serializing run state and logs.
   The `workflow/service.py`, `graph.py`, `state.py`, `schemas.py`, and `prompts.py` files indicate a graph-based or state-machine-like orchestration boundary around LLM-driven steps.

9. Execution is explicitly abstracted as an environment boundary:
   - `services/execution/local.py` for local runs,
   - `services/execution/e2b.py` for remote/sandboxed runs,
   - `services/execution/harness.py` for the common contract.
   This is important for the architecture diagram because model execution is not assumed to happen inside the web server process.

10. `src/mod-ref-benchmark` is a separate experimental pipeline, not part of the web product runtime. It contains:
   - benchmark problem corpora under `problems/`,
   - standalone agent scripts for parser/planner/modifier/executor/validator,
   - a LangGraph-based orchestrator under `langgraph_workflow/`,
   - baseline and ablation experiment runners.
   Its primary boundary is filesystem-based problem folders rather than an HTTP API.

11. The benchmark data model is folder-centric. Each `problemN` directory contains a base problem description and reference model plus multiple CR subfolders with CR-specific descriptions, input data, unit tests, and generated outputs/logs. That structure is the main input/output contract for the benchmark agents.

12. The benchmark workflow is a staged LLM pipeline:
   - Parser aligns natural language to code structure.
   - Planner produces a structured edit plan using CR context and parser output.
   - Modifier rewrites the target model.
   - Executor runs the candidate model to capture behavior/errors.
   - Validator reviews generated vs. reference behavior and can feed iterative repair loops.
   The README makes clear that this pipeline is designed to end in a unit test run when validation passes.

13. `llm_client.py`, `llm_prompts.py`, `llm_schemas.py`, and `model_presets.py` in the benchmark package are the shared LLM integration layer. They standardize provider access, prompt content, and structured outputs across the various agent stages and experiments.

14. In diagram terms, the key boundaries are:
   - browser/UI vs. backend API,
   - backend services vs. Supabase persistence,
   - orchestration logic vs. execution sandboxes,
   - product web app vs. benchmark/experiment pipeline.
   The repository is therefore best drawn as two related but distinct systems sharing the same research theme: one operational web app and one offline evaluation harness for model refinement.

Main entry points: [`src/cpmod_web/backend/main.py`](https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/cpmod_web/backend/main.py) (Backend app), [`src/mod-ref-benchmark/langgraph_workflow/workflow.py`](https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/mod-ref-benchmark/langgraph_workflow/workflow.py) (LangGraph flow), [`src/cpmod_web/frontend/app/page.tsx`](https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/cpmod_web/frontend/app/page.tsx) (Next app), [`src/cpmod_web/backend/services/model_package_service.py`](https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/cpmod_web/backend/services/model_package_service.py) (Domain services)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_repo["Repository"]
end

subgraph group_web["cpmod_web"]
  node_web_backend_app["Backend app<br/>FastAPI entrypoint<br/>[main.py]"]
  node_web_backend_auth["Auth guard<br/>middleware<br/>[auth.py]"]
  node_web_backend_routes["API routes<br/>router layer<br/>[projects.py]"]
  node_web_backend_services["Domain services<br/>service layer"]
  node_web_backend_workflow["CR workflow<br/>orchestration<br/>[service.py]"]
  node_web_backend_exec["Execution env<br/>sandbox adapters<br/>[factory.py]"]
  node_web_backend_db[("Supabase DB<br/>persistence<br/>[supabase_client.py]")]
  node_web_backend_migrations["Schema migrations<br/>supabase SQL"]
  node_web_frontend_app["Next app<br/>frontend shell<br/>[page.tsx]"]
  node_web_frontend_api["API client<br/>frontend integration<br/>[api.ts]"]
  node_web_frontend_supabase["Supabase auth<br/>client auth<br/>[supabase.ts]"]
  node_web_frontend_gate["Auth gate<br/>client routing<br/>[auth-gate.tsx]"]
  node_web_frontend_components["UI views<br/>presentation components<br/>[run-viewer.tsx]"]
end

subgraph group_bench["mod-ref-benchmark"]
  node_bench_problems[("Problem folders<br/>filesystem corpus<br/>[problem_desc.txt]")]
  node_bench_llm_core["LLM core<br/>shared LLM layer<br/>[llm_client.py]"]
  node_bench_workflow["LangGraph flow<br/>agent orchestrator<br/>[workflow.py]"]
  node_bench_agents["Agents<br/>[parser_agent.py]"]
  node_bench_experiments["Experiments<br/>runs and baselines<br/>[run_baseline.py]"]
  node_bench_legacy["Legacy flow<br/>older pipeline<br/>[run_modref.py]"]
end

node_web_backend_app -->|"enforces"| node_web_backend_auth
node_web_backend_app -->|"mounts"| node_web_backend_routes
node_web_backend_routes -->|"delegates"| node_web_backend_services
node_web_backend_services -->|"orchestrates"| node_web_backend_workflow
node_web_backend_workflow -->|"runs in"| node_web_backend_exec
node_web_backend_services -->|"queries"| node_web_backend_db
node_web_backend_db -->|"shaped by"| node_web_backend_migrations
node_web_frontend_app -->|"guards"| node_web_frontend_gate
node_web_frontend_gate -->|"uses"| node_web_frontend_supabase
node_web_frontend_app -->|"calls"| node_web_frontend_api
node_web_frontend_app -->|"composes"| node_web_frontend_components
node_web_frontend_api -->|"consumes"| node_web_backend_app
node_web_frontend_supabase -.->|"authenticates"| node_web_backend_db
node_bench_workflow -->|"uses"| node_bench_llm_core
node_bench_workflow -->|"coordinates"| node_bench_agents
node_bench_agents -->|"reads/writes"| node_bench_problems
node_bench_experiments -->|"executes"| node_bench_workflow
node_bench_legacy -.->|"uses"| node_bench_llm_core
node_bench_problems -.->|"evaluates"| node_bench_experiments
node_web_backend_app -.->|"separate runtime"| node_bench_workflow

click node_web_backend_app "https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/cpmod_web/backend/main.py"
click node_web_backend_auth "https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/cpmod_web/backend/middleware/auth.py"
click node_web_backend_routes "https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/cpmod_web/backend/routers/projects.py"
click node_web_backend_services "https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/cpmod_web/backend/services/model_package_service.py"
click node_web_backend_workflow "https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/cpmod_web/backend/workflow/service.py"
click node_web_backend_exec "https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/cpmod_web/backend/services/execution/factory.py"
click node_web_backend_db "https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/cpmod_web/backend/db/supabase_client.py"
click node_web_backend_migrations "https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/cpmod_web/supabase/migrations/001_initial_schema.sql"
click node_web_frontend_app "https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/cpmod_web/frontend/app/page.tsx"
click node_web_frontend_api "https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/cpmod_web/frontend/lib/api.ts"
click node_web_frontend_supabase "https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/cpmod_web/frontend/lib/supabase.ts"
click node_web_frontend_gate "https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/cpmod_web/frontend/components/auth-gate.tsx"
click node_web_frontend_components "https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/cpmod_web/frontend/components/run-viewer.tsx"
click node_bench_problems "https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/mod-ref-benchmark/problems/problem1/base/problem_desc.txt"
click node_bench_llm_core "https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/mod-ref-benchmark/llm_client.py"
click node_bench_workflow "https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/mod-ref-benchmark/langgraph_workflow/workflow.py"
click node_bench_agents "https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/mod-ref-benchmark/langgraph_workflow/agents/parser_agent.py"
click node_bench_experiments "https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/mod-ref-benchmark/baseline/run_baseline.py"
click node_bench_legacy "https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/mod-ref-benchmark/og_workflow_simple/run_modref.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_web_backend_app,node_web_backend_auth,node_web_backend_routes,node_web_backend_services,node_web_backend_workflow,node_web_backend_exec,node_web_backend_db,node_web_backend_migrations,node_web_frontend_app,node_web_frontend_api,node_web_frontend_supabase,node_web_frontend_gate,node_web_frontend_components toneAmber
class node_bench_problems,node_bench_llm_core,node_bench_workflow,node_bench_agents,node_bench_experiments,node_bench_legacy toneMint
```

## Components

### cpmod_web

Product web app for projects, change requests, runs, and model packages.

- [Backend app](https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/cpmod_web/backend/main.py): FastAPI entrypoint · Wires the HTTP application and mounts routers. · `src/cpmod_web/backend/main.py`
- [Auth guard](https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/cpmod_web/backend/middleware/auth.py): middleware · Request-boundary auth enforcement for backend traffic. · `src/cpmod_web/backend/middleware/auth.py`
- [API routes](https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/cpmod_web/backend/routers/projects.py): router layer · Domain HTTP endpoints for projects, runs, dashboard, settings, model packages, and change requests. · `src/cpmod_web/backend/routers/projects.py`
- [Domain services](https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/cpmod_web/backend/services/model_package_service.py): service layer · Business logic for packages, resources, storage, credentials, diffs, policies, and LLM support. · `src/cpmod_web/backend/services/model_package_service.py`
- [CR workflow](https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/cpmod_web/backend/workflow/service.py): orchestration · Graph/state-based automation for change-request processing and LLM-driven steps. · `src/cpmod_web/backend/workflow/service.py`
- [Execution env](https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/cpmod_web/backend/services/execution/factory.py): sandbox adapters · Common execution contract with local and remote E2B implementations plus factory selection. · `src/cpmod_web/backend/services/execution/factory.py`
- [Supabase DB](https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/cpmod_web/backend/db/supabase_client.py): persistence · Supabase client and query layer backing the system of record. · `src/cpmod_web/backend/db/supabase_client.py`
- [Schema migrations](https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/cpmod_web/supabase/migrations/001_initial_schema.sql): supabase SQL · Migration history for projects, change requests, runs, credentials, model selections, and validation artifacts. · `src/cpmod_web/supabase/migrations/001_initial_schema.sql`
- [Next app](https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/cpmod_web/frontend/app/page.tsx): frontend shell · App-router UI for auth, dashboard, projects, runs, settings, and package pages. · `src/cpmod_web/frontend/app/page.tsx`
- [API client](https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/cpmod_web/frontend/lib/api.ts): frontend integration · Centralized backend API calls from the browser app. · `src/cpmod_web/frontend/lib/api.ts`
- [Supabase auth](https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/cpmod_web/frontend/lib/supabase.ts): client auth · Browser-side Supabase client and session integration. · `src/cpmod_web/frontend/lib/supabase.ts`
- [Auth gate](https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/cpmod_web/frontend/components/auth-gate.tsx): client routing · Authenticated routing and access control on the client side. · `src/cpmod_web/frontend/components/auth-gate.tsx`
- [UI views](https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/cpmod_web/frontend/components/run-viewer.tsx): presentation components · Project, run, diff, CR form, and upload views mapped to backend concepts. · `src/cpmod_web/frontend/components/run-viewer.tsx`

### mod-ref-benchmark

Offline model-refinement benchmark and experiment pipeline.

- [Problem folders](https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/mod-ref-benchmark/problems/problem1/base/problem_desc.txt): filesystem corpus · Folder-centric benchmark inputs with base problem data and CR-specific subfolders. · `src/mod-ref-benchmark/problems/problem1/base/problem_desc.txt`
- [LLM core](https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/mod-ref-benchmark/llm_client.py): shared LLM layer · Provider access, prompts, schemas, and model presets shared across benchmark agents. · `src/mod-ref-benchmark/llm_client.py`
- [LangGraph flow](https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/mod-ref-benchmark/langgraph_workflow/workflow.py): agent orchestrator · Staged parser/planner/modifier/executor/validator workflow for refinement experiments. · `src/mod-ref-benchmark/langgraph_workflow/workflow.py`
- [Agents](https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/mod-ref-benchmark/langgraph_workflow/agents/parser_agent.py): LLM agents · Parser, planner, modifier, executor, and validator agent implementations. · `src/mod-ref-benchmark/langgraph_workflow/agents/parser_agent.py`
- [Experiments](https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/mod-ref-benchmark/baseline/run_baseline.py): runs and baselines · Baseline runner, ablations, and cross-model evaluation scripts. · `src/mod-ref-benchmark/baseline/run_baseline.py`
- [Legacy flow](https://github.com/ymoola/llm4cp-mod-ref/blob/main/src/mod-ref-benchmark/og_workflow_simple/run_modref.py): older pipeline · Simpler earlier mod-ref workflow used alongside the LangGraph pipeline. · `src/mod-ref-benchmark/og_workflow_simple/run_modref.py`

## Connections

- Backend app → Auth guard: enforces · Request handling passes through auth middleware.
- Backend app → API routes: mounts · FastAPI app wires the domain routers.
- API routes → Domain services: delegates · Routers call into service-layer business logic.
- Domain services → CR workflow: orchestrates · Change-request processing uses workflow orchestration.
- CR workflow → Execution env: runs in · Workflow invokes execution environments through the adapter contract.
- Domain services → Supabase DB: queries · Services persist and retrieve records through Supabase queries.
- Supabase DB → Schema migrations: shaped by · Schema evolution defines the persistence model.
- Next app → Auth gate: guards · Client routes are protected by the auth gate.
- Auth gate → Supabase auth: uses · Auth state is sourced from Supabase on the client.
- Next app → API client: calls · UI pages fetch and mutate via the centralized API client.
- Next app → UI views: composes · Pages are built from shared product-specific components.
- API client → Backend app: consumes · Browser UI talks to the FastAPI backend.
- Supabase auth → Supabase DB: authenticates · Frontend session management aligns with Supabase-backed backend data.
- LangGraph flow → LLM core: uses · Workflow stages share prompts, schemas, and model/provider access.
- LangGraph flow → Agents: coordinates · The orchestrator runs the staged agents.
- Agents → Problem folders: reads/writes · Agents operate over problem folders and generated artifacts.
- Experiments → LangGraph flow: executes · Baseline and evaluation scripts drive the workflow pipeline.
- Legacy flow → LLM core: uses · Legacy workflow also relies on the shared LLM layer.
- Problem folders → Experiments: evaluates · Experiment runs consume the benchmark corpus.
- Backend app → LangGraph flow: separate runtime · Architecturally distinct systems; no direct in-process dependency implied.
