# cginside-rookies/hyundaihs architecture

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

- Interactive diagram: https://gitdiagram.com/cginside-rookies/hyundaihs
- Repository: https://github.com/cginside-rookies/hyundaihs

## Overview

#### Purpose and service boundaries
HyundaiHS Document QA is an internal scanned-document verification system. It separates:
- A document-processing orchestrator: ingestion, page understanding, OCR/layout, ROI/extraction, persistence, review integration.
- A workflow-validation API: rule-driven business validation, currently including WF1 and WF2.
- Operator/product-facing static frontends that consume these APIs.

Primary service entry points are `main.py`, `orchestrator/api/server.py`, `orchestrator/server.py`, and `workflow_api/main.py`.

#### Ingestion, job execution, and runtime state
The orchestrator accepts local documents and untrusted ZIP uploads, normalizes files into upload workspaces, creates jobs, and exposes job status/SSE updates. ZIP handling is a security boundary: archive contents are validated and flattened before pipeline execution.

Core paths:
- `orchestrator/uploads.py`
- `orchestrator/runtime/app.py`
- `orchestrator/workflow_document_normalizer.py`
- `orchestrator/api/server.py`

Run artifacts and canonical job/run state are persisted through the storage layer rather than being owned by the UI.

#### Document intelligence pipeline
Processing is staged from PDF/image decoding through per-page classification and analysis, layout/OCR, ROI generation, optional field extraction, and reconciliation. This is the central control flow: normalized document → pages/artifacts → candidate fields/evidence → canonical result.

Core paths:
- `orchestrator/core/page_analysis.py`
- `orchestrator/core/layout_artifacts.py`
- `orchestrator/core/roi_strategy.py`
- `orchestrator/core/extraction.py`
- `orchestrator/core/reconcile.py`

PaddleX provides layout/OCR integration, while image/PDF operations are isolated behind adapters, allowing pipeline logic to remain independent of concrete OCR/image providers.

#### Domain-pack policy boundary
Document-specific behavior is loaded as a domain pack rather than embedded in generic orchestration. The Hyundai Home pack defines document types, schemas, ROI behavior, canonicalization policy, cross-confirmation, and prompt assets. This is the main extension boundary for adding document families or changing extraction semantics.

Core paths:
- `orchestrator/domain/loader.py`
- `domain_packs/hyundai_home/domain.py`
- `domain_packs/hyundai_home/schemas.py`
- `domain_packs/hyundai_home/canonical_policy.py`

LLM-facing tasks and prompt rendering are kept separate from domain prompt content.

#### Persistence and external integrations
Postgres is the durable system of record for orchestrator results and Label Studio state. The orchestrator integrates with Label Studio to export review tasks and synchronize annotations. It also uses OpenAI-compatible/Gemma client paths for model-backed analysis or extraction and PaddleX for document vision.

Core paths:
- `orchestrator/storage/store.py`
- `orchestrator/integrations/label_studio.py`
- `orchestrator/adapters/paddlex.py`
- `orchestrator/adapters/openai_compatible.py`
- `orchestrator/clients/gemma.py`

Host runtime additionally requires Poppler (`pdftoppm`) for PDF rendering; upload, temporary, and output directories are environment-configured.

#### Workflow validation API
`workflow_api` is an independently deployable FastAPI service for rule evaluation over document/extraction payloads. WF1 encapsulates address-oriented rules; WF2 resolves requirements and evaluates conditions, ingredient evidence, labeling, consumption, and coverage against JSON rule packs. This service is a business-policy boundary and should not depend on OCR/layout internals.

Core paths:
- `workflow_api/api.py`
- `workflow_api/wf1/engine.py`
- `workflow_api/wf2/engine.py`
- `workflow_api/wf2/resolver.py`
- `workflow_api/rule_packs/wf2_food_livestock.json`

#### Presentation and operator review
The current interfaces are static JavaScript operator/debug clients, not server-rendered application logic. The v2 frontend supports upload, list/detail review, evidence, checks, labeling, document selection, and status views; it calls orchestrator APIs and can direct operators into Label Studio workflows. A build step produces the deployable dashboard artifact from source templates.

Core paths:
- `frontend-v2/index.html`
- `frontend-v2/assets/detail/main.js`
- `frontend/src/dashboard/controller.js`
- `scripts/build_frontend.py`

