File size: 44,068 Bytes
ae36cfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed983e0
 
ae36cfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
#!/usr/bin/env python3
"""Publish verified local artifacts without provisioning any inference service."""
from __future__ import annotations

import argparse
import getpass
import hashlib
import json
import math
import os
import re
import sys
import tempfile
from datetime import datetime, timezone
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / 'scripts'))


def sha256(path):
    h = hashlib.sha256()
    with Path(path).open('rb') as f:
        for block in iter(lambda: f.read(8 * 1024 * 1024), b''):
            h.update(block)
    return h.hexdigest()


def read_json(path):
    return json.loads(Path(path).read_text())


def read_jsonl(path):
    return [json.loads(x) for x in Path(path).read_text().splitlines() if x.strip()]


def safe_member(root, name):
    target = (root / name).resolve()
    if not target.is_relative_to(root.resolve()) or not target.is_file():
        raise ValueError(f'Missing or unsafe artifact file: {name}')
    return target


def license_files(directory):
    """Require the full Apache license and preserve every existing notice verbatim."""
    directory = Path(directory)
    main = safe_member(directory, 'LICENSE')
    text = main.read_text(encoding='utf-8')
    if (len(text) < 9000 or 'Apache License' not in text or 'Version 2.0, January 2004' not in text
            or 'END OF TERMS AND CONDITIONS' not in text
            or any(not re.search(rf'(?m)^\s*{number}\.\s+\S', text) for number in range(1, 10))):
        raise ValueError('A full original Apache-2.0 LICENSE is required before upload')
    return {path.name: safe_member(directory, path.name) for path in sorted(directory.iterdir())
            if path.is_file() and path.name.startswith(('LICENSE', 'NOTICE'))}


def prepare_gguf_licenses(merged, gguf):
    """Copy only missing upstream notices; never replace a conflicting file."""
    originals = license_files(merged)
    existing = {path.name for path in gguf.iterdir() if path.is_file() and path.name.startswith(('LICENSE', 'NOTICE'))}
    if existing - set(originals):
        raise ValueError('GGUF contains license or notice files absent from the upstream export')
    for name, original in originals.items():
        target = gguf / name
        if target.exists() and (not target.is_file() or target.read_bytes() != original.read_bytes()):
            raise ValueError('GGUF license or notice differs from the original upstream file')
    for name, original in originals.items():
        target = gguf / name
        if not target.exists():
            with target.open('xb') as handle:
                handle.write(original.read_bytes())
                handle.flush()
                os.fsync(handle.fileno())
    return originals


def upload_licenses(api, repo, repo_type, files):
    for name, path in files.items():
        api.upload_file(repo_id=repo, repo_type=repo_type, path_or_fileobj=str(path), path_in_repo=name,
                        commit_message='Preserve complete original license and notice files')


