Initial public release

Luxe 0.1.0: a coding agent harness for the terminal. See README.md to get started and CHANGELOG.md for releases.
This commit is contained in:
Evan Whittaker
2026-10-17 12:08:30 -05:00
commit 09a7de2520
1730 changed files with 412952 additions and 0 deletions

View File

@@ -0,0 +1,99 @@
//! Cache stability — whether each request appends to the last or rewrites what a cache holds.
use std::fmt::Write as _;
use super::call::Call;
/// What changed from each request to the next, in the order of the log — one entry per
/// transition, empty where the prefix held.
pub(super) fn transitions(calls: &[Call]) -> Vec<Vec<String>> {
calls.windows(2).map(|pair| prefix_changes(&pair[0], &pair[1])).collect()
}
/// Does the cacheable prefix actually stay identical between turns?
///
/// Every provider that caches does it by prefix: the tools and system prompt come first, so one
/// changed byte in either throws away the whole cached prefix — and cache pricing means that is a
/// silent bill, not an error. This is the check that catches volatile state (a git dirty flag, a
/// todo counter, a timestamp) sitting inside the cached region.
pub(super) fn cache_stability(out: &mut String, calls: &[Call], transitions: &[Vec<String>]) {
if calls.len() < 2 {
return;
}
let _ = writeln!(out, "## Cache prefix stability\n");
let mut unstable = 0usize;
for (pair, reasons) in calls.windows(2).zip(transitions) {
let (before, after) = (&pair[0], &pair[1]);
let verdict = if reasons.is_empty() {
"stable".to_string()
} else {
unstable += 1;
reasons.join(" · ")
};
let _ = writeln!(out, " #{} → #{}: {}", before.index, after.index, verdict);
}
if unstable > 0 {
let _ = writeln!(
out,
"\n ▲ {unstable} transition(s) invalidate the cached prefix. Everything after the\n \
first changed byte has to be re-read (and re-billed) by the provider."
);
}
let _ = writeln!(out);
}
/// Everything about `after` that a cache cannot reuse from `before`, in prefix order.
///
/// The conversation is checked as well as the prefix above it: a transcript is only cacheable if
/// each request *appends* to the last one, and a harness that edits history in flight —
/// re-truncating an old tool result, dropping a stale image — re-bills every token after the edit
/// at the cache-write rate with nothing to show for it.
pub(super) fn prefix_changes(before: &Call, after: &Call) -> Vec<String> {
let mut reasons = Vec::new();
if before.system() != after.system() {
reasons.push(format!(
"system CHANGED{}",
first_difference(
before.system().unwrap_or_default(),
after.system().unwrap_or_default()
)
));
}
// Only the loaded tools: a deferred definition sits outside the cached prefix, so adding or
// changing one costs nothing
if before.loaded_tools() != after.loaded_tools() {
reasons.push("tools CHANGED".to_string());
}
let (was, now) = (before.cached_messages(), after.cached_messages());
if let Some(at) = was.iter().zip(now.iter()).position(|(a, b)| a != b) {
let resent: usize = was[at..].iter().map(|m| m.chars().count()).sum();
reasons.push(format!(
"conversation REWRITTEN at message {at}/{} (~{} tok re-sent){}",
was.len(),
resent / 4,
first_difference(&was[at], &now[at])
));
}
reasons
}
/// Drop every `cache_control` marker from a message, however deeply nested.
pub(super) fn strip_cache_control(value: &mut serde_json::Value) {
match value {
serde_json::Value::Object(map) => {
map.remove("cache_control");
map.values_mut().for_each(strip_cache_control);
}
serde_json::Value::Array(items) => items.iter_mut().for_each(strip_cache_control),
_ => {}
}
}
/// Where two strings first differ, as a short quoted excerpt.
pub(super) fn first_difference(before: &str, after: &str) -> String {
let common = before.char_indices().zip(after.chars()).take_while(|((_, a), b)| a == b).count();
let excerpt = |text: &str| -> String {
text.chars().skip(common).take(48).collect::<String>().replace('\n', "⏎")
};
format!(" at char {common}: {:?} → {:?}", excerpt(before), excerpt(after))
}

