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

@@ -18,13 +18,13 @@ name = "luxe"
path = "src/main.rs"
[dependencies]
luxe-core = { path = "../luxe-core", version = "0.1.1" }
luxe-providers = { path = "../luxe-providers", version = "0.1.1" }
luxe-tools = { path = "../luxe-tools", version = "0.1.1" }
luxe-lsp = { path = "../luxe-lsp", version = "0.1.1" }
luxe-dap = { path = "../luxe-dap", version = "0.1.1" }
luxe-mcp = { path = "../luxe-mcp", version = "0.1.1" }
luxe-tui = { path = "../luxe-tui", version = "0.1.1" }
luxe-core = { path = "../luxe-core", version = "0.1.2" }
luxe-providers = { path = "../luxe-providers", version = "0.1.2" }
luxe-tools = { path = "../luxe-tools", version = "0.1.2" }
luxe-lsp = { path = "../luxe-lsp", version = "0.1.2" }
luxe-dap = { path = "../luxe-dap", version = "0.1.2" }
luxe-mcp = { path = "../luxe-mcp", version = "0.1.2" }
luxe-tui = { path = "../luxe-tui", version = "0.1.2" }
anyhow = { workspace = true }
clap = { workspace = true }
shlex = { workspace = true }

View File

@@ -160,15 +160,11 @@ fn debug_adapters() -> String {
Vec::new()
});
for (adapter, from_config) in crate::engine_setup::merged_debug_adapters(&user) {
let program = adapter.spec.command.first().cloned().unwrap_or_default();
let state = match luxe_core::program::resolve(&program).is_some() {
true => "ok ",
false => "none ",
};
let (state, advice) = adapter_state(&adapter.spec.command);
let origin = if from_config { " (debug-adapters.json)" } else { "" };
let _ = writeln!(
out,
" {state} {:<12} {}{origin}",
" {state} {:<12} {}{origin}{advice}",
adapter.spec.id,
adapter.spec.command.join(" ")
);
@@ -177,6 +173,17 @@ fn debug_adapters() -> String {
out
}
/// Whether an adapter's `command` will start, and, when it is there but will not, what to do: the
/// same judgment that decides whether `debug` is offered.
fn adapter_state(command: &[String]) -> (&'static str, String) {
use luxe_dap::adapter::Unready;
match luxe_dap::adapter::unready(command) {
None => ("ok ", String::new()),
Some(why @ Unready::NoModule(..)) => ("none ", format!(" ({why})")),
Some(Unready::Missing(_)) => ("none ", String::new()),
}
}
/// The external programs the tools prefer, and what happens without each.
fn external_programs() -> String {
/// Program, what wants it, and what absence costs.

View File

@@ -2281,6 +2281,14 @@ on `PATH`, and the agent needs a POSIX shell for most commands. Without one, com
If WSL is installed, Luxe skips the `bash.exe` in `System32`, which would run commands inside Linux
against a different filesystem.
Python on Windows is `py` or `python`. `python3` is usually the Microsoft Store's placeholder, so
Luxe tries `py -3` first. For the Python debugger, run `py -m pip install debugpy`. The agent's
commands run Python in UTF-8 mode (`PYTHONUTF8=1`) unless you have set it yourself, so output with
characters outside your code page doesn't crash them.
Windows Terminal keeps a column at the right edge for its scrollbar. To give it to Luxe, set
**Scrollbar visibility** to **Hidden** in your profile's **Appearance** settings.
## Where Luxe keeps files
| | Linux and macOS | Windows |
@@ -3831,7 +3839,7 @@ what you do not recognize.
| `refresh_sessions` | | Answered by `sessions_refreshed` |
| `refresh_workspace` | | Answered by `workspace_updated` |
| `request_context_composition` | | What fills the context window; answered by `context_composition` |
| `compact_now` | `{"focus": "the API decisions"}`, or nothing | Summarize now; answered by `compaction_done` |
| `compact_now` | `{"focus": "the API decisions"}`, or nothing | Summarize now; answered by `compaction_started`, then `compaction_done` or `compaction_stopped` |
| `load_guided_review` | `{"request_id": "…", "target": {"kind": "working_tree"}}` | Capture a Guided Review; see [below](#guided-review) |
| `load_file_diff` | `{"request_id": "…", "path": "src/main.rs"}` | One file's change, outside any review; answered by `file_diff_loaded` |
| `list_directory` | `{"request_id": "…", "path": "src"}` | One project folder's entries, without what `.gitignore` ignores unless `"include_ignored": true`, which marks each such entry `"ignored": true`; answered by `directory_listed` |
@@ -3962,6 +3970,7 @@ There are around sixty. These are the ones a front end is built on:
| `review_started`, `review_finished` | A requested review began on `model`, and came back with a `verdict` (`continue`, `fix` or `unavailable`) and `findings`. On `fix` the findings also become the agent's next turn. |
| `turn_usage`, `session_usage`, `plan_usage` | Cost per call, for the session, and what a subscription has left |
| `context_usage`, `context_composition` | How full the context window is, and with what |
| `compaction_started`, `compaction_done`, `compaction_stopped` | A summary of the conversation is being written, on request or mid-turn. It can take a minute. It always ends in `compaction_done` (with `tokens_after`) or `compaction_stopped` (failed or cancelled). |
| `workspace_updated` | The project, Git changes, todos and tools: what the dock shows |
| `session_loaded`, `sessions_refreshed` | A transcript was restored; the session list changed |
| `notice`, `error` | Something to show. `error` ends the turn. |
@@ -4213,8 +4222,9 @@ your own terminal is how you give both. So when a step needs one, the agent puts
in a code block, you copy it into another terminal and run it, then tell the agent it is done.
The same goes for a command that waits on you at a terminal, such as a password, a confirmation or
an interactive installer. Commands the agent runs have no terminal at all, so one of those fails at
once and says what it wanted, rather than waiting where nobody can answer.
an interactive installer. Commands the agent runs have no terminal and nothing to read from, so one
of those fails or carries on with empty input, rather than waiting where nobody can answer. Most,
such as `sudo`, say what they wanted; a shell's `read` just gets an empty answer.
To allow it anyway, decide ahead of time in your global file. It only helps where no password is
needed, such as a passwordless `sudo` rule for one program:
@@ -4879,4 +4889,7 @@ script -f /tmp/luxe.tty -c luxe # replay with `cat` or `scriptreplay`
asciinema rec /tmp/luxe.cast -c luxe
```
Windows has neither. Take a screenshot instead, and in Windows Terminal right-click the tab and
choose **Export Text** to save what the screen shows.
Attach the recording and the log from the same time.

View File

@@ -68,9 +68,15 @@ rustix = { version = "1", features = ["process", "stdio", "system"] }
# Pointing stdin at the null device on Windows, and keeping the pipe Luxe took out of every child:
# no safe API does either, and without it every child of `luxe --rpc` stalls behind the protocol
# read. See `program::take_stdin`.
# read. See `program::take_stdin`. Job objects end a command's whole tree on a timeout, where
# Windows has no process group to signal; see `program::TreeGuard`.
[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.61", features = ["Win32_Foundation", "Win32_System_Console"] }
windows-sys = { version = "0.61", features = [
"Win32_Foundation",
"Win32_Security",
"Win32_System_Console",
"Win32_System_JobObjects",
] }
[dev-dependencies]
tempfile = "3"

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() {

View File

@@ -300,17 +300,23 @@ async fn compacting_on_request_ignores_the_threshold_and_says_what_it_freed() {
);
action_tx.send(Action::CompactNow { focus: Some("the API decisions".into()) }).await.unwrap();
let mut freed = None;
let (mut started, mut freed) = (None, None);
while let Ok(Some(event)) =
tokio::time::timeout(std::time::Duration::from_secs(10), event_rx.recv()).await
{
if let Event::CompactionDone { tokens_before, tokens_after } = event {
freed = Some((tokens_before, tokens_after));
break;
match event {
Event::CompactionStarted { tokens_before } => started = Some(tokens_before),
Event::CompactionDone { tokens_before, tokens_after } => {
freed = Some((tokens_before, tokens_after));
break;
}
_ => {}
}
}
let (before, after) = freed.expect("a compaction_done event");
assert!(before > after, "it reports what it freed: {before} -> {after}");
// The summary call can take a minute, and without the start a client shows nothing meanwhile
assert_eq!(started, Some(before), "a compaction_started came first, naming what it replaces");
let calls = calls.lock().await;
let summarization =

View File

@@ -141,6 +141,80 @@ impl Provider for CutOffSummaryProvider {
}
}
/// A session on a summarizer that always stops short, `turns` turns in.
async fn cut_off_session(
turns: usize,
tmp: &tempfile::TempDir,
) -> (tokio::sync::mpsc::Sender<Action>, tokio::sync::mpsc::Receiver<Event>, Vec<Event>) {
let mut store = JsonlSessionStore::new(tmp.path().join("sessions"));
let session_id = store.create("/tmp/test").unwrap();
let calls = Arc::new(Mutex::new(Vec::new()));
let provider = Arc::new(CutOffSummaryProvider { calls, cut_off: CutOff::Length });
let (action_tx, mut event_rx) = Engine::spawn(config(store, session_id, provider, tmp.path()));
let mut seen = Vec::new();
for turn in 0..turns {
let prompt = bulky(&format!("TURN-{turn}"));
let submit =
Action::SubmitUserMessage { content: prompt, images: vec![], remediation: None };
action_tx.send(submit).await.unwrap();
seen.extend(crate::fixtures::events_until_turn_end(&mut event_rx).await);
}
(action_tx, event_rx, seen)
}
/// `/compact` says why the summary it ran was refused.
///
/// The break this catches: a summarizer that ran out of output room failed every time, and
/// `/compact` reported it as "Nothing to compact — the conversation already fits", at 100% of a
/// million-token window, so the reader had no way to tell what was wrong.
#[tokio::test]
async fn compacting_on_request_reports_why_the_summary_was_refused() {
let tmp = tempfile::tempdir().unwrap();
let (action_tx, mut event_rx, _) = cut_off_session(6, &tmp).await;
action_tx.send(Action::CompactNow { focus: None }).await.unwrap();
let seen = crate::fixtures::events_until(&mut event_rx, "the compaction's outcome", |e| {
matches!(e, Event::Error(_) | Event::CompactionDone { .. })
})
.await;
let Some(Event::Error(message)) = seen.last() else { panic!("no error: {seen:?}") };
assert!(message.contains("stopped short"), "the real reason is given: {message}");
assert!(!message.contains("Nothing to compact"), "not a no-op: {message}");
// A client showing the compaction as under way needs its end, failure included
let position = |wanted: fn(&Event) -> bool| seen.iter().position(wanted);
let started = position(|e| matches!(e, Event::CompactionStarted { .. }));
let stopped = position(|e| matches!(e, Event::CompactionStopped));
assert!(started.is_some() && started < stopped, "started, then stopped: {seen:?}");
action_tx.send(Action::Close).await.unwrap();
}
/// A failed automatic compaction is announced, and so is the breaker turning it off.
///
/// The break this catches: every summary failed for a whole session and only the log said so;
/// the reader found out when the window was full and the model could no longer answer at all.
#[tokio::test]
async fn a_failing_automatic_compaction_tells_the_reader_and_says_when_it_stops() {
let tmp = tempfile::tempdir().unwrap();
let (action_tx, _event_rx, seen) = cut_off_session(TURNS + 5, &tmp).await;
let notices: Vec<&String> = seen
.iter()
.filter_map(|event| match event {
Event::Notice(message) => Some(message),
_ => None,
})
.collect();
let breaker = luxe_core::compaction::MAX_COMPACTION_FAILURES as usize;
let failures = notices.iter().filter(|notice| notice.contains("stopped short")).count();
assert_eq!(failures, breaker, "one notice per failure, until it stops: {notices:?}");
assert!(
notices.iter().any(|notice| notice.contains("/compact")),
"the last one says it is off and how to retry: {notices:?}"
);
action_tx.send(Action::Close).await.unwrap();
}
/// A summary the summarizer did not finish must not replace the conversation it summarizes.
///
/// The break this catches: the reply loop took an error, a length stop or a dropped stream as

View File

@@ -113,6 +113,11 @@ fn the_frames_a_client_cannot_work_without_are_pinned() {
Event::ActionRejected { kind: None, reason: "line is not JSON".into() },
r#"{"kind":"action_rejected","content":{"reason":"line is not JSON"}}"#,
),
(
Event::CompactionStarted { tokens_before: 180_000 },
r#"{"kind":"compaction_started","content":{"tokens_before":180000}}"#,
),
(Event::CompactionStopped, r#"{"kind":"compaction_stopped"}"#),
(
Event::PermissionRequested {
id: request_id("perm:1"),
@@ -558,7 +563,9 @@ fn every_known_event_roundtrips() {
Event::StateChanged(EngineState::default()),
Event::TurnEnded { reason: StopReason::EndTurn },
Event::LastTurnAvailable { session_id: "session-1".into(), turn_id: "turn-1".into() },
Event::CompactionStarted { tokens_before: 2_000 },
Event::CompactionDone { tokens_before: 2_000, tokens_after: 1_000 },
Event::CompactionStopped,
Event::ContextUsage { used_tokens: 1_000, context_window: 8_000 },
Event::TurnUsage {
prompt_tokens: 800,

View File

@@ -11,7 +11,7 @@ homepage.workspace = true
# Provides a native Debug Adapter Protocol client + the `debug` tool.
[dependencies]
luxe-core = { path = "../luxe-core", version = "0.1.1" }
luxe-core = { path = "../luxe-core", version = "0.1.2" }
thiserror = { workspace = true }
tracing = { workspace = true }
serde = { workspace = true }

View File

@@ -220,7 +220,19 @@ pub fn builtin_adapters() -> Vec<ConfiguredAdapter> {
fn default_adapters() -> Vec<ConfiguredAdapter> {
let customs: Vec<CustomAdapter> = serde_json::from_str(DEFAULT_ADAPTERS_JSON)
.expect("bundled default-debug-adapters.json must be valid");
customs.iter().map(ConfiguredAdapter::from_custom).collect()
customs.iter().map(ConfiguredAdapter::from_custom).map(with_local_python).collect()
}
/// The bundle's `python3` replaced by the Python that runs here, `py -3` on most Windows machines,
/// where `python3` is often the Store's stand-in and debugging would fail at every launch. Kept as
/// written when no Python answers, so the adapter reads as missing rather than as something else.
fn with_local_python(mut adapter: ConfiguredAdapter) -> ConfiguredAdapter {
if adapter.spec.command.first().map(String::as_str) == Some("python3") {
if let Some(python) = luxe_core::program::python() {
adapter.spec.command.splice(..1, python);
}
}
adapter
}
/// Whether any debug adapter — user-configured or built-in — is actually installed.
@@ -230,10 +242,62 @@ fn default_adapters() -> Vec<ConfiguredAdapter> {
/// this so that cost is paid only where the capability exists. Nothing is hidden by doing so —
/// unlike deferring a working tool, an absent adapter means there is no debugging to offer.
pub fn any_adapter_available(custom: &[ConfiguredAdapter]) -> bool {
custom
.iter()
.chain(default_adapters().iter())
.any(|c| c.spec.command.first().is_some_and(|program| on_path(program)))
custom.iter().chain(default_adapters().iter()).any(|c| unready(&c.spec.command).is_none())
}
/// Why an adapter cannot start.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Unready {
/// Its program does not resolve. Args: `(program)`.
Missing(String),
/// Its Python cannot import the module it runs. Args: `(program, module)`.
NoModule(String, String),
}
impl std::fmt::Display for Unready {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Missing(program) => {
write!(formatter, "debug adapter '{program}' not found on PATH")
}
Self::NoModule(program, module) => write!(
formatter,
"the {module} module is not installed for {program}: pip install {module}"
),
}
}
}
/// Why an adapter's `command` cannot start, or `None` when it can.
///
/// Its program has to resolve, and a Python module adapter (`python -m debugpy.adapter`) has to be
/// able to import its module: an interpreter without it starts, then fails every launch with an
/// error that never says to install anything.
pub fn unready(command: &[String]) -> Option<Unready> {
let program = command.first()?;
if !on_path(program) {
return Some(Unready::Missing(program.clone()));
}
let at = command.iter().position(|word| word == "-m")?;
let module = command.get(at + 1)?.split('.').next()?;
(!imports(&command[..at], module))
.then(|| Unready::NoModule(program.clone(), module.to_string()))
}
/// Whether `python` (its argv) can import `module`.
fn imports(python: &[String], module: &str) -> bool {
let Some(program) = python.first().and_then(|program| luxe_core::program::resolve(program))
else {
return false;
};
std::process::Command::new(program)
.args(&python[1..])
.args(["-c", &format!("import {module}")])
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.status()
.is_ok_and(|status| status.success())
}
/// Resolves the adapter for a target program.
@@ -279,13 +343,9 @@ pub fn resolve(
}
fn ensure_installed(spec: AdapterSpec) -> Result<AdapterSpec, DapError> {
if on_path(&spec.command[0]) {
Ok(spec)
} else {
Err(DapError::AdapterUnavailable(format!(
"debug adapter '{}' not found on PATH",
spec.command[0]
)))
match unready(&spec.command) {
None => Ok(spec),
Some(why) => Err(DapError::AdapterUnavailable(why.to_string())),
}
}
@@ -405,4 +465,24 @@ mod tests {
});
assert!(any_adapter_available(&[real]), "an installed command counts as available");
}
/// An adapter is ready when its program resolves and, for a Python module adapter, its module
/// imports; a missing module is named with the command that installs it.
#[test]
fn readiness_names_a_missing_program_or_module() {
let command =
|words: &[&str]| words.iter().map(|word| word.to_string()).collect::<Vec<_>>();
let missing = unready(&command(&["luxe-no-such-adapter"]));
assert_eq!(missing, Some(Unready::Missing("luxe-no-such-adapter".into())));
assert_eq!(unready(&[installed_program()]), None, "no module to check");
let Some(python) = luxe_core::program::python() else { return };
let mut absent = python.clone();
absent.extend(command(&["-m", "luxe_no_such_module.adapter"]));
let Some(Unready::NoModule(_, module)) = unready(&absent) else { panic!("{absent:?}") };
assert_eq!(module, "luxe_no_such_module");
assert!(unready(&absent).unwrap().to_string().contains("pip install luxe_no_such_module"));
let mut present = python;
present.extend(command(&["-m", "json.tool"]));
assert_eq!(unready(&present), None, "a module the standard library has imports");
}
}

View File

@@ -11,7 +11,7 @@ homepage.workspace = true
# Provides a generic LSP client (rust-analyzer first).
[dependencies]
luxe-core = { path = "../luxe-core", version = "0.1.1" }
luxe-core = { path = "../luxe-core", version = "0.1.2" }
thiserror = { workspace = true }
tracing = { workspace = true }
serde = { workspace = true }

View File

@@ -13,7 +13,7 @@ homepage.workspace = true
# no-HTTP rule forbids in core itself. The low-level stdio transport stays in luxe-core.
[dependencies]
luxe-core = { path = "../luxe-core", version = "0.1.1" }
luxe-core = { path = "../luxe-core", version = "0.1.2" }
thiserror = { workspace = true }
tracing = { workspace = true }
serde = { workspace = true }

View File

@@ -11,7 +11,7 @@ homepage.workspace = true
# Provides LLM provider adapters (OpenAI-compat, Anthropic, Gemini).
[dependencies]
luxe-core = { path = "../luxe-core", version = "0.1.1" }
luxe-core = { path = "../luxe-core", version = "0.1.2" }
thiserror = { workspace = true }
tracing = { workspace = true }
serde = { workspace = true }

View File

@@ -11,7 +11,7 @@ homepage.workspace = true
# Provides tool implementations (read, write, edit, bash, grep, glob, etc.).
[dependencies]
luxe-core = { path = "../luxe-core", version = "0.1.1" }
luxe-core = { path = "../luxe-core", version = "0.1.2" }
thiserror = { workspace = true }
tracing = { workspace = true }
serde = { workspace = true }

View File

@@ -437,9 +437,11 @@ mod tree_teardown {
}
/// A shell that backgrounds a grandchild and waits on it. Killing the shell alone leaves the
/// grandchild touching `marker` — which is how a survivor makes itself known.
/// grandchild touching `marker` — which is how a survivor makes itself known. Forward slashes,
/// which Git Bash takes on Windows, where a backslash would be an escape.
fn script(marker: &std::path::Path) -> String {
format!("(for _ in $(seq 1 40); do touch {}; sleep 0.25; done) & wait", marker.display())
let marker = marker.display().to_string().replace('\\', "/");
format!("(for _ in $(seq 1 40); do touch '{marker}'; sleep 0.25; done) & wait")
}
/// Whether anything is still touching `marker`, the loop, once a touch that was already under
@@ -459,8 +461,9 @@ mod tree_teardown {
#[tokio::test]
async fn a_dropped_call_takes_the_whole_tree_with_it() {
if !cfg!(unix) {
return; // the script is a POSIX one
// The script is a POSIX one: Git Bash runs it on Windows, where the tree is a job's
if !luxe_core::program::has_posix_shell() {
return;
}
let dir = tempfile::tempdir().expect("tempdir");
let marker = dir.path().join("alive");
@@ -480,7 +483,7 @@ mod tree_teardown {
#[tokio::test]
async fn a_timed_out_call_takes_the_whole_tree_with_it() {
if !cfg!(unix) {
if !luxe_core::program::has_posix_shell() {
return;
}
let dir = tempfile::tempdir().expect("tempdir");

View File

@@ -510,14 +510,17 @@ async fn under_a_terminal() {
/// A suspended command could never finish, and the old advice was to give it longer. A terminal
/// can no longer suspend one (see `a_command_that_touches_the_terminal_still_finishes`), so this
/// stops one by hand.
///
/// The stop is sent a few times over the first quarter second: on macOS a `SIGSTOP` sent the instant
/// a child is spawned can miss it, and the report then rightly lists a sleeper, not a stopped one.
#[cfg(unix)]
#[tokio::test]
async fn a_suspended_command_is_called_stuck_not_slow() {
let tmp_dir = tempfile::tempdir().unwrap();
let ctx = test_context(tmp_dir.path());
let result = BashTool::new()
.run(serde_json::json!({"command": "sleep 9 & kill -STOP $!; sleep 8", "timeout": 1}), &ctx)
.await;
let command = "sleep 9 & for _ in 1 2 3 4 5; do kill -STOP $!; sleep 0.05; done; sleep 8";
let result =
BashTool::new().run(serde_json::json!({"command": command, "timeout": 1}), &ctx).await;
let ContentBlock::Text(text) = &result.content[0] else { panic!("expected text") };
assert!(text.contains("State T means suspended, not slow"), "{text}");
assert!(!text.contains("pass `timeout`"), "more time cannot help a suspended command: {text}");

View File

@@ -374,7 +374,7 @@ impl Tool for EditTool {
Err(problem) => return ToolResultData::error(problem.message()),
};
let full_path = cx.cwd.join(&request.path);
let full_path = cx.cwd.join(&*luxe_core::paths::native(&request.path));
if let Err(refusal) = self.check_read_guard(cx, &full_path, &request.path) {
return refusal;

View File

@@ -76,10 +76,22 @@ fn shell_command(command: &str, cwd: &Path) -> Command {
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped());
if let Some(value) = python_utf8(std::env::var_os("PYTHONUTF8").as_deref(), cfg!(windows)) {
child.env("PYTHONUTF8", value);
}
luxe_core::program::detach_from_terminal(&mut child);
child
}
/// The `PYTHONUTF8` a command gets, when Luxe sets one: on Windows, unless the user chose.
///
/// Python there writes to a pipe in the console's code page, cp1252, so a script printing `✓`
/// fails with `UnicodeEncodeError` under Luxe and works in the user's terminal. UTF-8 mode is what
/// Python does everywhere else.
fn python_utf8(inherited: Option<&std::ffi::OsStr>, windows: bool) -> Option<&'static str> {
(windows && inherited.is_none()).then_some("1")
}
/// Whether commands here run through a POSIX shell, which decides how an argument is quoted.
pub(crate) fn posix_shell() -> bool {
luxe_core::program::shell().1 == "-c"
@@ -106,7 +118,7 @@ pub(crate) fn quote_argument(value: &str, posix: bool) -> String {
/// Runs `command` in `cwd` until it exits or `timeout` passes.
///
/// Every ending but a clean exit takes the whole tree with it, including the ones nobody writes
/// code for: a cancel drops this future, and [`KillTreeOnDrop`] is what answers that.
/// code for: a cancel drops this future, and a [`luxe_core::program::TreeGuard`] answers that.
pub(crate) async fn run_captured(
command: &str,
cwd: &Path,
@@ -115,8 +127,10 @@ pub(crate) async fn run_captured(
let mut child = shell_command(command, cwd).spawn()?;
let pid = child.id().unwrap_or(0);
// Armed from here to the first normal exit: every early return, and every drop this function
// never sees, takes the whole tree with it.
let mut reaper = KillTreeOnDrop::arm(pid);
// never sees, takes the whole tree with it. A cancel ends the call by dropping this future, so
// there is no code path to hang an explicit kill on: pressing Cancel on a `cargo build` left
// the build running, holding its locks and its CPU, with nothing left watching it.
let mut reaper = luxe_core::program::TreeGuard::arm(&child);
let pipes = Pipes::attach(&mut child);
let (stdout, stderr) = pipes.sinks();
// Not `tokio::time::timeout`: that drops the wait on expiry, and with it the shell
@@ -141,35 +155,6 @@ pub(crate) async fn run_captured(
}
}
/// Kills the command's whole process group unless the command finished on its own.
///
/// `kill_on_drop` reaps the direct child — the shell — and orphans everything the shell started.
/// A **cancel** ends the call by *dropping* its future, so there is no code path to hang an
/// explicit kill on: pressing Cancel on a `cargo build` left the build running, holding its locks
/// and its CPU, with nothing left watching it.
///
/// Disarmed once the child has been reaped, because a reaped pid can be reused and killing a
/// *group* by a recycled id is the one mistake worse than leaking one.
struct KillTreeOnDrop(Option<u32>);
impl KillTreeOnDrop {
fn arm(pid: u32) -> Self {
Self((pid != 0).then_some(pid))
}
fn disarm(&mut self) {
self.0 = None;
}
}
impl Drop for KillTreeOnDrop {
fn drop(&mut self) {
if let Some(pid) = self.0 {
luxe_core::program::kill_tree(pid);
}
}
}
/// One stream's bounded capture: its first bytes, its last bytes, and a count of what fell between.
#[derive(Default)]
pub(crate) struct Capture {

View File

@@ -1,5 +1,22 @@
use super::*;
/// Windows Python writes cp1252 into a pipe, so a `✓` in a test's output crashed it under Luxe.
#[test]
fn windows_commands_get_python_utf8_mode() {
assert_eq!(python_utf8(None, true), Some("1"));
}
#[test]
fn a_python_utf8_the_user_set_is_kept() {
assert_eq!(python_utf8(Some(std::ffi::OsStr::new("0")), true), None);
}
/// Elsewhere Python is UTF-8 already, and the environment is left as the user has it.
#[test]
fn unix_commands_get_no_python_setting() {
assert_eq!(python_utf8(None, false), None);
}
/// However much a stream prints, a capture holds its head and its tail and counts the rest.
#[test]
fn a_capture_holds_its_bound_and_counts_what_fell_between() {

View File

@@ -186,8 +186,7 @@ impl ReadTool {
limit: Option<usize>,
cx: &ToolContext,
) -> ToolResultData {
// Resolve path relative to cwd.
let full_path = cx.cwd.join(path_str);
let full_path = cx.cwd.join(&*luxe_core::paths::native(path_str));
debug!(path = %full_path.display(), offset, limit = limit.unwrap_or(usize::MAX), "reading file");

View File

@@ -161,8 +161,7 @@ impl Tool for WriteTool {
}
};
// Resolve path relative to cwd.
let full_path = cx.cwd.join(path_str);
let full_path = cx.cwd.join(&*luxe_core::paths::native(path_str));
if let Err(refusal) = self.check_blind_overwrite(cx, &full_path, path_str) {
return refusal;

View File

@@ -8,7 +8,7 @@ repository.workspace = true
homepage.workspace = true
[dependencies]
luxe-core = { path = "../luxe-core", version = "0.1.1" }
luxe-core = { path = "../luxe-core", version = "0.1.2" }
# `unstable-rendered-line-info` enables Paragraph::line_count, used for precise wrapping-aware
# mouse hit-testing over word-wrapped surfaces (guide/dock/conversation clicks). It adds no crate;
# the API is marked unstable upstream but is stable within our pinned minor (re-check on upgrade).

View File

@@ -167,11 +167,8 @@ pub struct App {
pub plan_written: Option<String>,
/// The id of the question modal when it is the plan's handoff, which the TUI answers itself
pub plan_handoff: Option<luxe_core::question::QuestionRequestId>,
/// What to send once a fresh session has loaded: the plan, built from scratch
pub pending_build: Option<String>,
/// Whether the composer's draft goes out once a fresh session has loaded — a prompt typed on
/// the start screen, waiting for the session it asked for
pub send_draft_on_load: bool,
/// What waits on the session this TUI last asked the engine for
pub(crate) arrival: navigation::SessionArrival,
pub file_provider: crate::ref_provider::FileRefProvider,
/// The `@` picker's file list being read again, off the render thread
pub file_listing: FileListing,

View File

@@ -157,6 +157,7 @@ impl App {
self.refresh_configuration();
}
WizardOutcome::Finished(outcome) => self.finish_login(*outcome),
WizardOutcome::CopyCode(code) => self.record_copy(code),
}
}

View File

@@ -98,8 +98,7 @@ impl App {
mention_listings: Default::default(),
plan_written: None,
plan_handoff: None,
pending_build: None,
send_draft_on_load: false,
arrival: Default::default(),
permission_auto_approve: false,
file_provider,
file_listing: FileListing::default(),

View File

@@ -8,6 +8,7 @@ use super::{App, ConvoBlock};
use luxe_core::config::NotificationEvent;
use luxe_core::event::Event;
mod compaction;
mod github;
mod review;
mod transcript;
@@ -116,16 +117,11 @@ pub(super) fn reduce(app: &mut App, event: Event) {
Event::ContextComposition(report) => app.push_convo(ConvoBlock::Context(report)),
// Compaction rewrites what the model can see and spends a provider call doing it. Saying
// so is the difference between "the agent forgot" and "the conversation was summarized".
Event::CompactionStarted { .. } => compaction::started(app),
Event::CompactionDone { tokens_before, tokens_after } => {
use luxe_core::usage::format_tokens;
// The summary may not have kept a server's tool names, so the next reference sends them
app.mcp_listed.clear();
app.push_convo(ConvoBlock::System(format!(
"Compacted the conversation — {} → {} of context.",
format_tokens(tokens_before),
format_tokens(tokens_after)
)));
compaction::done(app, tokens_before, tokens_after);
}
Event::CompactionStopped => compaction::ended(app),
Event::WorkspaceUpdated(snapshot) => workspace_updated(app, *snapshot),
Event::ToolActivityUpdated(activity) => app.apply_tool_activity(*activity),
Event::PermissionRequested { id, tool, summary, always_patterns, .. } => {

View File

@@ -0,0 +1,40 @@
//! Compaction — the dock while a summary is written, and its reading once one stands.
//!
//! A summary is one provider call that can take a minute, on request or mid-turn. Without a state
//! of its own the dock showed nothing for `/compact` and the turn's label for an automatic one, and
//! the context reading kept the full figure until the next provider call reported a new one.
use super::super::{App, AppState, ConvoBlock};
/// The engine began writing a summary.
pub(super) fn started(app: &mut App) {
app.status.compacting_mid_turn = matches!(app.status.state, AppState::Streaming);
app.status.state = AppState::Compacting;
}
/// The summary stands: the dock reads what it left, and the transcript says what happened.
pub(super) fn done(app: &mut App, tokens_before: u64, tokens_after: u64) {
use luxe_core::usage::format_tokens;
ended(app);
if app.status.context_window > 0 {
let used = tokens_after as f32 / app.status.context_window as f32 * 100.0;
app.status.context_pct = used.min(100.0);
}
// The summary may not have kept a server's tool names, so the next reference sends them
app.mcp_listed.clear();
app.push_convo(ConvoBlock::System(format!(
"Compacted the conversation — {} → {} of context.",
format_tokens(tokens_before),
format_tokens(tokens_after)
)));
}
/// The compaction under way is over, with or without a summary; the turn it interrupted, if any,
/// carries on.
pub(super) fn ended(app: &mut App) {
if !matches!(app.status.state, AppState::Compacting) {
return;
}
app.status.state =
if app.status.compacting_mid_turn { AppState::Streaming } else { AppState::Idle };
}

View File

@@ -109,6 +109,8 @@ pub(super) fn failed(app: &mut App, msg: String) {
/// A prompt can't outlive its turn (the engine already resolved or dropped it), and a card still
/// spinning is settled as cancelled with `reason`.
fn end_turn(app: &mut App, reason: &str) {
// A load that failed sends no snapshot, and one that never comes must not mute the next real one
app.arrival.owes_snapshot = false;
app.interrupt_sent_at = None;
app.streaming = false;
app.streamed_thinking = false;
@@ -132,7 +134,12 @@ pub(super) fn tool_started(app: &mut App, name: String) {
}
pub(super) fn state_changed(app: &mut App, state: EngineState) {
app.status.state = if state.streaming { AppState::Streaming } else { AppState::Idle };
// The load's own snapshot was taken before the turn its `SessionLoaded` sent, so its idle is
// about a moment that has passed; believed, it hides the running turn until the first token
let stale = std::mem::take(&mut app.arrival.owes_snapshot) && app.turn_in_flight();
if !stale {
follow_reported_activity(app, state.streaming);
}
if !state.model.model.is_empty() {
app.status.model = state.model.display().to_string();
app.status.provider = state.model.provider.clone();
@@ -142,7 +149,16 @@ pub(super) fn state_changed(app: &mut App, state: EngineState) {
// Not marked in the transcript: the composer's title, the bar and the dock say which
// mode is on, and a line per switch would fill the scrollback with every toggle
app.status.mode = state.mode.clone();
if !state.streaming {
}
/// Takes the engine's word on whether a turn is running. A turn that is still running leaves a
/// compaction inside it on show: the summary is the turn's work until its own end event lands.
fn follow_reported_activity(app: &mut App, streaming: bool) {
if streaming && matches!(app.status.state, AppState::Compacting) {
return;
}
app.status.state = if streaming { AppState::Streaming } else { AppState::Idle };
if !streaming {
app.status.active_tool = None;
app.status.tool_started = None;
}

View File

@@ -99,7 +99,7 @@ fn ask_in_a_new_session(app: &mut App) {
return;
}
start_new_session(app);
app.send_draft_on_load = true;
app.arrival.send_draft = true;
}
/// Runs a command typed or found on the start screen.
@@ -167,10 +167,24 @@ impl App {
NextSession::Load(session_id) => (Action::LoadSession(session_id), "load a session"),
NextSession::ResumeLatest => (Action::ResumeLatest, "resume the latest session"),
};
self.send_action(action, doing)
let sent = self.send_action(action, doing);
self.arrival.owes_snapshot = sent;
sent
}
}
/// What waits on a session the TUI asked the engine for, settled as it arrives.
#[derive(Debug, Default)]
pub(crate) struct SessionArrival {
/// The plan's build prompt, built from scratch in the new session
pub(crate) build: Option<String>,
/// Whether the composer's draft goes out once it loads: a prompt typed on the start screen
pub(crate) send_draft: bool,
/// Whether its closing state snapshot is still to come. It trails `SessionLoaded`, which may
/// already have sent a turn, so its "idle" is stale
pub(crate) owes_snapshot: bool,
}
/// Where `/sessions` reads the project's sessions from, and how many reads it has asked for.
#[derive(Default)]
pub(crate) struct SessionCatalog {

View File

@@ -64,7 +64,8 @@ pub(super) fn answered(app: &mut App, answers: Option<Vec<String>>) {
// Sent once the new session has loaded, or it would land in the one being left; parked
// only once there is a new session coming to send it to
if app.send_action(Action::NewSession, "start a new session") {
app.pending_build = Some(build);
app.arrival.build = Some(build);
app.arrival.owes_snapshot = true;
}
}
RUN_AS_LOOP => {
@@ -87,11 +88,11 @@ pub(super) fn answered(app: &mut App, answers: Option<Vec<String>>) {
pub(super) fn session_loaded(app: &mut App) {
// a plan another session wrote is not this session's to offer
app.plan_written = None;
if let Some(build) = app.pending_build.take() {
if let Some(build) = app.arrival.build.take() {
submit(app, &build);
}
// A prompt typed on the start screen, which asked for this session to answer it
if std::mem::take(&mut app.send_draft_on_load) {
if std::mem::take(&mut app.arrival.send_draft) {
super::input::submit_prompt(app);
}
}
@@ -137,7 +138,7 @@ mod tests {
fn building_fresh_parks_nothing_when_no_session_is_coming() {
let mut app = seeded_app("nord", FixtureScenario::ChatIdle).unwrap();
answered(&mut app, Some(vec![BUILD_FRESH.to_string()]));
assert_eq!(app.pending_build, None);
assert_eq!(app.arrival.build, None);
}
/// Building fresh asks for a new session and sends the build prompt once that session loads.
@@ -147,11 +148,12 @@ mod tests {
answered(&mut app, Some(vec![BUILD_FRESH.to_string()]));
let asked: Vec<Action> = std::iter::from_fn(|| engine.try_recv().ok()).collect();
assert!(asked.iter().any(|action| matches!(action, Action::NewSession)), "{asked:?}");
assert!(app.pending_build.is_some());
assert!(app.arrival.build.is_some());
let loaded = Event::SessionLoaded { session_id: "fresh".into(), messages: Vec::new() };
app.update(crate::app::Msg::Engine(loaded));
assert!(app.pending_build.is_none(), "sent into the session that loaded");
assert!(app.arrival.build.is_none(), "sent into the session that loaded");
assert!(app.arrival.owes_snapshot, "and the load's closing snapshot is known to be stale");
let sent: Vec<Action> = std::iter::from_fn(|| engine.try_recv().ok()).collect();
let submitted = sent.iter().any(|action| {
matches!(action, Action::SubmitUserMessage { content, .. } if content.contains("Build the plan"))

View File

@@ -23,6 +23,9 @@ pub struct StatusModel {
pub active_tool: Option<String>,
/// Current state (streaming, idle, compacting).
pub state: AppState,
/// Whether the compaction under way started inside a turn, which carries on once it ends; one
/// asked for with `/compact` returns to idle
pub(crate) compacting_mid_turn: bool,
/// Animation frame counter, advanced on every tick while streaming/compacting.
///
/// Monotonic within a turn: every consumer takes it modulo its own frame count, and the
@@ -101,6 +104,7 @@ impl Default for StatusModel {
cwd: None,
active_tool: None,
state: AppState::Idle,
compacting_mid_turn: false,
spinner_frame: 0,
animating: false,
turn_started: None,

View File

@@ -262,6 +262,42 @@ fn a_prompt_typed_on_the_start_screen_is_sent_in_the_new_session() {
assert!(app.editor.current_input().is_empty(), "and it left the composer");
}
/// The turn a start-screen prompt starts shows as running from the moment it is sent.
///
/// The engine finishes a session load with an idle snapshot *after* `SessionLoaded`, which is what
/// sends the draft, so that snapshot lands on a turn it never saw. The break this catches: taking it
/// at its word hid the activity bar and the clock until the model's first token, a second or two
/// of a screen that looked like nothing had happened.
#[test]
fn the_snapshot_finishing_a_load_does_not_stop_the_turn_the_load_started() {
let (tx, _rx) = mpsc::channel(100);
let mut app = App::new(tx, KeyMap::defaults(), Default::default());
app.status.model = "qwen3.8-27b".into();
app.enter_dashboard(crate::dashboard::DashboardState::new(Vec::new()));
app.editor.insert_text("summarize the parser");
app.handle_dashboard_outcome(crate::dashboard::DashOutcome::Ask);
app.update(Msg::Engine(Event::SessionLoaded { session_id: "s1".into(), messages: vec![] }));
app.update(Msg::Engine(idle_snapshot("new session: s1")));
assert!(crate::statusline::activity_parts(&app).is_some(), "the activity bar stays up");
assert!(app.status.turn_started.is_some(), "and so does the clock");
// Only the one snapshot is stale: a later idle report is believed
app.update(Msg::Engine(idle_snapshot("cancelled")));
assert!(crate::statusline::activity_parts(&app).is_none(), "a later report still counts");
}
/// The engine's at-rest state report, as it closes a session load or a cancel.
fn idle_snapshot(status: &str) -> Event {
Event::StateChanged(luxe_core::event::EngineState {
model: luxe_core::event::ModelRef::new("lmstudio", "qwen3.8-27b"),
streaming: false,
status: status.into(),
mode: luxe_core::mode::BUILD.into(),
thinking: luxe_core::provider::ThinkingLevel::Off,
})
}
/// With no model chosen, asking opens the picker over the start screen and keeps the draft — the
/// same courtesy the composer extends in a session.
#[test]

View File

@@ -346,6 +346,56 @@ fn test_status_model_format_cost() {
assert_eq!(StatusModel::format_cost(Some(0.004)), "<$0.01");
}
fn idle_app() -> App {
let (tx, _rx) = mpsc::channel(8);
App::new(tx, luxe_core::keymap::KeyMap::defaults(), luxe_core::config::Config::default())
}
fn engine_reports_streaming(app: &mut App, streaming: bool) {
app.update(Msg::Engine(Event::StateChanged(luxe_core::event::EngineState {
streaming,
..Default::default()
})));
}
/// `/compact` shows as under way, and its end leaves the dock idle reading what the summary left.
///
/// The break this catches: nothing set the compacting state, so `/compact` showed no sign of life
/// for the minute a summary takes, and the context figure stayed full until the next turn.
#[tokio::test]
async fn compact_on_request_shows_while_it_runs_and_refreshes_the_context_reading() {
let mut app = idle_app();
app.update(Msg::Engine(Event::ContextUsage { used_tokens: 190_000, context_window: 200_000 }));
app.update(Msg::Engine(Event::CompactionStarted { tokens_before: 190_000 }));
assert!(matches!(app.status.state, AppState::Compacting), "{:?}", app.status.state);
app.update(Msg::Engine(Event::CompactionDone { tokens_before: 190_000, tokens_after: 20_000 }));
assert!(matches!(app.status.state, AppState::Idle), "{:?}", app.status.state);
assert!((app.status.context_pct - 10.0).abs() < 0.01, "got {}", app.status.context_pct);
}
/// A compaction inside a turn hands the dock back to the turn, whether or not it succeeded.
#[tokio::test]
async fn a_stopped_compaction_mid_turn_returns_to_the_turn() {
let mut app = idle_app();
engine_reports_streaming(&mut app, true);
app.update(Msg::Engine(Event::CompactionStarted { tokens_before: 190_000 }));
app.update(Msg::Engine(Event::CompactionStopped));
assert!(matches!(app.status.state, AppState::Streaming), "{:?}", app.status.state);
}
/// The engine's turn reports leave a compaction on show while the turn runs, and end it with the turn.
#[tokio::test]
async fn turn_reports_keep_a_compaction_on_show_until_the_turn_ends() {
let mut app = idle_app();
engine_reports_streaming(&mut app, true);
app.update(Msg::Engine(Event::CompactionStarted { tokens_before: 190_000 }));
engine_reports_streaming(&mut app, true);
assert!(matches!(app.status.state, AppState::Compacting), "{:?}", app.status.state);
engine_reports_streaming(&mut app, false);
assert!(matches!(app.status.state, AppState::Idle), "{:?}", app.status.state);
}
pub(super) fn type_into(app: &mut App, text: &str) {
for character in text.chars() {
app.update(Msg::Key(KeyEvent::new(KeyCode::Char(character), KeyModifiers::NONE)));

View File

@@ -123,6 +123,8 @@ pub enum WizardOutcome {
Close,
/// A credential landed. Args: `(outcome)`. The app closes the wizard and picks a model.
Finished(Box<LoginOutcome>),
/// Put the device code on the clipboard, which the app owns. Args: `(code)`.
CopyCode(String),
/// Key consumed; the wizard stays as it is.
Consumed,
}
@@ -224,6 +226,17 @@ impl AuthWizardState {
self.pending_prompt().is_some()
}
/// The device code the flow is waiting to be entered, while nothing else is being asked.
pub fn device_code(&self) -> Option<&str> {
if self.pending.is_some() {
return None;
}
self.lines().iter().rev().find_map(|line| match line {
WizardLine::DeviceCode { code, .. } => Some(code.as_str()),
_ => None,
})
}
/// Transcript lines of a running (or finished) flow.
pub fn lines(&self) -> &[WizardLine] {
match &self.stage {
@@ -305,10 +318,12 @@ impl AuthWizardState {
}
fn running_key(&mut self, key: &KeyEvent) -> WizardOutcome {
let code = self.device_code().map(str::to_string);
let Some(current) = self.pending.as_mut() else {
// Nothing to answer: Esc aborts the flow, everything else waits.
return match key.code {
KeyCode::Esc => WizardOutcome::Close,
// Nothing to answer: Esc aborts the flow, `c` copies a code to enter, the rest waits
return match (key.code, code) {
(KeyCode::Esc, _) => WizardOutcome::Close,
(KeyCode::Char('c'), Some(code)) => WizardOutcome::CopyCode(code),
_ => WizardOutcome::Consumed,
};
};
@@ -455,6 +470,8 @@ mod tests_support {
}
}
#[cfg(test)]
mod click_tests;
#[cfg(test)]
mod tests;

View File

@@ -0,0 +1,114 @@
#![allow(clippy::unwrap_used)]
use super::tests_support::*;
use super::*;
use crate::mouse::{HitRegions, MouseTarget};
use ratatui::{backend::TestBackend, buffer::Buffer, Terminal};
use std::cell::RefCell;
fn theme() -> crate::theme::styles::ThemeStyles {
let resolved = luxe_core::theme::ThemeRegistry::new().resolve("dark").expect("dark resolves");
crate::theme::styles::ThemeStyles::from_resolved(&resolved, "dark")
}
/// The wizard drawn as `/login` draws it, with what each cell would be hit as.
fn draw(wizard: &AuthWizardState) -> (Buffer, RefCell<HitRegions>) {
let hit = RefCell::new(HitRegions::default());
let mut terminal = Terminal::new(TestBackend::new(90, 32)).unwrap();
terminal.draw(|frame| wizard.view(frame, frame.area(), &theme(), &hit)).unwrap();
(terminal.backend().buffer().clone(), hit)
}
/// Where `needle` is drawn, as `(column, row)`.
fn cell_of(buffer: &Buffer, needle: &str) -> (u16, u16) {
for y in 0..buffer.area.height {
let row: String = (0..buffer.area.width).map(|x| buffer[(x, y)].symbol()).collect();
if let Some(byte) = row.find(needle) {
return (row[..byte].chars().count() as u16, y);
}
}
panic!("{needle} is not drawn");
}
/// A running sign-in asking which GitHub, as Copilot's does.
fn asking_which_github(
) -> (AuthWizardState, tokio::sync::oneshot::Receiver<Result<SecretString, AuthError>>) {
let mut wizard = AuthWizardState::open(providers(), LoginScope::Any);
wizard.on_key(&key(KeyCode::Enter));
let (reply, answer) = tokio::sync::oneshot::channel();
let choice = |id: &str, label: &str, detail: &str| AuthChoice {
id: id.into(),
label: label.into(),
detail: Some(detail.into()),
};
let choices = vec![
choice("github", "GitHub", "github.com"),
choice("enterprise", "GitHub Enterprise", "your company's server"),
];
wizard.apply(AuthUiMsg::Prompt { prompt: AuthPrompt::select("Which GitHub?", choices), reply });
(wizard, answer)
}
/// Each choice of a question the sign-in asks is clickable where it is drawn, its detail line
/// included, as the provider and method lists are. It used to draw without registering anywhere,
/// so a click on "GitHub" did nothing.
#[test]
fn a_question_s_choices_are_clickable_where_they_are_drawn() {
let (wizard, _answer) = asking_which_github();
let (buffer, hit) = draw(&wizard);
let (x, y) = cell_of(&buffer, "GitHub Enterprise");
assert_eq!(hit.borrow().at(x, y), Some(&MouseTarget::AuthMethodRow(1)));
let (x, y) = cell_of(&buffer, "your company's server");
assert_eq!(hit.borrow().at(x, y), Some(&MouseTarget::AuthMethodRow(1)), "the detail line too");
let (x, y) = cell_of(&buffer, "github.com");
assert_eq!(hit.borrow().at(x, y), Some(&MouseTarget::AuthMethodRow(0)));
}
/// A click lands as the keyboard would: the row it hit becomes the answer.
#[tokio::test]
async fn a_clicked_choice_is_the_answer() {
let (mut wizard, answer) = asking_which_github();
wizard.select_method(1);
wizard.on_key(&key(KeyCode::Enter));
let chosen = answer.await.unwrap().unwrap();
assert_eq!(secrecy::ExposeSecret::expose_secret(&chosen), "enterprise");
}
/// A sign-in waiting on a device code offers it to copy, by click or by `c`, and says so.
#[test]
fn a_waiting_device_code_can_be_copied() {
let mut wizard = AuthWizardState::open(providers(), LoginScope::Any);
wizard.on_key(&key(KeyCode::Enter));
wizard.apply(AuthUiMsg::Event(AuthEvent::DeviceCode {
user_code: "WXYZ-9876".into(),
verification_uri: "https://github.com/login/device".into(),
expires_in_secs: 900,
interval_secs: 5,
}));
let (buffer, hit) = draw(&wizard);
let (x, y) = cell_of(&buffer, "Copy code WXYZ-9876");
assert_eq!(hit.borrow().at(x, y), Some(&MouseTarget::AuthCopyCode));
cell_of(&buffer, "c copy code");
match wizard.on_key(&key(KeyCode::Char('c'))) {
WizardOutcome::CopyCode(code) => assert_eq!(code, "WXYZ-9876"),
other => panic!("expected the code to copy, got {other:?}"),
}
}
/// While the flow is asking something, `c` is a letter of the answer, not a copy.
#[test]
fn c_types_into_a_question_rather_than_copying() {
let mut wizard = AuthWizardState::open(providers(), LoginScope::Any);
wizard.on_key(&key(KeyCode::Enter));
wizard.apply(AuthUiMsg::Event(AuthEvent::DeviceCode {
user_code: "WXYZ-9876".into(),
verification_uri: "https://github.com/login/device".into(),
expires_in_secs: 900,
interval_secs: 5,
}));
let (reply, _answer) = tokio::sync::oneshot::channel();
wizard.apply(AuthUiMsg::Prompt { prompt: AuthPrompt::text("Domain:", None), reply });
assert!(matches!(wizard.on_key(&key(KeyCode::Char('c'))), WizardOutcome::Consumed));
assert_eq!(wizard.pending_prompt().map(|(_, typed)| typed), Some("c"));
assert_eq!(wizard.device_code(), None, "no button while a question is up");
}

View File

@@ -79,7 +79,7 @@ impl AuthWizardState {
Stage::Provider { query, .. } => self.render_providers(frame, inner, query, theme, hit),
Stage::Method { .. } => self.render_methods(frame, inner, theme, hit),
Stage::Running { provider, lines } => {
self.render_running(frame, inner, provider, lines, theme)
self.render_running(frame, inner, (provider, lines), theme, hit)
}
Stage::Failed(message) => self.render_failed(frame, inner, message, theme),
}
@@ -93,6 +93,7 @@ impl AuthWizardState {
Stage::Running { .. } => match self.pending_prompt().map(|(prompt, _)| &prompt.kind) {
Some(AuthPromptKind::Select(_)) => "enter confirm · ↑/↓ choose · esc cancel",
Some(_) => "enter confirm · esc cancel",
None if self.device_code().is_some() => "c copy code · esc cancel",
None => "Working… · esc cancel",
},
Stage::Failed(_) => "r try again · esc close",

View File

@@ -3,7 +3,8 @@
use super::*;
impl AuthWizardState {
/// Rows the pending question needs, or zero when nothing is being asked.
/// Rows the pending question needs, the copy-code button's while a code waits to be entered,
/// or zero.
pub(super) fn prompt_height(&self) -> u16 {
match self.pending_prompt() {
// A blank line, the question, and what each choice takes.
@@ -12,6 +13,8 @@ impl AuthWizardState {
// A blank line, the question, and the input row.
_ => 3,
},
// A blank line and the button
None if self.device_code().is_some() => 2,
None => 0,
}
}
@@ -20,9 +23,9 @@ impl AuthWizardState {
&self,
frame: &mut Frame,
inner: Rect,
provider: &str,
lines: &[WizardLine],
(provider, lines): (&str, &[WizardLine]),
theme: &ThemeStyles,
hit: &std::cell::RefCell<HitRegions>,
) {
let prompt = self.pending_prompt();
let prompt_height = self.prompt_height();
@@ -47,6 +50,9 @@ impl AuthWizardState {
let Some((prompt, input)) = prompt else {
park_cursor(frame, inner);
if let Some(code) = self.device_code() {
render_copy_code(frame, chunks[2], code, theme, hit);
}
return;
};
let mut rows: Vec<Line> = vec![
@@ -59,6 +65,7 @@ impl AuthWizardState {
match &prompt.kind {
AuthPromptKind::Select(choices) => {
rows.extend(self.prompt_choice_lines(choices, chunks[2], theme));
publish_choice_rows(choices, chunks[2], hit);
}
kind => rows.push(prompt_input_line(prompt, kind, input, theme)),
}
@@ -124,6 +131,43 @@ impl AuthWizardState {
}
}
/// Makes each choice clickable over every row it draws, its detail line included, under the blank
/// line and the question. A click highlights the row and then answers, as Enter would.
fn publish_choice_rows(
choices: &[luxe_core::auth::AuthChoice],
area: Rect,
hit: &std::cell::RefCell<HitRegions>,
) {
let mut regions = hit.borrow_mut();
let mut y = area.y.saturating_add(2);
for (index, choice) in choices.iter().enumerate() {
let rows = choice_rows(choice).min(area.bottom().saturating_sub(y));
if rows == 0 {
break;
}
regions.push(Rect::new(area.x, y, area.width, rows), MouseTarget::AuthMethodRow(index));
y = y.saturating_add(rows);
}
}
/// The button that copies a device code, under the transcript that shows it: typing the code by
/// hand into a browser is the step most likely to go wrong.
fn render_copy_code(
frame: &mut Frame,
area: Rect,
code: &str,
theme: &ThemeStyles,
hit: &std::cell::RefCell<HitRegions>,
) {
let label = format!(" Copy code {code} ");
let button = Span::styled(label.clone(), theme.accent.patch(theme.selected_bg));
let line = Line::from(vec![button, Span::styled(" or press c", theme.muted)]);
let row = Rect { y: area.y.saturating_add(1), height: 1, ..area };
frame.render_widget(Paragraph::new(line), row);
let width = u16::try_from(UnicodeWidthStr::width(label.as_str())).unwrap_or(row.width);
hit.borrow_mut().push(Rect { width: width.min(row.width), ..row }, MouseTarget::AuthCopyCode);
}
/// The sign-in's transcript so far, its last [`MAX_LINES`] lines.
fn transcript_lines(lines: &[WizardLine], theme: &ThemeStyles) -> Vec<Line<'static>> {
let mut body: Vec<Line<'static>> = Vec::new();

View File

@@ -127,7 +127,6 @@ impl SetupState {
None => {
format!("Follow the desktop ({})", self.desktop.clone().unwrap_or_default())
}
Some(name) if name == "luxe" => "luxe (Luxe's own)".to_string(),
Some(name) => name.clone(),
};
let spans = vec![

View File

@@ -68,15 +68,13 @@ pub(super) fn clicked_target(app: &mut App, target: MouseTarget) -> RouteAction
synthetic_enter()
}
MouseTarget::ThemeRow(index) => theme_row(app, index),
MouseTarget::AuthCopyCode => copy_device_code(app),
// Session/project pickers are route_key-owned (dashboard flow), so a click resolves
// straight to the selection RouteAction rather than a synthetic key.
MouseTarget::SessionRow(index)
if app.current_view == crate::app::ViewMode::SessionPicker =>
{
match app.overlays.session_picker.as_ref().and_then(|p| p.session_id_at(index)) {
Some(id) => RouteAction::SessionSelected(id),
None => RouteAction::None,
}
session_row(app, index)
}
MouseTarget::ReviewDiffRow(row)
if app.current_view == crate::app::ViewMode::GuidedReview =>
@@ -216,6 +214,23 @@ fn configuration_page(app: &mut App, index: usize) -> RouteAction {
}
/// Previews the clicked theme, then accepts it the way Enter would.
/// Copies the sign-in wizard's device code, when it is showing one.
fn copy_device_code(app: &mut App) -> RouteAction {
let code = app.overlays.auth_wizard.as_ref().and_then(|w| w.device_code().map(String::from));
if let Some(code) = code {
app.record_copy(code);
}
RouteAction::None
}
/// Opens the session on the clicked picker row.
fn session_row(app: &App, index: usize) -> RouteAction {
match app.overlays.session_picker.as_ref().and_then(|p| p.session_id_at(index)) {
Some(id) => RouteAction::SessionSelected(id),
None => RouteAction::None,
}
}
fn theme_row(app: &mut App, index: usize) -> RouteAction {
if let Some(name) = app.overlays.theme_picker.as_mut().and_then(|p| p.select(index)) {
app.preview_theme(&name);

View File

@@ -120,8 +120,10 @@ pub enum MouseTarget {
ModelRow(usize),
/// A login-wizard provider row, by its index into the filtered list.
AuthProviderRow(usize),
/// A login-wizard method row, by its index.
/// A login-wizard method row, by its index; also a choice of the question a sign-in is asking.
AuthMethodRow(usize),
/// The login wizard's button that copies the device code a sign-in is waiting on.
AuthCopyCode,
/// A Configuration row — a way in, or a file to open — by its index in whichever is showing.
ConfigRow(usize),
/// A settings page in the list down the left, by its index.

View File

@@ -13,9 +13,9 @@ DashboardMinimal@64x20: frame=939001adda3bcbb4 hits=0f39b6e30d251353
Configuration@140x44: frame=73fbef3e76d36c7c hits=03493dafb9356d07
Configuration@100x30: frame=644345f8e6340004 hits=04d865ae91d0aa35
Configuration@64x20: frame=28ea511ad251f8c2 hits=77b9c8af75245265
Setup@140x44: frame=1ec7d489bf6353bb hits=36aaf8db347382c4
Setup@100x30: frame=4124eb2f29256470 hits=2d10b9f0a226a5e0
Setup@64x20: frame=1c978a2a7cabba3b hits=1b0ef6d44f0cf1bd
Setup@140x44: frame=d61d62a149232446 hits=36aaf8db347382c4
Setup@100x30: frame=1a7c35ff007f3aef hits=2d10b9f0a226a5e0
Setup@64x20: frame=2df799477067503a hits=1b0ef6d44f0cf1bd
SetupTools@140x44: frame=7f80f916b3e06e49 hits=2c443d3127ccc230
SetupTools@100x30: frame=1985e10dacdabc20 hits=ee965f337121049c
SetupTools@64x20: frame=8b216818fd028690 hits=a19d86f6dbe23c71
@@ -251,7 +251,7 @@ login-subscription@100x30: frame=d0d2a22124d02530 hits=57ef1e7d5ac5daaf
login-local@100x30: frame=2e2797e9080feb20 hits=9d3e38903152bd40
login-method@100x30: frame=25f0337c7e66d84b hits=d7190c558c1be1a3
login-secret@100x30: frame=8e5caa0414d23337 hits=b05996f46f500728
login-device@100x30: frame=f57f8bc46fc07b6e hits=b05996f46f500728
login-device@100x30: frame=9678dcaabe255bf8 hits=4f495f0ab9ddfec4
login-failed@100x30: frame=7a68823bb655e435 hits=b05996f46f500728
github@100x30: frame=29b83b4de4d5e45a hits=6ce218f729777fd8
github-detail@100x30: frame=8bff1a38ff99681f hits=6ce218f729777fd8
@@ -273,7 +273,7 @@ login-subscription@64x20: frame=36f5e865860fbdc2 hits=2d2883380c90c475
login-local@64x20: frame=1e111e16e697b7d2 hits=7aff247fdf8c800d
login-method@64x20: frame=c0818a1d2ffab01b hits=d068b54bba55b395
login-secret@64x20: frame=f7cb80562502782e hits=a16a29ea4502d9fb
login-device@64x20: frame=5d3e94107818674f hits=a16a29ea4502d9fb
login-device@64x20: frame=d61ec0dc1534f2a5 hits=44ef8648f0a8302d
login-failed@64x20: frame=8580d73e67e35808 hits=a16a29ea4502d9fb
github@64x20: frame=59c575f5a335deb0 hits=7c38e58fb5f36453
github-detail@64x20: frame=15178bfb7c3e702e hits=7c38e58fb5f36453