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.
- benchmarks/__init__.py +3 -0
- benchmarks/adapters/__init__.py +29 -0
- benchmarks/adapters/ablations.py +406 -0
- benchmarks/adapters/base.py +72 -0
- benchmarks/adapters/controlled/__init__.py +13 -0
- benchmarks/adapters/controlled/core_archival.py +115 -0
- benchmarks/adapters/controlled/extract_resolve.py +130 -0
- benchmarks/adapters/controlled/hybrid_rag.py +120 -0
- benchmarks/adapters/controlled/no_late_binding.py +113 -0
- benchmarks/adapters/dandelion.py +94 -0
- benchmarks/adapters/native/__init__.py +31 -0
- benchmarks/adapters/native/base.py +45 -0
- benchmarks/adapters/native/dandelion_native.py +164 -0
- benchmarks/adapters/native/dandelion_native_b0.py +165 -0
- benchmarks/adapters/native/dandelion_native_b1.py +170 -0
- benchmarks/adapters/native/dandelion_native_b2.py +169 -0
- benchmarks/adapters/native/dandelion_native_b3.py +171 -0
- benchmarks/adapters/native/dandelion_native_c4.py +261 -0
- benchmarks/adapters/native/dandelion_native_c4_1.py +281 -0
- benchmarks/adapters/native/dandelion_native_c4_2.py +435 -0
- benchmarks/adapters/native/event_evidence_b2.py +616 -0
- benchmarks/adapters/native/evidence_b2.py +609 -0
- benchmarks/adapters/native/letta_native.py +253 -0
- benchmarks/adapters/native/mem0_native.py +219 -0
- benchmarks/adapters/native/store_vector_b0.py +792 -0
- benchmarks/adapters/native/store_vector_b2.py +808 -0
- benchmarks/agent_adapter_stdio_acceptance.py +86 -0
- benchmarks/bootstrap_cross_stack.py +218 -0
- benchmarks/canonical_profile_instrumentation.py +167 -0
- benchmarks/canonical_public_baseline.py +288 -0
- benchmarks/cli.py +525 -0
- benchmarks/collector_public30_offline.py +1653 -0
- benchmarks/datasets/__init__.py +45 -0
- benchmarks/datasets/conversational_generator.py +238 -0
- benchmarks/datasets/generator.py +283 -0
- benchmarks/datasets/holdout_topology_generator.py +290 -0
- benchmarks/datasets/holdout_topology_generator_v2.py +294 -0
- benchmarks/datasets/multi_world.py +1643 -0
- benchmarks/datasets/scale_generator.py +289 -0
- benchmarks/datasets/schema.py +57 -0
- benchmarks/deep_collision_stress.py +199 -0
- benchmarks/evaluators/__init__.py +12 -0
- benchmarks/evaluators/manifest.py +142 -0
- benchmarks/evaluators/metrics.py +277 -0
- benchmarks/evaluators/runner.py +258 -0
- benchmarks/f1_step0/build_dev_measurement_manifest_v1.py +127 -0
- benchmarks/f1_step0/build_dev_measurement_manifest_v2.py +127 -0
- benchmarks/f1_step0/build_dev_measurement_manifest_v3.py +146 -0
- benchmarks/f1_step0/build_ledger.py +143 -0
- benchmarks/f1_step0/build_ledger_v2.py +296 -0
- benchmarks/f1_step0/compare_dev52_readiness_v1.py +106 -0
- benchmarks/f1_step0/locator_public_smoke.py +172 -0
- benchmarks/f1_step0/prepare_dev52_owner_v1.py +133 -0
- benchmarks/f1_step0/prepare_dev52_owner_v2.py +133 -0
- benchmarks/f1_step0/public_identity_probe.py +191 -0
- benchmarks/f1_step0/public_response_mapper.py +101 -0
- benchmarks/f1_step0/real_source_public_preflight.py +244 -0
- benchmarks/f1_step0/run_exact_fusion_dev_experiment_v1.py +83 -0
- benchmarks/f1_step0/scorer.py +110 -0
- benchmarks/f1_step0/source_aware_dev_measurement_v1.py +627 -0
- benchmarks/f1_step0/source_aware_dev_measurement_v2.py +659 -0
- benchmarks/f1_step0/source_aware_dev_measurement_v3.py +776 -0
- benchmarks/f1_step0/test_public_response_mapper.py +110 -0
- benchmarks/f1_step0/test_real_source_public_preflight.py +25 -0
- benchmarks/f1_step0/test_scorer.py +158 -0
- benchmarks/f1_step0/test_source_aware_dev_measurement.py +189 -0
- benchmarks/f1_step0/test_source_aware_dev_measurement_v2.py +263 -0
- benchmarks/f1_step0/test_source_aware_dev_measurement_v3.py +334 -0
- benchmarks/f1_step0/typed_readback_public_smoke.py +181 -0
- benchmarks/forensic_c4_1_dev.py +258 -0
- benchmarks/forensic_c4_dev.py +255 -0
- benchmarks/forensic_deep25_c4_1.py +478 -0
- benchmarks/legacy_migration_acceptance.py +29 -0
- benchmarks/longmemeval_actor.py +497 -0
- benchmarks/longmemeval_agent_judge.py +593 -0
- benchmarks/longmemeval_budget.py +139 -0
- benchmarks/longmemeval_full.py +410 -0
- benchmarks/longmemeval_judge.py +327 -0
- benchmarks/longmemeval_pilot.py +316 -0
- benchmarks/longmemeval_s.py +318 -0
- benchmarks/memory_bench.py +248 -0
- benchmarks/memory_formation/__init__.py +1 -0
- benchmarks/memory_formation/audit.py +801 -0
- benchmarks/memory_formation/real_audit.py +688 -0
- benchmarks/memory_session_acceptance.py +279 -0
- benchmarks/native_codex_host.py +513 -0
- benchmarks/owner_dense_acceptance.py +126 -0
- benchmarks/pilot_seeker_vault.py +266 -0
- benchmarks/prepare_public30_ripgrep_range_owner.py +115 -0
- benchmarks/profile_canonical.py +373 -0
- benchmarks/profile_canonical_lanes.py +405 -0
- benchmarks/profile_search_chunks.py +351 -0
- benchmarks/public_ranges_faq_proof.py +121 -0
- benchmarks/public_repos/build_history_scope_v3.py +330 -0
- benchmarks/public_repos/build_oracle.py +108 -0
- benchmarks/public_repos/build_oracle_v2.py +169 -0
- benchmarks/public_repos/build_oracle_v3.py +176 -0
- benchmarks/public_repos/map_public_v2.py +296 -0
- benchmarks/public_repos/project_prompts_v2.py +31 -0
- benchmarks/public_repos/project_prompts_v3.py +29 -0
- benchmarks/public_repos/ready_admin_v2.py +214 -0
- benchmarks/public_repos/score_public_v2.py +258 -0
- benchmarks/public_repos/test_map_public_v2.py +171 -0
- benchmarks/public_repos/test_public_repo_v2.py +44 -0
- benchmarks/public_repos/test_public_repo_v3.py +46 -0
- benchmarks/public_repos/test_ready_admin_v2.py +117 -0
- benchmarks/public_repos/test_score_public_v2.py +163 -0
- benchmarks/public_repos/test_wire_v2.py +292 -0
- benchmarks/public_repos/wire_v2.py +603 -0
- benchmarks/real_blocker_fix_acceptance.py +155 -0
- benchmarks/real_scan_rescan_acceptance.py +41 -0
- benchmarks/recall_v2_20k_smoke.py +112 -0
- benchmarks/recall_v2_e1_retrieve.py +70 -0
- benchmarks/recall_v2_e1_score.py +271 -0
- benchmarks/recall_v2_e2_prepare.py +69 -0
- benchmarks/recall_v2_e2_public_score.py +91 -0
- benchmarks/recall_v2_e2_score.py +108 -0
- benchmarks/recall_v2_navigation_mcp_replay.py +308 -0
- benchmarks/recall_v2_reader_capture.py +118 -0
- benchmarks/recall_v2_reader_replay.py +136 -0
- benchmarks/run_blind_h10_holdout.py +284 -0
- benchmarks/run_blind_h9_holdout.py +275 -0
- benchmarks/run_c4_2_nightly_scale.py +446 -0
- benchmarks/run_context_scale_simulation.py +189 -0
- benchmarks/run_conversational_bench.py +170 -0
- benchmarks/run_h11_evaluation.py +316 -0
- benchmarks/run_h11_v2_evaluation.py +431 -0
- benchmarks/run_native_fixed12.py +469 -0
- benchmarks/run_native_preflight.py +280 -0
- benchmarks/run_scale_stress.py +225 -0
- benchmarks/solanazation_brownfield_01/build_oracle.py +970 -0
- benchmarks/solanazation_brownfield_01/check_level_a.py +78 -0
- benchmarks/solanazation_brownfield_01/debug_scope.py +13 -0
- benchmarks/solanazation_brownfield_01/evaluate_bootstrap_02.py +570 -0
- benchmarks/solanazation_brownfield_01/evaluate_first_pass.py +306 -0
- benchmarks/solanazation_brownfield_01/generate_dryrun_report.py +222 -0
- benchmarks/solanazation_brownfield_01/generate_manifests.py +146 -0
- benchmarks/solanazation_brownfield_01/inspect_misses.py +29 -0
- benchmarks/solanazation_brownfield_01/run_miss_forensic.py +206 -0
- benchmarks/solanazation_brownfield_01/test_facts.py +15 -0
- benchmarks/statistics/__init__.py +15 -0
- benchmarks/statistics/significance.py +326 -0
- benchmarks/test_native_mcp_preflight.py +229 -0
- benchmarks/test_prepare_public30_range_owner.py +35 -0
- benchmarks/test_public30_source_reference.py +698 -0
- benchmarks/test_recall_v2_e1_reader_input.py +89 -0
- benchmarks/test_recall_v2_navigation_mcp_replay.py +155 -0
- benchmarks/test_recall_v2_reader_replay.py +41 -0
- dandelion/__init__.py +3 -0
- dandelion/__main__.py +11 -0
- dandelion/agent_adapter.py +2369 -0
- dandelion/bootstrap/__init__.py +872 -0
- dandelion/bootstrap/alias_policy.py +50 -0
- dandelion/bootstrap/authority.py +225 -0
- dandelion/bootstrap/canonical_history.py +412 -0
- dandelion/bootstrap/classifier.py +403 -0
- dandelion/bootstrap/component_graph.py +280 -0
- dandelion/bootstrap/discovery.py +279 -0
- dandelion/bootstrap/domain_induction.py +180 -0
- dandelion/bootstrap/domain_topology.py +673 -0
- dandelion/bootstrap/evidence_envelope.py +132 -0
- dandelion/bootstrap/extractors/base.py +171 -0
- dandelion/bootstrap/extractors/exact.py +1234 -0
- dandelion/bootstrap/extractors/generic.py +100 -0
- dandelion/bootstrap/extractors/git_temporal.py +111 -0
- dandelion/bootstrap/extractors/registry.py +345 -0
- dandelion/bootstrap/extractors/relations.py +89 -0
- dandelion/bootstrap/extractors/semantic.py +424 -0
- dandelion/bootstrap/extractors/status.py +218 -0
- dandelion/bootstrap/git_sweeper.py +135 -0
- dandelion/bootstrap/manifest.py +246 -0
- dandelion/bootstrap/quarantine.py +264 -0
- dandelion/bootstrap/report.py +259 -0
- dandelion/bootstrap/root_resolver.py +221 -0
- dandelion/bootstrap/tech_profiler.py +125 -0
- dandelion/bootstrap/topology.py +75 -0
- dandelion/bootstrap/topology_index.py +110 -0
- dandelion/bootstrap/transaction.py +315 -0
- dandelion/canonical_bootstrap.py +257 -0
- dandelion/canonical_memory.py +4130 -0
- dandelion/captured_input.py +295 -0
- dandelion/cli.py +767 -0
- dandelion/config.py +40 -0
- dandelion/derived_index.py +745 -0
- dandelion/domains.py +706 -0
- dandelion/embedder.py +229 -0
- dandelion/event_evidence.py +571 -0
- dandelion/evidence.py +812 -0
- dandelion/evidence_response.py +453 -0
- dandelion/evidence_workspace.py +2360 -0
- dandelion/exact_context.py +129 -0
- dandelion/hydrator.py +124 -0
- dandelion/inspector.py +442 -0
- dandelion/inspector_assets/dandelion-mark.svg +33 -0
- dandelion/inspector_assets/dandelion_graph.js +361 -0
- dandelion/inspector_assets/inspector.css +223 -0
- dandelion/inspector_assets/inspector.html +135 -0
- dandelion/inspector_assets/inspector.js +1059 -0
- dandelion/legacy_import.py +431 -0
- dandelion/live_source_grants.py +133 -0
- dandelion/local_owner.py +114 -0
- dandelion/memory_client.py +252 -0
- dandelion/memory_contract.py +107 -0
- dandelion/memory_owner.py +560 -0
- dandelion/reader_providers.py +118 -0
- dandelion/recall_v2.py +767 -0
- dandelion/reconciler.py +219 -0
- dandelion/request_budget.py +125 -0
- dandelion/retained_basis.py +223 -0
- dandelion/retrieval.py +485 -0
- dandelion/router.py +405 -0
- dandelion/search_terms.py +53 -0
- dandelion/security.py +124 -0
- dandelion/server.py +810 -0
- dandelion/source_content_fts.py +219 -0
- dandelion/source_delivery.py +89 -0
- dandelion/source_history.py +241 -0
- dandelion/source_identity.py +88 -0
- dandelion/source_navigator.py +5 -0
- dandelion/source_reader.py +444 -0
- dandelion/source_references.py +379 -0
- dandelion/source_sections.py +265 -0
- dandelion/store_kv.py +684 -0
- dandelion/store_vector.py +1289 -0
- dandelion/studio.py +325 -0
- dandelion/wal.py +227 -0
- dandelion_memory-1.0.0rc15.dist-info/METADATA +48 -0
- dandelion_memory-1.0.0rc15.dist-info/RECORD +232 -0
- dandelion_memory-1.0.0rc15.dist-info/WHEEL +5 -0
- dandelion_memory-1.0.0rc15.dist-info/entry_points.txt +5 -0
- dandelion_memory-1.0.0rc15.dist-info/licenses/LICENSE +21 -0
- 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")}
|