betterwithage Claude Opus 4.7 commited on
Commit
85c9da3
·
verified ·
1 Parent(s): d5ed552

deploy(hf): sync szl-holdings/a11oy@bb5447180fc63184a6c6e02450477e02f9e46251 derived COPY set

Browse files

Reusable Dockerfile-COPY-derived deploy from szl-holdings/a11oy bb5447180fc63184a6c6e02450477e02f9e46251.
Files: 1355 Pruned: 0
Derived from Dockerfile COPY sources (NO hand-maintained allowlist).

Signed-off-by: SZL Holdings <noreply@szlholdings.ai>
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

Dockerfile CHANGED
@@ -18,52 +18,34 @@
18
  # HF Space requirement: listen on PORT 7860.
19
 
20
  # ---------------------------------------------------------------------------
21
- # IMAGE-LEANNESS: multi-stage build. The CPU demo tier needs llama-cpp-python
22
- # compiled FROM SOURCE against glibc (see the long WHY note above the runtime
23
- # install far below). That compile pulls in a heavy build toolchain
24
- # (build-essential/cmake/ninja/git hundreds of MB of apt .deb churn) plus
25
- # compile intermediates. Doing it in a THROWAWAY builder stage and copying only
26
- # the resulting prebuilt wheel keeps all of that OUT of the published runtime
27
- # image, shrinking the final image + its layer count (faster GHCR pulls) without
28
- # changing what the demo tier serves.
29
- #
30
- # CONDITIONAL COMPILE: a constrained builder (HF Spaces cpu-basic) sets
31
- # A11OY_REQUIRE_LOCAL_LLM=0 and must NOT pay the heavy compile (it OOM/timed-out
32
- # -> BUILD_ERROR). Select the builder by ARG: =1 -> llama-build-1 (real source
33
- # compile), else -> llama-build-0 (empty, no compile). BuildKit only builds the
34
- # stage actually referenced by the `llama-build` alias, so on the constrained
35
- # path the compile is skipped entirely. The strict GHCR build sets =1.
36
- ARG A11OY_REQUIRE_LOCAL_LLM=0
37
-
38
- # Real compile path (=1): build the pinned llama-cpp-python from source into a
39
- # prebuilt glibc wheel, then assert the bundled libllama.so links glibc
40
- # (NEEDs libc.so.6), not musl. set -eux => a bad compile fails the build LOUD.
41
  FROM python:3.12-slim@sha256:423ed6ab25b1921a477529254bfeeabf5855151dc2c3141699a1bfc852199fbf AS llama-build-1
42
- RUN set -eux; \
43
- apt-get update; \
44
- apt-get install -y --no-install-recommends build-essential cmake ninja-build git; \
45
- CMAKE_ARGS="-DGGML_NATIVE=OFF" pip wheel --no-cache-dir --no-binary llama-cpp-python \
46
- --wheel-dir=/wheels "llama-cpp-python==0.3.35"
47
- RUN python3 <<'GLIBCCHK'
48
- import glob, sys, zipfile
49
- whls = glob.glob("/wheels/llama_cpp_python-*.whl")
50
- assert whls, "no llama_cpp_python wheel produced by the source build"
51
- w = whls[0]
52
- z = zipfile.ZipFile(w)
53
- sos = [n for n in z.namelist() if n.endswith("libllama.so")]
54
- assert sos, "libllama.so not present inside the built wheel " + w
55
- data = z.read(sos[0])
56
- assert b"libc.so.6" in data and b"libc.musl-x86_64.so.1" not in data, \
57
- "built libllama.so is not glibc-linked (would not load on python:3.12-slim): " + sos[0]
58
- print("[a11oy] built glibc wheel OK:", w, "->", sos[0])
59
- GLIBCCHK
60
-
61
- # Skip path (!=1): no compile; just an empty wheel dir so the runtime
62
- # COPY --from has a valid (empty) source on the constrained build.
63
  FROM python:3.12-slim@sha256:423ed6ab25b1921a477529254bfeeabf5855151dc2c3141699a1bfc852199fbf AS llama-build-0
64
  RUN mkdir -p /wheels
65
 
66
- # Pick the builder the runtime stage actually copies from.
67
  FROM llama-build-${A11OY_REQUIRE_LOCAL_LLM} AS llama-build
68
 
69
  # ---------------------------------------------------------------------------
@@ -116,6 +98,12 @@ RUN pip install --no-cache-dir \
116
  "defusedxml==0.7.1" \
117
  "numpy==2.5.2"
118
 
 
 
 
 
 
 
119
  # sqlite-vss removed from build: no pre-built wheel for python:3.12-slim;
120
  # szl_khipu_lmdb.py and szl_unay.py already have honest try/except fallback
121
  # to cosine similarity if the sqlite-vss .so cannot load. (P0 CI fix, Dev1 Rumi)
@@ -294,6 +282,7 @@ COPY formula_registry/formula-registry.v1.json ./formula_registry/formula-regist
294
  COPY content_credentials.py ./
295
  COPY schemas/quant-claims/ ./schemas/quant-claims/
296
  COPY OUROBOROS_RUN_ALL.py ./
 
297
  COPY knowledge.json szl_parity_gaps.py compliance_crosswalk.py szl_compliance_mesh.py a11oy_warhacker_obs.py serve.py szl_governed_api.py szl_demo_tier1.py szl_assurance.py govern_showcase.html a11oy_wireA_metrics.py cathedral.html a11oy_operator_organ.py a11oy_hf_assets.py szl_b2_secdata.py gates_manifest.json a11oy_code_orchestrator.py a11oy_agent_loop.py a11oy_org_rag.py a11oy_mcp_client.py szl_rag.py a11oy_code_ide.html wayra_serve.py wayra_snapshot.json wayra_digests_7d.json szl_khipu_os_routes.py szl_spaces_proxy.py szl_spaces_surface.py szl_khipu_consensus.py szl_puriq_formulas.py ayni_os_serve.py szl_live_wires.py live_wires.html live_wires_3d.js szl_intoto.py szl_intoto_routes.py szl_scitt.py szl_dsse.py szl_content_address.py szl_provenance.py szl_be_hardening.py szl_unay.py szl_khipu_lmdb.py szl_khipu_replicate.py szl_unay_routes.py szl_warhacker_aliases.py a11oy_v4_hickok.py szl_khipu.py szl_formulas.py a11oy_v4_formulas.py szl_anatomy_3d.py szl_anatomy_routes.py _vendor_blobs.py szl_v4_fleet.py operator_shell_v4.py szl_bridge.py szl_bridge_schemas.py agent.html a11oy_bridge_cli.py szl_ken.py a11oy_formula_endpoints.py a11oy_formula_registry_guard.py a11oy_formulas_page.py a11oy_frontier_patch.py a11oy_v4_agent.py szl_brain.py szl_wire.py szl_hub.py szl_rosie_companion.py szl_receipt_substrate.py szl_alloy_embed_fabric.py szl_ayni_quorum.py szl_agentic_loop.py szl_ltc_dynamics.py szl_sgh_scheduler.py szl_formula_wiring.py szl_formula_surfaces.py a11oy_code_engine.py a11oy_code_runloop.py a11oy_code.py a11oy_seismic.py szl_warhacker_real.py szl_warhacker_demos.py NOTICE_warhacker_demos.txt szl_llm_registry.py szl_elite_console.py szl_alloy_models.py szl_scaling.py szl_allodial.py szl_entanglement.py szl_neuroplasticity.py szl_neuromorphic.py szl_kan.py szl_titans.py szl_mor.py szl_ternary.py szl_agentmem.py szl_edgefusion.py szl_hybridssm.py szl_aigov.py szl_chain_of_title.py szl_sovereign_compute.py szl_a11oy_interpretability.py a11oy_active_flux_router.py szl_energy_budget.py szl_energy_sovereign.py szl_energy_provenance.py szl_heart_blood.py szl_engine_status.py szl_backend_hardening.py revenue_endpoints.py a11oy_harvest_endpoints.py szl_energy_measured.py joule_billing.py szl_durable_ledger.py szl_energy_ledger_recovery.py szl_energy_ledger.py szl_energy_operator.py szl_energy_projection.py szl_cheapest_watt.py szl_energy_live.py szl_orbital_topology.py szl_orbital_projection.py a11oy_orbital_page.py a11oy_frontier_page.py szl_frontier_manifest.py szl_frontier_zkinfer.py szl_frontier_fmverif.py szl_frontier_supplychain.py a11oy_code_as_action.py a11oy_governed_kernel.py szl_lambda_tripwire.py szl_provenance_receipt.py szl_khipu_verify.py szl_public_verify.py szl_attest_stack.py szl_demo_sign.py szl_sda.py szl_fabric_surface.py szl_nemo_agents.py szl_kverify.py szl_specdec.py szl_immune.py szl_quant_qbio_holo.py szl_materials.py szl_materials_predict.py a11oy_factory.py a11oy_constitution.py a11oy_nav_wireup.py szl_mbse_cosim.py szl_mbse_nav.py szl_mbse.py szl_factory.py szl_willay_gateway.py a11oy_willay_nav.py szl_waqay.py a11oy_waqay_nav.py szl_yupay.py a11oy_yupay_nav.py a11oy_uds_portability_nav.py szl_pinn_bounds.py szl_pinn_residual.py physical_bounds_certificate.json agentic_decision_trail.json physical_bounds_certificate.dsse.json szl_pinn_inverse.py szl_governed_ipinn.py szl_calphad_inverse.py szl_pnt_mesh.py quantum_sensing_limits.py pnt_resilience.py nav_coasting.py fundamental_limits.py szl_counter_uas_proxy.py szl_gpu_quant.py szl_joules_truth.py revenue_model.py szl_prod_hardening.py szl_resilience.py szl_observability.py szl_corpus_publish.py szl_lake_store.py szl_lake_ingest.py szl_e8.py szl_trajectory_sign.py szl_nemotron_ingest.py szl_nemotron_corpus.py szl_nemo_verify.py a11oy_nemo_core.py szl_restraint.py szl_sapa.py szl_sapa_patch.py szl_restraint_energy.py a11oy_react_core.py szl_org_lambda.py a11oy_canonical_domain.py a11oy_formula_tiers.py szl_physical_bounds.py szl_kc_loop_forge.py szl_kc_loop_forge_metrics.py szl_kc_atlas.py szl_eval_arena.py szl_vqc.py szl_kc_jpt.py ./
298
 
299
  # Wave M / Dev 4: Sovereign Local Model panel — imported GUARDED by serve.py
@@ -374,7 +363,7 @@ COPY src/a11oy/harvest/__init__.py src/a11oy/harvest/wasted_energy_harvest.py sr
374
 