def inspect_evaluation(summary_path, data, adapter):
    summary = read_json(summary_path)
    if summary.get('status') != 'complete' or summary.get('expected_prompts') != 500 or summary.get('recorded_prompts') != 500 or summary.get('infrastructure_failures', 1) != 0:
        raise ValueError('Publication requires a complete 500-prompt evaluation without infrastructure failures')
    if summary.get('run_seconds') != 60 or summary.get('pass_k') != 1 or summary.get('retries') != 0:
        raise ValueError('Publication requires unrepaired pass@1 with 60-second runtime windows')
    evaluation = summary_path.parent
    manifest = read_json(evaluation / 'manifest.json')
    if manifest.get('smoke_test', True) or manifest.get('expected_count') != 500 or manifest.get('run_seconds') != 60:
        raise ValueError('A smoke-test evaluation cannot be used for publication')
    if manifest.get('sample_indices') != [0, 200, 400] or summary.get('sample_indices') != [0, 200, 400]:
        raise ValueError('Evaluation does not use the predeclared sample indices 0, 200, and 400')
    if manifest.get('prompts_sha256') != sha256(data / 'eval_prompts.jsonl'):
        raise ValueError('Evaluation prompt hash differs from frozen dataset')
    prompts = read_jsonl(data / 'eval_prompts.jsonl')
    if len(prompts) != 500 or len({row['instruction'] for row in prompts}) != 500:
        raise ValueError('Dataset does not contain 500 unique evaluation prompts')
    # Evaluate the same adapter being uploaded, not an unrelated checkpoint.
    expected_adapter = manifest.get('generation', {}).get('adapter', {}).get('files', {})
    if expected_adapter.get('adapter_model.safetensors') != sha256(adapter / 'adapter_model.safetensors'):
        raise ValueError('Evaluated adapter does not match the adapter being uploaded')
    files = sorted((evaluation / 'items').glob('*/result.json'))
    if len(files) != 500:
        raise ValueError('Evaluation summary is missing underlying per-prompt evidence')
    results = [read_json(p) for p in files]
    if sorted(r.get('index', -1) for r in results) != list(range(500)):
        raise ValueError('Evaluation indices are incomplete or duplicated')
    compiled, clean = 0, 0
    for path, result in zip(files, results):
        attempt = read_json(path.parent / 'attempt.json')
        index = result['index']
        if attempt.get('index') != index or attempt.get('instruction') != prompts[index]['instruction']:
            raise ValueError('Per-prompt evidence does not match the frozen prompt ordering')
        if result.get('infrastructure_failure'):
            raise ValueError('Infrastructure failures cannot be reported as model metrics')
        declared = result.get('validation_evidence')
        # Prefer the exact evidence directory named by the evaluator. A glob is
        # used only to reject ambiguous completed attempts or support old records.
        completed_check = None
        if declared is not None:
            if not isinstance(declared, str) or not re.fullmatch(r'check(?:-resume-[0-9]{3,})?', declared):
                raise ValueError('Unsafe validation evidence directory')
            completed_check = (path.parent / declared).resolve()
            if not completed_check.is_relative_to(path.parent.resolve()) or not (completed_check / 'validation.json').is_file():
                raise ValueError('Validation evidence pointer is missing or leaves the evaluated item')
        checks = [x.resolve().parent for x in path.parent.glob('check*/validation.json') if x.is_file()]
        if completed_check is not None:
            if len(checks) != 1 or checks[0] != completed_check:
                raise ValueError('Validation evidence pointer does not match the sole completed attempt')
        elif len(checks) == 1 and checks[0].is_relative_to(path.parent.resolve()):
            completed_check = checks[0]
        if result.get('clean_run_60s') and not result.get('compile_success'):
            raise ValueError('A clean runtime cannot be reported after failed compilation')
        if result.get('compile_success'):
            compiled += 1
            if completed_check is None or not (completed_check / 'import.log').exists():
                raise ValueError('Successful compile lacks unambiguous import log evidence')
        if result.get('clean_run_60s'):
            clean += 1
            check = read_json(completed_check / 'validation.json')
            if not check.get('compile_success') or not check.get('clean_run_60s'):
                raise ValueError('Clean-run result contradicts validation evidence')
            runtime = check.get('runtime', {})
            if runtime.get('wall_seconds', 0) < 60:
                raise ValueError('Successful runtime was shorter than 60 seconds')
            runtime_log = completed_check / 'runtime.log'
            if not runtime_log.exists() or runtime.get('log_sha256') != sha256(runtime_log):
                raise ValueError('Clean runtime lacks matching log evidence')
    for name, count in [('compile_success', compiled), ('clean_run_60s', clean)]:
        if summary.get(name + '_count') != count or not math.isclose(summary.get(name + '_rate', -1), count / 500, abs_tol=1e-12):
            raise ValueError('Reported metrics disagree with per-prompt evidence')
    return summary, manifest


def inspect_weights(adapter, merged, gguf):
    license_files(merged)
    from safetensors import safe_open
    training = read_json(adapter / 'TRAINING_COMPLETE.json')
    if training.get('status') != 'complete' or training.get('global_step', 0) <= 0 or training.get('global_step') != training.get('target_steps'):
        raise ValueError('Adapter training is incomplete')
    config = read_json(adapter / 'adapter_config.json')
    if config.get('r') != 32 or config.get('lora_alpha') != 64:
        raise ValueError('Unexpected LoRA rank or alpha')
    adapter_file = adapter / 'adapter_model.safetensors'
    with safe_open(adapter_file, framework='pt', device='cpu') as f:
        keys = list(f.keys())
        adapter_parameters = sum(math.prod(f.get_slice(k).get_shape()) for k in keys)
    if len(keys) != 392 or adapter_parameters < 1_000_000:
        raise ValueError('Adapter does not contain the expected 28-layer, seven-module rank-32 tensors')
    merged_marker = read_json(merged / 'MERGE_COMPLETE.json')
    if merged_marker.get('adapter_sha256') != sha256(adapter_file):
        raise ValueError('Merged model was produced from a different adapter')
    index = read_json(merged / 'model.safetensors.index.json')['weight_map']
    parameters = 0
    by_file = {}
    for name, filename in index.items():
        by_file.setdefault(filename, []).append(name)
    for filename, names in by_file.items():
        with safe_open(safe_member(merged, filename), framework='pt', device='cpu') as f:
            if set(f.keys()) != set(names):
                raise ValueError('Merged tensor index and shard contents disagree')
            for name in names:
                tensor = f.get_slice(name)
                if tensor.get_dtype() != 'F16':
                    raise ValueError('Merged model contains a non-fp16 tensor')
                parameters += math.prod(tensor.get_shape())
    if not 7_000_000_000 < parameters < 8_000_000_000:
        raise ValueError('Merged artifact is not a complete Qwen2.5-Coder-7B-size model')
    gguf_file = gguf / 'VibeGame-8B-Q4_K_M.gguf'
    with gguf_file.open('rb') as f:
        if f.read(4) != b'GGUF':
            raise ValueError('GGUF file has invalid magic bytes')
    quant = read_json(gguf / 'GGUF_COMPLETE.json')
    if quant.get('quantization') != 'Q4_K_M' or quant.get('sha256') != sha256(gguf_file) or gguf_file.stat().st_size < 1_000_000_000:
        raise ValueError('GGUF is incomplete or does not match its export manifest')
    if quant.get('source_merge', {}).get('adapter_sha256') != sha256(adapter_file):
        raise ValueError('GGUF export was produced from a different adapter')
    return {'training': training, 'adapter_sha256': sha256(adapter_file),
            'merged_parameters': parameters, 'gguf_sha256': quant['sha256']}


