# komaksym/alfredotheclerk architecture

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

- Interactive diagram: https://gitdiagram.com/komaksym/alfredotheclerk
- Repository: https://github.com/komaksym/alfredotheclerk

## Overview

#### Purpose and entry points
AlfredoTheClerk converts supported native-text Polish VAT invoice PDFs into validated KSeF FA(3) XML. Its primary runtime entry point is the local review application, launched with `python -m src.review_ui`; its module launcher is `src/review_ui/__main__.py`. Batch evaluation is launched through `src/agentic_repair/benchmark_runner.py`. The system is intentionally not an automatic production submission service.

#### 1. PDF ingestion and deterministic extraction
The pipeline accepts a constrained PDF shape, parses its native text, then extracts invoice fields and evidence candidates. This creates the evidence boundary that downstream automation must respect: values are extracted from the source rather than generated.
- `src/input_processing/parse_pdf.py`
- `src/input_processing/invoice_text_field_extraction.py`
- `src/input_processing/extraction_diagnostics.py`

#### 2. Domain shell and invoice completion
Extracted values populate a structured invoice “shell,” which represents the incomplete invoice before rendering. Shell summaries and validation identify missing, inconsistent, or invalid fields; this is the contract between parsing, repair, review, and XML generation.
- `src/input_processing/populate_shell.py`
- `src/invoice_gen/domain_shell.py`
- `src/invoice_gen/domestic_vat_shell_validation.py`

#### 3. Evidence-constrained repair orchestration
The agentic-repair subsystem routes eligible defects to an LLM-backed decision stage. It packages candidate evidence, requires an explicit repair or human-review outcome per field, applies atomic repairs, and leaves ambiguity unresolved rather than inventing values.
- `src/agentic_repair/repair_orchestration.py`
- `src/agentic_repair/agent_extraction_repair.py`
- `src/agentic_repair/repair_kernel.py`

DeepSeek is the material external API, configured through `src/agentic_repair/config.py`; calls occur only for repair-required fields.

#### 4. Human-review boundary and local UI
Unresolved fields enter a local browser workflow that preserves the source PDF, displays automated changes as audit information, and collects human corrections. Session and presentation logic isolate the UI from repair and validation mechanics.
- `src/review_ui/app.py`
- `src/review_ui/session.py`
- `src/agentic_repair/human_review.py`

The app serves locally on port 8000 and uses HTML/CSS/JS assets under `src/review_ui/templates/` and `src/review_ui/static/`.

#### 5. Correctness gates and FA(3) generation
Both automated and human edits return through the same gate: shell checks, VAT/totals checks, FA(3) mapping, XML rendering, and XSD validation. XML availability is therefore controlled by correctness state, not merely successful extraction or review.
- `src/invoice_gen/invoice_correctness.py`
- `src/invoice_gen/domestic_vat_faktura_mapping.py`
- `src/invoice_gen/domestic_vat_xml_rendering.py`

FA(3) validation uses bundled schemas in `src/invoice_gen/schemas/schemat.xsd` and related schema files; generated XML is the core output.

#### 6. KSeF integration boundary
A separate KSeF client handles configuration, cryptography, transport, and submission models. It is architecturally separated from local conversion/review, consistent with the app not automatically submitting invoices.
- `src/ksef/submission.py`
- `src/ksef/crypto.py`
- `src/ksef/transport.py`

CI includes a secret-backed KSeF TEST submission gate via `.github/workflows/ci.yml`.

#### 7. Benchmark and regression corpus
Controlled benchmark tooling executes repair flows, scores decisions, and publishes results. Fixtures combine PDFs, expected XML, shell states, and comparison policies, making the corpus a major safety/regression boundary rather than incidental sample data.
- `src/agentic_repair/benchmark_runner.py`
- `src/agentic_repair/benchmark_scoring.py`
- `data/benchmark_cases/hard_cases/`

Python 3.13 with `uv` is the principal runtime/package environment; CI workflows are under `.github/workflows/`.