375
  # LIVE CPU demo tier: install llama.cpp + fetch ONE tiny Apache-2.0 GGUF
376
  # … (full rationale: docs/DOCKERFILE_NOTES.md §50)
377
- ARG A11OY_REQUIRE_LOCAL_LLM=0
378
  # The wheel is BIND-MOUNTED from the builder stage (not COPY'd) so it is
379
  # … (full rationale: docs/DOCKERFILE_NOTES.md §51)
380
  RUN --mount=type=bind,from=llama-build,source=/wheels,target=/wheels \
@@ -391,12 +380,12 @@ RUN --mount=type=bind,from=llama-build,source=/wheels,target=/wheels \
391
  fi
392
  # GGUF weight — RELIABLY PRESENT (pinned revision + retry + integrity verify), NOT best-effort.
393
  # … (full rationale: docs/DOCKERFILE_NOTES.md §52)
394
- ARG A11OY_ALLOY_GGUF_REPO=Qwen/Qwen2.5-Coder-0.5B-Instruct-GGUF
395
- ARG A11OY_ALLOY_GGUF_FILE=qwen2.5-coder-0.5b-instruct-q4_k_m.gguf
396
- ARG A11OY_ALLOY_GGUF_REV=ebb2015119c907b064c512bf053e945850b5875f
397
- ARG A11OY_ALLOY_GGUF_SHA256=1d9614638d18024d0fbb36575a15f1302a3adf044df10345688ec4f6e1c4ff32
398
- ARG A11OY_ALLOY_GGUF_SIZE=491400064
399
- ARG A11OY_REQUIRE_LOCAL_LLM=0
400
  ENV A11OY_REQUIRE_LOCAL_LLM=${A11OY_REQUIRE_LOCAL_LLM}
401
  RUN python3 <<'GGUFPY'
402
  import hashlib, os, sys, time
@@ -460,7 +449,8 @@ sys.exit(0)
460
  GGUFPY
461
  # Drop transient download metadata; the real weight stays at /app/models/<file>.
462
  RUN rm -rf /app/models/.cache /root/.cache/huggingface 2>/dev/null || true
463
- ENV A11OY_ALLOY_GGUF=/app/models/qwen2.5-coder-0.5b-instruct-q4_k_m.gguf
 
464
 
465
  # ── BUILD-TIME EXTERNAL-DOWNLOAD AUDIT ───────────────────────────────────────
466
  # … (full rationale: docs/DOCKERFILE_NOTES.md §54)
 
18
  # HF Space requirement: listen on PORT 7860.
19
 
20
  # ---------------------------------------------------------------------------
21
+ # OWNED KHIPU CPU RUNTIME: use the official universal manylinux wheel.
22
+ # No source-build toolchain or compile intermediates enter either
23
+ # the builder or the published runtime. The exact release asset is
24
+ # pinned by SHA-256 and verified for size plus glibc linkage.
25
+ ARG A11OY_REQUIRE_LOCAL_LLM=1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  FROM python:3.12-slim@sha256:423ed6ab25b1921a477529254bfeeabf5855151dc2c3141699a1bfc852199fbf AS llama-build-1
27
+ ARG LLAMA_CPP_WHEEL=llama_cpp_python-0.3.35-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
28
+ COPY scripts/fetch_owned_khipu_wheel.py /tmp/fetch_owned_khipu_wheel.py
29
+ RUN python3 /tmp/fetch_owned_khipu_wheel.py && rm /tmp/fetch_owned_khipu_wheel.py
30
+ RUN python3 <<'WHEELCHK'
31
+ import hashlib, os, zipfile
32
+ path = "/wheels/llama_cpp_python-0.3.35-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl"
33
+ assert os.path.getsize(path) == 23912624, os.path.getsize(path)
34
+ digest = hashlib.sha256(open(path, "rb").read()).hexdigest()
35
+ assert digest == "d172f3d3c8cdd194c3c47c71cb077ed6e61354a2d0f939ceeac0c8fd29999596", digest
36
+ with zipfile.ZipFile(path) as archive:
37
+ shared = [name for name in archive.namelist() if name.endswith("libllama.so")]
38
+ assert shared, "official wheel is missing libllama.so"
39
+ data = archive.read(shared[0])
40
+ assert b"libc.so.6" in data
41
+ assert b"libc.musl-x86_64.so.1" not in data
42
+ print("[a11oy] verified official llama-cpp-python CPU wheel", path)
43
+ WHEELCHK
44
+
45
+ # Explicit opt-out remains available for diagnostic image builds.
 
 
46
  FROM python:3.12-slim@sha256:423ed6ab25b1921a477529254bfeeabf5855151dc2c3141699a1bfc852199fbf AS llama-build-0
47
  RUN mkdir -p /wheels
48
 
 
49
  FROM llama-build-${A11OY_REQUIRE_LOCAL_LLM} AS llama-build
50
 
51
  # ---------------------------------------------------------------------------
 
98
  "defusedxml==0.7.1" \
99
  "numpy==2.5.2"
100
 
101
+ # Canonical non-generative Nemo witness: exact immutable source revision,
102
+ # zero action authority, and import-time doctrine identity verification.
103
+ RUN pip install --no-cache-dir \
104
+ "https://github.com/szl-holdings/szl-nemo/archive/810231a531188bb569e3faa17396386eb0a5e260.tar.gz" && \
105
+ python3 -c "import szl_nemo; assert szl_nemo.__version__ == '0.4.0'; assert tuple(szl_nemo.LOCKED_PROVEN_FORMULA_IDS) == ('F1','F4','F7','F11','F12','F18','F19','F22'); assert szl_nemo.ENVELOPE_RULE_VERSION == 'doctrine-v11/E1-E10'"
106
+
107
  # sqlite-vss removed from build: no pre-built wheel for python:3.12-slim;
108
  # szl_khipu_lmdb.py and szl_unay.py already have honest try/except fallback
109
  # to cosine similarity if the sqlite-vss .so cannot load. (P0 CI fix, Dev1 Rumi)
 
282
  COPY content_credentials.py ./
283
  COPY schemas/quant-claims/ ./schemas/quant-claims/
284
  COPY OUROBOROS_RUN_ALL.py ./
285
+ COPY a11oy_governed_cortex.py ./a11oy_governed_cortex.py
286
  COPY knowledge.json szl_parity_gaps.py compliance_crosswalk.py szl_compliance_mesh.py a11oy_warhacker_obs.py serve.py szl_governed_api.py szl_demo_tier1.py szl_assurance.py govern_showcase.html a11oy_wireA_metrics.py cathedral.html a11oy_operator_organ.py a11oy_hf_assets.py szl_b2_secdata.py gates_manifest.json a11oy_code_orchestrator.py a11oy_agent_loop.py a11oy_org_rag.py a11oy_mcp_client.py szl_rag.py a11oy_code_ide.html wayra_serve.py wayra_snapshot.json wayra_digests_7d.json szl_khipu_os_routes.py szl_spaces_proxy.py szl_spaces_surface.py szl_khipu_consensus.py szl_puriq_formulas.py ayni_os_serve.py szl_live_wires.py live_wires.html live_wires_3d.js szl_intoto.py szl_intoto_routes.py szl_scitt.py szl_dsse.py szl_content_address.py szl_provenance.py szl_be_hardening.py szl_unay.py szl_khipu_lmdb.py szl_khipu_replicate.py szl_unay_routes.py szl_warhacker_aliases.py a11oy_v4_hickok.py szl_khipu.py szl_formulas.py a11oy_v4_formulas.py szl_anatomy_3d.py szl_anatomy_routes.py _vendor_blobs.py szl_v4_fleet.py operator_shell_v4.py szl_bridge.py szl_bridge_schemas.py agent.html a11oy_bridge_cli.py szl_ken.py a11oy_formula_endpoints.py a11oy_formula_registry_guard.py a11oy_formulas_page.py a11oy_frontier_patch.py a11oy_v4_agent.py szl_brain.py szl_wire.py szl_hub.py szl_rosie_companion.py szl_receipt_substrate.py szl_alloy_embed_fabric.py szl_ayni_quorum.py szl_agentic_loop.py szl_ltc_dynamics.py szl_sgh_scheduler.py szl_formula_wiring.py szl_formula_surfaces.py a11oy_code_engine.py a11oy_code_runloop.py a11oy_code.py a11oy_seismic.py szl_warhacker_real.py szl_warhacker_demos.py NOTICE_warhacker_demos.txt szl_llm_registry.py szl_elite_console.py szl_alloy_models.py szl_scaling.py szl_allodial.py szl_entanglement.py szl_neuroplasticity.py szl_neuromorphic.py szl_kan.py szl_titans.py szl_mor.py szl_ternary.py szl_agentmem.py szl_edgefusion.py szl_hybridssm.py szl_aigov.py szl_chain_of_title.py szl_sovereign_compute.py szl_a11oy_interpretability.py a11oy_active_flux_router.py szl_energy_budget.py szl_energy_sovereign.py szl_energy_provenance.py szl_heart_blood.py szl_engine_status.py szl_backend_hardening.py revenue_endpoints.py a11oy_harvest_endpoints.py szl_energy_measured.py joule_billing.py szl_durable_ledger.py szl_energy_ledger_recovery.py szl_energy_ledger.py szl_energy_operator.py szl_energy_projection.py szl_cheapest_watt.py szl_energy_live.py szl_orbital_topology.py szl_orbital_projection.py a11oy_orbital_page.py a11oy_frontier_page.py szl_frontier_manifest.py szl_frontier_zkinfer.py szl_frontier_fmverif.py szl_frontier_supplychain.py a11oy_code_as_action.py a11oy_governed_kernel.py szl_lambda_tripwire.py szl_provenance_receipt.py szl_khipu_verify.py szl_public_verify.py szl_attest_stack.py szl_demo_sign.py szl_sda.py szl_fabric_surface.py szl_nemo_agents.py szl_kverify.py szl_specdec.py szl_immune.py szl_quant_qbio_holo.py szl_materials.py szl_materials_predict.py a11oy_factory.py a11oy_constitution.py a11oy_nav_wireup.py szl_mbse_cosim.py szl_mbse_nav.py szl_mbse.py szl_factory.py szl_willay_gateway.py a11oy_willay_nav.py szl_waqay.py a11oy_waqay_nav.py szl_yupay.py a11oy_yupay_nav.py a11oy_uds_portability_nav.py szl_pinn_bounds.py szl_pinn_residual.py physical_bounds_certificate.json agentic_decision_trail.json physical_bounds_certificate.dsse.json szl_pinn_inverse.py szl_governed_ipinn.py szl_calphad_inverse.py szl_pnt_mesh.py quantum_sensing_limits.py pnt_resilience.py nav_coasting.py fundamental_limits.py szl_counter_uas_proxy.py szl_gpu_quant.py szl_joules_truth.py revenue_model.py szl_prod_hardening.py szl_resilience.py szl_observability.py szl_corpus_publish.py szl_lake_store.py szl_lake_ingest.py szl_e8.py szl_trajectory_sign.py szl_nemotron_ingest.py szl_nemotron_corpus.py szl_nemo_verify.py a11oy_nemo_core.py szl_restraint.py szl_sapa.py szl_sapa_patch.py szl_restraint_energy.py a11oy_react_core.py szl_org_lambda.py a11oy_canonical_domain.py a11oy_formula_tiers.py szl_physical_bounds.py szl_kc_loop_forge.py szl_kc_loop_forge_metrics.py szl_kc_atlas.py szl_eval_arena.py szl_vqc.py szl_kc_jpt.py ./
