Files
stack/agents/codex/work/queue-333/r1-repair/build-static-packet.py
jason.woltje c012b2e0a3 docs(row333): R1 repair packet, receipts and author log appends (#1831)
Commits agents/codex/work/queue-333/r1-repair/ (packet, receipts, review and author files) and the append-only BUILD-LOG.md and docs/SESSIONS.md entries. The packet INDEX.json sha256 is 3eb9b8d10445aa328be3b854385e4792dda2c5712b19d443d485d1b00a18d0f8 and source-tests.patch is a78bc385801b64be270fb9d6e84672c443738a5fce8eb68f7d2a01359a57a770; the product commit for these bytes is 2627321f. Two packet tarballs are deliberately not committed: packet/baseline-input-bytes.tar.gz and packet/fixed-input-bytes.tar.gz. The secret check reported a -----BEGIN marker in each (members packages/bus/src/credentials.mjs and packages/discord/src/tools.mjs, byte-identical to 503b91e56 and with no PEM body). They stay untracked until the lead rules on them.
2026-11-02 13:37:52 -06:00

158 lines
11 KiB
Python

#!/usr/bin/env python3
"""Static bytes/Git/diff only. Never imports product modules or runs tests."""
from pathlib import Path
import hashlib, json, subprocess, re, io, tarfile, gzip, tempfile, shutil
ROOT=Path.cwd()
OUT=ROOT/'agents/codex/work/queue-333/r1-repair/packet'
OUT.mkdir(exist_ok=True)
HEAD='503b91e56137482abef475bc3b1b72a64b0a329c'
assert subprocess.check_output(['git','rev-parse','HEAD'],text=True).strip()==HEAD
assert subprocess.check_output(['git','diff','--cached','--name-only'],text=True).strip()==''
OBJECT_FORMAT=subprocess.check_output(['git','rev-parse','--show-object-format'],text=True).strip()
assert OBJECT_FORMAT=='sha1'
def digest(data): return hashlib.sha256(data).hexdigest()
def blob(data): return hashlib.sha1(b'blob '+str(len(data)).encode()+b'\0'+data).hexdigest()
def head_bytes(p):
result=subprocess.run(['git','show',f'{HEAD}:{p}'],capture_output=True)
return result.stdout if result.returncode==0 else None
def jsonfile(name,value): (OUT/name).write_text(json.dumps(value,indent=2,sort_keys=True)+'\n')
tracked=set(subprocess.check_output(['git','diff','--name-only',HEAD,'--','packages'],text=True).splitlines())
untracked=set(subprocess.check_output(['git','ls-files','--others','--exclude-standard','--','packages'],text=True).splitlines())
changed=sorted(tracked|untracked)
CONTROLS=['packages/bus/tests/p5-identity.test.mjs','packages/bus/tests/p5-key-compatibility.test.mjs','packages/bus/tests/p5-background.test.mjs','packages/bus/tests/p5-telemetry-fixture.mjs']
assert set(CONTROLS)<=untracked
parts={}
for p in changed:
if p in tracked:
part=subprocess.check_output(['git','diff','--binary','--full-index',HEAD,'--',p])
else:
result=subprocess.run(['git','diff','--no-index','--binary','--full-index','--','/dev/null',p],capture_output=True)
assert result.returncode==1,(p,result.stderr)
part=result.stdout
assert part
parts[p]=part
(OUT/'source-tests.patch').write_bytes(b''.join(parts[p] for p in changed))
(OUT/'baseline-controls.patch').write_bytes(b''.join(parts[p] for p in sorted(CONTROLS)))
(OUT/'baseline-to-fixed.patch').write_bytes(b''.join(parts[p] for p in changed if p not in CONTROLS))
jsonfile('changed-source-tests.json',[{'path':p,'bytes':(ROOT/p).stat().st_size,'sha256':digest((ROOT/p).read_bytes()),'git_blob':blob((ROOT/p).read_bytes()),'head_git_blob':blob(head_bytes(p)) if head_bytes(p) is not None else None} for p in changed])
whole=(ROOT/'agents/codex/work/queue-333/r1-repair/whole-files.txt').read_text().splitlines()
assert len(whole)==28 and len(set(whole))==28
(OUT/'whole-files.txt').write_text('\n'.join(whole)+'\n')
# Conservative static closure: old captured inputs + current controls/changed
# files + all shipped selectable plug-ins/role data, literal module and URL edges.
seed={line.split(' ',1)[1] for line in (ROOT/'agents/codex/work/queue-333/source-inputs.sha256').read_text().splitlines()}
seed.update(whole);seed.update(changed)
seed.update(str(p.relative_to(ROOT)) for p in (ROOT/'agents/codex/work/queue-333/r1-repair').glob('*') if p.is_file())
seed.update(str(p.relative_to(ROOT)) for p in (ROOT/'packages/plugins').glob('*/*/index.mjs'))
seed.update(str(p.relative_to(ROOT)) for p in (ROOT/'packages/plugins/src').glob('*.mjs'))
seed.update(str(p.relative_to(ROOT)) for p in (ROOT/'roles').rglob('*') if p.is_file())
for p in ['AGENTS.md','package.json','package-lock.json','RELEASE','docs/plans/2026-10-24_model-proxy-rows.md','docs/plans/2026-10-24_stack-model-proxy.md']:
if (ROOT/p).is_file(): seed.add(p)
patterns=[('module',re.compile(r'''\b(?:import|export)\s+(?:[^;'\"]*?\s+from\s*)?['\"]([^'\"]+)['\"]''')),
('dynamic-literal',re.compile(r'''\bimport\s*\(\s*['\"]([^'\"]+)['\"]''')),
('url-data',re.compile(r'''new\s+URL\s*\(\s*['\"]([^'\"]+)['\"]'''))]
inputs=set();pending=list(seed);edges=[];external=[];selectors=[]
while pending:
p=pending.pop()
if p in inputs: continue
file=ROOT/p
if not file.is_file(): continue
inputs.add(p)
if file.suffix not in {'.mjs','.js','.cjs'}:continue
src=file.read_text()
if re.search(r'\bimport\s*\(\s*(?![\'\"\s])',src) or 'pathToFileURL(' in src:
selectors.append(p)
for kind,pattern in patterns:
for spec in pattern.findall(src):
if not spec.startswith('.'):
if kind!='url-data': external.append({'from':p,'specifier':spec,'kind':kind})
continue
resolved=(file.parent/spec).resolve()
try: rel=str(resolved.relative_to(ROOT))
except ValueError: continue
if resolved.is_file():
edges.append({'from':p,'to':rel,'specifier':spec,'kind':kind});pending.append(rel)
parent=file.parent
while parent==ROOT or ROOT in parent.parents:
package=parent/'package.json'
if package.is_file(): pending.append(str(package.relative_to(ROOT)));break
parent=parent.parent
fixed={p:(ROOT/p).read_bytes() for p in sorted(inputs)}
baseline={}
for p in sorted(inputs):
before=fixed[p] if p in CONTROLS else head_bytes(p)
if before is not None:baseline[p]=before
def manifest(values):
return [{'path':p,'bytes':len(data),'sha256':digest(data),'git_blob':blob(data),'head_git_blob':blob(head_bytes(p)) if head_bytes(p) is not None else None} for p,data in values.items()]
jsonfile('fixed-inputs.json',manifest(fixed));jsonfile('baseline-inputs.json',manifest(baseline))
(OUT/'fixed-inputs.sha256').write_text(''.join(f'{digest(data)} {p}\n' for p,data in fixed.items()))
(OUT/'baseline-inputs.sha256').write_text(''.join(f'{digest(data)} {p}\n' for p,data in baseline.items()))
jsonfile('static-dependencies.json',{'provenance':'STATIC conservative input closure, NOT execution/consumed-source evidence','roots':whole,'literal_edges':sorted(edges,key=lambda e:(e['from'],e['to'],e['kind'])),'external_or_builtin_specifiers':sorted(external,key=lambda e:(e['from'],e['specifier'])),'nonliteral_selectors_require_actual_runner_capture':sorted(set(selectors)),'included_shipped_plugins_and_roles':True})
def archive(name,values):
with open(OUT/name,'wb') as raw:
with gzip.GzipFile(filename='',mode='wb',fileobj=raw,mtime=0) as zipped:
with tarfile.open(fileobj=zipped,mode='w') as tar:
for p,data in values.items():
info=tarfile.TarInfo(p);info.size=len(data);info.mode=0o644;info.mtime=0;info.uid=info.gid=0
tar.addfile(info,io.BytesIO(data))
archive('fixed-input-bytes.tar.gz',fixed);archive('baseline-input-bytes.tar.gz',baseline)
# Verify archived bytes match both manifests; hashes/Git blob IDs are byte pins,
# never claims that unexecuted modules or tests passed.
for name,values in [('fixed-input-bytes.tar.gz',fixed),('baseline-input-bytes.tar.gz',baseline)]:
with tarfile.open(OUT/name,'r:gz') as tar:
assert set(tar.getnames())==set(values)
for p,data in values.items(): assert tar.extractfile(p).read()==data
# Original evidence must remain identical to the original Git candidate.
original=ROOT/'agents/codex/work/queue-333'
old_files=subprocess.check_output(['git','ls-tree','-r','--name-only',HEAD,'--',str(original.relative_to(ROOT))],text=True).splitlines()
for p in old_files:
assert (ROOT/p).read_bytes()==head_bytes(p),f'original evidence changed: {p}'
# Base test prefixes are retained except the two explicitly authorized additive
# registry inventory changes which already existed at503 and remain unchanged.
base='09a847edaa25b609bac7728ff83f224708a29399'
prefixes=[]
for p in changed:
if '/tests/' not in p:continue
before=subprocess.run(['git','show',f'{base}:{p}'],capture_output=True)
if before.returncode==0:
retained=(ROOT/p).read_bytes().startswith(before.stdout)
assert retained,p
prefixes.append(p)
jsonfile('preserved-base-test-prefixes.json',prefixes)
# Static application check in disposable byte trees, never a product run or a
# second Git worktree/branch. Verify both overlay routes reproduce exact bytes.
tmp=Path(tempfile.mkdtemp(prefix='p5-static-overlay-'))
try:
archive_bytes=subprocess.check_output(['git','archive','--format=tar',HEAD,'--',*sorted(tracked)])
direct=tmp/'direct';staged=tmp/'staged';direct.mkdir();staged.mkdir()
for target in [direct,staged]:
with tarfile.open(fileobj=io.BytesIO(archive_bytes),mode='r:') as tar:tar.extractall(target,filter='data')
def apply(target,name):
subprocess.run(['git','apply','--check',str(OUT/name)],cwd=target,check=True,capture_output=True)
subprocess.run(['git','apply',str(OUT/name)],cwd=target,check=True,capture_output=True)
apply(direct,'source-tests.patch')
apply(staged,'baseline-controls.patch')
for path in tracked:assert (staged/path).read_bytes()==head_bytes(path)
apply(staged,'baseline-to-fixed.patch')
for path in changed:
assert (direct/path).read_bytes()==(ROOT/path).read_bytes()
assert (staged/path).read_bytes()==(ROOT/path).read_bytes()
jsonfile('static-overlay-check.json',{'status':'byte-application-verified-no-product-execution','source_patch_equals_controls_then_remainder':True,'changed_package_files':len(changed),'temporary_trees_removed':True})
finally:shutil.rmtree(tmp)
assert not tmp.exists()
review=ROOT/'agents/codex/work/queue-333/r1-repair/REVIEW-R1.md'
assert digest(review.read_bytes())=='7c95672a30c0d5c39189c7c7d610ada89cd539dcb9413617071fbb81200fd008'
evidence={str(p.relative_to(ROOT)):p.read_bytes() for p in sorted((ROOT/'agents/codex/work/queue-333/r1-repair/receipts').iterdir()) if p.is_file()}
evidence.update({str(review.relative_to(ROOT)):review.read_bytes()})
for p in sorted((ROOT/'agents/codex/work/queue-333/r1-repair/baselines').iterdir()):
if p.is_file():evidence[str(p.relative_to(ROOT))]=p.read_bytes()
jsonfile('r1-raw-evidence.json',manifest(evidence))
archive('r1-raw-evidence-bytes.tar.gz',evidence)
index={'status':'UNCOMMITTED_STATIC_PACKET_EXECUTION_PENDING','head':HEAD,'original_base':base,'review_sha256':'7c95672a30c0d5c39189c7c7d610ada89cd539dcb9413617071fbb81200fd008','changed_package_files':len(changed),'whole_test_files':len(whole),'fixed_static_input_files':len(fixed),'baseline_static_input_files':len(baseline),'git_object_format':OBJECT_FORMAT,'files':{p.name:{'bytes':p.stat().st_size,'sha256':digest(p.read_bytes())} for p in sorted(OUT.iterdir()) if p.is_file() and p.name not in {'INDEX.json','packet.sha256'}}}
jsonfile('INDEX.json',index)
(OUT/'packet.sha256').write_text(''.join(f'{digest(p.read_bytes())} {p.name}\n' for p in sorted(OUT.iterdir()) if p.is_file() and p.name!='packet.sha256'))
print(json.dumps({k:index[k] for k in ['status','head','changed_package_files','whole_test_files','fixed_static_input_files','baseline_static_input_files']},sort_keys=True))
print('source-tests.patch SHA256 '+digest((OUT/'source-tests.patch').read_bytes()))
print('INDEX.json SHA256 '+digest((OUT/'INDEX.json').read_bytes()))