Part II — Intermediate Development · Chapter 6 · Lesson 5 of 5 · 8 min read
Chapter 6 Checkpoint: Fault-Tolerant Import Processor
Checkpoint level: Intermediate
Purpose: Integrate diagnostic classification, boundary validation, assertions, structured exception handling, recovery accounting, cleanup, debugging, and safe evidence in one resilient import workflow.
Checkpoint outcomes
By completing this checkpoint, you will demonstrate that you can:
- validate portable import records before dependent access;
- separate expected invalid data from exceptional provider failures;
- use try, catch, finally, throw, and assert deliberately;
- continue after recoverable per-record failures;
- abort on failures that invalidate the whole batch;
- maintain accounting and state invariants across every exit path;
- preserve safe diagnostics without leaking sensitive data;
- debug incorrect recovery and partial state;
- compare interpreter and VM behavior; and
- return a complete portable summary suitable for a host boundary.
Project scenario
A business application imports source-defined transaction records. Each record must be validated, normalized, enriched through a simulated provider, and converted into an accepted result or a safe failure result. Ordinary invalid records and per-record provider failures do not stop later records. A designated fatal provider condition aborts the batch after required cleanup.
The checkpoint uses no live network, file, database, credential, or process access. Provider behavior is simulated deterministically so every path is reproducible.
6C.1 Input record contract
| Field | Meaning | Validation |
|---|---|---|
| record_id | Stable import identifier | Required nonempty string |
| account_code | Target business account | Required nonempty string |
| amount | Transaction amount | Required number greater than zero |
| currency | Currency code | One of USD, EUR, or INR |
| category | Business classification | One of standard, priority, or review |
| provider_mode | Deterministic provider behavior | ok, transient_error, permanent_error, or fatal_error |
| tags | Optional metadata labels | Required list of at most ten nonempty strings |
6C.2 Validation policy
- Check the outer record shape and required keys before accessing values.
- Validate value categories before operators or normalization.
- Validate amount, approved currency, category, provider_mode, and tags.
- Collect independent field errors in deterministic order.
- Do not call the provider for an invalid record.
- Return one structured invalid result with stable reason codes.
- Do not count invalid records as attempted provider calls.
6C.3 Simulated provider policy
| Mode | Behavior | Recovery |
|---|---|---|
| ok | Returns a deterministic enrichment map | Accept the record |
| transient_error | Throws on the first call and succeeds on the second | Retry once within a bounded policy |
| permanent_error | Throws on every call | Catch per record and continue |
| fatal_error | Throws a batch-invalidating failure | Perform cleanup and abort the batch |
6C.4 Retry contract
Only transient_error may be retried. The retry policy is:
- maximum two provider attempts for the record;
- no retry for validation, permission, permanent, fatal, or assertion failures;
- the simulated operation is idempotent;
- every attempt is counted;
- cleanup runs once for the record, not once per attempt; and
- terminal status records whether retry recovered the operation.
6C.5 Record outcomes
Every inspected record before a fatal batch stop must produce exactly one outcome:
- accepted — valid and provider work succeeded;
- invalid — boundary validation failed;
- provider_failed — valid input but provider recovery was exhausted; or
- fatal — a batch-invalidating provider failure occurred.
Records after a fatal stop are not_inspected and must not receive fabricated outcomes.
6C.6 Required result shape
Return a portable summary map containing:
- batch_status;
- inspected_count;
- not_inspected_count;
- valid_input_count;
- invalid_count;
- provider_attempt_count;
- accepted_count;
- provider_failed_count;
- fatal_count;
- accepted_amount_total;
- results in source order;
- audit entries;
- safe batch diagnostics;
- invariants_ok; and
- source_preserved.
6C.7 Per-record result contract
Each inspected record result must contain:
- record_id when safely available;
- outcome;
- valid_input;
- provider_attempts;
- accepted amount or null;
- normalized currency or null;
- safe enrichment or null;
- reason_codes;
- safe message;
- source, line, and column when an exception supplies them; and
- cleanup_complete.
6C.8 Required invariants
- inspected_count + not_inspected_count = input_count.
- inspected_count = invalid_count + accepted_count + provider_failed_count + fatal_count.
- valid_input_count = accepted_count + provider_failed_count + fatal_count.
- provider_attempt_count equals the sum of attempts in inspected valid results.
- Invalid records have zero provider attempts.
- Accepted amount total equals the sum of accepted record amounts.
- Every inspected record has exactly one result and one audit entry.
- Every result has cleanup_complete equal to true.
- Fatal count is zero or one.
- If fatal_count is one, batch_status is failed and later records are not inspected.
- If fatal_count is zero, batch_status is complete.
- The source input remains equal to its preserved copy.
6C.9 Processing pipeline
- Preserve an independent copy of the input.
- Initialize batch counters, lists, totals, and status.
- Inspect one record in source order.
- Validate shape and fields.
- For invalid input, append one invalid result and continue.
- For valid input, enter a bounded provider attempt loop.
- Use try/catch to classify provider failure and decide retry or terminal outcome.
- Use finally to append one audit entry and mark cleanup exactly once.
- For fatal failure, append the fatal result and leave the batch loop.
- Calculate not-inspected count and invariants.
- Validate the portable output contract.
6C.10 Exception-handling constraints
- Keep the per-record try region as small as practical.
- Do not catch malformed source or internal assertion failures merely to continue.
- Do not treat a permanent failure as transient.
- Do not mark success in finally.
- Do not let cleanup failure hide the primary provider failure.
- Do not expose raw exception objects or sensitive input records.
- Preserve safe source context in the result.
6C.11 Assertion plan
Use assertions only for internal guarantees, including:
- provider attempts remain within the configured maximum;
- accepted outcomes have non-null enrichment;
- invalid outcomes have zero provider attempts;
- fatal outcome ends later inspection;
- record cleanup occurs once;
- accepted amount is positive; and
- batch accounting reconciles.
6C.12 Diagnostic classification
| Condition | Classification | Response |
|---|---|---|
| Missing field or invalid amount | Expected validation failure | Structured invalid result |
| Transient provider failure | Retryable provider exception | Retry once |
| Permanent provider failure | Per-record HostError | Provider-failed result; continue |
| Fatal provider failure | Batch HostError | Cleanup and abort |
| Broken accounting assertion | Internal defect | Stop; do not continue with uncertain state |
| Instruction limit exceeded | ResourceLimitError | Terminate under host policy |
6C.13 Safe evidence
Record only information needed for testing and operations:
- stable reason code;
- safe message;
- record identifier;
- attempt number;
- source location when available;
- outcome and cleanup state;
- correlation identifier if the host supplies one; and
- resource snapshot when relevant.
Do not record credentials, full sensitive payloads, arbitrary host representations, or unrestricted stack data in public output.
6C.14 Required test matrix
| Case | Input | Expected emphasis |
|---|---|---|
| A | Empty list | Complete, all counts zero |
| B | One valid ok record | Accepted in one attempt |
| C | One ordinary invalid record | Invalid, zero provider attempts |
| D | Transient provider record | Accepted on second attempt |
| E | Permanent provider error | Provider failed; later record continues |
| F | Fatal error first | Abort; all later records not inspected |
| G | Fatal error in middle | Earlier results retained; later inputs untouched |
| H | Fatal error last | All records inspected; batch failed |
| I | Mixed valid, invalid, transient, and permanent | All accounting and ordering |
| J | Exact amount and size boundaries | Inclusive and exclusive rules |
6C.15 Deliberate defect matrix
Introduce and diagnose these temporary defects:
- provider call before validation;
- retrying permanent errors;
- attempt counter updated after a possible throw;
- accepted total updated before provider success;
- catch path omits a record result;
- finally marks every item successful;
- cleanup runs once per retry instead of once per record;
- fatal error is caught and batch continues;
- not-inspected count is calculated incorrectly;
- source record is mutated during normalization; and
- invariants_ok is set directly rather than calculated.
6C.16 Debugging requirement
For at least four defects:
- create a minimal deterministic reproduction;
- state the expected state at the suspected line;
- choose an executable breakpoint;
- inspect focused variables and stack frames;
- identify the first divergent transition;
- correct the root cause;
- run focused tests; and
- run the full matrix.
At least one investigation must cover catch/finally behavior and one must cover accounting after a fatal stop.
6C.17 Interpreter and VM parity
Run deterministic valid, invalid, transient, permanent, and fatal cases through both engines where supported. Compare:
- result ordering;
- counts and totals;
- throw/catch/finally behavior;
- source-level messages that are part of the contract;
- provider attempt counts;
- fatal termination position; and
- all invariants.
Reduce and report any divergence rather than accepting one engine as automatically correct.
6C.18 Resource and security tests
- Run under a deliberately small instruction budget and confirm bounded termination.
- Use an oversized input list and verify collection policy.
- Generate excessive validation errors and confirm output remains bounded.
- Confirm no process permission is requested.
- Confirm no file or network capability is required.
- Verify diagnostics exclude sensitive payloads.
- Verify fatal cleanup does not create an external side effect.
6C.19 Acceptance criteria
- Source formatting and checking succeed.
- Boundary validation occurs before dependent access.
- Expected invalid input never becomes a provider exception.
- Only transient provider failures are retried.
- Retry attempts are bounded and counted accurately.
- Permanent per-record failures do not stop later records.
- Fatal failure stops later inspection after cleanup.
- Every inspected record has exactly one result and audit entry.
- Finally never invents successful state.
- Accepted totals include only accepted records.
- All accounting invariants are calculated and true.
- The input collection remains unchanged.
- The result contains only portable values.
- Interpreter and VM results agree for required deterministic cases.
- Diagnostics are actionable and safe.
6C.20 Submission package
- fault_tolerant_import.pea in canonical formatting;
- input and output contract tables;
- validation and recovery policy;
- prediction and result records for cases A–J;
- per-record and batch invariants;
- audit and cleanup evidence;
- deliberate defect investigations;
- interpreter/VM parity evidence;
- resource and security test evidence;
- diagnostic redaction rules; and
- a concise operational runbook.
6C.21 Assessment rubric
| Area | Evidence of mastery |
|---|---|
| Validation | Malformed input is classified safely before provider work. |
| Exception handling | Try, catch, throw, and finally express the documented policy. |
| Recovery | Retries, continuation, and fatal propagation are correct. |
| Consistency | Partial state and all accounting invariants are controlled. |
| Diagnostics | Messages, codes, locations, and redaction are useful and safe. |
| Debugging | Root causes are found through focused evidence and regression tests. |
| Parity | Interpreter and VM behavior agrees or differences are reduced. |
| Security | Capabilities, resources, output, and sensitive data remain bounded. |
Completion standard: Every required test passes, fatal and recoverable paths follow policy, every invariant holds, source data remains unchanged, and the learner can explain each provider attempt, result, cleanup action, and batch exit.
6C.22 Reflection questions
- Which failures belong to validation rather than exceptions?
- Why is only the transient provider mode retried?
- Why must attempts be counted before a possible throw?
- How does finally differ from success handling?
- What evidence proves cleanup occurs exactly once?
- Which invariant best reveals a missing result?
- How does fatal failure affect not-inspected records?
- What information should remain internal to a host log?
- Which defect was easiest to find with a breakpoint?
- What would change when a real provider replaces the simulation?
Chapter 6 summary
You can now classify failures, write actionable diagnostics, validate boundaries, state assertions and invariants, recover with structured exception handling, clean up reliably, and debug with source and protocol tools. The fault-tolerant import processor combines these practices into a bounded workflow with explicit record outcomes, fatal-stop semantics, portable results, and verifiable accounting.
Transition to Chapter 7
Chapter 7 introduces optional typing and semantic analysis. The next lesson covers variable, parameter, and return annotations and explains how stronger contracts complement—rather than replace—runtime validation.
Checkpoint status: Ready for review.