Add quarantined Codex MCP spine

This commit is contained in:
Meta Vens 2026-05-28 00:00:12 +01:00
commit 210bfa6533
6 changed files with 8364 additions and 5 deletions

7848
epiphany-tool-codex-mcp-spine/Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,32 @@
[package]
name = "epiphany-tool-codex-mcp-spine"
version = "0.1.0"
edition = "2024"
publish = false
license-file = "../LICENSE"
[dependencies]
anyhow = "1"
allocative = { version = "=0.3.4", features = ["hashbrown"] }
async-channel = "2"
codex-config = { path = "../vendor/codex/codex-rs/config" }
codex-exec-server = { path = "../vendor/codex/codex-rs/exec-server" }
codex-mcp = { path = "../vendor/codex/codex-rs/codex-mcp" }
codex-protocol = { path = "../vendor/codex/codex-rs/protocol" }
cultcache-rs = { path = "../vendor/cultcache-rs" }
epiphany-tool-adapter = { path = "../epiphany-tool-adapter" }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
toml = "0.9"
[[bin]]
name = "epiphany-tool-codex-mcp-spine"
path = "src/main.rs"
[patch.crates-io]
tokio-tungstenite = { git = "https://github.com/openai-oss-forks/tokio-tungstenite", rev = "132f5b39c862e3a970f731d709608b3e6276d5f6" }
tungstenite = { git = "https://github.com/openai-oss-forks/tungstenite-rs", rev = "9200079d3b54a1ff51072e24d81fd354f085156f" }
[patch."ssh://git@github.com/openai-oss-forks/tungstenite-rs.git"]
tungstenite = { git = "https://github.com/openai-oss-forks/tungstenite-rs", rev = "9200079d3b54a1ff51072e24d81fd354f085156f" }

View file

