dandelion-memory 1.0.0rc15__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.
Files changed (232) hide show
  1. benchmarks/__init__.py +3 -0
  2. benchmarks/adapters/__init__.py +29 -0
  3. benchmarks/adapters/ablations.py +406 -0
  4. benchmarks/adapters/base.py +72 -0
  5. benchmarks/adapters/controlled/__init__.py +13 -0
  6. benchmarks/adapters/controlled/core_archival.py +115 -0
  7. benchmarks/adapters/controlled/extract_resolve.py +130 -0
  8. benchmarks/adapters/controlled/hybrid_rag.py +120 -0
  9. benchmarks/adapters/controlled/no_late_binding.py +113 -0
  10. benchmarks/adapters/dandelion.py +94 -0
  11. benchmarks/adapters/native/__init__.py +31 -0
  12. benchmarks/adapters/native/base.py +45 -0
  13. benchmarks/adapters/native/dandelion_native.py +164 -0
  14. benchmarks/adapters/native/dandelion_native_b0.py +165 -0
  15. benchmarks/adapters/native/dandelion_native_b1.py +170 -0
  16. benchmarks/adapters/native/dandelion_native_b2.py +169 -0
  17. benchmarks/adapters/native/dandelion_native_b3.py +171 -0
  18. benchmarks/adapters/native/dandelion_native_c4.py +261 -0
  19. benchmarks/adapters/native/dandelion_native_c4_1.py +281 -0
  20. benchmarks/adapters/native/dandelion_native_c4_2.py +435 -0
  21. benchmarks/adapters/native/event_evidence_b2.py +616 -0
  22. benchmarks/adapters/native/evidence_b2.py +609 -0
  23. benchmarks/adapters/native/letta_native.py +253 -0
  24. benchmarks/adapters/native/mem0_native.py +219 -0
  25. benchmarks/adapters/native/store_vector_b0.py +792 -0
  26. benchmarks/adapters/native/store_vector_b2.py +808 -0
  27. benchmarks/agent_adapter_stdio_acceptance.py +86 -0
  28. benchmarks/bootstrap_cross_stack.py +218 -0
  29. benchmarks/canonical_profile_instrumentation.py +167 -0
  30. benchmarks/canonical_public_baseline.py +288 -0
  31. benchmarks/cli.py +525 -0
  32. benchmarks/collector_public30_offline.py +1653 -0
  33. benchmarks/datasets/__init__.py +45 -0
  34. benchmarks/datasets/conversational_generator.py +238 -0
  35. benchmarks/datasets/generator.py +283 -0
  36. benchmarks/datasets/holdout_topology_generator.py +290 -0
  37. benchmarks/datasets/holdout_topology_generator_v2.py +294 -0
  38. benchmarks/datasets/multi_world.py +1643 -0
  39. benchmarks/datasets/scale_generator.py +289 -0
  40. benchmarks/datasets/schema.py +57 -0
  41. benchmarks/deep_collision_stress.py +199 -0
  42. benchmarks/evaluators/__init__.py +12 -0
  43. benchmarks/evaluators/manifest.py +142 -0
  44. benchmarks/evaluators/metrics.py +277 -0
  45. benchmarks/evaluators/runner.py +258 -0
  46. benchmarks/f1_step0/build_dev_measurement_manifest_v1.py +127 -0
  47. benchmarks/f1_step0/build_dev_measurement_manifest_v2.py +127 -0
  48. benchmarks/f1_step0/build_dev_measurement_manifest_v3.py +146 -0
  49. benchmarks/f1_step0/build_ledger.py +143 -0
  50. benchmarks/f1_step0/build_ledger_v2.py +296 -0
  51. benchmarks/f1_step0/compare_dev52_readiness_v1.py +106 -0
  52. benchmarks/f1_step0/locator_public_smoke.py +172 -0
  53. benchmarks/f1_step0/prepare_dev52_owner_v1.py +133 -0
  54. benchmarks/f1_step0/prepare_dev52_owner_v2.py +133 -0
  55. benchmarks/f1_step0/public_identity_probe.py +191 -0
  56. benchmarks/f1_step0/public_response_mapper.py +101 -0
  57. benchmarks/f1_step0/real_source_public_preflight.py +244 -0
  58. benchmarks/f1_step0/run_exact_fusion_dev_experiment_v1.py +83 -0
  59. benchmarks/f1_step0/scorer.py +110 -0
  60. benchmarks/f1_step0/source_aware_dev_measurement_v1.py +627 -0
  61. benchmarks/f1_step0/source_aware_dev_measurement_v2.py +659 -0
  62. benchmarks/f1_step0/source_aware_dev_measurement_v3.py +776 -0
  63. benchmarks/f1_step0/test_public_response_mapper.py +110 -0
  64. benchmarks/f1_step0/test_real_source_public_preflight.py +25 -0
  65. benchmarks/f1_step0/test_scorer.py +158 -0
  66. benchmarks/f1_step0/test_source_aware_dev_measurement.py +189 -0
  67. benchmarks/f1_step0/test_source_aware_dev_measurement_v2.py +263 -0
  68. benchmarks/f1_step0/test_source_aware_dev_measurement_v3.py +334 -0
  69. benchmarks/f1_step0/typed_readback_public_smoke.py +181 -0
  70. benchmarks/forensic_c4_1_dev.py +258 -0
  71. benchmarks/forensic_c4_dev.py +255 -0
  72. benchmarks/forensic_deep25_c4_1.py +478 -0
  73. benchmarks/legacy_migration_acceptance.py +29 -0
  74. benchmarks/longmemeval_actor.py +497 -0
  75. benchmarks/longmemeval_agent_judge.py +593 -0
  76. benchmarks/longmemeval_budget.py +139 -0
  77. benchmarks/longmemeval_full.py +410 -0
  78. benchmarks/longmemeval_judge.py +327 -0
  79. benchmarks/longmemeval_pilot.py +316 -0
  80. benchmarks/longmemeval_s.py +318 -0
  81. benchmarks/memory_bench.py +248 -0
  82. benchmarks/memory_formation/__init__.py +1 -0
  83. benchmarks/memory_formation/audit.py +801 -0
  84. benchmarks/memory_formation/real_audit.py +688 -0
  85. benchmarks/memory_session_acceptance.py +279 -0
  86. benchmarks/native_codex_host.py +513 -0
  87. benchmarks/owner_dense_acceptance.py +126 -0
  88. benchmarks/pilot_seeker_vault.py +266 -0
  89. benchmarks/prepare_public30_ripgrep_range_owner.py +115 -0
  90. benchmarks/profile_canonical.py +373 -0
  91. benchmarks/profile_canonical_lanes.py +405 -0
  92. benchmarks/profile_search_chunks.py +351 -0
  93. benchmarks/public_ranges_faq_proof.py +121 -0
  94. benchmarks/public_repos/build_history_scope_v3.py +330 -0
  95. benchmarks/public_repos/build_oracle.py +108 -0
  96. benchmarks/public_repos/build_oracle_v2.py +169 -0
  97. benchmarks/public_repos/build_oracle_v3.py +176 -0
  98. benchmarks/public_repos/map_public_v2.py +296 -0
  99. benchmarks/public_repos/project_prompts_v2.py +31 -0
  100. benchmarks/public_repos/project_prompts_v3.py +29 -0
  101. benchmarks/public_repos/ready_admin_v2.py +214 -0
  102. benchmarks/public_repos/score_public_v2.py +258 -0
  103. benchmarks/public_repos/test_map_public_v2.py +171 -0
  104. benchmarks/public_repos/test_public_repo_v2.py +44 -0
  105. benchmarks/public_repos/test_public_repo_v3.py +46 -0
  106. benchmarks/public_repos/test_ready_admin_v2.py +117 -0
  107. benchmarks/public_repos/test_score_public_v2.py +163 -0
  108. benchmarks/public_repos/test_wire_v2.py +292 -0
  109. benchmarks/public_repos/wire_v2.py +603 -0
  110. benchmarks/real_blocker_fix_acceptance.py +155 -0
  111. benchmarks/real_scan_rescan_acceptance.py +41 -0
  112. benchmarks/recall_v2_20k_smoke.py +112 -0
  113. benchmarks/recall_v2_e1_retrieve.py +70 -0
  114. benchmarks/recall_v2_e1_score.py +271 -0
  115. benchmarks/recall_v2_e2_prepare.py +69 -0
  116. benchmarks/recall_v2_e2_public_score.py +91 -0
  117. benchmarks/recall_v2_e2_score.py +108 -0
  118. benchmarks/recall_v2_navigation_mcp_replay.py +308 -0
  119. benchmarks/recall_v2_reader_capture.py +118 -0
  120. benchmarks/recall_v2_reader_replay.py +136 -0
  121. benchmarks/run_blind_h10_holdout.py +284 -0
  122. benchmarks/run_blind_h9_holdout.py +275 -0
  123. benchmarks/run_c4_2_nightly_scale.py +446 -0
  124. benchmarks/run_context_scale_simulation.py +189 -0
  125. benchmarks/run_conversational_bench.py +170 -0
  126. benchmarks/run_h11_evaluation.py +316 -0
  127. benchmarks/run_h11_v2_evaluation.py +431 -0
  128. benchmarks/run_native_fixed12.py +469 -0
  129. benchmarks/run_native_preflight.py +280 -0
  130. benchmarks/run_scale_stress.py +225 -0
  131. benchmarks/solanazation_brownfield_01/build_oracle.py +970 -0
  132. benchmarks/solanazation_brownfield_01/check_level_a.py +78 -0
  133. benchmarks/solanazation_brownfield_01/debug_scope.py +13 -0
  134. benchmarks/solanazation_brownfield_01/evaluate_bootstrap_02.py +570 -0
  135. benchmarks/solanazation_brownfield_01/evaluate_first_pass.py +306 -0
  136. benchmarks/solanazation_brownfield_01/generate_dryrun_report.py +222 -0
  137. benchmarks/solanazation_brownfield_01/generate_manifests.py +146 -0
  138. benchmarks/solanazation_brownfield_01/inspect_misses.py +29 -0
  139. benchmarks/solanazation_brownfield_01/run_miss_forensic.py +206 -0
  140. benchmarks/solanazation_brownfield_01/test_facts.py +15 -0
  141. benchmarks/statistics/__init__.py +15 -0
  142. benchmarks/statistics/significance.py +326 -0
  143. benchmarks/test_native_mcp_preflight.py +229 -0
  144. benchmarks/test_prepare_public30_range_owner.py +35 -0
  145. benchmarks/test_public30_source_reference.py +698 -0
  146. benchmarks/test_recall_v2_e1_reader_input.py +89 -0
  147. benchmarks/test_recall_v2_navigation_mcp_replay.py +155 -0
  148. benchmarks/test_recall_v2_reader_replay.py +41 -0
  149. dandelion/__init__.py +3 -0
  150. dandelion/__main__.py +11 -0
  151. dandelion/agent_adapter.py +2369 -0
  152. dandelion/bootstrap/__init__.py +872 -0
  153. dandelion/bootstrap/alias_policy.py +50 -0
  154. dandelion/bootstrap/authority.py +225 -0
  155. dandelion/bootstrap/canonical_history.py +412 -0
  156. dandelion/bootstrap/classifier.py +403 -0
  157. dandelion/bootstrap/component_graph.py +280 -0
  158. dandelion/bootstrap/discovery.py +279 -0
  159. dandelion/bootstrap/domain_induction.py +180 -0
  160. dandelion/bootstrap/domain_topology.py +673 -0
  161. dandelion/bootstrap/evidence_envelope.py +132 -0
  162. dandelion/bootstrap/extractors/base.py +171 -0
  163. dandelion/bootstrap/extractors/exact.py +1234 -0
  164. dandelion/bootstrap/extractors/generic.py +100 -0
  165. dandelion/bootstrap/extractors/git_temporal.py +111 -0
  166. dandelion/bootstrap/extractors/registry.py +345 -0
  167. dandelion/bootstrap/extractors/relations.py +89 -0
  168. dandelion/bootstrap/extractors/semantic.py +424 -0
  169. dandelion/bootstrap/extractors/status.py +218 -0
  170. dandelion/bootstrap/git_sweeper.py +135 -0
  171. dandelion/bootstrap/manifest.py +246 -0
  172. dandelion/bootstrap/quarantine.py +264 -0
  173. dandelion/bootstrap/report.py +259 -0
  174. dandelion/bootstrap/root_resolver.py +221 -0
  175. dandelion/bootstrap/tech_profiler.py +125 -0
  176. dandelion/bootstrap/topology.py +75 -0
  177. dandelion/bootstrap/topology_index.py +110 -0
  178. dandelion/bootstrap/transaction.py +315 -0
  179. dandelion/canonical_bootstrap.py +257 -0
  180. dandelion/canonical_memory.py +4130 -0
  181. dandelion/captured_input.py +295 -0
  182. dandelion/cli.py +767 -0
  183. dandelion/config.py +40 -0
  184. dandelion/derived_index.py +745 -0
  185. dandelion/domains.py +706 -0
  186. dandelion/embedder.py +229 -0
  187. dandelion/event_evidence.py +571 -0
  188. dandelion/evidence.py +812 -0
  189. dandelion/evidence_response.py +453 -0
  190. dandelion/evidence_workspace.py +2360 -0
  191. dandelion/exact_context.py +129 -0
  192. dandelion/hydrator.py +124 -0
  193. dandelion/inspector.py +442 -0
  194. dandelion/inspector_assets/dandelion-mark.svg +33 -0
  195. dandelion/inspector_assets/dandelion_graph.js +361 -0
  196. dandelion/inspector_assets/inspector.css +223 -0
  197. dandelion/inspector_assets/inspector.html +135 -0
  198. dandelion/inspector_assets/inspector.js +1059 -0
  199. dandelion/legacy_import.py +431 -0
  200. dandelion/live_source_grants.py +133 -0
  201. dandelion/local_owner.py +114 -0
  202. dandelion/memory_client.py +252 -0
  203. dandelion/memory_contract.py +107 -0
  204. dandelion/memory_owner.py +560 -0
  205. dandelion/reader_providers.py +118 -0
  206. dandelion/recall_v2.py +767 -0
  207. dandelion/reconciler.py +219 -0
  208. dandelion/request_budget.py +125 -0
  209. dandelion/retained_basis.py +223 -0
  210. dandelion/retrieval.py +485 -0
  211. dandelion/router.py +405 -0
  212. dandelion/search_terms.py +53 -0
  213. dandelion/security.py +124 -0
  214. dandelion/server.py +810 -0
  215. dandelion/source_content_fts.py +219 -0
  216. dandelion/source_delivery.py +89 -0
  217. dandelion/source_history.py +241 -0
  218. dandelion/source_identity.py +88 -0
  219. dandelion/source_navigator.py +5 -0
  220. dandelion/source_reader.py +444 -0
  221. dandelion/source_references.py +379 -0
  222. dandelion/source_sections.py +265 -0
  223. dandelion/store_kv.py +684 -0
  224. dandelion/store_vector.py +1289 -0
  225. dandelion/studio.py +325 -0
  226. dandelion/wal.py +227 -0
  227. dandelion_memory-1.0.0rc15.dist-info/METADATA +48 -0
  228. dandelion_memory-1.0.0rc15.dist-info/RECORD +232 -0
  229. dandelion_memory-1.0.0rc15.dist-info/WHEEL +5 -0
  230. dandelion_memory-1.0.0rc15.dist-info/entry_points.txt +5 -0
  231. dandelion_memory-1.0.0rc15.dist-info/licenses/LICENSE +21 -0
  232. dandelion_memory-1.0.0rc15.dist-info/top_level.txt +2 -0
