File size: 14,574 Bytes
3397fcb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Whitelist-only export of the Coder390/Fable MLX and deployment study.

Reads private raw evidence under the lab and writes finite, path-free JSON.
Never copies prompts, tool output, repository source, transcripts, credentials
or local absolute paths into the public artifact.
"""
from __future__ import annotations

import hashlib
import json
import math
import re
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parents[3]  # real-agent-lab
LOCAL = ROOT / "coder390" / "mlx-native" / "raw"
FABLE = ROOT / "fable" / "deployment" / "raw"
GUARD = ROOT / "fable" / "memory-guard-fix" / "raw"

RUN_ID = re.compile(r"^[A-Za-z0-9_\-.]{1,120}$")
DENY = ("/Users/", "/Volumes/", "hf_", "sk-", "Bearer ", "api-key", "api_key", "token=", "@")
PRIVATE_BLOB_MARKERS = ("/Users/", "/Volumes/", "sk-", "hf_", "Bearer ", "api-key", "api_key")


def assert_public(blob):
    """Refuse any serialized artifact carrying a private marker."""
    for bad in PRIVATE_BLOB_MARKERS:
        if bad in blob:
            raise ValueError(f"refusing to export private marker: {bad}")
    return blob


def finite(value, label):
    if value is None:
        return None
    if type(value) is bool:
        raise ValueError(f"{label}: boolean where number expected")
    if type(value) not in (int, float) or not math.isfinite(value):
        raise ValueError(f"{label}: non-finite numeric metric")
    return value


def flag(value, label):
    if value is None:
        return None
    if type(value) is not bool:
        raise ValueError(f"{label}: non-boolean flag")
    return value


def text(value, label, limit=200):
    if value is None:
        return None
    if not isinstance(value, str):
        raise ValueError(f"{label}: not a string")
    if any(bad in value for bad in DENY):
        raise ValueError(f"{label}: contains a private marker")
    return value[:limit]


def read(rel, base=LOCAL):
    path = base / rel if not str(rel).startswith("/") else Path(rel)
    if not path.is_file():
        raise FileNotFoundError(str(rel))
    return path, path.read_bytes()


def load(rel, base=LOCAL):
    _, raw = read(rel, base)
    return json.loads(raw)


def digest(rel, base=LOCAL):
    _, raw = read(rel, base)
    return hashlib.sha256(raw).hexdigest()


def windows(base, prefix):
    """Summarize owned inference-window receipts without local paths."""
    out = []
    for path in sorted(base.glob(prefix + "*/window.json")):
        row = json.loads(path.read_text())
        name = path.parent.name
        if not RUN_ID.match(name):
            raise ValueError("unsafe window name")
        out.append({
            "window": name,
            "restored": flag(row.get("restored"), "window.restored"),
            "worker_returncode": finite(row.get("worker_returncode"), "window.returncode"),
            "timed_out": flag(row.get("timed_out"), "window.timed_out"),
            "blocked_before_unload": row.get("phase") == "blocked_before_unload",
        })
    return out


def trial_rows(record):
    rows = []
    for trial in record.get("trials", []):
        if not isinstance(trial, dict):
            continue
        label = text(trial.get("label") or trial.get("arm") or "trial", "trial.label", 80)
        spec = trial.get("spec_message") or trial.get("speculative_stats") or ""
        rows.append({
            "label": label,
            "prompt_tokens": finite(trial.get("prompt_tokens"), "trial.prompt_tokens"),
            "generated_tokens": finite(
                trial.get("generation_tokens_including_reasoning_and_tool")
                or trial.get("num_tokens"), "trial.generated_tokens"),
            "client_ttft_s": finite(trial.get("client_ttft_s") or trial.get("first_text_ttft_s"), "trial.ttft"),
            "whole_request_s": finite(trial.get("whole_request_s") or trial.get("measured_whole_request_s"), "trial.wall"),
            "decode_tps": finite(trial.get("decode_tps") or trial.get("reported_generation_tps"), "trial.decode"),
            "prompt_tps": finite(trial.get("reported_prompt_tps"), "trial.prompt_tps"),
            "prefill_s": finite(trial.get("prefill_seconds"), "trial.prefill"),
            "finish_reason": text(trial.get("finish_reason"), "trial.finish", 24),
            "mean_accept_len": finite(trial.get("mean_accept_len") or trial.get("mean_accept"), "trial.accept"),
            "speculative": bool(spec) if spec is not None else None,
            "peak_memory_gb": finite(trial.get("peak_memory_gb_decimal") or trial.get("peak_memory_gb"), "trial.peak"),
        })
    return rows


def build():
    summary = load("continued-acceleration-reviewed.json")
    mlx = load("mlx-trial-reviewed-summary.json")
    conversion = load("output-verification.json")
    parity = load("real-tensor-parity.json")
    layout = load("strict-module-layout.json")
    tokproof = load("tokenizer-processor-proof.json")
    mtp = load("mtp-verification.json")
    source_layout = load("source-layout-audit.json")
    cpu = load("cpu-arithmetic-proof.json")
    audit = load("final-trial-service-audit.json")
    contention = load("contention-timing.json")

    cutover = load("cutover-receipt.json", FABLE)
    final_cutover = load("final-cutover-audit.json", FABLE)
    cleanup = load("retired-abliterated-cleanup-receipt.json", FABLE)
    image_handoff = load("image-handoff-proof.json", FABLE)
    switch = load("normal-and-fable-switch-proof.json", FABLE)
    live_api = load("live-api-proof.json", FABLE)
    live_svc = load("live-service-proof.json", FABLE)
    mem_policy = load("memory-policy-before.json", FABLE)
    mem_refusal = load("pi-prefill128-proof.json", FABLE)
    final_audit = load("final-cutover-audit.json", FABLE)

    guard_state = load("final-state.json", GUARD)
    guard_protocol = load("protocol-proof.json", GUARD)
    guard_stream = load("stream-protocol-proof.json", GUARD)
    guard_pi = load("pi-full-route-proof.json", GUARD)

    # Per-condition synthetic 8k comparisons, in declared arm order.
    conditions = []
    for name, note in [
        ("vlm-bf16-8k-mtp-r2", "official mlx-vlm, source MTP depth 3"),
        ("dspark-dflash-8k-cpuoff-r3", "mlx-dspark 0.19.0 DFlash2, cpu co-prefill off"),
        ("dspark0203-dflash-8k-r5", "mlx-dspark 0.20.3 DFlash2, newer kernels"),
        ("dspark019-dflash-8k-prefill2048-r6", "mlx-dspark 0.19.0 DFlash2, 2048-row prefill"),
    ]:
        rec = load(f"{name}/results.json")
        conditions.append({
            "condition": name,
            "note": note,
            "engine": text(rec.get("engine") or rec.get("runtime"), "engine", 120),
            "input_tokens": finite(rec.get("actual_input_tokens") or
                                   (rec.get("trials") or [{}])[0].get("prompt_tokens"), "input_tokens"),
            "prefill_chunk": finite(rec.get("prefill_chunk"), "prefill_chunk"),
            "draft_cap": finite(rec.get("derived_draft_cap"), "draft_cap"),
            "greedy_token_parity": flag(rec.get("greedy_token_parity"), "parity"),
            "trials": trial_rows(rec),
        })

    # Actual agent repairs with speculation (private clones, sanitized scalars).
    repairs = []
    for name in ("mtp-bf16-real-repair-r3", "dflash-bf16-real-repair-r4"):
        rec = load(f"{name}/repair.summary.json")
        turns = rec.get("turns") or []
        repairs.append({
            "run": name,
            "speculation": text(rec.get("speculation"), "speculation", 24),
            "trained_mtp": flag(rec.get("trained_mtp"), "trained_mtp"),
            "completed": flag(rec.get("completed"), "completed"),
            "elapsed_s": finite(rec.get("elapsed_s"), "elapsed_s"),
            "timed_out": flag(rec.get("timed_out"), "timed_out"),
            "tool_calls": finite(rec.get("tool_calls"), "tool_calls"),
            "tool_errors": finite(rec.get("tool_errors"), "tool_errors"),
            "independent_gate_pass": flag(rec.get("independent_gate_pass"), "gate"),
            "full_suite_pass": flag(rec.get("full_suite_pass"), "suite"),
            "source_restored": flag(rec.get("source_restored"), "restored"),
            "context_capacity_configured": finite(rec.get("context_capacity_configured"), "context"),
            "actual256k_qualified": flag(rec.get("actual256k_qualified"), "actual256k"),
            "initial_input_tokens": finite(turns[0]["usage"]["input"], "first_input") if turns else None,
            "initial_ttft_s": finite(turns[0].get("ttft_s"), "first_ttft") if turns else None,
            "turn_decode_tps": [finite(t.get("decode_tps"), "turn decode") for t in turns],
        })

    artifact = {
        "schema_version": 1,
        "title": "Fable MLX migration and native memory-policy study (sanitized evidence)",
        "authors": ["Chengyi Xu", "Nexora"],
        "brand": "Nexora Research",
        "scope": (
            "Descriptive deployment engineering on one Apple M4 Max 64 GiB host. "
            "Whitelisted finite scalars only; no prompts, tool output, repository "
            "source, transcripts, credentials or local absolute paths."
        ),
        "qualification": {
            "minimum_20_tps_met": False,
            "actual_near_limit_context_met": False,
            "peer_review_claimed": False,
            "ranked_intelligence_claimed": False,
        },
        "checkpoint": {
            "conversion": conversion,
            "nonquantized_tensor_parity": parity.get("raw_tensor_full_element_parity"),
            "quantized_rows_verified": parity.get("quantized_linears_sampled"),
            "module_layout": {k: layout.get(k) for k in
                              ("module_parameters", "checkpoint_tensors", "missing", "extra")},
            "tokenizer": {k: tokproof.get(k) for k in
                          ("tokenizer_parity_cases", "chat_template_parity", "vocab_size")},
            "mtp_tensors": len(mtp.get("files", {})),
            "source_tensor_categories": source_layout.get("categories_source_bytes"),
            "cpu_arithmetic": {
                "probes": cpu.get("probes"),
                "full_model_loaded": cpu.get("full_model_loaded"),
            },
        },
        "synthetic_conditions": conditions,
        "speculative_repairs": repairs,
        "reviewed_summary": {
            "smoke_plain_tps": mlx["smoke"]["plain_generation_tps"],
            "smoke_mtp_tps": mlx["smoke"]["mtp_generation_tps"],
            "smoke_greedy_parity": mlx["smoke"]["greedy_token_parity"],
            "short_input_tokens": 74,
            "repair_backend_metrics": mlx.get("actual_repair_backend_metrics"),
            "all_windows_restored": mlx.get("all_executed_windows_restored"),
        },
        "memory_policy": {
            "observed": {
                "kernel_free_target_bytes": mem_policy["kernel_free_target_bytes"],
                "floors": mem_policy["floors"],
                "compressor_bytes": mem_policy["live_reading"]["compressor_bytes"],
                "available_bytes": mem_policy["live_reading"]["available_bytes"],
                "free_bytes": mem_policy["live_reading"]["free_bytes"],
            },
            "refusal_before_switch": {
                "completed": mem_refusal.get("completed"),
                "prefill_chunk": mem_refusal.get("prefill_chunk"),
                "error_class": "runtime prefill system-memory guard",
            },
            "after_supported_switch_off": {
                "same_input_tokens": guard_pi["messages"][0]["usage"]["input"] if guard_pi.get("messages") else None,
                "completed": guard_pi.get("completed"),
                "elapsed_s": guard_pi.get("elapsed_s"),
                "exact_marker": guard_pi["messages"][0]["text"] if guard_pi.get("messages") else None,
                "prompt_reduced": False,
            },
            "effective_child_setting": guard_state["native_child_env"],
        },
        "protocol_matrix": {
            "non_streaming": [{"endpoint": r["endpoint"], "protocol": r["protocol"],
                               "status": r["status"], "passed": r["passed"]}
                              for r in guard_protocol["results"]],
            "streaming": [{"endpoint": r["endpoint"], "protocol": r["protocol"],
                           "status": r["status"], "terminal_received": r["terminal_received"],
                           "passed": r["passed"]} for r in guard_stream],
        },
        "deployment": {
            "cutover_ready": cutover.get("fable_ready"),
            "unrelated_providers_identical": cutover.get("unrelated_provider_records_identical"),
            "context_window": final_cutover.get("context_window"),
            "mtp_depth": final_cutover.get("mtp_depth"),
            "runtime_profile": final_cutover.get("runtime_profile"),
            "prefill_chunk": final_cutover.get("prefill_chunk"),
            "live_backend_reply_ok": bool(live_api.get("backend_answer")),
            "live_gateway_reply_ok": bool(live_api.get("magpie_answer")),
            "service_idle_verified": live_svc.get("idle_verified"),
            "image_handoff_restored": image_handoff.get("restored"),
            "normal_switch_verified": switch.get("normal_start_verified"),
            "retired_logical_bytes": cleanup.get("logical_bytes"),
            "retired_entry_count": len(cleanup.get("deleted", [])),
        },
        "windows_restored": {
            "coder390_mlx": windows(LOCAL, ""),
            "fable_deployment": windows(FABLE, ""),
        },
        "contention": {
            "external_start_estimated_during_window": contention.get(
                "external_start_estimated_during_window"),
            "exclusive_execution_proven": contention.get("exclusive_execution_proven"),
        },
        "final_state": {
            "fable_promoted": audit.get("candidate_promoted"),
            "near_limit_qualified": final_audit.get("near_limit_context_qualified"),
            "trial_ports_closed": final_audit.get("experiment_ports_closed") or
                                 audit.get("lab_ports_closed"),
        },
    }

    # Every string is already vetted by text(); assert no absolute path escaped.
    blob = json.dumps(artifact, indent=2, sort_keys=False)
    return assert_public(blob), artifact


def main():
    dest = Path(sys.argv[1])
    try:
        blob, _artifact = build()
    except ValueError as error:
        raise SystemExit(str(error)) from None
    dest.parent.mkdir(parents=True, exist_ok=True)
    dest.write_text(blob + "\n")
    print(f"wrote {dest}")


if __name__ == "__main__":
    main()