Unify terminal tool receipt authority

This commit is contained in:
Meta Vens 2026-08-14 03:11:07 +02:00
commit c40aa286d4
6 changed files with 55 additions and 34 deletions

1
Cargo.lock generated
View file

@ -2287,6 +2287,7 @@ name = "epiphany-tool-mcp-runtime"
version = "0.1.0"
dependencies = [
"anyhow",
"chrono",
"cultcache-rs",
"epiphany-core",
"epiphany-tool-adapter",

View file

@ -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::<EpiphanyToolInvocationIntent>(&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::<EpiphanyToolInvocationReceipt>(&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<Path>,
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)?;

View file

@ -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" }

View file

@ -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::<String>() + "...<truncated>"
}
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());
}

View file

@ -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(())
}

View file

@ -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"] }