View File

@@ -0,0 +1,202 @@
//! The captured calls — one provider request with what came back, read out of the log.
use std::collections::BTreeMap;
use super::cache::strip_cache_control;
/// One captured provider call: the request body plus whatever came back.
pub(super) struct Call {
/// Position in the log. The provider's own call id restarts per process, so a log stitched
/// from several `luxe` invocations would otherwise show `#0` three times.
pub(super) index: usize,
pub(super) provider: String,
pub(super) url: String,
pub(super) body: serde_json::Value,
pub(super) response: Option<serde_json::Value>,
/// The conversation, read out of `body` once: every section asks for it again
messages: Vec<serde_json::Value>,
/// The system prompt, whichever shape this API uses for it, found once
system: Option<String>,
}
impl Call {
pub(super) fn model(&self) -> &str {
self.body.get("model").and_then(|m| m.as_str()).unwrap_or("?")
}
/// The `## Tools` section of the system prompt, where tool guidance belongs.
pub(super) fn tools_section(&self) -> String {
let system = self.system().unwrap_or_default();
let Some(start) = system.find("## Tools") else { return String::new() };
let rest = &system[start..];
let end = rest[8..].find("\n## ").map(|at| at + 8).unwrap_or(rest.len());
rest[..end].to_string()
}
pub(super) fn new(
index: usize,
provider: String,
url: String,
body: serde_json::Value,
response: Option<serde_json::Value>,
) -> Self {
let messages = messages_of(&body);
let system = system_of(&body, &messages);
Self { index, provider, url, body, response, messages, system }
}
/// The system prompt, whichever shape this API uses for it.
pub(super) fn system(&self) -> Option<&str> {
self.system.as_deref()
}
pub(super) fn messages(&self) -> &[serde_json::Value] {
&self.messages
}
/// The conversation as a cache sees it: one rendering per message, with the breakpoint markers
/// stripped. `cache_control` is metadata rather than content, and the rolling one is *supposed*
/// to move every turn — comparing it would report churn on every transition and hide the real
/// thing this is looking for.
pub(super) fn cached_messages(&self) -> Vec<String> {
self.messages()
.iter()
.map(|message| {
let mut message = message.clone();
strip_cache_control(&mut message);
serde_json::to_string(&message).unwrap_or_default()
})
.collect()
}
pub(super) fn tools(&self) -> Vec<serde_json::Value> {
self.body.get("tools").and_then(|v| v.as_array()).cloned().unwrap_or_default()
}
/// The tools the model actually has in context: every one but those sent with Anthropic's
/// `defer_loading`, which load only where a `tool_reference` asks for them.
pub(super) fn loaded_tools(&self) -> Vec<serde_json::Value> {
self.tools().into_iter().filter(|tool| !is_deferred(tool)).collect()
}
/// `(name, description, whole-schema-json)` per loaded tool, across all three wire shapes.
pub(super) fn tool_entries(&self) -> Vec<(String, String, String)> {
entries(&self.loaded_tools())
}
/// [`Self::tool_entries`] for the deferred tools: on the wire, outside the context.
pub(super) fn deferred_entries(&self) -> Vec<(String, String, String)> {
entries(&self.tools().into_iter().filter(is_deferred).collect::<Vec<_>>())
}
pub(super) fn body_chars(&self) -> usize {
serde_json::to_string(&self.body).map(|s| s.chars().count()).unwrap_or(0)
}
/// [`Self::body_chars`] less the deferred tool definitions, which the provider doesn't bill.
pub(super) fn context_chars(&self) -> usize {
let deferred: usize =
self.deferred_entries().iter().map(|(_, _, json)| json.chars().count()).sum();
self.body_chars().saturating_sub(deferred)
}
pub(super) fn prompt_tokens(&self) -> Option<u64> {
self.response
.as_ref()?
.get("usage")?
.get("prompt_tokens")
.and_then(serde_json::Value::as_u64)
}
}
/// The messages of a request, whichever key this API keeps them under.
fn messages_of(body: &serde_json::Value) -> Vec<serde_json::Value> {
for key in ["messages", "input"] {
if let Some(list) = body.get(key).and_then(|v| v.as_array()) {
return list.clone();
}
}
Vec::new()
}
/// The system prompt of a request, in whichever of the three shapes it was sent.
fn system_of(body: &serde_json::Value, messages: &[serde_json::Value]) -> Option<String> {
// Responses API: a top-level `instructions` string.
if let Some(text) = body.get("instructions").and_then(|v| v.as_str()) {
return Some(text.to_string());
}
// Anthropic: `system` as a string or as content blocks.
match body.get("system") {
Some(serde_json::Value::String(text)) => return Some(text.clone()),
Some(serde_json::Value::Array(blocks)) => {
let joined: String = blocks
.iter()
.filter_map(|block| block.get("text").and_then(|t| t.as_str()))
.collect::<Vec<_>>()
.join("");
if !joined.is_empty() {
return Some(joined);
}
}
_ => {}
}
// Chat completions: a leading `system` message.
messages
.iter()
.find(|m| m.get("role").and_then(|r| r.as_str()) == Some("system"))
.and_then(|m| m.get("content").and_then(|c| c.as_str()))
.map(str::to_string)
}
/// Whether a tool definition is sent with Anthropic's `defer_loading`.
pub(super) fn is_deferred(tool: &serde_json::Value) -> bool {
tool.get("defer_loading").and_then(serde_json::Value::as_bool) == Some(true)
}
/// `(name, description, whole-schema-json)` per tool, across all three wire shapes.
pub(super) fn entries(tools: &[serde_json::Value]) -> Vec<(String, String, String)> {
tools
.iter()
.map(|tool| {
// Chat completions nests under `function`; Responses and Anthropic are flat.
let inner = tool.get("function").unwrap_or(tool);
let name = inner.get("name").and_then(|v| v.as_str()).unwrap_or("?").to_string();
let description =
inner.get("description").and_then(|v| v.as_str()).unwrap_or("").to_string();
let json = serde_json::to_string(tool).unwrap_or_default();
(name, description, json)
})
.collect()
}
/// Read the JSONL log, pairing each request with its response by call id.
pub(super) fn load(text: &str) -> Vec<Call> {
let mut calls: Vec<Call> = Vec::new();
let mut index: BTreeMap<u64, usize> = BTreeMap::new();
for line in text.lines().filter(|line| !line.trim().is_empty()) {
let Ok(record) = serde_json::from_str::<serde_json::Value>(line) else { continue };
let id = record.get("call").and_then(serde_json::Value::as_u64).unwrap_or(0);
let kind = record.get("kind").and_then(|k| k.as_str()).unwrap_or("");
match kind {
"request" => {
index.insert(id, calls.len());
calls.push(Call::new(
calls.len() + 1,
record.get("provider").and_then(|p| p.as_str()).unwrap_or("?").to_string(),
record.get("url").and_then(|u| u.as_str()).unwrap_or("").to_string(),
record.get("body").cloned().unwrap_or(serde_json::Value::Null),
None,
));
}
"response" => {
if let Some(position) = index.get(&id) {
if let Some(call) = calls.get_mut(*position) {
call.response = Some(record);
}
}
}
_ => {}
}
}
calls
}