287
 
288
  # Wave M / Dev 4: Sovereign Local Model panel — imported GUARDED by serve.py
 
363
 
364
  # LIVE CPU demo tier: install llama.cpp + fetch ONE tiny Apache-2.0 GGUF
365
  # … (full rationale: docs/DOCKERFILE_NOTES.md §50)
366
+ ARG A11OY_REQUIRE_LOCAL_LLM=1
367
  # The wheel is BIND-MOUNTED from the builder stage (not COPY'd) so it is
368
  # … (full rationale: docs/DOCKERFILE_NOTES.md §51)
369
  RUN --mount=type=bind,from=llama-build,source=/wheels,target=/wheels \
 
380
  fi
381
  # GGUF weight — RELIABLY PRESENT (pinned revision + retry + integrity verify), NOT best-effort.
382
  # … (full rationale: docs/DOCKERFILE_NOTES.md §52)
383
+ ARG A11OY_ALLOY_GGUF_REPO=SZLHOLDINGS/SZL-Khipu-1.5B-GGUF
384
+ ARG A11OY_ALLOY_GGUF_FILE=SZL-Khipu-1.5B-Q4_K_M.gguf
385
+ ARG A11OY_ALLOY_GGUF_REV=67d60ec577730747055491640cfb91fc4a4b5d25
386
+ ARG A11OY_ALLOY_GGUF_SHA256=13c1a1993063e1dff92f7413ccf48eaca6d48efc8801ae9af35961ae3396623a
387
+ ARG A11OY_ALLOY_GGUF_SIZE=986047904
388
+ ARG A11OY_REQUIRE_LOCAL_LLM=1
389
  ENV A11OY_REQUIRE_LOCAL_LLM=${A11OY_REQUIRE_LOCAL_LLM}
390
  RUN python3 <<'GGUFPY'
391
  import hashlib, os, sys, time
 
449
  GGUFPY
450
  # Drop transient download metadata; the real weight stays at /app/models/<file>.
451
  RUN rm -rf /app/models/.cache /root/.cache/huggingface 2>/dev/null || true
452
+ ENV A11OY_ALLOY_GGUF=/app/models/SZL-Khipu-1.5B-Q4_K_M.gguf \
453
+ A11OY_KHIPU_GGUF=/app/models/SZL-Khipu-1.5B-Q4_K_M.gguf
454
 
455
  # ── BUILD-TIME EXTERNAL-DOWNLOAD AUDIT ───────────────────────────────────────
456
  # … (full rationale: docs/DOCKERFILE_NOTES.md §54)
