"""Deterministic teaching model, not an LLM or production-system test.
No network, customer data, concurrency, durable storage, or real API calls.
"""
import json
from pathlib import Path

# Two jobs read before either reserves: a deliberately fixed interleaving.
stock = 12
readings = [stock, stock]
orders = [8, 8]
naive_promises = sum(q for q, seen in zip(orders, readings) if q <= seen)
remaining = stock
accepted = []
for q in orders:
    ok = q <= remaining  # Model check + reservation as one indivisible step.
    if ok:
        remaining -= q
    accepted.append(ok)

# The receiver has committed a shipment; only its reply is lost.
naive_shipments = []
for attempt in [1, 2]:
    naive_shipments.append({'order': 'DEMO-A', 'quantity': 8})

# A receiver contract: one operation ID + unchanged content returns same receipt.
# In production, token and effect need atomic durable storage and retention rules.
receipts = {}
shipments = []
def submit(key, payload):
    if key in receipts:
        previous, receipt = receipts[key]
        if previous != payload:
            raise ValueError('same operation ID with changed content')
        return receipt
    receipt = f'SHIP-{len(shipments) + 1}'
    shipments.append(dict(payload))
    receipts[key] = (dict(payload), receipt)
    return receipt

payload = {'order': 'DEMO-A', 'quantity': 8}
first = submit('DEMO-A/ship/v1', payload)  # Caller does not receive this response.
second = submit('DEMO-A/ship/v1', payload)
mismatch_rejected = False
try:
    submit('DEMO-A/ship/v1', {'order': 'DEMO-A', 'quantity': 9})
except ValueError:
    mismatch_rejected = True
assert naive_promises == 16 and naive_promises - stock == 4
assert accepted == [True, False] and remaining == 4
assert len(naive_shipments) == 2 and len(shipments) == 1
assert first == second and mismatch_rejected
result = {
    'kind': 'deterministic illustrative simulation; no AI/API/customer experiment',
    'inventory': {'stock': stock, 'order_quantities': orders, 'separate_reading': {'promised': naive_promises, 'shortfall': naive_promises-stock}, 'shared_reservation': {'accepted': accepted, 'reserved': 8, 'remaining': remaining, 'held_order_quantity': 8}},
    'lost_reply': {'naive_shipment_records': len(naive_shipments), 'same_operation_shipment_records': len(shipments), 'same_receipt': first == second, 'changed_content_rejected': mismatch_rejected},
    'limits': ['Fixed execution order, not a concurrency test', 'No durable transaction or crash-recovery test', 'No product-specific integration or LLM performance assessment']
}
Path(__file__).with_name('simulation-results.json').write_text(json.dumps(result, ensure_ascii=False, indent=2)+'\n')
print(json.dumps(result, ensure_ascii=False, indent=2))