View File

@@ -0,0 +1,51 @@
//! Secrets — nothing that looks like a live credential may reach the log.
use std::fmt::Write as _;
/// Credential prefixes worth scanning for, each followed by enough key material to be real.
const SECRET_PREFIXES: &[&str] = &["sk-", "sk_live_", "ghp_", "gho_", "xoxb-", "AKIA", "Bearer "];
/// Minimum run of key-shaped characters after a prefix before it counts as a credential.
///
/// Without this, reading Luxe's own `redact.rs` — whose source *contains* every prefix — reports a
/// leak on every audit. The prefix alone is prose; the prefix plus a key is a secret.
const SECRET_BODY: usize = 16;
/// Whether `text` contains something shaped like a live credential.
pub(super) fn looks_like_credential(text: &str) -> Option<String> {
for prefix in SECRET_PREFIXES {
for (at, _) in text.match_indices(prefix) {
let tail: String = text[at + prefix.len()..]
.chars()
.take_while(|c| c.is_ascii_alphanumeric() || *c == '_' || *c == '-')
.collect();
if tail.chars().count() >= SECRET_BODY {
let shown: String = tail.chars().take(4).collect();
return Some(format!("{prefix}{shown}… ({} chars)", tail.chars().count()));
}
}
}
None
}
/// Nothing that looks like a live credential may reach the log.
pub(super) fn report(out: &mut String, text: &str) {
let _ = writeln!(out, "## Secrets\n");
let mut hits = Vec::new();
for (line_number, line) in text.lines().enumerate() {
if let Some(hit) = looks_like_credential(line) {
hits.push(format!("line {}: {hit}", line_number + 1));
}
}
if hits.is_empty() {
let _ = writeln!(
out,
" clean — no credential-shaped value in any request, header, or reply\n"
);
} else {
for hit in hits.iter().take(10) {
let _ = writeln!(out, " ▲ {hit}");
}
let _ = writeln!(out);
}
}