a11oy_governed_cortex.py ADDED
@@ -0,0 +1,1048 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # SPDX-License-Identifier: Apache-2.0
2
+ """A11oy local owned-model governed inference cortex.
3
+
4
+ This module wires the existing A11oy public Second Brain, the canonical formal
5
+ formula authority, the exact szl-nemo E1-E10/R1-R5 witness, and the owned Khipu
6
+ GGUF into one proposal-only CPU inference path. It never grants tool or action
7
+ authority and never persists prompt, evidence text, model output, or private
8
+ reasoning.
9
+ """
10
+ from __future__ import annotations
11
+
12
+ import hashlib
13
+ import importlib.metadata
14
+ import json
15
+ import os
16
+ import platform
17
+ import re
18
+ import threading
19
+ import time
20
+ from copy import deepcopy
21
+ from pathlib import Path
22
+ from typing import Any, Mapping
23
+
24
+ from fastapi import FastAPI
25
+ from fastapi.responses import JSONResponse
26
+ from pydantic import BaseModel, ConfigDict, Field, field_validator
27
+
28
+ SCHEMA_VERSION = "szl.a11oy.owned-khipu-cortex/v1"
29
+ HEALTH_SCHEMA = "szl.a11oy.owned-khipu-cortex-health/v1"
30
+ CONTRACT_SCHEMA = "szl.a11oy.owned-khipu-cortex-contract/v1"
31
+ RESPONSE_SCHEMA = "szl.a11oy.owned-khipu-cortex-response/v1"
32
+ RECEIPT_SCHEMA = "szl.a11oy.owned-khipu-cortex-receipt/v1"
33
+ ANATOMY_SCHEMA = "szl.anatomy.ephemeral-owned-inference/v1"
34
+ MODEL_REPOSITORY = "SZLHOLDINGS/SZL-Khipu-1.5B-GGUF"
35
+ MODEL_REVISION = "67d60ec577730747055491640cfb91fc4a4b5d25"
36
+ MODEL_FILENAME = "SZL-Khipu-1.5B-Q4_K_M.gguf"
37
+ MODEL_SHA256 = "13c1a1993063e1dff92f7413ccf48eaca6d48efc8801ae9af35961ae3396623a"
38
+ MODEL_SIZE = 986_047_904
39
+ NEMO_REVISION = "810231a531188bb569e3faa17396386eb0a5e260"
40
+ NEMO_VERSION = "0.4.0"
41
+ LOCKED_FORMULAS = ("F1", "F4", "F7", "F11", "F12", "F18", "F19", "F22")
42
+ MAX_PROMPT_CHARS = 4_000
43
+ MAX_NEW_TOKENS = 128
44
+ MAX_EVIDENCE = 4
45
+ GENERATION_QUEUE_SECONDS = 2.0
46
+ FULL_SHA_RE = re.compile(r"^[0-9a-f]{40}$")
47
+ RESERVED_TOKENS = ("<|im_start|>", "<|im_end|>", "<s>", "</s>")
48
+
49
+ _MODEL_LOCK = threading.Lock()
50
+ _GENERATION_LOCK = threading.Lock()
51
+ _ANATOMY_LOCK = threading.Lock()
52
+ _MODEL: Any | None = None
53
+ _MODEL_IDENTITY: dict[str, Any] | None = None
54
+ _MODEL_ERROR: str | None = None
55
+ _ANATOMY = {"observation_count": 0, "last": None}
56
+
57
+
58
+ class CortexBoundaryError(RuntimeError):
59
+ def __init__(self, code: str, status: int = 503):
60
+ super().__init__(code)
61
+ self.code = code
62
+ self.status = status
63
+
64
+
65
+ class GovernedInferenceRequest(BaseModel):
66
+ model_config = ConfigDict(extra="forbid")
67
+
68
+ prompt: str = Field(min_length=1, max_length=MAX_PROMPT_CHARS)
69
+ max_new_tokens: int = Field(default=64, ge=16, le=MAX_NEW_TOKENS)
70
+ k: int = Field(default=3, ge=1, le=MAX_EVIDENCE)
71
+
72
+ @field_validator("prompt")
73
+ @classmethod
74
+ def normalize_prompt(cls, value: str) -> str:
75
+ clean = value.strip()
76
+ if not clean or "\x00" in clean:
77
+ raise ValueError("prompt must contain visible text and no NUL bytes")
78
+ if any(token in clean for token in RESERVED_TOKENS):
79
+ raise ValueError("prompt contains a reserved chat control token")
80
+ return clean
81
+
82
+
83
+ def canonical_bytes(value: Any) -> bytes:
84
+ return json.dumps(
85
+ value,
86
+ ensure_ascii=False,
87
+ sort_keys=True,
88
+ separators=(",", ":"),
89
+ ).encode("utf-8")
90
+
91
+
92
+ def canonical_sha256(value: Any) -> str:
93
+ return hashlib.sha256(canonical_bytes(value)).hexdigest()
94
+
95
+
96
+ def text_sha256(value: str) -> str:
97
+ return hashlib.sha256(value.encode("utf-8")).hexdigest()
98
+
99
+
100
+ def source_revision() -> str | None:
101
+ for key in ("SZL_GIT_SHA", "A11OY_GIT_SHA", "GITHUB_SHA"):
102
+ value = (os.environ.get(key) or "").strip().lower()
103
+ if FULL_SHA_RE.fullmatch(value):
104
+ return value
105
+ return None
106
+
107
+
108
+ def model_path() -> Path:
109
+ configured = (os.environ.get("A11OY_KHIPU_GGUF") or "").strip()
110
+ if configured:
111
+ return Path(configured)
112
+ legacy = (os.environ.get("A11OY_ALLOY_GGUF") or "").strip()
113
+ if legacy and Path(legacy).name == MODEL_FILENAME:
114
+ return Path(legacy)
115
+ return Path("/app/models") / MODEL_FILENAME
116
+
117
+
118
+ def _hash_file(path: Path) -> str:
119
+ digest = hashlib.sha256()
120
+ with path.open("rb") as handle:
121
+ for chunk in iter(lambda: handle.read(8 * 1024 * 1024), b""):
122
+ digest.update(chunk)
123
+ return digest.hexdigest()
124
+
125
+
126
+ def verify_model_artifact(path: Path | None = None) -> dict[str, Any]:
127
+ selected = path or model_path()
128
+ if not selected.is_file() or selected.is_symlink():
129
+ raise CortexBoundaryError("owned_model_artifact_missing")
130
+ size = selected.stat().st_size
131
+ if size != MODEL_SIZE:
132
+ raise CortexBoundaryError("owned_model_size_mismatch")
133
+ digest = _hash_file(selected)
134
+ if digest != MODEL_SHA256:
135
+ raise CortexBoundaryError("owned_model_sha256_mismatch")
136
+ return {
137
+ "repository": MODEL_REPOSITORY,
138
+ "revision": MODEL_REVISION,
139
+ "filename": MODEL_FILENAME,
140
+ "sha256": digest,
141
+ "size": size,
142
+ "path": str(selected),
143
+ "state": "VERIFIED",
144
+ }
145
+
146
+
147
+ def _load_formula_authority() -> dict[str, Any]:
148
+ try:
149
+ import szl_formula_registry as registry
150
+
151
+ document = registry.load_registry(verify=True)
152
+ payload = document["payload"]
153
+ except Exception as exc:
154
+ raise CortexBoundaryError(
155
+ f"formula_authority_unavailable:{type(exc).__name__}"
156
+ ) from exc
157
+ if tuple(payload.get("locked_proven_ids") or ()) != LOCKED_FORMULAS:
158
+ raise CortexBoundaryError("formula_locked_set_drift")
159
+ if payload.get("locked_proven_count") != len(LOCKED_FORMULAS):
160
+ raise CortexBoundaryError("formula_locked_count_drift")
161
+ lambda_rule = payload.get("lambda") or {}
162
+ if (
163
+ lambda_rule.get("formula_id") != "F23"
164
+ or lambda_rule.get("status") != "CONJECTURE_1_ADVISORY"
165
+ or lambda_rule.get("can_authorize") is not False
166
+ or lambda_rule.get("can_be_sole_allow_basis") is not False
167
+ ):
168
+ raise CortexBoundaryError("lambda_authority_drift")
169
+ return {
170
+ "registry_digest": document["registry_digest"]["value"],
171
+ "formal_source_repository": payload["formal_source"]["repository"],
172
+ "formal_source_commit": payload["formal_source"]["commit"],
173
+ "kernel_source_repository": payload["kernel_source"]["repository"],
174
+ "kernel_source_commit": payload["kernel_source"]["commit"],
175
+ "f_id_to_callable_mapping": payload["kernel_source"][
176
+ "f_id_to_callable_mapping"
177
+ ],
178
+ "locked_proven_count": payload["locked_proven_count"],
179
+ "locked_proven_ids": list(payload["locked_proven_ids"]),
180
+ "lambda": deepcopy(lambda_rule),
181
+ "policy": deepcopy(payload["policy"]),
182
+ }
183
+
184
+
185
+ def _load_nemo() -> Any:
186
+ try:
187
+ import szl_nemo
188
+ except Exception as exc:
189
+ raise CortexBoundaryError(f"nemo_unavailable:{type(exc).__name__}") from exc
190
+ if getattr(szl_nemo, "__version__", None) != NEMO_VERSION:
191
+ raise CortexBoundaryError("nemo_version_drift")
192
+ if tuple(szl_nemo.LOCKED_PROVEN_FORMULA_IDS) != LOCKED_FORMULAS:
193
+ raise CortexBoundaryError("nemo_locked_set_drift")
194
+ return szl_nemo
195
+
196
+
197
+ def _hardware_fingerprint(artifact: Mapping[str, Any]) -> str:
198
+ return "sha256:" + canonical_sha256(
199
+ {
200
+ "machine": platform.machine(),
201
+ "processor": platform.processor(),
202
+ "cpu_count": os.cpu_count(),
203
+ "model_sha256": artifact["sha256"],
204
+ "model_size": artifact["size"],
205
+ "threads": max(1, min(int(os.environ.get("A11OY_KHIPU_THREADS", "2")), 4)),
206
+ }
207
+ )
208
+
209
+
210
+ def _load_model() -> tuple[Any, dict[str, Any]]:
211
+ global _MODEL, _MODEL_IDENTITY, _MODEL_ERROR
212
+ if _MODEL is not None and _MODEL_IDENTITY is not None:
213
+ return _MODEL, deepcopy(_MODEL_IDENTITY)
214
+ with _MODEL_LOCK:
215
+ if _MODEL is not None and _MODEL_IDENTITY is not None:
216
+ return _MODEL, deepcopy(_MODEL_IDENTITY)
217
+ try:
218
+ artifact = verify_model_artifact()
219
+ import llama_cpp
220
+
221
+ version = importlib.metadata.version("llama-cpp-python")
222
+ threads = max(
223
+ 1,
224
+ min(int(os.environ.get("A11OY_KHIPU_THREADS", "2")), 4),
225
+ )
226
+ model = llama_cpp.Llama(
227
+ model_path=artifact["path"],
228
+ n_ctx=2048,
229
+ n_batch=128,
230
+ n_threads=threads,
231
+ n_threads_batch=threads,
232
+ seed=17,
233
+ use_mmap=True,
234
+ use_mlock=False,
235
+ logits_all=False,
236
+ embedding=False,
237
+ verbose=False,
238
+ )
239
+ revision = source_revision()
240
+ if revision is None:
241
+ raise CortexBoundaryError("source_revision_unavailable")
242
+ identity = {
243
+ "model": {
244
+ "id": f"{MODEL_REPOSITORY}/{MODEL_FILENAME}",
245
+ "repository": MODEL_REPOSITORY,
246
+ "revision": MODEL_REVISION,
247
+ "filename": MODEL_FILENAME,
248
+ "sha256": MODEL_SHA256,
249
+ "size": MODEL_SIZE,
250
+ "adapter_revision": "none",
251
+ "tokenizer_revision": MODEL_REVISION,
252
+ "template_revision": revision,
253
+ "ownership": "SZL_HOLDINGS_OWNED_ARTIFACT",
254
+ },
255
+ "runtime": {
256
+ "engine": "llama-cpp-python",
257
+ "version": version,
258
+ "library_version": getattr(llama_cpp, "__version__", version),
259
+ "hardware_fingerprint": _hardware_fingerprint(artifact),
260
+ "device": "CPU",
261
+ "threads": threads,
262
+ },
263
+ "artifact": artifact,
264
+ }
265
+ _MODEL = model
266
+ _MODEL_IDENTITY = identity
267
+ _MODEL_ERROR = None
268
+ return _MODEL, deepcopy(_MODEL_IDENTITY)
269
+ except CortexBoundaryError as exc:
270
+ _MODEL_ERROR = exc.code
271
+ raise
272
+ except Exception as exc:
273
+ _MODEL_ERROR = f"{type(exc).__name__}"
274
+ raise CortexBoundaryError(
275
+ f"owned_model_runtime_unavailable:{type(exc).__name__}"
276
+ ) from exc
277
+
278
+
279
+ def _safe_fragment(value: Any, limit: int = 400) -> str:
280
+ text = " ".join(str(value or "").replace("\x00", " ").split())
281
+ for token in RESERVED_TOKENS:
282
+ text = text.replace(token, "[reserved-token]")
283
+ return text[:limit]
284
+
285
+
286
+ def _brain_evidence(prompt: str, k: int) -> dict[str, Any]:
287
+ try:
288
+ import szl_brain_api
289
+
290
+ index = szl_brain_api.get_index("a11oy")
291
+ result = index.ask(prompt, k=k)
292
+ stats = index.stats()
293
+ except Exception as exc:
294
+ raise CortexBoundaryError(
295
+ f"second_brain_unavailable:{type(exc).__name__}"
296
+ ) from exc
297
+ grounding = result.get("grounding_subgraph") or {}
298
+ raw_nodes = grounding.get("nodes") or []
299
+ if not isinstance(raw_nodes, list) or not raw_nodes:
300
+ raise CortexBoundaryError("second_brain_returned_no_grounding")
301
+ projections: list[dict[str, Any]] = []
302
+ for raw in raw_nodes[:MAX_EVIDENCE]:
303
+ if not isinstance(raw, Mapping):
304
+ continue
305
+ node_id = _safe_fragment(raw.get("id"), 160)
306
+ if not node_id:
307
+ continue
308
+ source = _safe_fragment(
309
+ raw.get("url") or raw.get("source") or raw.get("kind") or "a11oy-brain",
310
+ 500,
311
+ )
312
+ public_projection = {
313
+ "node_id": node_id,
314
+ "title": _safe_fragment(raw.get("title") or node_id, 500),
315
+ "kind": _safe_fragment(raw.get("kind"), 100) or None,
316
+ "source": source,
317
+ "url": _safe_fragment(raw.get("url"), 500) or None,
318
+ "formula_id": _safe_fragment(raw.get("formula_id"), 32) or None,
319
+ "proof_status": _safe_fragment(raw.get("proof_status"), 100) or None,
320
+ "node_label": _safe_fragment(raw.get("node_label"), 100) or None,
321
+ "ppr": raw.get("ppr") if isinstance(raw.get("ppr"), (int, float)) else None,
322
+ }
323
+ projection_text = canonical_bytes(public_projection).decode("utf-8")
324
+ projections.append(
325
+ {
326
+ **public_projection,
327
+ "projection_text": projection_text,
328
+ "sha256": text_sha256(projection_text),
329
+ }
330
+ )
331
+ if not projections:
332
+ raise CortexBoundaryError("second_brain_returned_no_usable_handles")
333
+ return {
334
+ "state": "READY",
335
+ "retrieval": result.get("retrieval"),
336
+ "content_access": "PUBLIC_PROJECTION_HANDLES_ONLY",
337
+ "private_graph_present": False,
338
+ "node_count": stats.get("node_count"),
339
+ "content_hash": getattr(index, "content_hash", None),
340
+ "query_latency": result.get("query_latency"),
341
+ "items": projections,
342
+ }
343
+
344
+
345
+ def _formula_binding(
346
+ authority: Mapping[str, Any],
347
+ *,
348
+ prompt_sha256: str,
349
+ evidence_set_sha256: str,
350
+ ) -> dict[str, Any]:
351
+ basis = {
352
+ "formula_id": "F1",
353
+ "purpose": "canonical request/evidence/receipt replay identity",
354
+ "prompt_sha256": prompt_sha256,
355
+ "evidence_set_sha256": evidence_set_sha256,
356
+ "registry_digest": authority["registry_digest"],
357
+ }
358
+ return {
359
+ "locked_proven_ids": list(authority["locked_proven_ids"]),
360
+ "locked_proven_count": authority["locked_proven_count"],
361
+ "formal_source_repository": authority["formal_source_repository"],
362
+ "formal_source_commit": authority["formal_source_commit"],
363
+ "kernel_source_repository": authority["kernel_source_repository"],
364
+ "kernel_source_commit": authority["kernel_source_commit"],
365
+ "f_id_to_callable_mapping": authority["f_id_to_callable_mapping"],
366
+ "requested_formula_ids": ["F1"],
367
+ "authorization_basis_ids": [],
368
+ "applications": [
369
+ {
370
+ "formula_id": "F1",
371
+ "applicability": "APPLIES",
372
+ "basis_sha256": canonical_sha256(basis),
373
+ "scope": "REPLAY_HASH_DETERMINISM_ONLY",
374
+ "can_authorize_action": False,
375
+ }
376
+ ],
377
+ "lambda": deepcopy(authority["lambda"]),
378
+ }
379
+
380
+
381
+ def _scope() -> dict[str, Any]:
382
+ policy = {
383
+ "principal": "public-anonymous",
384
+ "tenant": "public",
385
+ "access": "PUBLIC_A11OY_BRAIN_PROJECTION_ONLY",
386
+ "model_authority": "PROPOSAL_ONLY",
387
+ "tools": False,
388
+ }
389
+ return {
390
+ "principal_id_sha256": text_sha256("public-anonymous"),
391
+ "tenant_id_sha256": text_sha256("public"),
392
+ "access_decision": "ALLOW",
393
+ "policy_revision": "sha256:" + canonical_sha256(policy),
394
+ }
395
+
396
+
397
+ def _nemo_envelope(
398
+ *,
399
+ stage: str,
400
+ identity: Mapping[str, Any],
401
+ evidence: Mapping[str, Any],
402
+ formulas: Mapping[str, Any],
403
+ claims: list[dict[str, Any]],
404
+ ) -> dict[str, Any]:
405
+ items = [
406
+ {
407
+ "node_id": item["node_id"],
408
+ "source": item["source"],
409
+ "sha256": item["sha256"],
410
+ }
411
+ for item in evidence["items"]
412
+ ]
413
+ return {
414
+ "schema": "szl.nemo.inference-envelope.v1",
415
+ "stage": stage,
416
+ "witness_identity": {
417
+ "artifact_kind": "SOFTWARE_KERNEL",
418
+ "generative": False,
419
+ "not_nemotron": True,
420
+ },
421
+ "model": {
422
+ "id": identity["model"]["id"],
423
+ "revision": identity["model"]["revision"],
424
+ "adapter_revision": identity["model"]["adapter_revision"],
425
+ "tokenizer_revision": identity["model"]["tokenizer_revision"],
426
+ "template_revision": identity["model"]["template_revision"],
427
+ },
428
+ "runtime": {
429
+ "engine": identity["runtime"]["engine"],
430
+ "version": identity["runtime"]["version"],
431
+ "hardware_fingerprint": identity["runtime"]["hardware_fingerprint"],
432
+ },
433
+ "scope": _scope(),
434
+ "evidence": {
435
+ "content_access": "HANDLES_ONLY",
436
+ "grounding_required": True,
437
+ "handles": [{"nodeId": item["node_id"]} for item in items],
438
+ "items": items,
439
+ "evidence_set_sha256": canonical_sha256(items),
440
+ },
441
+ "formulas": {
442
+ "locked_proven_ids": formulas["locked_proven_ids"],
443
+ "locked_proven_count": formulas["locked_proven_count"],
444
+ "formal_source_repository": formulas["formal_source_repository"],
445
+ "formal_source_commit": formulas["formal_source_commit"],
446
+ "kernel_source_repository": formulas["kernel_source_repository"],
447
+ "kernel_source_commit": formulas["kernel_source_commit"],
448
+ "f_id_to_callable_mapping": formulas["f_id_to_callable_mapping"],
449
+ "requested_formula_ids": formulas["requested_formula_ids"],
450
+ "authorization_basis_ids": formulas["authorization_basis_ids"],
451
+ "applications": [
452
+ {
453
+ "formula_id": item["formula_id"],
454
+ "applicability": item["applicability"],
455
+ "basis_sha256": item["basis_sha256"],
456
+ }
457
+ for item in formulas["applications"]
458
+ ],
459
+ "lambda": formulas["lambda"],
460
+ },
461
+ "authority": {
462
+ "model_authority": "PROPOSAL_ONLY",
463
+ "executed": False,
464
+ "execution_authority": "NONE",
465
+ },
466
+ "witness_history": [] if stage == "PRE_GENERATION" else ["PRE_GENERATION"],
467
+ "claims": claims,
468
+ }
469
+
470
+
471
+ def _decision_dict(decision: Any) -> dict[str, Any]:
472
+ if hasattr(decision, "to_dict"):
473
+ value = decision.to_dict()
474
+ else:
475
+ value = {
476
+ "decision": getattr(decision, "decision", None),
477
+ "violated_rules": list(getattr(decision, "violated_rules", ())),
478
+ "rule_version": getattr(decision, "rule_version", None),
479
+ "input_hash": getattr(decision, "input_hash", None),
480
+ "reasons": list(getattr(decision, "reasons", ())),
481
+ }
482
+ return {
483
+ "decision": str(value.get("decision") or ""),
484
+ "violated_rules": [str(item) for item in value.get("violated_rules") or ()],
485
+ "rule_version": str(value.get("rule_version") or ""),
486
+ "input_hash": str(value.get("input_hash") or ""),
487
+ "reasons": [str(item) for item in value.get("reasons") or ()],
488
+ }
489
+
490
+
491
+ def _compose_messages(
492
+ prompt: str,
493
+ evidence: Mapping[str, Any],
494
+ formulas: Mapping[str, Any],
495
+ ) -> list[dict[str, str]]:
496
+ rows = []
497
+ for item in evidence["items"]:
498
+ rows.append(
499
+ f"[{item['node_id']}] title={item['title']}; kind={item['kind']}; "
500
+ f"source={item['source']}; proof_status={item['proof_status']}; "
501
+ f"label={item['node_label']}"
502
+ )
503
+ system = (
504
+ "You are the local SZL Khipu proposal cortex inside A11oy. Answer only "
505
+ "from the supplied public evidence projections and cite supporting node "
506
+ "IDs in square brackets. If evidence is insufficient, state that plainly. "
507
+ "Do not expose hidden reasoning. Do not issue tool calls or claim that an "
508
+ "action was executed. Lambda is Conjecture 1, advisory, and cannot "
509
+ "authorize action. Never call Lambda a theorem, proven, certified, or "
510
+ "guaranteed. Never claim perfect or 100% trust. If asked about model "
511
+ "training, disclose that SZL fine-tuned Khipu from Qwen2.5-1.5B-Instruct "
512
+ "and did not train a foundation model from scratch. Label benchmark or "
513
+ "numeric performance claims MEASURED, REPORTED, MODELED, HEURISTIC, "
514
+ "UNKNOWN, or UNAVAILABLE. Return only the final proposal."
515
+ )
516
+ user = (
517
+ f"Operator request:\n{prompt}\n\n"
518
+ "Authorized evidence projections:\n"
519
+ + "\n".join(rows)
520
+ + "\n\nFormula applicability: F1 applies only to deterministic replay "
521
+ "hashing for this request. The other locked formulas are authority "
522
+ "metadata and are not asserted applicable. No formula independently "
523
+ "authorizes action.\n\nReturn a concise evidence-cited proposal."
524
+ )
525
+ return [
526
+ {"role": "system", "content": system},
527
+ {"role": "user", "content": user},
528
+ ]
529
+
530
+
531
+ def _sanitize_output(value: Any) -> str:
532
+ text = str(value or "")
533
+ text = re.sub(r"<think>[\s\S]*?</think>", "", text, flags=re.I)
534
+ text = re.sub(r"<analysis>[\s\S]*?</analysis>", "", text, flags=re.I)
535
+ text = re.sub(r"```analysis[\s\S]*?```", "", text, flags=re.I)
536
+ text = text.replace("\x00", " ").strip()
537
+ if not text:
538
+ raise CortexBoundaryError("owned_model_returned_empty_output", 502)
539
+ return text[:24_000]
540
+
541
+
542
+ def _extract_output(result: Any) -> str:
543
+ if not isinstance(result, Mapping):
544
+ raise CortexBoundaryError("owned_model_response_invalid", 502)
545
+ choices = result.get("choices")
546
+ if not isinstance(choices, list) or not choices:
547
+ raise CortexBoundaryError("owned_model_response_invalid", 502)
548
+ message = choices[0].get("message") if isinstance(choices[0], Mapping) else None
549
+ if not isinstance(message, Mapping):
550
+ raise CortexBoundaryError("owned_model_response_invalid", 502)
551
+ tool_calls = message.get("tool_calls")
552
+ if isinstance(tool_calls, list) and tool_calls:
553
+ raise CortexBoundaryError("owned_model_attempted_tool_call", 502)
554
+ return _sanitize_output(message.get("content"))
555
+
556
+
557
+ def _generate(
558
+ prompt: str,
559
+ evidence: Mapping[str, Any],
560
+ formulas: Mapping[str, Any],
561
+ max_new_tokens: int,
562
+ ) -> tuple[str, dict[str, Any], dict[str, Any]]:
563
+ model, identity = _load_model()
564
+ acquired = _GENERATION_LOCK.acquire(timeout=GENERATION_QUEUE_SECONDS)
565
+ if not acquired:
566
+ raise CortexBoundaryError("owned_model_busy", 429)
567
+ started = time.perf_counter_ns()
568
+ try:
569
+ result = model.create_chat_completion(
570
+ messages=_compose_messages(prompt, evidence, formulas),
571
+ max_tokens=max_new_tokens,
572
+ temperature=0.15,
573
+ top_p=0.9,
574
+ repeat_penalty=1.05,
575
+ stream=False,
576
+ )
577
+ except Exception as exc:
578
+ raise CortexBoundaryError(
579
+ f"owned_model_generation_failed:{type(exc).__name__}", 502
580
+ ) from exc
581
+ finally:
582
+ _GENERATION_LOCK.release()
583
+ elapsed_ms = round((time.perf_counter_ns() - started) / 1_000_000, 3)
584
+ output = _extract_output(result)
585
+ usage = result.get("usage") if isinstance(result, Mapping) else None
586
+ metrics = {
587
+ "generation_latency": {
588
+ "label": "MEASURED",
589
+ "value_ms": elapsed_ms,
590
+ "scope": "in-process llama.cpp create_chat_completion call",
591
+ },
592
+ "prompt_tokens": usage.get("prompt_tokens") if isinstance(usage, Mapping) else None,
593
+ "completion_tokens": usage.get("completion_tokens") if isinstance(usage, Mapping) else None,
594
+ "token_counts_label": "MEASURED" if isinstance(usage, Mapping) else "UNAVAILABLE",
595
+ }
596
+ return output, identity, metrics
597
+
598
+
599
+ def _extract_citations(output: str, evidence: Mapping[str, Any]) -> list[str]:
600
+ allowed = {item["node_id"] for item in evidence["items"]}
601
+ found: list[str] = []
602
+ for candidate in re.findall(r"\[([^\[\]]{1,160})\]", output):
603
+ if candidate in allowed and candidate not in found:
604
+ found.append(candidate)
605
+ return found
606
+
607
+
608
+ def _observe_anatomy(event: Mapping[str, Any]) -> dict[str, Any]:
609
+ forbidden = {"prompt", "answer", "content", "raw_prompt", "raw_answer", "private_graph"}
610
+ serialized = canonical_bytes(dict(event)).decode("utf-8")
611
+ if any(f'"{key}"' in serialized for key in forbidden):
612
+ raise CortexBoundaryError("unsafe_anatomy_observation", 500)
613
+ with _ANATOMY_LOCK:
614
+ _ANATOMY["observation_count"] += 1
615
+ _ANATOMY["last"] = deepcopy(dict(event))
616
+ return {
617
+ "observation_count": _ANATOMY["observation_count"],
618
+ "last": deepcopy(_ANATOMY["last"]),
619
+ }
620
+
621
+
622
+ def _headers() -> dict[str, str]:
623
+ revision = source_revision()
624
+ return {
625
+ "Cache-Control": "no-store",
626
+ "X-Content-Type-Options": "nosniff",
627
+ "X-SZL-Governed-Inference": "owned-khipu-v1",
628
+ "X-SZL-Source-Revision": revision or "unavailable",
629
+ "X-SZL-Model-Revision": MODEL_REVISION,
630
+ "X-SZL-Nemo-Revision": NEMO_REVISION,
631
+ }
632
+
633
+
634
+ def _error(code: str, status: int) -> JSONResponse:
635
+ return JSONResponse(
636
+ {
637
+ "schema": "szl.a11oy.owned-khipu-cortex-error/v1",
638
+ "error": code,
639
+ "state": "UNAVAILABLE",
640
+ "executed": False,
641
+ "tool_execution": False,
642
+ "authority_state": "NO_ACTION_AUTHORITY",
643
+ },
644
+ status_code=status,
645
+ headers=_headers(),
646
+ )
647
+
648
+
649
+ def health_payload(*, load_model: bool = True) -> tuple[dict[str, Any], int]:
650
+ revision = source_revision()
651
+ checks: dict[str, Any] = {
652
+ "source_revision": {
653
+ "state": "READY" if revision else "UNAVAILABLE",
654
+ "revision": revision,
655
+ }
656
+ }
657
+ ready = revision is not None
658
+ try:
659
+ formula = _load_formula_authority()
660
+ checks["formula_authority"] = {
661
+ "state": "READY",
662
+ "registry_digest": formula["registry_digest"],
663
+ "locked_proven_count": formula["locked_proven_count"],
664
+ "locked_proven_ids": formula["locked_proven_ids"],
665
+ "lambda": formula["lambda"],
666
+ }
667
+ except CortexBoundaryError as exc:
668
+ ready = False
669
+ checks["formula_authority"] = {"state": "UNAVAILABLE", "error": exc.code}
670
+ try:
671
+ nemo = _load_nemo()
672
+ checks["nemo"] = {
673
+ "state": "READY",
674
+ "version": nemo.__version__,
675
+ "revision": NEMO_REVISION,
676
+ "envelope_rules": nemo.ENVELOPE_RULE_VERSION,
677
+ "text_rules": "doctrine-v11/R1-R5",
678
+ }
679
+ except CortexBoundaryError as exc:
680
+ ready = False
681
+ checks["nemo"] = {"state": "UNAVAILABLE", "error": exc.code}
682
+ try:
683
+ probe = _brain_evidence("Lambda proof status", 1)
684
+ checks["second_brain"] = {
685
+ "state": "READY",
686
+ "node_count": probe["node_count"],
687
+ "content_hash": probe["content_hash"],
688
+ "content_access": probe["content_access"],
689
+ "private_graph_present": False,
690
+ "probe_handle_count": len(probe["items"]),
691
+ }
692
+ except CortexBoundaryError as exc:
693
+ ready = False
694
+ checks["second_brain"] = {"state": "UNAVAILABLE", "error": exc.code}
695
+ try:
696
+ if load_model:
697
+ _, identity = _load_model()
698
+ else:
699
+ artifact = verify_model_artifact()
700
+ identity = {"artifact": artifact, "runtime": {"state": "NOT_LOADED"}}
701
+ checks["owned_model"] = {
702
+ "state": "READY" if load_model else "ARTIFACT_VERIFIED",
703
+ "repository": MODEL_REPOSITORY,
704
+ "revision": MODEL_REVISION,
705
+ "filename": MODEL_FILENAME,
706
+ "sha256": MODEL_SHA256,
707
+ "size": MODEL_SIZE,
708
+ "runtime": identity.get("runtime"),
709
+ }
710
+ except CortexBoundaryError as exc:
711
+ ready = False
712
+ checks["owned_model"] = {
713
+ "state": "UNAVAILABLE",
714
+ "error": exc.code,
715
+ "last_error": _MODEL_ERROR,
716
+ }
717
+ payload = {
718
+ "schema": HEALTH_SCHEMA,
719
+ "status": "READY" if ready else "UNAVAILABLE",
720
+ "source_revision": revision,
721
+ "checks": checks,
722
+ "model_kind": "OWNED_KHIPU_GGUF_LOCAL_CPU",
723
+ "model_repository": MODEL_REPOSITORY,
724
+ "model_revision": MODEL_REVISION,
725
+ "tools": False,
726
+ "executed": False,
727
+ "action_authority": "NONE",
728
+ "receipt_persistence": "EPHEMERAL_PROCESS_MEMORY_ONLY",
729
+ }
730
+ return payload, 200 if ready else 503
731
+
732
+
733
+ def contract_payload() -> dict[str, Any]:
734
+ revision = source_revision()
735
+ return {
736
+ "schema": CONTRACT_SCHEMA,
737
+ "version": "1.0.0",
738
+ "source_repository": "szl-holdings/a11oy",
739
+ "source_revision": revision,
740
+ "endpoints": {
741
+ "health": "/api/v2/governed-health",
742
+ "contract": "/api/v2/governed-contract",
743
+ "infer": "/api/v2/governed-infer",
744
+ "anatomy_last": "/api/v2/anatomy/last",
745
+ },
746
+ "model": {
747
+ "kind": "OWNED_KHIPU_GGUF_LOCAL_CPU",
748
+ "repository": MODEL_REPOSITORY,
749
+ "revision": MODEL_REVISION,
750
+ "filename": MODEL_FILENAME,
751
+ "sha256": MODEL_SHA256,
752
+ "size": MODEL_SIZE,
753
+ "lineage": "SZL fine-tune of Qwen/Qwen2.5-1.5B-Instruct; not a foundation model trained from scratch",
754
+ },
755
+ "second_brain": {
756
+ "implementation": "szl_brain_api.BrainIndex.ask",
757
+ "retrieval": "HippoRAG-style PPR plus GraphRAG community context",
758
+ "content_access": "PUBLIC_PROJECTION_HANDLES_ONLY",
759
+ "private_graph_allowed": False,
760
+ },
761
+ "formula_authority": {
762
+ "implementation": "szl_formula_registry",
763
+ "locked_proven_count": len(LOCKED_FORMULAS),
764
+ "locked_proven_ids": list(LOCKED_FORMULAS),
765
+ "applicability": "EVIDENCE_BOUND_PER_REQUEST",
766
+ "f_id_to_callable_mapping": "UNKNOWN_NOT_ASSERTED",
767
+ "lambda": {
768
+ "formula_id": "F23",
769
+ "status": "CONJECTURE_1_ADVISORY",
770
+ "can_authorize": False,
771
+ "can_be_sole_allow_basis": False,
772
+ },
773
+ },
774
+ "nemo": {
775
+ "package": "szl-nemo",
776
+ "version": NEMO_VERSION,
777
+ "revision": NEMO_REVISION,
778
+ "envelope_rules": "doctrine-v11/E1-E10",
779
+ "text_rules": "doctrine-v11/R1-R5",
780
+ "generative": False,
781
+ "not_nemotron": True,
782
+ },
783
+ "authority": {
784
+ "model_authority": "PROPOSAL_ONLY",
785
+ "tool_execution": False,
786
+ "autonomous_execution": False,
787
+ "action_authority": "NONE",
788
+ },
789
+ "receipts": {
790
+ "schema": RECEIPT_SCHEMA,
791
+ "signature_status": "UNSIGNED_RUNTIME",
792
+ "persistence": "EPHEMERAL_PROCESS_MEMORY_ONLY",
793
+ "raw_prompt_persisted": False,
794
+ "raw_evidence_persisted": False,
795
+ "private_reasoning_persisted": False,
796
+ "must_be_signed_before_consequential_action": True,
797
+ },
798
+ }
799
+
800
+
801
+ def infer_payload(request: GovernedInferenceRequest) -> dict[str, Any]:
802
+ revision = source_revision()
803
+ if revision is None:
804
+ raise CortexBoundaryError("source_revision_unavailable")
805
+ prompt_digest = text_sha256(request.prompt)
806
+ authority = _load_formula_authority()
807
+ nemo = _load_nemo()
808
+ evidence = _brain_evidence(request.prompt, request.k)
809
+ envelope_items = [
810
+ {
811
+ "node_id": item["node_id"],
812
+ "source": item["source"],
813
+ "sha256": item["sha256"],
814
+ }
815
+ for item in evidence["items"]
816
+ ]
817
+ evidence_set_sha256 = canonical_sha256(envelope_items)
818
+ formulas = _formula_binding(
819
+ authority,
820
+ prompt_sha256=prompt_digest,
821
+ evidence_set_sha256=evidence_set_sha256,
822
+ )
823
+ _, identity = _load_model()
824
+ pre_envelope = _nemo_envelope(
825
+ stage="PRE_GENERATION",
826
+ identity=identity,
827
+ evidence=evidence,
828
+ formulas=formulas,
829
+ claims=[],
830
+ )
831
+ pre = _decision_dict(nemo.evaluate_envelope(pre_envelope))
832
+ if pre["decision"] != "ALLOW":
833
+ raise CortexBoundaryError("nemo_pre_generation_blocked", 422)
834
+ output, identity, metrics = _generate(
835
+ request.prompt,
836
+ evidence,
837
+ formulas,
838
+ request.max_new_tokens,
839
+ )
840
+ text_witness = _decision_dict(nemo.evaluate(request.prompt, output))
841
+ if text_witness["decision"] != "ALLOW":
842
+ raise CortexBoundaryError("nemo_text_witness_blocked", 422)
843
+ output_digest = text_sha256(output)
844
+ claims = [{"label": "MODELED", "statement_sha256": output_digest}]
845
+ post_envelope = _nemo_envelope(
846
+ stage="POST_GENERATION",
847
+ identity=identity,
848
+ evidence=evidence,
849
+ formulas=formulas,
850
+ claims=claims,
851
+ )
852
+ post = _decision_dict(nemo.evaluate_envelope(post_envelope))
853
+ if post["decision"] != "ALLOW":
854
+ raise CortexBoundaryError("nemo_post_generation_blocked", 422)
855
+ citations = _extract_citations(output, evidence)
856
+ request_id = canonical_sha256(
857
+ {
858
+ "source_revision": revision,
859
+ "prompt_sha256": prompt_digest,
860
+ "evidence_set_sha256": evidence_set_sha256,
861
+ "model_revision": MODEL_REVISION,
862
+ "max_new_tokens": request.max_new_tokens,
863
+ "k": request.k,
864
+ }
865
+ )[:32]
866
+ receipt_payload = {
867
+ "schema": RECEIPT_SCHEMA,
868
+ "request_id": request_id,
869
+ "source_repository": "szl-holdings/a11oy",
870
+ "source_revision": revision,
871
+ "prompt_sha256": prompt_digest,
872
+ "evidence_set_sha256": evidence_set_sha256,
873
+ "output_sha256": output_digest,
874
+ "model": {
875
+ "repository": MODEL_REPOSITORY,
876
+ "revision": MODEL_REVISION,
877
+ "filename": MODEL_FILENAME,
878
+ "sha256": MODEL_SHA256,
879
+ "kind": "OWNED_KHIPU_GGUF_LOCAL_CPU",
880
+ },
881
+ "formula_applications": formulas["applications"],
882
+ "nemo": {
883
+ "revision": NEMO_REVISION,
884
+ "pre_input_hash": pre["input_hash"],
885
+ "text_input_hash": text_witness["input_hash"],
886
+ "post_input_hash": post["input_hash"],
887
+ },
888
+ "decision": "REVIEW_PROPOSAL_ONLY",
889
+ "authority_state": "NO_ACTION_AUTHORITY",
890
+ "executed": False,
891
+ "tool_execution": False,
892
+ }
893
+ receipt_digest = canonical_sha256(receipt_payload)
894
+ anatomy_event = {
895
+ "schema": ANATOMY_SCHEMA,
896
+ "request_id": request_id,
897
+ "source_revision": revision,
898
+ "prompt_sha256": prompt_digest,
899
+ "evidence_set_sha256": evidence_set_sha256,
900
+ "output_sha256": output_digest,
901
+ "receipt_sha256": receipt_digest,
902
+ "model_revision": MODEL_REVISION,
903
+ "raw_prompt_present": False,
904
+ "raw_evidence_present": False,
905
+ "raw_output_present": False,
906
+ "private_reasoning_present": False,
907
+ "private_graph_present": False,
908
+ "observer_authority": "NONE",
909
+ }
910
+ observed = _observe_anatomy(anatomy_event)
911
+ return {
912
+ "schema": RESPONSE_SCHEMA,
913
+ "request_id": request_id,
914
+ "state": "PROPOSAL",
915
+ "decision": "review",
916
+ "output": output,
917
+ "output_sha256": output_digest,
918
+ "source_revision": revision,
919
+ "executed": False,
920
+ "tool_execution": False,
921
+ "authority_state": "NO_ACTION_AUTHORITY",
922
+ "model": identity["model"],
923
+ "runtime": identity["runtime"],
924
+ "second_brain": {
925
+ "state": evidence["state"],
926
+ "retrieval": evidence["retrieval"],
927
+ "node_count": evidence["node_count"],
928
+ "content_hash": evidence["content_hash"],
929
+ "content_access": evidence["content_access"],
930
+ "private_graph_present": False,
931
+ "query_latency": evidence["query_latency"],
932
+ },
933
+ "evidence_handles": envelope_items,
934
+ "evidence_set_sha256": evidence_set_sha256,
935
+ "citations": citations,
936
+ "citations_sha256": canonical_sha256(citations),
937
+ "claims": claims,
938
+ "claims_sha256": canonical_sha256(claims),
939
+ "formula_authority": {
940
+ "locked_proven_count": formulas["locked_proven_count"],
941
+ "locked_proven_ids": formulas["locked_proven_ids"],
942
+ "applications": formulas["applications"],
943
+ "authorization_basis_ids": [],
944
+ "lambda": formulas["lambda"],
945
+ },
946
+ "nemo": [
947
+ {"stage": "PRE_GENERATION", **pre},
948
+ {"stage": "TEXT_R1_R5", **text_witness},
949
+ {"stage": "POST_GENERATION", **post},
950
+ ],
951
+ "metrics": metrics,
952
+ "receipt": {
953
+ "schema": RECEIPT_SCHEMA,
954
+ "payload": receipt_payload,
955
+ "receipt_sha256": receipt_digest,
956
+ "signature": {
957
+ "status": "UNSIGNED_RUNTIME",
958
+ "durable": False,
959
+ "must_be_signed_before_consequential_action": True,
960
+ },
961
+ "persistence": "EPHEMERAL_PROCESS_MEMORY_ONLY",
962
+ "prompt_or_evidence_text_persisted": False,
963
+ },
964
+ "anatomy_observation": {
965
+ "delivery": "DELIVERED",
966
+ "persistence": "EPHEMERAL_PROCESS_MEMORY_ONLY",
967
+ "observer_authority": "NONE",
968
+ "observation_count": observed["observation_count"],
969
+ "event": anatomy_event,
970
+ },
971
+ "honesty": {
972
+ "output_is_owned_khipu_inference": True,
973
+ "output_is_model_proposal": True,
974
+ "output_is_signed": False,
975
+ "action_authority": False,
976
+ "lambda_is_theorem": False,
977
+ "private_chain_of_thought_exposed": False,
978
+ },
979
+ }
980
+
981
+
982
+ def register(app: FastAPI, ns: str = "a11oy") -> str:
983
+ del ns
984
+ existing = {getattr(route, "path", None) for route in app.routes}
985
+ if "/api/v2/governed-health" in existing:
986
+ return "owned-khipu-cortex-already-registered"
987
+
988
+ @app.get("/api/v2/governed-health")
989
+ def governed_health() -> JSONResponse:
990
+ payload, status = health_payload(load_model=True)
991
+ return JSONResponse(payload, status_code=status, headers=_headers())
992
+
993
+ @app.get("/api/v2/governed-contract")
994
+ @app.get("/.well-known/szl-governed-inference-contract.json")
995
+ def governed_contract() -> JSONResponse:
996
+ return JSONResponse(contract_payload(), headers=_headers())
997
+
998
+ @app.post("/api/v2/governed-infer")
999
+ def governed_infer(request: GovernedInferenceRequest) -> JSONResponse:
1000
+ try:
1001
+ payload = infer_payload(request)
1002
+ return JSONResponse(payload, headers=_headers())
1003
+ except CortexBoundaryError as exc:
1004
+ return _error(exc.code, exc.status)
1005
+
1006
+ @app.get("/api/v2/anatomy/last")
1007
+ def anatomy_last() -> JSONResponse:
1008
+ with _ANATOMY_LOCK:
1009
+ payload = {
1010
+ "schema": "szl.anatomy.last-owned-inference/v1",
1011
+ "state": "AVAILABLE" if _ANATOMY["last"] else "EMPTY",
1012
+ "persistence": "EPHEMERAL_PROCESS_MEMORY_ONLY",
1013
+ "observer_authority": "NONE",
1014
+ "observation_count": _ANATOMY["observation_count"],
1015
+ "last": deepcopy(_ANATOMY["last"]),
1016
+ }
1017
+ return JSONResponse(payload, headers=_headers())
1018
+
1019
+ return "owned-khipu-cortex-registered"
1020
+
1021
+
1022
+ __all__ = [
1023
+ "ANATOMY_SCHEMA",
1024
+ "CONTRACT_SCHEMA",
1025
+ "CortexBoundaryError",
1026
+ "GovernedInferenceRequest",
1027
+ "HEALTH_SCHEMA",
1028
+ "LOCKED_FORMULAS",
1029
+ "MODEL_FILENAME",
1030
+ "MODEL_REPOSITORY",
1031
+ "MODEL_REVISION",
1032
+ "MODEL_SHA256",
1033
+ "MODEL_SIZE",
1034
+ "NEMO_REVISION",
1035
+ "NEMO_VERSION",
1036
+ "RECEIPT_SCHEMA",
1037
+ "RESPONSE_SCHEMA",
1038
+ "canonical_bytes",
1039
+ "canonical_sha256",
1040
+ "contract_payload",
1041
+ "health_payload",
1042
+ "infer_payload",
1043
+ "model_path",
1044
+ "register",
1045
+ "source_revision",
1046
+ "text_sha256",
1047
+ "verify_model_artifact",
1048
+ ]
scripts/fetch_owned_khipu_wheel.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ # SPDX-License-Identifier: Apache-2.0
3
+ """Fetch the one pinned CPU wheel without a remote Docker ADD source.
4
+
5
+ Build-only supply-chain helper. No model, training, credential or provider
6
+ mutation authority. Bytes reach the final wheel path only after size and
7
+ SHA-256 verification. The Dockerfile separately verifies glibc linkage.
8
+ """
9
+ from __future__ import annotations
10
+
11
+ import hashlib
12
+ import os
13
+ import tempfile
14
+ import time
15
+ import urllib.parse
16
+ import urllib.request
17
+ from pathlib import Path
18
+
19
+ WHEEL = "llama_cpp_python-0.3.35-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl"
20
+ URL = "https://github.com/abetlen/llama-cpp-python/releases/download/v0.3.35/" + WHEEL
21
+ EXPECTED_SIZE = 23912624
22
+ EXPECTED_SHA256 = "d172f3d3c8cdd194c3c47c71cb077ed6e61354a2d0f939ceeac0c8fd29999596"
23
+ ALLOWED_HOSTS = frozenset({"github.com", "release-assets.githubusercontent.com", "objects.githubusercontent.com"})
24
+ MAX_SECONDS = 120.0
25
+ SOCKET_TIMEOUT = 20.0
26
+
27
+
28
+ def validate_url(url: str) -> None:
29
+ parsed = urllib.parse.urlsplit(url)
30
+ if (parsed.scheme != "https" or parsed.hostname not in ALLOWED_HOSTS
31
+ or parsed.username is not None or parsed.password is not None
32
+ or parsed.port not in (None, 443)):
33
+ raise ValueError("wheel transport requires an approved HTTPS release host")
34
+
35
+
36
+ class ReleaseRedirects(urllib.request.HTTPRedirectHandler):
37
+ def redirect_request(self, req, fp, code, msg, headers, newurl):
38
+ # Validate before urllib makes the redirected request, not afterward.
39
+ validate_url(newurl)
40
+ return super().redirect_request(req, fp, code, msg, headers, newurl)
41
+
42
+
43
+ def fetch_wheel(directory: Path = Path("/wheels")) -> Path:
44
+ validate_url(URL)
45
+ directory.mkdir(parents=True, exist_ok=True)
46
+ target = directory / WHEEL
47
+ temporary: Path | None = None
48
+ started = time.monotonic()
49
+ digest = hashlib.sha256()
50
+ total = 0
51
+ opener = urllib.request.build_opener(ReleaseRedirects())
52
+ request = urllib.request.Request(URL, headers={"User-Agent": "SZL-Owned-Wheel-Build/1.0"})
53
+ try:
54
+ with opener.open(request, timeout=SOCKET_TIMEOUT) as response:
55
+ validate_url(response.geturl())
56
+ if response.status != 200:
57
+ raise ValueError("wheel transport did not return HTTP 200")
58
+ with tempfile.NamedTemporaryFile(prefix=".owned-wheel-", dir=directory, delete=False) as out:
59
+ temporary = Path(out.name)
60
+ while True:
61
+ if time.monotonic() - started > MAX_SECONDS:
62
+ raise TimeoutError("wheel transport exceeded its elapsed-time budget")
63
+ chunk = response.read(min(1024 * 1024, EXPECTED_SIZE - total + 1))
64
+ if not chunk:
65
+ break
66
+ total += len(chunk)
67
+ if total > EXPECTED_SIZE:
68
+ raise ValueError("wheel exceeds its pinned byte size")
69
+ digest.update(chunk)
70
+ out.write(chunk)
71
+ out.flush()
72
+ os.fsync(out.fileno())
73
+ if total != EXPECTED_SIZE:
74
+ raise ValueError("wheel is truncated or has an unexpected byte size")
75
+ if digest.hexdigest() != EXPECTED_SHA256:
76
+ raise ValueError("wheel SHA-256 does not match the pinned release")
77
+ os.replace(temporary, target)
78
+ temporary = None
79
+ return target
80
+ finally:
81
+ if temporary is not None:
82
+ temporary.unlink(missing_ok=True)
83
+
84
+
85
+ if __name__ == "__main__":
86
+ result = fetch_wheel()
87
+ print(f"[a11oy] verified CPU wheel transport: {result.name} sha256={EXPECTED_SHA256} bytes={EXPECTED_SIZE}")
serve.py CHANGED
@@ -1848,6 +1848,22 @@ try:
1848
  except Exception as _brain_api_e: # pragma: no cover
