| /* | |
| * Licensed to the Apache Software Foundation (ASF) under one | |
| * or more contributor license agreements. See the NOTICE file | |
| * distributed with this work for additional information | |
| * regarding copyright ownership. The ASF licenses this file | |
| * to you under the Apache License, Version 2.0 (the | |
| * "License"); you may not use this file except in compliance | |
| * with the License. You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, | |
| * software distributed under the License is distributed on an | |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
| * KIND, either express or implied. See the License for the | |
| * specific language governing permissions and limitations | |
| * under the License. | |
| */ | |
| //! Actual external Host effects: original capability, one admission, immutable evidence. | |
| use super::{Owner, RunAccess, now}; | |
| use crate::executor::external::HostEffects; | |
| use futures_util::{FutureExt, future::BoxFuture}; | |
| use maka_agent_engine::{RunError, cleanup::Evidence}; | |
| use maka_agent_facts::{ | |
| durable::*, | |
| external_effects::{ | |
| ExternalEffectDescriptor, ExternalEffectEvent, ExternalEffectKey, ExternalReturn, | |
| StoredExternalEffectDescriptor, StoredToolDefinition, | |
| }, | |
| tool_call::{ToolOrigin, ToolSettlement}, | |
| tool_output::{DurableToolProjection, ReturnedToolResult, ToolOutput, ToolSuccess}, | |
| tools::{ToolCallContext, ToolDefinition, ToolError}, | |
| }; | |
| use maka_agent_state::{Change, ContentBlob, DriverClaim, Head, fold::RunView}; | |
| use maka_capabilities::call::Identity; | |
| use maka_event_log::StoreError; | |
| use maka_plugin_core::operation::{ | |
| OperationCleanup, OperationOutcome, OperationReply, Receipt, RecoveryRef, | |
| }; | |
| use maka_tools::ToolCatalog; | |
| use serde_json::Value; | |
| use sha2::{Digest, Sha256}; | |
| use std::{ | |
| panic::AssertUnwindSafe, | |
| sync::{Arc, Mutex}, | |
| time::SystemTime, | |
| }; | |
| use tokio::sync::oneshot; | |
| use tokio_util::{sync::CancellationToken, task::TaskTracker}; | |
| use uuid::Uuid; | |
| const PROTOCOL: &str = "maka.external-host-effect"; | |
| type EffectReply = oneshot::Sender<Result<ReturnedToolResult, ToolError>>; | |
| #[derive(Clone)] | |
| pub(super) struct NativeExternalEffects { | |
| owner: Arc<Owner>, | |
| state: maka_agent_state::Store, | |
| access: Arc<RunAccess>, | |
| claim: DriverClaim, | |
| cleanup: Evidence, | |
| workers: TaskTracker, | |
| pub(super) cells: Option<Arc<super::driver_cells::RunCells>>, | |
| observation: Arc<Mutex<Option<EffectReply>>>, | |
| retained: Arc<Mutex<Vec<Arc<RetainedBatch>>>>, | |
| calls: Arc<Mutex<Vec<Arc<tokio::sync::Mutex<OwnedCall>>>>>, | |
| active: Option<Arc<tokio::sync::Mutex<OwnedCall>>>, | |
| finish: CancellationToken, | |
| finishes_turn: bool, | |
| } | |
| struct RetainedBatch { | |
| changes: Vec<Change>, | |
| contents: Vec<ContentBlob>, | |
| artifacts: Vec<maka_agent_facts::event::ProjectionArtifactWrite>, | |
| admission: bool, | |
| acknowledged: std::sync::atomic::AtomicBool, | |
| } | |
| struct OwnedReader { | |
| request: Arc<maka_host_runtime::application::NativeRequest>, | |
| signal_armed: bool, | |
| } | |
| struct OwnedCall { | |
| key: ExternalEffectKey, | |
| custody: Option<Box<dyn Send>>, | |
| execution_owned: bool, | |
| observing_prior: bool, | |
| worker_done: bool, | |
| physical: bool, | |
| retained: Arc<Mutex<Vec<Arc<RetainedBatch>>>>, | |
| readers: Vec<OwnedReader>, | |
| host: Option<RecoveryRef>, | |
| observation: Option<OperationReply>, | |
| } | |
| impl OwnedCall { | |
| fn has_settlement(&self) -> bool { | |
| !self.execution_owned | |
| || self | |
| .retained | |
| .lock() | |
| .expect("external T2 material") | |
| .iter() | |
| .any(|batch| { | |
| batch.changes.iter().any(|change| { | |
| matches!( | |
| &change.fact, | |
| Fact::ExternalEffect { | |
| event: ExternalEffectEvent::Observed { .. } | |
| | ExternalEffectEvent::Returned { .. } | |
| | ExternalEffectEvent::Rejected { .. } | |
| } | |
| ) | |
| }) | |
| }) | |
| } | |
| } | |
| impl NativeExternalEffects { | |
| pub(super) fn new( | |
| owner: Arc<Owner>, | |
| access: Arc<RunAccess>, | |
| claim: DriverClaim, | |
| cleanup: Evidence, | |
| workers: TaskTracker, | |
| cells: Option<Arc<super::driver_cells::RunCells>>, | |
| ) -> Result<Self, RunError> { | |
| let writer = owner | |
| .store | |
| .data_writer_for_work(access.custody().as_ref(), &access.execution().session_id)?; | |
| let state = owner.durable.with_data_writer(writer)?; | |
| Ok(Self { | |
| owner, | |
| state, | |
| access, | |
| claim, | |
| cleanup, | |
| workers, | |
| cells, | |
| observation: Arc::new(Mutex::new(None)), | |
| retained: Arc::new(Mutex::new(Vec::new())), | |
| calls: Arc::default(), | |
| active: None, | |
| finish: CancellationToken::new(), | |
| finishes_turn: false, | |
| }) | |
| } | |
| pub(super) fn has_retained(&self) -> bool { | |
| !self | |
| .calls | |
| .lock() | |
| .expect("original external sources") | |
| .is_empty() | |
| } | |
| pub(super) fn belongs_to(&self, access: &Arc<RunAccess>) -> bool { | |
| Arc::ptr_eq(&self.access, access) | |
| } | |
| fn key(&self, context: &ToolCallContext) -> Result<ExternalEffectKey, ToolError> { | |
| if context.execution != *self.access.execution() | |
| || self.claim.run() != context.execution.run_id | |
| || context | |
| .custody | |
| .as_ref() | |
| .is_none_or(|custody| !Arc::ptr_eq(custody, self.access.custody())) | |
| { | |
| return Err(ToolError::Failed( | |
| "External Host call lacks original work custody".into(), | |
| )); | |
| } | |
| let ToolOrigin::External { | |
| task, operation, .. | |
| } = &context.call.origin | |
| else { | |
| return Err(ToolError::Failed( | |
| "External Host call lacks a typed task obligation".into(), | |
| )); | |
| }; | |
| if context.operation_id != format!("external:{task}:{operation}") | |
| || context.call.tool_call_id != context.operation_id | |
| { | |
| return Err(ToolError::Failed( | |
| "External Host operation identity differs".into(), | |
| )); | |
| } | |
| Ok(ExternalEffectKey { | |
| task: *task, | |
| operation: *operation, | |
| }) | |
| } | |
| async fn current( | |
| &self, | |
| context: &ToolCallContext, | |
| key: ExternalEffectKey, | |
| name: &str, | |
| cancellation: &CancellationToken, | |
| ) -> Result<RunView, ToolError> { | |
| if self.cells.is_some() && matches!(name, "exec" | "wait") { | |
| self.access.check_context(context).await?; | |
| } else { | |
| self.access.check_tool(context, name).await?; | |
| } | |
| let view = self.access.fresh().await.map_err(ToolError::Failed)?; | |
| let task = view | |
| .external | |
| .as_ref() | |
| .ok_or_else(|| ToolError::Failed("External task is missing".into()))?; | |
| if task.task != key.task | |
| || !task.admitted | |
| || task.closed.is_some() | |
| || view.stopped | |
| || view.terminal.is_some() | |
| || cancellation.is_cancelled() | |
| { | |
| return Err(ToolError::Failed( | |
| "External task no longer admits Host work".into(), | |
| )); | |
| } | |
| Ok(view) | |
| } | |
| async fn invoke_original( | |
| &self, | |
| catalog: ToolCatalog, | |
| context: ToolCallContext, | |
| definition: ToolDefinition, | |
| input: Value, | |
| cancellation: CancellationToken, | |
| ) -> Result<ReturnedToolResult, ToolError> { | |
| let name = definition.name.clone(); | |
| let key = self.key(&context)?; | |
| let view = self.access.fresh().await.map_err(ToolError::Failed)?; | |
| let task = view | |
| .external | |
| .as_ref() | |
| .ok_or_else(|| ToolError::Failed("External task is missing".into()))?; | |
| if task.task != key.task || !task.admitted { | |
| return Err(ToolError::Failed( | |
| "External Host call has no admitted parent task".into(), | |
| )); | |
| } | |
| if let Some(effect) = self | |
| .state | |
| .external_effect(view.run, key) | |
| .await | |
| .map_err(persistence)? | |
| { | |
| if let Some(active) = &self.active { | |
| active.lock().await.observing_prior = true; | |
| } | |
| let descriptor = self | |
| .load_descriptor(self.claim.run(), effect.descriptor.clone()) | |
| .await?; | |
| if descriptor.schema_version != SCHEMA_VERSION || descriptor.protocol != PROTOCOL { | |
| self.block(key).await?; | |
| return Err(ToolError::OutcomeUnknown( | |
| "Original external Host effect protocol is unsupported".into(), | |
| )); | |
| } | |
| if descriptor.key != key | |
| || descriptor.execution != context.execution | |
| || descriptor.origin != context.call.origin | |
| || descriptor.operation_id != context.operation_id | |
| || descriptor.name != name | |
| || descriptor.input != input | |
| { | |
| return Err(ToolError::Failed( | |
| "External operation differs from its original descriptor".into(), | |
| )); | |
| } | |
| if let Some(rejected) = &effect.rejected { | |
| return Err(ToolError::Failed(rejected.diagnostic.clone())); | |
| } | |
| if let Some(output) = &effect.returned { | |
| return self | |
| .read_return(key, &descriptor.name, output.clone()) | |
| .await; | |
| } | |
| if effect.admitted { | |
| if self.reconcile_owned_original(key).await? { | |
| let original = self | |
| .state | |
| .external_effect(view.run, key) | |
| .await | |
| .map_err(persistence)? | |
| .ok_or_else(|| { | |
| ToolError::Persistence("Original external effect disappeared".into()) | |
| })?; | |
| if let Some(output) = original.returned { | |
| return self.read_return(key, &descriptor.name, output).await; | |
| } | |
| return Err(ToolError::OutcomeUnknown( | |
| "The original Host effect still owns its return publication".into(), | |
| )); | |
| } | |
| return self | |
| .recover(key, &descriptor, effect.reference.clone()) | |
| .await; | |
| } | |
| // A lost prepared owner is not restored from a current handler with the same name. | |
| self.block(key).await?; | |
| return Err(ToolError::OutcomeUnknown( | |
| "Original prepared Host effect is unavailable".into(), | |
| )); | |
| } | |
| let progress_hint = self | |
| .access | |
| .worker_progress() | |
| .map(|_| super::worker_progress::tool(&name, &input)); | |
| self.prepare_descriptor( | |
| &context, | |
| key, | |
| &catalog, | |
| definition, | |
| input.clone(), | |
| &cancellation, | |
| ) | |
| .await?; | |
| let prepared = if let Some(cells) = &self.cells | |
| && matches!(name.as_str(), "exec" | "wait") | |
| { | |
| cells | |
| .prepare_external( | |
| context.clone(), | |
| &name, | |
| &input, | |
| catalog, | |
| cancellation.clone(), | |
| ) | |
| .await | |
| } else { | |
| match catalog.semantics(&name) { | |
| Ok(semantics) => super::driver::PreparedTool::prepare( | |
| &self.owner, | |
| &self.access, | |
| &catalog, | |
| name.clone(), | |
| input, | |
| context.clone(), | |
| cancellation.clone(), | |
| semantics, | |
| ) | |
| .await | |
| .map(|prepared| prepared.effect), | |
| Err(rejection) => Err(rejection), | |
| } | |
| }; | |
| let effect = match prepared { | |
| Ok(effect) => effect, | |
| Err(rejection) => { | |
| let diagnostic = rejection.to_string(); | |
| self.observe( | |
| vec![change(ExternalEffectEvent::Rejected { | |
| key, | |
| rejection_kind: rejection.kind(), | |
| diagnostic: diagnostic.clone(), | |
| })], | |
| vec![], | |
| ) | |
| .await?; | |
| return Err(ToolError::Failed(diagnostic)); | |
| } | |
| }; | |
| if let Err(error) = self.current(&context, key, &name, &cancellation).await { | |
| self.observe( | |
| vec![change(ExternalEffectEvent::Rejected { | |
| key, | |
| rejection_kind: maka_agent_facts::tool_call::RejectionKind::PreparationFailed, | |
| diagnostic: error.to_string(), | |
| })], | |
| vec![], | |
| ) | |
| .await?; | |
| return Err(error); | |
| } | |
| // The prepared effect names its exact established interaction. The T1 | |
| // transaction consumes that grant against the original descriptor. | |
| let approval = effect.approval_id(); | |
| let display = effect.display(self.access.spec().language.tag()); | |
| let display = change(ExternalEffectEvent::DisplayRecorded { key, display }); | |
| let admitted = change(ExternalEffectEvent::Admitted { key, approval }); | |
| let revision = self | |
| .append( | |
| &context, | |
| key, | |
| &name, | |
| &cancellation, | |
| vec![display, admitted], | |
| vec![], | |
| ) | |
| .await?; | |
| if cancellation.is_cancelled() { | |
| return self | |
| .settle( | |
| key, | |
| &name, | |
| Err(ToolError::Failed( | |
| "External Host call cancelled before execution".into(), | |
| )), | |
| ToolSettlement::Rejected, | |
| ) | |
| .await; | |
| } | |
| let running = match effect.begin(revision, cancellation.clone()) { | |
| Ok(running) => running, | |
| Err(error) => { | |
| return self | |
| .settle(key, &name, Err(error), ToolSettlement::Rejected) | |
| .await; | |
| } | |
| }; | |
| if let Err(error) = self.current(&context, key, &name, &cancellation).await { | |
| return self | |
| .settle_owned(key, &name, Err(error), ToolSettlement::Rejected, running) | |
| .await; | |
| } | |
| let _progress = self | |
| .access | |
| .worker_progress() | |
| .zip(progress_hint) | |
| .map(|(run, hint)| run.track(context.operation_id.clone(), hint)); | |
| let (custody, execution) = running.execute(); | |
| let result = AssertUnwindSafe(execution) | |
| .catch_unwind() | |
| .await | |
| .unwrap_or_else(|_| { | |
| Err(ToolError::OutcomeUnknown( | |
| "External Host worker panicked".into(), | |
| )) | |
| }); | |
| self.settle_owned(key, &name, result, ToolSettlement::Failed, custody) | |
| .await | |
| } | |
| async fn settle_owned<G: Send + 'static>( | |
| &self, | |
| key: ExternalEffectKey, | |
| name: &str, | |
| result: Result<ToolSuccess, ToolError>, | |
| failure: ToolSettlement, | |
| custody: G, | |
| ) -> Result<ReturnedToolResult, ToolError> { | |
| let active = self | |
| .active | |
| .as_ref() | |
| .expect("original invocation owns its source"); | |
| { | |
| let mut source = active.lock().await; | |
| source.execution_owned = true; | |
| source.physical = match &result { | |
| Err(ToolError::OperationUnknown { observation, .. }) => { | |
| ended_unknown_reply(observation) | |
| } | |
| Err( | |
| ToolError::OutcomeUnknown(_) | |
| | ToolError::CleanupUnconfirmed(_) | |
| | ToolError::OperationCleanupUnconfirmed { .. } | |
| | ToolError::Persistence(_), | |
| ) => false, | |
| _ => true, | |
| }; | |
| source.custody = Some(Box::new(custody)); | |
| } | |
| // No shared-tracker task waits for its own Root settlement. The same | |
| // actual source and immutable T2 material stay in this Root-owned actor. | |
| self.settle(key, name, result, failure).await | |
| } | |
| async fn finish_request( | |
| &self, | |
| request: maka_host_runtime::application::NativeRequest, | |
| ) -> Result<(), ToolError> { | |
| if let Err(error) = request.finish().await { | |
| if let Some(active) = &self.active { | |
| active.lock().await.readers.push(OwnedReader { | |
| request: Arc::new(request), | |
| signal_armed: false, | |
| }); | |
| } | |
| return Err(ToolError::CleanupUnconfirmed(error)); | |
| } | |
| Ok(()) | |
| } | |
| async fn original_cleanup( | |
| &self, | |
| key: ExternalEffectKey, | |
| reference: Option<RecoveryRef>, | |
| ) -> Result<bool, ToolError> { | |
| let Some((reference, request)) = | |
| self.recovery_request(key, None, reference.as_ref()).await? | |
| else { | |
| return Ok(false); | |
| }; | |
| if let Some(active) = &self.active { | |
| active.lock().await.host = Some(reference.clone()); | |
| } | |
| let reply = match request.recover(reference.clone()) { | |
| Ok(original) => original | |
| .await | |
| .map(|observation| { | |
| observation.map(|observation| OperationReply { | |
| reference, | |
| receipt: Receipt::Recorded { | |
| outcome: observation.outcome, | |
| cleanup: observation.cleanup, | |
| }, | |
| }) | |
| }) | |
| .map_err(ToolError::OutcomeUnknown), | |
| Err(error) => Err(ToolError::OutcomeUnknown(error)), | |
| }; | |
| self.finish_request(request).await?; | |
| let Some(reply) = reply? else { | |
| return Ok(false); | |
| }; | |
| let confirmed = matches!( | |
| &reply.receipt, | |
| Receipt::Recorded { | |
| cleanup: OperationCleanup::Confirmed, | |
| .. | |
| } | |
| ); | |
| self.observe_reply(key, &reply).await?; | |
| if confirmed && let Some(active) = &self.active { | |
| active.lock().await.physical = true; | |
| } | |
| Ok(confirmed) | |
| } | |
| pub(super) async fn original_cleanup_signals(&self) -> Vec<BoxFuture<'static, ()>> { | |
| let calls = self | |
| .calls | |
| .lock() | |
| .expect("original external sources") | |
| .clone(); | |
| let mut signals: Vec<BoxFuture<'static, ()>> = Vec::new(); | |
| for call in calls { | |
| let Ok(mut call) = call.try_lock() else { | |
| continue; | |
| }; | |
| for reader in call | |
| .readers | |
| .iter_mut() | |
| .filter(|reader| !reader.signal_armed) | |
| { | |
| let resources = reader.request.scope().resources.clone(); | |
| reader.signal_armed = true; | |
| signals.push(Box::pin(async move { | |
| let _historical_result = resources.settle().await; | |
| })); | |
| } | |
| } | |
| signals | |
| } | |
| pub(super) async fn original_cleanup_confirmed(&self) -> Result<bool, RunError> { | |
| let calls = self | |
| .calls | |
| .lock() | |
| .expect("original external sources") | |
| .clone(); | |
| let mut first_error = None; | |
| let mut confirmed = true; | |
| for call in calls { | |
| match self.settle_call(&call).await { | |
| Ok(true) => self | |
| .calls | |
| .lock() | |
| .expect("original external sources") | |
| .retain(|owned| !Arc::ptr_eq(owned, &call)), | |
| Ok(false) => confirmed = false, | |
| Err(error) => { | |
| first_error.get_or_insert(RunError::Tool(error)); | |
| confirmed = false; | |
| } | |
| } | |
| } | |
| if let Some(error) = first_error { | |
| return Err(error); | |
| } | |
| Ok(confirmed | |
| && self | |
| .calls | |
| .lock() | |
| .expect("original external sources") | |
| .is_empty()) | |
| } | |
| async fn settle_call( | |
| &self, | |
| call: &Arc<tokio::sync::Mutex<OwnedCall>>, | |
| ) -> Result<bool, ToolError> { | |
| let (key, mut physical, retained, readers, host) = { | |
| let source = call.lock().await; | |
| if !source.worker_done { | |
| return Ok(false); | |
| } | |
| ( | |
| source.key, | |
| source.physical, | |
| source.retained.clone(), | |
| source | |
| .readers | |
| .iter() | |
| .map(|reader| reader.request.clone()) | |
| .collect::<Vec<_>>(), | |
| source.host.clone(), | |
| ) | |
| }; | |
| for reader in readers { | |
| if reader.scope().resources.has_pending() { | |
| return Ok(false); | |
| } | |
| reader | |
| .finish() | |
| .await | |
| .map_err(ToolError::CleanupUnconfirmed)?; | |
| call.lock() | |
| .await | |
| .readers | |
| .retain(|owned| !Arc::ptr_eq(&owned.request, &reader)); | |
| } | |
| let mut actor = self.clone(); | |
| actor.active = Some(call.clone()); | |
| actor.retained = retained.clone(); | |
| let batches = retained.lock().expect("external T2 material").clone(); | |
| for batch in batches { | |
| actor.settle_batch(&batch).await?; | |
| } | |
| // A physical ACK does not prove that the settlement batch was captured. | |
| let has_settlement = call.lock().await.has_settlement(); | |
| if !physical || !has_settlement { | |
| physical = actor.original_cleanup(key, host).await? || physical; | |
| } | |
| let batches = retained.lock().expect("external T2 material").clone(); | |
| for batch in batches { | |
| actor.settle_batch(&batch).await?; | |
| } | |
| let custody = { | |
| let mut source = call.lock().await; | |
| if !physical || !source.readers.is_empty() || !source.has_settlement() { | |
| return Ok(false); | |
| } | |
| source.custody.take() | |
| }; | |
| drop(custody); | |
| Ok(true) | |
| } | |
| async fn settle_batch(&self, batch: &RetainedBatch) -> Result<(), ToolError> { | |
| use std::sync::atomic::Ordering; | |
| if batch.acknowledged.load(Ordering::SeqCst) { | |
| return Ok(()); | |
| } | |
| loop { | |
| let publication = self | |
| .state | |
| .publication( | |
| self.claim.run(), | |
| batch.changes.iter().map(|change| change.id).collect(), | |
| None, | |
| ) | |
| .await | |
| .map_err(persistence)?; | |
| if !publication.facts.is_empty() { | |
| if publication.facts.len() != batch.changes.len() | |
| || !publication | |
| .facts | |
| .iter() | |
| .zip(&batch.changes) | |
| .all(|(fact, change)| { | |
| fact.id == change.id | |
| && fact.run == self.claim.run() | |
| && fact.schema_version == SCHEMA_VERSION | |
| && fact.at == change.at | |
| && fact.fact == change.fact | |
| }) | |
| { | |
| return Err(persistence(StoreError::EventConflict)); | |
| } | |
| batch.acknowledged.store(true, Ordering::SeqCst); | |
| return Ok(()); | |
| } | |
| // The original invocation has ended. Absent preparation/admission | |
| // cannot be revived as a fresh grant by its cleanup observer. | |
| if batch.admission { | |
| return Ok(()); | |
| } | |
| match self | |
| .state | |
| .observe_artifacts( | |
| self.claim.run(), | |
| batch.changes.clone(), | |
| batch.contents.clone(), | |
| batch.artifacts.clone(), | |
| ) | |
| .await | |
| { | |
| Ok(_) => { | |
| batch.acknowledged.store(true, Ordering::SeqCst); | |
| return Ok(()); | |
| } | |
| Err(error) | |
| if matches!( | |
| error.underlying(), | |
| StoreError::CommitUnknown(_) | StoreError::OperationUnknown | |
| ) => {} | |
| Err(error) => return Err(persistence(error)), | |
| } | |
| } | |
| } | |
| fn retain( | |
| &self, | |
| changes: &[Change], | |
| contents: &[ContentBlob], | |
| artifacts: &[maka_agent_facts::event::ProjectionArtifactWrite], | |
| ) -> Arc<RetainedBatch> { | |
| self.retain_batch(changes, contents, artifacts, false) | |
| } | |
| fn retain_batch( | |
| &self, | |
| changes: &[Change], | |
| contents: &[ContentBlob], | |
| artifacts: &[maka_agent_facts::event::ProjectionArtifactWrite], | |
| admission: bool, | |
| ) -> Arc<RetainedBatch> { | |
| let batch = Arc::new(RetainedBatch { | |
| changes: changes.to_vec(), | |
| contents: contents.to_vec(), | |
| artifacts: artifacts.to_vec(), | |
| admission, | |
| acknowledged: std::sync::atomic::AtomicBool::new(false), | |
| }); | |
| self.retained | |
| .lock() | |
| .expect("external T2 material") | |
| .push(batch.clone()); | |
| batch | |
| } | |
| async fn reconcile_owned_original(&self, key: ExternalEffectKey) -> Result<bool, ToolError> { | |
| let calls = self | |
| .calls | |
| .lock() | |
| .expect("original external sources") | |
| .clone(); | |
| let mut owned = false; | |
| for call in calls { | |
| if self | |
| .active | |
| .as_ref() | |
| .is_some_and(|active| Arc::ptr_eq(active, &call)) | |
| { | |
| continue; | |
| } | |
| let batches = { | |
| let source = call.lock().await; | |
| if source.key != key || source.observing_prior { | |
| continue; | |
| } | |
| owned = true; | |
| source | |
| .retained | |
| .lock() | |
| .expect("external T2 material") | |
| .clone() | |
| }; | |
| for batch in batches { | |
| self.settle_batch(&batch).await?; | |
| } | |
| } | |
| Ok(owned) | |
| } | |
| pub(super) async fn prepare_descriptor( | |
| &self, | |
| context: &ToolCallContext, | |
| key: ExternalEffectKey, | |
| catalog: &ToolCatalog, | |
| definition: ToolDefinition, | |
| input: Value, | |
| cancellation: &CancellationToken, | |
| ) -> Result<ContentRef, ToolError> { | |
| let name = definition.name.clone(); | |
| let descriptor = ExternalEffectDescriptor { | |
| schema_version: SCHEMA_VERSION, | |
| protocol: PROTOCOL.into(), | |
| execution: context.execution.clone(), | |
| origin: context.call.origin.clone(), | |
| key, | |
| operation_id: context.operation_id.clone(), | |
| name: name.clone(), | |
| input, | |
| definition, | |
| catalog_digest: catalog.digest(), | |
| // Tool calls have no attached Host resource slots. | |
| resources: vec![], | |
| }; | |
| let definition_blob = ContentBlob::json(&descriptor.definition).map_err(persistence)?; | |
| let stored = StoredExternalEffectDescriptor::with_definition_ref( | |
| &descriptor, | |
| definition_blob.reference().clone(), | |
| ); | |
| let blob = ContentBlob::json(&stored).map_err(persistence)?; | |
| let reference = blob.reference().clone(); | |
| let prepared = change(ExternalEffectEvent::Prepared { | |
| key, | |
| descriptor: reference.clone(), | |
| }); | |
| self.append( | |
| context, | |
| key, | |
| &name, | |
| cancellation, | |
| vec![prepared], | |
| vec![blob, definition_blob], | |
| ) | |
| .await?; | |
| Ok(reference) | |
| } | |
| async fn load_descriptor( | |
| &self, | |
| run: RunId, | |
| reference: ContentRef, | |
| ) -> Result<ExternalEffectDescriptor, ToolError> { | |
| let bytes = self | |
| .state | |
| .content(run, reference) | |
| .await | |
| .map_err(persistence)?; | |
| let stored: StoredExternalEffectDescriptor = | |
| serde_json::from_slice(&bytes).map_err(persistence)?; | |
| let definition = match stored.definition { | |
| StoredToolDefinition::Inline(definition) => *definition, | |
| StoredToolDefinition::Ref(definition_ref) => { | |
| let definition_bytes = self | |
| .state | |
| .content(run, definition_ref) | |
| .await | |
| .map_err(persistence)?; | |
| serde_json::from_slice(&definition_bytes).map_err(persistence)? | |
| } | |
| }; | |
| Ok(ExternalEffectDescriptor { | |
| schema_version: stored.schema_version, | |
| protocol: stored.protocol, | |
| execution: stored.execution, | |
| key: stored.key, | |
| origin: stored.origin, | |
| operation_id: stored.operation_id, | |
| name: stored.name, | |
| input: stored.input, | |
| definition, | |
| catalog_digest: stored.catalog_digest, | |
| resources: stored.resources, | |
| }) | |
| } | |
| pub(super) async fn read_return( | |
| &self, | |
| key: ExternalEffectKey, | |
| name: &str, | |
| reference: ContentRef, | |
| ) -> Result<ReturnedToolResult, ToolError> { | |
| let run = self.claim.run(); | |
| let id = returned_fact_id(run, key); | |
| let original = self | |
| .state | |
| .fact_barrier(run, id) | |
| .await | |
| .map_err(persistence)? | |
| .ok_or_else(|| { | |
| ToolError::Persistence( | |
| "Original canonical external return source is unavailable".into(), | |
| ) | |
| })?; | |
| if original.run != run | |
| || original.id != id | |
| || original.schema_version != SCHEMA_VERSION | |
| || !matches!(&original.fact, Fact::ExternalEffect { | |
| event: ExternalEffectEvent::Returned { key: original_key, output, .. } | |
| } if *original_key == key && *output == reference) | |
| { | |
| return Err(persistence(StoreError::EventConflict)); | |
| } | |
| let returned = self | |
| .state | |
| .read_external_return(run, reference) | |
| .await | |
| .map_err(persistence)?; | |
| let name = name.to_owned(); | |
| tokio::task::spawn_blocking(move || render_return(id, key, &name, returned)) | |
| .await | |
| .map_err(|error| { | |
| ToolError::Persistence(format!("Original return reader failed: {error}")) | |
| })? | |
| } | |
| async fn append( | |
| &self, | |
| context: &ToolCallContext, | |
| key: ExternalEffectKey, | |
| name: &str, | |
| cancellation: &CancellationToken, | |
| changes: Vec<Change>, | |
| contents: Vec<ContentBlob>, | |
| ) -> Result<u64, ToolError> { | |
| let batch = self.retain_batch(&changes, &contents, &[], true); | |
| loop { | |
| let view = self.current(context, key, name, cancellation).await?; | |
| match self | |
| .state | |
| .append( | |
| &self.claim, | |
| Head::from(&view), | |
| changes.clone(), | |
| contents.clone(), | |
| ) | |
| .await | |
| { | |
| Ok(commit) => { | |
| batch | |
| .acknowledged | |
| .store(true, std::sync::atomic::Ordering::SeqCst); | |
| return Ok(commit | |
| .facts | |
| .last() | |
| .expect("nonempty external effect transaction") | |
| .revision); | |
| } | |
| Err(error) if matches!(error.underlying(), StoreError::RevisionConflict { .. }) => { | |
| continue; | |
| } | |
| Err(error) => return self.reconcile(&changes, error).await, | |
| } | |
| } | |
| } | |
| async fn reconcile(&self, changes: &[Change], error: StoreError) -> Result<u64, ToolError> { | |
| if !matches!( | |
| error.underlying(), | |
| StoreError::CommitUnknown(_) | StoreError::OperationUnknown | |
| ) { | |
| return Err(persistence(error)); | |
| } | |
| let publication = self | |
| .state | |
| .publication( | |
| self.claim.run(), | |
| changes.iter().map(|change| change.id).collect(), | |
| None, | |
| ) | |
| .await | |
| .map_err(persistence)?; | |
| if publication.facts.is_empty() { | |
| return Err(ToolError::OutcomeUnknown(error.to_string())); | |
| } | |
| if publication.facts.len() != changes.len() | |
| || !publication.facts.iter().zip(changes).all(|(fact, change)| { | |
| fact.id == change.id | |
| && fact.run == self.claim.run() | |
| && fact.schema_version == SCHEMA_VERSION | |
| && fact.at == change.at | |
| && fact.fact == change.fact | |
| }) | |
| { | |
| return Err(persistence(StoreError::EventConflict)); | |
| } | |
| Ok(publication | |
| .facts | |
| .last() | |
| .expect("nonempty original batch") | |
| .revision) | |
| } | |
| fn deliver(&self, result: Result<ReturnedToolResult, ToolError>) { | |
| if let Some(reply) = self | |
| .observation | |
| .lock() | |
| .expect("external effect observation") | |
| .take() | |
| { | |
| let _ = reply.send(result); | |
| } | |
| } | |
| async fn observe( | |
| &self, | |
| changes: Vec<Change>, | |
| contents: Vec<ContentBlob>, | |
| ) -> Result<(), ToolError> { | |
| let batch = self.retain(&changes, &contents, &[]); | |
| let result = match self | |
| .state | |
| .observe(self.claim.run(), changes.clone(), contents.clone()) | |
| .await | |
| { | |
| Ok(_) => Ok(()), | |
| Err(error) => self.reconcile(&changes, error).await.map(|_| ()), | |
| }; | |
| if result.is_ok() { | |
| batch | |
| .acknowledged | |
| .store(true, std::sync::atomic::Ordering::SeqCst); | |
| } | |
| result | |
| } | |
| async fn block(&self, key: ExternalEffectKey) -> Result<(), ToolError> { | |
| // A late producer may publish evidence, but cannot make policy for a new driver. | |
| let event = Change::new( | |
| Fact::Blocked { | |
| blocker: BlockerId::new(), | |
| reason: BlockerKind::ExternalEffectUnknown { | |
| task: key.task, | |
| operation: key.operation, | |
| }, | |
| }, | |
| at(), | |
| ); | |
| let batch = self.retain_batch(std::slice::from_ref(&event), &[], &[], true); | |
| loop { | |
| let view = match self.access.fresh().await { | |
| Ok(view) => view, | |
| Err(_) => return Ok(()), | |
| }; | |
| if view.stopped | |
| || view.terminal.is_some() | |
| || view.blockers.values().any(|blocker| { | |
| blocker.reason | |
| == BlockerKind::ExternalEffectUnknown { | |
| task: key.task, | |
| operation: key.operation, | |
| } | |
| && blocker.decision.is_none() | |
| && blocker.resolved_by.is_none() | |
| }) | |
| { | |
| return Ok(()); | |
| } | |
| match self | |
| .state | |
| .append(&self.claim, Head::from(&view), vec![event.clone()], vec![]) | |
| .await | |
| { | |
| Ok(_) => { | |
| batch | |
| .acknowledged | |
| .store(true, std::sync::atomic::Ordering::SeqCst); | |
| return Ok(()); | |
| } | |
| Err(error) if matches!(error.underlying(), StoreError::RevisionConflict { .. }) => { | |
| continue; | |
| } | |
| Err(error) => return self.reconcile(&[event], error).await.map(|_| ()), | |
| } | |
| } | |
| } | |
| async fn recovery_request( | |
| &self, | |
| key: ExternalEffectKey, | |
| expected_descriptor: Option<&ExternalEffectDescriptor>, | |
| expected_reference: Option<&RecoveryRef>, | |
| ) -> Result<Option<(RecoveryRef, maka_host_runtime::application::NativeRequest)>, ToolError> | |
| { | |
| let execution = self.access.execution(); | |
| let view = self | |
| .state | |
| .frontier(self.claim.run()) | |
| .await | |
| .map_err(persistence)?; | |
| if view.run != execution.run_id | |
| || view.spec != *self.access.spec() | |
| || view.spec.session_id != execution.session_id | |
| || view.spec.turn_id != execution.turn_id | |
| || view | |
| .external | |
| .as_ref() | |
| .is_none_or(|task| task.task != key.task || !task.admitted) | |
| { | |
| return Err(ToolError::OutcomeUnknown( | |
| "Original external parent task is unavailable".into(), | |
| )); | |
| } | |
| let effect = self | |
| .state | |
| .external_effect(view.run, key) | |
| .await | |
| .map_err(persistence)? | |
| .ok_or_else(|| { | |
| ToolError::OutcomeUnknown("Original external effect is unavailable".into()) | |
| })?; | |
| if !effect.admitted || effect.rejected.is_some() { | |
| return Err(ToolError::OutcomeUnknown( | |
| "Original external effect has no admission".into(), | |
| )); | |
| } | |
| let descriptor = self.load_descriptor(view.run, effect.descriptor).await?; | |
| let operation_id = format!("external:{}:{}", key.task, key.operation); | |
| if descriptor.schema_version != SCHEMA_VERSION | |
| || descriptor.protocol != PROTOCOL | |
| || descriptor.execution != *execution | |
| || descriptor.key != key | |
| || !matches!(&descriptor.origin, ToolOrigin::External { task, operation, .. } | |
| if *task == key.task && *operation == key.operation) | |
| || descriptor.operation_id != operation_id | |
| || descriptor.definition.name != descriptor.name | |
| || !descriptor.resources.is_empty() | |
| || expected_descriptor.is_some_and(|expected| expected != &descriptor) | |
| { | |
| return Err(ToolError::OutcomeUnknown( | |
| "Original external effect descriptor differs".into(), | |
| )); | |
| } | |
| let selection = self | |
| .owner | |
| .selection_snapshot(&self.owner.durable, view.run, &view.spec.tool_selection) | |
| .await | |
| .map_err(persistence)?; | |
| if !selection.definitions.contains(&descriptor.definition) { | |
| return Err(ToolError::OutcomeUnknown( | |
| "Original external tool definition changed".into(), | |
| )); | |
| } | |
| let selected = selection | |
| .selected_service( | |
| &self.owner.services, | |
| &self.owner.tool_basis, | |
| &descriptor.name, | |
| ) | |
| .map_err(ToolError::OutcomeUnknown)? | |
| .ok_or_else(|| { | |
| ToolError::OutcomeUnknown( | |
| "Original external service selection is unavailable".into(), | |
| ) | |
| })?; | |
| let operation = crate::tools::operation_uuid(execution, &operation_id); | |
| if effect | |
| .reference | |
| .as_ref() | |
| .is_some_and(|reference| reference.operation != operation) | |
| || expected_reference.is_some_and(|reference| { | |
| reference.operation != operation | |
| || effect | |
| .reference | |
| .as_ref() | |
| .is_some_and(|original| original != reference) | |
| }) | |
| { | |
| return Err(ToolError::OutcomeUnknown( | |
| "Original external reference differs".into(), | |
| )); | |
| } | |
| let request = selected | |
| .1 | |
| .request_with_work( | |
| Identity::Agent { | |
| execution: execution.clone(), | |
| operation_id: Some(operation_id.clone()), | |
| }, | |
| CancellationToken::new(), | |
| self.access.custody().clone(), | |
| ) | |
| .and_then(|request| { | |
| request.with_sandbox_floor(&self.access.spec().permission_ceiling.sandbox) | |
| }) | |
| .map_err(ToolError::OutcomeUnknown)?; | |
| let intent = match request.lookup_intent(operation) { | |
| Ok(observer) => observer.await, | |
| Err(error) => Err(error), | |
| }; | |
| let validated = (|| { | |
| let Some(intent) = intent.map_err(ToolError::OutcomeUnknown)? else { | |
| return if effect.reference.is_some() || expected_reference.is_some() { | |
| Err(ToolError::OutcomeUnknown( | |
| "Original external caller intent is unavailable".into(), | |
| )) | |
| } else { | |
| Ok(None) | |
| }; | |
| }; | |
| if intent.reference.operation != operation | |
| || effect | |
| .reference | |
| .as_ref() | |
| .is_some_and(|reference| reference != &intent.reference) | |
| || expected_reference.is_some_and(|reference| reference != &intent.reference) | |
| { | |
| return Err(ToolError::OutcomeUnknown( | |
| "External caller intent differs from original reference".into(), | |
| )); | |
| } | |
| selection | |
| .validate_recovery_service( | |
| &self.owner.services, | |
| &self.owner.tool_basis, | |
| &intent, | |
| execution, | |
| &operation_id, | |
| &selected, | |
| ) | |
| .map_err(ToolError::OutcomeUnknown)?; | |
| // The external bridge accepts only JSON service inputs. Decode the | |
| // same contract projection used by the original Service preparer. | |
| let declaration: crate::contract::ServiceTool = | |
| serde_json::from_value(intent.recovery["selection"].clone()) | |
| .map_err(persistence)?; | |
| if declaration.input != crate::contract::ToolInput::Json { | |
| return Err(ToolError::OutcomeUnknown( | |
| "External original is not a JSON service input".into(), | |
| )); | |
| } | |
| let binding = self | |
| .owner | |
| .services | |
| .binding(&declaration.target) | |
| .map_err(ToolError::OutcomeUnknown)?; | |
| let maka_plugin_core::contract::Interface::Service { methods } = | |
| &binding.contract().interface | |
| else { | |
| return Err(ToolError::OutcomeUnknown( | |
| "External original service contract changed".into(), | |
| )); | |
| }; | |
| let method = methods.get(&declaration.method).ok_or_else(|| { | |
| ToolError::OutcomeUnknown("External original service method changed".into()) | |
| })?; | |
| let (projection, _) = | |
| maka_tool_catalog::input::Projection::project(method.input.as_ref().clone()); | |
| let input = projection | |
| .decode(descriptor.input.clone()) | |
| .map_err(|error| ToolError::OutcomeUnknown(error.to_string()))?; | |
| if intent.recovery.get("input") != Some(&input) { | |
| return Err(ToolError::OutcomeUnknown( | |
| "External original input differs from caller intent".into(), | |
| )); | |
| } | |
| Ok(Some(intent.reference)) | |
| })(); | |
| match validated { | |
| Ok(Some(reference)) => Ok(Some((reference, request))), | |
| result => { | |
| self.finish_request(request).await?; | |
| result.map(|_| None) | |
| } | |
| } | |
| } | |
| async fn recover( | |
| &self, | |
| key: ExternalEffectKey, | |
| descriptor: &ExternalEffectDescriptor, | |
| reference: Option<RecoveryRef>, | |
| ) -> Result<ReturnedToolResult, ToolError> { | |
| let (reference, request) = match self | |
| .recovery_request(key, Some(descriptor), reference.as_ref()) | |
| .await | |
| { | |
| Ok(Some(original)) => original, | |
| Ok(None) => { | |
| self.block(key).await?; | |
| return Err(ToolError::OutcomeUnknown( | |
| "Original admitted Host effect has no observable reference; it will not be reexecuted".into(), | |
| )); | |
| } | |
| Err(error) => { | |
| self.block(key).await?; | |
| return Err(error); | |
| } | |
| }; | |
| let original = match request.recover(reference.clone()) { | |
| Ok(observer) => observer.await, | |
| Err(error) => Err(error), | |
| }; | |
| let finished = self.finish_request(request).await; | |
| let reply = OperationReply { | |
| reference, | |
| receipt: match original { | |
| Ok(Some(observation)) => Receipt::Recorded { | |
| outcome: observation.outcome, | |
| cleanup: observation.cleanup, | |
| }, | |
| Ok(None) => Receipt::NotRecorded, | |
| Err(diagnostic) => Receipt::Unavailable { diagnostic }, | |
| }, | |
| }; | |
| self.observe_reply(key, &reply).await?; | |
| if let Err(failure) = finished { | |
| let error = ToolError::OperationCleanupUnconfirmed { | |
| message: failure.to_string(), | |
| observation: Box::new(reply), | |
| }; | |
| self.cleanup.observe_tool(&error); | |
| self.block(key).await?; | |
| return Err(error); | |
| } | |
| if let Some(error) = operation_return_error(&reply) { | |
| self.cleanup.observe_tool(&error); | |
| if !ended_unknown_reply(&reply) { | |
| self.block(key).await?; | |
| } | |
| return Err(error); | |
| } | |
| let original = self | |
| .state | |
| .external_effect(self.claim.run(), key) | |
| .await | |
| .map_err(persistence)? | |
| .ok_or_else(|| ToolError::Persistence("Original external effect disappeared".into()))?; | |
| if let Some(reference) = original.returned { | |
| return self.read_return(key, &descriptor.name, reference).await; | |
| } | |
| Err(ToolError::Persistence( | |
| "Original canonical external return source is unavailable; receipt observation does not republish business output".into(), | |
| )) | |
| } | |
| async fn observe_reply( | |
| &self, | |
| key: ExternalEffectKey, | |
| reply: &OperationReply, | |
| ) -> Result<(), ToolError> { | |
| if let Some(active) = &self.active { | |
| let mut source = active.lock().await; | |
| // A successful tool wrapper is not the provider's physical ACK. | |
| if source.execution_owned | |
| && !matches!( | |
| &reply.receipt, | |
| Receipt::Recorded { | |
| cleanup: OperationCleanup::Confirmed, | |
| .. | |
| } | |
| ) | |
| { | |
| source.physical = false; | |
| } | |
| if source.observation.as_ref() == Some(reply) { | |
| return Ok(()); | |
| } | |
| source.host = Some(reply.reference.clone()); | |
| } | |
| let original = self | |
| .state | |
| .external_effect(self.claim.run(), key) | |
| .await | |
| .map_err(persistence)? | |
| .ok_or_else(|| ToolError::Persistence("Original external effect disappeared".into()))?; | |
| let mut changes = Vec::new(); | |
| if original.reference.is_none() { | |
| changes.push(change(ExternalEffectEvent::Bound { | |
| key, | |
| reference: reply.reference.clone(), | |
| })); | |
| } | |
| if original.observation.as_ref() != Some(reply) { | |
| changes.push(change(ExternalEffectEvent::Observed { | |
| key, | |
| observation: reply.clone(), | |
| })); | |
| } | |
| if let Receipt::Recorded { cleanup, .. } = &reply.receipt | |
| && original.cleanup != *cleanup | |
| { | |
| changes.push(change(ExternalEffectEvent::CleanupObserved { | |
| key, | |
| cleanup: cleanup.clone(), | |
| })); | |
| } | |
| if !changes.is_empty() { | |
| self.observe(changes, vec![]).await?; | |
| } | |
| if let Some(active) = &self.active { | |
| active.lock().await.observation = Some(reply.clone()); | |
| } | |
| Ok(()) | |
| } | |
| pub(super) async fn settle( | |
| &self, | |
| key: ExternalEffectKey, | |
| name: &str, | |
| result: Result<ToolSuccess, ToolError>, | |
| failure: ToolSettlement, | |
| ) -> Result<ReturnedToolResult, ToolError> { | |
| match result { | |
| Ok(success) => { | |
| let execution = self.access.execution().clone(); | |
| let run = self.claim.run(); | |
| let id = returned_fact_id(run, key); | |
| let name = name.to_owned(); | |
| let (blob, artifacts, output, is_error, display_budget_chars, projection) = | |
| tokio::task::spawn_blocking(move || { | |
| let normalized = success | |
| .into_durable( | |
| &format!("external:{}:{}", key.task, key.operation), | |
| SystemTime::now(), | |
| &execution, | |
| ) | |
| .map_err(|message| ToolError::OutcomeUnknown(message.into()))?; | |
| // Strip inline observation and duplicate text projection only in the | |
| // persisted wire copy for Completed cells; return the full unstripped | |
| // output to live HostEffects::invoke callers. | |
| let published_artifacts: Vec<_> = normalized | |
| .artifacts | |
| .iter() | |
| .map(|write| { | |
| let evidence = write.evidence(); | |
| maka_agent_facts::artifact::PublishedArtifact { | |
| artifact: write.artifact().clone(), | |
| payload: ContentRef { | |
| digest: evidence.digest, | |
| bytes: evidence.bytes, | |
| }, | |
| } | |
| }) | |
| .collect(); | |
| let stored = | |
| maka_agent_facts::external_effects::StoredExternalReturn::from_output( | |
| &normalized.output, | |
| normalized.is_error, | |
| &normalized.projection, | |
| published_artifacts, | |
| normalized.display_budget_chars, | |
| ); | |
| let blob = ContentBlob::json(&stored).map_err(persistence)?; | |
| let projection = display_projection( | |
| id, | |
| key, | |
| &name, | |
| normalized.projection, | |
| normalized.display_budget_chars, | |
| ); | |
| Ok::<_, ToolError>(( | |
| blob, | |
| normalized.artifacts, | |
| normalized.output, | |
| normalized.is_error, | |
| normalized.display_budget_chars, | |
| projection, | |
| )) | |
| }) | |
| .await | |
| .map_err(|error| { | |
| ToolError::OutcomeUnknown(format!( | |
| "Original return preparation failed: {error}" | |
| )) | |
| })??; | |
| if let ToolOutput::Operation(reply) = &output { | |
| self.observe_reply(key, reply).await?; | |
| if let Some(error) = operation_return_error(reply) { | |
| self.cleanup.observe_tool(&error); | |
| if !ended_unknown_reply(reply) { | |
| self.block(key).await?; | |
| } | |
| return Err(error); | |
| } | |
| } | |
| self.publish_return_blob(key, blob, artifacts, self.finishes_turn && !is_error) | |
| .await?; | |
| Ok(ReturnedToolResult { | |
| output, | |
| is_error, | |
| projection: projection?, | |
| loaded_tools: Default::default(), | |
| display_budget_chars, | |
| }) | |
| } | |
| Err( | |
| error @ (ToolError::OperationUnknown { .. } | |
| | ToolError::OperationCleanupUnconfirmed { .. }), | |
| ) => { | |
| let observation = match &error { | |
| ToolError::OperationUnknown { observation, .. } | |
| | ToolError::OperationCleanupUnconfirmed { observation, .. } => observation, | |
| _ => unreachable!(), | |
| }; | |
| self.observe_reply(key, observation).await?; | |
| self.cleanup.observe_tool(&error); | |
| if !matches!( | |
| &error, | |
| ToolError::OperationUnknown { observation, .. } | |
| if ended_unknown_reply(observation) | |
| ) { | |
| self.block(key).await?; | |
| } | |
| Err(error) | |
| } | |
| Err( | |
| error @ (ToolError::OutcomeUnknown(_) | |
| | ToolError::CleanupUnconfirmed(_) | |
| | ToolError::Persistence(_)), | |
| ) => { | |
| self.cleanup.observe_tool(&error); | |
| if matches!(error, ToolError::OutcomeUnknown(_)) { | |
| self.cleanup.observe_tool(&ToolError::CleanupUnconfirmed( | |
| "External Host worker has no physical cleanup proof".into(), | |
| )); | |
| } | |
| self.block(key).await?; | |
| Err(error) | |
| } | |
| Err(error) => { | |
| let (blob, error) = tokio::task::spawn_blocking(move || { | |
| let returned = ExternalReturn::Failure { | |
| settlement: failure, | |
| diagnostic: match &error { | |
| ToolError::Failed(diagnostic) => diagnostic.clone(), | |
| _ => error.to_string(), | |
| }, | |
| }; | |
| let blob = ContentBlob::json(&returned).map_err(persistence)?; | |
| Ok::<_, ToolError>((blob, error)) | |
| }) | |
| .await | |
| .map_err(|error| { | |
| ToolError::OutcomeUnknown(format!( | |
| "Original failure preparation failed: {error}" | |
| )) | |
| })??; | |
| self.publish_return_blob(key, blob, vec![], false).await?; | |
| Err(error) | |
| } | |
| } | |
| } | |
| async fn publish_return_blob( | |
| &self, | |
| key: ExternalEffectKey, | |
| blob: ContentBlob, | |
| artifacts: Vec<maka_agent_facts::event::ProjectionArtifactWrite>, | |
| finish_turn: bool, | |
| ) -> Result<(), ToolError> { | |
| let changes = vec![ | |
| change(ExternalEffectEvent::CleanupObserved { | |
| key, | |
| cleanup: OperationCleanup::Confirmed, | |
| }), | |
| Change { | |
| id: returned_fact_id(self.claim.run(), key), | |
| at: at(), | |
| fact: Fact::ExternalEffect { | |
| event: ExternalEffectEvent::Returned { | |
| key, | |
| output: blob.reference().clone(), | |
| finish_turn, | |
| }, | |
| }, | |
| }, | |
| ]; | |
| let batch = self.retain(&changes, std::slice::from_ref(&blob), &artifacts); | |
| let committed = match self | |
| .state | |
| .observe_artifacts( | |
| self.claim.run(), | |
| changes.clone(), | |
| vec![blob.clone()], | |
| artifacts.clone(), | |
| ) | |
| .await | |
| { | |
| Ok(_) => Ok(()), | |
| Err(error) => self.reconcile(&changes, error).await.map(|_| ()), | |
| }; | |
| if committed.is_ok() { | |
| if finish_turn { | |
| self.finish.cancel(); | |
| } | |
| batch | |
| .acknowledged | |
| .store(true, std::sync::atomic::Ordering::SeqCst); | |
| } | |
| drop((blob, artifacts)); | |
| committed | |
| } | |
| } | |
| impl HostEffects for NativeExternalEffects { | |
| fn finish_signal(&self) -> CancellationToken { | |
| self.finish.clone() | |
| } | |
| fn content<'a>( | |
| &'a self, | |
| returned: &'a ReturnedToolResult, | |
| cancellation: &'a CancellationToken, | |
| ) -> BoxFuture<'a, Result<Vec<maka_model::prompt::ContentPart>, String>> { | |
| use maka_agent_engine::preparation::{MediaSource, project_returned_tool_from}; | |
| use maka_agent_state::artifacts::ArtifactScope; | |
| use maka_model::prompt::ToolOutput; | |
| Box::pin(async move { | |
| let spec = self.access.spec(); | |
| let artifacts = self.state.artifacts(); | |
| let view = self | |
| .state | |
| .frontier(self.claim.run()) | |
| .await | |
| .map_err(|error| error.to_string())?; | |
| let media = MediaSource::LiveWithSession { | |
| artifacts: &artifacts, | |
| run: ArtifactScope { | |
| owner: spec.owner.clone(), | |
| session: spec.session_id.clone(), | |
| run: view.run, | |
| through: view.revision, | |
| }, | |
| }; | |
| let rendered = project_returned_tool_from( | |
| media, | |
| returned, | |
| spec.language, | |
| true, | |
| &self.owner.services.model_preparation_resources(), | |
| cancellation, | |
| ) | |
| .await | |
| .map_err(|error| error.to_string())?; | |
| match rendered { | |
| ToolOutput::Content(parts) => Ok(parts), | |
| _ => unreachable!("a media projection renders as content"), | |
| } | |
| }) | |
| } | |
| fn invoke( | |
| &self, | |
| catalog: ToolCatalog, | |
| context: ToolCallContext, | |
| definition: ToolDefinition, | |
| input: Value, | |
| cancellation: CancellationToken, | |
| ) -> BoxFuture<'static, Result<ReturnedToolResult, ToolError>> { | |
| let key = match self.key(&context) { | |
| Ok(key) => key, | |
| Err(error) => return Box::pin(async move { Err(error) }), | |
| }; | |
| let mut owner = self.clone(); | |
| owner.finishes_turn = matches!( | |
| catalog.semantics(&definition.name), | |
| Ok(maka_agent_facts::tools::ToolSemantics::FinishTurn) | |
| ); | |
| let (reply, observed) = oneshot::channel(); | |
| owner.observation = Arc::new(Mutex::new(Some(reply))); | |
| owner.retained = Arc::new(Mutex::new(Vec::new())); | |
| let call = Arc::new(tokio::sync::Mutex::new(OwnedCall { | |
| key, | |
| custody: None, | |
| execution_owned: false, | |
| observing_prior: false, | |
| worker_done: false, | |
| physical: false, | |
| retained: owner.retained.clone(), | |
| readers: vec![], | |
| host: None, | |
| observation: None, | |
| })); | |
| owner.active = Some(call.clone()); | |
| self.calls | |
| .lock() | |
| .expect("original external sources") | |
| .push(call.clone()); | |
| // Admission, execution and T2 have one owner, independent of this observer's lifetime. | |
| self.workers.spawn(async move { | |
| let result = owner | |
| .invoke_original(catalog, context, definition, input, cancellation) | |
| .await; | |
| if let Err(error) = &result { | |
| owner.cleanup.observe_tool(error); | |
| } | |
| { | |
| let mut source = call.lock().await; | |
| source.worker_done = true; | |
| // Observers own their readers and SQL, never the prior execution. | |
| if result.is_ok() || !source.execution_owned { | |
| source.physical = true; | |
| } | |
| } | |
| // The common successful path already owns every T2 ACK. Release it | |
| // without retaining the whole successful tool history until Run end. | |
| let ready = { | |
| let source = call.lock().await; | |
| let acknowledged = source | |
| .retained | |
| .lock() | |
| .expect("external T2 material") | |
| .iter() | |
| .all(|batch| batch.acknowledged.load(std::sync::atomic::Ordering::SeqCst)); | |
| source.physical | |
| && source.readers.is_empty() | |
| && source.has_settlement() | |
| && acknowledged | |
| }; | |
| if ready { | |
| let custody = call.lock().await.custody.take(); | |
| drop(custody); | |
| owner | |
| .calls | |
| .lock() | |
| .expect("original external sources") | |
| .retain(|owned| !Arc::ptr_eq(owned, &call)); | |
| } | |
| owner.deliver(result); | |
| }); | |
| Box::pin(async move { | |
| observed.await.map_err(|_| { | |
| ToolError::OutcomeUnknown("Original external Host observation closed".into()) | |
| })? | |
| }) | |
| } | |
| } | |
| fn returned_fact_id(run: RunId, key: ExternalEffectKey) -> FactId { | |
| let bytes = serde_json::to_vec(&("maka.external-host-return-fact.v1", run, key)) | |
| .expect("external return identity"); | |
| let digest = Sha256::digest(bytes); | |
| let mut bytes: [u8; 16] = digest[..16].try_into().expect("SHA256 prefix"); | |
| bytes[6] = (bytes[6] & 0x0f) | 0x80; | |
| bytes[8] = (bytes[8] & 0x3f) | 0x80; | |
| FactId::try_from(Uuid::from_bytes(bytes)).expect("nonnil external return fact identity") | |
| } | |
| fn render_return( | |
| id: FactId, | |
| key: ExternalEffectKey, | |
| name: &str, | |
| returned: ExternalReturn, | |
| ) -> Result<ReturnedToolResult, ToolError> { | |
| let (output, is_error, projection, display_budget_chars) = match returned { | |
| ExternalReturn::Output { | |
| output, | |
| is_error, | |
| projection, | |
| display_budget_chars, | |
| .. | |
| } => (output, is_error, projection, display_budget_chars), | |
| ExternalReturn::Failure { diagnostic, .. } => return Err(ToolError::Failed(diagnostic)), | |
| }; | |
| let projection = display_projection(id, key, name, projection, display_budget_chars)?; | |
| Ok(ReturnedToolResult { | |
| output, | |
| is_error, | |
| projection, | |
| loaded_tools: Default::default(), | |
| display_budget_chars, | |
| }) | |
| } | |
| fn display_projection( | |
| id: FactId, | |
| key: ExternalEffectKey, | |
| name: &str, | |
| projection: DurableToolProjection, | |
| display_budget_chars: Option<usize>, | |
| ) -> Result<DurableToolProjection, ToolError> { | |
| match maka_agent_facts::archive::ArchivedPlaceholder::prepare_projection( | |
| id.to_string(), | |
| format!("external:{}:{}", key.task, key.operation), | |
| name.to_owned(), | |
| &projection, | |
| display_budget_chars.unwrap_or(maka_agent_facts::read::MAX_PAGE_CHARS), | |
| ) | |
| .map_err(persistence)? | |
| { | |
| Some(archived) => Ok(archived), | |
| None => Ok(projection), | |
| } | |
| } | |
| fn ended_unknown_reply(reply: &OperationReply) -> bool { | |
| maka_agent_facts::tool_output::ended_unknown_diagnostic(reply).is_some() | |
| } | |
| fn operation_return_error(reply: &OperationReply) -> Option<ToolError> { | |
| match &reply.receipt { | |
| Receipt::Recorded { | |
| cleanup: OperationCleanup::Unconfirmed | OperationCleanup::Failed { .. }, | |
| .. | |
| } => Some(ToolError::OperationCleanupUnconfirmed { | |
| message: "Original Host operation cleanup is unconfirmed".into(), | |
| observation: Box::new(reply.clone()), | |
| }), | |
| Receipt::Recorded { | |
| outcome: OperationOutcome::Returned { .. } | OperationOutcome::NotExecuted { .. }, | |
| cleanup: OperationCleanup::Confirmed, | |
| } => None, | |
| _ => Some(ToolError::OperationUnknown { | |
| message: "Original Host operation outcome is unknown".into(), | |
| observation: Box::new(reply.clone()), | |
| }), | |
| } | |
| } | |
| fn at() -> UnixMillis { | |
| UnixMillis(now().try_into().expect("Unix milliseconds")) | |
| } | |
| fn change(event: ExternalEffectEvent) -> Change { | |
| Change::new(Fact::ExternalEffect { event }, at()) | |
| } | |
| fn persistence(error: impl std::fmt::Display) -> ToolError { | |
| ToolError::Persistence(error.to_string()) | |
| } |