# A duplicate-event consumer with a durable intent

This fixed authored exercise uses Python3 and its standard-library SQLite module. Save `exercise.py`, then run:

```sh
python3 exercise.py
```

It creates temporary databases, runs one consumer contract against three identity choices, interrupts two owned child processes, reopens their databases, checks the retained records and removes the temporary directory. Expected wrong variants are reported as rejected controls. Any unexpected condition exits nonzero. No HTTP server, network call, product import, provider integration, payment, email or other external action runs. This is not a service for ingesting your own webhook traffic.

## What the authored events mean

Logical event `evt_A` requests an inventory delta of +3 for `item_demo_001`. Two delivery attempts carry that same event. Event `evt_B` requests +2 for the same item and must also be accepted. The independent expected intent projection is +5 for that source/account/item. The same textual `evt_A` under another account or source produces its own +3 intent.

The envelope contains `source`, `account_id`, `delivery_id` and a JSON-text `body`. The body is a closed versioned schema: `payload_version: 1`, `event_id`, `event_type: "inventory.adjusted"`, `resource_id` and signed SQLite-integer `delta_units`. Boolean deltas, unknown body fields, invalid versions and missing/empty envelope fields are rejected. Source and account are synthetic fixture provenance here. A real handler must establish its trusted source/account scope; accepting those labels from an untrusted body is not authentication.

The declared semantic comparison includes every accepted body field and excludes per-attempt envelope metadata. Parsed property order and whitespace are intentionally irrelevant; ambiguous duplicate JSON properties are rejected. This is an exercise policy for a small schema, not general JSON canonicalization, signature verification or a rule for arbitrary provider payloads. Signature checks concern the producer’s actual byte/authentication contract.

## The three key choices

All variants use the same parser, conflict policy, transaction and oracle. Only their unique deduplication key changes:

- **Correct:** source + account + logical event ID. A repeated matching event returns duplicate without a second intent.
- **Wrong attempt-ID key:** source + account + delivery-attempt ID. The second delivery gets a new key and duplicates the intent. The first scope projects +8 instead of +5.
- **Wrong resource-ID key:** source + account + item ID. A different event for the same item collides and is refused, losing legitimate work. The first scope projects +3 instead of +5.

The oracle compares full persisted intent records, projected units, handling results and paired inbox/outbox records. Its expected records are declared independently of the consumer’s key-selection logic. A green overall result requires the identical oracle to accept the correct variant and reject both wrong variants.

## Store acceptance and intent together

The SQLite inbox uniquely stores the selected scoped key and accepted semantic body. One explicit transaction checks existing acceptance or inserts the inbox row and its uniquely associated outbox-intent row, then commits. A matching duplicate adds nothing. The same key with changed semantic fields returns conflict, preserves the first accepted body and inserts no new intent. The exercise makes that conflict decision visible rather than silently ignoring every duplicate key.

The outbox row is **intent to apply an inventory delta later**. The exercise only projects those intents for assertions; it does not adjust an external inventory system. A real dispatcher still needs its delivery/retry and acknowledgement policy, and its receiver may need its own deduplication boundary. Committing an intent does not establish an external effect or end-to-end exactly-once processing.

## Two real process interruptions

The driver uses this same file in an internal child mode; failpoint exits affect only its owned child. Public use remains the one fixed command above.

1. **Before commit:** the child exits after inserting the inbox row, before inserting the outbox row. Reopening the same database must show neither acceptance nor intent. A new attempt stores one pair; another repeat is duplicate.
2. **After commit / before local acknowledgement:** the child exits only after commit returns successfully, before returning the consumer result. Reopening must show both rows. Retrying returns duplicate and preserves the one original intent.

The script reports effective rollback-journal settings (`journal_mode=delete`, `synchronous=2` meaning FULL, `foreign_keys=1`) and its Python/SQLite versions. Recovery mechanisms stay enabled and all files remain together until the checks finish. These are local sequential process-interruption observations. They do not certify power-loss survival, every filesystem/configuration, concurrent workers, an HTTP acknowledgement or a crash during commit. If commit outcome is unknown, reopen and resolve acceptance by the logical key rather than assuming success or rollback.

Successful output names the correct key, both rejected wrong keys, semantic conflict/reordered JSON, closed-schema controls and both restart points, followed by `PASS local inbox/outbox-intent exercise`. It reports no model score or product qualification. Use these distinctions to design assertions at your own trusted admission, persistence and effect boundaries.
