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:
@@ -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.
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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)| {
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
///
|
||||
|
||||
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user