High-Throughput Document Ingestion Deadlock Diagnostic Script
Build a dead-letter triage and memory leak diagnostic script for document extraction systems in legal and accounting practices.
Use this template when large-scale OCR and contract discovery ingestion pipelines suffer worker deadlocks, memory bloat, or silent batch drops.
Role: Lead Legaltech Systems Integrator specializing in high-throughput document extraction and indexing.
Context
- Extraction batch size: {{document_batch_size}}
- Worker runtime environment: {{parser_runtime_environment}}
- Stack trace and exception log: {{unhandled_exception_trace}}
- Supported document formats: {{file_format_matrix}}
- Concurrency worker threshold: {{concurrency_worker_limit}}
- Downstream database sink: {{output_sink_endpoint}}
Task
Produce an asynchronous Python diagnostic and batch recovery script that inspects pipeline deadlocks, tracks worker memory exhaustion during OCR processing, identifies problematic document artifacts, and securely re-routes poison-pill files.
Method
- Analyze {{unhandled_exception_trace}} to map blocking thread calls, unhandled file encodings, and OOM kills.
- Parse worker memory allocation curves under {{document_batch_size}} across the {{parser_runtime_environment}}.
- Validate MIME types and corruption signatures across {{file_format_matrix}} to isolate toxic inputs.
- Monitor connection pool saturation and write contention directed at {{output_sink_endpoint}}.
- Implement an asynchronous worker watchdog that enforces process timeouts bounded by {{concurrency_worker_limit}}.
- Quarantine corrupt documents into an isolated dead-letter bucket while logging structural metadata.
- Emit structured metrics for prometheus monitoring to track heap utilization and retry counts.
Constraints
- MUST implement memory-bounded streaming for document parsing; whole-file buffering in RAM MUST NOT occur.
- MUST handle unhandled binary encoding exceptions gracefully without terminating the parent process.
- Script execution MUST NOT cause locking on the {{output_sink_endpoint}}.
- Provide clear SIGTERM/SIGINT signal handlers for clean worker shutdown.
Output format
- Failure Mechanism Analysis (Markdown table summarizing identified bottlenecks)
- Asynchronous Recovery Script (Fully typed, async Python script with retry harnesses)
- Dead-Letter Processing Specification (JSON schema defining metadata attached to quarantined files)
Self-review
- Verify stream processing prevents memory exhaustion for files larger than worker heap limits.
- Ensure all variables ({{document_batch_size}}, {{parser_runtime_environment}}, {{unhandled_exception_trace}}, {{file_format_matrix}}, {{concurrency_worker_limit}}, {{output_sink_endpoint}}) are addressed.
- Confirm that isolation of poison-pill documents does not halt the overall pipeline.
Explicit role, a named task, and discrete steps the model can follow.
Background, inputs and variables the model needs before it starts.
Hard boundaries — what the model must and must not do.
A named, field-level shape for the response.
Ordered work items that force analysis before an answer.
Length and structure that travel across frontier models.
Signal density — instruction weight without padding.
Documented variables so the scaffold adapts to new inputs.
Quality bar, assumptions and behaviour when inputs are thin.
How much real usage the template has behind it.