Luxe 0.1.3: your sessions on your phone, joinable sessions, and nested AGENTS.md

- luxe remote puts the sessions on your computer in reach of your own phone or tablet over
  Tailscale: write, steer, answer prompts, send photos, browse files, diffs and GitHub, with a
  paired device and your own Tailscale login.
- luxe join lets another program on the same machine join a running session over the RPC protocol.
- An AGENTS.md in a subfolder reaches the model the first time it works there.
- Long sessions stay fast: no stall before each model call, lighter memory, cheaper drawing.
- A session is open in one Luxe at a time, web_search shares results across its phrasings, and a
  grep glob starting with ! excludes.

The CHANGELOG has the full list.
This commit is contained in:
Evan Whittaker
2026-10-20 02:40:13 -05:00
parent 99e30d6d0f
commit d2129a886f
423 changed files with 31666 additions and 1669 deletions

View File

@@ -323,10 +323,12 @@ pub enum ToolActivityState {
/// distinction from `Running` is whose work it is: the model's rather than the tool's, which
/// is why the card's clock does not start until dispatch.
Composing,
/// The call has been written in full but has not started, because something else is running.
/// The call hasn't started because something else comes first: it's written in full and
/// waiting its turn, or still being written while an earlier call runs.
///
/// Tools are dispatched strictly one at a time, so **at most one call is ever `Running`** —
/// and a model writes the calls of one message in order, so at most one is ever `Composing`.
/// and a model writes the calls of one message in order, so at most one is ever `Composing`,
/// and never beside a `Running` one, since a call written while another runs waits behind it.
/// Between them that is the invariant a transcript has to show: exactly one card is working.
/// Drawing every un-finished call as running broke it in the obvious direction (two `bash`
/// cards spinning side by side, which reads as two shells at once); drawing them all as

View File

@@ -37,16 +37,24 @@ pub(crate) const RESUME_BRIEFING: &str = "## Resuming\n";
/// Those are only ever added after what the reader sent, so the first block is theirs whatever it
/// starts with: a pasted hook log is still a message.
pub fn readers_words(content: &[crate::message::ContentBlock]) -> Vec<(usize, String)> {
readers_texts(content.iter().map(|block| match block {
crate::message::ContentBlock::Text(text) => Some(text.as_str()),
_ => None,
}))
}
/// [`readers_words`] over each block's text, `None` for a block without any: for a front end that
/// has a message only as it arrived on the wire, an image's bytes left behind.
pub fn readers_texts<'a>(
blocks: impl IntoIterator<Item = Option<&'a str>>,
) -> Vec<(usize, String)> {
let for_the_model = |text: &str| {
let ours = [HOOK_CONTEXT, BRANCH_NOTE, RESUME_BRIEFING];
crate::mode::note(text).is_some() || ours.iter().any(|opener| text.starts_with(opener))
};
let text = content.iter().enumerate().filter_map(|(index, block)| match block {
crate::message::ContentBlock::Text(text) => Some((index, text)),
_ => None,
});
let text = blocks.into_iter().enumerate().filter_map(|(index, text)| Some((index, text?)));
text.filter(|(index, text)| *index == 0 || !for_the_model(text))
.map(|(index, text)| (index, text.clone()))
.map(|(index, text)| (index, text.to_string()))
.collect()
}

View File

@@ -25,6 +25,7 @@ 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};
pub(crate) use summary::SUMMARIZER_PROMPT;
pub use summary::{format_structured_summary, serialize_for_summary, summary_carries_content};
/// Settings controlling compaction behavior.

View File