1849
  print(f"[a11oy] Brain API NOT registered: {_brain_api_e!r}; SPA + API unaffected", file=__import__("sys").stderr)
1850
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1851
  # -- BRAIN CAPABILITIES LEDGER -- exposes the honest capability contract that the
1852
  # holographic brain must obey: OPERATIONAL / PARTIALLY OPERATIONAL / MODELED /
1853
  # SIMULATED / EXPERIMENTAL / UNAVAILABLE. Pure read; never upgrades stubs into
 
1848
  except Exception as _brain_api_e: # pragma: no cover
1849
  print(f"[a11oy] Brain API NOT registered: {_brain_api_e!r}; SPA + API unaffected", file=__import__("sys").stderr)
1850
 
1851
+ # -- OWNED KHIPU GOVERNED CORTEX -- proposal-only CPU inference over the
1852
+ # exact SZLHOLDINGS/SZL-Khipu-1.5B-GGUF artifact. Registered after the
1853
+ # Brain API and before the SPA fallback; import failure leaves existing
1854
+ # routes available and is reported honestly.
1855
+ try:
1856
+ import a11oy_governed_cortex as _a11oy_governed_cortex
1857
+ print(
1858
+ "[a11oy] " + _a11oy_governed_cortex.register(app, ns="a11oy"),
1859
+ file=__import__("sys").stderr,
1860
+ )
1861
+ except Exception as _a11oy_governed_cortex_e: # pragma: no cover
1862
+ print(
1863
+ f"[a11oy] owned Khipu cortex NOT registered: {_a11oy_governed_cortex_e!r}",
1864
+ file=__import__("sys").stderr,
1865
+ )
1866
+
1867
  # -- BRAIN CAPABILITIES LEDGER -- exposes the honest capability contract that the