View File

@@ -0,0 +1,257 @@
//! Report sections — sizes, composition, duplication and streaming, one per question the log answers.
use std::collections::BTreeMap;
use std::fmt::Write as _;
use super::call::Call;
/// Characters per token, measured from the run: total request characters over reported prompt
/// tokens. Falls back to the usual ~4 when nothing reported usage.
pub(super) fn chars_per_token(calls: &[Call]) -> f64 {
let (chars, tokens) =
calls.iter().fold((0usize, 0u64), |(chars, tokens), call| match call.prompt_tokens() {
Some(reported) if reported > 0 => (chars + call.context_chars(), tokens + reported),
_ => (chars, tokens),
});
if tokens == 0 {
return 4.0;
}
(chars as f64 / tokens as f64).max(1.0)
}
pub(super) fn tokens(chars: usize, ratio: f64) -> u64 {
(chars as f64 / ratio).round() as u64
}
pub(super) fn size(chars: usize, ratio: f64, total: usize) -> String {
let share = if total == 0 { 0.0 } else { chars as f64 / total as f64 * 100.0 };
format!("{:>7} ch ~{:>6} tok {:>5.1}%", chars, tokens(chars, ratio), share)
}
pub(super) fn overview(out: &mut String, calls: &[Call], ratio: f64, verdict: &[String]) {
let _ = writeln!(out, "# Wire audit\n");
let _ = writeln!(out, "{}\n", verdict.join("\n"));
let _ = writeln!(
out,
"{} provider call(s) · {} · model {} · ~{:.2} chars/token (measured)\n{}\n",
calls.len(),
calls[0].provider,
calls[0].model(),
ratio,
calls[0].url,
);
// `written` sits beside `cached` because the pair is the whole story and either alone misleads:
// a prefix rewritten every turn reads most of itself straight back, so a healthy-looking
// `cached` column can sit on top of a bill that is almost entirely writes.
let _ = writeln!(
out,
"{:<5} {:>8} {:>9} {:>8} {:>8} {:>8} {:>9} {:>8}",
"call", "req ch", "prompt", "cached", "written", "output", "first ms", "total ms"
);
for call in calls {
let usage = call.response.as_ref().and_then(|r| r.get("usage"));
let field = |name: &str| {
usage.and_then(|u| u.get(name)).and_then(serde_json::Value::as_u64).unwrap_or(0)
};
let first = call
.response
.as_ref()
.and_then(|r| r.get("first_delta_ms"))
.and_then(serde_json::Value::as_u64);
let elapsed = call
.response
.as_ref()
.and_then(|r| r.get("elapsed_ms"))
.and_then(serde_json::Value::as_u64)
.unwrap_or(0);
let _ = writeln!(
out,
"{:<5} {:>8} {:>9} {:>8} {:>8} {:>8} {:>9} {:>8}",
call.index,
call.body_chars(),
field("prompt_tokens"),
field("cache_read_tokens"),
field("cache_write_tokens"),
field("completion_tokens"),
first.map(|ms| ms.to_string()).unwrap_or_else(|| "-".into()),
elapsed,
);
}
let _ = writeln!(out);
}
/// Where the request's characters actually go, for the first call (the leanest one) and the last.
pub(super) fn composition(out: &mut String, calls: &[Call], ratio: f64, per_tool: bool) {
for (label, call) in [("first", calls.first()), ("last", calls.last())] {
let Some(call) = call else { continue };
if calls.len() == 1 && label == "last" {
continue;
}
let total = call.body_chars();
let _ = writeln!(out, "## Composition — {label} call (#{}) · {total} chars\n", call.index);
let system = call.system().unwrap_or_default();
let system_chars = system.chars().count();
let tool_entries = call.tool_entries();
let tool_chars: usize = tool_entries.iter().map(|(_, _, json)| json.chars().count()).sum();
let deferred_entries = call.deferred_entries();
let deferred_chars: usize =
deferred_entries.iter().map(|(_, _, json)| json.chars().count()).sum();
let message_chars: usize = call
.messages()
.iter()
.filter(|m| m.get("role").and_then(|r| r.as_str()) != Some("system"))
.map(|m| serde_json::to_string(m).map(|s| s.chars().count()).unwrap_or(0))
.sum();
let other =
total.saturating_sub(system_chars + tool_chars + deferred_chars + message_chars);
let _ = writeln!(out, " system prompt {}", size(system_chars, ratio, total));
for (heading, chars) in system_sections(system) {
let _ = writeln!(out, " {:<22} {}", heading, size(chars, ratio, total));
}
let _ = writeln!(
out,
" tool schemas {} ({} tools)",
size(tool_chars, ratio, total),
tool_entries.len()
);
if per_tool {
let mut sorted = tool_entries.clone();
sorted.sort_by_key(|(_, _, json)| std::cmp::Reverse(json.chars().count()));
for (name, _, json) in sorted {
let _ =
writeln!(out, " {:<22} {}", name, size(json.chars().count(), ratio, total));
}
}
// On the wire but not in the context: the provider bills none of it until a
// `tool_reference` loads one, so it's counted apart rather than as overhead
if !deferred_entries.is_empty() {
let _ = writeln!(
out,
" deferred tools {:>7} ch ({} tools, outside the context until loaded)",
deferred_chars,
deferred_entries.len()
);
}
let _ = writeln!(out, " conversation {}", size(message_chars, ratio, total));
for message in call.messages() {
let role = message.get("role").and_then(|r| r.as_str()).unwrap_or("item");
if role == "system" {
continue;
}
let chars = serde_json::to_string(&message).map(|s| s.chars().count()).unwrap_or(0);
let _ = writeln!(out, " {:<22} {}", role, size(chars, ratio, total));
}
let _ = writeln!(out, " wire overhead {}\n", size(other, ratio, total));
}
}
/// Split a system prompt into its `## Heading` sections with their sizes.
pub(super) fn system_sections(system: &str) -> Vec<(String, usize)> {
let mut sections: Vec<(String, usize)> = Vec::new();
let mut current = String::from("(preamble)");
let mut chars = 0usize;
for line in system.lines() {
if let Some(heading) = line.strip_prefix("## ") {
if chars > 0 {
sections.push((current.clone(), chars));
}
current = heading.trim().to_string();
chars = line.chars().count() + 1;
} else {
chars += line.chars().count() + 1;
}
}
if chars > 0 {
sections.push((current, chars));
}
sections
}
/// Content sent more than once in a single request.
pub(super) fn duplication(out: &mut String, calls: &[Call]) {
let Some(call) = calls.last() else { return };
let _ = writeln!(out, "## Duplication — last call (#{})\n", call.index);
let system = call.system().unwrap_or_default();
let mut findings = Vec::new();
// A tool's description repeated in the prompt is the classic double-send. Scoped to the
// `## Tools` section for the re-listing check: `**git**:` also appears in the environment
// line and in any AGENTS.md that uses bold labels, and flagging those is noise.
let tools_section = call.tools_section();
for (name, description, _) in call.tool_entries() {
let head: String = description.chars().take(60).collect();
if head.chars().count() > 20 && system.contains(head.trim()) {
findings.push(format!("tool `{name}` description also appears in the system prompt"));
}
if tools_section.contains(&format!("**{name}**:")) {
findings.push(format!("tool `{name}` is re-listed in the system prompt"));
}
}
// The same tool result twice means the model paid for the same file/output twice.
let mut seen: BTreeMap<String, usize> = BTreeMap::new();
for message in call.messages() {
let text = serde_json::to_string(&message).unwrap_or_default();
if text.chars().count() < 200 {
continue;
}
*seen.entry(text).or_default() += 1;
}
for (text, count) in seen.iter().filter(|(_, count)| **count > 1) {
let head: String = text.chars().take(70).collect();
findings
.push(format!("a {}-char message is sent {count}× — {head}…", text.chars().count()));
}
if findings.is_empty() {
let _ = writeln!(out, " none found\n");
} else {
for finding in findings {
let _ = writeln!(out, " ▲ {finding}");
}
let _ = writeln!(out);
}
}
/// Did text actually arrive incrementally, or all at once at the end?
pub(super) fn streaming(out: &mut String, calls: &[Call]) {
let _ = writeln!(out, "## Streaming\n");
for call in calls {
let Some(response) = &call.response else { continue };
let arrival: Vec<(u128, usize)> = response
.get("arrival")
.and_then(|a| a.as_array())
.map(|list| {
list.iter()
.filter_map(|entry| {
let pair = entry.as_array()?;
Some((pair.first()?.as_u64()? as u128, pair.get(1)?.as_u64()? as usize))
})
.collect()
})
.unwrap_or_default();
if arrival.is_empty() {
let _ = writeln!(out, " #{}: no deltas (tool-only or empty reply)", call.index);
continue;
}
let first = arrival.first().map(|(ms, _)| *ms).unwrap_or(0);
let last = arrival.last().map(|(ms, _)| *ms).unwrap_or(0);
let span = last.saturating_sub(first).max(1);
let gaps: Vec<u128> =
arrival.windows(2).map(|pair| pair[1].0.saturating_sub(pair[0].0)).collect();
let max_gap = gaps.iter().copied().max().unwrap_or(0);
let _ = writeln!(
out,
" #{}: {} deltas over {} ms (first at {} ms, max gap {} ms) — {}",
call.index,
arrival.len(),
span,
first,
max_gap,
if arrival.len() > 1 && span > 5 { "incremental" } else { "arrived as one chunk" }
);
}
let _ = writeln!(out);
}