@@ -5,6 +5,7 @@ use super::CompactionSettings;
use crate::message::{ContentBlock, Message, ToolOutput};
use crate::provider::ToolSchema;
use crate::session::Entry;
use std::borrow::Borrow;
use tracing::{debug, warn};
/// Estimate the token count for a set of entries.
@@ -15,8 +16,8 @@ use tracing::{debug, warn};
///
/// Still only an estimate. Where the provider has told us the real occupancy, prefer that:
/// [`ContextBudget::observed_tokens`] and [`last_observed_context_tokens`].
pub fn estimate_tokens(entries: &[Entry]) -> u64 {
entries.iter().map(entry_to_token_estimate).sum()
pub fn estimate_tokens<E: Borrow<Entry>>(entries: &[E]) -> u64 {
entries.iter().map(|entry| entry_to_token_estimate(entry.borrow())).sum()
}
/// Estimate the tokens a provider-facing message list occupies.
@@ -100,15 +101,15 @@ fn block_token_estimate(block: &ContentBlock) -> u64 {
///
/// Returns `None` when the transcript has nothing measured to offer, and the caller falls back to
/// [`estimate_tokens`].
pub fn last_observed_context_tokens(entries: &[Entry]) -> Option<u64> {
pub fn last_observed_context_tokens<E: Borrow<Entry>>(entries: &[E]) -> Option<u64> {
last_observed_measurement(entries).map(|(_, occupancy)| occupancy)
}
/// [`last_observed_context_tokens`], plus where in the transcript it was measured, so a caller can
/// estimate only what has been appended since.
fn last_observed_measurement(entries: &[Entry]) -> Option<(usize, u64)> {
fn last_observed_measurement<E: Borrow<Entry>>(entries: &[E]) -> Option<(usize, u64)> {
for (index, entry) in entries.iter().enumerate().rev() {
match &entry.kind {
match &entry.borrow().kind {
crate::session::EntryKind::Compaction { .. } => return None,
crate::session::EntryKind::Usage { source, totals, .. }
if *source == crate::usage::UsageSource::Agent && totals.calls == 1 =>
@@ -130,7 +131,11 @@ fn last_observed_measurement(entries: &[Entry]) -> Option<(usize, u64)> {
/// schemas, plus an estimate of the entries appended since. With nothing measured (a fresh session,
/// or one whose last usage predates a compaction) it estimates [`context_entries`] plus the prefix:
/// what a compaction replaced is not sent, so it is not counted.
pub fn prompt_tokens(entries: &[Entry], system: Option<&str>, tools: &[ToolSchema]) -> u64 {
pub fn prompt_tokens<E: Borrow<Entry>>(
entries: &[E],
system: Option<&str>,
tools: &[ToolSchema],
) -> u64 {
match last_observed_measurement(entries) {
// The measurement already includes the prefix that call was sent with.
Some((index, observed)) => observed + estimate_tokens(&entries[index + 1..]),
@@ -140,7 +145,7 @@ pub fn prompt_tokens(entries: &[Entry], system: Option<&str>, tools: &[ToolSchem
/// Estimated tokens for the conversation a request would carry — [`estimate_tokens`] over
/// [`context_entries`].
pub fn context_tokens(entries: &[Entry]) -> u64 {
pub fn context_tokens<E: Borrow<Entry>>(entries: &[E]) -> u64 {
context_entries(entries).into_iter().map(entry_to_token_estimate).sum()
}
@@ -217,11 +222,12 @@ pub fn clamp_max_tokens_to_context(
/// 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;
/// Room for the six sections over a whole window's worth of transcript *and* for the reasoning a
/// model that always thinks spends first, even asked not to: 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. Generous because only what's written is billed, while a refused
/// summary has already paid for reading the whole window.
pub(crate) const SUMMARY_MAX_TOKENS: u32 = 32_768;
/// 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.

View File

@@ -52,9 +52,9 @@ fn a_summary_gets_room_beyond_what_reasoning_spends() {
..Default::default()
};
// Fails if the allowance drops back to where thinking alone could exhaust it
// Fails if the allowance drops back to the 16K a long session's summary ran out of
let roomy = summary_max_tokens(&model(1_000_000, 128_000), 880_000).expect("a cap");
assert!(roomy >= 16_000, "{roomy}");
assert!(roomy >= 32_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
@@ -302,7 +302,7 @@ fn observed_occupancy_reads_the_last_agent_call_and_stops_at_a_compaction() {
// call measures its own context, not the turn's.
assert_eq!(last_observed_context_tokens(&[usage(UsageSource::Agent, 4, 200_000, 900)]), None);
assert_eq!(last_observed_context_tokens(&[usage(UsageSource::Review, 1, 50_000, 100)]), None);
assert_eq!(last_observed_context_tokens(&[]), None);
assert_eq!(last_observed_context_tokens::<Entry>(&[]), None);
// A call that measured nothing measured nothing — it is not an empty context. Reading it
// as one would report a full window as fresh and hand the next request the whole output

View File

@@ -2,6 +2,7 @@
use crate::message::Message;
use crate::session::Entry;
use std::borrow::Borrow;
/// How a compaction summary introduces itself to the model.
///
@@ -26,15 +27,16 @@ const SUMMARY_SUFFIX: &str = "\n</summary>";
/// what it summarized. That one reads first: it covers history older than the newest summary and
/// everything kept after it. A `first_kept` that isn't on the path (a hand-edited or partially
/// written log) degrades to summary-plus-everything-after rather than dropping the conversation.
pub fn context_entries(entries: &[Entry]) -> Vec<&Entry> {
pub fn context_entries<E: Borrow<Entry>>(entries: &[E]) -> Vec<&Entry> {
let entries: Vec<&Entry> = entries.iter().map(Borrow::borrow).collect();
let Some(cut) = entries
.iter()
.rposition(|entry| matches!(entry.kind, crate::session::EntryKind::Compaction { .. }))
else {
return entries.iter().collect();
return entries;
};
let crate::session::EntryKind::Compaction { first_kept, .. } = &entries[cut].kind else {
return entries.iter().collect();
return entries;
};
let kept_from = entries[..cut].iter().position(|entry| entry.id == *first_kept);
@@ -45,10 +47,10 @@ pub fn context_entries(entries: &[Entry]) -> Vec<&Entry> {
// what it stands for.
let is_summary =
|entry: &&Entry| matches!(entry.kind, crate::session::EntryKind::Compaction { .. });
context.extend(kept.iter().filter(is_summary));
context.push(&entries[cut]);
context.extend(kept.iter().filter(|entry| !is_summary(entry)));
context.extend(entries[cut + 1..].iter());
context.extend(kept.iter().copied().filter(is_summary));
context.push(entries[cut]);
context.extend(kept.iter().copied().filter(|entry| !is_summary(entry)));
context.extend(entries[cut + 1..].iter().copied());
context
}
@@ -57,7 +59,7 @@ pub fn context_entries(entries: &[Entry]) -> Vec<&Entry> {
///
/// The one place a transcript becomes a request, so the cut cannot be applied in some callers and
/// forgotten in others.
pub fn context_messages(entries: &[Entry]) -> Vec<Message> {
pub fn context_messages<E: Borrow<Entry>>(entries: &[E]) -> Vec<Message> {
context_entries(entries)
.into_iter()
.filter_map(|entry| match &entry.kind {

View File

@@ -27,8 +27,8 @@ impl ContextPressure {
///
/// Prefers the provider's own figures: the char/4 estimate reads a prompt of code as about two
/// thirds of its size, and a threshold hung off it fires late.
pub fn measure(
entries: &[Entry],
pub fn measure<E: std::borrow::Borrow<Entry>>(
entries: &[E],
system: Option<&str>,
tools: &[ToolSchema],
usable_window: u64,
@@ -54,10 +54,10 @@ impl ContextPressure {
/// measurement from before the cut describes a context that no longer exists, and count only
/// single-call records, because a batched flush's summed prompt tokens are not an occupancy figure
/// of anything.
fn observed_context_peak(entries: &[Entry]) -> Option<u64> {
fn observed_context_peak<E: std::borrow::Borrow<Entry>>(entries: &[E]) -> Option<u64> {
let mut peak = None;
for entry in entries.iter().rev() {
match &entry.kind {
match &entry.borrow().kind {
crate::session::EntryKind::Compaction { .. } => break,
crate::session::EntryKind::Usage { source, totals, .. }
if *source == crate::usage::UsageSource::Agent && totals.calls == 1 =>
@@ -138,13 +138,23 @@ fn elide_tool_output_before(messages: &mut [Message], boundary: u64) {
.collect();
for (mi, bi, tokens) in stale {
if let ContentBlock::ToolResult { content, .. } = &mut messages[mi].content[bi] {
*content = vec![ToolOutput::Text(format!(
"[{tokens} tokens of earlier tool output elided to save context]"
))];
*content = elided(std::mem::take(content), tokens);
}
}
}
/// A placeholder for `content`, keeping any nested instruction file it carried.
///
/// The saved log still holds the file, so it counts as delivered and won't go again; eliding it
/// here would leave the model without those rules for the rest of the session.
fn elided(content: Vec<ToolOutput>, tokens: u64) -> Vec<ToolOutput> {
let placeholder = format!("[{tokens} tokens of earlier tool output elided to save context]");
let notes = content.into_iter().filter(|output| {
matches!(output, ToolOutput::Text(text) if crate::instructions::nested::noted(text).is_some())
});
std::iter::once(ToolOutput::Text(placeholder)).chain(notes).collect()
}
/// Every non-empty tool result in the list as `(message, block, tokens)`, oldest first.
fn tool_output_tokens(messages: &[Message]) -> impl Iterator<Item = (usize, usize, u64)> + '_ {
messages.iter().enumerate().flat_map(|(mi, msg)| {

View File

@@ -302,3 +302,38 @@ fn the_elision_boundary_moves_in_whole_steps() {
assert_eq!(boundary(1_999), 500, "and it holds there until the next whole step");
assert_eq!(boundary(2_100), 1_000);
}
/// Eliding a stale result keeps the nested instruction file it carried, and only that.
///
/// The break this catches: the file going with the rest of the result. The saved log still holds
/// it, so it counts as delivered and is never sent again, and the model loses those rules.
#[test]
fn eliding_a_result_keeps_the_nested_instructions_it_carried() {
let settings =
CompactionSettings { prune_protect_tokens: 40, prune_min_tokens: 20, ..Default::default() };
let note = crate::instructions::nested::note("web/AGENTS.md", "- WEB-RULE");
let mut messages = [tool_msg("a", 1000, false), tool_msg("b", 100, false)]
.iter()
.map(msg_of)
.collect::<Vec<_>>();
if let ContentBlock::ToolResult { content, .. } = &mut messages[0].content[0] {
content.push(ToolOutput::Text(note.clone()));
content.push(ToolOutput::Text("[instructions: looks like one, isn't".to_string()));
}
prune_tool_outputs(&mut messages, &settings, under_pressure());
let ContentBlock::ToolResult { content, .. } = &messages[0].content[0] else {
panic!("expected a tool result");
};
let texts = texts_of(content);
assert!(texts[0].contains("elided to save context"), "not elided at all");
assert_eq!(texts[1..], [note.as_str()], "the note, and nothing else, kept");
}
/// The text blocks of a tool result.
fn texts_of(content: &[ToolOutput]) -> Vec<&str> {
let texts = content.iter().filter_map(|output| match output {
ToolOutput::Text(text) => Some(text.as_str()),
_ => None,
});
texts.collect()
}

View File

@@ -126,7 +126,7 @@ pub fn serialize_for_summary(entries: &[Entry]) -> String {
}
}
crate::session::EntryKind::Compaction { summary, .. } => {
output.push_str(&format!("[Compaction]: {summary}\n"));
output.push_str(&format!("[Compaction]: {}\n", without_file_list(summary)));
}
_ => {}
}
@@ -134,6 +134,26 @@ pub fn serialize_for_summary(entries: &[Entry]) -> String {
output
}
/// A previous summary as the summarizer reads it: everything but the file list.
///
/// The list is carried forward in code ([`files_listed_in`]) and rewritten on every summary, so
/// showing it to the summarizer only invites a copy: hundreds of lines on a long session, spent from
/// the output allowance the summary itself needs, and thrown away.
fn without_file_list(summary: &str) -> String {
let mut kept = String::new();
let mut inside = false;
for line in summary.lines() {
if let Some(heading) = line.trim().strip_prefix("## ") {
inside = FILE_SECTION_ALIASES.contains(&heading.trim());
}
if !inside {
kept.push_str(line);
kept.push('\n');
}
}
kept.trim_end().to_string()
}
/// Whether an entry puts anything into [`serialize_for_summary`]'s output.
///
/// The mirror of that function's `match`, and the reason it is a predicate rather than a length
@@ -201,6 +221,23 @@ const REQUIRED_SECTIONS: [&str; 6] = [
"Critical Context",
];
/// What the summarizer is told to write.
///
/// 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. No file
/// list is asked for, since one is added from the transcript afterward, and a model asked for one
/// copies hundreds of paths out of the allowance the summary needs. The last sentence is there
/// because a smaller model will otherwise file *these rules* under `Constraints & Preferences` as
/// though they were the session's.
pub(crate) const SUMMARIZER_PROMPT: &str = "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`. Don't list the files touched; that list is kept separately. These \
formatting rules are not part of the session: never describe them in the summary.";
/// Shape the summarizer's prose into the sections a resumed session expects, and append the file
/// record it cannot write for itself.
///

View File

@@ -137,6 +137,66 @@ fn an_earlier_compaction_summary_reaches_the_summarizer() {
assert!(serialized.starts_with("[Compaction]: ## Goal"), "{serialized}");
}
/// The summarizer is asked for every section a summary carries, and for no file list.
///
/// The break this catches: the prompt asking for `## Files` again, which spends the summary's
/// allowance on a list that's replaced anyway, or naming fewer sections than the summary is filed
/// under, which leaves the missing ones empty on every compaction.
#[test]
fn the_summarizer_is_asked_for_every_section_but_the_files() {
for section in REQUIRED_SECTIONS {
assert!(SUMMARIZER_PROMPT.contains(&format!("`## {section}`")), "{section} not asked for");
}
for files in FILE_SECTION_ALIASES {
assert!(!SUMMARIZER_PROMPT.contains(&format!("## {files}")), "{files} asked for");
}
}
fn earlier_summary(summary: &str) -> String {
serialize_for_summary(&[Entry {
id: EntryId::new(),
parent_id: None,
ts: 0,
kind: EntryKind::Compaction {
summary: summary.to_string(),
first_kept: EntryId::new(),
tokens_before: 90_000,
},
}])
}
/// The summarizer reads an earlier summary's prose but not its file list, which code carries.
///
/// The break this catches: showing the list again, which a summarizer copies out path by path,
/// hundreds of them on a long session, until the summary stops short and the compaction fails.
#[test]
fn an_earlier_summarys_file_list_is_kept_from_the_summarizer() {
let read = earlier_summary("## Goal\n\nShip it.\n\n## Files\n\n- src/listed.rs\n");
assert!(read.contains("Ship it."), "{read}");
assert!(!read.contains("src/listed.rs"), "{read}");
}
/// A section after the file list still reaches the summarizer.
///
/// The break this catches: dropping everything from the first file heading on, which loses any
/// section a summary wrote after its list.
#[test]
fn a_section_after_the_file_list_still_reaches_the_summarizer() {
let read = earlier_summary("## Files\n\n- src/listed.rs\n\n## Next Steps\n\nRun the gate.");
assert!(read.contains("Run the gate."), "{read}");
}
/// The two file headings older builds wrote are kept from the summarizer too.
///
/// The break this catches: matching only today's heading, so a session compacted by an older build
/// shows its whole list once more and copies it into the next summary.
#[test]
fn an_older_summarys_file_lists_are_kept_from_the_summarizer() {
let read =
earlier_summary("## Read Files\n\n- src/old.rs\n\n## Modified Files\n\n- src/older.rs");
assert!(!read.contains("src/old"), "{read}");
}
/// The crash this cap used to be: slicing byte 1997 of a block whose 1997th byte is the middle
/// of a character panics, in the middle of a compaction the user never asked for.
#[test]

View File

@@ -78,6 +78,9 @@ pub struct Config {
/// Report the agent's state to herdr when Luxe runs in one of its panes. `None` = use default
/// (on).
pub herdr: Option<bool>,
/// Let another front-end on this machine join a running session. `None` = use default (on).
/// Only this user can connect, and a project file can turn it off but not back on.
pub joinable: Option<bool>,
/// Statusline module order and visibility
pub statusline: StatuslineConfig,
/// Conversation canvas and tool-detail preferences
@@ -188,6 +191,15 @@ impl Config {
self.herdr.unwrap_or(true)
}
/// Whether a running session can be joined from another front-end on this machine (default:
/// yes). `LUXE_JOINABLE=0` turns it off for one launch.
pub fn joinable(&self) -> bool {
let opted_out = std::env::var("LUXE_JOINABLE").is_ok_and(|value| {
matches!(value.trim().to_ascii_lowercase().as_str(), "0" | "false" | "no" | "off")
});
!opted_out && self.joinable.unwrap_or(true)
}
/// Resolves the active theme name, falling back to the current desktop theme when known.
///
/// An explicit Luxe setting always wins. Without one, the desktop's theme is used when the

View File

@@ -110,6 +110,11 @@
"default": true,
"description": "When Luxe runs in a herdr pane, report whether it is working, idle or waiting on you, so herdr's sidebar, notifications and `herdr agent wait` follow it. On by default; nothing is sent outside herdr."
},
"joinable": {
"type": "boolean",
"default": true,
"description": "Let another front-end on this machine join a running session, such as `luxe join` or the `luxe remote` bridge. It goes through a socket (a named pipe on Windows) only your user account can open, and commands the agent runs are turned away. On by default; false turns it off, as does LUXE_JOINABLE=0. A project's config can turn it off but not back on."
},
"diff": {
"type": "object",
"description": "How every surface draws a diff: the edit/write tool card, the preview beside the conversation, and Guided Review. One setting, because the three differ only in how much room they have.",

View File

@@ -11,6 +11,12 @@ impl Config {
luxe_config_dir().join("config.json")
}
/// The user-level config alone, for what belongs to no one project, such as the theme
/// `luxe remote` draws the phone app in. Missing or malformed reads as the defaults.
pub fn load_global() -> Self {
load_global_config().unwrap_or_default()
}
/// Path to the optional custom debug-adapter registry (`debug-adapters.json`).
pub fn debug_adapters_path() -> PathBuf {
luxe_config_dir().join("debug-adapters.json")

View File

@@ -34,6 +34,7 @@ impl Layered for Config {
notification_method,
bell,
herdr,
joinable,
statusline,
conversation,
dock,
@@ -65,6 +66,11 @@ impl Layered for Config {
notification_method: notification_method.or(global.notification_method),
bell: bell.or(global.bell),
herdr: herdr.or(global.herdr),
// A project can close its sessions to other front-ends, never open what the user closed
joinable: match global.joinable {
Some(false) => Some(false),
_ => joinable.or(global.joinable),
},
statusline: Layered::layered(global.statusline, statusline),
conversation: Layered::layered(global.conversation, conversation),
dock: Layered::layered(global.dock, dock),

View File

@@ -674,6 +674,23 @@ fn herdr_reporting_is_on_until_it_is_turned_off() {
assert!(!off.herdr());
}
/// Sessions are joinable until turned off, and a project can turn that off but not back on.
///
/// The break this catches: merging `joinable` like any other key, so a cloned repository's
/// `.luxe/config.json` reopens the socket the user closed.
#[test]
fn a_project_can_close_joining_but_not_reopen_it() {
use super::merge::Layered;
let parse = |json: &str| serde_json::from_str::<Config>(json).unwrap();
assert_eq!(Config::default().joinable, None, "on, by default");
let reopened = Config::layered(parse(r#"{"joinable": false}"#), parse(r#"{"joinable": true}"#));
assert_eq!(reopened.joinable, Some(false), "the user's off stands");
let closed = Config::layered(Config::default(), parse(r#"{"joinable": false}"#));
assert_eq!(closed.joinable, Some(false), "the project's off stands");
let left = Config::layered(parse(r#"{"joinable": true}"#), Config::default());
assert_eq!(left.joinable, Some(true));
}
#[test]
fn ensure_config_file_bootstraps_schema_without_overwriting_config() {
let directory = tempfile::tempdir().unwrap();

View File

@@ -81,6 +81,8 @@ pub struct Engine {
/// Mutable session id — updated when loading/resuming a session so the next turn chains
/// from the resumed state, matching the `--resume` path.
session_id: tokio::sync::Mutex<crate::session::SessionId>,
/// This process's claim on `session_id`, which keeps a second Luxe from writing it too
session_claim: std::sync::Mutex<Option<crate::session::SessionClaim>>,
last_turn_baseline: tokio::sync::Mutex<Option<RetainedTurnBaseline>>,
active_guided_review: Arc<tokio::sync::Mutex<ActiveGuidedReview>>,
/// The one owner of the open review and its feedback session, running beside the engine
@@ -227,6 +229,7 @@ pub use review_desk::{CommentDraft, ReviewEdit};
pub use review_feedback::PULL_REQUEST_FIXES_REFUSED;
mod guided_review;
mod lifecycle;
mod nested_instructions;
mod opening;
mod posting;
mod previews;

View File

@@ -330,6 +330,14 @@ pub enum Action {
/// The review it is for
review_id: ReviewId,
},
/// Answers with the open review and its session, whoever opened it, as `OpenReviewShown`.
///
/// How a second front end joins the review on screen rather than loading its own, which would
/// replace it: a load always makes a new review.
ShowOpenReview {
/// Correlates the answer
request_id: ReviewLoadRequestId,
},
/// Holds one stored review-feedback submission for its turn.
///
/// Sent by the review desk once the submission is stored, never by a front end, which sends
@@ -397,3 +405,16 @@ pub enum Action {
/// Re-run the assistant turn for the current last user message (new branch).
Regenerate,
}
impl Action {
/// Whether the review desk takes this as it comes, mid-turn or not: a send that never waits.
pub(crate) fn goes_to_review_desk(&self) -> bool {
matches!(
self,
Self::EditReview { .. }
| Self::RecordReviewProgress { .. }
| Self::RecordReviewSeen { .. }
| Self::ShowOpenReview { .. }
)
}
}

View File

@@ -32,6 +32,8 @@ impl Engine {
remediation.as_ref().and_then(|context| context.submission_id.clone());
let turn_id = AgentTurnId::new();
debug!(turn_id = %turn_id.as_str(), "starting agent turn");
// First, so a hook refusing the turn below still ends one that had started
let _ = event_tx.send(Event::TurnStarted).await;
*self.last_turn_baseline.lock().await = None;
let retained_baseline = match self.capture_pre_turn_baseline(&turn_id).await {

View File

@@ -52,10 +52,10 @@ impl Engine {
async fn estimated_context_tokens(&self, leaf: &EntryId) -> u64 {
let sid = self.current_session_id().await;
let Ok(store) = self.config.session_store.read() else { return 0 };
store
.active_path(&sid, leaf)
.map(|entries| crate::compaction::context_tokens(&entries))
.unwrap_or(0)
crate::session::with_active_path(&**store, &sid, leaf, |entries| {
crate::compaction::context_tokens(entries)
})
.unwrap_or(0)
}
/// Whether the last measured occupancy is comfortably under the threshold.
@@ -345,10 +345,10 @@ impl Engine {
// What the cut actually bought, measured over the path it produced rather than predicted.
// A client watching for this is watching for the one thing compaction is for.
let tokens_after = self
.sessions()
.active_path(&sid, &compacted)
.map(|path| crate::compaction::context_tokens(&path))
let tokens_after =
crate::session::with_active_path(&**self.sessions(), &sid, &compacted, |path| {
crate::compaction::context_tokens(path)
})
.unwrap_or_default();
// What drafts carried of a conversation went with the transcript
self.sent_digests.lock().await.forget();

View File

@@ -17,22 +17,7 @@ impl Engine {
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.";
let system_prompt = crate::compaction::SUMMARIZER_PROMPT;
// 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.

View File

@@ -57,6 +57,7 @@ impl Engine {
before_mode: tokio::sync::Mutex::new(None),
current_leaf: tokio::sync::Mutex::new(None),
session_id: tokio::sync::Mutex::new(initial_session_id),
session_claim: std::sync::Mutex::new(None),
last_turn_baseline: tokio::sync::Mutex::new(None),
active_guided_review: Arc::new(tokio::sync::Mutex::new(ActiveGuidedReview::default())),
review_desk,

View File

@@ -182,6 +182,8 @@ impl Engine {
}
};
tracing::info!(?reply, waited_ms = asked.elapsed().as_millis(), "permission prompt closed");
// Every front-end showing it takes it down, whichever one answered
let _ = event_tx.send(Event::PromptClosed { id: id.as_str().to_string() }).await;
self.config
.hook_registry

View File

@@ -1,6 +1,9 @@
//! Baseline — the turn snapshots and persisted review a restarted engine picks up again.
use super::*;
use crate::guided_review::{
prune_turn_baselines, LastTurnRecordStoreError, UNREFERENCED_BASELINE_AGE,
};
impl Engine {
/// Restores a complete retained Last Turn baseline without reading the mutable workspace.
@@ -141,10 +144,15 @@ impl Engine {
);
let record = LastTurnRecord::new(&baseline.session_id, baseline.turn_id.as_str())
.map_err(|error| GuidedReviewError::LastTurnRecord(error.to_string()))?;
tokio::task::spawn_blocking(move || LastTurnRecordStore::new(project_root).save(&record))
.await
.map_err(|_| GuidedReviewError::LastTurnRecordTask)?
.map_err(|error| GuidedReviewError::LastTurnRecord(error.to_string()))?;
tokio::task::spawn_blocking(move || {
LastTurnRecordStore::new(&project_root).save(&record)?;
let keep = [record.baseline_id(), record.after_snapshot_id()];
prune_turn_baselines(&project_root, &keep, UNREFERENCED_BASELINE_AGE);
Ok::<_, LastTurnRecordStoreError>(())
})
.await
.map_err(|_| GuidedReviewError::LastTurnRecordTask)?
.map_err(|error| GuidedReviewError::LastTurnRecord(error.to_string()))?;
Ok(())
}
}

View File

@@ -143,11 +143,9 @@ impl Engine {
// A review edit clears the same bar: handing it to the desk is a send that
// never waits. Deferred, every comment written while the agent worked on the
// last batch sat behind the whole turn, and the review refused the next one.
Some(
action @ (Action::EditReview { .. }
| Action::RecordReviewProgress { .. }
| Action::RecordReviewSeen { .. }),
) => self.forward_to_review_desk(action),
Some(action) if action.goes_to_review_desk() => {
self.forward_to_review_desk(action);
}
// Awaits only locks the turn takes and drops at once, so `Cancel` still gets
// through. Deferred, a switch made to stop the agent writing reached it only
// after the turn had written, and the label never moved meanwhile

View File

@@ -6,15 +6,17 @@ impl Engine {
pub(crate) async fn latest_user_goal(&self) -> Option<String> {
let session_id = self.current_session_id().await;
let leaf = self.current_leaf.lock().await.clone()?;
let entries =
self.config.session_store.read().ok()?.active_path(&session_id, &leaf).ok()?;
entries.iter().rev().find_map(|entry| match &entry.kind {
EntryKind::Message(message) if message.role == Role::User => {
let text = message.text();
(!text.trim().is_empty()).then_some(text)
}
_ => None,
})
let store = self.config.session_store.read().ok()?;
let goal = crate::session::with_active_path(&**store, &session_id, &leaf, |entries| {
entries.iter().rev().find_map(|entry| match &entry.kind {
EntryKind::Message(message) if message.role == Role::User => {
let text = message.text();
(!text.trim().is_empty()).then_some(text)
}
_ => None,
})
});
goal.ok()?
}
/// Reads what produced the current changes off the active transcript.
@@ -25,20 +27,17 @@ impl Engine {
const RECENT_ENTRIES: usize = 120;
let session_id = self.current_session_id().await;
let leaf = self.current_leaf.lock().await.clone()?;
let entries =
self.config.session_store.read().ok()?.active_path(&session_id, &leaf).ok()?;
let messages: Vec<crate::message::Message> = entries
.iter()
.rev()
.take(RECENT_ENTRIES)
.collect::<Vec<_>>()
.into_iter()
.rev()
.filter_map(|entry| match &entry.kind {
let store = self.config.session_store.read().ok()?;
let recent = crate::session::with_active_path(&**store, &session_id, &leaf, |entries| {
let from = entries.len().saturating_sub(RECENT_ENTRIES);
let messages = entries[from..].iter().filter_map(|entry| match &entry.kind {
EntryKind::Message(message) => Some(message.clone()),
_ => None,
})
.collect();
});
messages.collect::<Vec<_>>()
});
let messages = recent.ok()?;
drop(store);
let provenance = crate::guided_review::TurnProvenance::from_messages(
self.config.guided_review_project_root.as_deref(),
&messages,
@@ -170,6 +169,13 @@ impl Engine {
})
}
/// Hold the claim on the session just loaded, letting go of the one before it.
fn hold_claim(&self, claim: Option<crate::session::SessionClaim>) {
// Only `load_session` takes it, and never across an await
let mut held = self.session_claim.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
*held = claim;
}
pub(crate) async fn load_session(
&self,
session_id: String,
@@ -177,13 +183,15 @@ impl Engine {
event_tx: mpsc::Sender<Event>,
) -> Result<(), SessionError> {
let id = SessionId(session_id.clone());
let (leaf, messages, used_tokens) = {
let (leaf, messages, used_tokens, claim) = {
let store = self
.config
.session_store
.read()
.map_err(|_| SessionError::Other("session store lock poisoned".to_string()))?;
let entries = store.load(&id)?;
// Before anything changes, so a session open in another Luxe leaves this one as it was
let claim = store.claim(&id)?;
let leaf = crate::session::latest_leaf(&entries);
let active = match leaf.as_ref() {
Some(leaf) => store.active_path(&id, leaf)?,
@@ -205,7 +213,7 @@ impl Engine {
_ => None,
})
.collect::<Vec<_>>();
(leaf, messages, used_tokens)
(leaf, messages, used_tokens, claim)
};
// A session that already carries messages is a resume (CLI `--resume`, `-c`, or the
@@ -221,6 +229,7 @@ impl Engine {
self.retire_session_ledger(self.current_session_id().await);
}
*self.session_id.lock().await = id.clone();
self.hold_claim(claim);
*self.current_leaf.lock().await = leaf;
// Loading a session is how its prompt picks up instruction and skill edits
self.forget_prompt();

View File

@@ -0,0 +1,144 @@
//! Nested instructions, delivered — the engine side of [`crate::instructions::nested`].
//!
//! Runs on a finished batch, just before it commits, so a file rides on the result of the first
//! call that reached its folder and is durable history from then on. Every later request carries
//! it as part of the transcript it already appends to; the system prompt never moves.
use super::*;
use crate::instructions::nested::{self, NestedFile};
use crate::message::ToolOutput;
use std::collections::HashSet;
use std::path::PathBuf;
impl Engine {
/// Appends each nested instruction file the batch reached, and the model doesn't have yet, to
/// the result of the first call that reached it.
///
/// Args: `(leaf, calls, results, event_tx)`. `leaf` is the reply the results answer, so the
/// conversation read is the one these results join.
pub(super) async fn attach_nested_instructions(
&self,
leaf: &EntryId,
calls: &[ToolCallInfo],
results: &mut [ContentBlock],
event_tx: &mpsc::Sender<Event>,
) {
if !nested::enabled() {
return;
}
let wanted = self.nested_wanted(calls, results);
if wanted.is_empty() {
return;
}
let held = self.nested_on_path(leaf).await;
for (call_id, file) in wanted.into_iter().filter(|(_, file)| !held.contains(&file.shown)) {
attach_one(&call_id, &file, results, event_tx).await;
}
}
/// The nested files this batch's successful calls reached, each once, with the call that
/// reached it first.
fn nested_wanted(
&self,
calls: &[ToolCallInfo],
results: &[ContentBlock],
) -> Vec<(String, NestedFile)> {
let launch = self.instruction_dir();
let mut seen: HashSet<PathBuf> = HashSet::new();
let mut wanted = Vec::new();
for call in calls.iter().filter(|call| succeeded(results, &call.id)) {
for file in reached(&launch, call) {
if seen.insert(file.path.clone()) {
wanted.push((call.id.clone(), file));
}
}
}
wanted
}
/// The nested files the conversation up to `leaf` already carries, by the notes that open
/// them. Read rather than remembered, so one a rewind or a compaction took away goes again.
async fn nested_on_path(&self, leaf: &EntryId) -> HashSet<String> {
let sid = self.current_session_id().await;
let read = crate::session::with_active_path(&**self.sessions(), &sid, leaf, |entries| {
crate::compaction::context_messages(entries)
});
match read {
Ok(messages) => notes_in(&messages),
Err(error) => {
// Sending a file twice costs tokens; not sending it costs the rules
warn!(%error, "could not read which nested instructions the model has");
HashSet::new()
}
}
}
}
/// The nested files `call` reached, from Luxe launched at `launch`.
///
/// Relative paths resolve the way the tool resolved them, against where tools run.
fn reached(launch: &std::path::Path, call: &ToolCallInfo) -> Vec<NestedFile> {
let args = parse_tool_arguments(&call.arguments);
let base = ToolContext::working_dir();
let targets = nested::touched_paths(&args);
targets
.into_iter()
.flat_map(|target| nested::nested_files(launch, &base.join(target)))
.collect()
}
/// The output of the result answering `call_id`.
fn output_of<'a>(
results: &'a mut [ContentBlock],
call_id: &str,
) -> Option<&'a mut Vec<ToolOutput>> {
results.iter_mut().find_map(|block| match block {
ContentBlock::ToolResult { call_id: id, content, .. } if id == call_id => Some(content),
_ => None,
})
}
/// Whether the result answering `call_id` ran and succeeded.
fn succeeded(results: &[ContentBlock], call_id: &str) -> bool {
results.iter().any(|block| {
matches!(block, ContentBlock::ToolResult { call_id: id, is_error: false, .. } if id == call_id)
})
}
/// Every nested file named by a note in a tool result of `messages`.
fn notes_in(messages: &[Message]) -> HashSet<String> {
let outputs =
messages.iter().flat_map(|message| &message.content).filter_map(|block| match block {
ContentBlock::ToolResult { content, .. } => Some(content),
_ => None,
});
outputs
.flatten()
.filter_map(|output| match output {
ToolOutput::Text(text) => nested::noted(text).map(str::to_string),
_ => None,
})
.collect()
}
/// Reads `file` and appends it to the result answering `call_id`, telling the person it did.
async fn attach_one(
call_id: &str,
file: &NestedFile,
results: &mut [ContentBlock],
event_tx: &mpsc::Sender<Event>,
) {
let Some(injected) = super::prompt::read_context_file(&file.path) else {
return;
};
let Some(content) = output_of(results, call_id) else {
return;
};
let block = crate::redact::process_secrets().redact(&nested::note(&file.shown, &injected.text));
content.push(ToolOutput::Text(block));
debug!(file = %file.shown, "nested instructions joined the conversation");
let _ = event_tx.send(Event::Notice(format!("Following {}", file.shown))).await;
if let Some(notice) = injected.notice {
let _ = event_tx.send(Event::Notice(notice)).await;
}
}

View File

@@ -3,6 +3,23 @@
use super::*;
impl Engine {
/// Starts whichever project read `action` asks for. Each is synchronous and spawns, so the
/// loop can take it mid-turn too.
pub(crate) fn handle_project_read(&self, action: Action, event_tx: &mpsc::Sender<Event>) {
match action {
Action::LoadFileDiff { request_id, path } => {
self.handle_load_file_diff(request_id, path, event_tx);
}
Action::ListDirectory { request_id, path, include_ignored } => {
self.handle_list_directory(request_id, path, include_ignored, event_tx);
}
Action::ReadProjectFile { request_id, path } => {
self.handle_read_project_file(request_id, path, event_tx);
}
other => debug!(?other, "not a project read"),
}
}
/// Captures one file's change and reports it, without touching review state.
///
/// Detached rather than awaited: a glance must never make the runtime wait on git, and the

View File

@@ -297,11 +297,11 @@ fn detect_git_branch(cwd: &std::path::Path) -> Option<crate::context::GitStatus>
const MAX_CONTEXT_FILE_CHARS: usize = 24_000;
/// One instruction file, as the prompt will carry it.
struct Injected {
pub(super) struct Injected {
/// The text that goes into the prompt.
text: String,
pub(super) text: String,
/// What the person who owns the file should know about it, if anything.
notice: Option<String>,
pub(super) notice: Option<String>,
/// Whether the ceiling cut it, which `/context` reports as its own finding.
truncated: bool,
}
@@ -312,7 +312,7 @@ struct Injected {
/// working from half a file without knowing it is the failure worth preventing. The **user** gets
/// the returned notice, because they are the only one who can fix it — silent truncation is how a
/// document grows to 43,000 characters with nobody noticing that a third of it never arrived.
fn read_context_file(path: &std::path::Path) -> Option<Injected> {
pub(super) fn read_context_file(path: &std::path::Path) -> Option<Injected> {
let content = std::fs::read_to_string(path).ok()?;
let name = path.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default();

View File

@@ -88,14 +88,16 @@ impl Engine {
}
};
let sid = self.current_session_id().await;
let entries = match self.sessions().active_path(&sid, &leaf) {
Ok(entries) => entries,
let read = crate::session::with_active_path(&**self.sessions(), &sid, &leaf, |entries| {
crate::compaction::context_messages(entries)
});
let messages = match read {
Ok(messages) => messages,
Err(error) => {
warn!(%error, "could not read which branch the model last heard of");
return None;
}
};
let messages = crate::compaction::context_messages(&entries);
let users = messages.iter().rev().filter(|message| message.role == Role::User);
users.flat_map(|message| message.content.iter().skip(1).rev()).find_map(|block| match block
{

View File

@@ -62,6 +62,8 @@ pub(crate) enum DeskCommand {
RecordProgress { review_id: ReviewId, progress: ReviewProgress },
/// Remember what the walked chapters covered, so the next review leads with what is new.
RecordSeen { review_id: ReviewId },
/// A front end asked for the open review, answered under its `request_id`.
Show { request_id: ReviewLoadRequestId },
/// A submission crossed into its turn.
Dispatched(SubmissionId),
/// A question's turn answered. Held for the completion it always precedes.
@@ -261,6 +263,7 @@ impl ReviewDesk {
|session: &mut _| mark_posted(session, &finding_ids, &comment, outcome);
self.lifecycle("mark posted", posted).await
}
DeskCommand::Show { request_id } => vec![self.shown(request_id)],
DeskCommand::Snapshot(reply) => {
let open = self.review.clone().zip(self.bound());
let _ = reply.send(open);
@@ -278,6 +281,15 @@ impl ReviewDesk {
}
}
/// The open review and its session as the desk holds them, or neither.
fn shown(&self, request_id: ReviewLoadRequestId) -> Effect {
let (review, session) = self.review.clone().zip(self.bound()).unzip();
let reading = review.as_ref().map(crate::guided_review::chapter_readings);
let reading = reading.unwrap_or_default();
let open = crate::event::OpenReview { request_id, review, session, reading };
Effect::Event(Event::OpenReviewShown(Box::new(open)))
}
/// Puts the Guide's chapters on the held review, when it is still the revision they describe.
///
/// Only ever an upgrade: a pass that wrote no brief must not take one away.

View File

@@ -28,6 +28,7 @@ impl crate::engine::Engine {
DeskCommand::RecordProgress { review_id, progress }
}
Action::RecordReviewSeen { review_id } => DeskCommand::RecordSeen { review_id },
Action::ShowOpenReview { request_id } => DeskCommand::Show { request_id },
other => {
debug!(?other, "not a review desk action");
return;

View File

@@ -135,3 +135,43 @@ async fn a_guide_upgrade_reaches_the_review_the_desk_holds() {
edit(&mut desk, ReviewEdit::Comment { draft: on_chapter, send: false }).await;
assert_eq!(saved_for(&effects, &request_id).findings.len(), 1);
}
/// What `show_open_review` answers, pulled out of the desk's effects.
async fn shown(desk: &mut ReviewDesk) -> crate::event::OpenReview {
let asked = ReviewLoadRequestId::new();
let mut effects = desk.handle(DeskCommand::Show { request_id: asked.clone() }).await;
let Some(Effect::Event(Event::OpenReviewShown(open))) = effects.pop() else {
panic!("show answers with open_review_shown");
};
assert_eq!(open.request_id, asked, "the answer carries the request it answers");
*open
}
/// With no review open, the answer says so rather than staying silent.
///
/// The break this catches: a desk that answers only when a review is open, which leaves a joining
/// client waiting forever.
#[tokio::test]
async fn showing_with_nothing_open_answers_with_neither() {
let mut desk = ReviewDesk::new(None);
let open = shown(&mut desk).await;
assert!(open.review.is_none() && open.session.is_none());
}
/// A joining client gets the open review with the reading done so far, so it continues the
/// review rather than starting over.
///
/// The break this catches: answering with the review but a fresh session, which drops the chapters
/// another client already walked.
#[tokio::test]
async fn showing_carries_the_open_review_and_its_walked_chapters() {
let review = review_from(TWO_FILES);
let (_project, mut desk) = desk_with(&review).await;
let walked = vec![review.chapters[0].id.clone()];
let progress = ReviewProgress { walked_chapter_ids: walked.clone(), ..Default::default() };
desk.handle(DeskCommand::RecordProgress { review_id: review.id.clone(), progress }).await;
let open = shown(&mut desk).await;
assert_eq!(open.reading, crate::guided_review::chapter_readings(&review));
assert_eq!(open.review.map(|it| it.id), Some(review.id));
assert_eq!(open.session.map(|it| it.progress.walked_chapter_ids), Some(walked));
}

View File

@@ -143,15 +143,9 @@ impl Engine {
)
.await;
}
Action::LoadFileDiff { request_id, path } => {
self.handle_load_file_diff(request_id, path, &event_tx);
}
Action::ListDirectory { request_id, path, include_ignored } => {
self.handle_list_directory(request_id, path, include_ignored, &event_tx);
}
Action::ReadProjectFile { request_id, path } => {
self.handle_read_project_file(request_id, path, &event_tx);
}
Action::LoadFileDiff { .. }
| Action::ListDirectory { .. }
| Action::ReadProjectFile { .. } => self.handle_project_read(action, &event_tx),
Action::RefreshGuidedReview { request_id, review_id, expected_revision } => {
self.handle_refresh_guided_review(
request_id,
@@ -186,7 +180,8 @@ impl Engine {
}
Action::EditReview { .. }
| Action::RecordReviewProgress { .. }
| Action::RecordReviewSeen { .. } => self.forward_to_review_desk(action),
| Action::RecordReviewSeen { .. }
| Action::ShowOpenReview { .. } => self.forward_to_review_desk(action),
Action::CommitReviewChapters {
request_id,
review_id,

View File

@@ -31,9 +31,10 @@ impl Engine {
/// retry grows the transcript by one identical message every time it is attempted. That entry
/// is already history, so a re-run's context is not added to it.
///
/// A message Luxe composed — review feedback, a question from a review, a draft's brief — is
/// announced with `MessageCommitted`, because no front-end typed it and so none has shown it.
/// The reader's own words are not: their front-end put them on screen when they were sent.
/// Every committed message is announced with `MessageCommitted`, the reader's own words too:
/// a client joined to the session didn't type them, and the front-end that did reconciles the
/// copy it already shows. A message Luxe composed (review feedback, a draft's brief) has no
/// such copy anywhere, so it's announced the same way.
pub(super) async fn commit_user_message(
&self,
user_message: &str,
@@ -68,16 +69,14 @@ impl Engine {
message.content.extend(context.and_then(hook_context_block));
let entry_id =
self.sessions_mut().append(&sid, parent, EntryKind::Message(message))?;
// Only the brief itself: what a hook added rides along for the model, not the reader
if remediation.is_some() || self.read_only.is_some() {
let _ = event_tx
.send(Event::MessageCommitted {
session_id: sid.to_string(),
entry_id: entry_id.clone(),
message: Message::user(user_message),
})
.await;
}
// Only what was sent: what a hook added rides along for the model, not the reader
let _ = event_tx
.send(Event::MessageCommitted {
session_id: sid.to_string(),
entry_id: entry_id.clone(),
message: user_message_with(user_message, images),
})
.await;
entry_id
}
};
@@ -205,6 +204,7 @@ impl Engine {
None => results.push(self.dispatch_one(call, event_tx).await),
}
}
self.attach_nested_instructions(&leaf, tool_calls, &mut results, event_tx).await;
self.commit_tool_message(leaf, Message { role: Role::Tool, content: results }, event_tx)
.await
}

View File

@@ -16,18 +16,7 @@ impl Engine {
pass: IterationPass,
) -> Result<CompletionRequest, EngineError> {
let sid = self.current_session_id().await;
let entries = self.sessions().active_path(&sid, leaf)?;
// The path holds every entry ever written, including the ones a compaction replaced — it
// is append-only, so the summary is a new entry rather than a deletion. `context_messages`
// applies that cut. Reasoning blocks stay in the history verbatim; each provider adapter
// decides how to replay them (Anthropic must resend signed thinking during a tool-use
// turn; the OpenAI paths drop it). Stripping here would break Anthropic's requirement.
let mut messages = crate::compaction::context_messages(&entries);
// A summary that replaced an `activate_tools` call would unload the tool it loaded on any
// adapter that loads tools in place, so the outgoing copy restates it right after the
// summary. Before the scrub and the pairing repair, which then see an ordinary pair.
crate::tool_activation::restore_after_compaction(&entries, &mut messages);
let activated = crate::tool_activation::activated_in(&entries);
let (mut messages, activated) = self.outgoing_transcript(&sid, leaf)?;
// Two shapes a transcript can reach here in that no provider will take, both of them a
// hard 400 that replays on every later request — so they end the session rather than just
// the turn. Fixed on this copy only, in the order that leaves the result valid: drop what
@@ -67,20 +56,20 @@ impl Engine {
// is, because eliding rewrites the middle of the transcript and re-bills everything after
// it as a cache write — worth paying for runway, never worth paying for tidiness.
let settings = &self.config.compaction_settings;
crate::compaction::prune_tool_outputs(
&mut messages,
settings,
crate::compaction::ContextPressure::measure(
&entries,
system.as_deref(),
&tools,
crate::compaction::usable_context_window(
u64::from(model.context_window),
model.max_tokens,
settings.reserve_tokens,
),
),
let usable = crate::compaction::usable_context_window(
u64::from(model.context_window),
model.max_tokens,
settings.reserve_tokens,
);
let (pressure, prompt_tokens) =
crate::session::with_active_path(&**self.sessions(), &sid, leaf, |entries| {
let system = system.as_deref();
(
crate::compaction::ContextPressure::measure(entries, system, &tools, usable),
crate::compaction::prompt_tokens(entries, system, &tools),
)
})?;
crate::compaction::prune_tool_outputs(&mut messages, settings, pressure);
// What this request spends before a word of conversation. Recorded here because this is
// the only place the system prompt and the resolved tool block exist together, and
// compaction needs it to know how much window the kept tail is really sharing.
@@ -93,7 +82,7 @@ impl Engine {
// endpoint and a silent truncation everywhere else.
let max_tokens = crate::compaction::clamp_max_tokens_to_context(
model.context_window,
crate::compaction::prompt_tokens(&entries, system.as_deref(), &tools),
prompt_tokens,
model.max_tokens,
);
Ok(CompletionRequest {
@@ -114,6 +103,29 @@ impl Engine {
})
}
/// The conversation a request carries, and the deferred tools it has loaded.
///
/// Borrows the path rather than cloning it: it holds every entry ever written, including the
/// ones a compaction replaced (it is append-only, so the summary is a new entry rather than a
/// deletion), and only what `context_messages` keeps after that cut is copied. Reasoning blocks
/// stay verbatim; each provider adapter decides how to replay them (Anthropic must resend
/// signed thinking during a tool-use turn; the OpenAI paths drop it).
fn outgoing_transcript(
&self,
sid: &SessionId,
leaf: &EntryId,
) -> Result<(Vec<Message>, std::collections::BTreeSet<String>), EngineError> {
let read = crate::session::with_active_path(&**self.sessions(), sid, leaf, |entries| {
let mut messages = crate::compaction::context_messages(entries);
// A summary that replaced an `activate_tools` call would unload the tool it loaded on
// an adapter that loads tools in place, so the outgoing copy restates it right after
// the summary. Before the scrub and the pairing repair, which then see an ordinary pair.
crate::tool_activation::restore_after_compaction(entries, &mut messages);
(messages, crate::tool_activation::activated_in(entries))
})?;
Ok(read)
}
/// Opens the provider stream for one iteration, making room and trying once more when the
/// prompt turns out not to fit.
///

View File

@@ -68,6 +68,23 @@ impl Handover {
self.closed.extend(reply.tool_calls.iter().map(|call| call.index));
}
/// Notes the calls `event` shows the model has finished writing. Returns whether it closes any.
fn note_closing(&mut self, event: &ProviderEvent, reply: &StreamedReply) -> bool {
match event {
ProviderEvent::ToolCallEnd { index } => {
self.closed.insert(*index);
true
}
ProviderEvent::ToolCallStart { .. }
| ProviderEvent::TextDelta { .. }
| ProviderEvent::ThinkingDelta { .. } => {
self.close_all(reply);
true
}
_ => false,
}
}
/// Hands over every newly runnable call, in order. Returns whether anything moved.
fn advance(&mut self, reply: &StreamedReply) -> bool {
let Some(to_run) = self.to_run.as_ref() else { return false };
@@ -109,30 +126,46 @@ fn runs_early(call: &ToolCallInfo) -> bool {
/// Anything opening after a call means the model has finished writing it, and so does its own
/// end. Handed over before the next card is announced, and with a yield so the call is running by
/// the time it is: otherwise the finished card flashes "queued" for a frame on its way from being
/// written to running.
async fn close_written_calls(
/// written to running. Each card hears the whole call first.
///
/// Args: `(event, handover, reply, composed, event_tx)`.
async fn close_written(
event: &ProviderEvent,
handover: &mut Handover,
reply: &StreamedReply,
composed: &mut HashMap<usize, Composed>,
event_tx: &mpsc::Sender<Event>,
) {
let closes = match event {
ProviderEvent::ToolCallEnd { index } => {
handover.closed.insert(*index);
true
}
ProviderEvent::ToolCallStart { .. }
| ProviderEvent::TextDelta { .. }
| ProviderEvent::ThinkingDelta { .. } => {
handover.close_all(reply);
true
}
_ => false,
};
if closes && handover.advance(reply) {
if !handover.note_closing(event, reply) {
return;
}
finish_composing(composed, &handover.closed, reply, event_tx).await;
if handover.advance(reply) {
tokio::task::yield_now().await;
}
}
/// Sends each card the model has stopped writing what the cadence held back, and stops watching it.
async fn finish_composing(
composed: &mut HashMap<usize, Composed>,
closed: &std::collections::HashSet<usize>,
reply: &StreamedReply,
event_tx: &mpsc::Sender<Event>,
) {
for call in reply.tool_calls.iter().filter(|call| closed.contains(&call.index)) {
let Some(mut state) = composed.remove(&call.index) else { continue };
let Some(frame) = state.last_frame(&call.name, &call.arguments) else { continue };
let _ = event_tx
.send(Event::ToolCallComposing {
call_id: call.id.clone(),
name: call.name.clone(),
subject: frame.subject,
body: frame.body,
})
.await;
}
}
/// Opens a card for a call the model has started writing, when it is one worth watching arrive.
///
/// A front-end opens the card here rather than at dispatch, so a call the model is still writing
@@ -258,7 +291,7 @@ impl Engine {
let mut composed: HashMap<usize, Composed> = HashMap::new();
while let Some(provider_event) = stream.next().await {
close_written_calls(&provider_event, &mut handover, &reply).await;
close_written(&provider_event, &mut handover, &reply, &mut composed, event_tx).await;
match provider_event {
ProviderEvent::TextDelta { delta, .. } => {
if !text_started {
@@ -368,149 +401,4 @@ impl Engine {
}
#[cfg(test)]
mod tests {
use super::*;
/// Copied from a real gpt-6.1-sol reply that went on like this for 128k tokens.
#[test]
fn a_blank_run_with_line_breaks_runs_away_and_indentation_does_not() {
let stuck = format!("{{\"subjects\":[\"a\"] \t\n \r\n{}", " \n".repeat(140));
assert!(runs_away(&stuck));
assert!(!runs_away(&stuck[..stuck.len() - 200]), "not yet that long");
let pretty = format!("{{\n{}\"n\": 1\n}}", " ".repeat(40));
assert!(!runs_away(&pretty));
// Spaces alone can sit inside a string, where they may be the content
let padded = format!("{{\"text\":\"{}", " ".repeat(1000));
assert!(!runs_away(&padded));
}
fn call(index: usize, name: &str, arguments: &str) -> ToolCallInfo {
ToolCallInfo {
index,
id: format!("call-{index}"),
name: name.into(),
arguments: arguments.into(),
}
}
fn reply(calls: Vec<ToolCallInfo>) -> StreamedReply {
StreamedReply {
text: String::new(),
thinking: HashMap::new(),
order: Vec::new(),
tool_calls: calls,
stop_reason: None,
}
}
/// Reasoning that is only an opaque token still commits, because it is replayed: Copilot's
/// Gemini sends its state with no readable text at all.
#[test]
fn opaque_reasoning_commits_without_text() {
let mut reply = reply(Vec::new());
reply.note_thinking_block(0).opaque = Some("CpcBAb4+9vs".into());
let blocks = reply.into_blocks();
assert!(matches!(
blocks.as_slice(),
[ContentBlock::Thinking { text, opaque: Some(opaque), .. }]
if text.is_empty() && opaque == "CpcBAb4+9vs"
));
}
/// A call is handed over once it is closed, in order; the first call that has to wait makes
/// every later one wait, so nothing runs ahead of a call written before it.
#[test]
fn calls_are_handed_over_in_order_once_closed() {
let (to_run, mut handed) = mpsc::unbounded_channel();
let mut handover = Handover::new(Some(to_run));
let mut reply = reply(vec![call(0, "edit", r#"{"path":"a"}"#)]);
assert!(!handover.advance(&reply), "still being written");
reply.tool_calls.push(call(1, "bash", r#"{"command":"#));
handover.close_all(&reply);
reply.tool_calls[1].arguments.push_str(r#""ls"}"#);
assert!(handover.advance(&reply), "a later block closed both");
assert_eq!(handed.try_recv().map(|call| call.index), Ok(0));
assert_eq!(handed.try_recv().map(|call| call.index), Ok(1));
reply.tool_calls.push(call(2, "read", r#"{"path":"b"}"#));
reply.tool_calls.push(call(3, "test", ""));
handover.close_all(&reply);
assert!(!handover.advance(&reply), "a lookup waits for the message");
assert!(handed.try_recv().is_err(), "and so does the call after it");
}
/// Only a call that is whole, named, and not a lookup runs early.
#[test]
fn what_may_run_early() {
assert!(runs_early(&call(0, "edit", r#"{"path":"a"}"#)));
assert!(runs_early(&call(0, "test", "")), "closed with no arguments takes none");
assert!(!runs_early(&call(0, "edit", r#"{"path":"#)), "cut off mid-argument");
assert!(!runs_early(&call(0, "edit", "[1]")), "arguments are an object");
assert!(!runs_early(&call(0, "grep", r#"{"pattern":"x"}"#)), "a lookup folds");
let mut nameless = call(0, "edit", "{}");
nameless.id.clear();
assert!(!runs_early(&nameless), "half an identity");
}
/// A wrap-up turn hands nothing over, however finished its calls are.
#[test]
fn nothing_is_handed_over_without_somewhere_to_run_it() {
let mut handover = Handover::new(None);
let reply = reply(vec![call(0, "edit", "{}")]);
handover.close_all(&reply);
assert!(!handover.advance(&reply));
}
/// A lookup never composes: three adjacent ones fold into a single line, declared by the
/// committed message, so a card opened early is a row the fold then has to take back.
#[test]
fn only_a_call_worth_watching_arrive_opens_a_card_early() {
for tool in ["write", "edit", "bash", "task", "screenshot"] {
assert!(composes("call-1", tool), "{tool} should open its card early");
}
for tool in ["read", "grep", "glob", "symbol_search", crate::tool::ACTIVATE_TOOLS_NAME] {
assert!(!composes("call-1", tool), "{tool} must wait for dispatch");
}
// Half an identity cannot be correlated with the call that follows it, so it keeps the old
// behavior and gets its card at `ToolStarted`.
assert!(!composes("", "write"));
assert!(!composes("call-1", ""));
}
/// The subject is worth interrupting the cadence for — one small string, sent once, and it is
/// what turns a bare spinner into a card. The payload is resent whole each time, so it waits
/// its turn rather than spending the channel on frames nobody draws.
#[test]
fn the_payload_is_rate_limited_and_the_name_is_not() {
let start = std::time::Instant::now();
let mut state = Composed::default();
let frame = |state: &mut Composed, after_ms: u64, arguments: &str| {
state.next_frame(start + std::time::Duration::from_millis(after_ms), "write", arguments)
};
assert!(frame(&mut state, 0, "{\"path\":\"a.rs").is_none(), "nothing has closed yet");
// The path closes: named at once, carrying whatever of the file has landed — there is no
// cadence to respect until something has actually gone out.
let named =
frame(&mut state, 1, "{\"path\":\"a.rs\",\"content\":\"one\\n").expect("a frame");
assert_eq!(named.subject.as_deref(), Some("a.rs"));
assert_eq!(named.body.map(|body| body.total_lines), Some(1));
// Inside the window the file keeps arriving and the channel stays quiet.
assert!(frame(&mut state, 50, "{\"path\":\"a.rs\",\"content\":\"one\\ntwo\\n").is_none());
let caught_up = frame(&mut state, 200, "{\"path\":\"a.rs\",\"content\":\"one\\ntwo\\n")
.expect("the window has passed");
assert!(caught_up.subject.is_none(), "the name cannot improve, so it is not resent");
assert_eq!(caught_up.body.map(|body| body.total_lines), Some(2), "and it caught up");
// A payload that has not grown is not a frame, however long it has been.
assert!(frame(&mut state, 900, "{\"path\":\"a.rs\",\"content\":\"one\\ntwo\\n").is_none());
let grown =
frame(&mut state, 1_000, "{\"path\":\"a.rs\",\"content\":\"one\\ntwo\\nthree\\n")
.expect("a third line");
assert_eq!(grown.body.map(|body| body.total_lines), Some(3));
}
}
mod tests;

View File

@@ -57,9 +57,11 @@ pub(super) fn composes(id: &str, name: &str) -> bool {
/// What a front-end has already been told about a call the model is still writing.
#[derive(Default)]
pub(super) struct Composed {
/// Whether the subject has been sent. It cannot improve, and a header that rewrote itself as
/// more arrived would read as a glitch rather than as progress.
named: bool,
/// The subject last sent. A later member can improve it: `bash` is named by its command until
/// the `description` written after it arrives, and the card has to carry that before it runs,
/// since dispatch brings no arguments with it. Members only count once whole, so it changes a
/// handful of times at most, never character by character.
named: Option<String>,
/// Bytes and lines of the payload last sent, so a body that has not grown is not resent. Both,
/// because the text is capped and stops growing while the line count it reports does not.
sent: (usize, usize),
@@ -84,24 +86,53 @@ impl Composed {
name: &str,
arguments: &str,
) -> Option<ComposingFrame> {
let cwd = crate::ToolContext::working_dir; // asked only when a frame is built
let subject = if self.named { None } else { describe_partial_call(name, arguments, cwd()) };
// The subject is worth interrupting the cadence for — it is one small string, sent once,
// and it is what turns a bare spinner into a card. The body waits its turn.
let due = self.at.is_none_or(|at| now.duration_since(at) >= COMPOSING_FRAME);
let body = due
.then(|| partial_card_body(name, arguments, cwd()))
.flatten()
.filter(|body| (body.text.len(), body.total_lines) > self.sent);
// The first subject is worth interrupting the cadence for: it is what turns a bare spinner
// into a card. A better one waits its turn with the body, since finding it means parsing
// the whole argument, and on every fragment of a long `write` that adds up.
let frame = self.frame(name, arguments, self.named.is_none() || due, due)?;
self.at = Some(now);
Some(frame)
}
/// Whatever the cadence held back, for a call the model has finished writing.
///
/// Sent before the call is handed over to run, because nothing after that brings its
/// arguments: a `bash` whose `description` closed inside the last window would otherwise run
/// under its command until the message commits, after the call is over.
pub(super) fn last_frame(&mut self, name: &str, arguments: &str) -> Option<ComposingFrame> {
self.frame(name, arguments, true, true)
}
/// The subject and body that changed since they were last sent, looking only where asked.
fn frame(
&mut self,
name: &str,
arguments: &str,
rename: bool,
refill: bool,
) -> Option<ComposingFrame> {
let subject = rename.then(|| self.new_subject(name, arguments)).flatten();
let body = refill.then(|| self.grown_body(name, arguments)).flatten();
if subject.is_none() && body.is_none() {
return None;
}
self.named |= subject.is_some();
if let Some(subject) = &subject {
self.named = Some(subject.clone());
}
if let Some(body) = &body {
self.sent = (body.text.len(), body.total_lines);
}
self.at = Some(now);
Some(ComposingFrame { subject, body })
}
fn new_subject(&self, name: &str, arguments: &str) -> Option<String> {
describe_partial_call(name, arguments, crate::ToolContext::working_dir())
.filter(|subject| self.named.as_ref() != Some(subject))
}
fn grown_body(&self, name: &str, arguments: &str) -> Option<crate::activity::ToolCardBody> {
partial_card_body(name, arguments, crate::ToolContext::working_dir())
.filter(|body| (body.text.len(), body.total_lines) > self.sent)
}
}

View File

@@ -0,0 +1,191 @@
use super::*;
/// Copied from a real gpt-6.1-sol reply that went on like this for 128k tokens.
#[test]
fn a_blank_run_with_line_breaks_runs_away_and_indentation_does_not() {
let stuck = format!("{{\"subjects\":[\"a\"] \t\n \r\n{}", " \n".repeat(140));
assert!(runs_away(&stuck));
assert!(!runs_away(&stuck[..stuck.len() - 200]), "not yet that long");
let pretty = format!("{{\n{}\"n\": 1\n}}", " ".repeat(40));
assert!(!runs_away(&pretty));
// Spaces alone can sit inside a string, where they may be the content
let padded = format!("{{\"text\":\"{}", " ".repeat(1000));
assert!(!runs_away(&padded));
}
fn call(index: usize, name: &str, arguments: &str) -> ToolCallInfo {
ToolCallInfo {
index,
id: format!("call-{index}"),
name: name.into(),
arguments: arguments.into(),
}
}
fn reply(calls: Vec<ToolCallInfo>) -> StreamedReply {
StreamedReply {
text: String::new(),
thinking: HashMap::new(),
order: Vec::new(),
tool_calls: calls,
stop_reason: None,
}
}
/// Reasoning that is only an opaque token still commits, because it is replayed: Copilot's
/// Gemini sends its state with no readable text at all.
#[test]
fn opaque_reasoning_commits_without_text() {
let mut reply = reply(Vec::new());
reply.note_thinking_block(0).opaque = Some("CpcBAb4+9vs".into());
let blocks = reply.into_blocks();
assert!(matches!(
blocks.as_slice(),
[ContentBlock::Thinking { text, opaque: Some(opaque), .. }]
if text.is_empty() && opaque == "CpcBAb4+9vs"
));
}
/// A call is handed over once it is closed, in order; the first call that has to wait makes
/// every later one wait, so nothing runs ahead of a call written before it.
#[test]
fn calls_are_handed_over_in_order_once_closed() {
let (to_run, mut handed) = mpsc::unbounded_channel();
let mut handover = Handover::new(Some(to_run));
let mut reply = reply(vec![call(0, "edit", r#"{"path":"a"}"#)]);
assert!(!handover.advance(&reply), "still being written");
reply.tool_calls.push(call(1, "bash", r#"{"command":"#));
handover.close_all(&reply);
reply.tool_calls[1].arguments.push_str(r#""ls"}"#);
assert!(handover.advance(&reply), "a later block closed both");
assert_eq!(handed.try_recv().map(|call| call.index), Ok(0));
assert_eq!(handed.try_recv().map(|call| call.index), Ok(1));
reply.tool_calls.push(call(2, "read", r#"{"path":"b"}"#));
reply.tool_calls.push(call(3, "test", ""));
handover.close_all(&reply);
assert!(!handover.advance(&reply), "a lookup waits for the message");
assert!(handed.try_recv().is_err(), "and so does the call after it");
}
/// Only a call that is whole, named, and not a lookup runs early.
#[test]
fn what_may_run_early() {
assert!(runs_early(&call(0, "edit", r#"{"path":"a"}"#)));
assert!(runs_early(&call(0, "test", "")), "closed with no arguments takes none");
assert!(!runs_early(&call(0, "edit", r#"{"path":"#)), "cut off mid-argument");
assert!(!runs_early(&call(0, "edit", "[1]")), "arguments are an object");
assert!(!runs_early(&call(0, "grep", r#"{"pattern":"x"}"#)), "a lookup folds");
let mut nameless = call(0, "edit", "{}");
nameless.id.clear();
assert!(!runs_early(&nameless), "half an identity");
}
/// A wrap-up turn hands nothing over, however finished its calls are.
#[test]
fn nothing_is_handed_over_without_somewhere_to_run_it() {
let mut handover = Handover::new(None);
let reply = reply(vec![call(0, "edit", "{}")]);
handover.close_all(&reply);
assert!(!handover.advance(&reply));
}
/// A lookup never composes: three adjacent ones fold into a single line, declared by the
/// committed message, so a card opened early is a row the fold then has to take back.
#[test]
fn only_a_call_worth_watching_arrive_opens_a_card_early() {
for tool in ["write", "edit", "bash", "task", "screenshot"] {
assert!(composes("call-1", tool), "{tool} should open its card early");
}
for tool in ["read", "grep", "glob", "symbol_search", crate::tool::ACTIVATE_TOOLS_NAME] {
assert!(!composes("call-1", tool), "{tool} must wait for dispatch");
}
// Half an identity cannot be correlated with the call that follows it, so it keeps the old
// behavior and gets its card at `ToolStarted`.
assert!(!composes("", "write"));
assert!(!composes("call-1", ""));
}
/// The first subject is worth interrupting the cadence for: one small string, and it is what
/// turns a bare spinner into a card. The payload is resent whole each time, so it waits its turn
/// rather than spending the channel on frames nobody draws.
#[test]
fn the_payload_is_rate_limited_and_the_name_is_not() {
let start = std::time::Instant::now();
let mut state = Composed::default();
let frame = |state: &mut Composed, after_ms: u64, arguments: &str| {
state.next_frame(start + std::time::Duration::from_millis(after_ms), "write", arguments)
};
assert!(frame(&mut state, 0, "{\"path\":\"a.rs").is_none(), "nothing has closed yet");
// The path closes: named at once, carrying whatever of the file has landed — there is no
// cadence to respect until something has actually gone out.
let named = frame(&mut state, 1, "{\"path\":\"a.rs\",\"content\":\"one\\n").expect("a frame");
assert_eq!(named.subject.as_deref(), Some("a.rs"));
assert_eq!(named.body.map(|body| body.total_lines), Some(1));
// Inside the window the file keeps arriving and the channel stays quiet.
assert!(frame(&mut state, 50, "{\"path\":\"a.rs\",\"content\":\"one\\ntwo\\n").is_none());
let caught_up = frame(&mut state, 200, "{\"path\":\"a.rs\",\"content\":\"one\\ntwo\\n")
.expect("the window has passed");
assert!(caught_up.subject.is_none(), "the name has not changed, so it is not resent");
assert_eq!(caught_up.body.map(|body| body.total_lines), Some(2), "and it caught up");
// A payload that has not grown is not a frame, however long it has been.
assert!(frame(&mut state, 900, "{\"path\":\"a.rs\",\"content\":\"one\\ntwo\\n").is_none());
let grown = frame(&mut state, 1_000, "{\"path\":\"a.rs\",\"content\":\"one\\ntwo\\nthree\\n")
.expect("a third line");
assert_eq!(grown.body.map(|body| body.total_lines), Some(3));
}
const BASH_COMMAND: &str = r#"{"command":"sleep 5","#;
const BASH_WHOLE: &str = r#"{"command":"sleep 5","description":"Wait for the build"}"#;
/// A `bash` is named by its command until the `description` written after it arrives, and then
/// by that, so the card running it says what it is for.
///
/// The break this catches: a subject sent once and never again, which leaves the command in the
/// title and again in the body until the call is over.
#[test]
fn a_later_member_renames_the_card() {
let start = std::time::Instant::now();
let mut state = Composed::default();
let at = |ms| start + std::time::Duration::from_millis(ms);
let named = state.next_frame(at(0), "bash", BASH_COMMAND).expect("the command closed");
assert_eq!(named.subject.as_deref(), Some("sleep 5"));
let renamed = state.next_frame(at(200), "bash", BASH_WHOLE).expect("the description closed");
assert_eq!(renamed.subject.as_deref(), Some("Wait for the build"));
assert!(state.next_frame(at(400), "bash", BASH_WHOLE).is_none(), "said once");
}
/// A call closed inside the cadence's window still tells its card the whole call before it is
/// handed over to run, since dispatch brings no arguments with it.
///
/// The break this catches: handing the call over with the frame still held back, so the card
/// runs under its command until the message commits after the call is over.
#[tokio::test]
async fn a_closed_call_tells_its_card_everything_before_it_runs() {
let start = std::time::Instant::now();
let mut composed = HashMap::from([(0, Composed::default())]);
let state = composed.get_mut(&0).expect("just inserted");
state.next_frame(start, "bash", BASH_COMMAND).expect("named by the command");
let late = start + std::time::Duration::from_millis(10);
assert!(state.next_frame(late, "bash", BASH_WHOLE).is_none(), "held back by the cadence");
let (event_tx, mut events) = mpsc::channel(8);
let (to_run, mut handed) = mpsc::unbounded_channel();
let mut handover = Handover::new(Some(to_run));
let reply = reply(vec![call(0, "bash", BASH_WHOLE)]);
let end = ProviderEvent::ToolCallEnd { index: 0 };
close_written(&end, &mut handover, &reply, &mut composed, &event_tx).await;
let Ok(Event::ToolCallComposing { subject, .. }) = events.try_recv() else {
panic!("the card heard nothing before the call was handed over");
};
assert_eq!(subject.as_deref(), Some("Wait for the build"));
assert_eq!(handed.try_recv().map(|call| call.index), Ok(0));
assert!(composed.is_empty(), "a written call is no longer watched");
}

View File

@@ -12,8 +12,8 @@ mod types;
pub use action::Action;
pub use events::Event;
pub use types::{
EngineState, GuidedReviewFailure, GuidedReviewFailureKind, ModelRef, RequestedReviewVerdict,
SessionMessage, StopReason,
EngineState, GuidedReviewFailure, GuidedReviewFailureKind, ModelChoice, ModelRef, OpenReview,
RequestedReviewVerdict, SessionMessage, StopReason,
};
/// Version of the `Action`/`Event` wire protocol, announced in [`Event::Ready`].

View File

@@ -135,6 +135,13 @@ pub enum Action {
/// The open review
review_id: ReviewId,
},
/// Ask for the review open now, whoever opened it, answered by `open_review_shown` under
/// `request_id`. How a second client joins a review instead of replacing it: loading one
/// makes a new review, and the session holds one at a time.
ShowOpenReview {
/// Correlates the answer
request_id: ReviewLoadRequestId,
},
/// Read the open review again, keeping its comments, if it is still at `revision_id`.
RefreshGuidedReview {
/// Correlates the loading, loaded and failed events
@@ -301,6 +308,12 @@ pub enum Action {
/// Project-relative, or an absolute path the session touched outside the project
path: String,
},
/// Ask which models this client can switch to, answered by `models_listed` to this client
/// alone.
ListModels {
/// Correlates the answer
request_id: String,
},
/// List one project folder, answered by `directory_listed` or `directory_list_failed`.
ListDirectory {
/// Correlates the answer
@@ -351,6 +364,23 @@ pub enum Action {
},
// === Lifecycle ===
/// A client that joined a running session names itself, so the interface can say who joined
/// and who answered a prompt. Refused from the client that started the session.
///
/// Args: `name` — how to call it, such as the device the bridge relays for. Cosmetic: any
/// program running as you could send it. `watch_only` — the client only reads, so the
/// interface doesn't say it joined, and nothing it sends afterward reaches the engine. Honored
/// only as the client's first line, so a client can't act and then go quiet.
Introduce {
/// How to call the client
name: String,
/// Reads without being announced, and can't act
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
watch_only: bool,
/// Paired by the person at the terminal, who needn't be told their own device joined
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
yours: bool,
},
/// Shut the engine down, stopping a turn in flight the way `cancel` does. The event stream
/// ends after it.
Close,

View File

@@ -35,6 +35,7 @@ impl Action {
Self::EditReview { .. } => "edit_review",
Self::RecordReviewProgress { .. } => "record_review_progress",
Self::RecordReviewSeen { .. } => "record_review_seen",
Self::ShowOpenReview { .. } => "show_open_review",
Self::RefreshGuidedReview { .. } => "refresh_guided_review",
Self::CommitReviewChapters { .. } => "commit_review_chapters",
Self::PreparePullRequest { .. } => "prepare_pull_request",
@@ -54,6 +55,7 @@ impl Action {
Self::PlanRestack { .. } => "plan_restack",
Self::CheckWorktreeRemoval => "check_worktree_removal",
Self::LoadFileDiff { .. } => "load_file_diff",
Self::ListModels { .. } => "list_models",
Self::ListDirectory { .. } => "list_directory",
Self::ReadProjectFile { .. } => "read_project_file",
Self::SetFocus { .. } => "set_focus",
@@ -61,6 +63,7 @@ impl Action {
Self::PostGithubComment(_) => "post_github_comment",
Self::ChangePullRequest { .. } => "change_pull_request",
Self::Restack { .. } => "restack",
Self::Introduce { .. } => "introduce",
Self::Close => "close",
// The one variant whose tag is not known until runtime, so it reports the category.
// A caller that needs the client's actual string reads it off the variant.
@@ -98,6 +101,7 @@ impl Action {
"edit_review",
"record_review_progress",
"record_review_seen",
"show_open_review",
"refresh_guided_review",
"commit_review_chapters",
"prepare_pull_request",
@@ -117,6 +121,7 @@ impl Action {
"plan_restack",
"check_worktree_removal",
"load_file_diff",
"list_models",
"list_directory",
"read_project_file",
"set_focus",
@@ -124,6 +129,7 @@ impl Action {
"post_github_comment",
"change_pull_request",
"restack",
"introduce",
"close",
];
@@ -175,6 +181,7 @@ impl Action {
| Self::EditReview { .. }
| Self::RecordReviewProgress { .. }
| Self::RecordReviewSeen { .. }
| Self::ShowOpenReview { .. }
| Self::RefreshGuidedReview { .. }
| Self::CommitReviewChapters { .. }
| Self::PreparePullRequest { .. }
@@ -190,10 +197,12 @@ impl Action {
| Self::PlanRestack { .. }
| Self::CheckWorktreeRemoval
| Self::LoadFileDiff { .. }
| Self::ListModels { .. }
| Self::ListDirectory { .. }
| Self::ReadProjectFile { .. }
| Self::SetFocus { .. }
| Self::LinkPullRequest { .. }
| Self::Introduce { .. }
| Self::Close
| Self::Other { .. } => false,
}

View File

@@ -70,6 +70,9 @@ pub(super) enum KnownAction {
RecordReviewSeen {
review_id: ReviewId,
},
ShowOpenReview {
request_id: ReviewLoadRequestId,
},
RefreshGuidedReview {
request_id: ReviewLoadRequestId,
review_id: ReviewId,
@@ -158,6 +161,9 @@ pub(super) enum KnownAction {
request_id: crate::file_diff::FileDiffRequestId,
path: String,
},
ListModels {
request_id: String,
},
ListDirectory {
request_id: crate::project_files::ListingRequestId,
#[serde(default)]
@@ -186,5 +192,12 @@ pub(super) enum KnownAction {
Restack {
request_id: crate::restack::RestackId,
},
Introduce {
name: String,
#[serde(default)]
watch_only: bool,
#[serde(default)]
yours: bool,
},
Close,
}

View File

@@ -79,9 +79,14 @@ pub(super) enum KnownEvent {
messages: Vec<SessionMessage>,
},
StateChanged(EngineState),
TurnStarted,
TurnEnded {
reason: StopReason,
},
ModelsListed {
request_id: String,
models: Vec<super::super::ModelChoice>,
},
LastTurnAvailable {
session_id: String,
turn_id: String,
@@ -121,6 +126,7 @@ pub(super) enum KnownEvent {
},
Error(String),
Notice(String),
GuestsChanged(Vec<String>),
PlanWritten {
path: String,
},
@@ -327,6 +333,7 @@ pub(super) enum KnownEvent {
ReviewSessionRestored {
session: Box<ReviewSession>,
},
OpenReviewShown(Box<super::super::OpenReview>),
ReviewSessionSaved {
request_id: ReviewSessionSaveRequestId,
session: Box<ReviewSession>,
@@ -361,6 +368,9 @@ pub(super) enum KnownEvent {
#[serde(default)]
multi: bool,
},
PromptClosed {
id: String,
},
Ready {
protocol_version: u32,
luxe_version: String,

View File

@@ -53,19 +53,13 @@ pub enum Event {
/// A call can run while the model is still writing the next one, so its result exists well
/// before the batch's results are committed and `ToolEnded` follows. This is what lets a card
/// settle the moment its own call does instead of when the last of the batch does. Optional for
/// a client: one that ignores it settles the card on `ToolEnded`, as before. `output` is the
/// result's text, already redacted. Args: `{call_id, is_error, output}`.
/// a client: one that ignores it settles the card on `ToolEnded`. `output` is the result's
/// text, already redacted. Args: `{call_id, is_error, output}`.
ToolCompleted { call_id: String, is_error: bool, output: String },
/// Tool finished (success or error).
ToolEnded { call_id: String, is_error: bool },
/// A full assistant message was committed to the session.
MessageCommitted {
/// Session which owns the persisted entry.
session_id: String,
/// Persisted entry which owns this message.
entry_id: EntryId,
message: Message,
},
/// A full message, from any role, was committed to the session.
MessageCommitted { session_id: String, entry_id: EntryId, message: Message },
/// A validated remediation report was extracted after its assistant transcript entry committed.
RemediationReported {
/// Persisted assistant entry that carried the named report block.
@@ -74,10 +68,7 @@ pub enum Event {
report: RemediationReport,
},
/// A queued feedback item crossed the engine boundary and can no longer be cancelled.
ReviewFeedbackTurnDispatched {
/// Immutable feedback submission whose agent turn is about to begin.
submission_id: SubmissionId,
},
ReviewFeedbackTurnDispatched { submission_id: SubmissionId },
/// A feedback turn failed before the engine could produce a terminal agent response.
ReviewFeedbackTurnFailed {
/// Persisted feedback submission whose turn failed.
@@ -94,10 +85,7 @@ pub enum Event {
answer: String,
},
/// A feedback turn reached its terminal assistant response and may be marked complete.
ReviewFeedbackTurnCompleted {
/// Persisted feedback submission whose turn completed.
submission_id: SubmissionId,
},
ReviewFeedbackTurnCompleted { submission_id: SubmissionId },
/// A resumed session was loaded with its active-path transcript.
SessionLoaded {
session_id: String,
@@ -106,8 +94,15 @@ pub enum Event {
},
/// Engine state snapshot changed.
StateChanged(EngineState),
/// A turn began, whichever client asked for it. Sent before anything else of the turn, the
/// user's message included, and always followed by `TurnEnded`, so a client that didn't start
/// the turn needn't guess from its first sign.
TurnStarted,
/// The in-flight turn ended.
TurnEnded { reason: StopReason },
/// The models this client can switch to, configured ones first, each with the thinking levels
/// it honors, answering the `list_models` with this `request_id`.
ModelsListed { request_id: String, models: Vec<super::ModelChoice> },
/// A complete retained baseline is available for the latest agent turn.
LastTurnAvailable {
/// Session which owns the persisted baseline.
@@ -125,9 +120,8 @@ pub enum Event {
ContextUsage { used_tokens: u32, context_window: u32 },
/// Token counts + cost for **one provider call**, as it completes.
///
/// Per call, not per turn: a turn that calls tools makes many, and each reports its own usage.
/// Anything that wants a running total should read [`Event::SessionUsage`] rather than trying
/// to reduce this stream — the engine already did the arithmetic.
/// Per call, not per turn: a turn that calls tools makes many, each reporting its own usage.
/// For a running total read [`Event::SessionUsage`], which the engine already summed.
TurnUsage {
prompt_tokens: u64,
completion_tokens: u64,
@@ -164,6 +158,8 @@ pub enum Event {
Error(String),
/// An informational message for the user. Unlike [`Event::Error`] it does not end the turn.
Notice(String),
/// Who's joined this session now, by name, for the terminal to show; sent as one comes or goes.
GuestsChanged(Vec<String>),
/// Plan mode wrote one of its plan files. Args: `path`, project-relative.
///
/// What a front-end offers the plan's next step on, once the turn that wrote it ends.
@@ -195,9 +191,8 @@ pub enum Event {
},
/// A richer Guide replaced the structural chapters a review opened with.
///
/// Chaptering is advisory and can involve several model passes, so it must never hold the diff
/// hostage: the review opens immediately with deterministic sections and upgrades in place when
/// (and only when) the pinned revision it describes is still the one on screen.
/// Chaptering is advisory and may take several model passes, so the review opens at once with
/// deterministic sections and upgrades in place only while its pinned revision is on screen.
GuidedReviewGuideUpdated {
/// Correlates this Guide with the request whose review it describes.
request_id: ReviewLoadRequestId,
@@ -511,19 +506,18 @@ pub enum Event {
/// Why not.
error: crate::project_files::ProjectFileFailure,
},
/// The feedback session bound to the review just loaded, which always follows
/// [`Event::GuidedReviewLoaded`]: carried over a refresh and moved onto its new source,
/// restored from disk after a restart, or new. Also sent when another writer's copy replaced
/// this one.
/// The session bound to the review just loaded, always after [`Event::GuidedReviewLoaded`]:
/// carried over a refresh onto its new source, restored after a restart, or new. Also sent when
/// another writer's copy replaced this one.
ReviewSessionRestored {
/// Complete authoritative state, as the review desk holds it.
session: Box<ReviewSession>,
},
/// The open review's feedback session changed and was stored.
///
/// Every change arrives this way, whoever made it: an `EditReview`, answered under its
/// `request_id`, or something Luxe did itself (a turn dispatched, a report recorded, a review
/// posted), under a fresh one.
/// The answer to `show_open_review`. Boxed: it carries a whole review.
OpenReviewShown(Box<super::types::OpenReview>),
/// The open review's feedback session changed and was stored. Every change arrives this way,
/// whoever made it: an `EditReview`, answered under its `request_id`, or something Luxe did
/// itself (a turn dispatched, a report recorded, a review posted), under a fresh one.
ReviewSessionSaved {
/// The edit this answers, or a fresh id for a change Luxe made itself.
request_id: ReviewSessionSaveRequestId,
@@ -585,11 +579,10 @@ pub enum Event {
/// Whether multiple options may be selected.
multi: bool,
},
/// First frame of an RPC stream: which protocol the peer is speaking, and to what.
///
/// Emitted by `luxe --rpc` before anything else, so a client checks one integer and refuses a
/// build it cannot speak to. Additive changes do not move `protocol_version`; removing or
/// retagging anything does. `docs/guide/rpc.md`.
/// A prompt stopped waiting, answered by any front-end or cancelled; the others take it down.
PromptClosed { id: String },
/// First frame of an RPC stream, from `luxe --rpc` or a joined session: protocol and session.
/// Additive changes do not move `protocol_version`; removing or retagging anything does.
Ready {
/// Wire protocol version — [`crate::PROTOCOL_VERSION`].
protocol_version: u32,

View File

@@ -112,6 +112,7 @@ fn sample_actions() -> Vec<Action> {
walked_chapter_ids: vec![id("chapter:1")],
},
Action::RecordReviewSeen { review_id: id("review:1") },
Action::ShowOpenReview { request_id: id("review-load:3") },
Action::RefreshGuidedReview {
request_id: id("review-load:2"),
review_id: id("review:1"),
@@ -183,6 +184,7 @@ fn sample_actions() -> Vec<Action> {
Action::PlanRestack { request_id: id("restack:1") },
Action::CheckWorktreeRemoval,
Action::LoadFileDiff { request_id: id("file-diff:1"), path: "src/main.rs".into() },
Action::ListModels { request_id: "models:1".into() },
Action::ListDirectory {
request_id: id("listing:1"),
path: "src".into(),
@@ -208,10 +210,39 @@ fn sample_actions() -> Vec<Action> {
}),
},
Action::Restack { request_id: id("restack:1") },
Action::Introduce { name: "iPhone".into(), watch_only: false, yours: false },
Action::Close,
]
}
/// A watch-only introduction says so on the wire, under the name the guide gives.
///
/// The break this catches: renaming `watch_only`, so a bridge built against one name joins a host
/// built against the other as an ordinary, announced client.
#[test]
fn a_watch_only_introduction_keeps_its_wire_name() {
const PINNED: &str =
r#"{"kind":"introduce","content":{"name":"Notifications","watch_only":true}}"#;
let watcher =
Action::Introduce { name: "Notifications".into(), watch_only: true, yours: false };
assert_eq!(serde_json::to_string(&watcher).unwrap(), PINNED);
assert_eq!(serde_json::from_str::<Action>(PINNED).unwrap(), watcher);
}
/// The owner's own device says so on the wire, and an introduction without the field is someone
/// else's, as every bridge before it sent.
///
/// The break this catches: renaming `yours`, so a host never knows a device is its owner's and
/// announces every reconnect, or a missing field read as yours, quieting a stranger's join.
#[test]
fn an_owners_introduction_keeps_its_wire_name() {
const PINNED: &str = r#"{"kind":"introduce","content":{"name":"iPhone","yours":true}}"#;
let device = Action::Introduce { name: "iPhone".into(), watch_only: false, yours: true };
assert_eq!(serde_json::to_string(&device).unwrap(), PINNED);
let unsaid = r#"{"kind":"introduce","content":{"name":"iPhone"}}"#;
assert!(matches!(serde_json::from_str(unsaid), Ok(Action::Introduce { yours: false, .. })));
}
fn section() -> crate::config::GithubSection {
crate::config::GithubSection {
name: "Mine".into(),
@@ -253,6 +284,7 @@ fn the_wire_action_vocabulary_is_pinned() {
r#"{"kind":"edit_review","content":{"request_id":"review-save:1","review_id":"review:1","edit":"comment","anchor":{"kind":"line","file_id":"file:1","hunk_id":"hunk:1","side":"new","line":12,"context_hash":""},"severity":"question","title":"","body":"Why does this retry twice?","proposed_fix":null,"send":true}}"#,
r#"{"kind":"record_review_progress","content":{"review_id":"review:1","walked_chapter_ids":["chapter:1"]}}"#,
r#"{"kind":"record_review_seen","content":{"review_id":"review:1"}}"#,
r#"{"kind":"show_open_review","content":{"request_id":"review-load:3"}}"#,
r#"{"kind":"refresh_guided_review","content":{"request_id":"review-load:2","review_id":"review:1","revision_id":"revision:1"}}"#,
r#"{"kind":"commit_review_chapters","content":{"request_id":"review-save:2","review_id":"review:1","revision_id":"revision:1","squash":true}}"#,
r#"{"kind":"prepare_pull_request","content":{"request_id":"pr-open:1","review_id":"review:1"}}"#,
@@ -272,6 +304,7 @@ fn the_wire_action_vocabulary_is_pinned() {
r#"{"kind":"plan_restack","content":{"request_id":"restack:1"}}"#,
r#"{"kind":"check_worktree_removal"}"#,
r#"{"kind":"load_file_diff","content":{"request_id":"file-diff:1","path":"src/main.rs"}}"#,
r#"{"kind":"list_models","content":{"request_id":"models:1"}}"#,
r#"{"kind":"list_directory","content":{"request_id":"listing:1","path":"src"}}"#,
r#"{"kind":"read_project_file","content":{"request_id":"file-read:1","path":"src/main.rs"}}"#,
r#"{"kind":"set_focus","content":{"focused":false}}"#,
@@ -279,6 +312,7 @@ fn the_wire_action_vocabulary_is_pinned() {
r#"{"kind":"post_github_comment","content":{"repository":null,"number":142,"thread":"PRRT_1","answering":null,"body":"Fixed in the latest push."}}"#,
r#"{"kind":"change_pull_request","content":{"number":142,"write":{"change":"merge","method":"squash","head_oid":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","delete_branch":"feat/threads"}}}"#,
r#"{"kind":"restack","content":{"request_id":"restack:1"}}"#,
r#"{"kind":"introduce","content":{"name":"iPhone"}}"#,
r#"{"kind":"close"}"#,
];
let samples = sample_actions();

View File

@@ -40,7 +40,9 @@ published! {
ReviewFeedbackTurnCompleted => "review_feedback_turn_completed",
SessionLoaded => "session_loaded",
StateChanged => "state_changed",
TurnStarted => "turn_started",
TurnEnded => "turn_ended",
ModelsListed => "models_listed",
LastTurnAvailable => "last_turn_available",
CompactionStarted => "compaction_started",
CompactionDone => "compaction_done",
@@ -53,6 +55,7 @@ published! {
ImageAttached => "image_attached",
Error => "error",
Notice => "notice",
GuestsChanged => "guests_changed",
PlanWritten => "plan_written",
ReviewStarted => "review_started",
ReviewFinished => "review_finished",
@@ -94,6 +97,7 @@ published! {
ProjectFileRead => "project_file_read",
ProjectFileReadFailed => "project_file_read_failed",
ReviewSessionRestored => "review_session_restored",
OpenReviewShown => "open_review_shown",
ReviewSessionSaved => "review_session_saved",
ReviewSessionSaveFailed => "review_session_save_failed",
SessionsRefreshed => "sessions_refreshed",
@@ -103,6 +107,7 @@ published! {
ToolActivityUpdated => "tool_activity_updated",
PermissionRequested => "permission_requested",
QuestionAsked => "question_asked",
PromptClosed => "prompt_closed",
Ready => "ready",
ActionRejected => "action_rejected",
}
@@ -227,7 +232,9 @@ pub(super) fn every_event() -> Vec<Event> {
Event::ReviewFeedbackTurnCompleted { submission_id: SubmissionId::new() },
Event::SessionLoaded { session_id: s("s1"), messages: Vec::new() },
Event::StateChanged(EngineState::default()),
Event::TurnStarted,
Event::TurnEnded { reason: StopReason::EndTurn },
Event::ModelsListed { request_id: s("models:1"), models: Vec::new() },
Event::LastTurnAvailable { session_id: s("s1"), turn_id: s("t1") },
Event::CompactionStarted { tokens_before: 2 },
Event::CompactionDone { tokens_before: 2, tokens_after: 1 },
@@ -248,6 +255,7 @@ pub(super) fn every_event() -> Vec<Event> {
Event::ImageAttached { mime: s("image/png"), width: 16, height: 9 },
Event::Error(s("lost")),
Event::Notice(s("heads up")),
Event::GuestsChanged(vec![s("iPhone")]),
Event::PlanWritten { path: s("PLAN.md") },
Event::ReviewStarted { model: s("m") },
Event::ReviewFinished { verdict: RequestedReviewVerdict::Fix, findings: vec![s("f")] },
@@ -415,6 +423,12 @@ pub(super) fn every_event() -> Vec<Event> {
error: crate::project_files::ProjectFileFailure::NotFound,
},
Event::ReviewSessionRestored { session: session() },
Event::OpenReviewShown(Box::new(crate::event::OpenReview {
request_id: ReviewLoadRequestId::new(),
review: Some(review()),
session: Some(*session()),
reading: Vec::new(),
})),
Event::ReviewSessionSaved {
request_id: ReviewSessionSaveRequestId::new(),
session: session(),
@@ -444,6 +458,7 @@ pub(super) fn every_event() -> Vec<Event> {
options: Vec::new(),
multi: true,
},
Event::PromptClosed { id: s("perm:1") },
Event::Ready {
protocol_version: 1,
luxe_version: s("0.1.0"),

View File

@@ -44,6 +44,20 @@ impl ModelRef {
}
}
/// One model a client can switch to, as `list_models` answers. Carries no endpoint: a base URL can
/// hold credentials, and switching needs only the two ids.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ModelChoice {
/// Provider id, as `select_model` takes it
pub provider: String,
/// Model id, as `select_model` takes it
pub model: String,
/// The name to show
pub name: String,
/// The thinking levels it honors, lowest first: only `off` for a model that doesn't reason
pub thinking_levels: Vec<crate::provider::ThinkingLevel>,
}
/// Stop reason from a completed LLM turn.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
@@ -162,3 +176,17 @@ pub enum GuidedReviewFailureKind {
/// Local capture, parsing, or persistence failed.
Capture,
}
/// The answer to `show_open_review`: the review open now and its session, or neither.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct OpenReview {
/// Correlates this answer with its request
pub request_id: crate::guided_review::ReviewLoadRequestId,
/// The open review, with the latest guide applied
pub review: Option<crate::guided_review::GuidedReview>,
/// Its feedback session as the desk holds it
pub session: Option<crate::guided_review::ReviewSession>,
/// What each chapter asks of the reader, decided from lines a client need not keep
#[serde(default)]
pub reading: Vec<crate::guided_review::ChapterReading>,
}

View File

@@ -42,7 +42,8 @@ mod untracked;
pub use attention::{review_attention, review_flags, AttentionKind, AttentionSignal};
pub use budget::{
chapter_needs_review, review_budget, Accounted, ChapterBudget, ItemVerdict, ReviewBudget,
chapter_needs_review, chapter_readings, review_budget, shared_reason, Accounted, ChapterBudget,
ChapterReading, ItemVerdict, ReviewBudget,
};
pub use chaptering::{
fallback_chapters, fallback_groups, source_items, validate_chapter_coverage,
@@ -140,6 +141,7 @@ pub use source_store::{
SOURCE_PAYLOAD_FILE,
};
pub use thread_findings::{thread_findings, ThreadAnswer, ThreadAnswerResult, ThreadReply};
pub(crate) use turn_baseline::{prune_turn_baselines, UNREFERENCED_BASELINE_AGE};
pub use turn_baseline::{
TurnBaselineArtifact, TurnBaselineExclusion, TurnBaselineFile, TurnBaselineManifest,
TurnBaselineManifestError, TurnBaselineStoreError, TURN_BASELINE_CACHE_RELATIVE_DIR,

View File

@@ -197,6 +197,46 @@ pub fn chapter_needs_review(budget: &ReviewBudget, chapter: &ReviewChapter) -> b
budget.chapter(&chapter.id).needs_review > 0
}
/// The one reason every item in `chapter` is accounted for: `None` when anything in it needs the
/// reviewer, or when different things vouch for different items.
pub fn shared_reason(budget: &ReviewBudget, chapter: &ReviewChapter) -> Option<Accounted> {
let mut reason: Option<Accounted> = None;
for item in &chapter.items {
let ItemVerdict::Accounted(accounted) = budget.verdict(item) else {
return None;
};
match &reason {
Some(seen) if *seen != accounted => return None,
_ => reason = Some(accounted),
}
}
reason
}
/// What one chapter asks of the reader, for a front end that has the chapters but not the lines
/// the budget is decided from.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ChapterReading {
/// The chapter it's about
pub chapter_id: ChapterId,
/// Items still on the reviewer's list
pub needs_review: usize,
/// The label of [`shared_reason`], when there is one
#[serde(default, skip_serializing_if = "Option::is_none")]
pub reason: Option<String>,
}
/// What every chapter of `review` asks of the reader, from the budget the terminal reads.
pub fn chapter_readings(review: &GuidedReview) -> Vec<ChapterReading> {
let budget = review_budget(review, &super::review_flags(review));
let reading = |chapter: &ReviewChapter| ChapterReading {
chapter_id: chapter.id.clone(),
needs_review: budget.chapter(&chapter.id).needs_review,
reason: shared_reason(&budget, chapter).map(|reason| reason.label()),
};
review.chapters.iter().map(reading).collect()
}
fn hunk_verdict(
hunk: &ReviewHunk,
file: &ReviewFile,

View File

@@ -249,3 +249,77 @@ fn chapter_roll_up_counts_what_still_needs_review() {
assert!(chapter_needs_review(&budget, chapter));
assert_eq!(budget.chapter(&chapter.id).needs_review, 1);
}
/// The churn review as one chapter holding all three hunks, so a reason has to be shared to stand.
fn one_churn_chapter() -> GuidedReview {
let mut review = churn_review();
let mut chapter = review.chapters[0].clone();
chapter.items = review.chapters.iter().flat_map(|it| it.items.clone()).collect();
review.chapters = vec![chapter];
review
}
/// One reason behind every item is the chapter's reason.
///
/// The break this catches: a chapter that reads as needing nothing but never says why, which is
/// the line a reader checks before skipping it.
#[test]
fn a_reason_every_item_shares_is_the_chapters_reason() {
let review = one_churn_chapter();
let budget = review_budget(&review, &[]);
let reason = shared_reason(&budget, &review.chapters[0]);
assert_eq!(reason, Some(Accounted::Noise { class: NoiseKind::FormattingOnly }));
}
/// Different reasons for different items name none of them.
///
/// The break this catches: the first item's reason standing for the whole chapter, which calls a
/// lockfile change formatting.
#[test]
fn mixed_reasons_name_none() {
let mut review = one_churn_chapter();
review.files[1].hunks[0].noise = NoiseKind::Lockfile;
let budget = review_budget(&review, &[]);
assert_eq!(shared_reason(&budget, &review.chapters[0]), None);
}
/// One item on the reading list means the chapter has no reason to be skipped.
///
/// The break this catches: skipping the item that needs review when its neighbors are churn,
/// which is exactly the hunk a reviewer is there for.
#[test]
fn an_item_that_needs_review_leaves_no_reason() {
let review = one_churn_chapter();
let flagged = signal(None, Some(review.files[1].hunks[0].id.clone()), false);
let budget = review_budget(&review, &[flagged]);
assert_eq!(shared_reason(&budget, &review.chapters[0]), None);
}
/// A chapter's reading carries its id, what still needs review, and the shared reason's words.
///
/// The break this catches: a reading that reaches a phone without the count or the words, so it
/// shows every chapter as one to read, or none.
#[test]
fn readings_carry_the_count_and_the_reasons_words() {
let churn = one_churn_chapter();
let [quiet] = chapter_readings(&churn).try_into().expect("one chapter");
assert_eq!(quiet.chapter_id, churn.chapters[0].id);
assert_eq!((quiet.needs_review, quiet.reason.as_deref()), (0, Some("formatting only")));
let [unvouched] = chapter_readings(&review()).try_into().expect("one chapter");
assert_eq!((unvouched.needs_review, unvouched.reason), (1, None));
}
/// A reading weighs what the lines say, as the terminal's budget does.
///
/// The break this catches: readings decided without the line flags, which would tell a phone that
/// a chapter adding a credential is only formatting.
#[test]
fn readings_weigh_what_the_lines_say() {
let mut review = one_churn_chapter();
// Built here so no key-shaped literal sits in the source for the scanners to trip on
let key = format!("AKIA{}", "Q7ZK4M2TPLX9WRB6");
let added = review.files[1].hunks[0].lines.iter_mut().find(|line| line.new_line.is_some());
added.expect("an added line").text = format!("let key = \"{key}\";");
let [flagged] = chapter_readings(&review).try_into().expect("one chapter");
assert_eq!((flagged.needs_review, flagged.reason), (1, None));
}

View File

@@ -116,13 +116,7 @@ impl LocalGit {
let snapshot = self
.capture_turn_snapshot(TurnBaselineId::from_session_turn(session_id, turn_id), policy)
.await?;
let artifact = snapshot.artifact;
let to_persist = artifact.clone();
let project_root = self.repo_path.clone();
tokio::task::spawn_blocking(move || to_persist.persist(&project_root))
.await
.map_err(|_| GuidedReviewError::TurnBaselinePersistenceTask)??;
Ok(artifact)
self.persist_turn_snapshot(snapshot.artifact).await
}
/// Captures and retains the complete workspace immediately after a turn without changing Git state.
@@ -138,13 +132,21 @@ impl LocalGit {
policy,
)
.await?;
let artifact = snapshot.artifact;
let to_persist = artifact.clone();
self.persist_turn_snapshot(snapshot.artifact).await
}
/// Writes a snapshot off the runtime and hands it back. Moved rather than cloned, since a
/// snapshot holds every file in the workspace and a copy doubled what a turn held at once.
async fn persist_turn_snapshot(
&self,
artifact: TurnBaselineArtifact,
) -> Result<TurnBaselineArtifact, GuidedReviewError> {
let project_root = self.repo_path.clone();
tokio::task::spawn_blocking(move || to_persist.persist(&project_root))
.await
.map_err(|_| GuidedReviewError::TurnBaselinePersistenceTask)??;
Ok(artifact)
let persisted =
tokio::task::spawn_blocking(move || artifact.persist(&project_root).map(|_| artifact))
.await
.map_err(|_| GuidedReviewError::TurnBaselinePersistenceTask)??;
Ok(persisted)
}
}

View File

@@ -303,9 +303,10 @@ impl TurnBaselineArtifact {
})
};
}
let earlier = retention::newest_payload_dir(&root);
let staging = root.join(format!(".staging-{}", Uuid::new_v4()));
create_private_dir(&staging)?;
let result = self.write_staging(&staging).and_then(|()| {
let result = self.write_staging(&staging, earlier.as_deref()).and_then(|()| {
fs::rename(&staging, &target).map_err(|source| TurnBaselineStoreError::Io {
operation: "commit",
path: target.clone(),
@@ -403,7 +404,11 @@ impl TurnBaselineArtifact {
Ok(())
}
fn write_staging(&self, staging: &Path) -> Result<(), TurnBaselineStoreError> {
fn write_staging(
&self,
staging: &Path,
earlier: Option<&Path>,
) -> Result<(), TurnBaselineStoreError> {
let payload_dir = staging.join(TURN_BASELINE_PAYLOAD_DIR);
create_private_dir(&payload_dir)?;
for (path, bytes) in &self.payloads {
@@ -421,9 +426,9 @@ impl TurnBaselineArtifact {
reason: "payload metadata differs",
});
}
let payload_path = payload_dir.join(digest);
let payload_path = payload_dir.join(&digest);
if !payload_path.exists() {
write_private_file(&payload_path, bytes)?;
retention::place_payload(&payload_path, earlier, &digest, bytes)?;
}
}
let manifest = serde_json::to_vec(&self.manifest)?;
@@ -486,5 +491,8 @@ fn is_sha256_hex(value: &str) -> bool {
value.len() == 64 && value.bytes().all(|byte| byte.is_ascii_hexdigit())
}
mod retention;
pub(crate) use retention::{prune_turn_baselines, UNREFERENCED_BASELINE_AGE};
#[cfg(test)]
mod tests;

View File

@@ -0,0 +1,101 @@
//! Retention — what a new turn baseline shares with the last one, and which old ones go.
//!
//! Every agent turn captures the whole workspace twice, and few files change between turns, so a
//! baseline hard-links each payload the newest earlier one already holds instead of writing and
//! syncing it again. Only the last turn's pair is ever read back, so the rest are deleted once
//! they're an hour old; the hour spares a turn that another session in this project is still in.
use super::{TurnBaselineId, TURN_BASELINE_CACHE_RELATIVE_DIR, TURN_BASELINE_PAYLOAD_DIR};
use crate::guided_review::{source_store::write_private_file, SourceStoreError};
use std::{
collections::HashSet,
fs,
path::{Path, PathBuf},
time::{Duration, SystemTime},
};
use tracing::{debug, warn};
/// How old a baseline the last-turn record doesn't name must be before pruning deletes it.
pub(crate) const UNREFERENCED_BASELINE_AGE: Duration = Duration::from_secs(60 * 60);
/// The payload directory of the newest committed baseline under `root`, to link unchanged files
/// from; none when there isn't one yet.
pub(super) fn newest_payload_dir(root: &Path) -> Option<PathBuf> {
fs::read_dir(root)
.ok()?
.filter_map(Result::ok)
.filter(|entry| entry.file_type().is_ok_and(|kind| kind.is_dir()))
.filter(|entry| !entry.file_name().to_string_lossy().starts_with('.'))
.filter_map(|entry| Some((entry.metadata().ok()?.modified().ok()?, entry.path())))
.max_by_key(|(modified, _)| *modified)
.map(|(_, path)| path.join(TURN_BASELINE_PAYLOAD_DIR))
}
/// Puts one payload at `destination`: a hard link to `earlier`'s copy of the same digest when it
/// has a plain file of the right size, else a fresh synced write. A link that fails (another
/// filesystem, one that has none) falls back to the write.
pub(super) fn place_payload(
destination: &Path,
earlier: Option<&Path>,
digest: &str,
bytes: &[u8],
) -> Result<(), SourceStoreError> {
if let Some(source) = earlier.map(|dir| dir.join(digest)) {
let same = fs::symlink_metadata(&source)
.is_ok_and(|meta| meta.is_file() && meta.len() == bytes.len() as u64);
if same && fs::hard_link(&source, destination).is_ok() {
return Ok(());
}
}
write_private_file(destination, bytes)
}
/// Deletes every baseline under the project's cache but `keep` once it's older than `age`, along
/// with staging a crashed capture left behind. A symlink is never followed, and one that can't be
/// deleted is logged and skipped, since a stale baseline costs only disk. Returns how many went.
pub(crate) fn prune_turn_baselines(
project_root: &Path,
keep: &[&TurnBaselineId],
age: Duration,
) -> usize {
let root = project_root.join(TURN_BASELINE_CACHE_RELATIVE_DIR);
let Some(entries) = baseline_entries(&root) else {
return 0;
};
let kept: HashSet<&str> = keep.iter().map(|id| id.cache_key()).collect();
let cutoff = SystemTime::now().checked_sub(age).unwrap_or(SystemTime::UNIX_EPOCH);
let pruned = entries
.filter(|entry| is_stale(entry, &kept, cutoff))
.filter(|entry| remove_baseline(&entry.path()))
.count();
debug!(pruned, "pruned turn baselines");
pruned
}
/// The cache's entries; none when it doesn't exist yet, or can't be listed, which is logged.
fn baseline_entries(root: &Path) -> Option<impl Iterator<Item = fs::DirEntry>> {
match fs::read_dir(root) {
Ok(entries) => Some(entries.filter_map(Result::ok)),
Err(error) if error.kind() == std::io::ErrorKind::NotFound => None,
Err(error) => {
warn!(path = %root.display(), %error, "could not list turn baselines to prune");
None
}
}
}
/// A directory, not a symlink, that `kept` doesn't name and that was last written before `cutoff`.
fn is_stale(entry: &fs::DirEntry, kept: &HashSet<&str>, cutoff: SystemTime) -> bool {
entry.file_type().is_ok_and(|kind| kind.is_dir())
&& !kept.contains(entry.file_name().to_string_lossy().as_ref())
&& entry.metadata().and_then(|meta| meta.modified()).is_ok_and(|at| at < cutoff)
}
fn remove_baseline(path: &Path) -> bool {
fs::remove_dir_all(path)
.inspect_err(|error| warn!(path = %path.display(), %error, "could not prune turn baseline"))
.is_ok()
}
#[cfg(test)]
mod tests;

View File

@@ -0,0 +1,145 @@
use super::*;
use crate::guided_review::{ReviewBase, TurnBaselineArtifact};
fn baseline(turn: &str, files: &[(&str, &[u8])]) -> TurnBaselineArtifact {
let files = files.iter().map(|(path, bytes)| (PathBuf::from(path), bytes.to_vec())).collect();
let id = TurnBaselineId::from_session_turn("session", turn);
TurnBaselineArtifact::capture(id, ReviewBase::EmptyTree, 1, false, false, 0, files)
.expect("a fixture of normal paths is a valid baseline")
}
/// Where `artifact`'s copy of `bytes` sits once persisted under `project`.
#[cfg(unix)]
fn payload(project: &Path, artifact: &TurnBaselineArtifact, bytes: &[u8]) -> PathBuf {
let digest = super::super::payload_digest(bytes);
let root = project.join(TURN_BASELINE_CACHE_RELATIVE_DIR);
root.join(artifact.manifest.id.cache_key()).join(TURN_BASELINE_PAYLOAD_DIR).join(digest)
}
fn persisted(project: &Path, turn: &str, files: &[(&str, &[u8])]) -> TurnBaselineArtifact {
let artifact = baseline(turn, files);
artifact.persist(project).expect("a valid baseline persists");
artifact
}
fn baseline_dirs(project: &Path) -> Vec<String> {
let root = project.join(TURN_BASELINE_CACHE_RELATIVE_DIR);
let mut names: Vec<String> = fs::read_dir(root)
.expect("the cache exists")
.map(|entry| entry.expect("listable").file_name().to_string_lossy().into_owned())
.collect();
names.sort();
names
}
#[cfg(unix)]
fn inode(path: &Path) -> u64 {
std::os::unix::fs::MetadataExt::ino(&fs::symlink_metadata(path).expect("the payload exists"))
}
// The break this catches: every capture writing and syncing every file again, which cost a
// project of 4,000 files about ten seconds a turn and a full copy of it on disk each time
#[cfg(unix)]
#[test]
fn a_baseline_links_a_file_the_last_one_already_holds() {
let project = tempfile::tempdir().expect("a temporary project");
let first = persisted(project.path(), "1", &[("src/main.rs", b"fn main() {}\n")]);
let second_files: &[(&str, &[u8])] = &[("src/main.rs", b"fn main() {}\n"), ("new.rs", b"new")];
let second = persisted(project.path(), "2", second_files);
let main = b"fn main() {}\n".as_slice();
assert_eq!(
inode(&payload(project.path(), &first, main)),
inode(&payload(project.path(), &second, main))
);
let loaded = TurnBaselineArtifact::load(project.path(), &second.manifest.id);
assert_eq!(loaded.expect("the linked baseline loads"), second);
}
// The break this catches: linking whatever file sits under the digest's name, so a truncated
// copy carries into every later baseline and none of them load
#[cfg(unix)]
#[test]
fn an_earlier_copy_of_the_wrong_size_is_written_fresh() {
let project = tempfile::tempdir().expect("a temporary project");
let first = persisted(project.path(), "1", &[("a.rs", b"whole")]);
let first_copy = payload(project.path(), &first, b"whole");
fs::write(&first_copy, b"cut").expect("the fixture truncates the earlier copy");
let second = persisted(project.path(), "2", &[("a.rs", b"whole")]);
assert_ne!(inode(&first_copy), inode(&payload(project.path(), &second, b"whole")));
assert!(TurnBaselineArtifact::load(project.path(), &second.manifest.id).is_ok());
}
// The break this catches: linking a symlink planted under the digest's name, which hard-links
// the symlink itself into the new baseline. Its target is as long as the payload, since a
// symlink's own size is its target's length, so the size check alone can't turn it away
#[cfg(unix)]
#[test]
fn an_earlier_copy_that_is_a_symlink_is_written_fresh() {
let project = tempfile::tempdir().expect("a temporary project");
let first = persisted(project.path(), "1", &[("a.rs", b"whole")]);
let first_copy = payload(project.path(), &first, b"whole");
fs::remove_file(&first_copy).expect("the fixture replaces the earlier copy");
std::os::unix::fs::symlink("abcde", &first_copy).expect("the fixture plants a symlink");
let second = persisted(project.path(), "2", &[("a.rs", b"whole")]);
let second_copy = fs::symlink_metadata(payload(project.path(), &second, b"whole"));
assert!(second_copy.expect("the payload exists").is_file());
}
// The break this catches: linking from the oldest baseline rather than the newest, which holds
// least of what the workspace has now
#[cfg(unix)]
#[test]
fn links_come_from_the_newest_baseline() {
let project = tempfile::tempdir().expect("a temporary project");
let older = persisted(project.path(), "1", &[("a.rs", b"a")]);
let newer = persisted(project.path(), "2", &[("a.rs", b"b")]);
let root = project.path().join(TURN_BASELINE_CACHE_RELATIVE_DIR);
let older_dir = root.join(older.manifest.id.cache_key());
let hours_ago = SystemTime::now() - Duration::from_secs(2 * 60 * 60);
fs::File::open(&older_dir).and_then(|dir| dir.set_modified(hours_ago)).expect("backdated");
let newest = newest_payload_dir(&root).expect("there are baselines");
assert_eq!(newest, root.join(newer.manifest.id.cache_key()).join(TURN_BASELINE_PAYLOAD_DIR));
}
// The break this catches: pruning that ignores what the last-turn record names, losing the one
// review the cache exists for, or that deletes nothing at all
#[test]
fn pruning_deletes_old_baselines_but_the_ones_kept() {
let project = tempfile::tempdir().expect("a temporary project");
let before = persisted(project.path(), "1", &[("a.rs", b"a")]);
let after = persisted(project.path(), "2", &[("a.rs", b"b")]);
persisted(project.path(), "3", &[("a.rs", b"c")]);
let keep = [&before.manifest.id, &after.manifest.id];
assert_eq!(prune_turn_baselines(project.path(), &keep, Duration::ZERO), 1);
let mut kept = vec![before.manifest.id.cache_key(), after.manifest.id.cache_key()];
kept.sort();
assert_eq!(baseline_dirs(project.path()), kept);
}
// The break this catches: pruning by the record alone, which deletes the baseline of a turn
// another session in the project is still running
#[test]
fn pruning_spares_a_baseline_younger_than_the_age() {
let project = tempfile::tempdir().expect("a temporary project");
persisted(project.path(), "1", &[("a.rs", b"a")]);
assert_eq!(prune_turn_baselines(project.path(), &[], UNREFERENCED_BASELINE_AGE), 0);
assert_eq!(baseline_dirs(project.path()).len(), 1);
}
// The break this catches: staging a crashed capture left behind piling up forever, since no
// record ever names it
#[test]
fn pruning_deletes_abandoned_staging() {
let project = tempfile::tempdir().expect("a temporary project");
let staging = project.path().join(TURN_BASELINE_CACHE_RELATIVE_DIR).join(".staging-crashed");
fs::create_dir_all(staging.join(TURN_BASELINE_PAYLOAD_DIR)).expect("the fixture stages");
assert_eq!(prune_turn_baselines(project.path(), &[], Duration::ZERO), 1);
assert!(!staging.exists());
}
// The break this catches: a project that has never captured a baseline failing its first turn
#[test]
fn pruning_a_project_with_no_cache_does_nothing() {
let project = tempfile::tempdir().expect("a temporary project");
assert_eq!(prune_turn_baselines(project.path(), &[], Duration::ZERO), 0);
}

View File

@@ -43,6 +43,7 @@
mod hidden;
mod link_comments;
pub(crate) mod nested;
/// Instruction text with the unreviewable parts removed, and what came out.
#[derive(Debug, Clone, PartialEq, Eq)]

View File

@@ -0,0 +1,106 @@
//! Nested instructions — the `AGENTS.md` files off the launch chain, read when work reaches them.
//!
//! The system prompt carries the files from the top of the checkout down to where Luxe started,
//! and it's written once per session. A file in a folder off that chain (`web/AGENTS.md` when Luxe
//! started at the root) can't join it without rewriting the prompt every later request is cached
//! against. So it rides on the result of the first tool call that reaches its folder: an append,
//! like any other result. Whether the model already has one is read off the conversation by the
//! note that opens it, so a rewind or a compaction that took it away lets it be sent again.
use std::path::{Path, PathBuf};
/// Opens the block that carries a nested file. Its path follows, up to [`NOTE_REST`].
const NOTE_OPEN: &str = "[instructions: ";
/// What follows the path, which is also where reading one back stops: a path may hold a `]`.
const NOTE_REST: &str = "] Instructions for work in this folder, from the file there. They add \
to the project's.";
/// Whether nested files are read at all. `LUXE_NESTED_INSTRUCTIONS=0` turns them off.
pub(crate) fn enabled() -> bool {
std::env::var("LUXE_NESTED_INSTRUCTIONS").as_deref() != Ok("0")
}
/// The paths a call's arguments name: `path`, and each string in `paths`.
///
/// Those two keys cover the tools that open, write or search a place in the tree. A shell command
/// isn't parsed: guessing at the paths in one would load rules for folders the call never reached.
pub(crate) fn touched_paths(args: &serde_json::Value) -> Vec<&str> {
let single = args.get("path").and_then(serde_json::Value::as_str);
let many = args.get("paths").and_then(serde_json::Value::as_array).into_iter().flatten();
single
.into_iter()
.chain(many.filter_map(serde_json::Value::as_str))
.filter(|path| !path.trim().is_empty())
.collect()
}
/// One nested instruction file a call reached.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct NestedFile {
/// The file, canonical
pub(crate) path: PathBuf,
/// Its path from the top of the checkout, with forward slashes: what the note names
pub(crate) shown: String,
}
/// The instruction files that apply to `target` but weren't read at session start, top first.
///
/// Only folders inside the checkout `launch` sits in, off its chain, and not ignored: a file under
/// `node_modules` was written by a dependency's authors, not this project's. `target` may not exist
/// yet (a file about to be written), so the nearest folder that does stands in.
pub(crate) fn nested_files(launch: &Path, target: &Path) -> Vec<NestedFile> {
let chain: Vec<PathBuf> = crate::project::chain_from_top(launch)
.iter()
.filter_map(|folder| folder.canonicalize().ok())
.collect();
let (Some(top), Some(folder)) = (chain.first(), existing_folder(target)) else {
return Vec::new();
};
folders_below(top, &folder)
.into_iter()
.filter(|dir| !chain.iter().any(|on_chain| on_chain == dir))
.filter_map(|dir| Some((dir, instruction_file(dir)?)))
.filter(|(dir, _)| !crate::project_files::is_ignored(top, dir))
.filter_map(|(_, file)| nested_file(top, file))
.collect()
}
/// The folders from just below `top` down to `folder`, top first; none when `folder` is outside.
fn folders_below<'a>(top: &Path, folder: &'a Path) -> Vec<&'a Path> {
if !folder.starts_with(top) {
return Vec::new();
}
let mut below: Vec<&Path> = folder.ancestors().take_while(|dir| *dir != top).collect();
below.reverse();
below
}
/// The nearest folder at or above `target` that exists, canonical.
fn existing_folder(target: &Path) -> Option<PathBuf> {
target.ancestors().find(|path| path.is_dir())?.canonicalize().ok()
}
/// The instruction file `dir` holds, by the same names and order the launch chain uses.
fn instruction_file(dir: &Path) -> Option<PathBuf> {
super::INSTRUCTION_FILES.iter().map(|name| dir.join(name)).find(|path| path.is_file())
}
/// `file` as a [`NestedFile`], named from `top` by where it sits rather than where a link points.
fn nested_file(top: &Path, file: PathBuf) -> Option<NestedFile> {
let shown = crate::paths::display(file.strip_prefix(top).ok()?);
Some(NestedFile { path: file.canonicalize().ok()?, shown })
}
/// The block that carries `shown`'s text to the model.
pub(crate) fn note(shown: &str, text: &str) -> String {
format!("{NOTE_OPEN}{shown}{NOTE_REST}\n\n{text}")
}
/// The file a block carries, when `text` is one.
pub(crate) fn noted(text: &str) -> Option<&str> {
text.strip_prefix(NOTE_OPEN)?.split_once(NOTE_REST).map(|(shown, _)| shown)
}
#[cfg(test)]
mod tests;

View File

@@ -0,0 +1,101 @@
use super::*;
/// A checkout at a temp dir's root, with `files` written into it.
fn checkout(files: &[&str]) -> tempfile::TempDir {
let dir = tempfile::tempdir().expect("temp dir");
std::fs::create_dir_all(dir.path().join(".git")).expect("git marker");
for file in files {
let path = dir.path().join(file);
std::fs::create_dir_all(path.parent().expect("a parent")).expect("folders");
std::fs::write(&path, format!("- rules from {file}\n")).expect("write");
}
dir
}
/// What [`nested_files`] names for `target`, from Luxe launched at `launch`, both under `root`.
fn shown(root: &Path, launch: &str, target: &str) -> Vec<String> {
let files = nested_files(&root.join(launch), &root.join(target));
files.into_iter().map(|file| file.shown).collect()
}
/// The break this catches: reading only `path`, so a multi-file `read` loaded nothing.
#[test]
fn a_call_touches_its_path_and_each_of_its_paths() {
let args = serde_json::json!({ "path": "a.rs", "paths": ["web/b.ts", 7, "", "c.md"] });
assert_eq!(touched_paths(&args), ["a.rs", "web/b.ts", "c.md"]);
assert!(touched_paths(&serde_json::json!({ "command": "cat web/x" })).is_empty());
assert!(touched_paths(&serde_json::json!({ "path": 3 })).is_empty());
}
/// The break this catches: the walk started at the launch folder, so a file one folder over
/// from it was never found.
#[test]
fn a_folder_off_the_launch_chain_is_found() {
let root = checkout(&["AGENTS.md", "web/AGENTS.md", "web/src/app.ts"]);
assert_eq!(shown(root.path(), "", "web/src/app.ts"), ["web/AGENTS.md"]);
}
/// The break this catches: the walk ran nearest first, so the more general rules arrived last
/// and read as the ones that win.
#[test]
fn files_come_top_down() {
let root = checkout(&["web/AGENTS.md", "web/src/AGENTS.md", "web/src/app.ts"]);
let found = shown(root.path(), "", "web/src/app.ts");
assert_eq!(found, ["web/AGENTS.md", "web/src/AGENTS.md"]);
}
/// The break this catches: folders on the launch chain were offered again, sending a file the
/// system prompt already carries.
#[test]
fn the_launch_chain_is_left_to_the_system_prompt() {
let root = checkout(&["AGENTS.md", "web/AGENTS.md", "web/src/AGENTS.md", "web/src/app.ts"]);
assert_eq!(shown(root.path(), "web", "web/src/app.ts"), ["web/src/AGENTS.md"]);
assert!(shown(root.path(), "", "README.md").is_empty(), "the top is the prompt's");
}
/// The break this catches: no ignore check, so a dependency's own `AGENTS.md` under an ignored
/// folder instructed the model as if this project wrote it.
#[test]
fn an_ignored_folder_is_skipped_and_its_sibling_is_not() {
let root = checkout(&["vendor/lib/AGENTS.md", "app/AGENTS.md"]);
std::fs::write(root.path().join(".gitignore"), "vendor/\n").expect("gitignore");
assert!(shown(root.path(), "", "vendor/lib/x.rs").is_empty());
assert_eq!(shown(root.path(), "", "app/x.rs"), ["app/AGENTS.md"]);
}
/// The break this catches: no checkout boundary, so a path outside the project loaded the rules
/// of whoever owns that folder.
#[test]
fn a_path_outside_the_checkout_finds_nothing() {
let root = checkout(&[]);
let elsewhere = checkout(&["lib/AGENTS.md", "lib/x.rs"]);
let files = nested_files(root.path(), &elsewhere.path().join("lib/x.rs"));
assert!(files.is_empty(), "{files:?}");
}
/// The break this catches: a target that doesn't exist yet (a file about to be written) found
/// no folder and so no rules.
#[test]
fn a_file_not_written_yet_uses_the_nearest_folder() {
let root = checkout(&["web/AGENTS.md"]);
assert_eq!(shown(root.path(), "", "web/new/deep/page.ts"), ["web/AGENTS.md"]);
}
/// The break this catches: only `AGENTS.md` was looked for, so a folder that still carries a
/// `CLAUDE.md` was silent here while the launch chain read it.
#[test]
fn a_claude_md_counts_like_it_does_on_the_chain() {
let root = checkout(&["web/CLAUDE.md"]);
assert_eq!(shown(root.path(), "", "web/x.ts"), ["web/CLAUDE.md"]);
}
/// The break this catches: reading a note back stopped at the first `]`, so a path holding one
/// read as a different file and was sent again.
#[test]
fn a_note_reads_back_as_its_file() {
let block = note("web/[id]/AGENTS.md", "- rules");
assert_eq!(noted(&block), Some("web/[id]/AGENTS.md"));
assert!(block.ends_with("\n\n- rules"), "{block}");
assert_eq!(noted("[instructions: web/AGENTS.md"), None, "a fragment isn't a note");
assert_eq!(noted("12: [instructions: x] y"), None, "nor is a line of a file that quotes one");
}

View File

@@ -38,6 +38,7 @@ pub mod issue;
pub mod json_file;
pub mod jsonrpc_frame;
pub mod keymap;
pub mod local_endpoint;
pub mod loop_controller;
pub mod loop_hooks;
pub(crate) mod loop_state;

View File

@@ -0,0 +1,91 @@
//! local_endpoint — the way into a running Luxe from the same machine, and the list of them.
//!
//! Each interactive Luxe serves its protocol here, so another front-end (the `luxe remote` bridge,
//! `luxe join`, a script) can join the session it is running. It is local IPC, not network I/O: a
//! Unix socket in a folder only this user can open, or on Windows a named pipe only this user can
//! open and no other machine can reach. This module is the one owner of those platform details.
use std::path::Path;
use tokio::io::{AsyncRead, AsyncWrite};
mod ancestry;
mod live;
#[cfg(unix)]
mod unix;
#[cfg(windows)]
mod windows;
pub use ancestry::descends_from;
pub use live::{live_sessions, running_sessions, LiveSession, Publication};
#[cfg(unix)]
use unix as platform;
#[cfg(windows)]
use windows as platform;
/// Both directions of a connection, whatever the platform carries them on.
pub trait LocalIo: AsyncRead + AsyncWrite + Unpin + Send {}
impl<T: AsyncRead + AsyncWrite + Unpin + Send> LocalIo for T {}
/// A connection to or from a running Luxe.
pub type LocalStream = Box<dyn LocalIo>;
/// A client that connected, with its process id where the platform reports one.
pub struct Joined {
/// The connection
pub stream: LocalStream,
/// The client's process id, for the check that it is not one of this Luxe's own children
pub peer: Option<u32>,
}
/// Why an endpoint could not be opened, accepted on, or reached.
#[derive(Debug, thiserror::Error)]
pub enum EndpointError {
/// The operating system refused. Args: what was being done, and its error.
#[error("{action}: {source}")]
Io {
/// What was being done, as a phrase
action: String,
/// The operating system's error
#[source]
source: std::io::Error,
},
}
impl EndpointError {
/// A `map_err` adapter naming what was being done.
fn io(action: impl Into<String>) -> impl FnOnce(std::io::Error) -> Self {
let action = action.into();
move |source| Self::Io { action, source }
}
}
/// This Luxe's endpoint, open for clients until it is dropped.
pub struct EndpointListener(platform::Listener);
impl EndpointListener {
/// Opens an endpoint for this process. On Unix its socket goes in `dir`, which is created
/// private to this user; a Windows pipe has a name rather than a place.
pub fn bind(dir: &Path) -> Result<Self, EndpointError> {
platform::Listener::bind(dir).map(Self)
}
/// What a client passes to [`connect`]: a socket path, or a pipe name.
pub fn address(&self) -> &str {
self.0.address()
}
/// The next client to connect, running as this user.
pub async fn accept(&mut self) -> Result<Joined, EndpointError> {
self.0.accept().await
}
}
/// Connects to the endpoint at `address`, as [`EndpointListener::address`] gave it.
pub async fn connect(address: &str) -> Result<LocalStream, EndpointError> {
platform::connect(address).await
}
#[cfg(test)]
mod tests;

View File

@@ -0,0 +1,115 @@
//! Ancestry — whether a process was started, at any depth, by another.
//!
//! The tripwire that keeps a Luxe's own children (the agent's `bash`, a hook) from joining its
//! session and answering their own prompts. A tripwire, not a wall: a process that detaches itself
//! leaves the chain, and the documentation says so. Blocking: run it off the async runtime.
/// How many parents to walk before giving up; real chains are a handful deep.
const MAX_DEPTH: usize = 64;
/// Whether `pid` is `ancestor` or was started, at any depth, by it. `false` when the chain cannot
/// be read, so a client this cannot place is let in, as it would be without the tripwire.
pub fn descends_from(pid: u32, ancestor: u32) -> bool {
descends_in(pid, ancestor, parent_lookup())
}
/// [`descends_from`] over an explicit parent table.
fn descends_in(pid: u32, ancestor: u32, parent_of: impl Fn(u32) -> Option<u32>) -> bool {
let mut current = pid;
for _ in 0..MAX_DEPTH {
if current == ancestor {
return true;
}
match parent_of(current) {
Some(parent) if parent != current && parent != 0 => current = parent,
_ => return false,
}
}
false
}
#[cfg(target_os = "linux")]
fn parent_lookup() -> impl Fn(u32) -> Option<u32> {
|pid| parent_in_stat(&std::fs::read_to_string(format!("/proc/{pid}/stat")).ok()?)
}
/// The parent id from a `/proc/<pid>/stat` line. The command name sits in parentheses and may hold
/// spaces and parentheses itself, so fields are counted from the last `)`.
#[cfg(any(target_os = "linux", test))]
fn parent_in_stat(stat: &str) -> Option<u32> {
let (_, fields) = stat.rsplit_once(')')?;
fields.split_whitespace().nth(1)?.parse().ok()
}
#[cfg(all(unix, not(target_os = "linux")))]
fn parent_lookup() -> impl Fn(u32) -> Option<u32> {
let table = process_table();
move |pid| table.get(&pid).copied()
}
/// Every process's parent, from `ps`, which macOS and the BSDs all have at the same path.
#[cfg(all(unix, not(target_os = "linux")))]
fn process_table() -> std::collections::HashMap<u32, u32> {
let listing = std::process::Command::new("/bin/ps").args(["-A", "-o", "pid=,ppid="]).output();
let Ok(listing) = listing else {
return std::collections::HashMap::new();
};
String::from_utf8_lossy(&listing.stdout)
.lines()
.filter_map(|line| {
let mut ids = line.split_whitespace().map(str::parse::<u32>);
Some((ids.next()?.ok()?, ids.next()?.ok()?))
})
.collect()
}
#[cfg(windows)]
fn parent_lookup() -> impl Fn(u32) -> Option<u32> {
let table = super::windows::process_parents();
move |pid| table.get(&pid).copied()
}
#[cfg(test)]
mod tests {
use super::*;
/// A process is its own descendant, a grandchild is found through its parent, and an
/// unrelated process or a broken chain is not.
#[test]
fn the_chain_is_walked_to_the_ancestor_and_no_further() {
let parents = |pid| match pid {
30 => Some(20),
20 => Some(10),
10 => Some(1),
1 => Some(0),
_ => None,
};
assert!(descends_in(30, 10, parents), "a grandchild");
assert!(descends_in(10, 10, parents), "itself");
assert!(!descends_in(10, 30, parents), "a parent is not a child");
assert!(!descends_in(99, 10, parents), "unknown, so not placed");
assert!(!descends_in(7, 10, |_| Some(7)), "a process that is its own parent ends the walk");
}
/// The parent is the fourth field, counted after a command name that can hold anything.
#[test]
fn the_parent_is_read_past_an_awkward_command_name() {
assert_eq!(parent_in_stat("4242 (bash) S 4000 4242 4242 0"), Some(4000));
assert_eq!(parent_in_stat("4242 (my (odd) prog) R 17 4242"), Some(17));
assert_eq!(parent_in_stat("garbage"), None);
}
/// A real child of this test is found, and this test is not the child's.
#[test]
#[cfg(unix)]
fn a_real_child_descends_from_this_process() {
let mut child = std::process::Command::new("sleep").arg("5").spawn().expect("sleep runs");
let (child_id, own_id) = (child.id(), std::process::id());
let found = descends_from(child_id, own_id);
let reversed = descends_from(own_id, child_id);
let _ = child.kill();
let _ = child.wait();
assert!(found, "the child is placed under this process");
assert!(!reversed);
}
}

View File

@@ -0,0 +1,85 @@
//! Live sessions — which Luxes on this machine can be joined, and where.
//!
//! Each running Luxe with an endpoint leaves a small record in the runtime folder, named for its
//! process, and takes it away when it stops. A record left by a Luxe that died is found out by the
//! one test that matters: its endpoint no longer answers.
use serde::{Deserialize, Serialize};
use std::io;
use std::path::{Path, PathBuf};
/// One running Luxe that can be joined.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LiveSession {
/// The Luxe's process id
pub pid: u32,
/// Session it is running
pub session_id: String,
/// Project folder it was opened in
pub cwd: String,
/// Where to [`super::connect`]
pub address: String,
/// The build serving it
pub luxe_version: String,
}
/// This Luxe's record in the runtime folder, removed when dropped.
pub struct Publication {
path: PathBuf,
}
impl LiveSession {
/// Writes this record into `dir`, readable only by this user.
pub fn publish(&self, dir: &Path) -> io::Result<Publication> {
crate::fs_atomic::create_private_dir(dir)?;
let publication = Publication { path: dir.join(format!("{}.json", self.pid)) };
publication.update(self)?;
Ok(publication)
}
}
impl Publication {
/// Rewrites the record, as when the Luxe moves to another session.
pub fn update(&self, session: &LiveSession) -> io::Result<()> {
let bytes = serde_json::to_vec_pretty(session).map_err(io::Error::other)?;
crate::fs_atomic::replace_private(&self.path, &bytes)
}
}
impl Drop for Publication {
fn drop(&mut self) {
let _ = std::fs::remove_file(&self.path);
}
}
/// Every record in `dir`, by process id. A missing folder has none, and a record that does not
/// read is skipped, since it is either being written or not one of these.
pub fn live_sessions(dir: &Path) -> Vec<LiveSession> {
let Ok(entries) = std::fs::read_dir(dir) else {
return Vec::new();
};
let mut found: Vec<LiveSession> =
entries.filter_map(|entry| read_record(&entry.ok()?.path())).collect();
found.sort_by_key(|session| session.pid);
found
}
/// [`live_sessions`] whose Luxe is still running: one that crashed leaves its record behind.
///
/// Checked by process id rather than by connecting, since a connection is a client joining, and the
/// terminal running the session says so each time.
pub fn running_sessions(dir: &Path) -> Vec<LiveSession> {
let mut sessions = live_sessions(dir);
sessions.retain(|session| crate::program::process_is_alive(session.pid));
sessions
}
fn read_record(path: &Path) -> Option<LiveSession> {
if path.extension()? != "json" {
return None;
}
let bytes = std::fs::read(path).ok()?;
serde_json::from_slice(&bytes)
.inspect_err(|error| tracing::debug!(path = %path.display(), %error, "skipped a record"))
.ok()
}

View File

@@ -0,0 +1,71 @@
use super::*;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
/// A client reaches the endpoint and both directions carry bytes; the listener names the client's
/// process, which here is this test's own.
#[tokio::test]
async fn a_client_reaches_the_endpoint_both_ways() {
let dir = tempfile::tempdir().expect("a temp dir");
let mut listener = EndpointListener::bind(&dir.path().join("run")).expect("binds");
let address = listener.address().to_string();
let (client, joined) = tokio::join!(connect(&address), listener.accept());
let (mut client, mut joined) = (client.expect("connects"), joined.expect("accepts"));
client.write_all(b"ping").await.expect("writes");
let mut heard = [0u8; 4];
joined.stream.read_exact(&mut heard).await.expect("reads");
joined.stream.write_all(b"pong").await.expect("answers");
client.read_exact(&mut heard).await.expect("hears");
assert_eq!(&heard, b"pong");
assert_eq!(joined.peer, Some(std::process::id()));
}
/// The socket and its folder are this user's alone, and the socket goes with the listener.
#[tokio::test]
#[cfg(unix)]
async fn the_socket_is_private_and_goes_with_the_listener() {
use std::os::unix::fs::PermissionsExt;
let dir = tempfile::tempdir().expect("a temp dir");
let run = dir.path().join("run");
let listener = EndpointListener::bind(&run).expect("binds");
let socket = std::path::PathBuf::from(listener.address());
let mode = |path: &std::path::Path| {
std::fs::metadata(path).expect("exists").permissions().mode() & 0o777
};
assert_eq!(mode(&run), 0o700, "the folder");
assert_eq!(mode(&socket), 0o600, "the socket");
drop(listener);
assert!(!socket.exists(), "a stopped Luxe leaves no socket behind");
assert!(connect(&socket.to_string_lossy()).await.is_err());
}
/// A published record is listed until it is dropped, and a stray file in the folder is not one.
#[test]
fn a_record_is_listed_while_its_luxe_runs() {
let dir = tempfile::tempdir().expect("a temp dir");
let session = LiveSession {
pid: 4242,
session_id: "session-a".into(),
cwd: "/work/widget".into(),
address: "/run/user/1000/luxe/4242.sock".into(),
luxe_version: "0.1.0".into(),
};
let publication = session.publish(dir.path()).expect("publishes");
std::fs::write(dir.path().join("notes.txt"), "not a record").expect("writes");
std::fs::write(dir.path().join("7.json"), "{ half").expect("writes");
assert_eq!(live_sessions(dir.path()), vec![session.clone()]);
let moved = LiveSession { session_id: "session-b".into(), ..session };
publication.update(&moved).expect("updates");
assert_eq!(live_sessions(dir.path()), vec![moved]);
drop(publication);
assert_eq!(live_sessions(dir.path()), Vec::new());
}
/// The record's shape is what other programs read, so it is pinned, not only round-tripped.
#[test]
fn the_record_shape_is_pinned() {
let literal = r#"{"pid":4242,"session_id":"session-a","cwd":"/work/widget","address":"/run/user/1000/luxe/4242.sock","luxe_version":"0.1.0"}"#;
let read: LiveSession = serde_json::from_str(literal).expect("reads");
assert_eq!(serde_json::to_string(&read).expect("writes"), literal);
}

View File

@@ -0,0 +1,81 @@
//! The Unix endpoint: a socket in a folder only this user can open.
use super::{EndpointError, Joined, LocalStream};
use std::path::{Path, PathBuf};
use tokio::net::{UnixListener, UnixStream};
/// A socket named for this process, removed when the listener is dropped.
pub(super) struct Listener {
listener: UnixListener,
path: PathBuf,
address: String,
}
impl Listener {
pub(super) fn bind(dir: &Path) -> Result<Self, EndpointError> {
crate::fs_atomic::create_private_dir(dir)
.map_err(EndpointError::io(format!("create {}", dir.display())))?;
let path = dir.join(format!("{}.sock", std::process::id()));
// left by an earlier process with this id; only this user can write in this folder
let _ = std::fs::remove_file(&path);
let listener = UnixListener::bind(&path)
.map_err(EndpointError::io(format!("listen on {}", path.display())))?;
restrict(&path)?;
let address = path.to_string_lossy().into_owned();
Ok(Self { listener, path, address })
}
pub(super) fn address(&self) -> &str {
&self.address
}
pub(super) async fn accept(&mut self) -> Result<Joined, EndpointError> {
loop {
let (stream, _) =
self.listener.accept().await.map_err(EndpointError::io("accept a client"))?;
if let Some(joined) = admit(stream) {
return Ok(joined);
}
}
}
}
impl Drop for Listener {
fn drop(&mut self) {
let _ = std::fs::remove_file(&self.path);
}
}
/// `stream` as a client, unless it runs as another user or will not say who it is.
///
/// The folder already keeps other users out; this holds if it ever does not.
fn admit(stream: UnixStream) -> Option<Joined> {
let credentials = match stream.peer_cred() {
Ok(credentials) => credentials,
Err(error) => {
tracing::warn!(%error, "refused a client whose credentials could not be read");
return None;
}
};
if credentials.uid() != rustix::process::geteuid().as_raw() {
tracing::warn!(uid = credentials.uid(), "refused a client running as another user");
return None;
}
let peer = credentials.pid().and_then(|pid| u32::try_from(pid).ok());
Some(Joined { stream: Box::new(stream), peer })
}
/// Owner read and write only. The folder is private already; this is the second lock.
fn restrict(path: &Path) -> Result<(), EndpointError> {
use std::os::unix::fs::PermissionsExt;
let owner_only = std::fs::Permissions::from_mode(crate::fs_atomic::OWNER_ONLY);
std::fs::set_permissions(path, owner_only)
.map_err(EndpointError::io(format!("restrict {}", path.display())))
}
pub(super) async fn connect(address: &str) -> Result<LocalStream, EndpointError> {
let stream = UnixStream::connect(address)
.await
.map_err(EndpointError::io(format!("connect to {address}")))?;
Ok(Box::new(stream))
}

View File

@@ -0,0 +1,75 @@
//! The Windows endpoint: a named pipe only this user can open, and no other machine can reach.
//!
//! The defaults are wrong for this. A pipe's default security grants read access to Everyone and
//! the anonymous account, and reading is the whole transcript; and a pipe can be opened across the
//! network while the Server service runs. So every instance is created with a descriptor granting
//! this user alone, remote clients are refused, and the first instance must be new, so nothing can
//! claim the name before Luxe does.
use super::{EndpointError, Joined, LocalStream};
use std::path::Path;
use std::time::Duration;
use tokio::net::windows::named_pipe::{ClientOptions, NamedPipeServer, ServerOptions};
use windows_sys::Win32::Foundation::ERROR_PIPE_BUSY;
mod security;
pub(super) use security::process_parents;
/// How long a client waits for the server's next instance, which only lags the last connection.
const BUSY_RETRIES: u32 = 40;
/// A pipe with an instance always waiting for the next client.
pub(super) struct Listener {
next: NamedPipeServer,
address: String,
descriptor: security::OwnerOnly,
}
impl Listener {
/// `_dir` has no part: a pipe has a name rather than a place.
pub(super) fn bind(_dir: &Path) -> Result<Self, EndpointError> {
let descriptor = security::OwnerOnly::new()
.map_err(EndpointError::io("describe who may open the pipe"))?;
let address = format!(r"\\.\pipe\luxe-{}", uuid::Uuid::new_v4().simple());
let next = create(&address, &descriptor, true)?;
Ok(Self { next, address, descriptor })
}
pub(super) fn address(&self) -> &str {
&self.address
}
pub(super) async fn accept(&mut self) -> Result<Joined, EndpointError> {
self.next.connect().await.map_err(EndpointError::io("accept a client"))?;
let waiting = create(&self.address, &self.descriptor, false)?;
let connected = std::mem::replace(&mut self.next, waiting);
let peer = security::client_process_id(&connected);
Ok(Joined { stream: Box::new(connected), peer })
}
}
/// One instance of the pipe at `address`; `first` refuses a name something else already holds.
fn create(
address: &str,
descriptor: &security::OwnerOnly,
first: bool,
) -> Result<NamedPipeServer, EndpointError> {
let mut options = ServerOptions::new();
options.first_pipe_instance(first).reject_remote_clients(true);
descriptor.create(&options, address).map_err(EndpointError::io(format!("open {address}")))
}
pub(super) async fn connect(address: &str) -> Result<LocalStream, EndpointError> {
for _ in 0..BUSY_RETRIES {
match ClientOptions::new().open(address) {
Ok(client) => return Ok(Box::new(client)),
Err(error) if error.raw_os_error() == Some(ERROR_PIPE_BUSY as i32) => {
tokio::time::sleep(Duration::from_millis(50)).await;
}
Err(error) => return Err(EndpointError::io(format!("connect to {address}"))(error)),
}
}
let busy = std::io::Error::from_raw_os_error(ERROR_PIPE_BUSY as i32);
Err(EndpointError::io(format!("connect to {address}"))(busy))
}

View File

@@ -0,0 +1,166 @@
//! The Windows calls behind the pipe: who this user is, a descriptor naming them alone, who
//! connected, and every process's parent.
//!
//! No safe API does any of it, so the `unsafe` lives here and nowhere else in the endpoint.
use std::collections::HashMap;
use std::io;
use std::os::windows::io::AsRawHandle;
use tokio::net::windows::named_pipe::{NamedPipeServer, ServerOptions};
use windows_sys::Win32::Foundation::{CloseHandle, LocalFree, HANDLE, INVALID_HANDLE_VALUE};
use windows_sys::Win32::Security::Authorization::{
ConvertSidToStringSidW, ConvertStringSecurityDescriptorToSecurityDescriptorW, SDDL_REVISION_1,
};
use windows_sys::Win32::Security::{
GetTokenInformation, TokenUser, PSECURITY_DESCRIPTOR, SECURITY_ATTRIBUTES, TOKEN_QUERY,
TOKEN_USER,
};
use windows_sys::Win32::System::Diagnostics::ToolHelp::{
CreateToolhelp32Snapshot, Process32FirstW, Process32NextW, PROCESSENTRY32W, TH32CS_SNAPPROCESS,
};
use windows_sys::Win32::System::Pipes::GetNamedPipeClientProcessId;
use windows_sys::Win32::System::Threading::{GetCurrentProcess, OpenProcessToken};
/// A security descriptor whose only entry grants this user everything, freed on drop.
pub(super) struct OwnerOnly(PSECURITY_DESCRIPTOR);
// SAFETY: the descriptor is immutable once built and only read by `CreateNamedPipeW`.
#[allow(unsafe_code)]
unsafe impl Send for OwnerOnly {}
// SAFETY: as above.
#[allow(unsafe_code)]
unsafe impl Sync for OwnerOnly {}
impl OwnerOnly {
/// `D:P(A;;GA;;;<this user>)`: a protected list, so nothing is inherited, with one entry.
#[allow(unsafe_code)]
pub(super) fn new() -> io::Result<Self> {
let sddl = format!("D:P(A;;GA;;;{})", current_user_sid()?);
let wide: Vec<u16> = sddl.encode_utf16().chain(std::iter::once(0)).collect();
let mut descriptor: PSECURITY_DESCRIPTOR = std::ptr::null_mut();
// SAFETY: `wide` is NUL-terminated and outlives the call; the descriptor it allocates is
// owned by the returned value and freed in `drop`.
let made = unsafe {
ConvertStringSecurityDescriptorToSecurityDescriptorW(
wide.as_ptr(),
SDDL_REVISION_1,
&mut descriptor,
std::ptr::null_mut(),
)
};
if made == 0 {
return Err(io::Error::last_os_error());
}
Ok(Self(descriptor))
}
/// A pipe instance at `address` carrying this descriptor.
#[allow(unsafe_code)]
pub(super) fn create(
&self,
options: &ServerOptions,
address: &str,
) -> io::Result<NamedPipeServer> {
let mut attributes = SECURITY_ATTRIBUTES {
nLength: std::mem::size_of::<SECURITY_ATTRIBUTES>() as u32,
lpSecurityDescriptor: self.0,
bInheritHandle: 0,
};
// SAFETY: `attributes` is a valid SECURITY_ATTRIBUTES pointing at a live descriptor this
// value owns, and both outlive the call.
unsafe {
options.create_with_security_attributes_raw(
address,
(&mut attributes as *mut SECURITY_ATTRIBUTES).cast(),
)
}
}
}
impl Drop for OwnerOnly {
#[allow(unsafe_code)]
fn drop(&mut self) {
// SAFETY: allocated by `ConvertStringSecurityDescriptorToSecurityDescriptorW`, freed once.
unsafe { LocalFree(self.0) };
}
}
/// This process's user, as a SID string (`S-1-5-21-…`).
#[allow(unsafe_code)]
fn current_user_sid() -> io::Result<String> {
let mut token: HANDLE = std::ptr::null_mut();
// SAFETY: the pseudo-handle needs no closing; `token` is closed below on every path.
if unsafe { OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &mut token) } == 0 {
return Err(io::Error::last_os_error());
}
let user = token_user(token);
// SAFETY: opened above and not used after this.
unsafe { CloseHandle(token) };
let user = user?;
// SAFETY: the buffer holds a TOKEN_USER written by `GetTokenInformation`, and is u64-aligned.
let sid = unsafe { (*user.as_ptr().cast::<TOKEN_USER>()).User.Sid };
let mut text: *mut u16 = std::ptr::null_mut();
// SAFETY: `sid` points into `user`, alive here; the string it allocates is freed below.
if unsafe { ConvertSidToStringSidW(sid, &mut text) } == 0 {
return Err(io::Error::last_os_error());
}
// SAFETY: `text` is a NUL-terminated string from the call above, freed once after copying.
let sid = unsafe {
let length = (0..).take_while(|&at| *text.add(at) != 0).count();
let copied = String::from_utf16_lossy(std::slice::from_raw_parts(text, length));
LocalFree(text.cast());
copied
};
Ok(sid)
}
/// The token's TOKEN_USER, in a buffer aligned for it.
#[allow(unsafe_code)]
fn token_user(token: HANDLE) -> io::Result<Vec<u64>> {
let mut size = 0u32;
// SAFETY: a size query, writing only `size`.
unsafe { GetTokenInformation(token, TokenUser, std::ptr::null_mut(), 0, &mut size) };
let mut buffer = vec![0u64; (size as usize).div_ceil(8)];
// SAFETY: `buffer` holds at least `size` bytes.
let read = unsafe {
GetTokenInformation(token, TokenUser, buffer.as_mut_ptr().cast(), size, &mut size)
};
if read == 0 {
return Err(io::Error::last_os_error());
}
Ok(buffer)
}
/// The process on the other end of a connected pipe.
#[allow(unsafe_code)]
pub(super) fn client_process_id(pipe: &NamedPipeServer) -> Option<u32> {
let mut pid = 0u32;
// SAFETY: the handle is the live, connected pipe, borrowed for the call.
let found = unsafe { GetNamedPipeClientProcessId(pipe.as_raw_handle() as HANDLE, &mut pid) };
(found != 0).then_some(pid)
}
/// Every running process's parent, from one snapshot; empty when Windows will not take one.
#[allow(unsafe_code)]
pub(in crate::local_endpoint) fn process_parents() -> HashMap<u32, u32> {
let mut parents = HashMap::new();
// SAFETY: a snapshot of processes, closed below.
let snapshot = unsafe { CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) };
if snapshot == INVALID_HANDLE_VALUE {
return parents;
}
let mut entry = PROCESSENTRY32W {
dwSize: std::mem::size_of::<PROCESSENTRY32W>() as u32,
..Default::default()
};
// SAFETY: `entry` is sized as the API requires and reused for each process.
let mut more = unsafe { Process32FirstW(snapshot, &mut entry) } != 0;
while more {
parents.insert(entry.th32ProcessID, entry.th32ParentProcessID);
// SAFETY: as above.
more = unsafe { Process32NextW(snapshot, &mut entry) } != 0;
}
// SAFETY: opened above and not used after this.
unsafe { CloseHandle(snapshot) };
parents
}

View File

@@ -173,6 +173,25 @@ pub fn list_directory(
Ok(DirectoryListing { path: normalized(relative), entries, omitted })
}
/// Whether an ignore file hides `folder`, or any folder on the way down to it from `top`.
///
/// Asked one level at a time, the way a listing sees it, so a rule in any `.gitignore` along the
/// way counts. Anything outside `top` reads as ignored.
pub(crate) fn is_ignored(top: &Path, folder: &Path) -> bool {
let Ok(relative) = folder.strip_prefix(top) else {
return true;
};
let mut parent = top.to_path_buf();
for part in relative.components() {
let name = part.as_os_str();
if !walk(&parent, true).any(|entry| entry.depth() == 1 && entry.file_name() == name) {
return true;
}
parent.push(part);
}
false
}
/// One level of `folder`, honoring the ignore files when `ignoring`, never `.git`.
fn walk(folder: &Path, ignoring: bool) -> impl Iterator<Item = ignore::DirEntry> {
ignore::WalkBuilder::new(folder)

View File

@@ -10,7 +10,7 @@
use std::collections::HashMap;
use std::fs;
use std::io::{self, BufRead, Write};
use std::io::{self, Write};
use std::path::PathBuf;
use std::time::SystemTime;
@@ -20,10 +20,12 @@ use uuid::Uuid;
use crate::event::ModelRef;
use crate::message::Message;
mod claim;
mod entry;
mod jsonl;
mod links;
pub use claim::SessionClaim;
pub use entry::{latest_leaf, Entry, EntryKind};
pub use jsonl::JsonlSessionStore;
pub use links::{linked_pull_requests, session_for_pull_request, session_issue, session_worktree};
@@ -87,6 +89,8 @@ pub enum SessionError {
NotFound(SessionId),
#[error("entry not found: {0}")]
EntryNotFound(EntryId),
#[error("session {0} is open in another Luxe")]
OpenElsewhere(SessionId),
#[error("{0}")]
Other(String),
}
@@ -115,6 +119,19 @@ pub trait SessionStore: Send + Sync {
/// Walk from leaf to root and reverse — this is resume.
fn active_path(&self, session: &SessionId, leaf: &EntryId) -> Result<Vec<Entry>, SessionError>;
/// Lends the entries from the root to `leaf` to `visit`, borrowed rather than cloned.
///
/// [`Self::active_path`] clones every entry on the path, tool output and the history a
/// compaction replaced included, which on a long session is tens of megabytes a call. A caller
/// that keeps a part of it borrows instead, through [`with_active_path`]. `visit` runs once,
/// and may run under the store's lock, so it must not block.
fn visit_active_path(
&self,
session: &SessionId,
leaf: &EntryId,
visit: &mut dyn FnMut(&[&Entry]),
) -> Result<(), SessionError>;
/// Append a compaction summary as the session's new leaf, returning its id.
///
/// `parent` is the entry this compaction *follows* — the current leaf — and `first_kept` names
@@ -155,6 +172,31 @@ pub trait SessionStore: Send + Sync {
///
/// Returns `(SessionId, mtime)` pairs so the dashboard can display age and sort consistently.
fn recent_sessions(&self, limit: usize) -> Vec<(SessionId, SystemTime)>;
/// Claim `session` for this process to write, so a second Luxe can't write it at once.
///
/// [`SessionError::OpenElsewhere`] when another process holds it. A store other processes
/// can't reach, like one in memory, has nothing to guard and returns `None`.
fn claim(&self, session: &SessionId) -> Result<Option<SessionClaim>, SessionError> {
let _ = session;
Ok(None)
}
}
/// What `read` makes of the active path to `leaf`, borrowing the entries rather than cloning
/// them: [`SessionStore::visit_active_path`] with a return value.
pub fn with_active_path<T>(
store: &(impl SessionStore + ?Sized),
session: &SessionId,
leaf: &EntryId,
read: impl FnOnce(&[&Entry]) -> T,
) -> Result<T, SessionError> {
let mut read = Some(read);
let mut result = None;
store.visit_active_path(session, leaf, &mut |path| {
result = read.take().map(|read| read(path));
})?;
result.ok_or_else(|| SessionError::Other("the session store never lent the path".to_string()))
}
#[cfg(test)]

View File

@@ -0,0 +1,107 @@
//! Session claims — one Luxe process writes a session's log at a time.
//!
//! Two processes appending to one log interleave entries that each parent on their own leaf, so a
//! resume walks one branch and silently drops the other's turns. A claim is an OS lock on a
//! `.lock` file beside the log: the system lets go of it when the process ends, however it ends,
//! so a crash never leaves a session stuck. Within one process the claim is counted instead,
//! since the TUI starts the next engine on a session before the last one has let go of it.
use std::collections::HashMap;
use std::fs::{File, OpenOptions, TryLockError};
use std::io;
use std::path::{Path, PathBuf};
use std::sync::{Mutex, MutexGuard, OnceLock};
/// This process's claim on one session, released when the last one for that session drops.
#[derive(Debug)]
pub struct SessionClaim {
lock_path: PathBuf,
}
/// The locks this process holds, by lock file, with how many claims share each.
struct Held {
_file: File,
claims: usize,
}
fn held() -> MutexGuard<'static, HashMap<PathBuf, Held>> {
static HELD: OnceLock<Mutex<HashMap<PathBuf, Held>>> = OnceLock::new();
// A panic mid-update leaves a count off by one at worst, which a restart clears
HELD.get_or_init(Default::default).lock().unwrap_or_else(std::sync::PoisonError::into_inner)
}
/// Claim the session whose lock file is `lock_path`. `None` when another process holds it.
pub(crate) fn claim(lock_path: &Path) -> io::Result<Option<SessionClaim>> {
let mut held = held();
if let Some(shared) = held.get_mut(lock_path) {
shared.claims += 1;
return Ok(Some(SessionClaim { lock_path: lock_path.to_path_buf() }));
}
let file = OpenOptions::new().create(true).truncate(false).write(true).open(lock_path)?;
match file.try_lock() {
Ok(()) => {}
Err(TryLockError::WouldBlock) => return Ok(None),
Err(TryLockError::Error(error)) => return Err(error),
}
held.insert(lock_path.to_path_buf(), Held { _file: file, claims: 1 });
Ok(Some(SessionClaim { lock_path: lock_path.to_path_buf() }))
}
impl Drop for SessionClaim {
fn drop(&mut self) {
let mut held = held();
let Some(shared) = held.get_mut(&self.lock_path) else { return };
shared.claims -= 1;
if shared.claims == 0 {
// Closing the file is what releases the lock; the empty file stays, since removing it
// would race a process that just opened it to lock
held.remove(&self.lock_path);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
/// Whether some other holder could take the lock right now, as another process would.
fn free_for_another(lock_path: &Path) -> bool {
let file = File::open(lock_path).expect("the lock file exists");
file.try_lock().is_ok()
}
/// The break this catches: a claim that takes no OS lock, so a second Luxe writes beside it.
#[test]
fn a_claim_keeps_other_holders_out_until_it_drops() {
let dir = tempfile::tempdir().expect("tempdir");
let path = dir.path().join("s.lock");
let claimed = claim(&path).expect("claim").expect("free");
assert!(!free_for_another(&path));
drop(claimed);
assert!(free_for_another(&path));
}
/// The break this catches: ignoring a lock someone else holds.
#[test]
fn a_session_held_elsewhere_is_refused() {
let dir = tempfile::tempdir().expect("tempdir");
let path = dir.path().join("s.lock");
let other = File::create(&path).expect("create");
other.try_lock().expect("the other holder locks first");
assert!(claim(&path).expect("claim").is_none());
}
/// The break this catches: releasing on the first drop, so the engine that's still running
/// loses its claim when the one it replaced shuts down.
#[test]
fn claims_within_one_process_are_counted() {
let dir = tempfile::tempdir().expect("tempdir");
let path = dir.path().join("s.lock");
let first = claim(&path).expect("claim").expect("free");
let second = claim(&path).expect("claim").expect("this process already holds it");
drop(first);
assert!(!free_for_another(&path), "still claimed by the second");
drop(second);
assert!(free_for_another(&path));
}
}

View File

@@ -2,6 +2,8 @@
use super::*;
mod cache;
/// In-memory index of sessions for fast lookup.
#[derive(Debug, Default)]
struct SessionIndex {
@@ -17,13 +19,20 @@ pub struct JsonlSessionStore {
index: SessionIndex,
/// Project hash for directory naming (derived from cwd).
project_hash: String,
/// The parsed entries of the session this store writes
cache: cache::SessionCache,
}
impl JsonlSessionStore {
/// Create a new store rooted at `root`, scanning for existing sessions.
pub fn new(root: impl Into<PathBuf>) -> Self {
let root = root.into();
let mut store = Self { root, index: SessionIndex::default(), project_hash: String::new() };
let mut store = Self {
root,
index: SessionIndex::default(),
project_hash: String::new(),
cache: cache::SessionCache::default(),
};
store.scan_for_sessions();
store
}
@@ -73,6 +82,13 @@ impl JsonlSessionStore {
self.index.sessions.keys().cloned().collect()
}
/// Path to a session's `.jsonl` file, or [`SessionError::NotFound`].
fn known_path(&self, session: &SessionId) -> Result<&PathBuf, SessionError> {
let (path, _) =
self.index.sessions.get(session).ok_or(SessionError::NotFound(session.clone()))?;
Ok(path)
}
/// Path to a session's `.jsonl` file, if the store knows the session.
pub fn session_path(&self, session: &SessionId) -> Option<PathBuf> {
self.index.sessions.get(session).map(|(path, _)| path.clone())
@@ -120,6 +136,7 @@ impl SessionStore for JsonlSessionStore {
writer.flush()?;
self.index.sessions.insert(session_id.clone(), (dir, Some(header.id.clone())));
self.cache.writing(&session_id);
Ok(session_id)
}
@@ -152,43 +169,28 @@ impl SessionStore for JsonlSessionStore {
}
fn load(&self, session: &SessionId) -> Result<Vec<Entry>, SessionError> {
let (path, _) =
self.index.sessions.get(session).ok_or(SessionError::NotFound(session.clone()))?;
let file = fs::File::open(path)?;
let reader = io::BufReader::new(file);
let mut entries = Vec::new();
for (line_num, line) in reader.lines().enumerate() {
let line = line?;
if line.is_empty() {
continue;
}
let entry: Entry = serde_json::from_str(&line)
.map_err(|e| SessionError::JsonParse { line: line_num + 1, error: e })?;
entries.push(entry);
}
Ok(entries)
let path = self.known_path(session)?;
self.cache.read(session, path, |entries| Ok(entries.to_vec()))
}
fn active_path(&self, session: &SessionId, leaf: &EntryId) -> Result<Vec<Entry>, SessionError> {
let entries = self.load(session)?;
let path = self.known_path(session)?;
self.cache.read(session, path, |entries| {
Ok(walk_to_root(entries, leaf)?.into_iter().cloned().collect())
})
}
let by_id: HashMap<&EntryId, &Entry> = entries.iter().map(|e| (&e.id, e)).collect();
let mut path = Vec::new();
let mut current_id: Option<&EntryId> = Some(leaf);
while let Some(id) = current_id {
let entry = by_id.get(id).ok_or(SessionError::EntryNotFound(id.clone()))?;
path.push((*entry).clone());
current_id = entry.parent_id.as_ref();
}
// Reverse to get root-to-leaf order.
path.reverse();
Ok(path)
fn visit_active_path(
&self,
session: &SessionId,
leaf: &EntryId,
visit: &mut dyn FnMut(&[&Entry]),
) -> Result<(), SessionError> {
let path = self.known_path(session)?;
self.cache.read(session, path, |entries| {
visit(&walk_to_root(entries, leaf)?);
Ok(())
})
}
fn list_sessions(&self) -> Vec<SessionId> {
@@ -205,4 +207,29 @@ impl SessionStore for JsonlSessionStore {
sorted.truncate(limit);
sorted
}
fn claim(&self, session: &SessionId) -> Result<Option<SessionClaim>, SessionError> {
let lock_path = self.session_dir(session).with_extension("lock");
match claim::claim(&lock_path)? {
Some(claimed) => {
self.cache.writing(session);
Ok(Some(claimed))
}
None => Err(SessionError::OpenElsewhere(session.clone())),
}
}
}
/// The entries from the root to `leaf`.
fn walk_to_root<'a>(entries: &'a [Entry], leaf: &EntryId) -> Result<Vec<&'a Entry>, SessionError> {
let by_id: HashMap<&EntryId, &Entry> = entries.iter().map(|e| (&e.id, e)).collect();
let mut path = Vec::new();
let mut current_id: Option<&EntryId> = Some(leaf);
while let Some(id) = current_id {
let entry = by_id.get(id).ok_or(SessionError::EntryNotFound(id.clone()))?;
path.push(*entry);
current_id = entry.parent_id.as_ref();
}
path.reverse();
Ok(path)
}

View File

@@ -0,0 +1,109 @@
//! Cache — the session a store writes, parsed once and then read on from where it left off.
//!
//! A long session's file runs to tens of megabytes, and the engine reads its active path on every
//! model call, so parsing it whole each time cost ~60ms a call on a 68 MB session. Only the session
//! this store writes is kept: the picker and the GitHub overview read every session, and keeping
//! those would hold all of them in memory.
use std::fs;
use std::io::{Read, Seek, SeekFrom};
use std::path::Path;
use std::sync::{Mutex, MutexGuard};
use super::super::{Entry, SessionError, SessionId};
/// The entries parsed so far and where in the file the parse stopped.
#[derive(Default)]
struct Parsed {
entries: Vec<Entry>,
bytes: u64,
lines: usize,
}
#[derive(Default)]
struct CacheState {
writing: Option<SessionId>,
parsed: Parsed,
}
/// The parsed entries of the session the store writes, refreshed from the file on each read.
#[derive(Default)]
pub(super) struct SessionCache {
state: Mutex<CacheState>,
}
impl SessionCache {
/// Marks `session` as the one this store writes, dropping another session's entries.
pub(super) fn writing(&self, session: &SessionId) {
let mut state = self.lock();
if state.writing.as_ref() != Some(session) {
*state = CacheState { writing: Some(session.clone()), parsed: Parsed::default() };
}
}
/// Calls `read` with every entry in `path`, parsing only what was added since the last call
/// when `session` is the one this store writes, and the whole file otherwise.
pub(super) fn read<T>(
&self,
session: &SessionId,
path: &Path,
read: impl FnOnce(&[Entry]) -> Result<T, SessionError>,
) -> Result<T, SessionError> {
let mut state = self.lock();
if state.writing.as_ref() != Some(session) {
drop(state);
let mut fresh = Parsed::default();
catch_up(&mut fresh, path)?;
return read(&fresh.entries);
}
catch_up(&mut state.parsed, path)?;
read(&state.parsed.entries)
}
fn lock(&self) -> MutexGuard<'_, CacheState> {
// the state is rebuilt from the file on the next read, so a panicked reader left nothing
// that matters
self.state.lock().unwrap_or_else(|poisoned| poisoned.into_inner())
}
}
/// Parses whatever `path` gained since `parsed` last read it. A file shorter than what was read
/// was replaced, so it's read again from the start.
fn catch_up(parsed: &mut Parsed, path: &Path) -> Result<(), SessionError> {
let length = fs::metadata(path)?.len();
if length < parsed.bytes {
*parsed = Parsed::default();
}
if length == parsed.bytes {
return Ok(());
}
let mut file = fs::File::open(path)?;
file.seek(SeekFrom::Start(parsed.bytes))?;
let mut tail = String::new();
file.read_to_string(&mut tail)?;
let (entries, lines) = parse_lines(&tail, parsed.lines)?;
parsed.entries.extend(entries);
parsed.lines += lines;
parsed.bytes += tail.len() as u64;
Ok(())
}
/// The entries in `text`, numbering lines on from `before` so an error names the file's line.
/// Returns them with how many lines `text` held.
fn parse_lines(text: &str, before: usize) -> Result<(Vec<Entry>, usize), SessionError> {
let mut entries = Vec::new();
let mut lines = 0;
for line in text.lines() {
lines += 1;
if line.is_empty() {
continue;
}
let entry = serde_json::from_str(line)
.map_err(|error| SessionError::JsonParse { line: before + lines, error })?;
entries.push(entry);
}
Ok((entries, lines))
}
#[cfg(test)]
mod tests;

View File

@@ -0,0 +1,138 @@
#![allow(clippy::unwrap_used)]
use super::super::super::{
EntryKind, JsonlSessionStore, Message, SessionError, SessionId, SessionStore,
};
use std::fs;
use std::path::Path;
fn store_at(root: &Path) -> JsonlSessionStore {
let mut store = JsonlSessionStore::new(root);
store.set_project_hash("project");
store
}
fn say(store: &mut JsonlSessionStore, session: &SessionId, words: &str) {
store.append(session, None, EntryKind::Message(Message::user(words))).unwrap();
}
/// Breaks the file's first line in place, keeping its length, so a store that parses the file
/// again fails and one reading on from where it stopped doesn't notice.
fn break_first_line(path: &Path) {
let mut bytes = fs::read(path).unwrap();
bytes[0] = b'x';
fs::write(path, bytes).unwrap();
}
// The break this catches: a cache that never looks at the file again, so another Luxe's entries
// (or a write this store didn't make) never show up
#[test]
fn entries_another_store_appends_are_read_on() {
let root = tempfile::tempdir().unwrap();
let mut mine = store_at(root.path());
let session = mine.create("/tmp").unwrap();
assert_eq!(mine.load(&session).unwrap().len(), 1);
say(&mut store_at(root.path()), &session, "from elsewhere");
assert_eq!(mine.load(&session).unwrap().len(), 2);
}
// The break this catches: the session being parsed whole on every read, ~60ms a model call on a
// long session, which is what the cache is for
#[test]
fn what_was_read_is_not_parsed_again() {
let root = tempfile::tempdir().unwrap();
let mut store = store_at(root.path());
let session = store.create("/tmp").unwrap();
store.load(&session).unwrap();
break_first_line(&store.session_dir(&session));
say(&mut store, &session, "after");
assert_eq!(store.load(&session).unwrap().len(), 2, "read on from where it stopped");
let fresh = store_at(root.path());
assert!(matches!(fresh.load(&session), Err(SessionError::JsonParse { line: 1, .. })));
}
// The break this catches: a resumed session, which this store claims rather than creates, parsed
// whole on every read
#[test]
fn a_claimed_session_is_kept() {
let root = tempfile::tempdir().unwrap();
let session = store_at(root.path()).create("/tmp").unwrap();
let store = store_at(root.path());
let _claim = store.claim(&session).unwrap();
store.load(&session).unwrap();
break_first_line(&store.session_dir(&session));
assert_eq!(store.load(&session).unwrap().len(), 1);
}
// The break this catches: a switch to another session keeping the first one cached, so the
// session now being written is parsed whole on every read
#[test]
fn switching_sessions_keeps_the_new_one() {
let root = tempfile::tempdir().unwrap();
let mut store = store_at(root.path());
let first = store.create("/tmp").unwrap();
store.load(&first).unwrap();
let second = store.create("/tmp").unwrap();
store.load(&second).unwrap();
break_first_line(&store.session_dir(&second));
assert_eq!(store.load(&second).unwrap().len(), 1);
}
// The break this catches: a file replaced by a shorter one served from the old entries, or read
// on from an offset past its end
#[test]
fn a_shorter_file_is_read_again_from_the_start() {
let root = tempfile::tempdir().unwrap();
let mut store = store_at(root.path());
let session = store.create("/tmp").unwrap();
let path = store.session_dir(&session);
let header = fs::read_to_string(&path).unwrap();
say(&mut store, &session, "dropped");
assert_eq!(store.load(&session).unwrap().len(), 2);
fs::write(&path, header).unwrap();
assert_eq!(store.load(&session).unwrap().len(), 1);
}
// The break this catches: every session the picker reads kept in memory, or one served from
// the cache though this store doesn't write it
#[test]
fn a_session_this_store_does_not_write_is_read_fresh() {
let root = tempfile::tempdir().unwrap();
let other = store_at(root.path()).create("/tmp").unwrap();
let mut store = store_at(root.path());
store.create("/tmp").unwrap();
store.load(&other).unwrap();
break_first_line(&store.session_dir(&other));
assert!(matches!(store.load(&other), Err(SessionError::JsonParse { line: 1, .. })));
}
// The break this catches: a bad line found while reading on reported by its place in the new
// part rather than in the file, pointing at the wrong line
#[test]
fn a_bad_line_read_later_names_its_line_in_the_file() {
let root = tempfile::tempdir().unwrap();
let mut store = store_at(root.path());
let session = store.create("/tmp").unwrap();
say(&mut store, &session, "fine");
store.load(&session).unwrap();
let path = store.session_dir(&session);
let mut text = fs::read_to_string(&path).unwrap();
text.push_str("not json\n");
fs::write(&path, text).unwrap();
assert!(matches!(store.load(&session), Err(SessionError::JsonParse { line: 3, .. })));
}
// The break this catches: the walk from a leaf served from the cache going stale, so a request
// built after an append is missing the newest entry
#[test]
fn the_active_path_includes_what_was_just_appended() {
let root = tempfile::tempdir().unwrap();
let mut store = store_at(root.path());
let session = store.create("/tmp").unwrap();
let header = store.load(&session).unwrap()[0].id.clone();
let leaf =
store.append(&session, Some(header), EntryKind::Message(Message::user("now"))).unwrap();
let path = store.active_path(&session, &leaf).unwrap();
assert_eq!(path.last().map(|entry| &entry.id), Some(&leaf));
assert_eq!(path.len(), 2);
}

View File

@@ -119,6 +119,46 @@ fn active_path_reconstructs_conversation() {
fs::remove_dir_all(root).ok();
}
/// The break this catches: lending the whole log, or the walk unreversed, rather than the branch
/// from the root to the leaf, which a request built from it would send as the conversation.
#[test]
fn with_active_path_lends_only_the_branch_to_the_leaf() {
let root = temp_root();
let mut store = JsonlSessionStore::new(&root);
store.set_project_hash("test-project");
let session = store.create("/tmp/test").unwrap();
let header = store.load(&session).unwrap()[0].id.clone();
let question = Message::user("q");
let user = store.append(&session, Some(header.clone()), EntryKind::Message(question)).unwrap();
let other = EntryKind::Message(Message::assistant("other"));
store.append(&session, Some(user.clone()), other).unwrap();
let kept = EntryKind::Message(Message::assistant("kept"));
let leaf = store.append(&session, Some(user.clone()), kept).unwrap();
let lent = with_active_path(&store, &session, &leaf, |path| {
path.iter().map(|entry| entry.id.clone()).collect::<Vec<_>>()
});
assert_eq!(lent.unwrap(), vec![header, user, leaf]);
fs::remove_dir_all(root).ok();
}
/// The break this catches: a leaf the log lacks lending a shortened path instead of failing, so a
/// request would go out without the conversation behind it.
#[test]
fn with_active_path_refuses_a_leaf_the_log_lacks() {
let root = temp_root();
let mut store = JsonlSessionStore::new(&root);
store.set_project_hash("test-project");
let session = store.create("/tmp/test").unwrap();
let missing = EntryId::new();
let lent = with_active_path(&store, &session, &missing, |path| path.len());
assert!(matches!(&lent, Err(SessionError::EntryNotFound(id)) if *id == missing), "{lent:?}");
fs::remove_dir_all(root).ok();
}
#[test]
fn branching_two_children() {
let root = temp_root();

View File

@@ -11,6 +11,8 @@ use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::Path;
pub mod css;
/// A parsed color value: RGB, ANSI-indexed, or terminal default.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ColorValue {

View File

@@ -0,0 +1,72 @@
//! Theme CSS — a theme's colors as CSS custom properties.
//!
//! The website and the phone app are drawn in the terminal's colors, and stay that way because
//! neither keeps a copy: both read the theme through here. `accentSecondary` is published as
//! `--luxe-accent-secondary`.
use super::ColorValue;
use std::collections::HashMap;
use std::fmt::Write as _;
/// A token's name as CSS spells it: `accentSecondary` is `accent-secondary`.
pub fn css_name(token: &str) -> String {
let mut out = String::with_capacity(token.len() + 4);
for (index, character) in token.char_indices() {
if character.is_ascii_uppercase() && index > 0 {
out.push('-');
}
out.push(character.to_ascii_lowercase());
}
out
}
/// A color as CSS hex. An ANSI index or the terminal's default has no fixed color, so none.
pub fn hex(value: &ColorValue) -> Option<String> {
match value {
ColorValue::Rgb(red, green, blue) => Some(format!("#{red:02x}{green:02x}{blue:02x}")),
_ => None,
}
}
/// One `--luxe-*` line for each of `tokens` that `theme` gives a color, in that order.
///
/// A name that isn't plain letters and digits is skipped: theme files can be the user's own, and
/// the name lands inside a stylesheet.
pub fn custom_properties<'a>(
theme: &HashMap<String, ColorValue>,
tokens: impl IntoIterator<Item = &'a str>,
) -> String {
let mut out = String::new();
for token in tokens {
if !token.chars().all(|character| character.is_ascii_alphanumeric()) {
continue;
}
if let Some(color) = theme.get(token).and_then(hex) {
let _ = writeln!(out, " --luxe-{}: {color};", css_name(token));
}
}
out
}
/// A stylesheet for a page drawn in `theme`: every token with a color, by name, and the color
/// scheme its background calls for, so form controls and scrollbars match.
pub fn stylesheet(theme: &HashMap<String, ColorValue>) -> String {
let mut tokens: Vec<&str> = theme.keys().map(String::as_str).collect();
tokens.sort_unstable();
let scheme = if is_light(theme.get("background")) { "light" } else { "dark" };
format!(":root {{\n color-scheme: {scheme};\n{}}}\n", custom_properties(theme, tokens))
}
/// Whether a background is light enough to want dark text, by its luminance. A color that isn't
/// RGB counts as dark, the terminal default it most likely is.
pub fn is_light(background: Option<&ColorValue>) -> bool {
let Some(ColorValue::Rgb(red, green, blue)) = background else {
return false;
};
let luminance =
0.2126 * f32::from(*red) + 0.7152 * f32::from(*green) + 0.0722 * f32::from(*blue);
luminance > 127.5
}
#[cfg(test)]
mod tests;

View File

@@ -0,0 +1,60 @@
use super::*;
use crate::theme::ThemeRegistry;
fn theme(name: &str) -> HashMap<String, ColorValue> {
ThemeRegistry::new().resolve(name).expect("a built-in theme")
}
/// Token names arrive in the shape a stylesheet expects.
///
/// The break this catches: a changed spelling, which renames every property the website and the
/// app style themselves with, leaving them on their fallbacks.
#[test]
fn token_names_become_kebab_case() {
assert_eq!(css_name("accentSecondary"), "accent-secondary");
assert_eq!(css_name("background"), "background");
assert_eq!(css_name("diffAddBg"), "diff-add-bg");
}
/// Every token with a color is published, under its CSS name, with its hex.
///
/// The break this catches: publishing only a fixed list, so a token the app styles with, such as
/// a diff or thinking color, is missing and falls back to a color from no theme.
#[test]
fn the_stylesheet_publishes_every_colored_token() {
let luxe = theme("luxe");
let css = stylesheet(&luxe);
for (token, value) in &luxe {
let Some(color) = hex(value) else { continue };
let line = format!("--luxe-{}: {color};", css_name(token));
assert!(css.contains(&line), "{line} is missing");
}
assert!(css.starts_with(":root {\n"));
}
/// A light theme asks for light form controls, and a dark one for dark.
///
/// The break this catches: a fixed scheme, so a light theme gets dark scrollbars and inputs, or
/// the other way round.
#[test]
fn the_scheme_follows_the_background() {
let light = HashMap::from([("background".to_string(), ColorValue::Rgb(250, 248, 245))]);
let dark = HashMap::from([("background".to_string(), ColorValue::Rgb(18, 16, 14))]);
assert!(stylesheet(&light).contains("color-scheme: light;"));
assert!(stylesheet(&dark).contains("color-scheme: dark;"));
}
/// A token whose name could break out of the stylesheet is left out.
///
/// The break this catches: writing a user theme's key as it is, so a name with `;` or `}` adds
/// rules of its own to the page.
#[test]
fn only_plain_token_names_are_published() {
let theme = HashMap::from([
("accent".to_string(), ColorValue::Rgb(1, 2, 3)),
("x:1}body{display:none".to_string(), ColorValue::Rgb(4, 5, 6)),
]);
let css = stylesheet(&theme);
assert!(css.contains("--luxe-accent: #010203;"));
assert!(!css.contains("display"), "{css}");
}

View File

@@ -14,6 +14,7 @@
//! [`restore_after_compaction`] covers the one moment an in-place load could be lost: a summary that
//! replaces the call it hung on.
use std::borrow::Borrow;
use std::collections::{BTreeSet, HashMap};
use crate::message::{ContentBlock, Message, Role, ToolOutput};
@@ -68,8 +69,8 @@ pub fn activated_tools<'a>(messages: impl IntoIterator<Item = &'a Message>) -> B
///
/// Reads the whole path, including what a compaction replaced, because a tool loaded before the
/// summary is still loaded after it ([`restore_after_compaction`] sees to that on the wire).
pub fn activated_in(entries: &[Entry]) -> BTreeSet<String> {
activated_tools(entries.iter().filter_map(|entry| match &entry.kind {
pub fn activated_in<E: Borrow<Entry>>(entries: &[E]) -> BTreeSet<String> {
activated_tools(entries.iter().filter_map(|entry| match &entry.borrow().kind {
EntryKind::Message(message) => Some(message),
_ => None,
}))
@@ -90,7 +91,8 @@ pub fn activated_in(entries: &[Entry]) -> BTreeSet<String> {
/// the summaries whenever there are any: the newest last, behind any older one a newer cut left on
/// the kept side. The exchange goes behind the newest. Does nothing without a compaction or
/// without activations.
pub fn restore_after_compaction(entries: &[Entry], messages: &mut Vec<Message>) {
pub fn restore_after_compaction<E: Borrow<Entry>>(entries: &[E], messages: &mut Vec<Message>) {
let entries: Vec<&Entry> = entries.iter().map(Borrow::borrow).collect();
let Some(cut) =
entries.iter().rposition(|entry| matches!(entry.kind, EntryKind::Compaction { .. }))
else {

View File

@@ -105,6 +105,30 @@ fn legacy_luxe_config_dir() -> Option<PathBuf> {
legacy_config(env)
}
/// Where a running Luxe leaves the way in to its session: `$LUXE_RUNTIME_DIR`, else `luxe` under a
/// folder only this user can open.
///
/// That is `$XDG_RUNTIME_DIR` on Linux and `$TMPDIR` on macOS, both private to the user. Linux's
/// `$TMPDIR`, when set at all, is usually the shared `/tmp`, so it is never used; without a private
/// folder, and on Windows, it is `run` under [`luxe_state_dir`].
pub fn luxe_runtime_dir() -> PathBuf {
runtime_in(env, cfg!(target_os = "macos")).unwrap_or_else(|| luxe_state_dir().join("run"))
}
/// [`luxe_runtime_dir`] under the lookup `var`, or `None` for the state-directory fallback.
fn runtime_in(var: impl Fn(&str) -> Option<String>, macos: bool) -> Option<PathBuf> {
let var = |name: &str| var(name).filter(|value| !value.trim().is_empty());
if let Some(explicit) = var("LUXE_RUNTIME_DIR") {
return Some(PathBuf::from(explicit));
}
let private = match macos {
true => var("TMPDIR"),
false if cfg!(windows) => None,
false => var("XDG_RUNTIME_DIR"),
};
Some(PathBuf::from(private?).join("luxe"))
}
/// Luxe's own cache directory, `luxe` under [`cache_dir`].
pub fn luxe_cache_dir() -> PathBuf {
cache_dir().join("luxe")

View File

@@ -23,6 +23,21 @@ fn a_luxe_variable_names_the_directory_itself() {
assert_eq!(dir(&other, None), None, "nowhere named, nowhere to look");
}
/// The runtime folder is one only this user can open: Linux's runtime directory, macOS's temp
/// directory, and never Linux's `$TMPDIR`, which is usually the shared `/tmp`.
#[test]
#[cfg(unix)]
fn the_runtime_folder_is_private_to_the_user() {
let linux = [("XDG_RUNTIME_DIR", "/run/user/1000"), ("TMPDIR", "/tmp")];
let runtime = |pairs: &[(&str, &str)], macos| runtime_in(env_of(pairs), macos);
assert_eq!(runtime(&linux, false), Some(PathBuf::from("/run/user/1000/luxe")));
assert_eq!(runtime(&[("TMPDIR", "/tmp")], false), None, "the shared /tmp is never it");
let mac = [("TMPDIR", "/var/folders/x/T/"), ("XDG_RUNTIME_DIR", "/elsewhere")];
assert_eq!(runtime(&mac, true), Some(PathBuf::from("/var/folders/x/T/luxe")));
let named = [("LUXE_RUNTIME_DIR", "/chosen"), ("XDG_RUNTIME_DIR", "/run/user/1000")];
assert_eq!(runtime(&named, false), Some(PathBuf::from("/chosen")), "the Luxe variable wins");
}
/// The old resolver: the XDG variable, then `HOME`, never the Windows folders, and nothing to
/// take over when `LUXE_CONFIG_DIR` was in charge then too, or when there was no home.
#[test]

View File

@@ -219,10 +219,15 @@ fn walk_threads() -> usize {
///
/// A pattern with no `/` describes a *file name* (`*.rs`); one with a `/` describes a path
/// (`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.
/// `*` does not, which is the dialect every tool description in this repo promises. A leading `!`
/// excludes what the rest matches, as `rg -g '!target/**'` does.
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 (excludes, pattern) = match pattern.strip_prefix('!') {
Some(rest) => (true, rest),
None => (false, pattern.as_str()),
};
let spans_directories = pattern.contains('/');
let compiled =
if spans_directories { Glob::new(&anchor(pattern))? } else { Glob::new(pattern)? };
@@ -231,6 +236,7 @@ pub fn matcher(pattern: &str) -> Result<PathMatcher, globset::Error> {
// Compiled with `literal_separator` off in both cases, because `*.rs` has to match a bare
// file name and `**/src/*.rs` needs `**` to cross separators.
against_name_only: !spans_directories,
excludes,
})
}
@@ -238,11 +244,18 @@ pub fn matcher(pattern: &str) -> Result<PathMatcher, globset::Error> {
pub struct PathMatcher {
matcher: GlobMatcher,
against_name_only: bool,
/// The pattern began with `!`: a match is a path to leave out
excludes: bool,
}
impl PathMatcher {
/// Whether `path` (project-relative) matches.
pub fn is_match(&self, path: &Path) -> bool {
self.glob_matches(path) != self.excludes
}
/// Whether the glob itself, without a leading `!`, matches `path`.
fn glob_matches(&self, path: &Path) -> bool {
if self.against_name_only {
path.file_name().is_some_and(|name| self.matcher.is_match(Path::new(name)))
} else {
@@ -266,145 +279,4 @@ fn anchor(pattern: &str) -> String {
#[cfg(test)]
#[allow(clippy::unwrap_used)]
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() {
let dir = repo();
let walk = |limit| walk_within(dir.path(), Path::new("."), (true, None), || false, limit);
let walked = walk(2);
assert_eq!(walked.files.len(), 2);
assert!(walked.capped);
let whole = walk(100);
assert!(!whole.capped, "{:?}", whole.files);
}
/// An index walk that stops at its limit keeps the same files every time: the first by name.
#[test]
fn a_capped_index_walk_keeps_the_first_files_by_name() {
let dir = tempfile::tempdir().unwrap();
for index in 0..40 {
let sub = dir.path().join(format!("d{}", index % 4));
std::fs::create_dir_all(&sub).unwrap();
std::fs::write(sub.join(format!("f{index:02}.rs")), "").unwrap();
}
std::fs::create_dir_all(dir.path().join(".hidden")).unwrap();
std::fs::write(dir.path().join(".hidden/a.rs"), "").unwrap();
let walk = || index_files(dir.path(), &[], |path| path.extension().is_some(), 10);
let first = walk();
assert!(first.capped);
assert_eq!(first.files.len(), 10);
assert!(first.files.iter().all(|path| path.starts_with("d0") || path.starts_with("d1")));
assert_eq!(first.files, walk().files, "the same ten each time");
assert!(!first.files.iter().any(|path| path.starts_with(".hidden")));
}
/// A listing keeps hidden files, honors the ignore file, and never enters a skipped folder,
/// ignored or not.
#[test]
fn a_listing_keeps_dotfiles_and_skips_the_named_folders() {
let dir = repo();
std::fs::create_dir_all(dir.path().join(".github")).unwrap();
std::fs::write(dir.path().join(".github/ci.yml"), "").unwrap();
std::fs::create_dir_all(dir.path().join("vendor")).unwrap();
std::fs::write(dir.path().join("vendor/lib.rs"), "").unwrap();
let listed = listing(dir.path(), &["vendor"]);
assert!(!listed.capped);
let dotfile = Path::new(".github").join("ci.yml");
assert!(listed.files.contains(&dotfile), "{:?}", listed.files);
assert!(listed.files.iter().all(|path| !path.starts_with("vendor")));
assert!(listed.files.iter().all(|path| !path.starts_with("target")));
}
/// A listing that stops at its limit keeps the same files every time: the first by name.
#[test]
fn a_capped_listing_keeps_the_first_files_by_name() {
let dir = tempfile::tempdir().unwrap();
for index in 0..40 {
let sub = dir.path().join(format!("d{}", index % 4));
std::fs::create_dir_all(&sub).unwrap();
std::fs::write(sub.join(format!("f{index:02}.rs")), "").unwrap();
}
let first = listing_within(dir.path(), &[], 10);
assert!(first.capped);
assert_eq!(first.files.len(), 10);
assert!(first.files.iter().all(|path| path.starts_with("d0")), "{:?}", first.files);
assert_eq!(first.files, listing_within(dir.path(), &[], 10).files);
}
/// A repo with one real source file and a build directory full of look-alikes.
fn repo() -> tempfile::TempDir {
let dir = tempfile::tempdir().unwrap();
std::fs::write(dir.path().join(".gitignore"), "target/\n").unwrap();
std::fs::create_dir_all(dir.path().join("src").join("deep")).unwrap();
std::fs::write(dir.path().join("src").join("main.rs"), "fn main() {}").unwrap();
std::fs::write(dir.path().join("src").join("deep").join("nested.rs"), "").unwrap();
std::fs::create_dir_all(dir.path().join("target").join("debug")).unwrap();
std::fs::write(dir.path().join("target").join("debug").join("gen.rs"), "").unwrap();
std::fs::create_dir_all(dir.path().join(".git")).unwrap();
std::fs::write(dir.path().join(".git").join("config"), "").unwrap();
dir
}
fn names(paths: &[PathBuf]) -> Vec<String> {
paths.iter().map(|path| crate::paths::display(path)).collect()
}
#[test]
fn the_walk_honors_ignore_files_and_never_enters_dot_git() {
let dir = repo();
let found = names(&files(dir.path(), Path::new("."), false));
assert!(found.contains(&"src/main.rs".to_string()), "{found:?}");
assert!(found.contains(&"src/deep/nested.rs".to_string()), "{found:?}");
assert!(!found.iter().any(|path| path.contains("target/")), "ignored: {found:?}");
assert!(
!found.iter().any(|path| path.contains(".git/")),
"never the object store: {found:?}"
);
}
#[test]
fn all_reaches_ignored_files_but_still_not_dot_git() {
let dir = repo();
let found = names(&files(dir.path(), Path::new("."), true));
assert!(found.contains(&"target/debug/gen.rs".to_string()), "{found:?}");
assert!(!found.iter().any(|path| path.contains(".git/")), "{found:?}");
}
/// The bug the `find` fallback shipped with: `**/` means *zero or more* directories, so
/// `src/**/*.rs` has to match `src/main.rs` — a file sitting directly under the globbed
/// directory, which is the common case.
#[test]
fn a_directory_spanning_glob_matches_zero_intermediate_directories() {
let glob = matcher("src/**/*.rs").unwrap();
assert!(glob.is_match(Path::new("src/main.rs")), "zero directories in between");
assert!(glob.is_match(Path::new("src/deep/nested.rs")), "and any number of them");
assert!(glob.is_match(Path::new("crates/x/src/main.rs")), "anchored to match at any depth");
assert!(!glob.is_match(Path::new("tests/main.rs")));
}
#[test]
fn a_bare_pattern_matches_the_file_name_at_any_depth() {
let glob = matcher("*.rs").unwrap();
assert!(glob.is_match(Path::new("main.rs")));
assert!(glob.is_match(Path::new("src/deep/nested.rs")));
assert!(!glob.is_match(Path::new("src/main.py")));
}
#[test]
fn a_malformed_glob_is_an_error_rather_than_a_silent_no_match() {
assert!(matcher("src/[").is_err());
}
}
mod tests;

View File

@@ -0,0 +1,152 @@
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 leading `!` excludes what the rest of the pattern matches, the way `rg -g` reads it.
///
/// The break this catches: `!` read as a literal character, so `!.pi/**` matched only paths
/// under a folder named `!.pi` and a grep with it found nothing at all.
#[test]
fn a_leading_bang_excludes_what_the_rest_matches() {
let folder = matcher("!.pi/**").unwrap();
assert!(!folder.is_match(Path::new(".pi/notes.md")));
assert!(!folder.is_match(Path::new("sub/.pi/notes.md")), "at any depth, like the rest");
assert!(folder.is_match(Path::new("src/main.rs")));
let name = matcher("!*.md").unwrap();
assert!(!name.is_match(Path::new("docs/guide.md")));
assert!(name.is_match(Path::new("docs/build.rs")));
}
/// 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() {
let dir = repo();
let walk = |limit| walk_within(dir.path(), Path::new("."), (true, None), || false, limit);
let walked = walk(2);
assert_eq!(walked.files.len(), 2);
assert!(walked.capped);
let whole = walk(100);
assert!(!whole.capped, "{:?}", whole.files);
}
/// An index walk that stops at its limit keeps the same files every time: the first by name.
#[test]
fn a_capped_index_walk_keeps_the_first_files_by_name() {
let dir = tempfile::tempdir().unwrap();
for index in 0..40 {
let sub = dir.path().join(format!("d{}", index % 4));
std::fs::create_dir_all(&sub).unwrap();
std::fs::write(sub.join(format!("f{index:02}.rs")), "").unwrap();
}
std::fs::create_dir_all(dir.path().join(".hidden")).unwrap();
std::fs::write(dir.path().join(".hidden/a.rs"), "").unwrap();
let walk = || index_files(dir.path(), &[], |path| path.extension().is_some(), 10);
let first = walk();
assert!(first.capped);
assert_eq!(first.files.len(), 10);
assert!(first.files.iter().all(|path| path.starts_with("d0") || path.starts_with("d1")));
assert_eq!(first.files, walk().files, "the same ten each time");
assert!(!first.files.iter().any(|path| path.starts_with(".hidden")));
}
/// A listing keeps hidden files, honors the ignore file, and never enters a skipped folder,
/// ignored or not.
#[test]
fn a_listing_keeps_dotfiles_and_skips_the_named_folders() {
let dir = repo();
std::fs::create_dir_all(dir.path().join(".github")).unwrap();
std::fs::write(dir.path().join(".github/ci.yml"), "").unwrap();
std::fs::create_dir_all(dir.path().join("vendor")).unwrap();
std::fs::write(dir.path().join("vendor/lib.rs"), "").unwrap();
let listed = listing(dir.path(), &["vendor"]);
assert!(!listed.capped);
let dotfile = Path::new(".github").join("ci.yml");
assert!(listed.files.contains(&dotfile), "{:?}", listed.files);
assert!(listed.files.iter().all(|path| !path.starts_with("vendor")));
assert!(listed.files.iter().all(|path| !path.starts_with("target")));
}
/// A listing that stops at its limit keeps the same files every time: the first by name.
#[test]
fn a_capped_listing_keeps_the_first_files_by_name() {
let dir = tempfile::tempdir().unwrap();
for index in 0..40 {
let sub = dir.path().join(format!("d{}", index % 4));
std::fs::create_dir_all(&sub).unwrap();
std::fs::write(sub.join(format!("f{index:02}.rs")), "").unwrap();
}
let first = listing_within(dir.path(), &[], 10);
assert!(first.capped);
assert_eq!(first.files.len(), 10);
assert!(first.files.iter().all(|path| path.starts_with("d0")), "{:?}", first.files);
assert_eq!(first.files, listing_within(dir.path(), &[], 10).files);
}
/// A repo with one real source file and a build directory full of look-alikes.
fn repo() -> tempfile::TempDir {
let dir = tempfile::tempdir().unwrap();
std::fs::write(dir.path().join(".gitignore"), "target/\n").unwrap();
std::fs::create_dir_all(dir.path().join("src").join("deep")).unwrap();
std::fs::write(dir.path().join("src").join("main.rs"), "fn main() {}").unwrap();
std::fs::write(dir.path().join("src").join("deep").join("nested.rs"), "").unwrap();
std::fs::create_dir_all(dir.path().join("target").join("debug")).unwrap();
std::fs::write(dir.path().join("target").join("debug").join("gen.rs"), "").unwrap();
std::fs::create_dir_all(dir.path().join(".git")).unwrap();
std::fs::write(dir.path().join(".git").join("config"), "").unwrap();
dir
}
fn names(paths: &[PathBuf]) -> Vec<String> {
paths.iter().map(|path| crate::paths::display(path)).collect()
}
#[test]
fn the_walk_honors_ignore_files_and_never_enters_dot_git() {
let dir = repo();
let found = names(&files(dir.path(), Path::new("."), false));
assert!(found.contains(&"src/main.rs".to_string()), "{found:?}");
assert!(found.contains(&"src/deep/nested.rs".to_string()), "{found:?}");
assert!(!found.iter().any(|path| path.contains("target/")), "ignored: {found:?}");
assert!(!found.iter().any(|path| path.contains(".git/")), "never the object store: {found:?}");
}
#[test]
fn all_reaches_ignored_files_but_still_not_dot_git() {
let dir = repo();
let found = names(&files(dir.path(), Path::new("."), true));
assert!(found.contains(&"target/debug/gen.rs".to_string()), "{found:?}");
assert!(!found.iter().any(|path| path.contains(".git/")), "{found:?}");
}
/// The bug the `find` fallback shipped with: `**/` means *zero or more* directories, so
/// `src/**/*.rs` has to match `src/main.rs` — a file sitting directly under the globbed
/// directory, which is the common case.
#[test]
fn a_directory_spanning_glob_matches_zero_intermediate_directories() {
let glob = matcher("src/**/*.rs").unwrap();
assert!(glob.is_match(Path::new("src/main.rs")), "zero directories in between");
assert!(glob.is_match(Path::new("src/deep/nested.rs")), "and any number of them");
assert!(glob.is_match(Path::new("crates/x/src/main.rs")), "anchored to match at any depth");
assert!(!glob.is_match(Path::new("tests/main.rs")));
}
#[test]
fn a_bare_pattern_matches_the_file_name_at_any_depth() {
let glob = matcher("*.rs").unwrap();
assert!(glob.is_match(Path::new("main.rs")));
assert!(glob.is_match(Path::new("src/deep/nested.rs")));
assert!(!glob.is_match(Path::new("src/main.py")));
}
#[test]
fn a_malformed_glob_is_an_error_rather_than_a_silent_no_match() {
assert!(matcher("src/[").is_err());
}