From e2a8b4799c72017790d401d782eb51988309a516 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Mon, 18 May 2026 05:42:18 +0100 Subject: [PATCH] Read graph query from memory graph store --- epiphany-codex-bridge/src/view.rs | 9 + epiphany-core/src/lib.rs | 1 + epiphany-core/src/memory_graph/mod.rs | 1 + .../src/memory_graph/profiles/mod.rs | 1 + .../src/memory_graph/profiles/repo.rs | 182 ++++++++++++++++++ notes/epiphany-memory-graph-unified-plan.md | 3 + notes/fresh-workspace-handoff.md | 2 +- state/ledgers.msgpack | Bin 140746 -> 141000 bytes state/map.yaml | 4 +- .../epiphany_read_routes.rs | 29 ++- 10 files changed, 227 insertions(+), 5 deletions(-) diff --git a/epiphany-codex-bridge/src/view.rs b/epiphany-codex-bridge/src/view.rs index b3ee2347..a2ae088c 100644 --- a/epiphany-codex-bridge/src/view.rs +++ b/epiphany-codex-bridge/src/view.rs @@ -6,9 +6,11 @@ use codex_protocol::protocol::EpiphanyThreadState; use codex_protocol::protocol::TokenUsageInfo as CoreTokenUsageInfo; use epiphany_core::EpiphanyDistillInput; use epiphany_core::EpiphanyMapProposalInput; +use epiphany_core::EpiphanyMemoryGraphSnapshot; use epiphany_core::EpiphanySceneInput; use epiphany_core::derive_scene; use epiphany_core::distill_observation; +use epiphany_core::epiphany_graphs_from_memory_graph; use epiphany_core::propose_map_update; use crate::context::map_epiphany_context; @@ -124,8 +126,15 @@ pub fn map_epiphany_graph_query_response( thread_id: String, loaded: bool, state: Option<&EpiphanyThreadState>, + memory_graph_snapshot: Option<&EpiphanyMemoryGraphSnapshot>, query: &ThreadEpiphanyGraphQuery, ) -> ThreadEpiphanyGraphQueryResponse { + let memory_backed_state = state.zip(memory_graph_snapshot).map(|(state, snapshot)| { + let mut state = state.clone(); + state.graphs = epiphany_graphs_from_memory_graph(snapshot); + state + }); + let state = memory_backed_state.as_ref().or(state); let (state_status, state_revision, graph, frontier, checkpoint, matched, missing) = map_epiphany_graph_query(state, query); ThreadEpiphanyGraphQueryResponse { diff --git a/epiphany-core/src/lib.rs b/epiphany-core/src/lib.rs index f4887577..868156f8 100644 --- a/epiphany-core/src/lib.rs +++ b/epiphany-core/src/lib.rs @@ -109,6 +109,7 @@ pub use memory_graph::MEMORY_GRAPH_TYPE; pub use memory_graph::apply_memory_patch_candidate; pub use memory_graph::compose_memory_graph_snapshots; pub use memory_graph::derive_memory_graph_freshness; +pub use memory_graph::epiphany_graphs_from_memory_graph; pub use memory_graph::lifecycle_allowed_for_profile; pub use memory_graph::load_memory_graph_entry; pub use memory_graph::load_memory_graph_snapshot; diff --git a/epiphany-core/src/memory_graph/mod.rs b/epiphany-core/src/memory_graph/mod.rs index 446af3b2..9716de89 100644 --- a/epiphany-core/src/memory_graph/mod.rs +++ b/epiphany-core/src/memory_graph/mod.rs @@ -42,6 +42,7 @@ pub use ids::memory_graph_edge_id; pub use ids::memory_graph_node_id; pub use patch::apply_memory_patch_candidate; pub use patch::review_memory_patch_candidate; +pub use profiles::epiphany_graphs_from_memory_graph; pub use profiles::memory_graph_from_agent_memories; pub use profiles::memory_graph_from_epiphany_graphs; pub use profiles::memory_graph_from_heartbeat_cognition; diff --git a/epiphany-core/src/memory_graph/profiles/mod.rs b/epiphany-core/src/memory_graph/profiles/mod.rs index be54c9e2..4a79bbe0 100644 --- a/epiphany-core/src/memory_graph/profiles/mod.rs +++ b/epiphany-core/src/memory_graph/profiles/mod.rs @@ -4,4 +4,5 @@ pub mod repo; pub use agent::memory_graph_from_agent_memories; pub use heartbeat::memory_graph_from_heartbeat_cognition; +pub use repo::epiphany_graphs_from_memory_graph; pub use repo::memory_graph_from_epiphany_graphs; diff --git a/epiphany-core/src/memory_graph/profiles/repo.rs b/epiphany-core/src/memory_graph/profiles/repo.rs index 28764ddf..392b4468 100644 --- a/epiphany-core/src/memory_graph/profiles/repo.rs +++ b/epiphany-core/src/memory_graph/profiles/repo.rs @@ -120,6 +120,151 @@ pub fn memory_graph_from_epiphany_graphs( } } +pub fn epiphany_graphs_from_memory_graph(snapshot: &EpiphanyMemoryGraphSnapshot) -> EpiphanyGraphs { + let architecture = EpiphanyGraph { + nodes: snapshot + .nodes + .iter() + .filter(|node| node.profile == EpiphanyMemoryProfile::RepoArchitecture) + .map(graph_node_from_memory_node) + .collect(), + edges: snapshot + .edges + .iter() + .filter(|edge| edge.profile == EpiphanyMemoryProfile::RepoArchitecture) + .map(graph_edge_from_memory_edge) + .collect(), + }; + let dataflow = EpiphanyGraph { + nodes: snapshot + .nodes + .iter() + .filter(|node| node.profile == EpiphanyMemoryProfile::RepoDataflow) + .map(graph_node_from_memory_node) + .collect(), + edges: snapshot + .edges + .iter() + .filter(|edge| edge.profile == EpiphanyMemoryProfile::RepoDataflow) + .map(graph_edge_from_memory_edge) + .collect(), + }; + EpiphanyGraphs { + architecture, + dataflow, + links: repo_graph_links_from_memory_graph(snapshot), + } +} + +fn graph_node_from_memory_node(node: &EpiphanyMemoryNode) -> EpiphanyGraphNode { + EpiphanyGraphNode { + id: node.id.clone(), + title: node.title.clone(), + purpose: node.claim.clone(), + mechanism: (!node.tension.trim().is_empty()).then_some(node.tension.clone()), + metaphor: None, + status: Some(memory_lifecycle_label(node.lifecycle).to_string()), + code_refs: code_refs_from_anchors(&node.anchors), + } +} + +fn graph_edge_from_memory_edge(edge: &EpiphanyMemoryEdge) -> EpiphanyGraphEdge { + EpiphanyGraphEdge { + source_id: edge.source_id.clone(), + target_id: edge.target_id.clone(), + kind: memory_edge_kind_label(edge.kind).to_string(), + id: Some(edge.id.clone()), + label: Some(edge.claim.clone()), + mechanism: None, + code_refs: code_refs_from_anchors(&edge.anchors), + } +} + +fn repo_graph_links_from_memory_graph( + snapshot: &EpiphanyMemoryGraphSnapshot, +) -> Vec { + snapshot + .edges + .iter() + .filter(|edge| edge.profile == EpiphanyMemoryProfile::RepoDataflow) + .filter_map(|edge| { + let architecture_node_id = edge + .anchors + .iter() + .find(|anchor| anchor.kind == "architecture_node") + .map(|anchor| anchor.target.clone())?; + Some(EpiphanyGraphLink { + dataflow_node_id: edge.source_id.clone(), + architecture_node_id, + relationship: Some(edge.claim.clone()), + code_refs: code_refs_from_anchors(&edge.anchors), + }) + }) + .collect() +} + +fn code_refs_from_anchors(anchors: &[EpiphanyMemoryAnchor]) -> Vec { + anchors + .iter() + .filter_map(|anchor| anchor.code_ref.clone()) + .collect() +} + +fn memory_edge_kind_label(kind: EpiphanyMemoryEdgeKind) -> &'static str { + match kind { + EpiphanyMemoryEdgeKind::Owns => "owns", + EpiphanyMemoryEdgeKind::Reads => "reads", + EpiphanyMemoryEdgeKind::Writes => "writes", + EpiphanyMemoryEdgeKind::Derives => "derives", + EpiphanyMemoryEdgeKind::Adapts => "adapts", + EpiphanyMemoryEdgeKind::Persists => "persists", + EpiphanyMemoryEdgeKind::Launches => "launches", + EpiphanyMemoryEdgeKind::Verifies => "verifies", + EpiphanyMemoryEdgeKind::Supports => "supports", + EpiphanyMemoryEdgeKind::Contradicts => "contradicts", + EpiphanyMemoryEdgeKind::Distills => "distills", + EpiphanyMemoryEdgeKind::Revises => "revises", + EpiphanyMemoryEdgeKind::Retires => "retires", + EpiphanyMemoryEdgeKind::Grounds => "grounds", + EpiphanyMemoryEdgeKind::Triggers => "triggers", + EpiphanyMemoryEdgeKind::SpokenAs => "spoken_as", + EpiphanyMemoryEdgeKind::Cools => "cools", + EpiphanyMemoryEdgeKind::ClustersWith => "clusters_with", + EpiphanyMemoryEdgeKind::ResonatesWith => "resonates_with", + EpiphanyMemoryEdgeKind::DependsOn => "depends_on", + EpiphanyMemoryEdgeKind::Other => "other", + } +} + +fn memory_lifecycle_label(lifecycle: EpiphanyMemoryLifecycle) -> &'static str { + match lifecycle { + EpiphanyMemoryLifecycle::Observed => "observed", + EpiphanyMemoryLifecycle::Proposed => "proposed", + EpiphanyMemoryLifecycle::Accepted => "accepted", + EpiphanyMemoryLifecycle::Active => "active", + EpiphanyMemoryLifecycle::Clustered => "clustered", + EpiphanyMemoryLifecycle::Distilled => "distilled", + EpiphanyMemoryLifecycle::Incubated => "incubated", + EpiphanyMemoryLifecycle::Pruned => "pruned", + EpiphanyMemoryLifecycle::Revised => "revised", + EpiphanyMemoryLifecycle::Retired => "retired", + EpiphanyMemoryLifecycle::Crystallized => "crystallized", + EpiphanyMemoryLifecycle::Stale => "stale", + EpiphanyMemoryLifecycle::Deepening => "deepening", + EpiphanyMemoryLifecycle::Cooling => "cooling", + EpiphanyMemoryLifecycle::Promoted => "promoted", + EpiphanyMemoryLifecycle::Queued => "queued", + EpiphanyMemoryLifecycle::Deferred => "deferred", + EpiphanyMemoryLifecycle::Spoken => "spoken", + EpiphanyMemoryLifecycle::Applied => "applied", + EpiphanyMemoryLifecycle::Obligated => "obligated", + EpiphanyMemoryLifecycle::Answered => "answered", + EpiphanyMemoryLifecycle::Reviewed => "reviewed", + EpiphanyMemoryLifecycle::Contradicted => "contradicted", + EpiphanyMemoryLifecycle::Superseded => "superseded", + } +} + fn repo_domain(id: String, profile: EpiphanyMemoryProfile, title: &str) -> EpiphanyMemoryDomain { EpiphanyMemoryDomain { id, @@ -443,6 +588,43 @@ mod tests { ); } + #[test] + fn repo_profile_projects_memory_graph_back_to_legacy_graph_view() { + let graphs = EpiphanyGraphs { + architecture: EpiphanyGraph { + nodes: vec![EpiphanyGraphNode { + id: "core".to_string(), + title: "Core policy".to_string(), + purpose: "Owns shared policy.".to_string(), + code_refs: vec![EpiphanyCodeRef { + path: PathBuf::from("epiphany-core/src/lib.rs"), + symbol: Some("policy".to_string()), + ..Default::default() + }], + ..Default::default() + }], + edges: vec![EpiphanyGraphEdge { + id: Some("edge-core-self".to_string()), + source_id: "core".to_string(), + target_id: "core".to_string(), + kind: "owns".to_string(), + label: Some("owns itself for fixture".to_string()), + ..Default::default() + }], + }, + ..Default::default() + }; + + let snapshot = memory_graph_from_epiphany_graphs("repo-profile", &graphs); + let projected = epiphany_graphs_from_memory_graph(&snapshot); + + assert_eq!(projected.architecture.nodes.len(), 1); + assert_eq!(projected.architecture.nodes[0].title, "Core policy"); + assert_eq!(projected.architecture.nodes[0].code_refs.len(), 1); + assert_eq!(projected.architecture.edges.len(), 1); + assert_eq!(projected.architecture.edges[0].kind, "owns"); + } + #[test] fn repo_profile_keeps_bad_topology_visible() { let graphs = EpiphanyGraphs { diff --git a/notes/epiphany-memory-graph-unified-plan.md b/notes/epiphany-memory-graph-unified-plan.md index d0692303..38379b41 100644 --- a/notes/epiphany-memory-graph-unified-plan.md +++ b/notes/epiphany-memory-graph-unified-plan.md @@ -418,6 +418,9 @@ frontier updates are withheld when the proposal depends on unapplied memory growth. Generic `thread/epiphany/update` and `thread/epiphany/promote` bridge mutation paths now reject `patch.graphs`; graph replacement fields remain only as compatibility/deserialization reliquary, not live mutation authority. +`thread/epiphany/graphQuery` now prefers `state/memory-graph.msgpack` for +loaded threads and projects the typed repo memory graph back into the legacy +graph response shape only at the bridge edge. ### Phase 7: Sleep And Repo Refresh diff --git a/notes/fresh-workspace-handoff.md b/notes/fresh-workspace-handoff.md index 936432cd..a517e25b 100644 --- a/notes/fresh-workspace-handoff.md +++ b/notes/fresh-workspace-handoff.md @@ -39,7 +39,7 @@ remembers doctrine; the branch remembers the blade. - `epiphany-openai-runtime` worker result ingress now parses assistant output directly into typed role/reorient ingress structs selected from the durable launch document, stores nested `statePatch` and `selfPatch` as typed MessagePack in `EpiphanyRuntimeRoleWorkerResult`, and no longer passes worker result data through generic `serde_json::Value`. JSON survives there only as the OpenAI text-output edge before typed deserialization. - Agent self-memory review/application now has typed `AgentSelfPatch` core APIs. JSON-value selfPatch functions are compatibility wrappers for CLI/artifact ingress; native callers should parse once at the edge and then call `review_agent_self_patch_document` / `apply_agent_self_patch_document`. Role-result self-persistence no longer exports a JSON-value reviewer; the live API is `review_role_self_patch_document`. - `epiphany-openai-codex-spine` no longer uses Codex's Responses client, model-provider layer, `codex-api`, `codex-model-provider`, `codex-model-provider-info`, or `codex-protocol` for outbound model requests. It reaches `codex-login` only through `epiphany-openai-auth-spine` so credential loading/refresh stays Codex-compatible while request/status/event/runtime surfaces stay typed Epiphany documents. -- `notes/epiphany-memory-graph-unified-plan.md` is the current memory architecture direction drawn from `Fractal Domains And The Cache That Bites` plus the VoidBot memory scar tissue. The repo semantic architecture/dataflow graph and agent memory are one `EpiphanyMemoryGraph` substrate with repo, role, short-term, incubation, agency, candidate-intervention, and evidence profiles. The first native skeleton is landed: `epiphany-state-model` owns shared graph document types, `epiphany-core::memory_graph` owns stable ids, validation, freshness, context cuts, and CultCache persistence, and `epiphany-memory-graph` can status/validate/context/semantic-context/smoke/index the store. `epiphany-core::semantic_cache` now owns the shared Qdrant/Ollama transport used by workspace retrieval and memory graph indexing, so Qdrant remains a rebuildable contribution cache for typed graph documents, not truth. Runtime role results expose typed `memoryPatchCandidates`; Modeling `roleAccept` applies them to `state/memory-graph.msgpack`; native role/reorientation launches now prefer that persisted memory graph for bounded typed `memoryContext` cuts and only fall back to legacy accepted thread graphs for bootstrap. Reorientation launch no longer carries full `graphs` cargo. The old `thread/epiphany/propose` read route no longer returns replacement `graphs`; it exposes graph growth as typed `memoryPatchCandidates` and withholds frontier updates that depend on unapplied memory growth. Generic `thread/epiphany/update` and `thread/epiphany/promote` now reject `patch.graphs` at the bridge. `notes/repo-fractal-dataflow-cache-plan.md` and `notes/agent-memory-fractal-cache-plan.md` are profile plans, not rival organs. +- `notes/epiphany-memory-graph-unified-plan.md` is the current memory architecture direction drawn from `Fractal Domains And The Cache That Bites` plus the VoidBot memory scar tissue. The repo semantic architecture/dataflow graph and agent memory are one `EpiphanyMemoryGraph` substrate with repo, role, short-term, incubation, agency, candidate-intervention, and evidence profiles. The first native skeleton is landed: `epiphany-state-model` owns shared graph document types, `epiphany-core::memory_graph` owns stable ids, validation, freshness, context cuts, and CultCache persistence, and `epiphany-memory-graph` can status/validate/context/semantic-context/smoke/index the store. `epiphany-core::semantic_cache` now owns the shared Qdrant/Ollama transport used by workspace retrieval and memory graph indexing, so Qdrant remains a rebuildable contribution cache for typed graph documents, not truth. Runtime role results expose typed `memoryPatchCandidates`; Modeling `roleAccept` applies them to `state/memory-graph.msgpack`; native role/reorientation launches now prefer that persisted memory graph for bounded typed `memoryContext` cuts and only fall back to legacy accepted thread graphs for bootstrap. Reorientation launch no longer carries full `graphs` cargo. The old `thread/epiphany/propose` read route no longer returns replacement `graphs`; it exposes graph growth as typed `memoryPatchCandidates` and withholds frontier updates that depend on unapplied memory growth. Generic `thread/epiphany/update` and `thread/epiphany/promote` now reject `patch.graphs` at the bridge. `thread/epiphany/graphQuery` now prefers the persisted memory graph for loaded threads and projects back into the legacy graph response shape only at the bridge edge. `notes/repo-fractal-dataflow-cache-plan.md` and `notes/agent-memory-fractal-cache-plan.md` are profile plans, not rival organs. - Durable Epiphany state types now live in root crate `epiphany-state-model`. `codex-protocol` re-exports them only so old rollout/API callers keep compiling; do not add new durable `Epiphany*` state structs under `vendor/codex`. `epiphany-core` imports the native model directly and no longer depends on `codex-protocol`. Pressure derives from native `EpiphanyTokenUsageSnapshot`; Codex token telemetry is mapped at the bridge. Codex rollout/event reconstruction now lives in `codex-core::epiphany_rollout`, where the Codex `RolloutItem` / `EventMsg` types belong. Exact retrieval path matching is native to `epiphany-core`, so `epiphany-core` has no direct vendored Codex crate dependency. - State-update contract proof and job launch/interrupt DTO ownership now live in native `epiphany-core`. The old giant `codex_thread.rs` Epiphany state-update test fixture was cut, and `epiphany-codex-bridge` no longer imports Epiphany job launch request types from `codex_core`; vendored `CodexThread` may call the native mutation/launch law for compatibility, but it must not regain ownership of state-update semantics, heartbeat launch-plan policy, or Epiphany DTOs. - MCP remains JSON because MCP is a JSON protocol. The latest processor cut only moved MCP refresh/OAuth/status/resource/tool-call route handling into `codex_message_processor/mcp_routes.rs`, reducing the root processor to about 8,977 lines; it did not claim MCP payloads are CultNet or make MCP an Epiphany-owned runtime contract. diff --git a/state/ledgers.msgpack b/state/ledgers.msgpack index 8c6b7b108c8417cb6ea56819d9b49c38c2b589c6..8da17fdba2b513e82c133830aa30dd20b753c5c1 100644 GIT binary patch delta 201 zcmX?gn&ZS-jtR<4YJD43r!g{CHSb|;-^0kbeGenk59R4Mk2CUdT7(#wSeY1E8B8vm zrp>r}GNX<(S6+U(LP1e#T58es*EIr)hxsVNF28AYjyDa8tjc_}~@`B|yS zCB;C&RE3htg4C4h580T6gER9=^1)&`sp*Nyl?v%ai3J%7MXALF`FX{u3dI?T1*r=8 mc{!B|i6vllNky3{>8T2-Ktiu!i;;nmnXZAUt|8oI#!&!Li$#Y3 delta 53 zcmX?cmgCfEjtR<4suMP thread, Err(ThreadReadViewError::InvalidRequest(message)) => { @@ -319,7 +322,28 @@ impl CodexMessageProcessor { } }; - let loaded = self.thread_manager.get_thread(thread_uuid).await.is_ok(); + let loaded_thread = self.thread_manager.get_thread(thread_uuid).await.ok(); + let loaded = loaded_thread.is_some(); + let memory_graph_snapshot = if let Some(loaded_thread) = loaded_thread.as_ref() { + let config_snapshot = loaded_thread.config_snapshot().await; + let store_path = memory_graph_store_path(config_snapshot.cwd.as_path()); + match load_memory_graph_snapshot(&store_path) { + Ok(snapshot) => snapshot, + Err(err) => { + self.send_internal_error( + request_id, + format!( + "failed to load Epiphany memory graph store {}: {err}", + store_path.display() + ), + ) + .await; + return; + } + } + } else { + None + }; let thread = match self.read_thread_view(thread_uuid, false).await { Ok(thread) => thread, Err(ThreadReadViewError::InvalidRequest(message)) => { @@ -336,6 +360,7 @@ impl CodexMessageProcessor { thread_id, loaded, thread.epiphany_state.as_ref(), + memory_graph_snapshot.as_ref(), ¶ms.query, ); self.outgoing.send_response(request_id, response).await;