blob: cbce4d597df60ee179fca1cf856525e82d874f24 [file]
/*
* 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.
*/
//! Fresh authorization for new model dispatch and body replay, never source observations.
use super::*;
use crate::owner::driver::{
same_tool_contract, source_authority_changed, tool_contract_digest, validate_tool_digest,
};
use maka_agent_engine::{PreparedModel, RunError};
use maka_agent_facts::composition::CompositionSurface;
use maka_agent_facts::durable::RequestSpec;
use std::collections::BTreeMap;
impl RunAccess {
pub(in crate::owner) fn bind_model_tools(
&self,
mcp: Arc<crate::mcp::Catalog>,
mode: maka_agent_facts::execution::ToolMode,
) {
assert!(
self.model_tools.set((mcp, mode)).is_ok(),
"one actual activation binds its model tools once"
);
}
pub(in crate::owner) fn current_model_tools(
&self,
workspace: &crate::contract::Workspace,
) -> Result<
(
Arc<crate::mcp::Catalog>,
maka_agent_facts::execution::ToolMode,
),
RunError,
> {
let (activation, mode) = self
.model_tools
.get()
.ok_or_else(|| source_authority_changed("actual activation tools are unavailable"))?;
let owner = self.owner().map_err(RunError::InvalidInput)?;
Ok((
owner
.mcp_projects
.current_activation(workspace, &self.execution.session_id, activation)
.map_err(source_authority_changed)?,
*mode,
))
}
#[expect(
clippy::too_many_arguments,
reason = "Live tool authorization reuses the already-verified snapshot, model and selection"
)]
pub(in crate::owner) async fn authorize_request_tools_with_live(
self: &Arc<Self>,
request: &RequestSpec,
surface: &CompositionSurface,
model: &PreparedModel,
current: &maka_agent_api::session::ResolvedConfiguration,
selection: Arc<crate::tools::SelectionSnapshot>,
skip_digest: bool,
cancellation: &CancellationToken,
) -> Result<(), RunError> {
let owner = self.owner().map_err(RunError::InvalidInput)?;
let (mcp, mode) = self.current_model_tools(&current.workspace)?;
let catalog = owner.run_catalog(self.clone(), &mcp, &selection)?;
let catalog = Self::select_live_tools(&catalog, current, self.spec.tool_discovery);
let availability = maka_tools::availability::Availability::new(
catalog.clone(),
maka_tools::availability::LoadedTools::new(owner.durable.cells().reader(
self.execution.session_id.clone(),
tokio::runtime::Handle::current(),
)),
);
let bound = availability
.capture(
Some(model.provider.tool_context()),
self.execution.clone(),
Some(self.custody.clone()),
current.workspace.path.clone(),
cancellation.clone(),
)
.await?;
if !skip_digest {
validate_tool_digest(request, surface)?;
}
let run = self.execution.run_id;
let code = mode == maka_agent_facts::execution::ToolMode::CodeMode;
let contracts: BTreeMap<String, String> = if code {
if let Some(reference) = &request.tool_contracts {
owner.run_content(run, reference).await?
} else {
let provenance: serde_json::Value =
owner.run_content(run, &request.provenance).await?;
match provenance.get("toolContracts") {
Some(value) => serde_json::from_value(value.clone()).map_err(RunError::Json)?,
// Empty tool contracts are omitted instead of writing an empty "{}" blob.
None => BTreeMap::new(),
}
}
} else {
BTreeMap::new()
};
let selected = self.validate_model_tools_with_contracts(
surface,
&selection,
&catalog,
&bound.all(),
&mcp,
mode,
current,
&contracts,
)?;
if code {
let current = self.live_configuration().await?;
for original in selected {
if current
.tools
.as_ref()
.is_some_and(|names| !names.contains(&original.name))
{
return Err(source_authority_changed(format!(
"selected tool {} is outside current authority",
original.name
)));
}
}
}
Ok(())
}
pub(in crate::owner) async fn authorize_request_memory(
&self,
request: &RequestSpec,
model: &PreparedModel,
budget: &maka_agent_facts::context::PreparedContextBreakdown,
memory: Option<&MemoryProjection>,
) -> Result<(), RunError> {
use maka_agent_state::memory::pinned::Authorize;
fn memory_read_failure(error: maka_event_log::StoreError) -> RunError {
match error {
maka_event_log::StoreError::InvalidTransition(message) => {
RunError::FrozenMemoryDenied(message)
}
error => source_authority_changed(error.to_string()),
}
}
let spec = self.spec();
if spec.memory_projection.is_none() {
return Ok(());
}
let data = self.data_writer().map_err(source_authority_changed)?;
let owner = self.owner().map_err(RunError::InvalidInput)?;
// The policy and the Run ledger live in separate databases. An unchanged
// policy revision brackets the snapshot that authorizes all protected input.
let mut policy = owner
.configuration
.runtime_policy()
.await
.map_err(|e| source_authority_changed(e.to_string()))?;
const MAX_POLICY_CHECKS: usize = 3;
for _ in 0..MAX_POLICY_CHECKS {
if !policy.policy.memory.enabled || !policy.policy.memory.agent_read_enabled {
return Err(RunError::FrozenMemoryDenied(
"admitted memory read permission revoked".into(),
));
}
let current = self.current_configuration().await?;
if current.tools.is_some() {
return Err(RunError::FrozenMemoryDenied(
"admitted memory read permission revoked".into(),
));
}
let mut dependencies = Vec::new();
if let Some(memory) = memory {
if request.accounting.purpose == maka_agent_facts::context::ModelPurpose::Main {
let limit = model.context.as_ref().and_then(|c| {
maka_agent_facts::context::effective_input_limit(
c.model_context_window,
c.context_window,
)
});
memory
.check_window(limit, budget.fixed_tokens())
.map_err(source_authority_changed)?;
}
dependencies = memory
.entries
.iter()
.map(|entry| entry.loaded.clone())
.collect();
}
let scope = if dependencies.iter().any(|entry| entry.scope != "personal") {
owner
.project_memory_scope(&self.execution.session_id, &current)
.await
.map_err(source_authority_changed)?
} else {
let mut scope = owner
.memory_scope(MemoryScope::Personal)
.await
.map_err(source_authority_changed)?;
scope.binding = Some((
self.execution.session_id.clone(),
current.permission_basis(),
));
scope
};
data.query(Authorize {
scope,
dependencies,
})
.await
.map_err(memory_read_failure)?;
let current_policy = owner
.configuration
.runtime_policy()
.await
.map_err(|e| source_authority_changed(e.to_string()))?;
if current_policy.revision == policy.revision {
return Ok(());
}
policy = current_policy;
}
Err(RunError::FrozenMemoryDenied(
"memory policy changed repeatedly during authorization; retry in a new Run".into(),
))
}
#[allow(clippy::too_many_arguments)]
pub(in crate::owner) async fn validate_model_tools(
&self,
request: &RequestSpec,
surface: &CompositionSurface,
selection: &crate::tools::SelectionSnapshot,
raw: &maka_tools::ToolCatalog,
bound: &maka_tools::ToolCatalog,
mcp: &crate::mcp::Catalog,
mode: maka_agent_facts::execution::ToolMode,
) -> Result<(), RunError> {
let owner = self.owner().map_err(RunError::InvalidInput)?;
validate_tool_digest(request, surface)?;
let current = self.live_configuration().await?;
let run = self.execution.run_id;
let code = mode == maka_agent_facts::execution::ToolMode::CodeMode;
let contracts: BTreeMap<String, String> = if code {
if let Some(reference) = &request.tool_contracts {
owner.run_content(run, reference).await?
} else {
let provenance: serde_json::Value =
owner.run_content(run, &request.provenance).await?;
match provenance.get("toolContracts") {
Some(value) => serde_json::from_value(value.clone()).map_err(RunError::Json)?,
// Empty tool contracts are omitted instead of writing an empty "{}" blob.
None => BTreeMap::new(),
}
}
} else {
BTreeMap::new()
};
let selected = self.validate_model_tools_with_contracts(
surface, selection, raw, bound, mcp, mode, &current, &contracts,
)?;
let current = self.live_configuration().await?;
for original in selected {
if current
.tools
.as_ref()
.is_some_and(|names| !names.contains(&original.name))
{
return Err(source_authority_changed(format!(
"selected tool {} is outside current authority",
original.name
)));
}
}
Ok(())
}
#[allow(clippy::too_many_arguments)]
pub(in crate::owner) fn validate_model_tools_with_contracts<'a>(
&self,
surface: &'a CompositionSurface,
selection: &'a crate::tools::SelectionSnapshot,
raw: &maka_tools::ToolCatalog,
bound: &maka_tools::ToolCatalog,
mcp: &crate::mcp::Catalog,
mode: maka_agent_facts::execution::ToolMode,
current: &maka_agent_api::session::ResolvedConfiguration,
contracts: &BTreeMap<String, String>,
) -> Result<Vec<&'a maka_agent_facts::tools::ToolDefinition>, RunError> {
let owner = self.owner().map_err(RunError::InvalidInput)?;
let code = mode == maka_agent_facts::execution::ToolMode::CodeMode;
let selected: Vec<_> = if code {
contracts
.keys()
.map(|name| {
selection
.definitions
.iter()
.find(|tool| &tool.name == name)
.ok_or_else(|| {
source_authority_changed(format!(
"selected tool {name} lost its original declaration"
))
})
})
.collect::<Result<_, _>>()?
} else {
surface
.tools
.iter()
.filter(|tool| tool.name != maka_tools::availability::SEARCH)
.map(Arc::as_ref)
.collect()
};
if let Some(name) =
mcp.unavailable_selected_binding(selected.iter().map(|tool| tool.name.as_str()))
{
return Err(source_authority_changed(format!(
"selected tool {name} MCP connection is unavailable"
)));
}
for original in &selected {
if current
.tools
.as_ref()
.is_some_and(|names| !names.contains(&original.name))
{
return Err(source_authority_changed(format!(
"selected tool {} is outside current authority",
original.name
)));
}
let declared = raw
.definitions()
.find(|tool| tool.name == original.name)
.ok_or_else(|| {
source_authority_changed(format!(
"selected tool {} is no longer bound",
original.name
))
})?;
if code && !same_tool_contract(original, declared) {
return Err(source_authority_changed(format!(
"selected tool {} declaration changed",
original.name
)));
}
if selection.internal_bindings.get(&original.name) != mcp.bindings.get(&original.name) {
return Err(source_authority_changed(format!(
"selected tool {} internal binding changed",
original.name
)));
}
if !selection.matches_service(&original.name, &owner.tool_basis) {
return Err(source_authority_changed(format!(
"selected tool {} binding changed",
original.name
)));
}
}
for original in &selected {
let actual = bound
.definitions()
.find(|tool| tool.name == original.name)
.ok_or_else(|| {
source_authority_changed(format!(
"selected tool {} is unavailable in the current scope",
original.name
))
})?;
let matches = if code {
contracts.get(&original.name) == Some(&tool_contract_digest(actual)?)
} else {
same_tool_contract(original, actual)
};
if !matches {
return Err(source_authority_changed(format!(
"selected tool {} request contract changed",
original.name
)));
}
}
Ok(selected)
}
}