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,261 @@
1
+ """Dandelion Candidate C4 Native Adapter (ARCH-ROUTE-01 / Hypothesis H11).
2
+
3
+ Implements Hierarchical Scope-Routed Semantic Plane:
4
+ 1. ScopeResolver structural constraints
5
+ 2. PartitionRouterIndex (Variant B descriptor-defined routing)
6
+ 3. EpistemicProgressiveWidener (L0 -> L1 -> L2 -> L3 -> L4 bounded cascade)
7
+ 4. Bounded local candidate search (100-500 chunks per partition)
8
+ 5. Evidence admission & late-binding fact hydration
9
+ """
10
+
11
+ from __future__ import annotations
12
+
13
+ from typing import Any, Optional
14
+ from pathlib import Path
15
+
16
+ import dandelion.store_kv
17
+ from dandelion.store_kv import KVStore
18
+ from dandelion.store_vector import VectorStore
19
+ from dandelion.hydrator import hydrate_template, pin_snapshots
20
+ from dandelion.domains import PartitionKey, PartitionDescriptor, UNSCOPED_LEGACY
21
+ from dandelion.router import PartitionRouterIndex, EpistemicProgressiveWidener
22
+ from dandelion.evidence import ScopeResolver, ScopeResolutionStatus
23
+ from benchmarks.adapters.native.base import NativeMemoryAdapter
24
+
25
+
26
+ class DandelionNativeAdapterC4(NativeMemoryAdapter):
27
+ """Candidate C4: Hierarchical Scope-Routed Semantic Memory Engine."""
28
+
29
+ def __init__(self) -> None:
30
+ self.kv: Optional[KVStore] = None
31
+ self.vec: Optional[VectorStore] = None
32
+ self.router: Optional[PartitionRouterIndex] = None
33
+ self.widener: Optional[EpistemicProgressiveWidener] = None
34
+ self.query_count = 0
35
+ self.total_tokens = 0
36
+ self.reset()
37
+
38
+ @property
39
+ def name(self) -> str:
40
+ return "dandelion_native_c4"
41
+
42
+ @property
43
+ def package_version(self) -> str:
44
+ return "dandelion-0.4.0-c4"
45
+
46
+ @property
47
+ def configuration_metadata(self) -> dict[str, Any]:
48
+ return {
49
+ "system": "dandelion_c4",
50
+ "architecture": "hierarchical_scope_routed_semantic_plane",
51
+ "candidate": "C4",
52
+ "embedding_model": "sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2",
53
+ "llm_used": "none (deterministic symbolic hydration)",
54
+ "vector_store": "qdrant_in_memory_partitioned",
55
+ "hydration_policy": "read_time_late_binding",
56
+ "security_policy": "deny_by_default_secret_masking",
57
+ "routing_policy": "variant_b_descriptor_proposals_with_epistemic_widening",
58
+ }
59
+
60
+ @property
61
+ def telemetry(self) -> dict[str, Any]:
62
+ avg_tok = (self.total_tokens / max(1, self.query_count)) if self.query_count else 0
63
+ return {
64
+ "query_count": self.query_count,
65
+ "llm_calls_total": 0,
66
+ "llm_calls_per_query": 0.0,
67
+ "api_calls_total": 0,
68
+ "api_calls_per_query": 0.0,
69
+ "cost_total_usd": 0.0,
70
+ "cost_per_1k_queries_usd": 0.0,
71
+ "mean_memory_context_tokens": round(avg_tok, 1),
72
+ }
73
+
74
+ def reset(self) -> None:
75
+ """Reset internal stores and partition router for a fresh benchmark run."""
76
+ self.kv = KVStore(path=None)
77
+ dandelion.store_kv._store = self.kv
78
+ self.vec = VectorStore(in_memory=True)
79
+ self.router = PartitionRouterIndex()
80
+ self.widener = EpistemicProgressiveWidener(self.router)
81
+ self.query_count = 0
82
+ self.total_tokens = 0
83
+
84
+ def register_partition(
85
+ self,
86
+ domain: str,
87
+ project: str = "default",
88
+ title: str = "",
89
+ description: str = "",
90
+ service_role: str = "",
91
+ keywords: Optional[list[str]] = None,
92
+ ) -> PartitionDescriptor:
93
+ """Register a known partition descriptor in the router (Variant B)."""
94
+ pkey = PartitionKey.from_domain(domain, project=project)
95
+ comp = pkey.component
96
+ desc = PartitionDescriptor(
97
+ key=pkey,
98
+ title=title or f"{comp.capitalize()} Component",
99
+ description=description or f"Operational and architectural memory for {domain} ({comp}).",
100
+ service_role=service_role or comp,
101
+ keywords=list(keywords or [pkey.project, pkey.domain, comp]),
102
+ )
103
+ self.router.register_descriptor(desc)
104
+ return desc
105
+
106
+ def ingest_fact(
107
+ self,
108
+ key: str,
109
+ value: str,
110
+ timestamp: float,
111
+ domain: str = "general",
112
+ source: str = "config_read",
113
+ ) -> None:
114
+ """Ingest exact fact into KVStore and register domain partition."""
115
+ self.kv.set_fact(key=key, value=value, source=source, timestamp=timestamp)
116
+ # Ensure partition descriptor in router
117
+ pkey = PartitionKey.from_domain(domain)
118
+ if not self.router.get_descriptor(pkey.canonical_id):
119
+ self.register_partition(domain=domain, project=pkey.project)
120
+
121
+ def ingest_memory(
122
+ self,
123
+ event_id: str,
124
+ template: str,
125
+ domain: str,
126
+ timestamp: float,
127
+ supersedes: Optional[str] = None,
128
+ source: str = "tool_observed",
129
+ ) -> str:
130
+ """Ingest episodic memory with pinned snapshots into partitioned vector space."""
131
+ pinned = pin_snapshots(template, self.kv.get_current_revision, domain)
132
+ pkey = PartitionKey.from_domain(domain)
133
+ if not self.router.get_descriptor(pkey.canonical_id):
134
+ self.register_partition(domain=domain, project=pkey.project)
135
+
136
+ return self.vec.save_chunk(
137
+ domain=domain,
138
+ template=pinned,
139
+ supersedes=supersedes,
140
+ source=source,
141
+ created_at=timestamp,
142
+ event_id=event_id,
143
+ partition_id=pkey.canonical_id,
144
+ project_id=pkey.project,
145
+ component=pkey.component,
146
+ )
147
+
148
+ def recall(
149
+ self,
150
+ query: str,
151
+ domain: Optional[str] = None,
152
+ limit: int = 3,
153
+ ) -> list[dict[str, Any]]:
154
+ """Hierarchical scope-routed retrieval across partitions."""
155
+ self.query_count += 1
156
+
157
+ # 1. Structural Scope Resolution Gate
158
+ scope = ScopeResolver.resolve(
159
+ query=query,
160
+ retrieval_scope=domain,
161
+ registry=self.kv.get_domain_registry(),
162
+ )
163
+ if scope.status == ScopeResolutionStatus.EXPLICIT_UNRESOLVED:
164
+ return []
165
+
166
+ # Enforce asserted scope conflict when query asserts a foreign scope
167
+ if scope.status == ScopeResolutionStatus.RESOLVED and domain:
168
+ from dandelion.domains import extract_component_root
169
+ retrieval_root = extract_component_root(domain)
170
+ domain_parts = set(p.lower() for p in domain.replace("/", ".").split(".") if p)
171
+ is_match = (
172
+ scope.asserted_scope == retrieval_root
173
+ or (scope.asserted_scope and scope.asserted_scope.lower() in domain_parts)
174
+ or domain.lower().startswith((scope.asserted_scope or "").lower())
175
+ or (scope.asserted_scope or "").lower().startswith(retrieval_root.lower())
176
+ )
177
+ if not is_match:
178
+ return []
179
+
180
+ # Effective explicit scope
181
+ effective_scope = domain or scope.asserted_scope
182
+
183
+ # 2. Local retrieval function inside proposed partitions
184
+ def _local_retrieval(pids: list[str]) -> list[dict[str, Any]]:
185
+ return self.vec.search_chunks(
186
+ query=query,
187
+ domains=[domain] if domain else None,
188
+ partition_ids=pids,
189
+ limit=max(limit * 5, 20),
190
+ )
191
+
192
+ # 3. Epistemic progressive widening cascade (L0 -> L1 -> L2 -> L3 -> L4 bounded)
193
+ admitted, _ = self.widener.widen_and_retrieve(
194
+ query=query,
195
+ retrieval_fn=_local_retrieval,
196
+ explicit_scope=effective_scope,
197
+ max_results=limit,
198
+ )
199
+
200
+ # 4. Late-binding fact hydration on admitted candidates
201
+ hydrated: list[dict[str, Any]] = []
202
+ for r in admitted:
203
+ text = hydrate_template(
204
+ text=r["template"],
205
+ kv_resolver=self.kv.resolve_ref,
206
+ chunk_domain=r.get("domain", "general"),
207
+ allow_secrets=False,
208
+ )
209
+ hydrated.append({
210
+ "id": r["id"],
211
+ "event_id": r.get("event_id", r["id"]),
212
+ "text": text,
213
+ "domain": r.get("domain", "general"),
214
+ "partition_id": r.get("partition_id", UNSCOPED_LEGACY.canonical_id),
215
+ "score": r.get("evidence_score", r.get("score", 0.0)),
216
+ "vector_score": r.get("vector_score", 0.0),
217
+ "has_fact": r.get("has_fact", False),
218
+ "is_historical": "{{ref:" in r["template"] and "@rev:" in r["template"],
219
+ })
220
+
221
+ tok_sum = sum(len(h["text"].split()) for h in hydrated)
222
+ self.total_tokens += tok_sum
223
+ return hydrated
224
+
225
+ def answer(
226
+ self,
227
+ query: str,
228
+ domain: Optional[str] = None,
229
+ ) -> dict[str, Any]:
230
+ """Context-envelope answering surface."""
231
+ candidates = self.recall(query=query, domain=domain, limit=3)
232
+ if not candidates:
233
+ return {
234
+ "answer": "I have no memory regarding this topic.",
235
+ "retrieved_context": "",
236
+ "context_tokens": 0,
237
+ "llm_calls": 0,
238
+ "api_calls": 0,
239
+ "cost_usd": 0.0,
240
+ }
241
+
242
+ context = "\n".join(f"[{c['domain']}] {c['text']}" for c in candidates)
243
+ tok_count = sum(len(c["text"].split()) for c in candidates)
244
+ return {
245
+ "answer": candidates[0]["text"],
246
+ "retrieved_context": context,
247
+ "context_tokens": tok_count,
248
+ "llm_calls": 0,
249
+ "api_calls": 0,
250
+ "cost_usd": 0.0,
251
+ }
252
+
253
+ def get_fact(
254
+ self,
255
+ key: str,
256
+ domain: Optional[str] = None,
257
+ at: Optional[float] = None,
258
+ ) -> Optional[str]:
259
+ if at is not None:
260
+ return self.kv.get_fact(key, at_timestamp=at)
261
+ return self.kv.resolve_ref(key, domain=domain)
@@ -0,0 +1,281 @@
1
+ """Dandelion Native Benchmark Adapter: Candidate C4.1 (ARCH-ROUTE-01 / Variable-Depth Lineage).
2
+
3
+ Architectural Evolution (C4 -> C4.1):
4
+ 1. Variable-depth PartitionPath: native arbitrary nesting (e.g. project/cloud/compute/hypervisor/xen).
5
+ 2. Structural explicit scope routing (Authority vs Discovery): no semantic cosine overriding explicit scopes.
6
+ 3. Generic Fact-Key Path Segment Alignment without magic constants (structural discriminant in ranking tuple).
7
+ 4. Strict Gate 4 candidate budgeting: tracks total unique candidates examined <= 500 across widening cascade.
8
+ 5. Lineage isolation: C3 frozen, C4 DEV frozen, C4.1 candidate checkpoint.
9
+ """
10
+
11
+ from __future__ import annotations
12
+
13
+ import time
14
+ from typing import Any, Optional
15
+
16
+ import dandelion.store_kv
17
+ from dandelion.domains import (
18
+ PartitionDescriptor,
19
+ PartitionKey,
20
+ PartitionPath,
21
+ UNSCOPED_LEGACY,
22
+ )
23
+ from dandelion.evidence import ScopeResolutionStatus, ScopeResolver
24
+ from dandelion.hydrator import hydrate_template, pin_snapshots
25
+ from dandelion.router import EpistemicProgressiveWidener, PartitionRouterIndex, ScopeLevel
26
+ from dandelion.store_kv import KVStore
27
+ from dandelion.store_vector import VectorStore
28
+
29
+
30
+ class DandelionNativeAdapterC41:
31
+ """Benchmark adapter evaluating Candidate C4.1 against standard retrieval surfaces."""
32
+
33
+ def __init__(self) -> None:
34
+ self.kv = KVStore(path=None)
35
+ dandelion.store_kv._store = self.kv
36
+ self.vec = VectorStore(in_memory=True)
37
+ self.router = PartitionRouterIndex()
38
+ self.widener = EpistemicProgressiveWidener(self.router)
39
+ self.query_count = 0
40
+ self.total_tokens = 0
41
+ self.query_telemetry_log: list[dict[str, Any]] = []
42
+
43
+ @property
44
+ def system_info(self) -> dict[str, Any]:
45
+ return {
46
+ "name": "dandelion_native_c4_1",
47
+ "version": "0.4.1",
48
+ "architecture": "hierarchical_variable_depth_scope_routed_semantic_plane",
49
+ "candidate": "C4.1",
50
+ "embedding_model": "sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2",
51
+ "llm_used": "none (deterministic symbolic hydration)",
52
+ "vector_store": "qdrant_in_memory_partitioned",
53
+ "hydration_policy": "read_time_late_binding",
54
+ "security_policy": "deny_by_default_secret_masking",
55
+ "routing_policy": "structural_path_authority_with_descriptor_discovery",
56
+ }
57
+
58
+ @property
59
+ def telemetry(self) -> dict[str, Any]:
60
+ avg_tok = (self.total_tokens / max(1, self.query_count)) if self.query_count else 0
61
+ return {
62
+ "query_count": self.query_count,
63
+ "llm_calls_total": 0,
64
+ "llm_calls_per_query": 0.0,
65
+ "api_calls_total": 0,
66
+ "api_calls_per_query": 0.0,
67
+ "cost_total_usd": 0.0,
68
+ "cost_per_1k_queries_usd": 0.0,
69
+ "mean_memory_context_tokens": round(avg_tok, 1),
70
+ "last_widening_telemetry": self.widener.last_telemetry,
71
+ }
72
+
73
+ def reset(self) -> None:
74
+ """Reset internal stores and partition router for a fresh benchmark run."""
75
+ self.kv = KVStore(path=None)
76
+ dandelion.store_kv._store = self.kv
77
+ self.vec = VectorStore(in_memory=True)
78
+ self.router = PartitionRouterIndex()
79
+ self.widener = EpistemicProgressiveWidener(self.router)
80
+ self.query_count = 0
81
+ self.total_tokens = 0
82
+ self.query_telemetry_log.clear()
83
+
84
+ def register_partition(
85
+ self,
86
+ domain: str,
87
+ project: str = "default",
88
+ title: str = "",
89
+ description: str = "",
90
+ service_role: str = "",
91
+ keywords: Optional[list[str]] = None,
92
+ ) -> PartitionDescriptor:
93
+ """Register a known partition descriptor in the router (Variant B) using variable-depth PartitionPath."""
94
+ ppath = PartitionPath.from_domain(domain, project=project)
95
+ comp = ppath.leaf_component
96
+
97
+ domain_tokens = [p.lower() for p in domain.replace("/", ".").split(".") if p]
98
+ base_keywords = list(set([ppath.project.lower()] + list(ppath.segments) + domain_tokens + [k.lower() for k in (keywords or [])]))
99
+
100
+ desc = self.router.get_descriptor(ppath.canonical_id)
101
+ if desc:
102
+ desc.add_keywords(domain_tokens)
103
+ return desc
104
+
105
+ desc = PartitionDescriptor(
106
+ key=ppath,
107
+ title=title or f"{comp.capitalize()} Component",
108
+ description=description or f"Operational and architectural memory for {domain} ({comp}).",
109
+ service_role=service_role or comp,
110
+ keywords=base_keywords,
111
+ )
112
+ self.router.register_descriptor(desc)
113
+ return desc
114
+
115
+ def ingest_fact(
116
+ self,
117
+ key: str,
118
+ value: str,
119
+ timestamp: float,
120
+ domain: str = "general",
121
+ source: str = "config_read",
122
+ ) -> None:
123
+ """Ingest exact fact into KVStore and register domain partition."""
124
+ self.kv.set_fact(key=key, value=value, source=source, timestamp=timestamp)
125
+ ppath = PartitionPath.from_domain(domain)
126
+ existing_desc = self.router.get_descriptor(ppath.canonical_id)
127
+ domain_tokens = [p.lower() for p in domain.replace("/", ".").split(".") if p]
128
+ if existing_desc:
129
+ existing_desc.add_keywords(domain_tokens)
130
+ else:
131
+ self.register_partition(domain=domain, project=ppath.project)
132
+
133
+ def ingest_memory(
134
+ self,
135
+ event_id: str,
136
+ template: str,
137
+ domain: str,
138
+ timestamp: float,
139
+ supersedes: Optional[str] = None,
140
+ source: str = "tool_observed",
141
+ ) -> str:
142
+ """Ingest episodic memory with pinned snapshots into partitioned vector space."""
143
+ pinned = pin_snapshots(template, self.kv.get_current_revision, domain)
144
+ ppath = PartitionPath.from_domain(domain)
145
+ existing_desc = self.router.get_descriptor(ppath.canonical_id)
146
+ domain_tokens = [p.lower() for p in domain.replace("/", ".").split(".") if p]
147
+ if existing_desc:
148
+ existing_desc.add_keywords(domain_tokens)
149
+ else:
150
+ self.register_partition(domain=domain, project=ppath.project)
151
+
152
+ return self.vec.save_chunk(
153
+ domain=domain,
154
+ template=pinned,
155
+ supersedes=supersedes,
156
+ source=source,
157
+ created_at=timestamp,
158
+ event_id=event_id,
159
+ partition_id=ppath.canonical_id,
160
+ project_id=ppath.project,
161
+ component=ppath.leaf_component,
162
+ )
163
+
164
+ def recall(
165
+ self,
166
+ query: str,
167
+ domain: Optional[str] = None,
168
+ limit: int = 3,
169
+ ) -> list[dict[str, Any]]:
170
+ """Hierarchical scope-routed retrieval across partitions."""
171
+ self.query_count += 1
172
+
173
+ # 1. Structural Scope Resolution Gate
174
+ scope = ScopeResolver.resolve(
175
+ query=query,
176
+ retrieval_scope=domain,
177
+ registry=self.kv.get_domain_registry(),
178
+ )
179
+ if scope.status == ScopeResolutionStatus.EXPLICIT_UNRESOLVED:
180
+ return []
181
+
182
+ # Enforce asserted scope conflict when query asserts a foreign scope
183
+ if scope.status == ScopeResolutionStatus.RESOLVED and domain:
184
+ from dandelion.domains import extract_component_root
185
+ retrieval_root = extract_component_root(domain)
186
+ domain_parts = set(p.lower() for p in domain.replace("/", ".").split(".") if p)
187
+ is_match = (
188
+ scope.asserted_scope == retrieval_root
189
+ or (scope.asserted_scope and scope.asserted_scope.lower() in domain_parts)
190
+ or domain.lower().startswith((scope.asserted_scope or "").lower())
191
+ or (scope.asserted_scope or "").lower().startswith(retrieval_root.lower())
192
+ )
193
+ if not is_match:
194
+ return []
195
+
196
+ # Effective explicit scope
197
+ effective_scope = domain or (
198
+ scope.raw_asserted_text
199
+ if (scope.raw_asserted_text and ("." in scope.raw_asserted_text or "/" in scope.raw_asserted_text))
200
+ else scope.asserted_scope
201
+ )
202
+
203
+ # 2. Local retrieval function inside proposed partitions
204
+ def _local_retrieval(pids: list[str]) -> list[dict[str, Any]]:
205
+ return self.vec.search_chunks(
206
+ query=query,
207
+ domains=[domain] if domain else None,
208
+ partition_ids=pids,
209
+ limit=max(limit * 5, 20),
210
+ )
211
+
212
+ # 3. Epistemic progressive widening cascade (L0 -> L1 -> L2 -> L3 -> L4 bounded)
213
+ admitted, _ = self.widener.widen_and_retrieve(
214
+ query=query,
215
+ retrieval_fn=_local_retrieval,
216
+ explicit_scope=effective_scope,
217
+ max_results=limit,
218
+ candidate_budget=500,
219
+ )
220
+
221
+ # Log Gate 4 telemetry
222
+ self.query_telemetry_log.append({
223
+ "query": query,
224
+ "domain": domain,
225
+ "telemetry": dict(self.widener.last_telemetry),
226
+ })
227
+
228
+ # 4. Late-binding fact hydration on admitted candidates
229
+ hydrated: list[dict[str, Any]] = []
230
+ for r in admitted:
231
+ text = hydrate_template(
232
+ text=r["template"],
233
+ kv_resolver=self.kv.resolve_ref,
234
+ chunk_domain=r.get("domain", "general"),
235
+ allow_secrets=False,
236
+ )
237
+ hydrated.append({
238
+ "id": r["id"],
239
+ "event_id": r.get("event_id", r["id"]),
240
+ "text": text,
241
+ "domain": r.get("domain", "general"),
242
+ "partition_id": r.get("partition_id", UNSCOPED_LEGACY.canonical_id),
243
+ "score": r.get("score", r.get("evidence_score", 0.0)),
244
+ "evidence_score": r.get("evidence_score", 0.0),
245
+ "vector_score": r.get("vector_score", 0.0),
246
+ "has_fact": r.get("has_fact", False),
247
+ "has_attr_match": bool(r.get("attr_match_count", 0) > 0),
248
+ "is_historical": "{{ref:" in r["template"] and "@rev:" in r["template"],
249
+ })
250
+
251
+ tok_sum = sum(len(h["text"].split()) for h in hydrated)
252
+ self.total_tokens += tok_sum
253
+ return hydrated
254
+
255
+ def answer(
256
+ self,
257
+ query: str,
258
+ domain: Optional[str] = None,
259
+ ) -> dict[str, Any]:
260
+ """Context-envelope answering surface."""
261
+ candidates = self.recall(query=query, domain=domain, limit=3)
262
+ if not candidates:
263
+ return {
264
+ "answer": "I have no memory regarding this topic.",
265
+ "retrieved_context": "",
266
+ "context_tokens": 0,
267
+ "llm_calls": 0,
268
+ "api_calls": 0,
269
+ "cost_usd": 0.0,
270
+ }
271
+
272
+ lines = [f"[{c['domain']}] {c['text']}" for c in candidates]
273
+ context = "\n".join(lines)
274
+ return {
275
+ "answer": context,
276
+ "retrieved_context": context,
277
+ "context_tokens": sum(len(c["text"].split()) for c in candidates),
278
+ "llm_calls": 0,
279
+ "api_calls": 0,
280
+ "cost_usd": 0.0,
281
+ }