def inspect_artifacts(adapter, merged, gguf, data, summary_path):
    license_files(ROOT)
    weights = inspect_weights(adapter, merged, gguf)
    dataset = read_json(data / 'dataset_manifest.json')
    rows = read_jsonl(data / 'train.jsonl') + read_jsonl(data / 'dev.jsonl')
    if dataset.get('train', 0) + dataset.get('dev', 0) != len(rows) or not rows:
        raise ValueError('Dataset counts are inconsistent')
    admitted = {r['output_sha256'] for r in read_jsonl(data / 'validation_manifest.jsonl') if r.get('accepted')}
    if any(hashlib.sha256(r['output'].encode()).hexdigest() not in admitted for r in rows):
        raise ValueError('Dataset contains a project without accepted Godot validation')
    for row in read_jsonl(data / 'reference_gd.jsonl'):
        if row.get('license') not in {'MIT', 'Apache-2.0', 'CC0-1.0'}:
            raise ValueError('Dataset reference corpus contains a disallowed license')
    if not (data / 'licenses').is_dir() or not list((data / 'licenses').rglob('*')):
        raise ValueError('Source license notices are missing')
    summary, evaluation_manifest = inspect_evaluation(summary_path, data, adapter)
    return {**weights, 'dataset': dataset, 'evaluation': summary,
            'evaluation_manifest': evaluation_manifest}


def provider_mapping(api, repo):
    info = api.model_info(repo_id=repo, expand=['inferenceProviderMapping'])
    mapping = getattr(info, 'inference_provider_mapping', None)
    if mapping is None:
        mapping = getattr(info, 'inferenceProviderMapping', None)
    # An absent field is not evidence of an empty mapping.
    if mapping is None:
        raise RuntimeError('Hugging Face did not return inferenceProviderMapping; cannot verify provider state')
    if not isinstance(mapping, (dict, list)):
        raise TypeError('Unexpected inference provider response')
    return mapping


def model_card(repo_ids, evidence, template=None):
    e, d = evidence['evaluation'], evidence['dataset']
    gpu = evidence.get('evaluation_manifest', {}).get('generation', {}).get('gpu', 'Not recorded')
    hardware = re.sub(r'[\r\n`|]', ' ', str(gpu))[:120]
    context = ('This table reports the separate full A100 pass on Colab Pro. The partial T4 experiment is '
               'preserved under `evaluation-t4-partial/`; its results are not pooled with this pass.'
               if 'A100' in hardware else 'The hardware and runtime versions for this pass are recorded in `evaluation/manifest.json`.')
    demo_selection = ('This video is a success-only showcase: exactly the first three clean results in frozen prompt order. '
                      'It is not representative of the model. All three recordings use untouched outputs, and the full '
                      '500-prompt results and actual times stay visible. The original benchmark sample slots 0, 200, '
                      'and 400 remain unchanged in the evidence.'
                      if evidence.get('demo_kind') == 'model-success-showcase' else
                      'Samples 0, 200, and 400 were chosen before evaluation. Failed samples keep their slots as diagnostic cards.')
    values = {'COMPILE_RATE': f"{e['compile_success_rate']:.1%}", 'CLEAN_RUN_RATE': f"{e['clean_run_60s_rate']:.1%}",
              'COMPILE_COUNT': str(e['compile_success_count']), 'CLEAN_RUN_COUNT': str(e['clean_run_60s_count']),
              'UNDER_60_COUNT': str(e['under_60s_launch_count']),
              'TRAIN_COUNT': str(d['train']), 'DEV_COUNT': str(d['dev']),
              'MEDIAN_SECONDS': str(e['prompt_to_launch_median_seconds']),
              'MEDIAN_LATENCY': (f"{e['prompt_to_launch_median_seconds']:.2f} seconds"
                                 if e['prompt_to_launch_median_seconds'] is not None else 'Not measured'),
              'EVAL_HARDWARE': hardware, 'EVAL_CONTEXT': context,
              'DEMO_SELECTION': demo_selection,
              'MODEL_REPO': repo_ids['merged'], 'GGUF_REPO': repo_ids['gguf'], 'ADAPTER_REPO': repo_ids['adapter'], 'DATASET_REPO': repo_ids['dataset']}
    if template and template.exists():
        body = template.read_text()
        # Strip template metadata; this publisher controls release metadata.
        if body.startswith('---\n') and '\n---\n' in body[4:]:
            body = body.split('\n---\n', 1)[1]
        for key, value in values.items():
            body = body.replace('{{' + key + '}}', value)
        if re.search(r'\{\{[A-Z0-9_]+\}\}', body):
            raise ValueError('Model card contains unresolved release placeholders')
    else:
        body = f'''# VibeGame-8B

I wanted a small open model that could turn a game idea into a Godot project I could actually open and play. This is the experiment: describe a 2D game and get project.godot, a scene, and GDScript in one JSON response.

The project name is VibeGame-8B. The base is Qwen2.5-Coder-7B-Instruct, with 7.61 billion parameters. “Prompt to playable game in 60 seconds” is the target. The measured timing below says how close this release gets; it is not a blanket promise.

Download the [GGUF](https://huggingface.co/{repo_ids['gguf']}) and its Modelfile. Run `ollama create vibegame-8b -f Modelfile`, then use the repository generator and validator. Generated code should pass the sandboxed Godot check before you open it. This model uses Godot 4.5.1 and procedural shapes, with no game assets bundled.

The [dataset](https://huggingface.co/datasets/{repo_ids['dataset']}) has {d['train']} training pairs and {d['dev']} development pairs. Whole synthetic projects must import and run cleanly for 60 seconds before admission. The reference corpus accepts MIT, Apache-2.0, and CC0 code, retains source notices, and excludes game assets. Deduplication happens before splitting. The 500 frozen evaluation prompts never enter the training loss.

These are small games. A script can run cleanly while the controls, scoring, or win condition are wrong. The benchmark checks parsing and runtime behavior without pressing buttons; it does not replace playtesting. Longer projects may exceed the 4096-token context, and JSON can be incomplete. We do not repair outputs during evaluation or hide failures.

If you want to help, send a small reproducible failing prompt, a permissively licensed project with clear provenance, or a validator improvement. More independently authored game mechanics and actual playtesting would help more than another favorable screenshot.
'''
        body += f'''

## Measured release results

| Check | Result |
| --- | --- |
| Frozen prompts | 500 |
| Evaluation GPU | {hardware} |
| Compile success | {e['compile_success_count']}/500 ({e['compile_success_rate']:.1%}) |
| Clean 60-second run | {e['clean_run_60s_count']}/500 ({e['clean_run_60s_rate']:.1%}) |
| Median prompt-to-launch among compiled games | {values['MEDIAN_LATENCY']} |
| Compiled games launched within 60 seconds | {e['under_60s_launch_count']}/500 |
| Sampling | One generation per prompt; no repairs or retries |

These numbers evaluate the **adapter on its 4-bit base**. They are not measurements of the merged fp16 or GGUF artifacts. Export changes numerical precision; those formats still need a separate run. Three sample indices are fixed at 0, 200, and 400 before evaluation, covering distinct game families. See `evaluation/summary.json` and the full per-prompt evidence for every failure.

{context}

{demo_selection}

The provider mapping was checked during publication. `inference: false` hides the widget; it is not an account billing switch. No endpoint or paid inference service is created by this release script. Account provider/billing settings are verified separately.
'''
    header = '---\nlibrary_name: transformers\nlicense: apache-2.0\nbase_model: Qwen/Qwen2.5-Coder-7B-Instruct\npipeline_tag: text-generation\ninference: false\ntags:\n- godot\n- gdscript\n- qlora\n- game-development\n---\n\n'
    return header + body.rstrip() + '\n'


