Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 51 additions & 16 deletions crates/ironclaw_engine/src/executor/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
use std::sync::Arc;

use crate::memory::RetrievalEngine;
use crate::traits::effect::EffectExecutor;
use crate::traits::effect::{EffectExecutor, ThreadExecutionContext};
use crate::types::capability::{ActionDef, CapabilityLease};
use crate::types::error::EngineError;
use crate::types::memory::MemoryDoc;
use crate::types::message::ThreadMessage;
use crate::types::project::ProjectId;

/// Maximum number of memory docs to inject into context.
const MAX_CONTEXT_DOCS: usize = 5;
Expand All @@ -26,16 +25,19 @@ pub async fn build_step_context(
leases: &[CapabilityLease],
effects: &Arc<dyn EffectExecutor>,
retrieval: Option<&RetrievalEngine>,
project_id: ProjectId,
user_id: &str,
goal: &str,
context: &ThreadExecutionContext,
) -> Result<(Vec<ThreadMessage>, Vec<ActionDef>), EngineError> {
// Fetch actions and memory docs in parallel — they are independent.
let actions_fut = effects.available_actions(leases);
let actions_fut = effects.available_actions(leases, context);
let docs_fut = async {
if let Some(engine) = retrieval {
engine
.retrieve_context(project_id, user_id, goal, MAX_CONTEXT_DOCS)
.retrieve_context(
context.project_id,
&context.user_id,
context.thread_goal.as_deref().unwrap_or(""),
MAX_CONTEXT_DOCS,
)
.await
} else {
Ok(Vec::new())
Expand Down Expand Up @@ -130,9 +132,18 @@ mod tests {
async fn available_actions(
&self,
_: &[CapabilityLease],
_: &crate::traits::effect::ThreadExecutionContext,
) -> Result<Vec<ActionDef>, EngineError> {
Ok(vec![])
}

async fn available_capabilities(
&self,
_: &[CapabilityLease],
_: &crate::traits::effect::ThreadExecutionContext,
) -> Result<Vec<crate::types::capability::CapabilitySummary>, EngineError> {
Ok(vec![])
}
}

#[tokio::test]
Expand Down Expand Up @@ -161,9 +172,17 @@ mod tests {
&[],
&effects,
Some(&retrieval),
project,
"test-user",
"search the web",
&crate::traits::effect::ThreadExecutionContext {
thread_id: crate::types::thread::ThreadId::new(),
thread_type: crate::types::thread::ThreadType::Foreground,
project_id: project,
user_id: "test-user".into(),
step_id: crate::types::step::StepId::new(),
current_call_id: None,
source_channel: None,
user_timezone: None,
thread_goal: Some("search the web".into()),
},
)
.await
.unwrap();
Expand Down Expand Up @@ -191,9 +210,17 @@ mod tests {
&[],
&effects,
None,
ProjectId::new(),
"test-user",
"hello",
&crate::traits::effect::ThreadExecutionContext {
thread_id: crate::types::thread::ThreadId::new(),
thread_type: crate::types::thread::ThreadType::Foreground,
project_id: ProjectId::new(),
user_id: "test-user".into(),
step_id: crate::types::step::StepId::new(),
current_call_id: None,
source_channel: None,
user_timezone: None,
thread_goal: Some("hello".into()),
},
)
.await
.unwrap();
Expand All @@ -217,9 +244,17 @@ mod tests {
&[],
&effects,
Some(&retrieval),
project,
"test-user",
"hello",
&crate::traits::effect::ThreadExecutionContext {
thread_id: crate::types::thread::ThreadId::new(),
thread_type: crate::types::thread::ThreadType::Foreground,
project_id: project,
user_id: "test-user".into(),
step_id: crate::types::step::StepId::new(),
current_call_id: None,
source_channel: None,
user_timezone: None,
thread_goal: Some("hello".into()),
},
)
.await
.unwrap();
Expand Down
149 changes: 146 additions & 3 deletions crates/ironclaw_engine/src/executor/loop_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::traits::llm::LlmBackend;
use crate::types::error::EngineError;
use crate::types::event::EventKind;
use crate::types::message::ThreadMessage;
use crate::types::step::Step;
use crate::types::step::{Step, StepId};
use crate::types::thread::{Thread, ThreadState};

const RUNTIME_CHECKPOINT_METADATA_KEY: &str = "runtime_checkpoint";
Expand Down Expand Up @@ -217,16 +217,37 @@ impl ExecutionLoop {
{
// Fetch active leases (needed for action list)
let active_leases = self.leases.active_for_thread(self.thread.id).await;
let actions = match self.effects.available_actions(&active_leases).await {
let prompt_context = crate::executor::thread_context::thread_execution_context(
&self.thread,
StepId::new(),
None,
);
let actions = match self
.effects
.available_actions(&active_leases, &prompt_context)
.await
{
Ok(a) => a,
Err(e) => {
debug!(thread_id = %self.thread.id, "failed to load actions for system prompt: {e}");
Vec::new()
}
};
let capabilities = match self
.effects
.available_capabilities(&active_leases, &prompt_context)
.await
{
Ok(c) => c,
Err(e) => {
debug!(thread_id = %self.thread.id, "failed to load capabilities for system prompt: {e}");
Vec::new()
}
};
// Build prompt using pre-fetched docs (no extra Store query)
let system_prompt = crate::executor::prompt::build_codeact_system_prompt_with_docs(
&actions,
&capabilities,
&system_docs,
self.platform_info.as_ref(),
);
Expand Down Expand Up @@ -412,7 +433,10 @@ mod tests {
use crate::runtime::messaging::ThreadSignal;
use crate::traits::effect::ThreadExecutionContext;
use crate::traits::llm::{LlmCallConfig, LlmOutput};
use crate::types::capability::{ActionDef, CapabilityLease, EffectType, GrantedActions};
use crate::types::capability::{
ActionDef, CapabilityLease, CapabilityStatus, CapabilitySummary, CapabilitySummaryKind,
EffectType, GrantedActions,
};
use crate::types::project::ProjectId;
use crate::types::step::LlmResponse;
use crate::types::step::{ActionResult, TokenUsage};
Expand Down Expand Up @@ -501,9 +525,18 @@ mod tests {
async fn available_actions(
&self,
_leases: &[CapabilityLease],
_context: &ThreadExecutionContext,
) -> Result<Vec<ActionDef>, EngineError> {
Ok(self.actions.clone())
}

async fn available_capabilities(
&self,
_: &[CapabilityLease],
_: &ThreadExecutionContext,
) -> Result<Vec<crate::types::capability::CapabilitySummary>, EngineError> {
Ok(vec![])
}
}

// ── Helpers ─────────────────────────────────────────────
Expand Down Expand Up @@ -597,6 +630,116 @@ mod tests {
assert!(exec.thread.total_tokens_used > 0);
}

#[tokio::test]
async fn system_prompt_includes_capability_background() {
struct CapturingLlm {
seen_messages: Mutex<Vec<Vec<ThreadMessage>>>,
}

#[async_trait::async_trait]
impl LlmBackend for CapturingLlm {
async fn complete(
&self,
messages: &[ThreadMessage],
_actions: &[ActionDef],
_config: &LlmCallConfig,
) -> Result<LlmOutput, EngineError> {
self.seen_messages.lock().unwrap().push(messages.to_vec());
Ok(text_response("done"))
}

fn model_name(&self) -> &str {
"capturing"
}
}

struct CapabilityEffects;

#[async_trait::async_trait]
impl EffectExecutor for CapabilityEffects {
async fn execute_action(
&self,
_action_name: &str,
_parameters: serde_json::Value,
_lease: &CapabilityLease,
_context: &ThreadExecutionContext,
) -> Result<ActionResult, EngineError> {
Ok(ActionResult {
call_id: String::new(),
action_name: String::new(),
output: serde_json::json!({}),
is_error: false,
duration: Duration::from_millis(1),
})
}

async fn available_actions(
&self,
_leases: &[CapabilityLease],
_context: &ThreadExecutionContext,
) -> Result<Vec<ActionDef>, EngineError> {
Ok(vec![test_action()])
}

async fn available_capabilities(
&self,
_: &[CapabilityLease],
_: &ThreadExecutionContext,
) -> Result<Vec<CapabilitySummary>, EngineError> {
Ok(vec![CapabilitySummary {
name: "telegram".into(),
display_name: Some("Telegram".into()),
kind: CapabilitySummaryKind::Channel,
status: CapabilityStatus::ReadyScoped,
description: Some("Telegram notifications".into()),
routing_hint: Some("Usable through message".into()),
}])
}
}

let project_id = ProjectId::new();
let thread = Thread::new(
"test goal",
ThreadType::Foreground,
project_id,
"test-user",
ThreadConfig::default(),
);
let tid = thread.id;

let llm = Arc::new(CapturingLlm {
seen_messages: Mutex::new(Vec::new()),
});
let effects: Arc<dyn EffectExecutor> = Arc::new(CapabilityEffects);
let leases = Arc::new(LeaseManager::new());
let policy = Arc::new(PolicyEngine::new());
leases
.grant(tid, "tools", GrantedActions::All, None, None)
.await
.unwrap();
let (_tx, rx) = crate::runtime::messaging::signal_channel(16);

let mut exec = ExecutionLoop::new(
thread,
llm.clone(),
effects,
leases,
policy,
rx,
"test-user".into(),
);

let outcome = exec.run().await.unwrap();
assert!(matches!(outcome, ThreadOutcome::Completed { response: Some(r) } if r == "done"));

let seen = llm.seen_messages.lock().unwrap();
let system_prompt = &seen[0][0].content;
assert!(system_prompt.contains("## Available capabilities (background status)"));
assert!(system_prompt.contains("`telegram`"));
assert!(system_prompt.contains("ready_scoped"));
assert!(system_prompt.contains("Usable through message"));
}

#[tokio::test]
async fn action_then_text() {
let (mut exec, _tx) = make_loop(
Expand Down
1 change: 1 addition & 0 deletions crates/ironclaw_engine/src/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub mod orchestrator;
pub mod prompt;
pub mod scripting;
pub mod structured;
pub(crate) mod thread_context;
pub mod trace;

pub use loop_engine::ExecutionLoop;
Expand Down
Loading
Loading