1use crate::RunEnv as Env;
19use crate::async_events::RuntimeEvent;
20use crate::lane::{Provider, Rlm, rlm_over};
21use crate::session_stats::{GuardOutcome, SessionStats};
22use agent::{AgentOptions, AgentTool, StreamFn};
23use ai::{Message, Model, UserMessage, UserMessageContent};
24use delegation::agents::{Agents, AgentsOptions, Completion, PredictCall, predict_adapter};
25use delegation::child::agent_runner;
26use delegation::host::{ChildRun, ChildRunner, Delegation, DelegationOptions, DetachedEnd};
27use delegation::usage::UsageLedger;
28use host::Host;
29use host::file_hooks::{FileHooks, GuardRecord, OnRecord};
30use host::loki_guard::{LokiGuard, LokiMode, git_root, loki_mode};
31use inference::Inference;
32use jsrt::sync;
33use kernel::store::{FileDocumentStore, MemoryDocumentStore};
34use kernel::{ContinuityPolicy, HostHandler, Kernel, KernelOptions, Signal};
35use kernel_apis::{
36 CodeSkills, CodeSkillsOptions, DurableWork, DurableWorkOptions, GoalDriver, HintsModule, KernelApis, MemoryApi,
37 MemoryBackend, MemoryService, SchedulesModule, SkillSource, SkillsModule, TaskGoalJobs,
38};
39use lanes::Park;
40use lanes::{CellEnd, Lanes, LanesOptions};
41use serde_json::{Map, Value, json};
42use std::collections::HashMap;
43use std::path::{Path, PathBuf};
44use std::sync::mpsc::Receiver;
45use std::sync::{Arc, Condvar, Mutex, Weak};
46use std::time::Duration;
47
48pub const CONTEXT_REMINDER_ENV: &str = "ULTRON_CONTEXT_REMINDER";
49pub const CONTEXT_REMINDER_TOKENS_ENV: &str = "ULTRON_CONTEXT_REMINDER_TOKENS";
50pub const GUIDE_ENV: &str = "ULTRON_GUIDE";
51pub const LOKI_ENV: &str = "ULTRON_LOKI";
52pub const LOKI_TIMEOUT_MS_ENV: &str = "ULTRON_LOKI_TIMEOUT_MS";
53pub const MASK_SECRETS_ENV: &str = "ULTRON_MASK_SECRETS";
54pub const READ_HANDLE_BYTES_ENV: &str = "ULTRON_READ_HANDLE_BYTES";
55
56pub const EXTENSIONS_UNSUPPORTED: &str = "Pi extensions are not supported by ultron-rs";
58
59pub type McpListener = Arc<dyn Fn(&Arc<mcp::gateway::Gateway>) + Send + Sync>;
61
62#[derive(Clone, Default)]
64pub struct Services {
65 pub memory: Option<Arc<dyn MemoryBackend>>,
69 pub on_mcp: Option<McpListener>,
71 pub mcp_notices: Option<mcp::gateway::NoticeSink>,
74 pub session: crate::session_flags::SessionFlags,
76 pub resources: Arc<crate::session_resources::SessionResources>,
78 pub values: Option<services::SessionValues>,
82 pub on_schedules: Option<kernel_apis::prompt_schedule::Listener>,
85 pub agent_lanes: Option<AgentLanes>,
88}
89
90pub type AgentLanes = Arc<dyn Fn(Weak<Stack>) -> delegation::agents::LaneRunner + Send + Sync>;
92
93pub struct Config {
96 pub cwd: PathBuf,
97 pub home: PathBuf,
98 pub agent_dir: PathBuf,
99 pub settings: Value,
101 pub loki: LokiMode,
102 pub loki_hygiene: loki::hygiene::Level,
104 pub loki_root: Option<PathBuf>,
106 pub runtime_path: PathBuf,
107 pub skills_dir: PathBuf,
108 pub mask_secrets: Option<String>,
109 pub output_bytes: Option<String>,
110 pub cell_yield_after: Option<String>,
113 pub snapshot_key: Option<Vec<u8>>,
114 pub spawn_depth: u32,
115 pub async_events: bool,
117 pub ts_guide: bool,
119 pub hints: bool,
121 pub turn_guards: bool,
123 pub result_footer: bool,
126 pub tool_rounds_nudge: u64,
128 pub skill_nudge: u64,
130 pub context_images: usize,
132 pub frame_env: HashMap<String, String>,
135 pub var: crate::VarLookup,
137 pub masker: Arc<host::secrets::OutputMasker>,
140 pub project_trusted: bool,
142 pub provider_token: mcp::connection::ProviderToken,
144 pub outside_changes: host::file_hooks::OutsideChanges,
146 pub lsp: Option<lsp::LspGuard>,
148 pub terminal_theme: Arc<std::sync::Mutex<Option<String>>>,
151}
152
153pub fn workspace_snapshotter() -> host::file_hooks::Snapshotter {
155 std::sync::Arc::new(|dir: &Path| {
156 let picture = delegation::workspace::snapshot_workspace(dir);
157 Ok(host::file_hooks::Snapshot {
158 root: picture.root,
159 entries: picture.entries,
160 complete: picture.complete,
161 })
162 })
163}
164
165const SESSION_ENV_NAMES: &[&str] = &[
168 "PATH",
169 "HOME",
170 "USER",
171 "LOGNAME",
172 "SHELL",
173 "TERM",
174 "LANG",
175 "LANGUAGE",
176 "LC_ALL",
177 "LC_CTYPE",
178 "TZ",
179 "TMPDIR",
180 "XDG_CONFIG_HOME",
181 "XDG_DATA_HOME",
182 "XDG_CACHE_HOME",
183 "XDG_STATE_HOME",
184 "XDG_RUNTIME_DIR",
185 "PYTHONPATH",
186 "PYTHONHOME",
187 "VIRTUAL_ENV",
188 config::env::OFFLINE_ENV,
189 config::env::SKIP_VERSION_CHECK_ENV,
190 kernel::env::PYTHON_ENV,
191 kernel::env::RLM_ENV_ALLOW_ENV,
192 kernel::env::BASH_YIELD_AFTER_ENV,
193 kernel::env::RLM_MAX_MEMORY_MB_ENV,
194 kernel::env::RLM_MAX_CPU_SECONDS_ENV,
195 kernel::env::RLM_OUTPUT_BYTES_ENV,
196 kernel::env::RLM_MAX_TREE_MEMORY_MB_ENV,
197 kernel::env::RLM_TREE_MEMORY_BACKEND_ENV,
198];
199
200pub fn session_environment(var: &dyn Fn(&str) -> Option<String>) -> Vec<(String, String)> {
205 let mut names: Vec<String> = std::env::vars_os()
206 .filter_map(|(name, _)| name.into_string().ok())
207 .collect();
208 for name in SESSION_ENV_NAMES {
209 if !names.iter().any(|known| known == name) {
210 names.push((*name).to_string());
211 }
212 }
213 names
214 .into_iter()
215 .filter_map(|name| var(&name).map(|value| (name, value)))
216 .collect()
217}
218
219pub fn ts_guide(var: &dyn Fn(&str) -> Option<String>) -> bool {
222 var(GUIDE_ENV).as_deref() == Some("ts")
223}
224
225pub fn hints(var: &dyn Fn(&str) -> Option<String>) -> bool {
228 kernel_apis::cell_hints::hints_enabled(var(kernel_apis::cell_hints::HINTS_ENV).as_deref())
229}
230
231pub const TURN_GUARDS_ENV: &str = "ULTRON_TURN_GUARDS";
233
234pub fn turn_guards(var: &dyn Fn(&str) -> Option<String>) -> bool {
238 !ts_guide(var) && !crate::flag_off(var(TURN_GUARDS_ENV).as_deref())
239}
240
241impl Config {
242 pub fn child_env(&self) -> Vec<(String, String)> {
243 session_environment(&*self.var)
244 }
245
246 pub fn from_env(env: &Env) -> Config {
248 let agent_dir = env.agent_dir();
249 let manager = config::settings::SettingsManager::for_worker(&env.cwd, &agent_dir, &env.home);
251 let settings = Value::Object(manager.settings().clone());
252 let loki_settings = loki::settings::saved(manager.global_settings());
253 let loki = loki_mode((env.var)(LOKI_ENV).as_deref(), &loki_settings);
254 let loki_hygiene = loki::hygiene::level((env.var)(loki::hygiene::HYGIENE_ENV).as_deref(), &loki_settings);
255 let loki_root = (loki != LokiMode::Off).then(|| git_root(&env.cwd).unwrap_or_else(|| env.cwd.clone()));
256 let skills_dir = (env.var)(kernel::env::CODE_SKILLS_DIR_ENV)
257 .map(|dir| dir.trim().to_string())
258 .filter(|dir| !dir.is_empty())
259 .map_or_else(|| agent_dir.join("skills"), PathBuf::from);
260 let lsp = lsp::enabled((env.var)(lsp::LSP_ENV).as_deref(), &settings)
261 .then(|| lsp::LspGuard::new(session_environment(&*env.var)));
262 Config {
263 cwd: env.cwd.clone(),
264 home: env.home.clone(),
265 snapshot_key: kernel::snapshot_store::session_snapshot_key(&agent_dir, &*env.var),
266 agent_dir,
267 settings,
268 loki,
269 loki_hygiene,
270 loki_root,
271 runtime_path: env.runtime(),
272 skills_dir,
273 mask_secrets: (env.var)(MASK_SECRETS_ENV),
274 output_bytes: (env.var)(kernel::env::RLM_OUTPUT_BYTES_ENV),
275 cell_yield_after: (env.var)(kernel::env::CELL_YIELD_AFTER_ENV),
276 spawn_depth: delegation::host::spawn_depth_limit((env.var)(delegation::host::SPAWN_DEPTH_ENV).as_deref()),
277 async_events: crate::async_events::async_events_enabled(
278 (env.var)(crate::async_events::ASYNC_EVENTS_ENV).as_deref(),
279 ),
280 ts_guide: ts_guide(&*env.var),
281 hints: hints(&*env.var),
282 turn_guards: turn_guards(&*env.var),
283 result_footer: !ts_guide(&*env.var) && config::keys::result_footer(manager.global_settings()),
284 tool_rounds_nudge: crate::nudge::tool_rounds_nudge_from_env(
285 (env.var)(crate::nudge::TOOL_ROUNDS_NUDGE_ENV).as_deref(),
286 ),
287 skill_nudge: crate::nudge::skill_nudge_from_env((env.var)(crate::nudge::SKILL_NUDGE_ENV).as_deref()),
288 context_images: crate::view_images::context_image_budget(
289 (env.var)(crate::view_images::CONTEXT_IMAGES_ENV).as_deref(),
290 ),
291 frame_env: inference::frame_env(&*env.var),
292 var: env.var.clone(),
293 masker: Arc::default(),
294 project_trusted: manager.is_project_trusted(),
295 outside_changes: host::file_hooks::OutsideChanges::default(),
296 lsp,
297 terminal_theme: Arc::default(),
298 provider_token: {
299 let env = env.clone();
300 Arc::new(move |provider: &str| crate::lane::api_key(&crate::ModelFlags::default(), &env, provider))
301 },
302 }
303 }
304
305 pub fn settings_reader(&self) -> Arc<dyn Fn() -> config::settings::SettingsManager + Send + Sync> {
308 let (cwd, agent_dir, trusted) = (self.cwd.clone(), self.agent_dir.clone(), self.project_trusted);
309 Arc::new(move || config::settings::SettingsManager::create(&cwd, &agent_dir, trusted))
310 }
311
312 pub fn bundled_skills(&self) -> Option<String> {
315 let root = self.runtime_path.ancestors().nth(4)?;
316 crate::session_resources::bundled_skills(&root.join("skills"), &*self.var)
317 }
318
319 pub fn mcp_gateway(&self) -> Arc<mcp::gateway::Gateway> {
322 let masker = self.masker.clone();
323 mcp::gateway::Gateway::new(mcp::gateway::GatewayOptions {
324 agent_dir: self.agent_dir.clone(),
325 project_trusted: self.project_trusted,
326 servers: None,
327 env: mcp::connection::ConnectionEnv {
328 cwd: self.cwd.clone(),
329 home: self.home.clone(),
330 env_overlay: HashMap::new(),
331 client_version: config::version().to_string(),
332 log: Some(Arc::new(mcp::log::ServerLog::new(self.agent_dir.join("mcp.log")))),
333 secrets: Some(Arc::new(move |values: &[String]| {
334 let values: Vec<Value> = values.iter().map(|v| Value::String(v.clone())).collect();
335 masker.register_secret_values(&values);
336 })),
337 provider_token: Some(self.provider_token.clone()),
338 proxy: Some(ai::net::ProxyEnv::from_lookup(&*self.var)),
340 var: Some(self.var.clone()),
341 ..mcp::connection::ConnectionEnv::default()
342 },
343 open_url: Some(config::browser::browser_opener(
345 (self.var)("PATH"),
346 (self.var)("HOME"),
347 config::browser::opener_vars(&*self.var),
348 )),
349 })
350 }
351
352 pub fn model(&self, provider: &str, id: &str) -> Option<Model> {
354 let env = Env {
355 cwd: self.cwd.clone(),
356 home: self.home.clone(),
357 var: self.var.clone(),
358 package: PathBuf::new(),
359 };
360 crate::lane::catalog_model(provider, id, &env).ok()
361 }
362
363 pub fn find_models(&self) -> Value {
366 let env = Env {
367 cwd: self.cwd.clone(),
368 home: self.home.clone(),
369 var: self.var.clone(),
370 package: PathBuf::new(),
371 };
372 Value::Array(
373 crate::lane::available_models(&env)
374 .iter()
375 .map(|m| json!({"provider": m.provider, "id": m.id, "name": m.name}))
376 .collect(),
377 )
378 }
379
380 pub fn race_models(&self) -> Value {
382 let picked = config::keys::rlm_model_settings((self.settings_reader())().global_settings()).race_models;
383 picked
384 .into_iter()
385 .filter(|spec| spec.split_once('/').is_some_and(|(p, id)| self.model(p, id).is_some()))
386 .map(Value::String)
387 .collect()
388 }
389
390 fn model_check(self: &Arc<Self>) -> delegation::host::ModelCheck {
392 let config = self.clone();
393 Arc::new(move |spec: &str| {
394 match spec
395 .split_once('/')
396 .and_then(|(provider, id)| config.model(provider, id))
397 {
398 Some(_) => Ok(()),
399 None => Err(format!("model must be provider/model naming a known model, not {spec}")),
400 }
401 })
402 }
403
404 fn file_hooks_in(&self, cwd: &Path, record: Option<OnRecord>) -> FileHooks {
409 let mut hooks = FileHooks::with_snapshots(cwd, workspace_snapshotter());
410 hooks.ignore_outside(self.outside_changes.clone());
411 hooks.protect(config::paths::AgentPaths::new(&self.agent_dir).protected(), &self.home);
412 match (
413 crate::guard_log::loki_log((self.var)(crate::guard_log::LOKI_LOG_ENV).as_deref()),
414 record,
415 ) {
416 (Some(log), Some(record)) => hooks.on_record(Arc::new(move |outcome: &GuardRecord| {
417 log(outcome);
418 record(outcome);
419 })),
420 (Some(only), None) | (None, Some(only)) => hooks.on_record(only),
421 (None, None) => {}
422 }
423 if let Some(root) = &self.loki_root {
424 let root = if cwd == self.cwd {
425 root.clone()
426 } else {
427 git_root(cwd).unwrap_or_else(|| cwd.to_path_buf())
428 };
429 let post_write = git_root(cwd).is_some() && root.join(".loki").is_dir();
432 hooks.add(Box::new(
433 LokiGuard::new(&root, self.loki)
434 .with_post_write(post_write)
435 .with_hygiene(self.loki_hygiene)
436 .with_project_python(crate::guard_log::project_python(&*self.var, &self.cwd))
437 .with_timeout(host::loki_guard::loki_timeout(
438 (self.var)(LOKI_TIMEOUT_MS_ENV).as_deref(),
439 )),
440 ));
441 }
442 if let Some(lsp) = &self.lsp {
443 hooks.add(Box::new(lsp.clone()));
444 }
445 hooks
446 }
447
448 fn session_dir(&self, kind: &str, session_id: &str) -> PathBuf {
450 self.agent_dir.join(kind).join(session_id)
451 }
452}
453
454fn lane_dir(lane: &str) -> String {
456 lane.chars()
457 .map(|c| {
458 if c.is_ascii_alphanumeric() || "._-".contains(c) {
459 c
460 } else {
461 '_'
462 }
463 })
464 .collect()
465}
466
467fn max_live_kernels(settings: &Value) -> usize {
469 settings["rlm"]["maxLiveKernels"]
470 .as_u64()
471 .filter(|n| *n >= 1)
472 .map_or(16, |n| n.min(1024) as usize)
473}
474
475fn lane_snapshot_path(config: &Arc<Config>, session_id: &str) -> lanes::SnapshotPathFn {
478 let snapshots = config.session_dir("rlm-snapshots", session_id);
479 Arc::new(move |lane: &str| {
480 Some(
481 snapshots
482 .join(format!("{}.snapshot", lane_dir(lane)))
483 .display()
484 .to_string(),
485 )
486 })
487}
488
489pub fn mcp_runtime_guide(config: &Config) -> resources::runtime_guide::RuntimeGuideOptions {
491 let tool = config.mcp_gateway().tool();
492 resources::runtime_guide::RuntimeGuideOptions {
493 mcp_servers: tool
494 .as_ref()
495 .map(|t| mcp::gateway::server_names_of(&t.description))
496 .unwrap_or_default(),
497 extension_tools: tool
500 .into_iter()
501 .map(|t| resources::runtime_guide::ExtensionToolSummary {
502 name: t.name,
503 description: t.description,
504 })
505 .collect(),
506 durable_apis: !config.ts_guide,
509 delivery: if config.async_events {
512 resources::runtime_guide::EventDelivery::Wake
513 } else {
514 resources::runtime_guide::EventDelivery::Off
515 },
516 host_waits: !config.ts_guide,
519 hints: config.hints,
521 result_footer: config.result_footer,
522 ..Default::default()
523 }
524}
525
526#[derive(Clone)]
530pub struct SystemPrompt(Arc<PromptState>);
531
532type RenderPrompt = dyn Fn(&crate::lane::PromptMode) -> Result<(String, Vec<String>), String> + Send + Sync;
533type RenderChildPrompt = dyn Fn(&crate::lane::PromptMode, ChildReader) -> Result<String, String> + Send + Sync;
534
535#[derive(Clone, Copy, Debug, PartialEq, Eq)]
538pub struct ChildReader {
539 pub in_memory: bool,
540 pub memory: crate::memory_guidance::MemoryGuidance,
541}
542
543struct PromptState {
544 render: Option<Box<RenderPrompt>>,
546 render_child: Option<Box<RenderChildPrompt>>,
548 child: Mutex<Option<(crate::lane::PromptMode, ChildReader, String)>>,
550 mode: Box<dyn Fn() -> crate::lane::PromptMode + Send + Sync>,
551 current: Mutex<(crate::lane::PromptMode, String, Vec<String>)>,
552 persona: Mutex<Option<bool>>,
554 model: Mutex<String>,
556}
557
558impl SystemPrompt {
559 pub fn fixed(text: impl Into<String>) -> SystemPrompt {
560 SystemPrompt(Arc::new(PromptState {
561 render: None,
562 render_child: None,
563 child: Mutex::new(None),
564 mode: Box::new(crate::lane::PromptMode::default),
565 current: Mutex::new((Default::default(), text.into(), Vec::new())),
566 persona: Mutex::new(None),
567 model: Mutex::new(String::new()),
568 }))
569 }
570
571 fn rendered(
574 env: &Env,
575 config: &Config,
576 trusted: bool,
577 guide: resources::runtime_guide::RuntimeGuideOptions,
578 flags: &crate::session_flags::SessionFlags,
579 extra: Arc<crate::session_resources::SessionResources>,
580 ) -> Result<SystemPrompt, String> {
581 let (cwd, agent_dir) = (env.cwd.clone(), config.agent_dir.clone());
582 let (no_skills, no_context_files) = (flags.no_skills, flags.no_context_files);
583 let mode = move || {
584 let settings = config::settings::SettingsManager::create(&cwd, &agent_dir, trusted);
585 crate::lane::PromptMode {
586 no_skills,
587 no_context_files,
588 ..crate::lane::PromptMode::from_settings(&settings)
589 }
590 };
591 let (env, loki_root) = (env.clone(), config.loki_root.clone());
592 let (child_env, child_loki, child_extra) = (env.clone(), loki_root.clone(), extra.clone());
593 let child_guide = guide.clone();
594 let render = move |mode: &crate::lane::PromptMode| {
595 crate::lane::system_prompt_with_resources(
596 &env,
597 &env.cwd.display().to_string(),
598 loki_root.as_deref(),
599 &guide,
600 mode,
601 &extra,
602 )
603 };
604 let render_child = move |mode: &crate::lane::PromptMode, reader: ChildReader| {
607 let guide = resources::runtime_guide::RuntimeGuideOptions {
608 reader: resources::runtime_guide::GuideReader::Subagent {
609 swarm: !reader.in_memory,
610 },
611 memory: reader.memory,
612 ..child_guide.clone()
613 };
614 crate::lane::system_prompt_with_resources(
615 &child_env,
616 &child_env.cwd.display().to_string(),
617 child_loki.as_deref(),
618 &guide,
619 mode,
620 &child_extra,
621 )
622 .map(|(text, _)| text)
623 };
624 let initial = mode();
625 let (text, warnings) = render(&initial)?;
626 Ok(SystemPrompt(Arc::new(PromptState {
627 render: Some(Box::new(render)),
628 render_child: Some(Box::new(render_child)),
629 child: Mutex::new(None),
630 mode: Box::new(mode),
631 current: Mutex::new((initial, text, warnings)),
632 persona: Mutex::new(None),
633 model: Mutex::new(String::new()),
634 })))
635 }
636
637 pub fn current(&self) -> String {
639 let state = &self.0;
640 let Some(render) = &state.render else {
641 return sync::lock(&state.current).1.clone();
642 };
643 let mut mode = (state.mode)();
644 if let Some(persona) = *sync::lock(&state.persona) {
645 mode.persona = persona;
646 }
647 mode.model = sync::lock(&state.model).clone();
648 let mut current = sync::lock(&state.current);
649 if current.0 != mode
650 && let Ok((text, warnings)) = render(&mode)
651 {
652 *current = (mode, text, warnings);
653 }
654 current.1.clone()
655 }
656
657 pub fn child_current(&self, reader: ChildReader, model: &str) -> String {
660 let state = &self.0;
661 let Some(render) = &state.render_child else {
662 return self.current();
663 };
664 let mut mode = (state.mode)();
665 if let Some(persona) = *sync::lock(&state.persona) {
666 mode.persona = persona;
667 }
668 mode.model = model.to_string();
669 let mut child = sync::lock(&state.child);
670 if let Some((cached, cached_reader, text)) = &*child
671 && *cached == mode
672 && *cached_reader == reader
673 {
674 return text.clone();
675 }
676 match render(&mode, reader) {
677 Ok(text) => {
678 *child = Some((mode, reader, text.clone()));
679 text
680 }
681 Err(_) => {
682 drop(child);
683 self.current()
684 }
685 }
686 }
687
688 pub fn set_persona(&self, persona: Option<bool>) {
690 *sync::lock(&self.0.persona) = persona;
691 }
692
693 pub fn set_model(&self, id: &str) {
695 *sync::lock(&self.0.model) = id.to_string();
696 }
697
698 pub fn warnings(&self) -> Vec<String> {
700 sync::lock(&self.0.current).2.clone()
701 }
702}
703
704pub struct Runtime {
706 pub config: Arc<Config>,
707 pub services: Services,
708 pub diagnostics: Vec<String>,
710 pub system: SystemPrompt,
712 pub sessions_dir: PathBuf,
714 pub mode: String,
716 pub hindsight: Option<hindsight::Endpoint>,
719 pub memorantado: Option<memorantado::Endpoint>,
722 pub memory_notice: Option<String>,
725 pub memory_explicit: bool,
729}
730
731pub struct MemoryChoice {
733 pub backend: memorantado::Backend,
734 pub hindsight: Option<hindsight::Endpoint>,
737 pub explicit: bool,
739}
740
741pub fn memory_backend(
745 var: &dyn Fn(&str) -> Option<String>,
746 manager: &config::settings::SettingsManager,
747 agent_dir: &Path,
748) -> MemoryChoice {
749 let (url, source) = hindsight::resolve::resolve_in(var, manager);
750 let hindsight_set = source != hindsight::resolve::Source::Default;
751 let hindsight = hindsight::Endpoint::at(url, var);
752 let resolved = match (&hindsight, hindsight_set) {
753 (None, _) => memorantado::Hindsight::Off,
754 (Some(_), false) => memorantado::Hindsight::Default,
755 (Some(_), true) => memorantado::Hindsight::Set,
756 };
757 let backend = memorantado::Endpoint::choose(var, manager, agent_dir, resolved, &memorantado::resolve::probe);
758 let explicit = hindsight_set
759 || (backend.memorantado().is_some()
760 && (memorantado::resolve::choice(manager) == Some(memorantado::Choice::Memorantado)
761 || var(memorantado::resolve::MEMORANTADO_URL_ENV).is_some()
762 || memorantado::resolve::setting_url(manager).is_some()));
763 MemoryChoice {
764 backend,
765 hindsight,
766 explicit,
767 }
768}
769
770impl Runtime {
771 pub fn new(env: &Env, mode: &str, sessions_dir: PathBuf, services: Services) -> Result<Runtime, String> {
774 let mut services = services;
775 let config = Config::from_env(env);
776 let trusted = config.project_trusted;
777 let manager = config::settings::SettingsManager::create(&env.cwd, &config.agent_dir, trusted);
778 let mut resolved = crate::session_resources::resolve(
780 env,
781 config::settings::SettingsManager::create(&env.cwd, &config.agent_dir, trusted),
782 services.session.append_system_prompt.as_deref(),
783 );
784 resolved
786 .skill_paths
787 .extend(services.session.skill_paths.iter().cloned());
788 services.resources = Arc::new(resolved);
789 let mut diagnostics: Vec<String> = Vec::new();
790 let MemoryChoice {
791 backend,
792 hindsight,
793 explicit: memory_explicit,
794 } = memory_backend(&*env.var, &manager, &config.agent_dir);
795 let memory_notice = match &backend {
797 memorantado::Backend::Memorantado { endpoint, up: false } => Some(endpoint.paused_notice()),
798 _ => None,
799 };
800 diagnostics.extend(memory_notice.clone());
801 let hindsight = hindsight.filter(|_| backend == memorantado::Backend::Hindsight);
802 let memorantado = backend.memorantado().cloned();
803 let runtime_guide = resources::runtime_guide::RuntimeGuideOptions {
805 memory_topics: memorantado.is_some() && services.memory.is_none(),
807 ..mcp_runtime_guide(&config)
808 };
809 let system = match &services.session.system_prompt {
810 Some(text) => SystemPrompt::fixed(text.clone()),
812 None => SystemPrompt::rendered(
813 env,
814 &config,
815 trusted,
816 runtime_guide,
817 &services.session,
818 services.resources.clone(),
819 )?,
820 };
821 diagnostics.extend(system.warnings());
822 if let Err(error) =
823 crate::core::turn_guard::parse_checks(config.settings.as_object().unwrap_or(&Default::default()))
824 {
825 diagnostics.push(format!("{error}; no turn-end check runs"));
826 }
827 Ok(Runtime {
828 config: Arc::new(config),
829 services,
830 diagnostics,
831 system,
832 sessions_dir,
833 mode: mode.to_string(),
834 hindsight,
835 memorantado,
836 memory_notice,
837 memory_explicit,
838 })
839 }
840
841 pub fn stack(&self, model: &Model, api_key: Option<String>, session_id: &str) -> Result<Arc<Stack>, String> {
844 self.stack_with(model, api_key, session_id, None)
845 }
846
847 pub fn stack_with(
850 &self,
851 model: &Model,
852 api_key: Option<String>,
853 session_id: &str,
854 subagents: Option<SubagentHost>,
855 ) -> Result<Arc<Stack>, String> {
856 let mut subagents = subagents;
857 let memory_children = subagents
860 .is_none()
861 .then(|| Arc::new(crate::memory_children::MemoryChildren::default()));
862 let config = self.config.clone();
863 let values = self.services.values.clone();
866 let jobs: Arc<dyn kernel::store::DocumentStore> = match &values {
867 Some(values) => Arc::new(crate::session_modules::ModuleJobJournal(
868 crate::session_modules::ModuleDocument(values.document(session_journal::ns::MODULE, "jobs")),
869 )),
870 None => Arc::new(kernel::store::FileDocumentStore::new(
871 config.session_dir("rlm-modules", session_id).join("jobs.json"),
872 )),
873 };
874 let stats = Arc::new(SessionStats::system("lane"));
876 let memory_records = Arc::new(kernel_apis::memory_records::RecordIndex::for_agent_dir(
879 &config.agent_dir,
880 ));
881 let skill_runs = Arc::new(delegation::skill_runs::RunBook::default());
886 let observers = self
887 .memory_pointers(session_id, &memory_records)
888 .into_iter()
889 .chain(self.situational_skills())
890 .chain(Some(skill_runs.clone() as Arc<dyn host::file_hooks::CellObserver>))
891 .collect();
892 let host = Arc::new(
893 self.host_in(&config.cwd, session_id, "bash", Some(&stats), observers)?
894 .with_journal(jobs)?,
895 );
896 crate::guard_log::loki_setup(&config);
897 let modules = |name: &str| -> Arc<dyn kernel::store::DocumentStore> {
899 match &values {
900 Some(values) => Arc::new(crate::session_modules::ModuleDocument(
901 values.document(session_journal::ns::MODULE, name),
902 )),
903 None => Arc::new(FileDocumentStore::new(
904 config
905 .session_dir("rlm-modules", session_id)
906 .join(format!("{name}.json")),
907 )),
908 }
909 };
910 let (cwd, home) = (config.cwd.display().to_string(), config.home.display().to_string());
913 let backend = match (&self.services.memory, &self.memorantado, &self.hindsight) {
914 (Some(backend), _, _) => Some(backend.clone()),
915 (None, Some(endpoint), _) => Some(Arc::new(
916 endpoint
917 .session_backend(session_id, &cwd, &home)
918 .map_err(|e| e.to_string())?,
919 ) as Arc<dyn MemoryBackend>),
920 (None, None, Some(endpoint)) => Some(Arc::new(
921 endpoint
922 .session_backend(session_id, &cwd, &home)
923 .map_err(|e| e.to_string())?,
924 ) as Arc<dyn MemoryBackend>),
925 (None, None, None) => None,
926 };
927 let memory = match &backend {
928 Some(backend) => Some(
929 MemoryService::new(
930 match &values {
931 Some(values) => Arc::new(crate::session_modules::ModuleDocument(values.document(
932 session_journal::ns::MEMORY_ROOT.namespace,
933 session_journal::ns::MEMORY_ROOT.key,
934 ))),
935 None => modules("memory"),
936 },
937 backend.clone(),
938 kernel_apis::worker_gate(),
939 )
940 .map_err(|e| format!("memory: {e:?}"))?
941 .with_records(memory_records.clone())
942 .with_root(config.cwd.clone()),
943 ),
944 None => None,
945 };
946 let skills_config = config.clone();
947 let no_skills = self.services.session.no_skills;
948 let extra = self.services.resources.clone();
949 let loader: kernel_apis::SkillLoader = Arc::new(move || Ok(skill_sources(&skills_config, &extra, no_skills)));
950 let code = CodeSkills::new(CodeSkillsOptions::for_session(
952 &config.skills_dir,
953 &config.runtime_path,
954 config.child_env(),
955 ));
956 let clock = kernel_apis::util::system_clock();
957 let local = Arc::new(services::LocalServices::worker(match &values {
961 Some(values) => Box::new(values.keyed(session_journal::ns::LOCAL)),
962 None => Box::new(kernel_apis::artifacts::FileKeyedDocuments::new(
963 config.session_dir("rlm-modules", session_id).join("local.json"),
964 )),
965 }));
966 let branch = values.as_ref().map(crate::session_modules::main_branch);
969 let refinements = services::instruction_hook(local.clone(), branch.clone());
970 let ledger = crate::auto_refine::Ledger {
971 services: local.clone(),
972 branch: branch.clone(),
973 };
974 let gateway = config.mcp_gateway();
977 let notices = gateway.start(self.services.mcp_notices.clone());
978 if let Some(notify) = &self.services.mcp_notices {
979 for (message, level) in notices {
980 notify(&message, &level);
981 }
982 }
983 if let Some(on_mcp) = &self.services.on_mcp {
984 on_mcp(&gateway);
985 }
986 let images = Arc::new(crate::view_images::ViewImages::new(
987 model.clone(),
988 config.cwd.clone(),
989 config.agent_dir.clone(),
990 config.project_trusted,
991 ));
992 let keys = crate::request_keys::Keys::new(
995 api_key.map(|key| (model.provider.clone(), key)),
996 config.provider_token.clone(),
997 );
998 let root_model = Arc::new(Mutex::new(model.clone()));
999 self.system.set_model(&model.id);
1000 let judge_ledger: Arc<std::sync::OnceLock<UsageLedger>> = Arc::default();
1002 let judge_rows: Arc<std::sync::OnceLock<services::SessionValues>> = Arc::default();
1004 if let Some(values) = &values {
1005 let _ = judge_rows.set(values.clone());
1006 }
1007 let judge = Arc::new(crate::session_judge::module(
1008 &config,
1009 root_model.clone(),
1010 keys.clone(),
1011 judge_rows.clone(),
1012 judge_ledger.clone(),
1013 ));
1014 let skills = Arc::new(SkillsModule::new(modules("skills"), loader, clock.clone(), Some(code)));
1015 let skill_checks: Arc<std::sync::OnceLock<delegation::spawn_contract::CheckRunner>> = Arc::default();
1017 let procedures = Arc::new(delegation::skill_runs::SkillRuns {
1020 book: skill_runs,
1021 skills: skills.clone(),
1022 judge: judge.clone(),
1023 cwd: config.cwd.clone(),
1024 env: config.child_env(),
1025 runner: skill_checks.clone(),
1026 proposals: config::paths::AgentPaths::new(&config.agent_dir).skill_proposals(),
1027 session_id: Some(session_id.to_string()),
1028 now: clock.clone(),
1029 });
1030 let _ = procedures.interrupt_orphans();
1031 let apis = ApiModules {
1032 local: Arc::new(services::LocalServicesModule {
1033 services: local,
1034 branch,
1035 }),
1036 gates: Arc::new(services::ReleaseGateModule::new(modules("release-gates"), None)),
1037 procedures,
1038 skills,
1039 memory: Arc::new(MemoryApi {
1040 service: memory.map(|memory| memory.with_judge(crate::session_judge::decider(judge.clone()))),
1041 }),
1042 hints: Arc::new(HintsModule::new(modules("hints"))),
1043 images: Arc::new(crate::session_images::module(&config, session_id)),
1044 diagram: Arc::new(crate::session_diagrams::module(&config, session_id, images.clone())),
1045 knowledge: Arc::new(crate::session_knowledge::module(
1046 &config,
1047 self.memorantado.clone(),
1048 judge.clone(),
1049 )),
1050 judge,
1051 schedule: Arc::new(
1052 kernel_apis::prompt_schedule::ScheduleModule::new(
1053 match &values {
1054 Some(values) => Box::new(values.keyed(kernel_apis::prompt_schedule::NAMESPACE)),
1055 None => Box::new(kernel_apis::artifacts::MemoryKeyedDocuments::default()),
1056 },
1057 session_id,
1058 kernel_apis::prompt_schedule::clock_for(&*config.var),
1059 self.services.on_schedules.clone(),
1060 )
1061 .with_github(kernel_apis::prompt_schedule::GitHub::for_env(&*config.var)),
1062 ),
1063 tools: Arc::new(kernel_apis::ToolCalls::new(
1065 gateway.tool().is_some().then(|| {
1066 Arc::new(crate::session_tools::SessionTools { mcp: gateway.clone() })
1067 as Arc<dyn kernel_apis::ToolRunner>
1068 }),
1069 modules("tool-calls"),
1070 )),
1071 instances: Arc::new(kernel_apis::InstancesModule::new(modules("instances"), None)),
1074 progress: Arc::new(kernel_apis::ProgressModule::new(modules("progress"))),
1075 family: Arc::new(kernel_apis::FamilyModule::new(
1076 modules("family"),
1077 kernel_apis::FamilyOptions::default(),
1078 )),
1079 sessions: Arc::new(session_search(&config, &self.sessions_dir, session_id)),
1080 };
1081 let var = |key: &str| (config.var)(key);
1083 let context_reminders = Arc::new(crate::context_reminder::ContextReminders::new(
1084 crate::context_reminder::ReminderSettings::from_vars(
1085 var(CONTEXT_REMINDER_ENV).as_deref(),
1086 var(CONTEXT_REMINDER_TOKENS_ENV).as_deref(),
1087 ),
1088 ));
1089 let cell_hints = Arc::new(kernel_apis::cell_hints::CellHints::new(
1090 apis.hints.clone(),
1091 kernel_apis::cell_hints::CellHintsOptions {
1092 enabled: config.hints,
1093 guards: config.turn_guards,
1094 max_per_tag: kernel_apis::cell_hints::hint_max_per_tag(
1095 var(kernel_apis::cell_hints::HINTS_MAX_ENV).as_deref(),
1096 ),
1097 async_events: config.async_events,
1098 read_handle_bytes: kernel_apis::cell_hints::read_handle_bytes(var(READ_HANDLE_BYTES_ENV).as_deref()),
1099 ..Default::default()
1100 },
1101 ));
1102 let session_id = session_id.to_string();
1103 let system = self.system.clone();
1104 let sessions_dir = self.sessions_dir.clone();
1105 let services = self.services.clone();
1106 let model = model.clone();
1107 let frame_stream = crate::request_keys::lane_stream(config.settings_reader(), keys.clone());
1110 let child = ChildDefaults {
1111 model: model.clone(),
1112 keys: keys.clone(),
1113 };
1114 let mut built: Result<(), String> = Ok(());
1115 let (schedules_store, goal_store) = (modules("schedules"), modules("goal"));
1116 let wake = crate::wake::RootWake::default();
1117 let hints_wake = wake.clone();
1118 cell_hints.set_lane_woken(Arc::new(move |lane: &str| hints_wake.woken(lane)));
1119 let memory_children_on = memory_children.is_some();
1120 let agent_lanes = self.services.agent_lanes.clone();
1121 let stack = Arc::new_cyclic(|me: &Weak<Stack>| {
1122 let runner_stack = me.clone();
1123 let (store, inner, agent_stores) = match subagents.take() {
1124 Some(host) => (Some(host.store), (host.runner)(me.clone()), host.agents),
1125 None => {
1126 let factory: delegation::child::ChildAgentFactory =
1127 Arc::new(move |run: &ChildRun| child_agent(&runner_stack, &child, run));
1128 let runner = match &memory_children {
1129 Some(children) => children.runner(factory),
1130 None => agent_runner(factory),
1131 };
1132 let agent_stores = agent_lanes.as_ref().map(|lanes| {
1134 let lanes = lanes.clone();
1135 AgentStores {
1136 tasks: Box::new(MemoryDocumentStore::default()),
1137 definitions: Box::new(MemoryDocumentStore::default()),
1138 usage: Box::new(MemoryDocumentStore::default()),
1139 lanes: Box::new(move |stack| lanes(stack)),
1140 frames: Arc::new(inference::MemoryTraceStore::default()),
1141 }
1142 });
1143 (None, runner, agent_stores)
1144 }
1145 };
1146 let goal_slot: Arc<std::sync::OnceLock<Arc<GoalDriver>>> = Arc::new(std::sync::OnceLock::new());
1147 let frames = inference::FrameSession::new(match &agent_stores {
1150 Some(stores) => stores.frames.clone(),
1151 None => Arc::new(inference::MemoryTraceStore::default()),
1152 });
1153 let (agents, usage, task_ends) = typed_agents(
1154 &config,
1155 agent_stores,
1156 me.clone(),
1157 root_model.clone(),
1158 keys.clone(),
1159 goal_slot.clone(),
1160 Some(refinements),
1161 crate::frames::frame_runner(frames.clone()),
1162 &stats,
1163 );
1164 let _ = judge_ledger.set(usage.clone());
1165 frames.set_tasks(Arc::new(crate::frames::StackFrameTasks {
1166 agents: agents.clone(),
1167 usage: usage.clone(),
1168 stack: me.clone(),
1169 }));
1170 let schedules = Arc::new(SchedulesModule::new(schedules_store, None));
1172 let goal = Arc::new(GoalDriver::new(
1173 goal_store,
1174 Arc::new(TaskGoalJobs::new(Arc::new(agents.clone()), host.clone())),
1175 kernel_apis::util::system_clock(),
1176 ));
1177 let _ = goal_slot.set(goal.clone());
1178 let closer = me.clone();
1179 let runner: ChildRunner = Arc::new(move |run: ChildRun| {
1182 let lane = run.lane_name.clone();
1183 let result = inner(run);
1184 if let Some(stack) = closer.upgrade() {
1185 stack.cell_hints.run_ended(&lane);
1186 let _ = stack.lanes.close_lane(&lane);
1187 stack.retire_workspace(&lane);
1188 }
1189 result
1190 });
1191 let mut options = DelegationOptions::new(runner);
1192 if let Some(store) = store {
1193 options.store = store;
1194 }
1195 let lane_wake = wake.clone();
1199 options.collect = Arc::new(move |lane: &str| lane_wake.collect_mode(lane));
1200 options.spawn_depth = config.spawn_depth;
1201 options.session_id = session_id.clone();
1202 options.workspace = Some(config.cwd.clone());
1203 options.now = Box::new(|| jsrt::date::now_ms() as f64);
1204 options.keep_worktrees = delegation::worktrees::keep_worktrees(
1206 (config.var)(delegation::worktrees::KEEP_WORKTREES_ENV).as_deref(),
1207 );
1208 let settings = config.settings_reader();
1211 options.worktree_setup = Arc::new(move || {
1212 let configured = config::keys::worktree_settings(settings().settings());
1213 Some(delegation::worktrees::WorktreeSetup {
1214 link: configured.link,
1215 copy: configured.copy,
1216 command: configured.command,
1217 timeout_ms: None,
1218 })
1219 });
1220 let merges = stats.clone();
1222 options.on_merge = Some(Arc::new(move |task: &str, status: &str| merges.merge(task, status)));
1223 options.model_check = Some(config.model_check());
1225 options.check_runner = crate::contract_checks::check_runner(me.clone());
1227 let _ = skill_checks.set(options.check_runner.clone());
1228 options.command_env = config.child_env();
1230 let (delegation, child_ends) = Delegation::new(options);
1231 let settings = config.settings_reader();
1234 delegation.set_child_model(Arc::new(move || {
1235 config::keys::rlm_model_settings(settings().global_settings()).child_model
1236 }));
1237 let pruning = delegation.clone();
1240 std::thread::spawn(move || pruning.prune_orphan_worktrees());
1241 agents.set_worktree_children(delegation.clone());
1243 let factory_stack = me.clone();
1244 let handler_stack = me.clone();
1245 let lanes = Lanes::new(LanesOptions {
1246 create: Arc::new(move |lane| {
1247 let stack = factory_stack.upgrade().ok_or("the session is closed")?;
1248 stack.kernel(lane)
1249 }),
1250 handlers: Arc::new(move |lane, signal, park| match handler_stack.upgrade() {
1251 Some(stack) => stack.cell_handler(lane, Some((signal, park))),
1252 None => Arc::new(Closed) as Arc<dyn HostHandler>,
1253 }),
1254 max_lanes: 64,
1255 cancel_grace: Duration::from_secs(5),
1256 pool: Some(lanes::PoolLimits::new(
1259 max_live_kernels(&config.settings),
1260 Some(lane_snapshot_path(&config, &session_id)),
1261 )),
1262 });
1263 let (lanes, cell_ends) = match lanes {
1264 Ok(pair) => {
1265 let calls = stats.clone();
1268 pair.0
1269 .observe_requests(Arc::new(move |kind: &str| calls.host_call(kind)));
1270 let guards_of = me.clone();
1271 pair.0.extend_pool_stats(Arc::new(move |pool: &mut Value| {
1272 if let Some(stack) = guards_of.upgrade() {
1273 let guards = stack.guard_stats();
1274 if !guards.is_empty() {
1275 pool["guards"] = Value::Array(guards);
1276 }
1277 }
1278 }));
1279 pair
1280 }
1281 Err(error) => {
1282 built = Err(error);
1283 let (lanes, ends) = Lanes::new(LanesOptions {
1284 create: Arc::new(|_| Err("no lanes".into())),
1285 handlers: Arc::new(|_, _, _| Arc::new(Closed) as Arc<dyn HostHandler>),
1286 max_lanes: 1,
1287 cancel_grace: Duration::ZERO,
1288 pool: None,
1289 })
1290 .expect("one lane is a valid limit");
1291 (lanes, ends)
1292 }
1293 };
1294 let events = Arc::new(RuntimeEvents::new(
1295 (task_ends, child_ends, cell_ends),
1296 config.async_events,
1297 session_mask(&config),
1298 ));
1299 Stack {
1300 config,
1301 mcp: gateway,
1302 host,
1303 delegation,
1304 apis,
1305 model,
1306 root_model,
1307 keys,
1308 frame_stream,
1309 session_id,
1310 sessions_dir,
1311 system,
1312 services,
1313 lanes,
1314 workspaces: Mutex::new(HashMap::new()),
1315 kernel_sandbox: std::sync::OnceLock::new(),
1316 events,
1317 cell_progress: Arc::new(Mutex::new(None)),
1318 agents,
1319 usage,
1320 frames,
1321 schedules,
1322 goal,
1323 work: std::sync::OnceLock::new(),
1324 cell_hints,
1325 context: std::sync::OnceLock::new(),
1326 images,
1327 swarm: std::sync::OnceLock::new(),
1328 budget_note: Arc::new(std::sync::OnceLock::new()),
1329 request_layer: Arc::new(std::sync::OnceLock::new()),
1330 context_reminders,
1331 lane_steer: Arc::new(std::sync::OnceLock::new()),
1332 stats,
1333 judge_rows,
1334 retired_guards: Mutex::new(Vec::new()),
1335 wake,
1336 memory_children: memory_children_on,
1337 ledger,
1338 }
1339 });
1340 built?;
1341 if let Some(children) = memory_children {
1342 children.attach_reminders(stack.context_reminders.clone());
1343 let _ = stack
1344 .lane_steer
1345 .set(Arc::new(move |lane: &str, message: &str| children.steer(lane, message)));
1346 }
1347 stack.lanes.open(session_journal::run::MAIN_LANE)?;
1348 let work = DurableWork::open(
1349 DurableWorkOptions::new(Arc::new(stack.agents.clone()))
1350 .schedules(stack.schedules.clone())
1351 .goal(stack.goal.clone())
1352 .jobs(stack.host.clone()),
1353 )?;
1354 let _ = stack.work.set(work);
1355 Ok(stack)
1356 }
1357
1358 fn file_hooks_in(
1361 &self,
1362 cwd: &Path,
1363 stats: Option<&Arc<SessionStats>>,
1364 observers: Vec<Arc<dyn host::file_hooks::CellObserver>>,
1365 ) -> FileHooks {
1366 let record = stats.map(|stats| {
1367 let stats = stats.clone();
1368 Arc::new(move |record: &GuardRecord| {
1369 stats.guard(&GuardOutcome {
1370 guard: &record.guard,
1371 phase: record.phase,
1372 outcome: record.outcome,
1373 ms: record.ms as f64,
1374 });
1375 }) as OnRecord
1376 });
1377 let hooks = self.config.file_hooks_in(cwd, record);
1378 for observer in observers {
1379 hooks.observe(observer);
1380 }
1381 hooks
1382 }
1383
1384 fn situational_skills(&self) -> Option<Arc<dyn host::file_hooks::CellObserver>> {
1387 let config = self.config.clone();
1388 let (extra, no_skills) = (self.services.resources.clone(), self.services.session.no_skills);
1389 let resolve = move |name: &str| {
1390 let cwd = config.cwd.display().to_string();
1391 let agent_dir = config.agent_dir.display().to_string();
1392 let bundled = config.bundled_skills();
1393 let skill = crate::session_resources::skills(&cwd, &agent_dir, &extra, no_skills, bundled.as_deref())
1394 .into_iter()
1395 .find(|skill| skill.name == name)?;
1396 let raw = std::fs::read_to_string(&skill.file_path).ok()?;
1397 Some((skill.file_path, resources::frontmatter::strip(&raw).ok()?))
1398 };
1399 let (cwd, agent_dir, home) = (
1400 self.config.cwd.clone(),
1401 self.config.agent_dir.clone(),
1402 self.config.home.clone(),
1403 );
1404 let session_cwd = cwd.clone();
1405 let engineering = move || {
1406 config::settings::SettingsManager::for_worker(&cwd, &agent_dir, &home)
1407 .settings()
1408 .get("engineering")
1409 == Some(&Value::Bool(true))
1410 };
1411 crate::situational_skills::session_observer(
1412 &self.config.settings,
1413 &session_cwd,
1414 Box::new(resolve),
1415 Box::new(engineering),
1416 )
1417 }
1418
1419 fn memory_pointers(
1422 &self,
1423 session_id: &str,
1424 records: &Arc<kernel_apis::memory_records::RecordIndex>,
1425 ) -> Option<Arc<dyn host::file_hooks::CellObserver>> {
1426 let config = &self.config;
1427 let filters = match &self.services.memory {
1428 Some(backend) => backend.scope_tags(),
1429 None if self.memorantado.is_some() || self.hindsight.is_some() => kernel_apis::memory::worker_scope_tags(
1430 session_id,
1431 &config.cwd.display().to_string(),
1432 &config.home.display().to_string(),
1433 ),
1434 None => return None,
1435 };
1436 kernel_apis::memory_pointers::session_pointers(&config.settings, records.clone(), filters)
1437 }
1438
1439 fn host_in(
1442 &self,
1443 cwd: &Path,
1444 session_id: &str,
1445 spill: &str,
1446 stats: Option<&Arc<SessionStats>>,
1447 observers: Vec<Arc<dyn host::file_hooks::CellObserver>>,
1448 ) -> Result<Host, String> {
1449 let config = &self.config;
1450 let jobs_dir = self.sessions_dir.join("rlm-jobs").join(session_id);
1451 let spill_dir = config.session_dir("rlm-output", session_id).join(spill);
1454 let tmp_dir = config.session_dir("rlm-tmp", session_id);
1455 for dir in [&spill_dir, &tmp_dir] {
1456 std::fs::create_dir_all(dir).map_err(|e| format!("{}: {e}", dir.display()))?;
1457 }
1458 let shell_path = config
1460 .settings
1461 .as_object()
1462 .and_then(|settings| config::keys::shell_path(settings, &config.home));
1463 Ok(Host::new(
1464 cwd,
1465 jobs_dir,
1466 spill_dir,
1467 Box::new(self.file_hooks_in(cwd, stats, observers)),
1468 )
1469 .with_shell_path(shell_path)
1470 .with_base_env(config.child_env())
1472 .recording_cell_writes(!config.ts_guide)
1475 .sandboxed(&config.settings, &config.home, Some(&tmp_dir)))
1476 }
1477}
1478
1479fn skill_sources(
1481 config: &Config,
1482 extra: &crate::session_resources::SessionResources,
1483 no_skills: bool,
1484) -> Vec<SkillSource> {
1485 let cwd = config.cwd.display().to_string();
1486 let agent_dir = config.agent_dir.display().to_string();
1487 let bundled = config.bundled_skills();
1488 crate::session_resources::skills(&cwd, &agent_dir, extra, no_skills, bundled.as_deref())
1489 .into_iter()
1490 .map(|skill| SkillSource {
1491 content: std::fs::read_to_string(&skill.file_path).unwrap_or_default(),
1492 name: skill.name,
1493 description: skill.description,
1494 file_path: skill.file_path,
1495 disable_model_invocation: skill.disable_model_invocation,
1496 })
1497 .collect()
1498}
1499
1500struct ApiModules {
1501 local: Arc<dyn kernel_apis::ApiModule>,
1502 gates: Arc<services::ReleaseGateModule>,
1503 skills: Arc<SkillsModule>,
1504 procedures: Arc<delegation::skill_runs::SkillRuns>,
1505 memory: Arc<MemoryApi>,
1506 hints: Arc<HintsModule>,
1507 images: Arc<kernel_apis::ImagesModule>,
1508 diagram: Arc<diagram::DiagramModule>,
1509 judge: Arc<judge::JudgeModule>,
1510 knowledge: Arc<graph::KnowledgeModule>,
1511 schedule: Arc<kernel_apis::prompt_schedule::ScheduleModule>,
1512 tools: Arc<kernel_apis::ToolCalls>,
1513 instances: Arc<kernel_apis::InstancesModule>,
1514 progress: Arc<kernel_apis::ProgressModule>,
1515 family: Arc<kernel_apis::FamilyModule>,
1516 sessions: Arc<kernel_apis::SessionsModule>,
1517}
1518
1519pub fn store_roots(agent_dir: &Path, home: &Path, session_root: Option<&Path>) -> Vec<PathBuf> {
1524 let paths = config::paths::AgentPaths::new(agent_dir);
1525 let mut projects: Vec<PathBuf> =
1526 std::fs::read_dir(home.join(config::paths::AGENT_CONFIG_DIR_NAME).join("projects"))
1527 .map(|entries| entries.flatten().map(|entry| entry.path().join("sessions")).collect())
1528 .unwrap_or_default();
1529 projects.sort();
1530 let mut roots: Vec<PathBuf> = Vec::new();
1531 for root in session_root
1532 .map(Path::to_path_buf)
1533 .into_iter()
1534 .chain([paths.experimental_sessions_dir(), paths.sessions_dir()])
1535 .chain(projects)
1536 {
1537 if !roots.contains(&root) {
1538 roots.push(root);
1539 }
1540 }
1541 roots
1542}
1543
1544pub(crate) fn session_roots(config: &Config, sessions_dir: &Path) -> Vec<PathBuf> {
1547 store_roots(&config.agent_dir, &config.home, sessions_dir.parent())
1548}
1549
1550fn session_search(config: &Config, sessions_dir: &Path, session_id: &str) -> kernel_apis::SessionsModule {
1553 kernel_apis::SessionsModule::new(
1554 session_roots(config, sessions_dir),
1555 session_id,
1556 &config.cwd.display().to_string(),
1557 kernel_apis::util::system_clock(),
1558 )
1559}
1560
1561struct Workspace {
1563 cwd: PathBuf,
1564 env: Vec<(String, String)>,
1565 host: Arc<Host>,
1566}
1567
1568struct Closed;
1570
1571impl HostHandler for Closed {
1572 fn handle(&self, kind: &str, _: Map<String, Value>, _: &Signal) -> Result<Value, String> {
1573 Err(format!("The session is closed ({kind})"))
1574 }
1575}
1576
1577pub struct Stack {
1579 config: Arc<Config>,
1580 mcp: Arc<mcp::gateway::Gateway>,
1582 pub host: Arc<Host>,
1583 pub delegation: Delegation,
1584 pub lanes: Lanes,
1585 pub events: Arc<RuntimeEvents>,
1586 apis: ApiModules,
1587 model: Model,
1588 root_model: Arc<Mutex<Model>>,
1590 keys: crate::request_keys::Keys,
1592 frame_stream: Arc<dyn StreamFn>,
1594 session_id: String,
1595 sessions_dir: PathBuf,
1596 system: SystemPrompt,
1597 services: Services,
1598 workspaces: Mutex<HashMap<String, Workspace>>,
1599 kernel_sandbox: std::sync::OnceLock<Option<Arc<sandbox::Launcher>>>,
1601 pub cell_progress: Arc<Mutex<Option<harness::chat::CellProgress>>>,
1604 pub agents: Agents,
1606 pub usage: UsageLedger,
1607 pub frames: Arc<inference::FrameSession>,
1609 pub schedules: Arc<SchedulesModule>,
1611 pub goal: Arc<GoalDriver>,
1612 pub work: std::sync::OnceLock<DurableWork>,
1614 cell_hints: Arc<kernel_apis::cell_hints::CellHints>,
1616 pub context: std::sync::OnceLock<Arc<kernel_apis::ContextControl>>,
1618 pub images: Arc<crate::view_images::ViewImages>,
1620 pub swarm: std::sync::OnceLock<Arc<crate::swarm_ops::SwarmOps>>,
1622 pub budget_note: Arc<std::sync::OnceLock<crate::budget_note::BudgetNote>>,
1624 pub request_layer: Arc<std::sync::OnceLock<crate::request_layer::RequestLayer>>,
1627 pub context_reminders: Arc<crate::context_reminder::ContextReminders>,
1630 pub lane_steer: Arc<std::sync::OnceLock<crate::agent_hub::LaneSteer>>,
1632 pub stats: Arc<SessionStats>,
1634 judge_rows: Arc<std::sync::OnceLock<services::SessionValues>>,
1636 retired_guards: Mutex<Vec<Value>>,
1639 pub wake: crate::wake::RootWake,
1642 memory_children: bool,
1644 pub ledger: crate::auto_refine::Ledger,
1646}
1647
1648impl Stack {
1649 pub fn record_judge_rows_in(&self, session: crate::durable_store::SharedSession) {
1652 let _ = self.judge_rows.set(services::SessionValues::shared(session));
1653 }
1654
1655 pub(crate) fn config(&self) -> &Arc<Config> {
1656 &self.config
1657 }
1658
1659 pub fn memory(&self) -> Option<&kernel_apis::MemoryService> {
1661 self.apis.memory.service.as_ref()
1662 }
1663
1664 pub fn prompt_schedules(&self) -> &Arc<kernel_apis::prompt_schedule::ScheduleModule> {
1666 &self.apis.schedule
1667 }
1668
1669 pub fn pool_stats(&self) -> Value {
1672 self.lanes.pool_stats()
1673 }
1674
1675 pub fn guard_stats(&self) -> Vec<Value> {
1678 let mut totals = self.host.guard_stats();
1679 let others: Vec<Vec<Value>> = sync::lock(&self.workspaces)
1680 .values()
1681 .map(|w| w.host.guard_stats())
1682 .chain(std::iter::once(sync::lock(&self.retired_guards).clone()))
1683 .collect();
1684 for stats in others {
1685 add_guard_stats(&mut totals, &stats);
1686 }
1687 totals
1688 }
1689
1690 pub fn begin_turn(&self) {
1692 self.host.begin_turn();
1693 for workspace in sync::lock(&self.workspaces).values() {
1694 workspace.host.begin_turn();
1695 }
1696 for retired in sync::lock(&self.retired_guards).iter_mut() {
1697 retired["turnMs"] = json!(0);
1698 }
1699 }
1700
1701 fn retire_workspace(&self, lane: &str) {
1704 let Some(workspace) = sync::lock(&self.workspaces).remove(lane) else {
1705 return;
1706 };
1707 add_guard_stats(&mut sync::lock(&self.retired_guards), &workspace.host.guard_stats());
1708 }
1709
1710 fn host_of(&self, lane: &str) -> Arc<Host> {
1712 sync::lock(&self.workspaces)
1713 .get(lane)
1714 .map_or_else(|| self.host.clone(), |w| w.host.clone())
1715 }
1716
1717 pub fn cell_handler(&self, lane: &str, cell: Option<(Signal, Park)>) -> Arc<dyn HostHandler> {
1720 let cell_signal = cell.as_ref().map(|(signal, _)| signal.clone());
1721 let delegation: Arc<dyn HostHandler> = match cell {
1722 Some((signal, park)) => Arc::new(self.delegation.cell_handler(lane, signal, park)),
1723 None => Arc::new(self.delegation.lane_handler(lane)),
1724 };
1725 let router = Router {
1726 delegation,
1727 host: self.host_of(lane),
1728 progress: (lane == session_journal::run::MAIN_LANE).then(|| self.cell_progress.clone()),
1729 lane: lane.to_string(),
1730 config: self.config.clone(),
1731 swarm: self.swarm.get().cloned(),
1732 cell: cell_signal.clone().map(CellShell::new),
1733 };
1734 let models = crate::frame_models::resolver(self.config.clone());
1738 let inference = Inference::new(
1739 Arc::new(router),
1740 self.root_model(),
1741 None,
1742 Arc::new(crate::lane::process_transport()),
1743 self.sessions_dir.join("rlm-context").join(&self.session_id),
1744 )
1745 .with_stream(self.frame_stream.clone())
1746 .with_models(Arc::new(move |spec: &str| models(spec).map(|(model, _)| (model, None))))
1749 .with_session(self.frames.clone(), lane);
1750 let settings = self.config.settings_reader();
1753 let models = move || {
1754 let settings = settings();
1755 let rlm = config::keys::rlm_model_settings(settings.global_settings());
1756 inference::ModelSettings {
1757 frame_model: rlm.frame_model,
1758 frame_thinking: rlm.frame_thinking,
1759 child_model: rlm.child_model,
1760 review_model: config::keys::review_model(settings.global_settings()),
1761 map_tokens: rlm.map_tokens,
1762 }
1763 };
1764 let frames = inference::Frames::new(inference, self.config.frame_env.clone(), models);
1765 let frames: Arc<dyn HostHandler> = match cell_signal {
1767 Some(cell) => Arc::new(FramesOfCell {
1768 inner: Arc::new(frames),
1769 cell,
1770 }),
1771 None => Arc::new(frames),
1772 };
1773 let typed: Arc<dyn HostHandler> = Arc::new(self.agents.handler(lane, frames));
1775 let mut apis = KernelApis::new(typed)
1778 .with_host(Arc::new(self.agents.clone()), lane)
1779 .with(self.schedules.clone() as Arc<dyn kernel_apis::ApiModule>)
1780 .with(self.goal.clone() as Arc<dyn kernel_apis::ApiModule>);
1781 if let Some(context) = self.context.get() {
1782 apis = apis.with(context.clone() as Arc<dyn kernel_apis::ApiModule>);
1783 }
1784 let apis = apis
1785 .with(self.apis.skills.clone())
1786 .with(self.apis.procedures.clone())
1787 .with(self.apis.memory.clone())
1788 .with(self.apis.hints.clone())
1789 .with(self.apis.images.clone())
1790 .with(self.apis.diagram.clone())
1791 .with(self.apis.judge.clone())
1792 .with(self.apis.knowledge.clone())
1793 .with(self.apis.schedule.clone())
1794 .with(self.apis.tools.clone())
1795 .with(self.apis.local.clone())
1796 .with(self.apis.gates.clone())
1797 .with(self.apis.instances.clone())
1798 .with(self.apis.progress.clone())
1799 .with(self.apis.sessions.clone())
1800 .with(Arc::new(crate::agent_hub::Family::new(
1801 self.apis.family.clone(),
1802 self.delegation.clone(),
1803 self.lane_steer.clone(),
1804 )) as Arc<dyn kernel_apis::ApiModule>);
1805 let observed: Arc<dyn HostHandler> = if self.cell_hints.active() {
1806 Arc::new(ObservedByHints {
1807 inner: Arc::new(apis),
1808 hints: self.cell_hints.clone(),
1809 lane: lane.to_string(),
1810 })
1811 } else {
1812 Arc::new(apis)
1813 };
1814 let viewed: Arc<dyn HostHandler> = Arc::new(crate::view_images::ViewImageLayer {
1816 inner: observed,
1817 images: self.images.clone(),
1818 lane: lane.to_string(),
1819 });
1820 Arc::new(crate::request_layer::Layered {
1822 inner: viewed,
1823 layer: self.request_layer.clone(),
1824 lane: lane.to_string(),
1825 })
1826 }
1827
1828 fn kernel(&self, lane: &str) -> Result<Kernel, String> {
1831 let config = &self.config;
1832 let (cwd, extra) = match sync::lock(&self.workspaces).get(lane) {
1833 Some(workspace) => (workspace.cwd.clone(), workspace.env.clone()),
1834 None => (config.cwd.clone(), Vec::new()),
1835 };
1836 let spill_dir = config.session_dir("rlm-output", &self.session_id).join(lane_dir(lane));
1837 let mut options = KernelOptions::new(&cwd, &config.runtime_path);
1838 options.base_env = Some(config.child_env());
1840 options.env = vec![
1841 (
1842 kernel::env::CODE_SKILLS_DIR_ENV.into(),
1843 config.skills_dir.display().to_string(),
1844 ),
1845 (kernel::env::RLM_SPILL_DIR_ENV.into(), spill_dir.display().to_string()),
1846 (kernel::env::FILE_HOOKS_ENV.into(), "1".into()),
1848 (kernel::env::RLM_STREAM_OUTPUT_ENV.into(), "1".into()),
1850 ("HOME".into(), config.home.display().to_string()),
1851 ];
1852 options.env.extend(extra);
1853 let snapshots = config.session_dir("rlm-snapshots", &self.session_id);
1854 std::fs::DirBuilder::new()
1855 .recursive(true)
1856 .create(&snapshots)
1857 .map_err(|e| format!("{}: {e}", snapshots.display()))?;
1858 options.snapshot_path = Some(snapshots.join(format!("{}.snapshot", lane_dir(lane))));
1859 options.snapshot_key = config.snapshot_key.clone();
1860 options.continuity = Some(ContinuityPolicy::default());
1861 options.sandbox = self.kernel_launcher(lane)?;
1863 options.tree_memory = Some(kernel::TreeMemoryOptions {
1866 backend: Some(
1867 (config.var)(kernel::env::RLM_TREE_MEMORY_BACKEND_ENV)
1868 .filter(|value| !value.trim().is_empty())
1869 .unwrap_or_else(|| "watchdog".into()),
1870 ),
1871 ..kernel::TreeMemoryOptions::default()
1872 });
1873 Kernel::new(options, self.cell_handler(lane, None)).map_err(|e| e.to_string())
1874 }
1875
1876 fn kernel_launcher(&self, lane: &str) -> Result<Option<Arc<sandbox::Launcher>>, String> {
1880 let host = self.host_of(lane);
1881 let workspace = sync::lock(&self.workspaces).contains_key(lane);
1882 if !workspace && let Some(launcher) = self.kernel_sandbox.get() {
1883 return Ok(launcher.clone());
1884 }
1885 let context = self.sessions_dir.join("rlm-context").join(&self.session_id);
1886 std::fs::create_dir_all(&context).map_err(|e| format!("{}: {e}", context.display()))?;
1887 let launcher = host.launcher().map(|launcher| {
1888 let mut policy = launcher.policy.clone();
1889 policy.writable.push(std::fs::canonicalize(&context).unwrap_or(context));
1890 Arc::new(sandbox::Launcher::new(launcher.mode, policy))
1891 });
1892 if !workspace {
1893 let _ = self.kernel_sandbox.set(launcher.clone());
1894 }
1895 Ok(launcher)
1896 }
1897
1898 pub fn rlm(&self, lane: &str) -> Result<Rlm, String> {
1900 self.lanes.open(lane)?;
1901 let spill_dir = self
1902 .config
1903 .session_dir("rlm-output", &self.session_id)
1904 .join(lane_dir(lane));
1905 let hints = self.cell_hints.active().then(|| self.cell_hints.clone());
1906 Ok(rlm_over(
1907 &self.config,
1908 self.lanes.clone(),
1909 lane,
1910 spill_dir,
1911 hints,
1912 self.images.clone(),
1913 Some(self.host_of(lane)),
1914 )
1915 .with_budget_note(self.budget_note.clone())
1916 .with_stats(self.stats.clone())
1917 .with_context_reminders(self.context_reminders.clone())
1918 .with_wake(self.wake.clone()))
1919 }
1920
1921 pub fn commands(&self) -> Vec<Value> {
1923 let bundled = self.config.bundled_skills();
1924 let commands = crate::commands::list_commands_with(
1925 &self.config.cwd.display().to_string(),
1926 &self.config.agent_dir.display().to_string(),
1927 bundled.as_deref(),
1928 &self.services.resources,
1929 );
1930 crate::session_flags::filter_commands(commands, &self.services.session)
1931 }
1932
1933 pub fn prompt_templates(&self) -> Vec<resources::prompt_templates::PromptTemplate> {
1935 crate::session_resources::prompts(
1936 &self.config.cwd.display().to_string(),
1937 &self.config.agent_dir.display().to_string(),
1938 &self.services.resources,
1939 )
1940 }
1941
1942 pub fn model(&self) -> &Model {
1944 &self.model
1945 }
1946
1947 pub fn root_model(&self) -> Model {
1948 sync::lock(&self.root_model).clone()
1949 }
1950
1951 pub fn set_root_model(&self, model: &Model) {
1954 *sync::lock(&self.root_model) = model.clone();
1955 self.system.set_model(&model.id);
1956 self.images.set_model(None, model.clone());
1957 }
1958
1959 pub fn keys(&self) -> &crate::request_keys::Keys {
1961 &self.keys
1962 }
1963
1964 pub fn system(&self) -> String {
1966 self.system.current()
1967 }
1968
1969 pub fn child_system(&self, model: &Model) -> String {
1972 self.system.child_current(
1973 ChildReader {
1974 in_memory: self.memory_children,
1975 memory: crate::memory_guidance::memory_guidance(model, &self.root_model(), true),
1976 },
1977 &model.id,
1978 )
1979 }
1980
1981 pub fn hand_over(&self) {
1985 if let Some(work) = self.work.get() {
1986 work.hand_over();
1987 }
1988 self.apis.tools.close();
1990 self.mcp.close();
1991 self.lanes.close();
1992 self.stats.close();
1993 }
1994
1995 pub fn close(&self) {
1997 if let Some(work) = self.work.get() {
1998 work.close();
1999 }
2000 self.apis.tools.close();
2001 self.mcp.close();
2002 self.agents.close();
2003 self.delegation.close();
2004 self.lanes.close();
2005 self.stats.close();
2007 }
2008
2009 pub fn cell_hints(&self) -> &Arc<kernel_apis::cell_hints::CellHints> {
2011 &self.cell_hints
2012 }
2013
2014 pub fn mcp(&self) -> &Arc<mcp::gateway::Gateway> {
2016 &self.mcp
2017 }
2018
2019 pub fn tool_calls(&self) -> &Arc<kernel_apis::ToolCalls> {
2021 &self.apis.tools
2022 }
2023
2024 pub fn mask(&self) -> crate::async_events::MaskText {
2026 session_mask(&self.config)
2027 }
2028}
2029
2030pub struct SubagentHost {
2032 pub store: Box<dyn kernel::store::DocumentStore>,
2033 pub runner: Box<dyn FnOnce(Weak<Stack>) -> delegation::host::ChildRunner>,
2035 pub agents: Option<AgentStores>,
2038}
2039
2040pub struct AgentStores {
2042 pub tasks: Box<dyn kernel::store::DocumentStore>,
2043 pub definitions: Box<dyn kernel::store::DocumentStore>,
2044 pub usage: Box<dyn kernel::store::DocumentStore>,
2045 pub lanes: Box<dyn FnOnce(Weak<Stack>) -> delegation::agents::LaneRunner>,
2046 pub frames: Arc<dyn inference::TraceStore>,
2048}
2049
2050#[allow(clippy::too_many_arguments)]
2054fn typed_agents(
2055 config: &Config,
2056 stores: Option<AgentStores>,
2057 stack: Weak<Stack>,
2058 root_model: Arc<Mutex<Model>>,
2059 keys: crate::request_keys::Keys,
2060 goal: Arc<std::sync::OnceLock<Arc<GoalDriver>>>,
2061 refinements: Option<delegation::agents::RefinementsHook>,
2062 frames: delegation::agents::FrameRunner,
2063 stats: &Arc<SessionStats>,
2064) -> (Agents, UsageLedger, Receiver<delegation::agents::TaskEnd>) {
2065 let (tasks, definitions, usage_store, lanes) = match stores {
2066 Some(stores) => (
2067 stores.tasks,
2068 stores.definitions,
2069 stores.usage,
2070 Some(hinted_lanes((stores.lanes)(stack.clone()), {
2071 let stack = stack.clone();
2072 Arc::new(move |lane: &str| {
2073 if let Some(stack) = stack.upgrade() {
2074 stack.cell_hints.run_ended(lane);
2075 }
2076 })
2077 })),
2078 ),
2079 None => (
2080 Box::new(MemoryDocumentStore::default()) as Box<dyn kernel::store::DocumentStore>,
2081 Box::new(MemoryDocumentStore::default()) as Box<dyn kernel::store::DocumentStore>,
2082 Box::new(MemoryDocumentStore::default()) as Box<dyn kernel::store::DocumentStore>,
2083 None,
2084 ),
2085 };
2086 let var = config.var.clone();
2089 let root_budget = delegation::usage::root_budget_settings(&config.settings["rootBudget"]);
2090 let limits = delegation::usage::limits_from_env(&|key| var(key), &root_budget);
2091 let refused = stats.clone();
2093 let usage = UsageLedger::new(
2094 usage_store,
2095 delegation::usage::UsageOptions {
2096 limits,
2097 on_refused: Some(Arc::new(move |_: &str| refused.usage_limit_block())),
2098 keep_turns: true,
2100 ..Default::default()
2101 },
2102 )
2103 .unwrap_or_else(|_| {
2104 UsageLedger::new(Box::new(MemoryDocumentStore::default()), Default::default()).expect("an empty ledger opens")
2105 });
2106 let mut options = AgentsOptions::new(tasks);
2107 options.definition_store = Some(definitions);
2108 options.usage = Some(usage.clone());
2109 options.lanes = lanes;
2110 options.frames = Some(frames);
2111 options.root_turns = true;
2112 options.refinements = refinements;
2113 options.pricing = Some(crate::budget_pricing::pricing(config, root_model.clone()));
2114 let context_stack = stack.clone();
2117 options.on_task_end = Some(Arc::new(move |task: &Value, cost, _| {
2118 if let (Some(goal), Some(id)) = (goal.get(), task["id"].as_str()) {
2119 let _ = goal.task_ended(id, task.get("result"));
2120 }
2121 if let Some(stack) = context_stack.upgrade()
2122 && let Some(context) = stack.context.get()
2123 {
2124 context.task_ended(task, cost);
2125 }
2126 }));
2127 let settings = config.settings_reader();
2130 options.predict = Some(predict_adapter(Arc::new(move |call: PredictCall<'_>| {
2131 let model = sync::lock(&root_model).clone();
2132 let idle = ai::http::http_idle_timeout(settings().settings().get("httpIdleTimeoutMs"));
2133 let provider = crate::request_keys::Keyed {
2134 inner: Provider::with_idle_timeout(idle),
2135 keys: keys.clone(),
2136 };
2137 complete_once(&model, &provider, &call)
2138 })));
2139 let (pin_stack, unpin_stack) = (stack.clone(), stack.clone());
2141 options.pin_lane = Some(Arc::new(move |lane: &str, holder: &str| {
2142 pin_stack.upgrade().is_some_and(|stack| stack.lanes.pin(lane, holder))
2143 }));
2144 options.unpin_lane = Some(Arc::new(move |lane: &str, holder: &str| {
2145 if let Some(stack) = unpin_stack.upgrade() {
2146 stack.lanes.unpin(lane, holder);
2147 }
2148 }));
2149 let extras_stack = stack.clone();
2151 options.status_extras = Some(Arc::new(move |lane: &str| match extras_stack.upgrade() {
2152 Some(stack) => crate::agents::status_extras(&stack.host, stack.tool_calls(), lane),
2153 None => Map::new(),
2154 }));
2155 let reuse_stack = stack;
2156 options.before_lane_reuse = Some(Arc::new(move |lane: &str| {
2157 let stack = reuse_stack.upgrade().ok_or("The session is closed")?;
2158 match stack.lanes.kernel(lane) {
2159 Some(kernel) => kernel.reset_scratch().map(drop).map_err(|e| e.to_string()),
2160 None => Ok(()),
2161 }
2162 }));
2163 let (agents, ends) = Agents::new(options);
2164 (agents, usage, ends)
2165}
2166
2167pub fn hinted_lanes(
2171 runner: delegation::agents::LaneRunner,
2172 ended: Arc<dyn Fn(&str) + Send + Sync>,
2173) -> delegation::agents::LaneRunner {
2174 Arc::new(move |run: delegation::agents::LaneRun| {
2175 let lane = run.lane.clone();
2176 let result = runner(run);
2177 ended(&lane);
2178 result
2179 })
2180}
2181
2182fn complete_once(model: &Model, provider: &dyn StreamFn, call: &PredictCall<'_>) -> Result<Completion, String> {
2184 let context = ai::TranscriptContext {
2185 messages: vec![
2186 Message::System(ai::SystemMessage {
2187 content: ai::SystemContent::Text(call.system.to_string()),
2188 sections: None,
2189 tools_added: None,
2190 tools_removed: None,
2191 timestamp: jsrt::date::now_ms(),
2192 }),
2193 Message::User(UserMessage {
2194 content: UserMessageContent::Text(call.user.clone()),
2195 timestamp: jsrt::date::now_ms(),
2196 }),
2197 ],
2198 };
2199 let abort = ai::AbortSignal::new();
2200 let mut options = ai::SimpleStreamOptions::default();
2201 options.base.signal = Some(abort.clone());
2202 let (signal, done) = (call.signal.clone(), Signal::new());
2203 let watched = done.clone();
2204 let watcher = std::thread::spawn(move || {
2205 while !watched.is_aborted() {
2206 if signal.wait(Duration::from_millis(200)) {
2207 abort.abort();
2208 return;
2209 }
2210 }
2211 });
2212 let mut sink = |_: &ai::AssistantMessageEvent| {};
2213 let runtime = tokio::runtime::Builder::new_current_thread()
2214 .enable_all()
2215 .build()
2216 .map_err(|e| e.to_string());
2217 let message = runtime.map(|runtime| runtime.block_on(provider.stream(model, &context, &options, &mut sink)));
2218 done.abort("done");
2219 let _ = watcher.join();
2220 let message = message?;
2221 let text = message
2222 .content
2223 .iter()
2224 .filter_map(|c| match c {
2225 ai::AssistantContent::Text(text) => Some(text.text.as_str()),
2226 _ => None,
2227 })
2228 .collect::<Vec<_>>()
2229 .join("");
2230 let stop = serde_json::to_value(message.stop_reason)
2231 .ok()
2232 .and_then(|v| v.as_str().map(str::to_string))
2233 .unwrap_or_default();
2234 Ok(Completion {
2235 text,
2236 stop_reason: stop,
2237 error_message: message.error_message,
2238 })
2239}
2240
2241pub struct ChildSetup {
2243 pub model: Model,
2244 pub system: String,
2245 pub thinking: String,
2246}
2247
2248pub fn child_setup(stack: &Stack, run: &ChildRun) -> ChildSetup {
2251 let model = run
2252 .model
2253 .as_deref()
2254 .and_then(|spec| spec.split_once('/'))
2255 .and_then(|(provider, id)| stack.config.model(provider, id))
2256 .unwrap_or_else(|| stack.model.clone());
2257 let root_cwd = stack.config.cwd.display().to_string();
2258 let mut system = stack.child_system(&model);
2260 if let Some(cwd) = run.cwd.as_deref() {
2261 let host = Runtime {
2263 config: stack.config.clone(),
2264 services: stack.services.clone(),
2265 diagnostics: Vec::new(),
2266 system: SystemPrompt::fixed(""),
2267 sessions_dir: stack.sessions_dir.clone(),
2268 mode: String::new(),
2269 hindsight: None,
2270 memorantado: None,
2271 memory_notice: None,
2272 memory_explicit: false,
2273 }
2274 .host_in(
2275 Path::new(cwd),
2276 &stack.session_id,
2277 &lane_dir(&run.lane_name),
2278 Some(&stack.stats),
2279 Vec::new(),
2280 );
2281 match host {
2282 Ok(host) => {
2283 let host = host.with_env(run.env.clone()).sharing_jobs(&stack.host);
2286 sync::lock(&stack.workspaces).insert(
2287 run.lane_name.clone(),
2288 Workspace {
2289 cwd: PathBuf::from(cwd),
2290 env: run.env.clone(),
2291 host: Arc::new(host),
2292 },
2293 );
2294 system = system.split(&root_cwd).collect::<Vec<_>>().join(cwd);
2295 }
2296 Err(error) => system.push_str(&format!("\n\nThe worktree could not be prepared: {error}")),
2297 }
2298 }
2299 stack.images.set_model(Some(&run.lane_name), model.clone());
2301 stack.context_reminders.set_limit(
2303 &run.lane_name,
2304 crate::context_reminder::lane_limit(&model, true, harness::chat::CompactionPolicy::default().reserve_tokens),
2305 );
2306 stack
2309 .stats
2310 .child_model(&run.task_id, &format!("{}/{}", model.provider, model.id));
2311 ChildSetup {
2312 model,
2313 system,
2314 thinking: run.thinking.clone().unwrap_or_else(|| "off".into()),
2315 }
2316}
2317
2318fn child_agent(stack: &Weak<Stack>, defaults: &ChildDefaults, run: &ChildRun) -> (AgentOptions, Box<dyn StreamFn>) {
2320 let Some(stack) = stack.upgrade() else {
2321 let mut options = AgentOptions::new(defaults.model.clone());
2323 options.system_prompt = Some("The session ended before this subagent started.".into());
2324 let provider = crate::request_keys::Keyed {
2325 inner: Provider::new(),
2326 keys: defaults.keys.clone(),
2327 };
2328 return (options, Box::new(provider));
2329 };
2330 let setup = child_setup(&stack, run);
2331 let system = setup.system;
2332 let mut options = AgentOptions::new(setup.model);
2333 options.system_prompt = Some(system.clone());
2334 options.thinking_level = setup.thinking;
2335 match stack.rlm(&run.lane_name) {
2336 Ok(tool) => options.tools = vec![Arc::new(tool) as Arc<dyn AgentTool>],
2337 Err(error) => options.system_prompt = Some(format!("{system}\n\nThe REPL failed to start: {error}")),
2339 }
2340 let provider = crate::request_keys::Keyed {
2343 inner: Provider::new().with_live_settings(stack.config.settings_reader()),
2344 keys: stack.keys.clone(),
2345 };
2346 (options, Box::new(provider))
2347}
2348
2349struct ChildDefaults {
2351 model: Model,
2352 keys: crate::request_keys::Keys,
2353}
2354
2355struct Router {
2359 delegation: Arc<dyn HostHandler>,
2360 host: Arc<Host>,
2361 progress: Option<Arc<Mutex<Option<harness::chat::CellProgress>>>>,
2363 lane: String,
2365 config: Arc<Config>,
2366 swarm: Option<Arc<crate::swarm_ops::SwarmOps>>,
2368 cell: Option<CellShell>,
2370}
2371
2372pub const CELL_CANCELLED_SHELL: &str = "the cell was cancelled; its shell command was stopped";
2375
2376#[derive(Clone)]
2381struct CellShell {
2382 waits: Signal,
2383 jobs: Arc<Mutex<(Vec<String>, bool)>>,
2385}
2386
2387impl CellShell {
2388 fn new(waits: Signal) -> CellShell {
2389 CellShell {
2390 waits,
2391 jobs: Arc::default(),
2392 }
2393 }
2394
2395 fn cancelled(&self, request: &Signal) -> bool {
2397 self.waits.is_aborted() && !request.is_aborted()
2398 }
2399
2400 fn run(
2403 &self,
2404 host: &Arc<Host>,
2405 lane: &str,
2406 kind: &str,
2407 payload: Map<String, Value>,
2408 request: &Signal,
2409 ) -> Result<Value, String> {
2410 if self.cancelled(request) {
2411 return Err(CELL_CANCELLED_SHELL.into());
2412 }
2413 let combined = Signal::new();
2414 let follow = |source: &Signal| {
2415 let target = combined.clone();
2416 source.link(move |reason| target.abort(reason))
2417 };
2418 let links = (follow(request), follow(&self.waits));
2419 let result = host.handle_lane(lane, kind, payload, &combined);
2420 drop(links);
2421 if let Ok(value) = &result {
2422 let job = match kind {
2423 "shell.run" => value["id"].as_str(),
2424 _ => value["job"]["id"].as_str(),
2425 };
2426 if let Some(job) = job.filter(|_| kind != "shell.cancel") {
2427 self.track(host, lane, job, request);
2428 }
2429 }
2430 if self.cancelled(request) {
2431 return Err(CELL_CANCELLED_SHELL.into());
2432 }
2433 result
2434 }
2435
2436 fn track(&self, host: &Arc<Host>, lane: &str, job: &str, request: &Signal) {
2438 let mut jobs = sync::lock(&self.jobs);
2439 if !jobs.0.iter().any(|known| known == job) {
2440 jobs.0.push(job.to_string());
2441 }
2442 if jobs.1 {
2443 return;
2444 }
2445 jobs.1 = true;
2446 drop(jobs);
2447 let stop = {
2448 let (host, lane, request, list) = (host.clone(), lane.to_string(), request.clone(), self.jobs.clone());
2449 move |_reason: String| stop_cell_jobs(&host, &lane, &request, &list)
2450 };
2451 if self.waits.on_abort(stop).is_none() {
2452 stop_cell_jobs(host, lane, request, &self.jobs);
2454 }
2455 }
2456}
2457
2458impl HostHandler for Router {
2459 fn handle(&self, kind: &str, payload: Map<String, Value>, signal: &Signal) -> Result<Value, String> {
2460 if kind == "rlm.find_models" {
2462 return Ok(self.config.find_models());
2463 }
2464 if kind == "rlm.race_models" {
2465 return Ok(self.config.race_models());
2466 }
2467 if let Some(swarm) = &self.swarm
2468 && let Some(result) = swarm.handle(&self.lane, kind, &payload, signal)
2469 {
2470 return result;
2471 }
2472 if kind == crate::turns::CELL_OUTPUT {
2473 if let Some(progress) = &self.progress {
2474 crate::turns::publish_output(
2475 progress,
2476 payload.get("text").and_then(Value::as_str).unwrap_or(""),
2477 &session_mask(&self.config),
2478 );
2479 }
2480 return Ok(Value::Null);
2481 }
2482 if Delegation::serves(kind) {
2483 return self.delegation.handle(kind, payload, signal);
2484 }
2485 if kind == "ext.call" || kind == "ext.list" {
2487 return Err(EXTENSIONS_UNSUPPORTED.into());
2488 }
2489 let shell = matches!(kind, "bash" | "shell.run" | "shell.result");
2490 let result = match &self.cell {
2491 Some(cell) if shell => cell.run(&self.host, &self.lane, kind, payload, signal),
2492 _ => self.host.handle_lane(&self.lane, kind, payload, signal),
2493 };
2494 if shell
2495 && let (Some(progress), Ok(value)) = (&self.progress, &result)
2496 && let Some(output) = value["output"].as_str().filter(|o| !o.is_empty())
2497 && let Some(progress) = sync::lock(progress).as_ref()
2498 {
2499 let output = session_mask(&self.config)(output);
2501 progress.output(
2502 if output.ends_with('\n') {
2503 output
2504 } else {
2505 format!("{output}\n")
2506 }
2507 .as_str(),
2508 );
2509 }
2510 result
2511 }
2512}
2513
2514fn stop_cell_jobs(host: &Arc<Host>, lane: &str, request: &Signal, jobs: &Mutex<(Vec<String>, bool)>) {
2517 if request.is_aborted() {
2518 return;
2519 }
2520 let ids = std::mem::take(&mut sync::lock(jobs).0);
2521 for id in ids {
2522 let mut payload = Map::new();
2523 payload.insert("id".into(), Value::String(id));
2524 let _ = host.handle_lane(lane, "shell.cancel", payload, &Signal::new());
2525 }
2526}
2527
2528struct ObservedByHints {
2531 inner: Arc<dyn HostHandler>,
2532 hints: Arc<kernel_apis::cell_hints::CellHints>,
2533 lane: String,
2534}
2535
2536impl HostHandler for ObservedByHints {
2537 fn handle(&self, kind: &str, payload: Map<String, Value>, signal: &Signal) -> Result<Value, String> {
2538 let started = jsrt::date::now_ms();
2539 let observed = payload.clone();
2540 let result = self.inner.handle(kind, payload, signal);
2541 self.hints
2542 .observe(&self.lane, kind, &observed, result.as_ref().ok(), started);
2543 result
2544 }
2545}
2546
2547struct FramesOfCell {
2550 inner: Arc<dyn HostHandler>,
2551 cell: Signal,
2552}
2553
2554impl HostHandler for FramesOfCell {
2555 fn handle(&self, kind: &str, payload: Map<String, Value>, signal: &Signal) -> Result<Value, String> {
2556 if kind != "rlm.infer" && kind != "rlm.map" {
2557 return self.inner.handle(kind, payload, signal);
2558 }
2559 let combined = Signal::new();
2560 let done = Signal::new();
2561 let (cell, request, abort, finished) = (self.cell.clone(), signal.clone(), combined.clone(), done.clone());
2562 let watcher = std::thread::spawn(move || {
2563 while !finished.wait(Duration::from_millis(20)) {
2564 for source in [&cell, &request] {
2565 if source.is_aborted() {
2566 abort.abort(source.reason().unwrap_or_else(|| "the cell was cancelled".into()));
2567 return;
2568 }
2569 }
2570 }
2571 });
2572 let result = self.inner.handle(kind, payload, &combined);
2573 done.abort("finished");
2574 let _ = watcher.join();
2575 result
2576 }
2577}
2578
2579pub fn task_event_summary(task: &Value) -> String {
2583 let result = &task["result"];
2584 let detail = if result["status"] == "succeeded" {
2585 match &result["value"] {
2586 Value::String(text) => text.clone(),
2587 other => other.to_string(),
2588 }
2589 } else {
2590 result["error"].as_str().unwrap_or_default().to_string()
2591 };
2592 let tag = if result.is_object() {
2593 delegation::verdict::verdict_tag(result)
2594 } else {
2595 None
2596 };
2597 let definition = task["definition"].as_str().unwrap_or("undefined");
2598 match tag {
2599 Some(tag) => format!("{definition}: {tag} {detail}"),
2600 None => format!("{definition}: {detail}"),
2601 }
2602}
2603
2604pub fn session_mask(config: &Config) -> crate::async_events::MaskText {
2607 let (masker, flag) = (config.masker.clone(), config.mask_secrets.clone());
2608 Arc::new(move |text: &str| masker.mask_counted(text, flag.as_deref()).0)
2609}
2610
2611pub struct RuntimeEvents {
2615 ends: Arc<(Mutex<Vec<RuntimeEvent>>, Condvar)>,
2616 enabled: bool,
2617 mask: crate::async_events::MaskText,
2618}
2619
2620impl RuntimeEvents {
2621 fn new(
2623 (task_ends, child_ends, cell_ends): (
2624 Receiver<delegation::agents::TaskEnd>,
2625 Receiver<DetachedEnd>,
2626 Receiver<CellEnd>,
2627 ),
2628 enabled: bool,
2629 mask: crate::async_events::MaskText,
2630 ) -> Self {
2631 let ends: Arc<(Mutex<Vec<RuntimeEvent>>, Condvar)> = Arc::default();
2632 fn read<T: Send + 'static>(
2633 from: Receiver<T>,
2634 into: &Arc<(Mutex<Vec<RuntimeEvent>>, Condvar)>,
2635 event: fn(T) -> Option<RuntimeEvent>,
2636 ) {
2637 let into = into.clone();
2638 std::thread::spawn(move || {
2639 for end in from {
2640 if let Some(event) = event(end) {
2641 sync::lock(&into.0).push(event);
2642 into.1.notify_all();
2643 }
2644 }
2645 });
2646 }
2647 read(task_ends, &ends, task_end_event);
2648 read(child_ends, &ends, |end| Some(child_end_event(end)));
2649 read(cell_ends, &ends, |end| Some(cell_end_event(end)));
2650 RuntimeEvents { ends, enabled, mask }
2651 }
2652
2653 pub fn recv(&self, wait: Duration) -> Vec<RuntimeEvent> {
2656 let ends = sync::lock(&self.ends.0);
2657 let (mut ends, _) = self
2658 .ends
2659 .1
2660 .wait_timeout_while(ends, wait, |ends| ends.is_empty())
2661 .unwrap_or_else(std::sync::PoisonError::into_inner);
2662 std::mem::take(&mut *ends)
2663 }
2664
2665 pub fn root_lines(&self, events: &[RuntimeEvent]) -> Vec<String> {
2667 events
2668 .iter()
2669 .filter(|e| e.lane == session_journal::run::MAIN_LANE)
2670 .map(|e| {
2671 crate::async_events::runtime_event_line(&e.kind, &e.id, &e.status, &e.summary, &e.fetch, &self.mask)
2672 })
2673 .collect()
2674 }
2675
2676 pub fn enabled(&self) -> bool {
2678 self.enabled
2679 }
2680}
2681
2682fn task_end_event(end: delegation::agents::TaskEnd) -> Option<RuntimeEvent> {
2684 if end.awaited {
2685 return None;
2686 }
2687 let task = &end.task;
2688 Some(RuntimeEvent {
2689 kind: end.kind.into(),
2690 id: task["id"].as_str().unwrap_or_default().into(),
2691 status: task_status(task).into(),
2692 summary: task_event_summary(task),
2693 fetch: end.fetch.clone(),
2694 lane: end.owner_lane.clone(),
2695 root_id: end.root_id.clone(),
2696 })
2697}
2698
2699fn child_end_event(end: DetachedEnd) -> RuntimeEvent {
2701 let task = &end.task;
2702 RuntimeEvent {
2703 kind: end.kind.into(),
2704 id: task["id"].as_str().unwrap_or_default().into(),
2705 status: task_status(task).into(),
2706 summary: task_event_summary(task),
2707 fetch: end.fetch.clone(),
2708 lane: end.owner_lane.clone(),
2709 root_id: None,
2710 }
2711}
2712
2713fn cell_end_event(end: CellEnd) -> RuntimeEvent {
2715 let (status, summary) = match &end.outcome {
2716 Ok(result) => (
2717 if result.status == kernel::Status::Ok {
2718 "completed"
2719 } else {
2720 "failed"
2721 },
2722 [result.stdout.as_str(), result.result.as_deref().unwrap_or_default()].join(" "),
2723 ),
2724 Err(error) => ("failed", error.clone()),
2725 };
2726 RuntimeEvent {
2727 kind: "cell_done".into(),
2728 id: end.cell,
2729 status: status.into(),
2730 summary,
2731 fetch: "await rlm.cells()".into(),
2732 lane: end.lane,
2733 root_id: None,
2734 }
2735}
2736
2737fn task_status(task: &Value) -> &str {
2739 task["result"]["status"]
2740 .as_str()
2741 .or(task["state"].as_str())
2742 .unwrap_or("ended")
2743}
2744
2745pub fn only_rlm(tools: &[Value]) -> bool {
2747 tools.len() == 1 && tools[0]["name"] == json!("rlm")
2748}
2749
2750fn add_guard_stats(totals: &mut Vec<Value>, more: &[Value]) {
2753 const COUNTERS: [&str; 7] = [
2754 "checks",
2755 "blocked",
2756 "unchecked",
2757 "afterChecks",
2758 "afterFindings",
2759 "totalMs",
2760 "turnMs",
2761 ];
2762 for (index, stats) in more.iter().enumerate() {
2763 match totals.get_mut(index).filter(|t| t["name"] == stats["name"]) {
2764 Some(total) => {
2765 for key in COUNTERS {
2766 let sum = total[key].as_u64().unwrap_or(0) + stats[key].as_u64().unwrap_or(0);
2767 total[key] = json!(sum);
2768 }
2769 }
2770 None => totals.push(stats.clone()),
2771 }
2772 }
2773}