Files
lc-mcp/scripts/export_feedback.py
T

140 lines
6.9 KiB
Python

"""Export whitelisted game feedback without credentials or unrelated files."""
import argparse
import hashlib
import json
import re
import zipfile
from datetime import datetime, timezone
from pathlib import Path
SECRET_FIELD = re.compile(r'^(api_?key|authorization|token|password|secret|credential)$', re.I)
SECRET_VALUE = re.compile(r'(?:oc_sk_[A-Za-z0-9_-]+|sk-[A-Za-z0-9_-]{12,}|Bearer\s+[A-Za-z0-9_.-]+)', re.I)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--source-root', type=Path, required=True)
parser.add_argument('--runtime-root', type=Path, required=True)
parser.add_argument('--skill-root', type=Path, required=True)
parser.add_argument('--output', type=Path, required=True)
args = parser.parse_args()
output = args.output.resolve()
output.mkdir(parents=True, exist_ok=True)
roots = ((args.source_root.resolve(), '{source_root}'),
(args.runtime_root.resolve(), '{runtime_root}'),
(args.skill_root.resolve(), '{skill_root}'),
(Path.home(), '{user_home}'))
redactions = 0
def clean_text(value):
nonlocal redactions
value, count = SECRET_VALUE.subn('[REDACTED]', value)
redactions += count
for root, label in roots:
value = value.replace(str(root), label).replace(root.as_posix(), label)
return value
def clean(value):
nonlocal redactions
if isinstance(value, dict):
result = {}
for key, item in value.items():
if SECRET_FIELD.fullmatch(key):
result[key] = '[REDACTED]'
redactions += 1
else:
result[key] = clean(item)
return result
if isinstance(value, list):
return [clean(item) for item in value]
return clean_text(value) if isinstance(value, str) else value
exported = []
def record(relative, source, raw, data, kind):
target = output / relative
target.parent.mkdir(parents=True, exist_ok=True)
target.write_bytes(data)
exported.append({'path': relative, 'source': clean_text(str(source)), 'kind': kind,
'source_sha256': hashlib.sha256(raw).hexdigest(),
'export_sha256': hashlib.sha256(data).hexdigest(), 'bytes': len(data)})
counts = {}
skipped_partial = {}
# Never include config files, Codex session transcripts, or auth tokens.
for name in ('agent_log.jsonl', 'lc_agent_log.jsonl', 'jev_mirror_log.jsonl',
'agent_smoke.jsonl', 'lc_agent_smoke.jsonl'):
source = args.source_root / name
if not source.exists():
continue
raw = source.read_bytes()
rows = []
skipped = 0
for line in raw.decode('utf-8-sig', 'replace').splitlines():
if not line.strip():
continue
try:
rows.append(clean(json.loads(line)))
except json.JSONDecodeError:
skipped += 1
data = ''.join(json.dumps(row, ensure_ascii=False) + '\n' for row in rows).encode('utf-8')
record('logs/' + name, source, raw, data, 'sanitized_jsonl')
counts[name] = len(rows)
if skipped:
skipped_partial[name] = skipped
for name in ('jev_mirror_status.json', 'jev_shop_context.json'):
source = args.source_root / name
if source.exists():
raw = source.read_bytes()
data = json.dumps(clean(json.loads(raw.decode('utf-8-sig'))), ensure_ascii=False, indent=2).encode('utf-8')
record('logs/' + name, source, raw, data, 'sanitized_snapshot')
sources = [(args.runtime_root / 'server.log', 'logs/server.txt')]
sources.extend((p, 'logs/runs/' + p.stem + '.txt') for p in sorted((args.runtime_root / 'runs').glob('*')) if p.is_file())
sources.extend((args.source_root / name, 'source_snapshots/' + name)
for name in ('lc_agent.py', 'jev_mirror_agent.py', 'jev_decision_client.mjs',
'test_jev_mirror_agent.py', 'mirror_dungeon_speedrun_notes.md'))
sources.extend((p, 'skill/' + p.relative_to(args.skill_root).as_posix())
for p in sorted(args.skill_root.rglob('*.md')))
for source, relative in sources:
if source.exists():
raw = source.read_bytes()
cleaned = clean_text(raw.decode('utf-8-sig', 'replace'))
if source.suffix == '.py':
cleaned = cleaned.rstrip() + '\n'
record(relative, source, raw, cleaned.encode('utf-8'), 'sanitized_text')
captures = []
archive = output / 'game-captures.zip'
with zipfile.ZipFile(archive, 'w', compression=zipfile.ZIP_DEFLATED, compresslevel=1) as bundle:
for label, folder in (('legacy_game', args.source_root / 'shots'),
('mcp_game', args.runtime_root / 'shots'),
('model_game', args.source_root / 'lc_shots'),
('fixtures', args.source_root / 'fixtures')):
for source in sorted(folder.glob('*')):
if not source.is_file() or source.suffix.lower() not in ('.png', '.jpg', '.jpeg'):
continue
try:
raw = source.read_bytes()
except FileNotFoundError:
continue # live screenshots can rotate during export
name = label + '/' + source.name
bundle.writestr(name, raw)
captures.append({'path': name, 'bytes': len(raw), 'sha256': hashlib.sha256(raw).hexdigest()})
(output / 'capture_manifest.json').write_text(json.dumps(captures, ensure_ascii=False, indent=2), encoding='utf-8')
manifest = {'snapshot_at_utc': datetime.now(timezone.utc).isoformat(),
'log_record_counts': counts, 'partial_lines_skipped': skipped_partial,
'redaction_count': redactions, 'files': exported, 'capture_count': len(captures),
'capture_archive_bytes': archive.stat().st_size,
'capture_archive_sha256': hashlib.sha256(archive.read_bytes()).hexdigest(),
'excluded': ['API/MCP credentials', 'all local configs', 'Codex session transcripts',
'unrelated asset extraction files', 'virtual environments'],
'limitations': ['MCP retains only 30 recent captures; deleted frames cannot be reconstructed.',
'Live session still running; snapshot is not proof of dungeon completion.']}
(output / 'manifest.json').write_text(json.dumps(manifest, ensure_ascii=False, indent=2), encoding='utf-8')
print(json.dumps({k:manifest[k] for k in ('snapshot_at_utc', 'log_record_counts', 'capture_count',
'capture_archive_bytes', 'redaction_count')}, ensure_ascii=False))
if __name__ == '__main__':
main()