FAILURE MAP
← Case archive

FA-60296 / Inventory cost layering / Open access

Periodic weighted average with vendor returns: return unit removal · case 01

The average cost falls after a vendor return because units stay in the pool while value leaves.

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

ROOT CAUSE

Returned units are not subtracted from pool units.

THE FAILURE

Returned units are not subtracted from pool units.

Unsuccessful approach: Decrementing units once per return line instead of by the returned quantity only works for single-unit returns.

Case contract

Input {begin:[qty,value], purchases:[[qty,value]], vendor_returns:[[purchase_index,qty]], ending_qty}. Each vendor return removes qty units and floor(value*qty/purchase_qty) value of that purchase. The pool is beginning plus purchases less returns; ending value = round-half-up(ending_qty*pool value/pool units); COGS = pool value - ending value. ending_qty outside [0, pool units] returns {"error"}. Return {units, pool, 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):
    units = x['begin'][0] + sum(q for q, v in x['purchases'])
    pool = x['begin'][1] + sum(v for q, v in x['purchases'])
    for i, rq in x['vendor_returns']:
        pq, pv = x['purchases'][i]
        pool -= pv * rq // pq
    e = x['ending_qty']
    if e < 0 or e > units:
        return {'error': 'invalid count'}
    ending = (2 * e * pool + units) // (2 * units) if units else 0
    return {'units': units, 'pool': pool, 'ending_value': ending, 'cogs': pool - ending}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['basic', {'begin': [10, 1000], 'purchases': [[20, 2600], [10, 1501]], 'vendor_returns': [[0, 5]], 'ending_qty': 12}, {'units': 35, 'pool': 4451, 'ending_value': 1526, 'cogs': 2925}], ['no returns', {'begin': [4, 480], 'purchases': [[6, 781]], 'vendor_returns': [], 'ending_qty': 3}, {'units': 10, 'pool': 1261, 'ending_value': 378, 'cogs': 883}], ['return second', {'begin': [0, 0], 'purchases': [[3, 100], [7, 777]], 'vendor_returns': [[1, 3]], 'ending_qty': 5}, {'units': 7, 'pool': 544, 'ending_value': 389, 'cogs': 155}], ['too many counted', {'begin': [2, 200], 'purchases': [[5, 600]], 'vendor_returns': [[0, 2]], 'ending_qty': 7}, {'error': 'invalid count'}], ['empty count', {'begin': [3, 330], 'purchases': [[3, 390]], 'vendor_returns': [[0, 1]], 'ending_qty': 0}, {'units': 5, 'pool': 590, 'ending_value': 0, 'cogs': 590}], ['odd costs', {'begin': [7, 701], 'purchases': [[3, 337], [11, 1410]], 'vendor_returns': [[1, 4], [0, 1]], 'ending_qty': 9}, {'units': 16, 'pool': 1824, 'ending_value': 1026, 'cogs': 798}], ['pricey begin', {'begin': [5, 2501], 'purchases': [[5, 500]], 'vendor_returns': [], 'ending_qty': 4}, {'units': 10, 'pool': 3001, 'ending_value': 1200, 'cogs': 1801}]], [['basic', {'begin': [10, 1000], 'purchases': [[20, 2600], [10, 1502]], 'vendor_returns': [[0, 5]], 'ending_qty': 12}, {'units': 35, 'pool': 4452, 'ending_value': 1526, 'cogs': 2926}], ['no returns', {'begin': [4, 480], 'purchases': [[6, 782]], 'vendor_returns': [], 'ending_qty': 3}, {'units': 10, 'pool': 1262, 'ending_value': 379, 'cogs': 883}], ['return second', {'begin': [0, 0], 'purchases': [[3, 100], [7, 777]], 'vendor_returns': [[1, 2]], 'ending_qty': 5}, {'units': 8, 'pool': 655, 'ending_value': 409, 'cogs': 246}], ['too many counted', {'begin': [2, 200], 'purchases': [[5, 600]], 'vendor_returns': [[0, 2]], 'ending_qty': 6}, {'error': 'invalid count'}], ['empty count', {'begin': [3, 330], 'purchases': [[3, 390]], 'vendor_returns': [[0, 1]], 'ending_qty': 0}, {'units': 5, 'pool': 590, 'ending_value': 0, 'cogs': 590}], ['odd costs', {'begin': [7, 701], 'purchases': [[3, 337], [11, 1411]], 'vendor_returns': [[1, 4], [0, 1]], 'ending_qty': 9}, {'units': 16, 'pool': 1824, 'ending_value': 1026, 'cogs': 798}], ['pricey begin', {'begin': [5, 2502], 'purchases': [[5, 500]], 'vendor_returns': [], 'ending_qty': 4}, {'units': 10, 'pool': 3002, 'ending_value': 1201, 'cogs': 1801}]], [['basic', {'begin': [10, 1000], 'purchases': [[20, 2600], [10, 1503]], 'vendor_returns': [[0, 5]], 'ending_qty': 12}, {'units': 35, 'pool': 4453, 'ending_value': 1527, 'cogs': 2926}], ['no returns', {'begin': [4, 480], 'purchases': [[6, 783]], 'vendor_returns': [], 'ending_qty': 3}, {'units': 10, 'pool': 1263, 'ending_value': 379, 'cogs': 884}], ['return second', {'begin': [0, 0], 'purchases': [[3, 100], [7, 777]], 'vendor_returns': [[1, 3]], 'ending_qty': 5}, {'units': 7, 'pool': 544, 'ending_value': 389, 'cogs': 155}], ['too many counted', {'begin': [2, 200], 'purchases': [[5, 600]], 'vendor_returns': [[0, 2]], 'ending_qty': 7}, {'error': 'invalid count'}], ['empty count', {'begin': [3, 330], 'purchases': [[3, 390]], 'vendor_returns': [[0, 1]], 'ending_qty': 0}, {'units': 5, 'pool': 590, 'ending_value': 0, 'cogs': 590}], ['odd costs', {'begin': [7, 701], 'purchases': [[3, 337], [11, 1412]], 'vendor_returns': [[1, 4], [0, 1]], 'ending_qty': 9}, {'units': 16, 'pool': 1825, 'ending_value': 1027, 'cogs': 798}], ['pricey begin', {'begin': [5, 2503], 'purchases': [[5, 500]], 'vendor_returns': [], 'ending_qty': 4}, {'units': 10, 'pool': 3003, 'ending_value': 1201, 'cogs': 1802}]], [['basic', {'begin': [10, 1000], 'purchases': [[20, 2600], [10, 1504]], 'vendor_returns': [[0, 5]], 'ending_qty': 12}, {'units': 35, 'pool': 4454, 'ending_value': 1527, 'cogs': 2927}], ['no returns', {'begin': [4, 480], 'purchases': [[6, 784]], 'vendor_returns': [], 'ending_qty': 3}, {'units': 10, 'pool': 1264, 'ending_value': 379, 'cogs': 885}], ['return second', {'begin': [0, 0], 'purchases': [[3, 100], [7, 777]], 'vendor_returns': [[1, 2]], 'ending_qty': 5}, {'units': 8, 'pool': 655, 'ending_value': 409, 'cogs': 246}], ['too many counted', {'begin': [2, 200], 'purchases': [[5, 600]], 'vendor_returns': [[0, 2]], 'ending_qty': 6}, {'error': 'invalid count'}], ['empty count', {'begin': [3, 330], 'purchases': [[3, 390]], 'vendor_returns': [[0, 1]], 'ending_qty': 0}, {'units': 5, 'pool': 590, 'ending_value': 0, 'cogs': 590}], ['odd costs', {'begin': [7, 701], 'purchases': [[3, 337], [11, 1413]], 'vendor_returns': [[1, 4], [0, 1]], 'ending_qty': 9}, {'units': 16, 'pool': 1826, 'ending_value': 1027, 'cogs': 799}], ['pricey begin', {'begin': [5, 2504], 'purchases': [[5, 500]], 'vendor_returns': [], 'ending_qty': 4}, {'units': 10, 'pool': 3004, 'ending_value': 1202, 'cogs': 1802}]], [['basic', {'begin': [10, 1000], 'purchases': [[20, 2600], [10, 1505]], 'vendor_returns': [[0, 5]], 'ending_qty': 12}, {'units': 35, 'pool': 4455, 'ending_value': 1527, 'cogs': 2928}], ['no returns', {'begin': [4, 480], 'purchases': [[6, 785]], 'vendor_returns': [], 'ending_qty': 3}, {'units': 10, 'pool': 1265, 'ending_value': 380, 'cogs': 885}], ['return second', {'begin': [0, 0], 'purchases': [[3, 100], [7, 777]], 'vendor_returns': [[1, 3]], 'ending_qty': 5}, {'units': 7, 'pool': 544, 'ending_value': 389, 'cogs': 155}], ['too many counted', {'begin': [2, 200], 'purchases': [[5, 600]], 'vendor_returns': [[0, 2]], 'ending_qty': 7}, {'error': 'invalid count'}], ['empty count', {'begin': [3, 330], 'purchases': [[3, 390]], 'vendor_returns': [[0, 1]], 'ending_qty': 0}, {'units': 5, 'pool': 590, 'ending_value': 0, 'cogs': 590}], ['odd costs', {'begin': [7, 701], 'purchases': [[3, 337], [11, 1414]], 'vendor_returns': [[1, 4], [0, 1]], 'ending_qty': 9}, {'units': 16, 'pool': 1826, 'ending_value': 1027, 'cogs': 799}], ['pricey begin', {'begin': [5, 2505], 'purchases': [[5, 500]], 'vendor_returns': [], 'ending_qty': 4}, {'units': 10, 'pool': 3005, 'ending_value': 1202, 'cogs': 1803}]]]
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
basic{'cogs': 3116, 'ending_value': 1335, 'pool': 4451, 'units': 40}{'cogs': 2925, 'ending_value': 1526, 'pool': 4451, 'units': 35}Failed
no returns{'cogs': 883, 'ending_value': 378, 'pool': 1261, 'units': 10}{'cogs': 883, 'ending_value': 378, 'pool': 1261, 'units': 10}Passed
return second{'cogs': 272, 'ending_value': 272, 'pool': 544, 'units': 10}{'cogs': 155, 'ending_value': 389, 'pool': 544, 'units': 7}Failed
too many counted{'cogs': 0, 'ending_value': 560, 'pool': 560, 'units': 7}{'error': 'invalid count'}Failed
empty count{'cogs': 590, 'ending_value': 0, 'pool': 590, 'units': 6}{'cogs': 590, 'ending_value': 0, 'pool': 590, 'units': 5}Failed
odd costs{'cogs': 1042, 'ending_value': 782, 'pool': 1824, 'units': 21}{'cogs': 798, 'ending_value': 1026, 'pool': 1824, 'units': 16}Failed
pricey begin{'cogs': 1801, 'ending_value': 1200, 'pool': 3001, 'units': 10}{'cogs': 1801, 'ending_value': 1200, 'pool': 3001, 'units': 10}Passed