@@ -0,0 +1,609 @@
1
+ """Epistemic Evidence Plane for Dandelion memory architecture.
2
+
3
+ The Epistemic Plane separates semantic similarity from grounding support:
4
+ - Semantic Plane: dense vectors, asymmetric passage/query embeddings ("What happened and why?")
5
+ - Symbolic Plane: KVStore, exact state, revisions, code identifiers ("What exactly is true?")
6
+ - Evidence Plane: support level, component scope, memory kind, entity/topic grounding ("What does this memory actually support?")
7
+
8
+ Core Thesis:
9
+ "Similarity finds candidates. Evidence decides whether they are knowledge. Dandelion separates relevance from support."
10
+
11
+ Operational constraint:
12
+ - 0 LLM calls on core memory path
13
+ - Deterministic, local, sub-millisecond execution
14
+ """
15
+
16
+ from __future__ import annotations
17
+
18
+ import enum
19
+ import re
20
+ from dataclasses import dataclass, field, asdict
21
+ from typing import Optional, Any
22
+
23
+ from dandelion.domains import extract_component_root, extract_domain_kind
24
+
25
+ # Technical Code Identifier Pattern
26
+ TECHNICAL_CODE_ID: re.Pattern = re.compile(
27
+ r"\b0x[a-fA-F0-9]{4,}\b|" # Hex addresses / hashes
28
+ r"\b[a-zA-Z0-9_]+/[a-zA-Z0-9_./\-]+\.[a-zA-Z0-9]+\b|" # File paths with extension (src/worker.py)
29
+ r"/(?:[a-zA-Z0-9_.\-]+/)+[a-zA-Z0-9_.\-]*\b|" # Absolute endpoints (/api/v1/...)
30
+ r"\b[a-zA-Z0-9_-]+\.[a-zA-Z0-9_-]+\b|" # Dotted symbols (api.port, config.yaml)
31
+ r"\b[A-Z0-9_]{2,}-[A-Z0-9_]{2,}\b|" # Hyphenated tags (POC-157, ADR-01)
32
+ r"\b[A-Z0-9]+_[A-Z0-9_]+\b|" # ALL_CAPS with underscore (ERR_TIMEOUT, HTTP_500)
33
+ r"\b[a-z]+[A-Z][a-zA-Z0-9]*\b|" # camelCase (getUserProfile)
34
+ r"\b[a-z0-9]+_[a-z0-9_]+\b|" # snake_case (droop_control)
35
+ r"\bADR\b|\bRFC\b" # Architectural decision / request for comment tags
36
+ )
37
+
38
+ QUERY_STOP_WORDS: set[str] = {
39
+ "why", "how", "what", "when", "where", "which", "who", "whom",
40
+ "this", "that", "with", "from", "into", "does", "have", "will",
41
+ "the", "for", "our", "and", "was", "were", "been", "about", "after",
42
+ "show", "find", "list", "check", "extract", "without", "during",
43
+ "not", "all", "can", "get", "set", "use", "has", "had", "are",
44
+ "you", "out", "per", "via", "but", "also", "during", "service",
45
+ "services", "system", "systems", "implementation", "details", "explain",
46
+ "describe", "overview", "note", "notes", "policy", "policies", "review",
47
+ "каков", "какой", "какая", "какие", "для", "после", "при", "как", "что",
48
+ "где", "когда", "почему", "это", "или", "все", "был", "была", "было",
49
+ }
50
+
51
+ COMMON_ACRONYM_EXCLUSIONS: set[str] = {
52
+ "CPU", "RAM", "RCA", "URL", "API", "SQL", "DNS", "SSH", "TCP", "UDP",
53
+ "AWS", "GCP", "SDK", "CLI", "IDE", "HTTP", "REST", "JSON", "YAML", "SIMD", "POST", "GET"
54
+ }
55
+
56
+ KNOWN_SOFTWARE_COMPONENTS: set[str] = {
57
+ "ecommerce", "identity", "streaming", "legaltech", "devtools", "fintech",
58
+ "telemetry", "search", "logistics", "healthcare", "energy", "gaming",
59
+ "iot", "biotech", "robotics", "edtech", "agritech", "aerospace",
60
+ "automotive", "quantum", "insurtech", "marinetech", "cleantech",
61
+ "miningtech", "govtech", "devops", "web3", "secops", "mlops", "mobile",
62
+ "dataeng", "messaging", "crm", "avionics", "genomics", "social",
63
+ "compiler", "hrtech", "traveltech", "audio", "autonomous", "soc",
64
+ "publishing", "maritime", "semiconductor", "pharmatech", "supplychain",
65
+ "space", "neurotech", "realestate",
66
+ }
67
+
68
+ # Cross-lingual equivalence pairs for bilingual support matching
69
+ CROSS_LINGUAL_PAIRS: list[tuple[str, str]] = [
70
+ ("streaming", "streaming"),
71
+ ("streaming", "асинхрон"),
72
+ ("queue", "очеред"),
73
+ ("grpc", "grpc"),
74
+ ("approved", "утвердил"),
75
+ ("decision", "решение"),
76
+ ("decision", "переход"),
77
+ ("asynchronous", "асинхрон"),
78
+ ("communication", "streaming"),
79
+ ("communication", "grpc"),
80
+ ("processing", "обработк"),
81
+ ("incident", "сбой"),
82
+ ("incident", "авария"),
83
+ ("incident", "падение"),
84
+ ("incident", "отказ"),
85
+ ]
86
+
87
+
88
+ COMMON_CAMEL_CASE_EXCLUSIONS: set[str] = {
89
+ "ediscovery", "ecommerce", "ebusiness", "egov"
90
+ }
91
+
92
+ # Domain title keyword associations for canonical component mapping
93
+ DOMAIN_TITLE_KEYWORDS: dict[str, set[str]] = {
94
+ "energy": {"smart", "grid", "scada"},
95
+ "legaltech": {"legal", "compliance", "ediscovery"},
96
+ "fintech": {"fintech", "payments", "ledger", "iso8583"},
97
+ "ecommerce": {"ecommerce", "retail", "catalog", "cart"},
98
+ "healthcare": {"healthcare", "hipaa", "ehr", "hl7", "fhir"},
99
+ "web3": {"web3", "defi", "staking", "evm"},
100
+ "secops": {"secops", "cybersecurity", "threat", "bastion"},
101
+ "mlops": {"mlops", "inference", "triton", "vllm"},
102
+ "mobile": {"mobile", "apns", "deep", "sqlite"},
103
+ "iot": {"iot", "embedded", "firmware", "mqtt"},
104
+ "dataeng": {"dataeng", "lakehouse", "clickhouse", "spark"},
105
+ "gaming": {"gaming", "matchmaking", "lobby"},
106
+ "search": {"search", "opensearch", "bm25"},
107
+ "messaging": {"messaging", "webrtc", "synapse"},
108
+ "identity": {"identity", "iam", "keycloak", "saml", "jwks"},
109
+ "telemetry": {"telemetry", "opentelemetry", "observability", "prometheus"},
110
+ "streaming": {"streaming", "transcoding", "hls", "ffmpeg"},
111
+ "crm": {"crm", "salesforce", "integrations"},
112
+ "logistics": {"logistics", "fleet", "telematics"},
113
+ "devtools": {"devtools", "runners", "docker"},
114
+ "avionics": {"avionics", "drone", "autopilot", "mavlink"},
115
+ "genomics": {"genomics", "bioinformatics", "nextflow", "blast"},
116
+ "social": {"social", "graph", "newsfeed", "neo4j"},
117
+ "quantum": {"quantum", "qiskit", "qubit"},
118
+ "devops": {"devops", "tooling", "infrastructure"},
119
+ }
120
+
121
+
122
+ class SupportLevel(str, enum.Enum):
123
+ """Epistemic support level evaluating candidate memory against query evidence."""
124
+ STRONG = "STRONG" # Grounded structural / exact proof (fact ref, code ID match, component+topic match)
125
+ MEDIUM = "MEDIUM" # Grounded semantic match (component match + topic/action overlap, or high semantic similarity)
126
+ WEAK = "WEAK" # Bare semantic similarity without verified component/topic grounding
127
+ UNSUPPORTED = "UNSUPPORTED" # Contradicted component, ungrounded topic query, or missing mandatory identifier
128
+
129
+
130
+ @dataclass
131
+ class MemoryEvidence:
132
+ """Evidence envelope stored in memory payload and resolved during recall."""
133
+ domain_root: str = ""
134
+ component_ids: list[str] = field(default_factory=list)
135
+ memory_kind: str = "general"
136
+ entities: list[str] = field(default_factory=list)
137
+ topics: list[str] = field(default_factory=list)
138
+ actions: list[str] = field(default_factory=list)
139
+ fact_refs: list[str] = field(default_factory=list)
140
+ template: str = ""
141
+ event_id: str = ""
142
+ event_sig: Optional[Any] = None
143
+
144
+ def to_dict(self) -> dict[str, Any]:
145
+ d = asdict(self)
146
+ d.pop("event_sig", None)
147
+ return d
148
+
149
+ @classmethod
150
+ def from_dict(cls, data: dict[str, Any]) -> MemoryEvidence:
151
+ return cls(
152
+ domain_root=data.get("domain_root", ""),
153
+ component_ids=data.get("component_ids", []),
154
+ memory_kind=data.get("memory_kind", "general"),
155
+ entities=data.get("entities", []),
156
+ topics=data.get("topics", []),
157
+ actions=data.get("actions", []),
158
+ fact_refs=data.get("fact_refs", []),
159
+ template=data.get("template", ""),
160
+ event_id=data.get("event_id", ""),
161
+ )
162
+
163
+
164
+ @dataclass
165
+ class QueryEvidence:
166
+ """Structured evidence requirements parsed from a recall query."""
167
+ query: str
168
+ explicit_domains: list[str] = field(default_factory=list)
169
+ explicit_identifiers: list[str] = field(default_factory=list)
170
+ explicit_revs: list[tuple[str, int]] = field(default_factory=list)
171
+ requested_kind: Optional[str] = None
172
+ scoped_prefix: Optional[str] = None
173
+ scope_tokens: list[str] = field(default_factory=list)
174
+ substantive_topics: list[str] = field(default_factory=list)
175
+ is_cyrillic: bool = False
176
+ event_sig: Optional[Any] = None
177
+
178
+
179
+ def extract_query_evidence(
180
+ query: str,
181
+ domain: Optional[str] = None,
182
+ explicit_revs: Optional[list[tuple[str, int]]] = None,
183
+ known_domains: Optional[set[str]] = None,
184
+ ) -> QueryEvidence:
185
+ """Extract structured evidence requirements from a query."""
186
+ q_lower = query.lower()
187
+ raw_tokens = set(re.findall(r"[a-zA-Z0-9\u0400-\u04FF]+", q_lower))
188
+ is_cyrillic = bool(re.search(r"[\u0400-\u04FF]", query))
189
+
190
+ # Incorporate dynamically known domain roots
191
+ components_pool = set(KNOWN_SOFTWARE_COMPONENTS)
192
+ known_roots: set[str] = set()
193
+ if known_domains:
194
+ for d in known_domains:
195
+ root = extract_component_root(d)
196
+ if root:
197
+ known_roots.add(root)
198
+ components_pool.add(root)
199
+
200
+ # Detect scoped component
201
+ scoped_prefix: Optional[str] = None
202
+ if domain:
203
+ scoped_prefix = extract_component_root(domain)
204
+
205
+ # 1. Target scope in query tail (supports English and Russian prepositions: in, for, about, для, в)
206
+ tail_tokens: set[str] = set()
207
+ norm_tail: str = ""
208
+ tail_match = re.search(
209
+ r"\b(?:in|for|about|для|в)\s+((?:(?!\b(?:in|for|about|для|в)\b)[A-Za-z0-9_& /,\-()\u0400-\u04FF])+?)(?:\.|\?|\s*)$",
210
+ query,
211
+ re.IGNORECASE,
212
+ )
213
+ if tail_match:
214
+ tail_str = tail_match.group(1).strip()
215
+ tail_tokens = set(re.findall(r"[a-zA-Z0-9\u0400-\u04FF]+", tail_str.lower()))
216
+ norm_tail = re.sub(r"[\s\-]+", "", tail_str.lower())
217
+
218
+ if not scoped_prefix and tail_tokens:
219
+ # Step 1: Multi-keyword title matches (overlap >= 2, e.g. "Smart Grid & SCADA Telemetry" -> energy)
220
+ best_dom = None
221
+ max_kw_overlap = 1
222
+ for dom, kw_set in DOMAIN_TITLE_KEYWORDS.items():
223
+ if known_roots and dom not in known_roots:
224
+ continue
225
+ overlap = len(kw_set & tail_tokens)
226
+ if overlap > max_kw_overlap and overlap >= 2:
227
+ max_kw_overlap = overlap
228
+ best_dom = dom
229
+ if best_dom:
230
+ scoped_prefix = best_dom
231
+
232
+ # Step 2: Exact known root match in tail tokens or normalized tail
233
+ if not scoped_prefix and known_roots:
234
+ for kr in sorted(known_roots, key=len, reverse=True):
235
+ if kr in tail_tokens or kr == norm_tail:
236
+ scoped_prefix = kr
237
+ break
238
+
239
+ # Step 3: Partial / substring root match with substantive tail tokens
240
+ if not scoped_prefix and known_roots:
241
+ for kr in sorted(known_roots, key=len, reverse=True):
242
+ if any(len(tok) >= 4 and (tok in kr or kr.startswith(tok)) for tok in tail_tokens):
243
+ scoped_prefix = kr
244
+ break
245
+
246
+ # Step 4: Single-keyword domain title match or generic components pool
247
+ if not scoped_prefix:
248
+ best_dom = None
249
+ max_kw_overlap = 0
250
+ for dom, kw_set in DOMAIN_TITLE_KEYWORDS.items():
251
+ if known_roots and dom not in known_roots:
252
+ continue
253
+ overlap = len(kw_set & tail_tokens)
254
+ if overlap > max_kw_overlap and overlap >= 1:
255
+ max_kw_overlap = overlap
256
+ best_dom = dom
257
+ if best_dom:
258
+ scoped_prefix = best_dom
259
+ else:
260
+ for comp in sorted(components_pool, key=len, reverse=True):
261
+ if known_roots and comp not in known_roots:
262
+ continue
263
+ if comp in tail_tokens or comp in norm_tail:
264
+ scoped_prefix = comp
265
+ break
266
+
267
+ # 2. Uppercase prefix match in query (e.g. ECOMMERCE-401, FINTECH, DEVTOOLS)
268
+ if not scoped_prefix:
269
+ upper_matches = re.findall(r"\b[A-Z]{3,}(?:-[A-Z0-9]+)?\b", query)
270
+ for m in upper_matches:
271
+ tag = m.split("-")[0]
272
+ if tag not in COMMON_ACRONYM_EXCLUSIONS:
273
+ t_low = tag.lower()
274
+ if (not known_roots or t_low in known_roots) and (t_low in components_pool or t_low in known_roots):
275
+ scoped_prefix = t_low
276
+ break
277
+
278
+ # 3. Known component token in raw query (excluding ambiguous technical words like streaming)
279
+ if not scoped_prefix:
280
+ ambiguous = {"streaming", "search", "mobile", "gaming", "social", "energy"}
281
+ search_pool = known_roots if known_roots else components_pool
282
+ for comp in sorted(search_pool, key=len, reverse=True):
283
+ if comp in raw_tokens and comp not in ambiguous:
284
+ scoped_prefix = comp
285
+ break
286
+
287
+ # Scope tokens associated with component
288
+ scope_tokens: set[str] = set()
289
+ if scoped_prefix:
290
+ scope_tokens.add(scoped_prefix)
291
+ if scoped_prefix in DOMAIN_TITLE_KEYWORDS:
292
+ scope_tokens.update(DOMAIN_TITLE_KEYWORDS[scoped_prefix])
293
+ if tail_tokens:
294
+ scope_tokens.update(tail_tokens)
295
+
296
+ # Code identifiers (excluding title words, acronyms, and non-code camelCase like eDiscovery)
297
+ code_ids = []
298
+ for ident in TECHNICAL_CODE_ID.findall(query):
299
+ cleaned = ident.strip(".:,-/")
300
+ if (
301
+ cleaned.lower() not in QUERY_STOP_WORDS
302
+ and cleaned not in COMMON_ACRONYM_EXCLUSIONS
303
+ and cleaned.lower() not in scope_tokens
304
+ and cleaned.lower() not in COMMON_CAMEL_CASE_EXCLUSIONS
305
+ ):
306
+ code_ids.append(cleaned)
307
+
308
+ # Memory kind intent
309
+ requested_kind: Optional[str] = None
310
+ if any(k in q_lower for k in ("incident", "outage", "failure", "crash", "postmortem", "post-mortem", "downtime", "alert", "500", "сбой", "инцидент", "авария")):
311
+ requested_kind = "incident"
312
+ elif any(k in q_lower for k in ("architecture", "architectural", "adr", "rfc", "design", "архитектур")):
313
+ requested_kind = "architecture"
314
+ elif any(k in q_lower for k in ("config", "settings", "port", "host", "endpoint", "url", "key", "конфиг")):
315
+ requested_kind = "config"
316
+ elif any(k in q_lower for k in ("bug", "bugfix", "fix", "patch", "issue", "ошибк", "исправлен")):
317
+ requested_kind = "bugfix"
318
+
319
+ # Substantive question topics (tokens not in stop words, not in scope tokens, not kind keywords)
320
+ kind_noise = {
321
+ "incident", "outage", "failure", "crash", "postmortem", "downtime", "alert",
322
+ "architecture", "architectural", "design",
323
+ "config", "configuration", "settings", "options", "setup",
324
+ "bugfix", "reference", "general"
325
+ }
326
+ substantive_topics = set()
327
+ for tok in raw_tokens:
328
+ if (
329
+ len(tok) > 2
330
+ and tok not in QUERY_STOP_WORDS
331
+ and tok not in scope_tokens
332
+ and tok not in kind_noise
333
+ and not tok.isdigit()
334
+ ):
335
+ substantive_topics.add(tok)
336
+
337
+ explicit_domains = [domain] if domain else []
338
+ from benchmarks.adapters.native.event_evidence_b2 import PredicateNormalizer
339
+ ev_sig = PredicateNormalizer.extract_signature(
340
+ text=query,
341
+ domain_root=scoped_prefix or (explicit_domains[0].split(".")[0] if explicit_domains else ""),
342
+ )
343
+
344
+ return QueryEvidence(
345
+ query=query,
346
+ explicit_domains=explicit_domains,
347
+ explicit_identifiers=sorted(code_ids),
348
+ explicit_revs=explicit_revs or [],
349
+ requested_kind=requested_kind,
350
+ scoped_prefix=scoped_prefix,
351
+ scope_tokens=sorted(scope_tokens),
352
+ substantive_topics=sorted(substantive_topics),
353
+ is_cyrillic=is_cyrillic,
354
+ event_sig=ev_sig,
355
+ )
356
+
357
+
358
+ def extract_memory_evidence(
359
+ domain: str,
360
+ template: str,
361
+ referenced_facts: Optional[list[str]] = None,
362
+ event_id: str = "",
363
+ ) -> MemoryEvidence:
364
+ """Extract evidence envelope from memory domain and template."""
365
+ domain_root = extract_component_root(domain)
366
+ domain_kind = extract_domain_kind(domain)
367
+
368
+ # Component IDs from domain and template
369
+ comp_ids: set[str] = set()
370
+ if domain_root:
371
+ comp_ids.add(domain_root)
372
+
373
+ # Extract [PREFIX-123] or service name from template
374
+ prefix_match = re.findall(r"\b([A-Z]{3,})-\d+\b", template)
375
+ for p in prefix_match:
376
+ if p not in COMMON_ACRONYM_EXCLUSIONS:
377
+ comp_ids.add(p.lower())
378
+
379
+ t_lower = template.lower()
380
+ domain_segments = set(domain.lower().split("."))
381
+ for comp in KNOWN_SOFTWARE_COMPONENTS:
382
+ if comp == domain_root or comp in domain_segments:
383
+ comp_ids.add(comp)
384
+
385
+ # Memory kind
386
+ mem_kind = domain_kind
387
+ if any(k in t_lower for k in ("incident", "outage", "failure", "crash", "postmortem", "alert", "500")):
388
+ if mem_kind == "general":
389
+ mem_kind = "incident"
390
+ elif any(k in t_lower for k in ("adr", "rfc", "architecture", "decision")):
391
+ if mem_kind == "general":
392
+ mem_kind = "architecture"
393
+
394
+ # Entities
395
+ entities = [
396
+ ident.strip(".:,-/")
397
+ for ident in TECHNICAL_CODE_ID.findall(template)
398
+ if ident.lower() not in QUERY_STOP_WORDS and ident not in COMMON_ACRONYM_EXCLUSIONS
399
+ ]
400
+
401
+ # Fact refs
402
+ fact_refs = list(referenced_facts) if referenced_facts else []
403
+ in_tmpl_refs = re.findall(r"\{\{ref:([\w.]+)(?:@rev:\d+)?\}\}", template)
404
+ for r in in_tmpl_refs:
405
+ if r not in fact_refs:
406
+ fact_refs.append(r)
407
+
408
+ # Actions and Topics
409
+ raw_tokens = [w.lower().strip(".:,-/?!\"'(){}[]") for w in re.split(r"[\s\-._/]+", template)]
410
+ topics = [
411
+ tok for tok in raw_tokens
412
+ if (
413
+ len(tok) > 2
414
+ and tok not in QUERY_STOP_WORDS
415
+ and tok not in comp_ids
416
+ and not tok.isdigit()
417
+ )
418
+ ]
419
+
420
+ from benchmarks.adapters.native.event_evidence_b2 import PredicateNormalizer
421
+ m_sig = PredicateNormalizer.extract_signature(
422
+ text=template,
423
+ domain_root=domain_root,
424
+ event_id=event_id,
425
+ )
426
+
427
+ return MemoryEvidence(
428
+ domain_root=domain_root,
429
+ component_ids=sorted(comp_ids),
430
+ memory_kind=mem_kind,
431
+ entities=sorted(set(entities)),
432
+ topics=sorted(set(topics)),
433
+ actions=[],
434
+ fact_refs=sorted(set(fact_refs)),
435
+ template=template,
436
+ event_id=event_id,
437
+ event_sig=m_sig,
438
+ )
439
+
440
+
441
+ def resolve_evidence(
442
+ query_ev: QueryEvidence,
443
+ mem_ev: MemoryEvidence,
444
+ vector_score: float,
445
+ is_fact_boosted: bool = False,
446
+ enable_event_gate: bool = True,
447
+ ) -> tuple[SupportLevel, float, str]:
448
+ """Resolve whether candidate memory provides epistemic support for the query.
449
+
450
+ Returns:
451
+ (SupportLevel, evidence_score, reason)
452
+ """
453
+ from benchmarks.adapters.native.event_evidence_b2 import (
454
+ PredicateNormalizer,
455
+ EventRelation,
456
+ resolve_event_relation,
457
+ )
458
+
459
+ q_sig = getattr(query_ev, "event_sig", None)
460
+ if q_sig is None:
461
+ q_sig = PredicateNormalizer.extract_signature(
462
+ text=query_ev.query,
463
+ domain_root=query_ev.scoped_prefix or (query_ev.explicit_domains[0].split(".")[0] if query_ev.explicit_domains else ""),
464
+ )
465
+
466
+ m_sig = getattr(mem_ev, "event_sig", None)
467
+ if m_sig is None:
468
+ m_sig = PredicateNormalizer.extract_signature(
469
+ text=mem_ev.template or "",
470
+ domain_root=mem_ev.domain_root,
471
+ event_id=mem_ev.event_id or "",
472
+ )
473
+
474
+ has_event_semantics = (
475
+ q_sig.event_kind != "general"
476
+ or m_sig.event_kind != "general"
477
+ or bool(q_sig.trigger)
478
+ or bool(q_sig.failure_mode)
479
+ )
480
+
481
+ # 1. Symbolic Plane: Fact reverse lookup match
482
+ if is_fact_boosted:
483
+ if enable_event_gate and has_event_semantics:
484
+ rel = resolve_event_relation(q_sig, m_sig)
485
+ if rel == EventRelation.CONFLICTING_EVENT:
486
+ return SupportLevel.UNSUPPORTED, 0.0, f"Event evidence rejection: {rel.value} (ungrounded)"
487
+ return SupportLevel.STRONG, 1.0, "Symbolic fact match"
488
+
489
+ # 2. Symbolic Plane: Explicit code identifier gate
490
+ if query_ev.explicit_identifiers:
491
+ mem_ident_set = set(ident.lower() for ident in mem_ev.entities)
492
+ mem_topics_set = set(t.lower() for t in mem_ev.topics)
493
+ all_mem_tokens = mem_ident_set | mem_topics_set | set(mem_ev.component_ids)
494
+
495
+ has_ident_match = any(
496
+ qid.lower() in all_mem_tokens or any(qid.lower() in tok for tok in all_mem_tokens)
497
+ for qid in query_ev.explicit_identifiers
498
+ )
499
+ if has_ident_match:
500
+ if enable_event_gate and has_event_semantics:
501
+ rel = resolve_event_relation(q_sig, m_sig)
502
+ if rel == EventRelation.CONFLICTING_EVENT:
503
+ return SupportLevel.UNSUPPORTED, 0.0, f"Event evidence rejection: {rel.value} (ungrounded)"
504
+ return SupportLevel.STRONG, 0.95, "Code identifier match"
505
+ else:
506
+ return SupportLevel.UNSUPPORTED, 0.0, "Missing required code identifier (ungrounded)"
507
+
508
+ # 3. Event Plane: ARCH-EVENT-01 Structured Event Resolution
509
+ if enable_event_gate and has_event_semantics:
510
+ rel = resolve_event_relation(q_sig, m_sig)
511
+ if rel == EventRelation.CONFLICTING_EVENT:
512
+ return SupportLevel.UNSUPPORTED, 0.0, f"Event evidence rejection: {rel.value} (ungrounded)"
513
+ elif rel == EventRelation.SAME_EVENT:
514
+ return SupportLevel.STRONG, 0.95, "Verified event identity match"
515
+ elif rel == EventRelation.RELATED_EVENT:
516
+ if (q_sig.failure_mode and q_sig.failure_mode == m_sig.failure_mode and vector_score >= 0.60):
517
+ pass
518
+ else:
519
+ return SupportLevel.UNSUPPORTED, 0.0, f"Event evidence rejection: {rel.value} (ungrounded)"
520
+ elif rel == EventRelation.INSUFFICIENT_EVIDENCE:
521
+ if query_ev.requested_kind == "incident" or q_sig.event_kind == "incident":
522
+ mem_tokens = set(mem_ev.topics) | set(mem_ev.component_ids) | set(mem_ev.entities)
523
+ has_text_overlap = any(qt in mem_tokens for qt in query_ev.substantive_topics)
524
+ if not has_text_overlap:
525
+ return SupportLevel.UNSUPPORTED, 0.0, f"Event evidence rejection: {rel.value} (ungrounded)"
526
+
527
+
528
+ mem_tokens = set(mem_ev.topics) | set(mem_ev.component_ids) | set(mem_ev.entities)
529
+
530
+ # 3. Evidence Plane: Component-Scope Anchoring (SELECT-A)
531
+ scoped_prefix = query_ev.scoped_prefix
532
+ if scoped_prefix:
533
+ mem_comp_set = set(mem_ev.component_ids)
534
+ if mem_ev.domain_root:
535
+ mem_comp_set.add(mem_ev.domain_root)
536
+
537
+ if scoped_prefix not in mem_comp_set:
538
+ # Component mismatch: query explicitly scoped to a different component!
539
+ return SupportLevel.UNSUPPORTED, 0.0, f"Component mismatch ({mem_ev.domain_root} != {scoped_prefix})"
540
+
541
+ # Kind check: If query requested an incident, memory MUST be an incident
542
+ if query_ev.requested_kind == "incident":
543
+ if mem_ev.memory_kind != "incident":
544
+ return SupportLevel.UNSUPPORTED, 0.0, "Requested incident but memory is not an incident"
545
+
546
+ # Check substantive question topics
547
+ q_topics = query_ev.substantive_topics
548
+ topic_hits = 0
549
+ for qt in q_topics:
550
+ if qt in mem_tokens:
551
+ topic_hits += 1
552
+ else:
553
+ for mt in mem_tokens:
554
+ if len(qt) >= 4 and len(mt) >= 4 and (qt[:4] == mt[:4] or qt in mt or mt in qt):
555
+ topic_hits += 1
556
+ break
557
+ # Cross-lingual equivalence check
558
+ matched_cl = False
559
+ for eng, rus in CROSS_LINGUAL_PAIRS:
560
+ if (qt == eng and rus in mt) or (rus in qt and mt == eng):
561
+ topic_hits += 1
562
+ matched_cl = True
563
+ break
564
+ if matched_cl:
565
+ break
566
+
567
+ if q_topics and topic_hits == 0:
568
+ # Component matched, but substantive question topics are completely ungrounded!
569
+ return SupportLevel.UNSUPPORTED, 0.0, "Substantive question topics ungrounded in component memory"
570
+
571
+ # SELECT-B: Grounded Component + Topic Match
572
+ if vector_score >= 0.55:
573
+ return SupportLevel.STRONG, 0.90 + min(0.08, topic_hits * 0.02), f"Grounded component and topic match ({topic_hits} hits)"
574
+ elif vector_score >= 0.45:
575
+ return SupportLevel.MEDIUM, 0.70 + min(0.08, topic_hits * 0.02), f"Plausible component and topic match ({topic_hits} hits)"
576
+ elif query_ev.explicit_domains and (topic_hits >= 1 or not q_topics):
577
+ return SupportLevel.MEDIUM, 0.60 + min(0.08, topic_hits * 0.02), f"Domain-scoped topic match ({topic_hits} hits)"
578
+ else:
579
+ return SupportLevel.WEAK, 0.40, "Low vector similarity"
580
+
581
+ # 4. Pure semantic query without explicit component scope
582
+ if query_ev.requested_kind == "incident" and mem_ev.memory_kind != "incident":
583
+ return SupportLevel.UNSUPPORTED, 0.0, "Query requested incident but memory is not an incident"
584
+
585
+ # Cross-lingual / Cyrillic
586
+ if query_ev.is_cyrillic:
587
+ cyr_q = {w.lower() for w in query_ev.substantive_topics if re.search(r"[\u0400-\u04FF]", w)}
588
+ cyr_t = {w.lower() for w in mem_tokens if re.search(r"[\u0400-\u04FF]", w)}
589
+ stem_q = {w[:4] for w in cyr_q if len(w) >= 4}
590
+ stem_t = {w[:4] for w in cyr_t if len(w) >= 4}
591
+ stem_hits = len(stem_q & stem_t)
592
+ cyr_overlap = len(cyr_q & cyr_t)
593
+ if vector_score >= 0.60 and (cyr_overlap > 0 or stem_hits > 0):
594
+ return SupportLevel.MEDIUM, 0.75, "Cyrillic semantic match with topic grounding"
595
+ else:
596
+ return SupportLevel.UNSUPPORTED, 0.0, "Cyrillic match lacks lexical grounding"
597
+
598
+ topic_hits = sum(1 for qt in query_ev.substantive_topics if any(qt in mt or mt in qt for mt in mem_tokens))
599
+ if query_ev.substantive_topics and topic_hits == 0:
600
+ return SupportLevel.UNSUPPORTED, 0.0, "Substantive question topics ungrounded in memory"
601
+
602
+ if vector_score >= 0.65:
603
+ return SupportLevel.MEDIUM, 0.80, "High cosine match"
604
+ elif vector_score >= 0.60:
605
+ return SupportLevel.MEDIUM, 0.70, "Medium cosine match"
606
+ elif query_ev.explicit_domains and (topic_hits >= 1 or not query_ev.substantive_topics):
607
+ return SupportLevel.MEDIUM, 0.65, f"Domain-scoped semantic match ({topic_hits} hits)"
608
+ else:
609
+ return SupportLevel.WEAK, 0.40, "Low similarity"