Main entry points: [`orchestrator/runtime/app.py`](https://github.com/cginside-rookies/hyundaihs/blob/main/orchestrator/runtime/app.py) (Job Runtime), [`orchestrator/api/server.py`](https://github.com/cginside-rookies/hyundaihs/blob/main/orchestrator/api/server.py) (Orchestrator API), [`orchestrator/core/layout_artifacts.py`](https://github.com/cginside-rookies/hyundaihs/blob/main/orchestrator/core/layout_artifacts.py) (Layout & OCR), [`orchestrator/core/extraction.py`](https://github.com/cginside-rookies/hyundaihs/blob/main/orchestrator/core/extraction.py) (ROI & Extraction)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_orchestrator["Document Orchestrator"]
  node_orchestrator_api["Orchestrator API<br/>FastAPI service<br/>[server.py]"]
  node_upload_security["Upload Normalization<br/>ingestion boundary<br/>[uploads.py]"]
  node_runtime["Job Runtime<br/>execution runtime<br/>[app.py]"]
  node_document_normalizer["Document Normalizer<br/>input preparation"]
  node_page_analysis["Page Analysis<br/>pipeline stage<br/>[page_analysis.py]"]
  node_layout_ocr["Layout &amp; OCR<br/>pipeline stage"]
  node_roi_extraction["ROI &amp; Extraction<br/>pipeline stage<br/>[extraction.py]"]
  node_reconciliation["Reconciliation<br/>pipeline stage<br/>[reconcile.py]"]
  node_storage[("Postgres Store<br/>persistence layer<br/>[store.py]")]
  node_label_studio["Label Studio<br/>review integration<br/>[label_studio.py]"]
  node_vision_provider{{"PaddleX<br/>vision adapter<br/>[paddlex.py]"}}
  node_llm_provider{{"OpenAI/Gemma Clients<br/>model adapter"}}
end

subgraph group_domain["Domain Pack"]
  node_domain_loader["Domain Loader<br/>extension boundary<br/>[loader.py]"]
  node_hyundai_pack["Hyundai Home Pack<br/>domain definition<br/>[domain.py]"]
  node_canonical_policy["Canonical Policy<br/>domain policy"]
end

subgraph group_workflow["Workflow Validation API"]
  node_workflow_api["Workflow API<br/>FastAPI service<br/>[api.py]"]
  node_wf1["WF1 Address Rules<br/>rule engine<br/>[engine.py]"]
  node_wf2["WF2 Rule Engine<br/>[engine.py]"]
  node_wf2_rules["WF2 Food/Livestock Pack<br/>JSON rule pack"]
end

subgraph group_presentation["Operator Interfaces"]
  node_operator_frontend["v2 Operator Dashboard<br/>static frontend<br/>[index.html]"]
  node_dashboard_build["Dashboard Build<br/>frontend build<br/>[build_frontend.py]"]
end

node_operator_frontend -->|"uploads and reviews"| node_orchestrator_api
node_operator_frontend -->|"requests validation"| node_workflow_api
node_dashboard_build -->|"produces artifact"| node_operator_frontend
node_orchestrator_api -->|"accepts documents"| node_upload_security
node_upload_security -->|"validated workspace"| node_document_normalizer
node_document_normalizer -->|"normalized input"| node_runtime
node_orchestrator_api -->|"creates jobs"| node_runtime
node_runtime -->|"executes pipeline"| node_page_analysis
node_page_analysis -->|"page artifacts"| node_layout_ocr
node_layout_ocr -->|"layout and OCR"| node_roi_extraction
node_roi_extraction -->|"candidate fields"| node_reconciliation
node_reconciliation -->|"canonical results"| node_storage
node_runtime -->|"job and run state"| node_storage
node_runtime -->|"loads policy"| node_domain_loader
node_domain_loader -->|"loads"| node_hyundai_pack
node_hyundai_pack -->|"defines"| node_canonical_policy
node_canonical_policy -->|"canonicalization rules"| node_reconciliation
node_layout_ocr -->|"uses"| node_vision_provider
node_roi_extraction -.->|"optional model tasks"| node_llm_provider
node_storage -->|"review task sync"| node_label_studio
node_workflow_api -->|"routes WF1"| node_wf1
node_workflow_api -->|"routes WF2"| node_wf2
node_wf2 -->|"evaluates"| node_wf2_rules

click node_orchestrator_api "https://github.com/cginside-rookies/hyundaihs/blob/main/orchestrator/api/server.py"
click node_upload_security "https://github.com/cginside-rookies/hyundaihs/blob/main/orchestrator/uploads.py"
click node_runtime "https://github.com/cginside-rookies/hyundaihs/blob/main/orchestrator/runtime/app.py"
click node_document_normalizer "https://github.com/cginside-rookies/hyundaihs/blob/main/orchestrator/workflow_document_normalizer.py"
click node_page_analysis "https://github.com/cginside-rookies/hyundaihs/blob/main/orchestrator/core/page_analysis.py"
click node_layout_ocr "https://github.com/cginside-rookies/hyundaihs/blob/main/orchestrator/core/layout_artifacts.py"
click node_roi_extraction "https://github.com/cginside-rookies/hyundaihs/blob/main/orchestrator/core/extraction.py"
click node_reconciliation "https://github.com/cginside-rookies/hyundaihs/blob/main/orchestrator/core/reconcile.py"
click node_storage "https://github.com/cginside-rookies/hyundaihs/blob/main/orchestrator/storage/store.py"
click node_label_studio "https://github.com/cginside-rookies/hyundaihs/blob/main/orchestrator/integrations/label_studio.py"
click node_domain_loader "https://github.com/cginside-rookies/hyundaihs/blob/main/orchestrator/domain/loader.py"
click node_hyundai_pack "https://github.com/cginside-rookies/hyundaihs/blob/main/domain_packs/hyundai_home/domain.py"
click node_canonical_policy "https://github.com/cginside-rookies/hyundaihs/blob/main/domain_packs/hyundai_home/canonical_policy.py"
click node_vision_provider "https://github.com/cginside-rookies/hyundaihs/blob/main/orchestrator/adapters/paddlex.py"
click node_llm_provider "https://github.com/cginside-rookies/hyundaihs/blob/main/orchestrator/adapters/openai_compatible.py"
click node_workflow_api "https://github.com/cginside-rookies/hyundaihs/blob/main/workflow_api/api.py"
click node_wf1 "https://github.com/cginside-rookies/hyundaihs/blob/main/workflow_api/wf1/engine.py"
click node_wf2 "https://github.com/cginside-rookies/hyundaihs/blob/main/workflow_api/wf2/engine.py"
click node_wf2_rules "https://github.com/cginside-rookies/hyundaihs/blob/main/workflow_api/rule_packs/wf2_food_livestock.json"
click node_operator_frontend "https://github.com/cginside-rookies/hyundaihs/blob/main/frontend-v2/index.html"
click node_dashboard_build "https://github.com/cginside-rookies/hyundaihs/blob/main/scripts/build_frontend.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_orchestrator_api,node_upload_security,node_runtime,node_document_normalizer,node_page_analysis,node_layout_ocr,node_roi_extraction,node_reconciliation,node_storage,node_label_studio,node_vision_provider,node_llm_provider toneBlue
class node_domain_loader,node_hyundai_pack,node_canonical_policy toneAmber
class node_workflow_api,node_wf1,node_wf2,node_wf2_rules toneMint
class node_operator_frontend,node_dashboard_build toneRose
```

## Components

### Document Orchestrator

Ingestion, processing, review integration, and durable job state.

- [Orchestrator API](https://github.com/cginside-rookies/hyundaihs/blob/main/orchestrator/api/server.py): FastAPI service · Creates jobs and exposes status and SSE updates. · `orchestrator/api/server.py`
- [Upload Normalization](https://github.com/cginside-rookies/hyundaihs/blob/main/orchestrator/uploads.py): ingestion boundary · Validates ZIPs and prepares flattened workspaces. · `orchestrator/uploads.py`
- [Job Runtime](https://github.com/cginside-rookies/hyundaihs/blob/main/orchestrator/runtime/app.py): execution runtime · Coordinates job lifecycle and pipeline execution. · `orchestrator/runtime/app.py`
- [Document Normalizer](https://github.com/cginside-rookies/hyundaihs/blob/main/orchestrator/workflow_document_normalizer.py): input preparation · Normalizes workflow document inputs. · `orchestrator/workflow_document_normalizer.py`
- [Page Analysis](https://github.com/cginside-rookies/hyundaihs/blob/main/orchestrator/core/page_analysis.py): pipeline stage · Decodes documents and classifies pages into artifacts. · `orchestrator/core/page_analysis.py`
- [Layout & OCR](https://github.com/cginside-rookies/hyundaihs/blob/main/orchestrator/core/layout_artifacts.py): pipeline stage · Produces layout and text artifacts per page. · `orchestrator/core/layout_artifacts.py`
- [ROI & Extraction](https://github.com/cginside-rookies/hyundaihs/blob/main/orchestrator/core/extraction.py): pipeline stage · Generates regions and candidate fields with evidence. · `orchestrator/core/extraction.py`
- [Reconciliation](https://github.com/cginside-rookies/hyundaihs/blob/main/orchestrator/core/reconcile.py): pipeline stage · Builds canonical document results from candidates. · `orchestrator/core/reconcile.py`
- [Postgres Store](https://github.com/cginside-rookies/hyundaihs/blob/main/orchestrator/storage/store.py): persistence layer · Durable record for jobs, artifacts, results, and review state. · `orchestrator/storage/store.py`
- [Label Studio](https://github.com/cginside-rookies/hyundaihs/blob/main/orchestrator/integrations/label_studio.py): review integration · Exports review tasks and synchronizes annotations. · `orchestrator/integrations/label_studio.py`
- [PaddleX](https://github.com/cginside-rookies/hyundaihs/blob/main/orchestrator/adapters/paddlex.py): vision adapter · Concrete layout and OCR provider. · `orchestrator/adapters/paddlex.py`
- [OpenAI/Gemma Clients](https://github.com/cginside-rookies/hyundaihs/blob/main/orchestrator/adapters/openai_compatible.py): model adapter · Model-backed analysis and extraction integration. · `orchestrator/adapters/openai_compatible.py`

### Domain Pack

Document-family policy and extraction semantics.

- [Domain Loader](https://github.com/cginside-rookies/hyundaihs/blob/main/orchestrator/domain/loader.py): extension boundary · Loads document-specific packs into generic orchestration. · `orchestrator/domain/loader.py`
- [Hyundai Home Pack](https://github.com/cginside-rookies/hyundaihs/blob/main/domain_packs/hyundai_home/domain.py): domain definition · Defines document types, schemas, ROI behavior, and prompts. · `domain_packs/hyundai_home/domain.py`
- [Canonical Policy](https://github.com/cginside-rookies/hyundaihs/blob/main/domain_packs/hyundai_home/canonical_policy.py): domain policy · Applies canonicalization and cross-confirmation rules. · `domain_packs/hyundai_home/canonical_policy.py`

### Workflow Validation API

Independent business-rule evaluation service.

- [Workflow API](https://github.com/cginside-rookies/hyundaihs/blob/main/workflow_api/api.py): FastAPI service · Evaluates business rules over document and extraction payloads. · `workflow_api/api.py`
- [WF1 Address Rules](https://github.com/cginside-rookies/hyundaihs/blob/main/workflow_api/wf1/engine.py): rule engine · Address-oriented validation rules. · `workflow_api/wf1/engine.py`
- [WF2 Rule Engine](https://github.com/cginside-rookies/hyundaihs/blob/main/workflow_api/wf2/engine.py): rule engine · Resolves requirements and evaluates evidence and coverage. · `workflow_api/wf2/engine.py`
- [WF2 Food/Livestock Pack](https://github.com/cginside-rookies/hyundaihs/blob/main/workflow_api/rule_packs/wf2_food_livestock.json): JSON rule pack · `workflow_api/rule_packs/wf2_food_livestock.json`

### Operator Interfaces

Static upload and review clients.

- [v2 Operator Dashboard](https://github.com/cginside-rookies/hyundaihs/blob/main/frontend-v2/index.html): static frontend · Upload, review, evidence, checks, and Label Studio navigation. · `frontend-v2/index.html`
- [Dashboard Build](https://github.com/cginside-rookies/hyundaihs/blob/main/scripts/build_frontend.py): frontend build · Builds the deployable dashboard from source templates. · `scripts/build_frontend.py`

## Connections

- v2 Operator Dashboard → Orchestrator API: uploads and reviews
- v2 Operator Dashboard → Workflow API: requests validation
- Dashboard Build → v2 Operator Dashboard: produces artifact
- Orchestrator API → Upload Normalization: accepts documents
- Upload Normalization → Document Normalizer: validated workspace
- Document Normalizer → Job Runtime: normalized input
- Orchestrator API → Job Runtime: creates jobs
- Job Runtime → Page Analysis: executes pipeline
- Page Analysis → Layout & OCR: page artifacts
- Layout & OCR → ROI & Extraction: layout and OCR
- ROI & Extraction → Reconciliation: candidate fields
- Reconciliation → Postgres Store: canonical results
- Job Runtime → Postgres Store: job and run state
- Job Runtime → Domain Loader: loads policy
- Domain Loader → Hyundai Home Pack: loads
- Hyundai Home Pack → Canonical Policy: defines
- Canonical Policy → Reconciliation: canonicalization rules
- Layout & OCR → PaddleX: uses
- ROI & Extraction → OpenAI/Gemma Clients: optional model tasks
- Postgres Store → Label Studio: review task sync
- Workflow API → WF1 Address Rules: routes WF1
- Workflow API → WF2 Rule Engine: routes WF2
- WF2 Rule Engine → WF2 Food/Livestock Pack: evaluates