def sanitized_text(text):
    # Remove local home prefixes from diagnostic paths, without changing model
    # tensors, training examples, relative project paths, or preserved hashes.
    text = re.sub(r"/Users/[^/\\\"\s]+", "/redacted-home", text)
    text = re.sub(r"/home/[^/\\\"\s]+", "/redacted-home", text)
    return text.replace('/content/drive/MyDrive', '/persistent-drive')


def public_evidence_text(text):
    """Reject recognizable credentials instead of uploading or printing them."""
    if re.search(r'\bhf_[A-Za-z0-9]{20,}\b|\bsk-[A-Za-z0-9_-]{20,}\b|\bBearer\s+[A-Za-z0-9._~-]{12,}', text, re.IGNORECASE):
        raise ValueError('Publication evidence contains a possible credential; nothing was uploaded')
    try:
        value = json.loads(text)
    except ValueError:
        value = None
    sensitive = {'token', 'hf_token', 'access_token', 'authorization', 'password', 'api_key', 'secret', 'client_secret'}
    def check(item):
        if isinstance(item, dict):
            for key, child in item.items():
                if str(key).lower() in sensitive and child not in (None, '', False):
                    raise ValueError('Publication evidence contains a credential field; nothing was uploaded')
                check(child)
        elif isinstance(item, list):
            for child in item:
                check(child)
    check(value)
    return sanitized_text(text)


