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,497 @@
1
+ """Small runtime preflight and single-turn native Codex actor for LongMemEval.
2
+
3
+ This module does not load benchmark questions or oracle data. Callers own the
4
+ evaluation contract, output directory, and wall-clock timeout.
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ import asyncio
10
+ import json
11
+ import math
12
+ import os
13
+ from pathlib import Path
14
+ import signal
15
+ import time
16
+ from typing import Any, Iterable
17
+ import uuid
18
+
19
+ from fastmcp import Client
20
+ from fastmcp.client.transports import StdioTransport
21
+
22
+
23
+ SERVER = "dandelion"
24
+ CODE_MODE_WARNING = "Under-development features enabled: code_mode. Under-development features are incomplete and may behave unpredictably."
25
+ RUN_MARKER = ".longmemeval-actor-run.json"
26
+ MEMORY_DB = Path(".dandelion") / "memory.sqlite3"
27
+ WRITER_TOOLS = frozenset({
28
+ "domain_map", "list_memories", "read_memory", "define_domain", "create_fact",
29
+ "remember_typed", "lifecycle_v1_correct_fact", "lifecycle_v1_correct_memory",
30
+ "get_fact", "lifecycle_v1_history",
31
+ })
32
+ READER_TOOLS = frozenset({
33
+ "domain_map", "list_memories", "read_memory", "get_fact", "search",
34
+ "lifecycle_v1_history",
35
+ })
36
+ DISABLED_FEATURES = (
37
+ "shell_tool", "apps", "plugins", "multi_agent", "view_image",
38
+ "browser_use", "computer_use", "image_generation", "memories", "hooks",
39
+ "plugin_hooks", "goals", "workspace_dependencies", "multi_agent_v2",
40
+ )
41
+
42
+
43
+ class ActorIsolationError(ValueError):
44
+ """The native actor trace cannot establish the allowed execution boundary."""
45
+
46
+
47
+ def audit_retained_session(events: list[dict[str, Any]], allowed_tools: Iterable[str],
48
+ session_root: Path, output: Path, *,
49
+ deadline: float | None = None) -> dict[str, Any]:
50
+ """Audit only this actor's normal native session, never unrelated history."""
51
+ ids = [event.get("thread_id") for event in events if event.get("type") == "thread.started"]
52
+ if len(ids) != 1:
53
+ raise ActorIsolationError("expected one native actor thread identity")
54
+ thread_id = str(uuid.UUID(ids[0]))
55
+ # Native stdout completion can precede the asynchronous session-journal flush.
56
+ # Await the durable terminal record, within the caller's ORIGINAL deadline.
57
+ while True:
58
+ matches = list(session_root.rglob(f"*-{thread_id}.jsonl"))
59
+ if len(matches) > 1:
60
+ raise ActorIsolationError("native actor session identity is not unique")
61
+ rows = None
62
+ if matches:
63
+ source = matches[0]
64
+ raw = source.read_bytes()
65
+ if raw.endswith(b"\n"):
66
+ try:
67
+ parsed = [json.loads(line) for line in raw.split(b"\n") if line]
68
+ if any(row.get("type") == "event_msg"
69
+ and row.get("payload", {}).get("type") == "task_complete"
70
+ for row in parsed):
71
+ rows = parsed
72
+ except (json.JSONDecodeError, UnicodeDecodeError):
73
+ pass
74
+ if rows is not None:
75
+ break
76
+ if deadline is None or time.monotonic() >= deadline:
77
+ raise ActorIsolationError("complete native session journal unavailable before deadline")
78
+ time.sleep(min(0.05, max(0, deadline - time.monotonic())))
79
+ calls_and_outputs = [row for row in rows if row.get("type") == "response_item"
80
+ and row.get("payload", {}).get("type") in {
81
+ "function_call", "function_call_output", "custom_tool_call",
82
+ "custom_tool_call_output"}]
83
+ permitted = {"exec", "wait"} | {f"mcp__{SERVER}__{name}" for name in allowed_tools}
84
+ calls = [row["payload"] for row in calls_and_outputs
85
+ if row["payload"]["type"] in {"function_call", "custom_tool_call"}]
86
+ forbidden = [call.get("name") for call in calls if call.get("name") not in permitted]
87
+ with output.open("x", encoding="utf-8") as stream:
88
+ for row in calls_and_outputs:
89
+ stream.write(json.dumps(row, ensure_ascii=False) + "\n")
90
+ if forbidden:
91
+ raise ActorIsolationError(f"forbidden native tool calls: {forbidden}")
92
+ if not calls:
93
+ raise ActorIsolationError("native actor trace contains no auditable tool calls")
94
+ return {"status": "PASS", "thread_id": thread_id, "source_path": str(source),
95
+ "tool_trace_path": str(output), "calls": len(calls),
96
+ "call_names": sorted({call["name"] for call in calls}),
97
+ "scope": "observed calls; not adversarial sandbox containment"}
98
+
99
+
100
+ def _json_text(result: Any) -> dict[str, Any]:
101
+ for block in result.content:
102
+ value = getattr(block, "text", None)
103
+ if value:
104
+ parsed = json.loads(value)
105
+ if isinstance(parsed, dict):
106
+ return parsed
107
+ raise RuntimeError("MCP tool returned no JSON object")
108
+
109
+
110
+ def _new_workspace(path: Path) -> Path:
111
+ path = path.resolve()
112
+ if path.exists():
113
+ raise FileExistsError(f"isolated memory workspace already exists: {path}")
114
+ if not path.parent.is_dir():
115
+ raise FileNotFoundError(f"workspace parent must already exist: {path.parent}")
116
+ path.mkdir()
117
+ return path
118
+
119
+
120
+ def create_actor_store(new_run_root: str | Path) -> Path:
121
+ """Create a new marked actor run and return its dedicated memory directory."""
122
+ root = Path(new_run_root).resolve()
123
+ if root.exists():
124
+ raise FileExistsError(f"actor run root already exists: {root}")
125
+ if not root.parent.is_dir():
126
+ raise FileNotFoundError(f"actor run parent must already exist: {root.parent}")
127
+ root.mkdir()
128
+ memory = root / "memory"
129
+ memory.mkdir()
130
+ marker = {"schema": "longmemeval.actor-run.v1", "root": str(root),
131
+ "memory_path": str(memory.resolve()), "writer_started": False}
132
+ with (root / RUN_MARKER).open("x", encoding="utf-8") as stream:
133
+ json.dump(marker, stream, indent=2)
134
+ stream.write("\n")
135
+ return memory.resolve()
136
+
137
+
138
+ def _validate_actor_store(memory_path: str | Path, *, role: str,
139
+ mark_writer: bool = True) -> tuple[Path, dict[str, Any]]:
140
+ memory = Path(memory_path).resolve()
141
+ root = memory.parent
142
+ marker_path = root / RUN_MARKER
143
+ if not root.is_dir() or not memory.is_dir() or not marker_path.is_file():
144
+ raise ValueError("memory path must belong to a coordinator-created marked actor store")
145
+ marker = json.loads(marker_path.read_text(encoding="utf-8"))
146
+ if (marker.get("schema") != "longmemeval.actor-run.v1"
147
+ or marker.get("root") != str(root)
148
+ or marker.get("memory_path") != str(memory)):
149
+ raise ValueError("actor run marker does not match the resolved memory root")
150
+ db = memory / MEMORY_DB
151
+ if role == "writer":
152
+ if db.exists():
153
+ raise FileExistsError("writer refuses an existing actor memory database")
154
+ if marker.get("writer_started"):
155
+ raise ValueError("writer already started for this actor store; retries are refused")
156
+ if mark_writer:
157
+ marker["writer_started"] = True
158
+ marker_path.write_text(json.dumps(marker, indent=2) + "\n", encoding="utf-8")
159
+ elif role == "reader":
160
+ if marker.get("writer_started") is not True or not db.is_file():
161
+ raise ValueError("reader requires the marked writer store and its SQLite database")
162
+ else:
163
+ raise ValueError("role must be writer or reader")
164
+ return root, marker
165
+
166
+
167
+ def _validate_fresh_receipts(paths: list[str | Path]) -> list[Path]:
168
+ resolved = [Path(path).resolve() for path in paths]
169
+ if len(set(resolved)) != len(resolved):
170
+ raise ValueError("event, stderr, and final receipts require distinct paths")
171
+ for path in resolved:
172
+ if path.exists():
173
+ raise FileExistsError(f"receipt path already exists: {path}")
174
+ if not path.parent.is_dir():
175
+ raise FileNotFoundError(f"receipt parent must already exist: {path.parent}")
176
+ return resolved
177
+
178
+
179
+ async def stdio_preflight(memory_exe: str | Path, workspace: str | Path) -> dict[str, Any]:
180
+ """Exercise a fresh, source-free trusted-local owner over ordinary stdio MCP."""
181
+ root = _new_workspace(Path(workspace))
182
+ argv = [str(Path(memory_exe).resolve()), "serve", "--memory-path", str(root),
183
+ "--local-owner", "--access-mode", "trusted-local", "--lifecycle-v1"]
184
+ transport = StdioTransport(command=argv[0], args=argv[1:], cwd=str(root))
185
+ calls: list[dict[str, Any]] = []
186
+
187
+ async with Client(transport, timeout=30) as client:
188
+ tools = await client.list_tools()
189
+ names = {item.name for item in tools}
190
+ required = WRITER_TOOLS | {"search"}
191
+ if not required.issubset(names):
192
+ raise RuntimeError(f"installed MCP tool contract missing: {sorted(required - names)}")
193
+ schemas = {item.name: item.input_schema for item in tools}
194
+ if not {"subject", "value", "reason"}.issubset(schemas["create_fact"].get("properties", {})):
195
+ raise RuntimeError("create_fact schema does not expose expected fields")
196
+ if "expected_revision" not in schemas["lifecycle_v1_correct_fact"].get("properties", {}):
197
+ raise RuntimeError("fact correction schema omits expected_revision")
198
+
199
+ async def call(name: str, arguments: dict[str, Any]) -> dict[str, Any]:
200
+ value = _json_text(await client.call_tool(name, arguments))
201
+ calls.append({"tool": name, "arguments": arguments, "result": value})
202
+ return value
203
+
204
+ before = await call("domain_map", {})
205
+ domain = "lme.runtime_preflight"
206
+ defined = await call("define_domain", {
207
+ "domain": domain, "label": "Runtime preflight",
208
+ "description": "Temporary source-free writer and fresh-reader route check.",
209
+ })
210
+ created = await call("create_fact", {
211
+ "subject": "preflight marker", "value": "alpha", "reason": "runtime route check",
212
+ })
213
+ key = created.get("key")
214
+ if not key or created.get("status") != "OK":
215
+ raise RuntimeError(f"create_fact did not return an owner-issued key: {created}")
216
+ live = created.get("reference_templates", {}).get("live")
217
+ pinned = created.get("reference_templates", {}).get("pinned")
218
+ if not live or not pinned:
219
+ raise RuntimeError("create_fact omitted live or pinned reference templates")
220
+ note_text = f"Preflight marker {live}; original value {pinned}."
221
+ note = await call("remember_typed", {
222
+ "content": note_text, "domain": domain, "record_kind": "OBSERVATION",
223
+ "disposition": "ACTIVE", "topic": "stdio-route",
224
+ })
225
+ if note.get("status") != "OK" or not note.get("memory_id"):
226
+ raise RuntimeError(f"remember_typed failed: {note}")
227
+ corrected = await call("lifecycle_v1_correct_fact", {
228
+ "key": key, "value": "beta", "reason": "expected-revision route check",
229
+ "expected_revision": created.get("revision"),
230
+ })
231
+ if corrected.get("revision") != 2:
232
+ raise RuntimeError(f"expected_revision correction did not append revision 2: {corrected}")
233
+ await call("get_fact", {"key": key})
234
+ await call("get_fact", {"key": key, "revision": 1})
235
+ created_history = await call("lifecycle_v1_history", {"record_kind": "fact", "canonical_id": key})
236
+ if [row.get("action") for row in created_history.get("history", [])] != ["CREATED", "CORRECTED"]:
237
+ raise RuntimeError("writer did not observe created and corrected fact history")
238
+ after = await call("domain_map", {})
239
+
240
+ # Reopen as a genuinely new stdio client/owner process and read the canonical result.
241
+ transport = StdioTransport(command=argv[0], args=argv[1:], cwd=str(root))
242
+ async with Client(transport, timeout=30) as client:
243
+ reopened_map = _json_text(await client.call_tool("domain_map", {}))
244
+ listed = _json_text(await client.call_tool("list_memories", {"domain": domain}))
245
+ readback = _json_text(await client.call_tool("read_memory", {
246
+ "memory_id": note["memory_id"], "domain": domain,
247
+ }))
248
+ current = _json_text(await client.call_tool("get_fact", {"key": key}))
249
+ original = _json_text(await client.call_tool("get_fact", {"key": key, "revision": 1}))
250
+ history = _json_text(await client.call_tool("lifecycle_v1_history", {
251
+ "record_kind": "fact", "canonical_id": key,
252
+ }))
253
+ reopened = {"domain_map": reopened_map, "list_memories": listed,
254
+ "read_memory": readback, "get_fact": current, "original_fact": original,
255
+ "history": history}
256
+
257
+ if current.get("value") != "beta" or current.get("revision") != 2:
258
+ raise RuntimeError("fresh stdio reader did not observe corrected current fact")
259
+ if readback.get("status") != "SUPPORTED" or "beta" not in readback.get("text", ""):
260
+ raise RuntimeError("fresh stdio reader did not resolve live linked fact")
261
+ if "alpha" not in readback.get("text", ""):
262
+ raise RuntimeError("fresh stdio reader did not retain pinned original fact")
263
+ if original.get("value") != "alpha" or original.get("revision") != 1:
264
+ raise RuntimeError("fresh stdio reader did not read revision 1")
265
+ if not any(row.get("domain") == domain for row in reopened_map.get("domains", [])):
266
+ raise RuntimeError("fresh stdio reader map omitted the created domain")
267
+ if not any(row.get("memory_id") == note["memory_id"] for row in listed.get("records", [])):
268
+ raise RuntimeError("fresh stdio reader list omitted the created memory")
269
+ if [row.get("action") for row in history.get("history", [])] != ["CREATED", "CORRECTED"]:
270
+ raise RuntimeError("fresh stdio reader lifecycle history did not preserve both revisions")
271
+ return {
272
+ "status": "PASS", "transport": "FastMCP StdioTransport",
273
+ "workspace": str(root), "source_binding": None,
274
+ "owner_args": argv[1:], "tools": sorted(names), "schemas": schemas,
275
+ "initial_domain_map": before, "domain_definition": defined,
276
+ "post_write_domain_map": after, "writer_calls": calls, "fresh_reader": reopened,
277
+ "assertions": {"current_revision_2": True, "pinned_alpha": True,
278
+ "live_beta": True, "source_free": True},
279
+ }
280
+
281
+
282
+ def codex_exec_command(codex_exe: str | Path, memory_exe: str | Path, *,
283
+ memory_path: str | Path, actor_cwd: str | Path,
284
+ model: str, role: str, prompt: str,
285
+ events_path: str | Path, stderr_path: str | Path,
286
+ final_path: str | Path, retain_session: bool = False) -> list[str]:
287
+ """Build one fresh native Codex invocation with a run-local stdio MCP server."""
288
+ if not isinstance(model, str) or not model.strip():
289
+ raise ValueError("an explicit model is required")
290
+ allowed = WRITER_TOOLS if role == "writer" else READER_TOOLS if role == "reader" else None
291
+ if allowed is None:
292
+ raise ValueError("role must be writer or reader")
293
+ cwd = Path(actor_cwd).resolve()
294
+ if not cwd.is_dir() or any(cwd.iterdir()):
295
+ raise ValueError("actor cwd must be a pre-created empty directory")
296
+ root, _ = _validate_actor_store(memory_path, role=role, mark_writer=False)
297
+ if cwd == root or cwd in root.parents or root in cwd.parents:
298
+ raise ValueError("actor cwd must be separate from the marked memory store")
299
+ host_paths = _validate_fresh_receipts([events_path, stderr_path, final_path])
300
+ for path in host_paths:
301
+ if path == cwd or cwd in path.parents:
302
+ raise ValueError("actor receipts must stay outside the actor cwd")
303
+ if str(path) in prompt or str(path).replace("\\", "/") in prompt:
304
+ raise ValueError("host receipt paths must not be included in the actor prompt")
305
+ _validate_actor_store(memory_path, role=role, mark_writer=True)
306
+ config = Path(memory_exe).resolve()
307
+ args = ["serve", "--memory-path", str(Path(memory_path).resolve()), "--local-owner",
308
+ "--access-mode", "trusted-local", "--lifecycle-v1"]
309
+ command = [str(Path(codex_exe)), "exec", "--ignore-user-config", "--ignore-rules",
310
+ "--strict-config", "--skip-git-repo-check", "--json", "--model", model,
311
+ "--sandbox", "read-only", "--cd", str(cwd), "--output-last-message", str(host_paths[2])]
312
+ if not retain_session:
313
+ command += ["--ephemeral"]
314
+ for feature in DISABLED_FEATURES:
315
+ command += ["--disable", feature]
316
+ # Explicitly authorized for the isolated actor: MCP dispatch uses this host.
317
+ # Shell, other servers, plugins and memory remain disabled independently.
318
+ command += ["--enable", "code_mode_host"]
319
+ command += ["-c", "project_doc_max_bytes=0", "-c", 'web_search="disabled"',
320
+ "-c", "features.code_mode.enabled=true",
321
+ "-c", 'features.code_mode.excluded_tool_namespaces=["functions","collaboration","clock"]',
322
+ "-c", 'model_reasoning_effort="medium"', "-c", "tool_output_token_limit=12000",
323
+ "-c", "mcp_servers={}",
324
+ "-c", f"mcp_servers.{SERVER}.command={json.dumps(str(config))}",
325
+ "-c", f"mcp_servers.{SERVER}.args={json.dumps(args)}",
326
+ "-c", f"mcp_servers.{SERVER}.enabled_tools={json.dumps(sorted(allowed))}",
327
+ "-c", f"mcp_servers.{SERVER}.required=true",
328
+ "-c", f'mcp_servers.{SERVER}.default_tools_approval_mode="approve"',
329
+ "-c", f"mcp_servers.{SERVER}.startup_timeout_sec=20",
330
+ "-c", f"mcp_servers.{SERVER}.tool_timeout_sec=60",
331
+ "-"]
332
+ return command
333
+
334
+
335
+ def check_codex_event(event: Any, allowed_tools: Iterable[str]) -> None:
336
+ if not isinstance(event, dict):
337
+ raise ActorIsolationError("Codex event is not an object")
338
+ if event.get("type") in {"thread.started", "turn.started", "turn.completed"}:
339
+ return
340
+ if event.get("type") not in {"item.started", "item.updated", "item.completed"}:
341
+ raise ActorIsolationError(f"Codex event rejected: {event.get('type')}")
342
+ item = event.get("item")
343
+ if not isinstance(item, dict):
344
+ raise ActorIsolationError("Codex item is not an object")
345
+ # This CLI emits its nonfatal experimental-feature notice as an error item.
346
+ # Preserve it in receipts; all other error items still invalidate the route.
347
+ if item.get("type") == "error" and str(item.get("message", "")).startswith(CODE_MODE_WARNING):
348
+ return
349
+ if item.get("type") in {"agent_message", "reasoning", "plan", "todo_list"}:
350
+ return
351
+ if (item.get("type") == "mcp_tool_call" and item.get("server") == SERVER
352
+ and item.get("tool") in set(allowed_tools)):
353
+ return
354
+ raise ActorIsolationError(f"Codex item rejected: {item.get('type')}")
355
+
356
+
357
+ async def _terminate_process_tree(proc: asyncio.subprocess.Process) -> None:
358
+ """Stop the owned CLI tree with bounded cleanup on the current platform."""
359
+ if proc.returncode is not None:
360
+ return
361
+ if os.name == "nt":
362
+ try:
363
+ killer = await asyncio.create_subprocess_exec(
364
+ "taskkill", "/PID", str(proc.pid), "/T", "/F",
365
+ stdout=asyncio.subprocess.DEVNULL, stderr=asyncio.subprocess.DEVNULL,
366
+ )
367
+ await asyncio.wait_for(killer.wait(), timeout=5)
368
+ except (asyncio.TimeoutError, OSError):
369
+ pass
370
+ else:
371
+ try:
372
+ os.killpg(proc.pid, signal.SIGKILL)
373
+ except ProcessLookupError:
374
+ pass
375
+ if proc.returncode is None:
376
+ try:
377
+ proc.kill()
378
+ except ProcessLookupError:
379
+ pass
380
+
381
+
382
+ async def run_codex_actor(codex_exe: str | Path, memory_exe: str | Path, *,
383
+ memory_path: str | Path, actor_cwd: str | Path,
384
+ model: str, role: str, prompt: str, timeout_seconds: float,
385
+ events_path: str | Path, stderr_path: str | Path,
386
+ final_path: str | Path, retain_session: bool = False) -> dict[str, Any]:
387
+ """Run one actor, applying the caller's deadline and retaining raw receipts."""
388
+ if (isinstance(timeout_seconds, bool) or not isinstance(timeout_seconds, (int, float))
389
+ or not math.isfinite(timeout_seconds) or timeout_seconds <= 0):
390
+ raise ValueError("timeout_seconds must be finite and positive")
391
+ allowed = WRITER_TOOLS if role == "writer" else READER_TOOLS if role == "reader" else None
392
+ if allowed is None:
393
+ raise ValueError("role must be writer or reader")
394
+ command = codex_exec_command(
395
+ codex_exe, memory_exe, memory_path=memory_path, actor_cwd=actor_cwd,
396
+ model=model, role=role, prompt=prompt, events_path=events_path,
397
+ stderr_path=stderr_path, final_path=final_path, retain_session=retain_session,
398
+ )
399
+ proc = await asyncio.create_subprocess_exec(
400
+ *command, cwd=str(Path(actor_cwd).resolve()),
401
+ stdin=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE,
402
+ stderr=asyncio.subprocess.PIPE,
403
+ **({"start_new_session": True} if os.name != "nt" else {}),
404
+ )
405
+ stdout_chunks: list[bytes] = []
406
+ stderr_chunks: list[bytes] = []
407
+
408
+ async def drain(reader: asyncio.StreamReader, chunks: list[bytes]) -> None:
409
+ while chunk := await reader.read(65536):
410
+ chunks.append(chunk)
411
+
412
+ async def send_prompt() -> None:
413
+ assert proc.stdin is not None
414
+ proc.stdin.write(prompt.encode("utf-8"))
415
+ await proc.stdin.drain()
416
+ proc.stdin.close()
417
+ await proc.stdin.wait_closed()
418
+
419
+ stdout_task = asyncio.create_task(drain(proc.stdout, stdout_chunks))
420
+ stderr_task = asyncio.create_task(drain(proc.stderr, stderr_chunks))
421
+ prompt_task = asyncio.create_task(send_prompt())
422
+
423
+ async def collect() -> tuple[bytes, bytes]:
424
+ await prompt_task
425
+ await proc.wait()
426
+ await asyncio.gather(stdout_task, stderr_task)
427
+ return b"".join(stdout_chunks), b"".join(stderr_chunks)
428
+
429
+ collect_task = asyncio.create_task(collect())
430
+ started = time.monotonic()
431
+ try:
432
+ stdout, stderr = await asyncio.wait_for(asyncio.shield(collect_task), timeout=timeout_seconds)
433
+ except asyncio.TimeoutError:
434
+ await _terminate_process_tree(proc)
435
+ try:
436
+ stdout, stderr = await asyncio.wait_for(asyncio.shield(collect_task), timeout=5)
437
+ except asyncio.TimeoutError:
438
+ collect_task.cancel()
439
+ prompt_task.cancel()
440
+ stdout_task.cancel()
441
+ stderr_task.cancel()
442
+ await asyncio.gather(collect_task, prompt_task, stdout_task, stderr_task,
443
+ return_exceptions=True)
444
+ stdout, stderr = b"".join(stdout_chunks), b"".join(stderr_chunks)
445
+ Path(events_path).write_bytes(stdout)
446
+ Path(stderr_path).write_bytes(stderr)
447
+ raise TimeoutError(f"Codex actor exceeded caller deadline ({timeout_seconds}s)") from None
448
+ except Exception:
449
+ await _terminate_process_tree(proc)
450
+ try:
451
+ await asyncio.wait_for(asyncio.shield(collect_task), timeout=5)
452
+ except Exception:
453
+ collect_task.cancel()
454
+ prompt_task.cancel()
455
+ stdout_task.cancel()
456
+ stderr_task.cancel()
457
+ await asyncio.gather(collect_task, prompt_task, stdout_task, stderr_task,
458
+ return_exceptions=True)
459
+ Path(events_path).write_bytes(b"".join(stdout_chunks))
460
+ Path(stderr_path).write_bytes(b"".join(stderr_chunks))
461
+ raise
462
+ Path(events_path).write_bytes(stdout)
463
+ Path(stderr_path).write_bytes(stderr)
464
+ allowed_tools = WRITER_TOOLS if role == "writer" else READER_TOOLS
465
+ events = []
466
+ for raw in stdout.splitlines():
467
+ if not raw.strip():
468
+ continue
469
+ event = json.loads(raw.decode("utf-8"))
470
+ check_codex_event(event, allowed_tools)
471
+ events.append(event)
472
+ if proc.returncode != 0:
473
+ raise RuntimeError(f"Codex actor exited with code {proc.returncode}; raw receipts preserved")
474
+ completed = [item for item in events if item.get("type") == "turn.completed"]
475
+ if not completed:
476
+ raise RuntimeError("Codex actor emitted no turn.completed event; raw receipts preserved")
477
+ final_file = Path(final_path)
478
+ if not final_file.is_file() or not final_file.read_text(encoding="utf-8").strip():
479
+ raise RuntimeError("Codex final-message receipt is missing or empty; raw receipts preserved")
480
+ audit = None
481
+ if retain_session:
482
+ audit = audit_retained_session(
483
+ events, allowed_tools, Path.home() / ".codex" / "sessions",
484
+ Path(events_path).with_suffix(".native-tools.jsonl"),
485
+ deadline=started + timeout_seconds,
486
+ )
487
+ return {"status": "PASS", "model": model, "role": role,
488
+ "native_trace_audit": audit,
489
+ "elapsed_seconds": round(time.monotonic() - started, 3),
490
+ "returncode": proc.returncode, "events_path": str(Path(events_path).resolve()),
491
+ "stderr_path": str(Path(stderr_path).resolve()),
492
+ "final_path": str(final_file.resolve()),
493
+ "usage_events": [item.get("usage") for item in completed],
494
+ "startup_warnings": [item["item"]["message"] for item in events
495
+ if item.get("type") == "item.completed"
496
+ and item.get("item", {}).get("type") == "error"],
497
+ "final_message": final_file.read_text(encoding="utf-8")}