#!/usr/bin/env python3 # SPDX-License-Identifier: MIT OR Apache-2.0 """The JSON-schema grammar oracle (O6b): llama.cpp b11192's own `json_schema_to_grammar` and the grammar llama-server builds for a response-format schema on the jinja chat path, both called inside the b11192 release's `libllama-common.so` (`testing/schema_oracle.cpp`; no model is loaded, no server runs). The template is read from the pinned model's GGUF metadata (Bonsai / Qwen3, sha256 30a75d10e60b57e2…); the request is fed as `oaicompat_chat_params_parse` feeds it with Savante's flags (`--jinja --reasoning off`: thinking off, reasoning format deepseek, an empty schema made `{"type": "object"}`). The corpus: llama.cpp's own `tests/test-json-schema-to-grammar.cpp` cases (`testing/json_schema_cases.json`, 81) and `testing/json_schema_corpus.json` (Pydantic v2 schemas shaped like mindX's models — nested `$defs`/`$ref`, Optional, Literal, enums, constrained ints and strings, formats, tuples, recursion, `extra="forbid"`/`"allow"` — and edge cases: odd property names, duplicate keys, float literals, every refusal path of the schema reader and the pattern translator, non-object schemas). Each record keeps the grammar or the error of both paths. LLAMA_SRC= BANKML_GGML_LIB= python3 testing/schema_oracle.py → .models/oracle-schema/schemas.jsonl, replayed by `oracle_schema_grammars` (cargo test --release -- --ignored)""" import json, os, struct, subprocess, sys from pathlib import Path root = Path(__file__).resolve().parents[1] out = root / ".models" / "oracle-schema" src = Path(os.environ.get("LLAMA_SRC", "")) lib = Path(os.environ.get("BANKML_GGML_LIB", "")) if not (src / "common" / "chat.h").exists() or not (lib / "libllama-common.so").exists(): sys.exit("set LLAMA_SRC (llama.cpp source at tag b11192) and BANKML_GGML_LIB (the b11192 release dir)") # 0.3.5: every reproduced template, each read from a model that carries it → its own record file if os.environ.get("BANKML_ORACLE_MODEL"): jobs = [(Path(os.environ["BANKML_ORACLE_MODEL"]), "schemas.jsonl")] else: bonsai = next((m for m in (root / ".models" / n for n in ("Bonsai-8B-Q1_0.gguf", "Bonsai-1.7B-Q1_0.gguf")) if m.exists()), None) \ or sys.exit("no pinned Bonsai model to read the chat template from (BANKML_ORACLE_MODEL)") jobs = [(bonsai, "schemas.jsonl")] + [(root / ".models" / f"{s}.gguf", f"schemas-{s}.jsonl") for s in ("SmolLM2-135M-Instruct-F16", "mindx-gen39-F16") if (root / ".models" / f"{s}.gguf").exists()] def chat_template(path): """tokenizer.chat_template from the GGUF header (metadata only; the tensors are not read)""" f = open(path, "rb") u32 = lambda: struct.unpack("