import datetime,json,os,subprocess
from pathlib import Path
os.umask(0o077)
HERE=Path(__file__).parent
REPO=Path('/workspace/contextra-analytics-runtime-lifecycle-refactor')
JOB='20260918-contextra-event-http-r01'
ROOT=Path('/tmp/codex/ltb1021/git-closeout')/JOB;ROOT.mkdir(mode=0o700)
def cmd(a):
    p=subprocess.run(a,text=True,capture_output=True,timeout=60)
    assert p.returncode==0,p.stdout+p.stderr
    return p.stdout
paths=cmd(['git','-C',str(REPO),'diff','--name-only']).splitlines()+cmd(['git','-C',str(REPO),'ls-files','--others','--exclude-standard']).splitlines()
assert len(paths)==8 and not cmd(['git','-C',str(REPO),'diff','--cached','--name-only']).strip()
cmd(['git','-C',str(REPO),'diff','--check'])
receipt=dict(schema_version='ltb1021.codex_hub.report_receipt.v1',receipt_id=JOB,
 timestamp=datetime.datetime.now(datetime.timezone(datetime.timedelta(hours=9))).isoformat(),
 session_alias='work-session',task='Contextra Event 전용 목적지 분리 및 반영',
 results=['사용자 승인 HTTP Event 주소를 별도 설정으로 분리하고 Sidecar만 반영했다.',
          'Analytics/Redis 및 채널 state/PID/run, HTTPS 인증/제어 경계를 보존했다.'],
 verify=['표적11개PASS, Sidecar health/auth readiness PASS. 수동 Event POST 없음.'],
 commits=[],remaining=['실제 관제 Event 수신은 별도 확인'])
receipt_path=HERE/'report-receipt.json'
with receipt_path.open('x') as f:json.dump(receipt,f,ensure_ascii=False,indent=2)
print(cmd(['codex-report-receipt','--profile','ltb1021','--session-alias','work-session','--receipt',str(receipt_path)]))
mf=ROOT/'manifest.json'
args=['codex-git-closeout','prepare','--repository',str(REPO),'--output',str(mf),'--job-id',JOB,
 '--profile','ltb1021','--session-alias','work-session','--title','fix(secure-bridge): Event 전용 전송 주소 분리',
 '--message','Why=사용자가 요청한 HTTP Event 주소만 변경하고 기존 HTTPS 제어와 Metadata 경로를 유지한다.',
 '--message','What=동일 관제 host의 Event endpoint override와 검증·테스트·Compose 연결을 추가하고 Sidecar만 반영했다.',
 '--message','Impact=Analytics/Redis 및 모든 채널 PID/run은 유지했다. 로그인/WSS/Artifact URL과 원장 재시도 정책은 변경하지 않았다.',
 '--message','Verify=표적11개PASS, Sidecar health/auth readiness PASS, 채널/공유서비스 보존 확인. 실제 Event 접수는 미확인이다.',
 '--message','Rollback=자동 rollback 없음. 기존 설정과 새 활성 image/endpoint를 private 원장에 보존했다.',
 '--verify','11 targeted tests PASS; Sidecar health/auth readiness PASS; Analytics/Redis/channel state preserved',
 '--verify','git diff --check PASS; no manual or historical Event replay','--expires-in-seconds','86400']
for p in sorted(paths):args+=['--path',p]
print(cmd(args))
batch=ROOT/'batch.json'
print(cmd(['codex-git-closeout','batch-prepare','--output',str(batch),'--batch-id',JOB,'--profile','ltb1021',
 '--session-alias','work-session','--expected-group-count','1','--repository-label','contextra-analytics',
 '--manifest',str(mf),'--expect-plan-hash',json.loads(mf.read_text())['plan_hash'],'--expires-in-seconds','86400']))
hash=json.loads(batch.read_text())['plan_hash']
print(cmd(['codex-git-closeout','batch-plan','--batch-manifest',str(batch),'--expect-batch-plan-hash',hash]))
print(cmd(['codex-git-closeout','batch-present','--batch-manifest',str(batch),'--expect-batch-plan-hash',hash]))
