affaan-m/ECC · error · ClaimError

completion contradicts the existing receipt

Error message

completion contradicts the existing receipt

What it means

_finish raises this when the claim row already reads 'delivered' but the existing receipt does not match the completion being replayed: no receipt row exists, the coordinate differs, or the receipt kind is not 'draft_sent'. Identical duplicate completions are idempotent no-ops; any mismatch is treated as contradictory evidence and refused.

Solutions

  1. Reuse the exact original coordinate string when replaying a completion after a timeout or crash.
  2. Check the existing receipt first (SELECT coordinate, kind FROM obligation_deliveries WHERE obligation_id=? AND decision_id=?) and compare before finishing.
  3. If the second coordinate is genuinely a different delivery, do not finish the same claim — the claim is already delivered; handle the extra delivery at the business level.
  4. Restore/remove any hand-edited receipt rows so the stored receipt reflects the real delivery.

Example fix

// before: retry builds a fresh coordinate
retry: claims.complete(db, token, str(new_message_id), now=ts2)  # ClaimError

// after: persist and replay the same coordinate
original_coordinate = str(first_send.id)
try:
    claims.complete(db, token, original_coordinate, now=ts1)
except TimeoutError:
    claims.complete(db, token, original_coordinate, now=ts2)  # idempotent no-op
Defensive patterns

Strategy: validation

Validate before calling

receipt = db.execute("SELECT coordinate, kind FROM obligation_deliveries WHERE obligation_id=? AND decision_id=?", (oid, did)).fetchone()
if receipt and (receipt['coordinate'] != coordinate or receipt['kind'] != 'draft_sent'):
    raise ValueError("retry coordinate differs from recorded receipt")

Try / catch

try:
    claims.complete(db, token, coordinate, now=ts)
except claims.ClaimError:
    log.error("completion contradicts recorded receipt; investigate before retrying")

Prevention

When it happens

Trigger: Calling complete()/reconcile() twice for the same token with a different coordinate; the receipt row was deleted or written by another writer with a different kind; passing a typo'd or alternate coordinate on retry.

Common situations: Retry logic that regenerates the coordinate (e.g. a new message ID per attempt) instead of reusing the original; two workers finishing the same claim with different delivery artifacts; manual DB edits to obligation_deliveries.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of affaan-m/ECC@8321021c54 (2026-09-16). Data as JSON: /api/errors/73eab5d2ee4e83b8. Report an issue: GitHub.

Appendix: source

Thrown at skills/operator-approval-loop/references/approval_claims.py:139

        if row['state'] == 'unknown':
            return
        if row['state'] != 'dispatching':
            raise ClaimError('only a dispatched attempt can become unknown')
        db.execute("UPDATE obligation_delivery_claims SET state='unknown',updated_ts=? WHERE token=?",
                   (now, token))


def _finish(db, token, coordinate, now, evidence):
    if not isinstance(coordinate, str) or not coordinate.strip():
        raise ClaimError('a confirmed nonempty coordinate is required')
    with _transaction(db, now):
        row = _claim_row(db, token)
        receipt = db.execute('''SELECT * FROM obligation_deliveries
            WHERE obligation_id=? AND decision_id=?''',
                             (row['obligation_id'], row['decision_id'])).fetchone()
        if row['state'] == 'delivered':
            if receipt is None or receipt['coordinate'] != coordinate or receipt['kind'] != 'draft_sent':
                raise ClaimError('completion contradicts the existing receipt')
            return False
        expected_state = 'dispatching' if evidence is None else 'unknown'
        if row['state'] != expected_state:
            raise ClaimError('completion requires the correct dispatch/reconciliation state')
        _snapshot(db, row['obligation_id'], row['decision_id'])
        db.execute('''INSERT INTO obligation_deliveries
            (obligation_id,decision_id,kind,coordinate,delivered_ts) VALUES (?,?,'draft_sent',?,?)''',
                   (row['obligation_id'], row['decision_id'], coordinate, now))
        db.execute('''UPDATE obligation_delivery_claims
            SET state='delivered',updated_ts=?,reconciliation_evidence=? WHERE token=?''',
                   (now, evidence, token))
        changed = db.execute("UPDATE obligations SET status='sent' WHERE id=? AND status='approved'",
                             (row['obligation_id'],)).rowcount
        if changed != 1:
            raise ClaimError('obligation completion failed')
    return True

View on GitHub (pinned to 8321021c54)