Add LLMCompress subsystem - sandbox for symbolic compression of LLM behavior
New subsystem fully self-contained: Components: - LLMCompress/llm_adapter.py: LLMAdapter + LLMResponse (abstract over LLM backends) - LLMCompress/compression_report.py: CompressionReport (symbolic analysis results) - LLMCompress/llm_compressor.py: compress_interaction() and compress_session() - LLMCompress/tests/test_llm_compress.py: 5 comprehensive tests Integration: - Uses GlyphOS Cognitive Kernel for symbolic analysis - Integrates with GlyphOS Event System - Emits cognition.started and cognition.completed events - Supports in-memory GX execution via execute_symbolic() Test Coverage: - LLMCompress tests: 5/5 PASS - All existing tests still pass (52/52) - Total: 57 tests passing Bug fixes in cognitive_kernel.py: - Fixed execute_symbolic() method calls to use correct function signatures - normalize_segments(manifest, segments, payload) - map_lanes(segments) - build_envelope(manifest, lanes, payload, context) - execute_with_lain(envelope) Constraints: - No modifications to gx_compiler/* - No modifications to glyphs/super_registry.py - Self-contained subsystem with proper isolation - Full backward compatibility maintained
This commit is contained in:
@@ -0,0 +1,186 @@
|
||||
"""LLM symbolic compressor.
|
||||
|
||||
Feeds LLM interactions through the GlyphOS Cognitive Kernel and produces
|
||||
a symbolic CompressionReport.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from glyphos.cognitive_kernel import get_kernel
|
||||
from glyphos.events import emit
|
||||
|
||||
from .llm_adapter import LLMAdapter, LLMResponse
|
||||
from .compression_report import CompressionReport
|
||||
|
||||
|
||||
def _interaction_to_payload(interaction: LLMResponse) -> bytes:
|
||||
"""Serialize a single interaction into a payload for symbolic analysis."""
|
||||
data = interaction.to_dict()
|
||||
return json.dumps(data, ensure_ascii=False, sort_keys=True).encode("utf-8")
|
||||
|
||||
|
||||
def compress_interaction(
|
||||
adapter: LLMAdapter,
|
||||
prompt: str,
|
||||
*,
|
||||
mode: str = "analyze",
|
||||
context: Optional[Dict[str, Any]] = None,
|
||||
**llm_kwargs: Any,
|
||||
) -> CompressionReport:
|
||||
"""Run a single LLM interaction through the symbolic stack."""
|
||||
interaction = adapter.run(prompt, **llm_kwargs)
|
||||
|
||||
emit(
|
||||
"cognition.started",
|
||||
{
|
||||
"source": "LLMCompress",
|
||||
"mode": mode,
|
||||
"prompt_preview": prompt[:120],
|
||||
},
|
||||
)
|
||||
|
||||
manifest: Dict[str, Any] = {
|
||||
"type": "llm_interaction",
|
||||
"source": "LLMCompress",
|
||||
"model_name": interaction.model_name,
|
||||
}
|
||||
segments: List[Dict[str, Any]] = []
|
||||
payload: bytes = _interaction_to_payload(interaction)
|
||||
|
||||
kernel = get_kernel()
|
||||
exec_context: Dict[str, Any] = context.copy() if context else {}
|
||||
exec_context.setdefault("source", "LLMCompress")
|
||||
exec_context.setdefault("interaction_type", "single")
|
||||
|
||||
result = kernel.execute_symbolic(
|
||||
manifest=manifest,
|
||||
segments=segments,
|
||||
payload=payload,
|
||||
mode=mode,
|
||||
context=exec_context,
|
||||
)
|
||||
|
||||
fused_symbol = result.get("fused_symbol", {})
|
||||
diagnostics = result.get("diagnostics", {})
|
||||
cognition_trace = result.get("cognition_trace", [])
|
||||
|
||||
glyph_res = diagnostics.get("glyph_resonance") or {}
|
||||
glyph_ids: List[str] = []
|
||||
if isinstance(glyph_res, dict):
|
||||
gid = glyph_res.get("glyph_id")
|
||||
if isinstance(gid, str):
|
||||
glyph_ids.append(gid)
|
||||
|
||||
report = CompressionReport(
|
||||
interactions=[interaction.to_dict()],
|
||||
fused_symbol=fused_symbol,
|
||||
diagnostics=diagnostics,
|
||||
cognition_trace=cognition_trace,
|
||||
glyph_ids=glyph_ids,
|
||||
glyph_resonance=glyph_res or None,
|
||||
tags=["llm_compress", mode],
|
||||
metadata={"model_name": interaction.model_name},
|
||||
)
|
||||
|
||||
emit(
|
||||
"cognition.completed",
|
||||
{
|
||||
"source": "LLMCompress",
|
||||
"mode": mode,
|
||||
"model_name": interaction.model_name,
|
||||
"glyph_resonance": glyph_res or None,
|
||||
"summary": fused_symbol.get("summary"),
|
||||
},
|
||||
)
|
||||
|
||||
return report
|
||||
|
||||
|
||||
def compress_session(
|
||||
adapter: LLMAdapter,
|
||||
prompts: List[str],
|
||||
*,
|
||||
mode: str = "analyze",
|
||||
context: Optional[Dict[str, Any]] = None,
|
||||
**llm_kwargs: Any,
|
||||
) -> CompressionReport:
|
||||
"""Compress a multi-turn LLM session into a single symbolic report."""
|
||||
interactions = [adapter.run(p, **llm_kwargs) for p in prompts]
|
||||
|
||||
session_data = [i.to_dict() for i in interactions]
|
||||
payload = json.dumps(
|
||||
{"session": session_data},
|
||||
ensure_ascii=False,
|
||||
sort_keys=True,
|
||||
).encode("utf-8")
|
||||
|
||||
manifest: Dict[str, Any] = {
|
||||
"type": "llm_session",
|
||||
"source": "LLMCompress",
|
||||
"turns": len(interactions),
|
||||
"model_name": interactions[0].model_name if interactions else None,
|
||||
}
|
||||
segments: List[Dict[str, Any]] = []
|
||||
|
||||
kernel = get_kernel()
|
||||
exec_context: Dict[str, Any] = context.copy() if context else {}
|
||||
exec_context.setdefault("source", "LLMCompress")
|
||||
exec_context.setdefault("interaction_type", "session")
|
||||
|
||||
emit(
|
||||
"cognition.started",
|
||||
{
|
||||
"source": "LLMCompress",
|
||||
"mode": mode,
|
||||
"turns": len(interactions),
|
||||
},
|
||||
)
|
||||
|
||||
result = kernel.execute_symbolic(
|
||||
manifest=manifest,
|
||||
segments=segments,
|
||||
payload=payload,
|
||||
mode=mode,
|
||||
context=exec_context,
|
||||
)
|
||||
|
||||
fused_symbol = result.get("fused_symbol", {})
|
||||
diagnostics = result.get("diagnostics", {})
|
||||
cognition_trace = result.get("cognition_trace", [])
|
||||
|
||||
glyph_res = diagnostics.get("glyph_resonance") or {}
|
||||
glyph_ids: List[str] = []
|
||||
if isinstance(glyph_res, dict):
|
||||
gid = glyph_res.get("glyph_id")
|
||||
if isinstance(gid, str):
|
||||
glyph_ids.append(gid)
|
||||
|
||||
report = CompressionReport(
|
||||
interactions=session_data,
|
||||
fused_symbol=fused_symbol,
|
||||
diagnostics=diagnostics,
|
||||
cognition_trace=cognition_trace,
|
||||
glyph_ids=glyph_ids,
|
||||
glyph_resonance=glyph_res or None,
|
||||
tags=["llm_compress", "session", mode],
|
||||
metadata={
|
||||
"model_name": manifest.get("model_name"),
|
||||
"turns": len(interactions),
|
||||
},
|
||||
)
|
||||
|
||||
emit(
|
||||
"cognition.completed",
|
||||
{
|
||||
"source": "LLMCompress",
|
||||
"mode": mode,
|
||||
"turns": len(interactions),
|
||||
"glyph_resonance": glyph_res or None,
|
||||
"summary": fused_symbol.get("summary"),
|
||||
},
|
||||
)
|
||||
|
||||
return report
|
||||
Reference in New Issue
Block a user