Main entry points: [`src/agentic_repair/repair_orchestration.py`](https://github.com/komaksym/alfredotheclerk/blob/main/src/agentic_repair/repair_orchestration.py) (Repair orchestration), [`src/invoice_gen/domain_shell.py`](https://github.com/komaksym/alfredotheclerk/blob/main/src/invoice_gen/domain_shell.py) (Invoice shell), [`src/input_processing/invoice_text_field_extraction.py`](https://github.com/komaksym/alfredotheclerk/blob/main/src/input_processing/invoice_text_field_extraction.py) (Field extraction), [`src/invoice_gen/domestic_vat_shell_validation.py`](https://github.com/komaksym/alfredotheclerk/blob/main/src/invoice_gen/domestic_vat_shell_validation.py) (Shell validation)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_conversion["Conversion pipeline"]
  node_pdf["Native-text PDF<br/>input"]
  node_parse["PDF parsing<br/>parser<br/>[parse_pdf.py]"]
  node_extract["Field extraction<br/>extractor"]
  node_diagnostics["Extraction diagnostics"]
  node_shell["Invoice shell<br/>domain model<br/>[domain_shell.py]"]
  node_populate["Shell population<br/>mapper<br/>[populate_shell.py]"]
  node_shell_validation["Shell validation<br/>validator"]
end

subgraph group_repair["Repair and review"]
  node_orchestration["Repair orchestration<br/>orchestrator"]
  node_llm_repair{{"LLM repair decision<br/>agent"}}
  node_repair_kernel["Atomic repair kernel<br/>repair engine<br/>[repair_kernel.py]"]
  node_human_review["Human review workflow<br/>review boundary<br/>[human_review.py]"]
  node_review_ui["Local review app<br/>local web UI<br/>[app.py]"]
  node_session["Review session<br/>UI state<br/>[session.py]"]
end

subgraph group_output["Correctness and output"]
  node_correctness["Invoice correctness gate<br/>validator"]
  node_fa3_mapping["FA&#40;3&#41; mapping<br/>mapper"]
  node_xml_render["FA&#40;3&#41; XML rendering<br/>renderer"]
  node_xsd["Bundled FA&#40;3&#41; schemas<br/>XSD<br/>[schemat.xsd]"]
  node_xml["Validated KSeF FA&#40;3&#41; XML<br/>output"]
end

subgraph group_external["External boundaries"]
  node_deepseek{{"DeepSeek API<br/>external API<br/>[config.py]"}}
  node_ksef{{"KSeF client<br/>submission client<br/>[submission.py]"}}
end

subgraph group_quality["Benchmark and CI"]
  node_benchmark["Repair benchmark runner<br/>batch runner"]
  node_corpus[("Hard-case corpus<br/>regression data")]
  node_ci["CI workflows<br/>automation<br/>[ci.yml]"]
end

node_pdf -->|"native text"| node_parse
node_parse -->|"parsed text"| node_extract
node_extract -->|"extraction evidence"| node_diagnostics
node_extract -->|"extracted values"| node_populate
node_populate -->|"populates"| node_shell
node_shell -->|"validate"| node_shell_validation
node_diagnostics -->|"repair-required defects"| node_orchestration
node_shell_validation -->|"eligible defects"| node_orchestration
node_orchestration -->|"evidence-constrained decision"| node_llm_repair
node_llm_repair -->|"repair-required calls"| node_deepseek
node_llm_repair -->|"approved repair"| node_repair_kernel
node_repair_kernel -->|"atomic update"| node_shell
node_orchestration -->|"unresolved fields"| node_human_review
node_human_review -->|"review tasks"| node_review_ui
node_review_ui -->|"local workflow"| node_session
node_session -->|"human corrections"| node_shell
node_shell_validation -->|"validated shell"| node_correctness
node_correctness -->|"correct invoice"| node_fa3_mapping
node_fa3_mapping -->|"FA&#40;3&#41; model"| node_xml_render
node_xml_render -->|"validate XML"| node_xsd
node_xsd -->|"valid"| node_xml
node_xml -.->|"optional submission"| node_ksef
node_corpus -->|"cases and expectations"| node_benchmark
node_benchmark -.->|"executes repair flows"| node_orchestration
node_ci -->|"regression gate"| node_benchmark
node_ci -.->|"TEST submission gate"| node_ksef

click node_parse "https://github.com/komaksym/alfredotheclerk/blob/main/src/input_processing/parse_pdf.py"
click node_extract "https://github.com/komaksym/alfredotheclerk/blob/main/src/input_processing/invoice_text_field_extraction.py"
click node_diagnostics "https://github.com/komaksym/alfredotheclerk/blob/main/src/input_processing/extraction_diagnostics.py"
click node_shell "https://github.com/komaksym/alfredotheclerk/blob/main/src/invoice_gen/domain_shell.py"
click node_populate "https://github.com/komaksym/alfredotheclerk/blob/main/src/input_processing/populate_shell.py"
click node_shell_validation "https://github.com/komaksym/alfredotheclerk/blob/main/src/invoice_gen/domestic_vat_shell_validation.py"
click node_orchestration "https://github.com/komaksym/alfredotheclerk/blob/main/src/agentic_repair/repair_orchestration.py"
click node_llm_repair "https://github.com/komaksym/alfredotheclerk/blob/main/src/agentic_repair/agent_extraction_repair.py"
click node_deepseek "https://github.com/komaksym/alfredotheclerk/blob/main/src/agentic_repair/config.py"
click node_repair_kernel "https://github.com/komaksym/alfredotheclerk/blob/main/src/agentic_repair/repair_kernel.py"
click node_human_review "https://github.com/komaksym/alfredotheclerk/blob/main/src/agentic_repair/human_review.py"
click node_review_ui "https://github.com/komaksym/alfredotheclerk/blob/main/src/review_ui/app.py"
click node_session "https://github.com/komaksym/alfredotheclerk/blob/main/src/review_ui/session.py"
click node_correctness "https://github.com/komaksym/alfredotheclerk/blob/main/src/invoice_gen/invoice_correctness.py"
click node_fa3_mapping "https://github.com/komaksym/alfredotheclerk/blob/main/src/invoice_gen/domestic_vat_faktura_mapping.py"
click node_xml_render "https://github.com/komaksym/alfredotheclerk/blob/main/src/invoice_gen/domestic_vat_xml_rendering.py"
click node_xsd "https://github.com/komaksym/alfredotheclerk/blob/main/src/invoice_gen/schemas/schemat.xsd"
click node_ksef "https://github.com/komaksym/alfredotheclerk/blob/main/src/ksef/submission.py"
click node_benchmark "https://github.com/komaksym/alfredotheclerk/blob/main/src/agentic_repair/benchmark_runner.py"
click node_ci "https://github.com/komaksym/alfredotheclerk/blob/main/.github/workflows/ci.yml"

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_pdf,node_parse,node_extract,node_diagnostics,node_shell,node_populate,node_shell_validation toneBlue
class node_orchestration,node_llm_repair,node_repair_kernel,node_human_review,node_review_ui,node_session toneAmber
class node_correctness,node_fa3_mapping,node_xml_render,node_xsd,node_xml toneMint
class node_deepseek,node_ksef toneRose
class node_benchmark,node_corpus,node_ci toneIndigo
```

## Components

### Conversion pipeline

- **Native-text PDF**: input · Supported Polish VAT invoice PDF.
- [PDF parsing](https://github.com/komaksym/alfredotheclerk/blob/main/src/input_processing/parse_pdf.py): parser · `src/input_processing/parse_pdf.py`
- [Field extraction](https://github.com/komaksym/alfredotheclerk/blob/main/src/input_processing/invoice_text_field_extraction.py): extractor · Extracts invoice values and evidence candidates. · `src/input_processing/invoice_text_field_extraction.py`
- [Extraction diagnostics](https://github.com/komaksym/alfredotheclerk/blob/main/src/input_processing/extraction_diagnostics.py): diagnostics · `src/input_processing/extraction_diagnostics.py`
- [Invoice shell](https://github.com/komaksym/alfredotheclerk/blob/main/src/invoice_gen/domain_shell.py): domain model · Incomplete structured invoice and cross-stage contract. · `src/invoice_gen/domain_shell.py`
- [Shell population](https://github.com/komaksym/alfredotheclerk/blob/main/src/input_processing/populate_shell.py): mapper · `src/input_processing/populate_shell.py`
- [Shell validation](https://github.com/komaksym/alfredotheclerk/blob/main/src/invoice_gen/domestic_vat_shell_validation.py): validator · Finds missing, inconsistent, and invalid fields. · `src/invoice_gen/domestic_vat_shell_validation.py`

### Repair and review

- [Repair orchestration](https://github.com/komaksym/alfredotheclerk/blob/main/src/agentic_repair/repair_orchestration.py): orchestrator · Routes eligible defects and requires repair or review outcomes. · `src/agentic_repair/repair_orchestration.py`
- [LLM repair decision](https://github.com/komaksym/alfredotheclerk/blob/main/src/agentic_repair/agent_extraction_repair.py): agent · Uses evidence candidates only for repair-required fields. · `src/agentic_repair/agent_extraction_repair.py`
- [Atomic repair kernel](https://github.com/komaksym/alfredotheclerk/blob/main/src/agentic_repair/repair_kernel.py): repair engine · Applies approved field repairs without inventing ambiguous values. · `src/agentic_repair/repair_kernel.py`
- [Human review workflow](https://github.com/komaksym/alfredotheclerk/blob/main/src/agentic_repair/human_review.py): review boundary · Collects corrections for unresolved fields. · `src/agentic_repair/human_review.py`
- [Local review app](https://github.com/komaksym/alfredotheclerk/blob/main/src/review_ui/app.py): local web UI · Serves source PDF, audit changes, and correction controls on port 8000. · `src/review_ui/app.py`
- [Review session](https://github.com/komaksym/alfredotheclerk/blob/main/src/review_ui/session.py): UI state · Isolates presentation and session state from repair mechanics. · `src/review_ui/session.py`

### Correctness and output

- [Invoice correctness gate](https://github.com/komaksym/alfredotheclerk/blob/main/src/invoice_gen/invoice_correctness.py): validator · Applies shell and VAT/totals checks before XML is available. · `src/invoice_gen/invoice_correctness.py`
- [FA(3) mapping](https://github.com/komaksym/alfredotheclerk/blob/main/src/invoice_gen/domestic_vat_faktura_mapping.py): mapper · `src/invoice_gen/domestic_vat_faktura_mapping.py`
- [FA(3) XML rendering](https://github.com/komaksym/alfredotheclerk/blob/main/src/invoice_gen/domestic_vat_xml_rendering.py): renderer · `src/invoice_gen/domestic_vat_xml_rendering.py`
- [Bundled FA(3) schemas](https://github.com/komaksym/alfredotheclerk/blob/main/src/invoice_gen/schemas/schemat.xsd): XSD · Validates generated XML. · `src/invoice_gen/schemas/schemat.xsd`
- **Validated KSeF FA(3) XML**: output · Core conversion output.

### External boundaries

- [DeepSeek API](https://github.com/komaksym/alfredotheclerk/blob/main/src/agentic_repair/config.py): external API · `src/agentic_repair/config.py`
- [KSeF client](https://github.com/komaksym/alfredotheclerk/blob/main/src/ksef/submission.py): submission client · Separate optional boundary for cryptography, transport, and submission. · `src/ksef/submission.py`

### Benchmark and CI

- [Repair benchmark runner](https://github.com/komaksym/alfredotheclerk/blob/main/src/agentic_repair/benchmark_runner.py): batch runner · Executes and scores controlled repair flows. · `src/agentic_repair/benchmark_runner.py`
- **Hard-case corpus**: regression data · PDFs, expected XML, shell states, and comparison policies.
- [CI workflows](https://github.com/komaksym/alfredotheclerk/blob/main/.github/workflows/ci.yml): automation · Includes a secret-backed KSeF TEST submission gate. · `.github/workflows/ci.yml`

## Connections

- Native-text PDF → PDF parsing: native text
- PDF parsing → Field extraction: parsed text
- Field extraction → Extraction diagnostics: extraction evidence
- Field extraction → Shell population: extracted values
- Shell population → Invoice shell: populates
- Invoice shell → Shell validation: validate
- Extraction diagnostics → Repair orchestration: repair-required defects
- Shell validation → Repair orchestration: eligible defects
- Repair orchestration → LLM repair decision: evidence-constrained decision
- LLM repair decision → DeepSeek API: repair-required calls
- LLM repair decision → Atomic repair kernel: approved repair
- Atomic repair kernel → Invoice shell: atomic update
- Repair orchestration → Human review workflow: unresolved fields
- Human review workflow → Local review app: review tasks
- Local review app → Review session: local workflow
- Review session → Invoice shell: human corrections
- Shell validation → Invoice correctness gate: validated shell
- Invoice correctness gate → FA(3) mapping: correct invoice
- FA(3) mapping → FA(3) XML rendering: FA(3) model
- FA(3) XML rendering → Bundled FA(3) schemas: validate XML
- Bundled FA(3) schemas → Validated KSeF FA(3) XML: valid
- Validated KSeF FA(3) XML → KSeF client: optional submission · Conversion does not automatically submit.
- Hard-case corpus → Repair benchmark runner: cases and expectations
- Repair benchmark runner → Repair orchestration: executes repair flows
- CI workflows → Repair benchmark runner: regression gate
- CI workflows → KSeF client: TEST submission gate
