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 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Run the post-credential live proof sequence without storing secrets."""

from __future__ import annotations

import argparse
from dataclasses import dataclass
from datetime import datetime, timezone
import json
from pathlib import Path
import re
import shlex
import subprocess
import sys
from typing import Any, Callable, Sequence


ROOT = Path(__file__).resolve().parents[1]
DEFAULT_ENV_FILE = ROOT / ".env.final.local"
DEFAULT_JSON = ROOT / "docs" / "assets" / "post-credential-live-proof-plan.json"
DEFAULT_MD = ROOT / "docs" / "assets" / "post-credential-live-proof-plan.md"
SCHEMA = "proofframe.post_credential_live_proof.v1"
B2_EVIDENCE = ROOT / "docs" / "assets" / "b2-live-proof-evidence.json"
FINAL_EVIDENCE = ROOT / "docs" / "assets" / "final-live-proof-evidence.json"
FORBIDDEN_EVIDENCE_KEYS = re.compile(
    r"(?i)(api[_-]?key|application[_-]?key|authorization|cookie|password|secret|token)"
)
FORBIDDEN_EVIDENCE_VALUES = [
    re.compile(r"(?i)authorization:\s*bearer\s+[A-Za-z0-9._\-]{20,}"),
    re.compile(r"(?i)(api[_-]?key|application[_-]?key|secret|token|cookie)=[^&\s]{8,}"),
    re.compile(r"(?i)x-amz-(credential|security-token|signature)=[^&\s]{8,}"),
    re.compile(r"(?i)gmi-[A-Za-z0-9_\-]{16,}"),
]
REQUIRED_EVIDENCE_VALUES = ("asset_sha256", "manifest_sha256", "asset_storage_key", "manifest_key")
FINAL_ALLOWED_ASSET_PROVIDERS = {
    "genblaze/gmicloud-image",
    "genblaze/openai-image",
    "genblaze/local-image",
}
EXPECTED_EVIDENCE_FIELDS = {
    "b2": {
        "ok": True,
        "storage_backend": "b2",
        "generation_backend": "mock",
        "asset_storage_backend": "b2",
        "asset_provider": "mock",
        "manifest_storage_backend": "b2",
    },
    "final": {
        "ok": True,
        "storage_backend": "b2",
        "generation_backend": "genblaze",
        "asset_storage_backend": "b2",
        "manifest_storage_backend": "b2",
    },
}


@dataclass(frozen=True)
class CommandSpec:
    command_id: str
    label: str
    command: list[str]


def utc_now() -> str:
    return datetime.now(timezone.utc).replace(microsecond=0).isoformat().replace("+00:00", "Z")


def rel(path: Path) -> str:
    try:
        return str(path.resolve().relative_to(ROOT.resolve()))
    except ValueError:
        return str(path)


def display_arg(arg: str) -> str:
    path = Path(arg)
    if path.is_absolute():
        try:
            return str(path.resolve().relative_to(ROOT.resolve()))
        except ValueError:
            if path.name.startswith("python"):
                return "python"
            return path.name
    return arg


def display_command(command: Sequence[str]) -> str:
    return shlex.join(display_arg(arg) for arg in command)


def script_command(script: str, *args: str, python: str = sys.executable) -> list[str]:
    return [python, str(ROOT / "scripts" / script), *args]


def task_done_command(task_id: str, note: str, python: str = sys.executable) -> list[str]:
    return script_command("task.py", "done", task_id, "--note", note, python=python)


def validate_evidence_command(kind: str, evidence_path: Path, python: str = sys.executable) -> list[str]:
    return [
        python,
        str(ROOT / "scripts" / "post_credential_live_proof.py"),
        "--validate-evidence",
        kind,
        "--evidence-path",
        rel(evidence_path),
    ]


def build_commands(
    *,
    env_file: Path = DEFAULT_ENV_FILE,
    update_tasks: bool = False,
    python: str = sys.executable,
) -> list[CommandSpec]:
    env_arg = rel(env_file)
    commands = [
        CommandSpec(
            "credential_handoff",
            "Verify local live credential presence without printing values",
            script_command("live_env_handoff.py", "--env-file", env_arg, "--strict", python=python),
        ),
        CommandSpec(
            "b2_live_proof",
            "Capture Backblaze B2 storage proof with mock generation",
            script_command(
                "run_b2_live_proof.py",
                "--env-file",
                env_arg,
                "--evidence-out",
                "docs/assets/b2-live-proof-evidence.json",
                python=python,
            ),
        ),
        CommandSpec(
            "validate_b2_evidence",
            "Validate sanitized B2 evidence before any T020 task update",
            validate_evidence_command("b2", B2_EVIDENCE, python=python),
        ),
    ]
    if update_tasks:
        commands.append(
            CommandSpec(
                "mark_t020_done",
                "Mark T020 done after sanitized B2 evidence exists",
                task_done_command(
                    "T020",
                    "B2 live proof evidence captured in docs/assets/b2-live-proof-evidence.json.",
                    python=python,
                ),
            )
        )
    commands.append(
        CommandSpec(
            "final_live_proof",
            "Capture final B2 plus Genblaze proof",
            script_command(
                "run_final_live_proof.py",
                "--env-file",
                env_arg,
                "--evidence-out",
                "docs/assets/final-live-proof-evidence.json",
                python=python,
            ),
        )
    )
    commands.append(
        CommandSpec(
            "validate_final_evidence",
            "Validate sanitized final B2 plus Genblaze evidence before any T021 task update",
            validate_evidence_command("final", FINAL_EVIDENCE, python=python),
        )
    )
    if update_tasks:
        commands.append(
            CommandSpec(
                "mark_t021_done",
                "Mark T021 done after sanitized final evidence exists",
                task_done_command(
                    "T021",
                    "Final B2 plus Genblaze live proof evidence captured in docs/assets/final-live-proof-evidence.json.",
                    python=python,
                ),
            )
        )
    commands.extend(
        [
            CommandSpec(
                "live_env_handoff_report",
                "Regenerate live credential handoff report",
                script_command("live_env_handoff.py", "--env-file", env_arg, python=python),
            ),
            CommandSpec("devpost_form_kit", "Regenerate Devpost form kit", script_command("devpost_form_kit.py", python=python)),
            CommandSpec(
                "devpost_submission_checklist",
                "Regenerate Devpost submission checklist",
                script_command("devpost_submission_checklist.py", python=python),
            ),
            CommandSpec("judge_brief", "Regenerate judge brief", script_command("judge_brief.py", python=python)),
            CommandSpec("judge_crosswalk", "Regenerate judge crosswalk", script_command("judge_crosswalk.py", python=python)),
            CommandSpec("demo_storyboard", "Regenerate demo storyboard", script_command("demo_storyboard.py", python=python)),
            CommandSpec("demo_readiness", "Regenerate demo readiness report", script_command("demo_readiness.py", python=python)),
            CommandSpec(
                "recording_assets",
                "Regenerate recording assets report and verify public mock assets",
                script_command("recording_assets.py", "--verify-public", python=python),
            ),
            CommandSpec(
                "award_readiness",
                "Regenerate award readiness scorecard",
                script_command("award_readiness.py", "--min-score", "75", python=python),
            ),
            CommandSpec(
                "final_operator_brief",
                "Regenerate final operator brief",
                script_command("final_operator_brief.py", python=python),
            ),
            CommandSpec(
                "final_launch_plan",
                "Regenerate final launch plan",
                script_command("final_launch_plan.py", python=python),
            ),
            CommandSpec(
                "final_rehearsal",
                "Regenerate final rehearsal checklist",
                script_command("final_rehearsal.py", python=python),
            ),
            CommandSpec(
                "final_submission_control",
                "Regenerate final submission control",
                script_command("final_submission_control.py", python=python),
            ),
            CommandSpec(
                "submission_audit",
                "Regenerate pre-submit audit report",
                script_command("submission_audit.py", python=python),
            ),
            CommandSpec(
                "devpost_submission_preview",
                "Regenerate one-page Devpost submission preview",
                script_command("devpost_submission_preview.py", python=python),
            ),
            CommandSpec("secret_scan", "Run no-value secret scan", script_command("secret_scan.py", python=python)),
            CommandSpec("submission_bundle", "Regenerate submission bundle manifest", script_command("submission_bundle.py", python=python)),
        ]
    )
    return commands


def load_evidence(path: Path) -> tuple[dict[str, Any] | None, str | None]:
    try:
        evidence = json.loads(path.read_text(encoding="utf-8"))
    except FileNotFoundError:
        return None, "Evidence file is missing."
    except json.JSONDecodeError as exc:
        return None, f"Evidence file is invalid JSON: {exc}"
    if not isinstance(evidence, dict):
        return None, "Evidence JSON must be an object."
    return evidence, None


def evidence_safety_findings(value: Any, path: str = "$") -> list[str]:
    findings: list[str] = []
    if isinstance(value, dict):
        for key, item in value.items():
            key_path = f"{path}.{key}"
            if FORBIDDEN_EVIDENCE_KEYS.search(str(key)):
                findings.append(f"{key_path}: forbidden evidence key")
            findings.extend(evidence_safety_findings(item, key_path))
    elif isinstance(value, list):
        for index, item in enumerate(value):
            findings.extend(evidence_safety_findings(item, f"{path}[{index}]"))
    elif isinstance(value, str):
        for pattern in FORBIDDEN_EVIDENCE_VALUES:
            if pattern.search(value):
                findings.append(f"{path}: forbidden evidence value")
                break
    return findings


def validate_evidence(kind: str, path: Path) -> dict[str, Any]:
    evidence, load_error = load_evidence(path)
    findings: list[dict[str, str]] = []
    if load_error:
        findings.append({"field": "file", "detail": load_error})
        evidence = {}

    expected = EXPECTED_EVIDENCE_FIELDS[kind]
    for key, expected_value in expected.items():
        actual = evidence.get(key)
        if actual != expected_value:
            findings.append({"field": key, "detail": f"Expected {expected_value!r}, got {actual!r}."})
    if kind == "final":
        asset_provider = evidence.get("asset_provider")
        if asset_provider not in FINAL_ALLOWED_ASSET_PROVIDERS:
            findings.append(
                {
                    "field": "asset_provider",
                    "detail": (
                        "Expected one of "
                        f"{sorted(FINAL_ALLOWED_ASSET_PROVIDERS)!r}, got {asset_provider!r}."
                    ),
                }
            )
    for key in REQUIRED_EVIDENCE_VALUES:
        if not evidence.get(key):
            findings.append({"field": key, "detail": "Required evidence value is missing."})
    for finding in evidence_safety_findings(evidence):
        findings.append({"field": "secret_safety", "detail": finding})

    return {
        "ok": not findings,
        "kind": kind,
        "path": rel(path),
        "checks": {
            "expected_fields": expected,
            "allowed_final_asset_providers": sorted(FINAL_ALLOWED_ASSET_PROVIDERS),
            "required_values": list(REQUIRED_EVIDENCE_VALUES),
            "secret_safety": "forbidden keys, bearer tokens, signed URL parameters, and GMI-style keys",
        },
        "findings": findings,
    }


def command_record(spec: CommandSpec, status: str, returncode: int | None = None) -> dict[str, Any]:
    return {
        "id": spec.command_id,
        "label": spec.label,
        "status": status,
        "returncode": returncode,
        "command": display_command(spec.command),
        "argv": [display_arg(arg) for arg in spec.command],
    }


def run_sequence(
    commands: Sequence[CommandSpec],
    *,
    execute: bool,
    runner: Callable[..., subprocess.CompletedProcess[Any]] = subprocess.run,
) -> dict[str, Any]:
    records: list[dict[str, Any]] = []
    if not execute:
        return {
            "ok": True,
            "mode": "plan_only",
            "commands": [command_record(spec, "planned") for spec in commands],
            "failed_command": None,
        }

    failed_command: str | None = None
    for index, spec in enumerate(commands):
        completed = runner(spec.command, cwd=ROOT, check=False)
        records.append(command_record(spec, "passed" if completed.returncode == 0 else "failed", completed.returncode))
        if completed.returncode != 0:
            failed_command = spec.command_id
            for skipped in commands[index + 1 :]:
                records.append(command_record(skipped, "skipped"))
            break
    return {
        "ok": failed_command is None,
        "mode": "executed" if failed_command is None else "failed",
        "commands": records,
        "failed_command": failed_command,
    }


def build_report(args: argparse.Namespace, sequence: dict[str, Any]) -> dict[str, Any]:
    next_actions = [
        "If mode is plan_only, rerun with --execute after credentials are entered locally.",
        "If live proof succeeds, record and upload the final public demo video.",
        "After the public video is verified, run the final secret scan, strict audit, Devpost submit, and receipt capture.",
    ]
    if sequence.get("failed_command") == "credential_handoff":
        next_actions.insert(
            0,
            "Enter missing local credentials with python scripts/final_env_wizard.py --output .env.final.local --missing-only --force.",
        )
    return {
        "schema": SCHEMA,
        "created_at": utc_now(),
        "ok": sequence["ok"],
        "mode": sequence["mode"],
        "env_file": rel(args.env_file),
        "update_tasks": bool(args.update_tasks),
        "execute": bool(args.execute),
        "commands": sequence["commands"],
        "failed_command": sequence["failed_command"],
        "next_actions": next_actions,
        "secret_policy": (
            "This report stores command strings, statuses, and artifact paths only. It never stores "
            "Backblaze keys, Genblaze provider keys, Devpost cookies, provider responses, or signed URLs."
        ),
    }


def render_markdown(report: dict[str, Any]) -> str:
    lines = [
        "# ProofFrame Post-Credential Live Proof Plan",
        "",
        f"Mode: `{report['mode']}`",
        f"OK: `{str(report['ok']).lower()}`",
        f"Created: `{report['created_at']}`",
        f"Env file: `{report['env_file']}`",
        f"Update tasks: `{str(report['update_tasks']).lower()}`",
        "",
        report["secret_policy"],
        "",
        "## Commands",
        "",
        "| Status | ID | Command |",
        "| --- | --- | --- |",
    ]
    for command in report["commands"]:
        lines.append(f"| {command['status'].upper()} | `{command['id']}` | `{command['command']}` |")
    lines.extend(["", "## Next Actions", ""])
    lines.extend(f"- {action}" for action in report["next_actions"])
    return "\n".join(lines) + "\n"


def write_outputs(report: dict[str, Any], json_path: Path, markdown_path: Path) -> None:
    json_path.parent.mkdir(parents=True, exist_ok=True)
    markdown_path.parent.mkdir(parents=True, exist_ok=True)
    json_path.write_text(json.dumps(report, indent=2) + "\n", encoding="utf-8")
    markdown_path.write_text(render_markdown(report), encoding="utf-8")


def build_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(
        description="Plan or run the post-credential ProofFrame live proof sequence."
    )
    parser.add_argument("--env-file", type=Path, default=DEFAULT_ENV_FILE)
    parser.add_argument("--json-out", type=Path, default=DEFAULT_JSON)
    parser.add_argument("--markdown-out", type=Path, default=DEFAULT_MD)
    parser.add_argument("--execute", action="store_true", help="Run the live proof sequence.")
    parser.add_argument(
        "--update-tasks",
        action="store_true",
        help="Mark T020/T021 done after their proof commands pass.",
    )
    parser.add_argument(
        "--validate-evidence",
        choices=sorted(EXPECTED_EVIDENCE_FIELDS),
        help="Internal fail-closed evidence validator used before task updates.",
    )
    parser.add_argument("--evidence-path", type=Path, help="Evidence JSON path for --validate-evidence.")
    return parser


def main() -> None:
    args = build_parser().parse_args()
    if args.validate_evidence:
        evidence_path = args.evidence_path
        if evidence_path is None:
            evidence_path = B2_EVIDENCE if args.validate_evidence == "b2" else FINAL_EVIDENCE
        report = validate_evidence(args.validate_evidence, evidence_path)
        print(json.dumps(report, indent=2))
        raise SystemExit(0 if report["ok"] else 2)

    commands = build_commands(env_file=args.env_file, update_tasks=args.update_tasks)
    sequence = run_sequence(commands, execute=args.execute)
    report = build_report(args, sequence)
    write_outputs(report, args.json_out, args.markdown_out)
    print(
        json.dumps(
            {
                "ok": report["ok"],
                "mode": report["mode"],
                "json": str(args.json_out),
                "markdown": str(args.markdown_out),
                "failed_command": report["failed_command"],
                "commands": len(report["commands"]),
            },
            indent=2,
        )
    )
    if args.execute and not report["ok"]:
        raise SystemExit(2)


if __name__ == "__main__":
    main()