# Build CHANGELOG.md for the scope commit: replace [Unreleased]..[0.68.0) with the canonical block; assert everything else byte-identical.
import io, sys, hashlib, subprocess
src_spec, block_path, out_path, member295 = sys.argv[1:5]
old = subprocess.run(['git','cat-file','-p',src_spec], capture_output=True, check=True).stdout
block = io.open(block_path,'rb').read()
assert old.count(b'\r')==0 and block.count(b'\r')==0
i = old.find(b'## [Unreleased]\n'); j = old.find(b'## [0.68.0]'); assert 0 < i < j
assert block.startswith(b'## [Unreleased]\n') and block.endswith(b'\n\n')
new = old[:i] + block + old[j:]
assert new[:i] == old[:i] and new[j - (j-i) + len(block):] == old[j:], "outside region changed"
assert hashlib.sha256(new[new.find(b'## [0.68.0]'):]).hexdigest()[:16] == '406dd3cdd2a37844'
u = new[i:new.find(b'## [0.68.0]')]
fixed = u[u.find(b'### Fixed'):u.find(b'### Internal')]; internal = u[u.find(b'### Internal'):]
assert b'<!-- [doc->REQ-RESIDENT-WEB-SKEW-DIAGNOSIS] -->\n- Service status and successful update completion' in fixed
assert b'<!-- [doc->REQ-CONN-HEALTHY-LIFECYCLE-BOUNDED] -->\n- Daemon logs now summarize' in internal
assert fixed.count(b'\n- ') == 8, fixed.count(b'\n- ')
assert internal.count(b'\n- ') == 1
m = subprocess.run(['git','cat-file','-p',member295+':CHANGELOG.md'], capture_output=True, check=True).stdout
mu = m[m.find(b'### Fixed')+len(b'### Fixed'):m.find(b'## [0.68.0]')].strip()
assert mu in fixed, "#295 member entry not verbatim"
assert u.count(b'[doc->') == 2
io.open(out_path,'wb').write(new)
print("OK bytes", len(old), "->", len(new), "fixed bullets 8, internal 1, tags 2, #295 verbatim, [0.68.0] 406dd3cdd2a37844")