SHA-256 / 8aa5131b3f3dc5d1e613eff1ddb6536f4db6ad6e31fe955543bf4065bdf5a310

2 / The unsuccessful fix

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

N = 1
observations = []
def solve(x):
    units = x['begin'][0] + sum(q for q, v in x['purchases'])
    pool = x['begin'][1] + sum(v for q, v in x['purchases'])
    for i, rq in x['vendor_returns']:
        pq, pv = x['purchases'][i]
        pool -= pv * rq // pq
        units -= 1
    e = x['ending_qty']
    if e < 0 or e > units:
        return {'error': 'invalid count'}
    ending = (2 * e * pool + units) // (2 * units) if units else 0
    return {'units': units, 'pool': pool, 'ending_value': ending, 'cogs': pool - ending}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['basic', {'begin': [10, 1000], 'purchases': [[20, 2600], [10, 1501]], 'vendor_returns': [[0, 5]], 'ending_qty': 12}, {'units': 35, 'pool': 4451, 'ending_value': 1526, 'cogs': 2925}], ['no returns', {'begin': [4, 480], 'purchases': [[6, 781]], 'vendor_returns': [], 'ending_qty': 3}, {'units': 10, 'pool': 1261, 'ending_value': 378, 'cogs': 883}], ['return second', {'begin': [0, 0], 'purchases': [[3, 100], [7, 777]], 'vendor_returns': [[1, 3]], 'ending_qty': 5}, {'units': 7, 'pool': 544, 'ending_value': 389, 'cogs': 155}], ['too many counted', {'begin': [2, 200], 'purchases': [[5, 600]], 'vendor_returns': [[0, 2]], 'ending_qty': 7}, {'error': 'invalid count'}], ['empty count', {'begin': [3, 330], 'purchases': [[3, 390]], 'vendor_returns': [[0, 1]], 'ending_qty': 0}, {'units': 5, 'pool': 590, 'ending_value': 0, 'cogs': 590}], ['odd costs', {'begin': [7, 701], 'purchases': [[3, 337], [11, 1410]], 'vendor_returns': [[1, 4], [0, 1]], 'ending_qty': 9}, {'units': 16, 'pool': 1824, 'ending_value': 1026, 'cogs': 798}], ['pricey begin', {'begin': [5, 2501], 'purchases': [[5, 500]], 'vendor_returns': [], 'ending_qty': 4}, {'units': 10, 'pool': 3001, 'ending_value': 1200, 'cogs': 1801}]], [['basic', {'begin': [10, 1000], 'purchases': [[20, 2600], [10, 1502]], 'vendor_returns': [[0, 5]], 'ending_qty': 12}, {'units': 35, 'pool': 4452, 'ending_value': 1526, 'cogs': 2926}], ['no returns', {'begin': [4, 480], 'purchases': [[6, 782]], 'vendor_returns': [], 'ending_qty': 3}, {'units': 10, 'pool': 1262, 'ending_value': 379, 'cogs': 883}], ['return second', {'begin': [0, 0], 'purchases': [[3, 100], [7, 777]], 'vendor_returns': [[1, 2]], 'ending_qty': 5}, {'units': 8, 'pool': 655, 'ending_value': 409, 'cogs': 246}], ['too many counted', {'begin': [2, 200], 'purchases': [[5, 600]], 'vendor_returns': [[0, 2]], 'ending_qty': 6}, {'error': 'invalid count'}], ['empty count', {'begin': [3, 330], 'purchases': [[3, 390]], 'vendor_returns': [[0, 1]], 'ending_qty': 0}, {'units': 5, 'pool': 590, 'ending_value': 0, 'cogs': 590}], ['odd costs', {'begin': [7, 701], 'purchases': [[3, 337], [11, 1411]], 'vendor_returns': [[1, 4], [0, 1]], 'ending_qty': 9}, {'units': 16, 'pool': 1824, 'ending_value': 1026, 'cogs': 798}], ['pricey begin', {'begin': [5, 2502], 'purchases': [[5, 500]], 'vendor_returns': [], 'ending_qty': 4}, {'units': 10, 'pool': 3002, 'ending_value': 1201, 'cogs': 1801}]], [['basic', {'begin': [10, 1000], 'purchases': [[20, 2600], [10, 1503]], 'vendor_returns': [[0, 5]], 'ending_qty': 12}, {'units': 35, 'pool': 4453, 'ending_value': 1527, 'cogs': 2926}], ['no returns', {'begin': [4, 480], 'purchases': [[6, 783]], 'vendor_returns': [], 'ending_qty': 3}, {'units': 10, 'pool': 1263, 'ending_value': 379, 'cogs': 884}], ['return second', {'begin': [0, 0], 'purchases': [[3, 100], [7, 777]], 'vendor_returns': [[1, 3]], 'ending_qty': 5}, {'units': 7, 'pool': 544, 'ending_value': 389, 'cogs': 155}], ['too many counted', {'begin': [2, 200], 'purchases': [[5, 600]], 'vendor_returns': [[0, 2]], 'ending_qty': 7}, {'error': 'invalid count'}], ['empty count', {'begin': [3, 330], 'purchases': [[3, 390]], 'vendor_returns': [[0, 1]], 'ending_qty': 0}, {'units': 5, 'pool': 590, 'ending_value': 0, 'cogs': 590}], ['odd costs', {'begin': [7, 701], 'purchases': [[3, 337], [11, 1412]], 'vendor_returns': [[1, 4], [0, 1]], 'ending_qty': 9}, {'units': 16, 'pool': 1825, 'ending_value': 1027, 'cogs': 798}], ['pricey begin', {'begin': [5, 2503], 'purchases': [[5, 500]], 'vendor_returns': [], 'ending_qty': 4}, {'units': 10, 'pool': 3003, 'ending_value': 1201, 'cogs': 1802}]], [['basic', {'begin': [10, 1000], 'purchases': [[20, 2600], [10, 1504]], 'vendor_returns': [[0, 5]], 'ending_qty': 12}, {'units': 35, 'pool': 4454, 'ending_value': 1527, 'cogs': 2927}], ['no returns', {'begin': [4, 480], 'purchases': [[6, 784]], 'vendor_returns': [], 'ending_qty': 3}, {'units': 10, 'pool': 1264, 'ending_value': 379, 'cogs': 885}], ['return second', {'begin': [0, 0], 'purchases': [[3, 100], [7, 777]], 'vendor_returns': [[1, 2]], 'ending_qty': 5}, {'units': 8, 'pool': 655, 'ending_value': 409, 'cogs': 246}], ['too many counted', {'begin': [2, 200], 'purchases': [[5, 600]], 'vendor_returns': [[0, 2]], 'ending_qty': 6}, {'error': 'invalid count'}], ['empty count', {'begin': [3, 330], 'purchases': [[3, 390]], 'vendor_returns': [[0, 1]], 'ending_qty': 0}, {'units': 5, 'pool': 590, 'ending_value': 0, 'cogs': 590}], ['odd costs', {'begin': [7, 701], 'purchases': [[3, 337], [11, 1413]], 'vendor_returns': [[1, 4], [0, 1]], 'ending_qty': 9}, {'units': 16, 'pool': 1826, 'ending_value': 1027, 'cogs': 799}], ['pricey begin', {'begin': [5, 2504], 'purchases': [[5, 500]], 'vendor_returns': [], 'ending_qty': 4}, {'units': 10, 'pool': 3004, 'ending_value': 1202, 'cogs': 1802}]], [['basic', {'begin': [10, 1000], 'purchases': [[20, 2600], [10, 1505]], 'vendor_returns': [[0, 5]], 'ending_qty': 12}, {'units': 35, 'pool': 4455, 'ending_value': 1527, 'cogs': 2928}], ['no returns', {'begin': [4, 480], 'purchases': [[6, 785]], 'vendor_returns': [], 'ending_qty': 3}, {'units': 10, 'pool': 1265, 'ending_value': 380, 'cogs': 885}], ['return second', {'begin': [0, 0], 'purchases': [[3, 100], [7, 777]], 'vendor_returns': [[1, 3]], 'ending_qty': 5}, {'units': 7, 'pool': 544, 'ending_value': 389, 'cogs': 155}], ['too many counted', {'begin': [2, 200], 'purchases': [[5, 600]], 'vendor_returns': [[0, 2]], 'ending_qty': 7}, {'error': 'invalid count'}], ['empty count', {'begin': [3, 330], 'purchases': [[3, 390]], 'vendor_returns': [[0, 1]], 'ending_qty': 0}, {'units': 5, 'pool': 590, 'ending_value': 0, 'cogs': 590}], ['odd costs', {'begin': [7, 701], 'purchases': [[3, 337], [11, 1414]], 'vendor_returns': [[1, 4], [0, 1]], 'ending_qty': 9}, {'units': 16, 'pool': 1826, 'ending_value': 1027, 'cogs': 799}], ['pricey begin', {'begin': [5, 2505], 'purchases': [[5, 500]], 'vendor_returns': [], 'ending_qty': 4}, {'units': 10, 'pool': 3005, 'ending_value': 1202, 'cogs': 1803}]]]
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
basic{'cogs': 3081, 'ending_value': 1370, 'pool': 4451, 'units': 39}{'cogs': 2925, 'ending_value': 1526, 'pool': 4451, 'units': 35}Failed
no returns{'cogs': 883, 'ending_value': 378, 'pool': 1261, 'units': 10}{'cogs': 883, 'ending_value': 378, 'pool': 1261, 'units': 10}Passed
return second{'cogs': 242, 'ending_value': 302, 'pool': 544, 'units': 9}{'cogs': 155, 'ending_value': 389, 'pool': 544, 'units': 7}Failed
too many counted{'error': 'invalid count'}{'error': 'invalid count'}Passed
empty count{'cogs': 590, 'ending_value': 0, 'pool': 590, 'units': 5}{'cogs': 590, 'ending_value': 0, 'pool': 590, 'units': 5}Passed
odd costs{'cogs': 960, 'ending_value': 864, 'pool': 1824, 'units': 19}{'cogs': 798, 'ending_value': 1026, 'pool': 1824, 'units': 16}Failed
pricey begin{'cogs': 1801, 'ending_value': 1200, 'pool': 3001, 'units': 10}{'cogs': 1801, 'ending_value': 1200, 'pool': 3001, 'units': 10}Passed

SHA-256 / 2e0a83281538b18e20c0e702dbfbeaf6ecf71e973c4d7fe80e94a3ae8ededd01

HELD IN THE MEMBER ARCHIVE

The verified repair and its recorded checks are member-only.

This mechanism has 7 recorded checks per implementation. The open-access tier publishes the failure and the unsuccessful fix; the repaired source that passes every check, and the observations that prove it, are available to members.

Every case sharing this mechanism uses the same contract and the same repair, so this one record is held back for all of them.

Member access is invitation-based. Sign in with your invited account to inspect the repair.

Sign in to the archive ↗

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:44.362974+00:00.

Case digest / e8f4d77e543c1534794725649f923c7192e0d3954474d8df82f9c35a8ca72886