"""合成离线样本检查。与同名 JSON 及文中列出的附件放在同一目录运行。"""
from pathlib import Path, PurePosixPath
import base64, hashlib, io, inspect, json, math, re, subprocess, sys, tempfile, wave, zipfile
from collections import Counter, defaultdict
from datetime import date, datetime
from decimal import Decimal
sys.dont_write_bytecode=True

def sha(b): return hashlib.sha256(b).hexdigest()
def rejected(fn):
    try: fn()
    except (ValueError,TypeError,KeyError,UnicodeError,OverflowError): return True
    return False

def git(root,*args,input=None,check=True):
    r=subprocess.run(['git','-c','core.autocrlf=false',*args],cwd=root,input=input,capture_output=True,check=check)
    return r

def verify(d,base):
    def norm(s):
        if not isinstance(s,str) or not s:raise ValueError('路径必须是非空字符串')
        normalized=s.replace(chr(92),'/')
        if normalized.startswith('/') or '..' in normalized.split('/') or ':' in normalized or chr(0) in normalized:raise ValueError('不允许的路径')
        result=str(PurePosixPath(normalized))
        if result=='.':raise ValueError('需要具体相对文件路径')
        return result
    plans=[{**p,'r':{norm(x) for x in p['read_set']},'w':{norm(x) for x in p['write_set']}} for p in d['plans']]
    out=[]
    for i,a in enumerate(plans):
        for b in plans[i+1:]:
            ww=a['w']&b['w'];rw=(a['w']&b['r'])|(b['w']&a['r'])
            if ww or rw:out.append({'calls':[a['call_id'],b['call_id']],'write_write':sorted(ww),'read_write':sorted(rw)})
    assert [x['calls'] for x in out]==d['expected_conflicting_pairs']
    assert out[0]['write_write']==out[0]['read_write']==['config.json']
    assert out[1]['write_write']==out[1]['read_write']==['config/a.json']
    negative=[{'path':s,'rejected':rejected(lambda s=s:norm(s))} for s in d['negative_paths']]
    assert all(x['rejected'] for x in negative)
    assert norm('config'+chr(92)+'a.json')==norm('config/a.json')
    return {'conflicts':out,'negative_path_cases':negative,'actual_tools_executed':0}

if __name__=='__main__':
    base=Path(__file__).resolve().parent
    data=json.loads((base/'a028.json').read_text('utf-8'))
    result=verify(data,base)
    print(json.dumps({'id':'A028','passed':True,'result':result},ensure_ascii=True))
