import hashlib
import json
import os
from pathlib import Path
import re
import subprocess

os.umask(0o077)
R = Path(__file__).parent
repo = Path('/workspace/contextra-analytics-runtime-lifecycle-refactor')
paths = ['docs/profiles/ltb1021/topics/runtime-lifecycle/README.ko.md',
         'docs/profiles/ltb1021/topics/runtime-lifecycle/runs/20260911T164550+0900-run-metadata-lan-auth-preflight.md']
def git(*args):
    return subprocess.check_output(['git', '-C', str(repo), *args], text=True).strip()
assert git('rev-parse', 'HEAD') == '1edaa70f3aff83f8f75876126b91968252a82e3d'
assert not git('diff', '--cached', '--name-only')
assert set(git('diff', '--name-only').splitlines()) | set(git('ls-files', '--others', '--exclude-standard').splitlines()) == set(paths)
for path in paths:
    content = (repo/path).read_text()
    for sensitive in ('192.168.50.215', '192.168.50.238', '192.168.30.18'):
        assert sensitive not in content
    for target in re.findall(r'\]\(([^)]+)\)', content):
        if '://' not in target and not target.startswith('#'):
            assert ((repo/path).parent/target.split('#')[0]).exists(), target
subprocess.run(['git', '-C', str(repo), 'diff', '--check'], check=True)
(R/'doc-check.json').write_text(json.dumps(dict(paths=2, links='PASS', sensitive_literals='PASS',
    source_unchanged=True, runtime_mutations=0, portfolio='LOW',
    hashes={p: hashlib.sha256((repo/p).read_bytes()).hexdigest() for p in paths}), indent=2))
args = ['/root/.local/bin/codex-git-closeout', 'prepare', '--repository', str(repo),
        '--output', str(R/'git-manifest.json'), '--job-id', 'ltb1021-metadata-lan-preflight-20260911',
        '--profile', 'ltb1021', '--session-alias', 'work-session',
        '--title', 'docs(metadata): LAN 인증 적용 전 로그 노출 blocker 기록',
        '--message', 'Why=기존 Redis client가 인증 접속 URL을 로그에 출력하므로 외부 공개 전 최소 교정이 필요하다.',
        '--message', 'What=actual Redis 무인증·key0·port0와 Analytics healthy/Stream stopped를 확인하고 source 결함 중단 조건 및 재개점을 기록했다.',
        '--message', 'Impact=Owner 문서2개만 변경했다. 계정 발급·Redis 설정·image build·배포·Stream 실행0이며 실제 비밀번호 유출은 없다.',
        '--message', 'Verify=bounded actual read-only preflight와 문서 link·민감 literal·diff-check PASS. 모델/config hash 보존.',
        '--message', 'Rollback=운영 변경0. 문서 포인터를 직전 상태로 되돌릴 수 있으며 source 로그 교정 후 기존 적용 절차로 재개한다.',
        '--verify', 'actual read-only gate; source/runtime mutation0; exact2 docs links/literals/diff checks PASS',
        '--expires-in-seconds', '604800']
for path in paths:
    args += ['--path', path]
subprocess.run(args, check=True)