def a100_receipts(evaluation, manifest, data, adapter):
    """Bind the extra A100 receipts to this pass and its preserved T4 source."""
    if evaluation.name != 'evaluation-a100-v1' or evaluation.is_symlink():
        raise ValueError('A100 release requires its separate evaluation-a100-v1 directory')
    declaration_path = safe_member(evaluation, 'pro-protocol.json')
    declaration = read_json(declaration_path)
    protocol = declaration['protocol']
    generation = manifest['generation']
    prior = evaluation.parent / 'evaluation'
    if prior.is_symlink():
        raise ValueError('T4 evidence directory must not be a symlink')
    prior_manifest_path = safe_member(prior, 'manifest.json')
    prior_manifest = read_json(prior_manifest_path)
    if (protocol.get('experiment') != 'evaluation-a100-v1'
            or protocol.get('source_t4_manifest_sha256') != sha256(prior_manifest_path)
            or 'T4' not in prior_manifest.get('generation', {}).get('gpu', '')
            or 'A100' not in generation.get('gpu', '')
            or protocol.get('gpu_runtime', {}).get('gpu') != generation['gpu']
            or protocol.get('train_sha256') != sha256(data / 'train.jsonl')):
        raise ValueError('A100 predeclaration does not match the selected experiment or T4 source')
    for key in ('expected_count', 'batch_size', 'workers', 'max_new_tokens', 'run_seconds', 'sample_indices', 'decoding', 'prompts_sha256'):
        if protocol.get(key) != manifest.get(key) or manifest.get(key) != prior_manifest.get(key):
            raise ValueError('A100 and T4 protocols differ: ' + key)
    if protocol.get('godot_sha256') != manifest.get('runtime_sha256') or manifest.get('runtime_sha256') != prior_manifest.get('runtime_sha256'):
        raise ValueError('A100 and T4 Godot fingerprints differ')
    for key in ('adapter', 'base_model', 'base_revision', 'system_prompt_sha256'):
        if protocol.get(key) != generation.get(key) or generation.get(key) != prior_manifest['generation'].get(key):
            raise ValueError('A100 and T4 model identities differ: ' + key)
    for key in ('torch_version', 'cuda_version', 'packages'):
        if protocol.get('gpu_runtime', {}).get(key) != generation.get(key) or generation.get(key) != prior_manifest['generation'].get(key):
            raise ValueError('A100 and T4 runtime versions differ: ' + key)
    declared = datetime.fromisoformat(declaration['declared_utc'])
    if declared.tzinfo is None:
        raise ValueError('A100 predeclaration needs an explicit timezone')
    first_attempt = min(datetime.fromisoformat(read_json(path)['started_utc']) for path in evaluation.glob('items/*/attempt.json'))
    if declared > first_attempt:
        raise ValueError('A100 protocol was declared after blind evaluation started')
    # Reuse the CPU-only receipt check; importing this module loads no model.
    from benchmark_batch import capacity_prompts
    from evaluate_pro import capacity_valid
    training_prompts = [row['instruction'] for row in capacity_prompts(adapter, data / 'train.jsonl')]
    capacities, receipts = {}, [declaration_path]
    for pattern in ('capacity-*.json', 'budget-*.json'):
        for path in sorted(evaluation.glob(pattern)):
            if not re.fullmatch(r'(?:capacity|budget)-[0-9]{4,}\.json', path.name):
                raise ValueError('Unexpected A100 receipt filename')
            path = safe_member(evaluation, path.name)
            public_evidence_text(path.read_text())
            receipt = read_json(path)
            receipts.append(path)
            if path.name.startswith('capacity-'):
                capacities[sha256(path)] = receipt
    budgets = [read_json(path) for path in receipts if path.name.startswith('budget-')]
    approved = []
    for budget in budgets:
        if budget.get('fits_observed_balance') is True:
            capacity = capacities.get(budget.get('capacity_report_sha256'))
            if capacity is None or not capacity_valid(capacity, protocol, prior_manifest['generation'], training_prompts):
                raise ValueError('Approved A100 budget has no matching valid capacity report')
            approved.append(budget)
    if not any(datetime.fromisoformat(row['observed_utc']) <= first_attempt for row in approved):
        raise ValueError('A100 release lacks an approved pre-evaluation capacity/budget receipt')
    return receipts, prior


def evaluation_uploads(summary_path, data, adapter):
    """Select and screen public evidence before requesting a publication token."""
    evaluation = summary_path.parent
    manifest = read_json(evaluation / 'manifest.json')
    sources = [('evaluation', evaluation, [])]
    if ('A100' in manifest.get('generation', {}).get('gpu', '')
            or evaluation.name == 'evaluation-a100-v1' or (evaluation / 'pro-protocol.json').exists()):
        receipts, prior = a100_receipts(evaluation, manifest, data, adapter)
        sources = [('evaluation', evaluation, receipts), ('evaluation-t4-partial', prior, [])]
    allowed_names = {'summary.json', 'manifest.json', 'result.json', 'attempt.json', 'generation.json',
                     'validation.json', 'interruption.json', 'import.log', 'runtime.log'}
    uploads = []
    for prefix, directory, extra in sources:
        extra = {path.resolve() for path in extra}
        files = {}
        for local in sorted(directory.rglob('*')):
            if not local.is_file():
                continue
            relative = local.relative_to(directory)
            showcase_file = (str(relative) == 'showcase/selection.json'
                             or (relative.parts[:2] == ('showcase', 'recordings')
                                 and re.fullmatch(r'sample-[0-9]{3}\.mp4|sample-[0-9]{3}\.recording\.json', local.name)))
            if not (local.name in allowed_names or local.resolve() in extra
                    or showcase_file or (relative.parts[0] == 'recordings' and local.suffix in {'.mp4', '.json'})):
                continue
            if local.is_symlink() or not local.resolve().is_relative_to(directory.resolve()):
                raise ValueError('Unsafe public evaluation evidence path')
            if local.suffix != '.mp4':
                public_evidence_text(local.read_text())
            files[str(relative)] = {'path': local, 'sha256': sha256(local)}
        if not {'summary.json', 'manifest.json'} <= files.keys():
            raise ValueError('Selected evaluation evidence lacks its manifest or summary')
        uploads.append({'prefix': prefix, 'files': files})
    return uploads


