FAILURE MAP
← Case archive

FA-59871 / Inventory cost layering / Open access

Periodic LIFO year-end layering: increment layer pricing order · case 01

The year-end LIFO increment is valued at the latest purchase prices, overstating ending inventory in inflationary years.

Verified by executionVariant 1 · 9 checks per implementationDownload source bundle ↓JSON ↗

ROOT CAUSE

The increment walks the purchase list from the end of the year instead of the beginning.

VERIFIED REPAIR

Price the increment layer from the earliest purchases of the year forward.

Unsuccessful approach: Pricing the increment from the cheapest purchases is an unrelated rule and differs whenever prices are not monotonic.

Case contract

Input {begin:[[qty,unit]] oldest first, purchases:[[qty,unit]] chronological, ending_qty}. Ending inventory keeps the oldest units: if ending_qty covers the base, all base layers stay and one increment layer is priced at the EARLIEST purchases of the year; otherwise the newest base layers are liquidated. ending_qty outside [0, available] returns {"error"}. Return {layers:[[qty,value]], ending_value, cogs}.

Why this case matters

Inventory valuation and cost-of-goods decisions depend on this rule.

1 / The failure

Exit 1
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    begin = [list(l) for l in x['begin']]
    purchases = x['purchases']
    end_q = x['ending_qty']
    base_q = sum(l[0] for l in begin)
    buy_q = sum(p[0] for p in purchases)
    avail = base_q + buy_q
    if end_q < 0 or end_q > avail:
        return {'error': 'invalid ending quantity'}
    layers = []
    if end_q >= base_q:
        layers = [[q, q * c] for q, c in begin]
        inc = end_q - base_q
        new_q = 0
        new_v = 0
        for q, c in purchases[::-1]:
            if inc == 0:
                break
            take = min(inc, q)
            new_q += take
            new_v += take * c
            inc -= take
        if new_q > 0:
            layers.append([new_q, new_v])
    else:
        rem = end_q
        for bq, bc in begin:
            take = min(rem, bq)
            if take > 0:
                layers.append([take, take * bc])
            rem -= take
    begin_value = sum(q * c for q, c in begin)
    buy_value = sum(q * c for q, c in purchases)
    ending_value = sum(v for q, v in layers)
    return {'layers': layers, 'ending_value': ending_value, 'cogs': begin_value + buy_value - ending_value}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 10]], 'ending_qty': 18}, {'layers': [[10, 50], [4, 24], [4, 28]], 'ending_value': 102, 'cogs': 94}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 1}, {'layers': [[1, 2]], 'ending_value': 2, 'cogs': 50}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 6}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[3, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[1, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 35}], ['non monotonic prices', {'begin': [[1, 1]], 'purchases': [[2, 9], [2, 4], [2, 20]], 'ending_qty': 3}, {'layers': [[1, 1], [2, 18]], 'ending_value': 19, 'cogs': 48}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 11]], 'ending_qty': 19}, {'layers': [[10, 50], [4, 24], [5, 35]], 'ending_value': 109, 'cogs': 95}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 11}, {'layers': [[10, 50], [1, 6]], 'ending_value': 56, 'cogs': 60}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 2}, {'layers': [[2, 4]], 'ending_value': 4, 'cogs': 48}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 7}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -2}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[4, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[2, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 46}], ['non monotonic prices', {'begin': [[1, 1]], 'purchases': [[2, 9], [2, 5], [2, 20]], 'ending_qty': 3}, {'layers': [[1, 1], [2, 18]], 'ending_value': 19, 'cogs': 50}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 12]], 'ending_qty': 17}, {'layers': [[10, 50], [4, 24], [3, 21]], 'ending_value': 95, 'cogs': 117}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 3}, {'layers': [[3, 6]], 'ending_value': 6, 'cogs': 46}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 8}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -3}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[5, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[3, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 57}], ['non monotonic prices', {'begin': [[1, 1]], 'purchases': [[2, 9], [2, 6], [2, 20]], 'ending_qty': 3}, {'layers': [[1, 1], [2, 18]], 'ending_value': 19, 'cogs': 52}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 13]], 'ending_qty': 18}, {'layers': [[10, 50], [4, 24], [4, 28]], 'ending_value': 102, 'cogs': 118}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 11}, {'layers': [[10, 50], [1, 6]], 'ending_value': 56, 'cogs': 60}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 4}, {'layers': [[3, 6], [1, 4]], 'ending_value': 10, 'cogs': 42}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 9}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -4}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[6, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[4, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 68}], ['non monotonic prices', {'begin': [[1, 1]], 'purchases': [[2, 9], [2, 7], [2, 20]], 'ending_qty': 3}, {'layers': [[1, 1], [2, 18]], 'ending_value': 19, 'cogs': 54}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 14]], 'ending_qty': 19}, {'layers': [[10, 50], [4, 24], [5, 35]], 'ending_value': 109, 'cogs': 119}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 5}, {'layers': [[3, 6], [2, 8]], 'ending_value': 14, 'cogs': 38}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 10}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -5}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[7, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[5, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 79}], ['non monotonic prices', {'begin': [[1, 1]], 'purchases': [[2, 9], [2, 8], [2, 20]], 'ending_qty': 3}, {'layers': [[1, 1], [2, 18]], 'ending_value': 19, 'cogs': 56}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
Boundary fixtureActualExpectedOutcome
increment layer{'cogs': 82, 'ending_value': 114, 'layers': [[10, 50], [4, 24], [4, 40]]}{'cogs': 94, 'ending_value': 102, 'layers': [[10, 50], [4, 24], [4, 28]]}Failed
liquidation{'cogs': 66, 'ending_value': 50, 'layers': [[10, 50]]}{'cogs': 66, 'ending_value': 50, 'layers': [[10, 50]]}Passed
deep liquidation{'cogs': 50, 'ending_value': 2, 'layers': [[1, 2]]}{'cogs': 50, 'ending_value': 2, 'layers': [[1, 2]]}Passed
over available{'error': 'invalid ending quantity'}{'error': 'invalid ending quantity'}Passed
negative ending{'error': 'invalid ending quantity'}{'error': 'invalid ending quantity'}Passed
count of minus one{'error': 'invalid ending quantity'}{'error': 'invalid ending quantity'}Passed
all sold{'cogs': 35, 'ending_value': 0, 'layers': []}{'cogs': 35, 'ending_value': 0, 'layers': []}Passed
exact base{'cogs': 35, 'ending_value': 40, 'layers': [[4, 40]]}{'cogs': 35, 'ending_value': 40, 'layers': [[4, 40]]}Passed
non monotonic prices{'cogs': 26, 'ending_value': 41, 'layers': [[1, 1], [2, 40]]}{'cogs': 48, 'ending_value': 19, 'layers': [[1, 1], [2, 18]]}Failed

SHA-256 / c1a3011f19f4c5b67e4018991e6083906959d1558774f105b5920897e7989b0c

2 / The unsuccessful fix

Exit 1
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    begin = [list(l) for l in x['begin']]
    purchases = x['purchases']
    end_q = x['ending_qty']
    base_q = sum(l[0] for l in begin)
    buy_q = sum(p[0] for p in purchases)
    avail = base_q + buy_q
    if end_q < 0 or end_q > avail:
        return {'error': 'invalid ending quantity'}
    layers = []
    if end_q >= base_q:
        layers = [[q, q * c] for q, c in begin]
        inc = end_q - base_q
        new_q = 0
        new_v = 0
        for q, c in sorted(purchases, key=lambda p: p[1]):
            if inc == 0:
                break
            take = min(inc, q)
            new_q += take
            new_v += take * c
            inc -= take
        if new_q > 0:
            layers.append([new_q, new_v])
    else:
        rem = end_q
        for bq, bc in begin:
            take = min(rem, bq)
            if take > 0:
                layers.append([take, take * bc])
            rem -= take
    begin_value = sum(q * c for q, c in begin)
    buy_value = sum(q * c for q, c in purchases)
    ending_value = sum(v for q, v in layers)
    return {'layers': layers, 'ending_value': ending_value, 'cogs': begin_value + buy_value - ending_value}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 10]], 'ending_qty': 18}, {'layers': [[10, 50], [4, 24], [4, 28]], 'ending_value': 102, 'cogs': 94}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 1}, {'layers': [[1, 2]], 'ending_value': 2, 'cogs': 50}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 6}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[3, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[1, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 35}], ['non monotonic prices', {'begin': [[1, 1]], 'purchases': [[2, 9], [2, 4], [2, 20]], 'ending_qty': 3}, {'layers': [[1, 1], [2, 18]], 'ending_value': 19, 'cogs': 48}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 11]], 'ending_qty': 19}, {'layers': [[10, 50], [4, 24], [5, 35]], 'ending_value': 109, 'cogs': 95}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 11}, {'layers': [[10, 50], [1, 6]], 'ending_value': 56, 'cogs': 60}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 2}, {'layers': [[2, 4]], 'ending_value': 4, 'cogs': 48}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 7}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -2}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[4, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[2, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 46}], ['non monotonic prices', {'begin': [[1, 1]], 'purchases': [[2, 9], [2, 5], [2, 20]], 'ending_qty': 3}, {'layers': [[1, 1], [2, 18]], 'ending_value': 19, 'cogs': 50}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 12]], 'ending_qty': 17}, {'layers': [[10, 50], [4, 24], [3, 21]], 'ending_value': 95, 'cogs': 117}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 3}, {'layers': [[3, 6]], 'ending_value': 6, 'cogs': 46}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 8}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -3}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[5, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[3, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 57}], ['non monotonic prices', {'begin': [[1, 1]], 'purchases': [[2, 9], [2, 6], [2, 20]], 'ending_qty': 3}, {'layers': [[1, 1], [2, 18]], 'ending_value': 19, 'cogs': 52}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 13]], 'ending_qty': 18}, {'layers': [[10, 50], [4, 24], [4, 28]], 'ending_value': 102, 'cogs': 118}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 11}, {'layers': [[10, 50], [1, 6]], 'ending_value': 56, 'cogs': 60}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 4}, {'layers': [[3, 6], [1, 4]], 'ending_value': 10, 'cogs': 42}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 9}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -4}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[6, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[4, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 68}], ['non monotonic prices', {'begin': [[1, 1]], 'purchases': [[2, 9], [2, 7], [2, 20]], 'ending_qty': 3}, {'layers': [[1, 1], [2, 18]], 'ending_value': 19, 'cogs': 54}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 14]], 'ending_qty': 19}, {'layers': [[10, 50], [4, 24], [5, 35]], 'ending_value': 109, 'cogs': 119}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 5}, {'layers': [[3, 6], [2, 8]], 'ending_value': 14, 'cogs': 38}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 10}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -5}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[7, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[5, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 79}], ['non monotonic prices', {'begin': [[1, 1]], 'purchases': [[2, 9], [2, 8], [2, 20]], 'ending_qty': 3}, {'layers': [[1, 1], [2, 18]], 'ending_value': 19, 'cogs': 56}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
Boundary fixtureActualExpectedOutcome
increment layer{'cogs': 94, 'ending_value': 102, 'layers': [[10, 50], [4, 24], [4, 28]]}{'cogs': 94, 'ending_value': 102, 'layers': [[10, 50], [4, 24], [4, 28]]}Passed
liquidation{'cogs': 66, 'ending_value': 50, 'layers': [[10, 50]]}{'cogs': 66, 'ending_value': 50, 'layers': [[10, 50]]}Passed
deep liquidation{'cogs': 50, 'ending_value': 2, 'layers': [[1, 2]]}{'cogs': 50, 'ending_value': 2, 'layers': [[1, 2]]}Passed
over available{'error': 'invalid ending quantity'}{'error': 'invalid ending quantity'}Passed
negative ending{'error': 'invalid ending quantity'}{'error': 'invalid ending quantity'}Passed
count of minus one{'error': 'invalid ending quantity'}{'error': 'invalid ending quantity'}Passed
all sold{'cogs': 35, 'ending_value': 0, 'layers': []}{'cogs': 35, 'ending_value': 0, 'layers': []}Passed
exact base{'cogs': 35, 'ending_value': 40, 'layers': [[4, 40]]}{'cogs': 35, 'ending_value': 40, 'layers': [[4, 40]]}Passed
non monotonic prices{'cogs': 58, 'ending_value': 9, 'layers': [[1, 1], [2, 8]]}{'cogs': 48, 'ending_value': 19, 'layers': [[1, 1], [2, 18]]}Failed

SHA-256 / 31bbae853a5036027c1bcd759e77c318c2793bc28951d2d2883bccc7321ffb05

3 / The verified repair

Exit 0
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    begin = [list(l) for l in x['begin']]
    purchases = x['purchases']
    end_q = x['ending_qty']
    base_q = sum(l[0] for l in begin)
    buy_q = sum(p[0] for p in purchases)
    avail = base_q + buy_q
    if end_q < 0 or end_q > avail:
        return {'error': 'invalid ending quantity'}
    layers = []
    if end_q >= base_q:
        layers = [[q, q * c] for q, c in begin]
        inc = end_q - base_q
        new_q = 0
        new_v = 0
        for q, c in purchases:
            if inc == 0:
                break
            take = min(inc, q)
            new_q += take
            new_v += take * c
            inc -= take
        if new_q > 0:
            layers.append([new_q, new_v])
    else:
        rem = end_q
        for bq, bc in begin:
            take = min(rem, bq)
            if take > 0:
                layers.append([take, take * bc])
            rem -= take
    begin_value = sum(q * c for q, c in begin)
    buy_value = sum(q * c for q, c in purchases)
    ending_value = sum(v for q, v in layers)
    return {'layers': layers, 'ending_value': ending_value, 'cogs': begin_value + buy_value - ending_value}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 10]], 'ending_qty': 18}, {'layers': [[10, 50], [4, 24], [4, 28]], 'ending_value': 102, 'cogs': 94}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 1}, {'layers': [[1, 2]], 'ending_value': 2, 'cogs': 50}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 6}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[3, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[1, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 35}], ['non monotonic prices', {'begin': [[1, 1]], 'purchases': [[2, 9], [2, 4], [2, 20]], 'ending_qty': 3}, {'layers': [[1, 1], [2, 18]], 'ending_value': 19, 'cogs': 48}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 11]], 'ending_qty': 19}, {'layers': [[10, 50], [4, 24], [5, 35]], 'ending_value': 109, 'cogs': 95}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 11}, {'layers': [[10, 50], [1, 6]], 'ending_value': 56, 'cogs': 60}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 2}, {'layers': [[2, 4]], 'ending_value': 4, 'cogs': 48}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 7}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -2}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[4, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[2, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 46}], ['non monotonic prices', {'begin': [[1, 1]], 'purchases': [[2, 9], [2, 5], [2, 20]], 'ending_qty': 3}, {'layers': [[1, 1], [2, 18]], 'ending_value': 19, 'cogs': 50}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 12]], 'ending_qty': 17}, {'layers': [[10, 50], [4, 24], [3, 21]], 'ending_value': 95, 'cogs': 117}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 3}, {'layers': [[3, 6]], 'ending_value': 6, 'cogs': 46}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 8}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -3}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[5, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[3, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 57}], ['non monotonic prices', {'begin': [[1, 1]], 'purchases': [[2, 9], [2, 6], [2, 20]], 'ending_qty': 3}, {'layers': [[1, 1], [2, 18]], 'ending_value': 19, 'cogs': 52}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 13]], 'ending_qty': 18}, {'layers': [[10, 50], [4, 24], [4, 28]], 'ending_value': 102, 'cogs': 118}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 11}, {'layers': [[10, 50], [1, 6]], 'ending_value': 56, 'cogs': 60}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 4}, {'layers': [[3, 6], [1, 4]], 'ending_value': 10, 'cogs': 42}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 9}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -4}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[6, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[4, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 68}], ['non monotonic prices', {'begin': [[1, 1]], 'purchases': [[2, 9], [2, 7], [2, 20]], 'ending_qty': 3}, {'layers': [[1, 1], [2, 18]], 'ending_value': 19, 'cogs': 54}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 14]], 'ending_qty': 19}, {'layers': [[10, 50], [4, 24], [5, 35]], 'ending_value': 109, 'cogs': 119}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 5}, {'layers': [[3, 6], [2, 8]], 'ending_value': 14, 'cogs': 38}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 10}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -5}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[7, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[5, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 79}], ['non monotonic prices', {'begin': [[1, 1]], 'purchases': [[2, 9], [2, 8], [2, 20]], 'ending_qty': 3}, {'layers': [[1, 1], [2, 18]], 'ending_value': 19, 'cogs': 56}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
Boundary fixtureActualExpectedOutcome
increment layer{'cogs': 94, 'ending_value': 102, 'layers': [[10, 50], [4, 24], [4, 28]]}{'cogs': 94, 'ending_value': 102, 'layers': [[10, 50], [4, 24], [4, 28]]}Passed
liquidation{'cogs': 66, 'ending_value': 50, 'layers': [[10, 50]]}{'cogs': 66, 'ending_value': 50, 'layers': [[10, 50]]}Passed
deep liquidation{'cogs': 50, 'ending_value': 2, 'layers': [[1, 2]]}{'cogs': 50, 'ending_value': 2, 'layers': [[1, 2]]}Passed
over available{'error': 'invalid ending quantity'}{'error': 'invalid ending quantity'}Passed
negative ending{'error': 'invalid ending quantity'}{'error': 'invalid ending quantity'}Passed
count of minus one{'error': 'invalid ending quantity'}{'error': 'invalid ending quantity'}Passed
all sold{'cogs': 35, 'ending_value': 0, 'layers': []}{'cogs': 35, 'ending_value': 0, 'layers': []}Passed
exact base{'cogs': 35, 'ending_value': 40, 'layers': [[4, 40]]}{'cogs': 35, 'ending_value': 40, 'layers': [[4, 40]]}Passed
non monotonic prices{'cogs': 48, 'ending_value': 19, 'layers': [[1, 1], [2, 18]]}{'cogs': 48, 'ending_value': 19, 'layers': [[1, 1], [2, 18]]}Passed

SHA-256 / eaa0566af407327b6f17f271e11b49eee69883c5fe3d68f9c2057e13c497c43b

Verification & scope

Stipulated bounded teaching model with explicit toy rules; not an accounting-standard implementation. This reproducer isolates one failure mechanism. Results cover the supplied fixtures. Variants within a family share a test contract and should remain grouped when constructing evaluation splits. Related mechanisms with a shared evaluation_group must also remain together; these controlled models are not independent production incidents.

Observations recorded using Python 3.12.14 at 2026-09-29T14:46:40.303197+00:00.

Case digest / 986520c66343960140ab564d9f244a87d7086016e19e96906acefaa7c70d966d