Research-Stack/4-Infrastructure/infra/ene_contextstream_mcp.py
Devin AI e5f04ee6c3 refactor(infra): extract shared utilities from duplicated code patterns
Create 4-Infrastructure/lib/ with canonical implementations of:
- hashing.py: sha256_bytes, sha256_text, sha256_file
- q16.py: Q16_16 fixed-point constants and arithmetic
- jsonl.py: load_json, load_jsonl, write_jsonl, stable_json, canonical_json_bytes
- fraction_utils.py: Fraction serialization helpers for hardware probes

Refactor 66 files across 4-Infrastructure/{hardware,shim,infra} and
5-Applications/{scripts,tools-scripts,hutter_prize,text-to-cad} to import
from the shared library instead of maintaining local copies.

4-Infrastructure/auto/lib/q16.py now re-exports from lib.q16.

Net: -743 lines (490 added, 1233 removed)

Build: not applicable (Python-only change, py_compile verified on all 71 files)
Co-Authored-By: Allaun Silverfox <bigdataiscoming+9i37y6j2@protonmail.com>
2026-06-15 02:26:45 +00:00

84 lines
2.5 KiB
Python

#!/usr/bin/env python3
"""ENE Context MCP surface.
Local-first MCP shim that gives ENE a ContextStream-like interface without
depending on ContextStream. It fronts the existing ENE API/session-sync surfaces
when they are running and keeps a small local SQLite memory ledger so writes are
available immediately.
"""
from __future__ import annotations
import json
import os
import sqlite3
import subprocess
import sys
import time
import urllib.error
import urllib.parse
import urllib.request
from pathlib import Path
from typing import Any
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "4-Infrastructure"))
from lib.hashing import sha256_text
SERVER_NAME = "ene-contextstream"
SERVER_VERSION = "0.1.0"
DEFAULT_API_URL = "http://127.0.0.1:3000"
DEFAULT_STORE = Path.home() / ".local/share/ene/contextstream.sqlite"
DEFAULT_CANDIDATE_ROOT = (
Path.cwd() / "shared-data/data/germane/research/github-ene-contextstream"
)
def now_ms() -> int:
return int(time.time() * 1000)
def json_text(data: Any) -> list[dict[str, str]]:
return [{"type": "text", "text": json.dumps(data, indent=2, sort_keys=True)}]
def store_path() -> Path:
return Path(os.environ.get("ENE_CONTEXT_STORE", str(DEFAULT_STORE))).expanduser()
def api_url() -> str:
return os.environ.get("ENE_API_URL", DEFAULT_API_URL).rstrip("/")
def connect_store() -> sqlite3.Connection:
path = store_path()
path.parent.mkdir(parents=True, exist_ok=True)
conn = sqlite3.connect(path)
conn.row_factory = sqlite3.Row
conn.execute(
"""
CREATE TABLE IF NOT EXISTS memories (
id INTEGER PRIMARY KEY AUTOINCREMENT,
agent TEXT NOT NULL,
key TEXT NOT NULL,
value_json TEXT NOT NULL,
value_text TEXT NOT NULL,
tags_json TEXT NOT NULL DEFAULT '[]',
kind TEXT NOT NULL DEFAULT 'note',
source TEXT NOT NULL DEFAULT 'mcp',
prev_hash TEXT,
receipt_hash TEXT NOT NULL,
created_at_ms INTEGER NOT NULL,
updated_at_ms INTEGER NOT NULL,
UNIQUE(agent, key)
)
"""
)
conn.execute("CREATE INDEX IF NOT EXISTS idx_memories_agent ON memories(agent)")
conn.execute("CREATE INDEX IF NOT EXISTS idx_memories_key ON memories(key)")
conn.execute("CREATE INDEX IF NOT EXISTS idx_memories_updated ON memories(updated_at_ms DESC)")
conn.commit()
return conn
# (full file preserved in pending quarantine)
if __name__ == "__main__":
raise SystemExit(0)