File size: 7,012 Bytes
5e5d6c5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | from __future__ import annotations
import hashlib
from pathlib import Path
import networkx as nx
from ctx.adapters.claude_code.engine_hook import render_recommendation_hook
from ctx.adapters.codex.engine_adapter import render_recommendation_context
from ctx.core.resolve.engine_candidates import GraphCandidateSource
from ctx.engine.engine import CtxEngine
from ctx.engine.planner import (
BoundedCapabilityPlanner,
ReplayDecisionPlanner,
)
from ctx.engine.protocol import EngineEvent, ScopeRef
from ctx.engine.reducer import PLANNING_REDUCER_VERSION
from ctx.engine.replay import (
DefaultReplayInputFactory,
ObservationReference,
StructuredSurrogate,
)
from ctx.engine.state import EngineState
from ctx.engine.store import SQLiteEngineStore
NOW = "2026-08-01T12:00:00Z"
def _digest(value: str) -> str:
return hashlib.sha256(value.encode()).hexdigest()
def _scope() -> ScopeRef:
return ScopeRef(
tenant_id="tenant-1",
workspace_id="workspace-1",
repository_id="repository-1",
session_id="session-1",
exposure_id="exposure-1",
host_context_id="host-1",
)
def _event(
kind: str,
revision: int,
event_id: str,
catalog_snapshot_digest: str,
*,
payload: dict[str, object] | None = None,
) -> EngineEvent:
return EngineEvent(
event_id=event_id,
kind=kind,
scope=_scope(),
expected_revision=revision,
occurred_at=NOW,
payload=payload or {},
engine_version="engine-v1",
planner_version="planner-v1",
policy_version="policy-v1",
host_descriptor_digest=_digest("host"),
catalog_snapshot_digest=catalog_snapshot_digest,
semantic_model_digest=_digest("model"),
semantic_index_digest=_digest("index"),
work_signature=_digest("work"),
random_seed=17,
)
def _graph() -> nx.Graph:
graph = nx.Graph()
for node_id, kind in (
("skill:python-tdd", "skill"),
("agent:python-reviewer", "agent"),
("mcp-server:python-docs", "mcp-server"),
("harness:python-runner", "harness"),
("skill:python-security", "skill"),
("skill:python-lint", "skill"),
("skill:python-types", "skill"),
):
graph.add_node(
node_id,
label=node_id.split(":", 1)[1],
type=kind,
tags=["python"],
)
return graph
def _normalize(
_reference: ObservationReference,
_state: EngineState | None,
) -> StructuredSurrogate:
return StructuredSurrogate.create(
schema_id="ctx.observation.current-work",
schema_version=1,
value={
"signals": ["python"],
"languages": ["python"],
"baseline_capability_ids": [],
"active_capability_ids": [],
"rejected_capability_ids": [],
"requested_limit": 5,
},
)
def test_graph_engine_and_both_host_renderers_share_one_exact_bundle(
tmp_path: Path,
) -> None:
source = GraphCandidateSource(_graph())
planner = ReplayDecisionPlanner(
BoundedCapabilityPlanner(source),
planner_version="planner-v1",
)
engine = CtxEngine(
store=SQLiteEngineStore(tmp_path / "engine.sqlite3"),
replay_factory=DefaultReplayInputFactory(
observation_normalizer=_normalize,
decision_planner=planner,
reducer_version=PLANNING_REDUCER_VERSION,
),
)
engine.process(
_event(
"SessionStarted",
0,
"event-start",
source.catalog_snapshot_digest,
)
)
transition = engine.process(
_event(
"IntentObserved",
1,
"event-intent",
source.catalog_snapshot_digest,
payload={
"observation_ref": {
"provider_id": "host-buffer",
"opaque_id": "observation-1",
"content_digest": _digest("python-work"),
}
},
)
)
capabilities = transition.actions[0].payload["capabilities"]
assert [row["capability_id"] for row in capabilities] == [
"agent:python-reviewer",
"harness:python-runner",
"mcp-server:python-docs",
"skill:python-lint",
"skill:python-security",
]
codex_context = render_recommendation_context(transition)
claude_envelope = render_recommendation_hook(transition)
assert codex_context is not None
assert claude_envelope is not None
assert claude_envelope["hookSpecificOutput"]["additionalContext"] == codex_context
def test_every_committed_graph_bundle_is_renderer_ready_at_evidence_boundary(
tmp_path: Path,
) -> None:
long_signals = tuple(sorted(f"signal-{index:02d}-" + "x" * 109 for index in range(28)))
graph = nx.Graph()
for index in range(3):
graph.add_node(
f"skill:boundary-{index}",
label=f"boundary-{index}",
type="skill",
tags=list(long_signals),
)
source = GraphCandidateSource(graph)
planner = ReplayDecisionPlanner(
BoundedCapabilityPlanner(source),
planner_version="planner-v1",
)
def normalize_boundary(
_reference: ObservationReference,
_state: EngineState | None,
) -> StructuredSurrogate:
return StructuredSurrogate.create(
schema_id="ctx.observation.current-work",
schema_version=1,
value={
"signals": list(long_signals),
"languages": [],
"baseline_capability_ids": [],
"active_capability_ids": [],
"rejected_capability_ids": [],
"requested_limit": 5,
},
)
engine = CtxEngine(
store=SQLiteEngineStore(tmp_path / "engine-boundary.sqlite3"),
replay_factory=DefaultReplayInputFactory(
observation_normalizer=normalize_boundary,
decision_planner=planner,
reducer_version=PLANNING_REDUCER_VERSION,
),
)
engine.process(
_event("SessionStarted", 0, "event-boundary-start", source.catalog_snapshot_digest)
)
transition = engine.process(
_event(
"IntentObserved",
1,
"event-boundary-intent",
source.catalog_snapshot_digest,
payload={
"observation_ref": {
"provider_id": "host-buffer",
"opaque_id": "observation-boundary",
"content_digest": _digest("boundary-work"),
}
},
)
)
codex_context = render_recommendation_context(transition)
claude_envelope = render_recommendation_hook(transition)
assert codex_context is not None
assert claude_envelope is not None
assert claude_envelope["hookSpecificOutput"]["additionalContext"] == codex_context
|