cognitive-engine 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cognitive_engine/__init__.py +7 -0
- cognitive_engine/adapters/__init__.py +2 -0
- cognitive_engine/adapters/plastic_numeric_adapter.py +141 -0
- cognitive_engine/api/__init__.py +2 -0
- cognitive_engine/api/service.py +41 -0
- cognitive_engine/compression/__init__.py +2 -0
- cognitive_engine/compression/knowledge_compressor.py +53 -0
- cognitive_engine/compression/semantic_compressor_v2.py +115 -0
- cognitive_engine/config/__init__.py +2 -0
- cognitive_engine/config/loader.py +44 -0
- cognitive_engine/config/schema.py +45 -0
- cognitive_engine/consolidation/__init__.py +2 -0
- cognitive_engine/consolidation/engine.py +25 -0
- cognitive_engine/consolidation/engine_v2.py +39 -0
- cognitive_engine/context/__init__.py +2 -0
- cognitive_engine/context/long_context.py +64 -0
- cognitive_engine/core/__init__.py +2 -0
- cognitive_engine/core/builder.py +154 -0
- cognitive_engine/core/engine.py +174 -0
- cognitive_engine/core/engine_v2.py +280 -0
- cognitive_engine/core/registry.py +29 -0
- cognitive_engine/core/types.py +346 -0
- cognitive_engine/interfaces/__init__.py +2 -0
- cognitive_engine/interfaces/base.py +181 -0
- cognitive_engine/memory/__init__.py +2 -0
- cognitive_engine/memory/graph_memory.py +165 -0
- cognitive_engine/memory/hybrid_memory.py +110 -0
- cognitive_engine/memory/project_memory.py +80 -0
- cognitive_engine/memory/stores.py +177 -0
- cognitive_engine/memory/vector_store.py +28 -0
- cognitive_engine/models/__init__.py +2 -0
- cognitive_engine/models/stable_core.py +79 -0
- cognitive_engine/modules/__init__.py +2 -0
- cognitive_engine/modules/importance_evaluator.py +96 -0
- cognitive_engine/modules/input_processing.py +78 -0
- cognitive_engine/modules/semantic_understanding.py +130 -0
- cognitive_engine/nlp/__init__.py +16 -0
- cognitive_engine/nlp/models.py +116 -0
- cognitive_engine/nlp/trainer.py +95 -0
- cognitive_engine/replay/__init__.py +2 -0
- cognitive_engine/replay/buffer.py +40 -0
- cognitive_engine/routing/__init__.py +2 -0
- cognitive_engine/routing/dynamic_router.py +45 -0
- cognitive_engine/routing/learned_router.py +165 -0
- cognitive_engine/specialists/__init__.py +2 -0
- cognitive_engine/specialists/runtime.py +97 -0
- cognitive_engine/stability/__init__.py +2 -0
- cognitive_engine/stability/governor.py +38 -0
- cognitive_engine/training/__init__.py +2 -0
- cognitive_engine/training/online_trainer.py +87 -0
- cognitive_engine/utils/__init__.py +2 -0
- cognitive_engine/utils/numeric.py +67 -0
- cognitive_engine/utils/seeding.py +13 -0
- cognitive_engine/utils/telemetry.py +39 -0
- cognitive_engine/utils/text.py +104 -0
- cognitive_engine/utils/visualization.py +87 -0
- cognitive_engine-0.2.0.dist-info/METADATA +91 -0
- cognitive_engine-0.2.0.dist-info/RECORD +60 -0
- cognitive_engine-0.2.0.dist-info/WHEEL +5 -0
- cognitive_engine-0.2.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
from cognitive_engine.adapters.plastic_numeric_adapter import PlasticArithmeticModule
|
|
6
|
+
from cognitive_engine.compression.knowledge_compressor import SemanticKnowledgeCompressor
|
|
7
|
+
from cognitive_engine.compression.semantic_compressor_v2 import SemanticCompressorV2
|
|
8
|
+
from cognitive_engine.config.loader import load_engine_config
|
|
9
|
+
from cognitive_engine.config.schema import EngineConfig
|
|
10
|
+
from cognitive_engine.consolidation.engine import BackgroundConsolidationEngine
|
|
11
|
+
from cognitive_engine.consolidation.engine_v2 import CognitiveConsolidationEngineV2
|
|
12
|
+
from cognitive_engine.core.engine import CognitiveEngine
|
|
13
|
+
from cognitive_engine.core.engine_v2 import CognitiveEngineV2
|
|
14
|
+
from cognitive_engine.core.registry import GLOBAL_REGISTRY
|
|
15
|
+
from cognitive_engine.context.long_context import LongContextManager
|
|
16
|
+
from cognitive_engine.memory.graph_memory import CognitiveGraphMemory
|
|
17
|
+
from cognitive_engine.memory.hybrid_memory import HybridCognitiveMemory
|
|
18
|
+
from cognitive_engine.memory.project_memory import ProjectIndexer
|
|
19
|
+
from cognitive_engine.memory.stores import HierarchicalMemorySystem
|
|
20
|
+
from cognitive_engine.models.stable_core import StableReasoningCore
|
|
21
|
+
from cognitive_engine.modules.importance_evaluator import AdaptiveImportanceEvaluator
|
|
22
|
+
from cognitive_engine.modules.input_processing import NumericInputProcessor, TextInputProcessor
|
|
23
|
+
from cognitive_engine.modules.semantic_understanding import HybridSemanticEncoder, NumericSemanticEncoder, TextSemanticEncoder
|
|
24
|
+
from cognitive_engine.replay.buffer import PrioritizedReplayBuffer
|
|
25
|
+
from cognitive_engine.routing.dynamic_router import AdaptiveDynamicRouter
|
|
26
|
+
from cognitive_engine.routing.learned_router import LearnedCognitiveRouter
|
|
27
|
+
from cognitive_engine.specialists.runtime import SpecialistRuntime
|
|
28
|
+
from cognitive_engine.stability.governor import StabilityGovernorV2
|
|
29
|
+
from cognitive_engine.training.online_trainer import OnlineTrainer
|
|
30
|
+
from cognitive_engine.utils.seeding import set_seed
|
|
31
|
+
from cognitive_engine.utils.telemetry import JsonlTelemetrySink
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class EngineBuilder:
|
|
35
|
+
def __init__(self, config: EngineConfig | None = None, config_path: str | None = None) -> None:
|
|
36
|
+
self.config = config or load_engine_config(config_path)
|
|
37
|
+
self._register_defaults()
|
|
38
|
+
|
|
39
|
+
def build(self) -> CognitiveEngine:
|
|
40
|
+
set_seed(self.config.seed)
|
|
41
|
+
telemetry = GLOBAL_REGISTRY.create("telemetry", "jsonl", path=Path("artifacts") / "logs" / "engine_trace.jsonl")
|
|
42
|
+
replay_buffer = GLOBAL_REGISTRY.create("replay", "prioritized", capacity=self.config.memory.replay_capacity)
|
|
43
|
+
memory_system = GLOBAL_REGISTRY.create("memory", "hierarchical", config=self.config.memory)
|
|
44
|
+
processors = [
|
|
45
|
+
GLOBAL_REGISTRY.create("processor", "numeric", device=self.config.device, scale=float(self.config.numeric_demo.max_operand)),
|
|
46
|
+
GLOBAL_REGISTRY.create("processor", "text", device=self.config.device),
|
|
47
|
+
]
|
|
48
|
+
semantic_encoder = GLOBAL_REGISTRY.create(
|
|
49
|
+
"semantic_encoder",
|
|
50
|
+
"hybrid",
|
|
51
|
+
text_encoder=GLOBAL_REGISTRY.create("semantic_encoder", "text", device=self.config.device),
|
|
52
|
+
numeric_encoder=GLOBAL_REGISTRY.create("semantic_encoder", "numeric", device=self.config.device),
|
|
53
|
+
)
|
|
54
|
+
plastic_learner = GLOBAL_REGISTRY.create(
|
|
55
|
+
"plasticity",
|
|
56
|
+
"arithmetic",
|
|
57
|
+
device=self.config.device,
|
|
58
|
+
output_scale=float(self.config.numeric_demo.max_operand**2),
|
|
59
|
+
)
|
|
60
|
+
trainer = OnlineTrainer(
|
|
61
|
+
plastic_learner=plastic_learner,
|
|
62
|
+
replay_buffer=replay_buffer,
|
|
63
|
+
replay_batch_size=self.config.numeric_demo.replay_batch_size,
|
|
64
|
+
device=self.config.device,
|
|
65
|
+
)
|
|
66
|
+
consolidator = BackgroundConsolidationEngine(memory_system=memory_system, replay_buffer=replay_buffer)
|
|
67
|
+
return CognitiveEngine(
|
|
68
|
+
config=self.config,
|
|
69
|
+
processors=processors,
|
|
70
|
+
semantic_encoder=semantic_encoder,
|
|
71
|
+
importance_evaluator=GLOBAL_REGISTRY.create("importance", "adaptive", thresholds=self.config.thresholds),
|
|
72
|
+
compressor=GLOBAL_REGISTRY.create("compressor", "semantic"),
|
|
73
|
+
memory_system=memory_system,
|
|
74
|
+
router=GLOBAL_REGISTRY.create("router", "adaptive"),
|
|
75
|
+
stable_core=GLOBAL_REGISTRY.create("stable_core", "reasoning", device=self.config.device),
|
|
76
|
+
plastic_learner=plastic_learner,
|
|
77
|
+
trainer=trainer,
|
|
78
|
+
replay_buffer=replay_buffer,
|
|
79
|
+
consolidator=consolidator,
|
|
80
|
+
telemetry=telemetry,
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
def build_v2(self) -> CognitiveEngineV2:
|
|
84
|
+
set_seed(self.config.seed)
|
|
85
|
+
telemetry = GLOBAL_REGISTRY.create("telemetry", "jsonl", path=Path("artifacts") / "logs" / "engine_trace_v2.jsonl")
|
|
86
|
+
replay_buffer = GLOBAL_REGISTRY.create("replay", "prioritized", capacity=self.config.memory.replay_capacity)
|
|
87
|
+
base_memory = GLOBAL_REGISTRY.create("memory", "hierarchical", config=self.config.memory)
|
|
88
|
+
graph_memory = GLOBAL_REGISTRY.create("memory", "graph")
|
|
89
|
+
memory_system = GLOBAL_REGISTRY.create("memory", "hybrid_v2", base_memory=base_memory, graph_memory=graph_memory)
|
|
90
|
+
processors = [
|
|
91
|
+
GLOBAL_REGISTRY.create("processor", "numeric", device=self.config.device, scale=float(self.config.numeric_demo.max_operand)),
|
|
92
|
+
GLOBAL_REGISTRY.create("processor", "text", device=self.config.device),
|
|
93
|
+
]
|
|
94
|
+
semantic_encoder = GLOBAL_REGISTRY.create(
|
|
95
|
+
"semantic_encoder",
|
|
96
|
+
"hybrid",
|
|
97
|
+
text_encoder=GLOBAL_REGISTRY.create("semantic_encoder", "text", device=self.config.device),
|
|
98
|
+
numeric_encoder=GLOBAL_REGISTRY.create("semantic_encoder", "numeric", device=self.config.device),
|
|
99
|
+
)
|
|
100
|
+
plastic_learner = GLOBAL_REGISTRY.create(
|
|
101
|
+
"plasticity",
|
|
102
|
+
"arithmetic",
|
|
103
|
+
device=self.config.device,
|
|
104
|
+
output_scale=float(self.config.numeric_demo.max_operand**2),
|
|
105
|
+
)
|
|
106
|
+
trainer = OnlineTrainer(
|
|
107
|
+
plastic_learner=plastic_learner,
|
|
108
|
+
replay_buffer=replay_buffer,
|
|
109
|
+
replay_batch_size=self.config.numeric_demo.replay_batch_size,
|
|
110
|
+
device=self.config.device,
|
|
111
|
+
)
|
|
112
|
+
return CognitiveEngineV2(
|
|
113
|
+
config=self.config,
|
|
114
|
+
processors=processors,
|
|
115
|
+
semantic_encoder=semantic_encoder,
|
|
116
|
+
importance_evaluator=GLOBAL_REGISTRY.create("importance", "adaptive", thresholds=self.config.thresholds),
|
|
117
|
+
compressor_v2=GLOBAL_REGISTRY.create("compressor", "semantic_v2"),
|
|
118
|
+
memory_system=memory_system,
|
|
119
|
+
router=GLOBAL_REGISTRY.create("router", "learned", device=self.config.device),
|
|
120
|
+
stable_core=GLOBAL_REGISTRY.create("stable_core", "reasoning", device=self.config.device),
|
|
121
|
+
plastic_learner=plastic_learner,
|
|
122
|
+
trainer=trainer,
|
|
123
|
+
replay_buffer=replay_buffer,
|
|
124
|
+
consolidator=GLOBAL_REGISTRY.create("consolidator", "cognitive_v2", memory_system=memory_system, replay_buffer=replay_buffer),
|
|
125
|
+
telemetry=telemetry,
|
|
126
|
+
specialist_runtime=GLOBAL_REGISTRY.create("specialists", "runtime"),
|
|
127
|
+
context_manager=GLOBAL_REGISTRY.create("context", "long"),
|
|
128
|
+
stability_governor=GLOBAL_REGISTRY.create("stability", "governor_v2"),
|
|
129
|
+
project_indexer=GLOBAL_REGISTRY.create("project", "indexer", graph_memory=graph_memory),
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
def _register_defaults(self) -> None:
|
|
133
|
+
GLOBAL_REGISTRY.register("telemetry", "jsonl", JsonlTelemetrySink)
|
|
134
|
+
GLOBAL_REGISTRY.register("replay", "prioritized", PrioritizedReplayBuffer)
|
|
135
|
+
GLOBAL_REGISTRY.register("memory", "hierarchical", HierarchicalMemorySystem)
|
|
136
|
+
GLOBAL_REGISTRY.register("memory", "graph", CognitiveGraphMemory)
|
|
137
|
+
GLOBAL_REGISTRY.register("memory", "hybrid_v2", HybridCognitiveMemory)
|
|
138
|
+
GLOBAL_REGISTRY.register("processor", "numeric", NumericInputProcessor)
|
|
139
|
+
GLOBAL_REGISTRY.register("processor", "text", TextInputProcessor)
|
|
140
|
+
GLOBAL_REGISTRY.register("semantic_encoder", "text", TextSemanticEncoder)
|
|
141
|
+
GLOBAL_REGISTRY.register("semantic_encoder", "numeric", NumericSemanticEncoder)
|
|
142
|
+
GLOBAL_REGISTRY.register("semantic_encoder", "hybrid", HybridSemanticEncoder)
|
|
143
|
+
GLOBAL_REGISTRY.register("importance", "adaptive", AdaptiveImportanceEvaluator)
|
|
144
|
+
GLOBAL_REGISTRY.register("compressor", "semantic", SemanticKnowledgeCompressor)
|
|
145
|
+
GLOBAL_REGISTRY.register("compressor", "semantic_v2", SemanticCompressorV2)
|
|
146
|
+
GLOBAL_REGISTRY.register("router", "adaptive", AdaptiveDynamicRouter)
|
|
147
|
+
GLOBAL_REGISTRY.register("router", "learned", LearnedCognitiveRouter)
|
|
148
|
+
GLOBAL_REGISTRY.register("stable_core", "reasoning", StableReasoningCore)
|
|
149
|
+
GLOBAL_REGISTRY.register("plasticity", "arithmetic", PlasticArithmeticModule)
|
|
150
|
+
GLOBAL_REGISTRY.register("consolidator", "cognitive_v2", CognitiveConsolidationEngineV2)
|
|
151
|
+
GLOBAL_REGISTRY.register("specialists", "runtime", SpecialistRuntime)
|
|
152
|
+
GLOBAL_REGISTRY.register("context", "long", LongContextManager)
|
|
153
|
+
GLOBAL_REGISTRY.register("stability", "governor_v2", StabilityGovernorV2)
|
|
154
|
+
GLOBAL_REGISTRY.register("project", "indexer", ProjectIndexer)
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any, Dict, List, Sequence
|
|
4
|
+
|
|
5
|
+
from cognitive_engine.config.schema import EngineConfig
|
|
6
|
+
from cognitive_engine.core.types import EngineResponse, MemoryQuery, TraceEvent, tensor_to_numpy
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class CognitiveEngine:
|
|
10
|
+
def __init__(
|
|
11
|
+
self,
|
|
12
|
+
config: EngineConfig,
|
|
13
|
+
processors: Sequence[Any],
|
|
14
|
+
semantic_encoder: Any,
|
|
15
|
+
importance_evaluator: Any,
|
|
16
|
+
compressor: Any,
|
|
17
|
+
memory_system: Any,
|
|
18
|
+
router: Any,
|
|
19
|
+
stable_core: Any,
|
|
20
|
+
plastic_learner: Any,
|
|
21
|
+
trainer: Any,
|
|
22
|
+
replay_buffer: Any,
|
|
23
|
+
consolidator: Any,
|
|
24
|
+
telemetry: Any,
|
|
25
|
+
) -> None:
|
|
26
|
+
self.config = config
|
|
27
|
+
self.processors = list(processors)
|
|
28
|
+
self.semantic_encoder = semantic_encoder
|
|
29
|
+
self.importance_evaluator = importance_evaluator
|
|
30
|
+
self.compressor = compressor
|
|
31
|
+
self.memory_system = memory_system
|
|
32
|
+
self.router = router
|
|
33
|
+
self.stable_core = stable_core
|
|
34
|
+
self.plastic_learner = plastic_learner
|
|
35
|
+
self.trainer = trainer
|
|
36
|
+
self.replay_buffer = replay_buffer
|
|
37
|
+
self.consolidator = consolidator
|
|
38
|
+
self.telemetry = telemetry
|
|
39
|
+
self.step_counter = 0
|
|
40
|
+
|
|
41
|
+
def process(self, payload: Any, allow_learning: bool = True) -> EngineResponse:
|
|
42
|
+
traces: List[TraceEvent] = []
|
|
43
|
+
processed = self._select_processor(payload).process(payload)
|
|
44
|
+
traces.append(self._trace("input", "Payload processed.", {"modality": processed.modality, "metadata": processed.metadata}))
|
|
45
|
+
|
|
46
|
+
semantic_state = self.semantic_encoder.encode(processed)
|
|
47
|
+
self.memory_system.update_working_memory(semantic_state)
|
|
48
|
+
traces.append(self._trace("semantic", "Semantic state created.", {"intent": semantic_state.intent, "concepts": [c.label for c in semantic_state.concepts]}))
|
|
49
|
+
|
|
50
|
+
route = self.router.route(processed, semantic_state)
|
|
51
|
+
query = MemoryQuery(
|
|
52
|
+
embedding=tensor_to_numpy(semantic_state.pooled_embedding.float()),
|
|
53
|
+
top_k=self.config.memory.retrieval_top_k,
|
|
54
|
+
modality=processed.modality,
|
|
55
|
+
intent=semantic_state.intent,
|
|
56
|
+
concepts=[concept.label for concept in semantic_state.concepts],
|
|
57
|
+
)
|
|
58
|
+
memory_bundle = self.memory_system.retrieve(query)
|
|
59
|
+
traces.append(
|
|
60
|
+
self._trace(
|
|
61
|
+
"memory_retrieval",
|
|
62
|
+
"Memory bundle retrieved.",
|
|
63
|
+
{
|
|
64
|
+
"short_term": len(memory_bundle.short_term),
|
|
65
|
+
"semantic_long_term": len(memory_bundle.semantic_long_term),
|
|
66
|
+
"episodic": len(memory_bundle.episodic),
|
|
67
|
+
},
|
|
68
|
+
)
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
importance = self.importance_evaluator.evaluate(semantic_state, memory_bundle)
|
|
72
|
+
semantic_state.metadata["importance_action"] = importance.action
|
|
73
|
+
traces.append(
|
|
74
|
+
self._trace(
|
|
75
|
+
"importance",
|
|
76
|
+
importance.rationale,
|
|
77
|
+
{
|
|
78
|
+
"importance_score": importance.importance_score,
|
|
79
|
+
"confidence_score": importance.confidence_score,
|
|
80
|
+
"learning_priority": importance.learning_priority,
|
|
81
|
+
"action": importance.action,
|
|
82
|
+
},
|
|
83
|
+
)
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
plastic_output = None
|
|
87
|
+
if route.engage_plasticity and processed.modality == "numeric":
|
|
88
|
+
plastic_output = self.plastic_learner.predict(semantic_state)
|
|
89
|
+
traces.append(self._trace("plastic_inference", plastic_output.explanation, plastic_output.artifacts))
|
|
90
|
+
|
|
91
|
+
inference = self.stable_core.infer(semantic_state, memory_bundle, route, plastic_output)
|
|
92
|
+
traces.append(self._trace("stable_core", inference.explanation, {"confidence": inference.confidence}))
|
|
93
|
+
|
|
94
|
+
learning_applied = False
|
|
95
|
+
if allow_learning and route.update_memory and importance.action not in {"ignore", "uncertain"}:
|
|
96
|
+
knowledge = self.compressor.compress(semantic_state, importance)
|
|
97
|
+
self.memory_system.write(knowledge)
|
|
98
|
+
learning_applied = True
|
|
99
|
+
traces.append(self._trace("memory_write", "Compressed knowledge stored.", {"record_id": knowledge.record_id, "summary": knowledge.summary}))
|
|
100
|
+
|
|
101
|
+
if allow_learning and processed.modality == "numeric" and semantic_state.metadata.get("target") is not None:
|
|
102
|
+
training_metrics = self.trainer.observe(semantic_state, priority=importance.learning_priority)
|
|
103
|
+
if training_metrics is not None:
|
|
104
|
+
learning_applied = True
|
|
105
|
+
traces.append(self._trace("online_training", "Plastic module updated.", training_metrics))
|
|
106
|
+
|
|
107
|
+
self.step_counter += 1
|
|
108
|
+
if self.step_counter % self.config.memory.consolidation_interval == 0:
|
|
109
|
+
report = self.consolidator.run()
|
|
110
|
+
traces.append(
|
|
111
|
+
self._trace(
|
|
112
|
+
"consolidation",
|
|
113
|
+
report.notes,
|
|
114
|
+
{
|
|
115
|
+
"merged_records": report.merged_records,
|
|
116
|
+
"pruned_records": report.pruned_records,
|
|
117
|
+
"replay_reweighted": report.replay_reweighted,
|
|
118
|
+
},
|
|
119
|
+
)
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
for event in traces:
|
|
123
|
+
self.telemetry.emit(event)
|
|
124
|
+
self.telemetry.flush()
|
|
125
|
+
|
|
126
|
+
return EngineResponse(
|
|
127
|
+
text=str(inference.explanation),
|
|
128
|
+
semantic_state=semantic_state,
|
|
129
|
+
importance=importance,
|
|
130
|
+
routing=route,
|
|
131
|
+
memory_bundle=memory_bundle,
|
|
132
|
+
inference=inference,
|
|
133
|
+
traces=traces,
|
|
134
|
+
learning_applied=learning_applied,
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
def snapshot(self) -> Dict[str, Any]:
|
|
138
|
+
return {
|
|
139
|
+
"steps": self.step_counter,
|
|
140
|
+
"memory": self.memory_system.snapshot(),
|
|
141
|
+
"replay_size": len(self.replay_buffer),
|
|
142
|
+
"plastic_drift": self.plastic_learner.parameter_drift(),
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
def describe_architecture(self) -> Dict[str, Any]:
|
|
146
|
+
return {
|
|
147
|
+
"processors": [processor.describe() for processor in self.processors],
|
|
148
|
+
"semantic_encoder": self.semantic_encoder.describe(),
|
|
149
|
+
"importance_evaluator": self.importance_evaluator.describe(),
|
|
150
|
+
"compressor": self.compressor.describe(),
|
|
151
|
+
"memory_system": self.memory_system.describe(),
|
|
152
|
+
"router": self.router.describe(),
|
|
153
|
+
"stable_core": self.stable_core.describe(),
|
|
154
|
+
"plastic_learner": self.plastic_learner.describe(),
|
|
155
|
+
"replay_buffer": self.replay_buffer.describe(),
|
|
156
|
+
"consolidator": self.consolidator.describe(),
|
|
157
|
+
"telemetry": self.telemetry.describe(),
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
def replace_component(self, slot: str, component: Any) -> Any:
|
|
161
|
+
if not hasattr(self, slot):
|
|
162
|
+
raise AttributeError(f"Engine has no component slot named '{slot}'.")
|
|
163
|
+
previous = getattr(self, slot)
|
|
164
|
+
setattr(self, slot, component)
|
|
165
|
+
return previous
|
|
166
|
+
|
|
167
|
+
def _select_processor(self, payload: Any) -> Any:
|
|
168
|
+
for processor in self.processors:
|
|
169
|
+
if processor.supports(payload):
|
|
170
|
+
return processor
|
|
171
|
+
raise TypeError(f"No input processor supports payload type: {type(payload)!r}")
|
|
172
|
+
|
|
173
|
+
def _trace(self, stage: str, message: str, payload: Dict[str, Any]) -> TraceEvent:
|
|
174
|
+
return TraceEvent(stage=stage, message=message, payload=payload)
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any, Dict, List, Sequence
|
|
4
|
+
|
|
5
|
+
from cognitive_engine.config.schema import EngineConfig
|
|
6
|
+
from cognitive_engine.core.types import (
|
|
7
|
+
EngineResponseV2,
|
|
8
|
+
GraphQuery,
|
|
9
|
+
MemoryQueryV2,
|
|
10
|
+
SemanticState,
|
|
11
|
+
SemanticStateV2,
|
|
12
|
+
TraceEvent,
|
|
13
|
+
tensor_to_numpy,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class CognitiveEngineV2:
|
|
18
|
+
def __init__(
|
|
19
|
+
self,
|
|
20
|
+
config: EngineConfig,
|
|
21
|
+
processors: Sequence[Any],
|
|
22
|
+
semantic_encoder: Any,
|
|
23
|
+
importance_evaluator: Any,
|
|
24
|
+
compressor_v2: Any,
|
|
25
|
+
memory_system: Any,
|
|
26
|
+
router: Any,
|
|
27
|
+
stable_core: Any,
|
|
28
|
+
plastic_learner: Any,
|
|
29
|
+
trainer: Any,
|
|
30
|
+
replay_buffer: Any,
|
|
31
|
+
consolidator: Any,
|
|
32
|
+
telemetry: Any,
|
|
33
|
+
specialist_runtime: Any,
|
|
34
|
+
context_manager: Any,
|
|
35
|
+
stability_governor: Any,
|
|
36
|
+
project_indexer: Any,
|
|
37
|
+
) -> None:
|
|
38
|
+
self.config = config
|
|
39
|
+
self.processors = list(processors)
|
|
40
|
+
self.semantic_encoder = semantic_encoder
|
|
41
|
+
self.importance_evaluator = importance_evaluator
|
|
42
|
+
self.compressor_v2 = compressor_v2
|
|
43
|
+
self.memory_system = memory_system
|
|
44
|
+
self.router = router
|
|
45
|
+
self.stable_core = stable_core
|
|
46
|
+
self.plastic_learner = plastic_learner
|
|
47
|
+
self.trainer = trainer
|
|
48
|
+
self.replay_buffer = replay_buffer
|
|
49
|
+
self.consolidator = consolidator
|
|
50
|
+
self.telemetry = telemetry
|
|
51
|
+
self.specialist_runtime = specialist_runtime
|
|
52
|
+
self.context_manager = context_manager
|
|
53
|
+
self.stability_governor = stability_governor
|
|
54
|
+
self.project_indexer = project_indexer
|
|
55
|
+
self.step_counter = 0
|
|
56
|
+
|
|
57
|
+
def process(self, payload: Any, allow_learning: bool = True, project_path: str | None = None, project_id: str | None = None) -> EngineResponseV2:
|
|
58
|
+
traces: List[TraceEvent] = []
|
|
59
|
+
if project_path:
|
|
60
|
+
project_record = self.project_indexer.index_project(project_path, project_id)
|
|
61
|
+
self.memory_system.add_project_record(project_record)
|
|
62
|
+
project_id = project_record.project_id
|
|
63
|
+
traces.append(self._trace("project_index", "Project indexed into graph memory.", project_record.__dict__))
|
|
64
|
+
|
|
65
|
+
processed = self._select_processor(payload).process(payload)
|
|
66
|
+
traces.append(self._trace("input_v2", "Payload processed.", {"modality": processed.modality, "metadata": processed.metadata}))
|
|
67
|
+
|
|
68
|
+
base_state = self.semantic_encoder.encode(processed)
|
|
69
|
+
semantic_state = self._upgrade_state(base_state)
|
|
70
|
+
self.memory_system.update_working_memory(semantic_state)
|
|
71
|
+
traces.append(
|
|
72
|
+
self._trace(
|
|
73
|
+
"semantic_v2",
|
|
74
|
+
"SemanticStateV2 created.",
|
|
75
|
+
{
|
|
76
|
+
"intent": semantic_state.intent,
|
|
77
|
+
"concepts": [concept.label for concept in semantic_state.concepts],
|
|
78
|
+
"code_symbols": semantic_state.code_symbols,
|
|
79
|
+
},
|
|
80
|
+
)
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
runtime_context = {
|
|
84
|
+
"project_id": project_id,
|
|
85
|
+
"has_project_memory": bool(self.memory_system.project_records),
|
|
86
|
+
"long_context": len(str(payload)) > 4000 or bool(project_path),
|
|
87
|
+
}
|
|
88
|
+
routing = self.router.route_v2(semantic_state, runtime_context)
|
|
89
|
+
traces.append(self._trace("routing_v2", routing.rationale, routing.gate_scores))
|
|
90
|
+
|
|
91
|
+
query = MemoryQueryV2(
|
|
92
|
+
embedding=tensor_to_numpy(semantic_state.pooled_embedding.float()),
|
|
93
|
+
top_k=self.config.memory.retrieval_top_k,
|
|
94
|
+
modality=processed.modality,
|
|
95
|
+
intent=semantic_state.intent,
|
|
96
|
+
concepts=[concept.label for concept in semantic_state.concepts] + semantic_state.code_symbols,
|
|
97
|
+
project_id=project_id,
|
|
98
|
+
graph_query=GraphQuery(
|
|
99
|
+
seeds=[concept.label for concept in semantic_state.concepts] + semantic_state.code_symbols,
|
|
100
|
+
project_id=project_id,
|
|
101
|
+
top_k=16,
|
|
102
|
+
),
|
|
103
|
+
)
|
|
104
|
+
memory_bundle = self.memory_system.retrieve_v2(query)
|
|
105
|
+
traces.append(
|
|
106
|
+
self._trace(
|
|
107
|
+
"memory_v2",
|
|
108
|
+
"Hybrid memory bundle retrieved.",
|
|
109
|
+
{
|
|
110
|
+
"semantic": len(memory_bundle.semantic_long_term),
|
|
111
|
+
"episodic": len(memory_bundle.episodic),
|
|
112
|
+
"procedural": len(memory_bundle.procedural),
|
|
113
|
+
"project": len(memory_bundle.project),
|
|
114
|
+
"graph_nodes": len(memory_bundle.graph_subgraph.nodes) if memory_bundle.graph_subgraph else 0,
|
|
115
|
+
},
|
|
116
|
+
)
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
specialist_context = self.specialist_runtime.prepare_context(semantic_state, memory_bundle, routing.selected_specialists)
|
|
120
|
+
context_package = self.context_manager.compose(
|
|
121
|
+
{
|
|
122
|
+
"semantic_state": semantic_state,
|
|
123
|
+
"memory_bundle": memory_bundle,
|
|
124
|
+
"specialist_context": specialist_context,
|
|
125
|
+
"context_budget": routing.context_budget,
|
|
126
|
+
}
|
|
127
|
+
)
|
|
128
|
+
traces.append(
|
|
129
|
+
self._trace(
|
|
130
|
+
"context_v2",
|
|
131
|
+
"Context package composed.",
|
|
132
|
+
{"token_estimate": context_package.token_estimate, "sections": list(context_package.sections)},
|
|
133
|
+
)
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
importance = self.importance_evaluator.evaluate(semantic_state, memory_bundle)
|
|
137
|
+
semantic_state.metadata["importance_action"] = importance.action
|
|
138
|
+
stability = self.stability_governor.validate_update(
|
|
139
|
+
{
|
|
140
|
+
"confidence": importance.confidence_score,
|
|
141
|
+
"contradiction_risk": importance.contradiction_risk,
|
|
142
|
+
"plastic_drift": self.plastic_learner.parameter_drift(),
|
|
143
|
+
}
|
|
144
|
+
)
|
|
145
|
+
traces.append(self._trace("stability_v2", "Stability decision computed.", stability.__dict__))
|
|
146
|
+
|
|
147
|
+
plastic_output = None
|
|
148
|
+
if routing.engage_plasticity and processed.modality == "numeric":
|
|
149
|
+
plastic_output = self.plastic_learner.predict(semantic_state)
|
|
150
|
+
traces.append(self._trace("plastic_v2", plastic_output.explanation, plastic_output.artifacts))
|
|
151
|
+
|
|
152
|
+
inference = self.stable_core.infer(semantic_state, memory_bundle, routing, plastic_output)
|
|
153
|
+
if context_package.sections.get("specialists"):
|
|
154
|
+
inference.explanation = f"{inference.explanation}\n\nSpecialist context: {context_package.sections['specialists']}"
|
|
155
|
+
traces.append(self._trace("stable_core_v2", inference.explanation, {"confidence": inference.confidence}))
|
|
156
|
+
|
|
157
|
+
learning_applied = False
|
|
158
|
+
if (
|
|
159
|
+
allow_learning
|
|
160
|
+
and routing.update_memory
|
|
161
|
+
and routing.learning_action == "learn"
|
|
162
|
+
and importance.action not in {"ignore", "uncertain"}
|
|
163
|
+
and stability.approved
|
|
164
|
+
):
|
|
165
|
+
compression = self.compressor_v2.compress_v2(semantic_state, importance, project_id=project_id or "global")
|
|
166
|
+
self.memory_system.write_v2(compression)
|
|
167
|
+
learning_applied = True
|
|
168
|
+
traces.append(
|
|
169
|
+
self._trace(
|
|
170
|
+
"memory_write_v2",
|
|
171
|
+
"CompressionBundle written to hybrid memory.",
|
|
172
|
+
{
|
|
173
|
+
"knowledge": len(compression.compressed_knowledge),
|
|
174
|
+
"triples": len(compression.triples),
|
|
175
|
+
"graph_nodes": len(compression.graph_patch.nodes),
|
|
176
|
+
"procedures": len(compression.procedures),
|
|
177
|
+
"preferences": len(compression.preferences),
|
|
178
|
+
},
|
|
179
|
+
)
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
if allow_learning and processed.modality == "numeric" and semantic_state.metadata.get("target") is not None:
|
|
183
|
+
training_metrics = self.trainer.observe(semantic_state, priority=importance.learning_priority)
|
|
184
|
+
if training_metrics is not None:
|
|
185
|
+
learning_applied = True
|
|
186
|
+
traces.append(self._trace("online_training_v2", "Plastic module updated.", training_metrics))
|
|
187
|
+
|
|
188
|
+
self.step_counter += 1
|
|
189
|
+
if self.step_counter % self.config.memory.consolidation_interval == 0 or routing.consolidation_action != "none":
|
|
190
|
+
report = self.consolidator.run()
|
|
191
|
+
traces.append(self._trace("consolidation_v2", report.notes, report.__dict__))
|
|
192
|
+
|
|
193
|
+
for event in traces:
|
|
194
|
+
self.telemetry.emit(event)
|
|
195
|
+
self.telemetry.flush()
|
|
196
|
+
|
|
197
|
+
return EngineResponseV2(
|
|
198
|
+
text=str(inference.explanation),
|
|
199
|
+
semantic_state=semantic_state,
|
|
200
|
+
importance=importance,
|
|
201
|
+
routing=routing,
|
|
202
|
+
memory_bundle=memory_bundle,
|
|
203
|
+
inference=inference,
|
|
204
|
+
traces=traces,
|
|
205
|
+
learning_applied=learning_applied,
|
|
206
|
+
routing_v2=routing,
|
|
207
|
+
memory_bundle_v2=memory_bundle,
|
|
208
|
+
context_package=context_package,
|
|
209
|
+
stability_decision=stability,
|
|
210
|
+
)
|
|
211
|
+
|
|
212
|
+
def snapshot(self) -> Dict[str, Any]:
|
|
213
|
+
return {
|
|
214
|
+
"version": "v2",
|
|
215
|
+
"steps": self.step_counter,
|
|
216
|
+
"memory": self.memory_system.snapshot(),
|
|
217
|
+
"router": self.router.describe(),
|
|
218
|
+
"specialists": self.specialist_runtime.describe(),
|
|
219
|
+
"context_manager": self.context_manager.describe(),
|
|
220
|
+
"stability": self.stability_governor.describe(),
|
|
221
|
+
"replay_size": len(self.replay_buffer),
|
|
222
|
+
"plastic_drift": self.plastic_learner.parameter_drift(),
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
def describe_architecture(self) -> Dict[str, Any]:
|
|
226
|
+
return {
|
|
227
|
+
"version": "v2",
|
|
228
|
+
"processors": [processor.describe() for processor in self.processors],
|
|
229
|
+
"semantic_encoder": self.semantic_encoder.describe(),
|
|
230
|
+
"router": self.router.describe(),
|
|
231
|
+
"memory_system": self.memory_system.describe(),
|
|
232
|
+
"compressor": self.compressor_v2.describe(),
|
|
233
|
+
"specialists": self.specialist_runtime.describe(),
|
|
234
|
+
"context_manager": self.context_manager.describe(),
|
|
235
|
+
"stability_governor": self.stability_governor.describe(),
|
|
236
|
+
"stable_core": self.stable_core.describe(),
|
|
237
|
+
"plastic_learner": self.plastic_learner.describe(),
|
|
238
|
+
"consolidator": self.consolidator.describe(),
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
def _upgrade_state(self, state: SemanticState) -> SemanticStateV2:
|
|
242
|
+
text = str(state.raw_input)
|
|
243
|
+
code_symbols = self._extract_code_symbols(text)
|
|
244
|
+
return SemanticStateV2(
|
|
245
|
+
raw_input=state.raw_input,
|
|
246
|
+
modality=state.modality,
|
|
247
|
+
sequence_embedding=state.sequence_embedding,
|
|
248
|
+
pooled_embedding=state.pooled_embedding,
|
|
249
|
+
intent=state.intent,
|
|
250
|
+
entities=state.entities,
|
|
251
|
+
concepts=state.concepts,
|
|
252
|
+
concept_graph_edges=state.concept_graph_edges,
|
|
253
|
+
compressed_context=state.compressed_context,
|
|
254
|
+
metadata=dict(state.metadata),
|
|
255
|
+
code_symbols=code_symbols,
|
|
256
|
+
graph_candidates=[],
|
|
257
|
+
uncertainty=max(0.0, 1.0 - min(1.0, len(state.concepts) / 6.0)),
|
|
258
|
+
modality_features={"contains_code": bool(code_symbols), "raw_length": len(text)},
|
|
259
|
+
)
|
|
260
|
+
|
|
261
|
+
def _extract_code_symbols(self, text: str) -> List[str]:
|
|
262
|
+
symbols = []
|
|
263
|
+
for raw in text.replace("(", " ").replace(")", " ").replace(":", " ").replace(".", " ").split():
|
|
264
|
+
token = raw.strip("`'\"")
|
|
265
|
+
if not token:
|
|
266
|
+
continue
|
|
267
|
+
if token.endswith(".py") or token in {"class", "def", "pytest", "import"}:
|
|
268
|
+
symbols.append(token)
|
|
269
|
+
elif "_" in token or (token[:1].isupper() and len(token) > 2):
|
|
270
|
+
symbols.append(token)
|
|
271
|
+
return list(dict.fromkeys(symbols))[:12]
|
|
272
|
+
|
|
273
|
+
def _select_processor(self, payload: Any) -> Any:
|
|
274
|
+
for processor in self.processors:
|
|
275
|
+
if processor.supports(payload):
|
|
276
|
+
return processor
|
|
277
|
+
raise TypeError(f"No input processor supports payload type: {type(payload)!r}")
|
|
278
|
+
|
|
279
|
+
def _trace(self, stage: str, message: str, payload: Dict[str, Any]) -> TraceEvent:
|
|
280
|
+
return TraceEvent(stage=stage, message=message, payload=payload)
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from collections import defaultdict
|
|
4
|
+
from typing import Any, Callable, Dict
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
Factory = Callable[..., Any]
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class ComponentRegistry:
|
|
11
|
+
def __init__(self) -> None:
|
|
12
|
+
self._factories: Dict[str, Dict[str, Factory]] = defaultdict(dict)
|
|
13
|
+
|
|
14
|
+
def register(self, category: str, name: str, factory: Factory) -> None:
|
|
15
|
+
self._factories[category][name] = factory
|
|
16
|
+
|
|
17
|
+
def create(self, category: str, name: str, **kwargs: Any) -> Any:
|
|
18
|
+
try:
|
|
19
|
+
factory = self._factories[category][name]
|
|
20
|
+
except KeyError as exc:
|
|
21
|
+
raise KeyError(f"Component '{name}' is not registered in category '{category}'.") from exc
|
|
22
|
+
return factory(**kwargs)
|
|
23
|
+
|
|
24
|
+
def available(self, category: str) -> Dict[str, Factory]:
|
|
25
|
+
return dict(self._factories.get(category, {}))
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
GLOBAL_REGISTRY = ComponentRegistry()
|
|
29
|
+
|