import json
import subprocess
from ops import R, REPO, p

def run(args):
    result=subprocess.run(args,capture_output=True,text=True,check=True,timeout=30)
    return result.stdout

tracked=run(['git','diff','--name-only']).splitlines()
untracked=run(['git','ls-files','--others','--exclude-standard']).splitlines()
assert not run(['git','diff','--cached','--name-only']).strip()
paths=sorted(set(tracked+untracked))
allowed=set(__import__('ops').FILES)|{
 'docs/profiles/ltb1021/topics/runtime-lifecycle/README.ko.md',
 'docs/profiles/ltb1021/topics/runtime-lifecycle/decision/20260916T124403+0900-decision-pose-batch.md',
 'docs/profiles/ltb1021/topics/runtime-lifecycle/runs/20260916T124403+0900-run-pose-batch.md',
 'docs/profiles/ltb1021/topics/runtime-lifecycle/runs/20260916T130958+0900-run-pose-batch-runtime.md'}
assert set(paths)==allowed,(set(paths)-allowed,allowed-set(paths))
args=['codex-git-closeout','prepare','--repository',str(REPO),'--output',str(R/'manifest2.json'),
      '--job-id','contextra-pose-batch-20260916-final','--profile','ltb1021','--session-alias','work-session',
      '--title','refactor(runtime): Pose batch 경로 준비와 제한 시험 결과 기록',
      '--message','Why: 다채널 처리 저하에서 불필요한 준비 비용을 줄이고 Pose batch 수용 범위를 확인한다.',
      '--message','What: pool4·추론용mux576·제외Frame 직렬화 생략·경량 generation fence·collector 최소 제거를 구현했다.',
      '--message','Impact: Pose4채널1.70~1.85FPS미달. Medium설정복원뒤에도저하해기존image/model/env로복원했고3채널9.69~9.88FPS참고치로회복했다. 새source/image/engine은후보이며품질은사용자평가대기다.',
      '--message','Verify: source/image 각67tests PASS. batch4 shape/finite PASS,실제Tritonbatch1~3관측. 8/12/16채널미실행, decodedqueue2보류.',
      '--message','Rollback: source exact diff 및private image/config/model CAS 원장으로부분복원. 기존engine과image보존. 무관한서비스변경없음.',
      '--verify','Source/image67 targeted tests each PASS; git diff --check PASS; four-channel below target; original channels restored.']
for path in paths:args+=['--path',path]
for index in range(1,len(args)):
    if args[index-1]=='--message':args[index]=args[index].replace(': ','=',1)
print(run(args))
manifest=json.loads((R/'manifest2.json').read_text())
print(run(['codex-git-closeout','batch-prepare','--output',str(R/'batch2.json'),'--batch-id','contextra-pose-batch-20260916-final',
    '--profile','ltb1021','--session-alias','work-session','--expected-group-count','1','--repository-label','contextra-analytics',
    '--manifest',str(R/'manifest2.json'),'--expect-plan-hash',manifest['plan_hash']]))
batch=json.loads((R/'batch2.json').read_text())
print(run(['codex-git-closeout','batch-plan','--batch-manifest',str(R/'batch2.json'),'--expect-batch-plan-hash',batch['plan_hash']]))
print(run(['codex-git-closeout','batch-present','--batch-manifest',str(R/'batch2.json'),'--expect-batch-plan-hash',batch['plan_hash']]))