def stage_evaluation_upload(upload, destination):
    redactions = {}
    for relative, record in upload['files'].items():
        local = record['path']
        if sha256(local) != record['sha256']:
            raise ValueError('Evaluation evidence changed after publication preflight')
        target = destination / relative
        target.parent.mkdir(parents=True, exist_ok=True)
        if local.suffix == '.mp4':
            target.symlink_to(local.resolve())
        else:
            original = local.read_text()
            redacted = public_evidence_text(original)
            target.write_text(redacted)
            if original != redacted:
                redactions[relative] = {'original_sha256': record['sha256'], 'published_sha256': sha256(target),
                                        'change': 'Local home-path prefix redaction only; original evidence hashes retained in manifests.'}
    (destination / 'REDACTIONS.json').write_text(json.dumps(redactions, indent=2) + '\n')


def inspect_demo(summary_path):
    """Require the launch asset produced from this evaluation and fixed samples."""
    video = summary_path.parent.parent / 'VibeGame-launch-60s.mp4'
    sidecar = video.with_suffix('.demo.json')
    if not video.is_file() or not sidecar.is_file():
        raise ValueError('Publication requires the launch video and its demo evidence')
    edit = read_json(sidecar)
    if edit.get('scaffold_preview') is not False or edit.get('output_sha256') != sha256(video) or edit.get('total_seconds') != 60:
        raise ValueError('Launch video lacks matching final-model evidence')
    plan = edit.get('source_plan', {})
    if edit.get('kind') == 'model-success-showcase':
        from build_demo import evaluation_plan
        from showcase import selection_path
        expected = evaluation_plan(summary_path.parent, showcase_selection=selection_path(summary_path.parent))
        if (plan != expected or edit.get('watermark') != expected['selection_disclosure']
                or len(plan['samples']) != 3 or not all(sample.get('capture') for sample in plan['samples'])):
            raise ValueError('Showcase must match the exact first three clean results and all three original captures')
        return video, sidecar
    if (edit.get('kind') != 'model-evaluation' or plan.get('kind') != 'model-evaluation'
            or plan.get('scaffold_preview') is not False
            or plan.get('evaluation_manifest_sha256') != sha256(summary_path.parent / 'manifest.json')):
        raise ValueError('Launch video belongs to a different evaluation or is a scaffold preview')
    samples = plan.get('samples', [])
    if not isinstance(samples, list) or [sample.get('index') for sample in samples if isinstance(sample, dict)] != [0, 200, 400] or len(samples) != 3:
        raise ValueError('Launch video does not use predeclared samples 0, 200, and 400')
    if not any(sample.get('capture') for sample in samples):
        raise ValueError('Launch video requires at least one actual gameplay capture')
    return video, sidecar