@ -0,0 +1,472 @@
use anyhow::Context;
use anyhow::Result;
use anyhow::anyhow;
use async_channel::unbounded;
use codex_config::Constrained;
use codex_config::CONFIG_TOML_FILE;
use codex_config::config_toml::ConfigToml;
use codex_config::load_global_mcp_servers;
use codex_config::types::OAuthCredentialsStoreMode;
use codex_exec_server::Environment;
use codex_mcp::McpConfig;
use codex_mcp::McpConnectionManager;
use codex_mcp::McpRuntimeEnvironment;
use codex_mcp::compute_auth_statuses;
use codex_mcp::effective_mcp_servers;
use codex_protocol::protocol::AskForApproval;
use codex_protocol::protocol::SandboxPolicy;
use cultcache_rs::CultCache;
use cultcache_rs::SingleFileMessagePackBackingStore;
use epiphany_tool_adapter::EpiphanyToolCapability;
use epiphany_tool_adapter::EpiphanyToolInvocationIntent;
use epiphany_tool_adapter::EpiphanyToolInvocationReceipt;
use epiphany_tool_adapter::TOOL_ADAPTER_CAPABILITY_SCHEMA_ID;
use epiphany_tool_adapter::TOOL_ADAPTER_INVOCATION_INTENT_SCHEMA_ID;
use epiphany_tool_adapter::TOOL_ADAPTER_INVOCATION_RECEIPT_SCHEMA_ID;
use serde::Serialize;
use serde_json::Value;
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::SystemTime;
use std::time::UNIX_EPOCH;
const ADAPTER_ID: &str = "codex-mcp";
#[tokio::main]
async fn main() -> Result<()> {
let command = Command::parse(std::env::args().skip(1).collect())?;
match command {
Command::Run(options) => {
let summary = run_invocation(options).await?;
println!("{}", serde_json::to_string_pretty(&summary)?);
}
Command::Smoke(options) => {
let summary = smoke(options)?;
println!("{}", serde_json::to_string_pretty(&summary)?);
}
}
Ok(())
}
#[derive(Debug, Clone, PartialEq, Eq)]
enum Command {
Run(RunOptions),
Smoke(SmokeOptions),
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct RunOptions {
store: PathBuf,
intent_id: String,
codex_home: Option<PathBuf>,
cwd: Option<PathBuf>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct SmokeOptions {
store: PathBuf,
intent_id: String,
}
impl Command {
fn parse(args: Vec<String>) -> Result<Self> {
let (command, rest) = args
.split_first()
.ok_or_else(|| anyhow!("usage: epiphany-tool-codex-mcp-spine <run|smoke> ..."))?;
let values = parse_flags(rest)?;
match command.as_str() {
"run" => Ok(Self::Run(RunOptions {
store: required_path(&values, "store")?,
intent_id: required_string(&values, "intent-id")?,
codex_home: optional_path(&values, "codex-home")?,
cwd: optional_path(&values, "cwd")?,
})),
"smoke" => Ok(Self::Smoke(SmokeOptions {
store: required_path(&values, "store")?,
intent_id: values
.get("intent-id")
.cloned()
.unwrap_or_else(|| "codex-mcp-spine-smoke".to_string()),
})),
other => Err(anyhow!(
"unknown command {other:?}; expected run or smoke"
)),
}
}
}
fn parse_flags(args: &[String]) -> Result<BTreeMap<String, String>> {
let mut values = BTreeMap::new();
let mut iter = args.iter();
while let Some(flag) = iter.next() {
let name = flag
.strip_prefix("--")
.ok_or_else(|| anyhow!("expected --flag, got {flag:?}"))?;
let value = iter
.next()
.ok_or_else(|| anyhow!("missing value for --{name}"))?;
values.insert(name.to_string(), value.to_string());
}
Ok(values)
}
fn required_string(values: &BTreeMap<String, String>, name: &str) -> Result<String> {
values
.get(name)
.cloned()
.ok_or_else(|| anyhow!("missing --{name}"))
}
fn required_path(values: &BTreeMap<String, String>, name: &str) -> Result<PathBuf> {
Ok(PathBuf::from(required_string(values, name)?))
}
fn optional_path(values: &BTreeMap<String, String>, name: &str) -> Result<Option<PathBuf>> {
Ok(values.get(name).map(PathBuf::from))
}
fn open_store(path: &Path) -> Result<CultCache> {
let mut cache = CultCache::new();
cache.register_entry_type::<EpiphanyToolCapability>()?;
cache.register_entry_type::<EpiphanyToolInvocationIntent>()?;
cache.register_entry_type::<EpiphanyToolInvocationReceipt>()?;
cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(path));
cache.pull_all_backing_stores()?;
Ok(cache)
}
async fn run_invocation(options: RunOptions) -> Result<RunSummary> {
let mut cache = open_store(&options.store)?;
let intent = cache
.get_required::<EpiphanyToolInvocationIntent>(&intent_key(&options.intent_id))
.with_context(|| format!("failed to load tool invocation intent {}", options.intent_id))?;
validate_intent(&intent)?;
let completed_at = now_utc_string();
let receipt_id = receipt_id(&intent.intent_id);
let result = execute_codex_mcp(&intent, &options).await;
let mut receipt = EpiphanyToolInvocationReceipt::new(
receipt_id.clone(),
intent.intent_id.clone(),
intent.adapter.clone(),
intent.server.clone(),
intent.tool_name.clone(),
if result.is_ok() { "completed" } else { "failed" },
completed_at,
);
match result {
Ok(result) => {
receipt.result_json = Some(serde_json::to_string(&result)?);
}
Err(error) => {
receipt.error = Some(format!("{error:#}"));
}
}
cache.put(receipt_key(&receipt.intent_id), &receipt)?;
Ok(RunSummary {
adapter: ADAPTER_ID.to_string(),
store: options.store.display().to_string(),
intent_id: intent.intent_id,
receipt_id,
status: receipt.status,
schemas: schema_map(),
})
}
async fn execute_codex_mcp(
intent: &EpiphanyToolInvocationIntent,
options: &RunOptions,
) -> Result<Value> {
let cwd = options
.cwd
.clone()
.unwrap_or(std::env::current_dir().context("failed to resolve current directory")?);
let codex_home = resolve_codex_home(options.codex_home.clone())?;
let mcp_config = load_mcp_config(&codex_home).await?;
let mut mcp_servers = effective_mcp_servers(&mcp_config, None);
mcp_servers.retain(|name, _| name == &intent.server);
if mcp_servers.is_empty() {
return Err(anyhow!(
"Codex MCP has no effective server named {:?}",
intent.server
));
}
let auth_statuses = compute_auth_statuses(
mcp_servers.iter(),
mcp_config.mcp_oauth_credentials_store_mode,
)
.await;
let (tx_event, rx_event) = unbounded();
drop(rx_event);
let runtime_environment =
McpRuntimeEnvironment::new(Arc::new(Environment::default_for_tests()), cwd);
let (manager, cancel_token) = McpConnectionManager::new(
&mcp_servers,
mcp_config.mcp_oauth_credentials_store_mode,
auth_statuses,
&mcp_config.approval_policy,
format!("epiphany-tool-{}", intent.intent_id),
tx_event,
SandboxPolicy::new_read_only_policy(),
runtime_environment,
)
.await;
let arguments = parse_arguments(&intent.arguments_json)?;
let result = manager
.call_tool(&intent.server, &intent.tool_name, arguments, None)
.await
.with_context(|| {
format!(
"Codex MCP call failed for server {:?} tool {:?}",
intent.server, intent.tool_name
)
});
cancel_token.cancel();
Ok(serde_json::to_value(result?)?)
}
async fn load_mcp_config(codex_home: &Path) -> Result<McpConfig> {
let config_toml = read_codex_config_toml(codex_home)?;
let mcp_servers = load_global_mcp_servers(codex_home)
.await
.with_context(|| {
format!(
"failed to load Codex MCP servers from {}",
codex_home.join(CONFIG_TOML_FILE).display()
)
})?;
Ok(McpConfig {
chatgpt_base_url: config_toml
.as_ref()
.and_then(|config| config.chatgpt_base_url.clone())
.unwrap_or_else(|| "https://chatgpt.com/backend-api/".to_string()),
codex_home: codex_home.to_path_buf(),
mcp_oauth_credentials_store_mode: config_toml
.as_ref()
.and_then(|config| config.mcp_oauth_credentials_store)
.unwrap_or(OAuthCredentialsStoreMode::Auto),
mcp_oauth_callback_port: config_toml
.as_ref()
.and_then(|config| config.mcp_oauth_callback_port),
mcp_oauth_callback_url: config_toml
.as_ref()
.and_then(|config| config.mcp_oauth_callback_url.clone()),
approval_policy: Constrained::allow_any(
config_toml
.as_ref()
.and_then(|config| config.approval_policy)
.unwrap_or(AskForApproval::Never),
),
codex_linux_sandbox_exe: None,
use_legacy_landlock: false,
configured_mcp_servers: mcp_servers.into_iter().collect::<HashMap<_, _>>(),
})
}
fn read_codex_config_toml(codex_home: &Path) -> Result<Option<ConfigToml>> {
let path = codex_home.join(CONFIG_TOML_FILE);
if !path.exists() {
return Ok(None);
}
let raw = fs::read_to_string(&path)
.with_context(|| format!("failed to read Codex config {}", path.display()))?;
toml::from_str::<ConfigToml>(&raw)
.map(Some)
.with_context(|| format!("failed to parse Codex config {}", path.display()))
}
fn resolve_codex_home(explicit: Option<PathBuf>) -> Result<PathBuf> {
if let Some(path) = explicit {
return Ok(path);
}
if let Some(path) = std::env::var_os("CODEX_HOME") {
return Ok(PathBuf::from(path));
}
let user_profile = std::env::var_os("USERPROFILE")
.or_else(|| std::env::var_os("HOME"))
.ok_or_else(|| anyhow!("cannot resolve Codex home: set --codex-home or CODEX_HOME"))?;
Ok(PathBuf::from(user_profile).join(".codex"))
}
fn parse_arguments(arguments_json: &str) -> Result<Option<Value>> {
let trimmed = arguments_json.trim();
if trimmed.is_empty() || trimmed == "null" {
return Ok(None);
}
let value: Value = serde_json::from_str(trimmed).context("arguments_json is not valid JSON")?;
match value {
Value::Null => Ok(None),
Value::Object(_) => Ok(Some(value)),
other => Err(anyhow!(
"arguments_json must decode to a JSON object or null, got {}",
json_kind(&other)
)),
}
}
fn validate_intent(intent: &EpiphanyToolInvocationIntent) -> Result<()> {
if intent.schema_id != TOOL_ADAPTER_INVOCATION_INTENT_SCHEMA_ID {
return Err(anyhow!(
"intent {} has schema_id {:?}, expected {:?}",
intent.intent_id,
intent.schema_id,
TOOL_ADAPTER_INVOCATION_INTENT_SCHEMA_ID
));
}
if intent.adapter != ADAPTER_ID {
return Err(anyhow!(
"intent {} targets adapter {:?}, expected {:?}",
intent.intent_id,
intent.adapter,
ADAPTER_ID
));
}
if intent.server.trim().is_empty() {
return Err(anyhow!("intent {} has empty MCP server", intent.intent_id));
}
if intent.tool_name.trim().is_empty() {
return Err(anyhow!("intent {} has empty MCP tool name", intent.intent_id));
}
Ok(())
}
fn smoke(options: SmokeOptions) -> Result<SmokeSummary> {
let mut cache = open_store(&options.store)?;
let intent = EpiphanyToolInvocationIntent::new(
options.intent_id.clone(),
ADAPTER_ID,
"smoke-server",
"smoke-tool",
"{}",
"epiphany-tool-codex-mcp-spine smoke",
"prove typed Codex MCP quarantine store round-trip",
now_utc_string(),
);
let receipt = EpiphanyToolInvocationReceipt::new(
receipt_id(&intent.intent_id),
intent.intent_id.clone(),
ADAPTER_ID,
intent.server.clone(),
intent.tool_name.clone(),
"smoke",
now_utc_string(),
);
cache.put(intent_key(&intent.intent_id), &intent)?;
cache.put(receipt_key(&intent.intent_id), &receipt)?;
Ok(SmokeSummary {
adapter: ADAPTER_ID.to_string(),
store: options.store.display().to_string(),
intent_id: intent.intent_id,
receipt_id: receipt.receipt_id,
schemas: schema_map(),
})
}
fn intent_key(intent_id: &str) -> String {
format!("intent:{intent_id}")
}
fn receipt_key(intent_id: &str) -> String {
format!("receipt:{intent_id}")
}
fn receipt_id(intent_id: &str) -> String {
format!("receipt-{intent_id}-{}", unix_millis())
}
fn now_utc_string() -> String {
format!("unix-ms:{}", unix_millis())
}
fn unix_millis() -> u128 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_millis()
}
fn json_kind(value: &Value) -> &'static str {
match value {
Value::Null => "null",
Value::Bool(_) => "boolean",
Value::Number(_) => "number",
Value::String(_) => "string",
Value::Array(_) => "array",
Value::Object(_) => "object",
}
}
fn schema_map() -> BTreeMap<String, String> {
BTreeMap::from([
(
"capability".to_string(),
TOOL_ADAPTER_CAPABILITY_SCHEMA_ID.to_string(),
),
(
"intent".to_string(),
TOOL_ADAPTER_INVOCATION_INTENT_SCHEMA_ID.to_string(),
),
(
"receipt".to_string(),
TOOL_ADAPTER_INVOCATION_RECEIPT_SCHEMA_ID.to_string(),
),
])
}
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct RunSummary {
adapter: String,
store: String,
intent_id: String,
receipt_id: String,
status: String,
schemas: BTreeMap<String, String>,
}
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct SmokeSummary {
adapter: String,
store: String,
intent_id: String,
receipt_id: String,
schemas: BTreeMap<String, String>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_object_or_null_arguments_only() -> Result<()> {
assert_eq!(parse_arguments("")?, None);
assert_eq!(parse_arguments("null")?, None);
assert!(parse_arguments(r#"{"path":"state/map.yaml"}"#)?.is_some());
assert!(parse_arguments("[]").is_err());
Ok(())
}
#[test]
fn rejects_non_codex_mcp_adapter() {
let intent = EpiphanyToolInvocationIntent::new(
"i",
"heimdall-eventually",
"server",
"tool",
"{}",
"test",
"test",
"now",
);
assert!(validate_intent(&intent).is_err());
}
}

View file

@ -39,7 +39,7 @@ remembers doctrine; the branch remembers the blade.
- `epiphany-mvp-status --source native` is the current provider-neutral status boundary. It projects scene, pressure, jobs, roles, planning, reorient, CRRC, and coordinator status from native core surfaces and returns provider/runtime worker results as `backendMissing` placeholders instead of asking Codex for model-worker state. Keep future pluggable model-provider work behind launch/result/runtime documents; do not let provider choice leak into status, prompt authority, scheduler policy, or durable `EpiphanyThreadState`.
- `epiphany-model-runtime` is the current provider-neutral executable boundary. It is built by the `epiphany-openai-runtime` crate for now and accepts `--provider openai-codex`; the old `epiphany-openai-runtime` binary, `--openai-runtime-bin`, and `openai-runtime` executor name survive only as compatibility aliases. Coordinator run mode, local wrapper run mode, and repo birth runner should call `--model-runtime-bin` plus `--model-provider`. The typed request/event/receipt contract is now provider-neutral in `epiphany-model-adapter` as `epiphany.model_request.v0`, `epiphany.model_stream_event.v0`, `epiphany.model_receipt.v0`, and `epiphany.model_adapter_status.v0`; OpenAI/Codex documents are adapter evidence behind that boundary, not scheduler/status/state authority.
- Coordinator plan/run passes now emit `epiphany.coordinator_run_receipt.v0` into runtime-spine. `coordinator-summary.json` and `coordinator-steps.jsonl` remain useful operator artifacts, but the durable receipt of what the coordinator decided is typed CultCache/CultNet state.
- The first tool/MCP contract surface exists in `epiphany-tool-adapter`: `epiphany.tool_capability.v0`, `epiphany.tool_invocation_intent.v0`, and `epiphany.tool_invocation_receipt.v0`. Runtime-spine advertises these as the Epiphany-facing tool boundary. Raw MCP JSON is still an execution-edge concern until a real adapter consumes the intents and emits the receipts.
- The first tool/MCP contract surface exists in `epiphany-tool-adapter`: `epiphany.tool_capability.v0`, `epiphany.tool_invocation_intent.v0`, and `epiphany.tool_invocation_receipt.v0`. Runtime-spine advertises these as the Epiphany-facing tool boundary. `epiphany-tool-codex-mcp-spine` is the quarantined executing adapter: it consumes one typed invocation intent from CultCache, calls user-declared Codex MCP through `codex-mcp`, and emits a typed invocation receipt. Raw MCP JSON remains protocol-edge cargo; Codex MCP is transport scaffolding, not Epiphany state, prompt, scheduler, or policy authority.
- The active foundation directive is now `notes/codex-starvation-and-cultnet-liberation-plan.md`. The previous Codex app-server control-plane rebuild made Epiphany less rotten inside Codex, but that is not the destination. Epiphany must become a native CultCache/CultMesh/CultNet runtime while remaining an honest modified Codex-derived backend for subscription auth/model use. Codex should remain relatively vanilla and keep doing Codex things, including useful app-server and streaming bridge affordances, but it must not own Epiphany state, processes, prompts, scheduler decisions, or policy. The bridge handles interop; Codex and Epiphany do not get to rummage around in each other's organs.
- `notes/epiphany-cultmesh-dreaming-roadmap.md` is the new concrete design for distributed Epiphany dreaming. The invariant is hard: private state stays local; public dreams are separately authored typed documents distributed through CultMesh/CultNet; foreign dreams are thought weather until a reviewed local adoption receipt digests them. The first code slice should be dream schemas plus a local CultMesh-backed store, not public fanout.
- CultMesh is now the preferred Rust abstraction for that local store work. The compile-time Rust substrate is repo-contained: `vendor/cultcache-rs`, `vendor/cultnet-rs`, and `vendor/cultmesh-rs`. The old `E:\Projects\CultLib\crates\*` dependency body is dead in this checkout and must not be revived by accident. Vendored CultCache reads/writes `cultcache.store.v1`, so `state/ledgers.msgpack` and `epiphany-state status` are aligned again. The first integrations are deliberately small: status, Verse policy, global room policy, organ contract policy, native operator status, native operator snapshots, and local operator run intent/receipt round-trip through CultMesh. Use `cargo run --manifest-path .\epiphany-core\Cargo.toml --bin epiphany-cultmesh-smoke`, `cargo run --manifest-path .\epiphany-core\Cargo.toml --bin epiphany-cultmesh-status -- smoke --store .\.epiphany-smoke\cultmesh\operator-status.ccmp --runtime-id epiphany-cultmesh-status-smoke`, `cargo run --manifest-path .\epiphany-core\Cargo.toml --bin epiphany-operator-snapshot -- smoke --store .\.epiphany-smoke\cultmesh\operator-snapshots.ccmp --runtime-id epiphany-operator-snapshot-smoke --snapshot-id smoke-status`, and `cargo run --manifest-path .\epiphany-core\Cargo.toml --bin epiphany-operator-run -- smoke --store .\.epiphany-smoke\cultmesh\operator-runs.ccmp --runtime-id epiphany-operator-run-smoke --run-id smoke-run` for the focused proofs. Future local dream/status/Verse work should start from CultMesh, not raw CultNet, unless the task is polishing the CultMesh internals themselves. Preserve the three Verse tiers: `epiphany-internal` for private sub-agent typed state, `gamecult-local` for trusted LAN/Yggdrasil-tunnel GameCult sharing, and `epiphany-global` for untrusted public dreams plus topic-specific threaded public rooms for Face posts.

Binary file not shown.

View file

@ -532,12 +532,19 @@ current_status:
existing coordinator artifact JSON remains operator display and sealed
audit evidence, but it is no longer the only durable account of what the
coordinator decided.
The first tool/MCP boundary contract is also named:
The first tool/MCP boundary contract and quarantined execution spine are
named:
`epiphany-tool-adapter` owns typed `epiphany.tool_capability.v0`,
`epiphany.tool_invocation_intent.v0`, and
`epiphany.tool_invocation_receipt.v0` documents. Runtime-spine advertises
them as the Epiphany-facing tool contract so MCP discovery/call JSON can
remain protocol-edge cargo instead of scheduler, state, or acceptance
authority. This is a contract surface only; the executing MCP adapter still
needs to be built before Codex MCP routes can be demoted.
next_action: Continue Codex starvation and CultNet liberation by building the executing tool/MCP adapter that consumes `epiphany.tool_invocation_intent.v0`, emits `epiphany.tool_invocation_receipt.v0`, and keeps raw MCP JSON sealed at the adapter boundary.
authority. `epiphany-tool-codex-mcp-spine` is the temporary executing
quarantine: it loads one typed invocation intent from CultCache, calls
user-declared Codex MCP through `codex-mcp`, and writes a typed invocation
receipt. It depends on Codex MCP/config/OAuth machinery as transport
scaffolding only; it does not own Epiphany scheduler policy, prompt
authority, durable state, or future provider shape. The future Heimdall or
native tool-auth spine can replace this adapter without changing the
Epiphany-facing intent/receipt contract.
next_action: Continue Codex starvation and CultNet liberation by wiring the typed tool invocation intent/receipt path into the coordinator/runtime flow only where a real tool call is needed, while keeping the Codex MCP spine quarantined and replaceable.