fix(backend): identité requester explicite + policy des tools OpenAI-compatible (#62)
AgentSessionFactory::start propage désormais l'identité du requester aux sessions structurées ; OpenTicketAssistant et LaunchAgent la portent correctement de bout en bout. ToolPolicyRegistry est branché sur AppOpenAiToolInvoker pour combler le trou de parité : TicketToolProvider n'appliquait pas la policy des tools sur le chemin OpenAI-compatible, contrairement au chemin structuré natif. QA vert (échecs de bind loopback écartés comme non-régression préexistante, tracés en #80). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@ -4,11 +4,16 @@
|
||||
//! le serveur MCP : même catalogue, même mapping en `OrchestratorCommand`, même
|
||||
//! `OrchestratorService::dispatch`.
|
||||
|
||||
use std::str::FromStr;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use application::OrchestratorService;
|
||||
use async_trait::async_trait;
|
||||
use domain::ports::{ProjectStore, ToolInvocationError, ToolInvoker, ToolSpec};
|
||||
use domain::ports::{
|
||||
AgentToolPolicyStore, ProjectStore, ToolInvocationError, ToolInvoker, ToolSpec,
|
||||
};
|
||||
use domain::{AgentToolPolicy, IssueRef};
|
||||
use infrastructure::TicketToolProvider;
|
||||
use serde_json::Value;
|
||||
|
||||
const PROJECT_ROOT_ARG: &str = "__ideaProjectRoot";
|
||||
@ -18,6 +23,8 @@ const REQUESTER_ARG: &str = "__ideaRequester";
|
||||
pub struct AppOpenAiToolInvoker {
|
||||
orchestrator: Arc<OrchestratorService>,
|
||||
projects: Arc<dyn ProjectStore>,
|
||||
policies: Arc<dyn AgentToolPolicyStore>,
|
||||
ticket_tools: Arc<dyn TicketToolProvider>,
|
||||
}
|
||||
|
||||
/// Proxy injecté avant que l'orchestrateur soit construit, puis lié dans la
|
||||
@ -68,10 +75,17 @@ impl ToolInvoker for LateBoundOpenAiToolInvoker {
|
||||
impl AppOpenAiToolInvoker {
|
||||
/// Construit l'invoker depuis le service orchestrateur et le store projet.
|
||||
#[must_use]
|
||||
pub fn new(orchestrator: Arc<OrchestratorService>, projects: Arc<dyn ProjectStore>) -> Self {
|
||||
pub fn new(
|
||||
orchestrator: Arc<OrchestratorService>,
|
||||
projects: Arc<dyn ProjectStore>,
|
||||
policies: Arc<dyn AgentToolPolicyStore>,
|
||||
ticket_tools: Arc<dyn TicketToolProvider>,
|
||||
) -> Self {
|
||||
Self {
|
||||
orchestrator,
|
||||
projects,
|
||||
policies,
|
||||
ticket_tools,
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -100,6 +114,7 @@ impl ToolInvoker for AppOpenAiToolInvoker {
|
||||
let project_root = args
|
||||
.get(PROJECT_ROOT_ARG)
|
||||
.and_then(Value::as_str)
|
||||
.map(str::to_owned)
|
||||
.ok_or_else(|| {
|
||||
ToolInvocationError::InvalidArguments(
|
||||
"contexte projet interne absent pour l'outil".to_owned(),
|
||||
@ -108,11 +123,13 @@ impl ToolInvoker for AppOpenAiToolInvoker {
|
||||
let requester = args
|
||||
.get(REQUESTER_ARG)
|
||||
.and_then(Value::as_str)
|
||||
.map(str::to_owned)
|
||||
.ok_or_else(|| {
|
||||
ToolInvocationError::InvalidArguments(
|
||||
"identité requester interne absente pour l'outil".to_owned(),
|
||||
)
|
||||
})?;
|
||||
enforce_tool_policy(self.policies.as_ref(), &requester, name, &value)?;
|
||||
let project = self
|
||||
.projects
|
||||
.list_projects()
|
||||
@ -125,7 +142,20 @@ impl ToolInvoker for AppOpenAiToolInvoker {
|
||||
"projet introuvable pour root `{project_root}`"
|
||||
))
|
||||
})?;
|
||||
let command = infrastructure::orchestrator::mcp::map_tool_call(name, &value, requester)
|
||||
if infrastructure::orchestrator::mcp::tools::is_ticket_tool(name) {
|
||||
let value = self
|
||||
.ticket_tools
|
||||
.handle_ticket_tool(&project, &requester, name, value)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
let detail =
|
||||
serde_json::to_string(&e.to_value()).unwrap_or_else(|_| e.to_string());
|
||||
ToolInvocationError::Execution(detail)
|
||||
})?;
|
||||
return serde_json::to_string(&value)
|
||||
.map_err(|e| ToolInvocationError::Execution(format!("JSON ticket tool: {e}")));
|
||||
}
|
||||
let command = infrastructure::orchestrator::mcp::map_tool_call(name, &value, &requester)
|
||||
.map_err(|e| match e {
|
||||
infrastructure::orchestrator::mcp::ToolMapError::UnknownTool(tool) => {
|
||||
ToolInvocationError::NotFound(tool)
|
||||
@ -147,3 +177,59 @@ impl ToolInvoker for AppOpenAiToolInvoker {
|
||||
Ok(outcome.reply.unwrap_or(outcome.detail))
|
||||
}
|
||||
}
|
||||
|
||||
fn enforce_tool_policy(
|
||||
policies: &dyn AgentToolPolicyStore,
|
||||
requester: &str,
|
||||
name: &str,
|
||||
arguments: &Value,
|
||||
) -> Result<(), ToolInvocationError> {
|
||||
let Some(policy) = policies.get_policy(requester) else {
|
||||
return Ok(());
|
||||
};
|
||||
enforce_policy(&policy, requester, name, arguments)
|
||||
}
|
||||
|
||||
fn enforce_policy(
|
||||
policy: &AgentToolPolicy,
|
||||
requester: &str,
|
||||
name: &str,
|
||||
arguments: &Value,
|
||||
) -> Result<(), ToolInvocationError> {
|
||||
if !policy.permits(name) {
|
||||
return Err(ToolInvocationError::Rejected(format!(
|
||||
"tool `{name}` is not permitted for requester {requester}"
|
||||
)));
|
||||
}
|
||||
if is_ticket_policy_mutation_tool(name) {
|
||||
let raw_ref = arguments
|
||||
.get("ref")
|
||||
.and_then(Value::as_str)
|
||||
.ok_or_else(|| {
|
||||
ToolInvocationError::InvalidArguments(format!(
|
||||
"tool `{name}` requires a ticket ref under the active policy"
|
||||
))
|
||||
})?;
|
||||
let issue_ref = IssueRef::from_str(raw_ref).map_err(|e| {
|
||||
ToolInvocationError::InvalidArguments(format!("invalid ticket ref: {e}"))
|
||||
})?;
|
||||
if !policy.permits_ticket_mutation(name, issue_ref) {
|
||||
return Err(ToolInvocationError::Rejected(format!(
|
||||
"tool `{name}` is not permitted for ticket {issue_ref}"
|
||||
)));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn is_ticket_policy_mutation_tool(name: &str) -> bool {
|
||||
matches!(
|
||||
name,
|
||||
"idea_ticket_update"
|
||||
| "idea_ticket_update_status"
|
||||
| "idea_ticket_update_priority"
|
||||
| "idea_ticket_update_carnet"
|
||||
| "idea_ticket_link"
|
||||
| "idea_ticket_unlink"
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user