pub const COMPACT_AT_OR_BELOW_TOKENS: u64 = 16_384;
pub const COMPACT_AT_OR_BELOW_PARAMS_B: f64 = 7.0;
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct ModelFacts {
pub context_tokens: Option<u64>,
pub parameters_b: Option<f64>,
pub served_locally: bool,
}
pub fn is_local_endpoint(base_url: &str) -> bool {
is_local_host(host_of(base_url))
}
fn host_of(base_url: &str) -> &str {
let after_scheme = base_url.split_once("//").map_or(base_url, |(_, rest)| rest);
let authority = after_scheme.split('/').next().unwrap_or("");
match authority.strip_prefix('[') {
Some(rest) => rest.split(']').next().unwrap_or(rest),
None => authority.rsplit_once(':').map_or(authority, |(host, _)| host),
}
}
fn is_local_host(host: &str) -> bool {
if matches!(host, "localhost" | "::1" | "0.0.0.0") || host.ends_with(".local") {
return true;
}
let Some(octets) = host.split('.').map(|label| label.parse::<u8>().ok()).collect::<Option<Vec<_>>>()
else {
return false;
};
match octets[..] {
[127, ..] | [10, _, _, _] | [192, 168, _, _] => true,
[172, second, _, _] => (16..=31).contains(&second),
_ => false,
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum PromptProfile {
#[default]
Auto,
Full,
Compact,
}
const CORE_TOOLS: &[&str] = &["read", "list", "glob", "grep", "write", "edit", "bash"];
impl PromptProfile {
pub(crate) fn catalog_budget_bytes(self) -> usize {
match self {
Self::Compact => 1_024,
_ => 8_192,
}
}
pub fn resolve(self, facts: ModelFacts) -> Self {
let Self::Auto = self else { return self };
let cramped = facts.context_tokens.is_some_and(|t| t <= COMPACT_AT_OR_BELOW_TOKENS);
let small = facts.parameters_b.is_some_and(|p| p <= COMPACT_AT_OR_BELOW_PARAMS_B);
let unmeasured = facts.context_tokens.is_none() && facts.parameters_b.is_none();
if cramped || small || (unmeasured && facts.served_locally) {
Self::Compact
} else {
Self::Full
}
}
pub(crate) fn withheld_tools(self, all: &[String]) -> Vec<String> {
match self {
Self::Compact => {
all.iter().filter(|id| !CORE_TOOLS.contains(&id.as_str())).cloned().collect()
}
_ => Vec::new(),
}
}
pub(crate) fn system_prompt(self) -> &'static str {
match self {
Self::Compact => COMPACT_SYSTEM_PROMPT,
_ => FULL_SYSTEM_PROMPT,
}
}
}
pub(crate) const FULL_SYSTEM_PROMPT: &str = include_str!("prompts/full.md");
pub(crate) const COMPACT_SYSTEM_PROMPT: &str = include_str!("prompts/compact.md");
#[cfg(test)]
mod tests {
use super::*;
fn all_tools() -> Vec<String> {
["read", "glob", "grep", "list", "webfetch", "todowrite", "question", "skill", "summarize",
"websearch", "write", "edit", "bash", "applypatch", "task"]
.iter()
.map(|s| (*s).to_owned())
.collect()
}
fn window(tokens: u64) -> ModelFacts {
ModelFacts { context_tokens: Some(tokens), ..ModelFacts::default() }
}
#[test]
fn a_cramped_window_picks_compact() {
assert_eq!(PromptProfile::Auto.resolve(window(4_096)), PromptProfile::Compact);
assert_eq!(PromptProfile::Auto.resolve(window(8_192)), PromptProfile::Compact);
assert_eq!(
PromptProfile::Auto.resolve(window(COMPACT_AT_OR_BELOW_TOKENS)),
PromptProfile::Compact
);
assert_eq!(PromptProfile::Auto.resolve(window(32_768)), PromptProfile::Full);
}
#[test]
fn a_small_model_picks_compact_however_large_its_window() {
let tiny_but_roomy =
ModelFacts { context_tokens: Some(131_072), parameters_b: Some(1.2), ..Default::default() };
assert_eq!(PromptProfile::Auto.resolve(tiny_but_roomy), PromptProfile::Compact);
let big_model =
ModelFacts { context_tokens: Some(131_072), parameters_b: Some(24.0), ..Default::default() };
assert_eq!(PromptProfile::Auto.resolve(big_model), PromptProfile::Full);
}
#[test]
fn a_big_model_on_a_cramped_window_still_picks_compact() {
let squeezed =
ModelFacts { context_tokens: Some(4_096), parameters_b: Some(70.0), ..Default::default() };
assert_eq!(PromptProfile::Auto.resolve(squeezed), PromptProfile::Compact);
}
#[test]
fn one_measurement_is_enough_to_stop_guessing_from_the_endpoint() {
let llama_cpp = ModelFacts {
context_tokens: Some(32_768),
parameters_b: None,
served_locally: true,
};
assert_eq!(
PromptProfile::Auto.resolve(llama_cpp),
PromptProfile::Full,
"a measured window is an answer; the endpoint only decides when nothing is known"
);
let known_model = ModelFacts {
context_tokens: None,
parameters_b: Some(24.0),
served_locally: true,
};
assert_eq!(PromptProfile::Auto.resolve(known_model), PromptProfile::Full);
let small_local = ModelFacts {
context_tokens: Some(4_096),
parameters_b: None,
served_locally: true,
};
assert_eq!(PromptProfile::Auto.resolve(small_local), PromptProfile::Compact);
}
#[test]
fn the_catalog_budget_differs_by_profile_and_is_never_nothing() {
let full = PromptProfile::Full.catalog_budget_bytes();
let compact = PromptProfile::Compact.catalog_budget_bytes();
assert!(compact > 0 && full > 0, "zero would defer a one-line catalog");
assert!(compact < full, "the compact profile carries less, not the same: {compact} vs {full}");
assert_eq!(full, 8 * 1024);
assert_eq!(compact, 1024);
}
#[test]
fn nothing_reported_from_a_hosted_endpoint_keeps_the_full_surface() {
assert_eq!(PromptProfile::Auto.resolve(ModelFacts::default()), PromptProfile::Full);
}
#[test]
fn nothing_reported_from_a_local_endpoint_gets_guidance() {
let local = ModelFacts { served_locally: true, ..Default::default() };
assert_eq!(PromptProfile::Auto.resolve(local), PromptProfile::Compact);
let roomy_local =
ModelFacts { context_tokens: Some(131_072), parameters_b: Some(24.0), served_locally: true };
assert_eq!(PromptProfile::Auto.resolve(roomy_local), PromptProfile::Full);
}
#[test]
fn local_endpoints_are_recognised_by_address() {
for local in [
"http://localhost:11434",
"http://127.0.0.1:8080",
"http://[::1]:8080",
"http://192.168.1.14:11434",
"http://10.0.0.5:8080",
"http://172.16.4.2:8080",
"http://studio.local:1234",
] {
assert!(is_local_endpoint(local), "{local} should read as local");
}
for hosted in [
"https://openrouter.ai/api",
"https://api.deepseek.com",
"https://172.1.2.3.example.com",
"https://api.together.xyz/v1",
] {
assert!(!is_local_endpoint(hosted), "{hosted} should read as hosted");
}
}
#[test]
fn an_explicit_profile_ignores_every_fact() {
assert_eq!(PromptProfile::Full.resolve(window(2_048)), PromptProfile::Full);
assert_eq!(PromptProfile::Compact.resolve(window(200_000)), PromptProfile::Compact);
}
#[test]
fn compact_keeps_the_core_and_withholds_the_rest() {
let withheld = PromptProfile::Compact.withheld_tools(&all_tools());
for core in CORE_TOOLS {
assert!(!withheld.contains(&(*core).to_owned()), "{core} must survive");
}
for optional in ["webfetch", "websearch", "todowrite", "summarize", "task", "skill"] {
assert!(withheld.contains(&optional.to_owned()), "{optional} must be withheld");
}
}
#[test]
fn full_withholds_nothing() {
assert!(PromptProfile::Full.withheld_tools(&all_tools()).is_empty());
}
#[test]
fn the_compact_prompt_is_smaller_but_keeps_every_rule() {
let full = PromptProfile::Full.system_prompt();
let compact = PromptProfile::Compact.system_prompt();
assert!(compact.len() < full.len(), "compact: {} full: {}", compact.len(), full.len());
assert!(compact.contains("READ-ONLY"));
assert!(compact.contains("do NOT try again"), "no retry after a read-only refusal");
assert!(compact.contains("Never guess"), "no inventing file names");
assert!(compact.contains("stop calling tools"), "must know when to finish");
}
}