"""Seeded semantic file-task generator. Oracles are returned, never written to inputs.""" from __future__ import annotations import csv import hashlib import io import json from pathlib import Path import random import secrets STRATA = ('facts', 'unicode', 'totals', 'duplicates', 'comparison', 'missing-fields', 'mapping', 'nested', 'policy', 'logs', 'recursive', 'money', 'ambiguity', 'large-table', 'merge', 'missing-file', 'repair-json', 'goal', 'repair-run', 'conflicts') def generate(workspace: Path, seed: int | None = None, count: int = 20): if count < len(STRATA): raise ValueError('At least 20 cases are required for stratified coverage') seed = secrets.randbits(128) if seed is None else seed rng = random.Random(seed) cases = [] for index in range(count): kind = STRATA[index % len(STRATA)] n = rng.randint(7, 31) values = [rng.randint(-50, 150) for _ in range(n)] board = f'板-{rng.randrange(100000, 999999)}' refs = sorted(rng.sample(range(1, 999), rng.randint(3, 9))) old, new = f'OLD-{rng.randrange(999999)}', f'NEW-{rng.randrange(999999)}' files, expected, goal = {}, {}, False def js(name, value): files[name] = json.dumps(value, ensure_ascii=False) def table(name, rows): stream = io.StringIO(newline='') csv.writer(stream, lineterminator='\n').writerows(rows) files[name] = stream.getvalue() if kind == 'facts': obj = {'project': board, 'references': [f'R{x}' for x in refs], 'new_part_number': new} files['notes.txt'] = f'工程={board}\n位号={",".join(obj["references"])}\n新料号={new}\n' request = '读取 notes.txt,result.json 写 project、references 数组、new_part_number。' answer = obj elif kind == 'unicode': answer = f'{board} 电阻 Ω\n{new} 中文路径 ✓\n' files['中文/说明.txt'] = answer request = '把 中文/说明.txt 原样复制为 result.txt,保留换行。' expected['result.txt'] = answer elif kind == 'totals': rows = [(rng.choice(('A', 'B', 'C', 'D')), rng.randint(1, 30)) for _ in values] table('bom.csv', [('part', 'quantity'), *rows]) answer = {p: sum(q for part, q in rows if part == p) for p in sorted(set(p for p, _ in rows))} answer['total'] = sum(q for _, q in rows) request = '汇总 bom.csv,每个出现料号为键、数量为值,另加 total 总数量,保存 result.json。' elif kind in ('duplicates', 'conflicts'): rows = [(board, f'R{x}', old) for x in refs] duplicate = rng.choice(refs) rows += [(board, f'R{duplicate}', new), (board+'B', f'R{duplicate}', old)] rng.shuffle(rows) table('bom.csv', [('project', 'refdes', 'part'), *rows]) answer = {'duplicates': [{'project': board, 'refdes': f'R{duplicate}'}]} request = '按 project+refdes 查 bom.csv 重复身份(包括料号冲突)。result.json 写 duplicates 数组,每项只有 project、refdes;不同工程不算重复。' elif kind == 'comparison': changed = set(rng.sample(refs, rng.randint(1, len(refs)))) table('before.csv', [('refdes', 'part'), *[(f'R{x}', old) for x in refs]]) table('after.csv', [('refdes', 'part'), *[(f'R{x}', new if x in changed else old) for x in refs]]) answer = {'changes': [{'refdes': f'R{x}', 'before': old, 'after': new} for x in refs if x in changed]} request = '比较 before.csv 和 after.csv,result.json 写 changes 数组,每项 refdes、before、after,按位号数字升序。' elif kind == 'missing-fields': rows = [(f'R{x}', old, new) for x in refs] bad = rng.sample(range(len(rows)), rng.randint(1, len(rows))) for i in bad: row = list(rows[i]); row[rng.randrange(3)] = ''; rows[i] = row table('bom.csv', [('refdes', 'old', 'new'), *rows]) answer = {'invalid_rows': sorted(i+2 for i in bad)} request = 'bom.csv 三列均必填,result.json 写 invalid_rows,Excel行号表头为1,升序。' elif kind == 'mapping': obj = {f'R{x}': rng.choice((old, 'KEEP')) for x in refs} js('source.json', obj) answer = {k: new if v == old else v for k, v in obj.items()} request = f'把 source.json 中值 {old} 改为 {new},其余保留,另存 result.json。' elif kind == 'nested': depth = rng.randint(2, 6); obj = {'enabled': False, 'limit': n, 'note': board} for _ in range(depth): obj = {'child': obj, 'keep': new} js('config.json', obj) answer = json.loads(files['config.json']); leaf = answer for _ in range(depth): leaf = leaf['child'] leaf['enabled'] = True; leaf['limit'] = n*3 request = f'读取 config.json,沿 child 递归到叶子,只把 enabled 改 true,limit 改 {n*3},其余保留,另存 result.json。' elif kind == 'policy': default = rng.choice(('part_label', 'preserve')) rows = [{'refdes': f'R{x}', 'mode': rng.choice(('inherit', 'preserve', 'custom')), 'custom': f'{rng.randint(1,999)}k'} for x in refs] js('rows.json', rows) answer = {r['refdes']: default if r['mode']=='inherit' else 'custom:'+r['custom'] if r['mode']=='custom' else 'preserve' for r in rows} request = f'默认Value策略={default}。rows.json inherit用默认,preserve原样,custom写custom:对应custom值。result.json以位号作键。' elif kind == 'logs': error = rng.choice(('LIBRARY_NOT_FOUND', 'ACCESS_DENIED', 'INVALID_CELL')) saved = rng.randrange(5) files['run.log'] = f'INFO start\nERROR {error}\nERROR EXECUTION_ABORTED\nINFO saved={saved}\n' answer = {'root_error': error, 'saved': saved, 'execution_succeeded': False} request = 'run.log 第一条ERROR为根因。result.json写 root_error、saved 数字、execution_succeeded 布尔值。发生ERROR即失败。' elif kind == 'recursive': ids = [f'ID-{x}' for x in rng.sample(range(99999), n)] for i, marker in enumerate(ids): files[f'd{i%3}/deep/{i}.txt'] = f'accept_id={marker}\n' files['ignore.md'] = 'accept_id=IGNORE' answer = {'ids': sorted(ids)} request = '递归读取所有.txt的accept_id,按字符串升序,result.json写ids数组,忽略其他扩展名。' elif kind == 'money': rows = [(rng.randint(1, 20), rng.randint(1, 2000)) for _ in values] table('prices.csv', [('qty', 'unit_price'), *[(q, f'{p//100}.{p%100:02d}') for q,p in rows]]) cents = sum(q*p for q,p in rows) answer = {'total': f'{cents//100}.{cents%100:02d}', 'currency': 'CNY'} request = 'prices.csv数量乘单价求和,result.json写total两位小数字符串、currency=CNY。' elif kind == 'ambiguity': candidates = rng.randint(2, 7) js('catalog.json', [{'name': board, 'path': f'X:/{i}/{board}.prj'} for i in range(candidates)]) answer = {'status': 'ambiguous', 'candidate_count': candidates, 'selected_path': None} request = f'工程名只有{board},catalog.json同名候选不唯一时不能任选;result.json写status=ambiguous、candidate_count、selected_path=null。' elif kind == 'large-table': values = [rng.randint(-10000, 10000) for _ in range(rng.randint(600, 1400))] table('numbers.csv', [('value',), *[(v,) for v in values]]) answer = {'count': len(values), 'sum': sum(values), 'min': min(values), 'max': max(values)} request = '实际读取numbers.csv全部数据,result.json写count、sum、min、max。' elif kind == 'merge': a = rng.sample(range(100), n); b = rng.sample(range(100), n) js('a.json', a); js('b.json', b) answer = {'values': sorted(set(a+b)), 'count': len(set(a+b))} request = '合并a.json、b.json去重,result.json写values数字升序数组和count。' elif kind == 'missing-file': files['existing.txt'] = board answer = {'status': 'missing', 'content': None} request = f'尝试读取 missing-{n}.txt;不存在则result.json写status=missing、content=null,不编造。' elif kind == 'repair-json': files['broken.json'] = json.dumps({'refdes': f'R{refs[0]}', 'part': new})[:-1]+',}' answer = {'refdes': f'R{refs[0]}', 'part': new} request = 'broken.json尾随逗号非法,修正为标准JSON另存result.json,保留数据和原文件。' elif kind == 'goal': goal = True for i,v in enumerate(values): js(f'inputs/part-{i}.json', {'id': i, 'quantity': v}) expected[f'outputs/part-{i}.json'] = {'id': i, 'double_quantity': v*2} answer = {'count': n, 'total_double_quantity': sum(values)*2} request = '建立goal并完成:逐个读inputs全部JSON,各另存outputs同名文件,字段id和double_quantity(quantity乘2);result.json写count和total_double_quantity;核对所有产物后完成goal,不只给计划。' else: files['calculate.py'] = f'import json\nvalues={values!r}\nprint(json.dumps({{"sum":sum(values)+1,"count":len(values)}}))\n' answer = {'sum': sum(values), 'count': n} request = 'calculate.py求和多加1;保留原文件,修复另存fixed.py,实际运行并将JSON输出保存result.json。' if kind != 'unicode': expected['result.json'] = answer directory = workspace / f'{index+1:02d}-{kind}' directory.mkdir(parents=True, exist_ok=False) for name, content in files.items(): target = directory / name; target.parent.mkdir(parents=True, exist_ok=True) target.write_bytes(content.encode('utf-8')) cases.append({'id': directory.name, 'stratum': kind, 'directory': str(directory), 'request': request, 'goal': goal, 'expected': expected, 'parameters': {'row_count': n, 'board': board, 'references': refs, 'old': old, 'new': new}, 'input_hashes': {name: hashlib.sha256(content.encode('utf-8')).hexdigest() for name,content in files.items()}}) return seed, cases