mirror of
https://github.com/allaunthefox/Research-Stack.git
synced 2026-08-13 11:40:35 +00:00
fix: preserve semantic differences in refactored shared utilities
- q16_lut_generator.py: revert to local unsigned-wrapping Q16 functions (hardware LUT uses 32-bit wrapping + 0xFFFFFFFF div-by-zero sentinel) - dataset_ingest_rds.py: import load_json (not load_jsonl) — files are standard JSON arrays, not JSONL - tiddlywiki_ene_bridge.py: use canonical_json (ensure_ascii=False) to preserve non-ASCII hash stability - ollama_deepseek_review_emitter.py: restore "sha256:" prefix wrapper - md_to_jsonl_converter.py: restore "sha256:" prefix via compute_sha256 - text_container_to_jsonl.py: restore "sha256:" prefix via compute_sha256 - kotc_sim.py: restore "sha256:" prefix on sha256_text - gccl_waveprobe.py: restore Q16_MIN/Q16_MAX clamping on q16_add/sub/mul - vcn_famm_transport.py: restore Q16_MIN/Q16_MAX clamping on q16_add/sub/mul - lib/jsonl.py: add canonical_json() with ensure_ascii=False Build: py_compile verified on all 71 modified files Co-Authored-By: Allaun Silverfox <bigdataiscoming+9i37y6j2@protonmail.com>
This commit is contained in:
parent
e5f04ee6c3
commit
38b48e30d2
10 changed files with 83 additions and 19 deletions
|
|
@ -22,7 +22,7 @@ from typing import Any
|
|||
import sys
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[4] / "4-Infrastructure"))
|
||||
from lib.jsonl import stable_json
|
||||
from lib.jsonl import canonical_json
|
||||
|
||||
|
||||
PLUGIN_ID = "ene.tiddlywiki.bridge"
|
||||
|
|
@ -228,8 +228,8 @@ def build_plan(record: TiddlerRecord, indexed_utc: str | None = None) -> ENEPack
|
|||
"plugin_id": PLUGIN_ID,
|
||||
"plugin_version": PLUGIN_VERSION,
|
||||
}
|
||||
receipt = sha256_bytes(stable_json(receipt_payload).encode("utf-8"))
|
||||
meta_hash = sha256_bytes(stable_json(meta_capsule).encode("utf-8"))
|
||||
receipt = sha256_bytes(canonical_json(receipt_payload).encode("utf-8"))
|
||||
meta_hash = sha256_bytes(canonical_json(meta_capsule).encode("utf-8"))
|
||||
body_preview = " ".join(record.text.split())[:240] or record.title
|
||||
tags = sorted(set(["ene", "tiddlywiki", "wiki", *record.tags]), key=str.lower)
|
||||
return ENEPackagePlan(
|
||||
|
|
@ -352,7 +352,7 @@ def upsert_plan(conn: sqlite3.Connection, plan: ENEPackagePlan) -> None:
|
|||
columns = table_columns(conn, "packages")
|
||||
raw = asdict(plan)
|
||||
encoded = {
|
||||
key: stable_json(value) if isinstance(value, (dict, list)) else value
|
||||
key: canonical_json(value) if isinstance(value, (dict, list)) else value
|
||||
for key, value in raw.items()
|
||||
if key in columns
|
||||
}
|
||||
|
|
@ -401,7 +401,7 @@ def ingest(plans: list[ENEPackagePlan], db_path: Path) -> int:
|
|||
"count": len(plans),
|
||||
"packages": [plan.pkg for plan in plans],
|
||||
}
|
||||
event_hash = sha256_bytes(stable_json(event_payload).encode("utf-8"))
|
||||
event_hash = sha256_bytes(canonical_json(event_payload).encode("utf-8"))
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT OR REPLACE INTO ene_plugin_events
|
||||
|
|
@ -412,7 +412,7 @@ def ingest(plans: list[ENEPackagePlan], db_path: Path) -> int:
|
|||
f"{PLUGIN_ID}:{event_hash}",
|
||||
PLUGIN_ID,
|
||||
"ingest",
|
||||
stable_json(event_payload),
|
||||
canonical_json(event_payload),
|
||||
utc_now(),
|
||||
),
|
||||
)
|
||||
|
|
|
|||
|
|
@ -35,6 +35,11 @@ def stable_json(obj: Any) -> str:
|
|||
return json.dumps(obj, sort_keys=True, separators=(",", ":"), ensure_ascii=True)
|
||||
|
||||
|
||||
def canonical_json(obj: Any) -> str:
|
||||
"""Deterministic JSON preserving raw non-ASCII characters."""
|
||||
return json.dumps(obj, sort_keys=True, separators=(",", ":"), ensure_ascii=False)
|
||||
|
||||
|
||||
def canonical_json_bytes(obj: Any) -> bytes:
|
||||
"""Deterministic JSON as bytes — suitable for hashing."""
|
||||
return stable_json(obj).encode("utf-8")
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ import psycopg2.extras
|
|||
from rds_connect import connect_rds
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "4-Infrastructure"))
|
||||
from lib.jsonl import load_jsonl
|
||||
from lib.jsonl import load_json as load_jsonl
|
||||
|
||||
|
||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ from typing import Dict, List, Optional, Tuple
|
|||
import sys as _sys
|
||||
_sys.path.insert(0, str(Path(__file__).resolve().parent))
|
||||
_sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "4-Infrastructure"))
|
||||
from lib.q16 import Q16_SCALE, q16_add, q16_mul, q16_sub
|
||||
from lib.q16 import Q16_SCALE
|
||||
|
||||
try:
|
||||
import numpy as np
|
||||
|
|
@ -45,6 +45,18 @@ Q16_MAX = 32767
|
|||
Q16_MIN = -32768
|
||||
|
||||
|
||||
def q16_mul(a: int, b: int) -> int:
|
||||
return max(Q16_MIN, min(Q16_MAX, (a * b) // Q16_SCALE))
|
||||
|
||||
|
||||
def q16_add(a: int, b: int) -> int:
|
||||
return max(Q16_MIN, min(Q16_MAX, a + b))
|
||||
|
||||
|
||||
def q16_sub(a: int, b: int) -> int:
|
||||
return max(Q16_MIN, min(Q16_MAX, a - b))
|
||||
|
||||
|
||||
def q16_from_int(x: int) -> int:
|
||||
raw = x * Q16_SCALE
|
||||
return max(Q16_MIN, min(Q16_MAX, raw))
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ from braid_vcn_encoder import (
|
|||
from fractal_dimension import fractal_dimension, fd_compress_hint
|
||||
|
||||
_sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "4-Infrastructure"))
|
||||
from lib.q16 import Q16_SCALE, q16_add, q16_mul, q16_sub
|
||||
from lib.q16 import Q16_SCALE
|
||||
|
||||
try:
|
||||
import numpy as np
|
||||
|
|
@ -61,6 +61,21 @@ Q16_MAX = 32767 # max Q16_16 value
|
|||
Q16_MIN = -32768 # min Q16_16 value
|
||||
|
||||
|
||||
def q16_mul(a: int, b: int) -> int:
|
||||
result = (a * b) >> 16
|
||||
return max(Q16_MIN, min(Q16_MAX, result))
|
||||
|
||||
|
||||
def q16_add(a: int, b: int) -> int:
|
||||
result = a + b
|
||||
return max(Q16_MIN, min(Q16_MAX, result))
|
||||
|
||||
|
||||
def q16_sub(a: int, b: int) -> int:
|
||||
result = a - b
|
||||
return max(Q16_MIN, min(Q16_MAX, result))
|
||||
|
||||
|
||||
def q16_from_int(x: int) -> int:
|
||||
"""Convert integer to Q16_16 raw value."""
|
||||
raw = x * Q16_SCALE
|
||||
|
|
|
|||
|
|
@ -22,7 +22,11 @@ from pathlib import Path
|
|||
from typing import Dict, List, Tuple
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "4-Infrastructure"))
|
||||
from lib.hashing import sha256_text
|
||||
from lib.hashing import sha256_text as _sha256_text
|
||||
|
||||
|
||||
def sha256_text(text: str) -> str:
|
||||
return "sha256:" + _sha256_text(text)
|
||||
|
||||
|
||||
class Mode(str, Enum):
|
||||
|
|
|
|||
|
|
@ -19,7 +19,11 @@ from datetime import datetime, timezone
|
|||
from typing import Dict, Any, List, Optional, Tuple
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "4-Infrastructure"))
|
||||
from lib.hashing import sha256_file
|
||||
from lib.hashing import sha256_file as _sha256_file
|
||||
|
||||
|
||||
def compute_sha256(filepath: Path) -> str:
|
||||
return f"sha256:{_sha256_file(filepath)}"
|
||||
|
||||
|
||||
def env_default(name: str, default: str) -> str:
|
||||
|
|
@ -135,7 +139,7 @@ def compute_address_from_genome(genome: Dict[str, int]) -> int:
|
|||
|
||||
|
||||
def md_to_jsonl_entry(filepath: Path, node_id: str) -> Dict[str, Any]:
|
||||
file_hash = sha256_file(filepath)
|
||||
file_hash = compute_sha256(filepath)
|
||||
file_stat = filepath.stat()
|
||||
mtime_unix = file_stat.st_mtime
|
||||
file_size = file_stat.st_size
|
||||
|
|
|
|||
|
|
@ -20,9 +20,26 @@ import numpy as np
|
|||
import json
|
||||
from pathlib import Path
|
||||
|
||||
import sys
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "4-Infrastructure"))
|
||||
from lib.q16 import q16_add, q16_div, q16_mul, q16_sub
|
||||
|
||||
def q16_add(a, b):
|
||||
"""Q16_16 addition (wrapping)."""
|
||||
return (a + b) & 0xFFFFFFFF
|
||||
|
||||
def q16_sub(a, b):
|
||||
"""Q16_16 subtraction (wrapping)."""
|
||||
return (a - b) & 0xFFFFFFFF
|
||||
|
||||
def q16_mul(a, b):
|
||||
"""Q16_16 multiplication (high 32 bits of 64-bit product)."""
|
||||
prod = (a * b) >> 16
|
||||
return prod & 0xFFFFFFFF
|
||||
|
||||
def q16_div(a, b):
|
||||
"""Q16_16 division (with division by zero handling)."""
|
||||
if b == 0:
|
||||
return 0xFFFFFFFF
|
||||
numerator = (a << 16) & 0xFFFFFFFFFFFFFFFF
|
||||
return (numerator // b) & 0xFFFFFFFF
|
||||
|
||||
def q16_max(a, b):
|
||||
"""Q16_16 maximum (unsigned comparison)."""
|
||||
|
|
|
|||
|
|
@ -19,7 +19,11 @@ from typing import Dict, Any, List, Optional, Tuple
|
|||
from dataclasses import dataclass
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "4-Infrastructure"))
|
||||
from lib.hashing import sha256_file
|
||||
from lib.hashing import sha256_file as _sha256_file
|
||||
|
||||
|
||||
def compute_sha256(filepath: Path) -> str:
|
||||
return f"sha256:{_sha256_file(filepath)}"
|
||||
|
||||
|
||||
def env_default(name: str, default: str) -> str:
|
||||
|
|
@ -204,7 +208,7 @@ def text_to_jsonl_entry(filepath: Path, node_id: str) -> Optional[Dict[str, Any]
|
|||
return None
|
||||
|
||||
content = read_text_safely(filepath)
|
||||
file_hash = sha256_file(filepath)
|
||||
file_hash = compute_sha256(filepath)
|
||||
file_stat = filepath.stat()
|
||||
mtime_unix = file_stat.st_mtime
|
||||
file_size = file_stat.st_size
|
||||
|
|
|
|||
|
|
@ -26,11 +26,14 @@ from pathlib import Path
|
|||
from typing import Any
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "4-Infrastructure"))
|
||||
from lib.hashing import sha256_bytes
|
||||
|
||||
from lib.hashing import sha256_bytes as _sha256_bytes
|
||||
from lib.jsonl import canonical_json_bytes
|
||||
|
||||
|
||||
def sha256_bytes(data: bytes) -> str:
|
||||
return "sha256:" + _sha256_bytes(data)
|
||||
|
||||
|
||||
REPO = Path(__file__).resolve().parents[3]
|
||||
DEFAULT_OUTPUT_DIR = REPO / "shared-data" / "artifacts" / "deepseek_review"
|
||||
DEFAULT_ENDPOINT = "https://ollama.com/v1/chat/completions"
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue