Luxe 0.1.2: Windows fixes, working compaction, and compaction progress in the dock

This commit is contained in:
Evan Whittaker
2026-10-18 15:04:51 -05:00
parent 4ed87ee4e4
commit 33b87c78a8
69 changed files with 1470 additions and 424 deletions

View File

@@ -15,13 +15,13 @@ mod planner;
mod prune;
mod summary;
pub(crate) use budget::IMAGE_TOKEN_ESTIMATE;
pub use budget::{
clamp_max_tokens_to_context, compaction_threshold, context_fullness_percent, context_tokens,
estimate_calibration, estimate_message_tokens, estimate_prefix_tokens, estimate_tokens,
last_observed_context_tokens, prompt_tokens, usable_context_window, ContextBudget,
CONTEXT_SAFETY_TOKENS,
};
pub(crate) use budget::{summary_max_tokens, IMAGE_TOKEN_ESTIMATE};
pub use context::{context_entries, context_messages};
pub use planner::find_compaction_point;
pub use prune::{prune_tool_outputs, ContextPressure};

View File

@@ -215,6 +215,27 @@ pub fn clamp_max_tokens_to_context(
Some(capped)
}
/// Output allowance for one summary, before the model's limit and the window cut it down.
///
/// Room for the seven sections over a whole window's worth of transcript *and* for the reasoning a
/// model that always thinks spends first: that comes out of the same allowance, and a summary that
/// hits the ceiling is refused whole, so a tight one fails every time on exactly the long sessions
/// that need it.
pub(crate) const SUMMARY_MAX_TOKENS: u32 = 16_384;
/// What one summary request may ask for: [`SUMMARY_MAX_TOKENS`], within `model`'s own output limit
/// (0 for unknown) and what `prompt_tokens` leaves of its window.
pub(crate) fn summary_max_tokens(
model: &crate::provider::ModelInfo,
prompt_tokens: u64,
) -> Option<u32> {
let ceiling = match model.max_tokens {
0 => SUMMARY_MAX_TOKENS,
limit => limit.min(SUMMARY_MAX_TOKENS),
};
clamp_max_tokens_to_context(model.context_window, prompt_tokens, ceiling)
}
/// Percent of the usable window currently occupied. Returns 0.0 when either input is 0 (e.g. no
/// usage observed yet), so callers read "unknown" as empty rather than full.
pub fn context_fullness_percent(used_tokens: u64, usable: u64) -> f64 {

View File

@@ -43,6 +43,27 @@ fn the_output_cap_never_claims_room_the_prompt_is_using() {
assert_eq!(clamp(8_192, 999_999, 32_000), Some(MIN_MAX_TOKENS));
}
/// A summary gets room to think and then write, within the model's limit and the window.
#[test]
fn a_summary_gets_room_beyond_what_reasoning_spends() {
let model = |context_window, max_tokens| crate::provider::ModelInfo {
context_window,
max_tokens,
..Default::default()
};
// Fails if the allowance drops back to where thinking alone could exhaust it
let roomy = summary_max_tokens(&model(1_000_000, 128_000), 880_000).expect("a cap");
assert!(roomy >= 16_000, "{roomy}");
// Fails if the model's own output limit is ignored
assert_eq!(summary_max_tokens(&model(1_000_000, 8_000), 10_000), Some(8_000));
// An unknown limit is no limit of its own
assert_eq!(summary_max_tokens(&model(1_000_000, 0), 10_000), Some(SUMMARY_MAX_TOKENS));
// Fails if the window clamp is skipped
let tight = 200_000 - 190_000 - CONTEXT_SAFETY_TOKENS;
assert_eq!(summary_max_tokens(&model(200_000, 128_000), 190_000), Some(tight));
}
/// The prompt figure the clamp runs on: measured where the provider told us, estimated only
/// for the tail it never saw.
#[test]

View File

@@ -3,13 +3,11 @@
use super::*;
use crate::compaction::CompactionTrigger;
/// Output allowance for one summary. Structured prose over a whole window's worth of transcript;
/// the clamp below trims it further when the slice being summarized leaves less than this.
const SUMMARY_MAX_TOKENS: u32 = 4_096;
mod summarizer;
impl Engine {
/// Run the compaction check over the active path ending at `leaf`, executing it if the
/// threshold is crossed. Failures are logged and non-fatal.
/// threshold is crossed. Failures are announced and non-fatal.
///
/// Returns the leaf the turn should continue from. A compaction that runs appends its summary
/// after `leaf` and becomes the new leaf itself — the caller has to adopt it, or the summary
@@ -86,7 +84,8 @@ impl Engine {
///
/// `focus` is the user's optional steer for the summary ("keep the API decisions"), passed
/// through to the summarizer. Announces the outcome either way: a compaction the user asked
/// for and cannot see happened is indistinguishable from one that failed.
/// for and cannot see happened is indistinguishable from one that failed, and a failure
/// reported as "nothing to do" leaves no way to tell what went wrong.
pub(crate) async fn compact_on_request(
&self,
focus: Option<String>,
@@ -104,9 +103,8 @@ impl Engine {
.await;
return Ok(());
};
let compacted =
self.compact_now(leaf.clone(), CompactionTrigger::Manual, focus, event_tx).await;
if compacted == leaf && !self.is_cancelled() {
let compacted = self.try_compact(leaf, CompactionTrigger::Manual, focus, event_tx).await?;
if compacted.is_none() && !self.is_cancelled() {
let _ = event_tx
.send(Event::Error(
"Nothing to compact — the conversation already fits within the recent-context \
@@ -119,7 +117,7 @@ impl Engine {
}
/// Plan and run one compaction over the path ending at `leaf`, returning the leaf to continue
/// from. Failures are logged, announced, and non-fatal — a turn is still worth taking.
/// from. A failure is announced and non-fatal: a turn is still worth taking.
pub(super) async fn compact_now(
&self,
leaf: EntryId,
@@ -127,32 +125,63 @@ impl Engine {
focus: Option<String>,
event_tx: &mpsc::Sender<Event>,
) -> EntryId {
match self.try_compact(leaf.clone(), trigger, focus, event_tx).await {
Ok(Some(compacted)) => compacted,
Ok(None) => leaf,
Err(error) => {
self.announce_compaction_failure(&error, event_tx).await;
leaf
}
}
}
/// One compaction attempt: the new leaf when a summary was committed, `None` when there was
/// nothing to do or a cancel stopped it, and the reason when it failed.
async fn try_compact(
&self,
leaf: EntryId,
trigger: CompactionTrigger,
focus: Option<String>,
event_tx: &mpsc::Sender<Event>,
) -> Result<Option<EntryId>, EngineError> {
let sid = self.current_session_id().await;
let entries = match self.sessions().active_path(&sid, &leaf) {
Ok(e) => e,
let entries = self.sessions().active_path(&sid, &leaf)?;
let Some(plan) = self.plan_compaction(&entries, trigger).await? else { return Ok(None) };
let before = crate::compaction::context_tokens(&entries);
let _ = event_tx.send(Event::CompactionStarted { tokens_before: plan.tokens_before }).await;
let outcome = self.execute_compaction(leaf, &plan, focus, event_tx).await;
// Paired with the start on every ending, cancel included: a cancel unwinds the summary
// call to an error rather than dropping this future, so this line always runs
if outcome.is_err() {
let _ = event_tx.send(Event::CompactionStopped).await;
}
let compacted = match outcome {
Ok(compacted) => compacted,
// The reader stopped it; nothing failed, so the breaker does not hear of it
Err(_) if self.is_cancelled() => return Ok(None),
Err(e) => {
warn!(error = %e, "compaction: failed to load active path");
return leaf;
self.compaction_failures.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
return Err(e);
}
};
let observed = self.last_context_tokens.load(std::sync::atomic::Ordering::Relaxed);
// The budget belongs to the model the next turn will use: switching to a million-token
// model mid-session has to raise the threshold, not keep compacting at the old one.
let model = self.effective_model().await;
let budget = crate::compaction::ContextBudget {
context_window: model.context_window as u64,
max_tokens: model.max_tokens,
observed_tokens: (observed > 0).then_some(observed as u64),
prefix_tokens: u64::from(
self.last_prefix_tokens.load(std::sync::atomic::Ordering::Relaxed),
),
};
match crate::compaction::find_compaction_point(
&entries,
&self.config.compaction_settings,
budget,
trigger,
) {
self.weigh_compaction(before, &compacted).await;
// The summary now stands for everything it replaced, so the occupancy the next threshold
// check reads has to be re-measured rather than carried over: a stale figure compacts
// again immediately, every turn.
self.last_context_tokens.store(0, std::sync::atomic::Ordering::Relaxed);
self.set_leaf(compacted.clone()).await;
Ok(Some(compacted))
}
/// Where to cut `entries`, or `None` when there is nothing worth summarizing.
async fn plan_compaction(
&self,
entries: &[crate::session::Entry],
trigger: CompactionTrigger,
) -> Result<Option<crate::compaction::CompactionPlan>, EngineError> {
let settings = &self.config.compaction_settings;
let budget = self.context_budget().await;
match crate::compaction::find_compaction_point(entries, settings, budget, trigger) {
Ok(plan) => {
debug!(
?trigger,
@@ -160,131 +189,78 @@ impl Engine {
messages_to_summarize = plan.messages_to_summarize.len(),
"compaction triggered"
);
let before = crate::compaction::context_tokens(&entries);
match self.execute_compaction(leaf.clone(), &plan, focus, event_tx).await {
Ok(compacted) => {
// A summary that freed almost nothing leaves occupancy over the line, so
// the next iteration asks again — a provider call per tool call for the
// rest of the turn. Counting it against the breaker keeps the transcript
// (the summary is not *wrong*, only insufficient) while bounding how many
// times a session may pay to learn the same thing.
let after = self.estimated_context_tokens(&compacted).await;
if crate::compaction::compaction_was_effective(before, after) {
self.compaction_failures.store(0, std::sync::atomic::Ordering::Relaxed);
} else {
let failures = self
.compaction_failures
.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
+ 1;
warn!(
before,
after,
failures,
"compaction freed too little to be worth repeating; \
raise the window or lower compaction.keep_recent"
);
}
// The summary now stands for everything it replaced, so the occupancy the
// next threshold check reads has to be re-measured rather than carried
// over — a stale figure compacts again immediately, every turn.
self.last_context_tokens.store(0, std::sync::atomic::Ordering::Relaxed);
self.set_leaf(compacted.clone()).await;
return compacted;
}
// The reader stopped it; nothing failed, so the breaker does not hear of it
Err(_) if self.is_cancelled() => debug!("compaction cancelled"),
Err(e) => {
let failures = self
.compaction_failures
.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
+ 1;
warn!(error = %e, failures, "compaction execution failed");
if crate::compaction::compaction_circuit_open(failures) {
warn!(
failures,
"auto-compaction disabled for this session after repeated failures"
);
}
}
}
Ok(Some(plan))
}
Err(crate::compaction::CompactionError::Cancelled) => {
debug!("compaction cancelled or below threshold");
Ok(None)
}
Err(e) => warn!(error = %e, "compaction check failed"),
Err(e) => Err(EngineError::Compaction(format!("could not plan the cut: {e}"))),
}
leaf
}
/// The one-off request that asks the summarizer for `input`, the provider to send it to, and
/// the model its cost is priced against.
async fn summary_request(
&self,
input: &str,
focus: Option<&str>,
) -> (
Arc<dyn crate::provider::Provider>,
crate::provider::ModelInfo,
crate::provider::CompletionRequest,
) {
// The `## Heading` shape is named because it is what the reply is parsed by: a model told
// only to be "structured" may answer in JSON, and every section of it is then dropped as
// preamble — which a 27B local model did, three times, for a summary that then replaced the
// transcript with nothing. The last sentence is there because a smaller model will otherwise
// file *these rules* under `Constraints & Preferences` as though they were the session's.
let system_prompt =
"You are a session compactor. Summarize the conversation segment below \
so the work can continue from the summary alone. Write about the \
session's own goal, constraints, progress (done, in progress, \
blocked), decisions, next steps, and anything that would be expensive \
to rediscover. Answer in Markdown using exactly these headings, one \
`## ` heading per section with prose under it and nothing before the \
first: `## Goal`, `## Constraints & Preferences`, `## Progress`, \
`## Key Decisions`, `## Next Steps`, `## Critical Context`, \
`## Files` (every file read or modified). These formatting rules are \
not part of the session: never describe them in the summary.";
// The user's steer goes *before* the transcript, where an instruction still reads as one:
// after fifty thousand characters of conversation it reads as more conversation.
let user_message = match focus.map(str::trim).filter(|focus| !focus.is_empty()) {
Some(focus) => format!(
"Summarize the following conversation, paying particular attention to: {focus}\n\n{input}"
/// The window the next turn's model gives the planner, and what of it is already measured.
///
/// The model the next turn will use, not the one that filled the window: switching to a
/// million-token model mid-session has to raise the threshold, not keep compacting at the old
/// one.
async fn context_budget(&self) -> crate::compaction::ContextBudget {
let observed = self.last_context_tokens.load(std::sync::atomic::Ordering::Relaxed);
let model = self.effective_model().await;
crate::compaction::ContextBudget {
context_window: model.context_window as u64,
max_tokens: model.max_tokens,
observed_tokens: (observed > 0).then_some(observed as u64),
prefix_tokens: u64::from(
self.last_prefix_tokens.load(std::sync::atomic::Ordering::Relaxed),
),
None => format!("Summarize the following conversation:\n\n{input}"),
};
}
}
// Resolve the summarizer: the configured `summarizer` override when set, else the active
// model. Route through the provider registry so a cross-provider summarizer works, falling
// back to the default provider when the registry has no entry (tests/single-provider).
let summarizer_model = crate::compaction::summarizer_model(
self.config.compaction_settings.summarizer.as_ref(),
&self.effective_model().await,
/// Reset the failure breaker after a compaction that freed real room, or count one that did not.
///
/// A summary that freed almost nothing leaves occupancy over the line, so the next iteration
/// asks again: a provider call per tool call for the rest of the turn. Counting it keeps the
/// transcript (the summary is not *wrong*, only insufficient) while bounding how many times a
/// session may pay to learn the same thing.
async fn weigh_compaction(&self, before: u64, compacted: &EntryId) {
let after = self.estimated_context_tokens(compacted).await;
if crate::compaction::compaction_was_effective(before, after) {
self.compaction_failures.store(0, std::sync::atomic::Ordering::Relaxed);
return;
}
let failures =
self.compaction_failures.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + 1;
warn!(
before,
after,
failures,
"compaction freed too little to be worth repeating; \
raise the window or lower compaction.keep_recent"
);
let provider = self.provider_for(&summarizer_model);
}
let priced_model = summarizer_model.clone();
let messages = vec![crate::message::Message::user(user_message)];
// The one call whose prompt is a whole window by construction, so the summary's own
// allowance has to come out of what is left rather than on top of it.
let max_tokens = crate::compaction::clamp_max_tokens_to_context(
summarizer_model.context_window,
crate::compaction::estimate_message_tokens(&messages)
+ crate::compaction::estimate_prefix_tokens(Some(system_prompt), &[]),
SUMMARY_MAX_TOKENS,
);
let request = crate::provider::CompletionRequest {
shared_system_len: 0,
model: summarizer_model,
messages,
system: Some(system_prompt.to_string()),
tools: vec![],
deferred_tools: Vec::new(),
thinking: crate::provider::ThinkingLevel::Off,
max_tokens,
reuse: crate::provider::PromptReuse::OneOff,
/// Tell the reader a compaction failed, and that automatic compaction has stopped when this one
/// tripped the breaker: otherwise the first they hear of it is a window too full to answer in.
async fn announce_compaction_failure(
&self,
error: &EngineError,
event_tx: &mpsc::Sender<Event>,
) {
let failures = self.compaction_failures.load(std::sync::atomic::Ordering::Relaxed);
warn!(%error, failures, "compaction failed");
let reason = match error {
EngineError::Compaction(reason) => reason.clone(),
other => other.to_string(),
};
(provider, priced_model, request)
let mut notice = format!("Couldn't compact the conversation: {reason}.");
if crate::compaction::compaction_circuit_open(failures) {
warn!(failures, "auto-compaction disabled for this session after repeated failures");
notice.push_str(
" Automatic compaction is off for this session; run /compact to try again.",
);
}
let _ = event_tx.send(Event::Notice(notice)).await;
}
/// Execute compaction: call the summarizer, format the summary, append it after `leaf`.
@@ -383,87 +359,4 @@ impl Engine {
debug!(%compacted, tokens_before = plan.tokens_before, tokens_after, "compaction executed");
Ok(compacted)
}
/// Sends the summarizer its request, unless a cancel lands first.
async fn open_summary(
&self,
provider: Arc<dyn Provider + Send + Sync>,
request: crate::provider::CompletionRequest,
) -> Result<futures::stream::BoxStream<'static, crate::provider::ProviderEvent>, EngineError>
{
let mut cancel = self.cancel_tx.subscribe();
tokio::select! {
biased;
_ = cancel.wait_for(|cancelled| *cancelled) => Err(cancelled()),
opened = provider.stream(request, self.cancel_tx.subscribe()) => {
opened.map_err(EngineError::Provider)
}
}
}
/// Read the summarizer's reply, billing its usage. Only a reply that finished counts: an
/// error, a length stop or a stream that just ends leave a fragment, however complete it looks.
async fn collect_summary(
&self,
mut stream: futures::stream::BoxStream<'static, crate::provider::ProviderEvent>,
priced_model: &crate::provider::ModelInfo,
event_tx: &mpsc::Sender<Event>,
) -> Result<String, EngineError> {
use futures::StreamExt;
let mut summary_output = String::new();
let mut finished = None;
// Raced rather than left to the provider, since one mid-request may not look at its own
// cancel receiver until the next byte arrives
let mut cancel = self.cancel_tx.subscribe();
loop {
let event = tokio::select! {
biased;
_ = cancel.wait_for(|cancelled| *cancelled) => return Err(cancelled()),
event = stream.next() => event,
};
let Some(event) = event else { break };
match event {
crate::provider::ProviderEvent::TextDelta { delta, .. } => {
summary_output.push_str(&delta);
}
// the call nobody asked for, and often the most expensive one in a session
crate::provider::ProviderEvent::Usage(usage) => {
self.account_call(
crate::usage::UsageSource::Compaction,
priced_model,
&usage,
event_tx,
)
.await;
}
crate::provider::ProviderEvent::Done { reason } => {
finished = Some(reason);
break;
}
crate::provider::ProviderEvent::Error { message, .. } => {
return Err(EngineError::Compaction(format!(
"the summarizer failed partway ({message}); the transcript stands"
)));
}
_ => {}
}
}
match finished {
None => Err(EngineError::Compaction(
"the summarizer's reply ended before it finished; the transcript stands".into(),
)),
Some(
reason @ (crate::provider::StopReason::Length
| crate::provider::StopReason::ContentFilter),
) => Err(EngineError::Compaction(format!(
"the summary stopped short ({reason:?}); the transcript stands"
))),
Some(_) => Ok(summary_output),
}
}
}
/// The error a cancelled summary unwinds with; callers tell it apart by the cancel flag.
fn cancelled() -> EngineError {
EngineError::Compaction("cancelled; the transcript stands".into())
}

View File

@@ -0,0 +1,161 @@
//! Summarizer — the one-off request a compaction sends, and reading its reply.
//!
//! `compact.rs` decides *when* and *where* to cut; this owns the conversation with the model that
//! writes the summary.
use super::*;
impl Engine {
/// The one-off request that asks the summarizer for `input`, the provider to send it to, and
/// the model its cost is priced against.
pub(super) async fn summary_request(
&self,
input: &str,
focus: Option<&str>,
) -> (
Arc<dyn crate::provider::Provider>,
crate::provider::ModelInfo,
crate::provider::CompletionRequest,
) {
// The `## Heading` shape is named because it is what the reply is parsed by: a model told
// only to be "structured" may answer in JSON, and every section of it is then dropped as
// preamble — which a 27B local model did, three times, for a summary that then replaced the
// transcript with nothing. The last sentence is there because a smaller model will otherwise
// file *these rules* under `Constraints & Preferences` as though they were the session's.
let system_prompt =
"You are a session compactor. Summarize the conversation segment below \
so the work can continue from the summary alone. Write about the \
session's own goal, constraints, progress (done, in progress, \
blocked), decisions, next steps, and anything that would be expensive \
to rediscover. Answer in Markdown using exactly these headings, one \
`## ` heading per section with prose under it and nothing before the \
first: `## Goal`, `## Constraints & Preferences`, `## Progress`, \
`## Key Decisions`, `## Next Steps`, `## Critical Context`, \
`## Files` (every file read or modified). These formatting rules are \
not part of the session: never describe them in the summary.";
// The user's steer goes *before* the transcript, where an instruction still reads as one:
// after fifty thousand characters of conversation it reads as more conversation.
let user_message = match focus.map(str::trim).filter(|focus| !focus.is_empty()) {
Some(focus) => format!(
"Summarize the following conversation, paying particular attention to: {focus}\n\n{input}"
),
None => format!("Summarize the following conversation:\n\n{input}"),
};
// Resolve the summarizer: the configured `summarizer` override when set, else the active
// model. Route through the provider registry so a cross-provider summarizer works, falling
// back to the default provider when the registry has no entry (tests/single-provider).
let summarizer_model = crate::compaction::summarizer_model(
self.config.compaction_settings.summarizer.as_ref(),
&self.effective_model().await,
);
let provider = self.provider_for(&summarizer_model);
let priced_model = summarizer_model.clone();
let messages = vec![crate::message::Message::user(user_message)];
// The one call whose prompt is a whole window by construction, so the summary's own
// allowance has to come out of what is left rather than on top of it.
let max_tokens = crate::compaction::summary_max_tokens(
&summarizer_model,
crate::compaction::estimate_message_tokens(&messages)
+ crate::compaction::estimate_prefix_tokens(Some(system_prompt), &[]),
);
let request = crate::provider::CompletionRequest {
shared_system_len: 0,
model: summarizer_model,
messages,
system: Some(system_prompt.to_string()),
tools: vec![],
deferred_tools: Vec::new(),
thinking: crate::provider::ThinkingLevel::Off,
max_tokens,
reuse: crate::provider::PromptReuse::OneOff,
};
(provider, priced_model, request)
}
/// Sends the summarizer its request, unless a cancel lands first.
pub(super) async fn open_summary(
&self,
provider: Arc<dyn Provider + Send + Sync>,
request: crate::provider::CompletionRequest,
) -> Result<futures::stream::BoxStream<'static, crate::provider::ProviderEvent>, EngineError>
{
let mut cancel = self.cancel_tx.subscribe();
tokio::select! {
biased;
_ = cancel.wait_for(|cancelled| *cancelled) => Err(cancelled()),
opened = provider.stream(request, self.cancel_tx.subscribe()) => {
opened.map_err(EngineError::Provider)
}
}
}
/// Read the summarizer's reply, billing its usage. Only a reply that finished counts: an
/// error, a length stop or a stream that just ends leave a fragment, however complete it looks.
pub(super) async fn collect_summary(
&self,
mut stream: futures::stream::BoxStream<'static, crate::provider::ProviderEvent>,
priced_model: &crate::provider::ModelInfo,
event_tx: &mpsc::Sender<Event>,
) -> Result<String, EngineError> {
use futures::StreamExt;
let mut summary_output = String::new();
let mut finished = None;
// Raced rather than left to the provider, since one mid-request may not look at its own
// cancel receiver until the next byte arrives
let mut cancel = self.cancel_tx.subscribe();
loop {
let event = tokio::select! {
biased;
_ = cancel.wait_for(|cancelled| *cancelled) => return Err(cancelled()),
event = stream.next() => event,
};
let Some(event) = event else { break };
match event {
crate::provider::ProviderEvent::TextDelta { delta, .. } => {
summary_output.push_str(&delta);
}
// the call nobody asked for, and often the most expensive one in a session
crate::provider::ProviderEvent::Usage(usage) => {
self.account_call(
crate::usage::UsageSource::Compaction,
priced_model,
&usage,
event_tx,
)
.await;
}
crate::provider::ProviderEvent::Done { reason } => {
finished = Some(reason);
break;
}
crate::provider::ProviderEvent::Error { message, .. } => {
return Err(EngineError::Compaction(format!(
"the summarizer failed partway ({message}); the transcript stands"
)));
}
_ => {}
}
}
match finished {
None => Err(EngineError::Compaction(
"the summarizer's reply ended before it finished; the transcript stands".into(),
)),
Some(
reason @ (crate::provider::StopReason::Length
| crate::provider::StopReason::ContentFilter),
) => Err(EngineError::Compaction(format!(
"the summary stopped short ({reason:?}); the transcript stands"
))),
Some(_) => Ok(summary_output),
}
}
}
/// The error a cancelled summary unwinds with; callers tell it apart by the cancel flag.
fn cancelled() -> EngineError {
EngineError::Compaction("cancelled; the transcript stands".into())
}

View File

@@ -151,14 +151,10 @@ impl Engine {
debug!("the provider stream was dropped by a cancel before it produced anything");
return StopReason::Other("cancelled".to_string());
}
warn!("the provider returned an empty response; nothing was committed");
let _ = event_tx
.send(Event::Error(
"the model returned an empty response — no text, no tool call. Nothing was added \
to the conversation; send your message again to retry."
.to_string(),
))
.await;
let used = self.last_context_tokens.load(std::sync::atomic::Ordering::Relaxed);
let window = self.effective_model().await.context_window;
warn!(used, window, "the provider returned an empty response; nothing was committed");
let _ = event_tx.send(Event::Error(empty_reply_message(used, window))).await;
StopReason::Other("empty response".to_string())
}
@@ -270,3 +266,47 @@ impl Engine {
Ok(entry_id)
}
}
/// What an empty reply tells the reader. Near a full window the silence is most likely the model
/// having no room left to answer in, and retrying the same message fails the same way until
/// `/compact` makes room.
fn empty_reply_message(used: u32, window: u32) -> String {
let nearly_full = window > 0 && u64::from(used) * 10 >= u64::from(window) * 9;
if !nearly_full {
return "the model returned an empty response — no text, no tool call. Nothing was added \
to the conversation; send your message again to retry."
.to_string();
}
let percent = (u64::from(used) * 100 / u64::from(window)).min(100);
format!(
"the model returned an empty response, most likely because the context window is full \
({percent}% used). Run /compact to summarize the conversation, then send your message \
again."
)
}
#[cfg(test)]
mod tests {
use super::empty_reply_message;
/// The silence at a full window looked like a retryable blip, and every retry failed alike.
#[test]
fn an_empty_reply_at_a_full_window_points_at_compact() {
let message = empty_reply_message(195_000, 200_000);
assert!(message.contains("/compact") && message.contains("97% used"), "{message}");
}
#[test]
fn an_empty_reply_with_room_left_says_retry() {
let message = empty_reply_message(100_000, 200_000);
assert!(!message.contains("/compact") && message.contains("again to retry"), "{message}");
}
/// Before a first measurement, or for a model whose window is unknown, there is nothing to
/// blame the silence on.
#[test]
fn an_unmeasured_window_is_not_called_full() {
assert!(!empty_reply_message(0, 0).contains("/compact"));
assert!(!empty_reply_message(5_000, 0).contains("/compact"));
}
}

View File

@@ -86,10 +86,14 @@ pub(super) enum KnownEvent {
session_id: String,
turn_id: String,
},
CompactionStarted {
tokens_before: u64,
},
CompactionDone {
tokens_before: u64,
tokens_after: u64,
},
CompactionStopped,
ContextUsage {
used_tokens: u32,
context_window: u32,

View File

@@ -12,8 +12,7 @@ use crate::session::EntryId;
use crate::workspace::WorkspaceSnapshot;
use serde::Serialize;
/// core→UI stream. `#[non_exhaustive]` so later phases can add variants.
/// Unknown variants deserialize into [`Event::Other`] and are preserved verbatim.
/// core→UI stream. Unknown variants deserialize into [`Event::Other`] and are preserved verbatim.
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(tag = "kind", content = "content", rename_all = "snake_case")]
#[non_exhaustive]
@@ -65,7 +64,6 @@ pub enum Event {
session_id: String,
/// Persisted entry which owns this message.
entry_id: EntryId,
/// Committed message content.
message: Message,
},
/// A validated remediation report was extracted after its assistant transcript entry committed.
@@ -102,7 +100,6 @@ pub enum Event {
},
/// A resumed session was loaded with its active-path transcript.
SessionLoaded {
/// Active session identity.
session_id: String,
/// Active-path messages paired with their persisted entry IDs.
messages: Vec<SessionMessage>,
@@ -118,8 +115,12 @@ pub enum Event {
/// Opaque turn key accepted by `ReviewTarget::LastTurn`.
turn_id: String,
},
/// A summary began, which can take a minute; `CompactionDone` or `CompactionStopped` ends it.
CompactionStarted { tokens_before: u64 },
/// Compaction completed.
CompactionDone { tokens_before: u64, tokens_after: u64 },
/// A started compaction failed or was cancelled; a failure's reason comes as a `Notice` or `Error`.
CompactionStopped,
/// Token usage after a provider call — lets UIs show context-window fullness.
ContextUsage { used_tokens: u32, context_window: u32 },
/// Token counts + cost for **one provider call**, as it completes.
@@ -546,10 +547,7 @@ pub enum Event {
///
/// The workspace runtime paces its background GitHub refreshes on it: a terminal nobody has
/// looked at for a while is checked less often, and one coming back is checked at once.
TerminalFocusChanged {
/// Whether it now has focus
focused: bool,
},
TerminalFocusChanged { focused: bool },
/// Project-local workspace summary changed.
WorkspaceUpdated(Box<WorkspaceSnapshot>),
/// Structured display state for a tool call changed.

View File

@@ -42,7 +42,9 @@ published! {
StateChanged => "state_changed",
TurnEnded => "turn_ended",
LastTurnAvailable => "last_turn_available",
CompactionStarted => "compaction_started",
CompactionDone => "compaction_done",
CompactionStopped => "compaction_stopped",
ContextUsage => "context_usage",
TurnUsage => "turn_usage",
SessionUsage => "session_usage",
@@ -227,7 +229,9 @@ pub(super) fn every_event() -> Vec<Event> {
Event::StateChanged(EngineState::default()),
Event::TurnEnded { reason: StopReason::EndTurn },
Event::LastTurnAvailable { session_id: s("s1"), turn_id: s("t1") },
Event::CompactionStarted { tokens_before: 2 },
Event::CompactionDone { tokens_before: 2, tokens_after: 1 },
Event::CompactionStopped,
Event::ContextUsage { used_tokens: 1, context_window: 8 },
Event::TurnUsage {
prompt_tokens: 8,

View File

@@ -72,6 +72,30 @@ pub fn dir_name(path: &Path) -> String {
.unwrap_or_else(|| display(path))
}
/// A path as a model wrote it, made one this platform opens: on Windows, Git Bash's `/c/Users/me`
/// is `C:/Users/me`.
///
/// The `bash` tool runs Git Bash there, and its `pwd` prints that form, so a model copies it into
/// a `read` that then looked for `C:\c\Users\…` under the project. Elsewhere, and for every other
/// path, the input comes back untouched: `/c` is a legal directory on unix.
pub fn native(path: &str) -> std::borrow::Cow<'_, str> {
native_on(path, cfg!(windows))
}
fn native_on(path: &str, windows: bool) -> std::borrow::Cow<'_, str> {
let bytes = path.as_bytes();
let drive = windows
&& bytes.len() >= 2
&& bytes[0] == b'/'
&& bytes[1].is_ascii_alphabetic()
&& bytes.get(2).is_none_or(|&byte| byte == b'/');
if !drive {
return std::borrow::Cow::Borrowed(path);
}
let letter = char::from(bytes[1]).to_ascii_uppercase();
std::borrow::Cow::Owned(format!("{letter}:/{}", path.get(3..).unwrap_or("")))
}
/// Rewrite an already-rendered path string to forward slashes.
///
/// For the cases where the string arrived as a string. On unix this is the identity function on
@@ -149,6 +173,29 @@ mod tests {
assert_eq!(slashify_on(r"weird\name.txt", false), r"weird\name.txt");
}
/// Git Bash's `pwd` form reaches the file tools on Windows; without this `read` looked for
/// `C:\c\Users\…` under the project.
#[test]
fn a_git_bash_drive_path_opens_on_windows() {
assert_eq!(native_on("/c/Users/dev/a.py", true), "C:/Users/dev/a.py");
assert_eq!(native_on("/D/work", true), "D:/work", "either case of drive letter");
assert_eq!(native_on("/c", true), "C:/", "the drive itself");
}
#[test]
fn only_a_single_letter_drive_is_rewritten() {
assert_eq!(native_on("/cd/a.py", true), "/cd/a.py", "a longer first part is a folder");
assert_eq!(native_on("/tmp/a.py", true), "/tmp/a.py");
assert_eq!(native_on("src/a.py", true), "src/a.py", "relative paths are left alone");
assert_eq!(native_on("/1/a.py", true), "/1/a.py", "a digit is no drive");
}
/// `/c` is an ordinary directory on unix.
#[test]
fn a_unix_path_is_never_rewritten() {
assert_eq!(native_on("/c/Users/dev", false), "/c/Users/dev");
}
#[test]
fn display_renders_through_the_platform_rule() {
let path: PathBuf = ["src", "main.rs"].iter().collect();

View File

@@ -112,8 +112,11 @@ fn is_path_tool(tool: &str) -> bool {
}
/// Lexically normalize a path (resolve `.`/`..` without touching the filesystem, so it works for
/// not-yet-created write targets). Relative paths are joined onto `root`.
/// not-yet-created write targets). Relative paths are joined onto `root`. Translated by
/// [`crate::paths::native`] first, as the file tools translate it, so the guard judges the file the
/// tool will open.
fn normalize(input: &str, root: &Path) -> PathBuf {
let input = &*crate::paths::native(input);
let joined =
if Path::new(input).is_absolute() { PathBuf::from(input) } else { root.join(input) };
let mut out = PathBuf::new();

View File

@@ -147,6 +147,17 @@ fn the_external_target_matches_the_guard_that_fires_on_it() {
assert_eq!(external_target("grep", &json!({"pattern": "/etc/hosts"}), &root()), None);
}
/// The guard judges the file the tool opens. The file tools read Git Bash's `/c/…` as a drive
/// path on Windows, so a guard that joined it onto the project judged a different file.
#[cfg(windows)]
#[test]
fn a_git_bash_path_is_judged_where_the_tool_opens_it() {
let root = PathBuf::from(r"C:\Users\dev\project");
let target = |path: &str| external_target("edit", &json!({ "path": path }), &root);
assert_eq!(target("/c/Users/dev/project/src/x.rs"), None, "inside the project");
assert_eq!(target("/c/Windows/x.ini"), Some(PathBuf::from(r"C:\Windows\x.ini")));
}
/// Reading a file outside the project is worth a prompt but is not a change, so only the
/// mutating tools reach the ledger.
#[test]

View File

@@ -29,7 +29,7 @@ pub use opener::{
};
pub use process::{
captured_command, detach_from_terminal, group_members, hostname, kill_tree, process_is_alive,
take_stdin, GroupMember,
take_stdin, GroupMember, TreeGuard,
};
/// Directories a package manager drops executables into, checked after `PATH`.
@@ -111,26 +111,105 @@ pub fn resolve(program: &str) -> Option<PathBuf> {
}
/// The runnable file at `base`, trying the platform's executable suffixes.
fn runnable(base: &Path) -> Option<PathBuf> {
runnable_as(base, cfg!(windows), &executable_suffixes(), is_executable)
}
/// The runnable file at `base` when `windows` decides by extension, and `runs` says whether a
/// file exists to be run.
///
/// On unix a program is its own filename. On Windows the name on disk carries an extension from
/// `PATHEXT` — `rg` is `rg.exe`, a shim may be `rg.cmd` — so a bare-name lookup finds nothing at all,
/// which reads as "ripgrep is not installed" on a machine where it plainly is.
fn runnable(base: &Path) -> Option<PathBuf> {
if is_executable(base) {
/// `PATHEXT` — `rg` is `rg.exe`, a shim may be `rg.cmd` — and only those run. A bare name is taken
/// as given only when it already carries one: npm installs an extensionless shell script beside
/// every `.cmd` shim, and taking that file fails with "not a valid Win32 application".
fn runnable_as(
base: &Path,
windows: bool,
suffixes: &[String],
runs: impl Fn(&Path) -> bool,
) -> Option<PathBuf> {
if !windows {
return runs(base).then(|| base.to_path_buf());
}
if has_suffix(base, suffixes) && runs(base) {
return Some(base.to_path_buf());
}
if !cfg!(windows) {
return None;
}
for extension in executable_suffixes() {
suffixes.iter().find_map(|suffix| {
let mut name = base.as_os_str().to_os_string();
name.push(&extension);
name.push(suffix);
let candidate = PathBuf::from(name);
if is_executable(&candidate) {
return Some(candidate);
}
runs(&candidate).then_some(candidate)
})
}
/// Whether `path` already ends in one of `suffixes`, `.CMD` matching `.cmd`.
fn has_suffix(path: &Path, suffixes: &[String]) -> bool {
let extension = path.extension().and_then(|extension| extension.to_str());
extension.is_some_and(|extension| {
suffixes.iter().any(|suffix| suffix.trim_start_matches('.').eq_ignore_ascii_case(extension))
})
}
/// The Python 3 that runs here, as the words that start it: `["py", "-3"]`, `["python3"]`.
///
/// On Windows `python3` and `python` can be the Microsoft Store's stand-in, a file that only prints
/// "Python was not found", so there each candidate is asked for its version before it counts, the
/// `py` launcher first; elsewhere finding the program is enough. Cached, since the answer holds for
/// the life of the process and asking costs a process start.
pub fn python() -> Option<Vec<String>> {
static FOUND: std::sync::OnceLock<Option<Vec<String>>> = std::sync::OnceLock::new();
FOUND
.get_or_init(|| {
python_candidates(cfg!(windows)).into_iter().find(|argv| match cfg!(windows) {
true => answers_as_python3(argv),
false => resolve(&argv[0]).is_some(),
})
})
.clone()
}
/// The ways to start Python 3, best first: Windows' `py` launcher is installed with Python itself,
/// never as a stand-in, while elsewhere `python` can still be Python 2.
fn python_candidates(windows: bool) -> Vec<Vec<String>> {
let argv = |words: &[&str]| words.iter().map(|word| word.to_string()).collect::<Vec<_>>();
match windows {
true => vec![argv(&["py", "-3"]), argv(&["python"]), argv(&["python3"])],
false => vec![argv(&["python3"]), argv(&["python"])],
}
None
}
/// Whether `argv` starts a working Python 3, by asking it, for at most two seconds.
fn answers_as_python3(argv: &[String]) -> bool {
use std::process::{Command, Stdio};
let Some(program) = resolve(&argv[0]) else { return false };
let child = Command::new(program)
.args(&argv[1..])
.args(["-c", "import sys; print(sys.version_info[0])"])
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::null())
.spawn();
let Ok(child) = child else { return false };
wait_briefly(child, std::time::Duration::from_secs(2)).is_some_and(|out| {
out.status.success() && String::from_utf8_lossy(&out.stdout).trim() == "3"
})
}
/// `child`'s output once it exits, or `None` when it has not within `limit`, killed then.
fn wait_briefly(
mut child: std::process::Child,
limit: std::time::Duration,
) -> Option<std::process::Output> {
let deadline = std::time::Instant::now() + limit;
while child.try_wait().ok()?.is_none() {
if std::time::Instant::now() >= deadline {
let _ = child.kill();
let _ = child.wait();
return None;
}
std::thread::sleep(std::time::Duration::from_millis(20));
}
child.wait_with_output().ok()
}
/// Windows executable suffixes, from `PATHEXT` when it is set.

View File

@@ -47,7 +47,7 @@ pub async fn run_bounded_with_input(
command.stdin(std::process::Stdio::piped());
}
let mut child = command.spawn().map_err(BoundedRunError::Spawn)?;
let mut reaper = TreeReaper(child.id());
let mut reaper = super::TreeGuard::arm(&child);
let stdin = child.stdin.take().filter(|_| !input.is_empty());
let feed = async {
if let Some(mut stdin) = stdin {
@@ -60,23 +60,10 @@ pub async fn run_bounded_with_input(
let run = async { tokio::join!(feed, child.wait_with_output()).1 };
match tokio::time::timeout(deadline, run).await {
Ok(result) => {
reaper.0 = None;
reaper.disarm();
result.map_err(BoundedRunError::Wait)
}
// the reaper takes the tree on the way out
Err(_) => Err(BoundedRunError::Timeout { after: deadline }),
}
}
/// Kills a process tree when dropped armed: on a timeout, or when the future waiting on the run is
/// dropped, as a cancelled turn or an abandoned capture drops it. `kill_on_drop` alone reaches the
/// child and nothing it started.
struct TreeReaper(Option<u32>);
impl Drop for TreeReaper {
fn drop(&mut self) {
if let Some(pid) = self.0.take() {
super::kill_tree(pid);
}
}
}

View File

@@ -2,6 +2,9 @@
use std::path::Path;
#[cfg(windows)]
mod job;
/// Kill the process `pid` started *and everything it started*.
///
/// Every timeout path in Luxe needs this, and none of them can use the child handle alone:
@@ -136,22 +139,70 @@ pub fn hostname() -> String {
/// probes for one takes its non-interactive path. Codex starts its commands the same way, and
/// opencode, Pi and Claude Code get it from Node's `detached`.
///
/// A no-op on Windows, where there is no controlling terminal to leave and `taskkill /T` walks the
/// parent-child links instead.
/// On Windows, where there is no controlling terminal to leave, the child gets a console of its own
/// that no window shows: one it shared with Luxe was Luxe's terminal tab, and a program that names
/// its console, as npm does, renamed the tab. Its tree is reached through [`TreeGuard`]'s job.
pub fn detach_from_terminal(command: &mut tokio::process::Command) {
// SAFETY: the hook runs in the forked child before `exec`, where only async-signal-safe work is
// allowed. `new_session` makes one or two raw syscalls through rustix and touches nothing else:
// no allocation, no lock, no state shared with the parent. One of the workspace's two `unsafe`
// blocks, beside `take_stdin`'s on Windows; `std`'s own `setsid` for `Command` is not stable.
// no allocation, no lock, no state shared with the parent. `std`'s own `setsid` for `Command`
// is not stable.
#[cfg(unix)]
#[allow(unsafe_code)]
unsafe {
command.pre_exec(new_session);
}
#[cfg(not(unix))]
#[cfg(windows)]
{
// `CREATE_NO_WINDOW`: a console for the child, shown in no window
const CREATE_NO_WINDOW: u32 = 0x0800_0000;
command.creation_flags(CREATE_NO_WINDOW);
}
#[cfg(not(any(unix, windows)))]
let _ = command;
}
/// A started program's whole tree, killed when dropped armed: on a timeout, and on a cancel, which
/// ends a call by dropping it. Disarm it once the program has exited on its own; nothing is killed
/// then, and a background process it left keeps running, as one does from a terminal.
///
/// On unix the tree is the process group [`detach_from_terminal`] made it lead. On Windows it is a
/// Job Object the child joins as it starts: `taskkill /T` walks parent links, and a shell killed
/// first, as `kill_on_drop` kills it, left its children unlinked and running.
pub struct TreeGuard {
pid: Option<u32>,
#[cfg(windows)]
job: Option<job::Job>,
}
impl TreeGuard {
/// Arms a guard over `child` and everything it starts from here on.
pub fn arm(child: &tokio::process::Child) -> Self {
Self {
pid: child.id().filter(|pid| *pid != 0),
#[cfg(windows)]
job: child.raw_handle().and_then(job::Job::holding),
}
}
/// The program exited on its own: leave what it started alone. A reaped id can be reused, and
/// killing a tree by a recycled id is the one mistake worse than leaking one.
pub fn disarm(&mut self) {
self.pid = None;
}
}
impl Drop for TreeGuard {
fn drop(&mut self) {
let Some(pid) = self.pid.take() else { return };
#[cfg(windows)]
if let Some(job) = &self.job {
job.terminate();
}
kill_tree(pid);
}
}
/// Takes stdin for the caller alone, and gives every process Luxe starts the null device instead.
///
/// `luxe --rpc` reads its protocol from stdin for as long as it runs, and everything it starts

View File

@@ -0,0 +1,65 @@
//! Job — a Windows Job Object holding a command and everything it starts, so a timeout or a cancel
//! can end them all.
//!
//! Windows has no process group to signal. `taskkill /T` walks parent links, and a shell killed
//! first, as `kill_on_drop` kills it, leaves its children unlinked and running. A process started
//! inside a job stays in it at any depth, so ending the job ends the tree whatever died first.
use std::os::windows::io::RawHandle;
use windows_sys::Win32::Foundation::{CloseHandle, HANDLE};
use windows_sys::Win32::System::JobObjects::{
AssignProcessToJobObject, CreateJobObjectW, TerminateJobObject,
};
/// A job holding one process and what it starts. Dropping it closes the handle and leaves them
/// running, as a command that finished leaves a background process it started; [`Job::terminate`]
/// ends them.
pub(crate) struct Job(HANDLE);
// SAFETY: a job handle names a kernel object, and every call on it is thread-safe.
#[allow(unsafe_code)]
unsafe impl Send for Job {}
// SAFETY: as above; `terminate` and `drop` take no state but the handle.
#[allow(unsafe_code)]
unsafe impl Sync for Job {}
impl Job {
/// A new job holding `process`, or `None` when Windows refuses one, as it can for a process
/// already in a job that forbids nesting.
#[allow(unsafe_code)]
pub(crate) fn holding(process: RawHandle) -> Option<Self> {
// SAFETY: `process` is a live process handle lent for this call; the job is created
// unnamed with default security, so nothing else can open it, and is closed here if the
// process cannot join it.
unsafe {
let job = CreateJobObjectW(std::ptr::null(), std::ptr::null());
if job.is_null() {
return None;
}
if AssignProcessToJobObject(job, process as HANDLE) == 0 {
CloseHandle(job);
return None;
}
Some(Self(job))
}
}
/// Ends every process in the job.
#[allow(unsafe_code)]
pub(crate) fn terminate(&self) {
// SAFETY: `self.0` is a live job handle this value owns until it drops.
unsafe {
TerminateJobObject(self.0, 1);
}
}
}
impl Drop for Job {
#[allow(unsafe_code)]
fn drop(&mut self) {
// SAFETY: closed once, here, by its only owner.
unsafe {
CloseHandle(self.0);
}
}
}

View File

@@ -329,3 +329,60 @@ fn an_activated_environment_answers_only_inside_its_own_tree() {
None => std::env::remove_var("VIRTUAL_ENV"),
}
}
/// On Windows the `py` launcher comes first, since it is installed with Python itself and never as
/// the Store's stand-in; elsewhere `python3` does, since `python` can still be Python 2.
#[test]
fn python_is_looked_for_in_the_platform_s_order() {
let first = |windows| python_candidates(windows).remove(0).join(" ");
assert_eq!(first(true), "py -3");
assert_eq!(first(false), "python3");
assert!(python_candidates(true).iter().any(|argv| argv == &["python"]), "then plain python");
}
/// A program that is not Python does not pass for one, however it exits.
#[cfg(unix)]
#[test]
fn something_that_is_not_python_is_not_taken_for_it() {
assert!(!answers_as_python3(&["true".to_string()]), "exits 0 but prints no version");
assert!(!answers_as_python3(&["luxe-no-such-program".to_string()]));
// And a real one does, where one is installed
if resolve("python3").is_some() {
assert!(answers_as_python3(&["python3".to_string()]), "python3 says it is 3");
}
}
fn suffixes() -> Vec<String> {
[".COM", ".EXE", ".BAT", ".CMD"].iter().map(|suffix| suffix.to_string()).collect()
}
/// npm installs a language server three ways, an extensionless shell script, a `.cmd` and a `.ps1`.
/// Windows runs only the `.cmd`, so it is the one found, though the script shares the bare name.
#[test]
fn on_windows_the_extensionless_npm_script_is_passed_over_for_its_cmd() {
let on_disk =
["npm/pyright-langserver", "npm/pyright-langserver.cmd", "npm/pyright-langserver.ps1"];
// Windows names files without regard to case, and `PATHEXT` lists `.CMD`
let runs =
|path: &Path| on_disk.iter().any(|file| file.eq_ignore_ascii_case(path.to_str().unwrap()));
let found = runnable_as(Path::new("npm/pyright-langserver"), true, &suffixes(), runs);
let found = found.map(|path| path.to_string_lossy().to_lowercase());
assert_eq!(found.as_deref(), Some("npm/pyright-langserver.cmd"));
}
/// A name that already carries a runnable extension is taken as given, whatever its case.
#[test]
fn on_windows_a_name_with_its_extension_is_taken_as_given() {
let runs = |path: &Path| path == Path::new("bin/rg.EXE");
let found = runnable_as(Path::new("bin/rg.EXE"), true, &suffixes(), runs);
assert_eq!(found, Some(PathBuf::from("bin/rg.EXE")));
}
/// Elsewhere a program is its own filename, extension or none.
#[test]
fn off_windows_a_program_is_its_own_filename() {
let runs = |path: &Path| path == Path::new("bin/pyright-langserver");
let found = runnable_as(Path::new("bin/pyright-langserver"), false, &suffixes(), runs);
assert_eq!(found, Some(PathBuf::from("bin/pyright-langserver")));
assert_eq!(runnable_as(Path::new("bin/missing"), false, &suffixes(), runs), None);
}

View File

@@ -221,6 +221,8 @@ fn walk_threads() -> usize {
/// (`src/**/*.ts`) and is anchored so it matches at any depth. `**` crosses directory boundaries and
/// `*` does not, which is the dialect every tool description in this repo promises.
pub fn matcher(pattern: &str) -> Result<PathMatcher, globset::Error> {
// On Windows `src\*.py` means a folder, not an escaped `*`; elsewhere a backslash stays an escape
let pattern = &crate::paths::slashify(pattern);
let spans_directories = pattern.contains('/');
let compiled =
if spans_directories { Glob::new(&anchor(pattern))? } else { Glob::new(pattern)? };
@@ -267,6 +269,16 @@ fn anchor(pattern: &str) -> String {
mod tests {
use super::*;
/// A model on Windows writes `src\*.py`, and globset read the backslash as an escape, so the
/// pattern matched nothing.
#[cfg(windows)]
#[test]
fn a_backslash_in_a_windows_glob_is_a_folder() {
let matcher = matcher(r"src\*.py").unwrap();
assert!(matcher.is_match(Path::new("src/a.py")));
assert!(!matcher.is_match(Path::new("lib/a.py")));
}
/// A walk that stops at its ceiling must say so, or a search over a huge tree reads as complete.
#[test]
fn a_walk_that_hits_its_ceiling_says_so() {