"""2.8 pilot: does processing many memos in one window make content bleed between them?

Batch: all 28 work/life transcripts in one prompt, cleaned up together (with the names list), output as JSON {id: text}.
Isolated: each memo cleaned up in its own call with the same instructions.
Bleed = a person's name that appears in the cleaned memo but not in the speaker's script of that memo
(names are fictional, so any extra name must have been imported from another memo or invented).
Run 3 times per condition; temperature 0."""
import json, pathlib, re, sys
from concurrent.futures import ThreadPoolExecutor
ROOT = pathlib.Path(__file__).resolve().parents[1]; sys.path.insert(0, str(ROOT))
from common import chat, dump
HERE = pathlib.Path(__file__).resolve().parent
d = json.load(open(ROOT / 'corpus/memos.json', encoding='utf-8'))
src = json.load(open(ROOT / 'corpus/transcripts_grok.json', encoding='utf-8'))
ids = [m['id'] for m in d['memos'] if m['set'] in 'WL']
ref = {m['id']: m['text'] for m in d['memos']}
names = (ROOT / '2_3_names/names.md').read_text(encoding='utf-8')
PEOPLE = list(d['people'])
ASK = '帮我整理语音转写：改正识别错误、补标点，不改写句子。'
def bleed(outs):
    found = {}
    for mid in ids:
        extra = [p for p in PEOPLE if p in outs.get(mid, '') and p not in ref[mid] and not (p == '蔺思远' and '思远' in ref[mid])]
        if extra: found[mid] = extra
    return found
def batch(model):
    blob = '\n\n'.join(f'[{mid}] {src[mid]}' for mid in ids)
    out, u, _ = chat([{'role': 'user', 'content': f'{ASK}\n\n{names}\n\n下面是 {len(ids)} 条转写，每条以 [编号] 开头。逐条整理，输出一个 JSON 对象，键是编号，值是整理好的全文，不要输出别的。\n\n{blob}'}],
                     model, temperature=0, response_format={'type': 'json_object'})
    try: return json.loads(out), u
    except Exception: return json.loads(re.search(r'\{.*\}', out, re.S).group(0)), u
def isolated(model):
    def one(mid):
        return mid, chat([{'role': 'user', 'content': f'{ASK}只输出整理好的全文。\n\n{names}\n\n转写：\n{src[mid]}'}], model, temperature=0)[0].strip()
    with ThreadPoolExecutor(8) as ex: return dict(ex.map(one, ids)), None
res = json.load(open(HERE / 'results.json', encoding='utf-8')) if (HERE / 'results.json').exists() else {}
for model in sys.argv[1:] or ['deepseek-v4.1-flash', 'gemini-3.8-flash']:
    for cond, fn in [('batch', batch), ('isolated', isolated)]:
        runs = []
        for r in range(3):
            outs, u = fn(model)
            b = bleed(outs); runs.append({'bleed_memos': len(b), 'bleed': b, 'missing': [i for i in ids if i not in outs], 'usage': u})
            dump(outs, HERE / f'out_{model}_{cond}_{r}.json')
            print(model, cond, r, 'bleed memos', len(b), b, 'missing', runs[-1]['missing'], flush=True)
        res[f'{model}:{cond}'] = runs
dump(res, HERE / 'results.json')
