How komaksym/alfredotheclerk works
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: repair_orchestration.py Repair orchestration · domain_shell.py Invoice shell · invoice_text_field_extraction.py Field extraction · domestic_vat_shell_validation.py Shell validation
Components 23
Conversion pipeline
- Native-text PDF · Supported Polish VAT invoice PDF.
- PDF parsing
src/input_processing/parse_pdf.py - Field extraction · Extracts invoice values and evidence candidates.
src/input_processing/invoice_text_field_extraction.py - Extraction diagnostics
src/input_processing/extraction_diagnostics.py - Invoice shell · Incomplete structured invoice and cross-stage contract.
src/invoice_gen/domain_shell.py - Shell population
src/input_processing/populate_shell.py - Shell validation · Finds missing, inconsistent, and invalid fields.
src/invoice_gen/domestic_vat_shell_validation.py
Repair and review
- Repair orchestration · Routes eligible defects and requires repair or review outcomes.
src/agentic_repair/repair_orchestration.py - LLM repair decision · Uses evidence candidates only for repair-required fields.
src/agentic_repair/agent_extraction_repair.py - Atomic repair kernel · Applies approved field repairs without inventing ambiguous values.
src/agentic_repair/repair_kernel.py - Human review workflow · Collects corrections for unresolved fields.
src/agentic_repair/human_review.py - Local review app · Serves source PDF, audit changes, and correction controls on port 8000.
src/review_ui/app.py - Review session · Isolates presentation and session state from repair mechanics.
src/review_ui/session.py
Correctness and output
- Invoice correctness gate · Applies shell and VAT/totals checks before XML is available.
src/invoice_gen/invoice_correctness.py - FA(3) mapping
src/invoice_gen/domestic_vat_faktura_mapping.py - FA(3) XML rendering
src/invoice_gen/domestic_vat_xml_rendering.py - Bundled FA(3) schemas · Validates generated XML.
src/invoice_gen/schemas/schemat.xsd - Validated KSeF FA(3) XML · Core conversion output.
External boundaries
- DeepSeek API
src/agentic_repair/config.py - KSeF client · Separate optional boundary for cryptography, transport, and submission.
src/ksef/submission.py
Benchmark and CI
- Repair benchmark runner · Executes and scores controlled repair flows.
src/agentic_repair/benchmark_runner.py - Hard-case corpus · PDFs, expected XML, shell states, and comparison policies.
- CI workflows · Includes a secret-backed KSeF TEST submission gate.
.github/workflows/ci.yml
Connections 26
- 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