From c40aa286d480a719ad52dd4d6f25e1bad60a73a0 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 14 Aug 2026 03:11:07 +0200 Subject: [PATCH] Unify terminal tool receipt authority --- Cargo.lock | 1 + epiphany-core/src/runtime_spine.rs | 67 ++++++++++++------- epiphany-tool-mcp-runtime/Cargo.toml | 1 + epiphany-tool-mcp-runtime/src/lib.rs | 8 ++- epiphany-tool-mcp-runtime/src/main.rs | 10 ++- ...iphany.tool-invocation-receipt.schema.json | 2 +- 6 files changed, 55 insertions(+), 34 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fdb78287..771a720d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2287,6 +2287,7 @@ name = "epiphany-tool-mcp-runtime" version = "0.1.0" dependencies = [ "anyhow", + "chrono", "cultcache-rs", "epiphany-core", "epiphany-tool-adapter", diff --git a/epiphany-core/src/runtime_spine.rs b/epiphany-core/src/runtime_spine.rs index 15d8edf2..26c811f7 100644 --- a/epiphany-core/src/runtime_spine.rs +++ b/epiphany-core/src/runtime_spine.rs @@ -151,6 +151,8 @@ use epiphany_state_model::EpiphanyThreadState; use epiphany_tool_adapter::EpiphanyToolCapability; use epiphany_tool_adapter::EpiphanyToolInvocationIntent; use epiphany_tool_adapter::EpiphanyToolInvocationReceipt; +use epiphany_tool_adapter::TOOL_ADAPTER_INVOCATION_INTENT_SCHEMA_ID; +use epiphany_tool_adapter::TOOL_ADAPTER_INVOCATION_RECEIPT_SCHEMA_ID; use epiphany_tool_adapter::tool_invocation_intent_key; use epiphany_tool_adapter::tool_invocation_receipt_key; use serde::Deserialize; @@ -2075,9 +2077,6 @@ pub fn put_runtime_tool_execution_receipt( &receipt.completed_at, "tool execution receipt completion time", )?; - if !matches!(receipt.status.as_str(), "completed" | "failed") { - return Err(anyhow!("tool execution receipt status is not terminal")); - } let store_path = store_path.as_ref(); let binding = require_runtime_tool_execution_binding(store_path, &receipt.intent_id)?; let mut cache = runtime_spine_cache(store_path)?; @@ -2085,14 +2084,7 @@ pub fn put_runtime_tool_execution_receipt( let intent = cache .get::(&tool_invocation_intent_key(&receipt.intent_id))? .ok_or_else(|| anyhow!("tool execution receipt lost its intent"))?; - if receipt.adapter != intent.adapter - || receipt.server != intent.server - || receipt.tool_name != intent.tool_name - { - return Err(anyhow!( - "tool execution receipt disagrees with its immutable intent" - )); - } + validate_terminal_tool_execution_family(&binding, &intent, receipt)?; let receipt_key = tool_invocation_receipt_key(&receipt.intent_id); if cache .get::(&receipt_key)? @@ -2126,6 +2118,32 @@ pub fn put_runtime_tool_execution_receipt( Ok(()) } +fn validate_terminal_tool_execution_family( + binding: &EpiphanyRuntimeToolExecutionBinding, + intent: &EpiphanyToolInvocationIntent, + receipt: &EpiphanyToolInvocationReceipt, +) -> Result<()> { + if binding.schema_version != RUNTIME_TOOL_EXECUTION_BINDING_SCHEMA_VERSION + || binding.binding_id != intent.intent_id + || binding.intent_id != intent.intent_id + || binding.model_request_id != intent.model_request_id + || intent.schema_id != TOOL_ADAPTER_INVOCATION_INTENT_SCHEMA_ID + || receipt.schema_id != TOOL_ADAPTER_INVOCATION_RECEIPT_SCHEMA_ID + || receipt.receipt_id.is_empty() + || receipt.intent_id != intent.intent_id + || receipt.adapter != intent.adapter + || receipt.server != intent.server + || receipt.tool_name != intent.tool_name + || !matches!(receipt.status.as_str(), "completed" | "failed") + || chrono::DateTime::parse_from_rfc3339(&receipt.completed_at).is_err() + { + return Err(anyhow!( + "tool execution binding, intent, and terminal receipt are not one exact family" + )); + } + Ok(()) +} + pub fn archive_completed_model_session( store_path: impl AsRef, session_id: &str, @@ -3871,16 +3889,9 @@ fn authenticated_public_source_lookup_receipts_for_worker( ))? .ok_or_else(|| anyhow!("public source tool intent has no terminal receipt"))?; let _ = governed_source_tool_authority(&cache, &binding.job_id, &intent)?; - if binding.intent_id != intent.intent_id - || intent.model_request_id != binding.model_request_id - || receipt.intent_id != intent.intent_id - || receipt.adapter != intent.adapter - || receipt.server != intent.server - || receipt.tool_name != intent.tool_name - || receipt.status != "completed" - || chrono::DateTime::parse_from_rfc3339(&receipt.completed_at).is_err() - { - return Err(anyhow!("public source tool family is not exact and terminal")); + validate_terminal_tool_execution_family(&binding, &intent, &receipt)?; + if receipt.status != "completed" { + return Err(anyhow!("public source lookup did not complete successfully")); } let arguments: serde_json::Value = serde_json::from_str(&intent.arguments_json) .context("public source intent arguments are invalid")?; @@ -20836,7 +20847,7 @@ pub(crate) mod tests { "source", "read", "completed", - "unix-ms:1", + "2026-08-10T02:00:08Z", ); put_runtime_tool_execution_receipt(&store, &receipt)?; assert!(put_runtime_tool_execution_receipt(&store, &receipt).is_err()); @@ -20847,7 +20858,7 @@ pub(crate) mod tests { "foreign", "read", "completed", - "unix-ms:2", + "2026-08-10T02:00:09Z", ); assert!(put_runtime_tool_execution_receipt(&store, &hostile_receipt).is_err()); let mut cache = runtime_spine_cache(&store)?; @@ -21027,6 +21038,14 @@ pub(crate) mod tests { }) .to_string(), ); + let mut malformed_time_receipt = exact_receipt.clone(); + malformed_time_receipt.completed_at = "unix-ms:1786668821385".into(); + let before_malformed_time = runtime_spine_backing_store(&store)?.pull_all()?; + assert!(put_runtime_tool_execution_receipt(&store, &malformed_time_receipt).is_err()); + assert_eq!( + runtime_spine_backing_store(&store)?.pull_all()?, + before_malformed_time + ); put_runtime_tool_execution_receipt(&store, &exact_receipt)?; let lookups = runtime_authenticated_public_source_lookups_for_worker( &store, @@ -21254,7 +21273,7 @@ pub(crate) mod tests { "source", "read", "completed", - "unix-ms:3", + "2026-08-10T03:00:07Z", ); put_runtime_tool_execution_receipt(&store, &tool_receipt)?; let mut cache = runtime_spine_cache(&store)?; diff --git a/epiphany-tool-mcp-runtime/Cargo.toml b/epiphany-tool-mcp-runtime/Cargo.toml index 298250cc..bdefe3ca 100644 --- a/epiphany-tool-mcp-runtime/Cargo.toml +++ b/epiphany-tool-mcp-runtime/Cargo.toml @@ -7,6 +7,7 @@ license-file = "../LICENSE" [dependencies] anyhow = "1" +chrono = "0.4.44" epiphany-tool-adapter = { path = "../epiphany-tool-adapter" } epiphany-core = { path = "../epiphany-core" } cultcache-rs = { path = "../vendor/cultcache-rs" } diff --git a/epiphany-tool-mcp-runtime/src/lib.rs b/epiphany-tool-mcp-runtime/src/lib.rs index 1fd3285d..426b65d7 100644 --- a/epiphany-tool-mcp-runtime/src/lib.rs +++ b/epiphany-tool-mcp-runtime/src/lib.rs @@ -12,6 +12,7 @@ pub use native_public::execute_epiphany_public; pub use native_source::execute_epiphany_source; use anyhow::{Context, Result, anyhow}; +use chrono::Utc; use epiphany_tool_adapter::{ EPIPHANY_TOOL_RUNTIME_ADAPTER_ID, EpiphanyToolInvocationIntent, EpiphanyToolInvocationReceipt, TOOL_ADAPTER_INVOCATION_INTENT_SCHEMA_ID, @@ -44,7 +45,7 @@ pub async fn invoke( intent: &EpiphanyToolInvocationIntent, config: &McpRuntimeConfig, ) -> InvocationOutcome { - let completed_at = now_utc_string(); + let completed_at = current_utc_timestamp(); let mut receipt = EpiphanyToolInvocationReceipt::new( format!("receipt-{}-{}", intent.intent_id, unix_millis()), intent.intent_id.clone(), @@ -272,8 +273,8 @@ fn bounded_error(value: &str) -> String { value.chars().take(LIMIT).collect::() + "..." } -fn now_utc_string() -> String { - format!("unix-ms:{}", unix_millis()) +pub fn current_utc_timestamp() -> String { + Utc::now().to_rfc3339() } fn unix_millis() -> u128 { @@ -318,6 +319,7 @@ mod tests { let outcome = invoke(&intent("{}"), &config).await; assert_eq!(outcome.receipt.status, "failed"); assert_eq!(outcome.receipt.intent_id, "intent-1"); + assert!(chrono::DateTime::parse_from_rfc3339(&outcome.receipt.completed_at).is_ok()); assert!(outcome.receipt.error.as_deref().unwrap().contains("demo")); assert!(outcome.raw_result.is_none()); } diff --git a/epiphany-tool-mcp-runtime/src/main.rs b/epiphany-tool-mcp-runtime/src/main.rs index 4099efec..a7dc7450 100644 --- a/epiphany-tool-mcp-runtime/src/main.rs +++ b/epiphany-tool-mcp-runtime/src/main.rs @@ -5,8 +5,8 @@ use epiphany_tool_adapter::{ TOOL_ADAPTER_INVOCATION_RECEIPT_SCHEMA_ID, tool_invocation_intent_key, }; use epiphany_tool_mcp_runtime::{ - McpRuntimeConfig, execute_epiphany_public, execute_epiphany_source, invoke, - validate_intent, + McpRuntimeConfig, current_utc_timestamp, execute_epiphany_public, execute_epiphany_source, + invoke, validate_intent, }; use serde::Serialize; use std::collections::BTreeMap; @@ -157,7 +157,7 @@ async fn execute_to_receipt( } else { "failed" }, - now(), + current_utc_timestamp(), ); match result { Ok(value) => receipt.result_json = serde_json::to_string(&value).ok(), @@ -253,9 +253,6 @@ fn unix_millis() -> u128 { .unwrap_or_default() .as_millis() } -fn now() -> String { - format!("unix-ms:{}", unix_millis()) -} fn bound(value: &str, limit: usize) -> String { if value.chars().count() <= limit { value.into() @@ -374,6 +371,7 @@ mod tests { &tool_invocation_receipt_key(&intent.intent_id), )?; assert_eq!(receipt.adapter, EPIPHANY_TOOL_RUNTIME_ADAPTER_ID); + assert!(chrono::DateTime::parse_from_rfc3339(&receipt.completed_at).is_ok()); assert!(receipt.result_json.as_deref().unwrap().contains("awake")); Ok(()) } diff --git a/schemas/cultnet/epiphany.tool-invocation-receipt.schema.json b/schemas/cultnet/epiphany.tool-invocation-receipt.schema.json index 5c644fe0..d079959b 100644 --- a/schemas/cultnet/epiphany.tool-invocation-receipt.schema.json +++ b/schemas/cultnet/epiphany.tool-invocation-receipt.schema.json @@ -22,7 +22,7 @@ "server": { "type": "string" }, "tool_name": { "type": "string" }, "status": { "type": "string" }, - "completed_at": { "type": "string" }, + "completed_at": { "type": "string", "format": "date-time" }, "result_json": { "type": ["string", "null"] }, "error": { "type": ["string", "null"] }, "sealed_raw_result_ref": { "type": ["string", "null"] }