1868
  # holographic brain must obey: OPERATIONAL / PARTIALLY OPERATIONAL / MODELED /
1869
  # SIMULATED / EXPERIMENTAL / UNAVAILABLE. Pure read; never upgrades stubs into
szl_dark_surfaces_register.py CHANGED
@@ -40,6 +40,12 @@ from __future__ import annotations
40
  import sys
41
  from typing import Any, Callable, List, Tuple
42
 
 
 
 
 
 
 
43
  # Doctrine echo block — same shape the other szl_* surfaces emit, so any response
44
  # that reuses this constant stays honesty-clean under the doctrine guards.
45
  DOCTRINE = {
@@ -81,7 +87,6 @@ def register(app: Any, ns: str = "a11oy") -> List[str]:
81
  """
82
  status: List[str] = []
83
 
84
- # (module_name, label, expected_primary_route) for the register(app, ns) surfaces.
85
  fn_surfaces: List[Tuple[str, str, str]] = [
86
  ("szl_energy_budget", "Energy-budget receipt", f"/api/{ns}/v1/energy/budget"),
87
  ("szl_engine_status", "Unified engine status", f"/api/{ns}/v1/engine/status"),
@@ -95,33 +100,17 @@ def register(app: Any, ns: str = "a11oy") -> List[str]:
95
  try:
96
  line, _ = _register_via_fn(app, ns, module_name, label, expect)
97
  status.append(line)
98
- except Exception as exc: # additive: one missing module never blocks the rest
99
  line = f"[a11oy:dark] {label} NOT registered ({module_name}): {exc!r}"
100
  _stderr(line)
101
  status.append(line)
102
 
103
- # AYNI-OS is a self-contained APIRouter whose routes declare bare paths
104
- # (/v1/ayni, /v1/replay, /v1/tinkuy). The DOCUMENTED, dashboard-facing contract
105
- # is /api/<ns>/v1/ayni (the same /api/<ns>/v1/* shape every other dark surface
106
- # uses); mounting the router WITHOUT a prefix left it at /v1/ayni, so the
107
- # documented /api/<ns>/v1/ayni 404'd (it fell through to the Node proxy, which
108
- # answered {"error":"not found","path":"/v1/ayni"}). FIX: include the router under
109
- # prefix=/api/<ns> so the documented /api/<ns>/v1/ayni path resolves LOCALLY and
110
- # wins ordering (registered here, before the SPA catch-all / Node proxy). We ALSO
111
- # keep the legacy bare /v1/ayni mount for back-compat — purely additive, so any
112
- # existing caller of the old path is never broken. NOT register(app, ns) — own
113
- # try/except below so AYNI absent never blocks the other six surfaces.
114
  try:
115
  from ayni_os_serve import router as _ayni_router # type: ignore
116
  included = False
117
  include_router = getattr(app, "include_router", None)
118
  if callable(include_router):
119
- # Documented path: /api/<ns>/v1/ayni (+ /replay, /tinkuy). Resolves LOCALLY.
120
  app.include_router(_ayni_router, prefix=f"/api/{ns}")
121
- # Legacy paths: current FastAPI versions de-duplicate a second inclusion
122
- # of the same APIRouter object, even when its prefix differs. Append the
123
- # original APIRoute objects instead so the claimed bare /v1/* contract is
124
- # actually present while the prefixed clones remain untouched.
125
  existing_paths = {
126
  getattr(route, "path", None) for route in app.router.routes
127
  }
@@ -131,8 +120,6 @@ def register(app: Any, ns: str = "a11oy") -> List[str]:
131
  existing_paths.add(getattr(route, "path", None))
132
  included = True
133
  else:
134
- # Bare Starlette fallback: splice the router's routes onto app.router at
135
- # BOTH the documented /api/<ns> prefix and the legacy bare path.
136
  from starlette.routing import Route as _Route
137
  for _r in getattr(_ayni_router, "routes", []):
138
  _path = getattr(_r, "path", None)
@@ -146,7 +133,7 @@ def register(app: Any, ns: str = "a11oy") -> List[str]:
146
  line = f"[a11oy:dark] AYNI-OS mounted: /api/{ns}/v1/ayni + /api/{ns}/v1/replay + /api/{ns}/v1/tinkuy (legacy /v1/ayni kept)"
147
  _stderr(line)
148
  status.append(line)
149
- except Exception as exc: # additive: AYNI absent never blocks the other six
150
  line = f"[a11oy:dark] AYNI-OS NOT mounted (ayni_os_serve): {exc!r}"
151
  _stderr(line)
152
  status.append(line)
 
40
  import sys
41
  from typing import Any, Callable, List, Tuple
42
 
43
+ try:
44
+ from szl_anatomy_alias_bind import bind_ptg_redirect
45
+ bind_ptg_redirect()
46
+ except Exception:
47
+ pass
48
+
49
  # Doctrine echo block — same shape the other szl_* surfaces emit, so any response
50
  # that reuses this constant stays honesty-clean under the doctrine guards.
51
  DOCTRINE = {
 
87
  """
88
  status: List[str] = []
89
 
 
90
  fn_surfaces: List[Tuple[str, str, str]] = [
91
  ("szl_energy_budget", "Energy-budget receipt", f"/api/{ns}/v1/energy/budget"),
92
  ("szl_engine_status", "Unified engine status", f"/api/{ns}/v1/engine/status"),
 
100
  try:
101
  line, _ = _register_via_fn(app, ns, module_name, label, expect)
102
  status.append(line)
103
+ except Exception as exc:
104
  line = f"[a11oy:dark] {label} NOT registered ({module_name}): {exc!r}"
105
  _stderr(line)
106
  status.append(line)
107
 
 
 
 
 
 
 
 
 
 
 
 
108
  try:
109
  from ayni_os_serve import router as _ayni_router # type: ignore
110
  included = False
111
  include_router = getattr(app, "include_router", None)
112
  if callable(include_router):
 
113
  app.include_router(_ayni_router, prefix=f"/api/{ns}")
 
 
 
 
114
  existing_paths = {
115
  getattr(route, "path", None) for route in app.router.routes
116
  }
 
120
  existing_paths.add(getattr(route, "path", None))
121
  included = True
122
  else:
 
 
123
  from starlette.routing import Route as _Route
124
  for _r in getattr(_ayni_router, "routes", []):
125
  _path = getattr(_r, "path", None)
 
133
  line = f"[a11oy:dark] AYNI-OS mounted: /api/{ns}/v1/ayni + /api/{ns}/v1/replay + /api/{ns}/v1/tinkuy (legacy /v1/ayni kept)"
134
  _stderr(line)
135
  status.append(line)
136
+ except Exception as exc:
137
  line = f"[a11oy:dark] AYNI-OS NOT mounted (ayni_os_serve): {exc!r}"
138
  _stderr(line)
139
  status.append(line)