View File

@@ -0,0 +1,220 @@
use super::cache::{cache_stability, transitions};
use super::secrets::looks_like_credential;
use super::sections::system_sections;
use super::*;
/// [`cache_stability`] over the transitions of `calls`.
fn stability(out: &mut String, calls: &[Call]) {
cache_stability(out, calls, &transitions(calls));
}
fn call(id: u64, system: &str, tools: serde_json::Value) -> Call {
let body = serde_json::json!({
"model": "m",
"messages": [{"role": "system", "content": system}, {"role": "user", "content": "hi"}],
"tools": tools,
});
Call::new(id as usize + 1, "test".into(), String::new(), body, None)
}
/// A call with `body` as it was sent.
fn with_body(body: serde_json::Value) -> Call {
Call::new(1, "test".into(), String::new(), body, None)
}
#[test]
fn system_prompt_is_found_in_every_wire_shape() {
let chat = call(0, "SYS", serde_json::json!([]));
assert_eq!(chat.system(), Some("SYS"));
let responses = with_body(serde_json::json!({"instructions": "SYS", "input": []}));
assert_eq!(responses.system(), Some("SYS"));
let anthropic = with_body(serde_json::json!({
"system": [{"type": "text", "text": "SYS", "cache_control": {"type": "ephemeral"}}],
"messages": [],
}));
assert_eq!(anthropic.system(), Some("SYS"));
}
#[test]
fn tool_entries_read_both_the_nested_and_flat_shapes() {
let nested = call(
0,
"",
serde_json::json!([{"type": "function", "function": {"name": "read", "description": "d"}}]),
);
assert_eq!(nested.tool_entries()[0].0, "read");
let flat =
call(0, "", serde_json::json!([{"type": "function", "name": "read", "description": "d"}]));
assert_eq!(flat.tool_entries()[0].0, "read");
}
/// A deferred definition is on the wire but outside the context: it isn't counted as a tool
/// the model carries, and adding one doesn't move the cached prefix.
#[test]
fn deferred_tools_are_counted_apart_and_never_break_the_prefix() {
let listed = serde_json::json!({"name": "read", "description": "Read a file."});
let deferred = |name: &str| serde_json::json!({"name": name, "description": "Rarely needed.", "defer_loading": true});
let pair = || {
(
call(0, "SYS", serde_json::json!([listed.clone(), deferred("debug")])),
call(
1,
"SYS",
serde_json::json!([listed.clone(), deferred("debug"), deferred("ast_grep")]),
),
)
};
let (before, after) = pair();
let names = |entries: Vec<(String, String, String)>| -> Vec<String> {
entries.into_iter().map(|(name, _, _)| name).collect()
};
assert_eq!(names(after.tool_entries()), vec!["read"]);
assert_eq!(names(after.deferred_entries()), vec!["debug", "ast_grep"]);
assert!(after.context_chars() < after.body_chars());
let mut out = String::new();
stability(&mut out, &[before, after]);
assert!(!out.contains("tools CHANGED"), "{out}");
let mut out = String::new();
let (before, after) = pair();
composition(&mut out, &[before, after], 4.0, false);
assert!(out.contains("(1 tools)"), "{out}");
assert!(out.contains("deferred tools") && out.contains("(2 tools, outside the context"));
}
/// One turn's messages, as an Anthropic body — with the rolling breakpoint on the last one,
/// the way the adapter really sends it.
fn conversation(messages: serde_json::Value) -> Call {
let mut body = serde_json::json!({"model": "m", "system": "SYS", "tools": []});
let mut messages = messages;
if let Some(last) = messages.as_array_mut().and_then(|list| list.last_mut()) {
last["cache_control"] = serde_json::json!({"type": "ephemeral"});
}
body["messages"] = messages;
with_body(body)
}
/// The half of the prefix nobody was watching, and the reason a real session spent 71% of its
/// cost on cache writes: the system prompt and the tools were rock solid while the harness
/// quietly re-truncated an old tool result on every single call.
#[test]
fn cache_stability_catches_a_rewritten_conversation() {
let before = conversation(serde_json::json!([
{"role": "user", "content": "a full tool result, all 4000 characters of it"},
{"role": "assistant", "content": "ok"},
]));
let after = conversation(serde_json::json!([
{"role": "user", "content": "[1000 tokens of earlier tool output elided]"},
{"role": "assistant", "content": "ok"},
{"role": "user", "content": "next"},
]));
let mut out = String::new();
stability(&mut out, &[before, after]);
assert!(out.contains("conversation REWRITTEN at message 0/2"), "{out}");
assert!(out.contains("invalidate the cached prefix"), "{out}");
}
/// Appending is the whole point, and the breakpoint marker moving with it is not a change.
#[test]
fn appending_a_turn_and_moving_the_breakpoint_is_stable() {
let before = conversation(serde_json::json!([{"role": "user", "content": "one"}]));
let after = conversation(serde_json::json!([
{"role": "user", "content": "one"},
{"role": "assistant", "content": "two"},
]));
let mut out = String::new();
stability(&mut out, &[before, after]);
assert!(out.contains("stable"), "{out}");
assert!(!out.contains('▲'), "{out}");
}
#[test]
fn cache_stability_names_the_first_changed_byte() {
let calls = vec![
call(0, "## Env\ngit: main (clean)", serde_json::json!([])),
call(1, "## Env\ngit: main (dirty)", serde_json::json!([])),
];
let mut out = String::new();
stability(&mut out, &calls);
assert!(out.contains("system CHANGED"), "{out}");
assert!(out.contains("invalidate the cached prefix"), "{out}");
assert!(out.contains("clean") && out.contains("dirty"), "shows the divergence: {out}");
}
#[test]
fn a_stable_prefix_reports_no_warning() {
let calls =
vec![call(0, "same", serde_json::json!([])), call(1, "same", serde_json::json!([]))];
let mut out = String::new();
stability(&mut out, &calls);
assert!(out.contains("stable"));
assert!(!out.contains('▲'), "{out}");
}
#[test]
fn duplication_catches_a_tool_re_listed_in_the_prompt() {
let calls = vec![call(
0,
"## Tools\n\n- **read**: Use 'read' for read",
serde_json::json!([{"type": "function", "function": {"name": "read", "description": "Read a file"}}]),
)];
let mut out = String::new();
duplication(&mut out, &calls);
assert!(out.contains("re-listed in the system prompt"), "{out}");
}
#[test]
fn a_bold_label_outside_the_tools_section_is_not_a_duplicate() {
// `**git**: main (dirty)` is the environment line, and any AGENTS.md may use bold labels.
let calls = vec![call(
0,
"## Environment\n\n**git**: main (dirty)\n\n## Tools\n\nReach for the real tool.",
serde_json::json!([{"type": "function", "function": {"name": "git", "description": "Git ops"}}]),
)];
let mut out = String::new();
duplication(&mut out, &calls);
assert!(out.contains("none found"), "{out}");
}
#[test]
fn a_credential_prefix_in_prose_is_not_a_leak_but_a_real_key_is() {
// Reading Luxe's own redact.rs puts every prefix in a tool result; that is not a leak.
assert!(looks_like_credential("patterns like sk- and ghp_ and AKIA").is_none());
assert!(looks_like_credential("Authorization: Bearer short").is_none());
let leak = looks_like_credential("key=sk-abcdefghijklmnopqrstuvwxyz0123")
.expect("a real-looking key is caught");
assert!(leak.starts_with("sk-abcd"), "{leak}");
assert!(!leak.contains("vwxyz"), "the value itself is not echoed: {leak}");
}
#[test]
fn the_verdict_names_prefix_churn_and_overhead() {
let calls = vec![call(0, "a", serde_json::json!([])), call(1, "b", serde_json::json!([]))];
let verdict = warnings(&calls, &transitions(&calls)).join("\n");
assert!(verdict.contains("cacheable prefix changes on 1"), "{verdict}");
let steady = vec![call(0, "a", serde_json::json!([])), call(1, "a", serde_json::json!([]))];
assert!(warnings(&steady, &transitions(&steady)).join("\n").contains("no structural problems"));
}
#[test]
fn system_sections_measure_each_heading() {
let sections = system_sections("## A\nline\n## B\nlonger line here\n");
let names: Vec<&str> = sections.iter().map(|(name, _)| name.as_str()).collect();
assert_eq!(names, vec!["A", "B"]);
assert!(sections[1].1 > sections[0].1);
}
#[test]
fn chars_per_token_is_measured_from_reported_usage() {
let mut call = call(0, "", serde_json::json!([]));
let chars = call.body_chars();
call.response = Some(serde_json::json!({"usage": {"prompt_tokens": chars / 4}}));
assert!((chars_per_token(&[call]) - 4.0).abs() < 0.5);
}