use std::sync::Arc;
use async_trait::async_trait;
use futures::future::BoxFuture;
use strands::Plugin;
use strands::agent::LocalAgent;
use strands::injection::InjectionTrigger;
use strands::sandbox::{ExecuteOptions, Sandbox};
use strands::vended_plugins::context_injector::{ContextInjector, ContextInjectorConfig};
use tokio::sync::OnceCell;
use crate::sandbox::{SandboxShell, shell_of};
const DEFAULT_NAME: &str = "strands:environment";
const DISCOVERY_DEPTH: usize = 2;
pub(crate) const AGENTS_MD_CAP: usize = 16_000;
const SKIP_DIRS: [&str; 8] = [
"node_modules",
"dist",
"build",
"target",
"__pycache__",
"venv",
"site-packages",
"vendor",
];
const AGENTS_FILE: &str = "AGENTS.md";
const README_FILE: &str = "README.md";
const PWSH_PLATFORM_PROBE: &str = "if ($IsLinux -or $IsMacOS) { uname -s } else { 'Windows' }";
#[derive(Debug, Clone, Default, PartialEq, Eq)]
struct Gathered {
platform: Option<String>,
cwd: Option<String>,
shell: Option<SandboxShell>,
agents_md: Option<String>,
other_agents: Vec<String>,
readmes: Vec<String>,
}
#[derive(Debug, Clone, Default, bon::Builder)]
#[builder(on(String, into))]
pub struct EnvironmentContextConfig {
pub name: Option<String>,
}
#[derive(Debug, Clone)]
pub struct EnvironmentContext {
name: String,
}
impl Default for EnvironmentContext {
fn default() -> Self {
Self::new(EnvironmentContextConfig::default())
}
}
impl EnvironmentContext {
#[must_use]
pub fn new(config: EnvironmentContextConfig) -> Self {
Self {
name: config.name.unwrap_or_else(|| DEFAULT_NAME.to_owned()),
}
}
}
#[async_trait]
impl Plugin for EnvironmentContext {
fn name(&self) -> &str {
&self.name
}
async fn init_agent(&self, agent: &mut LocalAgent) -> strands::Result<()> {
let memo: Arc<OnceCell<Gathered>> = Arc::default();
let config = ContextInjectorConfig::new(move |context| {
let memo = memo.clone();
let sandbox = context.agent.sandbox().clone();
async move { Ok(Some(render_environment(&sandbox, &memo).await)) }
})
.with_name(format!("{}:injector", self.name))
.with_trigger(InjectionTrigger::UserTurn);
ContextInjector::new(config).init_agent(agent).await
}
}
async fn render_environment(sandbox: &Arc<dyn Sandbox>, memo: &OnceCell<Gathered>) -> String {
let gathered = memo.get_or_init(|| gather(sandbox)).await;
render(gathered, &today_iso())
}
fn today_iso() -> String {
jiff::Zoned::now().date().to_string()
}
fn render(gathered: &Gathered, date: &str) -> String {
let mut env = Vec::new();
if let Some(platform) = &gathered.platform {
env.push(format!("Platform: {platform}"));
}
if gathered.shell == Some(SandboxShell::PowerShell) {
env.push("Shell: PowerShell (the shell tool runs commands in it, not sh)".to_owned());
}
env.push(format!("Date: {date}"));
if let Some(cwd) = &gathered.cwd {
env.push(format!("Working directory: {cwd}"));
}
let mut sections = vec![format!("<environment>\n{}\n</environment>", env.join("\n"))];
if let Some(agents_md) = gathered.agents_md.as_deref().filter(|text| !text.is_empty()) {
sections.push(format!("<AGENTS.md>\n{agents_md}\n</AGENTS.md>"));
}
if !gathered.other_agents.is_empty() {
sections.push(format!(
"Other AGENTS.md files nearby (read as needed): {}",
gathered.other_agents.join(", ")
));
}
if !gathered.readmes.is_empty() {
sections.push(format!(
"README files nearby (read as needed): {}",
gathered.readmes.join(", ")
));
}
format!("<system-reminder>\n{}\n</system-reminder>", sections.join("\n\n"))
}
async fn gather(sandbox: &Arc<dyn Sandbox>) -> Gathered {
let shell = shell_of(sandbox).await.ok();
let platform_probe = if shell == Some(SandboxShell::PowerShell) {
PWSH_PLATFORM_PROBE
} else {
"uname -s"
};
let platform = probe(sandbox.as_ref(), platform_probe).await;
let cwd = probe(sandbox.as_ref(), "echo \"$PWD\"").await;
let agents_md = read_capped(sandbox.as_ref(), AGENTS_FILE).await;
let mut found = Found::default();
visit(sandbox.as_ref(), String::new(), 0, &mut found).await;
Gathered {
platform,
cwd,
shell,
agents_md,
other_agents: found.agents.into_iter().filter(|path| path != AGENTS_FILE).collect(),
readmes: found.readmes,
}
}
async fn probe(sandbox: &dyn Sandbox, command: &str) -> Option<String> {
let result = sandbox.execute(command, ExecuteOptions::default()).await.ok()?;
let out = result.stdout.trim();
if result.exit_code != 0 || out.is_empty() {
return None;
}
out.lines().next().map(|line| line.trim_end_matches('\r').to_owned())
}
async fn read_capped(sandbox: &dyn Sandbox, path: &str) -> Option<String> {
let text = sandbox.read_text(path).await.ok()?;
if text.chars().count() <= AGENTS_MD_CAP {
return Some(text);
}
let kept: String = text.chars().take(AGENTS_MD_CAP).collect();
Some(format!(
"{kept}\n\u{2026} (truncated \u{2014} read the full file if you need the rest)"
))
}
#[derive(Default)]
struct Found {
agents: Vec<String>,
readmes: Vec<String>,
}
fn visit<'a>(sandbox: &'a dyn Sandbox, rel: String, depth: usize, found: &'a mut Found) -> BoxFuture<'a, ()> {
Box::pin(async move {
let listed = if rel.is_empty() { "." } else { rel.as_str() };
let Ok(entries) = sandbox.list_files(listed).await else {
return;
};
for entry in entries {
let path = if rel.is_empty() {
entry.name.clone()
} else {
format!("{rel}/{}", entry.name)
};
if entry.is_dir == Some(true) {
let descend = depth < DISCOVERY_DEPTH
&& !SKIP_DIRS.contains(&entry.name.as_str())
&& !entry.name.starts_with('.');
if descend {
visit(sandbox, path, depth + 1, found).await;
}
continue;
}
match entry.name.as_str() {
AGENTS_FILE => found.agents.push(path),
README_FILE => found.readmes.push(path),
_ => {}
}
}
})
}
#[cfg(test)]
mod tests;