use crate::{DeploymentGrade, FeatureFlagGrade};
pub const AGENT_DELEGATION_DEFAULT_GRADE: FeatureFlagGrade = FeatureFlagGrade::Adoption;
pub const DOCKER_CAPABILITY_DEFAULT_GRADE: FeatureFlagGrade = FeatureFlagGrade::Off;
pub const CONTAINER_SANDBOX_DEFAULT_GRADE: FeatureFlagGrade = FeatureFlagGrade::Off;
pub const LUA_DEFAULT_GRADE: FeatureFlagGrade = FeatureFlagGrade::Off;
pub const MACHINE_PAYMENTS_DEFAULT_GRADE: FeatureFlagGrade = FeatureFlagGrade::Off;
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct InternalFeatureFlags {
pub docker_capability: bool,
pub container_sandbox: bool,
pub lua: bool,
}
impl InternalFeatureFlags {
pub fn from_env() -> Self {
Self::for_deployment(DeploymentGrade::from_env())
}
pub fn for_deployment(deployment: DeploymentGrade) -> Self {
let enabled =
|name, default| FeatureFlagGrade::from_env(name, default).available(deployment);
Self {
docker_capability: enabled(
"FEATURE_DOCKER_CAPABILITY",
DOCKER_CAPABILITY_DEFAULT_GRADE,
),
container_sandbox: enabled(
"FEATURE_CONTAINER_SANDBOX",
CONTAINER_SANDBOX_DEFAULT_GRADE,
),
lua: enabled("FEATURE_LUA", LUA_DEFAULT_GRADE),
}
}
pub fn is_enabled(&self, flag: &str) -> bool {
match flag {
"docker_capability" => self.docker_capability,
"container_sandbox" => self.container_sandbox,
"lua" => self.lua,
_ => false,
}
}
}
#[derive(Debug, Clone)]
pub struct ExecutionFeatureDecisions {
pub agent_delegation: bool,
pub internal: InternalFeatureFlags,
deployment: DeploymentGrade,
}
impl ExecutionFeatureDecisions {
pub fn from_env(deployment: DeploymentGrade) -> Self {
Self {
agent_delegation: FeatureFlagGrade::from_env(
"FEATURE_AGENT_DELEGATION",
AGENT_DELEGATION_DEFAULT_GRADE,
)
.available(deployment),
internal: InternalFeatureFlags::for_deployment(deployment),
deployment,
}
}
pub fn is_enabled(&self, flag: &str) -> bool {
match flag {
"docker_capability" | "container_sandbox" | "lua" => self.internal.is_enabled(flag),
"agent_delegation" => self.agent_delegation,
_ => FeatureFlagGrade::from_env(
&format!("FEATURE_{}", flag.to_ascii_uppercase()),
if flag == "machine_payments" {
MACHINE_PAYMENTS_DEFAULT_GRADE
} else {
FeatureFlagGrade::Off
},
)
.available(self.deployment),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn grade_overrides_apply_to_capability_registration() {
const CHILD: &str = "EVERRUNS_FEATURE_GRADE_TEST_CHILD";
const KEYS: &[&str] = &[
"FEATURE_AGENT_DELEGATION",
"FEATURE_DOCKER_CAPABILITY",
"FEATURE_CONTAINER_SANDBOX",
"FEATURE_LUA",
"FEATURE_MACHINE_PAYMENTS",
"FEATURE_UNKNOWN",
];
let cases = [
(None, [true; 4]),
(Some("dev"), [true, false, false, false]),
(Some("internal"), [true; 4]),
(Some("preview"), [false; 4]),
(Some("adoption"), [true; 4]),
(Some("prod"), [true; 4]),
(Some("off"), [false; 4]),
(Some("true"), [false; 4]),
(Some("invalid"), [false; 4]),
];
if let Ok(index) = std::env::var(CHILD) {
let (value, expected) = cases[index.parse::<usize>().unwrap()];
for (deployment, available) in [
DeploymentGrade::Dev,
DeploymentGrade::Poc,
DeploymentGrade::Preview,
DeploymentGrade::Prod,
]
.into_iter()
.zip(expected)
{
let decisions = ExecutionFeatureDecisions::from_env(deployment);
assert_eq!(decisions.agent_delegation, available);
for name in [
"docker_capability",
"container_sandbox",
"lua",
"machine_payments",
"unknown",
] {
let expected = value.is_some() && available;
assert_eq!(
decisions.is_enabled(name),
expected,
"{name}, {deployment}, {value:?}"
);
}
}
return;
}
for (index, (value, _)) in cases.iter().enumerate() {
let mut command = std::process::Command::new(std::env::current_exe().unwrap());
command
.args([
"--exact",
"execution_features::tests::grade_overrides_apply_to_capability_registration",
"--nocapture",
])
.env(CHILD, index.to_string());
for key in KEYS {
command.env_remove(key);
if let Some(value) = value {
command.env(key, value);
}
}
assert!(command.status().unwrap().success(), "{value:?}");
}
}
}