use std::fmt;
pub struct Block {
pub name: &'static str,
pub body: &'static str,
pub families: &'static [&'static str],
}
pub const AUTO: &str = "auto";
pub const OFF: &str = "off";
pub const MAX_CHARS: usize = 4_000;
const UNIVERSAL_TASK: &str = "task-completion";
const UNIVERSAL_BATCH: &str = "parallel-tool-calls";
pub const BLOCKS: &[Block] = &[
Block {
name: UNIVERSAL_TASK,
families: &[],
body: "Finish the job. A plan, a stub or a described change is not a result: keep working \
until the artifact exists and has been exercised, then report what the tool actually returned. \
Never invent tool output, a file's contents or a test result. When a call fails, say what failed and \
try another way — a different tool, a different command — before giving up; if nothing works, say \
exactly what is blocked and why.",
},
Block {
name: UNIVERSAL_BATCH,
families: &[],
body: "Batch independent tool calls into one reply: calls made together are executed \
together, while every extra turn resends the whole conversation. Read two files at once; run \
commands one at a time only when a later call needs an earlier result.",
},
Block {
name: "tool-use-enforcement",
families: &[
"gpt", "codex", "gemini", "gemma", "grok", "glm", "qwen", "deepseek", "muse",
],
body: "Facts about this machine, this repository or this conversation come from a tool, not \
from memory: counts, arithmetic, dates, file contents, whether a port is open, what a command \
prints. Do not state a number you did not compute with a tool, and do not describe what a file says \
without reading it. When the user asks for a change, the tool call is the answer.",
},
Block {
name: "execution-guidance",
families: &["gpt", "codex", "grok", "glm", "qwen", "deepseek", "muse"],
body: "After a write, read the file back with `read_file` (or read the command's own output) \
before saying it worked. Keep identifiers, paths and numbers exactly as given: never \"repair\" a \
token, a case or a spelling that looks wrong — report it instead. Before saying \"all N done\", count \
what you actually changed and make the number in the sentence match. Prefer `patch` over rewriting a \
file.",
},
Block {
name: "google-operational",
families: &["gemini", "gemma"],
body: "One tool call per step: read its result before the next, and keep prose short — state \
the action, call the tool, report what came back. Do not restate a file you just wrote; cite the \
path and the lines you changed.",
},
];
pub fn is_block(name: &str) -> bool {
BLOCKS.iter().any(|block| block.name == name.trim())
}
pub fn names() -> Vec<&'static str> {
BLOCKS.iter().map(|block| block.name).collect()
}
pub fn block(name: &str) -> Option<&'static Block> {
BLOCKS.iter().find(|b| b.name == name.trim())
}
pub fn fits(block: &Block, model: &str) -> bool {
if block.families.is_empty() {
return true;
}
let model = model.trim().to_lowercase();
block
.families
.iter()
.any(|family| model.contains(&family.to_lowercase()))
}
pub fn text(names: &[&str]) -> String {
let mut out = String::new();
for block in BLOCKS.iter().filter(|b| names.contains(&b.name)) {
if !out.is_empty() {
out.push_str("\n\n");
}
out.push_str(&format!("## {}\n\n{}", block.name, block.body));
}
out
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum Choice {
#[default]
Auto,
Off,
List(Vec<String>),
}
impl Choice {
pub fn names_for(&self, model: &str) -> Vec<&'static str> {
match self {
Choice::Auto => BLOCKS
.iter()
.filter(|block| fits(block, model))
.map(|block| block.name)
.collect(),
Choice::Off => Vec::new(),
Choice::List(names) => BLOCKS
.iter()
.filter(|block| names.iter().any(|n| n == block.name))
.map(|block| block.name)
.collect(),
}
}
pub fn guidance(&self, model: &str) -> String {
text(&self.names_for(model))
}
pub fn summary(&self, model: &str) -> String {
let names = self.names_for(model);
let why = match self {
Choice::Auto => "auto".to_string(),
Choice::Off => "off".to_string(),
Choice::List(_) => "listed".to_string(),
};
if names.is_empty() {
return format!("{why}: no block (the prompt and the context files only)");
}
format!("{why} for `{model}`: {}", names.join(" · "))
}
}
impl fmt::Display for Choice {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Choice::Auto => write!(f, "{AUTO}"),
Choice::Off => write!(f, "{OFF}"),
Choice::List(names) => write!(f, "{}", names.join(" · ")),
}
}
}
impl<'de> serde::Deserialize<'de> for Choice {
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
use serde::de::Error as _;
let value = toml::Value::deserialize(d)?;
match value {
toml::Value::String(word) => match word.trim().to_lowercase().as_str() {
AUTO => Ok(Choice::Auto),
OFF => Ok(Choice::Off),
other => Err(D::Error::custom(format!(
"`prompt_blocks` takes `{AUTO}`, `{OFF}`, or a list of block names, not `{other}`"
))),
},
toml::Value::Array(items) => {
let mut picked = Vec::with_capacity(items.len());
for item in items {
let toml::Value::String(name) = item else {
return Err(D::Error::custom(
"a `prompt_blocks` list holds block names as strings",
));
};
let name = name.trim().to_string();
if name == AUTO || name == OFF {
return Err(D::Error::custom(format!(
"`{name}` is a mode, not a block name; use it alone rather than in a list"
)));
}
if !is_block(&name) {
return Err(D::Error::custom(format!(
"`{name}` is not a prompt block; this build ships {}",
names().join(", ")
)));
}
picked.push(name);
}
Ok(Choice::List(picked))
}
_ => Err(D::Error::custom(format!(
"`prompt_blocks` takes `{AUTO}`, `{OFF}`, or a list of block names"
))),
}
}
}
impl serde::Serialize for Choice {
fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
match self {
Choice::Auto => s.serialize_str(AUTO),
Choice::Off => s.serialize_str(OFF),
Choice::List(names) => names.serialize(s),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn chosen(toml_text: &str) -> Choice {
toml::from_str::<std::collections::BTreeMap<String, Choice>>(toml_text)
.expect("parses")
.remove("prompt_blocks")
.expect("the key")
}
#[test]
fn the_matrix_is_a_fact_for_three_shapes_of_model() {
let auto = Choice::Auto;
assert_eq!(
auto.names_for("gpt-oss:120b"),
vec![
"task-completion",
"parallel-tool-calls",
"tool-use-enforcement",
"execution-guidance"
]
);
assert_eq!(
auto.names_for("mmnuc/gemma-4-12b-it-qat"),
vec![
"task-completion",
"parallel-tool-calls",
"tool-use-enforcement",
"google-operational"
],
"Gemma gets the operational block instead of the execution one"
);
assert_eq!(
auto.names_for("inkling-2-mini"),
vec!["task-completion", "parallel-tool-calls"],
"a family nothing claims gets only what every model gets"
);
assert_eq!(
auto.names_for("DeepSeek-V4.1-Flash"),
auto.names_for("deepseek-v4.1-flash"),
"the gate is case-insensitive"
);
}
#[test]
fn a_family_block_is_not_sent_to_a_model_it_was_not_written_for() {
let auto = Choice::Auto;
for family in [
"tool-use-enforcement",
"execution-guidance",
"google-operational",
] {
assert!(!auto.names_for("llama-3-70b").contains(&family), "{family}");
}
assert!(!auto.names_for("gemma-4").contains(&"execution-guidance"));
assert!(!auto.names_for("gpt-5").contains(&"google-operational"));
}
#[test]
fn no_block_is_sent_twice_and_no_name_is_claimed_twice() {
let auto = Choice::Auto;
for model in ["gpt-4", "gemma-2", "deepseek-x", "nothing-known"] {
let names = auto.names_for(model);
let mut sorted = names.clone();
sorted.sort_unstable();
sorted.dedup();
assert_eq!(sorted.len(), names.len(), "{model}: {names:?}");
let text = auto.guidance(model);
for name in names {
assert_eq!(
text.matches(&format!("## {name}")).count(),
1,
"{name} once in {model}"
);
}
}
let mut all = names();
all.sort_unstable();
let unique = all.len();
all.dedup();
assert_eq!(all.len(), unique, "every block name is unique");
}
#[test]
fn the_whole_library_stays_under_its_budget() {
let whole = text(&names());
assert!(
whole.chars().count() <= MAX_CHARS,
"{}",
whole.chars().count()
);
let universal = text(&[UNIVERSAL_TASK, UNIVERSAL_BATCH]);
assert!(
universal.chars().count() <= 900,
"the universal pair is {} chars",
universal.chars().count()
);
}
#[test]
fn off_and_a_list_are_what_they_say() {
assert!(chosen("prompt_blocks = \"off\"")
.names_for("gpt-4")
.is_empty());
assert_eq!(chosen("prompt_blocks = \"off\"").guidance("gpt-4"), "");
let listed = chosen("prompt_blocks = [\"task-completion\"]");
assert_eq!(listed.names_for("gpt-4"), vec!["task-completion"]);
assert_eq!(
listed.names_for("gemma-2"),
vec!["task-completion"],
"a listed block is not gated: the list is the whole answer"
);
let round = toml::Value::try_from(&listed).unwrap();
assert_eq!(
round,
toml::Value::Array(vec![toml::Value::String("task-completion".into())]),
"a list is written back as a list"
);
assert_eq!(
toml::Value::try_from(&Choice::Auto).unwrap(),
toml::Value::String("auto".into())
);
}
#[test]
fn a_name_this_build_does_not_ship_is_refused() {
let why = toml::from_str::<std::collections::BTreeMap<String, Choice>>(
"prompt_blocks = [\"task-completions\"]",
)
.unwrap_err()
.to_string();
assert!(why.contains("task-completions"), "{why}");
assert!(why.contains("task-completion"), "the list of names: {why}");
let why = toml::from_str::<std::collections::BTreeMap<String, Choice>>(
"prompt_blocks = \"sometimes\"",
)
.unwrap_err()
.to_string();
assert!(why.contains("sometimes"), "{why}");
assert!(why.contains("auto"), "{why}");
}
#[test]
fn the_summary_says_what_this_model_is_being_told() {
let auto = Choice::Auto;
let line = auto.summary("gpt-oss:120b");
assert!(line.contains("auto"), "{line}");
assert!(line.contains("execution-guidance"), "{line}");
let unknown = auto.summary("nothing-known");
assert!(unknown.contains("task-completion"), "{unknown}");
assert!(!unknown.contains("execution-guidance"), "{unknown}");
let off = Choice::Off.summary("gpt-4");
assert!(off.contains("off"), "{off}");
assert!(off.contains("no block"), "{off}");
}
}