| /* | |
| * 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. | |
| */ | |
| use crate::{ | |
| Engine, RunError, | |
| ports::*, | |
| reconcile::{self, Action, Live, PreparedStepPrompt, WorkKey}, | |
| }; | |
| use futures_util::{FutureExt, StreamExt, future::BoxFuture, stream::FuturesUnordered}; | |
| use maka_agent_facts::{ | |
| context::{CheckpointMode, SummaryOrigin}, | |
| durable::*, | |
| tool_call::ToolRejection, | |
| tools::ToolSemantics, | |
| }; | |
| use maka_agent_state::{ | |
| Change, Commit, ContentBlob, DriverClaim, Head, Store, | |
| fold::{RunView, Tool, tool_key}, | |
| }; | |
| use maka_event_log::StoreError; | |
| use maka_plugin_core::operation::OperationOutcome; | |
| use std::{ | |
| collections::{HashSet, VecDeque}, | |
| fmt, | |
| future::poll_fn, | |
| sync::Arc, | |
| task::Poll, | |
| time::Duration, | |
| }; | |
| use tokio_util::sync::CancellationToken; | |
| #[cfg(test)] | |
| mod batch_admission_tests; | |
| mod cells; | |
| mod cleanup; | |
| mod compaction; | |
| mod external; | |
| mod preflight; | |
| mod publication; | |
| mod witness; | |
| pub use witness::{ | |
| ExternalCleanupPublication, ExternalWitness, ModelCleanupPublication, ModelWitness, | |
| PublicationWitness, | |
| }; | |
| #[derive(Clone, Debug)] | |
| pub enum DriverExit { | |
| /// Only this physical activation retired. The logical Run was not stopped. | |
| Retired { | |
| run: RunId, | |
| }, | |
| Terminal { | |
| run: RunId, | |
| terminal: Terminal, | |
| }, | |
| /// Native schedules this level from Store; notification delivery is only a hint. | |
| Waiting { | |
| run: RunId, | |
| due_at: Option<UnixMillis>, | |
| }, | |
| Blocked { | |
| run: RunId, | |
| blockers: Vec<(BlockerId, BlockerKind)>, | |
| due_at: Option<UnixMillis>, | |
| }, | |
| } | |
| /// Retain the original stable fact IDs after an uncertain commit. Reconciling | |
| /// this object is not a new admission and never repeats an external effect. | |
| #[derive(Clone)] | |
| pub struct PendingPublication { | |
| run: RunId, | |
| claim: Option<DriverClaim>, | |
| head: Head, | |
| changes: Vec<Change>, | |
| contents: Vec<ContentBlob>, | |
| steering: Option<maka_agent_state::queue::Consume>, | |
| } | |
| #[derive(Debug)] | |
| pub enum PublicationResolution { | |
| Committed(Commit), | |
| NotCommitted, | |
| } | |
| impl fmt::Debug for PendingPublication { | |
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
| f.debug_struct("PendingPublication") | |
| .field("run", &self.run) | |
| .field( | |
| "facts", | |
| &self | |
| .changes | |
| .iter() | |
| .map(|change| change.id) | |
| .collect::<Vec<_>>(), | |
| ) | |
| .finish() | |
| } | |
| } | |
| impl PendingPublication { | |
| pub fn run(&self) -> RunId { | |
| self.run | |
| } | |
| pub fn fact_ids(&self) -> impl Iterator<Item = FactId> + '_ { | |
| self.changes.iter().map(|change| change.id) | |
| } | |
| pub async fn reconcile(&self, store: &Store) -> Result<PublicationResolution, StoreError> { | |
| let original = store | |
| .publication(self.run, self.fact_ids().collect(), self.claim.as_ref()) | |
| .await?; | |
| if self.original_recorded(&original.facts)? { | |
| return Ok(PublicationResolution::Committed(Commit { | |
| head: original.head, | |
| facts: original.facts, | |
| })); | |
| } | |
| if self.claim.is_some() && (!original.claim_current || original.head != self.head) { | |
| if self.steering.is_none() && self.changes.iter().all(|change| change.fact.is_late()) { | |
| return store | |
| .observe(self.run, self.changes.clone(), self.contents.clone()) | |
| .await | |
| .map(PublicationResolution::Committed); | |
| } | |
| return Ok(PublicationResolution::NotCommitted); | |
| } | |
| match self.submit(store).await { | |
| Ok(commit) => Ok(PublicationResolution::Committed(commit)), | |
| Err(source) | |
| if self.claim.is_some() | |
| && matches!( | |
| source.underlying(), | |
| StoreError::RevisionConflict { .. } | StoreError::InvalidTransition(_) | |
| ) => | |
| { | |
| // Claim replacement has no SQL notification. Settle this race | |
| // with one more original-writer barrier, never a new admission. | |
| let current = store | |
| .publication(self.run, self.fact_ids().collect(), self.claim.as_ref()) | |
| .await?; | |
| if self.original_recorded(¤t.facts)? { | |
| return Ok(PublicationResolution::Committed(Commit { | |
| head: current.head, | |
| facts: current.facts, | |
| })); | |
| } | |
| if !current.claim_current { | |
| if self.steering.is_none() | |
| && self.changes.iter().all(|change| change.fact.is_late()) | |
| { | |
| return store | |
| .observe(self.run, self.changes.clone(), self.contents.clone()) | |
| .await | |
| .map(PublicationResolution::Committed); | |
| } | |
| return Ok(PublicationResolution::NotCommitted); | |
| } | |
| Err(source) | |
| } | |
| Err(source) => Err(source), | |
| } | |
| } | |
| fn original_recorded(&self, facts: &[RecordedFact]) -> Result<bool, StoreError> { | |
| for recorded in facts { | |
| if recorded.run != self.run | |
| || recorded.schema_version != SCHEMA_VERSION | |
| || !self.changes.iter().any(|change| { | |
| change.id == recorded.id | |
| && change.at == recorded.at | |
| && change.fact == recorded.fact | |
| }) | |
| { | |
| return Err(StoreError::EventConflict); | |
| } | |
| } | |
| if facts.len() == self.changes.len() { | |
| return Ok(true); | |
| } | |
| if !facts.is_empty() { | |
| return Err(StoreError::EventConflict); | |
| } | |
| Ok(false) | |
| } | |
| async fn submit(&self, store: &Store) -> Result<Commit, StoreError> { | |
| match &self.claim { | |
| Some(claim) if self.steering.is_some() => { | |
| store | |
| .append_steering( | |
| claim, | |
| self.head, | |
| self.changes.clone(), | |
| self.contents.clone(), | |
| self.steering.clone().expect("steering publication"), | |
| ) | |
| .await | |
| } | |
| Some(claim) => { | |
| store | |
| .append( | |
| claim, | |
| self.head, | |
| self.changes.clone(), | |
| self.contents.clone(), | |
| ) | |
| .await | |
| } | |
| None => { | |
| store | |
| .observe(self.run, self.changes.clone(), self.contents.clone()) | |
| .await | |
| } | |
| } | |
| } | |
| #[expect( | |
| clippy::too_many_arguments, | |
| reason = "Preparation publication threads optional model admission for coalesced pre-model dispatch" | |
| )] | |
| fn prepared_step( | |
| claim: &DriverClaim, | |
| view: RunView, | |
| live: &Live, | |
| model_admission: Option<ModelAdmissionFn<'_>>, | |
| prepared: PreparedStep, | |
| try_attempt: bool, | |
| compaction: Option<Box<CompactionCommit>>, | |
| now: UnixMillis, | |
| ) -> Result<(Self, Option<PreparedStepPrompt>, Option<AdmittedLaunch>), RunError> { | |
| let request = ContentBlob::json(&prepared.request)?; | |
| let step = prepared.request.step; | |
| let change = Change::new( | |
| Fact::StepPrepared { | |
| step, | |
| request: request.reference().clone(), | |
| adjudications: prepared | |
| .request | |
| .adjudications | |
| .iter() | |
| .map(|basis| basis.blocker) | |
| .collect(), | |
| }, | |
| now, | |
| ); | |
| let run = view.run; | |
| let head = Head::from(&view); | |
| let paired = if try_attempt && compaction.is_none() { | |
| attempt_after_step(view, live, model_admission, &prepared, &change) | |
| } else { | |
| None | |
| }; | |
| let mut changes = vec![change]; | |
| let mut admitted_attempt = None; | |
| if let Some(paired) = paired { | |
| changes.push(paired.prepared); | |
| if let Some((attempt_id, admitted_change)) = paired.admitted { | |
| changes.push(admitted_change); | |
| admitted_attempt = Some(attempt_id); | |
| } | |
| } | |
| let mut contents = prepared.contents; | |
| let frozen_request_bytes = admitted_attempt.and_then(|_| { | |
| contents | |
| .iter() | |
| .find(|blob| blob.reference() == &prepared.request.request) | |
| .and_then(|blob| blob.memory_bytes().map(ToOwned::to_owned)) | |
| }); | |
| contents.push(request); | |
| if let Some(compaction) = compaction { | |
| let CompactionCommit { | |
| mut checkpoint, | |
| baseline, | |
| .. | |
| } = *compaction; | |
| checkpoint.at = now; | |
| changes.insert(0, checkpoint); | |
| contents.push(baseline); | |
| } | |
| for notice in prepared.notifications { | |
| changes.push(Change::new( | |
| Fact::Cell { | |
| event: maka_agent_facts::cells::CellEvent::NotificationConsumed { | |
| cell: notice.cell, | |
| notification: notice.id, | |
| step, | |
| }, | |
| }, | |
| now, | |
| )); | |
| } | |
| let (prompt, launch) = match (admitted_attempt, frozen_request_bytes) { | |
| (Some(attempt), Some(bytes)) => ( | |
| None, | |
| Some(AdmittedLaunch { | |
| attempt, | |
| input: AdmittedModelInput { | |
| request: prepared.request, | |
| bytes, | |
| prompt: prepared.prompt, | |
| }, | |
| }), | |
| ), | |
| _ => ( | |
| prepared.prompt.map(|prompt| PreparedStepPrompt { | |
| step, | |
| request: prepared.request.request, | |
| prompt, | |
| }), | |
| None, | |
| ), | |
| }; | |
| Ok(( | |
| Self { | |
| run, | |
| claim: Some(claim.clone()), | |
| head, | |
| changes, | |
| contents, | |
| steering: prepared.steering, | |
| }, | |
| prompt, | |
| launch, | |
| )) | |
| } | |
| } | |
| struct AdmittedLaunch { | |
| attempt: ModelAttemptId, | |
| input: AdmittedModelInput, | |
| } | |
| struct PairedAttempt { | |
| prepared: Change, | |
| admitted: Option<(ModelAttemptId, Change)>, | |
| } | |
| type ModelAdmissionFn<'a> = | |
| &'a dyn Fn(RunId, ModelAttemptId, &RequestSpec) -> Result<serde_json::Value, RunError>; | |
| fn attempt_after_step( | |
| mut view: RunView, | |
| live: &Live, | |
| model_admission: Option<ModelAdmissionFn<'_>>, | |
| prepared: &PreparedStep, | |
| step: &Change, | |
| ) -> Option<PairedAttempt> { | |
| if !live.work.is_empty() | |
| || live.preparation_failure.is_some() | |
| || view.spec.budget.deadline.is_some() | |
| || view.retry_pending.is_some() | |
| || !view.replans.is_empty() | |
| || !view.waits.is_empty() | |
| || !prepared.request.adjudications.is_empty() | |
| || !prepared.notifications.is_empty() | |
| || prepared.steering.is_some() | |
| || !compaction::ordinary_main(&view, prepared) | |
| { | |
| return None; | |
| } | |
| // For this plain Main the new request has no planner work of its own. | |
| // Project only the new Step and let reconciliation order the existing work. | |
| let recorded = RecordedFact { | |
| schema_version: SCHEMA_VERSION, | |
| id: step.id, | |
| run: view.run, | |
| revision: view.revision.checked_add(1)?, | |
| at: step.at, | |
| fact: step.fact.clone(), | |
| }; | |
| view.apply(&recorded).ok()?; | |
| let step_id = prepared.request.step; | |
| if !matches!(reconcile::next(&view, live, step.at, None, false), Action::Attempt(next) if next == step_id) | |
| { | |
| return None; | |
| } | |
| let attempt_id = ModelAttemptId::new(); | |
| let attempt = Change::new( | |
| Fact::ModelAttemptPrepared { | |
| attempt: attempt_id, | |
| step: step_id, | |
| }, | |
| step.at, | |
| ); | |
| // Keep the standalone Step when the original fold would reject the next action. | |
| view.apply(&RecordedFact { | |
| schema_version: SCHEMA_VERSION, | |
| id: attempt.id, | |
| run: view.run, | |
| revision: view.revision.checked_add(1)?, | |
| at: attempt.at, | |
| fact: attempt.fact.clone(), | |
| }) | |
| .ok()?; | |
| // Coalesce ModelAttemptAdmitted into the exact same atomic pre-model authority | |
| // fence when the planner would immediately dispatch this attempt and admission succeeds. | |
| let admitted = (|| { | |
| if !matches!(reconcile::next(&view, live, step.at, None, false), Action::Start(next) if next == attempt_id) | |
| { | |
| return None; | |
| } | |
| view.check_effect_admission().ok()?; | |
| let upstream = model_admission?(view.run, attempt_id, &prepared.request).ok()?; | |
| let admitted_change = Change::new( | |
| Fact::ModelAttemptAdmitted { | |
| attempt: attempt_id, | |
| upstream, | |
| }, | |
| step.at, | |
| ); | |
| view.apply(&RecordedFact { | |
| schema_version: SCHEMA_VERSION, | |
| id: admitted_change.id, | |
| run: view.run, | |
| revision: view.revision.checked_add(1)?, | |
| at: admitted_change.at, | |
| fact: admitted_change.fact.clone(), | |
| }) | |
| .ok()?; | |
| Some((attempt_id, admitted_change)) | |
| })(); | |
| Some(PairedAttempt { | |
| prepared: attempt, | |
| admitted, | |
| }) | |
| } | |
| struct CompactionCommit { | |
| checkpoint: Change, | |
| baseline: ContentBlob, | |
| candidate: Option<SummaryOrigin>, | |
| } | |
| enum Completion { | |
| PublicationPrepared { | |
| id: ExternalPublicationId, | |
| control: u64, | |
| result: Result<PreparedExternalPublication, RunError>, | |
| }, | |
| PublicationValidated { | |
| key: maka_agent_facts::external_publication::ExternalPublicationKey, | |
| control: u64, | |
| result: Result<maka_sandbox::workspace::PermissionBasis, RunError>, | |
| }, | |
| PublicationLooked { | |
| key: maka_agent_facts::external_publication::ExternalPublicationKey, | |
| result: Result<OriginalLookup, RunError>, | |
| }, | |
| PublicationRecorded { | |
| key: maka_agent_facts::external_publication::ExternalPublicationKey, | |
| result: Result<FactId, RunError>, | |
| }, | |
| Cell { | |
| cell: maka_agent_facts::cells::CellId, | |
| result: Result<CellObservation, RunError>, | |
| }, | |
| ExternalPrepared { | |
| task: ExternalTaskId, | |
| control: u64, | |
| result: Result<PreparedExternal, RunError>, | |
| }, | |
| ExternalConnected { | |
| task: ExternalTaskId, | |
| result: Result<ExternalConnection, RunError>, | |
| }, | |
| External { | |
| task: ExternalTaskId, | |
| index: u64, | |
| connection: ExternalConnection, | |
| notice: Option<Result<FactId, RunError>>, | |
| }, | |
| Prepared { | |
| control: u64, | |
| try_attempt: bool, | |
| compaction: Option<Box<CompactionCommit>>, | |
| result: Result<PreparedStep, RunError>, | |
| }, | |
| ProactiveSummaryPrepared { | |
| control: u64, | |
| result: Result<PreparedStep, RunError>, | |
| }, | |
| Decided { | |
| key: ToolKey, | |
| control: u64, | |
| result: Result<PreparedDecision, RunError>, | |
| prevalidated: Option<Result<PrevalidatedTool, RunError>>, | |
| }, | |
| Looked { | |
| key: ToolKey, | |
| control: u64, | |
| result: Result<OriginalLookup, RunError>, | |
| }, | |
| Validated { | |
| key: ToolKey, | |
| control: u64, | |
| result: Result<ToolValidation, RunError>, | |
| }, | |
| Dispatched { | |
| key: ToolKey, | |
| result: Result<DispatchOutcome, RunError>, | |
| }, | |
| Observed { | |
| key: ToolKey, | |
| result: Result<maka_plugin_core::operation::OperationReply, RunError>, | |
| }, | |
| Assessed { | |
| attempt: ModelAttemptId, | |
| basis: Head, | |
| result: Result<RecoveryEvidence, RunError>, | |
| }, | |
| Connected { | |
| attempt: ModelAttemptId, | |
| result: Result<ModelConnection, RunError>, | |
| }, | |
| ModelCancelled { | |
| attempt: ModelAttemptId, | |
| result: Result<(), RunError>, | |
| }, | |
| Model { | |
| attempt: ModelAttemptId, | |
| index: u64, | |
| connection: ModelConnection, | |
| observation: Option<Result<ModelObservation, RunError>>, | |
| }, | |
| } | |
| struct ReadyDecidedTool { | |
| key: ToolKey, | |
| prepared: PreparedDecision, | |
| } | |
| fn port_error_blocker(error: RunError) -> Result<BlockerKind, RunError> { | |
| match error { | |
| RunError::IsolationUnavailable(diagnostic) => { | |
| Ok(BlockerKind::CurrentAuthorityUnavailable { | |
| stage: AuthorityStage::BackendScope, | |
| diagnostic, | |
| }) | |
| } | |
| RunError::Blocked(reason) => Ok(*reason), | |
| error => Err(error), | |
| } | |
| } | |
| fn batchable_decided_scope(completion: &Completion) -> Option<(ModelAttemptId, u64)> { | |
| match completion { | |
| Completion::Decided { | |
| key, | |
| control, | |
| result: | |
| Ok(PreparedDecision { | |
| decision: | |
| ToolDecision::Approved { | |
| approval: None, | |
| semantics: ToolSemantics::Parallel, | |
| resources, | |
| .. | |
| }, | |
| .. | |
| }), | |
| prevalidated: Some(Ok(PrevalidatedTool::Allowed)), | |
| } if resources | |
| .iter() | |
| .all(|resource| matches!(resource, ResourceRecovery::Reference { .. })) => | |
| { | |
| Some((key.attempt, *control)) | |
| } | |
| _ => None, | |
| } | |
| } | |
| fn take_batchable_decided(completion: Completion) -> Option<ReadyDecidedTool> { | |
| match completion { | |
| Completion::Decided { | |
| key, | |
| result: Ok(prepared), | |
| prevalidated: Some(Ok(PrevalidatedTool::Allowed)), | |
| .. | |
| } => Some(ReadyDecidedTool { key, prepared }), | |
| _ => None, | |
| } | |
| } | |
| fn complete_recorded_dispatch( | |
| run: RunId, | |
| live: &mut Live, | |
| key: ToolKey, | |
| original: RecordedFact, | |
| ) -> Result<(), RunError> { | |
| live.work.remove(&WorkKey::Dispatch(key.clone())); | |
| let matches = match &original.fact { | |
| Fact::LocalToolReturned { tool, .. } | |
| | Fact::HostToolResultRecorded { tool, .. } | |
| | Fact::ToolPreventedBeforeHostAdmission { tool, .. } | |
| | Fact::OperationObserved { tool, .. } => tool == &key, | |
| _ => false, | |
| }; | |
| if original.run != run || !matches { | |
| return Err(RunError::Internal( | |
| "tool worker acknowledged evidence for another original operation".into(), | |
| )); | |
| } | |
| if let Fact::LocalToolReturned { | |
| settlement: maka_agent_facts::tool_call::ToolSettlement::Unknown, | |
| .. | |
| } = &original.fact | |
| { | |
| live.ended_unknown.insert(key); | |
| } | |
| Ok(()) | |
| } | |
| fn is_batch_collection_boundary(completion: &Completion) -> bool { | |
| match completion { | |
| Completion::Decided { result: Err(_), .. } | |
| | Completion::Decided { | |
| prevalidated: Some(Err(_)), | |
| .. | |
| } | |
| | Completion::Decided { | |
| prevalidated: | |
| Some(Ok(PrevalidatedTool::Blocked(_) | PrevalidatedTool::LookupBlocked(_))), | |
| .. | |
| } => true, | |
| Completion::Decided { .. } => false, | |
| _ => true, | |
| } | |
| } | |
| type Work = FuturesUnordered<BoxFuture<'static, Completion>>; | |
| fn read_model( | |
| work: &mut Work, | |
| attempt: ModelAttemptId, | |
| index: u64, | |
| mut connection: ModelConnection, | |
| ) { | |
| work.push( | |
| async move { | |
| let observation = connection.next().await; | |
| Completion::Model { | |
| attempt, | |
| index, | |
| connection, | |
| observation, | |
| } | |
| } | |
| .boxed(), | |
| ); | |
| } | |
| impl Engine { | |
| async fn publish( | |
| &self, | |
| claim: Option<&DriverClaim>, | |
| view: &RunView, | |
| facts: Vec<Fact>, | |
| contents: Vec<ContentBlob>, | |
| now: UnixMillis, | |
| ) -> Result<bool, RunError> { | |
| let publication = PendingPublication { | |
| run: view.run, | |
| claim: claim.cloned(), | |
| head: Head::from(view), | |
| changes: facts | |
| .into_iter() | |
| .map(|fact| Change::new(fact, now)) | |
| .collect(), | |
| contents, | |
| steering: None, | |
| }; | |
| match publication.submit(&self.store).await { | |
| Ok(_) => Ok(true), | |
| Err(StoreError::RevisionConflict { .. }) if claim.is_some() => Ok(false), | |
| Err(source) | |
| if matches!( | |
| source.underlying(), | |
| StoreError::CommitUnknown(_) | StoreError::OperationUnknown | |
| ) => | |
| { | |
| Err(RunError::PublicationUncertain { | |
| publication: Box::new(publication), | |
| source, | |
| }) | |
| } | |
| Err(error) => Err(error.into()), | |
| } | |
| } | |
| async fn try_block( | |
| &self, | |
| claim: &DriverClaim, | |
| view: &RunView, | |
| reason: BlockerKind, | |
| now: UnixMillis, | |
| ) -> Result<bool, RunError> { | |
| if view.blockers.values().any(|blocker| { | |
| (blocker.reason == reason || matches!((&blocker.reason, &reason), | |
| (BlockerKind::CurrentAuthorityUnavailable { stage: old, .. }, BlockerKind::CurrentAuthorityUnavailable { stage: new, .. }) if old == new)) | |
| && blocker.decision.is_none() && blocker.resolved_by.is_none() | |
| }) { | |
| return Ok(true); | |
| } | |
| self.publish( | |
| Some(claim), | |
| view, | |
| vec![Fact::Blocked { | |
| blocker: BlockerId::new(), | |
| reason, | |
| }], | |
| vec![], | |
| now, | |
| ) | |
| .await | |
| } | |
| async fn block( | |
| &self, | |
| claim: &DriverClaim, | |
| view: &RunView, | |
| reason: BlockerKind, | |
| now: UnixMillis, | |
| ) -> Result<(), RunError> { | |
| self.try_block(claim, view, reason, now).await?; | |
| Ok(()) | |
| } | |
| #[expect( | |
| clippy::too_many_arguments, | |
| reason = "Settling a decided-tool blocker retries only on same-control revision movement so queued siblings cannot bypass it" | |
| )] | |
| async fn settle_decided_blocker( | |
| &self, | |
| claim: &DriverClaim, | |
| ports: &Arc<dyn DriverPorts>, | |
| mut view: RunView, | |
| control: u64, | |
| attempt: ModelAttemptId, | |
| reason: BlockerKind, | |
| mut now: UnixMillis, | |
| cancellation: &CancellationToken, | |
| retirement: &CancellationToken, | |
| ) -> Result<(), RunError> { | |
| loop { | |
| if self.try_block(claim, &view, reason.clone(), now).await? { | |
| return Ok(()); | |
| } | |
| if cancellation.is_cancelled() || retirement.is_cancelled() { | |
| return Ok(()); | |
| } | |
| view = self.store.frontier(claim.run()).await?; | |
| now = ports.now(); | |
| if control != view.control_version | |
| || view.stopped | |
| || view.terminal.is_some() | |
| || view.recovery_failure().is_some() | |
| || view.finish_requested() | |
| || view.model_steered(attempt) | |
| || view.current_attempt != Some(attempt) | |
| { | |
| return Ok(()); | |
| } | |
| } | |
| } | |
| /// Drive one original logical Run using a live Native claim. Model streaming, | |
| /// tool preparation and original receipt observation are polled concurrently. | |
| /// No parked checkpoint or physical successor Run participates in recovery. | |
| pub async fn drive( | |
| &self, | |
| claim: DriverClaim, | |
| ports: Arc<dyn DriverPorts>, | |
| cancellation: CancellationToken, | |
| ) -> Result<DriverExit, RunError> { | |
| let run = claim.run(); | |
| if claim.purpose() != maka_agent_state::DriverPurpose::Business { | |
| return Err(RunError::InvalidInput( | |
| "cleanup claim has no business ports".into(), | |
| )); | |
| } | |
| let retirement = claim.retirement(); | |
| let mut live = Live::default(); | |
| let mut work = Work::new(); | |
| let mut pending_completions: VecDeque<Completion> = VecDeque::new(); | |
| let mut cancellation_sent = false; | |
| let mut recovery_cancelled = false; | |
| let mut authority_checked = false; | |
| let mut model_level: Option<(Option<StepId>, Option<ContentRef>, reconcile::ModelLevel)> = | |
| None; | |
| let mut summary_key = None; | |
| let mut model_failure_key: Option<(ModelAttemptId, Option<ContentRef>, bool)> = None; | |
| let mut changes = self.store.subscribe_driver(&claim)?; | |
| 'drive: loop { | |
| if retirement.is_cancelled() { | |
| cancellation.cancel(); | |
| } | |
| if let Some(completion) = pending_completions.pop_front() { | |
| Box::pin(self.complete_next( | |
| &claim, | |
| &ports, | |
| &mut live, | |
| &mut work, | |
| &mut pending_completions, | |
| completion, | |
| &cancellation, | |
| &retirement, | |
| )) | |
| .await?; | |
| continue; | |
| } | |
| // Subscribe/mark before the authoritative read, so a commit between | |
| // reading the level and waiting cannot lose its invalidation hint. | |
| changes.borrow_and_update(); | |
| let view = self.store.frontier(run).await?; | |
| let now = ports.now(); | |
| if !authority_checked && view.terminal.is_none() && !cancellation.is_cancelled() { | |
| if self.resolve_authority(&claim, &view, now).await? { | |
| continue; | |
| } | |
| authority_checked = true; | |
| } | |
| if live.stop_request != view.stop_request { | |
| live.stop_request = view.stop_request; | |
| live.observed.clear(); | |
| live.looked_up.clear(); | |
| live.cells_observed.clear(); | |
| live.publication_looked_up.clear(); | |
| live.publication_observed.clear(); | |
| } | |
| if let Some(terminal) = &view.terminal { | |
| return Ok(DriverExit::Terminal { | |
| run, | |
| terminal: terminal.clone(), | |
| }); | |
| } | |
| if let Some(publication) = view | |
| .external | |
| .as_ref() | |
| .and_then(|task| task.publication.as_ref()) | |
| { | |
| live.publication_prepared.retain(|id| *id == publication.id); | |
| live.publication_looked_up.retain(|key| { | |
| key.publication == publication.id && key.index >= publication.settled | |
| }); | |
| live.publication_observed.retain(|key| { | |
| key.publication == publication.id && key.index >= publication.settled | |
| }); | |
| } else { | |
| live.publication_prepared.clear(); | |
| live.publication_looked_up.clear(); | |
| live.publication_observed.clear(); | |
| } | |
| live.looked_up | |
| .retain(|key| view.tools.contains_key(&tool_key(key))); | |
| live.observed | |
| .retain(|key| view.tools.contains_key(&tool_key(key))); | |
| live.cells_observed | |
| .retain(|cell| view.cells.contains_key(cell)); | |
| live.models_observed.retain(|attempt| { | |
| view.attempts | |
| .get(attempt) | |
| .is_some_and(|value| value.closed.is_none()) | |
| }); | |
| // Cancellation belongs to the physical activation. Only an explicit | |
| // Native control transaction may place the durable Stop fence. | |
| if recovery_cancelled && view.recovery_failure().is_none() { | |
| // Resume with new live ports if late evidence settled the failure | |
| // after this activation cancelled its workers. | |
| cancellation.cancel(); | |
| } | |
| let retiring = cancellation.is_cancelled() && view.terminal.is_none(); | |
| let complete_at_budget = !retiring && self.complete_at_budget(&view, now).await?; | |
| let fenced = view.stopped | |
| || view.cancelled_by_risk | |
| || view.spec.budget.deadline.is_some_and(|at| now >= at) | |
| || reconcile::budget_exhausted(&view); | |
| if ((fenced && !complete_at_budget) || retiring) && !cancellation_sent { | |
| live.cells_observed.clear(); | |
| ports.cancel_live(run); | |
| cancellation_sent = true; | |
| } | |
| if !retiring | |
| && !view.cancelled_by_risk | |
| && self.resolve_recovered_cells(&claim, &view, now).await? | |
| { | |
| continue; | |
| } | |
| if retiring && work.is_empty() { | |
| return Ok(DriverExit::Retired { run }); | |
| } | |
| for (wait, pending) in &view.waits { | |
| if !retiring | |
| && !fenced | |
| && pending.satisfied.is_none() | |
| && pending.cancelled.is_none() | |
| && let WaitReason::Child { run: child } = &pending.reason | |
| { | |
| let child = self.store.frontier(*child).await?; | |
| if let Some(terminal) = child.terminal { | |
| let evidence = ContentBlob::json( | |
| &serde_json::json!({"child":child.run,"revision":child.revision,"terminal":terminal}), | |
| )?; | |
| self.publish( | |
| Some(&claim), | |
| &view, | |
| vec![Fact::WaitSatisfied { | |
| wait: *wait, | |
| evidence: evidence.reference().clone(), | |
| }], | |
| vec![evidence], | |
| now, | |
| ) | |
| .await?; | |
| continue 'drive; | |
| } | |
| } | |
| } | |
| if !fenced | |
| && !retiring | |
| && matches!(view.spec.backend, BackendSpec::Model { .. }) | |
| && view.replans.is_empty() | |
| && !view | |
| .blockers | |
| .values() | |
| .any(|b| b.decision.is_none() && b.resolved_by.is_none()) | |
| { | |
| let current_summary = view.proactive_summary_attempt().map(|(id, attempt)| { | |
| ( | |
| id, | |
| attempt.closed.is_some(), | |
| attempt.interrupted.is_some(), | |
| attempt.cleanup == maka_plugin_core::operation::OperationCleanup::Confirmed, | |
| ) | |
| }); | |
| let overflow = view | |
| .current_attempt | |
| .and_then(|id| view.attempts.get(&id)) | |
| .is_some_and(|attempt| attempt.compactable_overflow()); | |
| let key = (current_summary, overflow, live.rejected_summaries.len()); | |
| let reload = summary_key != Some(key) | |
| || model_level.as_ref().is_none_or(|(step, baseline, _)| { | |
| *step != view.current_step || *baseline != view.baseline | |
| }); | |
| if reload { | |
| let level = match self | |
| .model_level(&view, &live.rejected_summaries, overflow) | |
| .await | |
| { | |
| Ok(level) => level, | |
| Err(error) => { | |
| self.port_failure(&claim, &view, error, now).await?; | |
| continue; | |
| } | |
| }; | |
| model_level = Some((view.current_step, view.baseline.clone(), level)); | |
| summary_key = Some(key); | |
| } | |
| // A canonical closure is immutable for its attempt; presence identifies | |
| // its one protocol body without cloning or hashing that body each loop. | |
| let failure_key = view.current_attempt.and_then(|id| { | |
| view.attempts.get(&id).map(|attempt| { | |
| (id, attempt.source_failure.clone(), attempt.closed.is_some()) | |
| }) | |
| }); | |
| if reload || model_failure_key != failure_key { | |
| let level = &mut model_level.as_mut().expect("model material loaded").2; | |
| if let Err(error) = self.refresh_model_failure(&view, level).await { | |
| self.port_failure(&claim, &view, error, now).await?; | |
| continue; | |
| } | |
| model_failure_key = failure_key; | |
| } | |
| } | |
| let mut action = if retiring { | |
| Action::Idle | |
| } else if !fenced && live.preparation_failure.is_some() { | |
| Action::Terminate(Terminal::Failed) | |
| } else if view.recovery_failure().is_some() && !view.cancelled_by_risk { | |
| if view.waiting_for_authority() { | |
| Action::Idle | |
| } else if work.is_empty() | |
| && let Some(observation) = reconcile::recovery_action(&view, &live) | |
| { | |
| observation | |
| } else { | |
| if !cancellation_sent { | |
| ports.cancel_live(run); | |
| cancellation_sent = true; | |
| recovery_cancelled = true; | |
| } | |
| if work.is_empty() || live.only_cleanup(&view) { | |
| Action::Terminate(Terminal::Failed) | |
| } else { | |
| Action::Idle | |
| } | |
| } | |
| } else { | |
| reconcile::next( | |
| &view, | |
| &live, | |
| now, | |
| model_level.as_ref().map(|(_, _, level)| level), | |
| complete_at_budget, | |
| ) | |
| }; | |
| if matches!(action, Action::Terminate(Terminal::Completed)) | |
| && !reconcile::budget_exhausted(&view) | |
| && !model_level.as_ref().is_some_and(|(_, _, level)| { | |
| matches!( | |
| level.input, | |
| maka_agent_facts::input::RunInput::ContextCompact { .. } | |
| ) | |
| }) | |
| { | |
| let queue = self | |
| .store | |
| .queue() | |
| .steering(view.spec.session_id.clone(), run) | |
| .await?; | |
| if !queue.entries.is_empty() | |
| && let Some(attempt) = view.current_attempt | |
| { | |
| action = Action::Prepare(Preparation::Steering { attempt }); | |
| } | |
| } | |
| if !retiring | |
| && !fenced | |
| && !view.finish_requested() | |
| && let Some((attempt, summary)) = view.proactive_summary_attempt() | |
| { | |
| let active = live.work.contains(&WorkKey::Model(attempt)); | |
| let open = summary.closed.is_none() && summary.interrupted.is_none(); | |
| let terminating = matches!(action, Action::Terminate(_)); | |
| let superseded = matches!(action, Action::Prepare(Preparation::Summary)); | |
| if open && !active && (summary.admission.is_some() || terminating || superseded) { | |
| action = Action::Interrupt(attempt, "Background summary did not finish".into()); | |
| } else if (terminating || superseded) && open && active { | |
| if !live.models_cancelled.contains(&attempt) { | |
| action = Action::CancelModel(attempt); | |
| } else if terminating { | |
| action = Action::Idle; | |
| } | |
| } else if matches!(action, Action::Terminate(Terminal::Completed)) | |
| && summary.admission.is_some() | |
| && summary.cleanup != maka_plugin_core::operation::OperationCleanup::Confirmed | |
| { | |
| action = reconcile::wait(&view, WaitReason::Model { attempt }, None, now); | |
| } else if open | |
| && summary.admission.is_none() | |
| && !matches!(action, Action::Terminate(_) | Action::Block(_)) | |
| && view.check_effect_admission().is_ok() | |
| { | |
| action = Action::Start(attempt); | |
| } | |
| } | |
| // Carry only the preparation of the Step going directly to admission. | |
| let starting_step = match &action { | |
| Action::Attempt(step) => Some(*step), | |
| Action::Start(attempt) => Some(view.attempts[attempt].step), | |
| _ => None, | |
| }; | |
| if live | |
| .prepared_prompt | |
| .as_ref() | |
| .is_some_and(|prepared| Some(prepared.step) != starting_step) | |
| { | |
| live.prepared_prompt = None; | |
| } | |
| let idle = matches!(action, Action::Idle); | |
| if idle { | |
| let observing_cell = live.work.iter().any(|key| matches!(key, WorkKey::Cell(_))); | |
| if !observing_cell && (work.is_empty() || live.only_cleanup(&view)) { | |
| if retiring { | |
| return Ok(DriverExit::Retired { run }); | |
| } | |
| if let Some(request) = view.stop_request | |
| && view.stop_assessed != Some(request) | |
| { | |
| self.publish( | |
| Some(&claim), | |
| &view, | |
| vec![Fact::StopAssessed { request }], | |
| vec![], | |
| now, | |
| ) | |
| .await?; | |
| continue; | |
| } | |
| return Ok(exit(&view)); | |
| } | |
| } else if Box::pin(async { | |
| match action { | |
| action @ (Action::PrepareExternal(_) | |
| | Action::StartExternal(_) | |
| | Action::ObserveExternal(_, _)) => { | |
| self.launch_external(&claim, &view, &ports, &mut live, &mut work, action) | |
| .await?; | |
| } | |
| Action::ObserveCell(cell) => { | |
| self.launch_cell(&claim, &view, &ports, &mut live, &mut work, cell) | |
| .await?; | |
| } | |
| action @ (Action::PreparePublication(_) | |
| | Action::ValidatePublication(_) | |
| | Action::LookupPublication(_) | |
| | Action::ObservePublication(_, _) | |
| | Action::AbandonPublication(_)) => { | |
| self.launch_publication( | |
| &claim, | |
| &view, | |
| ports.clone(), | |
| &mut live, | |
| &mut work, | |
| action, | |
| now, | |
| ) | |
| .await?; | |
| } | |
| action @ (Action::Compact(_) | Action::AdoptCandidate(_)) => { | |
| let (origin, candidate) = match action { | |
| Action::AdoptCandidate(origin) => (origin, Some(origin)), | |
| Action::Compact(attempt) => (SummaryOrigin { run, attempt }, None), | |
| _ => unreachable!(), | |
| }; | |
| let material = match self.compaction_checkpoint(&view, origin).await { | |
| Ok(Some(material)) => material, | |
| Ok(None) => return Ok::<_, RunError>(true), | |
| Err(RunError::CompactionFailed(diagnostic)) => { | |
| self.publish( | |
| Some(&claim), | |
| &view, | |
| termination_facts(&view, Terminal::Failed, Some(diagnostic)), | |
| vec![], | |
| now, | |
| ) | |
| .await?; | |
| return Ok(true); | |
| } | |
| Err(error) => { | |
| self.port_failure(&claim, &view, error, now).await?; | |
| return Ok(true); | |
| } | |
| }; | |
| let baseline = ContentBlob::json(&material)?; | |
| if let Some(target) = | |
| model_level.as_ref().and_then(|(_, _, m)| m.handoff.clone()) | |
| { | |
| match ports | |
| .handoff_checkpoint(&view, baseline.clone(), target, candidate) | |
| .await | |
| { | |
| Ok(()) => (), | |
| Err(RunError::CompactionMismatch) if candidate.is_some() => { | |
| live.rejected_summaries.extend(candidate); | |
| } | |
| Err( | |
| error @ (RunError::ModelUnavailable | |
| | RunError::AuthenticationRequired | |
| | RunError::InvalidInput(_)), | |
| ) => { | |
| let mut facts = vec![Fact::ContextCompacted { | |
| baseline: baseline.reference().clone(), | |
| }]; | |
| facts.extend(termination_facts( | |
| &view, | |
| Terminal::Failed, | |
| Some(format!( | |
| "Could not switch to the requested model: {error}" | |
| )), | |
| )); | |
| self.publish(Some(&claim), &view, facts, vec![baseline], now) | |
| .await?; | |
| } | |
| Err(RunError::Store(error)) | |
| if matches!( | |
| error.underlying(), | |
| StoreError::RevisionConflict { .. } | |
| | StoreError::SessionRevisionConflict { .. } | |
| ) => {} | |
| Err(error) => return Err(error), | |
| } | |
| return Ok(true); | |
| } | |
| let fact = Fact::ContextCompacted { | |
| baseline: baseline.reference().clone(), | |
| }; | |
| if material.checkpoint.mode == CheckpointMode::Standalone | |
| || material | |
| .native | |
| .as_ref() | |
| .is_some_and(|native| native.continuation.is_some()) | |
| { | |
| self.publish(Some(&claim), &view, vec![fact], vec![baseline], now) | |
| .await?; | |
| } else { | |
| let checkpoint = Change::new(fact, now); | |
| let preparation = | |
| Preparation::AdoptCompaction(Box::new(CompactionAdoption { | |
| origin, | |
| checkpoint: checkpoint.id, | |
| baseline: baseline.reference().clone(), | |
| material, | |
| })); | |
| let compaction = Box::new(CompactionCommit { | |
| checkpoint, | |
| baseline, | |
| candidate, | |
| }); | |
| live.work.insert(WorkKey::Prepare); | |
| let ports = ports.clone(); | |
| let snapshot = view.clone(); | |
| let control = snapshot.control_version; | |
| work.push( | |
| async move { | |
| Completion::Prepared { | |
| control, | |
| try_attempt: false, | |
| compaction: Some(compaction), | |
| result: ports.prepare_step(snapshot, preparation).await, | |
| } | |
| } | |
| .boxed(), | |
| ); | |
| } | |
| } | |
| Action::AcceptCellFinal(cell) => { | |
| self.accept_cell_final(&claim, &view, cell, ports.now()) | |
| .await?; | |
| } | |
| Action::ReportCellInterruption(cell) => { | |
| self.report_cell_interruption(&claim, &view, cell, ports.now()) | |
| .await?; | |
| } | |
| Action::Prepare(preparation) => { | |
| let try_attempt = matches!( | |
| preparation, | |
| Preparation::Initial | Preparation::ToolContinuation { .. }, | |
| ); | |
| live.work.insert(WorkKey::Prepare); | |
| let ports = ports.clone(); | |
| let snapshot = view.clone(); | |
| let control = snapshot.control_version; | |
| work.push( | |
| async move { | |
| Completion::Prepared { | |
| control, | |
| try_attempt, | |
| compaction: None, | |
| result: ports.prepare_step(snapshot, preparation).await, | |
| } | |
| } | |
| .boxed(), | |
| ); | |
| } | |
| Action::PrepareProactiveSummary => { | |
| live.work.insert(WorkKey::PrepareProactiveSummary); | |
| let ports = ports.clone(); | |
| let snapshot = view.clone(); | |
| let control = snapshot.control_version; | |
| work.push( | |
| async move { | |
| Completion::ProactiveSummaryPrepared { | |
| control, | |
| result: ports | |
| .prepare_step(snapshot, Preparation::Summary) | |
| .await, | |
| } | |
| } | |
| .boxed(), | |
| ); | |
| } | |
| Action::Attempt(step) => { | |
| self.publish( | |
| Some(&claim), | |
| &view, | |
| vec![Fact::ModelAttemptPrepared { | |
| attempt: ModelAttemptId::new(), | |
| step, | |
| }], | |
| vec![], | |
| now, | |
| ) | |
| .await?; | |
| } | |
| Action::Assess(attempt) => { | |
| let request = self | |
| .store | |
| .request(run, view.attempts[&attempt].step) | |
| .await?; | |
| live.work.insert(WorkKey::Assess(attempt)); | |
| let ports = ports.clone(); | |
| let snapshot = view.clone(); | |
| work.push( | |
| async move { | |
| Completion::Assessed { | |
| attempt, | |
| basis: Head::from(&snapshot), | |
| result: ports | |
| .assess_recovery(&snapshot, attempt, request) | |
| .await, | |
| } | |
| } | |
| .boxed(), | |
| ); | |
| } | |
| Action::Start(attempt) => { | |
| let request = self | |
| .store | |
| .request(run, view.attempts[&attempt].step) | |
| .await?; | |
| let bytes = self.store.content(run, request.request.clone()).await?; | |
| let upstream = ports.model_admission(run, attempt, &request)?; | |
| // This commit is the model admission's Stop/control linearization point. | |
| if self | |
| .publish( | |
| Some(&claim), | |
| &view, | |
| vec![Fact::ModelAttemptAdmitted { attempt, upstream }], | |
| vec![], | |
| now, | |
| ) | |
| .await? | |
| { | |
| live.work.insert(WorkKey::Model(attempt)); | |
| let ports = ports.clone(); | |
| let witness = | |
| ModelWitness::new(self.store.clone(), claim.clone(), attempt); | |
| let prompt = live.prepared_prompt.take().and_then(|prepared| { | |
| (prepared.step == request.step | |
| && prepared.request == request.request) | |
| .then_some(prepared.prompt) | |
| }); | |
| let input = AdmittedModelInput { | |
| request, | |
| bytes, | |
| prompt, | |
| }; | |
| work.push( | |
| async move { | |
| Completion::Connected { | |
| attempt, | |
| result: ports | |
| .start_model(run, attempt, input, witness) | |
| .await, | |
| } | |
| } | |
| .boxed(), | |
| ); | |
| } | |
| } | |
| Action::ObserveModel(attempt, descriptor) => { | |
| live.work.insert(WorkKey::Model(attempt)); | |
| live.models_observed.insert(attempt); | |
| let ports = ports.clone(); | |
| let after = view.attempts[&attempt].observed; | |
| let witness = ModelWitness::new(self.store.clone(), claim.clone(), attempt); | |
| work.push( | |
| async move { | |
| Completion::Connected { | |
| attempt, | |
| result: ports | |
| .observe_model(run, attempt, descriptor, after, witness) | |
| .await, | |
| } | |
| } | |
| .boxed(), | |
| ); | |
| } | |
| Action::Interrupt(attempt, diagnostic) => { | |
| self.publish( | |
| Some(&claim), | |
| &view, | |
| vec![Fact::ModelInterrupted { | |
| attempt, | |
| diagnostic, | |
| }], | |
| vec![], | |
| now, | |
| ) | |
| .await?; | |
| } | |
| Action::CancelModel(attempt) => { | |
| live.models_cancelled.insert(attempt); | |
| live.work.insert(WorkKey::CancelModel(attempt)); | |
| let ports = ports.clone(); | |
| work.push( | |
| async move { | |
| Completion::ModelCancelled { | |
| attempt, | |
| result: ports.cancel_model(run, attempt).await, | |
| } | |
| } | |
| .boxed(), | |
| ); | |
| } | |
| Action::Decide(key) => { | |
| let allow_direct_batch = ports.direct_tool_batching() | |
| && matches!(view.spec.backend, BackendSpec::Model { .. }); | |
| let attempt = key.attempt; | |
| let snapshot = Arc::new(view.clone()); | |
| let mut next_key = Some(key); | |
| while let Some(key) = next_key.take() { | |
| live.work.insert(WorkKey::Tool(key.clone())); | |
| let ports = ports.clone(); | |
| let snapshot = Arc::clone(&snapshot); | |
| work.push( | |
| async move { | |
| let result = ports | |
| .decide_tool(&snapshot, &snapshot.tools[&tool_key(&key)]) | |
| .await; | |
| let prevalidated = match &result { | |
| Ok(prepared) | |
| if matches!( | |
| prepared.decision, | |
| ToolDecision::Approved { approval: None, .. } | |
| ) => | |
| { | |
| ports | |
| .prevalidate_decided_tool( | |
| &snapshot, | |
| &snapshot.tools[&tool_key(&key)], | |
| prepared, | |
| ) | |
| .await | |
| .transpose() | |
| } | |
| _ => None, | |
| }; | |
| Completion::Decided { | |
| key, | |
| control: snapshot.control_version, | |
| result, | |
| prevalidated, | |
| } | |
| } | |
| .boxed(), | |
| ); | |
| if allow_direct_batch | |
| && !retiring | |
| && !fenced | |
| && let Action::Decide(following) = reconcile::next( | |
| &view, | |
| &live, | |
| now, | |
| model_level.as_ref().map(|(_, _, level)| level), | |
| complete_at_budget, | |
| ) | |
| && following.attempt == attempt | |
| { | |
| next_key = Some(following); | |
| } | |
| } | |
| } | |
| Action::Lookup(key) => { | |
| live.looked_up.insert(key.clone()); | |
| live.work.insert(WorkKey::Tool(key.clone())); | |
| let ports = ports.clone(); | |
| let control = view.control_version; | |
| work.push( | |
| async move { | |
| let result = ports.lookup_original(run, key.clone()).await; | |
| Completion::Looked { | |
| key, | |
| control, | |
| result, | |
| } | |
| } | |
| .boxed(), | |
| ); | |
| } | |
| Action::Observe(key) => { | |
| live.work.insert(WorkKey::Tool(key.clone())); | |
| live.observed.insert(key.clone()); | |
| let ports = ports.clone(); | |
| let reference = view.tools[&tool_key(&key)] | |
| .original | |
| .clone() | |
| .expect("observe has original"); | |
| work.push( | |
| async move { | |
| Completion::Observed { | |
| key, | |
| result: ports.observe_operation(reference).await, | |
| } | |
| } | |
| .boxed(), | |
| ); | |
| } | |
| Action::Reject(tool, rejection) => { | |
| let display = view.tools[&tool_key(&tool)] | |
| .decision | |
| .as_ref() | |
| .map(|decision| decision.display().clone()) | |
| .unwrap_or_default(); | |
| self.publish( | |
| Some(&claim), | |
| &view, | |
| vec![Fact::ToolDecided { | |
| tool, | |
| decision: ToolDecision::Rejected { rejection, display }, | |
| }], | |
| vec![], | |
| now, | |
| ) | |
| .await?; | |
| } | |
| Action::Accept(key, outcome) => { | |
| let (mut facts, first_result) = self | |
| .tool_acceptance(&view, key.clone(), outcome.clone()) | |
| .await?; | |
| let mut contents = vec![first_result]; | |
| let mut seen_digests = HashSet::new(); | |
| seen_digests.insert(contents[0].reference().digest.clone()); | |
| let single_plain = facts.len() == 1 | |
| && matches!(facts.first(), Some(Fact::ToolResultAccepted { .. })); | |
| if single_plain { | |
| for (next_key, next_outcome) in reconcile::batch_accept_siblings( | |
| &view, | |
| &live, | |
| now, | |
| complete_at_budget, | |
| &key, | |
| &outcome, | |
| ) { | |
| // Preserve the already-prepared prefix if preparing a later | |
| // sibling fails; the next loop iteration will surface its error | |
| // after the prefix commits. | |
| let Ok((next_facts, next_result)) = | |
| self.tool_acceptance(&view, next_key, next_outcome).await | |
| else { | |
| break; | |
| }; | |
| // Stop at the first non-plain acceptance (e.g. ToolsLoaded or | |
| // cell-coupled acceptance) so later siblings are never reordered | |
| // ahead of it. | |
| if next_facts.len() != 1 | |
| || !matches!( | |
| next_facts.first(), | |
| Some(Fact::ToolResultAccepted { .. }) | |
| ) | |
| { | |
| break; | |
| } | |
| facts.extend(next_facts); | |
| if seen_digests.insert(next_result.reference().digest.clone()) { | |
| contents.push(next_result); | |
| } | |
| } | |
| } | |
| self.publish(Some(&claim), &view, facts, contents, now) | |
| .await?; | |
| } | |
| Action::SelectTransport(attempt, phase) => { | |
| if let Err(error) = ports | |
| .validate_transport_selection(&view, attempt, phase) | |
| .await | |
| { | |
| self.port_failure(&claim, &view, error, now).await?; | |
| return Ok(true); | |
| } | |
| self.publish( | |
| Some(&claim), | |
| &view, | |
| vec![Fact::ModelTransportSelected { attempt, phase }], | |
| vec![], | |
| now, | |
| ) | |
| .await?; | |
| } | |
| Action::ChargeRetry(attempt, phase) => { | |
| let delay = 250_i64 | |
| .saturating_mul(1_i64 << view.retry_streak.backoff_exponent().min(8)); | |
| let provider_due = match &view.attempts[&attempt].recovery { | |
| Some( | |
| AttemptRecovery::FrozenRetrySafe { not_before } | |
| | AttemptRecovery::ContinueFromHistory { not_before }, | |
| ) => *not_before, | |
| _ => None, | |
| }; | |
| let due_at = UnixMillis(now.0.saturating_add(delay)) | |
| .max(provider_due.unwrap_or(now)); | |
| self.publish( | |
| Some(&claim), | |
| &view, | |
| vec![Fact::RetryCharged { | |
| attempt, | |
| phase, | |
| due_at, | |
| diagnostic: if matches!( | |
| view.attempts[&attempt].recovery, | |
| Some(AttemptRecovery::ContinueFromHistory { .. }) | |
| ) { | |
| "continue from recorded history after provider interruption" | |
| } else { | |
| "adapter proved frozen retry safe with no semantic progress" | |
| } | |
| .into(), | |
| }], | |
| vec![], | |
| now, | |
| ) | |
| .await?; | |
| } | |
| Action::Block(reason) => { | |
| if work.is_empty() | |
| && let Some(request) = view.stop_request | |
| && view.stop_assessed != Some(request) | |
| { | |
| let mut facts = vec![]; | |
| if !view.blockers.values().any(|b| { | |
| b.reason == reason | |
| && b.decision.is_none() | |
| && b.resolved_by.is_none() | |
| }) { | |
| facts.push(Fact::Blocked { | |
| blocker: BlockerId::new(), | |
| reason, | |
| }); | |
| } | |
| facts.push(Fact::StopAssessed { request }); | |
| self.publish(Some(&claim), &view, facts, vec![], now) | |
| .await?; | |
| } else { | |
| self.block(&claim, &view, reason, now).await?; | |
| } | |
| } | |
| Action::Wait(reason, due_at) => { | |
| self.publish( | |
| Some(&claim), | |
| &view, | |
| vec![Fact::WaitRegistered { | |
| wait: WaitId::new(), | |
| reason, | |
| due_at, | |
| }], | |
| vec![], | |
| now, | |
| ) | |
| .await?; | |
| } | |
| Action::CancelWait(wait) => { | |
| self.publish( | |
| Some(&claim), | |
| &view, | |
| vec![Fact::WaitCancelled { | |
| wait, | |
| diagnostic: "Run admission fence closed".into(), | |
| }], | |
| vec![], | |
| now, | |
| ) | |
| .await?; | |
| } | |
| Action::Satisfy(wait) => { | |
| let evidence = ContentBlob::json( | |
| &serde_json::json!({"run":run,"revision":view.revision,"at":now}), | |
| )?; | |
| if self | |
| .publish( | |
| Some(&claim), | |
| &view, | |
| vec![Fact::WaitSatisfied { | |
| wait, | |
| evidence: evidence.reference().clone(), | |
| }], | |
| vec![evidence], | |
| now, | |
| ) | |
| .await? | |
| { | |
| match &view.waits[&wait].reason { | |
| WaitReason::Operation { tool } => { | |
| live.observed.remove(tool); | |
| } | |
| WaitReason::External { .. } => { | |
| live.external_observed = false; | |
| } | |
| WaitReason::Model { attempt } => { | |
| live.models_observed.remove(attempt); | |
| } | |
| _ => {} | |
| } | |
| } | |
| } | |
| Action::Terminate(terminal) => { | |
| let diagnostic = view | |
| .external | |
| .as_ref() | |
| .and_then(|task| task.closed.as_ref()) | |
| .filter(|closed| { | |
| terminal == Terminal::Failed && closed.outcome == Terminal::Failed | |
| }) | |
| .and_then(|closed| closed.diagnostic.clone()) | |
| .or_else(|| { | |
| if terminal == Terminal::Failed { | |
| live.preparation_failure.clone() | |
| } else { | |
| None | |
| } | |
| }); | |
| let checkpoint = | |
| if matches!(terminal, Terminal::Completed | Terminal::Cancelled) { | |
| self.checkpoint_for_next_run(&view).await? | |
| } else { | |
| None | |
| }; | |
| let mut facts = termination_facts(&view, terminal, diagnostic); | |
| if let Some(content) = &checkpoint { | |
| facts.insert( | |
| 0, | |
| Fact::ContextCompacted { | |
| baseline: content.reference().clone(), | |
| }, | |
| ); | |
| } | |
| self.publish( | |
| Some(&claim), | |
| &view, | |
| facts, | |
| checkpoint.into_iter().collect(), | |
| now, | |
| ) | |
| .await?; | |
| } | |
| Action::Idle => unreachable!("Idle handled before dispatch"), | |
| } | |
| Ok(false) | |
| }) | |
| .await? | |
| { | |
| continue; | |
| } | |
| // Poll once with the driver's waker so children can wake it even | |
| // while it is awaiting the next frontier read. | |
| let ready = poll_fn(|cx| { | |
| Poll::Ready(match work.poll_next_unpin(cx) { | |
| Poll::Ready(completion) => completion, | |
| Poll::Pending => None, | |
| }) | |
| }) | |
| .await; | |
| let completion = if let Some(completion) = ready { | |
| Some(completion) | |
| } else if idle && !work.is_empty() { | |
| let remaining = view | |
| .spec | |
| .budget | |
| .deadline | |
| .filter(|_| !fenced) | |
| .map(|deadline| { | |
| Duration::from_millis(deadline.0.saturating_sub(now.0).max(0) as u64) | |
| }); | |
| tokio::select! { | |
| result = work.next() => result, | |
| change = changes.changed() => { | |
| change.map_err(|_| StoreError::ConnectionClosed)?; | |
| None | |
| }, | |
| _ = cancellation.cancelled(), if !retiring => None, | |
| _ = retirement.cancelled(), if !retiring => { cancellation.cancel(); None }, | |
| _ = async { match remaining { Some(delay) => tokio::time::sleep(delay).await, None => std::future::pending().await } } => None, | |
| } | |
| } else { | |
| None | |
| }; | |
| if let Some(completion) = completion { | |
| Box::pin(self.complete_next( | |
| &claim, | |
| &ports, | |
| &mut live, | |
| &mut work, | |
| &mut pending_completions, | |
| completion, | |
| &cancellation, | |
| &retirement, | |
| )) | |
| .await?; | |
| } | |
| } | |
| } | |
| #[expect( | |
| clippy::too_many_arguments, | |
| reason = "Coalescing already-ready parallel tool decisions threads the driver's live work, pending queue, and live cancellation tokens" | |
| )] | |
| async fn complete_next( | |
| &self, | |
| claim: &DriverClaim, | |
| ports: &Arc<dyn DriverPorts>, | |
| live: &mut Live, | |
| work: &mut Work, | |
| pending_completions: &mut VecDeque<Completion>, | |
| completion: Completion, | |
| cancellation: &CancellationToken, | |
| retirement: &CancellationToken, | |
| ) -> Result<(), RunError> { | |
| if let Completion::Dispatched { | |
| key, | |
| result: Ok(DispatchOutcome::Recorded { original }), | |
| } = completion | |
| { | |
| let run = claim.run(); | |
| complete_recorded_dispatch(run, live, key, original)?; | |
| // Non-blocking drain of already-ready Recorded tool completions so a | |
| // parallel burst settles with one authoritative frontier read instead of N. | |
| while !cancellation.is_cancelled() && !retirement.is_cancelled() { | |
| if let Some(front) = pending_completions.front() { | |
| if matches!( | |
| front, | |
| Completion::Dispatched { | |
| result: Ok(DispatchOutcome::Recorded { .. }), | |
| .. | |
| } | |
| ) { | |
| if let Some(Completion::Dispatched { | |
| key, | |
| result: Ok(DispatchOutcome::Recorded { original }), | |
| }) = pending_completions.pop_front() | |
| { | |
| complete_recorded_dispatch(run, live, key, original)?; | |
| continue; | |
| } | |
| } else { | |
| break; | |
| } | |
| } | |
| let Some(next) = poll_fn(|cx| { | |
| Poll::Ready(match work.poll_next_unpin(cx) { | |
| Poll::Ready(item) => item, | |
| Poll::Pending => None, | |
| }) | |
| }) | |
| .await | |
| else { | |
| break; | |
| }; | |
| match next { | |
| Completion::Dispatched { | |
| key, | |
| result: Ok(DispatchOutcome::Recorded { original }), | |
| } => { | |
| complete_recorded_dispatch(run, live, key, original)?; | |
| } | |
| other => { | |
| pending_completions.push_back(other); | |
| break; | |
| } | |
| } | |
| } | |
| return Ok(()); | |
| } | |
| if !ports.direct_tool_batching() { | |
| return self | |
| .complete( | |
| claim, | |
| ports, | |
| live, | |
| work, | |
| completion, | |
| cancellation, | |
| retirement, | |
| ) | |
| .await; | |
| } | |
| let Some((attempt, control)) = batchable_decided_scope(&completion) else { | |
| return self | |
| .complete( | |
| claim, | |
| ports, | |
| live, | |
| work, | |
| completion, | |
| cancellation, | |
| retirement, | |
| ) | |
| .await; | |
| }; | |
| let mut batch = Vec::new(); | |
| let mut seen = HashSet::new(); | |
| let first = take_batchable_decided(completion).expect("validated batchable tool decision"); | |
| seen.insert(tool_key(&first.key)); | |
| batch.push(first); | |
| let mut hit_boundary = false; | |
| let mut remaining = VecDeque::with_capacity(pending_completions.len()); | |
| while let Some(queued) = pending_completions.pop_front() { | |
| if batchable_decided_scope(&queued) == Some((attempt, control)) { | |
| let item = | |
| take_batchable_decided(queued).expect("validated batchable tool decision"); | |
| if seen.insert(tool_key(&item.key)) { | |
| batch.push(item); | |
| } | |
| } else { | |
| let boundary = is_batch_collection_boundary(&queued); | |
| remaining.push_back(queued); | |
| if boundary { | |
| remaining.append(pending_completions); | |
| hit_boundary = true; | |
| break; | |
| } | |
| } | |
| } | |
| *pending_completions = remaining; | |
| // Non-blocking drain only: collect already-ready independent sibling decisions | |
| // without waiting on slow siblings or adding any timer, stopping immediately at | |
| // any Run-level blocker/error or non-Decided completion boundary. | |
| while !hit_boundary | |
| && !cancellation.is_cancelled() | |
| && !retirement.is_cancelled() | |
| && let Some(next) = poll_fn(|cx| { | |
| Poll::Ready(match work.poll_next_unpin(cx) { | |
| Poll::Ready(item) => item, | |
| Poll::Pending => None, | |
| }) | |
| }) | |
| .await | |
| { | |
| if batchable_decided_scope(&next) == Some((attempt, control)) { | |
| let item = take_batchable_decided(next).expect("validated batchable tool decision"); | |
| if seen.insert(tool_key(&item.key)) { | |
| batch.push(item); | |
| } | |
| } else { | |
| let boundary = is_batch_collection_boundary(&next); | |
| pending_completions.push_back(next); | |
| if boundary { | |
| break; | |
| } | |
| } | |
| } | |
| self.complete_decided_batch( | |
| claim, | |
| ports, | |
| live, | |
| work, | |
| attempt, | |
| control, | |
| batch, | |
| cancellation, | |
| retirement, | |
| ) | |
| .await | |
| } | |
| #[expect( | |
| clippy::too_many_arguments, | |
| reason = "Batch admission commits prevalidated independent parallel tools atomically against one authoritative frontier with live cancellation checks" | |
| )] | |
| async fn complete_decided_batch( | |
| &self, | |
| claim: &DriverClaim, | |
| ports: &Arc<dyn DriverPorts>, | |
| live: &mut Live, | |
| work: &mut Work, | |
| attempt: ModelAttemptId, | |
| control: u64, | |
| batch: Vec<ReadyDecidedTool>, | |
| cancellation: &CancellationToken, | |
| retirement: &CancellationToken, | |
| ) -> Result<(), RunError> { | |
| for item in &batch { | |
| live.work.remove(&WorkKey::Tool(item.key.clone())); | |
| } | |
| if cancellation.is_cancelled() || retirement.is_cancelled() { | |
| return Ok(()); | |
| } | |
| let run = claim.run(); | |
| let view = self.store.frontier(run).await?; | |
| let now = ports.now(); | |
| let retiring = | |
| cancellation.is_cancelled() || retirement.is_cancelled() || view.terminal.is_some(); | |
| if retiring | |
| || !matches!(view.spec.backend, BackendSpec::Model { .. }) | |
| || control != view.control_version | |
| || view.stopped | |
| || view.recovery_failure().is_some() | |
| || view.finish_requested() | |
| || view.model_steered(attempt) | |
| || view.current_attempt != Some(attempt) | |
| || view | |
| .blockers | |
| .values() | |
| .any(|blocker| blocker.decision.is_none() && blocker.resolved_by.is_none()) | |
| { | |
| return Ok(()); | |
| } | |
| let recovering = live.recovering_tools(&view); | |
| let incomplete = if recovering { | |
| false | |
| } else { | |
| match reconcile::tool_incomplete(&view, attempt) { | |
| Ok(incomplete) => incomplete, | |
| Err(reason) => { | |
| return self | |
| .settle_decided_blocker( | |
| claim, | |
| ports, | |
| view, | |
| control, | |
| attempt, | |
| *reason, | |
| now, | |
| cancellation, | |
| retirement, | |
| ) | |
| .await; | |
| } | |
| } | |
| }; | |
| let admit = !recovering && !incomplete; | |
| let mut changes = Vec::with_capacity(if admit { batch.len() * 2 } else { batch.len() }); | |
| let mut contents = Vec::new(); | |
| let mut seen_contents = HashSet::new(); | |
| let mut dispatch = Vec::with_capacity(if admit { batch.len() } else { 0 }); | |
| for item in batch { | |
| let Some(current) = view.tools.get(&tool_key(&item.key)) else { | |
| continue; | |
| }; | |
| if current.decision.is_some() | |
| || current.admitted | |
| || current.accepted.is_some() | |
| || current.approval_refusal.is_some() | |
| || current.call.provider_executed | |
| { | |
| continue; | |
| } | |
| changes.push(Change::new( | |
| Fact::ToolDecided { | |
| tool: item.key.clone(), | |
| decision: item.prepared.decision.clone(), | |
| }, | |
| now, | |
| )); | |
| for blob in item.prepared.contents { | |
| if seen_contents.insert(blob.reference().digest.clone()) { | |
| contents.push(blob); | |
| } | |
| } | |
| if admit { | |
| changes.push(Change::new( | |
| Fact::ToolAdmitted { | |
| tool: item.key.clone(), | |
| }, | |
| now, | |
| )); | |
| let mut tool = current.clone(); | |
| tool.decision = Some(item.prepared.decision); | |
| tool.admitted = true; | |
| dispatch.push((item.key, tool)); | |
| } | |
| } | |
| if changes.is_empty() || cancellation.is_cancelled() || retirement.is_cancelled() { | |
| return Ok(()); | |
| } | |
| let publication = PendingPublication { | |
| run, | |
| claim: Some(claim.clone()), | |
| head: Head::from(&view), | |
| changes, | |
| contents, | |
| steering: None, | |
| }; | |
| match publication.submit(&self.store).await { | |
| Ok(_) => { | |
| for (key, tool) in dispatch { | |
| live.looked_up.remove(&key); | |
| live.work.insert(WorkKey::Dispatch(key.clone())); | |
| let ports = ports.clone(); | |
| work.push( | |
| async move { | |
| Completion::Dispatched { | |
| key, | |
| result: ports.dispatch_tool(run, tool).await, | |
| } | |
| } | |
| .boxed(), | |
| ); | |
| } | |
| Ok(()) | |
| } | |
| Err(StoreError::RevisionConflict { .. }) => { | |
| // Pre-execution control/Stop fence tripped; re-read authoritative frontier. | |
| Ok(()) | |
| } | |
| Err(source) | |
| if matches!( | |
| source.underlying(), | |
| StoreError::CommitUnknown(_) | StoreError::OperationUnknown | |
| ) => | |
| { | |
| Err(RunError::PublicationUncertain { | |
| publication: Box::new(publication), | |
| source, | |
| }) | |
| } | |
| Err(error) => Err(error.into()), | |
| } | |
| } | |
| #[expect( | |
| clippy::too_many_arguments, | |
| reason = "Completion handling checks live physical cancellation and retirement tokens after reading the authoritative frontier" | |
| )] | |
| async fn complete( | |
| &self, | |
| claim: &DriverClaim, | |
| ports: &Arc<dyn DriverPorts>, | |
| live: &mut Live, | |
| work: &mut Work, | |
| completion: Completion, | |
| cancellation: &CancellationToken, | |
| retirement: &CancellationToken, | |
| ) -> Result<(), RunError> { | |
| let run = claim.run(); | |
| if let Completion::Dispatched { | |
| key, | |
| result: Ok(DispatchOutcome::Recorded { original }), | |
| } = completion | |
| { | |
| return complete_recorded_dispatch(run, live, key, original); | |
| } | |
| let view = self.store.frontier(run).await?; | |
| let now = ports.now(); | |
| let retiring = | |
| cancellation.is_cancelled() || retirement.is_cancelled() || view.terminal.is_some(); | |
| match completion { | |
| completion @ (Completion::PublicationPrepared { .. } | |
| | Completion::PublicationValidated { .. } | |
| | Completion::PublicationLooked { .. } | |
| | Completion::PublicationRecorded { .. }) => { | |
| self.complete_publication( | |
| claim, &view, ports, live, work, completion, now, retiring, | |
| ) | |
| .await?; | |
| } | |
| Completion::Cell { cell, result } => { | |
| self.complete_cell(claim, &view, live, cell, result, now, retiring) | |
| .await?; | |
| } | |
| completion @ (Completion::ExternalPrepared { .. } | |
| | Completion::ExternalConnected { .. } | |
| | Completion::External { .. }) => { | |
| self.complete_external(claim, ports, live, work, completion, retiring) | |
| .await?; | |
| } | |
| Completion::Prepared { | |
| control, | |
| try_attempt, | |
| compaction, | |
| result, | |
| } => { | |
| live.work.remove(&WorkKey::Prepare); | |
| if retiring | |
| || control != view.control_version | |
| || view.stopped | |
| || view.terminal.is_some() | |
| || view.recovery_failure().is_some() | |
| || view.finish_requested() | |
| { | |
| return Ok(()); | |
| } | |
| let prepared = match result { | |
| Ok(value) => value, | |
| Err(RunError::CompactionMismatch) | |
| if compaction | |
| .as_ref() | |
| .is_some_and(|commit| commit.candidate.is_some()) => | |
| { | |
| live.rejected_summaries | |
| .extend(compaction.and_then(|commit| commit.candidate)); | |
| return Ok(()); | |
| } | |
| Err(RunError::CompactionFailed(diagnostic)) => { | |
| live.preparation_failure = Some(diagnostic); | |
| return Ok(()); | |
| } | |
| Err( | |
| error @ (RunError::CompactionMismatch | |
| | RunError::InvalidImage(_) | |
| | RunError::InvalidInput(_) | |
| | RunError::Json(_) | |
| | RunError::Model(_) | |
| | RunError::ModelUnavailable | |
| | RunError::AuthenticationRequired | |
| | RunError::Preparation(_) | |
| | RunError::Store( | |
| StoreError::InvalidTransition(_) | StoreError::Json(_), | |
| )), | |
| ) => { | |
| live.preparation_failure = Some(error.to_string()); | |
| return Ok(()); | |
| } | |
| Err(error) => return self.port_failure(claim, &view, error, now).await, | |
| }; | |
| let admit = | |
| |run, attempt, req: &RequestSpec| ports.model_admission(run, attempt, req); | |
| let (publication, prompt, launch) = PendingPublication::prepared_step( | |
| claim, | |
| view, | |
| live, | |
| Some(&admit), | |
| prepared, | |
| try_attempt, | |
| compaction, | |
| now, | |
| )?; | |
| match publication.submit(&self.store).await { | |
| Ok(_) => { | |
| live.prepared_prompt = prompt; | |
| if let Some(launch) = launch { | |
| let attempt = launch.attempt; | |
| live.work.insert(WorkKey::Model(attempt)); | |
| let ports = ports.clone(); | |
| let witness = | |
| ModelWitness::new(self.store.clone(), claim.clone(), attempt); | |
| work.push( | |
| async move { | |
| Completion::Connected { | |
| attempt, | |
| result: ports | |
| .start_model(run, attempt, launch.input, witness) | |
| .await, | |
| } | |
| } | |
| .boxed(), | |
| ); | |
| } | |
| } | |
| Err(StoreError::RevisionConflict { .. }) => {} | |
| Err(source) | |
| if matches!( | |
| source.underlying(), | |
| StoreError::CommitUnknown(_) | StoreError::OperationUnknown | |
| ) => | |
| { | |
| return Err(RunError::PublicationUncertain { | |
| publication: Box::new(publication), | |
| source, | |
| }); | |
| } | |
| Err(error) => return Err(error.into()), | |
| } | |
| } | |
| Completion::ProactiveSummaryPrepared { control, result } => { | |
| live.work.remove(&WorkKey::PrepareProactiveSummary); | |
| if retiring | |
| || control != view.control_version | |
| || view.recovery_failure().is_some() | |
| || reconcile::budget_exhausted(&view) | |
| || view.check_effect_admission().is_err() | |
| { | |
| return Ok(()); | |
| } | |
| let prepared = match result { | |
| Ok(prepared) => prepared, | |
| Err(error) => { | |
| tracing::warn!(%run, cause = %error, "Background summary preparation failed"); | |
| live.proactive_preparation_failed = true; | |
| return Ok(()); | |
| } | |
| }; | |
| let request = ContentBlob::json(&prepared.request)?; | |
| let mut contents = prepared.contents; | |
| contents.push(request.clone()); | |
| self.publish( | |
| Some(claim), | |
| &view, | |
| vec![Fact::ProactiveSummaryPrepared { | |
| step: prepared.request.step, | |
| request: request.reference().clone(), | |
| attempt: ModelAttemptId::new(), | |
| }], | |
| contents, | |
| now, | |
| ) | |
| .await?; | |
| } | |
| Completion::Decided { | |
| key, | |
| control, | |
| result, | |
| prevalidated, | |
| } => { | |
| live.work.remove(&WorkKey::Tool(key.clone())); | |
| if retiring { | |
| return Ok(()); | |
| } | |
| if control != view.control_version | |
| || view.stopped | |
| || view.terminal.is_some() | |
| || view.recovery_failure().is_some() | |
| || view.finish_requested() | |
| || view.model_steered(key.attempt) | |
| { | |
| return Ok(()); | |
| } | |
| let decision = match result { | |
| Ok(value) => value, | |
| Err(error) => { | |
| let reason = port_error_blocker(error)?; | |
| return self | |
| .settle_decided_blocker( | |
| claim, | |
| ports, | |
| view, | |
| control, | |
| key.attempt, | |
| reason, | |
| now, | |
| cancellation, | |
| retirement, | |
| ) | |
| .await; | |
| } | |
| }; | |
| if let Some(prevalidated) = prevalidated { | |
| match prevalidated { | |
| Ok(PrevalidatedTool::Allowed) | |
| if !live.recovering_tools(&view) | |
| && !matches!( | |
| reconcile::tool_incomplete(&view, key.attempt), | |
| Ok(true) | |
| ) => | |
| { | |
| if let Err(reason) = reconcile::tool_incomplete(&view, key.attempt) { | |
| return self | |
| .settle_decided_blocker( | |
| claim, | |
| ports, | |
| view, | |
| control, | |
| key.attempt, | |
| *reason, | |
| now, | |
| cancellation, | |
| retirement, | |
| ) | |
| .await; | |
| } | |
| let mut tool = view.tools[&tool_key(&key)].clone(); | |
| let publication = PendingPublication { | |
| run, | |
| claim: Some(claim.clone()), | |
| head: Head::from(&view), | |
| changes: vec![ | |
| Change::new( | |
| Fact::ToolDecided { | |
| tool: key.clone(), | |
| decision: decision.decision.clone(), | |
| }, | |
| now, | |
| ), | |
| Change::new(Fact::ToolAdmitted { tool: key.clone() }, now), | |
| ], | |
| contents: decision.contents, | |
| steering: None, | |
| }; | |
| match publication.submit(&self.store).await { | |
| Ok(_) => { | |
| live.looked_up.remove(&key); | |
| tool.decision = Some(decision.decision); | |
| tool.admitted = true; | |
| live.work.insert(WorkKey::Dispatch(key.clone())); | |
| let ports = ports.clone(); | |
| work.push( | |
| async move { | |
| Completion::Dispatched { | |
| key, | |
| result: ports.dispatch_tool(run, tool).await, | |
| } | |
| } | |
| .boxed(), | |
| ); | |
| return Ok(()); | |
| } | |
| Err(StoreError::RevisionConflict { .. }) => { | |
| // Pre-execution control/Stop fence tripped; re-read authoritative frontier. | |
| return Ok(()); | |
| } | |
| Err(source) | |
| if matches!( | |
| source.underlying(), | |
| StoreError::CommitUnknown(_) | StoreError::OperationUnknown | |
| ) => | |
| { | |
| return Err(RunError::PublicationUncertain { | |
| publication: Box::new(publication), | |
| source, | |
| }); | |
| } | |
| Err(error) => return Err(error.into()), | |
| } | |
| } | |
| Ok(PrevalidatedTool::Denied(rejection)) => { | |
| let display = decision.decision.display().clone(); | |
| self.publish( | |
| Some(claim), | |
| &view, | |
| vec![Fact::ToolDecided { | |
| tool: key, | |
| decision: ToolDecision::Rejected { rejection, display }, | |
| }], | |
| vec![], | |
| now, | |
| ) | |
| .await?; | |
| return Ok(()); | |
| } | |
| Ok(PrevalidatedTool::Blocked(reason)) | |
| | Ok(PrevalidatedTool::LookupBlocked(reason)) => { | |
| self.settle_decided_blocker( | |
| claim, | |
| ports, | |
| view, | |
| control, | |
| key.attempt, | |
| reason, | |
| now, | |
| cancellation, | |
| retirement, | |
| ) | |
| .await?; | |
| return Ok(()); | |
| } | |
| Ok(PrevalidatedTool::Found) | Ok(PrevalidatedTool::Allowed) => { | |
| // Fall through to standalone ToolDecided so recovery or existing | |
| // tool-ordering rules run cleanly on the authoritative frontier. | |
| } | |
| Err(error) => { | |
| let reason = port_error_blocker(error)?; | |
| return self | |
| .settle_decided_blocker( | |
| claim, | |
| ports, | |
| view, | |
| control, | |
| key.attempt, | |
| reason, | |
| now, | |
| cancellation, | |
| retirement, | |
| ) | |
| .await; | |
| } | |
| } | |
| } | |
| self.publish( | |
| Some(claim), | |
| &view, | |
| vec![Fact::ToolDecided { | |
| tool: key, | |
| decision: decision.decision, | |
| }], | |
| decision.contents, | |
| now, | |
| ) | |
| .await?; | |
| } | |
| Completion::Looked { | |
| key, | |
| control, | |
| result, | |
| } => { | |
| live.work.remove(&WorkKey::Tool(key.clone())); | |
| if view.model_steered(key.attempt) && !view.tools[&tool_key(&key)].admitted { | |
| return Ok(()); | |
| } | |
| let original = match result { | |
| Ok(value) => value, | |
| Err(error) => { | |
| if retiring { | |
| tracing::warn!(cause=%error,"retired original lookup failed"); | |
| return Ok(()); | |
| } | |
| return self.port_failure(claim, &view, error, now).await; | |
| } | |
| }; | |
| match original { | |
| OriginalLookup::Found(reference) => { | |
| self.publish( | |
| None, | |
| &view, | |
| vec![Fact::ToolOriginalBound { | |
| tool: key, | |
| reference, | |
| }], | |
| vec![], | |
| now, | |
| ) | |
| .await?; | |
| } | |
| OriginalLookup::Blocked(reason) => { | |
| if !retiring { | |
| self.block(claim, &view, reason, now).await?; | |
| } | |
| } | |
| OriginalLookup::Absent if retiring => {} | |
| OriginalLookup::Absent => { | |
| let tool = &view.tools[&tool_key(&key)]; | |
| if tool.admitted { | |
| // An absent intent cannot fence an already granted old driver. | |
| self.block( | |
| claim, | |
| &view, | |
| BlockerKind::OriginalAdmissionUnknown { tool: key }, | |
| now, | |
| ) | |
| .await?; | |
| } else if control == view.control_version | |
| && !view.stopped | |
| && view.terminal.is_none() | |
| { | |
| live.work.insert(WorkKey::Tool(key.clone())); | |
| let snapshot = view.clone(); | |
| let ports = ports.clone(); | |
| work.push( | |
| async move { | |
| let result = ports | |
| .validate_tool(&snapshot, &snapshot.tools[&tool_key(&key)]) | |
| .await; | |
| Completion::Validated { | |
| key, | |
| control, | |
| result, | |
| } | |
| } | |
| .boxed(), | |
| ); | |
| } | |
| } | |
| } | |
| } | |
| Completion::Validated { | |
| key, | |
| control, | |
| result, | |
| } => { | |
| live.work.remove(&WorkKey::Tool(key.clone())); | |
| if retiring { | |
| return Ok(()); | |
| } | |
| if control != view.control_version | |
| || view.stopped | |
| || view.terminal.is_some() | |
| || view.recovery_failure().is_some() | |
| || view.finish_requested() | |
| || view.model_steered(key.attempt) | |
| { | |
| return Ok(()); | |
| } | |
| let validation = match result { | |
| Ok(value) => value, | |
| Err(error) => return self.port_failure(claim, &view, error, now).await, | |
| }; | |
| match validation { | |
| ToolValidation::Allowed => { | |
| if live.recovering_tools(&view) { | |
| return Ok(()); | |
| } | |
| match reconcile::tool_incomplete(&view, key.attempt) { | |
| Ok(true) => return Ok(()), | |
| Ok(false) => {} | |
| Err(reason) => return self.block(claim, &view, *reason, now).await, | |
| } | |
| let mut tool = view.tools[&tool_key(&key)].clone(); | |
| if let Some(ToolDecision::Approved { | |
| approval: Some(id), | |
| display, | |
| .. | |
| }) = &tool.decision | |
| && view.approvals.get(id).is_none_or(|grant| { | |
| grant.expires_at.is_some_and(|expiry| ports.now() >= expiry) | |
| }) | |
| { | |
| self.publish( | |
| Some(claim), | |
| &view, | |
| vec![Fact::ToolDecided { | |
| tool: key, | |
| decision: ToolDecision::Rejected { | |
| display: display.clone(), | |
| rejection: ToolRejection::PolicyDenied { | |
| message: "Approval expired before final admission" | |
| .into(), | |
| }, | |
| }, | |
| }], | |
| vec![], | |
| ports.now(), | |
| ) | |
| .await?; | |
| return Ok(()); | |
| } | |
| let mut facts = Vec::new(); | |
| if let Some(ToolDecision::Approved { | |
| approval: Some(approval), | |
| .. | |
| }) = &tool.decision | |
| && !view.approvals[approval].consumed | |
| { | |
| facts.push(Fact::ApprovalConsumed { | |
| approval: *approval, | |
| tool: key.clone(), | |
| }); | |
| } | |
| facts.push(Fact::ToolAdmitted { tool: key.clone() }); | |
| // Atomic consumption and logical admission precede the external effect. | |
| let admitted = match self | |
| .publish(Some(claim), &view, facts, vec![], ports.now()) | |
| .await | |
| { | |
| Err(error @ RunError::Store(StoreError::InvalidTransition(_))) => { | |
| let current = self.store.frontier(run).await?; | |
| let expired = current | |
| .tools | |
| .get(&tool_key(&key)) | |
| .filter(|tool| !tool.admitted && tool.accepted.is_none()) | |
| .and_then(|tool| match &tool.decision { | |
| Some(ToolDecision::Approved { | |
| approval: Some(id), .. | |
| }) => current.approvals.get(id), | |
| _ => None, | |
| }) | |
| .is_some_and(|grant| { | |
| !grant.consumed | |
| && grant | |
| .expires_at | |
| .is_some_and(|expiry| ports.now() >= expiry) | |
| }); | |
| if !expired { | |
| return Err(error); | |
| } | |
| self.publish( | |
| Some(claim), | |
| ¤t, | |
| vec![Fact::ToolDecided { | |
| tool: key.clone(), | |
| decision: ToolDecision::Rejected { | |
| display: current.tools[&tool_key(&key)].decision.as_ref() | |
| .expect("expired tool has its original approved decision").display().clone(), | |
| rejection: ToolRejection::PolicyDenied { | |
| message: "Approval expired at final SQL admission" | |
| .into(), | |
| }, | |
| }, | |
| }], | |
| vec![], | |
| ports.now(), | |
| ) | |
| .await?; | |
| false | |
| } | |
| result => result?, | |
| }; | |
| if admitted { | |
| live.looked_up.remove(&key); | |
| tool.admitted = true; | |
| live.work.insert(WorkKey::Dispatch(key.clone())); | |
| let ports = ports.clone(); | |
| work.push( | |
| async move { | |
| Completion::Dispatched { | |
| key, | |
| result: ports.dispatch_tool(run, tool).await, | |
| } | |
| } | |
| .boxed(), | |
| ); | |
| } | |
| } | |
| ToolValidation::Denied { rejection } => { | |
| let display = view.tools[&tool_key(&key)] | |
| .decision | |
| .as_ref() | |
| .expect("validation has its original approved decision") | |
| .display() | |
| .clone(); | |
| self.publish( | |
| Some(claim), | |
| &view, | |
| vec![Fact::ToolDecided { | |
| tool: key, | |
| decision: ToolDecision::Rejected { rejection, display }, | |
| }], | |
| vec![], | |
| now, | |
| ) | |
| .await?; | |
| } | |
| ToolValidation::Blocked(reason) => { | |
| self.block(claim, &view, reason, now).await? | |
| } | |
| } | |
| } | |
| Completion::Dispatched { key, result } => { | |
| live.work.remove(&WorkKey::Dispatch(key.clone())); | |
| match result { | |
| Ok(DispatchOutcome::Original(reply)) => { | |
| let (reply, content) = | |
| maka_agent_state::operation::prepare_reply(reply).await?; | |
| self.publish( | |
| None, | |
| &view, | |
| vec![ | |
| Fact::ToolOriginalBound { | |
| tool: key.clone(), | |
| reference: reply.reference.clone(), | |
| }, | |
| Fact::OperationObserved { | |
| tool: key.clone(), | |
| reply, | |
| }, | |
| ], | |
| content.into_iter().collect(), | |
| now, | |
| ) | |
| .await?; | |
| live.observed.insert(key); | |
| } | |
| Ok(DispatchOutcome::Recorded { original }) => { | |
| complete_recorded_dispatch(run, live, key, original)?; | |
| } | |
| Ok(DispatchOutcome::Prevented { rejection }) => { | |
| self.publish( | |
| None, | |
| &view, | |
| vec![Fact::ToolPreventedBeforeHostAdmission { | |
| tool: key, | |
| rejection, | |
| }], | |
| vec![], | |
| now, | |
| ) | |
| .await?; | |
| } | |
| Err( | |
| error @ RunError::Tool( | |
| maka_agent_facts::tools::ToolError::CleanupUnconfirmed(_), | |
| ), | |
| ) => { | |
| let recorded = if retiring { | |
| Ok(()) | |
| } else { | |
| self.block( | |
| claim, | |
| &view, | |
| BlockerKind::ToolCleanupUnconfirmed { | |
| tool: key, | |
| diagnostic: error.to_string(), | |
| }, | |
| now, | |
| ) | |
| .await | |
| }; | |
| ports.cancel_live(run); | |
| recorded?; | |
| return Err(error); | |
| } | |
| Err(RunError::Blocked(reason)) if !retiring => { | |
| self.block(claim, &view, *reason, now).await? | |
| } | |
| Err(RunError::Blocked(_)) if retiring => {} | |
| Err(error) => { | |
| tracing::error!(run = %run, tool = ?key, cause = %error, "original tool admission/result is uncertain"); | |
| // No receipt is not a definite failure or permission to repeat an effect. | |
| if !retiring { | |
| self.block( | |
| claim, | |
| &view, | |
| BlockerKind::OriginalAdmissionUnknown { tool: key }, | |
| now, | |
| ) | |
| .await?; | |
| } | |
| } | |
| } | |
| } | |
| Completion::Observed { key, result } => { | |
| live.work.remove(&WorkKey::Tool(key.clone())); | |
| let result = match result { | |
| Err(RunError::Tool( | |
| maka_agent_facts::tools::ToolError::OperationCleanupUnconfirmed { | |
| message, | |
| observation, | |
| }, | |
| )) => { | |
| tracing::warn!(cause=%message, "original operation reader cleanup remains pending"); | |
| Ok(*observation) | |
| } | |
| other => other, | |
| }; | |
| match result { | |
| Ok(reply) => { | |
| let (reply, content) = | |
| maka_agent_state::operation::prepare_reply(reply).await?; | |
| if view.tools.get(&tool_key(&key)).is_some_and(|tool| { | |
| tool.original.as_ref() == Some(&reply.reference) | |
| && tool.receipt.as_ref() == Some(&reply.receipt) | |
| }) { | |
| return Ok(()); | |
| } | |
| self.publish( | |
| None, | |
| &view, | |
| vec![Fact::OperationObserved { tool: key, reply }], | |
| content.into_iter().collect(), | |
| now, | |
| ) | |
| .await?; | |
| } | |
| Err(RunError::Blocked(reason)) | |
| if !retiring | |
| && matches!( | |
| *reason, | |
| BlockerKind::CurrentAuthorityUnavailable { .. } | |
| ) => | |
| { | |
| self.block(claim, &view, *reason, now).await?; | |
| } | |
| Err(error) => { | |
| tracing::error!(cause=%error,"original operation could not be observed"); | |
| if !retiring { | |
| let tool = &view.tools[&tool_key(&key)]; | |
| let reference = tool | |
| .original | |
| .clone() | |
| .expect("original observation reference"); | |
| let reason = match &tool.receipt { | |
| Some(maka_agent_facts::operation::RecordedReceipt::Recorded { | |
| outcome: | |
| OperationOutcome::Returned { .. } | |
| | OperationOutcome::NotExecuted { .. }, | |
| cleanup, | |
| }) => { | |
| if *cleanup | |
| == maka_plugin_core::operation::OperationCleanup::Confirmed | |
| { | |
| return Ok(()); | |
| } | |
| BlockerKind::CleanupUnconfirmed { reference } | |
| } | |
| _ => BlockerKind::OperationUnknown { | |
| tool: key, | |
| reference, | |
| }, | |
| }; | |
| self.block(claim, &view, reason, now).await?; | |
| } | |
| } | |
| } | |
| } | |
| Completion::Assessed { | |
| attempt, | |
| basis, | |
| result, | |
| } => { | |
| live.work.remove(&WorkKey::Assess(attempt)); | |
| if retiring || Head::from(&view) != basis { | |
| return Ok(()); | |
| } | |
| let evidence = match result { | |
| Ok(value) => value, | |
| Err(error) => return self.port_failure(claim, &view, error, now).await, | |
| }; | |
| self.publish( | |
| Some(claim), | |
| &view, | |
| vec![Fact::AttemptRecoveryRecorded { | |
| attempt, | |
| recovery: evidence.recovery, | |
| }], | |
| evidence.contents, | |
| now, | |
| ) | |
| .await?; | |
| } | |
| Completion::ModelCancelled { attempt, result } => { | |
| live.work.remove(&WorkKey::CancelModel(attempt)); | |
| match result { | |
| Ok(()) => {} | |
| Err(error) if error.is_cleanup_unconfirmed() => { | |
| // The original admitted future still owns its source/ACK obligation. | |
| tracing::warn!(%attempt, cause=%error, "original model cancellation awaits source cleanup evidence"); | |
| } | |
| Err(error) => return Err(error), | |
| } | |
| } | |
| Completion::Connected { attempt, result } => match result { | |
| Ok(connection) => { | |
| read_model(work, attempt, view.attempts[&attempt].observed, connection) | |
| } | |
| Err(error) => { | |
| live.work.remove(&WorkKey::Model(attempt)); | |
| if retiring { | |
| return Ok(()); | |
| } | |
| if matches!( | |
| view.attempts[&attempt].recovery, | |
| Some(AttemptRecovery::ObserveOriginal { .. }) | |
| ) { | |
| let reason = match error { | |
| RunError::Blocked(reason) => *reason, | |
| error => { | |
| tracing::error!(attempt=%attempt,cause=%error,"original model read failed"); | |
| BlockerKind::UnsupportedProtocol { | |
| protocol: "original-model-observation-unavailable".into(), | |
| version: 1, | |
| } | |
| } | |
| }; | |
| self.block(claim, &view, reason, now).await?; | |
| } else if view.proactive_summary != Some(attempt) | |
| && let RunError::Blocked(reason) = error | |
| { | |
| self.block(claim, &view, *reason, now).await?; | |
| } else { | |
| self.publish( | |
| Some(claim), | |
| &view, | |
| vec![Fact::ModelInterrupted { | |
| attempt, | |
| diagnostic: error.to_string(), | |
| }], | |
| vec![], | |
| now, | |
| ) | |
| .await?; | |
| } | |
| } | |
| }, | |
| Completion::Model { | |
| attempt, | |
| index, | |
| connection, | |
| observation, | |
| } => match observation { | |
| Some(Ok(ModelObservation::Recorded { fact })) => { | |
| let original = self.store.fact(run, fact).await?.ok_or_else(|| { | |
| RunError::Internal( | |
| "model source acknowledged an absent original fact".into(), | |
| ) | |
| })?; | |
| match original.fact { | |
| Fact::ModelObserved { | |
| attempt: source, | |
| index: source_index, | |
| .. | |
| } if source == attempt => read_model( | |
| work, | |
| attempt, | |
| source_index | |
| .checked_add(1) | |
| .ok_or_else(|| RunError::Internal("model index overflow".into()))?, | |
| connection, | |
| ), | |
| Fact::AttemptRecoveryRecorded { | |
| attempt: source, .. | |
| } if source == attempt => read_model(work, attempt, index, connection), | |
| Fact::ModelCleanupObserved { | |
| attempt: source, .. | |
| } if source == attempt => read_model(work, attempt, index, connection), | |
| Fact::ModelFirstTokenObserved { attempt: source } | |
| | Fact::ModelPrivatePartialCaptured { | |
| attempt: source, .. | |
| } if source == attempt => read_model(work, attempt, index, connection), | |
| Fact::ModelSourceFailureCaptured { | |
| attempt: source, .. | |
| } if source == attempt => read_model(work, attempt, index, connection), | |
| Fact::ModelClosed { | |
| attempt: source, .. | |
| } if source == attempt => { | |
| live.work.remove(&WorkKey::Model(attempt)); | |
| } | |
| _ => { | |
| return Err(RunError::Internal( | |
| "model source acknowledged another attempt's fact".into(), | |
| )); | |
| } | |
| } | |
| } | |
| other => { | |
| live.work.remove(&WorkKey::Model(attempt)); | |
| if retiring { | |
| return Ok(()); | |
| } | |
| if let Some(Err(error @ RunError::FrozenMemoryDenied(_))) = other { | |
| return Err(error); | |
| } | |
| let diagnostic=match other {Some(Err(error))=>error.to_string(),None=>"Original observation transport ended without an upstream completion receipt".into(),Some(Ok(_))=>unreachable!()}; | |
| if matches!( | |
| view.attempts[&attempt].recovery, | |
| Some(AttemptRecovery::ObserveOriginal { .. }) | |
| ) { | |
| tracing::error!(attempt=%attempt,cause=%diagnostic,"original job is not observable"); | |
| self.block( | |
| claim, | |
| &view, | |
| BlockerKind::UnsupportedProtocol { | |
| protocol: "original-model-observation-unavailable".into(), | |
| version: 1, | |
| }, | |
| now, | |
| ) | |
| .await?; | |
| } else { | |
| self.publish( | |
| Some(claim), | |
| &view, | |
| vec![Fact::ModelInterrupted { | |
| attempt, | |
| diagnostic, | |
| }], | |
| vec![], | |
| now, | |
| ) | |
| .await?; | |
| } | |
| } | |
| }, | |
| } | |
| Ok(()) | |
| } | |
| async fn tool_acceptance( | |
| &self, | |
| view: &RunView, | |
| key: ToolKey, | |
| outcome: AcceptedResult, | |
| ) -> Result<(Vec<Fact>, ContentBlob), RunError> { | |
| let tool = &view.tools[&tool_key(&key)]; | |
| let result = if outcome == AcceptedResult::OutcomeUnknown { | |
| let diagnostic = self.unknown_outcome_diagnostic(view, tool).await?; | |
| ContentBlob::json( | |
| &maka_agent_facts::tool_output::AcceptedToolResult::OutcomeUnknown { | |
| diagnostic: maka_agent_facts::tool_output::ended_unknown_message(&diagnostic), | |
| }, | |
| )? | |
| } else { | |
| accepted_content(view, tool, &outcome)? | |
| }; | |
| let loaded = self.accept_tool_loading(view, tool, &outcome).await?; | |
| let mut acceptance = if let AcceptedResult::CellRecovered { cell } = &outcome { | |
| if !view.stopped | |
| && matches!( | |
| view.cells[cell].status, | |
| maka_agent_facts::cells::CellStatus::Completed { | |
| accepted: false, | |
| .. | |
| } | |
| ) | |
| { | |
| maka_agent_state::cells::acceptance(&view.cells[cell])? | |
| } else { | |
| vec![] | |
| } | |
| } else { | |
| self.accept_cell_with_tool(view, tool).await? | |
| }; | |
| acceptance.push(Fact::ToolResultAccepted { | |
| tool: key, | |
| result: result.reference().clone(), | |
| outcome, | |
| }); | |
| acceptance.extend(loaded); | |
| Ok((acceptance, result)) | |
| } | |
| /// A Host operation carries its diagnostic in the receipt; a local call | |
| /// recorded it as the text of its unknown return. | |
| async fn unknown_outcome_diagnostic( | |
| &self, | |
| view: &RunView, | |
| tool: &Tool, | |
| ) -> Result<String, RunError> { | |
| use maka_agent_facts::{ | |
| artifact::ToolReturn, | |
| tool_output::{StoredToolResult, ToolOutput}, | |
| }; | |
| if let Some(maka_agent_facts::operation::RecordedReceipt::Recorded { | |
| outcome: OperationOutcome::Unknown { diagnostic }, | |
| .. | |
| }) = &tool.receipt | |
| { | |
| return Ok(diagnostic.clone()); | |
| } | |
| let local = tool.local_returned.as_ref().ok_or_else(|| { | |
| RunError::Internal("an unknown result has neither a receipt nor a return".into()) | |
| })?; | |
| let bytes = self.store.content(view.run, local.result.clone()).await?; | |
| let returned: ToolReturn<StoredToolResult> = | |
| serde_json::from_slice(&bytes).map_err(|error| { | |
| RunError::InvalidInput(format!("original tool return is invalid: {error}")) | |
| })?; | |
| match returned.body { | |
| StoredToolResult::Inline(body) if matches!(body.output, ToolOutput::Text(_)) => { | |
| let ToolOutput::Text(text) = body.output else { | |
| unreachable!() | |
| }; | |
| Ok(text) | |
| } | |
| _ => Err(RunError::InvalidInput( | |
| "an unknown tool return has no diagnostic text".into(), | |
| )), | |
| } | |
| } | |
| async fn port_failure( | |
| &self, | |
| claim: &DriverClaim, | |
| view: &RunView, | |
| error: RunError, | |
| now: UnixMillis, | |
| ) -> Result<(), RunError> { | |
| let reason = port_error_blocker(error)?; | |
| self.block(claim, view, reason, now).await | |
| } | |
| } | |
| pub(super) fn termination_facts( | |
| view: &RunView, | |
| terminal: Terminal, | |
| diagnostic: Option<String>, | |
| ) -> Vec<Fact> { | |
| let mut facts = Vec::new(); | |
| if terminal == Terminal::Failed | |
| && let Some(diagnostic) = view.recovery_failure_diagnostic() | |
| { | |
| facts.extend( | |
| view.waits | |
| .iter() | |
| .filter(|(_, wait)| wait.satisfied.is_none() && wait.cancelled.is_none()) | |
| .map(|(wait, _)| Fact::WaitCancelled { | |
| wait: *wait, | |
| diagnostic: "Run could not reconcile the original operation".into(), | |
| }), | |
| ); | |
| facts.push(Fact::RunTerminated { | |
| terminal, | |
| diagnostic: Some(diagnostic), | |
| }); | |
| return facts; | |
| } | |
| if terminal == Terminal::Failed && reconcile::budget_exhausted(view) { | |
| facts.extend( | |
| view.waits | |
| .iter() | |
| .filter(|(_, wait)| { | |
| wait.satisfied.is_none() | |
| && wait.cancelled.is_none() | |
| && matches!(wait.reason, WaitReason::Child { .. }) | |
| }) | |
| .map(|(wait, _)| Fact::WaitCancelled { | |
| wait: *wait, | |
| diagnostic: "Run terminated at its token budget".into(), | |
| }), | |
| ); | |
| } | |
| let diagnostic = diagnostic | |
| .or_else(|| { | |
| let attempt = view | |
| .current_attempt | |
| .filter(|_| terminal == Terminal::Failed) | |
| .and_then(|id| view.attempts.get(&id)) | |
| .filter(|attempt| Some(attempt.step) == view.current_step)?; | |
| attempt | |
| .closed | |
| .as_ref() | |
| .filter(|closed| closed.outcome == maka_agent_facts::accounting::Outcome::Error) | |
| .and_then(|closed| closed.diagnostic.clone()) | |
| .or_else(|| attempt.interrupted.clone()) | |
| }) | |
| .or_else(|| { | |
| (terminal == Terminal::Failed | |
| && !reconcile::budget_exhausted(view) | |
| && reconcile::output_limit_exhausted(view)) | |
| .then(|| reconcile::OUTPUT_LIMIT_DIAGNOSTIC.into()) | |
| }); | |
| facts.push(Fact::RunTerminated { | |
| terminal, | |
| diagnostic, | |
| }); | |
| facts | |
| } | |
| impl Engine { | |
| async fn complete_at_budget(&self, view: &RunView, now: UnixMillis) -> Result<bool, RunError> { | |
| if !reconcile::budget_exhausted(view) | |
| || view.stopped | |
| || view.cancelled_by_risk | |
| || view.spec.budget.deadline.is_some_and(|at| now >= at) | |
| || !matches!(view.spec.backend, BackendSpec::Model { .. }) | |
| { | |
| return Ok(false); | |
| } | |
| let Some(step) = view.current_step else { | |
| return Ok(false); | |
| }; | |
| // Settlement reads the original request even after reopening without model ports. | |
| let request = self.store.request(view.run, step).await?; | |
| Ok(reconcile::complete_at_budget(view, &request, now)) | |
| } | |
| async fn accept_tool_loading( | |
| &self, | |
| view: &RunView, | |
| tool: &Tool, | |
| outcome: &AcceptedResult, | |
| ) -> Result<Vec<Fact>, RunError> { | |
| use maka_agent_facts::artifact::ToolReturn; | |
| #[derive(serde::Deserialize)] | |
| struct ToolLoading { | |
| is_error: bool, | |
| loaded_tools: std::collections::BTreeSet<String>, | |
| } | |
| if !view.spec.tool_discovery || !matches!(outcome, AcceptedResult::Succeeded) { | |
| return Ok(vec![]); | |
| } | |
| let result = tool | |
| .local_returned | |
| .as_ref() | |
| .map(|returned| &returned.result) | |
| .or_else(|| tool.host_returned.as_ref().map(|returned| &returned.result)); | |
| let Some(result) = result else { | |
| return Ok(vec![]); | |
| }; | |
| let bytes = self.store.content(view.run, result.clone()).await?; | |
| let returned: ToolReturn<ToolLoading> = | |
| serde_json::from_slice(&bytes).map_err(|error| { | |
| RunError::InvalidInput(format!("original tool return is invalid: {error}")) | |
| })?; | |
| if returned.schema_version != SCHEMA_VERSION { | |
| return Err(RunError::from(BlockerKind::UnsupportedProtocol { | |
| protocol: "returned-tool-result".into(), | |
| version: returned.schema_version, | |
| })); | |
| } | |
| let ToolLoading { | |
| is_error, | |
| loaded_tools, | |
| } = returned.body; | |
| if is_error { | |
| return Err(StoreError::EventConflict.into()); | |
| } | |
| if loaded_tools.is_empty() { | |
| return Ok(vec![]); | |
| } | |
| Ok(vec![Fact::ToolsLoaded { | |
| tool: tool.key.clone(), | |
| names: loaded_tools, | |
| }]) | |
| } | |
| } | |
| fn accepted_content( | |
| view: &RunView, | |
| tool: &Tool, | |
| outcome: &AcceptedResult, | |
| ) -> Result<ContentBlob, RunError> { | |
| use maka_agent_facts::tool_output::AcceptedToolResult; | |
| let result = | |
| if let AcceptedResult::CellRecovered { cell } = outcome { | |
| let evidence = match &view.cells[cell].status { | |
| maka_agent_facts::cells::CellStatus::Completed { result, .. } => { | |
| maka_agent_facts::tool_output::CellFinalEvidence::Completed { | |
| result: result.clone(), | |
| } | |
| } | |
| maka_agent_facts::cells::CellStatus::Interrupted { report } => { | |
| maka_agent_facts::tool_output::CellFinalEvidence::Interrupted { | |
| report: report.clone(), | |
| } | |
| } | |
| _ => { | |
| return Err(RunError::Internal( | |
| "recovered Cell has no original final evidence".into(), | |
| )); | |
| } | |
| }; | |
| AcceptedToolResult::RecoveredCell { | |
| cell: *cell, | |
| evidence, | |
| } | |
| } else if *outcome == AcceptedResult::ProviderOutcomeUnknown { | |
| AcceptedToolResult::ProviderOutcomeUnknown | |
| } else if let Some(original) = tool.local_returned.as_ref().filter(|local| { | |
| local.settlement != maka_agent_facts::tool_call::ToolSettlement::Unknown | |
| }) { | |
| AcceptedToolResult::Local { | |
| result: original.result.clone(), | |
| } | |
| } else if let Some(result) = &tool.provider_result { | |
| AcceptedToolResult::Provider { | |
| output: result.output.clone(), | |
| is_error: result.is_error, | |
| provider_options: result.provider_options.clone(), | |
| } | |
| } else if let Some(returned) = &tool.host_returned { | |
| AcceptedToolResult::Host { | |
| result: returned.result.clone(), | |
| } | |
| } else if let ( | |
| Some(reference), | |
| Some( | |
| receipt @ maka_agent_facts::operation::RecordedReceipt::Recorded { | |
| outcome: OperationOutcome::Returned { .. }, | |
| .. | |
| }, | |
| ), | |
| ) = (&tool.original, &tool.receipt) | |
| { | |
| AcceptedToolResult::ObservedOperation { | |
| reply: maka_plugin_core::operation::OperationReply { | |
| reference: reference.clone(), | |
| receipt: receipt.clone(), | |
| }, | |
| } | |
| } else if matches!(outcome, AcceptedResult::Rejected) { | |
| let mut diagnostic = tool | |
| .prohibited_before_host | |
| .as_ref() | |
| .or(match &tool.decision { | |
| Some(ToolDecision::Rejected { rejection, .. }) => Some(rejection), | |
| _ => None, | |
| }) | |
| .map(ToString::to_string) | |
| .unwrap_or_else(|| "original provider proved non-execution".into()); | |
| if let Some(arguments) = tool.call.input.invalid_json_diagnostic() { | |
| diagnostic.push('\n'); | |
| diagnostic.push_str(&arguments); | |
| } | |
| AcceptedToolResult::Rejected { diagnostic } | |
| } else { | |
| return Err(RunError::Internal( | |
| "tool acceptance has no matching original typed result".into(), | |
| )); | |
| }; | |
| Ok(ContentBlob::json(&result)?) | |
| } | |
| fn exit(view: &RunView) -> DriverExit { | |
| if let Some(terminal) = &view.terminal { | |
| return DriverExit::Terminal { | |
| run: view.run, | |
| terminal: terminal.clone(), | |
| }; | |
| } | |
| let due_at = view | |
| .waits | |
| .values() | |
| .filter(|wait| wait.satisfied.is_none() && wait.cancelled.is_none()) | |
| .filter_map(|wait| wait.due_at) | |
| .min(); | |
| let blockers: Vec<_> = view | |
| .blockers | |
| .iter() | |
| .filter(|(_, blocker)| blocker.decision.is_none() && blocker.resolved_by.is_none()) | |
| .map(|(id, blocker)| (*id, blocker.reason.clone())) | |
| .collect(); | |
| if blockers.is_empty() { | |
| DriverExit::Waiting { | |
| run: view.run, | |
| due_at, | |
| } | |
| } else { | |
| DriverExit::Blocked { | |
| run: view.run, | |
| blockers, | |
| due_at, | |
| } | |
| } | |
| } |