def main():
    p = argparse.ArgumentParser(description=__doc__)
    for name in ('adapter', 'merged', 'gguf', 'data', 'evaluation', 'output'):
        p.add_argument('--' + name, type=Path, required=True)
    p.add_argument('--namespace', required=True)
    p.add_argument('--model-card', type=Path, default=ROOT / 'docs/model_card.md')
    args = p.parse_args()
    if not re.fullmatch(r'[A-Za-z0-9][A-Za-z0-9_-]*', args.namespace):
        p.error('Invalid Hugging Face namespace')
    # Resolve every release gate before requesting credentials or creating repos.
    evidence = inspect_artifacts(args.adapter, args.merged, args.gguf, args.data, args.evaluation)
    upstream_licenses = prepare_gguf_licenses(args.merged, args.gguf)
    project_licenses = license_files(ROOT)
    video, video_evidence = inspect_demo(args.evaluation)
    evidence['demo_kind'] = read_json(video_evidence).get('kind')
    uploads = evaluation_uploads(args.evaluation, args.data, args.adapter)
    repo_ids = {'merged': f'{args.namespace}/VibeGame-8B', 'gguf': f'{args.namespace}/VibeGame-8B-GGUF',
                'adapter': f'{args.namespace}/VibeGame-8B-LoRA', 'dataset': f'{args.namespace}/VibeGame-8B-data'}
    card = model_card(repo_ids, evidence, args.model_card)
    from stage_exports import PublicationTracker, atomic_json, build_manifests
    previous_report = read_json(args.output) if args.output.exists() else {}
    from huggingface_hub import HfApi
    token = os.environ.get('HF_TOKEN') or getpass.getpass('Hugging Face write token (not saved): ')
    if not token:
        raise RuntimeError('A write token is required to publish; nothing was uploaded')
    api = HfApi(token=token)
    release_identity = hashlib.sha256(json.dumps({
        'namespace': args.namespace, 'adapter_sha256': evidence['adapter_sha256'],
        'adapter_config_sha256': sha256(args.adapter / 'adapter_config.json'),
        'gguf_sha256': evidence['gguf_sha256'], 'merge_receipt_sha256': sha256(args.merged / 'MERGE_COMPLETE.json'),
        'evaluation_manifest_sha256': sha256(args.evaluation.parent / 'manifest.json'),
        'upstream_licenses': {name: sha256(path) for name, path in upstream_licenses.items()},
        'project_licenses': {name: sha256(path) for name, path in project_licenses.items()},
        'data_sha256': {name: sha256(args.data / name) for name in ('train.jsonl', 'dev.jsonl', 'eval_prompts.jsonl')},
    }, sort_keys=True).encode()).hexdigest()
    report = {'status': 'uploading', 'repos': repo_ids, 'created_utc': datetime.now(timezone.utc).isoformat(),
              'release_identity': release_identity,
              'private_staging': dict(previous_report.get('private_staging', {})),
              'publication_ownership': dict(previous_report.get('publication_ownership', {})),
              'artifacts_verified': True, 'provider_checks': {}, 'paid_inference_calls': 0,
              'endpoint_creation_calls': 0, 'billing_changes': 0,
              'account_billing_status': 'Requires post-publication account settings verification; provider mappings alone do not prove account billing disabled.'}
    report['evaluation_evidence'] = {upload['prefix']: {relative: record['sha256'] for relative, record in upload['files'].items()}
                                     for upload in uploads}
    args.output.parent.mkdir(parents=True, exist_ok=True)
    def save_report():
        # Provider SDK records may be dataclasses; retain the earlier safe
        # string fallback while writing the receipt atomically.
        atomic_json(args.output, json.loads(json.dumps(report, default=str)))
    api = PublicationTracker(api, report, save_report)
    try:
        identity = api.whoami()
        allowed = {identity['name']} | {x['name'] for x in identity.get('orgs', [])}
        if args.namespace not in allowed:
            raise RuntimeError('Token identity has no membership in the requested namespace')
        existing = {}
        manifests = None
        for kind, repo in repo_ids.items():
            repo_type = 'dataset' if kind == 'dataset' else 'model'
            exists = api.repo_exists(repo_id=repo, repo_type=repo_type)
            existing[kind] = exists
            if exists:
                info = api.repo_info(repo_id=repo, repo_type=repo_type)
                if info.private is True:
                    if kind not in {'merged', 'gguf'}:
                        raise ValueError('Refusing to publish an unrelated private repository')
                    if manifests is None:
                        manifests = build_manifests(args.adapter, args.merged, args.gguf, args.namespace, weights=evidence)
                    api.adopt(manifests[kind], previous_report.get('private_staging', {}).get(repo))
                elif info.private is False:
                    previous = (previous_report.get('publication_ownership', {}).get(repo)
                                or previous_report.get('private_staging', {}).get(repo))
                    manifest = None
                    if previous and previous.get('manifest_sha256'):
                        if kind not in {'merged', 'gguf'}:
                            raise ValueError('Staging identity is invalid for this public repository')
                        if manifests is None:
                            manifests = build_manifests(args.adapter, args.merged, args.gguf, args.namespace, weights=evidence)
                        manifest = manifests[kind]
                    api.adopt_public(repo, repo_type, previous, manifest)
                else:
                    raise ValueError('Repository visibility could not be verified')
        for kind, repo in repo_ids.items():
            repo_type = 'dataset' if kind == 'dataset' else 'model'
            if not existing[kind]:
                api.create_repo(repo_id=repo, repo_type=repo_type, private=False, exist_ok=False)
                api.track_created(repo, repo_type)
            if repo_type == 'model':
                mapping = provider_mapping(api, repo)
                report['provider_checks'][repo] = {'before': mapping}
                if mapping:
                    raise RuntimeError(f'Provider mapping already active for {repo}; refusing upload')
            save_report()
        folders = {'merged': args.merged, 'gguf': args.gguf, 'adapter': args.adapter, 'dataset': args.data}
        patterns = {'merged': ['*.json', '*.safetensors', '*.txt', '*.model', '*.jinja', 'LICENSE*', 'NOTICE*'],
                    'gguf': ['*.gguf', '*.json', 'Modelfile', 'LICENSE*', 'NOTICE*'],
                    'adapter': ['*.json', '*.safetensors', '*.txt', '*.model', '*.jinja'],
                    'dataset': ['*.jsonl', '*.json', 'licenses/**']}
        for kind, repo in repo_ids.items():
            repo_type = 'dataset' if kind == 'dataset' else 'model'
            api.upload_folder(repo_id=repo, repo_type=repo_type, folder_path=folders[kind],
                              allow_patterns=patterns[kind], ignore_patterns=['**/.cache/**', '*.tmp', 'merge_progress.json', '*manifest.json', 'teacher_provenance.json', 'teacher_attempts.jsonl', 'validation_manifest.jsonl', 'rejected_candidates.jsonl'],
                              commit_message='Publish validated VibeGame research artifacts')
            # Upload from the original source so the frozen adapter directory
            # never changes and no notice is rewritten or reconstructed.
            upload_licenses(api, repo, repo_type, project_licenses if kind == 'dataset' else upstream_licenses)
            for local in folders[kind].glob('*'):
                if local.is_file() and (local.name.endswith('manifest.json') or local.name in {'teacher_provenance.json', 'teacher_attempts.jsonl', 'validation_manifest.jsonl', 'rejected_candidates.jsonl'}):
                    api.upload_file(repo_id=repo, repo_type=repo_type, path_or_fileobj=sanitized_text(local.read_text()).encode(),
                                    path_in_repo=local.name, commit_message='Add provenance with local home paths removed')
            if kind == 'dataset':
                dataset_card = '---\nlicense: apache-2.0\ntask_categories:\n- text-generation\nlanguage:\n- en\n---\n\n# VibeGame dataset\n\nOriginal synthetic projects plus a separately stored permissive GDScript reference corpus. Source files keep their original MIT, Apache-2.0, or CC0 terms and notices in `licenses/`; this repository does not relicense them. `train.jsonl` and `dev.jsonl` contain only validated projects. `eval_prompts.jsonl` is a frozen 500-prompt benchmark, never used for loss. See provenance and validation manifests for accepted and rejected examples. No game assets are included.\n'
                content = dataset_card
            else:
                content = card
                if kind == 'adapter':
                    content = content.replace('library_name: transformers', 'library_name: peft', 1)
                elif kind == 'gguf':
                    content = content.replace('library_name: transformers', 'library_name: gguf', 1)
            api.upload_file(repo_id=repo, repo_type=repo_type, path_or_fileobj=content.encode(), path_in_repo='README.md', commit_message='Document actual data, measurements, and limitations')
            if kind == 'merged':
                if video.exists():
                    api.upload_file(repo_id=repo, path_or_fileobj=str(video), path_in_repo='VibeGame-launch-60s.mp4',
                                    commit_message='Add measured 60-second model walkthrough')
                    api.upload_file(repo_id=repo, path_or_fileobj=sanitized_text(video_evidence.read_text()).encode(),
                                    path_in_repo='VibeGame-launch-60s.demo.json', commit_message='Preserve video provenance')
                for upload in uploads:
                    with tempfile.TemporaryDirectory(prefix='vibegame-public-evidence-') as temporary:
                        stage = Path(temporary)
                        stage_evaluation_upload(upload, stage)
                        api.upload_folder(repo_id=repo, repo_type='model', folder_path=stage, path_in_repo=upload['prefix'],
                                          commit_message='Preserve separate experiment evidence and protocol receipts')
                import subprocess
                subprocess.run([sys.executable, str(ROOT / 'scripts/build_notebook.py')], check=True, cwd=ROOT)
                with tempfile.TemporaryDirectory(prefix='vibegame-open-source-') as temporary:
                    stage = Path(temporary)
                    source_patterns = ['src/vibegame/*.py', 'scripts/*.py', 'tests/*.py', 'docs/*.md', 'requirements-colab.txt', 'pyproject.toml', 'LICENSE', 'NOTICE', 'README.md', 'notebooks/*.ipynb']
                    for pattern in source_patterns:
                        for local in ROOT.glob(pattern):
                            if local.is_file():
                                target = stage / local.relative_to(ROOT)
                                target.parent.mkdir(parents=True, exist_ok=True)
                                target.write_bytes(local.read_bytes())
                    (stage / 'Modelfile').write_bytes((args.gguf / 'Modelfile').read_bytes())
                    api.upload_folder(repo_id=repo, repo_type='model', folder_path=stage, path_in_repo='code',
                                      commit_message='Release pipeline source, tests, notebook, and licenses')
            if kind != 'dataset':
                mapping = provider_mapping(api, repo)
                report['provider_checks'][repo]['after'] = mapping
                if mapping:
                    # Make our release private immediately; do not create or invoke
                    # any inference service to try to fix a provider mapping.
                    api.update_repo_settings(repo_id=repo, private=True)
                    raise RuntimeError(f'Provider mapping appeared for {repo}; release made private for investigation')
            report.setdefault('uploaded', []).append(repo)
            save_report()
        # Matching staged exports remain private until all four repositories
        # have uploaded successfully and their exact owned contents are checked.
        api.promote()
        report['status'] = 'artifacts_uploaded_account_reverification_required'
        report['links'] = {kind: 'https://huggingface.co/' + ('datasets/' if kind == 'dataset' else '') + repo for kind, repo in repo_ids.items()}
        # Read-only endpoint inspection, if exposed by this token. It never
        # creates, starts, resumes, or deletes endpoint resources.
        try:
            endpoints = api.list_inference_endpoints(namespace='*')
            report['existing_inference_endpoints'] = [{'name': x.name, 'namespace': x.namespace, 'status': x.status} for x in endpoints]
        except Exception:  # noqa: BLE001 - token-scoped API errors must not expose request details
            report['existing_inference_endpoints'] = 'Not inspectable with this token; verify in account UI.'
        save_report()
        print(json.dumps({'status': report['status'], 'links': report['links'], 'provider_mappings': 'empty at checks',
                          'account_settings': 'Reverify spend limit 0 and all providers disabled in account settings now.'}, indent=2))
    except Exception as exc:  # noqa: BLE001 - suppress potentially credential-bearing API exception text
        report['status'] = 'failed'
        # Do not log request objects, token values, or third-party exception text.
        report['failure_type'] = type(exc).__name__
        save_report()
        raise RuntimeError('Publication stopped. See the credential-free report and the local artifact checks; no token was persisted.') from None
    finally:
        token = None
        api = None


if __name__ == '__main__':
    main()